[EXGCD] Zzu OJ 10402 C. Robot

Source: Internet
Author: User

Test instructions

Description

Dr Kongthe robot was designed to be very lively, both in-situ jumping and long jump. Due to hardware and software design constraints, robot Carl can only fixed-point long jump. If the robot stands (X,Y) position, it can jump in place, but only in the (X,Y), (X,- y), (- x,Y), (- x,- y), (Y,X), (Y,- x), (- y,X), (- y,- x) eight points jump to jump.

Now, Dr Kong want to design a counter on robot Carl to record the number of changes it bounces ( S , T ), that is, the sum of the passing position coordinate values.

Can you help Dr Kong determine if the robot can bounce and spell out numbers ( S , T )?

Suppose that the robot Carl initially stands ( 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

There is a space between the data.

Output

for each set of test data, output one line: Y or for N That can be spelled out and not spelled out .

Sample Input
32 1 3 31 1 0 11 0-2 3
Sample Output
Yny


Ideas:

First we set (A1,A2...A8) to represent how many times each position has been trampled.

Then we find that A1 and A4 can actually be made into one. Because A4+1 is waiting for a1-1.

So there are 4 variables left (A1,A2,A5,A6)

At this point we complete the equation:

S= (A1+A2) x+ (A5+A6) Y

t= (A5-A6) x+ (A1-A2) Y

Now it is only necessary to have an integer four-tuple (A1,A2,A5,A6) to satisfy both equations.

Personal yy has a method.

Suppose A=a1+a2 b=a5+a6 c=a5-a6 d=a1-a2

As long as (A+D) is an even number and (B+c) is an even number can be.

Because A1 and A2 can be obtained through A+d, A5 and A6 can be obtained by b+c.

And then actually this is a kind of equation

S=ax+by and T=cx+dy

We can use EXGCD to find out the general solution of A,b,c,d.

Of course, if no solution is directly "N".

Make TEP=GCD (x, y)

We can find out a=a+k1* (y/tep) b=b-k1* (x/tep) c=c+k2* (y/tep) d=d-k2* (x/tep)

Then I was very stupid to enumerate the situation of a,b,c,d.

Because the odd-even change of a, B with K1 c,d with the K2 parity transformation

A total of two groups (A, b) and two (c,d) cases

Enumeration to determine the answer.


Then note that all of the above are given x, y, and nonzero conditions.

The zero situation needs a special sentence.

Code:

#include "stdio.h" #include "algorithm" #include "string.h" #include "iostream" #include "queue" #include "map" #include "        String "#define LL long longusing namespace Std;ll exgcd (ll a,ll b,ll &x,ll &y) {if (b==0) {x=1;        y=0;    return A;    } ll R=EXGCD (b,a%b,x,y), t;    T=x;    X=y;    Y=t-a/b*y; return r;}    int main () {int t;    cin>>t;        while (t--) {ll a,b,s,tt;        scanf ("%lld%lld%lld%lld", &AMP;A,&AMP;B,&AMP;S,&AMP;TT);            if (a==0 && b==0) {if (s==0 && tt==0) puts ("Y");            Else puts ("N");        Continue } else if (a==0 | | b==0) {if (a==0) {if (s%b==0 && tt%b==0) puts                ("Y");            Else puts ("N");                } else {if (s%a==0 && tt%a==0) puts ("Y");            Else puts ("N");        } continue;        } ll X1,y1,x2,y2; ll TEP1=EXGCD (a,b,x1,y1);        ll TEP2=EXGCD (A,B,X2,Y2);            if (s%tep1!=0 | | tt%tep2!=0) {puts ("N");        Continue        } X1=X1*S/TEP1;        X2=X2*TT/TEP2;        Y1=Y1*S/TEP1;        Y2=Y2*TT/TEP2;        int f1,f2,f3,f4,v1,v2,v3,v4;        if (x1%2) f1=1;        else f1=0;        if (y1%2) f2=1;        else f2=0;        if ((B/TEP1)%2) f3=f1^1;        else f3=f1;        if ((A/TEP1)%2) f4=f2^1;        else F4=f2;        if (x2%2) v1=1;        else v1=0;        if (y2%2) v2=1;        else v2=0;        if ((B/TEP2)%2) v3=v1^1;        else V3=v1;        if ((A/TEP2)%2) v4=v2^1;        else V4=v2; if ((f1+v2)%2==0 && (F2+V1)%2==0) | |        ((F1+V4)%2==0 && (f2+v3)%2==0))            {puts ("Y");        Continue } if (((F3+V2)%2==0 && (F4+V1)%2==0) | |        ((F3+V4)%2==0 && (f4+v3)%2==0))            {puts ("Y");        Continue    } puts ("N"); } return 0;}


[EXGCD] Zzu OJ 10402 C. Robot

Related Article

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.