【BZOJ1856】[Scoi2010]字符串【组合数】【神题】

本文提供了一种使用组合数学原理解决特定编程竞赛问题的方法。通过快速幂运算和逆元计算,该算法有效地计算了两个整数集合的所有可能组合,避免了直接使用阶乘带来的大数问题。此解决方案适用于那些需要高效计算组合数的场景。

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

【题目链接】

太神了【将狼踩尽的题解】

/* Telekinetic Forest Guard */
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long LL;

const int maxn = 2000001, p = 20100403;

int n, m, fact[maxn];

inline int mul(int a, int b) {
	return (LL)a * b % p;
}

inline int qpow(int x, int n) {
	int s = 1;
	for(int t = x; n; n >>= 1, t = mul(t, t)) if(n & 1) s = mul(s, t);
	return s;
}

int main() {
	fact[0] = 1;
	for(int i = 1; i < maxn; i++) fact[i] = mul(fact[i - 1], i);
	scanf("%d%d", &n, &m);
	printf("%d\n", (mul(mul(fact[n + m], qpow(fact[n], p - 2)), qpow(fact[m], p - 2)) - mul(mul(fact[n + m], qpow(fact[n + 1], p - 2)), qpow(fact[m - 1], p - 2)) + p) % p);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值