contest_id
int32 1
2.13k
| index
stringclasses 62
values | problem_id
stringlengths 2
6
| title
stringlengths 0
67
| rating
int32 0
3.5k
| tags
stringlengths 0
139
| statement
stringlengths 0
6.96k
| input_spec
stringlengths 0
2.32k
| output_spec
stringlengths 0
1.52k
| note
stringlengths 0
5.06k
| sample_tests
stringlengths 0
1.02k
| difficulty_category
stringclasses 6
values | tag_count
int8 0
11
| statement_length
int32 0
6.96k
| input_spec_length
int16 0
2.32k
| output_spec_length
int16 0
1.52k
| contest_year
int16 0
21
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,575 |
G
|
1575G
|
G. GCD Festival
| 2,200 |
math; number theory
|
Mr. Chanek has an array \(a\) of \(n\) integers. The prettiness value of \(a\) is denoted as:$$$\(\sum_{i=1}^{n} {\sum_{j=1}^{n} {\gcd(a_i, a_j) \cdot \gcd(i, j)}}\)\(where \)\gcd(x, y)\( denotes the greatest common divisor (GCD) of integers \)x\( and \)y\(.In other words, the prettiness value of an array \)a\( is the total sum of \)\gcd(a_i, a_j) \cdot \gcd(i, j)\( for all pairs \)(i, j)\(.Help Mr. Chanek find the prettiness value of \)a\(, and output the result modulo \)10^9 + 7$$$!
|
The first line contains an integer \(n\) (\(2 \leq n \leq 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^5\)).
|
Output an integer denoting the prettiness value of \(a\) modulo \(10^9 + 7\).
|
Input: 5 3 6 2 1 4 | Output: 77
|
Hard
| 2 | 489 | 159 | 77 | 15 |
|
1,910 |
F
|
1910F
|
F. Build Railway Stations
| 2,000 |
*special; greedy; trees
|
Monocarp is playing a computer game where he's controlling an empire. An empire consists of \(n\) cities, connected by \(n - 1\) roads. The cities are numbered from \(1\) to \(n\). It's possible to reach every city from every other one using the roads.Traversing every road takes \(2\) hours. However, that can be improved. Monocarp can build railway stations in no more than \(k\) cities. After they are built, all existing roads that connect two cities with railway stations get converted to railroads and become \(1\) hour to traverse.Let \(f(x, y)\) be the total time it takes to traverse the roads on the shortest path between cities \(x\) and \(y\).Monocarp wants to build at most \(k\) railway stations in such a way that the following value is minimized: \(\sum \limits_{v=1}^{n} \sum \limits_{u=1}^{v-1} f(v, u)\) (the total time it takes to travel from every city to every other one). What the smallest value he can achieve?
|
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 two integers \(n\) and \(k\) (\(2 \le k \le n \le 2 \cdot 10^5\)) β the number of cities and the maximum number of railway stations Monocarp can build.Each of the following \(n-1\) lines contains two integers \(v\) and \(u\) (\(1 \le v, u \le n\); \(v \neq u\)) β a road that connects cities \(v\) and \(u\).It's possible to reach every city from every other one using the roads. The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
For each testcase, print a single integer β the smallest total time it takes to travel from every city to every other one that Monocarp can achieve after building at most \(k\) railway stations.
|
Input: 35 21 22 33 44 54 41 21 31 45 31 21 32 42 5 | Output: 34 9 26
|
Hard
| 3 | 934 | 585 | 194 | 19 |
|
431 |
B
|
431B
|
B. Shower Line
| 1,200 |
brute force; implementation
|
Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks. There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one. Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.
|
The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 β€ gij β€ 105). It is guaranteed that gii = 0 for all i.Assume that the students are numbered from 1 to 5.
|
Print a single integer β the maximum possible total happiness of the students.
|
In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:(g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.
|
Input: 0 0 0 0 90 0 0 0 00 0 0 0 00 0 0 0 07 0 0 0 0 | Output: 32
|
Easy
| 2 | 1,793 | 234 | 78 | 4 |
1,100 |
F
|
1100F
|
F. Ivan and Burgers
| 2,500 |
data structures; divide and conquer; greedy; math
|
Ivan loves burgers and spending money. There are \(n\) burger joints on the street where Ivan lives. Ivan has \(q\) friends, and the \(i\)-th friend suggested to meet at the joint \(l_i\) and walk to the joint \(r_i\) \((l_i \leq r_i)\). While strolling with the \(i\)-th friend Ivan can visit all joints \(x\) which satisfy \(l_i \leq x \leq r_i\).For each joint Ivan knows the cost of the most expensive burger in it, it costs \(c_i\) burles. Ivan wants to visit some subset of joints on his way, in each of them he will buy the most expensive burger and spend the most money. But there is a small issue: his card broke and instead of charging him for purchases, the amount of money on it changes as follows.If Ivan had \(d\) burles before the purchase and he spent \(c\) burles at the joint, then after the purchase he would have \(d \oplus c\) burles, where \(\oplus\) denotes the bitwise XOR operation.Currently Ivan has \(2^{2^{100}} - 1\) burles and he wants to go out for a walk. Help him to determine the maximal amount of burles he can spend if he goes for a walk with the friend \(i\). The amount of burles he spends is defined as the difference between the initial amount on his account and the final account.
|
The first line contains one integer \(n\) (\(1 \leq n \leq 500\,000\)) β the number of burger shops.The next line contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(0 \leq c_i \leq 10^6\)), where \(c_i\) β the cost of the most expensive burger in the burger joint \(i\).The third line contains one integer \(q\) (\(1 \leq q \leq 500\,000\)) β the number of Ivan's friends.Each of the next \(q\) lines contain two integers \(l_i\) and \(r_i\) (\(1 \leq l_i \leq r_i \leq n\)) β pairs of numbers of burger shops between which Ivan will walk.
|
Output \(q\) lines, \(i\)-th of which containing the maximum amount of money Ivan can spend with the friend \(i\).
|
In the first test, in order to spend the maximum amount of money with the first and third friends, Ivan just needs to go into the first burger. With a second friend, Ivan just go to the third burger.In the second test for a third friend (who is going to walk from the first to the third burger), there are only 8 options to spend money β \(0\), \(12\), \(14\), \(23\), \(12 \oplus 14 = 2\), \(14 \oplus 23 = 25\), \(12 \oplus 23 = 27\), \(12 \oplus 14 \oplus 23 = 20\). The maximum amount of money it turns out to spend, if you go to the first and third burger β \(12 \oplus 23 = 27\).
|
Input: 47 2 3 431 42 31 3 | Output: 737
|
Expert
| 4 | 1,221 | 541 | 114 | 11 |
1,817 |
A
|
1817A
|
A. Almost Increasing Subsequence
| 1,500 |
binary search; data structures; greedy
|
A sequence is almost-increasing if it does not contain three consecutive elements \(x, y, z\) such that \(x\ge y\ge z\).You are given an array \(a_1, a_2, \dots, a_n\) and \(q\) queries.Each query consists of two integers \(1\le l\le r\le n\). For each query, find the length of the longest almost-increasing subsequence of the subarray \(a_l, a_{l+1}, \dots, a_r\). A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.
|
The first line of input contains two integers, \(n\) and \(q\) (\(1 \leq n, q \leq 200\,000\)) β the length of the array \(a\) and the number of queries.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \leq a_i \leq 10^9\)) β the values of the array \(a\).Each of the next \(q\) lines contains the description of a query. Each line contains two integers \(l\) and \(r\) (\(1 \leq l \leq r \leq n\)) β the query is about the subarray \(a_l, a_{l+1}, \dots, a_r\).
|
For each of the \(q\) queries, print a line containing the length of the longest almost-increasing subsequence of the subarray \(a_l, a_{l+1}, \dots, a_r\).
|
In the first query, the subarray is \(a_1, a_2, a_3 = [1,2,4]\). The whole subarray is almost-increasing, so the answer is \(3\).In the second query, the subarray is \(a_1, a_2, a_3,a_4 = [1,2,4,3]\). The whole subarray is a almost-increasing, because there are no three consecutive elements such that \(x \geq y \geq z\). So the answer is \(4\).In the third query, the subarray is \(a_2, a_3, a_4, a_5 = [2, 4, 3, 3]\). The whole subarray is not almost-increasing, because the last three elements satisfy \(4 \geq 3 \geq 3\). An almost-increasing subsequence of length \(3\) can be found (for example taking \(a_2,a_3,a_5 = [2,4,3]\) ). So the answer is \(3\).
|
Input: 9 8 1 2 4 3 3 5 6 2 1 1 3 1 4 2 5 6 6 3 7 7 8 1 8 8 8 | Output: 3 4 3 1 4 2 7 1
|
Medium
| 3 | 526 | 483 | 156 | 18 |
311 |
E
|
311E
|
E. Biologist
| 2,300 |
flows
|
SmallR is a biologist. Her latest research finding is how to change the sex of dogs. In other words, she can change female dogs into male dogs and vice versa.She is going to demonstrate this technique. Now SmallR has n dogs, the costs of each dog's change may be different. The dogs are numbered from 1 to n. The cost of change for dog i is vi RMB. By the way, this technique needs a kind of medicine which can be valid for only one day. So the experiment should be taken in one day and each dog can be changed at most once.This experiment has aroused extensive attention from all sectors of society. There are m rich folks which are suspicious of this experiment. They all want to bet with SmallR forcibly. If SmallR succeeds, the i-th rich folk will pay SmallR wi RMB. But it's strange that they have a special method to determine whether SmallR succeeds. For i-th rich folk, in advance, he will appoint certain ki dogs and certain one gender. He will think SmallR succeeds if and only if on some day the ki appointed dogs are all of the appointed gender. Otherwise, he will think SmallR fails.If SmallR can't satisfy some folk that isn't her friend, she need not pay him, but if someone she can't satisfy is her good friend, she must pay g RMB to him as apologies for her fail.Then, SmallR hope to acquire money as much as possible by this experiment. Please figure out the maximum money SmallR can acquire. By the way, it is possible that she can't obtain any money, even will lose money. Then, please give out the minimum money she should lose.
|
The first line contains three integers n, m, g (1 β€ n β€ 104, 0 β€ m β€ 2000, 0 β€ g β€ 104). The second line contains n integers, each is 0 or 1, the sex of each dog, 0 represent the female and 1 represent the male. The third line contains n integers v1, v2, ..., vn (0 β€ vi β€ 104).Each of the next m lines describes a rich folk. On the i-th line the first number is the appointed sex of i-th folk (0 or 1), the next two integers are wi and ki (0 β€ wi β€ 104, 1 β€ ki β€ 10), next ki distinct integers are the indexes of appointed dogs (each index is between 1 and n). The last number of this line represents whether i-th folk is SmallR's good friend (0 β no or 1 β yes).
|
Print a single integer, the maximum money SmallR can gain. Note that the integer is negative if SmallR will lose money.
|
Input: 5 5 90 1 1 1 01 8 6 2 30 7 3 3 2 1 11 8 1 5 11 0 3 2 1 4 10 8 3 4 2 1 01 7 2 4 1 1 | Output: 2
|
Expert
| 1 | 1,549 | 664 | 119 | 3 |
|
416 |
E
|
416E
|
E. President's Path
| 2,500 |
dp; graphs; shortest paths
|
Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.Making the budget for such an event is not an easy task. For all possible distinct pairs s, t (s < t) find the number of roads that lie on at least one shortest path from s to t.
|
The first line of the input contains integers n, m (2 β€ n β€ 500, 0 β€ m β€ nΒ·(n - 1) / 2) β the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integers xi, yi, li (1 β€ xi, yi β€ n, xi β yi, 1 β€ li β€ 106), where xi, yi are the numbers of the cities connected by the i-th road and li is its length.
|
Print the sequence of integers c12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst = 0.
|
Input: 5 61 2 12 3 13 4 14 1 12 4 24 5 4 | Output: 1 4 1 2 1 5 6 1 2 1
|
Expert
| 3 | 798 | 404 | 299 | 4 |
|
149 |
C
|
149C
|
C. Division into Teams
| 1,500 |
greedy; math; sortings
|
Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers).The key in football is to divide into teams fairly before the game begins. There are n boys playing football in the yard (including Petya), each boy's football playing skill is expressed with a non-negative characteristic ai (the larger it is, the better the boy plays). Let's denote the number of players in the first team as x, the number of players in the second team as y, the individual numbers of boys who play for the first team as pi and the individual numbers of boys who play for the second team as qi. Division n boys into two teams is considered fair if three conditions are fulfilled: Each boy plays for exactly one team (x + y = n). The sizes of teams differ in no more than one (|x - y| β€ 1). The total football playing skills for two teams differ in no more than by the value of skill the best player in the yard has. More formally: Your task is to help guys divide into two teams fairly. It is guaranteed that a fair division into two teams always exists.
|
The first line contains the only integer n (2 β€ n β€ 105) which represents the number of guys in the yard. The next line contains n positive space-separated integers, ai (1 β€ ai β€ 104), the i-th number represents the i-th boy's playing skills.
|
On the first line print an integer x β the number of boys playing for the first team. On the second line print x integers β the individual numbers of boys playing for the first team. On the third line print an integer y β the number of boys playing for the second team, on the fourth line print y integers β the individual numbers of boys playing for the second team. Don't forget that you should fulfil all three conditions: x + y = n, |x - y| β€ 1, and the condition that limits the total skills.If there are multiple ways to solve the problem, print any of them.The boys are numbered starting from one in the order in which their skills are given in the input data. You are allowed to print individual numbers of boys who belong to the same team in any order.
|
Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 β€ 1) is fulfilled, the third limitation on the difference in skills ((2 + 1) - (1) = 2 β€ 2) is fulfilled.
|
Input: 31 2 1 | Output: 21 2 13
|
Medium
| 3 | 1,228 | 242 | 761 | 1 |
1,095 |
F
|
1095F
|
F. Make It Connected
| 1,900 |
dsu; graphs; greedy
|
You are given an undirected graph consisting of \(n\) vertices. A number is written on each vertex; the number on vertex \(i\) is \(a_i\). Initially there are no edges in the graph.You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices \(x\) and \(y\) is \(a_x + a_y\) coins. There are also \(m\) special offers, each of them is denoted by three numbers \(x\), \(y\) and \(w\), and means that you can add an edge connecting vertices \(x\) and \(y\) and pay \(w\) coins for it. You don't have to use special offers: if there is a pair of vertices \(x\) and \(y\) that has a special offer associated with it, you still may connect these two vertices paying \(a_x + a_y\) coins for it.What is the minimum number of coins you have to spend to make the graph connected? Recall that a graph is connected if it's possible to get from any vertex to any other vertex using only the edges belonging to this graph.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^5\)) β the number of vertices in the graph and the number of special offers, respectively.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^{12}\)) β the numbers written on the vertices.Then \(m\) lines follow, each containing three integers \(x\), \(y\) and \(w\) (\(1 \le x, y \le n\), \(1 \le w \le 10^{12}\), \(x \ne y\)) denoting a special offer: you may add an edge connecting vertex \(x\) and vertex \(y\), and this edge will cost \(w\) coins.
|
Print one integer β the minimum number of coins you have to pay to make the graph connected.
|
In the first example it is possible to connect \(1\) to \(2\) using special offer \(2\), and then \(1\) to \(3\) without using any offers.In next two examples the optimal answer may be achieved without using special offers.
|
Input: 3 2 1 3 3 2 3 5 2 1 1 | Output: 5
|
Hard
| 3 | 960 | 594 | 92 | 10 |
2,122 |
G
|
2122G
|
G. Tree Parking
| 3,300 |
combinatorics; fft; math; trees
|
Consider the following problem statement: You are given a tree with \(n\) vertices rooted at \(1\). For each \(1 \leq i \leq n\), a car will enter the root at time \(l_i\). It will then instantaneously travel from the root to vertex \(i\) through the unique simple path and park there. It will leave through the same path in the other direction at time \(r_i\).During the time when a car is parked in a vertex, it blocks other cars from traveling through that vertex. The tree is called valid if and only if all cars are able to enter and leave the tree at their desired times.Count the number of pairs of sequences \(l\), \(r\) such that \(l_i < r_i\), their concatenation is a permutation of \(1 \ldots 2n\), and the tree is valid. Calculate the sum of the answers to the problem over all labeled trees\(^{\text{β}}\) with \(n\) vertices and \(k\) leaves. The root is not considered a leaf. Since the answer may be large, calculate it modulo \(998\,244\,353\).\(^{\text{β}}\)Two labeled trees are considered different if and only if their edge sets are different.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The only line of each test case contains two integers \(n\), \(k\) (\(1 \leq k < n \leq 2 \cdot 10^5\)) β the number of vertices and leaves of the tree, respectively.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 answer modulo \(998\,244\,353\).
|
In the first case, there is only one tree that satisfies the constraints. The correct pairs of sequences are: $$$\( (l, r) = \bigl([1, 3], [2, 4]\bigr),\:\bigl([3, 1], [4, 2]\bigr),\:\bigl([2, 1], [3, 4]\bigr). \)$$$
|
Input: 32 18 365 43 | Output: 3 899171636 38330886
|
Master
| 4 | 1,065 | 420 | 77 | 21 |
2,094 |
G
|
2094G
|
G. Chimpanzini Bananini
| 1,700 |
data structures; implementation; math
|
Chimpanzini Bananini stands on the brink of a momentous battleβone destined to bring finality.For an arbitrary array \(b\) of length \(m\), let's denote the rizziness of the array to be \(\sum_{i=1}^mb_i\cdot i=b_1\cdot 1+b_2\cdot 2+b_3\cdot 3+\ldots + b_m\cdot m\).Chimpanzini Bananini gifts you an empty array. There are three types of operations you can perform on it. Perform a cyclic shift on the array. That is, the array \([a_1, a_2, \ldots, a_n]\) becomes \([a_n, a_1, a_2, \ldots, a_{n-1}].\) Reverse the entire array. That is, the array \([a_1, a_2, \ldots, a_n]\) becomes \([a_n, a_{n-1}, \ldots, a_1].\) Append an element to the end of the array. The array \([a_1, a_2, \ldots, a_n]\) becomes \([a_1, a_2, \ldots, a_n, k]\) after appending \(k\) to the end of the array. After each operation, you are interested in calculating the rizziness of your array. Note that all operations are persistent. This means that each operation modifies the array, and subsequent operations should be applied to the current state of the array after the previous operations.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of the input contains an integer \(q\) (\(1 \leq q \leq 2\cdot 10^5\)) β the number of operations you perform on your array.The following \(q\) lines first contain a single integer \(s\) (\(1 \leq s \leq 3\)) β the operation type. If \(s=1\), then the cyclic shift operation should be performed. If \(s=2\), then the reversal operation should be performed. If \(s=3\), then the line will contain an additional integer \(k\) (\(1 \leq k \leq 10^6\)), denoting the element appended to the back of the array. It is guaranteed that the sum of \(q\) will not exceed \(2\cdot 10^5\) over all test cases. Additionally, it is guaranteed that the first operation on each test case will be one with \(s=3\).
|
For each test case, output \(q\) lines, outputting the rizziness of your array after each operation.
|
The first six states of the array: \([1]\) \([1, 2]\) \([1, 2, 3]\) \([3, 1, 2]\) \([3, 1, 2, 4]\) \([4, 2, 1, 3]\)
|
Input: 1133 13 23 313 423 513 623 721 | Output: 1 5 14 11 27 23 48 38 74 73 122 102 88
|
Medium
| 3 | 1,068 | 805 | 100 | 20 |
886 |
F
|
886F
|
F. Symmetric Projections
| 2,900 |
geometry
|
You are given a set of n points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines.Multiset is a set where equal elements are allowed.Multiset is called symmetric, if there is a point P on the plane such that the multiset is centrally symmetric in respect of point P.
|
The first line contains a single integer n (1 β€ n β€ 2000) β the number of points in the set.Each of the next n lines contains two integers xi and yi ( - 106 β€ xi, yi β€ 106) β the coordinates of the points. It is guaranteed that no two points coincide.
|
If there are infinitely many good lines, print -1.Otherwise, print single integer β the number of good lines.
|
Picture to the first sample test: In the second sample, any line containing the origin is good.
|
Input: 31 22 13 3 | Output: 3
|
Master
| 1 | 393 | 251 | 109 | 8 |
992 |
B
|
992B
|
B. Nastya Studies Informatics
| 1,600 |
math; number theory
|
Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.We define a pair of integers (a, b) good, if GCD(a, b) = x and LCM(a, b) = y, where GCD(a, b) denotes the greatest common divisor of a and b, and LCM(a, b) denotes the least common multiple of a and b.You are given two integers x and y. You are to find the number of good pairs of integers (a, b) such that l β€ a, b β€ r. Note that pairs (a, b) and (b, a) are considered different if a β b.
|
The only line contains four integers l, r, x, y (1 β€ l β€ r β€ 109, 1 β€ x β€ y β€ 109).
|
In the only line print the only integer β the answer for the problem.
|
In the first example there are two suitable good pairs of integers (a, b): (1, 2) and (2, 1).In the second example there are four suitable good pairs of integers (a, b): (1, 12), (12, 1), (3, 4) and (4, 3).In the third example there are good pairs of integers, for example, (3, 30), but none of them fits the condition l β€ a, b β€ r.
|
Input: 1 2 1 2 | Output: 2
|
Medium
| 2 | 586 | 83 | 69 | 9 |
1,336 |
F
|
1336F
|
F. Journey
| 3,500 |
data structures; divide and conquer; graphs; trees
|
In the wilds far beyond lies the Land of Sacredness, which can be viewed as a tree β connected undirected graph consisting of \(n\) nodes and \(n-1\) edges. The nodes are numbered from \(1\) to \(n\). There are \(m\) travelers attracted by its prosperity and beauty. Thereupon, they set off their journey on this land. The \(i\)-th traveler will travel along the shortest path from \(s_i\) to \(t_i\). In doing so, they will go through all edges in the shortest path from \(s_i\) to \(t_i\), which is unique in the tree.During their journey, the travelers will acquaint themselves with the others. Some may even become friends. To be specific, the \(i\)-th traveler and the \(j\)-th traveler will become friends if and only if there are at least \(k\) edges that both the \(i\)-th traveler and the \(j\)-th traveler will go through. Your task is to find out the number of pairs of travelers \((i, j)\) satisfying the following conditions: \(1 \leq i < j \leq m\). the \(i\)-th traveler and the \(j\)-th traveler will become friends.
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(2 \le n, m \le 1.5 \cdot 10^5\), \(1\le k\le n\)). Each of the next \(n-1\) lines contains two integers \(u\) and \(v\) (\(1 \le u,v \le n\)), denoting there is an edge between \(u\) and \(v\). The \(i\)-th line of the next \(m\) lines contains two integers \(s_i\) and \(t_i\) (\(1\le s_i,t_i\le n\), \(s_i \neq t_i\)), denoting the starting point and the destination of \(i\)-th traveler. It is guaranteed that the given edges form a tree.
|
The only line contains a single integer β the number of pairs of travelers satisfying the given conditions.
|
In the first example there are \(4\) pairs satisfying the given requirements: \((1,2)\), \((1,3)\), \((1,4)\), \((3,4)\). The \(1\)-st traveler and the \(2\)-nd traveler both go through the edge \(6-8\). The \(1\)-st traveler and the \(3\)-rd traveler both go through the edge \(2-6\). The \(1\)-st traveler and the \(4\)-th traveler both go through the edge \(1-2\) and \(2-6\). The \(3\)-rd traveler and the \(4\)-th traveler both go through the edge \(2-6\).
|
Input: 8 4 1 1 7 1 2 2 5 4 6 6 3 6 2 6 8 7 8 3 8 2 6 4 1 | Output: 4
|
Master
| 4 | 1,032 | 506 | 107 | 13 |
121 |
C
|
121C
|
C. Lucky Permutation
| 1,900 |
brute force; combinatorics; number theory
|
Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.
|
The first line contains two integers n and k (1 β€ n, k β€ 109) β the number of elements in the permutation and the lexicographical number of the permutation.
|
If the k-th permutation of numbers from 1 to n does not exist, print the single number ""-1"" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.
|
A permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1 β€ i β€ n). Permutation a is lexicographically smaller that permutation b if there is such a i (1 β€ i β€ n), that ai < bi, and for any j (1 β€ j < i) aj = bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.In the first sample the permutation looks like that:1 2 3 4 6 7 5The only suitable position is 4.In the second sample the permutation looks like that:2 1 3 4The only suitable position is 4.
|
Input: 7 4 | Output: 1
|
Hard
| 3 | 422 | 156 | 227 | 1 |
257 |
E
|
257E
|
E. Greedy Elevator
| 2,200 |
data structures; implementation
|
The m-floor (m > 1) office of international corporation CodeForces has the advanced elevator control system established. It works as follows.All office floors are sequentially numbered with integers from 1 to m. At time t = 0, the elevator is on the first floor, the elevator is empty and nobody is waiting for the elevator on other floors. Next, at times ti (ti > 0) people come to the elevator. For simplicity, we assume that one person uses the elevator only once during the reported interval. For every person we know three parameters: the time at which the person comes to the elevator, the floor on which the person is initially, and the floor to which he wants to go.The movement of the elevator between the floors is as follows. At time t (t β₯ 0, t is an integer) the elevator is always at some floor. First the elevator releases all people who are in the elevator and want to get to the current floor. Then it lets in all the people waiting for the elevator on this floor. If a person comes to the elevator exactly at time t, then he has enough time to get into it. We can assume that all of these actions (going in or out from the elevator) are made instantly. After that the elevator decides, which way to move and at time (t + 1) the elevator gets to the selected floor.The elevator selects the direction of moving by the following algorithm. If the elevator is empty and at the current time no one is waiting for the elevator on any floor, then the elevator remains at the current floor. Otherwise, let's assume that the elevator is on the floor number x (1 β€ x β€ m). Then elevator calculates the directions' ""priorities"" pup and pdown: pup is the sum of the number of people waiting for the elevator on the floors with numbers greater than x, and the number of people in the elevator, who want to get to the floors with the numbers greater than x; pdown is the sum of the number of people waiting for the elevator on the floors with numbers less than x, and the number of people in the elevator, who want to get to the floors with the numbers less than x. If pup β₯ pdown, then the elevator goes one floor above the current one (that is, from floor x to floor x + 1), otherwise the elevator goes one floor below the current one (that is, from floor x to floor x - 1). Your task is to simulate the work of the elevator and for each person to tell the time when the elevator will get to the floor this person needs. Please note that the elevator is large enough to accommodate all the people at once.
|
The first line contains two space-separated integers: n, m (1 β€ n β€ 105, 2 β€ m β€ 105) β the number of people and floors in the building, correspondingly.Next n lines each contain three space-separated integers: ti, si, fi (1 β€ ti β€ 109, 1 β€ si, fi β€ m, si β fi) β the time when the i-th person begins waiting for the elevator, the floor number, where the i-th person was initially located, and the number of the floor, where he wants to go.
|
Print n lines. In the i-th line print a single number β the moment of time, when the i-th person gets to the floor he needs. The people are numbered in the order, in which they are given in the input. Please don't use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the first sample the elevator worked as follows: t = 1. The elevator is on the floor number 1. The elevator is empty. The floor number 2 has one person waiting. pup = 1 + 0 = 1, pdown = 0 + 0 = 0, pup β₯ pdown. So the elevator goes to the floor number 2. t = 2. The elevator is on the floor number 2. One person enters the elevator, he wants to go to the floor number 7. pup = 0 + 1 = 1, pdown = 0 + 0 = 0, pup β₯ pdown. So the elevator goes to the floor number 3. t = 3. The elevator is on the floor number 3. There is one person in the elevator, he wants to go to floor 7. The floors number 4 and 6 have two people waiting for the elevator. pup = 2 + 1 = 3, pdown = 0 + 0 = 0, pup β₯ pdown. So the elevator goes to the floor number 4. t = 4. The elevator is on the floor number 4. There is one person in the elevator who wants to go to the floor number 7. One person goes into the elevator, he wants to get to the floor number 8. The floor number 6 has one man waiting. pup = 1 + 2 = 3, pdown = 0 + 0 = 0, pup β₯ pdown. So the elevator goes to the floor number 5. t = 5. The elevator is on the floor number 5. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. There is one person waiting for the elevator on the floor number 6. pup = 1 + 2 = 3, pdown = 0 + 0 = 0, pup β₯ pdown. So the elevator goes to the floor number 6. t = 6. The elevator is on the floor number 6. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. One man enters the elevator, he wants to get to the floor number 5. pup = 0 + 2 = 2, pdown = 0 + 1 = 1, pup β₯ pdown. So the elevator goes to the floor number 7. t = 7. The elevator is on the floor number 7. One person leaves the elevator, this person wanted to get to the floor number 7. There are two people in the elevator, they want to get to the floors with numbers 8 and 5, correspondingly. pup = 0 + 1 = 1, pdown = 0 + 1 = 1, pup β₯ pdown. So the elevator goes to the floor number 8. t = 8. The elevator is on the floor number 8. One person leaves the elevator, this person wanted to go to the floor number 8. There is one person in the elevator, he wants to go to the floor number 5. pup = 0 + 0 = 0, pdown = 0 + 1 = 1, pup < pdown. So the elevator goes to the floor number 7. t = 9. The elevator is on the floor number 7. There is one person in the elevator, this person wants to get to the floor number 5. pup = 0 + 0 = 0, pdown = 0 + 1 = 1, pup < pdown. So the elevator goes to the floor number 6. t = 10. The elevator is on the floor number 6. There is one person in the elevator, he wants to get to the floor number 5. pup = 0 + 0 = 0, pdown = 0 + 1 = 1, pup < pdown. So the elevator goes to the floor number 5. t = 11. The elevator is on the floor number 5. One person leaves the elevator, this person initially wanted to get to the floor number 5. The elevator is empty and nobody needs it, so the elevator remains at the floor number 5.
|
Input: 3 101 2 73 6 53 4 8 | Output: 7118
|
Hard
| 2 | 2,513 | 440 | 346 | 2 |
1,556 |
H
|
1556H
|
H. DIY Tree
| 3,300 |
graphs; greedy; math; probabilities
|
William really likes puzzle kits. For one of his birthdays, his friends gifted him a complete undirected edge-weighted graph consisting of \(n\) vertices.He wants to build a spanning tree of this graph, such that for the first \(k\) vertices the following condition is satisfied: the degree of a vertex with index \(i\) does not exceed \(d_i\). Vertices from \(k + 1\) to \(n\) may have any degree.William wants you to find the minimum weight of a spanning tree that satisfies all the conditions.A spanning tree is a subset of edges of a graph that forms a tree on all \(n\) vertices of the graph. The weight of a spanning tree is defined as the sum of weights of all the edges included in a spanning tree.
|
The first line of input contains two integers \(n\), \(k\) (\(2 \leq n \leq 50\), \(1 \leq k \leq min(n - 1, 5)\)).The second line contains \(k\) integers \(d_1, d_2, \ldots, d_k\) (\(1 \leq d_i \leq n\)). The \(i\)-th of the next \(n - 1\) lines contains \(n - i\) integers \(w_{i,i+1}, w_{i,i+2}, \ldots, w_{i,n}\) (\(1 \leq w_{i,j} \leq 100\)): weights of edges \((i,i+1),(i,i+2),\ldots,(i,n)\).
|
Print one integer: the minimum weight of a spanning tree under given degree constraints for the first \(k\) vertices.
|
Input: 10 5 5 3 4 2 1 29 49 33 12 55 15 32 62 37 61 26 15 58 15 22 8 58 37 16 9 39 20 14 58 10 15 40 3 19 55 53 13 37 44 52 23 59 58 4 69 80 29 89 28 48 | Output: 95
|
Master
| 4 | 706 | 398 | 117 | 15 |
|
68 |
D
|
68D
|
D. Half-decay tree
| 2,500 |
data structures; divide and conquer; dp; math; probabilities
|
Recently Petya has become keen on physics. Anna V., his teacher noticed Petya's interest and gave him a fascinating physical puzzle β a half-decay tree. A half-decay tree is a complete binary tree with the height h. The height of a tree is the length of the path (in edges) from the root to a leaf in the tree. While studying the tree Petya can add electrons to vertices or induce random decay with synchrophasotron. Random decay is a process during which the edges of some path from the root to the random leaf of the tree are deleted. All the leaves are equiprobable. As the half-decay tree is the school property, Petya will return back the deleted edges into the tree after each decay.After being desintegrated, the tree decomposes into connected components. Charge of each component is the total quantity of electrons placed in vertices of the component. Potential of desintegerated tree is the maximum from the charges of its connected components. Each time before inducing random decay Petya is curious about the mathematical expectation of potential of the tree after being desintegrated.
|
First line will contain two integers h and q (1 β€ h β€ 30, 1 β€ q β€ 105). Next q lines will contain a query of one of two types: add v ePetya adds e electrons to vertex number v (1 β€ v β€ 2h + 1 - 1, 0 β€ e β€ 104). v and e are integers.The vertices of the tree are numbered in the following way: the root is numbered with 1, the children of the vertex with number x are numbered with 2x and 2x + 1. decayPetya induces tree decay.
|
For each query decay solution you should output the mathematical expectation of potential of the tree after being desintegrated. The absolute or relative error in the answer should not exceed 10 - 4.
|
Input: 1 4add 1 3add 2 10add 3 11decay | Output: 13.50000000
|
Expert
| 5 | 1,096 | 425 | 199 | 0 |
|
1,916 |
D
|
1916D
|
D. Mathematical Problem
| 1,700 |
brute force; constructive algorithms; geometry; math
|
The mathematicians of the 31st lyceum were given the following task:You are given an odd number \(n\), and you need to find \(n\) different numbers that are squares of integers. But it's not that simple. Each number should have a length of \(n\) (and should not have leading zeros), and the multiset of digits of all the numbers should be the same. For example, for \(\mathtt{234}\) and \(\mathtt{432}\), and \(\mathtt{11223}\) and \(\mathtt{32211}\), the multisets of digits are the same, but for \(\mathtt{123}\) and \(\mathtt{112233}\), they are not.The mathematicians couldn't solve this problem. Can you?
|
The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases.The following \(t\) lines contain one odd integer \(n\) (\(1 \leq n \leq 99\)) β the number of numbers to be found and their length.It is guaranteed that the solution exists within the given constraints.It is guaranteed that the sum of \(n^2\) does not exceed \(10^5\).The numbers can be output in any order.
|
For each test case, you need to output \(n\) numbers of length \(n\) β the answer to the problem.If there are several answers, print any of them.
|
Below are the squares of the numbers that are the answers for the second test case:\(\mathtt{169}\) = \(\mathtt{13}^2\)\(\mathtt{196}\) = \(\mathtt{14}^2\)\(\mathtt{961}\) = \(\mathtt{31}^2\)Below are the squares of the numbers that are the answers for the third test case:\(\mathtt{16384}\) = \(\mathtt{128}^2\)\(\mathtt{31684}\) = \(\mathtt{178}^2\)\(\mathtt{36481}\) = \(\mathtt{191}^2\)\(\mathtt{38416}\) = \(\mathtt{196}^2\)\(\mathtt{43681}\) = \(\mathtt{209}^2\)
|
Input: 3135 | Output: 1 169 196 961 16384 31684 36481 38416 43681
|
Medium
| 4 | 609 | 400 | 145 | 19 |
1,733 |
B
|
1733B
|
B. Rule of League
| 900 |
constructive algorithms; math
|
There is a badminton championship in which \(n\) players take part. The players are numbered from \(1\) to \(n\). The championship proceeds as follows: player \(1\) and player \(2\) play a game, then the winner and player \(3\) play a game, and then the winner and player \(4\) play a game, and so on. So, \(n-1\) games are played, and the winner of the last game becomes the champion. There are no draws in the games.You want to find out the result of championship. Currently, you only know the following information: Each player has either won \(x\) games or \(y\) games in the championship. Given \(n\), \(x\), and \(y\), find out if there is a result that matches this information.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases.The only line of each test case contains three integers \(n\), \(x\), \(y\) (\(2 \le n \le 10^5\), \(0 \le x, y < n\)).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
Print the answer for each test case, one per line. If there is no result that matches the given information about \(n\), \(x\), \(y\), print \(-1\). Otherwise, print \(n-1\) space separated integers, where the \(i\)-th integer is the player number of the winner of the \(i\)-th game. If there are multiple valid results, print any.
|
In the first test case, player \(1\) and player \(4\) won \(x\) times, player \(2\) and player \(3\) won \(y\) times.In the second, third, and fifth test cases, no valid result exists.
|
Input: 55 2 08 1 23 0 02 0 16 3 0 | Output: 1 1 4 4 -1 -1 2 -1
|
Beginner
| 2 | 685 | 302 | 331 | 17 |
601 |
D
|
601D
|
D. Acyclic Organic Compounds
| 2,400 |
data structures; dfs and similar; dsu; hashing; strings; trees
|
You are given a tree T with n vertices (numbered 1 through n) and a letter in each vertex. The tree is rooted at vertex 1.Let's look at the subtree Tv of some vertex v. It is possible to read a string along each simple path starting at v and ending at some vertex in Tv (possibly v itself). Let's denote the number of distinct strings which can be read this way as . Also, there's a number cv assigned to each vertex v. We are interested in vertices with the maximum value of .You should compute two statistics: the maximum value of and the number of vertices v with the maximum .
|
The first line of the input contains one integer n (1 β€ n β€ 300 000) β the number of vertices of the tree.The second line contains n space-separated integers ci (0 β€ ci β€ 109).The third line contains a string s consisting of n lowercase English letters β the i-th character of this string is the letter in vertex i.The following n - 1 lines describe the tree T. Each of them contains two space-separated integers u and v (1 β€ u, v β€ n) indicating an edge between vertices u and v.It's guaranteed that the input will describe a tree.
|
Print two lines. On the first line, print over all 1 β€ i β€ n. On the second line, print the number of vertices v for which .
|
In the first sample, the tree looks like this: The sets of strings that can be read from individual vertices are: Finally, the values of are: In the second sample, the values of are (5, 4, 2, 1, 1, 1). The distinct strings read in T2 are ; note that can be read down to vertices 3 or 4.
|
Input: 101 2 7 20 20 30 40 50 50 50cacabbcddd1 26 87 26 25 45 93 102 52 3 | Output: 513
|
Expert
| 6 | 580 | 532 | 124 | 6 |
130 |
H
|
130H
|
H. Balanced brackets
| 1,600 |
*special
|
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.You are given a string which consists of opening and closing round brackets. Check whether it is a balanced bracket sequence.
|
The only line of input contains a string between 1 and 100 characters long, inclusive. Each character in the string will be Β«(Β» or Β«)Β».
|
Output Β«YESΒ» if the bracket sequence is balanced, and Β«NOΒ» otherwise (quotes for clarity only).
|
Input: (()(()))() | Output: YES
|
Medium
| 1 | 355 | 135 | 95 | 1 |
|
757 |
E
|
757E
|
E. Bash Plays with Functions
| 2,500 |
brute force; combinatorics; dp; number theory
|
Bash got tired on his journey to become the greatest Pokemon master. So he decides to take a break and play with functions.Bash defines a function f0(n), which denotes the number of ways of factoring n into two factors p and q such that gcd(p, q) = 1. In other words, f0(n) is the number of ordered pairs of positive integers (p, q) such that pΒ·q = n and gcd(p, q) = 1.But Bash felt that it was too easy to calculate this function. So he defined a series of functions, where fr + 1 is defined as:Where (u, v) is any ordered pair of positive integers, they need not to be co-prime.Now Bash wants to know the value of fr(n) for different r and n. Since the value could be huge, he would like to know the value modulo 109 + 7. Help him!
|
The first line contains an integer q (1 β€ q β€ 106) β the number of values Bash wants to know.Each of the next q lines contain two integers r and n (0 β€ r β€ 106, 1 β€ n β€ 106), which denote Bash wants to know the value fr(n).
|
Print q integers. For each pair of r and n given, print fr(n) modulo 109 + 7 on a separate line.
|
Input: 50 301 253 652 54 48 | Output: 85254630
|
Expert
| 4 | 733 | 223 | 96 | 7 |
|
1,833 |
F
|
1833F
|
F. Ira and Flamenco
| 1,700 |
combinatorics; constructive algorithms; data structures; implementation; math; sortings; two pointers
|
Ira loves Spanish flamenco dance very much. She decided to start her own dance studio and found \(n\) students, \(i\)th of whom has level \(a_i\).Ira can choose several of her students and set a dance with them. So she can set a huge number of dances, but she is only interested in magnificent dances. The dance is called magnificent if the following is true: exactly \(m\) students participate in the dance; levels of all dancers are pairwise distinct; levels of every two dancers have an absolute difference strictly less than \(m\). For example, if \(m = 3\) and \(a = [4, 2, 2, 3, 6]\), the following dances are magnificent (students participating in the dance are highlighted in red): \([\color{red}{4}, 2, \color{red}{2}, \color{red}{3}, 6]\), \([\color{red}{4}, \color{red}{2}, 2, \color{red}{3}, 6]\). At the same time dances \([\color{red}{4}, 2, 2, \color{red}{3}, 6]\), \([4, \color{red}{2}, \color{red}{2}, \color{red}{3}, 6]\), \([\color{red}{4}, 2, 2, \color{red}{3}, \color{red}{6}]\) are not magnificent.In the dance \([\color{red}{4}, 2, 2, \color{red}{3}, 6]\) only \(2\) students participate, although \(m = 3\).The dance \([4, \color{red}{2}, \color{red}{2}, \color{red}{3}, 6]\) involves students with levels \(2\) and \(2\), although levels of all dancers must be pairwise distinct.In the dance \([\color{red}{4}, 2, 2, \color{red}{3}, \color{red}{6}]\) students with levels \(3\) and \(6\) participate, but \(|3 - 6| = 3\), although \(m = 3\).Help Ira count the number of magnificent dances that she can set. Since this number can be very large, count it modulo \(10^9 + 7\). Two dances are considered different if the sets of students participating in them are different.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β number of testcases.The first line of each testcase contains integers \(n\) and \(m\) (\(1 \le m \le n \le 2 \cdot 10^5\)) β the number of Ira students and the number of dancers in the magnificent dance.The second line of each testcase contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β levels of students.It is guaranteed that the sum of \(n\) over all testcases does not exceed \(2 \cdot 10^5\).
|
For each testcase, print a single integer β the number of magnificent dances. Since this number can be very large, print it modulo \(10^9 + 7\).
|
In the first testcase, Ira can set such magnificent dances: \([\color{red}{8}, 10, 10, \color{red}{9}, \color{red}{6}, 11, \color{red}{7}]\), \([\color{red}{8}, \color{red}{10}, 10, \color{red}{9}, 6, 11, \color{red}{7}]\), \([\color{red}{8}, 10, \color{red}{10}, \color{red}{9}, 6, 11, \color{red}{7}]\), \([\color{red}{8}, 10, \color{red}{10}, \color{red}{9}, 6, \color{red}{11}, 7]\), \([\color{red}{8}, \color{red}{10}, 10, \color{red}{9}, 6, \color{red}{11}, 7]\).The second testcase is explained in the statements.
|
Input: 97 48 10 10 9 6 11 75 34 2 2 3 68 21 5 2 2 3 1 3 33 33 3 35 13 4 3 10 712 35 2 1 1 4 3 5 5 5 2 7 51 113 21 2 32 21 2 | Output: 5 2 10 0 5 11 1 2 1
|
Medium
| 7 | 1,695 | 495 | 144 | 18 |
43 |
E
|
43E
|
E. Race
| 2,300 |
brute force; implementation; two pointers
|
Today s kilometer long auto race takes place in Berland. The track is represented by a straight line as long as s kilometers. There are n cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior β the system of segments on each of which the speed of the car is constant. The j-th segment of the i-th car is pair (vi, j, ti, j), where vi, j is the car's speed on the whole segment in kilometers per hour and ti, j is for how many hours the car had been driving at that speed. The segments are given in the order in which they are ""being driven on"" by the cars.Your task is to find out how many times during the race some car managed to have a lead over another car. A lead is considered a situation when one car appears in front of another car. It is known, that all the leads happen instantly, i. e. there are no such time segment of positive length, during which some two cars drive ""together"". At one moment of time on one and the same point several leads may appear. In this case all of them should be taken individually. Meetings of cars at the start and finish are not considered to be counted as leads.
|
The first line contains two integers n and s (2 β€ n β€ 100, 1 β€ s β€ 106) β the number of cars and the length of the track in kilometers. Then follow n lines β the description of the system of segments for each car. Every description starts with integer k (1 β€ k β€ 100) β the number of segments in the system. Then k space-separated pairs of integers are written. Each pair is the speed and time of the segment. These integers are positive and don't exceed 1000. It is guaranteed, that the sum of lengths of all segments (in kilometers) for each car equals to s; and all the leads happen instantly.
|
Print the single number β the number of times some car managed to take the lead over another car during the race.
|
Input: 2 332 5 1 2 141 3 11 | Output: 1
|
Expert
| 3 | 1,190 | 596 | 113 | 0 |
|
2,069 |
B
|
2069B
|
B. Set of Strangers
| 1,200 |
greedy; matrices
|
You are given a table of \(n\) rows and \(m\) columns. Initially, the cell at the \(i\)-th row and the \(j\)-th column has color \(a_{i, j}\).Let's say that two cells are strangers if they don't share a side. Strangers are allowed to touch with corners.Let's say that the set of cells is a set of strangers if all pairs of cells in the set are strangers. Sets with no more than one cell are sets of strangers by definition.In one step, you can choose any set of strangers such that all cells in it have the same color and paint all of them in some other color. You can choose the resulting color.What is the minimum number of steps you need to make the whole table the same color?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Next, \(t\) cases follow.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le m \le 700\)) β the number of rows and columns in the table.The next \(n\) lines contain the colors of cells in the corresponding row \(a_{i, 1}, \dots, a_{i, m}\) (\(1 \le a_{i, j} \le nm\)).It's guaranteed that the total sum of \(nm\) doesn't exceed \(5 \cdot 10^5\) over all test cases.
|
For each test case, print one integer β the minimum number of steps to paint all cells of the table the same color.
|
In the first test case, the table is painted in one color from the start.In the second test case, you can, for example, choose all cells with color \(1\) and paint them in \(3\). Then choose all cells with color \(2\) and also paint them in \(3\).In the third test case, you can choose all cells with color \(5\) and paint them in color \(4\).
|
Input: 41 113 31 2 12 3 21 3 11 65 4 5 4 4 53 41 4 2 21 4 3 56 6 3 5 | Output: 0 2 1 10
|
Easy
| 2 | 680 | 497 | 115 | 20 |
538 |
E
|
538E
|
E. Demiurges Play Again
| 2,200 |
dfs and similar; dp; math; trees
|
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf.Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers.
|
The first line contains a single integer n β the number of nodes in the tree (1 β€ n β€ 2Β·105).Each of the next n - 1 lines contains two integers ui and vi (1 β€ ui, vi β€ n) β the ends of the edge of the tree; the edge leads from node ui to node vi. It is guaranteed that the described graph is a rooted tree, and the root is the node 1.
|
Print two space-separated integers β the maximum possible and the minimum possible result of the game.
|
Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.
|
Input: 51 21 32 42 5 | Output: 3 2
|
Hard
| 4 | 1,477 | 334 | 102 | 5 |
546 |
A
|
546A
|
A. Soldier and Bananas
| 800 |
brute force; implementation; math
|
A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay iΒ·k dollars for the i-th banana). He has n dollars. How many dollars does he have to borrow from his friend soldier to buy w bananas?
|
The first line contains three positive integers k, n, w (1 β€ k, w β€ 1000, 0 β€ n β€ 109), the cost of the first banana, initial number of dollars the soldier has and number of bananas he wants.
|
Output one integer β the amount of dollars that the soldier must borrow from his friend. If he doesn't have to borrow money, output 0.
|
Input: 3 17 4 | Output: 13
|
Beginner
| 3 | 296 | 191 | 134 | 5 |
|
1,237 |
G
|
1237G
|
G. Balanced Distribution
| 3,500 |
data structures; dp; greedy
|
There are \(n\) friends living on a circular street. The friends and their houses are numbered clockwise from \(0\) to \(n-1\).Initially person \(i\) has \(a_i\) stones. The friends want to make the distribution of stones among them perfectly balanced: everyone should possess the same number of stones.The only way to change the distribution of stones is by conducting meetings. During a meeting, people from exactly \(k\) consecutive houses (remember that the street is circular) gather at the same place and bring all their stones with them. All brought stones may be redistributed among people attending the meeting arbitrarily. The total number of stones they possess before the meeting and after the meeting must stay the same. After the meeting, everyone returns to their home.Find a way to make the distribution of stones perfectly balanced conducting as few meetings as possible.
|
The first line contains two integers \(n\) and \(k\) (\(2 \le k < n \le 10^5\)), denoting the number of friends and the size of each meeting.The second line contains \(n\) integers \(a_0, a_1, \ldots, a_{n-1}\) (\(0 \le a_i \le 10^4\)), denoting the number of stones people initially have.The sum of all \(a_i\) is divisible by \(n\).
|
Output the minimum number of meetings \(m\) (\(m \ge 0\)), followed by \(m\) descriptions of meetings in chronological order.The \(i\)-th description must consist of an integer \(s_i\) (\(0 \le s_i < n\)), followed by \(k\) non-negative integers \(b_{i, 0}, b_{i, 1}, \ldots, b_{i, k-1}\) (\(b_{i, j} \ge 0\)). Such a description denotes a meeting of people \(s_i, (s_i + 1) \bmod n, \ldots, (s_i + k - 1) \bmod n\), and \(b_{i,j}\) denotes the number of stones person \((s_i + j) \bmod n\) must have after the \(i\)-th meeting. The sum of \(b_{i, j}\) must match the total number of stones owned by these people before the \(i\)-th meeting.We can show that a solution exists for any valid input, and any correct output contains at most \(10^7\) non-whitespace characters.
|
In the first example, the distribution of stones changes as follows: after the first meeting: \(2\) \(6\) \(\mathbf{7}\) \(\mathbf{3}\) \(\mathbf{4}\) \(2\); after the second meeting: \(\mathbf{4}\) \(\mathbf{2}\) \(7\) \(3\) \(4\) \(\mathbf{4}\); after the third meeting: \(4\) \(\mathbf{4}\) \(\mathbf{4}\) \(\mathbf{4}\) \(4\) \(4\). In the second example, the distribution of stones changes as follows: after the first meeting: \(1\) \(0\) \(1\) \(\mathbf{2}\) \(\mathbf{2}\) \(\mathbf{2}\) \(\mathbf{2}\) \(2\) \(4\) \(3\) \(3\); after the second meeting: \(\mathbf{5}\) \(0\) \(1\) \(2\) \(2\) \(2\) \(2\) \(2\) \(\mathbf{2}\) \(\mathbf{2}\) \(\mathbf{2}\); after the third meeting: \(\mathbf{2}\) \(\mathbf{2}\) \(\mathbf{2}\) \(2\) \(2\) \(2\) \(2\) \(2\) \(2\) \(2\) \(\mathbf{2}\).
|
Input: 6 3 2 6 1 10 3 2 | Output: 3 2 7 3 4 5 4 4 2 1 4 4 4
|
Master
| 3 | 888 | 334 | 772 | 12 |
1,454 |
E
|
1454E
|
E. Number of Simple Paths
| 2,000 |
combinatorics; dfs and similar; graphs; trees
|
You are given an undirected graph consisting of \(n\) vertices and \(n\) edges. It is guaranteed that the given graph is connected (i. e. it is possible to reach any vertex from any other vertex) and there are no self-loops and multiple edges in the graph.Your task is to calculate the number of simple paths of length at least \(1\) in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths). For example, paths \([1, 2, 3]\) and \([3, 2, 1]\) are considered the same.You have to answer \(t\) independent test cases.Recall that a path in the graph is a sequence of vertices \(v_1, v_2, \ldots, v_k\) such that each pair of adjacent (consecutive) vertices in this sequence is connected by an edge. The length of the path is the number of edges in it. A simple path is such a path that all vertices in it are distinct.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of vertices (and the number of edges) in the graph.The next \(n\) lines of the test case describe edges: edge \(i\) is given as a pair of vertices \(u_i\), \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)), where \(u_i\) and \(v_i\) are vertices the \(i\)-th edge connects. For each pair of vertices \((u, v)\), there is at most one edge between \(u\) and \(v\). There are no edges from the vertex to itself. So, there are no self-loops and multiple edges in the graph. The graph is undirected, i. e. all its edges are bidirectional. The graph is connected, i. e. it is possible to reach any vertex from any other vertex by moving along the edges of the graph.It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print one integer: the number of simple paths of length at least \(1\) in the given graph. Note that paths that differ only by their direction are considered the same (i. e. you have to calculate the number of undirected paths).
|
Consider the second test case of the example. It looks like that:There are \(11\) different simple paths: \([1, 2]\); \([2, 3]\); \([3, 4]\); \([2, 4]\); \([1, 2, 4]\); \([1, 2, 3]\); \([2, 3, 4]\); \([2, 4, 3]\); \([3, 2, 4]\); \([1, 2, 3, 4]\); \([1, 2, 4, 3]\).
|
Input: 3 3 1 2 2 3 1 3 4 1 2 2 3 3 4 4 2 5 1 2 2 3 1 3 2 5 4 3 | Output: 6 11 18
|
Hard
| 4 | 915 | 1,010 | 248 | 14 |
1,555 |
C
|
1555C
|
C. Coin Rows
| 1,300 |
brute force; constructive algorithms; dp; implementation
|
Alice and Bob are playing a game on a matrix, consisting of \(2\) rows and \(m\) columns. The cell in the \(i\)-th row in the \(j\)-th column contains \(a_{i, j}\) coins in it.Initially, both Alice and Bob are standing in a cell \((1, 1)\). They are going to perform a sequence of moves to reach a cell \((2, m)\).The possible moves are: Move right β from some cell \((x, y)\) to \((x, y + 1)\); Move down β from some cell \((x, y)\) to \((x + 1, y)\). First, Alice makes all her moves until she reaches \((2, m)\). She collects the coins in all cells she visit (including the starting cell).When Alice finishes, Bob starts his journey. He also performs the moves to reach \((2, m)\) and collects the coins in all cells that he visited, but Alice didn't.The score of the game is the total number of coins Bob collects.Alice wants to minimize the score. Bob wants to maximize the score. What will the score of the game be if both players play optimally?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.Then the descriptions of \(t\) testcases follow.The first line of the testcase contains a single integer \(m\) (\(1 \le m \le 10^5\)) β the number of columns of the matrix.The \(i\)-th of the next \(2\) lines contain \(m\) integers \(a_{i,1}, a_{i,2}, \dots, a_{i,m}\) (\(1 \le a_{i,j} \le 10^4\)) β the number of coins in the cell in the \(i\)-th row in the \(j\)-th column of the matrix.The sum of \(m\) over all testcases doesn't exceed \(10^5\).
|
For each testcase print a single integer β the score of the game if both players play optimally.
|
The paths for the testcases are shown on the following pictures. Alice's path is depicted in red and Bob's path is depicted in blue.
|
Input: 3 3 1 3 7 3 5 1 3 1 3 9 3 5 1 1 4 7 | Output: 7 8 0
|
Easy
| 4 | 952 | 545 | 96 | 15 |
317 |
E
|
317E
|
E. Princess and Her Shadow
| 3,100 |
constructive algorithms; shortest paths
|
Princess Vlada enjoys springing in the meadows and walking in the forest. One day β wonderful, sunny day β during her walk Princess found out with astonishment that her shadow was missing! ""Blimey!"", β she thought and started her search of the shadow in the forest.Normally the Shadow is too lazy and simply sleeps under the Princess. But at this terrifically hot summer day she got bored of such a dull life, so she decided to play with Vlada.The forest, where our characters entertain themselves, may be represented as a set of integer cells in the plane, where the Shadow and the Princess can move only up, down, left and right by 1. Some cells (as it happens in decent forests) are occupied by trees. The Shadow and the Princess are not allowed to enter a cell occupied by a tree. Unfortunately, these are the hard times for the forest, so there are very few trees growing here...At first the Princess was walking within the cell (vx, vy), while the Shadow hid from the Princess in the cell (sx, sy). The Princess, The Shadow and the trees are located in the different cells.The Shadow is playing with the Princess. As soon as the Princess moves by 1 in some direction, the Shadow simultaneously flies by 1 in the same direction, if it is possible (if the cell to fly to is not occupied by some tree); otherwise, the Shadow doesn't move. The Shadow is very shadowy, so our characters do not interfere with each other.We say that the Shadow is caught by the Princess if after some move both of them are located in the same cell. Vlada managed to catch her Shadow! Can you?
|
First line of the input contains the coordinates of the characters vx, vy, sx, sy and the number of trees m (0 β€ m β€ 400). The following m lines contain the coordinates of the trees.All the coordinates are integers between -100 and 100, inclusive. The Princess, The Shadow and the trees are located in the different cells.
|
If it is impossible for the Princess to catch the Shadow, print ""-1"" (without quotes).Otherwise print a sequence of characters ""L"", ""R"", ""D"", ""U"", corresponding to the Princess's moves, following which she will be able to catch the Shadow at some turn (L β move to the left, R β to the right, U β up, D β down; axis x is directed to the right, y β up).The number of characters (that is, the number of moves) must not exceed 106. All the Princess's moves should be correct, that is must not lead to the cell where a tree grows. It is allowed for the Princess and the Shadow to occupy the same cell before the last turn.
|
Below the pictures for the samples are given (Princess, Shadow and the trees are colored in pink, gray and black correspondingly; the blue dot marks the lattice center).In the first case the Princess may make two left steps, one step upwards and one right step: In the following case the Princess cannot catch the Shadow: In the last sample the Princess may make two left steps and one down step (in any order):
|
Input: 0 0 1 0 10 1 | Output: LLUR
|
Master
| 2 | 1,577 | 322 | 628 | 3 |
1,716 |
B
|
1716B
|
B. Permutation Chain
| 800 |
constructive algorithms; math
|
A permutation of length \(n\) is a sequence of integers from \(1\) to \(n\) such that each integer appears in it exactly once.Let the fixedness of a permutation \(p\) be the number of fixed points in it β the number of positions \(j\) such that \(p_j = j\), where \(p_j\) is the \(j\)-th element of the permutation \(p\).You are asked to build a sequence of permutations \(a_1, a_2, \dots\), starting from the identity permutation (permutation \(a_1 = [1, 2, \dots, n]\)). Let's call it a permutation chain. Thus, \(a_i\) is the \(i\)-th permutation of length \(n\).For every \(i\) from \(2\) onwards, the permutation \(a_i\) should be obtained from the permutation \(a_{i-1}\) by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation \(a_i\) should be strictly lower than the fixedness of the permutation \(a_{i-1}\).Consider some chains for \(n = 3\): \(a_1 = [1, 2, 3]\), \(a_2 = [1, 3, 2]\) β that is a valid chain of length \(2\). From \(a_1\) to \(a_2\), the elements on positions \(2\) and \(3\) get swapped, the fixedness decrease from \(3\) to \(1\). \(a_1 = [2, 1, 3]\), \(a_2 = [3, 1, 2]\) β that is not a valid chain. The first permutation should always be \([1, 2, 3]\) for \(n = 3\). \(a_1 = [1, 2, 3]\), \(a_2 = [1, 3, 2]\), \(a_3 = [1, 2, 3]\) β that is not a valid chain. From \(a_2\) to \(a_3\), the elements on positions \(2\) and \(3\) get swapped but the fixedness increase from \(1\) to \(3\). \(a_1 = [1, 2, 3]\), \(a_2 = [3, 2, 1]\), \(a_3 = [3, 1, 2]\) β that is a valid chain of length \(3\). From \(a_1\) to \(a_2\), the elements on positions \(1\) and \(3\) get swapped, the fixedness decrease from \(3\) to \(1\). From \(a_2\) to \(a_3\), the elements on positions \(2\) and \(3\) get swapped, the fixedness decrease from \(1\) to \(0\). Find the longest permutation chain. If there are multiple longest answers, print any of them.
|
The first line contains a single integer \(t\) (\(1 \le t \le 99\)) β the number of testcases.The only line of each testcase contains a single integer \(n\) (\(2 \le n \le 100\)) β the required length of permutations in the chain.
|
For each testcase, first, print the length of a permutation chain \(k\).Then print \(k\) permutations \(a_1, a_2, \dots, a_k\). \(a_1\) should be an identity permutation of length \(n\) (\([1, 2, \dots, n]\)). For each \(i\) from \(2\) to \(k\), \(a_i\) should be obtained by swapping two elements in \(a_{i-1}\). It should also have a strictly lower fixedness than \(a_{i-1}\).
|
Input: 223 | Output: 2 1 2 2 1 3 1 2 3 3 2 1 3 1 2
|
Beginner
| 2 | 1,902 | 230 | 378 | 17 |
|
1,297 |
D
|
1297D
|
D. Bonus Distribution
| 0 |
*special; binary search; greedy; sortings
|
For the first time, Polycarp's startup ended the year with a profit! Now he is about to distribute \(k\) burles as a bonus among \(n\) employees.It is known that the current salary of the \(i\)-th employee is \(a_i\) and all the values of \(a_i\) in the company are different.Polycarp wants to distribute the \(k\) burles between \(n\) employees so this month the \(i\)-th employee will be paid not \(a_i\), but \(a_i+d_i\) (\(d_i \ge 0\), \(d_i\) is an integer), where \(d_i\) is the bonus for the \(i\)-th employee. Of course, \(d_1+d_2+\dots+d_n=k\).Polycarp will follow two rules for choosing the values \(d_i\): the relative order of the salaries should not be changed: the employee with originally the highest salary (\(a_i\) is the maximum) should have the highest total payment after receiving their bonus (\(a_i+d_i\) is also the maximum), the employee whose salary was originally the second-largest should receive the second-largest total payment after receiving their bonus and so on. to emphasize that annual profit is a group effort, Polycarp wants to minimize the maximum total payment to an employee (i.e minimize the maximum value of \(a_i+d_i\)). Help Polycarp decide the non-negative integer bonuses \(d_i\) such that: their sum is \(k\), for each employee, the number of those who receive strictly more than them remains unchanged (that is, if you sort employees by \(a_i\) and by \(a_i+d_i\), you get the same order of employees), all \(a_i + d_i\) are different, the maximum of the values \(a_i+d_i\) is the minimum possible. Help Polycarp and print any of the possible answers \(d_1, d_2, \dots, d_n\).
|
The first line contains an 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 each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^5\), \(1 \le k \le 10^9\)) β the number of employees and the total bonus.The second line of each test case contains \(n\) different integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the current salary of the \(i\)-th employee.It is guaranteed that the sum of all \(n\) values in the input does not exceed \(10^5\).
|
Print the answers to \(t\) test cases in the order they appear in the input. Print each answer as a sequence of non-negative integers \(d_1, d_2, \dots, d_n\). If there are several answers, print any of them.
|
Input: 5 4 1 3 1 4 2 2 3 10 2 4 1000000000 987654321 1000000000 999999999 500000000 8 9 5 6 1 8 3 4 2 7 6 1 6 3 1 8 5 9 | Output: 0 0 1 0 0 3 134259259 121913582 121913582 621913577 2 2 0 2 0 1 0 2 1 0 0 0 0 0
|
Beginner
| 4 | 1,624 | 564 | 208 | 12 |
|
1,881 |
F
|
1881F
|
F. Minimum Maximum Distance
| 1,700 |
dfs and similar; dp; graphs; shortest paths; trees
|
You have a tree with \(n\) vertices, some of which are marked. A tree is a connected undirected graph without cycles.Let \(f_i\) denote the maximum distance from vertex \(i\) to any of the marked vertices.Your task is to find the minimum value of \(f_i\) among all vertices. For example, in the tree shown in the example, vertices \(2\), \(6\), and \(7\) are marked. Then the array \(f(i) = [2, 3, 2, 4, 4, 3, 3]\). The minimum \(f_i\) is for vertices \(1\) and \(3\).
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree and the number of marked vertices, respectively.The second line of each test case contains \(k\) integers \(a_i\) (\(1 \le a_i \le n, a_{i-1} < a_i\)) β the indices of the marked vertices.The next \(n - 1\) lines contain two integers \(u_i\) and \(v_i\) β the indices of vertices connected by the \(i\)-th edge.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the minimum value of \(f_i\) among all vertices.
|
Input: 67 32 6 71 21 32 42 53 63 74 41 2 3 41 22 33 45 111 21 31 41 55 24 51 22 31 44 510 81 2 3 4 5 8 9 102 1010 55 33 11 77 44 98 96 110 91 2 4 5 6 7 8 9 101 33 99 44 1010 66 77 22 55 8 | Output: 2 2 0 1 4 5
|
Medium
| 5 | 468 | 639 | 94 | 18 |
|
946 |
E
|
946E
|
E. Largest Beautiful Number
| 2,200 |
greedy; implementation
|
Yes, that's another problem with definition of ""beautiful"" numbers.Let's call a positive integer x beautiful if its decimal representation without leading zeroes contains even number of digits, and there exists a permutation of this representation which is palindromic. For example, 4242 is a beautiful number, since it contains 4 digits, and there exists a palindromic permutation 2442.Given a positive integer s, find the largest beautiful number which is less than s.
|
The first line contains one integer t (1 β€ t β€ 105) β the number of testcases you have to solve.Then t lines follow, each representing one testcase and containing one string which is the decimal representation of number s. It is guaranteed that this string has even length, contains no leading zeroes, and there exists at least one beautiful number less than s.The sum of lengths of s over all testcases doesn't exceed 2Β·105.
|
For each testcase print one line containing the largest beautiful number which is less than s (it is guaranteed that the answer exists).
|
Input: 48988100028923845 | Output: 88779928923839
|
Hard
| 2 | 472 | 425 | 136 | 9 |
|
1,423 |
B
|
1423B
|
B. Valuable Paper
| 1,900 |
binary search; flows; graph matchings; graphs
|
The pandemic is upon us, and the world is in shortage of the most important resource: toilet paper. As one of the best prepared nations for this crisis, BubbleLand promised to help all other world nations with this valuable resource. To do that, the country will send airplanes to other countries carrying toilet paper.In BubbleLand, there are \(N\) toilet paper factories, and \(N\) airports. Because of how much it takes to build a road, and of course legal issues, every factory must send paper to only one airport, and every airport can only take toilet paper from one factory.Also, a road can't be built between all airport-factory pairs, again because of legal issues. Every possible road has number \(d\) given, number of days it takes to build that road.Your job is to choose \(N\) factory-airport pairs, such that if the country starts building all roads at the same time, it takes the least amount of days to complete them.
|
The first line contains two integers \(N\) \((1 \leq N \leq 10^4)\) - number of airports/factories, and \(M\) \((1 \leq M \leq 10^5)\) - number of available pairs to build a road between.On next \(M\) lines, there are three integers \(u\), \(v\) \((1 \leq u,v \leq N)\), \(d\) \((1 \leq d \leq 10^9)\) - meaning that you can build a road between airport \(u\) and factory \(v\) for \(d\) days.
|
If there are no solutions, output -1. If there exists a solution, output the minimal number of days to complete all roads, equal to maximal \(d\) among all chosen roads.
|
Input: 3 5 1 2 1 2 3 2 3 3 3 2 1 4 2 2 5 | Output: 4
|
Hard
| 4 | 933 | 393 | 169 | 14 |
|
466 |
A
|
466A
|
A. Cheap Travel
| 1,200 |
implementation
|
Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket for m rides (she can buy it several times). It costs b rubles. Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of money she will have to spend to make n rides?
|
The single line contains four space-separated integers n, m, a, b (1 β€ n, m, a, b β€ 1000) β the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride ticket.
|
Print a single integer β the minimum sum in rubles that Ann will need to spend.
|
In the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy three m ride tickets.
|
Input: 6 2 1 2 | Output: 6
|
Easy
| 1 | 366 | 244 | 79 | 4 |
467 |
A
|
467A
|
A. George and Accommodation
| 800 |
implementation
|
George has recently entered the BSUCP (Berland State University for Cool Programmers). George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has n rooms in total. At the moment the i-th room has pi people living in it and the room can accommodate qi people in total (pi β€ qi). Your task is to count how many rooms has free place for both George and Alex.
|
The first line contains a single integer n (1 β€ n β€ 100) β the number of rooms.The i-th of the next n lines contains two integers pi and qi (0 β€ pi β€ qi β€ 100) β the number of people who already live in the i-th room and the room's capacity.
|
Print a single integer β the number of rooms where George and Alex can move in.
|
Input: 31 12 23 3 | Output: 0
|
Beginner
| 1 | 461 | 241 | 79 | 4 |
|
367 |
D
|
367D
|
D. Sereja and Sets
| 2,400 |
bitmasks; dfs and similar
|
Sereja has m non-empty sets of integers A1, A2, ..., Am. What a lucky coincidence! The given sets are a partition of the set of all integers from 1 to n. In other words, for any integer v (1 β€ v β€ n) there is exactly one set At such that . Also Sereja has integer d.Sereja decided to choose some sets from the sets he has. Let's suppose that i1, i2, ..., ik (1 β€ i1 < i2 < ... < ik β€ m) are indexes of the chosen sets. Then let's define an array of integers b, sorted in ascending order, as a union of the chosen sets, that is, . We'll represent the element with number j in this array (in ascending order) as bj. Sereja considers his choice of sets correct, if the following conditions are met:b1 β€ d; bi + 1 - bi β€ d (1 β€ i < |b|); n - d + 1 β€ b|b|.Sereja wants to know what is the minimum number of sets (k) that he can choose so that his choice will be correct. Help him with that.
|
The first line contains integers n, m, d (1 β€ d β€ n β€ 105, 1 β€ m β€ 20). The next m lines contain sets. The first number in the i-th line is si (1 β€ si β€ n). This number denotes the size of the i-th set. Then the line contains si distinct integers from 1 to n β set Ai.It is guaranteed that the sets form partition of all integers from 1 to n.
|
In a single line print the answer to the problem β the minimum value k at the right choice.
|
Input: 3 2 21 22 1 3 | Output: 1
|
Expert
| 2 | 885 | 342 | 91 | 3 |
|
1,431 |
B
|
1431B
|
B. Polycarp and the Language of Gods
| 1,400 |
*special; implementation; two pointers
|
Polycarp has just finished writing down the lecture on elvish languages. The language this week was ""VwV"" (pronounced as ""uwu""). The writing system of this language consists of only two lowercase Latin letters: 'v' and 'w'.Unfortunately, Polycarp has written all the lecture in cursive and without any spaces, so the notes look like a neverending sequence of squiggles. To be exact, Polycarp can't tell 'w' apart from 'vv' in his notes as both of them consist of the same two squiggles.Luckily, his brother Monocarp has better writing habits, so Polycarp managed to take his notes and now wants to make his own notes more readable. To do that he can follow the notes of Monocarp and underline some letters in his own notes in such a way that there is no more ambiguity. If he underlines a 'v', then it can't be mistaken for a part of some 'w', and if the underlines a 'w', then it can't be mistaken for two adjacent letters 'v'.What is the minimum number of letters Polycarp should underline to make his notes unambiguous?
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of testcases.Each of the next \(t\) lines contains a non-empty string in VwV language, which consists only of lowercase Latin letters 'v' and 'w'. The length of the string does not exceed \(100\).
|
For each testcase print a single integer: the minimum number of letters Polycarp should underline so that there is no ambiguity in his notes.
|
In the first testcase it's enough to underline any of the two letters 'v'.In the second testcase the letter 'v' is not ambiguous by itself already, so you don't have to underline anything.In the third testcase you have to underline 'w', so that you don't mix it up with two letters 'v'.In the fourth testcase you can underline 'w' to avoid all the ambiguity.In the fifth testcase you can underline both letters 'w' and any of the two letters 'v' between them.
|
Input: 5 vv v w vwv vwvvwv | Output: 1 0 1 1 3
|
Easy
| 3 | 1,026 | 278 | 141 | 14 |
1,336 |
D
|
1336D
|
D. Yui and Mahjong Set
| 3,200 |
constructive algorithms; interactive
|
This is an interactive problem.Yui is a girl who enjoys playing Mahjong. She has a mysterious set which consists of tiles (this set can be empty). Each tile has an integer value between \(1\) and \(n\), and at most \(n\) tiles in the set have the same value. So the set can contain at most \(n^2\) tiles.You want to figure out which values are on the tiles. But Yui is shy, she prefers to play a guessing game with you.Let's call a set consisting of three tiles triplet if their values are the same. For example, \(\{2,\,2,\,2\}\) is a triplet, but \(\{2,\,3,\,3\}\) is not.Let's call a set consisting of three tiles straight if their values are consecutive integers. For example, \(\{2,\,3,\,4\}\) is a straight, but \(\{1,\,3,\,5\}\) is not.At first, Yui gives you the number of triplet subsets and straight subsets of the initial set respectively. After that, you can insert a tile with an integer value between \(1\) and \(n\) into the set at most \(n\) times. Every time you insert a tile, you will get the number of triplet subsets and straight subsets of the current set as well.Note that two tiles with the same value are treated different. In other words, in the set \(\{1,\,1,\,2,\,2,\,3\}\) you can find \(4\) subsets \(\{1,\,2,\,3\}\).Try to guess the number of tiles in the initial set with value \(i\) for all integers \(i\) from \(1\) to \(n\).
|
The first line contains a single integer \(n\) (\(4 \le n \le 100\)).The second line contains two integers which represent the number of triplet subsets and straight subsets of the initial set respectively.
|
When you are ready to answer, print a single line of form ""! \(a_1\) \(a_2\) \(\ldots\) \(a_n\)"" (\(0 \le a_i \le n\)), where \(a_i\) is equal to the number of tiles in the initial set with value \(i\).
|
In the first test, the initial set of tiles is \(\{1, 1, 2, 3, 3, 3, 5, 5\}\). It has only one triplet subset \(\{3, 3, 3\}\) and six straight subsets, all equal to \(\{1, 2, 3\}\). After inserting a tile with value \(1\) the set of tiles will be \(\{1, 1, 1, 2, 3, 3, 3, 5, 5\}\) and will have two triplet subsets \(\{1, 1, 1\}\), \(\{3, 3, 3\}\) and nine straight subsets, all equal to \(\{1, 2, 3\}\).
|
Input: 5 1 6 2 9 5 12 5 24 6 24 | Output: + 1 + 1 + 2 + 5 ! 2 1 3 0 2
|
Master
| 2 | 1,359 | 206 | 204 | 13 |
2,031 |
B
|
2031B
|
B. Penchick and Satay Sticks
| 900 |
brute force; greedy; sortings
|
Penchick and his friend Kohane are touring Indonesia, and their next stop is in Surabaya!In the bustling food stalls of Surabaya, Kohane bought \(n\) satay sticks and arranged them in a line, with the \(i\)-th satay stick having length \(p_i\). It is given that \(p\) is a permutation\(^{\text{β}}\) of length \(n\).Penchick wants to sort the satay sticks in increasing order of length, so that \(p_i=i\) for each \(1\le i\le n\). For fun, they created a rule: they can only swap neighboring satay sticks whose lengths differ by exactly \(1\). Formally, they can perform the following operation any number of times (including zero): Select an index \(i\) (\(1\le i\le n-1\)) such that \(|p_{i+1}-p_i|=1\); Swap \(p_i\) and \(p_{i+1}\). Determine whether it is possible to sort the permutation \(p\), thus the satay sticks, by performing the above operation.\(^{\text{β}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot 10^5\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) β the number of satay sticks.The second line of each test case contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) β the permutation \(p\) representing the length of the satay sticks.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 sort permutation \(p\) by performing the operation. Otherwise, output ""NO"".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, we can sort permutation \(p = [2, 1, 3, 4]\) by performing an operation on index \(1\) (\(|p_2 - p_1| = |1 - 2| = 1\)), resulting in \(p = [1, 2, 3, 4]\).In the second test case, it can be proven that it is impossible to sort permutation \(p = [4, 2, 3, 1]\) by performing the operation. Here is an example of a sequence of operations that can be performed on the permutation: Select \(i = 2\) (\(|p_3 - p_2| = |3 - 2| = 1\)). This results in \(p = [4, 3, 2, 1]\). Select \(i = 1\) (\(|p_2 - p_1| = |3 - 4| = 1\)). This results in \(p = [3, 4, 2, 1]\). Select \(i = 3\) (\(|p_4 - p_3| = |1 - 2| = 1\)). This results in \(p = [3, 4, 1, 2]\). Unfortunately, permutation \(p\) remains unsorted after performing the operations.
|
Input: 242 1 3 444 2 3 1 | Output: YES NO
|
Beginner
| 3 | 1,196 | 557 | 294 | 20 |
1,106 |
E
|
1106E
|
E. Lunar New Year and Red Envelopes
| 2,100 |
data structures; dp
|
Lunar New Year is approaching, and Bob is going to receive some red envelopes with countless money! But collecting money from red envelopes is a time-consuming process itself.Let's describe this problem in a mathematical way. Consider a timeline from time \(1\) to \(n\). The \(i\)-th red envelope will be available from time \(s_i\) to \(t_i\), inclusive, and contain \(w_i\) coins. If Bob chooses to collect the coins in the \(i\)-th red envelope, he can do it only in an integer point of time between \(s_i\) and \(t_i\), inclusive, and he can't collect any more envelopes until time \(d_i\) (inclusive) after that. Here \(s_i \leq t_i \leq d_i\) holds.Bob is a greedy man, he collects coins greedily β whenever he can collect coins at some integer time \(x\), he collects the available red envelope with the maximum number of coins. If there are multiple envelopes with the same maximum number of coins, Bob would choose the one whose parameter \(d\) is the largest. If there are still multiple choices, Bob will choose one from them randomly.However, Alice β his daughter β doesn't want her father to get too many coins. She could disturb Bob at no more than \(m\) integer time moments. If Alice decides to disturb Bob at time \(x\), he could not do anything at time \(x\) and resumes his usual strategy at the time \(x + 1\) (inclusive), which may lead to missing some red envelopes.Calculate the minimum number of coins Bob would get if Alice disturbs him optimally.
|
The first line contains three non-negative integers \(n\), \(m\) and \(k\) (\(1 \leq n \leq 10^5\), \(0 \leq m \leq 200\), \(1 \leq k \leq 10^5\)), denoting the length of the timeline, the number of times Alice can disturb Bob and the total number of red envelopes, respectively.The following \(k\) lines describe those \(k\) red envelopes. The \(i\)-th line contains four positive integers \(s_i\), \(t_i\), \(d_i\) and \(w_i\) (\(1 \leq s_i \leq t_i \leq d_i \leq n\), \(1 \leq w_i \leq 10^9\)) β the time segment when the \(i\)-th envelope is available, the time moment Bob can continue collecting after collecting the \(i\)-th envelope, and the number of coins in this envelope, respectively.
|
Output one integer β the minimum number of coins Bob would get if Alice disturbs him optimally.
|
In the first sample, Alice has no chance to disturb Bob. Therefore Bob will collect the coins in the red envelopes at time \(1\) and \(5\), collecting \(13\) coins in total.In the second sample, Alice should disturb Bob at time \(1\). Therefore Bob skips the first envelope, collects the second one and can not do anything after that. So the answer is \(2\).
|
Input: 5 0 2 1 3 4 5 2 5 5 8 | Output: 13
|
Hard
| 2 | 1,473 | 696 | 95 | 11 |
1,506 |
D
|
1506D
|
D. Epic Transformation
| 1,400 |
constructive algorithms; data structures; greedy
|
You are given an array \(a\) of length \(n\) consisting of integers. You can apply the following operation, consisting of several steps, on the array \(a\) zero or more times: you select two different numbers in the array \(a_i\) and \(a_j\); you remove \(i\)-th and \(j\)-th elements from the array. For example, if \(n=6\) and \(a=[1, 6, 1, 1, 4, 4]\), then you can perform the following sequence of operations: select \(i=1, j=5\). The array \(a\) becomes equal to \([6, 1, 1, 4]\); select \(i=1, j=2\). The array \(a\) becomes equal to \([1, 4]\). What can be the minimum size of the array after applying some sequence of operations to it?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) is length of the array \(a\).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 \(2 \cdot 10^5\).
|
For each test case, output the minimum possible size of the array after applying some sequence of operations to it.
|
Input: 5 6 1 6 1 1 4 4 2 1 2 2 1 1 5 4 5 4 5 4 6 2 3 2 1 3 1 | Output: 0 0 2 1 0
|
Easy
| 3 | 643 | 426 | 115 | 15 |
|
768 |
B
|
768B
|
B. Code For 1
| 1,600 |
constructive algorithms; dfs and similar; divide and conquer
|
Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility. Initially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position , , sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.Now the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?
|
The first line contains three integers n, l, r (0 β€ n < 250, 0 β€ r - l β€ 105, r β₯ 1, l β₯ 1) β initial element and the range l to r.It is guaranteed that r is not greater than the length of the final list.
|
Output the total number of 1s in the range l to r in the final sequence.
|
Consider first example:Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.For the second example:Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5.
|
Input: 7 2 5 | Output: 4
|
Medium
| 3 | 1,027 | 204 | 72 | 7 |
1,478 |
A
|
1478A
|
A. Nezzar and Colorful Balls
| 800 |
brute force; greedy
|
Nezzar has \(n\) balls, numbered with integers \(1, 2, \ldots, n\). Numbers \(a_1, a_2, \ldots, a_n\) are written on them, respectively. Numbers on those balls form a non-decreasing sequence, which means that \(a_i \leq a_{i+1}\) for all \(1 \leq i < n\).Nezzar wants to color the balls using the minimum number of colors, such that the following holds. For any color, numbers on balls will form a strictly increasing sequence if he keeps balls with this chosen color and discards all other balls. Note that a sequence with the length at most \(1\) is considered as a strictly increasing sequence.Please help Nezzar determine the minimum number of colors.
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of testcases. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)).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 \(a_1 \leq a_2 \leq \ldots \leq a_n\).
|
For each test case, output the minimum number of colors Nezzar can use.
|
Let's match each color with some numbers. Then:In the first test case, one optimal color assignment is \([1,2,3,3,2,1]\).In the second test case, one optimal color assignment is \([1,2,1,2,1]\).
|
Input: 5 6 1 1 1 2 3 4 5 1 1 2 2 3 4 2 2 2 2 3 1 2 3 1 1 | Output: 3 2 4 1 1
|
Beginner
| 2 | 655 | 347 | 71 | 14 |
934 |
B
|
934B
|
B. A Prosperous Lot
| 1,200 |
constructive algorithms; implementation
|
Apart from Nian, there is a daemon named Sui, which terrifies children and causes them to become sick. Parents give their children money wrapped in red packets and put them under the pillow, so that when Sui tries to approach them, it will be driven away by the fairies inside.Big Banban is hesitating over the amount of money to give out. He considers loops to be lucky since it symbolizes unity and harmony.He would like to find a positive integer n not greater than 1018, such that there are exactly k loops in the decimal representation of n, or determine that such n does not exist.A loop is a planar area enclosed by lines in the digits' decimal representation written in Arabic numerals. For example, there is one loop in digit 4, two loops in 8 and no loops in 5. Refer to the figure below for all exact forms.
|
The first and only line contains an integer k (1 β€ k β€ 106) β the desired number of loops.
|
Output an integer β if no such n exists, output -1; otherwise output any such n. In the latter case, your output should be a positive decimal integer not exceeding 1018.
|
Input: 2 | Output: 462
|
Easy
| 2 | 818 | 90 | 169 | 9 |
|
552 |
C
|
552C
|
C. Vanya and Scales
| 1,900 |
brute force; dp; greedy; math; meet-in-the-middle; number theory
|
Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
|
The first line contains two integers w, m (2 β€ w β€ 109, 1 β€ m β€ 109) β the number defining the masses of the weights and the mass of the item.
|
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
|
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.
|
Input: 3 7 | Output: YES
|
Hard
| 6 | 545 | 142 | 67 | 5 |
730 |
L
|
730L
|
L. Expression Queries
| 3,200 |
data structures
|
A simplified arithmetic expression (SAE) is an arithmetic expression defined by the following grammar: <SAE> ::= <Number> | <SAE>+<SAE> | <SAE>*<SAE> | (<SAE>) <Number> ::= <Digit> | <Digit><Number> <Digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 In other words it's a correct arithmetic expression that is allowed to contain brackets, numbers (possibly with leading zeros), multiplications and additions. For example expressions ""(0+01)"", ""0"" and ""1*(0)"" are simplified arithmetic expressions, but expressions ""2-1"", ""+1"" and ""1+2)"" are not.Given a string s1s2...s|s| that represents a SAE; si denotes the i-th character of the string which can be either a digit ('0'-'9'), a plus sign ('+'), a multiplication sign ('*'), an opening round bracket '(' or a closing round bracket ')'.A part slsl + 1...sr of this string is called a sub-expression if and only if it is a SAE.You task is to answer m queries, each of which is a pair of integers li, ri (1 β€ li β€ ri β€ |s|). For each query determine whether the corresponding part of the given string is a sub-expression and in case it's a sub-expression calculate its value modulo 1000000007 (109 + 7). The values should be calculated using standard operator priorities.
|
The first line of the input contains non-empty string s (1 β€ |s| β€ 4Β·105) which represents a correct SAE. Each character of the string can be one of the following characters: '*', '+', '(', ')' or a digit ('0'-'9'). The expression might contain extra-huge numbers.The second line contains an integer m (1 β€ m β€ 4Β·105) which is the number of queries. Each of the next m lines contains two space-separated integers li, ri (1 β€ li β€ ri β€ |s|) β the i-th query.
|
The i-th number of output should be the answer for the i-th query. If the i-th query corresponds to a valid sub-expression output the value of the sub-expression modulo 1000000007 (109 + 7). Otherwise output -1 as an answer for the query. Print numbers on separate lines.
|
Input: ((1+2)*3+101*2)68 141 62 1011 145 54 5 | Output: 205-11022-1
|
Master
| 1 | 1,230 | 457 | 271 | 7 |
|
1,905 |
F
|
1905F
|
F. Field Should Not Be Empty
| 2,600 |
brute force; data structures; divide and conquer
|
You are given a permutation\(^{\dagger}\) \(p\) of length \(n\).We call index \(x\) good if for all \(y < x\) it holds that \(p_y < p_x\) and for all \(y > x\) it holds that \(p_y > p_x\). We call \(f(p)\) the number of good indices in \(p\). You can perform the following operation: pick \(2\) distinct indices \(i\) and \(j\) and swap elements \(p_i\) and \(p_j\). Find the maximum value of \(f(p)\) after applying the aforementioned operation exactly once.\(^{\dagger}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test consists of multiple test cases. The first line of contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the length of the permutation \(p\).The second line of each test case contain \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\)) β the elements of the permutation \(p\).It is guaranteed that sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer β the maximum value of \(f(p)\) after performing the operation exactly once.
|
In the first test case, \(p = [1,2,3,4,5]\) and \(f(p)=5\) which is already maximum possible. But must perform the operation anyway. We can get \(f(p)=3\) by choosing \(i=1\) and \(j=2\) which makes \(p = [2,1,3,4,5]\).In the second test case, we can transform \(p\) into \([1,2,3,4,5]\) by choosing \(i=1\) and \(j=2\). Thus \(f(p)=5\).
|
Input: 551 2 3 4 552 1 3 4 572 1 5 3 7 6 462 3 5 4 1 677 6 5 4 3 2 1 | Output: 3 5 2 3 2
|
Expert
| 3 | 797 | 570 | 120 | 19 |
1,175 |
F
|
1175F
|
F. The Number of Subpermutations
| 2,500 |
brute force; data structures; divide and conquer; hashing; math
|
You have an array \(a_1, a_2, \dots, a_n\). Let's call some subarray \(a_l, a_{l + 1}, \dots , a_r\) of this array a subpermutation if it contains all integers from \(1\) to \(r-l+1\) exactly once. For example, array \(a = [2, 2, 1, 3, 2, 3, 1]\) contains \(6\) subarrays which are subpermutations: \([a_2 \dots a_3]\), \([a_2 \dots a_4]\), \([a_3 \dots a_3]\), \([a_3 \dots a_5]\), \([a_5 \dots a_7]\), \([a_7 \dots a_7]\).You are asked to calculate the number of subpermutations.
|
The first line contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots , a_n\) (\(1 \le a_i \le n\)). This array can contain the same integers.
|
Print the number of subpermutations of the array \(a\).
|
There are \(7\) subpermutations in the first test case. Their segments of indices are \([1, 4]\), \([3, 3]\), \([3, 6]\), \([4, 7]\), \([6, 7]\), \([7, 7]\) and \([7, 8]\).In the second test case \(6\) subpermutations exist: \([1, 1]\), \([2, 2]\), \([2, 3]\), \([3, 4]\), \([4, 4]\) and \([4, 5]\).
|
Input: 8 2 4 1 3 4 2 1 2 | Output: 7
|
Expert
| 5 | 481 | 203 | 55 | 11 |
525 |
C
|
525C
|
C. Ilya and Sticks
| 1,600 |
greedy; math; sortings
|
In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of n sticks and an instrument. Each stick is characterized by its length li.Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed.Sticks with lengths a1, a2, a3 and a4 can make a rectangle if the following properties are observed: a1 β€ a2 β€ a3 β€ a4 a1 = a2 a3 = a4 A rectangle can be made of sticks with lengths of, for example, 3 3 3 3 or 2 2 4 4. A rectangle cannot be made of, for example, sticks 5 5 5 7.Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4.You have to answer the question β what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks?
|
The first line of the input contains a positive integer n (1 β€ n β€ 105) β the number of the available sticks.The second line of the input contains n positive integers li (2 β€ li β€ 106) β the lengths of the sticks.
|
The first line of the output must contain a single non-negative integer β the maximum total area of the rectangles that Ilya can make from the available sticks.
|
Input: 42 4 4 2 | Output: 8
|
Medium
| 3 | 1,181 | 213 | 160 | 5 |
|
802 |
I
|
802I
|
I. Fake News (hard)
| 2,300 |
string suffix structures
|
Now that you have proposed a fake post for the HC2 Facebook page, Heidi wants to measure the quality of the post before actually posting it. She recently came across a (possibly fake) article about the impact of fractal structure on multimedia messages and she is now trying to measure the self-similarity of the message, which is defined aswhere the sum is over all nonempty strings p and is the number of occurences of p in s as a substring. (Note that the sum is infinite, but it only has a finite number of nonzero summands.)Heidi refuses to do anything else until she knows how to calculate this self-similarity. Could you please help her? (If you would like to instead convince Heidi that a finite string cannot be a fractal anyway β do not bother, we have already tried.)
|
The input starts with a line indicating the number of test cases T (1 β€ T β€ 10). After that, T test cases follow, each of which consists of one line containing a string s (1 β€ |s| β€ 100 000) composed of lowercase letters (a-z).
|
Output T lines, every line containing one number β the answer to the corresponding test case.
|
A string s contains another string p as a substring if p is a contiguous subsequence of s. For example, ab is a substring of cab but not of acb.
|
Input: 4aaabcdcccabcc | Output: 5101412
|
Expert
| 1 | 778 | 227 | 93 | 8 |
762 |
F
|
762F
|
F. Tree nesting
| 2,800 |
combinatorics; graphs; trees
|
You are given two trees (connected undirected acyclic graphs) S and T.Count the number of subtrees (connected subgraphs) of S that are isomorphic to tree T. Since this number can get quite large, output it modulo 109 + 7.Two subtrees of tree S are considered different, if there exists a vertex in S that belongs to exactly one of them.Tree G is called isomorphic to tree H if there exists a bijection f from the set of vertices of G to the set of vertices of H that has the following property: if there is an edge between vertices A and B in tree G, then there must be an edge between vertices f(A) and f(B) in tree H. And vice versa β if there is an edge between vertices A and B in tree H, there must be an edge between f - 1(A) and f - 1(B) in tree G.
|
The first line contains a single integer |S| (1 β€ |S| β€ 1000) β the number of vertices of tree S.Next |S| - 1 lines contain two integers ui and vi (1 β€ ui, vi β€ |S|) and describe edges of tree S.The next line contains a single integer |T| (1 β€ |T| β€ 12) β the number of vertices of tree T.Next |T| - 1 lines contain two integers xi and yi (1 β€ xi, yi β€ |T|) and describe edges of tree T.
|
On the first line output a single integer β the answer to the given task modulo 109 + 7.
|
Input: 51 22 33 44 531 22 3 | Output: 3
|
Master
| 3 | 755 | 387 | 88 | 7 |
|
1,443 |
C
|
1443C
|
C. The Delivery Dilemma
| 1,400 |
binary search; greedy; sortings
|
Petya is preparing for his birthday. He decided that there would be \(n\) different dishes on the dinner table, numbered from \(1\) to \(n\). Since Petya doesn't like to cook, he wants to order these dishes in restaurants.Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from \(n\) different places. To speed up this process, he wants to order courier delivery at some restaurants. Thus, for each dish, there are two options for Petya how he can get it: the dish will be delivered by a courier from the restaurant \(i\), in this case the courier will arrive in \(a_i\) minutes, Petya goes to the restaurant \(i\) on his own and picks up the dish, he will spend \(b_i\) minutes on this. Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.For example, if Petya wants to order \(n = 4\) dishes and \(a = [3, 7, 4, 5]\), and \(b = [2, 1, 2, 4]\), then he can order delivery from the first and the fourth restaurant, and go to the second and third on your own. Then the courier of the first restaurant will bring the order in \(3\) minutes, the courier of the fourth restaurant will bring the order in \(5\) minutes, and Petya will pick up the remaining dishes in \(1 + 2 = 3\) minutes. Thus, in \(5\) minutes all the dishes will be at Petya's house.Find the minimum time after which all the dishes can be at Petya's home.
|
The first line contains one positive integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β the number of test cases. Then \(t\) test cases follow.Each test case begins with a line containing one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of dishes that Petya wants to order.The second line of each test case contains \(n\) integers \(a_1 \ldots a_n\) (\(1 \le a_i \le 10^9\)) β the time of courier delivery of the dish with the number \(i\).The third line of each test case contains \(n\) integers \(b_1 \ldots b_n\) (\(1 \le b_i \le 10^9\)) β the time during which Petya will pick up the dish with the number \(i\).The sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output one integer β the minimum time after which all dishes can be at Petya's home.
|
Input: 4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2 | Output: 5 3 2 3
|
Easy
| 3 | 1,583 | 694 | 103 | 14 |
|
1,791 |
C
|
1791C
|
C. Prepend and Append
| 800 |
implementation; two pointers
|
Timur initially had a binary string\(^{\dagger}\) \(s\) (possibly of length \(0\)). He performed the following operation several (possibly zero) times: Add \(\texttt{0}\) to one end of the string and \(\texttt{1}\) to the other end of the string. For example, starting from the string \(\texttt{1011}\), you can obtain either \(\color{red}{\texttt{0}}\texttt{1011}\color{red}{\texttt{1}}\) or \(\color{red}{\texttt{1}}\texttt{1011}\color{red}{\texttt{0}}\). You are given Timur's final string. What is the length of the shortest possible string he could have started with?\(^{\dagger}\) A binary string is a string (possibly the empty string) whose characters are either \(\texttt{0}\) or \(\texttt{1}\).
|
The first line of the input contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of testcases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2000\)) β the length of Timur's final string.The second line of each test case contains a string \(s\) of length \(n\) consisting of characters \(\texttt{0}\) or \(\texttt{1}\), denoting the final string.
|
For each test case, output a single nonnegative integer β the shortest possible length of Timur's original string. Note that Timur's original string could have been empty, in which case you should output \(0\).
|
In the first test case, the shortest possible string Timur started with is \(\texttt{0}\), and he performed the following operation: \(\texttt{0} \to \color{red}{\texttt{1}}\texttt{0}\color{red}{\texttt{0}}\).In the second test case, the shortest possible string Timur started with is \(\texttt{11}\), and he performed the following operation: \(\texttt{11} \to \color{red}{\texttt{0}}\texttt{11}\color{red}{\texttt{1}}\).In the third test case, the shortest possible string Timur started with is \(\texttt{10101}\), and he didn't perform any operations.In the fourth test case, the shortest possible string Timur started with is the empty string (which we denote by \(\varepsilon\)), and he performed the following operations: \(\varepsilon \to \color{red}{\texttt{1}}\texttt{}\color{red}{\texttt{0}} \to \color{red}{\texttt{0}}\texttt{10}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{0101}\color{red}{\texttt{0}}\).In the fifth test case, the shortest possible string Timur started with is \(\texttt{101}\), and he performed the following operations: \(\texttt{101} \to \color{red}{\texttt{0}}\texttt{101}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{01011}\color{red}{\texttt{0}}\).
|
Input: 931004011151010161010107101011011210211101011011010 | Output: 1 2 5 0 3 1 0 2 4
|
Beginner
| 2 | 704 | 384 | 210 | 17 |
1,231 |
C
|
1231C
|
C. Increasing Matrix
| 1,100 |
greedy
|
In this problem, a \(n \times m\) rectangular matrix \(a\) is called increasing if, for each row of \(i\), when go from left to right, the values strictly increase (that is, \(a_{i,1}<a_{i,2}<\dots<a_{i,m}\)) and for each column \(j\), when go from top to bottom, the values strictly increase (that is, \(a_{1,j}<a_{2,j}<\dots<a_{n,j}\)).In a given matrix of non-negative integers, it is necessary to replace each value of \(0\) with some positive integer so that the resulting matrix is increasing and the sum of its elements is maximum, or find that it is impossible.It is guaranteed that in a given value matrix all values of \(0\) are contained only in internal cells (that is, not in the first or last row and not in the first or last column).
|
The first line contains integers \(n\) and \(m\) (\(3 \le n, m \le 500\)) β the number of rows and columns in the given matrix \(a\).The following lines contain \(m\) each of non-negative integers β the values in the corresponding row of the given matrix: \(a_{i,1}, a_{i,2}, \dots, a_{i,m}\) (\(0 \le a_{i,j} \le 8000\)).It is guaranteed that for all \(a_{i,j}=0\), \(1 < i < n\) and \(1 < j < m\) are true.
|
If it is possible to replace all zeros with positive numbers so that the matrix is increasing, print the maximum possible sum of matrix elements. Otherwise, print -1.
|
In the first example, the resulting matrix is as follows: 1 3 5 6 73 6 7 8 95 7 8 9 108 9 10 11 12In the second example, the value \(3\) must be put in the middle cell.In the third example, the desired resultant matrix does not exist.
|
Input: 4 5 1 3 5 6 7 3 0 7 0 9 5 0 0 0 10 8 9 10 11 12 | Output: 144
|
Easy
| 1 | 748 | 408 | 166 | 12 |
435 |
E
|
435E
|
E. Special Graph
| 2,500 |
In this problem you will need to deal with an n Γ m grid graph. The graph's vertices are the nodes of the n Γ m grid. The graph's edges are all the sides and diagonals of the grid's unit squares.The figure below shows a 3 Γ 5 graph. The black lines are the graph's edges, the colored circles are the graph's vertices. The vertices of the graph are painted on the picture for a reason: the coloring is a correct vertex coloring of the 3 Γ 5 graph into four colors. A graph coloring is correct if and only if each vertex is painted and no two vertices connected by an edge are painted the same color. You are given the size of the grid graph n Γ m and the colors of some of its vertices. Find any way how to paint the unpainted vertices of the graph in 4 colors to make the final coloring a correct vertex graph coloring. If there is no such correct vertex coloring, say that the answer doesn't exist.
|
The first line contains two integers n and m (2 β€ n, m β€ 1000). Each of the next n lines consists of m characters β the given graph. Each character is either Β«0Β», Β«1Β», Β«2Β», Β«3Β», Β«4Β». Character Β«0Β» means that the corresponding vertex is unpainted, otherwise the character means the color of the vertex.Assume that all the available colors are numbered from 1 to 4.
|
If there is no way to get correct vertex coloring of the graph, print 0 in a single line. Otherwise print the colored n Γ m graph. Print the graph in the same format as in the input.If multiple answers exist, print any of them.
|
The answer to the first sample is shown on the picture (1 β green color, 2 β blue, 3 β dark blue, 4 β pink).In the second sample there exists 4! answers, each of them is considered correct.In the third sample two vertices with equal colors are connected. So the correct vertex coloring couldn't be obtained.
|
Input: 3 5101010002001000 | Output: 131314242431313
|
Expert
| 0 | 899 | 363 | 227 | 4 |
|
1,849 |
C
|
1849C
|
C. Binary String Copying
| 1,600 |
binary search; brute force; data structures; hashing; strings
|
You are given a string \(s\) consisting of \(n\) characters 0 and/or 1.You make \(m\) copies of this string, let the \(i\)-th copy be the string \(t_i\). Then you perform exactly one operation on each of the copies: for the \(i\)-th copy, you sort its substring \([l_i; r_i]\) (the substring from the \(l_i\)-th character to the \(r_i\)-th character, both endpoints inclusive). Note that each operation affects only one copy, and each copy is affected by only one operation.Your task is to calculate the number of different strings among \(t_1, t_2, \ldots, t_m\). Note that the initial string \(s\) should be counted only if at least one of the copies stays the same after the operation.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the length of \(s\) and the number of copies, respectively.The second line contains \(n\) characters 0 and/or 1 β the string \(s\).Then \(m\) lines follow. The \(i\)-th of them contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) β the description of the operation applied to the \(i\)-th copy.The sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\). The sum of \(m\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
Print one integer β the number of different strings among \(t_1, t_2, \ldots, t_m\).
|
Consider the first example. Copies below are given in order of the input operations. Underlined substrings are substrings that are sorted: 101100 \(\rightarrow\) 011100; 101100 \(\rightarrow\) 011100; 101100 \(\rightarrow\) 101100; 101100 \(\rightarrow\) 101100; 101100 \(\rightarrow\) 000111. There are three different strings among \(t_1, t_2, t_3, t_4, t_5\): 000111, 011100 and 101100.Consider the second example: 100111 \(\rightarrow\) 100111; 100111 \(\rightarrow\) 001111; 100111 \(\rightarrow\) 001111; 100111 \(\rightarrow\) 010111. There are three different strings among \(t_1, t_2, t_3, t_4\): 001111, 010111 and 100111.
|
Input: 36 51011001 21 32 45 51 66 41001112 21 41 31 21 101 1 | Output: 3 3 1
|
Medium
| 5 | 688 | 656 | 84 | 18 |
1,848 |
E
|
1848E
|
E. Vika and Stone Skipping
| 2,600 |
brute force; implementation; math; number theory
|
In Vika's hometown, Vladivostok, there is a beautiful sea.Often you can see kids skimming stones. This is the process of throwing a stone into the sea at a small angle, causing it to fly far and bounce several times off the water surface.Vika has skimmed stones many times and knows that if you throw a stone from the shore perpendicular to the coastline with a force of \(f\), it will first touch the water at a distance of \(f\) from the shore, then bounce off and touch the water again at a distance of \(f - 1\) from the previous point of contact. The stone will continue to fly in a straight line, reducing the distances between the points where it touches the water, until it falls into the sea.Formally, the points at which the stone touches the water surface will have the following coordinates: \(f\), \(f + (f - 1)\), \(f + (f - 1) + (f - 2)\), ... , \(f + (f - 1) + (f - 2) + \ldots + 1\) (assuming that \(0\) is the coordinate of the shoreline).Once, while walking along the embankment of Vladivostok in the evening, Vika saw a group of guys skipping stones across the sea, launching them from the same point with different forces.She became interested in what is the maximum number of guys who can launch a stone with their force \(f_i\), so that all \(f_i\) are different positive integers, and all \(n\) stones touched the water at the point with the coordinate \(x\) (assuming that \(0\) is the coordinate of the shoreline).After thinking a little, Vika answered her question. After that, she began to analyze how the answer to her question would change if she multiplied the coordinate \(x\) by some positive integers \(x_1\), \(x_2\), ... , \(x_q\), which she picked for analysis.Vika finds it difficult to cope with such analysis on her own, so she turned to you for help.Formally, Vika is interested in the answer to her question for the coordinates \(X_1 = x \cdot x_1\), \(X_2 = X_1 \cdot x_2\), ... , \(X_q = X_{q-1} \cdot x_q\). Since the answer for such coordinates can be quite large, find it modulo \(M\). It is guaranteed that \(M\) is prime.
|
The first line of the input contains three integers \(x\) (\(1 \le x \le 10^9\)), \(q\) (\(1 \le q \le 10^5\)) and \(M\) (\(100 \le M \le 2 \cdot 10^9\)) β the initial coordinate for which Vika answered the question on her own, the number of integers \(x_i\) by which Vika will multiply the initial coordinate and prime module \(M\).The second line of the input contains \(q\) integers \(x_1, x_2, x_3, \ldots, x_q\) (\(1 \le x_i \le 10^6\)) β the integers described in the statement.
|
Output \(q\) integers, where the \(i\)-th number corresponds to the answer to Vika's question for the coordinate \(X_i\). Output all the answers modulo \(M\).
|
In the first sample, to make the stone touch the water at a point with coordinate \(2\), it needs to be thrown with a force of \(2\). To make the stone touch the water at a point with coordinate \(2 \cdot 3 = 6\), it needs to be thrown with a force of \(3\) or \(6\).In the second sample, you can skim a stone with a force of \(5\) or \(14\) to make it touch the water at a point with coordinate \(7 \cdot 2 = 14\). For the coordinate \(14 \cdot 13 = 182\), there are \(4\) possible forces: \(20\), \(29\), \(47\), \(182\).
|
Input: 1 2 179 2 3 | Output: 1 2
|
Expert
| 4 | 2,070 | 484 | 158 | 18 |
1,339 |
A
|
1339A
|
A. Filling Diamonds
| 900 |
brute force; dp; implementation; math
|
You have integer \(n\). Calculate how many ways are there to fully cover belt-like area of \(4n-2\) triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. \(2\) coverings are different if some \(2\) triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one.Please look at pictures below for better understanding. On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill. These are the figures of the area you want to fill for \(n = 1, 2, 3, 4\). You have to answer \(t\) independent test cases.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^{4}\)) β the number of test cases.Each of the next \(t\) lines contains a single integer \(n\) (\(1 \le n \le 10^{9}\)).
|
For each test case, print the number of ways to fully cover belt-like area of \(4n-2\) triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed \(10^{18}\).
|
In the first test case, there are the following \(2\) ways to fill the area: In the second test case, there is a unique way to fill the area:
|
Input: 2 2 1 | Output: 2 1
|
Beginner
| 4 | 684 | 185 | 210 | 13 |
1,728 |
B
|
1728B
|
B. Best Permutation
| 800 |
constructive algorithms; greedy
|
Let's define the value of the permutation \(p\) of \(n\) integers \(1\), \(2\), ..., \(n\) (a permutation is an array where each element from \(1\) to \(n\) occurs exactly once) as follows: initially, an integer variable \(x\) is equal to \(0\); if \(x < p_1\), then add \(p_1\) to \(x\) (set \(x = x + p_1\)), otherwise assign \(0\) to \(x\); if \(x < p_2\), then add \(p_2\) to \(x\) (set \(x = x + p_2\)), otherwise assign \(0\) to \(x\); ... if \(x < p_n\), then add \(p_n\) to \(x\) (set \(x = x + p_n\)), otherwise assign \(0\) to \(x\); the value of the permutation is \(x\) at the end of this process. For example, for \(p = [4, 5, 1, 2, 3, 6]\), the value of \(x\) changes as follows: \(0, 4, 9, 0, 2, 5, 11\), so the value of the permutation is \(11\).You are given an integer \(n\). Find a permutation \(p\) of size \(n\) with the maximum possible value among all permutations of size \(n\). If there are several such permutations, you can print any of them.
|
The first line contains one integer \(t\) (\(1 \le t \le 97\)) β the number of test cases.The only line of each test case contains one integer \(n\) (\(4 \le n \le 100\)).
|
For each test case, print \(n\) integers β the permutation \(p\) of size \(n\) with the maximum possible value among all permutations of size \(n\).
|
Input: 3456 | Output: 2 1 3 4 1 2 3 4 5 4 5 1 2 3 6
|
Beginner
| 2 | 969 | 171 | 148 | 17 |
|
1,973 |
D
|
1973D
|
D. Cat, Fox and Maximum Array Split
| 2,400 |
brute force; interactive; math
|
This is an interactive problem.Fox gave Cat two positive integers \(n\) and \(k\). She has a hidden array \(a_1, \ldots , a_n\) of length \(n\), such that \(1 \leq a_i \leq n\) for every \(i\). Now they are going to play the following game:For any two integers \(l, r\) such that \(1 \leq l \leq r \leq n\), define \(f(l, r) = (r - l + 1) \cdot \max\limits_{x=l}^r a_x\). In other words, \(f(l, r)\) is equal to the maximum of the subarray \(a_l, \ldots, a_r\) multiplied by its size.Cat can ask Fox at most \(2 n\) questions about the array. He will tell her two integers \(l\) and \(x\) (\(1 \leq l \leq n, 1 \leq x \leq 10^9\)), and she will tell him one integer \(p\) as the answer β the smallest positive integer \(r\) such that \(f(l, r) = x\), or \(n+1\) if no such \(r\) exists.Now, Cat needs to find the largest value \(m\) such that there exists a sequence \(c_1, \ldots, c_{k-1}\) such that \(1 \leq c_1 < \ldots < c_{k-1} < n\) and \(f(1, c_1) = f(c_1 + 1, c_2) = \ldots = f(c_{k-1}+1, n) = m\). If no such \(m\) exists, he should indicate this and take \(-1\) as the answer. Note that for \(k = 1\), \(m\) is always equal to \(f(1, n)\).In other words, the goal is to find the largest \(m\) such that you can split the array into exactly \(k\) subarrays (\(k\) is the constant given to you in the beginning of the interaction) so that all the subarrays have the product of their length and their maximum equal to \(m\), or determine that no such \(m\) exists. Every element should belong in exactly one of the subarrays.Cat doesn't know what he should do, so he asked you to play the game for him.
|
The hidden arrays in the three testcases are \([1]\), \([1, 2]\) and \([1, 3, 6, 1, 2, 1]\). In the second testcase, no split satisfies the constraints, so the answer is \(-1\). The answer for the first query of the third testcase is \(7\) since no valid \(r\) exists. For the second query of the third testcase, since \(2 \cdot \max(1, 3) = 6\), we will get \(2\) as the answer, since \(r = 1\) doesn't satisfy the constraint.The sample interaction guessed all three answers (\(1, -1\) and \(6\)) correctly, so it received \(1\) after each answer.
|
Input: 3 1 1 1 2 2 1 3 1 6 3 7 2 3 6 1 | Output: ! 1 ? 1 1 ? 2 1 ! -1 ? 1 9 ? 1 6 ? 3 6 ? 4 6 ! 6
|
Expert
| 3 | 1,610 | 0 | 0 | 19 |
||
1,612 |
B
|
1612B
|
B. Special Permutation
| 900 |
constructive algorithms; greedy
|
A permutation of length \(n\) is an array \(p=[p_1,p_2,\dots, p_n]\) which contains every integer from \(1\) to \(n\) (inclusive) exactly once. For example, \(p=[4, 2, 6, 5, 3, 1]\) is a permutation of length \(6\).You are given three integers \(n\), \(a\) and \(b\), where \(n\) is an even number. Print any permutation of length \(n\) that the minimum among all its elements of the left half equals \(a\) and the maximum among all its elements of the right half equals \(b\). Print -1 if no such permutation exists.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)), the number of test cases in the test. The following \(t\) lines contain test case descriptions.Each test case description contains three integers \(n\), \(a\), \(b\) (\(2 \le n \le 100\); \(1 \le a,b \le n\); \(a \ne b\)), where \(n\) is an even number (i.e. \(n \bmod 2 = 0\)).
|
For each test case, print a single line containing any suitable permutation. Print -1 no such permutation exists. If there are multiple answers, print any of them.
|
Input: 7 6 2 5 6 1 3 6 4 3 4 2 4 10 5 3 2 1 2 2 2 1 | Output: 4 2 6 5 3 1 -1 6 4 5 1 3 2 3 2 4 1 -1 1 2 2 1
|
Beginner
| 2 | 517 | 357 | 163 | 16 |
|
1,520 |
D
|
1520D
|
D. Same Differences
| 1,200 |
data structures; hashing; math
|
You are given an array \(a\) of \(n\) integers. Count the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_j - a_i = j - i\).
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output the number of pairs of indices \((i, j)\) such that \(i < j\) and \(a_j - a_i = j - i\).
|
Input: 4 6 3 5 1 4 6 6 3 1 2 3 4 1 3 3 4 6 1 6 3 4 5 6 | Output: 1 3 3 10
|
Easy
| 3 | 142 | 398 | 114 | 15 |
|
1,788 |
F
|
1788F
|
F. XOR, Tree, and Queries
| 2,500 |
bitmasks; constructive algorithms; dfs and similar; dsu; graphs; greedy; implementation; trees
|
You are given a tree of \(n\) vertices. The vertices are numbered from \(1\) to \(n\).You will need to assign a weight to each edge. Let the weight of the \(i\)-th edge be \(a_i\) (\(1 \leq i \leq n-1\)). The weight of each edge should be an integer between \(0\) and \(2^{30}-1\), inclusive.You are given \(q\) conditions. Each condition consists of three integers \(u\), \(v\), and \(x\). This means that the bitwise XOR of all edges on the shortest path from \(u\) to \(v\) should be \(x\).Find out if there exist \(a_1, a_2, \ldots, a_{n-1}\) that satisfy the given conditions. If yes, print a solution such that \(a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}\) is the smallest. Here, \(\oplus\) denotes the bitwise XOR operation.If there are multiple solutions such that \(a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}\) is the smallest, print any.
|
The first line contains two integers \(n\) and \(q\) (\(2 \le n \le 2.5 \cdot 10^5\), \(0 \le q \le 2.5 \cdot 10^5\)).The \(i\)-th of the following \(n-1\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i, y_i \le n\), \(x_i \neq y_i\)), meaning that the \(i\)-th edge connects vertices \(x_i\) and \(y_i\) in the tree.It is guaranteed that the given edges form a tree.The following \(q\) lines contain information about conditions.Each line contains three integers \(u\), \(v\), \(x\) (\(1 \le u, v \le n\), \(u \neq v\), \(0 \le x \le 2^{30}-1\)), meaning that the bitwise XOR of all edges on the shortest path from \(u\) to \(v\) should be \(x\).
|
If there do not exist \(a_1\), \(a_2\), ..., \(a_{n-1}\) that satisfy the given conditions, print ""No"".Otherwise, print ""Yes"" in the first line.Then print \(n-1\) integers on the next line, where the \(i\)-th integer is the weight of the \(i\)-th edge. If there are multiple solutions that satisfy the given conditions, print a solution such that \(a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}\) is the smallest.If there are multiple solutions such that \(a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}\) is the smallest, print any.When printing ""Yes"" or ""No"", you can print each letter in any case (either upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
For the first example, there does not exist a set of edge weights that satisfies the given conditions.For the second example, the two given conditions are \(a_1 \oplus a_2 \oplus a_3=2\) and \(a_4 \oplus a_5=7\). There can be multiple solutions, for example, \((a_1, a_2, a_3, a_4, a_5)=(1, 2, 1, 4, 3)\).For the third example, the two given conditions are \(a_1 \oplus a_2 \oplus a_3=3\) and \(a_1 \oplus a_4 \oplus a_5=5\). There are multiple solutions that satisfy the given conditions. \((a_1, a_2, a_3, a_4, a_5)=(1, 1, 3, 4, 0)\) satisfies the given conditions, but the bitwise XOR of all edge weights is \(7\), which does not have the smallest \(a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}\), so it cannot be the answer.
|
Input: 4 4 1 2 2 3 3 4 1 4 3 2 4 2 1 3 1 2 3 1 | Output: No
|
Expert
| 8 | 850 | 661 | 733 | 17 |
1,559 |
D1
|
1559D1
|
D1. Mocha and Diana (Easy Version)
| 1,400 |
brute force; constructive algorithms; dsu; graphs; greedy; trees
|
This is the easy version of the problem. The only difference between the two versions is the constraint on \(n\). You can make hacks only if all versions of the problem are solved.A forest is an undirected graph without cycles (not necessarily connected).Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from \(1\) to \(n\), and they would like to add edges to their forests such that: After adding edges, both of their graphs are still forests. They add the same edges. That is, if an edge \((u, v)\) is added to Mocha's forest, then an edge \((u, v)\) is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add.
|
The first line contains three integers \(n\), \(m_1\) and \(m_2\) (\(1 \le n \le 1000\), \(0 \le m_1, m_2 < n\)) β the number of nodes and the number of initial edges in Mocha's forest and Diana's forest.Each of the next \(m_1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β the edges in Mocha's forest.Each of the next \(m_2\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β the edges in Diana's forest.
|
The first line contains only one integer \(h\), the maximum number of edges Mocha and Diana can add (in each forest).Each of the next \(h\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β the edge you add each time.If there are multiple correct answers, you can print any one of them.
|
In the first example, we cannot add any edge.In the second example, the initial forests are as follows.We can add an edge \((2, 4)\).
|
Input: 3 2 2 1 2 2 3 1 2 1 3 | Output: 0
|
Easy
| 6 | 731 | 476 | 319 | 15 |
1,826 |
D
|
1826D
|
D. Running Miles
| 1,700 |
brute force; dp; greedy
|
There is a street with \(n\) sights, with sight number \(i\) being \(i\) miles from the beginning of the street. Sight number \(i\) has beauty \(b_i\). You want to start your morning jog \(l\) miles and end it \(r\) miles from the beginning of the street. By the time you run, you will see sights you run by (including sights at \(l\) and \(r\) miles from the start). You are interested in the \(3\) most beautiful sights along your jog, but every mile you run, you get more and more tired.So choose \(l\) and \(r\), such that there are at least \(3\) sights you run by, and the sum of beauties of the \(3\) most beautiful sights minus the distance in miles you have to run is maximized. More formally, choose \(l\) and \(r\), such that \(b_{i_1} + b_{i_2} + b_{i_3} - (r - l)\) is maximum possible, where \(i_1, i_2, i_3\) are the indices of the three maximum elements in range \([l, r]\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(3 \leq n \leq 10^5\)).The second line of each test case contains \(n\) integers \(b_i\) (\(1 \leq b_i \leq 10^8\)) β beauties of sights \(i\) miles from the beginning of the street.It's guaranteed that the sum of all \(n\) does not exceed \(10^5\).
|
For each test case output a single integer equal to the maximum value \(b_{i_1} + b_{i_2} + b_{i_3} - (r - l)\) for some running range \([l, r]\).
|
In the first example, we can choose \(l\) and \(r\) to be \(1\) and \(5\). So we visit all the sights and the three sights with the maximum beauty are the sights with indices \(1\), \(3\), and \(5\) with beauties \(5\), \(4\), and \(3\), respectively. So the total value is \(5 + 4 + 3 - (5 - 1) = 8\).In the second example, the range \([l, r]\) can be \([1, 3]\) or \([2, 4]\), the total value is \(1 + 1 + 1 - (3 - 1) = 1\).
|
Input: 455 1 4 2 341 1 1 169 8 7 6 5 47100000000 1 100000000 1 100000000 1 100000000 | Output: 8 1 22 299999996
|
Medium
| 3 | 890 | 415 | 146 | 18 |
1,044 |
B
|
1044B
|
B. Intersecting Subtrees
| 1,900 |
dfs and similar; interactive; trees
|
You are playing a strange game with Li Chen. You have a tree with \(n\) nodes drawn on a piece of paper. All nodes are unlabeled and distinguishable. Each of you independently labeled the vertices from \(1\) to \(n\). Neither of you know the other's labelling of the tree.You and Li Chen each chose a subtree (i.e., a connected subgraph) in that tree. Your subtree consists of the vertices labeled \(x_1, x_2, \ldots, x_{k_1}\) in your labeling, Li Chen's subtree consists of the vertices labeled \(y_1, y_2, \ldots, y_{k_2}\) in his labeling. The values of \(x_1, x_2, \ldots, x_{k_1}\) and \(y_1, y_2, \ldots, y_{k_2}\) are known to both of you. The picture shows two labelings of a possible tree: yours on the left and Li Chen's on the right. The selected trees are highlighted. There are two common nodes. You want to determine whether your subtrees have at least one common vertex. Luckily, your friend Andrew knows both labelings of the tree. You can ask Andrew at most \(5\) questions, each of which is in one of the following two forms: A x: Andrew will look at vertex \(x\) in your labeling and tell you the number of this vertex in Li Chen's labeling. B y: Andrew will look at vertex \(y\) in Li Chen's labeling and tell you the number of this vertex in your labeling. Determine whether the two subtrees have at least one common vertex after asking some questions. If there is at least one common vertex, determine one of your labels for any of the common vertices.
|
For the first sample, Li Chen's hidden permutation is \([2, 3, 1]\), and for the second, his hidden permutation is \([5, 3, 2, 4, 1, 6]\) for both cases.In the first sample, there is a tree with three nodes in a line. On the top, is how you labeled the tree and the subtree you chose, and the bottom is how Li Chen labeled the tree and the subtree he chose: In the first question, you ask Andrew to look at node \(1\) in your labelling and tell you the label of it in Li Chen's labelling. Andrew responds with \(2\). At this point, you know that both of your subtrees contain the same node (i.e. node \(1\) according to your labeling), so you can output ""C 1"" and finish. However, you can also ask Andrew to look at node \(2\) in Li Chen's labelling and tell you the label of it in your labelling. Andrew responds with \(1\) (this step was given with the only reason β to show you how to ask questions).For the second sample, there are two test cases. The first looks is the one from the statement: We first ask ""B 2"", and Andrew will tell us \(3\). In this case, we know \(3\) is a common vertex, and moreover, any subtree with size \(3\) that contains node \(3\) must contain node \(1\) as well, so we can output either ""C 1"" or ""C 3"" as our answer.In the second case in the second sample, the situation looks as follows: In this case, you know that the only subtree of size \(3\) that doesn't contain node \(1\) is subtree \(4,5,6\). You ask Andrew for the label of node \(1\) in Li Chen's labelling and Andrew says \(5\). In this case, you know that Li Chen's subtree doesn't contain node \(1\), so his subtree must be consist of the nodes \(4,5,6\) (in your labelling), thus the two subtrees have no common nodes.
|
Input: 131 22 3111221 | Output: A 1B 2C 1
|
Hard
| 3 | 1,475 | 0 | 0 | 10 |
||
1,338 |
B
|
1338B
|
B. Edge Weight Assignment
| 1,800 |
bitmasks; constructive algorithms; dfs and similar; greedy; math; trees
|
You have unweighted tree of \(n\) vertices. You have to assign a positive weight to each edge so that the following condition would hold: For every two different leaves \(v_{1}\) and \(v_{2}\) of this tree, bitwise XOR of weights of all edges on the simple path between \(v_{1}\) and \(v_{2}\) has to be equal to \(0\). Note that you can put very large positive integers (like \(10^{(10^{10})}\)).It's guaranteed that such assignment always exists under given constraints. Now let's define \(f\) as the number of distinct weights in assignment. In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is \(0\). \(f\) value is \(2\) here, because there are \(2\) distinct edge weights(\(4\) and \(5\)). In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex \(1\) and vertex \(6\) (\(3, 4, 5, 4\)) is not \(0\). What are the minimum and the maximum possible values of \(f\) for the given tree? Find and print both.
|
The first line contains integer \(n\) (\(3 \le n \le 10^{5}\)) β the number of vertices in given tree.The \(i\)-th of the next \(n-1\) lines contains two integers \(a_{i}\) and \(b_{i}\) (\(1 \le a_{i} \lt b_{i} \le n\)) β it means there is an edge between \(a_{i}\) and \(b_{i}\). It is guaranteed that given graph forms tree of \(n\) vertices.
|
Print two integers β the minimum and maximum possible value of \(f\) can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints.
|
In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum. In the second example, possible assignments for each minimum and maximum are described in picture below. The \(f\) value of valid assignment of this tree is always \(3\). In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.
|
Input: 6 1 3 2 3 3 4 4 5 5 6 | Output: 1 4
|
Medium
| 6 | 1,004 | 345 | 195 | 13 |
426 |
B
|
426B
|
B. Sereja and Mirroring
| 1,300 |
implementation
|
Let's assume that we are given a matrix b of size x Γ y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a 2x Γ y matrix c which has the following properties: the upper half of matrix c (rows with numbers from 1 to x) exactly matches b; the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1). Sereja has an n Γ m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?
|
The first line contains two integers, n and m (1 β€ n, m β€ 100). Each of the next n lines contains m integers β the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 β€ aij β€ 1) β the i-th row of the matrix a.
|
In the single line, print the answer to the problem β the minimum number of rows of matrix b.
|
In the first test sample the answer is a 2 Γ 3 matrix b:001110If we perform a mirroring operation with this matrix, we get the matrix a that is given in the input:001110110001
|
Input: 4 30 0 11 1 01 1 00 0 1 | Output: 2
|
Easy
| 1 | 699 | 233 | 93 | 4 |
1,794 |
E
|
1794E
|
E. Labeling the Tree with Distances
| 2,400 |
data structures; dp; greedy; hashing; implementation; trees
|
You are given an unweighted tree of \(n\) vertices numbered from \(1\) to \(n\) and a list of \(n-1\) integers \(a_1, a_2, \ldots, a_{n-1}\). A tree is a connected undirected graph without cycles. You will use each element of the list to label one vertex. No vertex should be labeled twice. You can label the only remaining unlabeled vertex with any integer. A vertex \(x\) is called good if it is possible to do this labeling so that for each vertex \(i\), its label is the distance between \(x\) and \(i\). The distance between two vertices \(s\) and \(t\) on a tree is the minimum number of edges on a path that starts at vertex \(s\) and ends at vertex \(t\). Find all good vertices.
|
The first line contains one integer \(n\) (\(2\le n\le 2\cdot 10^5\)) β the number of vertices in the tree.The second line contains \(n - 1\) integers \(a_1,a_2,\ldots,a_{n-1}\) (\(0\le a_i < n\)) β the given list.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 the number of good vertices.In the second line, print the indices of all good vertices in ascending order.
|
This is the tree for the first example: And these are two possible labelings with the elements on the list so that \(2\) is a good vertex (left) and \(4\) is a good vertex (right). The square below each vertex represents its label. The black squares contain the numbers which were on the given list and the only white square contains the only number which was not on the given list.In the second example, the only good vertex is vertex \(3\).In the third example, there are no good vertices.
|
Input: 6 2 1 0 1 2 1 2 2 3 2 4 4 5 4 6 | Output: 2 2 4
|
Expert
| 6 | 687 | 408 | 130 | 17 |
963 |
D
|
963D
|
D. Frequency of String
| 2,500 |
hashing; string suffix structures; strings
|
You are given a string \(s\). You should answer \(n\) queries. The \(i\)-th query consists of integer \(k_i\) and string \(m_i\). The answer for this query is the minimum length of such a string \(t\) that \(t\) is a substring of \(s\) and \(m_i\) has at least \(k_i\) occurrences as a substring in \(t\).A substring of a string is a continuous segment of characters of the string.It is guaranteed that for any two queries the strings \(m_i\) from these queries are different.
|
The first line contains string \(s\) \((1 \leq \left | s \right | \leq 10^{5})\).The second line contains an integer \(n\) (\(1 \leq n \leq 10^5\)).Each of next \(n\) lines contains an integer \(k_i\) \((1 \leq k_i \leq |s|)\) and a non-empty string \(m_i\) β parameters of the query with number \(i\), in this order.All strings in input consists of lowercase English letters. Sum of length of all strings in input doesn't exceed \(10^5\). All \(m_i\) are distinct.
|
For each query output the answer for it in a separate line.If a string \(m_{i}\) occurs in \(s\) less that \(k_{i}\) times, output -1.
|
Input: aaaaa53 a3 aa2 aaa3 aaaa1 aaaaa | Output: 344-15
|
Expert
| 3 | 476 | 465 | 134 | 9 |
|
1,098 |
F
|
1098F
|
F. Π-function
| 3,500 |
string suffix structures; strings
|
The length of the longest common prefix of two strings \(s=s_1 s_2 \ldots s_n\) and \(t = t_1 t_2 \ldots t_m\) is defined as the maximum \(k \le \min(n, m)\) such that \(s_1 s_2 \ldots s_k\) equals \(t_1 t_2 \ldots t_k\). Let's denote the longest common prefix of two strings \(s\) and \(t\) as \(lcp(s,t)\).Z-function of a string \(s_1 s_2 \dots s_n\) is a sequence of integers \(z_1, z_2, \ldots, z_n\), where \(z_i = lcp(s_1 s_2 \ldots s_n,\ \ s_i s_{i+1} \dots s_n)\). Π-function of a string \(s\) is defined as \(z_1 + z_2 + \ldots + z_n\).You're given a string \(s=s_1 s_2 \ldots s_n\) and \(q\) queries. Each query is described by two integers \(l_i\) and \(r_i\), where \(1 \le l_i \le r_i \le n\). The answer for the query is defined as Π-function of the string \(s_{l_i} s_{l_i +1} \ldots s_{r_i}\).
|
The first line contains the string \(s\), consisting of lowercase English letters (\(1 \leq |s| \leq 200\,000\)). The second line contains one integer \(q\) β the number of queries (\(1 \leq q \leq 200\,000\)). Each of the following \(q\) lines contains two integers \(l_i\) and \(r_i\), describing the query (\(1 \leq l_i \leq r_i \leq |s|\)).
|
For every query output one integer: the value of Π-function of the corresponding substring.
|
In the first sample case there are four queries: the first query corresponds to the substring bb, and its Π-function equals \(2 + 1 = 3\); the second query corresponds to the substring abb, and its Π-function equals \(3 + 0 + 0 = 3\); the third query corresponds to the substring b, and its Π-function equals \(1\). the fourth query corresponds to the substring abdd, and its Π-function equals \(4 + 0 + 0 + 0= 4\).
|
Input: abbd 4 2 3 1 3 3 3 1 4 | Output: 3 3 1 4
|
Master
| 2 | 809 | 344 | 91 | 10 |
1,773 |
K
|
1773K
| 1,900 |
constructive algorithms
|
Hard
| 1 | 0 | 0 | 0 | 17 |
||||||
1,726 |
G
|
1726G
|
G. A Certain Magical Party
| 3,300 |
combinatorics; data structures; greedy; sortings
|
There are \(n\) people at a party. The \(i\)-th person has an amount of happiness \(a_i\).Every person has a certain kind of personality which can be represented as a binary integer \(b\). If \(b = 0\), it means the happiness of the person will increase if he tells the story to someone strictly less happy than them. If \(b = 1\), it means the happiness of the person will increase if he tells the story to someone strictly more happy than them.Let us define a speaking order as an ordering of the people from left to right. Now the following process occurs. We go from left to right. The current person tells the story to all people other than himself. Note that all happiness values stay constant while this happens. After the person is done, he counts the number of people who currently have strictly less/more happiness than him as per his kind of personality, and his happiness increases by that value. Note that only the current person's happiness value increases.As the organizer of the party, you don't want anyone to leave sad. Therefore, you want to count the number of speaking orders such that at the end of the process all \(n\) people have equal happiness.Two speaking orders are considered different if there exists at least one person who does not have the same position in the two speaking orders.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β the number of people.The second line contains a sequence of \(n\) integers \(a_1,a_2,...,a_n\) (\(1 \leq a_i \leq 2n\)) β the happiness values.The third line contains a sequence of \(n\) binary numbers \(b_1,b_2,...,b_n\) (\(b_i \in \{0,1\}\)) β the kinds of personality.
|
Output the number of different valid speaking orders. Since this number can be large, output it modulo \(998244353\).
|
Here is the explanation for the first example. One valid speaking order is \([2,1,4,3]\) (here, we have written the indices of each person). Each step shows the current happiness values and results.Step \(1\): \([1,2,4,4]\) \(\rightarrow\) Person \(2\) tells the story to others. Since his kind of personality is \(1\), his happiness increases by \(2\) since persons \(3\) and \(4\) have strictly greater happiness.Step \(2\): \([1,4,4,4]\) \(\rightarrow\) Person \(1\) tells the story to others. Since his kind of personality is \(1\), his happiness increases by \(3\) since persons \(2\), \(3\) and \(4\) have strictly greater happiness.Step \(3\): \([4,4,4,4]\) \(\rightarrow\) Person \(4\) tells the story to others. Since his kind of personality is \(0\), his happiness increases by \(0\) since no one has strictly lesser happiness.Step \(4\): \([4,4,4,4]\) \(\rightarrow\) Person \(3\) tells the story to others. Since his kind of personality is \(0\), his happiness increases by \(0\) since no one has strictly lesser happiness.At the end, everyone has equal happiness.Note that \([2,1,3,4]\) is also a valid answer for this example.It can be shown that there is no valid ordering for the second example.
|
Input: 4 1 2 4 4 1 1 0 0 | Output: 2
|
Master
| 4 | 1,315 | 353 | 117 | 17 |
2,070 |
A
|
2070A
|
A. FizzBuzz Remixed
| 800 |
brute force; math
|
FizzBuzz is one of the most well-known problems from coding interviews. In this problem, we will consider a remixed version of FizzBuzz:Given an integer \(n\), process all integers from \(0\) to \(n\). For every integer such that its remainders modulo \(3\) and modulo \(5\) are the same (so, for every integer \(i\) such that \(i \bmod 3 = i \bmod 5\)), print FizzBuzz.However, you don't have to solve it. Instead, given the integer \(n\), you have to report how many times the correct solution to that problem will print FizzBuzz.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case contains one line consisting of one integer \(n\) (\(0 \le n \le 10^9\)).
|
For each test case, print one integer β the number of times the correct solution will print FizzBuzz with the given value of \(n\).
|
In the first test case, the solution will print FizzBuzz for the integer \(0\).In the second test case, the solution will print FizzBuzz for the integers \(0, 1, 2\).In the third test case, the solution will print FizzBuzz for the integers \(0, 1, 2, 15\).
|
Input: 7051542133717101997998244353 | Output: 1 3 4 9 270 3420402 199648872
|
Beginner
| 2 | 532 | 180 | 131 | 20 |
1,388 |
A
|
1388A
|
A. Captain Flint and Crew Recruitment
| 800 |
brute force; greedy; math; number theory
|
Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task.Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer \(x\) as nearly prime if it can be represented as \(p \cdot q\), where \(1 < p < q\) and \(p\) and \(q\) are prime numbers. For example, integers \(6\) and \(10\) are nearly primes (since \(2 \cdot 3 = 6\) and \(2 \cdot 5 = 10\)), but integers \(1\), \(3\), \(4\), \(16\), \(17\) or \(44\) are not.Captain Flint guessed an integer \(n\) and asked you: can you represent it as the sum of \(4\) different positive integers where at least \(3\) of them should be nearly prime.Uncle Bogdan easily solved the task and joined the crew. Can you do the same?
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Next \(t\) lines contain test cases β one per line. The first and only line of each test case contains the single integer \(n\) \((1 \le n \le 2 \cdot 10^5)\) β the number Flint guessed.
|
For each test case print: YES and \(4\) different positive integers such that at least \(3\) of them are nearly prime and their sum is equal to \(n\) (if there are multiple answers print any of them); NO if there is no way to represent \(n\) as the sum of \(4\) different positive integers where at least \(3\) of them are nearly prime. You can print each character of YES or NO in any case.
|
In the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.In the third test case, \(n=31=2 \cdot 7 + 2 \cdot 5 + 2 \cdot 3 + 1\): integers \(14\), \(10\), \(6\) are nearly prime.In the fourth test case, \(n=36=5 + 2 \cdot 3 + 2 \cdot 5 + 3 \cdot 5\): integers \(6\), \(10\), \(15\) are nearly prime.In the fifth test case, \(n=44=2 \cdot 3 + 7 + 2 \cdot 5 + 3 \cdot 7\): integers \(6\), \(10\), \(21\) are nearly prime.In the sixth test case, \(n=100=2 + 2 \cdot 5 + 3 \cdot 11 + 5 \cdot 11\): integers \(10\), \(33\), \(55\) are nearly prime.In the seventh test case, \(n=258=2 \cdot 5 + 3 \cdot 7 + 13 \cdot 17 + 2 \cdot 3\): integers \(10\), \(21\), \(221\), \(6\) are nearly prime.
|
Input: 7 7 23 31 36 44 100 258 | Output: NO NO YES 14 10 6 1 YES 5 6 10 15 YES 6 7 10 21 YES 2 10 33 55 YES 10 21 221 6
|
Beginner
| 4 | 945 | 283 | 391 | 13 |
131 |
A
|
131A
|
A. cAPS lOCK
| 1,000 |
implementation; strings
|
wHAT DO WE NEED cAPS LOCK FOR?Caps lock is a computer keyboard key. Pressing it sets an input mode in which typed letters are capital by default. If it is pressed by accident, it leads to accidents like the one we had in the first passage. Let's consider that a word has been typed with the Caps lock key accidentally switched on, if: either it only contains uppercase letters; or all letters except for the first one are uppercase. In this case we should automatically change the case of all letters. For example, the case of the letters that form words ""hELLO"", ""HTTP"", ""z"" should be changed.Write a program that applies the rule mentioned above. If the rule cannot be applied, the program should leave the word unchanged.
|
The first line of the input data contains a word consisting of uppercase and lowercase Latin letters. The word's length is from 1 to 100 characters, inclusive.
|
Print the result of the given word's processing.
|
Input: cAPS | Output: Caps
|
Beginner
| 2 | 730 | 159 | 48 | 1 |
|
1,374 |
D
|
1374D
|
D. Zero Remainder Array
| 1,400 |
math; sortings; two pointers
|
You are given an array \(a\) consisting of \(n\) positive integers.Initially, you have an integer \(x = 0\). During one move, you can do one of the following two operations: Choose exactly one \(i\) from \(1\) to \(n\) and increase \(a_i\) by \(x\) (\(a_i := a_i + x\)), then increase \(x\) by \(1\) (\(x := x + 1\)). Just increase \(x\) by \(1\) (\(x := x + 1\)). The first operation can be applied no more than once to each \(i\) from \(1\) to \(n\).Your task is to find the minimum number of moves required to obtain such an array that each its element is divisible by \(k\) (the value \(k\) is given).You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(k\) (\(1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9\)) β the length of \(a\) and the required divisior. The second line of the test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the \(i\)-th element of \(a\).It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
For each test case, print the answer β the minimum number of moves required to obtain such an array that each its element is divisible by \(k\).
|
Consider the first test case of the example: \(x=0\), \(a = [1, 2, 1, 3]\). Just increase \(x\); \(x=1\), \(a = [1, 2, 1, 3]\). Add \(x\) to the second element and increase \(x\); \(x=2\), \(a = [1, 3, 1, 3]\). Add \(x\) to the third element and increase \(x\); \(x=3\), \(a = [1, 3, 3, 3]\). Add \(x\) to the fourth element and increase \(x\); \(x=4\), \(a = [1, 3, 3, 6]\). Just increase \(x\); \(x=5\), \(a = [1, 3, 3, 6]\). Add \(x\) to the first element and increase \(x\); \(x=6\), \(a = [6, 3, 3, 6]\). We obtained the required array. Note that you can't add \(x\) to the same element more than once.
|
Input: 5 4 3 1 2 1 3 10 6 8 7 1 8 3 7 5 10 8 9 5 10 20 100 50 20 100500 10 25 24 24 24 24 24 24 24 24 24 24 8 8 1 2 3 4 5 6 7 8 | Output: 6 18 0 227 8
|
Easy
| 3 | 653 | 568 | 144 | 13 |
920 |
C
|
920C
|
C. Swap Adjacent Elements
| 1,400 |
dfs and similar; greedy; math; sortings; two pointers
|
You have an array a consisting of n integers. Each integer from 1 to n appears exactly once in this array.For some indices i (1 β€ i β€ n - 1) it is possible to swap i-th element with (i + 1)-th, for other indices it is not possible. You may perform any number of swapping operations any order. There is no limit on the number of times you swap i-th element with (i + 1)-th (if the position is not forbidden).Can you make this array sorted in ascending order performing some sequence of swapping operations?
|
The first line contains one integer n (2 β€ n β€ 200000) β the number of elements in the array.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 200000) β the elements of the array. Each integer from 1 to n appears exactly once.The third line contains a string of n - 1 characters, each character is either 0 or 1. If i-th character is 1, then you can swap i-th element with (i + 1)-th any number of times, otherwise it is forbidden to swap i-th element with (i + 1)-th.
|
If it is possible to sort the array in ascending order using any sequence of swaps you are allowed to make, print YES. Otherwise, print NO.
|
In the first example you may swap a3 and a4, and then swap a4 and a5.
|
Input: 61 2 5 3 4 601110 | Output: YES
|
Easy
| 5 | 505 | 480 | 139 | 9 |
1,856 |
E1
|
1856E1
|
E1. PermuTree (easy version)
| 1,800 |
dfs and similar; dp; trees
|
This is the easy version of the problem. The differences between the two versions are the constraint on \(n\) and the time limit. You can make hacks only if both versions of the problem are solved.You are given a tree with \(n\) vertices rooted at vertex \(1\).For some permutation\(^\dagger\) \(a\) of length \(n\), let \(f(a)\) be the number of pairs of vertices \((u, v)\) such that \(a_u < a_{\operatorname{lca}(u, v)} < a_v\). Here, \(\operatorname{lca}(u,v)\) denotes the lowest common ancestor of vertices \(u\) and \(v\).Find the maximum possible value of \(f(a)\) over all permutations \(a\) of length \(n\).\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
The first line contains a single integer \(n\) (\(2 \le n \le 5000\)).The second line contains \(n - 1\) integers \(p_2,p_3,\ldots,p_n\) (\(1 \le p_i < i\)) indicating that there is an edge between vertices \(i\) and \(p_i\).
|
Output the maximum value of \(f(a)\).
|
The tree in the first test: One possible optimal permutation \(a\) is \([2, 1, 4, 5, 3]\) with \(4\) suitable pairs of vertices: \((2, 3)\), since \(\operatorname{lca}(2, 3) = 1\) and \(1 < 2 < 4\), \((2, 4)\), since \(\operatorname{lca}(2, 4) = 1\) and \(1 < 2 < 5\), \((2, 5)\), since \(\operatorname{lca}(2, 5) = 1\) and \(1 < 2 < 3\), \((5, 4)\), since \(\operatorname{lca}(5, 4) = 3\) and \(3 < 4 < 5\). The tree in the third test: The tree in the fourth test:
|
Input: 5 1 1 3 3 | Output: 4
|
Medium
| 3 | 954 | 225 | 37 | 18 |
1,479 |
C
|
1479C
|
C. Continuous City
| 2,500 |
bitmasks; constructive algorithms
|
Some time ago Homer lived in a beautiful city. There were \(n\) blocks numbered from \(1\) to \(n\) and \(m\) directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them. Homer discovered that for some two numbers \(L\) and \(R\) the city was \((L, R)\)-continuous. The city is said to be \((L, R)\)-continuous, if all paths from block \(1\) to block \(n\) are of length between \(L\) and \(R\) (inclusive); and for every \(L \leq d \leq R\), there is exactly one path from block \(1\) to block \(n\) whose length is \(d\). A path from block \(u\) to block \(v\) is a sequence \(u = x_0 \to x_1 \to x_2 \to \dots \to x_k = v\), where there is a road from block \(x_{i-1}\) to block \(x_{i}\) for every \(1 \leq i \leq k\). The length of a path is the sum of lengths over all roads in the path. Two paths \(x_0 \to x_1 \to \dots \to x_k\) and \(y_0 \to y_1 \to \dots \to y_l\) are different, if \(k \neq l\) or \(x_i \neq y_i\) for some \(0 \leq i \leq \min\{k, l\}\). After moving to another city, Homer only remembers the two special numbers \(L\) and \(R\) but forgets the numbers \(n\) and \(m\) of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than \(32\) (because the city was small).As the best friend of Homer, please tell him whether it is possible to find a \((L, R)\)-continuous city or not.
|
The single line contains two integers \(L\) and \(R\) (\(1 \leq L \leq R \leq 10^6\)).
|
If it is impossible to find a \((L, R)\)-continuous city within \(32\) blocks, print ""NO"" in a single line.Otherwise, print ""YES"" in the first line followed by a description of a \((L, R)\)-continuous city. The second line should contain two integers \(n\) (\(2 \leq n \leq 32\)) and \(m\) (\(1 \leq m \leq \frac {n(n-1)} 2\)), where \(n\) denotes the number of blocks and \(m\) denotes the number of roads.Then \(m\) lines follow. The \(i\)-th of the \(m\) lines should contain three integers \(a_i\), \(b_i\) (\(1 \leq a_i < b_i \leq n\)) and \(c_i\) (\(1 \leq c_i \leq 10^6\)) indicating that there is a directed road from block \(a_i\) to block \(b_i\) of length \(c_i\). It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every \(1 \leq i < j \leq m\), either \(a_i \neq a_j\) or \(b_i \neq b_j\).
|
In the first example there is only one path from block \(1\) to block \(n = 2\), and its length is \(1\). In the second example there are three paths from block \(1\) to block \(n = 5\), which are \(1 \to 2 \to 5\) of length \(4\), \(1 \to 3 \to 5\) of length \(5\) and \(1 \to 4 \to 5\) of length \(6\).
|
Input: 1 1 | Output: YES 2 1 1 2 1
|
Expert
| 2 | 1,557 | 86 | 863 | 14 |
44 |
D
|
44D
|
D. Hyperdrive
| 1,800 |
math
|
In a far away galaxy there are n inhabited planets, numbered with numbers from 1 to n. They are located at large distances from each other, that's why the communication between them was very difficult until on the planet number 1 a hyperdrive was invented. As soon as this significant event took place, n - 1 spaceships were built on the planet number 1, and those ships were sent to other planets to inform about the revolutionary invention. Paradoxical thought it may be, but the hyperspace is represented as simple three-dimensional Euclidean space. The inhabited planets may be considered fixed points in it, and no two points coincide and no three points lie on the same straight line. The movement of a ship with a hyperdrive between two planets is performed along a straight line at the constant speed, the same for all the ships. That's why the distance in the hyperspace are measured in hyperyears (a ship with a hyperdrive covers a distance of s hyperyears in s years).When the ship reaches an inhabited planet, the inhabitants of the planet dissemble it, make n - 2 identical to it ships with a hyperdrive and send them to other n - 2 planets (except for the one from which the ship arrived). The time to make a new ship compared to the time in which they move from one planet to another is so small that it can be disregarded. New ships are absolutely identical to the ones sent initially: they move at the same constant speed along a straight line trajectory and, having reached a planet, perform the very same mission, i.e. are dissembled to build new n - 2 ships and send them to all the planets except for the one from which the ship arrived. Thus, the process of spreading the important news around the galaxy continues.However the hyperdrive creators hurried to spread the news about their invention so much that they didn't study completely what goes on when two ships collide in the hyperspace. If two moving ships find themselves at one point, they provoke an explosion of colossal power, leading to the destruction of the galaxy!Your task is to find the time the galaxy will continue to exist from the moment of the ships' launch from the first planet.
|
The first line contains a number n (3 β€ n β€ 5000) β the number of inhabited planets in the galaxy. The next n lines contain integer coordinates of the planets in format ""xi yi zi"" ( - 104 β€ xi, yi, zi β€ 104).
|
Print the single number β the solution to the task with an absolute or relative error not exceeding 10 - 6.
|
Input: 40 0 00 0 10 1 01 0 0 | Output: 1.7071067812
|
Medium
| 1 | 2,174 | 210 | 107 | 0 |
|
177 |
A1
|
177A1
|
A1. Good Matrix Elements
| 800 |
implementation
|
The Smart Beaver from ABBYY got hooked on square matrices. Now he is busy studying an n Γ n size matrix, where n is odd. The Smart Beaver considers the following matrix elements good: Elements of the main diagonal. Elements of the secondary diagonal. Elements of the ""middle"" row β the row which has exactly rows above it and the same number of rows below it. Elements of the ""middle"" column β the column that has exactly columns to the left of it and the same number of columns to the right of it. The figure shows a 5 Γ 5 matrix. The good elements are marked with green. Help the Smart Beaver count the sum of good elements of the given matrix.
|
The first line of input data contains a single odd integer n. Each of the next n lines contains n integers aij (0 β€ aij β€ 100) separated by single spaces β the elements of the given matrix.The input limitations for getting 30 points are: 1 β€ n β€ 5 The input limitations for getting 100 points are: 1 β€ n β€ 101
|
Print a single integer β the sum of good matrix elements.
|
In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure.
|
Input: 31 2 34 5 67 8 9 | Output: 45
|
Beginner
| 1 | 650 | 309 | 57 | 1 |
220 |
A
|
220A
|
A. Little Elephant and Problem
| 1,300 |
implementation; sortings
|
The Little Elephant has got a problem β somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array.The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements.Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself.
|
The first line contains a single integer n (2 β€ n β€ 105) β the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, β array a.Note that the elements of the array are not necessarily distinct numbers.
|
In a single line print ""YES"" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and ""NO"" (without the quotes) otherwise.
|
In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is ""YES"".In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is ""YES"".In the third sample we can't sort the array in more than one swap operation, so the answer is ""NO"".
|
Input: 21 2 | Output: YES
|
Easy
| 2 | 662 | 257 | 168 | 2 |
1,627 |
B
|
1627B
|
B. Not Sitting
| 1,300 |
games; greedy; sortings
|
Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a \(n \times m\) grid. The seat in row \(r\) and column \(c\) is denoted by \((r, c)\), and the distance between two seats \((a,b)\) and \((c,d)\) is \(|a-c| + |b-d|\). As the class president, Tina has access to exactly \(k\) buckets of pink paint. The following process occurs. First, Tina chooses exactly \(k\) seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted \(k\) seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!Now, Rahul wonders for \(k = 0, 1, \dots, n \cdot m - 1\), if Tina has \(k\) buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 5 \cdot 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\), \(m\) (\(2 \leq n \cdot m \leq 10^5\)) β the number of rows and columns of seats in the classroom.The sum of \(n \cdot m\) across all test cases does not exceed \(10^5\).
|
For each test case, output \(n \cdot m\) ordered integers β the distance between Rahul and Tina if both of them act optimally for every \(k \in [0, n \cdot m - 1]\).
|
One possible sequence of choices for the first testcase where Tina has \(k=3\) buckets of paints is as follows.Tina paints the seats at positions \((1, 2)\), \((2, 2)\), \((3, 2)\) with pink paint. Rahul chooses the seat at \((3, 1)\) after which Tina chooses to sit at \((1, 3)\). Therefore, the distance between Tina and Rahul is \(|3-1| + |1-3| = 4\), and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.For \(k=0\) in the first test case, Rahul can decide to sit at \((2, 2)\) and Tina can decide to sit at \((4, 3)\) so the distance between them would be \(|2 - 4| + |2 - 3| = 3\).Below are pictorial representations of the \(k=3\) and \(k=0\) cases for the first test case. A possible seating arrangement for \(k=3\). A possible seating arrangement for \(k=0\).
|
Input: 24 31 2 | Output: 3 3 4 4 4 4 4 4 5 5 5 5 1 1
|
Easy
| 3 | 1,396 | 419 | 165 | 16 |
1,999 |
E
|
1999E
|
E. Triple Operations
| 1,300 |
dp; implementation; math
|
On the board Ivy wrote down all integers from \(l\) to \(r\), inclusive.In an operation, she does the following: pick two numbers \(x\) and \(y\) on the board, erase them, and in their place write the numbers \(3x\) and \(\lfloor \frac{y}{3} \rfloor\). (Here \(\lfloor \bullet \rfloor\) denotes rounding down to the nearest integer). What is the minimum number of operations Ivy needs to make all numbers on the board equal \(0\)? We have a proof that this is always possible.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The only line of each test case contains two integers \(l\) and \(r\) (\(1 \leq l < r \leq 2 \cdot 10^5\)).
|
For each test case, output a single integer β the minimum number of operations needed to make all numbers on the board equal \(0\).
|
In the first test case, we can perform \(5\) operations as follows: $$$\( 1,2,3 \xrightarrow[x=1,\,y=2]{} 3,0,3 \xrightarrow[x=0,\,y=3]{} 1,0,3 \xrightarrow[x=0,\,y=3]{} 1,0,1 \xrightarrow[x=0,\,y=1]{} 0,0,1 \xrightarrow[x=0,\,y=1]{} 0,0,0 .\)$$$
|
Input: 41 32 4199999 20000019 84 | Output: 5 6 36 263
|
Easy
| 3 | 476 | 200 | 131 | 19 |
1,992 |
B
|
1992B
|
B. Angry Monk
| 800 |
greedy; math; sortings
|
To celebrate his recovery, k1o0n has baked an enormous \(n\) metres long potato casserole.Turns out, Noobish_Monk just can't stand potatoes, so he decided to ruin k1o0n's meal. He has cut it into \(k\) pieces, of lengths \(a_1, a_2, \dots, a_k\) meters.k1o0n wasn't keen on that. Luckily, everything can be fixed. In order to do that, k1o0n can do one of the following operations: Pick a piece with length \(a_i \ge 2\) and divide it into two pieces with lengths \(1\) and \(a_i - 1\). As a result, the number of pieces will increase by \(1\); Pick a slice \(a_i\) and another slice with length \(a_j=1\) (\(i \ne j\)) and merge them into one piece with length \(a_i+1\). As a result, the number of pieces will decrease by \(1\). Help k1o0n to find the minimum number of operations he needs to do in order to merge the casserole into one piece with length \(n\).For example, if \(n=5\), \(k=2\) and \(a = [3, 2]\), it is optimal to do the following: Divide the piece with length \(2\) into two pieces with lengths \(2-1=1\) and \(1\), as a result \(a = [3, 1, 1]\). Merge the piece with length \(3\) and the piece with length \(1\), as a result \(a = [4, 1]\). Merge the piece with length \(4\) and the piece with length \(1\), as a result \(a = [5]\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of each test case consists of two lines. The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 10^9\), \(2 \le k \le 10^5\)) β length of casserole and the number of pieces.The second line contains \(k\) integers \(a_1, a_2, \ldots, a_k\) (\(1 \le a_i \le n - 1\), \(\sum a_i = n\)) β lengths of pieces of casserole, which Noobish_Monk has cut.It is guaranteed that the sum of \(k\) over all \(t\) test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, output the minimum number of operations K1o0n needs to restore his pie after the terror of Noobish_Monk.
|
Input: 45 33 1 15 23 211 42 3 1 516 61 6 1 1 1 6 | Output: 2 3 9 15
|
Beginner
| 3 | 1,252 | 585 | 124 | 19 |
|
1,634 |
C
|
1634C
|
C. OKEA
| 1,000 |
constructive algorithms
|
People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world.β Pedro DomingosYou work for a well-known department store that uses leading technologies and employs mechanistic work β that is, robots!The department you work in sells \(n \cdot k\) items. The first item costs \(1\) dollar, the second item costs \(2\) dollars, and so on: \(i\)-th item costs \(i\) dollars. The items are situated on shelves. The items form a rectangular grid: there are \(n\) shelves in total, and each shelf contains exactly \(k\) items. We will denote by \(a_{i,j}\) the price of \(j\)-th item (counting from the left) on the \(i\)-th shelf, \(1 \le i \le n, 1 \le j \le k\).Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items \(a_{i,l}, a_{i,l+1}, \ldots, a_{i,r}\) for some shelf \(i\) and indices \(l \le r\)? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down.You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array \(a\) that: Every number from \(1\) to \(n \cdot k\) (inclusively) occurs exactly once. For each \(i, l, r\), the mean price of items from \(l\) to \(r\) on \(i\)-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement.
|
The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The first and only line of each test case contains two integers \(n\) and \(k\) (\(1 \le n, k \le 500\)) β the number of shelves and length of each shelf, respectively.It is guaranteed that the sum \(n\) over all test cases does not exceed \(500\) and the sum \(k\) over all test cases does not exceed \(500\).
|
Print the answer for each test case.If such an arrangement exists, print ""YES"" on a single line. After that, print any example on \(n\) lines of \(k\) numbers each, one line per shelf. Each number from \(1\) to \(n \cdot k\) must occur exactly once in the output.If no good arrangement exists, print a single word ""NO"" on its own line.
|
Input: 41 12 23 33 1 | Output: YES 1 YES 1 3 2 4 NO YES 1 2 3
|
Beginner
| 1 | 1,557 | 406 | 339 | 16 |
|
1,491 |
C
|
1491C
|
C. Pekora and Trampoline
| 1,700 |
brute force; data structures; dp; greedy; implementation
|
There is a trampoline park with \(n\) trampolines in a line. The \(i\)-th of which has strength \(S_i\).Pekora can jump on trampolines in multiple passes. She starts the pass by jumping on any trampoline of her choice. If at the moment Pekora jumps on trampoline \(i\), the trampoline will launch her to position \(i + S_i\), and \(S_i\) will become equal to \(\max(S_i-1,1)\). In other words, \(S_i\) will decrease by \(1\), except of the case \(S_i=1\), when \(S_i\) will remain equal to \(1\). If there is no trampoline in position \(i + S_i\), then this pass is over. Otherwise, Pekora will continue the pass by jumping from the trampoline at position \(i + S_i\) by the same rule as above.Pekora can't stop jumping during the pass until she lands at the position larger than \(n\) (in which there is no trampoline). Poor Pekora!Pekora is a naughty rabbit and wants to ruin the trampoline park by reducing all \(S_i\) to \(1\). What is the minimum number of passes she needs to reduce all \(S_i\) to \(1\)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5000\)) β the number of trampolines.The second line of each test case contains \(n\) integers \(S_1, S_2, \dots, S_n\) (\(1 \le S_i \le 10^9\)), where \(S_i\) is the strength of the \(i\)-th trampoline.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(5000\).
|
For each test case, output a single integer β the minimum number of passes Pekora needs to do to reduce all \(S_i\) to \(1\).
|
For the first test case, here is an optimal series of passes Pekora can take. (The bolded numbers are the positions that Pekora jumps into during these passes.) \([1,4,\textbf{2},2,\textbf{2},2,\textbf{2}]\) \([1,\textbf{4},1,2,1,\textbf{2},1]\) \([1,\textbf{3},1,2,\textbf{1},\textbf{1},\textbf{1}]\) \([1,\textbf{2},1,\textbf{2},1,\textbf{1},\textbf{1}]\) For the second test case, the optimal series of passes is show below. \([\textbf{2},3]\) \([1,\textbf{3}]\) \([1,\textbf{2}]\) For the third test case, all \(S_i\) are already equal to \(1\).
|
Input: 3 7 1 4 2 2 2 2 2 2 2 3 5 1 1 1 1 1 | Output: 4 3 0
|
Medium
| 5 | 1,010 | 462 | 125 | 14 |
1,004 |
A
|
1004A
|
A. Sonya and Hotels
| 900 |
implementation
|
Sonya decided that having her own hotel business is the best way of earning money because she can profit and rest wherever she wants.The country where Sonya lives is an endless line. There is a city in each integer coordinate on this line. She has \(n\) hotels, where the \(i\)-th hotel is located in the city with coordinate \(x_i\). Sonya is a smart girl, so she does not open two or more hotels in the same city.Sonya understands that her business needs to be expanded by opening new hotels, so she decides to build one more. She wants to make the minimum distance from this hotel to all others to be equal to \(d\). The girl understands that there are many possible locations to construct such a hotel. Thus she wants to know the number of possible coordinates of the cities where she can build a new hotel. Because Sonya is lounging in a jacuzzi in one of her hotels, she is asking you to find the number of cities where she can build a new hotel so that the minimum distance from the original \(n\) hotels to the new one is equal to \(d\).
|
The first line contains two integers \(n\) and \(d\) (\(1\leq n\leq 100\), \(1\leq d\leq 10^9\)) β the number of Sonya's hotels and the needed minimum distance from a new hotel to all others.The second line contains \(n\) different integers in strictly increasing order \(x_1, x_2, \ldots, x_n\) (\(-10^9\leq x_i\leq 10^9\)) β coordinates of Sonya's hotels.
|
Print the number of cities where Sonya can build a new hotel so that the minimum distance from this hotel to all others is equal to \(d\).
|
In the first example, there are \(6\) possible cities where Sonya can build a hotel. These cities have coordinates \(-6\), \(5\), \(6\), \(12\), \(13\), and \(19\).In the second example, there are \(5\) possible cities where Sonya can build a hotel. These cities have coordinates \(2\), \(6\), \(13\), \(16\), and \(21\).
|
Input: 4 3-3 2 9 16 | Output: 6
|
Beginner
| 1 | 1,045 | 357 | 138 | 10 |
1,696 |
G
|
1696G
|
G. Fishingprince Plays With Array Again
| 3,300 |
brute force; data structures; geometry; math
|
Suppose you are given a 1-indexed sequence \(a\) of non-negative integers, whose length is \(n\), and two integers \(x\), \(y\). In consecutive \(t\) seconds (\(t\) can be any positive real number), you can do one of the following operations: Select \(1\le i<n\), decrease \(a_i\) by \(x\cdot t\), and decrease \(a_{i+1}\) by \(y\cdot t\). Select \(1\le i<n\), decrease \(a_i\) by \(y\cdot t\), and decrease \(a_{i+1}\) by \(x\cdot t\). Define the minimum amount of time (it might be a real number) required to make all elements in the sequence less than or equal to \(0\) as \(f(a)\).For example, when \(x=1\), \(y=2\), it takes \(3\) seconds to deal with the array \([3,1,1,3]\). We can: In the first \(1.5\) seconds do the second operation with \(i=1\). In the next \(1.5\) seconds do the first operation with \(i=3\). We can prove that it's not possible to make all elements less than or equal to \(0\) in less than \(3\) seconds, so \(f([3,1,1,3])=3\).Now you are given a 1-indexed sequence \(b\) of positive integers, whose length is \(n\). You are also given positive integers \(x\), \(y\). Process \(q\) queries of the following two types: 1 k v: change \(b_k\) to \(v\). 2 l r: print \(f([b_l,b_{l+1},\dots,b_r])\).
|
The first line of input contains two integers \(n\) and \(q\) (\(2\le n\le 2\cdot 10^5\), \(1\le q\le 2\cdot 10^5\)).The second line of input contains two integers \(x\) and \(y\) (\(1\le x,y\le 10^6\)).The third line of input contains \(n\) integers \(b_1,b_2,\ldots,b_n\) (\(1\le b_i\le 10^6\)).This is followed by \(q\) lines. Each of these \(q\) lines contains three integers. The first integer \(op\) is either \(1\) or \(2\). If it is \(1\), it is followed by two integers \(k\), \(v\) (\(1\le k\le n\), \(1\le v\le 10^6\)). It means that you should change \(b_k\) to \(v\). If it is \(2\), it is followed by two integers \(l\), \(r\) (\(1\le l<r\le n\)). It means that you should print \(f([b_l,b_{l+1},\dots,b_r])\).
|
For each query of type \(2\), print one real number β the answer to the query. Your answer is considered correct if its absolute error or relative error does not exceed \(10^{-9}\).
|
Let's analyse the sample.In the first query, we are asked to compute \(f([3,1,1,4])\). The answer is \(3.5\). One optimal sequence of operations is: In the first \(1.5\) seconds do the second operation with \(i=1\). In the next \(2\) seconds do the first operation with \(i=3\). In the third query, we are asked to compute \(f([1,1,1])\). The answer is \(1\). One optimal sequence of operations is: In the first \(0.5\) seconds do the second operation with \(i=1\). In the next \(0.5\) seconds do the first operation with \(i=2\).
|
Input: 4 3 1 2 3 1 1 4 2 1 4 1 1 1 2 1 3 | Output: 3.500000000000000 1.000000000000000
|
Master
| 4 | 1,224 | 724 | 181 | 16 |
2,112 |
D
|
2112D
|
D. Reachability and Tree
| 1,700 |
constructive algorithms; dfs and similar; graphs; trees
|
Let \(u\) and \(v\) be two distinct vertices in a directed graph. Let's call the ordered pair \((u, v)\) good if there exists a path from vertex \(u\) to vertex \(v\) along the edges of the graph.You are given an undirected tree with \(n\) vertices and \(n - 1\) edges. Determine whether it is possible to assign a direction to each edge of this tree so that the number of good pairs in it is exactly \(n\). If it is possible, print any way to direct the edges resulting in exactly \(n\) good pairs. One possible directed version of the tree for the first test case.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of vertices in the tree.The next \(n - 1\) lines describe the edges. The \(i\)-th line contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \neq v_i\)) β the vertices connected by the \(i\)-th edge.It is guaranteed that the edges in each test case form an undirected tree and that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print ""NO"" (case-insensitive) if it is impossible to direct all edges of the tree and obtain exactly \(n\) good pairs of vertices.Otherwise, print ""YES"" (case-insensitive) and then print \(n - 1\) pairs of integers \(u_i\) and \(v_i\) separated by spaces β the edges directed from \(u_i\) to \(v_i\).The edges can be printed in any order. If there are multiple answers, output any.
|
The tree from the first test case and its possible directed version are shown in the legend above. In this version, there are exactly \(5\) good pairs of vertices: \((3, 5)\), \((3, 1)\), \((3, 2)\), \((1, 2)\), and \((4, 2)\).One possible directed version of the tree from the second test case is shown below: In the presented answer, there are exactly \(5\) good pairs of vertices: \((2, 1)\), \((3, 1)\), \((4, 1)\), \((5, 4)\), and \((5, 1)\).In the third test case, there are only two directed pairs of vertices, but for any direction of the edge, only one pair will be good.
|
Input: 451 22 41 33 551 21 31 44 522 143 11 22 4 | Output: YES 1 2 3 1 3 5 4 2 YES 2 1 3 1 4 1 5 4 NO YES 1 3 2 1 2 4
|
Medium
| 4 | 566 | 569 | 405 | 21 |
1,387 |
B2
|
1387B2
|
B2. Village (Maximum)
| 2,500 |
*special; dfs and similar; trees
|
This problem is split into two tasks. In this task, you are required to find the maximum possible answer. In the task Village (Minimum) you are required to find the minimum possible answer. Each task is worth \(50\) points.There are \(N\) houses in a certain village. A single villager lives in each of the houses. The houses are connected by roads. Each road connects two houses and is exactly \(1\) kilometer long. From each house it is possible to reach any other using one or several consecutive roads. In total there are \(N-1\) roads in the village.One day all villagers decided to move to different houses β that is, after moving each house should again have a single villager living in it, but no villager should be living in the same house as before. We would like to know the largest possible total length in kilometers of the shortest paths between the old and the new houses for all villagers. Example village with seven houses For example, if there are seven houses connected by roads as shown on the figure, the largest total length is \(18\) km (this can be achieved by moving \(1 \to 7\), \(2 \to 3\), \(3 \to 4\), \(4 \to 1\), \(5 \to 2\), \(6 \to 5\), \(7 \to 6\)).Write a program that finds the largest total length of the shortest paths in kilometers and an example assignment of the new houses to the villagers.
|
The first line contains an integer \(N\) (\(1 < N \le 10^5\)). Houses are numbered by consecutive integers \(1, 2, \ldots, N\).Then \(N-1\) lines follow that describe the roads. Each line contains two integers \(a\) and \(b\) (\(1 \le a, b \le N\), \(a \neq b\)) denoting that there is a road connecting houses \(a\) and \(b\).
|
In the first line output the largest total length of the shortest paths in kilometers.In the second line describe one valid assignment of the new houses with the largest total length: \(N\) space-separated distinct integers \(v_1, v_2, \ldots, v_N\). For each \(i\), \(v_i\) is the house number where villager from the house \(i\) should move (\(v_i \neq i\)). If there are several valid assignments, output any of those.
|
Input: 4 1 2 2 3 3 4 | Output: 8 4 3 2 1
|
Expert
| 3 | 1,332 | 327 | 421 | 13 |
|
105 |
A
|
105A
|
A. Transmigration
| 1,700 |
implementation
|
In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes are characterized by different skills. Unfortunately, the skills that are uncommon for the given character's class are quite difficult to obtain. To avoid this limitation, there is the so-called transmigration. Transmigration is reincarnation of the character in a new creature. His soul shifts to a new body and retains part of his experience from the previous life. As a result of transmigration the new character gets all the skills of the old character and the skill levels are reduced according to the k coefficient (if the skill level was equal to x, then after transmigration it becomes equal to [kx], where [y] is the integral part of y). If some skill's levels are strictly less than 100, these skills are forgotten (the character does not have them any more). After that the new character also gains the skills that are specific for his class, but are new to him. The levels of those additional skills are set to 0. Thus, one can create a character with skills specific for completely different character classes via transmigrations. For example, creating a mage archer or a thief warrior is possible. You are suggested to solve the following problem: what skills will the character have after transmigration and what will the levels of those skills be?
|
The first line contains three numbers n, m and k β the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; n and m are integers, and k is a real number with exactly two digits after decimal point (1 β€ n, m β€ 20, 0.01 β€ k β€ 0.99).Then follow n lines, each of which describes a character's skill in the form ""name exp"" β the skill's name and the character's skill level: name is a string and exp is an integer in range from 0 to 9999, inclusive. Then follow m lines each of which contains names of skills specific for the class, into which the character transmigrates. All names consist of lowercase Latin letters and their lengths can range from 1 to 20 characters, inclusive. All character's skills have distinct names. Besides the skills specific for the class into which the player transmigrates also have distinct names.
|
Print on the first line number z β the number of skills the character will have after the transmigration. Then print z lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order.
|
Input: 5 4 0.75axe 350impaler 300ionize 80megafire 120magicboost 220healmegafireshieldmagicboost | Output: 6axe 262heal 0impaler 225magicboost 165megafire 0shield 0
|
Medium
| 1 | 1,584 | 954 | 260 | 1 |
|
372 |
A
|
372A
|
A. Counting Kangaroos is Fun
| 1,600 |
binary search; greedy; sortings; two pointers
|
There are n kangaroos with pockets. Each kangaroo has a size (integer number). A kangaroo can go into another kangaroo's pocket if and only if the size of kangaroo who hold the kangaroo is at least twice as large as the size of kangaroo who is held.Each kangaroo can hold at most one kangaroo, and the kangaroo who is held by another kangaroo cannot hold any kangaroos.The kangaroo who is held by another kangaroo cannot be visible from outside. Please, find a plan of holding kangaroos with the minimal number of kangaroos who is visible.
|
The first line contains a single integer β n (1 β€ n β€ 5Β·105). Each of the next n lines contains an integer si β the size of the i-th kangaroo (1 β€ si β€ 105).
|
Output a single integer β the optimal number of visible kangaroos.
|
Input: 825769842 | Output: 5
|
Medium
| 4 | 539 | 157 | 66 | 3 |
|
1,335 |
A
|
1335A
|
A. Candies and Two Sisters
| 800 |
math
|
There are two sisters Alice and Betty. You have \(n\) candies. You want to distribute these \(n\) candies between two sisters in such a way that: Alice will get \(a\) (\(a > 0\)) candies; Betty will get \(b\) (\(b > 0\)) candies; each sister will get some integer number of candies; Alice will get a greater amount of candies than Betty (i.e. \(a > b\)); all the candies will be given to one of two sisters (i.e. \(a+b=n\)). Your task is to calculate the number of ways to distribute exactly \(n\) candies between sisters in a way described above. Candies are indistinguishable.Formally, find the number of ways to represent \(n\) as the sum of \(n=a+b\), where \(a\) and \(b\) are positive integers and \(a>b\).You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.The only line of a test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^9\)) β the number of candies you have.
|
For each test case, print the answer β the number of ways to distribute exactly \(n\) candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print \(0\).
|
For the test case of the example, the \(3\) possible ways to distribute candies are: \(a=6\), \(b=1\); \(a=5\), \(b=2\); \(a=4\), \(b=3\).
|
Input: 6 7 1 2 3 2000000000 763243547 | Output: 3 0 0 1 999999999 381621773
|
Beginner
| 1 | 760 | 255 | 221 | 13 |
1,463 |
A
|
1463A
|
A. Dungeon
| 1,100 |
binary search; math
|
You are playing a new computer game in which you have to fight monsters. In a dungeon you are trying to clear, you met three monsters; the first of them has \(a\) health points, the second has \(b\) health points, and the third has \(c\).To kill the monsters, you can use a cannon that, when fired, deals \(1\) damage to the selected monster. Every \(7\)-th (i. e. shots with numbers \(7\), \(14\), \(21\) etc.) cannon shot is enhanced and deals \(1\) damage to all monsters, not just one of them. If some monster's current amount of health points is \(0\), it can't be targeted by a regular shot and does not receive damage from an enhanced shot.You want to pass the dungeon beautifully, i. e., kill all the monsters with the same enhanced shot (i. e. after some enhanced shot, the health points of each of the monsters should become equal to \(0\) for the first time). Each shot must hit a monster, i. e. each shot deals damage to at least one monster.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of a single line that contains three integers \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 10^8\)) β the number of health points each monster has.
|
For each test case, print YES if you can kill all the monsters with the same enhanced shot. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
|
In the first test case, you can do as follows: \(1\)-th shot to the first monster, \(2\)-th shot to the second monster, \(3\)-th shot to the third monster, \(4\)-th shot to the first monster, \(5\)-th shot to the third monster, \(6\)-th shot to the third monster, and \(7\)-th enhanced shot will kill all the monsters.In the second test case, you can't kill monsters with the same enhanced shot, because the total number of health points of monsters is \(3\), and you will kill them in the first 3 shots.
|
Input: 3 3 2 4 1 1 1 10 1 7 | Output: YES NO NO
|
Easy
| 2 | 954 | 266 | 227 | 14 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.