pku2115 C Looooops
http://poj.org/problem?id=2115
数论,模线性方程
1 #include <stdio.h> 2 #include <vector> 3 4 using namespace std; 5 6 long long extend_gcd(long long a, long long b, long long &x, long long &y) 7 { 8 if(b == 0) 9 { 10 x = 1; 11 y = 0; 12 return a; 13 } 14 long long r = extend_gcd(b, a%b, y, x); 15 y -= x*(a/b); 16 return r; 17 } 18 19 long long line_mod_equation(long long a, long long b, long long n) 20 { 21 long long x, y; 22 long long d = extend_gcd(a, n, x, y); 23 if(b%d == 0) 24 { 25 x %= n; 26 x += n; 27 x %= n; 28 x = x*(b/d)%(n/d); //最小解 29 x = (x%(n/d)+n/d)%(n/d);//最小正整数解 30 return x; 31 } 32 else 33 { 34 return -1; 35 } 36 } 37 38 int main() 39 { 40 long long A, B, C, K; 41 long long a, b, n, i, j, r; 42 while(scanf("%lld%lld%lld%lld", &A, &B, &C, &K), A||B||C||K) 43 { 44 a = C; 45 b = B - A; 46 n = (long long)1 << K; 47 r = line_mod_equation(a, b, n); 48 if(r == -1) 49 { 50 printf("FOREVER\n"); 51 } 52 else 53 { 54 printf("%lld\n", r); 55 } 56 } 57 return 0; 58 }