dctc1494 @ 2023-11-05 15:18:14
哪位大佬帮忙看一下我的代码为什么会MLE!
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 5e3 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
LL n, len = 1, f[N][N];
void func(int x) {
for (LL i = 1; i <= len; ++i) {
f[x][i] = f[x - 1][i] + f[x - 2][i];
}
for (LL i = 1; i <= len; ++i) {
if (f[x][i] > 9) {
f[x][i + 1] += f[x][i] / 10;
f[x][i] %= 10;
}
}
if (f[x][len + 1]) {
++len;
}
}
int main(void) {
cin >> n;
f[1][1] = 1;
f[2][1] = 2;
for (LL i = 3; i <= n; ++i) {
func(i);
}
for (LL i = len; i; --i) {
cout << f[n][i];
}
cout << endl;
return 0;
}
by LFRED2023 @ 2023-11-05 16:15:36
#include<bits/stdc++.h>
using namespace std;
int a[5010][2000];
int main()
{
int n;
scanf("%d",&n);
a[1][1] = 1;
a[2][1] = 2;
for(int i = 3;i <= n;i++)
{
for(int i1 = 1;i1 <= 2000;i1++)
{
a[i][i1] += (a[i - 1][i1] + a[i - 2][i1]);
a[i][i1 + 1] += a[i][i1] / 10;
a[i][i1] %= 10;
}
}
int len = 2000;
while(a[n][len] == 0)
{
len--;
}
while(len)
{
printf("%d",a[n][len]);
len--;
}
return 0;
}
蒟蒻五分钟写的简陋代码前来帮助