The Balance POJ 2142 扩展欧几里得
Description
You are asked to help her by calculating how many weights are required.

Input
The end of the input is indicated by a line containing three zeros separated by a space. It is not a dataset.
Output
- 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 扩展欧几里得的更多相关文章
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- poj 1061 扩展欧几里得解同余方程(求最小非负整数解)
题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...
- poj 1061(扩展欧几里得定理求不定方程)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...
- poj 2115 扩展欧几里得
题目链接:http://poj.org/problem?id=2115 题意: 给出一段循环程序,循环体变量初始值为 a,结束不等于 b ,步长为 c,看要循环多少次,其中运算限制在 k位:死循环输出 ...
- poj 2142 拓展欧几里得
#include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...
- POJ 1061 扩展欧几里得
#include<stdio.h> #include<string.h> typedef long long ll; void gcd(ll a,ll b,ll& d, ...
- 扩展欧几里得(E - The Balance POJ - 2142 )
题目链接:https://cn.vjudge.net/contest/276376#problem/E 题目大意:给你n,m,k,n,m代表当前由于无限个质量为n,m的砝码.然后当前有一个秤,你可以通 ...
- POJ - 2142 The Balance(扩展欧几里得求解不定方程)
d.用2种砝码,质量分别为a和b,称出质量为d的物品.求所用的砝码总数量最小(x+y最小),并且总质量最小(ax+by最小). s.扩展欧几里得求解不定方程. 设ax+by=d. 题意说不定方程一定有 ...
随机推荐
- 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
- 如何上传文件到 github
1. 下载并安装 Git 2. 打开命令窗口:Git Bash Here 3. 在 github.coding 等上新建项目,并复制地址,如:https://git.coding.net/wangzh ...
- 转发:吐血总结,彻底明白 python3 编码原理
吐血总结,彻底明白 python3 编码原理 写的不错,转发学习一下,侵删.. 原文地址https://zhuanlan.zhihu.com/p/40834093 防止原文看不到了 这里粘贴复制一下: ...
- poj2289 Jamie's Contact Groups
思路: 二分+最大流.实现: #include <stdio.h> #include <stdlib.h> #include <limits.h> #include ...
- iframe子页面让父页面跳转 parent.location.href
if ($roleNum < 9) { echo "<script > parent.location.href='admin_login.php' </script ...
- jquery.ajax之beforeSend方法使用介绍
常见的一种效果,在用ajax请求时,没有返回前会出现前出现一个转动的loading小图标或者“内容加载中..”,用来告知用户正在请求数据.这个就可以用beforeSend方法来实现. 下载demo:a ...
- (转)全文检索技术学习(一)——Lucene的介绍
http://blog.csdn.net/yerenyuan_pku/article/details/72582979 本文我将为大家讲解全文检索技术——Lucene,现在这个技术用到的比较多,我觉得 ...
- java 动态代理 和动态编程
概述 代理分两种技术,一种是jdk代理(机制就是反射,只对接口操作),一种就是字节码操作技术.前者不能算技术,后者算是新的技术.未来将有大的动作或者较为广泛的应用和变革,它可以实现代码自我的编码(人工 ...
- PIE SDK 监督分类对话框类(SupervisedClassificaitonDialog)使用经验
最近研究遥感,用到分类算法,PIE SDK正好提供了一些方法可供调用,他们的官方博客上也有相应的示例代码(可参考:https://www.cnblogs.com/PIESat/p/10725270.h ...
- eclipse 中常用快捷键
* 字母大小写转换 ctrl+shift+x 转为大写 ctrl+shift+y 转为小写 * eclipse 自动生成对象来接收方法的返回值的快捷键 说明:光标一定要定位到要自动生成返回值对 ...