p3087卢卡斯定理

数论的卢卡斯,据说可以只记结论啦啦啦啦。(反正我也不会~)

实际上也是好几个知识点的集合吧。

1。快速幂

ll pow(ll x,int y,int p){
	ll ans=1;
	x%=p;
	for(int i=y;i;i>>=1,x=x*x%p)	if(i&1)	ans=ans*x%p;
	return ans;
}

2。组合数求法

ll c(ll x,ll y){
	if(y>x)	return 0;
	return (a[x]*pow(a[y],p-2,p))%p*pow(a[x-y],p-2,p)%p;
}

a【i】是%p意义下的i的阶乘。(一种鬼算法)

好像还跟逆元有关cm(a,b)=(a!/(ab)!)(p2)mod p

逆元:a[i]=(p-p/i)*a[p%i](这个之前证过但懒得想了)

3。快读

inline int read(){
    int f=1,x=0;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
    return f*x;
}

试着自己打来着,写炸了。

4。卢卡斯定理

Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p)

所以代码是

#include <iostream>
#include <cstdio>
//#define  ll long long
using namespace std;
typedef long long ll;
ll a[100005];
int p;

ll pow(ll x,int y,int p){
	ll ans=1;
	x%=p;
	for(int i=y;i;i>>=1,x=x*x%p)	if(i&1)	ans=ans*x%p;
	return ans;
}

ll c(ll x,ll y){
	if(y>x)	return 0;
	return (a[x]*pow(a[y],p-2,p))%p*pow(a[x-y],p-2,p)%p;
}

ll lucas(ll n,ll m){
	if(!m)	return 1;
	return c(n%p,m%p)*lucas(n/p,m/p)%p;
}

inline int read(){
    int f=1,x=0;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
    return f*x;
}
int main(){
	int t=read();
	while(t--){
		int n=read(),m=read();p=read();//p一开始定义在里面了,结果调了半天
		a[0]=1;
		for(int i=1;i<=p;i++)	a[i]=(a[i-1]*i)%p;
		printf("%d\n",lucas(n+m,n));
	}
	return 0;
} 

  我肯定还会忘的。。。

转载于:https://www.cnblogs.com/jindui/p/11188721.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值