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 ...
随机推荐
- Handler机制原理图、源码、使用!!!!!
android的消息处理机制——Looper,Handler,Message (原理图.源码) 转自:http://my.oschina.net/u/1391648/blog/282892 在开始讨 ...
- VMware虚拟机三种网络模式的区别(上篇)
提到VMware大家就想起了虚拟机技术,虚拟机技术在最近的几年中得到了广泛的发展,一些大型网络服务商都开始采用虚拟机技术,不仅节省了投资成本,更节约了能源的消耗. 我们知道VMware也分几种版本,普 ...
- linux脚本之简单实例
利用脚本计算10的阶乘 简单说明一下: #!/bin/bash说明该shell使用的bash shell程序.这一句不可少for i in `seq 1 10`还可以写成for i in 1 2 3 ...
- tty/pts 相关指令
http://unix.stackexchange.com/questions/136662/how-can-we-know-whos-at-the-other-end-of-a-pseudo-ter ...
- vim改变多窗口的大小
摘自:http://blog.163.com/lgh_2002/blog/static/44017526201031671927647/ vim改变多窗口的大小 VIM改变窗口大小 *window-r ...
- Eclipse上GIT插件EGIT使用
一.安装EGit插件 參考:MyEclipse8.5整合Git 二.EGit配置 配置个人信息 Window > Preferences > Team > Git > Conf ...
- 为什么VS提示SurfFeatureDetector不是cv的成员函数
surf和sift算法都是在头文件#include <opencv2/features2d/features2d.hpp>中,但在新的opencv版本出来后,如果仍然使用这个头文件就会出现 ...
- GCC单独编译host/examples/ tx_waveforms.cpp
1.编译 须要链接uhd库和boost_program_options库以及boost_thread库: g++ tx_waveforms.cpp -o a -luhd -lboost_program ...
- 3D转换
CSS3 允许您使用 3D 转换来对元素进行格式化. 在本章中,您将学到其中的一些 3D 转换方法: 1. rotateX() 2. rotateY() <!DOCTYPE HTML>&l ...
- 【转】jQuery on()选择器函数
on()函数用于为指定元素的一个或多个事件绑定事件处理函数. 此外,你还可以额外传递给事件处理函数一些所需的数据. 从jQuery 1.7开始,on()函数提供了绑定事件处理程序所需的所有功能,用于统 ...