hdu 1757 A Simple Math Problem(矩阵快速幂乘法)
Lele now is thinking about a simple function f(x). If x < f(x) = x.
If x >= f(x) = a0 * f(x-) + a1 * f(x-) + a2 * f(x-) + …… + a9 * f(x-);
And ai(<=i<=) can only be or . Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
The problem contains mutiple test cases.Please process to the end of file.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<*^ , m < ^ )
In the second line , there are ten integers represent a0 ~ a9.
For each case, output f(k) % m in one line.

把问题转化为求矩阵的n-9次幂就行了;
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int k,MOD;
int a[];
int f[];
struct Matrix
{
int m[][];
}matrix; Matrix Mul(Matrix a,Matrix b)
{
Matrix res;
int i,j,k;
for(i=;i<;i++)
{
for(j=;j<;j++)
{
res.m[i][j] = ;
for(k=;k<;k++)
res.m[i][j] = (res.m[i][j]+(a.m[i][k]*b.m[k][j]))%MOD;
}
}
return res;
} Matrix fastm(Matrix a,int b)
{
Matrix res;
memset(res.m,,sizeof(res.m));
for(int i=;i<;i++)
res.m[i][i] = ;
while(b)
{
if(b&)
res = Mul(res,a);
a = Mul(a,a);
b >>= ;
}
return res;
}
void init()
{
for(int i=;i<=;i++)
{
f[i]=i;
}
}
int main()
{
init();
while(scanf("%d%d",&k,&MOD)==)
{
for(int i=;i<=;i++)
{
scanf("%d",&a[i]);
}
if(k<)
{
printf("%d\n",k%MOD);
continue;
} memset(matrix.m,,sizeof(matrix.m));
for(int i=;i<=;i++)
matrix.m[][i]=a[i];
for(int i=;i<=;i++)
matrix.m[i][i-] = ;
Matrix ans=fastm(matrix,k-);
Matrix cnt;
for(int i=;i<;i++)
{
cnt.m[i][]=f[-i];
}
Matrix p=Mul(ans,cnt);
printf("%d\n",p.m[][]%MOD);
}
return ;
}
hdu 1757 A Simple Math Problem(矩阵快速幂乘法)的更多相关文章
- HDU 1757 A Simple Math Problem (矩阵快速幂)
题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...
- hdu 1757 A Simple Math Problem_矩阵快速幂
题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...
- HDU 1757 A Simple Math Problem(矩阵)
A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- A Simple Math Problem(矩阵快速幂)----------------------蓝桥备战系列
Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 f(x) = a0 ...
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- HDU 1757 A Simple Math Problem(矩阵快速幂)
题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...
- hdu 1757 A Simple Math Problem (矩阵快速幂)
Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x >= 10 ...
- hdu 1757 A Simple Math Problem (矩阵快速幂,简单)
题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...
- HDU 1757 A Simple Math Problem( 矩阵快速幂 )
<font color = red , size = '4'>下列图表转载自 efreet 链接:传送门 题意:给出递推关系,求 f(k) % m 的值, 思路: 因为 k<2 * ...
随机推荐
- SVN trunk、branch、tag的用法
Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是svn://proj/|+-trunk+-branches+-tag ...
- hdu 5630 Rikka with Chess
来自官方题解: AC代码: #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream ...
- freemarker声明变量
freemarker声明变量 1.使用assign创建和替换变量 (1)新建声明变量的ftl variable.ftl: <html> <head> <meta http ...
- WebService应用一例,带有安全验证
1.创建WEB项目,添加WEB服务WebService1.asmx,代码如下: using System; using System.Collections.Generic; using System ...
- 深入char、varchar、text和nchar、nvarchar、ntext的区别详解
很多开发者进行数据库设计的时候往往并没有太多的考虑char, varchar类型,有的是根本就没注意,因为存储价格变得越来越便宜了,忘记了最开始的一些基本设计理论和原则,这点让我想到了现在的年轻人,大 ...
- theano安装
theano安装有两类方法,一种是自己一步步安装,还有一种是借助其他软件安装.我是安装到一半发现第二种方法的...........所以就用的第一种麻烦的办法装的,但是过程也是一种学习. 电脑:win7 ...
- 【Lucene4.8教程之三】搜索
1.关键类 Lucene的搜索过程中涉及的主要类有以下几个: (1)IndexSearcher:执行search()方法的类 (2)IndexReader:对索引文件进行读操作,并为IndexSear ...
- Berkeley DB基础教程
一.Berkeley DB的介绍 (1)Berkeley DB是一个嵌入式数据库,它适合于管理海量的.简单的数据.如Google使用其来保存账户信息,Heritrix用其来保存froniter. (2 ...
- Java、Tomcat 及 MySQL 环境配置
Java开发环境的配置 首先我们要下载JDK. 到Oracle官网上去下载即可,目前最新版是Java SE 8u25. 开始我很混乱,Java SE 和 JDK是什么关系呢?最后查了一下 Java S ...
- Entity Framework 利用 Database.SqlQuery<T> 执行存储过程,并返回Output参数值
做个记录: var pCount = this._dataProvider.GetParameter(); pCount.ParameterName = "totalCount"; ...