\(\qquad\!\!\)例题:
\(\qquad\!\!\)共有 \(n\) 级台阶,每次可以上 \(1-k\) 级,上完的方案数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include<bits/stdc++.h> #define in inline #define re register const int N=N; using namespace std; int n,k; int f[N]; in int qread() { int x=0,y=1; int ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-') { y=-1; } ch=getchar(); } while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*y; } in void qwrite(re int x) { if(x<0) { putchar('-'); qwrite(-x); } else { if(x>9) { qwrite(x/10); } putchar(x%10+'0'); } return ; } int main() { n=qread(); k=qread(); f[0]=1; for(re int i=1;i<=n;i++) { for(re int j=max(0,i-k);j<i;j++) { f[i]+=f[j]; } } qwrite(f[n]); putchar('\n'); return 0; }
|