A Poor King
Tag: Reversed BFS
Preprocessing is needed to calculate answers for all positions (states). Beginning with all checkmate states, you should do reversed Breath-First Search (BFS) to update values. The state can be specified by three positions, so 646464 states in all. For each state, make all possible next states and check if all their values had already determined. If so, you can determine the value (answer) for this state.
Best Division
Tag: DP, Trie, Data structure
This is a Dynamic Programming (DP) problem. Let dp[i] be the answer (maximum K) for the subarray A[1]~A[i]. Then, dp[i] = (max i-L≤j<i, S[i]^S[j]≤X dp[j]) + 1 (S[i] = A[1]A[2]…^A[i]). Now, build bit (1-0) trie to add S[i] values in order. For each i, indices such that S[i]^S[j]≤X is determined by going down along the trie, considering X. Maximum values of a subtree can be stored in its root. Adding or removing values to/from the trie can be done by suitable data structure such as sliding RMQ or multiset.
Convex Hull
Tag: Geometry, 3D transform, Convex hull
Several geometric procedures are needed to solve this problem. First, construct the 3D convex hull of given points. Then, for each query, use CG formulas to transform the query plane to XOY plane. After that, you can find out all intersection points between the edges of convex hull and XOY plane. Now, answer is the area of 2D convex hull of these points.
Different Sums
Tag: Construction, Math
For small n, it’s easy problem.
Let S[i] be the sum of first i numbers.
We take a prime number p larger than n, and an integer x (0 <= x < p), make S[i] = 2 * i * p + (i * (i+1) / 2 * x) % p. Let’s define r[i] = (i * (i+1) / 2 * x) % p.
If S[i] – S[j] = S[k] – S[l], then i – j = k – l because |r[i] -r[j]| < p and |r[k] – r[l]| < p. Also (r[i] – r[j] – r[k] + r[l]) must be divisible by p, this leads to i + j = k + l. It means that i = k and j = l.
Thus, the array that has these subsums satisfies the problem statement.
Now we must make all integers in the array less than 3n+18.
We can choose p as the smallest prime larger than n, and select x that minimizes max{S[i]-S[i-1]}.
For all 6 <= n <= 2000, if we choose x optimally, the maximum value of S[i]-S[i-1] is less than 3n+18.
Easy Homework
Tag: Number theory, Baby-step giant-step algorithm
A simple identity f(n+1) * f(n-1) – f(n) * f(n) = (-1) ^ n holds for all integers n.
Assume that f(n)=x, f(n-1)=y, then a quadratic modular congruence (Ax+y)y –x *x = (-1) ^ n holds.
Thus, the number of different pairs (x, y) is O(p) for modulo p, so the period must be O(p). Also for given x, there are at most four different y values. All y candidates can be calculated by Shanks-Tonelli algorithm. Now, you pre-calculate sqrt(p) pairs and check whether (x, y) pair appears using baby-step giant-step algorithm. By doing so, the period can be computed in O(sqrt(p)) steps and the total time complexity is O(sqrt(p)), the problem can be solved.
Flight
Tag: Data structure, RMQ
We can easily find out the answer for each query can be an integer from -1 to 3.
First find the diameter D of the given tree. Let’s assume that its two end nodes are U and V. From now, U is the root of the tree, and the path from U to V will be marked. Let’s denote f(u) as the nearest marked node from u.
Determining the possibility in two steps is the most important part.
This will be determined by min{dist(U, u) , dist(U, v)}>=d or min{dist(V, u) , dist(V, v)}>=d, or there exists at least one node w, f(w) is on the path from f(u) to f(v)), min{dist(w, u), dist(w, v)} >= d. The last condition can be checked by considering the center c of the path from u to v. So for all nodes w, if f(w) is on the path f(u) to f(c), dist(u, w) >=d, else dist(v, w) >=d must be held. This leads a simple range maximum query problem. These can be pre-calculated, so we can answer each query by O(1) time. So required time is O(N*log(N)+Q).
Generator
Tag: Inclusion/Exclusion principle, String Matching, Linear equations.
Consider the probability that all sequences are generated in i seconds. Let’s denote it as f(i). The answer is the sum of i(f(i)-f(i-1)) for all positive integers i. By inclusion/exclusion principle, f(i) is the signed sum of the probability of 2^N subsets of sequences are not generated in i seconds. For each subset S, let’s denote it as p(S, i). Then q(S,i) = 1-p(S,i) is the probability which at least a sequence of S is generated in i seconds. So f(i) is the signed sum of q(S,i), for all non-empty subset of {1,2,…,N}. The sign is +1 for odd subset, -1 for even subset. For a fixed subset S, summing i(q(S, i)-q(S,i-1)) for all integer i, it’s simply the expected time of at least a sequence of S is generated. Let’s denote it as e(S). So the answer is the signed sum of all e(S).
e(S) can be calculated by linear equation.
Let x(i) will be the probability i-th sequence of S is the first generated.
Then sum of all x(i) equals to 1. And there are |S| equations about x(i) and e(S). All coefficients are calculated by KMP matching. So the answer can be calculated by O(NNL+NNN*2^N).
Honey Tour
Tag: Plug DP, Matrix exponentiation
Cells on a path have exactly two adjacent on-path cells except the two ends.
Combine connected component id and cell’s degree to represent DP state.
The number of valid states Ns is less than 200.
Calculate state transition matrix by passing the maze once.
Intersection is not allowed!
Tag: Counting, Matrix
For the simplest case, if K = 1, the number of different ways from (1, a1) to (N, b1) is . Here, means binomial coefficient.
If K = 2, the answer is the product of two independent numbers of ways minus number of intersecting ways. Here, all intersecting ways correspond to the ways from (1, a1) to (N, b2) and from (1, a2) to (N, b1). Thus, the answer is
(C_{(b_1-a_1)+(N-1)}^{N-1} imes C_{(b_2-a_2)+(N-1)}^{N-1}-C_{(b_2-a_1)+(N-1)}^{N-1} imes C_{(b_1-a_2)+(N-1)}^{N-1})
(= egin{vmatrix} C_{(b_1-a_1)+(N-1)}^{N-1} & C_{(b_2-a_2)+(N-1)}^{N-1}\ C_{(b_1-a_2)+(N-1)}^{N-1} & C_{(b_2-a_1)+(N-1)}^{N-1} end{vmatrix})
Let (f(i,j) = C_{(b_j-a_i)}^{N-1})be the number of ways from (1, ai) to (N, bj).
In general, it can be proved that the answer is represented as the following determinant:
(|f(i,j)_{K*K}| =egin{vmatrix}C_{(b_1-a_1)+(N-1)}^{N-1} &cdots &C_{(b_K-a_2)+(N-1)}^{N-1}\cdots & cdots & cdots \C_{(b_1-a_K)+(N-1)}^{N-1} & cdots & C_{(b_K-a_K)+(N-1)}^{N-1}end{vmatrix})
Jong Hyok and String
Tag: Suffix automata
Suffix Automaton’s each node has corresponding set of strings related to it.
Strings in this set have common occurrences in the given strings P1, …, PN.
Therefore, your task is to count the number of strings related to the SAM node related to string qi.
This can be done by calculating len[u]-len[link[u]].
K-th Value
Tag: Binary search
Binary search for Ans.
Root the tree at an arbitrary node.
We define f(i, l) as the maximum number of edges whose length isn’t bigger than Ans of any downward path starting from i and with length l.
And define g(i, l) = k * f(i, l) – l.
If there exists a pair (i, l) with L <= l <= R and g(i, l) > 0, then return true.
Otherwise if there exists two pairs (i, l) and (j, h) with L <= l + h <= R and parent(i) = parent(j) and g(i, l) + g(j, h) > 0 return true.
This could take NR time using sweeping.
Otherwise return false.
So the time complexity is N * R * logN.
Less Time, More Profit
Tag: Binary search, Maximum flow
Find the minimum time by using binary search. Let’s see time tm is valid!
Make the graph G(V, E, C). V: nodes, E: edges C: value of nodes
V: = X + Y, X = {1, 2, …, N} , Y = {1, 2, … M}
E: (v, u), , we need u to enable v.
C: c[v] = pro[v]
Calculate maximum weight closure of G by using maximum flow.
When this value is not less than L, tm is valid.
Math is Fun
Tag: DP, State compression, Inclusion-exclusion principle
For every integer g, calculate the GLL value of the subset consists of multiples of g in A. This is done by DP-like approach. Represent LCMs as its prime factorization state. Use map (or unordered_map) to hash states. When adding a number, update map with original states and new states which is LCM of original state and a new number. For branching and bounding, primes that do not appear further should be removed to compress and merge states. This method makes it possible to calculate the sum of LCM (or LCM^2) very quickly. Finally, compute the answer from GLL of many g values, by using inclusion-exclusion principle.