Dollar Dayz POJ - 3181

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 }
时间: 2024-10-27 13:21:10

Dollar Dayz POJ - 3181的相关文章

DP:Dollar Dayz(POJ 3181)

一道高精度DP 题目大意,换工具,有m块钱,有k种价值的物品,(1...k),求一共有多少种换法 这一题就是完全背包,现在这种完全背包对我来说就是水题了, 状态转移方程闭着眼睛写dp[j]+=dp[j-i] 可是这一题还没完,数据量太大,会出现溢出的情况,这一题有一点高精度的要求,要求也挺简单的,两个long long就可以了 状态转移方程变为: dpl[j]+=dpl[j-i]; dph[j]+=dph[j-i]+f(j)  当dpl[j]>Up时,f(j)=1,且dpl[j]=dp[j]-U

POJ 3181 Dollar Dayz(完全背包+简单高精度加法)

POJ 3181 Dollar Dayz(完全背包+简单高精度加法) http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币分别是1美元,2美元-K美元且可以无限使用,问你用上面K种硬币构成n美元的话有多少种方法? 分析: 本题是一道明显的完全背包问题, 不过本题还可以换一种方法来看: 整数n由前K个自然数构造, 一共有多少种方法? (虽然本题要用到高精度加法, 但是很简单, 不要被吓到哦) 首先是DP部分: 令dp[i][j]==x 表示由前i种硬币构成j

POJ 3181 Dollar Dayz &amp;&amp; Uva 147 Dollars(完全背包)

首先是 Uva 147:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=83 细心看完这题后发现还是完全背包,只不过需要对浮点数处理一下.即把所有硬币的面值都乘以100,化为整数,对输入的数据也作同样的处理,然后就是套完全背包的模板了,在输出时还要用格式和精度来卡一卡你……一开始我没想到用printf可以的,于是百度了cout的输出格式控制,

poj3181 Dollar Dayz (DP+大数)

Dollar Dayz Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3181 Appoint description: System Crawler (2016-05-27) Description Farmer John goes to Dollar Da

Dollar Dayz (大数dp fuck!不是多组数据!!)

Dollar Dayz Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on s

POJ3181 Dollar Dayz 【母函数】+【高精度】

Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4204   Accepted: 1635 Description 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 sell

(完全背包 大数)Dollar Dayz (POJ 3181)

http://poj.org/problem?id=3181 Description 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

POJ 3181 Dollar Dayz 【完全背包】

题意: 给出两个数,n,m,问m以内的整数有多少种组成n的方法完全背包+大数划分 思路: dp[i][j] := 用i种价格配出金额j的方案数. 那么dp[i][0] = 1,使用任何价格配出金额0的方案个数都是1(什么都不用). 递推关系式: 实际上是完全背包问题,只是状态转移方程形式有所不同,不过状态转移的方向是完全相同的. dp[i][j] = dp[i – 1][j] + dp[i – 1][j – i] + dp[i – 1][j – 2 * i] + - + dp[i – 1][0]

POJ 3181 Dollar Dayz

其实这道题就是简单的完全背包问题.从低到高推断出每种硬币的所有面额的解即可.当然,离线的话会慢一点.可以将问题需要求出的子问题全部求出来.下一个问题出现的时候,如果之前已经求解过则不必求解,否则在之前的基础上继续求解.原本觉得没什么好写的,关键是同样的方法用STL中的vector代替数组会超时!搞得以后都不敢用STL了..... #include <iostream> #include <cstdio> #include <algorithm> #include <