思路:高精+动态规划+滚动数组
高精不必再提,f[i]表示第i层楼梯走法,那么状态转移方程为f[i]=f[i-1]+f[i-2],也就是斐波那契数列。当然,不同的是f[n]=n(n<=2)
代码:
#include<iostream>
using namespace std;
const int MAXN = 2001;
int last[MAXN] = { 1,1 }, now[MAXN] = { 1,2 };
int temp[MAXN] = { 1,1 };
int len = 1;
int N;
int main()
{
cin >> N;
if (N <= 2) {
cout << N;
return 0;
}
for (int i = 3; i <= N; i++) {
int k = 1;
for (int i = 1; i <= len; i++)
temp[i] = now[i];
while (k <= len) {
now[k] += last[k];
if (now[k] >= 10) {
now[k] %= 10;
now[k + 1] += 1;
}
k++;
}
for (int i = 1; i <= len; i++)
last[i] = temp[i];
if (now[len + 1] > 0)
len++;
}
for (int i = len; i >= 1; i--)
cout << now[i];
return 0;
}