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
c
2 and n2. Here, c1,
c
2, 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(扩展欧几里得)的更多相关文章

  1. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  2. UVA 10090 - Marbles 拓展欧几里得

    I have some (say, n) marbles (small glass balls) and I am going to buy some boxes to store them. The ...

  3. UVa 12169 (枚举+扩展欧几里得) Disgruntled Judge

    题意: 给出四个数T, a, b, x1,按公式生成序列 xi = (a*xi-1 + b) % 10001 (2 ≤ i ≤ 2T) 给出T和奇数项xi,输出偶数项xi 分析: 最简单的办法就是直接 ...

  4. UVA 12169 Disgruntled Judge 扩展欧几里得

    /** 题目:UVA 12169 Disgruntled Judge 链接:https://vjudge.net/problem/UVA-12169 题意:原题 思路: a,b范围都在10000以内. ...

  5. 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- ...

  6. UVA 10673 扩展欧几里得

    题意:给出x 和k,求解p和q使得等式x = p[x / k] + q [ x / k], 两个[x / k]分别为向下取整和向上取整 题解:扩展欧几里得 //meek///#include<b ...

  7. UVa 11768 格点判定(扩展欧几里得求线段整点)

    https://vjudge.net/problem/UVA-11768 题意: 给定两个点A(x1,y1)和B(x2,y2),均为0.1的整数倍.统计选段AB穿过多少个整点. 思路: 做了这道题之后 ...

  8. 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个点,求射 ...

  9. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

随机推荐

  1. bzoj 1176

    收获: 1.min, max, sum, 属于判定,等询问是”对象对答案贡献独立“,即不需要其他对象就了能更新答案,并保证只要所有对象更新过答案,那么该答案就是正确的.大概这就是所谓的”修改独立“. ...

  2. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题

    A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...

  3. How To Allow Blocked Content on Internet Explorer

    Follow the steps below if you are tired of having to "Enable Blocked Content" in IE each t ...

  4. chrome 浏览器 console 加入 jquery 测试调试 一介布衣

    chrome 浏览器 console 加入 jquery 测试调试 一介布衣   var jquery = document.createElement('script'); jquery.src = ...

  5. A* search算法解迷宫

    这是一个使用A* search算法解迷宫的问题,细节请看:http://www.laurentluce.com/posts/solving-mazes-using-python-simple-recu ...

  6. 2008 SCI 影响因子(Impact Factor)

    2008 SCI 影响因子(Impact Factor) Excel download 期刊名缩写 影响因子 ISSN号 CA-CANCER J CLIN 74.575 0007-9235 NEW E ...

  7. restful api安全验证问题

    没有绝对的安全,这个话题很深, 下文都是自己的一些理解,水平有限,如有勘误,希望大家予以指正. 由于 RESTful Api 是基于 Http 协议的 Api,是无状态传输,所以 只要和用户身份有关的 ...

  8. DocumentManager在标签位置显示气泡框 z

    关于DevExpress DockManager下的DocumentManager头部标签如何显示气泡框,类似Visual studio那样显示文件的路径,如下图所示,------- 方式很简单,从工 ...

  9. kafka-manager:kafak的管理界面的安装和使用

    下载打包 release下载:https://github.com/yahoo/kafka-manager/releases 源码位置:https://github.com/yahoo/kafka-m ...

  10. 【BZOJ】【2878】【NOI2012】迷失游乐园

    树形+基环树DP/数学期望 然而我并不会做…… 题解戳这里:http://blog.csdn.net/u011265346/article/details/46328543 好吧先考虑一个简单点的,当 ...