60分求助

P1255 数楼梯

lanhu @ 2024-07-07 08:56:20

#include<bits/stdc++.h>
using namespace std;
int n;
long long f[5050];
int main(){
    cin>>n;
    f[0]=f[1]=1;
    for(int i=2;i<=n;i++){
        f[i]=f[i-1]+f[i-2];
    }
    cout<<f[n]<<endl;
    return 0;
}  

by __oio__ @ 2024-07-07 09:01:21

这道题是高精+递推


by CZY880201 @ 2024-07-07 09:15:03

这道题,由于斐波那契数列增加的很快

所以

十年OI一场空,不打高精见祖宗


by CZY880201 @ 2024-07-07 09:16:56

#include <bits/stdc++.h>
using namespace std;
struct Bigint{
    int len,a[4000];
    Bigint(int x=0){
        memset(a,0,sizeof(a));
        for(len=1;x;len++){
            a[len]=x%10,x/=10;
        }
        len--;
    }
    int &operator[](int i){
        return a[i];
    }
    void flatten(int L){
            len=L;
            for(int i = 1;i <=len;i++){
                a[i+1]+=a[i]/10,a[i]%=10;
            }
            for(;!a[len];){
                len--;
            }

    }
    void print(){
        for(int i = max(len,1);i>=1;i--)
        {
            printf("%d",a[i]);
        }
    }
};
Bigint operator+(Bigint a,Bigint b){
    Bigint c;
    int len=max(a.len,b.len);
    for(int i = 1;i <= len;i++)
    {
        c[i]=a[i]+b[i];
    }
    c.flatten(len+2);
    return c;
}
Bigint a[5005];
int main(){
    int n;
    cin >> n;
    a[1]=1;
    a[2]=2;
    for(int i = 3;i <= n;i++)
    {
        a[i]=a[i-1]+a[i-2];
    }
    a[n].print();
    return 0;
}

|