求组合数
1:如果a、b小于2000:可以预处理
#include<iostream>
using namespace std;
const int maxn = 2010;
const int mod = 1e9 + 7;
int c[maxn][maxn];
void init() {
int n = 2000;
for (int i = 0; i <= n; i++) c[i][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
}
int main() {
init();
int T, a, b;
cin >> T;
while (T--) {
cin >> a >> b;
cout << c[a][b] << endl;
}
return 0;
}
2.如果a、b达到1e5 mod p
可以求逆元(费马小定理)
#include<iostream>
using namespace std;
const long long maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
typedef long long ll;
ll f[maxn], invf[maxn];
ll qpow(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
void init() {
f[0] = 1;
invf[0] = 1;
for (int i = 1; i <= 100001; i++) f[i] = (f[i - 1] * i) % mod;
for (int i = 1; i <= 100001; i++) invf[i] = invf[i - 1] * qpow(i, mod - 2) % mod;
}
int main() {
init();
int T, n, m;
cin >> T;
while (T--) {
cin >> n >> m;
cout << f[n] * invf[n - m] % mod * invf[m] % mod<< endl;
}
return 0;
}
3.如果a、b达到1e18,但模数p比较小(1e5)、
利用Lucas定理递归求解,转换成1e5的情况,转化为问题2(这里的p每次不一样,直接用定义算即可)
#include <iostream>
using namespace std;
typedef long long LL;
int p;
int qmi(int a, int b) {
int res = 1;
for (; b; b >>= 1) {
if (b & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
}
return res;
}
int C(int a, int b) {
int res = 1;
for (int i = 1, j = a; i <= b; i++, j--) {
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);
return (LL)C(a % p, b % p) * lucas(a / p, b / p) % p;
}
int main() {
int T;
cin >> T;
while (T--) {
LL a, b;
cin >> a >> b >> p;
cout << lucas(a, b) << endl;
}
return 0;
}
4.a、b达到5000,不取模
换个支持高精度的语言(bushi)
预处理a以内的质数,对组合数进行巧妙的因式分解(详见get()函数),然后把所有质因子暴力相乘(
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 5010;
bool st[N];
int prime[N], sum[N], cnt;
void eulor(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) prime[++cnt] = i;
for (int j = 1; prime[j] <= n / i; j++) {
st[prime[j] * i] = true;
if (i % prime[j]) break;
}
}
}
int get(int n, int p) {
int res = 0;
while(n) {
res += n / p;
n /= 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;
eulor(a);
for (int i = 1; i <= cnt; i++) {
int p = prime[i];
sum[i] = get(a, p) - get(b, p) - get(a - b, p);
}
vector<int> res;
res.push_back(1);
for (int i = 1; i <= cnt; i++)
for (int j = 1; j <= sum[i]; j++)
res = mul(res, prime[i]);
for (int i = res.size() - 1; i >= 0; i--) cout << res[i];
return 0;
}
应用:求卡特兰数
经验:maxn开成两倍最大范围比较好QAQ
#include<iostream>
using namespace std;
const long long maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
typedef long long ll;
ll f[maxn], invf[maxn];
ll qpow(ll a, ll b) {
ll ans = 1;
for (; b; b >>= 1) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
void init() {
f[0] = 1;
invf[0] = 1;
for (int i = 1; i <= 100001; i++) f[i] = (f[i - 1] * i) % mod;
for (int i = 1; i <= 100001; i++) invf[i] = invf[i - 1] * qpow(i, mod - 2) % mod;
}
signed main() {
init();
int n;
cin >> n;
cout << f[n * 2] * invf[n] % mod * invf[n] % mod << endl;
return 0;
}