码迷,mamicode.com
首页 > 其他好文 > 详细

Dollar Dayz POJ - 3181

时间:2017-08-30 17:12:49      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:john   class   inpu   设计   amp   blog   复杂度   oid   color   

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 }

 

Dollar Dayz POJ - 3181

标签:john   class   inpu   设计   amp   blog   复杂度   oid   color   

原文地址:http://www.cnblogs.com/zgglj-com/p/7453985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!