忘了讨论g=P的情况了qaq
然后加上以后又忘了写return 0了qaq
所以愉快地wa了几发
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
typedef long long LL;
inline int read()
{
int x=0;bool f=0;char c=getchar();
for (;c<'0'||c>'9';c=getchar()) f=c=='-'?1:0;
for (;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';
return f?-x:x;
}
const int P[4]={2,3,4679,35617},mod=999911659;
LL n,g,ans=0,x[4],fac[40010],fc=0,p,ftc[40010];
inline void add(LL &a,LL b)
{
a+=b;
if (a>=p) a-=p;
}
inline LL qpw(LL a,LL k,LL p)
{
LL rec=1;
while (k)
{
if (k&1) rec=(rec*a)%p;
a=(a*a)%p;k>>=1;
}
return rec;
}
LL cal_c(LL a,LL b)
{
if (a<b) return 0;
if (a<p&&b<p) return ftc[a]*qpw(ftc[b],p-2,p)%p*qpw(ftc[a-b],p-2,p)%p;
return cal_c(a%p,b%p)*cal_c(a/p,b/p)%p;
}
LL cal()
{
ftc[0]=1;
for (int i=1;i<p;i++) ftc[i]=ftc[i-1]*i%p;
LL rec=0;
for (int i=1;i<=fc;i++) add(rec,cal_c(n,fac[i]));
return rec;
}
int main()
{
n=read();g=read();
if (g==mod) return puts("0"),0;
for (LL i=1;i*i<=n;i++) if (n%i==0)
{
fac[++fc]=i;
if (i*i!=n) fac[++fc]=n/i;
}
sort(fac+1,fac+fc+1);
for (int i=0;i<4;i++) p=P[i],x[i]=cal();
p=mod-1;
for (int i=0;i<4;i++)
{
LL k=(mod-1)/P[i];
k=k*qpw(k,P[i]-2,P[i]);
add(ans,k*x[i]%p);
}
ans=qpw(g,ans,mod);
printf("%lld\n",ans);
return 0;
}