题目链接: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(完全背包)的更多相关文章

  1. UVA 562 Dividing coins --01背包的变形

    01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostre ...

  2. UVA 562 Dividing coins (01背包)

    //平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include ...

  3. UVA 10306 e-Coins(全然背包: 二维限制条件)

    UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...

  4. UVA 10465 Homer Simpson(全然背包: 二维目标条件)

    UVA 10465 Homer Simpson(全然背包: 二维目标条件) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&a ...

  5. UVA.10130 SuperSale (DP 01背包)

    UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...

  6. POJ 3260 The Fewest Coins(多重背包+全然背包)

    POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...

  7. UVA 562 Dividing coins(dp + 01背包)

    Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were figh ...

  8. UVA 562 Dividing coins【01背包 / 有一堆各种面值的硬币,将所有硬币分成两堆,使得两堆的总值之差尽可能小】

    It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nic ...

  9. uva 562 Dividing coins(01背包)

      Dividing coins  It's commonly known that the Dutch have invented copper-wire. Two Dutch men were f ...

随机推荐

  1. Delphi通过GetFileVersionInfo和VerQueryValue等API函数取得详细EXE信息

    This has been described at About: http://delphi.about.com/cs/adptips2001/a/bltip0701_4.htmBasically, ...

  2. Delphi利用Webbrowser登陆QQ群文档

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  3. cf459B Pashmak and Flowers

    B. Pashmak and Flowers time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  5. vim改变多窗口的大小

    摘自:http://blog.163.com/lgh_2002/blog/static/44017526201031671927647/ vim改变多窗口的大小 VIM改变窗口大小 *window-r ...

  6. linux下如何产生core,调试core

    linux下如何产生core,调试core 摘自:http://blog.163.com/redhumor@126/blog/static/19554784201131791239753/ 在程序不寻 ...

  7. hdu 2571 命运(dp)

    Problem Description 穿过幽谷意味着离大魔王lemon已经无限接近了! 可谁能想到,yifenfei在斩杀了一些虾兵蟹将后,却再次面临命运大迷宫的考验,这是魔王lemon设下的又一个 ...

  8. AS3: Socket 数据包 收 发

    AS3.0中使用Socket使用tcp服务器协议,它是一种流协议,不停的将分片传输给客户端,P作为流,发包是不会整包到达的,而是源源不断的. 它不同于UDP服务器协议,UDP作为数据包协议,整包到达. ...

  9. ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance

    1.启动报错SQL> startupORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instanceORAC ...

  10. js触摸屏案例

    js 手机端触发事事件.javascript手机端/移动端触发事件   处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: 1 2 3 4 touchstart:  ...