about summary refs log tree commit diff
path: root/ntu/luth2.c
blob: 46bd99a442c8888fcb19347dc6b930edb318b94b (plain) (blame)
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
#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;
}