标签:
program1 n!%P(P为质数)
#include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<utility> #include<set> #include<bitset> #include<vector> #include<functional> using namespace std; typedef long long LL; typedef double DB; typedef pair<int,int> PII; #define mmst(a,v) memset(a,v,sizeof(a)) #define mmcy(a,b) memcpy(a,b,sizeof(a)) #define re(i,a,b) for(i=a;i<=b;i++) #define red(i,a,b) for(i=a;i>=b;i--) #define fi first #define se second template<class T>inline T sqr(T x){return x*x;} template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;} template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-9; inline int dblcmp(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;} inline void SetOpen(string s) { freopen((s+".in").c_str(),"r",stdin); freopen((s+".out").c_str(),"w",stdout); } inline int Getin_Int() { int res=0,flag=1;char z; for(z=getchar();z!=EOF && z!=‘-‘ && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z==‘-‘){flag=-flag;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-‘0‘,z=getchar()); return res*flag; } inline LL Getin_LL() { LL res=0,flag=1;char z; for(z=getchar();z!=EOF && z!=‘-‘ && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z==‘-‘){flag=-flag;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-‘0‘,z=getchar()); return res*flag; } const LL maxpici=100000; const LL maxcnt=100; LL n,m,P; LL cnt,p[maxcnt+10],c[maxcnt+10],mod[maxcnt+10],euler[maxcnt+10],fac[maxcnt+10][maxpici+10]; LL ans; inline LL power(LL a,LL b,LL Mod) { LL x=1,y=a; while(b!=0){if(b&1)x=x*y%Mod;if(b==1)break;y=y*y%Mod;b/=2;} return x; } inline LL calfac(LL N,LL id,LL &g) { if(N<p[id])return fac[id][N]; LL s=N/mod[id],t=N%mod[id],res=1; res=res*power(fac[id][mod[id]-1],s,mod[id])%mod[id]; res=res*fac[id][t]%mod[id]; g+=N/p[id]; res=res*calfac(N/p[id],id,g)%mod[id]; return res; } inline LL solve(LL id) { LL g1=0,g2=0,g3=0; LL fenzi=calfac(m,id,g1),fenmo=calfac(n,id,g2)*calfac(m-n,id,g3)%mod[id]; return fenzi*power(fenmo,euler[id]-1,mod[id])%mod[id]*power(p[id],g1-g2-g3,mod[id])%mod[id]; } inline void extend_gcd(LL a,LL &x,LL b,LL &y) { if(b==0){x=1;y=0;return;} LL dx,dy; extend_gcd(b,dx,a%b,dy); x=dy; y=dx-a/b*dy; } int main() { SetOpen("program"); LL i,j,temp; m=Getin_Int();n=Getin_Int();P=Getin_Int(); if(m<n){puts("0\n");return 0;} temp=P; re(i,2,maxpici)if(temp%i==0) { ++cnt; p[cnt]=i; mod[cnt]=1; while(temp%i==0){c[cnt]++;mod[cnt]*=i;temp/=i;} euler[cnt]=mod[cnt]/p[cnt]*(p[cnt]-1); fac[cnt][0]=1; re(j,1,mod[cnt]-1)fac[cnt][j]=(j%p[cnt]==0)?fac[cnt][j-1]:fac[cnt][j-1]*j%mod[cnt]; } ans=0; re(i,1,cnt) { temp=solve(i); LL a=P/mod[i],x,b=mod[i],y; extend_gcd(a,x,b,y); ans=(ans+temp*a%P*x%P)%P; } ans=(ans%P+P)%P; cout<<ans<<endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/maijing/p/4649441.html