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. C学习之指针强化

    char *p = (char *)malloc(100); malloc是用于分配内存的函数,它的参数为int型,表示分配多少个字节长度,其返回类型为void*,在这里用char*就是强制转化,指定 ...

  2. textarea中的空格与换行

    当在一个textarea标签中键入一个回车时,实际上会插入2个符号:\n\r在javascript里, line breaks用\n表示when you pull text into Javascri ...

  3. SICP 习题 (1.13) 解题总结

    SICP习题1.13要求证明Fib(n)是最接近φn/√5 的整数,其中φ=(1+√5)/2 .题目还有一个提示,提示解题者利用归纳法和斐波那契数的定义证明Fib(n)=(φn - ψn) / √5 ...

  4. 机器学习算法实现(R&Python code)

    Machine Learning Algorithms Machine Learning Algorithms (Python and R) 明天考试,今天就来简单写写机器学习的算法 Types Su ...

  5. ActiveMQ的入门demo

    步骤: 1 :下载ActiveMQ 官网:http://activemq.apache.org/ 2 :解压AcitveMQ, 根据自己的操作系统选择运行win64或者win32下的activemq. ...

  6. JQ兼容性问题

    checkbox操作 1:设置为选中状态   $(this).prop("checked", true); 2:判断是否选中     $(this).is(":check ...

  7. JS中的this都有什么作用?

    1.全局代码中的this  是指向全局对象,在浏览器中是window alert(this) //window 2.作为单纯的函数调用: function fooCoder(x) { this.x = ...

  8. Ext Store Proxy Ajax

    使用Store ajax的方式来获取数据 <div id="grid1"> </div> <script> Ext.onReady(functi ...

  9. Git-常用命令集合

    该文章会陆续添加内容,学习网页来自http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 ...

  10. request.getParameterValues与request.getParameter的差别

    一. 简单的对照 request.getParameter用的比較多,相对熟悉 request.getParameterValues(String   name)是获得如checkbox类(名字同样, ...