poj 3181 Dollar Dayz(完全背包)
来源:互联网 发布:铁路12306软件下载 编辑:程序博客网 时间:2024/04/29 01:12
题目链接
Dollar Dayz
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6082 Accepted: 2297
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 @ 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
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
题目大意:
输入n,和k,问将n用1到k这k个数字进行拆分,有多少种拆分方法。例如:
n=5,k=3 则有n=3+2,n=3+1+1,n=2+1+1+1,n=2+2+1,n=1+1+1+1+1这5种拆分方法
解题思路:
这个题目是个比较明显的动态规划,而且是背包问题,可以写出状态转移方程如下:
用a[i][j]表示考虑到用数j进行拼接时数字i的拼接方法,可以得到状态转移方程如下:
当i小于j时a[i][j]=a[i][j-1]
当i大于等于j时a[i][j]=a[i][j-1]+a[i-j][j]
由于结果比较大,要敲大数模版。
代码如下:
#include<iostream>#include<cstdio>#include<algorithm>#include<vector>#include<cstring>using namespace std;const int inf=0x3f3f3f3f;const int maxn = 100;struct bign{ int len, s[maxn]; bign() { memset(s, 0, sizeof(s)); len = 1; } bign(int num) { *this = num; } bign(const char* num) { *this = num; } bign operator = (int num) { char s[maxn]; sprintf(s, "%d", num); *this = s; return *this; } bign operator = (const char* num) { len = strlen(num); for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0'; return *this; } string str() const { string res = ""; for(int i = 0; i < len; i++) res = (char)(s[i] + '0') + res; if(res == "") res = "0"; return res; } bign operator + (const bign& b) const{ bign c; c.len = 0; for(int i = 0, g = 0; g || i < max(len, b.len); i++) { int x = g; if(i < len) x += s[i]; if(i < b.len) x += b.s[i]; c.s[c.len++] = x % 10; g = x / 10; } return c; } void clean() { while(len > 1 && !s[len-1]) len--; } bign operator * (const bign& b) { bign c; c.len = len + b.len; for(int i = 0; i < len; i++) for(int j = 0; j < b.len; j++) c.s[i+j] += s[i] * b.s[j]; for(int i = 0; i < c.len-1; i++){ c.s[i+1] += c.s[i] / 10; c.s[i] %= 10; } c.clean(); return c; } bign operator - (const bign& b) { bign c; c.len = 0; for(int i = 0, g = 0; i < len; i++) { int x = s[i] - g; if(i < b.len) x -= b.s[i]; if(x >= 0) g = 0; else { g = 1; x += 10; } c.s[c.len++] = x; } c.clean(); return c; } bool operator < (const bign& b) const{ if(len != b.len) return len < b.len; for(int i = len-1; i >= 0; i--) if(s[i] != b.s[i]) return s[i] < b.s[i]; return false; } bool operator > (const bign& b) const{ return b < *this; } bool operator <= (const bign& b) { return !(b > *this); } bool operator == (const bign& b) { return !(b < *this) && !(*this < b); } bign operator += (const bign& b) { *this = *this + b; return *this; }}d[1005][105];istream& operator >> (istream &in, bign& x) { string s; in >> s; x = s.c_str(); return in;}ostream& operator << (ostream &out, const bign& x) { out << x.str(); return out;} int main(){ int n,k; scanf("%d%d",&n,&k); for(int i=1;i<=k;i++) d[0][i]=1; for(int i=1;i<=n;i++) { for(int j=1;j<=k;j++) { if(i<j) d[i][j]=d[i][j-1]; else if(i>=j) d[i][j]=d[i][j-1]+d[i-j][j]; } } cout << d[n][k] <<endl;}
- POJ 3181 Dollar Dayz (完全背包)
- poj 3181 Dollar Dayz 高精度+完全背包
- poj 3181 Dollar Dayz dp 完全背包
- POJ 3181 Dollar Dayz(高精度完全背包)
- poj 3181 Dollar Dayz(完全背包)
- POJ 3181 Dollar Dayz 完全背包 + 高精度
- POJ 3181 Dollar Dayz(完全背包+大数)
- poj 3181 Dollar Dayz(完全背包)
- poj Dollar Dayz(完全背包)
- poj 3181 Dollar Dayz (完全背包+高精度)
- POJ 3181 Dollar Dayz 01完全背包问题
- POJ 3181 - Dollar Dayz(完全背包+高精度)
- POJ 3181 Dollar Dayz(完全背包+简单高精度加法)
- POJ 题目3181 Dollar Dayz(完全背包,技巧)
- poj Dollar Dayz 3181 (DP&完全背包) 好题
- Dollar Dayz POJ - 3181 dp,完全背包,简单高精度加法
- POJ--3181--Dollar Dayz--背包/高精度
- POJ - 3181 Dollar Dayz(多重背包)
- 8.4 写一函数,使给定的一个二维数组(3×3)转置,即行列互换。
- 实现单元格自适应文本大小,UITableview的行高自适应单元格高度
- 20. Spring Boot Servlet【从零开始学Spring Boot】
- 安卓学习-WebView
- POJ 1218 THE DRUNK JAILER
- poj 3181 Dollar Dayz(完全背包)
- 我们并不是搬代码的工具
- 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】
- include动作标签和iframe包含的页面数据传入的问题
- HTTP协议_笔记
- EasyDarwin开源流媒体云平台VS调试断点提示“还没有为该文档加载任何符号”的解决办法
- nodejs与mysql的结合
- 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
- C# 多线程之--任务并行库(1)