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:
T ype 1: each box costs c1 Taka and can hold exactly n1 marbles
T ype 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 c1 and n1, and the third line contains c2 and n2.
Here, c1, c2, n1 and n2 are all positive integers having values smaller than 2,000,000,000.
A test case containing a zero for n 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 T ypei 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个球装进盒子,每个盒子都装满,并且花费最少的钱。

题解:http://blog.csdn.net/lyhvoyage/article/details/37932481

假设第一种盒子买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 <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll;
const int N=; /*m1*n1 + m2*n2 = n; a = n1;
b = n2; a*x + b*y = g m1 = x*n/g + k*b/g;
m2 = y*n/g - k*a/g; cost = m1*c1 + m2*c2; cost = x*n*c1/g + k*b*c1/g + y*n*c2/g - k*a*c2/g ; cost = k*(b*c1 - a*c2)/g + (y*n*c2+x*n*c1)/g;
*/
ll ExpGcd(ll a,ll b,ll &x,ll &y)
{
ll temp,p;
if(b==)
{
x=; y=;
return a;
}
p=ExpGcd(b,a%b,x,y);
temp=x; x=y; y=temp-(a/b)*y;
return p;
}
ll n,x,y,c1,c2,n1,n2,l,r;
int main() {
while(scanf("%lld",&n)!=EOF) {
if(n == ) break;
scanf("%lld%lld",&c1,&n1);
scanf("%lld%lld",&c2,&n2);
ll g = ExpGcd(n1,n2,x,y);
if(n % g != ) {
printf("failed\n");
continue;
}
ll k1 = ceil(-n * x * 1.0/ n2);
ll k2 = floor(n * y * 1.0/ n1);
if(k1>k2) {
printf("failed\n");
continue;
}
if((c2*n1 - c1*n2)>) {
l = n2 / g * k2 + n/g * x;
r = n / g * y - n1 / g * k2;
}
else {
l = n2 / g * k1 + n / g * x;
r = n / g * y - n1 / g * k1;
}
printf("%lld %lld\n", l , r);
}
return ;
}

代码

UVA 10090 - Marbles 拓展欧几里得的更多相关文章

  1. UVA 10090 Marbles 扩展欧几里得

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

  2. Lattice Point or Not UVA - 11768(拓展欧几里得)

    原文地址:https://www.cnblogs.com/zyb993963526/p/6783532.html 题意: 给定两个点A(x1,y1)和B(x2,y2),均为0.1的整数倍.统计选段AB ...

  3. Play with Floor and Ceil UVA - 10673(拓展欧几里得)

    因为我现在还不会用这个...emm...蒟蒻...只看了 从来没用过....所以切一道水题...练一下... 人家讲的很好  https://blog.csdn.net/u012860428/arti ...

  4. uva 10548 - Find the Right Changes(拓展欧几里得)

    题目链接:uva 10548 - Find the Right Changes 题目大意:给定A,B,C,求x,y,使得xA+yB=C,求有多少种解. 解题思路:拓展欧几里得,保证x,y均大于等于0, ...

  5. UVA.12169 Disgruntled Judge ( 拓展欧几里得 )

    UVA.12169 Disgruntled Judge ( 拓展欧几里得 ) 题意分析 给出T个数字,x1,x3--x2T-1.并且我们知道这x1,x2,x3,x4--x2T之间满足xi = (a * ...

  6. NOIP2012拓展欧几里得

    拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...

  7. poj 1061 青蛙的约会 拓展欧几里得模板

    // poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...

  8. bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得

    这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...

  9. POJ 2891 Strange Way to Express Integers(拓展欧几里得)

    Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express ...

随机推荐

  1. windows原生开发之界面疑云

        windows桌面开发,界面始终是最大的困惑.我们对前端工具的要求,其实只有窗体设计器.消息映射,过分点的话自适应屏幕.模型绑定.能够免于手工书写,其实这个问题并不复杂,但VS不实现.QT语法 ...

  2. LoadRunner执行过程报错“Failed to connect to server "xxx.xxx.xxx.xxx:xx":[10060] connetion time out”

    执行性能测试过程中,LR报错: Action.c(6):Error -27796: Failed to connect to server "xxx.xxx.xxx.xxx:xx" ...

  3. ImageSource的使用

    很多时候,我们会使用图片来装饰UI,比如作为控件背景等.而这些图片可以分为两种形式,即存在于本地文件系统中的图片和存在于内存中的图片对于这两种形式的图片,在WPF中,使用方法不同,下面主要说明针对这两 ...

  4. [LeetCode] Best Meeting Point

    Problem Description: A group of two or more people wants to meet and minimize the total travel dista ...

  5. MySQL降权:MySQL以Guests帐户启动设置方法

    MySQL安装到Windows上,默认是以SYSTEM权限运行,如下图: SYSTEM是超级管理员.不是必须,不推荐用此权限运行任何程序. 本文将演示如何在GUEST帐户下运行MySQL. 第一步:建 ...

  6. 几种图片下载lib对比

  7. (转)使用Custom Draw实现ListCtrl的重绘

    使用Custom Draw实现ListCtrl的重绘   common control 4.7版本介绍了一个新的特性叫做Custom Draw,这个名字显得模糊不清,让人有点摸不着头脑,而且MSDN里 ...

  8. mac x Yosemide(10.10) 下安装 jdk 1.7 (jdk 1.8)的方法

    当我们想在mac x yosemide 系统中更新jdk到1.7(1.8)的时候,会弹出下面的错误提示 解决这个问题的办法如下: 1.下载 好jdk 1.7(1.8) 地址:http://www.or ...

  9. 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)

    转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...

  10. ios auto layout demystified (一)

    Ambiguous Layout 在开发过程中,你可以通过调用hasAmbiguousLayout 来测试你的view约束是否足够的.这个会返回boolean值.如果有一个不同的frame就会返回ye ...