One person Game (expand Euclid to find the minimum number of steps)

Source: Internet
Author: User

One person Game Time limit: 2 Seconds Memory Limit: 65536 KB

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

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

Input

There is multiple test cases. The first line of input was an integer T(0 < T ≤1000) indicates the number of test cases. Then T test Cases follow. 4 integers a, B, a and b c6>, separated by spaces. ( -231≤ a, 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
20 1 1 20 1 2 4
Sample Output
1-1

Problem: It is not difficult to list the linear equation A (x+z) +b (y+z) =b-a, namely Ax+by=c;

Mainly in the middle to find the minimum number of steps;// since a+b can be substituted with C, so when X and Y are the same number, the Z=min (x, y) +max (x, y)-min (x, y) =max can be used to walk, That is, a step number can be equal to a+b // so the number of steps to find this situation ... a+b//Because the closer the x and Y, the more the *step, the more optimized, the PNS // so to be found around the point where the general solution intersects; Because the intersection may be a decimal, it is looked around;

Code:

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <vector>7 #defineMem (x, y) memset (x,y,sizeof (x))8 using namespacestd;9typedefLong LongLL;Ten Const intinf=0x3fffffff; One voidE_GCD (LL a,ll b,ll &d,ll &x,ll &y) { A     if(!b) { -D=A; -x=1; they=0; -     } -     Else{ -E_GCD (b,a%b,d,x,y); +LL temp=x; -x=y; +y=temp-a/b*y; A     } at } - 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!=0)return-1; inx*=c/D; -y*=c/D; to      //x=x0+b/d*t;y=y0-a/d*t; +B/=d;a/=D; -      //since a+b can be substituted with C, so when X and Y are the same number, the      //You can use Z=min (x, y) +max (x, y)-min (x, y) =max (x, y) to walk, that is, a step number can be equal to A+b *      //so we have to find the number of steps in this situation ...  $      //because the closer the X and Y, the more The (a+b) *step, the more optimized,Panax Notoginseng      //so look around at the point where the general solution intersects, because the intersection may be a decimal, so it's looking around . -LL mid= (y-x)/(A+B);//X0+bt=y0-at; thell ans= (LL) inf*(LL) INF; + LL temp; A     //printf ("%lld\n", ans); the       for(LL t=mid-1; t<=mid+1; 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)); $          ElseTemp=abs (x+b*t) +abs (y-a*t); $ans=min (ans,temp); -      //printf ("%lld\n", temp); -      } the      returnans; - }Wuyi intMain () { the LL t,a,b,a,b; -scanf"%lld",&T); Wu      while(t--){ -scanf"%lld%lld%lld%lld",&a,&b,&a,&b); AboutLL ans=cal (a,b,b-A); $printf"%lld\n", ans); -     } -     return 0; -}

One person Game (expand Euclid to find the minimum number of steps)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.