poj Fibonacci 矩阵快速幂

向右看齐 2022-05-16 11:22 299阅读 0赞

题目链接:点我
Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Input

The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.

Output

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input

0
9
999999999
1000000000
-1
Sample Output

0
34
626
6875
Hint

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

.

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

.

大佬博客:点我
这里应用到矩阵快速幂,矩阵快速幂是用来求解递推式的,所以第一步先要列出递推式:
f(n)=f(n-1)+f(n-2);第二步是建立矩阵递推式,找到转移矩阵:这里写图片描述,简写成T * A(n-1)=A(n),T矩阵就是那个2 乘2的常数矩阵,而
这里就是个矩阵乘法等式左边:1f(n-1)+1f(n-2)=f(n);1f(n-1)+0f(n-2)=f(n-1);

这里还是说一下构建矩阵递推的大致套路,一般An与A(n-1)都是按照原始递推式来构建的,当然可以先猜一个An,主要是利用矩阵乘法凑出矩阵T,第一行一般就是递推式,后面的行就是不需要的项就让与其的相乘系数为0。矩阵T就叫做转移矩阵(一定要是常数矩阵),它能把A(n-1)转移到A(n);然后这就是个等比数列,直接写出通项:此处A1叫初始矩阵。所以用一下矩阵快速幂然后乘上初始矩阵就能得到An,这里An就两个元素(两个位置),根据自己设置的A(n)对应位置就是对应的值,按照上面矩阵快速幂写法,res[1][1]=f(n)就是我们要求的。

这里给出实现代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<stack>
  7. #include<map>
  8. #include<vector>
  9. #include<queue>
  10. #include<set>
  11. #include<iomanip>
  12. #include<cctype>
  13. using namespace std;
  14. const int MAXN=2e5+5;
  15. const int INF=1<<30;
  16. //const long long mod=1e9+7;
  17. #define ll long long
  18. #define edl putchar('\n')
  19. #define sscc ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  20. #define FOR(i,a,b) for(int i=a;i<=b;i++)
  21. #define ROF(i,a,b) for(int i=a;i>=b;i--)
  22. #define FORLL(i,a,b) for(ll i=a;i<=b;i++)
  23. #define ROFLL(i,a,b) for(ll i=a;i>=b;i--)
  24. #define mst(a) memset(a,0,ssizeof(a))
  25. #define mstn(a,n) memset(a,n,ssizeof(a))
  26. #define zero(x)(((x)>0?(x):-(x))<eps)
  27. ll mod=10000;
  28. const int ssize=2;
  29. struct Matrix {
  30. ll a[ssize][ssize];
  31. Matrix() {
  32. memset(a,0,sizeof(a));
  33. }
  34. void init() {
  35. for(int i=0; i<ssize; i++)
  36. for(int j=0; j<ssize; j++)
  37. a[i][j]=(i==j);
  38. }
  39. Matrix operator + (const Matrix &B)const {
  40. Matrix C;
  41. for(int i=0; i<ssize; i++)
  42. for(int j=0; j<ssize; j++)
  43. C.a[i][j]=(a[i][j]+B.a[i][j])%mod;
  44. return C;
  45. }
  46. Matrix operator * (const Matrix &B)const {
  47. Matrix C;
  48. for(int i=0; i<ssize; i++)
  49. for(int k=0; k<ssize; k++)
  50. for(int j=0; j<ssize; j++)
  51. C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%mod;
  52. return C;
  53. }
  54. Matrix operator ^ (const ll &t)const {
  55. Matrix A=(*this),res;
  56. res.init();
  57. ll p=t;
  58. while(p) {
  59. if(p&1)res=res*A;
  60. A=A*A;
  61. p>>=1;
  62. }
  63. return res;
  64. }
  65. };
  66. int main() {
  67. Matrix a,b;
  68. int n;
  69. while(scanf("%d",&n)==1&&n!=-1) {
  70. a.a[0][0]=1;
  71. a.a[0][1]=1;
  72. a.a[1][0]=1;
  73. a.a[1][1]=0;
  74. b.a[0][0]=1;
  75. b.a[1][0]=0;
  76. if(n<=0){
  77. cout<<0<<endl;
  78. }
  79. else{
  80. a=a^(n-1);
  81. b=a*b;
  82. cout<<b.a[0][0]<<endl;
  83. }
  84. }
  85. }

发表评论

表情:
评论列表 (有 0 条评论,299人围观)

还没有评论,来说两句吧...

相关阅读