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 ...
随机推荐
- Codeforces Round #461 (Div. 2)
A - Cloning Toys /* 题目大意:给出两种机器,一种能将一种原件copy出额外一种原件和一个附件, 另一种可以把一种附件copy出额外两种附件,给你一个原件, 问能否恰好变出题目要求数 ...
- Alpha7
难受
- JDK源码学习笔记——Enum枚举使用及原理
一.为什么使用枚举 什么时候应该使用枚举呢?每当需要一组固定的常量的时候,如一周的天数.一年四季等.或者是在我们编译前就知道其包含的所有值的集合. 利用 public final static 完全可 ...
- Python168的学习笔记1
在对list的条件选择有两种常用方法,直接使用filter函数,就是filter(func,sequence);另外一种就是迭代操作,类似 x for x in sequence func.这两种方法 ...
- [Visual Studio] VS2012调试时很慢的解决方案
1.转自http://guooge.com/archives/408.html VS2010调试极慢获取出现死机,因为启动了IntelliTrace Visual Studio 2010 Ulti ...
- QCon大会上推荐阅读的10本书
QCon北京2014大会将于4月25-27日在北京国际会议中心盛大开幕.QCon是由@InfoQ 主办的全球顶级技术盛会.在此次盛会中,现场将有@人民邮电出版社-信息技术分社 主办的现场扫描二维码赠书 ...
- Linux线程 之 线程 线程组 进程 轻量级进程(LWP) -systemtap -mysql
http://blog.chinaunix.net/uid-24774106-id-3650136.html http://blog.itpub.net/15480802/viewspace-7627 ...
- mysql---总体备份和增量备份
总体备份: 对整张表或者整个数据库甚至全部数据库进行备份. 增量备份: 对某一范围内的数据进行备份. 1.总体备份: 对表进行备份: 针对存储引擎为myisam的表,能够直接复制frm.myd.myi ...
- 玩转kafka
http://zookeeper.apache.org/releases.html#download http://kafka.apache.org/downloads.html(下载最新 二进制版本 ...
- Installation of NVIDIA Drivers in RHEL/CentOS and Fedora
1.首先安装所需的软件: # yum groupinstall "Development Tools" # yum install kernel-devel kernel-head ...