有趣的数列

这两段代码展示了两种不同的C++实现方式来计算组合数,即n的阶乘除以k的阶乘。第一种方法通过Euler筛法找出所有质因数,并计算每个质因数在n!中出现的次数。第二种方法使用快速幂运算优化计算过程,先计算n!和(k-1)!,然后相除。这两种方法都适用于大整数计算,并能避免溢出问题。

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

1.为什么是卡特兰数


2.如何求组合数

应该是比较简单的。

way 1: 分解质因数


way 2: 求质数在 n! 中出现了多少次 (faster)


code 1:

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); i++)
#define per(i,j,k) for (int i = (j); i >= (k); i--)

template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
	x = 0; T f = 1;
	char ch = getchar ();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar ();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar ();
	}
	x *= f;
}
char For_Print[25];
template <typename T>
void write (T x) {
	if (x == 0) { putchar ('0'); return; }
	if (x < 0) { putchar ('-'); x = -x; }
	int poi = 0;
	while (x) {
		For_Print[++poi] = x % 10 + '0';
		x /= 10;
	}
	while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
	write (x); putchar (ch);
}

const int Maxn = 1e6 * 2;

LL n, p;
int bak[Maxn + 5];

int cnt, primes[Maxn + 5];
bool vis[Maxn + 5];
void Euler () {
	rep (i, 2, Maxn) {
		if (vis[i] == 0) {
			vis[i] = 1;
			primes[++cnt] = i;
		}
		rep (j, 1, cnt) {
			if (primes[j] > Maxn / i) break;
			vis[i * primes[j]] = 1;
			if (i % primes[j] == 0) break;
		}
	}
}

int main () {
	// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.in", "r", stdin);
	// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.out", "w", stdout);

	Euler ();

	read (n); read (p);
	
	rep (i, 1, cnt) {
		int x = primes[i];
		for (int j = x; j <= n * 2; j += x) {
			int tmp = j;
			if (1 <= j && j <= n)
				while (tmp % x == 0) {
					bak[x]--;
					tmp /= x;
				}
			else if (n + 2 <= j && j <= n * 2)
				while (tmp % x == 0) {
					bak[x]++;
					tmp /= x;
				}
		}
	}

	LL res = 1;
	rep (i, 1, cnt) 
		res = (res * (LL)pow (primes[i], bak[primes[i]])) % p;
	write (res);
	return 0;
}

code 2:

#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstdlib> 
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define ULL unsigned long long
#define PII pair <int, int>
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); i++)
#define per(i,j,k) for (int i = (j); i >= (k); i--)

template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }
template <typename T>
void read (T &x) {
	x = 0; T f = 1;
	char ch = getchar ();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar ();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar ();
	}
	x *= f;
}
char For_Print[25];
template <typename T>
void write (T x) {
	if (x == 0) { putchar ('0'); return; }
	if (x < 0) { putchar ('-'); x = -x; }
	int poi = 0;
	while (x) {
		For_Print[++poi] = x % 10 + '0';
		x /= 10;
	}
	while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
	write (x); putchar (ch);
}

const int Maxn = 1e6 * 2;

LL n, p;
int bak[Maxn + 5];

int cnt, primes[Maxn + 5];
bool vis[Maxn + 5];
void Euler () {
	rep (i, 2, n * 2) {
		if (vis[i] == 0) {
			vis[i] = 1;
			primes[++cnt] = i;
		}
		rep (j, 1, cnt) {
			if (primes[j] > n * 2 / i) break;
			vis[i * primes[j]] = 1;
			if (i % primes[j] == 0) break;
		}
	}
}
LL quick_pow (LL x, LL y) {
	LL res = 1;
	while (y) {
		if (y & 1) res = (res * x) % p;
		x = (x * x) % p; y >>= 1;
	}
	return res;
}
void Calc (LL x, int op) {
	rep (i, 1, cnt) {
		if (primes[i] > x) break;
		for (LL j = primes[i]; j <= x; j *= primes[i])
			bak[primes[i]] += op * (x / j);
	}
}

int main () {
	// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.in", "r", stdin);
	// freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.out", "w", stdout);

	read (n); read (p);
	
	Euler ();
	
	Calc (n * 2, 1);
	Calc (n + 1, -1);
	Calc (n, -1);

	LL res = 1;
	rep (i, 1, cnt) {
		if (primes[i] > n * 2) break;
		res = (res * quick_pow (primes[i], bak[primes[i]])) % p;
	}
	write (res);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值