解同余方程
#include<cstdio>
long long mod(long long a,long long b)
{ return (a % b + b) % b; }
struct triple { long long d,x,y; };
long long
{
if(b == 0) return a;
else return
}
triple Extended_Euclid(long long a,long long b)
{
triple result;
if(b == 0)
{ result.d = a; result.x = 1; result.y = 0; }
else
{
triple ee = Extended_Euclid(b,mod(a,b));
result.d = ee.d;
result.x = ee.y;
result.y = ee.x - (a/b)*ee.y;
}
return result;
}
long long MLES(long long a,long long b,long long n)
{
triple ee = Extended_Euclid(a,n);
if(mod(b,ee.d)) return -1;
else return mod((ee.x * (b / ee.d)),n/ee.d);
}
int main()
{
long long x, y, m, n, l;
while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l) != EOF)
{
long long mles;
mles = MLES(m-n,y-x,l);
/*此处输入三个参数设为 a,b,n 可求得(ax-b)%n=0的最小解。
如果返回-1,则表示此方程无解。*/
if(mles < 0)
printf("Impossible\n");
else
printf("%I64d\n",mles);
}
return 0;
}
posted on 2010-03-05 10:18 liugoodness 阅读(367) 评论(0) 编辑 收藏 举报