A Simple Math Problem

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

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
 
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstdlib>
  4. #include<cstring>
  5. using namespace std;
  6.  
  7. const int N= ;
  8.  
  9. struct node
  10. {
  11. __int64 mat[N][N];
  12. }hxl;
  13. int A[],k,m;
  14.  
  15. void make_first(node *cur)
  16. {
  17. int i,j;
  18. for(i=;i<=;i++)
  19. for(j=;j<=;j++)
  20. if(i==j)
  21. cur->mat[i][j]=;
  22. else cur->mat[i][j]=;
  23. }
  24.  
  25. struct node cheng(node cur,node now)
  26. {
  27. node ww;
  28. int i,j,k;
  29. memset(ww.mat,,sizeof(ww.mat));
  30. for(i=;i<=;i++)
  31. for(k=;k<=;k++)
  32. if(cur.mat[i][k])
  33. {
  34. for(j=;j<=;j++)
  35. if(now.mat[k][j])
  36. {
  37. ww.mat[i][j]+=cur.mat[i][k]*now.mat[k][j];
  38. if(ww.mat[i][j]>=m)
  39. ww.mat[i][j]%=m;
  40. }
  41. }
  42. return ww;
  43. }
  44.  
  45. void power_sum2(int n)
  46. {
  47. __int64 sum=;
  48. int i;
  49. node cur,now=hxl;
  50. make_first(&cur);
  51. while(n)
  52. {
  53. if(n&)
  54. {
  55. cur=cheng(cur,now);
  56. }
  57. n=n>>;
  58. now=cheng(now,now);
  59. }
  60. for(i=;i<=;i++)
  61. sum= (sum+(-i)*cur.mat[][i])%m;
  62. printf("%I64d\n",sum);
  63.  
  64. }
  65.  
  66. void make_ini()
  67. {
  68. int i,j;
  69. if(k<=)
  70. {
  71. printf("%d\n",k);
  72. return;
  73. }
  74. memset(hxl.mat,,sizeof(hxl.mat));
  75. for(i=;i<=;i++)
  76. hxl.mat[][i]=A[i];
  77.  
  78. for(i=;i<=;i++)// 初始化
  79. for(j=;j<=;j++)
  80. if(i-j==)
  81. hxl.mat[i][j]=;
  82.  
  83. power_sum2(k-);
  84. }
  85.  
  86. int main()
  87. {
  88. int i;
  89. while(scanf("%d%d",&k,&m)>)
  90. {
  91. for(i=;i<=;i++)
  92. scanf("%d",&A[i]);
  93. make_ini();
  94. // cs();
  95. }
  96. return ;
  97. }
Source
 
Recommend
lcy
 
 

HDU 1757 矩阵求第n的递推式的更多相关文章

  1. 矩阵乘法&矩阵快速幂&矩阵快速幂解决线性递推式

    矩阵乘法,顾名思义矩阵与矩阵相乘, 两矩阵可相乘的前提:第一个矩阵的行与第二个矩阵的列相等 相乘原则: a b     *     A B   =   a*A+b*C  a*c+b*D c d     ...

  2. HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识

    求$G(a,b,n,p) = (a^{\frac {p-1}{2}}+1)(b^{\frac{p-1}{2}}+1)[(\sqrt{a} + \sqrt{b})^{2F_n} + (\sqrt{a} ...

  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 (构造矩阵解决递推式问题)

    题意:有一个递推式f(x) 当 x < 10    f(x) = x.当 x >= 10  f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...

  5. P1067Warcraft III 守望者的烦恼(十大矩阵问题之七求递推式)

    https://vijos.org/p/1067 守望者-warden,长期在暗夜精灵的的首都艾萨琳内担任视察监狱的任务,监狱是成长条行的,守望者warden拥有一个技能名叫“闪烁”,这个技能可以把她 ...

  6. HDU - 2604 Queuing(递推式+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  7. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

  8. *HDU 1757 矩阵乘法

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

  9. 矩阵快速幂(queue递推)

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 Queuing Time Limit: 10000/5000 MS (Java/Others)    Me ...

随机推荐

  1. Requests Header | Http Header

    Requests Header | Http Header Header 解释 示例 Accept 指定客户端能够接收的内容类型 Accept: text/plain, text/html Accep ...

  2. 【javascript】—— JS判断浏览器类型、操作系统

    navigator.userAgent : userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值. navigator.platform : platform ...

  3. declare命令

    还是围绕以下几个问题进行学习; 1.declare是什么? 2.问什么要用declare? 3.怎样使用declare? 1.declare是什么? ♦declare应用的很多,向我们各种语言都会有声 ...

  4. IOS----UIScrollerView的使用

    刚刚遛狗回来,前段时间创建的这篇博客一直没有填充内容,今天把scrollerview正好整理一下. 1.scrollerview的主要作用:当界面显示不开要显示的内容,scrollerview提供了滑 ...

  5. AssertJ断言系列-----------<数据库断言二>

    那么,在实际的接口测试中,我们除了要断言响应的数据正确之外,可能有的还需要断言数据层是否数据真的有入库. assertj db是可以直接对数据库进行断言和操作的. 一.创建一个students表 CR ...

  6. 图的最短路径---迪杰斯特拉(Dijkstra)算法浅析

    什么是最短路径 在网图和非网图中,最短路径的含义是不一样的.对于非网图没有边上的权值,所谓的最短路径,其实就是指两顶点之间经过的边数最少的路径. 对于网图,最短路径就是指两顶点之间经过的边上权值之和最 ...

  7. Vue-cli 2.9 多页配置及多页面之间的跳转问题

    vue开发,现在大部分做的都是(SPA)应用,但是,由于,需求不同,我们针对的用户需求变更较为,频繁,如果每次都全量打包更新,给开发的自测,及测试妹子的任务就会多,每次都要重新验证一下才放心.所以,想 ...

  8. 【算法笔记】B1052 卖个萌

    题目链接:https://pintia.cn/problem-sets/994805260223102976/problems/994805273883951104 #include <math ...

  9. CodeForces - 1025B Weakened Common Divisor

    http://codeforces.com/problemset/problem/1025/B 大意:n对数对(ai,bi),求任意一个数满足是所有数对中至少一个数的因子(大于1) 分析: 首先求所有 ...

  10. 问题 K: 周期串plus

    问题 K: 周期串plus 时间限制: 1 Sec  内存限制: 128 MB提交: 682  解决: 237[提交] [状态] [命题人:外部导入] 题目描述 如果一个字符串可以由某个长度为k的字符 ...