poj 3181 动态规划
来源:互联网 发布:东北青年网络大电影 编辑:程序博客网 时间:2024/05/20 04:09
Dollar Dayz
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4840 Accepted: 1842
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 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$1Write 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
Source
USACO 2006 January Silver
给定n和k,k代表有k个商品价格分别是1-k,每个商品的数量无限,n代表有n元钱,求把这n元钱花完总共有多少种方法。
开始发现这题跟以前做的题很像,很快就写出了推导式,但是初始化的时候却出现了问题。最开始的时候将从1-k的dp全部设置为1,最后发现重复计算了很多。
后来网上搜了一下才明白要把dp【0】设置为1.这样就不会重复计算。
中间运算会超过long long的表示范围,因此用数组模拟就行。
dp[i][0..50]表示第i个数有多少种组合方式。dp数组第二维表示组合方式的数量。
#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}int dp[1111][55];int v[111];void add(int n,int m){ int tmp=0; for(int i=0;i<50;i++) { dp[n][i]+=dp[m][i]+tmp; if(dp[n][i]>=10) { tmp=1; dp[n][i]%=10; } else tmp=0; }}int main(){ int n,k; while(~scanf("%d%d",&n,&k)) { mem(dp,0); dp[0][0]=1; for(int i=1;i<=k;i++) //从每一个商品开始看它能组成哪些钱数 { for(int j=i;j<=n;j++) { add(j,j-i); } } int i=50; while(i>0 && dp[n][i]==0)--i; while(i>=0) { printf("%d",dp[n][i]); --i; } puts(""); } return 0;}
0 0
- poj 3181 动态规划
- 动态规划 POJ 3181 Dollar Dayz
- POJ 3181 -- Dollar Dayz(动态规划)
- poj 2411 动态规划
- Poj 1157(动态规划)
- POJ 1651动态规划
- Poj 1221 动态规划
- POJ 3186 动态规划
- [poj]动态规划2479
- [poj]动态规划1015
- [poj] 动态规划 1141
- POJ 1088 动态规划
- poj 1636 动态规划
- poj 1159 动态规划
- POJ 动态规划 题集
- poj 1088 动态规划
- POJ 3267 动态规划
- poj 2609 动态规划
- 简单工厂模式-Simple Factory Pattern
- 文件自动上传到FtP(shell)
- Tomcat 性能监控及调优
- Javascript的this用法
- 产品测试管理的四个阶段
- poj 3181 动态规划
- __blick 和 __weak的区别
- myeclipse取消js的提示错误和一些常规操作
- Java注解解析(三)——解析自定义注解
- 8.3facebook分享后不回调结果原因,java标签代码
- g++编译线程失败
- Android IntentService完全解析 当Service遇到Handler
- 医疗电子平台选择:FPGA、ARM、X86、DSP还是GPU
- iOS UISlider用法及自定义滑块