poj3181 Dollar Dayz
Description
$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
Output
Sample Input
5 3
Sample Output
5
题意:给你两个数n。k,让你用1到k这k个数表示n,问有几种方法,本质是整数的拆分。由于最后结果比較大,超过long long ,所以用两个long long连接起来,设两个数组a[][],b[][]分别表示没有超过long long的部分以及超过long long 部分。
当中a[n][m]表示n用一些数拆分,当中最大的数不超过m的方案数,绘图能够看到。当n<m时,a[i][j]=a[i][i];当n>=m时。a[i][j]=(a[i][j-1]+a[i-j][j])%inf;初始化的时候要令a[0][i]=1,由于a[2][2]=a[0][2]+a[2][1];
6
5 + 1
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1
#include<stdio.h>
#include<string.h>
#define ll long long
ll a[1006][106],b[1006][106];
ll inf;
int main()
{
int n,m,i,j;
inf=1;
for(i=1;i<=18;i++){
inf*=10;
}
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=1;i<=m;i++){
a[0][i]=1;
}
for(i=1;i<=n;i++){
a[i][1]=1;
}
for(j=1;j<=m;j++){
a[1][j]=1;
} for(j=2;j<=m;j++){
for(i=2;i<=n;i++){
if(i<j){
a[i][j]=a[i][i];
b[i][j]=b[i][i];
}
else{
a[i][j]=(a[i][j-1]+a[i-j][j])%inf;
b[i][j]=b[i][j-1]+b[i-j][j]+(a[i][j-1]+a[i-j][j])/inf;
}
//printf("%d %d %d\n",i,j,a[i][j]);
}
}
if(b[n][m])
printf("%lld",b[n][m]);
printf("%lld\n",a[n][m]);
}
return 0;
}
这题也能够用全然背包,并用高精度模拟。状态转移方程:dp[j]+=dp[j-w[i]]
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define inf 0x7fffffff
#define ll long long
int dp[1005][60];
void add(int a,int b){
int i,j;
for(i=1;i<=50;i++){
if(dp[a][i]+dp[b][i]<=9){
dp[a][i]=dp[a][i]+dp[b][i];
}
else{
dp[a][i]=(dp[a][i]+dp[b][i])%10;
dp[a][i+1]++;
}
}
} int main()
{
int n,m,i,j,k,t;
while(scanf("%d%d",&m,&k)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0][1]=1;
for(i=1;i<=k;i++){
for(j=i;j<=m;j++){
add(j,j-i);
}
}
t=50;
while(t>=2 && dp[m][t]==0)t--; for(i=t;i>=1;i--){
printf("%d",dp[m][i]);
}
printf("\n");
}
return 0;
}
poj3181 Dollar Dayz的更多相关文章
- poj3181 Dollar Dayz ——完全背包
link:http://poj.org/problem?id=3181 本来很常规的一道完全背包,比较有意思的一点是,结果会超int,更有意思的解决方法是,不用高精度,用两个整型的拼接起来就行了.OR ...
- Dollar Dayz(大数母函数,高低位存取)
Dollar Dayz Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5655 Accepted: 2125 Descr ...
- POJ 3181 Dollar Dayz(全然背包+简单高精度加法)
POJ 3181 Dollar Dayz(全然背包+简单高精度加法) id=3181">http://poj.org/problem?id=3181 题意: 给你K种硬币,每种硬币各自 ...
- poj 3181 Dollar Dayz(完全背包)
Dollar Dayz Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5419 Accepted: 2054 Descr ...
- Dollar Dayz poj3181
http://poj.org/problem?id=3181 这个题目一开始就能看出来是个dp问题,但是我并没有一开始就看出来是一个完全背包为题,只是想着根据以前的方法,这个问题应该是可以找到规律的, ...
- poj3181【Dollar Dayz】
做完这道题,心里五味陈杂,明明是最水的一道题,我却做了最长的时间. 题意是求用1-k的和表示n的方案数. 显然是个计数dp,但我不会.思考半小时未果. 然后找尹鹏哲,他给我讲了个错的dp方程,结果调试 ...
- (完全背包 大数)Dollar Dayz (POJ 3181)
http://poj.org/problem?id=3181 Description Farmer John goes to Dollar Days at The Cow Store and disc ...
- bzoj1655 [Usaco2006 Jan] Dollar Dayz 奶牛商店
Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...
- 【BZOJ】1655: [Usaco2006 Jan] Dollar Dayz 奶牛商店(背包+高精度)
http://www.lydsy.com/JudgeOnline/problem.php?id=1655 背包就没什么好说的了,裸的完全背包.. 但是我一开始交开了ull都wa了T_T.. 精度太大. ...
随机推荐
- 【Luogu】P3311数数(AC自动机上DP)
题目链接 蒟蒻今天终于学会了AC自动机,感觉很稳 (后一句愚人节快乐) 这题开一个f[i][j][k]表示有没有受到限制,正在枚举第j位,来到了AC自动机的第k个节点 的方案数 随后可以刷表更新 注意 ...
- HDU——1073Online Judge(string类以及其对应函数)
Online Judge Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- docker (centOS 7) 使用笔记4 - etcd服务
本次测试的系统包含centos 7.2 64 bit,centos 7.3 64 bit 1. 安装 yum -y install etcd 2. 配置 此处一共准备了3台机器(10.10.10.10 ...
- Jdbc执行存储过程报数据库事务无法执行的异常
Jdbc执行存储过程报数据库事务无法执行的异常 环境: Eclipse+Jdk1.7+spring-jdbc-3.0.7+同版本的jdbctemplate+Sqlserver 2012 问题: 一个小 ...
- gulpfile.js备份
var gulp = require('gulp'); var uglify = require('gulp-uglify'); // var rename = require('gulp-renam ...
- hdu 4301 dp
Divide Chocolate Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 批处理命令之Start的详细用法
Start启动单独的“命令提示符”窗口来运行指定程序或命令.如果在没有参数的情况下使用,start 将打开第二个命令提示符窗口. 语法start ["title"] [/dPath ...
- 【開發時,應注意事項】 vendor tools 無法 work 時,怎麼辦?
遇到 vendor tools 無法 work 時, 最好的方法直接請 vendor 來, 為什麼呢? 因為 tool 可能 有版本的問題, 譬如: vendor tool A tool 在 buil ...
- LA 2995 Image Is Everything
题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...
- 采集网站特殊文件Meta信息
采集网站特殊文件Meta信息 元(Meta)信息是描述文件的属性的特殊信息,如文件的所有者.联系方式.机构名.邮件地址等信息.而网站中常常会有共享的文档文件,如PDF.Excel.Word.这些文 ...