ZCMU 1041: 二哥的困惑 Ⅳ(大數斐波那契數列)

1041: 二哥的困惑 Ⅳ

Description
A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.

f(1) = 1, f(2) = 1, f(n > 2) = f(n - 1) + f(n - 2)

Your task is to take a number as input, and print that Fibonacci number.

Input

Input contains several cases, every case contains a positive integer number N.

Output
print the Nth Fibonacci number.

Sample Input

100

Sample Output

354224848179261915075

HINT
一個大數斐波那契數列

錯誤代碼1Code(超時了):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int a[1000000];
int b[1000000];
int c[1000000];
void dsf(int n)
{

    //printf("1\n");
    if(n==0)
    {
        printf("0");
        return ;
    }
    if(n==1||n==2)
    {
        printf("1");
        return ;
    }
    int l1=1,l2=1,l3=1;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    n-=2;
    a[0]=1;b[0]=1;
    while(n--)
    {
        //printf("1\n");
        memset(c,0,sizeof(c));
        for(int i=0; i<l2; i++)
        {
            c[i]+=a[i]+b[i];
            c[i+1]+=c[i]/10;
            c[i]%=10;
        }
        //printf("1\n");
        if(c[l1]==1)l3++;
        //for(int i=0; i<l3; i++)printf("%d",c[i]);
        for(int i=0; i<l2; i++)
        {
            a[i]=b[i];
        }
        l1=l2;
        for(int i=0; i<l3; i++)
        {
            b[i]=c[i];
        }
        l2=l3;//printf("1\n");
    }
    //printf("%d\n",c[0]);
    for(int i=l3-1; i>=0; i--)printf("%d",c[i]);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        dsf(n);
        printf("\n");
    }
    return 0;
}

錯誤代碼二(超時):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;

int a[1000000];
int b[1000000];
int c[1000000];
void dsf(int n)
{

    //printf("1\n");
    if(n==0)
    {
        printf("0");
        return ;
    }
    if(n==1||n==2)
    {
        printf("1");
        return ;
    }
    int l1=1,l2=1,l3=1;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    n-=2;
    a[0]=1;b[0]=1;
    while(n--)
    {
        //printf("1\n");
        memset(c,0,sizeof(c));
        for(int i=0; i<l2; i++)
        {
            c[i]+=a[i]+b[i];
            c[i+1]+=c[i]/100000000;
            c[i]%=100000000;
        }
        //printf("1\n");
        if(c[l1]==1)l3++;
        //for(int i=0; i<l3; i++)printf("%d",c[i]);
        for(int i=0; i<l2; i++)
        {
            a[i]=b[i];
        }
        l1=l2;
        for(int i=0; i<l3; i++)
        {
            b[i]=c[i];
        }
        l2=l3;//printf("1\n");
    }
    //printf("%d\n",c[0]);
    for(int i=l3-1; i>=0; i--)printf("%d",c[i]);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        dsf(n);
        printf("\n");
    }
    return 0;
}

AC代碼(最後還是打了表):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int i, j, n, a[8000][255];
int main(void)
{
    a[1][1] = 1;
    a[2][1] = 1;
    for (i = 3; i<8000; i++)
        for (j = 1; j<255; j++)
        {
            a[i][j] += a[i - 1][j] + a[i - 2][j];
            a[i][j + 1] += a[i][j] / 100000000;
            a[i][j] = a[i][j] % 100000000;
        }
 
    while (~scanf("%d", &n))
    {
        for (i = 254; i>0; i--)
            if (a[n][i])
                break;
        printf("%d", a[n][i]);
        for (--i; i>0; i--)
            printf("%.8d", a[n][i]);
        printf("\n");
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章