#define int long long intqmi(int a, int k, int p){ int res = 1; while (k) { if (k & 1) res = (__int128)res * a % p; a = (__int128)a * a % p; k >>= 1; } return res; }
booltest(int a, int u, int k, int n){ int v = qmi(a, u, n); if (v == 1 || v == 0 || v == n-1) returntrue; for (int i = 1; i <= k; i++) { v = (__int128)v * v % n; if (v == n-1 && i != k) returntrue; if (v == 1) returnfalse; } returnfalse; }
boolmr(int n){ staticint bases[] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; if (n < 3 || n % 2 == 0) return n == 2; int u = n-1, k = 0; while (u % 2 == 0) u /= 2, k++; for (int a: bases) if (!test(a, u, k, n)) returnfalse; returntrue; }