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. [转]C#中HttpClient使用注意:预热与长连接

    最近在测试一个第三方API,准备集成在我们的网站应用中.API的调用使用的是.NET中的HttpClient,由于这个API会在关键业务中用到,对调用API的整体响应速度有严格要求,所以对HttpCl ...

  2. SqlServer添加触发器不让删除数据

    触发器是:instead of delete 类型,注意了:instead类型的触发器相当于: DELETE命令过来后,直接走触发器中的代码,再往下,没有了…… 就是说,这个触发器会屏蔽掉你所有的DE ...

  3. 基于spring boot的统一异常处理

    一.springboot的默认异常处理 Spring Boot提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异常内容. 例如这里我 ...

  4. PAT 1011 A+B和C (15)(C++&JAVA&Python)

    1011 A+B和C (15)(15 分) 给定区间[-2^31^, 2^31^]内的3个整数A.B和C,请判断A+B是否大于C. 输入格式: 输入第1行给出正整数T(<=10),是测试用例的个 ...

  5. linux工具介绍

    http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/index.html 工具参考篇 1. gdb 调试利器 2. ldd 查看程序依赖库 3 ...

  6. 使用Loadrunner对IBM MQ进行性能测试

    一.概述         使用Loadrunner对IBM MQ进行性能测试,需要用到java vuser以及java编码知识.此次先介绍什么是IBM MQ,然后java vuser的使用与配置细节, ...

  7. com_pc-mcu

    #include <REG52.H> unsigned char UART_buff; bit New_rec = , Send_ed = , Money = ; void main (v ...

  8. maven web+spring mvc项目没有出现src/main/java路径

    直接在main 文件夹下创建java可以解决 https://www.cnblogs.com/zhujiabin/p/6343462.html

  9. 事务ACID如何定义,事务隔离性解决的问题

    挚享科技 2018.4.8 事务的四个特性: 1. 原子性: 同一个事务的多个操作,要么都成功,要么全部失败回滚. 2. 一致性: 事务必须确保数据库从一个一致性状态变换为另一个一致性状态. 其实就是 ...

  10. windows mysql 主从热备

    环境说明:   Master:192.168.1.200 Slave:192.168.1.210     MySQL 的 Master 配置:   配置my.ini:   [mysqld]   # T ...