Fibonacci




Fibonacci

题号找不到了但类似题目很多问题不大

问题描述

Time Limit : 2000/1000ms (Java/Other)

Memory Limit : 131072/65536K (Java/Other)

Problem 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

$$\left[\begin{matrix}F_{n+1}&F_n\\ F_n&F_{n-1}\ \end{matrix}\right]= \left[\begin{matrix} 1& 1\\ 1&0\ \end{matrix}\right]^n= \underbrace{\left[\begin{matrix} 1& 1\\ 1&0\ \end{matrix}\right]\left[\begin{matrix} 1& 1\\ 1&0\ \end{matrix}\right]\cdots \left[\begin{matrix} 1& 1\\ 1&0\ \end{matrix}\right]}_{ n\ times} $$ 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

题意:

斐波那契数列可以通过矩阵相乘来计算,给出一个范围在0到1,000,000,000的数,求Fn的最后四位。

解题思路:

因为题目的数据很大而且题目提示了用矩阵进行计算。所以这里可以用矩阵的快速幂求解。

矩阵的快速幂可以高效地计算矩阵的高次方。将O(n)的时间复杂度降到O(log n)。

一般会通过连乘n-1次来得到矩阵的n次幂,而利用矩阵乘法的结合律可以减少重复计算的次数:

把n个矩阵进行两两分组,如:$$A\times A\times A\times A\times A\times A = (A\times A)\times(A\times A)\times(A\times A)$$ 这样变的好处是只需要计算一次A*A,然后将结果(A*A)连乘自己两次就能得到A^6,即(A*A)^3=A^6。一共乘了3次,少于原来的5次。

但上面是取一个具体的数来作为最小单位长度,虽然能够改进效率,但当n趋向无穷大时,取2或者3和1没什么区别。所以需要找到一种与n增长速度相适应的单位长度。

可以通过二进制将计算离散化。比如A^156,156的二进制为10011100,于是可以得到: $$A^{156}=(A^4)\times (A^8)\times (A^{16})\times (A^{128})$$

而(A^16)可以通过(A^8)*(A^2)得到,而(A^8)可以通过(A^4)*(A^2),每一次计算都可以利用现有的结果。

AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <memory>
using namespace std;

#define MAXNUM 26

int id=1;

class matrix
{
public:
    int a[2][2];
    matrix operator*(const matrix& p)
    {
        matrix tmp;
        tmp.a[0][0]=(a[0][0]*p.a[0][0]+a[0][1]*p.a[1][0])%10000;
        tmp.a[0][1]=(a[0][0]*p.a[0][1]+a[0][1]*p.a[1][1])%10000;
        tmp.a[1][0]=(a[1][0]*p.a[0][0]+a[1][1]*p.a[1][0])%10000;
        tmp.a[1][1]=(a[1][0]*p.a[0][1]+a[1][1]*p.a[1][1])%10000;
        return tmp;
    }
    void operator=(const matrix& p)
    {
        a[0][0]=p.a[0][0];
        a[0][1]=p.a[0][1];
        a[1][0]=p.a[1][0];
        a[1][1]=p.a[1][1];
    }
};
int multiply(matrix a,int n)
{
    matrix res;
    res.a[0][0]=res.a[1][1]=1;
    res.a[0][1]=res.a[1][0]=0;
    while(n)
    {
        if(n&1)
            res=res*a;
        n>>=1;
        a=a*a;
    }
    return res.a[0][1];
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF&&n!=-1)
    {
        matrix res;
        res.a[0][1]=res.a[0][0]=res.a[1][0]=1;
        res.a[1][1]=0;
        printf("%d\n",multiply(res,n));
    }
    return 0;
}


登录后评论

共有1条评论


是咸鱼啊

$$\left[\begin{matrix}F_{n+1}&F_n\\
F_n&F_{n-1}\\
\end{matrix}\right]=
\left[\begin{matrix} 1& 1\\
1&0\\
\end{matrix}\right]^n=
\underbrace{\left[\begin{matrix} 1& 1\\
1&0\\
\end{matrix}\right]\left[\begin{matrix} 1& 1\\
1&0\\
\end{matrix}\right]\cdots
\left[\begin{matrix} 1& 1\\
1&0\\
\end{matrix}\right]}_{ n\ times}\tag{0}
$$

2020-07-15 15:09