Skip to content

4 min read中文

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.

0.webp

Problems not solved independently

  • D (hard to implement)
  • E

A

Problem

A connected graph with nn vertices and kk 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. n105,k100n\le 10^5,k\le 100.

Solution

Note that kk is small, so run a shortest-path search for each kind of goods; dis[i][j] denotes the distance from ii to the nearest instance of goods jj. Then, for each vertex, find its ss nearest kinds of goods.

Takeaway

AC’d in the contest, and redid it independently later.

B

Problem

There is a sequence of length nn: 1,2,...,n1, 2, ... , n. Shuffling means randomly picking two numbers and swapping them. Petr\mathrm{Petr} performs 3n3n operations, Alex\mathrm{Alex} performs 7n+17n+1 operations. Given a permutation of 1n1\sim n, determine who shuffled it.

Solution

Note that 3n3n has the same parity as nn, while 7n+17n+1 has the opposite parity of nn. 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 O(n)\mathrm{O}(n) (by repeatedly swapping aaia_{a_i} and aia_i until ai=ia_i=i), and then the parity of the restoration count can be compared against nn.

Takeaway

AC’d in the contest, and redid it independently later.

C

Problem

A set of mm integers, each between 00 and 2n12 ^ n - 1. 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 NN, choose MM positive integers a1,a2,...aMa_1,a_2,...a_M (repeats allowed) such that i=1maiN\prod_{i=1}^{m} a_i \ge N, minimizing their sum.

Solution

Brute-forcing small cases shows that using 22s and 33s is the most economical. Studying xlog(Nx)x^{\log(\frac N x)} shows it is maximized at x=ex = e, and both 22 and 33 are close to ee. So use 33s to build up the sum. Let kk be the sum of the MM positive integers, then:

  • k%3=0k \% 3 = 0: use all 33s
  • k%3=1k \% 3 = 1: use two 22s, the rest 33s
  • k%3=2k \% 3 = 2: use one 22, the rest 33s

The value of k can be found by binary search, but binary search is too slow. Solving N=3k3N = 3 ^ {\frac k 3} gives klog3N3k\approx log_{3}^N \cdot 3. The length of NN (at most log10Nlog_{10}^N) can be used to estimate an approximate value of kk, then multiplied up brute-force from there. This one’s constant-factor hell, so FFT\mathrm{FFT} 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 (107\le 10^7), 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 109+710^9+7.

Solution

First, decompose the query (x,y,w)(x, y, w) into

ans(1,x)×ans(1,y)×gcd(w,vallca(x,y))ans(w,lcax,y)2ans(1,x) \times ans(1,y) \times \mathrm{gcd}(w,val_{lca(x,y)})\over ans(w,lca_{x,y})^2

Then it’s a tree difference: offline the queries onto the tree and resolve them with a single dfs\mathrm{dfs}. gcd\gcd is essentially taking the min\min of each prime’s exponent. Note there are only about 61056\cdot 10^5 primes below 10710^7. Give each prime a bucket, where vec[p][i]vec[p][i] denotes, within the part currently being processed, the number of vertices whose prime factorization has pp raised to the power ii.

Takeaway

Not solved independently.

F

Problem

Given n,kn, k, determine whether nn can be written as a sum of several divisors of kk. The problem stipulates this is not allowed when k=1k = 1.

Solution

There are at most 5050 distinct values of kk, so offline the queries and solve for each kk separately. First factorize kk into primes. If the number of distinct prime factors is greater than 22, solve it with shortest path on residues; if it equals 22, let the two primes be a,ba, b and just solve ax+by=nax + by = n; if it equals 1, just check whether n%pn \% p equals 00.

For the residue shortest-path: work in the residue system of the smallest prime factor, and add an edge of weight facxfac_x from ii to (i+facx)%fac1(i + fac_x) \% fac_1 for each other prime factor. Running shortest path this way, the result disidis_i is the minimum value reachable using several of p2pnp_2 \sim p_n that is i(modp1)\equiv i \pmod{p_1}. If disindis_i \le n, it can be topped up to exactly nn using p1p_1; otherwise there is no solution.

Takeaway

Solved independently.