http://acm.hdu.edu.cn/showproblem.php?pid=4549

f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p.....f[n] = a^fib[n-1] * b^fib[n-2]%p。



这里p是质数,且a,p互素,那么我们求a^b%p,当b非常大时要对b降幂。

由于a,p互素,那么由费马小定理知a^(p-1)%p = 1。令b = k*(p-1) + b'。a^b%p = a^(k*(p-1)+b')%p = a^(k*(p-1))%p * a^b'%p

= a^b'%p。

当中p' = b%(p-1)。

那么上题的fib[n-1]和fib[n-2]能够经过矩阵高速幂对(p-1)取模,实现降幂以后再用高速幂求得。

事实上a^b%c较一般的形式是a^(b%phi[c]+phi[c])%c (b>=phi[c]),在c是素数或a与c互素的时候能够简化为a^(b%(c-1))%c。



纳闷了一中午,结果把fib数列弄错了。f[0] = 0, f[1] = 1,f[n] = f[n-1]+f[n-2](n >= 2)。第0项是0,sad....

http://www.narutoacm.com/archives/a-pow-b-mod-m/

对大神表示深深的ym.

  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <map>
  4. #include <set>
  5. #include <list>
  6. #include <stack>
  7. #include <vector>
  8. #include <math.h>
  9. #include <string.h>
  10. #include <queue>
  11. #include <string>
  12. #include <stdlib.h>
  13. #include <algorithm>
  14. #define LL long long
  15. #define _LL __int64
  16. #define eps 1e-12
  17. #define PI acos(-1.0)
  18. #define C 240
  19. #define S 20
  20. using namespace std;
  21. const int maxn = 110;
  22. const int mod = 1000000007;
  23.  
  24. struct matrix
  25. {
  26. LL mat[3][3];
  27. void init()
  28. {
  29. memset(mat,0,sizeof(mat));
  30. for(int i = 0; i < 2; i++)
  31. mat[i][i] = 1;
  32. }
  33. }m;
  34.  
  35. LL a,b,n;
  36.  
  37. matrix mul_matrix(matrix x, matrix y)
  38. {
  39. matrix ans;
  40. memset(ans.mat,0,sizeof(ans.mat));
  41. for(int i = 0; i < 2; i++)
  42. {
  43. for(int k = 0; k < 2; k++)
  44. {
  45. if(x.mat[i][k] == 0) continue;
  46. for(int j = 0; j < 2; j++)
  47. ans.mat[i][j] = (ans.mat[i][j] + x.mat[i][k]*y.mat[k][j])%(mod-1);
  48. }
  49. }
  50. return ans;
  51. }
  52.  
  53. matrix pow_matrix(matrix m, LL n)
  54. {
  55. matrix ans;
  56. ans.init();
  57. while(n)
  58. {
  59. if(n&1)
  60. ans = mul_matrix(ans,m);
  61. m = mul_matrix(m,m);
  62. n >>= 1;
  63. }
  64. return ans;
  65. }
  66.  
  67. LL pow_mod(LL a, LL b)
  68. {
  69. LL res = 1;
  70. a = a%mod;
  71. while(b)
  72. {
  73. if(b&1)
  74. res = res*a%mod;
  75. a = a*a%mod;
  76. b >>= 1;
  77. }
  78. return res;
  79. }
  80.  
  81. int main()
  82. {
  83.  
  84. while(~scanf("%lld %lld %lld",&a,&b,&n))
  85. {
  86. m.mat[0][0] = m.mat[0][1] = m.mat[1][0] = 1;
  87. m.mat[1][1] = 0;
  88. if(n == 0)
  89. {
  90. printf("%lld\n",a%mod);
  91. continue;
  92. }
  93. if(n == 1)
  94. {
  95. printf("%lld\n",b%mod);
  96. continue;
  97. }
  98. else if(n == 2)
  99. {
  100. printf("%lld\n",a*b%mod);
  101. continue;
  102. }
  103. matrix ans = pow_matrix(m,n);
  104. LL res = pow_mod(a,ans.mat[1][1]) * pow_mod(b,ans.mat[0][1]) %mod;
  105. printf("%lld\n",res);
  106. }
  107. return 0;
  108. }

hdu 4549 M斐波那契数列(矩阵高速幂,高速幂降幂)的更多相关文章

  1. hdu 4549 M斐波那契数列 矩阵快速幂+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Problem ...

  2. hdu 4549 M斐波那契数列(快速幂 矩阵快速幂 费马小定理)

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4549: 题目是中文的很容易理解吧.可一开始我把题目看错了,这毛病哈哈. 一开始我看错题时,就用了一个快速 ...

  3. [HDU 4549] M斐波那契数列

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  4. HDU 4549 M斐波那契数列(矩阵快速幂)

    题目链接:M斐波那契数列 题意:$F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]$.给定$a,b,n$,求$F[n]$. 题解:暴力打表后发现$ F[n]=a^{fib(n-1)} ...

  5. HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

  6. hdu 4549 M斐波拉契 (矩阵快速幂 + 费马小定理)

    Problem DescriptionM斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = aF[1] = bF[n] = F[n-1] * F[n-2] ( n > 1 ) 现在 ...

  7. HDU 4549 M斐波那契数列(矩阵幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4549 题意:F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]. 思路:手算一下可以发现 ...

  8. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

  9. HDU 1316 (斐波那契数列,大数相加,大数比较大小)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1316 Recall the definition of the Fibonacci numbers: ...

随机推荐

  1. 用Python利用pyFirmata控制Arduino实现Blink

    2018-03-2809:20:44 arduino中有相应的库 1.安装pyFirmata包 pip install pyFirmata 在python2.7或python3.X下都可以执行. py ...

  2. Python学习日记之快捷键

    Alt+Enter 自动添加包Ctrl+t SVN更新Ctrl+k SVN提交Ctrl + / 注释(取消注释)选择的行Ctrl+Shift+F 高级查找Ctrl+Enter 补全Shift + En ...

  3. c++类简介

    C++类(Class)总结   一.C++类的定义     C++中使用关键字 class 来定义类, 其基本形式如下:class 类名{ public: //行为或属性  protected: // ...

  4. HDU多校Round 3

    Solved:4 rank:268 C. Dynamic Graph Matching  状压DP一下 #include <stdio.h> #include <algorithm& ...

  5. qemu vm setup network(ssh) with buildroot

    1, build buildroot with buildroot.config, that is 'make qemu_x86_64_defconfig' + some packages, sshd ...

  6. mysql中InnoDB与MyISAM的区别

    两者的区别: 1. InnoDB支持事务,MyISAM不支持,对于InnoDB每一条SQL语言都默认封装成事务,自动提交,这样会影响速度,所以最好把多条SQL语言放在begin和commit之间,组成 ...

  7. Linux终端中的字符串使用总结(截取,切片,查找替换,转换,变量赋值等)

    1.基于模式匹配截取字符串 1.#.##号截取 #号截取,从左边开始,以一个出现的分隔符(最左)为准,删除左边字符. ##号截取,从左边开始,以最后(最右)一个分隔符为准,删除左边字符. str='h ...

  8. keep

    简介 什么是keepalived呢?keepalived是实现高可用的一种轻量级的技术手段,主要用来防止单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生.之所以说keepa ...

  9. 简述systemd的新特性及unit常见类型分析、使用systemd管理编译安装的nginx

    1. systemd新特性 并行处理(同时启动)所有服务. 基于依赖关系定义的服务控制逻辑 系统状态快照 按需激活进程,只有第一次被访问时才会真正启动: 2. systemd的常见unit类型 Ser ...

  10. 深入理解PHP之foreach

    招聘 标签(空格分隔): 招聘 PHP 国贸 语言基础 foreach 语法结构提供了遍历数组的简单方式. php5之前, foreach仅能用于数组php5+, 利用foreach可以遍历对象 fo ...