求组合数 1
给定 n 组询问,每组询问给定两个整数 a,b,请你输出 C(a,b) mod (109+7) 的值。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一组 a 和 b。
输出格式
共 n 行,每行输出一个询问的解。
数据范围
1 ≤ n ≤ 10000,
1 ≤ b ≤ a ≤ 2000
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 2010, mod = 1e9 + 7;
int c[N][N];
int n;
//初始化数组,直接进行调用
void init() {
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
if (!j) c[i][j] = 1; //当j为0时只有一种可能,即组合数为1
else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; //不为0时通过公式对整个数组进行初始化
}
}
}
int main() {
init();
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
cout << c[a][b] << endl;
}
return 0;
}
求组合数 2
给定 n 组询问,每组询问给定两个整数 a,b,请你输出 C(a,b) mod (109+7) 的值。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一组 a 和 b。
输出格式
共 n 行,每行输出一个询问的解。
数据范围
1 ≤ n ≤ 10000,
1 ≤ b ≤ a ≤ 10^5
tips:快速幂求逆元见 ztjou.st 传送门
#include<iostream>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10, mod = 1e9 + 7;
LL fact[N], infact[N];
//定义快速幂
int qmi(int a, int k, int p) {
int res = 1;
while (k) {
if (k & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int main() {
//首先初始化阶乘和逆元数组
fact[0] = infact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = (LL)fact[i - 1] * i % mod;
infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod; //qmi(i, mod - 2, mod)表示i的逆元
}
int n;
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
cout << (LL)fact[a] * infact[b] % mod * infact[a - b] % mod << endl;
}
return 0;
}
求组合数3
给定 n 组询问,每组询问给定三个整数 a,b,p,其中 p 是质数,请你输出 C(a,b) mod p 的值。
输入格式
第一行包含整数 n。
接下来 n 行,每行包含一组 a,b,p。
输出格式
共 n 行,每行输出一个询问的解。
数据范围
1 ≤ n ≤ 20,
1 ≤ b ≤ a ≤ 10^18,
1 ≤ p ≤ 10^5,
卢卡斯定理:
#include<iostream>
using namespace std;
typedef long long LL;
int p;
//快速幂
int qmi(int a, int k) {
int res = 1;
while (k) {
if (k & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int C(int a, int b) {
int res = 1;
for (int i = 1, j = a; i <= b; i++, j--) { //在本次循环中,i从1到b,j从a到a-b+1; a!/(b!(a-b)!) = a*(a-1)*…*(a-b+1) / b!
res = (LL)res * j % p;
res = (LL)res * qmi(i, p - 2) % p; //通过快速幂求逆元
}
return res;
}
//卢卡斯定理
int lucas(LL a, LL b) {
if (a < p && b < p) return C(a, b);
else return (LL)C(a % p,b % p) * lucas(a / p, b / p) % p;
}
int main() {
int n;
cin >> n;
while (n--) {
LL a, b;
cin >> a >> b >> p;
cout << lucas(a, b) << endl;
}
}
注意在本代码求 C(a,b) 的过程中,运用到了公式: a! / (b! * (a-b)!) = a * (a-1) * … * (a-b+1) / b!
求组合数4
输入 a,b,求 Cba 的值。
注意结果可能很大,需要使用高精度计算。
输入格式
共一行,包含两个整数 a 和 b。
输出格式
共一行,输出 Cba 的值。
数据范围
1 ≤ b ≤ a ≤ 5000
题解:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 5010;
int primes[N], cnt; //cnt存的是质数的个数
int sum[N]; //sum[i]中所存的是对于每个i计算a/b*(a-b)中p的个数
bool st[N]; //st用来标记是否为非质数
//线性筛质数
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] * i <= n; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
//取a中p的个数
int get(int a, int p) {
int res = 0;
while (a) {
res += a / p;
a /= p;
}
return res;
}
//高精度乘法
vector<int> mul(vector<int> a, int b) {
vector<int> c;
int t = 0;
for (int i = 0; i < a.size(); i++) {
t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
while (t) {
c.push_back(t % 10);
t /= 10;
}
return c;
}
int main() {
int a, b;
cin >> a >> b;
get_primes(a);
for (int i = 0; i < cnt; i++) { //所循环的次数为cnt的大小,也就是所有质数的个数
int p = primes[i];
sum[i] = get(a, p) - get(b, p) - get(a - b, p); //将对每个i中所有的p的个数运算后的个数存在sum数组中
}
vector<int> res;
res.push_back(1);
for (int i = 0; i < cnt; i++) { //外层循环的次数为总的质数的个数
for (int j = 0; j < sum[i]; j++) { //内层循环的次数为对于每个i中p的个数(p的个数即为primes[i]的个数
res = mul(res, primes[i]);
}
}//所达到的最终结果即对结果res乘以每个质数的sum[i]次方
for (int i = res.size() - 1; i >= 0; i--) {
cout << res[i]; //倒序输出
}
return 0;
}
满足条件的01序列(卡特兰数)
给定 n 个 0 和 n 个 1,它们将按照某种顺序排成长度为 2n 的序列,求它们能排列成的所有序列中,能够满足任意前缀序列中 0 的个数都不少于 1 的个数的序列有多少个。
输出的答案对 109+7 取模。
输入格式
共一行,包含整数 n。
输出格式
共一行,包含一个整数,表示答案。
数据范围
1 ≤ n ≤ 10^5
注意题上所要求的是0的个数不少于1的个数的序列
关于卡特兰数的应用:(在数轴上表示即为0代表往右走一位,1代表往上走一位)
要想使0的个数不少于1的个数则必须在图中绿线上或绿线的下方,也就是整个红线的下方
#include<iostream>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10, mod = 1e9 + 7;
int n;
int fact[N], infact[N];
//定义快速幂
int qmi(int a, int k) {
int res = 1;
while (k) {
if (k & 1) res = (LL)res * a % mod;
a = (LL)a * a % mod;
k >>= 1;
}
return res;
}
//对fact和infact数组进行初始化
void init() {
fact[0] = infact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = (LL)fact[i - 1] * i % mod;
infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2) % mod;
}
}
int main() {
init();
cin >> n;
int res = (LL)fact[2 * n] * infact[n] % mod * infact[n] % mod * qmi(n + 1, mod - 2) % mod;
cout << res << endl;
return 0;
}