Fibonaccitime limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 3569 accepted submission (s): 1627
Problem description is coming in 2007. After one year of practice in 2006, zouyu, a mathematical prodigy, finally ranked 0 to 100000000 In the Fibonacci series.
(F [0] = 0, F [1] = 1; F [I] = f [I-1] + F [I-2] (I> = 2 )) all the values are backed up.
Next, codestar decided to test him, so every time he asked him a number, he would say the answer, but some numbers are too long. Therefore, it is required that only the first four digits can be said, but codestar cannot remember it. So he decided to write a program to test whether zouyu was correct.
Input several numbers N (0 <= n <= 100000000), one row for each number. Read the end of the file.
Output outputs the first four digits of F [N] (if there are less than four digits, all are output ).
Sample Input
012345353637383940
Sample output
011235922714932415390863241023
Authordaringqq
The sourcehappy 2007 Fibonacci formula, the process of solving, you can use the formula to first find its fractional part, then calculate its power, the obtained value, perform the multiplication ten operation, until it becomes a four-digit code as follows:
</pre><pre name="code" class="cpp">//斐波那契数列公式:F(n)=[((1+sqrt(5.0)/2)^n-((1-sqrt(5.0))/2)^n]/sqrt(5.0);//求大数的前几位数字,可以用公式,先求出小数部分,然后进行 //由于(1-sqrt(5.0))/2的N次方非常小,所以可以忽略。 #include<stdio.h>#include<math.h>int fib[22];int main(){double num;int n,f,i;fib[0]=0;fib[1]=1;for(i=2;i<=20;i++)fib[i]=fib[i-1]+fib[i-2];while(~scanf("%d",&n)){if(n<21)//由于小于20的斐波那契数列小于四位数字,所以可以直接输出。 {printf("%d\n",fib[n]);continue;}num=n*(log10((1+sqrt(5.0))/2.0))-log10(sqrt(5.0));num-=(int)num;//得到小数部分 num=pow(10,num);//得到第一位数 while(num<1000)num*=10;f=num;//得到最后的四位数,消除小数部分 printf("%d\n",f);}return 0;}
HDU 1568 (Fibonacci) (the Fibonacci formula for large numbers)