Poj 1061

经典的线性模方程,考点在求最小正根

#include <iostream>

#include <stdio.h>

using namespace std;

 

typedef long long ll;

 

ll exgcd(ll a,ll b,ll &x,ll &y)

{

if(b==0){x=1;y=0;return a;}

ll d=exgcd(b,a%b,x,y);

ll t=x;

x=y;

y=t-(a/b)*y;

return d;

}

 

void modeq(ll a,ll b,ll n)

{

ll e,i,d,x,y;

d=exgcd(a,n,x,y);

if(b%d>0) printf("Impossible\n");

else

{

 

e=b/d*x;

n=n/d;

e=(e%n+n)%n;//防止负数

printf("%lld\n",e);

}

}

 

int main()

{

ll x,y,m,n,l,a,b;

freopen("in.txt","r",stdin);

while(scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l)!=EOF)

{

a=x-y;

b=n-m;

if(a<0){a=-a,b=-b;}

if(b<0) {b%=l,b+=l;}

modeq(b,a,l);

}

return 0;

}

posted on 2012-03-15 16:36  Inpeace7  阅读(137)  评论(0编辑  收藏  举报

导航