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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
952 |
A
|
952A
|
A. Quirky Quantifiers
| 800 |
math
|
The input contains a single integer a (10 β€ a β€ 999).
|
Output 0 or 1.
|
Input: 13 | Output: 1
|
Beginner
| 1 | 0 | 53 | 14 | 9 |
||
1,902 |
C
|
1902C
|
C. Insert and Equalize
| 1,300 |
brute force; constructive algorithms; greedy; math; number theory
|
You are given an integer array \(a_1, a_2, \dots, a_n\), all its elements are distinct.First, you are asked to insert one more integer \(a_{n+1}\) into this array. \(a_{n+1}\) should not be equal to any of \(a_1, a_2, \dots, a_n\).Then, you will have to make all elements of the array equal. At the start, you choose a positive integer \(x\) (\(x > 0\)). In one operation, you add \(x\) to exactly one element of the array. Note that \(x\) is the same for all operations.What's the smallest number of operations it can take you to make all elements equal, after you choose \(a_{n+1}\) and \(x\)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)). All \(a_i\) are distinct.The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print a single integer β the smallest number of operations it can take you to make all elements equal, after you choose integers \(a_{n+1}\) and \(x\).
|
In the first testcase, you can choose \(a_{n+1} = 4\), the array becomes \([1, 2, 3, 4]\). Then choose \(x = 1\) and apply the operation \(3\) times to the first element, \(2\) times to the second element, \(1\) time to the third element and \(0\) times to the fourth element.In the second testcase, you can choose \(a_{n+1} = 13, x = 4\).In the third testcase, you can choose \(a_{n+1} = 9, x = 1\). Then apply the operation once to \(a_{n+1}\).
|
Input: 331 2 351 -19 17 -3 -15110 | Output: 6 27 1
|
Easy
| 5 | 595 | 379 | 170 | 19 |
720 |
A
|
720A
|
A. Closing ceremony
| 2,000 |
greedy
|
The closing ceremony of Squanch Code Cup is held in the big hall with n Γ m seats, arranged in n rows, m seats in a row. Each seat has two coordinates (x, y) (1 β€ x β€ n, 1 β€ y β€ m). There are two queues of people waiting to enter the hall: k people are standing at (0, 0) and nΒ·m - k people are standing at (0, m + 1). Each person should have a ticket for a specific seat. If person p at (x, y) has ticket for seat (xp, yp) then he should walk |x - xp| + |y - yp| to get to his seat.Each person has a stamina β the maximum distance, that the person agrees to walk. You should find out if this is possible to distribute all nΒ·m tickets in such a way that each person has enough stamina to get to their seat.
|
The first line of input contains two integers n and m (1 β€ nΒ·m β€ 104) β the size of the hall.The second line contains several integers. The first integer k (0 β€ k β€ nΒ·m) β the number of people at (0, 0). The following k integers indicate stamina of each person there.The third line also contains several integers. The first integer l (l = nΒ·m - k) β the number of people at (0, m + 1). The following l integers indicate stamina of each person there.The stamina of the person is a positive integer less that or equal to n + m.
|
If it is possible to distribute tickets between people in the described manner print ""YES"", otherwise print ""NO"".
|
Input: 2 23 3 3 21 3 | Output: YES
|
Hard
| 1 | 706 | 525 | 117 | 7 |
|
793 |
F
|
793F
|
F. Julia the snail
| 3,000 |
data structures; divide and conquer; dp
|
After hard work Igor decided to have some rest.He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium.Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippery. To help the snail Igor put some ropes on the tree, fixing the lower end of the i-th rope on the trunk on the height li above the ground, and the higher end on the height ri above the ground.For some reason no two ropes share the same position of the higher end, i.e. all ri are distinct. Now Julia can move down at any place of the trunk, and also move up from the lower end of some rope to its higher end. Igor is proud of his work, and sometimes think about possible movements of the snail. Namely, he is interested in the following questions: Β«Suppose the snail is on the trunk at height x now. What is the highest position on the trunk the snail can get on if it would never be lower than x or higher than y?Β» Please note that Julia can't move from a rope to the trunk before it reaches the higher end of the rope, and Igor is interested in the highest position on the tree trunk.Igor is interested in many questions, and not always can answer them. Help him, write a program that answers these questions.
|
The first line contains single integer n (1 β€ n β€ 100000) β the height of the trunk.The second line contains single integer m (1 β€ m β€ 100000) β the number of ropes.The next m lines contain information about the ropes.The i-th of these lines contains two integers li and ri (1 β€ li β€ ri β€ n) β the heights on which the lower and the higher ends of the i-th rope are fixed, respectively. It is guaranteed that all ri are distinct.The next line contains single integer q (1 β€ q β€ 100000) β the number of questions.The next q lines contain information about the questions.Each of these lines contain two integers x and y (1 β€ x β€ y β€ n), where x is the height where Julia starts (and the height Julia can't get lower than), and y is the height Julia can't get higher than.
|
For each question print the maximum reachable for Julia height.
|
The picture of the first sample is on the left, the picture of the second sample is on the right. Ropes' colors are just for clarity, they don't mean anything.
|
Input: 841 23 42 56 751 21 41 62 76 8 | Output: 22557
|
Master
| 3 | 1,310 | 769 | 63 | 7 |
1,638 |
E
|
1638E
|
E. Colorful Operations
| 2,400 |
brute force; data structures; implementation
|
You have an array \(a_1,a_2, \dots, a_n\). Each element initially has value \(0\) and color \(1\). You are also given \(q\) queries to perform: Color \(l\) \(r\) \(c\): Change the color of elements \(a_l,a_{l+1},\cdots,a_r\) to \(c\) (\(1 \le l \le r \le n\), \(1 \le c \le n\)). Add \(c\) \(x\): Add \(x\) to values of all elements \(a_i\) (\(1 \le i \le n\)) of color \(c\) (\(1 \le c \le n\), \(-10^9 \le x \le 10^9\)). Query \(i\): Print \(a_i\) (\(1 \le i \le n\)).
|
The first line of input contains two integers \(n\) and \(q\) (\(1 \le n,q \le 10^6\)) β the length of array \(a\) and the number of queries you have to perform.Each of the next \(q\) lines contains the query given in the form described in the problem statement.
|
Print the answers to the queries of the third type on separate lines.
|
The first sample test is explained below. Blue, red and green represent colors \(1\), \(2\) and \(3\) respectively.
|
Input: 5 8 Color 2 4 2 Add 2 2 Query 3 Color 4 5 3 Color 2 2 3 Add 3 3 Query 2 Query 5 | Output: 2 5 3
|
Expert
| 3 | 470 | 262 | 69 | 16 |
2,044 |
A
|
2044A
|
A. Easy Problem
| 800 |
brute force; math
|
Cube is given an integer \(n\). She wants to know how many ordered pairs of positive integers \((a,b)\) there are such that \(a=n-b\). Since Cube is not very good at math, please help her!
|
The first line contains an integer \(t\) (\(1 \leq t \leq 99\)) β the number of test cases.The only line of each test case contains an integer \(n\) (\(2 \leq n \leq 100\)).
|
For each test case, output the number of ordered pairs \((a, b)\) on a new line.
|
In the first test case, the only ordered pair that works is \((a,b)=(1,1)\). In the second test case, the three ordered pairs of \((a,b)\) that work are \((3,1), (2,2), (1,3)\).
|
Input: 3246 | Output: 1 3 5
|
Beginner
| 2 | 188 | 173 | 80 | 20 |
855 |
A
|
855A
|
A. Tom Riddle's Diary
| 800 |
brute force; implementation; strings
|
Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence.He has names of n people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not.Formally, for a name si in the i-th line, output ""YES"" (without quotes) if there exists an index j such that si = sj and j < i, otherwise, output ""NO"" (without quotes).
|
First line of input contains an integer n (1 β€ n β€ 100) β the number of names in the list.Next n lines each contain a string si, consisting of lowercase English letters. The length of each string is between 1 and 100.
|
Output n lines each containing either ""YES"" or ""NO"" (without quotes), depending on whether this string was already present in the stream or not.You can print each letter in any case (upper or lower).
|
In test case 1, for i = 5 there exists j = 3 such that si = sj and j < i, which means that answer for i = 5 is ""YES"".
|
Input: 6tomluciusginnyharryginnyharry | Output: NONONONOYESYES
|
Beginner
| 3 | 672 | 217 | 203 | 8 |
1,578 |
D
|
1578D
|
D. Dragon Curve
| 3,200 |
A dragon curve is a self-similar fractal curve. In this problem, it is a curve that consists of straight-line segments of the same length connected at right angles. A simple way to construct a dragon curve is as follows: take a strip of paper, fold it in half \(n\) times in the same direction, then partially unfold it such that the segments are joined at right angles. This is illustrated here: In this example, a dragon curve of order \(3\) is constructed. In general, a dragon curve of a higher order will have a dragon curve of a lower order as its prefix. This allows us to define a dragon curve of infinite order, which is the limit of dragon curves of a finite order as the order approaches infinity.Consider four dragon curves of infinite order. Each starts at the origin (the point \((0,0)\)), and the length of each segment is \(\sqrt2\). The first segments of the curves end at the points \((1,1)\), \((-1,1)\), \((-1,-1)\) and \((1,-1)\), respectively. The first turn of each curve is left (that is, the second segment of the first curve ends at the point \((0,2)\)). In this case, every segment is a diagonal of an axis-aligned unit square with integer coordinates, and it can be proven that there is exactly one segment passing through every such square.Given a point \((x,y)\), your task is to find on which of the four curves lies the segment passing through the square with the opposite corners at \((x,y)\) and \((x+1,y+1)\), as well as the position of that segment on that curve. The curves are numbered \(1\) through \(4\). Curve \(1\) goes through \((1,1)\), \(2\) through \((-1,1)\), \(3\) through \((-1,-1)\), and \(4\) through \((1,-1)\). The segments are numbered starting with \(1\).
|
The first line contains an integer \(n\) (\(1\le n\le2\cdot10^5\)) β the number of test cases.Each of the following \(n\) lines contains two integers \(x\) and \(y\) (\(-10^9\le x,y\le10^9\)) β the coordinates.
|
For each test case, print a line containing two integers β the first is the index of the curve (an integer between \(1\) and \(4\), inclusive), and the second is the position on the curve (the first segment has the position \(1\)).
|
You can use this illustration to debug your solution:
|
Input: 5 0 0 -2 0 -7 -7 5 -9 9 9 | Output: 1 1 2 2 3 189 4 186 2 68
|
Master
| 0 | 1,710 | 210 | 231 | 15 |
|
1,282 |
B1
|
1282B1
|
B1. K for the Price of One (Easy Version)
| 1,400 |
dp; greedy; sortings
|
This is the easy version of this problem. The only difference is the constraint on \(k\) β the number of gifts in the offer. In this version: \(k=2\).Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky β today the offer ""\(k\) of goods for the price of one"" is held in store. Remember, that in this problem \(k=2\).Using this offer, Vasya can buy exactly \(k\) of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.More formally, for each good, its price is determined by \(a_i\) β the number of coins it costs. Initially, Vasya has \(p\) coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: Vasya can buy one good with the index \(i\) if he currently has enough coins (i.e \(p \ge a_i\)). After buying this good, the number of Vasya's coins will decrease by \(a_i\), (i.e it becomes \(p := p - a_i\)). Vasya can buy a good with the index \(i\), and also choose exactly \(k-1\) goods, the price of which does not exceed \(a_i\), if he currently has enough coins (i.e \(p \ge a_i\)). Thus, he buys all these \(k\) goods, and his number of coins decreases by \(a_i\) (i.e it becomes \(p := p - a_i\)). Please note that each good can be bought no more than once.For example, if the store now has \(n=5\) goods worth \(a_1=2, a_2=4, a_3=3, a_4=5, a_5=7\), respectively, \(k=2\), and Vasya has \(6\) coins, then he can buy \(3\) goods. A good with the index \(1\) will be bought by Vasya without using the offer and he will pay \(2\) coins. Goods with the indices \(2\) and \(3\) Vasya will buy using the offer and he will pay \(4\) coins. It can be proved that Vasya can not buy more goods with six coins.Help Vasya to find out the maximum number of goods he can buy.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The next lines contain a description of \(t\) test cases. The first line of each test case contains three integers \(n, p, k\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le p \le 2\cdot10^9\), \(k=2\)) β the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.The second line of each test case contains \(n\) integers \(a_i\) (\(1 \le a_i \le 10^4\)) β the prices of goods.It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\). It is guaranteed that in this version of the problem \(k=2\) for all test cases.
|
For each test case in a separate line print one integer \(m\) β the maximum number of goods that Vasya can buy.
|
Input: 6 5 6 2 2 4 3 5 7 5 11 2 2 4 3 5 7 2 10000 2 10000 10000 2 9999 2 10000 10000 5 13 2 8 2 8 2 5 3 18 2 1 2 3 | Output: 3 4 2 0 4 3
|
Easy
| 3 | 1,913 | 736 | 111 | 12 |
|
1,704 |
F
|
1704F
|
F. Colouring Game
| 2,600 |
constructive algorithms; dp; games
|
Alice and Bob are playing a game. There are \(n\) cells in a row. Initially each cell is either red or blue. Alice goes first.On each turn, Alice chooses two neighbouring cells which contain at least one red cell, and paints that two cells white. Then, Bob chooses two neighbouring cells which contain at least one blue cell, and paints that two cells white. The player who cannot make a move loses.Find the winner if both Alice and Bob play optimally.Note that a chosen cell can be white, as long as the other cell satisfies the constraints.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Description of test cases follows.For each test case, the first line contains an integer \(n\) (\(2 \leq n \leq 5 \cdot 10^5\)) β the number of cells.The second line contains a string \(s\) of length \(n\) β the initial state of the cells. The \(i\)-th cell is red if \(s_i = \) R, blue if \(s_i = \) B.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output the name of the winner on a separate line.
|
In the notes, the cell numbers increase from left to right.In the first testcase for Alice, she has two choices: paint the first and the second cells, or paint the second and the third cells. No matter what choice Alice makes, there will be exactly one blue cell after Alice's move. Bob just needs to paint the blue cell and its neighbour, then every cell will be white and Alice can't make a move. So Bob is the winner.In the second testcase no matter what Alice chooses, Bob can choose to paint the fourth and fifth cells in \(2\) turns.In the third testcase at first, Alice paints the third and the fourth cells. It doesn't matter if Bob paints the first and the second cells or the fifth and sixth cells, as Alice can paint the other two cells.In the fourth testcase at first, Alice paints the second and the third cells. If Bob paints the fifth and the sixth cells or the fourth and the fifth cells, then Alice paints the seventh and the eighth cells. If Bob paints the seventh and the eighth cells, then Alice paints the fifth and the sixth cells.In the fifth Alice chooses the middle two cells at first, then Bob obviously has only two options, whichever variant he chooses, Alice can choose the other one and win.In the eighth no matter what Alice chooses, Bob can choose the other two symmetrical cells.
|
Input: 83BRB5RRBBB6RBRBRB8BBRRBRRB6BRRBRB12RBRBRBRBRRBB12RBRBRBRBBBRR4RBBR | Output: Bob Bob Alice Alice Alice Alice Bob Bob
|
Expert
| 3 | 542 | 495 | 69 | 17 |
287 |
B
|
287B
|
B. Pipeline
| 1,700 |
binary search; math
|
Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe. The figure shows a 4-output splitter Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.
|
The first line contains two space-separated integers n and k (1 β€ n β€ 1018, 2 β€ k β€ 109).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.
|
Print a single integer β the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.
|
Input: 4 3 | Output: 2
|
Medium
| 2 | 1,148 | 236 | 162 | 2 |
|
656 |
C
|
656C
|
C. Without Text
| 2,000 |
*special
|
You can preview the image in better quality by the link: http://assets.codeforces.com/files/656/without-text.png
|
The only line of the input is a string (between 1 and 50 characters long, inclusive). Each character will be an alphanumeric character or a full stop ""."".
|
Output the required answer.
|
Input: Codeforces | Output: -87
|
Hard
| 1 | 112 | 156 | 27 | 6 |
|
262 |
B
|
262B
|
B. Roma and Changing Signs
| 1,200 |
greedy
|
Roma works in a company that sells TVs. Now he has to prepare a report for the last year.Roma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.The operation of changing a number's sign is the operation of multiplying this number by -1.Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly k changes.
|
The first line contains two integers n and k (1 β€ n, k β€ 105), showing, how many numbers are in the sequence and how many swaps are to be made.The second line contains a non-decreasing sequence, consisting of n integers ai (|ai| β€ 104).The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.
|
In the single line print the answer to the problem β the maximum total income that we can obtain after exactly k changes.
|
In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1.
|
Input: 3 2-1 -1 1 | Output: 3
|
Easy
| 1 | 681 | 363 | 121 | 2 |
1,830 |
B
|
1830B
|
B. The BOSS Can Count Pairs
| 2,000 |
brute force; math
|
You are given two arrays \(a\) and \(b\), both of length \(n\).Your task is to count the number of pairs of integers \((i,j)\) such that \(1 \leq i < j \leq n\) and \(a_i \cdot a_j = b_i+b_j\).
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the arrays.The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le n\)) β the elements of array \(a\).The third line of each test case contains \(n\) integers \(b_1,b_2,\ldots,b_n\) (\(1 \le b_i \le n\)) β the elements of array \(b\).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the number of good pairs.
|
In the first sample, there are \(2\) good pairs: \((1,2)\), \((1,3)\). In the second sample, there are \(7\) good pairs: \((1,2)\), \((1,5)\), \((2,8)\), \((3,4)\), \((4,7)\), \((5,6)\), \((5,7)\).
|
Input: 332 3 23 3 184 2 8 2 1 2 7 53 5 8 8 1 1 6 584 4 8 8 8 8 8 88 8 8 8 8 8 8 8 | Output: 2 7 1
|
Hard
| 2 | 193 | 667 | 52 | 18 |
939 |
B
|
939B
|
B. Hamster Farm
| 1,000 |
implementation
|
Dima has a hamsters farm. Soon N hamsters will grow up on it and Dima will sell them in a city nearby.Hamsters should be transported in boxes. If some box is not completely full, the hamsters in it are bored, that's why each box should be completely full with hamsters.Dima can buy boxes at a factory. The factory produces boxes of K kinds, boxes of the i-th kind can contain in themselves ai hamsters. Dima can buy any amount of boxes, but he should buy boxes of only one kind to get a wholesale discount.Of course, Dima would buy boxes in such a way that each box can be completely filled with hamsters and transported to the city. If there is no place for some hamsters, Dima will leave them on the farm.Find out how many boxes and of which type should Dima buy to transport maximum number of hamsters.
|
The first line contains two integers N and K (0 β€ N β€ 1018, 1 β€ K β€ 105) β the number of hamsters that will grow up on Dima's farm and the number of types of boxes that the factory produces.The second line contains K integers a1, a2, ..., aK (1 β€ ai β€ 1018 for all i) β the capacities of boxes.
|
Output two integers: the type of boxes that Dima should buy and the number of boxes of that type Dima should buy. Types of boxes are numbered from 1 to K in the order they are given in input.If there are many correct answers, output any of them.
|
Input: 19 35 4 10 | Output: 2 4
|
Beginner
| 1 | 805 | 294 | 245 | 9 |
|
959 |
D
|
959D
|
D. Mahmoud and Ehab and another array construction task
| 1,900 |
constructive algorithms; greedy; math; number theory
|
Mahmoud has an array a consisting of n integers. He asked Ehab to find another array b of the same length such that: b is lexicographically greater than or equal to a. bi β₯ 2. b is pairwise coprime: for every 1 β€ i < j β€ n, bi and bj are coprime, i. e. GCD(bi, bj) = 1, where GCD(w, z) is the greatest common divisor of w and z. Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it?An array x is lexicographically greater than an array y if there exists an index i such than xi > yi and xj = yj for all 1 β€ j < i. An array x is equal to an array y if xi = yi for all 1 β€ i β€ n.
|
The first line contains an integer n (1 β€ n β€ 105), the number of elements in a and b.The second line contains n integers a1, a2, ..., an (2 β€ ai β€ 105), the elements of a.
|
Output n space-separated integers, the i-th of them representing bi.
|
Note that in the second sample, the array is already pairwise coprime so we printed it.
|
Input: 52 3 5 4 13 | Output: 2 3 5 7 11
|
Hard
| 4 | 651 | 172 | 68 | 9 |
495 |
A
|
495A
|
A. Digital Counter
| 1,100 |
implementation
|
Malek lives in an apartment block with 100 floors numbered from 0 to 99. The apartment has an elevator with a digital counter showing the floor that the elevator is currently on. The elevator shows each digit of a number with 7 light sticks by turning them on or off. The picture below shows how the elevator shows each digit.One day when Malek wanted to go from floor 88 to floor 0 using the elevator he noticed that the counter shows number 89 instead of 88. Then when the elevator started moving the number on the counter changed to 87. After a little thinking Malek came to the conclusion that there is only one explanation for this: One of the sticks of the counter was broken. Later that day Malek was thinking about the broken stick and suddenly he came up with the following problem.Suppose the digital counter is showing number n. Malek calls an integer x (0 β€ x β€ 99) good if it's possible that the digital counter was supposed to show x but because of some(possibly none) broken sticks it's showing n instead. Malek wants to know number of good integers for a specific n. So you must write a program that calculates this number. Please note that the counter always shows two digits.
|
The only line of input contains exactly two digits representing number n (0 β€ n β€ 99). Note that n may have a leading zero.
|
In the only line of the output print the number of good integers.
|
In the first sample the counter may be supposed to show 88 or 89.In the second sample the good integers are 00, 08, 80 and 88.In the third sample the good integers are 03, 08, 09, 33, 38, 39, 73, 78, 79, 83, 88, 89, 93, 98, 99.
|
Input: 89 | Output: 2
|
Easy
| 1 | 1,193 | 123 | 65 | 4 |
151 |
B
|
151B
|
B. Phone Numbers
| 1,200 |
implementation; strings
|
Winters are just damn freezing cold in Nvodsk! That's why a group of n friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size si (that's the number of phone numbers). We know that taxi numbers consist of six identical digits (for example, 22-22-22), the numbers of pizza deliveries should necessarily be decreasing sequences of six different digits (for example, 98-73-21), all other numbers are the girls' numbers.You are given your friends' phone books. Calculate which friend is best to go to when you are interested in each of those things (who has maximal number of phone numbers of each type). If the phone book of one person contains some number two times, you should count it twice. That is, each number should be taken into consideration the number of times it occurs in the phone book.
|
The first line contains an integer n (1 β€ n β€ 100) β the number of friends. Then follow n data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer si and string namei (0 β€ si β€ 100) β the number of phone numbers in the phone book of the i-th friend and the name of the i-th friend. The name is a non-empty sequence of uppercase and lowercase Latin letters, containing no more than 20 characters. Next si lines contain numbers as ""XX-XX-XX"", where X is arbitrary digits from 0 to 9.
|
In the first line print the phrase ""If you want to call a taxi, you should call: "". Then print names of all friends whose phone books contain maximal number of taxi phone numbers. In the second line print the phrase ""If you want to order a pizza, you should call: "". Then print names of all friends who have maximal number of pizza phone numbers. In the third line print the phrase ""If you want to go to a cafe with a wonderful girl, you should call: "". Then print names of all friends who have maximal number of girls' phone numbers. Print the names in the order in which they are given in the input data. Separate two consecutive names with a comma and a space. Each line should end with exactly one point. For clarifications concerning the output form, see sample tests. It is necessary that you follow the output form strictly. Extra spaces are not allowed.
|
In the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number.Thus, if you need to order a taxi, you should obviously call Rogulenko, if you need to order a pizza you should call anybody of the following: Rogulenko, Fedorov, Kaluzhin (each of them has one number). Melnikov has maximal number of phone numbers of girls.
|
Input: 42 Fedorov22-22-2298-76-543 Melnikov75-19-0923-45-6799-99-987 Rogulenko22-22-2211-11-1133-33-3344-44-4455-55-5566-66-6695-43-213 Kaluzhin11-11-1199-99-9998-65-32 | Output: If you want to call a taxi, you should call: Rogulenko.If you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin.If you want to go to a cafe with a wonderful girl, you should call: Melnikov.
|
Easy
| 2 | 926 | 568 | 867 | 1 |
1,218 |
H
|
1218H
|
H. Function Composition
| 2,900 |
dfs and similar
|
We are definitely not going to bother you with another generic story when Alice finds about an array or when Alice and Bob play some stupid game. This time you'll get a simple, plain text.First, let us define several things. We define function \(F\) on the array \(A\) such that \(F(i, 1) = A[i]\) and \(F(i, m) = A[F(i, m - 1)]\) for \(m > 1\). In other words, value \(F(i, m)\) represents composition \(A[...A[i]]\) applied \(m\) times.You are given an array of length \(N\) with non-negative integers. You are expected to give an answer on \(Q\) queries. Each query consists of two numbers β \(m\) and \(y\). For each query determine how many \(x\) exist such that \(F(x,m) = y\).
|
The first line contains one integer \(N\) \((1 \leq N \leq 2 \cdot 10^5)\) β the size of the array \(A\). The next line contains \(N\) non-negative integers β the array \(A\) itself \((1 \leq A_i \leq N)\). The next line contains one integer \(Q\) \((1 \leq Q \leq 10^5)\) β the number of queries. Each of the next \(Q\) lines contain two integers \(m\) and \(y\) \((1 \leq m \leq 10^{18}, 1\leq y \leq N)\).
|
Output exactly \(Q\) lines with a single integer in each that represent the solution. Output the solutions in the order the queries were asked in.
|
For the first query we can notice that \(F(3, 10) = 1,\ F(9, 10) = 1\) and \(F(10, 10) = 1\).For the second query no \(x\) satisfies condition \(F(x, 5) = 7\).For the third query \(F(5, 10) = 6\) holds.For the fourth query \(F(3, 1) = 1.\)For the fifth query no \(x\) satisfies condition \(F(x, 10) = 8\).
|
Input: 10 2 3 1 5 6 4 2 10 7 7 5 10 1 5 7 10 6 1 1 10 8 | Output: 3 0 1 1 0
|
Master
| 1 | 683 | 408 | 146 | 12 |
1,896 |
F
|
1896F
|
F. Bracket Xoring
| 2,600 |
constructive algorithms; greedy; implementation; math
|
You are given a binary string \(s\) of length \(2n\) where each element is \(\mathtt{0}\) or \(\mathtt{1}\). You can do the following operation: Choose a balanced bracket sequence\(^\dagger\) \(b\) of length \(2n\). For every index \(i\) from \(1\) to \(2n\) in order, where \(b_i\) is an open bracket, let \(p_i\) denote the minimum index such that \(b[i,p_i]\) is a balanced bracket sequence. Then, we perform a range toggle operation\(^\ddagger\) from \(i\) to \(p_i\) on \(s\). Note that since a balanced bracket sequence of length \(2n\) will have \(n\) open brackets, we will do \(n\) range toggle operations on \(s\). Your task is to find a sequence of no more than \(10\) operations that changes all elements of \(s\) to \(\mathtt{0}\), or determine that it is impossible to do so. Note that you do not have to minimize the number of operations.Under the given constraints, it can be proven that if it is possible to change all elements of \(s\) to \(\mathtt{0}\), there exists a way that requires no more than \(10\) operations.\(^\dagger\) A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters \(+\) and \(1\). For example, sequences ""(())()"", ""()"", and ""(()(()))"" are balanced, while "")("", ""(()"", and ""(()))("" are not.\(^\ddagger\) If we perform a range toggle operation from \(l\) to \(r\) on a binary string \(s\), then we toggle all values of \(s_i\) such that \(l \leq i \leq r\). If \(s_i\) is toggled, we will set \(s_i := \mathtt{0}\) if \(s_i = \mathtt{1}\) or vice versa. For example, if \(s=\mathtt{1000101}\) and we perform a range toggle operation from \(3\) to \(5\), \(s\) will be changed to \(s=\mathtt{1011001}\).
|
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\) (\(1 \le n \le 2\cdot 10^5\)) β where \(2n\) is the length of string \(s\).The second line of each test case contains a binary string \(s\) of length \(2n\) (\(s_i = \mathtt{0}\) or \(s_i = \mathtt{1}\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output \(-1\) in a single line if it is impossible to change all elements of \(s\) to \(\mathtt{0}\).Otherwise, output a single integer \(k\) (\(0 \le k \le 10\)) representing the number of operations needed to change all elements of \(s\) to \(\mathtt{0}\). Then, on each of the next \(k\) lines, output a balanced bracket sequence of length \(2n\) representing the operations needed to change all elements of \(s\) to \(0\)s.If there are multiple ways to change all elements of \(s\) to \(\mathtt{0}\) that require not more than \(10\) operations, you can output any of them.
|
In the first test case, it can be proven that it is impossible to change all elements of \(s\) to \(\mathtt{0}\).In the second test case, the first operation using the bracket sequence \(b = \mathtt{()()}\) will convert the binary string \(s=\mathtt{0000}\) to \(s=\mathtt{1111}\). Then, the second operation using the same bracket sequence \(b = \mathtt{()()}\) will convert the binary string \(s=\mathtt{1111}\) back to \(s=\mathtt{0000}\). Note that since all elements of \(s\) is already \(\mathtt{0}\) initially, using \(0\) operations is also a valid answer.In the third test case, a single operation using the bracket sequence \(b = \mathtt{(())()}\) will change all elements of \(s\) to \(\mathtt{0}\). The operation will happen as follows. \(b_1\) is an open bracket and \(p_1 = 4\) since \(b[1,4]=\mathtt{(())}\) is a balanced bracket sequence. Hence, we do a range toggle operation from \(1\) to \(4\) on the binary string \(s = \mathtt{100111}\) to obtain \(s = \mathtt{011011}\). \(b_2\) is an open bracket and \(p_2 = 3\) since \(b[2,3]=\mathtt{()}\) is a balanced bracket sequence. Hence, we do a range toggle operation from \(2\) to \(3\) on the binary string \(s = \mathtt{011011}\) to obtain \(s = \mathtt{000011}\). \(b_3\) is not an open bracket, so no range toggle operation is done at this step. \(b_4\) is not an open bracket, so no range toggle operation is done at this step. \(b_5\) is an open bracket and \(p_5 = 6\) since \(b[5,6]=\mathtt{()}\) is a balanced bracket sequence. Hence, we do a range toggle operation from \(5\) to \(6\) on the binary string \(s = \mathtt{000011}\) to obtain \(s = \mathtt{000000}\). \(b_6\) is not an open bracket, so no range toggle operation is done at this step. In the fourth test case, the first operation using the bracket sequence \(b = \mathtt{(((())))}\) will convert the binary string \(s = \mathtt{01011100}\) to \(s = \mathtt{11111001}\). Then, the second operation using the bracket sequence \(b = \mathtt{()()(())}\) will convert the binary string \(s = \mathtt{11111001}\) to \(s=\mathtt{00000000}\).
|
Input: 4101200003100111401011100 | Output: -1 2 ()() ()() 1 (())() 2 (((()))) ()()(())
|
Expert
| 4 | 1,709 | 521 | 597 | 18 |
45 |
D
|
45D
|
D. Event Dates
| 1,900 |
greedy; meet-in-the-middle; sortings
|
On a history lesson the teacher asked Vasya to name the dates when n famous events took place. He doesn't remembers the exact dates but he remembers a segment of days [li, ri] (inclusive) on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such n dates of famous events that will fulfill both conditions. It is guaranteed that it is possible.
|
The first line contains one integer n (1 β€ n β€ 100) β the number of known events. Then follow n lines containing two integers li and ri each (1 β€ li β€ ri β€ 107) β the earliest acceptable date and the latest acceptable date of the i-th event.
|
Print n numbers β the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists.
|
Input: 31 22 33 4 | Output: 1 2 3
|
Hard
| 3 | 423 | 241 | 151 | 0 |
|
1,076 |
D
|
1076D
|
D. Edge Deletion
| 1,800 |
graphs; greedy; shortest paths
|
You are given an undirected connected weighted graph consisting of \(n\) vertices and \(m\) edges. Let's denote the length of the shortest path from vertex \(1\) to vertex \(i\) as \(d_i\). You have to erase some edges of the graph so that at most \(k\) edges remain. Let's call a vertex \(i\) good if there still exists a path from \(1\) to \(i\) with length \(d_i\) after erasing the edges.Your goal is to erase the edges in such a way that the number of good vertices is maximized.
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 3 \cdot 10^5\), \(1 \le m \le 3 \cdot 10^5\), \(n - 1 \le m\), \(0 \le k \le m\)) β the number of vertices and edges in the graph, and the maximum number of edges that can be retained in the graph, respectively.Then \(m\) lines follow, each containing three integers \(x\), \(y\), \(w\) (\(1 \le x, y \le n\), \(x \ne y\), \(1 \le w \le 10^9\)), denoting an edge connecting vertices \(x\) and \(y\) and having weight \(w\).The given graph is connected (any vertex can be reached from any other vertex) and simple (there are no self-loops, and for each unordered pair of vertices there exists at most one edge connecting these vertices).
|
In the first line print \(e\) β the number of edges that should remain in the graph (\(0 \le e \le k\)).In the second line print \(e\) distinct integers from \(1\) to \(m\) β the indices of edges that should remain in the graph. Edges are numbered in the same order they are given in the input. The number of good vertices should be as large as possible.
|
Input: 3 3 2 1 2 1 3 2 1 1 3 3 | Output: 2 1 2
|
Medium
| 3 | 484 | 713 | 354 | 10 |
|
1,153 |
B
|
1153B
|
B. Serval and Toy Bricks
| 1,200 |
constructive algorithms; greedy
|
Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny.He has a special interest to create difficult problems for others to solve. This time, with many \(1 \times 1 \times 1\) toy bricks, he builds up a 3-dimensional object. We can describe this object with a \(n \times m\) matrix, such that in each cell \((i,j)\), there are \(h_{i,j}\) bricks standing on the top of each other.However, Serval doesn't give you any \(h_{i,j}\), and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are \(m\) columns, and in the \(i\)-th of them, the height is the maximum of \(h_{1,i},h_{2,i},\dots,h_{n,i}\). It is similar for the left view, where there are \(n\) columns. And in the top view, there is an \(n \times m\) matrix \(t_{i,j}\), where \(t_{i,j}\) is \(0\) or \(1\). If \(t_{i,j}\) equals \(1\), that means \(h_{i,j}>0\), otherwise, \(h_{i,j}=0\).However, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try?
|
The first line contains three positive space-separated integers \(n, m, h\) (\(1\leq n, m, h \leq 100\)) β the length, width and height.The second line contains \(m\) non-negative space-separated integers \(a_1,a_2,\dots,a_m\), where \(a_i\) is the height in the \(i\)-th column from left to right of the front view (\(0\leq a_i \leq h\)).The third line contains \(n\) non-negative space-separated integers \(b_1,b_2,\dots,b_n\) (\(0\leq b_j \leq h\)), where \(b_j\) is the height in the \(j\)-th column from left to right of the left view.Each of the following \(n\) lines contains \(m\) numbers, each is \(0\) or \(1\), representing the top view, where \(j\)-th number of \(i\)-th row is \(1\) if \(h_{i, j}>0\), and \(0\) otherwise.It is guaranteed that there is at least one structure satisfying the input.
|
Output \(n\) lines, each of them contains \(m\) integers, the \(j\)-th number in the \(i\)-th line should be equal to the height in the corresponding position of the top view. If there are several objects satisfying the views, output any one of them.
|
The graph above illustrates the object in the first example. The first graph illustrates the object in the example output for the second example, and the second graph shows the three-view drawing of it.
|
Input: 3 7 3 2 3 0 0 2 0 1 2 1 3 1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 | Output: 1 0 0 0 2 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0 0
|
Easy
| 2 | 1,298 | 810 | 250 | 11 |
1,667 |
F
|
1667F
|
F. Yin Yang
| 3,500 |
implementation
|
You are given a rectangular grid with \(n\) rows and \(m\) columns. \(n\) and \(m\) are divisible by \(4\). Some of the cells are already colored black or white. It is guaranteed that no two colored cells share a corner or an edge.Color the remaining cells in a way that both the black and the white cells becomes orthogonally connected or determine that it is impossible.Consider a graph, where the black cells are the nodes. Two nodes are adjacent if the corresponding cells share an edge. If the described graph is connected, the black cells are orthogonally connected. Same for white cells.
|
The input consists of multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \le t \le 4000\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(m\) (\(8 \le n, m \le 500\), \(n\) and \(m\) are divisible by \(4\)) β the number of rows and columns.Each of the next \(n\) lines contains \(m\) characters. Each character is either 'B', 'W' or '.', representing black, white or empty cell respectively. Two colored (black or white) cell does not share a corner or an edge.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(250\,000\).
|
For each testcase print ""NO"" if there is no solution, otherwise print ""YES"" and a grid with the same format. If there are multiple solutions, you can print any.
|
Solution for test case 1: Test case 2: one can see that the black and the white part can't be connected in the same time. So the answer is ""NO"".
|
Input: 4 8 8 .W.W.... .....B.W .W.W.... .....W.W B.B..... ....B.B. B.W..... ....B.B. 8 8 B.W..B.W ........ W.B..W.B ........ ........ B.W..B.W ........ W.B..W.B 8 12 W.B......... ....B...B.W. B.B......... ....B...B.B. .B.......... ........B... .W..B.B...W. ............ 16 16 .W............W. ...W..W..W.W.... .B...........B.W ....W....W...... W......B....W.W. ..W.......B..... ....W...W....B.W .W....W....W.... ...B...........W W.....W...W..B.. ..W.W...W......B ............W... .W.B...B.B....B. .....W.....W.... ..W......W...W.. W...W..W...W...W | Output: YES BWWWWWWW BWBBBBBW BWBWWWBW BWBWBWBW BWBWBWBW BWBBBWBW BWWWWWBW BBBBBBBW NO YES WWBBBBBBBBBB BWWWBBBBBBWB BBBWBBBWWWWB BBBWBBBWBBBB BBBWBBBWBBBB BBBWWWWWBBBB BWWWBBBWWWWB BBBBBBBBBBBB YES WWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWW WBBBBBBBBBBBBBWW WBBBWBWWWWBWWBWW WBBWWBBBWWBWWBWW WBWWWBWWWWBWWBWW WBBWWBBBWWBWWBWW WWBWWWWWWWWWWWWW WWBBBBBBBBBBBBWW WBBBWWWBWWWBWBWW WWWBWBBBWBBBWBBB WWWBWBWWWWWBWWBW WWWBWBBBWBBBWWBW WWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWW
|
Master
| 1 | 594 | 679 | 164 | 16 |
38 |
C
|
38C
|
C. Blinds
| 1,400 |
brute force
|
The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are n blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths)Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe)After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way.Thus, if the blinds consist of k pieces each d in length, then they are of form of a rectangle of k Γ d bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than l bourlemeter. The window is of form of a rectangle with side lengths as positive integers.
|
The first output line contains two space-separated integers n and l (1 β€ n, l β€ 100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated n integers ai. They are the lengths of initial stripes in bourlemeters (1 β€ ai β€ 100).
|
Print the single number β the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0.
|
In the first sample test the required window is 2 Γ 4 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves.
|
Input: 4 21 2 3 4 | Output: 8
|
Easy
| 1 | 1,418 | 330 | 255 | 0 |
1,388 |
D
|
1388D
|
D. Captain Flint and Treasure
| 2,000 |
data structures; dfs and similar; graphs; greedy; implementation; trees
|
Captain Fint is involved in another treasure hunt, but have found only one strange problem. The problem may be connected to the treasure's location or may not. That's why captain Flint decided to leave the solving the problem to his crew and offered an absurdly high reward: one day off. The problem itself sounds like this...There are two arrays \(a\) and \(b\) of length \(n\). Initially, an \(ans\) is equal to \(0\) and the following operation is defined: Choose position \(i\) (\(1 \le i \le n\)); Add \(a_i\) to \(ans\); If \(b_i \neq -1\) then add \(a_i\) to \(a_{b_i}\). What is the maximum \(ans\) you can get by performing the operation on each \(i\) (\(1 \le i \le n\)) exactly once?Uncle Bogdan is eager to get the reward, so he is asking your help to find the optimal order of positions to perform the operation on them.
|
The first line contains the integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of arrays \(a\) and \(b\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(β10^6 \le a_i \le 10^6\)).The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le n\) or \(b_i = -1\)).Additional constraint: it's guaranteed that for any \(i\) (\(1 \le i \le n\)) the sequence \(b_i, b_{b_i}, b_{b_{b_i}}, \ldots\) is not cyclic, in other words it will always end with \(-1\).
|
In the first line, print the maximum \(ans\) you can get.In the second line, print the order of operations: \(n\) different integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)). The \(p_i\) is the position which should be chosen at the \(i\)-th step. If there are multiple orders, print any of them.
|
Input: 3 1 2 3 2 3 -1 | Output: 10 1 2 3
|
Hard
| 6 | 833 | 500 | 303 | 13 |
|
2,096 |
H
|
2096H
|
H. Wonderful XOR Problem
| 3,200 |
bitmasks; combinatorics; dp; fft; math
|
You are the proud... never mind, just solve this problem.There are \(n\) intervals \([l_1, r_1], [l_2, r_2], \ldots [l_n, r_n]\). For each \(x\) from \(0\) to \(2^m - 1\), find the number, modulo \(998\,244\,353\), of sequences \(a_1, a_2, \ldots a_n\) such that: \(l_i \leq a_i \leq r_i\) for all \(i\) from \(1\) to \(n\); \(a_1 \oplus a_2 \oplus \ldots \oplus a_n = x\), where \(\oplus\) denotes the bitwise XOR operator.
|
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 contains two integers \(n\) and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq m \leq 18\)).The \(i\)-th of the next \(n\) lines contains two integers \(l_i\) and \(r_i\) (\(0 \leq l_i \leq r_i < 2^m\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\), and the sum of \(2^m\) over all test cases does not exceed \(2^{18}\).
|
For each \(x\) from \(0\) to \(2^m - 1\), let: \(f_x\) be the number of valid sequences, modulo \(998\,244\,353\); \(g_x = f_x \cdot 2^x \mod 998\,244\,353\). Here, \(f_x\) and \(g_x\) are both integers in the interval \([0, 998\,244\,352]\).Let \(h = g_0 \oplus g_1 \oplus \ldots \oplus g_{2^m - 1}\).Output a single integer β the value of \(h\) itself. Do not perform a modulo operation.
|
For the first test case, the values of \(f_x\) are as follows: \(f_0 = 2\), because there are \(2\) valid sequences: \([1, 1]\) and \([2, 2]\); \(f_1 = 2\), because there are \(2\) valid sequences: \([0, 1]\) and \([2, 3]\); \(f_2 = 2\), because there are \(2\) valid sequences: \([0, 2]\) and \([1, 3]\); \(f_3 = 3\), because there are \(3\) valid sequences: \([0, 3]\), \([1, 2]\), and \([2, 1]\).The values of \(g_x\) are as follows: \(g_0 = f_0 \cdot 2^0 = 2 \cdot 2^0 = 2\); \(g_1 = f_1 \cdot 2^1 = 2 \cdot 2^1 = 4\); \(g_2 = f_2 \cdot 2^2 = 2 \cdot 2^2 = 8\); \(g_3 = f_3 \cdot 2^3 = 3 \cdot 2^3 = 24\).Thus, the value to output is \(2 \oplus 4 \oplus 8 \oplus 24 = 22\).For the second test case, the values of \(f_x\) are as follows: \(f_{0} = 120\); \(f_{1} = 120\); \(f_{2} = 119\); \(f_{3} = 118\); \(f_{4} = 105\); \(f_{5} = 105\); \(f_{6} = 106\); \(f_{7} = 107\).
|
Input: 42 20 21 35 33 71 30 21 53 610 14314 1592653 5897932 3846264 3383279 5028841 9716939 9375105 8209749 4459230 78161 50 29 | Output: 22 9812 75032210 1073741823
|
Master
| 5 | 424 | 544 | 389 | 20 |
1,116 |
B2
|
1116B2
|
B2. Not A, not B or not C?
| 0 |
*special
|
You are given a qubit which is guaranteed to be in one of the following states: \(|A\rangle = \frac{1}{\sqrt{2}} \big( |0\rangle + |1\rangle \big)\), \(|B\rangle = \frac{1}{\sqrt{2}} \big( |0\rangle + \omega |1\rangle \big)\), or \(|C\rangle = \frac{1}{\sqrt{2}} \big( |0\rangle + \omega^2 |1\rangle \big)\), where \(\omega = e^{2i\pi/3}\).These states are not orthogonal, and thus can not be distinguished perfectly. Your task is to figure out in which state the qubit is not. More formally: If the qubit was in state \(|A\rangle\), you have to return 1 or 2. If the qubit was in state \(|B\rangle\), you have to return 0 or 2. If the qubit was in state \(|C\rangle\), you have to return 0 or 1. In other words, return 0 if you're sure the qubit was not in state \(|A\rangle\), return 1 if you're sure the qubit was not in state \(|B\rangle\), and return 2 if you're sure the qubit was not in state \(|C\rangle\).Your solution will be called 1000 times, each time the state of the qubit will be chosen as \(|A\rangle\), \(|B\rangle\) or \(|C\rangle\) with equal probability. The state of the qubit after the operations does not matter.You have to implement an operation which takes a qubit as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (q : Qubit) : Int { // your code here }}
|
Beginner
| 1 | 1,414 | 0 | 0 | 11 |
||||
731 |
C
|
731C
|
C. Socks
| 1,600 |
dfs and similar; dsu; graphs; greedy
|
Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes. Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days β the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint β one for each of k colors.Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.
|
The first line of input contains three integers n, m and k (2 β€ n β€ 200 000, 0 β€ m β€ 200 000, 1 β€ k β€ 200 000) β the number of socks, the number of days and the number of available colors respectively.The second line contain n integers c1, c2, ..., cn (1 β€ ci β€ k) β current colors of Arseniy's socks.Each of the following m lines contains two integers li and ri (1 β€ li, ri β€ n, li β ri) β indices of socks which Arseniy should wear during the i-th day.
|
Print one integer β the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
|
In the first sample, Arseniy can repaint the first and the third socks to the second color.In the second sample, there is no need to change any colors.
|
Input: 3 2 31 2 31 22 3 | Output: 2
|
Medium
| 4 | 1,638 | 454 | 201 | 7 |
1,523 |
B
|
1523B
|
B. Lord of the Values
| 1,100 |
constructive algorithms
|
While trading on his favorite exchange trader William realized that he found a vulnerability. Using this vulnerability he could change the values of certain internal variables to his advantage. To play around he decided to change the values of all internal variables from \(a_1, a_2, \ldots, a_n\) to \(-a_1, -a_2, \ldots, -a_n\). For some unknown reason, the number of service variables is always an even number.William understands that with his every action he attracts more and more attention from the exchange's security team, so the number of his actions must not exceed \(5\,000\) and after every operation no variable can have an absolute value greater than \(10^{18}\). William can perform actions of two types for two chosen variables with indices \(i\) and \(j\), where \(i < j\): Perform assignment \(a_i = a_i + a_j\) Perform assignment \(a_j = a_j - a_i\) William wants you to develop a strategy that will get all the internal variables to the desired values.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\)). Description of the test cases follows.The first line of each test case contains a single even integer \(n\) (\(2 \le n \le 10^3\)), which is the number of internal variables.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((1 \le a_i \le 10^9)\), which are initial values of internal variables.
|
For each test case print the answer in the following format:The first line of output must contain the total number of actions \(k\), which the strategy will perform. Note that you do not have to minimize \(k\). The inequality \(k \le 5\,000\) must be satisfied. Each of the next \(k\) lines must contain actions formatted as ""type i j"", where ""type"" is equal to ""1"" if the strategy needs to perform an assignment of the first type and ""2"" if the strategy needs to perform an assignment of the second type. Note that \(i < j\) should hold.We can show that an answer always exists.
|
For the first sample test case one possible sequence of operations is as follows: ""2 1 2"". Values of variables after performing the operation: [1, 0, 1, 1] ""2 1 2"". Values of variables after performing the operation: [1, -1, 1, 1] ""2 1 3"". Values of variables after performing the operation: [1, -1, 0, 1] ""2 1 3"". Values of variables after performing the operation: [1, -1, -1, 1] ""2 1 4"". Values of variables after performing the operation: [1, -1, -1, 0] ""2 1 4"". Values of variables after performing the operation: [1, -1, -1, -1] ""1 1 2"". Values of variables after performing the operation: [0, -1, -1, -1] ""1 1 2"". Values of variables after performing the operation: [-1, -1, -1, -1] For the second sample test case one possible sequence of operations is as follows: ""2 1 4"". Values of variables after performing the operation: [4, 3, 1, -2] ""1 2 4"". Values of variables after performing the operation: [4, 1, 1, -2] ""1 2 4"". Values of variables after performing the operation: [4, -1, 1, -2] ""1 2 4"". Values of variables after performing the operation: [4, -3, 1, -2] ""1 3 4"". Values of variables after performing the operation: [4, -3, -1, -2] ""1 1 2"". Values of variables after performing the operation: [1, -3, -1, -2] ""1 1 2"". Values of variables after performing the operation: [-2, -3, -1, -2] ""1 1 4"". Values of variables after performing the operation: [-4, -3, -1, -2]
|
Input: 2 4 1 1 1 1 4 4 3 1 2 | Output: 8 2 1 2 2 1 2 2 1 3 2 1 3 2 1 4 2 1 4 1 1 2 1 1 2 8 2 1 4 1 2 4 1 2 4 1 2 4 1 3 4 1 1 2 1 1 2 1 1 4
|
Easy
| 1 | 972 | 448 | 587 | 15 |
1,427 |
H
|
1427H
|
H. Prison Break
| 3,500 |
binary search; games; geometry; ternary search
|
A prisoner wants to escape from a prison. The prison is represented by the interior of the convex polygon with vertices \(P_1, P_2, P_3, \ldots, P_{n+1}, P_{n+2}, P_{n+3}\). It holds \(P_1=(0,0)\), \(P_{n+1}=(0, h)\), \(P_{n+2}=(-10^{18}, h)\) and \(P_{n+3}=(-10^{18}, 0)\). The prison walls \(P_{n+1}P_{n+2}\), \(P_{n+2}P_{n+3}\) and \(P_{n+3}P_1\) are very high and the prisoner is not able to climb them. Hence his only chance is to reach a point on one of the walls \(P_1P_2, P_2P_3,\dots, P_{n}P_{n+1}\) and escape from there. On the perimeter of the prison, there are two guards. The prisoner moves at speed \(1\) while the guards move, remaining always on the perimeter of the prison, with speed \(v\).If the prisoner reaches a point of the perimeter where there is a guard, the guard kills the prisoner. If the prisoner reaches a point of the part of the perimeter he is able to climb and there is no guard there, he escapes immediately. Initially the prisoner is at the point \((-10^{17}, h/2)\) and the guards are at \(P_1\). Find the minimum speed \(v\) such that the guards can guarantee that the prisoner will not escape (assuming that both the prisoner and the guards move optimally).Notes: At any moment, the guards and the prisoner can see each other. The ""climbing part"" of the escape takes no time. You may assume that both the prisoner and the guards can change direction and velocity instantly and that they both have perfect reflexes (so they can react instantly to whatever the other one is doing). The two guards can plan ahead how to react to the prisoner movements.
|
The first line of the input contains \(n\) (\(1 \le n \le 50\)).The following \(n+1\) lines describe \(P_1, P_2,\dots, P_{n+1}\). The \(i\)-th of such lines contain two integers \(x_i\), \(y_i\) (\(0\le x_i, y_i\le 1,000\)) β the coordinates of \(P_i=(x_i, y_i)\).It is guaranteed that \(P_1=(0,0)\) and \(x_{n+1}=0\). The polygon with vertices \(P_1,P_2,\dots, P_{n+1}, P_{n+2}, P_{n+3}\) (where \(P_{n+2}, P_{n+3}\) shall be constructed as described in the statement) is guaranteed to be convex and such that there is no line containing three of its vertices.
|
Print a single real number, the minimum speed \(v\) that allows the guards to guarantee that the prisoner will not escape. Your answer will be considered correct if its relative or absolute error does not exceed \(10^{-6}\).
|
Input: 2 0 0 223 464 0 749 | Output: 1
|
Master
| 4 | 1,592 | 561 | 224 | 14 |
|
1,973 |
B
|
1973B
|
B. Cat, Fox and the Lonely Array
| 1,300 |
binary search; bitmasks; data structures; greedy; math; two pointers
|
Today, Cat and Fox found an array \(a\) consisting of \(n\) non-negative integers. Define the loneliness of \(a\) as the smallest positive integer \(k\) (\(1 \le k \le n\)) such that for any two positive integers \(i\) and \(j\) (\(1 \leq i, j \leq n - k +1\)), the following holds: $$$\(a_i | a_{i+1} | \ldots | a_{i+k-1} = a_j | a_{j+1} | \ldots | a_{j+k-1},\)\( where \)x | y\( denotes the bitwise OR of \)x\( and \)y\(. In other words, for every \)k\( consecutive elements, their bitwise OR should be the same. Note that the loneliness of \)a\( is well-defined, because for \)k = n\( the condition is satisfied.Cat and Fox want to know how lonely the array \)a$$$ is. Help them calculate the loneliness of the found array.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4 \)) β the number of test cases. The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 10^5\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < 2^{20}\)) β the elements of the array.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case, print one integer β the loneliness of the given array.
|
In the first example, the loneliness of an array with a single element is always \(1\), so the answer is \(1\). In the second example, the OR of each subarray of length \(k = 1\) is \(2\), so the loneliness of the whole array is \(1\).In the seventh example, it's true that \((0 | 1 | 3) = (1 | 3 | 2) = (3 | 2 | 2) = (2 | 2 | 1) = (2 | 1 | 0) = (1 | 0 | 3) = 3\), so the condition is satisfied for \(k = 3\). We can verify that the condition is not true for any smaller \(k\), so the answer is indeed \(3\).
|
Input: 71032 2 231 0 253 0 1 4 252 0 4 0 270 0 0 0 1 2 480 1 3 2 2 1 0 3 | Output: 1 1 3 4 4 7 3
|
Easy
| 6 | 726 | 524 | 74 | 19 |
1,646 |
D
|
1646D
|
D. Weight the Tree
| 2,000 |
constructive algorithms; dfs and similar; dp; implementation; trees
|
You are given a tree of \(n\) vertices numbered from \(1\) to \(n\). A tree is a connected undirected graph without cycles. For each \(i=1,2, \ldots, n\), let \(w_i\) be the weight of the \(i\)-th vertex. A vertex is called good if its weight is equal to the sum of the weights of all its neighbors.Initially, the weights of all nodes are unassigned. Assign positive integer weights to each vertex of the tree, such that the number of good vertices in the tree is maximized. If there are multiple ways to do it, you have to find one that minimizes the sum of weights of all vertices in the tree.
|
The first line contains one integer \(n\) (\(2\le n\le 2\cdot 10^5\)) β the number of vertices in the tree.Then, \(nβ1\) lines follow. Each of them contains two integers \(u\) and \(v\) (\(1\le u,v\le n\)) denoting an edge between vertices \(u\) and \(v\). It is guaranteed that the edges form a tree.
|
In the first line print two integers β the maximum number of good vertices and the minimum possible sum of weights for that maximum.In the second line print \(n\) integers \(w_1, w_2, \ldots, w_n\) (\(1\le w_i\le 10^9\)) β the corresponding weight assigned to each vertex. It can be proven that there exists an optimal solution satisfying these constraints.If there are multiple optimal solutions, you may print any.
|
This is the tree for the first test case: In this case, if you assign a weight of \(1\) to each vertex, then the good vertices (which are painted black) are \(1\), \(3\) and \(4\). It impossible to assign weights so that all vertices are good vertices. The minimum sum of weights in this case is \(1+1+1+1=4\), and it is impossible to have a lower sum because the weights have to be positive integers.This is the tree for the second test case: In this case, if you assign a weight of \(1\) to each vertex, then the good vertices (which are painted black) are \(2\) and \(3\). It can be proven that this is an optimal assignment.
|
Input: 4 1 2 2 3 2 4 | Output: 3 4 1 1 1 1
|
Hard
| 5 | 595 | 301 | 416 | 16 |
2,061 |
D
|
2061D
|
D. Kevin and Numbers
| 1,600 |
bitmasks; data structures
|
Kevin wrote an integer sequence \(a\) of length \(n\) on the blackboard.Kevin can perform the following operation any number of times: Select two integers \(x, y\) on the blackboard such that \(|x - y| \leq 1\), erase them, and then write down an integer \(x + y\) instead. Kevin wants to know if it is possible to transform these integers into an integer sequence \(b\) of length \(m\) through some sequence of operations.Two sequences \(a\) and \(b\) are considered the same if and only if their multisets are identical. In other words, for any number \(x\), the number of times it appears in \(a\) must be equal to the number of times it appears in \(b\).
|
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 integers \(n\) and \(m\) (\(1\leq m \leq n \leq 2\cdot 10^5\)) β the length of \(a\) and the length of \(b\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1\leq a_i \leq 10^9\)).The third line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1\leq b_i \leq 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output ""Yes"" if it is possible to transform \(a\) into \(b\), and ""No"" otherwise.You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
In the first test case, you can erase \(4, 5\), and write down \(9\).In the second test case, you can't erase \(3, 6\).In the third test case, one possible way could be: Erase \(2, 2\), and write down \(4\). The remaining numbers are \(1, 2, 4\) now. Erase \(1, 2\), and write down \(3\). The remaining numbers are \(3, 4\) now. In the fourth test case, one possible way could be: Erase \(1, 1\), and write down \(2\). The remaining numbers are \(2, 3, 3\) now. Erase \(2, 3\), and write down \(5\). The remaining numbers are \(3, 5\) now.
|
Input: 92 14 592 13 694 21 2 2 23 44 21 1 3 33 54 21 2 3 43 55 51 2 3 4 55 4 3 2 14 21 1 1 11 14 41 1 1 11 1 1 21 111000000000 | Output: Yes No Yes Yes No Yes No No No
|
Medium
| 2 | 658 | 591 | 266 | 20 |
1,334 |
F
|
1334F
|
F. Strange Function
| 2,500 |
binary search; data structures; dp; greedy
|
Let's denote the following function \(f\). This function takes an array \(a\) of length \(n\) and returns an array. Initially the result is an empty array. For each integer \(i\) from \(1\) to \(n\) we add element \(a_i\) to the end of the resulting array if it is greater than all previous elements (more formally, if \(a_i > \max\limits_{1 \le j < i}a_j\)). Some examples of the function \(f\): if \(a = [3, 1, 2, 7, 7, 3, 6, 7, 8]\) then \(f(a) = [3, 7, 8]\); if \(a = [1]\) then \(f(a) = [1]\); if \(a = [4, 1, 1, 2, 3]\) then \(f(a) = [4]\); if \(a = [1, 3, 1, 2, 6, 8, 7, 7, 4, 11, 10]\) then \(f(a) = [1, 3, 6, 8, 11]\). You are given two arrays: array \(a_1, a_2, \dots , a_n\) and array \(b_1, b_2, \dots , b_m\). You can delete some elements of array \(a\) (possibly zero). To delete the element \(a_i\), you have to pay \(p_i\) coins (the value of \(p_i\) can be negative, then you get \(|p_i|\) coins, if you delete this element). Calculate the minimum number of coins (possibly negative) you have to spend for fulfilling equality \(f(a) = b\).
|
The first line contains one integer \(n\) \((1 \le n \le 5 \cdot 10^5)\) β the length of array \(a\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le n)\) β the array \(a\).The third line contains \(n\) integers \(p_1, p_2, \dots, p_n\) \((|p_i| \le 10^9)\) β the array \(p\).The fourth line contains one integer \(m\) \((1 \le m \le n)\) β the length of array \(b\).The fifth line contains \(m\) integers \(b_1, b_2, \dots, b_m\) \((1 \le b_i \le n, b_{i-1} < b_i)\) β the array \(b\).
|
If the answer exists, in the first line print YES. In the second line, print the minimum number of coins you have to spend for fulfilling equality \(f(a) = b\).Otherwise in only line print NO.
|
Input: 11 4 1 3 3 7 8 7 9 10 7 11 3 5 0 -2 5 3 6 7 8 2 4 3 3 7 10 | Output: YES 20
|
Expert
| 4 | 1,056 | 519 | 192 | 13 |
|
1,010 |
D
|
1010D
|
D. Mars rover
| 2,000 |
dfs and similar; graphs; implementation; trees
|
Natasha travels around Mars in the Mars rover. But suddenly it broke down, namely β the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex \(1\), in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, which is output. One bit is fed to each input. One bit is returned at the output.There are four types of logical elements: AND (\(2\) inputs), OR (\(2\) inputs), XOR (\(2\) inputs), NOT (\(1\) input). Logical elements take values from their direct descendants (inputs) and return the result of the function they perform. Natasha knows the logical scheme of the Mars rover, as well as the fact that only one input is broken. In order to fix the Mars rover, she needs to change the value on this input.For each input, determine what the output will be if Natasha changes this input.
|
The first line contains a single integer \(n\) (\(2 \le n \le 10^6\)) β the number of vertices in the graph (both inputs and elements).The \(i\)-th of the next \(n\) lines contains a description of \(i\)-th vertex: the first word ""AND"", ""OR"", ""XOR"", ""NOT"" or ""IN"" (means the input of the scheme) is the vertex type. If this vertex is ""IN"", then the value of this input follows (\(0\) or \(1\)), otherwise follow the indices of input vertices of this element: ""AND"", ""OR"", ""XOR"" have \(2\) inputs, whereas ""NOT"" has \(1\) input. The vertices are numbered from one.It is guaranteed that input data contains a correct logical scheme with an output produced by the vertex \(1\).
|
Print a string of characters '0' and '1' (without quotes) β answers to the problem for each input in the ascending order of their vertex indices.
|
The original scheme from the example (before the input is changed):Green indicates bits '1', yellow indicates bits '0'.If Natasha changes the input bit \(2\) to \(0\), then the output will be \(1\).If Natasha changes the input bit \(3\) to \(0\), then the output will be \(0\).If Natasha changes the input bit \(6\) to \(1\), then the output will be \(1\).If Natasha changes the input bit \(8\) to \(0\), then the output will be \(1\).If Natasha changes the input bit \(9\) to \(0\), then the output will be \(0\).
|
Input: 10AND 9 4IN 1IN 1XOR 6 5AND 3 7IN 0NOT 10IN 1IN 1AND 2 8 | Output: 10110
|
Hard
| 4 | 901 | 694 | 145 | 10 |
1,423 |
K
|
1423K
|
K. Lonely Numbers
| 1,600 |
binary search; math; number theory; two pointers
|
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.More precisely, two different numbers \(a\) and \(b\) are friends if \(gcd(a,b)\), \(\frac{a}{gcd(a,b)}\), \(\frac{b}{gcd(a,b)}\) can form sides of a triangle.Three numbers \(a\), \(b\) and \(c\) can form sides of a triangle if \(a + b > c\), \(b + c > a\) and \(c + a > b\).In a group of numbers, a number is lonely if it doesn't have any friends in that group.Given a group of numbers containing all numbers from \(1, 2, 3, ..., n\), how many numbers in that group are lonely?
|
The first line contains a single integer \(t\) \((1 \leq t \leq 10^6)\) - number of test cases.On next line there are \(t\) numbers, \(n_i\) \((1 \leq n_i \leq 10^6)\) - meaning that in case \(i\) you should solve for numbers \(1, 2, 3, ..., n_i\).
|
For each test case, print the answer on separate lines: number of lonely numbers in group \(1, 2, 3, ..., n_i\).
|
For first test case, \(1\) is the only number and therefore lonely.For second test case where \(n=5\), numbers \(1\), \(3\) and \(5\) are lonely.For third test case where \(n=10\), numbers \(1\), \(5\) and \(7\) are lonely.
|
Input: 3 1 5 10 | Output: 1 3 3
|
Medium
| 4 | 594 | 248 | 112 | 14 |
771 |
B
|
771B
|
B. Bear and Different Names
| 1,500 |
constructive algorithms; greedy
|
In the army, it isn't easy to form a group of soldiers that will be effective on the battlefield. The communication is crucial and thus no two soldiers should share a name (what would happen if they got an order that Bob is a scouter, if there are two Bobs?).A group of soldiers is effective if and only if their names are different. For example, a group (John, Bob, Limak) would be effective, while groups (Gary, Bob, Gary) and (Alice, Alice) wouldn't.You are a spy in the enemy's camp. You noticed n soldiers standing in a row, numbered 1 through n. The general wants to choose a group of k consecutive soldiers. For every k consecutive soldiers, the general wrote down whether they would be an effective group or not.You managed to steal the general's notes, with n - k + 1 strings s1, s2, ..., sn - k + 1, each either ""YES"" or ""NO"". The string s1 describes a group of soldiers 1 through k (""YES"" if the group is effective, and ""NO"" otherwise). The string s2 describes a group of soldiers 2 through k + 1. And so on, till the string sn - k + 1 that describes a group of soldiers n - k + 1 through n. Your task is to find possible names of n soldiers. Names should match the stolen notes. Each name should be a string that consists of between 1 and 10 English letters, inclusive. The first letter should be uppercase, and all other letters should be lowercase. Names don't have to be existing names β it's allowed to print ""Xyzzzdj"" or ""T"" for example.Find and print any solution. It can be proved that there always exists at least one solution.
|
The first line of the input contains two integers n and k (2 β€ k β€ n β€ 50) β the number of soldiers and the size of a group respectively.The second line contains n - k + 1 strings s1, s2, ..., sn - k + 1. The string si is ""YES"" if the group of soldiers i through i + k - 1 is effective, and ""NO"" otherwise.
|
Find any solution satisfying all given conditions. In one line print n space-separated strings, denoting possible names of soldiers in the order. The first letter of each name should be uppercase, while the other letters should be lowercase. Each name should contain English letters only and has length from 1 to 10.If there are multiple valid solutions, print any of them.
|
In the first sample, there are 8 soldiers. For every 3 consecutive ones we know whether they would be an effective group. Let's analyze the provided sample output: First three soldiers (i.e. Adam, Bob, Bob) wouldn't be an effective group because there are two Bobs. Indeed, the string s1 is ""NO"". Soldiers 2 through 4 (Bob, Bob, Cpqepqwer) wouldn't be effective either, and the string s2 is ""NO"". Soldiers 3 through 5 (Bob, Cpqepqwer, Limak) would be effective, and the string s3 is ""YES"". ..., Soldiers 6 through 8 (Adam, Bob, Adam) wouldn't be effective, and the string s6 is ""NO"".
|
Input: 8 3NO NO YES YES YES NO | Output: Adam Bob Bob Cpqepqwer Limak Adam Bob Adam
|
Medium
| 2 | 1,559 | 310 | 373 | 7 |
766 |
A
|
766A
|
A. Mahmoud and Longest Uncommon Subsequence
| 1,000 |
constructive algorithms; strings
|
While Mahmoud and Ehab were practicing for IOI, they found a problem which name was Longest common subsequence. They solved it, and then Ehab challenged Mahmoud with another problem.Given two strings a and b, find the length of their longest uncommon subsequence, which is the longest string that is a subsequence of one of them and not a subsequence of the other.A subsequence of some string is a sequence of characters that appears in the same order in the string, The appearances don't have to be consecutive, for example, strings ""ac"", ""bc"", ""abc"" and ""a"" are subsequences of string ""abc"" while strings ""abbc"" and ""acb"" are not. The empty string is a subsequence of any string. Any string is a subsequence of itself.
|
The first line contains string a, and the second line β string b. Both of these strings are non-empty and consist of lowercase letters of English alphabet. The length of each string is not bigger than 105 characters.
|
If there's no uncommon subsequence, print ""-1"". Otherwise print the length of the longest uncommon subsequence of a and b.
|
In the first example: you can choose ""defgh"" from string b as it is the longest subsequence of string b that doesn't appear as a subsequence of string a.
|
Input: abcddefgh | Output: 5
|
Beginner
| 2 | 734 | 216 | 124 | 7 |
1,374 |
E2
|
1374E2
|
E2. Reading Books (hard version)
| 2,500 |
data structures; greedy; implementation; sortings; ternary search; two pointers
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully.Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn't think so. She says that they have to read exactly \(m\) books before all entertainments. Alice and Bob will read each book together to end this exercise faster.There are \(n\) books in the family library. The \(i\)-th book is described by three integers: \(t_i\) β the amount of time Alice and Bob need to spend to read it, \(a_i\) (equals \(1\) if Alice likes the \(i\)-th book and \(0\) if not), and \(b_i\) (equals \(1\) if Bob likes the \(i\)-th book and \(0\) if not).So they need to choose exactly \(m\) books from the given \(n\) books in such a way that: Alice likes at least \(k\) books from the chosen set and Bob likes at least \(k\) books from the chosen set; the total reading time of these \(m\) books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it's shared between them) and they read all books together, so the total reading time is the sum of \(t_i\) over all books that are in the chosen set.Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set.
|
The first line of the input contains three integers \(n\), \(m\) and \(k\) (\(1 \le k \le m \le n \le 2 \cdot 10^5\)).The next \(n\) lines contain descriptions of books, one description per line: the \(i\)-th line contains three integers \(t_i\), \(a_i\) and \(b_i\) (\(1 \le t_i \le 10^4\), \(0 \le a_i, b_i \le 1\)), where: \(t_i\) β the amount of time required for reading the \(i\)-th book; \(a_i\) equals \(1\) if Alice likes the \(i\)-th book and \(0\) otherwise; \(b_i\) equals \(1\) if Bob likes the \(i\)-th book and \(0\) otherwise.
|
If there is no solution, print only one integer -1.If the solution exists, print \(T\) in the first line β the minimum total reading time of the suitable set of books. In the second line print \(m\) distinct integers from \(1\) to \(n\) in any order β indices of books which are in the set you found.If there are several answers, print any of them.
|
Input: 6 3 16 0 011 1 09 0 121 1 110 1 08 0 1 | Output: 246 5 1
|
Expert
| 6 | 1,323 | 542 | 348 | 13 |
|
1,906 |
B
|
1906B
|
B. Button Pressing
| 2,600 |
bitmasks; constructive algorithms; hashing
|
You are given \(N\) buttons (numbered from \(1\) to \(N\)) and \(N\) lamps (numbered from \(1\) to \(N\)). Each lamp can either be on or off. Initially, lamp \(i\) is on if \(A_i = 1\), and off if \(A_i = 0\).Button \(i\) is connected to lamp \(i - 1\) (if \(i > 1\)) and lamp \(i + 1\) (if \(i < N\)). In one move, you can press a button \(i\) only if lamp \(i\) is on. When a button is pressed, the state of the lamps connected to this button is toggled. Formally, the lamps will be on if it was off previously, and the lamps will be off if it was on previously. Note that lamp \(i\) is not connected to button \(i\), thus, the state of lamp \(i\) does not change if button \(i\) is pressed.After zero or more moves, you want lamp \(i\) to be on if \(B_i = 1\), and off if \(B_i = 0\). Determine if it is possible to achieve this task.
|
This problem has multiple test cases. The first line consists of an integer \(T\) (\(1 \leq T \leq 1000\)), which represents the number of test cases. Each test case consists of three lines.The first line of each test case consists of an integer \(N\) (\(3 \le N \le 200\,000\)). The sum of \(N\) over all test cases does not exceed \(200\,000\).The second line of each test case consists of a string \(A\) of length \(N\). Each character of \(A\) can either be 0 or 1. The \(i\)-th character represents the initial state of lamp \(i\).The third line of each test case consists of a string \(A\) of length \(N\). Each character of \(B\) can either be 0 or 1. The \(i\)-th character represents the desired final state of lamp \(i\).
|
For each test case, output YES in a single line if the final state of all lamps can be reached after zero or more moves, or NO otherwise.
|
Explanation for the sample input/output #1For the first test case, by pressing the buttons \(4, 2, 4, 3, 1, 2\) in sequence, the condition of the buttons changes as: \(0101 \rightarrow 0111 \rightarrow 1101 \rightarrow 1111 \rightarrow 1010 \rightarrow 1110 \rightarrow 0100\).For the second test case, you are unable to press any button, hence it is impossible to reach the final state.
|
Input: 2 4 0101 0100 3 000 010 | Output: YES NO
|
Expert
| 3 | 837 | 731 | 137 | 19 |
683 |
F
|
683F
|
F. Reformat the String
| 1,800 |
*special
|
In this problem you are given a string s consisting of uppercase and lowercase Latin letters, spaces, dots and commas. Your task is to correct the formatting of this string by removing and inserting spaces, as well as changing the case of the letters.After formatting, the resulting string must meet the following requirements: the string must not start with a space; there should be exactly one space between any two consecutive words; there should be a Latin letter immediately before a dot or a comma, and there should be a space immediately after a dot or a comma in the case that there is a word after that dot or comma, otherwise that dot or comma must be the last character in the string; all letters must be lowercase, except all first letters in the first words of the sentences, they must be capitalized. The first word of a sentence is a word that is either the first word of the string or a word after a dot. It is guaranteed that there is at least one letter in the given string between any two punctuation marks (commas and dots are punctuation marks). There is at least one letter before the leftmost punctuation mark.
|
The first line contains a non-empty string s, consisting of uppercase and lowercase Latin letters, spaces, dots and commas. The length of the given string does not exceed 255. The string is guaranteed to have at least one character other than the space.
|
Output the corrected string which meets all the requirements described in the statement.
|
Input: hello ,i AM veRy GooD.Boris | Output: Hello, i am very good. Boris
|
Medium
| 1 | 1,133 | 253 | 88 | 6 |
|
1,120 |
E
|
1120E
|
E. The very same Munchhausen
| 2,600 |
brute force
|
A positive integer \(a\) is given. Baron Munchausen claims that he knows such a positive integer \(n\) that if one multiplies \(n\) by \(a\), the sum of its digits decreases \(a\) times. In other words, \(S(an) = S(n)/a\), where \(S(x)\) denotes the sum of digits of the number \(x\). Find out if what Baron told can be true.
|
The only line contains a single integer \(a\) (\(2 \le a \le 10^3\)).
|
If there is no such number \(n\), print \(-1\).Otherwise print any appropriate positive integer \(n\). Your number must not consist of more than \(5\cdot10^5\) digits. We can show that under given constraints either there is no answer, or there is an answer no longer than \(5\cdot10^5\) digits.
|
Input: 2 | Output: 6
|
Expert
| 1 | 325 | 69 | 295 | 11 |
|
2,075 |
A
|
2075A
|
A. To Zero
| 800 |
greedy; math
|
You are given two integers \(n\) and \(k\); \(k\) is an odd number not less than \(3\). Your task is to turn \(n\) into \(0\).To do this, you can perform the following operation any number of times: choose a number \(x\) from \(1\) to \(k\) and subtract it from \(n\). However, if the current value of \(n\) is even (divisible by \(2\)), then \(x\) must also be even, and if the current value of \(n\) is odd (not divisible by \(2\)), then \(x\) must be odd.In different operations, you can choose the same values of \(x\), but you don't have to. So, there are no limitations on using the same value of \(x\).Calculate the minimum number of operations required to turn \(n\) into \(0\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β the number of test cases.Each test case consists of one line containing two integers \(n\) and \(k\) (\(3 \le k \le n \le 10^9\), \(k\) is odd).
|
For each test case, output one integer β the minimum number of operations required to turn \(n\) into \(0\).
|
In the first example from the statement, you can first subtract \(5\) from \(39\) to get \(34\). Then subtract \(6\) five times to get \(4\). Finally, subtract \(4\) to get \(0\).In the second example, you can subtract \(3\) once, and then subtract \(2\) three times.In the third example, you can subtract \(2\) three times.
|
Input: 839 79 36 3999967802 35 56 5999999999 31000000000 3 | Output: 7 4 3 499983901 1 2 499999999 500000000
|
Beginner
| 2 | 686 | 212 | 108 | 20 |
743 |
C
|
743C
|
C. Vladik and fractions
| 1,500 |
brute force; constructive algorithms; math; number theory
|
Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer n he can represent fraction as a sum of three distinct positive fractions in form .Help Vladik with that, i.e for a given n find three distinct positive integers x, y and z such that . Because Chloe can't check Vladik's answer if the numbers are large, he asks you to print numbers not exceeding 109.If there is no such answer, print -1.
|
The single line contains single integer n (1 β€ n β€ 104).
|
If the answer exists, print 3 distinct numbers x, y and z (1 β€ x, y, z β€ 109, x β y, x β z, y β z). Otherwise print -1.If there are multiple answers, print any of them.
|
Input: 3 | Output: 2 7 42
|
Medium
| 4 | 451 | 56 | 168 | 7 |
|
1,237 |
B
|
1237B
|
B. Balanced Tunnel
| 1,300 |
data structures; sortings; two pointers
|
Consider a tunnel on a one-way road. During a particular day, \(n\) cars numbered from \(1\) to \(n\) entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.Traffic regulations prohibit overtaking inside the tunnel. If car \(i\) overtakes any other car \(j\) inside the tunnel, car \(i\) must be fined. However, each car can be fined at most once.Formally, let's say that car \(i\) definitely overtook car \(j\) if car \(i\) entered the tunnel later than car \(j\) and exited the tunnel earlier than car \(j\). Then, car \(i\) must be fined if and only if it definitely overtook at least one other car.Find the number of cars that must be fined.
|
The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)), denoting the number of cars.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)), denoting the ids of cars in order of entering the tunnel. All \(a_i\) are pairwise distinct.The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le n\)), denoting the ids of cars in order of exiting the tunnel. All \(b_i\) are pairwise distinct.
|
Output the number of cars to be fined.
|
The first example is depicted below:Car \(2\) definitely overtook car \(5\), while car \(4\) definitely overtook cars \(1\), \(2\), \(3\) and \(5\). Cars \(2\) and \(4\) must be fined.In the second example car \(5\) was definitely overtaken by all other cars.In the third example no car must be fined.
|
Input: 5 3 5 2 1 4 4 3 2 5 1 | Output: 2
|
Easy
| 3 | 979 | 459 | 38 | 12 |
1,242 |
B
|
1242B
|
B. 0-1 MST
| 1,900 |
dfs and similar; dsu; graphs; sortings
|
Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a description of a graph.It is an undirected weighted graph on \(n\) vertices. It is a complete graph: each pair of vertices is connected by an edge. The weight of each edge is either \(0\) or \(1\); exactly \(m\) edges have weight \(1\), and all others have weight \(0\).Since Ujan doesn't really want to organize his notes, he decided to find the weight of the minimum spanning tree of the graph. (The weight of a spanning tree is the sum of all its edges.) Can you find the answer for Ujan so he stops procrastinating?
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq \min(\frac{n(n-1)}{2},10^5)\)), the number of vertices and the number of edges of weight \(1\) in the graph. The \(i\)-th of the next \(m\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \leq a_i, b_i \leq n\), \(a_i \neq b_i\)), the endpoints of the \(i\)-th edge of weight \(1\).It is guaranteed that no edge appears twice in the input.
|
Output a single integer, the weight of the minimum spanning tree of the graph.
|
The graph from the first sample is shown below. Dashed edges have weight \(0\), other edges have weight \(1\). One of the minimum spanning trees is highlighted in orange and has total weight \(2\). In the second sample, all edges have weight \(0\) so any spanning tree has total weight \(0\).
|
Input: 6 11 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 | Output: 2
|
Hard
| 4 | 710 | 451 | 78 | 12 |
22 |
B
|
22B
|
B. Bargaining Table
| 1,500 |
brute force; dp
|
Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n Γ m meters. Each square meter of the room is either occupied by some furniture, or free. A bargaining table is rectangular, and should be placed so, that its sides are parallel to the office walls. Bob doesn't want to change or rearrange anything, that's why all the squares that will be occupied by the table should be initially free. Bob wants the new table to sit as many people as possible, thus its perimeter should be maximal. Help Bob find out the maximum possible perimeter of a bargaining table for his office.
|
The first line contains 2 space-separated numbers n and m (1 β€ n, m β€ 25) β the office room dimensions. Then there follow n lines with m characters 0 or 1 each. 0 stands for a free square meter of the office room. 1 stands for an occupied square meter. It's guaranteed that at least one square meter in the room is free.
|
Output one number β the maximum possible perimeter of a bargaining table for Bob's office room.
|
Input: 3 3000010000 | Output: 8
|
Medium
| 2 | 683 | 320 | 95 | 0 |
|
1,770 |
A
|
1770A
|
A. Koxia and Whiteboards
| 1,000 |
brute force; greedy
|
Kiyora has \(n\) whiteboards numbered from \(1\) to \(n\). Initially, the \(i\)-th whiteboard has the integer \(a_i\) written on it.Koxia performs \(m\) operations. The \(j\)-th operation is to choose one of the whiteboards and change the integer written on it to \(b_j\).Find the maximum possible sum of integers written on the whiteboards after performing all \(m\) operations.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n,m \le 100\)).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 third line of each test case contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1 \le b_i \le 10^9\)).
|
For each test case, output a single integer β the maximum possible sum of integers written on whiteboards after performing all \(m\) operations.
|
In the first test case, Koxia can perform the operations as follows: Choose the \(1\)-st whiteboard and rewrite the integer written on it to \(b_1=4\). Choose the \(2\)-nd whiteboard and rewrite to \(b_2=5\). After performing all operations, the numbers on the three whiteboards are \(4\), \(5\) and \(3\) respectively, and their sum is \(12\). It can be proven that this is the maximum possible sum achievable.In the second test case, Koxia can perform the operations as follows: Choose the \(2\)-nd whiteboard and rewrite to \(b_1=3\). Choose the \(1\)-st whiteboard and rewrite to \(b_2=4\). Choose the \(2\)-nd whiteboard and rewrite to \(b_3=5\). The sum is \(4 + 5 = 9\). It can be proven that this is the maximum possible sum achievable.
|
Input: 43 21 2 34 52 31 23 4 51 110015 31 1 1 1 11000000000 1000000000 1000000000 | Output: 12 9 1 3000000002
|
Beginner
| 2 | 379 | 493 | 144 | 17 |
1,738 |
A
|
1738A
|
A. Glory Addicts
| 800 |
greedy; implementation; sortings
|
The hero is addicted to glory, and is fighting against a monster. The hero has \(n\) skills. The \(i\)-th skill is of type \(a_i\) (either fire or frost) and has initial damage \(b_i\). The hero can perform all of the \(n\) skills in any order (with each skill performed exactly once). When performing each skill, the hero can play a magic as follows: If the current skill immediately follows another skill of a different type, then its damage is doubled. In other words, If a skill of type fire and with initial damage \(c\) is performed immediately after a skill of type fire, then it will deal \(c\) damage; If a skill of type fire and with initial damage \(c\) is performed immediately after a skill of type frost, then it will deal \(2c\) damage; If a skill of type frost and with initial damage \(c\) is performed immediately after a skill of type fire, then it will deal \(2c\) damage; If a skill of type frost and with initial damage \(c\) is performed immediately after a skill of type frost , then it will deal \(c\) damage. Your task is to find the maximum damage the hero can deal.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)), indicating the number of skills. The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \leq a_i \leq 1\)), where \(a_i\) indicates the type of the \(i\)-th skill. Specifically, the \(i\)-th skill is of type fire if \(a_i = 0\), and of type frost if \(a_i = 1\). The third line of each test case contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \leq b_i \leq 10^9\)), where \(b_i\) indicates the initial damage of the \(i\)-th skill. It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output the maximum damage the hero can deal.
|
In the first test case, we can order the skills by \([3, 1, 4, 2]\), and the total damage is \(100 + 2 \times 1 + 2 \times 1000 + 10 = 2112\).In the second test case, we can order the skills by \([1, 4, 2, 5, 3, 6]\), and the total damage is \(3 + 2 \times 6 + 2 \times 4 + 2 \times 7 + 2 \times 5 + 2 \times 8 = 63\).In the third test case, we can order the skills by \([1, 2, 3]\), and the total damage is \(1000000000 + 1000000000 + 1000000000 = 3000000000\).In the fourth test case, there is only one skill with initial damage \(1\), so the total damage is \(1\).
|
Input: 440 1 1 11 10 100 100060 0 0 1 1 13 4 5 6 7 831 1 11000000000 1000000000 1000000000111 | Output: 2112 63 3000000000 1
|
Beginner
| 3 | 1,093 | 837 | 64 | 17 |
221 |
A
|
221A
|
A. Little Elephant and Function
| 1,000 |
implementation; math
|
The Little Elephant enjoys recursive functions.This time he enjoys the sorting function. Let a is a permutation of an integers from 1 to n, inclusive, and ai denotes the i-th element of the permutation. The Little Elephant's recursive function f(x), that sorts the first x permutation's elements, works as follows: If x = 1, exit the function. Otherwise, call f(x - 1), and then make swap(ax - 1, ax) (swap the x-th and (x - 1)-th elements of a). The Little Elephant's teacher believes that this function does not work correctly. But that-be do not get an F, the Little Elephant wants to show the performance of its function. Help him, find a permutation of numbers from 1 to n, such that after performing the Little Elephant's function (that is call f(n)), the permutation will be sorted in ascending order.
|
A single line contains integer n (1 β€ n β€ 1000) β the size of permutation.
|
In a single line print n distinct integers from 1 to n β the required permutation. Numbers in a line should be separated by spaces.It is guaranteed that the answer exists.
|
Input: 1 | Output: 1
|
Beginner
| 2 | 808 | 74 | 171 | 2 |
|
737 |
F
|
737F
|
F. Dirty plates
| 3,300 |
constructive algorithms; math
|
After one of celebrations there is a stack of dirty plates in Nikita's kitchen. Nikita has to wash them and put into a dryer. In dryer, the plates should be also placed in a stack also, and the plates sizes should increase down up. The sizes of all plates are distinct.Nikita has no so much free space, specifically, he has a place for only one more stack of plates. Therefore, he can perform only such two operations: Take any number of plates from 1 to a from the top of the dirty stack, wash them and put them to the intermediate stack. Take any number of plates from 1 to b from the top of the intermediate stack and put them to the stack in the dryer. Note that after performing each of the operations, the plates are put in the same order as they were before the operation.You are given the sizes of the plates s1, s2, ..., sn in the down up order in the dirty stack, and integers a and b. All the sizes are distinct. Write a program that determines whether or not Nikita can put the plates in increasing down up order in the dryer. If he is able to do so, the program should find some sequence of operations (not necessary optimal) to achieve it.
|
The first line contains three integers n, a and b (1 β€ n β€ 2000, 1 β€ a, b β€ n). The second line contains integers s1, s2, ..., sn (1 β€ si β€ n) β the sizes of the plates in down up order. All the sizes are distinct.
|
In the first line print ""YES"" if there is a solution. In this case, in the second line print integer k β the number of operations. Then in k lines print the operations, one per line. Each operation is described by two integers tj and cj, where tj = 1, if the operation is to wash the top cj places from the dirty stack and put them onto the intermediate stack, and tj = 2, if the operation is to move th top cj plates from the intermediate stack to the dryer. In case there is no solution, print single line ""NO"".If there are multiple solutions, print any of them. Note that it is not necessary to minimize the number of operations.
|
In the first example the initial order of plates was 2, 3, 6, 4, 1, 5. Here is how the stacks look like after each of the operations: [1 2]: Dirty stack: 6, 4, 1, 5. Intermediary stack: 2, 3. The dryer is empty. [1 1]: Dirty stack: 4, 1, 5. Intermediary stack: 6, 2, 3. The dryer is empty. [2 1]: Dirty stack: 4, 1, 5. Intermediary stack: 2, 3. Dryer stack: 6. [1 2]: Dirty stack: 5. Intermediary stack: 4, 1, 2, 3. Dryer stack: 6. [1 1]: There are no dirty plates. Intermediary stack: 5, 4, 1, 2, 3. Dryer stack: 6. [2 1]: There are no dirty plates. Intermediary stack: 4, 1, 2, 3. Dryer stack: 5, 6. [2 1]: There are no dirty plates. Intermediary stack: 1, 2, 3. Dryer stack: 4, 5, 6. [2 3]: All the plates are in the dryer: 1, 2, 3, 4, 5, 6. In the second example it is possible to wash all the plates in one operation, and then move them all to the dryer.This is not possible in the third example, because it is not permitted to move more than one plate at the same time. It is possible to wash plates one by one so that they are placed onto the intermediary stack in the reverse order, and then move plates one by one to the dryer. The final order is correct.
|
Input: 6 2 32 3 6 4 1 5 | Output: YES81 21 12 11 21 12 12 12 3
|
Master
| 2 | 1,153 | 214 | 636 | 7 |
901 |
D
|
901D
|
D. Weighting a Tree
| 2,700 |
constructive algorithms; dfs and similar; graphs
|
You are given a connected undirected graph with n vertices and m edges. The vertices are enumerated from 1 to n. You are given n integers c1, c2, ..., cn, each of them is between - n and n, inclusive. It is also guaranteed that the parity of cv equals the parity of degree of vertex v. The degree of a vertex is the number of edges connected to it.You are to write a weight between - 2Β·n2 and 2Β·n2 (inclusive) on each edge in such a way, that for each vertex v the sum of weights on edges connected to this vertex is equal to cv, or determine that this is impossible.
|
The first line contains two integers n and m (2 β€ n β€ 105, n - 1 β€ m β€ 105) β the number of vertices and the number of edges.The next line contains n integers c1, c2, ..., cn ( - n β€ ci β€ n), where ci is the required sum of weights of edges connected to vertex i. It is guaranteed that the parity of ci equals the parity of degree of vertex i.The next m lines describe edges of the graph. The i-th of these lines contains two integers ai and bi (1 β€ ai, bi β€ n; ai β bi), meaning that the i-th edge connects vertices ai and bi.It is guaranteed that the given graph is connected and does not contain loops and multiple edges.
|
If there is no solution, print ""NO"".Otherwise print ""YES"" and then m lines, the i-th of them is the weight of the i-th edge wi ( - 2Β·n2 β€ wi β€ 2Β·n2).
|
Input: 3 32 2 21 22 31 3 | Output: YES111
|
Master
| 3 | 567 | 624 | 153 | 9 |
|
1,322 |
F
|
1322F
|
F. Assigning Fares
| 3,500 |
dp; trees
|
Mayor of city M. decided to launch several new metro lines during 2020. Since the city has a very limited budget, it was decided not to dig new tunnels but to use the existing underground network.The tunnel system of the city M. consists of \(n\) metro stations. The stations are connected with \(n - 1\) bidirectional tunnels. Between every two stations \(v\) and \(u\) there is exactly one simple path. Each metro line the mayor wants to create is a simple path between stations \(a_i\) and \(b_i\). Metro lines can intersects freely, that is, they can share common stations or even common tunnels. However, it's not yet decided which of two directions each line will go. More precisely, between the stations \(a_i\) and \(b_i\) the trains will go either from \(a_i\) to \(b_i\), or from \(b_i\) to \(a_i\), but not simultaneously.The city \(M\) uses complicated faring rules. Each station is assigned with a positive integer \(c_i\) β the fare zone of the station. The cost of travel from \(v\) to \(u\) is defined as \(c_u - c_v\) roubles. Of course, such travel only allowed in case there is a metro line, the trains on which go from \(v\) to \(u\). Mayor doesn't want to have any travels with a negative cost, so it was decided to assign directions of metro lines and station fare zones in such a way, that fare zones are strictly increasing during any travel on any metro line.Mayor wants firstly assign each station a fare zone and then choose a lines direction, such that all fare zones are increasing along any line. In connection with the approaching celebration of the day of the city, the mayor wants to assign fare zones so that the maximum \(c_i\) will be as low as possible. Please help mayor to design a new assignment or determine if it's impossible to do. Please note that you only need to assign the fare zones optimally, you don't need to print lines' directions. This way, you solution will be considered correct if there will be a way to assign directions of every metro line, so that the fare zones will be strictly increasing along any movement of the trains.
|
The first line contains an integers \(n\), \(m\) (\(2 \le n, \le 500\,000,\ 1 \le m \le 500\,000\)) β the number of stations in the city and the number of metro lines.Each of the following \(n-1\) lines describes another metro tunnel. Each tunnel is described with integers \(v_i\), \(u_i\) (\(1 \le v_i,\ u_i \le n\), \(v_i \ne u_i\)). It's guaranteed, that there is exactly one simple path between any two stations.Each of the following \(m\) lines describes another metro line. Each line is described with integers \(a_i\), \(b_i\) (\(1 \le a_i,\ b_i \le n\), \(a_i \neq b_i\)).
|
In the first line print integer \(k\) β the maximum fare zone used.In the next line print integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le k\)) β stations' fare zones. In case there are several possible answers, print any of them. In case it's impossible to assign fare zones, print ""-1"".
|
In the first example, line \(1 \rightarrow 3\) goes through the stations 1, 2, 3 in this order. In this order the fare zones of the stations are increasing. Since this line has 3 stations, at least three fare zones are needed. So the answer 1, 2, 3 is optimal.In the second example, it's impossible to assign fare zones to be consistent with a metro lines.
|
Input: 3 1 1 2 2 3 1 3 | Output: 3 1 2 3
|
Master
| 2 | 2,084 | 581 | 293 | 13 |
317 |
A
|
317A
|
A. Perfect Pair
| 1,600 |
brute force
|
Let us call a pair of integer numbers m-perfect, if at least one number in the pair is greater than or equal to m. Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not.Two integers x, y are written on the blackboard. It is allowed to erase one of them and replace it with the sum of the numbers, (x + y).What is the minimum number of such operations one has to perform in order to make the given pair of integers m-perfect?
|
Single line of the input contains three integers x, y and m ( - 1018 β€ x, y, m β€ 1018).Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preffered to use the cin, cout streams or the %I64d specifier.
|
Print the minimum number of operations or ""-1"" (without quotes), if it is impossible to transform the given pair to the m-perfect one.
|
In the first sample the following sequence of operations is suitable: (1, 2) (3, 2) (5, 2).In the second sample: (-1, 4) (3, 4) (7, 4) (11, 4) (15, 4).Finally, in the third sample x, y cannot be made positive, hence there is no proper sequence of operations.
|
Input: 1 2 5 | Output: 2
|
Medium
| 1 | 448 | 234 | 136 | 3 |
1,482 |
E
|
1482E
|
E. Skyline Photo
| 2,100 |
data structures; divide and conquer; dp
|
Alice is visiting New York City. To make the trip fun, Alice will take photos of the city skyline and give the set of photos as a present to Bob. However, she wants to find the set of photos with maximum beauty and she needs your help. There are \(n\) buildings in the city, the \(i\)-th of them has positive height \(h_i\). All \(n\) building heights in the city are different. In addition, each building has a beauty value \(b_i\). Note that beauty can be positive or negative, as there are ugly buildings in the city too. A set of photos consists of one or more photos of the buildings in the skyline. Each photo includes one or more buildings in the skyline that form a contiguous segment of indices. Each building needs to be in exactly one photo. This means that if a building does not appear in any photo, or if a building appears in more than one photo, the set of pictures is not valid. The beauty of a photo is equivalent to the beauty \(b_i\) of the shortest building in it. The total beauty of a set of photos is the sum of the beauty of all photos in it. Help Alice to find the maximum beauty a valid set of photos can have.
|
The first line contains an integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)), the number of buildings on the skyline. The second line contains \(n\) distinct integers \(h_1, h_2, \ldots, h_n\) (\(1 \le h_i \le n\)). The \(i\)-th number represents the height of building \(i\).The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(-10^9 \le b_i \le 10^9\)). The \(i\)-th number represents the beauty of building \(i\).
|
Print one number representing the maximum beauty Alice can achieve for a valid set of photos of the skyline.
|
In the first example, Alice can achieve maximum beauty by taking five photos, each one containing one building. In the second example, Alice can achieve a maximum beauty of \(10\) by taking four pictures: three just containing one building, on buildings \(1\), \(2\) and \(5\), each photo with beauty \(-3\), \(4\) and \(7\) respectively, and another photo containing building \(3\) and \(4\), with beauty \(2\). In the third example, Alice will just take one picture of the whole city.In the fourth example, Alice can take the following pictures to achieve maximum beauty: photos with just one building on buildings \(1\), \(2\), \(8\), \(9\), and \(10\), and a single photo of buildings \(3\), \(4\), \(5\), \(6\), and \(7\).
|
Input: 5 1 2 3 5 4 1 5 3 2 4 | Output: 15
|
Hard
| 3 | 1,137 | 426 | 108 | 14 |
2,095 |
G
|
2095G
|
G. Definitely a Geometry Problem
| 0 |
*special; geometry
|
Megumin is a wizard who can cast powerful magic spells that affect large areas. She is obsessed with the extremely devastating Explosion spell that can destroy everything around its target.There are currently \(n\) slimes in the wild. Each slime's location can be described in 2D Cartesian coordinates. Megumin wishes to kill at least \(k\) slimes so she can earn more experience points to level up her Explosion. To do that, she can choose any circle on the plane and cast an Explosion spell which kills every slime inside or on the border of the circle.However, the Explosion magic requires a large amount of mana to cast, and Megumin can only use it once per day. The amount of mana used is equal to the area inside the circle she chooses. What is the minimum amount of mana she needs to spend to kill \(k\) slimes with a single Explosion?
|
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^5\)) β the number of slimes and how many slimes Megumin needs to defeat. Each of the next \(n\) lines contains two integers \(x\) and \(y\) (\(-10^9 \le x, y \le 10^9\)), denoting the coordinates \((x, y)\) of a slime's location. It is guaranteed that all locations are distinct and that no three slimes lie on the same circle.
|
Print one real number β the answer.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\). Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{|a-b|}{\max(1,|b|)} \le 10^{-6}\).
|
In the first example, Megumin can hit the slime with a circle centered at \((0, 0)\) with radius \(0\).In the second example, Megumin can hit the first and last slimes with a circle centered at \((1, 0)\) with radius \(1\).
|
Input: 1 10 0 | Output: 0.000000000000000
|
Beginner
| 2 | 842 | 404 | 281 | 20 |
1,275 |
B
|
1275B
|
B. Code Review
| 0 |
*special
|
Π£ ΠΊΠΎΠΌΠ°Π½Π΄Ρ ΡΠ°Π·ΡΠ°Π±ΠΎΡΠΊΠΈ Π΄Π²ΠΈΠΆΠΊΠΎΠ² ΠΠΠΎΠ½ΡΠ°ΠΊΡΠ΅ Π΅ΡΡΡ ΠΎΠ±ΡΠΈΠΉ ΡΠ°Ρ. ΠΠΎΡΠ»Π΅ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»ΠΎΠΆΠ½ΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠ° Π² ΡΠ΅ΠΏΠΎΠ·ΠΈΡΠΎΡΠΈΠΉ, Π°Π²ΡΠΎΡ ΡΡΠΎΠ³ΠΎ ΠΊΠΎΠΌΠΌΠΈΡΠ° ΠΏΡΠΈΡΡΠ»Π°Π΅Ρ Π² ΡΠ°Ρ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΠ΅ Ρ ΠΏΡΠ΅Π΄Π»ΠΎΠΆΠ΅Π½ΠΈΠ΅ΠΌ ΠΏΡΠΎΠ²Π΅ΡΡΠΈ ΡΠ΅Π²ΡΡ. ΠΠ»Ρ ΠΎΠ΄ΠΎΠ±ΡΠ΅Π½ΠΈΡ ΠΈΠ»ΠΈ ΠΎΡΠΊΠ»ΠΎΠ½Π΅Π½ΠΈΡ ΠΊΠΎΠΌΠΌΠΈΡΠ° Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ, ΡΡΠΎΠ±Ρ Π΅Π³ΠΎ ΠΏΡΠΎΠ²Π΅ΡΠΈΠ» ΠΎΠ΄ΠΈΠ½ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊ, Π½Π΅ ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π²ΡΠΈΠΉ ΡΡΠ°ΡΡΠΈΠ΅ Π² Π΅Π³ΠΎ Π½Π°ΠΏΠΈΡΠ°Π½ΠΈΠΈ.ΠΠ΅ΡΠ΅Π΄ ΡΠ΅ΠΌ, ΠΊΠ°ΠΊ ΠΎΡΡΠ°Π²ΠΈΡΡ Π½ΠΎΠ²ΡΡ Π·Π°ΡΠ²ΠΊΡ Π½Π° ΡΠ΅Π²ΡΡ, ΠΊΠ°ΠΆΠ΄ΡΠΉ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΏΡΠΎΠ²ΠΎΠ΄ΠΈΡ ΡΠ΅Π²ΡΡ ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅ΠΉ ΠΈΠ· ΠΎΡΡΠ°Π²Π»Π΅Π½Π½ΡΡ
Π·Π°ΡΠ²ΠΎΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ Π΅ΡΠ΅ Π½ΠΈΠΊΠ΅ΠΌ Π½Π΅ ΠΏΡΠΎΠ²Π΅ΡΠ΅Π½Ρ (Π΅ΡΠ»ΠΈ ΠΌΠΎΠΆΠ΅Ρ β Π΅ΡΠ»ΠΈ ΠΎΠ½ Π½Π΅ ΡΠ²Π»ΡΠ΅ΡΡΡ Π°Π²ΡΠΎΡΠΎΠΌ ΡΡΠΎΠΉ Π·Π°ΡΠ²ΠΊΠΈ) ΠΈ ΡΠΎΠ»ΡΠΊΠΎ ΠΏΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΎΡΡΠ°Π²Π»ΡΠ΅Ρ Π½ΠΎΠ²ΡΡ Π·Π°ΡΠ²ΠΊΡ.ΠΠ°ΠΌ Π΄Π°Π½ Π»ΠΎΠ³ Π·Π°ΡΠ²ΠΎΠΊ Π½Π° ΡΠ΅Π²ΡΡ Ρ ΠΌΠΎΠΌΠ΅Π½ΡΠ° ΠΏΠΎΡΠ²Π»Π΅Π½ΠΈΡ ΡΠ°ΡΠ° Π² Ρ
ΡΠΎΠ½ΠΎΠ»ΠΎΠ³ΠΈΡΠ΅ΡΠΊΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. ΠΠ°ΠΉΠ΄ΠΈΡΠ΅ ΠΊΠΎΠΌΠΌΠΈΡΡ, ΠΊΠΎΡΠΎΡΡΠ΅ Π½ΠΈΠΊΡΠΎ Π½Π΅ ΠΏΡΠΎΠ²Π΅ΡΠΈΠ».
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ n (1 β€ n β€ 50 000) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΡΠΎΡΡΠ± ΠΎ code review Π² ΡΠ°ΡΠ΅.Π ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
n ΡΡΡΠΎΠΊΠ°Ρ
Π·Π°ΠΏΠΈΡΠ°Π½Ρ Π²Π½ΡΡΡΠ΅Π½Π½ΠΈΠΉ ΡΠ΅Π»ΠΎΡΠΈΡΠ»Π΅Π½Π½ΡΠΉ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊΠ° i ΠΈ Ρ
Π΅Ρ ΠΊΠΎΠΌΠΌΠΈΡΠ° h (1 β€ i β€ 50 000; h ΡΠΎΡΡΠΎΠΈΡ ΠΈΠ· ΡΡΡΠΎΡΠ½ΡΡ
Π±ΡΠΊΠ² Π»Π°ΡΠΈΠ½ΡΠΊΠΎΠ³ΠΎ Π°Π»ΡΠ°Π²ΠΈΡΠ° ΠΎΡ a Π΄ΠΎ f ΠΈ ΡΠΈΡΡ). ΠΡΠ΅ Ρ
Π΅ΡΠΈ ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ² ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½Ρ ΠΈ ΠΈΠΌΠ΅ΡΡ Π΄Π»ΠΈΠ½Ρ ΠΎΡ 1 Π΄ΠΎ 20 ΡΠΈΠΌΠ²ΠΎΠ»ΠΎΠ², Π²ΠΊΠ»ΡΡΠΈΡΠ΅Π»ΡΠ½ΠΎ.
|
ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ Π²ΡΠ΅ Ρ
Π΅ΡΠΈ ΠΊΠΎΠΌΠΌΠΈΡΠΎΠ², ΠΊΠΎΡΠΎΡΡΠ΅ Π½Π΅ ΠΏΠΎΠΏΠ°Π»ΠΈ Π½Π° ΡΠ΅Π²ΡΡ, Π² ΡΠΎΠΌ ΠΆΠ΅ ΠΏΠΎΡΡΠ΄ΠΊΠ΅, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ ΠΎΠ½ΠΈ Π±ΡΠ»ΠΈ Π΄Π°Π½Ρ Π²ΠΎ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
.
|
Input: 7 1 0e813c50 1 00e9422b 1 520cb7b4 2 052dd9ad 3 9dd5f347 3 e35f067b 1 bb4d4a99 | Output: 0e813c50 00e9422b 9dd5f347 bb4d4a99
|
Beginner
| 1 | 670 | 362 | 116 | 12 |
|
878 |
B
|
878B
|
B. Teams Formation
| 2,300 |
data structures; implementation
|
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city ai.Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a1, a2, ..., an repeated m times).After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city.Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected.
|
The first line contains three integers n, k and m (1 β€ n β€ 105, 2 β€ k β€ 109, 1 β€ m β€ 109).The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 105), where ai is the number of city, person from which must take seat i in the bus.
|
Output the number of remaining participants in the line.
|
In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line.
|
Input: 4 2 51 2 3 1 | Output: 12
|
Expert
| 2 | 977 | 236 | 56 | 8 |
1,768 |
A
|
1768A
|
A. Greatest Convex
| 800 |
greedy; math; number theory
|
You are given an integer \(k\). Find the largest integer \(x\), where \(1 \le x < k\), such that \(x! + (x - 1)!^\dagger\) is a multiple of \(^\ddagger\) \(k\), or determine that no such \(x\) exists.\(^\dagger\) \(y!\) denotes the factorial of \(y\), which is defined recursively as \(y! = y \cdot (y-1)!\) for \(y \geq 1\) with the base case of \(0! = 1\). For example, \(5! = 5 \cdot 4 \cdot 3 \cdot 2 \cdot 1 \cdot 0! = 120\).\(^\ddagger\) If \(a\) and \(b\) are integers, then \(a\) is a multiple of \(b\) if there exists an integer \(c\) such that \(a = b \cdot c\). For example, \(10\) is a multiple of \(5\) but \(9\) is not a multiple of \(6\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of test cases follows.The only line of each test case contains a single integer \(k\) (\(2 \le k \le 10^9\)).
|
For each test case output a single integer β the largest possible integer \(x\) that satisfies the conditions above. If no such \(x\) exists, output \(-1\).
|
In the first test case, \(2! + 1! = 2 + 1 = 3\), which is a multiple of \(3\).In the third test case, \(7! + 6! = 5040 + 720 = 5760\), which is a multiple of \(8\).
|
Input: 436810 | Output: 2 5 7 9
|
Beginner
| 3 | 653 | 223 | 156 | 17 |
1,181 |
E2
|
1181E2
|
E2. A Story of One Country (Hard)
| 3,000 |
brute force; greedy; sortings
|
This problem differs from the previous problem only in constraints.Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual.Initially, there were \(n\) different countries on the land that is now Berland. Each country had its own territory that was represented as a rectangle on the map. The sides of the rectangle were parallel to the axes, and the corners were located at points with integer coordinates. Territories of no two countries intersected, but it was possible that some territories touched each other. As time passed, sometimes two countries merged into one. It only happened if the union of their territories was also a rectangle. In the end only one country remained β Byteland.Initially, each country had a rectangular castle inside its territory. Its sides were parallel to the axes and its corners had integer coordinates. Some castles might touch the border of the corresponding country and sides or other castles. Miraculously, after all the unions the castles are still intact. Unfortunately, their locations are the only information we have to restore the initial territories of the countries. The possible formation of Byteland. The castles are shown in blue. Petya wonders why no information about the initial countries remained. He suspected that the whole story is a fake. You were recommended to him as a smart person. Please check whether or not there exists a possible set of initial territories that could make the story true.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 100\,000\)) β the number of countries and castles.Each of the next \(n\) lines contains four integers \(a_i, b_i, c_i, d_i\) (\(0 \leq a_i < c_i \leq 10^9\), \(0 \leq b_i < d_i \leq 10^9\)) β the coordinates of the \(i\)-th castle, where \((a_i, b_i)\) are the coordinates of the lower left corner and \((c_i, d_i)\) are the coordinates of the upper right corner.It is guaranteed that no two castles intersect, however, they may touch.
|
If there exists a possible set of territories that satisfies the story, print ""YES"", otherwise print ""NO"".You can print each letter in any case (upper or lower).
|
The castles in the first and second examples are shown on the pictures below.
|
Input: 4 0 0 1 2 0 2 1 3 1 0 2 1 1 1 2 3 | Output: YES
|
Master
| 3 | 1,522 | 499 | 165 | 11 |
223 |
D
|
223D
|
D. Spider
| 3,000 |
geometry; graphs
|
A plane contains a not necessarily convex polygon without self-intersections, consisting of n vertexes, numbered from 1 to n. There is a spider sitting on the border of the polygon, the spider can move like that: Transfer. The spider moves from the point p1 with coordinates (x1, y1), lying on the polygon border, to the point p2 with coordinates (x2, y2), also lying on the border. The spider can't go beyond the polygon border as it transfers, that is, the spider's path from point p1 to point p2 goes along the polygon border. It's up to the spider to choose the direction of walking round the polygon border (clockwise or counterclockwise). Descend. The spider moves from point p1 with coordinates (x1, y1) to point p2 with coordinates (x2, y2), at that points p1 and p2 must lie on one vertical straight line (x1 = x2), point p1 must be not lower than point p2 (y1 β₯ y2) and segment p1p2 mustn't have points, located strictly outside the polygon (specifically, the segment can have common points with the border). Initially the spider is located at the polygon vertex with number s. Find the length of the shortest path to the vertex number t, consisting of transfers and descends. The distance is determined by the usual Euclidean metric .
|
The first line contains integer n (3 β€ n β€ 105) β the number of vertexes of the given polygon. Next n lines contain two space-separated integers each β the coordinates of the polygon vertexes. The vertexes are listed in the counter-clockwise order. The coordinates of the polygon vertexes do not exceed 104 in their absolute value. The last line contains two space-separated integers s and t (1 β€ s, t β€ n) β the start and the end vertexes of the sought shortest way. Consider the polygon vertexes numbered in the order they are given in the input, that is, the coordinates of the first vertex are located on the second line of the input and the coordinates of the n-th vertex are on the (n + 1)-th line. It is guaranteed that the given polygon is simple, that is, it contains no self-intersections or self-tangencies.
|
In the output print a single real number β the length of the shortest way from vertex s to vertex t. The answer is considered correct, if its absolute or relative error does not exceed 10 - 6.
|
In the first sample the spider transfers along the side that connects vertexes 1 and 4.In the second sample the spider doesn't have to transfer anywhere, so the distance equals zero.In the third sample the best strategy for the spider is to transfer from vertex 3 to point (2,3), descend to point (2,1), and then transfer to vertex 1.
|
Input: 40 01 01 10 11 4 | Output: 1.000000000000000000e+000
|
Master
| 2 | 1,245 | 818 | 192 | 2 |
1,970 |
A1
|
1970A1
|
A1. Balanced Shuffle (Easy)
| 1,000 |
implementation; sortings
|
A parentheses sequence is a string consisting of characters ""("" and "")"", for example ""(()(("".A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example ""(()(()))"".The balance of a parentheses sequence is defined as the number of opening parentheses ""("" minus the number of closing parentheses "")"". For example, the balance of the sequence ""(()(("" is 3.A balanced parentheses sequence can also be defined as a parentheses sequence with balance 0 such that each of its prefixes has a non-negative balance.We define the balanced shuffle operation that takes a parentheses sequence and returns a parentheses sequence as follows: first, for every character of the input sequence, we compute the balance of the prefix of the sequence before that character and write those down in a table together with the positions of the characters in the input sequence, for example:Prefix balance01212321Position12345678Character(()(()))Then, we sort the columns of this table in increasing order of prefix balance, breaking ties in decreasing order of position. In the above example, we get:Prefix balance01112223Position18427536Character()(()())The last row of this table forms another parentheses sequence, in this case ""()(()())"". This sequence is called the result of applying the balanced shuffle operation to the input sequence, or in short just the balanced shuffle of the input sequence.You are given a balanced parentheses sequence. Print its balanced shuffle.
|
The only line of input contains a string \(s\) consisting only of characters ""("" and "")"". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding \(500\,000\).
|
Print the parentheses sequence \(t\) β the balanced shuffle of \(s\).
|
Input: (()(())) | Output: ()(()())
|
Beginner
| 2 | 1,575 | 211 | 69 | 19 |
|
1,884 |
D
|
1884D
|
D. Counting Rhyme
| 2,100 |
dp; math; number theory
|
You are given an array of integers \(a_1, a_2, \ldots, a_n\).A pair of integers \((i, j)\), such that \(1 \le i < j \le n\), is called good, if there does not exist an integer \(k\) (\(1 \le k \le n\)) such that \(a_i\) is divisible by \(a_k\) and \(a_j\) is divisible by \(a_k\) at the same time.Please, find the number of good pairs.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2 \cdot 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output the number of good pairs.
|
In the first test case, there are no good pairs. In the second test case, here are all the good pairs: \((1, 2)\), \((2, 3)\), and \((2, 4)\).
|
Input: 642 4 4 442 3 4 496 8 9 4 6 8 9 4 997 7 4 4 9 9 6 2 91810 18 18 15 14 4 5 6 8 9 10 12 15 16 18 17 13 112112 19 19 18 18 12 2 18 19 12 12 3 12 12 12 18 19 16 18 19 12 | Output: 0 3 26 26 124 82
|
Hard
| 3 | 335 | 447 | 52 | 18 |
1,272 |
F
|
1272F
|
F. Two Bracket Sequences
| 2,200 |
dp; strings; two pointers
|
You are given two bracket sequences (not necessarily regular) \(s\) and \(t\) consisting only of characters '(' and ')'. You want to construct the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous).Recall what is the regular bracket sequence: () is the regular bracket sequence; if \(S\) is the regular bracket sequence, then (\(S\)) is a regular bracket sequence; if \(S\) and \(T\) regular bracket sequences, then \(ST\) (concatenation of \(S\) and \(T\)) is a regular bracket sequence. Recall that the subsequence of the string \(s\) is such string \(t\) that can be obtained from \(s\) by removing some (possibly, zero) amount of characters. For example, ""coder"", ""force"", ""cf"" and ""cores"" are subsequences of ""codeforces"", but ""fed"" and ""z"" are not.
|
The first line of the input contains one bracket sequence \(s\) consisting of no more than \(200\) characters '(' and ')'.The second line of the input contains one bracket sequence \(t\) consisting of no more than \(200\) characters '(' and ')'.
|
Print one line β the shortest regular bracket sequence that contains both given bracket sequences as subsequences (not necessarily contiguous). If there are several answers, you can print any.
|
Input: (())(() ()))() | Output: (())()()
|
Hard
| 3 | 839 | 245 | 192 | 12 |
|
297 |
B
|
297B
|
B. Fish Weight
| 1,600 |
constructive algorithms; greedy
|
It is known that there are k fish species in the polar ocean, numbered from 1 to k. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the i-th type of fish be wi, then 0 < w1 β€ w2 β€ ... β€ wk holds.Polar bears Alice and Bob each have caught some fish, and they are guessing who has the larger sum of weight of the fish he/she's caught. Given the type of the fish they've caught, determine whether it is possible that the fish caught by Alice has a strictly larger total weight than Bob's. In other words, does there exist a sequence of weights wi (not necessary integers), such that the fish caught by Alice has a strictly larger total weight?
|
The first line contains three integers n, m, k (1 β€ n, m β€ 105, 1 β€ k β€ 109) β the number of fish caught by Alice and Bob respectively, and the number of fish species.The second line contains n integers each from 1 to k, the list of fish type caught by Alice. The third line contains m integers each from 1 to k, the list of fish type caught by Bob.Note that one may have caught more than one fish for a same species.
|
Output ""YES"" (without quotes) if it is possible, and ""NO"" (without quotes) otherwise.
|
In the first sample, if w1 = 1, w2 = 2, w3 = 2.5, then Alice has a total of 2 + 2 + 2 = 6 weight units, while Bob only has 1 + 1 + 2.5 = 4.5.In the second sample, the fish that Alice caught is a subset of Bob's. Therefore, the total weight of Bobβs fish is always not less than the total weight of Aliceβs fish.
|
Input: 3 3 32 2 21 1 3 | Output: YES
|
Medium
| 2 | 695 | 417 | 89 | 2 |
1,277 |
B
|
1277B
|
B. Make Them Odd
| 1,200 |
greedy; number theory
|
There are \(n\) positive integers \(a_1, a_2, \dots, a_n\). For the one move you can choose any even value \(c\) and divide by two all elements that equal \(c\).For example, if \(a=[6,8,12,6,3,12]\) and you choose \(c=6\), and \(a\) is transformed into \(a=[3,8,12,3,3,12]\) after the move.You need to find the minimal number of moves for transforming \(a\) to an array of only odd integers (each element shouldn't be divisible by \(2\)).
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. Then \(t\) test cases follow.The first line of a test case contains \(n\) (\(1 \le n \le 2\cdot10^5\)) β the number of integers in the sequence \(a\). The second line contains positive integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)).The sum of \(n\) for all test cases in the input doesn't exceed \(2\cdot10^5\).
|
For \(t\) test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd (i.e. not divisible by \(2\)).
|
In the first test case of the example, the optimal sequence of moves can be as follows: before making moves \(a=[40, 6, 40, 3, 20, 1]\); choose \(c=6\); now \(a=[40, 3, 40, 3, 20, 1]\); choose \(c=40\); now \(a=[20, 3, 20, 3, 20, 1]\); choose \(c=20\); now \(a=[10, 3, 10, 3, 10, 1]\); choose \(c=10\); now \(a=[5, 3, 5, 3, 5, 1]\) β all numbers are odd. Thus, all numbers became odd after \(4\) moves. In \(3\) or fewer moves, you cannot make them all odd.
|
Input: 4 6 40 6 40 3 20 1 1 1024 4 2 4 8 16 3 3 1 7 | Output: 4 10 4 0
|
Easy
| 2 | 438 | 442 | 218 | 12 |
359 |
C
|
359C
|
C. Prime Number
| 1,900 |
math; number theory
|
Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.Simon loves fractions very much. Today he wrote out number on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction. Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).
|
The first line contains two positive integers n and x (1 β€ n β€ 105, 2 β€ x β€ 109) β the size of the array and the prime number.The second line contains n space-separated integers a1, a2, ..., an (0 β€ a1 β€ a2 β€ ... β€ an β€ 109).
|
Print a single number β the answer to the problem modulo 1000000007 (109 + 7).
|
In the first sample . Thus, the answer to the problem is 8.In the second sample, . The answer to the problem is 27, as 351 = 13Β·27, 729 = 27Β·27.In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.In the fourth sample . Thus, the answer to the problem is 1.
|
Input: 2 22 2 | Output: 8
|
Hard
| 2 | 513 | 225 | 78 | 3 |
2,110 |
B
|
2110B
|
B. Down with Brackets
| 900 |
strings
|
In 2077, robots decided to get rid of balanced bracket sequences once and for all!A bracket sequence is called balanced if it can be constructed by the following formal grammar. The empty sequence \(\varnothing\) is balanced. If the bracket sequence \(A\) is balanced, then \(\mathtt{(}A\mathtt{)}\) is also balanced. If the bracket sequences \(A\) and \(B\) are balanced, then the concatenated sequence \(A B\) is also balanced. You are the head of the department for combating balanced bracket sequences, and your main task is to determine which brackets you can destroy and which you cannot.You are given a balanced bracket sequence represented by a string \(s\), consisting of the characters ( and ). Since the robots' capabilities are not limitless, they can remove exactly one opening bracket and exactly one closing bracket from the string.Your task is to determine whether the robots can delete such two brackets so that the string \(s\) is no longer a balanced bracket sequence.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. Each test case consists of a single string \(s\) (\(2 \leq |s| \leq 2 \cdot 10^5\)) β a sequence of the characters ( and ).It is guaranteed that \(s\) is a balanced bracket sequence.It is also guaranteed that the sum of \(|s|\) across all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output ""YES"" if the robots can make the string stop being a balanced bracket sequence, and ""NO"" otherwise.You may output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be accepted as a positive answer.
|
In the first test case, it can be shown that the robots will not be able to break the correct bracket sequence.In the second test case, one of the options for removing brackets is as follows:\(\texttt{(())}\color{red}{\texttt{(}}\texttt{)(}\color{red}{\texttt{)}} \rightarrow \texttt{(()))(}\), which is not a correct bracket sequence.In the fourth test case, one of the options for removal is as follows:\(\texttt{(}\color{red}{\texttt{(}}\texttt{))((}\color{red}{\texttt{)}}\texttt{)}\rightarrow \texttt{())(()}\), which is not a correct bracket sequence.
|
Input: 4(())(())()()()(())(()) | Output: NO YES NO YES
|
Beginner
| 1 | 987 | 444 | 297 | 21 |
2,030 |
G1
|
2030G1
|
G1. The Destruction of the Universe (Easy Version)
| 2,900 |
combinatorics; greedy; math
|
This is the easy version of the problem. In this version, \(n \leq 5000\). You can only make hacks if both versions of the problem are solved.Orangutans are powerful beingsβso powerful that they only need \(1\) unit of time to destroy every vulnerable planet in the universe!There are \(n\) planets in the universe. Each planet has an interval of vulnerability \([l, r]\), during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by \(1\) unit.Specifically, suppose the expansion is performed on planet \(p\) with interval of vulnerability \([l_p, r_p]\). Then, the resulting interval of vulnerability may be either \([l_p - 1, r_p]\) or \([l_p, r_p + 1]\).Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo \(998\,244\,353\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 5000\)) β the number of planets in the universe.The following \(n\) lines contain two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)) β the initial interval of vulnerability of the \(i\)-th planet.It is guaranteed that the sum of \(n\) does not exceed \(5000\) over all test cases.
|
For each test case, output an integer β the sum of scores to destroy all non-empty subsets of the planets in the universe, modulo \(998\,244\,353\).
|
In the first testcase, there are seven non-empty subsets of planets we must consider: For each of the subsets \(\{[1,1]\}, \{[2,3]\}, \{[3,3]\}\), the score is \(0\). For the subset \(\{[2,3], [3,3]\}\), the score is \(0\), because the point \(3\) is already contained in both planets' interval of vulnerability. For the subset \(\{[1,1], [2,3]\}\), the score is \(1\). By using one operation on changing the interval of vulnerability of the second planet to be \([1,3]\), the two planets now both have the point \(1\) in their interval. For the subset \(\{[1,1], [3,3]\}\), the score is \(2\). By using two operations on changing the interval of vulnerability of the first planet to be \([1,3]\), the two planets now both have the point \(3\) in their interval. For the subset \(\{[1,1], [2,3], [3,3]\}\), the score is \(2\). By using one operation on changing the interval of vulnerability of the first planet to be \([1,2]\) and one operation on changing the interval of vulnerability of the third planet to \([2,3]\), all three planets will have the point \(2\) in their interval. The sum of scores of all non-empty subsets of the first testcase is \(0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5\).
|
Input: 331 12 33 341 42 32 41 151 22 33 44 51 5 | Output: 5 6 24
|
Master
| 3 | 1,147 | 464 | 148 | 20 |
155 |
B
|
155B
|
B. Combination
| 1,100 |
greedy; sortings
|
Ilya plays a card game by the following rules.A player has several cards. Each card contains two non-negative integers inscribed, one at the top of the card and one at the bottom. At the beginning of the round the player chooses one of his cards to play it. If the top of the card contains number ai, and the bottom contains number bi, then when the player is playing the card, he gets ai points and also gets the opportunity to play additional bi cards. After the playing the card is discarded.More formally: let's say that there is a counter of the cards that can be played. At the beginning of the round the counter equals one. When a card is played, the counter decreases by one for the played card and increases by the number bi, which is written at the bottom of the card. Then the played card is discarded. If after that the counter is not equal to zero, the player gets the opportunity to play another card from the remaining cards. The round ends when the counter reaches zero or the player runs out of cards.Of course, Ilya wants to get as many points as possible. Can you determine the maximum number of points he can score provided that you know his cards?
|
The first line contains a single integer n (1 β€ n β€ 1000) β the number of cards Ilya has.Each of the next n lines contains two non-negative space-separated integers β ai and bi (0 β€ ai, bi β€ 104) β the numbers, written at the top and the bottom of the i-th card correspondingly.
|
Print the single number β the maximum number of points you can score in one round by the described rules.
|
In the first sample none of two cards brings extra moves, so you should play the one that will bring more points.In the second sample you should first play the third card that doesn't bring any points but lets you play both remaining cards.
|
Input: 21 02 0 | Output: 2
|
Easy
| 2 | 1,168 | 278 | 105 | 1 |
1,867 |
D
|
1867D
|
D. Cyclic Operations
| 1,800 |
brute force; constructive algorithms; dfs and similar; graphs; greedy; implementation
|
Egor has an array \(a\) of length \(n\), initially consisting of zeros. However, he wanted to turn it into another array \(b\) of length \(n\).Since Egor doesn't take easy paths, only the following operation can be used (possibly zero or several times): choose an array \(l\) of length \(k\) (\(1 \leq l_i \leq n\), all \(l_i\) are distinct) and change each element \(a_{l_i}\) to \(l_{(i\%k)+1}\) (\(1 \leq i \leq k\)). He became interested in whether it is possible to get the array \(b\) using only these operations. Since Egor is still a beginner programmer, he asked you to help him solve this problem.The operation \(\%\) means taking the remainder, that is, \(a\%b\) is equal to the remainder of dividing the number \(a\) by the number \(b\).
|
The first line of the input contains an integer \(t\) (\(1 \leq t \leq 10^5\)) - the number of test cases.Each test case consists of two lines. The first line contains two integers \(n\) and \(k\) (\(1 \leq k \leq n \leq 10^5\)).The second line contains the array \(b_1, b_2, \ldots, b_n\) (\(1 \leq b_i \leq n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output ""YES"" (without quotes) if there is a way to get the array \(b\) using only the given operation. Otherwise, output ""NO"" (without quotes). You can output each letter in any case (lowercase or uppercase). For example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be accepted as a positive answer.
|
Let's consider the first example: Apply the operation with \(l\) = \([1,2,3]\). Now \(a\) = \([2,3,1,0,0]\). Apply the operation with \(l\) = \([3,5,4]\). Now \(a\) = \([2,3,5,3,4]\) = \(b\). We see that it is possible to get the array \(b\). Therefore, the answer is YES.In the second example, it can be proven that the array \(b\) cannot be obtained, therefore the answer is NO.
|
Input: 65 32 3 5 3 44 22 4 3 11 113 11 2 35 35 4 3 2 16 11 2 3 1 5 6 | Output: YES NO YES YES NO NO
|
Medium
| 6 | 749 | 406 | 334 | 18 |
1,436 |
B
|
1436B
|
B. Prime Square
| 900 |
constructive algorithms; math
|
Sasha likes investigating different math objects, for example, magic squares. But Sasha understands that magic squares have already been studied by hundreds of people, so he sees no sense of studying them further. Instead, he invented his own type of square β a prime square. A square of size \(n \times n\) is called prime if the following three conditions are held simultaneously: all numbers on the square are non-negative integers not exceeding \(10^5\); there are no prime numbers in the square; sums of integers in each row and each column are prime numbers. Sasha has an integer \(n\). He asks you to find any prime square of size \(n \times n\). Sasha is absolutely sure such squares exist, so just help him!
|
The first line contains a single integer \(t\) (\(1 \le t \le 10\)) β the number of test cases.Each of the next \(t\) lines contains a single integer \(n\) (\(2 \le n \le 100\)) β the required size of a square.
|
For each test case print \(n\) lines, each containing \(n\) integers β the prime square you built. If there are multiple answers, print any.
|
Input: 2 4 2 | Output: 4 6 8 1 4 9 9 9 4 10 10 65 1 4 4 4 1 1 1 1
|
Beginner
| 2 | 716 | 210 | 140 | 14 |
|
234 |
G
|
234G
|
G. Practice
| 1,600 |
constructive algorithms; divide and conquer; implementation
|
Little time is left before Berland annual football championship. Therefore the coach of team ""Losewille Rangers"" decided to resume the practice, that were indefinitely interrupted for uncertain reasons. Overall there are n players in ""Losewille Rangers"". Each player on the team has a number β a unique integer from 1 to n. To prepare for the championship, the coach Mr. Floppe decided to spend some number of practices.Mr. Floppe spent some long nights of his holiday planning how to conduct the practices. He came to a very complex practice system. Each practice consists of one game, all n players of the team take part in the game. The players are sorted into two teams in some way. In this case, the teams may have different numbers of players, but each team must have at least one player.The coach wants to be sure that after the series of the practice sessions each pair of players had at least one practice, when they played in different teams. As the players' energy is limited, the coach wants to achieve the goal in the least number of practices.Help him to schedule the practices.
|
A single input line contains integer n (2 β€ n β€ 1000).
|
In the first line print m β the minimum number of practices the coach will have to schedule. Then print the descriptions of the practices in m lines.In the i-th of those lines print fi β the number of players in the first team during the i-th practice (1 β€ fi < n), and fi numbers from 1 to n β the numbers of players in the first team. The rest of the players will play in the second team during this practice. Separate numbers on a line with spaces. Print the numbers of the players in any order. If there are multiple optimal solutions, print any of them.
|
Input: 2 | Output: 11 1
|
Medium
| 3 | 1,096 | 54 | 558 | 2 |
|
2,027 |
B
|
2027B
|
B. Stalin Sort
| 1,100 |
brute force; greedy
|
Stalin Sort is a humorous sorting algorithm designed to eliminate elements which are out of place instead of bothering to sort them properly, lending itself to an \(\mathcal{O}(n)\) time complexity.It goes as follows: starting from the second element in the array, if it is strictly smaller than the previous element (ignoring those which have already been deleted), then delete it. Continue iterating through the array until it is sorted in non-decreasing order. For example, the array \([1, 4, 2, 3, 6, 5, 5, 7, 7]\) becomes \([1, 4, 6, 7, 7]\) after a Stalin Sort.We define an array as vulnerable if you can sort it in non-increasing order by repeatedly applying a Stalin Sort to any of its subarrays\(^{\text{β}}\), as many times as is needed.Given an array \(a\) of \(n\) integers, determine the minimum number of integers which must be removed from the array to make it vulnerable.\(^{\text{β}}\)An array \(a\) is a subarray of an array \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. This is followed by descriptions of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2000\)) β 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\)).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 minimum number of integers which must be removed from the array to make it vulnerable.
|
In the first test case, the optimal answer is to remove the numbers \(3\) and \(9\). Then we are left with \(a = [6, 4, 2, 5, 2]\). To show this array is vulnerable, we can first apply a Stalin Sort on the subarray \([4, 2, 5]\) to get \(a = [6, 4, 5, 2]\) and then apply a Stalin Sort on the subarray \([6, 4, 5]\) to get \(a = [6, 2]\), which is non-increasing.In the second test case, the array is already non-increasing, so we don't have to remove any integers.
|
Input: 673 6 4 9 2 5 255 4 4 2 282 2 4 4 6 6 10 101100096 8 9 10 12 9 7 5 47300000000 600000000 400000000 900000000 200000000 400000000 200000000 | Output: 2 0 6 0 4 2
|
Easy
| 2 | 1,122 | 495 | 136 | 20 |
1,656 |
E
|
1656E
|
E. Equal Tree Sums
| 2,200 |
constructive algorithms; dfs and similar; math; trees
|
You are given an undirected unrooted tree, i.e. a connected undirected graph without cycles.You must assign a nonzero integer weight to each vertex so that the following is satisfied: if any vertex of the tree is removed, then each of the remaining connected components has the same sum of weights in its vertices.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(3 \leq n \leq 10^5\)) β the number of vertices of the tree.The next \(n-1\) lines of each case contain each two integers \(u, v\) (\(1 \leq u,v \leq n\)) denoting that there is an edge between vertices \(u\) and \(v\). It is guaranteed that the given edges form a tree.The sum of \(n\) for all test cases is at most \(10^5\).
|
For each test case, you must output one line with \(n\) space separated integers \(a_1, a_2, \ldots, a_n\), where \(a_i\) is the weight assigned to vertex \(i\). The weights must satisfy \(-10^5 \leq a_i \leq 10^5\) and \(a_i \neq 0\).It can be shown that there always exists a solution satisfying these constraints. If there are multiple possible solutions, output any of them.
|
In the first case, when removing vertex \(1\) all remaining connected components have sum \(5\) and when removing vertex \(3\) all remaining connected components have sum \(2\). When removing other vertices, there is only one remaining connected component so all remaining connected components have the same sum.
|
Input: 2 5 1 2 1 3 3 4 3 5 3 1 2 1 3 | Output: -3 5 1 2 2 1 1 1
|
Hard
| 4 | 314 | 568 | 378 | 16 |
296 |
A
|
296A
|
A. Yaroslav and Permutations
| 1,100 |
greedy; math
|
Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring array elements. Now Yaroslav is wondering if he can obtain an array where any two neighboring elements would be distinct in a finite time.Help Yaroslav.
|
The first line contains integer n (1 β€ n β€ 100) β the number of elements in the array. The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 1000) β the array elements.
|
In the single line print ""YES"" (without the quotes) if Yaroslav can obtain the array he needs, and ""NO"" (without the quotes) otherwise.
|
In the first sample the initial array fits well.In the second sample Yaroslav can get array: 1, 2, 1. He can swap the last and the second last elements to obtain it.In the third sample Yarosav can't get the array he needs.
|
Input: 11 | Output: YES
|
Easy
| 2 | 251 | 176 | 139 | 2 |
1,264 |
D2
|
1264D2
|
D2. Beautiful Bracket Sequence (hard version)
| 2,900 |
combinatorics; probabilities
|
This is the hard version of this problem. The only difference is the limit of \(n\) - the length of the input string. In this version, \(1 \leq n \leq 10^6\).Let's define a correct bracket sequence and its depth as follow: An empty string is a correct bracket sequence with depth \(0\). If ""s"" is a correct bracket sequence with depth \(d\) then ""(s)"" is a correct bracket sequence with depth \(d + 1\). If ""s"" and ""t"" are both correct bracket sequences then their concatenation ""st"" is a correct bracket sequence with depth equal to the maximum depth of \(s\) and \(t\). For a (not necessarily correct) bracket sequence \(s\), we define its depth as the maximum depth of any correct bracket sequence induced by removing some characters from \(s\) (possibly zero). For example: the bracket sequence \(s = \)""())(())"" has depth \(2\), because by removing the third character we obtain a correct bracket sequence ""()(())"" with depth \(2\).Given a string \(a\) consists of only characters '(', ')' and '?'. Consider all (not necessarily correct) bracket sequences obtained by replacing all characters '?' in \(a\) by either '(' or ')'. Calculate the sum of all the depths of all these bracket sequences. As this number can be large, find it modulo \(998244353\).Hacks in this problem can be done only if easy and hard versions of this problem was solved.
|
The only line contains a non-empty string consist of only '(', ')' and '?'. The length of the string is at most \(10^6\).
|
Print the answer modulo \(998244353\) in a single line.
|
In the first test case, we can obtain \(4\) bracket sequences by replacing all characters '?' with either '(' or ')': ""(("". Its depth is \(0\); ""))"". Its depth is \(0\); "")("". Its depth is \(0\); ""()"". Its depth is \(1\). So, the answer is \(1 = 0 + 0 + 0 + 1\).In the second test case, we can obtain \(4\) bracket sequences by replacing all characters '?' with either '(' or ')': ""(((())"". Its depth is \(2\); ""()()))"". Its depth is \(2\); ""((()))"". Its depth is \(3\); ""()(())"". Its depth is \(2\). So, the answer is \(9 = 2 + 2 + 3 + 2\).
|
Input: ?? | Output: 1
|
Master
| 2 | 1,365 | 121 | 55 | 12 |
483 |
A
|
483A
|
A. Counterexample
| 1,100 |
brute force; implementation; math; number theory
|
Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one. Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime. You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l β€ a < b < c β€ r. More specifically, you need to find three numbers (a, b, c), such that l β€ a < b < c β€ r, pairs (a, b) and (b, c) are coprime, and pair (a, c) is not coprime.
|
The single line contains two positive space-separated integers l, r (1 β€ l β€ r β€ 1018; r - l β€ 50).
|
Print three positive space-separated integers a, b, c β three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order. If the counterexample does not exist, print the single number -1.
|
In the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are. In the second sample you cannot form a group of three distinct integers, so the answer is -1. In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.
|
Input: 2 4 | Output: 2 3 4
|
Easy
| 4 | 718 | 99 | 302 | 4 |
272 |
A
|
272A
|
A. Dima and Friends
| 1,000 |
implementation; math
|
Dima and his friends have been playing hide and seek at Dima's place all night. As a result, Dima's place got messy. In the morning they decided that they need to clean the place.To decide who exactly would clean the apartment, the friends want to play a counting-out game. First, all the guys stand in a circle, and then each of them shows some number of fingers on one hand (one to five), and then the boys count in a circle, starting from Dima, the number of people, respective to the total number of fingers shown. The person on who the countdown stops will clean the apartment.For example, if Dima and one of his friends played hide and seek, and 7 fingers were shown during the counting-out, then Dima would clean the place. If there were 2 or say, 8 fingers shown, then his friend would clean the place.Dima knows how many fingers each of his friends will show during the counting-out. Now he is interested in the number of ways to show some number of fingers on one hand (one to five), so that he did not have to clean the place. Help Dima.
|
The first line contains integer n (1 β€ n β€ 100) β the number of Dima's friends. Dima himself isn't considered to be his own friend. The second line contains n positive integers, not exceeding 5, representing, how many fingers the Dima's friends will show. The numbers in the lines are separated by a single space.
|
In a single line print the answer to the problem.
|
In the first sample Dima can show 1, 3 or 5 fingers. If Dima shows 3 fingers, then the counting-out will go like that: Dima, his friend, Dima, his friend.In the second sample Dima can show 2 or 4 fingers.
|
Input: 11 | Output: 3
|
Beginner
| 2 | 1,048 | 313 | 49 | 2 |
1,734 |
F
|
1734F
|
F. Zeros and Ones
| 2,500 |
bitmasks; divide and conquer; dp; math
|
Let \(S\) be the Thue-Morse sequence. In other words, \(S\) is the \(0\)-indexed binary string with infinite length that can be constructed as follows: Initially, let \(S\) be ""0"". Then, we perform the following operation infinitely many times: concatenate \(S\) with a copy of itself with flipped bits.For example, here are the first four iterations: Iteration\(S\) before iteration\(S\) before iteration with flipped bitsConcatenated \(S\)1010120110011030110100101101001401101001100101100110100110010110\(\ldots\)\(\ldots\)\(\ldots\)\(\ldots\) You are given two positive integers \(n\) and \(m\). Find the number of positions where the strings \(S_0 S_1 \ldots S_{m-1}\) and \(S_n S_{n + 1} \ldots S_{n + m - 1}\) are different.
|
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. The description of the test cases follows.The first and only line of each test case contains two positive integers, \(n\) and \(m\) respectively (\(1 \leq n,m \leq 10^{18}\)).
|
For each testcase, output a non-negative integer β the Hamming distance between the two required strings.
|
The string \(S\) is equal to 0110100110010110....In the first test case, \(S_0\) is ""0"", and \(S_1\) is ""1"". The Hamming distance between the two strings is \(1\).In the second test case, \(S_0 S_1 \ldots S_9\) is ""0110100110"", and \(S_5 S_6 \ldots S_{14}\) is ""0011001011"". The Hamming distance between the two strings is \(6\).
|
Input: 61 15 1034 21173 3419124639 5634877212073412269 96221437021 | Output: 1 6 95 20 28208137 48102976088
|
Expert
| 4 | 732 | 325 | 105 | 17 |
23 |
B
|
23B
|
B. Party
| 1,600 |
constructive algorithms; graphs; math
|
n people came to a party. Then those, who had no friends among people at the party, left. Then those, who had exactly 1 friend among those who stayed, left as well. Then those, who had exactly 2, 3, ..., n - 1 friends among those who stayed by the moment of their leaving, did the same.What is the maximum amount of people that could stay at the party in the end?
|
The first input line contains one number t β amount of tests (1 β€ t β€ 105). Each of the following t lines contains one integer number n (1 β€ n β€ 105).
|
For each test output in a separate line one number β the maximum amount of people that could stay in the end.
|
Input: 13 | Output: 1
|
Medium
| 3 | 363 | 150 | 109 | 0 |
|
1,372 |
C
|
1372C
|
C. Omkar and Baseball
| 1,500 |
constructive algorithms; math
|
Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across \(n\) sessions follow the identity permutation (ie. in the first game he scores \(1\) point, in the second game he scores \(2\) points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up! Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on \([1,2,3]\) can yield \([3,1,2]\) but it cannot yield \([3,2,1]\) since the \(2\) is in the same position. Given a permutation of \(n\) integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn't exceed \(10^{18}\).An array \(a\) is a subarray of an array \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the length of the given permutation.The second line of each test case contains \(n\) integers \(a_{1},a_{2},...,a_{n}\) (\(1 \leq a_{i} \leq n\)) β the initial permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.
|
In the first permutation, it is already sorted so no exchanges are needed.It can be shown that you need at least \(2\) exchanges to sort the second permutation.\([3, 2, 4, 5, 1, 6, 7]\)Perform special exchange on range (\(1, 5\))\([4, 1, 2, 3, 5, 6, 7]\)Perform special exchange on range (\(1, 4\))\([1, 2, 3, 4, 5, 6, 7]\)
|
Input: 251 2 3 4 573 2 4 5 1 6 7 | Output: 0 2
|
Medium
| 2 | 1,186 | 511 | 111 | 13 |
1,904 |
F
|
1904F
|
F. Beautiful Tree
| 2,800 |
data structures; dfs and similar; graphs; implementation; trees
|
Lunchbox has a tree of size \(n\) rooted at node \(1\). Each node is then assigned a value. Lunchbox considers the tree to be beautiful if each value is distinct and ranges from \(1\) to \(n\). In addition, a beautiful tree must also satisfy \(m\) requirements of \(2\) types: ""1 a b c"" β The node with the smallest value on the path between nodes \(a\) and \(b\) must be located at \(c\). ""2 a b c"" β The node with the largest value on the path between nodes \(a\) and \(b\) must be located at \(c\). Now, you must assign values to each node such that the resulting tree is beautiful. If it is impossible to do so, output \(-1\).
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\)).The next \(n - 1\) lines contain two integers \(u\) and \(v\) (\(1 \le u, v \le n, u \ne v\)) β denoting an edge between nodes \(u\) and \(v\). It is guaranteed that the given edges form a tree.The next \(m\) lines each contain four integers \(t\), \(a\), \(b\), and \(c\) (\(t \in \{1,2\}\), \(1 \le a, b, c \le n\)). It is guaranteed that node \(c\) is on the path between nodes \(a\) and \(b\).
|
If it is impossible to assign values such that the tree is beautiful, output \(-1\). Otherwise, output \(n\) integers, the \(i\)-th of which denotes the value of node \(i\).
|
Input: 7 5 1 2 1 3 1 4 3 5 4 6 3 7 1 6 5 1 2 6 7 3 1 2 7 1 1 7 5 7 2 4 2 2 | Output: 1 6 7 5 3 4 2
|
Master
| 5 | 634 | 484 | 173 | 19 |
|
475 |
E
|
475E
|
E. Strongly Connected City 2
| 2,700 |
dfs and similar
|
Imagine a city with n junctions and m streets. Junctions are numbered from 1 to n.In order to increase the traffic flow, mayor of the city has decided to make each street one-way. This means in the street between junctions u and v, the traffic moves only from u to v or only from v to u. The problem is to direct the traffic flow of streets in a way that maximizes the number of pairs (u, v) where 1 β€ u, v β€ n and it is possible to reach junction v from u by passing the streets in their specified direction. Your task is to find out maximal possible number of such pairs.
|
The first line of input contains integers n and m, (), denoting the number of junctions and streets of the city.Each of the following m lines contains two integers u and v, (u β v), denoting endpoints of a street in the city.Between every two junctions there will be at most one street. It is guaranteed that before mayor decision (when all streets were two-way) it was possible to reach each junction from any other junction.
|
Print the maximal number of pairs (u, v) such that that it is possible to reach junction v from u after directing the streets.
|
In the first sample, if the mayor makes first and second streets one-way towards the junction 1 and third and fourth streets in opposite direction, there would be 13 pairs of reachable junctions: {(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (2, 1), (3, 1), (1, 4), (1, 5), (2, 4), (2, 5), (3, 4), (3, 5)}
|
Input: 5 41 21 31 41 5 | Output: 13
|
Master
| 1 | 573 | 426 | 126 | 4 |
316 |
A2
|
316A2
|
A2. Special Task
| 1,400 |
math
|
Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe.And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string s with the following structure: if si = ""?"", then the digit that goes i-th in the safe code can be anything (between 0 to 9, inclusively); if si is a digit (between 0 to 9, inclusively), then it means that there is digit si on position i in code; if the string contains letters from ""A"" to ""J"", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. The length of the safe code coincides with the length of the hint. For example, hint ""?JGJ9"" has such matching safe code variants: ""51919"", ""55959"", ""12329"", ""93539"" and so on, and has wrong variants such as: ""56669"", ""00111"", ""03539"" and ""13666"".After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint.At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show ""Beavers are on the trail"" on his favorite TV channel, or he should work for a sleepless night...
|
The first line contains string s β the hint to the safe code. String s consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string s doesn't equal to character 0.The input limits for scoring 30 points are (subproblem A1): 1 β€ |s| β€ 5. The input limits for scoring 100 points are (subproblems A1+A2): 1 β€ |s| β€ 105. Here |s| means the length of string s.
|
Print the number of codes that match the given hint.
|
Input: AJ | Output: 81
|
Easy
| 1 | 2,595 | 396 | 52 | 3 |
|
1,799 |
A
|
1799A
|
A. Recent Actions
| 800 |
data structures; greedy; implementation; math
|
On Codeforces the ""Recent Actions"" field shows the last \(n\) posts with recent actions.Initially, there are posts \(1, 2, \ldots, n\) in the field (this is in order from top to down). Also there are infinitely many posts not in the field, numbered with integers \(n + 1, n + 2, \ldots\).When recent action happens in the post \(p\): If it is in the ""Recent Actions"" field, it moves from its position to the top position. Otherwise, it is added to the top position, and the post on the down position is removed from the ""Recent Actions"" field. You know, that the next \(m\) recent actions will happen in the posts \(p_1, p_2, \ldots, p_m\) (\(n + 1 \leq p_i \leq n + m\)) in the moments of time \(1, 2, \ldots, m\). Note, that recent actions only happen with posts with numbers \(\geq n + 1\).For each post \(i\) (\(1 \leq i \leq n\)), find the first time it will be removed from the ""Recent Actions"" field or say, that it won't be removed.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Descriptions of test cases follow.The first line of each test case contains two integers \(n\), \(m\) (\(1 \leq n, m \leq 5 \cdot 10^4\)) β the size of the ""Recent Actions"" field and the number of actions.The next line contains \(m\) integers \(p_1, p_2, \ldots, p_m\) (\(n + 1 \leq p_i \leq n + m\)).It is guaranteed, that the sum of \(n\) and the sum of \(m\) for all test cases does not exceed \(5 \cdot 10^4\).
|
For each test case print \(n\) integers \(t_1, t_2, \ldots, t_n\), where \(t_i=-1\) if the post \(i\) won't be removed or \(t_i\) equals to the first moment of time the post \(i\) will be removed (\(1 \leq t_i \leq m\)).
|
In the first test case, the only post \(1\) will be removed at the moment \(1\) and replaced by the post \(2\).In the second test case the ""Recent Actions"" field will be (given an order from top to down): Before moment \(1\): \([1, 2, 3]\), after moment \(1\): \([5, 1, 2]\). Post number \(3\) was removed. Before moment \(2\): \([5, 1, 2]\), after moment \(2\): \([4, 5, 1]\). Post number \(2\) was removed. Post number \(1\) won't be removed.In the third test case the ""Recent Actions"" field will be (given an order from top to down): Before moment \(1\): \([1, 2, 3, 4]\), after moment \(1\): \([5, 1, 2, 3]\). Post number \(4\) was removed. Before moment \(2\): \([5, 1, 2, 3]\), after moment \(2\): \([9, 5, 1, 2]\). Post number \(3\) was removed. Before moment \(3\): \([9, 5, 1, 2]\), after moment \(3\): \([9, 5, 1, 2]\). Nothing was changed. Before moment \(4\): \([9, 5, 1, 2]\), after moment \(4\): \([5, 9, 1, 2]\). The order was changed. Before moment \(5\): \([5, 9, 1, 2]\), after moment \(5\): \([7, 5, 9, 1]\). Post number \(2\) was removed. Post number \(1\) won't be removed.
|
Input: 101 123 25 44 55 9 9 5 75 56 7 8 9 103 44 4 4 44 45 5 6 63 54 5 5 5 44 205 5 24 24 24 5 6 7 8 9 10 12 13 14 15 16 17 18 19 205 77 8 7 11 7 12 106 78 11 7 8 8 8 12 | Output: 1 -1 2 1 -1 5 2 1 5 4 3 2 1 -1 -1 1 -1 -1 3 1 -1 2 1 8 7 3 1 7 6 4 2 1 -1 -1 7 3 2 1
|
Beginner
| 4 | 948 | 516 | 220 | 17 |
1,970 |
F1
|
1970F1
|
F1. Playing Quidditch (Easy)
| 2,300 |
implementation
|
This afternoon, you decided to enjoy the first days of Spring by taking a walk outside. As you come near the Quidditch field, you hear screams. Once again, there is a conflict about the score: the two teams are convinced that they won the game! To prevent this problem from happening one more time, you decide to get involved in the refereeing of the matches.Now, you will stay in the stadium to watch the game and count the score. At the end of the game, you will decide the winner.Today, two teams are competing: the red Gryffindor (R) and the blue Ravenclaw (B) team. Each team is composed of \(P\) players (\(1 \leq P \leq 10\)).The field is a rectangle of \(N\) lines and \(M\) columns (\(3 \leq N, M \leq 99\), \(N\) and \(M\) are odd). All the positions are integers, and several entities are allowed to be at the same position in the field. At the beginning of the game, the field contains goals for the two teams (each team can own between one and five goals), the players, and exactly one Quaffle. Other types of balls will be available in the harder versions of the problem.A game is composed of \(T\) steps (\(0 \leq T \leq 10000\)). At each step, one entity on the field (a player or a ball) performs one action. All entities can move. A player can also catch a ball or throw the Quaffle that it is carrying. To catch a ball, a player must be located on the same cell as it. The Quaffle does not perform any action while it is being carried; it only follows the movements of the player. If a player carrying the Quaffle decides to throw it, the Quaffle is simply put at the current position of the player.To win a point, a player must leave the Quaffle at a goal of the other team. When it does, the team of the player wins one point, and the Quaffle instantly moves to the middle of the field (the cell at the \((M+1)/2\)-th column of the \((N+1)/2\)-th line of the field, starting from 1). There is no goal in the middle of the field. If a player puts the ball in its own goal, the other team wins the point.
|
On the first line, the integers \(N\) and \(M\).The description of the field follows: \(N\) lines of \(M\) pairs of characters separated by spaces. Each pair of characters represents a position on the field. It can be either: .. to represent an empty cell R0, ..., R9, B0, ..., B9 to represent a player. The first character is the team of the player, and the second is the number of the player in the team. Each pair of characters is unique, but it is not guaranteed that all the pairs appear in the grid. RG or BG to represent a goal. The blue team tries to put the ball in a red goal (RG) while the red team tries to put the ball in a blue goal (BG). .Q to represent the Quaffle, which is the ball that the players use to score goals. The next line contains \(T\), the number of steps that compose the game. \(T\) lines follow, each describing one action. It contains several pieces of information separated by a space. First, a pair of characters representing the entity that must perform the action. Second, the description of the action: U, D, L, R indicate that the entity moves on the grid. It can move to the top of the grid (U), to the bottom (D), to the left (L), or to the right (R). Each entity moves by only one cell at a time. C indicates that the player catches the ball (only a player can catch a ball). Then, there is a space followed by a pair of characters: the description of the ball caught by the player. This information is needed since in the next versions of the problem, several balls may be in the same cell. T indicates that the player throws the Quaffle that it is carrying. All the actions performed by the entities are guaranteed to be valid: the players stay in the field, don't catch a ball if they are not in the same cell, don't release the Quaffle if they are not carrying it, ...
|
You must output the description of the main events of the game, one event per line. More precisely: Each time a team scores, you must print t RED GOAL or t BLUE GOAL, depending on the team who scored, where t is the current time (the position of the action in the list of actions, starting from 0). In the case where a player scores in the wrong goal (a red player scores in the red goal, or a blue player scores in the blue goal), you must print the name of the team who wins one point, that is, the other team. The events must be printed in ascending order of t.At the end of the game, you must print the final score as: FINAL SCORE: r b, where r is the score of the red team and b is the score of the blue team.
|
In the first example, a blue player goes to the middle of the grid, at the position of the Quaffle. He takes it, goes to the goal of the red team, and lets the ball there. He scores! The ball gets back to the middle of the field, a red player takes it and scores too.In the second example, the red player takes the Quaffle, move it and throw it. The blue player catches the ball, goes to the red goal and scores.In the third example, the red player takes the ball and scores in the goal of their own team: the blue team wins a point.
|
Input: 5 5.. R1 .. B1 ..RG .. .. .. BGRG R0 .Q B0 BGRG .. .. .. BG.. R2 .. B2 ..13B2 UB2 UB2 LB2 C .QB2 LB2 LB2 TR0 RR0 C .QR0 DR0 RR0 RR0 T | Output: 6 BLUE GOAL 12 RED GOAL FINAL SCORE: 1 1
|
Expert
| 1 | 2,023 | 1,816 | 714 | 19 |
568 |
B
|
568B
|
B. Symmetric and Transitive
| 1,900 |
combinatorics; dp; math
|
Little Johnny has recently learned about set theory. Now he is studying binary relations. You've probably heard the term ""equivalence relation"". These relations are very important in many areas of mathematics. For example, the equality of the two numbers is an equivalence relation.A set Ο of pairs (a, b) of elements of some set A is called a binary relation on set A. For two elements a and b of the set A we say that they are in relation Ο, if pair , in this case we use a notation .Binary relation is equivalence relation, if: It is reflexive (for any a it is true that ); It is symmetric (for any a, b it is true that if , then ); It is transitive (if and , than ).Little Johnny is not completely a fool and he noticed that the first condition is not necessary! Here is his ""proof"":Take any two elements, a and b. If , then (according to property (2)), which means (according to property (3)).It's very simple, isn't it? However, you noticed that Johnny's ""proof"" is wrong, and decided to show him a lot of examples that prove him wrong.Here's your task: count the number of binary relations over a set of size n such that they are symmetric, transitive, but not an equivalence relations (i.e. they are not reflexive).Since their number may be very large (not 0, according to Little Johnny), print the remainder of integer division of this number by 109 + 7.
|
A single line contains a single integer n (1 β€ n β€ 4000).
|
In a single line print the answer to the problem modulo 109 + 7.
|
If n = 1 there is only one such relation β an empty one, i.e. . In other words, for a single element x of set A the following is hold: .If n = 2 there are three such relations. Let's assume that set A consists of two elements, x and y. Then the valid relations are , Ο = {(x, x)}, Ο = {(y, y)}. It is easy to see that the three listed binary relations are symmetric and transitive relations, but they are not equivalence relations.
|
Input: 1 | Output: 1
|
Hard
| 3 | 1,369 | 57 | 64 | 5 |
2,059 |
E2
|
2059E2
|
E2. Stop Gaming (Hard Version)
| 2,900 |
brute force; constructive algorithms; data structures; hashing; strings
|
This is the hard version of the problem. The difference between the versions is that in this version you need to output all the operations that need to be performed. You can hack only if you solved all versions of this problem.You are given \(n\) arrays, each of which has a length of \(m\). Let the \(j\)-th element of the \(i\)-th array be denoted as \(a_{i, j}\). It is guaranteed that all \(a_{i, j}\) are pairwise distinct. In one operation, you can do the following: Choose some integer \(i\) (\(1 \le i \le n\)) and an integer \(x\) (\(1 \le x \le 2 \cdot n \cdot m\)). For all integers \(k\) from \(i\) to \(n\) in increasing order, do the following: Add the element \(x\) to the beginning of the \(k\)-th array. Assign \(x\) the value of the last element in the \(k\)-th array. Remove the last element from the \(k\)-th array. In other words, you can insert an element at the beginning of any array, after which all elements in this and all following arrays are shifted by one to the right. The last element of the last array is removed.You are also given a description of the arrays that need to be obtained after all operations. That is, after performing the operations, the \(j\)-th element of the \(i\)-th array should be equal to \(b_{i, j}\). It is guaranteed that all \(b_{i, j}\) are pairwise distinct.Determine the minimum number of operations that need to be performed to obtain the desired arrays, and also output the sequence of all operations itself.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\)) β the number of arrays and the number of elements in each array.The \(i\)-th of the following \(n\) lines contains \(m\) integers \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}\) (\(1 \le a_{i, j} \le 2 \cdot n \cdot m\)) β the elements of the \(i\)-th original array. It is guaranteed that all \(a_{i, j}\) are pairwise distinct.The \(i\)-th of the following \(n\) lines contains \(m\) integers \(b_{i, 1}, b_{i, 2}, \ldots, b_{i, m}\) (\(1 \le b_{i, j} \le 2 \cdot n \cdot m\)) β the elements of the \(i\)-th final array. It is guaranteed that all \(b_{i, j}\) are pairwise distinct.It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, output a single integer β the minimum number of operations that need to be performed.Next, for each operation output two integers \(i\) and \(x\) (\(1 \le i \le n\), \(1 \le x \le 2 \cdot n \cdot m\)) β the index of the array where the element is inserted and the value of the element, respectively.If there are multiple possible sequences with the minimum number of operations, output any of them.
|
In the first test case, the following sequence of \(3\) operations is suitable: Apply the operation to the first array with \(x = 1\). Then the element \(1\) will be added to the beginning of the first array, and the value of \(x\) will become \(6\). The last element will be removed, and the first array will look like \([1, 2]\). Next, the element \(x\) is added to the beginning of the second array, and the value of \(x\) becomes \(4\). The last element of the second array is removed, and both arrays look like \([1, 2]\) and \([6, 3]\) respectively after the first operation. Apply the operation to the second array with \(x = 8\). Then the first array remains unchanged, and both arrays will look like \([1, 2]\) and \([8, 6]\) respectively. Apply the operation to the second array with \(x = 7\), then both arrays will have the required appearance \([1, 2]\) and \([7, 8]\) respectively. In the second test case, the desired array can only be achieved in \(5\) operations.In the third test case, the following sequence of \(3\) operations is suitable: Apply the operation with \(x = 11\) to the first array. Apply the operation with \(x = 12\) to the second array. Apply the operation with \(x = 13\) to the third array.
|
Input: 42 22 63 41 27 81 55 4 1 2 35 4 3 2 13 31 2 34 5 67 8 911 1 212 3 413 5 64 41 2 3 45 6 7 89 10 11 1213 14 15 1617 1 2 34 18 5 67 19 8 209 21 22 10 | Output: 3 1 1 2 8 2 7 5 1 1 1 2 1 3 1 4 1 5 3 1 11 2 12 3 13 6 3 20 2 18 3 19 4 22 4 21 1 17
|
Master
| 5 | 1,472 | 965 | 418 | 20 |
1,151 |
C
|
1151C
|
C. Problem for Nazar
| 1,800 |
constructive algorithms; math
|
Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.Consider two infinite sets of numbers. The first set consists of odd positive numbers (\(1, 3, 5, 7, \ldots\)), and the second set consists of even positive numbers (\(2, 4, 6, 8, \ldots\)). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage β the first two numbers from the second set, on the third stage β the next four numbers from the first set, on the fourth β the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another. The ten first written numbers: \(1, 2, 4, 3, 5, 7, 9, 6, 8, 10\). Let's number the numbers written, starting with one.The task is to find the sum of numbers with numbers from \(l\) to \(r\) for given integers \(l\) and \(r\). The answer may be big, so you need to find the remainder of the division by \(1000000007\) (\(10^9+7\)).Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.
|
The first line contains two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq 10^{18}\)) β the range in which you need to find the sum.
|
Print a single integer β the answer modulo \(1000000007\) (\(10^9+7\)).
|
In the first example, the answer is the sum of the first three numbers written out (\(1 + 2 + 4 = 7\)).In the second example, the numbers with numbers from \(5\) to \(14\): \(5, 7, 9, 6, 8, 10, 12, 14, 16, 18\). Their sum is \(105\).
|
Input: 1 3 | Output: 7
|
Medium
| 2 | 1,308 | 134 | 71 | 11 |
472 |
E
|
472E
|
E. Design Tutorial: Learn from a Game
| 2,800 |
constructive algorithms; implementation
|
One way to create task is to learn from game. You should pick a game and focus on part of the mechanic of that game, then it might be a good task.Let's have a try. Puzzle and Dragon was a popular game in Japan, we focus on the puzzle part of that game, it is a tile-matching puzzle.(Picture from Wikipedia page: http://en.wikipedia.org/wiki/Puzzle_&_Dragons)There is an n Γ m board which consists of orbs. During the game you can do the following move. In the beginning of move you touch a cell of the board, then you can move your finger to one of the adjacent cells (a cell not on the boundary has 8 adjacent cells), then you can move your finger from the current cell to one of the adjacent cells one more time, and so on. Each time you move your finger from a cell to another cell, the orbs in these cells swap with each other. In other words whatever move you make, the orb in the cell you are touching never changes.The goal is to achieve such kind of pattern that the orbs will be cancelled and your monster will attack the enemy, but we don't care about these details. Instead, we will give you the initial board as an input and the target board as an output. Your goal is to determine whether there is a way to reach the target in a single move.
|
The first line contains two integers: n and m (1 β€ n, m β€ 30).The next n lines each contains m integers β the description of the initial board. The j-th integer in the i-th line is si, j (1 β€ si, j β€ 900), where si, j denotes the type of the orb located in the i-th row and j-th column of the board.The next n lines contain the target board in the same format. Note, that the initial board and the target board will be different.
|
If there is no solution, then output: -1.If there is a solution, then in the first line output an integer k (1 β€ k β€ 106) β the number of finger moves.In the next line print two integers x0 and y0 (1 β€ x0 β€ n; 1 β€ y0 β€ m) β the position of the cell you touch at the beginning. In each of the next k lines print two integers xi and yi (1 β€ xi β€ n; 1 β€ yi β€ m) β the position you move to. Note that this position must be adjacent to the previous position, that is max(|xi - xi - 1|, |yi - yi - 1|) = 1.If there are multiple solutions, you can print any of them. We can prove that under these constraints if there exists a solution then there is a solution with no more than 106 operations.
|
Input: 2 21 32 31 33 2 | Output: 31 12 22 11 1
|
Master
| 2 | 1,254 | 429 | 687 | 4 |
|
397 |
A
|
397A
|
A. On Segment's Own Points
| 0 |
implementation
|
Our old friend Alexey has finally entered the University of City N β the Berland capital. Alexey expected his father to get him a place to live in but his father said it was high time for Alexey to practice some financial independence. So, Alexey is living in a dorm. The dorm has exactly one straight dryer β a 100 centimeter long rope to hang clothes on. The dryer has got a coordinate system installed: the leftmost end of the dryer has coordinate 0, and the opposite end has coordinate 100. Overall, the university has n students. Dean's office allows i-th student to use the segment (li, ri) of the dryer. However, the dean's office actions are contradictory and now one part of the dryer can belong to multiple students!Alexey don't like when someone touch his clothes. That's why he want make it impossible to someone clothes touch his ones. So Alexey wonders: what is the total length of the parts of the dryer that he may use in a such way that clothes of the others (n - 1) students aren't drying there. Help him! Note that Alexey, as the most respected student, has number 1.
|
The first line contains a positive integer n (1 β€ n β€ 100). The (i + 1)-th line contains integers li and ri (0 β€ li < ri β€ 100) β the endpoints of the corresponding segment for the i-th student.
|
On a single line print a single number k, equal to the sum of lengths of the parts of the dryer which are inside Alexey's segment and are outside all other segments.
|
Note that it's not important are clothes drying on the touching segments (e.g. (0, 1) and (1, 2)) considered to be touching or not because you need to find the length of segments.In the first test sample Alexey may use the only segment (0, 1). In such case his clothes will not touch clothes on the segments (1, 6) and (2, 8). The length of segment (0, 1) is 1.In the second test sample Alexey may dry his clothes on segments (0, 1) and (5, 7). Overall length of these segments is 3.
|
Input: 30 52 81 6 | Output: 1
|
Beginner
| 1 | 1,086 | 194 | 165 | 3 |
1,163 |
A
|
1163A
|
A. Eating Soup
| 900 |
greedy; math
|
The three friends, Kuro, Shiro, and Katie, met up again! It's time for a party...What the cats do when they unite? Right, they have a party. Since they wanted to have as much fun as possible, they invited all their friends. Now \(n\) cats are at the party, sitting in a circle and eating soup. The rules are simple: anyone having finished their soup leaves the circle.Katie suddenly notices that whenever a cat leaves, the place where she was sitting becomes an empty space, which means the circle is divided into smaller continuous groups of cats sitting next to each other. At the moment Katie observes, there are \(m\) cats who left the circle. This raises a question for Katie: what is the maximum possible number of groups the circle is divided into at the moment?Could you help her with this curiosity?You can see the examples and their descriptions with pictures in the ""Note"" section.
|
The only line contains two integers \(n\) and \(m\) (\(2 \leq n \leq 1000\), \(0 \leq m \leq n\)) β the initial number of cats at the party and the number of cats who left the circle at the moment Katie observes, respectively.
|
Print a single integer β the maximum number of groups of cats at the moment Katie observes.
|
In the first example, originally there are \(7\) cats sitting as shown below, creating a single group: At the observed moment, \(4\) cats have left the table. Suppose the cats \(2\), \(3\), \(5\) and \(7\) have left, then there are \(3\) groups remaining. It is possible to show that it is the maximum possible number of groups remaining. In the second example, there are \(6\) cats sitting as shown below: At the observed moment, \(2\) cats have left the table. Suppose the cats numbered \(3\) and \(6\) left, then there will be \(2\) groups remaining (\(\{1, 2\}\) and \(\{4, 5\}\)). It is impossible to have more than \(2\) groups of cats remaining. In the third example, no cats have left, so there is \(1\) group consisting of all cats.In the fourth example, all cats have left the circle, so there are \(0\) groups.
|
Input: 7 4 | Output: 3
|
Beginner
| 2 | 894 | 226 | 91 | 11 |
1,403 |
A
|
1403A
|
A. The Potion of Great Power
| 2,400 |
*special; 2-sat; binary search; data structures; graphs; interactive; sortings; two pointers
|
Once upon a time, in the Land of the Shamans, everyone lived on the Sky-High Beanstalk. Each shaman had a unique identifying number \(i\) between \(0\) and \(N-1\), and an altitude value \(H_i\), representing how high he lived above ground level. The distance between two altitudes is the absolute value of their difference.All shamans lived together in peace, until one of them stole the formula of the world-famous Potion of Great Power. To cover his/her tracks, the Thief has put a Curse on the land: most inhabitants could no longer trust each other...Despite the very difficult circumstances, the Order of Good Investigators have gained the following information about the Curse: When the Curse first takes effect, everyone stops trusting each other. The Curse is unstable: at the end of each day (exactly at midnight), one pair of shamans will start or stop trusting each other. Unfortunately, each shaman will only ever trust at most \(D\) others at any given time. They have also reconstructed a log of who trusted whom: for each night they know which pair of shamans started/stopped trusting each other.They believe the Thief has whispered the formula to an Evil Shaman. To avoid detection, both of them visited the home of one of their (respective) trusted friends. During the visit, the Thief whispered the formula to the Evil Shaman through the window. (Note: this trusted friend did not have to be home at the time. In fact, it's even possible that they visited each other's houses β shamans are weird.)Fortunately, whispers only travel short distances, so the Order knows the two trusted friends visited (by the Thief and the Evil Shaman) must live very close to each other.They ask you to help with their investigation. They would like to test their suspicions: what if the Thief was \(x\), the Evil Shaman was \(y\), and the formula was whispered on day \(v\)? What is the smallest distance the whispered formula had to travel? That is, what is the minimum distance between the apartments of some shamans \(x'\) and \(y'\) (i.e. \(\min\left(\left|H_{x'} - H_{y'}\right|\right)\)), such that \(x'\) was a trusted friend of \(x\) and \(y'\) was a trusted friend of \(y\) on day \(v\)?They will share all their information with you, then ask you a number of questions. You need to answer each question immediately, before receiving the next one.
|
Example queries: Evolution of friendships:
|
Input: 6 5 11 4 2 42 1000 54 68 234 0 1 2 0 3 4 3 5 3 5 1 3 5 3 0 5 3 0 1 3 3 5 0 3 4 3 0 8 0 5 5 3 0 11 | Output: 26 0 1000000000 14
|
Expert
| 8 | 2,358 | 0 | 0 | 14 |
||
1,780 |
B
|
1780B
|
B. GCD Partition
| 1,100 |
brute force; greedy; math; number theory
|
While at Kira's house, Josuke saw a piece of paper on the table with a task written on it.The task sounded as follows. There is an array \(a\) of length \(n\). On this array, do the following: select an integer \(k > 1\); split the array into \(k\) subsegments \(^\dagger\); calculate the sum in each of \(k\) subsegments and write these sums to another array \(b\) (where the sum of the subsegment \((l, r)\) is \({\sum_{j = l}^{r}a_j}\)); the final score of such a split will be \(\gcd(b_1, b_2, \ldots, b_k)^\ddagger\). The task is to find such a partition that the score is maximum possible. Josuke is interested in this task but is not strong in computer science. Help him to find the maximum possible score.\(^\dagger\) A division of an array into \(k\) subsegments is \(k\) pairs of numbers \((l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k)\) such that \(l_i \le r_i\) and for every \(1 \le j \le k - 1\) \(l_{j + 1} = r_j + 1\), also \(l_1 = 1\) and \(r_k = n\). These pairs represent the subsegments.\(^\ddagger\) \(\gcd(b_1, b_2, \ldots, b_k)\) stands for the greatest common divisor (GCD) of the array \(b\).
|
The first line contains a single number \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.For each test case, the first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, a_3, \ldots, a_n\) (\(1 \le a_i \le 10^9 \)) β the array \(a\) itself.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 integer β the maximum score for the optimal partition.
|
In the first test case, you can choose \(k = 2\) and split the array into subsegments \((1, 2)\) and \((3, 4)\).Then the score of such a partition will be equal to \(\gcd(a_1 + a_2, a_3 + a_4) = \gcd(2 + 2, 1 + 3) = \gcd(4, 4) = 4\).In the fourth test case, you can choose \(k = 3\) and split the array into subsegments \((1, 2), (3, 5), (6, 6)\).The split score is \(\gcd(1 + 2, 1 + 1 + 1, 3) = 3\).
|
Input: 642 2 1 321 231 4 561 2 1 1 1 31012 30 37 88 12 78 89 17 2 1267 7 7 7 7 7 | Output: 4 1 5 3 1 21
|
Easy
| 4 | 1,114 | 435 | 88 | 17 |
2,124 |
C
|
2124C
|
C. Subset Multiplication
| 1,300 |
constructive algorithms; greedy; math; number theory
|
Alice has an array \(a\), consisting of \(n\) positive integers. The array satisfies the beautiful property that \(a_i\) divides \(a_{i+1}\) for each \(1 \leq i \leq n - 1\). Bob sees Alice's beautiful array and is jealous. To sabotage her, Bob first creates an array \(b\) of size \(n\) such that \(b_i=a_i\) for each \(1 \leq i \leq n\). Then, he chooses a positive integer \(x\) and multiplies some (possibly none, possibly all) elements in \(b\) by \(x\). Formally, he chooses a (possibly-empty) subset \(S\subseteq\{1,2,\ldots,n\}\), and for each \(i\in S\), he sets \(b_i:=b_i\cdot x\). You are given an array \(b\), but you don't know array \(a\) and the chosen number \(x\). Please output any integer \(x\) that Bob could choose, so that multiplying some subset of elements of the correct array \(a\) by \(x\) would result in array \(b\). It is guaranteed that the answer exists. If there are multiple possible integers, you can output any of them.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot10^5\)). The description of the test cases follows. The first line of each test case contains an integer \(n\) (\(2 \leq n \leq 6\cdot10^5\)) β the length of the array \(b\).The second line of each test case contains \(n\) integers \(b_1,b_2,\ldots,b_n\) (\(1 \leq b_i \leq 10^9\)) β denoting the array \(b\).It is guaranteed the array \(b\) can be obtained from some beautiful array \(a\) and some positive integer \(x\) as described in the statements.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(6\cdot 10^5\).
|
For each test case, output any possible value of \(x\) (\(1 \leq x \leq 10^9\)) on a new line. It is guaranteed at least one value of \(x\) exists.
|
In the first test case, it is possible Bob selected \(x=343\) and \(S=\{\}\) (meaning he did not change the array \(a\) at all). In the third test case, it is possible Bob selected \(x=4\) and \(S=\{1,2\}\), meaning he multiplied both \(b_1\) and \(b_2\) by \(4\). The original array was \(\{1,2,4,8\}\), which satisfies the required property.
|
Input: 422 431 1000000000 50000000044 8 4 8742 42 14 84 28 73080 255780 | Output: 343 2 4 6
|
Easy
| 4 | 956 | 660 | 147 | 21 |
1,304 |
F1
|
1304F1
|
F1. Animal Observation (easy version)
| 2,300 |
data structures; dp
|
The only difference between easy and hard versions is the constraint on \(k\).Gildong loves observing animals, so he bought two cameras to take videos of wild animals in a forest. The color of one camera is red, and the other one's color is blue.Gildong is going to take videos for \(n\) days, starting from day \(1\) to day \(n\). The forest can be divided into \(m\) areas, numbered from \(1\) to \(m\). He'll use the cameras in the following way: On every odd day (\(1\)-st, \(3\)-rd, \(5\)-th, ...), bring the red camera to the forest and record a video for \(2\) days. On every even day (\(2\)-nd, \(4\)-th, \(6\)-th, ...), bring the blue camera to the forest and record a video for \(2\) days. If he starts recording on the \(n\)-th day with one of the cameras, the camera records for only one day. Each camera can observe \(k\) consecutive areas of the forest. For example, if \(m=5\) and \(k=3\), he can put a camera to observe one of these three ranges of areas for two days: \([1,3]\), \([2,4]\), and \([3,5]\).Gildong got information about how many animals will be seen in each area each day. Since he would like to observe as many animals as possible, he wants you to find the best way to place the two cameras for \(n\) days. Note that if the two cameras are observing the same area on the same day, the animals observed in that area are counted only once.
|
The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 50\), \(1 \le m \le 2 \cdot 10^4\), \(1 \le k \le min(m,20)\)) β the number of days Gildong is going to record, the number of areas of the forest, and the range of the cameras, respectively.Next \(n\) lines contain \(m\) integers each. The \(j\)-th integer in the \(i+1\)-st line is the number of animals that can be seen on the \(i\)-th day in the \(j\)-th area. Each number of animals is between \(0\) and \(1000\), inclusive.
|
Print one integer β the maximum number of animals that can be observed.
|
The optimal way to observe animals in the four examples are as follows:Example 1: Example 2: Example 3: Example 4:
|
Input: 4 5 2 0 2 1 1 0 0 0 3 1 2 1 0 4 3 1 3 3 0 0 4 | Output: 25
|
Expert
| 2 | 1,369 | 506 | 71 | 13 |
145 |
E
|
145E
|
E. Lucky Queries
| 2,400 |
data structures
|
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya brought home string s with the length of n. The string only consists of lucky digits. The digits are numbered from the left to the right starting with 1. Now Petya should execute m queries of the following form: switch l r β ""switch"" digits (i.e. replace them with their opposites) at all positions with indexes from l to r, inclusive: each digit 4 is replaced with 7 and each digit 7 is replaced with 4 (1 β€ l β€ r β€ n); count β find and print on the screen the length of the longest non-decreasing subsequence of string s. Subsequence of a string s is a string that can be obtained from s by removing zero or more of its elements. A string is called non-decreasing if each successive digit is not less than the previous one.Help Petya process the requests.
|
The first line contains two integers n and m (1 β€ n β€ 106, 1 β€ m β€ 3Β·105) β the length of the string s and the number of queries correspondingly. The second line contains n lucky digits without spaces β Petya's initial string. Next m lines contain queries in the form described in the statement.
|
For each query count print an answer on a single line.
|
In the first sample the chronology of string s after some operations are fulfilled is as follows (the sought maximum subsequence is marked with bold): 47 74 74 In the second sample: 747 447 447 774 774
|
Input: 2 347countswitch 1 2count | Output: 21
|
Expert
| 1 | 985 | 295 | 54 | 1 |
615 |
B
|
615B
|
B. Longtail Hedgehog
| 1,600 |
dp; graphs
|
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of n points connected by m segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: Only segments already presented on the picture can be painted; The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.
|
First line of the input contains two integers n and m(2 β€ n β€ 100 000, 1 β€ m β€ 200 000) β the number of points and the number segments on the picture respectively. Then follow m lines, each containing two integers ui and vi (1 β€ ui, vi β€ n, ui β vi) β the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.
|
Print the maximum possible value of the hedgehog's beauty.
|
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3Β·3 = 9.
|
Input: 8 64 53 52 51 22 86 7 | Output: 9
|
Medium
| 2 | 1,390 | 379 | 58 | 6 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.