1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <cstdio> long long p = 0; long long pow (long long x, long long y) { if(y == 0) { return 1; } else { long long dq = pow(x, y >> 1); if(!(y & 1)) { return dq * dq % p; } else { return dq * dq % p * x % p; } } } void exgcd (long long a, long long b, long long &x, long long &y) { if(b == 0) { x = 1, y = 0; return ; } exgcd(b, a % b, x, y); long long xx = y; long long yy = x - (a / b) * y; x = xx, y = yy; } int main () { long long T = 0; scanf("%lld", &T); while(T--) { long long a = 0, b = 0, c = 0, d = 0, x = 0, y = 0; scanf("%lld %lld %lld %lld %lld", &a, &b, &c, &d, &p); exgcd(a, c, x, y); long long ans = 1; long long bb = b, dd = d; if(x < 0) { x = -x; long long X = 0, Y = 0; exgcd(b, p, X, Y); bb = (X + p) % p; } if(y < 0) { y = -y; long long X = 0, Y = 0; exgcd(d, p, X, Y); dd = (X + p) % p; } ans = pow(bb, x) * pow(dd, y) % p; if(pow(ans, a) == b && pow(ans, c) == d) { printf("%lld\n", ans); } else { printf("No Solution!\n", ans); } } return 0; }
|