Skip to content

Commit

Permalink
feat: upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiphereth-A committed Dec 7, 2023
1 parent 72aac68 commit de40f52
Show file tree
Hide file tree
Showing 12 changed files with 668 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ICPCnotebook.cls
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
\RequirePackage{seqsplit}
\RequirePackage{subcaption}
\RequirePackage{svg}
\RequirePackage{tabularx}
\RequirePackage{tabularx,diagbox}
\RequirePackage{tikz}
\RequirePackage{tocloft}
\RequirePackage{xspace}
Expand Down
34 changes: 19 additions & 15 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ notebook:
- tsearch: 三分
code_ext: hpp
test_ext: cpp
- simplex: 单纯形法
code_ext: hpp
test_ext: cpp
- heuristic_sa: 模拟退火
code_ext: hpp
test_ext: cpp
Expand Down Expand Up @@ -659,43 +662,43 @@ notebook:
- stirling2_smallp: 第二类 Stirling 数(小模数)
code_ext: hpp
test_ext: cpp
- gen_ball_box_ii: 球与盒(球全部相同,盒全部相同)序列
- gen_ball_box_ii: 球与盒(球相同,盒相同)序列
code_ext: hpp
test_ext: cpp
- ball_box_dda: 球与盒(球互不相同,盒互不相同)
- ball_box_dda: 球与盒(球不同,盒不同)
code_ext: hpp
test_ext: cpp
- ball_box_ddm: 球与盒(球互不相同,盒互不相同,至多装一个球)
- ball_box_ddm: 球与盒(球不同,盒不同,至多装一个球)
code_ext: hpp
test_ext: cpp
- ball_box_ddl: 球与盒(球互不相同,盒互不相同,至少装一个球)
- ball_box_ddl: 球与盒(球不同,盒不同,至少装一个球)
code_ext: hpp
test_ext: cpp
- ball_box_ida: 球与盒(球全部相同,盒互不相同)
- ball_box_ida: 球与盒(球相同,盒不同)
code_ext: hpp
test_ext: cpp
- ball_box_idm: 球与盒(球全部相同,盒互不相同,至多装一个球)
- ball_box_idm: 球与盒(球相同,盒不同,至多装一个球)
code_ext: hpp
test_ext: cpp
- ball_box_idl: 球与盒(球全部相同,盒互不相同,至少装一个球)
- ball_box_idl: 球与盒(球相同,盒不同,至少装一个球)
code_ext: hpp
test_ext: cpp
- ball_box_dia: 球与盒(球互不相同,盒全部相同)
- ball_box_dia: 球与盒(球不同,盒相同)
code_ext: hpp
test_ext: cpp
- ball_box_dim: 球与盒(球互不相同,盒全部相同,至多装一个球)
- ball_box_dim: 球与盒(球不同,盒相同,至多装一个球)
code_ext: hpp
test_ext: cpp
- ball_box_dil: 球与盒(球互不相同,盒全部相同,至少装一个球)
- ball_box_dil: 球与盒(球不同,盒相同,至少装一个球)
code_ext: hpp
test_ext: cpp
- ball_box_iia: 球与盒(球全部相同,盒全部相同)
- ball_box_iia: 球与盒(球相同,盒相同)
code_ext: hpp
test_ext: cpp
- ball_box_iim: 球与盒(球全部相同,盒全部相同,至多装一个球)
- ball_box_iim: 球与盒(球相同,盒相同,至多装一个球)
code_ext: hpp
test_ext: cpp
- ball_box_iil: 球与盒(球全部相同,盒全部相同,至少装一个球)
- ball_box_iil: 球与盒(球相同,盒相同,至少装一个球)
code_ext: hpp
test_ext: cpp
math:
Expand Down Expand Up @@ -1031,7 +1034,7 @@ notebook:
- conv_3ntt: 卷积(三模 NTT)
code_ext: hpp
test_ext: cpp
- conv_u128: 卷积(三模 NTT高精度整数用)
- conv_u128: 卷积(三模 NTT,高精度整数用)
code_ext: hpp
test_ext: cpp
- conv_cntt: 卷积(扩域 NTT)
Expand Down Expand Up @@ -1274,8 +1277,9 @@ cheatsheets:
nt-const: 初等数论常数表
seqences: 常见数列
oeis-seq: 常用 OEIS 数列
decimal: Python Decimal
formula: 常用公式
decimal: Python Decimal
bash: Bash 简单用法
lgv: lgv 定理
default_code_style: common
code_styles:
Expand Down
9 changes: 9 additions & 0 deletions notebook.bib
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ @misc{antileafstandard
howpublished = {\url{https://github.com/AntiLeaf/Standard-Code-Library}}
}

@misc{bqi343cp,
author = {Benjamin Qi, Spencer Compton, Zhezheng Luo},
title = {cp-notebook},
year = {2017},
publisher = {GitHub},
journal = {GitHub Repository},
howpublished = {\url{https://github.com/bqi343/cp-notebook}}
}

@article{massey1969shift,
author = {Massey, J.},
journal = {IEEE Transactions on Information Theory},
Expand Down
3 changes: 3 additions & 0 deletions src/cheatsheet/bash.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
From \url{https://learnxinyminutes.com/docs/bash/}.

\inputminted[mathescape=false]{bash}{src/src/LearnBash.sh}
90 changes: 90 additions & 0 deletions src/code/opt/simplex.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#ifndef TIFALIBS_OPT_SIMPLEX
#define TIFALIBS_OPT_SIMPLEX

#include "../util/fp_const.hpp"
#include "../util/util.hpp"

namespace tifa_libs::opt {

template <class T = double>
struct LPSolver {
#define ltj(X) \
if (s == -1 || std::make_pair(X[j], N[j]) < std::make_pair(X[s], N[s])) s = j
static constexpr T inf = 1 / .0;
int m, n; // # m = contraints, # n = variables
vec<int> N, B; // N[j] = non-basic variable (j-th column), = 0
vvec<T> D; // B[j] = basic variable (j-th row)
LPSolver(vvec<T> const& A, vec<T> const& b, vec<T> const& c) : m(sz(b)), n(sz(c)), N(n + 1), B(m), D(m + 2, vec<T>(n + 2)) {
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) D[i][j] = A[i][j];
for (int i = 0; i < m; ++i) B[i] = n + i, D[i][n] = -1, D[i][n + 1] = b[i];
// B[i]: basic variable for each constraint
// D[i][n]: artificial variable for testing feasibility
for (int j = 0; j < n; ++j) N[j] = j, D[m][j] = -c[j];
// D[m] stores negation of objective,
// which we want to minimize
N[n] = -1;
D[m + 1][n] = 1; // to find initial feasible
} // solution, minimize artificial variable
void pivot(int r, int s) { // swap B[r] (row)
T inv = 1 / D[r][s]; // with N[r] (column)
for (int i = 0; i <= m + 1; ++i)
if (i != r && abs(D[i][s]) > EPS<T>) {
T binv = D[i][s] * inv;
for (int j = 0; j < (n + 2); ++j)
if (j != s) D[i][j] -= D[r][j] * binv;
D[i][s] = -binv;
}
D[r][s] = 1;
for (int j = 0; j < (n + 2); ++j) D[r][j] *= inv; // scale r-th row
std::swap(B[r], N[s]);
}
bool simplex(int phase) {
int x = m + phase - 1;
while (1) { // if phase=1, ignore artificial variable
int s = -1;
for (int j = 0; j <= n; ++j)
if (N[j] != -phase) ltj(D[x]);
// find most negative col for nonbasic (NB) variable
if (D[x][s] >= -EPS<T>) return 1;
// can't get better sol by increasing NB variable
int r = -1;
for (int i = 0; i < m; ++i) {
if (D[i][s] <= EPS<T>) continue;
if (r == -1 || std::make_pair(D[i][n + 1] / D[i][s], B[i]) < std::make_pair(D[r][n + 1] / D[r][s], B[r])) r = i;
// find smallest positive ratio
} // -> max increase in NB variable
if (r == -1) return 0; // objective is unbounded
pivot(r, s);
}
}
T solve(vec<T>& x) { // 1. check if x=0 feasible
int r = 0;
for (int i = (1); i < m; ++i)
if (D[i][n + 1] < D[r][n + 1]) r = i;
if (D[r][n + 1] < -EPS<T>) { // if not, find feasible start
pivot(r, n); // make artificial variable basic
assert(simplex(2)); // I think this will always be true??
if (D[m + 1][n + 1] < -EPS<T>) return -inf;
// D[m+1][n+1] is max possible value of the negation of
// artificial variable, optimal value should be zero
// if exists feasible solution
for (int i = 0; i < m; ++i)
if (B[i] == -1) { // artificial var basic
int s = 0;
for (int j = (1); j <= n; ++j) ltj(D[i]); // -> nonbasic
pivot(i, s);
}
}
bool ok = simplex(1);
x = vec<T>(n);
for (int i = 0; i < m; ++i)
if (B[i] < n) x[B[i]] = D[i][n + 1];
return ok ? D[m][n + 1] : inf;
}
};
#undef ltj

} // namespace tifa_libs::opt

#endif
2 changes: 1 addition & 1 deletion src/doc_tex/comb/ball_box_iia.tex
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
\end{aligned}
\]

\(F_m(x)\) 的代码参见 \fullref{sec:球与盒(球全部相同, 盒全部相同)序列}
\(F_m(x)\) 的代码参见 \fullref{sec:球与盒(球相同,盒相同)序列}
2 changes: 1 addition & 1 deletion src/doc_tex/comb/ball_box_iil.tex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
设有 \(n\) 个球, \(m\) 个盒子

类似 \fullref{sec:球与盒(球全部相同, 盒全部相同)}, 答案为 \(f(n-m,m)\)
类似 \fullref{sec:球与盒(球相同,盒相同)}, 答案为 \(f(n-m,m)\)
2 changes: 1 addition & 1 deletion src/doc_tex/comb/ball_box_iim.tex
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
设有 \(n\) 个球, \(m\) 个盒子

\fullref{sec:球与盒(球互不相同, 盒全部相同, 至多装一个球)}
\fullref{sec:球与盒(球相同,盒相同,至多装一个球)}
51 changes: 51 additions & 0 deletions src/doc_tex/opt/simplex.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
求解如下线性规划问题:

\[
\begin{array}{ll}
\max & c^T x. \\
\textrm{s.t.} & Ax \le b, x \ge 0.
\end{array}
\]

若无解则返回 \verb|-inf|, 若无界则返回 \verb|inf|, 否则返回 \(c^T x\) 的最大值. 输入的 \verb|x| 会被修改为解. 不保证数值稳定性. \(x = 0\) 最好在可行域中.

\paragraph{使用}

\inputminted{cpp}{src/src/simplex_usage.txt}

\paragraph{原问题与对偶问题}

\begin{tabular}{|c|c|}
\hline
原问题 & 对偶问题 \\
\hline
\(\max_x c^T x\) & \(\min_y b^T y\) \\
\hline
\(n\) 个变量 \(x_1,\dots,x_n\) & \(n\) 个限制条件 \(A^T y\gtreqqless c\) \\
\(x_i\geq 0\) & 第 \(i\) 个限制条件为 \(\geq c_i\) \\
\(x_i\leq 0\) & 第 \(i\) 个限制条件为 \(\leq c_i\) \\
\(x_i\in \mathrm{R}\) & 第 \(i\) 个限制条件为 \(=c_i\) \\
\hline
\(m\) 个限制条件 \(Ax\gtreqqless b\) & \(m\) 个变量 \(y_1,\dots,y_m\) \\
\(i\) 个限制条件为 \(\geq b_i\) & \(y_i\leq 0\) \\
\(i\) 个限制条件为 \(\leq b_i\) & \(y_i\geq 0\) \\
\(i\) 个限制条件为 \(=b_i\) & \(y_i\in \mathrm{R}\) \\
\hline
\end{tabular}

\begin{tabular}{|cc|cc|}
\hline
\multicolumn{2}{|c|}{原问题} & \multicolumn{2}{|c|}{对偶问题} \\
\hline
\(\max\) & \(3x_1+4x_2\) & \(\min\) & \(7y_1\) \\
\(\textrm{s.t.}\) & \(5x_1+6x_2=7\) & \(\textrm{s.t.}\) & \(5y_1\geq 3\) \\
& & & \(6y_2\geq 4\) \\
& \(x_1\geq 0,x_2\geq 0\) & & \(y_1\in\mathrm{R}\) \\
\hline
\end{tabular}

\paragraph{复杂度}

\(O(NM \cdot \texttt{pivots})\), where a pivot may be e.g. an edge relaxation. \(O(2^N)\) in the general case.

\paragraph{参考} \cite{bqi343cp}
Loading

0 comments on commit de40f52

Please sign in to comment.