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
331B2
The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily! There are _n_ beavers, each of them has a unique id from 1 to _n_. Consider a permutation _a_1,u2009_a_2,u2009...,u2009_a__n_ of _n_ these beavers. Beavershave 5000 needs one session to shave beavers with ids from _x_ to _y_ (inclusive) if and only if there are such indices _i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__k_, that _a__i_1u2009=u2009_x_, _a__i_2u2009=u2009_x_u2009+u20091, ..., _a__i__k_u2009-u20091u2009=u2009_y_u2009-u20091, _a__i__k_u2009=u2009_y_. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1,u20092,u20093,u2009...,u2009_n_. If we can't shave beavers from _x_ to _y_ in one session, then we can split these beavers into groups [_x_,u2009_p_1], [_p_1u2009+u20091,u2009_p_2], ..., [_p__m_u2009+u20091,u2009_y_] (_x_u2009≤u2009_p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__m_u2009<u2009_y_), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs _m_u2009+u20091 working sessions to shave beavers from _x_ to _y_. All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types: what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from _x_ to _y_, inclusive? two beavers on positions _x_ and _y_ (the beavers _a__x_ and _a__y_) swapped. You can assume that any beaver can be shaved any number of times. Input The first line contains integer _n_ — the total number of beavers, 2u2009≤u2009_n_. The second line contains _n_ space-separated integers — the initial beaver permutation. The third line contains integer _q_ — the number of queries, 1u2009≤u2009_q_u2009≤u2009105. The next _q_ lines contain the queries. Each query _i_ looks as _p__i_ _x__i_ _y__i_, where _p__i_ is the query type (1 is to shave beavers from _x__i_ to _y__i_, inclusive, 2 is to swap beavers on positions _x__i_ and _y__i_). All queries meet the condition: 1u2009≤u2009_x__i_u2009<u2009_y__i_u2009≤u2009_n_. to get 30 points, you need to solve the problem with constraints: _n_u2009≤u2009100 (subproblem B1); to get 100 points, you need to solve the problem with constraints: _n_u2009≤u20093·105 (subproblems B1+B2). Note that the number of queries _q_ is limited 1u2009≤u2009_q_u2009≤u2009105 in both subproblem B1 and subproblem B2.
1,900
false
false
false
false
true
false
false
false
false
false
8,519
1540C2
This is the hard version of the problem. The only difference is that in this version $$$1 le q le 10^5$$$. You can make hacks only if both versions of the problem are solved. There is a process that takes place on arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ and length $$$n-1$$$ respectively. The process is an infinite sequence of operations. Each operation is as follows: First, choose a random integer $$$i$$$ ($$$1 le i le n-1$$$). Then, simultaneously set $$$a_i = minleft(a_i, frac{a_i+a_{i+1}-b_i}{2} ight)$$$ and $$$a_{i+1} = maxleft(a_{i+1}, frac{a_i+a_{i+1}+b_i}{2} ight)$$$ without any rounding (so values may become non-integer). See notes for an example of an operation. It can be proven that array $$$a$$$ converges, i.xa0e. for each $$$i$$$ there exists a limit $$$a_i$$$ converges to. Let function $$$F(a, b)$$$ return the value $$$a_1$$$ converges to after a process on $$$a$$$ and $$$b$$$. You are given array $$$b$$$, but not array $$$a$$$. However, you are given a third array $$$c$$$. Array $$$a$$$ is good if it contains only integers and satisfies $$$0 leq a_i leq c_i$$$ for $$$1 leq i leq n$$$. Your task is to count the number of good arrays $$$a$$$ where $$$F(a, b) geq x$$$ for $$$q$$$ values of $$$x$$$. Since the number of arrays can be very large, print it modulo $$$10^9+7$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100$$$). The second line contains $$$n$$$ integers $$$c_1, c_2 ldots, c_n$$$ ($$$0 le c_i le 100$$$). The third line contains $$$n-1$$$ integers $$$b_1, b_2, ldots, b_{n-1}$$$ ($$$0 le b_i le 100$$$). The fourth line contains a single integer $$$q$$$ ($$$1 le q le 10^5$$$). The fifth line contains $$$q$$$ space separated integers $$$x_1, x_2, ldots, x_q$$$ ($$$-10^5 le x_i le 10^5$$$). Output Output $$$q$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th query, i.xa0e. the number of good arrays $$$a$$$ where $$$F(a, b) geq x_i$$$ modulo $$$10^9+7$$$. Example Input 3 2 3 4 2 1 5 -1 0 1 -100000 100000 Note The following explanation assumes $$$b = [2, 1]$$$ and $$$c=[2, 3, 4]$$$ (as in the sample). Examples of arrays $$$a$$$ that are not good: $$$a = [3, 2, 3]$$$ is not good because $$$a_1 > c_1$$$; $$$a = [0, -1, 3]$$$ is not good because $$$a_2 < 0$$$. One possible good array $$$a$$$ is $$$[0, 2, 4]$$$. We can show that no operation has any effect on this array, so $$$F(a, b) = a_1 = 0$$$. Another possible good array $$$a$$$ is $$$[0, 1, 4]$$$. In a single operation with $$$i = 1$$$, we set $$$a_1 = min(frac{0+1-2}{2}, 0)$$$ and $$$a_2 = max(frac{0+1+2}{2}, 1)$$$. So, after a single operation with $$$i = 1$$$, $$$a$$$ becomes equal to $$$[-frac{1}{2}, frac{3}{2}, 4]$$$. We can show that no operation has any effect on this array, so $$$F(a, b) = -frac{1}{2}$$$.
2,900
true
false
false
true
false
false
false
false
false
false
2,939
1764C
Doremy's new city is under construction! The city can be regarded as a simple undirected graph with $$$n$$$ vertices. The $$$i$$$-th vertex has altitude $$$a_i$$$. Now Doremy is deciding which pairs of vertices should be connected with edges. Due to economic reasons, there should be no self-loops or multiple edges in the graph. Due to safety reasons, there should not be pairwise distinct vertices $$$u$$$, $$$v$$$, and $$$w$$$ such that $$$a_u leq a_v leq a_w$$$ and the edges $$$(u,v)$$$ and $$$(v,w)$$$ exist. Under these constraints, Doremy would like to know the maximum possible number of edges in the graph. Can you help her? Note that the constructed graph is allowed to be disconnected. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2cdot 10^5$$$)xa0— the number of vertices. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1le a_ile 10^6$$$)xa0— the altitudes of each vertex. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, there can only be at most $$$3$$$ edges in the graph. A possible construction is to connect $$$(1,3)$$$, $$$(2,3)$$$, $$$(3,4)$$$. In the picture below the red number above node $$$i$$$ is $$$a_i$$$. The following list shows all such $$$u$$$, $$$v$$$, $$$w$$$ that the edges $$$(u,v)$$$ and $$$(v,w)$$$ exist. $$$u=1$$$, $$$v=3$$$, $$$w=2$$$; $$$u=1$$$, $$$v=3$$$, $$$w=4$$$; $$$u=2$$$, $$$v=3$$$, $$$w=1$$$; $$$u=2$$$, $$$v=3$$$, $$$w=4$$$; $$$u=4$$$, $$$v=3$$$, $$$w=1$$$; $$$u=4$$$, $$$v=3$$$, $$$w=2$$$. Another possible construction is to connect $$$(1,4)$$$, $$$(2,4)$$$, $$$(3,4)$$$. An unacceptable construction is to connect $$$(1,3)$$$, $$$(2,3)$$$, $$$(2,4)$$$, $$$(3,4)$$$. Because when $$$u=4$$$, $$$v=2$$$, $$$w=3$$$, $$$a_ule a_v le a_w$$$ holds, and the respective edges exist.
1,400
false
true
false
false
false
false
false
false
false
true
1,725
297B
It is known that there are _k_ fish species in the polar ocean, numbered from 1 to _k_. They are sorted by non-decreasing order of their weight, which is a positive number. Let the weight of the _i_-th type of fish be _w__i_, then 0u2009<u2009_w_1u2009≤u2009_w_2u2009≤u2009...u2009≤u2009_w__k_ holds. Polar bears Alice and Bob each have caught some fish, and they are guessing who has the larger sum of weight of the fish he/she's caught. Given the type of the fish they've caught, determine whether it is possible that the fish caught by Alice has a strictly larger total weight than Bob's. In other words, does there exist a sequence of weights _w__i_ (not necessary integers), such that the fish caught by Alice has a strictly larger total weight? Input The first line contains three integers _n_,u2009_m_,u2009_k_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105,u20091u2009≤u2009_k_u2009≤u2009109) — the number of fish caught by Alice and Bob respectively, and the number of fish species. The second line contains _n_ integers each from 1 to _k_, the list of fish type caught by Alice. The third line contains _m_ integers each from 1 to _k_, the list of fish type caught by Bob. Note that one may have caught more than one fish for a same species. Output Output "YES" (without quotes) if it is possible, and "NO" (without quotes) otherwise. Examples Input 4 7 9 5 2 7 3 3 5 2 7 3 8 7 Note In the first sample, if _w_1u2009=u20091,u2009_w_2u2009=u20092,u2009_w_3u2009=u20092.5, then Alice has a total of 2u2009+u20092u2009+u20092u2009=u20096 weight units, while Bob only has 1u2009+u20091u2009+u20092.5u2009=u20094.5. In the second sample, the fish that Alice caught is a subset of Bob's. Therefore, the total weight of Bob’s fish is always not less than the total weight of Alice’s fish.
1,600
false
true
false
false
false
true
false
false
false
false
8,643
1903E
This is an interactive problem. Theofanis and his sister are playing the following game. They have $$$n$$$ points in a 2D plane and a starting point $$$(s_x,s_y)$$$. Each player (starting from the first player) chooses one of the $$$n$$$ points that wasn't chosen before and adds to the sum (which is initially $$$0$$$) the square of the to the new point (that the current player selected). The game ends after exactly $$$n$$$ moves (after all the points are chosen). The first player wins if the sum is even in the end. Otherwise, the second player wins. Theofanis is a very competitive person and he hates losing. Thus, he wants to choose whether he should play first or second. Can you show him, which player to choose, and how he should play to beat his sister? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0— the number of test cases. The data for each test case is only available after the end of the interaction (the end of the game) for all previous test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^{5}$$$)xa0— the number of points. The second line of each test case contains two integers $$$s_x$$$, $$$s_y$$$ ($$$0 le s_x, s_y le 10^{9}$$$)xa0— the coordinates of the starting point. Two or more points may have the same coordinates. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 le x_i, y_i le 10^{9}$$$)xa0— the coordinates of the $$$i$$$-th point. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^{5}$$$. Interaction For each test case, you should first output the player that you want to play as (First or Second). Then, you should play the game. When it's your turn, you should output the index $$$j$$$ ($$$1 le j le n$$$) of the point that you want to choose, and when it's the other player's turn, you should read the index that they choose. When all the turns are done, continue with reading the input for the next test case, or finish the program, if there are none. If you receive the integer $$$-1$$$ instead of an index or a valid value of $$$n$$$, it means your program has made an invalid move or has lost the game in the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a choice for a player, or a turn, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To hack, use the following format. The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^{5}$$$)xa0— the number of points. The second line of each test case contains two integers $$$s_x$$$, $$$s_y$$$ ($$$0 le s_x, s_y le 10^{9}$$$)xa0— the coordinates of the starting point. The $$$i$$$-th of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 le x_i, y_i le 10^{9}$$$)xa0— the coordinates of the $$$i$$$-th point. The sum of $$$n$$$ over all test cases should not exceed $$$10^{5}$$$. Note The examples above do not necessarily showcase optimal strategies or the correct player to choose. In the picture below, you can see the moves that each player made in the first example. The first player is red, and the second player is black.
2,000
true
true
false
false
false
false
false
false
false
false
892
1804A
You are given a checkerboard of size $$$201 imes 201$$$, i.xa0e. it has $$$201$$$ rows and $$$201$$$ columns. The rows of this checkerboard are numbered from $$$-100$$$ to $$$100$$$ from bottom to top. The columns of this checkerboard are numbered from $$$-100$$$ to $$$100$$$ from left to right. The notation $$$(r, c)$$$ denotes the cell located in the $$$r$$$-th row and the $$$c$$$-th column. There is a king piece at position $$$(0, 0)$$$ and it wants to get to position $$$(a, b)$$$ as soon as possible. In this problem our king is lame. Each second, the king makes exactly one of the following five moves. Skip move. King's position remains unchanged. Go up. If the current position of the king is $$$(r, c)$$$ he goes to position $$$(r + 1, c)$$$. Go down. Position changes from $$$(r, c)$$$ to $$$(r - 1, c)$$$. Go right. Position changes from $$$(r, c)$$$ to $$$(r, c + 1)$$$. Go left. Position changes from $$$(r, c)$$$ to $$$(r, c - 1)$$$. King is not allowed to make moves that put him outside of the board. The important consequence of the king being lame is that he is not allowed to make the same move during two consecutive seconds. For example, if the king goes right, the next second he can only skip, go up, down, or left. What is the minimum number of seconds the lame king needs to reach position $$$(a, b)$$$? Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. Then follow $$$t$$$ lines containing one test case description each. Each test case consists of two integers $$$a$$$ and $$$b$$$ ($$$-100 leq a, b leq 100$$$)xa0— the position of the cell that the king wants to reach. It is guaranteed that either $$$a e 0$$$ or $$$b e 0$$$. Output Print $$$t$$$ integers. The $$$i$$$-th of these integers should be equal to the minimum number of seconds the lame king needs to get to the position he wants to reach in the $$$i$$$-th test case. The king always starts at position $$$(0, 0)$$$. Example Input 5 -4 1 4 4 0 -6 -5 -4 7 -8 Note One of the possible solutions for the first example is: go down, go right, go down, go right, go down, go left, go down. One of the possible solutions for the second example is to alternate "go right" and "go up" moves $$$4$$$ times each. One of the possible solutions for the third example is to alternate "go left" and "skip" moves starting with "go left". Thus, "go left" will be used $$$6$$$ times, and "skip" will be used $$$5$$$ times.
800
true
true
false
false
false
false
false
false
false
false
1,457
1349A
For the multiset of positive integers $$$s={s_1,s_2,dots,s_k}$$$, define the Greatest Common Divisor (GCD) and Least Common Multiple (LCM) of $$$s$$$ as follow: $$$gcd(s)$$$ is the maximum positive integer $$$x$$$, such that all integers in $$$s$$$ are divisible on $$$x$$$. $$$ extrm{lcm}(s)$$$ is the minimum positive integer $$$x$$$, that divisible on all integers from $$$s$$$. For example, $$$gcd({8,12})=4,gcd({12,18,6})=6$$$ and $$$ extrm{lcm}({4,6})=12$$$. Note that for any positive integer $$$x$$$, $$$gcd({x})= extrm{lcm}({x})=x$$$. Orac has a sequence $$$a$$$ with length $$$n$$$. He come up with the multiset $$$t={ extrm{lcm}({a_i,a_j}) i<j}$$$, and asked you to find the value of $$$gcd(t)$$$ for him. In other words, you need to calculate the GCD of LCMs of all pairs of elements in the given sequence. Input The first line contains one integer $$$n (2le nle 100,000)$$$. The second line contains $$$n$$$ integers, $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 200,000$$$). Output Print one integer: $$$gcd({ extrm{lcm}({a_i,a_j}) i<j})$$$. Examples Input 10 540 648 810 648 720 540 594 864 972 648 Note For the first example, $$$t={ extrm{lcm}({1,1})}={1}$$$, so $$$gcd(t)=1$$$. For the second example, $$$t={120,40,80,120,240,80}$$$, and it's not hard to see that $$$gcd(t)=40$$$.
1,600
true
false
false
false
true
false
false
false
false
false
3,964
31E
Problem - 31E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp *2400 No tag edit access → Contest materials ") are «empty». Leading zeroes in numbers _A_,u2009_S_1,u2009_S_2 are allowed. In the end of the game the first player gets _S_1 dollars, and the second gets _S_2 dollars. One day Homer and Marge came to play the game. They managed to know the number _A_ beforehand. They want to find such sequence of their moves that both of them makes exactly _n_ moves and which maximizes their total prize. Help them. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u200918). The second line contains integer _A_ consisting of exactly 2_n_ digits. This number can have leading zeroes. Output Output the line of 2_n_ characters «H» and «M» — the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly _n_ moves. If there are several solutions, output any of them. Examples Input 2 1234 Output HHMM Input 2 9911 Output HMHM
2,400
false
false
false
true
false
false
false
false
false
false
9,839
1799E
You are given $$$n imes m$$$ grid. Some cells are filled and some are empty. A city is a maximal (by inclusion) set of filled cells such that it is possible to get from any cell in the set to any other cell in the set by moving to adjacent (by side) cells, without moving into any cells not in the set. In other words, a city is a connected component of filled cells with edges between adjacent (by side) cells. Initially, there are two cities on the grid. You want to change some empty cells into filled cells so that both of the following are satisfied: There is one city on the resulting grid. The shortest path between any two filled cells, achievable only by moving onto filled cells, is equal to the Manhattan distance between them. The Manhattan distance between two cells $$$(a, b)$$$ and $$$(c, d)$$$ is equal to $$$a - c + b - d$$$. Find a way to add filled cells that satisfies these conditions and minimizes the total number of filled cells. Input Input consists of multiple test cases. The first line contains a single integer $$$t$$$, the number of test cases ($$$1 le t le 5000$$$). The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 50$$$, $$$nm geq 3$$$). The next $$$n$$$ lines describe the grid. The $$$i$$$-th line contains a string $$$s_i$$$ of length $$$m$$$. $$$s_{i,j}$$$ is '#' if the cell in position $$$(i, j)$$$ is filled, and '.' if it is empty. It is guaranteed that there are exactly two cities in the initial grid. It is guaranteed that the sum of $$$ncdot m$$$ over all test cases does not exceed $$$25,000$$$. Output For each test case, output $$$n$$$ lines, each containing a string of length $$$m$$$, describing the grid you create in the same format as the input. If there are multiple possible answers with the minimum number of filled cells print any. Example Input 11 1 3 #.# 2 2 .# #. 4 4 ..## ...# #... ##.. 6 6 .##... ##.... ...... ....## .....# ...### 6 5 .#..# .#..# .#..# .#.## .#... ##... 5 5 ##### #...# #.#.# #...# ##### 4 4 .##. ##.# #.## .##. 5 5 ..### ....# ..... #.... #.... 5 6 .##... ##.... #....# ....## ...##. 6 5 ..##. ...## ....# #.... ##... .##.. 5 4 ..## ..#. ..#. #... #... Output ### .# ## ..## ..## ###. ##.. .##... ###... ..#... ..#### ...### ...### .#### .#### .#### .#### .#... ##... ##### ##### ##### ##### ##### .##. #### #### .##. ..### ..### ..#.. ###.. #.... .##... ###... ###### ...### ...##. ..##. ..### ..### ###.. ###.. .##.. ..## ..#. ..#. ###. #... Note In the first test case, we can add a single filled cell between the two cities to connect them. We can verify that the second condition is satisfied. In the second test case, we can also connect the cities with a single filled cell, while satisfying the second condition. In the third test case, note that if we filled the 3 cells in the top left, the cities would be connected, but the second condition would not be satisfied for cells $$$(4, 2)$$$ and $$$(2, 4)$$$.
2,300
true
true
true
false
false
true
false
false
false
false
1,479
1899B
Alex is participating in the filming of another video of BrMeast, and BrMeast asked Alex to prepare 250 thousand tons of TNT, but Alex didn't hear him well, so he prepared $$$n$$$ boxes and arranged them in a row waiting for trucks. The $$$i$$$-th box from the left weighs $$$a_i$$$ tons. All trucks that Alex is going to use hold the same number of boxes, denoted by $$$k$$$. Loading happens the following way: The first $$$k$$$ boxes goes to the first truck, The second $$$k$$$ boxes goes to the second truck, $$$dotsb$$$ The last $$$k$$$ boxes goes to the $$$frac{n}{k}$$$-th truck. Upon loading is completed, each truck must have exactly $$$k$$$ boxes. In other words, if at some point it is not possible to load exactly $$$k$$$ boxes into the truck, then the loading option with that $$$k$$$ is not possible. Alex hates justice, so he wants the maximum absolute difference between the total weights of two trucks to be as great as possible. If there is only one truck, this value is $$$0$$$. Alex has quite a lot of connections, so for every $$$1 leq k leq n$$$, he can find a company such that each of its trucks can hold exactly $$$k$$$ boxes. Print the maximum absolute difference between the total weights of any two trucks. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 150,000$$$)xa0— the number of boxes. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the weights of the boxes. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$150,000$$$. Output For each test case, print a single integerxa0— the answer to the problem. Example Input 5 2 1 2 6 10 2 3 6 1 3 4 1000000000 1000000000 1000000000 1000000000 15 60978 82265 78961 56708 39846 31071 4913 4769 29092 91348 64119 72421 98405 222 14294 8 19957 69913 37531 96991 57838 21008 14207 19198 Output 1 9 0 189114 112141 Note In the first case, we should pick two trucks, so the first one will have only the first box, and the second one will have only the second box. In the second case, we should pick six trucks, so the maximum will be $$$10$$$, the minimum will be $$$1$$$, and the answer is $$$10 - 1 = 9$$$. In the third case, for any possible $$$k$$$, the trucks will have the same total weight of boxes, so the answer is $$$0$$$.
1,100
false
false
true
false
false
false
true
false
false
false
921
1867B
You are given a binary string $$$s$$$ of length $$$n$$$ (a string that consists only of $$$0$$$ and $$$1$$$). A number $$$x$$$ is good if there exists a binary string $$$l$$$ of length $$$n$$$, containing $$$x$$$ ones, such that if each symbol $$$s_i$$$ is replaced by $$$s_i oplus l_i$$$ (where $$$oplus$$$ denotes the is equal to $$$1$$$ if number $$$i$$$ is good, and $$$0$$$ otherwise. A palindrome is a string that reads the same from left to right as from right to left. For example, 01010, 1111, 0110 are palindromes. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). 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 $$$10^5$$$. Output For each test case, output a single line containing a binary string $$$t$$$ of length $$$n+1$$$ - the answer to the problem. Example Input 5 6 101011 5 00000 9 100100011 3 100 1 1 Output 0010100 111111 0011111100 0110 11 Note Consider the first example. $$$t_2 = 1$$$ because we can choose $$$l = $$$ 010100, then the string $$$s$$$ becomes 111111, which is a palindrome. $$$t_4 = 1$$$ because we can choose $$$l = $$$ 101011. It can be shown that for all other $$$i$$$, there is no answer, so the remaining symbols are $$$0$$$.
1,100
false
false
false
false
false
true
false
false
false
false
1,074
442A
Have you ever played Hanabi? If not, then you've got to try it out! This problem deals with a simplified version of the game. Overall, the game has 25 types of cards (5 distinct colors and 5 distinct values). Borya is holding _n_ cards. The game is somewhat complicated by the fact that everybody sees Borya's cards except for Borya himself. Borya knows which cards he has but he knows nothing about the order they lie in. Note that Borya can have multiple identical cards (and for each of the 25 types of cards he knows exactly how many cards of this type he has). The aim of the other players is to achieve the state when Borya knows the color and number value of each of his cards. For that, other players can give him hints. The hints can be of two types: color hints and value hints. A color hint goes like that: a player names some color and points at all the cards of this color. Similarly goes the value hint. A player names some value and points at all the cards that contain the value. Determine what minimum number of hints the other players should make for Borya to be certain about each card's color and value. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of Borya's cards. The next line contains the descriptions of _n_ cards. The description of each card consists of exactly two characters. The first character shows the color (overall this position can contain five distinct letters — R, G, B, Y, W). The second character shows the card's value (a digit from 1 to 5). Borya doesn't know exact order of the cards they lie in. Output Print a single integer — the minimum number of hints that the other players should make. Note In the first sample Borya already knows for each card that it is a green three. In the second sample we can show all fours and all red cards. In the third sample you need to make hints about any four colors.
1,700
false
false
true
false
false
false
true
false
false
false
8,070
28C
Problem - 28C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp probabilities *2200 No tag edit access → Contest materials ") ") ") ") — the amount of students and the amount of rooms. The second line contains _m_ integers _a_1,u2009_a_2,u2009... ,u2009_a__m_ (1u2009≤u2009_a__i_u2009≤u200950). _a__i_ means the amount of wash basins in the _i_-th room. Output Output single number: the expected value of the size of the largest queue. Your answer must have an absolute or relative error less than 10u2009-u20099. Examples Input 1 1 2 Output 1.00000000000000000000 Input 2 2 1 1 Output 1.50000000000000000000 Input 2 3 1 1 1 Output 1.33333333333333350000 Input 7 5 1 1 2 3 1 Output 2.50216960000000070000
2,200
false
false
false
true
false
false
false
false
false
false
9,856
1254D
Hanh is a famous biologist. He loves growing trees and doing experiments on his own garden. One day, he got a tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. A tree with $$$n$$$ vertices is an undirected connected graph with $$$n-1$$$ edges. Initially, Hanh sets the value of every vertex to $$$0$$$. Now, Hanh performs $$$q$$$ operations, each is either of the following types: Type $$$1$$$: Hanh selects a vertex $$$v$$$ and an integer $$$d$$$. Then he chooses some vertex $$$r$$$ uniformly at random, lists all vertices $$$u$$$ such that the path from $$$r$$$ to $$$u$$$ passes through $$$v$$$. Hanh then increases the value of all such vertices $$$u$$$ by $$$d$$$. Type $$$2$$$: Hanh selects a vertex $$$v$$$ and calculates the expected value of $$$v$$$. Since Hanh is good at biology but not math, he needs your help on these operations. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 leq n, q leq 150,000$$$)xa0— the number of vertices on Hanh's tree and the number of operations he performs. Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n$$$), denoting that there is an edge connecting two vertices $$$u$$$ and $$$v$$$. It is guaranteed that these $$$n - 1$$$ edges form a tree. Each of the last $$$q$$$ lines describes an operation in either formats: $$$1$$$ $$$v$$$ $$$d$$$ ($$$1 leq v leq n, 0 leq d leq 10^7$$$), representing a first-type operation. $$$2$$$ $$$v$$$ ($$$1 leq v leq n$$$), representing a second-type operation. It is guaranteed that there is at least one query of the second type. Output For each operation of the second type, write the expected value on a single line. Let $$$M = 998244353$$$, it can be shown that the expected value can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Input 5 12 1 2 1 3 2 4 2 5 1 1 1 2 1 2 2 2 3 2 4 2 5 1 2 2 2 1 2 2 2 3 2 4 2 5 Output 1 199648871 399297742 199648871 199648871 598946614 199648873 2 2 2 Note The image below shows the tree in the example: For the first query, where $$$v = 1$$$ and $$$d = 1$$$: If $$$r = 1$$$, the values of all vertices get increased. If $$$r = 2$$$, the values of vertices $$$1$$$ and $$$3$$$ get increased. If $$$r = 3$$$, the values of vertices $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$ get increased. If $$$r = 4$$$, the values of vertices $$$1$$$ and $$$3$$$ get increased. If $$$r = 5$$$, the values of vertices $$$1$$$ and $$$3$$$ get increased. Hence, the expected values of all vertices after this query are ($$$1, 0.4, 0.8, 0.4, 0.4$$$). For the second query, where $$$v = 2$$$ and $$$d = 2$$$: If $$$r = 1$$$, the values of vertices $$$2$$$, $$$4$$$ and $$$5$$$ get increased. If $$$r = 2$$$, the values of all vertices get increased. If $$$r = 3$$$, the values of vertices $$$2$$$, $$$4$$$ and $$$5$$$ get increased. If $$$r = 4$$$, the values of vertices $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$ get increased. If $$$r = 5$$$, the values of vertices $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$ get increased. Hence, the expected values of all vertices after this query are ($$$2.2, 2.4, 2, 2, 2$$$).
2,700
false
false
false
false
true
false
false
false
false
false
4,412
1762C
A binary string$$$^dagger$$$ $$$b$$$ of odd length $$$m$$$ is good if $$$b_i$$$ is the median$$$^ddagger$$$ of $$$b[1,i]^S$$$ for all odd indices $$$i$$$ ($$$1 leq i leq m$$$). For a binary string $$$a$$$ of length $$$k$$$, a binary string $$$b$$$ of length $$$2k-1$$$ is an extension of $$$a$$$ if $$$b_{2i-1}=a_i$$$ for all $$$i$$$ such that $$$1 leq i leq k$$$. For example, 1001011 and 1101001 are extensions of the string 1001. String $$$x=$$$1011011 is not an extension of string $$$y=$$$1001 because $$$x_3 eq y_2$$$. Note that there are $$$2^{k-1}$$$ different extensions of $$$a$$$. You are given a binary string $$$s$$$ of length $$$n$$$. Find the sum of the number of good extensions over all prefixes of $$$s$$$. In other words, find $$$sum_{i=1}^{n} f(s[1,i])$$$, where $$$f(x)$$$ gives number of good extensions of string $$$x$$$. Since the answer can be quite large, you only need to find it modulo $$$998,244,353$$$. $$$^dagger$$$ A binary string is a string whose elements are either $$$mathtt{0}$$$ or $$$mathtt{1}$$$. $$$^ddagger$$$ For a binary string $$$a$$$ of length $$$2m-1$$$, the median of $$$a$$$ is the (unique) element that occurs at least $$$m$$$ times in $$$a$$$. $$$^S$$$ $$$a[l,r]$$$ denotes the string of length $$$r-l+1$$$ which is formed by the concatenation of $$$a_l,a_{l+1},ldots,a_r$$$ in that order. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$), where $$$n$$$ is the length of the binary string $$$s$$$. The second line of each test case contains the 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 the answer modulo $$$998,244,353$$$. Note In the first and second test cases, $$$f(s[1,1])=1$$$. In the third test case, the answer is $$$f(s[1,1])+f(s[1,2])=1+2=3$$$. In the fourth test case, the answer is $$$f(s[1,1])+f(s[1,2])+f(s[1,3])=1+1+1=3$$$. $$$f(mathtt{11})=2$$$ because two good extensions are possible: 101 and 111. $$$f(mathtt{01})=1$$$ because only one good extension is possible: 011.
1,400
true
false
false
false
false
false
false
false
false
false
1,738
1754B
For his birthday, Kevin received the set of pairwise distinct numbers $$$1, 2, 3, ldots, n$$$ as a gift. He is going to arrange these numbers in a way such that the minimum absolute difference between two consecutive numbers be maximum possible. More formally, if he arranges numbers in order $$$p_1, p_2, ldots, p_n$$$, he wants to maximize the value $$$$$$min limits_{i=1}^{n - 1} lvert p_{i + 1} - p_i vert,$$$$$$ where $$$x$$$ denotes the absolute value of $$$x$$$. Help Kevin to do that. Input Each test 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 only line of each test case contains an integer $$$n$$$ ($$$2 le n leq 1,000$$$)xa0— the size of the set. Output For each test case print a single line containing $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$) describing the arrangement that maximizes the minimum absolute difference of consecutive elements. Formally, you have to print a permutation $$$p$$$ which maximizes the value $$$min limits_{i=1}^{n - 1} lvert p_{i + 1} - p_i vert$$$. If there are multiple optimal solutions, print any of them. Note In the first test case the minimum absolute difference of consecutive elements equals $$$min {lvert 4 - 2 vert, lvert 1 - 4 vert, lvert 3 - 1 vert } = min {2, 3, 2} = 2$$$. It's easy to prove that this answer is optimal. In the second test case each permutation of numbers $$$1, 2, 3$$$ is an optimal answer. The minimum absolute difference of consecutive elements equals to $$$1$$$.
800
true
true
false
false
false
true
false
false
false
false
1,769
1153E
This is an interactive problem. Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a $$$n imes n$$$ grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die. Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of $$$4$$$. Today Serval got up too late and only have time to make $$$2019$$$ queries. As his best friend, can you help him find the positions of the head and the tail? Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length $$$0$$$, that means it only has adjacent head and tail. Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries. Input The first line contains a single integer $$$n$$$ ($$$2leq n leq 1000$$$)xa0— the size of the grid. Output When you are ready to answer, you should print ! x1 y1 x2 y2, where $$$(x_1, y_1)$$$ represents the position of the head and $$$(x_2,y_2)$$$ represents the position of the tail. You can print head and tail in any order. Interaction To make a query, you should print ? x1 y1 x2 y2 ($$$1 leq x_1 leq x_2 leq n$$$, $$$1leq y_1 leq y_2 leq n$$$), representing a rectangle consisting of all cells $$$(x,y)$$$ such that $$$x_1 leq x leq x_2$$$ and $$$y_1 leq y leq y_2$$$. You will get a single integer as the answer. After printing a query, do not forget to output the end of line and flush the output, otherwise you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Answer $$$-1$$$ instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving $$$-1$$$ and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict. Hacks To make a hack, print a single integer $$$n$$$ ($$$2 leq n leq 1000$$$) in the first line, indicating the size of the grid. Then print an integer $$$k$$$ ($$$2 leq k leq n^2$$$) in the second line, indicating the length of the snake. In the next $$$k$$$ lines, print $$$k$$$ pairs of integers $$$x_i, y_i$$$ ($$$1 leq x_i, y_i leq n$$$), each pair in a single line, indicating the $$$i$$$-th cell of snake, such that the adjacent pairs are adjacent, and all $$$k$$$ pairs are distinct. Examples Output ? 1 1 1 1 ? 1 2 1 2 ? 2 2 2 2 ! 1 1 2 1 Output ? 2 2 2 2 ? 2 1 2 3 ! 2 1 2 3 Note The pictures above show our queries and the answers in the first example. We first made a query for $$$(1,1)$$$ and got an answer $$$1$$$, then found that it must be connected to exactly one other cell. Then we made a query for $$$(1,2)$$$ and got an answer of $$$0$$$, then knew that the snake never entered it. So the cell connected to $$$(1,1)$$$ must be $$$(2,1)$$$. Then we made a query for $$$(2,2)$$$ and got an answer $$$0$$$, then knew that it never entered $$$(2,2)$$$ as well. So the snake cannot leave $$$(2,1)$$$, which implies that the answer is $$$(1,1)$$$ and $$$(2,1)$$$. The pictures above show our queries and the answers in the second example. By making query to $$$(2,2)$$$ and receiving $$$2$$$, we found that the snake occupies $$$(2,2)$$$. And by making query to rectangle from $$$(2,1)$$$ to $$$(2,3)$$$ and receiving answer $$$0$$$, we knew that it never goes out of the rectangle from $$$(2,1)$$$ to $$$(2,3)$$$. Since the first answer is $$$2$$$, both $$$(2,1)$$$ and $$$(2,3)$$$ must be occupied but none of others, so the answer is $$$(2,1)$$$ and $$$(2,3)$$$.
2,200
false
false
false
false
false
false
true
true
false
false
4,945
1462E1
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on $$$k$$$ and $$$m$$$ (in this version $$$k=2$$$ and $$$m=3$$$). Also, in this version of the problem, you DON'T NEED to output the answer by modulo. You are given a sequence $$$a$$$ of length $$$n$$$ consisting of integers from $$$1$$$ to $$$n$$$. The sequence may contain duplicates (i.e. some elements can be equal). Find the number of tuples of $$$m = 3$$$ elements such that the maximum number in the tuple differs from the minimum by no more than $$$k = 2$$$. Formally, you need to find the number of triples of indices $$$i < j < z$$$ such that $$$$$$max(a_i, a_j, a_z) - min(a_i, a_j, a_z) le 2.$$$$$$ For example, if $$$n=4$$$ and $$$a=[1,2,4,3]$$$, then there are two such triples ($$$i=1, j=2, z=4$$$ and $$$i=2, j=3, z=4$$$). If $$$n=4$$$ and $$$a=[1,1,1,1]$$$, then all four possible triples are suitable. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the sequence $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2,ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than $$$2$$$. Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
1,500
true
false
false
false
false
false
false
true
true
false
3,376
380B
Sereja adores trees. Today he came up with a revolutionary new type of binary root trees. His new tree consists of _n_ levels, each vertex is indexed by two integers: the number of the level and the number of the vertex on the current level. The tree root is at level 1, its index is (1,u20091). Here is a pseudo code of tree construction. //the global data are integer arrays cnt[], left[][], right[][]cnt[1] = 1; fill arrays left[][], right[][] with values -1; for(level = 1; level < n; level = level + 1){ cnt[level + 1] = 0; for(position = 1; position <= cnt[level]; position = position + 1){ if(the value of position is a power of two){ // that is, 1, 2, 4, 8... left[level][position] = cnt[level + 1] + 1; right[level][position] = cnt[level + 1] + 2; cnt[level + 1] = cnt[level + 1] + 2; }else{ right[level][position] = cnt[level + 1] + 1; cnt[level + 1] = cnt[level + 1] + 1; } } } After the pseudo code is run, cell cnt[level] contains the number of vertices on level _level_. Cell left[level][position] contains the number of the vertex on the level _level_u2009+u20091, which is the left child of the vertex with index (_level_,u2009_position_), or it contains -1, if the vertex doesn't have a left child. Similarly, cell right[level][position] is responsible for the right child. You can see how the tree with _n_u2009=u20094 looks like in the notes. Serja loves to make things complicated, so he first made a tree and then added an empty set _A_(_level_,u2009_position_) for each vertex. Then Sereja executes _m_ operations. Each operation is of one of the two following types: The format of the operation is "1 _t_ _l_ _r_ _x_". For all vertices _level_,u2009_position_ (_level_u2009=u2009_t_;xa0_l_u2009≤u2009_position_u2009≤u2009_r_) add value _x_ to set _A_(_level_,u2009_position_). The format of the operation is "2 _t_ _v_". For vertex _level_,u2009_position_ (_level_u2009=u2009_t_,u2009_position_u2009=u2009_v_), find the union of all sets of vertices that are in the subtree of vertex (_level_,u2009_position_). Print the size of the union of these sets. Help Sereja execute the operations. In this problem a set contains only distinct values like std::set in C++. Input The first line contains integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20097000). Next _m_ lines contain the descriptions of the operations. The operation of the first type is given by five integers: 1 _t_ _l_ _r_ _x_ (1u2009≤u2009_t_u2009≤u2009_n_;xa01u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_cnt_[_t_];xa01u2009≤u2009_x_u2009≤u2009106). The operation of the second type is given by three integers: 2 _t_ _v_ (1u2009≤u2009_t_u2009≤u2009_n_;xa01u2009≤u2009_v_u2009≤u2009_cnt_[_t_]).
2,400
false
false
true
false
false
false
false
false
false
true
8,309
1806F1
This is the easy version of the problem. The only difference between the two versions is the constraint on $$$m$$$. You can make hacks only if both versions of the problem are solved. You are given an array $$$a$$$ of length $$$n$$$ and two integers $$$m$$$ and $$$k$$$. Each element in $$$a$$$ satisfies $$$1le a_i le m$$$. In one operation, you choose two indices $$$i$$$ and $$$j$$$ such that $$$1 le i < j le a$$$, then append $$$gcd(a_i,a_j)$$$ to the back of the array and delete $$$a_i$$$ and $$$a_j$$$ from the array. Note that the length of the array decreases by one after this operation. Find the maximum possible sum of the array after performing exactly $$$k$$$ operations. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^5$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 10^6$$$; $$$1le m le 10^6$$$; $$$1 le k le n-1$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le m$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output the maximum possible sum of the array after performing $$$k$$$ operations optimally. Example Input 3 3 8 1 4 7 8 5 114 2 7 2 4 1 6 3 514 2 2 3 3 Note In the first test case, the best way is to choose $$$i=1$$$, $$$j=3$$$ in the first operation. The final sequence is $$$[7,4]$$$.
2,900
true
true
false
false
false
false
false
false
true
false
1,437
331C3
Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. The beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: "May the Great Beaver bless you! May your chacres open and may your third eye never turn blind from beholding the Truth! Take the magic number, subtract a digit from it (the digit must occur in the number) and get a new magic number. Repeat this operation until a magic number equals zero. The Earth will stand on Three Beavers for the time, equal to the number of subtractions you perform!" Distinct subtraction sequences can obviously get you different number of operations. But the Smart Beaver is ready to face the worst and is asking you to count the minimum number of operations he needs to reduce the magic number to zero. Input The single line contains the magic integer _n_, 0u2009≤u2009_n_. to get 20 points, you need to solve the problem with constraints: _n_u2009≤u2009106 (subproblem C1); to get 40 points, you need to solve the problem with constraints: _n_u2009≤u20091012 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: _n_u2009≤u20091018 (subproblems C1+C2+C3). Output Print a single integer — the minimum number of subtractions that turns the magic number to a zero. Note In the first test sample the minimum number of operations can be reached by the following sequence of subtractions: 24u2009→u200920u2009→u200918u2009→u200910u2009→u20099u2009→u20090
2,500
false
false
false
true
false
false
false
false
false
false
8,516
1450E
A society can be represented by a connected, undirected graph of $$$n$$$ vertices and $$$m$$$ edges. The vertices represent people, and an edge $$$(i,j)$$$ represents a friendship between people $$$i$$$ and $$$j$$$. In society, the $$$i$$$-th person has an income $$$a_i$$$. A person $$$i$$$ is envious of person $$$j$$$ if $$$a_j=a_i+1$$$. That is if person $$$j$$$ has exactly $$$1$$$ more unit of income than person $$$i$$$. The society is called capitalist if for every pair of friends one is envious of the other. For some friendships, you know which friend is envious of the other. For the remaining friendships, you do not know the direction of envy. The income inequality of society is defined as $$$maxlimits_{1 leq i leq n} a_i - minlimits_{1 leq i leq n} a_i$$$. You only know the friendships and not the incomes. If it is impossible for this society to be capitalist with the given knowledge, you should report about it. Otherwise, you should find an assignment of incomes in which the society is capitalist, and the income inequality is maximized. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$1le nle 200$$$, $$$n-1le mle 2000$$$) — the number of people and friendships, respectively. The following $$$m$$$ lines describe the friendships. Each friendship is described by three integers $$$i$$$, $$$j$$$, $$$b$$$ ($$$1le i, jle n, i e j, 0le ble 1$$$). This denotes that people $$$i$$$ and $$$j$$$ are friends. If $$$b=1$$$, we require that person $$$i$$$ is envious of person $$$j$$$. If $$$b=0$$$, one friend should be envious of the other in either direction. There is at most one friendship between each pair of people. It is guaranteed that if we consider the friendships as undirected edges, the graph is connected. Output Print "YES" if it is possible that the society is capitalist, or "NO" otherwise. You can print characters in any case (upper or lower). If the answer is "YES", you should print two additional lines. In the first line, print the maximum possible income inequality. On the next line you should print $$$n$$$ integers $$$a_1,ldots, a_n$$$ ($$$0le a_ile 10^6$$$), where $$$a_i$$$ denotes the income of the $$$i$$$-th person. We can prove that if there exists a solution, there exists one where $$$0le a_ile 10^6$$$ for all $$$i$$$. If there exist multiple solutions, print any. Examples Input 6 6 1 2 0 3 2 0 2 5 0 6 5 1 6 3 0 2 4 1 Input 4 4 1 2 1 2 3 0 3 4 1 4 1 1 Note In the first test, we can show that an income inequality greater than $$$3$$$ is impossible for the given society. In the given answer with income inequality equal to $$$3$$$: Person $$$2$$$ is envious of person $$$1$$$. Person $$$3$$$ is envious of person $$$2$$$. Person $$$5$$$ is envious of person $$$2$$$. Person $$$6$$$ is envious of person $$$5$$$ (the required direction is satisfied). Person $$$6$$$ is envious of person $$$3$$$. Person $$$2$$$ is envious of person $$$4$$$ (the required direction is satisfied). In the second test, we can show that there is no way to assign incomes to satisfy all requirements.
2,700
false
false
false
false
false
true
false
false
false
true
3,433
480D
Jaroslav owns a small courier service. He has recently got and introduced a new system of processing parcels. Each parcel is a box, the box has its weight and strength. The system works as follows. It originally has an empty platform where you can put boxes by the following rules: If the platform is empty, then the box is put directly on the platform, otherwise it is put on the topmost box on the platform. The total weight of all boxes on the platform cannot exceed the strength of platform _S_ at any time. The strength of any box of the platform at any time must be no less than the total weight of the boxes that stand above. You can take only the topmost box from the platform. The system receives _n_ parcels, the _i_-th parcel arrives exactly at time _in__i_, its weight and strength are equal to _w__i_ and _s__i_, respectively. Each parcel has a value of _v__i_ bourles. However, to obtain this value, the system needs to give the parcel exactly at time _out__i_, otherwise Jaroslav will get 0 bourles for it. Thus, Jaroslav can skip any parcel and not put on the platform, formally deliver it at time _in__i_ and not get anything for it. Any operation in the problem is performed instantly. This means that it is possible to make several operations of receiving and delivering parcels at the same time and in any order. Please note that the parcel that is delivered at time _out__i_, immediately gets outside of the system, and the following activities taking place at the same time are made u200bu200bwithout taking it into consideration. Since the system is very complex, and there are a lot of received parcels, Jaroslav asks you to say what maximum amount of money he can get using his system. Input The first line of the input contains two space-separated integers _n_ and _S_ (1u2009≤u2009_n_u2009≤u2009500, 0u2009≤u2009_S_u2009≤u20091000). Then _n_ lines follow, the _i_-th line contains five space-separated integers: _in__i_, _out__i_, _w__i_, _s__i_ and _v__i_ (0u2009≤u2009_in__i_u2009<u2009_out__i_u2009<u20092_n_, 0u2009≤u2009_w__i_,u2009_s__i_u2009≤u20091000, 1u2009≤u2009_v__i_u2009≤u2009106). It is guaranteed that for any _i_ and _j_ (_i_u2009≠u2009_j_) either _in__i_u2009≠u2009_in__j_, or _out__i_u2009≠u2009_out__j_. Note Note to the second sample (_T_ is the moment in time): _T_u2009=u20090: The first parcel arrives, we put in on the first platform. _T_u2009=u20091: The second and third parcels arrive, we put the third one on the current top (i.e. first) parcel on the platform, then we put the secod one on the third one. Now the first parcel holds weight _w_2u2009+u2009_w_3u2009=u20092 and the third parcel holds _w_2u2009=u20091. _T_u2009=u20092: We deliver the second parcel and get _v_2u2009=u20091 bourle. Now the first parcel holds weight _w_3u2009=u20091, the third one holds 0. _T_u2009=u20093: The fourth parcel comes. First we give the third parcel and get _v_3u2009=u20091 bourle. Now the first parcel holds weight 0. We put the fourth parcel on it — the first one holds _w_4u2009=u20092. _T_u2009=u20094: The fifth parcel comes. We cannot put it on the top parcel of the platform as in that case the first parcel will carry weight _w_4u2009+u2009_w_5u2009=u20093, that exceed its strength _s_1u2009=u20092, that's unacceptable. We skip the fifth parcel and get nothing for it. _T_u2009=u20095: Nothing happens. _T_u2009=u20096: We deliver the fourth, then the first parcel and get _v_1u2009+u2009_v_4u2009=u20093 bourles for them. Note that you could have skipped the fourth parcel and got the fifth one instead, but in this case the final sum would be 4 bourles.
2,600
false
false
false
true
false
false
false
false
false
true
7,911
547C
Mike is a bartender at Rico's bar. At Rico's, they put beer glasses in a special shelf. There are _n_ kinds of beer at Rico's numbered from 1 to _n_. _i_-th kind of beer has _a__i_ milliliters of foam on it. Maxim is Mike's boss. Today he told Mike to perform _q_ queries. Initially the shelf is empty. In each request, Maxim gives him a number _x_. If beer number _x_ is already in the shelf, then Mike should remove it from the shelf, otherwise he should put it in the shelf. After each query, Mike should tell him the score of the shelf. Bears are geeks. So they think that the score of a shelf is the number of pairs (_i_,u2009_j_) of glasses in the shelf such that _i_u2009<u2009_j_ and where is the greatest common divisor of numbers _a_ and _b_. Mike is tired. So he asked you to help him in performing these requests. Input The first line of input contains numbers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u20092u2009×u2009105), the number of different kinds of beer and number of queries. The next line contains _n_ space separated integers, _a_1,u2009_a_2,u2009... ,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20095u2009×u2009105), the height of foam in top of each kind of beer. The next _q_ lines contain the queries. Each query consists of a single integer integer _x_ (1u2009≤u2009_x_u2009≤u2009_n_), the index of a beer that should be added or removed from the shelf. Output For each query, print the answer for that query in one line.
2,300
true
false
false
true
false
false
false
false
false
false
7,650
361B
Problem - 361B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms math number theory *1200 No tag edit access → Contest materials shows the greatest common divisor of numbers _a_ and _b_. Levko assumes that element _p__i_ of permutation _p_1,u2009_p_2,u2009... ,u2009_p__n_ is good if _gcd_(_i_,u2009_p__i_)u2009>u20091. Levko considers a permutation beautiful, if it has exactly _k_ good elements. Unfortunately, he doesn’t know any beautiful permutation. Your task is to help him to find at least one of them. Input The single line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_k_u2009≤u2009_n_). Output In a single line print either any beautiful permutation or -1, if such permutation doesn’t exist. If there are multiple suitable permutations, you are allowed to print any of them. Examples Input 4 2 Output 2 4 3 1 Input 1 1 Output -1 Note In the first sample elements 4 and 3 are good because _gcd_(2,u20094)u2009=u20092u2009>u20091 and _gcd_(3,u20093)u2009=u20093u2009>u20091. Elements 2 and 1 are not good because _gcd_(1,u20092)u2009=u20091 and _gcd_(4,u20091)u2009=u20091. As there are exactly 2 good elements, the permutation is beautiful. The second sample has no beautiful permutations.
1,200
true
false
false
false
false
true
false
false
false
false
8,388
407C
Problem - 407C - 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 combinatorics implementation math *2500 No tag edit access → Contest materials . The second line contains _n_ integers _a_
2,500
true
false
true
false
false
false
true
false
false
false
8,196
1106E
Lunar New Year is approaching, and Bob is going to receive some red envelopes with countless money! But collecting money from red envelopes is a time-consuming process itself. Let's describe this problem in a mathematical way. Consider a timeline from time $$$1$$$ to $$$n$$$. The $$$i$$$-th red envelope will be available from time $$$s_i$$$ to $$$t_i$$$, inclusive, and contain $$$w_i$$$ coins. If Bob chooses to collect the coins in the $$$i$$$-th red envelope, he can do it only in an integer point of time between $$$s_i$$$ and $$$t_i$$$, inclusive, and he can't collect any more envelopes until time $$$d_i$$$ (inclusive) after that. Here $$$s_i leq t_i leq d_i$$$ holds. Bob is a greedy man, he collects coins greedily — whenever he can collect coins at some integer time $$$x$$$, he collects the available red envelope with the maximum number of coins. If there are multiple envelopes with the same maximum number of coins, Bob would choose the one whose parameter $$$d$$$ is the largest. If there are still multiple choices, Bob will choose one from them randomly. However, Alice — his daughter — doesn't want her father to get too many coins. She could disturb Bob at no more than $$$m$$$ integer time moments. If Alice decides to disturb Bob at time $$$x$$$, he could not do anything at time $$$x$$$ and resumes his usual strategy at the time $$$x + 1$$$ (inclusive), which may lead to missing some red envelopes. Calculate the minimum number of coins Bob would get if Alice disturbs him optimally. Input The first line contains three non-negative integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq n leq 10^5$$$, $$$0 leq m leq 200$$$, $$$1 leq k leq 10^5$$$), denoting the length of the timeline, the number of times Alice can disturb Bob and the total number of red envelopes, respectively. The following $$$k$$$ lines describe those $$$k$$$ red envelopes. The $$$i$$$-th line contains four positive integers $$$s_i$$$, $$$t_i$$$, $$$d_i$$$ and $$$w_i$$$ ($$$1 leq s_i leq t_i leq d_i leq n$$$, $$$1 leq w_i leq 10^9$$$)xa0— the time segment when the $$$i$$$-th envelope is available, the time moment Bob can continue collecting after collecting the $$$i$$$-th envelope, and the number of coins in this envelope, respectively. Output Output one integer — the minimum number of coins Bob would get if Alice disturbs him optimally. Examples Input 5 0 2 1 3 4 5 2 5 5 8 Input 10 1 6 1 1 2 4 2 2 6 2 3 3 3 3 4 4 4 5 5 5 5 7 6 6 6 9 Input 12 2 6 1 5 5 4 4 6 6 2 3 8 8 3 2 9 9 5 6 10 10 7 8 12 12 9 Note In the first sample, Alice has no chance to disturb Bob. Therefore Bob will collect the coins in the red envelopes at time $$$1$$$ and $$$5$$$, collecting $$$13$$$ coins in total. In the second sample, Alice should disturb Bob at time $$$1$$$. Therefore Bob skips the first envelope, collects the second one and can not do anything after that. So the answer is $$$2$$$.
2,100
false
false
false
true
true
false
false
false
false
false
5,171
331A2
— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me? — Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night? At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees. Let's consider the woodland belt as a sequence of trees. Each tree _i_ is described by the esthetic appeal _a__i_ — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly! The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart: The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; and of course, the walk should be successful: there must be at least two trees in the woodland belt left. Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart? Input The first line contains a single integer _n_ — the initial number of trees in the woodland belt, 2u2009≤u2009_n_. The second line contains space-separated integers _a__i_ — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value. to get 30 points, you need to solve the problem with constraints: _n_u2009≤u2009100 (subproblem A1); to get 100 points, you need to solve the problem with constraints: _n_u2009≤u20093·105 (subproblems A1+A2). Output In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees _k_. In the next line print _k_ integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to _n_ from left to right. If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.
1,500
false
false
false
false
true
false
false
false
true
false
8,521
1450C2
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. 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 .O. OOO .O. 6 XXXOOO XXXOOO XX..OO OO..XX OOOXXX OOOXXX 5 .OOO. OXXXO OXXXO OXXXO .OOO. Output .O. OXO .O. OXXOOX XOXOXO XX..OO OO..XX OXOXOX XOOXXO .OXO. OOXXO XXOXX OXXOO .OXO. Note In the first test case, there are initially three 'O' consecutive in the second row and the second column. By changing the middle token to 'X' we make the grid a draw, and we only changed $$$1le lfloor 5/3 floor$$$ token. In the second test case, the final grid is a draw. We only changed $$$8le lfloor 32/3 floor$$$ tokens. In the third test case, the final grid is a draw. We only changed $$$7le lfloor 21/3 floor$$$ tokens.
2,300
true
false
false
false
false
true
false
false
false
false
3,435
884A
Problem - 884A - Codeforces =============== xa0 — the number of days and the time required to read the book. The second line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u200986400) — the time Luba has to spend on her work during _i_-th day. Output Print the minimum day Luba can finish reading the book. It is guaranteed that answer doesn't exceed _n_. Examples Input 2 2 86400 86398 Output 2 Input 2 86400 0 86400 Output 1
800
false
false
true
false
false
false
false
false
false
false
6,214
232E
John Doe has a field, which is a rectangular table of size _n_u2009×u2009_m_. We assume that the field rows are numbered from 1 to _n_ from top to bottom, and the field columns are numbered from 1 to _m_ from left to right. Then the cell of the field at the intersection of the _x_-th row and the _y_-th column has coordinates (_x_; _y_). We know that some cells of John's field are painted white, and some are painted black. Also, John has a tortoise, which can move along the white cells of the field. The tortoise can get from a white cell with coordinates (_x_; _y_) into cell (_x_u2009+u20091; _y_) or (_x_; _y_u2009+u20091), if the corresponding cell is painted white. In other words, the turtle can move only along the white cells of the field to the right or down. The turtle can not go out of the bounds of the field. In addition, John has _q_ queries, each of them is characterized by four numbers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 (_x_1u2009≤u2009_x_2, _y_1u2009≤u2009_y_2). For each query John wants to know whether the tortoise can start from the point with coordinates (_x_1; _y_1), and reach the point with coordinates (_x_2; _y_2), moving only along the white squares of the field. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009500) — the field sizes. Each of the next _n_ lines contains _m_ characters "#" and ".": the _j_-th character of the _i_-th line equals "#", if the cell (_i_; _j_) is painted black and ".", if it is painted white. The next line contains integer _q_ (1u2009≤u2009_q_u2009≤u20096·105) — the number of queries. Next _q_ lines contain four space-separated integers _x_1, _y_1, _x_2 and _y_2 (1u2009≤u2009_x_1u2009≤u2009_x_2u2009≤u2009_n_, 1u2009≤u2009_y_1u2009≤u2009_y_2u2009≤u2009_m_) — the coordinates of the starting and the finishing cells. It is guaranteed that cells (_x_1; _y_1) and (_x_2; _y_2) are white. Output For each of _q_ queries print on a single line "Yes", if there is a way from cell (_x_1; _y_1) to cell (_x_2; _y_2), that meets the requirements, and "No" otherwise. Print the answers to the queries in the order, in which the queries are given in the input.
3,000
false
false
false
true
false
false
false
false
false
false
8,907
1921B
In order to test the hypothesis about the cats, the scientists must arrange the cats in the boxes in a specific way. Of course, they would like to test the hypothesis and publish a sensational article as quickly as possible, because they are too engrossed in the next hypothesis about the phone's battery charge. Scientists have $$$n$$$ boxes in which cats may or may not sit. Let the current state of the boxes be denoted by the sequence $$$b_1, dots, b_n$$$: $$$b_i = 1$$$ if there is a cat in box number $$$i$$$, and $$$b_i = 0$$$ otherwise. Fortunately, the unlimited production of cats has already been established, so in one day, the scientists can perform one of the following operations: Take a new cat and place it in a box (for some $$$i$$$ such that $$$b_i = 0$$$, assign $$$b_i = 1$$$). Remove a cat from a box and send it into retirement (for some $$$i$$$ such that $$$b_i = 1$$$, assign $$$b_i = 0$$$). Move a cat from one box to another (for some $$$i, j$$$ such that $$$b_i = 1, b_j = 0$$$, assign $$$b_i = 0, b_j = 1$$$). It has also been found that some boxes were immediately filled with cats. Therefore, the scientists know the initial position of the cats in the boxes $$$s_1, dots, s_n$$$ and the desired position $$$f_1, dots, f_n$$$. Due to the large amount of paperwork, the scientists do not have time to solve this problem. Help them for the sake of science and indicate the minimum number of days required to test the hypothesis. Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. This is followed by descriptions of the test cases. Each test case consists of three lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of boxes. The second line of each test case contains a string $$$s$$$ of $$$n$$$ characters, where the $$$i$$$-th character is '1' if there is a cat in the $$$i$$$-th box and '0' otherwise. The third line of each test case contains a string $$$f$$$ of $$$n$$$ characters, where the $$$i$$$-th character is '1' if there should be a cat in the $$$i$$$-th box and '0' otherwise. It is guaranteed that in a test the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer on a separate line — the minimum number of operations required to obtain the desired position from the initial position. It can be shown that a solution always exists. Example Input 6 5 10010 00001 1 1 1 3 000 111 4 0101 1010 3 100 101 8 10011001 11111110 Note In the first test case, you can first move the cat from the first box to the fifth, and then remove the cat from the fourth box. In the second test case, there is nothing to do — the only cat is already sitting in the correct box. In the third test case of input data, it takes three days to place a cat in each box.
800
false
true
true
false
false
false
false
false
false
false
764
1034C
There are $$$n$$$ cities in the Kingdom of Autumn, numbered from $$$1$$$ to $$$n$$$. People can travel between any two cities using $$$n-1$$$ two-directional roads. This year, the government decides to separate the kingdom. There will be regions of different levels. The whole kingdom will be the region of level $$$1$$$. Each region of $$$i$$$-th level should be separated into several (at least two) regions of $$$i+1$$$-th level, unless $$$i$$$-th level is the last level. Each city should belong to exactly one region of each level and for any two cities in the same region, it should be possible to travel between them passing the cities in the same region only. According to research, for each city $$$i$$$, there is a value $$$a_i$$$, which describes the importance of this city. All regions of the same level should have an equal sum of city importances. Your task is to find how many plans there are to determine the separation of the regions that all the conditions are satisfied. Two plans are considered different if and only if their numbers of levels are different or there exist two cities in the same region of one level in one plan but in different regions of this level in the other plan. Since the answer may be very large, output it modulo $$$10^9+7$$$. Note For the first example, there are $$$4$$$ different plans: Plan $$$1$$$: Level-$$$1$$$: $$${1,2,3,4}$$$. Plan $$$2$$$: Level-$$$1$$$: $$${1,2,3,4}$$$, Level-$$$2$$$: $$${1,2}$$$,$$${3,4}$$$. Plan $$$3$$$: Level-$$$1$$$: $$${1,2,3,4}$$$, Level-$$$2$$$: $$${1}$$$,$$${2}$$$,$$${3}$$$,$$${4}$$$. Plan $$$4$$$: Level-$$$1$$$: $$${1,2,3,4}$$$, Level-$$$2$$$: $$${1,2}$$$,$$${3,4}$$$, Level-$$$3$$$: $$${1}$$$,$$${2}$$$,$$${3}$$$,$$${4}$$$. For the second example, there are $$$2$$$ different plans: Plan $$$1$$$: Level-$$$1$$$: $$${1,2,3,4}$$$. Plan $$$2$$$: Level-$$$1$$$: $$${1,2,3,4}$$$, Level-$$$2$$$: $$${1}$$$,$$${2}$$$,$$${3}$$$,$$${4}$$$. For the third example, there are $$$3$$$ different plans: Plan $$$1$$$: Level-$$$1$$$: $$${1,2,3,4}$$$. Plan $$$2$$$: Level-$$$1$$$: $$${1,2,3,4}$$$, Level-$$$2$$$: $$${1,2}$$$,$$${3,4}$$$. Plan $$$3$$$: Level-$$$1$$$: $$${1,2,3,4}$$$, Level-$$$2$$$: $$${1,3}$$$,$$${2}$$$,$$${4}$$$.
2,700
false
false
false
true
false
false
false
false
false
false
5,526
285C
Problem - 285C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation sortings *1200 No tag edit access → Contest materials ") Tutorial") — the size of the sought permutation. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109). Output Print a single number — the minimum number of moves. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 2 3 0 Output 2 Input 3 -1 -1 2 Output 6 Note In the first sample you should decrease the first number by one and then increase the second number by one. The resulting permutation is (2,u20091). In the second sample you need 6 moves to build permutation (1,u20093,u20092).
1,200
false
true
true
false
false
false
false
false
true
false
8,694
1433C
There are $$$n$$$ piranhas with sizes $$$a_1, a_2, ldots, a_n$$$ in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium. Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them. Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely: The piranha $$$i$$$ can eat the piranha $$$i-1$$$ if the piranha $$$i-1$$$ exists and $$$a_{i - 1} < a_i$$$. The piranha $$$i$$$ can eat the piranha $$$i+1$$$ if the piranha $$$i+1$$$ exists and $$$a_{i + 1} < a_i$$$. When the piranha $$$i$$$ eats some piranha, its size increases by one ($$$a_i$$$ becomes $$$a_i + 1$$$). Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas. Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them. For example, if $$$a = [5, 3, 4, 4, 5]$$$, then the third piranha can be dominant. Consider the sequence of its moves: The piranha eats the second piranha and $$$a$$$ becomes $$$[5, underline{5}, 4, 5]$$$ (the underlined piranha is our candidate). The piranha eats the third piranha and $$$a$$$ becomes $$$[5, underline{6}, 5]$$$. The piranha eats the first piranha and $$$a$$$ becomes $$$[underline{7}, 5]$$$. The piranha eats the second piranha and $$$a$$$ becomes $$$[underline{8}]$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$) — the number of piranhas in the aquarium. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the size of the $$$i$$$-th piranha. It is guaranteed that the sum of $$$n$$$ does not exceed $$$3 cdot 10^5$$$ ($$$sum n le 3 cdot 10^5$$$). Output For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any. Note The first test case of the example is described in the problem statement. In the second test case of the example, there are no dominant piranhas in the aquarium. In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes $$$[4, 4, 5, 4]$$$, then it can eat any other piranha in the aquarium.
900
false
true
false
false
false
true
false
false
false
false
3,497
1358A
Due to the coronavirus pandemic, city authorities obligated citizens to keep a social distance. The mayor of the city Semyon wants to light up Gluharniki park so that people could see each other even at night to keep the social distance. The park is a rectangular table with $$$n$$$ rows and $$$m$$$ columns, where the cells of the table are squares, and the boundaries between the cells are streets. External borders are also streets. Every street has length $$$1$$$. For example, park with $$$n=m=2$$$ has $$$12$$$ streets. You were assigned to develop a plan for lighting the park. You can put lanterns in the middle of the streets. The lamp lights two squares near it (or only one square if it stands on the border of the park). The park sizes are: $$$n=4$$$, $$$m=5$$$. The lighted squares are marked yellow. Please note that all streets have length $$$1$$$. Lanterns are placed in the middle of the streets. In the picture not all the squares are lit. Semyon wants to spend the least possible amount of money on lighting but also wants people throughout the park to keep a social distance. So he asks you to find the minimum number of lanterns that are required to light all the squares. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is a line containing two integers $$$n$$$, $$$m$$$ ($$$1 le n, m le 10^4$$$) — park sizes. Output Print $$$t$$$ answers to the test cases. Each answer must be a single integer — the minimum number of lanterns that are required to light all the squares. Example Input 5 1 1 1 3 2 2 3 3 5 3 Note Possible optimal arrangement of the lanterns for the $$$2$$$-nd test case of input data example: Possible optimal arrangement of the lanterns for the $$$3$$$-rd test case of input data example:
800
true
true
false
false
false
false
false
false
false
false
3,897
1616A
Problem - 1616A - Codeforces =============== xa0 and negate these numbers (i.xa0e. change $$$x o (-x)$$$). What is the maximum number of different values in the array you can achieve? Input The first line of input contains one integer $$$t$$$ ($$$1 leq t leq 100$$$): the number of test cases. The next lines contain the description of the $$$t$$$ test cases, two lines per a test case. In the first line you are given one integer $$$n$$$ ($$$1 leq n leq 100$$$): the number of integers in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-100 leq a_i leq 100$$$). Output For each test case, print one integer: the maximum number of different elements in the array that you can achieve negating numbers in the array. Example Input 3 4 1 1 2 2 3 1 2 3 2 0 0 Output 4 3 1 Note In the first example we can, for example, negate the first and the last numbers, achieving the array $$$
800
false
false
true
false
false
false
false
false
false
false
2,556
82A
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on. This process continues ad infinitum. For example, Penny drinks the third can of cola and the queue will look like this: Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny. Write a program that will print the name of a man who will drink the _n_-th can. Note that in the very beginning the queue looks like that: Sheldon, Leonard, Penny, Rajesh, Howard. The first person is Sheldon. Input The input data consist of a single integer _n_ (1u2009≤u2009_n_u2009≤u2009109). It is guaranteed that the pretests check the spelling of all the five names, that is, that they contain all the five possible answers. Output Print the single line — the name of the person who drinks the _n_-th can of cola. The cans are numbered starting from 1. Please note that you should spell the names like this: "Sheldon", "Leonard", "Penny", "Rajesh", "Howard" (without the quotes). In that order precisely the friends are in the queue initially.
1,100
true
false
true
false
false
false
false
false
false
false
9,558
1809D
You are given a binary string $$$s$$$ consisting of only characters 0 and/or 1. You can perform several operations on this string (possibly zero). There are two types of operations: choose two consecutive elements and swap them. In order to perform this operation, you pay $$$10^{12}$$$ coins; choose any element from the string and remove it. In order to perform this operation, you pay $$$10^{12}+1$$$ coins. Your task is to calculate the minimum number of coins required to sort the string $$$s$$$ in non-decreasing order (i.u2009e. transform $$$s$$$ so that $$$s_1 le s_2 le dots le s_m$$$, where $$$m$$$ is the length of the string after applying all operations). An empty string is also considered sorted in non-decreasing order. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The only line of each test case contains the string $$$s$$$ ($$$1 le s le 3 cdot 10^5$$$), consisting of only characters 0 and/or 1. The sum of lengths of all given strings doesn't exceed $$$3 cdot 10^5$$$. Note In the first example, you have to remove the $$$1$$$-st element, so the string becomes equal to 00. In the second example, the string is already sorted. In the third example, you have to swap the $$$2$$$-nd and the $$$3$$$-rd elements, so the string becomes equal to 0011. In the fourth example, you have to swap the $$$3$$$-rd and the $$$4$$$-th elements, so the string becomes equal to 00011101, and then remove the $$$7$$$-th element, so the string becomes equal to 0001111. In the fifth example, you have to remove the $$$1$$$-st element, so the string becomes equal to 001101, and then remove the $$$5$$$-th element, so the string becomes equal to 00111. In the sixth example, the string is already sorted.
1,800
false
true
false
false
false
true
false
false
false
false
1,417
72F
After the nationalization of the oil industry, Dr. Mosaddegh wants to dig some oil wells to extract all the oil in Persian Gulf. But Persian Gulf is huge and has an infinite amount of oil. So Dr. Mosaddegh works only on a rectangular plane of size _n_u2009×u2009_m_ of the Persian Gulf. Each of the cells in this rectangle either contains an infinite amount of oil or nothing. Two cells are considered adjacent if and only if they have a common edge, a path is a sequence _c_1,u2009_c_2,u2009...,u2009_c__x_ of cells so that all of them contain oil and for each _i_, _c__i_ is adjacent to _c__i_u2009-u20091 and _c__i_u2009+u20091 (if they exist). Two cells are considered connected to each other if and only if there exists a path between them. If we dig a well in a certain cell, we can extract oil from all the cells that are connected to it by oil paths. It is not allowed to dig wells on empty cells. Dr. Mosaddegh also knows that in Persian Gulf, the empty cells form rows and columns. I. e. if some cell is empty, then it's column is completely empty or it's row is completely empty, or both. Help Dr. Mosaddegh find out how many wells he has to dig to access all the oil in that region. Input In the first line there are two positive integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100). In the second line there is an integer _t_ (0u2009≤u2009_t_u2009≤u2009_n_), the number of empty rows. _t_ distinct positive integers follow, these are the numbers of empty rows and are in range [1,u2009_n_]. In the second line there is an integer _s_ (0u2009≤u2009_s_u2009≤u2009_m_) that shows the number of columns not having any oil. _s_ distinct positive integers follow, these are the numbers of empty columns and are in range of [1,u2009_m_]. Note that rows are numbered from 1 to _n_ (from top to bottom) and columns are numbered from 1 to _m_ (from left to right). Output A single integer, the minimum number of wells that Dr. Mossadegh has to dig. This is actually finding how many regions are made by removing the given rows and columns.
1,900
true
true
false
false
false
false
false
false
false
false
9,606
864C
A bus moves along the coordinate line _Ox_ from the point _x_u2009=u20090 to the point _x_u2009=u2009_a_. After starting from the point _x_u2009=u20090, it reaches the point _x_u2009=u2009_a_, immediately turns back and then moves to the point _x_u2009=u20090. After returning to the point _x_u2009=u20090 it immediately goes back to the point _x_u2009=u2009_a_ and so on. Thus, the bus moves from _x_u2009=u20090 to _x_u2009=u2009_a_ and back. Moving from the point _x_u2009=u20090 to _x_u2009=u2009_a_ or from the point _x_u2009=u2009_a_ to _x_u2009=u20090 is called a bus journey. In total, the bus must make _k_ journeys. The petrol tank of the bus can hold _b_ liters of gasoline. To pass a single unit of distance the bus needs to spend exactly one liter of gasoline. The bus starts its first journey with a full petrol tank. There is a gas station in point _x_u2009=u2009_f_. This point is between points _x_u2009=u20090 and _x_u2009=u2009_a_. There are no other gas stations on the bus route. While passing by a gas station in either direction the bus can stop and completely refuel its tank. Thus, after stopping to refuel the tank will contain _b_ liters of gasoline. What is the minimum number of times the bus needs to refuel at the point _x_u2009=u2009_f_ to make _k_ journeys? The first journey starts in the point _x_u2009=u20090. Input The first line contains four integers _a_, _b_, _f_, _k_ (0u2009<u2009_f_u2009<u2009_a_u2009≤u2009106, 1u2009≤u2009_b_u2009≤u2009109, 1u2009≤u2009_k_u2009≤u2009104) — the endpoint of the first bus journey, the capacity of the fuel tank of the bus, the point where the gas station is located, and the required number of journeys. Output Print the minimum number of times the bus needs to refuel to make _k_ journeys. If it is impossible for the bus to make _k_ journeys, print -1. Note In the first example the bus needs to refuel during each journey. In the second example the bus can pass 10 units of distance without refueling. So the bus makes the whole first journey, passes 4 units of the distance of the second journey and arrives at the point with the gas station. Then it can refuel its tank, finish the second journey and pass 2 units of distance from the third journey. In this case, it will again arrive at the point with the gas station. Further, he can refill the tank up to 10 liters to finish the third journey and ride all the way of the fourth journey. At the end of the journey the tank will be empty. In the third example the bus can not make all 3 journeys because if it refuels during the second journey, the tanks will contain only 5 liters of gasoline, but the bus needs to pass 8 units of distance until next refueling.
1,500
true
true
true
false
false
false
false
false
false
false
6,285
787B
Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them. There are _n_ parallel universes participating in this event (_n_ Ricks and _n_ Mortys). I. e. each of _n_ universes has one Rick and one Morty. They're gathering in _m_ groups. Each person can be in many groups and a group can contain an arbitrary number of members. Ricks and Mortys have registered online in these groups. So, a person can have joined a group more than once (developer of this website hadn't considered this possibility). Summer from universe #1 knows that in each parallel universe (including hers) exactly one of Rick and Morty from that universe is a traitor and is loyal, but no one knows which one. She knows that we are doomed if there's a group such that every member in that group is a traitor (they will plan and destroy the world). Summer knows that if there's a possibility that world ends (there's a group where all members are traitors) she should immediately cancel this event. So she wants to know if she should cancel the event. You have to tell her yes if and only if there's at least one scenario (among all 2_n_ possible scenarios, 2 possible scenarios for who a traitor in each universe) such that in that scenario the world will end. Input The first line of input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009104) — number of universes and number of groups respectively. The next _m_ lines contain the information about the groups. _i_-th of them first contains an integer _k_ (number of times someone joined _i_-th group, _k_u2009>u20090) followed by _k_ integers _v__i_,u20091,u2009_v__i_,u20092,u2009...,u2009_v__i_,u2009_k_. If _v__i_,u2009_j_ is negative, it means that Rick from universe number u2009-u2009_v__i_,u2009_j_ has joined this group and otherwise it means that Morty from universe number _v__i_,u2009_j_ has joined it. Sum of _k_ for all groups does not exceed 104. Output In a single line print the answer to Summer's question. Print "YES" if she should cancel the event and "NO" otherwise. Examples Input 5 2 5 3 -2 1 -1 5 3 -5 2 5 Input 7 2 3 -1 6 7 7 -5 4 2 4 7 -3 4 Note In the first sample testcase, 1st group only contains the Rick from universe number 3, so in case he's a traitor, then all members of this group are traitors and so Summer should cancel the event.
1,300
true
true
true
false
false
false
false
false
false
false
6,622
959D
Problem - 959D - 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 greedy math number theory *1900 No tag edit access → Contest materials ") u2009=u20091, where _GCD_(_w_,u2009_z_) is the greatest common divisor of _w_ and _z_. Ehab wants to choose a special array so he wants the lexicographically minimal array between all the variants. Can you find it? An array _x_ is lexicographically greater than an array _y_ if there exists an index _i_ such than _x__i_u2009>u2009_y__i_ and _x__j_u2009=u2009_y__j_ for all 1u2009≤u2009_j_u2009<u2009_i_. An array _x_ is equal to an array _y_ if _x__i_u2009=u2009_y__i_ for all 1u2009≤u2009_i_u2009≤u2009_n_. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105), the number of elements in _a_ and _b_. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (2u2009≤u2009_a__i_u2009≤u2009105), the elements of _a_. Output Output _n_ space-separated integers, the _i_-th of them representing _b__i_. Examples Input 5 2 3 5 4 13 Output 2 3 5 7 11 Input 3 10 3 7 Output 10 3 7 Note Note that in the second sample, the array is already pairwise coprime so we printed it.
1,900
true
true
false
false
false
true
false
false
false
false
5,874
228C
Little Vasya likes painting fractals very much. He does it like this. First the boy cuts out a 2u2009×u20092-cell square out of squared paper. Then he paints some cells black. The boy calls the cut out square a fractal pattern. Then he takes a clean square sheet of paper and paints a fractal by the following algorithm: 1. He divides the sheet into four identical squares. A part of them is painted black according to the fractal pattern. 2. Each square that remained white, is split into 4 lesser white squares, some of them are painted according to the fractal pattern. Each square that remained black, is split into 4 lesser black squares. In each of the following steps step 2 repeats. To draw a fractal, the boy can make an arbitrary positive number of steps of the algorithm. But he need to make at least two steps. In other words step 2 of the algorithm must be done at least once. The resulting picture (the square with painted cells) will be a fractal. The figure below shows drawing a fractal (here boy made three steps of the algorithm). One evening Vasya got very tired, so he didn't paint the fractal, he just took a sheet of paper, painted a _n_u2009×u2009_m_-cell field. Then Vasya paint some cells black. Now he wonders, how many squares are on the field, such that there is a fractal, which can be obtained as described above, and which is equal to that square. Square is considered equal to some fractal if they consist of the same amount of elementary not divided cells and for each elementary cell of the square corresponding elementary cell of the fractal have the same color. Input The first line contains two space-separated integers _n_,u2009_m_ (2u2009≤u2009_n_,u2009_m_u2009≤u2009500) — the number of rows and columns of the field, correspondingly. Next _n_ lines contain _m_ characters each — the description of the field, painted by Vasya. Character "." represents a white cell, character "*" represents a black cell. It is guaranteed that the field description doesn't contain other characters than "." and "*". Output On a single line print a single integer — the number of squares on the field, such that these squares contain a drawn fractal, which can be obtained as described above. Examples Input 6 11 ......*.** *.*.*....* .***....*. ..***.*.... .*.*.....* ......*.*.. Note The answer for the first sample is shown on the picture below. Fractals are outlined by red, blue and green squares. The answer for the second sample is 0. There is no fractal, equal to the given picture.
2,000
false
false
false
true
false
false
false
false
false
false
8,926
385D
One day a bear lived on the _Oxy_ axis. He was afraid of the dark, so he couldn't move at night along the plane points that aren't lit. One day the bear wanted to have a night walk from his house at point (_l_,u20090) to his friend's house at point (_r_,u20090), along the segment of length (_r_u2009-u2009_l_). Of course, if he wants to make this walk, he needs each point of the segment to be lit. That's why the bear called his friend (and yes, in the middle of the night) asking for a very delicate favor. The _Oxy_ axis contains _n_ floodlights. Floodlight _i_ is at point (_x__i_,u2009_y__i_) and can light any angle of the plane as large as _a__i_ degree with vertex at point (_x__i_,u2009_y__i_). The bear asked his friend to turn the floodlights so that he (the bear) could go as far away from his house as possible during the walking along the segment. His kind friend agreed to fulfill his request. And while he is at it, the bear wonders: what is the furthest he can go away from his house? Hep him and find this distance. Consider that the plane has no obstacles and no other light sources besides the floodlights. The bear's friend cannot turn the floodlights during the bear's walk. Assume that after all the floodlights are turned in the correct direction, the bear goes for a walk and his friend goes to bed. Input The first line contains three space-separated integers _n_, _l_, _r_ (1u2009≤u2009_n_u2009≤u200920;xa0u2009-u2009105u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009105). The _i_-th of the next _n_ lines contain three space-separated integers _x__i_, _y__i_, _a__i_ (u2009-u20091000u2009≤u2009_x__i_u2009≤u20091000;xa01u2009≤u2009_y__i_u2009≤u20091000;xa01u2009≤u2009_a__i_u2009≤u200990) — the floodlights' description. Note that two floodlights can be at the same point of the plane. Output Print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20096. Note In the first sample, one of the possible solutions is: In the second sample, a single solution is: In the third sample, a single solution is:
2,200
false
false
false
true
false
false
false
false
false
false
8,288
792E
Problem - 792E - Codeforces =============== xa0 balls of different colors (each set contains only balls of one color), there are no two sets such that the difference between their sizes is greater than 1. Print the minimum possible number of sets. Input The first line contains one integer number _n_ (1u2009≤u2009_n_u2009≤u2009500). The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009... ,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output Print one integer number — the minimum possible number of sets. Examples Input 3 4 7 8 Output 5 Input 2 2 7 Output 4 Note In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color.
2,500
true
true
false
false
false
false
false
false
false
false
6,609
1082B
Vova has won $$$n$$$ trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row. The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment. Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap. Input The first line contains one integer $$$n$$$ ($$$2 le n le 10^5$$$) — the number of trophies. The second line contains $$$n$$$ characters, each of them is either G or S. If the $$$i$$$-th character is G, then the $$$i$$$-th trophy is a golden one, otherwise it's a silver trophy. Output Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap. Note In the first example Vova has to swap trophies with indices $$$4$$$ and $$$10$$$. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is $$$7$$$. In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is $$$4$$$. In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than $$$0$$$.
1,600
false
true
false
false
false
false
false
false
false
false
5,314
1184E2
After a successful field test, Heidi is considering deploying a trap along some Corridor, possibly not the first one. She wants to avoid meeting the Daleks inside the Time Vortex, so for abundance of caution she considers placing the traps only along those Corridors that are not going to be used according to the current Daleks' plan – which is to use a minimum spanning tree of Corridors. Heidi knows that all energy requirements for different Corridors are now different, and that the Daleks have a single unique plan which they are intending to use. Your task is to calculate the number $$$E_{max}(c)$$$, which is defined in the same way as in the easy version – i.e., the largest $$$e le 10^9$$$ such that if we changed the energy of corridor $$$c$$$ to $$$e$$$, the Daleks might use it – but now for every corridor that Heidi considers. Input The first line: number $$$n$$$ of destinations, number $$$m$$$ of Time Corridors ($$$2 leq n leq 10^5$$$, $$$n - 1 leq m leq 10^6$$$). The next $$$m$$$ lines: destinations $$$a$$$, $$$b$$$ and energy $$$e$$$ ($$$1 leq a, b leq n$$$, $$$a eq b$$$, $$$0 leq e leq 10^9$$$). No pair $$${a, b}$$$ will repeat. The graph is guaranteed to be connected. All energy requirements $$$e$$$ are distinct. Output Output $$$m-(n-1)$$$ lines, each containing one integer: $$$E_{max}(c_i)$$$ for the $$$i$$$-th Corridor $$$c_i$$$ from the input that is not part of the current Daleks' plan (minimum spanning tree). Note If $$$m = n-1$$$, then you need not output anything.
2,100
false
false
false
false
false
false
false
false
false
true
4,782
1719A
Burenka and Tonya are playing an old Buryat game with a chip on a board of $$$n imes m$$$ cells. At the beginning of the game, the chip is located in the lower left corner of the board. In one move, the player can move the chip to the right or up by any odd number of cells (but you cannot move the chip both to the right and up in one move). The one who cannot make a move loses. Burenka makes the first move, the players take turns. Burenka really wants to win the game, but she is too lazy to come up with a strategy, so you are invited to solve the difficult task of finding it. Name the winner of the game (it is believed that Burenka and Tonya are masters of playing with chips, so they always move in the optimal way). Chip's starting cell is green, the only cell from which chip can't move is red. if the chip is in the yellow cell, then blue cells are all options to move the chip in one move. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The following is a description of the input data sets. The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^9$$$) — the dimensions of the game board. Output For each test case print a single line — the name of the winner of the game ("Burenka" or "Tonya"). Example Input 6 1 1 1 4 5 6 2 2 6 3 999999999 1000000000 Output Tonya Burenka Burenka Tonya Burenka Burenka Note In the first case, Burenka has no move, so Tonya wins. In the second case, Burenka can move $$$3$$$ cells to the right, after which Tony will not be able to make a move, which means that Burenka wins. In the third case, Burenka can move $$$5$$$ squares to the right. Then we can say that we have a game on a board of $$$1 imes 5$$$ cells, and Tonya is the first player. In such game the second player wins, so in the original one Burenka will win.
800
true
false
false
false
false
false
false
false
false
false
1,968
1869B
Piggy lives on an infinite plane with the Cartesian coordinate system on it. There are $$$n$$$ cities on the plane, numbered from $$$1$$$ to $$$n$$$, and the first $$$k$$$ cities are defined as major cities. The coordinates of the $$$i$$$-th city are $$$(x_i,y_i)$$$. Piggy, as a well-experienced traveller, wants to have a relaxing trip after Zhongkao examination. Currently, he is in city $$$a$$$, and he wants to travel to city $$$b$$$ by air. You can fly between any two cities, and you can visit several cities in any order while travelling, but the final destination must be city $$$b$$$. Because of active trade between major cities, it's possible to travel by plane between them for free. Formally, the price of an air ticket $$$f(i,j)$$$ between two cities $$$i$$$ and $$$j$$$ is defined as follows: $$$$$$ f(i,j)= begin{cases} 0, & ext{if cities }i ext{ and }j ext{ are both major cities} x_i-x_j+y_i-y_j, & ext{otherwise} end{cases} $$$$$$ Piggy doesn't want to save time, but he wants to save money. So you need to tell him the minimum value of the total cost of all air tickets if he can take any number of flights. Input The first line of input contains a single integer $$$t$$$ ($$$1le tle 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains four integers $$$n$$$, $$$k$$$, $$$a$$$ and $$$b$$$ ($$$2le nle 2cdot 10^5$$$, $$$0le kle n$$$, $$$1le a,ble n$$$, $$$a e b$$$) — the number of cities, the number of major cities and the numbers of the starting and the ending cities. Then $$$n$$$ lines follow, the $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9le x_i,y_ile 10^9$$$) — the coordinates of the $$$i$$$-th city. The first $$$k$$$ lines describe major cities. It is guaranteed that all coordinates are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print a single integer — the minimum value of the total price of all air tickets. Example Input 5 6 2 3 5 0 0 1 -2 -2 1 -1 3 2 -2 -3 -3 2 0 1 2 -1000000000 -1000000000 1000000000 1000000000 7 5 4 2 154 147 -154 -147 123 456 20 23 43 20 998 244 353 100 3 1 3 1 0 10 1 20 2 30 4 3 2 4 0 0 -100 100 -1 -1 -1 0 Output 4 4000000000 0 22 1 Note In the first test case: The major cities are marked red. The optimal way to choose the flights is: $$$3 ightarrow 1 ightarrow 2 ightarrow 5$$$, which will cost $$$3+0+1=4$$$. Note that the flight $$$1 ightarrow 2$$$ costs $$$0$$$, because both city $$$1$$$ and $$$2$$$ are major cities. In the second test case, since there are only $$$2$$$ cities, the only way is to take a flight from city $$$1$$$ to $$$2$$$. In the third test case, since city $$$2$$$ and $$$4$$$ are both major cities, Piggy can directly take a flight from city $$$2$$$ to $$$4$$$, which costs $$$0$$$. In the fourth test case, Piggy can choose to take the following flights: $$$3 ightarrow 2 ightarrow 1$$$, and the cost is $$$11+11=22$$$.
1,100
true
false
false
false
false
false
false
false
true
false
1,060
145E
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya brought home string _s_ with the length of _n_. The string only consists of lucky digits. The digits are numbered from the left to the right starting with 1. Now Petya should execute _m_ queries of the following form: switch _l_ _r_ — "switch" digits (i.e. replace them with their opposites) at all positions with indexes from _l_ to _r_, inclusive: each digit 4 is replaced with 7 and each digit 7 is replaced with 4 (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_); count — find and print on the screen the length of the longest non-decreasing subsequence of string _s_. Subsequence of a string _s_ is a string that can be obtained from _s_ by removing zero or more of its elements. A string is called non-decreasing if each successive digit is not less than the previous one. Help Petya process the requests. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009106,u20091u2009≤u2009_m_u2009≤u20093·105) — the length of the string _s_ and the number of queries correspondingly. The second line contains _n_ lucky digits without spaces — Petya's initial string. Next _m_ lines contain queries in the form described in the statement. Output For each query count print an answer on a single line. Examples Input 2 3 47 count switch 1 2 count Input 3 5 747 count switch 1 1 count switch 1 3 count Note In the first sample the chronology of string _s_ after some operations are fulfilled is as follows (the sought maximum subsequence is marked with bold): 1. 47 2. 74 3. 74 In the second sample: 1. 747 2. 447 3. 447 4. 774 5. 774
2,400
false
false
false
false
true
false
false
false
false
false
9,291
1292B
With a new body, our idol Aroma White (or should we call her Kaori Minamiya?) begins to uncover her lost past through the OS space. The space can be considered a 2D plane, with an infinite number of data nodes, indexed from $$$0$$$, with their coordinates defined as follows: The coordinates of the $$$0$$$-th node is $$$(x_0, y_0)$$$ For $$$i > 0$$$, the coordinates of $$$i$$$-th node is $$$(a_x cdot x_{i-1} + b_x, a_y cdot y_{i-1} + b_y)$$$ Initially Aroma stands at the point $$$(x_s, y_s)$$$. She can stay in OS space for at most $$$t$$$ seconds, because after this time she has to warp back to the real world. She doesn't need to return to the entry point $$$(x_s, y_s)$$$ to warp home. While within the OS space, Aroma can do the following actions: From the point $$$(x, y)$$$, Aroma can move to one of the following points: $$$(x-1, y)$$$, $$$(x+1, y)$$$, $$$(x, y-1)$$$ or $$$(x, y+1)$$$. This action requires $$$1$$$ second. If there is a data node at where Aroma is staying, she can collect it. We can assume this action costs $$$0$$$ seconds. Of course, each data node can be collected at most once. Aroma wants to collect as many data as possible before warping back. Can you help her in calculating the maximum number of data nodes she could collect within $$$t$$$ seconds? Input The first line contains integers $$$x_0$$$, $$$y_0$$$, $$$a_x$$$, $$$a_y$$$, $$$b_x$$$, $$$b_y$$$ ($$$1 leq x_0, y_0 leq 10^{16}$$$, $$$2 leq a_x, a_y leq 100$$$, $$$0 leq b_x, b_y leq 10^{16}$$$), which define the coordinates of the data nodes. The second line contains integers $$$x_s$$$, $$$y_s$$$, $$$t$$$ ($$$1 leq x_s, y_s, t leq 10^{16}$$$)xa0– the initial Aroma's coordinates and the amount of time available. Output Print a single integerxa0— the maximum number of data nodes Aroma can collect within $$$t$$$ seconds. Examples Input 1 1 2 3 1 0 15 27 26 Note In all three examples, the coordinates of the first $$$5$$$ data nodes are $$$(1, 1)$$$, $$$(3, 3)$$$, $$$(7, 9)$$$, $$$(15, 27)$$$ and $$$(31, 81)$$$ (remember that nodes are numbered from $$$0$$$). In the first example, the optimal route to collect $$$3$$$ nodes is as follows: Go to the coordinates $$$(3, 3)$$$ and collect the $$$1$$$-st node. This takes $$$3 - 2 + 3 - 4 = 2$$$ seconds. Go to the coordinates $$$(1, 1)$$$ and collect the $$$0$$$-th node. This takes $$$1 - 3 + 1 - 3 = 4$$$ seconds. Go to the coordinates $$$(7, 9)$$$ and collect the $$$2$$$-nd node. This takes $$$7 - 1 + 9 - 1 = 14$$$ seconds. In the second example, the optimal route to collect $$$2$$$ nodes is as follows: Collect the $$$3$$$-rd node. This requires no seconds. Go to the coordinates $$$(7, 9)$$$ and collect the $$$2$$$-th node. This takes $$$15 - 7 + 27 - 9 = 26$$$ seconds. In the third example, Aroma can't collect any nodes. She should have taken proper rest instead of rushing into the OS space like that.
1,700
false
true
true
false
false
true
true
false
false
false
4,236
306A
Problem - 306A - Codeforces =============== xa0 . He wants to make a New Year present with candies to each friend. Polycarpus is planning to present all candies and he wants to do this in the fairest (that is, most equal) manner. He wants to choose such _a__i_, where _a__i_ is the number of candies in the _i_-th friend's present, that the maximum _a__i_ differs from the least _a__i_ as little as possible. For example, if _n_ is divisible by _m_, then he is going to present the same number of candies to all his friends, that is, the maximum _a__i_ won't differ from the minimum one. Input The single line of the input contains a pair of space-separated positive integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100;_n_u2009≥u2009_m_) — the number of candies and the number of Polycarpus's friends. Output Print the required sequence _a_1,u2009_a_2,u2009...,u2009_a__m_, where _a__i_ is the number of candies in the _i_-th friend's present. All numbers _a__i_ must be positive integers, total up to _n_, the maximum one should differ from the minimum one by the smallest possible value. Examples Input 12 3 Output 4 4 4 Input 15 4 Output 3 4 4 4 Input 18 7 Output 2 2 2 3 3 3 3 Note Print _a__i_ in any order, separate the numbers by spaces.
800
false
false
true
false
false
false
false
false
false
false
8,611
167C
In some country live wizards. They love playing with numbers. The blackboard has two numbers written on it — _a_ and _b_. The order of the numbers is not important. Let's consider _a_u2009≤u2009_b_ for the sake of definiteness. The players can cast one of the two spells in turns: Replace _b_ with _b_u2009-u2009_a__k_. Number _k_ can be chosen by the player, considering the limitations that _k_u2009>u20090 and _b_u2009-u2009_a__k_u2009≥u20090. Number _k_ is chosen independently each time an active player casts a spell. Replace _b_ with _b_xa0_mod_xa0_a_. If _a_u2009>u2009_b_, similar moves are possible. If at least one of the numbers equals zero, a player can't make a move, because taking a remainder modulo zero is considered somewhat uncivilized, and it is far too boring to subtract a zero. The player who cannot make a move, loses. To perform well in the magic totalizator, you need to learn to quickly determine which player wins, if both wizards play optimally: the one that moves first or the one that moves second. Input The first line contains a single integer _t_ — the number of input data sets (1u2009≤u2009_t_u2009≤u2009104). Each of the next _t_ lines contains two integers _a_, _b_ (0u2009≤u2009_a_,u2009_b_u2009≤u20091018). The numbers are separated by a space. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Output For any of the _t_ input sets print "First" (without the quotes) if the player who moves first wins. Print "Second" (without the quotes) if the player who moves second wins. Print the answers to different data sets on different lines in the order in which they are given in the input. Examples Output First Second Second First Note In the first sample, the first player should go to (11,10). Then, after a single move of the second player to (1,10), he will take 10 modulo 1 and win. In the second sample the first player has two moves to (1,10) and (21,10). After both moves the second player can win. In the third sample, the first player has no moves. In the fourth sample, the first player wins in one move, taking 30 modulo 10.
2,300
true
false
false
false
false
false
false
false
false
false
9,193
215A
Vasya's bicycle chain drive consists of two parts: _n_ stars are attached to the pedal axle, _m_ stars are attached to the rear wheel axle. The chain helps to rotate the rear wheel by transmitting the pedal rotation. We know that the _i_-th star on the pedal axle has _a__i_ (0u2009<u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_) teeth, and the _j_-th star on the rear wheel axle has _b__j_ (0u2009<u2009_b_1u2009<u2009_b_2u2009<u2009...u2009<u2009_b__m_) teeth. Any pair (_i_,u2009_j_) (1u2009≤u2009_i_u2009≤u2009_n_;xa01u2009≤u2009_j_u2009≤u2009_m_) is called a gear and sets the indexes of stars to which the chain is currently attached. Gear (_i_,u2009_j_) has a gear ratio, equal to the value . Since Vasya likes integers, he wants to find such gears (_i_,u2009_j_), that their ratios are integers. On the other hand, Vasya likes fast driving, so among all "integer" gears (_i_,u2009_j_) he wants to choose a gear with the maximum ratio. Help him to find the number of such gears. In the problem, fraction denotes division in real numbers, that is, no rounding is performed. Input The first input line contains integer _n_ (1u2009≤u2009_n_u2009≤u200950) — the number of stars on the bicycle's pedal axle. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009104) in the order of strict increasing. The third input line contains integer _m_ (1u2009≤u2009_m_u2009≤u200950) — the number of stars on the rear wheel axle. The fourth line contains _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (1u2009≤u2009_b__i_u2009≤u2009104) in the order of strict increasing. It is guaranteed that there exists at least one gear (_i_,u2009_j_), that its gear ratio is an integer. The numbers on the lines are separated by spaces. Output Print the number of "integer" gears with the maximum ratio among all "integer" gears. Examples Input 4 1 2 3 4 5 10 11 12 13 14 Note In the first sample the maximum "integer" gear ratio equals 3. There are two gears that have such gear ratio. For one of them _a_1u2009=u20094,u2009_b_1u2009=u200912, and for the other _a_2u2009=u20095,u2009_b_3u2009=u200915.
900
false
false
true
false
false
false
true
false
false
false
8,981
1270C
Let's call an array $$$a_1, a_2, dots, a_m$$$ of nonnegative integer numbers good if $$$a_1 + a_2 + dots + a_m = 2cdot(a_1 oplus a_2 oplus dots oplus a_m)$$$, where $$$oplus$$$ denotes the $$$. At the same time, array $$$[1, 2, 1, 3]$$$ isn't good, as $$$1 + 2 + 1 + 3 = 7 eq 2cdot 1 = 2cdot(1oplus 2 oplus 1 oplus 3)$$$. You are given an array of length $$$n$$$: $$$a_1, a_2, dots, a_n$$$. Append at most $$$3$$$ elements to it to make it good. Appended elements don't have to be different. It can be shown that the solution always exists under the given constraints. If there are different solutions, you are allowed to output any of them. Note that you don't have to minimize the number of added elements!. So, if an array is good already you are allowed to not append elements. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10,000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1le n le 10^5)$$$xa0— the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0le a_i le 10^9$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output two lines. In the first line, output a single integer $$$s$$$ ($$$0le sle 3$$$)xa0— the number of elements you want to append. In the second line, output $$$s$$$ integers $$$b_1, dots, b_s$$$ ($$$0le b_i le 10^{18}$$$)xa0— the elements you want to append to the array. If there are different solutions, you are allowed to output any of them. Example Input 3 4 1 2 3 6 1 8 2 1 1 Note In the first test case of the example, the sum of all numbers is $$$12$$$, and their $$$oplus$$$ is $$$6$$$, so the condition is already satisfied. In the second test case of the example, after adding $$$4, 4$$$, the array becomes $$$[8, 4, 4]$$$. The sum of numbers in it is $$$16$$$, $$$oplus$$$ of numbers in it is $$$8$$$.
1,400
true
false
false
false
false
true
false
false
false
false
4,342
750B
In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40u2009000 kilometers. Thus, travelling from North Pole to South Pole or vice versa takes exactly 20u2009000 kilometers. Limak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the North Pole. The description consists of _n_ parts. In the _i_-th part of his journey, Limak should move _t__i_ kilometers in the direction represented by a string _dir__i_ that is one of: "North", "South", "West", "East". Limak isn’t sure whether the description is valid. You must help him to check the following conditions: If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South. If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North. The journey must end on the North Pole. Check if the above conditions are satisfied and print "YES" or "NO" on a single line. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200950). The _i_-th of next _n_ lines contains an integer _t__i_ and a string _dir__i_ (1u2009≤u2009_t__i_u2009≤u2009106, )xa0— the length and the direction of the _i_-th part of the journey, according to the description Limak got. Output Print "YES" if the description satisfies the three conditions, otherwise print "NO", both without the quotes. Examples Input 5 7500 South 10000 East 3500 North 4444 West 4000 North Input 2 15000 South 4000 East Input 5 20000 South 1000 North 1000000 West 9000 North 10000 North Input 3 20000 South 10 East 20000 North Input 2 1000 North 1000 South Input 4 50 South 50 North 15000 South 15000 North Note Drawings below show how Limak's journey would look like in first two samples. In the second sample the answer is "NO" because he doesn't end on the North Pole.
1,300
false
false
true
false
false
false
false
false
false
false
6,792
1393C
Pinkie Pie has bought a bag of patty-cakes with different fillings! But it appeared that not all patty-cakes differ from one another with filling. In other words, the bag contains some patty-cakes with the same filling. Pinkie Pie eats the patty-cakes one-by-one. She likes having fun so she decided not to simply eat the patty-cakes but to try not to eat the patty-cakes with the same filling way too often. To achieve this she wants the minimum distance between the eaten with the same filling to be the largest possible. Herein Pinkie Pie called the distance between two patty-cakes the number of eaten patty-cakes strictly between them. Pinkie Pie can eat the patty-cakes in any order. She is impatient about eating all the patty-cakes up so she asks you to help her to count the greatest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating! Pinkie Pie is going to buy more bags of patty-cakes so she asks you to solve this problem for several bags! Input The first line contains a single integer $$$T$$$ ($$$1 le T le 100$$$): the number of bags for which you need to solve the problem. The first line of each bag description contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$): the number of patty-cakes in it. The second line of the bag description contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$): the information of patty-cakes' fillings: same fillings are defined as same integers, different fillings are defined as different integers. It is guaranteed that each bag contains at least two patty-cakes with the same filling. It is guaranteed that the sum of $$$n$$$ over all bags does not exceed $$$10^5$$$. Output For each bag print in separate line one single integer: the largest minimum distance between the eaten patty-cakes with the same filling amongst all possible orders of eating for that bag. Example Input 4 7 1 7 1 6 4 4 6 8 1 1 4 6 4 6 4 7 3 3 3 3 6 2 5 2 3 1 4 Note For the first bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $$$1$$$, $$$6$$$, $$$4$$$, $$$7$$$, $$$1$$$, $$$6$$$, $$$4$$$ (in this way, the minimum distance is equal to $$$3$$$). For the second bag Pinkie Pie can eat the patty-cakes in the following order (by fillings): $$$1$$$, $$$4$$$, $$$6$$$, $$$7$$$, $$$4$$$, $$$1$$$, $$$6$$$, $$$4$$$ (in this way, the minimum distance is equal to $$$2$$$).
1,700
true
true
false
false
false
true
false
false
true
false
3,705
789A
Anastasia loves going for a walk in Central Uzhlyandian Park. But she became uninterested in simple walking, so she began to collect Uzhlyandian pebbles. At first, she decided to collect all the pebbles she could find in the park. She has only two pockets. She can put at most _k_ pebbles in each pocket at the same time. There are _n_ different pebble types in the park, and there are _w__i_ pebbles of the _i_-th type. Anastasia is very responsible, so she never mixes pebbles of different types in same pocket. However, she can put different kinds of pebbles in different pockets at the same time. Unfortunately, she can't spend all her time collecting pebbles, so she can collect pebbles from the park only once a day. Help her to find the minimum number of days needed to collect all the pebbles of Uzhlyandian Central Park, taking into consideration that Anastasia can't place pebbles of different types in same pocket. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_k_u2009≤u2009109)xa0— the number of different pebble types and number of pebbles Anastasia can place in one pocket. The second line contains _n_ integers _w_1,u2009_w_2,u2009...,u2009_w__n_ (1u2009≤u2009_w__i_u2009≤u2009104)xa0— number of pebbles of each type. Output The only line of output contains one integerxa0— the minimum number of days Anastasia needs to collect all the pebbles. Note In the first sample case, Anastasia can collect all pebbles of the first type on the first day, of second typexa0— on the second day, and of third typexa0— on the third day. Optimal sequence of actions in the second sample case: In the first day Anastasia collects 8 pebbles of the third type. In the second day she collects 8 pebbles of the fourth type. In the third day she collects 3 pebbles of the first type and 1 pebble of the fourth type. In the fourth day she collects 7 pebbles of the fifth type. In the fifth day she collects 1 pebble of the second type.
1,100
true
false
true
false
false
false
false
false
false
false
6,616
1606E
Problem - 1606E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp math *2100 No tag edit access → Contest materials . Output Print one integerxa0— the number of ways to choose the initial health points for each hero $$$a_i$$$, where $$$1 le a_i le x$$$, so that there is no winner of the fight, taken modulo $$$998244353$$$. Examples Input 2 5 Output 5 Input 3 3 Output 15 Input 5 4 Output 1024 Input 13 37 Output 976890680
2,100
true
false
false
true
false
false
false
false
false
false
2,625
1003D
Polycarp has $$$n$$$ coins, the value of the $$$i$$$-th coin is $$$a_i$$$. It is guaranteed that all the values are integer powers of $$$2$$$ (i.e. $$$a_i = 2^d$$$ for some non-negative integer number $$$d$$$). Polycarp wants to know answers on $$$q$$$ queries. The $$$j$$$-th query is described as integer number $$$b_j$$$. The answer to the query is the minimum number of coins that is necessary to obtain the value $$$b_j$$$ using some subset of coins (Polycarp can use only coins he has). If Polycarp can't obtain the value $$$b_j$$$, the answer to the $$$j$$$-th query is -1. The queries are independent (the answer on the query doesn't affect Polycarp's coins). Input The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$) — the number of coins and the number of queries. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ — values of coins ($$$1 le a_i le 2 cdot 10^9$$$). It is guaranteed that all $$$a_i$$$ are integer powers of $$$2$$$ (i.e. $$$a_i = 2^d$$$ for some non-negative integer number $$$d$$$). The next $$$q$$$ lines contain one integer each. The $$$j$$$-th line contains one integer $$$b_j$$$ — the value of the $$$j$$$-th query ($$$1 le b_j le 10^9$$$). Output Print $$$q$$$ integers $$$ans_j$$$. The $$$j$$$-th integer must be equal to the answer on the $$$j$$$-th query. If Polycarp can't obtain the value $$$b_j$$$ the answer to the $$$j$$$-th query is -1.
1,600
false
true
false
false
false
false
false
false
false
false
5,671
704E
Tony Stark is playing a game with his suits (they have auto-pilot now). He lives in Malibu. Malibu has _n_ junctions numbered from 1 to _n_, connected with _n_u2009-u20091 roads. One can get from a junction to any other junction using these roads (graph of Malibu forms a tree). Tony has _m_ suits. There's a special plan for each suit. The _i_-th suit will appear at the moment of time _t__i_ in the junction _v__i_, and will move to junction _u__i_ using the shortest path between _v__i_ and _u__i_ with the speed _c__i_ roads per second (passing a junctions takes no time), and vanishing immediately when arriving at _u__i_ (if it reaches _u__i_ in time _q_, it's available there at moment _q_, but not in further moments). Also, suits move continuously (for example if _v__i_u2009≠u2009_u__i_, at time it's in the middle of a road. Please note that if _v__i_u2009=u2009_u__i_ it means the suit will be at junction number _v__i_ only at moment _t__i_ and then it vanishes. An explosion happens if at any moment of time two suits share the same exact location (it may be in a junction or somewhere on a road; while appearing, vanishing or moving). Your task is to tell Tony the moment of the the first explosion (if there will be any). Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100u2009000)xa0— the number of junctions and the number of suits respectively. The next _n_u2009-u20091 lines contain the roads descriptions. Each line contains two integers _a__i_ and _b__i_xa0— endpoints of the _i_-th road (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_). The next _m_ lines contain the suit descriptions. The _i_-th of them contains four integers _t__i_, _c__i_, _v__i_ and _u__i_ (0u2009≤u2009_t__i_u2009≤u200910u2009000,u20091u2009≤u2009_c__i_u2009≤u200910u2009000, 1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_), meaning the _i_-th suit will appear at moment of time _t__i_ at the junction _v__i_ and will move to the junction _u__i_ with a speed _c__i_ roads per second. Output If there would be no explosions at all, print -1 in the first and only line of output. Otherwise print the moment of the first explosion. Your answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20096. Examples Input 6 4 2 5 6 5 3 6 4 6 4 1 27 6 1 3 9 5 1 6 27 4 3 4 11 29 2 6 Input 6 4 3 1 4 5 6 4 6 1 2 6 16 4 4 5 13 20 6 2 3 16 4 5 28 5 3 5
3,300
false
false
false
false
true
false
false
false
false
false
7,002
1312B
Problem - 1312B - 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 *1000 No tag edit access → Contest materials . For example, if $$$a =
1,000
false
false
false
false
false
true
false
false
true
false
4,136
1835C
Famous worldwide astrophysicist Mleil waGrasse Tysok recently read about the existence of twin galaxy clusters. Before he shares this knowledge with the broader audience in his podcast called S.tarT-ok, he wants to prove their presence on his own. Mleil is aware that the vastness of the universe is astounding (almost as astounding as his observation skills) and decides to try his luck and find some new pair of twin clusters. To do so, he used his TLEscope to observe a part of the night sky that was not yet examined by humanity in which there are exactly $$$2^{k + 1}$$$ galaxies in a row. $$$i$$$-th of them consist of exactly $$$0 le g_i < 4^k$$$ stars. A galaxy cluster is any non-empty contiguous segment of galaxies. Moreover, its' trait is said to be equal to the . The second line contains $$$2^{k + 1}$$$ integers $$$g_1, g_2, ldots, g_{2^{k+1}}$$$ ($$$0 le g_i < 4^k$$$). We guarantee that the sum of values $$$2^{k + 1}$$$ over all test cases does not exceed $$$2^{18}$$$. Output Answers for all test cases should be present in separate lines. If there exist a pair of twin galaxies, then output four integers $$$a$$$, $$$b$$$, $$$c$$$, and $$$d$$$ denoting their inclusive ranges $$$[a, b]$$$ and $$$[c, d]$$$ (the first interval does not need to start before the second one, but they have to be disjoint). If no pair of such galaxies exist, output a single value $$$-1$$$ instead. Example Input 4 2 4 15 0 7 11 8 3 2 1 0 1 2 3 0 0 0 3 15 63 57 39 61 25 42 61 50 41 27 41 56 23 17 27 Output 2 4 6 6 2 2 3 4 1 1 2 2 1 1 4 10 Note In the first test case we pick intervals $$$[2, 4]$$$ and $$$[6, 6]$$$ as our twin clusters. The trait of the first interval equals $$$15 oplus 0 oplus 7 = 8$$$, and the trait of the second interval equals $$$8$$$, so these galaxy clusters are indeed twins.
2,600
true
false
false
false
false
true
true
false
false
false
1,276
1238B
Ivan plays an old action game called Heretic. He's stuck on one of the final levels of this game, so he needs some help with killing the monsters. The main part of the level is a large corridor (so large and narrow that it can be represented as an infinite coordinate line). The corridor is divided into two parts; let's assume that the point $$$x = 0$$$ is where these parts meet. The right part of the corridor is filled with $$$n$$$ monsters — for each monster, its initial coordinate $$$x_i$$$ is given (and since all monsters are in the right part, every $$$x_i$$$ is positive). The left part of the corridor is filled with crusher traps. If some monster enters the left part of the corridor or the origin (so, its current coordinate becomes less than or equal to $$$0$$$), it gets instantly killed by a trap. The main weapon Ivan uses to kill the monsters is the Phoenix Rod. It can launch a missile that explodes upon impact, obliterating every monster caught in the explosion and throwing all other monsters away from the epicenter. Formally, suppose that Ivan launches a missile so that it explodes in the point $$$c$$$. Then every monster is either killed by explosion or pushed away. Let some monster's current coordinate be $$$y$$$, then: if $$$c = y$$$, then the monster is killed; if $$$y < c$$$, then the monster is pushed $$$r$$$ units to the left, so its current coordinate becomes $$$y - r$$$; if $$$y > c$$$, then the monster is pushed $$$r$$$ units to the right, so its current coordinate becomes $$$y + r$$$. Ivan is going to kill the monsters as follows: choose some integer point $$$d$$$ and launch a missile into that point, then wait until it explodes and all the monsters which are pushed to the left part of the corridor are killed by crusher traps, then, if at least one monster is still alive, choose another integer point (probably the one that was already used) and launch a missile there, and so on. What is the minimum number of missiles Ivan has to launch in order to kill all of the monsters? You may assume that every time Ivan fires the Phoenix Rod, he chooses the impact point optimally. You have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 10^5$$$) — the number of queries. The first line of each query contains two integers $$$n$$$ and $$$r$$$ ($$$1 le n, r le 10^5$$$)xa0— the number of enemies and the distance that the enemies are thrown away from the epicenter of the explosion. The second line of each query contains $$$n$$$ integers $$$x_i$$$ ($$$1 le x_i le 10^5$$$)xa0— the initial positions of the monsters. It is guaranteed that sum of all $$$n$$$ over all queries does not exceed $$$10^5$$$. Note In the first test case, Ivan acts as follows: choose the point $$$3$$$, the first monster dies from a crusher trap at the point $$$-1$$$, the second monster dies from the explosion, the third monster is pushed to the point $$$7$$$; choose the point $$$7$$$, the third monster dies from the explosion. In the second test case, Ivan acts as follows: choose the point $$$5$$$, the first and fourth monsters die from the explosion, the second monster is pushed to the point $$$1$$$, the third monster is pushed to the point $$$2$$$; choose the point $$$2$$$, the first monster dies from a crusher trap at the point $$$0$$$, the second monster dies from the explosion.
1,300
false
true
false
false
false
false
false
false
true
false
4,502
1777B
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). There are $$$n! = n cdot (n-1) cdot (n - 2) cdot ldots cdot 1$$$ different permutations of length $$$n$$$. Given a permutation $$$p$$$ of $$$n$$$ numbers, we create an array $$$a$$$ consisting of $$$2n$$$ numbers, which is equal to $$$p$$$ concatenated with its reverse. We then define the beauty of $$$p$$$ as the number of inversions in $$$a$$$. The number of inversions in the array $$$a$$$ is the number of pairs of indices $$$i$$$, $$$j$$$ such that $$$i < j$$$ and $$$a_i > a_j$$$. For example, for permutation $$$p = [1, 2]$$$, $$$a$$$ would be $$$[1, 2, 2, 1]$$$. The inversions in $$$a$$$ are $$$(2, 4)$$$ and $$$(3, 4)$$$ (assuming 1-based indexing). Hence, the beauty of $$$p$$$ is $$$2$$$. Your task is to find the sum of beauties of all $$$n!$$$ permutations of size $$$n$$$. Print the remainder we get when dividing this value by $$$1,000,000,007$$$ ($$$10^9 + 7$$$). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. Each test case has only one line — the integer $$$n$$$ ($$$1 leq n leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print one integer — the sum of beauties of all permutations of size $$$n$$$ modulo $$$1,000,000,007$$$ ($$$10^9 + 7$$$). Note For the first test case of the example, $$$p = [1]$$$ is the only permutation. $$$a = [1, 1]$$$ has $$$0$$$ inversions. For the second test case of the example, the permutations are $$$[1, 2]$$$ and $$$[2, 1]$$$. Their respective $$$a$$$ arrays are $$$[1, 2, 2, 1]$$$ and $$$[2, 1, 1, 2]$$$, both of which have $$$2$$$ inversions.
900
true
true
false
false
false
false
false
false
false
false
1,613
212E
Сity N. has a huge problem with roads, food and IT-infrastructure. In total the city has _n_ junctions, some pairs of them are connected by bidirectional roads. The road network consists of _n_u2009-u20091 roads, you can get from any junction to any other one by these roads. Yes, you're right — the road network forms an undirected tree. Recently, the Mayor came up with a way that eliminates the problems with the food and the IT-infrastructure at the same time! He decided to put at the city junctions restaurants of two well-known cafe networks for IT professionals: "iMac D0naldz" and "Burger Bing". Since the network owners are not friends, it is strictly prohibited to place two restaurants of different networks on neighboring junctions. There are other requirements. Here's the full list: each junction must have at most one restaurant; each restaurant belongs either to "iMac D0naldz", or to "Burger Bing"; each network should build at least one restaurant; there is no pair of junctions that are connected by a road and contains restaurants of different networks. The Mayor is going to take a large tax from each restaurant, so he is interested in making the total number of the restaurants as large as possible. Help the Mayor to analyze the situation. Find all such pairs of (_a_,u2009_b_) that _a_ restaurants can belong to "iMac D0naldz", _b_ restaurants can belong to "Burger Bing", and the sum of _a_u2009+u2009_b_ is as large as possible. Input The first input line contains integer _n_ (3u2009≤u2009_n_u2009≤u20095000) — the number of junctions in the city. Next _n_u2009-u20091 lines list all roads one per line. Each road is given as a pair of integers _x__i_,u2009_y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_) — the indexes of connected junctions. Consider the junctions indexed from 1 to _n_. It is guaranteed that the given road network is represented by an undirected tree with _n_ vertexes. Output Print on the first line integer _z_ — the number of sought pairs. Then print all sought pairs (_a_,u2009_b_) in the order of increasing of the first component _a_. Examples Input 10 1 2 2 3 3 4 5 6 6 7 7 4 8 9 9 10 10 4 Output 6 1 8 2 7 3 6 6 3 7 2 8 1 Note The figure below shows the answers to the first test case. The junctions with "iMac D0naldz" restaurants are marked red and "Burger Bing" restaurants are marked blue.
1,500
false
false
false
true
false
false
false
false
false
false
8,989
936C
Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm! Of course, there is a code lock is installed on the safe. The lock has a screen that displays a string of _n_ lowercase Latin letters. Initially, the screen displays string _s_. Whitfield and Martin found out that the safe will open when string _t_ will be displayed on the screen. The string on the screen can be changed using the operation «shift _x_». In order to apply this operation, explorers choose an integer _x_ from 0 to _n_ inclusive. After that, the current string _p_u2009=u2009αβ changes to β_R_α, where the length of β is _x_, and the length of α is _n_u2009-u2009_x_. In other words, the suffix of the length _x_ of string _p_ is reversed and moved to the beginning of the string. For example, after the operation «shift 4» the string «abcacb» will be changed with string «bcacab », since αu2009=u2009ab, βu2009=u2009cacb, β_R_u2009=u2009bcac. Explorers are afraid that if they apply too many operations «shift», the lock will be locked forever. They ask you to find a way to get the string _t_ on the screen, using no more than 6100 operations. Input The first line contains an integer _n_, the length of the strings _s_ and _t_ (1u2009≤u2009_n_u2009≤u20092u2009000). After that, there are two strings _s_ and _t_, consisting of _n_ lowercase Latin letters each. Output If it is impossible to get string _t_ from string _s_ using no more than 6100 operations «shift», print a single number u2009-u20091. Otherwise, in the first line output the number of operations _k_ (0u2009≤u2009_k_u2009≤u20096100). In the next line output _k_ numbers _x__i_ corresponding to the operations «shift _x__i_» (0u2009≤u2009_x__i_u2009≤u2009_n_) in the order in which they should be applied. Examples In the first example, after applying the operations, the string on the screen will change as follows: 1. abacbb bbcaba 2. bbcaba ababbc 3. ababbc cbabab 4. cbabab babcba
2,300
false
false
true
false
false
true
false
false
false
false
5,956
607D
Genos and Saitama went shopping for Christmas trees. However, a different type of tree caught their attention, the exalted Power Tree. A Power Tree starts out as a single root vertex indexed 1. A Power Tree grows through a magical phenomenon known as an update. In an update, a single vertex is added to the tree as a child of some other vertex. Every vertex in the tree (the root and all the added vertices) has some value _v__i_ associated with it. The power of a vertex is defined as the strength of the multiset composed of the value associated with this vertex (_v__i_) and the powers of its direct children. The strength of a multiset is defined as the sum of all elements in the multiset multiplied by the number of elements in it. Or in other words for some multiset _S_: Saitama knows the updates that will be performed on the tree, so he decided to test Genos by asking him queries about the tree during its growth cycle. An update is of the form 1xa0_p_xa0_v_, and adds a new vertex with value _v_ as a child of vertex _p_. A query is of the form 2xa0_u_, and asks for the power of vertex _u_. Please help Genos respond to these queries modulo 109u2009+u20097. Input The first line of the input contains two space separated integers _v_1 and _q_ (1u2009≤u2009_v_1u2009<u2009109, 1u2009≤u2009_q_u2009≤u2009200u2009000) — the value of vertex 1 and the total number of updates and queries respectively. The next _q_ lines contain the updates and queries. Each of them has one of the following forms: 1xa0_p__i_xa0_v__i_, if these line describes an update. The index of the added vertex is equal to the smallest positive integer not yet used as an index in the tree. It is guaranteed that _p__i_ is some already existing vertex and 1u2009≤u2009_v__i_u2009<u2009109. 2xa0_u__i_, if these line describes a query. It is guaranteed _u__i_ will exist in the tree. It is guaranteed that the input will contain at least one query. Output For each query, print out the power of the given vertex modulo 109u2009+u20097. Examples Input 2 5 1 1 3 1 2 5 1 3 7 1 4 11 2 1 Input 5 5 1 1 4 1 2 3 2 2 1 2 7 2 1 Note For the first sample case, after all the updates the graph will have vertices labelled in the following manner: 1 — 2 — 3 — 4 — 5 These vertices will have corresponding values: 2 — 3 — 5 — 7 — 11 And corresponding powers: 344 — 170 — 82 — 36 — 11
2,600
false
false
false
false
true
false
false
false
false
false
7,416
1731D
Game studio "DbZ Games" wants to introduce another map in their popular game "Valiant". This time, the map named "Panvel" will be based on the city of Mumbai. Mumbai can be represented as $$$n imes m$$$ cellular grid. Each cell $$$(i, j)$$$ ($$$1 le i le n$$$; $$$1 le j le m$$$) of the grid is occupied by a cuboid building of height $$$a_{i,j}$$$. This time, DbZ Games want to make a map that has perfect vertical gameplay. That's why they want to choose an $$$l imes l$$$ square inside Mumbai, such that each building inside the square has a height of at least $$$l$$$. Can you help DbZ Games find such a square of the maximum possible size $$$l$$$? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 1000$$$). Description of the test cases follows. The first line of each test case contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 le n le m$$$; $$$1 leq n cdot m leq 10^6$$$). The $$$i$$$-th of next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i,1}, a_{i,2}, dots, a_{i,m}$$$ ($$$1 leq a_{i,j} leq 10^6$$$)xa0— heights of buildings on the $$$i$$$-th row. It's guaranteed that the sum of $$$n cdot m$$$ over all test cases doesn't exceed $$$10^6$$$. Output For each test case, print the maximum side length $$$l$$$ of the square DbZ Games can choose. Example Input 4 2 2 2 3 4 5 1 3 1 2 3 2 3 4 4 3 2 1 4 5 6 1 9 4 6 5 8 10 9 5 8 11 6 24 42 32 8 11 1 23 1 9 69 13 3 13 22 60 12 14 17 Note In the first test case, we can choose the square of side $$$l = 2$$$ (i.xa0e. the whole grid) since the heights of all buildings are greater than or equal to $$$2$$$. In the second test case, we can only choose the side as $$$1$$$, so the answer is $$$1$$$. In the third test case, there are no squares of size $$$2$$$ that have all buildings of height at least $$$2$$$, so the answer is $$$1$$$.
1,700
false
false
false
true
true
false
true
true
false
false
1,902
1864I
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags graphs greedy *3500 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST I. Future Dominators time limit per test4 seconds memory limit per test256 megabytes Dhruvil and amenotiomoi are sitting in different countries and chatting online. Initially, amenotiomoi has an empty board of size $$$n imes n$$$, and Dhruvil has a sequence of integers $$$1, 2, ldots, n^2$$$, each number occurring exactly once. The numbers may be placed in the cells of the board, each cell is either empty or contains exactly one number. The current state of the board is called good, if there is a way of placing the remaining numbers in empty cells so that all numbers except $$$1$$$ have a neighbor with a smaller value. Two cells are neighbors if they share an edge. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. The cell at the intersection of the $$$x$$$-th row and the $$$y$$$-th column is denoted as $$$(x, y)$$$. To chat, amenotiomoi asks $$$q$$$ queries to Dhruvil. Each time he provides Dhruvil with an empty cell $$$(x, y)$$$. Dhruvil has to place one of the remaining numbers in this cell so that the board is still good. Among all ways to do this, he chooses the largest possible number he can place and sends this number to amenotiomoi as the answer to the query. Since amenotiomoi knows the correct answer every time, he tells Dhruvil $$$(x oplus k,y oplus k)$$$ instead of $$$(x, y)$$$, where $$$k$$$ is the answer for the previous query. If amenotiomoi is sending the first query, he considers $$$k = 0$$$. Each time Dhruvil has to decode the query and send the answer to amenotiomoi. Here $$$oplus$$$ denotes the bitwise XOR operation. Help Dhruvil reply to all amenotiomoi's queries. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 10^3$$$, $$$1 le q le n^2$$$). The $$$i$$$-th of the following $$$q$$$ lines contains two integers $$$x_i'$$$ and $$$y_i'$$$. The corresponding cell is $$$(x_i, y_i)$$$, where $$$x_i'=x_i oplus k$$$ and $$$y_i' = y_i oplus k$$$, where $$$k$$$ is the correct answer for the previous query. If $$$i = 1$$$, then $$$k = 0$$$ is used. It is guaranteed that $$$1 le x_i, y_i le n$$$ and that $$$(x_i, y_i)$$$ is an empty cell at the moment of $$$i$$$-th query. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output one line, containing the answers to all queries, separated by spaces. Example input 3 2 4 1 1 6 6 3 0 1 2 3 9 2 1 8 11 4 4 4 4 2 0 4 4 11 10 1 3 3 2 1 1 1 1 output 4 2 3 1 9 7 6 3 5 8 2 1 4 1 Note In the first test case, the first query is $$$(1, 1)$$$, Dhruvil puts $$$4$$$ in that cell. The second query is $$$(6, 6)$$$, Dhruvil decode it as $$$(2, 2)$$$ using the previous answer ($$$2 oplus 4 = 6$$$). If Dhruvil places $$$3$$$ to the cell $$$(2, 2)$$$, the board stops being good. So, Dhruvil places $$$2$$$ in this cell. The third query is $$$(3, 0)$$$, Dhruvil decodes it as $$$(1, 2)$$$ using the previous answer ($$$1 oplus 2 = 3$$$, $$$2 oplus 2 = 0$$$). Dhruvil can place $$$3$$$ in this cell. The fourth query is $$$(1, 2)$$$, Dhruvil decodes it as $$$(2, 1)$$$ using the previous answer ($$$2 oplus 3 = 1$$$, $$$1 oplus 3 = 2$$$). Now, only $$$1$$$ remains in the sequence, and after Dhruvil places $$$1$$$ in this cell, the board becomes full and remains good. Here you can see the entire history of the board: In the second test case, the final state of the board is: $$$8$$$ $$$7$$$ $$$5$$$ $$$9$$$ $$$3$$$ $$$4$$$ $$$1$$$ $$$2$$$ $$$6$$$ Codeforces (c)
3,500
false
true
false
false
false
false
false
false
false
true
1,089
2022E2
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 978 (Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search combinatorics data structures dsu graphs *2600 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E2. Billetes MX (Hard Version) time limit per test2 seconds memory limit per test512 megabytes This is the hard version of the problem. In this version, it is guaranteed that $$$q leq 10^5$$$. You can make hacks only if both versions of the problem are solved. An integer grid $$$A$$$ with $$$p$$$ rows and $$$q$$$ columns is called beautiful if: All elements of the grid are integers between $$$0$$$ and $$$2^{30}-1$$$, and For any subgrid, the XOR of the values at the corners is equal to $$$0$$$. Formally, for any four integers $$$i_1$$$, $$$i_2$$$, $$$j_1$$$, $$$j_2$$$ ($$$1 le i_1 < i_2 le p$$$; $$$1 le j_1 < j_2 le q$$$), $$$A_{i_1, j_1} oplus A_{i_1, j_2} oplus A_{i_2, j_1} oplus A_{i_2, j_2} = 0$$$, where $$$oplus$$$ denotes the bitwise XOR operation. There is a partially filled integer grid $$$G$$$ with $$$n$$$ rows and $$$m$$$ columns where only $$$k$$$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful. However, Monocarp thinks that this problem is too easy. Therefore, he will perform $$$q$$$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates. For each of the $$$q + 1$$$ states of the grid, the initial state and after each of the $$$q$$$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $$$10^9+7$$$. Input The first line contains $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$k$$$ and $$$q$$$ ($$$2 le n, m le 10^5$$$; $$$0 le k, q leq 10^5$$$)xa0— the number of rows, the number of columns, the number of fixed cells, and the number of updates. The following $$$k$$$ lines contain three integers $$$r$$$, $$$c$$$ and $$$v$$$ ($$$1 le r le n, 1 le c le m$$$; $$$0 le v < 2^{30}$$$) indicating that $$$G_{r, c}$$$ is assigned the integer $$$v$$$. The following $$$q$$$ lines contain three integers $$$r$$$, $$$c$$$ and $$$v$$$ ($$$1 le r le n, 1 le c le m$$$; $$$0 le v < 2^{30}$$$) indicating that $$$G_{r, c}$$$ is assigned the integer $$$v$$$. It is guaranteed that the pairs $$$(r,c)$$$ over all assignments are distinct. It is guaranteed that the sum of $$$n$$$, $$$m$$$, $$$k$$$ and $$$q$$$ over all test cases does not exceed $$$10^5$$$ respectively. Output For each test case, output $$$q + 1$$$ lines. The $$$i$$$-th line of output should contain the answer of the $$$i$$$-th state of the grid modulo $$$10^9 + 7$$$. Example input 3 3 3 8 1 2 1 6 3 2 12 1 2 6 2 2 0 1 3 10 1 1 0 2 3 12 3 1 10 3 3 1 2 5 2 0 1 1 10 1 2 30 2 5 0 2 1 1 10 1 2 30 output 1 0 489373567 651321892 769740174 489373567 Note In the first test case of the example, we initially have the following grid: $$$0$$$ $$$6$$$ $$$10$$$ $$$6$$$ $$$0$$$ $$$12$$$ $$$10$$$ $$$12$$$ $$$?$$$ It can be proven that the only valid value for tile $$$(3, 3)$$$ is $$$0$$$, so the first answer is $$$1$$$. For the second query, the grid does not satisfy the condition, and thus the answer is $$$0$$$. Codeforces (c)
2,600
false
false
false
false
true
false
false
true
false
true
132
1804D
Annie is an amateur photographer. She likes to take pictures of giant residential buildings at night. She just took a picture of a huge rectangular building that can be seen as a table of $$$n imes m$$$ windows. That means that the building has $$$n$$$ floors and each floor has exactly $$$m$$$ windows. Each window is either dark or bright, meaning there is light turned on in the room behind it. Annies knows that each apartment in this building is either one-bedroom or two-bedroom. Each one-bedroom apartment has exactly one window representing it on the picture, and each two-bedroom apartment has exactly two consecutive windows on the same floor. Moreover, the value of $$$m$$$ is guaranteed to be divisible by $$$4$$$ and it is known that each floor has exactly $$$frac{m}{4}$$$ two-bedroom apartments and exactly $$$frac{m}{2}$$$ one-bedroom apartments. The actual layout of apartments is unknown and can be different for each floor. Annie considers an apartment to be occupied if at least one of its windows is bright. She now wonders, what are the minimum and maximum possible number of occupied apartments if judged by the given picture? Formally, for each of the floors, she comes up with some particular apartments layout with exactly $$$frac{m}{4}$$$ two-bedroom apartments (two consecutive windows) and $$$frac{m}{2}$$$ one-bedroom apartments (single window). She then counts the total number of apartments that have at least one bright window. What is the minimum and maximum possible number she can get? Input The first line of the input contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 leq n cdot m leq 5 cdot 10^5$$$)xa0— the number of floors in the building and the number of windows per floor, respectively. It is guaranteed that $$$m$$$ is divisible by $$$4$$$. Then follow $$$n$$$ lines containing $$$m$$$ characters each. The $$$j$$$-th character of the $$$i$$$-th line is "0" if the $$$j$$$-th window on the $$$i$$$-th floor is dark, and is "1" if this window is bright. Output Print two integers, the minimum possible number of occupied apartments and the maximum possible number of occupied apartments, assuming each floor can have an individual layout of $$$frac{m}{4}$$$ two-bedroom and $$$frac{m}{2}$$$ one-bedroom apartments. Examples Input 5 4 0100 1100 0110 1010 1011 Note In the first example, each floor consists of one two-bedroom apartment and two one-bedroom apartments. The following apartment layout achieves the minimum possible number of occupied apartments equal to $$$7$$$. 0 100 1 100 01 10 10 10 101 1 The following apartment layout achieves the maximum possible number of occupied apartments equal to $$$10$$$. 0 100 11 00 0 110 10 10 1 011
2,000
false
true
true
true
false
false
true
false
false
false
1,454
359B
Problem - 359B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms dp math *1400 No tag edit access → Contest materials ") ") . Output Print 2_n_ integers _a_1,u2009_a_2,u2009...,u2009_a_2_n_ — the required permutation _a_. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them. Examples Input 1 0 Output 1 2 Input 2 1 Output 3 2 1 4 Input 4 0 Output 2 7 4 6 1 3 5 8 Note Record _x_ represents the absolute value of number _x_. In the first sample 1u2009-u20092u2009-u20091u2009-u20092u2009=u20090. In the second sample 3u2009-u20092u2009+u20091u2009-u20094u2009-u20093u2009-u20092u2009+u20091u2009-u20094u2009=u20091u2009+u20093u2009-u20092u2009=u20092. In the third sample 2u2009-u20097u2009+u20094u2009-u20096u2009+u20091u2009-u20093u2009+u20095u2009-u20098u2009-u20092u2009-u20097u2009+u20094u2009-u20096u2009+u20091u2009-u20093u2009+u20095u2009-u20098u2009=u200912u2009-u200912u2009=u20090.
1,400
true
false
false
true
false
true
false
false
false
false
8,398
1181A
Soon after the Chunga-Changa island was discovered, it started to acquire some forms of civilization and even market economy. A new currency arose, colloquially called "chizhik". One has to pay in chizhiks to buy a coconut now. Sasha and Masha are about to buy some coconuts which are sold at price $$$z$$$ chizhiks per coconut. Sasha has $$$x$$$ chizhiks, Masha has $$$y$$$ chizhiks. Each girl will buy as many coconuts as she can using only her money. This way each girl will buy an integer non-negative number of coconuts. The girls discussed their plans and found that the total number of coconuts they buy can increase (or decrease) if one of them gives several chizhiks to the other girl. The chizhiks can't be split in parts, so the girls can only exchange with integer number of chizhiks. Consider the following example. Suppose Sasha has $$$5$$$ chizhiks, Masha has $$$4$$$ chizhiks, and the price for one coconut be $$$3$$$ chizhiks. If the girls don't exchange with chizhiks, they will buy $$$1 + 1 = 2$$$ coconuts. However, if, for example, Masha gives Sasha one chizhik, then Sasha will have $$$6$$$ chizhiks, Masha will have $$$3$$$ chizhiks, and the girls will buy $$$2 + 1 = 3$$$ coconuts. It is not that easy to live on the island now, so Sasha and Mash want to exchange with chizhiks in such a way that they will buy the maximum possible number of coconuts. Nobody wants to have a debt, so among all possible ways to buy the maximum possible number of coconuts find such a way that minimizes the number of chizhiks one girl gives to the other (it is not important who will be the person giving the chizhiks). Input The first line contains three integers $$$x$$$, $$$y$$$ and $$$z$$$ ($$$0 le x, y le 10^{18}$$$, $$$1 le z le 10^{18}$$$)xa0— the number of chizhics Sasha has, the number of chizhics Masha has and the price of a coconut.
1,000
true
true
false
false
false
false
false
false
false
false
4,814
1889D
Problem - 1889D - 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 dfs and similar graphs implementation trees *3000 No tag edit access → Contest materials ") : stacks := an array that contains n stacks r
3,000
false
false
true
false
false
false
true
false
false
true
962
1537C
You are a game designer and want to make an obstacle course. The player will walk from left to right. You have $$$n$$$ heights of mountains already selected and want to arrange them so that the absolute difference of the heights of the first and last mountains is as small as possible. In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains $$$i$$$ ($$$1 leq i < n$$$) such that $$$h_i leq h_{i+1}$$$ where $$$h_i$$$ is the height of the $$$i$$$-th mountain. You don't want to waste any of the mountains you modelled, so you have to use all of them. From all the arrangements that minimize $$$h_1-h_n$$$, find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any. Input The first line will contain a single integer $$$t$$$ ($$$1 leq t leq 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$) — the number of mountains. The second line of each test case contains $$$n$$$ integers $$$h_1,ldots,h_n$$$ ($$$1 leq h_i leq 10^9$$$), where $$$h_i$$$ is the height of the $$$i$$$-th mountain. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$n$$$ integers — the given heights in an order that maximizes the difficulty score among all orders that minimize $$$h_1-h_n$$$. If there are multiple orders that satisfy these requirements, you may output any. Note In the first test case: The player begins at height $$$2$$$, next going up to height $$$4$$$ increasing the difficulty by $$$1$$$. After that he will go down to height $$$1$$$ and the difficulty doesn't change because he is going downhill. Finally the player will go up to height $$$2$$$ and the difficulty will increase by $$$1$$$. The absolute difference between the starting height and the end height is equal to $$$0$$$ and it's minimal. The difficulty is maximal. In the second test case: The player begins at height $$$1$$$, next going up to height $$$3$$$ increasing the difficulty by $$$1$$$. The absolute difference between the starting height and the end height is equal to $$$2$$$ and it's minimal as they are the only heights. The difficulty is maximal.
1,200
true
true
true
false
false
true
false
false
false
false
2,960
1701B
Recall that a permutation of length $$$n$$$ is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once. For a fixed positive integer $$$d$$$, let's define the cost of the permutation $$$p$$$ of length $$$n$$$ as the number of indices $$$i$$$ $$$(1 le i < n)$$$ such that $$$p_i cdot d = p_{i + 1}$$$. For example, if $$$d = 3$$$ and $$$p = [5, 2, 6, 7, 1, 3, 4]$$$, then the cost of such a permutation is $$$2$$$, because $$$p_2 cdot 3 = p_3$$$ and $$$p_5 cdot 3 = p_6$$$. Your task is the following one: for a given value $$$n$$$, find the permutation of length $$$n$$$ and the value $$$d$$$ with maximum possible cost (over all ways to choose the permutation and $$$d$$$). If there are multiple answers, then print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. The single line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the value $$$d$$$ in the first line, and $$$n$$$ integers in the second linexa0— the permutation itself. If there are multiple answers, then print any of them.
800
false
true
false
false
false
false
false
false
false
false
2,075
940F
You come home and fell some unpleasant smell. Where is it coming from? You are given an array _a_. You have to answer the following queries: 1. You are given two integers _l_ and _r_. Let _c__i_ be the number of occurrences of _i_ in _a__l_:u2009_r_, where _a__l_:u2009_r_ is the subarray of _a_ from _l_-th element to _r_-th inclusive. Find the Mex of {_c_0,u2009_c_1,u2009...,u2009_c_109} 2. You are given two integers _p_ to _x_. Change _a__p_ to _x_. The Mex of a multiset of numbers is the smallest non-negative integer not in the set. Note that in this problem all elements of _a_ are positive, which means that _c_0 = 0 and 0 is never the answer for the query of the second type. Input The first line of input contains two integers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009100u2009000)xa0— the length of the array and the number of queries respectively. The second line of input contains _n_ integersxa0— _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Each of the next _q_ lines describes a single query. The first type of query is described by three integers _t__i_u2009=u20091, _l__i_, _r__i_, where 1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_xa0— the bounds of the subarray. The second type of query is described by three integers _t__i_u2009=u20092, _p__i_, _x__i_, where 1u2009≤u2009_p__i_u2009≤u2009_n_ is the index of the element, which must be changed and 1u2009≤u2009_x__i_u2009≤u2009109 is the new value. Output For each query of the first type output a single integer xa0— the Mex of {_c_0,u2009_c_1,u2009...,u2009_c_109}. Example Input 10 4 1 2 3 1 1 2 2 2 9 9 1 1 1 1 2 8 2 7 1 1 2 8 Note The subarray of the first query consists of the single elementxa0— 1. The subarray of the second query consists of four 2s, one 3 and two 1s. The subarray of the fourth query consists of three 1s, three 2s and one 3.
2,600
false
false
false
false
true
false
true
false
false
false
5,933
1194C
You are given three strings $$$s$$$, $$$t$$$ and $$$p$$$ consisting of lowercase Latin letters. You may perform any number (possibly, zero) operations on these strings. During each operation you choose any character from $$$p$$$, erase it from $$$p$$$ and insert it into string $$$s$$$ (you may insert this character anywhere you want: in the beginning of $$$s$$$, in the end or between any two consecutive characters). For example, if $$$p$$$ is aba, and $$$s$$$ is de, then the following outcomes are possible (the character we erase from $$$p$$$ and insert into $$$s$$$ is highlighted): aba $$$ ightarrow$$$ ba, de $$$ ightarrow$$$ ade; aba $$$ ightarrow$$$ ba, de $$$ ightarrow$$$ dae; aba $$$ ightarrow$$$ ba, de $$$ ightarrow$$$ dea; aba $$$ ightarrow$$$ aa, de $$$ ightarrow$$$ bde; aba $$$ ightarrow$$$ aa, de $$$ ightarrow$$$ dbe; aba $$$ ightarrow$$$ aa, de $$$ ightarrow$$$ deb; aba $$$ ightarrow$$$ ab, de $$$ ightarrow$$$ ade; aba $$$ ightarrow$$$ ab, de $$$ ightarrow$$$ dae; aba $$$ ightarrow$$$ ab, de $$$ ightarrow$$$ dea; Your goal is to perform several (maybe zero) operations so that $$$s$$$ becomes equal to $$$t$$$. Please determine whether it is possible. Note that you have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 100$$$) — the number of queries. Each query is represented by three consecutive lines. The first line of each query contains the string $$$s$$$ ($$$1 le s le 100$$$) consisting of lowercase Latin letters. The second line of each query contains the string $$$t$$$ ($$$1 le t le 100$$$) consisting of lowercase Latin letters. The third line of each query contains the string $$$p$$$ ($$$1 le p le 100$$$) consisting of lowercase Latin letters. Output For each query print YES if it is possible to make $$$s$$$ equal to $$$t$$$, and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES will all be recognized as positive answer). Example Input 4 ab acxb cax a aaaa aaabbcc a aaaa aabbcc ab baaa aaaaa Note In the first test case there is the following sequence of operation: 1. $$$s = $$$ ab, $$$t = $$$ acxb, $$$p = $$$ cax; 2. $$$s = $$$ acb, $$$t = $$$ acxb, $$$p = $$$ ax; 3. $$$s = $$$ acxb, $$$t = $$$ acxb, $$$p = $$$ a. In the second test case there is the following sequence of operation: 1. $$$s = $$$ a, $$$t = $$$ aaaa, $$$p = $$$ aaabbcc; 2. $$$s = $$$ aa, $$$t = $$$ aaaa, $$$p = $$$ aabbcc; 3. $$$s = $$$ aaa, $$$t = $$$ aaaa, $$$p = $$$ abbcc; 4. $$$s = $$$ aaaa, $$$t = $$$ aaaa, $$$p = $$$ bbcc.
1,300
false
false
true
false
false
false
false
false
false
false
4,734
868E
You are given a tree (a connected non-oriented graph without cycles) with vertices numbered from 1 to _n_, and the length of the _i_-th edge is _w__i_. In the vertex _s_ there is a policeman, in the vertices _x_1,u2009_x_2,u2009...,u2009_x__m_ (_x__j_u2009≠u2009_s_) _m_ criminals are located. The policeman can walk along the edges with speed 1, the criminals can move with arbitrary large speed. If a criminal at some moment is at the same point as the policeman, he instantly gets caught by the policeman. Determine the time needed for the policeman to catch all criminals, assuming everybody behaves optimally (i.e. the criminals maximize that time, the policeman minimizes that time). Everybody knows positions of everybody else at any moment of time. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u200950)xa0— the number of vertices in the tree. The next _n_u2009-u20091 lines contain three integers each: _u__i_, _v__i_, _w__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, 1u2009≤u2009_w__i_u2009≤u200950) denoting edges and their lengths. It is guaranteed that the given graph is a tree. The next line contains single integer _s_ (1u2009≤u2009_s_u2009≤u2009_n_)xa0— the number of vertex where the policeman starts. The next line contains single integer _m_ (1u2009≤u2009_m_u2009≤u200950)xa0— the number of criminals. The next line contains _m_ integers _x_1,u2009_x_2,u2009...,u2009_x__m_ (1u2009≤u2009_x__j_u2009≤u2009_n_, _x__j_u2009≠u2009_s_)xa0— the number of vertices where the criminals are located. _x__j_ are not necessarily distinct. Output If the policeman can't catch criminals, print single line "Terrorists win" (without quotes). Otherwise, print single integerxa0— the time needed to catch all criminals. Examples Input 4 1 2 2 1 3 1 1 4 1 2 4 3 1 4 1 Input 6 1 2 3 2 3 5 3 4 1 3 5 4 2 6 3 2 3 1 3 5 Note In the first example one of the optimal scenarios is the following. The criminal number 2 moves to vertex 3, the criminal 4xa0— to vertex 4. The policeman goes to vertex 4 and catches two criminals. After that the criminal number 1 moves to the vertex 2. The policeman goes to vertex 3 and catches criminal 2, then goes to the vertex 2 and catches the remaining criminal.
2,700
false
false
false
true
false
false
false
false
false
true
6,269
681C
Petya has recently learned data structure named "Binary heap". The heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap; Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal. In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format: insert _x_xa0— put the element with value _x_ in the heap; getMin _x_xa0— the value of the minimum element contained in the heap was equal to _x_; removeMinxa0— the minimum element was extracted from the heap (only one instance, if there were many). All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied. While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats. Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied. Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log. Input The first line of the input contains the only integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of the records left in Petya's journal. Each of the following _n_ lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value. Output The first line of the output should contain a single integer _m_xa0— the minimum possible number of records in the modified sequence of operations. Next _m_ lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value. Note that the input sequence of operations must be the subsequence of the output sequence. It's guaranteed that there exists the correct answer consisting of no more than 1u2009000u2009000 operations. Note In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap. In the second sample case number 1 is inserted two times, so should be similarly removed twice.
1,600
false
true
false
false
true
true
false
false
false
false
7,101
741E
All of us know that girls in Arpa’s land are... ok, you’ve got the idea :D Anyone knows that Arpa isn't a normal man, he is ... well, sorry, I can't explain it more. Mehrdad is interested about the reason, so he asked Sipa, one of the best biology scientists in Arpa's land, for help. Sipa has a DNA editor. Sipa put Arpa under the DNA editor. DNA editor showed Arpa's DNA as a string _S_ consisting of _n_ lowercase English letters. Also Sipa has another DNA _T_ consisting of lowercase English letters that belongs to a normal man. Now there are (_n_u2009+u20091) options to change Arpa's DNA, numbered from 0 to _n_. _i_-th of them is to put _T_ between _i_-th and (_i_u2009+u20091)-th characters of _S_ (0u2009≤u2009_i_u2009≤u2009_n_). If _i_u2009=u20090, _T_ will be put before _S_, and if _i_u2009=u2009_n_, it will be put after _S_. Mehrdad wants to choose the most interesting option for Arpa's DNA among these _n_u2009+u20091 options. DNA _A_ is more interesting than _B_ if _A_ is lexicographically smaller than _B_. Mehrdad asked Sipa _q_ questions: Given integers _l_,u2009_r_,u2009_k_,u2009_x_,u2009_y_, what is the most interesting option if we only consider such options _i_ that _l_u2009≤u2009_i_u2009≤u2009_r_ and ? If there are several most interesting options, Mehrdad wants to know one with the smallest number _i_. Since Sipa is a biology scientist but not a programmer, you should help him. Input The first line contains strings _S_, _T_ and integer _q_ (1u2009≤u2009_S_,u2009_T_,u2009_q_u2009≤u2009105)xa0— Arpa's DNA, the DNA of a normal man, and the number of Mehrdad's questions. The strings _S_ and _T_ consist only of small English letters. Next _q_ lines describe the Mehrdad's questions. Each of these lines contain five integers _l_,u2009_r_,u2009_k_,u2009_x_,u2009_y_ (0u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_k_u2009≤u2009_n_, 0u2009≤u2009_x_u2009≤u2009_y_u2009<u2009_k_). Output Print _q_ integers. The _j_-th of them should be the number _i_ of the most interesting option among those that satisfy the conditions of the _j_-th question. If there is no option _i_ satisfying the conditions in some question, print -1. Note Explanation of first sample case: In the first question Sipa has two options: dabc (_i_u2009=u20090) and abdc (_i_u2009=u20092). The latter (abcd) is better than abdc, so answer is 2. In the last question there is no _i_ such that 0u2009≤u2009_i_u2009≤u20091 and .
3,400
false
false
false
false
true
false
false
false
false
false
6,832
1299E
This problem is interactive. We have hidden a permutation $$$p_1, p_2, dots, p_n$$$ of numbers from $$$1$$$ to $$$n$$$ from you, where $$$n$$$ is even. You can try to guess it using the following queries: $$$?$$$ $$$k$$$ $$$a_1$$$ $$$a_2$$$ $$$dots$$$ $$$a_k$$$. In response, you will learn if the average of elements with indexes $$$a_1, a_2, dots, a_k$$$ is an integer. In other words, you will receive $$$1$$$ if $$$frac{p_{a_1} + p_{a_2} + dots + p_{a_k}}{k}$$$ is integer, and $$$0$$$ otherwise. You have to guess the permutation. You can ask not more than $$$18n$$$ queries. Note that permutations $$$[p_1, p_2, dots, p_k]$$$ and $$$[n + 1 - p_1, n + 1 - p_2, dots, n + 1 - p_k]$$$ are indistinguishable. Therefore, you are guaranteed that $$$p_1 le frac{n}{2}$$$. Note that the permutation $$$p$$$ is fixed before the start of the interaction and doesn't depend on your queries. In other words, interactor is not adaptive. Note that you don't have to minimize the number of queries. Interaction You begin the interaction by reading $$$n$$$. To ask a question about elements on positions $$$a_1, a_2, dots, a_k$$$, in a separate line output $$$?$$$ $$$k$$$ $$$a_1$$$ $$$a_2$$$ ... $$$a_k$$$ Numbers in the query have to satisfy $$$1 le a_i le n$$$, and all $$$a_i$$$ have to be different. Don't forget to 'flush', to get the answer. In response, you will receive $$$1$$$ if $$$frac{p_{a_1} + p_{a_2} + dots + p_{a_k}}{k}$$$ is integer, and $$$0$$$ otherwise. In case your query is invalid or you asked more than $$$18n$$$ queries, the program will print $$$-1$$$ and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you determine permutation, output $$$!$$$ $$$p_1$$$ $$$p_2$$$ ... $$$p_n$$$ After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack format For the hacks use the following format: The first line has to contain a single integer $$$n$$$ ($$$2 le n le 800$$$, $$$n$$$ is even). In the next line output $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$xa0— the valid permutation of numbers from $$$1$$$ to $$$n$$$. $$$p_1 le frac{n}{2}$$$ must hold.
3,400
true
false
false
false
false
false
false
false
false
false
4,197
958E1
The Rebel fleet is afraid that the Empire might want to strike back again. Princess Heidi needs to know if it is possible to assign _R_ Rebel spaceships to guard _B_ bases so that every base has exactly one guardian and each spaceship has exactly one assigned base (in other words, the assignment is a perfect matching). Since she knows how reckless her pilots are, she wants to be sure that any two (straight) paths – from a base to its assigned spaceship – do not intersect in the galaxy plane (that is, in 2D), and so there is no risk of collision. Input The first line contains two space-separated integers _R_,u2009_B_(1u2009≤u2009_R_,u2009_B_u2009≤u200910). For 1u2009≤u2009_i_u2009≤u2009_R_, the _i_u2009+u20091-th line contains two space-separated integers _x__i_ and _y__i_ (_x__i_,u2009_y__i_u2009≤u200910000) denoting the coordinates of the _i_-th Rebel spaceship. The following _B_ lines have the same format, denoting the position of bases. It is guaranteed that no two points coincide and that no three points are on the same line. Output If it is possible to connect Rebel spaceships and bases so as satisfy the constraint, output Yes, otherwise output No (without quote). Examples Input 3 3 0 0 2 0 3 1 -2 1 0 3 2 2 Note For the first example, one possible way is to connect the Rebels and bases in order. For the second example, there is no perfect matching between Rebels and bases.
1,600
true
true
false
false
false
false
true
false
false
false
5,883
171H
Problem - 171H - Codeforces =============== xa0 separated by a single space. Output Output two integers separated by a single space. Examples Input 1 0 Output 0 0 Input 2 15 Output 3 0 Input 4 160 Output 12 12
1,700
false
false
true
false
false
false
false
false
false
false
9,179
1444C
The new academic year has started, and Berland's university has $$$n$$$ first-year students. They are divided into $$$k$$$ academic groups, however, some of the groups might be empty. Among the students, there are $$$m$$$ pairs of acquaintances, and each acquaintance pair might be both in a common group or be in two different groups. Alice is the curator of the first years, she wants to host an entertaining game to make everyone know each other. To do that, she will select two different academic groups and then divide the students of those groups into two teams. The game requires that there are no acquaintance pairs inside each of the teams. Alice wonders how many pairs of groups she can select, such that it'll be possible to play a game after that. All students of the two selected groups must take part in the game. Please note, that the teams Alice will form for the game don't need to coincide with groups the students learn in. Moreover, teams may have different sizes (or even be empty). Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le n le 500,000$$$; $$$0 le m le 500,000$$$; $$$2 le k le 500,000$$$)xa0— the number of students, the number of pairs of acquaintances and the number of groups respectively. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le k$$$), where $$$c_i$$$ equals to the group number of the $$$i$$$-th student. Next $$$m$$$ lines follow. The $$$i$$$-th of them contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$), denoting that students $$$a_i$$$ and $$$b_i$$$ are acquaintances. It's guaranteed, that $$$a_i eq b_i$$$, and that no (unordered) pair is mentioned more than once. Output Print a single integerxa0— the number of ways to choose two different groups such that it's possible to select two teams to play the game. Examples Input 6 8 3 1 1 2 2 3 3 1 3 1 5 1 6 2 5 2 6 3 4 3 5 5 6 Input 4 3 3 1 1 2 2 1 2 2 3 3 4 Input 4 4 2 1 1 1 2 1 2 2 3 3 1 1 4 Input 5 5 2 1 2 1 2 1 1 2 2 3 3 4 4 5 5 1 Note The acquaintances graph for the first example is shown in the picture below (next to each student there is their group number written). In that test we can select the following groups: Select the first and the second groups. For instance, one team can be formed from students $$$1$$$ and $$$4$$$, while other team can be formed from students $$$2$$$ and $$$3$$$. Select the second and the third group. For instance, one team can be formed $$$3$$$ and $$$6$$$, while other team can be formed from students $$$4$$$ and $$$5$$$. We can't select the first and the third group, because there is no way to form the teams for the game. In the second example, we can select any group pair. Please note, that even though the third group has no students, we still can select it (with some other group) for the game.
2,500
false
false
false
false
true
false
false
false
false
true
3,452
670D1
This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only. Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs _n_ ingredients, and for each ingredient she knows the value _a__i_xa0— how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all _n_ ingredients. Apollinaria has _b__i_ gram of the _i_-th ingredient. Also she has _k_ grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the _n_ ingredients and can be used for baking cookies. Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder. Input The first line of the input contains two positive integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20091000)xa0— the number of ingredients and the number of grams of the magic powder. The second line contains the sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091000), where the _i_-th number is equal to the number of grams of the _i_-th ingredient, needed to bake one cookie. The third line contains the sequence _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u20091000), where the _i_-th number is equal to the number of grams of the _i_-th ingredient, which Apollinaria has. Output Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder. Examples Input 4 3 4 3 5 6 11 12 14 20 Note In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies. In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.
1,400
false
false
true
false
false
false
true
true
false
false
7,144
645F
In an attempt to make peace with the Mischievious Mess Makers, Bessie and Farmer John are planning to plant some flower gardens to complement the lush, grassy fields of Bovinia. As any good horticulturist knows, each garden they plant must have the exact same arrangement of flowers. Initially, Farmer John has _n_ different species of flowers he can plant, with _a__i_ flowers of the _i_-th species. On each of the next _q_ days, Farmer John will receive a batch of flowers of a new species. On day _j_, he will receive _c__j_ flowers of the same species, but of a different species from those Farmer John already has. Farmer John, knowing the right balance between extravagance and minimalism, wants exactly _k_ species of flowers to be used. Furthermore, to reduce waste, each flower of the _k_ species Farmer John chooses must be planted in some garden. And each of the gardens must be identical; that is to say that each of the _k_ chosen species should have an equal number of flowers in each garden. As Farmer John is a proponent of national equality, he would like to create the greatest number of gardens possible. After receiving flowers on each of these _q_ days, Farmer John would like to know the sum, over all possible choices of _k_ species, of the maximum number of gardens he could create. Since this could be a large number, you should output your result modulo 109u2009+u20097. Input The first line of the input contains three integers _n_, _k_ and _q_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_q_u2009≤u2009100u2009000). The _i_-th (1u2009≤u2009_i_u2009≤u2009_n_) of the next _n_ lines of the input contains an integer _a__i_ (1u2009≤u2009_a__i_u2009≤u20091u2009000u2009000), the number of flowers of species _i_ Farmer John has initially. The _j_-th (1u2009≤u2009_j_u2009≤u2009_q_) of the next _q_ lines of the input contains an integer _c__j_ (1u2009≤u2009_c__j_u2009≤u20091u2009000u2009000), the number of flowers of a new species Farmer John receives on day _j_. Output After each of the _q_ days, output the sum of the maximum possible number of gardens, where the sum is taken over all possible choices of _k_ species, modulo 109u2009+u20097. Note In the first sample case, after the first day Farmer John has (4,u20096,u20099,u20098) of each type of flower, and _k_u2009=u20093. Choosing (4,u20096,u20098) lets him make 2 gardens, each with (2,u20093,u20094) of each flower, respectively. Choosing (4,u20096,u20099), (4,u20099,u20098) and (6,u20099,u20098) each only let him make one garden, since there is no number of gardens that each species can be evenly split into. So the sum over all choices of _k_u2009=u20093 flowers is 2u2009+u20091u2009+u20091u2009+u20091u2009=u20095. After the second day, Farmer John has (4,u20096,u20099,u20098,u20096) of each flower. The sum over all choices is 1u2009+u20092u2009+u20092u2009+u20091u2009+u20091u2009+u20092u2009+u20092u2009+u20093u2009+u20091u2009+u20091u2009=u200916. In the second sample case, _k_u2009=u20091. With _x_ flowers Farmer John can make _x_ gardens. So the answers to the queries are 6u2009+u20095u2009+u20094u2009+u20093u2009+u20092u2009=u200920 and 6u2009+u20095u2009+u20094u2009+u20093u2009+u20092u2009+u20091u2009=u200921.
2,500
true
false
false
false
false
false
false
false
false
false
7,229
183E
The prestigious Codeforces kindergarten consists of _n_ kids, numbered 1 through _n_. Each of them are given allowance in rubles by their parents. Today, they are going to the most famous candy shop in the town. The shop sells candies in packages: for all _i_ between 1 and _m_, inclusive, it sells a package containing exactly _i_ candies. A candy costs one ruble, so a package containing _x_ candies costs _x_ rubles. The kids will purchase candies in turns, starting from kid 1. In a single turn, kid _i_ will purchase one candy package. Due to the highly competitive nature of Codeforces kindergarten, during a turn, the number of candies contained in the package purchased by the kid will always be strictly greater than the number of candies contained in the package purchased by the kid in the preceding turn (an exception is in the first turn: the first kid may purchase any package). Then, the turn proceeds to kid _i_u2009+u20091, or to kid 1 if it was kid _n_'s turn. This process can be ended at any time, but at the end of the purchase process, all the kids must have the same number of candy packages. Of course, the amount spent by each kid on the candies cannot exceed their allowance. You work at the candy shop and would like to prepare the candies for the kids. Print the maximum number of candies that can be sold by the candy shop to the kids. If the kids cannot purchase any candy (due to insufficient allowance), print 0. Input The first line contains two space-separated integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20092·105,u20092u2009≤u2009_m_u2009≤u20095·106,u2009_n_u2009≤u2009_m_), denoting the number of kids and the maximum number of candies in a package sold by the candy shop, respectively. Then _n_ lines follow, each line will contain a single positive integer not exceeding denoting the allowance of a kid in rubles. The allowances are given in order from kid 1 to kid _n_. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is recommended to use cin, cout streams (also you may use %I64d specificator).
2,900
false
true
false
false
false
false
false
false
false
false
9,105
1833F
Ira loves Spanish flamenco dance very much. She decided to start her own dance studio and found $$$n$$$ students, $$$i$$$th of whom has level $$$a_i$$$. Ira can choose several of her students and set a dance with them. So she can set a huge number of dances, but she is only interested in magnificent dances. The dance is called magnificent if the following is true: exactly $$$m$$$ students participate in the dance; levels of all dancers are pairwise distinct; levels of every two dancers have an absolute difference strictly less than $$$m$$$. For example, if $$$m = 3$$$ and $$$a = [4, 2, 2, 3, 6]$$$, the following dances are magnificent (students participating in the dance are highlighted in red): $$$[color{red}{4}, 2, color{red}{2}, color{red}{3}, 6]$$$, $$$[color{red}{4}, color{red}{2}, 2, color{red}{3}, 6]$$$. At the same time dances $$$[color{red}{4}, 2, 2, color{red}{3}, 6]$$$, $$$[4, color{red}{2}, color{red}{2}, color{red}{3}, 6]$$$, $$$[color{red}{4}, 2, 2, color{red}{3}, color{red}{6}]$$$ are not magnificent. In the dance $$$[color{red}{4}, 2, 2, color{red}{3}, 6]$$$ only $$$2$$$ students participate, although $$$m = 3$$$. The dance $$$[4, color{red}{2}, color{red}{2}, color{red}{3}, 6]$$$ involves students with levels $$$2$$$ and $$$2$$$, although levels of all dancers must be pairwise distinct. In the dance $$$[color{red}{4}, 2, 2, color{red}{3}, color{red}{6}]$$$ students with levels $$$3$$$ and $$$6$$$ participate, but $$$3 - 6 = 3$$$, although $$$m = 3$$$. Help Ira count the number of magnificent dances that she can set. Since this number can be very large, count it modulo $$$10^9 + 7$$$. Two dances are considered different if the sets of students participating in them are different. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— number of testcases. The first line of each testcase contains integers $$$n$$$ and $$$m$$$ ($$$1 le m le n le 2 cdot 10^5$$$)xa0— the number of Ira students and the number of dancers in the magnificent dance. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— levels of students. It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. Note In the first testcase, Ira can set such magnificent dances: $$$[color{red}{8}, 10, 10, color{red}{9}, color{red}{6}, 11, color{red}{7}]$$$, $$$[color{red}{8}, color{red}{10}, 10, color{red}{9}, 6, 11, color{red}{7}]$$$, $$$[color{red}{8}, 10, color{red}{10}, color{red}{9}, 6, 11, color{red}{7}]$$$, $$$[color{red}{8}, 10, color{red}{10}, color{red}{9}, 6, color{red}{11}, 7]$$$, $$$[color{red}{8}, color{red}{10}, 10, color{red}{9}, 6, color{red}{11}, 7]$$$. The second testcase is explained in the statements.
1,700
true
false
true
false
true
true
false
false
true
false
1,286
1313A
Tired of boring office work, Denis decided to open a fast food restaurant. On the first day he made $$$a$$$ portions of dumplings, $$$b$$$ portions of cranberry juice and $$$c$$$ pancakes with condensed milk. The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules: every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes); each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk; all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed? Input The first line contains an integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases to solve. Each of the remaining $$$t$$$ lines contains integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$0 leq a, b, c leq 10$$$)xa0— the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made. Output For each test case print a single integerxa0— the maximum number of visitors Denis can feed. Example Input 7 1 2 1 0 0 0 9 1 7 2 2 3 2 3 2 3 2 2 4 4 4 Note In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk. In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers. In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
900
false
true
true
false
false
false
true
false
false
false
4,130
115C
Little John aspires to become a plumber! Today he has drawn a grid consisting of _n_ rows and _m_ columns, consisting of _n_u2009×u2009_m_ square cells. In each cell he will draw a pipe segment. He can only draw four types of segments numbered from 1 to 4, illustrated as follows: Each pipe segment has two ends, illustrated by the arrows in the picture above. For example, segment 1 has ends at top and left side of it. Little John considers the piping system to be leaking if there is at least one pipe segment inside the grid whose end is not connected to another pipe's end or to the border of the grid. The image below shows an example of leaking and non-leaking systems of size 1u2009×u20092. Now, you will be given the grid that has been partially filled by Little John. Each cell will either contain one of the four segments above, or be empty. Find the number of possible different non-leaking final systems after Little John finishes filling all of the empty cells with pipe segments. Print this number modulo 1000003 (106u2009+u20093). Note that rotations or flipping of the grid are not allowed and so two configurations that are identical only when one of them has been rotated or flipped either horizontally or vertically are considered two different configurations. Input The first line will contain two single-space separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_,u2009_n_·_m_u2009≤u20095·105) — the number of rows and columns respectively. Then _n_ lines follow, each contains exactly _m_ characters — the description of the grid. Each character describes a cell and is either one of these: "1" - "4" — a pipe segment of one of four types as described above "." — an empty cell Output Print a single integer denoting the number of possible final non-leaking pipe systems modulo 1000003 (106u2009+u20093). If there are no such configurations, print 0. Note For the first example, the initial configuration of the grid is as follows. The only two possible final non-leaking pipe configurations are as follows: For the second example, the initial grid is already leaking, so there will be no final grid that is non-leaking. For the final example, there's only one possible non-leaking final grid as follows.
2,200
true
false
false
false
false
false
false
false
false
false
9,426
1267F
, # Foolpr¨ uf Security Input file: standard input Output file: standard output Time limit: 3 seconds Memory limit: 512 megabytes Alice and Bob obtained a map of the secret underground facility. The facility consists of n security units and m chemical labs , connected by bidirectional tunnels. The map of this facility forms a tree : there are exactly n + m − 1 tunnels, and there are no cycles. Vertices corresponding to security units have numbers from 1 to n, chemical labs have numbers from n + 1 to n + m. Each tunnel connects a security unit to a chemical lab; there are no tunnels between two security units or two chemical labs. In case Alice or Bob gets captured, they decided to split the map into two pieces. To do that, they calculated the Pr¨ ufer code of the tree. Alice then saved some of the numbers between 1 and n to her data storage in the same order as they go in the original code, and Bob saved some of the numbers from n + 1 to n + m to his storage in the same way. A Pr¨ ufer code of a tree on k vertices is a sequence of k − 2 integers from 1 to k, constructed as follows. Find the leaf (a vertex with degree one) with the smallest label, remove it from the tree, then print the label of its only neighbor. Repeat this k −3 more times, until only one edge remains. The printed sequence of k − 2 vertex labels is the Pr¨ ufer code. Alice and Bob safely returned and they are ready to combine their data to restore the original map. They could make a mistake during the backup, meaning no such map exists. Alice and Bob need your help to restore any possible map of the facility consistent with the collected data, so that both Alice’s and Bob’s parts are subsequences of the Pr¨ ufer code of the map. # Input The first line of the input contains four integers n, m, ka, and kb (2 ≤ n, m ≤ 10 5; 1 ≤ ka, k b; ka + kb ≤ n + m − 2). The second line contains ka integers a1, a 2, . . . , a ka (1 ≤ ai ≤ n) x16 Alice’s part of the map. The third line contains kb integers b1, b 2, . . . , b kb (n + 1 ≤ bi ≤ n + m) x16 Bob’s part of the map. # Output If there’s no such map, print “ No ”. Otherwise, print “ Yes ” on the first line, followed by n + m − 1 lines describing the possible facility map. Each line should contain two integers ui and vi x16 the security unit and the chemical lab connected by the i-th tunnel of the facility. # Examples standard input standard output Illustration 4 5 4 2 1 3 3 4 7 8 Yes 1 5 1 6 2 7 6 3 3 7 9 4 3 8 4 8 1 > 56 > 2 > 7 > 3 > 8 > 4 > 9 4 3 3 1 3 2 2 6No Page 1 of 2 , # Note The Pr¨ ufer code of the tree in the first example is (7 , 1, 6, 3, 3, 8, 4). Page 2 of 2
2,600
false
false
false
false
false
false
false
false
false
true
4,358
176C
Two players play a game. The game is played on a rectangular board with _n_u2009×u2009_m_ squares. At the beginning of the game two different squares of the board have two chips. The first player's goal is to shift the chips to the same square. The second player aims to stop the first one with a tube of superglue. We'll describe the rules of the game in more detail. The players move in turns. The first player begins. With every move the first player chooses one of his unglued chips, and shifts it one square to the left, to the right, up or down. It is not allowed to move a chip beyond the board edge. At the beginning of a turn some squares of the board may be covered with a glue. The first player can move the chip to such square, in this case the chip gets tightly glued and cannot move any longer. At each move the second player selects one of the free squares (which do not contain a chip or a glue) and covers it with superglue. The glue dries long and squares covered with it remain sticky up to the end of the game. If, after some move of the first player both chips are in the same square, then the first player wins. If the first player cannot make a move (both of his chips are glued), then the second player wins. Note that the situation where the second player cannot make a move is impossible — he can always spread the glue on the square from which the first player has just moved the chip. We will further clarify the case where both chips are glued and are in the same square. In this case the first player wins as the game ends as soon as both chips are in the same square, and the condition of the loss (the inability to move) does not arise. You know the board sizes and the positions of the two chips on it. At the beginning of the game all board squares are glue-free. Find out who wins if the players play optimally. Input The first line contains six integers _n_, _m_, _x_1, _y_1, _x_2, _y_2 — the board sizes and the coordinates of the first and second chips, correspondingly (1u2009≤u2009_n_,u2009_m_u2009≤u2009100; 2u2009≤u2009_n_u2009×u2009_m_; 1u2009≤u2009_x_1,u2009_x_2u2009≤u2009_n_; 1u2009≤u2009_y_1,u2009_y_2u2009≤u2009_m_). The numbers in the line are separated by single spaces. It is guaranteed that the chips are located in different squares. Output If the first player wins, print "First" without the quotes. Otherwise, print "Second" without the quotes.
2,000
false
false
false
false
false
true
false
false
false
false
9,157
386C
String diversity is the number of symbols that occur in the string at least once. Diversity of _s_ will be denoted by _d_(_s_). For example , _d_("aaa")=1, _d_("abacaba")=3. Given a string _s_, consisting of lowercase Latin letters. Consider all its substrings. Obviously, any substring diversity is a number from 1 to _d_(_s_). Find statistics about substrings diversity: for each _k_ from 1 to _d_(_s_), find how many substrings of _s_ has a diversity of exactly _k_. Input The input consists of a single line containing _s_. It contains only lowercase Latin letters, the length of _s_ is from 1 to 3·105. Output Print to the first line the value _d_(_s_). Print sequence _t_1,u2009_t_2,u2009...,u2009_t__d_(_s_) to the following lines, where _t__i_ is the number of substrings of _s_ having diversity of exactly _i_. Note Consider the first example. We denote by _s_(_i_,u2009_j_) a substring of "abca" with the indices in the segment [_i_,u2009_j_]. _s_(1,u20091)u2009=u2009 "a", _d_("a")u2009=u20091 _s_(2,u20092)u2009=u2009 "b", _d_("b")u2009=u20091 _s_(3,u20093)u2009=u2009 "c", _d_("c")u2009=u20091 _s_(4,u20094)u2009=u2009 "a", _d_("a")u2009=u20091 _s_(1,u20092)u2009=u2009 "ab", _d_("ab")u2009=u20092 _s_(2,u20093)u2009=u2009 "bc", _d_("bc")u2009=u20092 _s_(3,u20094)u2009=u2009 "ca", _d_("ca")u2009=u20092 _s_(1,u20093)u2009=u2009 "abc", _d_("abc")u2009=u20093 _s_(2,u20094)u2009=u2009 "bca", _d_("bca")u2009=u20093 _s_(1,u20094)u2009=u2009 "abca", _d_("abca")u2009=u20093 Total number of substring with diversity 1 is 4, with diversity 2 equals 3, 3 diversity is 3.
2,000
false
false
false
true
false
false
false
false
false
false
8,284
1827E
There is a country consisting of $$$n$$$ cities and $$$n - 1$$$ bidirectional roads connecting them such that we can travel between any two cities using these roads. In other words, these cities and roads form a tree. There are $$$m$$$ bus routes connecting the cities together. A bus route between city $$$x$$$ and city $$$y$$$ allows you to travel between any two cities in the simple path between $$$x$$$ and $$$y$$$ with this route. Determine if for every pair of cities $$$u$$$ and $$$v$$$, you can travel from $$$u$$$ to $$$v$$$ using at most two bus routes. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 5 cdot 10^5, 0 le m le 5 cdot 10^5$$$)xa0— the number of cities and the number of bus routes. Then $$$n - 1$$$ lines follow. Each line contains two integers $$$u$$$ and $$$v$$$ denoting a road connecting city $$$u$$$ and city $$$v$$$ ($$$1 le u, v le n, u eq v$$$). It is guaranteed that these cities and roads form a tree. Then $$$m$$$ lines follow. Each line contains two integers $$$x$$$ and $$$y$$$ denoting a bus route between city $$$x$$$ and city $$$y$$$ ($$$1 le x, y le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output "YES" if you can travel between any pair of cities using at most two bus routes. Otherwise, output "NO". In the next line, output two cities $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$) such that it is impossible to reach city $$$y$$$ from city $$$x$$$ using at most two bus routes. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 4 5 2 1 2 2 3 3 4 2 5 1 4 5 2 5 1 1 2 2 3 3 4 2 5 1 5 2 0 1 2 6 3 1 2 2 3 3 4 4 5 5 6 1 3 2 5 4 6 Output YES NO 1 3 NO 1 2 NO 1 6 Note Here are the graphs of test case $$$1$$$, $$$2$$$, and $$$4$$$: Sample 1 Sample 2 Sample 4
3,400
false
true
false
false
false
true
false
true
false
false
1,318
1635D
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers. Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: 1. $$$x = a_i$$$ for some $$$1 leq i leq n$$$. 2. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. 3. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$. For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$${1,2,3,4,5,7,8,9,11,12}$$$. Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$. Input The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 leq n, p leq 2 cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ $$$(1 leq a_i leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct. Output Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$. Examples Input 2 200000 48763 1000000000 Note In the first example, the elements smaller than $$$2^4$$$ are $$${1, 3, 4, 6, 7, 9, 12, 13, 15}$$$. In the second example, the elements smaller than $$$2^7$$$ are $$${5,11,20,23,39,41,44,47,79,80,83,89,92,95}$$$.
1,800
true
false
false
true
false
false
false
false
false
false
2,437
1547G
You are given a directed graph $$$G$$$ which can contain loops (edges from a vertex to itself). Multi-edges are absent in $$$G$$$ which means that for all ordered pairs $$$(u, v)$$$ exists at most one edge from $$$u$$$ to $$$v$$$. Vertices are numbered from $$$1$$$ to $$$n$$$. A path from $$$u$$$ to $$$v$$$ is a sequence of edges such that: vertex $$$u$$$ is the start of the first edge in the path; vertex $$$v$$$ is the end of the last edge in the path; for all pairs of adjacent edges next edge starts at the vertex that the previous edge ends on. We will assume that the empty sequence of edges is a path from $$$u$$$ to $$$u$$$. For each vertex $$$v$$$ output one of four values: $$$0$$$, if there are no paths from $$$1$$$ to $$$v$$$; $$$1$$$, if there is only one path from $$$1$$$ to $$$v$$$; $$$2$$$, if there is more than one path from $$$1$$$ to $$$v$$$ and the number of paths is finite; $$$-1$$$, if the number of paths from $$$1$$$ to $$$v$$$ is infinite. Let's look at the example shown in the figure. Then: the answer for vertex $$$1$$$ is $$$1$$$: there is only one path from $$$1$$$ to $$$1$$$ (path with length $$$0$$$); the answer for vertex $$$2$$$ is $$$0$$$: there are no paths from $$$1$$$ to $$$2$$$; the answer for vertex $$$3$$$ is $$$1$$$: there is only one path from $$$1$$$ to $$$3$$$ (it is the edge $$$(1, 3)$$$); the answer for vertex $$$4$$$ is $$$2$$$: there are more than one paths from $$$1$$$ to $$$4$$$ and the number of paths are finite (two paths: $$$[(1, 3), (3, 4)]$$$ and $$$[(1, 4)]$$$); the answer for vertex $$$5$$$ is $$$-1$$$: the number of paths from $$$1$$$ to $$$5$$$ is infinite (the loop can be used in a path many times); the answer for vertex $$$6$$$ is $$$-1$$$: the number of paths from $$$1$$$ to $$$6$$$ is infinite (the loop can be used in a path many times). Input The first contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases in the input. Then $$$t$$$ test cases follow. Before each test case, there is an empty line. The first line of the test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 4 cdot 10^5, 0 le m le 4 cdot 10^5$$$)xa0— numbers of vertices and edges in graph respectively. The next $$$m$$$ lines contain edges descriptions. Each line contains two integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i, b_i le n$$$)xa0— the start and the end of the $$$i$$$-th edge. The vertices of the graph are numbered from $$$1$$$ to $$$n$$$. The given graph can contain loops (it is possible that $$$a_i = b_i$$$), but cannot contain multi-edges (it is not possible that $$$a_i = a_j$$$ and $$$b_i = b_j$$$ for $$$i e j$$$). The sum of $$$n$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Similarly, the sum of $$$m$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Output Output $$$t$$$ lines. The $$$i$$$-th line should contain an answer for the $$$i$$$-th test case: a sequence of $$$n$$$ integers from $$$-1$$$ to $$$2$$$. Example Input 5 6 7 1 4 1 3 3 4 4 5 2 1 5 5 5 6 1 0 3 3 1 2 2 3 3 1 5 0 4 4 1 2 2 3 1 4 4 3 Output 1 0 1 2 -1 -1 1 -1 -1 -1 1 0 0 0 0 1 1 2 1
2,100
false
false
false
true
false
false
false
false
false
true
2,907
1996A
Problem - 1996A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search math ternary search *800 No tag edit access → Contest materials Editorial") xa0— the number of test cases. Each test case contains an integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^3$$$, $$$n$$$ is even). Output For each test case, output an integer, the minimum number of animals Farmer John can have on his farm. Example Input 3 2 6 8 Output 1 2 2
800
true
false
false
false
false
false
false
true
false
false
290