The full editorial video for Codeforces Round 1103 (Div. 3) is now available here — over 3 hours of problem discussions and solutions. Enjoy!
I'll update this blog later with submission links and video timestamps. For now, I'm exhausted after finishing this video editorial.
Problems are discussed in the following order: A → B → C → D → E → F1 → F2 → G.
2236A - Games on the Train
Hint 1What is the smallest possible final height that all towers can reach?
Hint 2Remember that every tower must receive a strictly positive increment.
Hint 3If the tallest tower has height mx, can the final height be mx?
Hint 4Try making every tower equal to mx+1.
Hint 5For a tower of height h[i], how much do we need to add?
Hint 6The answer is determined by the tower requiring the largest increment.
My submission — 378438460
2236B - Tatar TV Show
Hint 1Look at the leftmost character that is still equal to '1'.
Hint 2How many operations can affect position i?
Hint 3When processing position i, there is only one remaining operation that can flip it.
Hint 4If s[i]='1', you are forced to use the operation starting at i.
Hint 5Process positions from left to right greedily.
Hint 6After fixing the first n-k positions, what must be true about the remaining k positions?
My submission — 378439373
2236C - Omsk Programmers
Hint 1Would you ever want to perform +1 before a division?
Hint 2Compare: ($$$\frac{a+1}{x}$$$) vs ($$$\frac{a}{x} + 1$$$)
Hint 3Any addition done before division becomes less valuable afterwards.
Hint 4Assume all division operations happen first.
Hint 5How many times can a number be divided by x before reaching 0?
Hint 6Try recursively considering: - stop dividing and use additions - divide the larger number once more
Hint 7The recursion depth is only O(log(max(a,b))).
My submission — 378441912
2236D - Brand New Tatar TV Show
Hint 1Think in terms of winning and losing game states.
Hint 2Suppose the current chosen value is x. What future values can be picked?
Hint 3A state is winning if it can move to a losing state.
Hint 5Process values from largest to smallest.
Hint 6For value x, you need information about states in [x,x+k].
Hint 7Can you maintain winning/losing information using a range-query data structure?
My submission — 378444114
2236E - Friendly Gifts
Hint 1When is a segment a permutation of consecutive integers?
Hint 2All elements must be distinct.
Hint 3If mn and mx are minimum and maximum values, what relationship must hold with the segment length?
Hint 4A good segment satisfies: mx — mn + 1 = length.
Hint 5Imagine splitting the array into a left part and a right part.
Hint 6Store all good segments completely inside the left part.
Hint 7While extending a segment on the right, check whether the complementary value range already appeared on the left.
Hint 8Enumerating split positions leads to an O(n²) solution.
My submission — 378447263
2236F1 - Elections in Saransk (easy version)
Hint 1Let's first ignore the divisibility condition.
Suppose we have already chosen p₁, p₂, ..., pₙ.
What does
LCM(p₁,p₂,...,pₙ) = p₁ × p₂ × ... × pₙ
actually mean?
Hint 2Whenever you see LCM and Product together, try looking at one prime factor at a time.
Hint 3Fix a prime p.
Let vᵢ be the exponent of p inside pᵢ.
Hint 4For this prime:
Exponent in LCM = max(v₁,v₂,...,vₙ)
Exponent in Product = v₁ + v₂ + ... + vₙ
Hint 5Since K = 1 in F1, the condition becomes:
max(v₁,v₂,...,vₙ) = v₁ + v₂ + ... + vₙ
Hint 6Try a few examples.
✓ [3,0,0]
✓ [0,0,0]
✗ [2,1,0]
✗ [1,1,0]
Hint 7When can maximum of several non-negative numbers be equal to their sum?
Hint 8This happens iff at most one exponent is positive.
For every prime p, all copies of p must belong to at most one chosen number.
Hint 9Now use the condition: pᵢ divides aᵢ
Suppose exponent of prime p inside aᵢ is cᵢ.
Hint 10For this prime, we have two types of choices:
- Do not use p anywhere.
- Choose exactly one index i and place a positive exponent there.
Hint 11If index i is chosen, how many possible exponents can be assigned?
Remember that exponent inside pᵢ can be anything from 1 to cᵢ.
Hint 12Contribution of index i is therefore cᵢ.
Adding the option of not using the prime at all gives:
1 + c₁ + c₂ + ... + cₙ
Hint 13Different primes are completely independent.
Solve the counting problem separately for every prime factor.
Hint 14Factorize all aᵢ.
For every prime p, compute:
1 + (total exponent of p across all aᵢ)
My submission — 378448599
2236F2 - Elections in Saransk (hard version)
Hint 1What is the exponent of p in: LCM(p₁,p₂,...,pₙ)?
Hint 2What is the exponent of p in: p₁×p₂×...×pₙ?
Hint 3You should obtain: max(vᵢ)+vₖ = Σvᵢ
Hint 4Each aᵢ only gives an upper bound on vᵢ.
Hint 5DP over: (current maximum exponent, current sum of exponents).
My submission — 378449251
2236G - Criterion in Burlandia
Hint 1Let's first forget about the tree.
Suppose the array is just a normal sequence.
When does
XOR(segment) = SUM(segment)
hold?
Hint 2Think about how binary addition works.
SUM differs from XOR exactly because of carries.
Hint 3Try a few examples:
1 + 2 = 3 and 1 XOR 2 = 3 ✓
3 + 1 = 4 but 3 XOR 1 = 2 ✗
What changed?
Hint 4A carry appears whenever two numbers contain a common set bit.
Therefore:
XOR(segment) = SUM(segment)
iff no bit position appears in more than one element of the segment.
Hint 5Equivalently, if we maintain bitwise OR of the segment, then every bit can appear at most once.
Think of every bit as a resource that can only be used by one element.
Hint 6Now ask yourself:
How long can a valid segment actually become?
There are only about 20 bits available.
Hint 7Whenever a bit repeats, the segment immediately becomes invalid.
This means the number of distinct valid prefixes/suffixes starting from a node is actually very small.
Hint 8This bounded number of states is the key observation that makes the problem solvable.
A naive path can be very long, but the number of distinct valid XOR/SUM configurations remains small.
Hint 9Now bring the tree back.
Each query asks for the number of valid subsegments on the path between two vertices.
Hint 10Many path-query problems on trees can be solved by processing queries around a centroid.
Can we count all valid segments whose path passes through a particular centroid?
Hint 11Suppose a path passes through centroid c.
Such a path can be decomposed into:
(node in subtree A) → c → (node in subtree B)
Hint 12For every node, store all valid prefixes ending at the centroid.
Because valid prefixes are short (Hint 6), the amount of information per node remains small.
Hint 13While processing one subtree, keep information from previously processed subtrees.
Then count how many pairs of prefixes can be merged through the centroid while still having no overlapping bits.
Hint 14The merge condition is exactly the same as before:
No bit may appear twice.
So while combining two sides of the path, check whether their bitmasks are disjoint and also compatible with the centroid value.
Hint 15Using centroid decomposition:
- Every query is assigned to O(log n) centroids.
- Every stored prefix/suffix state is small because the number of usable bits is small.
This gives an overall complexity roughly
O((n + q) log n · maxBits).
My submission — 378429169
I think the DP is something like $$$O(n\log^3(v))$$$ to $$$O(n\log^4(v))$$$ with a small constant. Also F1 is quite hard to think but F2 is very natural :)
goddamn E tests. I don't even know how did my shitcode passed through all pretests
Of course, that matched the official solution... I came up with this algorithm yesterday.
Never uploading like that again...