Cheat Sheet of SSE/AVX Intrinsics, for Doing Arithmetic Ll F(Int Ind, Int K) { Return Dp[Ind][K]; } on Several Numbers at Once
Total Page:16
File Type:pdf, Size:1020Kb
University of Bergen Garbage Collectors Davide Pallotti, Jan Soukup, Olav Røthe Bakken NWERC 2017 Nov 8, 2017 UiB template .bashrc .vimrc troubleshoot 1 tan v + tan w Contest (1) Any possible infinite recursion? tan(v + w) = Invalidated pointers or iterators? 1 − tan v tan w template.cpp Are you using too much memory? v + w v − w 15 lines Debug with resubmits (e.g. remapped signals, see Various). sin v + sin w = 2 sin cos #include <bits/stdc++.h> 2 2 using namespace std; Time limit exceeded: v + w v − w Do you have any possible infinite loops? cos v + cos w = 2 cos cos #define rep(i, a, b) for(int i = a; i < (b); ++i) What is the complexity of your algorithm? 2 2 #define trav(a, x) for(auto& a : x) Are you copying a lot of unnecessary data? (References) #define all(x) x.begin(), x.end() How big is the input and output? (consider scanf) (V + W ) tan(v − w)=2 = (V − W ) tan(v + w)=2 #define sz(x) (int)(x).size() Avoid vector, map. (use arrays/unordered_map) typedef long long ll; What do your team mates think about your algorithm? where V; W are lengths of sides opposite angles v; w. typedef pair<int, int> pii; typedef vector<int> vi; Memory limit exceeded: a cos x + b sin x = r cos(x − φ) What is the max amount of memory your algorithm should need? int main() { Are you clearing all datastructures between test cases? a sin x + b cos x = r sin(x + φ) cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); p 2 2 } Mathematics (2) where r = a + b ; φ = atan2(b; a). .bashrc 3 lines 2.1 Equations 2.4 Geometry alias c=’g++ -Wall -Wconversion -Wfatal-errors -g -std=c++14 \ p -fsanitize=undefined,address’ 2 2.4.1 Triangles 2 −b ± b − 4ac xmodmap -e ’clear lock’ -e ’keycode 66=less greater’ #caps = <> ax + bx + c = 0 ) x = 2a Side lengths: a; b; c .vimrc a + b + c 2 lines The extremum is given by x = −b=2a. Semiperimeter: p = set cin aw ai is ts=4 sw=4 tm=50 nu noeb bg=dark ru cul 2 sy on | im jk <esc> | im kj <esc> | no ; : Area: A = pp(p − a)(p − b)(p − c) ed − bf abc troubleshoot.txt x = Circumradius: R = 52 lines ax + by = e ad − bc ) 4A Pre-submit: cx + dy = f af − ec A Write a few simple test cases, if sample is not enough. y = Inradius: r = Are time limits close? If so, generate max cases. ad − bc p Is the memory usage fine? Length of median (dividesp triangle into two equal-area Could anything overflow? In general, given an equation Ax = b, the solution to a 1 2 2 2 Make sure to submit the right file. triangles): ma = 2 2b + 2c − a variable xi is given by Length of bisector (divides angles in two): Wrong answer: v Print your solution! Print debug output, as well. 0 u " 2# det Ai a Are you clearing all datastructures between test cases? x = u i sa = tbc 1 − Can your algorithm handle the whole range of input? det A b + c Read the full problem statement again. 0 Do you handle all corner cases correctly? where Ai is A with the i'th column replaced by b. sin α sin β sin γ 1 Have you understood the problem correctly? Law of sines: = = = Any uninitialized variables? a b c 2R 2.2 Recurrences 2 2 2 Any overflows? Law of cosines: a = b + c − 2bc cos α Confusing N and M, i and j, etc.? α + β Are you sure your algorithm works? If an = c1an−1 + ··· + ckan−k, and r1; : : : ; rk are distinct a + b tan What special cases have you not thought of? roots of xk + c xk−1 + ··· + c , there are d ; : : : ; d s.t. Law of tangents: = 2 Are you sure the STL functions you use work as you think? 1 k 1 k a − b α − β Add some assertions, maybe resubmit. n n tan Create some testcases to run your algorithm on. an = d1r1 + ··· + dkrk : 2 Go through the algorithm for a simple case. Go through this list again. Non-distinct roots r become polynomial factors, e.g. 2.4.2 Quadrilaterals Explain your algorithm to a team mate. n Ask the team mate to look at your code. an = (d1n + d2)r . Go for a small walk, e.g. to the toilet. With side lengths a; b; c; d, diagonals e; f, diagonals angle θ, Is your output format correct? (including whitespace) area A and magic flux F = b2 + d2 − a2 − c2: Rewrite your solution from the start or let a team mate do it. 2.3 Trigonometry Runtime error: p 2 2 2 Have you tested all corner cases locally? sin(v + w) = sin v cos w + cos v sin w 4A = 2ef · sin θ = F tan θ = 4e f − F Any uninitialized variables? cos(v + w) = cos v cos w − sin v sin w Are you reading or writing outside the range of any vector? For cyclic quadrilaterals the sum of opposite angles is 180◦, Any assertions that might fail? p Any possible division by 0? (mod 0 for example) ef = ac + bd, and A = (p − a)(p − b)(p − c)(p − d). UiB 2 2.4.3 Spherical coordinates 2.7 Series First success distribution z x2 x3 ex = 1 + x + + + :::; (−∞ < x < 1) The number of trials needed to get the first success in r 2! 3! y independent yes/no experiments, each wich yields success x2 x3 x4 with probability p is Fs(p); 0 ≤ p ≤ 1. ln(1 + x) = x − + − + :::; (−1 < x ≤ 1) 2 3 4 x p(k) = p(1 − p)k−1; k = 1; 2;::: p x x2 2x3 5x4 1 + x = 1 + − + − + :::; (−1 ≤ x ≤ 1) 2 8 32 128 1 1 − p p µ = ; σ2 = x = r sin θ cos φ r = x2 + y2 + z2 2 x3 x5 x7 p p y = r sin θ sin φ θ = acos(z=px2 + y2 + z2) sin x = x − + − + :::; (−∞ < x < 1) 3! 5! 7! z = r cos θ φ = atan2(y; x) Poisson distribution x2 x4 x6 cos x = 1 − + − + :::; (−∞ < x < 1) 2.5 Derivatives/Integrals 2! 4! 6! The number of events occurring in a fixed period of time t if these events occur with a known average rate κ and 2.8 Probability theory independently of the time since the last event is d 1 d 1 arcsin x = p arccos x = −p Let X be a discrete random variable with probability p (x) Po(λ); λ = tκ. dx 1 − x2 dx 1 − x2 X of assuming the value x. It will then have an expected value k d 2 d 1 P −λ λ tan x = 1 + tan x arctan x = (mean) µ = E(X) = x xpX (x) and variance p(k) = e ; k = 0; 1; 2;::: dx dx 1 + x2 2 2 2 P 2 k! σ = V (X) = E(X ) − (E(X)) = (x − E(X)) pX (x) Z ln j cos axj Z sin ax − ax cos ax x tan ax = − x sin ax = where σ is the standard deviation. If X is instead 2 2 µ = λ, σ = λ a a continuous it will have a probability density function fX (x) p ax Z 2 π Z e and the sums above will instead be integrals with p (x) e−x = erf(x) xeaxdx = (ax − 1) X 2 a2 replaced by fX (x). 2.8.2 Continuous distributions Expectation is linear: Uniform distribution Integration by parts: E(aX + bY ) = aE(X) + bE(Y ) If the probability density function is constant between a and Z b Z b b 0 b and 0 elsewhere it is U(a; b); a < b. f(x)g(x)dx = [F (x)g(x)]a − F (x)g (x)dx For independent X and Y , a a 1 a < x < b V (aX + bY ) = a2V (X) + b2V (Y ): f(x) = b−a 2.6 Sums 0 otherwise b+1 a 2 a a+1 b c − c 2.8.1 Discrete distributions a + b (b − a) c + c + ··· + c = ; c 6= 1 µ = ; σ2 = c − 1 Binomial distribution 2 12 n(n + 1) The number of successes in n independent yes/no Exponential distribution 1 + 2 + 3 + ··· + n = 2 experiments, each which yields success with probability p is n(2n + 1)(n + 1) Bin(n; p); n = 1; 2;:::; 0 ≤ p ≤ 1. The time between events in a Poisson process is 12 + 22 + 32 + ··· + n2 = Exp(λ); λ > 0. 6 n n2(n + 1)2 p(k) = pk(1 − p)n−k 13 + 23 + 33 + ··· + n3 = λe−λx x ≥ 0 k f(x) = 4 0 x < 0 n(n + 1)(2n + 1)(3n2 + 3n − 1) 14 + 24 + 34 + ··· + n4 = µ = np; σ2 = np(1 − p) 30 1 1 µ = ; σ2 = Bin(n; p) is approximately Po(np) for small p. λ λ2 UiB OrderStatisticTree SegmentTree LazySegmentTree UnionFind SubMatrix 3 Normal distribution OrderStatisticTree.h struct Node { Description: A set (not multiset!) with support for finding the n'th ele- Node *l = 0, *r = 0; ment, and finding the index of an element. int lo, hi, mset = inf, madd = 0, val = -inf; 2 Time: O (log N) Node(int lo,int hi):lo(lo),hi(hi){} // Large interval of −inf Most real random values with mean µ and variance σ are 16 lines 2 Node(vi& v, int lo, int hi) : lo(lo), hi(hi) { well described by N (µ, σ ); σ > 0.