#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
LL gcdEx(LL a,LL b,LL& x,LL& y)
{ //求ax+by=gcd(a,b) 的整数解 返回gcd(a,b);
if(b==0) {
x=1; y=0; return a;
}
LL x1,y1;
int gcd=gcdEx(b,a%b,x1,y1);
x=y1;
y=x1-a/b*y1;
return gcd;
}
int main()
{
LL A,B,C,K,x,y,d,k,s,ans;
while(cin>>A>>B>>C>>K)
{
if(A==0&&B==0&&C==0&&K==0) break;
d=gcdEx(C,(1LL<<K),x,y); //要把1转化为long long 类型,不然会溢出
if((B-A)%d){
cout<<"FOREVER"<<endl; continue;
}
k=(B-A)/d;
ans=k*x; s=(1LL<<K)/d;
cout<<(ans%s+s)%s<<endl;
}
return 0;
}
POJ 2115 C Looooops (扩展欧几里得算法)
最新推荐文章于 2018-02-01 21:05:28 发布