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.
输入
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
输出
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.
样例输入
1 2 3 4 5 10 1 2 2 20
样例输出
4 Pat
同poj 1061
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
__int64 x, y, n, m;
__int64 len;
void ex_gcd(__int64 a,__int64 b,__int64 &d,__int64 &x,__int64 &y)
{
if(b == 0)
{
d = a;
x = 1;
y = 0;
}
else
{
ex_gcd(b,a%b,d,y,x);
y -= x*(a/b);
}
}
int main()
{
//freopen("input.txt","r",stdin);
while(scanf("%I64d %I64d %I64d %I64d %I64d",&x,&y,&m,&n,&len) != EOF)
{
__int64 x1,y1;
__int64 a, b, c;
__int64 d;
if(m > n)
{
a = m-n;
b = len;
c = (y-x+len) % len;
}
else
{
a = n-m;
b = len;
c = (x-y+len) % len;
}
ex_gcd(a,b,d,x1,y1);
if(c % d != 0)
printf("Pat\n");
else
{
while(x1 < 0)
x1 += len;
printf("%d\n",(x1*c/d) % len - (x1*c/d) % len / (len/d) * (len/d) );
}
}
return 0;
}