hdu 1250 Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7434 Accepted Submission(s): 2416
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.
Input
Each line will contain an integers. Process to end of file.
Output
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7434 Accepted Submission(s): 2416
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.
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.
<span style="font-size:14px;">/*题解:
用数组元素表示大数的每位数字,这道题用上面那种方法,可惜效率不高,网上有另外一种,每个元素存储8个数字,效率提高不少,废话不说了,直接上代码:
*/
#include<cstdio>
#include<cstring>
#define N 10000
int a[N][260]; //每个元素存储8位,开到260就行了
int main()
{
memset(a,0,sizeof(a));
a[1][0]=1;
a[2][0]=1;
a[3][0]=1;
a[4][0]=1;
int i,j,c,ans,m,d;
for(i=5,d=1; i<N; i++)
{
for(j=0,c=0; j<260; j++)
{
ans=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j]+c;
c=ans/100000000;
a[i][j]=ans%100000000; //每8位进一位
}
//260不是很大,这里不需要再控制位数
}
while(scanf("%d",&m)!=EOF)
{
j=259;
while(a[m][j]==0)
j--;
printf("%d",a[m][j]); //去零
for(i=j-1; i>=0; i--)
printf("%08d",a[m][i]); //每个元素存储8位,不足补零
printf("\n");
}
return 0;
}
/*
第一次做的代码:题目没给准确测试数据,这样写本来效率也不高,果断TLE了.
*/
TEL代码:
#include<cstdio>
#include<cstring>
#define N 2005
int a[N][N];
int main()
{
memset(a,0,sizeof(a));
a[1][0]=1;
a[2][0]=1;
a[3][0]=1;
a[4][0]=1;
int i,j,c,ans,m,d;
for(i=5,d=1; i<N; i++)
{
for(j=0,c=0; j<d; j++)
{
ans=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j]+c;
c=ans/10;
a[i][j]=ans%10;
}
while(c)
{
a[i][d++]=c%10;
c/=10;
}
}
while(scanf("%d",&m)!=EOF)
{
j=N-1;
while(a[m][j]==0)
j--;
for(i=j; i>=0; i--)
printf("%d",a[m][i]);
printf("\n");
}
return 0;
} </span>