Problem ID
stringlengths 2
6
| Problem Description
stringlengths 0
7.52k
| Rating
float64 800
3.5k
| math
bool 2
classes | greedy
bool 2
classes | implementation
bool 2
classes | dp
bool 2
classes | data structures
bool 2
classes | constructive algorithms
bool 2
classes | brute force
bool 2
classes | binary search
bool 2
classes | sortings
bool 2
classes | graphs
bool 2
classes | __index_level_0__
int64 3
9.98k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
15B | Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of _n_u2009×u2009_m_ cells and a robotic arm. Each cell of the field is a 1u2009×u20091 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized — if you move one of the lasers by a vector, another one moves by the same vector. The following facts about the experiment are known: initially the whole field is covered with a chocolate bar of the size _n_u2009×u2009_m_, both lasers are located above the field and are active; the chocolate melts within one cell of the field at which the laser is pointed; all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells; at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman. You are given _n_, _m_ and the cells (_x_1,u2009_y_1) and (_x_2,u2009_y_2), where the lasers are initially pointed at (_x__i_ is a column number, _y__i_ is a row number). Rows are numbered from 1 to _m_ from top to bottom and columns are numbered from 1 to _n_ from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions. Input The first line contains one integer number _t_ (1u2009≤u2009_t_u2009≤u200910000) — the number of test sets. Each of the following _t_ lines describes one test set. Each line contains integer numbers _n_, _m_, _x_1, _y_1, _x_2, _y_2, separated by a space (2u2009≤u2009_n_,u2009_m_u2009≤u2009109, 1u2009≤u2009_x_1,u2009_x_2u2009≤u2009_n_, 1u2009≤u2009_y_1,u2009_y_2u2009≤u2009_m_). Cells (_x_1,u2009_y_1) and (_x_2,u2009_y_2) are distinct. Output Each of the _t_ lines of the output should contain the answer to the corresponding input test set. Examples Input 2 4 4 1 1 3 3 4 3 1 1 2 2 | 1,800 | true | false | false | false | false | false | false | false | false | false | 9,919 |
1321A | Polycarp is preparing the first programming contest for robots. There are $$$n$$$ problems in it, and a lot of robots are going to participate in it. Each robot solving the problem $$$i$$$ gets $$$p_i$$$ points, and the score of each robot in the competition is calculated as the sum of $$$p_i$$$ over all problems $$$i$$$ solved by it. For each problem, $$$p_i$$$ is an integer not less than $$$1$$$. Two corporations specializing in problem-solving robot manufacturing, "Robo-Coder Inc." and "BionicSolver Industries", are going to register two robots (one for each corporation) for participation as well. Polycarp knows the advantages and flaws of robots produced by these companies, so, for each problem, he knows precisely whether each robot will solve it during the competition. Knowing this, he can try predicting the results — or manipulating them. For some reason (which absolutely cannot involve bribing), Polycarp wants the "Robo-Coder Inc." robot to outperform the "BionicSolver Industries" robot in the competition. Polycarp wants to set the values of $$$p_i$$$ in such a way that the "Robo-Coder Inc." robot gets strictly more points than the "BionicSolver Industries" robot. However, if the values of $$$p_i$$$ will be large, it may look very suspicious — so Polycarp wants to minimize the maximum value of $$$p_i$$$ over all problems. Can you help Polycarp to determine the minimum possible upper bound on the number of points given for solving the problems? Input The first line contains one integer $$$n$$$ ($$$1 le n le 100$$$) — the number of problems. The second line contains $$$n$$$ integers $$$r_1$$$, $$$r_2$$$, ..., $$$r_n$$$ ($$$0 le r_i le 1$$$). $$$r_i = 1$$$ means that the "Robo-Coder Inc." robot will solve the $$$i$$$-th problem, $$$r_i = 0$$$ means that it won't solve the $$$i$$$-th problem. The third line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$0 le b_i le 1$$$). $$$b_i = 1$$$ means that the "BionicSolver Industries" robot will solve the $$$i$$$-th problem, $$$b_i = 0$$$ means that it won't solve the $$$i$$$-th problem. Output If "Robo-Coder Inc." robot cannot outperform the "BionicSolver Industries" robot by any means, print one integer $$$-1$$$. Otherwise, print the minimum possible value of $$$max limits_{i = 1}^{n} p_i$$$, if all values of $$$p_i$$$ are set in such a way that the "Robo-Coder Inc." robot gets strictly more points than the "BionicSolver Industries" robot. Examples Input 5 1 1 1 0 0 0 1 1 1 1 Input 9 1 0 0 0 0 0 0 0 1 0 1 1 0 1 1 1 1 0 Note In the first example, one of the valid score assignments is $$$p = [3, 1, 3, 1, 1]$$$. Then the "Robo-Coder" gets $$$7$$$ points, the "BionicSolver" — $$$6$$$ points. In the second example, both robots get $$$0$$$ points, and the score distribution does not matter. In the third example, both robots solve all problems, so their points are equal. | 900 | false | true | false | false | false | false | false | false | false | false | 4,109 |
1703F | You are given an array $$$a_1, a_2, dots a_n$$$. Count the number of pairs of indices $$$1 leq i, j leq n$$$ such that $$$a_i < i < a_j < j$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0— the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 leq a_i leq 10^9$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of pairs of indices satisfying the condition in the statement. Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). Example Input 5 8 1 1 2 3 8 2 1 4 2 1 2 10 0 2 1 6 3 4 1 2 8 3 2 1 1000000000 3 0 1000000000 2 Note For the first test cases the pairs are $$$(i, j)$$$ = $$${(2, 4), (2, 8), (3, 8)}$$$. The pair $$$(2, 4)$$$ is true because $$$a_2 = 1$$$, $$$a_4 = 3$$$ and $$$1 < 2 < 3 < 4$$$. The pair $$$(2, 8)$$$ is true because $$$a_2 = 1$$$, $$$a_8 = 4$$$ and $$$1 < 2 < 4 < 8$$$. The pair $$$(3, 8)$$$ is true because $$$a_3 = 2$$$, $$$a_8 = 4$$$ and $$$2 < 3 < 4 < 8$$$. | 1,300 | false | true | false | true | true | false | false | true | true | false | 2,057 |
1468A | Let's call a sequence $$$b_1, b_2, b_3 dots, b_{k - 1}, b_k$$$ almost increasing if $$$$$$min(b_1, b_2) le min(b_2, b_3) le dots le min(b_{k - 1}, b_k).$$$$$$ In particular, any sequence with no more than two elements is almost increasing. You are given a sequence of integers $$$a_1, a_2, dots, a_n$$$. Calculate the length of its longest almost increasing subsequence. You'll be given $$$t$$$ test cases. Solve each test case independently. Reminder: a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of independent test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^5$$$)xa0— the length of the sequence $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the sequence itself. It's guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$5 cdot 10^5$$$. Output For each test case, print one integerxa0— the length of the longest almost increasing subsequence. Example Input 3 8 1 2 7 3 2 1 2 3 2 2 1 7 4 1 5 2 6 3 7 Note In the first test case, one of the optimal answers is subsequence $$$1, 2, 7, 2, 2, 3$$$. In the second and third test cases, the whole sequence $$$a$$$ is already almost increasing. | 2,200 | false | true | false | true | true | false | false | false | false | false | 3,352 |
1399B | You have $$$n$$$ gifts and you want to give all of them to children. Of course, you don't want to offend anyone, so all gifts should be equal between each other. The $$$i$$$-th gift consists of $$$a_i$$$ candies and $$$b_i$$$ oranges. During one move, you can choose some gift $$$1 le i le n$$$ and do one of the following operations: eat exactly one candy from this gift (decrease $$$a_i$$$ by one); eat exactly one orange from this gift (decrease $$$b_i$$$ by one); eat exactly one candy and exactly one orange from this gift (decrease both $$$a_i$$$ and $$$b_i$$$ by one). Of course, you can not eat a candy or orange if it's not present in the gift (so neither $$$a_i$$$ nor $$$b_i$$$ can become less than zero). As said above, all gifts should be equal. This means that after some sequence of moves the following two conditions should be satisfied: $$$a_1 = a_2 = dots = a_n$$$ and $$$b_1 = b_2 = dots = b_n$$$ (and $$$a_i$$$ equals $$$b_i$$$ is not necessary). Your task is to find the minimum number of moves required to equalize all the given gifts. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) — the number of gifts. 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 number of candies in the $$$i$$$-th gift. The third line of the test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^9$$$), where $$$b_i$$$ is the number of oranges in the $$$i$$$-th gift. Output For each test case, print one integer: the minimum number of moves required to equalize all the given gifts. Example Input 5 3 3 5 6 3 2 3 5 1 2 3 4 5 5 4 3 2 1 3 1 1 1 2 2 2 6 1 1000000000 1000000000 1000000000 1000000000 1000000000 1 1 1 1 1 1 3 10 12 8 7 5 4 Output 6 16 0 4999999995 7 Note In the first test case of the example, we can perform the following sequence of moves: choose the first gift and eat one orange from it, so $$$a = [3, 5, 6]$$$ and $$$b = [2, 2, 3]$$$; choose the second gift and eat one candy from it, so $$$a = [3, 4, 6]$$$ and $$$b = [2, 2, 3]$$$; choose the second gift and eat one candy from it, so $$$a = [3, 3, 6]$$$ and $$$b = [2, 2, 3]$$$; choose the third gift and eat one candy and one orange from it, so $$$a = [3, 3, 5]$$$ and $$$b = [2, 2, 2]$$$; choose the third gift and eat one candy from it, so $$$a = [3, 3, 4]$$$ and $$$b = [2, 2, 2]$$$; choose the third gift and eat one candy from it, so $$$a = [3, 3, 3]$$$ and $$$b = [2, 2, 2]$$$. | 800 | false | true | false | false | false | false | false | false | false | false | 3,678 |
1841F | Monocarp plays a strategic computer game in which he develops a city. The city is inhabited by creatures of four different races — humans, elves, orcs, and dwarves. Each inhabitant of the city has a happiness value, which is an integer. It depends on how many creatures of different races inhabit the city. Specifically, the happiness of each inhabitant is $$$0$$$ by default; it increases by $$$1$$$ for each other creature of the same race and decreases by $$$1$$$ for each creature of a hostile race. Humans are hostile to orcs (and vice versa), and elves are hostile to dwarves (and vice versa). At the beginning of the game, Monocarp's city is not inhabited by anyone. During the game, $$$n$$$ groups of creatures will come to his city, wishing to settle there. The $$$i$$$-th group consists of $$$a_i$$$ humans, $$$b_i$$$ orcs, $$$c_i$$$ elves, and $$$d_i$$$ dwarves. Each time, Monocarp can either accept the entire group of creatures into the city, or reject the entire group. The game calculates Monocarp's score according to the following formula: $$$m + k$$$, where $$$m$$$ is the number of inhabitants in the city, and $$$k$$$ is the sum of the happiness values of all creatures in the city. Help Monocarp earn the maximum possible number of points by the end of the game! Input The first line contains an integer $$$n$$$ ($$$1 leq n leq 3 cdot 10^{5}$$$) — the number of groups of creatures that come to Monocarp's city. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains four integers $$$a_i$$$, $$$b_i$$$, $$$c_i$$$, and $$$d_i$$$ ($$$0 leq a_i, b_i, c_i, d_i leq 10^{9}$$$) — the number of humans, orcs, elves and dwarves (respectively) in the $$$i$$$-th group. Output Output a single number — the maximum score Monocarp can have by the end of the game. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. That is, if your answer is $$$a$$$, and the jury's answer is $$$b$$$, then the solution will be accepted if $$$frac{a-b}{max(1,b)} le 10^{-9}$$$. Note that the correct answer is always an integer, but sometimes it doesn't fit in $$$64$$$-bit integer types, so you are allowed to print it as a non-integer number. Examples Input 5 0 0 1 0 1 3 4 2 2 5 1 2 4 5 4 3 1 4 4 5 Input 4 3 3 1 5 5 1 5 3 4 4 4 1 1 3 4 4 Note In the first example, the best course of action is to accept all of the groups. In the second example, the best course of action is to accept the groups $$$2$$$ and $$$3$$$, and decline the groups $$$1$$$ and $$$4$$$. | 2,700 | false | false | false | false | false | false | false | false | true | false | 1,240 |
616E | Problem - 616E - Codeforces =============== xa0 . The modulo operator _a_ mod _b_ stands for the remainder after dividing _a_ by _b_. For example 10 mod 3 = 1. Input The only line contains two integers _n_,u2009_m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091013) — the parameters of the sum. Output Print integer _s_ — the value of the required sum modulo 109u2009+u20097. Examples Input 3 4 Output 4 Input 4 4 Output 1 Input 1 1 Output 0 | 2,200 | true | false | true | false | false | false | false | false | false | false | 7,371 |
277D | Many of you must be familiar with the Google Code Jam round rules. Let us remind you of some key moments that are crucial to solving this problem. During the round, the participants are suggested to solve several problems, each divided into two subproblems: an easy one with small limits (Small input), and a hard one with large limits (Large input). You can submit a solution for Large input only after you've solved the Small input for this problem. There are no other restrictions on the order of solving inputs. In particular, the participant can first solve the Small input, then switch to another problem, and then return to the Large input. Solving each input gives the participant some number of points (usually different for each problem). This takes into account only complete solutions that work correctly on all tests of the input. The participant gets the test result of a Small input right after he submits it, but the test result of a Large input are out only after the round's over. In the final results table the participants are sorted by non-increasing of received points. If the points are equal, the participants are sorted by ascending of time penalty. By the Google Code Jam rules the time penalty is the time when the last correct solution was submitted. Vasya decided to check out a new tactics on another round. As soon as the round begins, the boy quickly read all the problems and accurately evaluated the time it takes to solve them. Specifically, for each one of the _n_ problems Vasya knows five values: Solving the Small input of the _i_-th problem gives to the participant _scoreSmall__i_ points, and solving the Large input gives _scoreLarge__i_ more points. That is, the maximum number of points you can get for the _i_-th problem equals _scoreSmall__i_u2009+u2009_scoreLarge__i_. Writing the solution for the Small input of the _i_-th problem takes exactly _timeSmall__i_ minutes for Vasya. Improving this code and turning it into the solution of the Large input takes another _timeLarge__i_ minutes. Vasya's had much practice, so he solves all Small inputs from the first attempt. But it's not so easy with the Large input: there is the _probFail__i_ probability that the solution to the Large input will turn out to be wrong at the end of the round. Please keep in mind that these solutions do not affect the participants' points and the time penalty. A round lasts for _t_ minutes. The time for reading problems and submitting solutions can be considered to equal zero. Vasya is allowed to submit a solution exactly at the moment when the round ends. Vasya wants to choose a set of inputs and the order of their solution so as to make the expectation of the total received points maximum possible. If there are multiple ways to do this, he needs to minimize the expectation of the time penalty. Help Vasya to cope with this problem. Input The first line contains two integers _n_ and _t_ (1u2009≤u2009_n_u2009≤u20091000,u20091u2009≤u2009_t_u2009≤u20091560). Then follow _n_ lines, each containing 5 numbers: _scoreSmall__i_,u2009_scoreLarge__i_,u2009_timeSmall__i_,u2009_timeLarge__i_,u2009_probFail__i_ (1u2009≤u2009_scoreSmall__i_,u2009_scoreLarge__i_u2009≤u2009109,u20091u2009≤u2009_timeSmall__i_,u2009_timeLarge__i_u2009≤u20091560,u20090u2009≤u2009_probFail__i_u2009≤u20091). _probFail__i_ are real numbers, given with at most 6 digits after the decimal point. All other numbers in the input are integers. Output Print two real numbers — the maximum expectation of the total points and the corresponding minimum possible time penalty expectation. The answer will be considered correct if the absolute or relative error doesn't exceed 10u2009-u20099. Examples Input 3 40 10 20 15 4 0.5 4 100 21 1 0.99 1 4 1 1 0.25 Input 1 1 100000000 200000000 1 1 0 Note In the first sample one of the optimal orders of solving problems is: 1. The Small input of the third problem. 2. The Small input of the first problem. 3. The Large input of the third problem. 4. The Large input of the first problem. Note that if you solve the Small input of the second problem instead of two inputs of the third one, then total score expectation will be the same but the time penalty expectation will be worse (38). | 2,800 | false | false | false | true | false | false | false | false | false | false | 8,724 |
673A | Problem - 673A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials xa0— the number of interesting minutes. The second line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t_1u2009<u2009_t_2u2009<u2009... _t__n_u2009≤u200990), given in the increasing order. Output Print the number of minutes Limak will watch the game. Examples Input 3 7 20 88 Output 35 Input 9 16 20 30 40 50 60 70 80 90 Output 15 Input 9 15 20 30 40 50 60 70 80 90 Output 90 Note In the first sample, minutes 21,u200922,u2009...,u200935 are all boring and thus Limak will turn TV off immediately after the 35-th minute. So, he would watch the game for 35 minutes. In the second sample, the first 15 minutes are boring. In the third sample, there are no consecutive 15 boring minutes. So, Limak will watch the whole game. | 800 | false | false | true | false | false | false | false | false | false | false | 7,133 |
1208A | Problem - 1208A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *900 No tag edit access → Contest materials = a$$$; $$$f(1) = b$$$; $$$f(n) = f(n-1) oplus f(n-2)$$$ when $$$n > 1$$$, where $$$oplus$$$ denotes the $$$. You have to answer for $$$T$$$ independent test cases. Input The input contains one or more independent test cases. The first line of input contains a single integer $$$T$$$ ($$$1 le T le 10^3$$$), the number of test cases. Each of the $$$T$$$ following lines contains three space-separated integers $$$a$$$, $$$b$$$, and $$$n$$$ ($$$0 le a, b, n le 10^9$$$) respectively. Output For each test case, output $$$f(n)$$$. Example Input 3 3 4 2 4 5 0 325 265 1231232 Output 7 4 76 Note In the first example, $$$f(2) = f(0) oplus f(1) = 3 oplus 4 = 7$$$. | 900 | true | false | false | false | false | false | false | false | false | false | 4,654 |
154A | Sergey attends lessons of the _N_-ish language. Each lesson he receives a hometask. This time the task is to translate some sentence to the _N_-ish language. Sentences of the _N_-ish language can be represented as strings consisting of lowercase Latin letters without spaces or punctuation marks. Sergey totally forgot about the task until half an hour before the next lesson and hastily scribbled something down. But then he recollected that in the last lesson he learned the grammar of _N_-ish. The spelling rules state that _N_-ish contains some "forbidden" pairs of letters: such letters can never occur in a sentence next to each other. Also, the order of the letters doesn't matter (for example, if the pair of letters "ab" is forbidden, then any occurrences of substrings "ab" and "ba" are also forbidden). Also, each pair has different letters and each letter occurs in no more than one forbidden pair. Now Sergey wants to correct his sentence so that it doesn't contain any "forbidden" pairs of letters that stand next to each other. However, he is running out of time, so he decided to simply cross out some letters from the sentence. What smallest number of letters will he have to cross out? When a letter is crossed out, it is "removed" so that the letters to its left and right (if they existed), become neighboring. For example, if we cross out the first letter from the string "aba", we get the string "ba", and if we cross out the second letter, we get "aa". Input The first line contains a non-empty string _s_, consisting of lowercase Latin letters — that's the initial sentence in _N_-ish, written by Sergey. The length of string _s_ doesn't exceed 105. The next line contains integer _k_ (0u2009≤u2009_k_u2009≤u200913) — the number of forbidden pairs of letters. Next _k_ lines contain descriptions of forbidden pairs of letters. Each line contains exactly two different lowercase Latin letters without separators that represent the forbidden pairs. It is guaranteed that each letter is included in no more than one pair. Output Print the single number — the smallest number of letters that need to be removed to get a string without any forbidden pairs of neighboring letters. Please note that the answer always exists as it is always possible to remove all letters. Note In the first sample you should remove two letters b. In the second sample you should remove the second or the third letter. The second restriction doesn't influence the solution. | 1,600 | false | true | false | false | false | false | false | false | false | false | 9,259 |
1416F | Another dull quarantine day was going by when BThero decided to start researching matrices of size $$$n imes m$$$. The rows are numerated $$$1$$$ through $$$n$$$ from top to bottom, and the columns are numerated $$$1$$$ through $$$m$$$ from left to right. The cell in the $$$i$$$-th row and $$$j$$$-th column is denoted as $$$(i, j)$$$. For each cell $$$(i, j)$$$ BThero had two values: 1. The cost of the cell, which is a single positive integer. 2. The direction of the cell, which is one of characters L, R, D, U. Those characters correspond to transitions to adjacent cells $$$(i, j - 1)$$$, $$$(i, j + 1)$$$, $$$(i + 1, j)$$$ or $$$(i - 1, j)$$$, respectively. No transition pointed outside of the matrix. Let us call a cell $$$(i_2, j_2)$$$ reachable from $$$(i_1, j_1)$$$, if, starting from $$$(i_1, j_1)$$$ and repeatedly moving to the adjacent cell according to our current direction, we will, sooner or later, visit $$$(i_2, j_2)$$$. BThero decided to create another matrix from the existing two. For a cell $$$(i, j)$$$, let us denote $$$S_{i, j}$$$ as a set of all reachable cells from it (including $$$(i, j)$$$ itself). Then, the value at the cell $$$(i, j)$$$ in the new matrix will be equal to the sum of costs of all cells in $$$S_{i, j}$$$. After quickly computing the new matrix, BThero immediately sent it to his friends. However, he did not save any of the initial matrices! Help him to restore any two valid matrices, which produce the current one. Input The first line of input file contains a single integer $$$T$$$ ($$$1 le T le 100$$$) denoting the number of test cases. The description of $$$T$$$ test cases follows. First line of a test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n cdot m le 10^5$$$). Each of the following $$$n$$$ lines contain exactly $$$m$$$ integers — the elements of the produced matrix. Each element belongs to the segment $$$[2, 10^9]$$$. It is guaranteed that $$$sum{(n cdot m)}$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, if an answer does not exist, print a single word NO. Otherwise, print YES and both matrices in the same format as in the input. The first matrix should be the cost matrix and the second matrix should be the direction matrix. All integers in the cost matrix should be positive. All characters in the direction matrix should be valid. No direction should point outside of the matrix. | 3,300 | false | true | true | false | false | false | false | false | false | false | 3,597 |
1767A | You are given a non-degenerate triangle (a non-degenerate triangle is a triangle with positive area). The vertices of the triangle have coordinates $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$ and $$$(x_3, y_3)$$$. You want to draw a straight line to cut the triangle into two non-degenerate triangles. Furthermore, the line you draw should be either horizontal or vertical. Can you draw the line to meet all the constraints? Here are some suitable ways to draw the line: However, these ways to draw the line are not suitable (the first line cuts the triangle into a triangle and a quadrangle; the second line doesn't cut the triangle at all; the third line is neither horizontal nor vertical): Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of four lines. The first of them is empty. The $$$i$$$-th of the next three lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le 10^8$$$) — the coordinates of the $$$i$$$-th vertex of the triangle. Additional constraint on the input: in each test case, the triangle formed by three vertices has positive area (i.u2009e. it is non-degenerate). Output For each test case, print YES if it is possible to cut the triangle according to the statement, or NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). Example Input 4 4 7 6 8 3 5 4 5 4 7 6 8 5 8 1 8 2 5 3 6 6 6 6 3 | 800 | false | false | true | false | false | false | false | false | false | false | 1,697 |
845C | Problem - 845C - Codeforces =============== xa0 — the number of shows. Each of the next _n_ lines contains two integers _l__i_ and _r__i_ (0u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009109) — starting and ending time of _i_-th show. Output If Polycarp is able to check out all the shows using only two TVs then print "YES" (without quotes). Otherwise, print "NO" (without quotes). Examples Input 3 1 2 2 3 4 5 Output YES Input 4 1 2 2 3 2 3 1 2 Output NO | 1,500 | false | true | false | false | true | false | false | false | true | false | 6,382 |
1992E | One of the first programming problems by K1o0n looked like this: "Noobish_Monk has $$$n$$$ $$$(1 le n le 100)$$$ friends. Each of them gave him $$$a$$$ $$$(1 le a le 10000)$$$ apples for his birthday. Delighted with such a gift, Noobish_Monk returned $$$b$$$ $$$(1 le b le min(10000, a cdot n))$$$ apples to his friends. How many apples are left with Noobish_Monk?" K1o0n wrote a solution, but accidentally considered the value of $$$n$$$ as a string, so the value of $$$n cdot a - b$$$ was calculated differently. Specifically: when multiplying the string $$$n$$$ by the integer $$$a$$$, he will get the string $$$s=underbrace{n + n + dots + n + n}_{a ext{times}}$$$ when subtracting the integer $$$b$$$ from the string $$$s$$$, the last $$$b$$$ characters will be removed from it. If $$$b$$$ is greater than or equal to the length of the string $$$s$$$, it will become empty. Learning about this, ErnKor became interested in how many pairs $$$(a, b)$$$ exist for a given $$$n$$$, satisfying the constraints of the problem, on which K1o0n's solution gives the correct answer. "The solution gives the correct answer" means that it outputs a non-empty string, and this string, when converted to an integer, equals the correct answer, i.e., the value of $$$n cdot a - b$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) xa0— the number of test cases. For each test case, a single line of input contains an integer $$$n$$$ ($$$1 le n le 100$$$). It is guaranteed that in all test cases, $$$n$$$ is distinct. Output For each test case, output the answer in the following format: In the first line, output the integer $$$x$$$xa0— the number of bad tests for the given $$$n$$$. In the next $$$x$$$ lines, output two integers $$$a_i$$$ and $$$b_i$$$xa0— such integers that K1o0n's solution on the test "$$$n$$$ $$$a_i$$$ $$$b_i$$$" gives the correct answer. Example Output 3 20 18 219 216 2218 2214 1 165 162 1 1262 2519 Note In the first example, $$$a = 20$$$, $$$b = 18$$$ are suitable, as "$$$ ext{2}$$$" $$$cdot 20 - 18 =$$$ "$$$ ext{22222222222222222222}$$$"$$$- 18 = 22 = 2 cdot 20 - 18$$$ | 1,700 | true | false | true | false | false | true | true | false | false | false | 315 |
883D | Game field is represented by a line of _n_ square cells. In some cells there are packmen, in some cells there are asterisks and the rest of the cells are empty. Packmen eat asterisks. Before the game starts you can choose a movement direction, left or right, for each packman. Once the game begins all the packmen simultaneously start moving according their directions. A packman can't change the given direction. Once a packman enters a cell containing an asterisk, packman immediately eats the asterisk. Once the packman leaves the cell it becomes empty. Each packman moves at speed 1 cell per second. If a packman enters a border cell, the packman stops. Packmen do not interfere with the movement of other packmen; in one cell there can be any number of packmen moving in any directions. Your task is to assign a direction to each packman so that they eat the maximal number of asterisks. If there are multiple ways to assign directions to eat the maximal number of asterisks, you should choose the way which minimizes the time to do that. Input The first line contains integer number _n_ (2u2009≤u2009_n_u2009≤u20091u2009000u2009000) — the number of cells in the game field. The second line contains _n_ characters. If the _i_-th character is '.', the _i_-th cell is empty. If the _i_-th character is '*', the _i_-th cell contains an asterisk. If the _i_-th character is 'P', the _i_-th cell contains a packman. The field contains at least one asterisk and at least one packman. Output Print two integer numbers — the maximal number of asterisks packmen can eat and the minimal time to do it. Note In the first example the leftmost packman should move to the right, the rightmost packman should move to the left. All the asterisks will be eaten, the last asterisk will be eaten after 4 seconds. | 2,500 | true | false | false | true | false | false | false | true | false | false | 6,224 |
1133B | International Women's Day is coming soon! Polycarp is preparing for the holiday. There are $$$n$$$ candy boxes in the shop for sale. The $$$i$$$-th box contains $$$d_i$$$ candies. Polycarp wants to prepare the maximum number of gifts for $$$k$$$ girls. Each gift will consist of exactly two boxes. The girls should be able to share each gift equally, so the total amount of candies in a gift (in a pair of boxes) should be divisible by $$$k$$$. In other words, two boxes $$$i$$$ and $$$j$$$ ($$$i e j$$$) can be combined as a gift if $$$d_i + d_j$$$ is divisible by $$$k$$$. How many boxes will Polycarp be able to give? Of course, each box can be a part of no more than one gift. Polycarp cannot use boxes "partially" or redistribute candies between them. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5, 1 le k le 100$$$) — the number the boxes and the number the girls. The second line of the input contains $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$1 le d_i le 10^9$$$), where $$$d_i$$$ is the number of candies in the $$$i$$$-th box. Output Print one integer — the maximum number of the boxes Polycarp can give as gifts. Examples Input 8 2 1 2 2 3 2 4 6 10 Note In the first example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $$$(2, 3)$$$; $$$(5, 6)$$$; $$$(1, 4)$$$. So the answer is $$$6$$$. In the second example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $$$(6, 8)$$$; $$$(2, 3)$$$; $$$(1, 4)$$$; $$$(5, 7)$$$. So the answer is $$$8$$$. In the third example Polycarp can give the following pairs of boxes (pairs are presented by indices of corresponding boxes): $$$(1, 2)$$$; $$$(6, 7)$$$. So the answer is $$$4$$$. | 1,200 | true | false | false | false | false | false | false | false | false | false | 5,053 |
1156B | You are given a string, consisting of lowercase Latin letters. A pair of neighbouring letters in a string is considered ugly if these letters are also neighbouring in a alphabet. For example, string "abaca" contains ugly pairs at positions $$$(1, 2)$$$ — "ab" and $$$(2, 3)$$$ — "ba". Letters 'a' and 'z' aren't considered neighbouring in a alphabet. Can you rearrange the letters of a given string so that there are no ugly pairs? You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. You also have to answer $$$T$$$ separate queries. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 100$$$) — the number of queries. Each of the next $$$T$$$ lines contains string $$$s$$$ $$$(1 le s le 100)$$$ — the string for the next query. It is guaranteed that it contains only lowercase Latin letters. Note that in hacks you have to set $$$T = 1$$$. Output Print $$$T$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th query. If the answer for the $$$i$$$-th query exists, then print such a rearrangment of letters of the given string that it contains no ugly pairs. You can choose any order of the letters of the given string but you can't add any new letters or remove the existing ones. You can also leave the order the same. If there are multiple answers, print any of them. Otherwise print "No answer" for that query. Example Input 4 abcd gg codeforces abaca Output cadb gg codfoerces No answer Note In the first example answer "bdac" is also correct. The second example showcases the fact that only neighbouring in alphabet letters are not allowed. The same letter is ok. There are lots of valid answers for the third example. | 1,800 | false | true | true | false | false | false | false | false | true | false | 4,929 |
1411C | You are given a $$$n imes n$$$ chessboard. Rows and columns of the board are numbered from $$$1$$$ to $$$n$$$. Cell $$$(x, y)$$$ lies on the intersection of column number $$$x$$$ and row number $$$y$$$. Rook is a chess piece, that can in one turn move any number of cells vertically or horizontally. There are $$$m$$$ rooks ($$$m < n$$$) placed on the chessboard in such a way that no pair of rooks attack each other. I.e. there are no pair of rooks that share a row or a column. In one turn you can move one of the rooks any number of cells vertically or horizontally. Additionally, it shouldn't be attacked by any other rook after movement. What is the minimum number of moves required to place all the rooks on the main diagonal? The main diagonal of the chessboard is all the cells $$$(i, i)$$$, where $$$1 le i le n$$$. Input The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^3$$$). Description of the $$$t$$$ test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$xa0— size of the chessboard and the number of rooks ($$$2 leq n leq 10^5$$$, $$$1 leq m < n$$$). Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$xa0— positions of rooks, $$$i$$$-th rook is placed in the cell $$$(x_i, y_i)$$$ ($$$1 leq x_i, y_i leq n$$$). It's guaranteed that no two rooks attack each other in the initial placement. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each of $$$t$$$ test cases print a single integerxa0— the minimum number of moves required to place all the rooks on the main diagonal. It can be proved that this is always possible. Example Input 4 3 1 2 3 3 2 2 1 1 2 5 3 2 3 3 1 1 2 5 4 4 5 5 1 2 2 3 3 Note Possible moves for the first three test cases: 1. $$$(2, 3) o (2, 2)$$$ 2. $$$(2, 1) o (2, 3)$$$, $$$(1, 2) o (1, 1)$$$, $$$(2, 3) o (2, 2)$$$ 3. $$$(2, 3) o (2, 4)$$$, $$$(2, 4) o (4, 4)$$$, $$$(3, 1) o (3, 3)$$$, $$$(1, 2) o (1, 1)$$$ | 1,700 | false | false | false | false | false | false | false | false | false | true | 3,619 |
1431F | You want to train a neural network model for your graduation work. There are $$$n$$$ images in the dataset, the $$$i$$$-th image's size is $$$a_i$$$ bytes. You don't have any powerful remote servers to train this model so you have to do it on your local machine. But there is a problem: the total size of the dataset is too big for your machine, so you decided to remove some images — though you don't want to make the dataset too weak so you can remove no more than $$$k$$$ images from it. Note that you can only remove images, you can't change their order. You want to remove these images optimally so you came up with a metric (you're a data scientist after all) that allows to measure the result of removals. Consider the array $$$b_1, b_2, ldots, b_m$$$ after removing at most $$$k$$$ images ($$$n - k le m le n$$$). The data from this array will be uploaded to the machine in blocks of $$$x$$$ consecutive elements each. More precisely: elements with indices from $$$1$$$ to $$$x$$$ ($$$b_1, b_2, ldots, b_x$$$) belong to the first block; elements with indices from $$$x + 1$$$ to $$$2x$$$ ($$$b_{x + 1}, b_{x + 2}, ldots, b_{2x}$$$) belong to the second block; elements with indices from $$$2x + 1$$$ to $$$3x$$$ ($$$b_{2x + 1}, b_{2x + 2}, ldots, b_{3x}$$$) belong to the third block; and so on. There will be $$$cnt = leftlceilfrac{m}{x} ight ceil$$$ blocks in total. Note that if $$$m$$$ is not divisible by $$$x$$$ then the last block contains less than $$$x$$$ elements, and it's okay. Let $$$w(i)$$$ be the total size of the $$$i$$$-th block — that is, the sum of sizes of images inside this block. For example, the size of the first block $$$w(1)$$$ is $$$b_1 + b_2 + ldots + b_x$$$, the size of the second block $$$w(2)$$$ is $$$b_{x + 1} + b_{x + 2} + ldots + b_{2x}$$$. The value of the metric you came up with is the maximum block size over the blocks of the resulting dataset. In other words, the value of the metric is $$$maxlimits_{i=1}^{cnt} w(i)$$$. You don't want to overload your machine too much, so you have to remove at most $$$k$$$ images in a way that minimizes the value of the metric described above. Input The first line of the input contains three integers $$$n$$$, $$$k$$$ and $$$x$$$ ($$$1 le n le 10^5$$$; $$$1 le k, x le n$$$) — the number of images in the dataset, the maximum number of images you can remove and the length of each block (except maybe for the last one), respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^5$$$), where $$$a_i$$$ is the size of the $$$i$$$-th image. Output Print one integer: the minimum possible value of the metric described in the problem statement after removing no more than $$$k$$$ images from the dataset. Note In the first example, you can remove the whole array so the answer is $$$0$$$. In the second example, you can remove the first and the last elements of $$$a$$$ and obtain $$$b = [1, 5, 5]$$$. The size of the first (and the only) block is $$$11$$$. So the answer is $$$11$$$. In the third example, you can remove the second element of $$$a$$$ and obtain $$$b = [3, 1, 3, 1, 2]$$$. The size of the first block is $$$8$$$ and the size of the second block is $$$2$$$. So the answer is $$$8$$$. In the fourth example, you can keep the array $$$a$$$ unchanged and obtain $$$b = [2, 2, 1, 2, 2, 1]$$$. The size of the first block is $$$5$$$ as well as the size of the second block. So the answer is $$$5$$$. | 2,100 | false | true | false | false | false | false | false | true | false | false | 3,504 |
1805A | There is an array $$$a$$$ consisting of non-negative integers. You can choose an integer $$$x$$$ and denote $$$b_i=a_i oplus x$$$ for all $$$1 le i le n$$$, where $$$oplus$$$ denotes the . Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 10^3$$$)xa0— the length of the array $$$a$$$. The second line of the test case contains $$$n$$$ integersxa0— array $$$a$$$ ($$$0 le a_i < 2^8$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. Output For each set test case, print the integer $$$x$$$ ($$$0 le x < 2^8$$$) if it exists, or $$$-1$$$ otherwise. Example Input 5 3 1 2 5 3 1 2 3 4 0 1 2 3 4 1 2 2 3 1 1 Note In the first test case, after applying the operation with the number $$$6$$$ the array $$$b$$$ becomes $$$[7, 4, 3]$$$, $$$7 oplus 4 oplus 3 = 0$$$. There are other answers in the third test case, such as the number $$$0$$$. | 800 | false | false | false | false | false | false | true | false | false | false | 1,449 |
1545C | Cirno has prepared $$$n$$$ arrays of length $$$n$$$ each. Each array is a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. These arrays are special: for all $$$1 leq i leq n$$$, if we take the $$$i$$$-th element of each array and form another array of length $$$n$$$ with these elements, the resultant array is also a permutation of $$$n$$$ integers from $$$1$$$ to $$$n$$$. In the other words, if you put these $$$n$$$ arrays under each other to form a matrix with $$$n$$$ rows and $$$n$$$ columns, this matrix is a $$$-th array, the $$$k$$$-th element of both arrays is the same. Notice that the arrays indexed from $$$n + 1$$$ to $$$2n$$$ don't have to form a Latin square. Also, Cirno made sure that for all $$$2n$$$ arrays, no two arrays are completely equal, i.xa0e. for all pair of indices $$$1 leq i < j leq 2n$$$, there exists at least one position $$$1 leq k leq n$$$, such that the $$$k$$$-th elements of the $$$i$$$-th and $$$j$$$-th array are different. Finally, Cirno arbitrarily changed the order of $$$2n$$$ arrays. AquaMoon calls a subset of all $$$2n$$$ arrays of size $$$n$$$ good if these arrays from a Latin square. AquaMoon wants to know how many good subsets exist. Because this number may be particularly large, find it modulo $$$998,244,353$$$. Also, she wants to find any good subset. Can you help her? Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$5 leq n leq 500$$$). Then $$$2n$$$ lines followed. The $$$i$$$-th of these lines contains $$$n$$$ integers, representing the $$$i$$$-th array. It is guaranteed, that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output For each test case print two lines. In the first line, print the number of good subsets by modulo $$$998,244,353$$$. In the second line, print $$$n$$$ indices from $$$1$$$ to $$$2n$$$ — indices of the $$$n$$$ arrays that form a good subset (you can print them in any order). If there are several possible answers — print any of them. Example Input 3 7 1 2 3 4 5 6 7 2 3 4 5 6 7 1 3 4 5 6 7 1 2 4 5 6 7 1 2 3 5 6 7 1 2 3 4 6 7 1 2 3 4 5 7 1 2 3 4 5 6 1 2 3 4 5 7 6 1 3 4 5 6 7 2 1 4 5 6 7 3 2 1 5 6 7 4 2 3 1 6 7 5 2 3 4 1 7 6 2 3 4 5 1 7 2 3 4 5 6 5 4 5 1 2 3 3 5 2 4 1 1 2 3 4 5 5 2 4 1 3 3 4 5 1 2 2 3 4 5 1 1 3 5 2 4 4 1 3 5 2 2 4 1 3 5 5 1 2 3 4 6 2 3 4 5 6 1 3 1 2 6 4 5 6 1 2 3 4 5 5 6 1 3 2 4 4 3 6 5 2 1 5 6 1 2 3 4 4 5 6 1 2 3 3 4 5 6 1 2 1 2 3 4 5 6 2 5 4 1 6 3 3 2 5 4 1 6 1 4 3 6 5 2 Output 1 1 2 3 4 5 6 7 2 1 3 5 6 10 4 1 3 6 7 8 9 Note In the first test case, the number of good subsets is $$$1$$$. The only such subset is the set of arrays with indices $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$6$$$, $$$7$$$. In the second test case, the number of good subsets is $$$2$$$. They are $$$1$$$, $$$3$$$, $$$5$$$, $$$6$$$, $$$10$$$ or $$$2$$$, $$$4$$$, $$$7$$$, $$$8$$$, $$$9$$$. | 2,800 | false | false | false | false | false | true | true | false | false | true | 2,920 |
784D | 1,900 | false | false | true | false | false | false | false | false | false | false | 6,637 |
|
848B | Wherever the destination is, whoever we meet, let's render this song together. On a Cartesian coordinate plane lies a rectangular stage of size _w_u2009×u2009_h_, represented by a rectangle with corners (0,u20090), (_w_,u20090), (_w_,u2009_h_) and (0,u2009_h_). It can be seen that no collisions will happen before one enters the stage. On the sides of the stage stand _n_ dancers. The _i_-th of them falls into one of the following groups: Vertical: stands at (_x__i_,u20090), moves in positive _y_ direction (upwards); Horizontal: stands at (0,u2009_y__i_), moves in positive _x_ direction (rightwards). According to choreography, the _i_-th dancer should stand still for the first _t__i_ milliseconds, and then start moving in the specified direction at 1 unit per millisecond, until another border is reached. It is guaranteed that no two dancers have the same group, position and waiting time at the same time. When two dancers collide (i.e. are on the same point at some time when both of them are moving), they immediately exchange their moving directions and go on. Dancers stop when a border of the stage is reached. Find out every dancer's stopping position. Input The first line of input contains three space-separated positive integers _n_, _w_ and _h_ (1u2009≤u2009_n_u2009≤u2009100u2009000, 2u2009≤u2009_w_,u2009_h_u2009≤u2009100u2009000) — the number of dancers and the width and height of the stage, respectively. The following _n_ lines each describes a dancer: the _i_-th among them contains three space-separated integers _g__i_, _p__i_, and _t__i_ (1u2009≤u2009_g__i_u2009≤u20092, 1u2009≤u2009_p__i_u2009≤u200999u2009999, 0u2009≤u2009_t__i_u2009≤u2009100u2009000), describing a dancer's group _g__i_ (_g__i_u2009=u20091 — vertical, _g__i_u2009=u20092 — horizontal), position, and waiting time. If _g__i_u2009=u20091 then _p__i_u2009=u2009_x__i_; otherwise _p__i_u2009=u2009_y__i_. It's guaranteed that 1u2009≤u2009_x__i_u2009≤u2009_w_u2009-u20091 and 1u2009≤u2009_y__i_u2009≤u2009_h_u2009-u20091. It is guaranteed that no two dancers have the same group, position and waiting time at the same time. Output Output _n_ lines, the _i_-th of which contains two space-separated integers (_x__i_,u2009_y__i_) — the stopping position of the _i_-th dancer in the input. Examples Input 8 10 8 1 1 10 1 4 13 1 7 1 1 8 2 2 2 0 2 5 14 2 6 0 2 6 1 Output 4 8 10 5 8 8 10 6 10 2 1 8 7 8 10 6 Input 3 2 3 1 1 2 2 1 1 1 1 5 Note The first example corresponds to the initial setup in the legend, and the tracks of dancers are marked with different colours in the following figure. In the second example, no dancers collide. | 1,900 | false | false | true | false | true | true | false | false | true | false | 6,357 |
1967B2 | Problem - 1967B2 - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math number theory *2200 No tag edit access → Contest materials ") time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved. You are given two positive integers $$$n$$$, $$$m$$$. Calculate the number of ordered pairs $$$(a, b)$$$ satisfying the following conditions: $$$1le ale n$$$, $$$1le ble m$$$; $$$b cdot gcd(a,b)$$$ is a multiple of $$$a+b$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1le tle 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1le n,mle 2 cdot 10^6$$$). It is guaranteed that neither the sum of $$$n$$$ nor the sum of $$$m$$$ over all test cases exceeds $$$2 cdot 10^6$$$. Output For each test case, print a single integer: the number of valid pairs. Example Input 6 1 1 2 3 3 5 10 8 100 1233 1000000 1145141 Output 0 1 1 6 423 5933961 Note In the first test case, no pair satisfies the conditions. In the fourth test case, $$$(2,2),(3,6),(4,4),(6,3),(6,6),(8,8)$$$ satisfy the conditions. | 2,200 | true | false | false | false | false | false | true | false | false | false | 506 |
558A | Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere. Lala Land has exactly _n_ apple trees. Tree number _i_ is located in a position _x__i_ and has _a__i_ apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in _x_u2009=u20090 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing. What is the maximum number of apples he can collect? Input The first line contains one number _n_ (1u2009≤u2009_n_u2009≤u2009100), the number of apple trees in Lala Land. The following _n_ lines contains two integers each _x__i_, _a__i_ (u2009-u2009105u2009≤u2009_x__i_u2009≤u2009105, _x__i_u2009≠u20090, 1u2009≤u2009_a__i_u2009≤u2009105), representing the position of the _i_-th tree and number of apples on it. It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0. Output Output the maximum number of apples Amr can collect. Note In the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples. In the second sample test the optimal solution is to go left to _x_u2009=u2009u2009-u20091, collect apples from there, then the direction will be reversed, Amr has to go to _x_u2009=u20091, collect apples from there, then the direction will be reversed and Amr goes to the final tree _x_u2009=u2009u2009-u20092. In the third sample test the optimal solution is to go right to _x_u2009=u20091, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left. | 1,100 | false | false | true | false | false | false | true | false | true | false | 7,603 |
433B | Kuriyama Mirai has killed many monsters and got many (namely _n_) stones. She numbers the stones from 1 to _n_. The cost of the _i_-th stone is _v__i_. Kuriyama Mirai wants to know something about these stones so she will ask you two kinds of questions: 1. She will tell you two numbers, _l_ and _r_xa0(1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_), and you should tell her . 2. Let _u__i_ be the cost of the _i_-th cheapest stone (the cost that will be on the _i_-th place if we arrange all the stone costs in non-decreasing order). This time she will tell you two numbers, _l_ and _r_xa0(1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_), and you should tell her . For every question you should give the correct answer, or Kuriyama Mirai will say "fuyukai desu" and then become unhappy. Input The first line contains an integer _n_xa0(1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ integers: _v_1,u2009_v_2,u2009...,u2009_v__n_xa0(1u2009≤u2009_v__i_u2009≤u2009109) — costs of the stones. The third line contains an integer _m_xa0(1u2009≤u2009_m_u2009≤u2009105) — the number of Kuriyama Mirai's questions. Then follow _m_ lines, each line contains three integers _type_, _l_ and _r_xa0(1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_;xa01u2009≤u2009_type_u2009≤u20092), describing a question. If _type_ equal to 1, then you should output the answer for the first question, else you should output the answer for the second one. Output Print _m_ lines. Each line must contain an integer — the answer to Kuriyama Mirai's question. Print the answers to the questions in the order of input. Examples Input 6 6 4 2 7 2 7 3 2 3 6 1 3 4 1 1 6 Input 4 5 5 2 3 10 1 2 4 2 1 4 1 1 1 2 1 4 2 1 2 1 1 1 1 3 3 1 1 3 1 4 4 1 2 2 Note Please note that the answers to the questions may overflow 32-bit integer type. | 1,200 | false | false | true | true | false | false | false | false | true | false | 8,108 |
1843C | Vanya really likes math. One day when he was solving another math problem, he came up with an interesting tree. This tree is built as follows. Initially, the tree has only one vertex with the number $$$1$$$xa0— the root of the tree. Then, Vanya adds two children to it, assigning them consecutive numbersxa0— $$$2$$$ and $$$3$$$, respectively. After that, he will add children to the vertices in increasing order of their numbers, starting from $$$2$$$, assigning their children the minimum unused indices. As a result, Vanya will have an infinite tree with the root in the vertex $$$1$$$, where each vertex will have exactly two children, and the vertex numbers will be arranged sequentially by layers. Part of Vanya's tree. Vanya wondered what the sum of the vertex numbers on the path from the vertex with number $$$1$$$ to the vertex with number $$$n$$$ in such a tree is equal to. Since Vanya doesn't like counting, he asked you to help him find this sum. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. This is followed by $$$t$$$ linesxa0— the description of the test cases. Each line contains one integer $$$n$$$ ($$$1 le n le 10^{16}$$$)xa0— the number of vertex for which Vanya wants to count the sum of vertex numbers on the path from the root to that vertex. Output For each test case, print one integerxa0— the desired sum. Example Input 6 3 10 37 1 10000000000000000 15 Output 4 18 71 1 19999999999999980 26 Note In the first test case of example on the path from the root to the vertex $$$3$$$ there are two vertices $$$1$$$ and $$$3$$$, their sum equals $$$4$$$. In the second test case of example on the path from the root to the vertex with number $$$10$$$ there are vertices $$$1$$$, $$$2$$$, $$$5$$$, $$$10$$$, sum of their numbers equals $$$1+2+5+10 = 18$$$. | 800 | true | false | false | false | false | false | false | false | false | false | 1,228 |
1195A | Old timers of Summer Informatics School can remember previous camps in which each student was given a drink of his choice on the vechorka (late-evening meal). Or may be the story was more complicated? There are $$$n$$$ students living in a building, and for each of them the favorite drink $$$a_i$$$ is known. So you know $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$, where $$$a_i$$$ ($$$1 le a_i le k$$$) is the type of the favorite drink of the $$$i$$$-th student. The drink types are numbered from $$$1$$$ to $$$k$$$. There are infinite number of drink sets. Each set consists of exactly two portions of the same drink. In other words, there are $$$k$$$ types of drink sets, the $$$j$$$-th type contains two portions of the drink $$$j$$$. The available number of sets of each of the $$$k$$$ types is infinite. You know that students will receive the minimum possible number of sets to give all students exactly one drink. Obviously, the number of sets will be exactly $$$lceil frac{n}{2} ceil$$$, where $$$lceil x ceil$$$ is $$$x$$$ rounded up. After students receive the sets, they will distribute their portions by their choice: each student will get exactly one portion. Note, that if $$$n$$$ is odd then one portion will remain unused and the students' teacher will drink it. What is the maximum number of students that can get their favorite drink if $$$lceil frac{n}{2} ceil$$$ sets will be chosen optimally and students will distribute portions between themselves optimally? Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 1,000$$$) — the number of students in the building and the number of different drinks. The next $$$n$$$ lines contain student's favorite drinks. The $$$i$$$-th line contains a single integer from $$$1$$$ to $$$k$$$ — the type of the favorite drink of the $$$i$$$-th student. Note In the first example, students could choose three sets with drinks $$$1$$$, $$$1$$$ and $$$2$$$ (so they will have two sets with two drinks of the type $$$1$$$ each and one set with two drinks of the type $$$2$$$, so portions will be $$$1, 1, 1, 1, 2, 2$$$). This way all students except the second one will get their favorite drinks. Another possible answer is sets with drinks $$$1$$$, $$$2$$$ and $$$3$$$. In this case the portions will be $$$1, 1, 2, 2, 3, 3$$$. Then all the students except one will gain their favorite drinks. The only student that will not gain the favorite drink will be a student with $$$a_i = 1$$$ (i.e. the first, the third or the fourth). | 1,000 | true | true | false | false | false | false | false | false | false | false | 4,729 |
718C | Sasha has an array of integers _a_1,u2009_a_2,u2009...,u2009_a__n_. You have to perform _m_ queries. There might be queries of two types: 1. 1 l r xxa0— increase all integers on the segment from _l_ to _r_ by values _x_; 2. 2 l rxa0— find , where _f_(_x_) is the _x_-th Fibonacci number. As this number may be large, you only have to find it modulo 109u2009+u20097. In this problem we define Fibonacci numbers as follows: _f_(1)u2009=u20091, _f_(2)u2009=u20091, _f_(_x_)u2009=u2009_f_(_x_u2009-u20091)u2009+u2009_f_(_x_u2009-u20092) for all _x_u2009>u20092. Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha? Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_m_u2009≤u2009100u2009000)xa0— the number of elements in the array and the number of queries respectively. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Then follow _m_ lines with queries descriptions. Each of them contains integers _tp__i_, _l__i_, _r__i_ and may be _x__i_ (1u2009≤u2009_tp__i_u2009≤u20092, 1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_, 1u2009≤u2009_x__i_u2009≤u2009109). Here _tp__i_u2009=u20091 corresponds to the queries of the first type and _tp__i_ corresponds to the queries of the second type. It's guaranteed that the input will contains at least one query of the second type. Output For each query of the second type print the answer modulo 109u2009+u20097. Examples Input 5 4 1 1 2 1 1 2 1 5 1 2 4 2 2 2 4 2 1 5 Note Initially, array _a_ is equal to 1, 1, 2, 1, 1. The answer for the first query of the second type is _f_(1)u2009+u2009_f_(1)u2009+u2009_f_(2)u2009+u2009_f_(1)u2009+u2009_f_(1)u2009=u20091u2009+u20091u2009+u20091u2009+u20091u2009+u20091u2009=u20095. After the query 1 2 4 2 array _a_ is equal to 1, 3, 4, 3, 1. The answer for the second query of the second type is _f_(3)u2009+u2009_f_(4)u2009+u2009_f_(3)u2009=u20092u2009+u20093u2009+u20092u2009=u20097. The answer for the third query of the second type is _f_(1)u2009+u2009_f_(3)u2009+u2009_f_(4)u2009+u2009_f_(3)u2009+u2009_f_(1)u2009=u20091u2009+u20092u2009+u20093u2009+u20092u2009+u20091u2009=u20099. | 2,300 | true | false | false | false | true | false | false | false | false | false | 6,942 |
713C | Problem - 713C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp sortings *2300 No tag edit access → Contest materials xa0— the length of the array. Next line contains _n_ integer _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109). Output Print the minimum number of operation required to make the array strictly increasing. Examples Input 7 2 1 5 11 5 9 11 Output 9 Input 5 5 4 3 2 1 Output 12 Note In the first sample, the array is going to look as follows: 2 3 5 6 7 9 11 2u2009-u20092u2009+u20091u2009-u20093u2009+u20095u2009-u20095u2009+u200911u2009-u20096u2009+u20095u2009-u20097u2009+u20099u2009-u20099u2009+u200911u2009-u200911u2009=u20099 And for the second sample: 1 2 3 4 5 5u2009-u20091u2009+u20094u2009-u20092u2009+u20093u2009-u20093u2009+u20092u2009-u20094u2009+u20091u2009-u20095u2009=u200912 | 2,300 | false | false | false | true | false | false | false | false | true | false | 6,964 |
58B | Problem - 58B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy *1300 No tag edit access → Contest materials ") - разбор A-D") which represents the denomination of the most expensive coin. Output Print the denominations of all the coins in the order of decreasing. The number of coins must be the largest possible (with the given denomination _n_ of the most expensive coin). Also, the denomination of every coin must be divisible by the denomination of any cheaper coin. Naturally, the denominations of all the coins should be different. If there are several solutins to that problem, print any of them. Examples Input 10 Output 10 5 1 Input 4 Output 4 2 1 Input 3 Output 3 1 | 1,300 | false | true | false | false | false | false | false | false | false | false | 9,684 |
288A | Problem - 288A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy *1300 No tag edit access → Contest materials , exactly _k_ of these letters are distinct. 2. No two neighbouring letters of a string coincide; that is, if we represent a string as _s_u2009=u2009_s_1_s_2... _s__n_, then the following inequality holds, _s__i_u2009≠u2009_s__i_u2009+u20091(1u2009≤u2009_i_u2009<u2009_n_). 3. Among all strings that meet points 1 and 2, the required string is lexicographically smallest. Help him find such string or state that such string doesn't exist. String _x_u2009=u2009_x_1_x_2... _x__p_ is lexicographically less than string _y_u2009=u2009_y_1_y_2... _y__q_, if either _p_u2009<u2009_q_ and _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__p_u2009=u2009_y__p_, or there is such number _r_ (_r_u2009<u2009_p_,u2009_r_u2009<u2009_q_), that _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__r_u2009=u2009_y__r_ and _x__r_u2009+u20091u2009<u2009_y__r_u2009+u20091. The characters of the strings are compared by their ASCII codes. Input A single line contains two positive integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009106,u20091u2009≤u2009_k_u2009≤u200926) — the string's length and the number of distinct letters. Output In a single line print the required string. If there isn't such string, print "-1" (without the quotes). Examples Input 7 4 Output ababacd Input 4 7 Output -1 | 1,300 | false | true | false | false | false | false | false | false | false | false | 8,684 |
662B | You are given an undirected graph that consists of _n_ vertices and _m_ edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue edges with an endpoint in this vertex change the color to red. Find the minimum possible number of moves required to make the colors of all edges equal. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100u2009000)xa0— the number of vertices and edges, respectively. The following _m_ lines provide the description of the edges, as the _i_-th of them contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_)xa0— the indices of the vertices connected by the _i_-th edge, and a character _c__i_ () providing the initial color of this edge. If _c__i_ equals 'R', then this edge is initially colored red. Otherwise, _c__i_ is equal to 'B' and this edge is initially colored blue. It's guaranteed that there are no self-loops and multiple edges. Output If there is no way to make the colors of all edges equal output u2009-u20091 in the only line of the output. Otherwise first output _k_xa0— the minimum number of moves required to achieve the goal, then output _k_ integers _a_1,u2009_a_2,u2009...,u2009_a__k_, where _a__i_ is equal to the index of the vertex that should be used at the _i_-th move. If there are multiple optimal sequences of moves, output any of them. Examples Input 6 5 1 3 R 2 3 R 3 4 B 4 5 R 4 6 R Input 4 5 1 2 R 1 3 R 2 3 B 3 4 B 1 4 B | 2,200 | false | false | false | false | false | false | false | false | false | true | 7,167 |
1927F | Given an undirected weighted graph with $$$n$$$ vertices and $$$m$$$ edges. There is at most one edge between each pair of vertices in the graph, and the graph does not contain loops (edges from a vertex to itself). The graph is not necessarily connected. A cycle in the graph is called simple if it doesn't pass through the same vertex twice and doesn't contain the same edge twice. Find any simple cycle in this graph in which the weight of the lightest edge is minimal. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n le m le min(frac{ncdot(n - 1)}{2}, 2 cdot 10^5)$$$)xa0— the size of the graph and the number of edges. The next $$$m$$$ lines of the test case contain three integers $$$u$$$, $$$v$$$, and $$$w$$$ ($$$1 le u, v le n$$$, $$$u e v$$$, $$$1 le w le 10^6$$$)xa0— vertices $$$u$$$ and $$$v$$$ are connected by an edge of weight $$$w$$$. It is guaranteed that there is at most one edge between each pair of vertices. Note that under the given constraints, there is always at least one simple cycle in the graph. It is guaranteed that the sum of the values of $$$m$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a pair of numbers $$$b$$$ and $$$k$$$, where: $$$b$$$xa0— the minimum weight of the edge in the found cycle, $$$k$$$xa0— the number of vertices in the found cycle. On the next line, output $$$k$$$ numbers from $$$1$$$ to $$$n$$$ xa0— the vertices of the cycle in traversal order. Note that the answer always exists, as under the given constraints, there is always at least one simple cycle in the graph. Example Input 5 6 6 1 2 1 2 3 1 3 1 1 4 5 1 5 6 1 6 4 1 6 6 1 2 10 2 3 8 3 1 5 4 5 100 5 6 40 6 4 3 6 15 1 2 4 5 2 8 6 1 7 6 3 10 6 5 1 3 2 8 4 3 4 5 3 6 2 6 6 5 4 5 4 1 3 6 4 5 4 2 1 3 1 7 1 5 5 4 6 2 3 2 1 3 10 1 4 1 3 4 7 2 4 5 1 2 2 4 5 2 1 10 3 1 3 4 2 6 1 4 7 2 3 3 Output 1 3 1 2 3 3 3 6 4 5 1 5 4 2 1 6 3 1 4 1 4 3 2 3 3 2 3 1 | 1,900 | false | true | true | false | true | false | false | false | true | true | 725 |
1146D | Problem - 1146D - Codeforces =============== xa0 ]( "Forethought Future Cup") $$$ be the number of distinct integers the frog can reach if it never jumps on an integer outside the interval $$$ | 2,100 | true | false | false | false | false | false | false | false | false | false | 4,988 |
76D | Problem - 76D - Codeforces =============== xa0 ") is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different. For example, if _X_u2009=u200910910u2009=u200911011012, _Y_u2009=u20094110u2009=u20091010012, then: _X_ xor _Y_xa0u2009=u2009xa06810xa0u2009=u2009xa010001002. Write a program, which takes two non-negative integers _A_ and _B_ as an input and finds two non-negative integers _X_ and _Y_, which satisfy the following conditions: _A_u2009=u2009_X_u2009+u2009_Y_ _B_xa0u2009=u2009xa0_X_ xor _Y_, where xor is bitwise exclusive or. _X_ is the smallest number among all numbers for which the first two conditions are true. Input The first line contains integer number _A_ and the second line contains integer number _B_ (0u2009≤u2009_A_,u2009_B_u2009≤u2009264u2009-u20091). Output The only output line should contain two integer non-negative numbers _X_ and _Y_. Print the only number -1 if there is no answer. Examples Input 142 76 Output 33 109 | 1,700 | true | true | false | true | false | false | false | false | false | false | 9,583 |
1931A | Nikita had a word consisting of exactly $$$3$$$ lowercase Latin letters. The letters in the Latin alphabet are numbered from $$$1$$$ to $$$26$$$, where the letter "a" has the index $$$1$$$, and the letter "z" has the index $$$26$$$. He encoded this word as the sum of the positions of all the characters in the alphabet. For example, the word "cat" he would encode as the integer $$$3 + 1 + 20 = 24$$$, because the letter "c" has the index $$$3$$$ in the alphabet, the letter "a" has the index $$$1$$$, and the letter "t" has the index $$$20$$$. However, this encoding turned out to be ambiguous! For example, when encoding the word "ava", the integer $$$1 + 22 + 1 = 24$$$ is also obtained. Determine the lexicographically smallest word of $$$3$$$ letters that could have been encoded. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases in the test. This is followed by the descriptions of the test cases. The first and only line of each test case contains an integer $$$n$$$ ($$$3 le n le 78$$$) — the encoded word. Output For each test case, output the lexicographically smallest three-letter word that could have been encoded on a separate line. Example Output aav rzz aaa czz auz | 800 | false | false | false | false | false | false | true | false | false | false | 701 |
2010B | Problem - 2010B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation math *800 No tag edit access xa0— the numbers of the brothers who arrived on time. The numbers are given in arbitrary order. Output Output a single integerxa0— the number of the brother who was late to the meeting. Example Input 3 1 Output 2 | 800 | true | false | true | false | false | false | true | false | false | false | 190 |
20C | Problem - 20C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags graphs shortest paths *1900 No tag edit access , where _n_ is the number of vertices and _m_ is the number of edges. Following _m_ lines contain one edge each in form _a__i_, _b__i_ and _w__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u20091u2009≤u2009_w__i_u2009≤u2009106), where _a__i_,u2009_b__i_ are edge endpoints and _w__i_ is the length of the edge. It is possible that the graph has loops and multiple edges between pair of vertices. Output Write the only integer -1 in case of no path. Write the shortest path in opposite case. If there are many solutions, print any of them. Examples Input 5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1 Output 1 4 3 5 Input 5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1 Output 1 4 3 5 | 1,900 | false | false | false | false | false | false | false | false | false | true | 9,893 |
1633C | Monocarp is playing a computer game. In this game, his character fights different monsters. A fight between a character and a monster goes as follows. Suppose the character initially has health $$$h_C$$$ and attack $$$d_C$$$; the monster initially has health $$$h_M$$$ and attack $$$d_M$$$. The fight consists of several steps: 1. the character attacks the monster, decreasing the monster's health by $$$d_C$$$; 2. the monster attacks the character, decreasing the character's health by $$$d_M$$$; 3. the character attacks the monster, decreasing the monster's health by $$$d_C$$$; 4. the monster attacks the character, decreasing the character's health by $$$d_M$$$; 5. and so on, until the end of the fight. The fight ends when someone's health becomes non-positive (i.u2009e. $$$0$$$ or less). If the monster's health becomes non-positive, the character wins, otherwise the monster wins. Monocarp's character currently has health equal to $$$h_C$$$ and attack equal to $$$d_C$$$. He wants to slay a monster with health equal to $$$h_M$$$ and attack equal to $$$d_M$$$. Before the fight, Monocarp can spend up to $$$k$$$ coins to upgrade his character's weapon and/or armor; each upgrade costs exactly one coin, each weapon upgrade increases the character's attack by $$$w$$$, and each armor upgrade increases the character's health by $$$a$$$. Can Monocarp's character slay the monster if Monocarp spends coins on upgrades optimally? Input The first line contains one integer $$$t$$$ ($$$1 le t le 5 cdot 10^4$$$) — the number of test cases. Each test case consists of three lines: The first line contains two integers $$$h_C$$$ and $$$d_C$$$ ($$$1 le h_C le 10^{15}$$$; $$$1 le d_C le 10^9$$$) — the character's health and attack; The second line contains two integers $$$h_M$$$ and $$$d_M$$$ ($$$1 le h_M le 10^{15}$$$; $$$1 le d_M le 10^9$$$) — the monster's health and attack; The third line contains three integers $$$k$$$, $$$w$$$ and $$$a$$$ ($$$0 le k le 2 cdot 10^5$$$; $$$0 le w le 10^4$$$; $$$0 le a le 10^{10}$$$) — the maximum number of coins that Monocarp can spend, the amount added to the character's attack with each weapon upgrade, and the amount added to the character's health with each armor upgrade, respectively. The sum of $$$k$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print YES if it is possible to slay the monster by optimally choosing the upgrades. Otherwise, print NO. Example Input 4 25 4 9 20 1 1 10 25 4 12 20 1 1 10 100 1 45 2 0 4 10 9 2 69 2 4 2 7 Note In the first example, Monocarp can spend one coin to upgrade weapon (damage will be equal to $$$5$$$), then health during battle will change as follows: $$$(h_C, h_M) = (25, 9) ightarrow (25, 4) ightarrow (5, 4) ightarrow (5, -1)$$$. The battle ended with Monocarp's victory. In the second example, Monocarp has no way to defeat the monster. In the third example, Monocarp has no coins, so he can't buy upgrades. However, the initial characteristics are enough for Monocarp to win. In the fourth example, Monocarp has $$$4$$$ coins. To defeat the monster, he has to spend $$$2$$$ coins to upgrade weapon and $$$2$$$ coins to upgrade armor. | 1,100 | true | false | false | false | false | false | true | false | false | false | 2,450 |
1909D | There are $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ on a blackboard. You are also given a positive integer $$$k$$$. You can perform the following operation some (possibly $$$0$$$) times: choose a number $$$x$$$ on the blackboard; erase one occurrence of $$$x$$$; write two positive integers $$$y$$$, $$$z$$$ such that $$$y+z = x+k$$$ on the blackboard. Is it possible to make all the numbers on the blackboard equal? If yes, what is the minimum number of operations you need? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 leq k leq 10^{12}$$$)xa0— the number of integers initially on the blackboard and the constant $$$k$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{12}$$$)xa0— the initial state of the blackboard. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single line containing an integer: the minimum number of operations you need to make all the numbers on the blackboard equal, or $$$-1$$$ if it is impossible. Example Input 9 2 1 3 4 2 3 7 11 3 10 100 40 100 2 1 1 2 2 2 1 2 1 327869541 327869541 5 26250314066 439986238782 581370817372 409476934981 287439719777 737637983182 5 616753575719 321037808624 222034505841 214063039282 441536506916 464097941819 5 431813672576 393004301966 405902283416 900951084746 672201172466 518769038906 Output 3 1 4 -1 -1 0 3119 28999960732 -1 Note In the first test case, $$$k = 1$$$. You can make all the numbers on the blackboard equal to $$$2$$$ with the following operations: Erase $$$x = 4$$$ and write $$$(y, z) = (2, 3)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains the multiset $$${3, 2, 3}$$$. Erase $$$x = 3$$$ and write $$$(y, z) = (2, 2)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains $$${2, 2, 2, 3}$$$. Erase $$$x = 3$$$ and write $$$(y, z) = (2, 2)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains $$${2, 2, 2, 2, 2}$$$. This makes all the numbers equal in $$$3$$$ operations. It can be shown that you cannot make all the numbers equal in less than $$$3$$$ operations. In the second test case, $$$k = 3$$$. You can make all the numbers on the blackboard equal to $$$7$$$ with the following operation: Erase $$$x = 11$$$ and write $$$(y, z) = (7, 7)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains $$${7, 7, 7}$$$. In the third test case, $$$k = 10$$$. You can make all the numbers on the blackboard equal to $$$40$$$ with the following operations: Erase $$$x = 100$$$ and write $$$(y, z) = (70, 40)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains $$${70, 40, 40, 100}$$$. Erase $$$x = 70$$$ and write $$$(y, z) = (40, 40)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains $$${40, 40, 40, 40, 100}$$$. Erase $$$x = 100$$$ and write $$$(y, z) = (40, 70)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains $$${40, 40, 40, 40, 40, 70}$$$. Erase $$$x = 70$$$ and write $$$(y, z) = (40, 40)$$$. Note that $$$y+z=x+k$$$. The blackboard now contains $$${40, 40, 40, 40, 40, 40, 40}$$$. In the fourth and in the fifth test case, you can show that it is impossible to make all the numbers on the blackboard equal. | 1,900 | true | true | false | false | false | false | false | false | false | false | 854 |
1010A | Natasha is going to fly on a rocket to Mars and return to Earth. Also, on the way to Mars, she will land on $$$n - 2$$$ intermediate planets. Formally: we number all the planets from $$$1$$$ to $$$n$$$. $$$1$$$ is Earth, $$$n$$$ is Mars. Natasha will make exactly $$$n$$$ flights: $$$1 o 2 o ldots n o 1$$$. Flight from $$$x$$$ to $$$y$$$ consists of two phases: take-off from planet $$$x$$$ and landing to planet $$$y$$$. This way, the overall itinerary of the trip will be: the $$$1$$$-st planet $$$ o$$$ take-off from the $$$1$$$-st planet $$$ o$$$ landing to the $$$2$$$-nd planet $$$ o$$$ $$$2$$$-nd planet $$$ o$$$ take-off from the $$$2$$$-nd planet $$$ o$$$ $$$ldots$$$ $$$ o$$$ landing to the $$$n$$$-th planet $$$ o$$$ the $$$n$$$-th planet $$$ o$$$ take-off from the $$$n$$$-th planet $$$ o$$$ landing to the $$$1$$$-st planet $$$ o$$$ the $$$1$$$-st planet. The mass of the rocket together with all the useful cargo (but without fuel) is $$$m$$$ tons. However, Natasha does not know how much fuel to load into the rocket. Unfortunately, fuel can only be loaded on Earth, so if the rocket runs out of fuel on some other planet, Natasha will not be able to return home. Fuel is needed to take-off from each planet and to land to each planet. It is known that $$$1$$$ ton of fuel can lift off $$$a_i$$$ tons of rocket from the $$$i$$$-th planet or to land $$$b_i$$$ tons of rocket onto the $$$i$$$-th planet. For example, if the weight of rocket is $$$9$$$ tons, weight of fuel is $$$3$$$ tons and take-off coefficient is $$$8$$$ ($$$a_i = 8$$$), then $$$1.5$$$ tons of fuel will be burnt (since $$$1.5 cdot 8 = 9 + 3$$$). The new weight of fuel after take-off will be $$$1.5$$$ tons. Please note, that it is allowed to burn non-integral amount of fuel during take-off or landing, and the amount of initial fuel can be non-integral as well. Help Natasha to calculate the minimum mass of fuel to load into the rocket. Note, that the rocket must spend fuel to carry both useful cargo and the fuel itself. However, it doesn't need to carry the fuel which has already been burnt. Assume, that the rocket takes off and lands instantly. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 1000$$$)xa0— number of planets. The second line contains the only integer $$$m$$$ ($$$1 le m le 1000$$$)xa0— weight of the payload. The third line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 1000$$$), where $$$a_i$$$ is the number of tons, which can be lifted off by one ton of fuel. The fourth line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 1000$$$), where $$$b_i$$$ is the number of tons, which can be landed by one ton of fuel. It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel. Output If Natasha can fly to Mars through $$$(n - 2)$$$ planets and return to Earth, print the minimum mass of fuel (in tons) that Natasha should take. Otherwise, print a single number $$$-1$$$. It is guaranteed, that if Natasha can make a flight, then it takes no more than $$$10^9$$$ tons of fuel. The answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$. Formally, let your answer be $$$p$$$, and the jury's answer be $$$q$$$. Your answer is considered correct if $$$frac{p - q}{max{(1, q)}} le 10^{-6}$$$. Examples Input 6 2 4 6 3 3 5 6 2 6 3 6 5 3 Note Let's consider the first example. Initially, the mass of a rocket with fuel is $$$22$$$ tons. At take-off from Earth one ton of fuel can lift off $$$11$$$ tons of cargo, so to lift off $$$22$$$ tons you need to burn $$$2$$$ tons of fuel. Remaining weight of the rocket with fuel is $$$20$$$ tons. During landing on Mars, one ton of fuel can land $$$5$$$ tons of cargo, so for landing $$$20$$$ tons you will need to burn $$$4$$$ tons of fuel. There will be $$$16$$$ tons of the rocket with fuel remaining. While taking off from Mars, one ton of fuel can raise $$$8$$$ tons of cargo, so to lift off $$$16$$$ tons you will need to burn $$$2$$$ tons of fuel. There will be $$$14$$$ tons of rocket with fuel after that. During landing on Earth, one ton of fuel can land $$$7$$$ tons of cargo, so for landing $$$14$$$ tons you will need to burn $$$2$$$ tons of fuel. Remaining weight is $$$12$$$ tons, that is, a rocket without any fuel. In the second case, the rocket will not be able even to take off from Earth. | 1,500 | true | false | false | false | false | false | false | true | false | false | 5,635 |
1187D | You are given an array $$$a_1, a_2, dots, a_n$$$ and an array $$$b_1, b_2, dots, b_n$$$. For one operation you can sort in non-decreasing order any subarray $$$a[l dots r]$$$ of the array $$$a$$$. For example, if $$$a = [4, 2, 2, 1, 3, 1]$$$ and you choose subbarray $$$a[2 dots 5]$$$, then the array turns into $$$[4, 1, 2, 2, 3, 1]$$$. You are asked to determine whether it is possible to obtain the array $$$b$$$ by applying this operation any number of times (possibly zero) to the array $$$a$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 3 cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$). The second line of each query contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). The third line of each query contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le n$$$). It is guaranteed that $$$sum n le 3 cdot 10^5$$$ over all queries in a test. Output For each query print YES (in any letter case) if it is possible to obtain an array $$$b$$$ and NO (in any letter case) otherwise. Example Input 4 7 1 7 1 4 4 5 6 1 1 4 4 5 7 6 5 1 1 3 3 5 1 1 3 3 5 2 1 1 1 2 3 1 2 3 3 2 1 Note In first test case the can sort subarray $$$a_1 dots a_5$$$, then $$$a$$$ will turn into $$$[1, 1, 4, 4, 7, 5, 6]$$$, and then sort subarray $$$a_5 dots a_6$$$. | 2,400 | false | false | false | false | true | false | false | false | true | false | 4,763 |
1492C | Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $$$s$$$ of length $$$n$$$ and $$$t$$$ of length $$$m$$$. A sequence $$$p_1, p_2, ldots, p_m$$$, where $$$1 leq p_1 < p_2 < ldots < p_m leq n$$$, is called beautiful, if $$$s_{p_i} = t_i$$$ for all $$$i$$$ from $$$1$$$ to $$$m$$$. The width of a sequence is defined as $$$maxlimits_{1 le i < m} left(p_{i + 1} - p_i ight)$$$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $$$s$$$ and $$$t$$$ there is at least one beautiful sequence. Input The first input line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq m leq n leq 2 cdot 10^5$$$)xa0— the lengths of the strings $$$s$$$ and $$$t$$$. The following line contains a single string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $$$t$$$ of length $$$m$$$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. Output Output one integerxa0— the maximum width of a beautiful sequence. Note In the first example there are two beautiful sequences of width $$$3$$$: they are $$${1, 2, 5}$$$ and $$${1, 4, 5}$$$. In the second example the beautiful sequence with the maximum width is $$${1, 5}$$$. In the third example there is exactly one beautiful sequencexa0— it is $$${1, 2, 3, 4, 5}$$$. In the fourth example there is exactly one beautiful sequencexa0— it is $$${1, 2}$$$. | 1,500 | false | true | false | true | true | false | false | true | false | false | 3,211 |
2029C | Hello, Codeforces Forcescode! Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode. Kevin has participated in $$$n$$$ contests on Forcescode. In the $$$i$$$-th contest, his performance rating is $$$a_i$$$. Now he has hacked into the backend of Forcescode and will select an interval $$$[l,r]$$$ ($$$1le lle rle n$$$), then skip all of the contests in this interval. After that, his rating will be recalculated in the following way: Initially, his rating is $$$x=0$$$; For each $$$1le ile n$$$, after the $$$i$$$-th contest, If $$$lle ile r$$$, this contest will be skipped, and the rating will remain unchanged; Otherwise, his rating will be updated according to the following rules: If $$$a_i>x$$$, his rating $$$x$$$ will increase by $$$1$$$; If $$$a_i=x$$$, his rating $$$x$$$ will remain unchanged; If $$$a_i<x$$$, his rating $$$x$$$ will decrease by $$$1$$$. You have to help Kevin to find his maximum possible rating after the recalculation if he chooses the interval $$$[l,r]$$$ optimally. Note that Kevin has to skip at least one contest. Input Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1le tle 5cdot 10^4$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 3cdot 10^5$$$)xa0— the number of contests. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ile n$$$)xa0— the performance ratings in the contests. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integerxa0— the maximum possible rating after the recalculation if Kevin chooses the interval optimally. Example Input 5 6 1 2 3 4 5 6 7 1 2 1 1 1 3 4 1 1 9 9 9 8 2 4 4 3 5 3 10 1 2 3 4 1 3 2 1 1 10 Note In the first test case, Kevin must skip at least one contest. If he chooses any interval of length $$$1$$$, his rating after the recalculation will be equal to $$$5$$$. In the second test case, Kevin's optimal choice is to select the interval $$$[3,5]$$$. During the recalculation, his rating changes as follows: $$$$$$ 0 xrightarrow{a_1=1} 1 xrightarrow{a_2=2} 2 xrightarrow{mathtt{skip}} 2 xrightarrow{mathtt{skip}} 2 xrightarrow{mathtt{skip}} 2 xrightarrow{a_6=3} 3 xrightarrow{a_7=4} 4 $$$$$$ In the third test case, Kevin must skip the only contest, so his rating will remain at the initial value of $$$0$$$. In the fourth test case, Kevin's optimal choice is to select the interval $$$[7,9]$$$. During the recalculation, his rating changes as follows: $$$$$$ 0 xrightarrow{a_1=9} 1 xrightarrow{a_2=9} 2 xrightarrow{a_3=8} 3 xrightarrow{a_4=2} 2 xrightarrow{a_5=4} 3 xrightarrow{a_6=4} 4 xrightarrow{mathtt{skip}} 4 xrightarrow{mathtt{skip}} 4 xrightarrow{mathtt{skip}} 4 $$$$$$ In the fifth test case, Kevin's optimal choice is to select the interval $$$[5,9]$$$. | 1,700 | false | true | false | true | true | false | false | true | false | false | 95 |
513B1 | Problem - 513B1 - Codeforces =============== xa0 ]( "16140") as the following sum: Find the lexicographically _m_-th permutation of length _n_ in the set of permutations having the maximum possible value of _f_(_p_). Input The single line of input contains two integers _n_ and _m_ (1u2009≤u2009_m_u2009≤u2009_cnt__n_), where _cnt__n_ is the number of permutations of length _n_ with maximum possible value of _f_(_p_). The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem B1 (3 points), the constraint 1u2009≤u2009_n_u2009≤u20098 will hold. In subproblem B2 (4 points), the constraint 1u2009≤u2009_n_u2009≤u200950 will hold. Output Output _n_ number forming the required permutation. Examples Input 2 2 Output 2 1 Input 3 2 Output 1 3 2 Note In the first example, both permutations of numbers {1, 2} yield maximum possible _f_(_p_) which is equal to 4. Among them, (2,u20091) comes second in lexicographical order. | 1,400 | false | false | false | false | false | false | true | false | false | false | 7,792 |
1133F2 | You are given an undirected unweighted connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. It is guaranteed that there are no self-loops or multiple edges in the given graph. Your task is to find any spanning tree of this graph such that the degree of the first vertex (vertex with label $$$1$$$ on it) is equal to $$$D$$$ (or say that there are no such spanning trees). Recall that the degree of a vertex is the number of edges incident to it. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$D$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$n - 1 le m le min(2 cdot 10^5, frac{n(n-1)}{2}), 1 le D < n$$$) — the number of vertices, the number of edges and required degree of the first vertex, respectively. The following $$$m$$$ lines denote edges: edge $$$i$$$ is represented by a pair of integers $$$v_i$$$, $$$u_i$$$ ($$$1 le v_i, u_i le n$$$, $$$u_i e v_i$$$), which are the indices of vertices connected by the edge. There are no loops or multiple edges in the given graph, i.u2009e. for each pair ($$$v_i, u_i$$$) there are no other pairs ($$$v_i, u_i$$$) or ($$$u_i, v_i$$$) in the list of edges, and for each pair $$$(v_i, u_i)$$$ the condition $$$v_i e u_i$$$ is satisfied. Output If there is no spanning tree satisfying the condition from the problem statement, print "NO" in the first line. Otherwise print "YES" in the first line and then print $$$n-1$$$ lines describing the edges of a spanning tree such that the degree of the first vertex (vertex with label $$$1$$$ on it) is equal to $$$D$$$. Make sure that the edges of the printed spanning tree form some subset of the input edges (order doesn't matter and edge $$$(v, u)$$$ is considered the same as the edge $$$(u, v)$$$). If there are multiple possible answers, print any of them. Examples Input 4 5 1 1 2 1 3 1 4 2 3 3 4 Input 4 5 3 1 2 1 3 1 4 2 3 3 4 Input 4 4 3 1 2 1 4 2 3 3 4 Note The picture corresponding to the first and second examples: The picture corresponding to the third example: | 1,900 | false | true | false | false | false | true | false | false | false | true | 5,048 |
472A | Problem - 472A - Codeforces =============== xa0 . Output Output two composite integers _x_ and _y_ (1u2009<u2009_x_,u2009_y_u2009<u2009_n_) such that _x_u2009+u2009_y_u2009=u2009_n_. If there are multiple solutions, you can output any of them. Examples Input 12 Output 4 8 Input 15 Output 6 9 Input 23 Output 8 15 Input 1000000 Output 500000 500000 Note In the first example, 12 = 4 + 8 and both 4, 8 are composite numbers. You can output "6 6" or "8 4" as well. In the second example, 15 = 6 + 9. Note that you can't output "1 14" because 1 is not a composite number. | 800 | true | false | false | false | false | false | false | false | false | false | 7,947 |
1700F | Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with $$$2$$$ rows and $$$n$$$ columns, every element of which is either $$$0$$$ or $$$1$$$. In one move you can swap two values in neighboring cells. More formally, let's number rows $$$1$$$ to $$$2$$$ from top to bottom, and columns $$$1$$$ to $$$n$$$ from left to right. Also, let's denote a cell in row $$$x$$$ and column $$$y$$$ as $$$(x, y)$$$. We consider cells $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ neighboring if $$$x_1 - x_2 + y_1 - y_2 = 1$$$. Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement. Input The first line contains an integer $$$n$$$ ($$$1 leq n leq 200,000$$$) — the number of columns in the puzzle. Following two lines describe the current arrangement on the puzzle. Each line contains $$$n$$$ integers, every one of which is either $$$0$$$ or $$$1$$$. The last two lines describe Alice's desired arrangement in the same format. Output If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print $$$-1$$$. Examples Input 5 0 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 Input 3 1 0 0 0 0 0 0 0 0 0 0 0 Note In the first example the following sequence of swaps will suffice: $$$(2, 1), (1, 1)$$$, $$$(1, 2), (1, 3)$$$, $$$(2, 2), (2, 3)$$$, $$$(1, 4), (1, 5)$$$, $$$(2, 5), (2, 4)$$$. It can be shown that $$$5$$$ is the minimal possible answer in this case. In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is $$$-1$$$. | 2,600 | false | true | false | true | false | true | false | false | false | false | 2,077 |
1951I | You are given an undirected connected simple graph with $$$n$$$ nodes and $$$m$$$ edges, where edge $$$i$$$ connects node $$$u_i$$$ and $$$v_i$$$, with two positive parameters $$$a_i$$$ and $$$b_i$$$ attached to it. Additionally, you are also given an integer $$$k$$$. A non-negative array $$$x$$$ with size $$$m$$$ is called a $$$k$$$-spanning-tree generator if it satisfies the following: Consider the undirected multigraph with $$$n$$$ nodes where edge $$$i$$$ is cloned $$$x_i$$$ times (i.e. there are $$$x_i$$$ edges connecting $$$u_i$$$ and $$$v_i$$$). It is possible to partition the edges of this graph into $$$k$$$ spanning trees, where each edge belongs to exactly one spanning tree$$$^dagger$$$. The cost of such array $$$x$$$ is defined as $$$sum_{i = 1}^m a_i x_i^2 + b_i x_i$$$. Find the minimum cost of a $$$k$$$-spanning-tree generator. $$$^dagger$$$ A spanning tree of a (multi)graph is a subset of the graph's edges that form a tree connecting all vertices of the graph. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 le n le 50, n - 1 le m le min(50, frac{n(n - 1)}{2}), 1 le k le 10^7$$$)xa0— the number of nodes in the graph, the number of edges in the graph, and the parameter for the $$$k$$$-spanning-tree generator. Each of the next $$$m$$$ lines of each test case contains four integers $$$u_i$$$, $$$v_i$$$, $$$a_i$$$, and $$$b_i$$$ ($$$1 le u_i, v_i le n, u_i eq v_i, 1 le a_i, b_i le 1000$$$)xa0— the endpoints of the edge $$$i$$$ and its two parameters. It is guaranteed that the graph is simple and connected. It is guaranteed that the sum of $$$n^2$$$ and the sum of $$$m^2$$$ over all test cases does not exceed $$$2500$$$. Output For each test case, output a single integer: the minimum cost of a $$$k$$$-spanning-tree generator. Example Input 4 5 5 1 4 3 5 5 2 1 5 7 2 4 6 2 5 3 3 5 2 5 2 9 5 5 3 4 3 5 5 2 1 5 7 2 4 6 2 5 3 3 5 2 5 2 9 2 1 10000000 1 2 1000 1000 10 15 10 7 1 7 6 5 8 6 6 4 8 2 2 4 3 10 9 10 8 3 4 4 6 6 1 5 4 1 3 9 3 4 3 8 3 9 9 7 5 10 3 2 1 3 4 6 1 6 4 2 5 7 3 10 7 2 1 8 2 6 8 Output 38 191 100000010000000000 2722 Note In the first test case, a valid $$$1$$$-spanning-tree generator is $$$x = [1, 1, 1, 1, 0]$$$, as indicated by the following figure. The cost of this generator is $$$(1^2 cdot 5 + 1 cdot 5) + (1^2 cdot 5 + 1 cdot 7) + (1^2 cdot 6 + 1 cdot 2) + (1^2 cdot 3 + 1 cdot 5) + (0^2 cdot 4 + 0 cdot 9) = 38$$$. It can be proven that no other generator has a lower cost. The $$$1$$$-spanning-tree partition of $$$x = [1, 1, 1, 1, 0]$$$ In the second test case, a valid $$$3$$$-spanning-tree generator is $$$x = [2, 3, 2, 2, 3]$$$, as indicated by the following figure. The cost of this generator is $$$(2^2 cdot 5 + 2 cdot 5) + (3^2 cdot 5 + 3 cdot 7) + (2^2 cdot 6 + 2 cdot 2) + (2^2 cdot 3 + 2 cdot 5) + (3^2 cdot 4 + 3 cdot 9) = 191$$$. It can be proven that no other generator has a lower cost. The $$$3$$$-spanning-tree partition of $$$x = [2, 3, 2, 2, 3]$$$ | 3,200 | false | true | false | false | false | true | false | true | false | true | 566 |
1867E2 | This is the hard version of the problem. The only difference between the versions is the limit on the number of queries. In this version, you can make no more than 57 queries. You can make hacks only if both versions of the problem are solved. This is an interactive problem! salyg1n has given you a positive integer $$$k$$$ and wants to play a game with you. He has chosen an array of $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). You must print $$$a_1 oplus a_2 oplus ldots oplus a_n$$$, where $$$oplus$$$ denotes the – the length of the chosen array and the length of the query subarray, respectively. To find the value of $$$a_i oplus a_{i + 1} oplus ldots oplus a_{i + k - 1}$$$, print the query in the format $$$?$$$ $$$i$$$ ($$$1 leq i leq n - k + 1$$$). Then read a single integer – the answer to your query. You can make no more than $$$57$$$ queries. When you are ready to print the answer, output it in the format $$$!$$$ $$$x$$$. After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as one of the $$$57$$$ queries. If your program makes more than $$$57$$$ queries for one set of input data, or makes an invalid query, then the response to the query will be -1. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict. After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10000$$$. The interactor in this problem is not adaptive. Hacks: To perform a hack, use the following format: The first line contains a single integer $$$t$$$ – the number of test cases. The description of each test case should consist of two lines. The first line contains the numbers $$$n$$$ and $$$k$$$ – the length of the chosen array and the length of the query subarray, respectively. The second line contains $$$n$$$ numbers $$$a_1, a_2, ldots, a_n$$$ – the array that the jury should choose for this test case. Note In the first test case, the jury has chosen the array $$$a$$$ $$$=$$$ $$$[4, 2, 5, 1]$$$ In the second test case, the jury has chosen the array $$$a$$$ $$$=$$$ $$$[5, 7, 1, 3, 3, 7]$$$ | 2,200 | false | false | false | false | false | true | false | false | false | false | 1,070 |
1256A | You have $$$a$$$ coins of value $$$n$$$ and $$$b$$$ coins of value $$$1$$$. You always pay in exact change, so you want to know if there exist such $$$x$$$ and $$$y$$$ that if you take $$$x$$$ ($$$0 le x le a$$$) coins of value $$$n$$$ and $$$y$$$ ($$$0 le y le b$$$) coins of value $$$1$$$, then the total value of taken coins will be $$$S$$$. You have to answer $$$q$$$ independent test cases. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 10^4$$$) — the number of test cases. Then $$$q$$$ test cases follow. The only line of the test case contains four integers $$$a$$$, $$$b$$$, $$$n$$$ and $$$S$$$ ($$$1 le a, b, n, S le 10^9$$$) — the number of coins of value $$$n$$$, the number of coins of value $$$1$$$, the value $$$n$$$ and the required total value. Output For the $$$i$$$-th test case print the answer on it — YES (without quotes) if there exist such $$$x$$$ and $$$y$$$ that if you take $$$x$$$ coins of value $$$n$$$ and $$$y$$$ coins of value $$$1$$$, then the total value of taken coins will be $$$S$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 4 1 2 3 4 1 2 3 6 5 2 6 27 3 3 5 18 | 1,000 | true | false | false | false | false | false | false | false | false | false | 4,407 |
1288E | Polycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has $$$n$$$ friends, numbered from $$$1$$$ to $$$n$$$. Recall that a permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array. So his recent chat list can be represented with a permutation $$$p$$$ of size $$$n$$$. $$$p_1$$$ is the most recent friend Polycarp talked to, $$$p_2$$$ is the second most recent and so on. Initially, Polycarp's recent chat list $$$p$$$ looks like $$$1, 2, dots, n$$$ (in other words, it is an identity permutation). After that he receives $$$m$$$ messages, the $$$j$$$-th message comes from the friend $$$a_j$$$. And that causes friend $$$a_j$$$ to move to the first position in a permutation, shifting everyone between the first position and the current position of $$$a_j$$$ by $$$1$$$. Note that if the friend $$$a_j$$$ is in the first position already then nothing happens. For example, let the recent chat list be $$$p = [4, 1, 5, 3, 2]$$$: if he gets messaged by friend $$$3$$$, then $$$p$$$ becomes $$$[3, 4, 1, 5, 2]$$$; if he gets messaged by friend $$$4$$$, then $$$p$$$ doesn't change $$$[4, 1, 5, 3, 2]$$$; if he gets messaged by friend $$$2$$$, then $$$p$$$ becomes $$$[2, 4, 1, 5, 3]$$$. For each friend consider all position he has been at in the beginning and after receiving each message. Polycarp wants to know what were the minimum and the maximum positions. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 3 cdot 10^5$$$) — the number of Polycarp's friends and the number of received messages, respectively. The second line contains $$$m$$$ integers $$$a_1, a_2, dots, a_m$$$ ($$$1 le a_i le n$$$) — the descriptions of the received messages. Note In the first example, Polycarp's recent chat list looks like this: $$$[1, 2, 3, 4, 5]$$$ $$$[3, 1, 2, 4, 5]$$$ $$$[5, 3, 1, 2, 4]$$$ $$$[1, 5, 3, 2, 4]$$$ $$$[4, 1, 5, 3, 2]$$$ So, for example, the positions of the friend $$$2$$$ are $$$2, 3, 4, 4, 5$$$, respectively. Out of these $$$2$$$ is the minimum one and $$$5$$$ is the maximum one. Thus, the answer for the friend $$$2$$$ is a pair $$$(2, 5)$$$. In the second example, Polycarp's recent chat list looks like this: $$$[1, 2, 3, 4]$$$ $$$[1, 2, 3, 4]$$$ $$$[2, 1, 3, 4]$$$ $$$[4, 2, 1, 3]$$$ | 2,000 | false | false | false | false | true | false | false | false | false | false | 4,248 |
1374F | You are given an array $$$a$$$ consisting of $$$n$$$ integers. In one move, you can choose some index $$$i$$$ ($$$1 le i le n - 2$$$) and shift the segment $$$[a_i, a_{i + 1}, a_{i + 2}]$$$ cyclically to the right (i.e. replace the segment $$$[a_i, a_{i + 1}, a_{i + 2}]$$$ with $$$[a_{i + 2}, a_i, a_{i + 1}]$$$). Your task is to sort the initial array by no more than $$$n^2$$$ such operations or say that it is impossible to do that. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — 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 500$$$) — the length of $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 500$$$), where $$$a_i$$$ is the $$$i$$$-th element $$$a$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$500$$$. Output For each test case, print the answer: -1 on the only line if it is impossible to sort the given array using operations described in the problem statement, or the number of operations $$$ans$$$ on the first line and $$$ans$$$ integers $$$idx_1, idx_2, dots, idx_{ans}$$$ ($$$1 le idx_i le n - 2$$$), where $$$idx_i$$$ is the index of left border of the segment for the $$$i$$$-th operation. You should print indices in order of performing operations. Example Input 5 5 1 2 3 4 5 5 5 4 3 2 1 8 8 4 5 2 3 6 7 3 7 5 2 1 6 4 7 3 6 1 2 3 3 6 4 Output 0 6 3 1 3 2 2 3 13 2 1 1 6 4 2 4 3 3 4 4 6 6 -1 4 3 3 4 4 | 2,400 | false | false | true | false | false | true | true | false | true | false | 3,788 |
1793B | For his birthday recently Fedya was given an array $$$a$$$ of $$$n$$$ integers arranged in a circle, For each pair of neighboring numbers ($$$a_1$$$ and $$$a_2$$$, $$$a_2$$$ and $$$a_3$$$, $$$ldots$$$, $$$a_{n - 1}$$$ and $$$a_n$$$, $$$a_n$$$ and $$$a_1$$$) the absolute difference between them is equal to $$$1$$$. Let's call a local maximum an element, which is greater than both of its neighboring elements. Also call a local minimum an element, which is less than both of its neighboring elements. Note, that elements $$$a_1$$$ and $$$a_n$$$ are neighboring elements. Unfortunately, Fedya lost an array, but he remembered in it the sum of local maximums $$$x$$$ and the sum of local minimums $$$y$$$. Given $$$x$$$ and $$$y$$$, help Fedya find any matching array of minimum length. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. Each line of each test case contain two integers $$$x$$$ and $$$y$$$ ($$$-10^{9} le y < x le 10^{9}$$$) — the sum of local maximums and the sum of local minimums, respectively. Output For each test case, in the first line print one integer $$$n$$$ — the minimum length of matching arrays. In the second line print $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^{9} leqslant a_i leqslant 10^{9}$$$) — the array elements such that the the absolute difference between each pair of neighboring is equal to $$$1$$$. If there are multiple solutions, print any of them. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^{5}$$$. Example Output 10 0 1 2 1 0 -1 0 -1 0 1 16 -2 -1 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 6 1 0 -1 0 1 0 16 2 3 2 1 0 -1 0 -1 0 -1 0 1 2 1 0 1 Note In the first test case, the local maximums are the numbers at $$$3, 7$$$ and $$$10$$$ positions, and the local minimums are the numbers at $$$1, 6$$$ and $$$8$$$ positions. $$$x = a_3 + a_7 + a_{10} = 2 + 0 + 1 = 3$$$, $$$y = a_1 + a_6 + a_8 = 0 + (-1) + (-1) = -2$$$. In the second test case, the local maximums are the numbers at $$$2$$$ and $$$10$$$ positions, and the local minimums are the numbers at $$$1$$$ and $$$3$$$ positions. $$$x = a_2 + a_{10} = -1 + 5 = 4$$$, $$$y = a_1 + a_3 = -2 + (-2) = -4$$$. In the third test case, the local maximums are the numbers at $$$1$$$ and $$$5$$$ positions, and the local minimums are the numbers at $$$3$$$ and $$$6$$$ positions. | 1,100 | true | false | false | false | false | true | false | false | false | false | 1,519 |
732F | Berland is a tourist country! At least, it can become suchxa0— the government of Berland is confident about this. There are _n_ cities in Berland, some pairs of which are connected by two-ways roads. Each road connects two different cities. In Berland there are no roads which connect the same pair of cities. It is possible to get from any city to any other city using given two-ways roads. According to the reform each road will become one-way. It will be oriented to one of two directions. To maximize the tourist attraction of Berland, after the reform for each city _i_ the value _r__i_ will be calculated. It will equal to the number of cities _x_ for which there is an oriented path from the city _i_ to the city _x_. In other words, _r__i_ will equal the number of cities which can be reached from the city _i_ by roads. The government is sure that tourist's attention will be focused on the minimum value of _r__i_. Help the government of Berland make the reform to maximize the minimum of _r__i_. Input The first line contains two integers _n_,u2009_m_ (2u2009≤u2009_n_u2009≤u2009400u2009000,u20091u2009≤u2009_m_u2009≤u2009400u2009000)xa0— the number of cities and the number of roads. The next _m_ lines describe roads in Berland: the _j_-th of them contains two integers _u__j_ and _v__j_ (1u2009≤u2009_u__j_,u2009_v__j_u2009≤u2009_n_, _u__j_u2009≠u2009_v__j_), where _u__j_ and _v__j_ are the numbers of cities which are connected by the _j_-th road. The cities are numbered from 1 to _n_. It is guaranteed that it is possible to get from any city to any other by following two-ways roads. In Berland there are no roads which connect the same pair of cities. Output In the first line print single integerxa0— the maximum possible value _min_1u2009≤u2009_i_u2009≤u2009_n_{_r__i_} after the orientation of roads. The next _m_ lines must contain the description of roads after the orientation: the _j_-th of them must contain two integers _u__j_,u2009_v__j_, it means that the _j_-th road will be directed from the city _u__j_ to the city _v__j_. Print roads in the same order as they are given in the input data. Example Input 7 9 4 3 2 6 7 1 4 1 7 3 3 5 7 4 6 5 2 5 Output 4 4 3 6 2 7 1 1 4 3 7 5 3 7 4 5 6 2 5 | 2,300 | false | false | false | false | false | false | false | false | false | true | 6,865 |
891A | Problem - 891A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force dp greedy math number theory *1500 No tag edit access → Contest materials , where _gcd_ denotes the — the number of elements in the array. The second line contains _n_ space separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the elements of the array. Output Print -1, if it is impossible to turn all numbers to 1. Otherwise, print the minimum number of operations needed to make all numbers equal to 1. Examples Input 5 2 2 3 4 6 Output 5 Input 4 2 4 6 8 Output -1 Input 3 2 6 9 Output 4 Note In the first sample you can turn all numbers to 1 using the following 5 moves: | 1,500 | true | true | false | true | false | false | true | false | false | false | 6,188 |
1100D | This is an interactive task. Dasha and NN like playing chess. While playing a match they decided that normal chess isn't interesting enough for them, so they invented a game described below. There are $$$666$$$ black rooks and $$$1$$$ white king on the chess board of size $$$999 imes 999$$$. The white king wins if he gets checked by rook, or, in other words, if he moves onto the square which shares either a row or column with a black rook. The sides take turns, starting with white. NN plays as a white king and on each of his turns he moves a king to one of the squares that are adjacent to his current position either by side or diagonally, or, formally, if the king was on the square $$$(x, y)$$$, it can move to the square $$$(nx, ny)$$$ if and only $$$max (nx - x, ny - y) = 1$$$ , $$$1 leq nx, ny leq 999$$$. NN is also forbidden from moving onto the squares occupied with black rooks, however, he can move onto the same row or column as a black rook. Dasha, however, neglects playing by the chess rules, and instead of moving rooks normally she moves one of her rooks on any space devoid of other chess pieces. It is also possible that the rook would move onto the same square it was before and the position wouldn't change. However, she can't move the rook on the same row or column with the king. Each player makes $$$2000$$$ turns, if the white king wasn't checked by a black rook during those turns, black wins. NN doesn't like losing, but thinks the task is too difficult for him, so he asks you to write a program that will always win playing for the white king. Note that Dasha can see your king and play depending on its position. Input In the beginning your program will receive $$$667$$$ lines from input. Each line contains two integers $$$x$$$ and $$$y$$$ ($$$1 leq x, y leq 999$$$) — the piece's coordinates. The first line contains the coordinates of the king and the next $$$666$$$ contain the coordinates of the rooks. The first coordinate denotes the number of the row where the piece is located, the second denotes the column. It is guaranteed that initially the king isn't in check and that all pieces occupy different squares. Interaction To make a move with the king, output two integers $$$x$$$ and $$$y$$$ ($$$1 leq x, y leq 999$$$) — the square to which the king would be moved. The king cannot move onto the square already occupied by a rook. It is guaranteed that the king would always have a valid move. After each of your turns read the rook's turn in the following format: a single line containing three integers $$$k$$$, $$$x$$$ and $$$y$$$ ($$$1 leq k leq 666$$$, $$$1 leq x_i, y_i leq 999$$$) — the number of the rook that would move and the square it would move to. It is guaranteed that the rook wouldn't move to a square already occupied by another chess piece, but it can move onto the square where it was before the turn so that its position wouldn't change. It is guaranteed that the move does not put your king into a check. If your king got in check, all three integers would be equal to $$$-1$$$ and in that case your program should terminate immediately. After printing your turn do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Answer "0 0 0" instead of a correct answer means that you made an invalid query. Exit immediately after receiving "0 0 0" and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. Hacks are not allowed for this problem. | 2,500 | false | false | false | false | false | true | false | false | false | false | 5,203 |
1510J | Joseph really likes the culture of Japan. Last year he learned Japanese traditional clothes and visual arts and now he is trying to find out the secret of the Japanese game called Nonogram. In the one-dimensional version of the game, there is a row of $$$n$$$ empty cells, some of which are to be filled with a pen. There is a description of a solution called a profilexa0— a sequence of positive integers denoting the lengths of consecutive sets of filled cells. For example, the profile of $$$[4, 3, 1]$$$ means that there are sets of four, three, and one filled cell, in that order, with at least one empty cell between successive sets. A suitable solution for $$$n = 12$$$ and $$$p = [4, 3, 1]$$$. A wrong solution: the first four filled cells should be consecutive. A wrong solution: there should be at least one empty cell before the last filled cell. Joseph found out that for some numbers $$$n$$$ and profiles $$$p$$$ there are lots of ways to fill the cells to satisfy the profile. Now he is in the process of solving a nonogram consisting of $$$n$$$ cells and a profile $$$p$$$. He has already created a mask of $$$p$$$xa0— he has filled all the cells that must be filled in every solution of the nonogram. The mask for $$$n = 12$$$ and $$$p = [4, 3, 1]$$$: all the filled cells above are filled in every solution. After a break, he lost the source profile $$$p$$$. He only has $$$n$$$ and the mask $$$m$$$. Help Joseph find any profile $$$p'$$$ with the mask $$$m$$$ or say that there is no such profile and Joseph has made a mistake. Input The only line contains a string $$$m$$$xa0— the mask of the source profile $$$p$$$. The length of $$$m$$$ is $$$n$$$ ($$$1 le n le 100,000$$$). The string $$$m$$$ consists of symbols # and _xa0— denoting filled and empty cells respectively. Output If there is no profile with the mask $$$m$$$, output the number $$$-1$$$. Otherwise, on the first line, output an integer $$$k$$$xa0— the number of integers in the profile $$$p'$$$. On the second line, output $$$k$$$ integers of the profile $$$p'$$$. | 2,700 | true | false | false | false | false | true | false | false | false | false | 3,118 |
1616B | You have a string $$$s_1 s_2 ldots s_n$$$ and you stand on the left of the string looking right. You want to choose an index $$$k$$$ ($$$1 le k le n$$$) and place a mirror after the $$$k$$$-th letter, so that what you see is $$$s_1 s_2 ldots s_k s_k s_{k - 1} ldots s_1$$$. What is the lexicographically smallest string you can see? A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input The first line of input contains one integer $$$t$$$ ($$$1 leq t leq 10,000$$$): the number of test cases. The next $$$t$$$ lines contain the description of the test cases, two lines per a test case. In the first line you are given one integer $$$n$$$ ($$$1 leq n leq 10^5$$$): the length of the string. The second line contains the string $$$s$$$ consisting of $$$n$$$ lowercase English characters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print the lexicographically smallest string you can see. Example Input 4 10 codeforces 9 cbacbacba 3 aaa 4 bbaa Note In the first test case choose $$$k = 1$$$ to obtain "cc". In the second test case choose $$$k = 3$$$ to obtain "cbaabc". In the third test case choose $$$k = 1$$$ to obtain "aa". In the fourth test case choose $$$k = 1$$$ to obtain "bb". | 1,100 | false | true | false | false | false | false | false | false | false | false | 2,555 |
1534E | This is an interactive problem. Note: the XOR-sum of an array $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$) is defined as $$$a_1 oplus a_2 oplus ldots oplus a_n$$$, where $$$oplus$$$ denotes the , and allows you to perform the following type of query: by giving the machine $$$k$$$ distinct indices $$$x_1, x_2, ldots, x_k$$$, it will output $$$a_{x_1} oplus a_{x_2} oplus ldots oplus a_{x_k}$$$. As Little Dormi's older brother, you would like to help him recover the XOR-sum of his array $$$a_1, a_2, ldots, a_n$$$ by querying the XOR machine. Little Dormi isn't very patient, so to be as fast as possible, you must query the XOR machine the minimum number of times to find the XOR-sum of his array. Formally, let $$$d$$$ be the minimum number of queries needed to find the XOR-sum of any array of length $$$n$$$ with a query size of $$$k$$$. Your program will be accepted if you find the correct XOR-sum in at most $$$d$$$ queries. Lastly, you also noticed that with certain configurations of the machine $$$k$$$ and values of $$$n$$$, it may not be possible to recover the XOR-sum of Little Dormi's lost array. If that is the case, you should report it as well. The array $$$a_1, a_2, ldots, a_n$$$ is fixed before you start querying the XOR machine and does not change with the queries. Input The only line of input contains the integers $$$n$$$ and $$$k$$$ ($$$1 le n le 500$$$, $$$1 le k le n$$$), the length of the lost array and the configured query size of the XOR machine. Elements of the original array satisfy $$$1 le a_i le 10^9$$$. It can be proven that that if it is possible to recover the XOR sum under the given constraints, it can be done in at most $$$500$$$ queries. That is, $$$d le 500$$$. After taking $$$n$$$ and $$$k$$$, begin interaction. Output If it is impossible to recover the XOR-sum of the array, output $$$-1$$$ immediately after taking $$$n$$$ and $$$k$$$. Do not begin interaction. Otherwise, when your program finds the XOR-sum of the lost array $$$a_1, a_2, ldots, a_n$$$, report the answer in the following format: "! x", where $$$x$$$ is the XOR sum of the array $$$a_1, a_2, ldots, a_n$$$, and terminate your program normally immediately after flushing the output stream. Note that answering does not count as a query. Interaction Each query is made in the format "? b", where $$$b$$$ is an array of exactly $$$k$$$ distinct integers from $$$1$$$ to $$$n$$$ denoting the indices of the elements in the lost array that you want to query the XOR sum of. You will then receive an integer $$$x$$$, the XOR sum of the queried elements. It can be proven that $$$0 le x le 2 cdot 10^9$$$ will always be true. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. If at any point you make an invalid query or try to make more than $$$500$$$ queries (which is the hard limit), the interaction will terminate immediately and give you a Wrong Answer verdict. Note that if you exceed $$$d$$$ queries, the interaction will continue normally unless you also exceed the $$$500$$$ query hard limit, though you will still receive a Wrong Answer verdict either way. Hacks To hack a solution, use the following format. The first line contains the integers $$$n$$$ and $$$k$$$ ($$$1 le n le 500$$$, $$$1 le k le n$$$). The second line contains the the array $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). Examples Output ? 1 2 3 ? 2 3 5 ? 4 1 5 ! 7 Note In the first example interaction, the array $$$a_1, a_2, ldots, a_n$$$ is $$$2, 1, 7, 5, 6$$$ and its XOR-sum is $$$7$$$. The first query made asks for indices $$$1,2,3$$$, so the response is $$$a_1 oplus a_2 oplus a_3 = 2 oplus 1 oplus 7 = 4$$$. The second query made asks for indices $$$2,3,5$$$, so the response is $$$a_2 oplus a_3 oplus a_5 = 1 oplus 7 oplus 6 = 0$$$. The third query made asks for indices $$$4,1,5$$$, so the response is $$$a_4 oplus a_1 oplus a_5 = 5 oplus 2 oplus 6 = 1$$$. Note that the indices may be output in any order. Additionally, even though three queries were made in the example interaction, it is just meant to demonstrate the interaction format and does not necessarily represent an optimal strategy. In the second example interaction, there is no way to recover the XOR-sum of Little Dormi's array no matter what is queried, so the program immediately outputs $$$-1$$$ and exits. | 2,300 | false | true | false | false | false | false | false | false | false | true | 2,979 |
1474D | During cleaning the coast, Alice found $$$n$$$ piles of stones. The $$$i$$$-th pile has $$$a_i$$$ stones. Piles $$$i$$$ and $$$i + 1$$$ are neighbouring for all $$$1 leq i leq n - 1$$$. If pile $$$i$$$ becomes empty, piles $$$i - 1$$$ and $$$i + 1$$$ doesn't become neighbouring. Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation: Select two neighboring piles and, if both of them are not empty, remove one stone from each of them. Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability: Before the start of cleaning, you can select two neighboring piles and swap them. Determine, if it is possible to remove all stones using the superability not more than once. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0— the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the number of stones in each pile. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print YES or NOxa0— is it possible to remove all stones using the superability not more than once or not. Example Input 5 3 1 2 1 3 1 1 2 5 2 2 2 1 3 5 2100 1900 1600 3000 1600 2 2443 2445 Note In the first test case, you can remove all stones without using a superability: $$$[1, 2, 1] ightarrow [1, 1, 0] ightarrow [0, 0, 0]$$$. In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase. In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $$$a = [2, 2, 2, 3, 1]$$$. In the fourth test case, you can apply superability to the first and the second piles, thus getting $$$a = [1900, 2100, 1600, 3000, 1600]$$$. | 2,200 | true | true | false | true | true | false | false | false | false | false | 3,307 |
1354C1 | The statement of this problem is the same as the statement of problem C2. The only difference is that, in problem C1, $$$n$$$ is always even, and in C2, $$$n$$$ is always odd. You are given a regular polygon with $$$2 cdot n$$$ vertices (it's convex and has equal sides and equal angles) and all its sides have length $$$1$$$. Let's name it as $$$2n$$$-gon. Your task is to find the square of the minimum size such that you can embed $$$2n$$$-gon in the square. Embedding $$$2n$$$-gon in the square means that you need to place $$$2n$$$-gon in the square in such way that each point which lies inside or on a border of $$$2n$$$-gon should also lie inside or on a border of the square. You can rotate $$$2n$$$-gon and/or the square. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 200$$$)xa0— the number of test cases. Next $$$T$$$ lines contain descriptions of test casesxa0— one per line. Each line contains single even integer $$$n$$$ ($$$2 le n le 200$$$). Don't forget you need to embed $$$2n$$$-gon, not an $$$n$$$-gon. Output Print $$$T$$$ real numbersxa0— one per test case. For each test case, print the minimum length of a side of the square $$$2n$$$-gon can be embedded in. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$. Example Output 1.000000000 2.414213562 127.321336469 | 1,400 | true | false | false | false | false | false | false | true | false | false | 3,937 |
237B | You've got table _a_, consisting of _n_ rows, numbered from 1 to _n_. The _i_-th line of table _a_ contains _c__i_ cells, at that for all _i_ (1u2009<u2009_i_u2009≤u2009_n_) holds _c__i_u2009≤u2009_c__i_u2009-u20091. Let's denote _s_ as the total number of cells of table _a_, that is, . We know that each cell of the table contains a single integer from 1 to _s_, at that all written integers are distinct. Let's assume that the cells of the _i_-th row of table _a_ are numbered from 1 to _c__i_, then let's denote the number written in the _j_-th cell of the _i_-th row as _a__i_,u2009_j_. Your task is to perform several swap operations to rearrange the numbers in the table so as to fulfill the following conditions: 1. for all _i_,u2009_j_ (1u2009<u2009_i_u2009≤u2009_n_;xa01u2009≤u2009_j_u2009≤u2009_c__i_) holds _a__i_,u2009_j_u2009>u2009_a__i_u2009-u20091,u2009_j_; 2. for all _i_,u2009_j_ (1u2009≤u2009_i_u2009≤u2009_n_;xa01u2009<u2009_j_u2009≤u2009_c__i_) holds _a__i_,u2009_j_u2009>u2009_a__i_,u2009_j_u2009-u20091. In one swap operation you are allowed to choose two different cells of the table and swap the recorded there numbers, that is the number that was recorded in the first of the selected cells before the swap, is written in the second cell after it. Similarly, the number that was recorded in the second of the selected cells, is written in the first cell after the swap. Rearrange the numbers in the required manner. Note that you are allowed to perform any number of operations, but not more than _s_. You do not have to minimize the number of operations. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200950) that shows the number of rows in the table. The second line contains _n_ space-separated integers _c__i_ (1u2009≤u2009_c__i_u2009≤u200950;xa0_c__i_u2009≤u2009_c__i_u2009-u20091) — the numbers of cells on the corresponding rows. Next _n_ lines contain table _а_. The _i_-th of them contains _c__i_ space-separated integers: the _j_-th integer in this line represents _a__i_,u2009_j_. It is guaranteed that all the given numbers _a__i_,u2009_j_ are positive and do not exceed _s_. It is guaranteed that all _a__i_,u2009_j_ are distinct. Output In the first line print a single integer _m_ (0u2009≤u2009_m_u2009≤u2009_s_), representing the number of performed swaps. In the next _m_ lines print the description of these swap operations. In the _i_-th line print four space-separated integers _x__i_,u2009_y__i_,u2009_p__i_,u2009_q__i_ (1u2009≤u2009_x__i_,u2009_p__i_u2009≤u2009_n_;xa01u2009≤u2009_y__i_u2009≤u2009_c__x__i_;xa01u2009≤u2009_q__i_u2009≤u2009_c__p__i_). The printed numbers denote swapping the contents of cells _a__x__i_,u2009_y__i_ and _a__p__i_,u2009_q__i_. Note that a swap operation can change the contents of distinct table cells. Print the swaps in the order, in which they should be executed. | 1,500 | false | false | true | false | false | false | false | false | true | false | 8,888 |
1504B | There is a binary string $$$a$$$ of length $$$n$$$. In one operation, you can select any prefix of $$$a$$$ with an equal number of $$$0$$$ and $$$1$$$ symbols. Then all symbols in the prefix are inverted: each $$$0$$$ becomes $$$1$$$ and each $$$1$$$ becomes $$$0$$$. For example, suppose $$$a=0111010000$$$. In the first operation, we can select the prefix of length $$$8$$$ since it has four $$$0$$$'s and four $$$1$$$'s: $$$[01110100]00 o [10001011]00$$$. In the second operation, we can select the prefix of length $$$2$$$ since it has one $$$0$$$ and one $$$1$$$: $$$[10]00101100 o [01]00101100$$$. It is illegal to select the prefix of length $$$4$$$ for the third operation, because it has three $$$0$$$'s and one $$$1$$$. Can you transform the string $$$a$$$ into the string $$$b$$$ using some finite number of operations (possibly, none)? Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 3cdot 10^5$$$) — the length of the strings $$$a$$$ and $$$b$$$. The following two lines contain strings $$$a$$$ and $$$b$$$ of length $$$n$$$, consisting of symbols $$$0$$$ and $$$1$$$. The sum of $$$n$$$ across all test cases does not exceed $$$3cdot 10^5$$$. Output For each test case, output "YES" if it is possible to transform $$$a$$$ into $$$b$$$, or "NO" if it is impossible. You can print each letter in any case (upper or lower). Example Input 5 10 0111010000 0100101100 4 0000 0000 3 001 000 12 010101010101 100110011010 6 000111 110100 Note The first test case is shown in the statement. In the second test case, we transform $$$a$$$ into $$$b$$$ by using zero operations. In the third test case, there is no legal operation, so it is impossible to transform $$$a$$$ into $$$b$$$. In the fourth test case, here is one such transformation: Select the length $$$2$$$ prefix to get $$$100101010101$$$. Select the length $$$12$$$ prefix to get $$$011010101010$$$. Select the length $$$8$$$ prefix to get $$$100101011010$$$. Select the length $$$4$$$ prefix to get $$$011001011010$$$. Select the length $$$6$$$ prefix to get $$$100110011010$$$. In the fifth test case, the only legal operation is to transform $$$a$$$ into $$$111000$$$. From there, the only legal operation is to return to the string we started with, so we cannot transform $$$a$$$ into $$$b$$$. | 1,200 | true | true | true | false | false | true | false | false | false | false | 3,153 |
24A | Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all _n_ cities of Berland were connected by _n_ two-way roads in the ring, i. e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Berland introduced one-way traffic on all _n_ roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other? Input The first line contains integer _n_ (3u2009≤u2009_n_u2009≤u2009100) — amount of cities (and roads) in Berland. Next _n_ lines contain description of roads. Each road is described by three integers _a__i_, _b__i_, _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_,u20091u2009≤u2009_c__i_u2009≤u2009100) — road is directed from city _a__i_ to city _b__i_, redirecting the traffic costs _c__i_. Output Output single integer — the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other. Examples Input 6 1 5 4 5 3 8 2 4 15 1 6 16 2 3 23 4 6 42 Input 4 1 2 9 2 3 8 3 4 7 4 1 5 | 1,400 | false | false | false | false | false | false | false | false | false | true | 9,878 |
372A | Problem - 372A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search greedy sortings two pointers *1600 No tag edit access → Contest materials . 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. Input The first line contains a single integer — _n_ (1u2009≤u2009_n_u2009≤u20095·105). Each of the next _n_ lines contains an integer _s__i_ — the size of the _i_-th kangaroo (1u2009≤u2009_s__i_u2009≤u2009105). Output Output a single integer — the optimal number of visible kangaroos. Examples Input 8 2 5 7 6 9 8 4 2 Output 5 Input 8 9 1 6 2 6 5 8 3 Output 5 | 1,600 | false | true | false | false | false | false | false | true | true | false | 8,343 |
1527E | You are given an array $$$a$$$ of $$$n$$$ integers. Define the cost of some array $$$t$$$ as follows: $$$$$$cost(t) = sum_{x in set(t) } last(x) - first(x),$$$$$$ where $$$set(t)$$$ is the set of all values in $$$t$$$ without repetitions, $$$first(x)$$$, and $$$last(x)$$$ are the indices of the first and last occurrence of $$$x$$$ in $$$t$$$, respectively. In other words, we compute the distance between the first and last occurrences for each distinct element and sum them up. You need to split the array $$$a$$$ into $$$k$$$ consecutive segments such that each element of $$$a$$$ belongs to exactly one segment and the sum of the cost of individual segments is minimum. Input The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 35,000$$$, $$$1 le k le min(n,100)$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). Output Output the minimum sum of the cost of individual segments. Note In the first example, we can divide the array into $$$[1,6,6,4]$$$ and $$$[6,6,6]$$$. Cost of $$$[1,6,6,4]$$$ will be $$$(1-1) + (3 - 2) + (4-4) = 1$$$ and cost of $$$[6,6,6]$$$ will be $$$3-1 = 2$$$. Total cost would be $$$1 + 2 = 3$$$. In the second example, divide the array into $$$[5,5],[5],[5,2,3]$$$ and $$$[3]$$$. Total Cost would be $$$1 + 0 + 0 + 0 = 1$$$. | 2,500 | false | false | false | true | true | false | false | true | false | false | 3,024 |
1922D | Monocarp is playing a computer game (yet again). Guess what is he doing? That's right, killing monsters. There are $$$n$$$ monsters in a row, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th monster has two parameters: attack value equal to $$$a_i$$$ and defense value equal to $$$d_i$$$. In order to kill these monsters, Monocarp put a berserk spell on them, so they're attacking each other instead of Monocarp's character. The fight consists of $$$n$$$ rounds. Every round, the following happens: first, every alive monster $$$i$$$ deals $$$a_i$$$ damage to the closest alive monster to the left (if it exists) and the closest alive monster to the right (if it exists); then, every alive monster $$$j$$$ which received more than $$$d_j$$$ damage during this round dies. I.u2009e. the $$$j$$$-th monster dies if and only if its defense value $$$d_j$$$ is strictly less than the total damage it received this round. For each round, calculate the number of monsters that will die during that round. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of three lines: 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 10^9$$$); the third line contains $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$1 le d_i le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of monsters that die during the $$$i$$$-th round. Example Input 3 5 3 4 7 5 10 4 9 1 18 1 2 2 1 1 3 4 1 1 2 4 3 3 4 2 Output 3 1 0 0 0 0 0 1 1 1 0 Note Explanation for the first test case of the example: During the first round, the following happens: the monster $$$1$$$ deals $$$3$$$ damage to the monster $$$2$$$; the monster $$$2$$$ deals $$$4$$$ damage to the monster $$$1$$$ and the monster $$$3$$$; the monster $$$3$$$ deals $$$7$$$ damage to the monster $$$2$$$ and the monster $$$4$$$; the monster $$$4$$$ deals $$$5$$$ damage to the monster $$$3$$$ and the monster $$$5$$$; the monster $$$5$$$ deals $$$10$$$ damage to the monster $$$4$$$; the monster $$$1$$$ does not die, since it received $$$4$$$ damage and its defense is $$$4$$$; the monster $$$2$$$ dies, since it received $$$10$$$ damage and its defense is $$$9$$$; the monster $$$3$$$ dies, since it received $$$9$$$ damage and its defense is $$$1$$$; the monster $$$4$$$ does not die, since it received $$$17$$$ damage and its defense is $$$18$$$; the monster $$$5$$$ dies, since it received $$$5$$$ damage and its defense is $$$1$$$. After the first round, the monsters $$$1$$$ and $$$4$$$ stay alive. During the second round, the following happens: the monster $$$1$$$ deals $$$3$$$ damage to the monster $$$4$$$; the monster $$$4$$$ deals $$$5$$$ damage to the monster $$$1$$$; the monster $$$1$$$ dies, since it received $$$5$$$ damage and its defense is $$$4$$$; the monster $$$4$$$ does not die, since it received $$$3$$$ damage and its defense is $$$18$$$. During the next three rounds, only the $$$4$$$-th monster is alive, so nothing happens. | 1,900 | true | false | true | false | true | false | true | false | false | false | 755 |
1017A | John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed. There are $$$n$$$ students, each of them has a unique id (from $$$1$$$ to $$$n$$$). Thomas's id is $$$1$$$. Every student has four scores correspond to his or her English, German, Math, and History scores. The students are given in order of increasing of their ids. In the table, the students will be sorted by decreasing the sum of their scores. So, a student with the largest sum will get the first place. If two or more students have the same sum, these students will be sorted by increasing their ids. Please help John find out the rank of his son. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 1000$$$)xa0— the number of students. Each of the next $$$n$$$ lines contains four integers $$$a_i$$$, $$$b_i$$$, $$$c_i$$$, and $$$d_i$$$ ($$$0leq a_i, b_i, c_i, d_ileq 100$$$)xa0— the grades of the $$$i$$$-th student on English, German, Math, and History. The id of the $$$i$$$-th student is equal to $$$i$$$. Output Print the rank of Thomas Smith. Thomas's id is $$$1$$$. Examples Input 5 100 98 100 100 100 100 100 100 100 100 99 99 90 99 90 100 100 98 60 99 Input 6 100 80 90 99 60 60 60 60 90 60 100 60 60 100 60 80 100 100 0 100 0 0 0 0 Note In the first sample, the students got total scores: $$$398$$$, $$$400$$$, $$$398$$$, $$$379$$$, and $$$357$$$. Among the $$$5$$$ students, Thomas and the third student have the second highest score, but Thomas has a smaller id, so his rank is $$$2$$$. In the second sample, the students got total scores: $$$369$$$, $$$240$$$, $$$310$$$, $$$300$$$, $$$300$$$, and $$$0$$$. Among the $$$6$$$ students, Thomas got the highest score, so his rank is $$$1$$$. | 800 | false | false | true | false | false | false | false | false | false | false | 5,605 |
1740G | Pak Chanek has an $$$n imes m$$$ grid of portals. The portal on the $$$i$$$-th row and $$$j$$$-th column is denoted as portal $$$(i,j)$$$. The portals $$$(1,1)$$$ and $$$(n,m)$$$ are on the north-west and south-east corner of the grid respectively. The portal $$$(i,j)$$$ has two settings: Type $$$t_{i,j}$$$, which is either $$$0$$$ or $$$1$$$. Strength $$$s_{i,j}$$$, which is an integer between $$$1$$$ and $$$10^9$$$ inclusive. Each portal has $$$4$$$ faces labelled with integers $$$0,1,2,3$$$, which correspond to the north, east, south, and west direction respectively. When a laser enters face $$$k$$$ of portal $$$(i, j)$$$ with speed $$$x_ ext{in}$$$, it leaves the portal going out of face $$$(k+2+t_{i,j}) bmod 4$$$ with speed $$$x_ ext{out} = max(x_ ext{in},s_{i,j})$$$. The portal also has to consume $$$x_ ext{out} - x_ ext{in}$$$ units of energy. Pak Chanek is very bored today. He will shoot $$$4nm$$$ lasers with an initial speed of $$$1$$$, one into each face of each portal. Each laser will travel throughout this grid of portals until it moves outside the grid or it has passed through $$$10^{100}$$$ portals. At the end, Pak Chanek thinks that a portal is good if and only if the total energy consumed by that portal modulo $$$2$$$ is equal to its type. Given the strength settings of all portals, find a way to assign the type settings of each portal such that the number of good portals is maximised. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 1000$$$) — the number of rows and columns in the grid. The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers, with the $$$j$$$-th integer being $$$s_{i,j}$$$ ($$$1 leq s_{i,j} leq 10^9$$$) — the strength of portal $$$(i, j)$$$. Output Print $$$n$$$ lines with each line containing a string of length $$$m$$$ consisting of characters $$$0$$$ or $$$1$$$ representing the type settings. The $$$j$$$-th character in the $$$i$$$-th string is the type setting of portal $$$(i, j)$$$. If there are multiple solutions, you can output any of them. Note In the first example, let's consider the laser Pak Chanek shoots into face $$$1$$$ of portal $$$(2, 2)$$$. The laser travels as follows: 1. The laser enters face $$$1$$$ of portal $$$(2, 2)$$$ with speed $$$1$$$. It leaves the portal going out of face $$$3$$$ with speed $$$5$$$. Portal $$$(2, 2)$$$ consumes $$$4$$$ units of energy. 2. The laser enters face $$$1$$$ of portal $$$(2, 1)$$$ with speed $$$5$$$. It leaves the portal going out of face $$$0$$$ with speed $$$6$$$. Portal $$$(2, 1)$$$ consumes $$$1$$$ units of energy. 3. The laser enters face $$$2$$$ of portal $$$(1, 1)$$$ with speed $$$6$$$. It leaves the portal going out of face $$$1$$$ with speed $$$8$$$. Portal $$$(1, 1)$$$ consumes $$$2$$$ units of energy. 4. The laser enters face $$$3$$$ of portal $$$(1, 2)$$$ with speed $$$8$$$. It leaves the portal going out of face $$$2$$$ with speed $$$8$$$. Portal $$$(1, 2)$$$ consumes $$$0$$$ units of energy. 5. The laser enters face $$$0$$$ of portal $$$(2, 2)$$$ with speed $$$8$$$. It leaves the portal going out of face $$$2$$$ with speed $$$8$$$. Portal $$$(2, 2)$$$ consumes $$$0$$$ units of energy. The illustration of the travel of the laser above is as follows. As an example, consider portal $$$(2, 3)$$$. We can calculate that the total energy consumed by that portal in the end will be $$$32$$$. Since $$$32 bmod 2 = 0$$$ and $$$t_{2,3} = 0$$$, then it is a good portal. | 3,100 | false | false | false | false | false | true | false | false | true | false | 1,841 |
2014A | There is a little bit of the outlaw in everyone, and a little bit of the hero too. The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor. Robin encounters $$$n$$$ people starting from the $$$1$$$-st and ending with the $$$n$$$-th. The $$$i$$$-th person has $$$a_i$$$ gold. If $$$a_i ge k$$$, Robin will take all $$$a_i$$$ gold, and if $$$a_i=0$$$, Robin will give $$$1$$$ gold if he has any. Robin starts with $$$0$$$ gold. Find out how many people Robin gives gold to. Input The first line of the input contains a single integer $$$t$$$ ($$$1leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 50, 1 le k le 100$$$)xa0— the number of people and the threshold at which Robin Hood takes the gold. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 100$$$)xa0— the gold of each person. Output For each test case, output a single integer, the number of people that will get gold from Robin Hood. Example Input 4 2 2 2 0 3 2 3 0 0 6 2 0 3 0 0 0 0 2 5 5 4 Note In the first test case, Robin takes $$$2$$$ gold from the first person and gives a gold to the second person. In the second test case, Robin takes $$$3$$$ gold and gives $$$1$$$ gold to each of the next $$$2$$$ people. In the third test case, Robin takes $$$3$$$ gold and so only gives gold to $$$3$$$ other people. | 800 | false | true | true | false | false | false | false | false | false | false | 171 |
93D | When Igor K. was a freshman, his professor strictly urged him, as well as all other freshmen, to solve programming Olympiads. One day a problem called "Flags" from a website called Timmy's Online Judge caught his attention. In the problem one had to find the number of three-colored flags that would satisfy the condition... actually, it doesn't matter. Igor K. quickly found the formula and got the so passionately desired Accepted. However, the professor wasn't very much impressed. He decided that the problem represented on Timmy's Online Judge was very dull and simple: it only had three possible colors of flag stripes and only two limitations. He suggested a complicated task to Igor K. and the fellow failed to solve it. Of course, we won't tell anybody that the professor couldn't solve it as well. And how about you? Can you solve the problem? The flags consist of one or several parallel stripes of similar width. The stripes can be one of the following colors: white, black, red or yellow. You should find the number of different flags with the number of stripes from _L_ to _R_, if: a flag cannot have adjacent stripes of one color; a flag cannot have adjacent white and yellow stripes; a flag cannot have adjacent red and black stripes; a flag cannot have the combination of black, white and red stripes following one after another in this or reverse order; symmetrical flags (as, for example, a WB and a BW flag, where W and B stand for the white and black colors) are considered the same. Note In the first test the following flags exist (they are listed in the lexicographical order, the letters B, R, W, Y stand for Black, Red, White and Yellow correspondingly): 3 stripes: BWB, BYB, BYR, RWR, RYR, WBW, WBY, WRW, WRY, YBY, YRY (overall 11 flags). 4 stripes: BWBW, BWBY, BYBW, BYBY, BYRW, BYRY, RWRW, RWRY, RYBW, RYBY, RYRW, RYRY (12 flags). That's why the answer to test 1 is equal to 11u2009+u200912u2009=u200923. | 2,500 | true | false | false | true | false | false | false | false | false | false | 9,512 |
290E | Problem - 290E - Codeforces =============== xa0 | 2,500 | false | false | false | false | false | true | false | false | false | false | 8,673 |
1211F | Polycarp really likes writing the word "kotlin". He wrote this word several times in a row without spaces. For example, he could write the string like "kotlinkotlinkotlinkotlin". Polycarp sliced (cut) the written string into $$$n$$$ pieces and mixed them. As a result, he has $$$n$$$ strings $$$s_1, s_2, dots, s_n$$$ and he can arrange them in the right order, concatenate (join) all of them and get a string like "kotlinkotlin...kotlin". Help Polycarp to find the right order of strings $$$s_1, s_2, dots, s_n$$$, so that if he writes the strings in this order, he will get the word "kotlin" or the sequence of this word. Pay attention that you must use all given strings and you must use each string only once. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of Polycarp's strings. Next lines of the input contain $$$n$$$ Polycarp's strings. Total sum of their lengths doesn't exceed $$$3cdot10^5$$$. It's guaranteed that there is the right order of arrangement the strings that if you concatenate them into one string, you will get some non-empty sequence of the word "kotlin". Output Print $$$n$$$ different integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$), where $$$p_i$$$ is an index of the string that should be the $$$i$$$-th in a required concatenation. In other words, the result of concatenation $$$s_{p_1}+s_{p_2}+dots+s_{p_n}$$$ must be in the form "kotlinkotlin...kotlin". If there are many solutions, print any of them. Examples Input 4 linkotlinkotlinkotl kotlin in kot Input 8 i n tlin o ko t k l | 2,300 | false | false | true | false | false | false | false | false | false | true | 4,623 |
1055C | Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days $$$[l_a; r_a]$$$ are lucky, then there are some unlucky days: $$$[r_a + 1; l_a + t_a - 1]$$$, and then there are lucky days again: $$$[l_a + t_a; r_a + t_a]$$$ and so on. In other words, the day is lucky for Alice if it lies in the segment $$$[l_a + k t_a; r_a + k t_a]$$$ for some non-negative integer $$$k$$$. The Bob's lucky day have similar structure, however the parameters of his sequence are different: $$$l_b$$$, $$$r_b$$$, $$$t_b$$$. So a day is a lucky for Bob if it lies in a segment $$$[l_b + k t_b; r_b + k t_b]$$$, for some non-negative integer $$$k$$$. Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob. Input The first line contains three integers $$$l_a$$$, $$$r_a$$$, $$$t_a$$$ ($$$0 le l_a le r_a le t_a - 1, 2 le t_a le 10^9$$$) and describes Alice's lucky days. The second line contains three integers $$$l_b$$$, $$$r_b$$$, $$$t_b$$$ ($$$0 le l_b le r_b le t_b - 1, 2 le t_b le 10^9$$$) and describes Bob's lucky days. It is guaranteed that both Alice and Bob have some unlucky days. Output Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob. Note The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests. | 1,900 | true | false | false | false | false | false | false | false | false | false | 5,437 |
472E | One way to create task is to learn from game. You should pick a game and focus on part of the mechanic of that game, then it might be a good task. Let's have a try. Puzzle and Dragon was a popular game in Japan, we focus on the puzzle part of that game, it is a tile-matching puzzle. (Picture from Wikipedia page: is an _n_u2009×u2009_m_ board which consists of orbs. During the game you can do the following move. In the beginning of move you touch a cell of the board, then you can move your finger to one of the adjacent cells (a cell not on the boundary has 8 adjacent cells), then you can move your finger from the current cell to one of the adjacent cells one more time, and so on. Each time you move your finger from a cell to another cell, the orbs in these cells swap with each other. In other words whatever move you make, the orb in the cell you are touching never changes. The goal is to achieve such kind of pattern that the orbs will be cancelled and your monster will attack the enemy, but we don't care about these details. Instead, we will give you the initial board as an input and the target board as an output. Your goal is to determine whether there is a way to reach the target in a single move. Input The first line contains two integers: _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200930). The next _n_ lines each contains _m_ integers — the description of the initial board. The _j_-th integer in the _i_-th line is _s__i_,u2009_j_ (1u2009≤u2009_s__i_,u2009_j_u2009≤u2009900), where _s__i_,u2009_j_ denotes the type of the orb located in the _i_-th row and _j_-th column of the board. The next _n_ lines contain the target board in the same format. Note, that the initial board and the target board will be different. Output If there is no solution, then output: -1. If there is a solution, then in the first line output an integer _k_ (1u2009≤u2009_k_u2009≤u2009106) — the number of finger moves. In the next line print two integers _x_0 and _y_0 (1u2009≤u2009_x_0u2009≤u2009_n_;xa01u2009≤u2009_y_0u2009≤u2009_m_) — the position of the cell you touch at the beginning. In each of the next _k_ lines print two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_;xa01u2009≤u2009_y__i_u2009≤u2009_m_) — the position you move to. Note that this position must be adjacent to the previous position, that is _max_(_x__i_u2009-u2009_x__i_u2009-u20091,u2009_y__i_u2009-u2009_y__i_u2009-u20091)u2009=u20091. If there are multiple solutions, you can print any of them. We can prove that under these constraints if there exists a solution then there is a solution with no more than 106 operations. | 2,800 | false | false | true | false | false | true | false | false | false | false | 7,943 |
79E | Fox Ciel safely returned to her castle, but there was something wrong with the security system of the castle: sensors attached in the castle were covering her. Ciel is at point (1,u20091) of the castle now, and wants to move to point (_n_,u2009_n_), which is the position of her room. By one step, Ciel can move from point (_x_,u2009_y_) to either (_x_u2009+u20091,u2009_y_) (rightward) or (_x_,u2009_y_u2009+u20091) (upward). In her castle, _c_2 sensors are set at points (_a_u2009+u2009_i_,u2009_b_u2009+u2009_j_) (for every integer _i_ and _j_ such that: 0u2009≤u2009_i_u2009<u2009_c_,u20090u2009≤u2009_j_u2009<u2009_c_). Each sensor has a count value and decreases its count value every time Ciel moves. Initially, the count value of each sensor is _t_. Every time Ciel moves to point (_x_,u2009_y_), the count value of a sensor at point (_u_,u2009_v_) decreases by (_u_u2009-u2009_x_u2009+u2009_v_u2009-u2009_y_). When the count value of some sensor becomes strictly less than 0, the sensor will catch Ciel as a suspicious individual! Determine whether Ciel can move from (1,u20091) to (_n_,u2009_n_) without being caught by a sensor, and if it is possible, output her steps. Assume that Ciel can move to every point even if there is a censor on the point. Input In the first line there are five integers _n_,u2009_t_,u2009_a_,u2009_b_,u2009_c_ (2u2009≤u2009_n_u2009≤u20092·105,u2009 0u2009≤u2009_t_u2009≤u20091014,u2009 1u2009≤u2009_a_u2009≤u2009_n_u2009-u2009_c_u2009+u20091,u2009 1u2009≤u2009_b_u2009≤u2009_n_u2009-u2009_c_u2009+u20091,u2009 1u2009≤u2009_c_u2009≤u2009_n_). Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin stream (also you may use the %I64d specificator). Output If Ciel's objective is possible, output in first line 2_n_u2009-u20092 characters that represent her feasible steps, where _i_-th character is R if _i_-th step is moving rightward, or U if moving upward. If there are several solution, output lexicographically first one. Character R is lexicographically earlier than the character U. If her objective is impossible, output Impossible. Note The answers for the first sample and the second sample are shown on the picture: Here, a red point represents a point that contains a sensor. | 2,900 | true | false | false | false | false | false | false | false | false | false | 9,566 |
1556B | William has an array of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. In one move he can swap two neighboring items. Two items $$$a_i$$$ and $$$a_j$$$ are considered neighboring if the condition $$$i - j = 1$$$ is satisfied. William wants you to calculate the minimal number of swaps he would need to perform to make it so that the array does not contain two neighboring items with the same parity. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 le n le 10^5)$$$ which is the total number of items in William's array. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le 10^9)$$$ which are William's array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case output the minimal number of operations needed or $$$-1$$$ if it is impossible to get the array to a state when no neighboring numbers have the same parity. Example Input 5 3 6 6 1 1 9 6 1 1 1 2 2 2 2 8 6 6 6 2 3 4 5 1 Note In the first test case the following sequence of operations would satisfy the requirements: 1. swap(2, 3). Array after performing the operation: $$$[6, 1, 6]$$$ In the second test case the array initially does not contain two neighboring items of the same parity. In the third test case the following sequence of operations would satisfy the requirements: 1. swap(3, 4). Array after performing the operation: $$$[1, 1, 2, 1, 2, 2]$$$ 2. swap(2, 3). Array after performing the operation: $$$[1, 2, 1, 1, 2, 2]$$$ 3. swap(4, 5). Array after performing the operation: $$$[1, 2, 1, 2, 1, 2]$$$ In the fourth test case it is impossible to satisfy the requirements. In the fifth test case the following sequence of operations would satisfy the requirements: 1. swap(2, 3). Array after performing the operation: $$$[6, 3, 2, 4, 5, 1]$$$ 2. swap(4, 5). Array after performing the operation: $$$[6, 3, 2, 5, 4, 1]$$$ | 1,300 | false | false | true | false | false | false | false | false | false | false | 2,854 |
1085G | Petya collects beautiful matrix. A matrix of size $$$n imes n$$$ is beautiful if: All elements of the matrix are integers between $$$1$$$ and $$$n$$$; For every row of the matrix, all elements of this row are different; For every pair of vertically adjacent elements, these elements are different. Today Petya bought a beautiful matrix $$$a$$$ of size $$$n imes n$$$, and now he wants to determine its rarity. The rarity of the matrix is its index in the list of beautiful matrices of size $$$n imes n$$$, sorted in lexicographical order. Matrix comparison is done row by row. (The index of lexicographically smallest matrix is zero). Since the number of beautiful matrices may be huge, Petya wants you to calculate the rarity of the matrix $$$a$$$ modulo $$$998,244,353$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 2000$$$) — the number of rows and columns in $$$a$$$. Each of the next $$$n$$$ lines contains $$$n$$$ integers $$$a_{i,j}$$$ ($$$1 le a_{i,j} le n$$$) — the elements of $$$a$$$. It is guaranteed that $$$a$$$ is a beautiful matrix. Output Print one integer — the rarity of matrix $$$a$$$, taken modulo $$$998,244,353$$$. Note There are only $$$2$$$ beautiful matrices of size $$$2 imes 2$$$: There are the first $$$5$$$ beautiful matrices of size $$$3 imes 3$$$ in lexicographical order: | 2,900 | false | false | false | true | true | false | false | false | false | false | 5,293 |
821D | Okabe likes to be able to walk through his city on a path lit by street lamps. That way, he doesn't get beaten up by schoolchildren. Okabe's city is represented by a 2D grid of cells. Rows are numbered from 1 to _n_ from top to bottom, and columns are numbered 1 to _m_ from left to right. Exactly _k_ cells in the city are lit by a street lamp. It's guaranteed that the top-left cell is lit. Okabe starts his walk from the top-left cell, and wants to reach the bottom-right cell. Of course, Okabe will only walk on lit cells, and he can only move to adjacent cells in the up, down, left, and right directions. However, Okabe can also temporarily light all the cells in any single row or column at a time if he pays 1 coin, allowing him to walk through some cells not lit initially. Note that Okabe can only light a single row or column at a time, and has to pay a coin every time he lights a new row or column. To change the row or column that is temporarily lit, he must stand at a cell that is lit initially. Also, once he removes his temporary light from a row or column, all cells in that row/column not initially lit are now not lit. Help Okabe find the minimum number of coins he needs to pay to complete his walk! Input The first line of input contains three space-separated integers _n_, _m_, and _k_ (2u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u2009104). Each of the next _k_ lines contains two space-separated integers _r__i_ and _c__i_ (1u2009≤u2009_r__i_u2009≤u2009_n_, 1u2009≤u2009_c__i_u2009≤u2009_m_)xa0— the row and the column of the _i_-th lit cell. It is guaranteed that all _k_ lit cells are distinct. It is guaranteed that the top-left cell is lit. Note In the first sample test, Okabe can take the path , paying only when moving to (2,u20093) and (4,u20094). In the fourth sample, Okabe can take the path , paying when moving to (1,u20092), (3,u20094), and (5,u20094). | 2,200 | false | false | false | false | false | false | false | false | false | true | 6,470 |
553A | Problem - 553A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp math *1500 No tag edit access → Contest materials the number of colors. Then, _k_ lines will follow. The _i_-th line will contain _c__i_, the number of balls of the _i_-th color (1u2009≤u2009_c__i_u2009≤u20091000). The total number of balls doesn't exceed 1000. Output A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1u2009000u2009000u2009007. Examples Input 3 2 2 1 Output 3 Input 4 1 2 3 4 Output 1680 Note In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: 1 2 1 2 3 1 1 2 2 3 2 1 1 2 3 | 1,500 | true | false | false | true | false | false | false | false | false | false | 7,622 |
1070F | Elections in Berland are coming. There are only two candidates — Alice and Bob. The main Berland TV channel plans to show political debates. There are $$$n$$$ people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views: supporting none of candidates (this kind is denoted as "00"), supporting Alice but not Bob (this kind is denoted as "10"), supporting Bob but not Alice (this kind is denoted as "01"), supporting both candidates (this kind is denoted as "11"). The direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions: at least half of spectators support Alice (i.e. $$$2 cdot a ge m$$$, where $$$a$$$ is number of spectators supporting Alice and $$$m$$$ is the total number of spectators), at least half of spectators support Bob (i.e. $$$2 cdot b ge m$$$, where $$$b$$$ is number of spectators supporting Bob and $$$m$$$ is the total number of spectators), the total influence of spectators is maximal possible. Help the TV channel direction to select such non-empty set of spectators, or tell that this is impossible. Input The first line contains integer $$$n$$$ ($$$1 le n le 4cdot10^5$$$) — the number of people who want to take part in the debate as a spectator. These people are described on the next $$$n$$$ lines. Each line describes a single person and contains the string $$$s_i$$$ and integer $$$a_i$$$ separated by space ($$$1 le a_i le 5000$$$), where $$$s_i$$$ denotes person's political views (possible values — "00", "10", "01", "11") and $$$a_i$$$ — the influence of the $$$i$$$-th person. Output Print a single integer — maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead. Examples Input 6 11 6 10 4 01 3 00 3 00 7 00 9 Input 5 11 1 01 1 00 100 10 1 01 1 Input 6 11 19 10 22 00 18 00 29 11 29 10 28 Input 3 00 5000 00 5000 00 5000 Note In the first example $$$4$$$ spectators can be invited to maximize total influence: $$$1$$$, $$$2$$$, $$$3$$$ and $$$6$$$. Their political views are: "11", "10", "01" and "00". So in total $$$2$$$ out of $$$4$$$ spectators support Alice and $$$2$$$ out of $$$4$$$ spectators support Bob. The total influence is $$$6+4+3+9=22$$$. In the second example the direction can select all the people except the $$$5$$$-th person. In the third example the direction can select people with indices: $$$1$$$, $$$4$$$, $$$5$$$ and $$$6$$$. In the fourth example it is impossible to select any non-empty set of spectators. | 1,500 | false | true | false | false | false | false | false | false | false | false | 5,362 |
807B | Not so long ago the Codecraft-17 contest was held on Codeforces. The top 25 participants, and additionally random 25 participants out of those who got into top 500, will receive a Codeforces T-shirt. Unfortunately, you didn't manage to get into top 25, but you got into top 500, taking place _p_. Now the elimination round of 8VC Venture Cup 2017 is being held. It has been announced that the Codecraft-17 T-shirt winners will be chosen as follows. Let _s_ be the number of points of the winner of the elimination round of 8VC Venture Cup 2017. Then the following pseudocode will be executed: i := (s div 50) mod 475 repeat 25 times: i := (i 96 + 42) mod 475 print (26 + i) Here "div" is the integer division operator, "mod" is the modulo (the remainder of division) operator. As the result of pseudocode execution, 25 integers between 26 and 500, inclusive, will be printed. These will be the numbers of places of the participants who get the Codecraft-17 T-shirts. It is guaranteed that the 25 printed integers will be pairwise distinct for any value of _s_. You're in the lead of the elimination round of 8VC Venture Cup 2017, having _x_ points. You believe that having at least _y_ points in the current round will be enough for victory. To change your final score, you can make any number of successful and unsuccessful hacks. A successful hack brings you 100 points, an unsuccessful one takes 50 points from you. It's difficult to do successful hacks, though. You want to win the current round and, at the same time, ensure getting a Codecraft-17 T-shirt. What is the smallest number of successful hacks you have to do to achieve that? Input The only line contains three integers _p_, _x_ and _y_ (26u2009≤u2009_p_u2009≤u2009500; 1u2009≤u2009_y_u2009≤u2009_x_u2009≤u200920000)xa0— your place in Codecraft-17, your current score in the elimination round of 8VC Venture Cup 2017, and the smallest number of points you consider sufficient for winning the current round. Output Output a single integerxa0— the smallest number of successful hacks you have to do in order to both win the elimination round of 8VC Venture Cup 2017 and ensure getting a Codecraft-17 T-shirt. It's guaranteed that your goal is achievable for any valid input data. Note In the first example, there is no need to do any hacks since 10880 points already bring the T-shirt to the 239-th place of Codecraft-17 (that is, you). In this case, according to the pseudocode, the T-shirts will be given to the participants at the following places: 475 422 84 411 453 210 157 294 146 188 420 367 29 356 398 155 102 239 91 133 365 312 449 301 343 In the second example, you have to do two successful and one unsuccessful hack to make your score equal to 7408. In the third example, you need to do as many as 24 successful hacks to make your score equal to 10400. In the fourth example, it's sufficient to do 6 unsuccessful hacks (and no successful ones) to make your score equal to 6500, which is just enough for winning the current round and also getting the T-shirt. | 1,300 | false | false | true | false | false | false | true | false | false | false | 6,536 |
1041F | You are given a tube which is reflective inside represented as two non-coinciding, but parallel to $$$Ox$$$ lines. Each line has some special integer pointsxa0— positions of sensors on sides of the tube. You are going to emit a laser ray in the tube. To do so, you have to choose two integer points $$$A$$$ and $$$B$$$ on the first and the second line respectively (coordinates can be negative): the point $$$A$$$ is responsible for the position of the laser, and the point $$$B$$$xa0— for the direction of the laser ray. The laser ray is a ray starting at $$$A$$$ and directed at $$$B$$$ which will reflect from the sides of the tube (it doesn't matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor. Examples of laser rays. Note that image contains two examples. The $$$3$$$ sensors (denoted by black bold points on the tube sides) will register the blue ray but only $$$2$$$ will register the red. Calculate the maximum number of sensors which can register your ray if you choose points $$$A$$$ and $$$B$$$ on the first and the second lines respectively. Input The first line contains two integers $$$n$$$ and $$$y_1$$$ ($$$1 le n le 10^5$$$, $$$0 le y_1 le 10^9$$$)xa0— number of sensors on the first line and its $$$y$$$ coordinate. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— $$$x$$$ coordinates of the sensors on the first line in the ascending order. The third line contains two integers $$$m$$$ and $$$y_2$$$ ($$$1 le m le 10^5$$$, $$$y_1 < y_2 le 10^9$$$)xa0— number of sensors on the second line and its $$$y$$$ coordinate. The fourth line contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$0 le b_i le 10^9$$$)xa0— $$$x$$$ coordinates of the sensors on the second line in the ascending order. Output Print the only integerxa0— the maximum number of sensors which can register the ray. Note One of the solutions illustrated on the image by pair $$$A_2$$$ and $$$B_2$$$. | 2,500 | true | false | false | true | true | false | false | false | false | false | 5,490 |
159B | Polycarpus has _n_ markers and _m_ marker caps. Each marker is described by two numbers: _x__i_ is the color and _y__i_ is the diameter. Correspondingly, each cap is described by two numbers: _a__j_ is the color and _b__j_ is the diameter. Cap (_a__j_,u2009_b__j_) can close marker (_x__i_,u2009_y__i_) only if their diameters match, that is, _b__j_u2009=u2009_y__i_. Besides, a marker is considered to be beautifully closed, if the cap color and the marker color match, that is, _a__j_u2009=u2009_x__i_. Find the way to close the maximum number of markers. If there are several such ways, then choose the one that has the maximum number of beautifully closed markers. Input The first input line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of markers and the number of caps, correspondingly. Next _n_ lines describe the markers. The _i_-th line contains two space-separated integers _x__i_, _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u20091000) — the _i_-th marker's color and diameter, correspondingly. Next _m_ lines describe the caps. The _j_-th line contains two space-separated integers _a__j_, _b__j_ (1u2009≤u2009_a__j_,u2009_b__j_u2009≤u20091000) — the color and diameter of the _j_-th cap, correspondingly. Output Print two space-separated integers _u_,u2009_v_, where _u_ is the number of closed markers and _v_ is the number of beautifully closed markers in the sought optimal way. Remember that you have to find the way to close the maximum number of markers, and if there are several such ways, you should choose the one where the number of beautifully closed markers is maximum. Examples Input 3 4 1 2 3 4 2 4 5 4 2 4 1 1 1 2 Note In the first test sample the first marker should be closed by the fourth cap, the second marker should be closed by the first cap and the third marker should be closed by the second cap. Thus, three markers will be closed, and two of them will be beautifully closed — the first and the third markers. | 1,100 | false | true | false | false | false | false | false | false | true | false | 9,239 |
1505B | . Output Output the result – an integer number. | 1,600 | false | false | true | false | false | false | false | false | false | false | 3,151 |
2027E2 | This is the hard version of this problem. The only difference is that you need to output the number of choices of games where Bob wins in this version, where the number of stones in each pile are not fixed. You must solve both versions to be able to hack. Alice and Bob are playing a familiar game where they take turns removing stones from $$$n$$$ piles. Initially, there are $$$x_i$$$ stones in the $$$i$$$-th pile, and it has an associated value $$$a_i$$$. A player can take $$$d$$$ stones away from the $$$i$$$-th pile if and only if both of the following conditions are met: $$$1 le d le a_i$$$, and $$$x , & , d = d$$$, where $$$x$$$ is the current number of stones in the $$$i$$$-th pile and $$$&$$$ denotes the . The description of the test cases follows. The first line of each test case contains $$$n$$$ ($$$1 le n le 10^4$$$)xa0— the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i < 2^{30}$$$). The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i < 2^{30}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Output Output a single integer, the number of games where Bob wins, modulo $$$10^9 + 7$$$. Example Input 7 3 1 2 3 3 2 2 1 13 45 5 5 4 7 8 6 4 4 5 5 5 4 6 4 8 8 12 13 14 12 3 92856133 46637598 12345678 29384774 73775896 87654321 2 65 12 110 31 4 677810235 275091182 428565855 720629731 74522416 889934149 3394714 230851724 Output 4 4 0 6552 722019507 541 665443265 Note In the first test case, no matter which values of $$$x_2$$$ and $$$x_3$$$ we choose, the second and third piles will always be chosen exactly once before no more stones can be taken from them. If $$$x_1 = 2$$$, then no stones can be taken from it, so Bob will make the last move. If $$$x_1 = 1$$$ or $$$x_1 = 3$$$, then exactly one move can be made on that pile, so Alice will make the last move. So Bob wins when $$$x = [2, 1, 1]$$$ or $$$x = [2, 1, 2]$$$ or $$$x = [2, 2, 1]$$$ or $$$x = [2, 2, 2]$$$. In the second test case, Bob wins when $$$x_1 = 14$$$ or $$$x_1 = 30$$$ by removing $$$14 - k$$$ stones, where $$$k$$$ is the number of stones Alice removes on her turn. Bob also wins when $$$x_1 = 16$$$ or $$$x_1 = 32$$$ since Alice does not have any moves to begin with. | 3,100 | true | false | false | true | false | false | false | false | false | false | 104 |
1654D | Alice's potion making professor gave the following assignment to his students: brew a potion using $$$n$$$ ingredients, such that the proportion of ingredient $$$i$$$ in the final potion is $$$r_i > 0$$$ (and $$$r_1 + r_2 + cdots + r_n = 1$$$). He forgot the recipe, and now all he remembers is a set of $$$n-1$$$ facts of the form, "ingredients $$$i$$$ and $$$j$$$ should have a ratio of $$$x$$$ to $$$y$$$" (i.e., if $$$a_i$$$ and $$$a_j$$$ are the amounts of ingredient $$$i$$$ and $$$j$$$ in the potion respectively, then it must hold $$$a_i/a_j = x/y$$$), where $$$x$$$ and $$$y$$$ are positive integers. However, it is guaranteed that the set of facts he remembers is sufficient to uniquely determine the original values $$$r_i$$$. He decided that he will allow the students to pass the class as long as they submit a potion which satisfies all of the $$$n-1$$$ requirements (there may be many such satisfactory potions), and contains a positive integer amount of each ingredient. Find the minimum total amount of ingredients needed to make a potion which passes the class. As the result can be very large, you should print the answer modulo $$$998,244,353$$$. Input 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 a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). Each of the next $$$n-1$$$ lines contains four integers $$$i, j, x, y$$$ ($$$1 le i, j le n$$$, $$$i ot=j$$$, $$$1le x, y le n$$$) — ingredients $$$i$$$ and $$$j$$$ should have a ratio of $$$x$$$ to $$$y$$$. It is guaranteed that the set of facts is sufficient to uniquely determine the original values $$$r_i$$$. It is also guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the minimum total amount of ingredients needed to make a potion which passes the class, modulo $$$998,244,353$$$. Example Input 3 4 3 2 3 4 1 2 4 3 1 4 2 4 8 5 4 2 3 6 4 5 4 1 3 5 2 6 8 2 1 3 5 3 4 3 2 2 5 6 7 4 3 17 8 7 4 16 9 17 4 5 5 14 13 12 11 1 17 14 6 13 8 9 2 11 3 11 4 17 7 2 17 16 8 6 15 5 1 14 16 7 1 10 12 17 13 10 11 16 7 2 10 11 6 4 13 17 14 6 3 11 15 8 15 6 12 8 Note In the first test case, the minimum total amount of ingredients is $$$69$$$. In fact, the amounts of ingredients $$$1, 2, 3, 4$$$ of a valid potion are $$$16, 12, 9, 32$$$, respectively. The potion is valid because Ingredients $$$3$$$ and $$$2$$$ have a ratio of $$$9 : 12 = 3 : 4$$$; Ingredients $$$1$$$ and $$$2$$$ have a ratio of $$$16 : 12 = 4 : 3$$$; Ingredients $$$1$$$ and $$$4$$$ have a ratio of $$$16 : 32 = 2 : 4$$$. In the second test case, the amounts of ingredients $$$1, 2, 3, 4, 5, 6, 7, 8$$$ in the potion that minimizes the total amount of ingredients are $$$60, 60, 24, 48, 32, 60, 45, 30$$$. | 2,100 | true | false | false | false | false | false | false | false | false | false | 2,359 |
1107C | You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character. You are playing the game on the new generation console so your gamepad have $$$26$$$ buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct. You are given a sequence of hits, the $$$i$$$-th hit deals $$$a_i$$$ units of damage to the opponent's character. To perform the $$$i$$$-th hit you have to press the button $$$s_i$$$ on your gamepad. Hits are numbered from $$$1$$$ to $$$n$$$. You know that if you press some button more than $$$k$$$ times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons. To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of $$$a_i$$$ over all $$$i$$$ for the hits which weren't skipped. Note that if you skip the hit then the counter of consecutive presses the button won't reset. Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$) — the number of hits and the maximum number of times you can push the same button in a row. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the damage of the $$$i$$$-th hit. The third line of the input contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit). Output Print one integer $$$dmg$$$ — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons. Examples Input 7 3 1 5 16 18 7 2 10 baaaaca Input 5 5 2 4 1 3 1000 aaaaa Input 5 4 2 4 1 3 1000 aaaaa Input 8 1 10 15 2 1 4 8 15 16 qqwweerr Input 6 3 14 18 9 19 2 15 cccccc Note In the first example you can choose hits with numbers $$$[1, 3, 4, 5, 6, 7]$$$ with the total damage $$$1 + 16 + 18 + 7 + 2 + 10 = 54$$$. In the second example you can choose all hits so the total damage is $$$2 + 4 + 1 + 3 + 1000 = 1010$$$. In the third example you can choose all hits expect the third one so the total damage is $$$2 + 4 + 3 + 1000 = 1009$$$. In the fourth example you can choose hits with numbers $$$[2, 3, 6, 8]$$$. Only this way you can reach the maximum total damage $$$15 + 2 + 8 + 16 = 41$$$. In the fifth example you can choose only hits with numbers $$$[2, 4, 6]$$$ with the total damage $$$18 + 19 + 15 = 52$$$. In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage $$$10$$$. | 1,300 | false | true | false | false | false | false | false | false | true | false | 5,167 |
1360D | Polycarp wants to buy exactly $$$n$$$ shovels. The shop sells packages with shovels. The store has $$$k$$$ types of packages: the package of the $$$i$$$-th type consists of exactly $$$i$$$ shovels ($$$1 le i le k$$$). The store has an infinite number of packages of each type. Polycarp wants to choose one type of packages and then buy several (one or more) packages of this type. What is the smallest number of packages Polycarp will have to buy to get exactly $$$n$$$ shovels? For example, if $$$n=8$$$ and $$$k=7$$$, then Polycarp will buy $$$2$$$ packages of $$$4$$$ shovels. Help Polycarp find the minimum number of packages that he needs to buy, given that he: will buy exactly $$$n$$$ shovels in total; the sizes of all packages he will buy are all the same and the number of shovels in each package is an integer from $$$1$$$ to $$$k$$$, inclusive. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases in the input. Then, $$$t$$$ test cases follow, one per line. Each test case consists of two positive integers $$$n$$$ ($$$1 le n le 10^9$$$) and $$$k$$$ ($$$1 le k le 10^9$$$)xa0— the number of shovels and the number of types of packages. Output Print $$$t$$$ answers to the test cases. Each answer is a positive integerxa0— the minimum number of packages. Example Input 5 8 7 8 1 6 10 999999733 999999732 999999733 999999733 Note The answer to the first test case was explained in the statement. In the second test case, there is only one way to buy $$$8$$$ shovelsxa0— $$$8$$$ packages of one shovel. In the third test case, you need to buy a $$$1$$$ package of $$$6$$$ shovels. | 1,300 | true | false | false | false | false | false | false | false | false | false | 3,882 |
54B | The Hedgehog recently remembered one of his favorite childhood activities, — solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces. All puzzle pieces turn out to be of the same size _X_u2009×u2009_Y_, because the picture is cut first by horizontal cuts with the pitch of _X_, then with vertical cuts with the pitch of _Y_. If we denote the initial size of the picture as _A_u2009×u2009_B_, then _A_ must be divisible by _X_ and _B_ must be divisible by _Y_ (_X_ and _Y_ are integer numbers). However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over). Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. Input The first line contains two numbers _A_ and _B_ which are the sizes of the picture. They are positive integers not exceeding 20. Then follow _A_ lines containing _B_ symbols each, describing the actual picture. The lines only contain uppercase English letters. Output In the first line print the number of possible good puzzles (in other words, the number of pairs (_X_,u2009_Y_) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers — the sizes _X_ and _Y_ of the smallest possible element among all good puzzles. The comparison is made firstly by the area _XY_ of one element and secondly — by the length _X_. Note The picture in the first sample test has the following good puzzles: (2,u20091), (2,u20092), (2,u20094). | 1,800 | false | false | true | false | false | false | false | false | false | false | 9,704 |
900D | Problem - 900D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks combinatorics dp math number theory *2000 No tag edit access → Contest materials . Announcement.") . Editorial.") consisting of positive integers such that _gcd_(_a_1,u2009_a_2,u2009...,u2009_a__n_)u2009=u2009_x_ and . As this number could be large, print the answer modulo 109u2009+u20097. _gcd_ here means the . Output Print the number of such sequences modulo 109u2009+u20097. Examples Input 3 9 Output 3 Input 5 8 Output 0 Note There are three suitable sequences in the first test: (3,u20093,u20093), (3,u20096), (6,u20093). There are no suitable sequences in the second test. | 2,000 | true | false | false | true | false | false | false | false | false | false | 6,143 |
167D | In some country live wizards. They love to build cities and roads. The country used to have _k_ cities, the _j_-th city (1u2009≤u2009_j_u2009≤u2009_k_) was located at a point (_x__j_, _y__j_). It was decided to create another _n_u2009-u2009_k_ cities. And the _i_-th one (_k_u2009<u2009_i_u2009≤u2009_n_) was created at a point with coordinates (_x__i_, _y__i_): _x__i_u2009=u2009(_a_·_x__i_u2009-u20091u2009+u2009_b_)xa0_mod_xa0(109u2009+u20099) _y__i_u2009=u2009(_c_·_y__i_u2009-u20091u2009+u2009_d_)xa0_mod_xa0(109u2009+u20099) Here _a_, _b_, _c_, _d_ are primes. Also, _a_u2009≠u2009_c_,u2009_b_u2009≠u2009_d_. After the construction of all _n_ cities, the wizards have noticed something surprising. It turned out that for every two different cities _i_ and _j_, _x__i_u2009≠u2009_x__j_ and _y__i_u2009≠u2009_y__j_ holds. The cities are built, it's time to build roads! It was decided to use the most difficult (and, of course, the most powerful) spell for the construction of roads. Using this spell creates a road between the towns of _u_, _v_ (_y__u_ > _y__v_) if and only if for any city _w_ which lies strictly inside the corner at the point _u_, _v_ (see below), there is a city _s_ that does not lie in the corner, which is located along the _x_-coordinate strictly between _w_ and _u_ and simultaneously _y__s_u2009>u2009_y__v_. A corner on the points _p_2(_x_2, _y_2), _p_1(_x_1, _y_1) (_y_1u2009<u2009_y_2) is the set of points (_x_,u2009_y_), for which at least one of the two conditions is fulfilled: _min_(_x_1,u2009_x_2)u2009≤u2009_x_u2009≤u2009_max_(_x_1,u2009_x_2) and _y_u2009≥u2009_y_1 _y_1u2009≤u2009_y_u2009≤u2009_y_2 and (_x_u2009-u2009_x_2)·(_x_1u2009-u2009_x_2)u2009≥u20090 The pictures showing two different corners In order to test the spell, the wizards will apply it to all the cities that lie on the _x_-coordinate in the interval [_L_,u2009_R_]. After the construction of roads the national government wants to choose the maximum number of pairs of cities connected by the road, so that no city occurs in two or more pairs. Your task is for each _m_ offered variants of values _L_, _R_ to calculate the maximum number of such pairs after the construction of the roads. Please note that the cities that do not lie in the interval [_L_,u2009_R_] on the _x_-coordinate, do not affect the construction of roads in any way. Input The first line contains two space-separated integers _n_, _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105, _k_u2009≤u200930). Next _k_ lines contain coordinates of the cities' location points from the first to the _k_-th one. The _j_-th line contains space-separated pair of integers _x__j_, _y__j_ (0u2009≤u2009_x__j_,u2009_y__j_u2009<u2009109u2009+u20099) — coordinates of the _j_-th city. The next line contains space-separated integers _a_,u2009_b_,u2009_c_,u2009_d_ (2u2009≤u2009_a_,u2009_b_,u2009_c_,u2009_d_u2009<u2009109u2009+u20099). It is guaranteed that those numbers are prime and also that _a_u2009≠u2009_c_,u2009_b_u2009≠u2009_d_. It's guaranteed, that for every two different cities _i_ and _j_, _x__i_u2009≠u2009_x__j_ and _y__i_u2009≠u2009_y__j_ holds. The next line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of variants to build the roads. Next _m_ lines contain pairs of space-separated integers _L__i_, _R__i_ (0u2009≤u2009_L__i_u2009≤u2009_R__i_u2009<u2009109u2009+u20099) — the variants of choosing the cities to build the roads. Note In the first sample the roads connect the cities in a chain in the order of increasing of _x_. In the second sample the remaining 5 cities will be located at points (5,u2009xa011);xa0(20,u2009xa0263098);xa0(65,u2009xa0292514823);xa0(200,u2009xa076958738);xa0(605,u2009xa0622120197). | 3,000 | false | true | false | false | true | false | false | false | false | true | 9,192 |
1618F | You are given two positive integers $$$x$$$ and $$$y$$$. You can perform the following operation with $$$x$$$: write it in its binary form without leading zeros, add $$$0$$$ or $$$1$$$ to the right of it, reverse the binary form and turn it into a decimal number which is assigned as the new value of $$$x$$$. For example: $$$34$$$ can be turned into $$$81$$$ via one operation: the binary form of $$$34$$$ is $$$100010$$$, if you add $$$1$$$, reverse it and remove leading zeros, you will get $$$1010001$$$, which is the binary form of $$$81$$$. $$$34$$$ can be turned into $$$17$$$ via one operation: the binary form of $$$34$$$ is $$$100010$$$, if you add $$$0$$$, reverse it and remove leading zeros, you will get $$$10001$$$, which is the binary form of $$$17$$$. $$$81$$$ can be turned into $$$69$$$ via one operation: the binary form of $$$81$$$ is $$$1010001$$$, if you add $$$0$$$, reverse it and remove leading zeros, you will get $$$1000101$$$, which is the binary form of $$$69$$$. $$$34$$$ can be turned into $$$69$$$ via two operations: first you turn $$$34$$$ into $$$81$$$ and then $$$81$$$ into $$$69$$$. Your task is to find out whether $$$x$$$ can be turned into $$$y$$$ after a certain number of operations (possibly zero). Input The only line of the input contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le 10^{18}$$$). Output Print YES if you can make $$$x$$$ equal to $$$y$$$ and NO if you can't. Examples Input 8935891487501725 71487131900013807 Note In the first example, you don't even need to do anything. The fourth example is described in the statement. | 2,000 | true | false | true | false | false | true | false | false | false | false | 2,537 |
1787G | An edge-weighted tree of $$$n$$$ nodes is given with each edge colored in some color. Each node of this tree can be blocked or unblocked, all nodes are unblocked initially. A simple path is a path in a graph that does not have repeating nodes. The length of a path is defined as the sum of weights of all edges on the path. A path is good when it is a simple path consisting of edges of the same color $$$c$$$, all edges of color $$$c$$$ are on this path, and every node on the path is unblocked. You need to operate $$$2$$$ kinds of queries: 1. block a node, 2. unblock a node. After each query, print the maximum length among all good paths. If there are no good paths, print $$$0$$$. Input The first line contains two integers $$$n$$$, $$$q$$$ ($$$1 leq n,q leq 2cdot 10^5$$$) — the number of nodes and the number of queries. Then $$$n-1$$$ lines follow, each containing four integers $$$u$$$, $$$v$$$, $$$w$$$ and $$$c$$$ ($$$1 leq u,v,w,c leq n$$$; $$$u ot = v$$$), denoting a weighted edge connecting node $$$u$$$ and node $$$v$$$ with weight $$$w$$$ and color $$$c$$$. It is guaranteed that these edges form a tree. Then $$$q$$$ lines follow, each containing two integers $$$p$$$ and $$$x$$$ ($$$p = 0$$$ or $$$p = 1$$$, $$$1leq xleq n$$$), denoting a query: 1. if $$$p = 0$$$, block the node $$$x$$$. It's guaranteed that it's not blocked at this time; 2. if $$$p = 1$$$, unblock the node $$$x$$$. It's guaranteed that it's blocked at this time. Output For each query, print the maximum length of a good path. If there are no good paths, print $$$0$$$. Examples Input 5 4 4 1 3 4 5 2 4 4 3 1 3 2 1 2 5 1 0 4 0 3 0 2 1 3 Input 5 5 4 1 4 4 4 5 2 2 3 1 2 4 3 2 3 1 0 3 0 4 1 3 1 4 0 1 Input 6 9 3 2 2 3 2 4 4 2 3 1 5 5 6 4 3 2 5 3 1 3 0 2 0 4 0 5 0 6 1 2 1 4 1 5 0 3 1 6 | 3,000 | false | false | false | false | true | false | true | false | false | false | 1,557 |
204D | The Little Elephant has found a ragged old black-and-white string _s_ on the attic. The characters of string _s_ are numbered from the left to the right from 1 to _s_, where _s_ is the length of the string. Let's denote the _i_-th character of string _s_ as _s__i_. As the string is black-and-white, each character of the string is either letter "B", or letter "W". Unfortunately, the string is very old and some characters are damaged. The damaged positions are denoted as "X". The Little Elephant in determined to restore the string and hang it on the wall. For that he needs to replace each character "X" by a "B" or a "W". The string must look good on the wall, so it must be beautiful. The Little Elephant considers a string beautiful if it has two non-intersecting substrings of a given length _k_, such that the left one fully consists of characters "B", and the right one fully consists of characters "W". More formally, there are four integers _a_,u2009_b_,u2009_c_,u2009_d_ (1u2009≤u2009_a_u2009≤u2009_b_u2009<u2009_c_u2009≤u2009_d_u2009≤u2009_s_;xa0_b_u2009-u2009_a_u2009+u20091u2009=u2009_d_u2009-u2009_c_u2009+u20091u2009=u2009_k_) such that _s__i_ = "B" (_a_u2009≤u2009_i_u2009≤u2009_b_) and _s__j_ = "W" (_c_u2009≤u2009_j_u2009≤u2009_d_). Help the Little Elephant find the number of different beautiful strings he can obtain from string _s_. Two strings are considered different if there is such position, where the character in the first string differs from the corresponding character in the second string. If this string doesn't contain characters «X» and it is already beautiful — the answer is 1. As the answer can be rather large, print it modulo 1000000007 (109u2009+u20097). | 2,400 | false | false | false | true | false | false | false | false | false | false | 9,024 |
1740A | Problem - 1740A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms number theory *800 No tag edit access → Contest materials ") — the number of test cases. The following lines contain the description of each test case. The only line of each test case contains a prime number $$$n$$$ ($$$2 leq n leq 10^5$$$). Output For each test case, output a line containing a prime number $$$m$$$ ($$$2 leq m leq 10^5$$$) such that $$$n + m$$$ is not prime. It can be proven that under the constraints of the problem, such $$$m$$$ always exists. If there are multiple solutions, you can output any of them. Example Input 3 7 2 75619 Output 2 7 47837 Note In the first test case, $$$m = 2$$$, which is prime, and $$$n + m = 7 + 2 = 9$$$, which is not prime. In the second test case, $$$m = 7$$$, which is prime, and $$$n + m = 2 + 7 = 9$$$, which is not prime. In the third test case, $$$m = 47837$$$, which is prime, and $$$n + m = 75619 + 47837 = 123456$$$, which is not prime. | 800 | false | false | false | false | false | true | false | false | false | false | 1,847 |
203A | A boy Valera registered on site Codeforces as Valera, and wrote his first Codeforces Round #300. He boasted to a friend Arkady about winning as much as _x_ points for his first contest. But Arkady did not believe his friend's words and decided to check whether Valera could have shown such a result. He knows that the contest number 300 was unusual because there were only two problems. The contest lasted for _t_ minutes, the minutes are numbered starting from zero. The first problem had the initial cost of _a_ points, and every minute its cost reduced by _d__a_ points. The second problem had the initial cost of _b_ points, and every minute this cost reduced by _d__b_ points. Thus, as soon as the zero minute of the contest is over, the first problem will cost _a_u2009-u2009_d__a_ points, and the second problem will cost _b_u2009-u2009_d__b_ points. It is guaranteed that at any moment of the contest each problem has a non-negative cost. Arkady asks you to find out whether Valera could have got exactly _x_ points for this contest. You should assume that Valera could have solved any number of the offered problems. You should also assume that for each problem Valera made no more than one attempt, besides, he could have submitted both problems at the same minute of the contest, starting with minute 0 and ending with minute number _t_u2009-u20091. Please note that Valera can't submit a solution exactly _t_ minutes after the start of the contest or later. Input The single line of the input contains six integers _x_,u2009_t_,u2009_a_,u2009_b_,u2009_d__a_,u2009_d__b_ (0u2009≤u2009_x_u2009≤u2009600;xa01u2009≤u2009_t_,u2009_a_,u2009_b_,u2009_d__a_,u2009_d__b_u2009≤u2009300) — Valera's result, the contest's duration, the initial cost of the first problem, the initial cost of the second problem, the number of points that the first and the second problem lose per minute, correspondingly. It is guaranteed that at each minute of the contest each problem has a non-negative cost, that is, _a_u2009-u2009_i_·_d__a_u2009≥u20090 and _b_u2009-u2009_i_·_d__b_u2009≥u20090 for all 0u2009≤u2009_i_u2009≤u2009_t_u2009-u20091. Output If Valera could have earned exactly _x_ points at a contest, print "YES", otherwise print "NO" (without the quotes). Note In the first sample Valera could have acted like this: he could have submitted the first problem at minute 0 and the second problem — at minute 2. Then the first problem brings him 20 points and the second problem brings him 10 points, that in total gives the required 30 points. | 1,200 | false | false | true | false | false | false | true | false | false | false | 9,032 |