Poj 2115

模线性方程,水题

#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("FOREVER\n");

else

{

 

e=b/d*x;

n=n/d;

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

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

}

}

 

int main()

{

ll a,b,c,k,s;

 

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

while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)!=EOF)

{

if(a==0 && b==0 && c==0 && k==0)

break;

s=1;

b=b-a;

for(int i=1;i<=k;i++)

s*=2;

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

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

//cout<<c<<" "<<b<<" "<<s<<endl;

modeq(c,b,s);

}

return 0;

}

posted on 2012-03-15 19:12  Inpeace7  阅读(156)  评论(0编辑  收藏  举报

导航