思路:题意转化为求 (ax+by=dis) || (ax+cy=dis) || (bx+cy=dis) 三个式子有解时的最小|x| + |y|。显然求解特解x,y直接用扩展欧几里得,那么怎么求|x| + |y|?xy关系为一条直线,那么|x| + |y|应该是在x取0或者y取0的时候,但是要整数,所以只能在周围取几个点。我们知道x=x1+b/gcd*t,那么x1+b/gcd*t = 0可以解得 t  = -x1 * gcd / b。然后在附近取几个点。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LS(n) node[(n)].ch[0]
#define RS(n) node[(n)].ch[1]
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = + ; ll ex_gcd(ll a, ll b, ll &x, ll &y){
ll d, t;
if(b == ){
x = ;
y = ;
return a;
}
d = ex_gcd(b, a%b, x, y);
t = x-a/b*y;
x = y;
y = t;
return d;
} ll dis;
//求|x|+|y|最小
ll solve(ll a, ll b){
ll x, y, d = ex_gcd(a, b, x, y);
if(dis % d != ) return INF;
x = x * dis / d;
y = y * dis / d;
a /= d, b /= d;
ll ans = abs(x) + abs(y); ll k;
k = -x / b - ;
for(int i = ; i <= ; i++){
ans = min(ans, abs(x + b * (k + i)) + abs(y - a * (k + i)));
} k = y / a - ;
for(int i = ; i <= ; i++){
ans = min(ans, abs(x + b * (k + i)) + abs(y - a * (k + i)));
} return ans;
}
int main(){
int T;
scanf("%d", &T);
while(T--){
ll a, b, A, B;
scanf("%lld%lld%lld%lld", &A, &B, &a, &b);
dis = abs(A - B);
ll ans;
ans = min(solve(a, a + b), min(solve(a, b), solve(b, a + b)));
printf("%lld\n", ans == INF? - : ans);
}
return ;
}

ZOJ 3593 One Person Game(ExGcd + 最优解)题解的更多相关文章

  1. ZOJ 3593.One Person Game-扩展欧几里得(exgcd)

    智障了,智障了,水一水博客. 本来是个水题,但是for循环遍历那里写挫了... One Person Game Time Limit: 2 Seconds      Memory Limit: 655 ...

  2. ZOJ Monthly, June 2014 月赛BCDEFGH题题解

    比赛链接:点击打开链接 上来先搞了f.c,,然后发现状态不正确,一下午都是脑洞大开,, 无脑wa,无脑ce...一样的错犯2次.. 硬着头皮搞了几发,最后20分钟码了一下G,不知道为什么把1直接当成不 ...

  3. ZOJ 3593 One Person Game(拓展欧几里得求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  4. ZOJ - 3593 One Person Game (扩展欧几里得)

    题意:一个人在坐标A,要前往坐标B的位置.可以往左或往右走a,b,a+b个单位,求到达B的最小步数. 分析:扩展欧几里得算法求解线性方程的套路不变.令C=fabs(A-B),c = a+b, 扩展gc ...

  5. ZOJ 3593 One Person Game

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  6. ZOJ 4027 Sequence Swapping(DP)题解

    题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...

  7. ZOJ 3829 Known Notation(贪心)题解

    题意:给一串字符,问你最少几步能变成后缀表达式.后缀表达式定义为,1 * 1 = 1 1 *,题目所给出的字串不带空格.你可以进行两种操作:加数字,交换任意两个字符. 思路:(不)显然,最终结果数字比 ...

  8. [数论]ZOJ3593 One Person Game

    题意:一个人要从A走到B  只能走a布.b步.(a+b)步,可以往左或右走   问 最少要走几步,不能走到的输出-1 可以列出方程 $ax+by=A-B$ 或者 $ax+(a+b)y=A-B$ 或者 ...

  9. One Person Game(zoj3593+扩展欧几里德)

    One Person Game Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Status ...

随机推荐

  1. 在Windows10中运行debug程序

    下载debug.exe 下载DOSBox 安装DOXBox,尽量不要装在C盘 将debug.exe放到F:/TASM 运行DOSBox.exe,执行 mount c f:\TASM #挂载目录 c: ...

  2. leetCoder-wordBreak判断能否分词

    题目 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determ ...

  3. python 翻译爬虫

    import urllib.request import urllib.parse import json while 1: content=input("请输入要翻译的内容:") ...

  4. C# 调用.bat 提示该命令不是内部命令或外部命令

    前提:双击.bat文件可以执行成功,用C#调用提示该命令不是内部命令或外部命令...... 解决方法:下面代码的红色标注,既要设置.bat文件的文件名FileName,也要设置.bat文件所在的文件夹 ...

  5. eclipse更换workspace需要重新设置的内容

    .jdk Window-->java-->Installed JREs,新增或修改你所需要的jdk版本,点击需要的jdk-->edit 在Default VM arguments里面 ...

  6. MVC中的Ajax与增删改查(一)

    自入手新项目以来,一直处于加班状态,博客也有两周没更,刚刚完成项目的两个模组,稍有喘息之机,写写关于项目中 的增删改查,这算是一个老生常谈的问题了,就连基本的教材书上都有.刚看书的时候,以为 没什么可 ...

  7. SLAM学习笔记 - 世界坐标系到相机坐标系的变换

    参考自: http://blog.csdn.net/yangdashi888/article/details/51356385 http://blog.csdn.net/li_007/article/ ...

  8. 基于word2vec训练词向量(一)

    转自:https://blog.csdn.net/fendouaini/article/details/79905328 1.回顾DNN训练词向量 上次说到了通过DNN模型训练词获得词向量,这次来讲解 ...

  9. Presto 学习参考资料

    Presto 文档资料: 0.1版:Presto 0.100 Documentation 0.213版:Presto 0.213 Documentation 阿里云 presto 学习资料:https ...

  10. Radio中REG

    Auto REG/REG OFF在广播接收质量不好时,收音机首先仅调整到该广播电台当前发射的可选频率.但是,如果接收质量差到“该发射电台濒临消失”的程度,则收音机也会接收德国NDR1(北德意志广播电台 ...