A Simple Math Problem

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1697    Accepted Submission(s): 959

Problem Description
Lele now is thinking about a simple function f(x).

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.

 
Input
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<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
 
Output
For each case, output f(k) % m in one line.
 
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
 
Sample Output
45
104
 
Author
linle
 
Source
 
Recommend
lcy
 

If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);

|f(10) |       |a0 a1 a2 ...a8 a9|    |f(9)|
| f(9)  |       | 1   0   0 ... 0    0 |    |f(8)|
| .....  |   =  | ..    ...    ...   ...    |    | .. |
| f(2) |        | 0   0   0 ... 0    0|     |f(1)|
| f(1) |   | 0   0   0 ... 1    0|     |f(0)|

另A举证为10*10的举证,如上图。
可以推出:
(f(n),f(n-1),...,f(n-9))^(-1) = A^(n-9)*(f(9),f(8),...,f(0))^(-1)

#include<iostream>
#include<cstdio>
#include<cstring> using namespace std; long long k,mod; struct Matrix{
int m[][];
}; Matrix unit,init; void Init(){
memset(init.m,,sizeof(init.m));
for(int i=;i<;i++)
init.m[i][i-]=;
memset(unit.m,,sizeof(unit.m));
for(int i=;i<;i++)
unit.m[i][i]=;
} Matrix Mul(Matrix a,Matrix b){
Matrix c;
for(int i=;i<;i++)
for(int j=;j<;j++){
c.m[i][j]=;
for(int k=;k<;k++)
c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;
c.m[i][j]%=mod;
}
return c;
} Matrix Pow(Matrix a,Matrix b,int x){
while(x){
if(x&){
b=Mul(a,b);
}
a=Mul(a,a);
x>>=;
}
return b;
} int main(){ //freopen("input.txt","r",stdin); while(cin>>k>>mod){
Init();
for(int i=;i<;i++)
scanf("%d",&init.m[][i]);
if(k<){
cout<<k%mod<<endl;
continue;
}
Matrix res=Pow(init,unit,k-);
int ans=;
for(int i=;i<;i++)
ans+=(res.m[][i]*(-i))%mod;
cout<<ans%mod<<endl;
}
return ;
}

HDU 1757 A Simple Math Problem (矩阵乘法)的更多相关文章

  1. HDU 1757 A Simple Math Problem (矩阵快速幂)

    题目 A Simple Math Problem 解析 矩阵快速幂模板题 构造矩阵 \[\begin{bmatrix}a_0&a_1&a_2&a_3&a_4&a ...

  2. HDU 1757 A Simple Math Problem(矩阵)

    A Simple Math Problem [题目链接]A Simple Math Problem [题目类型]矩阵快速幂 &题解: 这是一个模板题,也算是入门了吧. 推荐一个博客:点这里 跟 ...

  3. 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 ...

  4. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 1757 A Simple Math Problem(矩阵高速幂)

    题目地址:HDU 1757 最终会构造矩阵了.事实上也不难,仅仅怪自己笨..= =! f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 ...

  6. 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) + …… ...

  7. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  8. hdu 1757 A Simple Math Problem(矩阵快速幂乘法)

    Problem Description Lele now is thinking about a simple function f(x). If x < f(x) = x. If x > ...

  9. 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 ...

随机推荐

  1. 深度学习研究组Deep Learning Research Groups

    Deep Learning Research Groups Some labs and research groups that are actively working on deep learni ...

  2. Gedit

    Use Gedit as Remote File Editor via FTP and SSH Migrating from Windows Vista to Ubuntu 8.04 and have ...

  3. TRIZ理论的进化法则分析(TRIZ学习笔记)

    人们在创新和完好系统的过程能够遵循一定的规律(或者叫法则).从而降低创新和完好系统过程中的试错成本,以下就TRIZ的八大进化原则来进行说明(这个八大法则是前人们的总结,我这里当然会增加我的理解). 我 ...

  4. zoj 2860 四边形优化dp

    Breaking Strings Time Limit: 2 Seconds        Memory Limit: 65536 KB A certain string-processing lan ...

  5. C/C++语言中变量作用域:局部变量,全局变量,文件级变量

    C/C++语言中的变量分为全局变量和局部变量. 这样的划分方式的根据是变量的可见范围或者叫做作用域. 1 局部变量 局部变量指的是定义在{}中的变量,其作用域也在这个范围内.尽管常见的局部变量都是定义 ...

  6. DateNavigator

    <Border BorderThickness="1,1,1,1" BorderBrush="Black" Grid.Column="1&quo ...

  7. [Javascript] Closure Cove, 1

    Returning a function from a function, complete with variables from an external scope, is called a cl ...

  8. 2014年ENVI/IDL遥感应用与开发培训班-11月重庆站 開始报名了

    主办单位: 中国遥感应用协会 Esri中国信息技术有限公司 内容简单介绍: 依据中国遥感应用协会栾恩杰理事长推动国内遥感技术和应用的指示精神,2014年中国遥感应用协会组织培训交流部与Esri中国信息 ...

  9. ubuntu server 安装 question2answer 及 汉化包

    ubuntu server 安装 question2answer 及 汉化包 question2answer 是一个非常简洁方便的问答系统,可以用它快速的部署一个问答社区,提高在开发中的交流沟通效率: ...

  10. sql数据库表复制、查看是否锁表

    1.不同数据库之间复制表的数据的方法: 当表目标表存在时: insert into 目的数据库..表 select * from 源数据库..表 当目标表不存在时: select * into 目的数 ...