题意:一个人要从A走到B  只能走a布、b步、(a+b)步,可以往左或右走

     问 最少要走几步,不能走到的输出-1

可以列出方程 $ax+by=A-B$

或者     $ax+(a+b)y=A-B$

或者     $bx+(a+b)y=A-B$

要取这三个方程的最小的$(x+y)$

根据$ax+by=gcd(a, b) $

当$A-B$不是$gcd$的倍数时 就不能走到

利用ex_gcd可以分别求出这三个方程的解,但求出的这组并非最小的

因此利用枚举斜率 得到的交点为最小的一组解

 LL exgcd(LL a,LL b,LL &x,LL &y)
{
LL d=a;
if(b!=)
{
d=exgcd(b,a%b,y,x);
y-=(a/b)*x;
}
else x=,y=;
return d;
}
LL Abs(LL x)
{
return x<? -x:x;
}
LL A;
LL gao(LL a, LL b)
{
LL x, y;
LL g=exgcd(a, b, x, y);
x=x*(A/g), y=y*(A/g);
a/=g, b/=g; LL ans=Abs(x)+Abs(y);
for(int i=-;i<;i++)
ans=min(ans, Abs(x+(-x/b+i)*b)+Abs(y-(-x/b+i)*a));
for(int i=-;i<;i++)
ans=min(ans, Abs(x+(y/a+i)*b)+Abs(y-(y/a+i)*a));
return ans;
}
int main()
{
int t;
LL B,a,b;
cin>>t;
while(t--)
{
cin>>A>>B>>a>>b;
A=Abs(A-B);
if(!A)
{
puts("");
continue;
}
if(A%__gcd(a, b))
{
puts("-1");
continue;
}
cout<<min(gao(a, b), min(gao(a+b, a), gao(a+b, b)))<<endl;
}
return ;
}

ZOJ 3593

[数论]ZOJ3593 One Person Game的更多相关文章

  1. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  2. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  3. 数论学习笔记之解线性方程 a*x + b*y = gcd(a,b)

    ~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...

  4. hdu 1299 Diophantus of Alexandria (数论)

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  6. bzoj2219: 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  7. hdu5072 Coprime (2014鞍山区域赛C题)(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...

  8. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

  9. 数论初步(费马小定理) - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

随机推荐

  1. UITableView的简单使用

    UITableView分为两种style:UITableViewStyleGrouped和UITableViewStylePlain. (一)UITableViewStyleGrouped #impo ...

  2. AngularJS开发相关配置

    安装步骤: 1. Node.js,下载地址:https://nodejs.org/en/ 2. Git 下载地址:https://git-scm.com/download/ 3. Python (需为 ...

  3. 坑爹系列:sizeof运算符

    C语言里的sizeof关键字用于返回变量的类型宽度(变量所占的字节个数).例如: #include <stdio.h> int main() { int i = 0; int size = ...

  4. must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)问题

    依照视屏编写代码如下 class MyButtonListener implements OnClickListener{ @Override public void onClick(View v){ ...

  5. java concurrent包的学习(转)

    java concurrent包的学习(转) http://my.oschina.net/adwangxiao/blog/110188 我们都知道,在JDK1.5之前,Java中要进行业务并发时,通常 ...

  6. 《疯狂的android讲义第3版》读书笔记

    第一章.开始启程,你的第一行android代码 1.android系统架构: 1)linux内核层:为底层硬件提供驱动,如显示驱动.音频驱动.照相机驱动.蓝牙驱动.Wifi驱动.电源管理等 2)系统运 ...

  7. 从零学起PHP

    数据库连接conn.php <?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root", ...

  8. Python数据结构——二叉树的实现

    1. 二叉树 二叉树(binary tree)中的每个节点都不能有多于两个的儿子. 1.1 二叉树列表实现 如上图的二叉树可用列表表示: tree=['A', #root ['B', #左子树 ['D ...

  9. 扩展ServiceHost<T>类

    public class ServiceHost<T> : ServiceHost { public void EnableMetadataExchange(bool enableHttp ...

  10. javascript高级编程笔记04(基本概念)

    Function类型 Es5中规范了另一个函数对象的属性:caller,这个属性中保存着调用当前函数的函数的引用,如果是在全局作用域中调用当前函数,这它的值为null function outer() ...