题解
欧拉定理(模指数)+质因数分解(条件)->lucas定理(组合数)+中国剩余定理(合并)
要说的一点就是一定要给自己写一个好看的代码,不然就根本没办法调试,就像我原来的30分。。。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
inline ll ksm(ll a,ll b,ll p){
ll ans=1;
a%=p;
while(b){
if(b&1){
ans=(ans*a)%p;
}
a=(a*a)%p;
b>>=1;
}
return ans;
}
ll inv[50001];
ll jie[50001];
ll a[5];
ll lucas(ll n,ll m,ll p){
if(m>n)return 0;
if(m<p&&n<p)return jie[n]*inv[jie[n-m]]%p*inv[jie[m]]%p;
return lucas(n%p,m%p,p)%p*lucas(n/p,m/p,p)%p;
}
ll N;
ll solve(ll p){
inv[1]=1;
inv[0]=1;
jie[1]=1;
jie[0]=1;
for(int i=2;i<=50000;i++){
jie[i]=(jie[i-1]*i)%p;
inv[i]=(p-p/i)*inv[p%i]%p;
}
ll ans=0;
for(ll k=1;k<=sqrt(N);k++){
if(N%k==0){
ans=(ans+lucas(N,k,p))%p;
ll k2=N/k;
if(k2==k)continue;
ans=(ans+lucas(N,k2,p))%p;
}
}
return ans%p;
}
ll prime[5]={0,2,3,4679,35617};
ll p=999911659;
ll G,P;
ll CRT(){
ll M=p-1;
ll ans=0;
for(int i=1;i<=4;i++){
ll m=M/prime[i];
ll t=ksm(m,prime[i]-2,prime[i]);
ans=(ans+a[i]*m*t)%M;
}
return ans%M;
}
int main(){
scanf("%lld %lld",&N,&G);
if(G%p==0){
printf("0");
return 0;
}
for(int i=1;i<=4;i++){
a[i]=solve(prime[i]);
}
P=CRT();
printf("%lld",ksm(G,P,p));
return 0;
}