HDU 3117Fibonacci Numbers(求斐波那契前四位与后四位 数论知识+矩阵快速幂)

来源:互联网 发布:乐高玩具淘宝 编辑:程序博客网 时间:2024/05/17 01:24

Fibonacci Numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1387    Accepted Submission(s): 576


Problem Description
The Fibonacci sequence is the sequence of numbers such that every element is equal to the sum of the two previous elements, except for the first two elements f0 and f1 which are respectively zero and one.

What is the numerical value of the nth Fibonacci number?
 

Input
For each test case, a line will contain an integer i between 0 and 108 inclusively, for which you must compute the ith Fibonacci number fi. Fibonacci numbers get large pretty quickly, so whenever the answer has more than 8 digits, output only the first and last 4 digits of the answer, separating the two parts with an ellipsis (“...”). 

There is no special way to denote the end of the of the input, simply stop when the standard input terminates (after the EOF).
 

Sample Input
0123453536373839406465
 

Sample Output
0112359227465149303522415781739088169632459861023...41551061...77231716...7565
 


                  题目大意:斐波那契知识大家都懂一点吧!f(n)=f(n-1)+f(n-2)这是它的递推式,他的通项是f(n)=[((1.0+sqrt(5.0))/2)^n - ((1.0-sqrt(5.0))/2)^n]/sqrt(5). 为什么网上好多博客都中间都写的是加号,真心不解,那样连f(0)=0,f(1)=1,都得不到。

          解题思路:前四位容易求。先普及一下知识。
m=375,375=10^2.5740312677277188
a=2,b=0.5740312677277188
log10(m)-(int)log10(m)=(a+b)-a=b
那么10^b是什么东西呢?
实际上代表的是最高位,再乘以10的话就是高二位

           然后就是后四位了。给的n是10^8,又是体现快速幂作用的时候到了。


             快速幂的思想,可以参考:矩阵的快速幂

AC代码:
#include<iostream>#include<cmath>#include<cstdio>using namespace std;int ret[2][2],tmp[2][2],p[2][2];int mo=10000,n;//用矩阵快速幂求斐波那契后四位int first()  //求前四位{    double ans;    ans=n*log10(0.5+0.5*sqrt(5.0))-0.5*log10(5.0);    ans=ans-(int)ans;    ans=pow(10.0,ans);    while(ans<1000)        ans*=10;    return (int)ans;}void init(){    ret[0][0]=1; ret[0][1]=1;    ret[1][0]=1; ret[1][1]=0;    p[0][0]=1; p[0][1]=1;    p[1][0]=1; p[1][1]=0;}void cal1(){    int i,j,k;    for(i=0;i<2;i++)        for(j=0;j<2;j++)        {            tmp[i][j]=p[i][j];            p[i][j]=0;        }    for(i=0;i<2;i++)        for(j=0;j<2;j++)          for(k=0;k<2;k++)             p[i][j]=((p[i][j]+tmp[i][k]*tmp[k][j])%mo+mo)%mo;}void cal2(){    int i,j,k;    for(i=0;i<2;i++)        for(j=0;j<2;j++)        {            tmp[i][j]=ret[i][j];            ret[i][j]=0;        }    for(i=0;i<2;i++)        for(j=0;j<2;j++)          for(k=0;k<2;k++)             ret[i][j]=((ret[i][j]+tmp[i][k]*p[k][j])%mo+mo)%mo;}void fastmi(){    n-=2;    while(n)    {        if(!(n&1))        {            cal1();            n>>=1;        }        else        {            cal2();            n--;        }    }}int main(){    int fi[45],i;    fi[0]=0,fi[1]=1;    for(i=2;i<40;i++)        fi[i]=fi[i-1]+fi[i-2];    while(~scanf("%d",&n))    {        if(n<40)  //不超过8位的直接输出        {            printf("%d\n",fi[n]);            continue;        }        printf("%d...",first());  //计算高四位        init();        fastmi(); //计算低四位        printf("%04d\n",ret[0][0]);    }    return 0;}

很久以前就想把这个题目A了。。
原创粉丝点击