记一下模板
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0)
{
x=1,y=0;
return a;
}
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main()
{
ll a,b,c,k;
while(cin>>a>>b>>c>>k,a||b||c||k)
{
ll x,y;
ll z=1LL<<k; //要用long long类型的1做左移运算!
ll d=exgcd(c,z,x,y);
if((b-a)%d) cout<<"FOREVER"<<endl;
else{
x*=(b-a)/d;
z/=d;
x=(x%z+z)%z;
cout<<x<<endl;
}
}
}