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

代码:递推式套矩阵快速幂即可,注意多组输入

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>using namespace std;const int N=10;int n,mod;
int temp[N][N];
int res[N][N],a[N][N];
void mul(int a[][N],int b[][N]) {memset(temp,0,sizeof(temp));for(int i=0; i<N; i++)for(int j=0; j<N; j++)for(int k=0; k<N; k++)temp[i][j]=(temp[i][j]+a[i][k]*b[k][j]%mod)%mod;for(int i=0; i<N; i++)for(int j=0; j<N; j++)a[i][j]=temp[i][j];return ;
}
void QuickPow(int nn) {memset(res,0,sizeof(res));for(int i=0; i<N; i++)res[i][i]=1;while(nn) {if(nn&1)mul(res,a);mul(a,a);nn>>=1;}return ;
}
int main() {while(~scanf("%d %d",&n,&mod)) {memset(a,0,sizeof(a));for(int i=0; i<N; i++)scanf("%d",&a[0][i]);for(int i=1; i<N; i++)a[i][i-1]=1;if(n<10) printf("%d\n",n%mod);else {QuickPow(n-9);int ans=0;for(int i=0; i<N; i++)ans+=res[0][i]*(9-i)%mod;printf("%d\n",ans%mod);}}return 0;
}

转载于:https://www.cnblogs.com/Staceyacm/p/10781891.html

A Simple Math Problem (矩阵快速幂)相关推荐

  1. HDU - 1757 A Simple Math Problem(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:实现公式: f(x)=x,x<10 f(x)=a0*f(x-1)+a1*f(x-2)+--+a9*f(x-10) 题目给出a0~a9,一个n和一个m,要求输出f(n ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 在吴神的帮助下才明白如何构造矩阵,还是好弱啊. 此处盗一张图 1 #include <io ...

  3. DUToj1085 Water Problem(矩阵快速幂)

    Problem I: Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java ...

  4. A Simple Math Problem 矩阵打水题

    A Simple Math Problem Lele now is thinking about a simple function f(x). If x < 10 f(x) = x. If x ...

  5. HDU 4291 A Short problem 矩阵快速幂 循环节

    题解思路: 构造矩阵,矩阵乘法计算还是t; 需要找循环节;   (注意因为是复合函数,不可以在里面取mod) 暴力跑只有可以找到g(222222224)%1e9==g(0)%1e9; 所以 g(g(n ...

  6. 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 * ...

  7. 【HDU - 1757】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) = ...

  8. 43行代码AC——HDU 1757 A Simple Math Problem(矩阵快速幂,附快速幂讲解)

    一道经典的矩阵快速幂模板题. 传送门1-->快速幂基本思想 传送门2-->矩阵快速幂讲解(教主传授) 代码(去掉空行43行) #include<iostream> #inclu ...

  9. HDOJ 1757 A Simple Math Problem(矩阵快速幂)

    2018-5-24 简单的矩阵快速幂问题,重点是如何找到对应关系. f(10) a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 f(9) f(9) 1 0 0 0 0 0 0 0 0 0 ...

  10. 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) + -- ...

最新文章

  1. 1097 Deduplication on a Linked List
  2. 网站SEO优化如何讨好搜索引擎蜘蛛?
  3. http://www.cnblogs.com/qtqq/p/5271164.html
  4. webView 点击连接如何不让跳转到系统的 浏览器
  5. python前缀表达式求值_python数据结构与算法 11 后缀表达式求值
  6. java集合(6):TreeMap源码分析(jdk1.8)
  7. html文本域 高度自适应,textarea高度自适应,textarea随着内容高度变化
  8. 一些社会运行的底层规律,和你的利益息息相关
  9. java 加法程序_使用JAVAEE编写简单的加法程序
  10. 用opengl编写一个简单的画图软件示例代码
  11. centos 和 radhat 配置epel仓库
  12. HTML超连接(a标记)
  13. draw.io箭头设置虚线
  14. 常见容错机制:failover、failfast、failback、failsafe
  15. 一步一步搭建自己的web系统(一):搭建后端服务
  16. 【C认证】对标名企技术标准,大厂不是梦
  17. 复数计算器 Java 正则表达式
  18. 记录webView显示空白的又一坑
  19. 《AngularJS深度剖析与最佳实践》一1.3 创建项目
  20. 仿制微信/QQ在线聊天网站

热门文章

  1. 用计算机装扮校园图片,第4课装扮我们的校园——在场景中运用图形元件教学设计.doc...
  2. java连接mysql-8.0.11且成功读写
  3. java 内存快照怎么看_jvm工具篇-(1)-分析工具MAT使用(内存快照分析)
  4. python 时间处理_Python如何进行时间处理
  5. Unity 协同程序
  6. Nginx源码分析 - HTTP模块篇 - HTTP模块的阶段处理PHASE handler(23)
  7. Hash表、Hash函数及冲突解决
  8. Jenkins系列一Linux环境安装Jenkins
  9. Struts2之类型转换中的错误
  10. 【渝粤题库】陕西师范大学200071 古代汉语 作业(高起本、高起专)