(two-dimensional array of billions or scrolling arrays) Hat's Fibonacci hdu1250

Source: Internet
Author: User

Hat ' s Fibonacci

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)

Total submission (s): 12284 Accepted Submission (s): 4124

Problem 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, the 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.

The string will time out, and the following is the timeout code.

#include <iostream>#include<string>using namespacestd;stringAddstringAstringb) {    intlen1=a.length (); intLen2=b.length (); inti; if(len1>len2) {         for(i=1; i<=len1-len2;i++) b="0"+b; }    Else    {         for(i=1; i<=len2-len1;i++) A="0"+A; }    stringstr; intcf=0, T; Len1=a.length ();  for(i=len1-1; i>=0; i--) {T=a[i]-'0'+b[i]-'0'+CF; CF=t/Ten; T%=Ten; STR=Char(t+'0')+str; }    if(cf!=0) Str=Char(cf+'0')+str; returnstr;}stringFunintN) {    stringf[10010]; f[1]="1"; f[2]="1"; f[3]="1"; f[4]="1"; inti; stringA,b,c;  for(i=5; i<=n;i++) {a=add (f[i-1],f[i-2]); b=add (f[i-3],f[i-4]); F[i]=Add (A, b); }    returnf[n];}intMain () {intN;  while(cin>>N) {stringstr; STR=Fun (n); cout<<str<<Endl; cout<<Endl; }    return 0;}
View Code

The correct code

method One: Using two-dimensional arrays and billions of binary. #include<cstdio>#include<iostream>#include<cstring>using namespacestd;intstr[10001][260];//must be of type int. intMain () {memset (str,0,sizeof(str)); str[1][0]=1; str[2][0]=1; str[3][0]=1; str[4][0]=1; inti,j,ans=0, C,n;  for(i=5;i<10001; i++)    {         for(j=0, c=0;j<260; j + +)//Loop to enumerate the 8-bit numbers of the J arrays. {ans=str[i-1][j]+str[i-2][j]+str[i-3][j]+str[i-4][j]+C; C=ans/100000000; STR[I][J]=ans%100000000;//each number group is stored in 8 digits and C to determine whether to carry.     }    }     while(cin>>N) {j=259;  while(!str[n][j])//the first one is 0, clearing out 0. j--; cout<<str[n][j];//The first 0 of the beginning clears the X-digit number, which may be less than 8 bits.          for(i=j-1; i>=0; i--) printf ("%08d", Str[n][i]);//each 8 digit output one set, insufficient auto part 0. printf"\ n"); }    return 0;} Method Two: Solving # include with a rolling array<iostream>#include<cstdio>#include<cstring>using namespacestd; intt[6][2555]; intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {memset (T,0,sizeof(t)); t[0][0] =1; t[1][0] =1; t[2][0] =1; t[3][0] =1;  for(inti =4; I < n;i++)          {              intcarry =0; intK = i%4;  for(intj =0; J <2500; j + +)              {                  intx = t[0][J] + t[1][J] + t[2][J] + t[3][j]; T[K][J]= x +carry; Carry= T[k][j]/Ten; T[K][J]%=Ten; }          }          intK =2500;  while(T[(N-1) %4][--K] = =0);  for(inti = K;i >=0; i--) {printf ("%d", t[(N-1) %4][i]); } printf ("\ n"); }                  return 0; }
View Code

With Java

ImportJava.math.BigInteger;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); BigInteger a[]=Newbiginteger[10001]; intN;  while(In.hasnextint ()) {n=In.nextint (); a[1]=Biginteger.one; a[2]=Biginteger.one; a[3]=Biginteger.one; a[4]=Biginteger.one;  for(inti=5;i<=10000;i++) {A[i]=Biginteger.zero; A[i]=a[i].add (a[i-1]); A[i]=a[i].add (a[i-2]); A[i]=a[i].add (a[i-3]); A[i]=a[i].add (a[i-4]);        } System.out.println (A[n]); }    }}
View Code

(two-dimensional array of billions or scrolling arrays) Hat's Fibonacci hdu1250

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.