中国剩余定理+Lucas定理
题目就是要求 G∑i|nCinmodp
然后费马小定理一下,变成求指数 modp−1
模数是合数,拆开来对每一个小质因数做一遍,最后CRT合并一下即可。
注意费马小定理
ap−1≡1(modp)
成立,当且仅当
p
是质数,且
#include<cstdio>
#include<cmath>
#define MOD 999911658
#define N 35618
using namespace std;
namespace runzhe2000
{
typedef long long ll;
int n, G, m[4] = {2,3,4679,35617}, a[4], fac[4][N], inv[4][N];
int C(int a, int b, int i)
{
if(a < b) return 0;
else if(a < m[i]) return (ll)fac[i][a] * inv[i][b] * inv[i][a-b] % m[i];
else return (ll)C(a/m[i], b/m[i], i) * C(a%m[i], b%m[i], i) % m[i];
}
int fpow(int a, int b, int mod)
{
if(!(a%=(MOD+1)))return 0;
int r = 1;
for(; b; b>>=1)
{
if(b&1)r=(ll)r*a%mod;
a=(ll)a*a%mod;
}
return r;
}
int CRT()
{
int r = 0;
for(int i = 0; i < 4; i++)
{
int Mi = MOD / m[i];
int tmp = (ll)a[i] * Mi % MOD * fpow(Mi, m[i]-2, m[i]) % MOD;
(r += tmp) %= MOD;
}
return r;
}
void main()
{
scanf("%d%d",&n,&G);
for(int j = 0; j < 4; j++)
{
int *f = fac[j], *v = inv[j], mod = m[j];
f[0] = 1; for(int i = 1; i < mod; i++) f[i] = (ll) f[i-1] * i % mod;
v[1] = 1; for(int i = 2; i < mod; i++) v[i] = (ll) (mod-mod/i)*v[mod%i] % mod;
v[0] = 1; for(int i = 1; i < mod; i++) v[i] = (ll) v[i] * v[i-1] % mod;
}
for(int j = 1, jj = sqrt((double)n); j <= jj; j++)
if(n % j == 0)
{
for(int i = 0; i < 4; i++)
{
(a[i] += C(n, j, i)) %= m[i];
if(n / j != j) (a[i] += C(n, n/j, i)) %= m[i];
}
}
int a = CRT();
printf("%d\n",fpow(G, a, MOD+1));
}
}
int main()
{
runzhe2000::main();
}