您的位置:首页 > 其它

hdu1250

2015-08-23 10:41 204 查看


Hat's Fibonacci

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9310    Accepted Submission(s): 3039


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.

 

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int a[5][2010];
int i,j;
int temp;
int n,len;
while(cin>>n)
{
memset(a,0,sizeof(a));
a[0][0]=a[1][0]=a[2][0]=a[3][0]=1;
len=1;
for(i=4;i<n;i++)
{
temp=0;
for(j=0;j<len;j++)
{
temp+=a[(i-4)%5][j]+a[(i-3)%5][j]+a[(i-2)%5][j]+a[(i-1)%5][j];
a[i%5][j]=temp%10;
temp/=10;
}
while(temp)
{
a[i%5][j++]=temp%10;
temp/=10;
}
len=j;
}
len--;
for(;len>=0;len--)
cout<<a[(n-1)%5][len];
cout<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: