题意
给出 n , q ( 1 ≤ q , n ≤ 1 0 9 ) n,q(1\leq q,n\leq 10^9) n,q(1≤q,n≤109),计算 q ∑ d ∣ n C n d m o d 999911659 q^{\sum_{d|n}C^{d}_{n}}\ mod\ 999911659 q∑d∣nCnd mod 999911659
思路
因为
999911659
999911659
999911659是个质数,根据欧拉定理得:
q
∑
d
∣
n
C
n
d
≡
q
∑
d
∣
n
C
n
d
m
o
d
999911658
(
m
o
d
999911659
)
q^{\sum_{d|n}C^{d}_{n}}\equiv q^{\sum_{d|n}C^{d}_{n}\ mod\ 999911658}(mod\ 999911659)
q∑d∣nCnd≡q∑d∣nCnd mod 999911658(mod 999911659)
计算
∑
d
∣
n
C
n
d
m
o
d
999911658
\sum_{d|n}C^{d}_{n}\ mod\ 999911658
∑d∣nCnd mod 999911658,
分解质因数
999911658
=
2
∗
3
∗
4679
∗
35617
999911658=2*3*4679*35617
999911658=2∗3∗4679∗35617。
设
a
1
,
a
2
,
a
3
,
a
4
为
∑
d
∣
n
C
n
d
a_1,a_2,a_3,a_4为{\sum_{d|n}C^{d}_{n}}
a1,a2,a3,a4为∑d∣nCnd对
2
,
3
,
4679
,
35617
2,3,4679,35617
2,3,4679,35617四个取模的结果,求解线性同余方程组:
{
x
m
o
d
2
=
a
1
x
m
o
d
3
=
a
2
x
m
o
d
4679
=
a
3
x
m
o
d
35617
=
a
4
\left\{\begin{matrix} x\ mod\ 2=a_1\\ x\ mod\ 3=a_2\\ x\ mod\ 4679=a_3\\ x\ mod\ 35617=a_4 \end{matrix}\right.
⎩⎪⎪⎨⎪⎪⎧x mod 2=a1x mod 3=a2x mod 4679=a3x mod 35617=a4
即可得到
∑
d
∣
n
C
n
d
m
o
d
999911658
\sum_{d|n}C^{d}_{n}\ mod\ 999911658
∑d∣nCnd mod 999911658的最小非负整数解,快速幂一下就好了。
代码
#include<cstdio>
int q, n, ans;
int m[] = {0, 2, 3, 4679, 35617}, a[5], fac[35618];
int power(int a, int b, int p) {
long long res = 1;
for (; b; b >>= 1) {
if (b & 1) res = res * a % p;
a = (long long)a * a % p;
}
return res;
}
void init(int p) {
fac[0] = 1;
for (int i = 1; i < p; i++)
fac[i] = fac[i - 1] * i % p;
}
long long C(int n, int m, int p) {
if (n < m) return 0;
return fac[n] * power(fac[m], p - 2, p) % p * power(fac[n - m], p - 2, p) % p;
}
long long lucas(int n, int m, int p) {
if (!m) return 1;
return C(n % p, m % p, p) * lucas(n / p, m / p, p) % p;
}
int main() {
scanf("%d %d", &n, &q);
if (q % 999911659 == 0) {
printf("0");
return 0;
}
for (int i = 1; i <= 4; i++) {
init(m[i]);
for (int j = 1; j * j <= n; j++) {
if (n % j) continue;
a[i] = (a[i] + lucas(n, j, m[i])) % m[i];
if (n / j != j) a[i] = (a[i] + lucas(n, n / j, m[i])) % m[i];
}
}
for (int i = 1; i <= 4; i++) {
int M = 999911658 / m[i];
ans = (ans + (long long)a[i] * M * power(M, m[i] - 2, m[i])) % 999911658;
}
printf("%d", power(q, ans, 999911659));
}