The Balance
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 5991   Accepted: 2605

Description

Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side of the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and two 700mg weights on the other (Figure 2), she would not choose this solution because it is less convenient to use more weights. 
You are asked to help her by calculating how many weights are required. 

Input

The input is a sequence of datasets. A dataset is a line containing three positive integers a, b, and d separated by a space. The following relations hold: a != b, a <= 10000, b <= 10000, and d <= 50000. You may assume that it is possible to measure d mg using a combination of a mg and b mg weights. In other words, you need not consider "no solution" cases. 
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.

Output

The output should be composed of lines, each corresponding to an input dataset (a, b, d). An output line should contain two nonnegative integers x and y separated by a space. They should satisfy the following three conditions.

  • You can measure dmg using x many amg weights and y many bmg weights.
  • The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous condition.
  • The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous two conditions.

No extra characters (e.g. extra spaces) should appear in the output.

Sample Input

700 300 200
500 200 300
500 200 500
275 110 330
275 110 385
648 375 4002
3 1 10000
0 0 0

Sample Output

1 3
1 1
1 0
0 3
1 1
49 74
3333 1
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;
LL extgcd(LL a,LL b,LL &x,LL &y)
{
LL d=a;
if(b!=)
{
d=extgcd(b,a%b,y,x);
y-=(a/b*x);
}
else
{
x=;
y=;
}
return d;
}
LL GCD(LL a,LL b)
{
if(b==)
{
return a;
}
return GCD(b,a%b);
}
LL a,b,d;
int main()
{
while(scanf("%lld%lld%lld",&a,&b,&d)!=EOF&&(a+b+d)!=)
{
LL x,y;
LL gcd=GCD(a,b);
a/=gcd;
b/=gcd;
d/=gcd;
extgcd(a,b,x,y);
x*=d;
y*=d; LL x1=x;
x1=(x1%b+b)%b;//ax+by=1最小正整数解
LL y1=(d-a*x1)/b;
if(y1<)y1=-y1; LL y2=y; y2=(y2%a+a)%a; //最小正整数解
LL x2=(d-b*y2)/a;
if(x2<)x2=-x2; if(x1+y1<x2+y2)
{
printf("%lld %lld\n",x1,y1);
}
else
{
printf("%lld %lld\n",x2,y2);
}
} return ;
}

Java版:

import java.util.Scanner;

class BigInt{
private long x;
public BigInt(){}
public BigInt(long x)
{
this.x = x;
}
void setValue(long x)
{
this.x = x;
}
long getValue()
{
return x;
}
}
public class Main{
Scanner in = new Scanner(System.in);
long a, b, c;
long gcd(long a, long b)
{
if(b == )
{
return a;
}
return gcd(b, a % b);
}
long extgcd(long a, long b, BigInt x, BigInt y)
{
long d = a;
if(b != )
{
d = extgcd(b, a % b, y, x);
y.setValue(y.getValue() - a / b * x.getValue());
}
else
{
x.setValue();
y.setValue();
}
return d;
}
public Main()
{
while(in.hasNext())
{
a = in.nextLong();
b = in.nextLong();
c = in.nextLong();
if(a + b + c == ) break;
long gcd = gcd(a, b);
a /= gcd;
b /= gcd;
c /= gcd;
BigInt x = new BigInt(), y = new BigInt();
extgcd(a, b, x, y);
x.setValue(c * x.getValue());
y.setValue(c * y.getValue()); long x1 = x.getValue();
long y1 = y.getValue();
x1 = (x1 % b + b) % b;
y1 = (c - x1 * a) / b;
if(y1 < ) y1 = -y1; long x2 = x.getValue();
long y2 = y.getValue();
y2 = (y2 % a + a) % a;
x2 = (c - y2 * b) / a;
if(x2 < ) x2 = -x2;
if(x1 + y1 < x2 + y2)
{
System.out.println(x1 + " " + y1);
}
else
{
System.out.println(x2 + " " + y2);
}
}
}
public static void main(String[] args){ new Main();
}
}

POJ2142(扩展欧几里得)的更多相关文章

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

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

  3. UVA 10090 Marbles 扩展欧几里得

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

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

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

  5. 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】

    Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...

  6. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  7. poj 2142 扩展欧几里得解ax+by=c

    原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...

  8. poj 1061 扩展欧几里得解同余方程(求最小非负整数解)

    题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...

  9. Codeforces7C 扩展欧几里得

    Line Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

随机推荐

  1. 项目中如何使用EF

    本文将在技术层面挑战园子里的权威大牛们,言语不敬之处敬请包涵.本文旨为技术交流,欢迎拍砖. 园子里面分享和推荐Entity Framework(以下简称EF)的Repository(仓储)设计模式的文 ...

  2. MFC--根据串口采集的数据借助GDI绘制曲线

    根据采集到的数据绘制曲线 在串口编程中会涉及到这样一个问题,就是将采集到的数据以曲线的形式展示出来,大家自然而然会想到采用方便快捷的控件进行编程.编程周期短,完成任务快,但是真实情况来看,控件会实现很 ...

  3. redis:php-redis中有序集合 zset的使用

    ZSET(stored set) 和 set 一样是字符串的集合,不同的是每个元素都会关联一个 double 类型的 score .实现使用的是 skip list 和 hash table , sk ...

  4. python中的mysql操作

    一. 数据库在自动化测试中的应用 存测试数据 有的时候大批量的数据,我们需要存到数据库中,在测试的时候才能用到,测试的时候就从数据库中读取出来.这点是非常重要的! 存测试结果 二. python中的数 ...

  5. 最大流EK算法/DINIC算法学习

    之前一直觉得很难,没学过网络流,毕竟是基础知识现在重新来看. 定义一下网络流问题,就是在一幅有向图中,每条边有两个属性,一个是cap表示容量,一个是flow 表示流过的流量.我们要求解的问题就是从S点 ...

  6. day5-shelve模块

    一.概述 前面章节我们讲述了json和pickle模块的序列化和反序列化处理,他们有一个不足是在python 3中不能多次dump和load,shelve模块则可以规避这个问题.shelve模块是一个 ...

  7. 【51nod-1605】棋盘问题

    上帝创造了一个n*m棋盘,每一个格子都只有可能是黑色或者白色的. 亚当和夏娃在玩一个游戏,每次寻找边长为x的正方形,其中每个格子必须为黑色,然后将这些格子染白. 如果谁不能操作了,那么那个人就输了. ...

  8. 【poj2553】The Bottom of a Graph(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出.sink点是指该点能到达的点反过来又能回到该点. [思路] ...

  9. Node.js/Python爬取网上漫画

    某个周日晚上偶然发现了<火星异种>这部漫画,便在网上在线看了起来.在看的过程中图片加载很慢,而且有时候还不小心点到广告,大大延缓了我看的进度.后来想到能不能把先把漫画全部抓取到本地再去看. ...

  10. H264的编解码流程?