随笔分类 - 数学知识
包括质数,约数,欧拉函数,快速幂,扩展欧几里得算法,中国剩余定理,高斯消元,求组合数,容斥原理,博弈论等内容。
摘要:AcWing204.表达整数的奇怪方式 题解 模板 根据题目变形 #include <iostream> using namespace std; typedef long long LL; LL exgcd(LL a, LL b, LL &x, LL &y) { if(!b) { x = 1, y
阅读全文
摘要:AcWing878.线性同余方程 题解 #include <iostream> using namespace std; int exgcd(int a, int b, int &x, int &y) { if(b == 0) { x = 1, y = 0; return a; } int d =
阅读全文
摘要:AcWing877.扩展欧几里得算法 题解与证明 #include <iostream> using namespace std; int exgcd(int a, int b, int &x, int &y) { if(b == 0) { x = 1, y = 0; return a; } int
阅读全文
摘要:AcWing876.快速幂求逆元 题解 #include <iostream> using namespace std; typedef long long LL; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int qm
阅读全文
摘要:
阅读全文
摘要:AcWing875.快速幂 题解 #include <iostream> using namespace std; typedef long long LL; int main() { int n, a, b, p; cin >> n; while(n -- ) { int res = 1; cin
阅读全文
摘要:AcWing874.筛法求欧拉函数 题解 #include <iostream> using namespace std; typedef long long LL; const int N = 1e6 + 10; int primes[N], eulars[N], cnt; bool st[N];
阅读全文
摘要:AcWing873.欧拉函数 证明与题解 #include <iostream> using namespace std; int phi(int x) { int res = x; for(int i = 2; i <= x / i; ++i) { if(x % i == 0) { res = r
阅读全文
摘要:AcWing872.最大公约数 题解 #include <iostream> using namespace std; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n, x, y; cin >> n
阅读全文
摘要:AcWing871.约数之和 题解 约数定理 #include <iostream> #include <cmath> #include <unordered_map> using namespace std; const int MOD = 1e9 + 7; typedef long long L
阅读全文
摘要:AcWing870.约数个数 题解 约数定理 #include <iostream> #include <unordered_map> using namespace std; typedef long long LL; const int MOD = 1e9 + 7; int main() { i
阅读全文
摘要:AcWing869.试除法求约数 题解 #include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> get_divisors(int n) { vector<int> res;
阅读全文
摘要:AcWing868.筛质数 题解 任何数都可以分解成:若干个质数。 也就是说每个数都可以由它的 最小质因子×另一个数得出 比如说:i=6的最小质因子是2, st[26] = true 为什么不把36也筛掉 因为36=18可 通过 st[29]筛选掉 也就是说只要你这个数在范围内,我在后面必定可以用最
阅读全文
摘要:AcWing867.分解质因数 题解 首先我们要明白算术基本定理,任何数都是由质数组成的,比如说 4是由22, 9由33等等 故该算法通过将前面的质因数除尽就不会被非质数干扰,比如说一个数能被4整除,必然能分解成2个2. 同时,我们要将该算法优化为O(sqrt(n))必须明白,n中最多包含一个大于s
阅读全文
摘要:AcWing866.试除法判定质数 题解 注意is_prime函数中的循坏要i/n,因为i * i ⇐ n中i*i可能会爆精度。 #include <iostream> #include <cstdio> using namespace std; bool is_prime(int n) { if(
阅读全文
摘要:题解 代码 #include <iostream> #include <cstdio> using namespace std; typedef long long LL; LL exgcd(LL a,LL b,LL &x,LL &y) { if(!b){ x = 1, y = 0; return
阅读全文