不想搞数论啊啊啊QAQ
贴学长博客题解 : 【poj1061】青蛙的约会 exgcd解同余方程
我的代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
LL exgcd(LL a, LL b, LL &x0, LL &y0)
{
if(!b)
{
x0 = 1; y0 = 0;
return a;
}
int ans = exgcd(b, a%b, x0, y0);
int t = x0;
x0 = y0;
y0 = t-a/b*y0;
return ans;
}
int main()
{
LL x, y, m, n, l;
while(cin >> x >> y >> m >> n >> l)
{
if(m == n)
{
puts("Impossible");
continue;
}
if(m < n)
{
swap(m, n);
swap(x, y);
}
LL x0, y0;
LL c = y-x;
LL _gcd = exgcd(m-n, l, x0, y0);
if(c%_gcd) puts("Impossible");
else printf("%lld\n", ((x0*(c/_gcd)%(l/_gcd))+(l/_gcd))%(l/_gcd));
}
return 0;
}
// 1 2 3 4 5