1337xyz1337xyz/codeforces-editorial-full-2026-07-16 huggingface dataset:
Game of Stones
Contest: Divide by Zero 2017 and Codeforces Round 399 (Div. 1 + Div. 2, combined)
Difficulty rating: 2100
Tags: bitmasks, dp, games

Sam has been teaching Jon the Game of Stones to sharpen his mind and help him devise a strategy to fight the white walkers. The rules of this game are quite simple:

- The game starts with n piles of stones indexed from 1 to n. The i-th pile contains si stones.
- The players make their moves alternatively. A move is considered as removal of some number of stones from a pile. Removal of 0 stones does not count as a move.
- The player who is unable to make a move loses.

Now Jon believes that he is ready for battle, but Sam does not think so. To prove his argument, Sam suggested that they play a modified version of the game.

In this modified version, no move can be made more than once on a pile. For example, if 4 stones are removed from a pile, 4 stones cannot be removed from that pile again.

Sam sets up the game and makes the first move. Jon believes that Sam is just trying to prevent him from going to battle. Jon wants to know if he can win if both play optimally.

Input format:
First line consists of a single integer n (1 ≤ n ≤ 106) — the number of piles.

Each of next n lines contains an integer si (1 ≤ si ≤ 60) — the number of stones in i-th pile.

Output format:
Print a single line containing "YES" (without quotes) if Jon wins, otherwise print "NO" (without quotes)

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first case, Sam removes all the stones and Jon loses.

In second case, the following moves are possible by Sam: $$\{1,2\} \rightarrow \{0,2\}, \{1,2\} \rightarrow \{1,0\}, \{1,2\} \rightarrow \{1,1\}$$

In each of these cases, last move can be made by Jon to win the game as follows: $${ \{ 0, 2 \} } \rightarrow { \{ 0, 0 \} }, { \{ 1, 0 \} } \rightarrow { \{ 0, 0 \} }, { \{ 1, 1 \} } \rightarrow { \{ 0, 1 \} }$$

Example input:
1
5

Example output:
NO

Example input:
2
1
2

Example output:
YES

Editorial:
Barrels and boxes
Contest: Divide by Zero 2017 and Codeforces Round 399 (Div. 1 + Div. 2, combined)
Difficulty rating: 2300
Tags: brute force, combinatorics, math, number theory, probabilities

Tarly has two different type of items, food boxes and wine barrels. There are f food boxes and w wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no two stacks of wine barrels are together.

The height of a stack is defined as the number of items in the stack. Two stacks are considered different if either their heights are different or one of them contains food and other contains wine.

Jon Snow doesn't like an arrangement if any stack of wine barrels has height less than or equal to h. What is the probability that Jon Snow will like the arrangement if all arrangement are equiprobably?

Two arrangement of stacks are considered different if exists such i, that i-th stack of one arrangement is different from the i-th stack of the other arrangement.

Input format:
The first line of input contains three integers f, w, h (0 ≤ f, w, h ≤ 105) — number of food boxes, number of wine barrels and h is as described above. It is guaranteed that he has at least one food box or at least one wine barrel.

Output format:
Output the probability that Jon Snow will like the arrangement. The probability is of the form $$\frac{p}{q}$$, then you need to output a single integer p·q - 1 mod (109 + 7).

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example f  =  1, w = 1 and h = 1, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them.

In the second example f = 1, w = 2 and h = 1, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is $$\frac{2}{3}$$.

Example input:
1 1 1

Example output:
0

Example input:
1 2 1

Example output:
666666672

Editorial:
The Winds of Winter
Contest: Divide by Zero 2017 and Codeforces Round 399 (Div. 1 + Div. 2, combined)
Difficulty rating: 3300
Tags: binary search, data structures

Given a rooted tree with n nodes. The Night King removes exactly one node from the tree and all the edges associated with it. Doing this splits the tree and forms a forest. The node which is removed is not a part of the forest.

The root of a tree in the forest is the node in that tree which does not have a parent. We define the strength of the forest as the size of largest tree in forest.

Jon Snow wants to minimize the strength of the forest. To do this he can perform the following operation at most once.

He removes the edge between a node and its parent and inserts a new edge between this node and any other node in forest such that the total number of trees in forest remain same.

For each node v you need to find the minimum value of strength of the forest formed when node v is removed.

Input format:
The first line of the input contains an integer n (1  ≤  n  ≤  105) — the number of vertices in the tree. Each of the next n lines contains a pair of vertex indices ui and vi (1  ≤  ui,  vi  ≤  n) where ui is the parent of vi. If ui = 0 then vi is the root.

Output format:
Print n line each containing a single integer. The i-th of them should be equal to minimum value of strength of forest formed when i-th node is removed and Jon Snow performs the operation described above at most once.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
The tree for first test case is depicted below.  When you remove the first node, the tree splits to form the following forest. The strength of this forest is 4.  Jon Snow now changes the parent of vertex 10 from 5 to 3. The strength of forest now becomes 3.

Example input:
10
0 1
1 2
1 3
1 4
2 5
2 6
3 7
4 8
4 9
5 10

Example output:
3
4
5
5
5
9
9
9
9
9

Example input:
2
2 1
0 2

Example output:
1
1

Editorial:
Generate plus state or minus state
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1100
Tags: *special

You are given a qubit in state $$| 0 \rangle$$ and an integer sign.

Your task is to convert the given qubit to state $${ \left| + \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 \right\rangle } + { \left| 1 \right\rangle } )$$ if sign = 1 or $${ \left| - \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 \right\rangle } - { \left| 1 \right\rangle } )$$ if sign =  - 1.

Input format:
You have to implement an operation which takes a qubit and an integer as an input and has no output. The "output" of your solution is the state in which it left the input qubit.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Generate Bell state
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1400
Tags: *special

You are given two qubits in state $${\left|00\right\rangle}$$ and an integer index. Your task is to create one of the Bell states on them according to the index:

- $${ \left| B_{ 0 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 0 \right\rangle } + { \left| 1 1 \right\rangle } )$$
- $${ \left| B _ { 1 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 0 \right\rangle } - { \left| 1 1 \right\rangle } )$$
- $${ \left| B_{ 2 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 1 \right\rangle } + { \left| 1 0 \right\rangle } )$$
- $${ \left| B_{ 3 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 1 \right\rangle } - { \left| 1 0 \right\rangle } )$$

Input format:
You have to implement an operation which takes an array of 2 qubits and an integer as an input and has no output. The "output" of your solution is the state in which it left the input qubits.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Generate GHZ state
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1400
Tags: *special

You are given N qubits (1 ≤ N ≤ 8) in zero state $${\left|0\dots0\right\rangle}$$. Your task is to create Greenberger–Horne–Zeilinger (GHZ) state on them:

$${ \left| GHZ \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 \dots 0 \right\rangle } + { \left| 1 \dots 1 \right\rangle } )$$

Note that for N = 1 and N = 2 GHZ state becomes states $$|+\rangle$$ and $$| B_{0} \rangle$$ from the previous tasks, respectively.

Input format:
You have to implement an operation which takes an array of N qubits as an input and has no output. The "output" of your solution is the state in which it left the input qubits.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Distinguish plus state and minus state
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1400
Tags: *special

You are given a qubit which is guaranteed to be either in $${ \left| + \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 \right\rangle } + { \left| 1 \right\rangle } )$$ or in $${ \left| - \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 \right\rangle } - { \left| 1 \right\rangle } )$$ state.

Your task is to perform necessary operations and measurements to figure out which state it was and to return 1 if it was a $$|+\rangle$$ state or -1 if it was $$\rightarrow$$ state. The state of the qubit after the operations does not matter.

Input format:
You have to implement an operation which takes a qubit as an input and returns an integer.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Distinguish Bell states
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1600
Tags: *special

You are given two qubits which are guaranteed to be in one of the Bell states:

- $${ \left| B_{ 0 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 0 \right\rangle } + { \left| 1 1 \right\rangle } )$$
- $${ \left| B _ { 1 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 0 \right\rangle } - { \left| 1 1 \right\rangle } )$$
- $${ \left| B_{ 2 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 1 \right\rangle } + { \left| 1 0 \right\rangle } )$$
- $${ \left| B_{ 3 } \right\rangle } = { \frac { 1 } { \sqrt { 2 } } } ( { \left| 0 1 \right\rangle } - { \left| 1 0 \right\rangle } )$$

Your task is to perform necessary operations and measurements to figure out which state it was and to return the index of that state (0 for $$| B_{0} \rangle$$, 1 for $$\left|B_{1}\right\rangle$$ etc.). The state of the qubits after the operations does not matter.

Input format:
You have to implement an operation which takes an array of two qubits as an input and returns an integer.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Distinguish multi-qubit basis states
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1300
Tags: *special

You are given N qubits which are guaranteed to be in one of two basis states on N qubits. You are also given two bitstrings bits0 and bits1 which describe these basis states.

Your task is to perform necessary operations and measurements to figure out which state it was and to return 0 if it was the state described with bits0 or 1 if it was the state described with bits1. The state of the qubits after the operations does not matter.

Input format:
You have to implement an operation which takes the following inputs:

- an array of qubits qs,
- two arrays of boolean values bits0 and bits1, representing the basis states in which the qubits can be. These arrays will have the same length as the array of qubits. bits0 and bits1 will differ in at least one position.

An array of boolean values represents a basis state as follows: the i-th element of the array is true if the i-th qubit is in state $$\left|1\right\rangle$$, and false if it is in state $$| 0 \rangle$$. For example, array [true; false] describes 2-qubit state $${\left|10\right\rangle}$$.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Oracle for f(x) = k-th element of x
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1400
Tags: *special

Implement a quantum oracle on N qubits which implements a function f(x) = xk, i.e. the value of the function is the value of the k-th qubit.

For an explanation on how the quantum oracles work, see the tutorial blog post.

Input format:
You have to implement an operation which takes the following inputs:

- an array of qubits x (input register),
- a qubit y (output qubit),
- 0-based index of the qubit from input register K ($$0 \leq K < \text{Length}(x)$$).

The operation doesn't have an output; the "output" of your solution is the state in which it left the qubits.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Oracle for f(x) = parity of the number of 1s in x
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1200
Tags: *special

Implement a quantum oracle on N qubits which implements the following function: $$f(x) = \sum_{i} x_i \mod 2$$, i.e., the value of the function is 1 if x has odd number of 1s, and 0 otherwise.

Input format:
You have to implement an operation which takes the following inputs:

- an array of qubits x (input register),
- a qubit y (output qubit).

The operation doesn't have an output; the "output" of your solution is the state in which it left the qubits.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
Deutsch-Jozsa algorithm
Contest: Microsoft Q# Coding Contest - Summer 2018 - Warmup
Difficulty rating: 1700
Tags: *special

You are given a quantum oracle - an operation on N + 1 qubits which implements a function $$f: \{0, 1\}^N \to \{0, 1\}$$. You are guaranteed that the function f implemented by the oracle is either constant (returns 0 on all inputs or 1 on all inputs) or balanced (returns 0 on exactly one half of the input domain and 1 on the other half).

There are only two possible constant functions: f(x) = 0 and f(x) = 1. The functions implemented by oracles in the two previous problems (f(x) = xk and $$f(x) = \sum_{i} x_i \mod 2$$) are examples of balanced functions.

Your task is to figure out whether the function given by the oracle is constant. Your code is allowed to call the given oracle only once.

Input format:
You have to implement an operation which takes the following inputs:

- an integer N - the number of qubits in the oracle input,
- an oracle Uf, implemented as an operation with signature ((Qubit[], Qubit) => ()), i.e., an operation which takes as input an array of qubits and an output qubit and has no output.

The return of your operation is a Boolean value: true if the oracle implements a constant function and false otherwise.

Your code should have the following signature:

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Editorial:
The New Year: Meeting Friends
Contest: Codeforces Round 375 (Div. 2)
Difficulty rating: 800
Tags: implementation, math, sortings

There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?

It's guaranteed that the optimal answer is always integer.

Input format:
The first line of the input contains three distinct integers x1, x2 and x3 (1 ≤ x1, x2, x3 ≤ 100) — the coordinates of the houses of the first, the second and the third friends respectively.

Output format:
Print one integer — the minimum total distance the friends need to travel in order to meet together.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.

Example input:
7 1 4

Example output:
6

Example input:
30 20 10

Example output:
20

Editorial:
To solve this problem you need to understand that friends must meet in the middle point of the given points, so friends who live in the leftmost and in the rightmost points must go to the middle point. Because of that the answer equals to max(x1, x2, x3) - min(x1, x2, x3).
Polycarp at the Radio
Contest: Codeforces Round 375 (Div. 2)
Difficulty rating: 1600
Tags: greedy

Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be represented as a sequence a1, a2, ..., an, where ai is a band, which performs the i-th song. Polycarp likes bands with the numbers from 1 to m, but he doesn't really like others.

We define as bj the number of songs the group j is going to perform tomorrow. Polycarp wants to change the playlist in such a way that the minimum among the numbers b1, b2, ..., bm will be as large as possible.

Find this maximum possible value of the minimum among the bj (1 ≤ j ≤ m), and the minimum number of changes in the playlist Polycarp needs to make to achieve it. One change in the playlist is a replacement of the performer of the i-th song with any other group.

Input format:
The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 2000).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109), where ai is the performer of the i-th song.

Output format:
In the first line print two integers: the maximum possible value of the minimum among the bj (1 ≤ j ≤ m), where bj is the number of songs in the changed playlist performed by the j-th band, and the minimum number of changes in the playlist Polycarp needs to make.

In the second line print the changed playlist.

If there are multiple answers, print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first sample, after Polycarp's changes the first band performs two songs (b1 = 2), and the second band also performs two songs (b2 = 2). Thus, the minimum of these values equals to 2. It is impossible to achieve a higher minimum value by any changes in the playlist.

In the second sample, after Polycarp's changes the first band performs two songs (b1 = 2), the second band performs three songs (b2 = 3), and the third band also performs two songs (b3 = 2). Thus, the best minimum value is 2.

Example input:
4 2
1 2 3 2

Example output:
2 1
1 2 1 2

Example input:
7 3
1 3 2 2 2 2 1

Example output:
2 1
1 3 3 2 2 2 1

Editorial:
It is easy to understand that we always can get the maximum value of the minimum of the values bj which equals to n / m. So, we need to make vector can which will store positions of the given array in which we will change values. At first it is all positions of the given array, in which the values are more than m. Then we need to store for all i from 1 to m in vectors posi the positions of the given array, in which the bands equal to i. Then for every vector, which size is more than n / m we need to add tje first sz(posi) - (n / m) elements in the vector can, where sz(posi) is the size of the vector posi.
After that we need to iterate through the numbers of bands from 1 to m. If sz(posi) is less than n / m we need to take regular (n / m) - sz(posi) positions from the vector can and change the values in this positions to i. In the same time we need to count the number of changes in the playlist.
Lakes in Berland
Contest: Codeforces Round 375 (Div. 2)
Difficulty rating: 1600
Tags: dfs and similar, dsu, graphs, greedy, implementation

The map of Berland is a rectangle of the size n × m, which consists of cells of size 1 × 1. Each cell is either land or water. The map is surrounded by the ocean.

Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.

You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.

Input format:
The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 0 ≤ k ≤ 50) — the sizes of the map and the number of lakes which should be left on the map.

The next n lines contain m characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land).

It is guaranteed that the map contain at least k lakes.

Output format:
In the first line print the minimum number of cells which should be transformed from water to land.

In the next n lines print m symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them.

It is guaranteed that the answer exists on the given data.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example there are only two lakes — the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.

Example input:
5 4 1
****
*..*
****
**.*
..**

Example output:
1
****
*..*
****
****
..**

Example input:
3 3 0
***
*.*
***

Example output:
1
***
***
***

Editorial:
To solve this problem we need to find all connected components consisting of dots, which do not have common border with ocean. For that we need to implement depth search which returns vector of the points from which the current connected component consists.
Then we need to sort all connected components in order of increasing of their sizes and changes all dots on asterisks in components, beginning from the component with minimum size, until the number of left components does not equals to k.