链接:https://www.luogu.org/problemnew/show/P1192
思路:运用dp的思想,a[i]表示走到第i级台阶的方法数,状态方程:a[i]=Σkj=1a(i-j),注意边界i==0和i==1时,方法数为1
代码:
1 #include<bits/stdc++.h> 2 #define inf 0x3f3f3f3f 3 typedef long long ll; 4 const int M = int(1e5)*2 + 5; 5 using namespace std; 6 7 int a[M],mod= 100003; 8 int n, k; 9 10 int dp(int n) 11 { 12 if (n == 0 || n == 1) return 1; 13 if (a[n]) return a[n]; 14 int ans = 0; 15 for (int i = 1; i <= k && n-i>=0; i++) 16 { 17 ans = (ans + dp(n - i)) % mod; 18 } 19 a[n] = ans; 20 return ans; 21 } 22 23 int main() 24 { 25 26 cin >> n >> k; 27 a[0]=a[1]=1; 28 /*for (int i = 1; i <= n; i++) 29 { 30 for (int j = 1; j <= k && i - j >= 0; j++) 31 a[i] += a[i - j]; 32 a[i] %= mod; 33 } 34 cout << a[n] << endl;*/ 35 cout << dp(n) << endl; 36 return 0; 37 }
原文:https://www.cnblogs.com/harutomimori/p/10492176.html