Ultraviolet A 10306 e-Coins (two-dimensional full backpack), 10306e-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 normal sense of today, also have an infographic ical 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 lotITInside. All money of the old kind will keep its conventional value and get zero infographic ical value.
To successfully make value comparisons in the new system, something called the e-modulus is introduced. This is calculatedSQRT (X * X + Y * Y), WhereXAndYHold the sums of the conventional and infoequalical values respectively. For instance, money with a conventional value$3Altogether and an infographic ical value$4Will get an e-modulus$5. Bear in mind that you have to calculate the sums of the conventional and infoequalical 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 problemsN (0 <n <= 100), FollowedNTimes:
- A line with the integersM (0 <m <= 40)AndS (0 <s <= 300), WhereMIndicates the number of different e-coin types that exist in the problem, andSStates the value of the e-modulus that shall be matched exactly.
- MLines, 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 infographic ical 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 consistsNLines. Each line contains either a single integer holding the number of coins necessary to reach the specified e-modulusSOr, ifSCannot 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
Two-dimensional backpack: two attributes x and y. Calculate the least number (x1 + x2 +. xk) ^ 2 + (y1 + y2 + .. yk) ^ 2 = s * s;
Considering the backpack: dp [I] [j] indicates the time when the attribute is I and 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 REP( 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;}