AtCoder Beginner Contest 228E - Integer Sequence Fair


题目等价于计算\(M^{k^{N}} \ mod \ P\)
由费马小定理\(M^{p-1} \equiv 1(mod \ P)\)
原式等于\(M^{q(p-1)+r} mod \ P\)其中\(r=K^N mod(P-1)\)

#include 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define LL long long
using namespace std;
const LL MOD = 998244353;
LL n, m, k;
LL qmi(LL m, LL k, LL p)
{
	LL res = 1, t = m % p;
	while (k) {
		if (k & 1)	res = res * t % p;
		t = t * t % p;
		k >>= 1;
	}
	return res;
}
int main() {
	IOS;
	cin >> n >> k >> m;
	if (m % MOD == 0)	cout << 0 << endl;
	else cout << qmi(m, qmi(k, n, MOD - 1), MOD) << endl;
	return 0;
}