【FZUOJ 2020】 组合

lucas的应用!!
中间组合数学开始竟然看不懂!!!! 约掉了一部分啊!
还用到 逆元 欧拉定理之类的(就一句话 没有名字这样高大上)

#include <cstdio>
#define LL long long
LL F(LL x,LL y,LL mo)
{
	LL ans=1;
	while(y)
	{
		if(y&1) ans=ans*x%mo;
		x=x*x%mo;
		y>>=1;
	}
	return ans;
}
LL C(LL n,LL m,LL p)
{
	LL x=1,y=1;
	for(LL i=1;i<=m;i++)
		x=x*(n-i+1)%p,y=y*i%p;
	return x*F(y,p-2,p)%p;
}
LL Lucas(LL n,LL m,LL p)
{
	if(m==0) return 1;
	return C(n%p,m%p,p)*Lucas(n/p,m/p,p)%p;
}
int main()
{
	int T;scanf("%d",&T);
	while(T--)
	{
		LL m,n,p;
		scanf("%I64d %I64d %I64d",&m,&n,&p);
		printf("%I64d\n",Lucas(m,n,p));
	}
	return 0;
}

posted @ 2016-01-11 16:49  sxb_201  阅读(206)  评论(0编辑  收藏  举报