参考博客:
https://blog.csdn.net/u012469987/article/details/39041797
https://blog.csdn.net/qq_22902423/article/details/50569835
代码:
#include<iostream> #include<stdio.h> using namespace std; typedef long long ll; void Exgcd(ll a,ll b,ll &d,ll &x,ll &y){ if(!b){ d = a; x =1; y =0; } else{ Exgcd(b,a%b,d,y,x); y-=x*(a/b); } } int main(){ ll a,b,c,k; while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&k)&&(a||b||c||k)){ ll M = 1ll<<k; ll d,x,y; Exgcd(c,M,d,x,y); ll C = b-a; if(C%d!=0){ cout<<"FOREVER"<<endl; } else{ x =(x*(C/d))%M; x = (x%(M/d)+(M/d))%(M/d); cout<<x<<endl; } } return 0; }