Fibonacci Sequence hdu1250

Source: Internet
Author: User


Hat ' s Fibonaccicrawling in process ... crawling failed Time limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64u

Description A Fibonacci sequence is calculated by adding the previous and the sequence, with the first and both 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 would contain an integers. Process to end of file.

Output for each case, output of the result in a line.

Sample Input

100

Sample Output

4203968145672990846840663646 Note:no generated Fibonacci number in excess of 2005 digits would be in the test data, ie. F () = 66526 has 5 digits.


#include <iostream>
#include <stdio.h>
using namespace Std;
int a[8000][255]={{0}};
int n,k,ans,i,j,m;
int main ()
{
for (I=1; i<5; i++) a[i][1]=1;
for (i=5; i<8000; i++)
for (j=1; j<255; j + +)
{
A[I][J]+=A[I-1][J]+A[I-2][J]+A[I-3][J]+A[I-4][J];
a[i][j+1]+=a[i][j]/100000000;
a[i][j]=a[i][j]%100000000;//note here the bigger the better, I just started on 10000 operation, are WA, the corresponding below also should be%. 8d
}
while (scanf ("%d", &n)!=eof)
{
for (j=254; j>0; j--)
if (a[n][j]!=0)
{
K=j;
cout<<a[n][j];
Break

}
for (m=k-1; m>0; m--)
printf ("%.8d", a[n][m]);//must be handled this way, otherwise 111100001111 will only output 111101111
cout<<endl;


}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.