您的位置:首页 > 其它

HDU 1250-Hat's Fibonacci

2013-04-06 20:39 323 查看

Hat's Fibonacci

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

Total Submission(s): 4730 Accepted Submission(s): 1610



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
Each line will contain an integers. Process to end of file.

[align=left]Output[/align]
For each case, output the result in a line.

[align=left]Sample Input[/align]

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.[align=left]Output[/align] For each case, output the result in a line.

题目给出公式

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),然后给出n,要求出F
,数据比较大,要用到大数运算

AC code:

#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>

using namespace std;

int f[4][2100];
int ans[2100];

int main()
{
int N;

while(cin>>N)
{
if(N<=4)
{
cout<<"1"<<endl;
continue;
}
memset(f,0,sizeof f);
memset(ans,0,sizeof ans);
f[0][0]=f[1][0]=f[2][0]=f[3][0]=1;
int len=1;
for(int i=4;i<N;i++)
{
int carry=0;
for(int j=0;j<len;j++)
{
int temp=carry+f[0][j]+f[1][j]+f[2][j]+f[3][j];
ans[j]=temp%10;
carry=temp/10;
}
while(carry!=0)
{
ans[len++]=carry%10;
carry/=10;
}
for(int j=0;j<len;j++)
{
f[i%4][j]=ans[j];
}
}
for(int i=len-1;i>=0;i--)
cout<<ans[i];
cout<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: