60分,咋办

P1255 数楼梯

baibaobao666 @ 2023-03-03 21:19:43

#include <iostream>
using namespace std;

long long f(long long n,long long last,long long now,long long cnt)
{
    if(cnt == n)
    return now;
    return f(n,now,last+now,cnt+1);
}

int main()
{
    int n;
    cin >> n;
    cout << f(n,1,1,1) << endl;

    return 0;
}

by InversionShadow @ 2023-03-03 21:26:25

高精度


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

高精度+滚动数组+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;
}

|