首页 > 代码库 > poj3070 (斐波那契,矩阵快速幂)

poj3070 (斐波那契,矩阵快速幂)

Fibonacci
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9630 Accepted: 6839

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

099999999991000000000-1

Sample Output

0346266875

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:

.

 

a_{n}=\frac{\sqrt{5}}{5} \cdot \left[\left(\frac{1 + \sqrt{5}}{2}\right)^{n} - \left(\frac{1 - \sqrt{5}}{2}\right)^{n}\right]求斐波那契序列的公式。

由于该矩阵的特殊结构使得a(n+1)[0][0] = a(n)[0][0]+a(n)[0][1], a(n+1)[0][1] = a(n)[1][1], a(n+1)[1][0] = a(n)[0][1]+a(n)[1][0], a(n+1)[1][1] = a(n)[1][0];

 

code:

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<algorithm> 6 #include<cmath> 7 #define M(a,b) memset(a,b,sizeof(a)) 8  9 using namespace std;10 11 int n;12 struct matrix13 {14     int a[2][2];15     void init()16     {17         a[0][0] = a[1][0] = a[0][1] = 1;18         a[1][1] = 0;19     }20 };21 22 matrix mamul(matrix a,matrix b)23 {24     matrix c;25     for(int i = 0;i<2;i++)26     {27         for(int j = 0;j<2;j++)28         {29             c.a[i][j] = 0;30             for(int k = 0;k<2;k++)31                 c.a[i][j]+=(a.a[i][k]*b.a[k][j]);32             c.a[i][j]%=10000;33         }34     }35     return c;36 }37 38 matrix mul(matrix s, int k)39 {40     matrix ans;41     ans.init();42     while(k>=1)43     {44         if(k&1)45             ans = mamul(ans,s);46         k = k>>1;47         s = mamul(s,s);48     }49     return ans;50 }51 52 int main()53 {54     while(scanf("%d",&n)==1&n>=0)55     {56         if(n==0) puts("0");57         else58         {59             matrix ans;60             ans.init();61             ans = mul(ans,n-1);62             printf("%d\n",ans.a[0][1]%10000);63         }64     }65     return 0;66 }

 下面代码只是测试公式,无法解决取模的问题,因为中间为double型,无法取模:

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<vector> 5 #include<algorithm> 6 #include<cmath> 7 #define M(a,b) memset(a,b,sizeof(a)) 8  9 using namespace std;10 11 double Pow(double a,int n)12 {13     double ans = 1;14     while(n>=1)15     {16         if(n&1)17             ans = a*ans;18         n = n>>1;19         a = a*a;20     }21     return ans;22 }23 24 int main()25 {26    int n;27    double a = (sqrt(5.0)+1.0)/2;28    double b = (-sqrt(5.0)+1.0)/2;29    double c = (sqrt(5.0))/5;30    while(scanf("%d",&n)==1)31    {32         int ans = (int)(c*(Pow(a,n)-Pow(b,n)))%10000;33         printf("%d\n",ans);34    }35    return 0;36 }

 

poj3070 (斐波那契,矩阵快速幂)