POJ1061 青蛙的约会
#include <stdio.h>
#include <iostream>
using namespace std;
int gcd(long long m, long long n)
{
if(n == 0)
return m;
return gcd(n, m%n);
}
void Cal(long long a, long long b, long long &x, long long &y) // ax + by = gcd(a,b)
{
if(b == 0)
{
x = 1;
y = 0;
return ;
}
Cal(b, a%b, x, y);
long long temp_x = x;
long long temp_y = y;
x = temp_y;
y = temp_x - a/b*y;
return ;
}
int main()
{
long long x, y, m, n, l; // (n-m)*i + l*k = x-y
scanf("%lld%lld%lld%lld%lld", &x, &y, &m, &n, &l);
if((x-y) % gcd((n-m),l))
printf("Impossible
");
else
{
long long i, k; // i次跳跃, k个完整圈
Cal(n-m, l, i, k); // (n-m)*i + l*k = gcd((n-m),l)
i = ((x-y)/gcd((n-m),l) * i) % l;
if(i < 0)
i += l;
printf("%lld
", i);
}
return 0;
}