Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8816   Accepted: 3833

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

Source

    

  给出两种砝码质量为a,b,问能不能测出质量c的东西,求最小的砝码数量,如果有多个方案考虑总质量最小的方案。

  a*x+b*y=c ,求满足方程的 abs(x)+abs(y)的最小值。做两次exgcd取一个最优的。一次让x为最小正整数,一次让y为最小正整数。

(但我总觉得应该让求出来的解在减去一个d'/d看会不会更优,这里没进行这一步但是A了。

 #include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define LL long long
#define mp make_pair
#define pb push_back
#define inf 0x3f3f3f3f
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){d=a;x=;y=;}
else{exgcd(b,a%b,d,y,x);y-=(a/b)*x;}
}
int main(){
LL a,b,c,d,x1,x2,y1,y2;
while(cin>>a>>b>>c&&(a||b||c)){
exgcd(a,b,d,x1,y1);
exgcd(b,a,d,x2,y2);
if(c%d){
puts("no solution");
}
else{
LL d1=b/d,d2=a/d;
x1=x1*c/d,y1=y1*c/d;
x2=x2*c/d,y2=y2*c/d;
x1=(x1%d1+d1)%d1,y1=(c-a*x1)/b;
x2=(x2%d2+d2)%d2,y2=(c-b*x2)/a;
x1=fabs(x1),y1=fabs(y1);
x2=fabs(x2),y2=fabs(y2);
if(x1+y1<x2+y2) cout<<x1<<' '<<y1<<endl;
else if(x1+y1>x2+y2) cout<<y2<<' '<<x2<<endl;
else{
if(x1*a+y1*b<x2*a+y2*b) cout<<x1<<' '<<y1<<endl;
else cout<<y2<<' '<<x2<<endl;
}
}
}
return ;
}

poj-2142-exgcd/解的和最小的更多相关文章

  1. POJ.2142 The Balance (拓展欧几里得)

    POJ.2142 The Balance (拓展欧几里得) 题意分析 现有2种质量为a克与b克的砝码,求最少 分别用多少个(同时总质量也最小)砝码,使得能称出c克的物品. 设两种砝码分别有x个与y个, ...

  2. POJ 2142 The Balance【扩展欧几里德】

    题意:有两种类型的砝码,每种的砝码质量a和b给你,现在要求称出质量为c的物品,要求a的数量x和b的数量y最小,以及x+y的值最小. 用扩展欧几里德求ax+by=c,求出ax+by=1的一组通解,求出当 ...

  3. poj 2195 二分图带权匹配+最小费用最大流

    题意:有一个矩阵,某些格有人,某些格有房子,每个人可以上下左右移动,问给每个人进一个房子,所有人需要走的距离之和最小是多少. 貌似以前见过很多这样类似的题,都不会,现在知道是用KM算法做了 KM算法目 ...

  4. POJ 2142 - The Balance [ 扩展欧几里得 ]

    题意: 给定 a b n找到满足ax+by=n 的x,y 令|x|+|y|最小(等时令a|x|+b|y|最小) 分析: 算法一定是扩展欧几里得. 最小的时候一定是 x 是最小正值 或者 y 是最小正值 ...

  5. exgcd 解同余方程ax=b(%n)

    ax=n(%b)  ->   ax+by=n 方程有解当且仅当 gcd(a,b) | n ( n是gcd(a,b)的倍数 ) exgcd解得 a*x0+b*y0=gcd(a,b) 记k=n/gc ...

  6. POJ 2142 The Balance (解不定方程,找最小值)

    这题实际解不定方程:ax+by=c只不过题目要求我们解出的x和y 满足|x|+|y|最小,当|x|+|y|相同时,满足|ax|+|by|最小.首先用扩展欧几里德,很容易得出x和y的解.一开始不妨令a& ...

  7. POJ 2142 The Balance(exgcd)

    嗯... 题目链接:http://poj.org/problem?id=2142 AC代码: #include<cstdio> #include<iostream> using ...

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

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

  9. POJ 2891 Strange Way to Express Integers | exGcd解同余方程组

    题面就是让你解同余方程组(模数不互质) 题解: 先考虑一下两个方程 x=r1 mod(m1) x=r2 mod (m2) 去掉mod x=r1+m1y1   ......1 x=r2+m2y2   . ...

  10. POJ 2142 The balance | EXGCD

    题目: 求ax+by=c的一组解,使得abs(x)+abs(y)尽量小,满足前面前提下abs(ax)+abs(by)尽量小 题解: exgcd之后,分别求出让x尽量小和y尽量小的解,取min即可 #i ...

随机推荐

  1. HihoCoder 1195 高斯消元·一(高斯消元)

    题意 https://hihocoder.com/problemset/problem/1195 思路 高斯消元是解决高元方程的一种算法,复杂度 \(O(n^3)\) . 过程大致是: 构造一个未知数 ...

  2. Bytom交易说明(UTXO用户自己管理模式)

    比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom 该部分主 ...

  3. lvs笔记

    LVS是Linux Virtual Server的简写,意为Linux虚拟服务器,是虚拟的服务器集群系统,可在UNIX/LINUX平台下实现负载均衡集群功能.该项目在1998年5月由章文嵩博士组织成立 ...

  4. Twitter OAuth

    新浪微博和 Twitter 的 Oauth API 为什么感觉流程不一样 新浪: 开发者引导用户到新浪授权页面, 页面链接中需要带上自己的 apikey : 用户授权后新浪跳转到开发者指定指定的页面, ...

  5. ElasticSearch——日志工具

    Elasticsearch: 权威指南 官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.ht ...

  6. System.ServiceProcess与System.Configuration.Install命名空间的介绍

    System.ServiceProcess 命名空间提供用于实现.安装和控制 Windows 服务应用程序的类.服务是长期运行的可执行文件,其运行没有用户界面 System.ServiceProces ...

  7. 如何快速获取properties中的配置属性值

    本文为博主原创,未经博主允许,不得转载: 在项目中,经常需要将一些配置的常量信息放到properties文件中,这样在项目的配置变动的时候,只需要修改配置文件中 对应的配置常量即可. 在项目应用中,如 ...

  8. JavaScript中的方法事件和函数的方法的三种方法

    js中的很多事件  而事件相对应的就是方法(函数 )那么今天所说的就是这三种方法      已onclick事件为例 1: 基本方法 <div id="a" onclick= ...

  9. python网络编程基础之socket粘包现象

    粘包现象两种 登陆 #服务端import json import socket server=socket.socket()#创建socket对象 ip_port=('127.0.0.1',8001) ...

  10. PL/SQL Developer安装配置

    选择tool下的perferences 修改下面标记的内容即可