Problem Tutorial: "Erase Nodes": 1 CNT (U, V)
Problem Tutorial: "Erase Nodes": 1 CNT (U, V)
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.
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;
• 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;
– 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.
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
We can calculate partial sums to optimize it into time complexity O(max2 {n}).
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
• 2d ≤ j − i + 1;
• sk = sk+d for k = i, i + 1, . . . , j − d;
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.
Page 5 of 6
Day 8: Jingzhe Tang Contest 2, XIX Open Cup Onsite
37th Petrozavodsk Programming Camp, Summer 2019, Sunday, September 1, 2019
Page 6 of 6