码迷,mamicode.com
首页 > 其他好文 > 详细

One Person Game(扩展欧几里得)

时间:2019-08-11 20:44:18      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:game   技术   ica   long   stream   a*   int   follow   i++   

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

 

 


Author: CAO, Peng
Contest: The 12th Zhejiang University Programming Contest

技术图片
 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 ll t;
 8 ll d1,d2,d3,d4;
 9 ll X,Y,a,b,c;
10 ll GDC;
11 
12 ll gdc(ll x,ll y){
13     return y==0?x:gdc(y,x%y);
14 }
15 
16 void extend_gdc(ll a,ll b,ll &X,ll &Y){
17     if(b==0){
18         X=1;
19         Y=0;
20         return;
21     }
22     extend_gdc(b,a%b,X,Y);
23     ll temp=X;
24     X=Y;
25     Y=temp-a/b*Y;
26 }
27 
28 int main(){
29     ios::sync_with_stdio(false);
30     while(cin>>t){
31         while(t--){
32             cin>>d1>>d2>>d3>>d4;
33             a=d3,b=d4,c=d2-d1;
34             GDC=gdc(a,b);
35             if(c%GDC!=0){
36                 cout << "-1" << endl;
37                 continue;
38             }
39             extend_gdc(a,b,X,Y);
40             X=X*c/GDC;
41             Y=Y*c/GDC;
42             a=a/GDC;
43             b=b/GDC;
44             ll mid=(Y-X)/(a+b);
45             ll res=0x3f3f3f3f3f3f3f3f;
46             for(int i=mid-1;i<=mid+1;i++){
47                 ll ans=0;
48                 ll ee=X+b*i;
49                 ll rr=Y-a*i;
50                 if(ee*rr>=0) ans+=max(abs(ee),abs(rr));   //同号取最大值
51                 else ans+=abs(ee)+abs(rr);   //异号模相加
52 //                cout << ans << endl;
53                 res=min(res,ans);
54             }
55             cout << res << endl;
56         }
57     }
58     return 0;
59 }
View Code

 

 

One Person Game(扩展欧几里得)

标签:game   技术   ica   long   stream   a*   int   follow   i++   

原文地址:https://www.cnblogs.com/qq-1585047819/p/11336228.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!