C(m,n)%P
program1 n!%P(P为质数)

我们不假思索地认为g1-g2-g3>=0(否则C(m,n)算出来就是分数了),而且容易知道calcfac(n)*falcfac(m-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-;
inline int dblcmp(DB x){if(abs(x)<EPS)return ;return(x>)?:-;} 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=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
}
inline LL Getin_LL()
{
LL res=,flag=;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return ;
if(z=='-'){flag=-flag;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*+z-'',z=getchar());
return res*flag;
} const LL maxpici=;
const LL maxcnt=; LL n,m,P;
LL cnt,p[maxcnt+],c[maxcnt+],mod[maxcnt+],euler[maxcnt+],fac[maxcnt+][maxpici+];
LL ans; inline LL power(LL a,LL b,LL Mod)
{
LL x=,y=a;
while(b!=){if(b&)x=x*y%Mod;if(b==)break;y=y*y%Mod;b/=;}
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=;
res=res*power(fac[id][mod[id]-],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=,g2=,g3=;
LL fenzi=calfac(m,id,g1),fenmo=calfac(n,id,g2)*calfac(m-n,id,g3)%mod[id];
return fenzi*power(fenmo,euler[id]-,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==){x=;y=;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 ;}
temp=P;
re(i,,maxpici)if(temp%i==)
{
++cnt;
p[cnt]=i;
mod[cnt]=;
while(temp%i==){c[cnt]++;mod[cnt]*=i;temp/=i;}
euler[cnt]=mod[cnt]/p[cnt]*(p[cnt]-);
fac[cnt][]=;
re(j,,mod[cnt]-)fac[cnt][j]=(j%p[cnt]==)?fac[cnt][j-]:fac[cnt][j-]*j%mod[cnt];
}
ans=;
re(i,,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 ;
}
随机推荐
- hdu 5389 Zero Escape(记忆化搜索)
Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi ...
- jQuery插件开发 格式与解析3之$.extend()用途
前叙:$.extend()——用途:扩展和继承 1.Object extend() 用一个或多个对象扩展另一个对象,并返回已修改的原始对象.这对于简单继承是一个非常有用的实用工具. (1)扩展:(Do ...
- 用python演示一个简单的AST(抽象语法树)
如果对'a + 3 * b'进行解释,当中a=2,b=5 代码非常easy,就不再进行具体的解释了. Num = lambda env, n: n Var = lambda env, x: env[x ...
- 【转】 LINQ To SQL 语法及实例大全
LINQ to SQL语句(1)之Where Where操作 适用场景:实现过滤,查询等功能. 说明:与SQL命令中的Where作用相似,都是起到范围限定也就是过滤作用的,而判断条件就是它后面所接的子 ...
- FileGeodatabase和PersonalGeodatabase与ArcSDEGeodatabase三种数据库比较.
- iOS开发之17个常用代码整理
http://www.cnblogs.com/ios8/p/ios-17-code.html
- DKNightVersion的基本使用(夜间模式)
DKNightVersion下载地址: https://github.com/Draveness/DKNightVersion 基本原理就是利用一个单例对象来存储颜色, 然后通过runtime中的ob ...
- Java中int和String互相转换的多种方法
1 如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([ ...
- spring的定时任务或者说自动任务
http://kevin19900306.iteye.com/blog/1397744 //这个是别人的博客 引入除spring.jar外的Quartz的jar包quartz-all-1.8. ...
- Guava 15新特性介绍
原文:http://www.javacodegeeks.com/2013/10/guava-15-new-features.html Guava 是众所周知的google出品的开源工具包,十分好用,本 ...