题目地址:http://poj.org/problem?id=1061
就是求(x-y)*t +(n-m)*k = -a 的式子中t的最小解,但注意k!=0
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
if(!b) {d=a;x=1;y=0;}
else{
gcd(b,a%b,d,y,x);y-=x*(a/b);
}
}
int main()
{
LL x,y,m,n,L;
while(cin>>x>>y>>m>>n>>L)
{
LL a=x-y,b=n-m,d;
gcd(b,-L,d,x,y);
if((-a)%d) {
cout<<"Impossible"<<endl;
continue;
}
LL s=(-L)/d,k=(-a)/d;
LL ans=fabs((k*x-s)%s);
if((ans*b+a)%L==0) ans=fabs(L-ans); //处理k==0的情况
cout<<ans<<endl;
}
return 0;
}