hdu1250 Hat's Fibonacci(大数加法二)模板

 

Hat's Fibonacci

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

 

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.
 
分析:
大数加法一
因为有2005位,大概估计一下第8000项可以超过2005位 ,时间复杂度是8000*2000,也就是O(10^7)
View Code
 1 #include<iostream>
 2 #include<string>
 3 #define N 7050
 4 using namespace std;
 5 
 6 
 7 string bigmult(string a,string b)
 8 {
 9     int la,lb,lc,i,j,flag;
10     string c="";
11     char t;
12     i=la=a.length()-1;
13     j=lb=b.length()-1;
14     flag=0;
15     while(i>=0&&j>=0)
16     {
17         t=a[i]+b[j]+flag-'0';
18         if(t>'9') 
19         {
20             t=t-10;flag=1;
21         }
22         else flag=0;
23         c+=t;
24         i--;j--;
25     }
26     while(i>=0)
27     {
28         t=a[i]+flag;
29         if(t>'9')
30         {
31             t=t-10;flag=1;
32         }
33         else flag=0;
34         c+=t;
35         i--;
36     }
37     while(j>=0)
38     {
39         t=b[j]+flag;
40         if(t>'9')
41         {
42             t=t-10;flag=1;
43         }
44         else flag=0;
45         c+=t;
46         j--;
47     }
48     if(flag) c+=(flag+'0');
49     lc=c.length();
50     for(i=0,j=lc-1;i<j;i++,j--)
51     {
52         t=c[i];c[i]=c[j];c[j]=t;
53     }
54     return c;
55 }
56 
57 string f[N];
58 
59 int main()
60 {
61     int n,i;
62     f[1]=f[2]=f[3]=f[4]="1";
63     for(i=5;i<N;i++)
64     {
65         f[i]=bigmult(f[i-1],f[i-2]);
66         f[i]=bigmult(f[i],f[i-3]);
67         f[i]=bigmult(f[i],f[i-4]);
68     }
69     while(cin>>n)
70     {
71         cout<<f[n]<<endl;
72     }
73     return 0;
74 }

 

对大数加法一改进
对大数的加法一段一段的加,也就是没 8 位看成一个整形的数(mod 10^9),进行整数加法;
时间复杂度8000*2000/8 ,也就是 O(10^6)
如果改用int64时间复杂度还可以降下去。
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #define N 10000
 4 #define M 300
 5 using namespace std;
 6 
 7 int f[N][M];
 8 
 9 void work()
10 {
11     memset(f,0,sizeof(f));
12     f[1][1]=f[2][1]=f[3][1]=f[4][1]=1;
13     int i,j,t;
14     for(i=5;i<N;i++)
15     {
16         t=0;
17         for(j=1;j<M;j++)
18         {
19             t=t+f[i-1][j]+f[i-2][j]+f[i-3][j]+f[i-4][j];
20             f[i][j]=t%100000000;
21             t=t/100000000;
22         }
23     }
24 }
25 
26 int main()
27 {
28     int n,i;
29     work();
30     while(scanf("%d",&n)!=EOF)
31     {
32         i=M-1;
33         while(f[n][i]==0)i--;
34         printf("%d",f[n][i--]);
35         while(i>=1)
36         {
37             printf("%08d",f[n][i]);
38             i--;
39         }
40         printf("\n");
41     }
42     return 0;
43 }

 

 

posted @ 2012-05-07 11:37  mtry  阅读(818)  评论(0)    收藏  举报