(Extended Euclidean algorithm) Zzuoj 10402:c. Robot

Source: Internet
Author: User
Tags gcd

10402:c. Robotics
Descriptiondr. The design of the Kong robot Carl is very lively, both in-situ jumping, but also can jump. Due to hardware and software design constraints, robot Carl can only fixed-point long jump. If the robot is standing in the (x, y) position, it can jump in place, but only in (x, y), (x,-y), (-x,y), (-x,-y), (y,x), (Y,-x), (-y,x), (-y,-x) eight points. Now, Dr Kong wants to design a counter on robot Carl to record the number of bounces (s,t), i.e. the sum of the coordinates of a passing position. You can help Dr. Kong judge whether the robot can bounce, spell out the number (s,t) it? Assume that the robot Carl initially stands at (0,0) position. Input first line:             K                indicates how many sets of test data are available. Next there are K lines, each line: x  Y  S  T     1≤k≤10000   -2*109 <= x, Y, S, T <= 2*109 data There is a space between. Output for each set of test data, outputs one line: Y or N, respectively, can be spelled out, cannot spell out sample Input32 1 3 1 0 0-2 3Sample outputyny
Euclid and the extended Euclidean algorithm: http://www.cnblogs.com/frog112111/archive/2012/08/19/2646012.html

/*
Ideas: (x, Y), (x,-y), (-x,y), (-x,-y), (y,x), (Y,-x), (-y,x), (-y,-x)
Although eight points, actually useful only four points, the other four points can be replaced, such as
(x, y) can be substituted (----) <->-
Set these four points to be (x, y), (y, X), (y,-x) respectively through A1, A2, A3, A4 times
Then there are
(A1+A2) x + (a3+a4) y = s; ---> Ax + by = s; (Obviously the form of an indefinite equation)
(a1-a2) y + (a3-a4) x = t; ---> Dx + Cy = t;
Looking closely at the above, A+d and B+c are even
For ax + by = s, you can use EXGCD () to find the value of a and B, as well as to find the value of D,c
If a, B is the solution to the equation, then the rest is:
A = a + Y/GCD (A, B) *t (where T is an arbitrary integer)
B = B + X/GCD (A, b) *t

Using the above formula, enumerate the a,b,c,d and know that the results of satisfying a+d and b+c are even!
*/  

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <algorithm>5#include <vector>6#include <map>7#include <queue>8 #defineMAX 0x3f3f3f3f9 #defineN 550Ten using namespacestd; One  A Long LongEXGCD (Long LongALong LongBLong Long&x,Long Long&y) - { -     if(b==0) the     { -x=1; -y=0; -         returnA; +     } -     Long LongR=EXGCD (b,a%b,x,y); +     Long Longt=x; Ax=y; aty=t-a/b*y; -     returnR; - } -  - /* - x = x + B/GCD (A, b) *t; in y = Y-A/GCD (A, b) *t; - */ to  + intMain () { -     intK; the     Long Longx, Y, S, t; *scanf"%d", &k); $      while(k--){Panax Notoginsengscanf"%lld%lld%lld%lld", &x, &y, &s, &t); -         Long LongA, B, C, D, G; theg =exgcd (x, Y, A, b); +c =A; AD =b; the         if(s%g==0&& t%g==0){ +A = A * (s/g); -b = b* (s/g); $c = c* (t/g); $D = d* (t/g); -             BOOLFlag =false; -              for(inti=-2; i<=2&&!flag; ++i) { the                 Long LongAA, BB; -AA = a+x/g*i;WuyiBB = b-y/g*i; the                  for(intj=-2; j<=2&&!flag; ++j) { -                     Long Longcc, DD; WuCC = c+x/g*J; -DD = d-y/g*J; About                     if((AA+DD)%2==0&& (BB+CC)%2==0) $Flag =true; -                 } -             } -             if(flag) printf ("y\n"); A             Elseprintf"n\n"); +}Else { theprintf"n\n") ; -         } $     } the     return 0; the}

(Extended Euclidean algorithm) Zzuoj 10402:c. Robot

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.