UVa Problem Solution: 10090 - Marbles
来源:互联网 发布:网络暴力案例分析ppt 编辑:程序博客网 时间:2024/05/18 00:48
This problem requires us to find two non-negative numbers m1 and m2 that satisfying
n1*m1 + n2*m2 = n
and minimize the cost
c = c1*m1 + c2*m2.
First, we can use extended Euclid's algorithm to find m1' and m2' that give
n1*m1' + n2*m2' = g
where g is the greatest common divisor of n1 and n2.
Now, if g can not divide n, then there's no valid solution. Otherwise, we can get m1'' and m2'' that give
n1*m1'' + n2*m2'' = n
by multiplying m1' and m2' by n/g.
Then we can write m1 and m2 in the following form
m1 = m1'' + n2/g * t,
m2 = m2'' - n1/g * t,
where t is a interger parameter, to investigate for which values of t the solution is feasible for our problem. Given
m1'' + n2/g * t >= 0, m2'' - n1/g * t >= 0, n1 > 0, n2 > 0,
we can get
-m1'' * g/n2 <= t <= m2'' * g/n1,
and since t must be an integer, we futher have
ceil(-m1'' * g/n2) <= t <= floor(m2'' * g/n1).
So, all the feasible values of t form an interval of consecutive integer values.
Next, let's look at the cost function
c = c1*m1 + c2*m2 = (c1*m1'' + c2*m2'') + (c1 * n2/g - c2 * n1/g) * t.
Since both (c1*m1'' + c2*m2'') and (c1 * n2/g - c2 * n1/g) are constant values, the cost function is in fact a linear function of t. Thus, its minimum value can occur only at the boundary of the feasible region.
So, we can just check the two boundaries t = ceil(-m1'' * g/n2) and t = floor(m2'' * g/n1), and choose the one yeilding the cheaper cost.
Code:
- UVa Problem Solution: 10090 - Marbles
- UVa Problem 10090 Marbles (弹珠)
- uva 10090 marbles
- UVA 10090 - Marbles (数论)
- UVA, 10090 Marbles
- uva 10090 - Marbles 扩展欧几里得
- uva 10090 - Marbles(欧几里得+通解)
- UVa Problem Solution: 10189 - Minesweeper
- UVa Problem Solution: 10033 - Interpreter
- UVa Problem Solution: 10050 - Hartals
- UVa Problem Solution: 10149 - Yahtzee
- UVa Problem Solution: 10082 - WERTYU
- UVa Problem Solution: 10150 - Doublets
- UVa Problem Solution: 848 - Fmt
- UVa Problem Solution: 10037 - Bridge
- UVa Problem Solution: 10138 - CDVII
- UVa Problem Solution: 10152 - ShellSort
- UVa Problem Solution: 10127 - Ones
- 李开复揭密微软成功之道 寄语中国软件业
- sql cluster
- java调用oracle存储过程
- 在Oracle存储过程中实现分页
- oracle 笔记
- UVa Problem Solution: 10090 - Marbles
- 更改Oracle JDeveloper的显示语言为英语
- c# as is 类型转换
- 健康放松的十个习惯
- linux 多媒体视频
- slicon ups电源故障分析:
- 微软老前辈现身说法:什么是“外企素质”
- 超速发财的36个原则
- 栈:装水的杯子(五)四色地图