Hat's Fibonacci
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6880 Accepted Submission(s): 2270
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.
/*
1250 Hat's Fibonacci
大数运算 少于2005位
以下的方法 内存超过了
int a[7036][2050]={0},b[7036];
int main(){
int i,j,n,k,z;
a[1][0]=1;
a[2][0]=1;
a[3][0]=1;
a[4][0]=1;
b[1]=1;
b[2]=1;
b[3]=1;
b[4]=1;
k=1;//长度
for(i=5;i<7036;i++)
{
for(j=0;j<k;j++)
a[i][j]=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j];
z=0;//进位
for(j=0;j<k;j++)
{
a[i][j]+=z;
z=a[i][j]/10;
a[i][j]%=10;
}
while(z)//仍有进位
{
a[i][k++]=z%10;
z/=10;
}
b[i]=k;
}
重新构思方法 每个里面放8位 本来是10进位 改为10^8
*/
#include<iostream>
#include<cmath>
using namespace std;
int a[7036][251]={0},b[7036];
int main(){
int i,j,n,k,z;
a[1][0]=1;
a[2][0]=1;
a[3][0]=1;
a[4][0]=1;
b[1]=1;
b[2]=1;
b[3]=1;
b[4]=1;
k=1;//长度
for(i=5;i<7036;i++)
{
for(j=0;j<k;j++)
a[i][j]=a[i-1][j]+a[i-2][j]+a[i-3][j]+a[i-4][j];
z=0;//进位
for(j=0;j<k;j++)
{
a[i][j]+=z;
z=a[i][j]/100000000;
a[i][j]%=100000000;
}
while(z)//仍有进位
{
a[i][k++]=z%100000000;
z/=100000000;
}
b[i]=k;
}
while(scanf("%d",&n)!=EOF)
{
printf("%d",a[n][b[n]-1]);//第一位不输出前面的0
for(j=b[n]-2;j>=0;j--)
printf("%08d",a[n][j]);
printf("\n");
}
return 0;
}