0% found this document useful (0 votes)
79 views

Problem Tutorial: "Erase Nodes": 1 CNT (U, V)

The document contains tutorials for solving several programming contest problems. It discusses approaches for problems involving erasing nodes in a graph, counting occurrences in a linear congruential generator sequence, finding positions in a Fibonacci sequence, and calculating minimum cuts in a honeycomb graph. Key steps and time complexities are outlined for dynamic programming, number theory, and graph algorithms to solve the problems efficiently.

Uploaded by

Roberto Franco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Problem Tutorial: "Erase Nodes": 1 CNT (U, V)

The document contains tutorials for solving several programming contest problems. It discusses approaches for problems involving erasing nodes in a graph, counting occurrences in a linear congruential generator sequence, finding positions in a Fibonacci sequence, and calculating minimum cuts in a honeycomb graph. Key steps and time complexities are outlined for dynamic programming, number theory, and graph algorithms to solve the problems efficiently.

Uploaded by

Roberto Franco
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Day 8: Jingzhe Tang Contest 2, XIX Open Cup Onsite

37th Petrozavodsk Programming Camp, Summer 2019, Sunday, September 1, 2019

Problem Tutorial: “Erase Nodes”


The number of updates can be counted as the number of ordered pairs (u, v) such that when u is going
to be removed, there exists at least one path between u and v.
If the path between u and v is unique in the original graph, we can conclude that pair exists if u is the
1
first removed node on the path with respect to an order, and cnt(u,v) of all possible orders would meet
this condition, where cnt(u, v) is the number of nodes on the unique path between u and v (inclusive).
In other cases, there exist exactly two paths between u and v at the beginning, each passing through one
part of the only cycle in the graph. Let the number of nodes on these two paths be x, y respectively,
and the number of their common nodes be z. The probability that an order corresponds to this pair is
1 1 1
x + y − x+y−z .
Let p1 , p2 , . . . , pm be the nodes on the only cycle in order. Let’s for each pi (i = 1, 2, . . . , m) build a rooted
tree from pi without passing any edges on the cycle.
We can firstly handle the case that the path between u and v is unique, which implies u and v are in the
same rooted tree we have built. In this case, we only need to count the number of paths for each possible
length, so we can use Centroid Decomposition with Number-Theoretic Transform to do this in total time
complexity O(n log2 n).
Then we have to handle the case passing through the cycle. We can use Divide and Conquers to calculate
the contribution of (u, v), that is, when we merge information from [L, M ] and [M + 1, R] into [L, R],
we can count for pairs (u, v) such that u is from the union of trees whose roots are pL , pL+1 , . . . , pM
and v is from that for pM +1 , pM +2 , . . . , pR , and vice versa. This part can be done in time complexity
O(n log n log m).
By the way, it is not difficult to count the number of paths for each possible length if the given graph is
a cactus.

Problem Tutorial: “Linear Congruential Generator”


j k  j k
Note that Xi mod (Xj + 1) = Xi − XX i
j +1
(Xj + 1), and the number of pairs X j , Xi
Xj +1 is
Pm  m−1  
O i=1 i + 1 = O(m log m). If we can get the number of occurrences in an interval of the
sequence for values in [0, m) and calculate the partial sum, we can get the contribution of all Xi for each
pair (Xj , k) such that k(Xj + 1) ≤ Xi < (k + 1)(Xj + 1), and solve the problem in time complexity
O(m log m).
Applying the pigeonhole principle, there must exist two integers p and q such that 0 ≤ p < q ≤ m and
Xp = Xq . Also, for any non-negative integer k, we have Xp+k = Xq+k , and thus {Xn }∞ n=p is a periodic
sequence. We can easily count the number of occurrences for all possible values in [l1 , r1 ] and [l2 , r2 ]
respectively in total time complexity O(m).

Problem Tutorial: “Fibonacci Strikes Back”


If you are very familiar with quadratic congruences, you will know that, under the given data range,
the number of candidate positions for the answer in a period of the P -Fibonacci sequence in modulo 10k
is relatively small, like 8, and positions for 10k are easy to obtain from positions for 10k−1 . Together with
a series of rough analyses, you can write a Breath-First Search to find the answer.
Let’s solve the problem step by step. First, we can prove that {(Fk+1 ∞
 , Fk )}
 k=0 is a pure periodic
 sequence

0 1 k Fk−1 Fk
in modulo any integer m, as we can build an invertible matrix A = such that A = .
1 P Fk Fk+1
Let L(m) be the period length of the P -Fibonacci sequence in modulo m, which means Fi ≡ Fi+L(m)
(mod m) for any position i. We can try to find all possible x such that 0 ≤ x < L(10k ) and Fx ≡ FFn
(mod 10k ), and then for each x, find all possible y such that 0 ≤ y < L(L(10k )) and y ≡ Fx (mod L(10k )).
The answer n must be in the form of (y + t · L(L(10k )), so we can find the smallest integer t for each y
such that n ≥ m, and when (FFn mod 10k ) has leading zeros (k > 1), FFn ≥ 10k .

Page 1 of 6
Day 8: Jingzhe Tang Contest 2, XIX Open Cup Onsite
37th Petrozavodsk Programming Camp, Summer 2019, Sunday, September 1, 2019

Before that, we have to precalculate L(10k ) and L(L(10k )) first. In number theory, we know that

• L(p1 e1 p2 e2 . . . pt et ) = lcm(L(p1 e1 ), L(p2 e2 ), . . . , L(pt et )), e.g. L(10k ) = lcm(L(2k ), L(5k ));

• pe−1 L(p)|L(pe ) for any prime p and any positive integer e, e.g. 2k−1 L(2)|L(2k );

L(10k )
By enumerating different possible P , we can get L(2) ∈ {2, 3}, L(5) ∈ {12, 20}, so 10k−1
∈ {3, 6, 10, 15}
L(L(10k ))
when k ≥ 3. Together with L(3) ∈ {2, 8}, we have ∈ {3, 12, 75, 100} when k ≥ 5. Cases for
10k−2
smaller k are trivial, and by the way, for convenience of computing, we can calculate the P -Fibonacci
sequence in modulo 3 · 10k when k ≥ 3, which is the lowest common multiple of all possible modulo
numbers.
Then, we have to prove another insight — for any value v, the number of positions x such that
Fx ≡ v (mod 10k ) is small. Obviously, it only depends on the number of possible values Fx−1 , as each
possible pair (Fx−1 , Fx ) appears in a period only once. As we know det(Ax ) = detx (A) = (−1)x and
det(Ax ) = Fx−1 Fx+1 − Fx2 , we can get a equation Fx−1
2 + P Fx Fx−1 − Fx2 ≡ (−1)x (mod 10k ), which can
be rewritten as in the form of (2u + a)2 ≡ b (mod 4 · 10k ), when Fx and the parity of x are given.
The number of solutions x in modulo 2k+2 · 5k for the equation x2 ≡ y is the product of numbers of
solutions in modulo 2k+2 and 5k separately, so we only care about the number of solutions in modulo a
power of a prime pe . Let’s discuss in several cases.

1. When y ≡ 0 (mod pe ), each solution x only needs to satisfy pde/2e |x, so the number of solutions is
pbe/2c ;

2. When y = pu · v, 1 ≤ u < e, p - v, if there exists any solution, then 2|u and pu/2 |x. Assuming that
the number of solutions for the equation x0 2 ≡ v (mod pe−u ) be cnt, the number of solutions for
the equation x2 ≡ y (mod pe ) is pu−u/2 cnt;

3. When p - y and there exists a primitive root g in modulo pe (i.e. p is odd or e ≤ 2), let x ≡ g u , a ≡ g v
(mod pe ), and then we know 2u ≡ v (mod ϕ(pe )), which is a linear congruence having at most 2
solutions;

4. When p - y, p = 2, e > 2, we can use a pseudo-primitive root g 0 to represent all values in modulo pe
as in the form of (−1)u g 0 v , so it tells us the number of solutions is at most 4.

Since gcd(P, 1018 ) ∈ {1, 2, 4}, we don’t have to handle some larger cases, such as, finding x with Fx ≡ 1
(mod 10k ), when k = 18, P = 10k/2 , in which case the P -Fibonacci sequence is [0, 1, P, 1, 2P, 1, 3P, . . .],
k
and the number of positions for 1 is 10P = 10k/2 . Therefore, based on the above rules and a few discussions,
we can conclude that for any k, the number of solutions in [0, L(10k )) for Fx ≡ v (mod 10k ) is at most
8. (Although (2u + a)2 ≡ b ± 1 (mod 4 · 10k ) has 16 solutions u in modulo 2 · 10k , every two solutions
u are equivalent in modulo 10k .) This can help us avoid any calculation about quadratic congruences.
That is, we can find all solutions in modulo 10e and transform them into solutions in modulo 10e+1 . More
e+1 )
specifically, for each solution x ≡ x0 (mod L(10e )) for the former case, we can examine if x ≡ x0 +t L(10
L(10e )
(mod L(10e+1 )) is a solution for the latter case, and that works very efficiently. Note that when it comes
to finding y ≡ Fx (mod L(10k )), the number of solutions may double.
If we just find solutions for Fx ≡ FFn (mod 10k ) and y ≡ Fx (mod L(10k )) forcibly, the time complexity
will be O(8 · 16 · (120 + 10k)). If we prepare some information for k < 3, the time complexity can be even
better.

Problem Tutorial: “Honeycomb”


Since every non-special cell has no traversable edge, we can only build an undirected graph for special
cells, and the problem can be formed as calculating the cost of a minimum s-t cut for every two nodes.

Page 2 of 6
Day 8: Jingzhe Tang Contest 2, XIX Open Cup Onsite
37th Petrozavodsk Programming Camp, Summer 2019, Sunday, September 1, 2019

In fact, Gomory-Hu tree shows that we can build a weighted tree for these nodes such that each min-cut
can be represented as the minimum value on the path between the source and the sink. If we can build
the tree, we can easily get the answer by adding edges in decreasing order of weight.
To build the tree, we need to calculate (|V | − 1) times min-cut. Fortunately, the degree of each node is at
most 6, which implies the cost of any min-cut is at most 6, so we can use Dinic’s algorithm to calculate
min-cut forcibly, and the time complexity is O(6|V |(|V | + |E|)), where |V | ≤ 3000, |E| ≤ 3|V |.
A well-known implementation uses ‘divide and conquers’ to build the tree with vertex contractions,
however, we can do it more easily. Here is a brief introduction to Gusfield’s algorithm, which is also
used in this problem’s standard program:

• Label nodes from 0 to (|V | − 1), and set the parent of node i (i = 1, 2, . . . , |V | − 1) as 0;

• For each node i (i = 1, 2, . . . , |V | − 1):

– Find a min-cut (S, T ) between i and its parent pi , where i ∈ S, pi ∈ T ;


– For each node j such that i < j, j ∈ S, pj = pi , set pj as i.

Problem Tutorial: “Power of Function”


Let n = ei=0 ai k i , such that a0 , a1 , . . . P
P
, ae are all integers between 0 and (k − 1) and ae > 0. We can
conclude when f m (n) = 1, m = e − 2 + ei=0 ai .
Let’s partition the interval [l, r + 1) into many non-intersect subintervals, such that each subinterval is like
[a + bk u , a + ck u ), where a is a multiple of k u+1 , and b, c are integers between 0 and k. Since a + bk u > 0,
there is only one possible n = a + ck u − 1 with the maximum m in each subinterval. Like working on the
segment tree, we can find O(logk n) subintervals, and merge their information to get the answer.
By the way, you may notice that m must be from an integer n whose length in the base of k is either e
or (e − 1), and then discuss 11 possible cases.

Problem Tutorial: “Square Subsequences”


l m
n
It is easy to see the lower bound of the answer is |Σ| , and make it difficult to be passed by a brute-force
solution — enumerate the position of tm/2+1 and run a DP to find the longest common subsequence (LCS)
of two parts. However, we canspeed  up the process of calculating LCS by bit-level parallelism and pass
n3
the tests in time complexity O w , where w is the processor word size (which is 64 on 64-bit computers).
Assuming that we are finding LCS of s1 s2 · · · sp and sp+1 sp+2 · · · sn , we can design a DP like dp(i, j)
which means the length of LCS of s1 s2 · · · si and sp+1 sp+2 · · · sp+j , and get dp(i, j) from the maximum of
dp(i − 1, j) and dp(i, j − 1) when si 6= sp+j , or otherwise from dp(i − 1, j − 1) + 1.
Note that the transition makes dp(i, j − 1) ≤ dp(i, j) ≤ dp(i, j − 1) + 1, so we can define
f (i, j) = dp(i, j) − dp(i, j − 1) to convert the information into bits. Let’s define g(i, j) = [si = sp+j ],
where [x] is 1 when x is true, or otherwise 0, and then with further discussion or observation, we can get
the transition from f (i − 1) to f (i) :

• Let x1 , x2 , xk be the positions such that 1 ≤ x1 < x2 < . . . < xk ≤ n − p and f (i − 1, xt ) = 1 for
t = 1, 2, . . . , k, and define x0 , xk+1 as 0, (n − p + 1) respectively;

• for each xt (t = 1, 2, . . . , k + 1):

– find the lowest position u such that xt−1 < u ≤ xt and g(i, u) = 1, or in case there is no such
u, define u as xt ;
– set f (i, u) as 1, and f (i, v) as 0 for u = xt−1 +1, xt−1 +2, . . . , u−1, u+1, u+2, . . . , min{xt , n−p}.

Page 3 of 6
Day 8: Jingzhe Tang Contest 2, XIX Open Cup Onsite
37th Petrozavodsk Programming Camp, Summer 2019, Sunday, September 1, 2019

The above rule can be interpreted as that, for each interval in f (i − 1) whose bits are all zeros except for
the highest position, find the lowest position in (f (i − 1)|g(i)) that is 1 (i.e. ON), and set the interval as all
zeros except for the lowest ON position. This can be done by bitwise operations easily, for example, we can
use (f (i − 1) << 1|1) to get the lowest position in each interval, use ((f (i − 1)|g(i)) − (f (i − 1) << 1|1)) to
make the only difference on the lowest ON position in each interval, and then use other bitwise operations
to get them.
Though we can retrieve a needed subsequence from f , you can do a slower DP at the end so that you
can get the subsequence without any other thought. In addition, we don’t need to calculate g(i) for each
position i but for each character si , and that can be obtained by (n − p) changes.
By the way, if you are familiar with computing all LCS of a given string A and each substring of a given
string B in time complexity O(|A||B|), you may find a better solution. This problem is known as the
all-substrings longest common subsequence (ALCS) problem.

Problem Tutorial: “Cosmic Cleaner”


This is a typical 3D geometry problem. One thing you need to know is the volume of a spherical cap of
radius r and height h, which is

h h h
πh2
Z Z 
p 2 1
V = 2 2
π r − (x − r) dx = π (2rx − x )dx = π rx − x3
2 2
= (3r − h).
0 0 3 0 3

Problem Tutorial: “Quicksort”


We can prove that no matter how the pivot is selected, each possible permutation for the deeper level
appears with equal probability, so the expected number of inversions only depends on the level and the
length of the interval.
Let dp(i, j) be the expected number of inversions when n = i, k = j, and we have
( i(i−1)
if j = 0
dp(i, j) = Pi4 .
x=1 dp(x−1,j−1)+dp(i−x,j−1)
n otherwise

We can calculate partial sums to optimize it into time complexity O(max2 {n}).

Problem Tutorial: “Routes”


This unweighted graph is formed by k non-intersect cliques and m non-intersect chains, so it has many
specialties. For example, we can conclude the maximum value of the shortest distance between any two
cities is at most (k + k − 1), as we can pass one edge to all the cities in the same district, and travel to
any other city by passing through each district at most once.
If we take the hot air balloon at least once, for example, at the i-th district to travel from city u to city v,
we can conclude the minimum distance is (f (i, u) + f (i, v) + 1), where f (i, x) is the shortest distance from
u to any city in the i-th district. Consequently, the shortest distance from u to v is either the distance on
the railway, if they are on the same railway, or minki=1 {f (i, u) + f (i, v) + 1}. Since the shortest distance
is less than or equal to (k + k − 1), we can ignore the former case at first, and correct the distence after
calculating the latter case.
Let du be the district city u belongs to, and g(i, j) be the shortest distance from any city in
the i-th district to any city in the j-th district. Then, we have f (i, u) ≤ g(i, du ) + 1, and
minki=1 {f (i, u) + f (i, v) + 1} ≤ minki=1 {g(i, du ) + g(i, dv ) + 3}.
Let’s set a bitmask masku for u such that the i-th bit is 1 when f (i, u) = g(i, du ) + 1, or otherwise it is 0.
Then, for each pair of districts (du , dv ), we can discuss the three possible cases of the shortest distance by
masku , maskv . For example, if minki=1 {f (i, u) + f (i, v) + 1} is obtained from (g(j, du ) + g(j, dv ) + 1), then

Page 4 of 6
Day 8: Jingzhe Tang Contest 2, XIX Open Cup Onsite
37th Petrozavodsk Programming Camp, Summer 2019, Sunday, September 1, 2019

P
the j-th bit of both masku and maskv must be zeros. After precalculating cnt(i, S) = du =i,masku ⊆S 1,
we can count for each pair (du , dv ) in time complexity O(2k ) by inclusion-exclusion principle.
We can use a breadth-first search to calculate f (i, u) for each i in time complexity O(n), and enumerate
pairs (u, v) such that u and v are on the same railway with distance less than (k +k −1) in time complexity
O(nk), so the total time complexity is O(nk + k 2 2k ).
By the way, though we haven’t proved it yet, it is showed that the number of different pairs (du , masku )
k−1 k−1 k−1

, and thus it yields a solution in time complexity O(nk + k 6 ).
 
is at most k 2 + 1 + 0

Problem Tutorial: “Square Substrings”


We call the substring si si+1 · · · sj a run, denoted by (i, j, d), if there exists a smallest d such that

• 2d ≤ j − i + 1;

• sk = sk+d for k = i, i + 1, . . . , j − d;

• si−1 6= si+d−1 if si−1 exists;

• sj+1 6= sj−d+1 if sj+1 exists.

With respect to a strict total order < on the alphabet Σ, we call a string w Lyndon word if w < u is true
for each proper suffix u of w. Since for a run (i, j, d) we have sj+1 6= sj−d+1 , we can always find a Lyndon
word su su+1 · · · sv with respect to a strict total order < or its reversed order, such that v − u + 1 = d and
i < u ≤ i + d. Also, it can be proved that different runs don’t share any such words, and thus the number
of runs is O(n).
By further proof, we can get (i,j,d) is a run j−i+1
P
d ≤ 3n − 3, which implies that, if we pick up all the squres
as square-triples (L, R, k) such that each length-k substring of sL sL+1 · · · sR is a square, the number of
these square-triples are less than 23 n. If we can pick up these square-triples, we can solve the problem
using some data structures.
First, let’s talk about how to get these triples. There are many ways to detect runs, and then you can
pick up all the square-triples. For example, you can enumerate d and positions 1, d + 1, 2d + 1, . . . in the
string so that you can check if two consecutive positions are in the same run and then detect the run.
Alternatively, you can linearly build the Lyndon tree of the string and meanwhile get runs. The former
detects all runs in time complexity O(n log n), and the latter in O(n), in which building the suffix array
of the string in linear time is required.
Then, let’s see if we can maintain some data structure to solve the problem in time complexity
O((n + q) log n). Let’s consider the relationship between a query (li , ri ) and a square-triple (L, R, k).
Obviously, if the triple has any contribution to the query, it must have k ≤ ri − li + 1. Furthermore, we
can split one triple (L, R, k) into two triples without upper bounds, (L, ∞, k) and (R − k + 2, ∞, k), so
that we can handle things easily. That is, for a new triple (u, ∞, k), its contribution (without regard to
its sign) to the query (li , ri ) is (max{ri − u − k + 2, 0} − max{li − u, 0}), which can be calculated by
maintaining two Fenwick trees and enumerating intervals in increasing order of length.

Problem Tutorial: “Sticks”


The number of ways to partition a set of size 12 into 4 sets of size 3 without regard to order is
12!
3!3!3!3!4! = 15400. We can find these partitions through a depth-first search, and the problem can be
done. By the way, we can sort sticks in increasing order of length so that we can use a < b < c and
a + b > c to check the existence of a triangle with side lengths a, b, c.
12!
One brute-force way is to enumerate three of 3!9! (= 220) possible sets and prune when it is necessary,
however, it can hardly pass all the tests.

Page 5 of 6
Day 8: Jingzhe Tang Contest 2, XIX Open Cup Onsite
37th Petrozavodsk Programming Camp, Summer 2019, Sunday, September 1, 2019

Problem Tutorial: “Pyramid”


The key insight is that, for every three equidistant vertices, we can find the only upright equilateral
triangle such that these three vertices are on the triangle’s sides. (We can do so for the only inverted
equilateral triangle.) Based on this fact, many solutions can be designed.
For example, you can use some data structure to maintain some information for l when r is fixed and
shifted from
√ low to high, or even you can use Mo’s algorithm to solve the problem in time complexity
O(max{r} T ).
If you are very good at math, you can find out the combinatorial form of the answer (a polynomial of
degree 4), and then solve the problem with or without interpolation.

Page 6 of 6

You might also like