Hi again!

If you notice typos or errors, please send a private message.

### 688A: Opponents

#### Solution

Let's find out for each row of the given matrix if it is completely consisting of *ones* or not. Make another array *canWin*, and set *canWin*_{i} equal to one if the *i*-th row consists at least one *zero*. Then the problem is to find the maximum subsegment of *canWin* array, consisting only ones. It can be solved by finding for each element of *canWin*, the closest zero to it from left. The complexity of this solution is *O*(*nd*), but the limits allow you to solve the problem in *O*(*nd*^{2}) by iterating over all possible subsegments and check if each one of them is full of *ones* or not.

**C++ code**

**Python code**

### 688B: Lovely Palindromes

#### Hint

Try to characterize even-length palindrome numbers.

#### Solution

For simplifications, in the following solution we define *lovely integer* as an even-length positive palindrome number.

An even-length positive integer is lovely if and only if the first half of its digits is equal to the reverse of the second half.

So if *a* and *b* are two different 2*k*-digit lovely numbers, then the first *k* digits of *a* and *b* differ in at least one position.

So *a* is smaller than *b* if and only if the first half of *a* is smaller than the the first half of *b*.

Another useful fact: The first half of a a lovely number can be any arbitrary positive integer.

Using the above facts, it's easy to find the first half of the *n*-th lovely number — it exactly equals to integer *n*. When we know the first half of a lovely number, we can concatenate it with its reverse to restore the lovely integer. To sum up, the answer can be made by concatenating *n* and it's reverse together.

The complexity of this solution is .

**C++ code**

**Python code**

#### Challenge

What if you were asked to find *n*-th positive palindrome number? (1 ≤ *n* ≤ 10^{18})

### 687A: NP-Hard Problem

#### Hint

Try to use all of the vertices. Then look at the two vertex covers together in the graph and see how it looks like.

#### Solution

Looking at the two vertex covers in the graph, you see there must be no edge *uv* that *u* and *v* are in the same vertex cover. So the two vertex covers form a bipartition of the graph, so the graph have to be bipartite. And being bipartite is also sufficient, you can use each part as a vertex cover. Bipartition can be found using your favorite graph traversing algorithm(BFS or DFS). Here is a tutorial for bipartition of undirected graphs.

The complexity is *O*(*n* + *m*).

**C++ code**

#### Challenge

In the same constraints, what if you were asked to give two disjoint edge covers from the given graph? (The edge covers must be edges-disjoint but they can have common vertex.)

### 687B: Remainders Game

#### Hint

Assume the answer of a test is *No*. There must exist a pair of integers *x*_{1} and *x*_{2} such that both of them have the same remainders after dividing by any *c*_{i}, but they differ in remainders after dividing by *k*. Find more facts about *x*_{1} and *x*_{2}!

#### Solution

Consider the *x*_{1} and *x*_{2} from the hint part. We have *x*_{1} - *x*_{2} ≡ 0 () for each 1 ≤ *i* ≤ *n*.

So:

We also have (). As a result:

We've found a necessary condition. And I have to tell you it's also sufficient!

Assume , we are going to prove there exists *x*_{1}, *x*_{2} such that *x*_{1} - *x*_{2} ≡ 0 () (for each 1 ≤ *i* ≤ *n*), and ().

A possible solution is *x*_{1} = *lcm*(*c*_{1}, *c*_{2}, ..., *c*_{n}) and *x*_{2} = 2 × *lcm*(*c*_{1}, *c*_{2}, ..., *c*_{n}), so the sufficiency is also proved.

So you have to check if *lcm*(*c*_{1}, *c*_{2}, ..., *c*_{n}) is divisible by *k*, which could be done using prime factorization of *k* and *c*_{i} values.

For each integer *x* smaller than *MAXC*, find it's greatest prime divisor *gpd*_{x} using sieve of Eratosthenes in .

Then using *gpd* array, you can write the value of each coin as *p*_{1}^{q1}*p*_{2}^{q2}...*p*_{m}^{qm} where *p*_{i} is a prime integer and 1 ≤ *q*_{i} holds. This could be done in by moving from *c*_{i} to and adding *gpd*_{ci} to the answer. And you can factorize *k* by the same way. Now for every prime *p* that , see if there exists any coin *i* that the power of *p* in the factorization of *c*_{i} is not smaller than the power of *p* in the factorization of *k*.

Complexity is .

**C++ code**

And a nice implementation by Reyna: 18789803

### 687C: The values you can make

#### Hint

Use dynamic programming.

#### Solution

Let *dp*_{i, j, k} be true if and only if there exists a subset of the first *i* coins with sum *j*, that has a subset with sum *k*. There are 3 cases to handle:

- The
*i*-th coin is not used in the subsets. - The
*i*-th coin is used in the subset to make*j*, but it's not used in the subset of this subset. - The
*i*-th coin is used in both subsets.

So *dp*_{i, j, k} is equal to *dp*_{i - 1, j, k} *OR* *dp*_{i - 1, j - ci, k} *OR* *dp*_{i - 1, j - ci, k - ci}.

The complexity is *O*(*nk*^{2}).

**C++ code**

#### Challenge

In the same constraints, output the numbers you can never not make! Formally, the values *x* such that for every subset of coins with the sum *k*, there exists a subset of this subset with the sum *x*.

### 687D: Dividing Kingdom II

Unfortunately, our mistake in setting the constrains for this problem made it possible to get Ac with *O*(*qm*) solution. We hope you accept our apology.

#### Hint

Consider the following algorithm to answer a single query: Sort the edges and add them one by one to the graph in decreasing order of their weights. The answer is weight of the first edge, which makes an odd cycle in the graph. Now show that there are only *O*(*n*) effective edges, which removing them may change the answer of the query. Use this idea to optimize your solution.

#### Solution

First, let’s solve a single query separately. Sort edges from interval [l, r] in decreasing order of weights. Using *dsu*, we can find longest prefix of these edges, which doesn’t contains odd cycle. (Graph will be bipartite after adding these edges.) The answer will be weight of the next edge. (We call this edge “bottleneck”).

Why it's correct? Because if the answer is *w*, then the we can divide the graph in a way that none of the edges in the same part have value greater than *w*. So the graph induced by the edges with value greater than *w* must be bipartite. And if this graph is bipartite, then we can divide the graph into two parts as the bipartition, so no edge with value greater than *w* will be in the same part, and the answer is at most *w*.

Let's have a look at this algorithm in more details. For each vertex, we keep two values in dsu: Its parent and if its part differs from its parent or not. We keep the second value equal to "parity of length of the path in original graph, between this node and its parent". We can divide the graph anytime into two parts, walking from one vertex to its parent and after reaching the root, see if the starting vertex must be in the same part as the root or not.

In every connected component, there must not exist any edge with endpoints in the same part.

After sorting the edges, there are 3 possible situations for an edge when we are adding it to the graph:

The endpoints of this edge are between two different components of the current graph. Now we must merge these two components, and update the part of the root of one of the components.

The endpoints of this edge are in the same component of the current graph, and they are in different parts of this component. There is nothing to do.

The endpoints of this edge are in the same component of the current graph, and they are also in the same part of this component. This edge is the "bottleneck" and we can't keep our graph bipartite after adding it, so we terminate the algorithm.

We call the edges of the first and the third type "valuable edges".

The key observation is: If we run above algorithm on the valuable edges, the answer remains the same.

Proof idea: The edges of the first type are spanning trees of connected components of the graph, and with a spanning tree of a bipartite graph, we can uniquely determine if two vertices are in the same part or not.

So if we can ignore all other edges and run our algorithm on these valuable edges, we have *O*(*n*) edges instead of original *O*(*n*^{2}) and the answer is the same.

We answer the queries using a segment tree on the edges. In each node of this tree, we run the above algorithm and memorize the valuable edges. By implementing carefully (described here), making the segment tree could be done in .

Now for each query [*l*, *r*], you can decompose [*l*, *r*] into segments in segment tree and each one has *O*(*n*) valuable edges. Running the naive algorithm on these edges lead to an solution for each query, which fits in time limit.

**C++ code**

### 687E: TOF

#### Hint: What the actual problem is

Looking at the code in the statement, you can see only the first edge in `neighbors`

of each node is important. So for each vertex with at least one outgoing edge, you have to choose one edge and ignore the others. After this the graph becomes in the shape of some cycles with possible branches, and some paths. The number of `dfs`

calls equals to 998 × ( sum of sizes of cycles ) + *n* + number of cycles.

#### Solution

The goal is to minimize the sum of cycle sizes. Or, to maximize the number of vertices which are not in any cycle. Name them *good vertices*.

If there exists a vertex

*v*without any outgoing edge, we can make all of the vertices that*v*is reachable from them good. Consider the dfs-tree from*v*in the reverse graph. You can choose the edge from this tree as the first edge in`neighbors[u]`

, in order to make all of these vertices good.Vertices which are not in the sink strongly connected components could become good too, by choosing the edges from a path starting from them and ending in a sink strongly connected component.

In a sink strongly connected component, there exists a path from every vertex to others. So we can make every vertex good except a single cycle, by choosing the edges in the paths from other nodes to this cycle and the cycle edges.

So, every vertices could become good except a single cycle in every sink strongly connected component. And the length of those cycles must be minimized, so we can choose the smallest cycle in every sink strongly connected component and make every other vertices good. Finding the smallest cycle in a graph with *n*-vertex and *m* edges could be done in *O*(*n*(*n* + *m*)) with running a *BFS* from every vertex, so finding the smallest cycle in every sink strongly connected component is *O*(*n*(*n* + *m*)) overall.

**C++ code**

#### Challenge

What if the target function is reversed? It means you have to choose a single outgoing edge from every node with at least one outgoing edge, and maximize the sum of cycle sizes in the chosen graph.

I thought about this one a lot but can't find any solution. Do you have any idea?