uva 10306 e-Coin

来源:互联网 发布:zookeeper 默认端口 编辑:程序博客网 时间:2024/05/21 06:36

原题:
At the Department for Bills and Coins, an extension of today’s monetary system has newly been pro-
posed, 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 InfoTechno-
logical 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 3altogetherandanInfoTechnologicalvalueof4 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
大意:
每个e-Coin有两个值,一个是conventional value另一个是InfoTechnological value 现在给你一个值,让你用给你的e-Coin组成这个值,计算方方法是你选用的e-Coin的所有con值的平方加上所有info值的平方的和,如果这个和等于题目中给你的值的平方,那就输出你所用e-Coin的最小数量,否则输出not possible

#include<iostream>#include<algorithm>#include<map>#include<string>#include<cstring>#include<sstream>#include<cstdio>#include<vector>#include<cmath>#include<stack>#include<queue>#include<iomanip>#include<set>#include<fstream>#include <limits.h>using namespace std;//fstream output,input;int dp[301][301];int conv[41],info[41];int main(){    ios::sync_with_stdio(false);    int i,j,n,m,s;    cin>>n;    while(n--)    {        cin>>m>>s;        for(int i=0;i<=s;i++)            for(int j=0;j<=s;j++)            dp[i][j]=999999;        for(int i=1;i<=m;i++)        {            cin>>conv[i]>>info[i];            dp[conv[i]][info[i]]=1;        }        for(int k=1;k<=m;k++)        {            for(int i=conv[k];i<=s;i++)            {                for(int j=info[k];j<=s;j++)                    dp[i][j]=min(dp[i][j],dp[i-conv[k]][j-info[k]]+1);            }        }        int ans=999999;        for(int i=0;i<=s;i++)        {            for(int j=0;j<=s;j++)            {                if(i*i+j*j==s*s&&dp[i][j]<ans)                    ans=dp[i][j];            }        }        if(ans==999999)            cout<<"not possible"<<endl;        else            cout<<ans<<endl;    }//  input.close();//  output.close();    return 0;}

解析:
联系完全背包问题,转移方程还不是特别难想到dp[x][y]=min(dp[x][y],dp[x-conv[k]][y-info[k]]+1),x和y分别代表conv值的总和以及info值的总和,dp[x][y]表示满足conv的总和为x,info的总和为y时的所用的最小e-coin的数量,接下来没完。然后继续枚举每个x和y的值,判断x*x+y*y是否等于s*s,并求出最小的dp值即可。

0 0
原创粉丝点击