UVA 10306 E-coins (two-dimensional full backpack)

Source: Internet
Author: User
Tags modulus

At the Department for Bills and Coins, an extension of today ' s monetary system have newly been proposed, in order to make I T 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 O f  $4  will 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.


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 match Ed 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.


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 1> or, if S cannot is reached, the string "not possible".

Sample Input:

2 5
0 2
2 0

0 2
2 0
2 1

3 5
3 0
0 4
5 5

Sample Output:

Not possible

Two-dimensional backpack: Test Instructions: Two properties x, Y, minimum number to make (X1+X2+.XK) ^2+ (y1+y2+). YK) ^2=s*s;

Consider backpack: dp[i][j] Indicates the number of times the property is I,j.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Vector>typedef Long long ll;using namespace std; #define REPF (I, A, b) for (int i = A; I <= B; + + i) #define R EP (i, n) for (int i = 0; i < n; + + i) #define CLEAR (A, X) memset (A, x, sizeof a) const int inf=0x3f3f3f3f;     const int Maxn=330;int dp[maxn][maxn];int w1[55],w2[55];int t,n,s;int Main () {Std::ios::sync_with_stdio (false);     cin>>t;         while (t--) {cin>>n>>s;         CLEAR (Dp,inf);         dp[0][0]=0;         REP (i,n) cin>>w1[i]>>w2[i];                     REP (i,n) {REPF (j,w1[i],s) {REPF (k,w2[i],s) {                 if (Dp[j-w1[i]][k-w2[i]]!=inf) dp[j][k]=min (dp[j][k],dp[j-w1[i]][k-w2[i]]+1);         }}} int ans=inf;        REPF (i,0,s) {REPF (j,0,s)     {if (dp[i][j]!=inf&&i*i+j*j==s*s) ans=min (Ans,dp[i][j]);         }} if (Ans!=inf) cout<<ans<<endl;     else cout<< "not possible" <<endl; } return 0;}

UVA 10306 E-coins (two-dimensional full backpack)

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.