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
592C
Vector Willman and Array Bolt are the two most famous athletes of Byteforces. They are going to compete in a race with a distance of _L_ meters today. Willman and Bolt have exactly the same speed, so when they compete the result is always a tie. That is a problem for the organizers because they want a winner. While watching previous races the organizers have noticed that Willman can perform only steps of length equal to _w_ meters, and Bolt can perform only steps of length equal to _b_ meters. Organizers decided to slightly change the rules of the race. Now, at the end of the racetrack there will be an abyss, and the winner will be declared the athlete, who manages to run farther from the starting point of the the racetrack (which is not the subject to change by any of the athletes). Note that none of the athletes can run infinitely far, as they both will at some moment of time face the point, such that only one step further will cause them to fall in the abyss. In other words, the athlete will not fall into the abyss if the total length of all his steps will be less or equal to the chosen distance _L_. Since the organizers are very fair, the are going to set the length of the racetrack as an integer chosen randomly and uniformly in range from 1 to _t_ (both are included). What is the probability that Willman and Bolt tie again today? Input The first line of the input contains three integers _t_, _w_ and _b_ (1u2009≀u2009_t_,u2009_w_,u2009_b_u2009≀u20095Β·1018) β€” the maximum possible length of the racetrack, the length of Willman's steps and the length of Bolt's steps respectively. Output Print the answer to the problem as an irreducible fraction . Follow the format of the samples output. The fraction (_p_ and _q_ are integers, and both _p_u2009β‰₯u20090 and _q_u2009>u20090 holds) is called irreducible, if there is no such integer _d_u2009>u20091, that both _p_ and _q_ are divisible by _d_. Note In the first sample Willman and Bolt will tie in case 1, 6 or 7 are chosen as the length of the racetrack.
1,800
true
false
false
false
false
false
false
false
false
false
7,480
12B
Problem - 12B - 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 sortings *1100 No tag edit access β†’ Contest materials ") without leading zeroes. The second lines contains one integer _m_ (0u2009≀u2009_m_u2009≀u2009109) β€” Bob's answer, possibly with leading zeroes. Output Print OK if Bob's answer is correct and WRONG_ANSWER otherwise. Examples Input 3310 1033 Output OK Input 4 5 Output WRONG_ANSWER
1,100
false
false
true
false
false
false
false
false
true
false
9,934
15E
Problem - 15E - Codeforces =============== xa0 ") . After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: it starts and ends in the same place β€” the granny's house; the route goes along the forest paths only (these are the segments marked black in the picture); the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); the route cannot cross itself; there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. The example of the area map for _n_u2009=u200912 is given in the picture. Since the map has a regular structure, you can construct it for other _n_ by analogy using the example. Input The input data contain the only even integer _n_ (2u2009≀u2009_n_u2009≀u2009106). Output Output the only number β€” the amount of Peter's routes modulo 1000000009. Examples Input 2 Output 10 Input 4 Output 74
2,600
false
false
false
true
false
false
false
false
false
false
9,916
254A
Problem - 254A - 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 sortings *1200 No tag edit access β†’ Contest materials and Codeforces Round #155 (Div. 2)") β€” tutorial") . The second line contains the sequence of 2_n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a_2_n_ (1u2009≀u2009_a__i_u2009≀u20095000) β€” the numbers that are written on the cards. The numbers on the line are separated by single spaces. Output If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print _n_ pairs of integers, a pair per line β€” the indices of the cards that form the pairs. Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them. Examples Input 3 20 30 10 30 20 10 Output 4 2 1 5 6 3 Input 1 1 2 Output -1
1,200
false
false
false
false
false
true
false
false
true
false
8,819
1955D
Maxim has an array $$$a$$$ of $$$n$$$ integers and an array $$$b$$$ of $$$m$$$ integers ($$$m le n$$$). Maxim considers an array $$$c$$$ of length $$$m$$$ to be good if the elements of array $$$c$$$ can be rearranged in such a way that at least $$$k$$$ of them match the elements of array $$$b$$$. For example, if $$$b = [1, 2, 3, 4]$$$ and $$$k = 3$$$, then the arrays $$$[4, 1, 2, 3]$$$ and $$$[2, 3, 4, 5]$$$ are good (they can be reordered as follows: $$$[1, 2, 3, 4]$$$ and $$$[5, 2, 3, 4]$$$), while the arrays $$$[3, 4, 5, 6]$$$ and $$$[3, 4, 3, 4]$$$ are not good. Maxim wants to choose every subsegment of array $$$a$$$ of length $$$m$$$ as the elements of array $$$c$$$. Help Maxim count how many selected arrays will be good. In other words, find the number of positions $$$1 le l le n - m + 1$$$ such that the elements $$$a_l, a_{l+1}, dots, a_{l + m - 1}$$$ form a good array. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 le k le m le n le 2 cdot 10^5$$$)xa0β€” the number of elements in arrays $$$a$$$ and $$$b$$$, the required number of matching elements. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0β€” the elements of array $$$a$$$. Elements of the array $$$a$$$ are not necessarily unique. The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_i le 10^6$$$)xa0β€” the elements of array $$$b$$$. Elements of the array $$$b$$$ are not necessarily unique. 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 test cases does not exceed $$$2 cdot 10^5$$$. Note In the first example, all subsegments are good. In the second example, good subsegments start at positions $$$1$$$, $$$2$$$, and $$$3$$$. In the third example, good subsegments start at positions $$$1$$$ and $$$2$$$.
1,400
false
false
false
false
true
false
false
false
false
false
545
1737C
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Dytechlab Cup 2022 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 games implementation math *1500 No tag edit access β†’ Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST C. Ela and Crickets time limit per test1 second memory limit per test256 megabytes Ela likes Chess a lot. During breaks, she usually challenges her co-worker in DTL to some chess games. She's not an expert at classic chess, but she's very interested in Chess variants, where she has to adapt to new rules and test her tactical mindset to win the game. The problem, which involves a non-standard chess pieces type that is described below, reads: given $$$3$$$ white crickets on a $$$n cdot n$$$ board, arranged in an "L" shape next to each other, there are no other pieces on the board. Ela wants to know with a finite number of moves, can she put any white cricket on the square on row $$$x$$$, column $$$y$$$? An "L"-shape piece arrangement can only be one of the below: For simplicity, we describe the rules for crickets on the board where only three white crickets are. It can move horizontally, vertically, or diagonally, but only to a square in some direction that is immediately after another cricket piece (so that it must jump over it). If the square immediately behind the piece is unoccupied, the cricket will occupy the square. Otherwise (when the square is occupied by another cricket, or does not exist), the cricket isn't allowed to make such a move. See an example of valid crickets' moves on the pictures in the Note section. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains $$$n$$$ ($$$4 le n le 10^5$$$) β€” denotes the size of the chessboard. The second line of each test case contains 6 numbers: $$$r_1$$$, $$$c_1$$$, $$$r_2$$$, $$$c_2$$$, $$$r_3$$$, $$$c_3$$$ ($$$1 le r_1, c_1, r_2, c_2, r_3, c_3 le n$$$) β€” coordinates of the crickets. The input ensures that the three crickets are arranged in an "L" shape that the legend stated. The third line of each test case contains 2 numbers: $$$x$$$, $$$y$$$ ($$$1 le x, y le n$$$) β€” coordinates of the target square. Output For each test case, print "YES" or "NO" to denotes whether Ela can put a cricket on the target square. Example input 6 8 7 2 8 2 7 1 5 1 8 2 2 1 2 2 1 5 5 8 2 2 1 2 2 1 6 6 8 1 1 1 2 2 1 5 5 8 2 2 1 2 2 1 8 8 8 8 8 8 7 7 8 4 8 output YES NO YES NO YES YES Note Here's the solution for the first test case. The red square denotes where the crickets need to reach. Note that in chess horizontals are counted from bottom to top, as well as on this picture. Codeforces (c)
1,500
true
false
true
false
false
true
false
false
false
false
1,866
1717B
Madoka decided to participate in an underground sports programming competition. And there was exactly one task in it: A square table of size $$$n imes n$$$, where $$$n$$$ is a multiple of $$$k$$$, is called good if only the characters '.' and 'X' are written in it, as well as in any subtable of size $$$1 imes k$$$ or $$$k imes 1$$$, there is at least one character 'X'. In other words, among any $$$k$$$ consecutive vertical or horizontal cells, there must be at least one containing the character 'X'. Output any good table that has the minimum possible number of characters 'X', and also the symbol 'X' is written in the cell $$$(r, c)$$$. Rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, columns are numbered from $$$1$$$ to $$$n$$$ from left to right. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β€” the number of test cases. Description of the test cases follows. The first and the only line of each test case contains four integers $$$n$$$, $$$k$$$, $$$r$$$, $$$c$$$ ($$$1 le n le 500, 1 le k le n, 1 le r, c le n$$$)xa0β€” the size of the table, the integer $$$k$$$ and the coordinates of the cell, which must contain the character 'X'. It is guaranteed that $$$n$$$ is a multiple of $$$k$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output For each test case, output $$$n$$$ lines, each consisting of $$$n$$$ characters '.' and 'X',xa0β€” the desired table. If there are several answers, then you can output anyone. Example Input 3 3 3 3 2 2 1 1 2 6 3 4 2 Output X.. ..X .X. XX XX .X..X. X..X.. ..X..X .X..X. X..X.. ..X..X Note Let's analyze the first test case. The following tables can be printed as the correct answer: or It can be proved that there cannot be less than $$$3$$$ characters 'X' in the answer. Note that the following table is invalid because cell $$$(3, 2)$$$ does not contain the character 'X': In the second test case, the only correct table is: Each subtable of size $$$1 imes 1$$$ must contain a 'X' character, so all characters in the table must be equal to 'X'.
1,100
false
false
true
false
false
true
false
false
false
false
1,980
977E
You are given an undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Your task is to find the number of connected components which are cycles. Here are some definitions of graph theory. An undirected graph consists of two sets: set of nodes (called vertices) and set of edges. Each edge connects a pair of vertices. All edges are bidirectional (i.e. if a vertex $$$a$$$ is connected with a vertex $$$b$$$, a vertex $$$b$$$ is also connected with a vertex $$$a$$$). An edge can't connect vertex with itself, there is at most one edge between a pair of vertices. Two vertices $$$u$$$ and $$$v$$$ belong to the same connected component if and only if there is at least one path along edges connecting $$$u$$$ and $$$v$$$. A connected component is a cycle if and only if its vertices can be reordered in such a way that: the first vertex is connected with the second vertex by an edge, the second vertex is connected with the third vertex by an edge, ... the last vertex is connected with the first vertex by an edge, all the described edges of a cycle are distinct. A cycle doesn't contain any other edges except described above. By definition any cycle contains three or more vertices. There are $$$6$$$ connected components, $$$2$$$ of them are cycles: $$$[7, 10, 16]$$$ and $$$[5, 11, 9, 15]$$$. Input The first line contains two integer numbers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le m le 2 cdot 10^5$$$) β€” number of vertices and edges. The following $$$m$$$ lines contains edges: edge $$$i$$$ is given as a pair of vertices $$$v_i$$$, $$$u_i$$$ ($$$1 le v_i, u_i le n$$$, $$$u_i e v_i$$$). There is no multiple edges in the given graph, i.e. for each pair ($$$v_i, u_i$$$) there no other pairs ($$$v_i, u_i$$$) and ($$$u_i, v_i$$$) in the list of edges. Output Print one integer β€” the number of connected components which are also cycles. Examples Input 17 15 1 8 1 12 5 11 11 9 9 15 15 5 4 13 3 13 4 3 10 16 7 10 16 7 14 3 14 4 17 6 Note In the first example only component $$$[3, 4, 5]$$$ is also a cycle. The illustration above corresponds to the second example.
1,500
false
false
false
false
false
false
false
false
false
true
5,820
1288D
You are given $$$n$$$ arrays $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$; each array consists of exactly $$$m$$$ integers. We denote the $$$y$$$-th element of the $$$x$$$-th array as $$$a_{x, y}$$$. You have to choose two arrays $$$a_i$$$ and $$$a_j$$$ ($$$1 le i, j le n$$$, it is possible that $$$i = j$$$). After that, you will obtain a new array $$$b$$$ consisting of $$$m$$$ integers, such that for every $$$k in [1, m]$$$ $$$b_k = max(a_{i, k}, a_{j, k})$$$. Your goal is to choose $$$i$$$ and $$$j$$$ so that the value of $$$min limits_{k = 1}^{m} b_k$$$ is maximum possible. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 3 cdot 10^5$$$, $$$1 le m le 8$$$) β€” the number of arrays and the number of elements in each array, respectively. Then $$$n$$$ lines follow, the $$$x$$$-th line contains the array $$$a_x$$$ represented by $$$m$$$ integers $$$a_{x, 1}$$$, $$$a_{x, 2}$$$, ..., $$$a_{x, m}$$$ ($$$0 le a_{x, y} le 10^9$$$). Output Print two integers $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$, it is possible that $$$i = j$$$) β€” the indices of the two arrays you have to choose so that the value of $$$min limits_{k = 1}^{m} b_k$$$ is maximum possible. If there are multiple answers, print any of them. Example Input 6 5 5 0 3 1 2 1 8 9 1 3 1 2 3 4 5 9 1 0 3 7 2 3 0 6 3 6 4 1 7 0
2,000
false
false
false
true
false
false
false
true
false
false
4,249
1423C
As you may already know, DuΕ‘an is keen on playing with railway models. He has a big map with cities that are connected with railways. His map can be seen as a graph where vertices are cities and the railways connecting them are the edges. So far, the graph corresponding to his map is a tree. As you already know, a tree is a connected acyclic undirected graph. He is curious to find out whether his railway can be optimized somehow. He wants to add so-called shortcuts, which are also railways connecting pairs of cities. This shortcut will represent the railways in the unique path in the tree between the pair of cities it connects. Since DuΕ‘an doesn't like repeating the railways, he has also defined good paths in his newly obtained network (notice that after adding the shortcuts, his graph is no more a tree). He calls a path good, if no edge appears more than once, either as a regular railway edge or as an edge represented by some shortcut (Every shortcut in a good path has length 1, but uses up all the edges it represents - they can't appear again in that path). Having defined good paths, he defines good distance between two cities to be the length of the shortest good path between them. Finally, the shortcutting diameter of his network is the largest good distance between any two cities. Now he is curious to find out whether it is possible to achieve shortcutting diameter less or equal than $$$k$$$, while adding as few shortcuts as possible. Your solution should add no more than $$$mathbf{10 cdot n}$$$ shortcuts. Input The first line in the standard input contains an integer $$$n$$$ ($$$1 le n le 10^4$$$), representing the number of the cities in DuΕ‘an's railway map, and an integer k ($$$3 le k le n$$$) representing the shortcutting diameter that he wants to achieve. Each of the following $$$n - 1$$$ lines will contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n, u_i eq v_i$$$), meaning that there is a railway between cities $$$u_i$$$ and $$$v_i$$$. Output The first line of the output should contain a number $$$t$$$ representing the number of the shortcuts that were added. Each of the following $$$t$$$ lines should contain two integers $$$u_i$$$ and $$$v_i$$$, signifying that a shortcut is added between cities $$$u_i$$$ and $$$v_i$$$. Example Input 10 3 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 Output 8 3 7 3 5 3 6 3 1 7 9 7 10 7 4 7 5 Note Notice that adding a shortcut between all cities and city 1 will make a graph theoretic diameter become 2. On the other hand, the paths obtained that way might not be good, since some of the edges might get duplicated. In the example, adding a shortcut between all cities and city 1 doesn't create a valid solution, because for cities 5 and 10 the path that uses shortcuts 5-1 and 1-10 is not valid because it uses edges 1-2, 2-3, 3-4, 4-5 twice.
3,500
false
false
false
false
false
false
false
false
false
true
3,561
659G
Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of _n_ consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the _i_-th board to the left is _h__i_. Today Vasily decided to change the design of the fence he had built, by cutting his top connected part so that the fence remained good. The cut part should consist of only the upper parts of the boards, while the adjacent parts must be interconnected (share a non-zero length before cutting out of the fence). You, as Vasily's curious neighbor, will count the number of possible ways to cut exactly one part as is described above. Two ways to cut a part are called distinct, if for the remaining fences there is such _i_, that the height of the _i_-th boards vary. As Vasily's fence can be very high and long, get the remainder after dividing the required number of ways by 1u2009000u2009000u2009007 (109u2009+u20097). Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u20091u2009000u2009000)xa0β€” the number of boards in Vasily's fence. The second line contains _n_ space-separated numbers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≀u2009_h__i_u2009≀u2009109), where _h__i_ equals the height of the _i_-th board to the left. Output Print the remainder after dividing _r_ by 1u2009000u2009000u2009007, where _r_ is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good. Note From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good. All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part):
2,300
false
false
false
true
false
false
false
false
false
false
7,183
1647B
Madoka's father just reached $$$1$$$ million subscribers on Mathub! So the website decided to send him a personalized awardxa0β€” The Mathhub's Bit Button! The Bit Button is a rectangular table with $$$n$$$ rows and $$$m$$$ columns with $$$0$$$ or $$$1$$$ in each cell. After exploring the table Madoka found out that: A subrectangle $$$A$$$ is contained in a subrectangle $$$B$$$ if there's no cell contained in $$$A$$$ but not contained in $$$B$$$. Two subrectangles intersect if there is a cell contained in both of them. A subrectangle is called black if there's no cell with value $$$0$$$ inside it. A subrectangle is called nice if it's black and it's not contained in another black subrectangle. The table is called elegant if there are no two nice intersecting subrectangles. For example, in the first illustration the red subrectangle is nice, but in the second one it's not, because it's contained in the purple subrectangle. Help Madoka to determine whether the table is elegant. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 200$$$)xa0β€” the number of test cases. Description of the test cases follows. The first line of each test case contains two positive integers $$$n, m$$$ ($$$1 le n, m le 100$$$). The next $$$n$$$ lines contain strings of length $$$m$$$ consisting of zeros and onesxa0β€” the description of the table. It is guaranteed that the sum of the values of $$$n$$$ and the sum of the values of $$$m$$$ for all test cases do not exceed $$$777$$$. Output For each test case print "YES" if its table is elegant or print "NO" otherwise. You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer). Example Input 5 3 3 100 011 011 3 3 110 111 110 1 5 01111 4 5 11111 01010 01000 01000 3 2 11 00 11 Note In the second test case the table is not elegant, because the red and the purple subrectangles are nice and intersect. In the fourth test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.
1,200
false
false
true
false
false
true
true
false
false
true
2,388
590A
A schoolboy named Vasya loves reading books on programming and mathematics. He has recently read an encyclopedia article that described the method of median smoothing (or median filter) and its many applications in science and engineering. Vasya liked the idea of the method very much, and he decided to try it in practice. Applying the simplest variant of median smoothing to the sequence of numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ will result a new sequence _b_1,u2009_b_2,u2009...,u2009_b__n_ obtained by the following algorithm: _b_1u2009=u2009_a_1, _b__n_u2009=u2009_a__n_, that is, the first and the last number of the new sequence match the corresponding numbers of the original sequence. For _i_u2009=u20092,u2009...,u2009_n_u2009-u20091 value _b__i_ is equal to the median of three values _a__i_u2009-u20091, _a__i_ and _a__i_u2009+u20091. The median of a set of three numbers is the number that goes on the second place, when these three numbers are written in the non-decreasing order. For example, the median of the set 5, 1, 2 is number 2, and the median of set 1, 0, 1 is equal to 1. In order to make the task easier, Vasya decided to apply the method to sequences consisting of zeros and ones only. Having made the procedure once, Vasya looked at the resulting sequence and thought: what if I apply the algorithm to it once again, and then apply it to the next result, and so on? Vasya tried a couple of examples and found out that after some number of median smoothing algorithm applications the sequence can stop changing. We say that the sequence is stable, if it does not change when the median smoothing is applied to it. Now Vasya wonders, whether the sequence always eventually becomes stable. He asks you to write a program that, given a sequence of zeros and ones, will determine whether it ever becomes stable. Moreover, if it ever becomes stable, then you should determine what will it look like and how many times one needs to apply the median smoothing algorithm to initial sequence in order to obtain a stable one. Input The first input line of the input contains a single integer _n_ (3u2009≀u2009_n_u2009≀u2009500u2009000)xa0β€” the length of the initial sequence. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009=u20090 or _a__i_u2009=u20091), giving the initial sequence itself. Output If the sequence will never become stable, print a single number u2009-u20091. Otherwise, first print a single integerxa0β€” the minimum number of times one needs to apply the median smoothing algorithm to the initial sequence before it becomes is stable. In the second line print _n_ numbers separated by a space xa0β€” the resulting sequence itself.
1,700
false
false
true
false
false
false
false
false
false
false
7,489
1451B
Hr0d1y has $$$q$$$ queries on a binary string $$$s$$$ of length $$$n$$$. A binary string is a string containing only characters '0' and '1'. A query is described by a pair of integers $$$l_i$$$, $$$r_i$$$ $$$(1 leq l_i lt r_i leq n)$$$. For each query, he has to determine whether there exists a good subsequence in $$$s$$$ that is equal to the substring $$$s[l_ildots r_i]$$$. A substring $$$s[ildots j]$$$ of a string $$$s$$$ is the string formed by characters $$$s_i s_{i+1} ldots s_j$$$. String $$$a$$$ is said to be a subsequence of string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some characters without changing the order of the remaining characters. A subsequence is said to be good if it is not contiguous and has length $$$ge 2$$$. For example, if $$$s$$$ is "1100110", then the subsequences $$$s_1s_2s_4$$$ ("1100110") and $$$s_1s_5s_7$$$ ("1100110") are good, while $$$s_1s_2s_3$$$ ("1100110") is not good. Can you help Hr0d1y answer each query? Input The first line of the input contains a single integer $$$t$$$ ($$$1leq t leq 100$$$)xa0β€” the number of test cases. The description of each test case is as follows. The first line contains two integers $$$n$$$ ($$$2 leq n leq 100$$$) and $$$q$$$ ($$$1leq q leq 100$$$)xa0β€” the length of the string and the number of queries. The second line contains the string $$$s$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i lt r_i leq n$$$). Output For each test case, output $$$q$$$ lines. The $$$i$$$-th line of the output of each test case should contain "YES" if there exists a good subsequence equal to the substring $$$s[l_i...r_i]$$$, and "NO" otherwise. You may print each letter in any case (upper or lower). Example Input 2 6 3 001000 2 4 1 3 3 5 4 2 1111 1 4 2 3 Note In the first test case, $$$s[2ldots 4] = $$$ "010". In this case $$$s_1s_3s_5$$$ ("001000") and $$$s_2s_3s_6$$$ ("001000") are good suitable subsequences, while $$$s_2s_3s_4$$$ ("001000") is not good. $$$s[1ldots 3] = $$$ "001". No suitable good subsequence exists. $$$s[3ldots 5] = $$$ "100". Here $$$s_3s_5s_6$$$ ("001000") is a suitable good subsequence.
900
false
true
true
true
false
false
false
false
false
false
3,427
1954D
There are balls of $$$n$$$ different colors; the number of balls of the $$$i$$$-th color is $$$a_i$$$. The balls can be combined into groups. Each group should contain at most $$$2$$$ balls, and no more than $$$1$$$ ball of each color. Consider all $$$2^n$$$ sets of colors. For a set of colors, let's denote its value as the minimum number of groups the balls of those colors can be distributed into. For example, if there are three colors with $$$3$$$, $$$1$$$ and $$$7$$$ balls respectively, they can be combined into $$$7$$$ groups (and not less than $$$7$$$), so the value of that set of colors is $$$7$$$. Your task is to calculate the sum of values over all $$$2^n$$$ possible sets of colors. Since the answer may be too large, print it modulo $$$998,244,353$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 5000$$$)xa0β€” the number of colors. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 5000$$$)xa0β€” the number of balls of the $$$i$$$-th color. Additional constraint on input: the total number of balls doesn't exceed $$$5000$$$. Output Print a single integerxa0β€” the sum of values of all $$$2^n$$$ sets of colors, taken modulo $$$998,244,353$$$. Note Consider the first example. There are $$$8$$$ sets of colors: for the empty set, its value is $$$0$$$; for the set $$${1}$$$, its value is $$$1$$$; for the set $$${2}$$$, its value is $$$1$$$; for the set $$${3}$$$, its value is $$$2$$$; for the set $$${1,2}$$$, its value is $$$1$$$; for the set $$${1,3}$$$, its value is $$$2$$$; for the set $$${2,3}$$$, its value is $$$2$$$; for the set $$${1,2,3}$$$, its value is $$$2$$$. So, the sum of values over all $$$2^n$$$ sets of colors is $$$11$$$.
1,800
true
false
false
true
false
false
false
false
true
false
551
431A
Problem - 431A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation *800 No tag edit access β†’ Contest materials ") ") . The second line contains string _s_ (1u2009≀u2009_s_u2009≀u2009105), where the _Ρ–_-th character of the string equals "1", if on the _i_-th second of the game the square appears on the first strip, "2", if it appears on the second strip, "3", if it appears on the third strip, "4", if it appears on the fourth strip. Output Print a single integer β€” the total number of calories that Jury wastes. Examples Input 1 2 3 4 123214 Output 13 Input 1 5 3 2 11221 Output 13
800
false
false
true
false
false
false
false
false
false
false
8,119
1506B
You are given a number $$$k$$$ and a string $$$s$$$ of length $$$n$$$, consisting of the characters '.' and '*'. You want to replace some of the '*' characters with 'x' characters so that the following conditions are met: The first character '*' in the original string should be replaced with 'x'; The last character '*' in the original string should be replaced with 'x'; The distance between two neighboring replaced characters 'x' must not exceed $$$k$$$ (more formally, if you replaced characters at positions $$$i$$$ and $$$j$$$ ($$$i < j$$$) and at positions $$$[i+1, j-1]$$$ there is no "x" symbol, then $$$j-i$$$ must be no more than $$$k$$$). For example, if $$$n=7$$$, $$$s=$$$.**.**and $$$k=3$$$, then the following strings will satisfy the conditions above: .xx.*xx; .x*.x*x; .xx.xxx. But, for example, the following strings will not meet the conditions: .**.*xx (the first character '*' should be replaced with 'x'); .x*.xx(the last character '*' should be replaced with 'x'); .x*.*xx (the distance between characters at positions $$$2$$$ and $$$6$$$ is greater than $$$k=3$$$). Given $$$n$$$, $$$k$$$, and $$$s$$$, find the minimum number of '*' characters that must be replaced with 'x' in order to meet the above conditions. Input The first line contains one integer $$$t$$$ ($$$1 le t le 500$$$). Then $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 50$$$). The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of the characters '.' and '*'. It is guaranteed that there is at least one '*' in the string $$$s$$$. It is guaranteed that the distance between any two neighboring '*' characters does not exceed $$$k$$$. Output For each test case output the minimum number of '*' characters that must be replaced with 'x' characters in order to satisfy the conditions above. Example Input 5 7 3 .**.*** 5 1 ..*.. 5 2 *.*.* 3 2 *.* 1 1 *
1,100
false
true
true
false
false
false
false
false
false
false
3,142
1179D
Fedor runs for president of Byteland! In the debates, he will be asked how to solve Byteland's transport problem. It's a really hard problem because of Byteland's transport system is now a tree (connected graph without cycles). Fedor's team has found out in the ministry of transport of Byteland that there is money in the budget only for one additional road. In the debates, he is going to say that he will build this road as a way to maximize the number of distinct simple paths in the country. A simple path is a path which goes through every vertex no more than once. Two simple paths are named distinct if sets of their edges are distinct. But Byteland's science is deteriorated, so Fedor's team hasn't succeeded to find any scientists to answer how many distinct simple paths they can achieve after adding exactly one edge on the transport system? Help Fedor to solve it. An edge can be added between vertices that are already connected, but it can't be a loop. In this problem, we consider only simple paths of length at least two. Input The first line contains one integer $$$n$$$ ($$$2 leq n leq 500 000$$$)xa0β€” number of vertices in Byteland's transport system. Each of the following $$$n - 1$$$ lines contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 leq v_i, u_i leq n$$$). It's guaranteed that the graph is tree. Output Print exactly one integerxa0β€” a maximal number of simple paths that can be achieved after adding one edge. Examples Input 6 1 2 1 3 3 4 3 5 4 6
2,700
false
false
false
true
true
false
false
false
false
false
4,818
1305B
Now that Kuroni has reached 10 years old, he is a big boy and doesn't like arrays of integers as presents anymore. This year he wants a Bracket sequence as a Birthday present. More specifically, he wants a bracket sequence so complex that no matter how hard he tries, he will not be able to remove a simple subsequence! We say that a string formed by $$$n$$$ characters '(' or ')' is simple if its length $$$n$$$ is even and positive, its first $$$frac{n}{2}$$$ characters are '(', and its last $$$frac{n}{2}$$$ characters are ')'. For example, the strings () and (()) are simple, while the strings )( and ()() are not simple. Kuroni will be given a string formed by characters '(' and ')' (the given string is not necessarily simple). An operation consists of choosing a subsequence of the characters of the string that forms a simple string and removing all the characters of this subsequence from the string. Note that this subsequence doesn't have to be continuous. For example, he can apply the operation to the string ')()(()))', to choose a subsequence of bold characters, as it forms a simple string '(())', delete these bold characters from the string and to get '))()'. Kuroni has to perform the minimum possible number of operations on the string, in such a way that no more operations can be performed on the remaining string. The resulting string does not have to be empty. Since the given string is too large, Kuroni is unable to figure out how to minimize the number of operations. Can you help him do it instead? A sequence of characters $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters. Output In the first line, print an integer $$$k$$$ xa0β€” the minimum number of operations you have to apply. Then, print $$$2k$$$ lines describing the operations in the following format: For each operation, print a line containing an integer $$$m$$$ xa0β€” the number of characters in the subsequence you will remove. Then, print a line containing $$$m$$$ integers $$$1 le a_1 < a_2 < dots < a_m$$$ xa0β€” the indices of the characters you will remove. All integers must be less than or equal to the length of the current string, and the corresponding subsequence must form a simple string. If there are multiple valid sequences of operations with the smallest $$$k$$$, you may print any of them. Note In the first sample, the string is '(()(('. The operation described corresponds to deleting the bolded subsequence. The resulting string is '(((', and no more operations can be performed on it. Another valid answer is choosing indices $$$2$$$ and $$$3$$$, which results in the same final string. In the second sample, it is already impossible to perform any operations.
1,200
false
true
false
false
false
true
false
false
false
false
4,163
897A
Are you going to Scarborough Fair?Parsley, sage, rosemary and thyme. Remember me to one who lives there. He once was the true love of mine. Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there. Willem asks his friend, Grick for directions, Grick helped them, and gave them a task. Although the girl wants to help, Willem insists on doing it by himself. Grick gave Willem a string of length _n_. Willem needs to do _m_ operations, each operation has four parameters _l_,u2009_r_,u2009_c_1,u2009_c_2, which means that all symbols _c_1 in range [_l_,u2009_r_] (from _l_-th to _r_-th, including _l_ and _r_) are changed into _c_2. String is 1-indexed. Grick wants to know the final string after all the _m_ operations. Input The first line contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009100). The second line contains a string _s_ of length _n_, consisting of lowercase English letters. Each of the next _m_ lines contains four parameters _l_,u2009_r_,u2009_c_1,u2009_c_2 (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_n_, _c_1,u2009_c_2 are lowercase English letters), separated by space. Note For the second example: After the first operation, the string is wxxak. After the second operation, the string is waaak. After the third operation, the string is gaaak.
800
false
false
true
false
false
false
false
false
false
false
6,160
1922F
You are given an array $$$a_1, a_2, dots, a_n$$$, where each element is an integer from $$$1$$$ to $$$x$$$. You can perform the following operation with it any number of times: choose three integers $$$l$$$, $$$r$$$ and $$$k$$$ such that $$$1 le l le r le n$$$, $$$1 le k le x$$$ and each element $$$a_i$$$ such that $$$l le i le r$$$ is different from $$$k$$$. Then, for each $$$i in [l, r]$$$, replace $$$a_i$$$ with $$$k$$$. In other words, you choose a subsegment of the array and an integer from $$$1$$$ to $$$x$$$ which does not appear in that subsegment, and replace every element in the subsegment with that chosen integer. Your goal is to make all elements in the array equal. What is the minimum number of operations that you have to perform? Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases. Each test case consists of two lines: the first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le x le n le 100$$$); the second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le x$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output For each test case, print one integer β€” the minimum number of operations you have to perform. Example Input 3 3 2 1 2 1 6 3 1 2 3 1 2 3 12 3 3 1 3 1 2 1 1 2 3 1 1 3
2,500
false
false
false
true
false
false
false
false
false
false
753
797C
Problem - 797C - Codeforces =============== xa0 , consisting of lowercase English letters. Output Print resulting string _u_. Examples Input cab Output abc Input acdb Output abdc
1,700
false
true
false
false
true
false
false
false
false
false
6,585
1874E
It is well known that quick sort works by randomly selecting a 'pivot' element from the array and partitioning the other elements into two sub-arrays, according to whether they are less than or greater than the pivot. But Jellyfish thinks that choosing a random element is just a waste of time, so she always chooses the first element to be the pivot. The time her code needs to run can be calculated by the following pseudocode: function fun(A) if A.length > 0 let L[1 ... L.length] and R[1 ... R.length] be new arrays L.length = R.length = 0 for i = 2 to A.length if A[i] < A[1] L.length = L.length + 1 L[L.length] = A[i] else R.length = R.length + 1 R[R.length] = A[i] return A.length + fun(L) + fun(R) else return 0 Now you want to show her that her code is slow. When the function $$$mathrm{fun(A)}$$$ is greater than or equal to $$$lim$$$, her code will get $$$ ext{Time Limit Exceeded}$$$. You want to know how many distinct permutations $$$P$$$ of $$$[1, 2, dots, n]$$$ satisfies $$$mathrm{fun(P)} geq lim$$$. Because the answer may be large, you will only need to find the answer modulo $$$10^9+7$$$. Input The only line of the input contains two integers $$$n$$$ and $$$lim$$$ ($$$1 leq n leq 200$$$, $$$1 leq lim leq 10^9$$$). Output Output the number of different permutations that satisfy the condition modulo $$$10^9+7$$$. Note In the first example, $$$P = [1, 4, 2, 3]$$$ satisfies the condition, because: $$$mathrm{fun([1, 4, 2, 3]) = 4 + fun([4, 2, 3]) = 7 + fun([2, 3]) = 9 + fun([3]) = 10}$$$ Do remember to output the answer modulo $$$10^9+7$$$.
3,000
true
false
false
true
false
false
false
false
false
false
1,032
1184C2
The Cybermen solved that first test much quicker than the Daleks. Luckily for us, the Daleks were angry (shocking!) and they destroyed some of the Cybermen. After the fighting stopped, Heidi gave them another task to waste their time on. There are $$$n$$$ points on a plane. Given a radius $$$r$$$, find the maximum number of points that can be covered by an $$$L^1$$$-ball with radius $$$r$$$. An $$$L^1$$$-ball with radius $$$r$$$ and center $$$(x_0, y_0)$$$ in a 2D-plane is defined as the set of points $$$(x, y)$$$ such that the Manhattan distance between $$$(x_0, y_0)$$$ and $$$(x, y)$$$ is at most $$$r$$$. Manhattan distance between $$$(x_0, y_0)$$$ and $$$(x, y)$$$ is defined as $$$x - x_0 + y - y_0$$$. Input The first line contains two integers $$$n, r$$$ ($$$1 le n le 300,000, 1 le r le 10^6$$$), the number of points and the radius of the ball, respectively. Each of the next $$$n$$$ lines contains integers $$$x_i, y_i$$$ ($$$-10^6 leq x_i, y_i leq 10^6$$$), describing the coordinates of the $$$i$$$-th point. It is guaranteed, that all points are distinct. Output Print one integerxa0β€” the maximum number points that an $$$L^1$$$-ball with radius $$$r$$$ can cover. Examples Input 5 1 1 1 1 -1 -1 1 -1 -1 2 0 Input 5 2 1 1 1 -1 -1 1 -1 -1 2 0 Note In the first example, a ball centered at $$$(1, 0)$$$ covers the points $$$(1, 1)$$$, $$$(1, -1)$$$, $$$(2, 0)$$$. In the second example, a ball centered at $$$(0, 0)$$$ covers all the points. Note that $$$x_0$$$ and $$$y_0$$$ need not be integer.
2,200
false
false
false
false
true
false
false
false
false
false
4,787
119C
Yet another education system reform has been carried out in Berland recently. The innovations are as follows: An academic year now consists of _n_ days. Each day pupils study exactly one of _m_ subjects, besides, each subject is studied for no more than one day. After the lessons of the _i_-th subject pupils get the home task that contains no less than _a__i_ and no more than _b__i_ exercises. Besides, each subject has a special attribute, the complexity (_c__i_). A school can make its own timetable, considering the following conditions are satisfied: the timetable should contain the subjects in the order of the complexity's strict increasing; each day, except for the first one, the task should contain either _k_ times more exercises, or more by _k_ compared to the previous day (more formally: let's call the number of home task exercises in the _i_-th day as _x__i_, then for each _i_ (1u2009<u2009_i_u2009≀u2009_n_): either _x__i_u2009=u2009_k_u2009+u2009_x__i_u2009-u20091 or _x__i_u2009=u2009_k_Β·_x__i_u2009-u20091 must be true); the total number of exercises in all home tasks should be maximal possible. All limitations are separately set for each school. It turned out that in many cases _a__i_ and _b__i_ reach 1016 (however, as the Berland Minister of Education is famous for his love to half-measures, the value of _b__i_u2009-u2009_a__i_ doesn't exceed 100). That also happened in the Berland School β„–256. Nevertheless, you as the school's principal still have to work out the timetable for the next academic year... Input The first line contains three integers _n_, _m_, _k_ (1u2009≀u2009_n_u2009≀u2009_m_u2009≀u200950, 1u2009≀u2009_k_u2009≀u2009100) which represent the number of days in an academic year, the number of subjects and the _k_ parameter correspondingly. Each of the following _m_ lines contains the description of a subject as three integers _a__i_, _b__i_, _c__i_ (1u2009≀u2009_a__i_u2009≀u2009_b__i_u2009≀u20091016, _b__i_u2009-u2009_a__i_u2009≀u2009100, 1u2009≀u2009_c__i_u2009≀u2009100) β€” two limitations to the number of exercises on the _i_-th subject and the complexity of the _i_-th subject, correspondingly. Distinct subjects can have the same complexity. The subjects are numbered with integers from 1 to _m_. Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is preferred to use the cin stream or the %I64d specificator. Output If no valid solution exists, print the single word "NO" (without the quotes). Otherwise, the first line should contain the word "YES" (without the quotes) and the next _n_ lines should contain any timetable that satisfies all the conditions. The _i_u2009+u20091-th line should contain two positive integers: the number of the subject to study on the _i_-th day and the number of home task exercises given for this subject. The timetable should contain exactly _n_ subjects. Examples Input 4 5 2 1 10 1 1 10 2 1 10 3 1 20 4 1 100 5 Input 3 4 3 1 3 1 2 4 4 2 3 3 2 2 2
2,000
false
false
false
true
false
false
false
false
false
false
9,409
730H
Polycarp is a beginner programmer. He is studying how to use a command line. Polycarp faced the following problem. There are _n_ files in a directory and he needs to delete some of them. Polycarp wants to run a single delete command with filename pattern as an argument. All the files to be deleted should match the pattern and all other files shouldn't match the pattern. Polycarp doesn't know about an asterisk '*', the only special character he knows is a question mark '?' which matches any single character. All other characters in the pattern match themselves only. Formally, a pattern matches a filename if and only if they have equal lengths and all characters in the corresponding positions are equal except when the character in the pattern is '?', in which case the corresponding filename character does not matter. For example, the filename pattern "a?ba?": matches filenames "aabaa", "abba.", "a.ba9" and "a.ba."; does not match filenames "aaba", "abaab", "aabaaa" and "aabaa.". Help Polycarp find a pattern which matches files to be deleted and only them or report if there is no such pattern. Input The first line of the input contains two integers _n_ and _m_ (1u2009≀u2009_m_u2009≀u2009_n_u2009≀u2009100) β€” the total number of files and the number of files to be deleted. The following _n_ lines contain filenames, single filename per line. All filenames are non-empty strings containing only lowercase English letters, digits and dots ('.'). The length of each filename doesn't exceed 100. It is guaranteed that all filenames are distinct. The last line of the input contains _m_ distinct integer numbers in ascending order _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009≀u2009_a__i_u2009≀u2009_n_) β€” indices of files to be deleted. All files are indexed from 1 to _n_ in order of their appearance in the input. Output If the required pattern exists, print "Yes" in the first line of the output. The second line should contain the required pattern. If there are multiple solutions, print any of them. If the required pattern doesn't exist, print the only line containing "No". Examples Input 5 3 test tezt test. .est tes. 1 4 5 Input 6 3 .svn .git .... ... .. . 1 2 3
1,300
false
false
true
false
false
true
false
false
false
false
6,881
777A
Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball. Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.). Let's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly _n_ movements were made by the operator and the ball was under shell _x_ at the end. Now he wonders, what was the initial position of the ball? Input The first line of the input contains an integer _n_ (1u2009≀u2009_n_u2009≀u20092Β·109)xa0β€” the number of movements made by the operator. The second line contains a single integer _x_ (0u2009≀u2009_x_u2009≀u20092)xa0β€” the index of the shell where the ball was found after _n_ movements. Output Print one integer from 0 to 2xa0β€” the index of the shell where the ball was initially placed. Note In the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements. 1. During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell. 2. During the second move operator swapped the middle shell and the right one. The ball is still under the left shell. 3. During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle. 4. Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell.
1,000
true
false
true
false
false
true
false
false
false
false
6,661
1365B
Ashish has $$$n$$$ elements arranged in a line. These elements are represented by two integers $$$a_i$$$xa0β€” the value of the element and $$$b_i$$$xa0β€” the type of the element (there are only two possible types: $$$0$$$ and $$$1$$$). He wants to sort the elements in non-decreasing values of $$$a_i$$$. He can perform the following operation any number of times: Select any two elements $$$i$$$ and $$$j$$$ such that $$$b_i e b_j$$$ and swap them. That is, he can only swap two elements of different types in one move. Tell him if he can sort the elements in non-decreasing values of $$$a_i$$$ after performing any number of operations. Input The first line contains one integer $$$t$$$ $$$(1 le t le 100)$$$xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ $$$(1 le n le 500)$$$xa0β€” the size of the arrays. The second line contains $$$n$$$ integers $$$a_i$$$ $$$(1 le a_i le 10^5)$$$ xa0β€” the value of the $$$i$$$-th element. The third line containts $$$n$$$ integers $$$b_i$$$ $$$(b_i in {0, 1})$$$ xa0β€” the type of the $$$i$$$-th element. Output For each test case, print "Yes" or "No" (without quotes) depending on whether it is possible to sort elements in non-decreasing order of their value. You may print each letter in any case (upper or lower). Example Input 5 4 10 20 20 30 0 1 0 1 3 3 1 2 0 1 1 4 2 2 4 8 1 1 1 1 3 5 15 4 0 0 0 4 20 10 100 50 1 0 0 1 Note For the first case: The elements are already in sorted order. For the second case: Ashish may first swap elements at positions $$$1$$$ and $$$2$$$, then swap elements at positions $$$2$$$ and $$$3$$$. For the third case: The elements are already in sorted order. For the fourth case: No swap operations may be performed as there is no pair of elements $$$i$$$ and $$$j$$$ such that $$$b_i e b_j$$$. The elements cannot be sorted. For the fifth case: Ashish may swap elements at positions $$$3$$$ and $$$4$$$, then elements at positions $$$1$$$ and $$$2$$$.
1,300
false
false
true
false
false
true
false
false
false
false
3,856
856A
Masha and Grisha like studying sets of positive integers. One day Grisha has written a set _A_ containing _n_ different integers _a__i_ on a blackboard. Now he asks Masha to create a set _B_ containing _n_ different integers _b__j_ such that all _n_2 integers that can be obtained by summing up _a__i_ and _b__j_ for all possible pairs of _i_ and _j_ are different. Both Masha and Grisha don't like big numbers, so all numbers in _A_ are from 1 to 106, and all numbers in _B_ must also be in the same range. Help Masha to create the set _B_ that satisfies Grisha's requirement. Input Input data contains multiple test cases. The first line contains an integer _t_xa0β€” the number of test cases (1u2009≀u2009_t_u2009≀u2009100). Each test case is described in the following way: the first line of the description contains one integer _n_xa0β€” the number of elements in _A_ (1u2009≀u2009_n_u2009≀u2009100). The second line contains _n_ integers _a__i_xa0β€” the elements of _A_ (1u2009≀u2009_a__i_u2009≀u2009106). Output For each test first print the answer: NO, if Masha's task is impossible to solve, there is no way to create the required set _B_. YES, if there is the way to create the required set. In this case the second line must contain _n_ different positive integers _b__j_xa0β€” elements of _B_ (1u2009≀u2009_b__j_u2009≀u2009106). If there are several possible sets, output any of them. Example Output YES 1 2 3 YES 1 YES 1 2
1,600
false
false
false
false
false
true
true
false
false
false
6,320
1860A
A bracket sequence is a string consisting of characters '(' and/or ')'. A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters '1' and '+' between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (they can be transformed into "(1)+(1)" and "((1+1)+1)", respectively); bracket sequences ")(", "(" and ")" are not regular. You are given a bracket sequence $$$s$$$; let's define its length as $$$n$$$. Your task is to find a regular bracket sequence $$$t$$$ of length $$$2n$$$ such that $$$s$$$ does not occur in $$$t$$$ as a contiguous substring, or report that there is no such sequence. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The only line of each test case contains a string $$$s$$$ ($$$2 le s le 50$$$), consisting of characters "(" and/or ")". Output For each test case, print the answer to it. If there is no required regular bracket sequence, print NO in a separate line. Otherwise, print YES in the first line, and the required regular bracket sequence $$$t$$$ itself in the second line. If there are multiple answers β€” you may print any of them. Example Output YES (()) YES ()()() NO YES ()(()())
900
false
false
false
false
false
true
false
false
false
false
1,125
1942F
Farmer John has an array $$$a$$$ of length $$$n$$$. He also has a function $$$f$$$ with the following recurrence: $$$f(1) = sqrt{a_1}$$$; For all $$$i > 1$$$, $$$f(i) = sqrt{f(i-1)+a_i}$$$. Note that $$$f(i)$$$ is not necessarily an integer. He plans to do $$$q$$$ updates to the array. Each update, he gives you two integers $$$k$$$ and $$$x$$$ and he wants you to set $$$a_k = x$$$. After each update, he wants to know $$$lfloor f(n) floor$$$, where $$$lfloor t floor$$$ denotes the value of $$$t$$$ rounded down to the nearest integer. Input The first line contains $$$n$$$ and $$$q$$$ ($$$1 leq n, q leq 2 cdot 10^5$$$), the length of $$$a$$$ and the number of updates he will perform. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^{18}$$$). The next $$$q$$$ lines each contain two integers $$$k$$$ and $$$x$$$ ($$$1 leq k leq n$$$, $$$0 leq x leq 10^{18}$$$), the index of the update and the element he will replace $$$a_k$$$ with. Output For each update, output an integer, $$$lfloor f(n) floor$$$, on a new line. Examples Input 5 6 0 14 0 7 6 1 4 1 3 2 15 4 1 5 2 5 8 Input 15 10 3364 1623 5435 7 6232 245 7903 3880 9738 577 4598 1868 1112 8066 199 14 4284 14 8066 6 92 6 245 2 925 2 1623 5 176 5 6232 3 1157 3 5435 Output 16 17 16 17 16 17 16 17 16 17 Input 2 2 386056082462833225 923951085408043421 1 386056082462833225 1 386056082462833224 Output 961223744 961223743 Input 13 10 31487697732100 446330174221392699 283918145228010533 619870471872432389 11918456891794188 247842810542459080 140542974216802552 698742782599365547 533363381213535498 92488084424940128 401887157851719898 128798321287952855 137376848358184069 3 283918145228010532 3 283918145228010533 1 2183728930312 13 1000000000000000000 10 1000000000000000000 9 1000000000000000000 8 1000000000000000000 7 1000000000000000000 6 1000000000000000000 5 1000000000000000000 Output 370643829 370643830 370643829 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 Note In the first test case, the array after the first update is $$$[4, 14, 0, 7, 6]$$$. The values of $$$f$$$ are: $$$f(1)=2$$$; $$$f(2)=4$$$; $$$f(3)=2$$$; $$$f(4)=3$$$; $$$f(5)=3$$$. Since $$$lfloor f(5) floor = 3$$$, we output $$$3$$$. The array after the second update is $$$[3, 14, 0, 7, 6]$$$. The values of $$$f$$$, rounded to $$$6$$$ decimal places, are: $$$f(1)approx 1.732051$$$; $$$f(2)approx 3.966365$$$; $$$f(3)approx 1.991573$$$; $$$f(4)approx 2.998595$$$; $$$f(5)approx 2.999766$$$. Since $$$lfloor f(5) floor = 2$$$, we output $$$2$$$.
2,700
true
false
true
false
true
false
true
false
false
false
626
1955E
A binary string $$$s$$$ of length $$$n$$$ is given. A binary string is a string consisting only of the characters '1' and '0'. You can choose an integer $$$k$$$ ($$$1 le k le n$$$) and then apply the following operation any number of times: choose $$$k$$$ consecutive characters of the string and invert them, i.e., replace all '0' with '1' and vice versa. Using these operations, you need to make all the characters in the string equal to '1'. For example, if $$$n=5$$$, $$$s=00100$$$, you can choose $$$k=3$$$ and proceed as follows: choose the substring from the $$$1$$$-st to the $$$3$$$-rd character and obtain $$$s=color{blue}{110}00$$$; choose the substring from the $$$3$$$-rd to the $$$5$$$-th character and obtain $$$s=11color{blue}{111}$$$; Find the maximum value of $$$k$$$ for which it is possible to make all the characters in the string equal to '1' using the described operations. Note that the number of operations required to achieve this is not important. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 5000$$$)xa0β€” the length of the string $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of the characters '1' and '0'. It is guaranteed that the sum of the values $$$n^2$$$ over all test cases in the test does not exceed $$$25 cdot 10^6$$$. Output For each test case, output the maximum integer $$$k$$$ ($$$1 le k le n$$$) for which it is possible to obtain a string $$$s$$$ consisting only of the characters '1' using the described operations. Example Input 5 5 00100 5 01000 7 1011101 3 000 2 10
1,700
false
true
true
false
false
false
true
false
true
false
544
1416E
One day, BThero decided to play around with arrays and came up with the following problem: You are given an array $$$a$$$, which consists of $$$n$$$ positive integers. The array is numerated $$$1$$$ through $$$n$$$. You execute the following procedure exactly once: You create a new array $$$b$$$ which consists of $$$2n$$$ positive integers, where for each $$$1 le i le n$$$ the condition $$$b_{2i-1}+b_{2i} = a_i$$$ holds. For example, for the array $$$a = [6, 8, 2]$$$ you can create $$$b = [2, 4, 4, 4, 1, 1]$$$. You merge consecutive equal numbers in $$$b$$$. For example, $$$b = [2, 4, 4, 4, 1, 1]$$$ becomes $$$b = [2, 4, 1]$$$. Find and print the minimum possible value of $$$b$$$ (size of $$$b$$$) which can be achieved at the end of the procedure. It can be shown that under the given constraints there is at least one way to construct $$$b$$$. Input The first line of the input file contains a single integer $$$T$$$ ($$$1 le T le 5 cdot 10^5$$$) denoting the number of test cases. The description of $$$T$$$ test cases follows. The first line of each test contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$). The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$2 le a_i le 10^9$$$). It is guaranteed that $$$sum{n}$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, print a single line containing one integer β€” the minimum possible value of $$$b$$$. Example Input 3 3 6 8 2 1 4 3 5 6 6
3,200
false
true
false
true
true
false
false
true
false
false
3,598
1355D
Petya and Vasya are competing with each other in a new interesting game as they always do. At the beginning of the game Petya has to come up with an array of $$$N$$$ positive integers. Sum of all elements in his array should be equal to $$$S$$$. Then Petya has to select an integer $$$K$$$ such that $$$0 leq K leq S$$$. In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either $$$K$$$ or $$$S - K$$$. Otherwise Vasya loses. You are given integers $$$N$$$ and $$$S$$$. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that. Input The first line contains two integers $$$N$$$ and $$$S$$$ ($$$1 leq N leq S leq 10^{6}$$$)xa0β€” the required length of the array and the required sum of its elements. Output If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain $$$N$$$ positive integers with sum equal to $$$S$$$. In the third line print $$$K$$$. If there are many correct answers, you can print any of them. If Petya can't win, print "NO" (without quotes). You can print each letter in any register (lowercase or uppercase).
1,400
true
false
false
false
false
true
false
false
false
false
3,928
863E
Luba needs your help again! Luba has _n_ TV sets. She knows that _i_-th TV set will be working from moment of time _l__i_ till moment _r__i_, inclusive. Luba wants to switch off one of TV sets in order to free the socket. Let's call some TV set redundant if after switching it off the number of integer moments of time when at least one of TV sets is working won't decrease. Luba will be very upset if she has to switch off a non-redundant TV set. Help Luba by telling her the index of some redundant TV set. If there is no any, print -1. Input The first line contains one integer number _n_xa0(1u2009≀u2009_n_u2009≀u20092Β·105) β€” the number of TV sets. Then _n_ lines follow, each of them containing two integer numbers _l__i_,u2009_r__i_xa0(0u2009≀u2009_l__i_u2009≀u2009_r__i_u2009≀u2009109) denoting the working time of _i_-th TV set. Output If there is no any redundant TV set, print -1. Otherwise print the index of any redundant TV set (TV sets are indexed from 1 to _n_). If there are multiple answers, print any of them. Note Consider the first sample. Initially all integer moments of time such that at least one TV set is working are from the segment [1;7]. It's easy to see that this segment won't change if we switch off the first TV set (or the second one). Note that in the fourth sample you can switch off the second TV set, since even without it all integer moments such that any of the TV sets is working denote the segment [1;4].
2,000
false
false
false
false
true
false
false
false
true
false
6,290
218B
Lolek and Bolek are about to travel abroad by plane. The local airport has a special "Choose Your Plane" offer. The offer's conditions are as follows: it is up to a passenger to choose a plane to fly on; if the chosen plane has _x_ (_x_u2009>u20090) empty seats at the given moment, then the ticket for such a plane costs _x_ zlotys (units of Polish currency). The only ticket office of the airport already has a queue of _n_ passengers in front of it. Lolek and Bolek have not stood in the queue yet, but they are already wondering what is the maximum and the minimum number of zlotys the airport administration can earn if all _n_ passengers buy tickets according to the conditions of this offer? The passengers buy tickets in turn, the first person in the queue goes first, then goes the second one, and so on up to _n_-th person. Input The first line contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u20091000) β€” the number of passengers in the queue and the number of planes in the airport, correspondingly. The next line contains _m_ integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009≀u2009_a__i_u2009≀u20091000) β€” _a__i_ stands for the number of empty seats in the _i_-th plane before the ticket office starts selling tickets. The numbers in the lines are separated by a space. It is guaranteed that there are at least _n_ empty seats in total. Output Print two integers β€” the maximum and the minimum number of zlotys that the airport administration can earn, correspondingly. Note In the first test sample the number of passengers is equal to the number of empty seats, so regardless of the way the planes are chosen, the administration will earn the same sum. In the second sample the sum is maximized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person β€” to the 2-nd plane, the 3-rd person β€” to the 3-rd plane, the 4-th person β€” to the 1-st plane. The sum is minimized if the 1-st person in the queue buys a ticket to the 1-st plane, the 2-nd person β€” to the 1-st plane, the 3-rd person β€” to the 2-nd plane, the 4-th person β€” to the 2-nd plane.
1,100
false
false
true
false
false
false
false
false
false
false
8,965
670A
Problem - 670A - 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 constructive algorithms greedy math *900 No tag edit access β†’ Contest materials Editorial") . But Martians have the same weeks as earthlingsxa0β€” 5 work days and then 2 days off. Your task is to determine the minimum possible and the maximum possible number of days off per year on Mars. Input The first line of the input contains a positive integer _n_ (1u2009≀u2009_n_u2009≀u20091u2009000u2009000)xa0β€” the number of days in a year on Mars. Output Print two integersxa0β€” the minimum possible and the maximum possible number of days off per year on Mars. Examples Input 14 Output 4 4 Input 2 Output 0 2 Note In the first sample there are 14 days in a year on Mars, and therefore independently of the day a year starts with there will be exactly 4 days off . In the second sample there are only 2 days in a year on Mars, and they can both be either work days or days off.
900
true
true
false
false
false
true
true
false
false
false
7,147
1016B
You are given two strings $$$s$$$ and $$$t$$$, both consisting only of lowercase Latin letters. The substring $$$s[l..r]$$$ is the string which is obtained by taking characters $$$s_l, s_{l + 1}, dots, s_r$$$ without changing the order. Each of the occurrences of string $$$a$$$ in a string $$$b$$$ is a position $$$i$$$ ($$$1 le i le b - a + 1$$$) such that $$$b[i..i + a - 1] = a$$$ ($$$a$$$ is the length of string $$$a$$$). You are asked $$$q$$$ queries: for the $$$i$$$-th query you are required to calculate the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$. Input The first line contains three integer numbers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 le n, m le 10^3$$$, $$$1 le q le 10^5$$$) β€” the length of string $$$s$$$, the length of string $$$t$$$ and the number of queries, respectively. The second line is a string $$$s$$$ ($$$s = n$$$), consisting only of lowercase Latin letters. The third line is a string $$$t$$$ ($$$t = m$$$), consisting only of lowercase Latin letters. Each of the next $$$q$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) β€” the arguments for the $$$i$$$-th query. Output Print $$$q$$$ lines β€” the $$$i$$$-th line should contain the answer to the $$$i$$$-th query, that is the number of occurrences of string $$$t$$$ in a substring $$$s[l_i..r_i]$$$. Examples Input 10 3 4 codeforces for 1 3 3 10 5 6 5 7 Input 15 2 3 abacabadabacaba ba 1 15 3 4 2 14 Note In the first example the queries are substrings: "cod", "deforces", "fo" and "for", respectively.
1,300
false
false
true
false
false
false
true
false
false
false
5,611
1765K
You are given a square grid with $$$n$$$ rows and $$$n$$$ columns, where each cell has a non-negative integer written in it. There is a chip initially placed at the top left cell (the cell with coordinates $$$(1, 1)$$$). You need to move the chip to the bottom right cell (the cell with coordinates $$$(n, n)$$$). In one step, you can move the chip to the neighboring cell, but: 1. you can move only right or down. In other words, if the current cell is $$$(x, y)$$$, you can move either to $$$(x, y + 1)$$$ or to $$$(x + 1, y)$$$. There are two special cases: if the chip is in the last column (cell $$$(x, n)$$$) and you're moving right, you'll teleport to the first column (to the cell $$$(x, 1)$$$); if the chip is in the last row (cell $$$(n, y)$$$) and you're moving down, you'll teleport to the first row (to the cell $$$(1, y)$$$). 2. you cannot visit the same cell twice. The starting cell is counted visited from the beginning (so you cannot enter it again), and you can't leave the finishing cell once you visit it. Your total score is counted as the sum of numbers in all cells you have visited. What is the maximum possible score you can achieve? Input The first line contains the single integer $$$n$$$ ($$$2 le n le 200$$$)xa0β€” the number of rows and columns in the grid. Next $$$n$$$ lines contains the description of each row of the grid. The $$$i$$$-th line contains $$$n$$$ integers $$$a_{i, 1}, a_{i, 2}, dots, a_{i, n}$$$ ($$$0 le a_{i, j} le 10^9$$$) where $$$a_{i, j}$$$ is the number written in the cell $$$(i, j)$$$. Output Print one integerxa0β€” the maximum possible score you can achieve. Examples Input 3 10 10 10 10 0 10 10 10 10
1,500
true
true
false
false
false
false
false
false
false
false
1,707
1368D
Gottfried learned about binary number representation. He then came up with this task and presented it to you. You are given a collection of $$$n$$$ non-negative integers $$$a_1, ldots, a_n$$$. You are allowed to perform the following operation: choose two distinct indices $$$1 leq i, j leq n$$$. If before the operation $$$a_i = x$$$, $$$a_j = y$$$, then after the operation $$$a_i = x~mathsf{AND}~y$$$, $$$a_j = x~mathsf{OR}~y$$$, where $$$mathsf{AND}$$$ and $$$mathsf{OR}$$$ are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero). After all operations are done, compute $$$sum_{i=1}^n a_i^2$$$xa0β€” the sum of squares of all $$$a_i$$$. What is the largest sum of squares you can achieve? Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$0 leq a_i < 2^{20}$$$). Output Print a single integerxa0β€” the largest possible sum of squares that can be achieved after several (possibly zero) operations. Note In the first sample no operation can be made, thus the answer is $$$123^2$$$. In the second sample we can obtain the collection $$$1, 1, 7$$$, and $$$1^2 + 1^2 + 7^2 = 51$$$. If $$$x$$$ and $$$y$$$ are represented in binary with equal number of bits (possibly with leading zeros), then each bit of $$$x~mathsf{AND}~y$$$ is set to $$$1$$$ if and only if both corresponding bits of $$$x$$$ and $$$y$$$ are set to $$$1$$$. Similarly, each bit of $$$x~mathsf{OR}~y$$$ is set to $$$1$$$ if and only if at least one of the corresponding bits of $$$x$$$ and $$$y$$$ are set to $$$1$$$. For example, $$$x = 3$$$ and $$$y = 5$$$ are represented as $$$011_2$$$ and $$$101_2$$$ (highest bit first). Then, $$$x~mathsf{AND}~y = 001_2 = 1$$$, and $$$x~mathsf{OR}~y = 111_2 = 7$$$.
1,700
true
true
false
false
false
false
false
false
false
false
3,833
47C
Vasya trains to compose crossword puzzles. He can only compose crosswords of a very simplΠ΅ type so far. All of them consist of exactly six words; the words can be read only from top to bottom vertically and from the left to the right horizontally. The words are arranged in the form of a rectangular "eight" or infinity sign, not necessarily symmetrical. The top-left corner of the crossword coincides with the top-left corner of the rectangle. The same thing is correct for the right-bottom corners. The crossword can't degrade, i.e. it always has exactly four blank areas, two of which are surrounded by letters. Look into the output for the samples for clarification. Help Vasya β€” compose a crossword of the described type using the given six words. It is allowed to use the words in any order. Input Six lines contain the given words. Every word consists of no more than 30 and no less than 3 uppercase Latin letters. Output If it is impossible to solve the problem, print Impossible. Otherwise, print the sought crossword. All the empty squares should be marked as dots. If there can be several solutions to that problem, print the lexicographically minimum one. I.e. the solution where the first line is less than the first line of other solutions should be printed. If the two lines are equal, compare the second lines and so on. The lexicographical comparison of lines is realized by the < operator in the modern programming languages. Examples Input NOD BAA YARD AIRWAY NEWTON BURN Output BAA... U.I... R.R... NEWTON ..A..O ..YARD Input AAA AAA AAAAA AAA AAA AAAAA Output AAA.. A.A.. AAAAA ..A.A ..AAA Input PTC JYNYFDSGI ZGPPC IXEJNDOP JJFS SSXXQOFGJUZ Output JJFS.... Y..S.... N..X.... Y..X.... F..Q.... D..O.... S..F.... G..G.... IXEJNDOP ...U...T ...ZGPPC
2,000
false
false
true
false
false
false
false
false
false
false
9,740
648B
Statement is not available on English language Вася ΠΊΡƒΠΏΠΈΠ» стол, Ρƒ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ³ΠΎ _n_ Π½ΠΎΠΆΠ΅ΠΊ. КаТдая Π½ΠΎΠΆΠΊΠ° состоит ΠΈΠ· Π΄Π²ΡƒΡ… частСй, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΡΠΎΠ΅Π΄ΠΈΠ½ΡΡŽΡ‚ΡΡ Π΄Ρ€ΡƒΠ³ с Π΄Ρ€ΡƒΠ³ΠΎΠΌ. КаТдая Ρ‡Π°ΡΡ‚ΡŒ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΠΎΠΉ ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠΉ Π΄Π»ΠΈΠ½Ρ‹, Π½ΠΎ гарантируСтся, Ρ‡Ρ‚ΠΎ ΠΈΠ· всСх 2_n_ частСй Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠΎΡΡ‚Π°Π²ΠΈΡ‚ΡŒ _n_ Π½ΠΎΠΆΠ΅ΠΊ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠΉ Π΄Π»ΠΈΠ½Ρ‹. ΠŸΡ€ΠΈ составлСнии Π½ΠΎΠΆΠΊΠΈ Π»ΡŽΠ±Ρ‹Π΅ Π΄Π²Π΅ части ΠΌΠΎΠ³ΡƒΡ‚ Π±Ρ‹Ρ‚ΡŒ соСдинСны Π΄Ρ€ΡƒΠ³ с Π΄Ρ€ΡƒΠ³ΠΎΠΌ. Π˜Π·Π½Π°Ρ‡Π°Π»ΡŒΠ½ΠΎ всС Π½ΠΎΠΆΠΊΠΈ стола Ρ€Π°Π·ΠΎΠ±Ρ€Π°Π½Ρ‹, Π° Π²Π°ΠΌ Π·Π°Π΄Π°Π½Ρ‹ Π΄Π»ΠΈΠ½Ρ‹ 2_n_ частСй Π² ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΠΎΠΌ порядкС. ΠŸΠΎΠΌΠΎΠ³ΠΈΡ‚Π΅ ВасС ΡΠΎΠ±Ρ€Π°Ρ‚ΡŒ всС Π½ΠΎΠΆΠΊΠΈ стола Ρ‚Π°ΠΊ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ всС ΠΎΠ½ΠΈ Π±Ρ‹Π»ΠΈ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠΉ Π΄Π»ΠΈΠ½Ρ‹, Ρ€Π°Π·Π±ΠΈΠ² Π·Π°Π΄Π°Π½Π½Ρ‹Π΅ 2_n_ части Π½Π° ΠΏΠ°Ρ€Ρ‹ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Ρ‹ΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ. КаТдая Π½ΠΎΠΆΠΊΠ° ΠΎΠ±ΡΠ·Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎ Π΄ΠΎΠ»ΠΆΠ½Π° Π±Ρ‹Ρ‚ΡŒ составлСна Ρ€ΠΎΠ²Π½ΠΎ ΠΈΠ· Π΄Π²ΡƒΡ… частСй, Π½Π΅ Ρ€Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ ΠΊΠ°ΠΊ Π½ΠΎΠΆΠΊΡƒ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎΠ΄Π½Ρƒ Ρ‡Π°ΡΡ‚ΡŒ. Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π·Π°Π΄Π°Π½ΠΎ число _n_ (1u2009≀u2009_n_u2009≀u20091000)xa0β€” количСство Π½ΠΎΠΆΠ΅ΠΊ Ρƒ стола, ΠΊΡƒΠΏΠ»Π΅Π½Π½ΠΎΠ³ΠΎ ВасСй. Π’ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΉ строкС слСдуСт ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ ΠΈΠ· 2_n_ Ρ†Π΅Π»Ρ‹Ρ… ΠΏΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Ρ… чисСл _a_1,u2009_a_2,u2009...,u2009_a_2_n_ (1u2009≀u2009_a__i_u2009≀u2009100u2009000)xa0β€” Π΄Π»ΠΈΠ½Ρ‹ частСй Π½ΠΎΠΆΠ΅ΠΊ стола Π² ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½ΠΎΠΌ порядкС. Π’Ρ‹Ρ…ΠΎΠ΄Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ Π’Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ _n_ строк ΠΏΠΎ Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа Π² ΠΊΠ°ΠΆΠ΄ΠΎΠΉxa0β€” Π΄Π»ΠΈΠ½Ρ‹ частСй Π½ΠΎΠΆΠ΅ΠΊ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π½Π°Π΄ΠΎ ΡΠΎΠ΅Π΄ΠΈΠ½ΠΈΡ‚ΡŒ Π΄Ρ€ΡƒΠ³ с Π΄Ρ€ΡƒΠ³ΠΎΠΌ. ГарантируСтся, Ρ‡Ρ‚ΠΎ всСгда Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡΠΎΠ±Ρ€Π°Ρ‚ΡŒ _n_ Π½ΠΎΠΆΠ΅ΠΊ ΠΎΠ΄ΠΈΠ½Π°ΠΊΠΎΠ²ΠΎΠΉ Π΄Π»ΠΈΠ½Ρ‹. Если ΠΎΡ‚Π²Π΅Ρ‚ΠΎΠ² нСсколько, Ρ€Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ вывСсти любой ΠΈΠ· Π½ΠΈΡ…. ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹ Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ 3 1 3 2 4 5 3 Π’Ρ…ΠΎΠ΄Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ 3 1 1 1 2 2 2
800
false
false
false
false
false
true
false
false
true
false
7,226
1C
Problem - 1C - Codeforces =============== xa0 ]( "85") polygon, the size and the number of angles could vary from one circus to another. In each corner of the arena there was a special pillar, and the rope strung between the pillars marked the arena edges. Recently the scientists from Berland have discovered the remains of the ancient circus arena. They found only three pillars, the others were destroyed by the time. You are given the coordinates of these three pillars. Find out what is the smallest area that the arena could have. Input The input file consists of three lines, each of them contains a pair of numbers –– coordinates of the pillar. Any coordinate doesn't exceed 1000 by absolute value, and is given with at most six digits after decimal point. Output Output the smallest possible area of the ancient arena. This number should be accurate to at least 6 digits after the decimal point. It's guaranteed that the number of angles in the optimal polygon is not larger than 100. Examples Input 0.000000 0.000000 1.000000 1.000000 0.000000 1.000000 Output 1.00000000
2,100
true
false
false
false
false
false
false
false
false
false
9,982
1729G
You are given two non-empty strings $$$s$$$ and $$$t$$$, consisting of Latin letters. In one move, you can choose an occurrence of the string $$$t$$$ in the string $$$s$$$ and replace it with dots. Your task is to remove all occurrences of the string $$$t$$$ in the string $$$s$$$ in the minimum number of moves, and also calculate how many different sequences of moves of the minimum length exist. Two sequences of moves are considered different if the sets of indices at which the removed occurrences of the string $$$t$$$ in $$$s$$$ begin differ. For example, the sets $$${1, 2, 3}$$$ and $$${1, 2, 4}$$$ are considered different, the sets $$${2, 4, 6}$$$ and $$${2, 6}$$$ β€” too, but sets $$${3, 5}$$$ and $$${5, 3}$$$ β€” not. For example, let the string $$$s =$$$ "abababacababa" and the string $$$t =$$$ "aba". We can remove all occurrences of the string $$$t$$$ in $$$2$$$ moves by cutting out the occurrences of the string $$$t$$$ at the $$$3$$$th and $$$9$$$th positions. In this case, the string $$$s$$$ is an example of the form "ab...bac...ba". It is also possible to cut occurrences of the string $$$t$$$ at the $$$3$$$th and $$$11$$$th positions. There are two different sequences of minimum length moves. Since the answer can be large, output it modulo $$$10^9 + 7$$$. Input The first line of the input contains a single integer $$$q$$$ ($$$1 le q le 50$$$)xa0β€” the number of test cases. The descriptions of the sets follow. The first line of each set contains a non-empty string $$$s$$$ ($$$1 le s le 500$$$) consisting of lowercase Latin letters. The second line of each set contains a non-empty string $$$t$$$ ($$$1 le t le 500$$$) consisting of lowercase Latin letters. It is guaranteed that the sum of string lengths $$$s$$$ over all test cases does not exceed $$$500$$$. Similarly, it is guaranteed that the sum of string lengths $$$t$$$ over all test cases does not exceed $$$500$$$. Note The first test case is explained in the statement. In the second case, it is enough to cut any of the four occurrences. In the third case, string $$$s$$$ is the concatenation of two strings $$$t =$$$ "xyz", so there is a unique optimal sequence of $$$2$$$ moves. In the fourth and sixth cases, the string $$$s$$$ initially contains no occurrences of the string $$$t$$$. In the fifth case, the string $$$s$$$ contains exactly one occurrence of the string $$$t$$$.
2,100
false
false
false
true
false
false
false
false
false
false
1,912
1907F
Given an array of integers $$$a_1, a_2, ldots, a_n$$$. You can make two types of operations with this array: Shift: move the last element of array to the first place, and shift all other elements to the right, so you get the array $$$a_n, a_1, a_2, ldots, a_{n-1}$$$. Reverse: reverse the whole array, so you get the array $$$a_n, a_{n-1}, ldots, a_1$$$. Your task is to sort the array in non-decreasing order using the minimal number of operations, or say that it is impossible. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Descriptions of test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1le n le 10^5$$$)xa0β€” size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” elements of the array. It is guaranteed that sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case output the number $$$k$$$, the minimal number of operations you need to sort the array. If it is impossible to sort the array using these operations, output $$$-1$$$. Example Input 11 5 3 2 1 5 4 5 1 1 2 1 1 4 3 7 10 5 5 1 2 3 4 5 2 5 1 3 3 4 1 5 4 1 3 4 4 3 5 1 1 4 2 5 5 4 5 2 2 1 1 2 2 5 5 Output 3 2 -1 0 1 1 3 1 2 2 0 Note In the first test case of the example, to sort the array [$$$3, 2, 1, 5, 4$$$] you need to perform $$$3$$$ operations: Shift to obtain the array [$$$4, 3, 2, 1, 5$$$]; Shift to obtain the array [$$$5, 4, 3, 2, 1$$$]; Reverse to obtain the array [$$$1, 2, 3, 4, 5$$$]. In the third test case of the example, it can be shown that it is impossible to sort the array using the given operations. In the seventh test case of the example, to sort the array [$$$4, 1, 3, 4, 4$$$] you need to perform $$$3$$$ operations: Reverse to obtain the array [$$$4, 4, 3, 1, 4$$$]; Shift to obtain the array [$$$4, 4, 4, 3, 1$$$]; Reverse to obtain the array [$$$1, 3, 4, 4, 4$$$].
1,800
false
true
false
false
false
false
false
false
true
false
859
1907E
Given a non-negative integer number $$$n$$$ ($$$n ge 0$$$). Let's say a triple of non-negative integers $$$(a, b, c)$$$ is good if $$$a + b + c = n$$$, and $$$digsum(a) + digsum(b) + digsum(c) = digsum(n)$$$, where $$$digsum(x)$$$ is the sum of digits of number $$$x$$$. For example, if $$$n = 26$$$, then the pair $$$(4, 12, 10)$$$ is good, because $$$4 + 12 + 10 = 26$$$, and $$$(4) + (1 + 2) + (1 + 0) = (2 + 6)$$$. Your task is to find the number of good triples for the given number $$$n$$$. The order of the numbers in a triple matters. For example, the triples $$$(4, 12, 10)$$$ and $$$(10, 12, 4)$$$ are two different triples. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Descriptions of test cases follow. The first and only line of the test case contains one integer $$$n$$$ ($$$0 le n le 10^7$$$). Output For each test case output one integer, the number of good triples for the given integer $$$n$$$. Order of integers in a triple matters. Example Input 12 11 0 1 2 3 4 5 3141 999 2718 9999999 10000000 Output 9 1 3 6 10 15 21 1350 166375 29160 1522435234375 3 Note In the first example, the good triples are $$$(0, 0, 11)$$$, $$$(0, 1, 10)$$$, $$$(0, 10, 1)$$$, $$$(0, 11, 0)$$$, $$$(1, 0, 10)$$$, $$$(1, 10, 0)$$$, $$$(10, 0, 1)$$$, $$$(10, 1, 0)$$$, $$$(11, 0, 0)$$$. In the second example, there is only one good triple $$$(0, 0, 0)$$$.
1,600
false
false
false
false
false
false
true
false
false
false
860
222B
The Free Meteor Association (FMA) has got a problem: as meteors are moving, the Universal Cosmic Descriptive Humorous Program (UCDHP) needs to add a special module that would analyze this movement. UCDHP stores some secret information about meteors as an _n_u2009Γ—u2009_m_ table with integers in its cells. The order of meteors in the Universe is changing. That's why the main UCDHP module receives the following queries: The query to swap two table rows; The query to swap two table columns; The query to obtain a secret number in a particular table cell. As the main UCDHP module is critical, writing the functional of working with the table has been commissioned to you. Input The first line contains three space-separated integers _n_, _m_ and _k_ (1u2009≀u2009_n_,u2009_m_u2009≀u20091000, 1u2009≀u2009_k_u2009≀u2009500000) β€” the number of table columns and rows and the number of queries, correspondingly. Next _n_ lines contain _m_ space-separated numbers each β€” the initial state of the table. Each number _p_ in the table is an integer and satisfies the inequality 0u2009≀u2009_p_u2009≀u2009106. Next _k_ lines contain queries in the format "_s__i_ _x__i_ _y__i_", where _s__i_ is one of the characters "с", "r" or "g", and _x__i_, _y__i_ are two integers. If _s__i_ = "c", then the current query is the query to swap columns with indexes _x__i_ and _y__i_ (1u2009≀u2009_x_,u2009_y_u2009≀u2009_m_,u2009_x_u2009β‰ u2009_y_); If _s__i_ = "r", then the current query is the query to swap rows with indexes _x__i_ and _y__i_ (1u2009≀u2009_x_,u2009_y_u2009≀u2009_n_,u2009_x_u2009β‰ u2009_y_); If _s__i_ = "g", then the current query is the query to obtain the number that located in the _x__i_-th row and in the _y__i_-th column (1u2009≀u2009_x_u2009≀u2009_n_,u20091u2009≀u2009_y_u2009≀u2009_m_). The table rows are considered to be indexed from top to bottom from 1 to _n_, and the table columns β€” from left to right from 1 to _m_. Output For each query to obtain a number (_s__i_ = "g") print the required number. Print the answers to the queries in the order of the queries in the input. Examples Input 3 3 5 1 2 3 4 5 6 7 8 9 g 3 2 r 3 2 c 2 3 g 2 2 g 3 2 Input 2 3 3 1 2 4 3 1 5 c 2 1 r 1 2 g 1 3 Note Let's see how the table changes in the second test case. After the first operation is fulfilled, the table looks like that: 2 1 4 1 3 5 After the second operation is fulfilled, the table looks like that: 1 3 5 2 1 4 So the answer to the third query (the number located in the first row and in the third column) will be 5.
1,300
false
false
true
false
true
false
false
false
false
false
8,951
1720E
Misha has a square $$$n imes n$$$ matrix, where the number in row $$$i$$$ and column $$$j$$$ is equal to $$$a_{i, j}$$$. Misha wants to modify the matrix to contain exactly $$$k$$$ distinct integers. To achieve this goal, Misha can perform the following operation zero or more times: 1. choose any square submatrix of the matrix (you choose $$$(x_1,y_1)$$$, $$$(x_2,y_2)$$$, such that $$$x_1 leq x_2$$$, $$$y_1 leq y_2$$$, $$$x_2 - x_1 = y_2 - y_1$$$, then submatrix is a set of cells with coordinates $$$(x, y)$$$, such that $$$x_1 leq x leq x_2$$$, $$$y_1 leq y leq y_2$$$), 2. choose an integer $$$k$$$, where $$$1 leq k leq n^2$$$, 3. replace all integers in the submatrix with $$$k$$$. Please find the minimum number of operations that Misha needs to achieve his goal. Input The first input line contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 500, 1 leq k leq n^2$$$) xa0β€” the size of the matrix and the desired amount of distinct elements in the matrix. Then $$$n$$$ lines follows. The $$$i$$$-th of them contains $$$n$$$ integers $$$a_{i, 1}, a_{i, 2}, ldots, a_{i, n}$$$ ($$$1 leq a_{i,j} leq n^2$$$) β€” the elements of the $$$i$$$-th row of the matrix. Output Output one integer β€” the minimum number of operations required. Examples Input 3 4 1 1 1 1 1 2 3 4 5 Input 3 2 2 1 3 2 1 1 3 1 2 Input 3 3 1 1 1 1 1 2 2 2 2 Input 3 2 1 1 1 1 2 1 2 2 2 Note In the first test case the answer is $$$1$$$, because one can change the value in the bottom right corner of the matrix to $$$1$$$. The resulting matrix can be found below: In the second test case the answer is $$$2$$$. First, one can change the entire matrix to contain only $$$1$$$s, and the change the value of any single cell to $$$2$$$. One of the possible resulting matrices is displayed below:
2,700
true
true
true
false
true
true
false
false
false
false
1,960
109A
Problem - 109A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags brute force implementation *1000 No tag edit access β†’ Contest materials β€” the sum of digits of the required lucky number. Output Print on the single line the result β€” the minimum lucky number, whose sum of digits equals _n_. If such number does not exist, print -1. Examples Input 11 Output 47 Input 10 Output -1
1,000
false
false
true
false
false
false
true
false
false
false
9,449
1699B
You are given two even integers $$$n$$$ and $$$m$$$. Your task is to find any binary matrix $$$a$$$ with $$$n$$$ rows and $$$m$$$ columns where every cell $$$(i,j)$$$ has exactly two neighbours with a different value than $$$a_{i,j}$$$. Two cells in the matrix are considered neighbours if and only if they share a side. More formally, the neighbours of cell $$$(x,y)$$$ are: $$$(x-1,y)$$$, $$$(x,y+1)$$$, $$$(x+1,y)$$$ and $$$(x,y-1)$$$. It can be proven that under the given constraints, an answer always exists. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0β€” the number of test cases. The following lines contain the descriptions of the test cases. The only line of each test case contains two even integers $$$n$$$ and $$$m$$$ ($$$2 le n,m le 50$$$)xa0β€” the height and width of the binary matrix, respectively. Output For each test case, print $$$n$$$ lines, each of which contains $$$m$$$ numbers, equal to $$$0$$$ or $$$1$$$xa0β€” any binary matrix which satisfies the constraints described in the statement. It can be proven that under the given constraints, an answer always exists. Example Output 1 0 0 1 0 1 1 0 1 0 0 1 1 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 Note White means $$$0$$$, black means $$$1$$$.
900
false
false
false
false
false
true
false
false
false
false
2,086
1450C1
The only difference between the easy and hard versions is that tokens of type O do not appear in the input of the easy version. Errichto gave Monogon the following challenge in order to intimidate him from taking his top contributor spot on Codeforces. In a Tic-Tac-Toe grid, there are $$$n$$$ rows and $$$n$$$ columns. Each cell of the grid is either empty or contains a token. There are two types of tokens: X and O. If there exist three tokens of the same type consecutive in a row or column, it is a winning configuration. Otherwise, it is a draw configuration. The patterns in the first row are winning configurations. The patterns in the second row are draw configurations. In an operation, you can change an X to an O, or an O to an X. Let $$$k$$$ denote the total number of tokens in the grid. Your task is to make the grid a draw in at most $$$lfloor frac{k}{3} floor$$$ (rounding down) operations. You are not required to minimize the number of operations. Input The first line contains a single integer $$$t$$$ ($$$1le tle 100$$$) β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 300$$$) β€” the size of the grid. The following $$$n$$$ lines each contain a string of $$$n$$$ characters, denoting the initial grid. The character in the $$$i$$$-th row and $$$j$$$-th column is '.' if the cell is empty, or it is the type of token in the cell: 'X' or 'O'. It is guaranteed that not all cells are empty. In the easy version, the character 'O' does not appear in the input. The sum of $$$n$$$ across all test cases does not exceed $$$300$$$. Output For each test case, print the state of the grid after applying the operations. We have proof that a solution always exists. If there are multiple solutions, print any. Example Input 3 3 .X. XXX .X. 6 XX.XXX XXXXXX XXX.XX XXXXXX XX.X.X XXXXXX 5 XXX.X .X..X XXX.X ..X.. ..X.. Output .X. XOX .X. XX.XXO XOXXOX OXX.XX XOOXXO XX.X.X OXXOXX XOX.X .X..X XXO.O ..X.. ..X.. Note In the first test case, there are initially three 'X' consecutive in the second row and the second column. By changing the middle token to 'O' we make the grid a draw, and we only changed $$$1le lfloor 5/3 floor$$$ token. In the second test case, we change only $$$9le lfloor 32/3 floor$$$ tokens, and there does not exist any three 'X' or 'O' consecutive in a row or column, so it is a draw. In the third test case, we change only $$$3le lfloor 12/3 floor$$$ tokens, and the resulting grid is a draw.
2,100
true
false
false
false
false
true
false
false
false
false
3,436
1370B
# GCD Compression Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes Ashish has an array a of consisting of 2n positive integers. He wants to compress a into an array b of size n βˆ’ 1. To do this, he first discards exactly 2 (any two) elements from a. He then performs the following operation until there are no elements left in a: β€’ Remove any two elements from a and append their sum to b.The compressed array b has to have a special property. The greatest common divisor ( gcd ) of all its elements should be greater than 1.Recall that the gcd of an array of positive integers is the biggest integer that is a divisor of all integers in the array. It can be proven that it is always possible to compress array a into an array b of size n βˆ’ 1 such that gcd (b1, b 2..., b nβˆ’1) > 1.Help Ashish find a way to do so. # Input The first line contains a single integer t (1 ≀ t ≀ 10 ) x16 the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer n (2 ≀ n ≀ 1000 ). The second line of each test case contains 2n integers a1, a 2, . . . , a 2n (1 ≀ ai ≀ 1000 ) x16 the elements of the array a. # Output For each test case, output n βˆ’ 1 lines x16 the operations performed to compress the array a to the array b. The initial discard of the two elements is not an operation, you don’t need to output anything about it. The i-th line should contain two integers, the indices ( 1 x16based) of the two elements from the array a that are used in the i-th operation. All 2n βˆ’ 2 indices should be distinct integers from 1 to 2n.You don’t need to output two initially discarded elements from a.If there are multiple answers, you can find any. # Example standard input standard output 331 2 3 4 5 6 25 7 9 10 51 3 3 4 5 90 100 101 2 3 3 6 4 5 3 4 1 9 2 3 4 5 6 10 # Note In the first test case, b = {3 + 6 , 4 + 5 } = {9, 9} and gcd(9 , 9) = 9 . Page 1 of 2 In the second test case, b = {9 + 10 } = {19 } and gcd(19) = 19 .In the third test case, b = {1 + 2 , 3 + 3 , 4 + 5 , 90 + 3 } = {3, 6, 9, 93 } and gcd(3 , 6, 9, 93) = 3 . Page 2 of 2
1,100
true
false
false
false
false
true
false
false
false
false
3,820
1879E
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: [ are given a tree on $$$n$$$ vertices; vertex $$$1$$$ is the root of the tree. For every $$$i in [2, n]$$$, the parent of the $$$i$$$-th vertex is $$$p_i$$$, and $$$p_i < i$$$. You have to color all edges of the tree using the minimum possible number of colors such that you can win the game on that tree (every edge should be painted into exactly one color). The game we're going to play will be conducted as follows. After you paint the edges and print their colors, the jury will place a chip into one of the vertices of the tree (except for the root). Your goal is to move this chip to the root in exactly $$$d$$$ moves, where $$$d$$$ is the distance from the root to that vertex (the distance is equal to the number of edges on the path). If the chip reaches the root in $$$d$$$ moves, you win. Otherwise, you lose. The jury won't tell you where the chip is located. You won't even know the value of $$$d$$$ in advance. However, at the start of each move, you will be told how many edges of each color are incident to the current vertex (this includes both the edge leading up the tree and the edges leading away from the root). You have to choose one of these colors, and the chip will be moved along the edge of the chosen color (if there are multiple edges with that color incident to the current vertex, the jury gets to choose one of them). After the chip is moved, you will be told the same information about the current vertex again, and the game continues, until you either reach the root, or you make $$$d$$$ moves without reaching the root. The interactor for this problem is adaptive. It means that both the starting vertex and the current vertex are not fixed and may change "on the run" depending on the output of your program. However, the state of the game will always be consistent with the information you are given: there will always be at least one starting vertex and at least one path of your chip from that vertex consistent with both the information about the colors you receive and the colors you've chosen during the moves. Input The first line contains one integer $$$n$$$ ($$$3 le n le 100$$$) β€” the number of vertices in the tree. The second line contains $$$n-1$$$ integers $$$p_2, p_3, dots, p_n$$$ ($$$1 le p_i < i$$$), where $$$p_i$$$ is the parent of the $$$i$$$-th vertex in the tree. Interaction First, you have to print the coloring of the edges you've chosen as follows: in the first line, print one integer $$$k$$$ ($$$1 le k le n - 1$$$) β€” the number of colors you are using; in the second line, print $$$n-1$$$ integers $$$c_2, c_3, dots, c_n$$$ ($$$1 le c_i le k$$$), where $$$c_i$$$ should be the color of the edge connecting $$$p_i$$$ with $$$i$$$. Then, the game begins. At the start of each move, the jury program prints one of the following: one integer $$$1$$$ on a separate line, indicating that the chip has reached the root and you won; one integer $$$-1$$$ on a separate line, indicating that either you didn't reach the root in $$$d$$$ moves and lost, or you have done something incorrect (either the coloring you provided didn't meet the constraints, or your previous move is impossible); or one integer $$$0$$$ on a separate line, followed by a line containing $$$k$$$ integers $$$e_1, e_2, dots, e_k$$$, where $$$e_i$$$ is the number of edges with color $$$i$$$ incident to the current vertex. If you receive $$$1$$$ or $$$-1$$$, your program should terminate immediately, otherwise the verdict for your submission may be undefined. If you receive $$$0$$$ followed by $$$k$$$ integers $$$e_1, e_2, dots, e_k$$$, you have to print one integer indicating the color you choose during the move (of course, $$$e_i$$$ for that color should not be equal to $$$0$$$). Don't forget to flush the output every time you print something! Examples Input 4 1 2 3 0 0 1 0 0 1 1 0 0 1 0 1 1 Note In the first example, every vertex from $$$2$$$ to $$$n$$$ is connected to the root. So, we can paint all edges into the same color $$$1$$$, and when the game starts, there will be only one edge incident to the current vertex (and it will lead to the root). In the second example, the tree is a path of $$$4$$$ vertices. We have to paint its edges into different colors, because it can be shown that we don't have a winning strategy with just two colors.
2,400
false
false
true
false
false
true
true
false
false
true
1,006
1288F
You are given a bipartite graph: the first part of this graph contains $$$n_1$$$ vertices, the second part contains $$$n_2$$$ vertices, and there are $$$m$$$ edges. The graph can contain multiple edges. Initially, each edge is colorless. For each edge, you may either leave it uncolored (it is free), paint it red (it costs $$$r$$$ coins) or paint it blue (it costs $$$b$$$ coins). No edge can be painted red and blue simultaneously. There are three types of vertices in this graph β€” colorless, red and blue. Colored vertices impose additional constraints on edges' colours: for each red vertex, the number of red edges indicent to it should be strictly greater than the number of blue edges incident to it; for each blue vertex, the number of blue edges indicent to it should be strictly greater than the number of red edges incident to it. Colorless vertices impose no additional constraints. Your goal is to paint some (possibly none) edges so that all constraints are met, and among all ways to do so, you should choose the one with minimum total cost. Input The first line contains five integers $$$n_1$$$, $$$n_2$$$, $$$m$$$, $$$r$$$ and $$$b$$$ ($$$1 le n_1, n_2, m, r, b le 200$$$) β€” the number of vertices in the first part, the number of vertices in the second part, the number of edges, the amount of coins you have to pay to paint an edge red, and the amount of coins you have to pay to paint an edge blue, respectively. The second line contains one string consisting of $$$n_1$$$ characters. Each character is either U, R or B. If the $$$i$$$-th character is U, then the $$$i$$$-th vertex of the first part is uncolored; R corresponds to a red vertex, and B corresponds to a blue vertex. The third line contains one string consisting of $$$n_2$$$ characters. Each character is either U, R or B. This string represents the colors of vertices of the second part in the same way. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i le n_1$$$, $$$1 le v_i le n_2$$$) denoting an edge connecting the vertex $$$u_i$$$ from the first part and the vertex $$$v_i$$$ from the second part. The graph may contain multiple edges. Output If there is no coloring that meets all the constraints, print one integer $$$-1$$$. Otherwise, print an integer $$$c$$$ denoting the total cost of coloring, and a string consisting of $$$m$$$ characters. The $$$i$$$-th character should be U if the $$$i$$$-th edge should be left uncolored, R if the $$$i$$$-th edge should be painted red, or B if the $$$i$$$-th edge should be painted blue. If there are multiple colorings with minimum possible cost, print any of them. Examples Input 3 2 6 10 15 RRB UB 3 2 2 2 1 2 1 1 2 1 1 1 Input 3 1 3 4 5 RRR B 2 1 1 1 3 1 Input 3 1 3 4 5 URU B 2 1 1 1 3 1
2,900
false
false
false
false
false
true
false
false
false
false
4,247
313E
Ilya has recently taken up archaeology. He's recently found two numbers, written in the _m_-based notation. Each of the found numbers consisted of exactly _n_ digits. Ilya immediately started looking for information about those numbers. He learned that the numbers are part of a cyphered code and the one who can decypher it can get the greatest treasure. After considerable research Ilya understood that to decypher the code, he should do the following: Rearrange digits in the first number in some manner. Similarly, rearrange digits in the second number in some manner. As a result of this operation, the numbers can get leading zeroes. Add numbers, digit by digit, modulo _m_. In other words, we need to get the third number of length _n_, each digit of the number is the sum of the respective numbers of the found numbers. For example, suppose there are two numbers recorded in the ternary notation, 001210 and 012111, then if you add them to each other digit by digit modulo 3, you will get number 010021. The key to the code is the maximum possible number that can be obtained in the previous step. Help Ilya, find the key to the code. Input The first line contains two integers _n_,u2009_m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009105,u2009_m_u2009>u20091). The second line contains the first found number, the third line contains the second found number. The numbers are recorded as a sequence of digits in the _m_-based notation. Each digit is an integer from 0 to _m_u2009-u20091. The digits in the line are written in the order from the most significant digits to the least significant ones. The given numbers can contain leading zeroes. Output Print _n_ _m_-base digits. The resulting third number written in the _m_-based notation. Print the digits in the order from the most significant digits to the least significant ones. Examples Input 5 5 2 4 4 1 3 1 0 1 2 4
2,300
false
true
false
false
true
true
false
false
false
false
8,591
903D
Problem - 903D - 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 data structures math *2200 No tag edit access β†’ Contest materials over all pairs (_i_,u2009_j_) such that 1u2009≀u2009_i_u2009≀u2009_j_u2009≀u2009_n_. Input The first line contains one integer _n_ (1u2009≀u2009_n_u2009≀u2009200000) β€” the number of elements in _a_. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≀u2009_a__i_u2009≀u2009109) β€” elements of the array. Output Print one integer β€” the sum of _d_(_a__i_,u2009_a__j_) over all pairs (_i_,u2009_j_) such that 1u2009≀u2009_i_u2009≀u2009_j_u2009≀u2009_n_. Examples Input 5 1 2 3 1 3 Output 4 Input 4 6 6 5 5 Output 0 Input 4 6 6 4 4 Output -8 Note In the first example: 1. _d_(_a_1,u2009_a_2)u2009=u20090; 2. _d_(_a_1,u2009_a_3)u2009=u20092; 3. _d_(_a_1,u2009_a_4)u2009=u20090; 4. _d_(_a_1,u2009_a_5)u2009=u20092; 5. _d_(_a_2,u2009_a_3)u2009=u20090; 6. _d_(_a_2,u2009_a_4)u2009=u20090; 7. _d_(_a_2,u2009_a_5)u2009=u20090; 8. _d_(_a_3,u2009_a_4)u2009=u2009u2009-u20092; 9. _d_(_a_3,u2009_a_5)u2009=u20090; 10. _d_(_a_4,u2009_a_5)u2009=u20092.
2,200
true
false
false
false
true
false
false
false
false
false
6,131
930A
In Arcady's garden there grows a peculiar apple-tree that fruits one time per year. Its peculiarity can be explained in following way: there are _n_ inflorescences, numbered from 1 to _n_. Inflorescence number 1 is situated near base of tree and any other inflorescence with number _i_ (_i_u2009>u20091) is situated at the top of branch, which bottom is _p__i_-th inflorescence and _p__i_u2009<u2009_i_. Once tree starts fruiting, there appears exactly one apple in each inflorescence. The same moment as apples appear, they start to roll down along branches to the very base of tree. Each second all apples, except ones in first inflorescence simultaneously roll down one branch closer to tree base, e.g. apple in _a_-th inflorescence gets to _p__a_-th inflorescence. Apples that end up in first inflorescence are gathered by Arcady in exactly the same moment. Second peculiarity of this tree is that once two apples are in same inflorescence they annihilate. This happens with each pair of apples, e.g. if there are 5 apples in same inflorescence in same time, only one will not be annihilated and if there are 8 apples, all apples will be annihilated. Thus, there can be no more than one apple in each inflorescence in each moment of time. Help Arcady with counting number of apples he will be able to collect from first inflorescence during one harvest. Input First line of input contains single integer number _n_ (2u2009≀u2009_n_u2009≀u2009100u2009000) xa0β€” number of inflorescences. Second line of input contains sequence of _n_u2009-u20091 integer numbers _p_2,u2009_p_3,u2009...,u2009_p__n_ (1u2009≀u2009_p__i_u2009<u2009_i_), where _p__i_ is number of inflorescence into which the apple from _i_-th inflorescence rolls down. Output Single line of output should contain one integer number: amount of apples that Arcady will be able to collect from first inflorescence during one harvest. Examples Input 18 1 1 1 4 4 3 2 2 2 10 8 9 9 9 10 10 4 Note In first example Arcady will be able to collect only one apple, initially situated in 1st inflorescence. In next second apples from 2nd and 3rd inflorescences will roll down and annihilate, and Arcady won't be able to collect them. In the second example Arcady will be able to collect 3 apples. First one is one initially situated in first inflorescence. In a second apple from 2nd inflorescence will roll down to 1st (Arcady will collect it) and apples from 3rd, 4th, 5th inflorescences will roll down to 2nd. Two of them will annihilate and one not annihilated will roll down from 2-nd inflorescence to 1st one in the next second and Arcady will collect it.
1,500
false
false
false
false
false
false
false
false
false
true
5,986
794C
Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little things. Recently, they started a new company, but they are having trouble finding a name for the company. To settle this problem, they've decided to play a game. The company name will consist of _n_ letters. Oleg and Igor each have a set of _n_ letters (which might contain multiple copies of the same letter, the sets can be different). Initially, the company name is denoted by _n_ question marks. Oleg and Igor takes turns to play the game, Oleg moves first. In each turn, a player can choose one of the letters _c_ in his set and replace any of the question marks with _c_. Then, a copy of the letter _c_ is removed from his set. The game ends when all the question marks has been replaced by some letter. For example, suppose Oleg has the set of letters {_i_,u2009_o_,u2009_i_} and Igor has the set of letters {_i_,u2009_m_,u2009_o_}. One possible game is as follows : Initially, the company name is ???. Oleg replaces the second question mark with 'i'. The company name becomes ?i?. The set of letters Oleg have now is {_i_,u2009_o_}. Igor replaces the third question mark with 'o'. The company name becomes ?io. The set of letters Igor have now is {_i_,u2009_m_}. Finally, Oleg replaces the first question mark with 'o'. The company name becomes oio. The set of letters Oleg have now is {_i_}. In the end, the company name is oio. Oleg wants the company name to be as lexicographically small as possible while Igor wants the company name to be as lexicographically large as possible. What will be the company name if Oleg and Igor always play optimally? A string _s_u2009=u2009_s_1_s_2..._s__m_ is called lexicographically smaller than a string _t_u2009=u2009_t_1_t_2..._t__m_ (where _s_u2009β‰ u2009_t_) if _s__i_u2009<u2009_t__i_ where _i_ is the smallest index such that _s__i_u2009β‰ u2009_t__i_. (so _s__j_u2009=u2009_t__j_ for all _j_u2009<u2009_i_)
1,800
false
true
false
false
false
false
false
false
true
false
6,598
1750B
A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s$$$. For some non-empty substring$$$^dagger$$$ $$$t$$$ of string $$$s$$$ containing $$$x$$$ characters 0 and $$$y$$$ characters 1, define its cost as: $$$x cdot y$$$, if $$$x > 0$$$ and $$$y > 0$$$; $$$x^2$$$, if $$$x > 0$$$ and $$$y = 0$$$; $$$y^2$$$, if $$$x = 0$$$ and $$$y > 0$$$. Given a binary string $$$s$$$ of length $$$n$$$, find the maximum cost across all its non-empty substrings. $$$^dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the length of the string $$$s$$$. 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, print a single integer β€” the maximum cost across all substrings. Example Input 6 5 11100 7 1100110 6 011110 7 1001010 4 1000 1 0 Note In the first test case, we can take a substring $$$111$$$. It contains $$$3$$$ characters 1 and $$$0$$$ characters 0. So $$$a = 3$$$, $$$b = 0$$$ and its cost is $$$3^2 = 9$$$. In the second test case, we can take the whole string. It contains $$$4$$$ characters 1 and $$$3$$$ characters 0. So $$$a = 4$$$, $$$b = 3$$$ and its cost is $$$4 cdot 3 = 12$$$. In the third test case, we can can take a substring $$$1111$$$ and its cost is $$$4^2 = 16$$$. In the fourth test case, we can take the whole string and cost is $$$4 cdot 3 = 12$$$. In the fifth test case, we can take a substring $$$000$$$ and its cost is $$$3 cdot 3 = 9$$$. In the sixth test case, we can only take the substring $$$0$$$ and its cost is $$$1 cdot 1 = 1$$$.
800
false
true
true
false
false
false
true
false
false
false
1,784
1238D
The string $$$t_1t_2 dots t_k$$$ is good if each letter of this string belongs to at least one palindrome of length greater than 1. A palindrome is a string that reads the same backward as forward. For example, the strings A, BAB, ABBA, BAABBBAAB are palindromes, but the strings AB, ABBBAA, BBBA are not. Here are some examples of good strings: $$$t$$$ = AABBB (letters $$$t_1$$$, $$$t_2$$$ belong to palindrome $$$t_1 dots t_2$$$ and letters $$$t_3$$$, $$$t_4$$$, $$$t_5$$$ belong to palindrome $$$t_3 dots t_5$$$); $$$t$$$ = ABAA (letters $$$t_1$$$, $$$t_2$$$, $$$t_3$$$ belong to palindrome $$$t_1 dots t_3$$$ and letter $$$t_4$$$ belongs to palindrome $$$t_3 dots t_4$$$); $$$t$$$ = AAAAA (all letters belong to palindrome $$$t_1 dots t_5$$$); You are given a string $$$s$$$ of length $$$n$$$, consisting of only letters A and B. You have to calculate the number of good substrings of string $$$s$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$) β€” the length of the string $$$s$$$. The second line contains the string $$$s$$$, consisting of letters A and B. Output Print one integer β€” the number of good substrings of string $$$s$$$. Note In the first test case there are six good substrings: $$$s_1 dots s_2$$$, $$$s_1 dots s_4$$$, $$$s_1 dots s_5$$$, $$$s_3 dots s_4$$$, $$$s_3 dots s_5$$$ and $$$s_4 dots s_5$$$. In the second test case there are three good substrings: $$$s_1 dots s_2$$$, $$$s_1 dots s_3$$$ and $$$s_2 dots s_3$$$.
1,900
false
false
false
true
false
false
false
true
false
false
4,500
1276E
There are four stones on an infinite line in integer coordinates $$$a_1, a_2, a_3, a_4$$$. The goal is to have the stones in coordinates $$$b_1, b_2, b_3, b_4$$$. The order of the stones does not matter, that is, a stone from any position $$$a_i$$$ can end up in at any position $$$b_j$$$, provided there is a required number of stones in each position (that is, if a coordinate $$$x$$$ appears $$$k$$$ times among numbers $$$b_1, ldots, b_4$$$, there should be exactly $$$k$$$ stones at $$$x$$$ in the end). We are allowed to move stones with the following operation: choose two stones at distinct positions $$$x$$$ and $$$y$$$ with at least one stone each, and move one stone from $$$x$$$ to $$$2y - x$$$. In other words, the operation moves a stone to a symmetric position relative to some other stone. At any moment it is allowed to have any number of stones at the same position. Find any sequence of operations that achieves the goal, or determine that it is impossible. The sequence does not have to be shortest, but it may contain at most $$$1000$$$ operations. Input The first line contains four integers $$$a_1, ldots, a_4$$$ ($$$-10^9 leq a_i leq 10^9$$$)xa0β€” initial coordinates of the stones. There may be multiple stones sharing the same coordinate. The second line contains four integers $$$b_1, ldots, b_4$$$ ($$$-10^9 leq b_i leq 10^9$$$)xa0β€” target coordinates of the stones. There may be multiple targets sharing the same coordinate. Output If there is no sequence of operations that achieves the goal, print a single integer $$$-1$$$. Otherwise, on the first line print a single integer $$$k$$$ ($$$0 leq k leq 1000$$$)xa0β€” the number of operations in your sequence. On the next $$$k$$$ lines, describe the operations. The $$$i$$$-th of these lines should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$x_i eq y_i$$$)xa0β€” coordinates of the moved stone and the center of symmetry stone for the $$$i$$$-th operation. For each operation $$$i$$$, there should at least one stone in each of the coordinates $$$x_i$$$ and $$$y_i$$$, and the resulting coordinate $$$2y_i - x_i$$$ must not exceed $$$10^{18}$$$ by absolute value. If there are multiple suitable sequences, print any of them. It is guaranteed that if there is a suitable sequence of operations, then there is also a suitable sequence that satisfies all the additional requirement.
3,500
false
false
false
false
false
true
false
false
false
false
4,311
129A
Olga came to visit the twins Anna and Maria and saw that they have many cookies. The cookies are distributed into bags. As there are many cookies, Olga decided that it's no big deal if she steals a bag. However, she doesn't want the sisters to quarrel because of nothing when they divide the cookies. That's why Olga wants to steal a bag with cookies so that the number of cookies in the remaining bags was even, that is, so that Anna and Maria could evenly divide it into two (even 0 remaining cookies will do, just as any other even number). How many ways there are to steal exactly one cookie bag so that the total number of cookies in the remaining bags was even? Input The first line contains the only integer _n_ (1u2009≀u2009_n_u2009≀u2009100) β€” the number of cookie bags Anna and Maria have. The second line contains _n_ integers _a__i_ (1u2009≀u2009_a__i_u2009≀u2009100) β€” the number of cookies in the _i_-th bag. Output Print in the only line the only number β€” the sought number of ways. If there are no such ways print 0. Examples Input 10 1 2 2 3 4 4 4 2 2 2 Input 11 2 2 2 2 2 2 2 2 2 2 99 Note In the first sample Olga should take the only bag so that the twins ended up with the even number of cookies. In the second sample Olga can take any of five bags with two cookies or any of three bags with four cookies β€” 5u2009+u20093u2009=u20098 ways in total. In the third sample, no matter which bag with two cookies Olga chooses, the twins are left with 2u2009*u20099u2009+u200999u2009=u2009117 cookies. Thus, Olga has only one option: to take the bag with 99 cookies.
900
false
false
true
false
false
false
false
false
false
false
9,365
707A
Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead. As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such). Brain took a lot of colourful pictures on colored and black-and-white film. Then he developed and translated it into a digital form. But now, color and black-and-white photos are in one folder, and to sort them, one needs to spend more than one hour! As soon as Brain is a photographer not programmer now, he asks you to help him determine for a single photo whether it is colored or black-and-white. Photo can be represented as a matrix sized _n_u2009Γ—u2009_m_, and each element of the matrix stores a symbol indicating corresponding pixel color. There are only 6 colors: 'C' (cyan) 'M' (magenta) 'Y' (yellow) 'W' (white) 'G' (grey) 'B' (black) The photo is considered black-and-white if it has only white, black and grey pixels in it. If there are any of cyan, magenta or yellow pixels in the photo then it is considered colored. Input The first line of the input contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009100)xa0β€” the number of photo pixel matrix rows and columns respectively. Then _n_ lines describing matrix rows follow. Each of them contains _m_ space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', 'M', 'Y', 'W', 'G' or 'B'. Output Print the "#Black&White" (without quotes), if the photo is black-and-white and "#Color" (without quotes), if it is colored, in the only line.
800
false
false
true
false
false
false
false
false
false
false
6,994
1254B2
This is the harder version of the problem. In this version, 1≀n≀106 and 0≀ai≀106. You can hack this problem if you locked it. But you can hack the previous problem only if you locked both problems Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare n boxes of chocolate, numbered from 1 to n. Initially, the i-th box contains ai chocolate pieces. Since Bob is a typical nice guy, he will not send Alice n empty boxes. In other words, at least one of a1,a2,…,an is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer k>1 such that the number of pieces in each box is divisible by k. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box i and put it into either box iβˆ’1 or box i+1 (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. Input The first line contains a single integer n (1≀n≀106)xa0β€” the number of chocolate boxes. The second line contains n integers a1,a2,…,an (0≀ai≀106)xa0β€” the number of chocolate pieces in the i-th box. It is guaranteed that at least one of a1,a2,…,an is positive. Output If there is no way for Charlie to make Alice happy, print βˆ’1. Otherwise, print a single integer xxa0β€” the minimum number of seconds for Charlie to help Bob make Alice happy. Note In the first example, Charlie can move all chocolate pieces to the second box. Each box will be divisible by 17. In the second example, Charlie can move a piece from box 2 to box 3 and a piece from box 4 to box 5. Each box will be divisible by 3. In the third example, each box is already divisible by 5. In the fourth example, since Charlie has no available move, he cannot help Bob make Alice happy.
2,100
true
true
false
false
false
true
false
false
false
false
4,414
1868F
Entering senior high school life, Tom is attracted by LIS problems, not only the Longest Increasing Subsequence problem, but also the Largest Interval Sum problem. Now he gets a really interesting problem from his friend Daniel. However, it seems too hard for him to solve it, so he asks you for help. Given an array $$$a$$$ consisting of $$$n$$$ integers. In one operation, you do the following: Select an interval $$$[l,r]$$$ ($$$1le lle rle n$$$), such that the sum of the interval is the largest among all intervals in the array $$$a$$$. More formally, $$$displaystylesum_{i=l}^r a_i=max_{1le l'le r'le n}sum_{i=l'}^{r'} a_i$$$. Then subtract $$$1$$$ from all elements $$$a_l,a_{l+1},ldots,a_r$$$. Find the minimum number of operations you need to perform to make $$$a_i<0$$$ for every $$$1le ile n$$$. Input The first line of input contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$) β€” the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$-10^6le a_ile 10^6$$$) β€” the elements of the array $$$a$$$. Note In the first example, you can do operations on intervals $$$[1,5],[1,5],[2,5],[3,5],[4,5],[5,5]$$$ in such order. You may also do operations on intervals $$$[1,5],[2,5],[3,5],[4,5],[5,5],[1,5]$$$ in such order. In the second example, it's already satisfied that $$$a_i<0$$$ for every $$$1le ile n$$$. So you do not need to perform any operations.
3,500
false
true
true
false
true
false
false
false
false
false
1,062
354E
We know that lucky digits are digits 4 and 7, however Vasya's got another favorite digit 0 and he assumes it also is lucky! Lucky numbers are such non-negative integers whose decimal record only contains lucky digits. For example, numbers 0,u200947,u20097074 are lucky, but 1,u20097377,u2009895,u2009 -7 are not. Vasya has _t_ important positive integers he needs to remember. Vasya is quite superstitious and he wants to remember lucky numbers only, so he is asking you for each important number to represent it as a sum of exactly six lucky numbers (Vasya just can't remember more numbers). Then Vasya can just remember these six numbers and calculate the important number at any moment. For each of _t_ important integers represent it as the sum of six lucky numbers or state that this is impossible. Input The first line contains a single integer _t_ (1u2009≀u2009_t_u2009≀u20095000). Next _t_ lines contain a single positive integer _n__i_ (1u2009≀u2009_n__i_u2009≀u20091018) β€” the list of important numbers. Please, do not use the %lld to read or write 64-bit integers Π‘++. It is preferred to read the cin, cout streams or the %I64d specifier. Output Print _t_ lines. The _i_-th line must contain the answer for the _i_-th important number: if the solution exists, the line must contain exactly six lucky numbers the sum of which equals _n__i_, if the solution doesn't exist the string must contain a single integer -1. If there are multiple answers print any of them. Examples Output 7 7 7 7 7 7 -1 400 0 40 0 4 0 7 0 0 0 0 0 47 4 0 0 0 0
2,200
false
false
false
true
false
true
false
false
false
false
8,414
178A2
The Smart Beaver from ABBYY began to develop a new educational game for children. The rules of the game are fairly simple and are described below. The playing field is a sequence of _n_ non-negative integers _a__i_ numbered from 1 to _n_. The goal of the game is to make numbers _a_1,u2009_a_2,u2009...,u2009_a__k_ (i.e. some prefix of the sequence) equal to zero for some fixed _k_ (_k_u2009<u2009_n_), and this should be done in the smallest possible number of moves. One move is choosing an integer _i_ (1u2009≀u2009_i_u2009≀u2009_n_) such that _a__i_u2009>u20090 and an integer _t_ (_t_u2009β‰₯u20090) such that _i_u2009+u20092_t_u2009≀u2009_n_. After the values of _i_ and _t_ have been selected, the value of _a__i_ is decreased by 1, and the value of _a__i_u2009+u20092_t_ is increased by 1. For example, let _n_u2009=u20094 and _a_u2009=u2009(1,u20090,u20091,u20092), then it is possible to make move _i_u2009=u20093, _t_u2009=u20090 and get _a_u2009=u2009(1,u20090,u20090,u20093) or to make move _i_u2009=u20091, _t_u2009=u20091 and get _a_u2009=u2009(0,u20090,u20092,u20092) (the only possible other move is _i_u2009=u20091, _t_u2009=u20090). You are given _n_ and the initial sequence _a__i_. The task is to calculate the minimum number of moves needed to make the first _k_ elements of the original sequence equal to zero for each possible _k_ (1u2009≀u2009_k_u2009<u2009_n_). Input The first input line contains a single integer _n_. The second line contains _n_ integers _a__i_ (0u2009≀u2009_a__i_u2009≀u2009104), separated by single spaces. The input limitations for getting 20 points are: 1u2009≀u2009_n_u2009≀u2009300 The input limitations for getting 50 points are: 1u2009≀u2009_n_u2009≀u20092000 The input limitations for getting 100 points are: 1u2009≀u2009_n_u2009≀u2009105 Output Print exactly _n_u2009-u20091 lines: the _k_-th output line must contain the minimum number of moves needed to make the first _k_ elements of the original sequence _a__i_ equal to zero. 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.
1,000
false
true
false
false
false
false
false
false
false
false
9,139
1400A
A binary string is a string where each character is either 0 or 1. Two binary strings $$$a$$$ and $$$b$$$ of equal length are similar, if they have the same character in some position (there exists an integer $$$i$$$ such that $$$a_i = b_i$$$). For example: 10010 and 01111 are similar (they have the same character in position $$$4$$$); 10010 and 11111 are similar; 111 and 111 are similar; 0110 and 1001 are not similar. You are given an integer $$$n$$$ and a binary string $$$s$$$ consisting of $$$2n-1$$$ characters. Let's denote $$$s[l..r]$$$ as the contiguous substring of $$$s$$$ starting with $$$l$$$-th character and ending with $$$r$$$-th character (in other words, $$$s[l..r] = s_l s_{l + 1} s_{l + 2} dots s_r$$$). You have to construct a binary string $$$w$$$ of length $$$n$$$ which is similar to all of the following strings: $$$s[1..n]$$$, $$$s[2..n+1]$$$, $$$s[3..n+2]$$$, ..., $$$s[n..2n-1]$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$). The second line of each test case contains the binary string $$$s$$$ of length $$$2n - 1$$$. Each character $$$s_i$$$ is either 0 or 1. Output For each test case, print the corresponding binary string $$$w$$$ of length $$$n$$$. If there are multiple such strings β€” print any of them. It can be shown that at least one string $$$w$$$ meeting the constraints always exists. Example Input 4 1 1 3 00000 4 1110000 2 101 Note The explanation of the sample case (equal characters in equal positions are bold): The first test case: $$$mathbf{1}$$$ is similar to $$$s[1..1] = mathbf{1}$$$. The second test case: $$$mathbf{000}$$$ is similar to $$$s[1..3] = mathbf{000}$$$; $$$mathbf{000}$$$ is similar to $$$s[2..4] = mathbf{000}$$$; $$$mathbf{000}$$$ is similar to $$$s[3..5] = mathbf{000}$$$. The third test case: $$$mathbf{1}0mathbf{10}$$$ is similar to $$$s[1..4] = mathbf{1}1mathbf{10}$$$; $$$mathbf{1}01mathbf{0}$$$ is similar to $$$s[2..5] = mathbf{1}10mathbf{0}$$$; $$$mathbf{10}1mathbf{0}$$$ is similar to $$$s[3..6] = mathbf{10}0mathbf{0}$$$; $$$1mathbf{0}1mathbf{0}$$$ is similar to $$$s[4..7] = 0mathbf{0}0mathbf{0}$$$. The fourth test case: $$$0mathbf{0}$$$ is similar to $$$s[1..2] = 1mathbf{0}$$$; $$$mathbf{0}0$$$ is similar to $$$s[2..3] = mathbf{0}1$$$.
800
false
false
false
false
false
true
false
false
false
false
3,672
1515D
To satisfy his love of matching socks, Phoenix has brought his $$$n$$$ socks ($$$n$$$ is even) to the sock store. Each of his socks has a color $$$c_i$$$ and is either a left sock or right sock. Phoenix can pay one dollar to the sock store to either: recolor a sock to any color $$$c'$$$ $$$(1 le c' le n)$$$ turn a left sock into a right sock turn a right sock into a left sock The sock store may perform each of these changes any number of times. Note that the color of a left sock doesn't change when it turns into a right sock, and vice versa. A matching pair of socks is a left and right sock with the same color. What is the minimum cost for Phoenix to make $$$n/2$$$ matching pairs? Each sock must be included in exactly one matching pair. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$l$$$, and $$$r$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$n$$$ is even; $$$0 le l, r le n$$$; $$$l+r=n$$$)xa0β€” the total number of socks, and the number of left and right socks, respectively. The next line contains $$$n$$$ integers $$$c_i$$$ ($$$1 le c_i le n$$$)xa0β€” the colors of the socks. The first $$$l$$$ socks are left socks, while the next $$$r$$$ socks are right socks. It is guaranteed that the sum of $$$n$$$ across all the test cases will not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integerxa0β€” the minimum cost for Phoenix to make $$$n/2$$$ matching pairs. Each sock must be included in exactly one matching pair. Example Input 4 6 3 3 1 2 3 2 2 2 6 2 4 1 1 2 2 2 2 6 5 1 6 5 4 3 2 1 4 0 4 4 4 4 3 Note In the first test case, Phoenix can pay $$$2$$$ dollars to: recolor sock $$$1$$$ to color $$$2$$$ recolor sock $$$3$$$ to color $$$2$$$ There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 4)$$$, $$$(2, 5)$$$, and $$$(3, 6)$$$ are matching. In the second test case, Phoenix can pay $$$3$$$ dollars to: turn sock $$$6$$$ from a right sock to a left sock recolor sock $$$3$$$ to color $$$1$$$ recolor sock $$$4$$$ to color $$$1$$$ There are now $$$3$$$ matching pairs. For example, pairs $$$(1, 3)$$$, $$$(2, 4)$$$, and $$$(5, 6)$$$ are matching.
1,500
false
true
false
false
false
false
false
false
true
false
3,088
1334G
You are given a permutation $$$p$$$ consisting of exactly $$$26$$$ integers from $$$1$$$ to $$$26$$$ (since it is a permutation, each integer from $$$1$$$ to $$$26$$$ occurs in $$$p$$$ exactly once) and two strings $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. A substring $$$t'$$$ of string $$$t$$$ is an occurence of string $$$s$$$ if the following conditions are met: 1. $$$t' = s$$$; 2. for each $$$i in [1, s]$$$, either $$$s_i = t'_i$$$, or $$$p_{idx(s_i)} = idx(t'_i)$$$, where $$$idx(c)$$$ is the index of character $$$c$$$ in Latin alphabet ($$$idx( ext{a}) = 1$$$, $$$idx( ext{b}) = 2$$$, $$$idx( ext{z}) = 26$$$). For example, if $$$p_1 = 2$$$, $$$p_2 = 3$$$, $$$p_3 = 1$$$, $$$s = ext{abc}$$$, $$$t = ext{abcaaba}$$$, then three substrings of $$$t$$$ are occurences of $$$s$$$ (they are $$$t' = ext{abc}$$$, $$$t' = ext{bca}$$$ and $$$t' = ext{aba}$$$). For each substring of $$$t$$$ having length equal to $$$s$$$, check if it is an occurence of $$$s$$$. Input The first line contains $$$26$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_{26}$$$ ($$$1 le p_i le 26$$$, all these integers are pairwise distinct). The second line contains one string $$$s$$$, and the third line contains one string $$$t$$$ ($$$2 le s le t le 2 cdot 10^5$$$) both consisting of lowercase Latin letters. Output Print a string of $$$t - s + 1$$$ characters, each character should be either 0 or 1. The $$$i$$$-th character should be 1 if and only if the substring of $$$t$$$ starting with the $$$i$$$-th character and ending with the $$$(i + s - 1)$$$-th character (inclusive) is an occurence of $$$s$$$. Example Input 2 3 1 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 abc abcaaba
2,900
false
false
false
false
false
false
true
false
false
false
4,031
896C
β€” Willem... β€” What's the matter? β€” It seems that there's something wrong with Seniorious... β€” I'll have a look... Seniorious is made by linking special talismans in particular order. After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly. Seniorious has _n_ pieces of talisman. Willem puts them in a line, the _i_-th of which is an integer _a__i_. In order to maintain it, Willem needs to perform _m_ operations. There are four types of operations: 1 _l_ _r_ _x_: For each _i_ such that _l_u2009≀u2009_i_u2009≀u2009_r_, assign _a__i_u2009+u2009_x_ to _a__i_. 2 _l_ _r_ _x_: For each _i_ such that _l_u2009≀u2009_i_u2009≀u2009_r_, assign _x_ to _a__i_. 3 _l_ _r_ _x_: Print the _x_-th smallest number in the index range [_l_,u2009_r_], i.e. the element at the _x_-th position if all the elements _a__i_ such that _l_u2009≀u2009_i_u2009≀u2009_r_ are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1u2009≀u2009_x_u2009≀u2009_r_u2009-u2009_l_u2009+u20091. 4 _l_ _r_ _x_ _y_: Print the sum of the _x_-th power of _a__i_ such that _l_u2009≀u2009_i_u2009≀u2009_r_, modulo _y_, i.e. . Input The only line contains four integers _n_,u2009_m_,u2009_seed_,u2009_v__max_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009105,u20090u2009≀u2009_seed_u2009<u2009109u2009+u20097,u20091u2009≀u2009_vmax_u2009≀u2009109). The initial values and operations are generated using following pseudo code: def rnd():ret = seed seed = (seed 7 + 13) mod 1000000007 return ret for i = 1 to n: a[i] = (rnd() mod vmax) + 1 for i = 1 to m: op = (rnd() mod 4) + 1 l = (rnd() mod n) + 1 r = (rnd() mod n) + 1 if (l > r): swap(l, r) if (op == 3): x = (rnd() mod (r - l + 1)) + 1 else: x = (rnd() mod vmax) + 1 if (op == 4): y = (rnd() mod vmax) + 1 Here _op_ is the type of the operation mentioned in the legend. Output For each operation of types 3 or 4, output a line containing the answer. Note In the first example, the initial array is {8,u20099,u20097,u20092,u20093,u20091,u20095,u20096,u20094,u20098}. The operations are: 2 6 7 9 1 3 10 8 4 4 6 2 4 1 4 5 8 2 1 7 1 4 7 9 4 4 1 2 7 9 4 5 8 1 1 2 5 7 5 4 3 10 8 5
2,600
false
false
false
false
true
false
false
false
false
false
6,163
195D
Problem - 195D - 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 geometry math sortings *1900 No tag edit access β†’ Contest materials ") Π Π°Π·Π±ΠΎΡ€ Π—Π°Π΄Π°Ρ‡") ,u2009...,u2009_y__n_(_x_) of the given type as function _s_(_x_)u2009=u2009_y_1(_x_)u2009+u2009...u2009+u2009_y__n_(_x_) for any _x_. It's easy to show that in this case the graph _s_(_x_) is a polyline. You are given _n_ functions of the given type, your task is to find the number of angles that do not equal 180 degrees, in the graph _s_(_x_), that is the sum of the given functions.Valeric and Valerko really want to watch the next Euro Championship game, so they asked you to help them. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009105) β€” the number of functions. Each of the following _n_ lines contains two space-separated integer numbers _k__i_,u2009_b__i_ (u2009-u2009109u2009≀u2009_k__i_,u2009_b__i_u2009≀u2009109) that determine the _i_-th function. Output Print a single number β€” the number of angles that do not equal 180 degrees in the graph of the polyline that equals the sum of the given functions. Examples Input 1 1 0 Output 1 Input 3 1 0 0 2 -1 1 Output 2 Input 3 -2 -4 1 7 -5 1 Output 3
1,900
true
false
false
false
false
false
false
false
true
false
9,060
1476D
There are $$$n + 1$$$ cities, numbered from $$$0$$$ to $$$n$$$. $$$n$$$ roads connect these cities, the $$$i$$$-th road connects cities $$$i - 1$$$ and $$$i$$$ ($$$i in [1, n]$$$). Each road has a direction. The directions are given by a string of $$$n$$$ characters such that each character is either L or R. If the $$$i$$$-th character is L, it means that the $$$i$$$-th road initially goes from the city $$$i$$$ to the city $$$i - 1$$$; otherwise it goes from the city $$$i - 1$$$ to the city $$$i$$$. A traveler would like to visit as many cities of this country as possible. Initially, they will choose some city to start their journey from. Each day, the traveler must go from the city where they currently are to a neighboring city using one of the roads, and they can go along a road only if it is directed in the same direction they are going; i.u2009e., if a road is directed from city $$$i$$$ to the city $$$i + 1$$$, it is possible to travel from $$$i$$$ to $$$i + 1$$$, but not from $$$i + 1$$$ to $$$i$$$. After the traveler moves to a neighboring city, all roads change their directions to the opposite ones. If the traveler cannot go from their current city to a neighboring city, their journey ends; it is also possible to end the journey whenever the traveler wants to. The goal of the traveler is to visit as many different cities as possible (they can visit a city multiple times, but only the first visit is counted). For each city $$$i$$$, calculate the maximum number of different cities the traveler can visit during exactly one journey if they start in the city $$$i$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$). The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ characters, each character is either L or R. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print $$$n + 1$$$ integers. The $$$i$$$-th integer should be equal to the maximum number of different cities the traveler can visit during one journey if this journey starts in the $$$i$$$-th city.
1,700
false
false
true
true
false
false
false
false
false
false
3,294
1466I
What walks on four feet in the morning, two in the afternoon, and three at night? This is an interactive problem. This problem doesn't support hacks. Sphinx's duty is to guard the city of Thebes by making sure that no unworthy traveler crosses its gates. Only the ones who answer her riddle timely and correctly (or get an acc for short) are allowed to pass. As of those who fail, no one heard of them ever again... So you don't have a choice but to solve the riddle. Sphinx has an array $$$a_1, a_2, ldots, a_n$$$ of nonnegative integers strictly smaller than $$$2^b$$$ and asked you to find the maximum value among its elements. Of course, she will not show you the array, but she will give you $$$n$$$ and $$$b$$$. As it is impossible to answer this riddle blindly, you can ask her some questions. For given $$$i, y$$$, she'll answer you whether $$$a_i$$$ is bigger than $$$y$$$. As sphinxes are not very patient, you can ask at most $$$3 cdot (n + b) $$$ such questions. Although cunning, sphinxes are honest. Even though the array can change between your queries, answers to the previously asked questions will remain valid. Input The first line contains two integers $$$n$$$ and $$$b$$$ ($$$1 leq n, b leq 200$$$). The remaining parts of the input will be given throughout the interaction process. Interaction In each round your program must output a single line with an integer $$$i$$$ ($$$0 leq i leq n$$$) and a binary string of length exactly $$$b$$$ denoting the binary representation of $$$y$$$ (most significant bit first). If $$$i > 0$$$, this line encodes the question: Is $$$a_i$$$ bigger than $$$y$$$?. There should be at most $$$3 cdot (n+b)$$$ such lines; after each of them, the interactor will print yes or no in a single line. If $$$i = 0$$$, this is the last round of interaction, after which your program should terminate, and $$$y$$$ should be the maximal value among the elements of Sphinx's array. Note that this round does not count to the query limit. Note that the interactor is adaptive. After printing a query, do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. If your solution does not correctly follow the interaction guideline above, it may receive an arbitrary verdict. Otherwise, your program will receive the Wrong Answer judgment if it reports the wrong maximum. Examples Input 5 3 yes no no no no yes Output 5 101 5 110 4 100 3 101 2 001 1 000 0 110 Output 1 000 2 000 3 000 4 000 0 000 Note In all examples, the sequence is fixed beforehand. In the first example, the sequence is $$$2, 1, 4, 0, 6$$$. In the second example, the sequence is $$$0, 0, 0, 0$$$. In the third example, the sequence is $$$0$$$. Note that if the interactor was adaptive, then the interaction in the first and the third example would not be sufficient to return the correct value of maximum.
3,400
false
false
false
false
true
false
false
true
false
false
3,358
1042B
Berland shop sells $$$n$$$ kinds of juices. Each juice has its price $$$c_i$$$. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice can contain one, two or all three types of vitamins in it. Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it. Input The first line contains a single integer $$$n$$$ $$$(1 le n le 1,000)$$$ β€” the number of juices. Each of the next $$$n$$$ lines contains an integer $$$c_i$$$ $$$(1 le c_i le 100,000)$$$ and a string $$$s_i$$$ β€” the price of the $$$i$$$-th juice and the vitamins it contains. String $$$s_i$$$ contains from $$$1$$$ to $$$3$$$ characters, and the only possible characters are "A", "B" and "C". It is guaranteed that each letter appears no more than once in each string $$$s_i$$$. The order of letters in strings $$$s_i$$$ is arbitrary. Output Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins. Examples Input 5 10 A 9 BC 11 CA 4 A 5 B Input 6 100 A 355 BCA 150 BC 160 AC 180 B 190 CA Note In the first example Petya buys the first, the second and the fourth juice. He spends $$$5 + 6 + 4 = 15$$$ and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is $$$16$$$, which isn't optimal. In the second example Petya can't obtain all three vitamins, as no juice contains vitamin "C".
1,200
false
false
true
true
false
false
true
false
false
false
5,488
855G
After destroying all of Voldemort's Horcruxes, Harry and Voldemort are up for the final battle. They each cast spells from their wands and the spells collide. The battle scene is Hogwarts, which can be represented in the form of a tree. There are, in total, _n_ places in Hogwarts joined using _n_u2009-u20091 undirected roads. Ron, who was viewing this battle between Harry and Voldemort, wondered how many triplets of places (_u_,u2009_v_,u2009_w_) are there such that if Harry is standing at place _u_ and Voldemort is standing at place _v_, their spells collide at a place _w_. This is possible for a triplet only when _u_, _v_ and _w_ are distinct, and there exist paths from _u_ to _w_ and from _v_ to _w_ which do not pass through the same roads. Now, due to the battle havoc, new paths are being added all the time. You have to tell Ron the answer after each addition. Formally, you are given a tree with _n_ vertices and _n_u2009-u20091 edges. _q_ new edges are being added between the nodes of the tree. After each addition you need to tell the number of triplets (_u_,u2009_v_,u2009_w_) such that _u_, _v_ and _w_ are distinct and there exist two paths, one between _u_ and _w_, another between _v_ and _w_ such that these paths do not have an edge in common. Input First line contains an integer _n_ (1u2009≀u2009_n_u2009≀u2009105), the number of places in Hogwarts. Each of the next _n_u2009-u20091 lines contains two space separated integers _u_ and _v_ (1u2009≀u2009_u_,u2009_v_u2009≀u2009_n_) indicating a road between places _u_ and _v_. It is guaranteed that the given roads form a connected tree. Next line contains a single integer _q_ (1u2009≀u2009_q_u2009≀u2009105), the number of new edges being added. Each of the next _q_ lines contains two space separated integers _u_ and _v_ (1u2009≀u2009_u_,u2009_v_u2009≀u2009_n_) representing the new road being added. Note that it is possible that a newly added road connects places that were connected by a road before. Also, a newly added road may connect a place to itself. Output In the first line print the value for the number of triplets before any changes occurred. After that print _q_ lines, a single integer _ans__i_ in each line containing the value for the number of triplets after _i_-th edge addition. Note In the first sample case, for the initial tree, we have (1,u20093,u20092) and (3,u20091,u20092) as the only possible triplets (_u_,u2009_v_,u2009_w_). After addition of edge from 2 to 3, we have (1,u20093,u20092), (3,u20091,u20092), (1,u20092,u20093) and (2,u20091,u20093) as the possible triplets.
3,300
false
false
false
true
false
false
false
false
false
true
6,321
1454E
You are given an undirected graph consisting of $$$n$$$ vertices and $$$n$$$ edges. It is guaranteed that the given graph is connected (i.xa0e. it is possible to reach any vertex from any other vertex) and there are no self-loops and multiple edges in the graph. Your task is to calculate the number of simple paths of length at least $$$1$$$ in the given graph. Note that paths that differ only by their direction are considered the same (i.xa0e. you have to calculate the number of undirected paths). For example, paths $$$[1, 2, 3]$$$ and $$$[3, 2, 1]$$$ are considered the same. You have to answer $$$t$$$ independent test cases. Recall that a path in the graph is a sequence of vertices $$$v_1, v_2, ldots, v_k$$$ such that each pair of adjacent (consecutive) vertices in this sequence is connected by an edge. The length of the path is the number of edges in it. A simple path is such a path that all vertices in it are distinct. 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$$$ ($$$3 le n le 2 cdot 10^5$$$) β€” the number of vertices (and the number of edges) in the graph. The next $$$n$$$ lines of the test case describe edges: edge $$$i$$$ is given as a pair of vertices $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$), where $$$u_i$$$ and $$$v_i$$$ are vertices the $$$i$$$-th edge connects. For each pair of vertices $$$(u, v)$$$, there is at most one edge between $$$u$$$ and $$$v$$$. There are no edges from the vertex to itself. So, there are no self-loops and multiple edges in the graph. The graph is undirected, i.xa0e. all its edges are bidirectional. The graph is connected, i.xa0e. it is possible to reach any vertex from any other vertex by moving along the edges of the graph. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print one integer: the number of simple paths of length at least $$$1$$$ in the given graph. Note that paths that differ only by their direction are considered the same (i.xa0e. you have to calculate the number of undirected paths). Example Input 3 3 1 2 2 3 1 3 4 1 2 2 3 3 4 4 2 5 1 2 2 3 1 3 2 5 4 3 Note Consider the second test case of the example. It looks like that: There are $$$11$$$ different simple paths: 1. $$$[1, 2]$$$; 2. $$$[2, 3]$$$; 3. $$$[3, 4]$$$; 4. $$$[2, 4]$$$; 5. $$$[1, 2, 4]$$$; 6. $$$[1, 2, 3]$$$; 7. $$$[2, 3, 4]$$$; 8. $$$[2, 4, 3]$$$; 9. $$$[3, 2, 4]$$$; 10. $$$[1, 2, 3, 4]$$$; 11. $$$[1, 2, 4, 3]$$$.
2,000
false
false
false
false
false
false
false
false
false
true
3,404
258D
The Little Elephant loves permutations of integers from 1 to _n_ very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1,u20092,u20093,u2009...,u2009_n_. This time the Little Elephant has permutation _p_1,u2009_p_2,u2009...,u2009_p__n_. Its sorting program needs to make exactly _m_ moves, during the _i_-th move it swaps elements that are at that moment located at the _a__i_-th and the _b__i_-th positions. But the Little Elephant's sorting program happened to break down and now on every step it can equiprobably either do nothing or swap the required elements. Now the Little Elephant doesn't even hope that the program will sort the permutation, but he still wonders: if he runs the program and gets some permutation, how much will the result of sorting resemble the sorted one? For that help the Little Elephant find the mathematical expectation of the number of permutation inversions after all moves of the program are completed. We'll call a pair of integers _i_,u2009_j_ (1u2009≀u2009_i_u2009<u2009_j_u2009≀u2009_n_) an inversion in permutatuon _p_1,u2009_p_2,u2009...,u2009_p__n_, if the following inequality holds: _p__i_u2009>u2009_p__j_. Input The first line contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u20091000,u2009_n_u2009>u20091) β€” the permutation size and the number of moves. The second line contains _n_ distinct integers, not exceeding _n_ β€” the initial permutation. Next _m_ lines each contain two integers: the _i_-th line contains integers _a__i_ and _b__i_ (1u2009≀u2009_a__i_,u2009_b__i_u2009≀u2009_n_,u2009_a__i_u2009β‰ u2009_b__i_) β€” the positions of elements that were changed during the _i_-th move.
2,600
true
false
false
true
false
false
false
false
false
false
8,799
73E
Vasya plays The Elder Trolls III: Morrowindows. He has a huge list of items in the inventory, however, there is no limits on the size of things. Vasya does not know the total amount of items but he is sure that are not more than _x_ and not less than 2 items in his inventory. A new patch for the game appeared to view inventory in _n_ different modes. Displaying in mode _i_ is a partition of all inventory items on pages, each of which (except for maybe the last one) shows exactly _a__i_ items. In addition, each mode shows how many pages _b__i_ is in a complete list. Great! Perhaps this information will be enough for Vasya to find the required number. Moreover, it is very interesting, what is the fewest number of modes in which Vasya can see inventory to determine the number of items in it? Vasya cannot use the information that was received while looking on inventory in some mode for selection of next actions. I. e. Vasya chooses some set of modes first, and then sees all the results and determines the size. Knowing the number of _a__i_, _x_ and assuming that Vasya is very smart, check whether he can uniquely determine the number of items in his inventory, and how many modes he will need to do that if he knows numbers _a__i_, _x_ and he is able to know number _b__i_ after viewing items in mode _i_. Input The first line contains two integers _n_ and _x_ (0u2009≀u2009_n_u2009≀u2009105,u20092u2009≀u2009_x_u2009≀u2009109). The second line contains integers _a__i_ (1u2009≀u2009_a__i_u2009≀u2009109). Some numbers among all _a__i_ may be equal. Output Output the fewest amount of modes required to uniquely determine amount of items in the inventory. If there is no solution output u2009-u20091. Note In the second example Vasya is not able to determine items count uniquely because 3 items, as well as 4 items, can be displayed on two pages.
2,400
true
false
false
false
false
false
false
false
false
false
9,598
1851A
One day, Vlad became curious about who he can have a conversation with on the escalator in the subway. There are a total of $$$n$$$ passengers. The escalator has a total of $$$m$$$ steps, all steps indexed from $$$1$$$ to $$$m$$$ and $$$i$$$-th step has height $$$i cdot k$$$. Vlad's height is $$$H$$$ centimeters. Two people with heights $$$a$$$ and $$$b$$$ can have a conversation on the escalator if they are standing on different steps and the height difference between them is equal to the height difference between the steps. For example, if two people have heights $$$170$$$ and $$$180$$$ centimeters, and $$$m = 10, k = 5$$$, then they can stand on steps numbered $$$7$$$ and $$$5$$$, where the height difference between the steps is equal to the height difference between the two people: $$$k cdot 2 = 5 cdot 2 = 10 = 180 - 170$$$. There are other possible ways. Given an array $$$h$$$ of size $$$n$$$, where $$$h_i$$$ represents the height of the $$$i$$$-th person. Vlad is interested in how many people he can have a conversation with on the escalator individually. For example, if $$$n = 5, m = 3, k = 3, H = 11$$$, and $$$h = [5, 4, 14, 18, 2]$$$, Vlad can have a conversation with the person with height $$$5$$$ (Vlad will stand on step $$$1$$$, and the other person will stand on step $$$3$$$) and with the person with height $$$14$$$ (for example, Vlad can stand on step $$$3$$$, and the other person will stand on step $$$2$$$). Vlad cannot have a conversation with the person with height $$$2$$$ because even if they stand on the extreme steps of the escalator, the height difference between them will be $$$6$$$, while their height difference is $$$9$$$. Vlad cannot have a conversation with the rest of the people on the escalator, so the answer for this example is $$$2$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains integers: $$$n, m, k, H$$$ ($$$1 le n,m le 50$$$, $$$1 le k,H le 10^6$$$). Here, $$$n$$$ is the number of people, $$$m$$$ is the number of steps, $$$k$$$ is the height difference between neighboring steps, and $$$H$$$ is Vlad's height. The second line contains $$$n$$$ integers: $$$h_1, h_2, ldots, h_n$$$ ($$$1 le h_i le 10^6$$$). Here, $$$h_i$$$ represents the height of the $$$i$$$-th person. Output For each test case, output a single integerxa0β€” the number of people Vlad can have a conversation with on the escalator individually. Example Input 7 5 3 3 11 5 4 14 18 2 2 9 5 6 11 9 10 50 3 11 43 44 74 98 62 60 99 4 11 73 4 8 8 49 68 58 82 73 7 1 4 66 18 66 39 83 48 99 79 9 1 1 13 26 23 84 6 60 87 40 41 25 6 13 3 28 30 70 85 13 1 55 Note The first example is explained in the problem statement. In the second example, Vlad can have a conversation with the person with height $$$11$$$. In the third example, Vlad can have a conversation with people with heights: $$$44, 74, 98, 62$$$. Therefore, the answer is $$$4$$$. In the fourth example, Vlad can have a conversation with the person with height $$$73$$$.
800
true
false
false
false
false
true
true
false
false
false
1,174
209A
Problem - 209A - Codeforces =============== xa0 and (blue) are zebroids and sequence (red; red) is not a zebroid. Now Polycarpus wonders, how many ways there are to pick a zebroid subsequence from this sequence. Help him solve the problem, find the number of ways modulo 1000000007 (109u2009+u20097). Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009106) β€” the number of marbles in Polycarpus's sequence. Output Print a single number β€” the answer to the problem modulo 1000000007 (109u2009+u20097). Examples Input 3 Output 6 Input 4 Output 11 Note Let's consider the first test sample. Let's assume that Polycarpus initially had sequence (red; blue; red), so there are six ways to pick a zebroid: pick the first marble; pick the second marble; pick the third marble; pick the first and second marbles; pick the second and third marbles; pick the first, second and third marbles. It can be proven that if Polycarpus picks (blue; red; blue) as the initial sequence, the number of ways won't change.
1,600
true
false
false
true
false
false
false
false
false
false
8,996
1526A
You are given an array $$$a$$$ of $$$2n$$$ distinct integers. You want to arrange the elements of the array in a circle such that no element is equal to the the arithmetic mean of its $$$2$$$ neighbours. More formally, find an array $$$b$$$, such that: $$$b$$$ is a permutation of $$$a$$$. For every $$$i$$$ from $$$1$$$ to $$$2n$$$, $$$b_i eq frac{b_{i-1}+b_{i+1}}{2}$$$, where $$$b_0 = b_{2n}$$$ and $$$b_{2n+1} = b_1$$$. It can be proved that under the constraints of this problem, such array $$$b$$$ always exists. Input The first line of input contains a single integer $$$t$$$ $$$(1 leq t leq 1000)$$$ β€” the number of testcases. The description of testcases follows. The first line of each testcase contains a single integer $$$n$$$ $$$(1 leq n leq 25)$$$. The second line of each testcase contains $$$2n$$$ integers $$$a_1, a_2, ldots, a_{2n}$$$ $$$(1 leq a_i leq 10^9)$$$ β€” elements of the array. Note that there is no limit to the sum of $$$n$$$ over all testcases. Output For each testcase, you should output $$$2n$$$ integers, $$$b_1, b_2, ldots b_{2n}$$$, for which the conditions from the statement are satisfied. Example Input 3 3 1 2 3 4 5 6 2 123 456 789 10 1 6 9 Output 3 1 4 2 5 6 123 10 456 789 9 6 Note In the first testcase, array $$$[3, 1, 4, 2, 5, 6]$$$ works, as it's a permutation of $$$[1, 2, 3, 4, 5, 6]$$$, and $$$frac{3+4}{2} eq 1$$$, $$$frac{1+2}{2} eq 4$$$, $$$frac{4+5}{2} eq 2$$$, $$$frac{2+6}{2} eq 5$$$, $$$frac{5+3}{2} eq 6$$$, $$$frac{6+1}{2} eq 3$$$.
800
false
false
false
false
false
true
false
false
true
false
3,036
1891A
You are given an array of integers $$$a_1, a_2, ldots, a_n$$$. In one operation, you do the following: Choose a non-negative integer $$$m$$$, such that $$$2^m leq n$$$. Subtract $$$1$$$ from $$$a_i$$$ for all integers $$$i$$$, such that $$$1 leq i leq 2^m$$$. Can you sort the array in non-decreasing order by performing some number (possibly zero) of operations? An array is considered non-decreasing if $$$a_i leq a_{i + 1}$$$ for all integers $$$i$$$ such that $$$1 leq i leq n - 1$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 20$$$) β€” the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ β€” the integers in array $$$a$$$ ($$$0 leq a_i leq 1000$$$). Note In the first test case, the array is already sorted in non-decreasing order, so we don't have to perform any operations. In the second test case, we can choose $$$m = 1$$$ twice to get the array $$$[4, 3, 3, 4, 4]$$$. Then, we can choose $$$m = 0$$$ once and get the sorted in non-decreasing order array $$$[3, 3, 3, 4, 4]$$$. In the third test case, we can choose $$$m = 0$$$ once and get the array $$$[5, 5, 5, 7, 5, 6, 6, 8, 7]$$$. Then, we can choose $$$m = 2$$$ twice and get the array $$$[3, 3, 3, 5, 5, 6, 6, 8, 7]$$$. After that, we can choose $$$m = 3$$$ once and get the sorted in non-decreasing order array $$$[2, 2, 2, 4, 4, 5, 5, 7, 7]$$$. For the fourth and fifth test case, it can be shown that the array could not be sorted using these operations.
800
false
false
false
false
false
true
false
false
true
false
957
1423G
Sarah has always been a lover of nature, and a couple of years ago she saved up enough money to travel the world and explore all the things built by nature over its lifetime on earth. During this time she visited some truly special places which were left untouched for centuries, from watching icebergs in freezing weather to scuba-diving in oceans and admiring the sea life, residing unseen. These experiences were enhanced with breathtaking views built by mountains over time and left there for visitors to see for years on end. Over time, all these expeditions took a toll on Sarah and culminated in her decision to settle down in the suburbs and live a quiet life. However, as Sarah's love for nature never faded, she started growing flowers in her garden in an attempt to stay connected with nature. At the beginning she planted only blue orchids, but over time she started using different flower types to add variety to her collection of flowers. This collection of flowers can be represented as an array of $$$N$$$ flowers and the $$$i$$$-th of them has a type associated with it, denoted as $$$A_i$$$. Each resident, passing by her collection and limited by the width of his view, can only see $$$K$$$ contiguous flowers at each moment in time. To see the whole collection, the resident will look at the first $$$K$$$ contiguous flowers $$$A_1, A_2, ..., A_K$$$, then shift his view by one flower and look at the next section of K contiguous flowers $$$A_2, A_3, ..., A_{K+1}$$$ and so on until they scan the whole collection, ending with section $$$A_{N-K+1}, ..., A_{N-1}, A_N$$$. Each resident determines the beautiness of a section of $$$K$$$ flowers as the number of distinct flower types in that section. Furthermore, the beautiness of the whole collection is calculated by summing the beautiness values of each contiguous section. Formally, beautiness $$$B_i$$$ of a section starting at the $$$i$$$-th position is calculated as $$$B_i = distinct(A_i, A_{i+1}, ..., A_{i+K-1})$$$, and beautiness of the collection $$$B$$$ is calculated as $$$B=B_1 + B_2 + ... + B_{N-K+1}$$$. In addition, as Sarah wants to keep her collection of flowers have a fresh feel, she can also pick two points $$$L$$$ and $$$R$$$, dispose flowers between those two points and plant new flowers, all of them being the same type. You will be given $$$Q$$$ queries and each of those queries will be of the following two types: 1. You will be given three integers $$$L, R, X$$$ describing that Sarah has planted flowers of type $$$X$$$ between positions $$$L$$$ and $$$R$$$ inclusive. Formally collection is changed such that $$$A[i]=X$$$ for all $$$i$$$ in range $$$[L.. R]$$$. 2. You will be given integer $$$K$$$, width of the resident's view and you have to determine the beautiness value $$$B$$$ resident has associated with the collection For each query of second type print the result – beautiness $$$B$$$ of the collection. Input First line contains two integers $$$N$$$ and $$$Q ;(1 leq N, Q leq 10^5),$$$ β€” number of flowers and the number of queries, respectively. The second line contains $$$N$$$ integers $$$A_1, A_2, ..., A_N;(1 leq A_i leq 10^9),$$$ β€” where $$$A_i$$$ represents type of the $$$i$$$-th flower. Each of the next $$$Q$$$ lines describe queries and start with integer $$$Tin{1, 2}$$$. If $$$T = 1$$$, there will be three more integers in the line $$$L, R, X;(1 leq L, R leq N;; 1 leq X leq 10^9),$$$ β€” $$$L$$$ and $$$R$$$ describing boundaries and $$$X$$$ describing the flower type If $$$T = 2$$$, there will be one more integer in the line $$$K;(1 leq K leq N),$$$ β€” resident's width of view Output For each query of the second type print the beautiness $$$B$$$ of the collection. Example Input 5 5 1 2 3 4 5 2 3 1 1 2 5 2 4 1 2 4 5 2 2 Note Let's look at the example. Initially the collection is $$$[1, 2, 3, 4, 5]$$$. In the first query $$$K = 3$$$, we consider sections of three flowers with the first being $$$[1, 2, 3]$$$. Since beautiness of the section is the number of distinct flower types in that section, $$$B_1 = 3$$$. Second section is $$$[2, 3, 4]$$$ and $$$B_2 = 3$$$. Third section is $$$[3, 4, 5]$$$ and $$$B_3 = 3$$$, since the flower types are all distinct. The beautiness value resident has associated with the collection is $$$B = B_1 + B_2 + B_3 = 3 + 3 + 3 = 9$$$. After the second query, the collection becomes $$$[5, 5, 3, 4, 5]$$$. For the third query $$$K = 4$$$, so we consider sections of four flowers with the first being $$$[5, 5, 3, 4]$$$. There are three distinct flower types $$$[5, 3, 4]$$$ in this section, so $$$B_1 = 3$$$. Second section $$$[5, 3, 4, 5]$$$ also has $$$3$$$ distinct flower types, so $$$B_2 = 3$$$. The beautiness value resident has associated with the collection is $$$B = B_1 + B_2 = 3 + 3 = 6$$$ After the fourth query, the collection becomes $$$[5, 5, 5, 5, 5]$$$. For the fifth query $$$K = 2$$$ and in this case all the four sections are same with each of them being $$$[5, 5]$$$. Beautiness of $$$[5, 5]$$$ is $$$1$$$ since there is only one distinct element in this section $$$[5]$$$. Beautiness of the whole collection is $$$B = B_1 + B_2 + B_3 + B_4 = 1 + 1 + 1 + 1 = 4$$$
3,500
false
false
false
false
true
false
false
false
false
false
3,557
343A
Mad scientist Mike is building a time machine in his spare time. To finish the work, he needs a resistor with a certain resistance value. However, all Mike has is lots of identical resistors with unit resistance _R_0u2009=u20091. Elements with other resistance can be constructed from these resistors. In this problem, we will consider the following as elements: 1. one resistor; 2. an element and one resistor plugged in sequence; 3. an element and one resistor plugged in parallel. With the consecutive connection the resistance of the new element equals _R_u2009=u2009_R__e_u2009+u2009_R_0. With the parallel connection the resistance of the new element equals . In this case _R__e_ equals the resistance of the element being connected. Mike needs to assemble an element with a resistance equal to the fraction . Determine the smallest possible number of resistors he needs to make such an element. Input The single input line contains two space-separated integers _a_ and _b_ (1u2009≀u2009_a_,u2009_b_u2009≀u20091018). It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists. Output Print a single number β€” the answer to the problem. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specifier. Note In the first sample, one resistor is enough. In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively. Then, we get an element with resistance . We cannot make this element using two resistors.
1,600
true
false
false
false
false
false
false
false
false
false
8,463
678C
Problem - 678C - Codeforces =============== xa0 ]( "Educational Codeforces Round 13") . Output Print the only integer _s_ β€” the maximum number of chocolates Joty can get. Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. Examples Input 5 2 3 12 15 Output 39 Input 20 2 3 3 5 Output 51
1,600
true
false
true
false
false
false
false
false
false
false
7,114
138D
Recently Roma has become the happy owner of a new game World of Darkraft. This game combines elements of virtually all known genres, and on one of the later stages of the game Roma faced difficulties solving a puzzle. In this part Roma fights with a cunning enemy magician. The battle takes place on a rectangular field plaid _n_u2009Γ—u2009_m_. Each cell contains one magical character: L, R or X. Initially all the squares of the field are "active". The players, Roma and enemy magician, take turns. Roma makes the first move. During a move a player selects one of the active cells. Then depending on the image in the character in the cell one of the following actions takes place: L β€” magical waves radiate from the cell to the left downwards and to the right upwards along diagonal paths. All cells on the path of the waves (including the selected cell too) become inactive. The waves continue until the next inactive cell or to the edge of the field if there are no inactive cells on the way. R β€” the magical waves radiate to the left upwards and to the right downwards. X β€” the magical waves radiate in all four diagonal directions. If the next player cannot make a move (i.e., all cells are inactive), he loses. Roma has been trying to defeat the computer opponent for three days but he just keeps losing. He asks you to help him and determine whether it is guaranteed that he can beat the opponent, or he will have to hack the game. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u200920). Next _n_ lines contain _m_ characters describing the playing field: the _j_-th character of the _i_-th line equals to the magical character of the corresponding field square. Output On the first line print "WIN" if Roma can win or "LOSE" if it is impossible to win considering that the opponent pays optimally. Note In the first test each move makes one diagonal line of the square inactive, thus it is guaranteed that Roma loses after two moves. There are three variants of making a move in the second test: to "finish off" the main diagonal line or any of the squares that are left. That means that after three moves the game stops and Roma wins.
2,500
false
false
false
true
false
false
false
false
false
false
9,322
787A
Problem - 787A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags brute force math number theory *1200 No tag edit access β†’ Contest materials or that they will never scream at the same time. Input The first line of input contains two integers _a_ and _b_ (1u2009≀u2009_a_,u2009_b_u2009≀u2009100). The second line contains two integers _c_ and _d_ (1u2009≀u2009_c_,u2009_d_u2009≀u2009100). Output Print the first time Rick and Morty will scream at the same time, or u2009-u20091 if they will never scream at the same time. Examples Input 20 2 9 19 Output 82 Input 2 1 16 12 Output -1 Note In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82. In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time.
1,200
true
false
false
false
false
false
true
false
false
false
6,623
1956A
Nene invented a new game based on an increasing sequence of integers $$$a_1, a_2, ldots, a_k$$$. In this game, initially $$$n$$$ players are lined up in a row. In each of the rounds of this game, the following happens: Nene finds the $$$a_1$$$-th, $$$a_2$$$-th, $$$ldots$$$, $$$a_k$$$-th players in a row. They are kicked out of the game simultaneously. If the $$$i$$$-th player in a row should be kicked out, but there are fewer than $$$i$$$ players in a row, they are skipped. Once no one is kicked out of the game in some round, all the players that are still in the game are declared as winners. For example, consider the game with $$$a=[3, 5]$$$ and $$$n=5$$$ players. Let the players be named player A, player B, $$$ldots$$$, player E in the order they are lined up initially. Then, Before the first round, players are lined up as ABCDE. Nene finds the $$$3$$$-rd and the $$$5$$$-th players in a row. These are players C and E. They are kicked out in the first round. Now players are lined up as ABD. Nene finds the $$$3$$$-rd and the $$$5$$$-th players in a row. The $$$3$$$-rd player is player D and there is no $$$5$$$-th player in a row. Thus, only player D is kicked out in the second round. In the third round, no one is kicked out of the game, so the game ends after this round. Players A and B are declared as the winners. Nene has not yet decided how many people would join the game initially. Nene gave you $$$q$$$ integers $$$n_1, n_2, ldots, n_q$$$ and you should answer the following question for each $$$1 le i le q$$$ independently: How many people would be declared as winners if there are $$$n_i$$$ players in the game initially? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 250$$$). The description of test cases follows. The first line case contains two integers $$$k$$$ and $$$q$$$ ($$$1 le k, q le 100$$$)xa0β€” the length of the sequence $$$a$$$ and the number of values $$$n_i$$$ you should solve this problem for. The second line contains $$$k$$$ integers $$$a_1,a_2,ldots,a_k$$$ ($$$1leq a_1<a_2<ldots<a_kleq 100$$$)xa0β€” the sequence $$$a$$$. The third line contains $$$q$$$ integers $$$n_1,n_2,ldots,n_q$$$ ($$$1leq n_i leq 100$$$). Output For each test case, output $$$q$$$ integers: the $$$i$$$-th ($$$1le i le q$$$) of them should be the number of players declared as winners if initially $$$n_i$$$ players join the game. Example Input 6 2 1 3 5 5 5 3 2 4 6 7 9 1 3 5 5 4 3 4 5 6 7 1 2 3 4 2 3 69 96 1 10 100 1 1 100 50 3 3 10 20 30 1 10 100 Output 2 1 1 1 1 2 2 2 1 10 68 50 1 9 9 Note The first test case was explained in the statement. In the second test case, when $$$n=1$$$, the only player stays in the game in the first round. After that, the game ends and the only player is declared as a winner.
800
false
true
false
false
true
false
true
true
false
false
540
1421A
Problem - 1421A - 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 greedy math *800 No tag edit access β†’ Contest materials ") Editorial") + ($$$b oplus x$$$) for any given $$$x$$$, where $$$oplus$$$ denotes the . Description of the test cases follows. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^{9}$$$). Output For each testcase, output the smallest possible value of the given expression. Example Input 6 6 12 4 9 59 832 28 14 4925 2912 1 1 Output 10 13 891 18 6237 0 Note For the first test case Sana can choose $$$x=4$$$ and the value will be ($$$6 oplus 4$$$) + ($$$12 oplus 4$$$) = $$$2 + 8$$$ = $$$10$$$. It can be shown that this is the smallest possible value.
800
true
true
false
false
false
false
false
false
false
false
3,574
1097A
Gennady owns a small hotel in the countryside where he lives a peaceful life. He loves to take long walks, watch sunsets and play cards with tourists staying in his hotel. His favorite game is called "Mau-Mau". To play Mau-Mau, you need a pack of $$$52$$$ cards. Each card has a suit (Diamonds β€” D, Clubs β€” C, Spades β€” S, or Hearts β€” H), and a rank (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A). At the start of the game, there is one card on the table and you have five cards in your hand. You can play a card from your hand if and only if it has the same rank or the same suit as the card on the table. In order to check if you'd be a good playing partner, Gennady has prepared a task for you. Given the card on the table and five cards in your hand, check if you can play at least one card. Input The first line of the input contains one string which describes the card on the table. The second line contains five strings which describe the cards in your hand. Each string is two characters long. The first character denotes the rank and belongs to the set $$${{ t 2}, { t 3}, { t 4}, { t 5}, { t 6}, { t 7}, { t 8}, { t 9}, { t T}, { t J}, { t Q}, { t K}, { t A}}$$$. The second character denotes the suit and belongs to the set $$${{ t D}, { t C}, { t S}, { t H}}$$$. All the cards in the input are different. Output If it is possible to play a card from your hand, print one word "YES". Otherwise, print "NO". You can print each letter in any case (upper or lower). Note In the first example, there is an Ace of Spades (AS) on the table. You can play an Ace of Diamonds (AD) because both of them are Aces. In the second example, you cannot play any card. In the third example, you can play an Ace of Diamonds (AD) because it has the same suit as a Four of Diamonds (4D), which lies on the table.
800
false
false
true
false
false
false
true
false
false
false
5,224
1789D
Serval has two $$$n$$$-bit binary integer numbers $$$a$$$ and $$$b$$$. He wants to share those numbers with Toxel. Since Toxel likes the number $$$b$$$ more, Serval decides to change $$$a$$$ into $$$b$$$ by some (possibly zero) operations. In an operation, Serval can choose any positive integer $$$k$$$ between $$$1$$$ and $$$n$$$, and change $$$a$$$ into one of the following number: $$$aoplus(all k)$$$ $$$aoplus(agg k)$$$ In other words, the operation moves every bit of $$$a$$$ left or right by $$$k$$$ positions, where the overflowed bits are removed, and the missing bits are padded with $$$0$$$. The bitwise XOR of the shift result and the original $$$a$$$ is assigned back to $$$a$$$. Serval does not have much time. He wants to perform no more than $$$n$$$ operations to change $$$a$$$ into $$$b$$$. Please help him to find out an operation sequence, or determine that it is impossible to change $$$a$$$ into $$$b$$$ in at most $$$n$$$ operations. You do not need to minimize the number of operations. In this problem, $$$xoplus y$$$ denotes the . The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle2cdot10^{3}$$$) β€” the number of bits in numbers $$$a$$$ and $$$b$$$. The second and the third line of each test case contain a binary string of length $$$n$$$, representing $$$a$$$ and $$$b$$$, respectively. The strings contain only characters 0 and 1. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^{3}$$$. Output For each test case, if it is impossible to change $$$a$$$ into $$$b$$$ in at most $$$n$$$ operations, print a single integer $$$-1$$$. Otherwise, in the first line, print the number of operations $$$m$$$ ($$$0le mle n$$$). If $$$m>0$$$, in the second line, print $$$m$$$ integers $$$k_{1},k_{2},dots,k_{m}$$$ representing the operations. If $$$1le k_{i}le n$$$, it means logical left shift $$$a$$$ by $$$k_{i}$$$ positions. If $$$-nle k_{i}le-1$$$, it means logical right shift $$$a$$$ by $$$-k_{i}$$$ positions. If there are multiple solutions, print any of them. Example Input 3 5 00111 11000 1 1 1 3 001 000 Note In the first test case: The first operation changes $$$a$$$ into $$$ equire{cancel}00111opluscancel{001}11underline{000}=11111$$$. The second operation changes $$$a$$$ into $$$ equire{cancel}11111oplusunderline{00}111cancel{11}=11000$$$. The bits with strikethroughs are overflowed bits that are removed. The bits with underline are padded bits. In the second test case, $$$a$$$ is already equal to $$$b$$$, so no operations are needed. In the third test case, it can be shown that $$$a$$$ cannot be changed into $$$b$$$.
2,200
false
false
true
false
false
true
true
false
false
false
1,545
1379E
Ivan is fond of genealogy. Currently he is studying a particular genealogical structure, which consists of some people. In this structure every person has either both parents specified, or none. Additionally, each person has exactly one child, except for one special person, who does not have any children. The people in this structure are conveniently numbered from $$$1$$$ to $$$n$$$, and $$$s_i$$$ denotes the child of the person $$$i$$$ (and $$$s_i = 0$$$ for exactly one person who does not have any children). We say that $$$a$$$ is an ancestor of $$$b$$$ if either $$$a = b$$$, or $$$a$$$ has a child, who is an ancestor of $$$b$$$. That is $$$a$$$ is an ancestor for $$$a$$$, $$$s_a$$$, $$$s_{s_a}$$$, etc. We say that person $$$i$$$ is imbalanced in case this person has both parents specified, and the total number of ancestors of one of the parents is at least double the other. Ivan counted the number of imbalanced people in the structure, and got $$$k$$$ people in total. However, he is not sure whether he computed it correctly, and would like to check if there is at least one construction with $$$n$$$ people that have $$$k$$$ imbalanced people in total. Please help him to find one such construction, or determine if it does not exist. Input The input contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 100,000$$$, $$$0 leq k leq n$$$), the total number of people and the number of imbalanced people. Output If there are no constructions with $$$n$$$ people and $$$k$$$ imbalanced people, output NO. Otherwise output YES on the first line, and then $$$n$$$ integers $$$s_1, s_2, ldots, s_n$$$ ($$$0 leq s_i leq n$$$), which describes the construction and specify the child of each node (or 0, if the person does not have any children). Note In the first example case one can have a construction with 3 people, where 1 person has 2 parents. In the second example case one can use the following construction: Only person 1 is imbalanced, because one of their parents has 1 ancestor in total, and the other parent has 3 ancestors.
2,800
true
false
false
true
false
true
false
false
false
false
3,774
1368E
# Ski Accidents Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes Arthur owns a ski resort on a mountain. There are n landing spots on the mountain numbered from 1 to n from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot , but many tracks may enter the same spot. A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks. Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable. Formally, after closing some of the spots, there should not be a path that consists of two or more tracks .Arthur doesn’t want to close too many spots. He will be happy to find any way to close at most 47 n spots so that the remaining part is safe. Help him find any suitable way to do so. # Input The first line contains a single positive integer T x16 the number of test cases. T test case description follows. The first line of each description contains two integers n and m (1 ≀ n ≀ 2 Β· 10 5) x16 the number of landing spots and tracks respectively. The following m lines describe the tracks. Each of these lines contains two integers x and y (1 ≀ x < y ≀ n) x16 indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide. It is guaranteed that the sum of n over all test cases does not exceed 2 Β· 10 5. # Output For each test case, print a single integer k (0 ≀ k ≀ 47 n) x16 the number of spots to be closed. In the next line, print k distinct integers x16 indices of all spots to be closed, in any order. If there are several answers, you may output any of them. Note that you don’t have to minimize k. It can be shown that a suitable answer always exists. Page 1 of 2 Example standard input standard output 24 6 1 2 1 3 2 3 2 4 3 4 3 4 7 6 1 2 1 3 2 4 2 5 3 6 3 7 23 4 44 5 6 7 # Note In the first sample case, closing any two spots is suitable. In the second sample case, closing only the spot 1 is also suitable. Page 2 of 2
2,500
false
true
false
false
false
true
false
false
false
true
3,832
261B
Maxim has opened his own restaurant! The restaurant has got a huge table, the table's length is _p_ meters. Maxim has got a dinner party tonight, _n_ guests will come to him. Let's index the guests of Maxim's restaurant from 1 to _n_. Maxim knows the sizes of all guests that are going to come to him. The _i_-th guest's size (_a__i_) represents the number of meters the guest is going to take up if he sits at the restaurant table. Long before the dinner, the guests line up in a queue in front of the restaurant in some order. Then Maxim lets the guests in, one by one. Maxim stops letting the guests in when there is no place at the restaurant table for another guest in the queue. There is no place at the restaurant table for another guest in the queue, if the sum of sizes of all guests in the restaurant plus the size of this guest from the queue is larger than _p_. In this case, not to offend the guest who has no place at the table, Maxim doesn't let any other guest in the restaurant, even if one of the following guests in the queue would have fit in at the table. Maxim is now wondering, what is the average number of visitors who have come to the restaurant for all possible _n_! orders of guests in the queue. Help Maxim, calculate this number. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u200950) β€” the number of guests in the restaurant. The next line contains integers _a_1, _a_2, ..., _a__n_ (1u2009≀u2009_a__i_u2009≀u200950) β€” the guests' sizes in meters. The third line contains integer _p_ (1u2009≀u2009_p_u2009≀u200950) β€” the table's length in meters. The numbers in the lines are separated by single spaces. Output In a single line print a real number β€” the answer to the problem. The answer will be considered correct, if the absolute or relative error doesn't exceed 10u2009-u20094. Note In the first sample the people will come in the following orders: (1,u20092,u20093) β€” there will be two people in the restaurant; (1,u20093,u20092) β€” there will be one person in the restaurant; (2,u20091,u20093) β€” there will be two people in the restaurant; (2,u20093,u20091) β€” there will be one person in the restaurant; (3,u20091,u20092) β€” there will be one person in the restaurant; (3,u20092,u20091) β€” there will be one person in the restaurant. In total we get (2u2009+u20091u2009+u20092u2009+u20091u2009+u20091u2009+u20091)u2009/u20096 = 8u2009/u20096 = 1.(3).
1,900
true
false
false
true
false
false
false
false
false
false
8,789
1139B
You went to the store, selling $$$n$$$ types of chocolates. There are $$$a_i$$$ chocolates of type $$$i$$$ in stock. You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $$$x_i$$$ chocolates of type $$$i$$$ (clearly, $$$0 le x_i le a_i$$$), then for all $$$1 le j < i$$$ at least one of the following must hold: $$$x_j = 0$$$ (you bought zero chocolates of type $$$j$$$) $$$x_j < x_i$$$ (you bought less chocolates of type $$$j$$$ than of type $$$i$$$) For example, the array $$$x = [0, 0, 1, 2, 10]$$$ satisfies the requirement above (assuming that all $$$a_i ge x_i$$$), while arrays $$$x = [0, 1, 0]$$$, $$$x = [5, 5]$$$ and $$$x = [3, 2]$$$ don't. Calculate the maximum number of chocolates you can buy. Input The first line contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$), denoting the number of types of chocolate. The next line contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 10^9$$$), denoting the number of chocolates of each type. Output Print the maximum number of chocolates you can buy. Note In the first example, it is optimal to buy: $$$0 + 0 + 1 + 3 + 6$$$ chocolates. In the second example, it is optimal to buy: $$$1 + 2 + 3 + 4 + 10$$$ chocolates. In the third example, it is optimal to buy: $$$0 + 0 + 0 + 1$$$ chocolates.
1,000
false
true
true
false
false
false
false
false
false
false
5,033
1599C
Little Johnny Bubbles enjoys spending hours in front of his computer playing video games. His favorite game is Bubble Strike, fast-paced bubble shooting online game for two players. Each game is set in one of the N maps, each having different terrain configuration. First phase of each game decides on which map the game will be played. The game system randomly selects three maps and shows them to the players. Each player must pick one of those three maps to be discarded. The game system then randomly selects one of the maps that were not picked by any of the players and starts the game. Johnny is deeply enthusiastic about the game and wants to spend some time studying maps, thus increasing chances to win games played on those maps. However, he also needs to do his homework, so he does not have time to study all the maps. That is why he asked himself the following question: "What is the minimum number of maps I have to study, so that the probability to play one of those maps is at least $$$P$$$"? Can you help Johnny find the answer for this question? You can assume Johnny's opponents do not know him, and they will randomly pick maps. Input The first line contains two integers $$$N$$$ ($$$3$$$ $$$leq$$$ $$$N$$$ $$$leq$$$ $$$10^{3}$$$) and $$$P$$$ ($$$0$$$ $$$leq$$$ $$$P$$$ $$$leq$$$ $$$1$$$) – total number of maps in the game and probability to play map Johnny has studied. $$$P$$$ will have at most four digits after the decimal point. Output Output contains one integer number – minimum number of maps Johnny has to study.
2,000
true
false
false
false
false
false
false
false
false
false
2,662
1495D
We define a spanning tree of a graph to be a BFS tree rooted at vertex $$$s$$$ if and only if for every node $$$t$$$ the shortest distance between $$$s$$$ and $$$t$$$ in the graph is equal to the shortest distance between $$$s$$$ and $$$t$$$ in the spanning tree. Given a graph, we define $$$f(x,y)$$$ to be the number of spanning trees of that graph that are BFS trees rooted at vertices $$$x$$$ and $$$y$$$ at the same time. You are given an undirected connected graph with $$$n$$$ vertices and $$$m$$$ edges. Calculate $$$f(i,j)$$$ for all $$$i$$$, $$$j$$$ by modulo $$$998,244,353$$$. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 le n le 400$$$, $$$0 le m le 600$$$)xa0β€” the number of vertices and the number of edges in the graph. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$a_i$$$, $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$, $$$a_i < b_i$$$), representing an edge connecting $$$a_i$$$ and $$$b_i$$$. It is guaranteed that all edges are distinct and the graph is connected. Output Print $$$n$$$ lines, each consisting of $$$n$$$ integers. The integer printed in the row $$$i$$$ and the column $$$j$$$ should be $$$f(i,j) bmod 998,244,353$$$. Examples Output 2 1 0 1 1 2 1 0 0 1 2 1 1 0 1 2 Input 8 9 1 2 1 3 1 4 2 7 3 5 3 6 4 8 2 3 3 4 Output 1 0 0 0 0 0 0 0 0 2 0 0 0 0 2 0 0 0 1 0 1 1 0 0 0 0 0 2 0 0 0 2 0 0 1 0 1 1 0 0 0 0 1 0 1 1 0 0 0 2 0 0 0 0 2 0 0 0 0 2 0 0 0 2 Note The following picture describes the first example. The tree with red edges is a BFS tree rooted at both $$$1$$$ and $$$2$$$. Similarly, the BFS tree for other adjacent pairs of vertices can be generated in this way.
2,600
true
false
false
false
false
false
false
false
false
true
3,193
1282C
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes. The exam consists of $$$n$$$ problems that can be solved in $$$T$$$ minutes. Thus, the exam begins at time $$$0$$$ and ends at time $$$T$$$. Petya can leave the exam at any integer time from $$$0$$$ to $$$T$$$, inclusive. All problems are divided into two types: easy problems β€” Petya takes exactly $$$a$$$ minutes to solve any easy problem; hard problems β€” Petya takes exactly $$$b$$$ minutes ($$$b > a$$$) to solve any hard problem. Thus, if Petya starts solving an easy problem at time $$$x$$$, then it will be solved at time $$$x+a$$$. Similarly, if at a time $$$x$$$ Petya starts to solve a hard problem, then it will be solved at time $$$x+b$$$. For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time $$$t_i$$$ ($$$0 le t_i le T$$$) at which it will become mandatory (required). If Petya leaves the exam at time $$$s$$$ and there is such a problem $$$i$$$ that $$$t_i le s$$$ and he didn't solve it, then he will receive $$$0$$$ points for the whole exam. Otherwise (i.e if he has solved all such problems for which $$$t_i le s$$$) he will receive a number of points equal to the number of solved problems. Note that leaving at time $$$s$$$ Petya can have both "mandatory" and "non-mandatory" problems solved. For example, if $$$n=2$$$, $$$T=5$$$, $$$a=2$$$, $$$b=3$$$, the first problem is hard and $$$t_1=3$$$ and the second problem is easy and $$$t_2=2$$$. Then: if he leaves at time $$$s=0$$$, then he will receive $$$0$$$ points since he will not have time to solve any problems; if he leaves at time $$$s=1$$$, he will receive $$$0$$$ points since he will not have time to solve any problems; if he leaves at time $$$s=2$$$, then he can get a $$$1$$$ point by solving the problem with the number $$$2$$$ (it must be solved in the range from $$$0$$$ to $$$2$$$); if he leaves at time $$$s=3$$$, then he will receive $$$0$$$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $$$s=4$$$, then he will receive $$$0$$$ points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time $$$s=5$$$, then he can get $$$2$$$ points by solving all problems. Thus, the answer to this test is $$$2$$$. Help Petya to determine the maximal number of points that he can receive, before leaving the exam. Input The first line contains the integer $$$m$$$ ($$$1 le m le 10^4$$$)xa0β€” the number of test cases in the test. The next lines contain a description of $$$m$$$ test cases. The first line of each test case contains four integers $$$n, T, a, b$$$ ($$$2 le n le 2cdot10^5$$$, $$$1 le T le 10^9$$$, $$$1 le a < b le 10^9$$$)xa0β€” the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively. The second line of each test case contains $$$n$$$ numbers $$$0$$$ or $$$1$$$, separated by single space: the $$$i$$$-th number means the type of the $$$i$$$-th problem. A value of $$$0$$$ means that the problem is easy, and a value of $$$1$$$ that the problem is hard. The third line of each test case contains $$$n$$$ integers $$$t_i$$$ ($$$0 le t_i le T$$$), where the $$$i$$$-th number means the time at which the $$$i$$$-th problem will become mandatory. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2cdot10^5$$$. Output Print the answers to $$$m$$$ test cases. For each set, print a single integerxa0β€” maximal number of points that he can receive, before leaving the exam.
1,800
false
true
false
false
false
false
false
false
true
false
4,283
758A
In Berland it is the holiday of equality. In honor of the holiday the king decided to equalize the welfare of all citizens in Berland by the expense of the state treasury. Totally in Berland there are _n_ citizens, the welfare of each of them is estimated as the integer in _a__i_ burles (burle is the currency in Berland). You are the royal treasurer, which needs to count the minimum charges of the kingdom on the king's present. The king can only give money, he hasn't a power to take away them. Input The first line contains the integer _n_ (1u2009≀u2009_n_u2009≀u2009100)xa0β€” the number of citizens in the kingdom. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_, where _a__i_ (0u2009≀u2009_a__i_u2009≀u2009106)xa0β€” the welfare of the _i_-th citizen. Output In the only line print the integer _S_xa0β€” the minimum number of burles which are had to spend. Note In the first example if we add to the first citizen 4 burles, to the second 3, to the third 2 and to the fourth 1, then the welfare of all citizens will equal 4. In the second example it is enough to give one burle to the third citizen. In the third example it is necessary to give two burles to the first and the third citizens to make the welfare of citizens equal 3. In the fourth example it is possible to give nothing to everyone because all citizens have 12 burles.
800
true
false
true
false
false
false
false
false
false
false
6,757