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 ...
随机推荐
- Asp.Net构架(Http请求处理流程)、Asp.Net 构架(Http Handler 介绍)、Asp.Net 构架(HttpModule 介绍)
转载: HttpHaddler,HttpModule http://blog.csdn.net/jiuqiyuliang/article/details/18713451 http://www.cnb ...
- Create local metadata resource of yum
Today, I need install an oracle software for a machine whose os is Linux. As we all know, installing ...
- Linux下的sudo及其配置文件/etc/sudoers的详细配置说明
http://www.osedu.net/article/linux/2011-01-03/178.html Linux下的sudo及其配置文件/etc/sudoers的详细配置说明 1.sudo介绍 ...
- Connect the Cities(prime)
Connect the Cities Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- Android 自己定义 TextView drawableTop 图标与文字左对齐(效果图)
public class DrawableTopLeftTextView extends TextView { private Paint mPaint; private float fFontHei ...
- 网络视频传输的服务质量(QoS)
QoS(Qualityof Service)服务质量,是网络的一种安全机制, 是用来解决网络延迟和阻塞等问题的一种技术.在正常情况下,如果网络只用于特定的无时间限制的应用系统,并不需要QoS,比如We ...
- Android得到一个闹钟在第三方
收集报警信息 闹铃时间,闹铃备注信息 闹铃引起系统变化的点: 1. Send Notification (正点闹钟能够设置不发送) 2. Play audio 闹铃信息结构体 ClockInfo{ S ...
- .net 链接ORACLE的安装包
odp.net.ma
- 蜗牛爱课- CGAffineTransformMakeRotation 实现一张图片的自动旋转
self.locationTimer =[NSTimer scheduledTimerWithTimeInterval: 0.01 target: self selector:@selector(tr ...
- iOS开发之性能优化
1.避免过于庞大的XIB 当加载XIB的时候把所有的东西都放在了内存里,包括任何的图片:如果有一个不会即刻用到的view,就会浪费宝贵的内存资源了. 当加载一个引用了图片或者声音资源的nib时,nib ...