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

给个a,b,c,求ax+by==c,并且输出|a|+|b|最小的方案,如果|a|+|b|相同者输出|ax|+|by|最小的方案

先解出个可行解,然后调整x至x是最小正数,y跟着动,用这个更新下答案。

然后调x到x是最小负数,更新答案

还有y是最小正数、最小负数的情况,更新答案

 Select Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
#define int long long
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
LL a,b,c;
inline int exgcd(int a,int b,int &x,int &y)
{
if (!b){x=;y=;return a;}
int gcd=exgcd(b,a%b,x,y);
int t=x;x=y;y=t-a/b*y;
return gcd;
}
inline LL LLabs(LL x){return x<?-x:x;}
inline void work()
{
LL x,y,ans1=,ans2=;
int tt=exgcd(a,b,x,y);
if (c%tt!=)return;
x=x*c/tt;y=y*c/tt;
int aa=a/tt,bb=b/tt;
int d=(x-(x%bb+bb)%bb)/bb;
x-=d*bb;y+=d*aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
x-=bb;y+=aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
d=(y-(y%aa+aa)%aa)/aa;
x+=d*bb;y-=d*aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
x+=bb;y-=aa;
if (LLabs(x)+LLabs(y)<ans1+ans2||(LLabs(x)+LLabs(y)==ans1+ans2&&LLabs(x)*a+LLabs(y)*b<=ans1*a+ans2*b))ans1=LLabs(x),ans2=LLabs(y);
printf("%I64d %I64d\n",ans1,ans2);
}
main()
{
while (~scanf("%I64d%I64d%I64d",&a,&b,&c)&&a+b+c)work();
}

poj 2142

[暑假集训--数论]poj2142 The Balance的更多相关文章

  1. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  2. [暑假集训--数论]hdu1019 Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  3. [暑假集训--数论]poj2115 C Looooops

    A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...

  4. [暑假集训--数论]poj1365 Prime Land

    Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...

  5. [暑假集训--数论]poj2034 Anti-prime Sequences

    Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...

  6. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  7. [暑假集训--数论]poj2262 Goldbach's Conjecture

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...

  8. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  9. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

随机推荐

  1. Oracle 分区表的索引、分区索引

    对于分区表,可以建立不分区索引.也就是说表分区,但是索引不分区.以下着重介绍分区表的分区索引. 索引与表一样,也可以分区.索引分为两类:locally partition index(局部分区索引). ...

  2. Java基础面试题:super.getClass().getName() 执行结果是什么?

    package com.swift; import java.util.Date; public class Getclass_Test extends Date { public static vo ...

  3. Vue3.0脚手架搭建

    https://www.jianshu.com/p/fbcad30031c2 vue3.0官网:https://cli.vuejs.org/zh/guide/ 介绍: notice: 这份文档是对应 ...

  4. Oracle数据库学习(三)

    6.关于null 数据库中null是一个未知数,没有任何值:进行运算时使用nvl,但是结果仍为空:在聚集函数中只有全部记录为空才会返回null. 7.insert插入 (1)单行记录插入 insert ...

  5. 网络流_spfa最小费用最大流

    最大流: 不断搜索增广路,寻找最小的容量-流量,得到最大流量,但最大流量在有花费时不一定是最小花费. 最小费用最大流 算法思想: 采用贪心的思想,每次找到一条从源点到达汇点的花费最小的路径,增加流量, ...

  6. js操作地址栏

    //判断地址里是否有?号,如果没有就从最后一个/截到最后,如果有?就从最后一个/截至?号处 listTable.url = location.href.lastIndexOf("?" ...

  7. Python9-MySQL-MySQL存储过程-视图-触发器-函数-day45

    视图:某个查询语句设置别名,日后方便使用 CREATE VIEW v1 as SELECT * FROM student WHERE sid >10 -创建: create view 视图名称 ...

  8. python学习——StringIO和BytesIO

    StringIO 很多时候,数据读写不一定是文件,也可以在内存中读写. StringIO顾名思义就是在内存中读写str. 要把str写入StringIO,我们需要先创建一个StringIO,然后,像文 ...

  9. 连续小波变换CWT(2)

    如果让你说说连续小波变换最大的特点是什么?多分辨分析肯定是标准答案.所谓多分辨分析即是指小波在不同频率段会有不同的分辨率.具体表现形式,我们回到前一篇文章的第一个图, 图一 对应的信号为 低频时(频率 ...

  10. 【Word Ladder II】cpp

    题目: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) ...