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 toa+b.
You must arrive B as soon as possible. Please calculate the minimum number of steps.
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. (-231 ≤ A, B < 231, 0 < a, b < 231)
For each test case, output the minimum number of steps. If it's impossible to reach point B, output "-1" instead.
2 0 1 1 2 0 1 2 4
1 -1
1 #include<stdio.h> 2 #include<math.h> 3 #include<algorithm> 4 #define LL long long 5 using namespace std; 6 7 LL ans; 8 void exgcd(LL a,LL b,LL& d,LL& x,LL& y) 9 { 10 if(!b){d=a;x=1;y=0;} 11 else 12 { 13 exgcd(b,a%b,d,y,x); 14 y-=x*(a/b); 15 } 16 } 17 LL China(LL a,LL b,LL c) 18 { 19 LL x,y,d,bm,am; 20 21 exgcd(a,b,d,x,y); 22 if(c%d) return -1; 23 bm=b/d; 24 am=a/d; 25 x=x*c/d; 26 y=y*c/d; 27 28 29 LL sum=fabs(x)+fabs(y); 30 31 for(int i=-x/bm-1;i<=-x/bm+1;i++) 32 { 33 LL X=x+bm*i; 34 LL Y=y-am*i; 35 if(i) 36 { 37 LL tmp=fabs(X)+fabs(Y); 38 if(tmp<sum) sum=tmp; 39 } 40 } 41 for(int i=y/am-1;i<=y/am+1;i++) 42 { 43 LL X=x+bm*i; 44 LL Y=y-am*i; 45 if(i) 46 { 47 LL tmp=fabs(X)+fabs(Y); 48 if(tmp<sum) sum=tmp; 49 } 50 } 51 return sum; 52 } 53 54 55 int main() 56 { 57 int T; 58 LL A,B,C,a,b,c,d,x,y; 59 scanf("%d",&T); 60 while(T--) 61 { 62 scanf("%lld%lld%lld%lld",&A,&B,&a,&b); 63 c=a+b; 64 C=fabs(A-B); 65 66 LL t1=China(a,b,C); 67 LL t2=China(a,c,C); 68 LL t3=China(b,c,C); 69 70 if(t1==-1&&t2==-1&&t3==-1) 71 { 72 printf("-1\n"); 73 continue; 74 } 75 if(t1>t2) ans=t2; 76 else ans=t1; 77 78 if(ans>t3) ans=t3; 79 80 printf("%lld\n",ans); 81 } 82 return 0; 83 } View Code