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

完全背包+高精度数

没有优化直接wa,是数值范围太小
#include<iostream>
using namespace std;
int dp[][];
int main(){
int n,k;
cin>>n>>k;
dp[][]=;
for(int i=;i<=k;i++){
for(int j=;j<=n;j++){
for(int k=;k*i<=j;k++){
dp[i][j]+=dp[i-][j-k*i];
}
}
}
cout<<dp[k][n];
return ;
}

改用unsigned long long还是wa。那就用两个unsigned long long一个存低位一个存高位。unsigned long long的范围是1844674407370955161,所以用一个比它小一个数量级的数,

100000000000000000。
#include<iostream>
using namespace std;
unsigned long long dp[][][];
#define LIMIT_ULL 100000000000000000
int main(){
int n,k;
cin>>n>>k;
dp[][][]=;//低位
for(int i=;i<=k;i++){
for(int j=;j<=n;j++){
for(int k=;k*i<=j;k++){
dp[i][j][]+=dp[i-][j-k*i][];
dp[i][j][]+=dp[i-][j-k*i][];
dp[i][j][]+=dp[i][j][]/LIMIT_ULL;//低位的进位加到高位
dp[i][j][]=dp[i][j][]%LIMIT_ULL;//低位去除进位
}
}
}
if(dp[k][n][]){
cout<<dp[k][n][];
}
cout<<dp[k][n][];
return ;
}

以上比你不是最优的,可能会TLE

dp[i][j]+=dp[i-1][j-i*k]可以优化成dp[i][j]=dp[i-1][j]+dp[i-1][j-k]

因为当k>1时的计算结果,已经保存在了dp[i-1][j-k]

#include<iostream>
using namespace std;
unsigned long long dp[][][];
#define LIMIT_ULL 100000000000000000
int main(){
int n,k;
cin>>n>>k;
dp[][][]=;
for(int i=;i<=k;i++){
for(int j=;j<=n;j++){
if(j<i){
dp[i][j][]=dp[i-][j][];
dp[i][j][]=dp[i-][j][];
}
else{
dp[i][j][]=dp[i-][j][]+dp[i][j-i][];
dp[i][j][]=dp[i-][j][]+dp[i][j-i][];
dp[i][j][]+=dp[i][j][]/LIMIT_ULL;
dp[i][j][]=dp[i][j][]%LIMIT_ULL;
}
}
}
if(dp[k][n][]){
cout<<dp[k][n][];
}
cout<<dp[k][n][];
return ;
}

POJ3181--Dollar Dayz(动态规划)的更多相关文章

  1. poj3181 Dollar Dayz

    Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...

  2. poj3181 Dollar Dayz ——完全背包

    link:http://poj.org/problem?id=3181 本来很常规的一道完全背包,比较有意思的一点是,结果会超int,更有意思的解决方法是,不用高精度,用两个整型的拼接起来就行了.OR ...

  3. Dollar Dayz(大数母函数,高低位存取)

    Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5655   Accepted: 2125 Descr ...

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

    POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...

  5. poj 3181 Dollar Dayz(完全背包)

    Dollar Dayz Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5419   Accepted: 2054 Descr ...

  6. POJ 3181 Dollar Dayz(高精度 动态规划)

    题目链接:http://poj.org/problem?id=3181 题目大意:用1,2...K元的硬币,凑成N元的方案数. Sample Input 5 3 Sample Output 5 分析: ...

  7. Dollar Dayz poj3181

    http://poj.org/problem?id=3181 这个题目一开始就能看出来是个dp问题,但是我并没有一开始就看出来是一个完全背包为题,只是想着根据以前的方法,这个问题应该是可以找到规律的, ...

  8. poj3181【Dollar Dayz】

    做完这道题,心里五味陈杂,明明是最水的一道题,我却做了最长的时间. 题意是求用1-k的和表示n的方案数. 显然是个计数dp,但我不会.思考半小时未果. 然后找尹鹏哲,他给我讲了个错的dp方程,结果调试 ...

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

    http://poj.org/problem?id=3181 Description Farmer John goes to Dollar Days at The Cow Store and disc ...

  10. poj 3181 Dollar Dayz (整数划分问题---递归+DP)

    题目:http://poj.org/problem?id=3181 思路:将整数N划分为一系列正整数之和,最大不超过K.称为整数N的K划分. 递归:直接看代码: 动态规划:dp[i][j]:=将整数i ...

随机推荐

  1. Canvas绘图 (html5新增特性)

    Canvas 使用<canvas>对象,需要设置属性:width,height.指定绘图的区域大小.在canvas标签前后出现的信息将在不支持<canvas>元素的浏览器中显示 ...

  2. 事件类型(js)

    焦点事件:在页面获得或失去焦点时触发. 与document.hasFocus和document.activeElement属性配合,可以得到用户在页面的行踪. blur:元素失去焦点时触发.这个事件不 ...

  3. 探索未知种族之osg类生物---呼吸分解之事件循环三

    那我们就开始处理这些事件中得到的所有的交互事件,首先我们要判断这些事件是否包含osg的退出事件,那什么情况下会触发这个退出事件呢?如果您运行过osg中example中的小例子的,聪明的你一定就会发现当 ...

  4. C#泛型的学习

    编码: class Program { static void Main(string[] args) { ; Test<int> test1 = new Test<int>( ...

  5. 第五周Java学习总结(补)

    第五周java学习内容(补) 学习内容: File类方法的操作 public String getName() public boolean canRead() public boolean canW ...

  6. [AI]神经网络章3 损失函数

    损失函数 作用 在有监督的学习中,需要衡量神经网络输出和所预期的输出之间的差异大小.这种误差函数需要能够反映出当前网络输出和实际结果之间一种量化之后的不一致程度,也就是说函数值越大,反映出模型预测的结 ...

  7. UX设计秘诀之注册表单设计,细节决定成败

    以下内容由摹客团队翻译整理,仅供学习交流,摹客iDoc是支持智能标注和切图的产品协作设计神器. 说实话,现实生活中,又有多少人会真正喜欢填写表格?显然,并不多.因为填写表单这样的网页或App服务,并非 ...

  8. mysql 压缩版安装

    环境介绍:win2008_x64+mysql5.7.10  64位 1.将压缩包解压到d:\\mysql目录,并将mysql目录中的my-default.ini 重命名为my.ini 2.将my.in ...

  9. OneZero第三周第一次站立会议(2016.4.4)

    1. 时间: 13:30--13:45  共计15分钟. 2. 成员: X 夏一鸣 * 组长 (博客:http://www.cnblogs.com/xiaym896/), G 郭又铭 (博客:http ...

  10. 别人的Linux私房菜(1)计算机概论

    计算机主板 早期两个网桥控制通信,北桥连接速度比较快的CPU.内存.显卡.南桥连接较慢的接口,如硬盘,USB,网卡等.北桥的控制器集成到了CPU中. CPU工作频率 外频:CPU与外部组件进行数据传输 ...