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
1133E
You are a coach at your local university. There are $$$n$$$ students under your supervision, the programming skill of the $$$i$$$-th student is $$$a_i$$$. You have to form $$$k$$$ teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than $$$k$$$ (and at least one) non-empty teams so that the total number of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than $$$5$$$. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter). It is possible that some students not be included in any team at all. Your task is to report the maximum possible total number of students in no more than $$$k$$$ (and at least one) non-empty balanced teams. If you are Python programmer, consider using PyPy instead of Python when you submit your code. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 5000$$$) β€” the number of students and the maximum number of teams, correspondingly. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is a programming skill of the $$$i$$$-th student. Output Print one integer β€” the maximum possible total number of students in no more than $$$k$$$ (and at least one) non-empty balanced teams.
1,800
false
false
false
true
false
false
false
false
true
false
5,050
1856B
You are given an array of positive integers $$$a$$$ of length $$$n$$$. Let's call an array of positive integers $$$b$$$ of length $$$n$$$ good if: 1. $$$a_i eq b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$, 2. $$$a_1 + a_2 +ldots + a_n = b_1 + b_2 + ldots + b_n$$$. Does a good array exist? Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β€” the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10 ^ 9$$$)xa0β€” the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output "YES" (without quotes) if there exists a good array, and "NO" (without quotes) otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs","yes", "Yes", and "YES" will be recognized as positive responses. Example Input 6 3 6 1 2 2 1 1 4 3 1 2 4 1 17 5 1 2 1 1 1 3 618343152 819343431 1000000000 Output YES NO YES NO NO YES Note In the first test case, a possible good array is $$$[3, 3, 3]$$$. Some examples of not good arrays are: $$$[8, 0, 1]$$$xa0β€” the array does not consist of only positive integers, $$$[5, 2, 4]$$$xa0β€” the array does not have the same sum as the given array, $$$[5, 2, 2]$$$xa0β€” the third element is equal to the third element of the given array. In the second test case, $$$[1, 1]$$$ is the only array of positive integers of length $$$2$$$ that has the sum of it's elements equal to $$$2$$$. Since $$$[1, 1]$$$ is not a good array, the answer is "NO".
900
true
false
true
false
false
false
false
false
false
false
1,149
891D
Sloth is bad, mkay? So we decided to prepare a problem to punish lazy guys. You are given a tree, you should count the number of ways to remove an edge from it and then add an edge to it such that the final graph is a tree and has a perfect matching. Two ways of this operation are considered different if their removed edges or their added edges aren't the same. The removed edge and the added edge can be equal. A perfect matching is a subset of edges such that each vertex is an endpoint of exactly one of these edges. Input The first line contains _n_ (2u2009≀u2009_n_u2009≀u20095Β·105)xa0β€” the number of vertices. Each of the next _n_u2009-u20091 lines contains two integers _a_ and _b_ (1u2009≀u2009_a_,u2009_b_u2009≀u2009_n_)xa0β€” the endpoints of one edge. It's guaranteed that the graph is a tree. Output Output a single integerxa0β€” the answer to the problem. Examples Input 8 1 2 2 3 3 4 1 5 5 6 6 7 1 8 Note In first sample, there are 8 ways: edge between 2 and 3 turns to edge between 1 and 3, edge between 2 and 3 turns to edge between 1 and 4, edge between 2 and 3 turns to edge between 2 and 3, edge between 2 and 3 turns to edge between 2 and 4, edge between 1 and 2 turns to edge between 1 and 2, edge between 1 and 2 turns to edge between 1 and 4, edge between 3 and 4 turns to edge between 1 and 4, edge between 3 and 4 turns to edge between 3 and 4.
3,100
false
false
false
true
false
false
false
false
false
false
6,185
977D
Polycarp likes to play with numbers. He takes some integer number $$$x$$$, writes it down on the board, and then performs with it $$$n - 1$$$ operations of the two kinds: divide the number $$$x$$$ by $$$3$$$ ($$$x$$$ must be divisible by $$$3$$$); multiply the number $$$x$$$ by $$$2$$$. After each operation, Polycarp writes down the result on the board and replaces $$$x$$$ by the result. So there will be $$$n$$$ numbers on the board after all. You are given a sequence of length $$$n$$$ β€” the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board. Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp's game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number. It is guaranteed that the answer exists. Input The first line of the input contatins an integer number $$$n$$$ ($$$2 le n le 100$$$) β€” the number of the elements in the sequence. The second line of the input contains $$$n$$$ integer numbers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 3 cdot 10^{18}$$$) β€” rearranged (reordered) sequence that Polycarp can wrote down on the board. Output Print $$$n$$$ integer numbers β€” rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board. It is guaranteed that the answer exists. Examples Input 2 1000000000000000000 3000000000000000000 Output 3000000000000000000 1000000000000000000 Note In the first example the given sequence can be rearranged in the following way: $$$[9, 3, 6, 12, 4, 8]$$$. It can match possible Polycarp's game which started with $$$x = 9$$$.
1,400
true
false
false
false
false
false
false
false
true
false
5,821
2020E
You are given an array of $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$. You are also given an array $$$p_1, p_2, ldots, p_n$$$. Let $$$S$$$ denote the random multiset (i.xa0e., it may contain equal elements) constructed as follows: Initially, $$$S$$$ is empty. For each $$$i$$$ from $$$1$$$ to $$$n$$$, insert $$$a_i$$$ into $$$S$$$ with probability $$$frac{p_i}{10^4}$$$. Note that each element is inserted independently. Denote $$$f(S)$$$ as the )^2$$$. Output the answer modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer 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}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le 1023$$$). The third line of each test case contains $$$n$$$ integers $$$p_1,p_2,ldots,p_n$$$ ($$$1 le p_i le 10^4$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the expected value of $$$(f(S))^2$$$, modulo $$$10^9 + 7$$$. Example Input 4 2 1 2 5000 5000 2 1 1 1000 2000 6 343 624 675 451 902 820 6536 5326 7648 2165 9430 5428 1 1 10000 Output 500000007 820000006 280120536 1 Note In the first test case, $$$a = [1, 2]$$$ and each element is inserted into $$$S$$$ with probability $$$frac{1}{2}$$$, since $$$p_1 = p_2 = 5000$$$ and $$$frac{p_i}{10^4} = frac{1}{2}$$$. Thus, there are $$$4$$$ outcomes for $$$S$$$, each happening with the same probability of $$$frac{1}{4}$$$: $$$S = varnothing$$$. In this case, $$$f(S) = 0$$$, $$$(f(S))^2 = 0$$$. $$$S = {1}$$$. In this case, $$$f(S) = 1$$$, $$$(f(S))^2 = 1$$$. $$$S = {2}$$$. In this case, $$$f(S) = 2$$$, $$$(f(S))^2 = 4$$$. $$$S = {1,2}$$$. In this case, $$$f(S) = 1 oplus 2 = 3$$$, $$$(f(S))^2 = 9$$$. Hence, the answer is $$$0 cdot frac{1}{4} + 1 cdot frac{1}{4} + 4cdot frac{1}{4} + 9 cdot frac{1}{4} = frac{14}{4} = frac{7}{2} equiv 500,000,007 pmod{10^9 + 7}$$$. In the second test case, $$$a = [1, 1]$$$, $$$a_1$$$ is inserted into $$$S$$$ with probability $$$0.1$$$, while $$$a_2$$$ is inserted into $$$S$$$ with probability $$$0.2$$$. There are $$$3$$$ outcomes for $$$S$$$: $$$S = varnothing$$$. In this case, $$$f(S) = 0$$$, $$$(f(S))^2 = 0$$$. This happens with probability $$$(1-0.1) cdot (1-0.2) = 0.72$$$. $$$S = {1}$$$. In this case, $$$f(S) = 1$$$, $$$(f(S))^2 = 1$$$. This happens with probability $$$(1-0.1) cdot 0.2 + 0.1 cdot (1-0.2) = 0.26$$$. $$$S = {1, 1}$$$. In this case, $$$f(S) = 0$$$, $$$(f(S))^2 = 0$$$. This happens with probability $$$0.1 cdot 0.2 = 0.02$$$. Hence, the answer is $$$0 cdot 0.72 + 1 cdot 0.26 + 0 cdot 0.02 = 0.26 = frac{26}{100} equiv 820,000,006 pmod{10^9 + 7}$$$.
2,000
true
false
false
true
false
false
false
false
false
false
148
627F
A remote island chain contains _n_ islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of _n_u2009-u20091 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal. The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal. It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position. Input The first line contains a single integer _n_ (2u2009≀u2009_n_u2009≀u2009200u2009000)xa0β€” the total number of islands. The second line contains _n_ space-separated integers _a__i_ (0u2009≀u2009_a__i_u2009≀u2009_n_u2009-u20091)xa0β€” the statue currently located on the _i_-th island. If _a__i_u2009=u20090, then the island has no statue. It is guaranteed that the _a__i_ are distinct. The third line contains _n_ space-separated integers _b__i_ (0u2009≀u2009_b__i_u2009≀u2009_n_u2009-u20091)xa0β€” the desired statues of the _i_-th island. Once again, _b__i_u2009=u20090 indicates the island desires no statue. It is guaranteed that the _b__i_ are distinct. The next _n_u2009-u20091 lines each contain two distinct space-separated integers _u__i_ and _v__i_ (1u2009≀u2009_u__i_,u2009_v__i_u2009≀u2009_n_)xa0β€” the endpoints of the _i_-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input. Output Print a single line of integers: If the rearrangement can be done in the existing network, output 0 _t_, where _t_ is the number of moves necessary to perform the rearrangement. Otherwise, print _u_, _v_, and _t_ (1u2009≀u2009_u_u2009<u2009_v_u2009≀u2009_n_)xa0β€” the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement. If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing u2009-u20091. Examples Input 4 0 1 2 3 0 2 3 1 1 2 1 3 1 4 Note In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves. In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made. In the third sample, no added bridge and subsequent movements result in the desired position.
3,400
false
false
false
false
false
false
false
false
false
true
7,316
1807F
You are given a room that can be represented by a $$$n imes m$$$ grid. There is a ball at position $$$(i_1, j_1)$$$ (the intersection of row $$$i_1$$$ and column $$$j_1$$$), and it starts going diagonally in one of the four directions: The ball is going down and right, denoted by $$$ exttt{DR}$$$; it means that after a step, the ball's location goes from $$$(i, j)$$$ to $$$(i+1, j+1)$$$. The ball is going down and left, denoted by $$$ exttt{DL}$$$; it means that after a step, the ball's location goes from $$$(i, j)$$$ to $$$(i+1, j-1)$$$. The ball is going up and right, denoted by $$$ exttt{UR}$$$; it means that after a step, the ball's location goes from $$$(i, j)$$$ to $$$(i-1, j+1)$$$. The ball is going up and left, denoted by $$$ exttt{UL}$$$; it means that after a step, the ball's location goes from $$$(i, j)$$$ to $$$(i-1, j-1)$$$. After each step, the ball maintains its direction unless it hits a wall (that is, the direction takes it out of the room's bounds in the next step). In this case, the ball's direction gets flipped along the axis of the wall; if the ball hits a corner, both directions get flipped. Any instance of this is called a bounce. The ball never stops moving. In the above example, the ball starts at $$$(1, 7)$$$ and goes $$$ exttt{DL}$$$ until it reaches the bottom wall, then it bounces and continues in the direction $$$ exttt{UL}$$$. After reaching the left wall, the ball bounces and continues to go in the direction $$$ exttt{UR}$$$. When the ball reaches the upper wall, it bounces and continues in the direction $$$ exttt{DR}$$$. After reaching the bottom-right corner, it bounces once and continues in direction $$$ exttt{UL}$$$, and so on. Your task is to find how many bounces the ball will go through until it reaches cell $$$(i_2, j_2)$$$ in the room, or report that it never reaches cell $$$(i_2, j_2)$$$ by printing $$$-1$$$. Note that the ball first goes in a cell and only after that bounces if it needs to. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0β€” the number of test cases. The first line of each test case contains six integers and a string $$$n, m, i_1, j_1, i_2, j_2, d$$$ ($$$2 leq n, m leq 25000$$$; $$$1 leq i_1, i_2 leq n$$$; $$$1 leq j_1, j_2 leq m$$$; $$$d in{ exttt{DR}, exttt{DL}, exttt{UR}, exttt{UL}}$$$)xa0β€” the dimensions of the grid, the starting coordinates of the ball, the coordinates of the final cell and the starting direction of the ball. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$5 cdot 10^4$$$. Output For each test case, output a single integerxa0β€” the number of bounces the ball does until it reaches cell $$$(i_2, j_2)$$$ for the first time, or $$$-1$$$ if the ball never reaches the final cell. Example Input 6 5 7 1 7 2 4 DL 5 7 1 7 3 2 DL 3 3 1 3 2 2 UR 2 4 2 1 2 2 DR 4 3 1 1 1 3 UL 6 4 1 2 3 4 DR
1,700
false
false
true
false
false
false
true
false
false
false
1,430
630D
Problem - 630D - Codeforces =============== xa0 ]( "Experimental Educational Round: VolBIT Formulas Blitz") . Output Output one integer β€” the number of hexagons situated not farther than _n_ cells away from a given cell. Examples Input 2 Output 19
1,100
true
false
false
false
false
false
false
false
false
false
7,301
198A
Qwerty the Ranger took up a government job and arrived on planet Mars. He should stay in the secret lab and conduct some experiments on bacteria that have funny and abnormal properties. The job isn't difficult, but the salary is high. At the beginning of the first experiment there is a single bacterium in the test tube. Every second each bacterium in the test tube divides itself into _k_ bacteria. After that some abnormal effects create _b_ more bacteria in the test tube. Thus, if at the beginning of some second the test tube had _x_ bacteria, then at the end of the second it will have _kx_u2009+u2009_b_ bacteria. The experiment showed that after _n_ seconds there were exactly _z_ bacteria and the experiment ended at this point. For the second experiment Qwerty is going to sterilize the test tube and put there _t_ bacteria. He hasn't started the experiment yet but he already wonders, how many seconds he will need to grow at least _z_ bacteria. The ranger thinks that the bacteria will divide by the same rule as in the first experiment. Help Qwerty and find the minimum number of seconds needed to get a tube with at least _z_ bacteria in the second experiment. Input The first line contains four space-separated integers _k_, _b_, _n_ and _t_ (1u2009≀u2009_k_,u2009_b_,u2009_n_,u2009_t_u2009≀u2009106) β€” the parameters of bacterial growth, the time Qwerty needed to grow _z_ bacteria in the first experiment and the initial number of bacteria in the second experiment, correspondingly. Output Print a single number β€” the minimum number of seconds Qwerty needs to grow at least _z_ bacteria in the tube.
1,700
true
false
true
false
false
false
false
false
false
false
9,051
1678B2
This is the hard version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even. Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even. For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1' ($$$1 leq i leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good? Meanwhile, she also wants to know the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. Input The first contains a single positive integer $$$t$$$ ($$$1 leq t leq 10,000$$$)xa0β€” the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0β€” the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single line with two integersxa0β€” the minimum number of operations to make $$$s$$$ good, and the minimum number of subsegments that $$$s$$$ can be divided into among all solutions with the minimum number of operations. Example Input 5 10 1110011000 8 11001111 2 00 2 11 6 100110 Output 3 2 0 3 0 1 0 1 3 1 Note In the first test case, one of the ways to make $$$s$$$ good is the following. Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively, the number of subsegments of it is $$$2$$$. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100", the number of subsegments of them are $$$2$$$, $$$4$$$, $$$4$$$ respectively. It's easy to find that the minimum number of subsegments among all solutions with the minimum number of operations is $$$2$$$. In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required.
1,800
false
true
true
true
false
false
false
false
false
false
2,198
402E
Problem - 402E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags graphs math *2200 No tag edit access β†’ Contest materials the following inequality holds: _a__ij_u2009β‰₯u20090; . Matrix _b_ is strictly positive, if for any numbers _i_,u2009_j_ (1u2009≀u2009_i_,u2009_j_u2009≀u2009_n_) the inequality _b__ij_u2009>u20090 holds. You task is to determine if there is such integer _k_u2009β‰₯u20091, that matrix _a__k_ is strictly positive. Input The first line contains integer _n_ (2u2009≀u2009_n_u2009≀u20092000) β€” the number of rows and columns in matrix _a_. The next _n_ lines contain the description of the rows of matrix _a_. The _i_-th line contains _n_ non-negative integers _a__i_1,u2009_a__i_2,u2009...,u2009_a__in_ (0u2009≀u2009_a__ij_u2009≀u200950). It is guaranteed that . Output If there is a positive integer _k_u2009β‰₯u20091, such that matrix _a__k_ is strictly positive, print "YES" (without the quotes). Otherwise, print "NO" (without the quotes). Examples Input 2 1 0 0 1 Output NO Input 5 4 5 6 1 2 1 2 3 4 5 6 4 1 2 4 1 1 1 1 1 4 4 4 4 4 Output YES
2,200
true
false
false
false
false
false
false
false
false
true
8,213
931B
The last stage of Football World Cup is played using the play-off system. There are _n_ teams left in this stage, they are enumerated from 1 to _n_. Several rounds are held, in each round the remaining teams are sorted in the order of their ids, then the first in this order plays with the second, the thirdxa0β€” with the fourth, the fifthxa0β€” with the sixth, and so on. It is guaranteed that in each round there is even number of teams. The winner of each game advances to the next round, the loser is eliminated from the tournament, there are no draws. In the last round there is the only game with two remaining teams: the round is called the Final, the winner is called the champion, and the tournament is over. Arkady wants his two favorite teams to play in the Final. Unfortunately, the team ids are already determined, and it may happen that it is impossible for teams to meet in the Final, because they are to meet in some earlier stage, if they are strong enough. Determine, in which round the teams with ids _a_ and _b_ can meet. Input The only line contains three integers _n_, _a_ and _b_ (2u2009≀u2009_n_u2009≀u2009256, 1u2009≀u2009_a_,u2009_b_u2009≀u2009_n_)xa0β€” the total number of teams, and the ids of the teams that Arkady is interested in. It is guaranteed that _n_ is such that in each round an even number of team advance, and that _a_ and _b_ are not equal. Output In the only line print "Final!" (without quotes), if teams _a_ and _b_ can meet in the Final. Otherwise, print a single integerxa0β€” the number of the round in which teams _a_ and _b_ can meet. The round are enumerated from 1. Note In the first example teams 1 and 2 meet in the first round. In the second example teams 2 and 6 can only meet in the third round, which is the Final, if they win all their opponents in earlier rounds. In the third example the teams with ids 7 and 5 can meet in the second round, if they win their opponents in the first round.
1,200
false
false
true
false
false
true
false
false
false
false
5,980
83D
One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in the middle of a lesson. And only a loud ringing of a school bell could interrupt his sweet dream. Of course, the valuable material and the teacher's explanations were lost. However, Valera will one way or another have to do the homework. As he does not know the new material absolutely, he cannot do the job himself. That's why he asked you to help. You're his best friend after all, you just cannot refuse to help. Valera's home task has only one problem, which, though formulated in a very simple way, has not a trivial solution. Its statement looks as follows: if we consider all positive integers in the interval [_a_;_b_] then it is required to count the amount of such numbers in this interval that their smallest divisor will be a certain integer _k_ (you do not have to consider divisor equal to one). In other words, you should count the amount of such numbers from the interval [_a_;_b_], that are not divisible by any number between 2 and _k_u2009-u20091 and yet are divisible by _k_. Input The first and only line contains three positive integers _a_, _b_, _k_ (1u2009≀u2009_a_u2009≀u2009_b_u2009≀u20092Β·109,u20092u2009≀u2009_k_u2009≀u20092Β·109). Output Print on a single line the answer to the given problem. Note Comments to the samples from the statement: In the first sample the answer is numbers 2,u20094,u20096,u20098,u200910. In the second one β€” 15,u200921 In the third one there are no such numbers.
2,400
true
false
false
true
false
false
false
false
false
false
9,550
919C
Suppose that you are in a campus and have to go for classes day by day. As you may see, when you hurry to a classroom, you surprisingly find that many seats there are already occupied. Today you and your friends went for class, and found out that some of the seats were occupied. The classroom contains $$$n$$$ rows of seats and there are $$$m$$$ seats in each row. Then the classroom can be represented as an $$$n imes m$$$ matrix. The character '.' represents an empty seat, while '*' means that the seat is occupied. You need to find $$$k$$$ consecutive empty seats in the same row or column and arrange those seats for you and your friends. Your task is to find the number of ways to arrange the seats. Two ways are considered different if sets of places that students occupy differs. Input The first line contains three positive integers $$$n,m,k$$$ ($$$1 leq n, m, k leq 2,000$$$), where $$$n,m$$$ represent the sizes of the classroom and $$$k$$$ is the number of consecutive seats you need to find. Each of the next $$$n$$$ lines contains $$$m$$$ characters '.' or '*'. They form a matrix representing the classroom, '.' denotes an empty seat, and '*' denotes an occupied seat. Output A single number, denoting the number of ways to find $$$k$$$ empty seats in the same row or column. Note In the first sample, there are three ways to arrange those seats. You can take the following seats for your arrangement. $$$(1,3)$$$, $$$(2,3)$$$ $$$(2,2)$$$, $$$(2,3)$$$ $$$(2,1)$$$, $$$(2,2)$$$
1,300
false
false
true
false
false
false
true
false
false
false
6,054
833D
Slastyona likes to watch life of nearby grove's dwellers. This time she watches a strange red-black spider sitting at the center of a huge cobweb. The cobweb is a set of _n_ nodes connected by threads, each of the treads is either red of black. Using these threads, the spider can move between nodes. No thread connects a node to itself, and between any two nodes there is a unique sequence of threads connecting them. Slastyona decided to study some special qualities of the cobweb. She noticed that each of the threads has a value of clamminess _x_. However, Slastyona is mostly interested in jelliness of the cobweb. Consider those of the shortest paths between each pair of nodes on which the numbers of red and black threads differ at most twice. For each such path compute the product of the clamminess of threads on the path.The jelliness of the cobweb is the product of all obtained values among all paths. Those paths that differ by direction only are counted only once. Of course, this number can be huge, so Slastyona asks you to compute the jelliness of the given cobweb and print the answer modulo 109u2009+u20097. Input The first line contains the number of nodes _n_ (2u2009≀u2009_n_u2009≀u2009105). The next _n_u2009-u20091 lines contain four integers each, denoting the _i_-th thread of the cobweb: the nodes it connects _u__i_,u2009_v__i_ (1u2009≀u2009_u__i_u2009≀u2009_n_,u20091u2009≀u2009_v__i_u2009≀u2009_n_), the clamminess of the thread _x__i_ (1u2009≀u2009_x_u2009≀u2009109u2009+u20096), and the color of the thread _c__i_ (). The red color is denoted by 0, and the black color is denoted by 1. Output Print single integer the jelliness of the cobweb modulo 109u2009+u20097. If there are no paths such that the numbers of red and black threads differ at most twice, print 1. Examples Input 5 1 2 9 0 2 3 5 1 2 4 5 0 2 5 5 1 Input 8 1 2 7 1 2 3 4 1 3 4 19 1 5 1 2 0 6 2 3 0 7 3 3 0 8 4 4 0 Note In the first example there are 4 pairs of nodes such that the numbers of threads of both colors on them differ at most twice. There pairs are (1,u20093) with product of clamminess equal to 45, (1,u20095) with product of clamminess equal to 45, (3,u20094) with product of clamminess equal to 25 and (4,u20095) with product of clamminess equal to 25. The jelliness of the cobweb is equal to 1265625.
2,800
false
false
true
false
true
false
false
false
false
false
6,431
777E
Of course you have heard the famous task about Hanoi Towers, but did you know that there is a special factory producing the rings for this wonderful game? Once upon a time, the ruler of the ancient Egypt ordered the workers of Hanoi Factory to create as high tower as possible. They were not ready to serve such a strange order so they had to create this new tower using already produced rings. There are _n_ rings in factory's stock. The _i_-th ring has inner radius _a__i_, outer radius _b__i_ and height _h__i_. The goal is to select some subset of rings and arrange them such that the following conditions are satisfied: Outer radiuses form a non-increasing sequence, i.e. one can put the _j_-th ring on the _i_-th ring only if _b__j_u2009≀u2009_b__i_. Rings should not fall one into the the other. That means one can place ring _j_ on the ring _i_ only if _b__j_u2009>u2009_a__i_. The total height of all rings used should be maximum possible. Input The first line of the input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009100u2009000)xa0β€” the number of rings in factory's stock. The _i_-th of the next _n_ lines contains three integers _a__i_, _b__i_ and _h__i_ (1u2009≀u2009_a__i_,u2009_b__i_,u2009_h__i_u2009≀u2009109, _b__i_u2009>u2009_a__i_)xa0β€” inner radius, outer radius and the height of the _i_-th ring respectively. Output Print one integerxa0β€” the maximum height of the tower that can be obtained. Examples Input 4 1 2 1 1 3 3 4 6 2 5 7 1 Note In the first sample, the optimal solution is to take all the rings and put them on each other in order 3, 2, 1. In the second sample, one can put the ring 3 on the ring 4 and get the tower of height 3, or put the ring 1 on the ring 2 and get the tower of height 4.
2,000
false
true
false
true
true
false
true
false
true
false
6,657
1216F
You work as a system administrator in a dormitory, which has $$$n$$$ rooms one after another along a straight hallway. Rooms are numbered from $$$1$$$ to $$$n$$$. You have to connect all $$$n$$$ rooms to the Internet. You can connect each room to the Internet directly, the cost of such connection for the $$$i$$$-th room is $$$i$$$ coins. Some rooms also have a spot for a router. The cost of placing a router in the $$$i$$$-th room is also $$$i$$$ coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room $$$i$$$, you connect all rooms with the numbers from $$$max(1,~i - k)$$$ to $$$min(n,~i + k)$$$ inclusive to the Internet, where $$$k$$$ is the range of router. The value of $$$k$$$ is the same for all routers. Calculate the minimum total cost of connecting all $$$n$$$ rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 2 cdot 10^5$$$) β€” the number of rooms and the range of each router. The second line of the input contains one string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. If the $$$i$$$-th character of the string equals to '1' then there is a spot for a router in the $$$i$$$-th room. If the $$$i$$$-th character of the string equals to '0' then you cannot place a router in the $$$i$$$-th room. Output Print one integer β€” the minimum total cost of connecting all $$$n$$$ rooms to the Internet. Note In the first example it is enough to place the router in the room $$$3$$$, then all rooms will be connected to the Internet. The total cost of connection is $$$3$$$. In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is $$$1 + 2 + 3 + 4 + 5 + 6 = 21$$$. In the third example you need to connect the room $$$1$$$ directly and place the router in the room $$$3$$$. Thus, the total cost of connection of all rooms is $$$1 + 3 = 4$$$. In the fourth example you need to place routers in rooms $$$5$$$ and $$$10$$$. Then all rooms will be connected to the Internet. The total cost of connection is $$$5 + 10 = 15$$$.
2,100
false
true
false
true
true
false
false
false
false
false
4,591
1101E
Polycarp has recently got himself a new job. He now earns so much that his old wallet can't even store all the money he has. Berland bills somehow come in lots of different sizes. However, all of them are shaped as rectangles (possibly squares). All wallets are also produced in form of rectangles (possibly squares). A bill $$$x imes y$$$ fits into some wallet $$$h imes w$$$ if either $$$x le h$$$ and $$$y le w$$$ or $$$y le h$$$ and $$$x le w$$$. Bills can overlap with each other in a wallet and an infinite amount of bills can fit into a wallet. That implies that all the bills Polycarp currently have fit into a wallet if every single one of them fits into it independently of the others. Now you are asked to perform the queries of two types: 1. $$$+~x~y$$$ β€” Polycarp earns a bill of size $$$x imes y$$$; 2. $$$?~h~w$$$ β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h imes w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data. For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^5$$$) β€” the number of queries. Each of the next $$$n$$$ lines contains a query of one of these two types: 1. $$$+~x~y$$$ ($$$1 le x, y le 10^9$$$) β€” Polycarp earns a bill of size $$$x imes y$$$; 2. $$$?~h~w$$$ ($$$1 le h, w le 10^9$$$) β€” Polycarp wants to check if all the bills he has earned to this moment fit into a wallet of size $$$h imes w$$$. It is guaranteed that there is at least one query of type $$$1$$$ before the first query of type $$$2$$$ and that there is at least one query of type $$$2$$$ in the input data. Output For each query of type $$$2$$$ print "YES" if all the bills he has earned to this moment fit into a wallet of given size. Print "NO" otherwise. Example Input 9 + 3 2 + 2 3 ? 1 20 ? 3 3 ? 2 3 + 1 5 ? 10 10 ? 1 5 + 1 1 Note The queries of type $$$2$$$ of the example: 1. Neither bill fits; 2. Both bills fit (just checking that you got that bills can overlap); 3. Both bills fit (both bills are actually the same); 4. All bills fit (too much of free space in a wallet is not a problem); 5. Only bill $$$1 imes 5$$$ fit (all the others don't, thus it's "NO").
1,500
false
false
true
false
false
false
false
false
false
false
5,196
913A
Problem - 913A - Codeforces =============== xa0 , and denotes the remainder of division of _x_ by _y_. You are asked to solve the "reverse" problem. Given integers _n_ and _m_, calculate . Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009108). The second line contains a single integer _m_ (1u2009≀u2009_m_u2009≀u2009108). Output Output a single integerxa0β€” the value of . Examples Input 4 42 Output 10 Input 1 58 Output 0 Input 98765432 23456789 Output 23456789 Note In the first example, the remainder of division of 42 by 24u2009=u200916 is equal to 10. In the second example, 58 is divisible by 21u2009=u20092 without remainder, and the answer is 0.
900
true
false
true
false
false
false
false
false
false
false
6,091
1866L
After working at a factory (Lihmuf Sorting), Lihmuf wants to play a game with his good friend, Pak Chanek. There are $$$N$$$ boxes numbered from $$$1$$$ to $$$N$$$. The $$$i$$$-th box contains $$$i$$$ balls. Pak Chanek and Lihmuf will play a game using those boxes. There will be $$$N$$$ turns numbered from $$$1$$$ to $$$N$$$. On each turn, Pak Chanek chooses one box and takes all balls from that box, then Lihmuf does the same thing for one box he chooses (it is possible that the two chosen boxes are the same). Given an integer $$$M$$$. Before the game begins, Lihmuf must choose an integer $$$K$$$ satisfying $$$1 leq K leq M$$$. It is known that on the $$$j$$$-th turn, Pak Chanek will choose the $$$j$$$-th box, while Lihmuf will choose the $$$y$$$-th box, with $$$y=((j imes K - 1) bmod N) + 1$$$. Help Lihmuf choose the correct value of $$$K$$$ so that he will get as many balls as possible! If there are more than one possible values of $$$K$$$ that result in the maximum total balls, find the minimum such value of $$$K$$$. Keep in mind that each box can be chosen more than once, but after a box is chosen for the first time, the box becomes empty. Input The only line contains two integers $$$N$$$ and $$$M$$$ ($$$1 leq N leq 10^9$$$; $$$1 leq M leq 2000$$$) β€” the number of boxes and the maximum limit for the value of $$$K$$$. Output An integer representing the value of $$$K$$$ that will make Lihmuf get as many balls as possible. If there are more than one possible value of $$$K$$$ that result in the maximum total balls, find the minimum such value of $$$K$$$. Note In the first example, if Lihmuf chooses $$$K=1$$$, the game will go as follows: 1. Pak Chanek chooses the $$$1$$$-st box and gets $$$1$$$ ball, then Lihmuf chooses the $$$1$$$-st box and gets $$$0$$$ balls. 2. Pak Chanek chooses the $$$2$$$-nd box and gets $$$2$$$ balls, then Lihmuf chooses the $$$2$$$-nd box and gets $$$0$$$ balls. 3. Pak Chanek chooses the $$$3$$$-rd box and gets $$$3$$$ balls, then Lihmuf chooses the $$$3$$$-rd box and gets $$$0$$$ balls. In total, Lihmuf gets $$$0+0+0=0$$$ balls. The maximum total balls that can be earned by Lihmuf is $$$0$$$ because he can only choose $$$K=1$$$. Therefore, $$$K=1$$$ is the minimum possible value of $$$K$$$ that results in the maximum total balls. In the second example, if Lihmuf chooses $$$K=3$$$, the game will go as follows: 1. Pak Chanek chooses the $$$1$$$-st box and gets $$$1$$$ ball, then Lihmuf chooses the $$$3$$$-rd box and gets $$$3$$$ balls. 2. Pak Chanek chooses the $$$2$$$-nd box and gets $$$2$$$ balls, then Lihmuf chooses the $$$1$$$-st box and gets $$$0$$$ balls. 3. Pak Chanek chooses the $$$3$$$-rd box and gets $$$0$$$ balls, then Lihmuf chooses the $$$4$$$-th box and gets $$$4$$$ balls. 4. Pak Chanek chooses the $$$4$$$-th box and gets $$$0$$$ balls, then Lihmuf chooses the $$$2$$$-nd box and gets $$$0$$$ balls. 5. Pak Chanek chooses the $$$5$$$-th box and gets $$$5$$$ balls, then Lihmuf chooses the $$$5$$$-th box and gets $$$0$$$ balls. In total, Lihmuf gets $$$3+0+4+0+0=7$$$ balls. It can be obtained that $$$7$$$ is the maximum total balls that can be earned by Lihmuf. The possible values of $$$K$$$ that result in $$$7$$$ total balls are $$$3$$$ and $$$4$$$. Therefore, $$$K=3$$$ is the minimum possible value of $$$K$$$ that results in the maximum total balls.
2,400
true
false
false
false
false
false
true
true
false
false
1,077
761F
Dasha decided to have a rest after solving the problem _D_ and began to look photos from previous competitions. Let's call photos as the matrix with the size _n_u2009Γ—u2009_m_, which consists of lowercase English letters. Some _k_ photos especially interested her, because they can be received from photo-template by painting a rectangular area in a certain color. Let's call such photos special. More formally the _i_-th special photo is received from the photo-template by replacing all characters on some rectangle with upper left corner of the cell with coordinates (_a__i_,u2009_b__i_) and lower right corner in the cell with coordinates (_c__i_,u2009_d__i_) to the symbol _e__i_. Dasha asks you to find the special photo so that the total distance from it to all other special photos is minimum. And calculate this distance. Determine the distance between two photos as the sum of distances between all corresponding letters. The distance between two letters is the difference module of their positions in the alphabet. For example, the distance between letters 'h' and 'm' equals 8u2009-u200913u2009=u20095, because the letter 'h' is the 8-th in the alphabet, the letter 'm' is the 13-th. Input The first line contains three integers _n_, _m_, _k_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009103,u20091u2009≀u2009_k_u2009≀u20093Β·105) β€” the number of strings in the photo-template, the number of columns and the number of special photos which are interesting for Dasha. The next _n_ lines contains the string with _m_ length which consists of little Latin characters β€” the description of the photo-template. Each of the next _k_ lines contains the description of the special photo in the following format, "_a__i_ _b__i_ _c__i_ _d__i_ _e__i_" (1u2009≀u2009_a__i_u2009≀u2009_c__i_u2009≀u2009_n_,u20091u2009≀u2009_b__i_u2009≀u2009_d__i_u2009≀u2009_m_), where (_a__i_,u2009_b__i_) β€” is the coordinate of the upper left corner of the rectangle, (_c__i_,u2009_d__i_) β€” is the description of the lower right corner, and _e__i_ β€” is the little Latin letter which replaces the photo-template in the described rectangle. Output In the only line print the minimum total distance from the found special photo to all other special photos. Examples Input 3 3 2 aaa aaa aaa 1 1 2 2 b 2 2 3 3 c Input 5 5 3 abcde eabcd deabc cdeab bcdea 1 1 3 4 f 1 2 3 3 e 1 3 3 4 i Note In the first example the photos are following: bba aaa bba acc aaa acc The distance between them is 10.
2,600
false
false
true
true
true
false
true
false
false
false
6,744
732B
Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk. Empirically Polycarp learned that the dog needs at least _k_ walks for any two consecutive days in order to feel good. For example, if _k_u2009=u20095 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times. Polycarp analysed all his affairs over the next _n_ days and made a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_, where _a__i_ is the number of times Polycarp will walk with the dog on the _i_-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.). Help Polycarp determine the minimum number of walks he needs to do additionaly in the next _n_ days so that Cormen will feel good during all the _n_ days. You can assume that on the day before the first day and on the day after the _n_-th day Polycarp will go for a walk with Cormen exactly _k_ times. Write a program that will find the minumum number of additional walks and the appropriate schedulexa0β€” the sequence of integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (_b__i_u2009β‰₯u2009_a__i_), where _b__i_ means the total number of walks with the dog on the _i_-th day. Input The first line contains two integers _n_ and _k_ (1u2009≀u2009_n_,u2009_k_u2009≀u2009500)xa0β€” the number of days and the minimum number of walks with Cormen for any two consecutive days. The second line contains integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a__i_u2009≀u2009500)xa0β€” the number of walks with Cormen on the _i_-th day which Polycarp has already planned. Output In the first line print the smallest number of additional walks that Polycarp should do during the next _n_ days so that Cormen will feel good during all days. In the second line print _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_, where _b__i_xa0β€” the total number of walks on the _i_-th day according to the found solutions (_a__i_u2009≀u2009_b__i_ for all _i_ from 1 to _n_). If there are multiple solutions, print any of them.
1,000
false
true
false
true
false
false
false
false
false
false
6,869
827F
Arkady likes to walk around his kitchen. His labyrinthine kitchen consists of several important places connected with passages. Unfortunately it happens that these passages are flooded with milk so that it's impossible to pass through them. Namely, it's possible to pass through each passage in any direction only during some time interval. The lengths of all passages are equal and Arkady makes through them in one second. For security reasons, Arkady can never stop, also, he can't change direction while going through a passage. In other words, if he starts walking in some passage, he should reach its end and immediately leave the end. Today Arkady needs to quickly reach important place _n_ from place 1. He plans to exit the place 1 at time moment 0 and reach the place _n_ as early as he can. Please find the minimum time he should spend on his way. Input The first line contains two integers _n_ and _m_ (1u2009≀u2009_n_u2009≀u20095Β·105, 0u2009≀u2009_m_u2009≀u20095Β·105)xa0β€” the number of important places and the number of passages, respectively. After that, _m_ lines follow, each of them describe one passage. Each line contains four integers _a_, _b_, _l_ and _r_ (1u2009≀u2009_a_,u2009_b_u2009≀u2009_n_, _a_u2009β‰ u2009_b_, 0u2009≀u2009_l_u2009<u2009_r_u2009≀u2009109)xa0β€” the places the passage connects and the time segment during which it's possible to use this passage. Output Print one integerxa0β€” minimum time Arkady should spend to reach the destination. If he can't reach the place _n_, print -1. Examples Input 5 6 1 2 0 1 2 5 2 3 2 5 0 1 1 3 0 1 3 4 1 2 4 5 2 3 Note In the first example Arkady should go through important places 1u2009β†’u20093u2009β†’u20094u2009β†’u20095. In the second example Arkady can't start his walk because at time moment 0 it's impossible to use the only passage.
3,200
false
false
false
true
true
false
false
false
false
true
6,450
877B
Problem - 877B - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags brute force dp *1500 No tag edit access β†’ Contest materials . Announcement") without changing the order of the letters, where the 1-st and the 3-rd one contain only letters "a" and the 2-nd contains only letters "b". Nikita wants to make the string beautiful by removing some (possibly none) of its characters, but without changing their order. What is the maximum length of the string he can get? Input The first line contains a non-empty string of length not greater than 5u2009000 containing only lowercase English letters "a" and "b". Output Print a single integerxa0β€” the maximum possible size of beautiful string Nikita can get. Examples Input abba Output 4 Input bab Output 2 Note It the first sample the string is already beautiful. In the second sample he needs to delete one of "b" to make it beautiful.
1,500
false
false
false
true
false
false
true
false
false
false
6,239
1396E
You are given an integer $$$k$$$ and a tree $$$T$$$ with $$$n$$$ nodes ($$$n$$$ is even). Let $$$dist(u, v)$$$ be the number of edges on the shortest path from node $$$u$$$ to node $$$v$$$ in $$$T$$$. Let us define a undirected weighted complete graph $$$G = (V, E)$$$ as following: $$$V = {x mid 1 le x le n }$$$ i.e. the set of integers from $$$1$$$ to $$$n$$$ $$$E = {(u, v, w) mid 1 le u, v le n, u eq v, w = dist(u, v) }$$$ i.e. there is an edge between every pair of distinct nodes, the weight being the distance between their respective nodes in $$$T$$$ Your task is simple, find a perfect matching in $$$G$$$ with total edge weight $$$k$$$ $$$(1 le k le n^2)$$$. Input The first line of input contains two integers $$$n$$$, $$$k$$$ ($$$2 le n le 100,000$$$, $$$n$$$ is even, $$$1 le k le n^2$$$): number of nodes and the total edge weight of the perfect matching you need to find. The $$$i$$$-th of the following $$$n - 1$$$ lines contains two integers $$$v_i$$$, $$$u_i$$$ ($$$1 le v_i, u_i le n$$$) denoting an edge between $$$v_i$$$ and $$$u_i$$$ in $$$T$$$. It is guaranteed that the given graph is a tree. Output If there are no matchings that satisfy the above condition, output "NO" (without quotes) on a single line. Otherwise, you should output "YES" (without quotes) on the first line of output. You should then output $$$frac{n}{2}$$$ lines, the $$$i$$$-th line containing $$$p_i, q_i$$$ ($$$1 le p_i, q_i le n$$$): the $$$i$$$-th pair of the matching. Note A tree is a connected acyclic undirected graph. A matching is set of pairwise non-adjacent edges, none of which are loops; that is, no two edges share a common vertex. A perfect matching is a matching which matches all vertices of the graph; that is, every vertex of the graph is incident to exactly one edge of the matching.
3,200
false
false
false
false
false
true
false
false
false
false
3,689
1207A
There are two types of burgers in your restaurant β€” hamburgers and chicken burgers! To assemble a hamburger you need two buns and a beef patty. To assemble a chicken burger you need two buns and a chicken cutlet. You have $$$b$$$ buns, $$$p$$$ beef patties and $$$f$$$ chicken cutlets in your restaurant. You can sell one hamburger for $$$h$$$ dollars and one chicken burger for $$$c$$$ dollars. Calculate the maximum profit you can achieve. You have to answer $$$t$$$ independent queries. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) – the number of queries. The first line of each query contains three integers $$$b$$$, $$$p$$$ and $$$f$$$ ($$$1 le b, ~p, ~f le 100$$$) β€” the number of buns, beef patties and chicken cutlets in your restaurant. The second line of each query contains two integers $$$h$$$ and $$$c$$$ ($$$1 le h, ~c le 100$$$) β€” the hamburger and chicken burger prices in your restaurant. Output For each query print one integer β€” the maximum profit you can achieve. Example Input 3 15 2 3 5 10 7 5 2 10 12 1 100 100 100 100 Note In first query you have to sell two hamburgers and three chicken burgers. Your income is $$$2 cdot 5 + 3 cdot 10 = 40$$$. In second query you have to ell one hamburgers and two chicken burgers. Your income is $$$1 cdot 10 + 2 cdot 12 = 34$$$. In third query you can not create any type of burgers because because you have only one bun. So your income is zero.
800
true
true
true
false
false
false
true
false
false
false
4,661
1949B
The Czech cuisine features $$$n$$$ appetizers and $$$n$$$ main dishes. The $$$i$$$-th appetizer has spiciness $$$a_i$$$, and the $$$i$$$-th main dish has spiciness $$$b_i$$$. A typical Czech meal consists of exactly one appetizer and one main dish. You want to pair up the $$$n$$$ appetizers and $$$n$$$ main dishes into $$$n$$$ meals with each appetizer and each main dish being included in exactly one meal. Your meals shall surprise the diners, so you want the spiciness levels of the two parts of the same meal to be as different as possible. The charm of a meal is the difference (in absolute value) between the spiciness of the appetizer and the spiciness of the main dish. So, a meal consisting of an appetizer with spiciness $$$x$$$ and a main dish with spiciness $$$y$$$ has charm equal to $$$x-y$$$. You want to maximize the minimum charm of the resulting $$$n$$$ meals. What is the largest possible value of the minimum charm that you can achieve? Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1le tle 1,000$$$)xa0β€” the number of test cases. The descriptions of the $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 5,000$$$)xa0β€”the number of appetizers and main dishes. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^{9}$$$)xa0β€” the spicinesses of the $$$n$$$ appetizers. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$0 leq b_i leq 10^{9}$$$)xa0β€” the spicinesses of the $$$n$$$ main dishes. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$25cdot 10^6$$$. Output For each test case, print the largest possible value of the minimum charm you can achieve. Example Input 4 3 0 0 0 1000000000 1000000000 1000000000 5 1 2 3 4 5 1 2 3 4 5 6 0 0 0 100 100 100 100 100 100 0 0 0 7 14 25 62 74 86 95 12 51 62 71 72 92 20 84 Output 1000000000 2 100 30 Note In the first test case, no matter how you pair up the appetizers with the main dishes, each meal will have an appetizer with spiciness $$$0$$$ and a main dish with spiciness $$$1000000000$$$, so the charm of each meal will be $$$1000000000$$$. In the second test case, one optimal way to pair up appetizers and main dishes is: $$$(1, 5)$$$, $$$(2, 4)$$$, $$$(3, 1)$$$, $$$(4, 2)$$$, $$$(5, 3)$$$. The corresponding meals have charms: $$$4$$$, $$$2$$$, $$$2$$$, $$$2$$$, $$$2$$$. The resulting minimum charm is $$$2$$$. In the third test case, one way to maximize the minimum charm is to pair up the three appetizers with spiciness $$$0$$$ with the three main dishes with spiciness $$$100$$$, and the three appetizers with spiciness $$$100$$$ with the three main dishes with spiciness $$$0$$$. Doing so, the charm of each meal will be exactly $$$100$$$.
1,500
false
true
false
false
false
false
true
true
true
false
591
1283C
There are $$$n$$$ friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself. For each friend the value $$$f_i$$$ is known: it is either $$$f_i = 0$$$ if the $$$i$$$-th friend doesn't know whom he wants to give the gift to or $$$1 le f_i le n$$$ if the $$$i$$$-th friend wants to give the gift to the friend $$$f_i$$$. You want to fill in the unknown values ($$$f_i = 0$$$) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory. If there are several answers, you can print any. Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) β€” the number of friends. The second line of the input contains $$$n$$$ integers $$$f_1, f_2, dots, f_n$$$ ($$$0 le f_i le n$$$, $$$f_i e i$$$, all $$$f_i e 0$$$ are distinct), where $$$f_i$$$ is the either $$$f_i = 0$$$ if the $$$i$$$-th friend doesn't know whom he wants to give the gift to or $$$1 le f_i le n$$$ if the $$$i$$$-th friend wants to give the gift to the friend $$$f_i$$$. It is also guaranteed that there is at least two values $$$f_i = 0$$$. Output Print $$$n$$$ integers $$$nf_1, nf_2, dots, nf_n$$$, where $$$nf_i$$$ should be equal to $$$f_i$$$ if $$$f_i e 0$$$ or the number of friend whom the $$$i$$$-th friend wants to give the gift to. All values $$$nf_i$$$ should be distinct, $$$nf_i$$$ cannot be equal to $$$i$$$. Each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. If there are several answers, you can print any.
1,500
true
false
false
false
true
true
false
false
false
false
4,278
478C
Problem - 478C - 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 *1800 No tag edit access β†’ Contest materials ") :: Ρ€Π°Π·Π±ΠΎΡ€ Π·Π°Π΄Π°Ρ‡") β€” the number of red, green and blue baloons respectively. The numbers are separated by exactly one space. Output Print a single integer _t_ β€” the maximum number of tables that can be decorated in the required manner. Examples Input 5 4 3 Output 4 Input 1 1 1 Output 1 Input 2 3 3 Output 2 Note In the first sample you can decorate the tables with the following balloon sets: "rgg", "gbb", "brr", "rrg", where "r", "g" and "b" represent the red, green and blue balls, respectively.
1,800
false
true
false
false
false
false
false
false
false
false
7,919
192A
Problem - 192A - 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 brute force implementation *1300 No tag edit access β†’ Contest materials , and the coolest numbers are those that are representable as a sum of two triangular numbers. A well-known hipster Andrew adores everything funky and cool but unfortunately, he isn't good at maths. Given number _n_, help him define whether this number can be represented by a sum of two triangular numbers (not necessarily different)! Input The first input line contains an integer _n_ (1u2009≀u2009_n_u2009≀u2009109). Output Print "YES" (without the quotes), if _n_ can be represented as a sum of two triangular numbers, otherwise print "NO" (without the quotes). Examples Input 256 Output YES Input 512 Output NO Note In the first sample number . In the second sample number 512 can not be represented as a sum of two triangular numbers.
1,300
false
false
true
false
false
false
true
true
false
false
9,072
1783B
For a square matrix of integers of size $$$n imes n$$$, let's define its beauty as follows: for each pair of side-adjacent elements $$$x$$$ and $$$y$$$, write out the number $$$x-y$$$, and then find the number of different numbers among them. For example, for the matrix $$$begin{pmatrix} 1 & 3 4 & 2 end{pmatrix}$$$ the numbers we consider are $$$1-3=2$$$, $$$1-4=3$$$, $$$3-2=1$$$ and $$$4-2=2$$$; there are $$$3$$$ different numbers among them ($$$2$$$, $$$3$$$ and $$$1$$$), which means that its beauty is equal to $$$3$$$. You are given an integer $$$n$$$. You have to find a matrix of size $$$n imes n$$$, where each integer from $$$1$$$ to $$$n^2$$$ occurs exactly once, such that its beauty is the maximum possible among all such matrices. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 49$$$)xa0– the number of test cases. The first (and only) line of each test case contains a single integer $$$n$$$ ($$$2 le n le 50$$$). Output For each test case, print $$$n$$$ rows of $$$n$$$ integersxa0β€” a matrix of integers of size $$$n imes n$$$, where each number from $$$1$$$ to $$$n^2$$$ occurs exactly once, such that its beauty is the maximum possible among all such matrices. If there are multiple answers, print any of them. Example Output 1 3 4 2 1 3 4 9 2 7 5 8 6
1,100
true
false
false
false
false
true
false
false
false
false
1,578
520D
Once Vasya and Petya assembled a figure of _m_ cubes, each of them is associated with a number between 0 and _m_u2009-u20091 (inclusive, each number appeared exactly once). Let's consider a coordinate system such that the _OX_ is the ground, and the _OY_ is directed upwards. Each cube is associated with the coordinates of its lower left corner, these coordinates are integers for each cube. The figure turned out to be stable. This means that for any cube that is not on the ground, there is at least one cube under it such that those two cubes touch by a side or a corner. More formally, this means that for the cube with coordinates (_x_,u2009_y_) either _y_u2009=u20090, or there is a cube with coordinates (_x_u2009-u20091,u2009_y_u2009-u20091), (_x_,u2009_y_u2009-u20091) or (_x_u2009+u20091,u2009_y_u2009-u20091). Now the boys want to disassemble the figure and put all the cubes in a row. In one step the cube is removed from the figure and being put to the right of the blocks that have already been laid. The guys remove the cubes in such order that the figure remains stable. To make the process more interesting, the guys decided to play the following game. The guys take out the cubes from the figure in turns. It is easy to see that after the figure is disassembled, the integers written on the cubes form a number, written in the _m_-ary positional numerical system (possibly, with a leading zero). Vasya wants the resulting number to be maximum possible, and Petya, on the contrary, tries to make it as small as possible. Vasya starts the game. Your task is to determine what number is formed after the figure is disassembled, if the boys play optimally. Determine the remainder of the answer modulo 109u2009+u20099. Input The first line contains number _m_ (2u2009≀u2009_m_u2009≀u2009105). The following _m_ lines contain the coordinates of the cubes _x__i_,u2009_y__i_ (u2009-u2009109u2009≀u2009_x__i_u2009≀u2009109, 0u2009≀u2009_y__i_u2009≀u2009109) in ascending order of numbers written on them. It is guaranteed that the original figure is stable. No two cubes occupy the same place.
2,100
false
true
true
false
false
false
false
false
false
false
7,754
1822A
Mushroom Filippov cooked himself a meal and while having his lunch, he decided to watch a video on TubeTube. He can not spend more than $$$t$$$ seconds for lunch, so he asks you for help with the selection of video. The TubeTube feed is a list of $$$n$$$ videos, indexed from $$$1$$$ to $$$n$$$. The $$$i$$$-th video lasts $$$a_i$$$ seconds and has an entertainment value $$$b_i$$$. Initially, the feed is opened on the first video, and Mushroom can skip to the next video in $$$1$$$ second (if the next video exists). Mushroom can skip videos any number of times (including zero). Help Mushroom choose one video that he can open and watch in $$$t$$$ seconds. If there are several of them, he wants to choose the most entertaining one. Print the index of any appropriate video, or $$$-1$$$ if there is no such. Input The first line of the input data contains a single integer $$$q$$$ ($$$1 le q le 1000$$$)xa0β€” the number of test cases in the test. The description of the test cases follows. The first line of a test case contains two integers $$$n$$$ and $$$t$$$ ($$$1 le n le 50$$$, $$$1 le t le 200$$$)xa0β€” the number of videos in the feed and seconds for lunch, respectively. The second line of a test case contains $$$n$$$ integers $$$a_1, a_2, a_3, dots, a_n$$$ ($$$1 le a_i le 100$$$)xa0β€” durations of videos. The third line of a test case contains $$$n$$$ integers $$$b_1, b_2, b_3, dots, b_n$$$ ($$$1 le b_i le 100$$$)xa0β€” entertainment values of videos. Output Output $$$q$$$ integers, each of which is the answer to the corresponding test case. As an answer, output the index of the most entertaining video that Mushroom will have time to watch. If there are several answers, you are allowed to output any of them. Output $$$-1$$$, if there is no video he can watch during his lunch break. Example Input 5 5 9 1 5 7 6 6 3 4 7 1 9 4 4 4 3 3 2 1 2 3 4 5 7 5 5 5 5 5 2 1 3 9 7 4 33 54 71 69 96 42 24 99 1 2 179 55 66 77 88
800
false
false
true
false
false
false
true
false
false
false
1,351
703A
Mishka is a little polar bear. As known, little bears loves spending their free time playing dice for chocolates. Once in a wonderful sunny morning, walking around blocks of ice, Mishka met her friend Chris, and they started playing the game. Rules of the game are very simple: at first number of rounds _n_ is defined. In every round each of the players throws a cubical dice with distinct numbers from 1 to 6 written on its faces. Player, whose value after throwing the dice is greater, wins the round. In case if player dice values are equal, no one of them is a winner. In average, player, who won most of the rounds, is the winner of the game. In case if two players won the same number of rounds, the result of the game is draw. Mishka is still very little and can't count wins and losses, so she asked you to watch their game and determine its result. Please help her! Input The first line of the input contains single integer _n_ _n_ (1u2009≀u2009_n_u2009≀u2009100)xa0β€” the number of game rounds. The next _n_ lines contains rounds description. _i_-th of them contains pair of integers _m__i_ and _c__i_ (1u2009≀u2009_m__i_,u2009u2009_c__i_u2009≀u20096)xa0β€” values on dice upper face after Mishka's and Chris' throws in _i_-th round respectively. Output If Mishka is the winner of the game, print "Mishka" (without quotes) in the only line. If Chris is the winner of the game, print "Chris" (without quotes) in the only line. If the result of the game is draw, print "Friendship is magic!^^" (without quotes) in the only line. Examples Output Friendship is magic!^^ Note In the first sample case Mishka loses the first round, but wins second and third rounds and thus she is the winner of the game. In the second sample case Mishka wins the first round, Chris wins the second round, and the game ends with draw with score 1:1. In the third sample case Chris wins the first round, but there is no winner of the next two rounds. The winner of the game is Chris.
800
false
false
true
false
false
false
false
false
false
false
7,011
1796A
The FB-string is formed as follows. Initially, it is empty. We go through all positive integers, starting from $$$1$$$, in ascending order, and do the following for each integer: if the current integer is divisible by $$$3$$$, append F to the end of the FB-string; if the current integer is divisible by $$$5$$$, append B to the end of the FB-string. Note that if an integer is divisible by both $$$3$$$ and $$$5$$$, we append F, and then B, not in the opposite order. The first $$$10$$$ characters of the FB-string are FBFFBFFBFB: the first F comes from the integer $$$3$$$, the next character (B) comes from $$$5$$$, the next F comes from the integer $$$6$$$, and so on. It's easy to see that this string is infinitely long. Let $$$f_i$$$ be the $$$i$$$-th character of FB-string; so, $$$f_1$$$ is F, $$$f_2$$$ is B, $$$f_3$$$ is F, $$$f_4$$$ is F, and so on. You are given a string $$$s$$$, consisting of characters F and/or B. You have to determine whether it is a substring (contiguous subsequence) of the FB-string. In other words, determine if it is possible to choose two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r$$$) so that the string $$$f_l f_{l+1} f_{l+2} dots f_r$$$ is exactly $$$s$$$. For example: FFB is a substring of the FB-string: if we pick $$$l = 3$$$ and $$$r = 5$$$, the string $$$f_3 f_4 f_5$$$ is exactly FFB; BFFBFFBF is a substring of the FB-string: if we pick $$$l = 2$$$ and $$$r = 9$$$, the string $$$f_2 f_3 f_4 dots f_9$$$ is exactly BFFBFFBF; BBB is not a substring of the FB-string. Input The first line contains one integer $$$t$$$ ($$$1 le t le 2046$$$) β€” the number of test cases. Each test case consists of two lines. The first line contains one integer $$$k$$$ ($$$1 le k le 10$$$) β€” the number of characters in $$$s$$$. The second line contains $$$s$$$, which is a string of exactly $$$k$$$ characters. Each character of $$$s$$$ is either F or B. Output For each test case, print YES if $$$s$$$ is a substring of the FB-string, or NO otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
800
false
false
true
false
false
false
true
false
false
false
1,502
136A
Little Petya very much likes gifts. Recently he has received a new laptop as a New Year gift from his mother. He immediately decided to give it to somebody else as what can be more pleasant than giving somebody gifts. And on this occasion he organized a New Year party at his place and invited _n_ his friends there. If there's one thing Petya likes more that receiving gifts, that's watching others giving gifts to somebody else. Thus, he safely hid the laptop until the next New Year and made up his mind to watch his friends exchanging gifts while he does not participate in the process. He numbered all his friends with integers from 1 to _n_. Petya remembered that a friend number _i_ gave a gift to a friend number _p__i_. He also remembered that each of his friends received exactly one gift. Now Petya wants to know for each friend _i_ the number of a friend who has given him a gift. Input The first line contains one integer _n_ (1u2009≀u2009_n_u2009≀u2009100) β€” the quantity of friends Petya invited to the party. The second line contains _n_ space-separated integers: the _i_-th number is _p__i_ β€” the number of a friend who gave a gift to friend number _i_. It is guaranteed that each friend received exactly one gift. It is possible that some friends do not share Petya's ideas of giving gifts to somebody else. Those friends gave the gifts to themselves. Output Print _n_ space-separated integers: the _i_-th number should equal the number of the friend who gave a gift to friend number _i_.
800
false
false
true
false
false
false
false
false
false
false
9,332
1914E1
The easy and hard versions of this problem differ only in the constraints on the number of test cases and $$$n$$$. In the easy version, the number of test cases does not exceed $$$10^3$$$, and $$$n$$$ does not exceed $$$6$$$. Recently, Alice and Bob were given marbles of $$$n$$$ different colors by their parents. Alice has received $$$a_1$$$ marbles of color $$$1$$$, $$$a_2$$$ marbles of color $$$2$$$,..., $$$a_n$$$ marbles of color $$$n$$$. Bob has received $$$b_1$$$ marbles of color $$$1$$$, $$$b_2$$$ marbles of color $$$2$$$, ..., $$$b_n$$$ marbles of color $$$n$$$. All $$$a_i$$$ and $$$b_i$$$ are between $$$1$$$ and $$$10^9$$$. After some discussion, Alice and Bob came up with the following game: players take turns, starting with Alice. On their turn, a player chooses a color $$$i$$$ such that both players have at least one marble of that color. The player then discards one marble of color $$$i$$$, and their opponent discards all marbles of color $$$i$$$. The game ends when there is no color $$$i$$$ such that both players have at least one marble of that color. The score in the game is the difference between the number of remaining marbles that Alice has and the number of remaining marbles that Bob has at the end of the game. In other words, the score in the game is equal to $$$(A-B)$$$, where $$$A$$$ is the number of marbles Alice has and $$$B$$$ is the number of marbles Bob has at the end of the game. Alice wants to maximize the score, while Bob wants to minimize it. Calculate the score at the end of the game if both players play optimally. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$) β€” the number of test cases. Each test case consists of three lines: the first line contains a single integer $$$n$$$ ($$$2 le n le 6$$$) β€” the number of colors; the second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the number of marbles of the $$$i$$$-th color that Alice has; the third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^9$$$), where $$$b_i$$$ is the number of marbles of the $$$i$$$-th color that Bob has. Note In the first example, one way to achieve a score of $$$1$$$ is as follows: 1. Alice chooses color $$$1$$$, discards $$$1$$$ marble. Bob also discards $$$1$$$ marble; 2. Bob chooses color $$$3$$$, discards $$$1$$$ marble. Alice also discards $$$1$$$ marble; 3. Alice chooses color $$$2$$$, discards $$$1$$$ marble, and Bob discards $$$2$$$ marble. As a result, Alice has $$$a = [3, 1, 0]$$$ remaining, and Bob has $$$b = [0, 0, 3]$$$ remaining. The score is $$$3 + 1 - 3 = 1$$$. It can be shown that neither Alice nor Bob can achieve a better score if both play optimally. In the second example, Alice can first choose color $$$1$$$, then Bob will choose color $$$4$$$, after which Alice will choose color $$$2$$$, and Bob will choose color $$$3$$$. It can be shown that this is the optimal game.
1,400
false
true
false
false
false
false
true
false
true
false
815
1787A
Problem - 1787A - 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 *800 No tag edit access β†’ Contest materials ") Editorial") $$$ ($$$1leq x,yleq n$$$) such that $$$x^ycdot y+y^xcdot x = n$$$. Input The first line contains a single integer $$$t$$$ ($$$1leq tleq 10^4$$$) β€” the number of test cases. Each test case contains one line with a single integer $$$n$$$ ($$$1leq nleq 10^9$$$). Output For each test case, if possible, print two integers $$$x$$$ and $$$y$$$ ($$$1leq x,yleq n$$$). If there are multiple answers, print any. Otherwise, print $$$-1$$$. Example Input 5 3 7 42 31250 20732790 Output -1 -1 2 3 5 5 3 13 Note In the third test case, $$$2^3 cdot 3+3^2 cdot 2 = 42$$$, so $$$(2,3),(3,2)$$$ will be considered as legal solutions. In the fourth test case, $$$5^5 cdot 5+5^5 cdot 5 = 31250$$$, so $$$(5,5)$$$ is a legal solution.
800
true
false
false
false
false
true
false
false
false
false
1,563
40A
Problem - 40A - Codeforces =============== xa0 ")
1,300
true
false
true
false
false
true
false
false
false
false
9,789
329E
There are _n_ cities on a two dimensional Cartesian plane. The distance between two cities is equal to the Manhattan distance between them (see the Notes for definition). A Hamiltonian cycle of the cities is defined as a permutation of all _n_ cities. The length of this Hamiltonian cycle is defined as the sum of the distances between adjacent cities in the permutation plus the distance between the first and final city in the permutation. Please compute the longest possible length of a Hamiltonian cycle of the given cities. Input The first line contains an integer _n_ (3u2009≀u2009_n_u2009≀u2009105). Then _n_ lines follow, each consisting of two integers _x__i_ and _y__i_ (0u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009109), denoting the coordinates of a city. All given points will be distinct. Output A single line denoting the longest possible length of a Hamiltonian cycle of the given cities. You should not output the cycle, only its length. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Note In the example, one of the possible Hamiltonian cycles with length 6 is (1, 1) (1, 2) (2, 1) (2, 2). There does not exist any other Hamiltonian cycle with a length greater than 6. The Manhattan distance between two cities (_x__i_,u2009_y__i_) and (_x__j_,u2009_y__j_) is _x__i_u2009-u2009_x__j_u2009+u2009_y__i_u2009-u2009_y__j_.
3,100
true
false
false
false
false
false
false
false
false
false
8,525
1294A
Polycarp has three sisters: Alice, Barbara, and Cerene. They're collecting coins. Currently, Alice has $$$a$$$ coins, Barbara has $$$b$$$ coins and Cerene has $$$c$$$ coins. Recently Polycarp has returned from the trip around the world and brought $$$n$$$ coins. He wants to distribute all these $$$n$$$ coins between his sisters in such a way that the number of coins Alice has is equal to the number of coins Barbara has and is equal to the number of coins Cerene has. In other words, if Polycarp gives $$$A$$$ coins to Alice, $$$B$$$ coins to Barbara and $$$C$$$ coins to Cerene ($$$A+B+C=n$$$), then $$$a + A = b + B = c + C$$$. Note that A, B or C (the number of coins Polycarp gives to Alice, Barbara and Cerene correspondingly) can be 0. Your task is to find out if it is possible to distribute all $$$n$$$ coins between sisters in a way described above. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a new line and consists of four space-separated integers $$$a, b, c$$$ and $$$n$$$ ($$$1 le a, b, c, n le 10^8$$$) β€” the number of coins Alice has, the number of coins Barbara has, the number of coins Cerene has and the number of coins Polycarp has. Output For each test case, print "YES" if Polycarp can distribute all $$$n$$$ coins between his sisters and "NO" otherwise. Example Input 5 5 3 2 8 100 101 102 105 3 2 1 100000000 10 20 15 14 101 101 101 3
800
true
false
false
false
false
false
false
false
false
false
4,229
201E
Once _n_ people simultaneously signed in to the reception at the recently opened, but already thoroughly bureaucratic organization (abbreviated TBO). As the organization is thoroughly bureaucratic, it can accept and cater for exactly one person per day. As a consequence, each of _n_ people made an appointment on one of the next _n_ days, and no two persons have an appointment on the same day. However, the organization workers are very irresponsible about their job, so none of the signed in people was told the exact date of the appointment. The only way to know when people should come is to write some requests to TBO. The request form consists of _m_ empty lines. Into each of these lines the name of a signed in person can be written (it can be left blank as well). Writing a person's name in the same form twice is forbidden, such requests are ignored. TBO responds very quickly to written requests, but the reply format is of very poor quality β€” that is, the response contains the correct appointment dates for all people from the request form, but the dates are in completely random order. Responds to all requests arrive simultaneously at the end of the day (each response specifies the request that it answers). Fortunately, you aren't among these _n_ lucky guys. As an observer, you have the following task β€” given _n_ and _m_, determine the minimum number of requests to submit to TBO to clearly determine the appointment date for each person. Input The first line contains a single integer _t_ (1u2009≀u2009_t_u2009≀u20091000) β€” the number of test cases. Each of the following _t_ lines contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009109) β€” the number of people who have got an appointment at TBO and the number of empty lines in the request form, correspondingly. Note In the first sample, you need to submit three requests to TBO with three different names. When you learn the appointment dates of three people out of four, you can find out the fourth person's date by elimination, so you do not need a fourth request. In the second sample you need only two requests. Let's number the persons from 1 to 4 and mention persons 1 and 2 in the first request and persons 1 and 3 in the second request. It is easy to see that after that we can clearly determine each person's appointment date regardless of the answers obtained from TBO. In the fourth sample only one person signed up for an appointment. He doesn't need to submit any requests β€” his appointment date is tomorrow.
2,600
false
false
false
false
false
false
false
true
false
false
9,035
839B
Daenerys Targaryen has an army consisting of _k_ groups of soldiers, the _i_-th group contains _a__i_ soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has _n_ rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1,u20092}, {3,u20094}, {4,u20095}, {5,u20096} or {7,u20098}. A row in the airplane Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats. Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied. Input The first line contains two integers _n_ and _k_ (1u2009≀u2009_n_u2009≀u200910000, 1u2009≀u2009_k_u2009≀u2009100)xa0β€” the number of rows and the number of groups of soldiers, respectively. The second line contains _k_ integers _a_1,u2009_a_2,u2009_a_3,u2009...,u2009_a__k_ (1u2009≀u2009_a__i_u2009≀u200910000), where _a__i_ denotes the number of soldiers in the _i_-th group. It is guaranteed that _a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__k_u2009≀u20098Β·_n_. Output If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes). You can choose the case (lower or upper) for each letter arbitrary. Note In the first sample, Daenerys can place the soldiers like in the figure below: In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group. In the third example Daenerys can place the first group on seats (1,u20092,u20097,u20098), and the second group an all the remaining seats. In the fourth example she can place the first two groups on seats (1,u20092) and (7,u20098), the third group on seats (3), and the fourth group on seats (5,u20096).
1,900
false
true
true
false
false
false
true
false
false
false
6,407
1479D
In Homer's country, there are $$$n$$$ cities numbered $$$1$$$ to $$$n$$$ and they form a tree. That is, there are $$$(n-1)$$$ undirected roads between these $$$n$$$ cities and every two cities can reach each other through these roads. Homer's country is an industrial country, and each of the $$$n$$$ cities in it contains some mineral resource. The mineral resource of city $$$i$$$ is labeled $$$a_i$$$. Homer is given the plans of the country in the following $$$q$$$ years. The plan of the $$$i$$$-th year is described by four parameters $$$u_i, v_i, l_i$$$ and $$$r_i$$$, and he is asked to find any mineral resource $$$c_i$$$ such that the following two conditions hold: mineral resource $$$c_i$$$ appears an odd number of times between city $$$u_i$$$ and city $$$v_i$$$; and $$$l_i leq c_i leq r_i$$$. As the best friend of Homer, he asks you for help. For every plan, find any such mineral resource $$$c_i$$$, or tell him that there doesn't exist one. Input The first line contains two integers $$$n$$$ ($$$1 leq n leq 3 cdot 10^5$$$) and $$$q$$$ ($$$1 leq q leq 3 cdot 10^5$$$), indicating the number of cities and the number of plans. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq n$$$). Then the $$$i$$$-th line of the following $$$(n-1)$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 leq x_i, y_i leq n$$$) with $$$x_i eq y_i$$$, indicating that there is a bidirectional road between city $$$x_i$$$ and city $$$y_i$$$. It is guaranteed that the given roads form a tree. Then the $$$i$$$-th line of the following $$$q$$$ lines contains four integers $$$u_i$$$, $$$v_i$$$, $$$l_i$$$, $$$r_i$$$ ($$$1 leq u_i leq n$$$, $$$1 leq v_i leq n$$$, $$$1 leq l_i leq r_i leq n$$$), indicating the plan of the $$$i$$$-th year. Output Print $$$q$$$ lines, the $$$i$$$-th of which contains an integer $$$c_i$$$ such that $$$c_i = {-1}$$$ if there is no such mineral resource that meets the required condition; or $$$c_i$$$ is the label of the chosen mineral resource of the $$$i$$$-th year. The chosen mineral resource $$$c_i$$$ should meet those conditions in the $$$i$$$-th year described above in the problem statement. If there are multiple choices of $$$c_i$$$, you can print any of them. Example Input 6 8 3 2 1 3 1 3 1 2 1 3 2 4 2 5 4 6 3 5 1 1 3 5 1 3 3 5 1 3 1 1 2 2 1 1 3 3 1 4 1 5 1 6 1 3 1 6 1 3 Note In the first three queries, there are four cities between city $$$3$$$ and city $$$5$$$, which are city $$$1$$$, city $$$2$$$, city $$$3$$$ and city $$$5$$$. The mineral resources appear in them are mineral resources $$$1$$$ (appears in city $$$3$$$ and city $$$5$$$), $$$2$$$ (appears in city $$$2$$$) and $$$3$$$ (appears in city $$$1$$$). It is noted that The first query is only to check whether mineral source $$$1$$$ appears an odd number of times between city $$$3$$$ and city $$$5$$$. The answer is no, because mineral source $$$1$$$ appears twice (an even number of times) between city $$$3$$$ and city $$$5$$$. The second and the third queries are the same but they can choose different mineral resources. Both mineral resources $$$2$$$ and $$$3$$$ are available.
2,900
false
false
false
false
true
false
true
true
false
false
3,277
2B
Problem - 2B - Codeforces =============== xa0 , _n_ is the size of the matrix. Then follow _n_ lines containing the matrix elements (non-negative integer numbers not exceeding 109). Output In the first line print the least number of trailing zeros. In the second line print the correspondent way itself. Examples Input 3 1 2 3 4 5 6 7 8 9 Output 0 DDRR
2,000
true
false
false
true
false
false
false
false
false
false
9,980
676C
Problem - 676C - 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 dp strings two pointers *1500 No tag edit access β†’ Contest materials consisting of equal letters. Vasya can change no more than _k_ characters of the original string. What is the maximum beauty of the string he can achieve? Input The first line of the input contains two integers _n_ and _k_ (1u2009≀u2009_n_u2009≀u2009100u2009000,u20090u2009≀u2009_k_u2009≀u2009_n_)xa0β€” the length of the string and the maximum number of characters to change. The second line contains the string, consisting of letters 'a' and 'b' only. Output Print the only integerxa0β€” the maximum beauty of the string Vasya can achieve by changing no more than _k_ characters. Examples Input 4 2 abba Output 4 Input 8 1 aabaabaa Output 5 Note In the first sample, Vasya can obtain both strings "aaaa" and "bbbb". In the second sample, the optimal answer is obtained with the string "aaaaabaa" or with the string "aabaaaaa".
1,500
false
false
false
true
false
false
false
true
false
false
7,124
1433F
You are given a matrix $$$a$$$ of size $$$n imes m$$$ consisting of integers. You can choose no more than $$$leftlfloorfrac{m}{2} ight floor$$$ elements in each row. Your task is to choose these elements in such a way that their sum is divisible by $$$k$$$ and this sum is the maximum. In other words, you can choose no more than a half (rounded down) of elements in each row, you have to find the maximum sum of these elements divisible by $$$k$$$. Note that you can choose zero elements (and the sum of such set is $$$0$$$). Input The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le n, m, k le 70$$$) β€” the number of rows in the matrix, the number of columns in the matrix and the value of $$$k$$$. The next $$$n$$$ lines contain $$$m$$$ elements each, where the $$$j$$$-th element of the $$$i$$$-th row is $$$a_{i, j}$$$ ($$$1 le a_{i, j} le 70$$$). Output Print one integer β€” the maximum sum divisible by $$$k$$$ you can obtain. Examples Input 3 4 3 1 2 3 4 5 2 2 2 7 1 1 4 Input 5 5 4 1 2 4 2 1 3 5 1 2 4 1 5 7 1 2 3 8 7 1 2 8 4 7 1 6 Note In the first example, the optimal answer is $$$2$$$ and $$$4$$$ in the first row, $$$5$$$ and $$$2$$$ in the second row and $$$7$$$ and $$$4$$$ in the third row. The total sum is $$$2 + 4 + 5 + 2 + 7 + 4 = 24$$$.
2,100
false
false
false
true
false
false
false
false
false
false
3,494
336A
Problem - 336A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation math *1000 No tag edit access β†’ Contest materials ") ") , and the opposite vertex at point (_x_,u2009_y_). Of course, the sides of Vasya's favorite rectangle are parallel to the coordinate axes. Vasya also loves triangles, if the triangles have one vertex at point _B_u2009=u2009(0,u20090). That's why today he asks you to find two points _A_u2009=u2009(_x_1,u2009_y_1) and _C_u2009=u2009(_x_2,u2009_y_2), such that the following conditions hold: the coordinates of points: _x_1, _x_2, _y_1, _y_2 are integers. Besides, the following inequation holds: _x_1u2009<u2009_x_2; the triangle formed by point _A_, _B_ and _C_ is rectangular and isosceles ( is right); all points of the favorite rectangle are located inside or on the border of triangle _ABC_; the area of triangle _ABC_ is as small as possible. Help the bear, find the required points. It is not so hard to proof that these points are unique. Input The first line contains two integers _x_,u2009_y_ (u2009-u2009109u2009≀u2009_x_,u2009_y_u2009≀u2009109,u2009_x_u2009β‰ u20090,u2009_y_u2009β‰ u20090). Output Print in the single line four integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 β€” the coordinates of the required points. Examples Input 10 5 Output 0 15 15 0 Input -10 5 Output -15 0 0 15 Note Figure to the first sample
1,000
true
false
true
false
false
false
false
false
false
false
8,492
309A
People like to be fit. That's why many of them are ready to wake up at dawn, go to the stadium and run. In this problem your task is to help a company design a new stadium. The city of N has a shabby old stadium. Many people like it and every morning thousands of people come out to this stadium to run. The stadium can be represented as a circle, its length is exactly _l_ meters with a marked start line. However, there can't be simultaneous start in the morning, so exactly at 7, each runner goes to his favorite spot on the stadium and starts running from there. Note that not everybody runs in the same manner as everybody else. Some people run in the clockwise direction, some of them run in the counter-clockwise direction. It mostly depends on the runner's mood in the morning, so you can assume that each running direction is equiprobable for each runner in any fixed morning. The stadium is tiny and is in need of major repair, for right now there only is one running track! You can't get too playful on a single track, that's why all runners keep the same running speed β€” exactly 1 meter per a time unit. Nevertheless, the runners that choose different directions bump into each other as they meet. The company wants to design a new stadium, but they first need to know how bad the old one is. For that they need the expectation of the number of bumpings by _t_ time units after the running has begun. Help the company count the required expectation. Note that each runner chooses a direction equiprobably, independently from the others and then all runners start running simultaneously at 7 a.m. Assume that each runner runs for _t_ time units without stopping. Consider the runners to bump at a certain moment if at that moment they found themselves at the same point in the stadium. A pair of runners can bump more than once. Input The first line of the input contains three integers _n_, _l_, _t_ (1u2009≀u2009_n_u2009≀u2009106,u20091u2009≀u2009_l_u2009≀u2009109,u20091u2009≀u2009_t_u2009≀u2009109). The next line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≀u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_u2009<u2009_l_), here _a__i_ is the clockwise distance from the start line to the _i_-th runner's starting position. Note There are two runners in the first example. If the first runner run clockwise direction, then in 1 time unit he will be 1m away from the start line. If the second runner run counter-clockwise direction then in 1 time unit he will be also 1m away from the start line. And it is the only possible way to meet. We assume that each running direction is equiprobable, so the answer for the example is equal to 0.5Β·0.5u2009=u20090.25.
2,000
true
false
false
false
false
false
false
true
false
false
8,607
1870E
You are given an array of integers $$$a$$$ of size $$$n$$$. You can choose a set of non-overlapping subarrays of the given array (note that some elements may be not included in any subarray, this is allowed). For each selected subarray, calculate the MEX of its elements, and then calculate the of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of $$$[2,2,1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3,1,0,1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0,3,1,2]$$$ is $$$4$$$, because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the array, but $$$4$$$ does not. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 5000$$$) β€” the number of test cases. This is followed by the description of the test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 5000$$$) β€” the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq n$$$) β€” the array $$$a$$$. It is guaranteed that the sum of all $$$n$$$ values across all test cases does not exceed $$$5000$$$. Note In the first test case, the maximum XOR is $$$2$$$ if we take the entire array, $$$operatorname{MEX}([1, 0]) = 2$$$. In the second test case, the maximum XOR is $$$6$$$ if we partition the array into segments $$$[1, 2, 0]$$$ and $$$[7, 1, 2, 0, 2, 4, 3]$$$: $$$operatorname{MEX}([1, 2, 0]) = 3$$$, $$$operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5$$$, therefore, the XOR is $$$5 oplus 3=6$$$. In the third test case, the maximum XOR is $$$7$$$ if we partition the array into segments $$$[1, 0]$$$ and $$$[7, 1, 2, 0, 2, 4, 3]$$$: $$$operatorname{MEX}([1, 0]) = 2$$$, $$$operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5$$$, therefore, the XOR is $$$5 oplus 2 = 7$$$.
2,300
false
false
false
true
false
false
true
false
false
false
1,055
185A
Problem - 185A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math *1300 No tag edit access β†’ Contest materials β€” the number of full years when the plant grew. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. Output Print a single integer β€” the remainder of dividing the number of plants that will point "upwards" in _n_ years by 1000000007 (109u2009+u20097). Examples Input 1 Output 3 Input 2 Output 10 Note The first test sample corresponds to the second triangle on the figure in the statement. The second test sample corresponds to the third one.
1,300
true
false
false
false
false
false
false
false
false
false
9,104
855E
Harry came to know from Dumbledore that Salazar Slytherin's locket is a horcrux. This locket was present earlier at 12 Grimmauld Place, the home of Sirius Black's mother. It was stolen from there and is now present in the Ministry of Magic in the office of Dolorous Umbridge, Harry's former Defense Against the Dark Arts teacher. Harry, Ron and Hermione are infiltrating the Ministry. Upon reaching Umbridge's office, they observed a code lock with a puzzle asking them to calculate count of magic numbers between two integers _l_ and _r_ (both inclusive). Harry remembered from his detention time with Umbridge that she defined a magic number as a number which when converted to a given base _b_, all the digits from 0 to _b_u2009-u20091 appear even number of times in its representation without any leading zeros. You have to answer _q_ queries to unlock the office. Each query has three integers _b__i_, _l__i_ and _r__i_, the base and the range for which you have to find the count of magic numbers. Input First line of input contains _q_ (1u2009≀u2009_q_u2009≀u2009105)xa0β€” number of queries. Each of the next _q_ lines contain three space separated integers _b__i_, _l__i_, _r__i_ (2u2009≀u2009_b__i_u2009≀u200910, 1u2009≀u2009_l__i_u2009≀u2009_r__i_u2009≀u20091018). Output You have to output _q_ lines, each containing a single integer, the answer to the corresponding query. Note In sample test case 1, for first query, when we convert numbers 4 to 9 into base 2, we get: 4u2009=u20091002, 5u2009=u20091012, 6u2009=u20091102, 7u2009=u20091112, 8u2009=u200910002, 9u2009=u200910012. Out of these, only base 2 representation of 9 has even number of 1 and 0. Thus, the answer is 1.
2,200
false
false
false
true
false
false
false
false
false
false
6,323
1551C
Stephen Queen wants to write a story. He is a very unusual writer, he uses only letters 'a', 'b', 'c', 'd' and 'e'! To compose a story, Stephen wrote out $$$n$$$ words consisting of the first $$$5$$$ lowercase letters of the Latin alphabet. He wants to select the maximum number of words to make an interesting story. Let a story be a sequence of words that are not necessarily different. A story is called interesting if there exists a letter which occurs among all words of the story more times than all other letters together. For example, the story consisting of three words "bac", "aaada", "e" is interesting (the letter 'a' occurs $$$5$$$ times, all other letters occur $$$4$$$ times in total). But the story consisting of two words "aba", "abcde" is not (no such letter that it occurs more than all other letters in total). You are given a sequence of $$$n$$$ words consisting of letters 'a', 'b', 'c', 'd' and 'e'. Your task is to choose the maximum number of them to make an interesting story. If there's no way to make a non-empty story, output $$$0$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 5000$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the number of the words in the sequence. Then $$$n$$$ lines follow, each of them contains a word β€” a non-empty string consisting of lowercase letters of the Latin alphabet. The words in the sequence may be non-distinct (i.u2009e. duplicates are allowed). Only the letters 'a', 'b', 'c', 'd' and 'e' may occur in the words. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$; the sum of the lengths of all words over all test cases doesn't exceed $$$4 cdot 10^5$$$. Output For each test case, output the maximum number of words that compose an interesting story. Print 0 if there's no way to make a non-empty interesting story. Example Input 6 3 bac aaada e 3 aba abcde aba 2 baba baba 4 ab ab c bc 5 cbdca d a d e 3 b c ca Note In the first test case of the example, all $$$3$$$ words can be used to make an interesting story. The interesting story is "bac aaada e". In the second test case of the example, the $$$1$$$-st and the $$$3$$$-rd words can be used to make an interesting story. The interesting story is "aba aba". Stephen can't use all three words at the same time. In the third test case of the example, Stephen can't make a non-empty interesting story. So the answer is $$$0$$$. In the fourth test case of the example, Stephen can use the $$$3$$$-rd and the $$$4$$$-th words to make an interesting story. The interesting story is "c bc".
1,500
false
true
false
false
false
false
false
false
true
false
2,889
135D
Little Petya very much likes rectangular tables that consist of characters "0" and "1". Recently he has received one such table as a gift from his mother. The table contained _n_ rows and _m_ columns. The rows are numbered from top to bottom from 1 to _n_, the columns are numbered from the left to the right from 1 to _m_. Petya immediately decided to find the longest cool cycle whatever it takes. A cycle is a sequence of pairwise distinct cells where each two consecutive cells have a common side; besides, the first cell has a common side with the last cell. A cycle is called cool if it fulfills all the following conditions simultaneously: The cycle entirely consists of the cells that contain "1". Each cell that belongs to the cycle, has a common side with exactly two other cells that belong to the cycle. Each cell of the table that contains "1" either belongs to the cycle or is positioned outside of it (see definition below). To define the notion of "outside" formally, let's draw a cycle on a plane. Let each cell of the cycle (_i_,u2009_j_) (_i_ is the row number, _j_ is the column number) correspond to the point (_i_,u2009_j_) on the coordinate plane. Let a straight line segment join each pair of points that correspond to the cells belonging to the cycle and sharing a side. Thus, we will get a closed polyline that has no self-intersections and self-touches. The polyline divides the plane into two connected parts: the part of an infinite area and the part of a finite area. It is considered that cell (_r_,u2009_c_) lies outside of the cycle if it does not belong to the cycle and the corresponding point on the plane with coordinates (_r_,u2009_c_) lies in the part with the infinite area. Help Petya to find the length of the longest cool cycle in the table. The cycle length is defined as the number of cells that belong to the cycle. Input The first line contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u20091000) β€” the number of rows and columns in the table, respectively. Each of the following _n_ lines contains _m_ characters. Each character can be either "0" or "1". Output Print a single number β€” the length of the longest cool cycle in the table. If such cycles do not exist, print 0. Examples Input 5 5 01010 10101 01010 10101 01010 Input 7 7 1111111 1000101 1000101 1000101 1000111 1000001 1111111 Input 5 5 11111 10001 10101 10001 11111 Note In the first example there's only one cycle and it is cool. In the second sample there's no cycle at all. In the third sample there are two cool cycles: their lengths are 12 and 24. In the fourth sample there also is only one cycle but it isn't cool as there's a cell containing "1" inside this cycle.
2,500
false
false
true
false
false
false
true
false
false
false
9,334
1822E
You are given a string $$$s$$$, consisting of lowercase English letters. In one operation, you are allowed to swap any two characters of the string $$$s$$$. A string $$$s$$$ of length $$$n$$$ is called an anti-palindrome, if $$$s[i] e s[n - i + 1]$$$ for every $$$i$$$ ($$$1 le i le n$$$). For example, the strings "codeforces", "string" are anti-palindromes, but the strings "abacaba", "abc", "test" are not. Determine the minimum number of operations required to make the string $$$s$$$ an anti-palindrome, or output $$$-1$$$, if this is not possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of the test cases follows. Each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the length of the string $$$s$$$. The second line contains the string $$$s$$$, consisting of $$$n$$$ lowercase English letters. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β€” the minimum number of operations required to make the string $$$s$$$ an anti-palindrome, or $$$-1$$$ if this is not possible. Example Input 10 10 codeforces 3 abc 10 taarrrataa 10 dcbdbdcccc 4 wwww 12 cabbaccabaac 10 aadaaaaddc 14 aacdaaaacadcdc 6 abccba 12 dcbcaebacccd Output 0 -1 1 1 -1 3 -1 2 2 2 Note In the first test case, the string "codeforces" is already an anti-palindrome, so the answer is $$$0$$$. In the second test case, it can be shown that the string "abc" cannot be transformed into an anti-palindrome by performing the allowed operations, so the answer is $$$-1$$$. In the third test case, it is enough to swap the second and the fifth characters of the string "taarrrataa", and the new string "trararataa" will be an anti-palindrome, so the answer is $$$1$$$.
1,600
true
true
false
false
false
false
false
false
false
false
1,347
1608C
$$$n$$$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $$$n-1$$$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) β€” the number of players. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$, $$$a_i eq a_j$$$ for $$$i eq j$$$), where $$$a_i$$$ is the strength of the $$$i$$$-th player on the first map. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 leq b_i leq 10^9$$$, $$$b_i eq b_j$$$ for $$$i eq j$$$), where $$$b_i$$$ is the strength of the $$$i$$$-th player on the second map. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print a string of length $$$n$$$. $$$i$$$-th character should be "1" if the $$$i$$$-th player can win the tournament, or "0" otherwise. Example Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Note In the first test case, the $$$4$$$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
1,700
false
true
false
true
true
false
false
false
false
true
2,613
1181B
Dima worked all day and wrote down on a long paper strip his favorite number $$$n$$$ consisting of $$$l$$$ digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf. To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip. Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain. Input The first line contains a single integer $$$l$$$ ($$$2 le l le 100,000$$$)xa0β€” the length of the Dima's favorite number. The second line contains the positive integer $$$n$$$ initially written on the strip: the Dima's favorite number. The integer $$$n$$$ consists of exactly $$$l$$$ digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip. Output Print a single integerxa0β€” the smallest number Dima can obtain. Note In the first example Dima can split the number $$$1234567$$$ into integers $$$1234$$$ and $$$567$$$. Their sum is $$$1801$$$. In the second example Dima can split the number $$$101$$$ into integers $$$10$$$ and $$$1$$$. Their sum is $$$11$$$. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.
1,500
false
true
true
false
false
false
false
false
false
false
4,813
1997A
Monocarp's current password on Codeforces is a string $$$s$$$, consisting of lowercase Latin letters. Monocarp thinks that his current password is too weak, so he wants to insert exactly one lowercase Latin letter into the password to make it stronger. Monocarp can choose any letter and insert it anywhere, even before the first character or after the last character. Monocarp thinks that the password's strength is proportional to the time it takes him to type the password. The time it takes to type the password is calculated as follows: the time to type the first character is $$$2$$$ seconds; for each character other than the first, the time it takes to type it is $$$1$$$ second if it is the same as the previous character, or $$$2$$$ seconds otherwise. For example, the time it takes to type the password abacaba is $$$14$$$; the time it takes to type the password a is $$$2$$$; the time it takes to type the password aaabacc is $$$11$$$. You have to help Monocarp β€” insert a lowercase Latin letter into his password so that the resulting password takes the maximum possible amount of time to type. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) β€” the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 le s le 10$$$), consisting of lowercase Latin letters. Output For each test case, print one line containing the new password β€” a string which can be obtained from $$$s$$$ by inserting one lowercase Latin letter. The string you print should have the maximum possible required time to type it. If there are multiple answers, print any of them. Example Output wa aada abcb pastsword
800
false
false
true
false
false
false
true
false
false
false
283
1372F
# Omkar and Modes Input file: standard input Output file: standard output Time limit: 3 seconds Memory limit: 256 megabytes Ray lost his array and needs to find it by asking Omkar. Omkar is willing to disclose that the array has the following qualities: 1. The array has n (1 ≀ n ≀ 2 Β· 10 5) elements. 2. Every element in the array ai is an integer in the range 1 ≀ ai ≀ 10 9. 3. The array is sorted in nondecreasing order. Ray is allowed to send Omkar a series of queries. A query consists of two integers, l and r such that 1 ≀ l ≀ r ≀ n. Omkar will respond with two integers, x and f . x is the mode of the subarray from index l to index r inclusive. The mode of an array is defined by the number that appears the most frequently. If there are multiple numbers that appear the most number of times, the smallest such number is considered to be the mode. f is the amount of times that x appears in the queried subarray. The array has k (1 ≀ k ≀ min(25000 , n )) distinct elements. However, due to Ray’s sins, Omkar will not tell Ray what k is. Ray is allowed to send at most 4k queries. Help Ray find his lost array. # Input The only line of the input contains a single integer n (1 ≀ n ≀ 2 Β· 10 5), which equals to the length of the array that you are trying to find. # Interaction Protocol The interaction starts with reading n.Then you can make one type of query: β€’ β€œ? l r ” ( 1 ≀ l ≀ r ≀ n) where l and r are the bounds of the subarray that you wish to query. The answer to each query will be in the form β€œ x f ” where x is the mode of the subarray and f is the number of times x appears in the subarray. β€’ x satisfies ( 1 ≀ x ≀ 10 9). β€’ f satisfies ( 1 ≀ f ≀ r βˆ’ l + 1 ). β€’ If you make more than 4k queries or violate the number range in the query, you will get an output β€œ-1 .” β€’ If you terminate after receiving the response β€œ βˆ’1”, you will get the β€œ Wrong answer ” verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. To output your answer, print: β€’ β€œ! a1 a2 . . . anβˆ’1 an” which is an exclamation point followed by the array with a space between every element. Page 1 of 2 And quit after that. This query is not counted towards the 4k queries limit. 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 To hack, output 1 integer on the first line, n (1 ≀ n ≀ 2 Β· 10 5). On the second line output n integers a1, a 2, . . . , a nβˆ’1, a n separated by a space such that there are at most 25000 distinct numbers and aj ≀ aj+1 for all j from 1 to n βˆ’ 1. # Example standard input standard output 62 2 2 2 3 2 2 1 ? 1 6 ? 1 3 ? 4 6 ? 3 4 ! 1 2 2 3 3 4 # Note The first query is l = 1 and r = 6 . The mode is 2, and 2 appears 2 times, so x = 2 and f = 2 . Note that 3 also appears two times, but 2 is outputted because 2 is smaller. The second query is l = 1 and r = 3 . The mode is 2 and 2 appears twice in the subarray with indices [1 , 3] .The third query is l = 4 and r = 6 . The mode is 3 and 3 appears twice in the subarray with indices [4 , 6] .The fourth query is l = 3 and r = 4 . The mode is 2, which appears once in the subarray with indices [3 , 4] . Note that 3 also appears once in that range, but 2 is smaller than 3. Page 2 of 2
2,700
false
false
false
false
false
false
false
true
false
false
3,802
1929D
Sasha wants to take a walk with his girlfriend in the city. The city consists of $$$n$$$ intersections, numbered from $$$1$$$ to $$$n$$$. Some of them are connected by roads, and from any intersection, there is exactly one simple path$$$^{dagger}$$$ to any other intersection. In other words, the intersections and the roads between them form a tree. Some of the intersections are considered dangerous. Since it is unsafe to walk alone in the city, Sasha does not want to visit three or more dangerous intersections during the walk. Sasha calls a set of intersections good if the following condition is satisfied: If in the city only the intersections contained in this set are dangerous, then any simple path in the city contains no more than two dangerous intersections. However, Sasha does not know which intersections are dangerous, so he is interested in the number of different good sets of intersections in the city. Since this number can be very large, output it modulo $$$998,244,353$$$. $$$^{dagger}$$$A simple path is a path that passes through each intersection at most once. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n leq 3 cdot 10^5$$$) β€” the number of intersections in the city. The next $$$(n - 1)$$$ lines describe the roads. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i e v_i$$$) β€” the numbers of the intersections connected by the $$$i$$$-th road. It is guaranteed that these roads form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integer β€” the number of good sets of intersections modulo $$$998,244,353$$$. Example Input 4 3 1 3 3 2 4 3 4 2 3 3 1 5 1 2 3 4 5 1 2 3 4 1 2 2 3 3 4 Note In the first test case, there are $$$2^3 = 8$$$ sets of intersections. All of them are good, except for the set $$${1, 2, 3}$$$, because if intersections $$$1, 2$$$, and $$$3$$$ are dangerous, then the simple path $$$1 - 2 - 3$$$ contains $$$3$$$ dangerous intersections. Thus, there are $$$7$$$ good sets. In the second test case, there are $$$2^4 = 16$$$ sets of intersections. Among them, the sets $$${1, 2, 3, 4}$$$, $$${1, 2, 3}$$$, $$${1, 3, 4}$$$, $$${2, 3, 4}$$$ are not good. Thus, there are a total of $$$12$$$ good sets. The city layout is shown below:
1,900
true
false
false
true
false
false
false
false
false
false
714
1200C
Amugae is in a very large round corridor. The corridor consists of two areas. The inner area is equally divided by $$$n$$$ sectors, and the outer area is equally divided by $$$m$$$ sectors. A wall exists between each pair of sectors of same area (inner or outer), but there is no wall between the inner area and the outer area. A wall always exists at the 12 o'clock position. The inner area's sectors are denoted as $$$(1,1), (1,2), dots, (1,n)$$$ in clockwise direction. The outer area's sectors are denoted as $$$(2,1), (2,2), dots, (2,m)$$$ in the same manner. For a clear understanding, see the example image above. Amugae wants to know if he can move from one sector to another sector. He has $$$q$$$ questions. For each question, check if he can move between two given sectors. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 le n, m le 10^{18}$$$, $$$1 le q le 10^4$$$)xa0β€” the number of sectors in the inner area, the number of sectors in the outer area and the number of questions. Each of the next $$$q$$$ lines contains four integers $$$s_x$$$, $$$s_y$$$, $$$e_x$$$, $$$e_y$$$ ($$$1 le s_x, e_x le 2$$$; if $$$s_x = 1$$$, then $$$1 le s_y le n$$$, otherwise $$$1 le s_y le m$$$; constraints on $$$e_y$$$ are similar). Amague wants to know if it is possible to move from sector $$$(s_x, s_y)$$$ to sector $$$(e_x, e_y)$$$. Output For each question, print "YES" if Amugae can move from $$$(s_x, s_y)$$$ to $$$(e_x, e_y)$$$, and "NO" otherwise. You can print each letter in any case (upper or lower). Example Input 4 6 3 1 1 2 3 2 6 1 2 2 6 2 4 Note Example is shown on the picture in the statement.
1,400
true
false
false
false
false
false
false
false
false
false
4,699
1287B
Bees Alice and Alesya gave beekeeper Polina famous card game "Set" as a Christmas present. The deck consists of cards that vary in four features across three options for each kind of feature: number of shapes, shape, shading, and color. In this game, some combinations of three cards are said to make up a set. For every feature β€” color, number, shape, and shading β€” the three cards must display that feature as either all the same, or pairwise different. The picture below shows how sets look. Polina came up with a new game called "Hyperset". In her game, there are $$$n$$$ cards with $$$k$$$ features, each feature has three possible values: "S", "E", or "T". The original "Set" game can be viewed as "Hyperset" with $$$k = 4$$$. Similarly to the original game, three cards form a set, if all features are the same for all cards or are pairwise different. The goal of the game is to compute the number of ways to choose three cards that form a set. Unfortunately, winter holidays have come to an end, and it's time for Polina to go to school. Help Polina find the number of sets among the cards lying on the table. Input The first line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 1500$$$, $$$1 le k le 30$$$)xa0β€” number of cards and number of features. Each of the following $$$n$$$ lines contains a card description: a string consisting of $$$k$$$ letters "S", "E", "T". The $$$i$$$-th character of this string decribes the $$$i$$$-th feature of that card. All cards are distinct.
1,500
false
false
true
false
true
false
true
false
false
false
4,253
232A
Problem - 232A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search constructive algorithms graphs greedy *1600 No tag edit access β†’ Contest materials β€” the number of cycles of length 3 in the required graph. Output In the first line print integer _n_ (3u2009≀u2009_n_u2009≀u2009100) β€” the number of vertices in the found graph. In each of next _n_ lines print _n_ characters "0" and "1": the _i_-th character of the _j_-th line should equal "0", if vertices _i_ and _j_ do not have an edge between them, otherwise it should equal "1". Note that as the required graph is undirected, the _i_-th character of the _j_-th line must equal the _j_-th character of the _i_-th line. The graph shouldn't contain self-loops, so the _i_-th character of the _i_-th line must equal "0" for all _i_. Examples Input 1 Output 3 011 101 110 Input 10 Output 5 01111 10111 11011 11101 11110
1,600
false
true
false
false
false
true
false
true
false
true
8,911
1370C
# Number Game Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Ashishgup and FastestFinger play a game. They start with a number n and play in turns. In each turn, a player can make any one of the following moves: β€’ Divide n by any of its odd divisors greater than 1. β€’ Subtract 1 from n if n is greater than 1.Divisors of a number include the number itself. The player who is unable to make a move loses the game. Ashishgup moves first. Determine the winner of the game if both of them play optimally. # Input The first line contains a single integer t (1 ≀ t ≀ 100 ) x16 the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer x16 n (1 ≀ n ≀ 10 9). # Output For each test case, print β€œ Ashishgup ” if he wins, and β€œ FastestFinger ” otherwise (without quotes). # Example standard input standard output 712345612 FastestFinger Ashishgup Ashishgup FastestFinger Ashishgup FastestFinger Ashishgup # Note In the first test case, n = 1 , Ashishgup cannot make a move. He loses. In the second test case, n = 2 , Ashishgup subtracts 1 on the first move. Now n = 1 , FastestFinger cannot make a move, so he loses. In the third test case, n = 3 , Ashishgup divides by 3 on the first move. Now n = 1 , FastestFinger cannot make a move, so he loses. In the last test case, n = 12 , Ashishgup divides it by 3. Now n = 4 , FastestFinger is forced to subtract 1,and Ashishgup gets 3, so he wins by dividing it by 3. Page 1 of 1
1,400
true
false
false
false
false
false
false
false
false
false
3,819
1615D
Twas the night before Christmas, and Santa's frantically setting up his new Christmas tree! There are $$$n$$$ nodes in the tree, connected by $$$n-1$$$ edges. On each edge of the tree, there's a set of Christmas lights, which can be represented by an integer in binary representation. He has $$$m$$$ elves come over and admire his tree. Each elf is assigned two nodes, $$$a$$$ and $$$b$$$, and that elf looks at all lights on the simple path between the two nodes. After this, the elf's favorite number becomes the $$$, as well as the parity of the number of set bits in his favorite number. In other words, he remembers whether the number of $$$1$$$'s when his favorite number is written in binary is odd or even. Help Santa determine if it's possible that the memories are consistent, and if it is, remember what his tree looked like, and maybe you'll go down in history! Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$)xa0β€” the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains two integers, $$$n$$$ and $$$m$$$ ($$$2 leq n leq 2 cdot 10^5$$$; $$$1 leq m leq 2 cdot 10^5$$$)xa0β€” the size of tree and the number of elves respectively. The next $$$n-1$$$ lines of each test case each contains three integers, $$$x$$$, $$$y$$$, and $$$v$$$ ($$$1 leq x, y leq n$$$; $$$-1 leq v < 2^{30}$$$)xa0β€” meaning that there's an edge between nodes $$$x$$$ and $$$y$$$. If $$$v = -1$$$: Santa doesn't remember what the set of lights were on for this edge. $$$v geq 0$$$: The set of lights on the edge is $$$v$$$. The next $$$m$$$ lines of each test case each contains three integers, $$$a$$$, $$$b$$$, and $$$p$$$ ($$$1 leq a, b leq n$$$; $$$a eq b$$$; $$$0 leq p leq 1$$$)xa0β€” the nodes that the elf was assigned to, and the parity of the number of set bits in the elf's favorite number. It is guaranteed that the sum of all $$$n$$$ and the sum of all $$$m$$$ don't exceed $$$2 cdot 10^5$$$ each. It is guaranteed that the given edges form a tree. Output For each test case, first print either YES or NO (in any case), whether there's a tree consistent with Santa's memory or not. If the answer is YES, print $$$n-1$$$ lines each containing three integers: $$$x$$$, $$$y$$$, and $$$v$$$ ($$$1 le x, y le n$$$; $$$0 le v < 2^{30}$$$)xa0β€” the edge and the integer on that edge. The set of edges must be the same as in the input, and if the value of some edge was specified earlier, it can not change. You can print the edges in any order. If there are multiple answers, print any. Example Input 4 6 5 1 2 -1 1 3 1 4 2 7 6 3 0 2 5 -1 2 3 1 2 5 0 5 6 1 6 1 1 4 5 1 5 3 1 2 -1 1 3 -1 1 4 1 4 5 -1 2 4 0 3 4 1 2 3 1 3 3 1 2 -1 1 3 -1 1 2 0 1 3 1 2 3 0 2 1 1 2 1 1 2 0 Output YES 1 2 0 1 3 1 2 4 7 3 6 0 2 5 0 YES 1 2 1 1 3 0 1 4 1 4 5 1 NO NO Note The first test case is the image in the statement. One possible answer is assigning the value of the edge $$$(1, 2)$$$ to $$$5$$$, and the value of the edge $$$(2, 5)$$$ to $$$3$$$. This is correct because: The first elf goes from node $$$2$$$ to node $$$3$$$. This elf's favorite number is $$$4$$$, so he remembers the value $$$1$$$ (as $$$4$$$ has an odd number of $$$1$$$ bits in its binary representation). The second elf goes from node $$$2$$$ to node $$$5$$$. This elf's favorite number is $$$3$$$, so he remembers the value $$$0$$$ (as $$$3$$$ has an even number of $$$1$$$ bits in its binary representation). The third elf goes from node $$$5$$$ to node $$$6$$$. This elf's favorite number is $$$7$$$, so he remembers the value $$$1$$$ (as $$$7$$$ has an odd number of $$$1$$$ bits in its binary representation). The fourth elf goes from node $$$6$$$ to node $$$1$$$. This elf's favorite number is $$$1$$$, so he remembers the value $$$1$$$ (as $$$1$$$ has an odd number of $$$1$$$ bits in its binary representation). The fifth elf goes from node $$$4$$$ to node $$$5$$$. This elf's favorite number is $$$4$$$, so he remembers the number $$$1$$$ (as $$$4$$$ has an odd number of $$$1$$$ bits in its binary representation). Note that there are other possible answers.
2,200
false
false
false
false
false
false
false
false
false
true
2,561
441D
A permutation _p_ of length _n_ is a sequence of distinct integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≀u2009_p__i_u2009≀u2009_n_). A permutation is an identity permutation, if for any _i_ the following equation holds _p__i_u2009=u2009_i_. A swap (_i_,u2009_j_) is the operation that swaps elements _p__i_ and _p__j_ in the permutation. Let's assume that _f_(_p_) is the minimum number of swaps that you need to make the permutation _p_ an identity permutation. Valera wonders, how he can transform permutation _p_ into any permutation _q_, such that _f_(_q_)u2009=u2009_m_, using the minimum number of swaps. Help him do that. Input The first line contains integer _n_ (1u2009≀u2009_n_u2009≀u20093000) β€” the length of permutation _p_. The second line contains _n_ distinct integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≀u2009_p__i_u2009≀u2009_n_) β€” Valera's initial permutation. The last line contains integer _m_ (0u2009≀u2009_m_u2009<u2009_n_). Output In the first line, print integer _k_ β€” the minimum number of swaps. In the second line, print 2_k_ integers _x_1,u2009_x_2,u2009...,u2009_x_2_k_ β€” the description of the swap sequence. The printed numbers show that you need to consecutively make swaps (_x_1,u2009_x_2), (_x_3,u2009_x_4), ..., (_x_2_k_u2009-u20091,u2009_x_2_k_). If there are multiple sequence swaps of the minimum length, print the lexicographically minimum one. Note Sequence _x_1,u2009_x_2,u2009...,u2009_x__s_ is lexicographically smaller than sequence _y_1,u2009_y_2,u2009...,u2009_y__s_, if there is such integer _r_ (1u2009≀u2009_r_u2009≀u2009_s_), that _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009...,u2009_x__r_u2009-u20091u2009=u2009_y__r_u2009-u20091 and _x__r_u2009<u2009_y__r_.
2,100
true
false
true
false
false
true
false
false
false
true
8,072
1530D
Every December, VK traditionally holds an event for its employees named "Secret Santa". Here's how it happens. $$$n$$$ employees numbered from $$$1$$$ to $$$n$$$ take part in the event. Each employee $$$i$$$ is assigned a different employee $$$b_i$$$, to which employee $$$i$$$ has to make a new year gift. Each employee is assigned to exactly one other employee, and nobody is assigned to themselves (but two employees may be assigned to each other). Formally, all $$$b_i$$$ must be distinct integers between $$$1$$$ and $$$n$$$, and for any $$$i$$$, $$$b_i e i$$$ must hold. The assignment is usually generated randomly. This year, as an experiment, all event participants have been asked who they wish to make a gift to. Each employee $$$i$$$ has said that they wish to make a gift to employee $$$a_i$$$. Find a valid assignment $$$b$$$ that maximizes the number of fulfilled wishes of the employees. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. Each test case consists of two lines. The first line contains a single integer $$$n$$$xa0($$$2 le n le 2 cdot 10^5$$$)xa0β€” the number of participants of the event. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0($$$1 le a_i le n$$$; $$$a_i e i$$$)xa0β€” wishes of the employees in order from $$$1$$$ to $$$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 two lines. In the first line, print a single integer $$$k$$$xa0($$$0 le k le n$$$)xa0β€” the number of fulfilled wishes in your assignment. In the second line, print $$$n$$$ distinct integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$; $$$b_i e i$$$)xa0β€” the numbers of employees assigned to employees $$$1, 2, ldots, n$$$. $$$k$$$ must be equal to the number of values of $$$i$$$ such that $$$a_i = b_i$$$, and must be as large as possible. If there are multiple answers, print any. Example Input 2 3 2 1 2 7 6 4 6 2 4 5 6 Output 2 3 1 2 4 6 4 7 2 3 5 1 Note In the first test case, two valid assignments exist: $$$[3, 1, 2]$$$ and $$$[2, 3, 1]$$$. The former assignment fulfills two wishes, while the latter assignment fulfills only one. Therefore, $$$k = 2$$$, and the only correct answer is $$$[3, 1, 2]$$$.
1,600
true
true
false
false
false
true
false
false
false
true
3,012
1974F
Alice and Bob were playing a game again. They have a grid of size $$$a imes b$$$ ($$$1 le a, b le 10^9$$$), on which there are $$$n$$$ chips, with at most one chip in each cell. The cell at the intersection of the $$$x$$$-th row and the $$$y$$$-th column has coordinates $$$(x, y)$$$. Alice made the first move, and the players took turns. On each move, a player could cut several (but not all) rows or columns from the beginning or end of the remaining grid and earn a point for each chip that was on the cut part of the grid. Each move can be described by the character 'U', 'D', 'L', or 'R' and an integer $$$k$$$: If the character is 'U', then the first $$$k$$$ remaining rows will be cut; If the character is 'D', then the last $$$k$$$ remaining rows will be cut; If the character is 'L', then the first $$$k$$$ remaining columns will be cut; If the character is 'R', then the last $$$k$$$ remaining columns will be cut. Based on the initial state of the grid and the players' moves, determine the number of points earned by Alice and Bob, respectively. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains four integers $$$a$$$, $$$b$$$, $$$n$$$, and $$$m$$$ ($$$2 le a, b le 10^9$$$, $$$1 le n, m le 2 cdot 10^5$$$)xa0β€” the dimensions of the grid, the number of chips, and the number of moves. Each of the next $$$n$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le a$$$, $$$1 le y_i le b$$$)xa0β€” the coordinates of the chips. All pairs of coordinates are distinct. Each of the next $$$m$$$ lines contain a character $$$c_j$$$ and an integer $$$k_j$$$xa0β€” the description of the $$$j$$$-th move. It is guaranteed that $$$k$$$ is less than the number of rows/columns in the current grid. In other words, a player cannot cut the entire remaining grid on their move. It is guaranteed that the sum of the values of $$$n$$$ across all test cases in the test does not exceed $$$2 cdot 10^5$$$. It is guaranteed that the sum of the values of $$$m$$$ across all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two integersxa0β€” the number of points earned by Alice and Bob, respectively. Example Input 6 4 4 3 2 4 1 3 3 2 4 D 2 R 1 4 4 3 3 4 1 3 2 2 3 D 1 L 1 U 2 3 5 3 2 1 3 2 2 3 3 R 2 R 2 6 4 4 2 1 4 2 3 5 3 1 1 R 1 U 1 9 3 2 1 6 1 3 3 D 8 10 10 2 5 7 5 9 1 R 1 L 2 D 1 U 4 D 1 Output 2 1 2 0 0 3 1 1 2 0 0 1 Note Below is the game from the first example: On her turn, Alice cut $$$2$$$ rows from the bottom and scored $$$2$$$ points, then Bob cut $$$1$$$ column from the right and scored one point. Note that if Bob had cut $$$1$$$ row from the bottom, he would have also scored $$$1$$$ point.
1,900
false
false
true
false
true
false
true
true
true
false
444
1942G
Bessie has recently started playing a famous card game. In the game, there is only one deck of cards, consisting of $$$a$$$ "draw $$$0$$$" cards, $$$b$$$ "draw $$$1$$$" cards, $$$c$$$ "draw $$$2$$$" cards, and $$$5$$$ special cards. At the start of the game, all cards are in the randomly shuffled deck. Bessie starts the game by drawing the top $$$5$$$ cards of the deck. She may then play "draw $$$x$$$" cards from the hand to draw the next $$$x$$$ cards from the top of the deck. Note that every card can only be played once, special cards cannot be played, and if Bessie uses a "draw $$$2$$$" card when there is only $$$1$$$ card remaining in the deck, then she simply draws that remaining card. Bessie wins if she draws all $$$5$$$ special cards. Since Bessie is not very good at math problems, she wants you to find the probability that she wins, given that the deck is shuffled randomly over all $$$(a + b + c + 5)!$$$ possible orderings. It can be shown that this answer can always be expressed as a fraction $$$frac{p}{q}$$$ where $$$p$$$ and $$$q$$$ are coprime integers. Output $$$p cdot q^{-1}$$$ modulo $$$998,244,353$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Each test case contains three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$0 le a, b, c le 2 cdot 10^5$$$)xa0– the number of draw $$$0$$$ cards, draw $$$1$$$ cards, and draw $$$2$$$ cards, respectively. It is guaranteed that the sum of $$$a$$$ over all test cases does not exceed $$$2 cdot 10^5$$$, the sum of $$$b$$$ over all test cases does not exceed $$$2 cdot 10^5$$$, and the sum of $$$c$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0β€” the probability that Bessie wins, modulo $$$998,244,353$$$. Example Input 4 1 1 1 0 0 0 5 3 7 3366 1434 1234 Output 903173463 1 35118742 398952013 Note In the first case, we have $$$1$$$ of each type of "draw" card and $$$5$$$ special cards. There are $$$30,720$$$ starting decks where Bessie will win by drawing the top $$$5$$$ cards and $$$40,320$$$ starting decks in total. Thus, the probability of Bessie winning is $$$frac{30,720}{40,320} = frac{16}{21}$$$. One example of a winning starting deck is, top to bottom, 1. "Special", 2. "Draw $$$1$$$", 3. "Special", 4. "Special", 5. "Draw $$$0$$$", 6. "Draw $$$2$$$", 7. "Special", 8. "Special". One example of a losing starting deck is: 1. "Special", 2. "Draw $$$1$$$", 3. "Special", 4. "Special", 5. "Draw $$$0$$$", 6. "Special", 7. "Special", 8. "Draw $$$2$$$".
2,800
true
false
false
true
false
false
false
false
false
false
625
1965B
You are given two integers $$$n$$$ and $$$k$$$. Find a sequence $$$a$$$ of non-negative integers of size at most $$$25$$$ such that the following conditions hold. There is no subsequence of $$$a$$$ with a sum of $$$k$$$. For all $$$1 le v le n$$$ where $$$v e k$$$, there is a subsequence of $$$a$$$ with a sum of $$$v$$$. A sequence $$$b$$$ is a subsequence of $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by the deletion of several (possibly, zero or all) elements, without changing the order of the remaining elements. For example, $$$[5, 2, 3]$$$ is a subsequence of $$$[1, 5, 7, 8, 2, 4, 3]$$$. It can be shown that under the given constraints, a solution always exists. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β€” the number of test cases. The description of the test cases follows. Each test case consists of a single line containing two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^6$$$, $$$1 le k le n$$$)xa0β€” the parameters described above. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^7$$$. Output The first line of output for each test case should contain a single integer $$$m$$$ ($$$1 le m le 25$$$)xa0β€” the size of your chosen sequence. The second line of output for each test case should contain $$$m$$$ integers $$$a_i$$$ ($$$0 le a_i le 10^9$$$)xa0β€” the elements of your chosen sequence. If there are multiple solutions, print any. Example Output 1 1 5 2 3 4 5 6 7 1 1 1 1 1 1 1 4 7 1 4 1 4 1 2 8 3 Note In the first example, we just need a subsequence that adds up to $$$1$$$, but not one that adds up to $$$2$$$. So the array $$$a=[1]$$$ suffices. In the second example, all elements are greater than $$$k=1$$$, so no subsequence adds up to $$$1$$$. Every other integer between $$$1$$$ and $$$n$$$ is present in the array, so there is a subsequence of size $$$1$$$ adding up to each of those numbers.
1,800
false
true
false
false
false
true
false
false
false
false
515
1003C
The heat during the last few days has been really intense. Scientists from all over the Berland study how the temperatures and weather change, and they claim that this summer is abnormally hot. But any scientific claim sounds a lot more reasonable if there are some numbers involved, so they have decided to actually calculate some value which would represent how high the temperatures are. Mathematicians of Berland State University came up with a special heat intensity value. This value is calculated as follows: Suppose we want to analyze the segment of $$$n$$$ consecutive days. We have measured the temperatures during these $$$n$$$ days; the temperature during $$$i$$$-th day equals $$$a_i$$$. We denote the average temperature of a segment of some consecutive days as the arithmetic mean of the temperature measures during this segment of days. So, if we want to analyze the average temperature from day $$$x$$$ to day $$$y$$$, we calculate it as $$$frac{sum limits_{i = x}^{y} a_i}{y - x + 1}$$$ (note that division is performed without any rounding). The heat intensity value is the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. For example, if analyzing the measures $$$[3, 4, 1, 2]$$$ and $$$k = 3$$$, we are interested in segments $$$[3, 4, 1]$$$, $$$[4, 1, 2]$$$ and $$$[3, 4, 1, 2]$$$ (we want to find the maximum value of average temperature over these segments). You have been hired by Berland State University to write a program that would compute the heat intensity value of a given period of days. Are you up to this task? Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 5000$$$) β€” the number of days in the given period, and the minimum number of days in a segment we consider when calculating heat intensity value, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 5000$$$) β€” the temperature measures during given $$$n$$$ days. Output Print one real number β€” the heat intensity value, i. e., the maximum of average temperatures over all segments of not less than $$$k$$$ consecutive days. Your answer will be considered correct if the following condition holds: $$$res - res_0 < 10^{-6}$$$, where $$$res$$$ is your answer, and $$$res_0$$$ is the answer given by the jury's solution.
1,300
true
false
true
false
false
false
true
false
false
false
5,672
252B
Little Petya likes arrays of integers a lot. Recently his mother has presented him one such array consisting of _n_ elements. Petya is now wondering whether he can swap any two distinct integers in the array so that the array got unsorted. Please note that Petya can not swap equal integers even if they are in distinct positions in the array. Also note that Petya must swap some two integers even if the original array meets all requirements. Array _a_ (the array elements are indexed from 1) consisting of _n_ elements is called sorted if it meets at least one of the following two conditions: 1. _a_1u2009≀u2009_a_2u2009≀u2009...u2009≀u2009_a__n_; 2. _a_1u2009β‰₯u2009_a_2u2009β‰₯u2009...u2009β‰₯u2009_a__n_. Help Petya find the two required positions to swap or else say that they do not exist. Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009105). The second line contains _n_ non-negative space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ β€” the elements of the array that Petya's mother presented him. All integers in the input do not exceed 109. Output If there is a pair of positions that make the array unsorted if swapped, then print the numbers of these positions separated by a space. If there are several pairs of positions, print any of them. If such pair does not exist, print -1. The positions in the array are numbered with integers from 1 to _n_. Note In the first two samples the required pairs obviously don't exist. In the third sample you can swap the first two elements. After that the array will look like this: 2 1 3 4. This array is unsorted.
1,800
false
false
false
false
false
false
true
false
true
false
8,825
949A
Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not. Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days. Input In the only line of input data there is a non-empty string _s_ consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as _s_) does not exceed 200u2009000 characters. Output If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer _k_ (1u2009≀u2009_k_u2009≀u2009_s_), the resulting number of subsequences. In the _i_-th of following _k_ lines first print the integer _l__i_ (1u2009≀u2009_l__i_u2009≀u2009_s_), which is the length of the _i_-th subsequence, and then _l__i_ indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to _n_ must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1. Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of _k_.
1,600
false
true
false
false
false
false
false
false
false
false
5,924
592E
BCPC stands for Byteforces Collegiate Programming Contest, and is the most famous competition in Byteforces. BCPC is a team competition. Each team is composed by a coach and three contestants. Blenda is the coach of the Bit State University(BSU), and she is very strict selecting the members of her team. In BSU there are _n_ students numbered from 1 to _n_. Since all BSU students are infinitely smart, the only important parameters for Blenda are their reading and writing speed. After a careful measuring, Blenda have found that the _i_-th student have a reading speed equal to _r__i_ (words per minute), and a writing speed of _w__i_ (symbols per minute). Since BSU students are very smart, the measured speeds are sometimes very big and Blenda have decided to subtract some constant value _c_ from all the values of reading speed and some value _d_ from all the values of writing speed. Therefore she considers _r__i_'u2009=u2009_r__i_u2009-u2009_c_ and _w__i_'u2009=u2009_w__i_u2009-u2009_d_. The student _i_ is said to overwhelm the student _j_ if and only if _r__i_'Β·_w__j_'u2009>u2009_r__j_'Β·_w__i_'. Blenda doesn’t like fights in teams, so she thinks that a team consisting of three distinct students _i_,u2009_j_ and _k_ is good if _i_ overwhelms _j_, _j_ overwhelms _k_, and _k_ overwhelms _i_. Yes, the relation of overwhelming is not transitive as it often happens in real life. Since Blenda is busy preparing a training camp in Codeforces, you are given a task to calculate the number of different good teams in BSU. Two teams are considered to be different if there is at least one student that is present in one team but is not present in the other. In other words, two teams are different if the sets of students that form these teams are different. Input In the first line of the input three integers _n_, _c_ and _d_ (3u2009≀u2009_n_u2009≀u2009345678,u20091u2009≀u2009_c_,u2009_d_u2009≀u2009109) are written. They denote the number of students Blenda can use to form teams, the value subtracted from all reading speeds and the value subtracted from all writing speeds respectively. Each of the next _n_ lines contains two integers _r__i_ and _w__i_ (0u2009<u2009_r__i_,u2009_w__i_u2009≀u2009109,u2009_r__i_u2009-u2009_c_u2009+u2009_w__i_u2009-u2009_d_u2009>u20090). There are no two students, such that both their reading and writing speeds coincide, i.e. for every _i_u2009β‰ u2009_j_ condition _r__i_u2009-u2009_r__j_u2009+u2009_w__i_u2009-u2009_w__j_u2009>u20090 holds. Output Print the number of different teams in BSU, that are good according to Blenda's definition. Examples Input 5 2 2 1 1 4 1 2 3 3 2 3 4 Input 7 6 6 3 2 1 7 5 7 3 7 6 4 8 9 8 5 Note In the first sample the following teams are good: (_i_u2009=u20091,u2009_j_u2009=u20092,u2009_k_u2009=u20093), (_i_u2009=u20092,u2009_j_u2009=u20095,u2009_k_u2009=u20091), (_i_u2009=u20091,u2009_j_u2009=u20094,u2009_k_u2009=u20093), (_i_u2009=u20095,u2009_j_u2009=u20091,u2009_k_u2009=u20094). Note, that for example the team (_i_u2009=u20093,u2009_j_u2009=u20091,u2009_k_u2009=u20092) is also good, but is considered to be the same as the team (_i_u2009=u20091,u2009_j_u2009=u20092,u2009_k_u2009=u20093).
2,700
false
false
false
false
false
false
false
true
false
false
7,478
720D
Little girl Masha likes winter sports, today she's planning to take part in slalom skiing. The track is represented as a grid composed of _n_u2009Γ—u2009_m_ squares. There are rectangular obstacles at the track, composed of grid squares. Masha must get from the square (1,u20091) to the square (_n_,u2009_m_). She can move from a square to adjacent square: either to the right, or upwards. If the square is occupied by an obstacle, it is not allowed to move to that square. One can see that each obstacle can actually be passed in two ways: either it is to the right of Masha's path, or to the left. Masha likes to try all ways to do things, so she would like to know how many ways are there to pass the track. Two ways are considered different if there is an obstacle such that it is to the right of the path in one way, and to the left of the path in the other way. Help Masha to find the number of ways to pass the track. The number of ways can be quite big, so Masha would like to know it modulo 109u2009+u20097. The pictures below show different ways to pass the track in sample tests. Input The first line of input data contains three positive integers: _n_, _m_ and _k_ (3u2009≀u2009_n_,u2009_m_u2009≀u2009106, 0u2009≀u2009_k_u2009≀u2009105)xa0β€” the size of the track and the number of obstacles. The following _k_ lines contain four positive integers each: _x_1, _y_1, _x_2, _y_2 (1u2009≀u2009_x_1u2009≀u2009_x_2u2009≀u2009_n_, 1u2009≀u2009_y_1u2009≀u2009_y_2u2009≀u2009_m_)xa0β€” coordinates of bottom left, and top right squares of the obstacle. It is guaranteed that there are no obstacles at squares (1,u20091) and (_n_,u2009_m_), and no obstacles overlap (but some of them may touch). Output Output one integerxa0β€” the number of ways to pass the track modulo 109u2009+u20097. Examples Input 5 5 3 2 2 2 3 4 2 5 2 4 4 4 4
3,100
false
false
false
true
true
false
false
false
true
false
6,934
719A
Every summer Vitya comes to visit his grandmother in the countryside. This summer, he got a huge wart. Every grandma knows that one should treat warts when the moon goes down. Thus, Vitya has to catch the moment when the moon is down. Moon cycle lasts 30 days. The size of the visible part of the moon (in Vitya's units) for each day is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, and then cycle repeats, thus after the second 1 again goes 0. As there is no internet in the countryside, Vitya has been watching the moon for _n_ consecutive days and for each of these days he wrote down the size of the visible part of the moon. Help him find out whether the moon will be up or down next day, or this cannot be determined by the data he has. Input The first line of the input contains a single integer _n_ (1u2009≀u2009_n_u2009≀u200992)xa0β€” the number of consecutive days Vitya was watching the size of the visible part of the moon. The second line contains _n_ integers _a__i_ (0u2009≀u2009_a__i_u2009≀u200915)xa0β€” Vitya's records. It's guaranteed that the input data is consistent. Output If Vitya can be sure that the size of visible part of the moon on day _n_u2009+u20091 will be less than the size of the visible part on day _n_, then print "DOWN" at the only line of the output. If he might be sure that the size of the visible part will increase, then print "UP". If it's impossible to determine what exactly will happen with the moon, print -1. Note In the first sample, the size of the moon on the next day will be equal to 8, thus the answer is "UP". In the second sample, the size of the moon on the next day will be 11, thus the answer is "DOWN". In the third sample, there is no way to determine whether the size of the moon on the next day will be 7 or 9, thus the answer is -1.
1,100
false
false
true
false
false
false
false
false
false
false
6,939
1954B
Let's call an array $$$a$$$ beautiful if you can make all its elements the same by using the following operation an arbitrary number of times (possibly, zero): choose an index $$$i$$$ ($$$2 le i le a - 1$$$) such that $$$a_{i - 1} = a_{i + 1}$$$, and replace $$$a_i$$$ with $$$a_{i - 1}$$$. You are given a beautiful array $$$a_1, a_2, dots, a_n$$$. What is the minimum number of elements you have to remove from it in order for it to stop being beautiful? Swapping elements is prohibited. If it is impossible to do so, then output -1. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). Additional constraints on the input: in every test case, the given array $$$a$$$ is beautiful; the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integerxa0β€” the minimum number of elements you have to remove from the array $$$a$$$ in order for it to stop being beautiful. If it is impossible, then output -1. Example Input 4 3 2 2 2 5 1 2 1 2 1 1 1 7 3 3 3 5 3 3 3 Note In the first testcase, it is impossible to modify the array in such a way that it stops being beautiful. An array consisting of identical numbers will remain beautiful no matter how many numbers we remove from it. In the second testcase, you can remove the number at the index $$$5$$$, for example. The resulting array will be $$$[1, 2, 1, 2]$$$. Let's check if it is beautiful. Two operations are available: Choose $$$i = 2$$$: the array becomes $$$[1, 1, 1, 2]$$$. No more operations can be applied to it, and the numbers are not all the same. Choose $$$i = 3$$$ instead: the array becomes $$$[1, 2, 2, 2]$$$. No more operations can be applied to it either, and the numbers are still not all the same. Thus, the array $$$[1, 2, 1, 2]$$$ is not beautiful. In the fourth testcase, you can remove the first three elements, for example. The resulting array $$$[5, 3, 3, 3]$$$ is not beautiful.
1,200
true
false
true
false
false
false
false
false
false
false
553
1730A
One day, Vogons wanted to build a new hyperspace highway through a distant system with $$$n$$$ planets. The $$$i$$$-th planet is on the orbit $$$a_i$$$, there could be multiple planets on the same orbit. It's a pity that all the planets are on the way and need to be destructed. Vogons have two machines to do that. The first machine in one operation can destroy any planet at cost of $$$1$$$ Triganic Pu. The second machine in one operation can destroy all planets on a single orbit in this system at the cost of $$$c$$$ Triganic Pus. Vogons can use each machine as many times as they want. Vogons are very greedy, so they want to destroy all planets with minimum amount of money spent. Can you help them to know the minimum cost of this project? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) β€” the number of test cases. Then the test cases follow. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$c$$$ ($$$1 le n, c le 100$$$) β€” the number of planets and the cost of the second machine usage. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 100$$$), where $$$a_i$$$ is the orbit of the $$$i$$$-th planet. Output For each test case print a single integer β€” the minimum cost of destroying all planets. Examples Input 4 10 1 2 1 4 5 2 4 5 5 1 2 5 2 3 2 1 2 2 2 2 1 1 2 2 1 2 Note In the first test case, the cost of using both machines is the same, so you can always use the second one and destroy all planets in orbit $$$1$$$, all planets in orbit $$$2$$$, all planets in orbit $$$4$$$, all planets in orbit $$$5$$$. In the second test case, it is advantageous to use the second machine for $$$2$$$ Triganic Pus to destroy all the planets in orbit $$$2$$$, then destroy the remaining two planets using the first machine. In the third test case, you can use the first machine twice or the second machine once. In the fourth test case, it is advantageous to use the first machine twice.
800
false
true
false
false
true
false
false
false
true
false
1,911
1090M
Problem - 1090M - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation *1000 No tag edit access β†’ Contest materials ") . The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0β€” the colors of the houses along the road ($$$1 le a_i le k$$$). Output Output a single integerxa0β€” the maximum number of houses on the road segment having no two adjacent houses of the same color. Example Input 8 3 1 2 3 3 2 1 2 2 Output 4 Note In the example, the longest segment without neighboring houses of the same color is from the house 4 to the house 7. The colors of the houses are $$$
1,000
false
false
true
false
false
false
false
false
false
false
5,260
1685D1
This is an easy version of the problem. The difference between the easy and hard versions is that in this version, you can output any permutation with the smallest weight. You are given a permutation $$$p_1, p_2, ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$. Let's define the weight of the permutation $$$q_1, q_2, ldots, q_n$$$ of integers from $$$1$$$ to $$$n$$$ as $$$$$$q_1 - p_{q_{2}} + q_2 - p_{q_{3}} + ldots + q_{n-1} - p_{q_{n}} + q_n - p_{q_{1}}$$$$$$ You want your permutation to be as lightweight as possible. Find any permutation $$$q$$$ with the smallest possible weight. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 200$$$) xa0β€” the size of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct) xa0β€” the elements of the permutation. The sum of $$$n$$$ over all test cases doesn't exceed $$$400$$$. Output For each test case, output $$$n$$$ integers $$$q_1, q_2, ldots, q_n$$$ ($$$1 le q_i le n$$$, all $$$q_i$$$ are distinct) xa0β€” one of the permutations with the smallest weight. Example Input 3 2 2 1 4 2 3 1 4 5 5 4 3 2 1 Output 1 2 1 3 4 2 1 4 2 3 5 Note In the first test case, there are two permutations of length $$$2$$$: $$$(1, 2)$$$ and $$$(2, 1)$$$. Permutation $$$(1, 2)$$$ has weight $$$1 - p_2 + 2 - p_1 = 0$$$, and permutation $$$(2, 1)$$$ has the same weight: $$$2 - p_1 + 1 - p_2 = 0$$$. You can output any of these permutations in this version. In the second test case, the weight of the permutation $$$(1, 3, 4, 2)$$$ is $$$1 - p_3 + 3 - p_4 + 4 - p_2 + 2 - p_1 = 1 - 1 + 3 - 4 + 4 - 3 + 2 - 2 = 2$$$. There are no permutations with smaller weights. In the third test case, the weight of the permutation $$$(1, 4, 2, 3, 5)$$$ is $$$1 - p_4 + 4 - p_2 + 2 - p_3 + 3 - p_5 + 5 - p_1 = 1 - 2 + 4 - 4 + 2 - 3 + 3 - 1 + 5 - 5 = 4$$$. There are no permutations with smaller weights.
2,800
false
false
false
false
false
true
false
false
false
false
2,162
1829E
You are given an $$$n imes m$$$ grid $$$a$$$ of non-negative integers. The value $$$a_{i,j}$$$ represents the depth of water at the $$$i$$$-th row and $$$j$$$-th column. A lake is a set of cells such that: each cell in the set has $$$a_{i,j} > 0$$$, and there exists a path between any pair of cells in the lake by going up, down, left, or right a number of times and without stepping on a cell with $$$a_{i,j} = 0$$$. The volume of a lake is the sum of depths of all the cells in the lake. Find the largest volume of a lake in the grid. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0β€” the number of test cases. The first line of each test case contains two integers $$$n, m$$$ ($$$1 leq n, m leq 1000$$$)xa0β€” the number of rows and columns of the grid, respectively. Then $$$n$$$ lines follow each with $$$m$$$ integers $$$a_{i,j}$$$ ($$$0 leq a_{i,j} leq 1000$$$)xa0β€” the depth of the water at each cell. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single integerxa0β€” the largest volume of a lake in the grid. Example Input 5 3 3 1 2 0 3 4 0 0 0 5 1 1 0 3 3 0 1 1 1 0 1 1 1 1 5 5 1 1 1 1 1 1 0 0 0 1 1 0 5 0 1 1 0 0 0 1 1 1 1 1 1 5 5 1 1 1 1 1 1 0 0 0 1 1 1 4 0 1 1 0 0 0 1 1 1 1 1 1
1,100
false
false
true
false
false
false
false
false
false
true
1,310
1691B
A class of students got bored wearing the same pair of shoes every day, so they decided to shuffle their shoes among themselves. In this problem, a pair of shoes is inseparable and is considered as a single object. There are $$$n$$$ students in the class, and you are given an array $$$s$$$ in non-decreasing order, where $$$s_i$$$ is the shoe size of the $$$i$$$-th student. A shuffling of shoes is valid only if no student gets their own shoes and if every student gets shoes of size greater than or equal to their size. You have to output a permutation $$$p$$$ of $$${1,2,ldots,n}$$$ denoting a valid shuffling of shoes, where the $$$i$$$-th student gets the shoes of the $$$p_i$$$-th student ($$$p_i e i$$$). And output $$$-1$$$ if a valid shuffling does not exist. A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) β€” the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1leq nleq10^5$$$)xa0β€” the number of students. The second line of each test case contains $$$n$$$ integers $$$s_1, s_2,ldots,s_n$$$ ($$$1leq s_ileq10^9$$$, and for all $$$1le i<n$$$, $$$s_ile s_{i+1}$$$) β€” the shoe sizes of the students. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print the answer in a single line using the following format. If a valid shuffling does not exist, print the number $$$-1$$$ as the answer. If a valid shuffling exists, print $$$n$$$ space-separated integers β€” a permutation $$$p$$$ of $$$1,2,ldots,n$$$ denoting a valid shuffling of shoes where the $$$i$$$-th student gets the shoes of the $$$p_i$$$-th student. If there are multiple answers, then print any of them. Example Input 2 5 1 1 1 1 1 6 3 6 8 13 15 21 Note In the first test case, any permutation $$$p$$$ of $$$1,ldots,n$$$ where $$$p_i e i$$$ would represent a valid shuffling since all students have equal shoe sizes, and thus anyone can wear anyone's shoes. In the second test case, it can be shown that no valid shuffling is possible.
1,000
false
true
true
false
false
true
false
false
false
false
2,135
914H
Ember and Storm play a game. First, Ember picks a labelled tree _T_ of _n_ vertices, such that the degree of every vertex is at most _d_. Then, Storm picks two distinct vertices _u_ and _v_ in this tree and writes down the labels of the vertices in the path from _u_ to _v_ in a sequence _a_1,u2009_a_2... _a__k_. Finally, Ember picks any index _i_ (1u2009≀u2009_i_u2009<u2009_k_) in the array. Now he performs one of the following two operations exactly once: flip the subrange [_i_u2009+u20091,u2009_k_] and add _a__i_ to it. After this, the sequence becomes _a_1,u2009... _a__i_,u2009_a__k_u2009+u2009_a__i_,u2009_a__k_u2009-u20091u2009+u2009_a__i_,u2009... _a__i_u2009+u20091u2009+u2009_a__i_ negate the subrange [_i_u2009+u20091,u2009_k_] and add _a__i_ to it. i.e., the array becomes _a_1,u2009... _a__i_,u2009u2009-u2009_a__i_u2009+u20091u2009+u2009_a__i_,u2009u2009-u2009_a__i_u2009+u20092u2009+u2009_a__i_,u2009...u2009-u2009_a__k_u2009+u2009_a__i_ Ember wins if the array is monotonically increasing or decreasing after this. Otherwise Storm wins. The game can be described by the tuple (_T_,u2009_u_,u2009_v_,u2009_i_,u2009_op_) where _op_ is Β«flipΒ» or Β«negateΒ» depending on the action Ember chose in the last turn. Find the number of tuples that can occur if Ember and Storm play optimally. When they play optimally, if there are multiple moves by which they are guaranteed to win, then they may play any of the winning moves. Otherwise, if someone loses no matter what they play, then they may play any of the possible moves. Report the answer modulo _m_. Input The input consists of a single line containing three integers _n_, _d_ and _m_ (2u2009≀u2009_n_u2009≀u2009200,u20091u2009≀u2009_d_u2009<u2009_n_,u20091u2009≀u2009_m_u2009≀u20092Β·109). Output Print a single number xa0β€” the number of possible tuples if Ember and Storm play as described, modulo _m_. Note In the first sample case, there is only one possible tree. There are two possible paths, 1 to 2 and 2 to 1. For both paths, _i_ can only be 1, and _op_ can take both possibilities. Therefore, the answer is 4. In the second sample, there are no possible trees. In the third sample, there are three possible trees.
3,400
false
false
false
true
false
false
false
false
false
false
6,076
1926A
Problem - 1926A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation *800 No tag edit access β†’ Contest materials ") xa0β€” the number of test cases. The only line of each test case contains a string of length $$$5$$$ consisting of letters $$$ exttt{A}$$$ and $$$ exttt{B}$$$. All $$$t$$$ strings in a test are different (distinct). Output For each test case, output one letter ($$$ exttt{A}$$$ or $$$ exttt{B}$$$) denoting the character that appears most frequently in the string. Example Input 8 ABABB ABABA BBBAB AAAAA BBBBB BABAA AAAAB BAAAA Output B A B A B A A A
800
false
false
true
false
false
false
false
false
false
false
737
23D
Problem - 23D - Codeforces =============== xa0 . Each of the following _T_ lines contains numbers _x_1, _y_1, _x_2, _y_2, _x_3, _y_3 β€” coordinates of different points that are the centers of three equal sides (non-negative integer numbers, not exceeding 10). Output For each test output two lines. If the required tetragon exists, output in the first line YES, in the second line β€” four pairs of numbers β€” coordinates of the polygon's vertices in clockwise or counter-clockwise order. Don't forget, please, that the tetragon should be strictly convex, i.e. no 3 of its points lie on one line. Output numbers with 9 characters after a decimal point. If the required tetragon doen't exist, output NO in the first line, and leave the second line empty. Examples Input 3 1 1 2 2 3 3 0 1 1 0 2 2 9 3 7 9 9 8 Output NO YES 3.5 1.5 0.5 2.5 -0.5 -0.5 2.5 0.5 NO
2,600
true
false
false
false
false
false
false
false
false
false
9,880
630K
Problem - 630K - Codeforces =============== xa0 ]( "Experimental Educational Round: VolBIT Formulas Blitz") β€” the prediction on the number of people who will buy the game. Output Output one integer showing how many numbers from 1 to _n_ are not divisible by any number from 2 to 10. Examples Input 12 Output 2
1,500
true
false
false
false
false
false
false
false
false
false
7,294
1154G
Problem - 1154G - 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 greedy math number theory *2200 No tag edit access β†’ Contest materials ") Editorial") that $$$lcm(a_i, a_j)$$$ is minimum possible. $$$lcm(x, y)$$$ is the least common multiple of $$$x$$$ and $$$y$$$ (minimum positive number such that both $$$x$$$ and $$$y$$$ are divisors of this number). Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 10^6$$$) β€” the number of elements in $$$a$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^7$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. Output Print two integers $$$i$$$ and $$$j$$$ ($$$1 le i < j le n$$$) such that the value of $$$lcm(a_i, a_j)$$$ is minimum among all valid pairs $$$i, j$$$. If there are multiple answers, you can print any. Examples Input 5 2 4 8 3 6 Output 1 2 Input 5 5 2 11 3 7 Output 2 4 Input 6 2 5 10 1 10 2 Output 1 4
2,200
true
true
false
false
false
false
true
false
false
false
4,937
139A
One Sunday Petr went to a bookshop and bought a new book on sports programming. The book had exactly _n_ pages. Petr decided to start reading it starting from the next day, that is, from Monday. Petr's got a very tight schedule and for each day of the week he knows how many pages he will be able to read on that day. Some days are so busy that Petr will have no time to read whatsoever. However, we know that he will be able to read at least one page a week. Assuming that Petr will not skip days and will read as much as he can every day, determine on which day of the week he will read the last page of the book. Input The first input line contains the single integer _n_ (1u2009≀u2009_n_u2009≀u20091000) β€” the number of pages in the book. The second line contains seven non-negative space-separated integers that do not exceed 1000 β€” those integers represent how many pages Petr can read on Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday correspondingly. It is guaranteed that at least one of those numbers is larger than zero. Output Print a single number β€” the number of the day of the week, when Petr will finish reading the book. The days of the week are numbered starting with one in the natural order: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday. Examples Input 100 15 20 20 15 10 30 45 Note Note to the first sample: By the end of Monday and therefore, by the beginning of Tuesday Petr has 85 pages left. He has 65 pages left by Wednesday, 45 by Thursday, 30 by Friday, 20 by Saturday and on Saturday Petr finishes reading the book (and he also has time to read 10 pages of something else). Note to the second sample: On Monday of the first week Petr will read the first page. On Monday of the second week Petr will read the second page and will finish reading the book.
1,000
false
false
true
false
false
false
false
false
false
false
9,320
445A
DZY loves chessboard, and he enjoys playing with it. He has a chessboard of _n_ rows and _m_ columns. Some cells of the chessboard are bad, others are good. For every good cell, DZY wants to put a chessman on it. Each chessman is either white or black. After putting all chessmen, DZY wants that no two chessmen with the same color are on two adjacent cells. Two cells are adjacent if and only if they share a common edge. You task is to find any suitable placement of chessmen on the given chessboard. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009100). Each of the next _n_ lines contains a string of _m_ characters: the _j_-th character of the _i_-th string is either "." or "-". A "." means that the corresponding cell (in the _i_-th row and the _j_-th column) is good, while a "-" means it is bad. Output Output must contain _n_ lines, each line must contain a string of _m_ characters. The _j_-th character of the _i_-th string should be either "W", "B" or "-". Character "W" means the chessman on the cell is white, "B" means it is black, "-" means the cell is a bad cell. If multiple answers exist, print any of them. It is guaranteed that at least one answer exists. Note In the first sample, DZY puts a single black chessman. Of course putting a white one is also OK. In the second sample, all 4 cells are good. No two same chessmen share an edge in the sample output. In the third sample, no good cells are adjacent. So you can just put 3 chessmen, no matter what their colors are.
1,200
false
false
true
false
false
false
false
false
false
false
8,058
1547F
You are given an array of positive integers $$$a = $$$-th element is the $$$0$$$-th element). Formally speaking, a new array $$$b = [b_0, b_1, dots, b_{n - 1}]$$$ is being built from array $$$a = [a_0, a_1, dots, a_{n - 1}]$$$ such that $$$b_i$$$ $$$= gcd(a_i, a_{(i + 1) mod n})$$$, where $$$gcd(x, y)$$$ is the greatest common divisor of $$$x$$$ and $$$y$$$, and $$$x mod y$$$ is the remainder of $$$x$$$ dividing by $$$y$$$. In one step the array $$$b$$$ is built and then the array $$$a$$$ is replaced with $$$b$$$ (that is, the assignment $$$a$$$ := $$$b$$$ is taking place). For example, if $$$a = [16, 24, 10, 5]$$$ then $$$b = [gcd(16, 24)$$$, $$$gcd(24, 10)$$$, $$$gcd(10, 5)$$$, $$$gcd(5, 16)]$$$ $$$= [8, 2, 5, 1]$$$. Thus, after one step the array $$$a = [16, 24, 10, 5]$$$ will be equal to $$$[8, 2, 5, 1]$$$. For a given array $$$a$$$, find the minimum number of steps after which all values $$$a_i$$$ become equal (that is, $$$a_0 = a_1 = dots = a_{n - 1}$$$). If the original array $$$a$$$ consists of identical elements then consider the number of steps is equal to $$$0$$$. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. Each test case contains two lines. The first line contains an integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) β€” length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_0, a_1, dots, a_{n - 1}$$$ ($$$1 le a_i le 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$.
1,900
false
false
false
false
true
false
true
true
false
false
2,908
338D
Problem - 338D - 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 chinese remainder theorem math number theory *2900 No tag edit access β†’ Contest materials u2009=u2009_GCD_(_i_,u2009_j_) for all 1u2009≀u2009_i_u2009≀u2009_n_,u20091u2009≀u2009_j_u2009≀u2009_m_. _GCD_(_a_,u2009_b_) is the greatest common divisor of numbers _a_ and _b_. You have a sequence of positive integer numbers _a_1,u2009_a_2,u2009...,u2009_a__k_. We say that this sequence occurs in table _G_ if it coincides with consecutive elements in some row, starting from some position. More formally, such numbers 1u2009≀u2009_i_u2009≀u2009_n_ and 1u2009≀u2009_j_u2009≀u2009_m_u2009-u2009_k_u2009+u20091 should exist that _G_(_i_,u2009_j_u2009+u2009_l_u2009-u20091)u2009=u2009_a__l_ for all 1u2009≀u2009_l_u2009≀u2009_k_. Determine if the sequence _a_ occurs in table _G_. Input The first line contains three space-separated integers _n_, _m_ and _k_ (1u2009≀u2009_n_,u2009_m_u2009≀u20091012; 1u2009≀u2009_k_u2009≀u200910000). The second line contains _k_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≀u2009_a__i_u2009≀u20091012). Output Print a single word "YES", if the given sequence occurs in table _G_, otherwise print "NO". Examples Input 100 100 5 5 2 1 2 1 Output YES Input 100 8 5 5 2 1 2 1 Output NO Input 100 100 7 1 2 3 4 5 6 7 Output NO Note Sample 1. The tenth row of table _G_ starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence _a_. Sample 2. This time the width of table _G_ equals 8. Sequence _a_ doesn't occur there.
2,900
true
false
false
false
false
false
false
false
false
false
8,482
652B
Problem - 652B - Codeforces =============== xa0 ]( "Announcement") β€” the number of elements in the array _a_. The second line contains _n_ integers _a__i_ (1u2009≀u2009_a__i_u2009≀u2009109) β€” the elements of the array _a_. Output If it's possible to make the array _a_ _z_-sorted print _n_ space separated integers _a__i_ β€” the elements after _z_-sort. Otherwise print the only word "Impossible". Examples Input 4 1 2 2 1 Output 1 2 1 2 Input 5 1 3 2 2 5 Output 1 5 2 3 2
1,000
false
false
false
false
false
false
false
false
true
false
7,209
158E
Cool J has recently become a businessman Mr. Jackson, and he has to make a lot of phone calls now. Today he has _n_ calls planned. For each call we know the moment _t__i_ (in seconds since the start of the day) when it is scheduled to start and its duration _d__i_ (in seconds). All _t__i_ are different. Mr. Jackson is a very important person, so he never dials anybody himself, all calls will be incoming. Mr. Jackson isn't Caesar and he can't do several things at once. If somebody calls him while he hasn't finished the previous conversation, Mr. Jackson puts the new call on hold in the queue. In this case immediately after the end of the current call Mr. Jackson takes the earliest incoming call from the queue and starts the conversation. If Mr. Jackson started the call at the second _t_, and the call continues for _d_ seconds, then Mr. Jackson is busy at seconds _t_,u2009_t_u2009+u20091,u2009...,u2009_t_u2009+u2009_d_u2009-u20091, and he can start a new call at second _t_u2009+u2009_d_. Note that if Mr. Jackson is not busy talking when somebody calls, he can't put this call on hold. Mr. Jackson isn't Napoleon either, he likes to sleep. So sometimes he allows himself the luxury of ignoring a call, as if it never was scheduled. He can ignore at most _k_ calls. Note that a call which comes while he is busy talking can be ignored as well. What is the maximum number of seconds Mr. Jackson can sleep today, assuming that he can choose an arbitrary continuous time segment from the current day (that is, with seconds from the 1-st to the 86400-th, inclusive) when he is not busy talking? Note that some calls can be continued or postponed to the next day or even later. However, the interval for sleep should be completely within the current day. Input The first input line contains a pair of integers _n_, _k_ (0u2009≀u2009_k_u2009≀u2009_n_u2009≀u20094000) separated by a space. Following _n_ lines contain the description of calls for today. The description of each call is located on the single line and consists of two space-separated integers _t__i_ and _d__i_, (1u2009≀u2009_t__i_,u2009_d__i_u2009≀u200986400). All _t__i_ are distinct, the calls are given in the order of strict increasing _t__i_. Scheduled times of calls [_t__i_, _t__i_u2009+u2009_d__i_u2009-u20091] can arbitrarily intersect. Note In the first sample the most convenient way is to ignore the first two calls. In the second sample it is best to ignore the third call. In this case Mr. Jackson will have been speaking: first call: from 1-st to 20000-th second, second call: from 20001-st to 30000-th second, fourth call: from 30001-st to 40000-th second (the third call is ignored), fifth call: from 80000-th to 139999-th second. Thus, the longest period of free time is from the 40001-th to the 79999-th second.
1,900
false
false
false
true
false
false
false
false
true
false
9,241
1612C
You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something. More precisely, you want to spam the emote triangle of size $$$k$$$. It consists of $$$2k-1$$$ messages. The first message consists of one emote, the second one β€” of two emotes, ..., the $$$k$$$-th one β€” of $$$k$$$ emotes, the $$$k+1$$$-th one β€” of $$$k-1$$$ emotes, ..., and the last one β€” of one emote. For example, the emote triangle for $$$k=3$$$ consists of $$$5$$$ messages: Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least $$$x$$$ emotes in succession (you can assume you are the only user in the chat). Now you are interested β€” how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all $$$2k-1$$$ messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases. The next $$$t$$$ lines describe test cases. The only line of the test case contains integers $$$k$$$ and $$$x$$$ ($$$1 le k le 10^9; 1 le x le 10^{18}$$$). Output For each test case, print the number of messages you will write before getting banned for the corresponding values $$$k$$$ and $$$x$$$. Example Input 7 4 6 4 7 1 2 3 7 2 5 100 1 1000000000 923456789987654321 Output 3 4 1 4 3 1 1608737403 Note Let's analyze the test cases of the example. 1. In the first test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$3$$$ emotes respectively, and since $$$1 + 2 + 3 ge 6$$$, you get banned after that. 2. In the second test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$ emotes respectively, and since $$$1 + 2 + 3 + 4 ge 7$$$, you get banned after that. 3. In the third test case, you write one message containing exactly $$$1$$$ emote. It doesn't get you banned, since $$$1 < 2$$$, but you have already finished posting your emote triangle. So you wrote one message successfully. 4. In the fourth test case, you write four messages containing $$$1$$$, $$$2$$$, $$$3$$$ and $$$2$$$ emotes respectively, and since $$$1 + 2 + 3 + 2 ge 7$$$, you get banned after that. 5. In the fifth test case, you write three messages containing $$$1$$$, $$$2$$$ and $$$1$$$ emote respectively. It doesn't get you banned, since $$$1 + 2 + 1 < 5$$$, but you have already finished posting your emote triangle. So you wrote three messages successfully. 6. In the sixth test case, since $$$x = 1$$$, you get banned as soon as you send your first message. 7. The seventh test case is too large to analyze, so we'll skip it.
1,300
true
false
false
false
false
false
false
true
false
false
2,581
734A
Problem - 734A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation strings *800 No tag edit access β†’ Contest materials ") ") xa0β€” the number of games played. The second line contains a string _s_, consisting of _n_ uppercase English letters 'A' and 'D'xa0β€” the outcome of each of the games. The _i_-th character of the string is equal to 'A' if the Anton won the _i_-th game and 'D' if Danik won the _i_-th game. Output If Anton won more games than Danik, print "Anton" (without quotes) in the only line of the output. If Danik won more games than Anton, print "Danik" (without quotes) in the only line of the output. If Anton and Danik won the same number of games, print "Friendship" (without quotes). Examples Input 6 ADAAAA Output Anton Input 7 DDDAADA Output Danik Input 6 DADADA Output Friendship Note In the first sample, Anton won 6 games, while Danikxa0β€” only 1. Hence, the answer is "Anton". In the second sample, Anton won 3 games and Danik won 4 games, so the answer is "Danik". In the third sample, both Anton and Danik won 3 games and the answer is "Friendship".
800
false
false
true
false
false
false
false
false
false
false
6,858
446E
DZY owns 2_m_ islands near his home, numbered from 1 to 2_m_. He loves building bridges to connect the islands. Every bridge he builds takes one day's time to walk across. DZY has a strange rule of building the bridges. For every pair of islands _u_,u2009_v_xa0(_u_u2009β‰ u2009_v_), he has built 2_k_ different bridges connecting them, where (_a__b_ means _b_ is divisible by _a_). These bridges are bidirectional. Also, DZY has built some bridges connecting his home with the islands. Specifically, there are _a__i_ different bridges from his home to the _i_-th island. These are one-way bridges, so after he leaves his home he will never come back. DZY decides to go to the islands for sightseeing. At first he is at home. He chooses and walks across one of the bridges connecting with his home, and arrives at some island. After that, he will spend _t_ day(s) on the islands. Each day, he can choose to stay and rest, or to walk to another island across the bridge. It is allowed to stay at an island for more than one day. It's also allowed to cross one bridge more than once. Suppose that right after the _t_-th day DZY stands at the _i_-th island. Let _ans_[_i_] be the number of ways for DZY to reach the _i_-th island after _t_-th day. Your task is to calculate _ans_[_i_] for each _i_ modulo 1051131. Input To avoid huge input, we use the following way to generate the array _a_. You are given the first _s_ elements of array: _a_1,u2009_a_2,u2009...,u2009_a__s_. All the other elements should be calculated by formula: _a__i_u2009=u2009(101Β·_a__i_u2009-u2009_s_u2009+u200910007)xa0_mod_xa01051131 (_s_u2009<u2009_i_u2009≀u20092_m_). The first line contains three integers _m_,u2009_t_,u2009_s_ (1u2009≀u2009_m_u2009≀u200925;xa01u2009≀u2009_t_u2009≀u20091018;xa01u2009≀u2009_s_u2009≀u2009_min_(2_m_,u2009105)). The second line contains _s_ integers _a_1,u2009_a_2,u2009...,u2009_a__s_xa0(1u2009≀u2009_a__i_u2009≀u2009106). Output To avoid huge output, you only need to output xor-sum of all the answers for all _i_ modulo 1051131 (1u2009≀u2009_i_u2009≀u20092_m_), i.e. (_ans_[1]xa0_mod_xa01051131)xa0_xor_xa0(_ans_[2]xa0_mod_xa01051131)xa0_xor_... _xor_xa0(_ans_[_n_]xa0_mod_xa01051131). Examples Input 3 5 6 389094 705719 547193 653800 947499 17024 Note In the first sample, _ans_u2009=u2009[6,u20097,u20096,u20096]. If he wants to be at island 1 after one day, he has 6 different ways: 1. home β€”> 1 -(stay)-> 1 2. home β€”> 2 β€”> 1 3. home β€”> 3 β€”> 1 4. home β€”> 3 β€”> 1 (note that there are two different bridges between 1 and 3) 5. home β€”> 4 β€”> 1 6. home β€”> 4 β€”> 1 (note that there are two different bridges from home to 4) In the second sample, (_a_1,u2009_a_2,u2009_a_3,u2009_a_4,u2009_a_5,u2009_a_6,u2009_a_7,u2009_a_8)u2009=u2009(389094,u2009705719,u2009547193,u2009653800,u2009947499,u200917024,u2009416654,u2009861849), _ans_u2009=u2009[235771,u2009712729,u2009433182,u2009745954,u2009139255,u2009935785,u2009620229,u2009644335].
3,100
true
false
false
false
false
false
false
false
false
false
8,052
1949F
You are the developer of a dating app which ignores gender completely. The app has $$$n$$$ users, indexed from $$$1$$$ to $$$n$$$. Each user's profile features a list of the activities they enjoy doing. There are $$$m$$$ possible activities, indexed from $$$1$$$ to $$$m$$$. A match between two users is good if they share at least one activity and, at the same time, both of them like at least one activity that the other user does not like. Find a good match if it exists. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 200,000$$$, $$$1 leq m leq 10^6$$$) β€” the number of users and the number of activities. Each of the following $$$n$$$ lines contains a number $$$k_i$$$ ($$$0 leq k_i leq m$$$) β€” the number of activities that user $$$i$$$ likes β€” followed by $$$k_i$$$ distinct integers from $$$1$$$ to $$$m$$$ β€” the activities user $$$i$$$ likes. It is guaranteed that $$$k_1+k_2+cdots+k_n$$$ does not exceed $$$10^6$$$. Output Print $$$ exttt{YES}$$$ if a good match exists. Otherwise, print $$$ exttt{NO}$$$. If a good match exists, on the next line print two integersxa0β€” the indexes of two users that make a match. Examples Input 3 5 3 1 2 4 5 1 2 3 4 5 2 1 5 Note In the first sample, users $$$1$$$ and $$$3$$$ form a match, because they share activity $$$1$$$, and, furthermore, user $$$3$$$ likes activity $$$5$$$ (which user $$$1$$$ does not like) and user $$$1$$$ likes activity $$$4$$$ (which user $$$3$$$ does not like). Note that users $$$1$$$ and $$$2$$$, as well as users $$$2$$$ and $$$3$$$, do not form a match, as there is no activity that users $$$1$$$ or $$$3$$$ like, and user $$$2$$$ doesn't like.
2,200
false
true
false
false
false
false
false
false
true
false
587
888B
Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0,u20090). The robot can process commands. There are four types of commands it can perform: U β€” move from the cell (_x_,u2009_y_) to (_x_,u2009_y_u2009+u20091); D β€” move from (_x_,u2009_y_) to (_x_,u2009_y_u2009-u20091); L β€” move from (_x_,u2009_y_) to (_x_u2009-u20091,u2009_y_); R β€” move from (_x_,u2009_y_) to (_x_u2009+u20091,u2009_y_). Ivan entered a sequence of _n_ commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0,u20090), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations! Input The first line contains one number _n_ β€” the length of sequence of commands entered by Ivan (1u2009≀u2009_n_u2009≀u2009100). The second line contains the sequence itself β€” a string consisting of _n_ characters. Each character can be U, D, L or R. Output Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.
1,000
false
true
false
false
false
false
false
false
false
false
6,195
616A
Problem - 616A - Codeforces =============== xa0 . You should check what number _a_ or _b_ is greater or determine that they are equal. The input size is very large so don't use the reading of symbols one by one. Instead of that use the reading of a whole line or token. As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead of Scanner/System.out in Java. Don't use the function input() in Python2 instead of it use the function raw_input(). Input The first line contains a non-negative integer _a_. The second line contains a non-negative integer _b_. The numbers _a_,u2009_b_ may contain leading zeroes. Each of them contains no more than 106 digits. Output Print the symbol "<" if _a_u2009<u2009_b_ and the symbol ">" if _a_u2009>u2009_b_. If the numbers are equal print the symbol "=". Examples Input 9 10 Output < Input 11 10 Output > Input 00012345 12345 Output = Input 0123 9 Output > Input 0123 111 Output >
900
false
false
true
false
false
false
false
false
false
false
7,375
1310B
The biggest event of the year – Cota 2 world championship "The Innernational" is right around the corner. $$$2^n$$$ teams will compete in a double-elimination format (please, carefully read problem statement even if you know what is it) to identify the champion. Teams are numbered from $$$1$$$ to $$$2^n$$$ and will play games one-on-one. All teams start in the upper bracket. All upper bracket matches will be held played between teams that haven't lost any games yet. Teams are split into games by team numbers. Game winner advances in the next round of upper bracket, losers drop into the lower bracket. Lower bracket starts with $$$2^{n-1}$$$ teams that lost the first upper bracket game. Each lower bracket round consists of two games. In the first game of a round $$$2^k$$$ teams play a game with each other (teams are split into games by team numbers). $$$2^{k-1}$$$ loosing teams are eliminated from the championship, $$$2^{k-1}$$$ winning teams are playing $$$2^{k-1}$$$ teams that got eliminated in this round of upper bracket (again, teams are split into games by team numbers). As a result of each round both upper and lower bracket have $$$2^{k-1}$$$ teams remaining. See example notes for better understanding. Single remaining team of upper bracket plays with single remaining team of lower bracket in grand-finals to identify championship winner. You are a fan of teams with numbers $$$a_1, a_2, ..., a_k$$$. You want the championship to have as many games with your favourite teams as possible. Luckily, you can affect results of every championship game the way you want. What's maximal possible number of championship games that include teams you're fan of? Input First input line has two integers $$$n, k$$$xa0β€” $$$2^n$$$ teams are competing in the championship. You are a fan of $$$k$$$ teams ($$$2 le n le 17; 0 le k le 2^n$$$). Second input line has $$$k$$$ distinct integers $$$a_1, ldots, a_k$$$xa0β€” numbers of teams you're a fan of ($$$1 le a_i le 2^n$$$). Output Output single integerxa0β€” maximal possible number of championship games that include teams you're fan of. Note On the image, each game of the championship is denoted with an English letter ($$$a$$$ to $$$n$$$). Winner of game $$$i$$$ is denoted as $$$Wi$$$, loser is denoted as $$$Li$$$. Teams you're a fan of are highlighted with red background. In the first example, team $$$6$$$ will play in 6 games if it looses the first upper bracket game (game $$$c$$$) and wins all lower bracket games (games $$$h, j, l, m$$$). In the second example, teams $$$7$$$ and $$$8$$$ have to play with each other in the first game of upper bracket (game $$$d$$$). Team $$$8$$$ can win all remaining games in upper bracket, when teams $$$1$$$ and $$$7$$$ will compete in the lower bracket. In the third example, your favourite teams can play in all games of the championship.
2,500
false
false
true
true
false
false
false
false
false
false
4,148