HDU 1250-Hat's Fibonacci

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

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.
Output
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[n],数据比较大,要用到大数运算

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;
    }
}






 

posted @ 2013-04-06 22:03  坚固66  阅读(139)  评论(0编辑  收藏  举报