POJ3021 e-coins: difference between DP solution and normal solution

POJ 3021 e-coins: difference between DP algorithm and conventional method

One、 Different solutions

1, DP thinking

(1) The method of DP is an exhaustive method;

(2) DP is constantly dynamic programming when it exhausts all possible, that is to say, whether the previous results are improved according to the new calculation results or not, if there is improvement, it will be replaced by the current optimum;

(3) After all possibilities are calculated, then gets the ultimate optimum.

  1. Steps for DP to solve this problem:

(1) DP [i] [J] is defined as: the number of old currencies needed when I traditional value and j information value are selected from m old currencies;

(2) The optimal algorithm is dynamic transfer equation;

(3) For each I or j from 0 to s, the number of old currencies required to make up this (I, J) is calculated;

(4) Find out all (I, J) that meet the exchange conditions: I ^ 2 + J ^ 2 = = s ^ 2, and the minimum DP [i] [J] is required.

  1. The advantage of DP method is that it simplifies the algorithm with the cost of calculation, in other words, it only needs to find a simple state transition equation to carry out the calculation. The cost of DP for calculating is a little high, and all that can’t meet the exchange conditions have been calculated.

  2. Are there any other solutions:

(1) You can find all the combinations of (I, J) that satisfy s ^ 2 = I ^ 2 + J ^ 2;

(2) For each group (I, J), the loop looks for the minimum number of the currency tuple (a,b) that can satisfy (I, J). However, there may be many loops, such as m loops (step-by-step problem), for example, tuple 1 from 0 to s, tuple 2 from 0 to s…, tuple m from 0 to s. It seems to be more complex than DP.

Two、 POJ 3021 overview

  1. Currency has traditional value and information technology value. When it is converted into new currency, the sum of the square of traditional value and information value is equal to the sum of the square of new currency.

  2. Given m currencies (a, b), a is the traditional value of this currency, b is the IT value, each currency can be selected multiple times, so that the sum of traditional value and it value will be obtained.

  3. Given the number of new currencies needed, ask if it is possible to convert the m currencies given above into new currencies in the following ways: traditional value ^ 2 + IT ^ 2 = new currency ^ 2. If it can’t, output impossible, if it can, output the number of old currencies needed.

  4. Input:

The number of questions is n (0 < n < = 100), followed by n times of the following data:

(1) m (0 < m < = 40) and s (0 < s < = 300), m represents the type of old currency, and s represents the number of new currencies to be exchanged.

(2) m-line two tuples, indicating that there are m kinds of old currencies, each of which is described by a pair of traditional values and information values.

  1. Output: n lines, each containing an integer, the number of old currencies needed to convert s new currency, or “impossible”.

Three, POJ 3021 analysis

  1. Definition problem: let DP (i, j) store the minimum number of various currencies (a, b) when there are a total of i traditional values and j IT values. If such i and j just can exchange s (satisfying i ^ 2 + j ^ 2 = s ^ 2), the minimum of all such I and j is the requirement.

  2. State transition: for each DP (i, j) (i traditional value and j IT value), judge each currency (a, b) in turn. If this currency is used, then the traditional value increases by a, and the IT value increases by b, that is, it gets from DP (i-a, j-b) + 1. Otherwise, DP (i, j) will not change. So, the equation of state transition is: DP [i] [j] = min (DP [i] [j], DP [i-a[k]] [j-b[k]] + 1);

  3. At first, all DP (i, j) initial values are very large, and DP (0,0) = 0. Then, enumerate all the values from DP (0,0) to DP (s, s). Its calculation method is to traverse all currencies (a, b) for each i and j, to see if the minimum number of currencies that satify this i and j will change.

  4. Because i ^ 2 + j ^ 2 = s ^ 2, I can enumerate all possible combinations of s from 1 to s and j from 1 to s. Therefore, if all i from 1 to s and j from 1 to s, we traverse all the tuples in turn to see if there is a tuple that makes DP (i, j) smaller, it means that there is a better way to get the i traditional value and j IT value.

  5. For the combination of i and j that can satisfy the exchange condition, find the least number of tuples as the output result.

POJ3021 e-coins
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 868 Accepted: 420

Description
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 comparisions in the new system, something called the e-modulus is introduced. This is calculated as SQRT(XX+YY), 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 are 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 (0,4)
2 0
2 1 (16,8)

3 5
3 0
0 4
5 5

Sample Output
not possible
10
2
The illustration examplifies adding 8 coins of conventional value 2 and InfoTechnological value 1, and 2 coins with pure InfoTechnological value 2. The e-modulus is of course 20 as SQRT((82+20)2+(8*1+2*2)2)=SQRT(162+122)=20

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值