Dollar Dayz poj3181
http://poj.org/problem?id=3181
这个题目一开始就能看出来是个dp问题,但是我并没有一开始就看出来是一个完全背包为题,只是想着根据以前的方法,这个问题应该是可以找到规律的,但是,还是被坑了,这还是一个大数问题!
首先我膜拜一下hankcs大神的:
///////////////////////////////////////////////////////////
- #include <iostream>
- using namespace std;
- unsigned long long dp[100 + 16][1000 + 16]; // dp[i][j] := 用i种价格配出金额j的方案数
- ///////////////////////////SubMain//////////////////////////////////
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt", "r", stdin);
- freopen("out.txt", "w", stdout);
- #endif
- int N, K;
- cin >> N >> K;
- dp[0][0] = 1;
- for (int i = 1; i <= K; ++i)
- {
- for (int k = 0; k <= N; k += i)
- {
- for (int j = N; j >= k; --j)
- {
- dp[i][j] += dp[i - 1][j - k];
- }
- }
- }
- cout << dp[K][N] << endl;
- #ifndef ONLINE_JUDGE
- fclose(stdin);
- fclose(stdout);
- system("out.txt");
- #endif
- return 0;
- }
- ////////////////////////////////////////////////////////////
hancks的这个做法是用完全背包
dp[i][j] = dp[i – 1][j] + dp[i – 1][j – i] + dp[i – 1][j – 2 * i] + … + dp[i – 1][0]
由这个公式可以再递推:
将j换成j – i有
dp[i][j – i] = dp[i – 1][j – i] + dp[i – 1][j – 2 * i] + … + dp[i – 1][0]
得出:if j >= i:
dp[i][j] = dp[i-1][j] + dp[i][j-i];
我的做法是一开始就推出了这个公式,因为不小心看出了这个规律
i:1->4 ,j :1->5 dp[i][j]规律是这样的
1 1 1 1 1
1 2 2 3 3
1 2 3 4 5
1 2 3 4 6
得出了j >= i : dp[i][j] = dp[i-1][j] + dp[i][j-i]
然而,这还是个大数问题,即使unsigned long long 都不行,开始一直没想通!
/*************************************************************************
> File Name: DollarDayz_poj3181.cpp
> Author: spzhao
> Mail: spzhaol@163.com
> Created Time: 2015年10月14日 星期三 11时13分22秒
************************************************************************/ #include<iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> #define mod 10000000000000000
using namespace std; const int N = 1005;
const int K = 105;
unsigned long long dp[100+16][1000+16][2];
int n,k;
void solve()
{
for (int i = 1;i <= k;i++)
{
for (int j = 1;j <= n;j++)
{
if (j >= i)
{
dp[i][j][0] = dp[i-1][j][0]+dp[i][j-i][0];
dp[i][j][1] = dp[i-1][j][1]+dp[i][j-i][1];
dp[i][j][0] += dp[i][j][1]/mod;
dp[i][j][1] = dp[i][j][1]%mod;
}
else
{
dp[i][j][0] = dp[i-1][j][0];
dp[i][j][1] = dp[i-1][j][1];
}
}
}
if (dp[k][n][0])
cout << dp[k][n][0];
cout << dp[k][n][1] << endl;
}
int main ()
{
cin >> n >> k;
memset(dp,0,sizeof(dp));
dp[1][0][1] = 1;
for (int i = 1;i <= k;i++)
dp[i][0][1] = 1;
solve();
return 0;
}
Dollar Dayz poj3181的更多相关文章
- 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 ...
- poj3181 Dollar Dayz
Description Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of to ...
- poj3181 Dollar Dayz ——完全背包
link:http://poj.org/problem?id=3181 本来很常规的一道完全背包,比较有意思的一点是,结果会超int,更有意思的解决方法是,不用高精度,用两个整型的拼接起来就行了.OR ...
- 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.. 精度太大. ...
随机推荐
- MyBatis 一对多,多对一关联查询的时候Mapper的顺序
要先写association,然后写collection:这是由DTD决定的: <resultMap ...> <association ...> </associati ...
- SpringMvc项目中使用GoogleKaptcha 生成验证码
前言:google captcha 是google生成验证码的一个工具类,其原理是将随机生成字符串保存到session中,同时以图片的形式返回给页面,之后前台页面提交到后台进行对比. 1.jar包准备 ...
- 性能优化实战案例——助力某移动OA系统
前言 最近连续接触了4个OA系统,均存在着不同的性能问题,本文记述对某移动OA系统的优化全过程,让看官们对数据库优化流程有一个了解,并揭开隐式转换这无情杀手的神秘面纱. 本文使用的工具:SQL专家云平 ...
- JavaScript中this的指向问题
this是面向对象语言中一个重要的关键字,理解并掌握该关键字的使用对于我们代码的健壮性及优美性至关重要.而javascript的this又有区别于Java.C#等纯面向对象的语言,这使得this更加扑 ...
- Unable to find setter method for attribute: 属性名
tld文件里面的名称和标签内的属性名要一致.
- HDU4474
Yet Another Multiple Problem Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 用Mockito测试SpringMVC+Hibernate
用Mockito测试SpringMVC+Hibernate 译自:Spring 4 MVC+Hibernate 4+MySQL+Maven integration + Testing example ...
- Android在程序代码中使用String资源
this.getResources().getString(R.string.name)
- cvs上传复制项目
现在想重用,特别是重用框架. cvs上传新项目:右键—>team—>share project,根据向导,可选在使用项目名为module名. cvs删除项目:直接在cvs服务器目录上删除项 ...
- DllRegisterServer的调用失败的问题解决方法
1'按键盘上的win+x键调出常用命令. 2'选择“命令提示符(管理员)“ 3'在”命令提示符“中输入”regsvr32 c:\Windows\SysWOW64\comdlg32.ocx“或其他ocx ...