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 91 92 93
|
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int mod = 999911659, N = 40000; typedef long long ll;
int prime[4] = {2, 3, 4679, 35617}; int fact[N], infact[N]; int ans[4];
int qmi(int a, int k, int p) { int res = 1; while(k) { if(k & 1) res = (ll) res * a % p; a = (ll) a * a % p; k >>= 1; } return res; }
void init(int p, int n) { fact[0] = 1, infact[0] = 1; for(int i = 1; i <= n; i ++ ) { fact[i] = (ll)i * fact[i - 1] % p; infact[i] = (ll)infact[i - 1] * qmi(i, p - 2, p) % p; } }
int C(int a, int b, int p) { if(a > b) return 0; int res = (ll) fact[b] * infact[a] % p * infact[b - a] % p; return res; }
int Lucas(int a, int b, int p) { if(a < p && b < p) return C(a, b, p); return (ll) C(a % p, b % p, p) * Lucas(a / p, b / p, p) % p; }
void CRT(int &res) { int p = mod - 1; for(int i = 0; i < 4; i ++ ) { int x = p / prime[i]; res = (ll) (res + (ll) ans[i] * x % p * qmi(x, prime[i] - 2, prime[i]) % p) % p; } }
int main() { int n, g; cin >> n >> g; int res = 0; for(int j = 0; j < 4; j ++ ) { init(prime[j], prime[j]); int k = 0; for(int i = 1; i <= n / i; i ++ ) { if(n % i == 0) { k = (ll) (k + Lucas(i, n, prime[j])) % prime[j]; if(i * i != n) k = (ll) (k + Lucas(n / i, n, prime[j])) % prime[j]; } } ans[j] = k; } CRT(res); if(g % mod != 0) cout << qmi(g, res, mod) << endl; else cout << 0 << endl; return 0; }
|