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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read(Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0'; if (flag) x = ~x + 1; } template <class Type, class ...rest> void read(Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write(Type x) { if (x < 0) putchar('-'), x = ~x + 1; if (x > 9) write(x / 10); putchar(x % 10 + '0'); } typedef long long L; const int N = 5e4 + 10; const int p[5] = { 999911659, 2, 3, 4679, 35617 }; int n, g, mod, w[5]; int inv[N], fact[N], ifact[N]; void adj (int &x, int t) { x += x >> 31 & t; } void init () { inv[1] = 1; for (int i = 2; i < N; ++i) inv[i] = -(L)(mod / i) * inv[mod % i] % mod; fact[0] = ifact[0] = 1; for (int i = 1; i < N; ++i) { fact[i] = (L)fact[i - 1] * i % mod; ifact[i] = (L)ifact[i - 1] * inv[i] % mod; } } int C (int a, int b) { return a < b ? 0 : (L)fact[a] * ifact[b] % mod * ifact[a - b] % mod; } int lucas (int a, int b) { return b ? (L)C(a % mod, b % mod) * lucas(a / mod, b / mod) % mod : 1; } int calc () { init(); int res = 0; for (int i = 1; i * i <= n; ++i) if (!(n % i)) { res = (res + lucas(n, i)) % mod; if (i ^ n / i) res = (res + lucas(n, n / i)) % mod; } adj(res, mod); return res; } void exgcd (int a, int b, int &x, int &y) { if (!b) return x = 1, y = 0, void(); exgcd(b, a % b, y, x); y -= a / b * x; } int binpow (int b, int k) { int res = 1; for (; k; k >>= 1, b = (L)b * b % mod) if (k & 1) res = (L)res * b % mod; return res;
} int gcd (int a, int b) { return b ? gcd(b, a % b) : a; } int lcm (int a, int b) { return a / gcd(a, b) * b; } int main () { read(n, g); for (int i = 1; i <= 4; ++i) mod = p[i], w[i] = calc(); int res = w[1], t = p[1]; for (int i = 2; i <= 4; ++i) { int x, y; exgcd(t, p[i], x, y); x = (L)(w[i] - res) / gcd(t, p[i]) * x % p[i]; int d = lcm(t, p[i]); res = (res + (L)t * x) % d, t = d; adj(res, t); } mod = p[0]; write(binpow(g, res)); return 0; }
|