One Person Game (zoj3593 + Extended Euclidean), personzoj3593

Source: Internet
Author: User

One Person Game (zoj3593 + Extended Euclidean), personzoj3593
One Person GameTime Limit:2000 MSMemory Limit:65536KB64bit IO Format:% Lld & % maid Status Practice ZOJ 3593 Description

There is an interesting and simple one person game. Suppose there is a number axis under your feet. You are at pointAAt first and your aim is pointB. There are 6 kinds of operations you can perform in one step. That is to go left or rightA,BAndC, HereCAlways equalsA+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 integerT(0 <T≤ 1000) indicates the number of test cases. ThenTTest cases follow. Each test case is represented by a line containing four integers 4 integersA,B,AAndB, Separated by spaces. (-231 ≤A,B& Lt; 231, 0 & lt;A,B<231)

Output

For each test case, output the minimum number of steps. If it's impossible to reach pointB, Output "-1" instead.

Sample Input
20 1 1 20 1 2 4

 

Sample Output
1-1

 

 

 

 

 

The one-dimensional coordinate axis has two locations: A and B. from A to B, the distance between a, B, and c can be taken in any direction at a time. c = A + B, ask if I can go to B. If I can, I can go to B at least a few times. Idea: c = a + B, C = A-B, it is equivalent to {| x | + | y | ax + by = C | ax + cy = C | bx + cy = C }. For ax + by = C, use the Extended Euclidean Algorithm to obtain ax + by = gcd (a, B). Whether the solution exists can be determined by whether C is a multiple of gcd. If there is a solution, the group of the original equation is (x0, y0) = (xx * C/gcd, yy * C/gcd ). So am = a/gcd, bm = B/gcd, then the general solution can be expressed as (x0 + k * bm, y0-k * am ). Can make | x | + | y | the smallest entire point must be the closest to the intersection of the straight line and the coordinate axis, which can be seen by the continuous translation of the | x | + | y | = C image. Since when a negative number is rounded up, the absolute value of a negative number is rounded up, and then the negative number is added, so the scope of consideration is [-x0/bm-1, -x0/bm + 1], [y0/am-1, y0/am + 1].

 

Reprinted, please specify the Source: search for & STAR kids

 

1 # include <stdio. h> 2 # include <math. h> 3 # include <algorithm> 4 # define LL 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 else12 {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", & 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

 

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.