我哪错了

P1255 数楼梯

wanglaiyi @ 2023-02-20 19:43:11

???

#include<iostream>
using namespace std;
int a[10005];
int main(){
    int n;
    cin>>n;
    for(int i=1; i<=n; i++){
        a[1]=1;
        a[2]=2;
        a[i]=a[i-1]+a[i-2];
    }
    cout<<a[n];
    return 0;
}

by Loser_Syx @ 2023-02-20 19:46:42

@wanglaiyi 高精度


by Esawkm @ 2023-02-20 19:55:26

1≤N≤5000

by zhang_kevin @ 2023-02-20 20:18:42

@wanglaiyi 请使用高精度进行计算


by _BCO_ @ 2023-02-25 21:44:34


请考虑数据范围

by Roger_Spencer @ 2023-03-20 09:24:55

高精度+滚动数组+dP

博客主页

源码

#include <bits/stdc++.h>

using namespace std;

#define LL long long

#define endl "\n"

const int N = 1e5 + 10;

const int M = 5510;

int dp[N];

vector<int> add(vector<int> &A, vector<int> &B)
{
    if (A.size() < B.size()) return add(B, A);

    vector<int> C;
    int t = 0;
    for (int i = 0; i < A.size(); i ++ )
    {
        t += A[i];
        if (i < B.size()) t += B[i];
        C.push_back(t % 10);
        t /= 10;
    }

    if (t) C.push_back(t);
    return C;
}

void solve()
{
    int n;
    cin >> n;

    if ( n == 1) 
    {
        cout << 1 << endl;
        return;
    }

    vector<int> A, B, C, D;

    A.push_back(1);
    B.push_back(1);
    for (int i = 2; i <= n; i ++ )
    {
        C = add(A, B);
        A = B;
        B = C;
    }

    for (int i = C.size() - 1; i >= 0; i -- ) cout << C[i];

    cout << endl;

}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();

    return 0;
}

|