UVA 10090 Marbles(扩展欧几里得)
Marbles
Input: standard input
Output: standard output
I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The boxes are of two types:
Type 1: each box costs c1 Taka and can hold exactly
n1 marbles
Type 2: each box costs c2 Taka and can hold exactly
n2 marbles
I want each of the used boxes to be filled to its capacity and also to minimize the total cost of buying them. Since I find it difficult for me to figure out how to distribute my marbles among the boxes, I seek your help. I want your program to be efficient
also.
Input
The input file may contain multiple test cases. Each test case begins with a line containing the integer n (1 <= n <= 2,000,000,000). The second line contains
c1and n1, and the third line contains
c2 and n2. Here, c1,
c2, n1and n2 are all positive integers having values smaller than 2,000,000,000.
A test case containing a zero forn in the first line terminates the input.
Output
For each test case in the input print a line containing the minimum cost solution (two nonnegative integers
m1 and m2, where mi= number of
Type i boxes required) if one exists, print "failed" otherwise.
If a solution exists, you may assume that it is unique.
Sample Input
43
1 3
2 4
40
5 9
5 12
0
Sample Output
13 1
failed
题意:一个人有n个弹球。如今要把这些弹球所有装进盒子里。第一种盒子每一个盒子c1美元,能够恰好装n1个弹球。另外一种盒子每一个盒子c2元。能够恰好装n2个弹球。找出一种方法把这n个弹球装进盒子,每一个盒子都装满,而且花费最少的钱。
分析:如果第一种盒子买m1个,另外一种盒子买m2个,则n1*m1 + n2*m2 = n。由扩展欧几里得 ax+by=gcd(a,b)= g,如果n%g!=0。则方程无解。
联立两个方程。能够解出m1=nx/g, m2=ny/g,所以通解为m1=nx/g + bk/g, m2=ny/g - ak/g,
又由于m1和m2不能是负数,所以m1>=0, m2>=0,所以k的范围是 -nx/b <= k <= ny/a。且k必须是整数。
如果
k1=ceil(-nx/b)
k2=floor(ny/b)
假设k1>k2的话则k就没有一个可行的解。于是也是无解的情况。
设花费为cost,则cost = c1*m1 + c2*m2,
把m1和m2的表达式代入得
cost=c1*(-xn/g+bk/g)+c2*(yn/g-ak/g) = ((b*c1-a*c2)/g)*k+(c1*x*n+c2*y*n)/g
这是关于k的一次函数。单调性由b*c1-a*c2决定。
若b*c1-a*c2 >= 0,k取最小值(k1)时花费最少;否则,k取最大值(k2)时花费最少。
#include<iostream>
#include<cmath>
using namespace std;
typedef long long LL; LL extend_gcd(LL a, LL b, LL *x, LL *y)
{
LL xx, yy, g;
if(a < b) return extend_gcd(b, a, y, x);
if(b == 0) {
*x = 1;
*y = 0;
return a;
}
else {
g = extend_gcd(b, a%b, &xx, &yy);
*x = yy;
*y = (xx - a/b*yy);
return g;
}
} int main()
{
LL n, c1, n1, c2, n2, x, y;
while(cin >> n && n) {
cin >> c1 >> n1 >> c2 >> n2;
LL g = extend_gcd(n1, n2, &x, &y);
if(n % g != 0) {
cout << "failed" << endl;
continue;
}
LL mink = ceil(-n * x / (double)n2);
LL maxk = floor(n*y / (double)n1); // mink <= k <= maxk
if(mink > maxk) {
cout << "failed" << endl;
continue;
}
if(c1 * n2 <= c2 * n1) {
x = n2 / g * maxk + n / g * x;
y = n / g * y - n1 / g * maxk;
}
else {
x = n2 / g * mink + n / g * x;
y = n / g * y - n1 / g * mink;
}
cout << x << " " << y << endl;
}
return 0;
}
UVA 10090 Marbles(扩展欧几里得)的更多相关文章
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- UVA 10090 - Marbles 拓展欧几里得
I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The ...
- UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge
题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...
- UVA 12169 Disgruntled Judge 扩展欧几里得
/** 题目:UVA 12169 Disgruntled Judge 链接:https://vjudge.net/problem/UVA-12169 题意:原题 思路: a,b范围都在10000以内. ...
- UVA 12169 Disgruntled Judge 枚举+扩展欧几里得
题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...
- UVA 10673 扩展欧几里得
题意:给出x 和k,求解p和q使得等式x = p[x / k] + q [ x / k], 两个[x / k]分别为向下取整和向上取整 题解:扩展欧几里得 //meek///#include<b ...
- UVa 11768 格点判定(扩展欧几里得求线段整点)
https://vjudge.net/problem/UVA-11768 题意: 给定两个点A(x1,y1)和B(x2,y2),均为0.1的整数倍.统计选段AB穿过多少个整点. 思路: 做了这道题之后 ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
随机推荐
- BZOJ.2194.快速傅立叶之二(FFT 卷积)
题目链接 \(Descripiton\) 给定\(A[\ ],B[\ ]\),求\[C[k]=\sum_{i=k}^{n-1}A[i]*B[i-k]\ (0\leq k<n)\] \(Solut ...
- CodeForces 81D.Polycarp's Picture Gallery 乱搞
D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- RN生命周期
网上看的博客,看着写的很好,想深入学RN的详细看下之后,再自己敲敲吧!有助于身体健康! 一个RN组件从它被加载,到最终被卸载会经历一个完整的生命周期.所谓生命周期,就是一个对象从开始生成到最后消亡所经 ...
- PWM DAC vs. Standalone
http://analogtalk.com/?p=534 http://analogtalk.com/?p=551 Posted by AnalogAdvocate on April 09, 2010 ...
- Lua中调用C函数(lua-5.2.3)
Lua能够调用C函数的能力将极大的提高Lua的可扩展性和可用性. 对于有些和操作系统相关的功能,或者是对效率要求较高的模块,我们全然能够通过C函数来实现,之后再通过Lua调用指定的C函数. 对于那些可 ...
- java基础学习总结——对象转型
一.对象转型介绍 对象转型分为两种:一种叫向上转型(父类对象的引用或者叫基类对象的引用指向子类对象,这就是向上转型),另一种叫向下转型.转型的意思是:如把float类型转成int类型,把double类 ...
- Unity中一键创建常用文件夹
Unity中一键创建常用文件夹 说明 项目测试版本Unity5.3. 这个一个小工具:功能非常简单,就是一键给新建工程添加所有文件夹.到此结束. 但是具体咋操作呢? 与把大象装进冰箱一样,三步,下载代 ...
- Android实例剖析笔记(一)
摘要:用实例讲解Andriod的开发过程 开卷语 俗话说,“熟读唐诗三百首,不会作诗也会吟”.最近收集了很多Android的示例代码,从这些代码的阅读和实验中学习到很多知识,从而产生写这个系列的打算, ...
- 解决Sqoop报错Could not load db driver class: com.intersys.jdbc.CacheDriver
报错栈: // :: INFO tool.CodeGenTool: Beginning code generation // :: ERROR sqoop.Sqoop: Got exception r ...
- JSON.parse()和jQuery.parseJSON()的区别
jQuery.parseJSON(jsonString) : 将格式完好的JSON字符串转为与之对应的JavaScript对象 (jquery 方法) 1 2 3 var str = '[{&qu ...