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
1250L
A group of students has recently been admitted to the Faculty of Computer Sciences at the Berland State University. Now the programming teacher wants to divide them into three subgroups for practice sessions. The teacher knows that a lot of programmers argue which language is the best. The teacher doesn't want to hear any arguments in the subgroups, so she wants to divide the students into three subgroups so that no pair of students belonging to the same subgroup want to argue. To perform this division, the teacher asked each student which programming language he likes. There are $$$a$$$ students who answered that they enjoy Assembler, $$$b$$$ students stated that their favourite language is Basic, and $$$c$$$ remaining students claimed that C++ is the best programming language — and there was a large argument between Assembler fans and C++ fans. Now, knowing that Assembler programmers and C++ programmers can start an argument every minute, the teacher wants to divide the students into three subgroups so that every student belongs to exactly one subgroup, and there is no subgroup that contains at least one Assembler fan and at least one C++ fan. Since teaching a lot of students can be difficult, the teacher wants the size of the largest subgroup to be minimum possible. Please help the teacher to calculate the minimum possible size of the largest subgroup! Input The first line contains one integer $$$t$$$ ($$$1 le t le 5$$$) — the number of test cases in the input. Then test cases follow. Each test case consists of one line containing three integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 le a, b, c le 1000$$$) — the number of Assembler fans, Basic fans and C++ fans, respectively. Output For each test case print one integer — the minimum size of the largest subgroup if the students are divided in such a way that there is no subgroup that contains at least one Assembler fan and at least one C++ fan simultaneously. Examples Input 5 3 5 7 4 8 4 13 10 13 1000 1000 1000 13 22 7 Input 5 1 3 4 1000 1000 1 4 1 2 325 226 999 939 861 505 Note Explanation of the answers for the example $$$1$$$: 1. The first subgroup contains $$$3$$$ Assembler fans and $$$2$$$ Basic fans, the second subgroup — $$$5$$$ C++ fans, the third subgroup — $$$2$$$ C++ fans and $$$3$$$ Basic fans. 2. The first subgroup contains $$$4$$$ Assembler fans, the second subgroup — $$$6$$$ Basic fans, the third subgroup — $$$2$$$ Basic fans and $$$4$$$ C++ fans. 3. The first subgroup contains all Assembler fans, the second subgroup — all Basic fans, the third subgroup — all C++ fans. 4. The first subgroup contains all Assembler fans, the second subgroup — all Basic fans, the third subgroup — all C++ fans. 5. The first subgroup contains $$$12$$$ Assembler fans and $$$2$$$ Basic fans, the second subgroup — $$$1$$$ Assembler fan and $$$13$$$ Basic fans, the third subgroup — $$$7$$$ Basic fans and $$$7$$$ C++ fans.
1,500
true
true
false
false
false
false
false
true
false
false
4,444
1948D
You are given a string $$$s$$$, consisting of lowercase Latin letters and/or question marks. A tandem repeat is a string of an even length such that its first half is equal to its second half. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by the deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Your goal is to replace each question mark with some lowercase Latin letter in such a way that the length of the longest substring that is a tandem repeat is maximum possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. The only line of each testcase contains a string $$$s$$$ ($$$1 le s le 5000$$$), consisting only of lowercase Latin letters and/or question marks. The total length of the strings over all testcases doesn't exceed $$$5000$$$. Output For each testcase, print a single integerxa0— the maximum length of the longest substring that is a tandem repeat after you replace each question mark in the string with some lowercase Latin letter. If it's impossible to make any tandem repeat substrings in the string, print $$$0$$$. Example Input 4 zaabaabz ????? code?????s codeforces
1,700
false
false
false
false
false
false
true
false
false
false
596
325B
Daniel is organizing a football tournament. He has come up with the following tournament format: 1. In the first several (possibly zero) stages, while the number of teams is even, they split in pairs and play one game for each pair. At each stage the loser of each pair is eliminated (there are no draws). Such stages are held while the number of teams is even. 2. Eventually there will be an odd number of teams remaining. If there is one team remaining, it will be declared the winner, and the tournament ends. Otherwise each of the remaining teams will play with each other remaining team once in round robin tournament (if there are _x_ teams, there will be games), and the tournament ends. For example, if there were 20 teams initially, they would begin by playing 10 games. So, 10 teams would be eliminated, and the remaining 10 would play 5 games. Then the remaining 5 teams would play 10 games in a round robin tournament. In total there would be 10+5+10=25 games. Daniel has already booked the stadium for _n_ games. Help him to determine how many teams he should invite so that the tournament needs exactly _n_ games. You should print all possible numbers of teams that will yield exactly _n_ games in ascending order, or -1 if there are no such numbers. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091018), the number of games that should be played. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print all possible numbers of invited teams in ascending order, one per line. If exactly _n_ games cannot be played, output one number: -1.
1,800
true
false
false
false
false
false
false
true
false
false
8,540
549E
Berlanders like to eat cones after a hard day. Misha Square and Sasha Circle are local authorities of Berland. Each of them controls its points of cone trade. Misha has _n_ points, Sasha — _m_. Since their subordinates constantly had conflicts with each other, they decided to build a fence in the form of a circle, so that the points of trade of one businessman are strictly inside a circle, and points of the other one are strictly outside. It doesn't matter which of the two gentlemen will have his trade points inside the circle. Determine whether they can build a fence or not. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200910000), numbers of Misha's and Sasha's trade points respectively. The next _n_ lines contains pairs of space-separated integers _M__x_,u2009_M__y_ (u2009-u2009104u2009≤u2009_M__x_,u2009_M__y_u2009≤u2009104), coordinates of Misha's trade points. The next _m_ lines contains pairs of space-separated integers _S__x_,u2009_S__y_ (u2009-u2009104u2009≤u2009_S__x_,u2009_S__y_u2009≤u2009104), coordinates of Sasha's trade points. It is guaranteed that all _n_u2009+u2009_m_ points are distinct. Output The only output line should contain either word "YES" without quotes in case it is possible to build a such fence or word "NO" in the other case. Examples Input 4 4 1 0 0 1 -1 0 0 -1 1 1 -1 1 -1 -1 1 -1 Note In the first sample there is no possibility to separate points, because any circle that contains both points (u2009-u20091,u20090),u2009(1,u20090) also contains at least one point from the set (0,u2009u2009-u20091),u2009(0,u20091), and vice-versa: any circle that contains both points (0,u2009u2009-u20091),u2009(0,u20091) also contains at least one point from the set (u2009-u20091,u20090),u2009(1,u20090) In the second sample one of the possible solution is shown below. Misha's points are marked with red colour and Sasha's are marked with blue.
2,700
true
false
false
false
false
false
false
false
false
false
7,641
2000G
You live in a city consisting of $$$n$$$ intersections and $$$m$$$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly passing through some other intersections. Every minute, you can board a bus at intersection $$$u_i$$$ and travel for $$$l_{i1}$$$ minutes to intersection $$$v_i$$$. Conversely, you can travel from intersection $$$v_i$$$ to intersection $$$u_i$$$ in $$$l_{i1}$$$ minutes. You can only board and exit the bus at intersections. You can only board the bus at an intersection if you are currently there. You can also walk along each street, which takes $$$l_{i2} > l_{i1}$$$ minutes. You can make stops at intersections. You live at intersection number $$$1$$$. Today you woke up at time $$$0$$$, and you have an important event scheduled at intersection number $$$n$$$, which you must reach no later than time $$$t_0$$$. You also have a phone call planned that will last from $$$t_1$$$ to $$$t_2$$$ minutes ($$$t_1 < t_2 < t_0$$$). During the phone call, you cannot ride the bus, but you can walk along any streets, make stops, or stay at home. You can exit the bus at minute $$$t_1$$$ and board the bus again at minute $$$t_2$$$. Since you want to get enough sleep, you became curious — how late can you leave home to have time to talk on the phone and still not be late for the event? Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The following are the descriptions of the test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 le n le 10^5, 1 le m le 10^5$$$) — the number of intersections and streets in the city. The second line of each test case contains three integers $$$t_0$$$, $$$t_1$$$, $$$t_2$$$ ($$$1 le t_1 < t_2 < t_0 le 10^9$$$) — the start time of the event, the start time of the phone call, and its end time, respectively. The next $$$m$$$ lines of each test case contain descriptions of the streets. The $$$i$$$-th line contains four integers $$$u_i$$$, $$$v_i$$$, $$$l_{i1}$$$, $$$l_{i2}$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$, $$$1 le l_{i1} < l_{i2} le 10^9$$$) — the numbers of the intersections connected by the $$$i$$$-th street, as well as the travel time along the street by bus and on foot. It is guaranteed that no two streets connect the same pair of intersections and that it is possible to reach any intersection from any other. It is guaranteed that the sum of the values of $$$n$$$ across all test cases does not exceed $$$10^5$$$. It is also guaranteed that the sum of the values of $$$m$$$ across all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer — the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1. Example Input 7 5 5 100 20 80 1 5 30 100 1 2 20 50 2 3 20 50 3 4 20 50 4 5 20 50 2 1 100 50 60 1 2 55 110 4 4 100 40 60 1 2 30 100 2 4 30 100 1 3 20 50 3 4 20 50 3 3 100 80 90 1 2 1 10 2 3 10 50 1 3 20 21 3 2 58 55 57 2 1 1 3 2 3 3 4 2 1 12 9 10 2 1 6 10 5 5 8 5 6 2 1 1 8 2 3 4 8 4 2 2 4 5 3 3 4 4 5 2 6
2,100
false
true
false
false
false
false
true
true
false
true
257
954A
Problem - 954A - 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 . After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left. Your problem is to print the minimum possible length of the sequence of moves after the replacements. Input The first line of the input contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the length of the sequence. The second line contains the sequence consisting of _n_ characters U and R. Output Print the minimum possible length of the sequence of moves after all replacements are done. Examples Input 5 RUURU Output 3 Input 17 UUURRRRRUUURURUUU Output 13 Note In the first test the shortened sequence of moves may be DUD (its length is 3). In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
800
false
false
true
false
false
false
false
false
false
false
5,909
1185B
Methodius received an email from his friend Polycarp. However, Polycarp's keyboard is broken, so pressing a key on it once may cause the corresponding symbol to appear more than once (if you press a key on a regular keyboard, it prints exactly one symbol). For example, as a result of typing the word "hello", the following words could be printed: "hello", "hhhhello", "hheeeellllooo", but the following could not be printed: "hell", "helo", "hhllllooo". Note, that when you press a key, the corresponding symbol must appear (possibly, more than once). The keyboard is broken in a random manner, it means that pressing the same key you can get the different number of letters in the result. For each word in the letter, Methodius has guessed what word Polycarp actually wanted to write, but he is not sure about it, so he asks you to help him. You are given a list of pairs of words. For each pair, determine if the second word could be printed by typing the first one on Polycarp's keyboard. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of pairs to check. Further input contains $$$n$$$ descriptions of pairs. The first line of each description contains a single non-empty word $$$s$$$ consisting of lowercase Latin letters. The second line of the description contains a single non-empty word $$$t$$$ consisting of lowercase Latin letters. The lengths of both strings are not greater than $$$10^6$$$. It is guaranteed that the total length of all words $$$s$$$ in the input is not greater than $$$10^6$$$. Also, it is guaranteed that the total length of all words $$$t$$$ in the input is not greater than $$$10^6$$$.
1,200
false
false
true
false
false
false
false
false
false
false
4,779
580A
Problem - 580A - 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 implementation *900 No tag edit access → Contest materials ") he makes _a__i_ money. Kefa loves progress, that's why he wants to know the length of the maximum non-decreasing subsegment in sequence _a__i_. Let us remind you that the subsegment of the sequence is its continuous fragment. A subsegment of numbers is called non-decreasing if all numbers in it follow in the non-decreasing order. Help Kefa cope with this task! Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ integers _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output Print a single integer — the length of the maximum non-decreasing subsegment of sequence _a_. Examples Input 6 2 2 1 3 4 1 Output 3 Input 3 2 2 9 Output 3 Note In the first test the maximum non-decreasing subsegment is the numbers from the third to the fifth one. In the second test the maximum non-decreasing subsegment is the numbers from the first to the third one.
900
false
false
true
true
false
false
true
false
false
false
7,528
1978B
Bob decided to open a bakery. On the opening day, he baked $$$n$$$ buns that he can sell. The usual price of a bun is $$$a$$$ coins, but to attract customers, Bob organized the following promotion: Bob chooses some integer $$$k$$$ ($$$0 le k le min(n, b)$$$). Bob sells the first $$$k$$$ buns at a modified price. In this case, the price of the $$$i$$$-th ($$$1 le i le k$$$) sold bun is $$$(b - i + 1)$$$ coins. The remaining $$$(n - k)$$$ buns are sold at $$$a$$$ coins each. Note that $$$k$$$ can be equal to $$$0$$$. In this case, Bob will sell all the buns at $$$a$$$ coins each. Help Bob determine the maximum profit he can obtain by selling all $$$n$$$ buns. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 le n, a, b le 10^9$$$)xa0— the number of buns, the usual price of a bun, and the price of the first bun to be sold at a modified price. Output For each test case, output a single integerxa0— the maximum profit that Bob can obtain. Example Input 7 4 4 5 5 5 9 10 10 5 5 5 11 1000000000 1000000000 1000000000 1000000000 1000000000 1 1000 1 1000 Output 17 35 100 45 1000000000000000000 1000000000000000000 500500 Note In the first test case, it is optimal for Bob to choose $$$k = 1$$$. Then he will sell one bun for $$$5$$$ coins, and three buns at the usual price for $$$4$$$ coins each. Then the profit will be $$$5 + 4 + 4 + 4 = 17$$$ coins. In the second test case, it is optimal for Bob to choose $$$k = 5$$$. Then he will sell all the buns at the modified price and obtain a profit of $$$9 + 8 + 7 + 6 + 5 = 35$$$ coins. In the third test case, it is optimal for Bob to choose $$$k = 0$$$. Then he will sell all the buns at the usual price and obtain a profit of $$$10 cdot 10 = 100$$$ coins.
800
true
true
false
false
false
false
false
true
false
false
421
1791B
Alperen is standing at the point $$$(0,0)$$$. He is given a string $$$s$$$ of length $$$n$$$ and performs $$$n$$$ moves. The $$$i$$$-th move is as follows: if $$$s_i = exttt{L}$$$, then move one unit left; if $$$s_i = exttt{R}$$$, then move one unit right; if $$$s_i = exttt{U}$$$, then move one unit up; if $$$s_i = exttt{D}$$$, then move one unit down. If Alperen starts at the center point, he can make the four moves shown. There is a candy at $$$(1,1)$$$ (that is, one unit above and one unit to the right of Alperen's starting point). You need to determine if Alperen ever passes the candy. Alperen's path in the first test case. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of testcases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 50$$$)xa0— the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of characters $$$ exttt{L}$$$, $$$ exttt{R}$$$, $$$ exttt{D}$$$, and $$$ exttt{U}$$$, denoting the moves Alperen makes. Output For each test case, output "YES" (without quotes) if Alperen passes the candy, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Note In the first test case, Alperen follows the path $$$$$$(0,0) overset{ exttt{U}}{ o} (0,1) overset{ exttt{U}}{ o} (0,2) overset{ exttt{U}}{ o} (0,3) overset{ exttt{R}}{ o} (1,3) overset{ exttt{D}}{ o} (1,2) overset{ exttt{D}}{ o} color{green}{mathbf{(1,1)}} overset{ exttt{L}}{ o} (0,1).$$$$$$ Note that Alperen doesn't need to end at the candy's location of $$$(1,1)$$$, he just needs to pass it at some point. In the second test case, Alperen follows the path $$$$$$(0,0) overset{ exttt{U}}{ o} (0,1) overset{ exttt{R}}{ o} color{green}{mathbf{(1,1)}}.$$$$$$ In the third test case, Alperen follows the path $$$$$$(0,0) overset{ exttt{R}}{ o} (1,0) overset{ exttt{R}}{ o} (2,0) overset{ exttt{R}}{ o} (3,0) overset{ exttt{U}}{ o} (3,1) overset{ exttt{U}}{ o} (3,2) overset{ exttt{D}}{ o} (3,1) overset{ exttt{D}}{ o} (3,0) overset{ exttt{D}}{ o} (3,-1).$$$$$$ In the fourth test case, Alperen follows the path $$$$$$(0,0) overset{ exttt{L}}{ o} (-1,0) overset{ exttt{L}}{ o} (-2,0) overset{ exttt{L}}{ o} (-3,0).$$$$$$
800
false
false
true
false
false
false
false
false
false
false
1,534
1998B
You are given a permutation$$$^{ ext{∗}}$$$ $$$p$$$ of length $$$n$$$. Find a permutation $$$q$$$ of length $$$n$$$ that minimizes the number of pairs ($$$i, j$$$) ($$$1 leq i leq j leq n$$$) such that $$$p_i + p_{i+1} + ldots + p_j = q_i + q_{i+1} + ldots + q_j$$$. $$$^{ ext{∗}}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$). The following line contains $$$n$$$ space-separated integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0— denoting the permutation $$$p$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one line containing any permutation of length $$$n$$$ (the permutation $$$q$$$) such that $$$q$$$ minimizes the number of pairs. Example Input 3 2 1 2 5 1 2 3 4 5 7 4 7 5 1 2 6 3 Output 2 1 3 5 4 2 1 6 2 1 4 7 3 5 Note For the first test, there exists only one pair ($$$i, j$$$) ($$$1 leq i leq j leq n$$$) such that $$$p_i + p_{i+1} + ldots + p_j = q_i + q_{i+1} + ldots + q_j$$$, which is ($$$1, 2$$$). It can be proven that no such $$$q$$$ exists for which there are no pairs.
1,000
true
false
false
false
false
true
false
false
false
false
276
632F
Problem - 632F - Codeforces =============== xa0 , _a__ii_u2009=u20090 and _a__ij_u2009≤u2009_max_(_a__ik_,u2009_a__jk_) for all triples _i_,u2009_j_,u2009_k_. Note that _i_,u2009_j_,u2009_k_ do not need to be distinct. Determine if the matrix is magic. As the input/output can reach very 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. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092500) — the size of the matrix _A_. Each of the next _n_ lines contains _n_ integers _a__ij_ (0u2009≤u2009_a__ij_u2009<u2009109) — the elements of the matrix _A_. Note that the given matrix not necessarily is symmetric and can be arbitrary. Output Print ''MAGIC" (without quotes) if the given matrix _A_ is magic. Otherwise print ''NOT MAGIC". Examples Input 3 0 1 2 1 0 2 2 2 0 Output MAGIC Input 2 0 1 2 3 Output NOT MAGIC Input 4 0 1 2 3 1 0 3 4 2 3 0 5 3 4 5 0 Output NOT MAGIC
2,400
false
false
false
false
false
false
true
false
false
true
7,276
93B
Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams! Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe, the _m_ friends found _n_ different kinds of milk on the menu, that's why they ordered _n_ bottles — one bottle of each kind. We know that the volume of milk in each bottle equals _w_. When the bottles were brought in, they decided to pour all the milk evenly among the _m_ cups, so that each got a cup. As a punishment for not passing the test Igor was appointed the person to pour the milk. He protested that he was afraid to mix something up and suggested to distribute the drink so that the milk from each bottle was in no more than two different cups. His friends agreed but they suddenly faced the following problem — and what is actually the way to do it? Help them and write the program that will help to distribute the milk among the cups and drink it as quickly as possible! Note that due to Igor K.'s perfectly accurate eye and unswerving hands, he can pour any fractional amount of milk from any bottle to any cup. Input The only input data file contains three integers _n_, _w_ and _m_ (1u2009≤u2009_n_u2009≤u200950, 100u2009≤u2009_w_u2009≤u20091000, 2u2009≤u2009_m_u2009≤u200950), where _n_ stands for the number of ordered bottles, _w_ stands for the volume of each of them and _m_ stands for the number of friends in the company. Output Print on the first line "YES" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print "NO". If there is a solution, then print _m_ more lines, where the _i_-th of them describes the content of the _i_-th student's cup. The line should consist of one or more pairs that would look like "_b_ _v_". Each such pair means that _v_ (_v_u2009>u20090) units of milk were poured into the _i_-th cup from bottle _b_ (1u2009≤u2009_b_u2009≤u2009_n_). All numbers _b_ on each line should be different. If there are several variants to solve the problem, print any of them. Print the real numbers with no less than 6 digits after the decimal point. Examples Output YES 1 333.333333 2 333.333333 2 166.666667 1 166.666667 Output YES 3 20.000000 4 60.000000 1 80.000000 4 40.000000 2 40.000000 3 80.000000 2 60.000000 1 20.000000 Output YES 4 250.000000 5 500.000000 2 500.000000 3 500.000000 1 500.000000 4 250.000000
1,900
false
true
false
false
false
false
false
false
false
false
9,514
1749A
There's a chessboard of size $$$n imes n$$$. $$$m$$$ rooks are placed on it in such a way that: no two rooks occupy the same cell; no two rooks attack each other. A rook attacks all cells that are in its row or column. Is it possible to move exactly one rook (you can choose which one to move) into a different cell so that no two rooks still attack each other? A rook can move into any cell in its row or column if no other rook stands on its path. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0— the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 8$$$)xa0— the size of the chessboard and the number of the rooks. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$)xa0— the position of the $$$i$$$-th rook: $$$x_i$$$ is the row and $$$y_i$$$ is the column. No two rooks occupy the same cell. No two rooks attack each other. Output For each testcase, print "YES" if it's possible to move exactly one rook into a different cell so that no two rooks still attack each other. Otherwise, print "NO". Note In the first testcase, the rooks are in the opposite corners of a $$$2 imes 2$$$ board. Each of them has a move into a neighbouring corner, but moving there means getting attacked by another rook. In the second testcase, there's a single rook in a middle of a $$$3 imes 3$$$ board. It has $$$4$$$ valid moves, and every move is fine because there's no other rook to attack it.
800
false
true
true
false
false
false
false
false
false
false
1,791
1742D
Given an array of $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 1000$$$). Find the maximum value of $$$i + j$$$ such that $$$a_i$$$ and $$$a_j$$$ are coprime,$$$^{dagger}$$$ or $$$-1$$$ if no such $$$i$$$, $$$j$$$ exist. For example consider the array $$$. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 2cdot10^5$$$)xa0— the length of the array. The following line contains $$$n$$$ space-separated positive integers $$$a_1$$$, $$$a_2$$$,..., $$$a_n$$$ ($$$1 leq a_i leq 1000$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output a single integer xa0— the maximum value of $$$i + j$$$ such that $$$i$$$ and $$$j$$$ satisfy the condition that $$$a_i$$$ and $$$a_j$$$ are coprime, or output $$$-1$$$ in case no $$$i$$$, $$$j$$$ satisfy the condition. Example Input 6 3 3 2 1 7 1 3 5 2 4 7 7 5 1 2 3 4 5 3 2 2 4 6 5 4 3 15 12 16 5 1 2 2 3 6 Note For the first test case, we can choose $$$i = j = 3$$$, with sum of indices equal to $$$6$$$, since $$$1$$$ and $$$1$$$ are coprime. For the second test case, we can choose $$$i = 7$$$ and $$$j = 5$$$, with sum of indices equal to $$$7 + 5 = 12$$$, since $$$7$$$ and $$$4$$$ are coprime.
1,100
false
true
false
false
false
false
true
false
false
false
1,828
238B
This problem is the most boring one you've ever seen. Given a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ and a non-negative integer _h_, our goal is to partition the sequence into two subsequences (not necessarily consist of continuous elements). Each element of the original sequence should be contained in exactly one of the result subsequences. Note, that one of the result subsequences can be empty. Let's define function _f_(_a__i_,u2009_a__j_) on pairs of distinct elements (that is _i_u2009≠u2009_j_) in the original sequence. If _a__i_ and _a__j_ are in the same subsequence in the current partition then _f_(_a__i_,u2009_a__j_)u2009=u2009_a__i_u2009+u2009_a__j_ otherwise _f_(_a__i_,u2009_a__j_)u2009=u2009_a__i_u2009+u2009_a__j_u2009+u2009_h_. Consider all possible values of the function _f_ for some partition. We'll call the goodness of this partiotion the difference between the maximum value of function _f_ and the minimum value of function _f_. Your task is to find a partition of the given sequence _a_ that have the minimal possible goodness among all possible partitions. Input The first line of input contains integers _n_ and _h_ (2u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_h_u2009≤u2009108). In the second line there is a list of _n_ space-separated integers representing _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009108). Output The first line of output should contain the required minimum goodness. The second line describes the optimal partition. You should print _n_ whitespace-separated integers in the second line. The _i_-th integer is 1 if _a__i_ is in the first subsequence otherwise it should be 2. If there are several possible correct answers you are allowed to print any of them. Note In the first sample the values of _f_ are as follows: _f_(1,u20092)u2009=u20091u2009+u20092u2009+u20092u2009=u20095, _f_(1,u20093)u2009=u20091u2009+u20093u2009+u20092u2009=u20096 and _f_(2,u20093)u2009=u20092u2009+u20093u2009=u20095. So the difference between maximum and minimum values of _f_ is 1. In the second sample the value of _h_ is large, so it's better for one of the sub-sequences to be empty.
1,800
false
false
false
false
false
true
false
false
false
false
8,883
1077A
A frog is currently at the point $$$0$$$ on a coordinate axis $$$Ox$$$. It jumps by the following algorithm: the first jump is $$$a$$$ units to the right, the second jump is $$$b$$$ units to the left, the third jump is $$$a$$$ units to the right, the fourth jump is $$$b$$$ units to the left, and so on. Formally: if the frog has jumped an even number of times (before the current jump), it jumps from its current position $$$x$$$ to position $$$x+a$$$; otherwise it jumps from its current position $$$x$$$ to position $$$x-b$$$. Your task is to calculate the position of the frog after $$$k$$$ jumps. But... One more thing. You are watching $$$t$$$ different frogs so you have to answer $$$t$$$ independent queries. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of queries. Each of the next $$$t$$$ lines contain queries (one query per line). The query is described as three space-separated integers $$$a, b, k$$$ ($$$1 le a, b, k le 10^9$$$) — the lengths of two types of jumps and the number of jumps, respectively. Output Print $$$t$$$ integers. The $$$i$$$-th integer should be the answer for the $$$i$$$-th query. Example Input 6 5 2 3 100 1 4 1 10 5 1000000000 1 6 1 1 1000000000 1 1 999999999 Output 8 198 -17 2999999997 0 1 Note In the first query frog jumps $$$5$$$ to the right, $$$2$$$ to the left and $$$5$$$ to the right so the answer is $$$5 - 2 + 5 = 8$$$. In the second query frog jumps $$$100$$$ to the right, $$$1$$$ to the left, $$$100$$$ to the right and $$$1$$$ to the left so the answer is $$$100 - 1 + 100 - 1 = 198$$$. In the third query the answer is $$$1 - 10 + 1 - 10 + 1 = -17$$$. In the fourth query the answer is $$$10^9 - 1 + 10^9 - 1 + 10^9 - 1 = 2999999997$$$. In the fifth query all frog's jumps are neutralized by each other so the answer is $$$0$$$. The sixth query is the same as the fifth but without the last jump so the answer is $$$1$$$.
800
true
false
false
false
false
false
false
false
false
false
5,337
818D
Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another. The game rules are like this. Firstly Alice chooses some color _A_, then Bob chooses some color _B_ (_A_u2009≠u2009_B_). After each car they update the number of cars of their chosen color that have run past them. Let's define this numbers after _i_-th car _cnt__A_(_i_) and _cnt__B_(_i_). If _cnt__A_(_i_)u2009>u2009_cnt__B_(_i_) for every _i_ then the winner is Alice. If _cnt__B_(_i_)u2009≥u2009_cnt__A_(_i_) for every _i_ then the winner is Bob. Otherwise it's a draw. Bob knows all the colors of cars that they will encounter and order of their appearance. Alice have already chosen her color _A_ and Bob now wants to choose such color _B_ that he will win the game (draw is not a win). Help him find this color. If there are multiple solutions, print any of them. If there is no such color then print -1. Input The first line contains two integer numbers _n_ and _A_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_A_u2009≤u2009106) – number of cars and the color chosen by Alice. The second line contains _n_ integer numbers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u2009106) — colors of the cars that Alice and Bob will encounter in the order of their appearance. Output Output such color _B_ (1u2009≤u2009_B_u2009≤u2009106) that if Bob chooses it then he will win the game. If there are multiple solutions, print any of them. If there is no such color then print -1. It is guaranteed that if there exists any solution then there exists solution with (1u2009≤u2009_B_u2009≤u2009106). Note Let's consider availability of colors in the first example: _cnt_2(_i_)u2009≥u2009_cnt_1(_i_) for every _i_, and color 2 can be the answer. _cnt_4(2)u2009<u2009_cnt_1(2), so color 4 isn't the winning one for Bob. All the other colors also have _cnt__j_(2)u2009<u2009_cnt_1(2), thus they are not available. In the third example every color is acceptable except for 10.
1,700
false
false
true
false
true
false
false
false
false
false
6,484
1601C
You are given two arrays of integers $$$a_1, a_2, ldots, a_n$$$ and $$$b_1, b_2, ldots, b_m$$$. You need to insert all elements of $$$b$$$ into $$$a$$$ in an arbitrary way. As a result you will get an array $$$c_1, c_2, ldots, c_{n+m}$$$ of size $$$n + m$$$. Note that you are not allowed to change the order of elements in $$$a$$$, while you can insert elements of $$$b$$$ at arbitrary positions. They can be inserted at the beginning, between any elements of $$$a$$$, or at the end. Moreover, elements of $$$b$$$ can appear in the resulting array in any order. What is the minimum possible number of inversions in the resulting array $$$c$$$? Recall that an inversion is a pair of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$c_i > c_j$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^6$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 leq b_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ for all tests cases in one input doesn't exceed $$$10^6$$$. The sum of $$$m$$$ for all tests cases doesn't exceed $$$10^6$$$ as well. Output For each test case, print one integerxa0— the minimum possible number of inversions in the resulting array $$$c$$$. Example Input 3 3 4 1 2 3 4 3 2 1 3 3 3 2 1 1 2 3 5 4 1 3 5 3 1 4 3 6 1 Note Below is given the solution to get the optimal answer for each of the example test cases (elements of $$$a$$$ are underscored). In the first test case, $$$c = [underline{1}, 1, underline{2}, 2, underline{3}, 3, 4]$$$. In the second test case, $$$c = [1, 2, underline{3}, underline{2}, underline{1}, 3]$$$. In the third test case, $$$c = [underline{1}, 1, 3, underline{3}, underline{5}, underline{3}, underline{1}, 4, 6]$$$.
2,300
false
true
false
true
true
false
false
false
true
false
2,649
785A
Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons: Tetrahedron. Tetrahedron has 4 triangular faces. Cube. Cube has 6 square faces. Octahedron. Octahedron has 8 triangular faces. Dodecahedron. Dodecahedron has 12 pentagonal faces. Icosahedron. Icosahedron has 20 triangular faces. All five kinds of polyhedrons are shown on the picture below: Anton has a collection of _n_ polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number! Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of polyhedrons in Anton's collection. Each of the following _n_ lines of the input contains a string _s__i_xa0— the name of the _i_-th polyhedron in Anton's collection. The string can look like this: "Tetrahedron" (without quotes), if the _i_-th polyhedron in Anton's collection is a tetrahedron. "Cube" (without quotes), if the _i_-th polyhedron in Anton's collection is a cube. "Octahedron" (without quotes), if the _i_-th polyhedron in Anton's collection is an octahedron. "Dodecahedron" (without quotes), if the _i_-th polyhedron in Anton's collection is a dodecahedron. "Icosahedron" (without quotes), if the _i_-th polyhedron in Anton's collection is an icosahedron. Output Output one numberxa0— the total number of faces in all the polyhedrons in Anton's collection. Examples Input 4 Icosahedron Cube Tetrahedron Dodecahedron Input 3 Dodecahedron Octahedron Octahedron Note In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20u2009+u20096u2009+u20094u2009+u200912u2009=u200942 faces.
800
false
false
true
false
false
false
false
false
false
false
6,633
630I
To quickly hire highly skilled specialists one of the new IT City companies made an unprecedented move. Every employee was granted a car, and an employee can choose one of four different car makes. The parking lot before the office consists of one line of (2_n_u2009-u20092) parking spaces. Unfortunately the total number of cars is greater than the parking lot capacity. Furthermore even amount of cars of each make is greater than the amount of parking spaces! That's why there are no free spaces on the parking lot ever. Looking on the straight line of cars the company CEO thought that parking lot would be more beautiful if it contained exactly _n_ successive cars of the same make. Help the CEO determine the number of ways to fill the parking lot this way. Input The only line of the input contains one integer _n_ (3u2009≤u2009_n_u2009≤u200930) — the amount of successive cars of the same make. Output Output one integer — the number of ways to fill the parking lot by cars of four makes using the described way. Note Let's denote car makes in the following way: A — Aston Martin, B — Bentley, M — Mercedes-Maybach, Z — Zaporozhets. For _n_u2009=u20093 there are the following appropriate ways to fill the parking lot: AAAB AAAM AAAZ ABBB AMMM AZZZ BBBA BBBM BBBZ BAAA BMMM BZZZ MMMA MMMB MMMZ MAAA MBBB MZZZ ZZZA ZZZB ZZZM ZAAA ZBBB ZMMM Originally it was planned to grant sport cars of Ferrari, Lamborghini, Maserati and Bugatti makes but this idea was renounced because it is impossible to drive these cars having small road clearance on the worn-down roads of IT City.
1,700
true
false
false
false
false
false
false
false
false
false
7,296
1987B
You are given an array of integers $$$a$$$ of length $$$n$$$. You can apply the following operation any number of times (maybe, zero): First, choose an integer $$$k$$$ such that $$$1 le k le n$$$ and pay $$$k + 1$$$ coins. Then, choose exactly $$$k$$$ indices such that $$$1 le i_1 < i_2 < ldots < i_k le n$$$. Then, for each $$$x$$$ from $$$1$$$ to $$$k$$$, increase $$$a_{i_x}$$$ by $$$1$$$. Find the minimum number of coins needed to make $$$a$$$ non-decreasing. That is, $$$a_1 le a_2 le ldots le a_n$$$. 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, ldots, 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 a single integerxa0— the minimum number of coins needed to make $$$a$$$ non-decreasing. Example Input 5 3 1 7 9 5 2 1 4 7 6 4 1 3 2 4 1 179 9 344 12 37 60 311 613 365 328 675 Note In the first test case, $$$a$$$ is already sorted, so you don't have to spend any coins. In the second test case, the optimal sequence of operations is: Choose $$$k = 2$$$ and the indices $$$2$$$ and $$$5$$$: $$$[ 2, color{red}{1}, 4, 7, color{red}{6} ] ightarrow [2, 2, 4, 7, 7]$$$. This costs $$$3$$$ coins. It can be proven that it is not possible to make $$$a$$$ non-decreasing by spending less than $$$3$$$ coins.
1,000
false
true
false
false
false
false
false
false
false
false
356
569A
Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city of Ekaterinozavodsk and the song takes a lot of time to download. But Lesha is quite impatient. The song's duration is _T_ seconds. Lesha downloads the first _S_ seconds of the song and plays it. When the playback reaches the point that has not yet been downloaded, Lesha immediately plays the song from the start (the loaded part of the song stays in his phone, and the download is continued from the same place), and it happens until the song is downloaded completely and Lesha listens to it to the end. For _q_ seconds of real time the Internet allows you to download _q_u2009-u20091 seconds of the track. Tell Lesha, for how many times he will start the song, including the very first start. Input The single line contains three integers _T_,u2009_S_,u2009_q_ (2u2009≤u2009_q_u2009≤u2009104, 1u2009≤u2009_S_u2009<u2009_T_u2009≤u2009105). Output Print a single integerxa0— the number of times the song will be restarted. Note In the first test, the song is played twice faster than it is downloaded, which means that during four first seconds Lesha reaches the moment that has not been downloaded, and starts the song again. After another two seconds, the song is downloaded completely, and thus, Lesha starts the song twice. In the second test, the song is almost downloaded, and Lesha will start it only once. In the third sample test the download finishes and Lesha finishes listening at the same moment. Note that song isn't restarted in this case.
1,500
true
false
true
false
false
false
false
false
false
false
7,573
1766C
Sir Monocarp Hamilton is planning to paint his wall. The wall can be represented as a grid, consisting of $$$2$$$ rows and $$$m$$$ columns. Initially, the wall is completely white. Monocarp wants to paint a black picture on the wall. In particular, he wants cell $$$(i, j)$$$ (the $$$j$$$-th cell in the $$$i$$$-th row) to be colored black, if $$$c_{i, j} =$$$ 'B', and to be left white, if $$$c_{i, j} =$$$ 'W'. Additionally, he wants each column to have at least one black cell, so, for each $$$j$$$, the following constraint is satisfied: $$$c_{1, j}$$$, $$$c_{2, j}$$$ or both of them will be equal to 'B'. In order for the picture to turn out smooth, Monocarp wants to place down a paint brush in some cell $$$(x_1, y_1)$$$ and move it along the path $$$(x_1, y_1), (x_2, y_2), dots, (x_k, y_k)$$$ so that: for each $$$i$$$, $$$(x_i, y_i)$$$ and $$$(x_{i+1}, y_{i+1})$$$ share a common side; all black cells appear in the path exactly once; white cells don't appear in the path. Determine if Monocarp can paint the wall. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$)xa0— the number of columns in the wall. The $$$i$$$-th of the next two lines contains a string $$$c_i$$$, consisting of $$$m$$$ characters, where each character is either 'B' or 'W'. $$$c_{i, j}$$$ is 'B', if the cell $$$(i, j)$$$ should be colored black, and 'W', if the cell $$$(i, j)$$$ should be left white. Additionally, for each $$$j$$$, the following constraint is satisfied: $$$c_{1, j}$$$, $$$c_{2, j}$$$ or both of them are equal to 'B'. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print "YES" if Monocarp can paint a wall. Otherwise, print "NO". Example Input 6 3 WBB BBW 1 B B 5 BWBWB BBBBB 2 BW WB 5 BBBBW BWBBB 6 BWBBWB BBBBBB Output YES YES NO NO NO YES Note In the first testcase, Monocarp can follow a path $$$(2, 1)$$$, $$$(2, 2)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$ with his brush. All black cells appear in the path exactly once, no white cells appear in the path. In the second testcase, Monocarp can follow a path $$$(1, 1)$$$, $$$(2, 1)$$$. In the third testcase: the path $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(1, 3)$$$, $$$(2, 4)$$$, $$$(2, 5)$$$, $$$(1, 5)$$$ doesn't suffice because a pair of cells $$$(1, 3)$$$ and $$$(2, 4)$$$ doesn't share a common side; the path $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(2, 5)$$$, $$$(1, 5)$$$ doesn't suffice because cell $$$(2, 3)$$$ is visited twice; the path $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(2, 5)$$$, $$$(1, 5)$$$ doesn't suffice because a black cell $$$(1, 3)$$$ doesn't appear in the path; the path $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(2, 5)$$$, $$$(1, 5)$$$, $$$(1, 4)$$$, $$$(1, 3)$$$ doesn't suffice because a white cell $$$(1, 4)$$$ appears in the path.
1,300
false
false
true
true
false
false
false
false
false
false
1,701
559B
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings _a_ and _b_ of equal length are called equivalent in one of the two cases: 1. They are equal. 2. If we split string _a_ into two halves of the same size _a_1 and _a_2, and string _b_ into two halves of the same size _b_1 and _b_2, then one of the following is correct: 1. _a_1 is equivalent to _b_1, and _a_2 is equivalent to _b_2 2. _a_1 is equivalent to _b_2, and _a_2 is equivalent to _b_1 As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent. Gerald has already completed this home task. Now it's your turn! Input The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200u2009000 and consists of lowercase English letters. The strings have the same length. Output Print "YES" (without the quotes), if these two strings are equivalent, and "NO" (without the quotes) otherwise. Note In the first sample you should split the first string into strings "aa" and "ba", the second one — into strings "ab" and "aa". "aa" is equivalent to "aa"; "ab" is equivalent to "ba" as "ab" = "a" + "b", "ba" = "b" + "a". In the second sample the first string can be splitted into strings "aa" and "bb", that are equivalent only to themselves. That's why string "aabb" is equivalent only to itself and to string "bbaa".
1,700
false
false
false
false
false
false
false
false
true
false
7,597
862E
Dr. Evil is interested in math and functions, so he gave Mahmoud and Ehab array _a_ of length _n_ and array _b_ of length _m_. He introduced a function _f_(_j_) which is defined for integers _j_, which satisfy 0u2009≤u2009_j_u2009≤u2009_m_u2009-u2009_n_. Suppose, _c__i_u2009=u2009_a__i_u2009-u2009_b__i_u2009+u2009_j_. Then _f_(_j_)u2009=u2009_c_1u2009-u2009_c_2u2009+u2009_c_3u2009-u2009_c_4... _c__n_. More formally, . Dr. Evil wants Mahmoud and Ehab to calculate the minimum value of this function over all valid _j_. They found it a bit easy, so Dr. Evil made their task harder. He will give them _q_ update queries. During each update they should add an integer _x__i_ to all elements in _a_ in range [_l__i_;_r__i_] i.e. they should add _x__i_ to _a__l__i_,u2009_a__l__i_u2009+u20091,u2009... ,u2009_a__r__i_ and then they should calculate the minimum value of _f_(_j_) for all valid _j_. Please help Mahmoud and Ehab. Input The first line contains three integers _n_,u2009_m_ and _q_ (1u2009≤u2009_n_u2009≤u2009_m_u2009≤u2009105, 1u2009≤u2009_q_u2009≤u2009105)xa0— number of elements in _a_, number of elements in _b_ and number of queries, respectively. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_. (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109)xa0— elements of _a_. The third line contains _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_. (u2009-u2009109u2009≤u2009_b__i_u2009≤u2009109)xa0— elements of _b_. Then _q_ lines follow describing the queries. Each of them contains three integers _l__i_ _r__i_ _x__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_, u2009-u2009109u2009≤u2009_x_u2009≤u2009109)xa0— range to be updated and added value. Output The first line should contain the minimum value of the function _f_ before any update. Then output _q_ lines, the _i_-th of them should contain the minimum value of the function _f_ after performing the _i_-th update . Example Input 5 6 3 1 2 3 4 5 1 2 3 4 5 6 1 1 10 1 1 -9 1 5 -1 Note For the first example before any updates it's optimal to choose _j_u2009=u20090, _f_(0)u2009=u2009(1u2009-u20091)u2009-u2009(2u2009-u20092)u2009+u2009(3u2009-u20093)u2009-u2009(4u2009-u20094)u2009+u2009(5u2009-u20095)u2009=u20090u2009=u20090. After the first update _a_ becomes {11,u20092,u20093,u20094,u20095} and it's optimal to choose _j_u2009=u20091, _f_(1)u2009=u2009(11u2009-u20092)u2009-u2009(2u2009-u20093)u2009+u2009(3u2009-u20094)u2009-u2009(4u2009-u20095)u2009+u2009(5u2009-u20096)u2009=u20099u2009=u20099. After the second update _a_ becomes {2,u20092,u20093,u20094,u20095} and it's optimal to choose _j_u2009=u20091, _f_(1)u2009=u2009(2u2009-u20092)u2009-u2009(2u2009-u20093)u2009+u2009(3u2009-u20094)u2009-u2009(4u2009-u20095)u2009+u2009(5u2009-u20096)u2009=u20090u2009=u20090. After the third update _a_ becomes {1,u20091,u20092,u20093,u20094} and it's optimal to choose _j_u2009=u20090, _f_(0)u2009=u2009(1u2009-u20091)u2009-u2009(1u2009-u20092)u2009+u2009(2u2009-u20093)u2009-u2009(3u2009-u20094)u2009+u2009(4u2009-u20095)u2009=u20090u2009=u20090.
2,100
false
false
false
false
true
false
false
true
true
false
6,296
1155E
Jury picked a polynomial $$$f(x) = a_0 + a_1 cdot x + a_2 cdot x^2 + dots + a_k cdot x^k$$$. $$$k le 10$$$ and all $$$a_i$$$ are integer numbers and $$$0 le a_i < 10^6 + 3$$$. It's guaranteed that there is at least one $$$i$$$ such that $$$a_i > 0$$$. Now jury wants you to find such an integer $$$x_0$$$ that $$$f(x_0) equiv 0 mod (10^6 + 3)$$$ or report that there is not such $$$x_0$$$. You can ask no more than $$$50$$$ queries: you ask value $$$x_q$$$ and jury tells you value $$$f(x_q) mod (10^6 + 3)$$$. Note that printing the answer doesn't count as a query. Interaction To ask a question, print "? $$$x_q$$$" $$$(0 le x_q < 10^6 + 3)$$$. The judge will respond with a single integer $$$f(x_q) mod (10^6 + 3)$$$. If you ever get a result of $$$−1$$$ (because you printed an invalid query), exit immediately to avoid getting other verdicts. 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. When you are ready to answer, print "! $$$x_0$$$" where $$$x_0$$$ is the answer or $$$-1$$$ if there is no such $$$x_0$$$. You can ask at most $$$50$$$ questions per test case. Hack Format To hack, use the following format. The only line should contain $$$11$$$ integers $$$a_0, a_1, dots, a_{10}$$$ ($$$0 le a_i < 10^6 + 3$$$, $$$maxlimits_{0 le i le 10}{a_i} > 0$$$) — corresponding coefficients of the polynomial. Note The polynomial in the first sample is $$$1000002 + x^2$$$. The polynomial in the second sample is $$$1 + x^2$$$.
2,200
true
false
false
false
false
false
true
false
false
false
4,932
1346C
Tanya wants to organize her bookcase. There are $$$n$$$ bookshelves in the bookcase, the $$$i$$$-th bookshelf contains $$$a_i$$$ books on it. Tanya will be satisfied if each bookshelf contains no more than $$$k$$$ books. Tanya can do one of the two following operations to achieve her goal: 1. Choose exactly one bookshelf and put all the books from it in the storage room (i.u2009e. choose some $$$i$$$ and assign $$$a_i := 0$$$). During this operation she spends $$$x$$$ seconds. 2. Take all books from all $$$n$$$ bookshelves and distribute them between all $$$n$$$ bookshelves evenly (the definition of the term is given below). During this operation she spends $$$y$$$ seconds. Consider the sequence $$$a$$$ of $$$n$$$ integers. Then its even distribution is such a sequence $$$b$$$ of $$$n$$$ integers that the sum of $$$b$$$ equals the sum of $$$a$$$ and the value $$$max(b) - min(b)$$$ is the minimum possible. For example, if the array $$$a=[5, 4, 3]$$$ then its even distribution is $$$b=[4, 4, 4]$$$. If $$$a=[1, 2, 3, 4]$$$ then its even distribution is $$$b=[2, 3, 3, 2]$$$ (or any permutation of this array). Your task is to find the minimum number of seconds Tanya has to spend to obtain the bookcase with no more than $$$k$$$ books on each bookshelf. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains four integers $$$n, k, x$$$ and $$$y$$$ ($$$1 le k le n le 2 cdot 10^5; 1 le x, y le 10^4$$$)xa0— the number of bookshelves, the maximum required number of books on each bookshelf and the number of seconds Tanya spends during the first and the second operation respectively. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ is the number of books on the $$$i$$$-th bookshelf. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Note In the first test case of the example, it's optimal to use the first operation on the fifth bookshelf. So the array $$$a$$$ becomes $$$[1, 2, 2, 3, 5] ightarrow [1, 2, 2, 3, 0]$$$. In the second test case of the example, it's optimal to use the first operation on the second bookshelf and then use the second operation. So the array $$$a$$$ becomes $$$[1, 5, 1, 5, 5] ightarrow [1, 0, 1, 5, 5] ightarrow [2, 2, 3, 3, 2]$$$. In the third test case of the example, it's optimal to use the second operation. So the array $$$a$$$ becomes $$$[1, 2, 5, 3, 5] ightarrow [4, 3, 3, 3, 3]$$$. In the fourth test case of the example, it's optimal to use the first operation on the first and the second bookshelves. So the array $$$a$$$ becomes $$$[4, 4, 1, 1] ightarrow [0, 0, 1, 1]$$$. In the fifth test case of the example, it's optimal to use the second operation. So the array $$$a$$$ becomes $$$[4, 4, 1, 1] ightarrow [2, 3, 2, 3]$$$.
1,600
false
true
false
false
false
false
false
false
true
false
3,977
1583D
It turns out that the meaning of life is a permutation $$$p_1, p_2, ldots, p_n$$$ of the integers $$$1, 2, ldots, n$$$ ($$$2 leq n leq 100$$$). Omkar, having created all life, knows this permutation, and will allow you to figure it out using some queries. A query consists of an array $$$a_1, a_2, ldots, a_n$$$ of integers between $$$1$$$ and $$$n$$$. $$$a$$$ is not required to be a permutation. Omkar will first compute the pairwise sum of $$$a$$$ and $$$p$$$, meaning that he will compute an array $$$s$$$ where $$$s_j = p_j + a_j$$$ for all $$$j = 1, 2, ldots, n$$$. Then, he will find the smallest index $$$k$$$ such that $$$s_k$$$ occurs more than once in $$$s$$$, and answer with $$$k$$$. If there is no such index $$$k$$$, then he will answer with $$$0$$$. You can perform at most $$$2n$$$ queries. Figure out the meaning of life $$$p$$$. Interaction Start the interaction by reading single integer $$$n$$$ ($$$2 leq n leq 100$$$) xa0— the length of the permutation $$$p$$$. You can then make queries. A query consists of a single line "$$$? enspace a_1 enspace a_2 enspace ldots enspace a_n$$$" ($$$1 leq a_j leq n$$$). The answer to each query will be a single integer $$$k$$$ as described above ($$$0 leq k leq n$$$). After making 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. To output your answer, print a single line "$$$! enspace p_1 enspace p_2 enspace ldots enspace p_n$$$" then terminate. You can make at most $$$2n$$$ queries. Outputting the answer does not count as a query. Hack Format To hack, first output a line containing $$$n$$$ ($$$2 leq n leq 100$$$), then output another line containing the hidden permutation $$$p_1, p_2, ldots, p_n$$$ of numbers from $$$1$$$ to $$$n$$$. Note In the sample, the hidden permutation $$$p$$$ is $$$[3, 2, 1, 5, 4]$$$. Three queries were made. The first query is $$$a = [4, 4, 2, 3, 2]$$$. This yields $$$s = [3 + 4, 2 + 4, 1 + 2, 5 + 3, 4 + 2] = [7, 6, 3, 8, 6]$$$. $$$6$$$ is the only number that appears more than once, and it appears first at index $$$2$$$, making the answer to the query $$$2$$$. The second query is $$$a = [3, 5, 1, 5, 5]$$$. This yields $$$s = [3 + 3, 2 + 5, 1 + 1, 5 + 5, 4 + 5] = [6, 7, 2, 10, 9]$$$. There are no numbers that appear more than once here, so the answer to the query is $$$0$$$. The third query is $$$a = [5, 2, 4, 3, 1]$$$. This yields $$$s = [3 + 5, 2 + 2, 1 + 4, 5 + 3, 4 + 1] = [8, 4, 5, 8, 5]$$$. $$$5$$$ and $$$8$$$ both occur more than once here. $$$5$$$ first appears at index $$$3$$$, while $$$8$$$ first appears at index $$$1$$$, and $$$1 < 3$$$, making the answer to the query $$$1$$$. Note that the sample is only meant to provide an example of how the interaction works; it is not guaranteed that the above queries represent a correct strategy with which to determine the answer.
1,800
false
true
false
false
false
true
false
false
false
false
2,714
1538D
You are given two integers $$$a$$$ and $$$b$$$. In one turn, you can do one of the following operations: Take an integer $$$c$$$ ($$$c > 1$$$ and $$$a$$$ should be divisible by $$$c$$$) and replace $$$a$$$ with $$$frac{a}{c}$$$; Take an integer $$$c$$$ ($$$c > 1$$$ and $$$b$$$ should be divisible by $$$c$$$) and replace $$$b$$$ with $$$frac{b}{c}$$$. Your goal is to make $$$a$$$ equal to $$$b$$$ using exactly $$$k$$$ turns. For example, the numbers $$$a=36$$$ and $$$b=48$$$ can be made equal in $$$4$$$ moves: $$$c=6$$$, divide $$$b$$$ by $$$c$$$ $$$Rightarrow$$$ $$$a=36$$$, $$$b=8$$$; $$$c=2$$$, divide $$$a$$$ by $$$c$$$ $$$Rightarrow$$$ $$$a=18$$$, $$$b=8$$$; $$$c=9$$$, divide $$$a$$$ by $$$c$$$ $$$Rightarrow$$$ $$$a=2$$$, $$$b=8$$$; $$$c=4$$$, divide $$$b$$$ by $$$c$$$ $$$Rightarrow$$$ $$$a=2$$$, $$$b=2$$$. For the given numbers $$$a$$$ and $$$b$$$, determine whether it is possible to make them equal using exactly $$$k$$$ turns. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. Each test case is contains three integers $$$a$$$, $$$b$$$ and $$$k$$$ ($$$1 le a, b, k le 10^9$$$). Output For each test case output: "Yes", if it is possible to make the numbers $$$a$$$ and $$$b$$$ equal in exactly $$$k$$$ turns; "No" otherwise. The strings "Yes" and "No" can be output in any case. Example Input 8 36 48 2 36 48 3 36 48 4 2 8 1 2 8 2 1000000000 1000000000 1000000000 1 2 1 2 2 1 Output YES YES YES YES YES NO YES NO
1,700
true
false
false
false
false
true
false
false
false
false
2,952
1776K
In a parallel universe there are $$$n$$$ chemical elements, numbered from $$$1$$$ to $$$n$$$. The element number $$$n$$$ has not been discovered so far, and its discovery would be a pinnacle of research and would bring the person who does it eternal fame and the so-called SWERC prize. There are $$$m$$$ independent researchers, numbered from $$$1$$$ to $$$m$$$, that are trying to discover it. Currently, the $$$i$$$-th researcher has a sample of the element $$$s_i$$$. Every year, each researcher independently does one fusion experiment. In a fusion experiment, if the researcher currently has a sample of element $$$a$$$, they produce a sample of an element $$$b$$$ that is chosen uniformly at random between $$$a+1$$$ and $$$n$$$, and they lose the sample of element $$$a$$$. The elements discovered by different researchers or in different years are completely independent. The first researcher to discover element $$$n$$$ will get the SWERC prize. If several researchers discover the element in the same year, they all get the prize. For each $$$i = 1, , 2, , dots, , m$$$, you need to compute the probability that the $$$i$$$-th researcher wins the prize. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 10^{18}$$$, $$$1 le m le 100$$$) — the number of elements and the number of researchers. The second line contains $$$m$$$ integers $$$s_1, , s_2, , dots, , s_m$$$ ($$$1 le s_i < n$$$) — the elements that the researchers currently have. Output Print $$$m$$$ floating-point numbers. The $$$i$$$-th number should be the probability that the $$$i$$$-th researcher wins the SWERC prize. Your answer is accepted if each number differs from the correct number by at most $$$10^{-8}$$$. Examples Input 100 7 1 2 4 8 16 32 64 Output 0.178593469 0.179810455 0.182306771 0.187565366 0.199300430 0.229356322 0.348722518 Note In the first sample, all researchers will discover element $$$2$$$ in the first year and win the SWERC prize. In the second sample, the last researcher will definitely discover element $$$3$$$ in the first year and win the SWERC prize. The first two researchers have a $$$50%$$$ chance of discovering element $$$2$$$ and a $$$50%$$$ chance of discovering element $$$3$$$, and only element $$$3$$$ will bring them the prize. In the third sample, each researcher has an independent $$$50%$$$ chance of discovering element $$$3$$$ in the first year, in which case they definitely win the SWERC prize. Additionally, if they all discover element $$$2$$$ in the first year, which is a $$$12.5%$$$ chance, then they will all discover element $$$3$$$ in the second year and all win the prize.
3,200
true
false
false
true
false
false
false
false
false
false
1,618
83B
There are _n_ animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number _i_ in the queue will have to visit his office exactly _a__i_ times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home. Doctor plans to go home after receiving _k_ animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. Input The first line of input data contains two space-separated integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_k_u2009≤u20091014). In the second line are given space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). Output If the doctor will overall carry out less than _k_ examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers — number of animals in the order in which they stand in the queue. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. Note In the first sample test: Before examination: {1,u20092,u20093} After the first examination: {2,u20093} After the second examination: {3,u20092} After the third examination: {2} In the second sample test: Before examination: {1,u20092,u20093,u20094,u20095,u20096,u20097} After the first examination: {2,u20093,u20094,u20095,u20096,u20097} After the second examination: {3,u20094,u20095,u20096,u20097,u20092} After the third examination: {4,u20095,u20096,u20097,u20092,u20093} After the fourth examination: {5,u20096,u20097,u20092,u20093} After the fifth examination: {6,u20097,u20092,u20093,u20095} After the sixth examination: {7,u20092,u20093,u20095,u20096} After the seventh examination: {2,u20093,u20095,u20096} After the eighth examination: {3,u20095,u20096,u20092} After the ninth examination: {5,u20096,u20092,u20093} After the tenth examination: {6,u20092,u20093}
1,800
true
false
false
false
false
false
false
true
true
false
9,552
1528B
Problem - 1528B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp math *1700 No tag edit access → Contest materials $$$. Output Print the number of good pairings modulo $$$998244353$$$. Examples Input 1 Output 1 Input 2 Output 3 Input 3 Output 6 Input 100 Output 688750769 Note The good pairings for the second example are: In the third example, the good pairings are:
1,700
true
false
false
true
false
false
false
false
false
false
3,022
1866B
On the trip to campus during the mid semester exam period, Chaneka thinks of two positive integers $$$X$$$ and $$$Y$$$. Since the two integers can be very big, both are represented using their prime factorisations, such that: $$$X=A_1^{B_1} imes A_2^{B_2} imesldots imes A_N^{B_N}$$$ (each $$$A_i$$$ is prime, each $$$B_i$$$ is positive, and $$$A_1<A_2<ldots<A_N$$$) $$$Y=C_1^{D_1} imes C_2^{D_2} imesldots imes C_M^{D_M}$$$ (each $$$C_j$$$ is prime, each $$$D_j$$$ is positive, and $$$C_1<C_2<ldots<C_M$$$) Chaneka ponders about these two integers for too long throughout the trip, so Chaneka's friend commands her "Gece, deh!" (move fast) in order to not be late for the exam. Because of that command, Chaneka comes up with a problem, how many pairs of positive integers $$$p$$$ and $$$q$$$ such that $$$ ext{LCM}(p, q) = X$$$ and $$$ ext{GCD}(p, q) = Y$$$. Since the answer can be very big, output the answer modulo $$$998,244,353$$$. Notes: $$$ ext{LCM}(p, q)$$$ is the smallest positive integer that is simultaneously divisible by $$$p$$$ and $$$q$$$. $$$ ext{GCD}(p, q)$$$ is the biggest positive integer that simultaneously divides $$$p$$$ and $$$q$$$. Input The first line contains a single integer $$$N$$$ ($$$1 leq N leq 10^5$$$) — the number of distinct primes in the prime factorisation of $$$X$$$. The second line contains $$$N$$$ integers $$$A_1, A_2, A_3, ldots, A_N$$$ ($$$2 leq A_1 < A_2 < ldots < A_N leq 2 cdot 10^6$$$; each $$$A_i$$$ is prime) — the primes in the prime factorisation of $$$X$$$. The third line contains $$$N$$$ integers $$$B_1, B_2, B_3, ldots, B_N$$$ ($$$1 leq B_i leq 10^5$$$) — the exponents in the prime factorisation of $$$X$$$. The fourth line contains a single integer $$$M$$$ ($$$1 leq M leq 10^5$$$) — the number of distinct primes in the prime factorisation of $$$Y$$$. The fifth line contains $$$M$$$ integers $$$C_1, C_2, C_3, ldots, C_M$$$ ($$$2 leq C_1 < C_2 < ldots < C_M leq 2 cdot 10^6$$$; each $$$C_j$$$ is prime) — the primes in the prime factorisation of $$$Y$$$. The sixth line contains $$$M$$$ integers $$$D_1, D_2, D_3, ldots, D_M$$$ ($$$1 leq D_j leq 10^5$$$) — the exponents in the prime factorisation of $$$Y$$$. Output An integer representing the number of pairs of positive integers $$$p$$$ and $$$q$$$ such that $$$ ext{LCM}(p, q) = X$$$ and $$$ ext{GCD}(p, q) = Y$$$, modulo $$$998,244,353$$$. Examples Input 4 2 3 5 7 2 1 1 2 2 3 7 1 1 Input 2 1299721 1999993 100000 265 2 1299721 1999993 100000 265 Note In the first example, the integers are as follows: $$$X=2^2 imes3^1 imes5^1 imes7^2=2940$$$ $$$Y=3^1 imes7^1=21$$$ The following are all possible pairs of $$$p$$$ and $$$q$$$: $$$p=21$$$, $$$q=2940$$$ $$$p=84$$$, $$$q=735$$$ $$$p=105$$$, $$$q=588$$$ $$$p=147$$$, $$$q=420$$$ $$$p=420$$$, $$$q=147$$$ $$$p=588$$$, $$$q=105$$$ $$$p=735$$$, $$$q=84$$$ $$$p=2940$$$, $$$q=21$$$ In the third example, the integers are as follows: $$$X=2^1 imes5^1=10$$$ $$$Y=2^1 imes3^1=6$$$ There is no pair $$$p$$$ and $$$q$$$ that simultaneously satisfies $$$ ext{LCM}(p,q)=10$$$ and $$$ ext{GCD}(p,q)=6$$$.
1,400
true
false
false
false
false
false
false
false
false
false
1,087
548A
Problem - 548A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation strings *1100 No tag edit access → Contest materials . The second line contains integer _k_ (1u2009≤u2009_k_u2009≤u20091000). Output Print "YES"(without quotes) if he has worn his own back-bag or "NO"(without quotes) otherwise. Examples Input saba 2 Output NO Input saddastavvat 2 Output YES Note Palindrome is a string reading the same forward and backward. In the second sample, the faxes in his back-bag can be "saddas" and "tavvat".
1,100
false
false
true
false
false
false
true
false
false
false
7,647
256D
Problem - 256D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp *2700 No tag edit access → Contest materials about all the people at the table. The honest people are going to say the correct answer, the liars are going to say any integer from 1 to _n_, which is not the correct answer. Every liar chooses his answer, regardless of the other liars, so two distinct liars may give distinct answer. Serge does not know any information about the people besides their answers to his question. He took a piece of paper and wrote _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_, where _a__i_ is the answer of the _i_-th person in the row. Given this sequence, Serge determined that exactly _k_ people sitting at the table apparently lie. Serge wonders, how many variants of people's answers (sequences of answers _a_ of length _n_) there are where one can say that exactly _k_ people sitting at the table apparently lie. As there can be rather many described variants of answers, count the remainder of dividing the number of the variants by 777777777. Input The first line contains two integers _n_, _k_, (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u200928). It is guaranteed that _n_ — is the power of number 2. Output Print a single integer — the answer to the problem modulo 777777777. Examples Input 1 1 Output 0 Input 2 1 Output 2
2,700
false
false
false
true
false
false
false
false
false
false
8,809
1736E
Problem - 1736E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp greedy *2600 No tag edit access → Contest materials ") Editorial") after swapping. In either case(whether you swap or not), after this you add $$$a_i$$$ to your score. What's the maximum possible score you can get? Input The first line contains a single integer $$$n$$$ ($$$2 le n le 500$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$). Output Print a single integerxa0— the maximum possible score. Examples Input 2 3 1 Output 6 Input 5 7 3 9 6 12 Output 52 Note In the first example, to get the maximum score we do as follows. Do nothing on the first turn, add $$$3$$$ to the score. Swap the first and the second elements and turn $$$1$$$ to $$$0$$$ on the second turn, and add $$$3$$$ to the score. The final score is $$$6$$$.
2,600
false
true
false
true
false
false
false
false
false
false
1,869
1162B
You are given two $$$n imes m$$$ matrices containing integers. A sequence of integers is strictly increasing if each next number is greater than the previous one. A row is strictly increasing if all numbers from left to right are strictly increasing. A column is strictly increasing if all numbers from top to bottom are strictly increasing. A matrix is increasing if all rows are strictly increasing and all columns are strictly increasing. For example, the matrix $$$begin{bmatrix} 9&10&11 11&12&14 end{bmatrix}$$$ is increasing because each individual row and column is strictly increasing. On the other hand, the matrix $$$begin{bmatrix} 1&1 2&3 end{bmatrix}$$$ is not increasing because the first row is not strictly increasing. Let a position in the $$$i$$$-th row (from top) and $$$j$$$-th column (from left) in a matrix be denoted as $$$(i, j)$$$. In one operation, you can choose any two numbers $$$i$$$ and $$$j$$$ and swap the number located in $$$(i, j)$$$ in the first matrix with the number in $$$(i, j)$$$ in the second matrix. In other words, you can swap two numbers in different matrices if they are located in the corresponding positions. You would like to make both matrices increasing by performing some number of operations (possibly none). Determine if it is possible to do this. If it is, print "Possible", otherwise, print "Impossible". Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n,m leq 50$$$)xa0— the dimensions of each matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, ldots, a_{im}$$$ ($$$1 leq a_{ij} leq 10^9$$$)xa0— the number located in position $$$(i, j)$$$ in the first matrix. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i1}, b_{i2}, ldots, b_{im}$$$ ($$$1 leq b_{ij} leq 10^9$$$)xa0— the number located in position $$$(i, j)$$$ in the second matrix. Output Print a string "Impossible" or "Possible". Examples Input 2 2 2 10 11 5 9 4 3 12 Input 2 3 2 4 5 4 5 6 3 6 7 8 10 11 Input 3 2 1 3 2 4 5 10 3 1 3 6 4 8 Note The first example, we can do an operation on the top left and bottom right cells of the matrices. The resulting matrices will be $$$begin{bmatrix} 9&10 11&12 end{bmatrix}$$$ and $$$begin{bmatrix} 2&4 3&5 end{bmatrix}$$$. In the second example, we don't need to do any operations. In the third example, no matter what we swap, we can't fix the first row to be strictly increasing in both matrices.
1,400
false
true
false
false
false
false
true
false
false
false
4,905
676D
Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size _n_u2009×u2009_m_ and consists of blocks of size 1u2009×u20091. Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees clockwise or pass to the neighbouring block. Theseus can go from block _A_ to some neighbouring block _B_ only if block _A_ has a door that leads to block _B_ and block _B_ has a door that leads to block _A_. Theseus found an entrance to labyrinth and is now located in block (_x__T_,u2009_y__T_)xa0— the block in the row _x__T_ and column _y__T_. Theseus know that the Minotaur is hiding in block (_x__M_,u2009_y__M_) and wants to know the minimum number of minutes required to get there. Theseus is a hero, not a programmer, so he asks you to help him. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000)xa0— the number of rows and the number of columns in labyrinth, respectively. Each of the following _n_ lines contains _m_ characters, describing the blocks of the labyrinth. The possible characters are: «+» means this block has 4 doors (one door to each neighbouring block); «-» means this block has 2 doorsxa0— to the left and to the right neighbours; «» means this block has 2 doorsxa0— to the top and to the bottom neighbours; «^» means this block has 1 doorxa0— to the top neighbour; «>» means this block has 1 doorxa0— to the right neighbour; «<» means this block has 1 doorxa0— to the left neighbour; «v» means this block has 1 doorxa0— to the bottom neighbour; «L» means this block has 3 doorsxa0— to all neighbours except left one; «R» means this block has 3 doorsxa0— to all neighbours except right one; «U» means this block has 3 doorsxa0— to all neighbours except top one; «D» means this block has 3 doorsxa0— to all neighbours except bottom one; «*» means this block is a wall and has no doors. Left, right, top and bottom are defined from representing labyrinth as a table, where rows are numbered from 1 to _n_ from top to bottom and columns are numbered from 1 to _m_ from left to right. Next line contains two integersxa0— coordinates of the block (_x__T_,u2009_y__T_) (1u2009≤u2009_x__T_u2009≤u2009_n_, 1u2009≤u2009_y__T_u2009≤u2009_m_), where Theseus is initially located. Last line contains two integersxa0— coordinates of the block (_x__M_,u2009_y__M_) (1u2009≤u2009_x__M_u2009≤u2009_n_, 1u2009≤u2009_y__M_u2009≤u2009_m_), where Minotaur hides. It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block. Output If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding.
2,000
false
false
true
false
false
false
false
false
false
true
7,123
1271F
Recently a lot of students were enrolled in Berland State University. All students were divided into groups according to their education program. Some groups turned out to be too large to attend lessons in the same auditorium, so these groups should be divided into two subgroups. Your task is to help divide the first-year students of the computer science faculty. There are $$$t$$$ new groups belonging to this faculty. Students have to attend classes on three different subjects — maths, programming and P.u2009E. All classes are held in different places according to the subject — maths classes are held in auditoriums, programming classes are held in computer labs, and P. E. classes are held in gyms. Each group should be divided into two subgroups so that there is enough space in every auditorium, lab or gym for all students of the subgroup. For the first subgroup of the $$$i$$$-th group, maths classes are held in an auditorium with capacity of $$$a_{i, 1}$$$ students; programming classes are held in a lab that accomodates up to $$$b_{i, 1}$$$ students; and P.u2009E. classes are held in a gym having enough place for $$$c_{i, 1}$$$ students. Analogically, the auditorium, lab and gym for the second subgroup can accept no more than $$$a_{i, 2}$$$, $$$b_{i, 2}$$$ and $$$c_{i, 2}$$$ students, respectively. As usual, some students skip some classes. Each student considers some number of subjects (from $$$0$$$ to $$$3$$$) to be useless — that means, he skips all classes on these subjects (and attends all other classes). This data is given to you as follows — the $$$i$$$-th group consists of: 1. $$$d_{i, 1}$$$ students which attend all classes; 2. $$$d_{i, 2}$$$ students which attend all classes, except for P.u2009E.; 3. $$$d_{i, 3}$$$ students which attend all classes, except for programming; 4. $$$d_{i, 4}$$$ students which attend only maths classes; 5. $$$d_{i, 5}$$$ students which attend all classes, except for maths; 6. $$$d_{i, 6}$$$ students which attend only programming classes; 7. $$$d_{i, 7}$$$ students which attend only P.u2009E. There is one more type of students — those who don't attend any classes at all (but they, obviously, don't need any place in auditoriums, labs or gyms, so the number of those students is insignificant in this problem). Your task is to divide each group into two subgroups so that every auditorium (or lab, or gym) assigned to each subgroup has enough place for all students from this subgroup attending the corresponding classes (if it is possible). Each student of the $$$i$$$-th group should belong to exactly one subgroup of the $$$i$$$-th group; it is forbidden to move students between groups. Input The first line contains one integer $$$t$$$ ($$$1 le t le 300$$$) — the number of groups. Then the descriptions of groups follow. The description of the $$$i$$$-th group consists of three lines: the first line contains three integers $$$a_{i, 1}$$$, $$$b_{i, 1}$$$ and $$$c_{i, 1}$$$ ($$$1 le a_{i, 1}, b_{i, 1}, c_{i, 1} le 3000$$$) — the capacity of the auditorium, lab and gym assigned to the first subgroup of the $$$i$$$-th group, respectively; the second line contains three integers $$$a_{i, 2}$$$, $$$b_{i, 2}$$$ and $$$c_{i, 2}$$$ ($$$1 le a_{i, 2}, b_{i, 2}, c_{i, 2} le 3000$$$) — the capacity of the auditorium, lab and gym assigned to the second subgroup of the $$$i$$$-th group, respectively; the third line contains integers $$$d_{i, 1}$$$, $$$d_{i, 2}$$$, ..., $$$d_{i, 7}$$$ ($$$0 le d_{i, j} le 3000$$$) — the number of students belonging to each of the seven aforementioned types in the $$$i$$$-th group. It is not guaranteed that the sum of these values is positive — a group can consist entirely of students that don't attend classes at all. It is guaranteed that the total number of students in all groups is not greater than $$$3000$$$.
2,700
false
false
false
false
false
false
true
false
false
false
4,330
556B
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was. Its most important components are a button and a line of _n_ similar gears. Each gear has _n_ teeth containing all numbers from 0 to _n_u2009-u20091 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on. Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if _n_u2009=u20095, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active. Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0,u20091,u20092,u2009...,u2009_n_u2009-u20091. Write a program that determines whether the given puzzle is real or fake. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of gears. The second line contains _n_ digits _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009_n_u2009-u20091) — the sequence of active teeth: the active tooth of the _i_-th gear contains number _a__i_. Output In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise. Note In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
1,100
false
false
true
false
false
false
true
false
false
false
7,609
303C
Problem - 303C - 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 graphs math number theory *2400 No tag edit access → Contest materials , so that for every pair of the remaining integers (_a__i_,u2009_a__j_), the following unequality holds: . Input The first line contains two integers _n_ and _k_ (1u2009u2009≤u2009_n_u2009u2009≤u20095000,u20090u2009≤u2009_k_u2009≤u20094), which we have mentioned above. The second line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009106). Output Print a single positive integer — the minimum _m_. Examples Input 7 0 0 2 3 6 7 12 18 Output 13 Input 7 1 0 2 3 6 7 12 18 Output 7
2,400
true
false
false
false
false
false
true
false
false
true
8,621
250A
Polycarpus has been working in the analytic department of the "F.R.A.U.D." company for as much as _n_ days. Right now his task is to make a series of reports about the company's performance for the last _n_ days. We know that the main information in a day report is value _a__i_, the company's profit on the _i_-th day. If _a__i_ is negative, then the company suffered losses on the _i_-th day. Polycarpus should sort the daily reports into folders. Each folder should include data on the company's performance for several consecutive days. Of course, the information on each of the _n_ days should be exactly in one folder. Thus, Polycarpus puts information on the first few days in the first folder. The information on the several following days goes to the second folder, and so on. It is known that the boss reads one daily report folder per day. If one folder has three or more reports for the days in which the company suffered losses (_a__i_u2009<u20090), he loses his temper and his wrath is terrible. Therefore, Polycarpus wants to prepare the folders so that none of them contains information on three or more days with the loss, and the number of folders is minimal. Write a program that, given sequence _a__i_, will print the minimum number of folders. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100), _n_ is the number of days. The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009≤u2009100), where _a__i_ means the company profit on the _i_-th day. It is possible that the company has no days with the negative _a__i_. Output Print an integer _k_ — the required minimum number of folders. In the second line print a sequence of integers _b_1, _b_2, ..., _b__k_, where _b__j_ is the number of day reports in the _j_-th folder. If there are multiple ways to sort the reports into _k_ days, print any of them. Examples Input 11 1 2 3 -4 -5 -6 5 -5 -6 -7 6 Note Here goes a way to sort the reports from the first sample into three folders: 1 2 3 -4 -5 -6 5 -5 -6 -7 6 In the second sample you can put all five reports in one folder.
1,000
false
true
false
false
false
false
false
false
false
false
8,836
55A
Problem - 55A - Codeforces =============== xa0 . For example, after the first minute the flea jumps to the neighboring hassock. You should answer: will the flea visit all the hassocks or not. We assume that flea has infinitely much time for this jumping. Input The only line contains single integer: 1u2009≤u2009_n_u2009≤u20091000 — number of hassocks. Output Output "YES" if all the hassocks will be visited and "NO" otherwise. Examples Input 1 Output YES Input 3 Output NO
1,200
true
false
true
false
false
false
false
false
false
false
9,700
1984D
You are given a string $$$s$$$ consisting of lowercase Latin characters. Count the number of nonempty strings $$$t eq$$$ "$$$ exttt{a}$$$" such that it is possible to partition$$$^{dagger}$$$ $$$s$$$ into some substrings satisfying the following conditions: each substring either equals $$$t$$$ or "$$$ exttt{a}$$$", and at least one substring equals $$$t$$$. $$$^{dagger}$$$ A partition of a string $$$s$$$ is an ordered sequence of some $$$k$$$ strings $$$t_1, t_2, ldots, t_k$$$ (called substrings) such that $$$t_1 + t_2 + ldots + t_k = s$$$, where $$$+$$$ represents the concatenation operation. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The only line of each test case contains a string $$$s$$$ consisting of lowercase Latin characters ($$$2 leq s leq 2 cdot 10^5$$$). The sum of $$$s$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of nonempty strings $$$t eq$$$ "$$$ exttt{a}$$$" that satisfy all constraints. Example Input 8 aaaaa baba cabacb aaabaaa bitset ab abbaaaabbb yearnineteeneightyfour Note In the first test case, $$$t$$$ can be "$$$ exttt{aa}$$$", "$$$ exttt{aaa}$$$", "$$$ exttt{aaaa}$$$", or the full string. In the second test case, $$$t$$$ can be "$$$ exttt{b}$$$", "$$$ exttt{bab}$$$", "$$$ exttt{ba}$$$", or the full string. In the third test case, the only such $$$t$$$ is the full string.
2,000
true
false
true
false
false
false
true
false
false
false
379
975C
Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar's warriors are falling in battle. Ivar has $$$n$$$ warriors, he places them on a straight line in front of the main gate, in a way that the $$$i$$$-th warrior stands right after $$$(i-1)$$$-th warrior. The first warrior leads the attack. Each attacker can take up to $$$a_i$$$ arrows before he falls to the ground, where $$$a_i$$$ is the $$$i$$$-th warrior's strength. Lagertha orders her warriors to shoot $$$k_i$$$ arrows during the $$$i$$$-th minute, the arrows one by one hit the first still standing warrior. After all Ivar's warriors fall and all the currently flying arrows fly by, Thor smashes his hammer and all Ivar's warriors get their previous strengths back and stand up to fight again. In other words, if all warriors die in minute $$$t$$$, they will all be standing to fight at the end of minute $$$t$$$. The battle will last for $$$q$$$ minutes, after each minute you should tell Ivar what is the number of his standing warriors. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q leq 200,000$$$)xa0— the number of warriors and the number of minutes in the battle. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) that represent the warriors' strengths. The third line contains $$$q$$$ integers $$$k_1, k_2, ldots, k_q$$$ ($$$1 leq k_i leq 10^{14}$$$), the $$$i$$$-th of them represents Lagertha's order at the $$$i$$$-th minute: $$$k_i$$$ arrows will attack the warriors. Output Output $$$q$$$ lines, the $$$i$$$-th of them is the number of standing warriors after the $$$i$$$-th minute. Examples Input 5 5 1 2 1 2 1 3 10 1 1 1 Note In the first example: after the 1-st minute, the 1-st and 2-nd warriors die. after the 2-nd minute all warriors die (and all arrows left over are wasted), then they will be revived thus answer is 5xa0— all warriors are alive. after the 3-rd minute, the 1-st warrior dies. after the 4-th minute, the 2-nd warrior takes a hit and his strength decreases by 1. after the 5-th minute, the 2-nd warrior dies.
1,400
false
false
false
false
false
false
false
true
false
false
5,833
13B
Problem - 13B - Codeforces =============== xa0 , while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1u2009/u20094 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1u2009/u20094). Input The first line contains one integer _t_ (1u2009≤u2009_t_u2009≤u200910000) — the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers — coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. Output Output one line for each test case. Print «YES» (without quotes), if the segments form the letter _A_ and «NO» otherwise. Examples Input 3 4 4 6 0 4 1 5 2 4 0 4 4 0 0 0 6 0 6 2 -4 1 1 0 1 0 0 0 5 0 5 2 -1 1 2 0 1 Output YES NO YES
2,000
false
false
true
false
false
false
false
false
false
false
9,929
292C
The problem uses a simplified TCP/IP address model, please read the statement carefully. An IP address is a 32-bit integer, represented as a group of four decimal 8-bit integers (without leading zeroes), separated by commas. For example, record 0.255.1.123 shows a correct IP address and records 0.256.1.123 and 0.255.1.01 do not. In the given problem an arbitrary group of four 8-bit integers is a correct IP address. Our hero Polycarpus still works as a system administrator in some large corporation. He likes beautiful IP addresses. To check if some IP address is beautiful, he should do the following: 1. write out in a line four 8-bit numbers of the IP address, without the commas; 2. check if the resulting string is a palindrome. Let us remind you that a palindrome is a string that reads the same from right to left and from left to right. For example, IP addresses 12.102.20.121 and 0.3.14.130 are beautiful (as strings "1210220121" and "0314130" are palindromes), and IP addresses 1.20.20.1 and 100.4.4.1 are not. Polycarpus wants to find all beautiful IP addresses that have the given set of digits. Each digit from the set must occur in the IP address at least once. IP address must not contain any other digits. Help him to cope with this difficult task. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200910) — the number of digits in the set. The second line contains the set of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u20099). It is guaranteed that all digits in the set are distinct. Output In the first line print a single integer _k_ — the number of beautiful IP addresses that contain the given set of digits. In the following _k_ lines print the IP addresses, one per line in the arbitrary order. Examples Output 6 78.190.209.187 79.180.208.197 87.190.209.178 89.170.207.198 97.180.208.179 98.170.207.189 Output 16 4.4.4.4 4.4.4.44 4.4.44.4 4.4.44.44 4.44.4.4 4.44.4.44 4.44.44.4 4.44.44.44 44.4.4.4 44.4.4.44 44.4.44.4 44.4.44.44 44.44.4.4 44.44.4.44 44.44.44.4 44.44.44.44
2,000
false
false
false
false
false
false
true
false
false
false
8,664
113E
One day Vasya was lying in bed watching his electronic clock to fall asleep quicker. Vasya lives in a strange country, where days have _h_ hours, and every hour has _m_ minutes. Clock shows time in decimal number system, in format H:M, where the string H always has a fixed length equal to the number of digits in the decimal representation of number _h_u2009-u20091. To achieve this, leading zeros are added if necessary. The string M has a similar format, and its length is always equal to the number of digits in the decimal representation of number _m_u2009-u20091. For example, if _h_u2009=u200917, _m_u2009=u20091000, then time equal to 13 hours and 75 minutes will be displayed as "13:075". Vasya had been watching the clock from _h_1 hours _m_1 minutes to _h_2 hours _m_2 minutes inclusive, and then he fell asleep. Now he asks you to count how many times he saw the moment at which at least _k_ digits changed on the clock simultaneously. For example, when switching 04:19 u2009→u2009 04:20 two digits change. When switching 23:59 u2009→u2009 00:00, four digits change. Consider that Vasya has been watching the clock for strictly less than one day. Note that the last time Vasya saw on the clock before falling asleep was "h2:m2". That is, Vasya didn't see the moment at which time "h2:m2" switched to the next value. Input The first line of the input file contains three space-separated integers _h_, _m_ and _k_ (2u2009≤u2009_h_,u2009_m_u2009≤u2009109, 1u2009≤u2009_k_u2009≤u200920). The second line contains space-separated integers _h_1, _m_1 (0u2009≤u2009_h_1u2009<u2009_h_, 0u2009≤u2009_m_1u2009<u2009_m_). The third line contains space-separated integers _h_2, _m_2 (0u2009≤u2009_h_2u2009<u2009_h_, 0u2009≤u2009_m_2u2009<u2009_m_). Output Print a single number — the number of times Vasya saw the moment of changing at least _k_ digits simultaneously. Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin stream (also you may use the %I64d specificator). Note In the first example Vasya will see the following moments of time: 4:4 0:0 u2009→u2009 0:1 u2009→u2009 0:2 u2009→u2009 0:3 u2009→u2009 0:4 1:0 u2009→u2009 1:1 u2009→u2009 1:2 u2009→u2009 1:3 u2009→u2009 1:4 2:0 u2009→u2009 2:1 u2009→u2009 2:2 u2009→u2009 2:3 u2009→u2009 2:4. Double arrow () marks the sought moments of time (in this example — when Vasya sees two numbers changing simultaneously). In the second example _k_u2009=u20091. Any switching time can be accepted, since during switching of the clock at least one digit is changed. Total switching equals to 24·60u2009=u20091440, but Vasya have not seen one of them — the switching of 23:59 00:00. In the third example Vasya fell asleep immediately after he began to look at the clock, so he did not see any change.
2,700
true
false
true
false
false
false
false
false
false
false
9,431
1119B
Alyona has recently bought a miniature fridge that can be represented as a matrix with $$$h$$$ rows and $$$2$$$ columns. Initially there is only one shelf at the bottom of the fridge, but Alyona can install arbitrary number of shelves inside the fridge between any two rows. A shelf is two cells wide, does not occupy any space but separates the inside of the fridge to the lower and upper part. An example of a fridge with $$$h = 7$$$ and two shelves. The shelves are shown in black. The picture corresponds to the first example. Alyona has $$$n$$$ bottles of milk that she wants to put in the fridge. The $$$i$$$-th bottle is $$$a_i$$$ cells tall and $$$1$$$ cell wide. She can put a bottle on some shelf if the corresponding space above the shelf is at least as tall as the bottle. She can not put a bottle on top of another bottle (if there is no shelf between them). Two bottles can not share a cell. Alyona is interested in the largest integer $$$k$$$ such that she can put bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. Find this largest $$$k$$$. Input The first line contains two integers $$$n$$$ and $$$h$$$ ($$$1 le n le 10^3$$$, $$$1 le h le 10^9$$$)xa0— the number of bottles and the height of the fridge. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le h$$$)xa0— the heights of the bottles. Output Print the single integer $$$k$$$xa0— the maximum integer such that Alyona can put the bottles $$$1$$$, $$$2$$$, ..., $$$k$$$ in the fridge at the same time. If Alyona can put all bottles in the fridge, print $$$n$$$. It is easy to see that Alyona can always put at least one bottle in the fridge. Examples Input 10 10 9 1 1 1 1 1 1 1 1 1 Note One of optimal locations in the first example is shown on the picture in the statement. One of optimal locations in the second example is shown on the picture below. One of optimal locations in the third example is shown on the picture below.
1,300
false
true
false
false
false
false
false
true
true
false
5,093
1184D1
The Third Doctor Who once correctly said that travel between parallel universes is "like travelling sideways". However, he incorrectly thought that there were infinite parallel universes, whereas in fact, as we now all know, there will never be more than 250. Heidi recently got her hands on a multiverse observation tool. She was able to see all $$$n$$$ universes lined up in a row, with non-existent links between them. She also noticed that the Doctor was in the $$$k$$$-th universe. The tool also points out that due to restrictions originating from the space-time discontinuum, the number of universes will never exceed $$$m$$$. Obviously, the multiverse is unstable because of free will. Each time a decision is made, one of two events will randomly happen: a new parallel universe is created, or a non-existent link is broken. More specifically, When a universe is created, it will manifest itself between any two adjacent universes or at one of the ends. When a link is broken, it could be cut between any two adjacent universes. After separating the multiverse into two segments, the segment NOT containing the Doctor will cease to exist. Heidi wants to perform a simulation of $$$t$$$ decisions. Each time a decision is made, Heidi wants to know the length of the multiverse (i.e. the number of universes), and the position of the Doctor. Input The first line contains four integers $$$n$$$, $$$k$$$, $$$m$$$ and $$$t$$$ ($$$2 le k le n le m le 250$$$, $$$1 le t le 1000$$$). Each of the following $$$t$$$ lines is in one of the following formats: "$$$1$$$ $$$i$$$" — meaning that a universe is inserted at the position $$$i$$$ ($$$1 le i le l + 1$$$), where $$$l$$$ denotes the current length of the multiverse. "$$$0$$$ $$$i$$$" — meaning that the $$$i$$$-th link is broken ($$$1 le i le l - 1$$$), where $$$l$$$ denotes the current length of the multiverse. Output Output $$$t$$$ lines. Each line should contain $$$l$$$, the current length of the multiverse and $$$k$$$, the current position of the Doctor. It is guaranteed that the sequence of the steps will be valid, i.e. the multiverse will have length at most $$$m$$$ and when the link breaking is performed, there will be at least one universe in the multiverse. Example Input 5 2 10 4 0 1 1 1 0 4 1 2 Note The multiverse initially consisted of 5 universes, with the Doctor being in the second. First, link 1 was broken, leaving the multiverse with 4 universes, and the Doctor in the first. Then, a universe was added to the leftmost end of the multiverse, increasing the multiverse length to 5, and the Doctor was then in the second universe. Then, the rightmost link was broken. Finally, a universe was added between the first and the second universe.
1,600
false
false
true
false
false
false
false
false
false
false
4,785
285D
Problem - 285D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks combinatorics dp implementation meet-in-the-middle *1900 No tag edit access → Contest materials ") Tutorial") _mod_ _n_)u2009+u20091 (1u2009≤u2009_i_u2009≤u2009_n_). Operation means taking the remainder after dividing number _x_ by number _y_. Obviously, not for all permutations _a_ and _b_ exists permutation _c_ that is sum of _a_ and _b_. That's why Petya got sad and asked you to do the following: given _n_, count the number of such pairs of permutations _a_ and _b_ of length _n_, that exists permutation _c_ that is sum of _a_ and _b_. The pair of permutations _x_,u2009_y_ (_x_u2009≠u2009_y_) and the pair of permutations _y_,u2009_x_ are considered distinct pairs. As the answer can be rather large, print the remainder after dividing it by 1000000007 (109u2009+u20097). Input The single line contains integer _n_ (1u2009≤u2009_n_u2009≤u200916). Output In the single line print a single non-negative integer — the number of such pairs of permutations _a_ and _b_, that exists permutation _c_ that is sum of _a_ and _b_, modulo 1000000007 (109u2009+u20097). Examples Input 3 Output 18 Input 5 Output 1800
1,900
false
false
true
true
false
false
false
false
false
false
8,693
81B
Polycarp is very careful. He even types numeric sequences carefully, unlike his classmates. If he sees a sequence without a space after the comma, with two spaces in a row, or when something else does not look neat, he rushes to correct it. For example, number sequence written like "1,2xa0,3,...,xa0xa0xa010" will be corrected to "1,xa02,xa03,xa0...,xa010". In this task you are given a string _s_, which is composed by a concatination of terms, each of which may be: a positive integer of an arbitrary length (leading zeroes are not allowed), a "comma" symbol (","), a "space" symbol (" "), "three dots" ("...", that is, exactly three points written one after another, also known as suspension points). Polycarp wants to add and remove spaces in the string _s_ to ensure the following: each comma is followed by exactly one space (if the comma is the last character in the string, this rule does not apply to it), each "three dots" term is preceded by exactly one space (if the dots are at the beginning of the string, this rule does not apply to the term), if two consecutive numbers were separated by spaces only (one or more), then exactly one of them should be left, there should not be other spaces. Automate Polycarp's work and write a program that will process the given string _s_. Input The input data contains a single string _s_. Its length is from 1 to 255 characters. The string _s_ does not begin and end with a space. Its content matches the description given above. Output Print the string _s_ after it is processed. Your program's output should be exactly the same as the expected answer. It is permissible to end output line with a line-break character, and without it. Examples Output 1, , , 4 ...5 ... ...6
1,700
false
false
true
false
false
false
false
false
false
false
9,562
1582F1
This is an easier version of the problem with smaller constraints. Korney Korneevich dag up an array $$$a$$$ of length $$$n$$$. Korney Korneevich has recently read about the operation elements. A sequence $$$s_1, s_2, ldots , s_m$$$ is called increasing if $$$s_1 < s_2 < ldots < s_m$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 500$$$) — the elements of the array $$$a$$$. Output In the first line print a single integer $$$k$$$ — the number of found $$$x$$$ values. In the second line print $$$k$$$ integers in increasing order $$$x_1, x_2, ldots x_k$$$ ($$$0 le x_1 < ldots < x_k$$$) — found $$$x$$$ values. Examples Output 12 0 1 2 3 4 5 6 7 10 11 12 13 Note In the first test case: To get value $$$x = 0$$$ it is possible to choose and empty subsequence To get value $$$x = 2$$$ it is possible to choose a subsequence $$$[2]$$$ To get value $$$x = 4$$$ it is possible to choose a subsequence $$$[4]$$$ To get value $$$x = 6$$$ it is possible to choose a subsequence $$$[2, 4]$$$
1,800
false
true
false
true
false
false
false
false
false
false
2,720
137D
Friday is Polycarpus' favourite day of the week. Not because it is followed by the weekend, but because the lessons on Friday are 2 IT lessons, 2 math lessons and 2 literature lessons. Of course, Polycarpus has prepared to all of them, unlike his buddy Innocentius. Innocentius spent all evening playing his favourite game Fur2 and didn't have enough time to do the literature task. As Innocentius didn't want to get an F, he decided to do the task and read the book called "Storm and Calm" during the IT and Math lessons (he never used to have problems with these subjects). When the IT teacher Mr. Watkins saw this, he decided to give Innocentius another task so that the boy concentrated more on the lesson and less — on the staff that has nothing to do with IT. Mr. Watkins said that a palindrome is a string that can be read the same way in either direction, from the left to the right and from the right to the left. A concatenation of strings _a_, _b_ is a string _ab_ that results from consecutive adding of string _b_ to string _a_. Of course, Innocentius knew it all but the task was much harder than he could have imagined. Mr. Watkins asked change in the "Storm and Calm" the minimum number of characters so that the text of the book would also be a concatenation of no more than _k_ palindromes. Innocentius can't complete the task and therefore asks you to help him. Input The first input line contains a non-empty string _s_ which is the text of "Storm and Calm" (without spaces). The length of the string _s_ does not exceed 500 characters. String _s_ consists of uppercase and lowercase Latin letters. The second line contains a single number _k_ (1u2009≤u2009_k_u2009≤u2009_s_, where _s_ represents the length of the string _s_). Output Print on the first line the minimum number of changes that Innocentius will have to make. Print on the second line the string consisting of no more than _k_ palindromes. Each palindrome should be non-empty and consist of uppercase and lowercase Latin letters. Use the character "+" (ASCII-code 43) to separate consecutive palindromes. If there exist several solutions, print any of them. The letters' case does matter, that is an uppercase letter is not considered equivalent to the corresponding lowercase letter. Examples Input abacababababbcbabcd 3 Output 1 abacaba+babab+bcbabcb
1,900
false
false
false
true
false
false
false
false
false
false
9,327
77B
For some reason in many American cartoons anvils fall from time to time onto heroes' heads. Of course, safes, wardrobes, cruisers, planes fall sometimes too... But anvils do so most of all. Anvils come in different sizes and shapes. Quite often they get the hero stuck deep in the ground. But have you ever thought who throws anvils from the sky? From what height? We are sure that such questions have never troubled you! It turns out that throwing an anvil properly is not an easy task at all. Let's describe one of the most popular anvil throwing models. Let the height _p_ of the potential victim vary in the range [0;_a_] and the direction of the wind _q_ vary in the range [u2009-u2009_b_;_b_]. _p_ and _q_ could be any real (floating) numbers. Then we can assume that the anvil will fit the toon's head perfectly only if the following equation has at least one real root: Determine the probability with which an aim can be successfully hit by an anvil. You can assume that the _p_ and _q_ coefficients are chosen equiprobably and independently in their ranges. Input The first line contains integer _t_ (1u2009≤u2009_t_u2009≤u200910000) — amount of testcases. Each of the following _t_ lines contain two space-separated integers _a_ and _b_ (0u2009≤u2009_a_,u2009_b_u2009≤u2009106). Pretests contain all the tests with 0u2009<u2009_a_u2009<u200910,u20090u2009≤u2009_b_u2009<u200910. Output Print _t_ lines — the probability of a successful anvil hit for each testcase. The absolute or relative error of the answer should not exceed 10u2009-u20096. Examples Output 0.6250000000 0.5312500000
1,800
true
false
false
false
false
false
false
false
false
false
9,579
925D
Indiana Jones found ancient Aztec catacombs containing a golden idol. The catacombs consists of $$$n$$$ caves. Each pair of caves is connected with a two-way corridor that can be opened or closed. The entrance to the catacombs is in the cave $$$1$$$, the idol and the exit are in the cave $$$n$$$. When Indiana goes from a cave $$$x$$$ to a cave $$$y$$$ using an open corridor, all corridors connected to the cave $$$x$$$ change their state: all open corridors become closed, all closed corridors become open. Indiana wants to go from cave $$$1$$$ to cave $$$n$$$ going through as small number of corridors as possible. Help him find the optimal path, or determine that it is impossible to get out of catacombs. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 3cdot 10^5$$$, $$$0 leq m leq 3 cdot 10^5$$$)xa0— the number of caves and the number of open corridors at the initial moment. The next $$$m$$$ lines describe the open corridors. The $$$i$$$-th of these lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$)xa0— the caves connected by the $$$i$$$-th open corridor. It is guaranteed that each unordered pair of caves is presented at most once. Output If there is a path to exit, in the first line print a single integer $$$k$$$xa0— the minimum number of corridors Indians should pass through ($$$1 leq k leq 10^6$$$). In the second line print $$$k+1$$$ integers $$$x_0, ldots, x_k$$$xa0— the number of caves in the order Indiana should visit them. The sequence $$$x_0, ldots, x_k$$$ should satisfy the following: $$$x_0 = 1$$$, $$$x_k = n$$$; for each $$$i$$$ from $$$1$$$ to $$$k$$$ the corridor from $$$x_{i - 1}$$$ to $$$x_i$$$ should be open at the moment Indiana walks along this corridor. If there is no path, print a single integer $$$-1$$$. We can show that if there is a path, there is a path consisting of no more than $$$10^6$$$ corridors.
2,600
false
false
false
false
false
true
false
false
false
false
6,008
75C
Problem - 75C - 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 number theory *1600 No tag edit access → Contest materials ") Editorial") , my solutions") , i.e. _low_u2009≤u2009_d_u2009≤u2009_high_. It is possible that there is no common divisor in the given range. You will be given the two integers _a_ and _b_, then _n_ queries. Each query is a range from _low_ to _high_ and you have to answer each query. Input The first line contains two integers _a_ and _b_, the two integers as described above (1u2009≤u2009_a_,u2009_b_u2009≤u2009109). The second line contains one integer _n_, the number of queries (1u2009≤u2009_n_u2009≤u2009104). Then _n_ lines follow, each line contains one query consisting of two integers, _low_ and _high_ (1u2009≤u2009_low_u2009≤u2009_high_u2009≤u2009109). Output Print _n_ lines. The _i_-th of them should contain the result of the _i_-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query. Examples Input 9 27 3 1 5 10 11 9 11 Output 3 -1 9
1,600
false
false
false
false
false
false
false
true
false
false
9,589
1478A
Nezzar has $$$n$$$ balls, numbered with integers $$$1, 2, ldots, n$$$. Numbers $$$a_1, a_2, ldots, a_n$$$ are written on them, respectively. Numbers on those balls form a non-decreasing sequence, which means that $$$a_i leq a_{i+1}$$$ for all $$$1 leq i < n$$$. Nezzar wants to color the balls using the minimum number of colors, such that the following holds. For any color, numbers on balls will form a strictly increasing sequence if he keeps balls with this chosen color and discards all other balls. Note that a sequence with the length at most $$$1$$$ is considered as a strictly increasing sequence. Please help Nezzar determine the minimum number of colors. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of testcases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le n$$$). It is guaranteed that $$$a_1 leq a_2 leq ldots leq a_n$$$. Output For each test case, output the minimum number of colors Nezzar can use. Example Input 5 6 1 1 1 2 3 4 5 1 1 2 2 3 4 2 2 2 2 3 1 2 3 1 1 Note Let's match each color with some numbers. Then: In the first test case, one optimal color assignment is $$$[1,2,3,3,2,1]$$$. In the second test case, one optimal color assignment is $$$[1,2,1,2,1]$$$.
800
false
true
false
false
false
false
true
false
false
false
3,284
1503C
Problem - 1503C - 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 data structures dp greedy shortest paths sortings two pointers *2200 No tag edit access → Contest materials $$$ dollars, where $$$c_i$$$ is the price floor enforced by city $$$i$$$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. Input The first line contains a single integer $$$n$$$ ($$$2le nle 10^5$$$) — the number of cities. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$, $$$c_i$$$ ($$$0le a_i,c_ile 10^9$$$) — the beauty and price floor of the $$$i$$$-th city. Output Output a single integer — the minimum total cost. Examples Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 Note In the first test case, we can travel in order $$$1 o 3 o 2 o 1$$$. The flight $$$1 o 3$$$ costs $$$max(c_1,a_3-a_1)=max(9,4-1)=9$$$. The flight $$$3 o 2$$$ costs $$$max(c_3, a_2-a_3)=max(1,2-4)=1$$$. The flight $$$2 o 1$$$ costs $$$max(c_2,a_1-a_2)=max(1,1-2)=1$$$. The total cost is $$$11$$$, and we cannot do better.
2,200
false
true
false
true
true
false
false
true
true
false
3,158
1676G
You are given a rooted tree consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The root is vertex $$$1$$$. There is also a string $$$s$$$ denoting the color of each vertex: if $$$s_i = exttt{B}$$$, then vertex $$$i$$$ is black, and if $$$s_i = exttt{W}$$$, then vertex $$$i$$$ is white. A subtree of the tree is called balanced if the number of white vertices equals the number of black vertices. Count the number of balanced subtrees. A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. In this problem, all trees have root $$$1$$$. The tree is specified by an array of parents $$$a_2, dots, a_n$$$ containing $$$n-1$$$ numbers: $$$a_i$$$ is the parent of the vertex with the number $$$i$$$ for all $$$i = 2, dots, n$$$. The parent of a vertex $$$u$$$ is a vertex that is the next vertex on a simple path from $$$u$$$ to the root. The subtree of a vertex $$$u$$$ is the set of all vertices that pass through $$$u$$$ on a simple path to the root. For example, in the picture below, $$$7$$$ is in the subtree of $$$3$$$ because the simple path $$$7 o 5 o 3 o 1$$$ passes through $$$3$$$. Note that a vertex is included in its subtree, and the subtree of the root is the entire tree. The picture shows the tree for $$$n=7$$$, $$$a=[1,1,2,3,3,5]$$$, and $$$s= exttt{WBBWWBW}$$$. The subtree at the vertex $$$3$$$ is balanced. Input The first line of input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 4000$$$)xa0— the number of vertices in the tree. The second line of each test case contains $$$n-1$$$ integers $$$a_2, dots, a_n$$$ ($$$1 le a_i < i$$$)xa0— the parents of the vertices $$$2, dots, n$$$. The third line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of the characters $$$ exttt{B}$$$ and $$$ exttt{W}$$$xa0— the coloring of the tree. It is guaranteed that the sum of the values $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of balanced subtrees. Example Input 3 7 1 1 2 3 3 5 WBBWWBW 2 1 BW 8 1 2 3 4 5 6 7 BWBWBWBW Note The first test case is pictured in the statement. Only the subtrees at vertices $$$2$$$ and $$$3$$$ are balanced. In the second test case, only the subtree at vertex $$$1$$$ is balanced. In the third test case, only the subtrees at vertices $$$1$$$, $$$3$$$, $$$5$$$, and $$$7$$$ are balanced.
1,300
false
false
false
true
false
false
false
false
false
true
2,209
1932E
You are in a nuclear laboratory that is about to explode and destroy the Earth. You must save the Earth before the final countdown reaches zero. The countdown consists of $$$n$$$ ($$$1 le n le 4 cdot 10^5$$$) mechanical indicators, each showing one decimal digit. You noticed that when the countdown changes its state from $$$x$$$ to $$$x-1$$$, it doesn't happen in one move. Instead, each change of a single digit takes one second. So, for example, if the countdown shows 42, then it will change to 41 in one second, because only one digit is changed, but if the countdown shows 2300, then it will change to 2299 in three seconds, because the three last digits are changed. Find out how much time is left before the countdown reaches zero. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 4cdot 10^5$$$). The second line contains a string of $$$n$$$ digits, the current state of the countdown. It is guaranteed that at least one digit is not zero. The sum of $$$n$$$ for all tests does not exceed $$$4cdot 10^5$$$. Output For each test case, print a single integer without leading zeroes, the number of seconds left before the countdown reaches zero. Note that this number may be huge. Example Input 5 2 42 5 12345 2 99 4 0005 27 456480697259671309012631002 Output 46 13715 108 5 507200774732968121125145546 Note In the first example, there are four changes that take 2 seconds: 40 to 39, 30 to 29, 20 to 19, and 10 to 09, other changes take 1 second each. So the total time is $$$2cdot 4 + 1cdot(42-4) = 46$$$.
1,600
true
false
true
false
false
false
false
false
false
false
690
1601F
Integers from $$$1$$$ to $$$n$$$ (inclusive) were sorted lexicographically (considering integers as strings). As a result, array $$$a_1, a_2, dots, a_n$$$ was obtained. Calculate value of $$$(sum_{i = 1}^n ((i - a_i) mod 998244353)) mod 10^9 + 7$$$. $$$x mod y$$$ here means the remainder after division $$$x$$$ by $$$y$$$. This remainder is always non-negative and doesn't exceed $$$y - 1$$$. For example, $$$5 mod 3 = 2$$$, $$$(-1) mod 6 = 5$$$. Note A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. For example, $$$42$$$ is lexicographically smaller than $$$6$$$, because they differ in the first digit, and $$$4 < 6$$$; $$$42 < 420$$$, because $$$42$$$ is a prefix of $$$420$$$. Let's denote $$$998244353$$$ as $$$M$$$. In the first example, array $$$a$$$ is equal to $$$[1, 2, 3]$$$. $$$(1 - 1) mod M = 0 mod M = 0$$$ $$$(2 - 2) mod M = 0 mod M = 0$$$ $$$(3 - 3) mod M = 0 mod M = 0$$$ As a result, $$$(0 + 0 + 0) mod 10^9 + 7 = 0$$$ In the second example, array $$$a$$$ is equal to $$$[1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9]$$$. $$$(1 - 1) mod M = 0 mod M = 0$$$ $$$(2 - 10) mod M = (-8) mod M = 998244345$$$ $$$(3 - 11) mod M = (-8) mod M = 998244345$$$ $$$(4 - 12) mod M = (-8) mod M = 998244345$$$ $$$(5 - 2) mod M = 3 mod M = 3$$$ $$$(6 - 3) mod M = 3 mod M = 3$$$ $$$(7 - 4) mod M = 3 mod M = 3$$$ $$$(8 - 5) mod M = 3 mod M = 3$$$ $$$(9 - 6) mod M = 3 mod M = 3$$$ $$$(10 - 7) mod M = 3 mod M = 3$$$ $$$(11 - 8) mod M = 3 mod M = 3$$$ $$$(12 - 9) mod M = 3 mod M = 3$$$ As a result, $$$(0 + 998244345 + 998244345 + 998244345 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3) mod 10^9 + 7$$$ $$$=$$$ $$$2994733059 mod 10^9 + 7$$$ $$$=$$$ $$$994733045$$$
3,400
true
false
false
false
false
false
false
true
false
false
2,646
1559E
Mocha wants to be an astrologer. There are $$$n$$$ stars which can be seen in Zhijiang, and the brightness of the $$$i$$$-th star is $$$a_i$$$. Mocha considers that these $$$n$$$ stars form a constellation, and she uses $$$(a_1,a_2,ldots,a_n)$$$ to show its state. A state is called mathematical if all of the following three conditions are satisfied: For all $$$i$$$ ($$$1le ile n$$$), $$$a_i$$$ is an integer in the range $$$$$$ and $$$(b_1,b_2,ldots,b_n)$$$ are considered different if there exists $$$i$$$ ($$$1le ile n$$$) such that $$$a_i e b_i$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 50$$$, $$$1 le m le 10^5$$$) — the number of stars and the upper bound of the sum of the brightness of stars. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le m$$$) — the range of the brightness of the $$$i$$$-th star. Output Print a single integer — the number of different mathematical states of this constellation, modulo $$$998,244,353$$$.
2,200
true
false
false
true
false
false
false
false
false
false
2,831
491B
Think of New York as a rectangular grid consisting of _N_ vertical avenues numerated from 1 to _N_ and _M_ horizontal streets numerated 1 to _M_. _C_ friends are staying at _C_ hotels located at some street-avenue crossings. They are going to celebrate birthday of one of them in the one of _H_ restaurants also located at some street-avenue crossings. They also want that the maximum distance covered by one of them while traveling to the restaurant to be minimum possible. Help friends choose optimal restaurant for a celebration. Suppose that the distance between neighboring crossings are all the same equal to one kilometer. Input The first line contains two integers _N_ и _M_xa0— size of the city (1u2009≤u2009_N_,u2009_M_u2009≤u2009109). In the next line there is a single integer _C_ (1u2009≤u2009_C_u2009≤u2009105)xa0— the number of hotels friends stayed at. Following _C_ lines contain descriptions of hotels, each consisting of two coordinates _x_ and _y_ (1u2009≤u2009_x_u2009≤u2009_N_, 1u2009≤u2009_y_u2009≤u2009_M_). The next line contains an integer _H_xa0— the number of restaurants (1u2009≤u2009_H_u2009≤u2009105). Following _H_ lines contain descriptions of restaurants in the same format. Several restaurants and hotels may be located near the same crossing. Output In the first line output the optimal distance. In the next line output index of a restaurant that produces this optimal distance. If there are several possibilities, you are allowed to output any of them. Examples Input 10 10 2 1 1 3 3 2 1 10 4 4
2,100
true
true
false
false
false
false
false
false
false
false
7,870
1191B
Tokitsukaze is playing a game derivated from Japanese mahjong. In this game, she has three tiles in her hand. Each tile she owns is a suited tile, which means it has a suit (manzu, pinzu or souzu) and a number (a digit ranged from $$$1$$$ to $$$9$$$). In this problem, we use one digit and one lowercase letter, which is the first character of the suit, to represent a suited tile. All possible suited tiles are represented as 1m, 2m, $$$ldots$$$, 9m, 1p, 2p, $$$ldots$$$, 9p, 1s, 2s, $$$ldots$$$, 9s. In order to win the game, she must have at least one mentsu (described below) in her hand, so sometimes she should draw extra suited tiles. After drawing a tile, the number of her tiles increases by one. She can draw any tiles she wants, including those already in her hand. Do you know the minimum number of extra suited tiles she needs to draw so that she can win? Here are some useful definitions in this game: A mentsu, also known as meld, is formed by a koutsu or a shuntsu; A koutsu, also known as triplet, is made of three identical tiles, such as [1m, 1m, 1m], however, [1m, 1p, 1s] or [1m, 4m, 7m] is NOT a koutsu; A shuntsu, also known as sequence, is made of three sequential numbered tiles in the same suit, such as [1m, 2m, 3m] and [5s, 7s, 6s], however, [9m, 1m, 2m] or [1m, 2p, 3s] is NOT a shuntsu. Some examples: [2m, 3p, 2s, 4m, 1s, 2s, 4s] — it contains no koutsu or shuntsu, so it includes no mentsu; [4s, 3m, 3p, 4s, 5p, 4s, 5p] — it contains a koutsu, [4s, 4s, 4s], but no shuntsu, so it includes a mentsu; [5p, 5s, 9m, 4p, 1s, 7p, 7m, 6p] — it contains no koutsu but a shuntsu, [5p, 4p, 6p] or [5p, 7p, 6p], so it includes a mentsu. Note that the order of tiles is unnecessary and you can assume the number of each type of suited tiles she can draw is infinite. Input The only line contains three stringsxa0— the tiles in Tokitsukaze's hand. For each string, the first character is a digit ranged from $$$1$$$ to $$$9$$$ and the second character is m, p or s. Note In the first example, Tokitsukaze already has a shuntsu. In the second example, Tokitsukaze already has a koutsu. In the third example, Tokitsukaze can get a shuntsu by drawing one suited tilexa0— 1p or 4p. The resulting tiles will be [3p, 9m, 2p, 1p] or [3p, 9m, 2p, 4p].
1,200
false
false
true
false
false
false
true
false
false
false
4,743
464C
Andrew and Eugene are playing a game. Initially, Andrew has string _s_, consisting of digits. Eugene sends Andrew multiple queries of type "_d__i_u2009→u2009_t__i_", that means "replace all digits _d__i_ in string _s_ with substrings equal to _t__i_". For example, if _s_u2009=u2009123123, then query "2u2009→u200900" transforms _s_ to 10031003, and query "3u2009→u2009" ("replace 3 by an empty string") transforms it to _s_u2009=u20091212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to _s_ by 1000000007xa0(109u2009+u20097). When you represent _s_ as a decimal number, please ignore the leading zeroes; also if _s_ is an empty string, then it's assumed that the number equals to zero. Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him! Input The first line contains string _s_ (1u2009≤u2009_s_u2009≤u2009105), consisting of digitsxa0— the string before processing all the requests. The second line contains a single integer _n_ (0u2009≤u2009_n_u2009≤u2009105)xa0— the number of queries. The next _n_ lines contain the descriptions of the queries. The _i_-th query is described by string "_d__i_->_t__i_", where _d__i_ is exactly one digit (from 0 to 9), _t__i_ is a string consisting of digits (_t__i_ can be an empty string). The sum of lengths of _t__i_ for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed. Output Print a single integer — remainder of division of the resulting number by 1000000007xa0(109u2009+u20097). Note Note that the leading zeroes are not removed from string _s_ after the replacement (you can see it in the third sample).
2,100
false
false
false
true
false
false
false
false
false
false
7,982
74B
A stowaway and a controller play the following game. The train is represented by _n_ wagons which are numbered with positive integers from 1 to _n_ from the head to the tail. The stowaway and the controller are initially in some two different wagons. Every minute the train can be in one of two conditions — moving or idle. Every minute the players move. The controller's move is as follows. The controller has the movement direction — to the train's head or to its tail. During a move the controller moves to the neighbouring wagon correspondingly to its movement direction. If at the end of his move the controller enters the 1-st or the _n_-th wagon, that he changes the direction of his movement into the other one. In other words, the controller cyclically goes from the train's head to its tail and back again during all the time of a game, shifting during each move by one wagon. Note, that the controller always have exactly one possible move. The stowaway's move depends from the state of the train. If the train is moving, then the stowaway can shift to one of neighbouring wagons or he can stay where he is without moving. If the train is at a station and is idle, then the stowaway leaves the train (i.e. he is now not present in any train wagon) and then, if it is not the terminal train station, he enters the train again into any of _n_ wagons (not necessarily into the one he's just left and not necessarily into the neighbouring one). If the train is idle for several minutes then each such minute the stowaway leaves the train and enters it back. Let's determine the order of the players' moves. If at the given minute the train is moving, then first the stowaway moves and then the controller does. If at this minute the train is idle, then first the stowaway leaves the train, then the controller moves and then the stowaway enters the train. If at some point in time the stowaway and the controller happen to be in one wagon, then the controller wins: he makes the stowaway pay fine. If after a while the stowaway reaches the terminal train station, then the stowaway wins: he simply leaves the station during his move and never returns there again. At any moment of time the players know each other's positions. The players play in the optimal way. Specifically, if the controller wins, then the stowaway plays so as to lose as late as possible. As all the possible moves for the controller are determined uniquely, then he is considered to play optimally always. Determine the winner. Input The first line contains three integers _n_, _m_ and _k_. They represent the number of wagons in the train, the stowaway's and the controller's initial positions correspondingly (2u2009≤u2009_n_u2009≤u200950, 1u2009≤u2009_m_,u2009_k_u2009≤u2009_n_, _m_u2009≠u2009_k_). The second line contains the direction in which a controller moves. "to head" means that the controller moves to the train's head and "to tail" means that the controller moves to its tail. It is guaranteed that in the direction in which the controller is moving, there is at least one wagon. Wagon 1 is the head, and wagon _n_ is the tail. The third line has the length from 1 to 200 and consists of symbols "0" and "1". The _i_-th symbol contains information about the train's state at the _i_-th minute of time. "0" means that in this very minute the train moves and "1" means that the train in this very minute stands idle. The last symbol of the third line is always "1" — that's the terminal train station. Output If the stowaway wins, print "Stowaway" without quotes. Otherwise, print "Controller" again without quotes, then, separated by a space, print the number of a minute, at which the stowaway will be caught.
1,500
false
true
false
true
false
false
false
false
false
false
9,595
1626F
You are given an integer array $$$a_0, a_1, dots, a_{n - 1}$$$, and an integer $$$k$$$. You perform the following code with it: ``` long long ans = 0; // create a 64-bit signed variable which is initially equal to 0for(int i = 1; i <= k; i++){ int idx = rnd.next(0, n - 1); // generate a random integer between 0 and n - 1, both inclusive // each integer from 0 to n - 1 has the same probability of being chosen ans += a[idx]; a[idx] -= (a[idx] % i);} ``` Your task is to calculate the expected value of the variable ans after performing this code. Note that the input is generated according to special rules (see the input format section). Input The only line contains six integers $$$n$$$, $$$a_0$$$, $$$x$$$, $$$y$$$, $$$k$$$ and $$$M$$$ ($$$1 le n le 10^7$$$; $$$1 le a_0, x, y < M le 998244353$$$; $$$1 le k le 17$$$). The array $$$a$$$ in the input is constructed as follows: $$$a_0$$$ is given in the input; for every $$$i$$$ from $$$1$$$ to $$$n - 1$$$, the value of $$$a_i$$$ can be calculated as $$$a_i = (a_{i - 1} cdot x + y) bmod M$$$. Output Let the expected value of the variable ans after performing the code be $$$E$$$. It can be shown that $$$E cdot n^k$$$ is an integer. You have to output this integer modulo $$$998244353$$$. Examples Input 2 15363 270880 34698 17 2357023 Note The array in the first example test is $$$[10, 35, 22]$$$. In the second example, it is $$$[15363, 1418543]$$$.
2,800
true
false
false
true
false
false
false
false
false
false
2,482
366E
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with _n_ strings and _m_ frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the _i_-th string holding it on the _j_-th fret the guitar produces a note, let's denote it as _a__ij_. We know that Dima's guitar can produce _k_ distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that _a__ij_u2009=u2009_a__pq_ at (_i_,u2009_j_)u2009≠u2009(_p_,u2009_q_). Dima has already written a song — a sequence of _s_ notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein). We'll represent a way to play a song as a sequence of pairs (_x__i_,u2009_y__i_) (1u2009≤u2009_i_u2009≤u2009_s_), such that the _x__i_-th string on the _y__i_-th fret produces the _i_-th note from the song. The complexity of moving between pairs (_x_1,u2009_y_1) and (_x_2,u2009_y_2) equals + . The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs. Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool! Input The first line of the input contains four integers _n_, _m_, _k_ and _s_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092000,u20091u2009≤u2009_k_u2009≤u20099,u20092u2009≤u2009_s_u2009≤u2009105). Then follow _n_ lines, each containing _m_ integers _a__ij_ (1u2009≤u2009_a__ij_u2009≤u2009_k_). The number in the _i_-th row and the _j_-th column (_a__ij_) means a note that the guitar produces on the _i_-th string and the _j_-th fret. The last line of the input contains _s_ integers _q__i_ (1u2009≤u2009_q__i_u2009≤u2009_k_) — the sequence of notes of the song. Output In a single line print a single number — the maximum possible complexity of the song. Examples Input 4 6 5 7 3 1 2 2 3 1 3 2 2 2 5 5 4 2 2 2 5 3 3 2 2 1 4 3 2 3 1 4 1 5 1 Input 4 4 9 5 4 7 9 5 1 2 1 7 8 3 4 9 5 7 7 2 7 1 9 2 5
2,200
true
false
true
false
false
false
true
false
false
false
8,366
660B
Consider 2_n_ rows of the seats in a bus. _n_ rows of the seats on the left and _n_ rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4_n_. Consider that _m_ (_m_u2009≤u20094_n_) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to _m_ (in the order of their entering the bus). The pattern of the seat occupation is as below: 1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , _n_-th row left window seat, _n_-th row right window seat. After occupying all the window seats (for _m_u2009>u20092_n_) the non-window seats are occupied: 1-st row left non-window seat, 1-st row right non-window seat, ... , _n_-th row left non-window seat, _n_-th row right non-window seat. All the passengers go to a single final destination. In the final destination, the passengers get off in the given order. 1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , _n_-th row left non-window seat, _n_-th row left window seat, _n_-th row right non-window seat, _n_-th row right window seat. The seating for _n_u2009=u20099 and _m_u2009=u200936. You are given the values _n_ and _m_. Output _m_ numbers from 1 to _m_, the order in which the passengers will get off the bus. Input The only line contains two integers, _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_m_u2009≤u20094_n_) — the number of pairs of rows and the number of passengers.
1,000
false
false
true
false
false
false
false
false
false
false
7,181
1367B
You are given an array $$$a[0 ldots n-1]$$$ of length $$$n$$$ which consists of non-negative integers. Note that array indices start from zero. An array is called good if the parity of each index matches the parity of the element at that index. More formally, an array is good if for all $$$i$$$ ($$$0 le i le n - 1$$$) the equality $$$i bmod 2 = a[i] bmod 2$$$ holds, where $$$x bmod 2$$$ is the remainder of dividing $$$x$$$ by 2. For example, the arrays [$$$0, 5, 2, 1$$$] and [$$$0, 17, 0, 3$$$] are good, and the array [$$$2, 4, 6, 7$$$] is bad, because for $$$i=1$$$, the parities of $$$i$$$ and $$$a[i]$$$ are different: $$$i bmod 2 = 1 bmod 2 = 1$$$, but $$$a[i] bmod 2 = 4 bmod 2 = 0$$$. In one move, you can take any two elements of the array and swap them (these elements are not necessarily adjacent). Find the minimum number of moves in which you can make the array $$$a$$$ good, or say that this is not possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case starts with a line containing an integer $$$n$$$ ($$$1 le n le 40$$$)xa0— the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_0, a_1, ldots, a_{n-1}$$$ ($$$0 le a_i le 1000$$$)xa0— the initial array. Output For each test case, output a single integerxa0— the minimum number of moves to make the given array $$$a$$$ good, or -1 if this is not possible. Note In the first test case, in the first move, you can swap the elements with indices $$$0$$$ and $$$1$$$, and in the second move, you can swap the elements with indices $$$2$$$ and $$$3$$$. In the second test case, in the first move, you need to swap the elements with indices $$$0$$$ and $$$1$$$. In the third test case, you cannot make the array good.
800
true
true
false
false
false
false
false
false
false
false
3,842
916D
Why I have to finish so many assignments??? Jamie is getting very busy with his school life. He starts to forget the assignments that he has to do. He decided to write the things down on a to-do list. He assigns a value priority for each of his assignment (lower value means more important) so he can decide which he needs to spend more time on. After a few days, Jamie finds out the list is too large that he can't even manage the list by himself! As you are a good friend of Jamie, help him write a program to support the following operations on the to-do list: _set_ _a__i_ _x__i_xa0— Add assignment _a__i_ to the to-do list if it is not present, and set its priority to _x__i_. If assignment _a__i_ is already in the to-do list, its priority is changed to _x__i_. _remove_ _a__i_xa0— Remove assignment _a__i_ from the to-do list if it is present in it. _query_ _a__i_xa0— Output the number of assignments that are more important (have a smaller priority value) than assignment _a__i_, so Jamie can decide a better schedule. Output u2009-u20091 if _a__i_ is not in the to-do list. _undo_ _d__i_xa0— Undo all changes that have been made in the previous _d__i_ days (not including the day of this operation) At day 0, the to-do list is empty. In each of the following _q_ days, Jamie will do exactly one out of the four operations. If the operation is a _query_, you should output the result of the query before proceeding to the next day, or poor Jamie cannot make appropriate decisions. Input The first line consists of a single integer _q_ (1u2009≤u2009_q_u2009≤u2009105)xa0— the number of operations. The following _q_ lines consists of the description of the operations. The _i_-th line consists of the operation that Jamie has done in the _i_-th day. The query has the following format: The first word in the line indicates the type of operation. It must be one of the following four: set, remove, query, undo. If it is a set operation, a string _a__i_ and an integer _x__i_ follows (1u2009≤u2009_x__i_u2009≤u2009109). _a__i_ is the assignment that need to be set to priority _x__i_. If it is a remove operation, a string _a__i_ follows. _a__i_ is the assignment that need to be removed. If it is a query operation, a string _a__i_ follows. _a__i_ is the assignment that needs to be queried. If it is a undo operation, an integer _d__i_ follows (0u2009≤u2009_d__i_u2009<u2009_i_). _d__i_ is the number of days that changes needed to be undone. All assignment names _a__i_ only consists of lowercase English letters and have a length 1u2009≤u2009_a__i_u2009≤u200915. It is guaranteed that the last operation is a query operation. Output For each query operation, output a single integerxa0— the number of assignments that have a priority lower than assignment _a__i_, or u2009-u20091 if _a__i_ is not in the to-do list. Interaction If the operation is a _query_, you should output the result of the query and flush the output stream before proceeding to the next operation. Otherwise, you may get the verdict Idleness Limit Exceed. For flushing the output stream, please refer to the documentation of your chosen programming language. The flush functions of some common programming languages are listed below: C: fflush(stdout); C++: cout « flush; Java: System.out.flush(); Examples Input 8 set chemlabreport 1 set physicsexercise 2 set chinesemockexam 3 query physicsexercise query chinesemockexam remove physicsexercise query physicsexercise query chinesemockexam Input 8 set physicsexercise 2 set chinesemockexam 3 set physicsexercise 1 query physicsexercise query chinesemockexam undo 4 query physicsexercise query chinesemockexam Input 5 query economicsessay remove economicsessay query economicsessay undo 2 query economicsessay Input 5 set economicsessay 1 remove economicsessay undo 1 undo 1 query economicsessay
2,200
false
false
false
false
true
false
false
false
false
false
6,065
1310D
Masha lives in a country with $$$n$$$ cities numbered from $$$1$$$ to $$$n$$$. She lives in the city number $$$1$$$. There is a direct train route between each pair of distinct cities $$$i$$$ and $$$j$$$, where $$$i eq j$$$. In total there are $$$n(n-1)$$$ distinct routes. Every route has a cost, cost for route from $$$i$$$ to $$$j$$$ may be different from the cost of route from $$$j$$$ to $$$i$$$. Masha wants to start her journey in city $$$1$$$, take exactly $$$k$$$ routes from one city to another and as a result return to the city $$$1$$$. Masha is really careful with money, so she wants the journey to be as cheap as possible. To do so Masha doesn't mind visiting a city multiple times or even taking the same route multiple times. Masha doesn't want her journey to have odd cycles. Formally, if you can select visited by Masha city $$$v$$$, take odd number of routes used by Masha in her journey and return to the city $$$v$$$, such journey is considered unsuccessful. Help Masha to find the cheapest (with minimal total cost of all taken routes) successful journey. Input First line of input had two integer numbers $$$n,k$$$ ($$$2 leq n leq 80; 2 leq k leq 10$$$): number of cities in the country and number of routes in Masha's journey. It is guaranteed that $$$k$$$ is even. Next $$$n$$$ lines hold route descriptions: $$$j$$$-th number in $$$i$$$-th line represents the cost of route from $$$i$$$ to $$$j$$$ if $$$i eq j$$$, and is 0 otherwise (there are no routes $$$i o i$$$). All route costs are integers from $$$0$$$ to $$$10^8$$$. Output Output a single integerxa0— total cost of the cheapest Masha's successful journey. Examples Input 5 8 0 1 2 2 0 0 0 1 1 2 0 1 0 0 0 2 1 1 0 0 2 0 1 2 0 Input 3 2 0 1 1 2 0 1 2 2 0
2,300
false
false
false
true
false
false
false
false
false
true
4,146
618A
Your friend recently gave you some slimes for your birthday. You have _n_ slimes all initially with value 1. You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other _n_u2009-u20091 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value _v_, you combine them together to create a slime with value _v_u2009+u20091. You would like to see what the final state of the row is after you've added all _n_ slimes. Please print the values of the slimes in the row from left to right. Input The first line of the input will contain a single integer, _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000). Output Output a single line with _k_ integers, where _k_ is the number of slimes in the row after you've finished the procedure described in the problem statement. The _i_-th of these numbers should be the value of the _i_-th slime from the left. Note In the first sample, we only have a single slime with value 1. The final state of the board is just a single slime with value 1. In the second sample, we perform the following steps: Initially we place a single slime in a row by itself. Thus, row is initially 1. Then, we will add another slime. The row is now 1 1. Since two rightmost slimes have the same values, we should replace these slimes with one with value 2. Thus, the final state of the board is 2. In the third sample, after adding the first two slimes, our row is 2. After adding one more slime, the row becomes 2 1. In the last sample, the steps look as follows: 1. 1 2. 2 3. 2 1 4. 3 5. 3 1 6. 3 2 7. 3 2 1 8. 4
800
false
false
true
false
false
false
false
false
false
false
7,364
1228C
Let's introduce some definitions that will be needed later. Let $$$prime(x)$$$ be the set of prime divisors of $$$x$$$. For example, $$$prime(140) = { 2, 5, 7 }$$$, $$$prime(169) = { 13 }$$$. Let $$$g(x, p)$$$ be the maximum possible integer $$$p^k$$$ where $$$k$$$ is an integer such that $$$x$$$ is divisible by $$$p^k$$$. For example: $$$g(45, 3) = 9$$$ ($$$45$$$ is divisible by $$$3^2=9$$$ but not divisible by $$$3^3=27$$$), $$$g(63, 7) = 7$$$ ($$$63$$$ is divisible by $$$7^1=7$$$ but not divisible by $$$7^2=49$$$). Let $$$f(x, y)$$$ be the product of $$$g(y, p)$$$ for all $$$p$$$ in $$$prime(x)$$$. For example: $$$f(30, 70) = g(70, 2) cdot g(70, 3) cdot g(70, 5) = 2^1 cdot 3^0 cdot 5^1 = 10$$$, $$$f(525, 63) = g(63, 3) cdot g(63, 5) cdot g(63, 7) = 3^2 cdot 5^0 cdot 7^1 = 63$$$. You have integers $$$x$$$ and $$$n$$$. Calculate $$$f(x, 1) cdot f(x, 2) cdot ldots cdot f(x, n) bmod{(10^{9} + 7)}$$$. Input The only line contains integers $$$x$$$ and $$$n$$$ ($$$2 le x le 10^{9}$$$, $$$1 le n le 10^{18}$$$)xa0— the numbers used in formula. Examples Input 947 987654321987654321 Note In the first example, $$$f(10, 1) = g(1, 2) cdot g(1, 5) = 1$$$, $$$f(10, 2) = g(2, 2) cdot g(2, 5) = 2$$$. In the second example, actual value of formula is approximately $$$1.597 cdot 10^{171}$$$. Make sure you print the answer modulo $$$(10^{9} + 7)$$$. In the third example, be careful about overflow issue.
1,700
true
false
false
false
false
false
false
false
false
false
4,533
100F
Problem - 100F - Codeforces =============== xa0 u2009=u2009(_x_u2009+u2009_a_1)·(_x_u2009+u2009_a_2)·... (_x_u2009+u2009_a__n_). Write Pike program to print it in a standard form _p_(_x_)u2009=u2009_x__n_u2009+u2009_b_1_x__n_u2009-u20091u2009+u2009...u2009+u2009_b__n_u2009-u20091_x_u2009+u2009_b__n_. You should write each addend in form «C*X^K» (for example, 5*X^8). Please, write the polynom in the shortest way, so you should skip unnecessary terms: some terms «C*X^K» should be reduced or even omitted. Look for the samples for clarification. Input The first line of the input contains _n_ (1u2009≤u2009_n_u2009≤u20099). The following _n_ lines contain integer _a__i_ (u2009-u200910u2009≤u2009_a__i_u2009≤u200910). Output Print the given polynom in a standard way. Note, that the answer in this problem response uniquely determined. Examples Input 2 -1 1 Output X^2-1 Input 2 1 1 Output X^2+2*X+1
1,800
false
false
true
false
false
false
false
false
false
false
9,484
1780G
Today is an important day for chef Tonio — an auditor has arrived in his hometown of Morioh. He has also arrived at Tonio's restaurant and ordered dessert. Tonio has not been prepared for this turn of events. As you know, dessert is a string of lowercase English letters. Tonio remembered the rule of dessertsxa0— a string $$$s$$$ of length $$$n$$$. Any dessert $$$t$$$ is delicious if the number of occurrences of $$$t$$$ in $$$s$$$ as a substring is divisible by the length of $$$t$$$. Now Tonio wants to know the number of delicious substrings of $$$s$$$. If the substring occurs several times in the string $$$s$$$, then all occurrences must be taken into account. Input The first line contains an integer $$$n$$$ ($$$1 leq n leq 10^6$$$) — the length of the rule $$$s$$$. The second line contains the string $$$s$$$ of length $$$n$$$ — the rule. The rule consists only of lowercase English letters. Output In a single line print the number of delicious substrings of $$$s$$$. Note In the first sample, there are many delicious substrings. $$$7$$$ of them are substrings of length $$$1$$$ (because any number is divisible by $$$1$$$). Consider other delicious substrings: "ab" occurs in $$$s$$$ $$$2$$$ times, which is divisible by the length of the substring. "ba" also occurs $$$2$$$ times. Therefore, the answer is $$$7 + 2 + 2 = 11$$$. Note that the answer includes both occurrences of "ab" and "ba".
2,400
true
false
false
false
false
false
false
true
false
false
1,589
1616H
Problem - 1616H - Codeforces =============== xa0 $$$ where $$$1 leq i < j leq k$$$, the inequality $$$a_{b_i} oplus a_{b_j} leq x$$$ is held. Here, $$$oplus$$$ denotes the . Here, $$$n$$$ is the size of the array. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i < 2^{30}$$$): the array itself. Output Print one integer: the number of non-empty subsets such that the bitwise XOR of every pair of elements is at most $$$x$$$, modulo $$$998,244,353$$$. Examples Input 4 2 0 1 2 3 Output 8 Input 3 6 4 2 2 Output 7 Input 4 0 1 1 2 2 Output 6
3,000
true
false
false
true
true
false
false
false
false
false
2,549
1312A
You are given two integers $$$n$$$ and $$$m$$$ ($$$m < n$$$). Consider a convex regular polygon of $$$n$$$ vertices. Recall that a regular polygon is a polygon that is equiangular (all angles are equal in measure) and equilateral (all sides have the same length). Examples of convex regular polygons Your task is to say if it is possible to build another convex regular polygon with $$$m$$$ vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon. 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 as two space-separated integers $$$n$$$ and $$$m$$$ ($$$3 le m < n le 100$$$) — the number of vertices in the initial polygon and the number of vertices in the polygon you want to build. Output For each test case, print the answer — "YES" (without quotes), if it is possible to build another convex regular polygon with $$$m$$$ vertices such that its center coincides with the center of the initial polygon and each of its vertices is some vertex of the initial polygon and "NO" otherwise. Note The first test case of the example It can be shown that the answer for the second test case of the example is "NO".
800
true
true
false
false
false
false
false
false
false
false
4,137
1829D
Initially you have a single pile with $$$n$$$ gold nuggets. In an operation you can do the following: Take any pile and split it into two piles, so that one of the resulting piles has exactly twice as many gold nuggets as the other. (All piles should have an integer number of nuggets.) One possible move is to take a pile of size $$$6$$$ and split it into piles of sizes $$$2$$$ and $$$4$$$, which is valid since $$$4$$$ is twice as large as $$$2$$$. Can you make a pile with exactly $$$m$$$ gold nuggets using zero or more operations? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^7$$$)xa0— the starting and target pile sizes, respectively. Output For each test case, output "YES" if you can make a pile of size exactly $$$m$$$, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 11 6 4 9 4 4 2 18 27 27 4 27 2 27 10 1 1 3 1 5 1 746001 2984004 Output YES YES NO NO YES YES NO YES YES NO NO Note The first test case is pictured in the statement. We can make a pile of size $$$4$$$. In the second test case, we can perform the following operations: $$${color{red}{9}} o {color{red}{6},3} o {4,2,3}$$$. The pile that is split apart is colored red before each operation. In the third test case, we can't perform a single operation. In the fourth test case, we can't end up with a larger pile than we started with.
1,000
false
false
true
true
false
false
true
false
false
false
1,311
1560D
You are given an integer $$$n$$$. In $$$1$$$ move, you can do one of the following actions: erase any digit of the number (it's acceptable that the number before the operation has exactly one digit and after the operation, it is "empty"); add one digit to the right. The actions may be performed in any order any number of times. Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number $$$301$$$ the digit $$$3$$$, the result is the number $$$01$$$ (not $$$1$$$). You need to perform the minimum number of actions to make the number any power of $$$2$$$ (i.e. there's an integer $$$k$$$ ($$$k ge 0$$$) such that the resulting number is equal to $$$2^k$$$). The resulting number must not have leading zeroes. E.g. consider $$$n=1052$$$. The answer is equal to $$$2$$$. First, let's add to the right one digit $$$4$$$ (the result will be $$$10524$$$). Then let's erase the digit $$$5$$$, so the result will be $$$1024$$$ which is a power of $$$2$$$. E.g. consider $$$n=8888$$$. The answer is equal to $$$3$$$. Let's erase any of the digits $$$8$$$ three times. The result will be $$$8$$$ which is a power of $$$2$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$n$$$ ($$$1 le n le 10^9$$$). Output For each test case, output in a separate line one integer $$$m$$$ — the minimum number of moves to transform the number into any power of $$$2$$$. Example Input 12 1052 8888 6 75 128 1 301 12048 1504 6656 1000000000 687194767 Output 2 3 1 3 0 0 2 1 3 4 9 2 Note The answer for the first test case was considered above. The answer for the second test case was considered above. In the third test case, it's enough to add to the right the digit $$$4$$$ — the number $$$6$$$ will turn into $$$64$$$. In the fourth test case, let's add to the right the digit $$$8$$$ and then erase $$$7$$$ and $$$5$$$ — the taken number will turn into $$$8$$$. The numbers of the fifth and the sixth test cases are already powers of two so there's no need to make any move. In the seventh test case, you can delete first of all the digit $$$3$$$ (the result is $$$01$$$) and then the digit $$$0$$$ (the result is $$$1$$$).
1,300
true
true
false
false
false
false
false
false
false
false
2,827
1530H
Alan Turing is standing on a tape divided into cells that is infinite in both directions. Cells are numbered with consecutive integers from left to right. Alan is initially standing in cell $$$0$$$. Every cell $$$x$$$ has cell $$$x - 1$$$ on the left and cell $$$x + 1$$$ on the right. Each cell can either contain an integer or be empty. Initially all cells are empty. Alan is given a permutation $$$a_1, a_2, ldots, a_n$$$ of integers from $$$1$$$ to $$$n$$$ that was chosen uniformly at random among all permutations of length $$$n$$$. At time $$$1$$$, integer $$$a_1$$$ is written down into cell $$$0$$$ where Alan is located. At each time $$$i$$$ from $$$2$$$ to $$$n$$$ inclusive, the following happens. First, Alan decides whether to stay in the same cell he's currently in, move to the neighboring cell on the left, or move to the neighboring cell on the right. After that, integer $$$a_i$$$ is written down into the cell where Alan is located. If that cell already contained some integer, the old integer is overwritten and irrelevant from that moment on. Once $$$a_n$$$ is written down into some cell at time $$$n$$$, sequence $$$b$$$ of all integers contained in the cells from left to right is formed. Empty cells are ignored. Turing's award is equal to the length of the longest increasing subsequence of sequence $$$b$$$. Help Alan and determine the largest possible value of his award if he acts optimally. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. Each test case is given in two lines. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 15,000$$$)xa0— the length of the permutation given to Alan. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the elements of the permutation. It is guaranteed that the permutation was chosen uniformly at random among all permutations of the corresponding length. The sum of $$$n$$$ over all test cases does not exceed $$$15,000$$$. Output For each test case output a single integerxa0— the largest possible value of Turing's award. Hacks are not allowed for this problem. Example Input 4 2 1 2 4 4 1 2 3 7 3 6 5 7 4 1 2 7 5 2 3 7 6 1 4 Note Longest increasing subsequence of sequence $$$b$$$ is the longest increasing sequence that can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements. In the first test case, Alan can make a decision only at time $$$2$$$. If Alan stays in cell $$$0$$$, sequence $$$b$$$ will be equal to $$$[2]$$$. If Alan moves to the left, to cell $$$-1$$$, sequence $$$b$$$ will be equal to $$$[2, 1]$$$. If Alan moves to the right, to cell $$$1$$$, sequence $$$b$$$ will be equal to $$$[1, 2]$$$. Only in the last case the length of the longest increasing subsequence of $$$b$$$ is $$$2$$$, therefore, the answer is equal to $$$2$$$. In the second test case, one of the optimal sequences of actions looks as follows: move to the left at times $$$2$$$ and $$$3$$$, and move to the right at time $$$4$$$. Then sequence $$$b$$$ will be equal to $$$[2, 3, 4]$$$, and the length of its longest increasing subsequence is $$$3$$$. In the third test case, one of the best ways is to always move to the left. Then sequence $$$b$$$ will be equal to $$$[2, 1, 4, 7, 5, 6, 3]$$$, and the length of its longest increasing subsequence is $$$4$$$. In the fourth test case, one of the best ways is to move to the right four times, then move to the left once, and stay in the same cell once. Sequence $$$b$$$ will be equal to $$$[5, 2, 3, 4, 6]$$$.
3,400
false
false
false
true
true
false
false
false
false
false
3,008
98D
In a far away kingdom is the famous Lio Shan monastery. Gods constructed three diamond pillars on the monastery's lawn long ago. Gods also placed on one pillar _n_ golden disks of different diameters (in the order of the diameters' decreasing from the bottom to the top). Besides, gods commanded to carry all the disks from the first pillar to the third one according to the following rules: you can carry only one disk in one move; you cannot put a larger disk on a smaller one. There was no universal opinion concerning what is to happen after the gods' will is done: some people promised world peace and eternal happiness to everyone, whereas others predicted that the kingdom will face communi… (gee, what am I rambling about?) the Armageddon. However, as everybody knew that it was impossible to solve the problem in less than 2_n_u2009-u20091 moves and the lazy Lio Shan monks never even started to solve it, everyone lives peacefully even though the problem was never solved and nobody was afraid of the Armageddon. However, the monastery wasn't doing so well lately and the wise prior Ku Sean Sun had to cut some disks at the edges and use the gold for the greater good. Wouldn't you think that the prior is entitled to have an air conditioning system? Besides, staying in the monastery all year is sooo dull… One has to have a go at something new now and then, go skiing, for example… Ku Sean Sun realize how big a mistake he had made only after a while: after he cut the edges, the diameters of some disks got the same; that means that some moves that used to be impossible to make, were at last possible (why, gods never prohibited to put a disk on a disk of the same diameter). Thus, the possible Armageddon can come earlier than was initially planned by gods. Much earlier. So much earlier, in fact, that Ku Sean Sun won't even have time to ski all he wants or relax under the air conditioner. The wise prior could never let that last thing happen and he asked one very old and very wise witch PikiWedia to help him. May be she can determine the least number of moves needed to solve the gods' problem. However, the witch laid out her cards and found no answer for the prior. Then he asked you to help him. Can you find the shortest solution of the problem, given the number of disks and their diameters? Keep in mind that it is allowed to place disks of the same diameter one on the other one, however, the order in which the disks are positioned on the third pillar in the end should match the initial order of the disks on the first pillar. Input The first line contains an integer _n_ — the number of disks (1u2009≤u2009_n_u2009≤u200920). The second line contains _n_ integers _d__i_ — the disks' diameters after Ku Sean Sun cut their edges. The diameters are given from the bottom to the top (1u2009≤u2009_d__i_u2009≤u200920, besides, _d__i_u2009≥u2009_d__i_u2009+u20091 for any 1u2009≤u2009_i_u2009<u2009_n_). Output Print on the first line number _m_ — the smallest number of moves to solve the gods' problem. Print on the next _m_ lines the description of moves: two space-separated positive integers _s__i_ and _t__i_ that determine the number of the pillar from which the disk is moved and the number of pillar where the disk is moved, correspondingly (1u2009≤u2009_s__i_,u2009_t__i_u2009≤u20093, _s__i_u2009≠u2009_t__i_). Note Pay attention to the third test demonstrating that the order of disks should remain the same in the end, even despite the disks' same radius. If this condition was not necessary to fulfill, the gods' task could have been solved within a smaller number of moves (three — simply moving the three disks from the first pillar on the third one).
2,500
false
false
false
false
false
true
false
false
false
false
9,493
1540B
You are given a tree consisting of $$$n$$$ nodes. You generate an array from the tree by marking nodes one by one. Initially, when no nodes are marked, a node is equiprobably chosen and marked from the entire tree. After that, until all nodes are marked, a node is equiprobably chosen and marked from the set of unmarked nodes with at least one edge to a marked node. It can be shown that the process marks all nodes in the tree. The final array $$$a$$$ is the list of the nodes' labels in order of the time each node was marked. Find the expected number of inversions in the array that is generated by the tree and the aforementioned process. The number of inversions in an array $$$a$$$ is the number of pairs of indices $$$(i, j)$$$ such that $$$i < j$$$ and $$$a_i > a_j$$$. For example, the array $$$[4, 1, 3, 2]$$$ contains $$$4$$$ inversions: $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(3, 4)$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 200$$$)xa0— the number of nodes in the tree. The next $$$n - 1$$$ lines each contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$; $$$x eq y$$$), denoting an edge between node $$$x$$$ and $$$y$$$. It's guaranteed that the given edges form a tree. Output Output the expected number of inversions in the generated array 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}$$$. Examples Input 6 2 1 2 3 6 1 1 4 2 5 Note This is the tree from the first sample: For the first sample, the arrays are almost fixed. If node $$$2$$$ is chosen initially, then the only possible array is $$$[2, 1, 3]$$$ ($$$1$$$ inversion). If node $$$3$$$ is chosen initially, then the only possible array is $$$[3, 1, 2]$$$ ($$$2$$$ inversions). If node $$$1$$$ is chosen initially, the arrays $$$[1, 2, 3]$$$ ($$$0$$$ inversions) and $$$[1, 3, 2]$$$ ($$$1$$$ inversion) are the only possibilities and equiprobable. In total, the expected number of inversions is $$$frac{1}{3}cdot 1 + frac{1}{3} cdot 2 + frac{1}{3} cdot (frac{1}{2} cdot 0 + frac{1}{2} cdot 1) = frac{7}{6}$$$. $$$166666669 cdot 6 = 7 pmod {10^9 + 7}$$$, so the answer is $$$166666669$$$. This is the tree from the second sample: This is the tree from the third sample:
2,300
true
false
false
true
false
false
true
false
false
true
2,941
39A
C*++ language is quite similar to C++. The similarity manifests itself in the fact that the programs written in C*++ sometimes behave unpredictably and lead to absolutely unexpected effects. For example, let's imagine an arithmetic expression in C*++ that looks like this (_expression_ is the main term): _expression_ ::= _summand_ _expression_u2009+u2009_summand_ _expression_u2009-u2009_summand_ _summand_ ::= _increment_ _coefficient_*_increment_ _increment_ ::= a++ ++a _coefficient_ ::= 012...1000 For example, "5*a++-3*++a+a++" is a valid expression in C*++. Thus, we have a sum consisting of several summands divided by signs "+" or "-". Every summand is an expression "a++" or "++a" multiplied by some integer coefficient. If the coefficient is omitted, it is suggested being equal to 1. The calculation of such sum in C*++ goes the following way. First all the summands are calculated one after another, then they are summed by the usual arithmetic rules. If the summand contains "a++", then during the calculation first the value of the "a" variable is multiplied by the coefficient, then value of "a" is increased by 1. If the summand contains "++a", then the actions on it are performed in the reverse order: first "a" is increased by 1, then — multiplied by the coefficient. The summands may be calculated in any order, that's why sometimes the result of the calculation is completely unpredictable! Your task is to find its largest possible value. Input The first input line contains an integer _a_ (u2009-u20091000u2009≤u2009_a_u2009≤u20091000) — the initial value of the variable "a". The next line contains an expression in C*++ language of the described type. The number of the summands in the expression does not exceed 1000. It is guaranteed that the line describing the expression contains no spaces and tabulation. Note Consider the second example. Initially _a_u2009=u20093. Suppose that at first the first summand is calculated, and then the second one is. The first summand gets equal to 3, and the value of _a_ is increased by 1. At the calculation of the second summand _a_ is increased once more (gets equal to 5). The value of the second summand is 5, and together they give 8. If we calculate the second summand first and the first summand later, then the both summands equals to 4, and the result is 8, too.
2,000
false
true
false
false
false
false
false
false
false
false
9,800
294A
Shaass has decided to hunt some birds. There are _n_ horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to _n_ from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delicious kind of birds in Shaass's territory. Supposed there are _a__i_ oskols sitting on the _i_-th wire. Sometimes Shaass shots one of the birds and the bird dies (suppose that this bird sat at the _i_-th wire). Consequently all the birds on the _i_-th wire to the left of the dead bird get scared and jump up on the wire number _i_u2009-u20091, if there exists no upper wire they fly away. Also all the birds to the right of the dead bird jump down on wire number _i_u2009+u20091, if there exists no such wire they fly away. Shaass has shot _m_ birds. You're given the initial number of birds on each wire, tell him how many birds are sitting on each wire after the shots. Input The first line of the input contains an integer _n_, (1u2009≤u2009_n_u2009≤u2009100). The next line contains a list of space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_, (0u2009≤u2009_a__i_u2009≤u2009100). The third line contains an integer _m_, (0u2009≤u2009_m_u2009≤u2009100). Each of the next _m_ lines contains two integers _x__i_ and _y__i_. The integers mean that for the _i_-th time Shaass shoot the _y__i_-th (from left) bird on the _x__i_-th wire, (1u2009≤u2009_x__i_u2009≤u2009_n_,u20091u2009≤u2009_y__i_). It's guaranteed there will be at least _y__i_ birds on the _x__i_-th wire at that moment. Output On the _i_-th line of the output print the number of birds on the _i_-th wire. Examples Input 5 10 10 10 10 10 5 2 5 3 13 2 12 1 13 4 6
800
true
false
true
false
false
false
false
false
false
false
8,656
1249B1
The only difference between easy and hard versions is constraints. There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed. For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on. Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$. Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. Output For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query.
1,000
true
false
false
false
false
false
false
false
false
false
4,463
436F
All modern mobile applications are divided into free and paid. Even a single application developers often release two versions: a paid version without ads and a free version with ads. Suppose that a paid version of the app costs _p_ (_p_ is an integer) rubles, and the free version of the application contains _c_ ad banners. Each user can be described by two integers: _a__i_ — the number of rubles this user is willing to pay for the paid version of the application, and _b__i_ — the number of banners he is willing to tolerate in the free version. The behavior of each member shall be considered strictly deterministic: if for user _i_, value _b__i_ is at least _c_, then he uses the free version, otherwise, if value _a__i_ is at least _p_, then he buys the paid version without advertising, otherwise the user simply does not use the application. Each user of the free version brings the profit of _c_u2009×u2009_w_ rubles. Each user of the paid version brings the profit of _p_ rubles. Your task is to help the application developers to select the optimal parameters _p_ and _c_. Namely, knowing all the characteristics of users, for each value of _c_ from 0 to (_max_xa0_b__i_)u2009+u20091 you need to determine the maximum profit from the application and the corresponding parameter _p_. Input The first line contains two integers _n_ and _w_ (1u2009≤u2009_n_u2009≤u2009105;xa01u2009≤u2009_w_u2009≤u2009105) — the number of users and the profit from a single banner. Each of the next _n_ lines contains two integers _a__i_ and _b__i_ (0u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009105) — the characteristics of the _i_-th user. Output Print (_max_xa0_b__i_)u2009+u20092 lines, in the _i_-th line print two integers: _pay_ — the maximum gained profit at _c_u2009=u2009_i_u2009-u20091, _p_ (0u2009≤u2009_p_u2009≤u2009109) — the corresponding optimal app cost. If there are multiple optimal solutions, print any of them.
3,000
false
false
false
true
true
false
true
false
false
false
8,092
1482F
You are given a weighted undirected graph on $$$n$$$ vertices along with $$$q$$$ triples $$$(u, v, l)$$$, where in each triple $$$u$$$ and $$$v$$$ are vertices and $$$l$$$ is a positive integer. An edge $$$e$$$ is called useful if there is at least one triple $$$(u, v, l)$$$ and a path (not necessarily simple) with the following properties: $$$u$$$ and $$$v$$$ are the endpoints of this path, $$$e$$$ is one of the edges of this path, the sum of weights of all edges on this path doesn't exceed $$$l$$$. Please print the number of useful edges in this graph. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2leq nleq 600$$$, $$$0leq mleq frac{n(n-1)}2$$$). Each of the following $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$w$$$ ($$$1leq u, vleq n$$$, $$$u eq v$$$, $$$1leq wleq 10^9$$$), denoting an edge connecting vertices $$$u$$$ and $$$v$$$ and having a weight $$$w$$$. The following line contains the only integer $$$q$$$ ($$$1leq qleq frac{n(n-1)}2$$$) denoting the number of triples. Each of the following $$$q$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$l$$$ ($$$1leq u, vleq n$$$, $$$u eq v$$$, $$$1leq lleq 10^9$$$) denoting a triple $$$(u, v, l)$$$. It's guaranteed that: the graph doesn't contain loops or multiple edges; all pairs $$$(u, v)$$$ in the triples are also different. Output Print a single integer denoting the number of useful edges in the graph. Examples Input 4 6 1 2 1 2 3 1 3 4 1 1 3 3 2 4 3 1 4 5 1 1 4 4 Input 4 2 1 2 10 3 4 10 6 1 2 11 1 3 11 1 4 11 2 3 11 2 4 11 3 4 9 Input 3 2 1 2 1 2 3 2 1 1 2 5 Note In the first example each edge is useful, except the one of weight $$$5$$$. In the second example only edge between $$$1$$$ and $$$2$$$ is useful, because it belongs to the path $$$1-2$$$, and $$$10 leq 11$$$. The edge between $$$3$$$ and $$$4$$$, on the other hand, is not useful. In the third example both edges are useful, for there is a path $$$1-2-3-2$$$ of length exactly $$$5$$$. Please note that the path may pass through a vertex more than once.
2,400
false
false
false
false
false
false
false
false
false
true
3,262
2032E
You are given a cyclic array $$$a$$$ with $$$n$$$ elements, where $$$n$$$ is odd. In each operation, you can do the following: Choose an index $$$1 le i le n$$$ and increase $$$a_{i - 1}$$$ by $$$1$$$, $$$a_i$$$ by $$$2$$$, and $$$a_{i + 1}$$$ by $$$1$$$. The element before the first element is the last element because this is a cyclic array. A cyclic array is called balanced if all its elements are equal to each other. Find any sequence of operations to make this cyclic array balanced or determine that it is impossible. Please note that you do not have to minimize the number of operations. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$) — 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 < 2 cdot 10^5$$$, $$$n$$$ is odd)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{6}$$$) — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case: If it is impossible to make the cyclic array balanced, output $$$-1$$$. Otherwise, output $$$n$$$ integers $$$v_1, v_2, ldots, v_n$$$ ($$$0 leq v_i leq 10^{18}$$$)xa0— where $$$v_i$$$ denotes the number of operations applied to index $$$i$$$. It can be proved that if any solution exists, then there exists a solution under the given constraints. If there are several solutions under the given constraints, output any of them. Example Input 6 3 2 1 2 3 1 2 3 5 1 2 1 2 1 7 1 2 1 2 1 3 1 9 10000 10000 10000 10000 10000 10001 10002 10001 10000 1 10 Output 0 1 0 2 1 0 2 0 3 0 2 4 2 7 0 8 0 6 1 1 1 1 1 1 0 1 1 0 Note In the first test case: After $$$1$$$ operation applied to index $$$i = 2$$$, the array $$$a = [3, 3, 3]$$$. In the second test case: After $$$2$$$ operations applied to index $$$i = 1$$$, the array $$$a = [5, 4, 5]$$$. After $$$1$$$ operation applied to index $$$i = 2$$$, the array $$$a = [6, 6, 6]$$$. In the third test case: After $$$2$$$ operations applied to index $$$i = 1$$$, the array $$$a = [5, 4, 1, 2, 3]$$$. After $$$3$$$ operations applied to index $$$i = 3$$$, the array $$$a = [5, 7, 7, 5, 3]$$$. After $$$2$$$ operations applied to index $$$i = 5$$$, the array $$$a = [7, 7, 7, 7, 7]$$$.
2,400
true
true
true
false
true
true
false
false
false
false
70
13A
Problem - 13A - Codeforces =============== xa0 . Output Output should contain required average value in format «X/Y», where X is the numerator and Y is the denominator. Examples Input 5 Output 7/3 Input 3 Output 2/1 Note In the first sample number 5 written in all bases from 2 to 4 looks so: 101, 12, 11. Sums of digits are 2, 3 and 2, respectively.
1,000
true
false
true
false
false
false
false
false
false
false
9,930
1741A
Two T-shirt sizes are given: $$$a$$$ and $$$b$$$. The T-shirt size is either a string M or a string consisting of several (possibly zero) characters X and one of the characters S or L. For example, strings M, XXL, S, XXXXXXXS could be the size of some T-shirts. And the strings XM, LL, SX are not sizes. The letter M stands for medium, S for small, L for large. The letter X refers to the degree of size (from eXtra). For example, XXL is extra-extra-large (bigger than XL, and smaller than XXXL). You need to compare two given sizes of T-shirts $$$a$$$ and $$$b$$$. The T-shirts are compared as follows: any small size (no matter how many letters X) is smaller than the medium size and any large size; any large size (regardless of the number of letters X) is larger than the medium size and any small size; the more letters X before S, the smaller the size; the more letters X in front of L, the larger the size. For example: XXXS < XS XXXL > XL XL > M XXL = XXL XXXXXS < M XL > XXXS Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each test case consists of one line, in which $$$a$$$ and $$$b$$$ T-shirt sizes are written. The lengths of the strings corresponding to the T-shirt sizes do not exceed $$$50$$$. It is guaranteed that all sizes are correct. Output For each test case, print on a separate line the result of comparing $$$a$$$ and $$$b$$$ T-shirt sizes (lines "<", ">" or "=" without quotes). Example Input 6 XXXS XS XXXL XL XL M XXL XXL XXXXXS M L M
800
false
false
true
false
false
false
false
false
false
false
1,838
1530B
To celebrate your birthday you have prepared a festive table! Now you want to seat as many guests as possible. The table can be represented as a rectangle with height $$$h$$$ and width $$$w$$$, divided into $$$h imes w$$$ cells. Let $$$(i, j)$$$ denote the cell in the $$$i$$$-th row and the $$$j$$$-th column of the rectangle ($$$1 le i le h$$$; $$$1 le j le w$$$). Into each cell of the table you can either put a plate or keep it empty. As each guest has to be seated next to their plate, you can only put plates on the edge of the tablexa0— into the first or the last row of the rectangle, or into the first or the last column. Formally, for each cell $$$(i, j)$$$ you put a plate into, at least one of the following conditions must be satisfied: $$$i = 1$$$, $$$i = h$$$, $$$j = 1$$$, $$$j = w$$$. To make the guests comfortable, no two plates must be put into cells that have a common side or corner. In other words, if cell $$$(i, j)$$$ contains a plate, you can't put plates into cells $$$(i - 1, j)$$$, $$$(i, j - 1)$$$, $$$(i + 1, j)$$$, $$$(i, j + 1)$$$, $$$(i - 1, j - 1)$$$, $$$(i - 1, j + 1)$$$, $$$(i + 1, j - 1)$$$, $$$(i + 1, j + 1)$$$. Put as many plates on the table as possible without violating the rules above. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. Each of the following $$$t$$$ lines describes one test case and contains two integers $$$h$$$ and $$$w$$$ ($$$3 le h, w le 20$$$)xa0— the height and the width of the table. Output For each test case, print $$$h$$$ lines containing $$$w$$$ characters each. Character $$$j$$$ in line $$$i$$$ must be equal to $$$1$$$ if you are putting a plate into cell $$$(i, j)$$$, and $$$0$$$ otherwise. If there are multiple answers, print any. All plates must be put on the edge of the table. No two plates can be put into cells that have a common side or corner. The number of plates put on the table under these conditions must be as large as possible. You are allowed to print additional empty lines. Note For the first test case, example output contains the only way to put $$$6$$$ plates on the table. For the second test case, there are many ways to put $$$4$$$ plates on the table, example output contains one of them. Putting more than $$$6$$$ plates in the first test case or more than $$$4$$$ plates in the second test case is impossible.
800
false
false
true
false
false
true
false
false
false
false
3,014
742B
There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious problem: Given an array and a number _x_, count the number of pairs of indices _i_,u2009_j_ (1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_n_) such that , where is bitwise xor operation (see notes for explanation). Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem. Input First line contains two integers _n_ and _x_ (1u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_x_u2009≤u2009105)xa0— the number of elements in the array and the integer _x_. Second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105)xa0— the elements of the array. Output Print a single integer: the answer to the problem. Note In the first sample there is only one pair of _i_u2009=u20091 and _j_u2009=u20092. so the answer is 1. In the second sample the only two pairs are _i_u2009=u20093, _j_u2009=u20094 (since ) and _i_u2009=u20091, _j_u2009=u20095 (since ). A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: [
1,500
true
false
false
false
false
false
true
false
false
false
6,830
377E
Kostya is playing the computer game Cookie Clicker. The goal of this game is to gather cookies. You can get cookies using different buildings: you can just click a special field on the screen and get the cookies for the clicks, you can buy a cookie factory, an alchemy lab, a time machine and it all will bring lots and lots of cookies. At the beginning of the game (time 0), Kostya has 0 cookies and no buildings. He has _n_ available buildings to choose from: the _i_-th building is worth _c__i_ cookies and when it's built it brings _v__i_ cookies at the end of each second. Also, to make the game more interesting to play, Kostya decided to add a limit: at each moment of time, he can use only one building. Of course, he can change the active building each second at his discretion. It's important that Kostya is playing a version of the game where he can buy new buildings and change active building only at time moments that are multiples of one second. Kostya can buy new building and use it at the same time. If Kostya starts to use a building at the time moment _t_, he can get the first profit from it only at the time moment _t_u2009+u20091. Kostya wants to earn at least _s_ cookies as quickly as possible. Determine the number of seconds he needs to do that. Input The first line contains two integers _n_ and _s_ (1u2009≤u2009_n_u2009≤u20092·105, 1u2009≤u2009_s_u2009≤u20091016)xa0— the number of buildings in the game and the number of cookies Kostya wants to earn. Each of the next _n_ lines contains two integers _v__i_ and _c__i_ (1u2009≤u2009_v__i_u2009≤u2009108, 0u2009≤u2009_c__i_u2009≤u2009108)xa0— the number of cookies the _i_-th building brings per second and the building's price. Output Output the only integerxa0— the minimum number of seconds Kostya needs to earn at least _s_ cookies. It is guaranteed that he can do it. Examples Input 1 10000000000000000 1 0
2,800
false
false
false
true
false
false
false
false
false
false
8,320
599E
Rooted tree is a connected graph without any simple cycles with one vertex selected as a root. In this problem the vertex number 1 will always serve as a root. Lowest common ancestor of two vertices _u_ and _v_ is the farthest from the root vertex that lies on both the path from _u_ to the root and on path from _v_ to the root. We will denote it as _LCA_(_u_,u2009_v_). Sandy had a rooted tree consisting of _n_ vertices that she used to store her nuts. Unfortunately, the underwater storm broke her tree and she doesn't remember all it's edges. She only managed to restore _m_ edges of the initial tree and _q_ triples _a__i_, _b__i_ and _c__i_, for which she supposes _LCA_(_a__i_,u2009_b__i_)u2009=u2009_c__i_. Help Sandy count the number of trees of size _n_ with vertex 1 as a root, that match all the information she remembered. If she made a mess and there are no such trees then print 0. Two rooted trees are considered to be distinct if there exists an edge that occur in one of them and doesn't occur in the other one. Input The first line of the input contains three integers _n_, _m_ and _q_ (1u2009≤u2009_n_u2009≤u200913,u20090u2009≤u2009_m_u2009<u2009_n_,u20090u2009≤u2009_q_u2009≤u2009100)xa0— the number of vertices, the number of edges and _LCA_ triples remembered by Sandy respectively. Each of the next _m_ lines contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_,u2009_u__i_u2009≠u2009_v__i_)xa0— the numbers of vertices connected by the _i_-th edge. It's guaranteed that this set of edges is a subset of edges of some tree. The last _q_ lines contain the triplets of numbers _a__i_, _b__i_, _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_,u2009_c__i_u2009≤u2009_n_). Each of these triples define _LCA_(_a__i_,u2009_b__i_)u2009=u2009_c__i_. It's not guaranteed that there exists a tree that satisfy all the given _LCA_ conditions. Output Print a single integerxa0— the number of trees of size _n_ that satisfy all the conditions. Note In the second sample correct answer looks like this: In the third sample there are two possible trees: In the fourth sample the answer is 0 because the information about _LCA_ is inconsistent.
2,600
false
false
false
true
false
false
false
false
false
false
7,447
1606D
You are given a matrix, consisting of $$$n$$$ rows and $$$m$$$ columns. The $$$j$$$-th cell of the $$$i$$$-th row contains an integer $$$a_{ij}$$$. First, you have to color each row of the matrix either red or blue in such a way that at least one row is colored red and at least one row is colored blue. Then, you have to choose an integer $$$k$$$ ($$$1 le k < m$$$) and cut the colored matrix in such a way that the first $$$k$$$ columns become a separate matrix (the left matrix) and the last $$$m-k$$$ columns become a separate matrix (the right matrix). The coloring and the cut are called perfect if two properties hold: every red cell in the left matrix contains an integer greater than every blue cell in the left matrix; every blue cell in the right matrix contains an integer greater than every red cell in the right matrix. Find any perfect coloring and cut, or report that there are none. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 5 cdot 10^5$$$; $$$n cdot m le 10^6$$$)xa0— the number of rows and the number of columns in the matrix, respectively. The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i1}, a_{i2}, dots, a_{im}$$$ ($$$1 le a_{ij} le 10^6$$$). The sum of $$$n cdot m$$$ over all testcases doesn't exceed $$$10^6$$$. Output For each testcase print an answer. If there are no perfect colorings and cuts in the matrix, then print "NO". Otherwise, first, print "YES". Then a string, consisting of $$$n$$$ characters: the $$$i$$$-th character should be 'R' if the $$$i$$$-th row is colored red and 'B' if it's colored blue. The string should contain at least one 'R' and at least one 'B'. Finally, print an integer $$$k$$$ ($$$1 le k < m$$$)xa0— the number of columns from the left that are cut. Example Input 3 5 5 1 5 8 8 7 5 2 1 4 3 1 6 9 7 5 9 3 3 3 2 1 7 9 9 8 3 3 8 9 8 1 5 3 7 5 7 2 6 3 3 3 2 2 2 1 1 1 4 4 4 Output YES BRBRB 1 NO YES RB 3 Note The coloring and the cut for the first testcase:
2,400
false
false
true
false
false
true
true
false
true
false
2,626
1582D
Vupsen and Pupsen were gifted an integer array. Since Vupsen doesn't like the number $$$0$$$, he threw away all numbers equal to $$$0$$$ from the array. As a result, he got an array $$$a$$$ of length $$$n$$$. Pupsen, on the contrary, likes the number $$$0$$$ and he got upset when he saw the array without zeroes. To cheer Pupsen up, Vupsen decided to come up with another array $$$b$$$ of length $$$n$$$ such that $$$sum_{i=1}^{n}a_i cdot b_i=0$$$. Since Vupsen doesn't like number $$$0$$$, the array $$$b$$$ must not contain numbers equal to $$$0$$$. Also, the numbers in that array must not be huge, so the sum of their absolute values cannot exceed $$$10^9$$$. Please help Vupsen to find any such array $$$b$$$! Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The next $$$2 cdot t$$$ lines contain the description of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^4 le a_i le 10^4$$$, $$$a_i eq 0$$$) — the elements of the array $$$a$$$. 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 $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ — elements of the array $$$b$$$ ($$$b_1+b_2+ldots +b_n le 10^9$$$, $$$b_i eq 0$$$, $$$sum_{i=1}^{n}a_i cdot b_i=0$$$). It can be shown that the answer always exists. Example Input 3 2 5 5 5 5 -2 10 -9 4 7 1 2 3 4 5 6 7 Output 1 -1 -1 5 1 -1 -1 -10 2 2 -3 5 -1 -1 Note In the first test case, $$$5 cdot 1 + 5 cdot (-1)=5-5=0$$$. You could also print $$$3$$$ $$$-3$$$, for example, since $$$5 cdot 3 + 5 cdot (-3)=15-15=0$$$ In the second test case, $$$5 cdot (-1) + (-2) cdot 5 + 10 cdot 1 + (-9) cdot (-1) + 4 cdot (-1)=-5-10+10+9-4=0$$$.
1,600
true
false
false
false
false
true
false
false
false
false
2,722
5D
Problem - 5D - Codeforces =============== xa0 by a direct road. To improve the road's traffic capacity, there was placed just one traffic sign, limiting the maximum speed. Traffic signs in Berland are a bit peculiar, because they limit the speed only at that point on the road where they are placed. Right after passing the sign it is allowed to drive at any speed. It is known that the car of an average Berland citizen has the acceleration (deceleration) speed of _a_ km/h2, and has maximum speed of _v_ km/h. The road has the length of _l_ km, and the speed sign, limiting the speed to _w_ km/h, is placed _d_ km (1u2009≤u2009_d_u2009<u2009_l_) away from the capital of Berland. The car has a zero speed at the beginning of the journey. Find the minimum time that an average Berland citizen will need to get from the capital to Bercouver, if he drives at the optimal speed. The car can enter Bercouver at any speed. Input The first line of the input file contains two integer numbers _a_ and _v_ (1u2009≤u2009_a_,u2009_v_u2009≤u200910000). The second line contains three integer numbers _l_, _d_ and _w_ (2u2009≤u2009_l_u2009≤u200910000; 1u2009≤u2009_d_u2009<u2009_l_; 1u2009≤u2009_w_u2009≤u200910000). Output Print the answer with at least five digits after the decimal point. Examples Input 1 1 2 1 3 Output 2.500000000000 Input 5 70 200 170 40 Output 8.965874696353
2,100
true
false
true
false
false
false
false
false
false
false
9,967