06 2022 档案
摘要:AcWing282.石子合并 题解 #include <iostream> using namespace std; const int N = 310; int n, f[N][N], nums[N], s[N]; int main() { cin >> n; for(int i = 1; i <
阅读全文
摘要:AcWing896.最长上升子序列 II 题解 二分解法 q[i]保存长度为i的最小末尾,这样即可严格单调递增,从而对于每个新的数只要找出小于它的最大值就可算出长度 #include <cstdio> #include <iostream> using namespace std; const in
阅读全文
摘要:AcWing895.最长上升子序列 题解 #include <iostream> using namespace std; const int N = 1010; int f[N], nums[N]; int main() { int n, ans = 0; cin >> n; for(int i
阅读全文
摘要:AcWing898.数字三角形 题解 自底向上 #include <iostream> using namespace std; const int N = 510; int f[N][N]; int main() { int n; cin >> n; for(int i = 1; i <= n;
阅读全文
摘要:AcWing9.分组背包问题 题解 一维状压 #include <iostream> using namespace std; const int N = 110; int n, m, s[N], v[N][N], w[N][N], f[N]; int main() { cin >> n >> m;
阅读全文
摘要:AcWing5.多重背包问题 II 题解 #include <iostream> using namespace std; const int N = 20010, M = 2010; int v[N], w[N], cnt, n, m, f[M]; int main() { int a, b, c
阅读全文
摘要:AcWing4.多重背包问题 I 题解 状态分析与朴素完全背包问题相同。 不可如完全背包问题那样优化成二维循环:因为物品有个数限制 j - v的k和v的k不一定是一致的,例如:j-v → j-(s[i]+1) * v, j → j - s[i]*v #include <iostream> using
阅读全文
摘要:AcWing3.完全背包问题 题解 朴素写法 #include <iostream> using namespace std; const int N = 1010; int v[N], w[N]; int f[N][N]; int main() { int n, m; cin >> n >> m;
阅读全文
摘要:AcWing2.01背包问题 题解 二维 #include <iostream> using namespace std; const int N = 1010; int dp[N][N]; int v[N], w[N]; int main() { int n, V; cin >> n >> V;
阅读全文
摘要: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(
阅读全文
摘要:AcWing861.二分图的最大匹配 题解 匈牙利算法:若该左点匹配到的右点之前已匹配到了左点,则查找该之前匹配的左点可否换一个点,使新的左点能匹配该右点 #include <iostream> #include <cstring> #include <cstdio> using namespace
阅读全文
摘要:AcWing860.染色法判定二分图 题解 若出现奇数环,染色一定矛盾 当前点没有染色,就染色包括其子节点 #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int N = 1e5
阅读全文
摘要:AcWing859.Kruskal算法求最小生成树 题解 可以通过并查集查看a,b的根结点是否相同,相同则代表连通,即会成环不能加入最小生成树 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm>
阅读全文