ZOJ3593-One Person Game-Extended Euclid

Mental retardation, mental retardation, Shui Yishui blog.

Originally it was a water problem, but the for loop traversed there and got frustrated. . .

 

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 ABa and b, separated by spaces. (-231 ≤ AB < 231, 0 < ab < 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

 

I didn't understand the question at first, but later I understood, c=a+b, so there are 3 equations, namely a*x+b*y=r, a*x+c*y=r, b*x+ c*y=r;

After extending Euclidean to find out gcd, by judging whether r is a multiple of gcd, whether the equation has a solution, and then go on. . .

Because x and y obtained by extended Euclid are not optimal solutions, through the general formula of x and y, x=x 0 +b/gcd*k;y=y 0 -a/gcd*k;( k is a multiple)

Originally, what I thought was that the optimal solution and the general solution obtained should not be much different, so I traversed directly from -100 to 100. It turned out that it was always wa. The senior said that the result is the closest distance between the line where the equation is located and the source point. .

It is to determine the range of for, not just thinking about -100 to 100 and thinking that water can pass. . .

First x=x 0 +b/gcd*k, find the range of k, which is [-x 0 *gcd/b-1,-x 0 *gcd/b+1], for once in this range, and then Through the general solution of y, the obtained range of k is for again to get the optimal solution.

I am mentally retarded, when calculating k of y, it should be /a, I wrote /b _(:з"∠)_ 

Code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<vector>
 9 #include<stack>
10 using namespace std;
11 typedef long long ll;
12 const int maxn=1e5+10;
13 const int inf=0x3f3f3f3f;
14 const int eps=1e-6;
15 
16 ll exgcd(ll a,ll b,ll &x,ll &y){
17     if(b==0){
18         x=1;y=0;
19         return a;
20     }
21     ll r=exgcd(b,a%b,x,y);
22     ll t=y;
23     y=x-(a/b)*y;
24     x=t;
25     return r;
26 }
27 
28 ll fun(ll a,ll b,ll r){
29     ll x,y;
30     ll gcd=exgcd(a,b,x,y);
 31      if (r%gcd!= 0 ) return - 1 ;
 32      else {
 33          x*=r/ gcd;
 34          y*=r/ gcd;
 35          ll k1=- 1.0 *x*gcd/b- 1 ,k2=- 1.0 *x*gcd/b+ 1 ;
 36          ll kk1= 1.0 *y*gcd/a- 1 ,kk2= 1.0 *y*gcd/a+ 1 ; // y is Divided by a, mental retardation, written as divided by b. 
37          ll ans=fabs(x)+ fabs(y);
 38          for(ll i=k1;i<=k2;i++){
39             if(fabs(x+i*b/gcd)+fabs(y-i*a/gcd)<ans)
40                 ans=fabs(x+i*b/gcd)+fabs(y-i*a/gcd);
41 
42         }
43         for(ll i=kk1;i<=kk2;i++){
44              if(fabs(x+i*b/gcd)+fabs(y-i*a/gcd)<ans)
45                 ans=fabs(x+i*b/gcd)+fabs(y-i*a/gcd);
46 
47         }
48         return ans;
49 
50     }
51 }
52 
53 int main(){
54     int t;
55     cin>>t;
56     while(t--){
57         ll A,B,a,b,c;
58         cin>>A>>B>>a>>b;
59         c=a+b;
60         ll r=fabs(A-B);
61         ll h1=fun(a,b,r);
62         ll h2=fun(a,c,r);
63         ll h3=fun(b,c,r);
64         if(h1==-1&&h2==-1&&h3==-1)cout<<"-1"<<endl;
66{
else65                      ll years= min(h1,h2);
67              years old= min(years,h3);
68              cost<<ans<< endl;
69          }
 70      }
 71 }

 

 

Water is water, slipped, and slowly replenished. Go to see the chairman tree. . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325039392&siteId=291194637