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
1016D
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the matrix that the teacher has constructed! Vasya knows that the matrix consists of _n_ rows and _m_ columns. For each row, he knows the xor (bitwise excluding or) of the elements in this row. The sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ denotes the xor of elements in rows with indices 1, 2, ..., _n_, respectively. Similarly, for each column, he knows the xor of the elements in this column. The sequence _b_1,u2009_b_2,u2009...,u2009_b__m_ denotes the xor of elements in columns with indices 1, 2, ..., _m_, respectively. Help Vasya! Find a matrix satisfying the given constraints or tell him that there is no suitable matrix. Input The first line contains two numbers _n_ and _m_xa0(2u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the dimensions of the matrix. The second line contains _n_ numbers _a_1,u2009_a_2,u2009...,u2009_a__n_xa0(0u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is the xor of all elements in row _i_. The third line contains _m_ numbers _b_1,u2009_b_2,u2009...,u2009_b__m_xa0(0u2009≤u2009_b__i_u2009≤u2009109), where _b__i_ is the xor of all elements in column _i_. Output If there is no matrix satisfying the given constraints in the first line, output "NO". Otherwise, on the first line output "YES", and then _n_ rows of _m_ numbers in each _c__i_1,u2009_c__i_2,u2009... ,u2009_c__im_xa0(0u2009≤u2009_c__ij_u2009≤u20092·109) — the description of the matrix. If there are several suitable matrices, it is allowed to print any of them.
1,800
true
false
false
false
false
true
false
false
false
false
5,609
1902C
You are given an integer array $$$a_1, a_2, dots, a_n$$$, all its elements are distinct. First, you are asked to insert one more integer $$$a_{n+1}$$$ into this array. $$$a_{n+1}$$$ should not be equal to any of $$$a_1, a_2, dots, a_n$$$. Then, you will have to make all elements of the array equal. At the start, you choose a positive integer $$$x$$$ ($$$x > 0$$$). In one operation, you add $$$x$$$ to exactly one element of the array. Note that $$$x$$$ is the same for all operations. What's the smallest number of operations it can take you to make all elements equal, after you choose $$$a_{n+1}$$$ and $$$x$$$? 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 $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$). All $$$a_i$$$ are distinct. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the smallest number of operations it can take you to make all elements equal, after you choose integers $$$a_{n+1}$$$ and $$$x$$$. Example Input 3 3 1 2 3 5 1 -19 17 -3 -15 1 10 Note In the first testcase, you can choose $$$a_{n+1} = 4$$$, the array becomes $$$[1, 2, 3, 4]$$$. Then choose $$$x = 1$$$ and apply the operation $$$3$$$ times to the first element, $$$2$$$ times to the second element, $$$1$$$ time to the third element and $$$0$$$ times to the fourth element. In the second testcase, you can choose $$$a_{n+1} = 13, x = 4$$$. In the third testcase, you can choose $$$a_{n+1} = 9, x = 1$$$. Then apply the operation once to $$$a_{n+1}$$$.
1,300
true
true
false
false
false
true
true
false
false
false
901
185B
As you very well know, the whole Universe traditionally uses three-dimensional Cartesian system of coordinates. In this system each point corresponds to three real coordinates (_x_,u2009_y_,u2009_z_). In this coordinate system, the distance between the center of the Universe and the point is calculated by the following formula: . Mushroom scientists that work for the Great Mushroom King think that the Universe isn't exactly right and the distance from the center of the Universe to a point equals _x__a_·_y__b_·_z__c_. To test the metric of mushroom scientists, the usual scientists offered them a task: find such _x_,u2009_y_,u2009_z_ (0u2009≤u2009_x_,u2009_y_,u2009_z_;xa0_x_u2009+u2009_y_u2009+u2009_z_u2009≤u2009_S_), that the distance between the center of the Universe and the point (_x_,u2009_y_,u2009_z_) is maximum possible in the metric of mushroom scientists. The mushroom scientists aren't good at maths, so they commissioned you to do the task. Note that in this problem, it is considered that 00u2009=u20091. Input The first line contains a single integer _S_ (1u2009≤u2009_S_u2009≤u2009103) — the maximum sum of coordinates of the sought point. The second line contains three space-separated integers _a_, _b_, _c_ (0u2009≤u2009_a_,u2009_b_,u2009_c_u2009≤u2009103) — the numbers that describe the metric of mushroom scientists. Output Print three real numbers — the coordinates of the point that reaches maximum value in the metrics of mushroom scientists. If there are multiple answers, print any of them that meets the limitations. A natural logarithm of distance from the center of the Universe to the given point in the metric of mushroom scientists shouldn't differ from the natural logarithm of the maximum distance by more than 10u2009-u20096. We think that _ln_(0)u2009=u2009u2009-u2009∞.
1,800
true
false
false
false
false
false
false
false
false
false
9,103
1111B
Every superhero has been given a power value by the Felicity Committee. The avengers crew wants to maximize the average power of the superheroes in their team by performing certain operations. Initially, there are $$$n$$$ superheroes in avengers team having powers $$$a_1, a_2, ldots, a_n$$$, respectively. In one operation, they can remove one superhero from their team (if there are at least two) or they can increase the power of a superhero by $$$1$$$. They can do at most $$$m$$$ operations. Also, on a particular superhero at most $$$k$$$ operations can be done. Can you help the avengers team to maximize the average power of their crew? Input The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 le n le 10^{5}$$$, $$$1 le k le 10^{5}$$$, $$$1 le m le 10^{7}$$$)xa0— the number of superheroes, the maximum number of times you can increase power of a particular superhero, and the total maximum number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{6}$$$)xa0— the initial powers of the superheroes in the cast of avengers. Output Output a single numberxa0— the maximum final average power. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$frac{a - b}{max{(1, b)}} le 10^{-6}$$$. Examples Output 11.00000000000000000000 Output 5.00000000000000000000 Note In the first example, the maximum average is obtained by deleting the first element and increasing the second element four times. In the second sample, one of the ways to achieve maximum average is to delete the first and the third element and increase the second and the fourth elements by $$$2$$$ each.
1,700
true
false
true
false
false
false
true
false
false
false
5,140
54C
In the probability theory the following paradox called Benford's law is known: "In many lists of random numbers taken from real sources, numbers starting with digit 1 occur much more often than numbers starting with any other digit" (that's the simplest form of the law). Having read about it on Codeforces, the Hedgehog got intrigued by the statement and wishes to thoroughly explore it. He finds the following similar problem interesting in particular: there are _N_ random variables, the _i_-th of which can take any integer value from some segment [_L__i_;_R__i_] (all numbers from this segment are equiprobable). It means that the value of the _i_-th quantity can be equal to any integer number from a given interval [_L__i_;_R__i_] with probability 1u2009/u2009(_R__i_u2009-u2009_L__i_u2009+u20091). The Hedgehog wants to know the probability of the event that the first digits of at least _K_% of those values will be equal to one. In other words, let us consider some set of fixed values of these random variables and leave only the first digit (the MSD — most significant digit) of each value. Then let's count how many times the digit 1 is encountered and if it is encountered in at least _K_ per cent of those _N_ values, than such set of values will be called a good one. You have to find the probability that a set of values of the given random variables will be a good one. Input The first line contains number _N_ which is the number of random variables (1u2009≤u2009_N_u2009≤u20091000). Then follow _N_ lines containing pairs of numbers _L__i_,u2009_R__i_, each of whom is a description of a random variable. It is guaranteed that 1u2009≤u2009_L__i_u2009≤u2009_R__i_u2009≤u20091018. The last line contains an integer _K_ (0u2009≤u2009_K_u2009≤u2009100). All the numbers in the input file are integers. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). Output Print the required probability. Print the fractional number with such a precision that the relative or absolute error of the result won't exceed 10u2009-u20099.
2,000
true
false
false
true
false
false
false
false
false
false
9,703
1932A
During your journey through computer universes, you stumbled upon a very interesting world. It is a path with $$$n$$$ consecutive cells, each of which can either be empty, contain thorns, or a coin. In one move, you can move one or two cells along the path, provided that the destination cell does not contain thorns (and belongs to the path). If you move to the cell with a coin, you pick it up. Here, green arrows correspond to legal moves, and the red arrow corresponds to an illegal move. You want to collect as many coins as possible. Find the maximum number of coins you can collect in the discovered world if you start in the leftmost cell of the path. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Then the descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the length of the path. The second line of each test case contains a string of $$$n$$$ characters, the description of the path. The character '.' denotes an empty cell, '@' denotes a cell with a coin, and '*' denotes a cell with thorns. It is guaranteed that the first cell is empty. Output For each test case, output a single integer, the maximum number of coins you can collect. Example Input 3 10 .@@*@.**@@ 5 .@@@@ 15 .@@..@***..@@@* Note The picture for the first example is in the problem statement. Here is the picture for the second example: And here is the picture for the third example:
800
false
true
true
true
false
false
false
false
false
false
694
1181E2
This problem differs from the previous problem only in constraints. Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual. Initially, there were $$$n$$$ different countries on the land that is now Berland. Each country had its own territory that was represented as a rectangle on the map. The sides of the rectangle were parallel to the axes, and the corners were located at points with integer coordinates. Territories of no two countries intersected, but it was possible that some territories touched each other. As time passed, sometimes two countries merged into one. It only happened if the union of their territories was also a rectangle. In the end only one country remainedxa0— Byteland. Initially, each country had a rectangular castle inside its territory. Its sides were parallel to the axes and its corners had integer coordinates. Some castles might touch the border of the corresponding country and sides or other castles. Miraculously, after all the unions the castles are still intact. Unfortunately, their locations are the only information we have to restore the initial territories of the countries. The possible formation of Byteland. The castles are shown in blue. Petya wonders why no information about the initial countries remained. He suspected that the whole story is a fake. You were recommended to him as a smart person. Please check whether or not there exists a possible set of initial territories that could make the story true. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 100,000$$$)xa0— the number of countries and castles. Each of the next $$$n$$$ lines contains four integers $$$a_i, b_i, c_i, d_i$$$ ($$$0 leq a_i < c_i leq 10^9$$$, $$$0 leq b_i < d_i leq 10^9$$$)xa0— the coordinates of the $$$i$$$-th castle, where $$$(a_i, b_i)$$$ are the coordinates of the lower left corner and $$$(c_i, d_i)$$$ are the coordinates of the upper right corner. It is guaranteed that no two castles intersect, however, they may touch. Output If there exists a possible set of territories that satisfies the story, print "YES", otherwise print "NO". You can print each letter in any case (upper or lower). Examples Input 4 0 0 1 2 0 2 1 3 1 0 2 1 1 1 2 3 Input 4 0 0 2 1 1 2 3 3 2 0 3 2 0 1 1 3 Note The castles in the first and second examples are shown on the pictures below.
3,000
false
true
false
false
false
false
true
false
true
false
4,809
610E
You are given a string _s_ of length _n_, consisting of first _k_ lowercase English letters. We define a _c_-repeat of some string _q_ as a string, consisting of _c_ copies of the string _q_. For example, string "acbacbacbacb" is a 4-repeat of the string "acb". Let's say that string _a_ contains string _b_ as a subsequence, if string _b_ can be obtained from _a_ by erasing some symbols. Let _p_ be a string that represents some permutation of the first _k_ lowercase English letters. We define function _d_(_p_) as the smallest integer such that a _d_(_p_)-repeat of the string _p_ contains string _s_ as a subsequence. There are _m_ operations of one of two types that can be applied to string _s_: 1. Replace all characters at positions from _l__i_ to _r__i_ by a character _c__i_. 2. For the given _p_, that is a permutation of first _k_ lowercase English letters, find the value of function _d_(_p_). All operations are performed sequentially, in the order they appear in the input. Your task is to determine the values of function _d_(_p_) for all operations of the second type. Input The first line contains three positive integers _n_, _m_ and _k_ (1u2009≤u2009_n_u2009≤u2009200u2009000,u20091u2009≤u2009_m_u2009≤u200920000,u20091u2009≤u2009_k_u2009≤u200910)xa0— the length of the string _s_, the number of operations and the size of the alphabet respectively. The second line contains the string _s_ itself. Each of the following lines _m_ contains a description of some operation: 1. Operation of the first type starts with 1 followed by a triple _l__i_, _r__i_ and _c__i_, that denotes replacement of all characters at positions from _l__i_ to _r__i_ by character _c__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_, _c__i_ is one of the first _k_ lowercase English letters). 2. Operation of the second type starts with 2 followed by a permutation of the first _k_ lowercase English letters. Output For each query of the second type the value of function _d_(_p_). Examples Input 7 4 3 abacaba 1 3 5 b 2 abc 1 4 4 c 2 cba Note After the first operation the string _s_ will be abbbbba. In the second operation the answer is 6-repeat of abc: ABcaBcaBcaBcaBcAbc. After the third operation the string _s_ will be abbcbba. In the fourth operation the answer is 5-repeat of cba: cbAcBacBaCBacBA. Uppercase letters means the occurrences of symbols from the string _s_.
2,500
false
false
false
false
true
false
false
false
false
false
7,402
271A
Problem - 271A - 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 *800 No tag edit access → Contest materials ") — the year number. Output Print a single integer — the minimum year number that is strictly larger than _y_ and all it's digits are distinct. It is guaranteed that the answer exists. Examples Input 1987 Output 2013 Input 2013 Output 2014
800
false
false
false
false
false
false
true
false
false
false
8,751
533A
The biggest gold mine in Berland consists of _n_ caves, connected by _n_u2009-u20091 transitions. The entrance to the mine leads to the cave number 1, it is possible to go from it to any remaining cave of the mine by moving along the transitions. The mine is being developed by the InMine Inc., _k_ miners work for it. Each day the corporation sorts miners into caves so that each cave has at most one miner working there. For each cave we know the height of its ceiling _h__i_ in meters, and for each miner we know his height _s__j_, also in meters. If a miner's height doesn't exceed the height of the cave ceiling where he is, then he can stand there comfortably, otherwise, he has to stoop and that makes him unhappy. Unfortunately, miners typically go on strike in Berland, so InMine makes all the possible effort to make miners happy about their work conditions. To ensure that no miner goes on strike, you need make sure that no miner has to stoop at any moment on his way from the entrance to the mine to his cave (in particular, he must be able to stand comfortably in the cave where he works). To reach this goal, you can choose exactly one cave and increase the height of its ceiling by several meters. However enlarging a cave is an expensive and complex procedure. That's why InMine Inc. asks you either to determine the minimum number of meters you should raise the ceiling of some cave so that it is be possible to sort the miners into the caves and keep all miners happy with their working conditions or to determine that it is impossible to achieve by raising ceiling in exactly one cave. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20095·105) — the number of caves in the mine. Then follows a line consisting of _n_ positive integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≤u2009_h__i_u2009≤u2009109), where _h__i_ is the height of the ceiling in the _i_-th cave. Next _n_u2009-u20091 lines contain the descriptions of transitions between the caves. Each line has the form _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_), where _a__i_ and _b__i_ are the numbers of the caves connected by a path. The next line contains integer _k_ (1u2009≤u2009_k_u2009≤u2009_n_). The last line contains _k_ integers _s_1,u2009_s_2,u2009...,u2009_s__k_ (1u2009≤u2009_s__j_u2009≤u2009109), where _s__j_ is the _j_-th miner's height. Output In the single line print the minimum number of meters that you need to raise the ceiling by in some cave so that all miners could be sorted into caves and be happy about the work conditions. If it is impossible to do, print u2009-u20091. If it is initially possible and there's no need to raise any ceiling, print 0. Examples Input 6 5 8 4 6 3 12 1 2 1 3 4 2 2 5 6 3 6 7 4 2 5 3 11 Input 7 10 14 7 12 4 50 1 1 2 2 3 2 4 5 1 6 5 1 7 6 7 3 4 8 8 10 Note In the first sample test we should increase ceiling height in the first cave from 5 to 11. After that we can distribute miners as following (first goes index of a miner, then index of a cave): . In the second sample test there is no need to do anything since it is already possible to distribute miners as following: . In the third sample test it is impossible.
3,000
false
true
false
false
true
false
false
true
false
false
7,707
566D
Even the most successful company can go through a crisis period when you have to make a hard decision — to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following model of a company. There are _n_ people working for the Large Software Company. Each person belongs to some department. Initially, each person works on his own project in his own department (thus, each company initially consists of _n_ departments, one person in each). However, harsh times have come to the company and the management had to hire a crisis manager who would rebuild the working process in order to boost efficiency. Let's use _team_(_person_) to represent a team where person _person_ works. A crisis manager can make decisions of two types: 1. Merge departments _team_(_x_) and _team_(_y_) into one large department containing all the employees of _team_(_x_) and _team_(_y_), where _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_) — are numbers of two of some company employees. If _team_(_x_) matches _team_(_y_), then nothing happens. 2. Merge departments _team_(_x_),u2009_team_(_x_u2009+u20091),u2009...,u2009_team_(_y_), where _x_ and _y_ (1u2009≤u2009_x_u2009≤u2009_y_u2009≤u2009_n_) — the numbers of some two employees of the company. At that the crisis manager can sometimes wonder whether employees _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_) work at the same department. Help the crisis manager and answer all of his queries. Input The first line of the input contains two integers _n_ and _q_ (1u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_q_u2009≤u2009500u2009000) — the number of the employees of the company and the number of queries the crisis manager has. Next _q_ lines contain the queries of the crisis manager. Each query looks like _type_xa0_x_xa0_y_, where . If _type_u2009=u20091 or _type_u2009=u20092, then the query represents the decision of a crisis manager about merging departments of the first and second types respectively. If _type_u2009=u20093, then your task is to determine whether employees _x_ and _y_ work at the same department. Note that _x_ can be equal to _y_ in the query of any type. Output For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department. Examples Input 8 6 3 2 5 1 2 5 3 2 5 2 4 7 2 1 2 3 1 7
1,900
false
false
false
false
true
false
false
false
false
false
7,588
1250G
Eulampius has created a game with the following rules: there are two players in the game: a human and a computer; the game lasts for no more than $$$n$$$ rounds. Initially both players have $$$0$$$ points. In the $$$j$$$-th round the human gains $$$a_j$$$ points, and the computer gains $$$b_j$$$ points. The points are gained simultaneously; the game ends when one of the players gets $$$k$$$ or more points. This player loses the game. If both players get $$$k$$$ or more points simultaneously, both lose; if both players have less than $$$k$$$ points after $$$n$$$ rounds, the game ends in a tie; after each round the human can push the "Reset" button. If the human had $$$x$$$ points, and the computer had $$$y$$$ points before the button is pushed (of course, $$$x < k$$$ and $$$y < k$$$), then after the button is pushed the human will have $$$x' = max(0, , x - y)$$$ points, and the computer will have $$$y' = max(0, , y - x)$$$ points. E.u2009g. the push of "Reset" button transforms the state $$$(x=3, , y=5)$$$ into the state $$$(x'=0, , y'=2)$$$, and the state $$$(x=8, , y=2)$$$ into the state $$$(x'=6, , y'=0)$$$. Eulampius asked his friend Polycarpus to test the game. Polycarpus has quickly revealed that amounts of points gained by the human and the computer in each of $$$n$$$ rounds are generated before the game and stored in a file. In other words, the pushes of the "Reset" button do not influence the values $$$a_j$$$ and $$$b_j$$$, so sequences $$$a$$$ and $$$b$$$ are fixed and known in advance. Polycarpus wants to make a plan for the game. He would like to win the game pushing the "Reset" button as few times as possible. Your task is to determine this minimal number of pushes or determine that Polycarpus cannot win. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10000$$$)xa0— the number of test cases. Then the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$2 le k le 10^9$$$)xa0— the maximum possible number of rounds in the game and the number of points, after reaching which a player loses, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_j < k)$$$, where $$$a_j$$$ is the amount of points the human gains in the $$$j$$$-th round. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_j < k$$$), where $$$b_j$$$ is the amount of points the computer gains in the $$$j$$$-th round. The sum of $$$n$$$ over all test cases in the input does not exceed $$$2 cdot 10^5$$$. Output Print the answers for all test cases in the order they appear in the input. If Polycarpus cannot win the game, then simply print one line "-1" (without quotes). In this case, you should not output anything else for that test case. Otherwise, the first line of the test case answer should contain one integer $$$d$$$xa0— the minimum possible number of "Reset" button pushes, required to win the game. The next line should contain $$$d$$$ distinct integers $$$r_1, r_2, dots, r_d$$$ ($$$1 le r_i < n$$$)xa0— the numbers of rounds, at the end of which Polycarpus has to press the "Reset" button, in arbitrary order. If $$$d=0$$$ then either leave the second line of the test case answer empty, or do not print the second line at all. If there are several possible solutions, print any of them. Example Input 3 4 17 1 3 5 7 3 5 7 9 11 17 5 2 8 2 4 6 1 2 7 2 5 4 6 3 3 5 1 7 4 2 5 3 6 17 6 1 2 7 2 5 1 7 4 2 5 3 Note In the second test case, if the human pushes the "Reset" button after the second and the fourth rounds, the game goes as follows: 1. after the first round the human has $$$5$$$ points, the computerxa0— $$$4$$$ points; 2. after the second round the human has $$$7$$$ points, the computerxa0— $$$10$$$ points; 3. the human pushes the "Reset" button and now he has $$$0$$$ points and the computerxa0— $$$3$$$ points; 4. after the third round the human has $$$8$$$ points, the computerxa0— $$$6$$$ points; 5. after the fourth round the human has $$$10$$$ points, the computerxa0— $$$9$$$ points; 6. the human pushes "Reset" button again, after it he has $$$1$$$ point, the computerxa0— $$$0$$$ points; 7. after the fifth round the human has $$$5$$$ points, the computerxa0— $$$5$$$ points; 8. after the sixth round the human has $$$11$$$ points, the computerxa0— $$$6$$$ points; 9. after the seventh round the human has $$$12$$$ points, the computerxa0— $$$13$$$ points; 10. after the eighth round the human has $$$14$$$ points, the computerxa0— $$$17$$$ points; 11. the human wins, as the computer has $$$k$$$ or more points and the humanxa0— strictly less than $$$k$$$ points.
2,300
false
true
false
true
false
false
false
false
false
false
4,449
546E
In the country there are _n_ cities and _m_ bidirectional roads between them. Each city has an army. Army of the _i_-th city consists of _a__i_ soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road. Check if is it possible that after roaming there will be exactly _b__i_ soldiers in the _i_-th city. Input First line of input consists of two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_m_u2009≤u2009200). Next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009100). Next line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (0u2009≤u2009_b__i_u2009≤u2009100). Then _m_ lines follow, each of them consists of two integers _p_ and _q_ (1u2009≤u2009_p_,u2009_q_u2009≤u2009_n_, _p_u2009≠u2009_q_) denoting that there is an undirected road between cities _p_ and _q_. It is guaranteed that there is at most one road between each pair of cities. Output If the conditions can not be met output single word "NO". Otherwise output word "YES" and then _n_ lines, each of them consisting of _n_ integers. Number in the _i_-th line in the _j_-th column should denote how many soldiers should road from city _i_ to city _j_ (if _i_u2009≠u2009_j_) or how many soldiers should stay in city _i_ (if _i_u2009=u2009_j_). If there are several possible answers you may output any of them. Examples Input 4 4 1 2 6 3 3 5 3 1 1 2 2 3 3 4 4 2 Output YES 1 0 0 0 2 0 0 0 0 5 1 0 0 0 2 1
2,100
true
false
false
false
false
false
false
false
false
true
7,653
1893E
You are given an undirected connected graph in which any two distinct simple cycles do not have common vertices. Since the graph can be very large, it is given to you in a compressed form: for each edge, you are also given a number $$$d$$$, which indicates that there are $$$d$$$ additional vertices on this edge. You need to assign a weight to each vertex and each edge of the graphxa0— an integer from $$$1$$$ to $$$3$$$. An edge of the graph is called good if the . Each of the next $$$m$$$ lines contains three integers $$$a_i, b_i$$$, and $$$d_i$$$ ($$$1 le a_i, b_i le n$$$, $$$a_i e b_i$$$, $$$0 le d_i le 10^9$$$), indicating that there is an edge in the graph connecting vertices $$$a_i$$$ and $$$b_i$$$. Additionally, on this edge, there are $$$d_i$$$ additional vertices. It is guaranteed that the given graph is connected, there are no multiple edges, loops, and any two distinct simple cycles of the graph do not have common vertices. Output Output a single integerxa0— the answer to the problem modulo $$$998,244,353$$$. Examples Input 3 3 1 2 0 2 3 0 3 1 0 Input 6 7 1 2 0 2 3 0 3 1 0 4 5 0 5 6 0 6 4 0 4 3 0 Input 3 3 1 2 0 2 3 110850709 3 1 1000000000 Note In the first test, the graph is a simple cycle of $$$3$$$ vertices. It can be shown, that there are exactly $$$12$$$ ways to assign weights, to make all vertexes and edges good. In the second test, the graph has the form of two simple cycles of $$$3$$$ vertices connected by an edge. It can be shown that for such a graph there are no ways to arrange weights so that all vertices and edges are good.
3,500
false
false
false
true
false
false
false
false
false
true
947
1090H
, # Problem A. Linearization Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes Bitwise “and” of two non-negative integers is calculated as follows: write both numbers in binary, then the i-th binary digit of the result is equal to 1 if both arguments have the i-th digit equal to 1. For example, (14 and 7) = (1110 2 and 0111 2) = 110 2 = 6 .“Exclusive or” (xor) of two binary digits equals 1 if they are unequal, and 0 if they are equal. Thus, 0 xor 0 = 0 , 0 xor 1 = 1 , 1 xor 0 = 1 and 1 xor 1 = 0 .Parity function P (x) for a non-negative integer x equals 1 if the binary notation of x has odd number of ones, and 0 if the binary notation of x has even number of ones. For example, P (5) = P (101 2) = 0 , P (7) = P (111 2) = 1 .Consider a binary string whose length is a power of two: s = s0s1 . . . s n−1, where n = 2 k. We will call this string linear , if there is an integer x, 0 x14 x < n , and a binary digit b, such that for all i from 0 to n x00 1 holds si = P (i and x) xor b.For example, a string “ 1100 ” is linear: take x = 2 = 10 2 and b = 1 . x0f s0 = P (0 and 2) xor 1 = P (0) xor 1 = 0 xor 1 = 1 x0f s1 = P (1 and 2) xor 1 = P (0) xor 1 = 0 xor 1 = 1 x0f s2 = P (2 and 2) xor 1 = P (2) xor 1 = 1 xor 1 = 0 x0f s3 = P (3 and 2) xor 1 = P (2) xor 1 = 1 xor 1 = 0 Meanwhile, “ 0001 ” is not linear: whatever x we chose, we would have P (0 and x) = P (0) = 0 , therefore b = 0 . We have 0 = P (1 and x) and 0 = P (2 and x), therefore x = 0 . But P (3 and 0) = 0 ̸ = s3 = 1 .Consider a binary string. In one action you can take a continuous segment of digits and invert them: change all zeros to ones and vice versa. Call hardness of linearization of this string the minimal number of actions one needs to make it linear. For example, the hardness of linearization for the string “ 0001 ” is 1: you can invert the left three digits to get the string “ 1111 ” which is linear with x = 0 , b = 1 . There are other ways to linearize it in one action. You are given a string t and q queries (li, r i). For each query, consider a substring of t from li-th digit to ri-th digit, inclusive. Digits of t are numbered from left to right, starting with 0. It is guaranteed that the length of each query is a power of two. Calculate the hardness of linearization for every given substring. # Input The first line of input contains a single integer m — the length of the string t (1 x14 m x14 200 000) . The second line contains a binary string t of length m.The next line contains integer q — the number of queries ( 1 x14 q x14 200 000 ). Each of the next q lines contains two integers, li and ri (0 x14 li x14 ri < m , ri x00 li + 1 x15 2, substring length is a power of two). # Output For each query, print one integer: the hardness of linearization of the corresponding substring of t. Page 1 of 2 , # Example standard input standard output 800000101 30 7 2 5 0 3 210 # Note In the first query we need to linearize the whole string. This can be done, for example, by inverting the segment from 4-th to 6-th digit, getting the string “ 00001011 ”, and then inverting the 5-th digit, getting “00001111 ” which is linear with x = 4 and b = 0 .In the second query, the string “ 0001’ ’ can be linearized in one action, as described in the problem statement. In the third query the string “ 0000 ” is already linear with x = 0 , b = 0 . Page 2 of 2
2,900
false
false
false
false
false
false
false
false
false
true
5,265
985C
You have _m_u2009=u2009_n_·_k_ wooden staves. The _i_-th stave has length _a__i_. You have to assemble _n_ barrels consisting of _k_ staves each, you can use any _k_ staves to construct a barrel. Each stave must belong to exactly one barrel. Let volume _v__j_ of barrel _j_ be equal to the length of the minimal stave in it. You want to assemble exactly _n_ barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed _l_, i.e. _v__x_u2009-u2009_v__y_u2009≤u2009_l_ for any 1u2009≤u2009_x_u2009≤u2009_n_ and 1u2009≤u2009_y_u2009≤u2009_n_. Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above. Input The first line contains three space-separated integers _n_, _k_ and _l_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009105, 1u2009≤u2009_n_·_k_u2009≤u2009105, 0u2009≤u2009_l_u2009≤u2009109). The second line contains _m_u2009=u2009_n_·_k_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009≤u2009_a__i_u2009≤u2009109) — lengths of staves. Output Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly _n_ barrels satisfying the condition _v__x_u2009-u2009_v__y_u2009≤u2009_l_ for any 1u2009≤u2009_x_u2009≤u2009_n_ and 1u2009≤u2009_y_u2009≤u2009_n_. Examples Input 4 2 1 2 2 1 2 3 2 2 3 Note In the first example you can form the following barrels: [1,u20092], [2,u20092], [2,u20093], [2,u20093]. In the second example you can form the following barrels: [10], [10]. In the third example you can form the following barrels: [2,u20095]. In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.
1,500
false
true
false
false
false
false
false
false
false
false
5,777
1006A
Mishka got an integer array $$$a$$$ of length $$$n$$$ as a birthday present (what a surprise!). Mishka doesn't like this present and wants to change it somehow. He has invented an algorithm and called it "Mishka's Adjacent Replacements Algorithm". This algorithm can be represented as a sequence of steps: Replace each occurrence of $$$1$$$ in the array $$$a$$$ with $$$2$$$; Replace each occurrence of $$$2$$$ in the array $$$a$$$ with $$$1$$$; Replace each occurrence of $$$3$$$ in the array $$$a$$$ with $$$4$$$; Replace each occurrence of $$$4$$$ in the array $$$a$$$ with $$$3$$$; Replace each occurrence of $$$5$$$ in the array $$$a$$$ with $$$6$$$; Replace each occurrence of $$$6$$$ in the array $$$a$$$ with $$$5$$$; $$$dots$$$ Replace each occurrence of $$$10^9 - 1$$$ in the array $$$a$$$ with $$$10^9$$$; Replace each occurrence of $$$10^9$$$ in the array $$$a$$$ with $$$10^9 - 1$$$. Note that the dots in the middle of this algorithm mean that Mishka applies these replacements for each pair of adjacent integers ($$$2i - 1, 2i$$$) for each $$$i in{1, 2, ldots, 5 cdot 10^8}$$$ as described above. For example, for the array $$$a = [1, 2, 4, 5, 10]$$$, the following sequence of arrays represents the algorithm: $$$[1, 2, 4, 5, 10]$$$ $$$ ightarrow$$$ (replace all occurrences of $$$1$$$ with $$$2$$$) $$$ ightarrow$$$ $$$[2, 2, 4, 5, 10]$$$ $$$ ightarrow$$$ (replace all occurrences of $$$2$$$ with $$$1$$$) $$$ ightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$ ightarrow$$$ (replace all occurrences of $$$3$$$ with $$$4$$$) $$$ ightarrow$$$ $$$[1, 1, 4, 5, 10]$$$ $$$ ightarrow$$$ (replace all occurrences of $$$4$$$ with $$$3$$$) $$$ ightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$ ightarrow$$$ (replace all occurrences of $$$5$$$ with $$$6$$$) $$$ ightarrow$$$ $$$[1, 1, 3, 6, 10]$$$ $$$ ightarrow$$$ (replace all occurrences of $$$6$$$ with $$$5$$$) $$$ ightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$ ightarrow$$$ $$$dots$$$ $$$ ightarrow$$$ $$$[1, 1, 3, 5, 10]$$$ $$$ ightarrow$$$ (replace all occurrences of $$$10$$$ with $$$9$$$) $$$ ightarrow$$$ $$$[1, 1, 3, 5, 9]$$$. The later steps of the algorithm do not change the array. Mishka is very lazy and he doesn't want to apply these changes by himself. But he is very interested in their result. Help him find it. Input The first line of the input contains one integer number $$$n$$$ ($$$1 le n le 1000$$$) — the number of elements in Mishka's birthday present (surprisingly, an array). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) — the elements of the array.
800
false
false
true
false
false
false
false
false
false
false
5,655
2041H
Image generated by ChatGPT 4o. Alice likes singing. As a singing enthusiast, Alice has listened to countless songs and has tried singing them many times. However, occasionally, some songs make Alice feel bored. After some research, Alice believes that this is because even though the songs she chose are all different, due to her instinctive preference, they all turn out to be musically similar to one another. To thoroughly analyze this, Alice decided to study the sheet music of the songs. For convenience, Alice represented a song of length $$$n$$$ as an integer sequence $$$a_1, a_2, ldots, a_n$$$, where $$$a_i$$$ is the pitch of the $$$i$$$-th note. Then she defined the musical equivalence between songs. Two songs $$$a_1, a_2, ldots, a_n$$$ and $$$b_1, b_2, ldots, b_n$$$ of length $$$n$$$ are musically equivalent if for all $$$1leq i<n$$$, both $$$a_i, a_{i+1}$$$ and $$$b_{i}, b_{i+1}$$$ have the same pitch relationship. More specifically, $$$a_i, a_{i+1}$$$ and $$$b_i, b_{i+1}$$$ have the same pitch relationship if either $$$a_i < a_{i + 1}$$$ and $$$b_i < b_{i + 1}$$$, $$$a_i = a_{i + 1}$$$ and $$$b_i = b_{i + 1}$$$, or $$$a_i > a_{i + 1}$$$ and $$$b_i > b_{i + 1}$$$. For example, $$$1, 2, 3, 3, 2$$$ and $$$5, 9, 13, 13, 1$$$ are musically equivalent, while $$$1, 2, 3, 2, 1$$$ and $$$1, 2, 2, 2, 1$$$ are not. Having practiced consistently for a long time, Alice is able to sing any note in the range of $$$[1, k]$$$. She wants to know how many different songs of length $$$n$$$ within her range there are, if we treat musically equivalent songs as the same one. Can you help her calculate the number? Since the answer might be large, print the answer modulo $$$998244353$$$.
2,300
true
false
false
true
false
false
false
false
false
false
6
1486B
You and your friends live in $$$n$$$ houses. Each house is located on a 2D plane, in a point with integer coordinates. There might be different houses located in the same point. The mayor of the city is asking you for places for the building of the Eastern exhibition. You have to find the number of places (points with integer coordinates), so that the summary distance from all the houses to the exhibition is minimal. The exhibition can be built in the same point as some house. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$x_1 - x_2 + y_1 - y_2$$$, where $$$x$$$ is the absolute value of $$$x$$$. Input First line contains a single integer $$$t$$$ $$$(1 leq t leq 1000)$$$ — the number of test cases. The first line of each test case contains a single integer $$$n$$$ $$$(1 leq n leq 1000)$$$. Next $$$n$$$ lines describe the positions of the houses $$$(x_i, y_i)$$$ $$$(0 leq x_i, y_i leq 10^9)$$$. It's guaranteed that the sum of all $$$n$$$ does not exceed $$$1000$$$. Output For each test case output a single integer - the number of different positions for the exhibition. The exhibition can be built in the same point as some house. Example Input 6 3 0 0 2 0 1 2 4 1 0 0 2 2 3 3 1 4 0 0 0 1 1 0 1 1 2 0 0 1 1 2 0 0 2 0 2 0 0 0 0 Note Here are the images for the example test cases. Blue dots stand for the houses, green — possible positions for the exhibition. First test case. Second test case. Third test case. Fourth test case. Fifth test case. Sixth test case. Here both houses are located at $$$(0, 0)$$$.
1,500
false
false
false
false
false
false
false
true
true
false
3,252
887E
Masha's little brother draw two points on a sheet of paper. After that, he draws some circles and gave the sheet to his sister. Masha has just returned from geometry lesson so she instantly noticed some interesting facts about brother's drawing. At first, the line going through two points, that brother drew, doesn't intersect or touch any circle. Also, no two circles intersect or touch, and there is no pair of circles such that one circle is located inside another. Moreover, for each circle, Masha drew a square of the minimal area with sides parallel axis such that this circle is located inside the square and noticed that there is no two squares intersect or touch and there is no pair of squares such that one square is located inside other. Now Masha wants to draw circle of minimal possible radius such that it goes through two points that brother drew and doesn't intersect any other circle, but other circles can touch Masha's circle and can be located inside it. It's guaranteed, that answer won't exceed 1012. It should be held for hacks as well. Input First line contains four integers _x_1, _y_1, _x_2, _y_2 (u2009-u2009105u2009≤u2009_x_1,u2009_y_1,u2009_x_2,u2009_y_2u2009≤u2009105)xa0— coordinates of points that brother drew. First point has coordinates (_x_1, _y_1) and second point has coordinates (_x_2, _y_2). These two points are different. The second line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of circles that brother drew. Next _n_ lines contains descriptions of circles. Each line contains three integers _x__i_, _y__i_, _r__i_ (u2009-u2009105u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009105, 1u2009≤u2009_r__i_u2009≤u2009105) describing circle with center (_x__i_, _y__i_) and radius _r__i_. Output Output smallest real number, that it's possible to draw a circle with such radius through given points in such a way that it doesn't intersect other circles. The output is considered correct if it has a relative or absolute error of at most 10u2009-u20094.
2,800
false
false
false
false
false
false
false
true
true
false
6,198
1471B
You have given an array $$$a$$$ of length $$$n$$$ and an integer $$$x$$$ to a brand new robot. What the robot does is the following: it iterates over the elements of the array, let the current element be $$$q$$$. If $$$q$$$ is divisible by $$$x$$$, the robot adds $$$x$$$ copies of the integer $$$frac{q}{x}$$$ to the end of the array, and moves on to the next element. Note that the newly added elements could be processed by the robot later. Otherwise, if $$$q$$$ is not divisible by $$$x$$$, the robot shuts down. Please determine the sum of all values of the array at the end of the process. Input The first input line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 leq n leq 10^5$$$, $$$2 leq x leq 10^9$$$)xa0— the length of the array and the value which is used by the robot. The next line contains integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the initial values in the array. It is guaranteed that the sum of values $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case the array initially consists of a single element $$$[12]$$$, and $$$x=2$$$. After the robot processes the first element, the array becomes $$$[12, 6, 6]$$$. Then the robot processes the second element, and the array becomes $$$[12, 6, 6, 3, 3]$$$. After the robot processes the next element, the array becomes $$$[12, 6, 6, 3, 3, 3, 3]$$$, and then the robot shuts down, since it encounters an element that is not divisible by $$$x = 2$$$. The sum of the elements in the resulting array is equal to $$$36$$$. In the second test case the array initially contains integers $$$[4, 6, 8, 2]$$$, and $$$x=2$$$. The resulting array in this case looks like $$$ [4, 6, 8, 2, 2, 2, 3, 3, 4, 4, 1, 1, 1, 1, 1, 1]$$$.
1,100
true
true
true
false
false
false
true
false
false
false
3,325
1602B
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 751 (Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms implementation *1100 No tag edit access → Contest materials Announcement Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST B. Divine Array time limit per test2 seconds memory limit per test256 megabytes Black is gifted with a Divine array $$$a$$$ consisting of $$$n$$$ ($$$1 le n le 2000$$$) integers. Each position in $$$a$$$ has an initial value. After shouting a curse over the array, it becomes angry and starts an unstoppable transformation. The transformation consists of infinite steps. Array $$$a$$$ changes at the $$$i$$$-th step in the following way: for every position $$$j$$$, $$$a_j$$$ becomes equal to the number of occurrences of $$$a_j$$$ in $$$a$$$ before starting this step. Here is an example to help you understand the process better: Initial array: $$$2$$$ $$$1$$$ $$$1$$$ $$$4$$$ $$$3$$$ $$$1$$$ $$$2$$$ After the $$$1$$$-st step: $$$2$$$ $$$3$$$ $$$3$$$ $$$1$$$ $$$1$$$ $$$3$$$ $$$2$$$ After the $$$2$$$-nd step: $$$2$$$ $$$3$$$ $$$3$$$ $$$2$$$ $$$2$$$ $$$3$$$ $$$2$$$ After the $$$3$$$-rd step: $$$4$$$ $$$3$$$ $$$3$$$ $$$4$$$ $$$4$$$ $$$3$$$ $$$4$$$ ... ... In the initial array, we had two $$$2$$$-s, three $$$1$$$-s, only one $$$4$$$ and only one $$$3$$$, so after the first step, each element became equal to the number of its occurrences in the initial array: all twos changed to $$$2$$$, all ones changed to $$$3$$$, four changed to $$$1$$$ and three changed to $$$1$$$. The transformation steps continue forever. You have to process $$$q$$$ queries: in each query, Black is curious to know the value of $$$a_x$$$ after the $$$k$$$-th step of transformation. 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. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2000$$$)xa0— the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the initial values of array $$$a$$$. The third line of each test case contains a single integer $$$q$$$ ($$$1 le q le 100,000$$$)xa0— the number of queries. Next $$$q$$$ lines contain the information about queriesxa0— one query per line. The $$$i$$$-th line contains two integers $$$x_i$$$ and $$$k_i$$$ ($$$1 le x_i le n$$$; $$$0 le k_i le 10^9$$$), meaning that Black is asking for the value of $$$a_{x_i}$$$ after the $$$k_i$$$-th step of transformation. $$$k_i = 0$$$ means that Black is interested in values of the initial array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2000$$$ and the sum of $$$q$$$ over all test cases doesn't exceed $$$100,000$$$. Output For each test case, print $$$q$$$ answers. The $$$i$$$-th of them should be the value of $$$a_{x_i}$$$ after the $$$k_i$$$-th step of transformation. It can be shown that the answer to each query is unique. Example input 2 7 2 1 1 4 3 1 2 4 3 0 1 1 2 2 6 1 2 1 1 2 1 0 2 1000000000 output 1 2 3 3 1 2 Note The first test case was described ih the statement. It can be seen that: $$$k_1 = 0$$$ (initial array): $$$a_3 = 1$$$; $$$k_2 = 1$$$ (after the $$$1$$$-st step): $$$a_1 = 2$$$; $$$k_3 = 2$$$ (after the $$$2$$$-nd step): $$$a_2 = 3$$$; $$$k_4 = 1$$$ (after the $$$1$$$-st step): $$$a_6 = 3$$$. For the second test case, Initial array: $$$1$$$ $$$1$$$ After the $$$1$$$-st step: $$$2$$$ $$$2$$$ After the $$$2$$$-nd step: $$$2$$$ $$$2$$$ ... ... It can be seen that: $$$k_1 = 0$$$ (initial array): $$$a_1 = 1$$$; $$$k_2 = 1000000000$$$: $$$a_2 = 2$$$; Codeforces (c)
1,100
false
false
true
false
false
true
false
false
false
false
2,644
1316F
There are $$$n$$$ officers in the Army of Byteland. Each officer has some power associated with him. The power of the $$$i$$$-th officer is denoted by $$$p_{i}$$$. As the war is fast approaching, the General would like to know the strength of the army. The strength of an army is calculated in a strange way in Byteland. The General selects a random subset of officers from these $$$n$$$ officers and calls this subset a battalion.(All $$$2^n$$$ subsets of the $$$n$$$ officers can be chosen equally likely, including empty subset and the subset of all officers). The strength of a battalion is calculated in the following way: Let the powers of the chosen officers be $$$a_{1},a_{2},ldots,a_{k}$$$, where $$$a_1 le a_2 le dots le a_k$$$. The strength of this battalion is equal to $$$a_1a_2 + a_2a_3 + dots + a_{k-1}a_k$$$. (If the size of Battalion is $$$leq 1$$$, then the strength of this battalion is $$$0$$$). The strength of the army is equal to the expected value of the strength of the battalion. As the war is really long, the powers of officers may change. Precisely, there will be $$$q$$$ changes. Each one of the form $$$i$$$ $$$x$$$ indicating that $$$p_{i}$$$ is changed to $$$x$$$. You need to find the strength of the army initially and after each of these $$$q$$$ updates. Note that the changes are permanent. The strength should be found by modulo $$$10^{9}+7$$$. Formally, let $$$M=10^{9}+7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$p/q$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q otequiv 0 bmod M$$$). Output the integer equal to $$$pcdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 leq x < M$$$ and $$$x ⋅ q equiv p bmod M$$$). Input The first line of the input contains a single integer $$$n$$$ ($$$1 leq n leq 3⋅10^{5}$$$) xa0— the number of officers in Byteland's Army. The second line contains $$$n$$$ integers $$$p_{1},p_{2},ldots,p_{n}$$$ ($$$1 leq p_{i} leq 10^{9}$$$). The third line contains a single integer $$$q$$$ ($$$1 leq q leq 3⋅10^{5}$$$) xa0— the number of updates. Each of the next $$$q$$$ lines contains two integers $$$i$$$ and $$$x$$$ ($$$1 leq i leq n$$$, $$$ 1 leq x leq 10^{9}$$$), indicating that $$$p_{i}$$$ is updated to $$$x$$$ . Output In the first line output the initial strength of the army. In $$$i$$$-th of the next $$$q$$$ lines, output the strength of the army after $$$i$$$-th update. Note In first testcase, initially, there are four possible battalions {} Strength = $$$0$$$ {$$$1$$$} Strength = $$$0$$$ {$$$2$$$} Strength = $$$0$$$ {$$$1,2$$$} Strength = $$$2$$$ So strength of army is $$$frac{0+0+0+2}{4}$$$ = $$$frac{1}{2}$$$ After changing $$$p_{1}$$$ to $$$2$$$, strength of battallion {$$$1,2$$$} changes to $$$4$$$, so strength of army becomes $$$1$$$. After changing $$$p_{2}$$$ to $$$1$$$, strength of battalion {$$$1,2$$$} again becomes $$$2$$$, so strength of army becomes $$$frac{1}{2}$$$.
2,800
false
false
false
false
true
false
false
false
false
false
4,116
1107E
Vasya has a string $$$s$$$ of length $$$n$$$ consisting only of digits 0 and 1. Also he has an array $$$a$$$ of length $$$n$$$. Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets $$$a_x$$$ points for erasing substring of length $$$x$$$. Vasya wants to maximize his total points, so help him with this! Input The first line contains one integer $$$n$$$ ($$$1 le n le 100$$$) — the length of string $$$s$$$. The second line contains string $$$s$$$, consisting only of digits 0 and 1. The third line contains $$$n$$$ integers $$$a_1, a_2, dots a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the number of points for erasing the substring of length $$$i$$$. Output Print one integer — the maximum total points Vasya can get. Examples Input 7 1101001 3 4 9 100 1 2 3 Input 5 10101 3 10 15 15 15 Note In the first example the optimal sequence of erasings is: 1101001 $$$ ightarrow$$$ 111001 $$$ ightarrow$$$ 11101 $$$ ightarrow$$$ 1111 $$$ ightarrow$$$ $$$varnothing$$$. In the second example the optimal sequence of erasings is: 10101 $$$ ightarrow$$$ 1001 $$$ ightarrow$$$ 11 $$$ ightarrow$$$ $$$varnothing$$$.
2,400
false
false
false
true
false
false
false
false
false
false
5,165
408A
Little Vasya went to the supermarket to get some groceries. He walked about the supermarket for a long time and got a basket full of products. Now he needs to choose the cashier to pay for the products. There are _n_ cashiers at the exit from the supermarket. At the moment the queue for the _i_-th cashier already has _k__i_ people. The _j_-th person standing in the queue to the _i_-th cashier has _m__i_,u2009_j_ items in the basket. Vasya knows that: the cashier needs 5 seconds to scan one item; after the cashier scans each item of some customer, he needs 15 seconds to take the customer's money and give him the change. Of course, Vasya wants to select a queue so that he can leave the supermarket as soon as possible. Help him write a program that displays the minimum number of seconds after which Vasya can get to one of the cashiers. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of cashes in the shop. The second line contains _n_ space-separated integers: _k_1,u2009_k_2,u2009...,u2009_k__n_ (1u2009≤u2009_k__i_u2009≤u2009100), where _k__i_ is the number of people in the queue to the _i_-th cashier. The _i_-th of the next _n_ lines contains _k__i_ space-separated integers: _m__i_,u20091,u2009_m__i_,u20092,u2009...,u2009_m__i_,u2009_k__i_ (1u2009≤u2009_m__i_,u2009_j_u2009≤u2009100)xa0— the number of products the _j_-th person in the queue for the _i_-th cash has. Output Print a single integer — the minimum number of seconds Vasya needs to get to the cashier. Examples Input 4 1 4 3 2 100 1 2 2 3 1 9 1 7 8 Note In the second test sample, if Vasya goes to the first queue, he gets to the cashier in 100·5u2009+u200915u2009=u2009515 seconds. But if he chooses the second queue, he will need 1·5u2009+u20092·5u2009+u20092·5u2009+u20093·5u2009+u20094·15u2009=u2009100 seconds. He will need 1·5u2009+u20099·5u2009+u20091·5u2009+u20093·15u2009=u2009100 seconds for the third one and 7·5u2009+u20098·5u2009+u20092·15u2009=u2009105 seconds for the fourth one. Thus, Vasya gets to the cashier quicker if he chooses the second or the third queue.
900
false
false
true
false
false
false
false
false
false
false
8,193
704B
Scott Lang is at war with Darren Cross. There are _n_ chairs in a hall where they are, numbered with 1,u20092,u2009...,u2009_n_ from left to right. The _i_-th chair is located at coordinate _x__i_. Scott is on chair number _s_ and Cross is on chair number _e_. Scott can jump to all other chairs (not only neighboring chairs). He wants to start at his position (chair number _s_), visit each chair exactly once and end up on chair number _e_ with Cross. As we all know, Scott can shrink or grow big (grow big only to his normal size), so at any moment of time he can be either small or large (normal). The thing is, he can only shrink or grow big while being on a chair (not in the air while jumping to another chair). Jumping takes time, but shrinking and growing big takes no time. Jumping from chair number _i_ to chair number _j_ takes _x__i_u2009-u2009_x__j_ seconds. Also, jumping off a chair and landing on a chair takes extra amount of time. If Scott wants to jump to a chair on his left, he can only be small, and if he wants to jump to a chair on his right he should be large. Jumping off the _i_-th chair takes: _c__i_ extra seconds if he's small. _d__i_ extra seconds otherwise (he's large). Also, landing on _i_-th chair takes: _b__i_ extra seconds if he's small. _a__i_ extra seconds otherwise (he's large). In simpler words, jumping from _i_-th chair to _j_-th chair takes exactly: _x__i_u2009-u2009_x__j_u2009+u2009_c__i_u2009+u2009_b__j_ seconds if _j_u2009<u2009_i_. _x__i_u2009-u2009_x__j_u2009+u2009_d__i_u2009+u2009_a__j_ seconds otherwise (_j_u2009>u2009_i_). Given values of _x_, _a_, _b_, _c_, _d_ find the minimum time Scott can get to Cross, assuming he wants to visit each chair exactly once. Input The first line of the input contains three integers _n_,u2009_s_ and _e_ (2u2009≤u2009_n_u2009≤u20095000,u20091u2009≤u2009_s_,u2009_e_u2009≤u2009_n_,u2009_s_u2009≠u2009_e_)xa0— the total number of chairs, starting and ending positions of Scott. The second line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (1u2009≤u2009_x_1u2009<u2009_x_2u2009<u2009...u2009<u2009_x__n_u2009≤u2009109). The third line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a_1,u2009_a_2,u2009...,u2009_a__n_u2009≤u2009109). The fourth line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b_1,u2009_b_2,u2009...,u2009_b__n_u2009≤u2009109). The fifth line contains _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c_1,u2009_c_2,u2009...,u2009_c__n_u2009≤u2009109). The sixth line contains _n_ integers _d_1,u2009_d_2,u2009...,u2009_d__n_ (1u2009≤u2009_d_1,u2009_d_2,u2009...,u2009_d__n_u2009≤u2009109). Output Print the minimum amount of time Scott needs to get to the Cross while visiting each chair exactly once. Example Input 7 4 3 8 11 12 16 17 18 20 17 16 20 2 20 5 13 17 8 8 16 12 15 13 12 4 16 4 15 7 6 8 14 2 11 17 12 8 Note In the sample testcase, an optimal solution would be . Spent time would be 17u2009+u200924u2009+u200923u2009+u200920u2009+u200933u2009+u200922u2009=u2009139.
2,500
false
true
false
true
false
false
false
false
false
true
7,005
742A
Problem - 742A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation math number theory *1000 No tag edit access → Contest materials . Output Print single integerxa0— the last digit of 1378_n_. Examples Input 1 Output 8 Input 2 Output 4 Note In the first example, last digit of 13781u2009=u20091378 is 8. In the second example, last digit of 13782u2009=u20091378·1378u2009=u20091898884 is 4.
1,000
true
false
true
false
false
false
false
false
false
false
6,831
1843F2
This is the hard version of the problem. The only difference between the simple and hard versions is that in this version $$$u$$$ can take any possible value. As is known, Omsk is the capital of Berland. Like any capital, Omsk has a well-developed metro system. The Omsk metro consists of a certain number of stations connected by tunnels, and between any two stations there is exactly one path that passes through each of the tunnels no more than once. In other words, the metro is a tree. To develop the metro and attract residents, the following system is used in Omsk. Each station has its own weight $$$x in {-1, 1}$$$. If the station has a weight of $$$-1$$$, then when the station is visited by an Omsk resident, a fee of $$$1$$$ burle is charged. If the weight of the station is $$$1$$$, then the Omsk resident is rewarded with $$$1$$$ burle. Omsk Metro currently has only one station with number $$$1$$$ and weight $$$x = 1$$$. Every day, one of the following events occurs: A new station with weight $$$x$$$ is added to the station with number $$$v_i$$$, and it is assigned a number that is one greater than the number of existing stations. Alex, who lives in Omsk, wonders: is there a subsegment$$$dagger$$$ (possibly empty) of the path between vertices $$$u$$$ and $$$v$$$ such that, by traveling along it, exactly $$$k$$$ burles can be earned (if $$$k < 0$$$, this means that $$$k$$$ burles will have to be spent on travel). In other words, Alex is interested in whether there is such a subsegment of the path that the sum of the weights of the vertices in it is equal to $$$k$$$. Note that the subsegment can be empty, and then the sum is equal to $$$0$$$. You are a friend of Alex, so your task is to answer Alex's questions. $$$dagger$$$Subsegment — continuous sequence of elements. Input The first line contains a single number $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains the number $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of events. Then there are $$$n$$$ lines describing the events. In the $$$i$$$-th line, one of the following options is possible: First comes the symbol "+" (without quotes), then two numbers $$$v_i$$$ and $$$x_i$$$ ($$$x_i in {-1, 1}$$$, it is also guaranteed that the vertex with number $$$v_i$$$ exists). In this case, a new station with weight $$$x_i$$$ is added to the station with number $$$v_i$$$. First comes the symbol "?" (without quotes), and then three numbers $$$u_i$$$, $$$v_i$$$, and $$$k_i$$$ ($$$-n le k_i le n$$$). It is guaranteed that the vertices with numbers $$$u_i$$$ and $$$v_i$$$ exist. In this case, it is necessary to determine whether there is a subsegment (possibly empty) of the path between stations $$$u_i$$$ and $$$v_i$$$ with a sum of weights exactly equal to $$$k_i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each of Alex's questions, output "Yes" (without quotes) if the subsegment described in the condition exists, otherwise output "No" (without quotes). You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Examples Input 1 8 + 1 -1 ? 1 1 2 ? 1 2 1 + 1 1 ? 1 3 -1 ? 1 1 1 ? 1 3 2 ? 1 1 0 Output NO YES NO YES YES YES Input 1 7 + 1 -1 + 2 -1 + 2 1 + 3 -1 ? 5 2 2 ? 3 1 -1 ? 5 4 -3 Note Explanation of the first sample. The answer to the second question is "Yes", because there is a path $$$1$$$. In the fourth question, we can choose the $$$1$$$ path again. In the fifth query, the answer is "Yes", since there is a path $$$1-3$$$. In the sixth query, we can choose an empty path because the sum of the weights on it is $$$0$$$. It is not difficult to show that there are no paths satisfying the first and third queries.
2,300
true
false
false
true
true
false
false
false
false
false
1,224
1945D
The guys lined up in a queue of $$$n$$$ people, starting with person number $$$i = 1$$$, to ask Serafim the Owl about the meaning of life. Unfortunately, Kirill was very busy writing the legend for this problem, so he arrived a little later and stood at the end of the line after the $$$n$$$-th person. Kirill is completely dissatisfied with this situation, so he decided to bribe some people ahead of him. For the $$$i$$$-th person in the queue, Kirill knows two values: $$$a_i$$$ and $$$b_i$$$. If at the moment Kirill is standing at position $$$i$$$, then he can choose any position $$$j$$$ such that $$$j < i$$$ and exchange places with the person at position $$$j$$$. In this case, Kirill will have to pay him $$$a_j$$$ coins. And for each $$$k$$$ such that $$$j < k < i$$$, Kirill will have to pay $$$b_k$$$ coins to the person at position $$$k$$$. Kirill can perform this action any number of times. Kirill is thrifty, so he wants to spend as few coins as possible, but he doesn't want to wait too long, so Kirill believes he should be among the first $$$m$$$ people in line. Help Kirill determine the minimum number of coins he will have to spend in order to not wait too long. Input Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then follows the description of the test case. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le m le n le 200,000$$$)xa0— the number of people in the queue besides Kirill and the maximum allowable final position of Kirill, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ separated by spaces ($$$1 le a_i le 10^9$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ separated by spaces ($$$1 le b_i le 10^9$$$). It is guaranteed that the sum of the values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer — the minimum number of coins Kirill needs to spend. Example Input 4 4 2 7 3 6 9 4 3 8 5 6 2 6 9 7 1 8 3 5 8 8 1 4 1 7 7 7 2 9 2 6 5 9 9 1 10 7 1 4 9 2 1 2 3 1 1
1,300
false
true
false
true
false
false
false
false
false
false
610
1336B
Xenia is a girl being born a noble. Due to the inflexibility and harshness of her family, Xenia has to find some ways to amuse herself. xa0Recently Xenia has bought $$$n_r$$$ red gems, $$$n_g$$$ green gems and $$$n_b$$$ blue gems. Each of the gems has a weight. Now, she is going to pick three gems. Xenia loves colorful things, so she will pick exactly one gem of each color. Xenia loves balance, so she will try to pick gems with little difference in weight. Specifically, supposing the weights of the picked gems are $$$x$$$, $$$y$$$ and $$$z$$$, Xenia wants to find the minimum value of $$$(x-y)^2+(y-z)^2+(z-x)^2$$$. As her dear friend, can you help her? Input The first line contains a single integer $$$t$$$ ($$$1le t le 100$$$) xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$n_r,n_g,n_b$$$ ($$$1le n_r,n_g,n_ble 10^5$$$) xa0— the number of red gems, green gems and blue gems respectively. The second line of each test case contains $$$n_r$$$ integers $$$r_1,r_2,ldots,r_{n_r}$$$ ($$$1le r_i le 10^9$$$) xa0— $$$r_i$$$ is the weight of the $$$i$$$-th red gem. The third line of each test case contains $$$n_g$$$ integers $$$g_1,g_2,ldots,g_{n_g}$$$ ($$$1le g_i le 10^9$$$) xa0— $$$g_i$$$ is the weight of the $$$i$$$-th green gem. The fourth line of each test case contains $$$n_b$$$ integers $$$b_1,b_2,ldots,b_{n_b}$$$ ($$$1le b_i le 10^9$$$) xa0— $$$b_i$$$ is the weight of the $$$i$$$-th blue gem. It is guaranteed that $$$sum n_r le 10^5$$$, $$$sum n_g le 10^5$$$, $$$sum n_b le 10^5$$$ (the sum for all test cases). Output For each test case, print a line contains one integer xa0— the minimum value which Xenia wants to find. Example Input 5 2 2 3 7 8 6 3 3 1 4 1 1 1 1 1 1000000000 2 2 2 1 2 5 4 6 7 2 2 2 1 2 3 4 6 7 3 4 1 3 2 1 7 3 3 4 6 Output 14 1999999996000000002 24 24 14 Note In the first test case, Xenia has the following gems: If she picks the red gem with weight $$$7$$$, the green gem with weight $$$6$$$, and the blue gem with weight $$$4$$$, she will achieve the most balanced selection with $$$(x-y)^2+(y-z)^2+(z-x)^2=(7-6)^2+(6-4)^2+(4-7)^2=14$$$.
1,700
true
true
false
false
false
false
false
true
true
false
4,022
1249F
You are given a tree, which consists of $$$n$$$ vertices. Recall that a tree is a connected undirected graph without cycles. Example of a tree. Vertices are numbered from $$$1$$$ to $$$n$$$. All vertices have weights, the weight of the vertex $$$v$$$ is $$$a_v$$$. Recall that the distance between two vertices in the tree is the number of edges on a simple path between them. Your task is to find the subset of vertices with the maximum total weight (the weight of the subset is the sum of weights of all vertices in it) such that there is no pair of vertices with the distance $$$k$$$ or less between them in this subset. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 200$$$) — the number of vertices in the tree and the distance restriction, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^5$$$), where $$$a_i$$$ is the weight of the vertex $$$i$$$. The next $$$n - 1$$$ lines contain edges of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$ — the labels of vertices it connects ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$). It is guaranteed that the given edges form a tree. Output Print one integer — the maximum total weight of the subset in which all pairs of vertices have distance more than $$$k$$$. Examples Input 5 1 1 2 3 4 5 1 2 2 3 3 4 3 5 Input 7 2 2 1 2 1 2 1 1 6 4 1 5 3 1 2 3 7 5 7 4
2,200
false
false
false
true
false
false
false
false
false
false
4,456
1898E
Sofia has a string $$$s$$$ of length $$$n$$$, consisting only of lowercase English letters. She can perform operations of the following types with this string. 1. Select an index $$$1 le i le s$$$ and remove the character $$$s_i$$$ from the string. 2. Select a pair of indices $$$(l, r)$$$ ($$$1 le l le r le s$$$) and sort the substring $$$s_{l} s_{l+1} ldots s_r$$$ in alphabetical order. Here, $$$s$$$ denotes the current length of $$$s$$$. In particular, $$$s = n$$$ before the first operation. For example, if $$$s = mathtt{sofia}$$$, then performing the operation of the first type with $$$i=4$$$ results in $$$s$$$ becoming $$$mathtt{sofa}$$$, and performing the operation of the second type with $$$(l, r) = (2, 4)$$$ after that results in $$$s$$$ becoming $$$mathtt{safo}$$$. Sofia wants to obtain the string $$$t$$$ of length $$$m$$$ after performing zero or more operations on string $$$s$$$ as described above. Please determine whether it is possible or not. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10,000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1leq m leq n leq 2cdot 10^5$$$)xa0— the lengths of string $$$s$$$ and $$$t$$$, respectively. The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting only of lowercase English letters. The third line of each test case contains the string $$$t$$$ of length $$$m$$$, consisting only of lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output "YES" if Sofia can obtain the string $$$t$$$ from $$$s$$$ using the operations above. Otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 8 5 5 sofia afios 3 2 cba bc 5 1 sofia e 15 7 anavolimilovana aamanan 26 4 abcdefghijklmnopqrstuvwxyz nope 26 4 zyxwvutsrqponmlkjihgfedcba nope 7 3 apricot cat 3 3 cba acb Output YES YES NO YES NO YES NO YES Note In the first test case, Sofia can perform the following operation: 1. operation of the second type with $$$l=1$$$ and $$$r=5$$$: string $$$s$$$ becomes $$$mathtt{afios}$$$ after it. In the second test case, Sofia can perform the following operations: 1. operation of the second type with $$$l=1$$$ and $$$r=2$$$: string $$$s$$$ becomes $$$mathtt{bca}$$$ after it; 2. operation of the first type with $$$i=3$$$: string $$$s$$$ becomes $$$mathtt{bc}$$$ after it. In the third test case, it can be shown that it is impossible to obtain $$$t$$$ from $$$s$$$ using the provided operations.
2,200
false
true
false
false
true
false
false
false
true
false
924
832A
It's one more school day now. Sasha doesn't like classes and is always bored at them. So, each day he invents some game and plays in it alone or with friends. Today he invented one simple game to play with Lena, with whom he shares a desk. The rules are simple. Sasha draws _n_ sticks in a row. After that the players take turns crossing out exactly _k_ sticks from left or right in each turn. Sasha moves first, because he is the inventor of the game. If there are less than _k_ sticks on the paper before some turn, the game ends. Sasha wins if he makes strictly more moves than Lena. Sasha wants to know the result of the game before playing, you are to help him. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20091018, _k_u2009≤u2009_n_)xa0— the number of sticks drawn by Sasha and the number _k_xa0— the number of sticks to be crossed out on each turn. Output If Sasha wins, print "YES" (without quotes), otherwise print "NO" (without quotes). You can print each letter in arbitrary case (upper of lower). Note In the first example Sasha crosses out 1 stick, and then there are no sticks. So Lena can't make a move, and Sasha wins. In the second example Sasha crosses out 4 sticks, then Lena crosses out 4 sticks, and after that there are only 2 sticks left. Sasha can't make a move. The players make equal number of moves, so Sasha doesn't win.
800
true
false
false
false
false
false
false
false
false
false
6,439
652F
_n_ ants are on a circle of length _m_. An ant travels one unit of distance per one unit of time. Initially, the ant number _i_ is located at the position _s__i_ and is facing in the direction _d__i_ (which is either L or R). Positions are numbered in counterclockwise order starting from some point. Positions of the all ants are distinct. All the ants move simultaneously, and whenever two ants touch, they will both switch their directions. Note that it is possible for an ant to move in some direction for a half of a unit of time and in opposite direction for another half of a unit of time. Print the positions of the ants after _t_ time units. Input The first line contains three integers _n_, _m_ and _t_ (2u2009≤u2009_n_u2009≤u20093·105,u20092u2009≤u2009_m_u2009≤u2009109,u20090u2009≤u2009_t_u2009≤u20091018) — the number of ants, the length of the circle and the number of time units. Each of the next _n_ lines contains integer _s__i_ and symbol _d__i_ (1u2009≤u2009_s__i_u2009≤u2009_m_ and _d__i_ is either L or R) — the position and the direction of the _i_-th ant at the start. The directions L and R corresponds to the clockwise and counterclockwise directions, respectively. It is guaranteed that all positions _s__i_ are distinct. Output Print _n_ integers _x__j_ — the position of the _j_-th ant after _t_ units of time. The ants are numbered from 1 to _n_ in order of their appearing in input.
2,800
true
false
false
false
false
true
false
false
false
false
7,205
809D
Leha and Noora decided to go on a trip in the Baltic States. As you know from the previous problem, Leha has lost his car on the parking of the restaurant. Unfortunately, requests to the watchman didn't helped hacker find the car, so friends decided to go hitchhiking. In total, they intended to visit _n_ towns. However it turned out that sights in _i_-th town are open for visitors only on days from _l__i_ to _r__i_. What to do? Leha proposed to choose for each town _i_ a day, when they will visit this town, i.e any integer _x__i_ in interval [_l__i_,u2009_r__i_]. After that Noora choses some subsequence of towns _id_1,u2009_id_2,u2009...,u2009_id__k_, which friends are going to visit, that at first they are strictly increasing, i.e _id__i_u2009<u2009_id__i_u2009+u20091 is for all integers _i_ from 1 to _k_u2009-u20091, but also the dates of the friends visits are strictly increasing, i.e _x__id__i_u2009<u2009_x__id__i_u2009+u20091 is true for all integers _i_ from 1 to _k_u2009-u20091. Please help Leha and Noora in choosing such _x__i_ for each town _i_, and such subsequence of towns _id_1,u2009_id_2,u2009...,u2009_id__k_, so that friends can visit maximal number of towns. You may assume, that Leha and Noora can start the trip any day. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20093·105) denoting the number of towns Leha and Noora intended to visit. Each line _i_ of the _n_ subsequent lines contains two integers _l__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009109), denoting that sights in _i_-th town are open for visitors on any day . Note Consider the first example. Let's take this plan: let's visit the sight in the second town on the first day, in the third town on the third day and in the fifth town on the fourth. That's would be the optimal answer.
2,900
false
false
false
true
true
false
false
false
false
false
6,525
1175F
Problem - 1175F - 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 data structures divide and conquer hashing math *2500 No tag edit access → Contest materials . The second line contains $$$n$$$ integers $$$a_1, a_2, dots , a_n$$$ ($$$1 le a_i le n$$$). This array can contain the same integers. Output Print the number of subpermutations of the array $$$a$$$. Examples Input 8 2 4 1 3 4 2 1 2 Output 7 Input 5 1 1 2 1 2 Output 6 Note There are $$$7$$$ subpermutations in the first test case. Their segments of indices are $$$
2,500
true
false
false
false
true
false
true
false
false
false
4,840
224A
Problem - 224A - 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 geometry math *1100 No tag edit access → Contest materials and do not exceed 104. It is guaranteed that there exists at least one parallelepiped that satisfies the problem statement. Output Print a single number — the sum of all edges of the parallelepiped. Examples Input 1 1 1 Output 12 Input 4 6 6 Output 28 Note In the first sample the parallelepiped has sizes 1u2009×u20091u2009×u20091, in the second onexa0— 2u2009×u20092u2009×u20093.
1,100
true
false
false
false
false
false
true
false
false
false
8,942
641C
Little Artem is fond of dancing. Most of all dances Artem likes ruedaxa0— Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together. More detailed, there are _n_ pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves: 1. Value _x_ and some direction are announced, and all boys move _x_ positions in the corresponding direction. 2. Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that _n_ is even. Your task is to determine the final position of each boy. Input The first line of the input contains two integers _n_ and _q_ (2u2009≤u2009_n_u2009≤u20091u2009000u2009000, 1u2009≤u2009_q_u2009≤u20092u2009000u2009000)xa0— the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that _n_ is even. Next _q_ lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as _x_ (u2009-u2009_n_u2009≤u2009_x_u2009≤u2009_n_), where 0u2009≤u2009_x_u2009≤u2009_n_ means all boys moves _x_ girls in clockwise direction, while u2009-u2009_x_ means all boys move _x_ positions in counter-clockwise direction. There is no other input for commands of the second type. Output Output _n_ integers, the _i_-th of them should be equal to the index of boy the _i_-th girl is dancing with after performing all _q_ moves.
1,800
false
false
true
false
false
true
true
false
false
false
7,249
1621E
You have decided to open a new school. You have already found $$$n$$$ teachers and $$$m$$$ groups of students. The $$$i$$$-th group of students consists of $$$k_i geq 2$$$ students. You know age of each teacher and each student. The ages of teachers are $$$a_1, a_2, ldots, a_n$$$ and the ages of students of the $$$i$$$-th group are $$$b_{i, 1}, b_{i, 2}, ldots, b_{i, k_i}$$$. To start lessons you should assign the teacher to each group of students. Such assignment should satisfy the following requirements: To each group exactly one teacher assigned. To each teacher at most $$$1$$$ group of students assigned. The average of students' ages in each group doesn't exceed the age of the teacher assigned to this group. The average of set $$$x_1, x_2, ldots, x_k$$$ of $$$k$$$ integers is $$$frac{x_1 + x_2 + ldots + x_k}{k}$$$. Recently you have heard that one of the students will refuse to study in your school. After this, the size of one group will decrease by $$$1$$$ while all other groups will remain unchanged. You don't know who will refuse to study. For each student determine if you can start lessons in case of his refusal. Note, that it is not guaranteed that it is possible to start lessons before any refusal. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq m leq n leq 10^5$$$)xa0— the number of teachers and the number of groups of students. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^5$$$)xa0— the ages of teachers. The next $$$2m$$$ lines contains descriptions of groups. The first line of description of group contains a single integer $$$k_i$$$ ($$$2 leq k_i leq 10^5$$$)xa0— the number of students in this group. The second line of description of group contains $$$k_i$$$ integers $$$b_{i, 1}, b_{i, 2}, ldots, b_{i, k_i}$$$ ($$$1 leq b_{i, j} leq 10^5$$$)xa0— the ages of students of this group. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$ and that the total sum of $$$k_1 + k_2 + ldots + k_m$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$ Output For each test case output string of symbols $$$0$$$ and $$$1$$$ of length $$$k_1 + k_2 + ldots + k_m$$$. The $$$i$$$-th symbol of this string should be equals $$$1$$$ if it is possible to start lessons in case of the $$$i$$$-th student refuse and it should be equals $$$0$$$ otherwise. Students are numbered by integers from $$$1$$$ to $$$k_1 + k_2 + ldots + k_m$$$ in the order they appear in the input. Thus, students of the $$$1$$$-st group are numbered by integers $$$1$$$, $$$2$$$, $$$ldots$$$, $$$k_1$$$, students of the $$$2$$$-nd group are numbered by integers $$$k_1 + 1$$$, $$$k_1 + 2$$$, $$$ldots$$$, $$$k_1 + k_2$$$ and so on. Example Input 2 1 1 30 3 25 16 37 4 2 9 12 12 6 2 4 5 3 111 11 11 Note In the first test case there is one group of students with average age $$$frac{25+16+37}{3}=26$$$ and one teacher with age $$$30$$$. There exists only one assignment that allows to start lessons. If the student with age $$$16$$$ will refuse to study, the average age of students in this group will become $$$frac{25+37}{2}=31$$$ so there won't be any assignment that allows to start lessons. In the second test case it is impossible to start lessons initially. However, if the $$$3$$$-rd student with age $$$111$$$ will refuse to study, the average ages of groups will become $$$frac{4 + 5}{2}=4.5$$$ and $$$frac{11+11}{2} = 11$$$ correspondingly. Then it is possible to assing the first group to the first teacher and the second group to the third teacher.
2,300
false
true
true
true
true
false
false
true
true
false
2,516
1107B
Problem - 1107B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math number theory *1000 No tag edit access → Contest materials ") $$$. Then $$$S(5)=5$$$, $$$S(38)=S(3+8=11)=S(1+1=2)=2$$$, $$$S(10)=S(1+0=1)=1$$$. As a homework Petya got $$$n$$$ tasks of the form: find $$$k$$$-th positive number whose digital root is $$$x$$$. Petya has already solved all the problems, but he doesn't know if it's right. Your task is to solve all $$$n$$$ tasks from Petya's homework. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^3$$$) — the number of tasks in Petya's homework. The next $$$n$$$ lines contain two integers $$$k_i$$$ ($$$1 le k_i le 10^{12}$$$) and $$$x_i$$$ ($$$1 le x_i le 9$$$) — $$$i$$$-th Petya's task in which you need to find a $$$k_i$$$-th positive number, the digital root of which is $$$x_i$$$. Output Output $$$n$$$ lines, $$$i$$$-th line should contain a single integer — the answer to the $$$i$$$-th problem. Example Input 3 1 5 5 2 3 1 Output 5 38 19
1,000
true
false
false
false
false
false
false
false
false
false
5,168
269C
Emuskald considers himself a master of flow algorithms. Now he has completed his most ingenious program yet — it calculates the maximum flow in an undirected graph. The graph consists of _n_ vertices and _m_ edges. Vertices are numbered from 1 to _n_. Vertices 1 and _n_ being the source and the sink respectively. However, his max-flow algorithm seems to have a little flaw — it only finds the flow volume for each edge, but not its direction. Help him find for each edge the direction of the flow through this edges. Note, that the resulting flow should be correct maximum flow. More formally. You are given an undirected graph. For each it's undirected edge (_a__i_, _b__i_) you are given the flow volume _c__i_. You should direct all edges in such way that the following conditions hold: 1. for each vertex _v_ (1u2009<u2009_v_u2009<u2009_n_), sum of _c__i_ of incoming edges is equal to the sum of _c__i_ of outcoming edges; 2. vertex with number 1 has no incoming edges; 3. the obtained directed graph does not have cycles. Input The first line of input contains two space-separated integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20092·105, _n_u2009-u20091u2009≤u2009_m_u2009≤u20092·105), the number of vertices and edges in the graph. The following _m_ lines contain three space-separated integers _a__i_, _b__i_ and _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_, 1u2009≤u2009_c__i_u2009≤u2009104), which means that there is an undirected edge from _a__i_ to _b__i_ with flow volume _c__i_. It is guaranteed that there are no two edges connecting the same vertices; the given graph is connected; a solution always exists. Output Output _m_ lines, each containing one integer _d__i_, which should be 0 if the direction of the _i_-th edge is _a__i_u2009→u2009_b__i_ (the flow goes from vertex _a__i_ to vertex _b__i_) and should be 1 otherwise. The edges are numbered from 1 to _m_ in the order they are given in the input. If there are several solutions you can print any of them. Examples Input 3 3 3 2 10 1 2 10 3 1 5 Input 4 5 1 2 10 1 3 10 2 3 5 4 2 15 3 4 5 Note In the first test case, 10 flow units pass through path , and 5 flow units pass directly from source to sink: .
2,100
false
true
false
false
false
true
false
false
false
true
8,756
1505E
Problem - 1505E - Codeforces =============== xa0 , she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares. The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions... Input The first line of input contains two integers $$$H$$$ and $$$W$$$ ($$$1 le H, W le 5$$$), separated by a space, — the height and the width of the cake. The next $$$H$$$ lines contain a string of $$$W$$$ characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry. Output Output the number of berries the mouse will eat following her strategy. Examples Input 4 3 *.. .*. ..* ... Output 3 Input 4 4 .*.. *... ...* ..*. Output 2 Input 3 4 ..** *... .... Output 1 Input 5 5 ..*.. ..... **... **... **... Output 1
1,800
false
true
true
false
false
false
false
false
false
false
3,148
903E
We had a string _s_ consisting of _n_ lowercase Latin letters. We made _k_ copies of this string, thus obtaining _k_ identical strings _s_1,u2009_s_2,u2009...,u2009_s__k_. After that, in each of these strings we swapped exactly two characters (the characters we swapped could be identical, but they had different indices in the string). You are given _k_ strings _s_1,u2009_s_2,u2009...,u2009_s__k_, and you have to restore any string _s_ so that it is possible to obtain these strings by performing aforementioned operations. Note that the total length of the strings you are given doesn't exceed 5000 (that is, _k_·_n_u2009≤u20095000). Input The first line contains two integers _k_ and _n_ (1u2009≤u2009_k_u2009≤u20092500,u20092u2009≤u2009_n_u2009≤u20095000,u2009_k_xa0·xa0_n_u2009≤u20095000) — the number of strings we obtained, and the length of each of these strings. Next _k_ lines contain the strings _s_1,u2009_s_2,u2009...,u2009_s__k_, each consisting of exactly _n_ lowercase Latin letters. Output Print any suitable string _s_, or -1 if such string doesn't exist. Examples Input 5 4 abcd dcba acbd dbca zzzz Note In the first example _s_1 is obtained by swapping the second and the fourth character in acab, _s_2 is obtained by swapping the first and the second character, and to get _s_3, we swap the third and the fourth character. In the second example _s_1 is obtained by swapping the third and the fourth character in kbub, _s_2 — by swapping the second and the fourth, and _s_3 — by swapping the first and the third. In the third example it's impossible to obtain given strings by aforementioned operations.
2,200
false
false
true
false
false
false
true
false
false
false
6,130
559C
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an _h_u2009×u2009_w_ field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win? The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process. Input The first line of the input contains three integers: _h_,u2009_w_,u2009_n_ — the sides of the board and the number of black cells (1u2009≤u2009_h_,u2009_w_u2009≤u2009105,u20091u2009≤u2009_n_u2009≤u20092000). Next _n_ lines contain the description of black cells. The _i_-th of these lines contains numbers _r__i_,u2009_c__i_ (1u2009≤u2009_r__i_u2009≤u2009_h_,u20091u2009≤u2009_c__i_u2009≤u2009_w_) — the number of the row and column of the _i_-th cell. It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct. Output Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 109u2009+u20097. Examples Input 100 100 3 15 16 16 15 99 88
2,200
true
false
false
true
false
false
false
false
false
false
7,596
76B
Modern researches has shown that a flock of hungry mice searching for a piece of cheese acts as follows: if there are several pieces of cheese then each mouse chooses the closest one. After that all mice start moving towards the chosen piece of cheese. When a mouse or several mice achieve the destination point and there is still a piece of cheese in it, they eat it and become well-fed. Each mice that reaches this point after that remains hungry. Moving speeds of all mice are equal. If there are several ways to choose closest pieces then mice will choose it in a way that would minimize the number of hungry mice. To check this theory scientists decided to conduct an experiment. They located _N_ mice and _M_ pieces of cheese on a cartesian plane where all mice are located on the line _y_u2009=u2009_Y_0 and all pieces of cheese — on another line _y_u2009=u2009_Y_1. To check the results of the experiment the scientists need a program which simulates the behavior of a flock of hungry mice. Write a program that computes the minimal number of mice which will remain hungry, i.e. without cheese. Input The first line of the input contains four integer numbers _N_ (1u2009≤u2009_N_u2009≤u2009105), _M_ (0u2009≤u2009_M_u2009≤u2009105), _Y_0 (0u2009≤u2009_Y_0u2009≤u2009107), _Y_1 (0u2009≤u2009_Y_1u2009≤u2009107, _Y_0u2009≠u2009_Y_1). The second line contains a strictly increasing sequence of _N_ numbers — _x_ coordinates of mice. Third line contains a strictly increasing sequence of _M_ numbers — _x_ coordinates of cheese. All coordinates are integers and do not exceed 107 by absolute value. Output The only line of output should contain one number — the minimal number of mice which will remain without cheese. Note All the three mice will choose the first piece of cheese. Second and third mice will eat this piece. The first one will remain hungry, because it was running towards the same piece, but it was late. The second piece of cheese will remain uneaten.
2,100
false
true
false
false
false
false
false
false
false
false
9,585
1725A
Pak Chanek has a grid that has $$$N$$$ rows and $$$M$$$ columns. Each row is numbered from $$$1$$$ to $$$N$$$ from top to bottom. Each column is numbered from $$$1$$$ to $$$M$$$ from left to right. Each tile in the grid contains a number. The numbers are arranged as follows: Row $$$1$$$ contains integers from $$$1$$$ to $$$M$$$ from left to right. Row $$$2$$$ contains integers from $$$M+1$$$ to $$$2 imes M$$$ from left to right. Row $$$3$$$ contains integers from $$$2 imes M+1$$$ to $$$3 imes M$$$ from left to right. And so on until row $$$N$$$. A domino is defined as two different tiles in the grid that touch by their sides. A domino is said to be tight if and only if the two numbers in the domino have a difference of exactly $$$1$$$. Count the number of distinct tight dominoes in the grid. Two dominoes are said to be distinct if and only if there exists at least one tile that is in one domino, but not in the other. Input The only line contains two integers $$$N$$$ and $$$M$$$ ($$$1 leq N, M leq 10^9$$$) — the number of rows and columns in the grid. Output An integer representing the number of distinct tight dominoes in the grid. Note The picture below is the grid that Pak Chanek has in the first example. The picture below is an example of a tight domino in the grid.
800
true
false
false
false
false
false
false
false
false
false
1,946
813D
Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time! Alice has a sheet with _n_ notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal. Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Subsequence forms a melody when each two adjacent notes either differs by 1 or are congruent modulo 7. You should write a program which will calculate maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody. Input The first line contains one integer number _n_ (2u2009≤u2009_n_u2009≤u20095000). The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105) — notes written on a sheet. Output Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody. Note In the first example subsequences [1,u20092] and [4,u20095] give length 4 in total. In the second example subsequences [62,u200948,u200949] and [60,u200961] give length 5 in total. If you choose subsequence [62,u200961] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal.
2,600
false
false
false
true
false
false
false
false
false
false
6,508
1951B
There are $$$n$$$ cows participating in a coding tournament. Cow $$$i$$$ has a Cowdeforces rating of $$$a_i$$$ (all distinct), and is initially in position $$$i$$$. The tournament consists of $$$n-1$$$ matches as follows: The first match is between the cow in position $$$1$$$ and the cow in position $$$2$$$. Subsequently, each match $$$i$$$ is between the cow in position $$$i+1$$$ and the winner of match $$$i-1$$$. In each match, the cow with the higher Cowdeforces rating wins and proceeds to the next match. You are the owner of cow $$$k$$$. For you, winning the tournament is not important; rather, you want your cow to win in as many matches as possible. As an acquaintance of the tournament organizers, you can ask them to swap the position of your cow with another cow only once, or you can choose to do nothing. Find the maximum number of wins your cow can achieve. Input Each test contains multiple test cases. The first line contains an 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 two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^5, 1 le k le n$$$)xa0— the number of cows and your cow's index. 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 Cowdeforces rating of the cows. It is guaranteed that $$$a_i$$$'s are pairwise different. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print one integer: the maximum number of wins cow $$$k$$$ can achieve if you choose to swap (or do nothing) optimally. Example Input 3 6 1 12 10 14 11 8 3 6 5 7 2 727 10 12 13 2 2 1000000000 1 Note In the first test case, it is optimal to do nothing. Let $$$a'$$$ be the Cowdeforces rating of the cows in the original order (with your cow's rating bolded), then Initially, $$$a' = [mathbf{12}, 10, 14, 11, 8, 3]$$$. Your cow plays against the cow with Cowdeforces rating $$$10$$$ and wins. $$$a' = [mathbf{12}, 14, 11, 8, 3]$$$. Your cow plays against the cow with Cowdeforces rating $$$14$$$ and loses. In total, your cow wins $$$1$$$ match. In the second test case, it is optimal to swap your cow to position $$$3$$$. Then, let $$$a'$$$ be the Cowdeforces rating of the cows in the order after the swap. Initially, $$$a' = [7, 2, mathbf{12}, 10, 727, 13]$$$. The cow with Cowdeforces rating $$$7$$$ plays against the cow with Cowdeforces rating $$$2$$$ and wins. $$$a' = [7, mathbf{12}, 10, 727, 13]$$$. The cow with Cowdeforces rating $$$7$$$ plays against your cow, and your cow wins. $$$a' = [mathbf{12}, 10, 727, 13]$$$. Your cow plays against the cow with Cowdeforces rating $$$10$$$ and wins. $$$a' = [mathbf{12}, 727, 13]$$$. Your cow plays against the cow with Cowdeforces rating $$$727$$$ and loses. In total, your cow wins $$$2$$$ matches.
1,200
false
true
false
false
true
false
false
true
false
false
573
1535B
Problem - 1535B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force greedy math number theory sortings *900 No tag edit access → Contest materials > 1$$$ (where $$$gcd(x, y)$$$ is the greatest common divisor of $$$x$$$ and $$$y$$$). Find the maximum number of good index pairs if you can reorder the array $$$a$$$ in an arbitrary way. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$2 le n le 2000$$$)xa0— the number of elements in the array. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output a single integerxa0— the maximum number of good index pairs if you can reorder the array $$$a$$$ in an arbitrary way. Example Input 3 4 3 6 5 3 2 1 7 5 1 4 2 4 1 Output 4 0 9 Note In the first example, the array elements can be rearranged as follows: $$$
900
true
true
false
false
false
false
true
false
true
false
2,973
856F
Vasya and Petya are playing an online game. As most online games, it has hero progress system that allows players to gain experience that make their heroes stronger. Of course, Vasya would like to get as many experience points as possible. After careful study of experience points allocation, he found out that if he plays the game alone, he gets one experience point each second. However, if two players are playing together, and their current experience values differ by at most _C_ points, they can boost their progress, and each of them gets 2 experience points each second. Since Vasya and Petya are middle school students, their parents don't allow them to play all the day around. Each of the friends has his own schedule: Vasya can only play during intervals [_a_1;_b_1],u2009[_a_2;_b_2],u2009...,u2009[_a__n_;_b__n_], and Petya can only play during intervals [_c_1;_d_1],u2009[_c_2;_d_2],u2009...,u2009[_c__m_;_d__m_]. All time periods are given in seconds from the current moment. Vasya is good in math, so he has noticed that sometimes it can be profitable not to play alone, because experience difference could become too big, and progress would not be boosted even when played together. Now they would like to create such schedule of playing that Vasya's final experience was greatest possible. The current players experience is the same. Petya is not so concerned about his experience, so he is ready to cooperate and play when needed to maximize Vasya's experience. Input The first line of input data contains integers _n_, _m_ and _C_ — the number of intervals when Vasya can play, the number of intervals when Petya can play, and the maximal difference in experience level when playing together still gives a progress boost (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105, 0u2009≤u2009_C_u2009≤u20091018). The following _n_ lines contain two integers each: _a__i_,u2009_b__i_xa0— intervals when Vasya can play (0u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u20091018, _b__i_u2009<u2009_a__i_u2009+u20091). The following _m_ lines contain two integers each: _c__i_,u2009_d__i_xa0— intervals when Petya can play (0u2009≤u2009_c__i_u2009<u2009_d__i_u2009≤u20091018, _d__i_u2009<u2009_c__i_u2009+u20091).
3,000
false
true
false
false
false
false
false
false
false
false
6,315
911D
Problem - 911D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math *1800 No tag edit access → Contest materials such that _i_u2009>u2009_j_ and _a__i_u2009<u2009_a__j_. For example, a permutation
1,800
true
false
false
false
false
false
true
false
false
false
6,100
494C
Malek is a rich man. He also is very generous. That's why he decided to split his money between poor people. A charity institute knows _n_ poor people numbered from 1 to _n_. The institute gave Malek _q_ recommendations. A recommendation is a segment of people like [_l_,u2009_r_] which means the institute recommended that Malek gives one dollar to every person whose number is in this segment. However this charity has very odd rules about the recommendations. Because of those rules the recommendations are given in such a way that for every two recommendation [_a_,u2009_b_] and [_c_,u2009_d_] one of the following conditions holds: The two segments are completely disjoint. More formally either _a_u2009≤u2009_b_u2009<u2009_c_u2009≤u2009_d_ or _c_u2009≤u2009_d_u2009<u2009_a_u2009≤u2009_b_ One of the two segments are inside another. More formally either _a_u2009≤u2009_c_u2009≤u2009_d_u2009≤u2009_b_ or _c_u2009≤u2009_a_u2009≤u2009_b_u2009≤u2009_d_. The goodness of a charity is the value of maximum money a person has after Malek finishes giving his money. The institute knows for each recommendation what is the probability that Malek will accept it. They want to know the expected value of goodness of this charity. So they asked you for help. You have been given the list of recommendations and for each recommendation the probability of it being accepted by Malek. You have also been given how much money each person initially has. You must find the expected value of goodness. Input In the first line two space-separated integers _n_,u2009_q_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_q_u2009≤u20095000) are given. In the second line _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109) are given meaning that person number _i_ initially has _a__i_ dollars. Each of the next _q_ lines contains three space-separated numbers _l__i_,u2009_r__i_,u2009_p__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_, 0u2009≤u2009_p_u2009≤u20091) where _l__i_ and _r__i_ are two integers describing the segment of recommendation and _p__i_ is a real number given with exactly three digits after decimal point which is equal to probability of Malek accepting this recommendation. Note that a segment may appear several times in recommendations. Output Output the sought value. Your answer will be considered correct if its absolute or relative error is less than 10u2009-u20096. Examples Input 5 2 1 7 2 4 3 1 3 0.500 2 2 0.500 Input 5 2 281 280 279 278 282 1 4 1.000 1 4 0.000 Input 3 5 1 2 3 1 3 0.500 2 2 0.250 1 2 0.800 1 1 0.120 2 2 0.900
2,600
false
false
false
true
false
false
false
false
false
false
7,856
457F
Pieguy and Piegirl are playing a game. They have a rooted binary tree, that has a property that each node is either a leaf or has exactly two children. Each leaf has a number associated with it. On his/her turn a player can choose any two leafs that share their immediate parent, remove them, and associate either of their values with their parent, that now became a leaf (the player decides which of the two values to associate). The game ends when only one node (the one that was the root of the tree) is left. Pieguy goes first, and his goal is to maximize the value that will be associated with the root when the game ends. Piegirl wants to minimize that value. Assuming that both players are playing optimally, what number will be associated with the root when the game ends? Input First line contains a single integer _t_ (1u2009≤u2009_t_u2009≤u2009100) — number of test cases. Then _t_ test cases follow. Each test case begins with an empty line, followed by a line with a single integer _n_ (1u2009≤u2009_n_u2009≤u2009250), followed by _n_ lines describing _n_ nodes of the tree. Each of those _n_ lines either contains a non-negative number _a__i_, indicating a leaf node with value _a__i_ (0u2009≤u2009_a__i_u2009≤u20091000) associated with it, or u2009-u20091 followed by integers _l_ and _r_, indicating a non-leaf node with children _l_ and _r_ (0u2009≤u2009_l_,u2009_r_u2009≤u2009_n_u2009-u20091). Nodes are numbered from 0 to _n_u2009-u20091. The root is always node 0. Output For each test case print one line with one integer on it — the number that will be associated with the root when the game ends. Examples Input 43 -1 1 2 10 5 5 -1 1 2 -1 3 4 10 5 20 7 -1 1 2 -1 3 4 -1 5 6 1 2 3 4 11 -1 1 2 -1 3 4 -1 5 6 -1 7 8 15 7 -1 9 10 7 8 9 11
3,200
false
true
false
true
false
false
false
false
false
false
8,007
385A
The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following _n_ days. According to the bear's data, on the _i_-th (1u2009≤u2009_i_u2009≤u2009_n_) day, the price for one barrel of honey is going to is _x__i_ kilos of raspberry. Unfortunately, the bear has neither a honey barrel, nor the raspberry. At the same time, the bear's got a friend who is ready to lend him a barrel of honey for exactly one day for _c_ kilograms of raspberry. That's why the bear came up with a smart plan. He wants to choose some day _d_ (1u2009≤u2009_d_u2009<u2009_n_), lent a barrel of honey and immediately (on day _d_) sell it according to a daily exchange rate. The next day (_d_u2009+u20091) the bear wants to buy a new barrel of honey according to a daily exchange rate (as he's got some raspberry left from selling the previous barrel) and immediately (on day _d_u2009+u20091) give his friend the borrowed barrel of honey as well as _c_ kilograms of raspberry for renting the barrel. The bear wants to execute his plan at most once and then hibernate. What maximum number of kilograms of raspberry can he earn? Note that if at some point of the plan the bear runs out of the raspberry, then he won't execute such a plan. Input The first line contains two space-separated integers, _n_ and _c_ (2u2009≤u2009_n_u2009≤u2009100,u20090u2009≤u2009_c_u2009≤u2009100), — the number of days and the number of kilos of raspberry that the bear should give for borrowing the barrel. The second line contains _n_ space-separated integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (0u2009≤u2009_x__i_u2009≤u2009100), the price of a honey barrel on day _i_. Output Print a single integer — the answer to the problem. Examples Input 6 2 100 1 10 40 10 40 Note In the first sample the bear will lend a honey barrel at day 3 and then sell it for 7. Then the bear will buy a barrel for 3 and return it to the friend. So, the profit is (7 - 3 - 1) = 3. In the second sample bear will lend a honey barrel at day 1 and then sell it for 100. Then the bear buy the barrel for 1 at the day 2. So, the profit is (100 - 1 - 2) = 97.
1,000
false
true
true
false
false
false
true
false
false
false
8,291
243C
Old MacDonald has a farm and a large potato field, (1010u2009+u20091)u2009×u2009(1010u2009+u20091) square meters in size. The field is divided into square garden beds, each bed takes up one square meter. Old McDonald knows that the Colorado potato beetle is about to invade his farm and can destroy the entire harvest. To fight the insects, Old McDonald wants to spray some beds with insecticides. So Old McDonald went to the field, stood at the center of the central field bed and sprayed this bed with insecticides. Now he's going to make a series of movements and spray a few more beds. During each movement Old McDonald moves left, right, up or down the field some integer number of meters. As Old McDonald moves, he sprays all the beds he steps on. In other words, the beds that have any intersection at all with Old McDonald's trajectory, are sprayed with insecticides. When Old McDonald finished spraying, he wrote out all his movements on a piece of paper. Now he wants to know how many beds won't be infected after the invasion of the Colorado beetles. It is known that the invasion of the Colorado beetles goes as follows. First some bed on the field border gets infected. Than any bed that hasn't been infected, hasn't been sprayed with insecticides and has a common side with an infected bed, gets infected as well. Help Old McDonald and determine the number of beds that won't be infected by the Colorado potato beetle. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of Old McDonald's movements. Next _n_ lines contain the description of Old McDonald's movements. The _i_-th of these lines describes the _i_-th movement. Each movement is given in the format "_d__i_ _x__i_", where _d__i_ is the character that determines the direction of the movement ("L", "R", "U" or "D" for directions "left", "right", "up" and "down", correspondingly), and _x__i_ (1u2009≤u2009_x__i_u2009≤u2009106) is an integer that determines the number of meters in the movement. Output Print a single integer — the number of beds that won't be infected by the Colorado potato beetle. 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.
2,200
false
false
true
false
false
false
false
false
false
false
8,861
1909F1
In the easy version, the $$$a_i$$$ are in the range $$$[0, n]$$$; in the hard version, the $$$a_i$$$ are in the range $$$[-1, n]$$$ and the definition of good permutation is slightly different. You can make hacks only if all versions of the problem are solved. You are given an integer $$$n$$$ and an array $$$a_1, a_2 dots, a_n$$$ of integers in the range $$$[0, n]$$$. A permutation $$$p_1, p_2, dots, p_n$$$ of $$$[1, 2, dots, n]$$$ is good if, for each $$$i$$$, the following condition is true: the number of values $$$leq i$$$ in $$$[p_1, p_2, dots, p_i]$$$ is exactly $$$a_i$$$. Count the good permutations of $$$[1, 2, dots, n]$$$, modulo $$$998,244,353$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 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$$$ ($$$0 le a_i le n$$$), which describe the conditions for a good permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single line containing the number of good permutations, modulo $$$998,244,353$$$. Example Input 5 5 1 2 3 4 5 6 0 2 2 2 4 6 6 0 1 3 4 5 5 6 1 2 3 2 4 6 15 0 0 1 1 1 2 3 4 5 6 7 9 11 13 15 Note In the first test case, the only good permutation is $$$[1, 2, 3, 4, 5]$$$. In the second test case, there are $$$4$$$ good permutations: $$$[2, 1, 5, 6, 3, 4]$$$, $$$[2, 1, 5, 6, 4, 3]$$$, $$$[2, 1, 6, 5, 3, 4]$$$, $$$[2, 1, 6, 5, 4, 3]$$$. For example, $$$[2, 1, 5, 6, 3, 4]$$$ is good because: $$$a_1 = 0$$$, and there are $$$0$$$ values $$$leq 1$$$ in $$$[p_1] = [2]$$$; $$$a_2 = 2$$$, and there are $$$2$$$ values $$$leq 2$$$ in $$$[p_1, p_2] = [2, 1]$$$; $$$a_3 = 2$$$, and there are $$$2$$$ values $$$leq 3$$$ in $$$[p_1, p_2, p_3] = [2, 1, 5]$$$; $$$a_4 = 2$$$, and there are $$$2$$$ values $$$leq 4$$$ in $$$[p_1, p_2, p_3, p_4] = [2, 1, 5, 6]$$$; $$$a_5 = 4$$$, and there are $$$4$$$ values $$$leq 5$$$ in $$$[p_1, p_2, p_3, p_4, p_5] = [2, 1, 5, 6, 3]$$$; $$$a_6 = 6$$$, and there are $$$6$$$ values $$$leq 6$$$ in $$$[p_1, p_2, p_3, p_4, p_5, p_6] = [2, 1, 5, 6, 3, 4]$$$. In the third test case, there are no good permutations, because there are no permutations with $$$a_6 = 5$$$ values $$$leq 6$$$ in $$$[p_1, p_2, p_3, p_4, p_5, p_6]$$$.
2,200
true
false
false
true
false
false
true
false
false
false
852
1796C
A set of positive integers $$$S$$$ is called beautiful if, for every two integers $$$x$$$ and $$$y$$$ from this set, either $$$x$$$ divides $$$y$$$ or $$$y$$$ divides $$$x$$$ (or both). You are given two integers $$$l$$$ and $$$r$$$. Consider all beautiful sets consisting of integers not less than $$$l$$$ and not greater than $$$r$$$. You have to print two numbers: the maximum possible size of a beautiful set where all elements are from $$$l$$$ to $$$r$$$; the number of beautiful sets consisting of integers from $$$l$$$ to $$$r$$$ with the maximum possible size. Since the second number can be very large, print it modulo $$$998244353$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Each test case consists of one line containing two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le 10^6$$$). Output For each test case, print two integers — the maximum possible size of a beautiful set consisting of integers from $$$l$$$ to $$$r$$$, and the number of such sets with maximum possible size. Since the second number can be very large, print it modulo $$$998244353$$$. Note In the first test case, the maximum possible size of a beautiful set with integers from $$$3$$$ to $$$11$$$ is $$$2$$$. There are $$$4$$$ such sets which have the maximum possible size: $$${ 3, 6 }$$$; $$${ 3, 9 }$$$; $$${ 4, 8 }$$$; $$${ 5, 10 }$$$.
1,600
true
false
false
false
false
false
false
true
false
false
1,500
2018E2
This is the hard version of the problem. In this version, the constraints on $$$n$$$ and the time limit are higher. You can make hacks only if both versions of the problem are solved. A set of (closed) segments is complex if it can be partitioned into some subsets such that all the subsets have the same size; and a pair of segments intersects if and only if the two segments are in the same subset. You are given $$$n$$$ segments $$$[l_1, r_1], [l_2, r_2], ldots, [l_n, r_n]$$$. Find the maximum size of a complex subset of these segments. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the number of segments. The second line of each test case contains $$$n$$$ integers $$$l_1, l_2, ldots, l_n$$$ ($$$1 le l_i le 2n$$$)xa0— the left endpoints of the segments. The third line of each test case contains $$$n$$$ integers $$$r_1, r_2, ldots, r_n$$$ ($$$l_i leq r_i le 2n$$$)xa0— the right endpoints of the segments. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integer: the maximum size of a complex subset of the given segments. Example Input 3 3 1 2 3 5 4 6 5 1 2 3 6 8 5 4 7 9 10 5 3 1 4 1 5 7 2 6 5 10 Note In the first test case, all pairs of segments intersect, therefore it is optimal to form a single group containing all of the three segments. In the second test case, there is no valid partition for all of the five segments. A valid partition with four segments is the following: $$${{ [1, 5], [2, 4] }, { [6, 9], [8, 10] }}$$$. In the third test case, it is optimal to make a single group containing all the segments except the second.
3,400
true
true
false
false
true
false
false
true
true
false
158
319E
Problem - 319E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures *3000 No tag edit access → Contest materials from our set to interval (_c_,u2009_d_) from our set if and only if _c_u2009<u2009_a_u2009<u2009_d_ or _c_u2009<u2009_b_u2009<u2009_d_. Also there is a path from interval _I_1 from our set to interval _I_2 from our set if there is a sequence of successive moves starting from _I_1 so that we can reach _I_2. Your program should handle the queries of the following two types: 1. "1 x y" (_x_u2009<u2009_y_) — add the new interval (_x_,u2009_y_) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals. 2. "2 a b" (_a_u2009≠u2009_b_) — answer the question: is there a path from _a_-th (one-based) added interval to _b_-th (one-based) added interval? Answer all the queries. Note, that initially you have an empty set of intervals. Input The first line of the input contains integer _n_ denoting the number of queries, (1u2009≤u2009_n_u2009≤u2009105). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value. It's guaranteed that all queries are correct. Output For each query of the second type print "YES" or "NO" on a separate line depending on the answer. Examples Input 5 1 1 5 1 5 11 2 1 2 1 2 9 2 1 2 Output NO YES
3,000
false
false
false
false
true
false
false
false
false
false
8,554
1492E
You are storing an integer array of length $$$m$$$ in a database. To maintain internal integrity and protect data, the database stores $$$n$$$ copies of this array. Unfortunately, the recent incident may have altered the stored information in every copy in the database. It's believed, that the incident altered at most two elements in every copy. You need to recover the original array based on the current state of the database. In case there are multiple ways to restore the array, report any. If there is no array that differs from every copy in no more than two positions, report that as well. Input The first line contains integers $$$n$$$ and $$$m$$$ ($$$2 le n$$$; $$$1 le m$$$; $$$n cdot m le 250,000$$$)xa0— the number of copies and the size of the array. Each of the following $$$n$$$ lines describes one of the currently stored copies in the database, it consists of $$$m$$$ integers $$$s_{i, 1}, s_{i, 2}, dots, s_{i, m}$$$ ($$$1 le s_{i, j} le 10^9$$$). Output If there is an array consistent with all given copies, print "Yes" and then the array itself. The array must have length $$$m$$$ and contain integers between $$$1$$$ and $$$10^9$$$ only. Otherwise, print "No". If there are multiple possible arrays, print any of them. Examples Input 3 4 1 10 10 100 1 1 1 100 10 100 1 100 Input 10 7 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 2 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 Input 2 5 2 2 1 1 1 1 1 2 2 2 Note In the first example, the array $$$[1, 10, 1, 100]$$$ differs from first and second copies in just one position, and from the third copy in two positions. In the second example, array $$$[1, 1, 1, 1, 1, 1, 1]$$$ is the same as the first copy and differs from all other copies in at most two positions. In the third example, there is no array differing in at most two positions from every database's copy.
2,500
false
true
true
false
false
true
true
false
false
false
3,209
1749D
Consider an array $$$a$$$ of length $$$n$$$ with elements numbered from $$$1$$$ to $$$n$$$. It is possible to remove the $$$i$$$-th element of $$$a$$$ if $$$gcd(a_i, i) = 1$$$, where $$$gcd$$$ denotes the greatest common divisor. After an element is removed, the elements to the right are shifted to the left by one position. An array $$$b$$$ with $$$n$$$ integers such that $$$1 le b_i le n - i + 1$$$ is a removal sequence for the array $$$a$$$ if it is possible to remove all elements of $$$a$$$, if you remove the $$$b_1$$$-th element, then the $$$b_2$$$-th, ..., then the $$$b_n$$$-th element. For example, let $$$a = [42, 314]$$$: $$$[1, 1]$$$ is a removal sequence: when you remove the $$$1$$$-st element of the array, the condition $$$gcd(42, 1) = 1$$$ holds, and the array becomes $$$[314]$$$; when you remove the $$$1$$$-st element again, the condition $$$gcd(314, 1) = 1$$$ holds, and the array becomes empty. $$$[2, 1]$$$ is not a removal sequence: when you try to remove the $$$2$$$-nd element, the condition $$$gcd(314, 2) = 1$$$ is false. An array is ambiguous if it has at least two removal sequences. For example, the array $$$[1, 2, 5]$$$ is ambiguous: it has removal sequences $$$[3, 1, 1]$$$ and $$$[1, 2, 1]$$$. The array $$$[42, 314]$$$ is not ambiguous: the only removal sequence it has is $$$[1, 1]$$$. You are given two integers $$$n$$$ and $$$m$$$. You have to calculate the number of ambiguous arrays $$$a$$$ such that the length of $$$a$$$ is from $$$1$$$ to $$$n$$$ and each $$$a_i$$$ is an integer from $$$1$$$ to $$$m$$$. Output Print one integer — the number of ambiguous arrays $$$a$$$ such that the length of $$$a$$$ is from $$$1$$$ to $$$n$$$ and each $$$a_i$$$ is an integer from $$$1$$$ to $$$m$$$. Since the answer can be very large, print it modulo $$$998244353$$$.
1,900
true
false
false
true
false
false
false
false
false
false
1,788
1279C
Santa has to send presents to the kids. He has a large stack of $$$n$$$ presents, numbered from $$$1$$$ to $$$n$$$; the topmost present has number $$$a_1$$$, the next present is $$$a_2$$$, and so on; the bottom present has number $$$a_n$$$. All numbers are distinct. Santa has a list of $$$m$$$ distinct presents he has to send: $$$b_1$$$, $$$b_2$$$, ..., $$$b_m$$$. He will send them in the order they appear in the list. To send a present, Santa has to find it in the stack by removing all presents above it, taking this present and returning all removed presents on top of the stack. So, if there are $$$k$$$ presents above the present Santa wants to send, it takes him $$$2k + 1$$$ seconds to do it. Fortunately, Santa can speed the whole process up — when he returns the presents to the stack, he may reorder them as he wishes (only those which were above the present he wanted to take; the presents below cannot be affected in any way). What is the minimum time required to send all of the presents, provided that Santa knows the whole list of presents he has to send and reorders the presents optimally? Santa cannot change the order of presents or interact with the stack of presents in any other way. Your program has to answer $$$t$$$ different test cases. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then the test cases follow, each represented by three lines. The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le m le n le 10^5$$$) — the number of presents in the stack and the number of presents Santa wants to send, respectively. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le n$$$, all $$$a_i$$$ are unique) — the order of presents in the stack. The third line contains $$$m$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_m$$$ ($$$1 le b_i le n$$$, all $$$b_i$$$ are unique) — the ordered list of presents Santa has to send. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print one integer — the minimum number of seconds which Santa has to spend sending presents, if he reorders the presents optimally each time he returns them into the stack. Example Input 2 3 3 3 1 2 3 2 1 7 2 2 1 7 3 4 5 6 3 1
1,400
false
false
true
false
true
false
false
false
false
false
4,298
427D
Police headquarter is monitoring signal on different frequency levels. They have got two suspiciously encoded strings _s_1 and _s_2 from two different frequencies as signals. They are suspecting that these two strings are from two different criminals and they are planning to do some evil task. Now they are trying to find a common substring of minimum length between these two strings. The substring must occur only once in the first string, and also it must occur only once in the second string. Given two strings _s_1 and _s_2 consist of lowercase Latin letters, find the smallest (by length) common substring _p_ of both _s_1 and _s_2, where _p_ is a unique substring in _s_1 and also in _s_2. See notes for formal definition of substring and uniqueness. Input The first line of input contains _s_1 and the second line contains _s_2 (1u2009≤u2009_s_1,u2009_s_2u2009≤u20095000). Both strings consist of lowercase Latin letters. Output Print the length of the smallest common unique substring of _s_1 and _s_2. If there are no common unique substrings of _s_1 and _s_2 print -1. Note Imagine we have string _a_u2009=u2009_a_1_a_2_a_3..._a__a_, where _a_ is the length of string _a_, and _a__i_ is the _i__th_ letter of the string. We will call string _a__l__a__l_u2009+u20091_a__l_u2009+u20092..._a__r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_a_) the substring [_l_,u2009_r_] of the string _a_. The substring [_l_,u2009_r_] is unique in _a_ if and only if there is no pair _l_1,u2009_r_1 such that _l_1u2009≠u2009_l_ and the substring [_l_1,u2009_r_1] is equal to the substring [_l_,u2009_r_] in _a_.
2,200
false
false
false
true
false
false
false
false
false
false
8,128
1313E
Vasya had three strings $$$a$$$, $$$b$$$ and $$$s$$$, which consist of lowercase English letters. The lengths of strings $$$a$$$ and $$$b$$$ are equal to $$$n$$$, the length of the string $$$s$$$ is equal to $$$m$$$. Vasya decided to choose a substring of the string $$$a$$$, then choose a substring of the string $$$b$$$ and concatenate them. Formally, he chooses a segment $$$[l_1, r_1]$$$ ($$$1 leq l_1 leq r_1 leq n$$$) and a segment $$$[l_2, r_2]$$$ ($$$1 leq l_2 leq r_2 leq n$$$), and after concatenation he obtains a string $$$a[l_1, r_1] + b[l_2, r_2] = a_{l_1} a_{l_1 + 1} ldots a_{r_1} b_{l_2} b_{l_2 + 1} ldots b_{r_2}$$$. Now, Vasya is interested in counting number of ways to choose those segments adhering to the following conditions: segments $$$[l_1, r_1]$$$ and $$$[l_2, r_2]$$$ have non-empty intersection, i.e. there exists at least one integer $$$x$$$, such that $$$l_1 leq x leq r_1$$$ and $$$l_2 leq x leq r_2$$$; the string $$$a[l_1, r_1] + b[l_2, r_2]$$$ is equal to the string $$$s$$$. Input The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 500,000, 2 leq m leq 2 cdot n$$$)xa0— the length of strings $$$a$$$ and $$$b$$$ and the length of the string $$$s$$$. The next three lines contain strings $$$a$$$, $$$b$$$ and $$$s$$$, respectively. The length of the strings $$$a$$$ and $$$b$$$ is $$$n$$$, while the length of the string $$$s$$$ is $$$m$$$. All strings consist of lowercase English letters. Output Print one integerxa0— the number of ways to choose a pair of segments, which satisfy Vasya's conditions. Examples Input 6 5 aabbaa baaaab aaaaa Input 9 12 abcabcabc xyzxyzxyz abcabcayzxyz Note Let's list all the pairs of segments that Vasya could choose in the first example: 1. $$$[2, 2]$$$ and $$$[2, 5]$$$; 2. $$$[1, 2]$$$ and $$$[2, 4]$$$; 3. $$$[5, 5]$$$ and $$$[2, 5]$$$; 4. $$$[5, 6]$$$ and $$$[3, 5]$$$;
2,700
false
false
false
false
true
false
false
false
false
false
4,125
1549A
Gregor is learning about RSA cryptography, and although he doesn't understand how RSA works, he is now fascinated with prime numbers and factoring them. Gregor's favorite prime number is $$$P$$$. Gregor wants to find two bases of $$$P$$$. Formally, Gregor is looking for two integers $$$a$$$ and $$$b$$$ which satisfy both of the following properties. $$$P bmod a = P bmod b$$$, where $$$x bmod y$$$ denotes the remainder when $$$x$$$ is divided by $$$y$$$, and $$$2 le a < b le P$$$. Help Gregor find two bases of his favorite prime number! Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Each subsequent line contains the integer $$$P$$$ ($$$5 le P le {10}^9$$$), with $$$P$$$ guaranteed to be prime. Output Your output should consist of $$$t$$$ lines. Each line should consist of two integers $$$a$$$ and $$$b$$$ ($$$2 le a < b le P$$$). If there are multiple possible solutions, print any. Note The first query is $$$P=17$$$. $$$a=3$$$ and $$$b=5$$$ are valid bases in this case, because $$$17 bmod 3 = 17 bmod 5 = 2$$$. There are other pairs which work as well. In the second query, with $$$P=5$$$, the only solution is $$$a=2$$$ and $$$b=4$$$.
800
true
false
false
false
false
false
false
false
false
false
2,900
1714A
Vlad, like everyone else, loves to sleep very much. Every day Vlad has to do $$$n$$$ things, each at a certain time. For each of these things, he has an alarm clock set, the $$$i$$$-th of them is triggered on $$$h_i$$$ hours $$$m_i$$$ minutes every day ($$$0 le h_i < 24, 0 le m_i < 60$$$). Vlad uses the $$$24$$$-hour time format, so after $$$h=12, m=59$$$ comes $$$h=13, m=0$$$ and after $$$h=23, m=59$$$ comes $$$h=0, m=0$$$. This time Vlad went to bed at $$$H$$$ hours $$$M$$$ minutes ($$$0 le H < 24, 0 le M < 60$$$) and asks you to answer: how much he will be able to sleep until the next alarm clock. If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length $$$0$$$. Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases in the test. The first line of the case contains three integers $$$n$$$, $$$H$$$ and $$$M$$$ ($$$1 le n le 10, 0 le H < 24, 0 le M < 60$$$) — the number of alarms and the time Vlad went to bed. The following $$$n$$$ lines contain two numbers each $$$h_i$$$ and $$$m_i$$$ ($$$0 le h_i < 24, 0 le m_i < 60$$$) — the time of the $$$i$$$ alarm. It is acceptable that two or more alarms will trigger at the same time. Numbers describing time do not contain leading zeros. Output Output $$$t$$$ lines, each containing the answer to the corresponding test case. As an answer, output two numbers xa0— the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be 0 0. Example Input 3 1 6 13 8 0 3 6 0 12 30 14 45 6 0 2 23 35 20 15 10 30
900
true
false
true
false
false
false
false
false
false
false
2,000
949E
It is never too late to play the fancy "Binary Cards" game! There is an infinite amount of cards of positive and negative ranks that are used in the game. The absolute value of any card rank is a power of two, i.e. each card has a rank of either 2_k_ or u2009-u20092_k_ for some integer _k_u2009≥u20090. There is an infinite amount of cards of any valid rank. At the beginning of the game player forms his deck that is some multiset (possibly empty) of cards. It is allowed to pick any number of cards of any rank but the small deck is considered to be a skill indicator. Game consists of _n_ rounds. In the _i_-th round jury tells the player an integer _a__i_. After that the player is obligated to draw such a subset of his deck that the sum of ranks of the chosen cards is equal to _a__i_ (it is allowed to not draw any cards, in which case the sum is considered to be equal to zero). If player fails to do so, he loses and the game is over. Otherwise, player takes back all of his cards into his deck and the game proceeds to the next round. Player is considered a winner if he is able to draw the suitable set of cards in each of the rounds. Somebody told you which numbers _a__i_ the jury is going to tell you in each round. Now you want to pick a deck consisting of the minimum number of cards that allows you to win the "Binary Cards" game. Input The first line of input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000), the number of rounds in the game. The second line of input contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009100u2009000u2009≤u2009_a__i_u2009≤u2009100u2009000), the numbers that jury is going to tell in each round. Output In the first line print the integer _k_ (0u2009≤u2009_k_u2009≤u2009100u2009000), the minimum number of cards you have to pick in your deck in ordered to win the "Binary Cards". In the second line print _k_ integers _b_1,u2009_b_2,u2009...,u2009_b__k_ (u2009-u2009220u2009≤u2009_b__i_u2009≤u2009220, _b__i_ is a power of two), the ranks of the cards in your deck. You may output ranks in any order. If there are several optimum decks, you are allowed to print any of them. It is guaranteed that there exists a deck of minimum size satisfying all the requirements above. Note In the first sample there is the only round in the game, in which you may simply draw both your cards. Note that this sample test is the only one satisfying the first test group constraints. In the second sample you may draw the only card u2009-u20091 in the first round, cards 4 and u2009-u20091 in the second round, nothing in the third round, the only card 4 in the fourth round and the whole deck in the fifth round.
2,700
false
false
false
false
false
false
true
false
false
false
5,920
1946A
You are given an array $$$a$$$ of $$$n$$$ integers. The median of an array $$$q_1, q_2, ldots, q_k$$$ is the number $$$p_{lceil frac{k}{2} ceil}$$$, where $$$p$$$ is the array $$$q$$$ sorted in non-decreasing order. For example, the median of the array $$$[9, 5, 1, 2, 6]$$$ is $$$5$$$, as in the sorted array $$$[1, 2, 5, 6, 9]$$$, the number at index $$$lceil frac{5}{2} ceil = 3$$$ is $$$5$$$, and the median of the array $$$[9, 2, 8, 3]$$$ is $$$3$$$, as in the sorted array $$$[2, 3, 8, 9]$$$, the number at index $$$lceil frac{4}{2} ceil = 2$$$ is $$$3$$$. You are allowed to choose an integer $$$i$$$ ($$$1 le i le n$$$) and increase $$$a_i$$$ by $$$1$$$ in one operation. Your task is to find the minimum number of operations required to increase the median of the array. Note that the array $$$a$$$ may not necessarily contain distinct numbers. 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. Then follows the description of the test cases. 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 array $$$a$$$. It is guaranteed that the sum of the values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, you can apply one operation to the first number and obtain the array $$$[3, 2, 8]$$$, the median of this array is $$$3$$$, as it is the number at index $$$lceil frac{3}{2} ceil = 2$$$ in the non-decreasing sorted array $$$[2, 3, 8]$$$. The median of the original array $$$[2, 2, 8]$$$ is $$$2$$$, as it is the number at index $$$lceil frac{3}{2} ceil = 2$$$ in the non-decreasing sorted array $$$[2, 2, 8]$$$. Thus, the median increased ($$$3 > 2$$$) in just one operation. In the fourth test case, you can apply one operation to each of the numbers at indices $$$1, 2, 3$$$ and obtain the array $$$[6, 6, 6, 4, 5]$$$, the median of this array is $$$6$$$, as it is the number at index $$$lceil frac{5}{2} ceil = 3$$$ in the non-decreasing sorted array $$$[4, 5, 6, 6, 6]$$$. The median of the original array $$$[5, 5, 5, 4, 5]$$$ is $$$5$$$, as it is the number at index $$$lceil frac{5}{2} ceil = 2$$$ in the non-decreasing sorted array $$$[4, 5, 5, 5, 5]$$$. Thus, the median increased ($$$6 > 5$$$) in three operations. It can be shown that this is the minimum possible number of operations. In the fifth test case, you can apply one operation to each of the numbers at indices $$$1, 3$$$ and obtain the array $$$[3, 1, 3, 3, 1, 4]$$$, the median of this array is $$$3$$$, as it is the number at index $$$lceil frac{6}{2} ceil = 3$$$ in the non-decreasing sorted array $$$[1, 1, 3, 3, 3, 4]$$$. The median of the original array $$$[2, 1, 2, 3, 1, 4]$$$ is $$$2$$$, as it is the number at index $$$lceil frac{6}{2} ceil = 3$$$ in the non-decreasing sorted array $$$[1, 1, 2, 2, 3, 4]$$$. Thus, the median increased ($$$3 > 2$$$) in two operations. It can be shown that this is the minimum possible number of operations.
800
false
true
true
false
false
false
false
false
true
false
605
629B
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has _n_ friends and each of them can come to the party in a specific range of days of the year from _a__i_ to _b__i_. Of course, Famil Door wants to have as many friends celebrating together with him as possible. Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party. Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095000)xa0— then number of Famil Door's friends. Then follow _n_ lines, that describe the friends. Each line starts with a capital letter 'F' for female friends and with a capital letter 'M' for male friends. Then follow two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_u2009≤u2009_b__i_u2009≤u2009366), providing that the _i_-th friend can come to the party from day _a__i_ to day _b__i_ inclusive. Output Print the maximum number of people that may come to Famil Door's party. Examples Input 4 M 151 307 F 343 352 F 117 145 M 24 128 Input 6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200 Note In the first sample, friends 3 and 4 can come on any day in range [117,u2009128]. In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
1,100
false
false
false
false
false
false
true
false
false
false
7,308
847B
Ivan has an array consisting of _n_ different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array. Ivan represent his array with increasing sequences with help of the following algorithm. While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down. For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one — [2, 4]. Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the number of elements in Ivan's array. The second line contains a sequence consisting of distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — Ivan's array. Output Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
1,600
false
false
false
false
true
false
false
true
false
false
6,370
1728F
There are $$$n$$$ fishermen who have just returned from a fishing trip. The $$$i$$$-th fisherman has caught a fish of size $$$a_i$$$. The fishermen will choose some order in which they are going to tell the size of the fish they caught (the order is just a permutation of size $$$n$$$). However, they are not entirely honest, and they may "increase" the size of the fish they have caught. Formally, suppose the chosen order of the fishermen is $$$[p_1, p_2, p_3, dots, p_n]$$$. Let $$$b_i$$$ be the value which the $$$i$$$-th fisherman in the order will tell to the other fishermen. The values $$$b_i$$$ are chosen as follows: the first fisherman in the order just honestly tells the actual size of the fish he has caught, so $$$b_1 = a_{p_1}$$$; every other fisherman wants to tell a value that is strictly greater than the value told by the previous fisherman, and is divisible by the size of the fish that the fisherman has caught. So, for $$$i > 1$$$, $$$b_i$$$ is the smallest integer that is both strictly greater than $$$b_{i-1}$$$ and divisible by $$$a_{p_i}$$$. For example, let $$$n = 7$$$, $$$a = [1, 8, 2, 3, 2, 2, 3]$$$. If the chosen order is $$$p = [1, 6, 7, 5, 3, 2, 4]$$$, then: $$$b_1 = a_{p_1} = 1$$$; $$$b_2$$$ is the smallest integer divisible by $$$2$$$ and greater than $$$1$$$, which is $$$2$$$; $$$b_3$$$ is the smallest integer divisible by $$$3$$$ and greater than $$$2$$$, which is $$$3$$$; $$$b_4$$$ is the smallest integer divisible by $$$2$$$ and greater than $$$3$$$, which is $$$4$$$; $$$b_5$$$ is the smallest integer divisible by $$$2$$$ and greater than $$$4$$$, which is $$$6$$$; $$$b_6$$$ is the smallest integer divisible by $$$8$$$ and greater than $$$6$$$, which is $$$8$$$; $$$b_7$$$ is the smallest integer divisible by $$$3$$$ and greater than $$$8$$$, which is $$$9$$$. You have to choose the order of fishermen in a way that yields the minimum possible $$$sumlimits_{i=1}^{n} b_i$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 1000$$$) — the number of fishermen. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$).
3,100
false
true
false
false
false
false
false
false
false
false
1,920
182C
Problem - 182C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures greedy *2000 No tag edit access → Contest materials ") Разбор Задач") — the number of elements in the array and the length of the chosen subinterval of the array, correspondingly. The second line contains a sequence consisting of _n_ integers _a_1, _a_2, ..., _a__n_ (_a__i_u2009≤u2009109) — the original array. The third line contains a single integer _k_ (0u2009≤u2009_k_u2009≤u2009_n_) — the maximum allowed number of operations. All numbers in lines are separated by a single space. Output In a single line print the maximum possible optimal sum after no more than _k_ acceptable operations are fulfilled. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. Examples Input 5 3 0 -2 3 -5 1 2 Output 10 Input 5 2 1 -3 -10 4 1 3 Output 14 Input 3 3 -2 -5 4 1 Output 11
2,000
false
true
false
false
true
false
false
false
false
false
9,112
1413B
All techniques in the ninja world consist of hand seals. At the moment Naruto is learning a new technique, which consists of $$$ncdot m$$$ different seals, denoted by distinct numbers. All of them were written in an $$$n imes m$$$ table. The table is lost now. Naruto managed to remember elements of each row from left to right, and elements of each column from top to bottom, but he doesn't remember the order of rows and columns. Please restore the table consistent with this data so that Naruto will be able to learn the new technique. Input The first line of the input contains the only integer $$$t$$$ ($$$1leq tleq 100,000$$$) denoting the number of test cases. Their descriptions follow. The first line of each test case description consists of two space-separated integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 500$$$) standing for the number of rows and columns in the table, respectively. All hand seals are encoded by the positive integers from $$$1$$$ to $$$ncdot m$$$. The following $$$n$$$ lines contain $$$m$$$ space separated integers each, denoting elements of an arbitrary row in the table left to right. The following $$$m$$$ lines contain $$$n$$$ space separated integers each, denoting elements of an arbitrary column in the table top to bottom. Sum of $$$nm$$$ over all test cases does not exceed $$$250,000$$$. It is guaranteed that each row occurs in the input exactly once, as well as each column. It is also guaranteed that each number from $$$1$$$ to $$$nm$$$ occurs exactly once in all rows, as well as in all columns. Finally, it is guaranteed that a table consistent with the input exists. Output For each test case, output $$$n$$$ lines with $$$m$$$ space-separated integers each, denoting the restored table. One can show that the answer is always unique. Example Input 2 2 3 6 5 4 1 2 3 1 6 2 5 3 4 3 1 2 3 1 3 1 2 Note Consider the first test case. The matrix is $$$2 imes 3$$$. You are given the rows and columns in arbitrary order. One of the rows is $$$[6, 5, 4]$$$. One of the rows is $$$[1, 2, 3]$$$. One of the columns is $$$[1, 6]$$$. One of the columns is $$$[2, 5]$$$. One of the columns is $$$[3, 4]$$$. You are to reconstruct the matrix. The answer is given in the output.
1,100
false
false
true
false
false
false
false
false
false
false
3,613
1426D
Kolya got an integer array $$$a_1, a_2, dots, a_n$$$. The array can contain both positive and negative integers, but Kolya doesn't like $$$0$$$, so the array doesn't contain any zeros. Kolya doesn't like that the sum of some subsegments of his array can be $$$0$$$. The subsegment is some consecutive segment of elements of the array. You have to help Kolya and change his array in such a way that it doesn't contain any subsegments with the sum $$$0$$$. To reach this goal, you can insert any integers between any pair of adjacent elements of the array (integers can be really any: positive, negative, $$$0$$$, any by absolute value, even such a huge that they can't be represented in most standard programming languages). Your task is to find the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $$$0$$$. Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 200,000$$$) — the number of elements in Kolya's array. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^{9} le a_i le 10^{9}, a_i eq 0$$$) — the description of Kolya's array. Output Print the minimum number of integers you have to insert into Kolya's array in such a way that the resulting array doesn't contain any subsegments with the sum $$$0$$$. Examples Input 9 -1 1 -1 1 -1 1 1 -1 -1 Input 8 16 -5 -11 -15 10 5 4 -4 Note Consider the first example. There is only one subsegment with the sum $$$0$$$. It starts in the second element and ends in the fourth element. It's enough to insert one element so the array doesn't contain any subsegments with the sum equal to zero. For example, it is possible to insert the integer $$$1$$$ between second and third elements of the array. There are no subsegments having sum $$$0$$$ in the second example so you don't need to do anything.
1,500
false
true
false
false
true
true
false
false
true
false
3,536
6A
Problem - 6A - 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 geometry *900 No tag edit access → Contest materials ")
900
false
false
false
false
false
false
true
false
false
false
9,965
785E
Anton likes permutations, especially he likes to permute their elements. Note that a permutation of _n_ elements is a sequence of numbers {_a_1,u2009_a_2,u2009...,u2009_a__n_}, in which every number from 1 to _n_ appears exactly once. One day Anton got a new permutation and started to play with it. He does the following operation _q_ times: he takes two elements of the permutation and swaps these elements. After each operation he asks his friend Vanya, how many inversions there are in the new permutation. The number of inversions in a permutation is the number of distinct pairs (_i_,u2009_j_) such that 1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_n_ and _a__i_u2009>u2009_a__j_. Vanya is tired of answering Anton's silly questions. So he asked you to write a program that would answer these questions instead of him. Initially Anton's permutation was {1,u20092,u2009...,u2009_n_}, that is _a__i_u2009=u2009_i_ for all _i_ such that 1u2009≤u2009_i_u2009≤u2009_n_. Input The first line of the input contains two integers _n_ and _q_ (1u2009≤u2009_n_u2009≤u2009200u2009000,u20091u2009≤u2009_q_u2009≤u200950u2009000)xa0— the length of the permutation and the number of operations that Anton does. Each of the following _q_ lines of the input contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_,u2009_r__i_u2009≤u2009_n_)xa0— the indices of elements that Anton swaps during the _i_-th operation. Note that indices of elements that Anton swaps during the _i_-th operation can coincide. Elements in the permutation are numbered starting with one. Output Output _q_ lines. The _i_-th line of the output is the number of inversions in the Anton's permutation after the _i_-th operation. Examples Input 6 7 1 4 3 5 2 3 3 3 3 6 2 1 5 1 Note Consider the first sample. After the first Anton's operation the permutation will be {1,u20092,u20093,u20095,u20094}. There is only one inversion in it: (4,u20095). After the second Anton's operation the permutation will be {1,u20095,u20093,u20092,u20094}. There are four inversions: (2,u20093), (2,u20094), (2,u20095) and (3,u20094). After the third Anton's operation the permutation will be {1,u20094,u20093,u20092,u20095}. There are three inversions: (2,u20093), (2,u20094) and (3,u20094). After the fourth Anton's operation the permutation doesn't change, so there are still three inversions.
2,200
false
false
false
false
true
false
true
false
false
false
6,629
1499B
You are given a string $$$s$$$, consisting only of characters '0' or '1'. Let $$$s$$$ be the length of $$$s$$$. You are asked to choose some integer $$$k$$$ ($$$k > 0$$$) and find a sequence $$$a$$$ of length $$$k$$$ such that: $$$1 le a_1 < a_2 < dots < a_k le s$$$; $$$a_{i-1} + 1 < a_i$$$ for all $$$i$$$ from $$$2$$$ to $$$k$$$. The characters at positions $$$a_1, a_2, dots, a_k$$$ are removed, the remaining characters are concatenated without changing the order. So, in other words, the positions in the sequence $$$a$$$ should not be adjacent. Let the resulting string be $$$s'$$$. $$$s'$$$ is called sorted if for all $$$i$$$ from $$$2$$$ to $$$s'$$$ $$$s'_{i-1} le s'_i$$$. Does there exist such a sequence $$$a$$$ that the resulting string $$$s'$$$ is sorted? 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 only line of each testcase contains a string $$$s$$$ ($$$2 le s le 100$$$). Each character is either '0' or '1'. Output For each testcase print "YES" if there exists a sequence $$$a$$$ such that removing the characters at positions $$$a_1, a_2, dots, a_k$$$ and concatenating the parts without changing the order produces a sorted string. Otherwise, print "NO". You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer). Example Input 5 10101011011 0000 11111 110 1100 Note In the first testcase you can choose a sequence $$$a=[1,3,6,9]$$$. Removing the underlined letters from "10101011011" will produce a string "0011111", which is sorted. In the second and the third testcases the sequences are already sorted. In the fourth testcase you can choose a sequence $$$a=[3]$$$. $$$s'=$$$ "11", which is sorted. In the fifth testcase there is no way to choose a sequence $$$a$$$ such that $$$s'$$$ is sorted.
1,000
false
true
true
true
false
false
true
false
false
false
3,174
1870F
You are given positive integers $$$n$$$ and $$$k$$$. For each number from $$$1$$$ to $$$n$$$, we write its representation in the number system with base $$$k$$$ (without leading zeros) and then sort the resulting array in lexicographic order as strings. In the sorted array, we number the elements from $$$1$$$ to $$$n$$$ (i.e., indexing starts from $$$1$$$). Find the number of values $$$i$$$ such that the representation of number $$$i$$$ is at the $$$i$$$-th position in the sorted array of representations. Examples of representations: $$$1$$$ in any number system is equal to $$$1$$$, $$$7$$$ with $$$k = 3$$$ is written as $$$21$$$, and $$$81$$$ with $$$k = 9$$$ is written as $$$100$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$) — the number of test cases. This is followed by a description of the test cases. The only line of each test case contains integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^{18}$$$, $$$2 leq k leq 10^{18}$$$). Output For each test case, output a single integer — the number of values $$$1 leq i leq n$$$ such that the representation of number $$$i$$$ in the number system with base $$$k$$$ is at the $$$i$$$-th position after sorting. Example Input 8 2 2 4 2 6 4 33 2 532 13 780011804570805480 3788 366364720306464627 4702032149561577 293940402103595405 2 Note In the first test case, for numbers $$$1$$$ and $$$2$$$, their representations are at positions $$$1$$$ and $$$2$$$ respectively. In the second test case, the sorted array is $$$[1_2 = 1, 10_2 = 2, 100_2 = 4, 11_2 = 3]$$$, and only the representations of numbers $$$1$$$ and $$$2$$$ are at the required positions. In the third test case, the sorted array is $$$[1_4 = 1, 10_4 = 4, 11_4 = 5, 12_4 = 6, 2_4 = 2, 3_4 = 3]$$$, and only the number $$$1$$$ is at its position.
2,900
true
false
false
false
false
false
false
true
false
false
1,054
2008E
Sakurako really loves alternating strings. She calls a string $$$s$$$ of lowercase Latin letters an alternating string if characters in the even positions are the same, if characters in the odd positions are the same, and the length of the string is even. For example, the strings 'abab' and 'gg' are alternating, while the strings 'aba' and 'ggwp' are not. As a good friend, you decided to gift such a string, but you couldn't find one. Luckily, you can perform two types of operations on the string: 1. Choose an index $$$i$$$ and delete the $$$i$$$-th character from the string, which will reduce the length of the string by $$$1$$$. This type of operation can be performed no more than $$$1$$$ time; 2. Choose an index $$$i$$$ and replace $$$s_i$$$ with any other letter. Since you are in a hurry, you need to determine the minimum number of operations required to make the string an alternating one. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) xa0— the number of test cases. The first line of each test case contains a single number $$$n$$$ ($$$1 le nle 2cdot 10^5$$$) xa0— the length of the string. The second line of each test case contains a string $$$s$$$, consisting of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the minimum number of operations required to turn the string $$$s$$$ into an alternating one. Example Input 10 1 a 2 ca 3 aab 5 ababa 6 acdada 9 ejibmyyju 6 bbccbc 6 abacba 5 bcbca 5 dcbdb Output 1 0 1 1 2 6 2 3 1 1 Note For the string ababa, you can delete the first character to get baba, which is an alternating string. For the string acdada, you can change the first two characters to get dadada, which is an alternating string.
1,500
false
true
true
true
true
false
true
false
false
false
204
1038B
Find out if it is possible to partition the first $$$n$$$ positive integers into two non-empty disjoint sets $$$S_1$$$ and $$$S_2$$$ such that: $$$mathrm{gcd}(mathrm{sum}(S_1), mathrm{sum}(S_2)) > 1$$$ Here $$$mathrm{sum}(S)$$$ denotes the sum of all elements present in set $$$S$$$ and $$$mathrm{gcd}$$$ means the Output If such partition doesn't exist, print "No" (quotes for clarity). Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing $$$S_1$$$ and $$$S_2$$$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitionsxa0— print any of them. Note In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No". In the second example, the sums of the sets are $$$2$$$ and $$$4$$$ respectively. The $$$mathrm{gcd}(2, 4) = 2 > 1$$$, hence that is one of the possible answers.
1,100
true
false
false
false
false
true
false
false
false
false
5,507
1921F
You are given an array $$$a$$$ of $$$n$$$ numbers. There are also $$$q$$$ queries of the form $$$s, d, k$$$. For each query $$$q$$$, find the sum of elements $$$a_s + a_{s+d} cdot 2 + dots + a_{s + d cdot (k - 1)} cdot k$$$. In other words, for each query, it is necessary to find the sum of $$$k$$$ elements of the array with indices starting from the $$$s$$$-th, taking steps of size $$$d$$$, multiplying it by the serial number of the element in the resulting sequence. Input Each test consists of several testcases. The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of testcases. Next lines contain descriptions of testcases. The first line of each testcase contains two numbers $$$n, q$$$ ($$$1 le n le 10^5, 1 le q le 2 cdot 10^5$$$) — the number of elements in the array $$$a$$$ and the number of queries. The second line contains $$$n$$$ integers $$$a_1, ... a_n$$$ ($$$-10^8 le a_1, ..., a_n le 10^8$$$) — elements of the array $$$a$$$. The next $$$q$$$ lines each contain three integers $$$s$$$, $$$d$$$, and $$$k$$$ ($$$1 le s, d, k le n$$$, $$$s + dcdot (k - 1) le n$$$ ). It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$10^5$$$, and that the sum of $$$q$$$ over all testcases does not exceed $$$2 cdot 10^5 $$$. Output For each testcase, print $$$q$$$ numbers in a separate line — the desired sums, separated with space. Example Input 5 3 3 1 1 2 1 2 2 2 2 1 1 1 2 3 1 -100000000 -100000000 -100000000 1 1 3 5 3 1 2 3 4 5 1 2 3 2 3 2 1 1 5 3 1 100000000 100000000 100000000 1 1 3 7 7 34 87 5 42 -44 66 -32 2 2 2 4 3 1 1 3 2 6 2 1 5 2 2 2 5 2 6 1 2 Output 5 1 3 -600000000 22 12 55 600000000 171 42 118 66 -108 23 2
1,900
true
false
true
true
true
false
true
false
false
false
760
47B
Problem - 47B - 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 *1200 No tag edit access → Contest materials ") (> or < sign)(letter). For example, if coin "A" proved lighter than coin "B", the result of the weighting is A<B. Output It the results are contradictory, print Impossible. Otherwise, print without spaces the rearrangement of letters «A», «B» and «C» which represent the coins in the increasing order of their weights. Examples Input A>B C<B A>C Output CBA Input A<B B>C C>A Output ACB
1,200
false
false
true
false
false
false
false
false
false
false
9,741
1936E
You are given a permutation $$$p$$$ of length $$$n$$$. Please count the number of permutations $$$q$$$ of length $$$n$$$ which satisfy the following: for each $$$1 le i < n$$$, $$$max(q_1,ldots,q_i) eq max(p_1,ldots,p_i)$$$. Since the answer may be large, output the answer modulo $$$998,244,353$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$). It is guaranteed that $$$p$$$ is a permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the answer modulo $$$998,244,353$$$. Example Input 6 2 2 1 3 1 2 3 3 3 1 2 4 2 4 1 3 5 3 5 1 4 2 15 6 13 2 8 7 11 1 3 9 15 4 5 12 10 14 Output 1 3 2 4 18 424488915 Note In the first test case, $$$p = [2, 1]$$$. The only suitable $$$q$$$ is $$$[1, 2]$$$. Indeed, we need to satisfy the inequality $$$q_1 eq p_1$$$. It only holds for $$$q = [1, 2]$$$. In the second test case, $$$p = [1, 2, 3]$$$. So $$$q$$$ has to satisfy two inequalities: $$$q_1 eq p_1$$$ and $$$max(q_1, q_2) eq max(1, 2) = 2$$$. One can prove that this only holds for the following $$$3$$$ permutations: $$$q = [2, 3, 1]$$$: in this case $$$q_1 = 2 eq 1$$$ and $$$max(q_1, q_2) = 3 eq 2$$$; $$$q = [3, 1, 2]$$$: in this case $$$q_1 = 3 eq 1$$$ and $$$max(q_1, q_2) = 3 eq 2$$$; $$$q = [3, 2, 1]$$$: in this case $$$q_1 = 3 eq 1$$$ and $$$max(q_1, q_2) = 3 eq 2$$$.
3,400
true
false
false
false
false
false
false
false
false
false
664
1992D
ErnKor is ready to do anything for Julen, even to swim through crocodile-infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of $$$1$$$ meter and a length of $$$n$$$ meters. The river is very cold. Therefore, in total (that is, throughout the entire swim from $$$0$$$ to $$$n+1$$$) ErnKor can swim in the water for no more than $$$k$$$ meters. For the sake of humanity, we have added not only crocodiles to the river, but also logs on which he can jump. Our test is as follows: Initially, ErnKor is on the left bank and needs to reach the right bank. They are located at the $$$0$$$ and $$$n+1$$$ meters respectively. The river can be represented as $$$n$$$ segments, each with a length of $$$1$$$ meter. Each segment contains either a log 'L', a crocodile 'C', or just water 'W'. ErnKor can move as follows: If he is on the surface (i.e., on the bank or on a log), he can jump forward for no more than $$$m$$$ ($$$1 le m le 10$$$) meters (he can jump on the bank, on a log, or in the water). If he is in the water, he can only swim to the next river segment (or to the bank if he is at the $$$n$$$-th meter). ErnKor cannot land in a segment with a crocodile in any way. Determine if ErnKor can reach the right bank. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) xa0— the number of test cases. The first line of each test case contains three numbers $$$n, m, k$$$ ($$$0 le k le 2 cdot 10^5$$$, $$$1 le n le 2 cdot 10^5$$$, $$$1 le m le 10$$$)xa0— the length of the river, the distance ErnKor can jump, and the number of meters ErnKor can swim without freezing. The second line of each test case contains a string $$$a$$$ of length $$$n$$$. $$$a_i$$$ denotes the object located at the $$$i$$$-th meter. ($$$a_i in {$$$'W','C','L'$$$}$$$) 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 "YES" if ErnKor can pass the test, and output "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 6 6 2 0 LWLLLW 6 1 1 LWLLLL 6 1 1 LWLLWL 6 2 15 LWLLCC 6 10 0 CCCCCC 6 6 1 WCCCCW Output YES YES NO NO YES YES Note Let's consider examples: First example: We jump from the shore to the first log ($$$0 ightarrow 1$$$), from the first log to the second ($$$1 ightarrow 3$$$), from the second to the fourth ($$$3 ightarrow 5$$$), and from the last log to the shore ($$$5 ightarrow 7$$$). So, we have $$$0 ightarrow 1 ightarrow 3 ightarrow 5 ightarrow 7$$$. Since we did not encounter a crocodile and swam no more than k meters, the answer is «YES». Second example: $$$0 ightarrow 1$$$, we jump into the water from the first log ($$$1 ightarrow 2$$$), swim a cell to the log ($$$2 leadsto 3$$$), $$$3 ightarrow 4 ightarrow 5 ightarrow 6 ightarrow 7$$$. Since we did not encounter a crocodile and swam no more than k meters, the answer is «YES». In the third example, ErnKor needs to swim two cells 'W', but can only swim one. Therefore, the answer is «NO». Sixth example: We jump from the shore into the water ($$$0 ightarrow 6$$$) and swim one cell in the water ($$$6 leadsto 7$$$). Since we did not encounter a crocodile and swam no more than k meters, the answer is «YES».
1,200
false
true
true
true
false
false
false
false
false
false
316
1407C
This is an interactive problem. We hid from you a permutation $$$p$$$ of length $$$n$$$, consisting of the elements from $$$1$$$ to $$$n$$$. You want to guess it. To do that, you can give us 2 different indices $$$i$$$ and $$$j$$$, and we will reply with $$$p_{i} bmod p_{j}$$$ (remainder of division $$$p_{i}$$$ by $$$p_{j}$$$). We have enough patience to answer at most $$$2 cdot n$$$ queries, so you should fit in this constraint. Can you do it? As a reminder, 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 only line of the input contains a single integer $$$n$$$ ($$$1 le n le 10^4$$$) — length of the permutation. Interaction The interaction starts with reading $$$n$$$. Then you are allowed to make at most $$$2 cdot n$$$ queries in the following way: "? x y" ($$$1 le x, y le n, x e y$$$). After each one, you should read an integer $$$k$$$, that equals $$$p_x bmod p_y$$$. When you have guessed the permutation, print a single line "! " (without quotes), followed by array $$$p$$$ and quit. 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. Exit immediately after receiving "-1" and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. Hack format In the first line output $$$n$$$ ($$$1 le n le 10^4$$$). In the second line print the permutation of $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$. Example Output ? 1 2 ? 3 2 ? 1 3 ? 2 1 ! 1 3 2
1,600
true
false
false
false
false
true
false
false
false
false
3,639
40B
Problem - 40B - Codeforces =============== xa0 ") . This process continues ad infinitum. You have to figure out how many squares we repainted exactly _x_ times. The upper left square of the board has to be assumed to be always black. Two squares are called corner-adjacent, if they have exactly one common point. Input The first line contains integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20095000). The second line contains integer _x_ (1u2009≤u2009_x_u2009≤u2009109). Output Print how many squares will be painted exactly _x_ times. Examples Input 3 3 1 Output 4 Input 3 3 2 Output 1 Input 1 1 1 Output 1
1,600
true
false
false
false
false
false
false
false
false
false
9,788
1858C
Alex got a new game called "GCD permutations" as a birthday present. Each round of this game proceeds as follows: First, Alex chooses a permutation$$$^{dagger}$$$ $$$a_1, a_2, ldots, a_n$$$ of integers from $$$1$$$ to $$$n$$$. Then, for each $$$i$$$ from $$$1$$$ to $$$n$$$, an integer $$$d_i = gcd(a_i, a_{(i bmod n) + 1})$$$ is calculated. The score of the round is the number of distinct numbers among $$$d_1, d_2, ldots, d_n$$$. Alex has already played several rounds so he decided to find a permutation $$$a_1, a_2, ldots, a_n$$$ such that its score is as large as possible. Recall that $$$gcd(x, y)$$$ denotes the , and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). 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 containing a single integer $$$n$$$ ($$$2 le n le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print $$$n$$$ distinct integers $$$a_{1},a_{2},ldots,a_{n}$$$ ($$$1 le a_i le n$$$)xa0— the permutation with the largest possible score. If there are several permutations with the maximum possible score, you can print any one of them. Example Output 1 2 4 3 5 1 2 1 2 3 6 4 5 7 1 2 3 4 8 5 10 6 9 7 Note In the first test case, Alex wants to find a permutation of integers from $$$1$$$ to $$$5$$$. For the permutation $$$a=[1,2,4,3,5]$$$, the array $$$d$$$ is equal to $$$[1,2,1,1,1]$$$. It contains $$$2$$$ distinct integers. It can be shown that there is no permutation of length $$$5$$$ with a higher score. In the second test case, Alex wants to find a permutation of integers from $$$1$$$ to $$$2$$$. There are only two such permutations: $$$a=[1,2]$$$ and $$$a=[2,1]$$$. In both cases, the array $$$d$$$ is equal to $$$[1,1]$$$, so both permutations are correct. In the third test case, Alex wants to find a permutation of integers from $$$1$$$ to $$$7$$$. For the permutation $$$a=[1,2,3,6,4,5,7]$$$, the array $$$d$$$ is equal to $$$[1,1,3,2,1,1,1]$$$. It contains $$$3$$$ distinct integers so its score is equal to $$$3$$$. It can be shown that there is no permutation of integers from $$$1$$$ to $$$7$$$ with a score higher than $$$3$$$.
1,000
true
true
false
false
false
true
false
false
false
false
1,135
1401C
You are given an array $$$a_1, a_2, dots, a_n$$$ where all $$$a_i$$$ are integers and greater than $$$0$$$. In one operation, you can choose two different indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$). If $$$gcd(a_i, a_j)$$$ is equal to the minimum element of the whole array $$$a$$$, you can swap $$$a_i$$$ and $$$a_j$$$. $$$gcd(x, y)$$$ denotes the . Determine if you can do this. An array $$$a$$$ is non-decreasing if and only if $$$a_1 le a_2 le ldots le a_n$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of array $$$a$$$. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, ldots a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the array itself. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, output "YES" if it is possible to make the array $$$a$$$ non-decreasing using the described operation, or "NO" if it is impossible to do so. Example Input 4 1 8 6 4 3 6 6 2 9 4 4 5 6 7 5 7 5 2 2 4 Note In the first and third sample, the array is already non-decreasing. In the second sample, we can swap $$$a_1$$$ and $$$a_3$$$ first, and swap $$$a_1$$$ and $$$a_5$$$ second to make the array non-decreasing. In the forth sample, we cannot the array non-decreasing using the operation.
1,300
true
false
false
false
false
true
false
false
true
false
3,663
1857A
You are given an array consisting of $$$n$$$ integers. Your task is to determine whether it is possible to color all its elements in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored. For example, if the array is [$$$1,2,4,3,2,3,5,4$$$], we can color it as follows: [$$$color{blue}{1},color{blue}{2},color{red}{4},color{blue}{3},color{red}{2},color{red}{3},color{red}{5},color{red}{4}$$$], where the sum of the blue elements is $$$6$$$ and the sum of the red elements is $$$18$$$. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Each test case begins with a line containing an integer $$$n$$$ ($$$2 le n le 50$$$)xa0— the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1,a_2, dots, a_n$$$ ($$$1 le a_i le 50$$$)xa0— the elements of the array $$$a$$$. Output For each test case, output "YES" (without quotes) if it is possible to color the array in two colors in such a way that the sums of the elements of both colors have the same parity and each color has at least one element colored, and "NO" otherwise. You can output "Yes" and "No" in any case (for example, the strings "yES", "yes", and "Yes" will be recognized as correct answers). Example Input 7 8 1 2 4 3 2 3 5 4 2 4 7 3 3 9 8 2 1 7 5 5 4 3 2 1 4 4 3 4 5 2 50 48 Output YES NO YES YES NO YES YES Note The first sample is described in the statement. In the second sample, there are only two colorings $$$[color{blue}{4},color{red}{7}]$$$ and $$$[color{red}{4},color{blue}{7}]$$$ , but in both cases the parity of sums is different. In the third sample, you can color $$$[color{blue}{3},color{blue}{9},color{red}{8}]$$$ and $$$12$$$ and $$$8$$$ are both even.
800
true
true
false
false
false
false
false
false
false
false
1,144
1658A
Today, Marin is at a cosplay exhibition and is preparing for a group photoshoot! For the group picture, the cosplayers form a horizontal line. A group picture is considered beautiful if for every contiguous segment of at least $$$2$$$ cosplayers, the number of males does not exceed the number of females (obviously). Currently, the line has $$$n$$$ cosplayers which can be described by a binary string $$$s$$$. The $$$i$$$-th cosplayer is male if $$$s_i = 0$$$ and female if $$$s_i = 1$$$. To ensure that the line is beautiful, you can invite some additional cosplayers (possibly zero) to join the line at any position. You can't remove any cosplayer from the line. Marin wants to know the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. She can't do this on her own, so she's asking you for help. Can you help her? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$) — the number of test cases. The first line of each test case contains a positive integer $$$n$$$ ($$$1 leq n leq 100$$$) — the number of cosplayers in the initial line. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ — describing the cosplayers already in line. Each character of the string is either 0 describing a male, or 1 describing a female. Note that there is no limit on the sum of $$$n$$$. Output For each test case, print the minimum number of cosplayers you need to invite so that the group picture of all the cosplayers is beautiful. Example Input 9 3 000 3 001 3 010 3 011 3 100 3 101 3 110 3 111 19 1010110000100000101 Note In the first test case, for each pair of adjacent cosplayers, you can invite two female cosplayers to stand in between them. Then, $$$000 ightarrow 0110110$$$. In the third test case, you can invite one female cosplayer to stand next to the second cosplayer. Then, $$$010 ightarrow 0110$$$.
800
true
false
true
false
false
true
false
false
false
false
2,339
459D
Problem - 459D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures divide and conquer sortings *1800 No tag edit access → Contest materials ! Parmida has prepared the following test problem for Pashmak. There is a sequence _a_ that consists of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Let's denote _f_(_l_,u2009_r_,u2009_x_) the number of indices _k_ such that: _l_u2009≤u2009_k_u2009≤u2009_r_ and _a__k_u2009=u2009_x_. His task is to calculate the number of pairs of indicies _i_,u2009_j_ (1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_n_) such that _f_(1,u2009_i_,u2009_a__i_)u2009>u2009_f_(_j_,u2009_n_,u2009_a__j_). Help Pashmak with the test. Input The first line of the input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009106). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output Print a single integer — the answer to the problem. Examples Input 7 1 2 1 1 2 2 1 Output 8 Input 3 1 1 1 Output 1 Input 5 1 2 3 4 5 Output 0
1,800
false
false
false
false
true
false
false
false
true
false
8,003
215E
Problem - 215E - 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 number theory *2100 No tag edit access → Contest materials ") ") Tutorial") is a periodic string. Your task is to calculate, how many periodic numbers are in the interval from _l_ to _r_ (both ends are included). Input The single input line contains two integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u20091018). The numbers are separated by a space. 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 a single integer, showing how many periodic numbers are in the interval from _l_ to _r_ (both ends are included). Examples Input 1 10 Output 3 Input 25 38 Output 2 Note In the first sample periodic numbers are 3, 7 and 10. In the second sample periodic numbers are 31 and 36.
2,100
false
false
false
true
false
false
false
false
false
false
8,977
354C
Problem - 354C - 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 number theory *2100 No tag edit access → Contest materials . Unfortunately, the shop has only one array _a_ left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by _k_ for each number). The seller can obtain array _b_ from array _a_ if the following conditions hold: _b__i_u2009>u20090;u20020u2009≤u2009_a__i_u2009-u2009_b__i_u2009≤u2009_k_ for all 1u2009≤u2009_i_u2009≤u2009_n_. Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain). Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20093·105;u20091u2009≤u2009_k_u2009≤u2009106). The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106) — array _a_. Output In the single line print a single number — the maximum possible beauty of the resulting array. Examples Input 6 1 3 6 10 12 13 16 Output 3 Input 5 3 8 21 52 15 77 Output 7 Note In the first sample we can obtain the array: 3u20026u20029u200212u200212u200215 In the second sample we can obtain the next array: 7u200221u200249u200214u200277
2,100
false
false
false
true
false
false
true
false
false
false
8,416
1142E
This is an interactive task. Scientists are about to invent a new optimization for the Floyd-Warshall algorithm, which will allow it to work in linear time. There is only one part of the optimization still unfinished. It is well known that the Floyd-Warshall algorithm takes a graph with $$$n$$$ nodes and exactly one edge between each pair of nodes. The scientists have this graph, what is more, they have directed each edge in one of the two possible directions. To optimize the algorithm, exactly $$$m$$$ edges are colored in pink color and all the rest are colored in green. You know the direction of all $$$m$$$ pink edges, but the direction of green edges is unknown to you. In one query you can ask the scientists about the direction of exactly one green edge, however, you can perform at most $$$2 cdot n$$$ such queries. Your task is to find the node from which every other node can be reached by a path consisting of edges of same color. Be aware that the scientists may have lied that they had fixed the direction of all edges beforehand, so their answers may depend on your queries. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 100,000$$$, $$$0 le m le 100,000$$$)xa0— the number of nodes and the number of pink edges. The next $$$m$$$ lines describe the pink edges, the $$$i$$$-th of these lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$)xa0— the start and the end of the $$$i$$$-th pink edge. It is guaranteed, that all unordered pairs $$$(u_i, v_i)$$$ are distinct. Output When you found the answer, print "!" and the number of the node from which every other node can be reached by a single-colored path. Interaction To ask the direction of the green edge between nodes $$$a$$$ and $$$b$$$, print "?", $$$a$$$ and $$$b$$$ in single line, separated by the space characters. In answer to this read a single integer, which will be $$$1$$$ if the edge is directed from $$$a$$$ to $$$b$$$ and $$$0$$$ if the edge is directed from $$$b$$$ to $$$a$$$. You can ask at most $$$2 cdot n$$$ queries, otherwise you will get Wrong Answer. After printing a query do not forget to output end of line and flush the output. Otherwise you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Answer $$$-1$$$ instead of $$$0$$$ or $$$1$$$ means that you made an invalid query or exceeded the query limit. Exit immediately after receiving $$$-1$$$ and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream. Hacks Hacks should be formatted as follows. The first line should contain two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 300$$$, $$$0 le m le n cdot (n - 1) / 2$$$)xa0— the number of nodes and number of pink edges. The next $$$m$$$ lines should describe the pink edges, the $$$i$$$-th line should contain 2 integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i, b_i le n$$$, $$$a_i e b_i$$$), which means that there is a pink edge from $$$a_i$$$ to $$$b_i$$$. All unordered pairs $$$(a_i, b_i)$$$ should be distinct. The next $$$(n cdot (n - 1) / 2 - m)$$$ lines should describe the green edges, the $$$i$$$-th line should contain two integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i, b_i le n$$$, $$$a_i e b_i$$$)), which means that there is a green edge from $$$a_i$$$ to $$$b_i$$$. All unordered pairs of $$$(a_i, b_i)$$$ should be distinct and should also be different from the pairs for the pink edges. Example Output ? 1 3 ? 4 2 ? 3 2 ! 3 Note In the example above the answer for the query "? 1 3" is 0, so the edge is directed from 3 to 1. The answer for the query "? 4 2" is 1, so the edge is directed from 4 to 2. The answer for the query "? 3 2" is 1, so the edge is directed from 3 to 2. So there are green paths from node 3 to nodes 1 and 2 and there is a pink path from node 3 to node 4.
3,200
false
false
false
false
false
false
false
false
false
true
5,009
353D
There are _n_ schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes. Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the _i_-th position has a boy and the (_i_u2009+u20091)-th position has a girl, then in a second, the _i_-th position will have a girl and the (_i_u2009+u20091)-th one will have a boy. Let's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more. Your task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing. Input The first line contains a sequence of letters without spaces _s_1_s_2... _s__n_ (1u2009≤u2009_n_u2009≤u2009106), consisting of capital English letters M and F. If letter _s__i_ equals M, that means that initially, the line had a boy on the _i_-th position. If letter _s__i_ equals F, then initially the line had a girl on the _i_-th position. Note In the first test case the sequence of changes looks as follows: MFM u2009→u2009 FMM. The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF u2009→u2009 MFMF u2009→u2009 FMFM u2009→u2009 FFMM.
2,000
false
false
false
true
false
true
false
false
false
false
8,420
268D
Manao is working for a construction company. Recently, an order came to build wall bars in a children's park. Manao was commissioned to develop a plan of construction, which will enable the company to save the most money. After reviewing the formal specifications for the wall bars, Manao discovered a number of controversial requirements and decided to treat them to the company's advantage. His resulting design can be described as follows: Let's introduce some unit of length. The construction center is a pole of height _n_. At heights 1,u20092,u2009...,u2009_n_ exactly one horizontal bar sticks out from the pole. Each bar sticks in one of four pre-fixed directions. A child can move from one bar to another if the distance between them does not exceed _h_ and they stick in the same direction. If a child is on the ground, he can climb onto any of the bars at height between 1 and _h_. In Manao's construction a child should be able to reach at least one of the bars at heights _n_u2009-u2009_h_u2009+u20091,u2009_n_u2009-u2009_h_u2009+u20092,u2009...,u2009_n_ if he begins at the ground. The figure to the left shows what a common set of wall bars looks like. The figure to the right shows Manao's construction Manao is wondering how many distinct construction designs that satisfy his requirements exist. As this number can be rather large, print the remainder after dividing it by 1000000009xa0(109u2009+u20099). Two designs are considered distinct if there is such height _i_, that the bars on the height _i_ in these designs don't stick out in the same direction. Input A single line contains two space-separated integers, _n_ and _h_ (1u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_h_u2009≤u2009_min_(_n_,u200930)). Output In a single line print the remainder after dividing the number of designs by 1000000009xa0(109u2009+u20099). Note Consider several designs for _h_u2009=u20092. A design with the first bar sticked out in direction _d_1, the second — in direction _d_2 and so on (1u2009≤u2009_d__i_u2009≤u20094) is denoted as string _d_1_d_2..._d__n_. Design "1231" (the first three bars are sticked out in different directions, the last one — in the same as first). A child can reach neither the bar at height 3 nor the bar at height 4. Design "414141". A child can reach the bar at height 5. To do this, he should first climb at the first bar, then at the third and then at the fifth one. He can also reach bar at height 6 by the route second u2009→u2009 fourth u2009→u2009 sixth bars. Design "123333". The child can't reach the upper two bars. Design "323323". The bar at height 6 can be reached by the following route: first u2009→u2009 third u2009→u2009 fourth u2009→u2009 sixth bars.
2,300
false
false
false
true
false
false
false
false
false
false
8,760
910C
Petya has _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_. His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'. Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091u2009000) — the number of Petya's numbers. Each of the following lines contains non-empty string _s__i_ consisting of lowercase Latin letters from 'a' to 'j' — the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters. Output Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests. Examples Input 5 abcdef ghij bdef accbd g Note In the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10,u200923,u200914]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration. In the second example the numbers after the restoration can look like: [120468,u20093579,u20092468,u200910024,u20093]. In the second example the numbers after the restoration can look like: [11,u200922,u200911].
1,700
true
true
false
false
false
true
false
false
false
false
6,104
547E
What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears! In that country there is a rock band called CF consisting of _n_ bears (including Mike) numbered from 1 to _n_. Phone number of _i_-th member of CF is _s__i_. May 17th is a holiday named Phone Calls day. In the last Phone Calls day, everyone called all the numbers that are substrings of his/her number (one may call some number several times). In particular, everyone called himself (that was really strange country). Denote as _call_(_i_,u2009_j_) the number of times that _i_-th member of CF called the _j_-th member of CF. The geek Mike has _q_ questions that he wants to ask you. In each question he gives you numbers _l_,u2009_r_ and _k_ and you should tell him the number Input The first line of input contains integers _n_ and _q_ (1u2009≤u2009_n_u2009≤u20092u2009×u2009105 and 1u2009≤u2009_q_u2009≤u20095u2009×u2009105). The next _n_ lines contain the phone numbers, _i_-th line contains a string _s__i_ consisting of lowercase English letters (). The next _q_ lines contain the information about the questions, each of them contains integers _l_,u2009_r_ and _k_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_ and 1u2009≤u2009_k_u2009≤u2009_n_). Output Print the answer for each question in a separate line. Examples Input 5 5 a ab abab ababab b 1 5 1 3 5 1 1 5 2 1 5 3 1 4 5
2,800
false
false
false
false
true
false
false
false
false
false
7,648
1085C
The Squareland national forest is divided into equal $$$1 imes 1$$$ square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates $$$(x, y)$$$ of its south-west corner. Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of land $$$A, B, C$$$ in the forest. Initially, all plots in the forest (including the plots $$$A, B, C$$$) are covered by trees. The friends want to visit each other, so they want to clean some of the plots from trees. After cleaning, one should be able to reach any of the plots $$$A, B, C$$$ from any other one of those by moving through adjacent cleared plots. Two plots are adjacent if they share a side. For example, $$$A=(0,0)$$$, $$$B=(1,1)$$$, $$$C=(2,2)$$$. The minimal number of plots to be cleared is $$$5$$$. One of the ways to do it is shown with the gray color. Of course, the friends don't want to strain too much. Help them find out the smallest number of plots they need to clean from trees. Input The first line contains two integers $$$x_A$$$ and $$$y_A$$$xa0— coordinates of the plot $$$A$$$ ($$$0 leq x_A, y_A leq 1000$$$). The following two lines describe coordinates $$$(x_B, y_B)$$$ and $$$(x_C, y_C)$$$ of plots $$$B$$$ and $$$C$$$ respectively in the same format ($$$0 leq x_B, y_B, x_C, y_C leq 1000$$$). It is guaranteed that all three plots are distinct. Output On the first line print a single integer $$$k$$$xa0— the smallest number of plots needed to be cleaned from trees. The following $$$k$$$ lines should contain coordinates of all plots needed to be cleaned. All $$$k$$$ plots should be distinct. You can output the plots in any order. If there are multiple solutions, print any of them. Examples Output 5 0 0 1 0 1 1 1 2 2 2 Note The first example is shown on the picture in the legend. The second example is illustrated with the following image:
1,600
true
false
true
false
false
false
false
false
false
false
5,297
2003A
Turtle thinks a string $$$s$$$ is a good string if there exists a sequence of strings $$$t_1, t_2, ldots, t_k$$$ ($$$k$$$ is an arbitrary integer) such that: $$$k ge 2$$$. $$$s = t_1 + t_2 + ldots + t_k$$$, where $$$+$$$ represents the concatenation operation. For example, $$$ exttt{abc} = exttt{a} + exttt{bc}$$$. For all $$$1 le i < j le k$$$, the first character of $$$t_i$$$ isn't equal to the last character of $$$t_j$$$. Turtle is given a string $$$s$$$ consisting of lowercase Latin letters. Please tell him whether the string $$$s$$$ is a good string! Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 100$$$) — the length of the string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters. Output For each test case, output "YES" if the string $$$s$$$ is a good string, and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 4 2 aa 3 aba 4 abcb 12 abcabcabcabc Note In the first test case, the sequence of strings $$$ exttt{a}, exttt{a}$$$ satisfies the condition $$$s = t_1 + t_2 + ldots + t_k$$$, but the first character of $$$t_1$$$ is equal to the last character of $$$t_2$$$. It can be seen that there doesn't exist any sequence of strings which satisfies all of the conditions, so the answer is "NO". In the third test case, the sequence of strings $$$ exttt{ab}, exttt{cb}$$$ satisfies all of the conditions. In the fourth test case, the sequence of strings $$$ exttt{abca}, exttt{bcab}, exttt{cabc}$$$ satisfies all of the conditions.
800
false
true
false
false
false
false
false
false
false
false
239