分析
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=999911659;
ll n,g;
ll b[4]={2,3,4679,35617},a[5],fr[40000];
ll sum[4];
void init(ll x)
{
fr[0]=1;
for(int i=1;i<=x;i++)
fr[i]=(fr[i-1]*i)%x;
}
ll qpow(ll a,ll b, ll p)
{
ll res=1;
for(;b;b>>=1)
{
if(b&1)
res=res*a%p;
a=a*a%p;
}
return res;
}
ll C(ll n, ll m,ll p)
{
if(n<m) return 0;
return fr[n]*qpow(fr[m],p-2,p)%p*qpow(fr[n-m],p-2,p)%p;
}
ll lucas(ll n,ll m, ll p)
{
if(n<m) return 0;
if(!n) return 1;
return lucas(n/p,m/p,p)*C(n%p,m%p,p)%p;
}
ll ans;
void crt()
{
for(int i=0;i<4;i++)
ans=(ans+a[i]*((mod-1)/b[i])%mod*qpow((mod-1)/b[i],b[i]-2,b[i]))%(mod-1);
}
int main()
{
//freopen("pig.in","r",stdin);
//freopen("pig.out","w",stdout);
scanf("%lld%lld",&n,&g);
if(g%mod==0)
{
printf("0");
return 0;
}
for(int i=0;i<4;i++)
{
init(b[i]);
for(int j=1;j*j<=n;j++)
{
if(n%j==0)
{
a[i]=(a[i]+lucas(n,j,b[i]))%b[i];
if(j*j!=n)
a[i]=(a[i]+lucas(n,n/j,b[i]))%b[i];
}
}
}
crt();
printf("%lld\n",qpow(g,ans,mod));
return 0;
}