标准的同余方程题
设跳x次,青蛙A起点a,每次跳m;青蛙B起点b,每次跳n。则有下列关系:
(a+mx) mod L = (b+nx) mod L;
即为:(m-n)x=(b-a)(mod L) 注意,m-n和L必须都为正。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll x, y;
ll e_gcd(ll a, ll b, ll &x, ll&y)
{
if (b==0)
{
x = 1; y = 0;
return a;
}
ll r = e_gcd(b, a%b, x, y);
ll t = y;
y = x - (a / b)*y;
x = t;
return r;
}
bool tongyu(ll a, ll b, ll n)
{
ll d = e_gcd(a, n, x, y);
if (b%d) return false;
x = x*(b / d) % n; //最小解
x = (x + n / d) % (n / d); //最小正数解
return true;
}
int main()
{
ll a, b, m, n, L;
cin >> a >> b >> m >> n>>L;
if (m < n)
{
swap(m, n);
swap(a, b);
}
if (tongyu(m-n,b-a,L))
cout << x << endl;
else cout << "Impossible" << endl;
system("pause");
}