You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
605 B
C++
25 lines
605 B
C++
#include <cstdio>
|
|
typedef long long int64;
|
|
void exgcd(int64 a, int64 b, int64 &d, int64 &x, int64 &y)
|
|
{
|
|
!b ? (x = 1, y = 0, d = a) : (exgcd(b, a % b, d, y, x), y -= x * (a / b));
|
|
}
|
|
int main()
|
|
{
|
|
int64 A, B, C;
|
|
int k;
|
|
while (scanf("%lld%lld%lld%d", &A, &B, &C, &k), A | B | C | k)
|
|
{
|
|
int64 a = C, b = B - A, x, y, n = 1ll << k, d;
|
|
exgcd(a, n, d, x, y);
|
|
if (b % d)
|
|
puts("FOREVER");
|
|
else
|
|
{
|
|
x = (x * (b / d)) % n;
|
|
x = (x % (n / d) + n / d) % (n / d);
|
|
printf("%lld\n", x);
|
|
}
|
|
}
|
|
return 0;
|
|
} |