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
1,952
H
1952H
H. Palindrome
0
*special; implementation; strings
A palindrome is a string that reads the same in both directions, for example z, uwu, or moom.
The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of testcases.The following \(t\) lines each contain a string of length at most \(100\) consisting of lowercase English letters.
For each test case, output ""YES"" or ""NO"", denoting the answer.
Input: 8actleradarracecarphpatcodercodeforcessteam | Output: NO NO YES YES NO NO YES YES
Beginner
3
93
205
66
19
662
D
662D
D. International Olympiad
2,000
constructive algorithms; greedy; implementation; math
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Amo...
The first line of the input contains a single integer n (1 ≀ n ≀ 1000) β€” the number of abbreviations to process. Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
For each abbreviation given in the input, find the year of the corresponding Olympiad.
Input: 5IAO'15IAO'2015IAO'1IAO'9IAO'0 | Output: 201512015199119891990
Hard
4
770
241
86
6
1,407
B
1407B
B. Big Vova
1,300
brute force; greedy; math; number theory
Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace ""Zmey-Gorynych"", and offers Alex a job: if he shows his p...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^3\)) β€” the length of the sequence \(a\).The second line of each test case contai...
For each test case output the answer in a single line β€” the desired sequence \(b\). If there are multiple answers, print any.
In the first test case of the example, there are only two possible permutations \(b\) β€” \([2, 5]\) and \([5, 2]\): for the first one \(c=[2, 1]\), for the second one \(c=[5, 1]\).In the third test case of the example, number \(9\) should be the first in \(b\), and \(GCD(9, 3)=3\), \(GCD(9, 8)=1\), so the second number ...
Input: 7 2 2 5 4 1 8 2 3 3 3 8 9 5 64 25 75 100 50 1 42 6 96 128 88 80 52 7 5 2 4 8 16 17 | Output: 5 2 8 2 1 3 9 3 8 100 50 25 75 64 42 128 96 80 88 52 7 17 2 4 8 16
Easy
4
1,181
486
125
14
81
C
81C
C. Average Score
1,700
greedy; math; sortings
After the educational reform Polycarp studies only two subjects at school, Safety Studies and PE (Physical Education). During the long months of the fourth term, he received n marks in them. When teachers wrote a mark in the journal, they didn't write in what subject the mark was for, they just wrote the mark.Now it's ...
The first line contains an integer n (2 ≀ n ≀ 105), n is the number of marks in Polycarp's Journal. The second line contains two positive integers a, b (1 ≀ a, b ≀ n - 1, a + b = n). The third line contains a sequence of integers t1, t2, ..., tn (1 ≀ ti ≀ 5), they are Polycarp's marks.
Print the sequence of integers f1, f2, ..., fn, where fi (1 ≀ fi ≀ 2) is the number of a subject to which the i-th mark should be attributed. If there are several possible solutions, then print such that the sequence f1, f2, ..., fn is the smallest lexicographically.The sequence p1, p2, ..., pn is lexicographically les...
In the first sample the average score in the first subject is equal to 4, and in the second one β€” to 4.5. The total average score is 8.5.
Input: 53 24 4 5 4 4 | Output: 1 1 2 1 2
Medium
3
1,054
286
422
0
860
E
860E
E. Arkady and a Nobody-men
2,700
data structures; dfs and similar; trees
Arkady words in a large company. There are n employees working in a system of a strict hierarchy. Namely, each employee, with an exception of the CEO, has exactly one immediate manager. The CEO is a manager (through a chain of immediate managers) of all employees.Each employee has an integer rank. The CEO has rank equa...
The first line contains single integer n (1 ≀ n ≀ 5Β·105) β€” the number of employees in the company.The second line contains n integers p1, p2, ..., pn (0 ≀ pi ≀ n), where pi = 0 if the i-th employee is the CEO, otherwise pi equals the id of the immediate manager of the employee with id i. The employees are numbered from...
Print n integers β€” the negligibilities of all employees in the order of their ids: z1, z2, ..., zn.
Consider the first example: The CEO has no managers, thus z1 = 0. r(2, 1) = 2 (employees 2 and 4 suit the conditions, employee 3 has too large rank). Thus z2 = r(2, 1) = 2. Similarly, z4 = r(4, 1) = 2. r(3, 2) = 1 (employee 3 is a subordinate of 2 and has suitable rank). r(3, 1) = 3 (employees 2, 3, 4 suit the conditio...
Input: 40 1 2 1 | Output: 0 2 4 2
Master
3
1,286
485
99
8
1,758
B
1758B
B. XOR = Average
900
constructive algorithms
You are given an integer \(n\). Find a sequence of \(n\) integers \(a_1, a_2, \dots, a_n\) such that \(1 \leq a_i \leq 10^9\) for all \(i\) and $$$\(a_1 \oplus a_2 \oplus \dots \oplus a_n = \frac{a_1 + a_2 + \dots + a_n}{n},\)\( where \)\oplus$$$ represents the bitwise XOR.It can be proven that there exists a sequence ...
The first line of input contains \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first and only line of each test case contains one integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the length of the sequence you have to find.The sum of \(n\) over all test cases does not exceed \(10^5\).
For each test case, output \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) satisfying the conditions in the statement. If there are several possible answers, you can output any of them.
In the first test case, \(69 = \frac{69}{1} = 69\).In the second test case, \(13 \oplus 2 \oplus 8 \oplus 1 = \frac{13 + 2 + 8 + 1}{4} = 6\).
Input: 3143 | Output: 69 13 2 8 1 7 7 7
Beginner
1
372
293
193
17
1,323
B
1323B
B. Count Subrectangles
1,500
binary search; greedy; implementation
You are given an array \(a\) of length \(n\) and array \(b\) of length \(m\) both consisting of only integers \(0\) and \(1\). Consider a matrix \(c\) of size \(n \times m\) formed by following rule: \(c_{i, j} = a_i \cdot b_j\) (i.e. \(a_i\) multiplied by \(b_j\)). It's easy to see that \(c\) consists of only zeroes a...
The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \leq n, m \leq 40\,000, 1 \leq k \leq n \cdot m\)), length of array \(a\), length of array \(b\) and required size of subrectangles.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 1\)), elements of \(a\).The third li...
Output single integer β€” the number of subrectangles of \(c\) with size (area) \(k\) consisting only of ones.
In first example matrix \(c\) is: There are \(4\) subrectangles of size \(2\) consisting of only ones in it: In second example matrix \(c\) is:
Input: 3 3 2 1 0 1 1 1 1 | Output: 4
Medium
3
895
416
108
13
1,283
A
1283A
A. Minutes Before the New Year
800
math
New Year is coming and you are excited to know how many minutes remain before the New Year. You know that currently the clock shows \(h\) hours and \(m\) minutes, where \(0 \le hh < 24\) and \(0 \le mm < 60\). We use 24-hour time format!Your task is to find the number of minutes before the New Year. You know that New Y...
The first line of the input contains one integer \(t\) (\(1 \le t \le 1439\)) β€” the number of test cases.The following \(t\) lines describe test cases. The \(i\)-th line contains the time as two integers \(h\) and \(m\) (\(0 \le h < 24\), \(0 \le m < 60\)). It is guaranteed that this time is not a midnight, i.e. the fo...
For each test case, print the answer on it β€” the number of minutes before the New Year.
Input: 5 23 55 23 0 0 1 4 20 23 59 | Output: 5 60 1439 1180 1
Beginner
1
429
470
87
12
2,125
A
2125A
A. Difficult Contest
800
constructive algorithms; implementation; sortings; strings
It is known that a contest can be represented by a string \(s\), consisting of uppercase Latin letters that denote problems. It is also known that a contest is difficult if it contains ""FFT"" or ""NTT"" as a contiguous substring.Your task is to rearrange the problem in contest \(s\) in such a way that this contest is ...
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. The description of the test cases follows.The only line of each test case contains \(s\) (\(1 \le |s| \le 2 \cdot 10^{5}\)).Additional constraints on the input data: the total le...
For each test case, output a string β€” a non-difficult contest that was obtained from \(s\) by rearranging the letters.If there are multiple correct answers, you may output any. It can be shown that at least one correct answer always exists.
Input: 5FFTABFBANTTAFFTNTTFFTFFTFFTNNTNNTAFFTBFFNTTFTTZ | Output: FTF ABFBANATT NTFTFT TFFFFFFNTNTNTNT AFTFBTTFFNFTTZ
Beginner
4
402
393
240
21
1,267
L
1267L
L. Lexicography
1,800
constructive algorithms; strings
Lucy likes letters. She studied the definition of the lexicographical order at school and plays with it.At first, she tried to construct the lexicographically smallest word out of given letters. It was so easy! Then she tried to build multiple words and minimize one of them. This was much harder!Formally, Lucy wants to...
The first line contains three integers \(n\), \(l\), and \(k\) (\(1\le k \le n \le 1\,000\); \(1 \le l \le 1\,000\)) β€” the total number of words, the length of each word, and the index of the word Lucy wants to minimize.The next line contains a string of \(n \cdot l\) lowercase letters of the English alphabet.
Output \(n\) words of \(l\) letters each, one per line, using the letters from the input. Words must be sorted in the lexicographic order, and the \(k\)-th of them must be lexicographically as small as possible. If there are multiple answers with the smallest \(k\)-th word, output any of them.
Input: 3 2 2 abcdef | Output: af bc ed
Medium
2
497
311
294
12
865
F
865F
F. Egg Roulette
3,300
bitmasks; brute force; divide and conquer; math; meet-in-the-middle
The game of Egg Roulette is played between two players. Initially 2R raw eggs and 2C cooked eggs are placed randomly into a carton. The shells are left on so there is no way to distinguish a raw egg from a cooked egg. One at a time, a player will select an egg, and then smash the egg on his/her forehead. If the egg was...
The first line of input will contain integers R and C (1 ≀ R, C ≀ 20, R + C ≀ 30).The second line of input will contain the string S of length 2(R + C) consisting only of characters 'A', 'B', '?'.
Print the number of valid orderings that minimize unfairness and match S.
In the first test case, the minimum unfairness is 0, and the orderings that achieve it are ""ABBA"" and ""BAAB"", neither of which match S. Note that an ordering such as ""ABBB"" would also have an unfairness of 0, but is invalid because it does not contain the same number of 'A's as 'B's.In the second example, the onl...
Input: 1 1??BB | Output: 0
Master
5
1,529
196
73
8
92
A
92A
A. Chips
800
implementation; math
There are n walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number 2 sits to the left of the walrus number 1, the walrus number 3 sits to the left of the walrus number 2, ..., the walrus number 1 sits to the left of the walrus number n.The presenter has m chips. The presenter s...
The first line contains two integers n and m (1 ≀ n ≀ 50, 1 ≀ m ≀ 104) β€” the number of walruses and the number of chips correspondingly.
Print the number of chips the presenter ended up with.
In the first sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, four chips to the walrus number 4, then again one chip to the walrus number 1. After that the presenter runs out of chips. He can't give anything to the walrus number 2 and the ...
Input: 4 11 | Output: 0
Beginner
2
708
136
54
0
1,650
B
1650B
B. DIV + MOD
900
math
Not so long ago, Vlad came up with an interesting function: \(f_a(x)=\left\lfloor\frac{x}{a}\right\rfloor + x \bmod a\), where \(\left\lfloor\frac{x}{a}\right\rfloor\) is \(\frac{x}{a}\), rounded down, \(x \bmod a\) β€” the remainder of the integer division of \(x\) by \(a\).For example, with \(a=3\) and \(x=11\), the va...
The first line of input data contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of input test cases.This is followed by \(t\) lines, each of which contains three integers \(l_i\), \(r_i\) and \(a_i\) (\(1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9\)) β€” the left and right boundaries of the segment and the f...
For each test case, output one number on a separate line β€” the maximum value of the function on a given segment for a given \(a\).
In the first sample: \(f_3(1) = \left\lfloor\frac{1}{3}\right\rfloor + 1 \bmod 3 = 0 + 1 = 1\), \(f_3(2) = \left\lfloor\frac{2}{3}\right\rfloor + 2 \bmod 3 = 0 + 2 = 2\), \(f_3(3) = \left\lfloor\frac{3}{3}\right\rfloor + 3 \bmod 3 = 1 + 0 = 1\), \(f_3(4) = \left\lfloor\frac{4}{3}\right\rfloor + 4 \bmod 3 = 1 + 1 = 2\) ...
Input: 51 4 35 8 46 10 61 1000000000 100000000010 12 8 | Output: 2 4 5 999999999 5
Beginner
1
579
340
130
16
1,110
B
1110B
B. Tape
1,400
greedy; sortings
You have a long stick, consisting of \(m\) segments enumerated from \(1\) to \(m\). Each segment is \(1\) centimeter long. Sadly, some segments are broken and need to be repaired.You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be pr...
The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n \le 10^5\), \(n \le m \le 10^9\), \(1 \le k \le n\)) β€” the number of broken segments, the length of the stick and the maximum number of pieces you can use.The second line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le m\)) β€” th...
Print the minimum total length of the pieces.
In the first example, you can use a piece of length \(11\) to cover the broken segments \(20\) and \(30\), and another piece of length \(6\) to cover \(75\) and \(80\), for a total length of \(17\).In the second example, you can use a piece of length \(4\) to cover broken segments \(1\), \(2\) and \(4\), and two pieces...
Input: 4 100 220 30 75 80 | Output: 17
Easy
2
704
440
45
11
385
A
385A
A. Bear and Raspberry
1,000
brute force; greedy; implementation
The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following n days. According to the bear's data, on the i-th (1 ≀ i ≀ n) day, the price for one barrel of honey is going to is xi kilos of raspberry.Unfortunately, the bea...
The first line contains two space-separated integers, n and c (2 ≀ n ≀ 100, 0 ≀ c ≀ 100), β€” the number of days and the number of kilos of raspberry that the bear should give for borrowing the barrel.The second line contains n space-separated integers x1, x2, ..., xn (0 ≀ xi ≀ 100), the price of a honey barrel on day i.
Print a single integer β€” the answer to the problem.
In the first sample the bear will lend a honey barrel at day 3 and then sell it for 7. Then the bear will buy a barrel for 3 and return it to the friend. So, the profit is (7 - 3 - 1) = 3.In the second sample bear will lend a honey barrel at day 1 and then sell it for 100. Then the bear buy the barrel for 1 at the day ...
Input: 5 15 10 7 3 20 | Output: 3
Beginner
3
1,226
320
51
3
1,266
C
1266C
C. Diverse Matrix
1,400
constructive algorithms; greedy; math; number theory
Let \(a\) be a matrix of size \(r \times c\) containing positive integers, not necessarily distinct. Rows of the matrix are numbered from \(1\) to \(r\), columns are numbered from \(1\) to \(c\). We can construct an array \(b\) consisting of \(r + c\) integers as follows: for each \(i \in [1, r]\), let \(b_i\) be the g...
The only line in the input contains two space separated integers \(r\) and \(c\) (\(1 \leq r,c \leq 500\)) β€” the number of rows and the number of columns of the matrix to be found.
If there is no solution, output a single integer \(0\).Otherwise, output \(r\) rows. The \(i\)-th of them should contain \(c\) space-separated integers, the \(j\)-th of which is \(a_{i,j}\) β€” the positive integer in the \(i\)-th row and \(j\)-th column of a diverse matrix minimizing the magnitude.Furthermore, it must h...
In the first example, the GCDs of rows are \(b_1 = 4\) and \(b_2 = 1\), and the GCDs of columns are \(b_3 = 2\) and \(b_4 = 3\). All GCDs are pairwise distinct and the maximum of them is \(4\). Since the GCDs have to be distinct and at least \(1\), it is clear that there are no diverse matrices of size \(2 \times 2\) w...
Input: 2 2 | Output: 4 12 2 9
Easy
4
1,510
180
501
12
1,831
A
1831A
A. Twin Permutations
800
constructive algorithms
You are given a permutation\(^\dagger\) \(a\) of length \(n\).Find any permutation \(b\) of length \(n\) such that \(a_1+b_1 \le a_2+b_2 \le a_3+b_3 \le \ldots \le a_n+b_n\).It can be proven that a permutation \(b\) that satisfies the condition above always exists.\(^\dagger\) A permutation of length \(n\) is an array ...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2000\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the length of permutations \(a\) and \(b\).The...
For each test case, output any permutation \(b\) which satisfies the constraints mentioned in the statement. It can be proven that a permutation \(b\) that satisfies the condition above always exists.
In the first test case \(a=[1, 2, 4, 5, 3]\). Then the permutation \(b=[1, 2, 4, 3, 5]\) satisfies the condition because \(1 + 1 \le 2 + 2 \le 4 + 4 \le 5 + 3 \le 3 + 5\).
Input: 551 2 4 5 321 21133 2 141 4 3 2 | Output: 1 2 4 3 5 2 1 1 1 2 3 1 2 3 4
Beginner
1
602
569
200
18
2,048
I2
2048I2
I2. Kevin and Puzzle (Hard Version)
3,500
bitmasks; fft; math
This is the hard version of the problem. The difference between the versions is that in this version, you need to count the number of good arrays. You can hack only if you solved all versions of this problem. Kevin is visiting the Red Church, and he found a puzzle on the wall.For an array \( a \), let \( c(l,r) \) indi...
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. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2\leq n\leq 2\cdot 10^5\)) β€” the length of string \(s\).The second line o...
For each test case, output the number of good arrays modulo \(998\,244\,353\).
All arrays satisfying the conditions can be found in the easy version of this problem.
Input: 43LLR3RRL4RRLR5LLRLR | Output: 1 2 0 1
Master
3
903
545
78
20
2,084
G2
2084G2
G2. Wish Upon a Satellite (Hard Version)
3,500
data structures; dp
This is the hard version of the problem. The difference between the versions is that in this version, \(t \le 10^4\), \(n \le 5 \cdot 10^5\) and the sum of \(n\) does not exceed \(5 \cdot 10^5\). You can hack only if you solved all versions of this problem. For a non-empty sequence \(c\) of length \(k\), define \(f(c)\...
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 integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_...
For each test case, output a single integer β€” the maximum beauty of the permutation \(b\).
In the first test case, the permutation \(b\) with the maximum beauty is \([1, 2]\). The beauty of \([1, 2]\) is \(4\) since \(f([1]) + f([2]) + f([1, 2]) = 1 + 2 + 1 = 4\). If \(c = [1, 2]\), then \(f(c) = 1\) since Turtle can only choose \(i = 1\) and he will set \(c_1\) to \(\min(c_1, c_2) = 1\).In the second test c...
Input: 821 030 0 030 1 053 2 4 5 170 3 2 5 0 0 0101 2 6 5 8 9 0 0 0 050 4 1 0 050 1 5 2 3 | Output: 4 12 11 44 110 300 45 40
Master
2
2,579
533
90
20
922
E
922E
E. Birds
2,200
dp
Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp needs strong magic. There are n trees in a row on an alley in a park, there is a nest on each of the trees. In the i-th nest there are ci birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him cost...
The first line contains four integers n, W, B, X (1 ≀ n ≀ 103, 0 ≀ W, B, X ≀ 109) β€” the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.The second line contains n integers...
Print a single integer β€” the maximum number of birds Imp can summon.
In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 man...
Input: 2 12 0 43 44 2 | Output: 6
Hard
1
844
578
68
9
253
B
253B
B. Physics Practical
1,400
binary search; dp; sortings; two pointers
One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as n measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had...
The first line contains integer n (2 ≀ n ≀ 105) β€” the number of measurements Vasya made. The second line contains n integers c1, c2, ..., cn (1 ≀ ci ≀ 5000) β€” the results of the measurements. The numbers on the second line are separated by single spaces.
Print a single integer β€” the minimum number of results Vasya will have to remove.
In the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one...
Input: 64 5 3 8 3 7 | Output: 2
Easy
4
1,254
254
81
2
962
F
962F
F. Simple Cycles Edges
2,400
dfs and similar; graphs; trees
You are given an undirected graph, consisting of \(n\) vertices and \(m\) edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).A cycle in a graph is called a simple, if it con...
The first line contain two integers \(n\) and \(m\) \((1 \le n \le 100\,000\), \(0 \le m \le \min(n \cdot (n - 1) / 2, 100\,000))\) β€” the number of vertices and the number of edges.Each of the following \(m\) lines contain two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β€” the description of the edges.
In the first line print the number of edges, which belong to exactly one simple cycle.In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.
Input: 3 31 22 33 1 | Output: 31 2 3
Expert
3
495
320
277
9
1,621
C
1621C
C. Hidden Permutations
1,700
dfs and similar; interactive; math
This is an interactive problem.The jury has a permutation \(p\) of length \(n\) and wants you to guess it. For this, the jury created another permutation \(q\) of length \(n\). Initially, \(q\) is an identity permutation (\(q_i = i\) for all \(i\)).You can ask queries to get \(q_i\) for any \(i\) you want. After each q...
The first line of input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases.
In the first test case the hidden permutation \(p = [4, 2, 1, 3]\).Before the first query \(q = [1, 2, 3, 4]\) so answer for the query will be \(q_3 = 3\).Before the second query \(q = [4, 2, 1, 3]\) so answer for the query will be \(q_2 = 2\).Before the third query \(q = [3, 2, 4, 1]\) so answer for the query will be ...
Input: 2 4 3 2 1 4 2 4 4 | Output: ? 3 ? 2 ? 4 ! 4 2 1 3 ? 2 ? 3 ? 2 ! 1 3 4 2
Medium
3
625
108
0
16
1,987
G1
1987G1
G1. Spinning Round (Easy Version)
2,900
divide and conquer; dp; trees
This is the easy version of the problem. The only difference between the two versions are the allowed characters in \(s\). In the easy version, \(s\) only contains the character ?. You can make hacks only if both versions of the problem are solved.You are given a permutation \(p\) of length \(n\). You are also given a ...
Each test contains multiple test cases. The first line of input 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\) (\(2 \le n \le 4 \cdot 10^5\)) β€” the length of the permuta...
For each test case, output the maximum possible diameter over all connected graphs that you form, or \(-1\) if it is not possible to form any connected graphs.
In the first test case, here are some possible connected graphs that you can form (the labels are indices): In the second test case, the only connected graph has a diameter of \(1\).
Input: 852 1 4 3 5?????21 2??33 1 2???75 3 1 6 4 2 7???????55 2 1 3 4?????66 2 3 4 5 1??????81 7 5 6 2 8 4 3????????126 10 7 1 8 5 12 2 11 3 4 9???????????? | Output: 4 1 2 6 4 5 5 8
Master
3
1,415
728
159
19
914
E
914E
E. Palindromes in a Tree
2,400
bitmasks; data structures; divide and conquer; trees
You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered from 1 to n and each vertex is assigned a character from a to t.A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome.For each vertex, output the number of palind...
The first line contains an integer n (2 ≀ n ≀ 2Β·105) β€” the number of vertices in the tree.The next n - 1 lines each contain two integers u and v (1 ≀ u, v ≀ n, u β‰  v) denoting an edge connecting vertex u and vertex v. It is guaranteed that the given graph is a tree.The next line contains a string consisting of n lowerc...
Print n integers in a single line, the i-th of which is the number of palindromic paths passing through vertex i in the tree.
In the first sample case, the following paths are palindromic:2 - 3 - 42 - 3 - 54 - 3 - 5Additionally, all paths containing only one vertex are palindromic. Listed below are a few paths in the first sample that are not palindromic:1 - 2 - 31 - 2 - 3 - 41 - 2 - 3 - 5
Input: 51 22 33 43 5abcbb | Output: 1 3 4 3 3
Expert
4
544
421
125
9
1,001
I
1001I
I. Deutsch-Jozsa algorithm
1,700
*special
You are given a quantum oracle - an operation on N + 1 qubits which implements a function . You are guaranteed that the function f implemented by the oracle is either constant (returns 0 on all inputs or 1 on all inputs) or balanced (returns 0 on exactly one half of the input domain and 1 on the other half).There are o...
You have to implement an operation which takes the following inputs: an integer N - the number of qubits in the oracle input, an oracle Uf, implemented as an operation with signature ((Qubit[], Qubit) => ()), i.e., an operation which takes as input an array of qubits and an output qubit and has no output.The return of ...
Medium
1
635
651
0
10
1,704
B
1704B
B. Luke is a Foodie
1,000
brute force; greedy; implementation
Luke likes to eat. There are \(n\) piles of food aligned in a straight line in front of him. The \(i\)-th pile contains \(a_i\) units of food. Luke will walk from the \(1\)-st pile towards the \(n\)-th pile, and he wants to eat every pile of food without walking back. When Luke reaches the \(i\)-th pile, he can eat tha...
The input 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 test cases follows.For each test case, the first line contains two integers, \(n, x\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq x \leq 10^9\)) β€” the number of...
For each test case, output an integer on a separate line, which is the minimum number of changes needed.
In the first test case, Luke can set \(v\) to \(5\) before he starts to walk. And he can walk straight to eat every piles of food without changing \(v\).In the second test case, Luke can set \(v\) to \(3\) before he starts to walk. And he could change \(v\) to \(10\) before he eats the second pile. After that, he can w...
Input: 75 33 8 5 6 75 33 10 9 8 712 825 3 3 17 8 6 1 16 15 25 17 2310 21 2 3 4 5 6 7 8 9 108 22 4 6 8 6 4 12 148 22 7 8 9 6 13 21 2815 511 4 13 23 7 10 5 21 20 11 17 5 29 16 11 | Output: 0 1 2 1 2 4 6
Beginner
3
754
626
104
17
1,140
A
1140A
A. Detective Book
1,000
implementation
Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The \(i\)-th page contains some mystery that will be explained on page \(a_i\) (\(a_i \ge i\)).Ivan wants to read the whole book. Each day, he reads the first p...
The first line contains single integer \(n\) (\(1 \le n \le 10^4\)) β€” the number of pages in the book.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(i \le a_i \le n\)), where \(a_i\) is the number of page which contains the explanation of the mystery on page \(i\).
Print one integer β€” the number of days it will take to read the whole book.
Explanation of the example test:During the first day Ivan will read only the first page. During the second day Ivan will read pages number \(2\) and \(3\). During the third day β€” pages \(4\)-\(8\). During the fourth (and the last) day Ivan will read remaining page number \(9\).
Input: 9 1 3 3 6 7 6 8 8 9 | Output: 4
Beginner
1
735
286
75
11
444
B
444B
B. DZY Loves FFT
2,300
probabilities
DZY loves Fast Fourier Transformation, and he enjoys using it.Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if a, b and c are sequences with length n, which are indexed from 0 to n - 1, andWe can calculate c fast using Fast Fourier Transformation.DZY made a little change on th...
The only line of input contains three space-separated integers n, d, x (1 ≀ d ≀ n ≀ 100000; 0 ≀ x ≀ 1000000006). Because DZY is naughty, x can't be equal to 27777500.
Output n lines, the i-th line should contain an integer ci - 1.
In the first sample, a is [1 3 2], b is [1 0 0], so c0 = max(1Β·1) = 1, c1 = max(1Β·0, 3Β·1) = 3, c2 = max(1Β·0, 3Β·0, 2Β·1) = 2.In the second sample, a is [2 1 4 5 3], b is [1 1 1 0 1].In the third sample, a is [5 2 1 4 3], b is [1 1 1 1 0].
Input: 3 1 1 | Output: 132
Expert
1
1,132
166
63
4
1,907
A
1907A
A. Rook
800
implementation
As you probably know, chess is a game that is played on a board with 64 squares arranged in an \(8\times 8\) grid. Columns of this board are labeled with letters from a to h, and rows are labeled with digits from 1 to 8. Each square is described by the row and column it belongs to. The rook is a piece in the game of ch...
The first line of input contains single integer \(t\) (\(1 \le t \le 64\)) β€” the number of test cases. The descriptions of test cases follow.Each test case contains one string of two characters, description of the square where rook is positioned. The first character is a letter from a to h, the label of column, and the...
For each test case, output descriptions of all squares where the rook can move, in the same format as in the input. You can output squares in any order per test case.
Input: 1d5 | Output: d1 d2 b5 g5 h5 d3 e5 f5 d8 a5 d6 d7 c5 d4
Beginner
1
487
435
166
19
1,515
A
1515A
A. Phoenix and Gold
800
constructive algorithms; greedy; math
Phoenix has collected \(n\) pieces of gold, and he wants to weigh them together so he can feel rich. The \(i\)-th piece of gold has weight \(w_i\). All weights are distinct. He will put his \(n\) pieces of gold on a weight scale, one piece at a time. The scale has an unusual defect: if the total weight on it is exactly...
The input consists of multiple test cases. The first line contains an 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 100\); \(1 \le x \le 10^4\)) β€” the number of gold pieces that Phoenix has and the weight to avoid, r...
For each test case, if Phoenix cannot place all \(n\) pieces without the scale exploding, print NO. Otherwise, print YES followed by the rearranged array \(w\). If there are multiple solutions, print any.
In the first test case, Phoenix puts the gold piece with weight \(3\) on the scale first, then the piece with weight \(2\), and finally the piece with weight \(1\). The total weight on the scale is \(3\), then \(5\), then \(6\). The scale does not explode because the total weight on the scale is never \(2\).In the seco...
Input: 3 3 2 3 2 1 5 3 1 2 3 4 8 1 5 5 | Output: YES 3 2 1 YES 8 1 2 3 4 NO
Beginner
3
615
521
204
15
2,081
G2
2081G2
G2. Hard Formula (Hard Version)
3,400
math
This is the hard version of the problem. The difference between the versions is that in this version, the limit on \(n\) and the time limit are higher. You can hack only if you solved all versions of this problem. You are given an integer \(n\), and you need to compute \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\), w...
The only line contains a single integer \(n\) (\(1 \le n \le 10^{12}\)).
Print a single integer, representing \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\).
Input: 5 | Output: 2
Master
1
429
72
85
20
71
E
71E
E. Nuclear Fusion
2,200
bitmasks; dp
There is the following puzzle popular among nuclear physicists.A reactor contains a set of n atoms of some chemical elements. We shall understand the phrase ""atomic number"" as the number of this atom's element in the periodic table of the chemical elements.You are allowed to take any two different atoms and fuse a ne...
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 17). The second line contains space-separated symbols of elements of n atoms, which are available from the start. The third line contains space-separated symbols of elements of k atoms which need to be the result of the fusion. The symbols of the elements coinci...
If it is impossible to synthesize the required atoms, print ""NO"" without the quotes. Otherwise, print on the first line Β«YESΒ», and on the next k lines print the way of synthesizing each of k atoms as equations. Each equation has the following form: ""x1+x2+...+xt->yi"", where xj is the symbol of the element of some a...
The reactions from the first example possess the following form (the atomic number is written below and to the left of the element):To find a periodic table of the chemical elements, you may use your favorite search engine.The pretest set contains each of the first 100 elements of the periodic table at least once. You ...
Input: 10 3Mn Co Li Mg C P F Zn Sc KSn Pt Y | Output: YESMn+C+K->SnCo+Zn+Sc->PtLi+Mg+P+F->Y
Hard
2
712
690
701
0
1,409
B
1409B
B. Minimum Product
1,100
brute force; greedy; math
You are given four integers \(a\), \(b\), \(x\) and \(y\). Initially, \(a \ge x\) and \(b \ge y\). You can do the following operation no more than \(n\) times: Choose either \(a\) or \(b\) and decrease it by one. However, as a result of this operation, value of \(a\) cannot become less than \(x\), and value of \(b\) ca...
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.The only line of the test case contains five integers \(a\), \(b\), \(x\), \(y\) and \(n\) (\(1 \le a, b, x, y, n \le 10^9\)). Additional constraint on the input: \(a \ge x\) an...
For each test case, print one integer: the minimum possible product of \(a\) and \(b\) (\(a \cdot b\)) you can achieve by applying the given operation no more than \(n\) times.
In the first test case of the example, you need to decrease \(b\) three times and obtain \(10 \cdot 7 = 70\).In the second test case of the example, you need to decrease \(a\) one time, \(b\) one time and obtain \(11 \cdot 7 = 77\).In the sixth test case of the example, you need to decrease \(a\) five times and obtain ...
Input: 7 10 10 8 5 3 12 8 8 7 2 12343 43 4543 39 123212 1000000000 1000000000 1 1 1 1000000000 1000000000 1 1 1000000000 10 11 2 1 5 10 11 9 1 10 | Output: 70 77 177177 999999999000000000 999999999 55 10
Easy
3
555
347
176
14
1,527
B1
1527B1
B1. Palindrome Game (easy version)
1,200
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 the example, in the \(1\)-st move Alice has to perform the \(1\)-st operation, since the string is currently a palindrome. in the \(2\)-nd move Bob reverses the string. in the \(3\)-rd move Alice again has to perform the \(1\)-st operation. All characters of the string are '1', game over. Alic...
Input: 2 4 1001 1 0 | Output: BOB BOB
Easy
2
1,362
457
165
15
690
B3
690B3
B3. Recover Polygon (hard)
2,600
data structures
Zombies have found out about the Zombie Contamination level checker and managed to damage it! Now detecting the shape of their main compound will be a real challenge for Heidi. As before, a lair can be represented as a strictly convex polygon on a lattice. Each vertex of the polygon occupies a point on the lattice. How...
The input contains multiple test cases.The first line of each test case contains two space-separated integers N and M, where N is the size of the lattice grid (5 ≀ N ≀ 100000) and M is the number of lattice points for which the Zombie Contamination level is 1, 2, or 3 (8 ≀ M ≀ 200000).The second line of each test case ...
For each test case, the following output is expected:The first line of the output should contain one integer V, the number of vertices of the polygon that is the secret lair. The next V lines each should contain two integers, denoting the vertices of the polygon in the clockwise order, starting from the lexicographical...
It is guaranteed that the solution always exists and is unique. It is guaranteed that in the correct solution the coordinates of the polygon vertices are between 1 and N - 1. A vertex (x1, y1) is lexicographically smaller than vertex (x2, y2) if x1 < x2 or .
Input: 8 192 3 2 4 2 5 3 3 3 5 4 3 4 5 4 6 5 2 5 3 5 6 6 2 6 3 6 4 6 5 6 6 6 7 7 6 7 75 82 2 2 3 2 4 3 2 3 4 4 2 4 3 4 40 0 | Output: 42 32 46 65 242 22 33 33 2
Expert
1
717
911
339
6
1,443
A
1443A
A. Kids Seating
800
constructive algorithms; math
Today the kindergarten has a new group of \(n\) kids who need to be seated at the dinner table. The chairs at the table are numbered from \(1\) to \(4n\). Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers \(a\) and \(b\) (\(a \neq b\)) will indulge if: \(gcd(a, b) = 1\) or, ...
The first line contains one integer \(t\) (\(1 \leq t \leq 100\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case consists of one line containing an integer \(n\) (\(1 \leq n \leq 100\)) β€” the number of kids.
Output \(t\) lines, which contain \(n\) distinct integers from \(1\) to \(4n\) β€” the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print \(n\) numbers in any order.
Input: 3 2 3 4 | Output: 6 4 4 6 10 14 10 12 8
Beginner
2
1,072
232
254
14
272
B
272B
B. Dima and Sequence
1,400
implementation; math
Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence: f(0) = 0; f(2Β·x) = f(x); f(2Β·x + 1) = f(x) + 1. Dima wonders, how many pairs of indexes (i, j) (1 ≀ i < j ≀ n) are there, such...
The first line contains integer n (1 ≀ n ≀ 105). The second line contains n positive integers a1, a2, ..., an (1 ≀ ai ≀ 109).The numbers in the lines are separated by single spaces.
In a single line print the answer to the problem.Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
In the first sample any pair (i, j) will do, so the answer is 3.In the second sample only pair (1, 2) will do.
Input: 31 2 4 | Output: 3
Easy
2
382
181
195
2
1,891
A
1891A
A. Sorting with Twos
800
constructive algorithms; sortings
You are given an array of integers \(a_1, a_2, \ldots, a_n\). In one operation, you do the following: Choose a non-negative integer \(m\), such that \(2^m \leq n\). Subtract \(1\) from \(a_i\) for all integers \(i\), such that \(1 \leq i \leq 2^m\). Can you sort the array in non-decreasing order by performing some numb...
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 a single integer \(n\) (\(1 \leq n \leq 20\)) β€” the length of array \(a\).The second line of each test case contains \(n\) integers \(a_...
For each test case, output ""YES"" if the array can be sorted, and ""NO"" otherwise.
In the first test case, the array is already sorted in non-decreasing order, so we don't have to perform any operations.In the second test case, we can choose \(m = 1\) twice to get the array \([4, 3, 3, 4, 4]\). Then, we can choose \(m = 0\) once and get the sorted in non-decreasing order array \([3, 3, 3, 4, 4]\).In ...
Input: 851 2 3 4 556 5 3 4 496 5 5 7 5 6 6 8 744 3 2 162 2 4 5 3 281 3 17 19 27 57 179 1353 17 57 179 92101 2 3 4 0 6 7 8 9 10 | Output: YES YES YES NO NO NO YES YES
Beginner
2
474
399
84
18
1,654
F
1654F
F. Minimal String Xoration
2,800
bitmasks; data structures; divide and conquer; greedy; hashing; sortings; strings
You are given an integer \(n\) and a string \(s\) consisting of \(2^n\) lowercase letters of the English alphabet. The characters of the string \(s\) are \(s_0s_1s_2\cdots s_{2^n-1}\).A string \(t\) of length \(2^n\) (whose characters are denoted by \(t_0t_1t_2\cdots t_{2^n-1}\)) is a xoration of \(s\) if there exists ...
The first line contains a single integer \(n\) (\(1 \le n \le 18\)).The second line contains a string \(s\) consisting of \(2^n\) lowercase letters of the English alphabet.
Print a single line containing the lexicographically minimal xoration of \(s\).
In the first test, the lexicographically minimal xoration \(t\) of \(s =\)""acba"" is ""abca"". It's a xoration because, for \(j = 3\), \(t_0 = s_{0 \oplus j} = s_3 =\) ""a""; \(t_1 = s_{1 \oplus j} = s_2 =\) ""b""; \(t_2 = s_{2 \oplus j} = s_1 =\) ""c""; \(t_3 = s_{3 \oplus j} = s_0 =\) ""a"". There isn't any xoration...
Input: 2 acba | Output: abca
Master
7
847
172
79
16
117
A
117A
A. Elevator
1,300
implementation; math
And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All n participants who have made it to the finals found themselves in a huge m-floored 108-star hotel. Of course the first thought to come in a place like this is ""How about checking out the elevato...
The first line contains two space-separated integers n and m (1 ≀ n ≀ 105, 2 ≀ m ≀ 108). Next n lines contain information about the participants in the form of three space-separated integers si fi ti (1 ≀ si, fi ≀ m, 0 ≀ ti ≀ 108), described in the problem statement.
Print n lines each containing one integer β€” the time of the arrival for each participant to the required floor.
Let's consider the first sample. The first participant starts at floor s = 2 by the time equal to t = 3. To get to the floor f = 4, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. I...
Input: 7 42 4 31 2 02 2 01 2 14 3 51 2 24 2 0 | Output: 91071075
Easy
2
1,552
267
111
1
1,866
K
1866K
K. Keen Tree Calculation
2,500
binary search; data structures; dp; geometry; graphs; implementation; trees
There is a tree of \(N\) vertices and \(N-1\) edges. The \(i\)-th edge connects vertices \(U_i\) and \(V_i\) and has a length of \(W_i\).Chaneka, the owner of the tree, asks you \(Q\) times. For the \(j\)-th question, the following is the question format: \(X_j\) \(K_j\) – If each edge that contains vertex \(X_j\) has ...
The first line contains a single integer \(N\) (\(2\leq N\leq10^5\)) β€” the number of vertices in the tree.The \(i\)-th of the next \(N-1\) lines contains three integers \(U_i\), \(V_i\), and \(W_i\) (\(1 \leq U_i,V_i \leq N\); \(1\leq W_i\leq10^9\)) β€” an edge that connects vertices \(U_i\) and \(V_i\) with a length of ...
Output \(Q\) lines with an integer in each line. The integer in the \(j\)-th line represents the diameter of the tree on the \(j\)-th question.
In the first example, the following is the tree without any changes.The following is the tree on the \(1\)-st question.The maximum distance is between vertices \(6\) and \(7\), which is \(6+6+6=18\), so the diameter is \(18\).The following is the tree on the \(2\)-nd question.The maximum distance is between vertices \(...
Input: 7 5 1 2 1 4 2 3 4 1 2 5 3 6 1 6 4 7 2 2 4 3 3 2 | Output: 18 11
Expert
7
613
595
143
18
500
G
500G
G. New Year Running
3,200
number theory; trees
New Year is coming in Tree Island! In this island, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists exactly one path between them. For every person in Tree Island, it takes exactly one minute to pass by exactly one road.There is a weird New Year tradi...
The first line contains a single positive integer n (5 ≀ n ≀ 2 Γ— 105) β€” the number of cities in Tree Island.Next n - 1 lines describe the roads of Tree Island. The i-th line (1 ≀ i ≀ n - 1) of them contains two space-separated integers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the vertices connected by a single road of the...
For each test case, print an integer describing the amount of time they should run in minutes. If they have to run for an infinitely long time (in other words, if they never meet at the same city), print -1 instead. If they meet at the beginning of their run, print 0.
The example looks like:
Input: 71 33 67 43 75 47 246 5 5 33 5 4 61 5 1 31 5 3 1 | Output: 210-1
Master
2
1,603
746
268
5
914
F
914F
F. Substrings in a String
3,000
bitmasks; brute force; data structures; string suffix structures; strings
Given a string s, process q queries, each having one of the following forms: 1 i c β€” Change the i-th character in the string to c. 2 l r y β€” Consider the substring of s starting at position l and ending at position r. Output the number of times y occurs as a substring in it.
The first line of the input contains the string s (1 ≀ |s| ≀ 105) of lowercase English letters.The second line contains an integer q (1 ≀ q ≀ 105) β€” the number of queries to process.The next q lines describe the queries and may have one of the following forms: 1 i c (1 ≀ i ≀ |s|) 2 l r y (1 ≀ l ≀ r ≀ |s|) c is a lowerc...
For each query of type 2, output the required answer in a separate line.
Consider the first sample case. Initially, the string aba occurs 3 times in the range [1, 7]. Note that two occurrences may overlap. After the update, the string becomes ababcbaba and now aba occurs only once in the range [1, 7].
Input: ababababa32 1 7 aba1 5 c2 1 7 aba | Output: 31
Master
5
275
599
72
9
2,108
B
2108B
B. SUMdamental Decomposition
1,300
bitmasks; constructive algorithms; greedy; implementation; math
On a recent birthday, your best friend Maurice gave you a pair of numbers \(n\) and \(x\), and asked you to construct an array of positive numbers \(a\) of length \(n\) such that \(a_1 \oplus a_2 \oplus \cdots \oplus a_n = x\) \(^{\text{βˆ—}}\). This task seemed too simple to you, and therefore you decided to give Mauric...
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. Each test case consists of a single line containing a pair of numbers \(n\) and \(x\) (\(1 \le n \le 10^9, \; 0 \le x \le 10^9\)) β€” the numbers given to you b...
For each test case, output your gift to Maurice β€” the sum of the elements of the array that satisfies all the described properties. If a suitable array does not exist, output \(-1\).
In the first test case, one of the suitable arrays is \([2, 3]\). It can be shown that it is impossible to achieve a smaller sum of array elements.In the second case, one of the suitable arrays is \([1, 3, 4]\). It can also be shown that this is the optimal amount.
Input: 82 13 61 02 05 02 2715 4312345678 9101112 | Output: 5 8 -1 2 8 27 55 21446778
Easy
5
660
330
182
21
2,052
B
2052B
2,400
Expert
0
0
0
0
20
1,514
D
1514D
D. Cut and Stick
2,000
binary search; data structures; greedy; implementation; sortings
Baby Ehab has a piece of Cut and Stick with an array \(a\) of length \(n\) written on it. He plans to grab a pair of scissors and do the following to it: pick a range \((l, r)\) and cut out every element \(a_l\), \(a_{l + 1}\), ..., \(a_r\) in this range; stick some of the elements together in the same order they were ...
The first line contains two integers \(n\) and \(q\) (\(1 \le n,q \le 3 \cdot 10^5\)) β€” the length of the array \(a\) and the number of queries.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_{n}\) (\(1 \le a_i \le n\)) β€” the elements of the array \(a\).Each of the next \(q\) lines contains two integ...
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
In the first query, you can just put the whole array in one subsequence, since its length is \(6\), and no value occurs more than \(3\) times in it.In the second query, the elements of the query range are \([3,2,3,3]\). You can't put them all in one subsequence, since its length is \(4\), and \(3\) occurs more than \(2...
Input: 6 2 1 3 2 3 3 2 1 6 2 5 | Output: 1 2
Hard
5
1,145
392
179
15
1,237
F
1237F
F. Balanced Domino Placements
2,600
combinatorics; dp
Consider a square grid with \(h\) rows and \(w\) columns with some dominoes on it. Each domino covers exactly two cells of the grid that share a common side. Every cell is covered by at most one domino.Let's call a placement of dominoes on the grid perfectly balanced if no row and no column contains a pair of cells cov...
The first line contains three integers \(h\), \(w\), and \(n\) (\(1 \le h, w \le 3600\); \(0 \le n \le 2400\)), denoting the dimensions of the grid and the number of already placed dominoes. The rows are numbered from \(1\) to \(h\), and the columns are numbered from \(1\) to \(w\).Each of the next \(n\) lines contains...
Output the number of ways to place zero or more extra dominoes on the grid to keep the placement perfectly balanced, modulo \(998\,244\,353\).
In the first example, the initial grid looks like this:Here are \(8\) ways to place zero or more extra dominoes to keep the placement perfectly balanced:In the second example, the initial grid looks like this:No extra dominoes can be placed here.
Input: 5 7 2 3 1 3 2 4 4 4 5 | Output: 8
Expert
2
717
687
142
12
1,572
B
1572B
B. Xor of 3
2,500
brute force; constructive algorithms; greedy; two pointers
You are given a sequence \(a\) of length \(n\) consisting of \(0\)s and \(1\)s.You can perform the following operation on this sequence: Pick an index \(i\) from \(1\) to \(n-2\) (inclusive). Change all of \(a_{i}\), \(a_{i+1}\), \(a_{i+2}\) to \(a_{i} \oplus a_{i+1} \oplus a_{i+2}\) simultaneously, where \(\oplus\) de...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)).The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2\cdot10^5\)) β€” the length of \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\...
For each test case, do the following: if there is no way of making all the elements of \(a\) equal to \(0\) after performing the above operation some number of times, print ""NO"". otherwise, in the first line print ""YES"", in the second line print \(k\) (\(0 \le k \le n\)) β€” the number of operations that you want to ...
In the first example, the sequence contains only \(0\)s so we don't need to change anything.In the second example, we can transform \([1, 1, 1, 1, 0]\) to \([1, 1, 0, 0, 0]\) and then to \([0, 0, 0, 0, 0]\) by performing the operation on the third element of \(a\) and then on the first element of \(a\).In the third exa...
Input: 3 3 0 0 0 5 1 1 1 1 0 4 1 0 0 1 | Output: YES 0 YES 2 3 1 NO
Expert
4
659
457
535
15
1,607
G
1607G
G. Banquet Preparations 1
2,200
greedy
A known chef has prepared \(n\) dishes: the \(i\)-th dish consists of \(a_i\) grams of fish and \(b_i\) grams of meat. The banquet organizers estimate the balance of \(n\) dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technicall...
The first line of input data contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of the test cases.Each test case's description is preceded by a blank line. Next comes a line that contains integers \(n\) and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\); \(0 \leq m \leq 10^6\)). The next \(n\) lines describe dishes...
For each test case, print on the first line the minimal balance value that can be achieved by eating exactly \(m\) grams of food from each dish.Then print \(n\) lines that describe a way to do this: the \(i\)-th line should contain two integers \(x_i\) and \(y_i\) (\(0 \leq x_i \leq a_i\); \(0 \leq y_i \leq b_i\); \(x_...
Input: 8 1 5 3 4 1 6 3 4 2 2 1 3 4 2 2 4 1 3 1 7 3 6 1 7 1 8 1 9 3 6 1 8 1 9 30 10 3 4 3 1 3 2 4 1 5 4 0 7 6 4 0 8 4 1 5 3 | Output: 0 2 3 1 3 3 0 1 1 1 1 2 1 3 0 4 3 0 6 0 6 0 6 7 1 5 1 5 6 0 0 3 1 3 1 3 1 0 0 4 2 2 0 4 3 1 1 3
Hard
1
938
725
524
16
344
A
344A
A. Magnets
800
implementation
Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a ""plus"") and negative (a ""minus""). If two magnets are put together at a close distance, then the like poles will repel each other and the ...
The first line of the input contains an integer n (1 ≀ n ≀ 100000) β€” the number of magnets. Then n lines follow. The i-th line (1 ≀ i ≀ n) contains either characters ""01"", if Mike put the i-th magnet in the ""plus-minus"" position, or characters ""10"", if Mike put the magnet in the ""minus-plus"" position.
On the single line of the output print the number of groups of magnets.
The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.The second testcase has two groups, each consisting of two magnets.
Input: 6101010011010 | Output: 3
Beginner
1
935
310
71
3
962
E
962E
E. Byteland, Berland and Disputed Cities
2,200
constructive algorithms; greedy
The cities of Byteland and Berland are located on the axis \(Ox\). In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line \(Ox\) there are three types of cities: the cities of Byteland, the cities of Berland, disputed cities. Recently, the pro...
The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^{5}\)) β€” the number of cities.The following \(n\) lines contains an integer \(x_i\) and the letter \(c_i\) (\(-10^{9} \le x_i \le 10^{9}\)) β€” the coordinate of the city and its type. If the city belongs to Byteland, \(c_i\) equals to 'B'. If the c...
Print the minimal total length of such set of cables, that if we delete all Berland cities (\(c_i\)='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities (\(c_i\)='B'), it will be possible to find a way from any re...
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be \(5 + 3 + 4 = 12\).In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neigh...
Input: 4-5 R0 P3 P7 B | Output: 12
Hard
2
1,709
536
384
9
1,279
E
1279E
E. New Year Permutations
2,700
combinatorics; dp
Yeah, we failed to make up a New Year legend for this problem.A permutation of length \(n\) is an array of \(n\) integers such that every integer from \(1\) to \(n\) appears in it exactly once. An element \(y\) of permutation \(p\) is reachable from element \(x\) if \(x = y\), or \(p_x = y\), or \(p_{p_x} = y\), and so...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Then the test cases follow. Each test case is represented by one line containing two integers \(n\) and \(k\) (\(1 \le n \le 50\), \(1 \le k \le 10^{18}\)).
For each test case, print the answer to it as follows: if the number of good permutations of length \(n\) is less than \(k\), print one integer \(-1\); otherwise, print the \(k\)-th good permutation on \(n\) elements (in lexicographical order).
Input: 5 3 3 5 15 4 13 6 8 4 2 | Output: 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
Master
2
3,066
248
244
12
75
C
75C
C. Modified GCD
1,600
binary search; number theory
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.A common divisor for two positive numbers is a number which both numbers are divisible by.But your teacher wants to give you a harder task, in this task you...
The first line contains two integers a and b, the two integers as described above (1 ≀ a, b ≀ 109). The second line contains one integer n, the number of queries (1 ≀ n ≀ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≀ low ≀ high ≀ 109).
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
Input: 9 2731 510 119 11 | Output: 3-19
Medium
2
668
291
201
0
1,357
B2
1357B2
B2. ""Is the number divisible by 3?"" oracle
0
*special
Implement a quantum oracle on \(N\) qubits which checks whether the input bit string is a little-endian notation of a number that is divisible by 3. Your operation should take the following inputs: an array of \(N \le 8\) qubits ""inputs"" in an arbitrary state. a qubit ""output"" in an arbitrary state. Your operation ...
Beginner
1
1,737
0
0
13
1,747
B
1747B
B. BAN BAN
900
constructive algorithms
You are given an integer \(n\).Let's define \(s(n)\) as the string ""BAN"" concatenated \(n\) times. For example, \(s(1)\) = ""BAN"", \(s(3)\) = ""BANBANBAN"". Note that the length of the string \(s(n)\) is equal to \(3n\).Consider \(s(n)\). You can perform the following operation on \(s(n)\) any number of times (possi...
The input consists of multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 100)\) β€” the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer \(n\) \((1 \leq n \leq 100)\).
For each test case, in the first line output \(m\) (\(0 \le m \le 10^5\)) β€” the minimum number of operations required. It's guaranteed that the objective is always achievable in at most \(10^5\) operations under the constraints of the problem. Then, output \(m\) lines. The \(k\)-th of these lines should contain two int...
In the first testcase, \(s(1) = \) ""BAN"", we can swap \(s(1)_1\) and \(s(1)_2\), converting \(s(1)\) to ""ABN"", which does not contain ""BAN"" as a subsequence.In the second testcase, \(s(2) = \) ""BANBAN"", we can swap \(s(2)_2\) and \(s(2)_6\), converting \(s(2)\) to ""BNNBAA"", which does not contain ""BAN"" as a...
Input: 2 1 2 | Output: 1 1 2 1 2 6
Beginner
1
794
272
618
17
2,022
E2
2022E2
E2. Billetes MX (Hard Version)
2,600
binary search; combinatorics; data structures; dsu; graphs
This is the hard version of the problem. In this version, it is guaranteed that \(q \leq 10^5\). You can make hacks only if both versions of the problem are solved.An integer grid \(A\) with \(p\) rows and \(q\) columns is called beautiful if: All elements of the grid are integers between \(0\) and \(2^{30}-1\), and Fo...
The first line contains \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains four integers \(n\), \(m\), \(k\) and \(q\) (\(2 \le n, m \le 10^5\); \(0 \le k, q \leq 10^5\)) β€” the number of rows, the number of columns, the number of fixed cells, and the number of updates.The ...
For each test case, output \(q + 1\) lines. The \(i\)-th line of output should contain the answer of the \(i\)-th state of the grid modulo \(10^9 + 7\).
In the first test case of the example, we initially have the following grid: \(0\)\(6\)\(10\)\(6\)\(0\)\(12\)\(10\)\(12\)\(?\) It can be proven that the only valid value for tile \((3, 3)\) is \(0\), so the first answer is \(1\). For the second query, the grid does not satisfy the condition, and thus the answer is \(0\...
Input: 33 3 8 12 1 63 2 121 2 62 2 01 3 101 1 02 3 123 1 103 3 12 5 2 01 1 101 2 302 5 0 21 1 101 2 30 | Output: 1 0 489373567 651321892 769740174 489373567
Expert
5
1,491
889
152
20
1,562
C
1562C
C. Rings
1,500
constructive algorithms; math
Frodo was caught by Saruman. He tore a pouch from Frodo's neck, shook out its contents β€”there was a pile of different rings: gold and silver...""How am I to tell which is the One?!"" the mage howled.""Throw them one by one into the Cracks of Doom and watch when Mordor falls!"" Somewhere in a parallel Middle-earth, when...
Each test contains multiple test cases.The first line contains one positive integer \(t\) (\(1 \le t \le 10^3\)), denoting the number of test cases. Description of the test cases follows.The first line of each test case contains one positive integer \(n\) (\(2 \le n \le 2 \cdot 10^4\)) β€” length of the string.The second...
For every test case print four integers \(l_1\), \(r_1\), \(l_2\), \(r_2\), which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively.If there are multiple solutions, print any.
In the first testcase \(f(t) = f(1111) = 15\), \(f(w) = f(101) = 5\).In the second testcase \(f(t) = f(111000111) = 455\), \(f(w) = f(000111) = 7\).In the third testcase \(f(t) = f(0000) = 0\), \(f(w) = f(1000) = 8\).In the fourth testcase \(f(t) = f(11011) = 27\), \(f(w) = f(011) = 3\).In the fifth testcase \(f(t) = f...
Input: 7 6 101111 9 111000111 8 10000000 5 11011 6 001111 3 101 30 100000000000000100000000000000 | Output: 3 6 1 3 1 9 4 9 5 8 1 4 1 5 3 5 1 6 2 4 1 2 2 3 1 15 16 30
Medium
2
1,766
479
291
15
220
B
220B
B. Little Elephant and Array
1,800
constructive algorithms; data structures
The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let's denote the number with index i as ai. Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 ≀ lj ≀ rj ≀ n). For each query lj...
The first line contains two space-separated integers n and m (1 ≀ n, m ≀ 105) β€” the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, ..., an (1 ≀ ai ≀ 109). Next m lines contain descriptions of queries, one per line. The j-th of these lines contains the...
In m lines print m integers β€” the answers to the queries. The j-th line should contain the answer to the j-th query.
Input: 7 23 1 2 2 3 3 71 73 4 | Output: 31
Medium
2
525
411
116
2
1,687
E
1687E
E. Become Big For Me
3,500
combinatorics; constructive algorithms; greedy; math; number theory
Come, let's build a world where even the weak are not forgotten!β€”Kijin Seija, Double Dealing CharactersShinmyoumaru has a mallet that can turn objects bigger or smaller. She is testing it out on a sequence \(a\) and a number \(v\) whose initial value is \(1\). She wants to make \(v = \gcd\limits_{i\ne j}\{a_i\cdot a_j\...
The first line contains a single integer \(n\) (\(2\leq n\leq 10^5\)) β€” the size of sequence \(a\).The second line contains \(n\) integers \(a_1,a_2,\cdots,a_n\) (\(1\leq a_i\leq 10^6\)) β€” the sequence \(a\).It can be shown that the answer exists.
The first line contains a non-negative integer \(k\) (\(0\leq k\leq 10^5\)) β€” the number of operations.The following \(k\) lines contains several integers. For each line, the first two integers \(f\) (\(f\in\{0,1\}\)) and \(p\) (\(1\le p\le n\)) stand for the option you choose (\(0\) for Enlarge and \(1\) for Reduce) a...
Test case 1:\(\gcd\limits_{i\ne j}\{a_i\cdot a_j\}=\gcd\{60,90,150\}=30\).Perform \(v = v\cdot \operatorname{lcm}\{a_1,a_2,a_3\}=30\).Test case 2:\(\gcd\limits_{i\ne j}\{a_i\cdot a_j\}=8\).Perform \(v = v\cdot \operatorname{lcm}\{a_4\}=16\).Perform \(v = \frac{v}{\operatorname{lcm}\{a_1\}}=8\).
Input: 3 6 10 15 | Output: 1 0 3 1 2 3
Master
5
1,018
247
508
16
1,975
E
1975E
E. Chain Queries
2,100
binary search; data structures; dfs and similar; implementation; trees
You are given a tree of \(n\) vertices numbered from \(1\) to \(n\). Initially, all vertices are colored white or black.You are asked to perform \(q\) queries: ""u"" β€” toggle the color of vertex \(u\) (if it was white, change it to black and vice versa). After each query, you should answer whether all the black vertice...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1\leq n,q\leq 2\cdot 10^5\)).The second line of each test case contains \(n\) integer...
For each query, output ""Yes"" if the black vertices form a chain, and output ""No"" otherwise.You can output ""Yes"" and ""No"" in any case (for example, strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive response).
In the second test case, the color of the vertices are as follows:The initial tree: The first query toggles the color of vertex \(4\): The second query toggles the color of vertex \(3\): The third query toggles the color of vertex \(2\): The fourth query toggles the color of vertex \(5\):
Input: 22 11 01 215 41 0 0 0 01 21 31 53 44325 | Output: No No Yes Yes No
Hard
5
607
1,021
244
19
896
D
896D
D. Nephren Runs a Cinema
2,900
chinese remainder theorem; combinatorics; math; number theory
Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema.However, one day, the No. 68 Cinema runs out of changes (they don't have 50-yuan notes currently), but Nephren still wants to start their business. (Assume that yuan is a kind of currency in Regulu Ere.)There are three types o...
One line containing four integers n (1 ≀ n ≀ 105), p (1 ≀ p ≀ 2Β·109), l and r (0 ≀ l ≀ r ≀ n).
One line indicating the answer modulo p.
We use A, B and C to indicate customers with 50-yuan notes, customers with 100-yuan notes and customers with VIP cards respectively.For the first sample, the different possible queues that there are 2 50-yuan notes left are AAAB, AABA, ABAA, AACC, ACAC, ACCA, CAAC, CACA and CCAA, and the different possible queues that ...
Input: 4 97 2 3 | Output: 13
Master
4
1,019
94
40
8
2,021
B
2021B
B. Maximize Mex
1,200
brute force; greedy; math; number theory
You are given an array \(a\) of \(n\) positive integers and an integer \(x\). You can do the following two-step operation any (possibly zero) number of times: Choose an index \(i\) (\(1 \leq i \leq n\)). Increase \(a_i\) by \(x\), in other words \(a_i := a_i + x\). Find the maximum value of the \(\operatorname{MEX}\) o...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(x\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le x \le 10^9\)) β€” the length of the array and the i...
For each test case, output a single integer: the maximum \(\operatorname{MEX}\) of \(a\) if you perform the operations optimally.
In the first test case, the \(\operatorname{MEX}\) of \(a\) is \(4\) without performing any operations, which is the maximum.In the second test case, the \(\operatorname{MEX}\) of \(a\) is \(5\) without performing any operations. If we perform two operations both with \(i=1\), we will have the array \(a=[5,3,4,1,0,2]\)...
Input: 36 30 3 2 1 5 26 21 3 4 1 0 24 52 5 10 3 | Output: 4 6 0
Easy
4
831
574
129
20
908
A
908A
A. New Year and Counting Cards
800
brute force; implementation
Your friend has n cards.You know that each card has a lowercase English letter on one side and a digit on the other.Currently, your friend has laid out the cards on a table so only one side of each card is visible.You would like to know if the following statement is true for cards that your friend owns: ""If a card has...
The first and only line of input will contain a string s (1 ≀ |s| ≀ 50), denoting the sides of the cards that you can see on the table currently. Each character of s is either a lowercase English letter or a digit.
Print a single integer, the minimum number of cards you must turn over to verify your claim.
In the first sample, we must turn over both cards. Note that even though both cards have the same letter, they could possibly have different numbers on the other side.In the second sample, we don't need to turn over any cards. The statement is vacuously true, since you know your friend has no cards with a vowel on them...
Input: ee | Output: 2
Beginner
2
1,147
214
92
9
1,671
A
1671A
A. String Building
800
implementation
You are given a string \(s\). You have to determine whether it is possible to build the string \(s\) out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.For example: aaaabbb can be built as aa \(+\) aa \(+\) bbb; bbaaaaabbb can...
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Each test case consists of one line containing the string \(s\) (\(1 \le |s| \le 50\)), consisting of characters a and/or b.
For each test case, print YES if it is possible to build the string \(s\). Otherwise, print NO.You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
The first four test cases of the example are described in the statement.
Input: 8aaaabbbbbaaaaabbbaaaaaaabababaaaabbbaaa | Output: YES YES YES NO NO NO NO YES
Beginner
1
460
216
261
16
1,722
G
1722G
G. Even-Odd XOR
1,500
bitmasks; constructive algorithms; greedy
Given an integer \(n\), find any array \(a\) of \(n\) distinct nonnegative integers less than \(2^{31}\) such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
The first line of the input contains an integer \(t\) (\(1 \leq t \leq 629\)) β€” the number of test cases.Then \(t\) lines follow, each containing a single integer \(n\) \((3 \leq n \leq 2\cdot10^5)\) β€” the length of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
For each test case, output one line containing \(n\) distinct integers that satisfy the conditions.If there are multiple answers, you can output any of them.
In the first test case the XOR on odd indices is \(4 \oplus 1 \oplus 0 \oplus 7 = 2\) and the XOR on even indices is \(2 \oplus 5 \oplus 6 \oplus 3= 2\).
Input: 7 8 3 4 5 6 7 9 | Output: 4 2 1 5 0 6 7 3 2 1 3 2 1 3 0 2 0 4 5 3 4 1 2 12 3 8 1 2 3 4 5 6 7 8 2 3 7 4 0 5 6 9
Medium
3
217
317
157
17
1,017
E
1017E
E. The Supersonic Rocket
2,400
geometry; hashing; strings
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two ""engines"". Each engine is a set of ""power sources"". The first engine has \(n\) power sources, and the second one has \(m\) power sources. A power source can be described as a point \((x_i, y_i)\)...
The first line contains two integers \(n\), \(m\) (\(3 \le n, m \le 10^5\)) β€” the number of power sources in each engine.Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(0\leq x_i, y_i\leq 10^8\)) β€” the coordinates of the \(i\)-th power source in the first engine.Each of the next \(m\) lines co...
Print ""YES"" if the supersonic rocket is safe, otherwise ""NO"".You can print each letter in an arbitrary case (upper or lower).
The first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with \(\theta = \pi\) (to rotate all power sources \(180\) degrees).The power sources in the first engine become \((0, 0)\), \((0, -2)\), and \((-2, 0)\). Second, manipulate the s...
Input: 3 40 00 22 00 22 22 01 1 | Output: YES
Expert
3
1,940
569
129
10
1,433
C
1433C
C. Dominant Piranha
900
constructive algorithms; greedy
There are \(n\) piranhas with sizes \(a_1, a_2, \ldots, a_n\) in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other pira...
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β€” the number of piranhas in the aquarium. The second line of the test case contains \(n...
For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any.
The first test case of the example is described in the problem statement.In the second test case of the example, there are no dominant piranhas in the aquarium.In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes \([4, 4, 5, 4]\), then it can eat any...
Input: 6 5 5 3 4 4 5 3 1 1 1 5 4 4 3 4 4 5 5 5 4 3 2 3 1 1 2 5 5 4 3 5 5 | Output: 3 -1 4 3 3 1
Beginner
2
1,655
536
183
14
754
D
754D
D. Fedor and coupons
2,100
binary search; data structures; greedy; sortings
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket. The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has n discount coupons, the i-th of them can be used with products with id...
The first line contains two integers n and k (1 ≀ k ≀ n ≀ 3Β·105) β€” the number of coupons Fedor has, and the number of coupons he wants to choose.Each of the next n lines contains two integers li and ri ( - 109 ≀ li ≀ ri ≀ 109) β€” the description of the i-th coupon. The coupons can be equal.
In the first line print single integer β€” the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.In the second line print k distinct integers p1, p2, ..., pk (1 ≀ pi ≀ n) β€” the ids of the coupons which Fedor should cho...
In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
Input: 4 21 10040 70120 130125 180 | Output: 311 2
Hard
4
700
290
373
7
1,149
C
1149C
C. Tree Generatorβ„’
2,700
data structures; implementation; trees
Owl Pacino has always been into trees β€” unweighted rooted trees in particular. He loves determining the diameter of every tree he sees β€” that is, the maximum length of any simple path in the tree.Owl Pacino's owl friends decided to present him the Tree Generatorβ„’ β€” a powerful machine creating rooted trees from their de...
The first line of the input contains two integers \(n, q\) (\(3 \le n \le 100\,000\), \(1 \le q \le 100\,000\)) β€” the number of vertices in the tree and the number of changes to the tree description. The following line contains a description of the initial tree β€” a string of length \(2(n-1)\) consisting of opening and ...
Output \(q + 1\) integers β€” the diameter of each constructed tree, in the order their descriptions have been written down.
The following figure shows each constructed tree and its description in the first example test:
Input: 5 5 (((()))) 4 5 3 4 5 6 3 6 2 5 | Output: 4 3 3 2 4 4
Master
3
1,278
704
122
11
1,116
C1
1116C1
C1. Alternating bits oracle
0
*special
Implement a quantum oracle on \(N\) qubits which checks whether the bits in the input vector \(\vec{x}\) alternate (i.e., implements the function \(f(\vec{x}) = 1\) if \(\vec{x}\) does not have a pair of adjacent bits in state 00 or 11).You have to implement an operation which takes the following inputs: an array of \(...
Beginner
1
1,198
0
0
11
1,987
C
1987C
C. Basil's Garden
1,200
dp; greedy
There are \(n\) flowers in a row, the \(i\)-th of them initially has a positive height of \(h_i\) meters.Every second, the wind will blow from the left, causing the height of some flowers to decrease.Specifically, every second, for each \(i\) from \(1\) to \(n\), in this order, the following happens: If \(i = n\) or \(...
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 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 10^5\)) β€” the number of flowers.The second line of ...
For each test case, output a single integer β€” the number of seconds that will pass before \(h_i=0\) for all \(1 \le i \le n\).
In the first test case, the flower heights change as follows: \([1, 1, 2] \rightarrow [1, 1, 1] \rightarrow [1, 1, 0] \rightarrow [1, 0, 0] \rightarrow [0, 0, 0]\).In the second test case, the flower heights change as follows: \([3, 1] \rightarrow [2, 0] \rightarrow [1, 0] \rightarrow [0, 0]\).
Input: 431 1 223 11957 4 4 3 2 | Output: 4 3 9 7
Easy
2
482
525
126
19
848
A
848A
A. From Y to Y
1,600
constructive algorithms
From beginning till end, this message has been waiting to be conveyed.For a given unordered multiset of n lowercase English letters (""multi"" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation n - 1 times: Remove any two elements s and t from...
The first and only line of input contains a non-negative integer k (0 ≀ k ≀ 100 000) β€” the required minimum cost.
Output a non-empty string of no more than 100 000 lowercase English letters β€” any multiset satisfying the requirements, concatenated to be a string.Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters.
For the multiset {'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'}, one of the ways to complete the process is as follows: {""ab"", ""a"", ""b"", ""a"", ""b"", ""a"", ""b""}, with a cost of 0; {""aba"", ""b"", ""a"", ""b"", ""a"", ""b""}, with a cost of 1; {""abab"", ""a"", ""b"", ""a"", ""b""}, with a cost of 1; {""abab"", ""a...
Input: 12 | Output: abababab
Medium
1
716
113
287
8
1,792
B
1792B
B. Stand-up Comedian
1,200
greedy; math
Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob.Eve prepared \(a_1 + a_2 + a_3 + a_4\) jokes to tell, grouped by their type: type 1: both Alice and Bob like them; type 2: Alice likes them, but Bob doesn't; type 3: Bob likes them, but Alice doesn't; type 4: nei...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of testcases.The only line of each testcase contains four integers \(a_1, a_2, a_3, a_4\) (\(0 \le a_1, a_2, a_3, a_4 \le 10^8\); \(a_1 + a_2 + a_3 + a_4 \ge 1\)) β€” the number of jokes of each type Eve prepared.
For each testcase, print a single integer β€” the maximum number of jokes Eve can tell before at least one of the spectators leaves or before she runs out of jokes.
In the first testcase, Eve only has jokes of the first type. Thus, there's no order to choose. She tells all her jokes, both Alice and Bob like them. Their mood becomes \(5\). The show ends after Eve runs out of jokes.In the second testcase, Eve only has jokes of the fourth type. Thus, once again no order to choose. Sh...
Input: 45 0 0 00 0 0 52 5 10 63 0 0 7 | Output: 5 1 15 7
Easy
2
947
294
162
17
1,459
A
1459A
A. Red-Blue Shuffle
800
math; probabilities
There are \(n\) cards numbered \(1, \ldots, n\). The card \(i\) has a red digit \(r_i\) and a blue digit \(b_i\) written on it.We arrange all \(n\) cards in random order from left to right, with all permutations of \(1, \ldots, n\) having the same probability. We then read all red digits on the cards from left to right...
The first line contains a single integer \(T\) (\(1 \leq T \leq 100\)) β€” the number of test cases.Descriptions of \(T\) test cases follow. Each test case description starts with a line containing a single integer \(n\) (\(1 \leq n \leq 1000\)) β€” the number of cards.The following line contains a string of \(n\) digits \...
Print \(T\) answers for the test cases in order, one per line.If Red has a strictly higher change to win, print ""RED"".If Blue has a strictly higher change to win, print ""BLUE"".If both players are equally likely to win, print ""EQUAL"".Note that all answers are case-sensitive.
Formally, let \(n_R\) be the number of permutations of cards \(1, \ldots, n\) such that the resulting numbers \(R\) and \(B\) satisfy \(R > B\). Similarly, let \(n_B\) be the number of permutations such that \(R < B\). If \(n_R > n_B\), you should print ""RED"". If \(n_R < n_B\), you should print ""BLUE"". If \(n_R = n...
Input: 3 3 777 111 3 314 159 5 09281 09281 | Output: RED BLUE EQUAL
Beginner
2
1,054
591
280
14
2,101
C
2101C
C. 23 Kingdom
2,200
binary search; brute force; data structures; greedy; ternary search; two pointers
The distance of a value \(x\) in an array \(c\), denoted as \(d_x(c)\), is defined as the largest gap between any two occurrences of \(x\) in \(c\). Formally, \(d_x(c) = \max(j - i)\) over all pairs \(i < j\) where \(c_i = c_j = x\). If \(x\) appears only once or not at all in \(c\), then \(d_x(c) = 0\). The beauty of ...
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 integer \(n\) (\(1\le n\le 2\cdot10^5\)) β€” the length of array \(a\).The second line of each test case cont...
For each test case, output a single integer representing the maximum possible beauty among all nice arrays.
In the first test case, if \(b = [1, 2, 1, 2]\), then \(d_1(b) = 3 - 1 = 2\) and \(d_2(b) = 4 - 2 = 2\), resulting in a beauty of \(2 + 2 = 4\). It can be proven that there are no nice arrays with a beauty greater than \(4\).In the second test case, both \(b = [1, 1]\) and \(b = [2, 2]\) are valid solutions with a beau...
Input: 441 2 1 222 2101 2 1 5 1 2 2 1 1 281 5 2 8 4 1 4 2 | Output: 4 1 16 16
Hard
6
708
503
107
21
77
A
77A
A. Heroes
1,400
brute force; implementation
The year of 2012 is coming...According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us m...
The first line contains a single non-negative integer n (0 ≀ n ≀ 42) β€” amount of liking between the heroes. Next n lines describe liking in the form ""p likes q"", meaning that the hero p likes the hero q (p β‰  q). Every liking is described in the input exactly once, no hero likes himself.In the last line are given thre...
Print two integers β€” the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).When calculating the second answer, the team division ...
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
Input: 3Troll likes DraculDracul likes AnkaSnowy likes Hexadecimal210 200 180 | Output: 30 3
Easy
2
2,139
574
499
0
910
C
910C
C. Minimum Sum
1,700
constructive algorithms; greedy; math
Petya has n positive integers a1, a2, ..., an. His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digi...
The first line contains a single integer n (1 ≀ n ≀ 1 000) β€” the number of Petya's numbers.Each of the following lines contains non-empty string si consisting of lowercase Latin letters from 'a' to 'j' β€” the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters.
Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.
In the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10, 23, 14]. The sum of them is equal to 47, which is the mi...
Input: 3abdeaj | Output: 47
Medium
3
693
301
237
9
1,375
E
1375E
E. Inversion SwapSort
2,500
constructive algorithms; greedy; sortings
Madeline has an array \(a\) of \(n\) integers. A pair \((u, v)\) of integers forms an inversion in \(a\) if: \(1 \le u < v \le n\). \(a_u > a_v\). Madeline recently found a magical paper, which allows her to write two indices \(u\) and \(v\) and swap the values \(a_u\) and \(a_v\). Being bored, she decided to write a l...
The first line of the input contains a single integer \(n\) (\(1 \le n \le 1000\)) β€” the length of the array.Next line contains \(n\) integers \(a_1,a_2,...,a_n\) \((1 \le a_i \le 10^9)\) β€” elements of the array.
Print -1 if no such list exists. Otherwise in the first line you should print a single integer \(m\) (\(0 \le m \le \dfrac{n(n-1)}{2}\)) β€” number of pairs in the list.The \(i\)-th of the following \(m\) lines should contain two integers \(u_i, v_i\) (\(1 \le u_i < v_i\le n\)).If there are multiple possible answers, you...
In the first sample test case the array will change in this order \([3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]\).In the second sample test case it will be \([1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]\).In the third sample test case the array is already sorted.
Input: 3 3 1 2 | Output: 2 1 3 1 2
Expert
3
875
212
342
13
178
D1
178D1
D1. Magic Squares
1,500
The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.The magic square is a matrix of size n Γ— n. The elements of this matrix are integers. T...
The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 ≀ ai ≀ 108), separated by single spaces.The input limitations for getting 20 points are: 1 ≀ n ≀ 3 The input limitations for getting 50 points are: 1 ≀ n ≀ 4 It is guaranteed that there are no more than 9 distinct numbers am...
The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed...
Input: 31 2 3 4 5 6 7 8 9 | Output: 152 7 69 5 14 3 8
Medium
0
970
387
342
1
1,253
A
1253A
A. Single Push
1,000
implementation
You're given two arrays \(a[1 \dots n]\) and \(b[1 \dots n]\), both of the same length \(n\).In order to perform a push operation, you have to choose three integers \(l, r, k\) satisfying \(1 \le l \le r \le n\) and \(k > 0\). Then, you will add \(k\) to elements \(a_l, a_{l+1}, \ldots, a_r\).For example, if \(a = [3, ...
The first line contains a single integer \(t\) (\(1 \le t \le 20\)) β€” the number of test cases in the input.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\ 000\)) β€” the number of elements in each array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n...
For each test case, output one line containing ""YES"" if it's possible to make arrays \(a\) and \(b\) equal by performing at most once the described operation or ""NO"" if it's impossible.You can print each letter in any case (upper or lower).
The first test case is described in the statement: we can perform a push operation with parameters \((l=3, r=5, k=2)\) to make \(a\) equal to \(b\).In the second test case, we would need at least two operations to make \(a\) equal to \(b\).In the third test case, arrays \(a\) and \(b\) are already equal.In the fourth t...
Input: 4 6 3 7 1 4 1 2 3 7 3 6 3 2 5 1 1 1 1 1 1 2 1 3 1 2 42 42 42 42 1 7 6 | Output: YES NO YES NO
Beginner
1
615
539
244
12
631
C
631C
C. Report
1,700
data structures; sortings
Each month Blake gets the report containing main economic indicators of the company ""Blake Technologies"". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands...
The first line of the input contains two integers n and m (1 ≀ n, m ≀ 200 000) β€” the number of commodities in the report and the number of managers, respectively.The second line contains n integers ai (|ai| ≀ 109) β€” the initial report before it gets to the first manager.Then follow m lines with the descriptions of the ...
Print n integers β€” the final report, which will be passed to Blake by manager number m.
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the rep...
Input: 3 11 2 32 2 | Output: 2 1 3
Medium
2
878
567
87
6
1,982
F
1982F
F. Sorting Problem Again
2,600
binary search; data structures; sortings
You have an array \(a\) of \(n\) elements. There are also \(q\) modifications of the array. Before the first modification and after each modification, you would like to know the following:What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array \(a\) to be completely so...
Each test consists of several test cases. The first line contains an integer \(t\) (\(1 \le t \le 10\)) β€” the number of test cases. Then follows the description 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 of each test case contains \(n\...
For each test case, output \(q + 1\) lines. Each line should contain \(2\) integers \(l, r\) β€” the boundaries of the minimum subarray, such that sorting it will make the array \(a\) completely sorted. If \(a\) is already sorted, then output \(l = -1\), \(r = -1\).
Let's consider the first test case: Initially, the array is sorted in non-decreasing order: \([2, 2, 3, 4, 5]\) After the first query, the array looks like this: \([\color{red}{2}, \color{red}{1}, 3, 4, 5]\). After the second query, the array looks like this: \([\color{red}{2}, \color{red}{1}, \color{red}{3}, \color{re...
Input: 252 2 3 4 532 14 11 151 2 3 4 591 42 35 23 11 15 14 13 12 1 | Output: -1 -1 1 2 1 4 3 4 -1 -1 1 3 1 3 1 5 1 5 2 5 2 5 2 5 2 5 -1 -1
Expert
3
898
912
264
19
627
A
627A
A. XOR Equation
1,700
dp; math
Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)?
The first line of the input contains two integers s and x (2 ≀ s ≀ 1012, 0 ≀ x ≀ 1012), the sum and bitwise xor of the pair of positive integers, respectively.
Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.
In the first sample, we have the following solutions: (2, 7), (3, 6), (6, 3), (7, 2).In the second sample, the only solutions are (1, 2) and (2, 1).
Input: 9 5 | Output: 4
Medium
2
133
159
104
6
601
E
601E
E. A Museum Robbery
2,800
data structures; dp
There's a famous museum in the city where KleofΓ‘Ε‘ lives. In the museum, n exhibits (numbered 1 through n) had been displayed for a long time; the i-th of those exhibits has value vi and mass wi. Then, the museum was bought by a large financial group and started to vary the exhibits. At about the same time, KleofΓ‘Ε‘... g...
The first line of the input contains two space-separated integers n and k (1 ≀ n ≀ 5000, 1 ≀ k ≀ 1000) β€” the initial number of exhibits in the museum and the maximum interesting mass of stolen exhibits. Then, n lines follow. The i-th of them contains two space-separated positive integers vi and wi (1 ≀ vi ≀ 1 000 000, ...
As the number of values s(m) can get large, output the answers to events of type 3 in a special format.For each event of type 3, consider the values s(m) computed for the question that KleofΓ‘Ε‘ asked in this event; print one line containing a single number where p = 107 + 19 and q = 109 + 7.Print the answers to events o...
In the first sample, the numbers of displayed exhibits and values s(1), ..., s(10) for individual events of type 3 are, in order: The values of individual exhibits are v1 = 30, v2 = 60, v3 = 5, v4 = 42, v5 = 20, v6 = 40 and their masses are w1 = 4, w2 = 6, w3 = 1, w4 = 5, w5 = 3, w6 = 6.In the second sample, the only q...
Input: 3 1030 460 65 1931 42 51 20 332 22 431 40 63 | Output: 556674384168191145947033915181541912
Master
2
1,254
954
376
6
573
A
573A
A. Bear and Poker
1,300
implementation; math; number theory
Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.Each player can double his bid any number of times and triple his bid any number of ti...
First line of input contains an integer n (2 ≀ n ≀ 105), the number of players.The second line contains n integer numbers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the bids of players.
Print ""Yes"" (without the quotes) if players can make their bids become equal, or ""No"" otherwise.
In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.It can be shown that in the second sample test there is no way to make all bids equal.
Input: 475 150 75 50 | Output: Yes
Easy
3
444
175
100
5
1,991
H
1991H
H. Prime Split Game
3,300
bitmasks; dp; fft; games; math; number theory
Alice and Bob are playing a game with \(n\) piles of stones, where the \(i\)-th pile has \(a_i\) stones. Players take turns making moves, with Alice going first.On each move, the player does the following three-step process: Choose an integer \(k\) (\(1 \leq k \leq \frac n 2\)). Note that the value of \(k\) can be diff...
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. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of piles of stones.The second line ...
For each test case, output ""Alice"" (without quotes) if Alice wins and ""Bob"" (without quotes) otherwise.You can output each letter in any case (upper or lower). For example, the strings ""alIcE"", ""Alice"", and ""alice"" will all be considered identical.
In the first test case, there are \(2\) piles of stones with \(2\) and \(1\) stones respectively. Since neither \(1\) nor \(2\) can be split into two prime numbers, Alice cannot make a move, so Bob wins.In the second test case, there are \(3\) piles of stones with \(3\), \(5\), and \(7\) stones respectively. Alice can ...
Input: 422 133 5 744 6 8 1058 8 8 8 8 | Output: Bob Alice Alice Bob
Master
6
612
549
258
19
835
A
835A
A. Key races
800
math
Two boys decided to compete in text typing on the site ""Key races"". During the competition, they have to type a text consisting of s characters. The first participant types one character in v1 milliseconds and has ping t1 milliseconds. The second participant types one character in v2 milliseconds and has ping t2 mill...
The first line contains five integers s, v1, v2, t1, t2 (1 ≀ s, v1, v2, t1, t2 ≀ 1000) β€” the number of characters in the text, the time of typing one character for the first participant, the time of typing one character for the the second participant, the ping of the first participant and the ping of the second partici...
If the first participant wins, print ""First"". If the second participant wins, print ""Second"". In case of a draw print ""Friendship"".
In the first example, information on the success of the first participant comes in 7 milliseconds, of the second participant β€” in 14 milliseconds. So, the first wins.In the second example, information on the success of the first participant comes in 11 milliseconds, of the second participant β€” in 5 milliseconds. So, th...
Input: 5 1 2 1 2 | Output: First
Beginner
1
962
325
137
8
1,238
F
1238F
F. The Maximum Subtree
2,200
dfs and similar; dp; graphs; trees
Assume that you have \(k\) one-dimensional segments \(s_1, s_2, \dots s_k\) (each segment is denoted by two integers β€” its endpoints). Then you can build the following graph on these segments. The graph consists of \(k\) vertexes, and there is an edge between the \(i\)-th and the \(j\)-th vertexes (\(i \neq j\)) if and...
The first line contains one integer \(q\) (\(1 \le q \le 15 \cdot 10^4\)) β€” the number of the queries. The first line of each query contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β€” the number of vertices in the tree.Each of the next \(n - 1\) lines contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\))...
For each query print one integer β€” the maximum size of a good subtree of the given tree.
In the first query there is a good subtree of size \(8\). The vertices belonging to this subtree are \({9, 4, 10, 2, 5, 1, 6, 3}\).
Input: 1 10 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10 | Output: 8
Hard
4
923
496
88
12
1,866
D
1866D
D. Digital Wallet
2,300
dp; greedy
There are \(N\) arrays, each array has \(M\) positive integer elements The \(j\)-th element of the \(i\)-th array is \(A_{i,j}\).Initially, Chaneka's digital wallet contains \(0\) money. Given an integer \(K\). Chaneka will do \(M-K+1\) operations. In the \(p\)-th operation, Chaneka does the following procedure: Choose...
The first line contains three integers \(N\), \(M\), and \(K\) (\(1 \leq N \leq 10\); \(1 \leq M \leq 10^5\); \(1 \leq K \leq \min(10, M)\)) β€” the number of arrays, the size of each array, and the constant that describes the operation constraints.The \(i\)-th of the next \(N\) lines contains \(M\) integers \(A_{i,1}, A...
Output an integer representing the maximum total money that can be earned.
In the first example, the following is a sequence of operations of one optimal strategy: Choosing element \(A_{1, 1}\) with a value of \(10\). Choosing element \(A_{3, 2}\) with a value of \(8\). Choosing element \(A_{2, 3}\) with a value of \(9\). So the total money earned is \(10+8+9=27\).In the second example, the f...
Input: 3 3 1 10 4 2 8 1 9 4 8 2 | Output: 27
Expert
2
611
414
74
18
712
A
712A
A. Memory and Crow
800
implementation; math
There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure: The crow sets ai initially 0. The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus, ai = bi - bi + 1 + bi + 2 - bi ...
The first line of the input contains a single integer n (2 ≀ n ≀ 100 000) β€” the number of integers written in the row.The next line contains n, the i'th of which is ai ( - 109 ≀ ai ≀ 109) β€” the value of the i'th number.
Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.
In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and - 4 = 4 - 6 + 1 - 3.In the second sample test, the sequence 1, - 3, 4, 11, ...
Input: 56 -4 8 -2 3 | Output: 2 4 6 1 3
Beginner
2
472
219
138
7
1,791
G2
1791G2
G2. Teleporters (Hard Version)
1,900
binary search; greedy; sortings
The only difference between the easy and hard versions are the locations you can teleport to.Consider the points \(0,1,\dots,n+1\) on the number line. There is a teleporter located on each of the points \(1,2,\dots,n\). At point \(i\), you can do the following: Move left one unit: it costs \(1\) coin. Move right one un...
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(c\) (\(1 \leq n \leq 2\cdot10^5\); \(1 \leq c \leq 10^9\)) β€” the length of...
For each test case, output the maximum number of teleporters you can use.
In the first test case, you can move one unit to the right, use the teleporter at index \(1\) and teleport to point \(n+1\), move one unit to the left and use the teleporter at index \(5\). You are left with \(6-1-1-1-1 = 2\) coins, and wherever you teleport, you won't have enough coins to use another teleporter. You h...
Input: 105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000 | Output: 2 3 0 1 3 2 1 1 2 2
Hard
3
650
619
73
17
2,074
B
2074B
B. The Third Side
800
geometry; greedy; math
The pink soldiers have given you a sequence \(a\) consisting of \(n\) positive integers.You must repeatedly perform the following operation until there is only \(1\) element left. Choose two distinct indices \(i\) and \(j\). Then, choose a positive integer value \(x\) such that there exists a non-degenerate triangle\(^...
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 integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_...
For each test case, output the maximum possible value of the only last element on a separate line.
On the first test case, there is already only one element. The value of the only last element is \(10\).On the second test case, \(a\) is initially \([998,244,353]\). The following series of operations is valid: Erase \(a_2=244\) and \(a_3=353\), and append \(596\) to the end of \(a\). \(a\) is now \([998,596]\). Erase...
Input: 41103998 244 35351 2 3 4 599 9 8 2 4 4 3 5 3 | Output: 10 1593 11 39
Beginner
3
679
493
98
20
1,990
E1
1990E1
E1. Catch the Mole(Easy Version)
2,500
binary search; data structures; dfs and similar; interactive; trees
This is the easy version of the problem. The only difference is the limit on the number of queries.This is an interactive problem.You are given a tree of \(n\) nodes with node \(1\) as its root node.There is a hidden mole in one of the nodes. To find its position, you can pick an integer \(x\) (\(1 \le x \le n\)) to ma...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.
In the first test case, the mole is in node \(2\) initially.For the query ""? 2"", the jury returns \(1\) because the mole is in subtree \(2\). After this query, the mole does not move.The answer \(2\) is the current node where the mole is located, so the answer is considered correct.In the second test case, the mole i...
Input: 2 2 1 2 1 6 1 2 1 3 1 4 4 5 5 6 0 0 1 | Output: ? 2 ! 2 ? 2 ? 6 ? 4 ! 4
Expert
5
673
160
0
19
1,239
A
1239A
A. Ivan the Fool and the Probability Theory
1,700
combinatorics; dp; math
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area.To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of \(n\)...
The only line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 100\,000\)), the number of rows and the number of columns of the field.
Print one integer, the number of random pictures modulo \(10^9 + 7\).
The picture below shows all possible random pictures of size \(2\) by \(3\).
Input: 2 3 | Output: 8
Medium
3
932
141
69
12
1,330
A
1330A
A. Dreamoon and Ranking Collection
900
implementation
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from \(1\) to \(54\) after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in \(n\) Codeforces rounds. His place in the first round is \...
The first line contains an integer \(t\) (\(1 \leq t \leq 5\)) denoting the number of test cases in the input.Each test case contains two lines. The first line contains two integers \(n, x\) (\(1 \leq n, x \leq 100\)). The second line contains \(n\) positive non-zero integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \le...
For each test case print one line containing the largest \(v\), such that it is possible that after \(x\) other contests, for each \(1 \leq i \leq v\), there will exist a contest where this person took the \(i\)-th place.
The first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place \(1,2,\ldots,99\) and place \(101\) on them in some order, to collect places \(1,2,\ldots,101\).
Input: 5 6 2 3 1 1 5 7 10 1 100 100 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 57 80 60 40 20 | Output: 5 101 2 2 60
Beginner
1
1,053
329
221
13
383
D
383D
D. Antimatter
2,300
dp
Iahub accidentally discovered a secret lab. He found there n devices ordered in a line, numbered from 1 to n from left to right. Each device i (1 ≀ i ≀ n) can create either ai units of matter or ai units of antimatter. Iahub wants to choose some contiguous subarray of devices in the lab, specify the production mode for...
The first line contains an integer n (1 ≀ n ≀ 1000). The second line contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1000).The sum a1 + a2 + ... + an will be less than or equal to 10000.
Output a single integer, the number of ways Iahub can take a photo, modulo 1000000007 (109 + 7).
The possible photos are [1+, 2-], [1-, 2+], [2+, 3-], [2-, 3+], [3+, 4-], [3-, 4+], [1+, 2+, 3-, 4-], [1+, 2-, 3+, 4-], [1+, 2-, 3-, 4+], [1-, 2+, 3+, 4-], [1-, 2+, 3-, 4+] and [1-, 2-, 3+, 4+], where ""i+"" means that the i-th element produces matter, and ""i-"" means that the i-th element produces antimatter.
Input: 41 1 1 1 | Output: 12
Expert
1
879
184
96
3
1,932
G
1932G
G. Moving Platforms
2,300
graphs; math; number theory; shortest paths
There is a game where you need to move through a labyrinth. The labyrinth consists of \(n\) platforms, connected by \(m\) passages. Each platform is at some level \(l_i\), an integer number from \(0\) to \(H - 1\). In a single step, if you are currently on platform \(i\), you can stay on it, or move to another platform...
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains three integers \(n\), \(m\), and \(H\) (\(2 \le n \le 10^5\), \(1 \le m \le 10^5\), \(1 \le H \le 10^9\)).The second line co...
For each test case, print a single integer, the minimum number of steps needed to get from platform \(1\) to platform \(n\).If it is impossible to get to platform \(n\), print \(-1\).
This is how levels of the platforms change, and what actions we need to perform in the first example.Platform 1Platform 2Platform 3ActionStep 1194Stay on the platform 1Step 2324Stay on the platform 1Step 3554Move to the platform 2Step 4784Stay on the platform 2Step 5914Stay on the platform 2Step 6144Move to the platfor...
Input: 33 3 101 9 42 3 01 23 21 32 1 101 24 61 28 7 2522 14 5 3 10 14 11 19 5 4 10 7 16 18 182 86 33 57 52 61 44 7 | Output: 6 -1 52
Expert
4
700
901
183
19
1,798
B
1798B
B. Three Sevens
1,000
brute force; data structures; greedy; implementation
Lottery ""Three Sevens"" was held for \(m\) days. On day \(i\), \(n_i\) people with the numbers \(a_{i, 1}, \ldots, a_{i, n_i}\) participated in the lottery.It is known that in each of the \(m\) days, only one winner was selected from the lottery participants. The lottery winner on day \(i\) was not allowed to particip...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\,000\)). The description of the test cases follows.The first line of each test case contains a single integer \(m\) (\(1 \le m \le 50\,000\)) β€” the number of days in which the lottery was held.Next, for each...
For each test case, if there is no solution, print a single integer \(-1\).Otherwise, print \(m\) integers \(p_1, p_2, \ldots, p_m\) (\(1 \le p_i \le 50\,000\)) β€” lottery winners on days from \(1\) to \(m\). If there are multiple solutions, print any of them.
In the first test case, one of the answers is \([8, 2, 1]\) since the participant with the number \(8\) participated on day \(1\), but did not participate on days \(2\) and \(3\); the participant with the number \(2\) participated on day \(2\), but did not participate on day \(3\); and the participant with the number \...
Input: 3341 2 4 832 9 121 4221 222 1441 2 3 4111413 | Output: 8 2 1 -1 2 1 4 3
Beginner
4
564
868
259
17
2,046
F2
2046F2
F2. Yandex Cuneiform (Hard Version)
3,500
constructive algorithms; data structures; greedy; implementation
This is the hard version of the problem. The difference between the versions is that in this version, there is no restriction on the number of question marks. You can hack only if you solved all versions of this problem. For a long time, no one could decipher Sumerian cuneiform. However, it has finally succumbed to pre...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5 \cdot 10^4\)). The description of the test cases follows. Each test case consists of a single line containing a template of length \(n\) (\(3 \leq n < 2 \cdot 10^5\), \(n \bmod 3 = 0\)), consisting only of ch...
For each test case, output a single line containing 'NO' if it is not possible to obtain a cuneiform from the given template.Otherwise, output 'YES' on the first line, and on the second line, any obtainable cuneiform. After that, you need to output the sequence of operations that leads to the cuneiform you printed.A se...
In the second example, the string is transformed like this: \("""" \to \mathtt{YDX} \to \mathtt{YDXDYX}\).
Input: 4???Y??D?X???D??DXYXYX | Output: YES YDX X 0 D 0 Y 0 YES YDXDYX X 0 Y 0 D 1 X 2 D 3 Y 4 YES YDX Y 0 D 1 X 2 NO
Master
4
1,223
444
1,016
20