The Balance POJ 2142 扩展欧几里得

时间:2022-09-19 08:19:44

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. 
The Balance POJ 2142 扩展欧几里得

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<set>
#include<map>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
typedef long long LL;
/*
a*x = b*y + d
a*x + b*y = d
|x|+|y| 的最小值
|x|+|y|==|x0+b/d*t|+|y0-a/d*t|,我们规定 a>b(如果 a<b,我们便交换 a b),从这个式子中,
我们可以轻易的发现:|x0+b/d*t|是单调递增的,|y0-a/d*t|是单调递减的,而由于我们规定了 a>b,
那么减的斜率边要大于增的斜率,于是整个函数减少的要比增加的快,但是由于绝对值的符号的作用,
最终函数还是递增的。
也就是说,函数是凹的,先减小,再增大。那么什么时候最小呢?很显然是 y0-a/d*t==0 的时候,
于是我们的最小值|x|+|y|也一定是在 t=y0*d/a附近了,只要在附近枚举几个值就能找到最优解了。
*/
typedef long long LL;
#define INF 0x3f3f3f3f
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(b==)
{
x = ;
y = ;
return a;
}
LL ans = ex_gcd(b,a%b,x,y);
LL tmp = x;
x = y;
y = tmp - a/b*x;
return ans;
}
void cal(LL a,LL b,LL c)//求ax+by==c 的一组|x|+|y| 最小的解
{
bool f = false;
if(a<b)
{
f = true;
swap(a,b);
}
LL x=,y=;
LL gcd = ex_gcd(a,b,x,y);
x *= c/gcd;
y *= c/gcd;
LL t = y*gcd/a,ans=INF;
LL kx,ky;
for(LL i=t-;i<=t+;i++)
{
LL t1 = x + (b/gcd)*i;
LL t2 = y - (a/gcd)*i;
if( abs((long)t1)+abs((long)t2)<ans)
{
ans = abs((long)t1)+abs((long)t2);
kx = (t1>)?t1:-t1;
ky = (t2>)?t2:-t2;
}
}
if(!f)
printf("%lld %lld\n",kx,ky);
else
printf("%lld %lld\n",ky,kx);
}
int main()
{
LL a,b,c;
while(scanf("%lld%lld%lld",&a,&b,&c),a+b+c)
{
cal(a,b,c);
}
}

The Balance POJ 2142 扩展欧几里得的更多相关文章

  1. poj 2142 扩展欧几里得解ax&plus;by&equals;c

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

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

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

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

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

  4. poj 1061&lpar;扩展欧几里得定理求不定方程)

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...

  5. poj 2115 扩展欧几里得

    题目链接:http://poj.org/problem?id=2115 题意: 给出一段循环程序,循环体变量初始值为 a,结束不等于 b ,步长为 c,看要循环多少次,其中运算限制在 k位:死循环输出 ...

  6. poj 2142 拓展欧几里得

    #include <cstdio> #include <algorithm> #include <cstring> #include <iostream&gt ...

  7. POJ 1061 扩展欧几里得

    #include<stdio.h> #include<string.h> typedef long long ll; void gcd(ll a,ll b,ll& d, ...

  8. 扩展欧几里得(E - The Balance POJ - 2142 )

    题目链接:https://cn.vjudge.net/contest/276376#problem/E 题目大意:给你n,m,k,n,m代表当前由于无限个质量为n,m的砝码.然后当前有一个秤,你可以通 ...

  9. POJ - 2142 The Balance(扩展欧几里得求解不定方程)

    d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有 ...

随机推荐

  1. struts深入原理之RequestProcessor与xml

    和配置文件相对应的代码(struts1) public void process(HttpServletRequest request, HttpServletResponse response)   ...

  2. c&num;中的var优缺点和适用场景

    var是c# 3.0新加的特性,叫做隐式类型局部变量,大家都知道c#其实是一种强类型的语言,为什么会引入匿名类型呢? 我猜测是因为linq的原因吧,因为感觉var在linq中被大量使用.下面说下var ...

  3. android漂亮的对话框项目sweet-alert-dialog

      漂亮的对话框 sweet-alert-dialog 项目地址: https://github.com/pedant/sweet-alert-dialog android原生的dialog太生硬了, ...

  4. WIN10系统如何隐藏底部搜索框

    右击任务栏,搜索,可以切换三种模式,建议还是显示搜索图标,因为这个搜索还是能比较快速定位到系统功能的,只不过显示搜索框的话比较占地方,不方便

  5. POD类型

    POD类型 POD全称Plain Old Data.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是一个POD类型. C++11将POD划分为两个基本概念的合集,即:平凡的和标准 ...

  6. 解决shell脚本参数传递含有空格的问题

    有这样一个py文件,需要传一个字典作为参数: import json import sys def parse_params(data): json_data = json.loads(data[1] ...

  7. 114&period; Flatten Binary Tree to Linked List【Medium】【将给定的二叉树转化为&OpenCurlyDoubleQuote;只有右孩子节点”的链表(树)】

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 ...

  8. 善待Erlang 代码 -- 巧用 user&lowbar;default

    这是一篇水文 ----------------------------------------------------- 很好用的一个技巧 http://www.erlang.org/doc/man/ ...

  9. 学习如何用VS2010创建ocx控件

    1参考文章 (1)这一篇将使用vc创建ocx控件:http://blog.csdn.net/jiadelin/article/details/2917225 (2)这一篇文章有关vs2010创建act ...

  10. 微信支付 h5

    Android开发要点说明 商户在微信开放平台申请开发应用后,微信开放平台会生成APP的唯一标识APPID.由于需要保证支付安全,需要在开放平台绑定商户应用包名和应用签名,设置好后才能正常发起支付. ...