Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## Re:在网上找了CTU的数据过了，为什么在这测评会WA

Posted by ragnarok01 at 2013-03-17 18:23:35 on Problem 2115
In Reply To:在网上找了CTU的数据过了，为什么在这测评会WA Posted by:464271301 at 2013-03-17 16:47:10
```不是很懂PASCAL，有几个地方貌似逻辑有错，不过改了还是AC不了：

var a,b,c,k,t,now,c1,x,y:int64;
flag:boolean;

function gcd(a,b:int64):int64;
var n,m:int64;
begin
if b=0 then
begin x:=1;y:=0;exit(a);end;
gcd:=gcd(b,a mod b);
n:=x;m:=y;
x:=m;y:=n-a div b*m;
end;

procedure main;
begin
if k=0 then
begin flag:=false;exit;end;
t:=1<<(k-1);t:=t<<1;
now:=gcd(t,c);
if (b-a) mod now<>0 then writeln('FOREVER') else
begin
c1:=(b-a) div now;
t:=t div now;
x:=(y*c1 mod t+t) mod t;
writeln(x);
end;
end;

begin
flag:=true;
while flag do main;
end.

long long A,B,C,K,i,j,k,gcd;

void calc(long long K,long long C)
{
if(!C)
{
i=1;
j=0;
gcd=K;
return;
}
calc(C,K%C);
k=i-K/C*j;
i=j;
j=k;
}

main()
{
while(scanf("%lld%lld%lld%lld",&A,&B,&C,&K))
{
if(!K) break;
K=1LL<<K;
calc(K,C);
if((B-A)%gcd) puts("FOREVER");
else
{
i=K/gcd;
printf("%lld\n",(j*(B-A)/gcd%i+i)%i);
}
}
}

```

Followed by: