60分求助

P1255 数楼梯

Greeper @ 2024-08-12 14:01:48

#include<bits/stdc++.h>
using namespace std;
long long a[5005];
long long s(long n)
{
    if(n<=2)
    {
        return n;
    }
    if(a[n])
    {
        return a[n];
    }
    a[n]=s(n-1)+s(n-2);
    return a[n];
}
int main()
{
    long long n;
    cin>>n;
    cout<<s(n);
    return 0;
}

by lij123 @ 2024-08-12 14:06:22

要用高精度


by nlyta @ 2024-08-12 14:08:01

再加一下高精


by ___LuXun___ @ 2024-08-12 14:08:06

@Greeper 抄吧↓

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5005;
int f[N][N];
int lenc=1;
int n;

void gadd(int k){
    for(int i=1;i<=lenc;i++){
        f[k][i]=f[k-1][i]+f[k-2][i];
    }
    for(int i=1;i<=lenc;i++){
        if(f[k][i]>9){
            f[k][i+1]+=f[k][i]/10;
            f[k][i]%=10;
        }
    }
    if(f[k][lenc+1]>0) lenc++;
    while(f[k][lenc]==0&&lenc>1) lenc--;
}

int main() {
    cin >> n;
    f[1][1]=1,f[2][1]=2;
    for(int i=3;i<=n;i++) gadd(i);
    for(int i=lenc;i>=1;i--) cout<<f[n][i];
    return 0;
}

by Greeper @ 2024-08-12 14:09:53

@LuXun 算了吧,学会再来做


by ___LuXun___ @ 2024-08-12 14:10:48

@Greeper 为什么,这可是大水题


by nlyta @ 2024-08-12 14:12:30

高精背代码


by Greeper @ 2024-08-12 14:23:13

@LuXun 我怕抄题答案被封号,我好不容易刷的绿名


by ___LuXun___ @ 2024-08-12 14:25:33

@Greeper 不会的


by guanzisheng2 @ 2024-08-13 18:40:12

#include<bits/stdc++.h>
using namespace std;
int n;
vector<int> dp[5005];
vector<int> add(vector<int> a,vector<int> b){
    vector<int>  c(max(a.size(),b.size())+1);
    for(int i=0;i<int(a.size());i++){
        c[i]+=a[i];
    }
    for(int i=0;i<int(b.size());i++){
        c[i]+=b[i];
    }
    for(int i=0;i<int(c.size());i++){
        if(c[i]>=10){
            c[i+1]++;
            c[i]%=10;
        }
    }
    if(!c.back()){
        c.pop_back();
    }
    return c;
}
int main(){
    cin>>n;
    dp[0].push_back(1);
    dp[1].push_back(1);
    for(int i=2;i<=n;i++){
        dp[i]=add(dp[i-2],dp[i-1]);
    }
    for(int i=int(dp[n].size())-1;i>=0;i--){
        cout<<dp[n][i];
    }
    return 0;
}

by qwq_better_on_coding @ 2024-08-19 21:46:58

@Greeper抄完了不交就好了啊,顺便学习一下高精度


|