UVA-10306 E-coins

Source: Internet
Author: User
Tags modulus

Topic:

There are n items, each item has x, y two value, and can have an unlimited number of.

Given S, making (x1+x2+ ...) ^2 + (y1+y2+ ...) ^2 = s ^ 2. and the minimum number of items.


Ah, after a full backpack ... One more sweep ... You can do it.

#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;ELSECOUT<<ANS<<ENDL;} return 0;}


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

Submitstatus

Description

Problem G

E-coins

input: standard input

output: standard output

Time Limit: ten seconds

Memory Limit: MB

At the Department for Bills and Coins, an extension of today ' s monetary system have newly been proposed, in order to MAK E It fit the new economy better. A number of new so called E-coins would be produced, which, in addition to have a value in the normal sense of today, ALS O has an infotechnological value. The goal of this reform are, of course, to make justice to the economy of numerous dotcom companies which, despite the fact That they is low in money surely has a lot of IT inside. All money of the old kind would keep its conventional value and get zero infotechnological value.

To successfully make value comparisons on 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 $ $ altogether and an infotechnological value of $ 4 would get an e-modulus of $ $ . Calculate the sums of the conventional and infotechnological values separately before you CA Lculate the E-modulus of the money.

To simplify the move to e-currency, you is assigned to write a program that, given the e-modulus that shall is reached an D A list of the different types of e-coins that is available, calculates the smallest amount of e-coins that is needed t O exactly match the e-modulus. There is no limit on how many e-coins for each of the type, then be used to match the given E-modulus.

Input

A line with the number of problems N (0<n<=100), followed byn Times:

    • A line with the integers m (0<m<=40) andS (0<s<=300), where m indicates the number of different e-coin types that exist in the problem, andS 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 a 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 was present on a line, they would be separated by a space. Between each problem, there'll be a 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 is 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 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 Solvi Ng Paradigms:: Dynamic Programming:: Coin change (CC)

UVA-10306 E-coins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.