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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
104A | One rainy gloomy evening when all modules hid in the nearby cafes to drink hot energetic cocktails, the Hexadecimal virus decided to fly over the Mainframe to look for a Great Idea. And she has found one! Why not make her own Codeforces, with blackjack and other really cool stuff? Many people will surely be willing to visit this splendid shrine of high culture. In Mainframe a standard pack of 52 cards is used to play blackjack. The pack contains cards of 13 values: 2, 3, 4, 5, 6, 7, 8, 9, 10, jacks, queens, kings and aces. Each value also exists in one of four suits: hearts, diamonds, clubs and spades. Also, each card earns some value in points assigned to it: cards with value from two to ten earn from 2 to 10 points, correspondingly. An ace can either earn 1 or 11, whatever the player wishes. The picture cards (king, queen and jack) earn 10 points. The number of points a card earns does not depend on the suit. The rules of the game are very simple. The player gets two cards, if the sum of points of those cards equals _n_, then the player wins, otherwise the player loses. The player has already got the first card, it's the queen of spades. To evaluate chances for victory, you should determine how many ways there are to get the second card so that the sum of points exactly equals _n_. Note In the first sample only four two's of different suits can earn the required sum of points. In the second sample we can use all tens, jacks, queens and kings; overall it's 15 cards, as the queen of spades (as any other card) is only present once in the pack of cards and it's already in use. In the third sample there is no card, that would add a zero to the current ten points. | 800 | false | false | true | false | false | false | false | false | false | false | 9,467 |
462B | Problem - 462B - 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 . The next line contains _n_ uppercase letters without spaces β the _i_-th letter describes the _i_-th card of the Appleman. Output Print a single integer β the answer to the problem. Examples Input 15 10 DZFDFZDFDDDDDDF Output 82 Input 6 4 YJSNPI Output 4 Note In the first test example Toastman can choose nine cards with letter D and one additional card with any letter. For each card with D he will get 9 coins and for the additional card he will get 1 coin. | 1,300 | false | true | false | false | false | false | false | false | false | false | 7,990 |
803A | Problem - 803A - Codeforces =============== xa0 and is lexicographically maximal. One matrix is lexicographically greater than the other if the first different number in the first different row from the top in the first matrix is greater than the corresponding number in the second one. If there exists no such matrix then output -1. Input The first line consists of two numbers _n_ and _k_ (1u2009β€u2009_n_u2009β€u2009100, 0u2009β€u2009_k_u2009β€u2009106). Output If the answer exists then output resulting matrix. Otherwise output -1. Examples Input 2 1 Output 1 0 0 0 Input 3 2 Output 1 0 0 0 1 0 0 0 0 Input 2 5 Output -1 | 1,400 | false | false | false | false | false | true | false | false | false | false | 6,552 |
1718E | Burenka has two pictures $$$a$$$ and $$$b$$$, which are tables of the same size $$$n imes m$$$. Each cell of each painting has a color β a number from $$$0$$$ to $$$2 cdot 10^5$$$, and there are no repeating colors in any row or column of each of the two paintings, except color $$$0$$$. Burenka wants to get a picture $$$b$$$ from the picture $$$a$$$. To achieve her goal, Burenka can perform one of $$$2$$$ operations: swap any two rows of $$$a$$$ or any two of its columns. Tell Burenka if she can fulfill what she wants, and if so, tell her the sequence of actions. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n cdot m leq 2 cdot 10^5$$$) β the sizes of the table. The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i, 1}, a_{i, 2}, ldots, a_{i, m}$$$ ($$$0 leq a_{i,j} leq 2 cdot 10^5$$$)xa0β the colors of the $$$i$$$-th row of picture $$$a$$$. It is guaranteed that there are no identical colors in the same row or column, except color $$$0$$$. The $$$i$$$-th of the following $$$n$$$ lines contains $$$m$$$ integers $$$b_{i, 1}, b_{i, 2}, ldots, b_{i, m}$$$ ($$$0 leq b_{i,j} leq 2 cdot 10^5$$$)xa0β the colors of the $$$i$$$-th row of picture $$$b$$$. It is guaranteed that there are no identical colors in the same row or column, except color $$$0$$$. Output In the first line print the number $$$-1$$$ if it is impossible to achieve what Burenka wants, otherwise print the number of actions in your solution $$$k$$$ ($$$0 le k le 2 cdot 10^5$$$). It can be proved that if a solution exists, then there exists a solution where $$$k le 2 cdot 10^5$$$. In the next $$$k$$$ lines print the operations. First print the type of the operation ($$$1$$$ β swap rows, $$$2$$$ β columns), and then print the two indices of rows or columns to which the operation is applied. Note that you don't have to minimize the number of operations. Examples Input 3 3 1 0 2 0 0 0 2 0 1 2 0 1 0 0 0 1 0 2 Input 4 4 0 0 1 2 3 0 0 0 0 1 0 0 1 0 0 0 2 0 0 1 0 3 0 0 0 1 0 0 0 0 1 0 Output 4 1 3 4 2 3 4 2 2 3 2 1 2 Input 3 3 1 2 0 0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 | 3,500 | true | false | true | false | false | true | false | false | false | true | 1,970 |
1149B | During the archaeological research in the Middle East you found the traces of three ancient religions: First religion, Second religion and Third religion. You compiled the information on the evolution of each of these beliefs, and you now wonder if the followers of each religion could coexist in peace. The Word of Universe is a long word containing the lowercase English characters only. At each moment of time, each of the religion beliefs could be described by a word consisting of lowercase English characters. The three religions can coexist in peace if their descriptions form disjoint subsequences of the Word of Universe. More formally, one can paint some of the characters of the Word of Universe in three colors: $$$1$$$, $$$2$$$, $$$3$$$, so that each character is painted in at most one color, and the description of the $$$i$$$-th religion can be constructed from the Word of Universe by removing all characters that aren't painted in color $$$i$$$. The religions however evolve. In the beginning, each religion description is empty. Every once in a while, either a character is appended to the end of the description of a single religion, or the last character is dropped from the description. After each change, determine if the religions could coexist in peace. Input The first line of the input contains two integers $$$n, q$$$ ($$$1 leq n leq 100,000$$$, $$$1 leq q leq 1000$$$) β the length of the Word of Universe and the number of religion evolutions, respectively. The following line contains the Word of Universe β a string of length $$$n$$$ consisting of lowercase English characters. Each of the following line describes a single evolution and is in one of the following formats: + $$$i$$$ $$$c$$$ ($$$i in {1, 2, 3}$$$, $$$c in {mathtt{a}, mathtt{b}, dots, mathtt{z}}$$$: append the character $$$c$$$ to the end of $$$i$$$-th religion description. - $$$i$$$ ($$$i in {1, 2, 3}$$$) β remove the last character from the $$$i$$$-th religion description. You can assume that the pattern is non-empty. You can assume that no religion will have description longer than $$$250$$$ characters. Output Write $$$q$$$ lines. The $$$i$$$-th of them should be YES if the religions could coexist in peace after the $$$i$$$-th evolution, or NO otherwise. You can print each character in any case (either upper or lower). Note In the first example, after the 6th evolution the religion descriptions are: ad, bc, and ab. The following figure shows how these descriptions form three disjoint subsequences of the Word of Universe: | 2,200 | false | false | true | true | false | false | false | false | false | false | 4,968 |
1566C | A binary string is a string that consists of characters $$$0$$$ and $$$1$$$. A bi-table is a table that has exactly two rows of equal length, each being a binary string. Let $$$operatorname{MEX}$$$ of a bi-table be the smallest digit among $$$0$$$, $$$1$$$, or $$$2$$$ that does not occur in the bi-table. For example, $$$operatorname{MEX}$$$ for $$$begin{bmatrix} 0011 1010 end{bmatrix}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ occur in the bi-table at least once. $$$operatorname{MEX}$$$ for $$$begin{bmatrix} 111 111 end{bmatrix}$$$ is $$$0$$$, because $$$0$$$ and $$$2$$$ do not occur in the bi-table, and $$$0 < 2$$$. You are given a bi-table with $$$n$$$ columns. You should cut it into any number of bi-tables (each consisting of consecutive columns) so that each column is in exactly one bi-table. It is possible to cut the bi-table into a single bi-table β the whole bi-table. What is the maximal sum of $$$operatorname{MEX}$$$ of all resulting bi-tables can be? Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. Description of the test cases follows. The first line of the description of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β the number of columns in the bi-table. Each of the next two lines contains a binary string of length $$$n$$$xa0β the rows of the bi-table. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print a single integerxa0β the maximal sum of $$$operatorname{MEX}$$$ of all bi-tables that it is possible to get by cutting the given bi-table optimally. Example Input 4 7 0101000 1101100 5 01100 10101 2 01 01 6 000000 111111 Note In the first test case you can cut the bi-table as follows: $$$begin{bmatrix} 0 1 end{bmatrix}$$$, its $$$operatorname{MEX}$$$ is $$$2$$$. $$$begin{bmatrix} 10 10 end{bmatrix}$$$, its $$$operatorname{MEX}$$$ is $$$2$$$. $$$begin{bmatrix} 1 1 end{bmatrix}$$$, its $$$operatorname{MEX}$$$ is $$$0$$$. $$$begin{bmatrix} 0 1 end{bmatrix}$$$, its $$$operatorname{MEX}$$$ is $$$2$$$. $$$begin{bmatrix} 0 0 end{bmatrix}$$$, its $$$operatorname{MEX}$$$ is $$$1$$$. $$$begin{bmatrix} 0 0 end{bmatrix}$$$, its $$$operatorname{MEX}$$$ is $$$1$$$. The sum of $$$operatorname{MEX}$$$ is $$$8$$$. | 1,000 | false | true | false | true | false | true | false | false | false | false | 2,811 |
1648F | In order to make the capital of Berland a more attractive place for tourists, the great king came up with the following plan: choose two streets of the city and call them avenues. Certainly, these avenues will be proclaimed extremely important historical places, which should attract tourists from all over the world. The capital of Berland can be represented as a graph, the vertices of which are crossroads, and the edges are streets connecting two crossroads. In total, there are $$$n$$$ vertices and $$$m$$$ edges in the graph, you can move in both directions along any street, you can get from any crossroad to any other by moving only along the streets, each street connects two different crossroads, and no two streets connect the same pair of crossroads. In order to reduce the flow of ordinary citizens moving along the great avenues, it was decided to introduce a toll on each avenue in both directions. Now you need to pay $$$1$$$ tugrik for one passage along the avenue. You don't have to pay for the rest of the streets. Analysts have collected a sample of $$$k$$$ citizens, $$$i$$$-th of them needs to go to work from the crossroad $$$a_i$$$ to the crossroad $$$b_i$$$. After two avenues are chosen, each citizen will go to work along the path with minimal cost. In order to earn as much money as possible, it was decided to choose two streets as two avenues, so that the total number of tugriks paid by these $$$k$$$ citizens is maximized. Help the king: according to the given scheme of the city and a sample of citizens, find out which two streets should be made avenues, and how many tugriks the citizens will pay according to this choice. Input Each test consists of multiple test cases. The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^5$$$) β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 leq n leq 500,000$$$, $$$n - 1 leq m leq 500,000$$$, $$$m le frac{n (n - 1)}{2}$$$) β the number of crossroads and streets, respectively. The next $$$m$$$ lines contain the description of streets, $$$i$$$-th line contains two integers $$$s_i$$$ and $$$f_i$$$ ($$$1 leq s_i, f_i leq n$$$, $$$s_i eq f_i$$$) β indexes of crossroads which are connected by $$$i$$$-th street. It is guaranteed that no two streets connect the same pair of crossroads, you can get from any crossroad to any other by moving only along the streets. The next line contains a single integer $$$k$$$ ($$$1 leq k leq 500,000$$$) β the amount of citizens in the sample. The next $$$k$$$ lines contain the description of citizens, $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$, $$$a_i eq b_i$$$) β $$$i$$$-th citizen goes to work from crossroad $$$a_i$$$ to crossroad $$$b_i$$$. Let $$$M$$$ be the sum of $$$m$$$ over all test cases and $$$K$$$ be the sum of $$$k$$$ over all test cases. It is guaranteed that $$$M, K le 500,000$$$. Output For each test case print the answer to the problem. In the first line print the total amount of tugriks that will be paid by citizens. In the second line print two integers $$$x_1$$$ and $$$y_1$$$xa0β the numbers of crossroads that will be connected by the first avenue. In the third line print two integers $$$x_2$$$ and $$$y_2$$$xa0β the numbers of crossroads that will be connected by the second avenue. The numbers of crossroads connected by an avenue can be printed in any order, each of the printed streets should be among $$$m$$$ streets of the city, chosen streets should be different. Example Input 3 6 5 1 2 2 3 2 4 4 5 4 6 3 1 6 5 3 2 5 5 5 1 2 2 3 3 4 4 5 5 1 6 1 5 1 3 1 3 2 4 2 5 5 3 8 10 1 2 2 3 3 4 4 5 5 6 6 7 7 8 7 1 1 8 3 6 4 2 5 3 7 2 5 7 8 Output 5 4 2 5 4 5 1 5 3 2 3 7 6 2 3 | 3,500 | false | false | false | false | true | false | false | false | false | true | 2,378 |
965D | A lot of frogs want to cross a river. A river is $$$w$$$ units width, but frogs can only jump $$$l$$$ units long, where $$$l < w$$$. Frogs can also jump on lengths shorter than $$$l$$$. but can't jump longer. Hopefully, there are some stones in the river to help them. The stones are located at integer distances from the banks. There are $$$a_i$$$ stones at the distance of $$$i$$$ units from the bank the frogs are currently at. Each stone can only be used once by one frog, after that it drowns in the water. What is the maximum number of frogs that can cross the river, given that then can only jump on the stones? Input The first line contains two integers $$$w$$$ and $$$l$$$ ($$$1 le l < w le 10^5$$$)xa0β the width of the river and the maximum length of a frog's jump. The second line contains $$$w - 1$$$ integers $$$a_1, a_2, ldots, a_{w-1}$$$ ($$$0 le a_i le 10^4$$$), where $$$a_i$$$ is the number of stones at the distance $$$i$$$ from the bank the frogs are currently at. Output Print a single integerxa0β the maximum number of frogs that can cross the river. Examples Input 10 5 0 0 1 0 2 0 0 1 0 Input 10 3 1 1 1 1 2 1 1 1 1 Note In the first sample two frogs can use the different stones at the distance $$$5$$$, and one frog can use the stones at the distances $$$3$$$ and then $$$8$$$. In the second sample although there are two stones at the distance $$$5$$$, that does not help. The three paths are: $$$0 o 3 o 6 o 9 o 10$$$, $$$0 o 2 o 5 o 8 o 10$$$, $$$0 o 1 o 4 o 7 o 10$$$. | 1,900 | false | true | false | false | false | false | false | true | false | false | 5,839 |
1626D | Monocarp is planning to host a martial arts tournament. There will be three divisions based on weight: lightweight, middleweight and heavyweight. The winner of each division will be determined by a single elimination system. In particular, that implies that the number of participants in each division should be a power of two. Additionally, each division should have a non-zero amount of participants. $$$n$$$ participants have registered for the tournament so far, the $$$i$$$-th of them weighs $$$a_i$$$. To split participants into divisions, Monocarp is going to establish two integer weight boundaries $$$x$$$ and $$$y$$$ ($$$x < y$$$). All participants who weigh strictly less than $$$x$$$ will be considered lightweight. All participants who weigh greater or equal to $$$y$$$ will be considered heavyweight. The remaining participants will be considered middleweight. It's possible that the distribution doesn't make the number of participants in each division a power of two. It can also lead to empty divisions. To fix the issues, Monocarp can invite an arbitrary number of participants to each division. Note that Monocarp can't kick out any of the $$$n$$$ participants who have already registered for the tournament. However, he wants to invite as little extra participants as possible. Help Monocarp to choose $$$x$$$ and $$$y$$$ in such a way that the total amount of extra participants required is as small as possible. Output that amount. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the number of the registered participants. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$)xa0β the weights of the registered participants. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0β the smallest number of extra participants Monocarp is required to invite after he chooses the weight boundaries $$$x$$$ and $$$y$$$. Example Input 4 4 3 1 2 1 1 1 6 2 2 2 1 1 1 8 6 3 6 3 6 3 6 6 Note In the first testcase of the example, Monocarp can choose $$$x=2$$$ and $$$y=3$$$. Lightweight, middleweight and heavyweight divisions will have $$$2$$$, $$$1$$$ and $$$1$$$ participants, respectively. They all are powers of two, so no extra participants are required. In the second testcase of the example, regardless of the choice of $$$x$$$ and $$$y$$$, one division will have $$$1$$$ participant, the rest will have $$$0$$$. Thus, Monocarp will have to invite $$$1$$$ participant into both of the remaining divisions. In the third testcase of the example, Monocarp can choose $$$x=1$$$ and $$$y=2$$$. Lightweight, middleweight and heavyweight divisions will have $$$0$$$, $$$3$$$ and $$$3$$$ participants, respectively. So an extra participant is needed in each division. In the fourth testcase of the example, Monocarp can choose $$$x=8$$$ and $$$y=9$$$. Lightweight, middleweight and heavyweight divisions will have $$$8$$$, $$$0$$$ and $$$0$$$ participants, respectively. Middleweight and heavyweight division need an extra participant each. | 2,100 | true | true | false | false | false | false | true | true | false | false | 2,484 |
1876D | Pak Chanek loves his faculty, the Faculty of Computer Science, University of Indonesia (Fasilkom). He wants to play with the colours of the faculty's logo, blue and red. There is an array $$$a$$$ consisting of $$$n$$$ elements, element $$$i$$$ has a value of $$$a_i$$$. Pak Chanek wants to colour each element in the array blue or red such that these following conditions are satisfied: If all blue elements are formed into a subsequence$$$^dagger$$$ and so are all the red elements, the blue subsequence is strictly less than the red subsequence lexicographically$$$^ddagger$$$. Array $$$a$$$ does not have any subarray that is imbalanced. A subarray is imbalanced if and only if there is a value $$$k$$$ such that the absolute difference between the number of blue elements with value $$$k$$$ and the number of red elements with value $$$k$$$ in this subarray is $$$2$$$ or more. Note that it is possible to colour every element of the array the same colour. How many different colourings satisfy all those conditions? Since the answer can be very big, print the answer modulo $$$998,244,353$$$. Two colourings are different if and only if there is at least one element that is blue in one colouring, but red in the other. $$$^dagger$$$ A subsequence of an array is a sequence that can be obtained from the array by deleting some elements (possibly none), without changing the order of the remaining elements. $$$^ddagger$$$ Let $$$p$$$ and $$$q$$$ be two different sequences. Sequence $$$p$$$ is said to be lexicographically less than sequence $$$q$$$ if and only if $$$p$$$ is a prefix of $$$q$$$ or there is an index $$$i$$$ such that $$$p_j=q_j$$$ holds for every $$$1leq j<i$$$, and $$$p_i<q_i$$$. In particular, an empty sequence is always lexicographically less than any non-empty sequence. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2cdot10^5$$$) β the size of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,a_3,ldots,a_n$$$ ($$$1leq a_ileq2cdot10^5$$$). Output An integer representing the number of different colourings that satisfy all of the problem's conditions, modulo $$$998,244,353$$$. Note In the first example, the $$$3$$$ ways for colouring all elements from index $$$1$$$ to index $$$8$$$ are: Blue, red, red, blue, blue, red, red, blue. Blue, red, red, red, blue, blue, red, blue. Red, red, blue, blue, blue, red, red, blue. As an example, if we colour the elements from index $$$1$$$ to index $$$8$$$ to be red, red, blue, red, red, blue, blue, blue, it is not valid colouring, because for subarray $$$a[2..6]$$$, there are $$$0$$$ blue elements with value $$$3$$$ and $$$2$$$ red elements with value $$$3$$$, making subarray $$$a[2..6]$$$ an imbalanced subarray. | 2,500 | false | false | false | false | false | false | false | false | false | true | 1,023 |
1847E | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 882 (Div. 2) 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 combinatorics implementation interactive math probabilities *2900 No tag edit access β Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E. Triangle Platinum? time limit per test2 seconds memory limit per test256 megabytes This is an interactive problem. Made in Heaven is a rather curious Stand. Of course, it is (arguably) the strongest Stand in existence, but it is also an ardent puzzle enjoyer. For example, it gave Qtaro the following problem recently: Made in Heaven has $$$n$$$ hidden integers $$$a_1, a_2, dots, a_n$$$ ($$$3 le n le 5000$$$, $$$1 le a_i le 4$$$). Qtaro must determine all the $$$a_i$$$ by asking Made in Heaven some queries of the following form: In one query Qtaro is allowed to give Made in Heaven three distinct indexes $$$i$$$, $$$j$$$ and $$$k$$$ ($$$1 leq i, j, k leq n$$$). If $$$a_i, a_j, a_k$$$ form the sides of a non-degenerate triangle$$$^dagger$$$, Made in Heaven will respond with the area of this triangle. Otherwise, Made in Heaven will respond with $$$0$$$. By asking at most $$$5500$$$ such questions, Qtaro must either tell Made in Heaven all the values of the $$$a_i$$$, or report that it is not possible to uniquely determine them. Unfortunately due to the universe reboot, Qtaro is not as smart as Jotaro. Please help Qtaro solve Made In Heaven's problem. ββββββββββββββββββββββ $$$^dagger$$$ Three positive integers $$$a, b, c$$$ are said to form the sides of a non-degenerate triangle if and only if all of the following three inequalities hold: $$$a+b > c$$$, $$$b+c > a$$$, $$$c+a > b$$$. Interaction The interaction begins with reading $$$n$$$ ($$$3 le n le 5000$$$), the number of hidden integers. To ask a question corresponding to the triple $$$(i, j, k)$$$ ($$$1 leq i < j < k leq n$$$), output "? $$$i$$$ $$$j$$$ $$$k$$$" without quotes. Afterward, you should read a single integer $$$s$$$. If $$$s = 0$$$, then $$$a_i$$$, $$$a_j$$$, and $$$a_k$$$ are not the sides of a non-degenerate triangle. Otherwise, $$$s = 16 Delta^2$$$, where $$$Delta$$$ is the area of the triangle. The area is provided in this format for your convenience so that you need only take integer input. If the numbers $$$a_i$$$ cannot be uniquely determined print "! $$$-1$$$" without quotes. On the other hand, if you have determined all the values of $$$a_i$$$ print "! $$$a_1$$$ $$$a_2$$$ $$$dots$$$ $$$a_n$$$" on a single line. The interactor is non-adaptive. The hidden array $$$a_1, a_2, dots, a_n$$$ is fixed beforehand and is not changed during the interaction process. 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. Hacks You can hack a solution with the following input format. The first line contains a single integer $$$n$$$ ($$$3 le n le 5000$$$)xa0β the number of hidden integers. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 4$$$)xa0β the hidden array. Examples input 3 63 output ? 1 2 3 ! -1 input 6 0 0 0 63 15 135 output ? 1 2 3 ? 2 3 4 ? 4 5 6 ? 1 5 6 ? 3 5 6 ? 1 2 4 ! 3 2 1 4 2 2 input 15 3 3 3 3 3 0 output ? 1 2 3 ? 4 6 7 ? 8 9 10 ? 11 12 13 ? 13 14 15 ? 4 5 6 ! -1 input 15 3 15 0 3 3 3 output ? 1 2 3 ? 3 4 5 ? 4 5 6 ? 7 8 9 ? 10 11 12 ? 13 14 15 ! 1 1 1 2 2 4 1 1 1 1 1 1 1 1 1 1 input 10 3 48 3 48 63 0 output ? 1 3 5 ? 4 6 8 ? 1 5 9 ? 6 8 10 ? 4 2 6 ? 7 10 8 ! 1 3 1 2 1 2 4 2 1 2 Note In the first example, the interaction process happens as follows: Stdin Stdout Explanation 3 Read $$$n = 3$$$. There are $$$3$$$ hidden integers ? 1 2 3 Ask for the area formed by $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ 63 Received $$$16Delta^2 = 63$$$. So the area $$$Delta = sqrt{frac{63}{16}} approx 1.984313$$$ ! -1 Answer that there is no unique array satisfying the queries. From the area received, we can deduce that the numbers that forms the triangle are either ($$$4$$$, $$$4$$$, $$$1$$$) or ($$$3$$$, $$$2$$$, $$$2$$$) (in some order). As there are multiple arrays of numbers that satisfy the queries, a unique answer cannot be found. In the second example, the interaction process happens as follows: Step Stdin Stdout Explanation 1 6 Read $$$n = 6$$$. There are $$$6$$$ hidden integers 2 ? 1 2 3 Ask for the area formed by $$$a_1$$$, $$$a_2$$$ and $$$a_3$$$ 3 0 Does not form a non-degenerate triangle 4 ? 2 3 4 Ask for the area formed by $$$a_2$$$, $$$a_3$$$ and $$$a_4$$$ 5 0 Does not form a non-degenerate triangle 6 ? 4 5 6 Ask for the area formed by $$$a_4$$$, $$$a_5$$$ and $$$a_6$$$ 7 0 Does not form a non-degenerate triangle 8 ? 1 5 6 Ask for the area formed by $$$a_1$$$, $$$a_5$$$ and $$$a_6$$$ 9 63 Received $$$16Delta^2 = 63$$$. So the area $$$Delta = sqrt{frac{63}{16}} approx 1.984313$$$ 10 ? 3 5 6 Ask for the area formed by $$$a_3$$$, $$$a_5$$$ and $$$a_6$$$ 11 15 Received $$$16Delta^2 = 15$$$. So the area $$$Delta = sqrt{frac{15}{16}} approx 0.968245$$$ 12 ? 1 2 4 Ask for the area formed by $$$a_3$$$, $$$a_5$$$ and $$$a_6$$$ 13 135 Received $$$16Delta^2 = 135$$$. So the area $$$Delta = sqrt{frac{135}{16}} approx 2.904738$$$ 14 ! 3 2 1 4 2 2 A unique answer is found, which is $$$a = [3, 2, 1, 4, 2, 2]$$$. From steps $$$10$$$ and $$$11$$$, we can deduce that the the multiset $$$left{a_3, a_5, a_6 ight}$$$ must be $$$left{2, 2, 1 ight}$$$. From steps $$$8$$$ and $$$9$$$, the multiset $$$left{a_1, a_5, a_6 ight}$$$ must be either $$$left{4, 4, 1 ight}$$$ or $$$left{3, 2, 2 ight}$$$. As $$$left{a_3, a_5, a_6 ight}$$$ and $$$left{a_1, a_5, a_6 ight}$$$ share $$$a_5$$$ and $$$a_6$$$, we conclude that $$$a_5 = a_6 = 2$$$, as well as $$$a_1 = 3$$$, $$$a_3 = 1$$$. From steps $$$6$$$ and $$$7$$$, we know that $$$a_5 = a_6 = 2$$$, and $$$a_4$$$, $$$a_5$$$ and $$$a_6$$$ cannot form a non-degenerate triangle, hence $$$a_4 = 4$$$. With all the known information, only $$$a_2 = 2$$$ satisfies the queries made in steps $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$, $$$12$$$ and $$$13$$$. In the third example, one array that satisfies the queries is $$$[1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$$$. Codeforces (c) | 2,900 | true | false | true | false | false | false | true | false | false | false | 1,196 |
1658B | Marin wants you to count number of permutations that are beautiful. A beautiful permutation of length $$$n$$$ is a permutation that has the following property: $$$$$$ gcd (1 cdot p_1, , 2 cdot p_2, , dots, , n cdot p_n) > 1, $$$$$$ where $$$gcd$$$ is the and $$$[1,3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^3$$$) β the number of test cases. Each test case consists of one line containing one integer $$$n$$$ ($$$1 le n le 10^3$$$). Output For each test case, print one integer β number of beautiful permutations. Because the answer can be very big, please print the answer modulo $$$998,244,353$$$. Example Output 0 1 0 4 0 36 665702330 Note In first test case, we only have one permutation which is $$$[1]$$$ but it is not beautiful because $$$gcd(1 cdot 1) = 1$$$. In second test case, we only have one beautiful permutation which is $$$[2, 1]$$$ because $$$gcd(1 cdot 2, 2 cdot 1) = 2$$$. | 800 | true | false | false | false | false | false | false | false | false | false | 2,338 |
1975H | 378QAQ has a string $$$s$$$ of length $$$n$$$. Define the core of a string as the substring$$$^dagger$$$ with maximum lexicographic$$$^ddagger$$$ order. For example, the core of "$$$mathtt{bazoka}$$$" is "$$$mathtt{zoka}$$$", and the core of "$$$mathtt{aaa}$$$" is "$$$mathtt{aaa}$$$". 378QAQ wants to rearrange the string $$$s$$$ so that the core is lexicographically minimum. Find the lexicographically minimum possible core over all rearrangements of $$$s$$$. $$$^dagger$$$ A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, "$$$mathtt{defor}$$$", "$$$mathtt{code}$$$" and "$$$mathtt{o}$$$" are all substrings of "$$$mathtt{codeforces}$$$" while "$$$mathtt{codes}$$$" and "$$$mathtt{aaa}$$$" are not. $$$^ddagger$$$ A string $$$p$$$ is lexicographically smaller than a string $$$q$$$ if and only if one of the following holds: $$$p$$$ is a prefix of $$$q$$$, but $$$p e q$$$; or in the first position where $$$p$$$ and $$$q$$$ differ, the string $$$p$$$ has a smaller element than the corresponding element in $$$q$$$ (when compared by their ASCII code). For example, "$$$mathtt{code}$$$" and "$$$mathtt{coda}$$$" are both lexicographically smaller than "$$$mathtt{codeforces}$$$" while "$$$mathtt{codeforceston}$$$" and "$$$mathtt{z}$$$" are not. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1leq tleq 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1leq nleq 10^6$$$)xa0β the length of string $$$s$$$. The next line of each test case contains the string $$$s$$$ of length $$$n$$$. The string $$$s$$$ consists of lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output the lexicographically minimum possible core over all rearrangements of $$$s$$$. Example Input 6 3 qaq 4 cccc 6 bazoka 6 zazzzz 7 ababbbb 7 ccbabcc Output qaq cccc z zzz bbababb cbcacbc Note In the first test case, all possible rearrangements and their corresponding cores are as follows: "$$$mathtt{qaq}$$$", its core is "$$$mathtt{qaq}$$$". "$$$mathtt{aqq}$$$", its core is "$$$mathtt{qq}$$$". "$$$mathtt{qqa}$$$", its core is "$$$mathtt{qqa}$$$". So the core with the minimum lexicographic order in all rearrangement plans is "$$$mathtt{qaq}$$$". | 3,500 | false | true | false | false | false | false | false | false | false | false | 435 |
1882C | There are $$$n$$$ cards stacked in a deck. Initially, $$$a_{i}$$$ is written on the $$$i$$$-th card from the top. The value written on a card does not change. You will play a game. Initially your score is $$$0$$$. In each turn, you can do one of the following operations: Choose an odd$$$^{dagger}$$$ positive integer $$$i$$$, which is not greater than the number of cards left in the deck. Remove the $$$i$$$-th card from the top of the deck and add the number written on the card to your score. The remaining cards will be reindexed starting from the top. Choose an even$$$^{ddagger}$$$ positive integer $$$i$$$, which is not greater than the number of cards left in the deck. Remove the $$$i$$$-th card from the top of the deck. The remaining cards will be reindexed starting from the top. End the game. You can end the game whenever you want, you do not have to remove all cards from the initial deck. What is the maximum score you can get when the game ends? $$$^{dagger}$$$ An integer $$$i$$$ is odd, if there exists an integer $$$k$$$ such that $$$i = 2k + 1$$$. $$$^{ddagger}$$$ An integer $$$i$$$ is even, if there exists an integer $$$k$$$ such that $$$i = 2k$$$. 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 a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^{5}$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^{9} le a_i le 10^{9}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^{5}$$$. Output For each test case, print a single integerxa0β the maximum score you can get when the game ends. Example Input 4 4 -4 1 -3 5 4 1 -2 3 -4 3 -1 3 -5 1 -1 Note In the first test case, one can get the score of $$$5$$$ as follows: 1. In the first turn, choose $$$i=2$$$. Your score remains $$$0$$$ and the numbers written on the cards from the top will become $$$[-4, -3, 5]$$$. 2. In the second turn, choose $$$i=3$$$. Your score will become $$$5$$$ and the numbers written on the cards from the top will become $$$[-4, -3]$$$. 3. In the third turn, end the game with the score of $$$5$$$. In the second test case, one can get the score of $$$4$$$ as follows: 1. In the first turn, choose $$$i=3$$$. Your score will become $$$3$$$ and the numbers written on the cards from the top will become $$$[1, -2, -4]$$$. 2. In the second turn, choose $$$i=1$$$. Your score will become $$$4$$$ and the numbers written on the cards from the top will become $$$[-2, -4]$$$. 3. In the third turn, end the game with the score of $$$4$$$. In the third test case, one can get the score of $$$2$$$ as follows: 1. In the first turn, choose $$$i=1$$$. Your score will become $$$-1$$$ and the numbers written on the cards from the top will become $$$[3, -5]$$$. 2. In the second turn, choose $$$i=1$$$. Your score will become $$$2$$$ and the numbers written on the cards from the top will become $$$[-5]$$$. 3. In the third turn, end the game with the score of $$$2$$$. | 1,500 | false | true | false | false | false | false | true | false | false | false | 995 |
959B | Mahmoud wants to send a message to his friend Ehab. Their language consists of _n_ words numbered from 1 to _n_. Some words have the same meaning so there are _k_ groups of words such that all the words in some group have the same meaning. Mahmoud knows that the _i_-th word can be sent with cost _a__i_. For each word in his message, Mahmoud can either replace it with another word of the same meaning or leave it as it is. Can you help Mahmoud determine the minimum cost of sending the message? The cost of sending the message is the sum of the costs of sending every word in it. Input The first line of input contains integers _n_, _k_ and _m_ (1u2009β€u2009_k_u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_m_u2009β€u2009105)xa0β the number of words in their language, the number of groups of words, and the number of words in Mahmoud's message respectively. The second line contains _n_ strings consisting of lowercase English letters of length not exceeding 20 which represent the words. It's guaranteed that the words are distinct. The third line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009β€u2009_a__i_u2009β€u2009109) where _a__i_ is the cost of sending the _i_-th word. The next _k_ lines describe the groups of words of same meaning. The next _k_ lines each start with an integer _x_ (1u2009β€u2009_x_u2009β€u2009_n_) which means that there are _x_ words in this group, followed by _x_ integers which represent the indices of words in this group. It's guaranteed that each word appears in exactly one group. The next line contains _m_ space-separated words which represent Mahmoud's message. Each of these words appears in the list of language's words. Output The only line should contain the minimum cost to send the message after replacing some words (maybe none) with some words of the same meaning. Examples Input 5 4 4 i loser am the second 100 1 1 5 10 1 1 1 3 2 2 5 1 4 i am the second Input 5 4 4 i loser am the second 100 20 1 5 10 1 1 1 3 2 2 5 1 4 i am the second Note In the first sample, Mahmoud should replace the word "second" with the word "loser" because it has less cost so the cost will be 100+1+5+1=107. In the second sample, Mahmoud shouldn't do any replacement so the cost will be 100+1+5+10=116. | 1,200 | false | true | true | false | false | false | false | false | false | false | 5,876 |
859G | _n_ evenly spaced points have been marked around the edge of a circle. There is a number written at each point. You choose a positive real number _k_. Then you may repeatedly select a set of 2 or more points which are evenly spaced, and either increase all numbers at points in the set by _k_ or decrease all numbers at points in the set by _k_. You would like to eventually end up with all numbers equal to 0. Is it possible? A set of 2 points is considered evenly spaced if they are diametrically opposed, and a set of 3 or more points is considered evenly spaced if they form a regular polygon. Input The first line of input contains an integer _n_ (3u2009β€u2009_n_u2009β€u2009100000), the number of points along the circle. The following line contains a string _s_ with exactly _n_ digits, indicating the numbers initially present at each of the points, in clockwise order. Output Print "YES" (without quotes) if there is some sequence of operations that results in all numbers being 0, otherwise "NO" (without quotes). You can print each letter in any case (upper or lower). Examples Input 30 000100000100000110000000001100 Note If we label the points from 1 to _n_, then for the first test case we can set _k_u2009=u20091. Then we increase the numbers at points 7 and 22 by 1, then decrease the numbers at points 7, 17, and 27 by 1, then decrease the numbers at points 4, 10, 16, 22, and 28 by 1. | 3,000 | true | false | false | false | false | false | false | false | false | false | 6,302 |
873A | Luba has to do _n_ chores today. _i_-th chore takes _a__i_ units of time to complete. It is guaranteed that for every the condition _a__i_u2009β₯u2009_a__i_u2009-u20091 is met, so the sequence is sorted. Also Luba can work really hard on some chores. She can choose not more than _k_ any chores and do each of them in _x_ units of time instead of _a__i_ (). Luba is very responsible, so she has to do all _n_ chores, and now she wants to know the minimum time she needs to do everything. Luba cannot do two chores simultaneously. Input The first line contains three integers _n_,u2009_k_,u2009_x_xa0(1u2009β€u2009_k_u2009β€u2009_n_u2009β€u2009100,u20091u2009β€u2009_x_u2009β€u200999) β the number of chores Luba has to do, the number of chores she can do in _x_ units of time, and the number _x_ itself. The second line contains _n_ integer numbers _a__i_xa0(2u2009β€u2009_a__i_u2009β€u2009100) β the time Luba has to spend to do _i_-th chore. It is guaranteed that , and for each _a__i_u2009β₯u2009_a__i_u2009-u20091. Output Print one number β minimum time Luba needs to do all _n_ chores. Examples Input 5 2 1 100 100 100 100 100 Note In the first example the best option would be to do the third and the fourth chore, spending _x_u2009=u20092 time on each instead of _a_3 and _a_4, respectively. Then the answer is 3u2009+u20096u2009+u20092u2009+u20092u2009=u200913. In the second example Luba can choose any two chores to spend _x_ time on them instead of _a__i_. So the answer is 100Β·3u2009+u20092Β·1u2009=u2009302. | 800 | false | false | true | false | false | false | false | false | false | false | 6,254 |
358A | Dima and Seryozha live in an ordinary dormitory room for two. One day Dima had a date with his girl and he asked Seryozha to leave the room. As a compensation, Seryozha made Dima do his homework. The teacher gave Seryozha the coordinates of _n_ distinct points on the abscissa axis and asked to consecutively connect them by semi-circus in a certain order: first connect the first point with the second one, then connect the second point with the third one, then the third one with the fourth one and so on to the _n_-th point. Two points with coordinates (_x_1,u20090) and (_x_2,u20090) should be connected by a semi-circle that passes above the abscissa axis with the diameter that coincides with the segment between points. Seryozha needs to find out if the line on the picture intersects itself. For clarifications, see the picture Seryozha showed to Dima (the left picture has self-intersections, the right picture doesn't have any). Seryozha is not a small boy, so the coordinates of the points can be rather large. Help Dima cope with the problem. Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009103). The second line contains _n_ distinct integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (u2009-u2009106u2009β€u2009_x__i_u2009β€u2009106) β the _i_-th point has coordinates (_x__i_,u20090). The points are not necessarily sorted by their _x_ coordinate. Output In the single line print "yes" (without the quotes), if the line has self-intersections. Otherwise, print "no" (without the quotes). Note The first test from the statement is on the picture to the left, the second test is on the picture to the right. | 1,400 | false | false | true | false | false | false | true | false | false | false | 8,404 |
915E | This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is very soon, but, unfortunately, Alex still hasn't attended a single lesson! Since Alex doesn't want to get expelled, he wants to know the number of working days left until the end of the term, so he can attend physical education lessons during these days. But in BSU calculating the number of working days is a complicated matter: There are _n_ days left before the end of the term (numbered from 1 to _n_), and initially all of them are working days. Then the university staff sequentially publishes _q_ orders, one after another. Each order is characterised by three numbers _l_, _r_ and _k_: If _k_u2009=u20091, then all days from _l_ to _r_ (inclusive) become non-working days. If some of these days are made working days by some previous order, then these days still become non-working days; If _k_u2009=u20092, then all days from _l_ to _r_ (inclusive) become working days. If some of these days are made non-working days by some previous order, then these days still become working days. Help Alex to determine the number of working days left after each order! Input The first line contains one integer _n_, and the second line β one integer _q_ (1u2009β€u2009_n_u2009β€u2009109, 1u2009β€u2009_q_u2009β€u20093Β·105) β the number of days left before the end of the term, and the number of orders, respectively. Then _q_ lines follow, _i_-th line containing three integers _l__i_, _r__i_ and _k__i_ representing _i_-th order (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_, 1u2009β€u2009_k__i_u2009β€u20092). Output Print _q_ integers. _i_-th of them must be equal to the number of working days left until the end of the term after the first _i_ orders are published. Example Input 4 6 1 2 1 3 4 1 2 3 2 1 3 2 2 4 1 1 4 2 | 2,300 | false | false | true | false | true | false | false | false | true | false | 6,071 |
234B | Vasya is going to the Olympics in the city Ntown by train. The boy wants to read the textbook to prepare for the Olympics. He counted that he needed _k_ hours for this. He also found that the light in the train changes every hour. The light is measured on a scale from 0 to 100, where 0 is very dark, and 100 is very light. Vasya has a train lighting schedule for all _n_ hours of the trip β _n_ numbers from 0 to 100 each (the light level in the first hour, the second hour and so on). During each of those hours he will either read the whole time, or not read at all. He wants to choose _k_ hours to read a book, not necessarily consecutive, so that the minimum level of light among the selected hours were maximum. Vasya is very excited before the upcoming contest, help him choose reading hours. Input The first input line contains two integers _n_ and _k_ (1u2009β€u2009_n_u2009β€u20091000,u20091u2009β€u2009_k_u2009β€u2009_n_) β the number of hours on the train and the number of hours to read, correspondingly. The second line contains _n_ space-separated integers _a__i_ (0u2009β€u2009_a__i_u2009β€u2009100), _a__i_ is the light level at the _i_-th hour. Output In the first output line print the minimum light level Vasya will read at. In the second line print _k_ distinct space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__k_, β the indexes of hours Vasya will read at (1u2009β€u2009_b__i_u2009β€u2009_n_). The hours are indexed starting from 1. If there are multiple optimal solutions, print any of them. Print the numbers _b__i_ in an arbitrary order. Examples Input 6 5 90 20 35 40 60 100 Note In the first sample Vasya should read at the first hour (light 20), third hour (light 30) and at the fourth hour (light 40). The minimum light Vasya will have to read at is 20. | 1,000 | false | false | false | false | false | false | false | false | true | false | 8,903 |
1834E | You are given an array $$$a$$$ of length $$$n$$$. A positive integer $$$x$$$ is called good if it is impossible to find a subsegment$$$^{dagger}$$$ of the array such that the β the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 3 cdot 10^5$$$) β the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots , a_n$$$ ($$$1 leq a_i leq 10^9$$$) β the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Note In the first test case, $$$4$$$ is a good integer, and it is the smallest one, since the integers $$$1,2,3$$$ appear in the array, which means that there are subsegments of the array of length $$$1$$$ with least common multiples of $$$1,2,3$$$. However, it is impossible to find a subsegment of the array with a least common multiple equal to $$$4$$$. In the second test case, $$$7$$$ is a good integer. The integers $$$1,2,3,4,5$$$ appear explicitly in the array, and the integer $$$6$$$ is the least common multiple of the subsegments $$$[2, 3]$$$ and $$$[1, 3]$$$. In the third test case, $$$1$$$ is a good integer, since the least common multiples for the integer in the subsegments $$$[1, 1], [1, 2], [2, 2]$$$ are $$$2,6,3$$$, respectively. | 2,300 | true | false | true | false | true | false | false | true | false | false | 1,280 |
865E | Copying large hexadecimal (base 16) strings by hand can be error prone, but that doesn't stop people from doing it. You've discovered a bug in the code that was likely caused by someone making a mistake when copying such a string. You suspect that whoever copied the string did not change any of the digits in the string, nor the length of the string, but may have permuted the digits arbitrarily. For example, if the original string was 0_abc_ they may have changed it to _a_0_cb_ or 0_bca_, but not _abc_ or 0_abb_. Unfortunately you don't have access to the original string nor the copied string, but you do know the length of the strings and their numerical absolute difference. You will be given this difference as a hexadecimal string _S_, which has been zero-extended to be equal in length to the original and copied strings. Determine the smallest possible numerical value of the original string. Input Input will contain a hexadecimal string _S_ consisting only of digits 0 to 9 and lowercase English letters from _a_ to _f_, with length at most 14. At least one of the characters is non-zero. Output If it is not possible, print "NO" (without quotes). Otherwise, print the lowercase hexadecimal string corresponding to the smallest possible numerical value, including any necessary leading zeros for the length to be correct. Note The numerical value of a hexadecimal string is computed by multiplying each digit by successive powers of 16, starting with the rightmost digit, which is multiplied by 160. Hexadecimal digits representing values greater than 9 are represented by letters: _a_u2009=u200910,u2009_b_u2009=u200911,u2009_c_u2009=u200912,u2009_d_u2009=u200913,u2009_e_u2009=u200914,u2009_f_u2009=u200915. For example, the numerical value of 0_f_1_e_ is 0Β·163u2009+u200915Β·162u2009+u20091Β·161u2009+u200914Β·160u2009=u20093870, the numerical value of 00_f_1 is 0Β·163u2009+u20090Β·162u2009+u200915Β·161u2009+u20091Β·160u2009=u2009241, and the numerical value of 100_f_ is 1Β·163u2009+u20090Β·162u2009+u20090Β·161u2009+u200915Β·160u2009=u20094111. Since 3870u2009+u2009241u2009=u20094111 and 00_f_1 is a permutation of 100_f_, 00_f_1 is a valid answer to the second test case. | 3,300 | false | false | false | true | false | false | true | false | false | true | 6,277 |
1701A | There is a field of size $$$2 imes 2$$$. Each cell of this field can either contain grass or be empty. The value $$$a_{i, j}$$$ is $$$1$$$ if the cell $$$(i, j)$$$ contains grass, or $$$0$$$ otherwise. In one move, you can choose one row and one column and cut all the grass in this row and this column. In other words, you choose the row $$$x$$$ and the column $$$y$$$, then you cut the grass in all cells $$$a_{x, i}$$$ and all cells $$$a_{i, y}$$$ for all $$$i$$$ from $$$1$$$ to $$$2$$$. After you cut the grass from a cell, it becomes empty (i.u2009e. its value is replaced by $$$0$$$). Your task is to find the minimum number of moves required to cut the grass in all non-empty cells of the field (i.u2009e. make all $$$a_{i, j}$$$ zeros). You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 16$$$) β the number of test cases. Then $$$t$$$ test cases follow. The test case consists of two lines, each of these lines contains two integers. The $$$j$$$-th integer in the $$$i$$$-th row is $$$a_{i, j}$$$. If $$$a_{i, j} = 0$$$ then the cell $$$(i, j)$$$ is empty, and if $$$a_{i, j} = 1$$$ the cell $$$(i, j)$$$ contains grass. Output For each test case, print one integer β the minimum number of moves required to cut the grass in all non-empty cells of the field (i.u2009e. make all $$$a_{i, j}$$$ zeros) in the corresponding test case. | 800 | false | false | true | false | false | false | false | false | false | false | 2,076 |
1278E | We had a really tough time generating tests for problem D. In order to prepare strong tests, we had to solve the following problem. Given an undirected labeled tree consisting of $$$n$$$ vertices, find a set of segments such that: 1. both endpoints of each segment are integers from $$$1$$$ to $$$2n$$$, and each integer from $$$1$$$ to $$$2n$$$ should appear as an endpoint of exactly one segment; 2. all segments are non-degenerate; 3. for each pair $$$(i, j)$$$ such that $$$i e j$$$, $$$i in [1, n]$$$ and $$$j in [1, n]$$$, the vertices $$$i$$$ and $$$j$$$ are connected with an edge if and only if the segments $$$i$$$ and $$$j$$$ intersect, but neither segment $$$i$$$ is fully contained in segment $$$j$$$, nor segment $$$j$$$ is fully contained in segment $$$i$$$. Can you solve this problem too? Input The first line contains one integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$) β the number of vertices in the tree. Then $$$n - 1$$$ lines follow, each containing two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$) denoting the endpoints of the $$$i$$$-th edge. It is guaranteed that the given graph is a tree. Output Print $$$n$$$ pairs of integers, the $$$i$$$-th pair should contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i < r_i le 2n$$$) β the endpoints of the $$$i$$$-th segment. All $$$2n$$$ integers you print should be unique. It is guaranteed that the answer always exists. Examples Input 6 1 2 1 3 3 4 3 5 2 6 Output 9 12 7 10 3 11 1 5 2 4 6 8 | 2,200 | false | false | false | false | false | true | false | false | false | false | 4,302 |
1354A | Polycarp has spent the entire day preparing problems for you. Now he has to sleep for at least $$$a$$$ minutes to feel refreshed. Polycarp can only wake up by hearing the sound of his alarm. So he has just fallen asleep and his first alarm goes off in $$$b$$$ minutes. Every time Polycarp wakes up, he decides if he wants to sleep for some more time or not. If he's slept for less than $$$a$$$ minutes in total, then he sets his alarm to go off in $$$c$$$ minutes after it is reset and spends $$$d$$$ minutes to fall asleep again. Otherwise, he gets out of his bed and proceeds with the day. If the alarm goes off while Polycarp is falling asleep, then he resets his alarm to go off in another $$$c$$$ minutes and tries to fall asleep for $$$d$$$ minutes again. You just want to find out when will Polycarp get out of his bed or report that it will never happen. Please check out the notes for some explanations of the example. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of testcases. The only line of each testcase contains four integers $$$a, b, c, d$$$ ($$$1 le a, b, c, d le 10^9$$$)xa0β the time Polycarp has to sleep for to feel refreshed, the time before the first alarm goes off, the time before every succeeding alarm goes off and the time Polycarp spends to fall asleep. Output For each test case print one integer. If Polycarp never gets out of his bed then print -1. Otherwise, print the time it takes for Polycarp to get out of his bed. Example Input 7 10 3 6 4 11 3 6 4 5 9 4 10 6 5 2 3 1 1 1 1 3947465 47342 338129 123123 234123843 13 361451236 361451000 Output 27 27 9 -1 1 6471793 358578060125049 Note In the first testcase Polycarp wakes up after $$$3$$$ minutes. He only rested for $$$3$$$ minutes out of $$$10$$$ minutes he needed. So after that he sets his alarm to go off in $$$6$$$ minutes and spends $$$4$$$ minutes falling asleep. Thus, he rests for $$$2$$$ more minutes, totaling in $$$3+2=5$$$ minutes of sleep. Then he repeats the procedure three more times and ends up with $$$11$$$ minutes of sleep. Finally, he gets out of his bed. He spent $$$3$$$ minutes before the first alarm and then reset his alarm four times. The answer is $$$3+4 cdot 6 = 27$$$. The second example is almost like the first one but Polycarp needs $$$11$$$ minutes of sleep instead of $$$10$$$. However, that changes nothing because he gets $$$11$$$ minutes with these alarm parameters anyway. In the third testcase Polycarp wakes up rested enough after the first alarm. Thus, the answer is $$$b=9$$$. In the fourth testcase Polycarp wakes up after $$$5$$$ minutes. Unfortunately, he keeps resetting his alarm infinitely being unable to rest for even a single minute :( | 900 | true | false | false | false | false | false | false | false | false | false | 3,939 |
1696F | Fishingprince loves trees. A tree is a connected undirected graph without cycles. Fishingprince has a tree of $$$n$$$ vertices. The vertices are numbered $$$1$$$ through $$$n$$$. Let $$$d(x,y)$$$ denote the shortest distance on the tree from vertex $$$x$$$ to vertex $$$y$$$, assuming that the length of each edge is $$$1$$$. However, the tree was lost in an accident. Fortunately, Fishingprince still remembers some information about the tree. More specifically, for every triple of integers $$$x,y,z$$$ ($$$1le x<yle n$$$, $$$1le zle n$$$) he remembers whether $$$d(x,z)=d(y,z)$$$ or not. Help him recover the structure of the tree, or report that no tree satisfying the constraints exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 200$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2le nle 100$$$) β the number of vertices in the tree. Then $$$n-1$$$ lines follow. The $$$i$$$-th line of these $$$n-1$$$ lines contains $$$n-i$$$ strings of length $$$n$$$ consisting of 0 and 1. If the $$$k$$$-th character in the $$$j$$$-th string of the $$$i$$$-th line is 0, it means that $$$d(i,k) e d(i+j,k)$$$; if the $$$k$$$-th character in the $$$j$$$-th string of the $$$i$$$-th line is 1, it means that $$$d(i,k)=d(i+j,k)$$$. It is guaranteed that in one input file, there are at most $$$2$$$ test cases that have $$$n>50$$$; there are at most $$$5$$$ test cases that have $$$n>20$$$. Output For each test case: if no answer exists, output No; otherwise, on the first line output Yes. Then output $$$n-1$$$ lines. Each line should contain two integers $$$x,y$$$ ($$$1le x,yle n$$$), denoting an edge between vertices $$$x$$$ and $$$y$$$ of the tree. If there are multiple solutions, print any. When printing Yes and No, you can print each letter in any case (upper or lower). Example Input 5 2 00 2 10 3 001 000 000 3 001 010 000 5 00000 01001 00000 01100 00000 10000 00000 00000 11010 00000 Output Yes 1 2 No Yes 1 3 2 3 No Yes 1 2 1 4 2 3 2 5 | 2,600 | false | false | false | false | false | true | true | false | false | true | 2,103 |
1272D | Problem - 1272D - 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 *1500 No tag edit access β Contest materials ") Editorial") β the number of elements in $$$a$$$. 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 $$$i$$$-th element of $$$a$$$. Output Print one integer β the maximum possible length of the strictly increasing contiguous subarray of the array $$$a$$$ after removing at most one element. Examples Input 5 1 2 5 3 4 Output 4 Input 2 1 2 Output 2 Input 7 6 5 4 3 2 4 3 Output 2 Note In the first example, you can delete $$$a_3=5$$$. Then the resulting array will be equal to $$$ | 1,500 | false | false | false | true | false | false | true | false | false | false | 4,326 |
1826C | Because to take away a man's freedom of choice, even his freedom to make the wrong choice, is to manipulate him as though he were a puppet and not a person. β Madeleine L'Engle There are $$$n$$$ programmers choosing their favorite algorithm amongst $$$m$$$ different choice options. Before the first round, all $$$m$$$ options are available. In each round, every programmer makes a vote for one of the remaining algorithms. After the round, only the algorithms with the maximum number of votes remain. The voting process ends when there is only one option left. Determine whether the voting process can continue indefinitely or no matter how people vote, they will eventually choose a single option after some finite amount of rounds? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) β the number of test cases. Each test case consists of a single line containing two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^6$$$) β the number of people and choice options respectively. Output For each test case output "YES" if the programmers will eventually choose a single option, and "NO" otherwise. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as a positive answer). Example Input 5 3 2 4 2 5 3 1000000 1000000 1 1000000 Note In the first example, there are $$$8$$$ ways people could vote: $$${111, 112, 121, 122, 211, 212, 221, 222}$$$. In cases $$$1$$$, $$$2$$$, $$$3$$$, and $$$5$$$, the programmers are left with the first algorithm, and in the remaining cases people are left with the second one, so the voting ends in one round in any case. In the second example, the programmers could always vote $$$1122$$$. Both algorithms have the maximum number of votes and remain for the next round, so the voting never ends. | 1,300 | true | true | false | false | false | false | false | false | false | false | 1,327 |
1660A | Vasya decided to go to the grocery store. He found in his wallet $$$a$$$ coins of $$$1$$$ burle and $$$b$$$ coins of $$$2$$$ burles. He does not yet know the total cost of all goods, so help him find out $$$s$$$ ($$$s > 0$$$): the minimum positive integer amount of money he cannot pay without change or pay at all using only his coins. For example, if $$$a=1$$$ and $$$b=1$$$ (he has one $$$1$$$-burle coin and one $$$2$$$-burle coin), then: he can pay $$$1$$$ burle without change, paying with one $$$1$$$-burle coin, he can pay $$$2$$$ burle without change, paying with one $$$2$$$-burle coin, he can pay $$$3$$$ burle without change by paying with one $$$1$$$-burle coin and one $$$2$$$-burle coin, he cannot pay $$$4$$$ burle without change (moreover, he cannot pay this amount at all). So for $$$a=1$$$ and $$$b=1$$$ the answer is $$$s=4$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases in the test. The description of each test case consists of one line containing two integers $$$a_i$$$ and $$$b_i$$$ ($$$0 le a_i, b_i le 10^8$$$)xa0β the number of $$$1$$$-burle coins and $$$2$$$-burles coins Vasya has respectively. Output For each test case, on a separate line print one integer $$$s$$$ ($$$s > 0$$$): the minimum positive integer amount of money that Vasya cannot pay without change or pay at all. Example Input 5 1 1 4 0 0 2 0 0 2314 2374 Note The first test case of the example is clarified into the main part of the statement. In the second test case, Vasya has only $$$1$$$ burle coins, and he can collect either any amount from $$$1$$$ to $$$4$$$, but $$$5$$$ can't. In the second test case, Vasya has only $$$2$$$ burle coins, and he cannot pay $$$1$$$ burle without change. In the fourth test case you don't have any coins, and he can't even pay $$$1$$$ burle. | 800 | true | true | false | false | false | false | false | false | false | false | 2,326 |
1552F | An ant moves on the real line with constant speed of $$$1$$$ unit per second. It starts at $$$0$$$ and always moves to the right (so its position increases by $$$1$$$ each second). There are $$$n$$$ portals, the $$$i$$$-th of which is located at position $$$x_i$$$ and teleports to position $$$y_i < x_i$$$. Each portal can be either active or inactive. The initial state of the $$$i$$$-th portal is determined by $$$s_i$$$: if $$$s_i=0$$$ then the $$$i$$$-th portal is initially inactive, if $$$s_i=1$$$ then the $$$i$$$-th portal is initially active. When the ant travels through a portal (i.e., when its position coincides with the position of a portal): if the portal is inactive, it becomes active (in this case the path of the ant is not affected); if the portal is active, it becomes inactive and the ant is instantly teleported to the position $$$y_i$$$, where it keeps on moving as normal. How long (from the instant it starts moving) does it take for the ant to reach the position $$$x_n + 1$$$? It can be shown that this happens in a finite amount of time. Since the answer may be very large, compute it modulo $$$998,244,353$$$. Input The first line contains the integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$) β the number of portals. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$s_i$$$ ($$$1le y_i < x_ile 10^9$$$, $$$s_iin{0,1}$$$) β the position of the $$$i$$$-th portal, the position where the ant is teleported when it travels through the $$$i$$$-th portal (if it is active), and the initial state of the $$$i$$$-th portal. The positions of the portals are strictly increasing, that is $$$x_1<x_2<cdots<x_n$$$. It is guaranteed that the $$$2n$$$ integers $$$x_1, , x_2, , dots, , x_n, , y_1, , y_2, , dots, , y_n$$$ are all distinct. Output Output the amount of time elapsed, in seconds, from the instant the ant starts moving to the instant it reaches the position $$$x_n+1$$$. Since the answer may be very large, output it modulo $$$998,244,353$$$. Examples Input 4 3 2 0 6 5 1 7 4 0 8 1 1 Input 1 454971987 406874902 1 Input 5 243385510 42245605 0 644426565 574769163 0 708622105 208990040 0 786625660 616437691 0 899754846 382774619 0 Input 5 200000000 100000000 1 600000000 400000000 0 800000000 300000000 0 900000000 700000000 1 1000000000 500000000 0 Note Explanation of the first sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed $$$1$$$ and the time spent during the movement is written above the arrow). $$$$$$ 0 stackrel{6}{longrightarrow} 6 leadsto 5 stackrel{3}{longrightarrow} 8 leadsto 1 stackrel{2}{longrightarrow} 3 leadsto 2 stackrel{4}{longrightarrow} 6 leadsto 5 stackrel{2}{longrightarrow} 7 leadsto 4 stackrel{2}{longrightarrow} 6 leadsto 5 stackrel{4}{longrightarrow} 9 $$$$$$ Notice that the total time is $$$6+3+2+4+2+2+4=23$$$. Explanation of the second sample: The ant moves as follows (a curvy arrow denotes a teleporting, a straight arrow denotes normal movement with speed $$$1$$$ and the time spent during the movement is written above the arrow). $$$$$$ 0 stackrel{454971987}{longrightarrow} 454971987 leadsto 406874902 stackrel{48097086}{longrightarrow} 454971988 $$$$$$ Notice that the total time is $$$454971987+48097086=503069073$$$. Explanation of the third sample: Since all portals are initially off, the ant will not be teleported and will go straight from $$$0$$$ to $$$x_n+1=899754846+1=899754847$$$. | 2,200 | false | false | false | true | true | false | false | true | true | false | 2,879 |
1660C | A string $$$a=a_1a_2dots a_n$$$ is called even if it consists of a concatenation (joining) of strings of length $$$2$$$ consisting of the same characters. In other words, a string $$$a$$$ is even if two conditions are satisfied at the same time: its length $$$n$$$ is even; for all odd $$$i$$$ ($$$1 le i le n - 1$$$), $$$a_i = a_{i+1}$$$ is satisfied. For example, the following strings are even: "" (empty string), "tt", "aabb", "oooo", and "ttrrrroouuuuuuuukk". The following strings are not even: "aaa", "abab" and "abba". Given a string $$$s$$$ consisting of lowercase Latin letters. Find the minimum number of characters to remove from the string $$$s$$$ to make it even. The deleted characters do not have to be consecutive. Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0βthe number of test cases in the test. The descriptions of the test cases follow. Each test case consists of one string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$), where $$$s$$$xa0β the length of the string $$$s$$$. The string consists of lowercase Latin letters. It is guaranteed that the sum of $$$s$$$ on all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single numberxa0β the minimum number of characters that must be removed to make $$$s$$$ even. Example Input 6 aabbdabdccc zyx aaababbb aabbcc oaoaaaoo bmefbmuyw Note In the first test case you can remove the characters with indices $$$6$$$, $$$7$$$, and $$$9$$$ to get an even string "aabbddcc". In the second test case, each character occurs exactly once, so in order to get an even string, you must remove all characters from the string. In the third test case, you can get an even string "aaaabb" by removing, for example, $$$4$$$-th and $$$6$$$-th characters, or a string "aabbbb" by removing the $$$5$$$-th character and any of the first three. | 1,300 | false | true | false | true | false | false | false | false | false | false | 2,324 |
1137E | Vasya likes to travel by train, but doesn't like when the car he travels in is located in the tail of the train. Vasya gets on the train at the station. The train consists of $$$n$$$ cars indexed from $$$1$$$ to $$$n$$$ counting from the locomotive (head of the train). Three types of events occur while the train is moving: 1. Some number of cars are added to the head of the train; 2. Some number of cars are added to the tail of the train; 3. Vasya recalculates the values of the convenience of the cars (read more about it below). At each moment of time we will index the cars from the head of the train, starting from $$$1$$$. Note that when adding new cars to the head of the train, the indexing of the old ones may shift. To choose which car to go in, Vasya will use the value $$$A_i$$$ for each car (where $$$i$$$ is a car index), which is calculated as follows: At the beginning of the trip $$$A_i=0$$$, as well as for the new cars at the time of their addition. During the next recalculation Vasya chooses some positive integers $$$b$$$ and $$$s$$$ and adds to all $$$A_i$$$ value $$$b + (i - 1) cdot s$$$. Vasya hasn't decided yet where he will get on the train and where will get off the train, so after each event of one of the three types he wants to know the least index of the car, such that its value $$$A_i$$$ is minimal. Since there is a lot of cars, Vasya asked you to write a program that answers his question. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 10^9$$$, $$$1 leq m leq 300,000$$$), the number of cars in the train at the time of departure from the station and the number of stations, respectively. Next $$$m$$$ lines contain the descriptions of events. Each event is one of the following three types: "$$$1$$$ $$$k$$$" ($$$1 le k le 10^9$$$), add $$$k$$$ cars to the head of the train "$$$2$$$ $$$k$$$" ($$$1 le k le 10^9$$$), add $$$k$$$ cars to the tail of the train "$$$3$$$ $$$b$$$ $$$s$$$" ($$$1 le b, s le 10^9$$$), recalculate the convenience of all train cars. It is guaranteed that at any time the train length does not exceed $$$10^9$$$. Also it's guaranteed that the integers $$$A_i$$$ will not grow too high. Formally, it's guaranteed that if we sum the largest addition over all events of the $$$3$$$-rd type (that is, $$$b + (n - 1) cdot s$$$, where $$$n$$$ is the number of cars at that moment) then the acquired sum would be at most $$$10^{18}$$$. Output After each of the $$$m$$$ queries print two integers: $$$j$$$ and $$$A_j$$$xa0β the number of the car closest to the head of the train, such that its value $$$A_j$$$ is minimal, and the value $$$A_j$$$ itself. Example Input 1 8 1 1 3 1 1 3 1 1 2 1 2 1 3 1 1 2 1 3 1 5 Output 1 0 1 1 1 2 3 0 3 0 1 3 5 0 1 4 Note Initially the train consists of one car with $$$A_1 = 0$$$, let's denote train as $$$[0]$$$ for simplicity. After adding one car to the head, train is $$$[0, 0]$$$. After recalculation of values with parameters $$$b=1, s=1$$$, train is $$$[1, 2]$$$. After another recalculation of values with the parameters $$$b=1, s=1$$$, train is $$$[2, 4]$$$. After adding one car to the end, train is $$$[2, 4, 0]$$$. After another adding one car to the end, train is $$$[2, 4, 0, 0]$$$. After recalculation of values with parameters $$$b=1$$$, $$$s=1$$$, train is $$$[3, 6, 3, 4]$$$. After adding one car to the end, train is $$$[3, 6, 3, 4, 0]$$$. After recalculation of values with parameters $$$b=1$$$, $$$s=5$$$, train is $$$[4, 12, 14, 20, 21]$$$. | 2,700 | false | true | false | false | true | false | false | false | false | false | 5,038 |
1027E | You are given a square board, consisting of $$$n$$$ rows and $$$n$$$ columns. Each tile in it should be colored either white or black. Let's call some coloring beautiful if each pair of adjacent rows are either the same or different in every position. The same condition should be held for the columns as well. Let's call some coloring suitable if it is beautiful and there is no rectangle of the single color, consisting of at least $$$k$$$ tiles. Your task is to count the number of suitable colorings of the board of the given size. Since the answer can be very large, print it modulo $$$998244353$$$. Input A single line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 500$$$, $$$1 le k le n^2$$$) β the number of rows and columns of the board and the maximum number of tiles inside the rectangle of the single color, respectively. Output Print a single integer β the number of suitable colorings of the board of the given size modulo $$$998244353$$$. Note Board of size $$$1 imes 1$$$ is either a single black tile or a single white tile. Both of them include a rectangle of a single color, consisting of $$$1$$$ tile. Here are the beautiful colorings of a board of size $$$2 imes 2$$$ that don't include rectangles of a single color, consisting of at least $$$3$$$ tiles: The rest of beautiful colorings of a board of size $$$2 imes 2$$$ are the following: | 2,100 | true | false | false | true | false | false | false | false | false | false | 5,572 |
813F | Problem - 813F - Codeforces =============== xa0 . Input The first line contains two integers _n_ and _q_ (2u2009β€u2009_n_,u2009_q_u2009β€u2009100000). Then _q_ lines follow. _i_th line contains two numbers _x__i_ and _y__i_ (1u2009β€u2009_x__i_u2009<u2009_y__i_u2009β€u2009_n_). These numbers describe _i_th query: if there is an edge between vertices _x__i_ and _y__i_, then remove it, otherwise add it. Output Print _q_ lines. _i_th line must contain YES if the graph is bipartite after _i_th query, and NO otherwise. Example Input 3 5 2 3 1 3 1 2 1 2 1 2 Output YES YES NO YES NO | 2,500 | false | false | false | false | true | false | false | false | false | true | 6,506 |
671B | We all know the impressive story of Robin Hood. Robin Hood uses his archery skills and his wits to steal the money from rich, and return it to the poor. There are _n_ citizens in Kekoland, each person has _c__i_ coins. Each day, Robin Hood will take exactly 1 coin from the richest person in the city and he will give it to the poorest person (poorest person right after taking richest's 1 coin). In case the choice is not unique, he will select one among them at random. Sadly, Robin Hood is old and want to retire in _k_ days. He decided to spend these last days with helping poor people. After taking his money are taken by Robin Hood richest person may become poorest person as well, and it might even happen that Robin Hood will give his money back. For example if all people have same number of coins, then next day they will have same number of coins too. Your task is to find the difference between richest and poorest persons wealth after _k_ days. Note that the choosing at random among richest and poorest doesn't affect the answer. Input The first line of the input contains two integers _n_ and _k_ (1u2009β€u2009_n_u2009β€u2009500u2009000,u20090u2009β€u2009_k_u2009β€u2009109)xa0β the number of citizens in Kekoland and the number of days left till Robin Hood's retirement. The second line contains _n_ integers, the _i_-th of them is _c__i_ (1u2009β€u2009_c__i_u2009β€u2009109)xa0β initial wealth of the _i_-th person. Output Print a single line containing the difference between richest and poorest peoples wealth. Note Lets look at how wealth changes through day in the first sample. 1. [1,u20091,u20094,u20092] 2. [2,u20091,u20093,u20092] or [1,u20092,u20093,u20092] So the answer is 3u2009-u20091u2009=u20092 In second sample wealth will remain the same for each person. | 2,000 | false | true | false | false | false | false | false | true | false | false | 7,139 |
320B | Problem - 320B - 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 dfs and similar graphs *1500 No tag edit access β Contest materials time limit per test 2 seconds memory limit per test 256 megabytes input stdin output stdout In this problem at each moment you have a set of intervals. You can move from interval (_a_,u2009_b_) from our set to interval (_c_,u2009_d_) from our set if and only if _c_u2009<u2009_a_u2009<u2009_d_ or _c_u2009<u2009_b_u2009<u2009_d_. Also there is a path from interval _I_1 from our set to interval _I_2 from our set if there is a sequence of successive moves starting from _I_1 so that we can reach _I_2. Your program should handle the queries of the following two types: 1. "1 x y" (_x_u2009<u2009_y_) β add the new interval (_x_,u2009_y_) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. 2. "2 a b" (_a_u2009β u2009_b_) β answer the question: is there a path from _a_-th (one-based) added interval to _b_-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals. Input The first line of the input contains integer _n_ denoting the number of queries, (1u2009β€u2009_n_u2009β€u2009100). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct. Output For each query of the second type print "YES" or "NO" on a separate line depending on the answer. Examples Input 5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2 Output NO YES | 1,500 | false | false | false | false | false | false | false | false | false | true | 8,552 |
571C | 'In Boolean logic, a formula is in conjunctive normal form (CNF) or clausal normal form if it is a conjunction of clauses, where a clause is a disjunction of literals' (cited from the other words, CNF is a formula of type , where & represents a logical "AND" (conjunction), represents a logical "OR" (disjunction), and _v__ij_ are some boolean variables or their negations. Each statement in brackets is called a clause, and _v__ij_ are called literals. You are given a CNF containing variables _x_1,u2009...,u2009_x__m_ and their negations. We know that each variable occurs in at most two clauses (with negation and without negation in total). Your task is to determine whether this CNF is satisfiable, that is, whether there are such values of variables where the CNF value is true. If CNF is satisfiable, then you also need to determine the values of the variables at which the CNF is true. It is guaranteed that each variable occurs at most once in each clause. Input The first line contains integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20092Β·105) β the number of clauses and the number variables, correspondingly. Next _n_ lines contain the descriptions of each clause. The _i_-th line first contains first number _k__i_ (_k__i_u2009β₯u20091) β the number of literals in the _i_-th clauses. Then follow space-separated literals _v__ij_ (1u2009β€u2009_v__ij_u2009β€u2009_m_). A literal that corresponds to _v__ij_ is _x__v__ij_ either with negation, if _v__ij_ is negative, or without negation otherwise. Output If CNF is not satisfiable, print a single line "NO" (without the quotes), otherwise print two strings: string "YES" (without the quotes), and then a string of _m_ numbers zero or one β the values of variables in satisfying assignment in the order from _x_1 to _x__m_. Examples Input 4 3 1 1 1 2 3 -1 -2 3 1 -3 Input 5 6 2 1 2 3 1 -2 3 4 -3 5 4 6 2 -6 -4 1 5 Note In the first sample test formula is . One of possible answer is _x_1u2009=u2009_TRUE_,u2009_x_2u2009=u2009_TRUE_. | 2,500 | false | true | false | false | false | true | false | false | false | true | 7,564 |
1672C | You are given an array $$$a$$$ of length $$$n$$$. We define the equality of the array as the number of indices $$$1 le i le n - 1$$$ such that $$$a_i = a_{i + 1}$$$. We are allowed to do the following operation: Select two integers $$$i$$$ and $$$x$$$ such that $$$1 le i le n - 1$$$ and $$$1 le x le 10^9$$$. Then, set $$$a_i$$$ and $$$a_{i + 1}$$$ to be equal to $$$x$$$. Find the minimum number of operations needed such that the equality of the array is less than or equal to $$$1$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 2 cdot 10 ^ 5$$$) β the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$) β elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10 ^ 5$$$ Output For each test case, print the minimum number of operations needed. Example Input 4 5 1 1 1 1 1 5 2 1 1 1 2 6 1 1 2 3 3 4 6 1 2 1 4 5 4 Note In the first test case, we can select $$$i=2$$$ and $$$x=2$$$ to form $$$[1, 2, 2, 1, 1]$$$. Then, we can select $$$i=3$$$ and $$$x=3$$$ to form $$$[1, 2, 3, 3, 1]$$$. In the second test case, we can select $$$i=3$$$ and $$$x=100$$$ to form $$$[2, 1, 100, 100, 2]$$$. | 1,100 | false | true | true | false | false | true | false | false | false | false | 2,243 |
1857E | You are given $$$n$$$ points with integer coordinates $$$x_1,dots x_n$$$, which lie on a number line. For some integer $$$s$$$, we construct segments [$$$s,x_1$$$], [$$$s,x_2$$$], $$$dots$$$, [$$$s,x_n$$$]. Note that if $$$x_i<s$$$, then the segment will look like [$$$x_i,s$$$]. The segment [$$$a, b$$$] covers all integer points $$$a, a+1, a+2, dots, b$$$. We define the power of a point $$$p$$$ as the number of segments that intersect the point with coordinate $$$p$$$, denoted as $$$f_p$$$. Your task is to compute $$$sumlimits_{p=1}^{10^9}f_p$$$ for each $$$s in {x_1,dots,x_n}$$$, i.e., the sum of $$$f_p$$$ for all integer points from $$$1$$$ to $$$10^9$$$. For example, if the initial coordinates are $$$[1,2,5,7,1]$$$ and we choose $$$s=5$$$, then the segments will be: $$$[1,5]$$$,$$$[2,5]$$$,$$$[5,5]$$$,$$$[5,7]$$$,$$$[1,5]$$$. And the powers of the points will be: $$$f_1=2, f_2=3, f_3=3, f_4=3, f_5=5, f_6=1, f_7=1, f_8=0, dots, f_{10^9}=0$$$. Their sum is $$$2+3+3+3+5+1+1=18$$$. Input The first line contains an integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2cdot 10^5$$$)xa0β the number of points. The second line contains $$$n$$$ integers $$$x_1,x_2 dots x_n$$$ ($$$1 le x_i le 10^9$$$)xa0β the coordinates of the points. It is guaranteed that the sum of the values of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Note In the first test case we first choose $$$s=x_1=1$$$, then the following segments are formed: $$$[1,1]$$$,$$$[1,4]$$$,$$$[1,3]$$$. The powers of the points will be as follows: $$$f_1=3, f_2=2, f_3=2, f_4=1, f_5=0 dots$$$ The sum of powers of the points: $$$3+2+2+1+0+dots+0=8$$$. After that we choose $$$s=x_2=4$$$. Then there will be such segments: $$$[1,4]$$$,$$$[4,4]$$$,$$$[3,4]$$$, and powers of the points are $$$f_1=1, f_2=1, f_3=2, f_4=3$$$. At the end we take $$$s=x_3=3$$$ and the segments look like this: $$$[1,3]$$$,$$$[3,4]$$$,$$$[3,3]$$$, the powers of the points are $$$f_1=1, f_2=1, f_3=3, f_4=1$$$. | 1,500 | true | false | false | false | false | false | false | false | true | false | 1,140 |
1941C | Rudolf has a string $$$s$$$ of length $$$n$$$. Rudolf considers the string $$$s$$$ to be ugly if it contains the substring$$$^dagger$$$ "pie" or the substring "map", otherwise the string $$$s$$$ will be considered beautiful. For example, "ppiee", "mmap", "dfpiefghmap" are ugly strings, while "mathp", "ppiiee" are beautiful strings. Rudolf wants to shorten the string $$$s$$$ by removing some characters to make it beautiful. The main character doesn't like to strain, so he asks you to make the string beautiful by removing the minimum number of characters. He can remove characters from any positions in the string (not just from the beginning or end of the string). $$$^dagger$$$ String $$$a$$$ is a substring of $$$b$$$ if there exists a consecutive segment of characters in string $$$b$$$ equal to $$$a$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$) β the length of the string $$$s$$$. The next line of each test case contains the string $$$s$$$ of length $$$n$$$. The string $$$s$$$ consists of lowercase Latin letters. The sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single integer β the minimum number of characters that need to be deleted to make the string $$$s$$$ beautiful. If the string is initially beautiful, then output $$$0$$$. Example Input 6 9 mmapnapie 9 azabazapi 8 mappppie 18 mapmapmapmapmapmap 1 p 11 pppiepieeee Note In the first test case, for example, you can delete the $$$4$$$th and $$$9$$$th characters to make the string beautiful. In the second test case, the string is already beautiful. | 900 | false | true | false | true | false | false | false | false | false | false | 637 |
1225C | Vasya will fancy any number as long as it is an integer power of two. Petya, on the other hand, is very conservative and only likes a single integer $$$p$$$ (which may be positive, negative, or zero). To combine their tastes, they invented $$$p$$$-binary numbers of the form $$$2^x + p$$$, where $$$x$$$ is a non-negative integer. For example, some $$$-9$$$-binary ("minus nine" binary) numbers are: $$$-8$$$ (minus eight), $$$7$$$ and $$$1015$$$ ($$$-8=2^0-9$$$, $$$7=2^4-9$$$, $$$1015=2^{10}-9$$$). The boys now use $$$p$$$-binary numbers to represent everything. They now face a problem: given a positive integer $$$n$$$, what's the smallest number of $$$p$$$-binary numbers (not necessarily distinct) they need to represent $$$n$$$ as their sum? It may be possible that representation is impossible altogether. Help them solve this problem. For example, if $$$p=0$$$ we can represent $$$7$$$ as $$$2^0 + 2^1 + 2^2$$$. And if $$$p=-9$$$ we can represent $$$7$$$ as one number $$$(2^4-9)$$$. Note that negative $$$p$$$-binary numbers are allowed to be in the sum (see the Notes section for an example). Output If it is impossible to represent $$$n$$$ as the sum of any number of $$$p$$$-binary numbers, print a single integer $$$-1$$$. Otherwise, print the smallest possible number of summands. Note $$$0$$$-binary numbers are just regular binary powers, thus in the first sample case we can represent $$$24 = (2^4 + 0) + (2^3 + 0)$$$. In the second sample case, we can represent $$$24 = (2^4 + 1) + (2^2 + 1) + (2^0 + 1)$$$. In the third sample case, we can represent $$$24 = (2^4 - 1) + (2^2 - 1) + (2^2 - 1) + (2^2 - 1)$$$. Note that repeated summands are allowed. In the fourth sample case, we can represent $$$4 = (2^4 - 7) + (2^1 - 7)$$$. Note that the second summand is negative, which is allowed. In the fifth sample case, no representation is possible. | 1,600 | true | false | false | false | false | false | true | false | false | false | 4,549 |
380C | Sereja has a bracket sequence _s_1,u2009_s_2,u2009...,u2009_s__n_, or, in other words, a string _s_ of length _n_, consisting of characters "(" and ")". Sereja needs to answer _m_ queries, each of them is described by two integers _l__i_,u2009_r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_). The answer to the _i_-th query is the length of the maximum correct bracket subsequence of sequence _s__l__i_,u2009_s__l__i_u2009+u20091,u2009...,u2009_s__r__i_. Help Sereja answer all queries. You can find the definitions for a subsequence and a correct bracket sequence in the notes. Input The first line contains a sequence of characters _s_1,u2009_s_2,u2009...,u2009_s__n_ (1u2009β€u2009_n_u2009β€u2009106) without any spaces. Each character is either a "(" or a ")". The second line contains integer _m_ (1u2009β€u2009_m_u2009β€u2009105) β the number of queries. Each of the next _m_ lines contains a pair of integers. The _i_-th line contains integers _l__i_,u2009_r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_n_) β the description of the _i_-th query. Output Print the answer to each question on a single line. Print the answers in the order they go in the input. Examples Input ())(())(())( 7 1 1 2 3 1 2 1 12 8 12 5 11 2 10 Note A subsequence of length _x_ of string _s_u2009=u2009_s_1_s_2... _s__s_ (where _s_ is the length of string _s_) is string _x_u2009=u2009_s__k_1_s__k_2... _s__k__x_ (1u2009β€u2009_k_1u2009<u2009_k_2u2009<u2009...u2009<u2009_k__x_u2009β€u2009_s_). A correct bracket sequence is a bracket sequence that can be transformed into a correct aryphmetic expression by inserting characters "1" and "+" between the characters of the string. For example, bracket sequences "()()", "(())" are correct (the resulting expressions "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. For the third query required sequence will be Β«()Β». For the fourth query required sequence will be Β«()(())(())Β». | 2,000 | false | false | false | false | true | false | false | false | false | false | 8,308 |
2030G2 | This is the hard version of the problem. In this version, $$$n leq 10^6$$$. You can only make hacks if both versions of the problem are solved. Orangutans are powerful beingsβso powerful that they only need $$$1$$$ unit of time to destroy every vulnerable planet in the universe! There are $$$n$$$ planets in the universe. Each planet has an interval of vulnerability $$$[l, r]$$$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $$$1$$$ unit. Specifically, suppose the expansion is performed on planet $$$p$$$ with interval of vulnerability $$$[l_p, r_p]$$$. Then, the resulting interval of vulnerability may be either $$$[l_p - 1, r_p]$$$ or $$$[l_p, r_p + 1]$$$. Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed. Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $$$998,244,353$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^6$$$)xa0β the number of planets in the universe. The following $$$n$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i leq r_i leq n$$$)xa0β the initial interval of vulnerability of the $$$i$$$-th planet. It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^6$$$ over all test cases. Output For each test case, output an integerxa0β the sum of scores to destroy all non-empty subsets of the planets in the universe, modulo $$$998,244,353$$$. Note In the first testcase, there are seven non-empty subsets of planets we must consider: For each of the subsets $$${[1,1]}, {[2,3]}, {[3,3]}$$$, the score is $$$0$$$. For the subset $$${[2,3], [3,3]}$$$, the score is $$$0$$$, because the point $$$3$$$ is already contained in both planets' interval of vulnerability. For the subset $$${[1,1], [2,3]}$$$, the score is $$$1$$$. By using one operation on changing the interval of vulnerability of the second planet to be $$$[1,3]$$$, the two planets now both have the point $$$1$$$ in their interval. For the subset $$${[1,1], [3,3]}$$$, the score is $$$2$$$. By using two operations on changing the interval of vulnerability of the first planet to be $$$[1,3]$$$, the two planets now both have the point $$$3$$$ in their interval. For the subset $$${[1,1], [2,3], [3,3]}$$$, the score is $$$2$$$. By using one operation on changing the interval of vulnerability of the first planet to be $$$[1,2]$$$ and one operation on changing the interval of vulnerability of the third planet to $$$[2,3]$$$, all three planets will have the point $$$2$$$ in their interval. The sum of scores of all non-empty subsets of the first testcase is $$$0 cdot 4 + 1 cdot 1 + 2cdot2 = 5$$$. | 3,100 | true | false | false | false | false | false | false | false | false | false | 81 |
388B | Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with _n_ vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2." Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly to _k_? Input The first line contains a single integer _k_ (1u2009β€u2009_k_u2009β€u2009109). Output You should output a graph _G_ with _n_ vertexes (2u2009β€u2009_n_u2009β€u20091000). There must be exactly _k_ shortest paths between vertex 1 and vertex 2 of the graph. The first line must contain an integer _n_. Then adjacency matrix _G_ with _n_ rows and _n_ columns must follow. Each element of the matrix must be 'N' or 'Y'. If _G__ij_ is 'Y', then graph _G_ has a edge connecting vertex _i_ and vertex _j_. Consider the graph vertexes are numbered from 1 to _n_. The graph must be undirected and simple: _G__ii_ = 'N' and _G__ij_u2009=u2009_G__ji_ must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them. Examples Output 8 NNYYYNNN NNNNNYYY YNNNNYYY YNNNNYYY YNNNNYYY NYYYYNNN NYYYYNNN NYYYYNNN Note In first example, there are 2 shortest paths: 1-3-2 and 1-4-2. In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2. | 1,900 | true | false | true | false | false | true | false | false | false | true | 8,276 |
1515B | Problem - 1515B - Codeforces =============== xa0 ]( "Codeforces Global Round 14") . Can he do it? Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 10^9$$$)xa0β the number of puzzle pieces. Output For each test case, if Phoenix can create a square with the $$$n$$$ puzzle pieces, print YES. Otherwise, print NO. Example Input 3 2 4 6 Output YES YES NO Note For $$$n=2$$$, Phoenix can create a square like this: For $$$n=4$$$, Phoenix can create a square like this: For $$$n=6$$$, it is impossible for Phoenix to create a square. | 1,000 | true | false | false | false | false | false | true | false | false | false | 3,090 |
1099B | Little Sofia is in fourth grade. Today in the geometry lesson she learned about segments and squares. On the way home, she decided to draw $$$n$$$ squares in the snow with a side length of $$$1$$$. For simplicity, we assume that Sofia lives on a plane and can draw only segments of length $$$1$$$, parallel to the coordinate axes, with vertices at integer points. In order to draw a segment, Sofia proceeds as follows. If she wants to draw a vertical segment with the coordinates of the ends $$$(x, y)$$$ and $$$(x, y+1)$$$. Then Sofia looks if there is already a drawn segment with the coordinates of the ends $$$(x', y)$$$ and $$$(x', y+1)$$$ for some $$$x'$$$. If such a segment exists, then Sofia quickly draws a new segment, using the old one as a guideline. If there is no such segment, then Sofia has to take a ruler and measure a new segment for a long time. Same thing happens when Sofia wants to draw a horizontal segment, but only now she checks for the existence of a segment with the same coordinates $$$x$$$, $$$x+1$$$ and the differing coordinate $$$y$$$. For example, if Sofia needs to draw one square, she will have to draw two segments using a ruler: After that, she can draw the remaining two segments, using the first two as a guide: If Sofia needs to draw two squares, she will have to draw three segments using a ruler: After that, she can draw the remaining four segments, using the first three as a guide: Sofia is in a hurry, so she wants to minimize the number of segments that she will have to draw with a ruler without a guide. Help her find this minimum number. | 1,100 | true | false | false | false | false | true | false | true | false | false | 5,209 |
150A | You can't possibly imagine how cold our friends are this winter in Nvodsk! Two of them play the following game to warm up: initially a piece of paper has an integer _q_. During a move a player should write any integer number that is a non-trivial divisor of the last written number. Then he should run this number of circles around the hotel. Let us remind you that a number's divisor is called non-trivial if it is different from one and from the divided number itself. The first person who can't make a move wins as he continues to lie in his warm bed under three blankets while the other one keeps running. Determine which player wins considering that both players play optimally. If the first player wins, print any winning first move. Input The first line contains the only integer _q_ (1u2009β€u2009_q_u2009β€u20091013). Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. Output In the first line print the number of the winning player (1 or 2). If the first player wins then the second line should contain another integer β his first move (if the first player can't even make the first move, print 0). If there are multiple solutions, print any of them. Note Number 6 has only two non-trivial divisors: 2 and 3. It is impossible to make a move after the numbers 2 and 3 are written, so both of them are winning, thus, number 6 is the losing number. A player can make a move and write number 6 after number 30; 6, as we know, is a losing number. Thus, this move will bring us the victory. | 1,400 | true | false | false | false | false | false | false | false | false | false | 9,276 |
714B | Today, hedgehog Filya went to school for the very first time! Teacher gave him a homework which Filya was unable to complete without your help. Filya is given an array of non-negative integers _a_1,u2009_a_2,u2009...,u2009_a__n_. First, he pick an integer _x_ and then he adds _x_ to some elements of the array (no more than once), subtract _x_ from some other elements (also, no more than once) and do no change other elements. He wants all elements of the array to be equal. Now he wonders if it's possible to pick such integer _x_ and change some elements of the array using this _x_ in order to make all elements equal. Input The first line of the input contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009100u2009000)xa0β the number of integers in the Filya's array. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u2009109)xa0β elements of the array. Output If it's impossible to make all elements of the array equal using the process given in the problem statement, then print "NO" (without quotes) in the only line of the output. Otherwise print "YES" (without quotes). Note In the first sample Filya should select _x_u2009=u20091, then add it to the first and the last elements of the array and subtract from the second and the third elements. | 1,200 | false | false | true | false | false | false | false | false | true | false | 6,960 |
266A | Problem - 266A - 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 ") Tutorial") β the number of stones on the table. The next line contains string _s_, which represents the colors of the stones. We'll consider the stones in the row numbered from 1 to _n_ from left to right. Then the _i_-th character _s_ equals "R", if the _i_-th stone is red, "G", if it's green and "B", if it's blue. Output Print a single integer β the answer to the problem. Examples Input 3 RRG Output 1 Input 5 RRRRR Output 4 Input 4 BRBG Output 0 | 800 | false | false | true | false | false | false | false | false | false | false | 8,771 |
938F | Problem - 938F - 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 dp greedy *2700 No tag edit access β Contest materials . Output Print the lexicographically minimal string you may obtain after performing _k_ operations. Examples Input adcbca Output aba Input abacabadabacaba Output aabacaba Note Possible operations in examples: 1. adcbca adcba aba; 2. abacabadabacaba abcabadabacaba aabadabacaba aabacaba. | 2,700 | false | true | false | true | false | false | false | false | false | false | 5,946 |
821C | Okabe and Super Hacker Daru are stacking and removing boxes. There are _n_ boxes numbered from 1 to _n_. Initially there are no boxes on the stack. Okabe, being a control freak, gives Daru 2_n_ commands: _n_ of which are to add a box to the top of the stack, and _n_ of which are to remove a box from the top of the stack and throw it in the trash. Okabe wants Daru to throw away the boxes in the order from 1 to _n_. Of course, this means that it might be impossible for Daru to perform some of Okabe's remove commands, because the required box is not on the top of the stack. That's why Daru can decide to wait until Okabe looks away and then reorder the boxes in the stack in any way he wants. He can do it at any point of time between Okabe's commands, but he can't add or remove boxes while he does it. Tell Daru the minimum number of times he needs to reorder the boxes so that he can successfully complete all of Okabe's commands. It is guaranteed that every box is added before it is required to be removed. Input The first line of input contains the integer _n_ (1u2009β€u2009_n_u2009β€u20093Β·105)xa0β the number of boxes. Each of the next 2_n_ lines of input starts with a string "add" or "remove". If the line starts with the "add", an integer _x_ (1u2009β€u2009_x_u2009β€u2009_n_) follows, indicating that Daru should add the box with number _x_ to the top of the stack. It is guaranteed that exactly _n_ lines contain "add" operations, all the boxes added are distinct, and _n_ lines contain "remove" operations. It is also guaranteed that a box is always added before it is required to be removed. Output Print the minimum number of times Daru needs to reorder the boxes to successfully complete all of Okabe's commands. Examples Input 3 add 1 remove add 2 add 3 remove remove Input 7 add 3 add 2 add 1 remove add 4 remove remove remove add 6 add 7 add 5 remove remove remove Note In the first sample, Daru should reorder the boxes after adding box 3 to the stack. In the second sample, Daru should reorder the boxes after adding box 4 and box 7 to the stack. | 1,500 | false | true | false | false | true | false | false | false | false | false | 6,471 |
1413F | In the Land of Fire there are $$$n$$$ villages and $$$n-1$$$ bidirectional road, and there is a path between any pair of villages by roads. There are only two types of roads: stone ones and sand ones. Since the Land of Fire is constantly renovating, every morning workers choose a single road and flip its type (so it becomes a stone road if it was a sand road and vice versa). Also everyone here loves ramen, that's why every morning a ramen pavilion is set in the middle of every stone road, and at the end of each day all the pavilions are removed. For each of the following $$$m$$$ days, after another road is flipped, Naruto and Jiraiya choose a simple path β that is, a route which starts in a village and ends in a (possibly, the same) village, and doesn't contain any road twice. Since Naruto and Jiraiya also love ramen very much, they buy a single cup of ramen on each stone road and one of them eats it. Since they don't want to offend each other, they only choose routes where they can eat equal number of ramen cups. Since they both like traveling, they choose any longest possible path. After every renovation find the maximal possible length of a path (that is, the number of roads in it) they can follow. Input The first line contains the only positive integer $$$n$$$ ($$$2 leq n leq 500,000$$$) standing for the number of villages in the Land of Fire. Each of the following $$$(n-1)$$$ lines contains a description of another road, represented as three positive integers $$$u$$$, $$$v$$$ and $$$t$$$ ($$$1 leq u, v leq n$$$, $$$t in {0,1}$$$). The first two numbers denote the villages connected by the road, and the third denotes the initial type of the road: $$$0$$$ for the sand one and $$$1$$$ for the stone one. Roads are numbered from $$$1$$$ to $$$(n-1)$$$ in the order from the input. The following line contains a positive integer $$$m$$$ ($$$1 leq m leq 500,000$$$) standing for the number of days Naruto and Jiraiya travel for. Each of the following $$$m$$$ lines contains the single integer $$$id$$$ ($$$1 leq id leq n-1$$$) standing for the index of the road whose type is flipped on the morning of corresponding day. It is guaranteed that there is a road path between any pair of villages. Output Output $$$m$$$ lines. In the $$$i$$$-th of them print the only integer denoting the maximal possible length of any valid path on the $$$i$$$-th day. Example Input 5 1 2 0 1 3 0 3 5 0 3 4 0 5 3 4 1 3 4 Note After the renovation of the $$$3$$$-rd road the longest path consists of the roads $$$1$$$, $$$2$$$ and $$$4$$$. After the renovation of the $$$4$$$-th road one of the longest paths consists of the roads $$$1$$$ and $$$2$$$. After the renovation of the $$$1$$$-st road one of the longest paths consists of the roads $$$1$$$, $$$2$$$ and $$$3$$$. After the renovation of the $$$3$$$-rd road the longest path consists of the roads $$$1$$$, $$$2$$$ and $$$4$$$. After the renovation of the $$$4$$$-rd road one of the longest paths consists of the roads $$$2$$$ and $$$4$$$. | 2,800 | false | false | false | false | true | false | false | false | false | false | 3,609 |
958C3 | Heidi is now just one code away from breaking the encryption of the Death Star plans. The screen that should be presenting her with the description of the next code looks almost like the previous one, though who would have thought that the evil Empire engineers would fill this small screen with several million digits! It is just ridiculous to think that anyone would read them all... Heidi is once again given a sequence _A_ and two integers _k_ and _p_. She needs to find out what the encryption key _S_ is. Let _X_ be a sequence of integers, and _p_ a positive integer. We define the score of _X_ to be the sum of the elements of _X_ modulo _p_. Heidi is given a sequence _A_ that consists of _N_ integers, and also given integers _k_ and _p_. Her goal is to split _A_ into _k_ parts such that: Each part contains at least 1 element of _A_, and each part consists of contiguous elements of _A_. No two parts overlap. The total sum _S_ of the scores of those parts is minimized (not maximized!). Output the sum _S_, which is the encryption code. Input The first line of the input contains three space-separated integers _N_, _k_ and _p_ (_k_u2009β€u2009_N_u2009β€u2009500u2009000, 2u2009β€u2009_k_u2009β€u2009100, 2u2009β€u2009_p_u2009β€u2009100) β the number of elements in _A_, the number of parts _A_ should be split into, and the modulo for computing scores, respectively. The second line contains _N_ space-separated integers that are the elements of _A_. Each integer is from the interval [1,u20091u2009000u2009000]. Output Output the number _S_ as described in the problem statement. Examples Input 10 5 12 16 3 24 13 9 8 7 5 12 12 Note In the first example, if the input sequence is split as (3), (4,u20097), (2), the total score would be . It is easy to see that this score is the smallest possible. In the second example, one possible way to obtain score 13 is to make the following split: (16,u20093), (24), (13), (9,u20098), (7,u20095,u200912,u200912). | 2,500 | false | false | false | true | true | false | false | false | false | false | 5,886 |
1582G | Kuzya started going to school. He was given math homework in which he was given an array $$$a$$$ of length $$$n$$$ and an array of symbols $$$b$$$ of length $$$n$$$, consisting of symbols '*' and '/'. Let's denote a path of calculations for a segment $$$[l; r]$$$ ($$$1 le l le r le n$$$) in the following way: Let $$$x=1$$$ initially. For every $$$i$$$ from $$$l$$$ to $$$r$$$ we will consequently do the following: if $$$b_i=$$$ '*', $$$x=x*a_i$$$, and if $$$b_i=$$$ '/', then $$$x=frac{x}{a_i}$$$. Let's call a path of calculations for the segment $$$[l; r]$$$ a list of all $$$x$$$ that we got during the calculations (the number of them is exactly $$$r - l + 1$$$). For example, let $$$a=[7,$$$ $$$12,$$$ $$$3,$$$ $$$5,$$$ $$$4,$$$ $$$10,$$$ $$$9]$$$, $$$b=[/,$$$ $$$*,$$$ $$$/,$$$ $$$/,$$$ $$$/,$$$ $$$*,$$$ $$$*]$$$, $$$l=2$$$, $$$r=6$$$, then the path of calculations for that segment is $$$[12,$$$ $$$4,$$$ $$$0.8,$$$ $$$0.2,$$$ $$$2]$$$. Let's call a segment $$$[l;r]$$$ simple if the path of calculations for it contains only integer numbers. Kuzya needs to find the number of simple segments $$$[l;r]$$$ ($$$1 le l le r le n$$$). Since he obviously has no time and no interest to do the calculations for each option, he asked you to write a program to get to find that number! Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^6$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$). The third line contains $$$n$$$ symbols without spaces between them β the array $$$b_1, b_2 ldots b_n$$$ ($$$b_i=$$$ '/' or $$$b_i=$$$ '*' for every $$$1 le i le n$$$). | 2,600 | false | false | false | false | true | false | false | false | false | false | 2,718 |
226E | In Berland each feudal owns exactly one castle and each castle belongs to exactly one feudal. Each feudal, except one (the King) is subordinate to another feudal. A feudal can have any number of vassals (subordinates). Some castles are connected by roads, it is allowed to move along the roads in both ways. Two castles have a road between them if and only if the owner of one of these castles is a direct subordinate to the other owner. Each year exactly one of these two events may happen in Berland. 1. The barbarians attacked castle _c_. The interesting fact is, the barbarians never attacked the same castle twice throughout the whole Berlandian history. 2. A noble knight sets off on a journey from castle _a_ to castle _b_ (provided that on his path he encounters each castle not more than once). Let's consider the second event in detail. As the journey from _a_ to _b_ is not short, then the knight might want to stop at a castle he encounters on his way to have some rest. However, he can't stop at just any castle: his nobility doesn't let him stay in the castle that has been desecrated by the enemy's stench. A castle is desecrated if and only if it has been attacked after the year of _y_. So, the knight chooses the _k_-th castle he encounters, starting from _a_ (castles _a_ and _b_ aren't taken into consideration), that hasn't been attacked in years from _y_u2009+u20091 till current year. The knights don't remember which castles were attacked on what years, so he asked the court scholar, aka you to help them. You've got a sequence of events in the Berland history. Tell each knight, in what city he should stop or else deliver the sad news β that the path from city _a_ to city _b_ has less than _k_ cities that meet his requirements, so the knight won't be able to rest. Input The first input line contains integer _n_ (2u2009β€u2009_n_u2009β€u2009105) β the number of feudals. The next line contains _n_ space-separated integers: the _i_-th integer shows either the number of the _i_-th feudal's master, or a 0, if the _i_-th feudal is the King. The third line contains integer _m_ (1u2009β€u2009_m_u2009β€u2009105) β the number of queries. Then follow _m_ lines that describe the events. The _i_-th line (the lines are indexed starting from 1) contains the description of the event that occurred in year _i_. Each event is characterised by type _t__i_ (1u2009β€u2009_t__i_u2009β€u20092). The description of the first type event looks as two space-separated integers _t__i_ _c__i_ (_t__i_u2009=u20091;xa01u2009β€u2009_c__i_u2009β€u2009_n_), where _c__i_ is the number of the castle that was attacked by the barbarians in the _i_-th year. The description of the second type contains five space-separated integers: _t__i_ _a__i_ _b__i_ _k__i_ _y__i_ (_t__i_u2009=u20092;xa01u2009β€u2009_a__i_,u2009_b__i_,u2009_k__i_u2009β€u2009_n_;xa0_a__i_u2009β u2009_b__i_;xa00u2009β€u2009_y__i_u2009<u2009_i_), where _a__i_ is the number of the castle from which the knight is setting off, _b__i_ is the number of the castle to which the knight is going, _k__i_ and _y__i_ are the _k_ and _y_ from the second event's description. You can consider the feudals indexed from 1 to _n_. It is guaranteed that there is only one king among the feudals. It is guaranteed that for the first type events all values _c__i_ are different. Output For each second type event print an integer β the number of the castle where the knight must stay to rest, or -1, if he will have to cover the distance from _a__i_ to _b__i_ without a rest. Separate the answers by whitespaces. Print the answers in the order, in which the second type events are given in the input. Examples Input 3 0 1 2 5 2 1 3 1 0 1 2 2 1 3 1 0 2 1 3 1 1 2 1 3 1 2 Input 6 2 5 2 2 0 5 3 2 1 6 2 0 1 2 2 4 5 1 0 Note In the first sample there is only castle 2 on the knight's way from castle 1 to castle 3. When the knight covers the path 1u2009-u20093 for the first time, castle 2 won't be desecrated by an enemy and the knight will stay there. In the second year the castle 2 will become desecrated, so the knight won't have anywhere to stay for the next two years (as finding a castle that hasn't been desecrated from years 1 and 2, correspondingly, is important for him). In the fifth year the knight won't consider the castle 2 desecrated, so he will stay there again. | 2,900 | false | false | false | false | true | false | false | false | false | false | 8,931 |
1567D | On the board, Bob wrote $$$n$$$ positive integers in . Alice sees the board, but accidentally interprets the numbers on the board as base-$$$11$$$ integers and adds them up (in base $$$11$$$). What numbers should Bob write on the board, so Alice's sum is as large as possible? Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$s$$$ and $$$n$$$ ($$$1 leq s leq 10^9$$$; $$$1 leq n leq min(100, s)$$$)xa0β the sum and amount of numbers on the board, respectively. Numbers $$$s$$$ and $$$n$$$ are given in decimal notation (base $$$10$$$). Output For each test case, output $$$n$$$ positive integersxa0β the numbers Bob should write on the board, so Alice's sum is as large as possible. If there are multiple answers, print any of them. Example Input 6 97 2 17 1 111 4 100 2 10 9 999999 3 Output 70 27 17 3 4 100 4 10 90 1 1 2 1 1 1 1 1 1 999900 90 9 Note In the first test case, $$$70_{10} + 27_{10} = 97_{10}$$$, and Alice's sum is $$$$$$70_{11} + 27_{11} = 97_{11} = 9 cdot 11 + 7 = 106_{10}.$$$$$$ (Here $$$x_b$$$ represents the number $$$x$$$ in base $$$b$$$.) It can be shown that it is impossible for Alice to get a larger sum than $$$106_{10}$$$. In the second test case, Bob can only write a single number on the board, so he must write $$$17$$$. In the third test case, $$$3_{10} + 4_{10} + 100_{10} + 4_{10} = 111_{10}$$$, and Alice's sum is $$$$$$3_{11} + 4_{11} + 100_{11} + 4_{11} = 110_{11} = 1 cdot 11^2 + 1 cdot 11 = 132_{10}.$$$$$$ It can be shown that it is impossible for Alice to get a larger sum than $$$132_{10}$$$. | 2,000 | true | true | true | false | false | true | false | false | false | false | 2,801 |
998A | There are quite a lot of ways to have fun with inflatable balloons. For example, you can fill them with water and see what happens. Grigory and Andrew have the same opinion. So, once upon a time, they went to the shop and bought $$$n$$$ packets with inflatable balloons, where $$$i$$$-th of them has exactly $$$a_i$$$ balloons inside. They want to divide the balloons among themselves. In addition, there are several conditions to hold: Do not rip the packets (both Grigory and Andrew should get unbroken packets); Distribute all packets (every packet should be given to someone); Give both Grigory and Andrew at least one packet; To provide more fun, the total number of balloons in Grigory's packets should not be equal to the total number of balloons in Andrew's packets. Help them to divide the balloons or determine that it's impossible under these conditions. Input The first line of input contains a single integer $$$n$$$ ($$$1 le n le 10$$$)xa0β the number of packets with balloons. The second line contains $$$n$$$ integers: $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_n$$$ ($$$1 le a_i le 1000$$$)xa0β the number of balloons inside the corresponding packet. Output If it's impossible to divide the balloons satisfying the conditions above, print $$$-1$$$. Otherwise, print an integer $$$k$$$xa0β the number of packets to give to Grigory followed by $$$k$$$ distinct integers from $$$1$$$ to $$$n$$$xa0β the indices of those. The order of packets doesn't matter. If there are multiple ways to divide balloons, output any of them. Note In the first test Grigory gets $$$3$$$ balloons in total while Andrey gets $$$1$$$. In the second test there's only one way to divide the packets which leads to equal numbers of balloons. In the third test one of the boys won't get a packet at all. | 1,000 | false | false | true | false | false | true | false | false | false | false | 5,713 |
1383F | Koa the Koala has a directed graph $$$G$$$ with $$$n$$$ nodes and $$$m$$$ edges. Each edge has a capacity associated with it. Exactly $$$k$$$ edges of the graph, numbered from $$$1$$$ to $$$k$$$, are special, such edges initially have a capacity equal to $$$0$$$. Koa asks you $$$q$$$ queries. In each query she gives you $$$k$$$ integers $$$w_1, w_2, ldots, w_k$$$. This means that capacity of the $$$i$$$-th special edge becomes $$$w_i$$$ (and other capacities remain the same). Koa wonders: what is the $$$, $$$1 le q le 2 cdot 10^5$$$)xa0β the number of nodes, the number of edges, the number of special edges and the number of queries. Each of the next $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$, $$$w$$$ ($$$1 le u, v le n$$$; $$$0 le w le 25$$$)xa0β the description of a directed edge from node $$$u$$$ to node $$$v$$$ with capacity $$$w$$$. Edges are numbered starting from $$$1$$$ in the same order they are listed in the input. The first $$$k$$$ edges are the special edges. It is guaranteed that $$$w_i = 0$$$ for all $$$i$$$ with $$$1 le i le k$$$. Each of the next $$$q$$$ lines contains $$$k$$$ integers $$$w_1, w_2, ldots, w_k$$$ ($$$0 le w_i le 25$$$) xa0β the description of the query. $$$w_i$$$ denotes the capacity of $$$i$$$-th edge. Note For the second sample, the following images correspond to the first two queries (from left to right respectively). For each edge there is a pair flow/capacity denoting flow pushed through the edge and edge's capacity. The special edges are colored in red. As you can see in first query maximum flow from node $$$1$$$ to node $$$4$$$ equals $$$0$$$ and in second query equals $$$1$$$. | 3,200 | false | false | false | false | false | false | false | false | false | true | 3,751 |
1712A | God's Blessing on This PermutationForces! A Random Pebble You are given a permutation $$$p_1,p_2,ldots,p_n$$$ of length $$$n$$$ and a positive integer $$$k le n$$$. In one operation you can choose two indices $$$i$$$ and $$$j$$$ ($$$1 le i < j le n$$$) and swap $$$p_i$$$ with $$$p_j$$$. Find the minimum number of operations needed to make the sum $$$p_1 + p_2 + ldots + p_k$$$ as small as possible. A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 100$$$). The second line of each test case contains $$$n$$$ integers $$$p_1,p_2,ldots,p_n$$$ ($$$1 le p_i le n$$$). It is guaranteed that the given numbers form a permutation of length $$$n$$$. Output For each test case print one integerxa0β the minimum number of operations needed to make the sum $$$p_1 + p_2 + ldots + p_k$$$ as small as possible. Example Input 4 3 1 2 3 1 3 3 1 2 3 4 2 3 4 1 2 1 1 1 Note In the first test case, the value of $$$p_1 + p_2 + ldots + p_k$$$ is initially equal to $$$2$$$, but the smallest possible value is $$$1$$$. You can achieve it by swapping $$$p_1$$$ with $$$p_3$$$, resulting in the permutation $$$[1, 3, 2]$$$. In the second test case, the sum is already as small as possible, so the answer is $$$0$$$. | 800 | false | true | true | false | false | false | false | false | false | false | 2,013 |
2002A | Problem - 2002A - 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 implementation math *800 No tag edit access β Contest materials ") Editorial") $$$, $$$(x_2,y_2)$$$ are two distinct cells with the same color, then $$$max(x_1-x_2,y_1-y_2)ge k$$$. You don't like using too many colors. Please find the minimum number of colors needed to color the grid. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1le tle1000$$$). The description of the test cases follows. The only line of each test case consists of three positive integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1le n,m,kle10^4$$$)xa0β the dimensions of the grid and the magic constant. Output For each test case, print a single integerxa0β the minimum number of colors needed to color the grid. Example Input 6 3 3 2 5 1 10000 7 3 4 3 2 7 8 9 6 2 5 4 Output 4 5 12 6 36 8 Note In the first test case, one of the optimal constructions is: In the second test case, the color of all cells must be pairwise different, so the answer is $$$5$$$. | 800 | true | false | true | false | false | true | false | false | false | false | 249 |
1765N | Problem - 1765N - 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 *1500 No tag edit access β Contest materials ; $$$10$$$ can be converted to $$$1$$$ (but not to $$$0$$$ since it is not positive). Your task is to find the minimum positive integer that you can obtain from $$$x$$$ if you can apply the aforementioned operation exactly $$$k$$$ times. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$x$$$ ($$$1 le x < 10^{500000}$$$). The second line contains a single integer $$$k$$$ ($$$0 le k < x$$$), where $$$x$$$ is the length of the number $$$x$$$. The sum of $$$x$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, print one integerxa0β the minimum positive number that you can obtain from $$$x$$$ if you can apply the operation exactly $$$k$$$ times. Example Input 5 10000 4 1337 0 987654321 6 66837494128 5 7808652 3 Output 1 1337 321 344128 7052 | 1,500 | false | true | false | false | false | false | false | false | false | false | 1,704 |
271D | You've got string _s_, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring _s_[_l_..._r_] (1u2009β€u2009_l_u2009β€u2009_r_u2009β€u2009_s_) of string _s_u2009u2009=u2009u2009_s_1_s_2..._s__s_ (where _s_ is the length of string _s_) is string u2009_s__l__s__l_u2009+u20091..._s__r_. The substring _s_[_l_..._r_] is good, if among the letters u2009_s__l_,u2009_s__l_u2009+u20091,u2009...,u2009_s__r_ there are at most _k_ bad ones (look at the sample's explanation to understand it more clear). Your task is to find the number of distinct good substrings of the given string _s_. Two substrings _s_[_x_..._y_] and _s_[_p_..._q_] are considered distinct if their content is different, i.e. _s_[_x_..._y_]u2009β u2009_s_[_p_..._q_]. Input The first line of the input is the non-empty string _s_, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the _i_-th character of this string equals "1", then the _i_-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer _k_ (0u2009β€u2009_k_u2009β€u2009_s_) β the maximum acceptable number of bad characters in a good substring. Output Print a single integer β the number of distinct good substrings of string _s_. Examples Input ababab 01000000000000000000000000 1 Input acbacbacaa 00000000000000000000000000 2 Note In the first example there are following good substrings: "a", "ab", "b", "ba", "bab". In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb". | 1,800 | false | false | false | false | true | false | false | false | false | false | 8,748 |
1392G | Omkar has a pie tray with $$$k$$$ ($$$2 leq k leq 20$$$) spots. Each spot in the tray contains either a chocolate pie or a pumpkin pie. However, Omkar does not like the way that the pies are currently arranged, and has another ideal arrangement that he would prefer instead. To assist Omkar, $$$n$$$ elves have gathered in a line to swap the pies in Omkar's tray. The $$$j$$$-th elf from the left is able to swap the pies at positions $$$a_j$$$ and $$$b_j$$$ in the tray. In order to get as close to his ideal arrangement as possible, Omkar may choose a contiguous subsegment of the elves and then pass his pie tray through the subsegment starting from the left. However, since the elves have gone to so much effort to gather in a line, they request that Omkar's chosen segment contain at least $$$m$$$ ($$$1 leq m leq n$$$) elves. Formally, Omkar may choose two integers $$$l$$$ and $$$r$$$ satisfying $$$1 leq l leq r leq n$$$ and $$$r - l + 1 geq m$$$ so that first the pies in positions $$$a_l$$$ and $$$b_l$$$ will be swapped, then the pies in positions $$$a_{l + 1}$$$ and $$$b_{l + 1}$$$ will be swapped, etc. until finally the pies in positions $$$a_r$$$ and $$$b_r$$$ are swapped. Help Omkar choose a segment of elves such that the amount of positions in Omkar's final arrangement that contain the same type of pie as in his ideal arrangement is the maximum possible. Note that since Omkar has a big imagination, it might be that the amounts of each type of pie in his original arrangement and in his ideal arrangement do not match. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 leq m leq n leq 10^6$$$ and $$$2 leq k leq 20$$$) xa0β the number of elves, the minimum subsegment length, and the number of spots in Omkar's tray respectively. The second and third lines each contain a string of length $$$k$$$ consisting of $$$0$$$s and $$$1$$$s that represent initial arrangement of pies and ideal arrangement of pies; the $$$j$$$-th character in each string is equal to $$$0$$$ if the $$$j$$$-th spot in the arrangement contains a chocolate pie and is equal to $$$1$$$ if the $$$j$$$-th spot in the arrangement contains a pumpkin pie. It is not guaranteed that the two strings have the same amount of $$$0$$$s or the same amount of $$$1$$$s. $$$n$$$ lines follow. The $$$j$$$-th of these lines contains two integers $$$a_j$$$ and $$$b_j$$$ ($$$1 leq a_j, b_j leq k$$$, $$$a_j eq b_j$$$) which indicate that the $$$j$$$-th elf from the left can swap the pies at positions $$$a_j$$$ and $$$b_j$$$ in the tray. Output Output two lines. The first line should contain a single integer $$$s$$$ ($$$0 leq s leq k$$$) equal to the amount of positions that contain the same type of pie in Omkar's final arrangement and in Omkar's ideal arrangement; $$$s$$$ should be the maximum possible. The second line should contain two integers $$$l$$$ and $$$r$$$ satisfying $$$1 leq l leq r leq n$$$ and $$$r - l + 1 geq m$$$, indicating that Omkar should pass his tray through the subsegment $$$l, l + 1, dots, r$$$ to achieve a final arrangement with $$$s$$$ positions having the same type of pie as his ideal arrangement. If there are multiple answers you may output any of them. Examples Input 4 2 5 11000 00011 1 3 3 5 4 2 3 4 Input 4 3 5 11000 00011 1 3 1 5 2 4 1 5 Note In the first test case, the swaps will go like this: Swap $$$1$$$ and $$$3$$$: 11000 becomes 01100 Swap $$$3$$$ and $$$5$$$: 01100 becomes 01001 Swap $$$4$$$ and $$$2$$$: 01001 becomes 00011 The final arrangement is the same as the ideal arrangement 00011, so there are $$$5$$$ positions with the same type of pie, which is optimal. In the second test case, the swaps will go like this: Swap $$$1$$$ and $$$3$$$: 11000 becomes 01100 Swap $$$1$$$ and $$$5$$$: 01100 becomes 01100 Swap $$$4$$$ and $$$2$$$: 01100 becomes 00110 Swap $$$1$$$ and $$$5$$$: 00110 becomes 00110 The final arrangement has $$$3$$$ positions with the same type of pie as the ideal arrangement 00011, those being positions $$$1$$$, $$$2$$$, and $$$4$$$. In this case the subsegment of elves $$$(l, r) = (2, 3)$$$ is more optimal, but that subsegment is only length $$$2$$$ and therefore does not satisfy the constraint that the subsegment be of length at least $$$m = 3$$$. | 2,900 | true | false | false | true | false | false | false | false | false | false | 3,710 |
1697A | You are walking through a parkway near your house. The parkway has $$$n+1$$$ benches in a row numbered from $$$1$$$ to $$$n+1$$$ from left to right. The distance between the bench $$$i$$$ and $$$i+1$$$ is $$$a_i$$$ meters. Initially, you have $$$m$$$ units of energy. To walk $$$1$$$ meter of distance, you spend $$$1$$$ unit of your energy. You can't walk if you have no energy. Also, you can restore your energy by sitting on benches (and this is the only way to restore the energy). When you are sitting, you can restore any integer amount of energy you want (if you sit longer, you restore more energy). Note that the amount of your energy can exceed $$$m$$$. Your task is to find the minimum amount of energy you have to restore (by sitting on benches) to reach the bench $$$n+1$$$ from the bench $$$1$$$ (and end your walk). 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 two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 100$$$; $$$1 le m le 10^4$$$). The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 100$$$), where $$$a_i$$$ is the distance between benches $$$i$$$ and $$$i+1$$$. Output For each test case, print one integer β the minimum amount of energy you have to restore (by sitting on benches) to reach the bench $$$n+1$$$ from the bench $$$1$$$ (and end your walk) in the corresponding test case. Example Input 3 3 1 1 2 1 4 5 3 3 5 2 5 16 1 2 3 4 5 Note In the first test case of the example, you can walk to the bench $$$2$$$, spending $$$1$$$ unit of energy, then restore $$$2$$$ units of energy on the second bench, walk to the bench $$$3$$$, spending $$$2$$$ units of energy, restore $$$1$$$ unit of energy and go to the bench $$$4$$$. In the third test case of the example, you have enough energy to just go to the bench $$$6$$$ without sitting at all. | 800 | false | true | true | false | false | false | false | false | false | false | 2,100 |
1530A | Problem - 1530A - 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 math *800 No tag edit access β Contest materials ") ") binary decimals. Compute the smallest number of binary decimals required for that. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$), denoting the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^9$$$), denoting the number to be represented. Output For each test case, output the smallest number of binary decimals required to represent $$$n$$$ as a sum. Example Input 3 121 5 1000000000 Output 2 5 1 Note In the first test case, $$$121$$$ can be represented as $$$121 = 110 + 11$$$ or $$$121 = 111 + 10$$$. In the second test case, $$$5$$$ can be represented as $$$5 = 1 + 1 + 1 + 1 + 1$$$. In the third test case, $$$1,000,000,000$$$ is a binary decimal itself, thus the answer is $$$1$$$. | 800 | true | true | false | false | false | false | false | false | false | false | 3,015 |
156A | Dr. Moriarty is about to send a message to Sherlock Holmes. He has a string _s_. String _p_ is called a substring of string _s_ if you can read it starting from some position in the string _s_. For example, string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba". Dr. Moriarty plans to take string _s_ and cut out some substring from it, let's call it _t_. Then he needs to change the substring _t_ zero or more times. As a result, he should obtain a fixed string _u_ (which is the string that should be sent to Sherlock Holmes). One change is defined as making one of the following actions: Insert one letter to any end of the string. Delete one letter from any end of the string. Change one letter into any other one. Moriarty is very smart and after he chooses some substring _t_, he always makes the minimal number of changes to obtain _u_. Help Moriarty choose the best substring _t_ from all substrings of the string _s_. The substring _t_ should minimize the number of changes Moriarty should make to obtain the string _u_ from it. Input The first line contains a non-empty string _s_, consisting of lowercase Latin letters. The second line contains a non-empty string _u_, consisting of lowercase Latin letters. The lengths of both strings are in the range from 1 to 2000, inclusive. Output Print the only integer β the minimum number of changes that Dr. Moriarty has to make with the string that you choose. Note In the first sample Moriarty can take any substring of length 3, and it will be equal to the required message _u_, so Moriarty won't have to make any changes. In the second sample you should take a substring consisting of characters from second to fourth ("bca") or from fifth to sixth ("bc"). Then you will only have to make one change: to change or to add the last character. In the third sample the initial string _s_ doesn't contain any character that the message should contain, so, whatever string you choose, you will have to make at least 7 changes to obtain the required message. | 1,700 | false | false | false | false | false | false | true | false | false | false | 9,252 |
1807D | You have an array $$$a_1, a_2, dots, a_n$$$. Answer $$$q$$$ queries of the following form: If we change all elements in the range $$$a_l, a_{l+1}, dots, a_r$$$ of the array to $$$k$$$, will the sum of the entire array be odd? Note that queries are independent and do not affect future queries. 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 consists of $$$2$$$ integers $$$n$$$ and $$$q$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le q le 2 cdot 10^5$$$)xa0β the length of the array and the number of queries. The second line of each test case consists of $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 10^9$$$)xa0β the array $$$a$$$. The next $$$q$$$ lines of each test case consists of $$$3$$$ integers $$$l,r,k$$$ ($$$1 le l le r le n$$$; $$$1 le k le 10^9$$$)xa0β the queries. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$, and the sum of $$$q$$$ doesn't exceed $$$2 cdot 10^5$$$. Output For each query, output "YES" if the sum of the entire array becomes odd, and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 2 5 5 2 2 1 3 2 2 3 3 2 3 4 1 5 5 1 4 9 2 4 3 10 5 1 1 1 1 1 1 1 1 1 1 3 8 13 2 5 10 3 8 10 1 10 2 1 9 100 Output YES YES YES NO YES NO NO NO NO YES Note For the first test case: If the elements in the range $$$(2, 3)$$$ would get set to $$$3$$$ the array would become $$${2, 3, 3, 3, 2}$$$, the sum would be $$$2+3+3+3+2 = 13$$$ which is odd, so the answer is "YES". If the elements in the range $$$(2, 3)$$$ would get set to $$$4$$$ the array would become $$${2, 4, 4, 3, 2}$$$, the sum would be $$$2+4+4+3+2 = 15$$$ which is odd, so the answer is "YES". If the elements in the range $$$(1, 5)$$$ would get set to $$$5$$$ the array would become $$${5, 5, 5, 5, 5}$$$, the sum would be $$$5+5+5+5+5 = 25$$$ which is odd, so the answer is "YES". If the elements in the range $$$(1, 4)$$$ would get set to $$$9$$$ the array would become $$${9, 9, 9, 9, 2}$$$, the sum would be $$$9+9+9+9+2 = 38$$$ which is even, so the answer is "NO". If the elements in the range $$$(2, 4)$$$ would get set to $$$3$$$ the array would become $$${2, 3, 3, 3, 2}$$$, the sum would be $$$2+3+3+3+2 = 13$$$ which is odd, so the answer is "YES". | 900 | false | false | true | false | true | false | false | false | false | false | 1,432 |
1764G2 | The only difference between this problem and the other two versions is the maximum number of queries. In this version, you are allowed to ask at most $$$mathbf{25}$$$ queries. You can make hacks only if all versions of the problem are solved. This is an interactive problem. "Everybody! Doremy's Perfect Data Structure Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's Data Structure class, Doremy is teaching everyone a powerful data structurexa0β Doremy tree! Now she gives you a quiz to prove that you are paying attention in class. Given an array $$$a$$$ of length $$$m$$$, Doremy tree supports the query $$$Q(l,r,k)$$$, where $$$1 leq l leq r leq m$$$ and $$$1 leq k leq m$$$, which returns the number of distinct integers in the array $$$left[lfloorfrac{a_l}{k} floor, lfloorfrac{a_{l+1}}{k} floor, ldots, lfloorfrac{a_r}{k} floor ight]$$$. Doremy has a secret permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$. You can make queries, in one query, you give $$$3$$$ integers $$$l,r,k$$$ ($$$1 leq l leq r leq n$$$, $$$1 leq k leq n$$$) and receive the value of $$$Q(l,r,k)$$$ for the array $$$p$$$. Can you find the index $$$y$$$ ($$$1 leq y leq n$$$) such that $$$p_y=1$$$ in at most $$$mathbf{25}$$$ queries? Note that the permutation $$$p$$$ is fixed before any queries are made. Interaction You begin the interaction by reading an integer $$$n$$$ ($$$3 le n le 1024$$$) in the first linexa0β the length of the permutation. To make a query, you should output "? $$$l r k$$$" ($$$1 leq l leq r leq n$$$, $$$1 leq k leq n$$$) in a separate line. After each query, you should read an integer $$$x$$$xa0β the value of $$$Q(l,r,k)$$$ for $$$p$$$. In this version of the problem, you can make at most $$$25$$$ such queries. To give the answer, you should output "! $$$y$$$" ($$$1 leq y leq n$$$) in a separate line, where $$$p_y=1$$$. After printing a query or the answer, 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 documentation for other languages. Hacks Format The first line of the hack contains an integer $$$n$$$ ($$$3 le n le 1024$$$)xa0β the length of the permutation. The second line of the hack contains $$$n$$$ distinct integers $$$p_1,p_2,ldots,p_n$$$ ($$$1 le p_ile n$$$)xa0β the permutation. Example Output ? 1 3 4 ? 3 5 3 ? 3 4 5 ? 3 5 2 ! 4 Note The permutation in the example is $$$[3,5,2,1,4]$$$. The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity). In this interaction process: For the first query, $$$lfloorfrac{3}{4} floor=0,lfloorfrac{5}{4} floor=1,lfloorfrac{2}{4} floor=0$$$, so the answer is $$$2$$$. For the second query, $$$lfloorfrac{2}{3} floor=0,lfloorfrac{1}{3} floor=0,lfloorfrac{4}{3} floor=1$$$, so the answer is still $$$2$$$. For the third query, $$$lfloorfrac{2}{5} floor=0,lfloorfrac{1}{5} floor=0$$$, so the answer is $$$1$$$. For the fourth query, $$$lfloorfrac{2}{2} floor=1,lfloorfrac{1}{2} floor=0,lfloorfrac{4}{2} floor=2$$$, so the answer is $$$3$$$. The correct answer is got after $$$4$$$ queries, so this process will be judged correct. | 3,000 | false | false | false | false | false | false | false | true | false | false | 1,720 |
990G | You are given a tree consisting of $$$n$$$ vertices. A number is written on each vertex; the number on vertex $$$i$$$ is equal to $$$a_i$$$. Let's denote the function $$$g(x, y)$$$ as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex $$$x$$$ to vertex $$$y$$$ (including these two vertices). For every integer from $$$1$$$ to $$$2 cdot 10^5$$$ you have to count the number of pairs $$$(x, y)$$$ $$$(1 le x le y le n)$$$ such that $$$g(x, y)$$$ is equal to this number. Input The first line contains one integer $$$n$$$ β the number of vertices $$$(1 le n le 2 cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ $$$(1 le a_i le 2 cdot 10^5)$$$ β the numbers written on vertices. Then $$$n - 1$$$ lines follow, each containing two integers $$$x$$$ and $$$y$$$ $$$(1 le x, y le n, x e y)$$$ denoting an edge connecting vertex $$$x$$$ with vertex $$$y$$$. It is guaranteed that these edges form a tree. Output For every integer $$$i$$$ from $$$1$$$ to $$$2 cdot 10^5$$$ do the following: if there is no pair $$$(x, y)$$$ such that $$$x le y$$$ and $$$g(x, y) = i$$$, don't output anything. Otherwise output two integers: $$$i$$$ and the number of aforementioned pairs. You have to consider the values of $$$i$$$ in ascending order. See the examples for better understanding. Examples Input 6 1 2 4 8 16 32 1 6 6 3 3 4 4 2 6 5 Output 1 6 2 5 4 6 8 1 16 2 32 1 Input 4 9 16 144 6 1 3 2 3 4 3 Output 1 1 2 1 3 1 6 2 9 2 16 2 144 1 | 2,400 | false | false | false | true | false | false | false | false | false | false | 5,746 |
1684G | Let's consider Euclid's algorithm for finding the : if a < b: swap(a, b)if b == 0: return a r = reminder from dividing a by b if r > 0: append r to the back of t return Euclid(b, r) There is an array $$$p$$$ of pairs of positive integers that are not greater than $$$m$$$. Initially, the list $$$t$$$ is empty. Then the function is run on each pair in $$$p$$$. After that the list $$$t$$$ is shuffled and given to you. You have to find an array $$$p$$$ of any size not greater than $$$2 cdot 10^4$$$ that produces the given list $$$t$$$, or tell that no such array exists. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^3$$$, $$$1 le m le 10^9$$$)xa0β the length of the array $$$t$$$ and the constraint for integers in pairs. The second line contains $$$n$$$ integers $$$t_1, t_2, ldots, t_n$$$ ($$$1 le t_i le m$$$)xa0β the elements of the array $$$t$$$. Output If the answer does not exist, output $$$-1$$$. If the answer exists, in the first line output $$$k$$$ ($$$1 le k le 2 cdot 10^4$$$)xa0β the size of your array $$$p$$$, i.xa0e. the number of pairs in the answer.The $$$i$$$-th of the next $$$k$$$ lines should contain two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le m$$$)xa0β the $$$i$$$-th pair in $$$p$$$. If there are multiple valid answers you can output any of them. Examples Input 1 1000000000 845063470 Note In the first sample let's consider the array $$$t$$$ for each pair: $$$(19,, 11)$$$: $$$t = [8, 3, 2, 1]$$$; $$$(15,, 9)$$$: $$$t = [6, 3]$$$; $$$(3,, 7)$$$: $$$t = [1]$$$. So in total $$$t = [8, 3, 2, 1, 6, 3, 1]$$$, which is the same as the input $$$t$$$ (up to a permutation). In the second test case it is impossible to find such array $$$p$$$ of pairs that all integers are not greater than $$$10$$$ and $$$t = [7, 1]$$$ In the third test case for the pair $$$(15,, 8)$$$ array $$$t$$$ will be $$$[7, 1]$$$. | 2,800 | true | false | false | false | false | true | false | false | false | false | 2,167 |
11D | Problem - 11D - Codeforces =============== xa0 ]( "Codeforces Beta Round #11") β respectively the number of vertices and edges of the graph. Each of the subsequent _m_ lines contains two integers _a_ and _b_, (1u2009β€u2009_a_,u2009_b_u2009β€u2009_n_, _a_u2009β u2009_b_) indicating that vertices _a_ and _b_ are connected by an undirected edge. There is no more than one edge connecting any pair of vertices. Output Output the number of cycles in the given graph. Examples Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 7 Note The example graph is a clique and contains four cycles of length 3 and three cycles of length 4. | 2,200 | false | false | false | true | false | false | false | false | false | true | 9,937 |
1656B | You are given a list of $$$n$$$ integers. You can perform the following operation: you choose an element $$$x$$$ from the list, erase $$$x$$$ from the list, and subtract the value of $$$x$$$ from all the remaining elements. Thus, in one operation, the length of the list is decreased by exactly $$$1$$$. Given an integer $$$k$$$ ($$$k>0$$$), find if there is some sequence of $$$n-1$$$ operations such that, after applying the operations, the only remaining element of the list is equal to $$$k$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 2cdot 10^5$$$, $$$1 leq k leq 10^9$$$), the number of integers in the list, and the target value, respectively. The second line of each test case contains the $$$n$$$ integers of the list $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases is not greater that $$$2 cdot 10^5$$$. Output For each test case, print YES if you can achieve $$$k$$$ with a sequence of $$$n-1$$$ operations. Otherwise, print NO. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer). Example Input 4 4 5 4 2 2 7 5 4 1 9 1 3 4 2 17 17 0 2 17 18 18 Note In the first example we have the list $$${4, 2, 2, 7}$$$, and we have the target $$$k = 5$$$. One way to achieve it is the following: first we choose the third element, obtaining the list $$${2, 0, 5}$$$. Next we choose the first element, obtaining the list $$${-2, 3}$$$. Finally, we choose the first element, obtaining the list $$${5}$$$. | 1,100 | true | true | false | false | true | false | false | false | false | false | 2,353 |
802E | Problem - 802E - 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 *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 Good job! Now that Heidi is able to distinguish between Poisson and uniform distributions, she is in a good position to actually estimate the populations. Can you help Heidi estimate each village's population? Input Same as the easy version. Output Output one line per village, in the same order as provided in the input, containing your (integer) population estimate. Your answer is considered correct if it is an integer that falls into the interval , where _P_ is the real population of the village, used to create the distribution (either Poisson or uniform) from which the marmots drew their answers. | 2,200 | true | false | false | false | false | false | false | false | false | false | 6,563 |
1998C | I see satyam343. I'm shaking. Please more median problems this time. I love those. Please satyam343 we believe in you. β satyam343's biggest fan You are given an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$. You are also given a binary array $$$b$$$ of length $$$n$$$. You can perform the following operation at most $$$k$$$ times: Select an index $$$i$$$ ($$$1 leq i leq n$$$) such that $$$b_i = 1$$$. Set $$$a_i = a_i + 1$$$ (i.e., increase $$$a_i$$$ by $$$1$$$). Your score is defined to be $$$maxlimits_{i = 1}^{n} left( a_i + operatorname{median}(c_i) ight)$$$, where $$$c_i$$$ denotes the array of length $$$n-1$$$ that you get by deleting $$$a_i$$$ from $$$a$$$. In other words, your score is the maximum value of $$$a_i + operatorname{median}(c_i)$$$ over all $$$i$$$ from $$$1$$$ to $$$n$$$. Find the maximum score that you can achieve if you perform the operations optimally. For an arbitrary array $$$p$$$, $$$operatorname{median}(p)$$$ is defined as the $$$leftlfloor frac{p+1}{2} ight floor$$$-th smallest element of $$$p$$$. For example, $$$operatorname{median} left( [3,2,1,3] ight) = 2$$$ and $$$operatorname{median} left( [6,2,4,5,1] ight) = 4$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β the number of test cases. Each test case begins with two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 2 cdot 10^5$$$, $$$0 leq k leq 10^9$$$)xa0β the length of the $$$a$$$ and the number of operations you can perform. The following line contains $$$n$$$ space separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0β denoting the array $$$a$$$. The following line contains $$$n$$$ space separated integers $$$b_1, b_2, ldots, b_n$$$ ($$$b_i$$$ is $$$0$$$ or $$$1$$$)xa0β denoting the array $$$b$$$. 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 the maximum value of score you can get on a new line. Example Input 8 2 10 3 3 1 1 3 10 3 3 3 0 0 0 4 4 2 1 5 1 0 1 0 1 5 4 7 5 2 5 4 0 0 1 0 1 5 1 5 15 15 2 11 1 0 0 1 1 5 2 10 11 4 10 15 1 1 0 1 0 4 4 1 1 2 5 1 1 0 0 2 1000000000 1000000000 1000000000 1 1 Output 16 6 8 13 21 26 8 3000000000 Note For the first test case, it is optimal to perform $$$5$$$ operations on both elements so $$$a = [8,8]$$$. So, the maximum score we can achieve is $$$max(8 + operatorname{median}[8], 8 + operatorname{median}[8]) = 16$$$, as $$$c_1 = [a_2] = [8]$$$. It can be proven that you cannot get a better score. For the second test case, you are not able to perform operations on any elements, so $$$a$$$ remains $$$[3,3,3]$$$. So, the maximum score we can achieve is $$$3 + operatorname{median}[3, 3] = 6$$$, as $$$c_1 = [a_2, a_3] = [3, 3]$$$. It can be proven that you cannot get a better score. | 1,900 | false | true | true | false | false | true | true | true | false | false | 275 |
134B | Problem - 134B - Codeforces =============== xa0 . We can get a new pair (_a_u2009+u2009_b_,u2009_b_) or (_a_,u2009_a_u2009+u2009_b_) from the given pair in a single step. Let the initial pair of numbers be (1,1). Your task is to find number _k_, that is, the least number of steps needed to transform (1,1) into the pair where at least one number equals _n_. Input The input contains the only integer _n_ (1u2009β€u2009_n_u2009β€u2009106). Output Print the only integer _k_. Examples Input 5 Output 3 Input 1 Output 0 Note The pair (1,1) can be transformed into a pair containing 5 in three moves: (1,1) u2009βu2009 (1,2) u2009βu2009 (3,2) u2009βu2009 (5,2). | 1,900 | true | false | false | false | false | false | true | false | false | false | 9,339 |
1342C | Problem - 1342C - 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 number theory *1600 No tag edit access β Contest materials bmod b) e ((x bmod b) bmod a)$$$. Calculate the answer for each query. Recall that $$$y bmod z$$$ is the remainder of the division of $$$y$$$ by $$$z$$$. For example, $$$5 bmod 3 = 2$$$, $$$7 bmod 8 = 7$$$, $$$9 bmod 4 = 1$$$, $$$9 bmod 9 = 0$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$)xa0β the number of test cases. Then the test cases follow. The first line of each test case contains three integers $$$a$$$, $$$b$$$ and $$$q$$$ ($$$1 le a, b le 200$$$; $$$1 le q le 500$$$). Then $$$q$$$ lines follow, each containing two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le 10^{18}$$$) for the corresponding query. Output For each test case, print $$$q$$$ integersxa0β the answers to the queries of this test case in the order they appear. Example Input 2 4 6 5 1 1 1 3 1 5 1 7 1 9 7 10 2 7 8 100 200 Output 0 0 0 2 4 0 91 | 1,600 | true | false | false | false | false | false | false | false | false | false | 3,997 |
1691D | You are given an array $$$a$$$ of $$$n$$$ integers. You are asked to find out if the inequality $$$$$$max(a_i, a_{i + 1}, ldots, a_{j - 1}, a_{j}) geq a_i + a_{i + 1} + dots + a_{j - 1} + a_{j}$$$$$$ holds for all pairs of indices $$$(i, j)$$$, where $$$1 leq i leq j leq n$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) xa0β the size of the array. The next line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, on a new line output "YES" if the condition is satisfied for the given array, and "NO" otherwise. You can print each letter in any case (upper or lower). Example Input 3 4 -1 1 -1 2 5 -1 2 -3 2 -1 3 2 3 -1 Note In test cases $$$1$$$ and $$$2$$$, the given condition is satisfied for all $$$(i, j)$$$ pairs. In test case $$$3$$$, the condition isn't satisfied for the pair $$$(1, 2)$$$ as $$$max(2, 3) < 2 + 3$$$. | 1,800 | false | false | true | false | true | true | false | true | false | false | 2,133 |
555C | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom. A bar of chocolate can be presented as an _n_u2009Γu2009_n_ table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to _n_ from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following _q_ actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge. After each action, he wants to know how many pieces he ate as a result of this action. Input The first line contains integers _n_ (1u2009β€u2009_n_u2009β€u2009109) and _q_ (1u2009β€u2009_q_u2009β€u20092Β·105) β the size of the chocolate bar and the number of actions. Next _q_ lines contain the descriptions of the actions: the _i_-th of them contains numbers _x__i_ and _y__i_ (1u2009β€u2009_x__i_,u2009_y__i_u2009β€u2009_n_, _x__i_u2009+u2009_y__i_u2009=u2009_n_u2009+u20091) β the numbers of the column and row of the chosen cell and the character that represents the direction (L β left, U β up). Output Print _q_ lines, the _i_-th of them should contain the number of eaten pieces as a result of the _i_-th action. Examples Input 6 5 3 4 U 6 1 L 2 5 L 1 6 U 4 3 U Input 10 6 2 9 U 10 1 U 1 10 U 8 3 L 10 1 L 6 5 U Note Pictures to the sample tests: The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten. In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | 2,200 | false | false | false | false | true | false | false | false | false | false | 7,613 |
1223D | You are given a sequence $$$a_1, a_2, dots, a_n$$$, consisting of integers. You can apply the following operation to this sequence: choose some integer $$$x$$$ and move all elements equal to $$$x$$$ either to the beginning, or to the end of $$$a$$$. Note that you have to move all these elements in one direction in one operation. For example, if $$$a = [2, 1, 3, 1, 1, 3, 2]$$$, you can get the following sequences in one operation (for convenience, denote elements equal to $$$x$$$ as $$$x$$$-elements): $$$[1, 1, 1, 2, 3, 3, 2]$$$ if you move all $$$1$$$-elements to the beginning; $$$[2, 3, 3, 2, 1, 1, 1]$$$ if you move all $$$1$$$-elements to the end; $$$[2, 2, 1, 3, 1, 1, 3]$$$ if you move all $$$2$$$-elements to the beginning; $$$[1, 3, 1, 1, 3, 2, 2]$$$ if you move all $$$2$$$-elements to the end; $$$[3, 3, 2, 1, 1, 1, 2]$$$ if you move all $$$3$$$-elements to the beginning; $$$[2, 1, 1, 1, 2, 3, 3]$$$ if you move all $$$3$$$-elements to the end; You have to determine the minimum number of such operations so that the sequence $$$a$$$ becomes sorted in non-descending order. Non-descending order means that for all $$$i$$$ from $$$2$$$ to $$$n$$$, the condition $$$a_{i-1} le a_i$$$ is satisfied. Note that you have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$)xa0β the number of the queries. Each query is represented by two consecutive lines. The first line of each query contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0β the number of elements. The second line of each query contains $$$n$$$ integers $$$a_1, a_2, dots , a_n$$$ ($$$1 le a_i le n$$$)xa0β the elements. It is guaranteed that the sum of all $$$n$$$ does not exceed $$$3 cdot 10^5$$$. Output For each query print one integerxa0β the minimum number of operation for sorting sequence $$$a$$$ in non-descending order. Example Input 3 7 3 1 6 6 3 1 1 8 1 1 4 4 4 7 8 8 7 4 2 5 2 6 2 7 Note In the first query, you can move all $$$1$$$-elements to the beginning (after that sequence turn into $$$[1, 1, 1, 3, 6, 6, 3]$$$) and then move all $$$6$$$-elements to the end. In the second query, the sequence is sorted initially, so the answer is zero. In the third query, you have to move all $$$2$$$-elements to the beginning. | 2,000 | false | true | false | true | false | false | false | false | false | false | 4,556 |
1709E | You are given a tree consisting of $$$n$$$ vertices. A number is written on each vertex; the number on vertex $$$i$$$ is equal to $$$a_i$$$. Recall that a simple path is a path that visits each vertex at most once. Let the weight of the path be the bitwise XOR of the values written on vertices it consists of. Let's say that a tree is good if no simple path has weight $$$0$$$. You can apply the following operation any number of times (possibly, zero): select a vertex of the tree and replace the value written on it with an arbitrary positive integer. What is the minimum number of times you have to apply this operation in order to make the tree good? Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the number of vertices. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i < 2^{30}$$$)xa0β the numbers written on vertices. Then $$$n - 1$$$ lines follow, each containing two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n; x e y$$$) denoting an edge connecting vertex $$$x$$$ with vertex $$$y$$$. It is guaranteed that these edges form a tree. Output Print a single integerxa0β the minimum number of times you have to apply the operation in order to make the tree good. Examples Input 6 3 2 1 3 2 1 4 5 3 4 1 4 2 1 6 1 Input 4 2 1 1 1 1 2 1 3 1 4 Input 5 2 2 2 2 2 1 2 2 3 3 4 4 5 Note In the first example, it is enough to replace the value on the vertex $$$1$$$ with $$$13$$$, and the value on the vertex $$$4$$$ with $$$42$$$. | 2,400 | false | true | false | false | true | false | false | false | false | false | 2,022 |
1726B | Mainak has two positive integers $$$n$$$ and $$$m$$$. Mainak finds a sequence $$$a_1, a_2, ldots, a_n$$$ of $$$n$$$ positive integers interesting, if for all integers $$$i$$$ ($$$1 le i le n$$$), the , $$$[4,1,1,2,4]$$$ ($$$p_1 = 1 oplus 1 oplus 2 = 2 e 0$$$), $$$[29,30,30]$$$ ($$$p_2 = 29 e 0$$$) aren't interesting. Here $$$a oplus b$$$ denotes bitwise XOR of integers $$$a$$$ and $$$b$$$. Find any interesting sequence $$$a_1, a_2, ldots, a_n$$$ (or report that there exists no such sequence) such that the sum of the elements in the sequence $$$a$$$ is equal to $$$m$$$, i.e. $$$a_1 + a_2 ldots + a_n = m$$$. As a reminder, the bitwise XOR of an empty sequence is considered to be $$$0$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0β the number of test cases. Description of the test cases follows. The first line and the only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5$$$, $$$1 le m le 10^9$$$) β the length of the sequence and the sum of the elements. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, if there exists some interesting sequence, output "Yes" on the first line, otherwise output "No". You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer). If the answer is "Yes", output $$$n$$$ positive integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i ge 1$$$), forming an interesting sequence such that $$$a_1 + a_2 ldots + a_n = m$$$. If there are multiple solutions, output any. | 1,100 | true | false | false | false | false | true | false | false | false | false | 1,932 |
756C | Nikita has a stack. A stack in this problem is a data structure that supports two operations. Operation push(x) puts an integer _x_ on the top of the stack, and operation pop() deletes the top integer from the stack, i.xa0e. the last added. If the stack is empty, then the operation pop() does nothing. Nikita made _m_ operations with the stack but forgot them. Now Nikita wants to remember them. He remembers them one by one, on the _i_-th step he remembers an operation he made _p__i_-th. In other words, he remembers the operations in order of some permutation _p_1,u2009_p_2,u2009...,u2009_p__m_. After each step Nikita wants to know what is the integer on the top of the stack after performing the operations he have already remembered, in the corresponding order. Help him! Input The first line contains the integer _m_ (1u2009β€u2009_m_u2009β€u2009105)xa0β the number of operations Nikita made. The next _m_ lines contain the operations Nikita remembers. The _i_-th line starts with two integers _p__i_ and _t__i_ (1u2009β€u2009_p__i_u2009β€u2009_m_, _t__i_u2009=u20090 or _t__i_u2009=u20091)xa0β the index of operation he remembers on the step _i_, and the type of the operation. _t__i_ equals 0, if the operation is pop(), and 1, is the operation is push(x). If the operation is push(x), the line also contains the integer _x__i_ (1u2009β€u2009_x__i_u2009β€u2009106)xa0β the integer added to the stack. It is guaranteed that each integer from 1 to _m_ is present exactly once among integers _p__i_. Output Print _m_ integers. The integer _i_ should equal the number on the top of the stack after performing all the operations Nikita remembered on the steps from 1 to _i_. If the stack is empty after performing all these operations, print -1. Examples Input 5 5 0 4 0 3 1 1 2 1 1 1 1 2 Note In the first example, after Nikita remembers the operation on the first step, the operation push(2) is the only operation, so the answer is 2. After he remembers the operation pop() which was done before push(2), answer stays the same. In the second example, the operations are push(2), push(3) and pop(). Nikita remembers them in the order they were performed. In the third example Nikita remembers the operations in the reversed order. | 2,200 | false | false | false | false | true | false | false | false | false | false | 6,768 |
142A | Once upon a time in the Kingdom of Far Far Away lived Sam the Farmer. Sam had a cow named Dawn and he was deeply attached to her. Sam would spend the whole summer stocking hay to feed Dawn in winter. Sam scythed hay and put it into haystack. As Sam was a bright farmer, he tried to make the process of storing hay simpler and more convenient to use. He collected the hay into cubical hay blocks of the same size. Then he stored the blocks in his barn. After a summer spent in hard toil Sam stored _A_Β·_B_Β·_C_ hay blocks and stored them in a barn as a rectangular parallelepiped _A_ layers high. Each layer had _B_ rows and each row had _C_ blocks. At the end of the autumn Sam came into the barn to admire one more time the hay he'd been stacking during this hard summer. Unfortunately, Sam was horrified to see that the hay blocks had been carelessly scattered around the barn. The place was a complete mess. As it turned out, thieves had sneaked into the barn. They completely dissembled and took away a layer of blocks from the parallelepiped's front, back, top and sides. As a result, the barn only had a parallelepiped containing (_A_u2009-u20091)u2009Γu2009(_B_u2009-u20092)u2009Γu2009(_C_u2009-u20092) hay blocks. To hide the evidence of the crime, the thieves had dissembled the parallelepiped into single 1u2009Γu20091u2009Γu20091 blocks and scattered them around the barn. After the theft Sam counted _n_ hay blocks in the barn but he forgot numbers _A_, _B_ ΠΈ _C_. Given number _n_, find the minimally possible and maximally possible number of stolen hay blocks. Input The only line contains integer _n_ from the problem's statement (1u2009β€u2009_n_u2009β€u2009109). Output Print space-separated minimum and maximum number of hay blocks that could have been stolen by the thieves. Note that the answer to the problem can be large enough, so you must use the 64-bit integer type for calculations. Please, do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. Note Let's consider the first sample test. If initially Sam has a parallelepiped consisting of 32u2009=u20092u2009Γu20094u2009Γu20094 hay blocks in his barn, then after the theft the barn has 4u2009=u2009(2u2009-u20091)u2009Γu2009(4u2009-u20092)u2009Γu2009(4u2009-u20092) hay blocks left. Thus, the thieves could have stolen 32u2009-u20094u2009=u200928 hay blocks. If Sam initially had a parallelepiped consisting of 45u2009=u20095u2009Γu20093u2009Γu20093 hay blocks in his barn, then after the theft the barn has 4u2009=u2009(5u2009-u20091)u2009Γu2009(3u2009-u20092)u2009Γu2009(3u2009-u20092) hay blocks left. Thus, the thieves could have stolen 45u2009-u20094u2009=u200941 hay blocks. No other variants of the blocks' initial arrangement (that leave Sam with exactly 4 blocks after the theft) can permit the thieves to steal less than 28 or more than 41 blocks. | 1,600 | true | false | false | false | false | false | true | false | false | false | 9,307 |
745B | Hongcow likes solving puzzles. One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an _n_ by _m_ grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified. The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap. You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. Input The first line of input will contain two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u2009500), the dimensions of the puzzle piece. The next _n_ lines will describe the jigsaw piece. Each line will have length _m_ and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. Output Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. Examples Input 5 5 ..... ..X.. ..... ..... ..... Note For the first sample, one example of a rectangle we can form is as follows 111222 111222 For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle. In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: ..... ..XX. ..... ..... ..... | 1,400 | false | false | true | false | false | false | false | false | false | false | 6,818 |
1228F | You have an integer $$$n$$$. Let's define following tree generation as McDic's generation: 1. Make a complete and full binary tree of $$$2^{n} - 1$$$ vertices. Complete and full binary tree means a tree that exactly one vertex is a root, all leaves have the same depth (distance from the root), and all non-leaf nodes have exactly two child nodes. 2. Select a non-root vertex $$$v$$$ from that binary tree. 3. Remove $$$v$$$ from tree and make new edges between $$$v$$$'s parent and $$$v$$$'s direct children. If $$$v$$$ has no children, then no new edges will be made. You have a tree. Determine if this tree can be made by McDic's generation. If yes, then find the parent vertex of removed vertex in tree. Input The first line contains integer $$$n$$$ ($$$2 le n le 17$$$). The $$$i$$$-th of the next $$$2^{n} - 3$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 le a_{i} lt b_{i} le 2^{n} - 2$$$)xa0β meaning there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that the given edges form a tree. Output Print two lines. In the first line, print a single integerxa0β the number of answers. If given tree cannot be made by McDic's generation, then print $$$0$$$. In the second line, print all possible answers in ascending order, separated by spaces. If the given tree cannot be made by McDic's generation, then don't print anything. Examples Input 4 1 2 1 3 2 4 2 5 3 6 3 13 3 14 4 7 4 8 5 9 5 10 6 11 6 12 Input 3 1 2 2 3 3 4 4 5 5 6 Note In the first example, $$$3$$$ is the only possible answer. In the second example, there are $$$2$$$ possible answers. In the third example, the tree can't be generated by McDic's generation. | 2,500 | false | false | true | false | false | true | false | false | false | false | 4,530 |
2003B | Turtle and Piggy are playing a game on a sequence. They are given a sequence $$$a_1, a_2, ldots, a_n$$$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.). The game goes as follows: Let the current length of the sequence be $$$m$$$. If $$$m = 1$$$, the game ends. If the game does not end and it's Turtle's turn, then Turtle must choose an integer $$$i$$$ such that $$$1 le i le m - 1$$$, set $$$a_i$$$ to $$$max(a_i, a_{i + 1})$$$, and remove $$$a_{i + 1}$$$. If the game does not end and it's Piggy's turn, then Piggy must choose an integer $$$i$$$ such that $$$1 le i le m - 1$$$, set $$$a_i$$$ to $$$min(a_i, a_{i + 1})$$$, and remove $$$a_{i + 1}$$$. Turtle wants to maximize the value of $$$a_1$$$ in the end, while Piggy wants to minimize the value of $$$a_1$$$ in the end. Find the value of $$$a_1$$$ in the end if both players play optimally. You can refer to notes for further clarification. 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 a single integer $$$n$$$ ($$$2 le n le 10^5$$$) β the length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^5$$$) β the elements of the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer β the value of $$$a_1$$$ in the end if both players play optimally. Example Input 5 2 1 2 3 1 1 2 3 1 2 3 5 3 1 2 2 3 10 10 2 5 2 7 9 2 5 10 7 Note In the first test case, initially $$$a = [1, 2]$$$. Turtle can only choose $$$i = 1$$$. Then he will set $$$a_1$$$ to $$$max(a_1, a_2) = 2$$$ and remove $$$a_2$$$. The sequence $$$a$$$ becomes $$$[2]$$$. Then the length of the sequence becomes $$$1$$$, and the game will end. The value of $$$a_1$$$ is $$$2$$$, so you should output $$$2$$$. In the second test case, one of the possible game processes is as follows: Initially $$$a = [1, 1, 2]$$$. Turtle can choose $$$i = 2$$$. Then he will set $$$a_2$$$ to $$$max(a_2, a_3) = 2$$$ and remove $$$a_3$$$. The sequence $$$a$$$ will become $$$[1, 2]$$$. Piggy can choose $$$i = 1$$$. Then he will set $$$a_1$$$ to $$$min(a_1, a_2) = 1$$$ and remove $$$a_2$$$. The sequence $$$a$$$ will become $$$[1]$$$. The length of the sequence becomes $$$1$$$, so the game will end. The value of $$$a_1$$$ will be $$$1$$$ in the end. In the fourth test case, one of the possible game processes is as follows: Initially $$$a = [3, 1, 2, 2, 3]$$$. Turtle can choose $$$i = 4$$$. Then he will set $$$a_4$$$ to $$$max(a_4, a_5) = 3$$$ and remove $$$a_5$$$. The sequence $$$a$$$ will become $$$[3, 1, 2, 3]$$$. Piggy can choose $$$i = 3$$$. Then he will set $$$a_3$$$ to $$$min(a_3, a_4) = 2$$$ and remove $$$a_4$$$. The sequence $$$a$$$ will become $$$[3, 1, 2]$$$. Turtle can choose $$$i = 2$$$. Then he will set $$$a_2$$$ to $$$max(a_2, a_3) = 2$$$ and remove $$$a_3$$$. The sequence $$$a$$$ will become $$$[3, 2]$$$. Piggy can choose $$$i = 1$$$. Then he will set $$$a_1$$$ to $$$min(a_1, a_2) = 2$$$ and remove $$$a_2$$$. The sequence $$$a$$$ will become $$$[2]$$$. The length of the sequence becomes $$$1$$$, so the game will end. The value of $$$a_1$$$ will be $$$2$$$ in the end. | 800 | false | true | false | false | false | false | false | false | true | false | 238 |
702A | Problem - 702A - Codeforces =============== xa0 β the number of integers. The second line contains _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009109). Output Print the maximum length of an increasing subarray of the given array. Examples Input 5 1 7 2 11 15 Output 3 Input 6 100 100 100 100 100 100 Output 1 Input 3 1 2 3 Output 3 | 800 | false | true | true | true | false | false | false | false | false | false | 7,017 |
121C | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya dreamt of a lexicographically _k_-th permutation of integers from 1 to _n_. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers. Input The first line contains two integers _n_ and _k_ (1u2009β€u2009_n_,u2009_k_u2009β€u2009109) β the number of elements in the permutation and the lexicographical number of the permutation. Output If the _k_-th permutation of numbers from 1 to _n_ does not exist, print the single number "-1" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes _i_, that _i_ and _a__i_ are both lucky numbers. Note A permutation is an ordered set of _n_ elements, where each integer from 1 to _n_ occurs exactly once. The element of permutation in position with index _i_ is denoted as _a__i_ (1u2009β€u2009_i_u2009β€u2009_n_). Permutation _a_ is lexicographically smaller that permutation _b_ if there is such a _i_ (1u2009β€u2009_i_u2009β€u2009_n_), that _a__i_u2009<u2009_b__i_, and for any _j_ (1u2009β€u2009_j_u2009<u2009_i_) _a__j_u2009=u2009_b__j_. Let's make a list of all possible permutations of _n_ elements and sort it in the order of lexicographical increasing. Then the lexicographically _k_-th permutation is the _k_-th element of this list of permutations. In the first sample the permutation looks like that: 1 2 3 4 6 7 5 The only suitable position is 4. In the second sample the permutation looks like that: 2 1 3 4 The only suitable position is 4. | 1,900 | false | false | false | false | false | false | true | false | false | false | 9,394 |
1899F | Tree is a connected graph without cycles. It can be shown that any tree of $$$n$$$ vertices has exactly $$$n - 1$$$ edges. Leaf is a vertex in the tree with exactly one edge connected to it. Distance between two vertices $$$u$$$ and $$$v$$$ in a tree is the minimum number of edges that must be passed to come from vertex $$$u$$$ to vertex $$$v$$$. Alex's birthday is coming up, and Timofey would like to gift him a tree of $$$n$$$ vertices. However, Alex is a very moody boy. Every day for $$$q$$$ days, he will choose an integer, denoted by the integer chosen on the $$$i$$$-th day by $$$d_i$$$. If on the $$$i$$$-th day there are not two leaves in the tree at a distance exactly $$$d_i$$$, Alex will be disappointed. Timofey decides to gift Alex a designer so that he can change his tree as he wants. Timofey knows that Alex is also lazy (a disaster, not a human being), so at the beginning of every day, he can perform no more than one operation of the following kind: Choose vertices $$$u$$$, $$$v_1$$$, and $$$v_2$$$ such that there is an edge between $$$u$$$ and $$$v_1$$$ and no edge between $$$u$$$ and $$$v_2$$$. Then remove the edge between $$$u$$$ and $$$v_1$$$ and add an edge between $$$u$$$ and $$$v_2$$$. This operation cannot be performed if the graph is no longer a tree after it. Somehow Timofey managed to find out all the $$$d_i$$$. After that, he had another brilliant ideaxa0β just in case, make an instruction manual for the set, one that Alex wouldn't be disappointed. Timofey is not as lazy as Alex, but when he saw the integer $$$n$$$, he quickly lost the desire to develop the instruction and the original tree, so he assigned this task to you. It can be shown that a tree and a sequence of operations satisfying the described conditions always exist. Here is an example of an operation where vertices were selected: $$$u$$$xa0β $$$6$$$, $$$v_1$$$xa0β $$$1$$$, $$$v_2$$$xa0β $$$4$$$. Input The first line contains the integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ ($$$3 leq n leq 500$$$) and $$$q$$$ ($$$1 leq q leq 500$$$)xa0β the number of nodes in the tree and the number of days, respectively. The $$$i$$$th of the following $$$q$$$ lines contains the integer $$$d_i$$$ ($$$2 leq d_i leq n - 1$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. The same is guaranteed for $$$q$$$. It can be shown that a tree and a sequence of operations satisfying the described conditions always exist. Output For each test case, first print an $$$n - 1$$$ string describing the edges of the tree. If you want the tree to have an edge between nodes $$$u$$$ and $$$v$$$, there must be a string $$$v$$$ $$$u$$$ or $$$u$$$ $$$v$$$ among these $$$n - 1$$$ lines. In the next $$$q$$$ lines, print three integers each $$$u$$$ $$$v_1$$$ $$$v_2$$$xa0β a description of the operations. If Alex doesn't need to perform an operation the following day, print $$$-1$$$ $$$-1$$$ $$$-1$$$. | 1,600 | false | true | false | false | false | true | false | false | false | true | 917 |
1213B | Polycarp analyzes the prices of the new berPhone. At his disposal are the prices for $$$n$$$ last days: $$$a_1, a_2, dots, a_n$$$, where $$$a_i$$$ is the price of berPhone on the day $$$i$$$. Polycarp considers the price on the day $$$i$$$ to be bad if later (that is, a day with a greater number) berPhone was sold at a lower price. For example, if $$$n=6$$$ and $$$a=[3, 9, 4, 6, 7, 5]$$$, then the number of days with a bad price is $$$3$$$ β these are days $$$2$$$ ($$$a_2=9$$$), $$$4$$$ ($$$a_4=6$$$) and $$$5$$$ ($$$a_5=7$$$). Print the number of days with a bad price. You have to answer $$$t$$$ independent data sets. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10000$$$) β the number of sets of input data in the test. Input data sets must be processed independently, one after another. Each input data set consists of two lines. The first line contains an integer $$$n$$$ ($$$1 le n le 150000$$$) β the number of days. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$), where $$$a_i$$$ is the price on the $$$i$$$-th day. It is guaranteed that the sum of $$$n$$$ over all data sets in the test does not exceed $$$150000$$$. Output Print $$$t$$$ integers, the $$$j$$$-th of which should be equal to the number of days with a bad price in the $$$j$$$-th input data set. Example Input 5 6 3 9 4 6 7 5 1 1000000 2 2 1 10 31 41 59 26 53 58 97 93 23 84 7 3 2 1 2 3 4 5 | 1,100 | false | false | true | false | true | false | false | false | false | false | 4,618 |
916E | To your surprise, Jamie is the final boss! Ehehehe. Jamie has given you a tree with _n_ vertices, numbered from 1 to _n_. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it. Jamie also gives you three types of queries on the tree: 1 _v_xa0β Change the tree's root to vertex with number _v_. 2 _u_ _v_ _x_xa0β For each vertex in the subtree of smallest size that contains _u_ and _v_, add _x_ to its value. 3 _v_xa0β Find sum of values of vertices in the subtree of vertex with number _v_. A subtree of vertex _v_ is a set of vertices such that _v_ lies on shortest path from this vertex to root of the tree. Pay attention that subtree of a vertex can change after changing the tree's root. Show your strength in programming to Jamie by performing the queries accurately! Input The first line of input contains two space-separated integers _n_ and _q_ (1u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_q_u2009β€u2009105)xa0β the number of vertices in the tree and the number of queries to process respectively. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009108u2009β€u2009_a__i_u2009β€u2009108)xa0β initial values of the vertices. Next _n_u2009-u20091 lines contains two space-separated integers _u__i_,u2009_v__i_ (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_) describing edge between vertices _u__i_ and _v__i_ in the tree. The following _q_ lines describe the queries. Each query has one of following formats depending on its type: 1 _v_ (1u2009β€u2009_v_u2009β€u2009_n_) for queries of the first type. 2 _u_ _v_ _x_ (1u2009β€u2009_u_,u2009_v_u2009β€u2009_n_,u2009u2009-u2009108u2009β€u2009_x_u2009β€u2009108) for queries of the second type. 3 _v_ (1u2009β€u2009_v_u2009β€u2009_n_) for queries of the third type. All numbers in queries' descriptions are integers. The queries must be carried out in the given order. It is guaranteed that the tree is valid. Output For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie. Examples Input 6 7 1 4 2 8 5 7 1 2 3 1 4 3 4 5 3 6 3 1 2 4 6 3 3 4 1 6 2 2 4 -5 1 4 3 3 Input 4 6 4 3 5 6 1 2 2 3 3 4 3 1 1 3 2 2 4 3 1 1 2 2 4 -3 3 1 Note The following picture shows how the tree varies after the queries in the first sample. | 2,400 | false | false | false | false | true | false | false | false | false | false | 6,064 |
1584A | Ivan decided to prepare for the test on solving integer equations. He noticed that all tasks in the test have the following form: You are given two positive integers $$$u$$$ and $$$v$$$, find any pair of integers (not necessarily positive) $$$x$$$, $$$y$$$, such that: $$$$$$frac{x}{u} + frac{y}{v} = frac{x + y}{u + v}.$$$$$$ The solution $$$x = 0$$$, $$$y = 0$$$ is forbidden, so you should find any solution with $$$(x, y) eq (0, 0)$$$. Please help Ivan to solve some equations of this form. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0β the number of test cases. The next lines contain descriptions of test cases. The only line of each test case contains two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq 10^9$$$) β the parameters of the equation. Output For each test case print two integers $$$x$$$, $$$y$$$ β a possible solution to the equation. It should be satisfied that $$$-10^{18} leq x, y leq 10^{18}$$$ and $$$(x, y) eq (0, 0)$$$. We can show that an answer always exists. If there are multiple possible solutions you can print any. Example Output -1 1 -4 9 -18 50 -4 9 Note In the first test case: $$$frac{-1}{1} + frac{1}{1} = 0 = frac{-1 + 1}{1 + 1}$$$. In the second test case: $$$frac{-4}{2} + frac{9}{3} = 1 = frac{-4 + 9}{2 + 3}$$$. In the third test case: $$$frac{-18}{3} + frac{50}{5} = 4 = frac{-18 + 50}{3 + 5}$$$. In the fourth test case: $$$frac{-4}{6} + frac{9}{9} = frac{1}{3} = frac{-4 + 9}{6 + 9}$$$. | 800 | true | false | false | false | false | false | false | false | false | false | 2,709 |
2025C | There's a new game Monocarp wants to play. The game uses a deck of $$$n$$$ cards, where the $$$i$$$-th card has exactly one integer $$$a_i$$$ written on it. At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the same number as on the card taken on the previous turn or a number that is one greater than the number on the card taken on the previous turn. In other words, if on the previous turn Monocarp took a card with the number $$$x$$$, then on the current turn he can take either a card with the number $$$x$$$ or a card with the number $$$x + 1$$$. Monocarp can take any card which meets that condition, regardless of its position in the deck. After Monocarp takes a card on the current turn, it is removed from the deck. According to the rules of the game, the number of distinct numbers written on the cards that Monocarp has taken must not exceed $$$k$$$. If, after a turn, Monocarp cannot take a card without violating the described rules, the game ends. Your task is to determine the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 200,000$$$)xa0β the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp takes. The second line contains a sequence of integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^{9}$$$), where $$$a_i$$$ is the number written on the $$$i$$$-th card. Additional constraint of the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$200,000$$$. Output For each test case, print the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck. Note In the first example, Monocarp needs to take any of the cards with the number $$$3$$$. On the next two turns, he needs to take the two remaining cards with the number $$$3$$$. On the next three turns, he needs to take three cards with the number $$$4$$$. After that, Monocarp will not be able to take any more cards from the deck, and he will have $$$6$$$ cards. | 1,300 | false | true | true | false | false | false | true | true | true | false | 121 |
817F | Problem - 817F - Codeforces =============== xa0 integer number which is not presented in the set. Input The first line contains one integer number _n_ (1u2009β€u2009_n_u2009β€u2009105). Next _n_ lines contain three integer numbers _t_,u2009_l_,u2009_r_ (1u2009β€u2009_t_u2009β€u20093,u20091u2009β€u2009_l_u2009β€u2009_r_u2009β€u20091018) β type of the query, left and right bounds. Output Print MEX of the set after each query. Examples Input 3 1 3 4 3 1 6 2 1 3 Output 1 3 1 Input 4 1 1 3 3 5 6 2 4 4 3 1 6 Output 4 4 4 1 Note Here are contents of the set after each query in the first example: 1. {3,u20094} β the interval | 2,300 | false | false | false | false | true | false | false | true | false | false | 6,488 |
219A | Problem - 219A - 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 strings *1000 No tag edit access β Contest materials ") ") . The second line contains _s_, all characters in _s_ are lowercase English letters. The string length _s_ satisfies the inequality 1u2009β€u2009_s_u2009β€u20091000, where _s_ is the length of string _s_. Output Rearrange the letters in string _s_ in such a way that the result is a _k_-string. Print the result on a single output line. If there are multiple solutions, print any of them. If the solution doesn't exist, print "-1" (without quotes). Examples Input 2 aazz Output azaz Input 3 abcabcabz Output -1 | 1,000 | false | false | true | false | false | false | false | false | false | false | 8,964 |
415B | Problem - 415B - 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 implementation math *1500 No tag edit access β Contest materials . The second line of input contains _n_ space-separated integers _x_1,u2009_x_2,u2009...,u2009_x__n_xa0(1u2009β€u2009_x__i_u2009β€u2009109). Output Output _n_ space-separated integers. The _i_-th of them is the number of tokens Mashmokh can save on the _i_-th day. Examples Input 5 1 4 12 6 11 9 1 Output 0 2 3 1 1 Input 3 1 2 1 2 3 Output 1 0 1 Input 1 1 1 1 Output 0 | 1,500 | true | true | true | false | false | false | false | true | false | false | 8,163 |
2041F | Peter loves folding segments. There is a segment on a number line occupying the interval $$$[ell, r]$$$. Since it is the prime time for folding the segments, Peter decides to fold the segment carefully. In each step, he chooses one of the two following operations whenever possible: 1. Operation $$$ t{LTR}$$$: he folds the segment from left to right, where $$$ell$$$ coincides with a point $$$x$$$ ($$$ell < x le r$$$) such that $$$ell+x$$$ is a prime number$$$^{ ext{β}}$$$. When Peter chooses this operation, he always chooses the largest possible value $$$x$$$. Note that the segment occupies the interval $$$[frac{1}{2}(ell+x), r]$$$ afterwards. 2. Operation $$$ t{RTL}$$$: he folds the segment from right to left, where $$$r$$$ coincides with a point $$$x$$$ ($$$ell le x < r$$$) such that $$$r+x$$$ is a prime number. When Peter chooses this operation, he always chooses the smallest possible value $$$x$$$. Note that the segment occupies the interval $$$[ell, frac{1}{2}(r+x)]$$$ afterwards. A folding sequence refers to a sequence of operations specified above. Peter wants to fold the segment several times, resulting in the shortest possible interval whose length that cannot be further reduced. The length of an interval $$$[ell, r]$$$ is defined naturally to be $$$r-ell$$$. Let's consider the following example. Suppose that we are folding a segment initially occupying the interval $$$[1, 30]$$$. There are three folding sequences that lead to the shortest possible resulting interval, as shown in the following figure. Please help Peter determine the number of folding sequences such that the resulting interval has a shortest possible length. Output the number modulo $$$998244353$$$. $$$^{ ext{β}}$$$Recall that an integer $$$p>1$$$ is a prime number if there do not exist integers $$$a, b>1$$$ such that $$$p=ab$$$. Input The first line contains an integer $$$t$$$, denoting the number of test cases. In each of the following $$$t$$$ lines, there are two integers $$$ell$$$ and $$$r$$$. $$$1 le t le 10$$$ $$$1 le ell < r le 10^{12}$$$ $$$r - ell le 10^5$$$ Output For each test case, please output a line denoting the number of ways to fold the given segment such that the resulting segment has the shortest possible length, modulo $$$998244353$$$. Example Input 3 1 30 16 18 142857 240135 | 2,400 | false | false | false | false | false | false | true | false | false | false | 8 |
980B | The city of Fishtopia can be imagined as a grid of $$$4$$$ rows and an odd number of columns. It has two main villages; the first is located at the top-left cell $$$(1,1)$$$, people who stay there love fishing at the Tuna pond at the bottom-right cell $$$(4, n)$$$. The second village is located at $$$(4, 1)$$$ and its people love the Salmon pond at $$$(1, n)$$$. The mayor of Fishtopia wants to place $$$k$$$ hotels in the city, each one occupying one cell. To allow people to enter the city from anywhere, hotels should not be placed on the border cells. A person can move from one cell to another if those cells are not occupied by hotels and share a side. Can you help the mayor place the hotels in a way such that there are equal number of shortest paths from each village to its preferred pond? Input The first line of input contain two integers, $$$n$$$ and $$$k$$$ ($$$3 leq n leq 99$$$, $$$0 leq k leq 2 imes(n-2)$$$), $$$n$$$ is odd, the width of the city, and the number of hotels to be placed, respectively. Output Print "YES", if it is possible to place all the hotels in a way that satisfies the problem statement, otherwise print "NO". If it is possible, print an extra $$$4$$$ lines that describe the city, each line should have $$$n$$$ characters, each of which is "#" if that cell has a hotel on it, or "." if not. Examples Output YES ....... .#..... .#..... ....... Output YES ..... .###. ..... ..... | 1,600 | false | false | false | false | false | true | false | false | false | false | 5,805 |
1208B | You are given an array $$$a_{1}, a_{2}, ldots, a_{n}$$$. You can remove at most one subsegment from it. The remaining elements should be pairwise distinct. In other words, at most one time you can choose two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$) and delete integers $$$a_l, a_{l+1}, ldots, a_r$$$ from the array. Remaining elements should be pairwise distinct. Find the minimum size of the subsegment you need to remove to make all remaining elements distinct. Input The first line of the input contains a single integer $$$n$$$ ($$$1 le n le 2000$$$)xa0β the number of elements in the given array. The next line contains $$$n$$$ spaced integers $$$a_{1}, a_{2}, ldots, a_{n}$$$ ($$$1 le a_{i} le 10^{9}$$$)xa0β the elements of the array. Output Print a single integerxa0β the minimum size of the subsegment you need to remove to make all elements of the array pairwise distinct. If no subsegment needs to be removed, print $$$0$$$. Note In the first example all the elements are already distinct, therefore no subsegment needs to be removed. In the second example you can remove the subsegment from index $$$2$$$ to $$$3$$$. In the third example you can remove the subsegments from index $$$1$$$ to $$$2$$$, or from index $$$2$$$ to $$$3$$$, or from index $$$3$$$ to $$$4$$$. | 1,500 | false | false | true | false | false | false | true | true | false | false | 4,653 |
1857G | Given a tree consisting of $$$n$$$ vertices. A tree is a connected undirected graph without cycles. Each edge of the tree has its weight, $$$w_i$$$. Your task is to count the number of different graphs that satisfy all four conditions: 1. The graph does not have self-loops and multiple edges. 2. The weights on the edges of the graph are integers and do not exceed $$$S$$$. 3. The graph has exactly one xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$S$$$ ($$$2 le n le 2 cdot 10^5, 1le Sle 10^9$$$)xa0β the number of vertices and the upper bound of the weights. Then follow $$$n-1$$$ lines describing the tree, the $$$i$$$-th line contains three integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1le u_i,v_ile n, u_i e v_i, 1le w_ile S$$$)xa0β an edge in the tree with weight $$$w_i$$$. It is guaranteed that the sum of $$$n$$$ for all tests does not exceed $$$2cdot 10^5$$$. Output For each test, output the number of different graphs that satisfy the conditions, modulo $$$998244353$$$. Example Input 4 2 5 1 2 4 4 5 1 2 2 2 3 4 3 4 3 5 6 1 2 3 1 3 2 3 4 6 3 5 1 10 200 1 2 3 2 3 33 3 4 200 1 5 132 5 6 1 5 7 29 7 8 187 7 9 20 7 10 4 Note In the first sample, there is only one graph, which is the given tree. In the second samle, the given tree looks like this: All possible graphs for the second sample are shown below, the minimum spanning tree is highlighted in red: | 2,000 | false | true | false | false | false | false | false | false | true | true | 1,138 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.