poj 2115 C Looooops

http://poj.org/problem?id=2115

这是一道线性同余方程,式子为:c*x=(b-a)%(2^k)可化为c*x+(2^k)*y=b-a;但是首先我们得保证c是正数才可以用扩展GCD;故此时,我们应做(b-a)%(2^k)+(2^k)即可,

用模板酒很简单了,最后被卡在1<<k是默认为int型必须强制转化问long long,为了这个wa很多次,翻遍了解题报告总算发现错误了。。

View Code
#include <stdio.h>
#include
<stdlib.h>
#include
<string.h>
#include
<math.h>
long long exgcd(long long a,long long b,long long &x,long long &y)
{
if(b==0)
{
x
=1,y=0;
return a;
}
else
{
long long d=exgcd(b,a%b,x,y);
long long t=x;
x
=y;
y
=t-a/b*y;
return d;
}
}
int main()
{
long long a,b,c,k,d,x,y,p,t;
while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)&&a+b+c+k)
{
t
=(long long)1<<k;//就是这里没有强制转化
d=exgcd(c,t,x,y);p=b-a;
if(p%d) puts("FOREVER");
else
{
p
/=d;t/=d;c/=d;
x
*=p;
if(x<0)
x
=x%t+t;
printf(
"%lld\n",x%t);
}
}
return 0;
}

posted @ 2011-08-15 12:00  ○o尐懶錨o  阅读(199)  评论(0编辑  收藏  举报