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. 9.26 H5日记

    9.26 1.新的背景属性,background-position background-position有两个值,水平和垂直,单位px ❤在html和CSS当中,有三个属性可以向服务器发送请求,分别 ...

  2. angular 1.x 控制器之间互相传递参数

    我们要向前方看齐,基于js引用类型的对象就不记了,所以使用基于事件的方式: angular 中 $on,$emit,$boardcast来实现父控制器和子控制器互相通讯, 其中$on表示事件监听, $ ...

  3. list集合与HashMap的使用

    List<Map<String, Object>> arrays= new ArrayList<Map<String, Object>>(); Hash ...

  4. centos7.2下nginx安装教程

    1.准备工作 1)关闭iptables 关闭操作 iptables -t nat -F 查看操作 iptables -t nat -L 2)关闭selinux 查看操作 setenforce 关闭操作 ...

  5. python消息队列Queue

    实例1:消息队列Queue,不要将文件命名为"queue.py",否则会报异常"ImportError: cannot import name 'Queue'" ...

  6. Spring PropertyResolver 占位符解析(二)源码分析

    Spring PropertyResolver 占位符解析(二)源码分析 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) ...

  7. Gazebo: Could not find parameter robot_description on parameter server

    robot_state_publisher looks for the parameter "robot_description" by default. The robot_st ...

  8. 【NIFI】 实现数据库到数据库之间数据同步

    本里需要基础知识:[NIFI] Apache NiFI 安装及简单的使用 数据同步 界面如下: 具体流程: 1.使用ExecuteSQL连接mysql数据库,通过写sql查询所需要的数据 2.nifi ...

  9. [网络]10M、100M、1000M网线的水晶头接法

    在网络维护过程中经常要自己制作网线,水晶头理论上是这样接的: 10M和100M和1000M以太网在使用网线时,对网线各自有不同的要求. 10M和100M在目前来说,连接网络的时候,只用到两对线来传输网 ...

  10. [C#]this.Invoke和this.BeginInvoke的区别

    private void button1_Click(object sender, EventArgs e) { "; this.Invoke(new EventHandler(delega ...