目录
前言
质数
在所有严格大于1的自然数(所有<=1的自然数既不是质数也不是合数),如果只包含1和本身这两个约数,就被叫为质数,或者叫素数。
算法思想:
(1)质数的判定——试除法:复杂度O(√n)
朴素做法:循环2—n判断n % i 是否 == 0,若有等于0则不只包含1和本身这两个约数,不为质数。时间复杂度为 O(n),较高
若d能整除n 则 n|d 也能整除 n,所以我们只需枚举 d <= n|d,变形得 d <= √n。同时这里有很多不同的写法:sqrt(n):每次都需要执行函数影响速度 ;i * i <=n:存在溢出风险,所以我们采用 i <= n / i 的写法进行枚举
(2)分解质因数——试除法:
从小到大枚举 n 的所有因数,n当中最多只包含一个大于√n的质因子,同样只需要枚举到√n
(3)朴素筛质数:将1—n中所有数 x 的倍数删掉(用bool数组标记),筛完之后剩下的数就是质数,时间复杂度为O(nlogn)
(4)优化筛质数——埃氏筛:对于朴素筛质数,我们只需要将质数的倍数删掉即可,时间复杂度为O(nloglogn)
(5)线性筛:n 只会被他的最小质因子筛掉,若不是质数,则加入数组,从小到大枚举所有的质数,将质数的倍数筛掉即可,时间复杂度为O
模板:
// 试除法判断素数
bool is_prime(int n)
{
if (n < 2) return false;
for (int i = 2; i <= n / i; i ++)
if (n % i == 0)
return false;
return true;
}
// 分解质因数
void divide(int n)
{
for (int i = 2; i <= n / i; i ++)
if (n % i == 0)
{
int s = 0;
while (n % i == 0)
{
s ++;
n /= i;
}
cout << i << ' ' << s << endl;
}
if (n > 1) cout << n << ' ' << 1 << endl;
cout << endl;
}
// 朴素筛质数
void get_primes(int n)
{
for (int i = 2; i <= n; i ++)
{
if (!st[i])
{
primes[cnt ++] = i;
}
for (int j = i + i; j <= n; j += i) st[j] = true; // i的倍数都标记true
}
}
// 埃氏筛
void get_primes(int n)
{
for (int i = 2; i <= n; i ++)
{
if (!st[i])
{
primes[cnt ++] = i;
for (int j = i + i; j <= n; j += i) st[j] = true; // i的倍数都标记true
}
}
}
// 线性筛
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; // primes[j] 一定是i的最小质因子
}
}
}
例题:
AcWing 866. 试除法判定质数
AcWing 867. 分解质因数
AcWing 868. 筛质数
AC代码:
// 试除法
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool is_prime(int n)
{
if (n < 2) return false;
for (int i = 2; i <= n / i; i ++)
if (n % i == 0)
return false;
return true;
}
int main()
{
int n;
cin >> n;
while (n --)
{
int a;
cin >> a;
if (is_prime(a)) cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
// 分解质因数
#include <iostream>
#include <algorithm>
using namespace std;
void divide(int n)
{
for (int i = 2; i <= n / i; i ++)
if (n % i == 0)
{
int s = 0;
while (n % i == 0)
{
s ++;
n /= i;
}
cout << i << ' ' << s << endl;
}
if (n > 1) cout << n << ' ' << 1 << endl;
cout << endl;
}
int main()
{
int n;
cin >> n;
while (n --)
{
int a;
cin >> a;
divide(a);
}
}
// 朴素筛质数
#include <iostream>
using namespace std;
const int N = 1e6 + 5;
int cnt;
int primes[N];
bool st[N];
void get_primes(int n)
{
for (int i = 2; i <= n; i ++)
{
if (!st[i])
{
primes[cnt ++] = i;
}
for (int j = i + i; j <= n; j += i) st[j] = true; // i的倍数都标记true
}
}
int main()
{
int n;
cin >> n;
get_primes(n);
cout << cnt << endl;
}
约数
算法思想:
(1)试除法求约数
(2)约数个数:int范围内约数最多一个数约数个数大概在1500个,根据算术基本定理:
由于 N = (p1x1)(P1x2)(p3x3)…(pkxk)
所以约数个数 = (x1 + 1)(x2 + 1)(x3 + 1)…(xk + 1)
例如 24 = 23 + 31,所以2可以取0,1,2,3,而3可以取0,1 所以24的约数共 4 x 2 = 8 种情况,分别为1,2,3,4,6,8,12,24
(3)约数之和:约数之和 = (p10+p11+p12+……+p1x1)……(pk0+pk1+pk2+……+pkx1)
(4)欧几里得算法:gcd(a, b) = gcd(b, a mod b)
模板:
// 欧几里得算法
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
例题:
AcWing 869. 试除法求约数
AcWing 870. 约数个数
AcWing 871. 约数之和
AC代码:
// 试除法求约数
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
vector<int> get_divisiors(int n)
{
vector<int> res;
for (int i = 1; i <= n / i; i ++)
if (n % i == 0)
{
res.push_back(i);
if (i != n / i) res.push_back(n / i); // n 可能是 i 的平方
}
sort(res.begin(), res.end());
return res;
}
int main()
{
int n;
cin >> n;
while (n --)
{
int a;
cin >> a;
auto res = get_divisiors(a);
for (auto item : res)
cout << item << ' ';
cout << endl;
}
return 0;
}
// 约数个数
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 105, MOD = 1e9 + 7;
int n;
typedef long long LL;
int main()
{
cin >> n;
unordered_map<int, int> hash;
while (n --)
{
int x;
cin >> x;
// 分解质因子
for (int i = 2; i <= x / i; i ++)
while (x % i == 0)
{
hash[i] ++; // 质因子的指数 + 1
x /= i;
}
if (x > 1) hash[x] ++;
}
LL res = 1;
for (auto item : hash) res = res *(item.second + 1) % MOD;
cout << res << endl;
return 0;
}
// 约数之和
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int N = 105, MOD = 1e9 + 7;
int n;
typedef long long LL;
int main()
{
cin >> n;
unordered_map<int, int> hash;
while (n --)
{
int x;
cin >> x;
// 分解质因子
for (int i = 2; i <= x / i; i ++)
while (x % i == 0)
{
hash[i] ++; // 质因子的指数 + 1
x /= i;
}
if (x > 1) hash[x] ++;
}
LL res = 1;
for (auto item : hash)
{
int p = item.first, a = item.second; // 取质因子和它的指数
LL t = 1; // 秦九韶算法
while (a --) t = (t * p + 1) % MOD;
res = res * t % MOD;
}
cout << res << endl;
return 0;
}
// 欧几里得算法
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int n;
cin >> n;
while (n --)
{
int a, b;
cin >> a >> b;
cout << gcd(a, b) << endl;
}
return 0;
}
欧拉函数
算法思想:
定义:
1∼N 中与 N 互质的数的个数被称为欧拉函数,记为 ϕ(N)。
若在算数基本定理中,N = p1a1p2a2…pmam,则:
ϕ(N) = N × (p1 − 1) / p1× (p2 − 1) / p2 × … × (pm − 1) / pm
即由由容斥原理可证
筛法 求欧拉函数:通过线性筛,求1~n的欧拉函数,证明略见模板
欧拉定理: 若 a 与 n 互质 => aφ(n) mod n == 1
费马小定理: 由欧拉定理推得,当 a 与 p 互质时, aφ(n) mod n = 1,若 n 为质数,则 φ(n) = n - 1,所以原公式推得 a n-1 mod n = 1
即
a n-1 mod n = 1 当 a 与 n 互质且 n 为质数
模板:
LL get_eulars(int n)
{
phi[1] = 1;
for (int i = 2; i <= n; i ++)
{
if (!st[i])
{
primes[cnt ++] = i;
phi[i] = i - 1;
}
for (int j = 0; primes[j] <= n / i; j ++)
{
st[primes[j] * i] = true;
if (i % primes[j] == 0)
{
phi[primes[j] * i] = phi[i] * primes[j];
break; // primes[j] 一定是i的最小质因子
}
phi[primes[j] * i] = phi[i] * (primes[j] - 1);
}
}
LL res = 0;
for (int i = 1; i <= n; i ++) res += phi[i];
return res;
}
例题:
AcWing 873. 欧拉函数
AcWing 874. 筛法求欧拉函数
AC代码:
// 求欧拉函数
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
while (n --)
{
int x;
cin >> x;
int res = x;
for (int i = 2; i <= x / i; i ++)
if (x % i == 0)
{
res = res / i * (i - 1);
while (x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
cout << res << endl;
}
return 0;
}
// 筛法求欧拉函数
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
bool st[N];
int primes[N], cnt;
int phi[N];
LL get_eulars(int n)
{
phi[1] = 1;
for (int i = 2; i <= n; i ++)
{
if (!st[i])
{
primes[cnt ++] = i;
phi[i] = i - 1;
}
for (int j = 0; primes[j] <= n / i; j ++)
{
st[primes[j] * i] = true;
if (i % primes[j] == 0)
{
phi[primes[j] * i] = phi[i] * primes[j];
break; // primes[j] 一定是i的最小质因子
}
phi[primes[j] * i] = phi[i] * (primes[j] - 1);
}
}
LL res = 0;
for (int i = 1; i <= n; i ++) res += phi[i];
return res;
}
int main()
{
int n;
cin >> n;
cout << get_eulars(n) << endl;
return 0;
}
快速幂
算法思想:
在 O(log k) 复杂度求出 ak mod p 的结果,其中 1 <= a,p,k <= 109
朴素做法: k 次循环累乘,时间复杂度为O(k)太慢
快速幂: 先对a的二的幂的次数进行预处理,再将k转为二进制表示,分解出2的幂的和求解
模板:
LL qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1) res = LL(res) * a % p; // 如果 k 的二进制表示最后一位是 1
k >>= 1; // 不管是不是 1 都要右移
a = LL(a) * a % p; // 不管是不是 1 都要平方,只有if成立才用到
}
return res;
}
例题:
AC代码:
#include <iostream>
using namespace std;
const int N = 2e9 + 5;
typedef long long LL;
LL qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1) res = LL(res) * a % p; // 如果 k 的二进制表示最后一位是 1
k >>= 1; // 不管是不是 1 都要右移
a = LL(a) * a % p; // 不管是不是 1 都要平方,只有if成立才用到
}
return res;
}
int main()
{
int n;
cin >> n;
while (n --)
{
int a, k, p;
cin >> a >> k >> p;
cout << qmi(a, k, p) << endl;
}
return 0;
}
扩展欧几里得算法
算法思想:
裴蜀定理: 有一对正整数a,b,那么一定存在非零整数x,y使得 ax + by = gcd(a,b)
扩展欧几里得算法: 求 x,y 使得 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;
}
例题:
AC代码:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
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;
}
int main()
{
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
while (n --)
{
int a, b, x, y;
cin >> a >> b;
exgcd(a, b, x, y);
cout << x << ' ' << y << endl;
}
return 0;
}
中国剩余定理
算法思想:
例题:
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL exgcd(LL a, LL b, LL &x, LL &y)
{
if (!b)
{
x = 1, y = 0;
return a;
}
LL d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
int main()
{
int n;
cin >> n;
LL x = 0, m1, a1;
cin >> m1 >> a1;
for (int i = 0; i < n - 1; i ++ )
{
LL m2, a2;
cin >> m2 >> a2;
LL k1, k2;
LL d = exgcd(m1, m2, k1, k2);
if ((a2 - a1) % d)
{
x = -1;
break;
}
k1 *= (a2 - a1) / d;
k1 = (k1 % (m2 / d) + m2 / d) % (m2 / d);
x = k1 * m1 + a1;
LL m = abs(m1 / d * m2);
a1 = k1 * m1 + a1;
m1 = m;
}
if (x != -1) x = (a1 % m1 + m1) % m1;
cout << x << endl;
return 0;
}
高斯消元
算法思想:
对系数矩阵进行初等行变换操作:
(1) 把某一行乘一个非零的数
(2) 交换某两行
(3) 把某行的若干倍加到另一行上去
例题:
AC代码:
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 110;
const double eps = 1e-6;
int n;
double a[N][N];
int gauss()
{
int c, r; // c为列,r为行
for (c = 0, r = 0; c < n; c ++) // 从第 0 列开始
{
int t = r;
for (int i = r; i < n; i ++)
if (fabs(a[i][c]) > fabs(a[t][c])) // 找出该列最大的一行 t
t = i;
if (fabs(a[t][c]) < eps) continue;
for (int i = c; i <= n; i ++) swap(a[t][i], a[r][i]);
for (int i = n; i >= c; i --) a[r][i] /= a[r][c];
for (int i = r + 1; i < n; i ++)
if (fabs(a[i][c]) > eps)
for (int j = n; j >= c; j --)
a[i][j] -= a[r][j] * a[i][c];
r ++;
}
if (r < n)
{
for (int i = r; i < n; i ++)
if (fabs(a[i][n]) > eps)
return 2;
return 1;
}
for (int i = n - 1; i >= 0; i -- )
for (int j = i + 1; j < n; j ++ )
a[i][n] -= a[i][j] * a[j][n];
return 0; // 有唯一解
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++)
for (int j = 0; j < n + 1; j ++)
cin >> a[i][j];
int t = gauss();
if (t == 2) puts("No solution");
else if (t == 1) puts("Infinite group solutions");
else
{
for (int i = 0; i < n; i ++ )
{
if (fabs(a[i][n]) < eps) a[i][n] = 0; // 去掉输出 -0.00 的情况
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}
求组合数
算法思想:
组合数 I : 10w组询问,数据范围:1 <= b <= a <= 2000 :递归
组合数 II : 1w组询问,数据范围:1 <= b <= a <= 1e5 :逆元递归
组合数 II : 20组询问,数据范围:1 <= b <= a <= 1e18 :卢卡斯定理
卢卡斯定理 :
组合数IV: 没有p : 高精度
例题:
AcWing 885. 求组合数 I
AcWing 886. 求组合数 II
AcWing 887. 求组合数 III
AcWing 888. 求组合数 IV
AC代码:
// 求组合数I
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2010, MOD = 1e9 + 7;
int n, a, b;
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();
cin >> n;
while (n --)
{
cin >> a >> b;
cout << c[a][b] << endl;
}
return 0;
}
// 求组合数II
#include <iostream>
#include <cstring>
#include <algorithm>
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;
}
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;
}
int n;
cin >> n;
while (n --)
{
int a, b;
cin >> a >> b;
printf("%d\n",(LL)fact[a] * infact[b] % MOD * infact[a - b] % MOD);
}
return 0;
}
// 求组合数III
#include <iostream>
#include <algorithm>
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 --)
{
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 n;
cin >> n;
while (n --)
{
LL a, b;
cin >> a >> b >> p;
cout << lucas(a, b) << endl;
}
return 0;
}
// 求组合数IV
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 5005;
int primes[N], cnt;
bool st[N];
int sum[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;
}
}
}
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;
get_primes(a);
for (int i = 0; i < cnt; i ++)
{
int p = primes[i];
sum[i] = get(a, p) - get(b, p) - get(a - b, p);
}
vector<int> res;
res.push_back(1);
for (int i = 0; i < cnt; i ++)
for (int j = 0; j < sum[i]; j ++)
res = mul(res, primes[i]);
for (int i = res.size() - 1; i >= 0; i --)
cout << res[i];
cout << endl;
return 0;
}
容斥原理
算法思想:
例题:
AC代码:
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 20;
int p[N];
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i ++ ) cin >> p[i];
int res = 0;
for (int i = 1; i < 1 << m; i ++ )
{
int t = 1, s = 0;
for (int j = 0; j < m; j ++ )
if (i >> j & 1)
{
if ((LL)t * p[j] > n)
{
t = -1;
break;
}
t *= p[j];
s ++ ;
}
if (t != -1)
{
if (s % 2) res += n / t;
else res -= n / t;
}
}
cout << res << endl;
return 0;
}
博弈论
算法思想:
NIM游戏
给定N堆物品,第i堆物品有Ai个。两名玩家轮流行动,每次可以任选一堆,取走任意多个物品,可把一堆取光,但不能不取。取走最后一件物品者获胜。两人都采取最优策略,问先手是否必胜。
我们把这种游戏称为NIM博弈。把游戏过程中面临的状态称为局面。整局游戏第一个行动的称为先手,第二个行动的称为后手。若在某一局面下无论采取何种行动,都会输掉游戏,则称该局面必败。
所谓采取最优策略是指,若在某一局面下存在某种行动,使得行动后对面面临必败局面,则优先采取该行动。同时,这样的局面被称为必胜。我们讨论的博弈问题一般都只考虑理想情况,即两人均无失误,都采取最优策略行动时游戏的结果。
NIM博弈不存在平局,只有先手必胜和先手必败两种情况。
定理: NIM博弈先手必胜,当且仅当 A1 ^ A2 ^ … ^ An != 0
公平组合游戏ICG
若一个游戏满足:
- 由两名玩家交替行动;
- 在游戏进程的任意时刻,可以执行的合法行动与轮到哪名玩家无关;
- 不能行动的玩家判负;
则称该游戏为一个公平组合游戏。
NIM博弈属于公平组合游戏,但城建的棋类游戏,比如围棋,就不是公平组合游戏。因为围棋交战双方分别只能落黑子和白子,胜负判定也比较复杂,不满足条件2和条件3。
有向图游戏
给定一个有向无环图,图中有一个唯一的起点,在起点上放有一枚棋子。两名玩家交替地把这枚棋子沿有向边进行移动,每次可以移动一步,无法移动者判负。该游戏被称为有向图游戏。
任何一个公平组合游戏都可以转化为有向图游戏。具体方法是,把每个局面看成图中的一个节点,并且从每个局面向沿着合法行动能够到达的下一个局面连有向边。
Mex运算
设S表示一个非负整数集合。定义mex(S)为求出不属于集合S的最小非负整数的运算,即:
mex(S) = min{x}, x属于自然数,且x不属于S
SG函数
在有向图游戏中,对于每个节点x,设从x出发共有k条有向边,分别到达节点y1, y2, …, yk,定义SG(x)为x的后继节点y1, y2, …, yk 的SG函数值构成的集合再执行mex(S)运算的结果,即:
SG(x) = mex({SG(y1), SG(y2), …, SG(yk)})
特别地,整个有向图游戏G的SG函数值被定义为有向图游戏起点s的SG函数值,即SG(G) = SG(s)。
有向图游戏的和
设G1, G2, …, Gm 是m个有向图游戏。定义有向图游戏G,它的行动规则是任选某个有向图游戏Gi,并在Gi上行动一步。G被称为有向图游戏G1, G2, …, Gm的和。
有向图游戏的和的SG函数值等于它包含的各个子游戏SG函数值的异或和,即:
SG(G) = SG(G1) ^ SG(G2) ^ … ^ SG(Gm)
定理
有向图游戏的某个局面必胜,当且仅当该局面对应节点的SG函数值大于0。
有向图游戏的某个局面必败,当且仅当该局面对应节点的SG函数值等于0。