One Person Game


Time Limit: 2 Seconds      Memory Limit: 65536 KB

There is an interesting and simple one person game. Suppose there is a number axis under your feet. You are at point A at first and your aim is point B. There are 6 kinds of operations you can perform in one step. That is to go left or right by a,b and c, here c always equals to a+b.

You must arrive B as soon as possible. Please calculate the minimum number of steps.

Input

There are multiple test cases. The first line of input is an integer T(0 < T ≤ 1000) indicates the number of test cases. Then T test cases follow. Each test case is represented by a line containing four integers 4 integers A, B, a and b, separated by spaces. (-231A, B < 231, 0 < a, b < 231)

Output

For each test case, output the minimum number of steps. If it's impossible to reach point B, output "-1" instead.

Sample Input

2
0 1 1 2
0 1 2 4

Sample Output

1
-1

题解:不难列出线性方程a(x+z)+b(y+z)=B-A;即ax+by=C;

主要是中间找最小步数;//由于a+b可以用c来代替;所以,当x和y同号时, 34 //就可以用z=min(x,y)+max(x,y)-min(x,y)=max(x,y)来走,也就是一部分步数可以等于a+b 35 //所以还要找这种情况的步数。。。 36 //因为x和y越接近,(a+b)*step的越多,越优化, 37 //所以要在通解相交的点周围找;由于交点可能为小数,所以才在周围找的;

代码:

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
typedef long long LL;
const int INF=0x3fffffff;
void e_gcd(LL a,LL b,LL &d,LL &x,LL &y){
if(!b){
d=a;
x=;
y=;
}
else{
e_gcd(b,a%b,d,x,y);
LL temp=x;
x=y;
y=temp-a/b*y;
}
}
LL cal(LL a,LL b,LL c){
LL x,y,d;
e_gcd(a,b,d,x,y);
//printf("%lld %lld %lld\n",d,x,y);
if(c%d!=)return -;
x*=c/d;
y*=c/d;
//x=x0+b/d*t;y=y0-a/d*t;
b/=d;a/=d;
//由于a+b可以用c来代替;所以,当x和y同号时,
//就可以用z=min(x,y)+max(x,y)-min(x,y)=max(x,y)来走,也就是一部分步数可以等于a+b
//所以还要找这种情况的步数。。。
//因为x和y越接近,(a+b)*step的越多,越优化,
//所以要在通解相交的点周围找;由于交点可能为小数,所以才在周围找的;
LL mid=(y-x)/(a+b); // x0+bt=y0-at;
LL ans=(LL)INF*(LL)INF;
LL temp;
// printf("%lld\n",ans);
for(LL t=mid-;t<=mid+;t++){
if(abs(x+b*t)+abs(y-a*t)==abs(x+b*t+y-a*t))
temp=max(abs(x+b*t),abs(y-a*t));
else temp=abs(x+b*t)+abs(y-a*t);
ans=min(ans,temp);
// printf("%lld\n",temp);
}
return ans;
}
int main(){
LL T,A,B,a,b;
scanf("%lld",&T);
while(T--){
scanf("%lld%lld%lld%lld",&A,&B,&a,&b);
LL ans=cal(a,b,B-A);
printf("%lld\n",ans);
}
return ;
}

One Person Game(扩展欧几里德求最小步数)的更多相关文章

  1. POJ-1061青蛙的约会,扩展欧几里德求逆元!

                                                               青蛙的约会 以前不止一次看过这个题,但都没有去补..好吧,现在慢慢来做. 友情提示 ...

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

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

  3. POJ 1753 Flip Game (高斯消元 枚举自由变元求最小步数)

    题目链接 题意:4*4的黑白棋,求把棋全变白或者全变黑的最小步数. 分析:以前用状态压缩做过. 和上题差不多,唯一的不同是这个终态是黑棋或者白棋, 但是只需要把给的初态做不同的两次处理就行了. 感觉现 ...

  4. CodeForces 146E - Lucky Subsequence DP+扩展欧几里德求逆元

    题意: 一个数只含有4,7就是lucky数...现在有一串长度为n的数...问这列数有多少个长度为k子串..这些子串不含两个相同的lucky数... 子串的定义..是从这列数中选出的数..只要序号不同 ...

  5. 公钥密码之RSA密码算法扩展欧几里德求逆元!!

    扩展欧几里得求逆元 实话说这个算法如果手推的话问题不大,无非就是辗转相除法的逆过程,还有一种就是利用扩展欧几里德算法,学信安数学基础的时候问题不大,但现在几乎都忘了,刷题的时候也是用kuangbin博 ...

  6. HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    M ...

  7. POJ 3185 The Water Bowls (高斯消元 求最小步数)

    题目链接 题意:有20个数字,0或1.如果改变一个数的状态,它左右两边的两个数的状态也会变反.问从目标状态到全0,至少需要多少次操作. 分析: 和上一题差不多,但是比上一题还简单,不多说了,但是在做题 ...

  8. poj 3009 冰球 【DFS】求最小步数

    题目链接:https://vjudge.net/problem/POJ-3009 转载于:https://www.cnblogs.com/Ash-ly/p/5728439.html 题目大意: 要求把 ...

  9. 51Nod 3的幂的和(扩展欧几里德求逆元)

    求:3^0 + 3^1 +...+ 3^(N) mod 1000000007 Input 输入一个数N(0 <= N <= 10^9) Output 输出:计算结果 Input示例 3 O ...

随机推荐

  1. JAVA 语 言 如 何 进 行 异 常 处 理 , 关 键 字 : throws,throw,try,catch,final

    throws是获取异常throw是抛出异常try是将会发生异常的语句括起来,从而进行异常的处理,catch是如果有异常就会执行他里面的语句,而finally不论是否有异常都会进行执行的语句.

  2. Chain of Responsibility模式

    熟悉VC/MFC的都知道,VC是“基于消息,事件驱动”,消息在VC开发中起着举足轻重的作用.MFC提供了消息的处理的链式处理策略,处理消息的请求将沿着预定好的路径依次进行处理.消息的发送者并不知道该消 ...

  3. STL模板_智能指针概念

    一.智能指针1.类类型对象,在其内部封装了一个普通指针.当智能指针对象因离开作用域而被析构时,其析构函数被执行,通过其内部封装的普通指针,销毁该指针的目标对象,避免内存泄露.2.为了表现出和普通指针一 ...

  4. ROS使用rqt_console

    打开一个新的终端在里面输入: sudo apt-get install ros-hydro-rqt ros-hydro-rqt-common-plugins ros-hydro-turtlesim 安 ...

  5. javascript函数apply和call

    apply:方法能劫持另外一个对象的方法,继承另外一个对象的属性. Function.apply(obj,args)方法能接收两个参数obj:这个对象将代替Function类里this对象args:这 ...

  6. CI(-)框架结构

    一 CI 是什么 CodeIgniter is an Application Development Framework - a toolkit - for people who build web ...

  7. 各种排序算法(C语言)

    #include <stdlib.h> #include <stdio.h> void DataSwap(int* data1, int* data2) { int temp ...

  8. Linux发展历史大事编年表(截止2013年)

    这篇文章主要介绍了Linux发展历史大事编年表(截止2013年),Linux现在已经无处不在,是一个伟大的开原项目,让我一起来看看23年来它的发展历程吧   我们周围到处都有Linux的身影,在家中. ...

  9. Apache服务无法启动的解决方法

    apache服务无法启动的解决方法 在配置apache的时候,把apache安装为服务myweb,用apacheMonitor启动myweb发现无法启动,提示:the requested operat ...

  10. Spring Boot MyBatis 连接数据库

    最近比较忙,没来得及抽时间把MyBatis的集成发出来,其实mybatis官网在2015年11月底就已经发布了对SpringBoot集成的Release版本,Github上有代码:https://gi ...