# (Number theory, Chinese remainder theorem, extended Euclidean algorithm) Luo Gu P1516 frog appointment (increase + / provincial election -)

Title Description

Two frogs on the Internet acquaintance, and they talked happily, so that it is necessary to see the side. They are pleased to find that they live in the same line of latitude, so they agreed to their west-hop until you meet so far. But before they start forgetting a very important thing, neither ask other features, there is no agreement to meet the specific location. But the frogs are very optimistic, as long as they feel has been moving in a certain direction jump, always run into each other's. But unless these two frogs jump to the same point at the same time, otherwise it is impossible to never meet the. To help these two optimistic frog, you are asked to write a program to determine whether it can meet two frogs, will meet at what time.

We call two frogs were called A frog and frog B, a predetermined longitude and latitude 0 degrees at the line of origin in the positive direction from east to west, a unit length 1 m, so that we get a butt axes . A frog is provided is the starting point coordinates x, the coordinates of the starting point frog B is y. A frog m m a jump, a jump frog n B m, a two frog jump takes the same time. Latitude line length L meters. Now you will meet several times after the jump find them.

Input Format

Input line 5 includes only integers x, y, m, n, L

其中0<x≠y < =2000000000,0 < m、n < =2000000000,0 < L < =2100000000。

Output Format

Output to meet the required number of days, if you never meet the output line "Impossible".

Sample input and output

Input # 1
1 2 3 4 5
Output # 1
4 
Analysis: provided jumped k times;
frog A: current coordinates (x + k * m)% l;
frog B: current coordinates (y + k * m)% l;
asks whether there is a k, so that x + k * m = y + k * n (mod l);
then obtain: k * (mn) = yx (mod l)
Therefore: Let a = mn, b = yx;
then the problem is equivalent to: a * k + t * l = b;
namely; (mn) * x + l * y = b
to obtain the X * a '+ B * Y' = GCD (a, B);
Code:

#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
ll ans,x1,y1;

ll exgcd(ll a,ll b,ll &x1, ll &y1)
{
if(!b)
{
x1=1;
y1=0;
return a;
}
ans=exgcd(b,a%b,x1,y1);
ll t=x1;
x1=y1;
y1=t-a/b*y1;
return ans;
}

int main()
{
ll n,m,x,y,l;
cin>>x>>y>>m>>n>>l;
ll b=n-m,a=x-y;
if(b<0)
{
b=-b;
a=-a;
}//????
exgcd(b,l,x1,y1);
if(a%ans!=0)
cout<<"Impossible";
else
cout<<((x1*(a/ans))%(l/ans)+(l/ans))%(l/ans);
}

 
 

Guess you like

Origin www.cnblogs.com/little-cute-hjr/p/11462049.html