题目链接:http://poj.org/problem?id=1061
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
Source
PS:
根据题意得:x + m*t - (y + n*t) = k*l; ---->> t*(m-n) - k*l = y-x --->> k*l + t*(n-m) = x-y;
最终化简得:k * l + t*(n - m) = x - y;
代码如下:
#include <cstdio>
#include <cstring>
#include <cmath>
typedef __int64 LL;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
else
{
LL r = exgcd(b,a%b,x,y);
LL t = x;
x = y;
y = t-a/b*y;
return r;
}
}
LL cal(LL a, LL b, LL c)
{
LL x, y;
LL tt = exgcd(a, b, x, y);
if(c%tt)//无整数解
{
return -1;
}
x*=c/tt;
b/=tt;
LL ans = (x%b+b)%b;//最小整数解
return ans;
}
int main()
{
LL a, b, n, m, l, x, y;
while(~scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l))
{
//t(n-m) + k*l = x - y;
LL ans = cal(n-m, l, x-y);
if(ans == -1)
{
printf("Impossible\n");
continue;
}
printf("%I64d\n",ans);
}
return 0;
}
原文地址:http://blog.csdn.net/u012860063/article/details/39778135