Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:
1 @ US$3 + 1 @ US$2 1 @ US$3 + 2 @ US$1 1 @ US$2 + 3 @ US$1 2 @ US$2 + 1 @ US$1 5 @ US$1
Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100).Input
A single line with two space-separated integers: N and K.
Output
A single line with a single integer that is the number of unique ways FJ can spend his money.
Sample Input
5 3
Sample Output
5 详解:http://blog.csdn.net/libin56842/article/details/9455979有n个无区别的物品,将它们划分成不超过m组,这样的划分被称作n的m划分,特别地,m=n时称作n的划分数。用dp[i][j]表示j的i划分的总数,dp[i][j]=dp[i][j-i]+dp[i-1][j],复杂度O(nm) ----------------------------摘自《挑战程序设计竞赛》
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 typedef long long ll; 7 8 int n,k; 9 ll a[1100],b[1100],INF; 10 11 void Init(){ 12 memset(a,0,sizeof(a)); 13 memset(b,0,sizeof(b)); 14 INF=1; 15 for(int i=0;i<18;i++) INF=INF*10; 16 } 17 18 void Solve(){ 19 a[0]=1; 20 for(int i=1;i<=k;i++){ 21 for(int j=1;j<=n;j++){ 22 if(j-i<0) continue; 23 b[j]=b[j]+b[j-i]+(a[j]+a[j-i])/INF; 24 a[j]=(a[j]+a[j-i])%INF; 25 } 26 } 27 28 if(b[n]) printf("%lld",b[n]); 29 printf("%lld\n",a[n]); 30 } 31 32 int main() 33 { cin>>n>>k; 34 Init(); 35 Solve(); 36 return 0; 37 }