UVA - 10306 e-Coins

题目:

有n种物品,每种物品有x, y两个价值,并且可以有无限多个。

给定s,使得(x1+x2+....)^2 + (y1+y2+....)^2 = s ^ 2。并且物品个数最少。

啊,一顿完全背包之后……再扫一顿……就可以了

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int dp[1010][1010];
int x[50],y[50];
int main()
{
	int T,n,s,i,j,ans,k,inf;
	cin>>T;
	while(T--)
	{
		cin>>n>>s;
		for(i=0;i<n;i++)
			cin>>x[i]>>y[i];
		memset(dp,127,sizeof(dp));
		inf=*dp[0];
		dp[0][0]=0;
		for(i=0;i<=s;i++)
			for(j=0;j<=s;j++)
				for(k=0;k<n;k++)
					if(i>=x[k]&&j>=y[k])
						dp[i][j]=min(dp[i][j],dp[i-x[k]][j-y[k]]+1);
		ans=inf;
		for(i=0;i<=s;i++)
			for(j=0;j<=s;j++)
				if(i*i+j*j==s*s)
					ans=min(ans,dp[i][j]);
		if(ans==inf)
			cout<<"not possible"<<endl;
		else
			cout<<ans<<endl;
	}
	return 0;
}

e-Coins

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

Submit
Status

Description

Problem G

e-Coins

Input: standard input

Output: standard output

Time Limit: 10 seconds

Memory Limit: 32 MB

At the Department for Bills and Coins, an extension of today‘s monetary system has newly been proposed, in order to make it fit the new economy better. A number of new so called e-coins will be produced, which, in addition to having a value in the normal
sense of today, also have an InfoTechnological value. The goal of this reform is, of course, to make justice to the economy of numerous dotcom companies which, despite the fact that they are low on money surely have a lot of
IT inside. All money of the old kind will keep its conventional value and get zero InfoTechnological value.

To successfully make value comparisons in the new system, something called the e-modulus is introduced. This is calculated as
SQRT(X*X+Y*Y), where X and Y hold the sums of the conventional and InfoTechnological values respectively. For instance, money with a conventional value of
$3 altogether and an InfoTechnological value of $4 will get an e-modulus of
$5. Bear in mind that you have to calculate the sums of the conventional and InfoTechnological values separately before you calculate the e-modulus of the money.

To simplify the move to e-currency, you are assigned to write a program that, given the e-modulus that shall be reached and a list of the different types of e-coins that are available, calculates the smallest amount of e-coins that are needed to exactly
match the e-modulus. There is no limit on how many e-coins of each type that may be used to match the given e-modulus.

Input

A line with the number of problems n (0<n<=100), followed by
n times:

  • A line with the integers m (0<m<=40) and
    S (0<S<=300), where m indicates the number of different e-coin types that exist in the problem, and
    S states the value of the e-modulus that shall be matched exactly.
  • m lines, each consisting of one pair of non-negative integers describing the value of an e-coin. The first number in the pair states the conventional value, and the second number holds the InfoTechnological value
    of the coin.

When more than one number is present on a line, they will be separated by a space. Between each problem, there will be one blank line.

Output

The output consists of n lines. Each line contains either a single integer holding the number of coins necessary to reach the specified e-modulus
S or, if S cannot be reached, the string
"not possible"
.

Sample Input:

3

2 5

0 2

2 0

3 20

0 2

2 0

2 1

3 5

3 0

0 4

5 5

Sample Output:

not possible

10

2


(Joint Effort Contest, Problem Source: Swedish National Programming Contest, arranged by department of Computer Science at Lund Institute of Technology.)

Source

Root :: Competitive Programming: Increasing the Lower Bound of Programming Contests (Steven & Felix Halim) :: Chapter 3. Problem Solving Paradigms :: Dynamic Programming ::
Coin Change - Classical

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 5. Dynamic Programming

Root :: Competitive Programming 2: This increases the lower bound of Programming Contests. Again (Steven & Felix Halim) :: Problem Solving Paradigms :: Dynamic Programming ::
Coin Change (CC)

Root :: Competitive Programming 3: The New Lower Bound of Programming Contests (Steven & Felix Halim) :: Problem Solving Paradigms :: Dynamic Programming ::
Coin Change (CC)

时间: 2024-10-08 16:25:21

UVA - 10306 e-Coins的相关文章

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

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

uva 562 Dividing coins

Dividing coins It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great lengt

UVA 10306 (二维完全背包)

UVA 10306 题意:有种新货币,它的价值有两个值 x 和 y,有一种 e-modulus 值, 计算方式为 sqrt(X*X + Y*Y); 其中 X 和 Y 分别是所有货币 x 值的总和 和 y 值的总和,即 (x1 + x2 +... +xm)^2 + (y1 + y2 +...+ym)^2 = s^2;给出 e-modulus 的值 s , 每种货币的数量无限, 求满足 S 值的货币的最小数量. 输入:第一行输入 n , n 组测试:第二行 m , s :表示有 m 中货币,e-mo

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

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1247 题意: 对于每个样例,先给定两个数n,m,分别表示有n种硬币,对于每一种硬币有两个价值,分别记做x,y,题目要求从中选择一些硬币,使得满足m*m=X*X+Y*Y, 其中X是选出的硬币的所有x价值的和,Y是所有选出的硬币的y价值的和,硬币有无数多个,现在要求的是,满足上述要求使用的最少的

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

option=com_onlinejudge&Itemid=8&page=show_problem&problem=1247">http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1247 题意: 对于每一个例子.先给定两个数n,m,分别表示有n种硬币,对于每一种硬币有两个价值.分别记做x,y,题目要求从中

UVA 10306 e-Coins(二维完全背包)

At the Department for Bills and Coins, an extension of today's monetary system has newly been proposed, in order to make it fit the new economy better. A number of new so called e-coins will be produced, which, in addition to having a value in the no

UVA 562 Dividing coins (01背包基础)

[题目链接]:click here~~ 代码: /* * Problem: UVA No.562 * Running time: 0MS * Complier: C++ * Author: ACM_herongwei * Create Time: 11:12 2015/9/9 星期三 * zeroonebags * 将金币总价值的一半作为背包容量,然后zeronebags */ #include <stdio.h> #include <iostream> #include <

(背包dp)UVA - 562 Dividing coins

题意:有n个硬币,每个硬币有个价值,两个人分配硬币,要求最公平分配时候两人拿到的钱的差. 分析:很明显,两人拿到的钱的差越小越公平. 一开始想,一定是一人一半最公平,所以直接把总和sum/2,对着half跑01背包,但是WA了,修改后分别讨论奇偶,额外进行一次sum-half的01背包,也WA,仔细想想觉得有些漏洞. 所以,这题其实可以干脆直接跑sum的背包,不断更新ans=min(ans,sum-dp[j]*2)就行了.如果ans==inf,表示不能分,也就是1个,这时输出0. 代码: 1 #

UVA 562 Dividing coins (01背包)

//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include <algorithm> using namespace std; int value[100000],dp[100000]; int main() { int n,m,sum,sum1; cin>>n; while(n--) { cin>>m; sum=0; for(int