题意:
模拟for循环for(int i=A;i!=B;i+=C),且数据范围为k位无符号数以内,即0~1<<k-1,如果能循环为有限次则输出循环次数,否则输出FOREVER。
输入给出a, b, c, k
思路:
exgcd即可
cx + ky = b - a
注意虽然z = a - b可能小于0,但由于z在等式右侧,所以正负性不需要考虑
// Decline is inevitable,
// Romance will last forever.
//#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define mp make_pair
#define pii pair<int,int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define LL long long
#define int long long
#define endl '\n'
const int maxn = 3e2 + 10;
const int INF = 0x3f3f3f3f;
const int dx[] = {0, 0, -1, 1}; //{0, 0, 1, 1, 1,-1,-1,-1}
const int dy[] = {1, -1, 0, 0}; //{1,-1, 1, 0,-1, 1, 0,-1}
const int P = 998244353;
int exgcd(int a, int b, int &x, int &y) {
if(!b) {x = 1; y = 0; return a;}
int d =exgcd(b, a%b, x, y);
int z = x; x = y; y = z - y * (a / b);
return d;
}
void solve() {
int a, b, c, k;
int x, y;
while(cin >> a >> b >> c >> k) {
if(!a && !b && !c && !k) break;
k = (1ll << k);
int d = exgcd(c, k, x, y);
int z = b - a;
if(z % d) {
cout << "FOREVER\n";
continue;
}
x = z / d * x;
x = (x % (k / d) + k / d) % (k / d);
cout << x << endl;
}
}
signed main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int T; scanf("%d", &T); while(T--)
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
// int T; cin >> T; while(T--)
solve();
return 0;
}