uva 10306 - e-Coins(完全背包)
题目大意:给出m和s, 再给出m种电子硬币,每种硬币有两种金额xi,yi。现在要在m种硬币种选若干个硬币,可以重复选同一种硬币, 使得(x1 + x2 + .... + xn) ^ 2 + (y1 + y2 + ... + yn) ^ 2 == s * s, 要求n尽量小,(n为选取硬币的个数), 如果不能选出满足条件的硬币,输出-1。
解题思路:二维的完全背包问题,注意要用long long。
#include <stdio.h>
#include <string.h>
const int N = 305; struct coin{
int x;
int y;
}val[50];
int n, s, S, dp[N][N]; void read() {
memset(val, 0, sizeof(val));
scanf("%d%d", &n, &s);
S = s * s;
for (int i = 0; i < n; i++)
scanf("%d%d", &val[i].x, &val[i].y);
} int solve() {
int cnt = 1 << 30;
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for (int k = 0; k < n; k++) {
for (int i = 0; i <= s; i++) {
for (int j = 0; j <= s; j++) {
if (dp[i][j]) {
int p = i + val[k].x, q = j + val[k].y;
if (p > s || q > s) break;
if (dp[p][q] == 0 || dp[i][j] + 1 < dp[p][q])
dp[p][q] = dp[i][j] + 1;
if (p * p + q * q == S && dp[p][q] < cnt)
cnt = dp[p][q];
}
}
}
}
if (cnt == 1 << 30) return 0;
else return cnt;
} int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
read();
int ans = solve();
if (ans)
printf("%d\n", ans - 1);
else
printf("not possible\n");
}
return 0;
}
uva 10306 - e-Coins(完全背包)的更多相关文章
- UVA 562 Dividing coins --01背包的变形
01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...
- UVA 562 Dividing coins (01背包)
//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...
- UVA 10306 e-Coins(全然背包: 二维限制条件)
UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...
- UVA 10465 Homer Simpson(全然背包: 二维目标条件)
UVA 10465 Homer Simpson(全然背包: 二维目标条件) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&a ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- UVA 562 Dividing coins(dp + 01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...
- UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】
It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...
- uva 562 Dividing coins(01背包)
Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...
随机推荐
- haproxy image跳转 haproxy匹配 匹配到了就停止,不会继续往下匹配
<pre name="code" class="html">/***第一种 nginx 配置: location / { root /var/www ...
- 顶尖大数据挖掘实战平台(TipDM-H8)产品白皮书
顶尖大数据挖掘实战平台 (TipDM-H8) 产 品 说 明 书 广州泰迪智能科技有限公司 版权所有 地址: 广州市经济技术开发区科学城232号 网址: http: ...
- thinkphp 表名注意
$db = "DomainInfo";//查找相关任务的结果表 $result = M($db); $rescc = $result->select(); var_dump( ...
- TX enqueue DRM
- OC基础12:数字、字符串和集合1
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.有时要将一些数字数据类型的值当做对象来 ...
- Web 开发中 9 个有用的提示和技巧
在本文中,我们给出 9 个有用的 HTML.CSS 和 JavaScript 的技巧和提示,可能在做 Web 开发中经常会需要用到的,其中有几个是关于 HTML5 和 CSS3 的,如果你是一个前端开 ...
- XPath在asp.net中查询XML
.NET Framework 2.0中可以使用System.Xml.XPath命名空间下的类对XML文档进行基于路径的查询,在查询过程中需要构造类似SQL的查询字符串,该字符串遵循XPath语法.它由 ...
- Free Sql Server SMSS format Plugin
免费Sql Server 格式化插件 http://www.apexsql.com/sql_tools_refactor.aspx http://architectshack.com/PoorMans ...
- CentOS mini版安装后增加gcc编译环境
使用如下命令即可: sudo yum install gcc gcc-c++ make -y
- java中关于窗体居中显示问题
最近在学着用java写qq聊天程序,首先是登录和聊天界面,书上没有给出居中显示,通过上网查找知道有两种方案 先说第一种方法,也是以前用的方法 /获得屏幕大小Dimension screenSize = ...