Editorial | CF986 / Codeforces Round 485 (Div. 1)
Editorial for a virtual-contest run of Codeforces Round 485 (Div. 1), covering the approach and solution for problems A through F.
Machine-translated from the Chinese original.

Problems not solved independently
- D (hard to implement)
- E
A
Problem
A connected graph with vertices and kinds of goods, one kind at each vertex. The cost of shipping goods is the shortest-path distance between the two vertices. For each vertex, find the minimum cost to gather s kinds of goods there. .
Solution
Note that is small, so run a shortest-path search for each kind of goods; dis[i][j] denotes the distance from to the nearest instance of goods . Then, for each vertex, find its nearest kinds of goods.
Takeaway
AC’d in the contest, and redid it independently later.
B
Problem
There is a sequence of length : . Shuffling means randomly picking two numbers and swapping them. performs operations, performs operations. Given a permutation of , determine who shuffled it.
Solution
Note that has the same parity as , while has the opposite parity of . Also, the number of operations used to restore the sequence has the same parity as the number of operations used to shuffle it. So the sequence can be restored in (by repeatedly swapping and until ), and then the parity of the restoration count can be compared against .
Takeaway
AC’d in the contest, and redid it independently later.
C
Problem
A set of integers, each between and . Build an undirected graph with each integer as a vertex; two integers x, y are joined by an edge whenever x & y = 0. Count the number of connected components.
Solution
Let z be the bitwise complement of x; then x & y = 0 means y & z = z, i.e. the set bits of y are a subset of the set bits of z. There are only 2 ^ 22 states in total, so a brute-force search plus marking for every unmarked x is well within the time limit.
Takeaway
Solved it independently when I redid it.
D
Problem
Given , choose positive integers (repeats allowed) such that , minimizing their sum.
Solution
Brute-forcing small cases shows that using s and s is the most economical. Studying shows it is maximized at , and both and are close to . So use s to build up the sum. Let be the sum of the positive integers, then:
- : use all s
- : use two s, the rest s
- : use one , the rest s
The value of k can be found by binary search, but binary search is too slow. Solving gives . The length of (at most ) can be used to estimate an approximate value of , then multiplied up brute-force from there. This one’s constant-factor hell, so is clearly needed, probably with base-compressed bignums too.
Takeaway
I still remember the approach, but it’s fairly hard to implement.
E
Problem
Given a tree where each vertex has a weight (), and m queries; each query gives x, y, w and asks for the product, over the vertices on the path x -> y, of gcd(vertex weight, w), modulo .
Solution
First, decompose the query into
Then it’s a tree difference: offline the queries onto the tree and resolve them with a single . is essentially taking the of each prime’s exponent. Note there are only about primes below . Give each prime a bucket, where denotes, within the part currently being processed, the number of vertices whose prime factorization has raised to the power .
Takeaway
Not solved independently.
F
Problem
Given , determine whether can be written as a sum of several divisors of . The problem stipulates this is not allowed when .
Solution
There are at most distinct values of , so offline the queries and solve for each separately. First factorize into primes. If the number of distinct prime factors is greater than , solve it with shortest path on residues; if it equals , let the two primes be and just solve ; if it equals 1, just check whether equals .
For the residue shortest-path: work in the residue system of the smallest prime factor, and add an edge of weight from to for each other prime factor. Running shortest path this way, the result is the minimum value reachable using several of that is . If , it can be topped up to exactly using ; otherwise there is no solution.
Takeaway
Solved independently.