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. linux的chmod,chown命令详解

    指令名称 : chmod 使用权限 : 所有使用者 使用方式 : chmod [-cfvR] [--help] [--version] mode file... 说明 : Linux/Unix 的档案 ...

  2. 如何读取抓取的wifi包内容

    有密码的WIFI,WIFI的密码会生成一个临时会话密钥,这个临时会话密钥可以用来加密会话内容,也就是说.比如你在浏览网页,用有密码的WIFI,连接上以后,浏览的网页流量是加密了的,所以更安全.无密码的 ...

  3. hdu 4548 美素数 超级大水题

    美素数 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submis ...

  4. OTL翻译(9) --常量的SQL语句

    常量的SQL语句 一个没有绑定变量的SQL语句.SQL语句块或是存储过程就被称为常量的SQL语句.OTL通过一个静态的函数来执行这样的SQL语句. 例如: // static otl_cursor:: ...

  5. 【K8S】K8S-网络模型、POD/RC/SVC YAML 语法官方文档

    K8S-网络模型.POD/RC/SVC YAML 语法官方文档 Kubernetes - Production-Grade Container Orchestration kubernetes/kub ...

  6. 【转】gcc选项

    http://zodiac1111.github.io/blog/config-gcc-warning/

  7. 动态改变UIPopupList选项(NGUI)

    NGUI的UIPopupList 可以通过修改items属性来动态改变菜单选项: public class popListvahnge : MonoBehaviour { public UIPopup ...

  8. [Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup

    Below we've already created an express server, but we want to start building a real-time Q&A mod ...

  9. [Node.js]25. Level 5. Route params

    Create a route that responds to a GET request '/quotes/<name>', then use the param from the UR ...

  10. Sticker.js生成图片或者页面元素“速干贴”效果

    ​在线演示 本地下载 真实的效果,真的非常好玩!