# BZOJ1951：[Sdoi2010]古代猪文 （扩展欧拉定理+Lucas定理+CRT）

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;
}

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客