diff options
author | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2016-10-30 16:37:10 +0700 |
---|---|---|
committer | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2016-10-30 16:37:10 +0700 |
commit | 250b7d75204bb18311f51d8b67164f9ad4cef9f2 (patch) | |
tree | 4b78c748460f8339c871c403d99040775c6a61f9 /NTU/luth2.c | |
parent | 88ae37bb28a41ea8844532c38e5cd8622b9f4701 (diff) | |
download | cp-250b7d75204bb18311f51d8b67164f9ad4cef9f2.tar.gz |
NTU: Add luth2.c and TutOnEqua.c
Diffstat (limited to 'NTU/luth2.c')
-rw-r--r-- | NTU/luth2.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/NTU/luth2.c b/NTU/luth2.c new file mode 100644 index 0000000..46bd99a --- /dev/null +++ b/NTU/luth2.c @@ -0,0 +1,55 @@ +#include <stdio.h> + +long long modinv(long a, long b) +{ + /* From: http://rosettacode.org/wiki/Modular_inverse#C */ + + long long t = 0, nt = 1, r = b, nr = a % b, q, tmp; + + while (nr) { + q = r / nr; + + tmp = nt; + nt = t - q * nt; + t = tmp; + + tmp = nr; + nr = r - q * nr; + r = tmp; + } + + return (t < 0) ? t + b : t; +} + +long powmod(long a, long b, long k) +{ + if (b == 1) + return a; + + long long p = powmod(a, b / 2, k); + p = (b % 2) ? p * p * a % k : p * p % k; + + return p; +} + +int main() +{ + short t, i; + long a, b, k, j; + long long c = 1; + + scanf("%hd", &t); + + for (i = t; i; i--) { + scanf("%ld %ld %ld", &a, &b, &k); + + if (b < 0) { + a = modinv(a, k) % k; + b = -b; + } + + printf("%ld\n", (b) ? powmod(a, b, k) : 1); + } + + return 0; +} |