Dataset Viewer
Auto-converted to Parquet
contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
117
B
117B
B. Very Interesting Game
1,800
brute force; number theory
In a very ancient country the following game was popular. Two people play the game. Initially first player writes a string s1, consisting of exactly nine digits and representing a number that does not exceed a. After that second player looks at s1 and writes a string s2, consisting of exactly nine digits and representing a number that does not exceed b. Here a and b are some given constants, s1 and s2 are chosen by the players. The strings are allowed to contain leading zeroes.If a number obtained by the concatenation (joining together) of strings s1 and s2 is divisible by mod, then the second player wins. Otherwise the first player wins. You are given numbers a, b, mod. Your task is to determine who wins if both players play in the optimal manner. If the first player wins, you are also required to find the lexicographically minimum winning move.
The first line contains three integers a, b, mod (0 ≀ a, b ≀ 109, 1 ≀ mod ≀ 107).
If the first player wins, print ""1"" and the lexicographically minimum string s1 he has to write to win. If the second player wins, print the single number ""2"".
The lexical comparison of strings is performed by the < operator in modern programming languages. String x is lexicographically less than string y if exists such i (1 ≀ i ≀ 9), that xi < yi, and for any j (1 ≀ j < i) xj = yj. These strings always have length 9.
Input: 1 10 7 | Output: 2
Medium
2
858
81
163
1
1,007
A
1007A
A. Reorder the Array
1,300
combinatorics; data structures; math; sortings; two pointers
You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.For instance, if we are given an array \([10, 20, 30, 40]\), we can permute it so that it becomes \([20, 40, 10, 30]\). Then on the first and the second positions the integers became larger (\(20>10\), \(40>20\)) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals \(2\). Read the note for the first example, there is one more demonstrative test case.Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the length of the array.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)) β€” the elements of the array.
Print a single integer β€” the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.
In the first sample, one of the best permutations is \([1, 5, 5, 3, 10, 1, 1]\). On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.In the second sample, there is no way to increase any element with a permutation, so the answer is 0.
Input: 710 1 1 1 5 5 3 | Output: 4
Easy
5
816
219
172
10
1,176
A
1176A
A. Divide it!
800
brute force; greedy; implementation
You are given an integer \(n\).You can perform any of the following operations with this number an arbitrary (possibly, zero) number of times: Replace \(n\) with \(\frac{n}{2}\) if \(n\) is divisible by \(2\); Replace \(n\) with \(\frac{2n}{3}\) if \(n\) is divisible by \(3\); Replace \(n\) with \(\frac{4n}{5}\) if \(n\) is divisible by \(5\). For example, you can replace \(30\) with \(15\) using the first operation, with \(20\) using the second operation or with \(24\) using the third operation.Your task is to find the minimum number of moves required to obtain \(1\) from \(n\) or say that it is impossible to do it.You have to answer \(q\) independent queries.
The first line of the input contains one integer \(q\) (\(1 \le q \le 1000\)) β€” the number of queries.The next \(q\) lines contain the queries. For each query you are given the integer number \(n\) (\(1 \le n \le 10^{18}\)).
Print the answer for each query on a new line. If it is impossible to obtain \(1\) from \(n\), print -1. Otherwise, print the minimum number of moves required to do it.
Input: 7 1 10 25 30 14 27 1000000000000000000 | Output: 0 4 6 6 -1 6 72
Beginner
3
669
224
168
11
1,970
E1
1970E1
E1. Trails (Easy)
1,800
dp
Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are \(m\) cabins, numbered 1 to \(m\). Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin \(i\) is connected with \(s_i\) short trails and \(l_i\) long trails to the lake.Each day, Harry walks a trail from the cabin where he currently is to Lake Geneva, and then from there he walks a trail to any of the \(m\) cabins (including the one he started in). However, as he has to finish the hike in a day, at least one of the two trails has to be short.How many possible combinations of trails can Harry take if he starts in cabin 1 and walks for \(n\) days?Give the answer modulo \(10^9 + 7\).
The first line contains the integers \(m\) and \(n\).The second line contains \(m\) integers, \(s_1, \dots, s_m\), where \(s_i\) is the number of short trails between cabin \(i\) and Lake Geneva.The third and last line contains \(m\) integers, \(l_1, \dots, l_m\), where \(l_i\) is the number of long trails between cabin \(i\) and Lake Geneva.We have the following constraints:\(0 \le s_i, l_i \le 10^3\).\(1 \le m \le 10^2\).\(1 \le n \le 10^3\).
The number of possible combinations of trails, modulo \(10^9 + 7\).
Input: 3 21 0 10 1 1 | Output: 18
Medium
1
754
448
67
19
556
A
556A
A. Case of the Zeros and Ones
900
greedy
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
First line of the input contains a single integer n (1 ≀ n ≀ 2Β·105), the length of the string that Andreid has.The second line contains the string of length n consisting only from zeros and ones.
Output the minimum length of the string that may remain after applying the described operations several times.
In the first sample test it is possible to change the string like the following: .In the second sample test it is possible to change the string like the following: .In the third sample test it is possible to change the string like the following: .
Input: 41100 | Output: 0
Beginner
1
638
195
110
5
1,002
A3
1002A3
A3. Generate superposition of two basis states
1,500
*special
You are given N qubits (1 ≀ N ≀ 8) in zero state . You are also given two bitstrings bits0 and bits1 which describe two different basis states on N qubits and .Your task is to generate a state which is an equal superposition of the given basis states:You have to implement an operation which takes the following inputs: an array of qubits qs, two arrays of Boolean values bits0 and bits1 representing the basis states and . These arrays will have the same length as the array of qubits. bits0 and bits1 will differ in at least one position.The operation doesn't have an output; its ""output"" is the state in which it leaves the qubits.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[], bits0 : Bool[], bits1 : Bool[]) : () { body { // your code here } }}
Medium
1
866
0
0
10
172
B
172B
B. Pseudorandom Sequence Period
1,200
*special; implementation; number theory
Polycarpus has recently got interested in sequences of pseudorandom numbers. He learned that many programming languages generate such sequences in a similar way: (for i β‰₯ 1). Here a, b, m are constants, fixed for the given realization of the pseudorandom numbers generator, r0 is the so-called randseed (this value can be set from the program using functions like RandSeed(r) or srand(n)), and denotes the operation of taking the remainder of division.For example, if a = 2, b = 6, m = 12, r0 = 11, the generated sequence will be: 4, 2, 10, 2, 10, 2, 10, 2, 10, 2, 10, ....Polycarpus realized that any such sequence will sooner or later form a cycle, but the cycle may occur not in the beginning, so there exist a preperiod and a period. The example above shows a preperiod equal to 1 and a period equal to 2.Your task is to find the period of a sequence defined by the given values of a, b, m and r0. Formally, you have to find such minimum positive integer t, for which exists such positive integer k, that for any i β‰₯ k: ri = ri + t.
The single line of the input contains four integers a, b, m and r0 (1 ≀ m ≀ 105, 0 ≀ a, b ≀ 1000, 0 ≀ r0 < m), separated by single spaces.
Print a single integer β€” the period of the sequence.
The first sample is described above. In the second sample the sequence is (starting from the first element): 0, 3, 4, 1, 0, 3, 4, 1, 0, ...In the third sample the sequence is (starting from the first element): 33, 24, 78, 78, 78, 78, ...
Input: 2 6 12 11 | Output: 2
Easy
3
1,036
138
52
1
1,726
A
1726A
A. Mainak and Array
900
greedy; math
Mainak has an array \(a_1, a_2, \ldots, a_n\) of \(n\) positive integers. He will do the following operation to this array exactly once: Pick a subsegment of this array and cyclically rotate it by any amount. Formally, he can do the following exactly once: Pick two integers \(l\) and \(r\), such that \(1 \le l \le r \le n\), and any positive integer \(k\). Repeat this \(k\) times: set \(a_l=a_{l+1}, a_{l+1}=a_{l+2}, \ldots, a_{r-1}=a_r, a_r=a_l\) (all changes happen at the same time). Mainak wants to maximize the value of \((a_n - a_1)\) after exactly one such operation. Determine the maximum value of \((a_n - a_1)\) that he can obtain.
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 50\)) β€” the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 999\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).
For each test case, output a single integer β€” the maximum value of \((a_n - a_1)\) that Mainak can obtain by doing the operation exactly once.
In the first test case, we can rotate the subarray from index \(3\) to index \(6\) by an amount of \(2\) (i.e. choose \(l = 3\), \(r = 6\) and \(k = 2\)) to get the optimal array: $$$\([1, 3, \underline{9, 11, 5, 7}] \longrightarrow [1, 3, \underline{5, 7, 9, 11}]\)\( So the answer is \)a_n - a_1 = 11 - 1 = 10\(. In the second testcase, it is optimal to rotate the subarray starting and ending at index \)1\( and rotating it by an amount of \)2\(. In the fourth testcase, it is optimal to rotate the subarray starting from index \)1\( to index \)4\( and rotating it by an amount of \)3\(. So the answer is \)8 - 1 = 7$$$.
Input: 561 3 9 11 5 712039 99 99942 1 8 132 1 5 | Output: 10 0 990 7 4
Beginner
2
644
454
142
17
25
A
25A
A. IQ test
1,300
brute force
Bob is preparing to pass IQ test. The most frequent task in this test is to find out which one of the given n numbers differs from the others. Bob observed that one number usually differs from the others in evenness. Help Bob β€” to check his answers, he needs a program that among the given n numbers finds one that is different in evenness.
The first line contains integer n (3 ≀ n ≀ 100) β€” amount of numbers in the task. The second line contains n space-separated natural numbers, not exceeding 100. It is guaranteed, that exactly one of these numbers differs from the others in evenness.
Output index of number that differs from the others in evenness. Numbers are numbered from 1 in the input order.
Input: 52 4 7 8 10 | Output: 3
Easy
1
340
248
112
0
172
E
172E
E. BHTML+BCSS
2,200
*special; dfs and similar; expression parsing
This problem is about imaginary languages BHTML and BCSS, which slightly resemble HTML and CSS. Read the problem statement carefully as the resemblance is rather slight and the problem uses very simplified analogs.You are given a BHTML document that resembles HTML but is much simpler. It is recorded as a sequence of opening and closing tags. A tag that looks like ""<tagname>"" is called an opening tag and a tag that looks like ""</tagname>"" is called a closing tag. Besides, there are self-closing tags that are written as ""<tagname/>"" and in this problem they are fully equivalent to ""<tagname></tagname>"". All tagnames in this problem are strings consisting of lowercase Latin letters with length from 1 to 10 characters. Tagnames of different tags may coincide.The document tags form a correct bracket sequence, that is, we can obtain an empty sequence from the given one using the following operations: remove any self-closing tag ""<tagname/>"", remove a pair of an opening and a closing tag that go consecutively (in this order) and have the same names. In other words, remove substring ""<tagname></tagname>"". For example, you may be given such document: ""<header><p><a/><b></b></p></header><footer></footer>"" but you may not be given documents ""<a>"", ""<a></b>"", ""</a><a>"" or ""<a><b></a></b>"".Obviously, for any opening tag there is the only matching closing one β€” each such pair is called an element. A self-closing tag also is an element. Let's consider that one element is nested inside another one, if tags of the first element are between tags of the second one. An element is not nested to itself. For instance, in the example above element ""b"" is nested in ""header"" and in ""p"", but it isn't nested in ""a"" and ""footer"", also it isn't nested to itself (""b""). Element ""header"" has three elements nested in it, and ""footer"" has zero.We need the BCSS rules to apply styles when displaying elements of the BHTML documents. Each rule is recorded as a subsequence of words ""x1 x2 ... xn"". This rule has effect over all such elements t, which satisfy both conditions from the list: there is a sequence of nested elements with tagnames ""x1"", ""x2"", ..., ""xn"" (that is, the second element is nested in the first one, the third element is nested in the second one and so on), this sequence ends with element t (i.e. tagname of element t equals ""xn""). For example, element ""b"" meets the conditions of the rule ""a b"" if for element ""b"" exists element ""a"" in which it is nested. Element ""c"" meets the conditions of the rule ""a b b c"", if three elements exist: ""a"", ""b"", ""b"", and in the chain ""a""-""b""-""b""-""c"" each following element is nested in the previous one.Given a BHTML document and a set of BCSS rules, write a program that determines the number of elements that meet the conditions of each rule.
The first line of the input contains a BHTML-document. The document has length from 4 to 106 characters. The document has a correct structure, doesn't contain spaces or any other unnecessary characters. Tagnames consist of lowercase Latin letters, their lengths are from 1 to 10 characters.The second line contains an integer m (1 ≀ m ≀ 200) β€” the number of queries. Then m lines contain the queries, one per line. Each query is a sequence x1, x2, ..., xn, where xi is the i-th element of the query, and n (1 ≀ n ≀ 200) is the number of elements in the query. The elements are separated by single spaces. Each query doesn't begin with and doesn't end with a space. Each query element is a sequence of lowercase Latin letters with length from 1 to 10.
Print m lines, the j-th line should contain the number of elements of the document that correspond to the j-th BCSS-rule. If there are no such elements at all, print on the line 0.
Input: <a><b><b></b></b></a><a><b></b><b><v/></b></a><b></b>4aa b ba bb a | Output: 2140
Hard
3
2,872
750
180
1
2,104
E
2104E
E. Unpleasant Strings
1,700
binary search; dp; greedy; strings
Let's call a letter allowed if it is a lowercase letter and is one of the first \(k\) letters of the Latin alphabet.You are given a string \(s\) of length \(n\), consisting only of allowed letters.Let's call a string \(t\) pleasant if \(t\) is a subsequence of \(s\).You are given \(q\) strings \(t_1, t_2, \dots, t_q\). All of them consist only of allowed letters. For each string \(t_i\), calculate the minimum number of allowed letters you need to append to it on the right so that it stops being pleasant.A sequence \(t\) is a subsequence of a sequence \(s\) if \(t\) can be obtained from \(s\) by the deletion of several (possibly, zero or all) element from arbitrary positions.
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 10^6\); \(1 \le k \le 26\)) β€” the length of the string \(s\) and the number of allowed letters.The second line contains the string \(s\), consisting of \(n\) lowercase Latin letters. Each character of the string is one of the first \(k\) letters of the Latin alphabet.The third line contains one integer \(q\) (\(1 \le q \le 2 \cdot 10^5\)) β€” the number of queries.The next \(q\) lines contain queries: one query per line. The \(i\)-th line contains the string \(t_i\), consisting only of allowed letters.Additional constraint on input: the total length of all \(t_i\) does not exceed \(10^6\).
For each query, output one integer β€” the minimum number of allowed letters that need to be appended to the string on the right so that it stops being pleasant.
In the first example: The string cc is already unpleasant, so nothing needs to be appended to it; bcb is pleasant, so at least one letter needs to be appended to the right: bcba will not work, but bcbb and bcbc are unpleasant. To b, at least two letters need to be appended, since ba, bb, and bc are pleasant. For example, we can obtain an unpleasant string bbb.
Input: 7 3abacaba3ccbcbb | Output: 0 1 2
Medium
4
683
662
159
21
1,994
G
1994G
G. Minecraft
2,600
bitmasks; brute force; dp; graphs; math
After winning another Bed Wars game, Masha and Olya wanted to relax and decided to play a new game. Masha gives Olya an array \(a\) of length \(n\) and a number \(s\). Now Olya's task is to find a non-negative number \(x\) such that \(\displaystyle\sum_{i=1}^{n} a_i \oplus x = s\). But she is very tired after a tight round, so please help her with this.But this task seemed too simple to them, so they decided to make the numbers larger (up to \(2^k\)) and provide you with their binary representation.
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then follows the description of the test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n, k, n \cdot k \le 2 \cdot 10^6\)) β€” the length of the array \(a\) and the length of the binary representation of all numbers.The second line contains a string of length \(k\), consisting of zeros and ones β€” the binary representation of the number \(s\), starting from the most significant bits.The next \(n\) lines also contain strings of length \(k\), consisting of zeros and ones, the \(i\)-th of these strings contains the binary representation of the number \(a_i\), starting from the most significant bits.It is guaranteed that the sum of the values \(n \cdot k\) for all test cases does not exceed \(2 \cdot 10^6\).
For each test case, output a string of length \(k\) on a separate line, consisting of zeros or ones β€” the binary representation of any suitable number \(x\) (\(x \ge 0\)), starting from the most significant bits, or \(-1\) if such \(x\) does not exist.
In the first test case, \(s = 11, a = [14, 6, 12, 15]\), if \(x = 14\), then \(\displaystyle\sum_{i=1}^{n} a_i \oplus x = (14 \oplus 14) + (6 \oplus 14) + (12 \oplus 14) + (15 \oplus 14) = 0 + 8 + 2 + 1 = 11 = s\).In the second test case, \(s = 41, a = [191, 158]\), if \(x = 154\), then \(\displaystyle\sum_{i=1}^{n} a_i \oplus x = (191 \oplus 154) + (158 \oplus 154) = 37 + 4 = 41 = s\).
Input: 44 501011011100011001100011112 80010100110111111100111105 40101001000000000001000116 500011101101100101010111001001110000 | Output: 01110 10011010 0010 -1
Expert
5
504
890
252
19
124
A
124A
A. The number of positions
1,000
math
Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing behind him. Find the number of different positions Petr can occupy.
The only line contains three integers n, a and b (0 ≀ a, b < n ≀ 100).
Print the single number β€” the number of the sought positions.
The possible positions in the first sample are: 2 and 3 (if we number the positions starting with 1).In the second sample they are 3, 4 and 5.
Input: 3 1 1 | Output: 2
Beginner
1
264
70
61
1
283
D
283D
D. Cows and Cool Sequences
2,400
dp; math; number theory
Bessie and the cows have recently been playing with ""cool"" sequences and are trying to construct some. Unfortunately they are bad at arithmetic, so they need your help!A pair (x, y) of positive integers is ""cool"" if x can be expressed as the sum of y consecutive integers (not necessarily positive). A sequence (a1, a2, ..., an) is ""cool"" if the pairs (a1, a2), (a2, a3), ..., (an - 1, an) are all cool. The cows have a sequence of n positive integers, a1, a2, ..., an. In one move, they may replace some ai with any other positive integer (there are no other limits on the new value of ai). Determine the smallest number of moves needed to make the resulting sequence cool.
The first line contains a single integer, n (2 ≀ n ≀ 5000). The next line contains n space-separated integers, a1, a2, ..., an (1 ≀ ai ≀ 1015).Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
A single integer, the minimum number of ai that must be changed to make the sequence cool.
In the first sample, the sequence is already cool, so we don't need to change any elements. In the second sample, we can change a2 to 5 and a3 to 10 to make (20, 5, 10, 4) which is cool. This changes 2 elements.
Input: 36 4 1 | Output: 0
Expert
3
680
289
90
2
764
A
764A
A. Taymyr is calling you
800
brute force; implementation; math
Comrade Dujikov is busy choosing artists for Timofey's birthday and is recieving calls from Taymyr from Ilia-alpinist.Ilia-alpinist calls every n minutes, i.e. in minutes n, 2n, 3n and so on. Artists come to the comrade every m minutes, i.e. in minutes m, 2m, 3m and so on. The day is z minutes long, i.e. the day consists of minutes 1, 2, ..., z. How many artists should be killed so that there are no artists in the room when Ilia calls? Consider that a call and a talk with an artist take exactly one minute.
The only string contains three integers β€” n, m and z (1 ≀ n, m, z ≀ 104).
Print single integer β€” the minimum number of artists that should be killed so that there are no artists in the room when Ilia calls.
Taymyr is a place in the north of Russia.In the first test the artists come each minute, as well as the calls, so we need to kill all of them.In the second test we need to kill artists which come on the second and the fourth minutes.In the third test β€” only the artist which comes on the sixth minute.
Input: 1 1 10 | Output: 10
Beginner
3
511
73
132
7
786
D
786D
D. Rap God
3,400
data structures; dfs and similar; hashing; strings; trees
Rick is in love with Unity. But Mr. Meeseeks also love Unity, so Rick and Mr. Meeseeks are ""love rivals"". Unity loves rap, so it decided that they have to compete in a rap game (battle) in order to choose the best. Rick is too nerds, so instead he's gonna make his verse with running his original algorithm on lyrics ""Rap God"" song. His algorithm is a little bit complicated. He's made a tree with n vertices numbered from 1 to n and there's a lowercase english letter written on each edge. He denotes str(a, b) to be the string made by writing characters on edges on the shortest path from a to b one by one (a string of length equal to distance of a to b). Note that str(a, b) is reverse of str(b, a) and str(a, a) is empty. In order to make the best verse he can, he needs to answer some queries, but he's not a computer scientist and is not able to answer those queries, so he asked you to help him. Each query is characterized by two vertices x and y (x β‰  y). Answer to this query is the number of vertices like z such that z β‰  x, z β‰  y and str(x, y) is lexicographically larger than str(x, z).String x = x1x2...x|x| is lexicographically larger than string y = y1y2...y|y|, if either |x| > |y| and x1 = y1, x2 = y2, ..., x|y| = y|y|, or exists such number r (r < |x|, r < |y|), that x1 = y1, x2 = y2, ..., xr = yr and xr + 1 > yr + 1. Characters are compared like their ASCII codes (or alphabetic order).Help Rick get the girl (or whatever gender Unity has).
The first line of input contain two integers n and q (2 ≀ n ≀ 20000, 1 ≀ q ≀ 20000) β€” number of vertices in tree and number of queries respectively.The next n - 1 lines contain the edges. Each line contains two integers v and u (endpoints of the edge) followed by an English lowercase letter c (1 ≀ v, u ≀ n, v β‰  u).The next q line contain the queries. Each line contains two integers x and y (1 ≀ x, y ≀ n, x β‰  y).
Print the answer for each query in one line.
Here's the tree of first sample testcase: Here's the tree of second sample testcase: In this test: str(8, 1) = poo str(8, 2) = poe str(8, 3) = po str(8, 4) = pop str(8, 5) = popd str(8, 6) = popp str(8, 7) = p So, for the first query, and for the third query is the answer.
Input: 4 34 1 t3 2 p1 2 s3 21 32 1 | Output: 011
Master
5
1,467
415
44
7
723
D
723D
D. Lakes in Berland
1,600
dfs and similar; dsu; graphs; greedy; implementation
The map of Berland is a rectangle of the size n Γ— m, which consists of cells of size 1 Γ— 1. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell.You task is to fill up with the earth the minimum number of water cells so that there will be exactly k lakes in Berland. Note that the initial number of lakes on the map is not less than k.
The first line of the input contains three integers n, m and k (1 ≀ n, m ≀ 50, 0 ≀ k ≀ 50) β€” the sizes of the map and the number of lakes which should be left on the map.The next n lines contain m characters each β€” the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land).It is guaranteed that the map contain at least k lakes.
In the first line print the minimum number of cells which should be transformed from water to land. In the next n lines print m symbols β€” the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. It is guaranteed that the answer exists on the given data.
In the first example there are only two lakes β€” the first consists of the cells (2, 2) and (2, 3), the second consists of the cell (4, 3). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean.
Input: 5 4 1*****..*******.*..** | Output: 1*****..*********..**
Medium
5
804
437
393
7
1,380
C
1380C
C. Create The Teams
1,400
brute force; dp; greedy; implementation; sortings
There are \(n\) programmers that you want to split into several non-empty teams. The skill of the \(i\)-th programmer is \(a_i\). You want to assemble the maximum number of teams from them. There is a restriction for each team: the number of programmers in the team multiplied by the minimum skill among all programmers in the team must be at least \(x\).Each programmer should belong to at most one team. Some programmers may be left without a team.Calculate the maximum number of teams that you can assemble.
The first line contains the integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(x\) (\(1 \le n \le 10^5; 1 \le x \le 10^9\)) β€” the number of programmers and the restriction of team skill respectively.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots , a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the skill of the \(i\)-th programmer.The sum of \(n\) over all inputs does not exceed \(10^5\).
For each test case print one integer β€” the maximum number of teams that you can assemble.
Input: 3 5 10 7 11 2 9 5 4 8 2 4 2 3 4 11 1 3 3 7 | Output: 2 1 0
Easy
5
510
501
89
13
2,057
G
2057G
G. Secret Message
3,000
constructive algorithms; dfs and similar; math
Every Saturday, Alexander B., a teacher of parallel X, writes a secret message to Alexander G., a teacher of parallel B, in the evening. Since Alexander G. is giving a lecture at that time and the message is very important, Alexander B. has to write this message on an interactive online board.The interactive online board is a grid consisting of \(n\) rows and \(m\) columns, where each cell is \(1 \times 1\) in size. Some cells of this board are already filled in, and it is impossible to write a message in them; such cells are marked with the symbol ""."", while the remaining cells are called free and are marked with the symbol ""#"".Let us introduce two characteristics of the online board: \(s\) is the number of free cells. \(p\) is the perimeter of the grid figure formed by the union of free cells. Let \(A\) be the set of free cells. Your goal is to find a set of cells \(S \subseteq A\) that satisfies the following properties: \(|S| \le \frac{1}{5} \cdot (s+p)\). Any cell from \(A\) either lies in \(S\) or shares a side with some cell from \(S\). We can show that at least one set \(S\) satisfying these properties exists; you are required to find any suitable one.
The first line contains the number \(t\) (\(1 \le t \le 80\,000\)) β€” the number of test cases.In the first line of each test case, the numbers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^6\)) β€” the dimensions of the grid are given.The following \(n\) lines contain the description of the grid.It is guaranteed that the sum of \(n \cdot m\) across all test cases does not exceed \(2 \cdot 10^6\).
For each test case, output \(n\) lines consisting of \(m\) symbols, where each symbol encodes the state of the cell: ""#"" β€” the cell is in \(A\) but not in \(S\); ""S"" β€” the cell is in both \(A\) and \(S\); ""."" β€” the cell is neither in \(A\) nor in \(S\).
In the first example, \(s=5\) and \(p=12\), thus the number of cells in \(S\) must not exceed \(\frac{1}{5} \cdot (5+12) = 3.4\), meaning \(|S| \le 3\). Note that the presented set \(S\) consists of \(1\) cell and clearly satisfies all the constraints.In the second example, \(s=12\) and \(p=16\), thus the number of cells in \(S\) must not exceed \(\frac{1}{5} \cdot (12+16)= 5.6\), meaning \(|S| \le 5\). Note that the presented set \(S\) consists of \(4\) cells and clearly satisfies all the constraints.In the third example, we will explain what perimeter is, as it may not be obvious. Any grid figure has a boundary, which can be represented as a union of segments that do not intersect at interior points. Thus, in the picture below, the thick black line denotes the boundary of the figure formed by the union of free cells. The total length of these segments is \(p=24\).At the same time, the value \(s=11\) and the upper limit is \(|S| \le 7\), the presented set has a size of \(6\) and clearly satisfies all the constraints.
Input: 33 3.#.###.#.2 6############3 7###....#.#.######.... | Output: .#. #S# .#. #S##S# #S##S# S#S.... #.#.S#S S#S....
Master
3
1,182
396
259
20
1,941
E
1941E
E. Rudolf and k Bridges
1,600
binary search; data structures; dp; two pointers
Bernard loves visiting Rudolf, but he is always running late. The problem is that Bernard has to cross the river on a ferry. Rudolf decided to help his friend solve this problem.The river is a grid of \(n\) rows and \(m\) columns. The intersection of the \(i\)-th row and the \(j\)-th column contains the number \(a_{i,j}\) β€” the depth in the corresponding cell. All cells in the first and last columns correspond to the river banks, so the depth for them is \(0\). The river may look like this. Rudolf can choose the row \((i,1), (i,2), \ldots, (i,m)\) and build a bridge over it. In each cell of the row, he can install a support for the bridge. The cost of installing a support in the cell \((i,j)\) is \(a_{i,j}+1\). Supports must be installed so that the following conditions are met: A support must be installed in cell \((i,1)\); A support must be installed in cell \((i,m)\); The distance between any pair of adjacent supports must be no more than \(d\). The distance between supports \((i, j_1)\) and \((i, j_2)\) is \(|j_1 - j_2| - 1\). Building just one bridge is boring. Therefore, Rudolf decided to build \(k\) bridges on consecutive rows of the river, that is, to choose some \(i\) (\(1 \le i \le n - k + 1\)) and independently build a bridge on each of the rows \(i, i + 1, \ldots, i + k - 1\). Help Rudolf minimize the total cost of installing supports.
The first line contains a single integer \(t\) \((1 \le t \le 10^3)\) β€” the number of test cases. The descriptions of the test cases follow.The first line of each test case contains four integers \(n\), \(m\), \(k\), and \(d\) (\(1 \le k \le n \le 100\), \(3 \le m \le 2 \cdot 10^5\), \(1 \le d \le m\)) β€” the number of rows and columns of the field, the number of bridges, and the maximum distance between supports.Then follow \(n\) lines, \(i\)-th line contains \(m\) positive integers \(a_{i, j}\) (\(0 \le a_{i, j} \le 10^6\), \(a_{i, 1} = a_{i, m} = 0\)) β€” the depths of the river cells.It is guaranteed that the sum of \(n \cdot m\) for all sets of input data does not exceed \(2 \cdot 10^5\).
For each test case, output a single number β€” the minimum total cost of supports installation.
In the first test case, it is most profitable to build a bridge on the second row. It is not a top view, but side view: gray cells β€” bridge itself, white cells are empty, black cells β€” supports, blue cells β€” water, brown cells β€” river bottom. In the second test case, it is most profitable to build bridges on the second and third rows. The supports will be placed in cells \((2, 3)\), \((3, 2)\), and on the river banks.In the third test case the supports can be placed along the river banks.
Input: 53 11 1 40 1 2 3 4 5 4 3 2 1 00 1 2 3 2 1 2 3 3 2 00 1 2 3 5 5 5 5 5 2 04 4 2 10 3 3 00 2 1 00 1 2 00 3 3 04 5 2 50 1 1 1 00 2 2 2 00 2 1 1 00 3 2 1 01 8 1 10 10 4 8 4 4 2 04 5 3 20 8 4 4 00 3 4 8 00 8 1 10 00 10 1 5 0 | Output: 4 8 4 15 14
Medium
4
1,369
699
93
19
1,492
A
1492A
A. Three swimmers
800
math
Three swimmers decided to organize a party in the swimming pool! At noon, they started to swim from the left side of the pool.It takes the first swimmer exactly \(a\) minutes to swim across the entire pool and come back, exactly \(b\) minutes for the second swimmer and \(c\) minutes for the third. Hence, the first swimmer will be on the left side of the pool after \(0\), \(a\), \(2a\), \(3a\), ... minutes after the start time, the second one will be at \(0\), \(b\), \(2b\), \(3b\), ... minutes, and the third one will be on the left side of the pool after \(0\), \(c\), \(2c\), \(3c\), ... minutes.You came to the left side of the pool exactly \(p\) minutes after they started swimming. Determine how long you have to wait before one of the swimmers arrives at the left side of the pool.
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β€” the number of test cases. Next \(t\) lines contains test case descriptions, one per line.Each line contains four integers \(p\), \(a\), \(b\) and \(c\) (\(1 \leq p, a, b, c \leq 10^{18}\)), time in minutes after the start, when you came to the pool and times in minutes it take the swimmers to cross the entire pool and come back.
For each test case, output one integer β€” how long you have to wait (in minutes) before one of the swimmers arrives at the left side of the pool.
In the first test case, the first swimmer is on the left side in \(0, 5, 10, 15, \ldots\) minutes after the start time, the second swimmer is on the left side in \(0, 4, 8, 12, \ldots\) minutes after the start time, and the third swimmer is on the left side in \(0, 8, 16, 24, \ldots\) minutes after the start time. You arrived at the pool in \(9\) minutes after the start time and in a minute you will meet the first swimmer on the left side.In the second test case, the first swimmer is on the left side in \(0, 6, 12, 18, \ldots\) minutes after the start time, the second swimmer is on the left side in \(0, 10, 20, 30, \ldots\) minutes after the start time, and the third swimmer is on the left side in \(0, 9, 18, 27, \ldots\) minutes after the start time. You arrived at the pool \(2\) minutes after the start time and after \(4\) minutes meet the first swimmer on the left side.In the third test case, you came to the pool \(10\) minutes after the start time. At the same time, all three swimmers are on the left side. A rare stroke of luck!In the fourth test case, all swimmers are located on the left side in \(0, 9, 18, 27, \ldots\) minutes after the start time. You arrived at the pool \(10\) minutes after the start time and after \(8\) minutes meet all three swimmers on the left side.
Input: 4 9 5 4 8 2 6 10 9 10 2 5 10 10 9 9 9 | Output: 1 4 0 8
Beginner
1
792
417
144
14
1,491
A
1491A
A. K-th Largest Value
800
brute force; greedy; implementation
You are given an array \(a\) consisting of \(n\) integers. Initially all elements of \(a\) are either \(0\) or \(1\). You need to process \(q\) queries of two kinds: 1 x : Assign to \(a_x\) the value \(1 - a_x\). 2 k : Print the \(k\)-th largest value of the array. As a reminder, \(k\)-th largest value of the array \(b\) is defined as following: Sort the array in the non-increasing order, return \(k\)-th element from it. For example, the second largest element in array \([0, 1, 0, 1]\) is \(1\), as after sorting in non-increasing order it becomes \([1, 1, 0, 0]\), and the second element in this array is equal to \(1\).
The first line contains two integers \(n\) and \(q\) (\(1 \le n, q \le 10^5\)) β€” the length of the given array and the number of queries.The second line contains \(n\) integers \(a_1, a_2, a_3, \dots, a_n\) (\(0 \le a_i \le 1\)) β€” elements of the initial array.Each of the following \(q\) lines contains two integers. The first integer is \(t\) (\(1 \le t \le 2\)) β€” the type of query. If \(t = 1\) the second integer is \(x\) (\(1 \le x \le n\)) β€” the position of the modified number. You have to assign to \(a_x\) the value \(1 - a_x\). If \(t = 2\) the second integer is \(k\) (\(1 \le k \le n\)) β€” you need to print the \(k\)-th largest value of the array.It's guaranteed that there will be at least one query of the second type (satisfying \(t = 2\)).
For each query of the second type, print a single integer β€” the answer to the query.
Initially \(a = [1, 1, 0, 1, 0]\).The first operation is printing the third largest value, which is \(1\).The second operation is assigning \(a_2\) the value \(0\), \(a\) becomes \([1, 0, 0, 1, 0]\).The third operation is printing the third largest value, it is \(0\).The fourth operation is printing the first largest value, it is \(1\).The last operation is printing the fifth largest value, it is \(0\).
Input: 5 5 1 1 0 1 0 2 3 1 2 2 3 2 1 2 5 | Output: 1 0 1 0
Beginner
3
626
756
84
14
162
A
162A
A. Pentagonal numbers
1,100
*special; implementation
Pentagonal numbers are figurate numbers which can be calculated using the formula pn = (3n2 - n) / 2 (always integer). You are given n; calculate n-th pentagonal number.
The only line of input contains an integer n (1 ≀ n ≀ 100).
Output the n-th pentagonal number.
Input: 2 | Output: 5
Easy
2
169
59
34
1
1,793
D
1793D
D. Moscow Gorillas
1,800
binary search; dp; greedy; implementation; math; two pointers
In winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation \(p\) of length \(n\) to the zoo.A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in any order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) occurs twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\), but \(4\) is present in the array).The gorillas had their own permutation \(q\) of length \(n\). They suggested that you count the number of pairs of integers \(l, r\) (\(1 \le l \le r \le n\)) such that \(\operatorname{MEX}([p_l, p_{l+1}, \ldots, p_r])=\operatorname{MEX}([q_l, q_{l+1}, \ldots, q_r])\).The \(\operatorname{MEX}\) of the sequence is the minimum integer positive number missing from this sequence. For example, \(\operatorname{MEX}([1, 3]) = 2\), \(\operatorname{MEX}([5]) = 1\), \(\operatorname{MEX}([3, 1, 2, 6]) = 4\).You do not want to risk your health, so you will not dare to refuse the gorillas.
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the permutations length.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) β€” the elements of the permutation \(p\).The third line contains \(n\) integers \(q_1, q_2, \ldots, q_n\) (\(1 \le q_i \le n\)) β€” the elements of the permutation \(q\).
Print a single integer β€” the number of suitable pairs \(l\) and \(r\).
In the first example, two segments are correct – \([1, 3]\) with \(\operatorname{MEX}\) equal to \(4\) in both arrays and \([3, 3]\) with \(\operatorname{MEX}\) equal to \(1\) in both of arrays.In the second example, for example, the segment \([1, 4]\) is correct, and the segment \([6, 7]\) isn't correct, because \(\operatorname{MEX}(5, 4) \neq \operatorname{MEX}(1, 4)\).
Input: 31 3 22 1 3 | Output: 2
Medium
6
1,089
359
70
17
665
C
665C
C. Simple Strings
1,300
dp; greedy; strings
zscoder loves simple strings! A string t is called simple if every pair of adjacent characters are distinct. For example ab, aba, zscoder are simple whereas aa, add are not simple.zscoder is given a string s. He wants to change a minimum number of characters so that the string s becomes simple. Help him with this task!
The only line contains the string s (1 ≀ |s| ≀ 2Β·105) β€” the string given to zscoder. The string s consists of only lowercase English letters.
Print the simple string s' β€” the string s after the minimal number of changes. If there are multiple solutions, you may output any of them.Note that the string s' should also consist of only lowercase English letters.
Input: aab | Output: bab
Easy
3
320
141
217
6
55
C
55C
C. Pie or die
1,900
games
Volodya and Vlad play the following game. There are k pies at the cells of n Γ— m board. Each turn Volodya moves one pie to the neighbouring (by side) cell. If the pie lies at the border of the board then Volodya can move it outside the board, get the pie and win. After Volodya's move, Vlad bans some edge at the border of the board of length 1 (between two knots of the board) so that Volodya is not able to move the pie outside the board through this edge anymore. The question is: will Volodya win this game? We suppose both players follow the optimal strategy.
First line contains 3 integers, separated by space: 1 ≀ n, m ≀ 100 β€” dimensions of the board and 0 ≀ k ≀ 100 β€” the number of pies. Each of the next k lines contains 2 integers, separated by space: 1 ≀ x ≀ n, 1 ≀ y ≀ m β€” coordinates of the corresponding pie. There could be more than one pie at a cell.
Output only one word: ""YES"" β€” if Volodya wins, ""NO"" β€” otherwise.
Input: 2 2 11 2 | Output: YES
Hard
1
564
301
68
0
776
E
776E
E. The Holmes Children
2,100
math; number theory
The Holmes children are fighting over who amongst them is the cleverest.Mycroft asked Sherlock and Eurus to find value of f(n), where f(1) = 1 and for n β‰₯ 2, f(n) is the number of distinct ordered positive integer pairs (x, y) that satisfy x + y = n and gcd(x, y) = 1. The integer gcd(a, b) is the greatest common divisor of a and b.Sherlock said that solving this was child's play and asked Mycroft to instead get the value of . Summation is done over all positive integers d that divide n.Eurus was quietly observing all this and finally came up with her problem to astonish both Sherlock and Mycroft.She defined a k-composite function Fk(n) recursively as follows:She wants them to tell the value of Fk(n) modulo 1000000007.
A single line of input contains two space separated integers n (1 ≀ n ≀ 1012) and k (1 ≀ k ≀ 1012) indicating that Eurus asks Sherlock and Mycroft to find the value of Fk(n) modulo 1000000007.
Output a single integer β€” the value of Fk(n) modulo 1000000007.
In the first case, there are 6 distinct ordered pairs (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1) satisfying x + y = 7 and gcd(x, y) = 1. Hence, f(7) = 6. So, F1(7) = f(g(7)) = f(f(7) + f(1)) = f(6 + 1) = f(7) = 6.
Input: 7 1 | Output: 6
Hard
2
727
192
63
7
1,826
B
1826B
B. Lunatic Never Content
1,100
math; number theory
You have an array \(a\) of \(n\) non-negative integers. Let's define \(f(a, x) = [a_1 \bmod x, a_2 \bmod x, \dots, a_n \bmod x]\) for some positive integer \(x\). Find the biggest \(x\), such that \(f(a, x)\) is a palindrome.Here, \(a \bmod x\) is the remainder of the integer division of \(a\) by \(x\).An array is a palindrome if it reads the same backward as forward. More formally, an array \(a\) of length \(n\) is a palindrome if for every \(i\) (\(1 \leq i \leq n\)) \(a_i = a_{n - i + 1}\).
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^5\)).The second line of each test case contains \(n\) integers \(a_i\) (\(0 \leq a_i \leq 10^9\)).It's guaranteed that the sum of all \(n\) does not exceed \(10^5\).
For each test case output the biggest \(x\), such that \(f(a, x)\) is a palindrome. If \(x\) can be infinitely large, output \(0\) instead.
In the first example, \(f(a, x = 1) = [0, 0]\) which is a palindrome.In the second example, \(f(a, x = 2) = [1, 0, 1, 0, 0, 1, 0, 1]\) which is a palindrome.It can be proven that in the first two examples, no larger \(x\) satisfies the condition.In the third example, \(f(a, x) = [0]\) for any \(x\), so we can choose it infinitely large, so the answer is \(0\).
Input: 421 283 0 1 2 0 3 2 1103100 1 1000000000 | Output: 1 2 0 999999900
Easy
2
498
349
139
18
260
A
260A
A. Adding Digits
1,400
implementation; math
Vasya has got two number: a and b. However, Vasya finds number a too short. So he decided to repeat the operation of lengthening number a n times.One operation of lengthening a number means adding exactly one digit to the number (in the decimal notation) to the right provided that the resulting number is divisible by Vasya's number b. If it is impossible to obtain the number which is divisible by b, then the lengthening operation cannot be performed.Your task is to help Vasya and print the number he can get after applying the lengthening operation to number a n times.
The first line contains three integers: a, b, n (1 ≀ a, b, n ≀ 105).
In a single line print the integer without leading zeros, which Vasya can get when he applies the lengthening operations to number a n times. If no such number exists, then print number -1. If there are multiple possible answers, print any of them.
Input: 5 4 5 | Output: 524848
Easy
2
574
68
248
2
212
B
212B
B. Polycarpus is Looking for Good Substrings
2,300
bitmasks; hashing; implementation
We'll call string s[a, b] = sasa + 1... sb (1 ≀ a ≀ b ≀ |s|) a substring of string s = s1s2... s|s|, where |s| is the length of string s.The trace of a non-empty string t is a set of characters that the string consists of. For example, the trace of string ""aab"" equals {'a', 'b'}.Let's consider an arbitrary string s and the set of its substrings with trace equal to C. We will denote the number of substrings from this set that are maximal by inclusion by r(C, s). Substring s[a, b] of length n = b - a + 1 belonging to some set is called maximal by inclusion, if there is no substring s[x, y] in this set with length greater than n, such that 1 ≀ x ≀ a ≀ b ≀ y ≀ |s|. Two substrings of string s are considered different even if they are equal but they are located at different positions of s.Polycarpus got a challenging practical task on a stringology exam. He must do the following: given string s and non-empty sets of characters C1, C2, ..., Cm, find r(Ci, s) for each set Ci. Help Polycarpus to solve the problem as he really doesn't want to be expelled from the university and go to the army!
The first line contains a non-empty string s (1 ≀ |s| ≀ 106).The second line contains a single integer m (1 ≀ m ≀ 104). Next m lines contain descriptions of sets Ci. The i-th line contains string ci such that its trace equals Ci. It is guaranteed that all characters of each string ci are different.Note that Ci are not necessarily different. All given strings consist of lowercase English letters.
Print m integers β€” the i-th integer must equal r(Ci, s).
Input: aaaaa2aa | Output: 11
Expert
3
1,102
398
56
2
1,321
C
1321C
C. Remove Adjacent
1,600
brute force; constructive algorithms; greedy; strings
You are given a string \(s\) consisting of lowercase Latin letters. Let the length of \(s\) be \(|s|\). You may perform several operations on this string.In one operation, you can choose some index \(i\) and remove the \(i\)-th character of \(s\) (\(s_i\)) if at least one of its adjacent characters is the previous letter in the Latin alphabet for \(s_i\). For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index \(i\) should satisfy the condition \(1 \le i \le |s|\) during each operation.For the character \(s_i\) adjacent characters are \(s_{i-1}\) and \(s_{i+1}\). The first and the last characters of \(s\) both have only one adjacent character (unless \(|s| = 1\)).Consider the following example. Let \(s=\) bacabcab. During the first move, you can remove the first character \(s_1=\) b because \(s_2=\) a. Then the string becomes \(s=\) acabcab. During the second move, you can remove the fifth character \(s_5=\) c because \(s_4=\) b. Then the string becomes \(s=\) acabab. During the third move, you can remove the sixth character \(s_6=\)'b' because \(s_5=\) a. Then the string becomes \(s=\) acaba. During the fourth move, the only character you can remove is \(s_4=\) b, because \(s_3=\) a (or \(s_5=\) a). The string becomes \(s=\) acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally.
The first line of the input contains one integer \(|s|\) (\(1 \le |s| \le 100\)) β€” the length of \(s\).The second line of the input contains one string \(s\) consisting of \(|s|\) lowercase Latin letters.
Print one integer β€” the maximum possible number of characters you can remove if you choose the sequence of moves optimally.
The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is \(4\).In the second example, you can remove all but one character of \(s\). The only possible answer follows. During the first move, remove the third character \(s_3=\) d, \(s\) becomes bca. During the second move, remove the second character \(s_2=\) c, \(s\) becomes ba. And during the third move, remove the first character \(s_1=\) b, \(s\) becomes a.
Input: 8 bacabcab | Output: 4
Medium
4
1,561
204
123
13
2,059
C
2059C
C. Customer Service
1,600
brute force; constructive algorithms; graph matchings; greedy; math; sortings
Nikyr has started working as a queue manager at the company ""Black Contour."" He needs to choose the order of servicing customers. There are a total of \(n\) queues, each initially containing \(0\) people. In each of the next \(n\) moments of time, there are two sequential events: New customers arrive in all queues. More formally, at the \(j\)-th moment of time, the number of people in the \(i\)-th queue increases by a positive integer \(a_{i,j}\). Nikyr chooses exactly one of the \(n\) queues to be served at that moment in time. The number of customers in this queue becomes \(0\).Let the number of people in the \(i\)-th queue after all events be \(x_i\). Nikyr wants MEX\(^{\dagger}\) of the collection \(x_1, x_2, \ldots, x_n\) to be as large as possible. Help him determine the maximum value he can achieve with an optimal order of servicing the queues.\(^{\dagger}\)The minimum excluded (MEX) of a collection of integers \(c_1, c_2, \ldots, c_k\) is defined as the smallest non-negative integer \(y\) which does not occur in the collection \(c\). For example: \(\operatorname{MEX}([2,2,1])= 0\), since \(0\) does not belong to the array. \(\operatorname{MEX}([3,1,0,1]) = 2\), since \(0\) and \(1\) belong to the array, but \(2\) does not. \(\operatorname{MEX}([0,3,1,2]) = 4\), since \(0\), \(1\), \(2\), and \(3\) belong to the array, but \(4\) does not.
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 300\)) β€” the number of queues and moments of time.The \(i\)-th of the next \(n\) lines contains \(n\) integers \(a_{i,1}, a_{i,2}, \ldots, a_{i,n}\) (\(1 \le a_{i,j} \le 10^9\)) β€” the number of new customers in the \(i\)-th queue at each moment of time.It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output a single integer β€” the maximum value of \(\operatorname{MEX}([x_1, x_2, \ldots, x_n])\) that can be achieved.
In the first test case, the second queue can be served at time \(1\), and the first queue at time \(2\). There will be \(x_1 = 0\) people left in the first queue and \(x_2 = 1\) person left in the second queue. Therefore, the answer is \(\operatorname{MEX}([0, 1]) = 2\).In the second test case, the first queue can be served both times. There will be \(x_1 = 0\) people left in the first queue and \(x_2 = 20\) people left in the second queue. Therefore, the answer is \(\operatorname{MEX}([0, 20]) = 1\).In the third test case, the third queue can be served at time \(1\), the second queue at time \(2\), and the first queue at time \(3\). There will be \(x_1 = 0\) people left in the first queue, \(x_2 = 1\) person left in the second queue, and \(x_3 = 2\) people left in the third queue. Therefore, the answer is \(\operatorname{MEX}([0, 1, 2]) = 3\).
Input: 421 22 1210 1010 1032 3 34 4 12 1 144 2 2 171 9 3 15 5 5 111 2 1 1 | Output: 2 1 3 3
Medium
6
1,369
618
136
20
1,368
C
1368C
C. Even Picture
1,500
constructive algorithms
Leo Jr. draws pictures in his notebook with checkered sheets (that is, each sheet has a regular square grid printed on it). We can assume that the sheets are infinitely large in any direction.To draw a picture, Leo Jr. colors some of the cells on a sheet gray. He considers the resulting picture beautiful if the following conditions are satisfied: The picture is connected, that is, it is possible to get from any gray cell to any other by following a chain of gray cells, with each pair of adjacent cells in the path being neighbours (that is, sharing a side). Each gray cell has an even number of gray neighbours. There are exactly \(n\) gray cells with all gray neighbours. The number of other gray cells can be arbitrary (but reasonable, so that they can all be listed).Leo Jr. is now struggling to draw a beautiful picture with a particular choice of \(n\). Help him, and provide any example of a beautiful picture.To output cell coordinates in your answer, assume that the sheet is provided with a Cartesian coordinate system such that one of the cells is chosen to be the origin \((0, 0)\), axes \(0x\) and \(0y\) are orthogonal and parallel to grid lines, and a unit step along any axis in any direction takes you to a neighbouring cell.
The only line contains a single integer \(n\) (\(1 \leq n \leq 500\)) β€” the number of gray cells with all gray neighbours in a beautiful picture.
In the first line, print a single integer \(k\) β€” the number of gray cells in your picture. For technical reasons, \(k\) should not exceed \(5 \cdot 10^5\).Each of the following \(k\) lines should contain two integers β€” coordinates of a gray cell in your picture. All listed cells should be distinct, and the picture should satisdfy all the properties listed above. All coordinates should not exceed \(10^9\) by absolute value.One can show that there exists an answer satisfying all requirements with a small enough \(k\).
The answer for the sample is pictured below:
Input: 4 | Output: 12 1 0 2 0 0 1 1 1 2 1 3 1 0 2 1 2 2 2 3 2 1 3 2 3
Medium
1
1,246
145
522
13
358
D
358D
D. Dima and Hares
1,800
dp; greedy
Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima felt so grateful to Inna about the present that he decided to buy her n hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to n from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them?Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and n don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares.Help Inna maximize the total joy the hares radiate. :)
The first line of the input contains integer n (1 ≀ n ≀ 3000) β€” the number of hares. Then three lines follow, each line has n integers. The first line contains integers a1 a2 ... an. The second line contains b1, b2, ..., bn. The third line contains c1, c2, ..., cn. The following limits are fulfilled: 0 ≀ ai, bi, ci ≀ 105.Number ai in the first line shows the joy that hare number i gets if his adjacent hares are both hungry. Number bi in the second line shows the joy that hare number i radiates if he has exactly one full adjacent hare. Number сi in the third line shows the joy that hare number i radiates if both his adjacent hares are full.
In a single line, print the maximum possible total joy of the hares Inna can get by feeding them.
Input: 41 2 3 44 3 2 10 1 1 0 | Output: 13
Medium
2
980
647
97
3
30
C
30C
C. Shooting Gallery
1,800
dp; probabilities
One warm and sunny day king Copa decided to visit the shooting gallery, located at the Central Park, and try to win the main prize β€” big pink plush panda. The king is not good at shooting, so he invited you to help him.The shooting gallery is an infinite vertical plane with Cartesian coordinate system on it. The targets are points on this plane. Each target is described by it's coordinates xi, and yi, by the time of it's appearance ti and by the number pi, which gives the probability that Copa hits this target if he aims at it.A target appears and disappears instantly, so Copa can hit the target only if at the moment ti his gun sight aimed at (xi, yi). Speed of movement of the gun sight on the plane is equal to 1. Copa knows all the information about the targets beforehand (remember, he is a king!). He wants to play in the optimal way, which maximizes the expected value of the amount of hit targets. He can aim at any target at the moment 0.
The first line contains integer n (1 ≀ n ≀ 1000) β€” amount of targets in the shooting gallery. Then n lines follow, each describing one target. Each description consists of four numbers xi, yi, ti, pi (where xi, yi, ti β€” integers, - 1000 ≀ xi, yi ≀ 1000, 0 ≀ ti ≀ 109, real number pi is given with no more than 6 digits after the decimal point, 0 ≀ pi ≀ 1). No two targets may be at the same point.
Output the maximum expected value of the amount of targets that was shot by the king. Your answer will be accepted if it differs from the correct answer by not more than 10 - 6.
Input: 10 0 0 0.5 | Output: 0.5000000000
Medium
2
954
397
177
0
1,373
B
1373B
B. 01 Game
900
games
Alica and Bob are playing a game.Initially they have a binary string \(s\) consisting of only characters 0 and 1.Alice and Bob make alternating moves: Alice makes the first move, Bob makes the second move, Alice makes the third one, and so on. During each move, the current player must choose two different adjacent characters of string \(s\) and delete them. For example, if \(s = 1011001\) then the following moves are possible: delete \(s_1\) and \(s_2\): \(\textbf{10}11001 \rightarrow 11001\); delete \(s_2\) and \(s_3\): \(1\textbf{01}1001 \rightarrow 11001\); delete \(s_4\) and \(s_5\): \(101\textbf{10}01 \rightarrow 10101\); delete \(s_6\) and \(s_7\): \(10110\textbf{01} \rightarrow 10110\). If a player can't make any move, they lose. Both players play optimally. You have to determine if Alice can win.
First line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Only line of each test case contains one string \(s\) (\(1 \le |s| \le 100\)), consisting of only characters 0 and 1.
For each test case print answer in the single line.If Alice can win print DA (YES in Russian) in any register. Otherwise print NET (NO in Russian) in any register.
In the first test case after Alice's move string \(s\) become empty and Bob can not make any move.In the second test case Alice can not make any move initially.In the third test case after Alice's move string \(s\) turn into \(01\). Then, after Bob's move string \(s\) become empty and Alice can not make any move.
Input: 3 01 1111 0011 | Output: DA NET NET
Beginner
1
815
205
163
13
208
D
208D
D. Prizes, Prizes, more Prizes
1,200
implementation
Vasya, like many others, likes to participate in a variety of sweepstakes and lotteries. Now he collects wrappings from a famous chocolate bar ""Jupiter"". According to the sweepstake rules, each wrapping has an integer written on it β€” the number of points that the participant adds to his score as he buys the bar. After a participant earns a certain number of points, he can come to the prize distribution center and exchange the points for prizes. When somebody takes a prize, the prize's cost is simply subtracted from the number of his points.Vasya didn't only bought the bars, he also kept a record of how many points each wrapping cost. Also, he remembers that he always stucks to the greedy strategy β€” as soon as he could take at least one prize, he went to the prize distribution centre and exchanged the points for prizes. Moreover, if he could choose between multiple prizes, he chose the most expensive one. If after an exchange Vasya had enough points left to get at least one more prize, then he continued to exchange points.The sweepstake has the following prizes (the prizes are sorted by increasing of their cost): a mug (costs a points), a towel (costs b points), a bag (costs c points), a bicycle (costs d points), a car (costs e points). Now Vasya wants to recollect what prizes he has received. You know sequence p1, p2, ..., pn, where pi is the number of points Vasya got for the i-th bar. The sequence of points is given in the chronological order. You also know numbers a, b, c, d, e. Your task is to find, how many prizes Vasya received, what prizes they are and how many points he's got left after all operations are completed.
The first line contains a single integer n (1 ≀ n ≀ 50) β€” the number of chocolate bar wrappings that brought points to Vasya. The second line contains space-separated integers p1, p2, ..., pn (1 ≀ pi ≀ 109). The third line contains 5 integers a, b, c, d, e (1 ≀ a < b < c < d < e ≀ 109) β€” the prizes' costs.
Print on the first line 5 integers, separated by a space β€” the number of mugs, towels, bags, bicycles and cars that Vasya has got, respectively. On the second line print a single integer β€” the number of points Vasya will have left after all operations of exchange are completed.Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
In the first sample Vasya gets 3 points after eating the first chocolate bar. Then he exchanges 2 points and gets a mug. Vasya wins a bag after eating the second chocolate bar. Then he wins a towel after eating the third chocolate bar. After all chocolate bars 3 - 2 + 10 - 10 + 4 - 4 = 1 points remains.
Input: 33 10 42 4 10 15 20 | Output: 1 1 1 0 0 1
Easy
1
1,653
307
425
2
1,895
D
1895D
D. XOR Construction
1,900
bitmasks; constructive algorithms; data structures; math; string suffix structures; trees
You are given \(n-1\) integers \(a_1, a_2, \dots, a_{n-1}\).Your task is to construct an array \(b_1, b_2, \dots, b_n\) such that: every integer from \(0\) to \(n-1\) appears in \(b\) exactly once; for every \(i\) from \(1\) to \(n-1\), \(b_i \oplus b_{i+1} = a_i\) (where \(\oplus\) denotes the bitwise XOR operator).
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)).The second line contains \(n-1\) integers \(a_1, a_2, \dots, a_{n-1}\) (\(0 \le a_i \le 2n\)).Additional constraint on the input: it's always possible to construct at least one valid array \(b\) from the given sequence \(a\).
Print \(n\) integers \(b_1, b_2, \dots, b_n\). If there are multiple such arrays, you may print any of them.
Input: 42 1 2 | Output: 0 2 3 1
Hard
6
318
298
108
18
1,357
A1
1357A1
A1. Figure out direction of CNOT
0
*special
You are given an operation that implements a two-qubit unitary transformation: either the CNOT gate with the first qubit as control and the second qubit as target (CNOT\(_{12}\)), or the CNOT gate with the second qubit as control and the first qubit as target (CNOT\(_{21}\)). The operation will have Adjoint and Controlled variants defined.Your task is to perform necessary operations and measurements to figure out which unitary it was and to return 0 if it was the CNOT\(_{12}\) gate or 1 if it was the CNOT\(_{21}\) gate. You are allowed to apply the given operation and its adjoint/controlled variants exactly once.You have to implement an operation which takes a two-qubit operation unitary as an input and returns an integer. The operation unitary will accept an array of qubits as input, but it will fail if the array is empty or has one or more than two qubits. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (unitary : (Qubit[] => Unit is Adj+Ctl)) : Int { // your code here }}
Beginner
1
1,057
0
0
13
998
A
998A
A. Balloons
1,000
constructive algorithms; implementation
There are quite a lot of ways to have fun with inflatable balloons. For example, you can fill them with water and see what happens.Grigory and Andrew have the same opinion. So, once upon a time, they went to the shop and bought \(n\) packets with inflatable balloons, where \(i\)-th of them has exactly \(a_i\) balloons inside.They want to divide the balloons among themselves. In addition, there are several conditions to hold: Do not rip the packets (both Grigory and Andrew should get unbroken packets); Distribute all packets (every packet should be given to someone); Give both Grigory and Andrew at least one packet; To provide more fun, the total number of balloons in Grigory's packets should not be equal to the total number of balloons in Andrew's packets. Help them to divide the balloons or determine that it's impossible under these conditions.
The first line of input contains a single integer \(n\) (\(1 \le n \le 10\)) β€” the number of packets with balloons.The second line contains \(n\) integers: \(a_1\), \(a_2\), \(\ldots\), \(a_n\) (\(1 \le a_i \le 1000\)) β€” the number of balloons inside the corresponding packet.
If it's impossible to divide the balloons satisfying the conditions above, print \(-1\).Otherwise, print an integer \(k\) β€” the number of packets to give to Grigory followed by \(k\) distinct integers from \(1\) to \(n\) β€” the indices of those. The order of packets doesn't matter.If there are multiple ways to divide balloons, output any of them.
In the first test Grigory gets \(3\) balloons in total while Andrey gets \(1\).In the second test there's only one way to divide the packets which leads to equal numbers of balloons.In the third test one of the boys won't get a packet at all.
Input: 31 2 1 | Output: 21 2
Beginner
2
857
276
347
9
592
E
592E
E. BCPC
2,700
binary search; geometry; two pointers
BCPC stands for Byteforces Collegiate Programming Contest, and is the most famous competition in Byteforces.BCPC is a team competition. Each team is composed by a coach and three contestants. Blenda is the coach of the Bit State University(BSU), and she is very strict selecting the members of her team. In BSU there are n students numbered from 1 to n. Since all BSU students are infinitely smart, the only important parameters for Blenda are their reading and writing speed. After a careful measuring, Blenda have found that the i-th student have a reading speed equal to ri (words per minute), and a writing speed of wi (symbols per minute). Since BSU students are very smart, the measured speeds are sometimes very big and Blenda have decided to subtract some constant value c from all the values of reading speed and some value d from all the values of writing speed. Therefore she considers ri' = ri - c and wi' = wi - d. The student i is said to overwhelm the student j if and only if ri'Β·wj' > rj'Β·wi'. Blenda doesn’t like fights in teams, so she thinks that a team consisting of three distinct students i, j and k is good if i overwhelms j, j overwhelms k, and k overwhelms i. Yes, the relation of overwhelming is not transitive as it often happens in real life.Since Blenda is busy preparing a training camp in Codeforces, you are given a task to calculate the number of different good teams in BSU. Two teams are considered to be different if there is at least one student that is present in one team but is not present in the other. In other words, two teams are different if the sets of students that form these teams are different.
In the first line of the input three integers n, c and d (3 ≀ n ≀ 345678, 1 ≀ c, d ≀ 109) are written. They denote the number of students Blenda can use to form teams, the value subtracted from all reading speeds and the value subtracted from all writing speeds respectively.Each of the next n lines contains two integers ri and wi (0 < ri, wi ≀ 109, |ri - c| + |wi - d| > 0). There are no two students, such that both their reading and writing speeds coincide, i.e. for every i β‰  j condition |ri - rj| + |wi - wj| > 0 holds.
Print the number of different teams in BSU, that are good according to Blenda's definition.
In the first sample the following teams are good: (i = 1, j = 2, k = 3), (i = 2, j = 5, k = 1), (i = 1, j = 4, k = 3), (i = 5, j = 1, k = 4).Note, that for example the team (i = 3, j = 1, k = 2) is also good, but is considered to be the same as the team (i = 1, j = 2, k = 3).
Input: 5 2 21 14 12 33 23 4 | Output: 4
Master
3
1,645
525
91
5
1,654
B
1654B
B. Prefix Removals
800
strings
You are given a string \(s\) consisting of lowercase letters of the English alphabet. You must perform the following algorithm on \(s\): Let \(x\) be the length of the longest prefix of \(s\) which occurs somewhere else in \(s\) as a contiguous substring (the other occurrence may also intersect the prefix). If \(x = 0\), break. Otherwise, remove the first \(x\) characters of \(s\), and repeat. A prefix is a string consisting of several first letters of a given string, without any reorders. An empty prefix is also a valid prefix. For example, the string ""abcd"" has 5 prefixes: empty string, ""a"", ""ab"", ""abc"" and ""abcd"".For instance, if we perform the algorithm on \(s =\) ""abcabdc"", Initially, ""ab"" is the longest prefix that also appears somewhere else as a substring in \(s\), so \(s =\) ""cabdc"" after \(1\) operation. Then, ""c"" is the longest prefix that also appears somewhere else as a substring in \(s\), so \(s =\) ""abdc"" after \(2\) operations. Now \(x=0\) (because there are no non-empty prefixes of ""abdc"" that also appear somewhere else as a substring in \(s\)), so the algorithm terminates. Find the final state of the string after performing the algorithm.
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.This is followed by \(t\) lines, each containing a description of one test case. Each line contains a string \(s\). The given strings consist only of lowercase letters of the English alphabet and have lengths between \(1\) and \(2 \cdot 10^5\) inclusive.It is guaranteed that the sum of the lengths of \(s\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, print a single line containing the string \(s\) after executing the algorithm. It can be shown that such string is non-empty.
The first test case is explained in the statement.In the second test case, no operations can be performed on \(s\).In the third test case, Initially, \(s =\) ""bbbbbbbbbb"". After \(1\) operation, \(s =\) ""b"". In the fourth test case, Initially, \(s =\) ""codeforces"". After \(1\) operation, \(s =\) ""odeforces"". After \(2\) operations, \(s =\) ""deforces"".
Input: 6abcabdcabbbbbbbbbbcodeforcescffcfccffccfcffcfccfcffccffcfccfzyzyzwxxyyxxyyzzyzzxxwzxwywxwzxxyzzw | Output: abdc a b deforces cf xyzzw
Beginner
1
1,196
458
145
16
1,725
F
1725F
F. Field Photography
2,100
bitmasks; data structures; sortings
Pak Chanek is traveling to Manado. It turns out that OSN (Indonesian National Scientific Olympiad) 2019 is being held. The contestants of OSN 2019 are currently lining up in a field to be photographed. The field is shaped like a grid of size \(N \times 10^{100}\) with \(N\) rows and \(10^{100}\) columns. The rows are numbered from \(1\) to \(N\) from north to south, the columns are numbered from \(1\) to \(10^{100}\) from west to east. The tile in row \(r\) and column \(c\) is denoted as \((r,c)\).There are \(N\) provinces that participate in OSN 2019. Initially, each contestant who represents province \(i\) stands in tile \((i, p)\) for each \(p\) satisfying \(L_i \leq p \leq R_i\). So, we can see that there are \(R_i-L_i+1\) contestants who represent province \(i\).Pak Chanek has a variable \(Z\) that is initially equal to \(0\). In one operation, Pak Chanek can choose a row \(i\) and a positive integer \(k\). Then, Pak Chanek will do one of the two following possibilities: Move all contestants in row \(i\) exactly \(k\) tiles to the west. In other words, a contestant who is in \((i, p)\) is moved to \((i, p-k)\). Move all contestants in row \(i\) exactly \(k\) tiles to the east. In other words, a contestant who is in \((i, p)\) is moved to \((i, p+k)\). After each operation, the value of \(Z\) will change into \(Z \text{ OR } k\), with \(\text{OR}\) being the bitwise OR operation. Note that Pak Chanek can do operations to the same row more than once. Also note that Pak Chanek is not allowed to move contestants out of the grid.There are \(Q\) questions. For the \(j\)-th question, you are given a positive integer \(W_j\), Pak Chanek must do zero or more operations so that the final value of \(Z\) is exactly \(W_j\). Define \(M\) as the biggest number such that after all operations, there is at least one column that contains exactly \(M\) contestants. For each question, you must find the biggest possible \(M\) for all sequences of operations that can be done by Pak Chanek. Note that the operations done by Pak Chanek for one question do not carry over to other questions.
The first line contains a single integer \(N\) (\(1 \leq N \leq 10^5\)) β€” the number of rows in the grid and also the number of provinces that participate in OSN 2019.The \(i\)-th of the next \(N\) lines contains two integers \(L_i\) and \(R_i\) (\(1 \leq L_i \leq R_i \leq 10^9\)) describing the positions of the contestants who represent province \(i\).The next line contains a single integer \(Q\) (\(1 \leq Q \leq 10^5\)) β€” the number of questions.The \(j\)-th of the next \(Q\) lines contains a single integer \(W_j\) (\(1 \leq W_j \leq 10^9\)) β€” the required final value of \(Z\) for the \(j\)-th question.
Output \(Q\) lines, with the \(j\)-th line containing an integer that is the answer to the \(j\)-th question.
For the \(1\)-st question, Pak Chanek can do the following operations to get \(M=2\): Move all contestants in row \(2\) to the east by \(4\) tiles. \(Z\) changes into \(0 \text{ OR } 4 = 4\). Move all contestants in row \(1\) to the east by \(12\) tiles. \(Z\) changes into \(4 \text{ OR } 12 = 12\). Now, columns \(14\) and \(15\) each contains exactly \(2\) contestants.For the \(2\)-nd question, Pak Chanek can do the following operations to get \(M=3\): Move all contestants in row \(3\) to the east by \(4\) tiles. \(Z\) changes into \(0 \text{ OR } 4 = 4\). Move all contestants in row \(3\) to the west by \(1\) tiles. \(Z\) changes into \(4 \text{ OR } 1 = 5\). Move all contestants in row \(1\) to the east by \(5\) tiles. \(Z\) changes into \(5 \text{ OR } 5 = 5\). Move all contestants in row \(1\) to the east by \(5\) tiles. \(Z\) changes into \(5 \text{ OR } 5 = 5\). Now, column \(11\) contains exactly \(3\) contestants.The following is an illustration of the example operations for the \(2\)-nd question.
Input: 3 1 5 10 11 8 8 2 12 5 | Output: 2 3
Hard
3
2,106
612
109
17
1,967
C
1967C
C. Fenwick Tree
2,300
bitmasks; brute force; combinatorics; data structures; dp; math; trees
Let \(\operatorname{lowbit}(x)\) denote the value of the lowest binary bit of \(x\), e.g. \(\operatorname{lowbit}(12)=4\), \(\operatorname{lowbit}(8)=8\).For an array \(a\) of length \(n\), if an array \(s\) of length \(n\) satisfies \(s_k=\left(\sum\limits_{i=k-\operatorname{lowbit}(k)+1}^{k}a_i\right)\bmod 998\,244\,353\) for all \(k\), then \(s\) is called the Fenwick Tree of \(a\). Let's denote it as \(s=f(a)\).For a positive integer \(k\) and an array \(a\), \(f^k(a)\) is defined as follows:$$$\( f^k(a)= \begin{cases} f(a)&\textrm{if }k=1\\ f(f^{k-1}(a))&\textrm{otherwise.}\\ \end{cases} \)\(You are given an array \)b\( of length \)n\( and a positive integer \)k\(. Find an array \)a\( that satisfies \)0\le a_i < 998\,244\,353\( and \)f^k(a)=b$$$. It can be proved that an answer always exists. If there are multiple possible answers, you may print any of them.
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le 10^4\)). The description of the test cases follows.The first line of each test case contains two positive integers \(n\) (\(1 \leq n \leq 2\cdot 10^5\)) and \(k\) (\(1\le k\le 10^9\)), representing the length of the array and the number of times the function \(f\) is performed.The second line of each test case contains an array \(b_1, b_2, \ldots, b_n\) (\(0\le b_i < 998\,244\,353\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
For each test case, print a single line, containing a valid array \(a\) of length \(n\).
In the first test case, it can be seen that \(f^1([1,1,1,1,1,1,1,1])=[1,2,1,4,1,2,1,8]\).In the second test case, it can be seen that \(f^2([1,2,3,4,5,6])=f^1([1,3,3,10,5,11])=[1,4,3,17,5,16]\).
Input: 28 11 2 1 4 1 2 1 86 21 4 3 17 5 16 | Output: 1 1 1 1 1 1 1 1 1 2 3 4 5 6
Expert
7
875
586
88
19
2,020
B
2020B
B. Brightness Begins
1,200
binary search; math
Imagine you have \(n\) light bulbs numbered \(1, 2, \ldots, n\). Initially, all bulbs are on. To flip the state of a bulb means to turn it off if it used to be on, and to turn it on otherwise.Next, you do the following: for each \(i = 1, 2, \ldots, n\), flip the state of all bulbs \(j\) such that \(j\) is divisible by \(i^\dagger\). After performing all operations, there will be several bulbs that are still on. Your goal is to make this number exactly \(k\).Find the smallest suitable \(n\) such that after performing the operations there will be exactly \(k\) bulbs on. We can show that an answer always exists.\(^\dagger\) An integer \(x\) is divisible by \(y\) if there exists an integer \(z\) such that \(x = y\cdot z\).
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The only line of each test case contains a single integer \(k\) (\(1 \le k \le 10^{18}\)).
For each test case, output \(n\) β€” the minimum number of bulbs.
In the first test case, the minimum number of bulbs is \(2\). Let's denote the state of all bulbs with an array, where \(1\) corresponds to a turned on bulb, and \(0\) corresponds to a turned off bulb. Initially, the array is \([1, 1]\). After performing the operation with \(i = 1\), the array becomes \([\underline{0}, \underline{0}]\). After performing the operation with \(i = 2\), the array becomes \([0, \underline{1}]\). In the end, there are \(k = 1\) bulbs on. We can also show that the answer cannot be less than \(2\).In the second test case, the minimum number of bulbs is \(5\). Initially, the array is \([1, 1, 1, 1, 1]\). After performing the operation with \(i = 1\), the array becomes \([\underline{0}, \underline{0}, \underline{0}, \underline{0}, \underline{0}]\). After performing the operation with \(i = 2\), the array becomes \([0, \underline{1}, 0, \underline{1}, 0]\). After performing the operation with \(i = 3\), the array becomes \([0, 1, \underline{1}, 1, 0]\). After performing the operation with \(i = 4\), the array becomes \([0, 1, 1, \underline{0}, 0]\). After performing the operation with \(i = 5\), the array becomes \([0, 1, 1, 0, \underline{1}]\). In the end, there are \(k = 3\) bulbs on. We can also show that the answer cannot be smaller than \(5\).
Input: 3138 | Output: 2 5 11
Easy
2
728
251
63
20
2,033
A
2033A
A. Sakurako and Kosuke
800
constructive algorithms; implementation; math
Sakurako and Kosuke decided to play some games with a dot on a coordinate line. The dot is currently located in position \(x=0\). They will be taking turns, and Sakurako will be the one to start. On the \(i\)-th move, the current player will move the dot in some direction by \(2\cdot i-1\) units. Sakurako will always be moving the dot in the negative direction, whereas Kosuke will always move it in the positive direction. In other words, the following will happen: Sakurako will change the position of the dot by \(-1\), \(x = -1\) now Kosuke will change the position of the dot by \(3\), \(x = 2\) now Sakurako will change the position of the dot by \(-5\), \(x = -3\) now \(\cdots\) They will keep on playing while the absolute value of the coordinate of the dot does not exceed \(n\). More formally, the game continues while \(-n\le x\le n\). It can be proven that the game will always end.Your task is to determine who will be the one who makes the last turn.
The first line contains one integer \(t\) (\(1\le t\le 100\)) β€” the number of games that Sakurako and Kosuke played.Each game is described by one number \(n\) (\(1 \le n\le 100\)) β€” the number that defines the condition when the game ends.
For each of the \(t\) games, output a line with the result of that game. If Sakurako makes the last turn, output ""Sakurako"" (without quotes); else output ""Kosuke"".
Input: 416398 | Output: Kosuke Sakurako Kosuke Sakurako
Beginner
3
967
239
167
20
710
C
710C
C. Magic Odd Square
1,500
constructive algorithms; math
Find an n Γ— n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.
The only line contains odd integer n (1 ≀ n ≀ 49).
Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.
Input: 1 | Output: 1
Medium
2
121
50
152
7
216
B
216B
B. Forming Teams
1,700
dfs and similar; implementation
One day n students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people.We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student A is an archenemy to student B, then student B is an archenemy to student A.The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench.Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last.
The first line contains two integers n and m (2 ≀ n ≀ 100, 1 ≀ m ≀ 100) β€” the number of students and the number of pairs of archenemies correspondingly.Next m lines describe enmity between students. Each enmity is described as two numbers ai and bi (1 ≀ ai, bi ≀ n, ai β‰  bi) β€” the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies.You can consider the students indexed in some manner with distinct integers from 1 to n.
Print a single integer β€” the minimum number of students you will have to send to the bench in order to start the game.
Input: 5 41 22 45 31 4 | Output: 1
Medium
2
670
537
118
2
2,101
B
2101B
B. Quartet Swapping
1,800
brute force; data structures; divide and conquer; greedy; sortings
You are given a permutation \(a\) of length \(n\)\(^{\text{βˆ—}}\). You are allowed to do the following operation any number of times (possibly zero): Choose an index \(1\le i\le n - 3\). Then, swap \(a_i\) with \(a_{i + 2}\), and \(a_{i + 1}\) with \(a_{i + 3}\) simultaneously. In other words, permutation \(a\) will be transformed from \([\ldots, a_i, a_{i+1}, a_{i+2}, a_{i+3}, \ldots]\) to \([\ldots, a_{i+2}, a_{i+3}, a_{i}, a_{i+1}, \ldots]\). Determine the lexicographically smallest permutation\(^{\text{†}}\) that can be obtained by applying the above operation any number of times.\(^{\text{βˆ—}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array). \(^{\text{†}}\)An array \(x\) is lexicographically smaller than an array \(y\) of the same size if and only if the following holds: in the first position where \(x\) and \(y\) differ, the array \(x\) has a smaller element than the corresponding element in \(y\).
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(4\le n\le 2\cdot 10^5\)) β€” the length of permutation \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β€” the elements of permutation \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
For each test case, output the lexicographically smallest permutation that can be obtained by applying the above operation any number of times.
In the first test case, an operation can be done on index \(i = 1\), and the permutation will become \([1, 2, 3, 4]\), which is the lexicographically smallest permutation achievable.In the second test case, we can do the following sequence of operations: Do an operation on index \(i = 2\). The permutation becomes \([5, 1, 2, 4, 3]\). Do an operation on index \(i = 1\). The permutation becomes \([2, 4, 5, 1, 3]\). Do an operation on index \(i = 2\). The permutation becomes \([2, 1, 3, 4, 5]\).
Input: 343 4 1 255 4 3 1 21010 9 8 7 6 5 4 3 2 1 | Output: 1 2 3 4 2 1 3 4 5 2 1 4 3 6 5 8 7 10 9
Medium
5
1,192
504
143
21
1,769
D2
1769D2
D2. Π˜Π³Ρ€Π° Π² ДСвятку II
2,200
*special; brute force
Π’ этой вСрсии Π·Π°Π΄Π°Ρ‡ΠΈ ΠΈΠ³Ρ€ΠΎΠΊΠΈ Π½Π°Ρ‡ΠΈΠ½Π°ΡŽΡ‚ ΠΈΠ³Ρ€Π°Ρ‚ΡŒ Π½Π΅ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π½Π° ΠΏΠΎΠ±Π΅Π΄Ρƒ, Π½ΠΎ ΠΈ Π½Π° ΠΎΠΏΡ‚ΠΈΠΌΠΈΠ·Π°Ρ†ΠΈΡŽ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° ΠΈΠ³Ρ€Ρ‹ для Π½ΠΈΡ…. Вводится понятиС Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Ρ‹ ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π°, ΠΈ Π½ΡƒΠΆΠ½ΠΎ Π½Π°ΠΉΡ‚ΠΈ \(13\) раскладов с Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹ΠΌΠΈ значСниями этой Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Ρ‹.Алиса ΠΈ Π‘ΠΎΠ± Ρ€Π΅ΡˆΠΈΠ»ΠΈ ΡΡ‹Π³Ρ€Π°Ρ‚ΡŒ Π² ΠΊΠ°Ρ€Ρ‚ΠΎΡ‡Π½ΡƒΡŽ ΠΈΠ³Ρ€Ρƒ «ДСвятка». ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, Π²Π½ΠΈΠΌΠ°Ρ‚Π΅Π»ΡŒΠ½ΠΎ ΠΏΡ€ΠΎΡ‡ΠΈΡ‚Π°ΠΉΡ‚Π΅ условиС Π·Π°Π΄Π°Ρ‡ΠΈ, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ ΠΏΡ€Π°Π²ΠΈΠ»Π° ΠΌΠΎΠ³ΡƒΡ‚ ΠΎΡ‚Π»ΠΈΡ‡Π°Ρ‚ΡŒΡΡ ΠΎΡ‚ извСстных Π²Π°ΠΌ.Для ΠΈΠ³Ρ€Ρ‹ Π½ΡƒΠΆΠ½Π° стандартная ΠΊΠΎΠ»ΠΎΠ΄Π° ΠΈΠ· \(36\) ΠΊΠ°Ρ€Ρ‚ β€” ΠΏΠΎ Π΄Π΅Π²ΡΡ‚ΡŒ ΠΊΠ°Ρ€Ρ‚ (ΠΎΡ‚ ΡˆΠ΅ΡΡ‚Ρ‘Ρ€ΠΊΠΈ Π΄ΠΎ Ρ‚ΡƒΠ·Π°) ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· Ρ‡Π΅Ρ‚Ρ‹Ρ€Ρ‘Ρ… мастСй (Ρ‚Ρ€Π΅Ρ„Ρ‹, Π±ΡƒΠ±Π½Ρ‹, ΠΏΠΈΠΊΠΈ ΠΈ Ρ‡Π΅Ρ€Π²ΠΈ). ΠšΠ°Ρ€Ρ‚Ρ‹ ΠΏΠΎ достоинству ΠΎΡ‚ младшСй ΠΊ ΡΡ‚Π°Ρ€ΡˆΠ΅ΠΉ ΠΈΠ΄ΡƒΡ‚ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ: ΡˆΠ΅ΡΡ‚Ρ‘Ρ€ΠΊΠ°, сСмёрка, Π²ΠΎΡΡŒΠΌΡ‘Ρ€ΠΊΠ°, дСвятка, дСсятка, Π²Π°Π»Π΅Ρ‚, Π΄Π°ΠΌΠ°, ΠΊΠΎΡ€ΠΎΠ»ΡŒ, Ρ‚ΡƒΠ·.ΠŸΠ΅Ρ€Π΅Π΄ ΠΈΠ³Ρ€ΠΎΠΉ ΠΊΠΎΠ»ΠΎΠ΄Π° ΠΏΠ΅Ρ€Π΅ΠΌΠ΅ΡˆΠΈΠ²Π°Π΅Ρ‚ΡΡ, ΠΈ ΠΊΠ°ΠΆΠ΄ΠΎΠΌΡƒ ΠΈΠ³Ρ€ΠΎΠΊΡƒ раздаётся ΠΏΠΎ \(18\) ΠΊΠ°Ρ€Ρ‚. ΠšΠ°Ρ€Ρ‚Ρ‹ Π½ΡƒΠΆΠ½ΠΎ Π²Ρ‹ΠΊΠ»Π°Π΄Ρ‹Π²Π°Ρ‚ΡŒ ΠΈΠ· Ρ€ΡƒΠΊΠΈ Π½Π° стол ΠΏΠΎ ΠΎΠΏΡ€Π΅Π΄Π΅Π»Ρ‘Π½Π½Ρ‹ΠΌ ΠΏΡ€Π°Π²ΠΈΠ»Π°ΠΌ. Π’Ρ‹ΠΈΠ³Ρ€Ρ‹Π²Π°Π΅Ρ‚ ΠΈΠ³Ρ€ΠΎΠΊ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΏΠ΅Ρ€Π²Ρ‹ΠΌ Π²Ρ‹Π»ΠΎΠΆΠΈΡ‚ всС ΠΊΠ°Ρ€Ρ‚Ρ‹ ΠΈΠ· своСй Ρ€ΡƒΠΊΠΈ.Π˜Π³Ρ€ΠΎΠΊΠΈ ходят ΠΏΠΎ ΠΎΡ‡Π΅Ρ€Π΅Π΄ΠΈ. Π₯ΠΎΠ΄ ΠΈΠ³Ρ€ΠΎΠΊΠ° ΠΈΠΌΠ΅Π΅Ρ‚ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… Π²ΠΈΠ΄ΠΎΠ²: Π²Ρ‹Π»ΠΎΠΆΠΈΡ‚ΡŒ Π½Π° стол ΠΈΠ· своСй Ρ€ΡƒΠΊΠΈ дСвятку любой масти; Π²Ρ‹Π»ΠΎΠΆΠΈΡ‚ΡŒ Π½Π° стол ΡˆΠ΅ΡΡ‚Ρ‘Ρ€ΠΊΡƒ, сСмёрку ΠΈΠ»ΠΈ Π²ΠΎΡΡŒΠΌΡ‘Ρ€ΠΊΡƒ любой масти, Ссли Π½Π° столС ΡƒΠΆΠ΅ Π»Π΅ΠΆΠΈΡ‚ ΠΊΠ°Ρ€Ρ‚Π° Ρ‚ΠΎΠΉ ΠΆΠ΅ масти достоинством Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡ†Ρƒ Π²Ρ‹ΡˆΠ΅; Π²Ρ‹Π»ΠΎΠΆΠΈΡ‚ΡŒ Π½Π° стол дСсятку, Π²Π°Π»Π΅Ρ‚Π°, Π΄Π°ΠΌΡƒ, короля ΠΈΠ»ΠΈ Ρ‚ΡƒΠ·Π° любой масти, Ссли Π½Π° столС ΡƒΠΆΠ΅ Π»Π΅ΠΆΠΈΡ‚ ΠΊΠ°Ρ€Ρ‚Π° Ρ‚ΠΎΠΉ ΠΆΠ΅ масти достоинством Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡ†Ρƒ Π½ΠΈΠΆΠ΅. НапримСр, дСвятку ΠΏΠΈΠΊ ΠΌΠΎΠΆΠ½ΠΎ Π²Ρ‹Π»ΠΎΠΆΠΈΡ‚ΡŒ Π½Π° стол Π² любой ΠΌΠΎΠΌΠ΅Π½Ρ‚, для выкладывания сСмёрки Ρ‚Ρ€Π΅Ρ„ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ Π½Π° столС Π²ΠΎΡΡŒΠΌΡ‘Ρ€ΠΊΠΈ Ρ‚Ρ€Π΅Ρ„, Π° для выкладывания Ρ‚ΡƒΠ·Π° Ρ‡Π΅Ρ€Π²Π΅ΠΉ Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°Π»ΠΈΡ‡ΠΈΠ΅ Π½Π° столС короля Ρ‡Π΅Ρ€Π²Π΅ΠΉ.Если ΠΈΠ³Ρ€ΠΎΠΊ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π²Ρ‹Π»ΠΎΠΆΠΈΡ‚ΡŒ Π½Π° стол Π½ΠΈ ΠΎΠ΄Π½Ρƒ ΠΊΠ°Ρ€Ρ‚Ρƒ ΠΈΠ· своСй Ρ€ΡƒΠΊΠΈ, Ρ‚ΠΎ Ρ…ΠΎΠ΄ ΠΏΠ΅Ρ€Π΅Ρ…ΠΎΠ΄ΠΈΡ‚ ΠΊ сопСрнику. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅: нСльзя ΠΏΡ€ΠΎΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ Ρ…ΠΎΠ΄ просто Ρ‚Π°ΠΊ β€” всСгда Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΠΎ Π²Ρ‹Π»ΠΎΠΆΠΈΡ‚ΡŒ ΠΊΠ°Ρ€Ρ‚Ρƒ Π½Π° стол ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ, Ссли это Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ.Помимо Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΈΠ³Ρ€ΠΎΠΊ стрСмится ΠΈΠ·Π±Π°Π²ΠΈΡ‚ΡŒΡΡ ΠΎΡ‚ ΠΊΠ°Ρ€Ρ‚ Π² своСй Ρ€ΡƒΠΊΠ΅, Алиса ΠΈ Π‘ΠΎΠ± Ρ‚Π°ΠΊΠΆΠ΅ хотят, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π² ΠΊΠΎΠ½Ρ†Π΅ ΠΈΠ³Ρ€Ρ‹ Π² Ρ€ΡƒΠΊΠ΅ Ρƒ ΠΈΡ… сопСрника ΠΊΠ°Ρ€Ρ‚ ΠΎΡΡ‚Π°Π»ΠΎΡΡŒ ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ большС, Π° Π² ΠΈΡ… Ρ€ΡƒΠΊΠ΅ β€” ΠΊΠ°ΠΊ ΠΌΠΎΠΆΠ½ΠΎ мСньшС. Напомним, Ρ‡Ρ‚ΠΎ ΠΈΠ³Ρ€Π° заканчиваСтся, ΠΊΠ°ΠΊ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² Π²Ρ‹ΠΊΠ»Π°Π΄Ρ‹Π²Π°Π΅Ρ‚ Π½Π° стол послСднюю ΠΊΠ°Ρ€Ρ‚Ρƒ ΠΈΠ· своСй Ρ€ΡƒΠΊΠΈ.Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ΠΎΠΌ ΠΈΠ³Ρ€Ρ‹ Π½Π°Π·ΠΎΠ²Ρ‘ΠΌ ΡΠΎΠ²ΠΎΠΊΡƒΠΏΠ½ΠΎΡΡ‚ΡŒ ΠΈΠ· ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ ΠΎ Ρ‚ΠΎΠΌ, ΠΊΡ‚ΠΎ ΠΈΠ· Π΄Π²ΡƒΡ… ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² Π²Ρ‹ΠΈΠ³Ρ€Π°Π΅Ρ‚ ΠΏΡ€ΠΈ ΠΎΠΏΡ‚ΠΈΠΌΠ°Π»ΡŒΠ½ΠΎΠΉ ΠΈΠ³Ρ€Π΅, Π° Ρ‚Π°ΠΊΠΆΠ΅ ΠΎ Ρ‚ΠΎΠΌ, сколько ΠΊΠ°Ρ€Ρ‚ останСтся Π² Ρ€ΡƒΠΊΠ΅ Ρƒ ΠΏΡ€ΠΎΠΈΠ³Ρ€Π°Π²ΡˆΠ΅Π³ΠΎ.ΠŸΡƒΡΡ‚ΡŒ Алиса ΠΈ Π‘ΠΎΠ± ΡƒΠΆΠ΅ взяли Π² Ρ€ΡƒΠΊΠΈ свои \(18\) ΠΊΠ°Ρ€Ρ‚ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ, Π½ΠΎ Π΅Ρ‰Ρ‘ Π½Π΅ Ρ€Π΅ΡˆΠΈΠ»ΠΈ, ΠΊΡ‚ΠΎ ΠΈΠ· Π½ΠΈΡ… Π±ΡƒΠ΄Π΅Ρ‚ Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ ΠΏΠ΅Ρ€Π²Ρ‹ΠΌ. Π’Π΅Π»ΠΈΡ‡ΠΈΠ½ΠΎΠΉ ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π° для Π΄Π°Π½Π½ΠΎΠ³ΠΎ расклада Π½Π°Π·ΠΎΠ²Ρ‘ΠΌ Π°Π±ΡΠΎΠ»ΡŽΡ‚Π½ΡƒΡŽ Ρ€Π°Π·Π½ΠΎΡΡ‚ΡŒ ΠΌΠ΅ΠΆΠ΄Ρƒ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π°ΠΌΠΈ ΠΈΠ³Ρ€Ρ‹ Π² случаС, Ссли ΠΏΠ΅Ρ€Π²ΠΎΠΉ Π±ΡƒΠ΄Π΅Ρ‚ Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ Алиса, ΠΈ Π² случаС, Ссли ΠΏΠ΅Ρ€Π²Ρ‹ΠΌ Π±ΡƒΠ΄Π΅Ρ‚ Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ Π‘ΠΎΠ±.НапримСр, Ссли Π² ΠΎΠ±ΠΎΠΈΡ… случаях Π²Ρ‹ΠΈΠ³Ρ€Π°Π΅Ρ‚ Π‘ΠΎΠ±, Π½ΠΎ Π² ΠΎΠ΄Π½ΠΎΠΌ случаС Ρƒ Алисы останСтся \(6\) ΠΊΠ°Ρ€Ρ‚ Π² Ρ€ΡƒΠΊΠ΅ Π² ΠΊΠΎΠ½Ρ†Π΅ ΠΈΠ³Ρ€Ρ‹, Π° Π²ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ β€” всСго \(2\), Ρ‚ΠΎ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Π° ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π° Ρ€Π°Π²Π½Π° \(4\). Если ΠΆΠ΅ Π² ΠΎΠ΄Π½ΠΎΠΌ случаС Π²Ρ‹ΠΈΠ³Ρ€Π°Π΅Ρ‚ Алиса ΠΈ Ρƒ Π‘ΠΎΠ±Π° останСтся \(5\) ΠΊΠ°Ρ€Ρ‚ Π² Ρ€ΡƒΠΊΠ΅, Π° Π²ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ случаС Π²Ρ‹ΠΈΠ³Ρ€Π°Π΅Ρ‚ Π‘ΠΎΠ± ΠΈ Ρƒ Алисы останСтся \(3\) ΠΊΠ°Ρ€Ρ‚Ρ‹ Π² Ρ€ΡƒΠΊΠ΅, Ρ‚ΠΎ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Π° ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π° Ρ€Π°Π²Π½Π° \(8\).РСбята хотят ΡƒΠ·Π½Π°Ρ‚ΡŒ, насколько Ρ€Π°Π·Π½ΠΎΠΉ Π±Ρ‹Π²Π°Π΅Ρ‚ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Π° ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π° для Ρ€Π°Π·Π½Ρ‹Ρ… раскладов. По Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡƒ числу \(k \le 13\) ΠΏΠΎΠΌΠΎΠ³ΠΈΡ‚Π΅ ΠΈΠΌ Π½Π°ΠΉΡ‚ΠΈ Ρ‚Π°ΠΊΠΈΠ΅ \(k\) раскладов, Ρ‡Ρ‚ΠΎ Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Ρ‹ ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π° для всСх Π½ΠΈΡ… β€” Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹Π΅ Ρ†Π΅Π»Ρ‹Π΅ числа.
Π’ СдинствСнной строкС Π·Π°Π΄Π°Π½ΠΎ Ρ†Π΅Π»ΠΎΠ΅ число \(k\) (\(2 \le k \le 13\)) β€” число Π½Π΅ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΠΌΡ‹Ρ… раскладов.Π’ Π·Π°Π΄Π°Ρ‡Π΅ Π΄Π²Π° тСста. Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ тСстС \(k = 2\), Π²ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ тСстС \(k = 13\).
Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ \(k\) ΠΏΠ°Ρ€ строк. КаТдая ΠΏΠ°Ρ€Π° строк Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΠΎΠ²Π°Ρ‚ΡŒ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ раскладу. Π’Π΅Π»ΠΈΡ‡ΠΈΠ½Ρ‹ ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π° для всСх Π²Ρ‹Π²Π΅Π΄Π΅Π½Π½Ρ‹Ρ… раскладов Π΄ΠΎΠ»ΠΆΠ½Ρ‹ Π±Ρ‹Ρ‚ΡŒ Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹ΠΌΠΈ Ρ†Π΅Π»Ρ‹ΠΌΠΈ числами.Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΏΠ°Ρ€Ρ‹ Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ \(18\) строк Π΄Π»ΠΈΠ½Ρ‹ \(2\) Ρ‡Π΅Ρ€Π΅Π· ΠΏΡ€ΠΎΠ±Π΅Π», ΠΎΠΏΠΈΡΡ‹Π²Π°ΡŽΡ‰ΠΈΡ… ΠΊΠ°Ρ€Ρ‚Ρ‹ Алисы Π² любом порядкС. ΠŸΠ΅Ρ€Π²Ρ‹ΠΉ символ строки Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°Ρ‚ΡŒ достоинство ΠΊΠ°Ρ€Ρ‚Ρ‹ β€” символ ΠΈΠ· Π½Π°Π±ΠΎΡ€Π° 6, 7, 8, 9, T, J, Q, K, A, ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°ΡŽΡ‰ΠΈΠΉ ΡˆΠ΅ΡΡ‚Ρ‘Ρ€ΠΊΡƒ, сСмёрку, Π²ΠΎΡΡŒΠΌΡ‘Ρ€ΠΊΡƒ, дСвятку, дСсятку, Π²Π°Π»Π΅Ρ‚Π°, Π΄Π°ΠΌΡƒ, короля ΠΈ Ρ‚ΡƒΠ·Π° соотвСтствСнно. Π’Ρ‚ΠΎΡ€ΠΎΠΉ символ строки Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°Ρ‚ΡŒ ΠΌΠ°ΡΡ‚ΡŒ ΠΊΠ°Ρ€Ρ‚Ρ‹ β€” символ ΠΈΠ· Π½Π°Π±ΠΎΡ€Π° C, D, S, H, ΠΎΠ±ΠΎΠ·Π½Π°Ρ‡Π°ΡŽΡ‰ΠΈΠΉ Ρ‚Ρ€Π΅Ρ„Ρ‹, Π±ΡƒΠ±Π½Ρ‹, ΠΏΠΈΠΊΠΈ ΠΈ Ρ‡Π΅Ρ€Π²ΠΈ соотвСтствСнно.Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ \(18\) строк Π΄Π»ΠΈΠ½Ρ‹ \(2\) Ρ‡Π΅Ρ€Π΅Π· ΠΏΡ€ΠΎΠ±Π΅Π», ΠΎΠΏΠΈΡΡ‹Π²Π°ΡŽΡ‰ΠΈΡ… ΠΊΠ°Ρ€Ρ‚Ρ‹ Π‘ΠΎΠ±Π° Π² Ρ‚ΠΎΠΌ ΠΆΠ΅ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅.КаТдая ΠΈΠ· \(36\) Π²ΠΎΠ·ΠΌΠΎΠΆΠ½Ρ‹Ρ… ΠΊΠ°Ρ€Ρ‚ Π΄ΠΎΠ»ΠΆΠ½Π° Π½Π°Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒΡΡ Π² Ρ€ΡƒΠΊΠ΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΈΠ· Π΄Π²ΡƒΡ… ΠΈΠ³Ρ€ΠΎΠΊΠΎΠ² Π² СдинствСнном экзСмплярС.
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ Π²Ρ‹Π²Π΅Π΄Π΅Π½Π½ΠΎΠΌ раскладС всС дСвятки находятся Π² Ρ€ΡƒΠΊΠ΅ Ρƒ Алисы. Π”Π°ΠΆΠ΅ Ссли Π‘ΠΎΠ± Π±ΡƒΠ΄Π΅Ρ‚ Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ ΠΏΠ΅Ρ€Π²Ρ‹ΠΌ, Π΅ΠΌΡƒ всё Ρ€Π°Π²Π½ΠΎ придётся ΠΏΡ€ΠΎΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ ΠΆΠ΅ свой Ρ…ΠΎΠ΄. Π‘Π»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ, ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ Ρ…ΠΎΠ΄ ΠΏΡ€ΠΈ Ρ‚Π°ΠΊΠΎΠΌ раскладС ΠΈΠΌΠ΅Π΅Ρ‚ Π²Π°ΠΆΠ½ΠΎΡΡ‚ΡŒ \(0\).Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ Π²Ρ‹Π²Π΅Π΄Π΅Π½Π½ΠΎΠΌ раскладС Π²Π½Π΅ зависимости ΠΎΡ‚ Ρ‚ΠΎΠ³ΠΎ, Ρ‡ΡŒΠΈΠΌ Π±ΡƒΠ΄Π΅Ρ‚ ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ Ρ…ΠΎΠ΄, Π²Ρ‹ΠΈΠ³Ρ€Π°Π΅Ρ‚ Алиса. Однако Ссли Алиса Π±ΡƒΠ΄Π΅Ρ‚ Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ ΠΏΠ΅Ρ€Π²ΠΎΠΉ, Ρ‚ΠΎ Ρƒ Π‘ΠΎΠ±Π° Π² ΠΊΠΎΠ½Ρ†Π΅ ΠΈΠ³Ρ€Ρ‹ Π² Ρ€ΡƒΠΊΠ΅ останСтся ΠΎΠ΄Π½Π° ΠΊΠ°Ρ€Ρ‚Π°, Π° Ссли ΠΆΠ΅ ΠΎΠ½Π° Π±ΡƒΠ΄Π΅Ρ‚ Ρ…ΠΎΠ΄ΠΈΡ‚ΡŒ Π²Ρ‚ΠΎΡ€ΠΎΠΉ, Ρ‚ΠΎ Ρƒ Π‘ΠΎΠ±Π° останСтся ΠΏΡΡ‚ΡŒ ΠΊΠ°Ρ€Ρ‚. БоотвСтствСнно, Π²Π΅Π»ΠΈΡ‡ΠΈΠ½Π° ваТности ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ Ρ…ΠΎΠ΄Π° ΠΏΡ€ΠΈ Ρ‚Π°ΠΊΠΎΠΌ раскладС Ρ€Π°Π²Π½Π° \(4\).
Input: 2 | Output: KS QD 8D QC 8S 8C JD 9H AC TH 9S 9D QH 7H 8H TS 7S 9C 6D JS 7D KH QS TC AD AS KC 6C 7C TD AH KD 6S JC JH 6H JC JS 8S TD JD KH 7D 9C KC TH QD 8D 7H TC KD 9H 8C 6D 7S AC QH AD 8H TS 6H JH 6C AH 7C 6S 9D QC AS QS KS 9S
Hard
2
3,116
171
860
17
1,198
B
1198B
B. Welfare State
1,600
binary search; brute force; data structures; sortings
There is a country with \(n\) citizens. The \(i\)-th of them initially has \(a_{i}\) money. The government strictly controls the wealth of its citizens. Whenever a citizen makes a purchase or earns some money, they must send a receipt to the social services mentioning the amount of money they currently have.Sometimes the government makes payouts to the poor: all citizens who have strictly less money than \(x\) are paid accordingly so that after the payout they have exactly \(x\) money. In this case the citizens don't send a receipt.You know the initial wealth of every citizen and the log of all events: receipts and payouts. Restore the amount of money each citizen has after all events.
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^{5}\)) β€” the numer of citizens.The next line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_n\) (\(0 \le a_{i} \le 10^{9}\)) β€” the initial balances of citizens.The next line contains a single integer \(q\) (\(1 \le q \le 2 \cdot 10^{5}\)) β€” the number of events.Each of the next \(q\) lines contains a single event. The events are given in chronological order.Each event is described as either 1 p x (\(1 \le p \le n\), \(0 \le x \le 10^{9}\)), or 2 x (\(0 \le x \le 10^{9}\)). In the first case we have a receipt that the balance of the \(p\)-th person becomes equal to \(x\). In the second case we have a payoff with parameter \(x\).
Print \(n\) integers β€” the balances of all citizens after all events.
In the first example the balances change as follows: 1 2 3 4 \(\rightarrow\) 3 3 3 4 \(\rightarrow\) 3 2 3 4 \(\rightarrow\) 3 2 3 4In the second example the balances change as follows: 3 50 2 1 10 \(\rightarrow\) 3 0 2 1 10 \(\rightarrow\) 8 8 8 8 10 \(\rightarrow\) 8 8 20 8 10
Input: 4 1 2 3 4 3 2 3 1 2 2 2 1 | Output: 3 2 3 4
Medium
4
694
712
69
11
138
B
138B
B. Digits Permutations
1,900
greedy
Andrey's favourite number is n. Andrey's friends gave him two identical numbers n as a New Year present. He hung them on a wall and watched them adoringly.Then Andrey got bored from looking at the same number and he started to swap digits first in one, then in the other number, then again in the first number and so on (arbitrary number of changes could be made in each number). At some point it turned out that if we sum the resulting numbers, then the number of zeroes with which the sum will end would be maximum among the possible variants of digit permutations in those numbers.Given number n, can you find the two digit permutations that have this property?
The first line contains a positive integer n β€” the original number. The number of digits in this number does not exceed 105. The number is written without any leading zeroes.
Print two permutations of digits of number n, such that the sum of these numbers ends with the maximum number of zeroes. The permutations can have leading zeroes (if they are present, they all should be printed). The permutations do not have to be different. If there are several answers, print any of them.
Input: 198 | Output: 981819
Hard
1
664
174
307
1
1,994
E
1994E
E. Wooden Game
2,000
bitmasks; greedy; math; trees
You are given a forest of \(k\) rooted trees\(^{\text{βˆ—}}\). Lumberjack Timofey wants to cut down the entire forest by applying the following operation: Select a subtree\(^{\text{†}}\) of any vertex of one of the trees and remove it from the tree. Timofey loves bitwise operations, so he wants the bitwise OR of the sizes of the subtrees he removed to be maximum. Help him and find the maximum result he can obtain.\(^{\text{βˆ—}}\) A tree is a connected graph without cycles, loops, or multiple edges. In a rooted tree, a selected vertex is called a root. A forest is a collection of one or more trees.\(^{\text{†}}\) The subtree of a vertex \(v\) is the set of vertices for which \(v\) lies on the shortest path from this vertex to the root, including \(v\) itself.
Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. Then follows the description of the test cases.The first line of each test case contains a single integer \(k\) (\(1 \leq k \leq 10^6\)) β€” the number of trees in the forest.This is followed by a description of each of the \(k\) trees:The first line contains a single integer \(n\) (\(1 \leq n \leq 10^6\)) β€” the size of the tree. The vertices of the tree are numbered with integers from \(1\) to \(n\). The root of the tree is vertex number \(1\).The second line contains \(n - 1\) integers \(p_2, p_3, \ldots p_n\) (\(1 \leq p_i < i\)), where \(p_i\) β€” the parent of vertex \(i\).It is guaranteed that the sum of \(k\) and \(n\) for all sets of input data does not exceed \(10^6\).
For each test case, output a single integer β€” the maximum result that can be obtained.
In the second test case, the trees look like this:The first operation removes the entire second tree.The second operation removes vertex \(4\) from the first tree.The third operation removes the first tree. The result is \(6|1|3 = 7\) (\(|\) denotes bitwise OR).In the third test case, the entire tree needs to be removed.
Input: 311241 2 261 1 3 1 31101 2 2 1 1 5 7 6 4 | Output: 1 7 10
Hard
4
765
819
86
19
1,475
G
1475G
G. Strange Beauty
1,900
dp; math; number theory; sortings
Polycarp found on the street an array \(a\) of \(n\) elements.Polycarp invented his criterion for the beauty of an array. He calls an array \(a\) beautiful if at least one of the following conditions must be met for each different pair of indices \(i \ne j\): \(a_i\) is divisible by \(a_j\); or \(a_j\) is divisible by \(a_i\). For example, if: \(n=5\) and \(a=[7, 9, 3, 14, 63]\), then the \(a\) array is not beautiful (for \(i=4\) and \(j=2\), none of the conditions above is met); \(n=3\) and \(a=[2, 14, 42]\), then the \(a\) array is beautiful; \(n=4\) and \(a=[45, 9, 3, 18]\), then the \(a\) array is not beautiful (for \(i=1\) and \(j=4\) none of the conditions above is met); Ugly arrays upset Polycarp, so he wants to remove some elements from the array \(a\) so that it becomes beautiful. Help Polycarp determine the smallest number of elements to remove to make the array \(a\) beautiful.
The first line contains one integer \(t\) (\(1 \leq t \leq 10\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the length of the array \(a\).The second line of each test case contains \(n\) numbers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)) β€” elements of the array \(a\).
For each test case output one integer β€” the minimum number of elements that must be removed to make the array \(a\) beautiful.
In the first test case, removing \(7\) and \(14\) will make array \(a\) beautiful.In the second test case, the array \(a\) is already beautiful.In the third test case, removing one of the elements \(45\) or \(18\) will make the array \(a\) beautiful.In the fourth test case, the array \(a\) is beautiful.
Input: 4 5 7 9 3 14 63 3 2 14 42 4 45 9 3 18 3 2 2 8 | Output: 2 0 1 0
Hard
4
901
393
126
14
933
E
933E
E. A Preponderant Reunion
3,200
constructive algorithms; dp
East or west, home is best. That's why family reunion, the indispensable necessity of Lunar New Year celebration, is put in such a position.After the reunion dinner, Little Tommy plays a game with the family. Here is a concise introduction to this game: There is a sequence of n non-negative integers p1, p2, ..., pn in the beginning. It is ruled that each integer in this sequence should be non-negative at any time. You can select two consecutive positive integers in this sequence, pi and pi + 1 (1 ≀ i < n), and then decrease them by their minimum (i. e. min(pi, pi + 1)), the cost of this operation is equal to min(pi, pi + 1). We call such operation as a descension. The game immediately ends when there are no two consecutive positive integers. Your task is to end the game so that the total cost of your operations is as small as possible. Obviously, every game ends after at most n - 1 descensions. Please share your solution of this game with the lowest cost.
The first line contains one integer n (1 ≀ n ≀ 3Β·105).The second line contains n space-separated integers p1, p2, ..., pn (0 ≀ pi ≀ 109, i = 1, 2, ..., n).
In the first line print one integer as the number of descensions m (0 ≀ m ≀ n - 1).In the next m lines print the descensions chronologically. More precisely, in each line of the next m lines print one integer i (1 ≀ i < n) representing a descension would operate on pi and pi + 1 such that all the descensions could be utilized from top to bottom.If there are many possible solutions to reach the minimal cost, print any of them.
In the first sample, one possible best solution is , of which the cost is 1 + 1 = 2.In the second sample, one possible best solution is , of which the cost is 1 + 1 + 1 = 3.
Input: 42 1 3 1 | Output: 213
Master
2
969
155
429
9
1,849
D
1849D
D. Array Painting
1,700
constructive algorithms; greedy; two pointers
You are given an array of \(n\) integers, where each integer is either \(0\), \(1\), or \(2\). Initially, each element of the array is blue.Your goal is to paint each element of the array red. In order to do so, you can perform operations of two types: pay one coin to choose a blue element and paint it red; choose a red element which is not equal to \(0\) and a blue element adjacent to it, decrease the chosen red element by \(1\), and paint the chosen blue element red. What is the minimum number of coins you have to spend to achieve your goal?
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2\)).
Print one integer β€” the minimum number of coins you have to spend in order to paint all elements red.
In the first example, you can paint all elements red with having to spend only one coin as follows: paint the \(2\)-nd element red by spending one coin; decrease the \(2\)-nd element by \(1\) and paint the \(1\)-st element red; decrease the \(2\)-nd element by \(1\) and paint the \(3\)-rd element red. In the second example, you can paint all elements red spending only two coins as follows: paint the \(4\)-th element red by spending one coin; decrease the \(4\)-th element by \(1\) and paint the \(3\)-rd element red; paint the \(1\)-st element red by spending one coin; decrease the \(3\)-rd element by \(1\) and paint the \(2\)-nd element red.
Input: 3 0 2 0 | Output: 1
Medium
3
549
160
101
18
268
A
268A
A. Games
800
brute force
Manao works on a sports TV. He's spent much time watching the football games of some country. After a while he began to notice different patterns. For example, each team has two sets of uniforms: home uniform and guest uniform. When a team plays a game at home, the players put on the home uniform. When a team plays as a guest on somebody else's stadium, the players put on the guest uniform. The only exception to that rule is: when the home uniform color of the host team matches the guests' uniform, the host team puts on its guest uniform as well. For each team the color of the home and guest uniform is different.There are n teams taking part in the national championship. The championship consists of nΒ·(n - 1) games: each team invites each other team to its stadium. At this point Manao wondered: how many times during the championship is a host team going to put on the guest uniform? Note that the order of the games does not affect this number.You know the colors of the home and guest uniform for each team. For simplicity, the colors are numbered by integers in such a way that no two distinct colors have the same number. Help Manao find the answer to his question.
The first line contains an integer n (2 ≀ n ≀ 30). Each of the following n lines contains a pair of distinct space-separated integers hi, ai (1 ≀ hi, ai ≀ 100) β€” the colors of the i-th team's home and guest uniforms, respectively.
In a single line print the number of games where the host team is going to play in the guest uniform.
In the first test case the championship consists of 6 games. The only game with the event in question is the game between teams 2 and 1 on the stadium of team 2.In the second test sample the host team will have to wear guest uniform in the games between teams: 1 and 2, 2 and 1, 2 and 3, 3 and 4, 4 and 2 (the host team is written first).
Input: 31 22 43 4 | Output: 1
Beginner
1
1,180
230
101
2
1,477
E
1477E
E. Nezzar and Tournaments
3,300
data structures; greedy
In the famous Oh-Suit-United tournament, two teams are playing against each other for the grand prize of precious pepper points.The first team consists of \(n\) players, and the second team consists of \(m\) players. Each player has a potential: the potential of the \(i\)-th player in the first team is \(a_i\), and the potential of the \(i\)-th player in the second team is \(b_i\).In the tournament, all players will be on the stage in some order. There will be a scoring device, initially assigned to an integer \(k\), which will be used to value the performance of all players.The scores for all players will be assigned in the order they appear on the stage. Let the potential of the current player be \(x\), and the potential of the previous player be \(y\) (\(y\) equals \(x\) for the first player). Then, \(x-y\) is added to the value in the scoring device, Afterwards, if the value in the scoring device becomes negative, the value will be reset to \(0\). Lastly, the player's score is assigned to the current value on the scoring device. The score of a team is the sum of the scores of all its members.As an insane fan of the first team, Nezzar desperately wants the biggest win for the first team. He now wonders what is the maximum difference between scores of the first team and the second team.Formally, let the score of the first team be \(score_f\) and the score of the second team be \(score_s\). Nezzar wants to find the maximum value of \(score_f - score_s\) over all possible orders of players on the stage.However, situation often changes and there are \(q\) events that will happen. There are three types of events: \(1\) \(pos\) \(x\) β€” change \(a_{pos}\) to \(x\); \(2\) \(pos\) \(x\) β€” change \(b_{pos}\) to \(x\); \(3\) \(x\) β€” tournament is held with \(k = x\) and Nezzar wants you to compute the maximum value of \(score_f - score_s\). Can you help Nezzar to answer the queries of the third type?
The first line contains three integers \(n\), \(m\), and \(q\) (\(1 \le n,m \le 2 \cdot 10^5, 1 \le q \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^6\)).The third line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(0 \le b_i \le 10^6\)).The following \(q\) lines contain descriptions of events, described in the statement, each in one of the three possible formats: \(1\) \(pos\) \(x\) (\(1 \le pos \le n\), \(0 \le x \le 10^6\)); \(2\) \(pos\) \(x\) (\(1 \le pos \le m\), \(0 \le x \le 10^6\)); \(3\) \(x\) (\(0 \le x \le 10^6\)).
For each query of the third type print the answer to this query.
In the first query of the first test, the tournament is held with \(k = 5\). It would be optimal to arrange players in such way (here their potentials are written):\(\underline{7}\), \(3\), \(5\), \(4\), \(6\), \(\underline{1}\), \(\underline{2}\) (underlined numbers are potentials of players that are from the first team). The individual scores of players, numbered in the order of their appearance, are: \(\max(5 + (7 - 7), 0) = 5\) for the \(\underline{1}\)-st player; \(\max(5 + (3 - 7), 0) = 1\) for the \(2\)-nd player; \(\max(1 + (5 - 3), 0) = 3\) for the \(3\)-rd player; \(\max(3 + (4 - 5), 0) = 2\) for the \(4\)-th player; \(\max(2 + (6 - 4), 0) = 4\) for the \(5\)-th player; \(\max(4 + (1 - 6), 0) = 0\) for the \(\underline{6}\)-th player; \(\max(0 + (2 - 1), 0) = 1\) for the \(\underline{7}\)-th player. So, \(score_f = 5 + 0 + 1 = 6\) and \(score_s = 1 + 3 + 2 + 4 = 10\). The score difference is \(6 - 10 = -4\). It can be proven, that it is the maximum possible score difference.
Input: 3 4 3 1 2 7 3 4 5 6 3 5 1 1 10 3 5 | Output: -4 9
Master
2
1,925
597
64
14
1,613
D
1613D
D. MEX Sequences
1,900
dp; math
Let's call a sequence of integers \(x_1, x_2, \dots, x_k\) MEX-correct if for all \(i\) (\(1 \le i \le k\)) \(|x_i - \operatorname{MEX}(x_1, x_2, \dots, x_i)| \le 1\) holds. Where \(\operatorname{MEX}(x_1, \dots, x_k)\) is the minimum non-negative integer that doesn't belong to the set \(x_1, \dots, x_k\). For example, \(\operatorname{MEX}(1, 0, 1, 3) = 2\) and \(\operatorname{MEX}(2, 1, 5) = 0\).You are given an array \(a\) consisting of \(n\) non-negative integers. Calculate the number of non-empty MEX-correct subsequences of a given array. The number of subsequences can be very large, so print it modulo \(998244353\). Note: a subsequence of an array \(a\) is a sequence \([a_{i_1}, a_{i_2}, \dots, a_{i_m}]\) meeting the constraints \(1 \le i_1 < i_2 < \dots < i_m \le n\). If two different ways to choose the sequence of indices \([i_1, i_2, \dots, i_m]\) yield the same subsequence, the resulting subsequence should be counted twice (i. e. two subsequences are different if their sequences of indices \([i_1, i_2, \dots, i_m]\) are not the same).
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le n\)).The sum of \(n\) over all test cases doesn't exceed \(5 \cdot 10^5\).
For each test case, print a single integer β€” the number of non-empty MEX-correct subsequences of a given array, taken modulo \(998244353\).
In the first example, the valid subsequences are \([0]\), \([1]\), \([0,1]\) and \([0,2]\).In the second example, the valid subsequences are \([0]\) and \([1]\).In the third example, any non-empty subsequence is valid.
Input: 4 3 0 2 1 2 1 0 5 0 0 0 0 0 4 0 1 2 3 | Output: 4 2 31 7
Hard
2
1,059
349
139
16
1,307
E
1307E
E. Cow and Treats
2,500
binary search; combinatorics; dp; greedy; implementation; math
After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass!On the field, there is a row of \(n\) units of grass, each with a sweetness \(s_i\). Farmer John has \(m\) cows, each with a favorite sweetness \(f_i\) and a hunger value \(h_i\). He would like to pick two disjoint subsets of cows to line up on the left and right side of the grass row. There is no restriction on how many cows must be on either side. The cows will be treated in the following manner: The cows from the left and right side will take turns feeding in an order decided by Farmer John. When a cow feeds, it walks towards the other end without changing direction and eats grass of its favorite sweetness until it eats \(h_i\) units. The moment a cow eats \(h_i\) units, it will fall asleep there, preventing further cows from passing it from both directions. If it encounters another sleeping cow or reaches the end of the grass row, it will get upset. Farmer John absolutely does not want any cows to get upset. Note that grass does not grow back. Also, to prevent cows from getting upset, not every cow has to feed since FJ can choose a subset of them. Surprisingly, FJ has determined that sleeping cows are the most satisfied. If FJ orders optimally, what is the maximum number of sleeping cows that can result, and how many ways can FJ choose the subset of cows on the left and right side to achieve that maximum number of sleeping cows (modulo \(10^9+7\))? The order in which FJ sends the cows does not matter as long as no cows get upset.
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 5000\), \(1 \le m \le 5000\)) β€” the number of units of grass and the number of cows. The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(1 \le s_i \le n\)) β€” the sweetness values of the grass.The \(i\)-th of the following \(m\) lines contains two integers \(f_i\) and \(h_i\) (\(1 \le f_i, h_i \le n\)) β€” the favorite sweetness and hunger value of the \(i\)-th cow. No two cows have the same hunger and favorite sweetness simultaneously.
Output two integers β€” the maximum number of sleeping cows that can result and the number of ways modulo \(10^9+7\).
In the first example, FJ can line up the cows as follows to achieve \(2\) sleeping cows: Cow \(1\) is lined up on the left side and cow \(2\) is lined up on the right side. Cow \(2\) is lined up on the left side and cow \(1\) is lined up on the right side. In the second example, FJ can line up the cows as follows to achieve \(1\) sleeping cow: Cow \(1\) is lined up on the left side. Cow \(2\) is lined up on the left side. Cow \(1\) is lined up on the right side. Cow \(2\) is lined up on the right side. In the third example, FJ can line up the cows as follows to achieve \(2\) sleeping cows: Cow \(1\) and \(2\) are lined up on the left side. Cow \(1\) and \(2\) are lined up on the right side. Cow \(1\) is lined up on the left side and cow \(2\) is lined up on the right side. Cow \(1\) is lined up on the right side and cow \(2\) is lined up on the left side. In the fourth example, FJ cannot end up with any sleeping cows, so there will be no cows lined up on either side.
Input: 5 2 1 1 1 1 1 1 2 1 3 | Output: 2 2
Expert
6
1,574
522
115
13
1,810
B
1810B
B. Candies
800
constructive algorithms; math; number theory
This problem is about candy. Initially, you only have \(1\) candy, and you want to have exactly \(n\) candies.You can use the two following spells in any order at most \(40\) times in total. Assume you have \(x\) candies now. If you use the first spell, then \(x\) candies become \(2x-1\) candies. Assume you have \(x\) candies now. If you use the second spell, then \(x\) candies become \(2x+1\) candies. Construct a sequence of spells, such that after using them in order, you will have exactly \(n\) candies, or determine it's impossible.
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Their description follows.Each test case contains one line with a single integer \(n\) (\(2 \le n \le 10^9\)) β€” the required final number of candies.
For each test case, output the following.If it's possible to eventually have \(n\) candies within \(40\) spells, in the first line print an integer \(m\) (\(1 \le m \le 40\)), representing the total number of spells you use.In the second print \(m\) integers \(a_{1}, a_{2}, \ldots, a_{m}\) (\(a_{i}\) is \(1\) or \(2\)) separated by spaces, where \(a_{i} = 1\) means that you use the first spell in the \(i\)-th step, while \(a_{i} = 2\) means that you use the second spell in the \(i\)-th step.Note that you do not have to minimize \(m\), and if there are multiple solutions, you may output any one of them.If it's impossible, output \(-1\) in one line.
For \(n=3\), you can just use the second spell once, and then have \(2 \cdot 1 + 1 = 3\) candies.For \(n=7\), you can use the second spell twice. After the first step, you will have \(3\) candies. And after the second step, you will have \(2 \cdot 3 + 1 = 7\) candies.
Input: 423717 | Output: -1 1 2 2 2 2 4 2 1 1 1
Beginner
3
541
287
655
18
1,295
F
1295F
F. Good Contest
2,700
combinatorics; dp; probabilities
An online contest will soon be held on ForceCoders, a large competitive programming platform. The authors have prepared \(n\) problems; and since the platform is very popular, \(998244351\) coder from all over the world is going to solve them.For each problem, the authors estimated the number of people who would solve it: for the \(i\)-th problem, the number of accepted solutions will be between \(l_i\) and \(r_i\), inclusive.The creator of ForceCoders uses different criteria to determine if the contest is good or bad. One of these criteria is the number of inversions in the problem order. An inversion is a pair of problems \((x, y)\) such that \(x\) is located earlier in the contest (\(x < y\)), but the number of accepted solutions for \(y\) is strictly greater.Obviously, both the creator of ForceCoders and the authors of the contest want the contest to be good. Now they want to calculate the probability that there will be no inversions in the problem order, assuming that for each problem \(i\), any integral number of accepted solutions for it (between \(l_i\) and \(r_i\)) is equally probable, and all these numbers are independent.
The first line contains one integer \(n\) (\(2 \le n \le 50\)) β€” the number of problems in the contest.Then \(n\) lines follow, the \(i\)-th line contains two integers \(l_i\) and \(r_i\) (\(0 \le l_i \le r_i \le 998244351\)) β€” the minimum and maximum number of accepted solutions for the \(i\)-th problem, respectively.
The probability that there will be no inversions in the contest can be expressed as an irreducible fraction \(\frac{x}{y}\), where \(y\) is coprime with \(998244353\). Print one integer β€” the value of \(xy^{-1}\), taken modulo \(998244353\), where \(y^{-1}\) is an integer such that \(yy^{-1} \equiv 1\) \((mod\) \(998244353)\).
The real answer in the first test is \(\frac{1}{2}\).
Input: 3 1 2 1 2 1 2 | Output: 499122177
Master
3
1,150
320
328
12
1,085
B
1085B
B. Div Times Mod
1,100
math
Vasya likes to solve equations. Today he wants to solve \((x~\mathrm{div}~k) \cdot (x \bmod k) = n\), where \(\mathrm{div}\) and \(\mathrm{mod}\) stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, \(k\) and \(n\) are positive integer parameters, and \(x\) is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible \(x\). Can you help him?
The first line contains two integers \(n\) and \(k\) (\(1 \leq n \leq 10^6\), \(2 \leq k \leq 1000\)).
Print a single integer \(x\) β€” the smallest positive integer solution to \((x~\mathrm{div}~k) \cdot (x \bmod k) = n\). It is guaranteed that this equation has at least one positive integer solution.
The result of integer division \(a~\mathrm{div}~b\) is equal to the largest integer \(c\) such that \(b \cdot c \leq a\). \(a\) modulo \(b\) (shortened \(a \bmod b\)) is the only integer \(c\) such that \(0 \leq c < b\), and \(a - c\) is divisible by \(b\).In the first sample, \(11~\mathrm{div}~3 = 3\) and \(11 \bmod 3 = 2\). Since \(3 \cdot 2 = 6\), then \(x = 11\) is a solution to \((x~\mathrm{div}~3) \cdot (x \bmod 3) = 6\). One can see that \(19\) is the only other positive integer solution, hence \(11\) is the smallest one.
Input: 6 3 | Output: 11
Easy
1
450
102
198
10
730
G
730G
G. Car Repair Shop
1,600
implementation
Polycarp starts his own business. Tomorrow will be the first working day of his car repair shop. For now the car repair shop is very small and only one car can be repaired at a given time.Polycarp is good at marketing, so he has already collected n requests from clients. The requests are numbered from 1 to n in order they came.The i-th request is characterized by two values: si β€” the day when a client wants to start the repair of his car, di β€” duration (in days) to repair the car. The days are enumerated from 1, the first day is tomorrow, the second day is the day after tomorrow and so on.Polycarp is making schedule by processing requests in the order from the first to the n-th request. He schedules the i-th request as follows: If the car repair shop is idle for di days starting from si (si, si + 1, ..., si + di - 1), then these days are used to repair a car of the i-th client. Otherwise, Polycarp finds the first day x (from 1 and further) that there are di subsequent days when no repair is scheduled starting from x. In other words he chooses the smallest positive x that all days x, x + 1, ..., x + di - 1 are not scheduled for repair of any car. So, the car of the i-th client will be repaired in the range [x, x + di - 1]. It is possible that the day x when repair is scheduled to start will be less than si. Given n requests, you are asked to help Polycarp schedule all of them according to the rules above.
The first line contains integer n (1 ≀ n ≀ 200) β€” the number of requests from clients.The following n lines contain requests, one request per line. The i-th request is given as the pair of integers si, di (1 ≀ si ≀ 109, 1 ≀ di ≀ 5Β·106), where si is the preferred time to start repairing the i-th car, di is the number of days to repair the i-th car.The requests should be processed in the order they are given in the input.
Print n lines. The i-th line should contain two integers β€” the start day to repair the i-th car and the finish day to repair the i-th car.
Input: 39 27 32 4 | Output: 9 101 34 7
Medium
1
1,427
423
138
7
584
C
584C
C. Marina and Vasya
1,700
constructive algorithms; greedy; strings
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly t characters. Help Vasya find at least one such string.More formally, you are given two strings s1, s2 of length n and number t. Let's denote as f(a, b) the number of characters in which strings a and b are different. Then your task will be to find any string s3 of length n, such that f(s1, s3) = f(s2, s3) = t. If there is no such string, print - 1.
The first line contains two integers n and t (1 ≀ n ≀ 105, 0 ≀ t ≀ n).The second line contains string s1 of length n, consisting of lowercase English letters.The third line contain string s2 of length n, consisting of lowercase English letters.
Print a string of length n, differing from string s1 and from s2 in exactly t characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
Input: 3 2abcxyc | Output: ayd
Medium
3
468
244
193
5
900
E
900E
E. Maximum Questions
2,100
data structures; dp; strings
Vasya wrote down two strings s of length n and t of length m consisting of small English letters 'a' and 'b'. What is more, he knows that string t has a form ""abab..."", namely there are letters 'a' on odd positions and letters 'b' on even positions.Suddenly in the morning, Vasya found that somebody spoiled his string. Some letters of the string s were replaced by character '?'.Let's call a sequence of positions i, i + 1, ..., i + m - 1 as occurrence of string t in s, if 1 ≀ i ≀ n - m + 1 and t1 = si, t2 = si + 1, ..., tm = si + m - 1.The boy defines the beauty of the string s as maximum number of disjoint occurrences of string t in s. Vasya can replace some letters '?' with 'a' or 'b' (letters on different positions can be replaced with different letter). Vasya wants to make some replacements in such a way that beauty of string s is maximum possible. From all such options, he wants to choose one with the minimum number of replacements. Find the number of replacements he should make.
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the length of s.The second line contains the string s of length n. It contains small English letters 'a', 'b' and characters '?' only.The third line contains a single integer m (1 ≀ m ≀ 105) β€” the length of t. The string t contains letters 'a' on odd positions and 'b' on even positions.
Print the only integer β€” the minimum number of replacements Vasya has to perform to make the beauty of string s the maximum possible.
In the first sample string t has a form 'a'. The only optimal option is to replace all characters '?' by 'a'.In the second sample using two replacements we can make string equal to ""aba?aba??"". It is impossible to get more than two occurrences.
Input: 5bb?a?1 | Output: 2
Hard
3
999
346
133
9
1,739
E
1739E
E. Cleaning Robot
2,400
bitmasks; dp
Consider a hallway, which can be represented as the matrix with \(2\) rows and \(n\) columns. Let's denote the cell on the intersection of the \(i\)-th row and the \(j\)-th column as \((i, j)\). The distance between the cells \((i_1, j_1)\) and \((i_2, j_2)\) is \(|i_1 - i_2| + |j_1 - j_2|\).There is a cleaning robot in the cell \((1, 1)\). Some cells of the hallway are clean, other cells are dirty (the cell with the robot is clean). You want to clean the hallway, so you are going to launch the robot to do this.After the robot is launched, it works as follows. While at least one cell is dirty, the robot chooses the closest (to its current cell) cell among those which are dirty, moves there and cleans it (so the cell is no longer dirty). After cleaning a cell, the robot again finds the closest dirty cell to its current cell, and so on. This process repeats until the whole hallway is clean.However, there is a critical bug in the robot's program. If at some moment, there are multiple closest (to the robot's current position) dirty cells, the robot malfunctions.You want to clean the hallway in such a way that the robot doesn't malfunction. Before launching the robot, you can clean some (possibly zero) of the dirty cells yourself. However, you don't want to do too much dirty work yourself while you have this nice, smart (yet buggy) robot to do this. Note that you cannot make a clean cell dirty.Calculate the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of columns in the hallway.Then two lines follow, denoting the \(1\)-st and the \(2\)-nd row of the hallway. These lines contain \(n\) characters each, where 0 denotes a clean cell and 1 denotes a dirty cell. The starting cell of the robot \((1, 1)\) is clean.
Print one integer β€” the maximum possible number of cells you can leave dirty before launching the robot, so that it doesn't malfunction.
In the first example, you can clean the cell \((1, 2)\), so the path of the robot is \((1, 1) \rightarrow (2, 1) \rightarrow (2, 2)\).In the second example, you can leave the hallway as it is, so the path of the robot is \((1, 1) \rightarrow (1, 2) \rightarrow (2, 2)\).In the third example, you can clean the cell \((1, 2)\), so the path of the robot is \((1, 1) \rightarrow (2, 1) \rightarrow (2, 3) \rightarrow (2, 4) \rightarrow (1, 4)\).In the fourth example, the hallway is already clean. Maybe you have launched the robot earlier?
Input: 2 01 11 | Output: 2
Expert
2
1,538
345
136
17
333
E
333E
E. Summer Earnings
2,500
binary search; bitmasks; brute force; geometry; sortings
Many schoolchildren look for a job for the summer, and one day, when Gerald was still a schoolboy, he also decided to work in the summer. But as Gerald was quite an unusual schoolboy, he found quite unusual work. A certain Company agreed to pay him a certain sum of money if he draws them three identical circles on a plane. The circles must not interfere with each other (but they may touch each other). He can choose the centers of the circles only from the n options granted by the Company. He is free to choose the radius of the circles himself (all three radiuses must be equal), but please note that the larger the radius is, the more he gets paid. Help Gerald earn as much as possible.
The first line contains a single integer n β€” the number of centers (3 ≀ n ≀ 3000). The following n lines each contain two integers xi, yi ( - 104 ≀ xi, yi ≀ 104) β€” the coordinates of potential circle centers, provided by the Company.All given points are distinct.
Print a single real number β€” maximum possible radius of circles. The answer will be accepted if its relative or absolute error doesn't exceed 10 - 6.
Input: 30 11 01 1 | Output: 0.50000000000000000000
Expert
5
692
263
149
3
449
A
449A
A. Jzzhu and Chocolate
1,700
greedy; math
Jzzhu has a big rectangular chocolate bar that consists of n Γ— m unit squares. He wants to cut this bar exactly k times. Each cut must meet the following requirements: each cut should be straight (horizontal or vertical); each cut should go along edges of unit squares (it is prohibited to divide any unit chocolate square with cut); each cut should go inside the whole chocolate bar, and all cuts must be distinct. The picture below shows a possible way to cut a 5 Γ— 6 chocolate for 5 times. Imagine Jzzhu have made k cuts and the big chocolate is splitted into several pieces. Consider the smallest (by area) piece of the chocolate, Jzzhu wants this piece to be as large as possible. What is the maximum possible area of smallest piece he can get with exactly k cuts? The area of a chocolate piece is the number of unit squares in it.
A single line contains three integers n, m, k (1 ≀ n, m ≀ 109; 1 ≀ k ≀ 2Β·109).
Output a single integer representing the answer. If it is impossible to cut the big chocolate k times, print -1.
In the first sample, Jzzhu can cut the chocolate following the picture below: In the second sample the optimal division looks like this: In the third sample, it's impossible to cut a 2 Γ— 3 chocolate 4 times.
Input: 3 4 1 | Output: 6
Medium
2
836
78
112
4
1,677
E
1677E
E. Tokitsukaze and Beautiful Subsegments
2,900
data structures
Tokitsukaze has a permutation \(p\) of length \(n\).Let's call a segment \([l,r]\) beautiful if there exist \(i\) and \(j\) satisfying \(p_i \cdot p_j = \max\{p_l, p_{l+1}, \ldots, p_r \}\), where \(l \leq i < j \leq r\).Now Tokitsukaze has \(q\) queries, in the \(i\)-th query she wants to know how many beautiful subsegments \([x,y]\) there are in the segment \([l_i,r_i]\) (i. e. \(l_i \leq x \leq y \leq r_i\)).
The first line contains two integers \(n\) and \(q\) (\(1\leq n \leq 2 \cdot 10^5\); \(1 \leq q \leq 10^6\)) β€” the length of permutation \(p\) and the number of queries.The second line contains \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n\)) β€” the permutation \(p\).Each of the next \(q\) lines contains two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)) β€” the segment \([l_i,r_i]\) of this query.
For each query, print one integer β€” the numbers of beautiful subsegments in the segment \([l_i,r_i]\).
In the first example, for the first query, there are \(2\) beautiful subsegments β€” \([1,2]\) and \([1,3]\).
Input: 8 3 1 3 5 2 4 7 6 8 1 3 1 1 1 8 | Output: 2 0 10
Master
1
415
438
102
16
1,194
C
1194C
C. From S To T
1,300
implementation; strings
You are given three strings \(s\), \(t\) and \(p\) consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings.During each operation you choose any character from \(p\), erase it from \(p\) and insert it into string \(s\) (you may insert this character anywhere you want: in the beginning of \(s\), in the end or between any two consecutive characters). For example, if \(p\) is aba, and \(s\) is de, then the following outcomes are possible (the character we erase from \(p\) and insert into \(s\) is highlighted): aba \(\rightarrow\) ba, de \(\rightarrow\) ade; aba \(\rightarrow\) ba, de \(\rightarrow\) dae; aba \(\rightarrow\) ba, de \(\rightarrow\) dea; aba \(\rightarrow\) aa, de \(\rightarrow\) bde; aba \(\rightarrow\) aa, de \(\rightarrow\) dbe; aba \(\rightarrow\) aa, de \(\rightarrow\) deb; aba \(\rightarrow\) ab, de \(\rightarrow\) ade; aba \(\rightarrow\) ab, de \(\rightarrow\) dae; aba \(\rightarrow\) ab, de \(\rightarrow\) dea; Your goal is to perform several (maybe zero) operations so that \(s\) becomes equal to \(t\). Please determine whether it is possible.Note that you have to answer \(q\) independent queries.
The first line contains one integer \(q\) (\(1 \le q \le 100\)) β€” the number of queries. Each query is represented by three consecutive lines.The first line of each query contains the string \(s\) (\(1 \le |s| \le 100\)) consisting of lowercase Latin letters.The second line of each query contains the string \(t\) (\(1 \le |t| \le 100\)) consisting of lowercase Latin letters.The third line of each query contains the string \(p\) (\(1 \le |p| \le 100\)) consisting of lowercase Latin letters.
For each query print YES if it is possible to make \(s\) equal to \(t\), and NO otherwise.You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer).
In the first test case there is the following sequence of operation: \(s = \) ab, \(t = \) acxb, \(p = \) cax; \(s = \) acb, \(t = \) acxb, \(p = \) ax; \(s = \) acxb, \(t = \) acxb, \(p = \) a. In the second test case there is the following sequence of operation: \(s = \) a, \(t = \) aaaa, \(p = \) aaabbcc; \(s = \) aa, \(t = \) aaaa, \(p = \) aabbcc; \(s = \) aaa, \(t = \) aaaa, \(p = \) abbcc; \(s = \) aaaa, \(t = \) aaaa, \(p = \) bbcc.
Input: 4 ab acxb cax a aaaa aaabbcc a aaaa aabbcc ab baaa aaaaa | Output: YES YES NO NO
Easy
2
1,188
494
233
11
2,122
C
2122C
C. Manhattan Pairs
1,700
constructive algorithms; geometry; greedy; math; sortings
You are given \(n\) points \((x_i, y_i)\) on a 2D plane, where \(n\) is even. Select \(\tfrac{n}{2}\) disjoint pairs \((a_i, b_i)\) to maximize the sum of Manhattan distances between points in pairs. In other words, maximize $$$\(\sum_{i=1}^{n/2} |x_{a_i} - x_{b_i}| + |y_{a_i} - y_{b_i}|.\)$$$
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single even integer \(n\) (\(2 \leq n \leq 2 \cdot 10^5\)) β€” the number of points.The \(i\)-th of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(-10^6 \le x_i, y_i \le 10^6\)) β€” the coordinates of the \(i\)-th point.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output \(\tfrac{n}{2}\) lines, the \(i\)-th line containing two integers \(a_i\) and \(b_i\) β€” the indices of points in the \(i\)-th pair.If there are multiple solutions, print any of them.
In the first test case, an optimal solution is to select the pairs \((1, 4)\) and \((2, 3)\), which achieves a distance sum of \(5 + 3 = 8\).In the second test case, an optimal solution is to select the pairs \((1, 8)\), \((9, 10)\), \((5, 7)\), \((2, 3)\), \((4, 6)\), which achieves a distance sum of \(4 + 7 + 10 + 5 + 7 = 33\).
Input: 241 13 04 23 410-1 -1-1 2-2 -2-2 00 22 -3-4 -4-4 -20 1-4 -2 | Output: 4 1 2 3 8 1 9 10 7 5 2 3 6 4
Medium
5
294
533
209
21
342
D
342D
D. Xenia and Dominoes
2,100
bitmasks; dfs and similar; dp
Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles. A puzzle is a 3 Γ— n table with forbidden cells (black squares) containing dominoes (colored rectangles on the picture). A puzzle is called correct if it meets the following conditions: each domino occupies exactly two non-forbidden cells of the table; no two dominoes occupy the same table cell; exactly one non-forbidden cell of the table is unoccupied by any domino (it is marked by a circle in the picture). To solve the puzzle, you need multiple steps to transport an empty cell from the starting position to some specified position. A move is transporting a domino to the empty cell, provided that the puzzle stays correct. The horizontal dominoes can be moved only horizontally, and vertical dominoes can be moved only vertically. You can't rotate dominoes. The picture shows a probable move.Xenia has a 3 Γ— n table with forbidden cells and a cell marked with a circle. Also, Xenia has very many identical dominoes. Now Xenia is wondering, how many distinct correct puzzles she can make if she puts dominoes on the existing table. Also, Xenia wants the circle-marked cell to be empty in the resulting puzzle. The puzzle must contain at least one move.Help Xenia, count the described number of puzzles. As the described number can be rather large, print the remainder after dividing it by 1000000007 (109 + 7).
The first line contains integer n (3 ≀ n ≀ 104) β€” the puzzle's size. Each of the following three lines contains n characters β€” the description of the table. The j-th character of the i-th line equals ""X"" if the corresponding cell is forbidden; it equals ""."", if the corresponding cell is non-forbidden and ""O"", if the corresponding cell is marked with a circle.It is guaranteed that exactly one cell in the table is marked with a circle. It is guaranteed that all cells of a given table having at least one common point with the marked cell is non-forbidden.
Print a single number β€” the answer to the problem modulo 1000000007 (109 + 7).
Two puzzles are considered distinct if there is a pair of cells that contain one domino in one puzzle and do not contain it in the other one.
Input: 5....X.O......X. | Output: 1
Hard
3
1,467
564
78
3
332
D
332D
D. Theft of Blueprints
2,400
graphs; math
Insurgents accidentally got hold of the plan of a top secret research polygon created on a distant planet for the needs of the Galaxy Empire. The insurgents suppose that this polygon is developing new deadly weapon. The polygon consists of n missile silos connected by bidirectional underground passages. The passages are linked to laboratories where research is conducted. Naturally, the passages are guarded severely: the passage between silos i and j is patrolled by ci, j war droids.The insurgents studied the polygon plan and noticed its unusual structure. As it turned out, for any k-element set of silos S there is exactly one silo that is directly connected by a passage with each silo from S (we'll call this silo adjacent with S). Having considered that, the insurgents decided to act as follows: they choose a k-element set of silos S; a group of scouts lands from the air into each silo from S; each group moves along the corresponding passage to the silo, adjacent with S (as the scouts move, they check out the laboratories and watch for any signs of weapon blueprints); in the silo, adjacent with S, the groups get on the ship and fly away. The danger of the operation is the total number of droids that patrol the passages through which the scouts will go. The danger of the operation obviously only depends on the way to choose set S. The insurgents haven't yet decided on the exact silos to send the scouts to. However, they already want to start preparing the weapons for the scout groups. To do that, the insurgents need to know the mathematical average of the dangers of the operations that correspond to all possible ways to choose set S. Solve this problem to help the insurgents protect the ideals of the Republic!
The first line contains two integers n and k (2 ≀ n ≀ 2000, 1 ≀ k ≀ n - 1) β€” the number of silos and the number of scout groups, correspondingly. The next n - 1 lines describe the polygon plan: the i-th of these lines contains n - i integers ci, i + 1, ci, i + 2, ..., ci, n β€” the number of droids that patrol the corresponding passages (-1 ≀ ci, j ≀ 109; if ci, j = -1, then silos i and j don't have a passage between them). All passages are bidirectional, that is, we can assume that ci, j = cj, i. No passages connect a silo with itself. It is guaranteed that the polygon plan meets the conditions of the problem statement.
Print the average danger of the scouting operation, rounded down to an integer. Note that at the given limits the answer to the problem always fits into the standard integer 64-bit data type.Please do not use the %lld specifier to write 64-bit integers in Π‘++. It is preferred to use the cout stream or the %I64d specifier.
In the first sample there are 6 one-element sets of silos. For sets {1}, {5} the operation danger will equal 8, for sets {3}, {6} β€” 3, for sets {2}, {4} β€” 5. The mathematical average equals .In the second sample there are 3 two-elements sets of silos: {1, 3} (danger equals 21), {1, 2} (danger equals 11), {2, 3} (danger equals 10). The average operation danger equals .
Input: 6 1-1 -1 -1 8 -1-1 5 -1 -1-1 -1 3-1 -1-1 | Output: 5
Expert
2
1,738
626
323
3
1,980
C
1980C
C. Sofia and the Lost Operations
1,300
constructive algorithms; greedy
Sofia had an array of \(n\) integers \(a_1, a_2, \ldots, a_n\). One day she got bored with it, so she decided to sequentially apply \(m\) modification operations to it.Each modification operation is described by a pair of numbers \(\langle c_j, d_j \rangle\) and means that the element of the array with index \(c_j\) should be assigned the value \(d_j\), i.e., perform the assignment \(a_{c_j} = d_j\). After applying all modification operations sequentially, Sofia discarded the resulting array.Recently, you found an array of \(n\) integers \(b_1, b_2, \ldots, b_n\). You are interested in whether this array is Sofia's array. You know the values of the original array, as well as the values \(d_1, d_2, \ldots, d_m\). The values \(c_1, c_2, \ldots, c_m\) turned out to be lost.Is there a sequence \(c_1, c_2, \ldots, c_m\) such that the sequential application of modification operations \(\langle c_1, d_1, \rangle, \langle c_2, d_2, \rangle, \ldots, \langle c_m, d_m \rangle\) to the array \(a_1, a_2, \ldots, a_n\) transforms it into the array \(b_1, b_2, \ldots, b_n\)?
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Then follow the descriptions of the test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the size of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β€” the elements of the original array.The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 10^9\)) β€” the elements of the found array.The fourth line contains an integer \(m\) (\(1 \le m \le 2 \cdot 10^5\)) β€” the number of modification operations.The fifth line contains \(m\) integers \(d_1, d_2, \ldots, d_m\) (\(1 \le d_j \le 10^9\)) β€” the preserved value for each modification operation.It is guaranteed that the sum of the values of \(n\) for all test cases does not exceed \(2 \cdot 10^5\), similarly the sum of the values of \(m\) for all test cases does not exceed \(2 \cdot 10^5\).
Output \(t\) lines, each of which is the answer to the corresponding test case. As an answer, output ""YES"" if there exists a suitable sequence \(c_1, c_2, \ldots, c_m\), and ""NO"" otherwise.You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive answer).
Input: 731 2 11 3 241 3 1 241 2 3 52 1 3 522 357 6 1 10 103 6 1 11 1134 3 1143 1 7 82 2 7 10510 3 2 2 155 7 1 7 94 10 1 2 981 1 9 8 7 2 10 441000000000 203 203 203203 1000000000 203 10000000002203 100000000011151 3 4 5 1 | Output: YES NO NO NO YES NO YES
Easy
2
1,076
996
336
19
1,969
C
1969C
C. Minimizing the Sum
1,700
dp; implementation
You are given an integer array \(a\) of length \(n\).You can perform the following operation: choose an element of the array and replace it with any of its neighbor's value.For example, if \(a=[3, 1, 2]\), you can get one of the arrays \([3, 3, 2]\), \([3, 2, 2]\) and \([1, 1, 2]\) using one operation, but not \([2, 1, 2\)] or \([3, 4, 2]\). Your task is to calculate the minimum possible total sum of the array if you can perform the aforementioned operation at most \(k\) times.
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 3 \cdot 10^5\); \(0 \le k \le 10\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).Additional constraint on the input: the sum of \(n\) over all test cases doesn't exceed \(3 \cdot 10^5\).
For each test case, print a single integer β€” the minimum possible total sum of the array if you can perform the aforementioned operation at most \(k\) times.
In the first example, one of the possible sequences of operations is the following: \([3, 1, 2] \rightarrow [1, 1, 2\)].In the second example, you do not need to apply the operation.In the third example, one of the possible sequences of operations is the following: \([2, 2, 1, 3] \rightarrow [2, 1, 1, 3] \rightarrow [2, 1, 1, 1]\).In the fourth example, one of the possible sequences of operations is the following: \([4, 1, 2, 2, 4, 3] \rightarrow [1, 1, 2, 2, 4, 3] \rightarrow [1, 1, 1, 2, 4, 3] \rightarrow [1, 1, 1, 2, 2, 3]\).
Input: 43 13 1 21 354 22 2 1 36 34 1 2 2 4 3 | Output: 4 5 5 10
Medium
2
482
414
157
19
266
D
266D
D. BerDonalds
2,400
graphs; math; shortest paths
BerDonalds, a well-known fast food restaurant, is going to open a cafe in Bertown. The important thing is to choose the new restaurant's location so that it would be easy to get there. The Bertown road system is represented by n junctions, connected by m bidirectional roads. For each road we know its length. We also know that we can get from any junction to any other one, moving along the roads.Your task is to find such location of the restaurant, that the shortest distance along the roads from the cafe to the farthest junction would be minimum. Note that the restaurant can be located not only on the junction, but at any point of any road.
The first line contains two integers n and m () β€” the number of junctions and the number of roads, correspondingly. Then m lines follow, describing all Bertown roads. Each road is described by three integers ai, bi, wi (1 ≀ ai, bi ≀ n, ai β‰  bi; 1 ≀ wi ≀ 105), where ai and bi are the numbers of the junctions, connected by the i-th road, and wi is the length of the i-th road. It is guaranteed that each road connects two distinct junctions, there is at most one road between any two junctions, and you can get from any junction to any other one.
Print a single real number β€” the shortest distance from the optimal restaurant location to the farthest junction. The answer will be considered correct, if its absolute or relative error doesn't exceed 10 - 9.
Input: 2 11 2 1 | Output: 0.50
Expert
3
647
546
209
2
1,842
C
1842C
C. Tenzing and Balls
1,500
dp
Enjoy erasing Tenzing, identified as Accepted!Tenzing has \(n\) balls arranged in a line. The color of the \(i\)-th ball from the left is \(a_i\).Tenzing can do the following operation any number of times: select \(i\) and \(j\) such that \(1\leq i < j \leq |a|\) and \(a_i=a_j\), remove \(a_i,a_{i+1},\ldots,a_j\) from the array (and decrease the indices of all elements to the right of \(a_j\) by \(j-i+1\)). Tenzing wants to know the maximum number of balls he can remove.
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1\leq t\leq 10^3\)) β€” the number of test cases. The description of test cases follows.The first line contains a single integer \(n\) (\(1\leq n\leq 2\cdot 10^5\)) β€” the number of balls.The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\leq a_i \leq n\)) β€” the color of the balls.It is guaranteed that sum of \(n\) of all test cases will not exceed \(2\cdot 10^5\).
For each test case, output the maximum number of balls Tenzing can remove.
In the first example, Tenzing will choose \(i=2\) and \(j=3\) in the first operation so that \(a=[1,3,3]\). Then Tenzing will choose \(i=2\) and \(j=3\) again in the second operation so that \(a=[1]\). So Tenzing can remove \(4\) balls in total.In the second example, Tenzing will choose \(i=1\) and \(j=3\) in the first and only operation so that \(a=[2]\). So Tenzing can remove \(3\) balls in total.
Input: 251 2 2 3 341 2 1 2 | Output: 4 3
Medium
1
475
480
74
18
1,473
G
1473G
G. Tiles
2,800
combinatorics; dp; fft; math
Consider a road consisting of several rows. Each row is divided into several rectangular tiles, and all tiles in the same row are equal. The first row contains exactly one rectangular tile. Look at the picture below which shows how the tiles are arranged.The road is constructed as follows: the first row consists of \(1\) tile; then \(a_1\) rows follow; each of these rows contains \(1\) tile greater than the previous row; then \(b_1\) rows follow; each of these rows contains \(1\) tile less than the previous row; then \(a_2\) rows follow; each of these rows contains \(1\) tile greater than the previous row; then \(b_2\) rows follow; each of these rows contains \(1\) tile less than the previous row; ... then \(a_n\) rows follow; each of these rows contains \(1\) tile greater than the previous row; then \(b_n\) rows follow; each of these rows contains \(1\) tile less than the previous row. An example of the road with \(n = 2\), \(a_1 = 4\), \(b_1 = 2\), \(a_2 = 2\), \(b_2 = 3\). Rows are arranged from left to right. You start from the only tile in the first row and want to reach the last row (any tile of it). From your current tile, you can move to any tile in the next row which touches your current tile.Calculate the number of different paths from the first row to the last row. Since it can be large, print it modulo \(998244353\).
The first line contains one integer \(n\) (\(1 \le n \le 1000\)).Then \(n\) lines follow. The \(i\)-th of them contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le 10^5\); \(|a_i - b_i| \le 5\)).Additional constraint on the input: the sequence of \(a_i\) and \(b_i\) never results in a row with non-positive number of tiles.
Print one integer β€” the number of paths from the first row to the last row, taken modulo \(998244353\).
Input: 2 4 2 2 3 | Output: 850
Master
4
1,350
335
103
14
178
F1
178F1
F1. Representative Sampling
1,800
The Smart Beaver from ABBYY has a long history of cooperating with the ""Institute of Cytology and Genetics"". Recently, the Institute staff challenged the Beaver with a new problem. The problem is as follows.There is a collection of n proteins (not necessarily distinct). Each protein is a string consisting of lowercase Latin letters. The problem that the scientists offered to the Beaver is to select a subcollection of size k from the initial collection of proteins so that the representativity of the selected subset of proteins is maximum possible.The Smart Beaver from ABBYY did some research and came to the conclusion that the representativity of a collection of proteins can be evaluated by a single number, which is simply calculated. Let's suppose we have a collection {a1, ..., ak} consisting of k strings describing proteins. The representativity of this collection is the following value: where f(x, y) is the length of the longest common prefix of strings x and y; for example, f(""abc"", ""abd"") = 2, and f(""ab"", ""bcd"") = 0.Thus, the representativity of collection of proteins {""abc"", ""abd"", ""abe""} equals 6, and the representativity of collection {""aaa"", ""ba"", ""ba""} equals 2.Having discovered that, the Smart Beaver from ABBYY asked the Cup contestants to write a program that selects, from the given collection of proteins, a subcollection of size k which has the largest possible value of representativity. Help him to solve this problem!
The first input line contains two integers n and k (1 ≀ k ≀ n), separated by a single space. The following n lines contain the descriptions of proteins, one per line. Each protein is a non-empty string of no more than 500 characters consisting of only lowercase Latin letters (a...z). Some of the strings may be equal.The input limitations for getting 20 points are: 1 ≀ n ≀ 20 The input limitations for getting 50 points are: 1 ≀ n ≀ 100 The input limitations for getting 100 points are: 1 ≀ n ≀ 2000
Print a single number denoting the largest possible value of representativity that a subcollection of size k of the given collection of proteins can have.
Input: 3 2ababzdabq | Output: 2
Medium
0
1,476
501
154
1
94
A
94A
A. Restoring Password
900
implementation; strings
Igor K. always used to trust his favorite Kashpirovsky Antivirus. That is why he didn't hesitate to download the link one of his groupmates sent him via QIP Infinium. The link was said to contain ""some real funny stuff about swine influenza"". The antivirus had no objections and Igor K. run the flash application he had downloaded. Immediately his QIP Infinium said: ""invalid login/password"".Igor K. entered the ISQ from his additional account and looked at the info of his main one. His name and surname changed to ""H1N1"" and ""Infected"" correspondingly, and the ""Additional Information"" field contained a strange-looking binary code 80 characters in length, consisting of zeroes and ones. ""I've been hacked"" β€” thought Igor K. and run the Internet Exploiter browser to quickly type his favourite search engine's address.Soon he learned that it really was a virus that changed ISQ users' passwords. Fortunately, he soon found out that the binary code was actually the encrypted password where each group of 10 characters stood for one decimal digit. Accordingly, the original password consisted of 8 decimal digits.Help Igor K. restore his ISQ account by the encrypted password and encryption specification.
The input data contains 11 lines. The first line represents the binary code 80 characters in length. That is the code written in Igor K.'s ISQ account's info. Next 10 lines contain pairwise distinct binary codes 10 characters in length, corresponding to numbers 0, 1, ..., 9.
Print one line containing 8 characters β€” The password to Igor K.'s ISQ account. It is guaranteed that the solution exists.
Input: 010011001001011000000101100010010110010001011001100101101000010110101001011011000100110000010011001001011000000101100010010110010001011001100101101000010110101001011011000101101110 | Output: 12345678
Beginner
2
1,218
275
122
0
95
A
95A
A. Hockey
1,600
implementation; strings
Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1, s2, ..., sn. All those strings consist of uppercase and lowercase Latin letters. String w has the length of |w|, its characters are numbered from 1 to |w|.First Petya should find all the occurrences of forbidden substrings in the w string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings ""aBC"" and ""ABc"" are considered equal.After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position i should be replaced by any other one if for position i in string w there exist pair of indices l, r (1 ≀ l ≀ i ≀ r ≀ |w|) such that substring w[l ... r] is contained in the collection s1, s2, ..., sn, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.Letter letter (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the letter occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them.
The first line contains the only integer n (1 ≀ n ≀ 100) β€” the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter letter.
Output the only line β€” Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically.The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if a is a prefix of b, or there exists such an i (1 ≀ i ≀ |a|), that ai < bi, and for any j (1 ≀ j < i) aj = bj. |a| stands for the length of string a.
Input: 3bersuckyeluPetrLoveLuckyNumberst | Output: PetrLovtTttttNumtttt
Medium
2
1,708
364
487
0
618
A
618A
A. Slime Combining
800
implementation
Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1.You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n - 1 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value v, you combine them together to create a slime with value v + 1.You would like to see what the final state of the row is after you've added all n slimes. Please print the values of the slimes in the row from left to right.
The first line of the input will contain a single integer, n (1 ≀ n ≀ 100 000).
Output a single line with k integers, where k is the number of slimes in the row after you've finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left.
In the first sample, we only have a single slime with value 1. The final state of the board is just a single slime with value 1.In the second sample, we perform the following steps:Initially we place a single slime in a row by itself. Thus, row is initially 1.Then, we will add another slime. The row is now 1 1. Since two rightmost slimes have the same values, we should replace these slimes with one with value 2. Thus, the final state of the board is 2.In the third sample, after adding the first two slimes, our row is 2. After adding one more slime, the row becomes 2 1.In the last sample, the steps look as follows: 1 2 2 1 3 3 1 3 2 3 2 1 4
Input: 1 | Output: 1
Beginner
1
625
79
231
6
1,116
D2
1116D2
D2. Pattern of increasing blocks
0
*special
Implement a unitary operation on \(N\) qubits which is represented by a square matrix of size \(2^N\) defined as follows: top right and bottom left quarters are filled with zero elements, the top left quarter is the same pattern of size \(2^{N-1}\) (for \(N = 1\), the top left quarter is a non-zero element), the bottom right quarter is filled with non-zero elements.For example, for \(N = 3\) the matrix of the operation should have the following shape:X........X........XX......XX........XXXX....XXXX....XXXX....XXXXHere X denotes a ""non-zero"" element of the matrix (a complex number which has the square of the absolute value greater than or equal to \(10^{-5}\)), and . denotes a ""zero"" element of the matrix (a complex number which has the square of the absolute value less than \(10^{-5}\)).The row and column indices of the matrix follow little endian format: the least significant bit of the index is stored first in the qubit array. Thus, the first column of the matrix gives you the coefficients of the basis states you'll get if you apply the unitary to the \(|00..0\rangle\) basis state, the second column - to the \(|10..0\rangle\) basis state etc. You can use the DumpUnitary tool to get the coefficients of the matrix your unitary implements (up to relative phases between columns) and the corresponding pattern of Xs and .s.You have to implement an operation which takes an array of \(N\) (\(2 \le N \le 5\)) qubits as an input and applies the unitary transformation with the matrix of the described shape to it. If there are multiple unitaries which satisfy the requirements, you can implement any of them. The ""output"" of your operation is the pattern of the matrix coefficients implemented by it; you can see the testing harness in the UnitaryPatterns kata.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : Unit { // your code here }}You are not allowed to use measurements in your operation.
Beginner
1
2,032
0
0
11
886
B
886B
B. Vlad and Cafes
1,000
Vlad likes to eat in cafes very much. During his life, he has visited cafes n times. Unfortunately, Vlad started to feel that his last visits are not any different from each other. To fix that Vlad had a small research.First of all, Vlad assigned individual indices to all cafes. Then, he wrote down indices of cafes he visited in a row, in order of visiting them. Now, Vlad wants to find such a cafe that his last visit to that cafe was before his last visits to every other cafe. In other words, he wants to find such a cafe that he hasn't been there for as long as possible. Help Vlad to find that cafe.
In first line there is one integer n (1 ≀ n ≀ 2Β·105) β€” number of cafes indices written by Vlad.In second line, n numbers a1, a2, ..., an (0 ≀ ai ≀ 2Β·105) are written β€” indices of cafes in order of being visited by Vlad. Vlad could visit some cafes more than once. Note that in numeration, some indices could be omitted.
Print one integer β€” index of the cafe that Vlad hasn't visited for as long as possible.
In first test, there are three cafes, and the last visits to cafes with indices 1 and 2 were after the last visit to cafe with index 3; so this cafe is the answer. In second test case, there are also three cafes, but with indices 1, 2 and 4. Cafes with indices 1 and 4 were visited after the last visit of cafe with index 2, so the answer is 2. Note that Vlad could omit some numbers while numerating the cafes.
Input: 51 3 2 1 2 | Output: 3
Beginner
0
606
319
87
8
End of preview. Expand in Data Studio

MathsBench

This repository contains programming evaluation problem sets from Codeforces for Large Language Models (LLMs).

If you find this work relevant or helpful to your work, please kindly cite it:

@misc{codebenchdataset,
  title={CodeBench Dataset}, 
  author={Finbarrs Oketunji},
  year={2025}
}

Copyright

(c) Copyright 2025 Finbarrs Oketunji. All Rights Reserved.

Downloads last month
9