poj 2115 扩展欧几里德

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define max 32
 4 
 5 typedef long long LL;
 6 LL pow2[max+5];
 7 
 8 void init(){
 9     for(int i=1;i<=max;i++){
10         pow2[i]=1LL<<i;
11     }
12 }
13 
14 LL a,b,c,k;
15 
16 void  gcd(LL a,LL b,LL& d,LL& x,LL& y){
17     if(!b){
18         d=a; x=1; y=0; return;
19     }
20     gcd(b,a%b,d,y,x); y-=(a/b)*x;
21 }
22 
23 int main(){
24     init();
25     while(scanf("%lld%lld%lld%lld",&a,&b,&c,&k)&&a+b+c+k!=0){
26                 LL d,x,y;
27         if(a==b){
28             puts("0"); continue;
29         }
30         gcd(c,pow2[k],d,x,y);
31         if((b-a)%d){
32             puts("FOREVER");
33         }
34         else{
35             x=x*(b-a)/d;
36             x=(x%(pow2[k]/d)+(pow2[k]/d))%(pow2[k]/d);
37             printf("%lld\n",x);
38         }
39     }
40 }

 

posted on 2013-07-25 20:03  Stomach_ache  阅读(145)  评论(0编辑  收藏  举报

导航