六:扩展欧几里得算法
同余:
若 a≡b(modm),则 m 整除 a−b,即 a=b+km(k 为整数)。
扩展欧几里得算法
扩展欧几里得算法可用于求解 ax+by=gcd(a,b) 的一组整数解。
#include <iostream>
using namespace std;
// 扩展欧几里得算法,用于求解 ax + by = gcd(a, b) 的一组整数解
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
// 判断方程 ax + by = c 是否有解,并尝试求解
bool solveLinearEquation(int a, int b, int c, int &x, int &y) {
int d = exgcd(a, b, x, y);
if (c % d != 0) {
// 如果 c 不是 gcd(a, b) 的倍数,则方程无解
return false;
}
int k = c / d;
// 得到方程 ax + by = c 的一组特解
x *= k;
y *= k;
return true;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
int x, y;
if (solveLinearEquation(a, b, c, x, y)) {
cout << "方程有解,一组解为: x = " << x << ", y = " << y << endl;
} else {
cout << "方程无解" << endl;
}
return 0;
}
七:组合数
1-:访问次数多,数值较小,使用动态规划法:
当访问次数较多且数据范围较小时,可利用组合数的递推公式
Cnm(n个中m个的组合)=Cn−1m(第n个不选,剩下的n-1个中选m个)+Cn−1m−1(选中n,剩m-1个在其余n-1中选)
进行动态规划求解。
#include <iostream>
using namespace std;
const int N = 2010, MOD = 1e9 + 7;
int c[N][N];
void init() {
for (int i = 0; i < N; i++) {
for (int j = 0; j <= i; j++) {
if (!j) c[i][j] = 1;
else c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD;
}
}
}
int main() {
init();
int n;
cin >> n;
while (n--) {
int a, b;
cin >> a >> b;
cout << c[a][b] << endl;
}
return 0;
}
2-:qmi求组合数(数据范围较大但是访问次数少)
若数据范围较大但访问次数少,可通过快速幂来计算阶乘的逆元,进而计算组合数。若数据较少但访问较多,也可预处理阶乘和阶乘逆元数组。
这里有一个可以把n logn 的时间复杂度降低为 n的一个初始化方法:
#include <iostream>
using namespace std;
typedef long long LL;
const int N = 100010, MOD = 1e9 + 7;
int 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;
}
// 预处理阶乘和阶乘逆元数组
void init() {
//求出fact (fact[i]表示i!)
intfact[0]=fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = (LL)fact[i - 1] * i % MOD;
}
//通过infact[i]=infact[i+1]×(i+1)mod p 对我们的球infact进行优化
infact[N - 1] = qmi(fact[N - 1], MOD - 2, MOD);
for (int i = N - 2; i >= 1; i--) {
infact[i] = (LL)infact[i + 1] * (i + 1) % MOD;
}
}
int main() {
init();
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-:Lucas 定理求组合数(数据量更大,但是模数是质数时)
当数据量更大时,可使用 Lucas 定理来计算组合数。Lucas 定理表述为:
Cnm≡(C(n mod p)(m mod p)×C(n/p)(m/p) )mod p,其中 p 为质数。
这里我们使用最基本的线性的方式求出一个组合数配合lucas定理来计算组合数
#include <iostream>
using namespace std;
typedef long long LL;
// 快速幂函数
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;
}
// 计算 C(a, b) % p
int C(int a, int b, int p) {
if (b > a) return 0;
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) % p;
}
return res;
}
// Lucas 定理
int lucas(LL a, LL b, int p) {
if (a < p && b < p) return C(a, b, p);//数据a,b比p小,直接返回答案
return (LL)C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;//小数据直接用C函数,大的继续调用lucas
}
int main() {
int n;
cin >> n;
while (n--) {
LL a, b;
int p;
cin >> a >> b >> p;
cout << lucas(a, b, p) << endl;
}
return 0;
}
-4:超大数据需要配合高精度乘法和加法
比如a,b都在1~10000的范围内
筛出质数:使用筛法(如埃氏筛法或线性筛法)找出 1 到 10000 内的所有质数。
分解质因数:分别对 a!、b! 和 (a−b)! 进行质因数分解(枚举质数然后获取其在这三个数的权重),统计每个质因数的指数。
计算组合数:通过 Cab 的质因数分解结果,使用高精度乘法计算最终的组合数。
#include <iostream>
#include <vector>
using namespace std;
const int N = 10010;
int primes[N], cnt;
bool st[N];
// 线性筛法求质数
void get_primes(int n) {
for (int i = 2; i <= n; i++) {
if (!st[i]) primes[cnt++] = i;
for (int j = 0; primes[j] <= n / i; j++) {
st[primes[j] * i] = true;
if (i % primes[j] == 0) break;
}
}
}
// 计算 n! 中质因数 p 的指数
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() || t; i++) {
if (i < a.size()) t += a[i] * b;
c.push_back(t % 10);
t /= 10;
}
while (c.size() > 1 && c.back() == 0) c.pop_back();
return c;
}
// 计算组合数 C(a, b)
vector<int> combination(int a, int b) {
get_primes(a);
vector<int> res(1, 1);
for (int i = 0; i < cnt; i++) {
int p = primes[i];
// 计算 C(a, b) 中质因数 p 的指数
int s = get(a, p) - get(b, p) - get(a - b, p);
// 累乘质因数 p 的 s 次幂
for (int j = 0; j < s; j++) {
res = mul(res, p);
}
}
return res;
}
int main() {
int a, b;
cin >> a >> b;
vector<int> res = combination(a, b);
for (int i = res.size() - 1; i >= 0; i--) {
cout << res[i];
}
cout << endl;
return 0;
}