组合数模板

博客介绍了不同范围下组合数的求解方法。当 n,m <= 1e8 时,可直接递推求 1 至 1e8 的阶乘及对应的逆元,再计算组合数;当 n,m > 1e8 时,使用 Lucas 定理求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 对于 n,m <= 1e8

直接递推求 1 ~ 1e8的阶乘 frac[ i ]

求出对应的逆元 carf[ i ]

C( n,m )  = frac[ n ] * carf[ n-m ]*carf[ m ]

typedef unsigned long long ll;
#define maxn 1002000
const ll oo=1000003;
ll frac[maxn], carf[maxn];

ll inv(ll x){
	if (x==1) return 1;
	else return (oo-oo/x)*inv(oo%x)%oo;
}
void init(){
	frac[0]=1;
	for (int i=1;i<=1000000;i++)
		frac[i]=frac[i-1]*i%oo;
	carf[1000000]=inv(frac[1000000]);
	for (int i=1000000;i>=1;i--)
		carf[i-1]=carf[i]*i%oo;
}

ll C(int n, int m){
	if (m>n) return 0;
	else return frac[n]*carf[n-m]%oo*carf[m]%oo;
}

对于 n,m > 1e8

Lucas 定理

ll lucas(ll n,ll m){
	if (m==0) return 1;
	if (m>n) return 0;
	return C(n%oo,m%oo)*lucas(n/oo,m/oo)%oo;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值