经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
Recursive sequence(HDU5950 矩阵快速幂)
来源:cnblogs  作者:sykline  时间:2018/10/11 9:26:48  对本文有异议

题目:

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.

Input:

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and i4i4. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.

Output:

For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo 2147493647.

Hint:

 

  1. In the first case, the third number is 85 = 2*1+2+3^4.
    In the second case, the third number is 93 = 2*1+1*10+3^4 and the fourth number is 369 = 2 * 10 + 93 + 4^4.

 

题意:

奶牛报数,先给两个数a和b,分别是f[n-2],f[n-1],之后每头奶牛i报数为f[i-1] + 2 * f[i-2] + i^4;给出n,求din头奶牛要报的数字,对2147493647取余。

思路:

看到这个式子知道这是一个矩阵快速幂,然后开始推式子,在我给队友写出平方差公式来队友看到杨辉三角形式后后,就去推7*7的矩阵快速幂了,但因为刚刚学这个,但结束就挂死在这个题上了。

具体式子如下:

 

 之后就是套裸的矩阵快速幂就好了,个人感觉做题补题真的是长知识最快的方法啊。补题的时候自己直接用矩阵来写麻烦的要死,就把矩阵放在一个结构体中,顺便方便很多。

代码:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <vector>
  9. using namespace std;
  10. typedef long long ll;
  11. const ll MOD = 2147493647;
  12. struct Maxt {
  13. ll mp[8][8];
  14. Maxt() {
  15. for(int i = 1; i<=7; i++) {
  16. for(int j = 1; j<=7; j++) {
  17. mp[i][j] = 0;
  18. }
  19. }
  20. }
  21. } fp,tmp;
  22. int n,a,b,T;
  23. int read() {
  24. int res = 0;
  25. char op;
  26. op = getchar();
  27. if(op>='0' && op<='9') {
  28. res = op-'0';
  29. op = getchar();
  30. }
  31. while(op>='0' && op<='9') {
  32. res = res*10 + op-'0';
  33. op = getchar();
  34. }
  35. return res;
  36. }
  37. void init() {
  38. for(int i = 1; i<=7; i++) {
  39. for(int j =1; j<=7; j++) {
  40. fp.mp[i][j] = 0;
  41. tmp.mp[i][j] = 0;
  42. }
  43. }
  44. fp.mp[1][1] = 1,fp.mp[2][1] = 1,fp.mp[2][2] = 1,fp.mp[7][6] = 1;
  45. fp.mp[3][1] = 1,fp.mp[3][2] = 2,fp.mp[3][3] = 1,fp.mp[4][1] = 1;
  46. fp.mp[4][2] = 3,fp.mp[4][3] = 3,fp.mp[4][4] = 1,fp.mp[5][1] = 1;
  47. fp.mp[5][2] = 4,fp.mp[5][3] = 6,fp.mp[5][4] = 4,fp.mp[5][5] = 1;
  48. fp.mp[6][5] = 1,fp.mp[6][6] = 1,fp.mp[6][7] = 2;
  49. tmp.mp[1][1] = 1,tmp.mp[2][1] = 3,tmp.mp[3][1] = 9,tmp.mp[4][1] = 27,tmp.mp[5][1] = 81,tmp.mp[6][1] = b,tmp.mp[7][1] = a;
  50. }
  51. Maxt Maxtcalc(const Maxt& a,const Maxt& b) {
  52. Maxt t;
  53. for(int i = 1; i<=7; i++) {
  54. for(int j = 1; j<=7; j++) {
  55. t.mp[i][j] = 0;
  56. for(int k = 1; k<=7; k++) {
  57. t.mp[i][j] = (t.mp[i][j] + (a.mp[i][k]*b.mp[k][j]) % MOD) % MOD;
  58. }
  59. }
  60. }
  61. return t;
  62. }
  63. Maxt qcalc(int x,Maxt s) {
  64. Maxt tmp;
  65. for(int i = 1; i<=7; i++) {
  66. tmp.mp[i][i] = 1;
  67. }
  68. while(x) {
  69. if(x&1) {
  70. tmp = Maxtcalc(tmp, s);
  71. }
  72. s = Maxtcalc(s, s);
  73. x>>=1;
  74. }
  75. return tmp;
  76. }
  77. int main() {
  78. T = read();
  79. while(T--) {
  80. n = read();
  81. a = read();
  82. b= read();
  83. if(n == 1) {
  84. printf("%d\n",a);
  85. continue;
  86. }
  87. if(n == 2) {
  88. printf("%d\n",b);
  89. continue;
  90. }
  91. if(n == 3) {
  92. printf("%d\n",81+2*a+b);
  93. continue;
  94. }
  95. n = n-2;
  96. init();
  97. fp = qcalc(n,fp);
  98. Maxt ans = Maxtcalc(fp, tmp);
  99. printf("%lld\n",ans.mp[6][1]%MOD);
  100. }
  101. return 0;
  102. }
  103. /*
  104. 样例输入:
  105. 2
  106. 3 1 2
  107. 4 1 10
  108. 样例输出:
  109. 85
  110. 369
  111. */
View Code

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号