Skip to content

Commit

Permalink
2018 ICPC Asia Singapore Regional
Browse files Browse the repository at this point in the history
  • Loading branch information
fsh0524 committed Dec 10, 2018
1 parent ea98532 commit d223ce5
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 53 deletions.
8 changes: 8 additions & 0 deletions code/Basic/vimrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
set ts=4
set sw=4
set si
set nu
set mouse=a
imap { {}<esc>i<cr><esc>v<o
map <f9> :w<lf>:!g++ -O2 -std=c++14 -o %.out % && echo "----Test Start----" && ./%.out<lf>
imap <f9> <esc><f9>
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
#include <bits/extc++.h>
typedef __gnu_pbds::priority_queue<int> heap_t;
heap_t a,b;

int main() {
a.clear();
b.clear();
a.push(1);
a.push(3);
b.push(2);
b.push(4);
assert(a.top() == 3);
assert(b.top() == 4);
a.clear();b.clear();
a.push(1);a.push(3);b.push(2);b.push(4);
// merge two heap
a.join(b);
assert(a.top() == 4);
assert(b.empty());

return 0;
}
17 changes: 17 additions & 0 deletions code/DataStructure/pb_ds rbtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int main() {
ordered_set o_set;
o_set.insert(5);
o_set.insert(1);
o_set.insert(2);
cout << *(o_set.find_by_order(1)) << endl; // 2
cout << o_set.order_of_key(4) << endl; // 2
cout << o_set.order_of_key(5) << endl; // 2
if (o_set.find(2) != o_set.end())
o_set.erase(o_set.find(2));
cout << *(o_set.find_by_order(1)) << endl; // 5
cout << o_set.order_of_key(4) << endl; // 1
}
17 changes: 0 additions & 17 deletions code/Math/FWHT.cpp

This file was deleted.

124 changes: 124 additions & 0 deletions code/Math/Simplex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Two-phase simplex algorithm for solving linear programs of the form
//
// maximize c^T x
// subject to Ax <= b
// x >= 0
//
// INPUT: A -- an m x n matrix
// b -- an m-dimensional vector
// c -- an n-dimensional vector
// x -- a vector where the optimal solution will be stored
//
// OUTPUT: value of the optimal solution (infinity if unbounded
// above, nan if infeasible)
//
// To use this code, create an LPSolver object with A, b, and c as
// arguments. Then, call Solve(x).

#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <limits>

using namespace std;

typedef long double DOUBLE;
typedef vector<DOUBLE> VD;
typedef vector<VD> VVD;
typedef vector<int> VI;

const DOUBLE EPS = 1e-9;

struct LPSolver {
int m, n;
VI B, N;
VVD D;

LPSolver(const VVD &A, const VD &b, const VD &c) :
m(b.size()), n(c.size()), N(n + 1), B(m), D(m + 2, VD(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]; }
for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; }
N[n] = -1; D[m + 1][n] = 1;
}

void Pivot(int r, int s) {
double inv = 1.0 / D[r][s];
for (int i = 0; i < m + 2; i++) if (i != r)
for (int j = 0; j < n + 2; j++) if (j != s)
D[i][j] -= D[r][j] * D[i][s] * inv;
for (int j = 0; j < n + 2; j++) if (j != s) D[r][j] *= inv;
for (int i = 0; i < m + 2; i++) if (i != r) D[i][s] *= -inv;
D[r][s] = inv;
swap(B[r], N[s]);
}

bool Simplex(int phase) {
int x = phase == 1 ? m + 1 : m;
while (true) {
int s = -1;
for (int j = 0; j <= n; j++) {
if (phase == 2 && N[j] == -1) continue;
if (s == -1 || D[x][j] < D[x][s] || D[x][j] == D[x][s] && N[j] < N[s]) s = j;
}
if (D[x][s] > -EPS) return true;
int r = -1;
for (int i = 0; i < m; i++) {
if (D[i][s] < EPS) continue;
if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] ||
(D[i][n + 1] / D[i][s]) == (D[r][n + 1] / D[r][s]) && B[i] < B[r]) r = i;
}
if (r == -1) return false;
Pivot(r, s);
}
}

DOUBLE Solve(VD &x) {
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) {
Pivot(r, n);
if (!Simplex(1) || D[m + 1][n + 1] < -EPS) return -numeric_limits<DOUBLE>::infinity();
for (int i = 0; i < m; i++) if (B[i] == -1) {
int s = -1;
for (int j = 0; j <= n; j++)
if (s == -1 || D[i][j] < D[i][s] || D[i][j] == D[i][s] && N[j] < N[s]) s = j;
Pivot(i, s);
}
}
if (!Simplex(2)) return numeric_limits<DOUBLE>::infinity();
x = VD(n);
for (int i = 0; i < m; i++) if (B[i] < n) x[B[i]] = D[i][n + 1];
return D[m][n + 1];
}
};

int main() {

const int m = 4;
const int n = 3;
DOUBLE _A[m][n] = {
{ 6, -1, 0 },
{ -1, -5, 0 },
{ 1, 5, 1 },
{ -1, -5, -1 }
};
DOUBLE _b[m] = { 10, -4, 5, -5 };
DOUBLE _c[n] = { 1, -1, 0 };

VVD A(m);
VD b(_b, _b + m);
VD c(_c, _c + n);
for (int i = 0; i < m; i++) A[i] = VD(_A[i], _A[i] + n);

LPSolver solver(A, b, c);
VD x;
DOUBLE value = solver.Solve(x);

cerr << "VALUE: " << value << endl; // VALUE: 1.29032
cerr << "SOLUTION:"; // SOLUTION: 1.74194 0.451613 1
for (size_t i = 0; i < x.size(); i++) cerr << " " << x[i];
cerr << endl;
return 0;
}
13 changes: 0 additions & 13 deletions code/Math/inverse.cpp

This file was deleted.

23 changes: 11 additions & 12 deletions code/Math/pollardRho.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
// from PEC
// does not work when n is prime
Int f(Int x, Int mod){
return add(mul(x, x, mod), 1, mod);
inline LL f(LL x , LL mod) {
return (x * x % mod + 1) % mod;
}
Int pollard_rho(Int n) {
if ( !(n & 1) ) return 2;
while (true) {
Int y = 2, x = rand()%(n-1) + 1, res = 1;
for ( int sz = 2 ; res == 1 ; sz *= 2 ) {
for ( int i = 0 ; i < sz && res <= 1 ; i++) {
x = f(x, n);
res = __gcd(abs(x-y), n);
inline LL pollard_rho(LL n) {
if(!(n&1)) return 2;
while(true) {
LL y = 2 , x = rand() % (n - 1) + 1 , res = 1;
for(int sz = 2; res == 1; sz *= 2) {
for(int i = 0; i < sz && res <= 1; i++) {
x = f(x , n);
res = __gcd(abs(x - y) , n);
}
y = x;
}
if ( res != 0 && res != n ) return res;
if (res != 0 && res != n) return res;
}
}
21 changes: 20 additions & 1 deletion code/Math/theorem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,24 @@ construct a solution:
Burnside's lemma
|G| * |X/G| = sum( |X^g| ) where g in G
總方法數: 每一種旋轉下不動點的個數總和 除以 旋轉的方法數
*/
-------------------------------------------------------
Lagrange multiplier
f(x,y) 求極值。必須滿足 g(x,y) = 0。
湊得 f(x,y) = f(x,y) + λ g(x,y)
定義 s(x,y,λ) = f(x,y) + λ g(x,y)
f(x,y) 的極值,等同 s(x,y,λ) = f(x,y) + λ g(x,y) 的極值。
欲求極值:
對 x 偏微分,讓斜率是 0。
對 y 偏微分,讓斜率是 0。
不管 λ 如何變化,λ g(x,y) 都是零,s(x,y,λ) 永遠不變。
欲求永遠不變的地方:
對 λ 偏微分,讓斜率是 0。
三道偏微分方程式聯立之後,其解涵蓋了(不全是)所有符合約束條件的極值。
{ ∂/∂x s(x,y,λ) = 0
{ ∂/∂y s(x,y,λ) = 0
{ ∂/∂λ s(x,y,λ) = 0
*/

0 comments on commit d223ce5

Please sign in to comment.