[Luogu] P1255 数楼梯
Luogu P1255 数楼梯
递推公式:\(F[i] = F[i - 1] + F[i - 2]\),\(2 \leqslant i \leqslant n\)。
初始化\(F[0] = F[1] = 1\)即可。
数据太大,记得高精
#include <cstdio>
const int MAXN = 5001;
int n;
int f[MAXN][1100];
inline void print(int *x) {
for (int i = x[0]; i >= 1; --i)
printf("%d", x[i]);
printf("\n");
return ;
}
inline void sum(int *a, int *b, int *c) {
int len = b[0] > c[0] ? b[0] : c[0];
for (int i = 1; i <= len; ++i) {
a[i] = a[i] + b[i] + c[i];
if(a[i] >= 10) a[i + 1] = a[i] / 10, a[i] = a[i] % 10;
}
a[0] = a[len + 1] ? len + 1 : len;
return ;
}
int main() {
scanf("%d", &n);
if(n == 0) {printf("0\n"); return 0;}
f[0][1] = f[0][0] = f[1][1] = f[1][0] = 1;
for (int i = 2; i <= n; ++i)
sum(f[i], f[i - 1], f[i - 2]);
print(f[n]);
return 0;
}