- 长整型常量一定要加LL!!例如:1LL<< k;
- 同余方程用扩展欧几里得求解。
- 注意边界值特殊情况的考虑。
题目链接:http://acm.hust.edu.cn/vjudge/problem/21464
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;
#define ms(s) memset(s,0,sizeof(s))
typedef unsigned long long ULL;
typedef long long LL;
const int INF = 0x3fffffff;
LL e_gcd(LL a, LL b, LL& x, LL& y){
if(b == 0){
x = 1;
y = 0;
return a;
}
LL ans = e_gcd(b, a%b, y, x);
y -= x*(a/b);
return ans;
}
int main(){
// freopen("F:\\input.txt","r",stdin);
// freopen("F:\\output.txt","w",stdout);
// ios::sync_with_stdio(false);
LL a,b,c,k,m,r;
LL x,y;
while((~scanf("%lld%lld%lld%lld",&a,&b,&c,&k)) && k){
m = 1LL<<k;
LL gcdd = e_gcd(c,m,x,y);
if((b-a)%gcdd != 0 || (c==0 && a!=b)){
printf("FOREVER\n");
}
else{
x = x*((b-a)/gcdd)%m;
x = (x%(m/gcdd)+m/gcdd)%(m/gcdd);
printf("%lld\n",x);
}
}
return 0;
}