求救一半的TLE

P1255 数楼梯

Azusares @ 2024-03-01 13:33:45

#include<bits/stdc++.h>
using namespace std;
string sub(string str1,string str2)
{
    string str;
    int tmp=str1.length()-str2.length();
    int cf=0;
    for(int i=str2.length()-1;i>=0;i--)
    {
        if(str1[tmp+i]<str2[i]+cf)
        {
            str=char(str1[tmp+i]-str2[i]-cf+'0'+10)+str;
            cf=1;
        }
        else
        {
            str=char(str1[tmp+i]-str2[i]-cf+'0')+str;
            cf=0;
        }
    }
    for(int i=tmp-1;i>=0;i--)
    {
        if(str1[i]-cf>='0')
        {
            str=char(str1[i]-cf)+str;
            cf=0;
        }
        else
        {
            str=char(str1[i]-cf+10)+str;
            cf=1;
        }
    }
    str.erase(0,str.find_first_not_of('0'));
    return str;
}
string add(string str1,string str2)
{
    string str;
    int len1=str1.length();
    int len2=str2.length();
    if(len1<len2)
    {
        for(int i=1;i<=len2-len1;i++)
           str1="0"+str1;
    }
    else
    {
        for(int i=1;i<=len1-len2;i++)
           str2="0"+str2;
    }
    len1=str1.length();
    int cf=0;
    int temp;
    for(int i=len1-1;i>=0;i--)
    {
        temp=str1[i]-'0'+str2[i]-'0'+cf;
        cf=temp/10;
        temp%=10;
        str=char(temp+'0')+str;
    }
    if(cf!=0)  str=char(cf+'0')+str;
    return str;
}
string fb(string a)
{
    if(a=="1") return "1";
    if(a=="2") return "2";
    else return add(fb(sub(a,"1")),fb(sub(a,"2")));
}
void fucksync()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int main()
{
    string a;
    fucksync();
    cin>>a;
    cout<<fb(a)<<"\n";
}

by BSJT_303 @ 2024-03-01 20:17:55

代码太长了,用这个

#include<iostream>
using namespace std;

int n;
int v[5010][5010];
int main() {
    v[1][1] = 1;
    v[2][1] = 2;
    cin >> n;
    for (int i = 3; i <= n; i++) {
        for (int j = 1; j <= 2000; j++) {
            v[i][j] += v[i - 1][j] + v[i - 2][j];
            v[i][j + 1] += (v[i][j] / 10);
            v[i][j] %= 10;
        }
    }

    int k = 2000;
    while (v[n][k] == 0 && k > 1) {
        k--;
    }
    for (int i = k; i >= 1; i--) {
        cout << v[n][i];
    }
    return 0;
}

可以用高精度储存


by Azusares @ 2024-03-03 13:57:52

@BSJT_303 orz


|