题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1951
题目分析:又是一道把几个数论算法强行拼起来的题目。
题面要求的其实是 g∑d|nCdnmodp g ∑ d | n C n d mod p 。用扩展欧拉定理可以变成 g(∑d|nCdn)modϕ(p)+ϕ(p)modp g ( ∑ d | n C n d ) mod ϕ ( p ) + ϕ ( p ) mod p 。写个程序发现 p=999911659 p = 999911659 是质数,然后写个质因数分解发现 p−1=999911658=2∗3∗4679∗35617 p − 1 = 999911658 = 2 ∗ 3 ∗ 4679 ∗ 35617 。于是对每个质数用Lucas定理求,然后CRT合并就好了。
刚刚逛网页的时候忽然发现了某dalao的blog,然后感觉自己还是太弱了。
CODE:
#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=50000;
const int M[4]={2,3,4679,35617};
int fac[4][maxn];
int nfac[4][maxn];
int p=M[0]*M[1]*M[2]*M[3];
int v[4];
int g,n;
int Pow(int x,int y,int z)
{
if (!y) return 1;
long long temp=Pow(x,y>>1,z);
temp=temp*temp%z;
if (y&1) temp=temp*x%z;
return temp;
}
int C(int n,int m,int x)
{
if ( n<M[x] && m<M[x] )
return (long long)fac[x][n]*nfac[x][m]%M[x]*nfac[x][n-m]%M[x];
return (long long)C(n/M[x],m/M[x],x)*C(n%M[x],m%M[x],x)%M[x];
}
int Work(int d)
{
for (int i=0; i<4; i++) v[i]=C(n,d,i);
int ans=0;
for (int i=0; i<4; i++)
ans=(ans+ (long long)v[i]*(p/M[i])%p*Pow(p/M[i],M[i]-2,M[i])%p )%p;
return ans;
}
int main()
{
//freopen("1951.in","r",stdin);
//freopen("1951.out","w",stdout);
for (int i=0; i<4; i++)
{
int x=M[i];
fac[i][0]=1;
for (int j=1; j<x; j++) fac[i][j]=fac[i][j-1]*j%x;
for (int j=0; j<x; j++) nfac[i][j]=Pow(fac[i][j],x-2,x);
}
scanf("%d%d",&n,&g);
int sn=(int)floor( sqrt( (double)n )+1e-4 );
int ans=0;
for (int i=1; i<=sn; i++) if (n%i==0)
{
ans=(ans+ Work(i) )%p;
if (i!=n/i) ans=(ans+ Work(n/i) )%p;
}
ans+=p;
ans=Pow(g,ans,p+1);
printf("%d\n",ans);
return 0;
}