首页 > 代码库 > HDU1250_Hat's Fibonacci【大数】【水题】

HDU1250_Hat's Fibonacci【大数】【水题】

Hat‘s Fibonacci


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7854    Accepted Submission(s): 2551

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.
 
Author

戴帽子的

#include<stdio.h>
#include<string.h>

int f1[2050],f2[2050],f3[2050],f4[2050],f5[2050];

int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        if(N==1)
            printf("1\n");
        else if(N==2)
            printf("1\n");
        else if(N==3)
            printf("1\n");
        else if(N==4)
            printf("1\n");
        else
        {
            memset(f1,0,sizeof(f1));
            memset(f2,0,sizeof(f2));
            memset(f3,0,sizeof(f3));
            memset(f4,0,sizeof(f4));
            memset(f5,0,sizeof(f5));
            f1[0] = f2[0] = f3[0] = f4[0] = 1;
            for(int i = 5; i<= N; i++)
            {
                for(int j = 0; j <= 2010; j++)
                {
                    f5[j] = f1[j] + f2[j] + f3[j] + f4[j];
                    if(f5[j] >= 10)
                    {
                        f1[j+1] += (f5[j]/10);
                        f5[j] = (f5[j]%10);
                    }
                    f1[j] = f2[j];
                    f2[j] = f3[j];
                    f3[j] = f4[j];
                    f4[j] = f5[j];

                }
            }
            int j;
            for(j = 2010; j >= 0; j--)
                if(f5[j]!=0)
                    break;
            for(int i = j; i >= 0; i--)
                printf("%d",f5[i]);
            printf("\n");
        }
    }
    return 0;
}


HDU1250_Hat's Fibonacci【大数】【水题】