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
1982A
Dima loves watching soccer. In such a game, the score on the scoreboard is represented as $$$x$$$ : $$$y$$$, where $$$x$$$ is the number of goals of the first team, and $$$y$$$ is the number of goals of the second team. At any given time, only one team can score a goal, so the score $$$x$$$ : $$$y$$$ can change to either $$$(x + 1)$$$ : $$$y$$$, or $$$x$$$ : $$$(y + 1)$$$. While watching a soccer game, Dima was distracted by very important matters, and after some time, he returned to watching the game. Dima remembers the score right before he was distracted, and the score right after he returned. Given these two scores, he wonders the following question. Is it possible that, while Dima was not watching the game, the teams never had an equal score? It is guaranteed that at neither of the two time points Dima remembers the teams had equal scores. However, it is possible that the score did not change during his absence. Help Dima and answer the question! Input Each test consists of several test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^{4}$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $$$x_{1}, y_{1}$$$ ($$$0 le x_{1}, y_{1} le 10^{9}$$$, $$$x_{1} eq y_{1}$$$)xa0— the score before Dima was distracted. The second line of each test case contains two integers $$$x_{2}, y_{2}$$$ ($$$x_{1} le x_{2} le 10^{9}$$$, $$$y_{1} le y_{2} le 10^{9}$$$, $$$x_{2} eq y_{2}$$$)xa0— the score when Dima returned. Output For each test case, output "YES" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 6 1 0 5 0 1 2 3 2 1 2 4 5 1 2 4 3 1 2 1 2 998244353 0 1000000000 999999999 Output YES NO YES NO YES YES Note In the first test case, the score before Dima left was $$$1$$$ : $$$0$$$. When he leaves, the first team scores several goals in a row until the score becomes $$$5$$$ : $$$0$$$, so the answer is YES. In the second test case, the score could only change as follows: $$$1$$$ : $$$2$$$ $$$2$$$ : $$$2$$$ $$$3$$$ : $$$2$$$ In this scenario, there is a moment when the teams have an equal score, so the answer is NO. In the third test case, one of the possible developments is: $$$1$$$ : $$$2$$$ $$$1$$$ : $$$3$$$ $$$2$$$ : $$$3$$$ $$$2$$$ : $$$4$$$ $$$2$$$ : $$$5$$$ $$$3$$$ : $$$5$$$ $$$4$$$ : $$$5$$$ In this scenario, there was no time when the score was equal, so the answer is YES.
800
true
true
true
false
false
false
false
false
true
false
396
1065C
There is a toy building consisting of $$$n$$$ towers. Each tower consists of several cubes standing on each other. The $$$i$$$-th tower consists of $$$h_i$$$ cubes, so it has height $$$h_i$$$. Let's define operation slice on some height $$$H$$$ as following: for each tower $$$i$$$, if its height is greater than $$$H$$$, then remove some top cubes to make tower's height equal to $$$H$$$. Cost of one "slice" equals to the total number of removed cubes from all towers. Let's name slice as good one if its cost is lower or equal to $$$k$$$ ($$$k ge n$$$). Calculate the minimum number of good slices you have to do to make all towers have the same height. Of course, it is always possible to make it so. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$n le k le 10^9$$$) — the number of towers and the restriction on slices, respectively. The second line contains $$$n$$$ space separated integers $$$h_1, h_2, dots, h_n$$$ ($$$1 le h_i le 2 cdot 10^5$$$) — the initial heights of towers. Output Print one integer — the minimum number of good slices you have to do to make all towers have the same heigth. Note In the first example it's optimal to make $$$2$$$ slices. The first slice is on height $$$2$$$ (its cost is $$$3$$$), and the second one is on height $$$1$$$ (its cost is $$$4$$$).
1,600
false
true
false
false
false
false
false
false
false
false
5,386
1304E
Gildong was hiking a mountain, walking by millions of trees. Inspired by them, he suddenly came up with an interesting idea for trees in data structures: What if we add another edge in a tree? Then he found that such tree-like graphs are called 1-trees. Since Gildong was bored of solving too many tree problems, he wanted to see if similar techniques in trees can be used in 1-trees as well. Instead of solving it by himself, he's going to test you by providing queries on 1-trees. First, he'll provide you a tree (not 1-tree) with $$$n$$$ vertices, then he will ask you $$$q$$$ queries. Each query contains $$$5$$$ integers: $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$, and $$$k$$$. This means you're asked to determine if there exists a path from vertex $$$a$$$ to $$$b$$$ that contains exactly $$$k$$$ edges after adding a bidirectional edge between vertices $$$x$$$ and $$$y$$$. A path can contain the same vertices and same edges multiple times. All queries are independent of each other; i.e. the added edge in a query is removed in the next query. Input The first line contains an integer $$$n$$$ ($$$3 le n le 10^5$$$), the number of vertices of the tree. Next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$, $$$u e v$$$) each, which means there is an edge between vertex $$$u$$$ and $$$v$$$. All edges are bidirectional and distinct. Next line contains an integer $$$q$$$ ($$$1 le q le 10^5$$$), the number of queries Gildong wants to ask. Next $$$q$$$ lines contain five integers $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$, and $$$k$$$ each ($$$1 le x,y,a,b le n$$$, $$$x e y$$$, $$$1 le k le 10^9$$$) – the integers explained in the description. It is guaranteed that the edge between $$$x$$$ and $$$y$$$ does not exist in the original tree. Output For each query, print "YES" if there exists a path that contains exactly $$$k$$$ edges from vertex $$$a$$$ to $$$b$$$ after adding an edge between vertices $$$x$$$ and $$$y$$$. Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 5 1 2 2 3 3 4 4 5 5 1 3 1 2 2 1 4 1 3 2 1 4 1 3 3 4 2 3 3 9 5 2 3 3 9 Note The image below describes the tree (circles and solid lines) and the added edges for each query (dotted lines). Possible paths for the queries with "YES" answers are: $$$1$$$-st query: $$$1$$$ – $$$3$$$ – $$$2$$$ $$$2$$$-nd query: $$$1$$$ – $$$2$$$ – $$$3$$$ $$$4$$$-th query: $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$ – $$$4$$$ – $$$2$$$ – $$$3$$$
2,000
false
false
false
false
true
false
false
false
false
false
4,167
1710D
Rhodoks has a tree with $$$n$$$ vertices, but he doesn't remember its structure. The vertices are indexed from $$$1$$$ to $$$n$$$. A segment $$$[l,r]$$$ ($$$1 leq l leq r leq n$$$) is good if the vertices with indices $$$l$$$, $$$l + 1$$$, ..., $$$r$$$ form a connected component in Rhodoks' tree. Otherwise, it is bad. For example, if the tree is the one in the picture, then only the segment $$$[3,4]$$$ is bad while all the other segments are good. For each of the $$$frac{n(n+1)}{2}$$$ segments, Rhodoks remembers whether it is good or bad. Can you help him recover the tree? If there are multiple solutions, print any. It is guaranteed that the there is at least one tree satisfying Rhodoks' description. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 1000$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2000$$$) — the number of vertices in the tree. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains a string $$$good_i$$$ of length $$$n+1-i$$$ consisting of 0 and 1. If the segment $$$[i,i+j-1]$$$ is good then the $$$j$$$-th character of $$$good_i$$$ is 1, otherwise $$$j$$$-th character of $$$good_i$$$ is 0. It is guaranteed that the there is at least one tree consistent with the given data. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, print $$$n-1$$$ lines describing the tree you recover. The $$$i$$$-th line should contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i,v_i leq n$$$), denoting an edge between vertices $$$u_i$$$ and $$$v_i$$$. If there are multiple solutions, print any. Example Input 3 4 1111 111 10 1 6 111111 11111 1111 111 11 1 12 100100000001 11100000001 1000000000 100000000 10010001 1110000 100000 10000 1001 111 10 1 Output 1 2 2 3 2 4 1 2 2 3 3 4 4 5 5 6 2 3 6 7 10 11 2 4 6 8 10 12 1 4 5 8 9 12 5 12 2 12 Note The first test case is explained in the statement. In the second test case, one possible tree is as follows: In the third test case, one possible tree is as follows:
3,400
false
false
false
false
false
true
false
false
false
false
2,017
1411D
Currently, XXOC's rap is a string consisting of zeroes, ones, and question marks. Unfortunately, haters gonna hate. They will write $$$x$$$ angry comments for every occurrence of subsequence 01 and $$$y$$$ angry comments for every occurrence of subsequence 10. You should replace all the question marks with 0 or 1 in such a way that the number of angry comments would be as small as possible. String $$$b$$$ is a subsequence of string $$$a$$$, if it can be obtained by removing some characters from $$$a$$$. Two occurrences of a subsequence are considered distinct if sets of positions of remaining characters are distinct. Input The first line contains string $$$s$$$xa0— XXOC's rap ($$$1 le s leq 10^5$$$). The second line contains two integers $$$x$$$ and $$$y$$$xa0— the number of angry comments XXOC will recieve for every occurrence of 01 and 10 accordingly ($$$0 leq x, y leq 10^6$$$). Output Output a single integerxa0— the minimum number of angry comments. Note In the first example one of the optimum ways to replace is 001. Then there will be $$$2$$$ subsequences 01 and $$$0$$$ subsequences 10. Total number of angry comments will be equal to $$$2 cdot 2 + 0 cdot 3 = 4$$$. In the second example one of the optimum ways to replace is 11111. Then there will be $$$0$$$ subsequences 01 and $$$0$$$ subsequences 10. Total number of angry comments will be equal to $$$0 cdot 13 + 0 cdot 37 = 0$$$. In the third example one of the optimum ways to replace is 1100. Then there will be $$$0$$$ subsequences 01 and $$$4$$$ subsequences 10. Total number of angry comments will be equal to $$$0 cdot 239 + 4 cdot 7 = 28$$$. In the fourth example one of the optimum ways to replace is 01101001. Then there will be $$$8$$$ subsequences 01 and $$$8$$$ subsequences 10. Total number of angry comments will be equal to $$$8 cdot 5 + 8 cdot 7 = 96$$$.
2,100
false
true
true
false
false
false
true
false
false
false
3,618
1941D
Rudolf and Bernard decided to play a game with their friends. $$$n$$$ people stand in a circle and start throwing a ball to each other. They are numbered from $$$1$$$ to $$$n$$$ in the clockwise order. Let's call a transition a movement of the ball from one player to his neighbor. The transition can be made clockwise or counterclockwise. Let's call the clockwise (counterclockwise) distance from player $$$y_1$$$ to player $$$y_2$$$ the number of transitions clockwise (counterclockwise) that need to be made to move from player $$$y_1$$$ to player $$$y_2$$$. For example, if $$$n=7$$$ then the clockwise distance from $$$2$$$ to $$$5$$$ is $$$3$$$, and the counterclockwise distance from $$$2$$$ to $$$5$$$ is $$$4$$$. Initially, the ball is with the player number $$$x$$$ (players are numbered clockwise). On the $$$i$$$-th move the person with the ball throws it at a distance of $$$r_i$$$ ($$$1 le r_i le n - 1$$$) clockwise or counterclockwise. For example, if there are $$$7$$$ players, and the $$$2$$$nd player, after receiving the ball, throws it a distance of $$$5$$$, then the ball will be caught by either the $$$7$$$th player (throwing clockwise) or the $$$4$$$th player (throwing counterclockwise). An illustration of this example is shown below. The game was interrupted after $$$m$$$ throws due to unexpected rain. When the rain stopped, the guys gathered again to continue. However, no one could remember who had the ball. As it turned out, Bernard remembered the distances for each of the throws and the direction for some of the throws (clockwise or counterclockwise). Rudolf asks you to help him and based on the information from Bernard, calculate the numbers of the players who could have the ball after $$$m$$$ throws. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains three integers $$$n, m, x$$$ ($$$2 le n le 1000$$$, $$$1 le m le 1000$$$, $$$1 le x le n$$$) — the number of players, the number of throws made, and the number of the player who threw the ball first, respectively. The next $$$m$$$ lines contain information about each throw in order. Each of them contains an integer $$$r_i$$$ ($$$1 le r_i le n - 1$$$) — the distance at which the $$$i$$$-th throw was made, and a symbol $$$c_i$$$, equal to '0', '1', or '?': if $$$c_i$$$='0', then the $$$i$$$-th throw was made clockwise, if $$$c_i$$$='1', then the $$$i$$$-th throw was made counterclockwise, if $$$c_i$$$='?', then Bernard does not remember the direction and the $$$i$$$-th throw could have been made either clockwise or counterclockwise. It is guaranteed that the sum $$$n cdot m$$$ ($$$n$$$ multiplied by $$$m$$$) over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two lines. In the first line, output the number of players $$$k$$$ ($$$1 le k le n$$$) who could have the ball at the end of the game. In the next line, output $$$k$$$ numbers $$$b_i$$$ ($$$1 le b_i le n$$$) — the numbers of the players in increasing order. All numbers must be different. Example Input 5 6 3 2 2 ? 2 ? 2 ? 12 1 2 3 1 10 7 4 2 ? 9 1 4 ? 7 0 2 0 8 1 5 ? 5 3 1 4 0 4 ? 1 ? 4 1 1 2 ? Output 3 2 4 6 1 11 4 3 5 7 9 3 2 3 5 1 3 Note Below is an illustration of three throws for the first test case. The arrows denote possible throw directions. Players who could have the ball after the throw are highlighted in gray.
1,200
false
false
true
true
false
false
false
false
false
false
636
2036D
One morning, Polycarp woke up and realized that $$$1543$$$ is the most favorite number in his life. The first thing that Polycarp saw that day as soon as he opened his eyes was a large wall carpet of size $$$n$$$ by $$$m$$$ cells; $$$n$$$ and $$$m$$$ are even integers. Each cell contains one of the digits from $$$0$$$ to $$$9$$$. Polycarp became curious about how many times the number $$$1543$$$ would appear in all layers$$$^{ ext{∗}}$$$ of the carpet when traversed clockwise. $$$^{ ext{∗}}$$$The first layer of a carpet of size $$$n imes m$$$ is defined as a closed strip of length $$$2 cdot (n+m-2)$$$ and thickness of $$$1$$$ element, surrounding its outer part. Each subsequent layer is defined as the first layer of the carpet obtained by removing all previous layers from the original carpet. Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$) — the number of test cases. The following lines describe the test cases. The first line of each test case contains a pair of numbers $$$n$$$ and $$$m$$$ ($$$2 leq n, m leq 10^3$$$, $$$n, m$$$xa0— even integers). This is followed by $$$n$$$ lines of length $$$m$$$, consisting of digits from $$$0$$$ to $$$9$$$ — the description of the carpet. It is guaranteed that the sum of $$$n cdot m$$$ across all test cases does not exceed $$$10^6$$$. Output For each test case, output a single number — the total number of times $$$1543$$$ appears in all layers of the carpet in the order of traversal clockwise. Example Input 8 2 4 1543 7777 2 4 7154 8903 2 4 3451 8888 2 2 54 13 2 2 51 43 2 6 432015 512034 4 4 5431 1435 5518 7634 6 4 5432 1152 4542 2432 2302 5942 Note Occurrences of $$$1543$$$ in the seventh example. Different layers are colored in different colors.
1,300
false
false
true
false
false
false
true
false
false
false
49
1790G
You are given an undirected connected graph, some vertices of which contain tokens and/or bonuses. Consider a game involving one player xa0— you. You can move tokens according to the following rules: At the beginning of the game, you can make exactly one turn: move any token to any adjacent vertex. If the movement of the token ended on the bonus, then you are allowed to make another turn with any other token. You can use different bonuses in any order. The same bonus can be used an unlimited number of times. Bonuses do not move during the game. There can be several tokens in one vertex at the same time, but initially there is no more than one token in each vertex. The vertex with number $$$1$$$ is the finish vertex, and your task is to determine whether it is possible to hit it with any token by making turns with the tiles according to the rules described above. If a token is initially located at the vertex of $$$1$$$, then the game is considered already won. The finish line is in black, the bonuses are in red, the chips are in grey. For example, for a given graph, you can reach the finish line with a chip from the $$$8$$$th vertex by making the following sequence of turns: 1. Move from the $$$8$$$-th vertex to the $$$6$$$-th. 2. Move from the $$$7$$$-th vertex to the $$$5$$$-th. 3. Move from the $$$6$$$-th vertex to the $$$4$$$-th. 4. Move from the $$$5$$$-th vertex to the $$$6$$$-th. 5. Move from the $$$4$$$-th vertex to the $$$2$$$-nd. 6. Move from the $$$6$$$-th vertex to the $$$4$$$-th. 7. Move from the $$$2$$$-nd vertex to the $$$1$$$-st vertex, which is the finish. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— number of test cases in the test. The descriptions of the test cases follow. The first line of the description of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le m le 2 cdot 10^5$$$)xa0— the number of vertices and edges in the graph, respectively. The second line of the description of each test case contains two integers $$$p$$$ and $$$b$$$ ($$$1 le p le n, 0 le b le n$$$)xa0— the number of tokens and bonuses, respectively. The third line of the description of each test case contains $$$p$$$ different integers from $$$1$$$ to $$$n$$$ — the indices of the vertices in which the tokens are located. The fourth line of the description of each input data set contains $$$b$$$ different integers from $$$1$$$ to $$$n$$$ — the indices of the vertices in which the bonuses are located. Note that the value of $$$b$$$ can be equal to $$$0$$$. In this case, this line is empty. There can be both a token and a bonus in one vertex at the same time. The next $$$m$$$ lines of the description of each test case contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$) — vertices connected by the $$$i$$$-th edge. There is at most one edge between each pair of vertices. The given graph is connected, that is, from any vertex you can get to any one by moving along the edges. The test cases are separated by an empty string. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Similarly, it is guaranteed that the sum of $$$m$$$ over all input data sets does not exceed $$$2 cdot 10^5$$$. Output For each test case, print YES in a separate line if you can reach the finish with some token, and NO otherwise. You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).
2,300
false
false
false
false
false
true
false
false
false
true
1,536
1760B
In order to write a string, Atilla needs to first learn all letters that are contained in the string. Atilla needs to write a message which can be represented as a string $$$s$$$. He asks you what is the minimum alphabet size required so that one can write this message. The alphabet of size $$$x$$$ ($$$1 leq x leq 26$$$) contains only the first $$$x$$$ Latin letters. For example an alphabet of size $$$4$$$ contains only the characters $$$ exttt{a}$$$, $$$ exttt{b}$$$, $$$ exttt{c}$$$ and $$$ exttt{d}$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters. Output For each test case, output a single integerxa0— the minimum alphabet size required to so that Atilla can write his message $$$s$$$. Example Input 5 1 a 4 down 10 codeforces 3 bcf 5 zzzzz Note For the first test case, Atilla needs to know only the character $$$ exttt{a}$$$, so the alphabet of size $$$1$$$ which only contains $$$ exttt{a}$$$ is enough. For the second test case, Atilla needs to know the characters $$$ exttt{d}$$$, $$$ exttt{o}$$$, $$$ exttt{w}$$$, $$$ exttt{n}$$$. The smallest alphabet size that contains all of them is $$$23$$$ (such alphabet can be represented as the string $$$ exttt{abcdefghijklmnopqrstuvw}$$$).
800
false
true
true
false
false
false
false
false
false
false
1,754
1554B
You are given $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ and an integer $$$k$$$. Find the maximum value of $$$i cdot j - k cdot (a_i a_j)$$$ over all pairs $$$(i, j)$$$ of integers with $$$1 le i < j le n$$$. Here, $$$$$$ is the xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ ($$$2 le n le 10^5$$$) and $$$k$$$ ($$$1 le k le min(n, 100)$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print a single integer xa0— the maximum possible value of $$$i cdot j - k cdot (a_i a_j)$$$. Example Input 4 3 3 1 1 3 2 2 1 2 4 3 0 1 2 3 6 6 3 2 0 0 5 6 Note Let $$$f(i, j) = i cdot j - k cdot (a_i a_j)$$$. In the first test case, $$$f(1, 2) = 1 cdot 2 - k cdot (a_1 a_2) = 2 - 3 cdot (1 1) = -1$$$. $$$f(1, 3) = 1 cdot 3 - k cdot (a_1 a_3) = 3 - 3 cdot (1 3) = -6$$$. $$$f(2, 3) = 2 cdot 3 - k cdot (a_2 a_3) = 6 - 3 cdot (1 3) = -3$$$. So the maximum is $$$f(1, 2) = -1$$$. In the fourth test case, the maximum is $$$f(3, 4) = 12$$$.
1,700
true
true
false
false
false
false
true
false
false
false
2,865
257B
Petya and Vasya decided to play a little. They found _n_ red cubes and _m_ blue cubes. The game goes like that: the players take turns to choose a cube of some color (red or blue) and put it in a line from left to right (overall the line will have _n_u2009+u2009_m_ cubes). Petya moves first. Petya's task is to get as many pairs of neighbouring cubes of the same color as possible. Vasya's task is to get as many pairs of neighbouring cubes of different colors as possible. The number of Petya's points in the game is the number of pairs of neighboring cubes of the same color in the line, the number of Vasya's points in the game is the number of neighbouring cubes of the different color in the line. Your task is to calculate the score at the end of the game (Petya's and Vasya's points, correspondingly), if both boys are playing optimally well. To "play optimally well" first of all means to maximize the number of one's points, and second — to minimize the number of the opponent's points. Input The only line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of red and blue cubes, correspondingly. Output On a single line print two space-separated integers — the number of Petya's and Vasya's points correspondingly provided that both players play optimally well. Note In the first test sample the optimal strategy for Petya is to put the blue cube in the line. After that there will be only red cubes left, so by the end of the game the line of cubes from left to right will look as [blue, red, red, red]. So, Petya gets 2 points and Vasya gets 1 point. If Petya would choose the red cube during his first move, then, provided that both boys play optimally well, Petya would get 1 point and Vasya would get 2 points.
1,300
false
true
true
false
false
false
false
false
false
false
8,806
1714C
Problem - 1714C - 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 *800 No tag edit access → Contest materials ") Editorial") . For example, if $$$s=20$$$, then the answer is $$$389$$$. This is the minimum number in which all digits are different and the sum of the digits is $$$20$$$ ($$$3+8+9=20$$$). For the given $$$s$$$ print the required number. Input The first line contains an integer $$$t$$$ ($$$1 le t le 45$$$) — the number of test cases. Each test case is specified by a line that contains the only integer $$$s$$$ ($$$1 le s le 45$$$). Output Print $$$t$$$ integers — the answers to the given test cases. Example Input 4 20 8 45 10 Output 389 8 123456789 19
800
false
true
false
false
false
false
false
false
false
false
1,998
1704G
Mio has an array $$$a$$$ consisting of $$$n$$$ integers, and an array $$$b$$$ consisting of $$$m$$$ integers. Mio can do the following operation to $$$a$$$: Choose an integer $$$i$$$ ($$$1 leq i leq n$$$) that has not been chosen before, then add $$$1$$$ to $$$a_i$$$, subtract $$$2$$$ from $$$a_{i+1}$$$, add $$$3$$$ to $$$a_{i+2}$$$ an so on. Formally, the operation is to add $$$(-1)^{j-i} cdot (j-i+1) $$$ to $$$a_j$$$ for $$$i leq j leq n$$$. Mio wants to transform $$$a$$$ so that it will contain $$$b$$$ as a subarray. Could you answer her question, and provide a sequence of operations to do so, if it is possible? An array $$$b$$$ is a subarray of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. 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. The description of test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, cdots, a_n$$$ ($$$-10^5 leq a_i leq 10^5$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the test case contains one integer $$$m$$$ ($$$2 leq m leq n$$$) — the number of elements in $$$b$$$. The fourth line of the test case contains $$$m$$$ integers $$$b_1, b_2, cdots, b_m$$$ ($$$-10^{12} leq b_i leq 10^{12}$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output If it is impossible to transform $$$a$$$ so that it contains $$$b$$$ as a subarray, output $$$-1$$$. Otherwise, the first line of output should contain an integer $$$k$$$ ($$$0 leq k leq n$$$), the number of operations to be done. The second line should contain $$$k$$$ distinct integers, representing the operations done in order. If there are multiple solutions, you can output any. Notice that you do not need to minimize the number of operations. Note In the first test case, the sequence $$$a$$$ = $$$[1,2,3,4,5]$$$. One of the possible solutions is doing one operation at $$$i = 1$$$ (add $$$1$$$ to $$$a_1$$$, subtract $$$2$$$ from $$$a_2$$$, add $$$3$$$ to $$$a_3$$$, subtract $$$4$$$ from $$$a_4$$$, add $$$5$$$ to $$$a_5$$$). Then array $$$a$$$ is transformed to $$$a$$$ = $$$[2,0,6,0,10]$$$, which contains $$$b$$$ = $$$[2, 0, 6, 0, 10]$$$ as a subarray. In the second test case, the sequence $$$a$$$ = $$$[1,2,3,4,5]$$$. One of the possible solutions is doing one operation at $$$i = 4$$$ (add $$$1$$$ to $$$a_4$$$, subtract $$$2$$$ from $$$a_5$$$). Then array $$$a$$$ is transformed to $$$a$$$ = $$$[1,2,3,5,3]$$$, which contains $$$b$$$ = $$$[3,5,3]$$$ as a subarray. In the third test case, the sequence $$$a$$$ = $$$[-3, 2, -3, -4, 4, 0, 1, -2]$$$. One of the possible solutions is the following. Choose an integer $$$i=8$$$ to do the operation. Then array $$$a$$$ is transformed to $$$a$$$ = $$$[-3, 2, -3, -4, 4, 0, 1, -1]$$$. Choose an integer $$$i=6$$$ to do the operation. Then array $$$a$$$ is transformed to $$$a$$$ = $$$[-3, 2, -3, -4, 4, 1, -1, 2]$$$. Choose an integer $$$i=4$$$ to do the operation. Then array $$$a$$$ is transformed to $$$a$$$ = $$$[-3, 2, -3, -3, 2, 4, -5, 7]$$$. Choose an integer $$$i=3$$$ to do the operation. Then array $$$a$$$ is transformed to $$$a$$$ = $$$[-3, 2, -2, -5, 5, 0, 0, 1]$$$. Choose an integer $$$i=1$$$ to do the operation. Then array $$$a$$$ is transformed to $$$a$$$ = $$$[-2, 0, 1, -9, 10, -6, 7, -7]$$$. The resulting $$$a$$$ is $$$[-2, 0, 1, -9, 10, -6, 7, -7]$$$, which contains $$$b$$$ = $$$[10, -6, 7, -7]$$$ as a subarray. In the fourth test case, it is impossible to transform $$$a$$$ so that it contains $$$b$$$ as a subarray. In the fifth test case, it is impossible to transform $$$a$$$ so that it contains $$$b$$$ as a subarray.
3,500
true
false
false
false
false
true
false
false
false
false
2,049
1616C
An array $$$a_1, a_2, ldots, a_n$$$ is good if and only if for every subsegment $$$1 leq l leq r leq n$$$, the following holds: $$$a_l + a_{l + 1} + ldots + a_r = frac{1}{2}(a_l + a_r) cdot (r - l + 1)$$$. You are given an array of integers $$$a_1, a_2, ldots, a_n$$$. In one operation, you can replace any one element of this array with any real number. Find the minimum number of operations you need to make this array good. Input The first line of input contains one integer $$$t$$$ ($$$1 leq t leq 100$$$): the number of test cases. Each of the next $$$t$$$ lines contains the description of a test case. In the first line you are given one integer $$$n$$$ ($$$1 leq n leq 70$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-100 leq a_i leq 100$$$): the initial array. Output For each test case, print one integer: the minimum number of elements that you need to replace to make the given array good. Example Input 5 4 1 2 3 4 4 1 1 2 2 2 0 -1 6 3 -2 4 -1 -4 0 1 -100 Note In the first test case, the array is good already. In the second test case, one of the possible good arrays is $$$[1, 1, underline{1}, underline{1}]$$$ (replaced elements are underlined). In the third test case, the array is good already. In the fourth test case, one of the possible good arrays is $$$[underline{-2.5}, -2, underline{-1.5}, -1, underline{-0.5}, 0]$$$.
1,500
true
false
true
false
false
false
true
false
false
false
2,554
952B
Problem - 952B - Codeforces =============== xa0
1,900
false
false
false
false
false
false
true
false
false
false
5,915
45H
The Berland capital (as you very well know) contains _n_ junctions, some pairs of which are connected by two-way roads. Unfortunately, the number of traffic jams in the capital has increased dramatically, that's why it was decided to build several new roads. Every road should connect two junctions. The city administration noticed that in the cities of all the developed countries between any two roads one can drive along at least two paths so that the paths don't share any roads (but they may share the same junction). The administration decided to add the minimal number of roads so that this rules was fulfilled in the Berland capital as well. In the city road network should exist no more than one road between every pair of junctions before or after the reform. Input The first input line contains a pair of integers _n_, _m_ (2u2009≤u2009_n_u2009≤u2009900,u20091u2009≤u2009_m_u2009≤u2009100000), where _n_ is the number of junctions and _m_ is the number of roads. Each of the following _m_ lines contains a description of a road that is given by the numbers of the connected junctions _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_). The junctions are numbered from 1 to _n_. It is possible to reach any junction of the city from any other one moving along roads. Output On the first line print _t_ — the number of added roads. Then on _t_ lines print the descriptions of the added roads in the format of the input data. You can use any order of printing the roads themselves as well as the junctions linked by every road. If there are several solutions to that problem, print any of them. If the capital doesn't need the reform, print the single number 0. If there's no solution, print the single number -1.
2,100
false
false
false
false
false
false
false
false
false
true
9,752
1409D
Problem - 1409D - 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 *1500 No tag edit access → Contest materials ") Editorial") . Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains two integers $$$n$$$ and $$$s$$$ ($$$1 le n le 10^{18}$$$; $$$1 le s le 162$$$). Output For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of $$$n$$$ be less than or equal to $$$s$$$. Example Input 5 2 1 1 1 500 4 217871987498122 10 100000000000000001 1 Output 8 0 500 2128012501878 899999999999999999
1,500
true
true
false
false
false
false
false
false
false
false
3,624
1701E
You wanted to write a text $$$t$$$ consisting of $$$m$$$ lowercase Latin letters. But instead, you have written a text $$$s$$$ consisting of $$$n$$$ lowercase Latin letters, and now you want to fix it by obtaining the text $$$t$$$ from the text $$$s$$$. Initially, the cursor of your text editor is at the end of the text $$$s$$$ (after its last character). In one move, you can do one of the following actions: press the "left" button, so the cursor is moved to the left by one position (or does nothing if it is pointing at the beginning of the text, i.u2009e. before its first character); press the "right" button, so the cursor is moved to the right by one position (or does nothing if it is pointing at the end of the text, i.u2009e. after its last character); press the "home" button, so the cursor is moved to the beginning of the text (before the first character of the text); press the "end" button, so the cursor is moved to the end of the text (after the last character of the text); press the "backspace" button, so the character before the cursor is removed from the text (if there is no such character, nothing happens). Your task is to calculate the minimum number of moves required to obtain the text $$$t$$$ from the text $$$s$$$ using the given set of actions, or determine it is impossible to obtain the text $$$t$$$ from the text $$$s$$$. You have to answer $$$T$$$ independent test cases. Input The first line of the input contains one integer $$$T$$$ ($$$1 le T le 5000$$$) — 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 m le n le 5000$$$) — the length of $$$s$$$ and the length of $$$t$$$, respectively. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. The third line of the test case contains the string $$$t$$$ consisting of $$$m$$$ lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$ ($$$sum n le 5000$$$). Output For each test case, print one integer — the minimum number of moves required to obtain the text $$$t$$$ from the text $$$s$$$ using the given set of actions, or -1 if it is impossible to obtain the text $$$t$$$ from the text $$$s$$$ in the given test case. Example Input 6 9 4 aaaaaaaaa aaaa 7 3 abacaba aaa 5 4 aabcd abcd 4 2 abba bb 6 4 baraka baka 8 7 question problem
2,500
false
true
false
true
false
false
true
false
false
false
2,072
758C
Problem - 758C - 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 constructive algorithms implementation math *1700 No tag edit access → Contest materials ") Editorial") . Output Print three integers: 1. the maximum number of questions a particular pupil is asked, 2. the minimum number of questions a particular pupil is asked, 3. how many times the teacher asked Sergei. Examples Input 1 3 8 1 1 Output 3 2 3 Input 4 2 9 4 2 Output 2 1 1 Input 5 5 25 4 3 Output 1 1 1 Input 100 100 1000000000000000000 100 100 Output 101010101010101 50505050505051 50505050505051 Note The order of asking pupils in the first test: 1. the pupil from the first row who seats at the first table, it means it is Sergei; 2. the pupil from the first row who seats at the second table; 3. the pupil from the first row who seats at the third table; 4. the pupil from the first row who seats at the first table, it means it is Sergei; 5. the pupil from the first row who seats at the second table; 6. the pupil from the first row who seats at the third table; 7. the pupil from the first row who seats at the first table, it means it is Sergei; 8. the pupil from the first row who seats at the second table; The order of asking pupils in the second test: 1. the pupil from the first row who seats at the first table; 2. the pupil from the first row who seats at the second table; 3. the pupil from the second row who seats at the first table; 4. the pupil from the second row who seats at the second table; 5. the pupil from the third row who seats at the first table; 6. the pupil from the third row who seats at the second table; 7. the pupil from the fourth row who seats at the first table; 8. the pupil from the fourth row who seats at the second table, it means it is Sergei; 9. the pupil from the third row who seats at the first table;
1,700
true
false
true
false
false
true
false
true
false
false
6,755
850E
The presidential election is coming in Bearland next year! Everybody is so excited about this! So far, there are three candidates, Alice, Bob, and Charlie. There are _n_ citizens in Bearland. The election result will determine the life of all citizens of Bearland for many years. Because of this great responsibility, each of _n_ citizens will choose one of six orders of preference between Alice, Bob and Charlie uniformly at random, independently from other voters. The government of Bearland has devised a function to help determine the outcome of the election given the voters preferences. More specifically, the function is (takes _n_ boolean numbers and returns a boolean number). The function also obeys the following property: _f_(1u2009-u2009_x_1,u20091u2009-u2009_x_2,u2009...,u20091u2009-u2009_x__n_)u2009=u20091u2009-u2009_f_(_x_1,u2009_x_2,u2009...,u2009_x__n_). Three rounds will be run between each pair of candidates: Alice and Bob, Bob and Charlie, Charlie and Alice. In each round, _x__i_ will be equal to 1, if _i_-th citizen prefers the first candidate to second in this round, and 0 otherwise. After this, _y_u2009=u2009_f_(_x_1,u2009_x_2,u2009...,u2009_x__n_) will be calculated. If _y_u2009=u20091, the first candidate will be declared as winner in this round. If _y_u2009=u20090, the second will be the winner, respectively. Define the probability that there is a candidate who won two rounds as _p_. _p_·6_n_ is always an integer. Print the value of this integer modulo 109u2009+u20097u2009=u20091 000 000 007. Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u200920). The next line contains a string of length 2_n_ of zeros and ones, representing function _f_. Let _b__k_(_x_) the _k_-th bit in binary representation of _x_, _i_-th (0-based) digit of this string shows the return value of _f_(_b_1(_i_),u2009_b_2(_i_),u2009...,u2009_b__n_(_i_)). It is guaranteed that _f_(1u2009-u2009_x_1,u20091u2009-u2009_x_2,u2009...,u20091u2009-u2009_x__n_)u2009=u20091u2009-u2009_f_(_x_1,u2009_x_2,u2009...,u2009_x__n_) for any values of _x_1,u2009_x_2,u2009_ldots_,u2009_x__n_. Note In first sample, result is always fully determined by the first voter. In other words, _f_(_x_1,u2009_x_2,u2009_x_3)u2009=u2009_x_1. Thus, any no matter what happens, there will be a candidate who won two rounds (more specifically, the candidate who is at the top of voter 1's preference list), so _p_u2009=u20091, and we print 1·63u2009=u2009216.
2,800
true
false
false
false
false
false
true
false
false
false
6,347
1271A
A new delivery of clothing has arrived today to the clothing store. This delivery consists of $$$a$$$ ties, $$$b$$$ scarves, $$$c$$$ vests and $$$d$$$ jackets. The store does not sell single clothing items — instead, it sells suits of two types: a suit of the first type consists of one tie and one jacket; a suit of the second type consists of one scarf, one vest and one jacket. Each suit of the first type costs $$$e$$$ coins, and each suit of the second type costs $$$f$$$ coins. Calculate the maximum possible cost of a set of suits that can be composed from the delivered clothing items. Note that one item cannot be used in more than one suit (though some items may be left unused). Input The first line contains one integer $$$a$$$ $$$(1 le a le 100,000)$$$ — the number of ties. The second line contains one integer $$$b$$$ $$$(1 le b le 100,000)$$$ — the number of scarves. The third line contains one integer $$$c$$$ $$$(1 le c le 100,000)$$$ — the number of vests. The fourth line contains one integer $$$d$$$ $$$(1 le d le 100,000)$$$ — the number of jackets. The fifth line contains one integer $$$e$$$ $$$(1 le e le 1,000)$$$ — the cost of one suit of the first type. The sixth line contains one integer $$$f$$$ $$$(1 le f le 1,000)$$$ — the cost of one suit of the second type. Output Print one integer — the maximum total cost of some set of suits that can be composed from the delivered items. Note It is possible to compose three suits of the second type in the first example, and their total cost will be $$$6$$$. Since all jackets will be used, it's impossible to add anything to this set. The best course of action in the second example is to compose nine suits of the first type and eleven suits of the second type. The total cost is $$$9 cdot 4 + 11 cdot 6 = 102$$$.
800
true
true
false
false
false
false
true
false
false
false
4,335
1184E1
Heidi found out that the Daleks have created a network of bidirectional Time Corridors connecting different destinations (at different times!).xa0She suspects that they are planning another invasion on the entire Space and Time. In order to counter the invasion, she plans to deploy a trap in the Time Vortex, along a carefully chosen Time Corridor. She knows that tinkering with the Time Vortex is dangerous, so she consulted the Doctor on how to proceed. She has learned the following: Different Time Corridors require different amounts of energy to keep stable. Daleks are unlikely to use all corridors in their invasion. They will pick a set of Corridors that requires the smallest total energy to maintain, yet still makes (time) travel possible between any two destinations (for those in the know: they will use a minimum spanning tree). Setting the trap may modify the energy required to keep the Corridor stable. Heidi decided to carry out a field test and deploy one trap, placing it along the first Corridor. But she needs to know whether the Daleks are going to use this corridor after the deployment of the trap. She gives you a map of Time Corridors (an undirected graph) with energy requirements for each Corridor. For a Corridor $$$c$$$, $$$E_{max}(c)$$$ is the largest $$$e le 10^9$$$ such that if we changed the required amount of energy of $$$c$$$ to $$$e$$$, then the Daleks may still be using $$$c$$$ in their invasion (that is, it belongs to some minimum spanning tree). Your task is to calculate $$$E_{max}(c_1)$$$ for the Corridor $$$c_1$$$ that Heidi plans to arm with a trap, which is the first edge in the graph. Input The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10^5$$$, $$$n - 1 leq m leq 10^6$$$), number of destinations to be invaded and the number of Time Corridors. Each of the next $$$m$$$ lines describes a Corridor: destinations $$$a$$$, $$$b$$$ and energy $$$e$$$ ($$$1 leq a, b leq n$$$, $$$a eq b$$$, $$$0 leq e leq 10^9$$$). It's guaranteed, that no pair $$${a, b}$$$ will repeat and that the graph is connectedxa0— that is, it is possible to travel between any two destinations using zero or more Time Corridors. Output Output a single integer: $$$E_{max}(c_1)$$$ for the first Corridor $$$c_1$$$ from the input. Note After the trap is set, the new energy requirement for the first Corridor may be either smaller, larger, or equal to the old energy requiremenet. In the example, if the energy of the first Corridor is set to $$$4$$$ or less, then the Daleks may use the set of Corridors $$${ { 1,2 }, { 2,3 } }$$$ (in particular, if it were set to less than $$$4$$$, then this would be the only set of Corridors that they would use). However, if it is larger than $$$4$$$, then they will instead use the set $$${ {2,3}, {3,1} }$$$.
1,900
false
false
false
false
false
false
false
false
false
true
4,783
1766D
Let's name a pair of positive integers $$$(x, y)$$$ lucky if the greatest common divisor of them is equal to $$$1$$$ ($$$gcd(x, y) = 1$$$). Let's define a chain induced by $$$(x, y)$$$ as a sequence of pairs $$$(x, y)$$$, $$$(x + 1, y + 1)$$$, $$$(x + 2, y + 2)$$$, $$$dots$$$, $$$(x + k, y + k)$$$ for some integer $$$k ge 0$$$. The length of the chain is the number of pairs it consists of, or $$$(k + 1)$$$. Let's name such chain lucky if all pairs in the chain are lucky. You are given $$$n$$$ pairs $$$(x_i, y_i)$$$. Calculate for each pair the length of the longest lucky chain induced by this pair. Note that if $$$(x_i, y_i)$$$ is not lucky itself, the chain will have the length $$$0$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0— the number of pairs. Next $$$n$$$ lines contains $$$n$$$ pairsxa0— one per line. The $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i < y_i le 10^7$$$)xa0— the corresponding pair. Output Print $$$n$$$ integers, where the $$$i$$$-th integer is the length of the longest lucky chain induced by $$$(x_i, y_i)$$$ or $$$-1$$$ if the chain can be infinitely long. Example Input 4 5 15 13 37 8 9 10009 20000 Note In the first test case, $$$gcd(5, 15) = 5 > 1$$$, so it's already not lucky, so the length of the lucky chain is $$$0$$$. In the second test case, $$$gcd(13 + 1, 37 + 1) = gcd(14, 38) = 2$$$. So, the lucky chain consists of the single pair $$$(13, 37)$$$.
1,600
true
false
false
false
false
false
false
false
false
false
1,700
786D
Rick is in love with Unity. But Mr. Meeseeks also love Unity, so Rick and Mr. Meeseeks are "love rivals". Unity loves rap, so it decided that they have to compete in a rap game (battle) in order to choose the best. Rick is too nerds, so instead he's gonna make his verse with running his original algorithm on lyrics "Rap God" song. His algorithm is a little bit complicated. He's made a tree with _n_ vertices numbered from 1 to _n_ and there's a lowercase english letter written on each edge. He denotes _str_(_a_,u2009_b_) to be the string made by writing characters on edges on the shortest path from _a_ to _b_ one by one (a string of length equal to distance of _a_ to _b_). Note that _str_(_a_,u2009_b_) is reverse of _str_(_b_,u2009_a_) and _str_(_a_,u2009_a_) is empty. In order to make the best verse he can, he needs to answer some queries, but he's not a computer scientist and is not able to answer those queries, so he asked you to help him. Each query is characterized by two vertices _x_ and _y_ (_x_u2009≠u2009_y_). Answer to this query is the number of vertices like _z_ such that _z_u2009≠u2009_x_,u2009_z_u2009≠u2009_y_ and _str_(_x_,u2009_y_) is lexicographically larger than _str_(_x_,u2009_z_). String _x_u2009u2009=u2009u2009_x_1_x_2..._x__x_ is lexicographically larger than string _y_u2009u2009=u2009u2009_y_1_y_2..._y__y_, if either _x_u2009u2009>u2009u2009_y_ and _x_1u2009u2009=u2009u2009_y_1,u2009u2009_x_2u2009u2009=u2009u2009_y_2,u2009u2009...,u2009u2009_x__y_u2009u2009=u2009u2009_y__y_, or exists such number _r_ (_r_u2009u2009<u2009u2009_x_,u2009u2009_r_u2009u2009<u2009u2009_y_), that _x_1u2009u2009=u2009u2009_y_1,u2009u2009_x_2u2009u2009=u2009u2009_y_2,u2009u2009...,u2009u2009_x__r_u2009u2009=u2009u2009_y__r_ and _x__r_u2009u2009+u2009u20091u2009u2009>u2009u2009_y__r_u2009u2009+u2009u20091. Characters are compared like their ASCII codes (or alphabetic order). Help Rick get the girl (or whatever gender Unity has). Input The first line of input contain two integers _n_ and _q_ (2u2009≤u2009_n_u2009≤u200920000, 1u2009≤u2009_q_u2009≤u200920000) — number of vertices in tree and number of queries respectively. The next _n_u2009-u20091 lines contain the edges. Each line contains two integers _v_ and _u_ (endpoints of the edge) followed by an English lowercase letter _c_ (1u2009≤u2009_v_,u2009_u_u2009≤u2009_n_,u2009_v_u2009≠u2009_u_). The next _q_ line contain the queries. Each line contains two integers _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_,u2009_x_u2009≠u2009_y_). Note Here's the tree of first sample testcase: Here's the tree of second sample testcase: In this test: _str_(8,u20091) = poo _str_(8,u20092) = poe _str_(8,u20093) = po _str_(8,u20094) = pop _str_(8,u20095) = popd _str_(8,u20096) = popp _str_(8,u20097) = p So, for the first query, and for the third query is the answer.
3,400
false
false
false
false
true
false
false
false
false
false
6,625
1799B
You are given an array $$$a_1, a_2, ldots, a_n$$$ of positive integers. You can make this operation multiple (possibly zero) times: Choose two indices $$$i$$$, $$$j$$$ ($$$1 leq i, j leq n$$$, $$$i eq j$$$). Assign $$$a_i := lceil frac{a_i}{a_j} ceil$$$. Here $$$lceil x ceil$$$ denotes $$$x$$$ rounded up to the smallest integer $$$geq x$$$. Is it possible to make all array elements equal by some sequence of operations (possibly empty)? If yes, print any way to do it in at most $$$30n$$$ operations. It can be proven, that under the problem constraints, if some way exists to make all elements equal, there exists a way with at most $$$30n$$$ operations. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of test cases. Descriptions of test cases follow. The first line of each test case description contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$). The second line of each test case description contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$1000$$$. Output For each test case print a single integer $$$q$$$ ($$$-1 leq q leq 30n$$$). If $$$q=-1$$$, there is no solution, otherwise $$$q$$$ is equal to the number of operations. If $$$q geq 0$$$, on the next $$$q$$$ lines print two integers $$$i$$$, $$$j$$$ ($$$1 leq i, j leq n$$$, $$$i eq j$$$) — descriptions of operations. If there are multiple solutions, you can print any. Example Input 10 1 100 3 1 1 1 2 2 1 2 5 5 3 4 3 2 4 3 3 4 4 2 2 100 5 5 3 6 7 8 6 3 3 80 3 8 3 4 19 40 19 55 Output 0 0 -1 0 2 1 3 2 1 4 3 1 4 2 1 3 2 4 6 2 1 2 1 2 1 2 1 2 1 2 1 8 5 2 4 2 3 2 1 3 1 3 2 1 4 1 5 1 4 5 1 3 1 3 1 3 1 9 4 2 2 1 1 2 1 2 3 2 3 2 1 4 2 4 3 4 Note In the first and second, fourth test cases all numbers are equal, so it is possible to do nothing. In the third test case, it is impossible to make all numbers equal. In the fifth test case: $$$[color{red}{4}, 3, color{blue}{2}] o [color{blue}{2}, color{red}{3}, 2] o [2, 2, 2]$$$. In the sixth test case: $$$[color{blue}{3}, 3, color{red}{4}, 4] o [3, color{blue}{3}, 2, color{red}{4}] o [color{red}{3}, 3, color{blue}{2}, 2] o [2, color{red}{3}, 2, color{blue}{2}] o [2, 2, 2, 2]$$$. Here the red numbers are $$$i$$$ indices (that will be assigned), blue numbers are $$$j$$$ indices.
1,200
true
true
false
false
false
true
true
false
false
false
1,483
1154B
You are given a sequence $$$a_1, a_2, dots, a_n$$$ consisting of $$$n$$$ integers. You can choose any non-negative integer $$$D$$$ (i.e. $$$D ge 0$$$), and for each $$$a_i$$$ you can: add $$$D$$$ (only once), i.u2009e. perform $$$a_i := a_i + D$$$, or subtract $$$D$$$ (only once), i.u2009e. perform $$$a_i := a_i - D$$$, or leave the value of $$$a_i$$$ unchanged. It is possible that after an operation the value $$$a_i$$$ becomes negative. Your goal is to choose such minimum non-negative integer $$$D$$$ and perform changes in such a way, that all $$$a_i$$$ are equal (i.e. $$$a_1=a_2=dots=a_n$$$). Print the required $$$D$$$ or, if it is impossible to choose such value $$$D$$$, print -1. For example, for array $$$[2, 8]$$$ the value $$$D=3$$$ is minimum possible because you can obtain the array $$$[5, 5]$$$ if you will add $$$D$$$ to $$$2$$$ and subtract $$$D$$$ from $$$8$$$. And for array $$$[1, 4, 7, 7]$$$ the value $$$D=3$$$ is also minimum possible. You can add it to $$$1$$$ and subtract it from $$$7$$$ and obtain the array $$$[4, 4, 4, 4]$$$. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 100$$$) — 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 100$$$) — the sequence $$$a$$$. Output Print one integer — the minimum non-negative integer value $$$D$$$ such that if you add this value to some $$$a_i$$$, subtract this value from some $$$a_i$$$ and leave some $$$a_i$$$ without changes, all obtained values become equal. If it is impossible to choose such value $$$D$$$, print -1.
1,200
true
false
false
false
false
false
false
false
false
false
4,942
1204B
Mislove had an array $$$a_1$$$, $$$a_2$$$, $$$cdots$$$, $$$a_n$$$ of $$$n$$$ positive integers, but he has lost it. He only remembers the following facts about it: The number of different numbers in the array is not less than $$$l$$$ and is not greater than $$$r$$$; For each array's element $$$a_i$$$ either $$$a_i = 1$$$ or $$$a_i$$$ is even and there is a number $$$dfrac{a_i}{2}$$$ in the array. For example, if $$$n=5$$$, $$$l=2$$$, $$$r=3$$$ then an array could be $$$[1,2,2,4,4]$$$ or $$$[1,1,1,1,2]$$$; but it couldn't be $$$[1,2,2,4,8]$$$ because this array contains $$$4$$$ different numbers; it couldn't be $$$[1,2,2,3,3]$$$ because $$$3$$$ is odd and isn't equal to $$$1$$$; and it couldn't be $$$[1,1,2,2,16]$$$ because there is a number $$$16$$$ in the array but there isn't a number $$$frac{16}{2} = 8$$$. According to these facts, he is asking you to count the minimal and the maximal possible sums of all elements in an array. Input The only input line contains three integers $$$n$$$, $$$l$$$ and $$$r$$$ ($$$1 leq n leq 1,000$$$, $$$1 leq l leq r leq min(n, 20)$$$)xa0— an array's size, the minimal number and the maximal number of distinct elements in an array. Output Output two numbersxa0— the minimal and the maximal possible sums of all elements in an array. Note In the first example, an array could be the one of the following: $$$[1,1,1,2]$$$, $$$[1,1,2,2]$$$ or $$$[1,2,2,2]$$$. In the first case the minimal sum is reached and in the last case the maximal sum is reached. In the second example, the minimal sum is reached at the array $$$[1,1,1,1,1]$$$, and the maximal one is reached at the array $$$[1,2,4,8,16]$$$.
900
true
true
false
false
false
false
false
false
false
false
4,674
1227F1
Your program fails again. This time it gets "Wrong answer on test 233" . This is the easier version of the problem. In this version $$$1 le n le 2000$$$. You can hack this problem only if you solve and lock both problems. The problem is about a test containing $$$n$$$ one-choice-questions. Each of the questions contains $$$k$$$ options, and only one of them is correct. The answer to the $$$i$$$-th question is $$$h_{i}$$$, and if your answer of the question $$$i$$$ is $$$h_{i}$$$, you earn $$$1$$$ point, otherwise, you earn $$$0$$$ points for this question. The values $$$h_1, h_2, dots, h_n$$$ are known to you in this problem. However, you have a mistake in your program. It moves the answer clockwise! Consider all the $$$n$$$ answers are written in a circle. Due to the mistake in your program, they are shifted by one cyclically. Formally, the mistake moves the answer for the question $$$i$$$ to the question $$$i bmod n + 1$$$. So it moves the answer for the question $$$1$$$ to question $$$2$$$, the answer for the question $$$2$$$ to the question $$$3$$$, ..., the answer for the question $$$n$$$ to the question $$$1$$$. We call all the $$$n$$$ answers together an answer suit. There are $$$k^n$$$ possible answer suits in total. You're wondering, how many answer suits satisfy the following condition: after moving clockwise by $$$1$$$, the total number of points of the new answer suit is strictly larger than the number of points of the old one. You need to find the answer modulo $$$998,244,353$$$. For example, if $$$n = 5$$$, and your answer suit is $$$a=[1,2,3,4,5]$$$, it will submitted as $$$a'=[5,1,2,3,4]$$$ because of a mistake. If the correct answer suit is $$$h=[5,2,2,3,4]$$$, the answer suit $$$a$$$ earns $$$1$$$ point and the answer suite $$$a'$$$ earns $$$4$$$ points. Since $$$4 > 1$$$, the answer suit $$$a=[1,2,3,4,5]$$$ should be counted. Input The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 2000$$$, $$$1 le k le 10^9$$$)xa0— the number of questions and the number of possible answers to each question. The following line contains $$$n$$$ integers $$$h_1, h_2, dots, h_n$$$, ($$$1 le h_{i} le k)$$$xa0— answers to the questions. Note For the first example, valid answer suits are $$$[2,1,1], [2,1,2], [2,1,3], [3,1,1], [3,1,2], [3,1,3], [3,2,1], [3,2,2], [3,2,3]$$$.
2,200
false
false
false
true
false
false
false
false
false
false
4,538
1209C
You are given a sequence of $$$n$$$ digits $$$d_1d_2 dots d_{n}$$$. You need to paint all the digits in two colors so that: each digit is painted either in the color $$$1$$$ or in the color $$$2$$$; if you write in a row from left to right all the digits painted in the color $$$1$$$, and then after them all the digits painted in the color $$$2$$$, then the resulting sequence of $$$n$$$ digits will be non-decreasing (that is, each next digit will be greater than or equal to the previous digit). For example, for the sequence $$$d=914$$$ the only valid coloring is $$$211$$$ (paint in the color $$$1$$$ two last digits, paint in the color $$$2$$$ the first digit). But $$$122$$$ is not a valid coloring ($$$9$$$ concatenated with $$$14$$$ is not a non-decreasing sequence). It is allowed that either of the two colors is not used at all. Digits painted in the same color are not required to have consecutive positions. Find any of the valid ways to paint the given sequence of digits or determine that it is impossible to do. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10000$$$) — the number of test cases in the input. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$) — the length of a given sequence of digits. The next line contains a sequence of $$$n$$$ digits $$$d_1d_2 dots d_{n}$$$ ($$$0 le d_i le 9$$$). The digits are written in a row without spaces or any other separators. The sequence can start with 0. It is guaranteed that the sum of the values u200bu200bof $$$n$$$ for all test cases in the input does not exceed $$$2cdot10^5$$$. Output Print $$$t$$$ lines — the answers to each of the test cases in the input. If there is a solution for a test case, the corresponding output line should contain any of the valid colorings written as a string of $$$n$$$ digits $$$t_1t_2 dots t_n$$$ ($$$1 le t_i le 2$$$), where $$$t_i$$$ is the color the $$$i$$$-th digit is painted in. If there are several feasible solutions, print any of them. If there is no solution, then the corresponding output line should contain a single character '-' (the minus sign). Example Input 5 12 040425524644 1 0 9 123456789 2 98 3 987 Output 121212211211 1 222222222 21 - Note In the first test case, $$$d=040425524644$$$. The output $$$t=121212211211$$$ is correct because $$$0022444$$$ (painted in $$$1$$$) concatenated with $$$44556$$$ (painted in $$$2$$$) is $$$002244444556$$$ which is a sorted sequence of $$$n$$$ given digits.
1,500
false
true
true
false
false
true
false
false
false
false
4,644
1399D
You are given a binary string $$$s$$$ consisting of $$$n$$$ zeros and ones. Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like "010101 ..." or "101010 ..." (i.e. the subsequence should not contain two adjacent zeros or ones). Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of "1011101" are "0", "1", "11111", "0111", "101", "1001", but not "000", "101010" and "11100". You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the length of $$$s$$$. The second line of the test case contains $$$n$$$ characters '0' and '1' — the string $$$s$$$. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer: in the first line print one integer $$$k$$$ ($$$1 le k le n$$$) — the minimum number of subsequences you can divide the string $$$s$$$ to. In the second line print $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le k$$$), where $$$a_i$$$ is the number of subsequence the $$$i$$$-th character of $$$s$$$ belongs to. If there are several answers, you can print any. Example Input 4 4 0011 6 111111 5 10101 8 01010000 Output 2 1 2 2 1 6 1 2 3 4 5 6 1 1 1 1 1 1 4 1 1 1 1 1 2 3 4
1,500
false
true
true
false
true
true
false
false
false
false
3,676
1975A
Mocha likes arrays, so before her departure, Bazoka gave her an array $$$a$$$ consisting of $$$n$$$ positive integers as a gift. Now Mocha wants to know whether array $$$a$$$ could become sorted in non-decreasing order after performing the following operation some (possibly, zero) times: Split the array into two partsxa0— a prefix and a suffix, then swap these two parts. In other words, let $$$a=x+y$$$. Then, we can set $$$a:= y+x$$$. Here $$$+$$$ denotes the array concatenation operation. For example, if $$$a=[3,1,4,1,5]$$$, we can choose $$$x=[3,1]$$$ and $$$y=[4,1,5]$$$, satisfying $$$a=x+y$$$. Then, we can set $$$a:= y + x = [4,1,5,3,1]$$$. We can also choose $$$x=[3,1,4,1,5]$$$ and $$$y=[,]$$$, satisfying $$$a=x+y$$$. Then, we can set $$$a := y+x = [3,1,4,1,5]$$$. Note that we are not allowed to choose $$$x=[3,1,1]$$$ and $$$y=[4,5]$$$, neither are we allowed to choose $$$x=[1,3]$$$ and $$$y=[5,1,4]$$$, as both these choices do not satisfy $$$a=x+y$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1leq tleq 1000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2leq nleq 50$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq a_i leq 10^6$$$)xa0— the elements of array $$$a$$$. Output For each test case, output "Yes" if $$$a$$$ could become non-decreasing after performing the operation any number of times, and output "No" if not. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response). Note In the first test case, it can be proven that $$$a$$$ cannot become non-decreasing after performing the operation any number of times. In the second test case, we can perform the following operations to make $$$a$$$ sorted in non-decreasing order: Split the array into two parts: $$$x=[7]$$$ and $$$y=[9,2,2,3]$$$, then swap these two parts. The array will become $$$y+x = [9,2,2,3,7]$$$. Split the array into two parts: $$$x=[9]$$$ and $$$y=[2,2,3,7]$$$, then swap these two parts. The array will become $$$y+x=[2,2,3,7,9]$$$, which is non-decreasing.
800
false
true
true
false
false
false
true
false
true
false
442
1033F
Alice has a computer that operates on $$$w$$$-bit integers. The computer has $$$n$$$ registers for values. The current content of the registers is given as an array $$$a_1, a_2, ldots, a_n$$$. The computer uses so-called "number gates" to manipulate this data. Each "number gate" takes two registers as inputs and calculates a function of the two values stored in those registers. Note that you can use the same register as both inputs. Each "number gate" is assembled from bit gates. There are six types of bit gates: AND, OR, XOR, NOTxa0AND, NOTxa0OR, and NOTxa0XOR, denoted "A", "O", "X", "a", "o", "x", respectively. Each bit gate takes two bits as input. Its output given the input bits $$$b_1$$$, $$$b_2$$$ is given below: $$$begin{matrix} b_1 & b_2 & A & O & X & a & o & x 0 & 0 & 0 & 0 & 0 & 1 & 1 & 1 0 & 1 & 0 & 1 & 1 & 1 & 0 & 0 1 & 0 & 0 & 1 & 1 & 1 & 0 & 0 1 & 1 & 1 & 1 & 0 & 0 & 0 & 1 end{matrix}$$$ To build a "number gate", one takes $$$w$$$ bit gates and assembles them into an array. A "number gate" takes two $$$w$$$-bit integers $$$x_1$$$ and $$$x_2$$$ as input. The "number gate" splits the integers into $$$w$$$ bits and feeds the $$$i$$$-th bit of each input to the $$$i$$$-th bit gate. After that, it assembles the resulting bits again to form an output word. For instance, for $$$4$$$-bit computer we might have a "number gate" "AXoA" (AND, XOR, NOT OR, AND). For two inputs, $$$13 = 1101_2$$$ and $$$10 = 1010_2$$$, this returns $$$12 = 1100_2$$$, as $$$1$$$ and $$$1$$$ is $$$1$$$, $$$1$$$ xor $$$0$$$ is $$$1$$$, not ($$$0$$$ or $$$1$$$) is $$$0$$$, and finally $$$1$$$ and $$$0$$$ is $$$0$$$. You are given a description of $$$m$$$ "number gates". For each gate, your goal is to report the number of register pairs for which the "number gate" outputs the number $$$0$$$. In other words, find the number of ordered pairs $$$(i,j)$$$ where $$$1 leq i,j leq n$$$, such that $$$w_k(a_i, a_j) = 0$$$, where $$$w_k$$$ is the function computed by the $$$k$$$-th "number gate". Input The first line contains three integers: $$$w$$$, $$$n$$$, and $$$m~(1 leq w leq 12, 1 leq n leq 3cdot 10^4, 1 leq m leq 5cdot 10^4)$$$xa0— the word size, the number of variables, and the number of gates. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(0 leq a_i < 2^w)$$$xa0— the value of variables stored in the registers. Each of the next $$$m$$$ lines contains a string $$$g_j~(g_j = w)$$$ with a description of a single gate. Each character of $$$g_j$$$ is one of "A", "O", "X", "a", "o", "x". Note In the first test case, the inputs in binary are $$$1101$$$, $$$1010$$$, $$$0110$$$. The pairs that return $$$0$$$ are $$$(13, 6)$$$, $$$(6, 13)$$$, and $$$(6, 6)$$$. As it was already mentioned in the problem statement, $$$13 oplus 10 = 10 oplus 13 = 12$$$. The other pairs are $$$13 oplus 13 = 11$$$, $$$10 oplus 10 = 8$$$ and $$$10 oplus 6 = 6 oplus 10 = 4$$$.
2,800
true
false
false
false
false
false
true
false
false
false
5,530
1523F
Problem - 1523F - 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 *3300 No tag edit access → Contest materials to one of the following locations: (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1). To accelerate movement the game has $$$n$$$ fast travel towers. $$$i$$$-th tower is located at location ($$$xa_i, ya_i$$$). To be able to instantly travel to the tower from any location in the game world it must first be activated. Activation of tower $$$i$$$ happens at the moment when the player is in cell ($$$xa_i, ya_i$$$) after this the tower remains active throughout the entire game. William also knows that the game has $$$m$$$ quests. $$$i$$$-th quest can be completed instantly by being at location ($$$xb_i, yb_i$$$) on turn $$$t_i$$$. William wants to find out the maximal number of quests he will be able to complete by optimally traversing the game world. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 le n le 14, 1 le m le 100$$$), which are the number of towers and the number of quests, respectively. Each of the next $$$n$$$ lines contains two integers $$$xa_i, ya_i$$$ ($$$1 le xa_i, ya_i le 10^6$$$), which are the coordinates of fast travel towers. Each of the next $$$m$$$ lines contains two integers $$$xb_i$$$, $$$yb_i$$$ and $$$t_i$$$ ($$$1 le xb_i, yb_i le 10^6$$$, $$$1 le t_i le 10^9$$$), which are the coordinates of quests and the turn at which it may be completed. It is guaranteed that all locations in a test are different. Output Print a single number xa0— the maximal number of quests William will be able to complete. Example Input 3 4 1 1 2 3 5 2 2 2 12 5 1 4 6 2 11 3 5 10 Output 3 Note In the first sample test one of the possible sequences of William's actions is as follows: Spawn at $$$(3, 2)$$$ On turn $$$1$$$ move to $$$(4, 2)$$$ On turn $$$2$$$ move to $$$(5, 2)$$$. By visiting this cell William activates tower number $$$3$$$. On turn $$$3$$$ move to $$$(5, 1)$$$, where he waits for $$$1$$$ turn to complete the $$$2$$$nd quest On turn $$$5$$$ move to $$$(5, 2)$$$ On turn $$$6$$$ move to $$$(5, 3)$$$ On turn $$$7$$$ move to $$$(5, 4)$$$ On turn $$$8$$$ move to $$$(5, 5)$$$ On turn $$$9$$$ move to $$$(4, 5)$$$ On turn $$$10$$$ move to $$$(3, 5)$$$. By moving to this location William will complete the $$$4$$$th quest On turn $$$10$$$ instantly move to an already activated fast travel tower at $$$(5, 2)$$$ On turn $$$11$$$ move to $$$(6, 2)$$$. By moving to this location William will complete the $$$3$$$rd quest William will not be able to complete the quest number $$$1$$$, because the tower at $$$(2, 3)$$$ was not activated
3,300
false
false
false
true
false
false
false
false
false
false
3,045
1977B
You are given a positive integer $$$x$$$. Find any array of integers $$$a_0, a_1, ldots, a_{n-1}$$$ for which the following holds: $$$1 le n le 32$$$, $$$a_i$$$ is $$$1$$$, $$$0$$$, or $$$-1$$$ for all $$$0 le i le n - 1$$$, $$$x = displaystyle{sum_{i=0}^{n - 1}{a_i cdot 2^i}}$$$, There does not exist an index $$$0 le i le n - 2$$$ such that both $$$a_{i} eq 0$$$ and $$$a_{i + 1} eq 0$$$. It can be proven that under the constraints of the problem, a valid array always exists. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains a single positive integer $$$x$$$ ($$$1 le x < 2^{30}$$$). Output For each test case, output two lines. On the first line, output an integer $$$n$$$ ($$$1 le n le 32$$$)xa0— the length of the array $$$a_0, a_1, ldots, a_{n-1}$$$. On the second line, output the array $$$a_0, a_1, ldots, a_{n-1}$$$. If there are multiple valid arrays, you can output any of them. Example Output 1 1 5 0 -1 0 0 1 6 0 0 0 -1 0 1 5 -1 0 0 0 1 6 -1 0 -1 0 0 1 5 -1 0 -1 0 1 5 -1 0 1 0 1 Note In the first test case, one valid array is $$$[1]$$$, since $$$(1) cdot 2^0 = 1$$$. In the second test case, one possible valid array is $$$[0,-1,0,0,1]$$$, since $$$(0) cdot 2^0 + (-1) cdot 2^1 + (0) cdot 2^2 + (0) cdot 2^3 + (1) cdot 2^4 = -2 + 16 = 14$$$.
1,100
true
true
false
false
false
true
false
false
false
false
426
201A
Problem - 201A - 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 dp math *1700 No tag edit access → Contest materials (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_) both of the following conditions must be met: _A__i_,u2009_j_u2009=u2009_A__n_u2009-u2009_i_u2009+u20091,u2009_j_ and _A__i_,u2009_j_u2009=u2009_A__i_,u2009_n_u2009-u2009_j_u2009+u20091. Let's define the sharpness of matrix _A_ as the number of ones in it. Given integer _x_, your task is to find the smallest positive integer _n_ such that there exists a clear symmetrical matrix _A_ with side _n_ and sharpness _x_. Input The only line contains a single integer _x_ (1u2009≤u2009_x_u2009≤u2009100) — the required sharpness of the matrix. Output Print a single number — the sought value of _n_. Examples Input 4 Output 3 Input 9 Output 5 Note The figure below shows the matrices that correspond to the samples:
1,700
true
false
false
true
false
true
false
false
false
false
9,039
241C
Mirror Box is a name of a popular game in the Iranian National Amusement Park (INAP). There is a wooden box, 105 cm long and 100 cm high in this game. Some parts of the box's ceiling and floor are covered by mirrors. There are two negligibly small holes in the opposite sides of the box at heights _h__l_ and _h__r_ centimeters above the floor. The picture below shows what the box looks like. In the game, you will be given a laser gun to shoot once. The laser beam must enter from one hole and exit from the other one. Each mirror has a preset number _v__i_, which shows the number of points players gain if their laser beam hits that mirror. Also — to make things even funnier — the beam must not hit any mirror more than once. Given the information about the box, your task is to find the maximum score a player may gain. Please note that the reflection obeys the law "the angle of incidence equals the angle of reflection". Input The first line of the input contains three space-separated integers _h__l_,u2009_h__r_,u2009_n_ (0u2009<u2009_h__l_,u2009_h__r_u2009<u2009100, 0u2009≤u2009_n_u2009≤u2009100) — the heights of the holes and the number of the mirrors. Next _n_ lines contain the descriptions of the mirrors. The _i_-th line contains space-separated _v__i_,u2009_c__i_,u2009_a__i_,u2009_b__i_; the integer _v__i_ (1u2009≤u2009_v__i_u2009≤u20091000) is the score for the _i_-th mirror; the character _c__i_ denotes _i_-th mirror's position — the mirror is on the ceiling if _c__i_ equals "T" and on the floor if _c__i_ equals "F"; integers _a__i_ and _b__i_ (0u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u2009105) represent the _x_-coordinates of the beginning and the end of the mirror. No two mirrors will share a common point. Consider that the _x_ coordinate increases in the direction from left to right, so the border with the hole at height _h__l_ has the _x_ coordinate equal to 0 and the border with the hole at height _h__r_ has the _x_ coordinate equal to 105. Output The only line of output should contain a single integer — the maximum possible score a player could gain. Examples Input 50 50 7 10 F 1 80000 20 T 1 80000 30 T 81000 82000 40 T 83000 84000 50 T 85000 86000 60 T 87000 88000 70 F 81000 89000 Input 80 72 9 15 T 8210 15679 10 F 11940 22399 50 T 30600 44789 50 F 32090 36579 5 F 45520 48519 120 F 49250 55229 8 F 59700 80609 35 T 61940 64939 2 T 92540 97769 Note The second sample is depicted above. The red beam gets 10u2009+u200950u2009+u20095u2009+u200935u2009+u20098u2009+u20092u2009=u2009110 points and the blue one gets 120. The red beam on the picture given in the statement shows how the laser beam can go approximately, this is just illustration how the laser beam can gain score. So for the second sample there is no such beam that gain score 110.
2,000
false
false
true
false
false
false
false
false
false
false
8,873
1342E
Problem - 1342E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics fft math *2300 No tag edit access → Contest materials }{2}$$$). Output Print one integer — the number of ways to place the rooks, taken modulo $$$998244353$$$. Examples Input 3 2 Output 6 Input 3 3 Output 0 Input 4 0 Output 24 Input 1337 42 Output 807905441
2,300
true
false
false
false
false
false
false
false
false
false
3,995
1603E
A sequence of integers $$$b_1, b_2, ldots, b_m$$$ is called good if $$$max(b_1, b_2, ldots, b_m) cdot min(b_1, b_2, ldots, b_m) ge b_1 + b_2 + ldots + b_m$$$. A sequence of integers $$$a_1, a_2, ldots, a_n$$$ is called perfect if every non-empty subsequence of $$$a$$$ is good. YouKn0wWho has two integers $$$n$$$ and $$$M$$$, $$$M$$$ is prime. Help him find the number, modulo $$$M$$$, of perfect sequences $$$a_1, a_2, ldots, a_n$$$ such that $$$1 le a_i le n + 1$$$ for each integer $$$i$$$ from $$$1$$$ to $$$n$$$. A sequence $$$d$$$ is a subsequence of a sequence $$$c$$$ if $$$d$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements. Input The first and only line of the input contains two space-separated integers $$$n$$$ and $$$M$$$ ($$$1 le n le 200$$$; $$$10^8 le M le 10^9$$$). It is guaranteed that $$$M$$$ is prime. Output Print a single integer xa0— the number of perfect sequences modulo $$$M$$$. Note In the first test case, the perfect sequences are $$$[2, 2]$$$, $$$[2, 3]$$$, $$$[3, 2]$$$ and $$$[3, 3]$$$. In the second test case, some of the perfect sequences are $$$[3, 4, 3, 5]$$$, $$$[4, 5, 4, 4]$$$, $$$[4, 5, 5, 5]$$$ etc. One example of a sequence which is not perfect is $$$[2, 3, 3, 4]$$$, because, for example, the subsequence $$$[2, 3, 4]$$$ is not an good as $$$2 cdot 4 < 2 + 3 + 4$$$.
3,200
true
false
false
true
false
false
false
false
false
false
2,639
255E
Problem - 255E - 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 games implementation math *2200 No tag edit access → Contest materials and decrease the number of coins in this pile to _y_. In other words, after the described move the pile will have _y_ coins left. The player who can't make a move, loses. Your task is to find out, who wins in the given game if both Furlo and Rublo play optimally well. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u200977777) — the number of piles. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009777777777777) — the sizes of piles. The numbers are separated by single spaces. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output If both players play optimally well and Furlo wins, print "Furlo", otherwise print "Rublo". Print the answers without the quotes. Examples Input 1 1 Output Rublo Input 2 1 2 Output Rublo Input 10 1 2 3 4 5 6 7 8 9 10 Output Furlo
2,200
true
false
true
false
false
false
false
false
false
false
8,810
1910B
Monocarp is a security guard in Berland State University. Every day, he tracks how many people and at what time enter and leave the university. He writes this information down as follows: when someone enters the university, Monocarp writes a plus sign '+'; when someone leaves the university, Monocarp writes a minus sign '-'. At the beginning of the current day, there are no people at the university, except for Monocarp. During the day, Monocarp wrote out a sequence $$$s$$$. The characters in $$$s$$$ are listed in the order Monocarp wrote them. Suddenly, Monocarp's boss decided to check his work. Unfortunately, Monocarp is a bit careless. So, the sequence $$$s$$$ that he wrote might be impossible. For example, the sequence "+--" can't happen, since it represents a scenario when one person enters the university and two leave. Before his boss starts checking the sequence, Monocarp has the time to swap at most one pair of characters in it. Can he do it in such a way that the resulting sequence is plausible? Note that if the given sequence is already plausible, Monocarp doesn't have to swap anything. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The only line of each test case contains a single string $$$s$$$ ($$$1 le s le 3 cdot 10^5$$$), consisting only of characters '+' and/or '-'. A plus sign '+' represents a person entering the university. A minus sign '-' represents a person leaving the university. The sum of all $$$s$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print an answer. If it's impossible to swap at most one pair of characters so that the resulting sequence is plausible, print -1. Otherwise, print two integers. If you swap one pair of characters, print two distinct integers from $$$1$$$ to $$$n$$$xa0— the indices of characters to swap. If you don't swap, print one integer from $$$1$$$ to $$$n$$$ twicexa0— swap a character with itself. If there are multiple answers, print any of them. Example Output 1 2 1 1 1 1 -1 1 1 -1
1,600
false
true
false
false
false
false
false
false
false
false
846
567A
All cities of Lineland are located on the _Ox_ coordinate axis. Thus, each city is associated with its position _x__i_ — a coordinate on the _Ox_ axis. No two cities are located at a single point. Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in). Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city. For each city calculate two values u200bu200b_min__i_ and _max__i_, where _min__i_ is the minimum cost of sending a letter from the _i_-th city to some other city, and _max__i_ is the the maximum cost of sending a letter from the _i_-th city to some other city Input The first line of the input contains integer _n_ (2u2009≤u2009_n_u2009≤u2009105) — the number of cities in Lineland. The second line contains the sequence of _n_ distinct integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (u2009-u2009109u2009≤u2009_x__i_u2009≤u2009109), where _x__i_ is the _x_-coordinate of the _i_-th city. All the _x__i_'s are distinct and follow in ascending order. Output Print _n_ lines, the _i_-th line must contain two integers _min__i_,u2009_max__i_, separated by a space, where _min__i_ is the minimum cost of sending a letter from the _i_-th city, and _max__i_ is the maximum cost of sending a letter from the _i_-th city.
900
false
true
true
false
false
false
false
false
false
false
7,584
346D
The boss of the Company of Robot is a cruel man. His motto is "Move forward Or Die!". And that is exactly what his company's product do. Look at the behavior of the company's robot when it is walking in the directed graph. This behavior has been called "Three Laws of Robotics": Law 1. The Robot will destroy itself when it visits a vertex of the graph which it has already visited. Law 2. The Robot will destroy itself when it has no way to go (that is when it reaches a vertex whose out-degree is zero). Law 3. The Robot will move randomly when it has multiple ways to move (that is when it reach a vertex whose out-degree is more than one). Of course, the robot can move only along the directed edges of the graph. Can you imagine a robot behaving like that? That's why they are sold at a very low price, just for those who are short of money, including mzry1992, of course. mzry1992 has such a robot, and she wants to move it from vertex _s_ to vertex _t_ in a directed graph safely without self-destruction. Luckily, she can send her robot special orders at each vertex. A special order shows the robot which way to move, if it has multiple ways to move (to prevent random moving of the robot according to Law 3). When the robot reaches vertex _t_, mzry1992 takes it off the graph immediately. So you can see that, as long as there exists a path from _s_ to _t_, she can always find a way to reach the goal (whatever the vertex _t_ has the outdegree of zero or not). Sample 2 However, sending orders is expensive, so your task is to find the minimum number of orders mzry1992 needs to send in the worst case. Please note that mzry1992 can give orders to the robot while it is walking on the graph. Look at the first sample to clarify that part of the problem. Input The first line contains two integers _n_ (1u2009≤u2009_n_u2009≤u2009106) — the number of vertices of the graph, and _m_ (1u2009≤u2009_m_u2009≤u2009106) — the number of edges. Then _m_ lines follow, each with two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_; _v__i_u2009≠u2009_u__i_), these integers denote that there is a directed edge from vertex _u__i_ to vertex _v__i_. The last line contains two integers _s_ and _t_ (1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_). It is guaranteed that there are no multiple edges and self-loops. Output If there is a way to reach a goal, print the required minimum number of orders in the worst case. Otherwise, print -1. Examples Input 4 6 1 2 2 1 1 3 3 1 2 4 3 4 1 4 Input 4 5 1 2 2 1 1 3 2 4 3 4 1 4 Note Consider the first test sample. Initially the robot is on vertex 1. So, on the first step the robot can go to vertex 2 or 3. No matter what vertex the robot chooses, mzry1992 must give an order to the robot. This order is to go to vertex 4. If mzry1992 doesn't give an order to the robot at vertex 2 or 3, the robot can choose the "bad" outgoing edge (return to vertex 1) according Law 3. So, the answer is one.
2,600
false
false
false
true
false
false
false
false
false
true
8,446
1340F
Nastya is a competitive programmer, but she is only studying now. Recently, Denis told her about the way to check if the string is correct bracket sequence. After that, unexpectedly, Nastya came up with a much more complex problem that Denis couldn't solve. Can you solve it? A string $$$s$$$ is given. It consists of $$$k$$$ kinds of pairs of brackets. Each bracket has the form $$$t$$$ xa0— it is an integer, such that $$$1 leq t leq k$$$. If the bracket has a form $$$t$$$, then: If $$$t > 0$$$, then it's an opening bracket of the type $$$t$$$. If $$$t < 0$$$, then it's a closing bracket of the type $$$-t$$$. Thus, there are $$$k$$$ types of pairs of brackets in total. The queries need to be answered: 1. Replace the bracket at position $$$i$$$ with the bracket of the form $$$t$$$. 2. Check if the substring from the $$$l$$$-th to $$$r$$$-th position (including) is the correct bracket sequence. Recall the definition of the correct bracket sequence: An empty sequence is correct. If $$$A$$$ and $$$B$$$ are two correct bracket sequences, then their concatenation "$$$A$$$ $$$B$$$" is also correct bracket sequence. If $$$A$$$ is the correct bracket sequence, $$$c$$$ $$$(1 leq c leq k)$$$ is a type of brackets, then the sequence "$$$c$$$ $$$A$$$ $$$-c$$$" is also correct bracket sequence. Input The first line contains an integer $$$n$$$ $$$(1 leq n leq 10^5)$$$ xa0— length of string and $$$k$$$ $$$(1 leq k leq n)$$$ xa0— the number of kinds of pairs of brackets. The second line contains string $$$s$$$ of length $$$n$$$ xa0— $$$n$$$ integers $$$s_1, s_2, ldots, s_n$$$ $$$(1 leq s_i leq k)$$$ The third line contains single integer $$$q$$$ $$$(1 leq q leq 10^5)$$$ xa0— the number of queries. Each of the following $$$q$$$ lines describes the queries: $$$1$$$ $$$i$$$ $$$t$$$ - query of the $$$1$$$ type $$$(1 leq i leq n, 1 leq t leq k)$$$. $$$2$$$ $$$l$$$ $$$r$$$ - query of the $$$2$$$ type $$$(1 leq l leq r leq n)$$$. Output For each query of $$$2$$$ type, output "Yes" if the substring from the query is the correct bracket sequence and "No", otherwise. All letters can be displayed in any case. Examples Input 6 2 1 2 -2 -1 1 -1 3 2 1 6 2 1 4 2 2 5 Input 2 2 -1 1 4 2 1 2 1 1 1 1 2 -1 2 1 2 Note In the fourth test, initially, the string is not a correct bracket sequence, so the answer to the first query is "No". After two changes it will be equal to "$$$1$$$ $$$-1$$$", so it is a correct bracket sequence and the answer to the fourth query is "Yes".
3,300
false
false
false
false
true
false
true
false
false
false
4,002
2005A
Narek has to spend 2 hours with some 2-year-old kids at the kindergarten. He wants to teach them competitive programming, and their first lesson is about palindromes. Narek found out that the kids only know the vowels of the English alphabet (the letters $$$mathtt{a}$$$, $$$mathtt{e}$$$, $$$mathtt{i}$$$, $$$mathtt{o}$$$, and $$$mathtt{u}$$$), so Narek needs to make a string that consists of vowels only. After making the string, he'll ask the kids to count the number of subsequences that are palindromes. Narek wants to keep it simple, so he's looking for a string such that the amount of palindrome subsequences is minimal. Help Narek find a string of length $$$n$$$, consisting of lowercase English vowels only (letters $$$mathtt{a}$$$, $$$mathtt{e}$$$, $$$mathtt{i}$$$, $$$mathtt{o}$$$, and $$$mathtt{u}$$$), which minimizes the amount of palindrome$$$^{dagger}$$$ subsequences$$$^{ddagger}$$$ in it. $$$^{dagger}$$$ A string is called a palindrome if it reads the same from left to right and from right to left. $$$^{ddagger}$$$ String $$$t$$$ is a subsequence of string $$$s$$$ if $$$t$$$ can be obtained from $$$s$$$ by removing several (possibly, zero or all) characters from $$$s$$$ and concatenating the remaining ones, without changing their order. For example, $$$mathtt{odocs}$$$ is a subsequence of $$$ exttt{c}{color{red}{ exttt{od}}} exttt{ef}{color{red}{ exttt{o}}} exttt{r}{color{red}{ exttt{c}}} exttt{e}{color{red}{ exttt{s}}}$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. Subsequently, the description of each test case follows. The only line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the size of the string. Note In the first example, $$$ exttt{uo}$$$ has only three palindrome subsequences: $$$ exttt{u}$$$, $$$ exttt{o}$$$, and the empty string. It can be shown that there is no better answer. In the third example, $$$ exttt{oeiiua}$$$ has only eight palindrome subsequences: $$$ exttt{o}$$$, $$$ exttt{e}$$$, $$$ exttt{i}$$$, $$$ exttt{i}$$$, $$$ exttt{u}$$$, $$$ exttt{a}$$$, $$$ exttt{ii}$$$, and the empty string. It can be shown that there is no better answer.
900
true
true
false
false
false
true
false
false
false
false
224
1603D
Problem - 1603D - 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 divide and conquer dp number theory *3000 No tag edit access → Contest materials let $$$c(l, r)$$$ denote the number of integer pairs $$$(i, j)$$$ such that $$$l le i le j le r$$$ and $$$operatorname{gcd}(i, j) ge l$$$. Here, $$$operatorname{gcd}(i, j)$$$ is the $$$ denote the minimum of $$$sumlimits_{i=1}^{k}{c(x_i+1,x_{i+1})}$$$ over all integer sequences $$$0=x_1 lt x_2 lt ldots lt x_{k} lt x_{k+1}=n$$$. Help YouKn0wWho find $$$f(n, k)$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 3 cdot 10^5$$$)xa0— the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^5$$$). Output For each test case, print a single integerxa0— $$$f(n, k)$$$. Example Input 4 6 2 4 4 3 1 10 3 Output 8 4 6 11 Note In the first test case, YouKn0wWho can select the sequence $$$
3,000
false
false
false
true
false
false
false
false
false
false
2,640
756E
There are _n_ types of coins in Byteland. Conveniently, the denomination of the coin type _k_ divides the denomination of the coin type _k_u2009+u20091, the denomination of the coin type 1 equals 1 tugrick. The ratio of the denominations of coin types _k_u2009+u20091 and _k_ equals _a__k_. It is known that for each _x_ there are at most 20 coin types of denomination _x_. Byteasar has _b__k_ coins of type _k_ with him, and he needs to pay exactly _m_ tugricks. It is known that Byteasar never has more than 3·105 coins with him. Byteasar want to know how many ways there are to pay exactly _m_ tugricks. Two ways are different if there is an integer _k_ such that the amount of coins of type _k_ differs in these two ways. As all Byteland citizens, Byteasar wants to know the number of ways modulo 109u2009+u20097. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u20093·105)xa0— the number of coin types. The second line contains _n_u2009-u20091 integers _a_1, _a_2, ..., _a__n_u2009-u20091 (1u2009≤u2009_a__k_u2009≤u2009109)xa0— the ratios between the coin types denominations. It is guaranteed that for each _x_ there are at most 20 coin types of denomination _x_. The third line contains _n_ non-negative integers _b_1, _b_2, ..., _b__n_xa0— the number of coins of each type Byteasar has. It is guaranteed that the sum of these integers doesn't exceed 3·105. The fourth line contains single integer _m_ (0u2009≤u2009_m_u2009<u20091010000)xa0— the amount in tugricks Byteasar needs to pay. Output Print single integerxa0— the number of ways to pay exactly _m_ tugricks modulo 109u2009+u20097. Note In the first example Byteasar has 4 coins of denomination 1, and he has to pay 2 tugricks. There is only one way. In the second example Byteasar has 4 coins of each of two different types of denomination 1, he has to pay 2 tugricks. There are 3 ways: pay one coin of the first type and one coin of the other, pay two coins of the first type, and pay two coins of the second type. In the third example the denominations are equal to 1, 3, 9.
3,200
true
false
false
true
false
false
false
false
false
false
6,766
1312E
You are given an array $$$a_1, a_2, dots, a_n$$$. You can perform the following operation any number of times: Choose a pair of two neighboring equal elements $$$a_i = a_{i + 1}$$$ (if there is at least one such pair). Replace them by one element with value $$$a_i + 1$$$. After each such operation, the length of the array will decrease by one (and elements are renumerated accordingly). What is the minimum possible length of the array $$$a$$$ you can get? Input The first line contains the single integer $$$n$$$ ($$$1 le n le 500$$$) — the initial length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 1000$$$) — the initial array $$$a$$$. Output Print the only integer — the minimum possible length you can get after performing the operation described above any number of times. Note In the first test, this is one of the optimal sequences of operations: $$$4$$$ $$$3$$$ $$$2$$$ $$$2$$$ $$$3$$$ $$$ ightarrow$$$ $$$4$$$ $$$3$$$ $$$3$$$ $$$3$$$ $$$ ightarrow$$$ $$$4$$$ $$$4$$$ $$$3$$$ $$$ ightarrow$$$ $$$5$$$ $$$3$$$. In the second test, this is one of the optimal sequences of operations: $$$3$$$ $$$3$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$3$$$ $$$3$$$ $$$ ightarrow$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$3$$$ $$$3$$$ $$$ ightarrow$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$ ightarrow$$$ $$$5$$$ $$$4$$$ $$$4$$$ $$$4$$$ $$$ ightarrow$$$ $$$5$$$ $$$5$$$ $$$4$$$ $$$ ightarrow$$$ $$$6$$$ $$$4$$$. In the third and fourth tests, you can't perform the operation at all.
2,100
false
true
false
true
false
false
false
false
false
false
4,133
596B
Problem - 596B - 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 implementation *1100 No tag edit access → Contest materials xa0— the length of the array _a__i_. Initially _a__i_u2009=u20090 for every position _i_, so this array is not given in the input. The second line of the input contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (u2009-u2009109u2009≤u2009_b__i_u2009≤u2009109). Output Print the minimum number of steps that Wilbur needs to make in order to achieve _a__i_u2009=u2009_b__i_ for all _i_. Examples Input 5 1 2 3 4 5 Output 5 Input 4 1 2 2 1 Output 3 Note In the first sample, Wilbur may successively choose indices 1, 2, 3, 4, and 5, and add 1 to corresponding suffixes. In the second sample, Wilbur first chooses indices 1 and 2 and adds 1 to corresponding suffixes, then he chooses index 4 and subtract 1.
1,100
false
true
true
false
false
false
false
false
false
false
7,464
1210E
Wojtek has just won a maths competition in Byteland! The prize is admirable — a great book called 'Card Tricks for Everyone.' 'Great!' he thought, 'I can finally use this old, dusted deck of cards that's always been lying unused on my desk!' The first chapter of the book is 'How to Shuffle $$$k$$$ Cards in Any Order You Want.' It's basically a list of $$$n$$$ intricate methods of shuffling the deck of $$$k$$$ cards in a deterministic way. Specifically, the $$$i$$$-th recipe can be described as a permutation $$$(P_{i,1}, P_{i,2}, dots, P_{i,k})$$$ of integers from $$$1$$$ to $$$k$$$. If we enumerate the cards in the deck from $$$1$$$ to $$$k$$$ from top to bottom, then $$$P_{i,j}$$$ indicates the number of the $$$j$$$-th card from the top of the deck after the shuffle. The day is short and Wojtek wants to learn only some of the tricks today. He will pick two integers $$$l, r$$$ ($$$1 le l le r le n$$$), and he will memorize each trick from the $$$l$$$-th to the $$$r$$$-th, inclusive. He will then take a sorted deck of $$$k$$$ cards and repeatedly apply random memorized tricks until he gets bored. He still likes maths, so he started wondering: how many different decks can he have after he stops shuffling it? Wojtek still didn't choose the integers $$$l$$$ and $$$r$$$, but he is still curious. Therefore, he defined $$$f(l, r)$$$ as the number of different decks he can get if he memorizes all the tricks between the $$$l$$$-th and the $$$r$$$-th, inclusive. What is the value of $$$$$$sum_{l=1}^n sum_{r=l}^n f(l, r)?$$$$$$ Input The first line contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 200,000$$$, $$$1 le k le 5$$$) — the number of tricks and the number of cards in Wojtek's deck. Each of the following $$$n$$$ lines describes a single trick and is described by $$$k$$$ distinct integers $$$P_{i,1}, P_{i,2}, dots, P_{i, k}$$$ ($$$1 le P_{i, j} le k$$$). Note Consider the first sample: The first trick swaps two top cards. The second trick takes a card from the bottom and puts it on the top of the deck. The third trick swaps two bottom cards. The first or the third trick allow Wojtek to generate only two distinct decks (either the two cards are swapped or not). Therefore, $$$f(1, 1) = f(3, 3) = 2$$$. The second trick allows him to shuffle the deck in a cyclic order. Therefore, $$$f(2,2)=3$$$. It turns that two first tricks or two last tricks are enough to shuffle the deck in any way desired by Wojtek. Therefore, $$$f(1,2) = f(2,3) = f(1,3) = 3! = 6$$$.
2,700
true
false
false
false
false
false
false
false
false
false
4,632
1860B
Monocarp is going to make a purchase with cost of exactly $$$m$$$ burles. He has two types of coins, in the following quantities: coins worth $$$1$$$ burle: $$$a_1$$$ regular coins and infinitely many fancy coins; coins worth $$$k$$$ burles: $$$a_k$$$ regular coins and infinitely many fancy coins. Monocarp wants to make his purchase in such a way that there's no changexa0— the total worth of provided coins is exactly $$$m$$$. He can use both regular and fancy coins. However, he wants to spend as little fancy coins as possible. What's the smallest total number of fancy coins he can use to make a purchase? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 3 cdot 10^4$$$)xa0— the number of testcases. The only line of each testcase contains four integers $$$m, k, a_1$$$ and $$$a_k$$$ ($$$1 le m le 10^8$$$; $$$2 le k le 10^8$$$; $$$0 le a_1, a_k le 10^8$$$)xa0— the cost of the purchase, the worth of the second type of coin and the amounts of regular coins of both types, respectively. Output For each testcase, print a single integerxa0— the smallest total number of fancy coins Monocarp can use to make a purchase. Example Input 4 11 3 0 0 11 3 20 20 11 3 6 1 100000000 2 0 0 Note In the first testcase, there are no regular coins of either type. Monocarp can use $$$2$$$ fancy coins worth $$$1$$$ burle and $$$3$$$ fancy coins worth $$$3$$$ (since $$$k=3$$$) burles to get $$$11$$$ total burles with $$$5$$$ total fancy coins. In the second testcase, Monocarp has a lot of regular coins of both types. He can use $$$11$$$ regular coins worth $$$1$$$ burle, for example. Notice that Monocarp doesn't have to minimize the total number of used coins. That way he uses $$$0$$$ fancy coins. In the third testcase, Monocarp can use $$$5$$$ regular coins worth $$$1$$$ burle and $$$1$$$ regular coin worth $$$3$$$ burles. That will get him to $$$8$$$ total burles when he needs $$$11$$$. So, $$$1$$$ fancy coin worth $$$3$$$ burles is enough.
1,200
true
true
false
false
false
false
true
true
false
false
1,124
216B
One day _n_ students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people. We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student _A_ is an archenemy to student _B_, then student _B_ is an archenemy to student _A_. The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench. Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last. Input The first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u2009100) — the number of students and the number of pairs of archenemies correspondingly. Next _m_ lines describe enmity between students. Each enmity is described as two numbers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_) — the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies. You can consider the students indexed in some manner with distinct integers from 1 to _n_. Output Print a single integer — the minimum number of students you will have to send to the bench in order to start the game. Examples Input 6 6 1 2 2 3 3 1 4 5 5 6 6 4
1,700
false
false
true
false
false
false
false
false
false
false
8,975
1203C
Problem - 1203C - 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 math *1300 No tag edit access → Contest materials Editorial") . Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 4 cdot 10^5$$$) — 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^{12}$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Output Print one integer — the number of such positive integers $$$x$$$ such that $$$x$$$ divides each number from the given array (in other words, the answer is the number of common divisors of all elements in the array). Examples Input 5 1 2 3 4 5 Output 1 Input 6 6 90 12 18 30 18 Output 4
1,300
true
false
true
false
false
false
false
false
false
false
4,681
1690B
Kristina has two arrays $$$a$$$ and $$$b$$$, each containing $$$n$$$ non-negative integers. She can perform the following operation on array $$$a$$$ any number of times: apply a decrement to each non-zero element of the array, that is, replace the value of each element $$$a_i$$$ such that $$$a_i > 0$$$ with the value $$$a_i - 1$$$ ($$$1 le i le n$$$). If $$$a_i$$$ was $$$0$$$, its value does not change. Determine whether Kristina can get an array $$$b$$$ from an array $$$a$$$ in some number of operations (probably zero). In other words, can she make $$$a_i = b_i$$$ after some number of operations for each $$$1 le i le n$$$? For example, let $$$n = 4$$$, $$$a = [3, 5, 4, 1]$$$ and $$$b = [1, 3, 2, 0]$$$. In this case, she can apply the operation twice: after the first application of the operation she gets $$$a = [2, 4, 3, 0]$$$; after the second use of the operation she gets $$$a = [1, 3, 2, 0]$$$. Thus, in two operations, she can get an array $$$b$$$ from an array $$$a$$$. Input The first line of the input 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. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^4$$$). The second line of each test case contains exactly $$$n$$$ non-negative integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$). The third line of each test case contains exactly $$$n$$$ non-negative integers $$$b_1, b_2, dots, b_n$$$ ($$$0 le b_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ values over all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output For each test case, output on a separate line: YES, if by doing some number of operations it is possible to get an array $$$b$$$ from an array $$$a$$$; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). Example Input 6 4 3 5 4 1 1 3 2 0 3 1 2 1 0 1 0 4 5 3 7 2 1 1 1 1 5 1 2 3 4 5 1 2 3 4 6 1 8 0 1 4 6 Output YES YES NO NO YES NO Note The first test case is analyzed in the statement. In the second test case, it is enough to apply the operation to array $$$a$$$ once. In the third test case, it is impossible to get array $$$b$$$ from array $$$a$$$.
800
false
true
true
false
false
false
false
false
false
false
2,142
1215E
Monocarp has arranged $$$n$$$ colored marbles in a row. The color of the $$$i$$$-th marble is $$$a_i$$$. Monocarp likes ordered things, so he wants to rearrange marbles in such a way that all marbles of the same color form a contiguos segment (and there is only one such segment for each color). In other words, Monocarp wants to rearrange marbles so that, for every color $$$j$$$, if the leftmost marble of color $$$j$$$ is $$$l$$$-th in the row, and the rightmost marble of this color has position $$$r$$$ in the row, then every marble from $$$l$$$ to $$$r$$$ has color $$$j$$$. To achieve his goal, Monocarp can do the following operation any number of times: choose two neighbouring marbles, and swap them. You have to calculate the minimum number of operations Monocarp has to perform to rearrange the marbles. Note that the order of segments of marbles having equal color does not matter, it is only required that, for every color, all the marbles of this color form exactly one contiguous segment. Input The first line contains one integer $$$n$$$ $$$(2 le n le 4 cdot 10^5)$$$ — the number of marbles. The second line contains an integer sequence $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le 20)$$$, where $$$a_i$$$ is the color of the $$$i$$$-th marble. Output Print the minimum number of operations Monocarp has to perform to achieve his goal. Examples Input 13 5 5 4 4 3 5 7 6 5 4 4 6 5 Note In the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is $$$[3, 4, 3, 2, 4, 2, 2]$$$. Then Monocarp should swap the second and the third marbles, so the sequence is $$$[3, 3, 4, 2, 4, 2, 2]$$$. And finally, Monocarp should swap the fourth and the fifth marbles, so the sequence is $$$[3, 3, 4, 4, 2, 2, 2]$$$. In the second example there's no need to perform any operations.
2,200
false
false
false
true
false
false
false
false
false
false
4,599
89E
The Fire Lord attacked the Frost Kingdom. He has already got to the Ice Fortress, where the Snow Queen dwells. He arranged his army on a segment _n_ in length not far from the city walls. And only the frost magician Solomon can save the Frost Kingdom. The _n_-long segment is located at a distance equal exactly to 1 from the castle walls. It can be imaginarily divided into unit segments. On some of the unit segments fire demons are located — no more than one demon per position. Each demon is characterised by his strength - by some positive integer. We can regard the fire demons being idle. Initially Solomon is positioned on the fortress wall. He can perform the following actions several times in a row: "L" — Solomon shifts one unit to the left. This movement cannot be performed on the castle wall. "R" — Solomon shifts one unit to the left. This movement cannot be performed if there's no ice block to the right. "A" — If there's nothing to the right of Solomon, then Solomon creates an ice block that immediately freezes to the block that Solomon is currently standing on. If there already is an ice block, then Solomon destroys it. At that the ice blocks to the right of the destroyed one can remain but they are left unsupported. Those ice blocks fall down. Solomon spends exactly a second on each of these actions. As the result of Solomon's actions, ice blocks' segments fall down. When an ice block falls on a fire demon, the block evaporates and the demon's strength is reduced by 1. When the demons' strength is equal to 0, the fire demon vanishes. The picture below shows how it happens. The ice block that falls on the position with no demon, breaks into lots of tiny pieces and vanishes without hurting anybody. Help Solomon destroy all the Fire Lord's army in minimum time. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091000). The next line contains _n_ numbers, the _i_-th of them represents the strength of the fire demon standing of the _i_-th position, an integer from 1 to 100. If there's no demon on the _i_-th position, then the _i_-th number equals to 0. It is guaranteed that the input data have at least one fire demon. Output Print a string of minimum length, containing characters "L", "R" and "A" — the succession of actions leading to the required result. If there are several possible answers, print any of them.
2,900
false
true
false
false
false
false
false
false
false
false
9,525
1253E
The mayor of the Central Town wants to modernize Central Street, represented in this problem by the $$$(Ox)$$$ axis. On this street, there are $$$n$$$ antennas, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th antenna lies on the position $$$x_i$$$ and has an initial scope of $$$s_i$$$: it covers all integer positions inside the interval $$$[x_i - s_i; x_i + s_i]$$$. It is possible to increment the scope of any antenna by $$$1$$$, this operation costs $$$1$$$ coin. We can do this operation as much as we want (multiple times on the same antenna if we want). To modernize the street, we need to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Note that it is authorized to cover positions outside $$$[1; m]$$$, even if it's not required. What is the minimum amount of coins needed to achieve this modernization? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 80$$$ and $$$n le m le 100 000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$s_i$$$ ($$$1 le x_i le m$$$ and $$$0 le s_i le m$$$). On each position, there is at most one antenna (values $$$x_i$$$ are pairwise distinct). Output You have to output a single integer: the minimum amount of coins required to make all integer positions from $$$1$$$ to $$$m$$$ inclusive covered by at least one antenna. Examples Input 3 595 43 2 300 4 554 10 Input 5 240 13 0 50 25 60 5 155 70 165 70 Note In the first example, here is a possible strategy: Increase the scope of the first antenna by $$$40$$$, so that it becomes $$$2 + 40 = 42$$$. This antenna will cover interval $$$[43 - 42; 43 + 42]$$$ which is $$$[1; 85]$$$ Increase the scope of the second antenna by $$$210$$$, so that it becomes $$$4 + 210 = 214$$$. This antenna will cover interval $$$[300 - 214; 300 + 214]$$$, which is $$$[86; 514]$$$ Increase the scope of the third antenna by $$$31$$$, so that it becomes $$$10 + 31 = 41$$$. This antenna will cover interval $$$[554 - 41; 554 + 41]$$$, which is $$$[513; 595]$$$ Total cost is $$$40 + 210 + 31 = 281$$$. We can prove that it's the minimum cost required to make all positions from $$$1$$$ to $$$595$$$ covered by at least one antenna. Note that positions $$$513$$$ and $$$514$$$ are in this solution covered by two different antennas, but it's not important. — In the second example, the first antenna already covers an interval $$$[0; 2]$$$ so we have nothing to do. Note that the only position that we needed to cover was position $$$1$$$; positions $$$0$$$ and $$$2$$$ are covered, but it's not important.
2,200
false
true
false
true
true
false
false
false
true
false
4,418
1700E
Little pirate Serega robbed a ship with puzzles of different kinds. Among all kinds, he liked only one, the hardest. A puzzle is a table of $$$n$$$ rows and $$$m$$$ columns, whose cells contain each number from $$$1$$$ to $$$n cdot m$$$ exactly once. To solve a puzzle, you have to find a sequence of cells in the table, such that any two consecutive cells are adjacent by the side in the table. The sequence can have arbitrary length and should visit each cell one or more times. For a cell containing the number $$$i$$$, denote the position of the first occurrence of this cell in the sequence as $$$t_i$$$. The sequence solves the puzzle, if $$$t_1 < t_2 < dots < t_{nm}$$$. In other words, the cell with number $$$x$$$ should be first visited before the cell with number $$$x + 1$$$ for each $$$x$$$. Let's call a puzzle solvable, if there exists at least one suitable sequence. In one move Serega can choose two arbitrary cells in the table (not necessarily adjacent by the side) and swap their numbers. He would like to know the minimum number of moves to make his puzzle solvable, but he is too impatient. Thus, please tell if the minimum number of moves is $$$0$$$, $$$1$$$, or at least $$$2$$$. In the case, where $$$1$$$ move is required, please also find the number of suitable cell pairs to swap. Input In the first line there are two whole positive numbers $$$n, m$$$ ($$$1 leq ncdot m leq 400,000$$$) — table dimensions. In the next $$$n$$$ lines there are $$$m$$$ integer numbers $$$a_{i1}, a_{i2}, dots, a_{im}$$$ ($$$1 le a_{ij} le nm$$$). It is guaranteed that every number from $$$1$$$ to $$$nm$$$ occurs exactly once in the table. Output Let $$$a$$$ be the minimum number of moves to make the puzzle solvable. If $$$a = 0$$$, print $$$0$$$. If $$$a = 1$$$, print $$$1$$$ and the number of valid swaps. If $$$a ge 2$$$, print $$$2$$$. Examples Input 3 3 2 1 3 6 7 4 9 8 5 Note In the first example the sequence $$$(1, 2), (1, 1), (1, 2), (1, 3), (2, 3), (3, 3)$$$, $$$(2, 3), (1, 3), (1, 2), (1, 1), (2, 1), (2, 2), (3, 2), (3, 1)$$$ solves the puzzle, so the answer is $$$0$$$. The puzzle in the second example can't be solved, but it's solvable after any of three swaps of cells with values $$$(1, 5), (1, 6), (2, 6)$$$. The puzzle from the third example requires at least two swaps, so the answer is $$$2$$$.
2,600
false
false
false
false
false
true
true
false
false
false
2,078
1349C
Please notice the unusual memory limit of this problem. Orac likes games. Recently he came up with the new game, "Game of Life". You should play this game on a black and white grid with $$$n$$$ rows and $$$m$$$ columns. Each cell is either black or white. For each iteration of the game (the initial iteration is $$$0$$$), the color of each cell will change under the following rules: If there are no adjacent cells with the same color as this cell on the current iteration, the color of it on the next iteration will be the same. Otherwise, the color of the cell on the next iteration will be different. Two cells are adjacent if they have a mutual edge. Now Orac has set an initial situation, and he wants to know for the cell $$$(i,j)$$$ (in $$$i$$$-th row and $$$j$$$-th column), what will be its color at the iteration $$$p$$$. He may ask you these questions several times. Input The first line contains three integers $$$n,m,t (1le n,mle 1000, 1le tle 100,000)$$$, representing the number of rows, columns, and the number of Orac queries. Each of the following $$$n$$$ lines contains a binary string of length $$$m$$$, the $$$j$$$-th character in $$$i$$$-th line represents the initial color of cell $$$(i,j)$$$. '0' stands for white, '1' stands for black. Each of the following $$$t$$$ lines contains three integers $$$i,j,p (1le ile n, 1le jle m, 1le ple 10^{18})$$$, representing a query from Orac. Output Print $$$t$$$ lines, in $$$i$$$-th line you should print the answer to the $$$i$$$-th query by Orac. If the color of this cell is black, you should print '1'; otherwise, you should write '0'. Examples Input 3 3 3 000 111 000 1 1 1 2 2 2 3 3 3 Input 5 2 2 01 10 01 10 01 1 1 4 5 1 4 Input 5 5 3 01011 10110 01101 11010 10101 1 1 4 1 2 3 5 5 3 Input 1 1 3 0 1 1 1 1 1 2 1 1 3 Note For the first example, the picture above shows the initial situation and the color of cells at the iteration $$$1$$$, $$$2$$$, and $$$3$$$. We can see that the color of $$$(1,1)$$$ at the iteration $$$1$$$ is black, the color of $$$(2,2)$$$ at the iteration $$$2$$$ is black, and the color of $$$(3,3)$$$ at the iteration $$$3$$$ is also black. For the second example, you can prove that the cells will never change their colors.
2,000
false
false
true
false
false
false
false
false
false
true
3,962
1677A
Tokitsukaze has a permutation $$$p$$$ of length $$$n$$$. Recall that a permutation $$$p$$$ of length $$$n$$$ is a sequence $$$p_1, p_2, ldots, p_n$$$ consisting of $$$n$$$ distinct integers, each of which from $$$1$$$ to $$$n$$$ ($$$1 leq p_i leq n$$$). She wants to know how many different indices tuples $$$[a,b,c,d]$$$ ($$$1 leq a < b < c < d leq n$$$) in this permutation satisfy the following two inequalities: $$$p_a < p_c$$$ and $$$p_b > p_d$$$. Note that two tuples $$$[a_1,b_1,c_1,d_1]$$$ and $$$[a_2,b_2,c_2,d_2]$$$ are considered to be different if $$$a_1 e a_2$$$ or $$$b_1 e b_2$$$ or $$$c_1 e c_2$$$ or $$$d_1 e d_2$$$. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$4 leq n leq 5000$$$)xa0— the length of permutation $$$p$$$. The second line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0— the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each test case, print a single integerxa0— the number of different $$$[a,b,c,d]$$$ tuples. Example Input 3 6 5 3 6 1 4 2 4 1 2 3 4 10 5 1 6 2 8 3 4 10 9 7 Note In the first test case, there are $$$3$$$ different $$$[a,b,c,d]$$$ tuples. $$$p_1 = 5$$$, $$$p_2 = 3$$$, $$$p_3 = 6$$$, $$$p_4 = 1$$$, where $$$p_1 < p_3$$$ and $$$p_2 > p_4$$$ satisfies the inequality, so one of $$$[a,b,c,d]$$$ tuples is $$$[1,2,3,4]$$$. Similarly, other two tuples are $$$[1,2,3,6]$$$, $$$[2,3,5,6]$$$.
1,600
false
false
false
true
true
false
true
false
false
false
2,206
1780A
Today Hayato came home from school with homework. In the assignment, Hayato was given an array $$$a$$$ of length $$$n$$$. The task was to find $$$3$$$ numbers in this array whose sum is odd. At school, he claimed that there are such $$$3$$$ numbers, but Hayato was not sure, so he asked you for help. Answer if there are such three numbers, and if so, output indices $$$i$$$, $$$j$$$, and $$$k$$$ such that $$$a_i + a_j + a_k$$$ is odd. The odd numbers are integers that are not divisible by $$$2$$$: $$$1$$$, $$$3$$$, $$$5$$$, and so on. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. For each test case, the first line contains one integer $$$n$$$ ($$$3 le n le 300$$$) — the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^5$$$) — the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, in the first line print one word "YES" (without quotes) if there are $$$3$$$ numbers with an odd sum or "NO" (without quotes) if there are no such $$$3$$$ numbers. If the answer exists, then on the second line print $$$3$$$ distinct integers $$$i, j, k$$$ ($$$1 le i, j, k le n$$$) — the indices of the numbers. If there are several answers, output any. Example Input 6 3 1 1 1 4 1 1 2 2 3 1 2 3 5 1 4 5 1 2 4 2 6 2 4 5 5 6 3 2 1 Output YES 1 2 3 YES 3 4 1 NO YES 1 3 4 NO YES 1 3 5 Note In the first test case, there is one way to choose $$$3$$$ numbers, and since $$$1 + 1 + 1 = 3$$$, this triple is fine for us. In the second test case, you need to choose the numbers $$$1, 2, 2$$$, since $$$1 + 2 + 2 = 5$$$. In the third test case, there is one way to choose three numbers, but $$$1 + 2 + 3 = 6$$$ is an even number, so the required triple does not exist. In the fifth test case, no matter what three numbers we choose, their sum is even.
800
false
true
false
false
false
true
false
false
false
false
1,594
1849D
You are given an array of $$$n$$$ integers, where each integer is either $$$0$$$, $$$1$$$, or $$$2$$$. Initially, each element of the array is blue. Your goal is to paint each element of the array red. In order to do so, you can perform operations of two types: pay one coin to choose a blue element and paint it red; choose a red element which is not equal to $$$0$$$ and a blue element adjacent to it, decrease the chosen red element by $$$1$$$, and paint the chosen blue element red. What is the minimum number of coins you have to spend to achieve your goal? Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 2$$$). Output Print one integer — the minimum number of coins you have to spend in order to paint all elements red. Note In the first example, you can paint all elements red with having to spend only one coin as follows: 1. paint the $$$2$$$-nd element red by spending one coin; 2. decrease the $$$2$$$-nd element by $$$1$$$ and paint the $$$1$$$-st element red; 3. decrease the $$$2$$$-nd element by $$$1$$$ and paint the $$$3$$$-rd element red. In the second example, you can paint all elements red spending only two coins as follows: 1. paint the $$$4$$$-th element red by spending one coin; 2. decrease the $$$4$$$-th element by $$$1$$$ and paint the $$$3$$$-rd element red; 3. paint the $$$1$$$-st element red by spending one coin; 4. decrease the $$$3$$$-rd element by $$$1$$$ and paint the $$$2$$$-nd element red.
1,700
false
true
false
false
false
true
false
false
false
false
1,185
248E
Piglet has got a birthday today. His friend Winnie the Pooh wants to make the best present for him — a honey pot. Of course Winnie realizes that he won't manage to get the full pot to Piglet. In fact, he is likely to eat all the honey from the pot. And as soon as Winnie planned a snack on is way, the pot should initially have as much honey as possible. The day before Winnie the Pooh replenished his honey stocks. Winnie-the-Pooh has _n_ shelves at home, each shelf contains some, perhaps zero number of honey pots. During the day Winnie came to the honey shelves _q_ times; on the _i_-th time he came to some shelf _u__i_, took from it some pots _k__i_, tasted the honey from each pot and put all those pots on some shelf _v__i_. As Winnie chose the pots, he followed his intuition. And that means that among all sets of _k__i_ pots on shelf _u__i_, he equiprobably chooses one. Now Winnie remembers all actions he performed with the honey pots. He wants to take to the party the pot he didn't try the day before. For that he must know the mathematical expectation of the number _m_ of shelves that don't have a single untasted pot. To evaluate his chances better, Winnie-the-Pooh wants to know the value _m_ after each action he performs. Your task is to write a program that will find those values for him. Input The first line of the input contains a single number _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of shelves at Winnie's place. The second line contains _n_ integers _a__i_ (1u2009≤u2009_i_u2009≤u2009_n_, 0u2009≤u2009_a__i_u2009≤u2009100) — the number of honey pots on a shelf number _i_. The next line contains integer _q_ (1u2009≤u2009_q_u2009≤u2009105) — the number of actions Winnie did the day before. Then follow _q_ lines, the _i_-th of them describes an event that follows chronologically; the line contains three integers _u__i_, _v__i_ and _k__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, 1u2009≤u2009_k__i_u2009≤u20095) — the number of the shelf from which Winnie took pots, the number of the shelf on which Winnie put the pots after he tasted each of them, and the number of the pots Winnie tasted, correspondingly. Consider the shelves with pots numbered with integers from 1 to _n_. It is guaranteed that Winnie-the-Pooh Never tried taking more pots from the shelf than it has. Output For each Winnie's action print the value of the mathematical expectation _m_ by the moment when this action is performed. The relative or absolute error of each value mustn't exceed 10u2009-u20099. Examples Input 3 2 2 3 5 1 2 1 2 1 2 1 2 2 3 1 1 3 2 2 Output 0.000000000000 0.333333333333 1.000000000000 1.000000000000 2.000000000000
2,600
true
false
false
true
false
false
false
false
false
false
8,839
1137D
This is an interactive problem. Misha likes to play cooperative games with incomplete information. Today he suggested ten his friends to play a cooperative game "Lake". Misha has already come up with a field for the upcoming game. The field for this game is a directed graph consisting of two parts. The first part is a road along the coast of the lake which is a cycle of $$$c$$$ vertices. The second part is a path from home to the lake which is a chain of $$$t$$$ vertices, and there is an edge from the last vertex of this chain to the vertex of the road along the coast which has the most beautiful view of the lake, also known as the finish vertex. Misha decided to keep the field secret, so nobody knows neither $$$t$$$ nor $$$c$$$. Note that each vertex of the field has exactly one outgoing edge and all the vertices except the home vertex and the finish vertex have exactly one ingoing edge. The home vertex has no incoming edges, the finish vertex has two incoming edges. At the beginning of the game pieces of all the ten players, indexed with consecutive integers from $$$0$$$ to $$$9$$$, are at the home vertex. After that on each turn some of the players can ask Misha to simultaneously move their pieces along the corresponding edges. Misha will not answer more than $$$q$$$ such queries. After each move Misha will tell players whose pieces are at the same vertices and whose pieces are at different vertices. The goal of the game is to move all the pieces to the finish vertex. Misha's friends have no idea how to win in such a game without knowledge of $$$c$$$, $$$t$$$ and $$$q$$$, but luckily they are your friends. Help them: coordinate their actions to win the game. Misha has drawn such a field that $$$1 le t, c$$$, $$$(t+c) leq 1000$$$ and $$$q = 3 cdot (t+c)$$$. Input There is no inputxa0— go to the interaction part straight away. Output After all friends gather at the finish vertex, print "done" and terminate your program. Interaction To give a command to move the friends, print "next" and then space-separated indices of the friends you want to move. For example, to give the command to move the friends with indices $$$0$$$, $$$2$$$, $$$5$$$ and $$$9$$$ print "next 0 2 5 9". At each turn, you must move at least one of your friends. As an answer, first read an integer $$$k$$$, and then $$$10$$$ digits divided into $$$k$$$ space-separated groups. The friends that correspond to the indices in the same group are in the same vertex. The friends that correspond to indices in different groups are in different vertices. The indices in each group follow in ascending order. For example, the answer "2 05 12346789" means that the friends with indices $$$0$$$ and $$$5$$$ are in one vertex, and all other friends are in the same but different vertex. The answer "4 01 567 234 89" means that Misha's friends are in four different vertices: the friends with indices $$$0$$$ and $$$1$$$ are in the first, the friends with indices $$$5$$$, $$$6$$$ and $$$7$$$ are in the second, the friends with indices $$$2$$$, $$$3$$$ and $$$4$$$ are in the third, and the friends with indices $$$8$$$ and $$$9$$$ are in the fourth. After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Answer "stop" instead of a valid one means that you made an invalid query. Exit immediately after receiving "stop" and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. Hacks In order to hack, print two integers $$$t$$$ and $$$c$$$ in a single line ($$$1 le t, c$$$, $$$(t+c) leq 1000$$$). Example Input 2 05 12346789 3 246789 135 0 3 246789 0 135 3 246789 0 135 2 135 0246789 1 0123456789 Output next 0 5 next 0 1 3 next 2 3 0 1 4 5 6 7 8 9 next 9 8 7 6 5 4 3 2 1 0 next 0 1 3 5 next 1 3 5 done Note In the sample input and output values are aligned only for simplicity of interpreting them chronologically. In real interaction no "extra" line breaks should appear. In the example, the friends move as follows:
2,400
false
false
false
false
false
true
false
false
false
false
5,039
1929F
Having overcome all the difficulties and hardships, Sasha finally decided to marry his girlfriend. To do this, he needs to give her an engagement ring. However, his girlfriend does not like such romantic gestures, but she does like binary search trees$$$^{dagger}$$$. So Sasha decided to give her such a tree. After spending a lot of time on wedding websites for programmers, he found the perfect binary search tree with the root at vertex $$$1$$$. In this tree, the value at vertex $$$v$$$ is equal to $$$val_v$$$. But after some time, he forgot the values in some vertices. Trying to remember the found tree, Sasha wondered — how many binary search trees could he have found on the website, if it is known that the values in all vertices are integers in the segment $$$[1, C]$$$. Since this number can be very large, output it modulo $$$998,244,353$$$. $$$^{dagger}$$$A binary search tree is a rooted binary tree in which for any vertex $$$x$$$, the following property holds: the values of all vertices in the left subtree of vertex $$$x$$$ (if it exists) are less than or equal to the value at vertex $$$x$$$, and the values of all vertices in the right subtree of vertex $$$x$$$ (if it exists) are greater than or equal to the value at vertex $$$x$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$C$$$ ($$$2 leq n leq 5 cdot 10^5$$$, $$$1 leq C leq 10^9$$$) — the number of vertices in the tree and the maximum allowed value at the vertex. The next $$$n$$$ lines describe the vertices of the tree. The $$$i$$$-th line contains three integers $$$L_i, R_i$$$ and $$$val_i$$$ ($$$-1 le L_i, R_i le n$$$, $$$-1 le val_i le C$$$, $$$L_i, R_i, val_i e 0$$$) — the number of the left child, the number of the right child, and the value at the $$$i$$$-th vertex, respectively. If $$$L_i = -1$$$, then the $$$i$$$-th vertex has no left son. If $$$R_i = -1$$$, then the $$$i$$$-th vertex has no right son. If $$$val_i = -1$$$, then the value at the $$$i$$$-th vertex is unknown. It is guaranteed that at least one suitable binary search tree exists. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output a single integer — the number of suitable binary search trees modulo $$$998,244,353$$$. Example Input 3 5 5 2 3 -1 -1 -1 2 4 -1 3 -1 5 -1 -1 -1 -1 3 69 2 3 47 -1 -1 13 -1 -1 69 3 3 2 3 -1 -1 -1 -1 -1 -1 -1 Note In the first test case, the binary search tree has the following form: Then the possible values at the vertices are: $$$[2, 2, 3, 2, 2]$$$, $$$[2, 2, 3, 2, 3]$$$, $$$[2, 2, 3, 3, 3]$$$, and $$$[3, 2, 3, 3, 3]$$$. In the second test case, the values at all vertices are known, so there is only one suitable binary search tree.
2,300
true
false
false
false
true
false
true
false
false
false
712
865A
Attention: we lost all the test cases for this problem, so instead of solving the problem, we need you to generate test cases. We're going to give you the answer, and you need to print a test case that produces the given answer. The original problem is in the following paragraph. People don't use cash as often as they used to. Having a credit card solves some of the hassles of cash, such as having to receive change when you can't form the exact amount of money needed to purchase an item. Typically cashiers will give you as few coins as possible in change, but they don't have to. For example, if your change is 30 cents, a cashier could give you a 5 cent piece and a 25 cent piece, or they could give you three 10 cent pieces, or ten 1 cent pieces, two 5 cent pieces, and one 10 cent piece. Altogether there are 18 different ways to make 30 cents using only 1 cent pieces, 5 cent pieces, 10 cent pieces, and 25 cent pieces. Two ways are considered different if they contain a different number of at least one type of coin. Given the denominations of the coins and an amount of change to be made, how many different ways are there to make change? As we mentioned before, we lost all the test cases for this problem, so we're actually going to give you the number of ways, and want you to produce a test case for which the number of ways is the given number. There could be many ways to achieve this (we guarantee there's always at least one), so you can print any, as long as it meets the constraints described below. Output In the first line print integers _N_ and _M_ (1u2009≤u2009_N_u2009≤u2009106,u20091u2009≤u2009_M_u2009≤u200910), the amount of change to be made, and the number of denominations, respectively. Then print _M_ integers _D_1,u2009_D_2,u2009...,u2009_D__M_ (1u2009≤u2009_D__i_u2009≤u2009106), the denominations of the coins. All denominations must be distinct: for any _i_u2009≠u2009_j_ we must have _D__i_u2009≠u2009_D__j_. If there are multiple tests, print any of them. You can print denominations in atbitrary order.
1,400
false
false
false
false
false
true
false
false
false
false
6,281
1549B
There is a chessboard of size $$$n$$$ by $$$n$$$. The square in the $$$i$$$-th row from top and $$$j$$$-th column from the left is labelled $$$(i,j)$$$. Currently, Gregor has some pawns in the $$$n$$$-th row. There are also enemy pawns in the $$$1$$$-st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from $$$(i,j)$$$ to $$$(i-1,j)$$$) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from $$$(i,j)$$$ to either $$$(i-1,j-1)$$$ or $$$(i-1,j+1)$$$) if and only if there is an enemy pawn in that square. The enemy pawn is also removed. Gregor wants to know what is the maximum number of his pawns that can reach row $$$1$$$? Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row $$$1$$$, it is stuck and cannot make any further moves. Input The first line of the input contains one integer $$$t$$$ ($$$1le tle 2cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of three lines. The first line contains a single integer $$$n$$$ ($$$2le nle 2cdot{10}^{5}$$$) — the size of the chessboard. The second line consists of a string of binary digits of length $$$n$$$, where a $$$1$$$ in the $$$i$$$-th position corresponds to an enemy pawn in the $$$i$$$-th cell from the left, and $$$0$$$ corresponds to an empty cell. The third line consists of a string of binary digits of length $$$n$$$, where a $$$1$$$ in the $$$i$$$-th position corresponds to a Gregor's pawn in the $$$i$$$-th cell from the left, and $$$0$$$ corresponds to an empty cell. It is guaranteed that the sum of $$$n$$$ across all test cases is less than $$$2cdot{10}^{5}$$$. Output For each test case, print one integer: the maximum number of Gregor's pawns which can reach the $$$1$$$-st row. Example Input 4 3 000 111 4 1111 1111 3 010 010 5 11001 00000 Note In the first example, Gregor can simply advance all $$$3$$$ of his pawns forward. Thus, the answer is $$$3$$$. In the second example, Gregor can guarantee that all $$$4$$$ of his pawns reach the enemy row, by following the colored paths as demonstrated in the diagram below. Remember, only Gregor takes turns in this "game"! In the third example, Gregor's only pawn is stuck behind the enemy pawn, and cannot reach the end. In the fourth example, Gregor has no pawns, so the answer is clearly $$$0$$$.
800
false
true
true
true
false
false
false
false
false
true
2,899
1488E
A subsequence is a sequence that can be obtained from another sequence by removing some elements without changing the order of the remaining elements. A palindromic sequence is a sequence that is equal to the reverse of itself. You are given a sequence of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. Any integer value appears in $$$a$$$ no more than twice. What is the length of the longest palindromic subsequence of sequence $$$a$$$? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 250,000$$$)xa0— the number of elements in the sequence. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). Any integer value appears in $$$a$$$ no more than twice. The sum of $$$n$$$ over all testcases doesn't exceed $$$250,000$$$. Output For each testcase print a single integerxa0— the length of the longest palindromic subsequence of sequence $$$a$$$. Example Input 5 6 2 1 3 1 5 2 6 1 3 3 4 4 1 1 1 2 1 1 7 4 4 2 5 7 2 3 Note Here are the longest palindromic subsequences for the example testcases: 2 1 3 1 5 2 1 3 3 4 4 1 or 1 3 3 4 4 1 1 1 1 4 4 2 5 7 2 3 or 4 4 2 5 7 2 3
2,200
false
false
false
true
true
false
false
false
false
false
3,235
2000H
Ksyusha decided to start a game development company. To stand out among competitors and achieve success, she decided to write her own game engine. The engine must support a set initially consisting of $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$. The set will undergo $$$m$$$ operations sequentially. The operations can be of the following types: Insert element $$$x$$$ into the set; Remove element $$$x$$$ from the set; Report the $$$k$$$-load of the set. The $$$k$$$-load of the set is defined as the minimum positive integer $$$d$$$ such that the integers $$$d, d + 1, ldots, d + (k - 1)$$$ do not appear in this set. For example, the $$$3$$$-load of the set $$${3, 4, 6, 11}$$$ is $$$7$$$, since the integers $$$7, 8, 9$$$ are absent from the set, and no smaller value fits. Ksyusha is busy with management tasks, so you will have to write the engine. Implement efficient support for the described operations. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The following lines describe the test cases. The first line contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the initial size of the set. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_1 < a_2 < ldots < a_n le 2 cdot 10^6$$$)xa0— the initial state of the set. The third line contains an integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$)xa0— the number of operations. The next $$$m$$$ lines contain the operations. The operations are given in the following format: + $$$x$$$ ($$$1 le x le 2 cdot 10^6$$$)xa0— insert element $$$x$$$ into the set (it is guaranteed that $$$x$$$ is not in the set); - $$$x$$$ ($$$1 le x le 2 cdot 10^6$$$)xa0— remove element $$$x$$$ from the set (it is guaranteed that $$$x$$$ is in the set); ? $$$k$$$ ($$$1 le k le 2 cdot 10^6$$$)xa0— output the value of the $$$k$$$-load of the set. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$, and the same holds for $$$m$$$. Output For each test case, output the answers to the operations of type "?". Example Input 3 5 1 2 5 905 2000000 15 - 2 ? 2 ? 1 - 1 ? 1 + 4 + 2 ? 2 + 6 - 4 + 7 ? 2 ? 3 ? 4 ? 2000000 5 3 4 5 6 8 9 ? 5 - 5 ? 5 + 1 ? 2 - 6 - 8 + 6 ? 5 5 6 7 8 9 10 10 ? 5 - 6 ? 4 - 10 + 5 - 8 + 3 + 2 - 3 + 10 Output 2 2 1 6 3 8 8 2000001 9 9 9 7 1 1
2,200
false
false
true
false
true
false
true
true
false
false
256
1732B
A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s_1 s_2 ldots s_n$$$. It is necessary to make this string non-decreasing in the least number of operations. In other words, each character should be not less than the previous. In one operation, you can do the following: Select an arbitrary index $$$1 leq i leq n$$$ in the string; For all $$$j geq i$$$, change the value in the $$$j$$$-th position to the opposite, that is, if $$$s_j = 1$$$, then make $$$s_j = 0$$$, and vice versa. What is the minimum number of operations needed to make the string non-decreasing? Input Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test cases a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the length of the string. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer — the minimum number of operations that are needed to make the string non-decreasing. Example Input 8 1 1 2 10 3 101 4 1100 5 11001 6 100010 10 0000110000 7 0101010 Note In the first test case, the string is already non-decreasing. In the second test case, you can select $$$i = 1$$$ and then $$$s = mathtt{01}$$$. In the third test case, you can select $$$i = 1$$$ and get $$$s = mathtt{010}$$$, and then select $$$i = 2$$$. As a result, we get $$$s = mathtt{001}$$$, that is, a non-decreasing string. In the sixth test case, you can select $$$i = 5$$$ at the first iteration and get $$$s = mathtt{100001}$$$. Then choose $$$i = 2$$$, then $$$s = mathtt{111110}$$$. Then we select $$$i = 1$$$, getting the non-decreasing string $$$s = mathtt{000001}$$$.
900
false
true
true
true
false
false
true
false
false
false
1,898
1746E2
The only difference between this problem and the hard version is the maximum number of questions. This is an interactive problem. There is a hidden integer $$$1 le x le n$$$ which you have to find. In order to find it you can ask at most $$$mathbf{53}$$$ questions. In each question you can choose a non-empty integer set $$$S$$$ and ask if $$$x$$$ belongs to $$$S$$$ or not, after each question, if $$$x$$$ belongs to $$$S$$$, you'll receive "YES", otherwise "NO". But the problem is that not all answers are necessarily true (some of them are joking), it's just guaranteed that for each two consecutive questions, at least one of them is answered correctly. Additionally to the questions, you can make at most $$$2$$$ guesses for the answer $$$x$$$. Each time you make a guess, if you guess $$$x$$$ correctly, you receive ":)" and your program should terminate, otherwise you'll receive ":(". As a part of the joking, we will not fix the value of $$$x$$$ in the beginning. Instead, it can change throughout the interaction as long as all the previous responses are valid as described above. Note that your answer guesses are always answered correctly. If you ask a question before and after a guess, at least one of these two questions is answered correctly, as normal. Input The only line of the input contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$), the maximum possible value of $$$x$$$. Interaction For each question, if you want to ask about a set $$$S$$$, first print the character '?', then print the size of $$$S$$$ and then print the elements of $$$S$$$ one by one. Each element should be an integer between $$$1$$$ and $$$n$$$, the elements must be distinct. After each question, read a string "YES" or "NO", as explained in the statement. You can make at most $$$53$$$ such questions. If you want to guess for $$$x$$$, first print the character '!' and then print your guess. After each guess, read ":)" or ":(". If you guess $$$x$$$ correctly, the answer is ":)" and your program should terminate immediately, otherwise you'll receive ":(". You can make at most $$$2$$$ such guesses. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacking is not allowed in this problem. Example Output ? 5 1 2 5 4 3! 6 ? 4 1 2 3 4 ! 5 Note If the answer of the first question were correct, then $$$x$$$ would have been equal to $$$6$$$, but as we can see in the first guess, $$$6$$$ is not the answer. So the answer of the first question is joking. As we know, the answer of at least one of our two questions is correct, since the answer of the first question was joking, the answer of the second question should be correct. So we will understand that $$$x$$$ is not equal to $$$1, 2, 3$$$ or $$$4$$$, and we also knew that $$$x$$$ is not equal to $$$6$$$ either. Hence $$$x$$$ should be equal to $$$5$$$.
3,200
false
false
false
true
false
false
false
false
false
false
1,805
1118D1
The only difference between easy and hard versions is the constraints. Polycarp has to write a coursework. The coursework consists of $$$m$$$ pages. Polycarp also has $$$n$$$ cups of coffee. The coffee in the $$$i$$$-th cup has $$$a_i$$$ caffeine in it. Polycarp can drink some cups of coffee (each one no more than once). He can drink cups in any order. Polycarp drinks each cup instantly and completely (i.e. he cannot split any cup into several days). Surely, courseworks are not usually being written in a single day (in a perfect world of Berland, at least). Some of them require multiple days of hard work. Let's consider some day of Polycarp's work. Consider Polycarp drinks $$$k$$$ cups of coffee during this day and caffeine dosages of cups Polycarp drink during this day are $$$a_{i_1}, a_{i_2}, dots, a_{i_k}$$$. Then the first cup he drinks gives him energy to write $$$a_{i_1}$$$ pages of coursework, the second cup gives him energy to write $$$max(0, a_{i_2} - 1)$$$ pages, the third cup gives him energy to write $$$max(0, a_{i_3} - 2)$$$ pages, ..., the $$$k$$$-th cup gives him energy to write $$$max(0, a_{i_k} - k + 1)$$$ pages. If Polycarp doesn't drink coffee during some day, he cannot write coursework at all that day. Polycarp has to finish his coursework as soon as possible (spend the minimum number of days to do it). Your task is to find out this number of days or say that it is impossible. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 100$$$, $$$1 le m le 10^4$$$) — the number of cups of coffee and the number of pages in the coursework. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 100$$$), where $$$a_i$$$ is the caffeine dosage of coffee in the $$$i$$$-th cup. Output If it is impossible to write the coursework, print -1. Otherwise print the minimum number of days Polycarp needs to do it. Note In the first example Polycarp can drink fourth cup during first day (and write $$$1$$$ page), first and second cups during second day (and write $$$2 + (3 - 1) = 4$$$ pages), fifth cup during the third day (and write $$$2$$$ pages) and third cup during the fourth day (and write $$$1$$$ page) so the answer is $$$4$$$. It is obvious that there is no way to write the coursework in three or less days in this test. In the second example Polycarp can drink third, fourth and second cups during first day (and write $$$4 + (2 - 1) + (3 - 2) = 6$$$ pages) and sixth cup during second day (and write $$$4$$$ pages) so the answer is $$$2$$$. It is obvious that Polycarp cannot write the whole coursework in one day in this test. In the third example Polycarp can drink all cups of coffee during first day and write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) + (5 - 4) = 15$$$ pages of coursework. In the fourth example Polycarp cannot drink all cups during first day and should drink one of them during the second day. So during first day he will write $$$5 + (5 - 1) + (5 - 2) + (5 - 3) = 14$$$ pages of coursework and during second day he will write $$$5$$$ pages of coursework. This is enough to complete it. In the fifth example Polycarp cannot write the whole coursework at all, even if he will drink one cup of coffee during each day, so the answer is -1.
1,700
false
true
false
false
false
false
true
false
false
false
5,099
886E
One day Petya was solving a very interesting problem. But although he used many optimization techniques, his solution still got Time limit exceeded verdict. Petya conducted a thorough analysis of his program and found out that his function for finding maximum element in an array of _n_ positive integers was too slow. Desperate, Petya decided to use a somewhat unexpected optimization using parameter _k_, so now his function contains the following code: int fast_max(int n, int a[]) { int ans = 0; int offset = 0; for (int i = 0; i < n; ++i) if (ans < a[i]) { ans = a[i]; offset = 0; } else { offset = offset + 1; if (offset == k) return ans; } return ans; } That way the function iteratively checks array elements, storing the intermediate maximum, and if after _k_ consecutive iterations that maximum has not changed, it is returned as the answer. Now Petya is interested in fault rate of his function. He asked you to find the number of permutations of integers from 1 to _n_ such that the return value of his function on those permutations is not equal to _n_. Since this number could be very big, output the answer modulo 109u2009+u20097. Input The only line contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009106), separated by a spacexa0— the length of the permutations and the parameter _k_. Note Permutations from second example: [4,u20091,u20092,u20093,u20095], [4,u20091,u20093,u20092,u20095], [4,u20092,u20091,u20093,u20095], [4,u20092,u20093,u20091,u20095], [4,u20093,u20091,u20092,u20095], [4,u20093,u20092,u20091,u20095].
2,400
true
false
false
true
false
false
false
false
false
false
6,204
977B
Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams. You are given a string $$$s$$$ consisting of $$$n$$$ capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string $$$s$$$ = "BBAABBBA" the answer is two-gram "BB", which contained in $$$s$$$ three times. In other words, find any most frequent two-gram. Note that occurrences of the two-gram can overlap with each other. Input The first line of the input contains integer number $$$n$$$ ($$$2 le n le 100$$$) — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ capital Latin letters. Output Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string $$$s$$$ as a substring (i.e. two consecutive characters of the string) maximal number of times. Note In the first example "BA" is also valid answer. In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.
900
false
false
true
false
false
false
false
false
false
false
5,823
1780B
While at Kira's house, Josuke saw a piece of paper on the table with a task written on it. The task sounded as follows. There is an array $$$a$$$ of length $$$n$$$. On this array, do the following: select an integer $$$k > 1$$$; split the array into $$$k$$$ subsegments $$$^dagger$$$; calculate the sum in each of $$$k$$$ subsegments and write these sums to another array $$$b$$$ (where the sum of the subsegment $$$(l, r)$$$ is $$${sum_{j = l}^{r}a_j}$$$); the final score of such a split will be $$$gcd(b_1, b_2, ldots, b_k)^ddagger$$$. The task is to find such a partition that the score is maximum possible. Josuke is interested in this task but is not strong in computer science. Help him to find the maximum possible score. $$$^dagger$$$ A division of an array into $$$k$$$ subsegments is $$$k$$$ pairs of numbers $$$(l_1, r_1), (l_2, r_2), ldots, (l_k, r_k)$$$ such that $$$l_i le r_i$$$ and for every $$$1 le j le k - 1$$$ $$$l_{j + 1} = r_j + 1$$$, also $$$l_1 = 1$$$ and $$$r_k = n$$$. These pairs represent the subsegments. $$$^ddagger$$$ $$$gcd(b_1, b_2, ldots, b_k)$$$ stands for the — the number of test cases. For each test case, the first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, a_3, ldots, a_n$$$ ($$$1 le a_i le 10^9 $$$) — the array $$$a$$$ itself. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print a single integer — the maximum score for the optimal partition. Example Input 6 4 2 2 1 3 2 1 2 3 1 4 5 6 1 2 1 1 1 3 10 12 30 37 88 12 78 89 17 2 12 6 7 7 7 7 7 7 Note In the first test case, you can choose $$$k = 2$$$ and split the array into subsegments $$$(1, 2)$$$ and $$$(3, 4)$$$. Then the score of such a partition will be equal to $$$gcd(a_1 + a_2, a_3 + a_4) = gcd(2 + 2, 1 + 3) = gcd(4, 4) = 4$$$. In the fourth test case, you can choose $$$k = 3$$$ and split the array into subsegments $$$(1, 2), (3, 5), (6, 6)$$$. The split score is $$$gcd(1 + 2, 1 + 1 + 1, 3) = 3$$$.
1,100
true
true
false
false
false
false
true
false
false
false
1,593
1543E
Finally, you have defeated Razor and now, you are the Most Wanted street racer. Sergeant Cross has sent the full police force after you in a deadly pursuit. Fortunately, you have found a hiding spot but you fear that Cross and his force will eventually find you. To increase your chances of survival, you want to tune and repaint your BMW M3 GTR. The car can be imagined as a permuted $$$n$$$-dimensional hypercube. A simple $$$n$$$-dimensional hypercube is an undirected unweighted graph built recursively as follows: Take two simple $$$(n-1)$$$-dimensional hypercubes one having vertices numbered from $$$0$$$ to $$$2^{n-1}-1$$$ and the other having vertices numbered from $$$2^{n-1}$$$ to $$$2^{n}-1$$$. A simple $$$0$$$-dimensional Hypercube is just a single vertex. Add an edge between the vertices $$$i$$$ and $$$i+2^{n-1}$$$ for each $$$0leq i < 2^{n-1}$$$. A permuted $$$n$$$-dimensional hypercube is formed by permuting the vertex numbers of a simple $$$n$$$-dimensional hypercube in any arbitrary manner. Examples of a simple and permuted $$$3$$$-dimensional hypercubes are given below: Note that a permuted $$$n$$$-dimensional hypercube has the following properties: There are exactly $$$2^n$$$ vertices. There are exactly $$$ncdot 2^{n-1}$$$ edges. Each vertex is connected to exactly $$$n$$$ other vertices. There are no self-loops or duplicate edges. Let's denote the permutation used to generate the permuted $$$n$$$-dimensional hypercube, representing your car, from a simple $$$n$$$-dimensional hypercube by $$$P$$$. Before messing up the functionalities of the car, you want to find this permutation so that you can restore the car if anything goes wrong. But the job isn't done yet. You have $$$n$$$ different colours numbered from $$$0$$$ to $$$n-1$$$. You want to colour the vertices of this permuted $$$n$$$-dimensional hypercube in such a way that for each and every vertex $$$u$$$ satisfying $$$0leq u < 2^n$$$ and for each and every colour $$$c$$$ satisfying $$$0leq c < n$$$, there is at least one vertex $$$v$$$ adjacent to $$$u$$$ having a colour $$$c$$$. In other words, from each and every vertex, it must be possible to reach a vertex of any colour by just moving to an adjacent vertex. Given the permuted $$$n$$$-dimensional hypercube, find any valid permutation $$$P$$$ and colouring. Input The first line of input contains a single integer $$$t$$$ ($$$1leq tleq 4096$$$) — the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$1leq nleq 16$$$). Each of the next $$$ncdot 2^{n-1}$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$0leq u, v < 2^n$$$) denoting that there is an edge between the vertices numbered $$$u$$$ and $$$v$$$. It is guaranteed that the graph described in the input is a permuted $$$n$$$-dimensional hypercube. Additionally, it is guaranteed that the sum of $$$2^n$$$ over all test cases does not exceed $$$2^{16}=65,536$$$. Output For each test case, print two lines. In the first line, output any permutation $$$P$$$ of length $$$2^n$$$ that can be used to transform a simple $$$n$$$-dimensional hypercube to the permuted $$$n$$$-dimensional hypercube given in the input. Two permuted hypercubes are considered the same if they have the same set of edges. If there are multiple answers, output any of them. In the second line, print the colouring. If there is no way to colour the vertices satisfying the conditions, output $$$-1$$$. Otherwise, output a single line containing $$$2^n$$$ space separated integers. The $$$i$$$-th integer must be the colour of the vertex numbered $$$(i-1)$$$ in the permuted $$$n$$$-dimensional hypercube. If there are multiple answers, output any of them. Example Input 3 1 0 1 2 0 1 1 2 2 3 3 0 3 0 1 0 5 0 7 1 2 1 4 2 5 2 6 3 5 3 6 3 7 4 6 4 7 Output 0 1 0 0 0 1 3 2 0 0 1 1 5 3 0 7 2 6 1 4 -1 Note The colouring and the permuted hypercube for the first test case is shown below: The colouring and the permuted hypercube for the second test case is shown below: The permuted hypercube for the third test case is given in the problem statement. However, it can be shown that there exists no way to colour that cube satifying all the conditions. Note that some other permutations like $$$[0, 5, 7, 3, 1, 2, 4, 6]$$$ and $$$[0, 1, 5, 2, 7, 4, 3, 6]$$$ will also give the same permuted hypercube.
2,700
true
true
false
false
false
true
false
false
false
true
2,923
567B
Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room. Today was the pilot launch of an automated reading room visitors' accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form "reader entered room", "reader left room". Every reader is assigned a registration number during the registration procedure at the library — it's a unique integer from 1 to 106. Thus, the system logs events of two forms: "+ _r__i_" — the reader with registration number _r__i_ entered the room; "- _r__i_" — the reader with registration number _r__i_ left the room. The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors. Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence. Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you. Input The first line contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of records in the system log. Next follow _n_ events from the system journal in the order in which the were made. Each event was written on a single line and looks as "+ _r__i_" or "- _r__i_", where _r__i_ is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers). It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors. Output Print a single integer — the minimum possible capacity of the reading room. Examples Input 6 + 12001 - 12001 - 1 - 1200 + 1 + 7 Note In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.
1,300
false
false
true
false
false
false
false
false
false
false
7,583
979E
Kuro has recently won the "Most intelligent cat ever" contest. The three friends then decided to go to Katie's home to celebrate Kuro's winning. After a big meal, they took a small break then started playing games. Kuro challenged Katie to create a game with only a white paper, a pencil, a pair of scissors and a lot of arrows (you can assume that the number of arrows is infinite). Immediately, Katie came up with the game called Topological Parity. The paper is divided into $$$n$$$ pieces enumerated from $$$1$$$ to $$$n$$$. Shiro has painted some pieces with some color. Specifically, the $$$i$$$-th piece has color $$$c_{i}$$$ where $$$c_{i} = 0$$$ defines black color, $$$c_{i} = 1$$$ defines white color and $$$c_{i} = -1$$$ means that the piece hasn't been colored yet. The rules of the game is simple. Players must put some arrows between some pairs of different pieces in such a way that for each arrow, the number in the piece it starts from is less than the number of the piece it ends at. Also, two different pieces can only be connected by at most one arrow. After that the players must choose the color ($$$0$$$ or $$$1$$$) for each of the unpainted pieces. The score of a valid way of putting the arrows and coloring pieces is defined as the number of paths of pieces of alternating colors. For example, $$$[1 o 0 o 1 o 0]$$$, $$$[0 o 1 o 0 o 1]$$$, $$$[1]$$$, $$$[0]$$$ are valid paths and will be counted. You can only travel from piece $$$x$$$ to piece $$$y$$$ if and only if there is an arrow from $$$x$$$ to $$$y$$$. But Kuro is not fun yet. He loves parity. Let's call his favorite parity $$$p$$$ where $$$p = 0$$$ stands for "even" and $$$p = 1$$$ stands for "odd". He wants to put the arrows and choose colors in such a way that the score has the parity of $$$p$$$. It seems like there will be so many ways which satisfy Kuro. He wants to count the number of them but this could be a very large number. Let's help him with his problem, but print it modulo $$$10^{9} + 7$$$. Input The first line contains two integers $$$n$$$ and $$$p$$$ ($$$1 leq n leq 50$$$, $$$0 leq p leq 1$$$) — the number of pieces and Kuro's wanted parity. The second line contains $$$n$$$ integers $$$c_{1}, c_{2}, ..., c_{n}$$$ ($$$-1 leq c_{i} leq 1$$$) — the colors of the pieces. Note In the first example, there are $$$6$$$ ways to color the pieces and add the arrows, as are shown in the figure below. The scores are $$$3, 3, 5$$$ for the first row and $$$5, 3, 3$$$ for the second row, both from left to right.
2,400
false
false
false
true
false
false
false
false
false
false
5,807
1665B
You are given an array $$$a$$$ of $$$n$$$ integers. Initially there is only one copy of the given array. You can do operations of two types: 1. Choose any array and clone it. After that there is one more copy of the chosen array. 2. Swap two elements from any two copies (maybe in the same copy) on any positions. You need to find the minimal number of operations needed to obtain a copy where all elements are equal. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the array $$$a$$$. 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$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case all elements in the array are already equal, that's why the answer is $$$0$$$. In the second test case it is possible to create a copy of the given array. After that there will be two identical arrays: $$$[ 0 1 3 3 7 0 ]$$$ and $$$[ 0 1 3 3 7 0 ]$$$ After that we can swap elements in a way so all zeroes are in one array: $$$[ 0 underline{0} underline{0} 3 7 0 ]$$$ and $$$[ underline{1} 1 3 3 7 underline{3} ]$$$ Now let's create a copy of the first array: $$$[ 0 0 0 3 7 0 ]$$$, $$$[ 0 0 0 3 7 0 ]$$$ and $$$[ 1 1 3 3 7 3 ]$$$ Let's swap elements in the first two copies: $$$[ 0 0 0 underline{0} underline{0} 0 ]$$$, $$$[ underline{3} underline{7} 0 3 7 0 ]$$$ and $$$[ 1 1 3 3 7 3 ]$$$. Finally, we made a copy where all elements are equal and made $$$6$$$ operations. It can be proven that no fewer operations are enough.
900
false
true
false
false
false
true
false
false
true
false
2,289
18D
Last year Bob earned by selling memory sticks. During each of _n_ days of his work one of the two following events took place: A customer came to Bob and asked to sell him a 2_x_ MB memory stick. If Bob had such a stick, he sold it and got 2_x_ berllars. Bob won some programming competition and got a 2_x_ MB memory stick as a prize. Bob could choose whether to present this memory stick to one of his friends, or keep it. Bob never kept more than one memory stick, as he feared to mix up their capacities, and deceive a customer unintentionally. It is also known that for each memory stick capacity there was at most one customer, who wanted to buy that memory stick. Now, knowing all the customers' demands and all the prizes won at programming competitions during the last _n_ days, Bob wants to know, how much money he could have earned, if he had acted optimally. Input The first input line contains number _n_ (1u2009≤u2009_n_u2009≤u20095000) — amount of Bob's working days. The following _n_ lines contain the description of the days. Line sell x stands for a day when a customer came to Bob to buy a 2_x_ MB memory stick (0u2009≤u2009_x_u2009≤u20092000). It's guaranteed that for each _x_ there is not more than one line sell x. Line win x stands for a day when Bob won a 2_x_ MB memory stick (0u2009≤u2009_x_u2009≤u20092000). Output Output the maximum possible earnings for Bob in berllars, that he would have had if he had known all the events beforehand. Don't forget, please, that Bob can't keep more than one memory stick at a time. Examples Input 7 win 10 win 5 win 3 sell 5 sell 3 win 10 sell 10
2,000
false
true
false
true
false
false
true
false
false
false
9,902
1221G
You are given an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. You have to write a number on each vertex of this graph, each number should be either $$$0$$$ or $$$1$$$. After that, you write a number on each edge equal to the sum of numbers on vertices incident to that edge. You have to choose the numbers you will write on the vertices so that there is at least one edge with $$$0$$$ written on it, at least one edge with $$$1$$$ and at least one edge with $$$2$$$. How many ways are there to do it? Two ways to choose numbers are different if there exists at least one vertex which has different numbers written on it in these two ways. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 40$$$, $$$0 le m le frac{n(n - 1)}{2}$$$) — the number of vertices and the number of edges, respectively. Then $$$m$$$ lines follow, each line contains two numbers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$) — the endpoints of the $$$i$$$-th edge. It is guaranteed that each pair of vertices is connected by at most one edge. Output Print one integer — the number of ways to write numbers on all vertices so that there exists at least one edge with $$$0$$$ written on it, at least one edge with $$$1$$$ and at least one edge with $$$2$$$. Examples Input 6 5 1 2 2 3 3 4 4 5 5 1 Input 35 29 1 2 2 3 3 1 4 1 4 2 3 4 7 8 8 9 9 10 10 7 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30
2,900
false
false
false
true
false
false
true
false
false
false
4,560
288E
Problem - 288E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp implementation math *2800 No tag edit access → Contest materials , both of them are lucky numbers. Moreover, their lengths (that is, the number of digits in the decimal representation without the leading zeroes) are equal to each other. Let's assume that _n_ is the number of distinct lucky numbers, each of them cannot be greater than _r_ or less than _l_, and _a__i_ is the _i_-th (in increasing order) number of them. Find _a_1·_a_2u2009+u2009_a_2·_a_3u2009+u2009...u2009+u2009_a__n_u2009-u20091·_a__n_. As the answer can be rather large, print the remainder after dividing it by 1000000007 (109u2009+u20097). Input The first line contains a positive integer _l_, and the second line contains a positive integer _r_ (1u2009≤u2009_l_u2009<u2009_r_u2009≤u200910100000). The numbers are given without any leading zeroes. It is guaranteed that the lengths of the given numbers are equal to each other and that both of them are lucky numbers. Output In the single line print a single integer — the answer to the problem modulo 1000000007 (109u2009+u20097). Examples Input 4 7 Output 28 Input 474 777 Output 2316330
2,800
true
false
true
true
false
false
false
false
false
false
8,680
722B
You are given a text consisting of _n_ lines. Each line contains some space-separated words, consisting of lowercase English letters. We define a syllable as a string that contains exactly one vowel and any arbitrary number (possibly none) of consonants. In English alphabet following letters are considered to be vowels: 'a', 'e', 'i', 'o', 'u' and 'y'. Each word of the text that contains at least one vowel can be divided into syllables. Each character should be a part of exactly one syllable. For example, the word "mamma" can be divided into syllables as "ma" and "mma", "mam" and "ma", and "mamm" and "a". Words that consist of only consonants should be ignored. The verse patterns for the given text is a sequence of _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_. Text matches the given verse pattern if for each _i_ from 1 to _n_ one can divide words of the _i_-th line in syllables in such a way that the total number of syllables is equal to _p__i_. You are given the text and the verse pattern. Check, if the given text matches the given verse pattern. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of lines in the text. The second line contains integers _p_1,u2009...,u2009_p__n_ (0u2009≤u2009_p__i_u2009≤u2009100)xa0— the verse pattern. Next _n_ lines contain the text itself. Text consists of lowercase English letters and spaces. It's guaranteed that all lines are non-empty, each line starts and ends with a letter and words are separated by exactly one space. The length of each line doesn't exceed 100 characters. Output If the given text matches the given verse pattern, then print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes). Note In the first sample, one can split words into syllables in the following way: in-tel co-de ch al-len-ge Since the word "ch" in the third line doesn't contain vowels, we can ignore it. As the result we get 2 syllabels in first two lines and 3 syllables in the third one.
1,200
false
false
true
false
false
false
false
false
false
false
6,925
1091E
Bob is an active user of the social network Faithbug. On this network, people are able to engage in a mutual friendship. That is, if $$$a$$$ is a friend of $$$b$$$, then $$$b$$$ is also a friend of $$$a$$$. Each user thus has a non-negative amount of friends. This morning, somebody anonymously sent Bob the following link: , the number of people on the network excluding Bob. The second line contains $$$n$$$ numbers $$$a_1,a_2, dots, a_n$$$xa0($$$0 leq a_i leq n$$$), with $$$a_i$$$ being the number of people that person $$$i$$$ is a friend of. Output Print all possible values of $$$a_{n+1}$$$xa0— the amount of people that Bob can be friend of, in increasing order. If no solution exists, output $$$-1$$$. Examples Input 35 21 26 18 4 28 2 15 13 16 25 6 32 11 5 31 17 9 3 24 33 14 27 29 1 20 4 12 7 10 30 34 8 19 23 22 Note In the first test case, the only solution is that everyone is friends with everyone. That is why Bob should have $$$3$$$ friends. In the second test case, there are three possible solutions (apart from symmetries): $$$a$$$ is friend of $$$b$$$, $$$c$$$ is friend of $$$d$$$, and Bob has no friends, or $$$a$$$ is a friend of $$$b$$$ and both $$$c$$$ and $$$d$$$ are friends with Bob, or Bob is friends of everyone. The third case is impossible to solve, as the second person needs to be a friend with everybody, but the first one is a complete stranger.
2,400
true
true
true
false
true
false
false
true
true
true
5,255
1920F2
The only difference between the two versions of this problem is the constraint on $$$q$$$. You can make hacks only if both versions of the problem are solved. Thomas is sailing around an island surrounded by the ocean. The ocean and island can be represented by a grid with $$$n$$$ rows and $$$m$$$ columns. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and the columns are numbered from $$$1$$$ to $$$m$$$ from left to right. The position of a cell at row $$$r$$$ and column $$$c$$$ can be represented as $$$(r, c)$$$. Below is an example of a valid grid. Example of a valid grid There are three types of cells: island, ocean and underwater volcano. Cells representing the island are marked with a '#', cells representing the ocean are marked with a '.', and cells representing an underwater volcano are marked with a 'v'. It is guaranteed that there is at least one island cell and at least one underwater volcano cell. It is also guaranteed that the set of all island cells forms a single connected component$$$^{dagger}$$$ and the set of all ocean cells and underwater volcano cells forms a single connected component. Additionally, it is guaranteed that there are no island cells at the edge of the grid (that is, at row $$$1$$$, at row $$$n$$$, at column $$$1$$$, and at column $$$m$$$). Define a round trip starting from cell $$$(x, y)$$$ as a path Thomas takes which satisfies the following conditions: The path starts and ends at $$$(x, y)$$$. If Thomas is at cell $$$(i, j)$$$, he can go to cells $$$(i+1, j)$$$, $$$(i-1, j)$$$, $$$(i, j-1)$$$, and $$$(i, j+1)$$$ as long as the destination cell is an ocean cell or an underwater volcano cell and is still inside the grid. Note that it is allowed for Thomas to visit the same cell multiple times in the same round trip. The path must go around the island and fully encircle it. Some path $$$p$$$ fully encircles the island if it is impossible to go from an island cell to a cell on the grid border by only traveling to adjacent on a side or diagonal cells without visiting a cell on path $$$p$$$. In the image below, the path starting from $$$(2, 2)$$$, going to $$$(1, 3)$$$, and going back to $$$(2, 2)$$$ the other way does not fully encircle the island and is not considered a round trip. Example of a path that does not fully encircle the island The safety of a round trip is the minimum Manhattan distance$$$^{ddagger}$$$ from a cell on the round trip to an underwater volcano (note that the presence of island cells does not impact this distance). You have $$$q$$$ queries. A query can be represented as $$$(x, y)$$$ and for every query, you want to find the maximum safety of a round trip starting from $$$(x, y)$$$. It is guaranteed that $$$(x, y)$$$ is an ocean cell or an underwater volcano cell. $$$^{dagger}$$$A set of cells forms a single connected component if from any cell of this set it is possible to reach any other cell of this set by moving only through the cells of this set, each time going to a cell with a common side. $$$^{ddagger}$$$Manhattan distance between cells $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ is equal to $$$r_1 - r_2 + c_1 - c_2$$$. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$3 leq n, m leq 10^5$$$, $$$9 leq n cdot m leq 3 cdot 10^5$$$, $$$1 leq q leq 3 cdot 10^5$$$) — the number of rows and columns of the grid and the number of queries. Each of the following $$$n$$$ lines contains $$$m$$$ characters describing the cells of the grid. The character '#' denotes an island cell, '.' denotes an ocean cell, and 'v' denotes an underwater volcano cell. It is guaranteed that there is at least one island cell and at least one underwater volcano cell. It is guaranteed that the set of all island cells forms a single connected component and the set of all ocean cells and underwater volcano cells forms a single connected component. Also, it is guaranteed that there are no island cells at the edge of the grid (that is, at the row $$$1$$$, at the row $$$n$$$, at the column $$$1$$$, and at the column $$$m$$$). The following $$$q$$$ lines describe the queries. Each of these lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 leq x leq n$$$, $$$1 leq y leq m$$$) denoting a round trip starting from $$$(x, y)$$$. It is guaranteed that $$$(x, y)$$$ is an ocean cell or an underwater volcano cell. Output For each query, output a single integer — the maximum safety of a round trip starting from the specified position. Examples Input 9 9 3 ......... ......... ....###.. ...v#.... ..###.... ...##...v ...##.... ......... v........ 1 1 9 1 5 7 Input 3 3 5 ..v .#. ... 1 2 1 3 2 3 2 1 3 2 Input 14 13 5 ............. ............. ............. ...vvvvvvv... ...v.....v... ...v.###.v... ...v.#.#.v... ...v..v..v... ...v..v..v... ....v...v.... .....vvv..... ............. ............. ............. 1 1 7 7 5 6 4 10 13 6 Input 10 11 4 ........... ..#######.. ..#..#..#.. ..#.....#.. ..#..v..#.. ..#.###.#.. ..#.#.#.#.. ..#...#.#.. ..#####.#.. ........... 7 6 3 7 6 8 1 1 Note For the first example, the image below shows an optimal round trip starting from $$$(1, 1)$$$. The round trip has a safety of $$$3$$$ as the minimum Manhattan distance from a cell on the round trip to an underwater volcano is $$$3$$$. Example of an optimal round trip For the fourth example, remember that it is allowed for Thomas to visit the same cell multiple times in the same round trip. For example, doing so is necessary for the round trip starting from $$$(7, 6)$$$.
3,000
false
false
false
false
true
false
false
true
false
true
766
1717E
Madoka wants to enter to "Novosibirsk State University", but in the entrance exam she came across a very difficult task: Given an integer $$$n$$$, it is required to calculate $$$sum{operatorname{lcm}(c, gcd(a, b))}$$$, for all triples of positive integers $$$(a, b, c)$$$, where $$$a + b + c = n$$$. In this problem $$$gcd(x, y)$$$ denotes the $$$ denotes the )}$$$. Since the answer can be very large, then output it modulo $$$10^9 + 7$$$. Note In the first example, there is only one suitable triple $$$(1, 1, 1)$$$. So the answer is $$$operatorname{lcm}(1, gcd(1, 1)) = operatorname{lcm}(1, 1) = 1$$$. In the second example, $$$operatorname{lcm}(1, gcd(3, 1)) + operatorname{lcm}(1, gcd(2, 2)) + operatorname{lcm}(1, gcd(1, 3)) + operatorname{lcm}(2, gcd(2, 1)) + operatorname{lcm}(2, gcd(1, 2)) + operatorname{lcm}(3, gcd(1, 1)) = operatorname{lcm}(1, 1) + operatorname{lcm}(1, 2) + operatorname{lcm}(1, 1) + operatorname{lcm}(2, 1) + operatorname{lcm}(2, 1) + operatorname{lcm}(3, 1) = 1 + 2 + 1 + 2 + 2 + 3 = 11$$$
2,200
true
false
false
false
false
false
false
false
false
false
1,977
1326F2
This is the hard version of the problem. The difference is constraints on the number of wise men and the time limit. You can make hacks only if all versions of this task are solved. $$$n$$$ wise men live in a beautiful city. Some of them know each other. For each of the $$$n!$$$ possible permutations $$$p_1, p_2, ldots, p_n$$$ of the wise men, let's generate a binary string of length $$$n-1$$$: for each $$$1 leq i < n$$$ set $$$s_i=1$$$ if $$$p_i$$$ and $$$p_{i+1}$$$ know each other, and $$$s_i=0$$$ otherwise. For all possible $$$2^{n-1}$$$ binary strings, find the number of permutations that produce this binary string. Input The first line of input contains one integer $$$n$$$ ($$$2 leq n leq 18)$$$ xa0— the number of wise men in the city. The next $$$n$$$ lines contain a binary string of length $$$n$$$ each, such that the $$$j$$$-th character of the $$$i$$$-th string is equal to '1' if wise man $$$i$$$ knows wise man $$$j$$$, and equals '0' otherwise. It is guaranteed that if the $$$i$$$-th man knows the $$$j$$$-th man, then the $$$j$$$-th man knows $$$i$$$-th man and no man knows himself. Output Print $$$2^{n-1}$$$ space-separated integers. For each $$$0 leq x < 2^{n-1}$$$: Let's consider a string $$$s$$$ of length $$$n-1$$$, such that $$$s_i = lfloor frac{x}{2^{i-1}} floor bmod 2$$$ for all $$$1 leq i leq n - 1$$$. The $$$(x+1)$$$-th number should be equal to the required answer for $$$s$$$. Examples Input 4 0101 1000 0001 1010 Note In the first test, each wise man knows each other, so every permutation will produce the string $$$11$$$. In the second test: If $$$p = {1, 2, 3, 4}$$$, the produced string is $$$101$$$, because wise men $$$1$$$ and $$$2$$$ know each other, $$$2$$$ and $$$3$$$ don't know each other, and $$$3$$$ and $$$4$$$ know each other; If $$$p = {4, 1, 2, 3}$$$, the produced string is $$$110$$$, because wise men $$$1$$$ and $$$4$$$ know each other, $$$1$$$ and $$$2$$$ know each other and $$$2$$$, and $$$3$$$ don't know each other; If $$$p = {1, 3, 2, 4}$$$, the produced string is $$$000$$$, because wise men $$$1$$$ and $$$3$$$ don't know each other, $$$3$$$ and $$$2$$$ don't know each other, and $$$2$$$ and $$$4$$$ don't know each other.
3,200
true
false
false
true
false
false
false
false
false
false
4,080
1154A
Problem - 1154A - 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 *800 No tag edit access → Contest materials ") Editorial") and sum of all three numbers (one number). So, there are four numbers on a board in random order: $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$. You have to guess three numbers $$$a$$$, $$$b$$$ and $$$c$$$ using given numbers. Print three guessed integers in any order. Pay attention that some given numbers $$$a$$$, $$$b$$$ and $$$c$$$ can be equal (it is also possible that $$$a=b=c$$$). Input The only line of the input contains four positive integers $$$x_1, x_2, x_3, x_4$$$ ($$$2 le x_i le 10^9$$$) — numbers written on a board in random order. It is guaranteed that the answer exists for the given number $$$x_1, x_2, x_3, x_4$$$. Output Print such positive integers $$$a$$$, $$$b$$$ and $$$c$$$ that four numbers written on a board are values $$$a+b$$$, $$$a+c$$$, $$$b+c$$$ and $$$a+b+c$$$ written in some order. Print $$$a$$$, $$$b$$$ and $$$c$$$ in any order. If there are several answers, you can print any. It is guaranteed that the answer exists. Examples Input 3 6 5 4 Output 2 1 3 Input 40 40 40 60 Output 20 20 20 Input 201 101 101 200 Output 1 100 100
800
true
false
false
false
false
false
false
false
false
false
4,943
23B
Problem - 23B - Codeforces =============== xa0 . Each of the following _t_ lines contains one integer number _n_ (1u2009≤u2009_n_u2009≤u2009105). Output For each test output in a separate line one number — the maximum amount of people that could stay in the end. Examples Input 1 3 Output 1
1,600
true
false
false
false
false
true
false
false
false
true
9,882
1290B
Let's call two strings $$$s$$$ and $$$t$$$ anagrams of each other if it is possible to rearrange symbols in the string $$$s$$$ to get a string, equal to $$$t$$$. Let's consider two strings $$$s$$$ and $$$t$$$ which are anagrams of each other. We say that $$$t$$$ is a reducible anagram of $$$s$$$ if there exists an integer $$$k ge 2$$$ and $$$2k$$$ non-empty strings $$$s_1, t_1, s_2, t_2, dots, s_k, t_k$$$ that satisfy the following conditions: 1. If we write the strings $$$s_1, s_2, dots, s_k$$$ in order, the resulting string will be equal to $$$s$$$; 2. If we write the strings $$$t_1, t_2, dots, t_k$$$ in order, the resulting string will be equal to $$$t$$$; 3. For all integers $$$i$$$ between $$$1$$$ and $$$k$$$ inclusive, $$$s_i$$$ and $$$t_i$$$ are anagrams of each other. If such strings don't exist, then $$$t$$$ is said to be an irreducible anagram of $$$s$$$. Note that these notions are only defined when $$$s$$$ and $$$t$$$ are anagrams of each other. For example, consider the string $$$s = $$$ "gamegame". Then the string $$$t = $$$ "megamage" is a reducible anagram of $$$s$$$, we may choose for example $$$s_1 = $$$ "game", $$$s_2 = $$$ "gam", $$$s_3 = $$$ "e" and $$$t_1 = $$$ "mega", $$$t_2 = $$$ "mag", $$$t_3 = $$$ "e": On the other hand, we can prove that $$$t = $$$ "memegaga" is an irreducible anagram of $$$s$$$. You will be given a string $$$s$$$ and $$$q$$$ queries, represented by two integers $$$1 le l le r le s$$$ (where $$$s$$$ is equal to the length of the string $$$s$$$). For each query, you should find if the substring of $$$s$$$ formed by characters from the $$$l$$$-th to the $$$r$$$-th has at least one irreducible anagram. Input The first line contains a string $$$s$$$, consisting of lowercase English characters ($$$1 le s le 2 cdot 10^5$$$). The second line contains a single integer $$$q$$$ ($$$1 le q le 10^5$$$) xa0— the number of queries. Each of the following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le s$$$), representing a query for the substring of $$$s$$$ formed by characters from the $$$l$$$-th to the $$$r$$$-th. Output For each query, print a single line containing "Yes" (without quotes) if the corresponding substring has at least one irreducible anagram, and a single line containing "No" (without quotes) otherwise. Examples Input aabbbbbbc 6 1 2 2 4 2 2 1 9 5 7 3 5 Output No Yes Yes Yes No No Note In the first sample, in the first and third queries, the substring is "a", which has itself as an irreducible anagram since two or more non-empty strings cannot be put together to obtain "a". On the other hand, in the second query, the substring is "aaa", which has no irreducible anagrams: its only anagram is itself, and we may choose $$$s_1 = $$$ "a", $$$s_2 = $$$ "aa", $$$t_1 = $$$ "a", $$$t_2 = $$$ "aa" to show that it is a reducible anagram. In the second query of the second sample, the substring is "abb", which has, for example, "bba" as an irreducible anagram.
1,800
false
false
false
false
true
true
false
true
false
false
4,245
132A
INTERCAL is the oldest of esoteric programming languages. One of its many weird features is the method of character-based output, known as Turing Tape method. It converts an array of unsigned 8-bit integers into a sequence of characters to print, using the following method. The integers of the array are processed one by one, starting from the first. Processing _i_-th element of the array is done in three steps: 1. The 8-bit binary notation of the ASCII-code of the previous printed character is reversed. When the first element of the array is processed, the result of this step is considered to be 0. 2. The _i_-th element of the array is subtracted from the result of the previous step modulo 256. 3. The binary notation of the result of the previous step is reversed again to produce ASCII-code of the _i_-th character to be printed. You are given the text printed using this method. Restore the array used to produce this text. Input The input will consist of a single line _text_ which contains the message printed using the described method. String _text_ will contain between 1 and 100 characters, inclusive. ASCII-code of each character of _text_ will be between 32 (space) and 126 (tilde), inclusive. Output Output the initial array, which was used to produce _text_, one integer per line. Examples Output 238 108 112 0 64 194 48 26 244 168 24 16 162 Note Let's have a closer look at the beginning of the example. The first character is "H" with ASCII-code 72u2009=u2009010010002. Its reverse is 000100102u2009=u200918, and this number should become the result of the second step of processing. The result of the first step is considered to be 0, so the first element of the array has to be (0u2009-u200918) mod 256u2009=u2009238, where _a_ mod _b_ is the remainder of division of _a_ by _b_.
1,300
false
false
true
false
false
false
false
false
false
false
9,347
1352C
Problem - 1352C - 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 math *1200 No tag edit access → Contest materials — the number of test cases in the input. Next, $$$t$$$ test cases are given, one per line. Each test case is two positive integers $$$n$$$ ($$$2 le n le 10^9$$$) and $$$k$$$ ($$$1 le k le 10^9$$$). Output For each test case print the $$$k$$$-th positive integer that is not divisible by $$$n$$$. Example Input 6 3 7 4 12 2 1000000000 7 97 1000000000 1000000000 2 1 Output 10 15 1999999999 113 1000000001 1
1,200
true
false
false
false
false
false
false
true
false
false
3,950
853E
After long-term research and lots of experiments leading Megapolian automobile manufacturer «AutoVoz» released a brand new car model named «Lada Malina». One of the most impressive features of «Lada Malina» is its highly efficient environment-friendly engines. Consider car as a point in _Oxy_ plane. Car is equipped with _k_ engines numbered from 1 to _k_. Each engine is defined by its velocity vector whose coordinates are (_vx__i_,u2009_vy__i_) measured in distance units per day. An engine may be turned on at any level _w__i_, that is a real number between u2009-u20091 and u2009+u20091 (inclusive) that result in a term of (_w__i_·_vx__i_,u2009_w__i_·_vy__i_) in the final car velocity. Namely, the final car velocity is equal to (_w_1·_vx_1u2009+u2009_w_2·_vx_2u2009+u2009...u2009+u2009_w__k_·_vx__k_,u2009xa0xa0_w_1·_vy_1u2009+u2009_w_2·_vy_2u2009+u2009...u2009+u2009_w__k_·_vy__k_) Formally, if car moves with constant values of _w__i_ during the whole day then its _x_-coordinate will change by the first component of an expression above, and its _y_-coordinate will change by the second component of an expression above. For example, if all _w__i_ are equal to zero, the car won't move, and if all _w__i_ are equal to zero except _w_1u2009=u20091, then car will move with the velocity of the first engine. There are _n_ factories in Megapolia, _i_-th of them is located in (_fx__i_,u2009_fy__i_). On the _i_-th factory there are _a__i_ cars «Lada Malina» that are ready for operation. As an attempt to increase sales of a new car, «AutoVoz» is going to hold an international exposition of cars. There are _q_ options of exposition location and time, in the _i_-th of them exposition will happen in a point with coordinates (_px__i_,u2009_py__i_) in _t__i_ days. Of course, at the «AutoVoz» is going to bring as much new cars from factories as possible to the place of exposition. Cars are going to be moved by enabling their engines on some certain levels, such that at the beginning of an exposition car gets exactly to the exposition location. However, for some of the options it may be impossible to bring cars from some of the factories to the exposition location by the moment of an exposition. Your task is to determine for each of the options of exposition location and time how many cars will be able to get there by the beginning of an exposition. Input The first line of input contains three integers _k_,u2009_n_,u2009_q_ (2u2009≤u2009_k_u2009≤u200910, 1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_q_u2009≤u2009105), the number of engines of «Lada Malina», number of factories producing «Lada Malina» and number of options of an exposition time and location respectively. The following _k_ lines contain the descriptions of «Lada Malina» engines. The _i_-th of them contains two integers _vx__i_, _vy__i_ (u2009-u20091000u2009≤u2009_vx__i_,u2009_vy__i_u2009≤u20091000) defining the velocity vector of the _i_-th engine. Velocity vector can't be zero, i.e. at least one of _vx__i_ and _vy__i_ is not equal to zero. It is guaranteed that no two velosity vectors are collinear (parallel). Next _n_ lines contain the descriptions of factories. The _i_-th of them contains two integers _fx__i_, _fy__i_, _a__i_ (u2009-u2009109u2009≤u2009_fx__i_,u2009_fy__i_u2009≤u2009109, 1u2009≤u2009_a__i_u2009≤u2009109) defining the coordinates of the _i_-th factory location and the number of cars that are located there. The following _q_ lines contain the descriptions of the car exposition. The _i_-th of them contains three integers _px__i_, _py__i_, _t__i_ (u2009-u2009109u2009≤u2009_px__i_,u2009_py__i_u2009≤u2009109, 1u2009≤u2009_t__i_u2009≤u2009105) defining the coordinates of the exposition location and the number of days till the exposition start in the _i_-th option. Note Images describing sample tests are given below. Exposition options are denoted with crosses, factories are denoted with points. Each factory is labeled with a number of cars that it has. First sample test explanation:
3,400
false
false
false
false
true
false
false
false
false
false
6,330
798B
Problem - 798B - 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 strings *1300 No tag edit access → Contest materials ") Editorial") — the number of strings. This is followed by _n_ lines which contain a string each. The _i_-th line corresponding to string _s__i_. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50. Output Print the minimal number of moves Mike needs in order to make all the strings equal or print u2009-u20091 if there is no solution. Examples Input 4 xzzwo zwoxz zzwox xzzwo Output 5 Input 2 molzv lzvmo Output 2 Input 3 kc kc kc Output 0 Input 3 aa aa ab Output -1 Note In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
1,300
false
false
false
true
false
false
true
false
false
false
6,580
525C
In the evening, after the contest Ilya was bored, and he really felt like maximizing. He remembered that he had a set of _n_ sticks and an instrument. Each stick is characterized by its length _l__i_. Ilya decided to make a rectangle from the sticks. And due to his whim, he decided to make rectangles in such a way that maximizes their total area. Each stick is used in making at most one rectangle, it is possible that some of sticks remain unused. Bending sticks is not allowed. Sticks with lengths _a_1, _a_2, _a_3 and _a_4 can make a rectangle if the following properties are observed: _a_1u2009≤u2009_a_2u2009≤u2009_a_3u2009≤u2009_a_4 _a_1u2009=u2009_a_2 _a_3u2009=u2009_a_4 A rectangle can be made of sticks with lengths of, for example, 3xa03xa03xa03 or 2xa02xa04xa04. A rectangle cannot be made of, for example, sticks 5xa05xa05xa07. Ilya also has an instrument which can reduce the length of the sticks. The sticks are made of a special material, so the length of each stick can be reduced by at most one. For example, a stick with length 5 can either stay at this length or be transformed into a stick of length 4. You have to answer the question — what maximum total area of the rectangles can Ilya get with a file if makes rectangles from the available sticks? Input The first line of the input contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0—xa0the number of the available sticks. The second line of the input contains _n_ positive integers _l__i_ (2u2009≤u2009_l__i_u2009≤u2009106)xa0—xa0the lengths of the sticks.
1,600
true
true
false
false
false
false
false
false
true
false
7,734
576D
In the country there are exactly _n_ cities numbered with positive integers from 1 to _n_. In each city there is an airport is located. Also, there is the only one airline, which makes _m_ flights. Unfortunately, to use them, you need to be a regular customer of this company, namely, you have the opportunity to enjoy flight _i_ from city _a__i_ to city _b__i_ only if you have already made at least _d__i_ flights before that. Please note that flight _i_ flies exactly from city _a__i_ to city _b__i_. It can not be used to fly from city _b__i_ to city _a__i_. An interesting fact is that there may possibly be recreational flights with a beautiful view of the sky, which begin and end in the same city. You need to get from city 1 to city _n_. Unfortunately, you've never traveled by plane before. What minimum number of flights you have to perform in order to get to city _n_? Note that the same flight can be used multiple times. Input The first line contains two integers, _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009150, 1u2009≤u2009_m_u2009≤u2009150) — the number of cities in the country and the number of flights the company provides. Next _m_ lines contain numbers _a__i_, _b__i_, _d__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, 0u2009≤u2009_d__i_u2009≤u2009109), representing flight number _i_ from city _a__i_ to city _b__i_, accessible to only the clients who have made at least _d__i_ flights. Output Print "Impossible" (without the quotes), if it is impossible to get from city 1 to city _n_ using the airways. But if there is at least one way, print a single integer — the minimum number of flights you need to make to get to the destination point.
2,700
false
false
false
true
false
false
false
false
false
false
7,540
1946E
Some permutation of length $$$n$$$ is guessed. You are given the indices of its prefix maximums and suffix maximums. Recall that a permutation of length $$$k$$$ is an array of size $$$k$$$ such that each integer from $$$1$$$ to $$$k$$$ occurs exactly once. Prefix maximums are the elements that are the maximum on the prefix ending at that element. More formally, the element $$$a_i$$$ is a prefix maximum if $$$a_i > a_j$$$ for every $$$j < i$$$. Similarly, suffix maximums are defined, the element $$$a_i$$$ is a suffix maximum if $$$a_i > a_j$$$ for every $$$j > i$$$. You need to output the number of different permutations that could have been guessed. As this number can be very large, output the answer modulo $$$10^9 + 7$$$. Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains three integers $$$n, m_1$$$ and $$$m_2$$$ ($$$1 le m_1, m_2 le n le 2 cdot 10^5$$$)xa0— the length of the permutation, the number of prefix maximums, and the number of suffix maximums, respectively. The second line of each test case contains $$$m_1$$$ integers $$$p_1 < p_2 < ldots < p_{m_1}$$$ ($$$1 le p_i le n$$$)xa0— the indices of the prefix maximums in increasing order. The third line of each test case contains $$$m_2$$$ integers $$$s_1 < s_2 < ldots < s_{m_2}$$$ ($$$1 le s_i le n$$$)xa0— the indices of the suffix maximums in increasing order. It is guaranteed that the sum of the values of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer on a separate linexa0— the number of suitable permutations modulo $$$10^9 + 7$$$. Example Input 6 1 1 1 1 1 4 2 3 1 2 2 3 4 3 3 1 1 2 3 3 5 3 4 1 2 3 2 3 4 5 20 5 4 1 2 3 4 12 12 13 18 20 6 2 3 1 3 3 4 6 Output 1 3 1 0 317580808 10 Note The following permutations are suitable for the second set of input data: $$$[1, 4, 3, 2]$$$ $$$[2, 4, 3, 1]$$$ $$$[3, 4, 2, 1]$$$ The following permutations are suitable for the sixth set of input data: $$$[2, 1, 6, 5, 3, 4]$$$ $$$[3, 1, 6, 5, 2, 4]$$$ $$$[3, 2, 6, 5, 1, 4]$$$ $$$[4, 1, 6, 5, 2, 3]$$$ $$$[4, 2, 6, 5, 1, 3]$$$ $$$[4, 3, 6, 5, 1, 2]$$$ $$$[5, 1, 6, 4, 2, 3]$$$ $$$[5, 2, 6, 4, 1, 3]$$$ $$$[5, 3, 6, 4, 1, 2]$$$ $$$[5, 4, 6, 3, 1, 2]$$$
2,200
true
false
false
true
false
false
false
false
false
false
601
846D
Recently Luba bought a monitor. Monitor is a rectangular matrix of size _n_u2009×u2009_m_. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square _k_u2009×u2009_k_ consisting entirely of broken pixels. She knows that _q_ pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all _q_ pixels stopped working). Input The first line contains four integer numbers _n_,u2009_m_,u2009_k_,u2009_q_xa0(1u2009≤u2009_n_,u2009_m_u2009≤u2009500,u20091u2009≤u2009_k_u2009≤u2009_min_(_n_,u2009_m_),u20090u2009≤u2009_q_u2009≤u2009_n_·_m_) — the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels. Each of next _q_ lines contain three integer numbers _x__i_,u2009_y__i_,u2009_t__i_xa0(1u2009≤u2009_x__i_u2009≤u2009_n_,u20091u2009≤u2009_y__i_u2009≤u2009_m_,u20090u2009≤u2009_t_u2009≤u2009109) — coordinates of _i_-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once. We consider that pixel is already broken at moment _t__i_. Output Print one number — the minimum moment the monitor became broken, or "-1" if it's still not broken after these _q_ pixels stopped working. Examples Input 2 3 2 5 2 1 8 2 2 8 1 2 1 1 3 4 2 3 2 Input 3 3 2 5 1 2 2 2 2 1 2 3 5 3 2 10 2 1 100
1,900
false
false
false
false
true
false
false
true
false
false
6,374
1381A1
This is the easy version of the problem. The difference between the versions is the constraint on $$$n$$$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$ (a binary string is a string consisting of symbols $$$0$$$ and $$$1$$$). In an operation, you select a prefix of $$$a$$$, and simultaneously invert the bits in the prefix ($$$0$$$ changes to $$$1$$$ and $$$1$$$ changes to $$$0$$$) and reverse the order of the bits in the prefix. For example, if $$$a=001011$$$ and you select the prefix of length $$$3$$$, it becomes $$$011011$$$. Then if you select the entire string, it becomes $$$001001$$$. Your task is to transform the string $$$a$$$ into $$$b$$$ in at most $$$3n$$$ operations. It can be proved that it is always possible. Input The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$) xa0— the number of test cases. Next $$$3t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 1000$$$) xa0— the length of the binary strings. The next two lines contain two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$1000$$$. Output For each test case, output an integer $$$k$$$ ($$$0le kle 3n$$$), followed by $$$k$$$ integers $$$p_1,ldots,p_k$$$ ($$$1le p_ile n$$$). Here $$$k$$$ is the number of operations you use and $$$p_i$$$ is the length of the prefix you flip in the $$$i$$$-th operation. Example Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 Note In the first test case, we have $$$01 o 11 o 00 o 10$$$. In the second test case, we have $$$01011 o 00101 o 11101 o 01000 o 10100 o 00100 o 11100$$$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $$$2$$$, which will leave $$$a$$$ unchanged.
1,300
false
false
false
false
true
true
false
false
false
false
3,764
17A
Problem - 17A - Codeforces =============== xa0 and _k_ (0u2009≤u2009_k_u2009≤u20091000). Output Output YES if at least _k_ prime numbers from 2 to _n_ inclusively can be expressed as it was described above. Otherwise output NO. Examples Input 27 2 Output YES Input 45 7 Output NO Note In the first sample the answer is YES since at least two numbers can be expressed as it was described (for example, 13 and 19). In the second sample the answer is NO since it is impossible to express 7 prime numbers from 2 to 45 in the desired form.
1,000
true
false
false
false
false
false
true
false
false
false
9,910
1497C1
Problem - 1497C1 - 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 math *1200 No tag edit access → Contest materials time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It is the easy version of the problem. The only difference is that in this version $$$k = 3$$$. You are given a positive integer $$$n$$$. Find $$$k$$$ positive integers $$$a_1, a_2, ldots, a_k$$$, such that: $$$a_1 + a_2 + ldots + a_k = n$$$ $$$LCM(a_1, a_2, ldots, a_k) le frac{n}{2}$$$ Here $$$LCM$$$ is the $$$ xa0— the number of test cases. The only line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 le n le 10^9$$$, $$$k = 3$$$). Output For each test case print $$$k$$$ positive integers $$$a_1, a_2, ldots, a_k$$$, for which all conditions are satisfied. Example Input 3 3 3 8 3 14 3 Output 1 1 1 4 2 2 2 6 6
1,200
true
false
false
false
false
true
false
false
false
false
3,186