Dollar Dayz POJ
来源:互联网 发布:ubuntu火狐设置中文 编辑:程序博客网 时间:2024/05/11 18:34
Dollar Dayz
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6839 Accepted: 2563
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
这些题全是套路。这一题就是一个简单的完全背包问题,但是发现范围很大,因此想到了第一种做法,一言不和就高精度。
#include <iostream>#include <cstdio>using namespace std;const int maxn = 1e3 + 100;string dp[maxn][maxn];string add(string s1,string s2)//s1=s1+s2{ int i,j,sum=0; int l1 = s1.length(); int l2 = s2.length(); if(l1<l2) { swap(s1,s2); swap(l1, l2); } for(i=l1-1,j=l2-1; i>=0; i--,j--) { sum+=s1[i]-'0'; if(j>=0) { sum+=s2[j]-'0'; } s1[i]=sum%10+'0'; sum/=10; } if(sum) { s1='1'+s1; } return s1;}int main(){ int n, k; cin >> n >> k; for(int i = 0; i <= k; ++i) { dp[i][0] = "1"; } for(int i = 1; i <= k; ++i) { for(int j = 0; j <= n; ++j) { if(j >= i) { dp[i][j] = add(dp[i - 1][j], dp[i][j - i]); } else { dp[i][j] = dp[i - 1][j]; } } } cout << dp[k][n] << endl; return 0;}
然而超时了。看了别人的思路发现,开两个数组一个存前14位,一个存后14位。
#include <iostream>#include <cstdio>using namespace std;const int maxn = 1e3 + 100;typedef unsigned long long ull;ull dp[maxn][maxn][2];const ull limit = 1e14;int main(){ int n, k; cin >> n >> k; for(int i = 0; i <= k; ++i) { dp[i][0][1] = 1; } for(int i = 1; i <= k; ++i) { for(int j = 0; j <= n; ++j) { if(j >= i) { dp[i][j][1] = dp[i - 1][j][1] + dp[i][j - i][1]; dp[i][j][0] = dp[i - 1][j][0] + dp[i][j - i][0]; dp[i][j][0] += dp[i][j][1] / limit; dp[i][j][1] = dp[i][j][1] % limit; } else { dp[i][j][1] = dp[i - 1][j][1]; dp[i][j][0] = dp[i - 1][j][0]; } } } if(dp[k][n][0]) { cout << dp[k][n][0]; } cout << dp[k][n][1] << endl; return 0;}
0 0
- poj 3181 Dollar Dayz
- POJ 3181 Dollar Dayz
- Poj 3181 Dollar Dayz
- POJ 3181 Dollar Dayz
- poj 3181 Dollar Dayz
- poj 3181 Dollar Dayz
- POJ 3181 Dollar Dayz
- poj 3181 Dollar Dayz
- poj 3181 Dollar Dayz
- POJ-3181-Dollar Dayz
- POJ 3181 Dollar Dayz
- poj 3181 Dollar Dayz
- poj 3181 Dollar Dayz
- POJ 3181 Dollar Dayz
- POJ 3181 Dollar Dayz
- POJ 3181 Dollar Dayz
- POJ 3181Dollar Dayz
- Dollar Dayz POJ
- caffe学习笔记33-关于1*1卷积核理解
- Linux命令——文件搜索命令
- 操作系统如何自启
- 概率有向图模型
- 练手/2048/IO/Swing/Java
- Dollar Dayz POJ
- html css入门书籍推荐
- 二叉树中的那些常见的面试题
- 进程与线程及进程间通信
- 怎么解决RN TextInput 被键盘遮挡的问题
- 每天进步一点点——五分钟理解一致性哈希算法(consistent hashing)
- C# 第十章 汽车租赁系统
- 2.javascript中的事件对象【学习笔记】
- 编码格式