题目链接:http://poj.org/problem?id=2115
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> using namespace std; const int INF=0x3f3f3f3f; const int maxn=1010; typedef __int64 LL; LL A,B,C,k; LL Extended_Euclid(LL a,LL b,LL &x,LL &y){ if(b==0){ x=1; y=0; return a; } LL d=Extended_Euclid(b,a%b,x,y); LL tmp=x; x=y; y=tmp-a/b*y; return d; } int main(){ #ifndef ONLINE_JUDGE freopen("test.in","r",stdin); freopen("test.out","w",stdout); #endif while(~scanf("%I64d%I64d%I64d%I64d",&A,&B,&C,&k)){ if(!A&&!B&&!C&&!k) break; LL x,y,n=(LL)1<<k; LL d=Extended_Euclid(C,n,x,y); if((B-A)%d!=0) puts("FOREVER"); else{ x=(x*((B-A)/d))%n; x=(x%(n/d)+n/d)%(n/d); cout<<x<<endl; } } return 0; }