http://poj.org/problem?id=1061
原理参照扩展欧几里得算法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define LL long long
using namespace std;
LL int x , y , m , n , l ;
LL xx , yy , mm , nn , ll , c , d ;
LL mod1( LL x1 , LL y1 )
{
if( x1 >= 0 )
return x1 % y1 ;
else
return x1 % y1 + y1 ;
}
LL exgcd( LL a ,LL b , LL &x ,LL &y )
{
if( b == 0 )
{
x = 1 ;
y = 0 ;
return a ;
}
LL r = exgcd( b , a % b , x , y ) ;
LL t = x ;
x = y ;
y = t - a / b * y ;
return r ;
}
int main()
{
while( cin >> xx>> yy >> mm >> nn >> ll )
{
if( mm > nn )
{
d = exgcd( mm - nn , ll , x , y ) ;
c = yy - xx ;
}
else
{
d = exgcd( nn - mm , ll , x , y ) ;
c = xx - yy ;
}
if( c % d != 0 )
{
cout << "Impossible" << endl ;
return 0 ;
}
ll = ll / d ;
x = mod1( x * c / d , ll ) ;
cout << x << endl ;
}
}