Appointment
Two crickets gets know with each other on freecity bbs. They feel very happy chatting with each other, so they want to meet the other. Fortunately, they find they both live the lakeside of QiZhen Lake, which is a circle. Because they can't swim, so they only can move along the lakeside. Then, they set out, unfortunately, they forget to fix a place to meet each other. but they are optimistic, they think that if they jump all the while, they can come up against each other.(The meaning of meeting with each other is that they get to the same place at same time).
You are asked to help the two crickets. Telling them whether they can meet each other.
We can select a point on the circle as the origin, and the clockwise as the positive direction to make a coordinate axis.
You are given five integer X, Y, M, N, L. X is the start coordinate of the first cricket, Y is the other's. The first cricket can jump M once, and the other can jump N. The sign of M,N denote the direction they jump, They use the same time in one jump. L denote the length of the circle lake.
Input:
Each line contain five integers, X, Y, M, N, L which satisfy these conditions: 2 <= L <= 1,000,000,000; 0 <= x, y < L; x != y; 0< |m|,|n|< L
Output:
For each case in input, output a number in a line, which denote the minimum steps they can meet. If they can never meet with each other, print "Pat" in this line.
Sample Input:
1 2 3 4 5 10 1 2 2 20Sample Output:
4 Pat
Author: JIANG DongmingSource: ZOJ Monthly, February 2006
/* ax+by=c 求解该方程 1:首先若gcd(a,b)=d 若c不是d的倍数 方程无解 因为左边是d的倍数而右边的不是 2:在有解的情况可以设的c=d*k;(k倍) 那么可先求出方程ax+by=d 的一组解(x0,y0) 则a*x0+y*y0=c; 两边都乘以k得到 a(k*x0)+b*(k*y0)=d;因此k*x0,k*y0是原方程的一组解 3:求解ax+by=gcd(a,b)的过程 void EuclidGCD(LLD a,LLD b,LLD &d,LLD &x,LLD &y) { if(!b){d=a;x=1;y=0;} else { EuclidGCD(b,a%b,d,y,x); y-=(a/b)*x; } } 4:其通解为(x+k*b’,y-k*a’) //其中k去任意整数 ( 设(x1,y1) ,(x2,y2)为方程的一组解 那么有a*x1+b*y1=a*x2+b*y2=c, 有a(x1-x2)=b(y2-y1); 因为有d=gcd(a,b) 故消去d后 方程为 a’*(x1-x2)=b’*(y2-y1) 此时a’和b’一定互素 故x1-x2一定是b’的倍数 设为k*b’; 故通解为(x+k*b’,y-k*a’) 其中a’=a/d , b’=b/d; ) */
#include<cstdio> void EuclidGCD(long long a,long long b,long long &d,long long &x,long long &y) { if(!b) { d=a; //printf("d==%lld/n",d); x=1; y=0; return; } EuclidGCD(b,a%b,d,y,x); y=y-(a/b)*x; } int main() { long long x,y,m,n,l; long long a,b,c,d,x1,y1; while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)!=EOF) { if(x!=y&&m==n) { printf("Pat/n"); continue; } a=m-n;//ax+by=c b=l; c=y-x; if(a<0)//necessary,不需要改变b,因为y未知 { a=-a; c=-c; } EuclidGCD(a,b,d,x1,y1);//算出来的是ax+by=gcd(a,b) 中的x0,y0 //printf("d==%lld/n",d); if(c%d!=0) { printf("Pat/n"); continue; } x=x1*c/d; b=b/d; x=(x%b+b)%b; printf("%lld/n",x); } return 0; }