Skip to content

Commit

Permalink
:(
Browse files Browse the repository at this point in the history
  • Loading branch information
littlecube8152 committed Nov 15, 2023
1 parent 12a34a4 commit 2468bcb
Show file tree
Hide file tree
Showing 82 changed files with 316 additions and 176 deletions.
53 changes: 53 additions & 0 deletions codebook/4_Flow_Matching/Hopcroft-Karp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "common.h"
struct hopcroftKarp { // 0-based
bool dfs(int a, int L, vector<vector<int>> &g,
vector<int> &btoa, vector<int> &A,
vector<int> &B) {
if (A[a] != L) return 0;
A[a] = -1;
for (int b : g[a])
if (B[b] == L + 1) {
B[b] = 0;
if (btoa[b] == -1 ||
dfs(btoa[b], L + 1, g, btoa, A, B))
return btoa[b] = a, 1;
}
return 0;
}

int solve(vector<vector<int>> &g, int m) {
int res = 0;
vector<int> btoa(-1, m), A(g.size()),
B(btoa.size()), cur, next;
for (;;) {
fill(all(A), 0), fill(all(B), 0);
cur.clear();
for (int a : btoa)
if (a != -1) A[a] = -1;
for (int a = 0; a < g.size(); a++)
if (A[a] == 0) cur.push_back(a);
/// Find all layers using bfs.
for (int lay = 1;; lay++) {
bool islast = 0;
next.clear();
for (int a : cur)
for (int b : g[a]) {
if (btoa[b] == -1) {
B[b] = lay;
islast = 1;
} else if (btoa[b] != a && !B[b]) {
B[b] = lay;
next.push_back(btoa[b]);
}
}
if (islast) break;
if (next.empty()) return res;
for (int a : next) A[a] = lay;
cur.swap(next);
}
/// Use DFS to scan for augmenting paths.
for (int a = 0; a < g.size(); a++)
res += dfs(a, 0, g, btoa, A, B);
}
}
};
47 changes: 26 additions & 21 deletions codebook/5_String/Aho-Corasick_Automatan.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "common.h"

#define sumS 500005
#define sigma 26
#define base 'a'
struct AhoCorasick {
int ch[sumS][sigma] = {{}}, f[sumS] = {-1},
tag[sumS], mv[sumS][sigma], jump[sumS],
cnt[sumS];
int idx = 0;
int insert(string &s) {
int idx = 0, t = -1;
vector<int> E[sumS], q;
pii o[sumS];
int insert(string &s, int t) {
int j = 0;
for (int i = 0; i < (int)s.size(); i++) {
if (!ch[j][s[i] - base])
Expand All @@ -18,36 +22,37 @@ struct AhoCorasick {
int next(int u, int c) {
return u < 0 ? 0 : mv[u][c];
}
void dfs(int u) {
o[u].F = ++t;
for (auto v : E[u]) dfs(v);
o[u].S = t;
}
void build() {
queue<int> q;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
int k = -1;
q.emplace_back(0);
while (++k < q.size()) {
int u = q[k];
for (int v = 0; v < sigma; v++) {
if (ch[u][v]) {
f[ch[u][v]] = next(f[u], v);
q.push(ch[u][v]);
q.emplace_back(ch[u][v]);
}
mv[u][v] =
(ch[u][v] ? ch[u][v] : next(f[u], v));
}
if (u) jump[u] = (tag[f[u]] ? f[u] : jump[f[u]]);
}
reverse(q.begin(), q.end());
for (int i = 1; i <= idx; i++)
E[f[i]].emplace_back(i);
dfs(0);
}
void match(string &s) {
for (int i = 0; i <= idx; i++) cnt[i] = 0;
for (int i = 0, j = 0; i < (int)s.size(); i++) {
j = next(j, s[i] - base);
cnt[j]++;
}
vector<int> v;
v.emplace_back(0);
for (int i = 0; i < (int)v.size(); i++)
for (int j = 0; j < sigma; j++)
if (ch[v[i]][j]) v.emplace_back(ch[v[i]][j]);
reverse(v.begin(), v.end());
for (int i : v)
fill(cnt, cnt + idx + 1, 0);
for (int i = 0, j = 0; i < (int)s.size(); i++)
cnt[j = next(j, s[i] - base)]++;

for (int i : q)
if (f[i] > 0) cnt[f[i]] += cnt[i];
}
} ac;
18 changes: 10 additions & 8 deletions codebook/6_Math/Berlekamp-Massey.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#include "common.h"
template <typename T>
vector<T> BerlekampMassey(const vector<T> &output) {
vector<T> d(SZ(output) + 1), me, he;
for (int f = 0, i = 1; i <= SZ(output); ++i) {
for (int j = 0; j < SZ(me); ++j)
vector<T> d(output.size() + 1), me, he;
for (int f = 0, i = 1; i <= output.size(); ++i) {
for (int j = 0; j < me.size(); ++j)
d[i] += output[i - j - 2] * me[j];
if ((d[i] -= output[i - 1]) == 0) continue;
if (me.empty()) {
me.resize(f = i);
continue;
}
vector<T> o(i - f - 1);
T k = -d[i] / d[f]; o.pb(-k);
for (T x : he) o.pb(x * k);
o.resize(max(SZ(o), SZ(me)));
for (int j = 0; j < SZ(me); ++j) o[j] += me[j];
if (i - f + SZ(he) >= SZ(me)) he = me, f = i;
T k = -d[i] / d[f];
o.pb(-k);
for (T x : he) o.emplace_back(x * k);
o.resize(max(o.size(), me.size()));
for (int j = 0; j < me.size(); ++j) o[j] += me[j];
if (i - f + (int)he.size()) >= (int)me.size()) he = me, f = i;
me = o;
}
return me;
Expand Down
3 changes: 2 additions & 1 deletion codebook/6_Math/Big_number.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
template<typename T>
inline string to_string(const T& x){
stringstream ss;
Expand Down Expand Up @@ -88,7 +89,7 @@ struct bigN:vector<ll>{
if((res[i+j]+=at(i)*b[j])>=base){
res[i+j+1]+=res[i+j]/base;
res[i+j]%=base;
}//­¼ªk¥Îcarry·|·¸¦ì
}//���k��carry�|����
return res.trim(),res;
}
bigN operator/(const bigN &b)const{
Expand Down
12 changes: 7 additions & 5 deletions codebook/6_Math/Determinant.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "common.h"
#undef M
struct Matrix {
int n, m;
ll M[MAXN][MAXN];
ll M[N][N];
int row_swap(int i, int j) {
if (i == j) return 0;
for (int k = 0; k < m; ++k)
Expand All @@ -16,16 +18,16 @@ struct Matrix {
rt += row_swap(i, piv);
for (int j = i + 1; j < n; ++j) {
while (M[j][i]) {
int tmp = P - M[i][i] / M[j][i];
int tmp = mod - M[i][i] / M[j][i];
for (int k = i; k < m; ++k)
M[i][k] = (M[j][k] * tmp + M[i][k]) % P;
M[i][k] = (M[j][k] * tmp + M[i][k]) % mod;
rt += row_swap(i, j);
}
}
}
rt = (rt & 1) ? P - 1 : 1;
rt = (rt & 1) ? mod - 1 : 1;
for (int i = 0; i < n; ++i)
rt = rt * M[i][i] % P;
rt = rt * M[i][i] % mod;
return rt;
// round(rt) if using double to cal. int. det
}
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/DiscreteLog.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
int DiscreteLog(int s, int x, int y, int m) {
constexpr int kStep = 32000;
unordered_map<int, int> p;
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/Fraction.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
struct fraction {
ll n, d;
fraction(const ll &_n=0, const ll &_d=1): n(_n), d(_d) {
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/Gaussian_gcd.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
cpx gaussian_gcd(cpx a, cpx b) {
#define rnd(a, b) ((a >= 0 ? a * 2 + b : a * 2 - b) / (b * 2))
ll c = a.real() * b.real() + a.imag() * b.imag();
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/Miller_Rabin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
// n < 4,759,123,141 3 : 2, 7, 61
// n < 1,122,004,669,633 4 : 2, 13, 23, 1662803
// n < 3,474,749,660,383 6 : primes <= 13
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/ModMin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
// min{k | l <= ((ak) mod m) <= r}, no solution -> -1
ll mod_min(ll a, ll m, ll l, ll r) {
if (a == 0) return l ? -1 : 0;
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/PiCount.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
ll PrimeCount(ll n) { // n ~ 10^13 => < 2s
if (n <= 1) return 0;
int v = sqrt(n), s = (v + 1) / 2, pc = 0;
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/Pollard_Rho.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
map<ll, int> cnt;
void PollardRho(ll n) {
if (n == 1) return;
Expand Down
6 changes: 2 additions & 4 deletions codebook/6_Math/QuadraticResidue.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Berlekamp-Rabin

#include "common.h"
// Berlekamp-Rabin, log^2(p)
ll trial(ll y, ll z, ll m) {
ll a0 = 1, a1 = 0, b0 = z, b1 = 1, p = (m - 1) / 2;
while (p) {
Expand All @@ -15,9 +15,7 @@ ll trial(ll y, ll z, ll m) {
if (a1) return inv(a1, m);
return -1;
}

mt19937 rd(49);

ll psqrt(ll y, ll p) {
if (fpow(y, (p - 1) / 2, p) != 1) return -1;
for (int i = 0; i < 30; i++) {
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/SchreierSims.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
namespace schreier {
int n;
vector<vector<vector<int>>> bkts, binv;
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/Simplex_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
const int MAXN = 11000, MAXM = 405;
const double eps = 1E-10;
double a[MAXN][MAXM], b[MAXN], c[MAXM];
Expand Down
4 changes: 3 additions & 1 deletion codebook/6_Math/Simultaneous_Equations.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "Fraction.cpp"
#undef M
struct matrix { //m variables, n equations
int n, m;
fraction M[MAXN][MAXN + 1], sol[MAXN];
fraction M[N][N + 1], sol[N];
int solve() { //-1: inconsistent, >= 0: rank
for (int i = 0; i < n; ++i) {
int piv = 0;
Expand Down
11 changes: 0 additions & 11 deletions codebook/6_Math/ax=b%n.cpp

This file was deleted.

1 change: 1 addition & 0 deletions codebook/6_Math/cantor_expansion.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
#define MAXN 11
int factorial[MAXN];
inline void init(){
Expand Down
6 changes: 4 additions & 2 deletions codebook/6_Math/chineseRemainder.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "ExtGCD.cpp"
ll solve(ll x1, ll m1, ll x2, ll m2) {
ll g = gcd(m1, m2);
if ((x2 - x1) % g) return -1; // no sol
m1 /= g; m2 /= g;
pll p = exgcd(m1, m2);
ll x, y;
extgcd(m1, m2, __gcd(m1, m2), x, y);
ll lcm = m1 * m2 * g;
ll res = p.first * (x2 - x1) * m1 + x1;
ll res = x * (x2 - x1) * m1 + x1;
// be careful with overflow
return (res % lcm + lcm) % lcm;
}
1 change: 1 addition & 0 deletions codebook/6_Math/fac_no_p.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
// O(p^k + log^2 n), pk = p^k
ll prod[MAXP];
ll fac_no_p(ll n, ll p, ll pk) {
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/floor_ceil.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
int floor(int a, int b)
{ return a / b - (a % b && (a < 0) ^ (b < 0)); }
int ceil(int a, int b)
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/floor_enumeration.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
// enumerating x = floor(n / i), [l, r]
for (int l = 1, r; l <= n; l = r + 1) {
int x = n / l;
Expand Down
1 change: 1 addition & 0 deletions codebook/6_Math/floor_sum.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
ll floorsum(ll A, ll B, ll C, ll N) {
if (A == 0) return (N + 1) * (B / C);
if (A > C || B > C)
Expand Down
1 change: 1 addition & 0 deletions codebook/7_Polynomial/Fast_Fourier_Transform.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
template<int MAXN>
struct FFT {
using val_t = complex<double>;
Expand Down
1 change: 1 addition & 0 deletions codebook/7_Polynomial/Fast_Walsh_Transform.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
/* x: a[j], y: a[j + (L >> 1)]
or: (y += x * op), and: (x += y * op)
xor: (x, y = (x + y) * op, (x - y) * op)
Expand Down
1 change: 1 addition & 0 deletions codebook/7_Polynomial/NTT.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
//9223372036737335297, 3
#define base ll // complex<double>
#define N 524288
Expand Down
1 change: 1 addition & 0 deletions codebook/7_Polynomial/Number_Theory_Transform.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
//(2^16)+1, 65537, 3
//7*17*(2^23)+1, 998244353, 3
//1255*(2^20)+1, 1315962881, 3
Expand Down
4 changes: 2 additions & 2 deletions codebook/7_Polynomial/Polynomial_Operation.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define poly vector<base>

#include "NTT.cpp"
#define poly vector<ll>
poly inv(poly A) {
A.resize(1 << (__lg(A.size() - 1) + 1));
poly B = {inverse(A[0])};
Expand Down
1 change: 1 addition & 0 deletions codebook/7_Polynomial/Value_Poly.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
struct Poly {
mint base; // f(x) = poly[x - base]
vector<mint> poly;
Expand Down
2 changes: 0 additions & 2 deletions codebook/8_Geometry/3DConeDist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ void iterate() {
dis.resize(n);
from.resize(n);
vis.resize(n);

for (int i = 1; i < n; i++) dis[i] = 1e18;

for (int t = 0; t < n; t++) {
int u = 0;
ld d = 1e18;
Expand Down
1 change: 1 addition & 0 deletions codebook/8_Geometry/CircleCover.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "common.h"
const int N = 1021;
struct CircleCover {
int C;
Expand Down
7 changes: 4 additions & 3 deletions codebook/8_Geometry/Convex_hull.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "Default_code.cpp"
void hull(vector<pll> &dots) { // n=1 => ans = {}
sort(dots.begin(), dots.end());
vector<pll> ans(1, dots[0]);
for (int ct = 0; ct < 2; ++ct, reverse(ALL(dots)))
for (int i = 1, t = SZ(ans); i < SZ(dots); ans.pb(dots[i++]))
while (SZ(ans) > t && ori(ans[SZ(ans) - 2], ans.back(), dots[i]) <= 0)
for (int ct = 0; ct < 2; ++ct, reverse(all(dots)))
for (int i = 1, t = ans.size(); i < dots.size(); ans.emplace_back(dots[i++]))
while (ans.size() > t && ori(ans.end()[-2], ans.back(), dots[i]) <= 0)
ans.pop_back();
ans.pop_back(), ans.swap(dots);
}
Loading

0 comments on commit 2468bcb

Please sign in to comment.