题意:把K个不超过N的非负整数加起来,使得它们的和为N,有多少种方法
思路:令dp[i][j]为和为i用了j个数的方案数,那么转移就是dp[i][j]=dp[i-1][j]+dp[i][j-1]
#include<bits/stdc++.h> using namespace std; int dp[101][101]; const int mod = 1e6; int main() { for (int i = 1;i<=100;i++) { dp[i][1]=1; dp[1][i]=i; } for (int i = 2;i<=100;i++) for (int j = 2;j<=100;j++) dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod; int n,k; while (scanf("%d%d",&n,&k)!=EOF && n+k) { printf("%d\n",dp[n][k]); } }