HDU1250~Hat's Fibonacci(大数加法)

来源:互联网 发布:plc编程教学 编辑:程序博客网 时间:2024/05/22 04:10

Hat's Fibonacci

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


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
4203968145672990846840663646Note:No generated Fibonacci number in excess of 2005 digits will be in the test data, ie. F(20) = 66526 has 5 digits.
 

Author
戴帽子的 

Recommend
Ignatius.L
一看数据就明白了是大数,模拟吧。。
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>#include<queue>#include<vector>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3ftypedef long long LL;using namespace std;//注意,此处数组不能开小了,开的太小算不出2005位char a[10005][2005];int main(){    int num[100005];    int n,sum=1;    mem(a,'0');    a[1][1]='1';    a[2][1]='1';    a[3][1]='1';    a[4][1]='1';    //一直计算到2005位停止        for(int k=5;;k++)    {        for(int i=k-4;i<k;i++)        {           for(int j=1;j<=sum;j++)           {               a[k][j]+=a[i][j]-'0';               if(a[k][j]>'9')               {                   int t=(a[k][j]-'0')/10;                   a[k][j]=(a[k][j]-'0')%10+'0';                   a[k][j+1]+=t;                   if(j==sum)                   {                       sum++;                   }                   if(sum==2005)                      break;               }               if(sum==2005)                break;           }           if(sum==2005)        break;        }         num[k]=sum;    if(sum==2005)        break;    }    while(~scanf("%d",&n))    {         if(n<=4)         {             printf("1\n");             continue;         }    for(int i=num[n];i>=1;i--)        printf("%c",a[n][i]);        printf("\n");    }}



 
0 0
原创粉丝点击