contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
117
B
117B
B. Very Interesting Game
1,800
brute force; number theory
In a very ancient country the following game was popular. Two people play the game. Initially first player writes a string s1, consisting of exactly nine digits and representing a number that does not exceed a. After that second player looks at s1 and writes a string s2, consisting of exactly nine digits and representi...
The first line contains three integers a, b, mod (0 ≤ a, b ≤ 109, 1 ≤ mod ≤ 107).
If the first player wins, print ""1"" and the lexicographically minimum string s1 he has to write to win. If the second player wins, print the single number ""2"".
The lexical comparison of strings is performed by the < operator in modern programming languages. String x is lexicographically less than string y if exists such i (1 ≤ i ≤ 9), that xi < yi, and for any j (1 ≤ j < i) xj = yj. These strings always have length 9.
Input: 1 10 7 | Output: 2
Medium
2
858
81
163
1
1,007
A
1007A
A. Reorder the Array
1,300
combinatorics; data structures; math; sortings; two pointers
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.For instance, if we are given an array \([10, 20, 30, 40]\), we can ...
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) — the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) — the elements of the array.
Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.
In the first sample, one of the best permutations is \([1, 5, 5, 3, 10, 1, 1]\). On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.In the second sample, there is no way to increase any element with a permutation, so the answer is 0.
Input: 710 1 1 1 5 5 3 | Output: 4
Easy
5
816
219
172
10
1,176
A
1176A
A. Divide it!
800
brute force; greedy; implementation
You are given an integer \(n\).You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace \(n\) with \(\frac{n}{2}\) if \(n\) is divisible by \(2\); Replace \(n\) with \(\frac{2n}{3}\) if \(n\) is divisible by \(3\); Replace \(n\) with \(\frac{4n}{5}\) if \(n...
The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) — the number of queries.The next \(q\) lines contain the queries. For each query you are given the integer number \(n\) (\(1 \le n \le 10^{18}\)).
Print the answer for each query on a new line. If it is impossible to obtain \(1\) from \(n\), print -1. Otherwise, print the minimum number of moves required to do it.
Input: 7 1 10 25 30 14 27 1000000000000000000 | Output: 0 4 6 6 -1 6 72
Beginner
3
669
224
168
11
1,970
E1
1970E1
E1. Trails (Easy)
1,800
dp
Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are \(m\) cabins, numbered 1 to \(m\). Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin \(i\) is connected with \(s_i\) short trails and \(l_i\) long tra...
The first line contains the integers \(m\) and \(n\).The second line contains \(m\) integers, \(s_1, \dots, s_m\), where \(s_i\) is the number of short trails between cabin \(i\) and Lake Geneva.The third and last line contains \(m\) integers, \(l_1, \dots, l_m\), where \(l_i\) is the number of long trails between cabi...
The number of possible combinations of trails, modulo \(10^9 + 7\).
Input: 3 21 0 10 1 1 | Output: 18
Medium
1
754
448
67
19
556
A
556A
A. Case of the Zeros and Ones
900
greedy
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and th...
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.The second line contains the string of length n consisting only from zeros and ones.
Output the minimum length of the string that may remain after applying the described operations several times.
In the first sample test it is possible to change the string like the following: .In the second sample test it is possible to change the string like the following: .In the third sample test it is possible to change the string like the following: .
Input: 41100 | Output: 0
Beginner
1
638
195
110
5
1,002
A3
1002A3
A3. Generate superposition of two basis states
1,500
*special
You are given N qubits (1 ≤ N ≤ 8) in zero state . You are also given two bitstrings bits0 and bits1 which describe two different basis states on N qubits and .Your task is to generate a state which is an equal superposition of the given basis states:You have to implement an operation which takes the following inputs: ...
Medium
1
866
0
0
10
172
B
172B
B. Pseudorandom Sequence Period
1,200
*special; implementation; number theory
Polycarpus has recently got interested in sequences of pseudorandom numbers. He learned that many programming languages generate such sequences in a similar way: (for i ≥ 1). Here a, b, m are constants, fixed for the given realization of the pseudorandom numbers generator, r0 is the so-called randseed (this value can b...
The single line of the input contains four integers a, b, m and r0 (1 ≤ m ≤ 105, 0 ≤ a, b ≤ 1000, 0 ≤ r0 < m), separated by single spaces.
Print a single integer — the period of the sequence.
The first sample is described above. In the second sample the sequence is (starting from the first element): 0, 3, 4, 1, 0, 3, 4, 1, 0, ...In the third sample the sequence is (starting from the first element): 33, 24, 78, 78, 78, 78, ...
Input: 2 6 12 11 | Output: 2
Easy
3
1,036
138
52
1
1,726
A
1726A
A. Mainak and Array
900
greedy; math
Mainak has an array \(a_1, a_2, \ldots, a_n\) of \(n\) positive integers. He will do the following operation to this array exactly once: Pick a subsegment of this array and cyclically rotate it by any amount. Formally, he can do the following exactly once: Pick two integers \(l\) and \(r\), such that \(1 \le l \le r \l...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 50\)) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line of each test case contains \(n\) integers ...
For each test case, output a single integer — the maximum value of \((a_n - a_1)\) that Mainak can obtain by doing the operation exactly once.
In the first test case, we can rotate the subarray from index \(3\) to index \(6\) by an amount of \(2\) (i.e. choose \(l = 3\), \(r = 6\) and \(k = 2\)) to get the optimal array: $$$\([1, 3, \underline{9, 11, 5, 7}] \longrightarrow [1, 3, \underline{5, 7, 9, 11}]\)\( So the answer is \)a_n - a_1 = 11 - 1 = 10\(. In th...
Input: 561 3 9 11 5 712039 99 99942 1 8 132 1 5 | Output: 10 0 990 7 4
Beginner
2
644
454
142
17
25
A
25A
A. IQ test
1,300
brute force
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob — to check his answers, he needs a program that among the given n numbers finds one that is di...
The first line contains integer n (3 ≤ n ≤ 100) — amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness.
Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order.
Input: 52 4 7 8 10 | Output: 3
Easy
1
340
248
112
0
172
E
172E
E. BHTML+BCSS
2,200
*special; dfs and similar; expression parsing
This problem is about imaginary languages BHTML and BCSS, which slightly resemble HTML and CSS. Read the problem statement carefully as the resemblance is rather slight and the problem uses very simplified analogs.You are given a BHTML document that resembles HTML but is much simpler. It is recorded as a sequence of op...
The first line of the input contains a BHTML-document. The document has length from 4 to 106 characters. The document has a correct structure, doesn't contain spaces or any other unnecessary characters. Tagnames consist of lowercase Latin letters, their lengths are from 1 to 10 characters.The second line contains an in...
Print m lines, the j-th line should contain the number of elements of the document that correspond to the j-th BCSS-rule. If there are no such elements at all, print on the line 0.
Input: <a><b><b></b></b></a><a><b></b><b><v/></b></a><b></b>4aa b ba bb a | Output: 2140
Hard
3
2,872
750
180
1
2,104
E
2104E
E. Unpleasant Strings
1,700
binary search; dp; greedy; strings
Let's call a letter allowed if it is a lowercase letter and is one of the first \(k\) letters of the Latin alphabet.You are given a string \(s\) of length \(n\), consisting only of allowed letters.Let's call a string \(t\) pleasant if \(t\) is a subsequence of \(s\).You are given \(q\) strings \(t_1, t_2, \dots, t_q\)....
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 10^6\); \(1 \le k \le 26\)) — the length of the string \(s\) and the number of allowed letters.The second line contains the string \(s\), consisting of \(n\) lowercase Latin letters. Each character of the string is one of the first \(k\) letters of the...
For each query, output one integer — the minimum number of allowed letters that need to be appended to the string on the right so that it stops being pleasant.
In the first example: The string cc is already unpleasant, so nothing needs to be appended to it; bcb is pleasant, so at least one letter needs to be appended to the right: bcba will not work, but bcbb and bcbc are unpleasant. To b, at least two letters need to be appended, since ba, bb, and bc are pleasant. For exampl...
Input: 7 3abacaba3ccbcbb | Output: 0 1 2
Medium
4
683
662
159
21
1,994
G
1994G
G. Minecraft
2,600
bitmasks; brute force; dp; graphs; math
After winning another Bed Wars game, Masha and Olya wanted to relax and decided to play a new game. Masha gives Olya an array \(a\) of length \(n\) and a number \(s\). Now Olya's task is to find a non-negative number \(x\) such that \(\displaystyle\sum_{i=1}^{n} a_i \oplus x = s\). But she is very tired after a tight r...
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n, k, n \cdot k \le 2 \cdot 10^6\)) — the length of t...
For each test case, output a string of length \(k\) on a separate line, consisting of zeros or ones — the binary representation of any suitable number \(x\) (\(x \ge 0\)), starting from the most significant bits, or \(-1\) if such \(x\) does not exist.
In the first test case, \(s = 11, a = [14, 6, 12, 15]\), if \(x = 14\), then \(\displaystyle\sum_{i=1}^{n} a_i \oplus x = (14 \oplus 14) + (6 \oplus 14) + (12 \oplus 14) + (15 \oplus 14) = 0 + 8 + 2 + 1 = 11 = s\).In the second test case, \(s = 41, a = [191, 158]\), if \(x = 154\), then \(\displaystyle\sum_{i=1}^{n} a_...
Input: 44 501011011100011001100011112 80010100110111111100111105 40101001000000000001000116 500011101101100101010111001001110000 | Output: 01110 10011010 0010 -1
Expert
5
504
890
252
19
124
A
124A
A. The number of positions
1,000
math
Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing behind him. Find the number of different positions Petr can occupy.
The only line contains three integers n, a and b (0 ≤ a, b < n ≤ 100).
Print the single number — the number of the sought positions.
The possible positions in the first sample are: 2 and 3 (if we number the positions starting with 1).In the second sample they are 3, 4 and 5.
Input: 3 1 1 | Output: 2
Beginner
1
264
70
61
1
283
D
283D
D. Cows and Cool Sequences
2,400
dp; math; number theory
Bessie and the cows have recently been playing with ""cool"" sequences and are trying to construct some. Unfortunately they are bad at arithmetic, so they need your help!A pair (x, y) of positive integers is ""cool"" if x can be expressed as the sum of y consecutive integers (not necessarily positive). A sequence (a1, ...
The first line contains a single integer, n (2 ≤ n ≤ 5000). The next line contains n space-separated integers, a1, a2, ..., an (1 ≤ ai ≤ 1015).Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
A single integer, the minimum number of ai that must be changed to make the sequence cool.
In the first sample, the sequence is already cool, so we don't need to change any elements. In the second sample, we can change a2 to 5 and a3 to 10 to make (20, 5, 10, 4) which is cool. This changes 2 elements.
Input: 36 4 1 | Output: 0
Expert
3
680
289
90
2
764
A
764A
A. Taymyr is calling you
800
brute force; implementation; math
Comrade Dujikov is busy choosing artists for Timofey's birthday and is recieving calls from Taymyr from Ilia-alpinist.Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, i.e. in minutes m, 2m, 3m and so on. The day is z minutes long, i.e. the day consis...
The only string contains three integers — n, m and z (1 ≤ n, m, z ≤ 104).
Print single integer — the minimum number of artists that should be killed so that there are no artists in the room when Ilia calls.
Taymyr is a place in the north of Russia.In the first test the artists come each minute, as well as the calls, so we need to kill all of them.In the second test we need to kill artists which come on the second and the fourth minutes.In the third test — only the artist which comes on the sixth minute.
Input: 1 1 10 | Output: 10
Beginner
3
511
73
132
7
786
D
786D
D. Rap God
3,400
data structures; dfs and similar; hashing; strings; trees
Rick is in love with Unity. But Mr. Meeseeks also love Unity, so Rick and Mr. Meeseeks are ""love rivals"". Unity loves rap, so it decided that they have to compete in a rap game (battle) in order to choose the best. Rick is too nerds, so instead he's gonna make his verse with running his original algorithm on lyrics "...
The first line of input contain two integers n and q (2 ≤ n ≤ 20000, 1 ≤ q ≤ 20000) — number of vertices in tree and number of queries respectively.The next n - 1 lines contain the edges. Each line contains two integers v and u (endpoints of the edge) followed by an English lowercase letter c (1 ≤ v, u ≤ n, v ≠ u).The ...
Print the answer for each query in one line.
Here's the tree of first sample testcase: Here's the tree of second sample testcase: In this test: str(8, 1) = poo str(8, 2) = poe str(8, 3) = po str(8, 4) = pop str(8, 5) = popd str(8, 6) = popp str(8, 7) = p So, for the first query, and for the third query is the answer.
Input: 4 34 1 t3 2 p1 2 s3 21 32 1 | Output: 011
Master
5
1,467
415
44
7
723
D
723D
D. Lakes in Berland
1,600
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 i...
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...
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, prin...
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 th...
Input: 5 4 1*****..*******.*..** | Output: 1*****..*********..**
Medium
5
804
437
393
7
1,380
C
1380C
C. Create The Teams
1,400
brute force; dp; greedy; implementation; sortings
There are \(n\) programmers that you want to split into several non-empty teams. The skill of the \(i\)-th programmer is \(a_i\). You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers ...
The first line contains the integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(x\) (\(1 \le n \le 10^5; 1 \le x \le 10^9\)) — the number of programmers and the restriction of team skill respectively.The second line of each test case contain...
For each test case print one integer — the maximum number of teams that you can assemble.
Input: 3 5 10 7 11 2 9 5 4 8 2 4 2 3 4 11 1 3 3 7 | Output: 2 1 0
Easy
5
510
501
89
13
2,057
G
2057G
G. Secret Message
3,000
constructive algorithms; dfs and similar; math
Every Saturday, Alexander B., a teacher of parallel X, writes a secret message to Alexander G., a teacher of parallel B, in the evening. Since Alexander G. is giving a lecture at that time and the message is very important, Alexander B. has to write this message on an interactive online board.The interactive online boa...
The first line contains the number \(t\) (\(1 \le t \le 80\,000\)) — the number of test cases.In the first line of each test case, the numbers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^6\)) — the dimensions of the grid are given.The following \(n\) lines contain the description of the grid.It is guaranteed that the ...
For each test case, output \(n\) lines consisting of \(m\) symbols, where each symbol encodes the state of the cell: ""#"" — the cell is in \(A\) but not in \(S\); ""S"" — the cell is in both \(A\) and \(S\); ""."" — the cell is neither in \(A\) nor in \(S\).
In the first example, \(s=5\) and \(p=12\), thus the number of cells in \(S\) must not exceed \(\frac{1}{5} \cdot (5+12) = 3.4\), meaning \(|S| \le 3\). Note that the presented set \(S\) consists of \(1\) cell and clearly satisfies all the constraints.In the second example, \(s=12\) and \(p=16\), thus the number of cel...
Input: 33 3.#.###.#.2 6############3 7###....#.#.######.... | Output: .#. #S# .#. #S##S# #S##S# S#S.... #.#.S#S S#S....
Master
3
1,182
396
259
20
1,941
E
1941E
E. Rudolf and k Bridges
1,600
binary search; data structures; dp; two pointers
Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem.The river is a grid of \(n\) rows and \(m\) columns. The intersection of the \(i\)-th row and the \(j\)-th column contains the number \(a_{i,j...
The first line contains a single integer \(t\) \((1 \le t \le 10^3)\) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains four integers \(n\), \(m\), \(k\), and \(d\) (\(1 \le k \le n \le 100\), \(3 \le m \le 2 \cdot 10^5\), \(1 \le d \le m\)) — the number of ...
For each test case, output a single number — the minimum total cost of supports installation.
In the first test case, it is most profitable to build a bridge on the second row. It is not a top view, but side view: gray cells — bridge itself, white cells are empty, black cells — supports, blue cells — water, brown cells — river bottom. In the second test case, it is most profitable to build bridges on the second...
Input: 53 11 1 40 1 2 3 4 5 4 3 2 1 00 1 2 3 2 1 2 3 3 2 00 1 2 3 5 5 5 5 5 2 04 4 2 10 3 3 00 2 1 00 1 2 00 3 3 04 5 2 50 1 1 1 00 2 2 2 00 2 1 1 00 3 2 1 01 8 1 10 10 4 8 4 4 2 04 5 3 20 8 4 4 00 3 4 8 00 8 1 10 00 10 1 5 0 | Output: 4 8 4 15 14
Medium
4
1,369
699
93
19
1,492
A
1492A
A. Three swimmers
800
math
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly \(a\) minutes to swim across the entire pool and come back, exactly \(b\) minutes for the second swimmer and \(c\) minutes for the third. Hence, the first swim...
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) — the number of test cases. Next \(t\) lines contains test case descriptions, one per line.Each line contains four integers \(p\), \(a\), \(b\) and \(c\) (\(1 \leq p, a, b, c \leq 10^{18}\)), time in minutes after the start, when you c...
For each test case, output one integer — how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
In the first test case, the first swimmer is on the left side in \(0, 5, 10, 15, \ldots\) minutes after the start time, the second swimmer is on the left side in \(0, 4, 8, 12, \ldots\) minutes after the start time, and the third swimmer is on the left side in \(0, 8, 16, 24, \ldots\) minutes after the start time. You ...
Input: 4 9 5 4 8 2 6 10 9 10 2 5 10 10 9 9 9 | Output: 1 4 0 8
Beginner
1
792
417
144
14
1,491
A
1491A
A. K-th Largest Value
800
brute force; greedy; implementation
You are given an array \(a\) consisting of \(n\) integers. Initially all elements of \(a\) are either \(0\) or \(1\). You need to process \(q\) queries of two kinds: 1 x : Assign to \(a_x\) the value \(1 - a_x\). 2 k : Print the \(k\)-th largest value of the array. As a reminder, \(k\)-th largest value of the array \(b...
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)) — the length of the given array and the number of queries.The second line contains \(n\) integers \(a_1, a_2, a_3, \dots, a_n\) (\(0 \le a_i \le 1\)) — elements of the initial array.Each of the following \(q\) lines contains two integers. Th...
For each query of the second type, print a single integer — the answer to the query.
Initially \(a = [1, 1, 0, 1, 0]\).The first operation is printing the third largest value, which is \(1\).The second operation is assigning \(a_2\) the value \(0\), \(a\) becomes \([1, 0, 0, 1, 0]\).The third operation is printing the third largest value, it is \(0\).The fourth operation is printing the first largest v...
Input: 5 5 1 1 0 1 0 2 3 1 2 2 3 2 1 2 5 | Output: 1 0 1 0
Beginner
3
626
756
84
14
162
A
162A
A. Pentagonal numbers
1,100
*special; implementation
Pentagonal numbers are figurate numbers which can be calculated using the formula pn = (3n2 - n) / 2 (always integer). You are given n; calculate n-th pentagonal number.
The only line of input contains an integer n (1 ≤ n ≤ 100).
Output the n-th pentagonal number.
Input: 2 | Output: 5
Easy
2
169
59
34
1
1,793
D
1793D
D. Moscow Gorillas
1,800
binary search; dp; greedy; implementation; math; two pointers
In winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation \(p\) of length \(n\) to the zoo.A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in any order. For example, \([2,3...
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the permutations length.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) — the elements of the permutation \(p\).The third line contains \(n\) integers \(q_1, q_2, \ldots, q_n\) (\(1 \le q_i \le n\)) —...
Print a single integer — the number of suitable pairs \(l\) and \(r\).
In the first example, two segments are correct – \([1, 3]\) with \(\operatorname{MEX}\) equal to \(4\) in both arrays and \([3, 3]\) with \(\operatorname{MEX}\) equal to \(1\) in both of arrays.In the second example, for example, the segment \([1, 4]\) is correct, and the segment \([6, 7]\) isn't correct, because \(\op...
Input: 31 3 22 1 3 | Output: 2
Medium
6
1,089
359
70
17
665
C
665C
C. Simple Strings
1,300
dp; greedy; strings
zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add are not simple.zscoder is given a string s. He wants to change a minimum number of characters so that the string s becomes simple. Help him with this task!
The only line contains the string s (1 ≤ |s| ≤ 2·105) — the string given to zscoder. The string s consists of only lowercase English letters.
Print the simple string s' — the string s after the minimal number of changes. If there are multiple solutions, you may output any of them.Note that the string s' should also consist of only lowercase English letters.
Input: aab | Output: bab
Easy
3
320
141
217
6
55
C
55C
C. Pie or die
1,900
games
Volodya and Vlad play the following game. There are k pies at the cells of n × m board. Each turn Volodya moves one pie to the neighbouring (by side) cell. If the pie lies at the border of the board then Volodya can move it outside the board, get the pie and win. After Volodya's move, Vlad bans some edge at the border ...
First line contains 3 integers, separated by space: 1 ≤ n, m ≤ 100 — dimensions of the board and 0 ≤ k ≤ 100 — the number of pies. Each of the next k lines contains 2 integers, separated by space: 1 ≤ x ≤ n, 1 ≤ y ≤ m — coordinates of the corresponding pie. There could be more than one pie at a cell.
Output only one word: ""YES"" — if Volodya wins, ""NO"" — otherwise.
Input: 2 2 11 2 | Output: YES
Hard
1
564
301
68
0
776
E
776E
E. The Holmes Children
2,100
math; number theory
The Holmes children are fighting over who amongst them is the cleverest.Mycroft asked Sherlock and Eurus to find value of f(n), where f(1) = 1 and for n ≥ 2, f(n) is the number of distinct ordered positive integer pairs (x, y) that satisfy x + y = n and gcd(x, y) = 1. The integer gcd(a, b) is the greatest common diviso...
A single line of input contains two space separated integers n (1 ≤ n ≤ 1012) and k (1 ≤ k ≤ 1012) indicating that Eurus asks Sherlock and Mycroft to find the value of Fk(n) modulo 1000000007.
Output a single integer — the value of Fk(n) modulo 1000000007.
In the first case, there are 6 distinct ordered pairs (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1) satisfying x + y = 7 and gcd(x, y) = 1. Hence, f(7) = 6. So, F1(7) = f(g(7)) = f(f(7) + f(1)) = f(6 + 1) = f(7) = 6.
Input: 7 1 | Output: 6
Hard
2
727
192
63
7
1,826
B
1826B
B. Lunatic Never Content
1,100
math; number theory
You have an array \(a\) of \(n\) non-negative integers. Let's define \(f(a, x) = [a_1 \bmod x, a_2 \bmod x, \dots, a_n \bmod x]\) for some positive integer \(x\). Find the biggest \(x\), such that \(f(a, x)\) is a palindrome.Here, \(a \bmod x\) is the remainder of the integer division of \(a\) by \(x\).An array is a pa...
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^5\)).The second line of each test case contains \(n\) integers \(a_i\) (\(0 \leq a_i \leq 10^9\)).It's guaranteed that the sum of all \(...
For each test case output the biggest \(x\), such that \(f(a, x)\) is a palindrome. If \(x\) can be infinitely large, output \(0\) instead.
In the first example, \(f(a, x = 1) = [0, 0]\) which is a palindrome.In the second example, \(f(a, x = 2) = [1, 0, 1, 0, 0, 1, 0, 1]\) which is a palindrome.It can be proven that in the first two examples, no larger \(x\) satisfies the condition.In the third example, \(f(a, x) = [0]\) for any \(x\), so we can choose it...
Input: 421 283 0 1 2 0 3 2 1103100 1 1000000000 | Output: 1 2 0 999999900
Easy
2
498
349
139
18
260
A
260A
A. Adding Digits
1,400
implementation; math
Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to repeat the operation of lengthening number a n times.One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by V...
The first line contains three integers: a, b, n (1 ≤ a, b, n ≤ 105).
In a single line print the integer without leading zeros, which Vasya can get when he applies the lengthening operations to number a n times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them.
Input: 5 4 5 | Output: 524848
Easy
2
574
68
248
2
212
B
212B
B. Polycarpus is Looking for Good Substrings
2,300
bitmasks; hashing; implementation
We'll call string s[a, b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|) a substring of string s = s1s2... s|s|, where |s| is the length of string s.The trace of a non-empty string t is a set of characters that the string consists of. For example, the trace of string ""aab"" equals {'a', 'b'}.Let's consider an arbitrary string s a...
The first line contains a non-empty string s (1 ≤ |s| ≤ 106).The second line contains a single integer m (1 ≤ m ≤ 104). Next m lines contain descriptions of sets Ci. The i-th line contains string ci such that its trace equals Ci. It is guaranteed that all characters of each string ci are different.Note that Ci are not ...
Print m integers — the i-th integer must equal r(Ci, s).
Input: aaaaa2aa | Output: 11
Expert
3
1,102
398
56
2
1,321
C
1321C
C. Remove Adjacent
1,600
brute force; constructive algorithms; greedy; strings
You are given a string \(s\) consisting of lowercase Latin letters. Let the length of \(s\) be \(|s|\). You may perform several operations on this string.In one operation, you can choose some index \(i\) and remove the \(i\)-th character of \(s\) (\(s_i\)) if at least one of its adjacent characters is the previous lett...
The first line of the input contains one integer \(|s|\) (\(1 \le |s| \le 100\)) — the length of \(s\).The second line of the input contains one string \(s\) consisting of \(|s|\) lowercase Latin letters.
Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is \(4\).In the second example, you can remove all but one character of \(s\). The only possible answer follows. During th...
Input: 8 bacabcab | Output: 4
Medium
4
1,561
204
123
13
2,059
C
2059C
C. Customer Service
1,600
brute force; constructive algorithms; graph matchings; greedy; math; sortings
Nikyr has started working as a queue manager at the company ""Black Contour."" He needs to choose the order of servicing customers. There are a total of \(n\) queues, each initially containing \(0\) people. In each of the next \(n\) moments of time, there are two sequential events: New customers arrive in all queues. M...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 300\)) — the number of queues and moments of time...
For each test case, output a single integer — the maximum value of \(\operatorname{MEX}([x_1, x_2, \ldots, x_n])\) that can be achieved.
In the first test case, the second queue can be served at time \(1\), and the first queue at time \(2\). There will be \(x_1 = 0\) people left in the first queue and \(x_2 = 1\) person left in the second queue. Therefore, the answer is \(\operatorname{MEX}([0, 1]) = 2\).In the second test case, the first queue can be s...
Input: 421 22 1210 1010 1032 3 34 4 12 1 144 2 2 171 9 3 15 5 5 111 2 1 1 | Output: 2 1 3 3
Medium
6
1,369
618
136
20
1,368
C
1368C
C. Even Picture
1,500
constructive algorithms
Leo Jr. draws pictures in his notebook with checkered sheets (that is, each sheet has a regular square grid printed on it). We can assume that the sheets are infinitely large in any direction.To draw a picture, Leo Jr. colors some of the cells on a sheet gray. He considers the resulting picture beautiful if the followi...
The only line contains a single integer \(n\) (\(1 \leq n \leq 500\)) — the number of gray cells with all gray neighbours in a beautiful picture.
In the first line, print a single integer \(k\) — the number of gray cells in your picture. For technical reasons, \(k\) should not exceed \(5 \cdot 10^5\).Each of the following \(k\) lines should contain two integers — coordinates of a gray cell in your picture. All listed cells should be distinct, and the picture sho...
The answer for the sample is pictured below:
Input: 4 | Output: 12 1 0 2 0 0 1 1 1 2 1 3 1 0 2 1 2 2 2 3 2 1 3 2 3
Medium
1
1,246
145
522
13
358
D
358D
D. Dima and Hares
1,800
dp; greedy
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with car...
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≤ ai, bi, ci ≤ 1...
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Input: 41 2 3 44 3 2 10 1 1 0 | Output: 13
Medium
2
980
647
97
3
30
C
30C
C. Shooting Gallery
1,800
dp; probabilities
One warm and sunny day king Copa decided to visit the shooting gallery, located at the Central Park, and try to win the main prize — big pink plush panda. The king is not good at shooting, so he invited you to help him.The shooting gallery is an infinite vertical plane with Cartesian coordinate system on it. The target...
The first line contains integer n (1 ≤ n ≤ 1000) — amount of targets in the shooting gallery. Then n lines follow, each describing one target. Each description consists of four numbers xi, yi, ti, pi (where xi, yi, ti — integers, - 1000 ≤ xi, yi ≤ 1000, 0 ≤ ti ≤ 109, real number pi is given with no more than 6 digits a...
Output the maximum expected value of the amount of targets that was shot by the king. Your answer will be accepted if it differs from the correct answer by not more than 10 - 6.
Input: 10 0 0 0.5 | Output: 0.5000000000
Medium
2
954
397
177
0
1,373
B
1373B
B. 01 Game
900
games
Alica and Bob are playing a game.Initially they have a binary string \(s\) consisting of only characters 0 and 1.Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent char...
First line contains one integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases.Only line of each test case contains one string \(s\) (\(1 \le |s| \le 100\)), consisting of only characters 0 and 1.
For each test case print answer in the single line.If Alice can win print DA (YES in Russian) in any register. Otherwise print NET (NO in Russian) in any register.
In the first test case after Alice's move string \(s\) become empty and Bob can not make any move.In the second test case Alice can not make any move initially.In the third test case after Alice's move string \(s\) turn into \(01\). Then, after Bob's move string \(s\) become empty and Alice can not make any move.
Input: 3 01 1111 0011 | Output: DA NET NET
Beginner
1
815
205
163
13
208
D
208D
D. Prizes, Prizes, more Prizes
1,200
implementation
Vasya, like many others, likes to participate in a variety of sweepstakes and lotteries. Now he collects wrappings from a famous chocolate bar ""Jupiter"". According to the sweepstake rules, each wrapping has an integer written on it — the number of points that the participant adds to his score as he buys the bar. Afte...
The first line contains a single integer n (1 ≤ n ≤ 50) — the number of chocolate bar wrappings that brought points to Vasya. The second line contains space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ 109). The third line contains 5 integers a, b, c, d, e (1 ≤ a < b < c < d < e ≤ 109) — the prizes' costs.
Print on the first line 5 integers, separated by a space — the number of mugs, towels, bags, bicycles and cars that Vasya has got, respectively. On the second line print a single integer — the number of points Vasya will have left after all operations of exchange are completed.Please, do not use the %lld specifier to r...
In the first sample Vasya gets 3 points after eating the first chocolate bar. Then he exchanges 2 points and gets a mug. Vasya wins a bag after eating the second chocolate bar. Then he wins a towel after eating the third chocolate bar. After all chocolate bars 3 - 2 + 10 - 10 + 4 - 4 = 1 points remains.
Input: 33 10 42 4 10 15 20 | Output: 1 1 1 0 0 1
Easy
1
1,653
307
425
2
1,895
D
1895D
D. XOR Construction
1,900
bitmasks; constructive algorithms; data structures; math; string suffix structures; trees
You are given \(n-1\) integers \(a_1, a_2, \dots, a_{n-1}\).Your task is to construct an array \(b_1, b_2, \dots, b_n\) such that: every integer from \(0\) to \(n-1\) appears in \(b\) exactly once; for every \(i\) from \(1\) to \(n-1\), \(b_i \oplus b_{i+1} = a_i\) (where \(\oplus\) denotes the bitwise XOR operator).
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line contains \(n-1\) integers \(a_1, a_2, \dots, a_{n-1}\) (\(0 \le a_i \le 2n\)).Additional constraint on the input: it's always possible to construct at least one valid array \(b\) from the given sequence \(a\).
Print \(n\) integers \(b_1, b_2, \dots, b_n\). If there are multiple such arrays, you may print any of them.
Input: 42 1 2 | Output: 0 2 3 1
Hard
6
318
298
108
18
1,357
A1
1357A1
A1. Figure out direction of CNOT
0
*special
You are given an operation that implements a two-qubit unitary transformation: either the CNOT gate with the first qubit as control and the second qubit as target (CNOT\(_{12}\)), or the CNOT gate with the second qubit as control and the first qubit as target (CNOT\(_{21}\)). The operation will have Adjoint and Control...
Beginner
1
1,057
0
0
13
998
A
998A
A. Balloons
1,000
constructive algorithms; implementation
There are quite a lot of ways to have fun with inflatable balloons. For example, you can fill them with water and see what happens.Grigory and Andrew have the same opinion. So, once upon a time, they went to the shop and bought \(n\) packets with inflatable balloons, where \(i\)-th of them has exactly \(a_i\) balloons ...
The first line of input contains a single integer \(n\) (\(1 \le n \le 10\)) — the number of packets with balloons.The second line contains \(n\) integers: \(a_1\), \(a_2\), \(\ldots\), \(a_n\) (\(1 \le a_i \le 1000\)) — the number of balloons inside the corresponding packet.
If it's impossible to divide the balloons satisfying the conditions above, print \(-1\).Otherwise, print an integer \(k\) — the number of packets to give to Grigory followed by \(k\) distinct integers from \(1\) to \(n\) — the indices of those. The order of packets doesn't matter.If there are multiple ways to divide ba...
In the first test Grigory gets \(3\) balloons in total while Andrey gets \(1\).In the second test there's only one way to divide the packets which leads to equal numbers of balloons.In the third test one of the boys won't get a packet at all.
Input: 31 2 1 | Output: 21 2
Beginner
2
857
276
347
9
592
E
592E
E. BCPC
2,700
binary search; geometry; two pointers
BCPC stands for Byteforces Collegiate Programming Contest, and is the most famous competition in Byteforces.BCPC is a team competition. Each team is composed by a coach and three contestants. Blenda is the coach of the Bit State University(BSU), and she is very strict selecting the members of her team. In BSU there are...
In the first line of the input three integers n, c and d (3 ≤ n ≤ 345678, 1 ≤ c, d ≤ 109) are written. They denote the number of students Blenda can use to form teams, the value subtracted from all reading speeds and the value subtracted from all writing speeds respectively.Each of the next n lines contains two integer...
Print the number of different teams in BSU, that are good according to Blenda's definition.
In the first sample the following teams are good: (i = 1, j = 2, k = 3), (i = 2, j = 5, k = 1), (i = 1, j = 4, k = 3), (i = 5, j = 1, k = 4).Note, that for example the team (i = 3, j = 1, k = 2) is also good, but is considered to be the same as the team (i = 1, j = 2, k = 3).
Input: 5 2 21 14 12 33 23 4 | Output: 4
Master
3
1,645
525
91
5
1,654
B
1654B
B. Prefix Removals
800
strings
You are given a string \(s\) consisting of lowercase letters of the English alphabet. You must perform the following algorithm on \(s\): Let \(x\) be the length of the longest prefix of \(s\) which occurs somewhere else in \(s\) as a contiguous substring (the other occurrence may also intersect the prefix). If \(x = 0\...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.This is followed by \(t\) lines, each containing a description of one test case. Each line contains a string \(s\). The given strings consist only of lowercase letters of the English alphabet and have lengths between \(1\) ...
For each test case, print a single line containing the string \(s\) after executing the algorithm. It can be shown that such string is non-empty.
The first test case is explained in the statement.In the second test case, no operations can be performed on \(s\).In the third test case, Initially, \(s =\) ""bbbbbbbbbb"". After \(1\) operation, \(s =\) ""b"". In the fourth test case, Initially, \(s =\) ""codeforces"". After \(1\) operation, \(s =\) ""odeforces"". Af...
Input: 6abcabdcabbbbbbbbbbcodeforcescffcfccffccfcffcfccfcffccffcfccfzyzyzwxxyyxxyyzzyzzxxwzxwywxwzxxyzzw | Output: abdc a b deforces cf xyzzw
Beginner
1
1,196
458
145
16
1,725
F
1725F
F. Field Photography
2,100
bitmasks; data structures; sortings
Pak Chanek is traveling to Manado. It turns out that OSN (Indonesian National Scientific Olympiad) 2019 is being held. The contestants of OSN 2019 are currently lining up in a field to be photographed. The field is shaped like a grid of size \(N \times 10^{100}\) with \(N\) rows and \(10^{100}\) columns. The rows are n...
The first line contains a single integer \(N\) (\(1 \leq N \leq 10^5\)) — the number of rows in the grid and also the number of provinces that participate in OSN 2019.The \(i\)-th of the next \(N\) lines contains two integers \(L_i\) and \(R_i\) (\(1 \leq L_i \leq R_i \leq 10^9\)) describing the positions of the contes...
Output \(Q\) lines, with the \(j\)-th line containing an integer that is the answer to the \(j\)-th question.
For the \(1\)-st question, Pak Chanek can do the following operations to get \(M=2\): Move all contestants in row \(2\) to the east by \(4\) tiles. \(Z\) changes into \(0 \text{ OR } 4 = 4\). Move all contestants in row \(1\) to the east by \(12\) tiles. \(Z\) changes into \(4 \text{ OR } 12 = 12\). Now, columns \(14\)...
Input: 3 1 5 10 11 8 8 2 12 5 | Output: 2 3
Hard
3
2,106
612
109
17
1,967
C
1967C
C. Fenwick Tree
2,300
bitmasks; brute force; combinatorics; data structures; dp; math; trees
Let \(\operatorname{lowbit}(x)\) denote the value of the lowest binary bit of \(x\), e.g. \(\operatorname{lowbit}(12)=4\), \(\operatorname{lowbit}(8)=8\).For an array \(a\) of length \(n\), if an array \(s\) of length \(n\) satisfies \(s_k=\left(\sum\limits_{i=k-\operatorname{lowbit}(k)+1}^{k}a_i\right)\bmod 998\,244\,...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le 10^4\)). The description of the test cases follows.The first line of each test case contains two positive integers \(n\) (\(1 \leq n \leq 2\cdot 10^5\)) and \(k\) (\(1\le k\le 10^9\)), representing the length of ...
For each test case, print a single line, containing a valid array \(a\) of length \(n\).
In the first test case, it can be seen that \(f^1([1,1,1,1,1,1,1,1])=[1,2,1,4,1,2,1,8]\).In the second test case, it can be seen that \(f^2([1,2,3,4,5,6])=f^1([1,3,3,10,5,11])=[1,4,3,17,5,16]\).
Input: 28 11 2 1 4 1 2 1 86 21 4 3 17 5 16 | Output: 1 1 1 1 1 1 1 1 1 2 3 4 5 6
Expert
7
875
586
88
19
2,020
B
2020B
B. Brightness Begins
1,200
binary search; math
Imagine you have \(n\) light bulbs numbered \(1, 2, \ldots, n\). Initially, all bulbs are on. To flip the state of a bulb means to turn it off if it used to be on, and to turn it on otherwise.Next, you do the following: for each \(i = 1, 2, \ldots, n\), flip the state of all bulbs \(j\) such that \(j\) is divisible by ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The only line of each test case contains a single integer \(k\) (\(1 \le k \le 10^{18}\)).
For each test case, output \(n\) — the minimum number of bulbs.
In the first test case, the minimum number of bulbs is \(2\). Let's denote the state of all bulbs with an array, where \(1\) corresponds to a turned on bulb, and \(0\) corresponds to a turned off bulb. Initially, the array is \([1, 1]\). After performing the operation with \(i = 1\), the array becomes \([\underline{0},...
Input: 3138 | Output: 2 5 11
Easy
2
728
251
63
20
2,033
A
2033A
A. Sakurako and Kosuke
800
constructive algorithms; implementation; math
Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position \(x=0\). They will be taking turns, and Sakurako will be the one to start. On the \(i\)-th move, the current player will move the dot in some direction by \(2\cdot i-1\) units. Sakurako will always b...
The first line contains one integer \(t\) (\(1\le t\le 100\)) — the number of games that Sakurako and Kosuke played.Each game is described by one number \(n\) (\(1 \le n\le 100\)) — the number that defines the condition when the game ends.
For each of the \(t\) games, output a line with the result of that game. If Sakurako makes the last turn, output ""Sakurako"" (without quotes); else output ""Kosuke"".
Input: 416398 | Output: Kosuke Sakurako Kosuke Sakurako
Beginner
3
967
239
167
20
710
C
710C
C. Magic Odd Square
1,500
constructive algorithms; math
Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.
The only line contains odd integer n (1 ≤ n ≤ 49).
Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.
Input: 1 | Output: 1
Medium
2
121
50
152
7
216
B
216B
B. Forming Teams
1,700
dfs and similar; implementation
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people.We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is...
The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100) — the number of students and the number of pairs of archenemies correspondingly.Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≤ ai, bi ≤ n, ai ≠ bi) — the indexes of the students who are enemies...
Print a single integer — the minimum number of students you will have to send to the bench in order to start the game.
Input: 5 41 22 45 31 4 | Output: 1
Medium
2
670
537
118
2
2,101
B
2101B
B. Quartet Swapping
1,800
brute force; data structures; divide and conquer; greedy; sortings
You are given a permutation \(a\) of length \(n\)\(^{\text{∗}}\). You are allowed to do the following operation any number of times (possibly zero): Choose an index \(1\le i\le n - 3\). Then, swap \(a_i\) with \(a_{i + 2}\), and \(a_{i + 1}\) with \(a_{i + 3}\) simultaneously. In other words, permutation \(a\) will be ...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(4\le n\le 2\cdot 10^5\)) — the length of permutation \(a\).The second line contains \(n\) ...
For each test case, output the lexicographically smallest permutation that can be obtained by applying the above operation any number of times.
In the first test case, an operation can be done on index \(i = 1\), and the permutation will become \([1, 2, 3, 4]\), which is the lexicographically smallest permutation achievable.In the second test case, we can do the following sequence of operations: Do an operation on index \(i = 2\). The permutation becomes \([5,...
Input: 343 4 1 255 4 3 1 21010 9 8 7 6 5 4 3 2 1 | Output: 1 2 3 4 2 1 3 4 5 2 1 4 3 6 5 8 7 10 9
Medium
5
1,192
504
143
21
1,769
D2
1769D2
D2. Игра в Девятку II
2,200
*special; brute force
В этой версии задачи игроки начинают играть не только на победу, но и на оптимизацию результата игры для них. Вводится понятие величины важности первого хода, и нужно найти \(13\) раскладов с различными значениями этой величины.Алиса и Боб решили сыграть в карточную игру «Девятка». Пожалуйста, внимательно прочитайте ус...
В единственной строке задано целое число \(k\) (\(2 \le k \le 13\)) — число необходимых раскладов.В задаче два теста. В первом тесте \(k = 2\), во втором тесте \(k = 13\).
Выведите \(k\) пар строк. Каждая пара строк должна соответствовать некоторому раскладу. Величины важности первого хода для всех выведенных раскладов должны быть различными целыми числами.В первой строке каждой пары выведите \(18\) строк длины \(2\) через пробел, описывающих карты Алисы в любом порядке. Первый символ ст...
В первом выведенном раскладе все девятки находятся в руке у Алисы. Даже если Боб будет ходить первым, ему всё равно придётся пропустить первый же свой ход. Следовательно, первый ход при таком раскладе имеет важность \(0\).Во втором выведенном раскладе вне зависимости от того, чьим будет первый ход, выиграет Алиса. Одна...
Input: 2 | Output: KS QD 8D QC 8S 8C JD 9H AC TH 9S 9D QH 7H 8H TS 7S 9C 6D JS 7D KH QS TC AD AS KC 6C 7C TD AH KD 6S JC JH 6H JC JS 8S TD JD KH 7D 9C KC TH QD 8D 7H TC KD 9H 8C 6D 7S AC QH AD 8H TS 6H JH 6C AH 7C 6S 9D QC AS QS KS 9S
Hard
2
3,116
171
860
17
1,198
B
1198B
B. Welfare State
1,600
binary search; brute force; data structures; sortings
There is a country with \(n\) citizens. The \(i\)-th of them initially has \(a_{i}\) money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes t...
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^{5}\)) — the numer of citizens.The next line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 \le a_{i} \le 10^{9}\)) — the initial balances of citizens.The next line contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^{5}\)) — th...
Print \(n\) integers — the balances of all citizens after all events.
In the first example the balances change as follows: 1 2 3 4 \(\rightarrow\) 3 3 3 4 \(\rightarrow\) 3 2 3 4 \(\rightarrow\) 3 2 3 4In the second example the balances change as follows: 3 50 2 1 10 \(\rightarrow\) 3 0 2 1 10 \(\rightarrow\) 8 8 8 8 10 \(\rightarrow\) 8 8 20 8 10
Input: 4 1 2 3 4 3 2 3 1 2 2 2 1 | Output: 3 2 3 4
Medium
4
694
712
69
11
138
B
138B
B. Digits Permutations
1,900
greedy
Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly.Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and so on ...
The first line contains a positive integer n — the original number. The number of digits in this number does not exceed 105. The number is written without any leading zeroes.
Print two permutations of digits of number n, such that the sum of these numbers ends with the maximum number of zeroes. The permutations can have leading zeroes (if they are present, they all should be printed). The permutations do not have to be different. If there are several answers, print any of them.
Input: 198 | Output: 981819
Hard
1
664
174
307
1
1,994
E
1994E
E. Wooden Game
2,000
bitmasks; greedy; math; trees
You are given a forest of \(k\) rooted trees\(^{\text{∗}}\). Lumberjack Timofey wants to cut down the entire forest by applying the following operation: Select a subtree\(^{\text{†}}\) of any vertex of one of the trees and remove it from the tree. Timofey loves bitwise operations, so he wants the bitwise OR of the size...
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains a single integer \(k\) (\(1 \leq k \leq 10^6\)) — the number of trees in the forest.This is fo...
For each test case, output a single integer — the maximum result that can be obtained.
In the second test case, the trees look like this:The first operation removes the entire second tree.The second operation removes vertex \(4\) from the first tree.The third operation removes the first tree. The result is \(6|1|3 = 7\) (\(|\) denotes bitwise OR).In the third test case, the entire tree needs to be remove...
Input: 311241 2 261 1 3 1 31101 2 2 1 1 5 7 6 4 | Output: 1 7 10
Hard
4
765
819
86
19
1,475
G
1475G
G. Strange Beauty
1,900
dp; math; number theory; sortings
Polycarp found on the street an array \(a\) of \(n\) elements.Polycarp invented his criterion for the beauty of an array. He calls an array \(a\) beautiful if at least one of the following conditions must be met for each different pair of indices \(i \ne j\): \(a_i\) is divisible by \(a_j\); or \(a_j\) is divisible by ...
The first line contains one integer \(t\) (\(1 \leq t \leq 10\)) — the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the array \(a\).The second line of each test case contains \(n\) numbers \(a_1, a_2, \ldo...
For each test case output one integer — the minimum number of elements that must be removed to make the array \(a\) beautiful.
In the first test case, removing \(7\) and \(14\) will make array \(a\) beautiful.In the second test case, the array \(a\) is already beautiful.In the third test case, removing one of the elements \(45\) or \(18\) will make the array \(a\) beautiful.In the fourth test case, the array \(a\) is beautiful.
Input: 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 | Output: 2 0 1 0
Hard
4
901
393
126
14
933
E
933E
E. A Preponderant Reunion
3,200
constructive algorithms; dp
East or west, home is best. That's why family reunion, the indispensable necessity of Lunar New Year celebration, is put in such a position.After the reunion dinner, Little Tommy plays a game with the family. Here is a concise introduction to this game: There is a sequence of n non-negative integers p1, p2, ..., pn in ...
The first line contains one integer n (1 ≤ n ≤ 3·105).The second line contains n space-separated integers p1, p2, ..., pn (0 ≤ pi ≤ 109, i = 1, 2, ..., n).
In the first line print one integer as the number of descensions m (0 ≤ m ≤ n - 1).In the next m lines print the descensions chronologically. More precisely, in each line of the next m lines print one integer i (1 ≤ i < n) representing a descension would operate on pi and pi + 1 such that all the descensions could be u...
In the first sample, one possible best solution is , of which the cost is 1 + 1 = 2.In the second sample, one possible best solution is , of which the cost is 1 + 1 + 1 = 3.
Input: 42 1 3 1 | Output: 213
Master
2
969
155
429
9
1,849
D
1849D
D. Array Painting
1,700
constructive algorithms; greedy; two pointers
You are given an array of \(n\) integers, where each integer is either \(0\), \(1\), or \(2\). Initially, each element of the array is blue.Your goal is to paint each element of the array red. In order to do so, you can perform operations of two types: pay one coin to choose a blue element and paint it red; choose a re...
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2\)).
Print one integer — the minimum number of coins you have to spend in order to paint all elements red.
In the first example, you can paint all elements red with having to spend only one coin as follows: paint the \(2\)-nd element red by spending one coin; decrease the \(2\)-nd element by \(1\) and paint the \(1\)-st element red; decrease the \(2\)-nd element by \(1\) and paint the \(3\)-rd element red. In the second exa...
Input: 3 0 2 0 | Output: 1
Medium
3
549
160
101
18
268
A
268A
A. Games
800
brute force
Manao works on a sports TV. He's spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team plays as ...
The first line contains an integer n (2 ≤ n ≤ 30). Each of the following n lines contains a pair of distinct space-separated integers hi, ai (1 ≤ hi, ai ≤ 100) — the colors of the i-th team's home and guest uniforms, respectively.
In a single line print the number of games where the host team is going to play in the guest uniform.
In the first test case the championship consists of 6 games. The only game with the event in question is the game between teams 2 and 1 on the stadium of team 2.In the second test sample the host team will have to wear guest uniform in the games between teams: 1 and 2, 2 and 1, 2 and 3, 3 and 4, 4 and 2 (the host team ...
Input: 31 22 43 4 | Output: 1
Beginner
1
1,180
230
101
2
1,477
E
1477E
E. Nezzar and Tournaments
3,300
data structures; greedy
In the famous Oh-Suit-United tournament, two teams are playing against each other for the grand prize of precious pepper points.The first team consists of \(n\) players, and the second team consists of \(m\) players. Each player has a potential: the potential of the \(i\)-th player in the first team is \(a_i\), and the...
The first line contains three integers \(n\), \(m\), and \(q\) (\(1 \le n,m \le 2 \cdot 10^5, 1 \le q \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^6\)).The third line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(0 \le b_i \le 10^6\)).The following \(q...
For each query of the third type print the answer to this query.
In the first query of the first test, the tournament is held with \(k = 5\). It would be optimal to arrange players in such way (here their potentials are written):\(\underline{7}\), \(3\), \(5\), \(4\), \(6\), \(\underline{1}\), \(\underline{2}\) (underlined numbers are potentials of players that are from the first te...
Input: 3 4 3 1 2 7 3 4 5 6 3 5 1 1 10 3 5 | Output: -4 9
Master
2
1,925
597
64
14
1,613
D
1613D
D. MEX Sequences
1,900
dp; math
Let's call a sequence of integers \(x_1, x_2, \dots, x_k\) MEX-correct if for all \(i\) (\(1 \le i \le k\)) \(|x_i - \operatorname{MEX}(x_1, x_2, \dots, x_i)| \le 1\) holds. Where \(\operatorname{MEX}(x_1, \dots, x_k)\) is the minimum non-negative integer that doesn't belong to the set \(x_1, \dots, x_k\). For example,...
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) — the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le n\)).The sum of \(n\) over all test cases doe...
For each test case, print a single integer — the number of non-empty MEX-correct subsequences of a given array, taken modulo \(998244353\).
In the first example, the valid subsequences are \([0]\), \([1]\), \([0,1]\) and \([0,2]\).In the second example, the valid subsequences are \([0]\) and \([1]\).In the third example, any non-empty subsequence is valid.
Input: 4 3 0 2 1 2 1 0 5 0 0 0 0 0 4 0 1 2 3 | Output: 4 2 31 7
Hard
2
1,059
349
139
16
1,307
E
1307E
E. Cow and Treats
2,500
binary search; combinatorics; dp; greedy; implementation; math
After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass!On the field, there is a row of \(n\) units of grass, each with a sweetness \(s_i\). Farmer John has \(m\) cows, each with a favorite sweetness \(f_i\) and a hunger value \(h_i\). He would like to pick t...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 5000\), \(1 \le m \le 5000\)) — the number of units of grass and the number of cows. The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s_i \le n\)) — the sweetness values of the grass.The \(i\)-th of the following \(m\) lines c...
Output two integers — the maximum number of sleeping cows that can result and the number of ways modulo \(10^9+7\).
In the first example, FJ can line up the cows as follows to achieve \(2\) sleeping cows: Cow \(1\) is lined up on the left side and cow \(2\) is lined up on the right side. Cow \(2\) is lined up on the left side and cow \(1\) is lined up on the right side. In the second example, FJ can line up the cows as follows to ac...
Input: 5 2 1 1 1 1 1 1 2 1 3 | Output: 2 2
Expert
6
1,574
522
115
13
1,810
B
1810B
B. Candies
800
constructive algorithms; math; number theory
This problem is about candy. Initially, you only have \(1\) candy, and you want to have exactly \(n\) candies.You can use the two following spells in any order at most \(40\) times in total. Assume you have \(x\) candies now. If you use the first spell, then \(x\) candies become \(2x-1\) candies. Assume you have \(x\) ...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Their description follows.Each test case contains one line with a single integer \(n\) (\(2 \le n \le 10^9\)) — the required final number of candies.
For each test case, output the following.If it's possible to eventually have \(n\) candies within \(40\) spells, in the first line print an integer \(m\) (\(1 \le m \le 40\)), representing the total number of spells you use.In the second print \(m\) integers \(a_{1}, a_{2}, \ldots, a_{m}\) (\(a_{i}\) is \(1\) or \(2\))...
For \(n=3\), you can just use the second spell once, and then have \(2 \cdot 1 + 1 = 3\) candies.For \(n=7\), you can use the second spell twice. After the first step, you will have \(3\) candies. And after the second step, you will have \(2 \cdot 3 + 1 = 7\) candies.
Input: 423717 | Output: -1 1 2 2 2 2 4 2 1 1 1
Beginner
3
541
287
655
18
1,295
F
1295F
F. Good Contest
2,700
combinatorics; dp; probabilities
An online contest will soon be held on ForceCoders, a large competitive programming platform. The authors have prepared \(n\) problems; and since the platform is very popular, \(998244351\) coder from all over the world is going to solve them.For each problem, the authors estimated the number of people who would solve ...
The first line contains one integer \(n\) (\(2 \le n \le 50\)) — the number of problems in the contest.Then \(n\) lines follow, the \(i\)-th line contains two integers \(l_i\) and \(r_i\) (\(0 \le l_i \le r_i \le 998244351\)) — the minimum and maximum number of accepted solutions for the \(i\)-th problem, respectively.
The probability that there will be no inversions in the contest can be expressed as an irreducible fraction \(\frac{x}{y}\), where \(y\) is coprime with \(998244353\). Print one integer — the value of \(xy^{-1}\), taken modulo \(998244353\), where \(y^{-1}\) is an integer such that \(yy^{-1} \equiv 1\) \((mod\) \(99824...
The real answer in the first test is \(\frac{1}{2}\).
Input: 3 1 2 1 2 1 2 | Output: 499122177
Master
3
1,150
320
328
12
1,085
B
1085B
B. Div Times Mod
1,100
math
Vasya likes to solve equations. Today he wants to solve \((x~\mathrm{div}~k) \cdot (x \bmod k) = n\), where \(\mathrm{div}\) and \(\mathrm{mod}\) stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, \(k\) and \(n\) are positive integer parameters, and \(x\)...
The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 10^6\), \(2 \leq k \leq 1000\)).
Print a single integer \(x\) — the smallest positive integer solution to \((x~\mathrm{div}~k) \cdot (x \bmod k) = n\). It is guaranteed that this equation has at least one positive integer solution.
The result of integer division \(a~\mathrm{div}~b\) is equal to the largest integer \(c\) such that \(b \cdot c \leq a\). \(a\) modulo \(b\) (shortened \(a \bmod b\)) is the only integer \(c\) such that \(0 \leq c < b\), and \(a - c\) is divisible by \(b\).In the first sample, \(11~\mathrm{div}~3 = 3\) and \(11 \bmod 3...
Input: 6 3 | Output: 11
Easy
1
450
102
198
10
730
G
730G
G. Car Repair Shop
1,600
implementation
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time.Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order t...
The first line contains integer n (1 ≤ n ≤ 200) — the number of requests from clients.The following n lines contain requests, one request per line. The i-th request is given as the pair of integers si, di (1 ≤ si ≤ 109, 1 ≤ di ≤ 5·106), where si is the preferred time to start repairing the i-th car, di is the number of...
Print n lines. The i-th line should contain two integers — the start day to repair the i-th car and the finish day to repair the i-th car.
Input: 39 27 32 4 | Output: 9 101 34 7
Medium
1
1,427
423
138
7
584
C
584C
C. Marina and Vasya
1,700
constructive algorithms; greedy; strings
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b ...
The first line contains two integers n and t (1 ≤ n ≤ 105, 0 ≤ t ≤ n).The second line contains string s1 of length n, consisting of lowercase English letters.The third line contain string s2 of length n, consisting of lowercase English letters.
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
Input: 3 2abcxyc | Output: ayd
Medium
3
468
244
193
5
900
E
900E
E. Maximum Questions
2,100
data structures; dp; strings
Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form ""abab..."", namely there are letters 'a' on odd positions and letters 'b' on even positions.Suddenly in the morning, Vasya found that somebody spoiled his string...
The first line contains a single integer n (1 ≤ n ≤ 105) — the length of s.The second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only.The third line contains a single integer m (1 ≤ m ≤ 105) — the length of t. The string t contains letters 'a' on odd positions ...
Print the only integer — the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible.
In the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'.In the second sample using two replacements we can make string equal to ""aba?aba??"". It is impossible to get more than two occurrences.
Input: 5bb?a?1 | Output: 2
Hard
3
999
346
133
9
1,739
E
1739E
E. Cleaning Robot
2,400
bitmasks; dp
Consider a hallway, which can be represented as the matrix with \(2\) rows and \(n\) columns. Let's denote the cell on the intersection of the \(i\)-th row and the \(j\)-th column as \((i, j)\). The distance between the cells \((i_1, j_1)\) and \((i_2, j_2)\) is \(|i_1 - i_2| + |j_1 - j_2|\).There is a cleaning robot i...
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the number of columns in the hallway.Then two lines follow, denoting the \(1\)-st and the \(2\)-nd row of the hallway. These lines contain \(n\) characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the r...
Print one integer — the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
In the first example, you can clean the cell \((1, 2)\), so the path of the robot is \((1, 1) \rightarrow (2, 1) \rightarrow (2, 2)\).In the second example, you can leave the hallway as it is, so the path of the robot is \((1, 1) \rightarrow (1, 2) \rightarrow (2, 2)\).In the third example, you can clean the cell \((1,...
Input: 2 01 11 | Output: 2
Expert
2
1,538
345
136
17
333
E
333E
E. Summer Earnings
2,500
binary search; bitmasks; brute force; geometry; sortings
Many schoolchildren look for a job for the summer, and one day, when Gerald was still a schoolboy, he also decided to work in the summer. But as Gerald was quite an unusual schoolboy, he found quite unusual work. A certain Company agreed to pay him a certain sum of money if he draws them three identical circles on a pl...
The first line contains a single integer n — the number of centers (3 ≤ n ≤ 3000). The following n lines each contain two integers xi, yi ( - 104 ≤ xi, yi ≤ 104) — the coordinates of potential circle centers, provided by the Company.All given points are distinct.
Print a single real number — maximum possible radius of circles. The answer will be accepted if its relative or absolute error doesn't exceed 10 - 6.
Input: 30 11 01 1 | Output: 0.50000000000000000000
Expert
5
692
263
149
3
449
A
449A
A. Jzzhu and Chocolate
1,700
greedy; math
Jzzhu has a big rectangular chocolate bar that consists of n × m unit squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements: each cut should be straight (horizontal or vertical); each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate squa...
A single line contains three integers n, m, k (1 ≤ n, m ≤ 109; 1 ≤ k ≤ 2·109).
Output a single integer representing the answer. If it is impossible to cut the big chocolate k times, print -1.
In the first sample, Jzzhu can cut the chocolate following the picture below: In the second sample the optimal division looks like this: In the third sample, it's impossible to cut a 2 × 3 chocolate 4 times.
Input: 3 4 1 | Output: 6
Medium
2
836
78
112
4
1,677
E
1677E
E. Tokitsukaze and Beautiful Subsegments
2,900
data structures
Tokitsukaze has a permutation \(p\) of length \(n\).Let's call a segment \([l,r]\) beautiful if there exist \(i\) and \(j\) satisfying \(p_i \cdot p_j = \max\{p_l, p_{l+1}, \ldots, p_r \}\), where \(l \leq i < j \leq r\).Now Tokitsukaze has \(q\) queries, in the \(i\)-th query she wants to know how many beautiful subse...
The first line contains two integers \(n\) and \(q\) (\(1\leq n \leq 2 \cdot 10^5\); \(1 \leq q \leq 10^6\)) — the length of permutation \(p\) and the number of queries.The second line contains \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) — the permutation \(p\).Each of the next \(q\) lines...
For each query, print one integer — the numbers of beautiful subsegments in the segment \([l_i,r_i]\).
In the first example, for the first query, there are \(2\) beautiful subsegments — \([1,2]\) and \([1,3]\).
Input: 8 3 1 3 5 2 4 7 6 8 1 3 1 1 1 8 | Output: 2 0 10
Master
1
415
438
102
16
1,194
C
1194C
C. From S To T
1,300
implementation; strings
You are given three strings \(s\), \(t\) and \(p\) consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose any character from \(p\), erase it from \(p\) and insert it into string \(s\) (you may insert this character anywhere you wan...
The first line contains one integer \(q\) (\(1 \le q \le 100\)) — the number of queries. Each query is represented by three consecutive lines.The first line of each query contains the string \(s\) (\(1 \le |s| \le 100\)) consisting of lowercase Latin letters.The second line of each query contains the string \(t\) (\(1 ...
For each query print YES if it is possible to make \(s\) equal to \(t\), and NO otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
In the first test case there is the following sequence of operation: \(s = \) ab, \(t = \) acxb, \(p = \) cax; \(s = \) acb, \(t = \) acxb, \(p = \) ax; \(s = \) acxb, \(t = \) acxb, \(p = \) a. In the second test case there is the following sequence of operation: \(s = \) a, \(t = \) aaaa, \(p = \) aaabbcc; \(s = \) a...
Input: 4 ab acxb cax a aaaa aaabbcc a aaaa aabbcc ab baaa aaaaa | Output: YES YES NO NO
Easy
2
1,188
494
233
11
2,122
C
2122C
C. Manhattan Pairs
1,700
constructive algorithms; geometry; greedy; math; sortings
You are given \(n\) points \((x_i, y_i)\) on a 2D plane, where \(n\) is even. Select \(\tfrac{n}{2}\) disjoint pairs \((a_i, b_i)\) to maximize the sum of Manhattan distances between points in pairs. In other words, maximize $$$\(\sum_{i=1}^{n/2} |x_{a_i} - x_{b_i}| + |y_{a_i} - y_{b_i}|.\)$$$
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single even integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) — the number of points.The \(i\)-th of the next \(n\) l...
For each test case, output \(\tfrac{n}{2}\) lines, the \(i\)-th line containing two integers \(a_i\) and \(b_i\) — the indices of points in the \(i\)-th pair.If there are multiple solutions, print any of them.
In the first test case, an optimal solution is to select the pairs \((1, 4)\) and \((2, 3)\), which achieves a distance sum of \(5 + 3 = 8\).In the second test case, an optimal solution is to select the pairs \((1, 8)\), \((9, 10)\), \((5, 7)\), \((2, 3)\), \((4, 6)\), which achieves a distance sum of \(4 + 7 + 10 + 5 ...
Input: 241 13 04 23 410-1 -1-1 2-2 -2-2 00 22 -3-4 -4-4 -20 1-4 -2 | Output: 4 1 2 3 8 1 9 10 7 5 2 3 6 4
Medium
5
294
533
209
21
342
D
342D
D. Xenia and Dominoes
2,100
bitmasks; dfs and similar; dp
Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles. A puzzle is a 3 × n table with forbidden cells (black squares) containing dominoes (colored rectangles on the picture). A puzzle is called correct if it meets the follo...
The first line contains integer n (3 ≤ n ≤ 104) — the puzzle's size. Each of the following three lines contains n characters — the description of the table. The j-th character of the i-th line equals ""X"" if the corresponding cell is forbidden; it equals ""."", if the corresponding cell is non-forbidden and ""O"", if ...
Print a single number — the answer to the problem modulo 1000000007 (109 + 7).
Two puzzles are considered distinct if there is a pair of cells that contain one domino in one puzzle and do not contain it in the other one.
Input: 5....X.O......X. | Output: 1
Hard
3
1,467
564
78
3
332
D
332D
D. Theft of Blueprints
2,400
graphs; math
Insurgents accidentally got hold of the plan of a top secret research polygon created on a distant planet for the needs of the Galaxy Empire. The insurgents suppose that this polygon is developing new deadly weapon. The polygon consists of n missile silos connected by bidirectional underground passages. The passages ar...
The first line contains two integers n and k (2 ≤ n ≤ 2000, 1 ≤ k ≤ n - 1) — the number of silos and the number of scout groups, correspondingly. The next n - 1 lines describe the polygon plan: the i-th of these lines contains n - i integers ci, i + 1, ci, i + 2, ..., ci, n — the number of droids that patrol the corres...
Print the average danger of the scouting operation, rounded down to an integer. Note that at the given limits the answer to the problem always fits into the standard integer 64-bit data type.Please do not use the %lld specifier to write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d specifi...
In the first sample there are 6 one-element sets of silos. For sets {1}, {5} the operation danger will equal 8, for sets {3}, {6} — 3, for sets {2}, {4} — 5. The mathematical average equals .In the second sample there are 3 two-elements sets of silos: {1, 3} (danger equals 21), {1, 2} (danger equals 11), {2, 3} (danger...
Input: 6 1-1 -1 -1 8 -1-1 5 -1 -1-1 -1 3-1 -1-1 | Output: 5
Expert
2
1,738
626
323
3
1,980
C
1980C
C. Sofia and the Lost Operations
1,300
constructive algorithms; greedy
Sofia had an array of \(n\) integers \(a_1, a_2, \ldots, a_n\). One day she got bored with it, so she decided to sequentially apply \(m\) modification operations to it.Each modification operation is described by a pair of numbers \(\langle c_j, d_j \rangle\) and means that the element of the array with index \(c_j\) sh...
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2...
Output \(t\) lines, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if there exists a suitable sequence \(c_1, c_2, \ldots, c_m\), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a p...
Input: 731 2 11 3 241 3 1 241 2 3 52 1 3 522 357 6 1 10 103 6 1 11 1134 3 1143 1 7 82 2 7 10510 3 2 2 155 7 1 7 94 10 1 2 981 1 9 8 7 2 10 441000000000 203 203 203203 1000000000 203 10000000002203 100000000011151 3 4 5 1 | Output: YES NO NO NO YES NO YES
Easy
2
1,076
996
336
19
1,969
C
1969C
C. Minimizing the Sum
1,700
dp; implementation
You are given an integer array \(a\) of length \(n\).You can perform the following operation: choose an element of the array and replace it with any of its neighbor's value.For example, if \(a=[3, 1, 2]\), you can get one of the arrays \([3, 3, 2]\), \([3, 2, 2]\) and \([1, 1, 2]\) using one operation, but not \([2, 1,...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5\); \(0 \le k \le 10\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).Additional ...
For each test case, print a single integer — the minimum possible total sum of the array if you can perform the aforementioned operation at most \(k\) times.
In the first example, one of the possible sequences of operations is the following: \([3, 1, 2] \rightarrow [1, 1, 2\)].In the second example, you do not need to apply the operation.In the third example, one of the possible sequences of operations is the following: \([2, 2, 1, 3] \rightarrow [2, 1, 1, 3] \rightarrow [2...
Input: 43 13 1 21 354 22 2 1 36 34 1 2 2 4 3 | Output: 4 5 5 10
Medium
2
482
414
157
19
266
D
266D
D. BerDonalds
2,400
graphs; math; shortest paths
BerDonalds, a well-known fast food restaurant, is going to open a cafe in Bertown. The important thing is to choose the new restaurant's location so that it would be easy to get there. The Bertown road system is represented by n junctions, connected by m bidirectional roads. For each road we know its length. We also kn...
The first line contains two integers n and m () — the number of junctions and the number of roads, correspondingly. Then m lines follow, describing all Bertown roads. Each road is described by three integers ai, bi, wi (1 ≤ ai, bi ≤ n, ai ≠ bi; 1 ≤ wi ≤ 105), where ai and bi are the numbers of the junctions, connected ...
Print a single real number — the shortest distance from the optimal restaurant location to the farthest junction. The answer will be considered correct, if its absolute or relative error doesn't exceed 10 - 9.
Input: 2 11 2 1 | Output: 0.50
Expert
3
647
546
209
2
1,842
C
1842C
C. Tenzing and Balls
1,500
dp
Enjoy erasing Tenzing, identified as Accepted!Tenzing has \(n\) balls arranged in a line. The color of the \(i\)-th ball from the left is \(a_i\).Tenzing can do the following operation any number of times: select \(i\) and \(j\) such that \(1\leq i < j \leq |a|\) and \(a_i=a_j\), remove \(a_i,a_{i+1},\ldots,a_j\) from ...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1\leq t\leq 10^3\)) — the number of test cases. The description of test cases follows.The first line contains a single integer \(n\) (\(1\leq n\leq 2\cdot 10^5\)) — the number of balls.The second line contains \(n\) integ...
For each test case, output the maximum number of balls Tenzing can remove.
In the first example, Tenzing will choose \(i=2\) and \(j=3\) in the first operation so that \(a=[1,3,3]\). Then Tenzing will choose \(i=2\) and \(j=3\) again in the second operation so that \(a=[1]\). So Tenzing can remove \(4\) balls in total.In the second example, Tenzing will choose \(i=1\) and \(j=3\) in the first...
Input: 251 2 2 3 341 2 1 2 | Output: 4 3
Medium
1
475
480
74
18
1,473
G
1473G
G. Tiles
2,800
combinatorics; dp; fft; math
Consider a road consisting of several rows. Each row is divided into several rectangular tiles, and all tiles in the same row are equal. The first row contains exactly one rectangular tile. Look at the picture below which shows how the tiles are arranged.The road is constructed as follows: the first row consists of \(1...
The first line contains one integer \(n\) (\(1 \le n \le 1000\)).Then \(n\) lines follow. The \(i\)-th of them contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^5\); \(|a_i - b_i| \le 5\)).Additional constraint on the input: the sequence of \(a_i\) and \(b_i\) never results in a row with non-positive n...
Print one integer — the number of paths from the first row to the last row, taken modulo \(998244353\).
Input: 2 4 2 2 3 | Output: 850
Master
4
1,350
335
103
14
178
F1
178F1
F1. Representative Sampling
1,800
The Smart Beaver from ABBYY has a long history of cooperating with the ""Institute of Cytology and Genetics"". Recently, the Institute staff challenged the Beaver with a new problem. The problem is as follows.There is a collection of n proteins (not necessarily distinct). Each protein is a string consisting of lowercas...
The first input line contains two integers n and k (1 ≤ k ≤ n), separated by a single space. The following n lines contain the descriptions of proteins, one per line. Each protein is a non-empty string of no more than 500 characters consisting of only lowercase Latin letters (a...z). Some of the strings may be equal.Th...
Print a single number denoting the largest possible value of representativity that a subcollection of size k of the given collection of proteins can have.
Input: 3 2ababzdabq | Output: 2
Medium
0
1,476
501
154
1
94
A
94A
A. Restoring Password
900
implementation; strings
Igor K. always used to trust his favorite Kashpirovsky Antivirus. That is why he didn't hesitate to download the link one of his groupmates sent him via QIP Infinium. The link was said to contain ""some real funny stuff about swine influenza"". The antivirus had no objections and Igor K. run the flash application he ha...
The input data contains 11 lines. The first line represents the binary code 80 characters in length. That is the code written in Igor K.'s ISQ account's info. Next 10 lines contain pairwise distinct binary codes 10 characters in length, corresponding to numbers 0, 1, ..., 9.
Print one line containing 8 characters — The password to Igor K.'s ISQ account. It is guaranteed that the solution exists.
Input: 010011001001011000000101100010010110010001011001100101101000010110101001011011000100110000010011001001011000000101100010010110010001011001100101101000010110101001011011000101101110 | Output: 12345678
Beginner
2
1,218
275
122
0
95
A
95A
A. Hockey
1,600
implementation; strings
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1, s2, ..., sn. All those strings consist of uppercase and lowercase Latin ...
The first line contains the only integer n (1 ≤ n ≤ 100) — the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The l...
Output the only line — Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically.The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller t...
Input: 3bersuckyeluPetrLoveLuckyNumberst | Output: PetrLovtTttttNumtttt
Medium
2
1,708
364
487
0
618
A
618A
A. Slime Combining
800
implementation
Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1.You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n - 1 slimes one by one. When you add a slime, you place it at the right of all alr...
The first line of the input will contain a single integer, n (1 ≤ n ≤ 100 000).
Output a single line with k integers, where k is the number of slimes in the row after you've finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left.
In the first sample, we only have a single slime with value 1. The final state of the board is just a single slime with value 1.In the second sample, we perform the following steps:Initially we place a single slime in a row by itself. Thus, row is initially 1.Then, we will add another slime. The row is now 1 1. Since t...
Input: 1 | Output: 1
Beginner
1
625
79
231
6
1,116
D2
1116D2
D2. Pattern of increasing blocks
0
*special
Implement a unitary operation on \(N\) qubits which is represented by a square matrix of size \(2^N\) defined as follows: top right and bottom left quarters are filled with zero elements, the top left quarter is the same pattern of size \(2^{N-1}\) (for \(N = 1\), the top left quarter is a non-zero element), the bottom...
Beginner
1
2,032
0
0
11
886
B
886B
B. Vlad and Cafes
1,000
Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he ...
In first line there is one integer n (1 ≤ n ≤ 2·105) — number of cafes indices written by Vlad.In second line, n numbers a1, a2, ..., an (0 ≤ ai ≤ 2·105) are written — indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted.
Print one integer — index of the cafe that Vlad hasn't visited for as long as possible.
In first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer. In second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4 were visited after the last visit of cafe with inde...
Input: 51 3 2 1 2 | Output: 3
Beginner
0
606
319
87
8
518
B
518B
B. Tanya and Postcard
1,400
greedy; implementation; strings
Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — string s of length n, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string s. The...
The first line contains line s (1 ≤ |s| ≤ 2·105), consisting of uppercase and lowercase English letters — the text of Tanya's message.The second line contains line t (|s| ≤ |t| ≤ 2·105), consisting of uppercase and lowercase English letters — the text written in the newspaper.Here |a| means the length of the string a.
Print two integers separated by a space: the first number is the number of times Tanya shouts ""YAY!"" while making the message, the second number is the number of times Tanya says ""WHOOPS"" while making the message.
Input: AbCDCbA | Output: 3 0
Easy
3
1,295
319
217
5
1,866
H
1866H
H. Happy Sets
2,100
combinatorics
Define a set \(A\) as a child of set \(B\) if and only if for each element of value \(x\) that is in \(A\), there exists an element of value \(x+1\) that is in \(B\).Given integers \(N\) and \(K\). In order to make Chaneka happy, you must find the number of different arrays containing \(N\) sets \([S_1, S_2, S_3, \ldot...
The only line contains two integers \(N\) and \(K\) (\(1 \leq N,K \leq 2\cdot10^5\)) — the number of sets in the array and the maximum limit for the values in the sets.
An integer representing the number of different arrays of sets that satisfy the problem condition, modulo \(998\,244\,353\).
In the first example, there are \(11\) different arrays of sets possible, which are: \([\{\}, \{\}]\) \([\{\}, \{1\}]\) \([\{\}, \{1, 2\}]\) \([\{\}, \{2\}]\) \([\{1\}, \{\}]\) \([\{1\}, \{1, 2\}]\) \([\{1\}, \{2\}]\) \([\{1, 2\}, \{\}]\) \([\{1, 2\}, \{1\}]\) \([\{2\}, \{\}]\) \([\{2\}, \{1\}]\)
Input: 2 2 | Output: 11
Hard
1
922
168
124
18
1,822
D
1822D
D. Super-Permutation
1,200
constructive algorithms; math
A permutation is a sequence \(n\) integers, where each integer from \(1\) to \(n\) appears exactly once. For example, \([1]\), \([3,5,2,1,4]\), \([1,3,2]\) are permutations, while \([2,3,2]\), \([4,3,1]\), \([0]\) are not.Given a permutation \(a\), we construct an array \(b\), where \(b_i = (a_1 + a_2 +~\dots~+ a_i) \b...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of the test cases follows.Each test case consists of a single line containing one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) — the length of the desired permutation.The sum of \(n\) over all test cases do...
For each test case, output in a separate line: \(n\) integers — a super-permutation of length \(n\), if it exists. \(-1\), otherwise. If there are several suitable permutations, output any of them.
Input: 41236 | Output: 1 2 1 -1 6 5 2 3 4 1
Easy
2
693
351
197
18
1,716
E
1716E
E. Swap and Maximum Block
2,500
bitmasks; data structures; dfs and similar; divide and conquer; dp
You are given an array of length \(2^n\). The elements of the array are numbered from \(1\) to \(2^n\).You have to process \(q\) queries to this array. In the \(i\)-th query, you will be given an integer \(k\) (\(0 \le k \le n-1\)). To process the query, you should do the following: for every \(i \in [1, 2^n-2^k]\) in ...
The first line contains one integer \(n\) (\(1 \le n \le 18\)).The second line contains \(2^n\) integers \(a_1, a_2, \dots, a_{2^n}\) (\(-10^9 \le a_i \le 10^9\)).The third line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)).Then \(q\) lines follow, the \(i\)-th of them contains one integer \(k\) (\(0 \le k ...
For each query, print one integer — the maximum sum over all contiguous subsegments of the array (including the empty subsegment) after processing the query.
Transformation of the array in the example: \([-3, 5, -3, 2, 8, -20, 6, -1] \rightarrow [-3, 2, -3, 5, 6, -1, 8, -20] \rightarrow [2, -3, 5, -3, -1, 6, -20, 8] \rightarrow [5, -3, 2, -3, -20, 8, -1, 6]\).
Input: 3 -3 5 -3 2 8 -20 6 -1 3 1 0 1 | Output: 18 8 13
Expert
5
1,481
361
157
17
138
D
138D
D. World of Darkraft
2,500
dp; games
Recently Roma has become the happy owner of a new game World of Darkraft. This game combines elements of virtually all known genres, and on one of the later stages of the game Roma faced difficulties solving a puzzle.In this part Roma fights with a cunning enemy magician. The battle takes place on a rectangular field p...
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 20).Next n lines contain m characters describing the playing field: the j-th character of the i-th line equals to the magical character of the corresponding field square.
On the first line print ""WIN"" if Roma can win or ""LOSE"" if it is impossible to win considering that the opponent pays optimally.
In the first test each move makes one diagonal line of the square inactive, thus it is guaranteed that Roma loses after two moves.There are three variants of making a move in the second test: to ""finish off"" the main diagonal line or any of the squares that are left. That means that after three moves the game stops a...
Input: 2 2RLLR | Output: LOSE
Expert
2
1,426
243
132
1
1,917
B
1917B
B. Erase First or Second Letter
1,100
brute force; combinatorics; data structures; dp; strings
You are given a string \(s\) of length \(n\). Let's define two operations you can apply on the string: remove the first character of the string; remove the second character of the string. Your task is to find the number of distinct non-empty strings that can be generated by applying the given operations on the initial ...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) — the number of test cases. The description of the test cases follows.The first line of each test case contains \(n\) (\(1 \leq n \leq 10^5\)) — the length of the string.The second line of each test case c...
For each test case, output a single integer: the number of distinct non-empty strings you can get.
In the first test case, we can get the following strings: \(a\), \(aa\), \(aaa\), \(aaaa\), \(aaaaa\).In the third test case, for example, the word \(ba\) can be reached in the following way: remove the first character of the current string \(ababa\), getting \(baba\); remove the second character of the current string ...
Input: 55aaaaa1z5ababa14bcdaaaabcdaaaa20abcdefghijklmnopqrst | Output: 5 1 9 50 210
Easy
5
377
527
98
19
1,796
D
1796D
D. Maximum Subarray
2,000
data structures; dp; greedy; two pointers
You are given an array \(a_1, a_2, \dots, a_n\), consisting of \(n\) integers. You are also given two integers \(k\) and \(x\).You have to perform the following operation exactly once: add \(x\) to the elements on exactly \(k\) distinct positions, and subtract \(x\) from all the others.For example, if \(a = [2, -1, 2, ...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases.The first line of each test case contains three integers \(n\), \(k\) and \(x\) (\(1 \le n \le 2 \cdot 10^5\); \(0 \le k \le \min(20, n)\); \(-10^9 \le x \le 10^9\)).The second line contains \(n\) integers \(a_1, a_2, \dots...
For each test case, print one integer — the maximum possible value of \(f(a')\).
Input: 44 1 22 -1 2 32 2 3-1 23 0 53 2 46 2 -84 -1 9 -3 7 -8 | Output: 5 7 0 44
Hard
4
923
427
80
17
1,527
B2
1527B2
B2. Palindrome Game (hard version)
1,900
constructive algorithms; games
The only difference between the easy and hard versions is that the given string \(s\) in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, ""101101"" is a palindrome, while ""0101"...
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)). Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^3\)).The second line of each test case contains the string \(s\) of length \(n\), consisting of the characters '0' and '1'. It is guarant...
For each test case print a single word in a new line: ""ALICE"", if Alice will win the game, ""BOB"", if Bob will win the game, ""DRAW"", if the game ends in a draw.
In the first test case of example, in the \(1\)-st move, Alice will use the \(2\)-nd operation to reverse the string, since doing the \(1\)-st operation will result in her loss anyway. This also forces Bob to use the \(1\)-st operation. in the \(2\)-nd move, Bob has to perform the \(1\)-st operation, since the \(2\)-nd...
Input: 3 3 110 2 00 4 1010 | Output: ALICE BOB ALICE
Hard
2
1,312
436
165
15
109
E
109E
E. Lucky Interval
2,700
brute force; math
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.One day Petya came across an interval of numbers [a, a + l - 1]. Let F(x) be the number of lucky digit...
The single line contains two integers a and l (1 ≤ a, l ≤ 109) — the interval's first number and the interval's length correspondingly.
On the single line print number b — the answer to the problem.
Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is,
Input: 7 4 | Output: 17
Master
2
440
135
62
1
1,001
C
1001C
C. Generate GHZ state
1,400
*special
You are given N qubits (1 ≤ N ≤ 8) in zero state . Your task is to create Greenberger–Horne–Zeilinger (GHZ) state on them:Note that for N = 1 and N = 2 GHZ state becomes states and from the previous tasks, respectively.
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:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation S...
Easy
1
219
376
0
10
1,360
H
1360H
H. Binary Median
2,100
binary search; bitmasks; brute force; constructive algorithms
Consider all binary strings of length \(m\) (\(1 \le m \le 60\)). A binary string is a string that consists of the characters 0 and 1 only. For example, 0110 is a binary string, and 012aba is not. Obviously, there are exactly \(2^m\) such strings in total.The string \(s\) is lexicographically smaller than the string \(...
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) — the number of test cases. Then, \(t\) test cases follow.The first line of each test case contains integers \(n\) (\(1 \le n \le \min(2^m-1, 100)\)) and \(m\) (\(1 \le m \le 60\)), where \(n\) is the number of strings to remove, and \(m\) is the length of...
Print \(t\) answers to the test cases. For each test case, print a string of length \(m\) — the median of the sorted sequence of remaining strings in the corresponding test case.
The first test case is explained in the statement.In the second test case, the result after removing strings and sorting is \([\)001, 010, 101, 110\(]\). Therefore, the desired median is 010.
Input: 5 3 3 010 001 111 4 3 000 111 100 011 1 1 1 1 1 0 3 2 00 01 10 | Output: 100 010 0 1 11
Hard
4
1,541
534
178
13
977
E
977E
E. Cyclic Components
1,500
dfs and similar; dsu; graphs
You are given an undirected graph consisting of \(n\) vertices and \(m\) edges. Your task is to find the number of connected components which are cycles.Here are some definitions of graph theory.An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of verti...
The first line contains two integer numbers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^5\)) — number of vertices and edges.The following \(m\) lines contains edges: edge \(i\) is given as a pair of vertices \(v_i\), \(u_i\) (\(1 \le v_i, u_i \le n\), \(u_i \ne v_i\)). There is no multiple e...
Print one integer — the number of connected components which are also cycles.
In the first example only component \([3, 4, 5]\) is also a cycle.The illustration above corresponds to the second example.
Input: 5 41 23 45 43 5 | Output: 1
Medium
3
1,278
455
77
9
1,343
B
1343B
B. Balanced Array
800
constructive algorithms; math
You are given a positive integer \(n\), it is guaranteed that \(n\) is even (i.e. divisible by \(2\)).You want to construct the array \(a\) of length \(n\) such that: The first \(\frac{n}{2}\) elements of \(a\) are even (divisible by \(2\)); the second \(\frac{n}{2}\) elements of \(a\) are odd (not divisible by \(2\));...
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. Then \(t\) test cases follow.The only line of the test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) — the length of the array. It is guaranteed that that \(n\) is even (i.e. divisible by \(2\)).It...
For each test case, print the answer — ""NO"" (without quotes), if there is no suitable answer for the given test case or ""YES"" in the first line and any suitable array \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) satisfying conditions from the problem statement on the second line.
Input: 5 2 4 6 8 10 | Output: NO YES 2 4 1 5 NO YES 2 4 6 8 1 3 5 11 NO
Beginner
2
660
440
289
13
332
C
332C
C. Students' Revenge
2,200
data structures; greedy; sortings
A student's life is fraught with complications. Some Berland University students know this only too well. Having studied for two years, they contracted strong antipathy towards the chairperson of some department. Indeed, the person in question wasn't the kindest of ladies to begin with: prone to reforming groups, banni...
The first line contains three integers n (1 ≤ n ≤ 105), p (1 ≤ p ≤ n), k (1 ≤ k ≤ p) — the number of orders the directors are going to discuss, the number of orders to pass and the number of orders to be obeyed by the chairperson, correspondingly. Each of the following n lines contains two integers ai and bi (1 ≤ ai, b...
Print in an arbitrary order p distinct integers — the numbers of the orders to accept so that the students could carry out the revenge. The orders are indexed from 1 to n in the order they occur in the input. If there are multiple solutions, you can print any of them.
In the first sample one of optimal solutions is to pass orders 1, 2, 3. In this case the chairperson obeys orders number 1 and 2. She gets 10 new grey hairs in the head and the directors' displeasement will equal 3. Note that the same result can be achieved with order 4 instead of order 3.In the second sample, the chai...
Input: 5 3 25 65 81 34 34 11 | Output: 3 1 2
Hard
3
1,425
365
268
3
1,401
A
1401A
A. Distance and Axis
900
constructive algorithms; math
We have a point \(A\) with coordinate \(x = n\) on \(OX\)-axis. We'd like to find an integer point \(B\) (also on \(OX\)-axis), such that the absolute difference between the distance from \(O\) to \(B\) and the distance from \(A\) to \(B\) is equal to \(k\). The description of the first test case. Since sometimes it's ...
The first line contains one integer \(t\) (\(1 \le t \le 6000\)) — the number of test cases.The only line of each test case contains two integers \(n\) and \(k\) (\(0 \le n, k \le 10^6\)) — the initial position of point \(A\) and desirable absolute difference.
For each test case, print the minimum number of steps to make point \(B\) exist.
In the first test case (picture above), if we set the coordinate of \(B\) as \(2\) then the absolute difference will be equal to \(|(2 - 0) - (4 - 2)| = 0\) and we don't have to move \(A\). So the answer is \(0\).In the second test case, we can increase the coordinate of \(A\) by \(3\) and set the coordinate of \(B\) a...
Input: 6 4 0 5 8 0 1000000 0 0 1 0 1000000 1000000 | Output: 0 3 1000000 0 1 0
Beginner
2
513
260
80
14