POJ 2115 C Looooops Extended Euclidean and pojlooooops
It is not hard to understand the meaning of the question. The following formula can be obtained after reading the question:
(A + C * x-B) mod (2 ^ k) = 0
(C * x) mod (2 ^ k) = (B-A) mod (2 ^ k)
Use the modulus linear equation (linear homogeneous equation) to solve the problem.
Template direct car access
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> using namespace std; typedef long ll; ll exgcd (ll, ll B, ll & x, ll & y) {if (B = 0) {x = 1; y = 0; return a;} ll r = exgcd (B, a % B, y, x); ll t = x; y = y-a/B * t; return r;} bool modular_linear_equation (ll a, ll B, ll n) {ll x, y, x0, I; ll d = exgcd (a, n, x, y); if (B % d) {printf ("FOREVER \ n "); return fa Lse;} x0 = x * (B/d) % n; // x0 is a special solution of the equation, which can be positive or negative. The minimum non-negative ll ans is required. if (x0 <0) {ans = x0; for (I = 0; ans <0; I ++) ans = (x0 + I * (n/d) % n;} else {ans = x0; ll temp; for (I = 0; ans> = 0; I ++) {temp = ans; ans = (x0-I * (n/d) % n;} ans = temp;} printf ("% I64d \ n ", ans); return true;} int main () {// freopen ("in.txt", "r", stdin); ll A, B, C, k; while (scanf ("% I64d % I64d % I64d % I64d", & A, & B, & C, & k )) {if (A = 0 & B = 0 & C = 0 & k = 0) break; ll k2 = (ll) 1 <k; if (A = B) printf ("0 \ n"); else modular_linear_equation (C, B-A, k2);} return 0 ;}