/*
解题人:lingnichong
解题时间:2014-10-18 23:48:54
解题体会:一开始用数组存一位的话显得有点浪费,还容易超内存,数组开小了,又会显示访问到未知内存,可以每个数组的每个位上存上100000这样大的数。
*/
Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7831 Accepted Submission(s): 2547
Problem Description
A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.
F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4)
Your task is to take a number as input, and print that Fibonacci number.
Input
Each line will contain an integers. Process to end of file.
Output
For each case, output the result in a line.
Sample Input
100
Sample Output
4203968145672990846840663646 Note: No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
Author
戴帽子的
#include<stdio.h>
#include<string.h>
int a[8000][1000];
int main()
{
int i,j,k,len,t,p;
memset(a,0,sizeof(a));
a[1][0]=1;a[2][0]=1;
a[3][0]=1;a[4][0]=1;
for(i=5;i<8000;i++)
{
for(j=1000-1;j>=0;j--)
if(a[i-1][j]!=0)
break;
for(k=0;k<=j;k++)
{
a[i][k]+=(a[i-1][k]+a[i-2][k]+a[i-3][k]+a[i-4][k]);
if(a[i][k]>=100000)//每一位数组存下100000,满五位进一
{
p=a[i][k];
a[i][k]%=100000;
a[i][k+1]+=(p/100000);
}
}
}
while(~scanf("%d",&t))
{
for(i=1000-1;(i>=0)&&(a[t][i]==0);i--);
printf("%d",a[t][i--]);//输出一开始输出的第一位数字,
for(;i>=0;i--)
printf("%.5d",a[t][i]);//后面每五位输出次,因为是每五位进一
printf("\n");
}
return 0;
}