CF-2210

3/29/2026(.md)

  • contest

Thoughts

I have begun training for WF, and want my practice to take a more structured form. Hence this recapitulation, and any others that may follow.

I wasn't as fast as I could have been today. The WA on D was also pointless. It could have been easily avoided by looking at my code for 2 seconds before submitting.

Solutions

A

Reversing the identity sequence clearly works.

B

Let's say that the optimal sequence of operations results in us stopping at position i.

Then it's optimal to have sat at all chairs j behind us such that either pj≤j or i≤pj. The first category doesn't prevent us from going forward at all (as they mark chairs behind or at our current position), and the second category won't prevent us from reaching i.

The optimal score for every stopping point can therefore be computed by iterating over i in increasing order, and using either a std::set or a range query DS.

C1

Notice that the condition:

gcd⁡(al,al+1,…,ar)=gcd⁡(al′,al+1′,…,ar′) for all 1≤l<r≤n

is equivalent to

gcd⁡(ai,ai+1)=gcd⁡(ai′,ai+1′) for all 1≤i<n

Now, for a particular 1<i<n, its value in the second array, ai′ only matters to two conditions:

  1. gcd⁡(ai,ai−1)=gcd⁡(ai′,ai−1′)
  2. gcd⁡(ai,ai+1)=gcd⁡(ai′,ai+1′)

We notice that ai′ must therefore be divisible by ci=lcm⁡(gcd⁡(ai,ai−1),gcd⁡(ai,ai+1)).

ci is also the smallest value for ai′ that satisfies both these conditions (at least from i's side).

If ci<ai=bi, we simply set ai′=ci, and this increases our score by 1.

Otherwise, we have ci=ai. In this case:

  1. We cannot set ai′ to anything less than ci, as we have shown ci to be the smallest valid value of ci.
  2. We cannot set ai′ to any other multiple of ci as ci=ai=bi.

Therefore, we are forced to set ai′=ai (not make a move).

C2

The harder version decouples ai and bi.

This only affects our previous analysis at one place: previously, when we had ci=ai, in point 2, we argued that we cannot set ai′ to some non-trivial multiple of ci, as we had ci=ai=bi.

But now, we can have ai<bi, which will allow us to set ai′=mi⋅ci (where mi>1).

Lemma 1:

There exists an optimal solution wherein the chosen mi for all i at which we set ai′=mi⋅ci (mi>1) is prime.

Proof: easy enough to show, but left as an exercise to the reader.

Lemma 2:

There exists an optimal solution wherein the set of used mi is a subset of the first P=25 primes.

Proof: left as an exercise to the reader (hint: mi is only restricted by mi−1,ai−1,ci,mi+1,ai+1, and any number up to 1018 cannot have too many distinct prime factors).

We can therefore do a dynamic programming with the following state:

  • f(i) = maximum number of moves we can make in the prefix i, if we make no move at position i.
  • g(i,x) = maximum number of moves we can make in the prefix i, if we set ai′=ci⋅px (where px is the x-th smallest prime).

Computing all of these states takes O(n⋅P2⋅log⁡109) time, which is fast enough.

D

Firstly, let f(s) be the maximum x≤n/2 such that for all i≤x, si gets matched to sn−i+1 by RBS rules.

Lemma 1:

f(s)=f(t) is a necessary requirement for s and t to be interconvertible, as f(s)=f(s′) after any operation that converts s to s′

Proof: Exercise for the reader (hint: this outer section can never be modified by any operation).

If f(s)≠f(t), we're done. Otherwise, we get rid of the first and last f(s) characters from both s and t.

Now, consider the RBS trees T(s) and T(t) built from s and t (note that since we removed the first/last f(s) characters, these trees will either be empty, or the root will have > 1 child).

The equivalent problem on these trees is the following:

You can perform any number of the following operations on either tree:

Select nodes u and v such that neither is an ancestor of the either, and attach some "adjacent" children of v (in the ordered child sequence) to the parent of u, and attach v to u.

Can the two trees be made isomorphic?

Now, notice the following invariant: The number of leaf nodes remains the same after every operation.

While obviously necessary, it turns out that having an equal number of leaf nodes is sufficient for two trees to be interconvertible.

Proof:

Let's say a tree has l leaves.

Regardless of the initial structure of the tree, we can always convert it to the following equivalence class representative:

A tree with l−1 leaves hanging from the root, and a chain containing the remaining nodes.