A simple extension of Euclid's question
Here 2^k can not be clever with 1<<k to write, K >= 31 o'clock exploded int, even if defined as long long can not directly write this
Later honestly for (int i=1; i<=k; i++) bb = bb*2; It's over. = =
1#include <cstdio>2#include <cstring>3 4 using namespacestd;5 #definell Long Long6ll EX_GCD (ll A, ll &x, LL B, LL &y)7 {8 if(b = =0){9x =1, y =0;Ten returnA; One } All ans = EX_GCD (b, X, a%b, y); - intt =x; -x = y, y = t-a/b*y; the returnans; - } - intMain () - { + //freopen ("a.in", "R", stdin); - intA, B, C, K; + while(SCANF ("%d%d%d%d", &a, &b, &c, &k)) { A if(A = =0&& b = =0&& c = =0&& k = =0) Break; atll aa = c, bb =1, CC = bA, x, y; - for(intI=1; I<=k; i++) BB = bb*2; -ll g =EX_GCD (aa, X, BB, y); - if(cc% G! =0){ -Puts"FOREVER"); - Continue; in } -ll KK = CC/G; toX*=KK, y*=KK; +Aa/=g, bb/=G; - if(x >=0) thex = x-x/bb*BB; * Else $x = x-x/bb*bb+BB;Panax Notoginsengprintf"%i64d\n", x); - } the return 0; +}
POJ 2115 simple equation solving with modulo