hdu 3117

来源:互联网 发布:php做直播 编辑:程序博客网 时间:2024/06/06 09:46
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
 

Source
IPCP 2005 Northern Preliminary for Northeast North-America
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1588 1757 2294 2256 2971 
#include <iostream>#include <math.h>#include <stdio.h>#define mod 10000using namespace std;long long a[45];typedef struct{ long long m[2][2];}hehe;hehe p={0,1,        1,1};hehe i={1,0,        0,1};hehe chengji (hehe a,hehe b){  int i,j,k;  hehe c;  for(i=0;i<2;i++)   for(j=0;j<2;j++)   {    c.m[i][j]=0;    for(k=0;k<2;k++)     c.m[i][j]+=(a.m[i][k]*b.m[k][j])%mod;     c.m[i][j]%=mod;  }  return c;}hehe quickpow(long long n){ hehe m=p,b=i; while(n>=1) {  if(n&1)  b=chengji(b,m);  n=n>>1;  m=chengji(m,m); } return b;}int main(){    a[0]=0,a[1]=1;    for(int i=2;i<=45;i++)    {     a[i]=a[i-1]+a[i-2];    }    int n,len;    double x,tmp;    hehe ans;    while(cin>>n)    {     if(n<=39)     cout<<a[n]<<endl;     else     {     len=-1*log10(sqrt(5.0))+n*log10((1+sqrt(5.0))/2)+1;     x=-1*log10(sqrt(5.0))+n*log10((1+sqrt(5.0))/2);     tmp=x-len+4;    cout <<(int)pow(10,tmp);    cout<<"...";    ans=quickpow(n);     int res=ans.m[0][1];     printf("%04d\n",res);     }    }    return 0;}关于斐波拉契数列的综合题,矩阵的快速幂
0 0
原创粉丝点击