Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P2480 [SDOI2010]古代猪文

P2480 [SDOI2010]古代猪文

计算 $g ^ {\sum _ {d | n} \binom n d } \mod 999911659$

拓展欧拉定理:

$$
a^b\equiv
\begin{cases}
a^{b\ mod\ \varphi(m)}&(a,m)=1\\
a^b&b<\varphi(m)\\
a^{b\ mod\ \varphi(m)+\varphi(m)}&b\geq \varphi(m)
\end{cases}\ \pmod m
$$

考虑计算 $\sum _ {d | n} \binom n d \mod 999911658$ 。$n$ 太大了,不能直接算。注意到 $999911659 = 2 \times 3 \times 4679 \times 35617$ 。如果将 $\binom n d$ 分别在 $4$ 个质数下计算,用 CRT 合并后刚好就是模 $999911658$ 剩余系下的答案。

CRT
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
#include <cstdio>
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 << 1) + (x << 3) + c - '0';
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)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef long long LL;
const int N = 35618, p = 999911658, p1 = 2, p2 = 3, p3 = 4679, p4 = 35617;
int n, g;
int binpow (int b, int k, const int mod = p + 1)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
template <const int mod>
struct Calc
{
int fact[mod], ifact[mod];
int binpow (int b, int k = mod - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
int C (int a, int b) { return a < b ? 0 : (LL)fact[a] * ifact[b] % mod * ifact[a - b] % mod; }
int lucas (int a, int b) { return b ? (LL)C(a % mod, b % mod) * lucas(a / mod, b / mod) % mod : 1; }
void init ()
{
fact[0] = 1;
for (int i = 1; i < mod; ++i)
fact[i] = (LL)fact[i - 1] * i % mod;
ifact[mod - 1] = binpow(fact[mod - 1]);
for (int i = mod - 1; i; --i)
ifact[i - 1] = (LL)ifact[i] * i % mod;
}
};
Calc <p1> q1; Calc <p2> q2; Calc <p3> q3; Calc <p4> q4;
const LL k1 = (LL)p2 * p3 * p4 * q1.binpow((LL)p2 * p3 * p4 % p1), k2 = (LL)p1 * p3 * p4 * q2.binpow((LL)p1 * p3 * p4 % p2), k3 = (LL)p1 * p2 * p4 * q3.binpow((LL)p1 * p2 * p4 % p3), k4 = (LL)p1 * p2 * p3 * q4.binpow((LL)p1 * p2 * p3 % p4);
int CRT (int a, int b, int c, int d) { return (a * k1 + b * k2 + c * k3 + d * k4) % p; }
int main ()
{
read(n, g);
if (g == p + 1) return puts("0"), 0;
q1.init(), q2.init(), q3.init(), q4.init();
int res = 0;
for (int i = 1; i * i <= n; ++i)
if (n % i == 0)
{
(res += CRT(q1.lucas(n, i), q2.lucas(n, i), q3.lucas(n, i), q4.lucas(n, i))) %= p;
if (i ^ n / i) (res += CRT(q1.lucas(n, n / i), q2.lucas(n, n / i), q3.lucas(n, n / i), q4.lucas(n, n / i))) %= p;
}
write(binpow(g, res));
return 0;
}
exCRT
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;
}