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. Android -- SQLite 数据库创建,增删改查,事务处理

    1. 概述 在Android平台上,集成了一个嵌入式关系型数据库-SQLite,SQLite3支持 NULL.INTEGER.REAL(浮点数字).TEXT(字符串文本)和BLOB(二进制对象)数据类 ...

  2. Vue初步认识

    什么是Vue Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架(根据需求使用特定的功 能).与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue 的 ...

  3. 提升 CSS 选择器性能的方法

    CSS 选择器性能损耗来自? CSS选择器对性能的影响源于浏览器匹配选择器和文档元素时所消耗的时间,所以优化选择器的原则是应尽量避免使用消耗更多匹配时间的选择器.而在这之前我们需要了解CSS选择器匹配 ...

  4. Linux 任务控制(bg job fg nohup &) (转)

    常用命令 & 将指令丢到后台中去执行[ctrl]+z 將前台任务丟到后台中暂停jobs 查看后台的工作状态fg %jobnumber 将后台的任务拿到前台来处理bg %jobnumber 将任 ...

  5. Django框架(二)

    一:Django项目创建步骤: 方式1:命令创建: 进入指定目录 C:\Users\bing>F: F:\>cd mysite F:\mysite>django-admin star ...

  6. Four-operations: 使用node.js实现四则运算程序

    一. 项目基本信息 项目成员: 陈旭钦, 郭鹏燕 项目仓库: https://github.com/Yanzery/Four-operations 二. PSP2.1表格 PSP2.1 Persona ...

  7. vue: data binding

    1.文本 第一种“Mustache” 语法(双大括号)写法第二种 用v-text的指今写法第三种和第四是对es6写法的拓展写法,称模板字符串 <template> <div> ...

  8. 【CSAPP】三、程序的机器级表示

    本章基于两种相关的机器语言:Intel IA32和x86-64,前者注重32位,后者注重64位. 本章脉络:c\汇编\机器码之间的关系,数据的表示,控制结构如何实现.运行栈,局部变量的存储,数据结构. ...

  9. SVN的搭建及使用(三)用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突,重新设置用户名和密码等

    添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态,如图: 这时候,你需要告知TortoiseSVN你的操作,如图: 加入以后,你的文件会变 ...

  10. 纯css实现Magicline Navigation(下划线动画导航菜单)

    看别人网站的时候,看到一种导航菜单的动画,觉得很有意思,就仔细研究起来. 目前见过的动画有三种:水平下划线动画导航.水平背景动画导航.垂直动画导航,他们实现思路都是一样的,都是依赖 css3的同级通用 ...