hdu Hat's Fibonacci
Hat's Fibonacci |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 789 Accepted Submission(s): 343 |
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
戴帽子的
|
Recommend
Ignatius.L
|
分析:大整数加法,利用分部整数数组计算
#include<stdio.h> #include<string.h> int main() { char a[6],r[201]; int n,i,j,x,count,jw,b[6],count1; int s[201]; while(scanf("%s%d",a,&n)==2) { memset(r,0,sizeof(r)); count=x=count1=0; for(i=5;i>=0;--i) { if(a[i]!='.') { b[count]=a[i]-'0'; ++count; } else x=n*(5-i); } for(i=0;i<x/n;++i) { if(!b[i]) { ++count1; } else break; } x-=n*count1; for(i=0;i<count-count1;++i) { b[i]=b[i+count1]; r[200-i]=b[i]; } while(--n) { memset(s,0,sizeof(s)); for(j=0;j<count-count1;++j) { for(i=200;i>=6;--i) { s[i-j]+=r[i]*b[j]; } } jw=0; for(i=200;i>=0;--i) { r[i]=(s[i]+jw)%10; jw=(s[i]+jw)/10; } } if(!x) { for(i=0;i<201;++i) if(r[i]) break; if(i==201) printf("0"); else { for(;i<201;++i) putchar(r[i]+'0'); } } else { for(i=0;i<=200-x;++i) if(r[i]) break; for(;i<=200-x;++i) putchar(r[i]+'0'); putchar('.'); for(;i<=200;++i) putchar(r[i]+'0'); } putchar(10); } return 0; }
这条路我们走的太匆忙~拥抱着并不真实的欲望~