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
47A
Problem - 47A - 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 *800 No tag edit access → Contest materials ") — the given integer. Output If the given integer is a triangular number output YES, otherwise output NO. Examples Input 1 Output YES Input 2 Output NO Input 3 Output YES
800
true
false
false
false
false
false
true
false
false
false
9,742
1979F
This is an interactive problem. Kostyanych has chosen a complete undirected graph$$$^{dagger}$$$ with $$$n$$$ vertices, and then removed exactly $$$(n - 2)$$$ edges from it. You can ask queries of the following type: "? $$$d$$$" — Kostyanych tells you the number of vertex $$$v$$$ with a degree at least $$$d$$$. Among all possible such vertices, he selects the vertex with the minimum degree, and if there are several such vertices, he selects the one with the minimum number. He also tells you the number of another vertex in the graph, with which $$$v$$$ is not connected by an edge (if none is found, then $$$0$$$ is reported). Among all possible such vertices, he selects the one with the minimum number. Then he removes the vertex $$$v$$$ and all edges coming out of it. If the required vertex $$$v$$$ is not found, then "$$$0 0$$$" is reported. Find a Hamiltonian path$$$^{ddagger}$$$ in the original graph in at most $$$n$$$ queries. It can be proven that under these constraints, a Hamiltonian path always exists. $$$^{dagger}$$$A complete undirected graph is a graph in which there is exactly one undirected edge between any pair of distinct vertices. Thus, a complete undirected graph with $$$n$$$ vertices contains $$$frac{n(n-1)}{2}$$$ edges. $$$^{ddagger}$$$A Hamiltonian path in a graph is a path that passes through each vertex exactly once. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$) — the number of vertices in the graph. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Interaction Interaction for each test case begins with reading the integer $$$n$$$. Then you can make no more than $$$n$$$ queries. To make a query, output a line in the format "? $$$d$$$" (without quotes) ($$$0 le d le n - 1$$$). After each query, read two integersxa0— the answer to your query. When you are ready to report the answer, output a line in the format "! $$$v_1 v_2 ldots v_n$$$" (without quotes)xa0— the vertices in the order of their occurrence in the Hamiltonian path. Outputting the answer does not count as one of the $$$n$$$ queries. After solving one test case, the program should immediately move on to the next one. After solving all test cases, the program should be terminated immediately. If your program makes more than $$$n$$$ queries for one test case or makes an incorrect query, then the response to the query will be $$$-1$$$, and after receiving such a response, your program should immediately terminate to receive the verdict Wrong answer. Otherwise, it may receive any other verdict. After outputting a query, do not forget to output an end of line and flush the output buffer. Otherwise, you will receive the verdict 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. The interactor is non-adaptive. The graph does not change during the interaction. Hacks To hack, use the following format: The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$) — the number of vertices in the graph. Each of the following $$$(n - 2)$$$ lines should contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$) — ends of the edge that was removed from the graph. Each edge must not occur more than once. The sum of $$$n$$$ over all test cases should not exceed $$$10^5$$$. Note In the first test case, the original graph looks as follows: Consider the queries: There are no vertices with a degree of at least $$$3$$$ in the graph, so "$$$0 0$$$" is reported. There are four vertices with a degree of at least $$$2$$$, and all of them have a degree of exactly $$$2$$$: $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$. Vertex $$$1$$$ is reported, because it has the minimum number, and vertex $$$4$$$ is reported, because it is the only one not connected to vertex $$$1$$$. After this, vertex $$$1$$$ is removed from the graph. There are three vertices with a degree of at least $$$1$$$, among them vertices $$$2$$$ and $$$3$$$ have a minimum degree of $$$1$$$ (vertex $$$4$$$ has a degree of $$$2$$$). Vertex $$$2$$$ is reported, because it has the minimum number, and vertex $$$3$$$ is reported, because it is the only one not connected to vertex $$$2$$$. After this, vertex $$$2$$$ is removed from the graph. The path $$$4 - 3 - 1 - 2$$$ is a Hamiltonian path. In the second test case, the original graph looks as follows: Consider the queries: Vertex $$$1$$$ has a degree of at least $$$3$$$, but it is connected to all vertices, so "$$$1 0$$$" is reported. After this, vertex $$$1$$$ is removed from the graph. The remaining vertices $$$2$$$, $$$3$$$, and $$$4$$$ have a degree of at least $$$0$$$, but among them vertex $$$4$$$ has the minimum degree of $$$0$$$ (vertices $$$2$$$ and $$$3$$$ have a degree of $$$1$$$). Vertex $$$4$$$ is not connected to both vertices $$$2$$$ and $$$3$$$, so vertex $$$2$$$ is reported (as it has the minimum number). After this, vertex $$$4$$$ is removed from the graph. The path $$$4 - 1 - 2 - 3$$$ is a Hamiltonian path. In the third test case, the graph consists of $$$2$$$ vertices connected by an edge.
2,900
false
false
false
false
false
true
true
false
false
true
411
500D
New Year is coming in Tree World! In this world, as the name implies, there are _n_ cities connected by _n_u2009-u20091 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to _n_, and the roads are numbered by integers from 1 to _n_u2009-u20091. Let's define _d_(_u_,u2009_v_) as total length of roads on the path between city _u_ and city _v_. As an annual event, people in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that in the _i_-th year, the length of the _r__i_-th road is going to become _w__i_, which is shorter than its length before. Assume that the current year is year 1. Three Santas are planning to give presents annually to all the children in Tree World. In order to do that, they need some preparation, so they are going to choose three distinct cities _c_1, _c_2, _c_3 and make exactly one warehouse in each city. The _k_-th (1u2009≤u2009_k_u2009≤u20093) Santa will take charge of the warehouse in city _c__k_. It is really boring for the three Santas to keep a warehouse alone. So, they decided to build an only-for-Santa network! The cost needed to build this network equals to _d_(_c_1,u2009_c_2)u2009+u2009_d_(_c_2,u2009_c_3)u2009+u2009_d_(_c_3,u2009_c_1) dollars. Santas are too busy to find the best place, so they decided to choose _c_1,u2009_c_2,u2009_c_3 randomly uniformly over all triples of distinct numbers from 1 to _n_. Santas would like to know the expected value of the cost needed to build the network. However, as mentioned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value. Input The first line contains an integer _n_ (3u2009≤u2009_n_u2009≤u2009105) — the number of cities in Tree World. Next _n_u2009-u20091 lines describe the roads. The _i_-th line of them (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091) contains three space-separated integers _a__i_, _b__i_, _l__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_, 1u2009≤u2009_l__i_u2009≤u2009103), denoting that the _i_-th road connects cities _a__i_ and _b__i_, and the length of _i_-th road is _l__i_. The next line contains an integer _q_ (1u2009≤u2009_q_u2009≤u2009105) — the number of road length changes. Next _q_ lines describe the length changes. The _j_-th line of them (1u2009≤u2009_j_u2009≤u2009_q_) contains two space-separated integers _r__j_, _w__j_ (1u2009≤u2009_r__j_u2009≤u2009_n_u2009-u20091, 1u2009≤u2009_w__j_u2009≤u2009103). It means that in the _j_-th repair, the length of the _r__j_-th road becomes _w__j_. It is guaranteed that _w__j_ is smaller than the current length of the _r__j_-th road. The same road can be repaired several times. Output Output _q_ numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10u2009-u20096. Examples Input 3 2 3 5 1 3 3 5 1 4 2 2 1 2 2 1 1 1 Output 14.0000000000 12.0000000000 8.0000000000 6.0000000000 4.0000000000 Input 6 1 5 3 5 3 2 6 1 7 1 4 4 5 2 3 5 1 2 2 1 3 5 4 1 5 2 Output 19.6000000000 18.6000000000 16.6000000000 13.6000000000 12.6000000000 Note Consider the first sample. There are 6 triples: (1,u20092,u20093),u2009(1,u20093,u20092),u2009(2,u20091,u20093),u2009(2,u20093,u20091),u2009(3,u20091,u20092),u2009(3,u20092,u20091). Because _n_u2009=u20093, the cost needed to build the network is always _d_(1,u20092)u2009+u2009_d_(2,u20093)u2009+u2009_d_(3,u20091) for all the triples. So, the expected cost equals to _d_(1,u20092)u2009+u2009_d_(2,u20093)u2009+u2009_d_(3,u20091).
1,900
false
false
false
false
false
false
false
false
false
true
7,834
38B
Problem - 38B - Codeforces =============== xa0 - Codeforces Beta Round 38 (ACM-ICPC Rules)]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation math *1200 No tag edit access → Contest materials - Codeforces Beta Round #38 (ACM-ICPC Rules)") ") ") ") ") ") ") ")
1,200
true
false
true
false
false
false
true
false
false
false
9,807
1176B
You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, dots , a_n$$$. In one operation you can choose two elements of the array and replace them with the element equal to their sum (it does not matter where you insert the new element). For example, from the array $$$[2, 1, 4]$$$ you can obtain the following arrays: $$$[3, 4]$$$, $$$[1, 6]$$$ and $$$[2, 5]$$$. Your task is to find the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing this operation an arbitrary (possibly, zero) number of times. You have to answer $$$t$$$ independent queries. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 le n le 100$$$). The second line of each query contains $$$n$$$ integers $$$a_1, a_2, dots , a_n$$$ ($$$1 le a_i le 10^9$$$). Output For each query print one integer in a single line — the maximum possible number of elements divisible by $$$3$$$ that are in the array after performing described operation an arbitrary (possibly, zero) number of times. Example Input 2 5 3 1 2 3 1 7 1 1 1 1 1 2 2 Note In the first query of the example you can apply the following sequence of operations to obtain $$$3$$$ elements divisible by $$$3$$$: $$$[3, 1, 2, 3, 1] ightarrow [3, 3, 3, 1]$$$. In the second query you can obtain $$$3$$$ elements divisible by $$$3$$$ with the following sequence of operations: $$$[1, 1, 1, 1, 1, 2, 2] ightarrow [1, 1, 1, 1, 2, 3] ightarrow [1, 1, 1, 3, 3] ightarrow [2, 1, 3, 3] ightarrow [3, 3, 3]$$$.
1,100
true
false
false
false
false
false
false
false
false
false
4,837
1804C
While James is gone on business, Vesper takes her time and explores what the legendary Casino Royale has to offer to people who are fond of competitive programming. Her attention was grabbed by the very new "Pull Your Luck" roulette which functions in a pretty peculiar way. The roulette's wheel consists of $$$n$$$ sectors number from $$$0$$$ to $$$n - 1$$$. There is no ball and the winning sector is determined by a static arrow pointing to one of the sectors. Sectors' indexes go in the natural order and the wheel always spins in the direction of indexes increment. That means that sector $$$i + 1$$$ goes right after sector $$$i$$$ for all $$$i$$$ from $$$0$$$ to $$$n - 2$$$, and sector $$$0$$$ goes right after sector $$$n - 1$$$. After a bet is made, the player is allowed to pull the triggering handle herself and cause the wheel to spin. If the player's initial pull is made with the force equal to positive integer $$$f$$$, the wheel will spin for $$$f$$$ seconds. During the first second it will advance $$$f$$$ sectors, the next second it will advance $$$f - 1$$$ sectors, then $$$f - 2$$$ sectors, and so on until it comes to a complete stop. After the wheel comes to a complete stop, the sector which the arrow is pointing to is the winning one. The roulette's arrow currently points at sector $$$x$$$. Vesper knows that she can pull the handle with any integer force from $$$1$$$ to $$$p$$$ inclusive. Note that it is not allowed to pull the handle with force $$$0$$$, i.xa0e. not pull it all. The biggest prize is awarded if the winning sector is $$$0$$$. Now Vesper wonders if she can make sector $$$0$$$ win by pulling the triggering handle exactly once? Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. Then follow $$$t$$$ lines containing one test description each. Each test description consists of three integers $$$n$$$, $$$x$$$ and $$$p$$$ ($$$3 leq n leq 10^5$$$, $$$0 leq x < n$$$, $$$1 leq p leq 10^9$$$). They are the number of sectors on the wheel, the current sector the arrow points at, and the maximum force that Vesper can pull the handle with, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output Print $$$t$$$ lines, the $$$i$$$-th line should contain the answer for the $$$i$$$-th test case. If it is possible to pull the handle with the integer force from $$$1$$$ to $$$p$$$ in order to make sector $$$0$$$ win, print "Yes". Otherwise, print "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 7 5 2 1 5 2 2 10 0 100 11 7 100 3 1 1000 31 0 10 100 49 7 Output No Yes Yes Yes No No No Note In the first example, the only possible way to pull the handle is with force $$$1$$$. That is not enough to make the arrow point at sector $$$0$$$, at least force $$$2$$$ is required to do so. In the second example, Vesper can pull the handle with the force $$$2$$$ so the wheel will spin $$$2 + 1 = 3$$$ sectors ahead and the arrow will point at sector $$$0$$$. In the third example, Vesper can pull the handle with the force $$$4$$$ so the wheel will spin $$$4 + 3 + 2 + 1 = 10$$$ sectors and will point at sector $$$0$$$ again. In the fourth example, Vesper can pull the handle with the force $$$5$$$ so the wheel will spin $$$5 + 4 + 3 + 2 + 1 = 15$$$ sectors. That will make the wheel make one full turn plus $$$4$$$ more sectors. In the fifth example, whatever force Vesper chooses to pull the handle with, she can only make sectors $$$1$$$ and $$$2$$$ win.
1,500
true
true
false
false
false
false
true
false
false
false
1,455
95B
Problem - 95B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp greedy *1800 No tag edit access → Contest materials . This number doesn't have leading zeroes. Output Output the least super lucky number that is more than or equal to _n_. Examples Input 4500 Output 4747 Input 47 Output 47
1,800
false
true
false
true
false
false
false
false
false
false
9,507
832C
_n_ people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-negative integer coordinate, and blow it up. At this moment all people will start running with their maximum speed in the direction they are facing. Also, two strange rays will start propagating from the bomb with speed _s_: one to the right, and one to the left. Of course, the speed _s_ is strictly greater than people's maximum speed. The rays are strange because if at any moment the position and the direction of movement of some ray and some person coincide, then the speed of the person immediately increases by the speed of the ray. You need to place the bomb is such a point that the minimum time moment in which there is a person that has run through point 0, and there is a person that has run through point 106, is as small as possible. In other words, find the minimum time moment _t_ such that there is a point you can place the bomb to so that at time moment _t_ some person has run through 0, and some person has run through point 106. Input The first line contains two integers _n_ and _s_ (2u2009≤u2009_n_u2009≤u2009105, 2u2009≤u2009_s_u2009≤u2009106)xa0— the number of people and the rays' speed. The next _n_ lines contain the description of people. The _i_-th of these lines contains three integers _x__i_, _v__i_ and _t__i_ (0u2009<u2009_x__i_u2009<u2009106, 1u2009≤u2009_v__i_u2009<u2009_s_, 1u2009≤u2009_t__i_u2009≤u20092)xa0— the coordinate of the _i_-th person on the line, his maximum speed and the direction he will run to (1 is to the left, i.e. in the direction of coordinate decrease, 2 is to the right, i.e. in the direction of coordinate increase), respectively. It is guaranteed that the points 0 and 106 will be reached independently of the bomb's position. Output Print the minimum time needed for both points 0 and 106 to be reached. Your answer is considered correct if its absolute or relative error doesn't exceed 10u2009-u20096. Namely, if your answer is _a_, and the jury's answer is _b_, then your answer is accepted, if . Examples Input 2 999 400000 1 2 500000 1 1 Output 500000.000000000000000000000000000000 Input 2 1000 400000 500 1 600000 500 2 Output 400.000000000000000000000000000000 Note In the first example, it is optimal to place the bomb at a point with a coordinate of 400000. Then at time 0, the speed of the first person becomes 1000 and he reaches the point 106 at the time 600. The bomb will not affect on the second person, and he will reach the 0 point at the time 500000. In the second example, it is optimal to place the bomb at the point 500000. The rays will catch up with both people at the time 200. At this time moment, the first is at the point with a coordinate of 300000, and the second is at the point with a coordinate of 700000. Their speed will become 1500 and at the time 400 they will simultaneously run through points 0 and 106.
2,500
true
false
true
false
false
false
false
true
false
false
6,437
1067A
Ivan unexpectedly saw a present from one of his previous birthdays. It is array of $$$n$$$ numbers from $$$1$$$ to $$$200$$$. Array is old and some numbers are hard to read. Ivan remembers that for all elements at least one of its neighbours ls not less than it, more formally: $$$a_{1} le a_{2}$$$, $$$a_{n} le a_{n-1}$$$ and $$$a_{i} le max(a_{i-1}, ,, a_{i+1})$$$ for all $$$i$$$ from $$$2$$$ to $$$n-1$$$. Ivan does not remember the array and asks to find the number of ways to restore it. Restored elements also should be integers from $$$1$$$ to $$$200$$$. Since the number of ways can be big, print it modulo $$$998244353$$$. Input First line of input contains one integer $$$n$$$ ($$$2 le n le 10^{5}$$$)xa0— size of the array. Second line of input contains $$$n$$$ integers $$$a_{i}$$$xa0— elements of array. Either $$$a_{i} = -1$$$ or $$$1 le a_{i} le 200$$$. $$$a_{i} = -1$$$ means that $$$i$$$-th element can't be read. Output Print number of ways to restore the array modulo $$$998244353$$$. Note In the first example, only possible value of $$$a_{2}$$$ is $$$2$$$. In the second example, $$$a_{1} = a_{2}$$$ so there are $$$200$$$ different values because all restored elements should be integers between $$$1$$$ and $$$200$$$.
1,900
false
false
false
true
false
false
false
false
false
false
5,375
1512D
You are given a number $$$n$$$ and an array $$$b_1, b_2, ldots, b_{n+2}$$$, obtained according to the following algorithm: some array $$$a_1, a_2, ldots, a_n$$$ was guessed; array $$$a$$$ was written to array $$$b$$$, i.e. $$$b_i = a_i$$$ ($$$1 le i le n$$$); The $$$(n+1)$$$-th element of the array $$$b$$$ is the sum of the numbers in the array $$$a$$$, i.e. $$$b_{n+1} = a_1+a_2+ldots+a_n$$$; The $$$(n+2)$$$-th element of the array $$$b$$$ was written some number $$$x$$$ ($$$1 le x le 10^9$$$), i.e. $$$b_{n+2} = x$$$; The array $$$b$$$ was shuffled. For example, the array $$$b=[2, 3, 7, 12 ,2]$$$ it could be obtained in the following ways: $$$a=[2, 2, 3]$$$ and $$$x=12$$$; $$$a=[3, 2, 7]$$$ and $$$x=2$$$. For the given array $$$b$$$, find any array $$$a$$$ that could have been guessed initially. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second row of each test case contains $$$n+2$$$ integers $$$b_1, b_2, ldots, b_{n+2}$$$ ($$$1 le b_i le 10^9$$$). 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: "-1", if the array $$$b$$$ could not be obtained from any array $$$a$$$; $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$, otherwise. If there are several arrays of $$$a$$$, you can output any. Example Input 4 3 2 3 7 12 2 4 9 1 7 1 6 5 5 18 2 2 3 2 9 2 3 2 6 9 2 1 Output 2 3 7 -1 2 2 2 3 9 1 2 6
1,200
false
true
false
false
true
true
false
false
false
false
3,106
899D
There are _n_ shovels in Polycarp's shop. The _i_-th shovel costs _i_ burles, that is, the first shovel costs 1 burle, the second shovel costs 2 burles, the third shovel costs 3 burles, and so on. Polycarps wants to sell shovels in pairs. Visitors are more likely to buy a pair of shovels if their total cost ends with several 9s. Because of this, Polycarp wants to choose a pair of shovels to sell in such a way that the sum of their costs ends with maximum possible number of nines. For example, if he chooses shovels with costs 12345 and 37454, their total cost is 49799, it ends with two nines. You are to compute the number of pairs of shovels such that their total cost ends with maximum possible number of nines. Two pairs are considered different if there is a shovel presented in one pair, but not in the other. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009109) — the number of shovels in Polycarp's shop. Output Print the number of pairs of shovels such that their total cost ends with maximum possible number of nines. Note that it is possible that the largest number of 9s at the end is 0, then you should count all such ways. It is guaranteed that for every _n_u2009≤u2009109 the answer doesn't exceed 2·109. Note In the first example the maximum possible number of nines at the end is one. Polycarp cah choose the following pairs of shovels for that purpose: 2 and 7; 3 and 6; 4 and 5. In the second example the maximum number of nines at the end of total cost of two shovels is one. The following pairs of shovels suit Polycarp: 1 and 8; 2 and 7; 3 and 6; 4 and 5; 5 and 14; 6 and 13; 7 and 12; 8 and 11; 9 and 10. In the third example it is necessary to choose shovels 49 and 50, because the sum of their cost is 99, that means that the total number of nines is equal to two, which is maximum possible for _n_u2009=u200950.
1,800
true
false
false
false
false
true
false
false
false
false
6,149
1336E1
This is the easy version of the problem. The only difference between easy and hard versions is the constraint of $$$m$$$. You can make hacks only if both versions are solved. Chiori loves dolls and now she is going to decorate her bedroom! xa0As a doll collector, Chiori has got $$$n$$$ dolls. The $$$i$$$-th doll has a non-negative integer value $$$a_i$$$ ($$$a_i < 2^m$$$, $$$m$$$ is given). Chiori wants to pick some (maybe zero) dolls for the decoration, so there are $$$2^n$$$ different picking ways. Let $$$x$$$ be the bitwise-xor-sum of values of dolls Chiori picks (in case Chiori picks no dolls $$$x = 0$$$). The value of this picking way is equal to the number of $$$1$$$-bits in the binary representation of $$$x$$$. More formally, it is also equal to the number of indices $$$0 leq i < m$$$, such that $$$leftlfloor frac{x}{2^i} ight floor$$$ is odd. Tell her the number of picking ways with value $$$i$$$ for each integer $$$i$$$ from $$$0$$$ to $$$m$$$. Due to the answers can be very huge, print them by modulo $$$998,244,353$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le m le 35$$$) xa0— the number of dolls and the maximum value of the picking way. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < 2^m$$$) xa0— the values of dolls. Output Print $$$m+1$$$ integers $$$p_0, p_1, ldots, p_m$$$ xa0— $$$p_i$$$ is equal to the number of picking ways with value $$$i$$$ by modulo $$$998,244,353$$$. Examples Input 6 7 11 45 14 9 19 81 Output 1 2 11 20 15 10 5 0
2,700
true
false
false
false
false
false
true
false
false
false
4,019
1402A
Everybody knows that Balázs has the fanciest fence in the whole town. It's built up from $$$N$$$ fancy sections. The sections are rectangles standing closely next to each other on the ground. The $$$i$$$th section has integer height $$$h_i$$$ and integer width $$$w_i$$$. We are looking for fancy rectangles on this fancy fence. A rectangle is fancy if: its sides are either horizontal or vertical and have integer lengths the distance between the rectangle and the ground is integer the distance between the rectangle and the left side of the first section is integer it's lying completely on sections What is the number of fancy rectangles? This number can be very big, so we are interested in it modulo $$$10^9+7$$$. Input The first line contains $$$N$$$ ($$$1leq N leq 10^{5}$$$) – the number of sections. The second line contains $$$N$$$ space-separated integers, the $$$i$$$th number is $$$h_i$$$ ($$$1 leq h_i leq 10^{9}$$$). The third line contains $$$N$$$ space-separated integers, the $$$i$$$th number is $$$w_i$$$ ($$$1 leq w_i leq 10^{9}$$$). Output You should print a single integer, the number of fancy rectangles modulo $$$10^9+7$$$. So the output range is $$$0,1,2,ldots, 10^9+6$$$. Scoring $$$ begin{array}{ccc} hline ext{Subtask} & ext{Points} & ext{Constraints} hline 1 & 0 & ext{sample} hline 2 & 12 & N leq 50 : ext{and} : h_i leq 50 : ext{and} : w_i = 1 : ext{for all} : i hline 3 & 13 & h_i = 1 : ext{or} : h_i = 2 : ext{for all} : i hline 4 & 15 & ext{all} : h_i : ext{are equal} hline 5 & 15 & h_i leq h_{i+1} : ext{for all} : i leq N-1 hline 6 & 18 & N leq 1000 hline 7 & 27 & ext{no additional constraints} hline end{array} $$$ Note The fence looks like this: There are 5 fancy rectangles of shape: There are 3 fancy rectangles of shape: There is 1 fancy rectangle of shape: There are 2 fancy rectangles of shape: There is 1 fancy rectangle of shape:
1,800
true
false
true
false
true
false
false
false
true
false
3,659
454A
Problem - 454A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials is an _n_u2009×u2009_n_ matrix with a diamond inscribed into it. You are given an odd integer _n_. You need to draw a crystal of size _n_. The diamond cells of the matrix should be represented by character "D". All other cells of the matrix should be represented by character "*". Look at the examples to understand what you need to draw. Input The only line contains an integer _n_ (3u2009≤u2009_n_u2009≤u2009101; _n_ is odd). Output Output a crystal of size _n_. Examples Input 3 Output *D DDD *D Input 5 Output **D* *DDD DDDDD *DDD **D* Input 7 Output ***D** **DDD* *DDDDD DDDDDDD *DDDDD **DDD* ***D**
800
false
false
true
false
false
false
false
false
false
false
8,021
609D
Nura wants to buy _k_ gadgets. She has only _s_ burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing. Nura can buy gadgets for _n_ days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds. Each day (from 1 to _n_) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during _n_ days. Help Nura to find the minimum day index when she will have _k_ gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to _m_ in order of their appearing in input. Input First line contains four integers _n_,u2009_m_,u2009_k_,u2009_s_ (1u2009≤u2009_n_u2009≤u20092·105,u20091u2009≤u2009_k_u2009≤u2009_m_u2009≤u20092·105,u20091u2009≤u2009_s_u2009≤u2009109) — number of days, total number and required number of gadgets, number of burles Nura has. Second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106) — the cost of one dollar in burles on _i_-th day. Third line contains _n_ integers _b__i_ (1u2009≤u2009_b__i_u2009≤u2009106) — the cost of one pound in burles on _i_-th day. Each of the next _m_ lines contains two integers _t__i_,u2009_c__i_ (1u2009≤u2009_t__i_u2009≤u20092,u20091u2009≤u2009_c__i_u2009≤u2009106) — type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. Output If Nura can't buy _k_ gadgets print the only line with the number -1. Otherwise the first line should contain integer _d_ — the minimum day index, when Nura will have _k_ gadgets. On each of the next _k_ lines print two integers _q__i_,u2009_d__i_ — the number of gadget and the day gadget should be bought. All values _q__i_ should be different, but the values _d__i_ can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to _n_. In case there are multiple possible solutions, print any of them. Examples Input 5 4 2 2 1 2 3 2 1 3 2 1 2 3 1 1 2 1 1 2 2 2 Input 4 3 2 200 69 70 71 72 104 105 106 107 1 1 2 2 1 2 Input 4 3 1 1000000000 900000 910000 940000 990000 990000 999000 999900 999990 1 87654 2 76543 1 65432
2,000
false
true
false
false
false
false
false
true
false
false
7,409
1671B
You are given $$$n$$$ points with integer coordinates on a coordinate axis $$$OX$$$. The coordinate of the $$$i$$$-th point is $$$x_i$$$. All points' coordinates are distinct and given in strictly increasing order. For each point $$$i$$$, you can do the following operation no more than once: take this point and move it by $$$1$$$ to the left or to the right (i..e., you can change its coordinate $$$x_i$$$ to $$$x_i - 1$$$ or to $$$x_i + 1$$$). In other words, for each point, you choose (separately) its new coordinate. For the $$$i$$$-th point, it can be either $$$x_i - 1$$$, $$$x_i$$$ or $$$x_i + 1$$$. Your task is to determine if you can move some points as described above in such a way that the new set of points forms a consecutive segment of integers, i.u2009e. for some integer $$$l$$$ the coordinates of points should be equal to $$$l, l + 1, ldots, l + n - 1$$$. Note that the resulting points should have distinct coordinates. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of points in the set $$$x$$$. The second line of the test case contains $$$n$$$ integers $$$x_1 < x_2 < ldots < x_n$$$ ($$$1 le x_i le 10^6$$$), where $$$x_i$$$ is the coordinate of the $$$i$$$-th point. It is guaranteed that the points are given in strictly increasing order (this also means that all coordinates are distinct). It is also guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer — if the set of points from the test case can be moved to form a consecutive segment of integers, print YES, otherwise print NO. Example Input 5 2 1 4 3 1 2 3 4 1 2 3 7 1 1000000 3 2 5 6
1,000
true
false
false
false
false
false
true
false
true
false
2,250
1034D
Little D is a friend of Little C who loves intervals very much instead of number "$$$3$$$". Now he has $$$n$$$ intervals on the number axis, the $$$i$$$-th of which is $$$[a_i,b_i]$$$. Only the $$$n$$$ intervals can not satisfy him. He defines the value of an interval of intervals $$$[l,r]$$$ ($$$1 leq l leq r leq n$$$, $$$l$$$ and $$$r$$$ are both integers) as the total length of the union of intervals from the $$$l$$$-th to the $$$r$$$-th. He wants to select exactly $$$k$$$ different intervals of intervals such that the sum of their values is maximal. Please help him calculate the maximal sum. Input First line contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 3 cdot 10^5$$$, $$$1 leq k leq min{frac{n(n+1)}{2},10^9}$$$) — the number of intervals Little D has and the number of intervals of intervals he will select. Each of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$, the $$$i$$$-th line of the $$$n$$$ lines describing the $$$i$$$-th interval ($$$1 leq a_i < b_i leq 10^9$$$). Output Print one integer — the maximal sum of values Little D can get. Note For the first example, Little D will select $$$[1,2]$$$, the union of the first interval and the second interval is $$$[1,4]$$$, whose length is $$$3$$$. For the second example, Little D will select $$$[1,2]$$$, $$$[2,3]$$$ and $$$[1,3]$$$, the answer is $$$5+6+4=15$$$.
3,500
false
false
false
false
true
false
false
true
false
false
5,525
1041D
A plane is flying at a constant height of $$$h$$$ meters above the ground surface. Let's consider that it is flying from the point $$$(-10^9, h)$$$ to the point $$$(10^9, h)$$$ parallel with $$$Ox$$$ axis. A glider is inside the plane, ready to start his flight at any moment (for the sake of simplicity let's consider that he may start only when the plane's coordinates are integers). After jumping from the plane, he will fly in the same direction as the plane, parallel to $$$Ox$$$ axis, covering a unit of distance every second. Naturally, he will also descend; thus his second coordinate will decrease by one unit every second. There are ascending air flows on certain segments, each such segment is characterized by two numbers $$$x_1$$$ and $$$x_2$$$ ($$$x_1 < x_2$$$) representing its endpoints. No two segments share any common points. When the glider is inside one of such segments, he doesn't descend, so his second coordinate stays the same each second. The glider still flies along $$$Ox$$$ axis, covering one unit of distance every second. If the glider jumps out at $$$1$$$, he will stop at $$$10$$$. Otherwise, if he jumps out at $$$2$$$, he will stop at $$$12$$$. Determine the maximum distance along $$$Ox$$$ axis from the point where the glider's flight starts to the point where his flight ends if the glider can choose any integer coordinate to jump from the plane and start his flight. After touching the ground the glider stops altogether, so he cannot glide through an ascending airflow segment if his second coordinate is $$$0$$$. Input The first line contains two integers $$$n$$$ and $$$h$$$ $$$(1 le n le 2cdot10^{5}, 1 le h le 10^{9})$$$xa0— the number of ascending air flow segments and the altitude at which the plane is flying, respectively. Each of the next $$$n$$$ lines contains two integers $$$x_{i1}$$$ and $$$x_{i2}$$$ $$$(1 le x_{i1} < x_{i2} le 10^{9})$$$xa0— the endpoints of the $$$i$$$-th ascending air flow segment. No two segments intersect, and they are given in ascending order. Output Print one integerxa0— the maximum distance along $$$Ox$$$ axis that the glider can fly from the point where he jumps off the plane to the point where he lands if he can start his flight at any integer coordinate. Examples Input 5 10 5 7 11 12 16 20 25 26 30 33 Input 1 1000000000 1 1000000000 Note In the first example if the glider can jump out at $$$(2, 4)$$$, then the landing point is $$$(12, 0)$$$, so the distance is $$$12-2 = 10$$$. In the second example the glider can fly from $$$(16,10)$$$ to $$$(34,0)$$$, and the distance is $$$34-16=18$$$. In the third example the glider can fly from $$$(-100,1000000000)$$$ to $$$(1999999899,0)$$$, so the distance is $$$1999999899-(-100)=1999999999$$$.
1,700
false
false
false
false
true
false
false
true
false
false
5,492
1770H
Wow, what a big face! Kagura Mahiru Koxia and Mahiru are enjoying the Winter Festival. The streets of the Winter Festival can be represented as a $$$n imes n$$$ undirected grid graph. Formally, the set of vertices is $$${(i,j) ; ; 1 leq i,jleq n }$$$ and two vertices $$$(i_1,j_1)$$$ and $$$(i_2,j_2)$$$ are connected by an edge if and only if $$$i_1-i_2+j_1-j_2=1$$$. A network with size $$$n = 3$$$. Koxia and Mahiru are planning to visit The Winter Festival by traversing $$$2n$$$ routes. Although routes are not planned yet, the endpoints of the routes are already planned as follows: In the $$$i$$$-th route, they want to start from vertex $$$(1, i)$$$ and end at vertex $$$(n, p_i)$$$, where $$$p$$$ is a permutation of length $$$n$$$. In the $$$(i+n)$$$-th route, they want to start from vertex $$$(i, 1)$$$ and end at vertex $$$(q_i, n)$$$, where $$$q$$$ is a permutation of length $$$n$$$. A network with size $$$n = 3$$$, points to be connected are shown in the same color for $$$p = [3, 2, 1]$$$ and $$$q = [3, 1, 2]$$$. Your task is to find a routing scheme — $$$2n$$$ paths where each path connects the specified endpoints. Let's define the congestion of an edge as the number of times it is used (both directions combined) in the routing scheme. In order to ensure that Koxia and Mahiru won't get too bored because of traversing repeated edges, please find a routing scheme that minimizes the maximum congestion among all edges. An example solution — the maximum congestion is $$$2$$$, which is optimal in this case. Input The first line contains an integer $$$n$$$ ($$$2 leq n leq 200$$$) — the size of the network. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 leq p_i leq n$$$). The third line contains $$$n$$$ integers $$$q_1, q_2, dots, q_n$$$ ($$$1 leq q_i leq n$$$). It is guaranteed that both $$$p$$$ and $$$q$$$ are permutations of length $$$n$$$. Output Output $$$2n$$$ lines, each line describing a route. The first $$$n$$$ lines should describe the connections from top to bottom. The $$$i$$$-th line should describe the route starting at vertex $$$(1, i)$$$ and ending at vertex $$$(n, p_i)$$$. The next $$$n$$$ lines should describe the connections from left to right. The $$$(i+n)$$$-th line should describe the route starting at vertex $$$(i, 1)$$$ and ending at vertex $$$(q_i, n)$$$. Each line describing a route should start with an integer $$$k$$$ ($$$2 le k le 10^5$$$) — the number of vertices the route passes, including the starting and ending vertices. Then output all the vertices on the route in order. In other words, if the route is $$$(x_1, y_1) ightarrow (x_2, y_2) ightarrow dots ightarrow (x_k, y_k)$$$, then output $$$k~x_1~y_1~x_2~y_2 ldots x_k~y_k$$$. Note that $$$x_i-x_{i+1}+y_i-y_{i+1} = 1$$$ should holds for $$$1 le i < k$$$. If there are multiple solutions that minimize the maximum congestion, you may output any. Examples Output 5 1 1 2 1 2 2 3 2 3 3 3 1 2 2 2 3 2 5 1 3 1 2 1 1 2 1 3 1 5 1 1 1 2 1 3 2 3 3 3 4 2 1 2 2 2 3 1 3 4 3 1 3 2 3 3 2 3 Output 6 1 1 1 2 2 2 2 3 3 3 4 3 6 1 2 1 3 2 3 2 4 3 4 4 4 5 1 3 2 3 2 2 3 2 4 2 7 1 4 1 3 1 2 2 2 2 1 3 1 4 1 7 1 1 2 1 3 1 3 2 3 3 2 3 2 4 6 2 1 2 2 3 2 4 2 4 3 4 4 6 3 1 3 2 3 3 3 4 2 4 1 4 5 4 1 4 2 4 3 3 3 3 4 Output 3 1 1 2 1 3 1 3 1 2 2 2 3 2 3 1 3 2 3 3 3 3 1 1 1 2 1 3 3 2 1 2 2 2 3 3 3 1 3 2 3 3 Note The first example corresponds to the figures in the problem statement. The output for examples $$$2$$$ and $$$3$$$ respectively are visualized below: Sample output for examples $$$2$$$ and $$$3$$$. Maximum congestions are $$$2$$$ and $$$1$$$ respectively.
3,500
false
false
false
false
false
true
false
false
false
false
1,670
1264F
The well-known Fibonacci sequence $$$F_0, F_1, F_2,ldots $$$ is defined as follows: $$$F_0 = 0, F_1 = 1$$$. For each $$$i geq 2$$$: $$$F_i = F_{i - 1} + F_{i - 2}$$$. Given an increasing arithmetic sequence of positive integers with $$$n$$$ elements: $$$(a, a + d, a + 2cdot d,ldots, a + (n - 1)cdot d)$$$. You need to find another increasing arithmetic sequence of positive integers with $$$n$$$ elements $$$(b, b + e, b + 2cdot e,ldots, b + (n - 1)cdot e)$$$ such that: $$$0 < b, e < 2^{64}$$$, for all $$$0leq i < n$$$, the decimal representation of $$$a + i cdot d$$$ appears as substring in the last $$$18$$$ digits of the decimal representation of $$$F_{b + i cdot e}$$$ (if this number has less than $$$18$$$ digits, then we consider all its digits). Input The first line contains three positive integers $$$n$$$, $$$a$$$, $$$d$$$ ($$$1 leq n, a, d, a + (n - 1) cdot d < 10^6$$$). Output If no such arithmetic sequence exists, print $$$-1$$$. Otherwise, print two integers $$$b$$$ and $$$e$$$, separated by space in a single line ($$$0 < b, e < 2^{64}$$$). If there are many answers, you can output any of them. Note In the first test case, we can choose $$$(b, e) = (2, 1)$$$, because $$$F_2 = 1, F_3 = 2, F_4 = 3$$$. In the second test case, we can choose $$$(b, e) = (19, 5)$$$ because: $$$F_{19} = 4181$$$ contains $$$1$$$; $$$F_{24} = 46368$$$ contains $$$3$$$; $$$F_{29} = 514229$$$ contains $$$5$$$; $$$F_{34} = 5702887$$$ contains $$$7$$$; $$$F_{39} = 63245986$$$ contains $$$9$$$.
3,500
false
false
false
false
false
true
false
false
false
false
4,375
1847C
. Then he summons a new Stand user, with index $$$m+1$$$ and strength given by: $$$$$$a_{m+1} = a_i oplus a_{i+1} oplus ldots oplus a_m,$$$$$$where the operator $$$oplus$$$ denotes the . The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) xa0– the number of Stand users initially summoned. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < 2^8$$$) xa0– the strength of each Stand user. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integer, maximum strength of a Stand user among all possible ways that DIO can summon. Example Input 3 4 0 2 5 1 3 1 2 3 5 8 2 4 12 1 Note In the first test case, one of the ways to add new Stand users is as follows: Choose $$$i=n$$$. Now, $$$a$$$ becomes $$$[0,2,5,1,1]$$$. Choose $$$i=1$$$. Now, $$$a$$$ becomes $$$[0,2,5,1,1,7]$$$. $$$7$$$ is the maximum strength of a Stand user DIO can summon. In the second test case, DIO does not need to add more Stand users because $$$3$$$ is the maximum strength of a Stand user DIO can summon.
1,400
false
true
false
true
false
false
true
false
false
false
1,198
1538C
You are given an array $$$a$$$ of $$$n$$$ integers. Find the number of pairs $$$(i, j)$$$ ($$$1 le i < j le n$$$) where the sum of $$$a_i + a_j$$$ is greater than or equal to $$$l$$$ and less than or equal to $$$r$$$ (that is, $$$l le a_i + a_j le r$$$). For example, if $$$n = 3$$$, $$$a = [5, 1, 2]$$$, $$$l = 4$$$ and $$$r = 7$$$, then two pairs are suitable: $$$i=1$$$ and $$$j=2$$$ ($$$4 le 5 + 1 le 7$$$); $$$i=1$$$ and $$$j=3$$$ ($$$4 le 5 + 2 le 7$$$). Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains three integers $$$n, l, r$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le l le r le 10^9$$$)xa0— the length of the array and the limits on the sum in the pair. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ overall test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of index pairs $$$(i, j)$$$ ($$$i < j$$$), such that $$$l le a_i + a_j le r$$$. Example Input 4 3 4 7 5 1 2 5 5 8 5 1 2 4 3 4 100 1000 1 1 1 1 5 9 13 2 5 5 1 1
1,300
true
false
false
false
true
false
false
true
false
false
2,953
1500C
You are given two tables $$$A$$$ and $$$B$$$ of size $$$n imes m$$$. We define a sorting by column as the following: we choose a column and reorder the rows of the table by the value in this column, from the rows with the smallest value to the rows with the largest. In case there are two or more rows with equal value in this column, their relative order does not change (such sorting algorithms are called stable). You can find this behavior of sorting by column in many office software for managing spreadsheets. Petya works in one, and he has a table $$$A$$$ opened right now. He wants to perform zero of more sortings by column to transform this table to table $$$B$$$. Determine if it is possible to do so, and if yes, find a sequence of columns to sort by. Note that you do not need to minimize the number of sortings. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 1500$$$)xa0— the sizes of the tables. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$a_{i,j}$$$ ($$$1 le a_{i, j} le n$$$), denoting the elements of the table $$$A$$$. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$b_{i, j}$$$ ($$$1 le b_{i, j} le n$$$), denoting the elements of the table $$$B$$$. Output If it is not possible to transform $$$A$$$ into $$$B$$$, print $$$-1$$$. Otherwise, first print an integer $$$k$$$ ($$$0 le k le 5000$$$)xa0— the number of sortings in your solution. Then print $$$k$$$ integers $$$c_1, ldots, c_k$$$ ($$$1 le c_i le m$$$)xa0— the columns, by which Petya needs to perform a sorting. We can show that if a solution exists, there is one in no more than $$$5000$$$ sortings. Examples Input 3 3 2 3 2 1 3 3 1 1 2 1 1 2 1 3 3 2 3 2 Note Consider the second example. After the sorting by the first column the table becomes $$$$$$begin{matrix} 1&3&3 1&1&2 2&3&2. end{matrix}$$$$$$ After the sorting by the second column the table becomes $$$$$$begin{matrix} 1&1&2 1&3&3 2&3&2, end{matrix}$$$$$$ and this is what we need. In the third test any sorting does not change anything, because the columns are already sorted.
2,600
false
true
false
false
false
true
true
false
false
false
3,166
1599G
Problem - 1599G - 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 shortest paths *2700 No tag edit access → Contest materials - the number of points, and $$$K$$$ ($$$1 leq K leq N$$$) - the index of the starting point. Each of the next $$$N$$$ lines contain two integers, $$$A_i$$$, $$$B_i$$$ ($$$-10^6 leq A_i, B_i leq 10^6$$$) - coordinates of the $$$i-th$$$ point. Output The output contains one number - the shortest path to visit all given points starting from point $$$K$$$. The absolute difference between your solution and the main solution shouldn't exceed $$$10^-6$$$; Example Input 5 2 0 0 -1 1 2 -2 0 1 -2 2 Output 7.478709 Note The shortest path consists of these moves: 2 -> 5 5 -> 4 4 -> 1 1 -> 3 There isn't any shorter path possible.
2,700
true
false
false
false
false
false
true
false
false
false
2,658
813B
Problem - 813B - Codeforces =============== xa0 and year 18 isn't unlucky as there is no such representation for it. Such interval of years that there are no unlucky years in it is called The Golden Age. You should write a program which will find maximum length of The Golden Age which starts no earlier than the year _l_ and ends no later than the year _r_. If all years in the interval
1,800
true
false
false
false
false
false
true
false
false
false
6,510
508C
Anya loves to watch horror movies. In the best traditions of horror, she will be visited by _m_ ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly _t_ seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly _t_ seconds and then goes out and can no longer be used. For each of the _m_ ghosts Anya knows the time at which it comes: the _i_-th visit will happen _w__i_ seconds after midnight, all _w__i_'s are distinct. Each visit lasts exactly one second. What is the minimum number of candles Anya should use so that during each visit, at least _r_ candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time. Input The first line contains three integers _m_, _t_, _r_ (1u2009≤u2009_m_,u2009_t_,u2009_r_u2009≤u2009300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit. The next line contains _m_ space-separated numbers _w__i_ (1u2009≤u2009_i_u2009≤u2009_m_, 1u2009≤u2009_w__i_u2009≤u2009300), the _i_-th of them repesents at what second after the midnight the _i_-th ghost will come. All _w__i_'s are distinct, they follow in the strictly increasing order. Output If it is possible to make at least _r_ candles burn during each visit, then print the minimum number of candles that Anya needs to light for that. If that is impossible, print u2009-u20091. Note Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit. It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively. In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight. In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight. In the third sample test the answer is u2009-u20091, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes.
1,600
false
true
false
false
false
true
false
false
false
false
7,809
1917E
You are given an even integer $$$n$$$ and an integer $$$k$$$. Your task is to construct a matrix of size $$$n imes n$$$ consisting of numbers $$$0$$$ and $$$1$$$ in such a way that the following conditions are true, or report that it is impossible: the sum of all the numbers in the matrix is exactly $$$k$$$; the bitwise $$$ exttt{XOR}$$$ of all the numbers in the row $$$i$$$ is the same for each $$$i$$$; the bitwise $$$ exttt{XOR}$$$ of all the numbers in the column $$$j$$$ is the same for each $$$j$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 130$$$)xa0— the number of test cases. The description of the test cases follows. Each test case is described by a single line, which contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 1000$$$, $$$n$$$ is even, $$$0 leq k leq n^2$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output $$$ exttt{Yes}$$$ if it's possible to construct a matrix that satisfies all of the problem's conditions, and $$$ exttt{No}$$$ otherwise. If it is possible to construct a matrix, the $$$i$$$-th of the next $$$n$$$ lines should contain $$$n$$$ integers representing the elements in the $$$i$$$-th row of the matrix. Example Input 5 4 0 6 6 6 5 4 2 6 36 Output Yes 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Yes 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 No No Yes 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Note In the first example, all conditions are satisfied: the sum of all the numbers in the matrix is exactly $$$0$$$; the bitwise $$$ exttt{XOR}$$$ of all the numbers in the row $$$i$$$ is $$$0$$$ for each $$$i$$$; the bitwise $$$ exttt{XOR}$$$ of all the numbers in the column $$$j$$$ is $$$0$$$ for each $$$j$$$. In the third example, it can be shown that it's impossible to find a matrix satisfying all the problem's conditions.
2,500
true
false
false
false
false
true
false
false
false
false
790
378B
Two semifinals have just been in the running tournament. Each semifinal had _n_ participants. There are _n_ participants advancing to the finals, they are chosen as follows: from each semifinal, we choose _k_ people (0u2009≤u20092_k_u2009≤u2009_n_) who showed the best result in their semifinals and all other places in the finals go to the people who haven't ranked in the top _k_ in their semifinal but got to the _n_u2009-u20092_k_ of the best among the others. The tournament organizers hasn't yet determined the _k_ value, so the participants want to know who else has any chance to get to the finals and who can go home. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of participants in each semifinal. Each of the next _n_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009109)xa0— the results of the _i_-th participant (the number of milliseconds he needs to cover the semifinals distance) of the first and second semifinals, correspondingly. All results are distinct. Sequences _a_1, _a_2, ..., _a__n_ and _b_1, _b_2, ..., _b__n_ are sorted in ascending order, i.e. in the order the participants finished in the corresponding semifinal. Output Print two strings consisting of _n_ characters, each equals either "0" or "1". The first line should correspond to the participants of the first semifinal, the second line should correspond to the participants of the second semifinal. The _i_-th character in the _j_-th line should equal "1" if the _i_-th participant of the _j_-th semifinal has any chances to advance to the finals, otherwise it should equal a "0". Examples Input 4 9840 9920 9860 9980 9930 10020 10040 10090 Input 4 9900 9850 9940 9930 10000 10020 10060 10110 Note Consider the first sample. Each semifinal has 4 participants. The results of the first semifinal are 9840, 9860, 9930, 10040. The results of the second semifinal are 9920, 9980, 10020, 10090. If _k_u2009=u20090, the finalists are determined by the time only, so players 9840, 9860, 9920 and 9930 advance to the finals. If _k_u2009=u20091, the winners from both semifinals move to the finals (with results 9840 and 9920), and the other places are determined by the time (these places go to the sportsmen who run the distance in 9860 and 9930 milliseconds). If _k_u2009=u20092, then first and second places advance from each seminfial, these are participants with results 9840, 9860, 9920 and 9980 milliseconds.
1,300
false
false
true
false
false
false
false
false
true
false
8,318
389A
Problem - 389A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy math *1000 No tag edit access → Contest materials . Then the second line contains _n_ integers: _x_1, _x_2, ..., _x__n_ (1u2009≤u2009_x__i_u2009≤u2009100). Output Output a single integer — the required minimal sum. Examples Input 2 1 2 Output 2 Input 3 2 4 6 Output 6 Input 2 12 18 Output 12 Input 5 45 12 27 30 18 Output 15 Note In the first example the optimal way is to do the assignment: _x_2 = _x_2 - _x_1. In the second example the optimal sequence of operations is: _x_3 = _x_3 - _x_2, _x_2 = _x_2 - _x_1.
1,000
true
true
false
false
false
false
false
false
false
false
8,272
1976F
You are given a rooted tree, consisting of $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Vertex $$$1$$$ is the root. Additionally, the root only has one child. You are asked to add exactly $$$k$$$ edges to the tree (possibly, multiple edges and/or edges already existing in the tree). Recall that a bridge is such an edge that, after you remove it, the number of connected components in the graph increases. So, initially, all edges of the tree are bridges. After $$$k$$$ edges are added, some original edges of the tree are still bridges and some are not anymore. You want to satisfy two conditions: for every bridge, all tree edges in the subtree of the lower vertex of that bridge should also be bridges; the number of bridges is as small as possible. Solve the task for all values of $$$k$$$ from $$$1$$$ to $$$n - 1$$$ and output the smallest number of bridges. 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$$$ ($$$2 le n le 3 cdot 10^5$$$)xa0— the number of vertices of the tree. Each of the next $$$n - 1$$$ lines contain two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$)xa0— the description of the edges of the tree. It's guaranteed that the given edges form a valid tree. Additional constraint on the input: the root (vertex $$$1$$$) has exactly one child. The sum of $$$n$$$ over all testcases doesn't exceed $$$3 cdot 10^5$$$. Output For each testcase, print $$$n - 1$$$ integers. For each $$$k$$$ from $$$1$$$ to $$$n - 1$$$ print the smallest number of bridges that can be left after you add $$$k$$$ edges to the tree. Example Input 4 2 1 2 12 4 10 5 12 12 11 3 6 9 6 1 6 12 7 11 6 2 11 10 9 10 8 8 1 2 2 3 2 4 3 5 3 6 4 7 4 8 5 1 2 2 3 3 4 4 5 Output 0 7 3 1 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0
2,800
false
true
false
true
true
false
false
false
true
false
428
870E
Problem - 870E - Codeforces =============== xa0 ") ") xa0— the number of points. _n_ lines follow. The (_i_u2009+u20091)-th of these lines contains two integers _x__i_, _y__i_ (u2009-u2009109u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009109)xa0— coordinates of the _i_-th point. It is guaranteed that all points are distinct. Output Print the number of possible distinct pictures modulo 109u2009+u20097. Examples Input 4 1 1 1 2 2 1 2 2 Output 16 Input 2 -1 -1 0 1 Output 9 Note In the first example there are two vertical and two horizontal lines passing through the points. You can get pictures with any subset of these lines. For example, you can get the picture containing all four lines in two ways (each segment represents a line containing it). The first way: The second way: In the second example you can work with two points independently. The number of pictures is 32u2009=u20099.
2,300
false
false
false
false
false
false
false
false
false
true
6,257
357B
In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions: overall, there must be _m_ dances; exactly three people must take part in each dance; each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland). The agency has _n_ dancers, and their number can be less than 3_m_. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance. You considered all the criteria and made the plan for the _m_ dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the _n_ dancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances. Input The first line contains two space-separated integers _n_ (3u2009≤u2009_n_u2009≤u2009105) and _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of dancers and the number of dances, correspondingly. Then _m_ lines follow, describing the dances in the order of dancing them. The _i_-th line contains three distinct integers — the numbers of the dancers that take part in the _i_-th dance. The dancers are numbered from 1 to _n_. Each dancer takes part in at least one dance. Output Print _n_ space-separated integers: the _i_-th number must represent the color of the _i_-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.
1,400
false
false
true
false
false
true
false
false
false
false
8,405
1903B
In Cyprus, the weather is pretty hot. Thus, Theofanis saw this as an opportunity to create an ice cream company. He keeps the ice cream safe from other ice cream producers by locking it inside big storage rooms. However, he forgot the password. Luckily, the lock has a special feature for forgetful people! It gives you a table $$$M$$$ with $$$n$$$ rows and $$$n$$$ columns of non-negative integers, and to open the lock, you need to find an array $$$a$$$ of $$$n$$$ elements such that: $$$0 le a_i < 2^{30}$$$, and $$$M_{i,j} = a_i a_j$$$ for all $$$i eq j$$$, where $$$$$$ denotes the xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^{3}$$$)xa0— the size of the hidden array. The next $$$n$$$ lines describe the rows of $$$M$$$, line $$$i$$$ contains the table values $$$M_{i,1}, M_{i,2}, ldots, M_{i,n}$$$ ($$$0 le M_{i,j} < 2^{30}$$$). It is guaranteed that $$$M_{i,i} = 0$$$ and $$$M_{i,j} = M_{j,i}$$$ for all $$$1 le i,j le n$$$. It is also guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^{3}$$$. Output For each test case, if there is a solution print YES and an array that satisfies the property, otherwise print NO. If there are multiple solutions, print any of them. 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 1 0 4 0 3 3 5 3 0 3 7 3 3 0 7 5 7 7 0 5 0 7 7 5 5 7 0 3 2 6 7 3 0 3 7 5 2 3 0 4 5 6 7 4 0 3 0 0 1 0 0 0 1 0 0 Output YES 7 YES 1 3 2 5 YES 5 2 3 0 4 NO
1,200
false
true
false
false
false
true
true
false
false
false
896
1439A2
This is the hard version of the problem. The difference between the versions is in the number of possible operations that can be made. You can make hacks if and only if you solved both versions of the problem. You are given a binary table of size $$$n imes m$$$. This table consists of symbols $$$0$$$ and $$$1$$$. You can make such operation: select $$$3$$$ different cells that belong to one $$$2 imes 2$$$ square and change the symbols in these cells (change $$$0$$$ to $$$1$$$ and $$$1$$$ to $$$0$$$). Your task is to make all symbols in the table equal to $$$0$$$. You are allowed to make at most $$$nm$$$ operations. You don't need to minimize the number of operations. It can be proved, that it is always possible. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5000$$$)xa0— the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 leq n, m leq 100$$$). Each of the next $$$n$$$ lines contains a binary string of length $$$m$$$, describing the symbols of the next row of the table. It is guaranteed, that the sum of $$$nm$$$ for all test cases does not exceed $$$20000$$$. Output For each test case print the integer $$$k$$$ ($$$0 leq k leq nm$$$)xa0— the number of operations. In the each of the next $$$k$$$ lines print $$$6$$$ integers $$$x_1, y_1, x_2, y_2, x_3, y_3$$$ ($$$1 leq x_1, x_2, x_3 leq n, 1 leq y_1, y_2, y_3 leq m$$$) describing the next operation. This operation will be made with three cells $$$(x_1, y_1)$$$, $$$(x_2, y_2)$$$, $$$(x_3, y_3)$$$. These three cells should be different. These three cells should belong to some $$$2 imes 2$$$ square. Example Input 5 2 2 10 11 3 3 011 101 110 4 4 1111 0110 0110 1111 5 5 01011 11001 00010 11011 10000 2 3 011 101 Output 1 1 1 2 1 2 2 2 2 1 3 1 3 2 1 2 1 3 2 3 4 1 1 1 2 2 2 1 3 1 4 2 3 3 2 4 1 4 2 3 3 4 3 4 4 4 1 2 2 1 2 2 1 4 1 5 2 5 4 1 4 2 5 1 4 4 4 5 3 4 2 1 3 2 2 2 3 1 2 2 1 2 2 Note In the first test case, it is possible to make only one operation with cells $$$(1, 1)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After that, all symbols will be equal to $$$0$$$. In the second test case: operation with cells $$$(2, 1)$$$, $$$(3, 1)$$$, $$$(3, 2)$$$. After it the table will be: 011 001 000 operation with cells $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$. After it the table will be: 000 000 000 In the fifth test case: operation with cells $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$. After it the table will be: 010 110 operation with cells $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(2, 2)$$$. After it the table will be: 000 000
1,900
false
true
true
false
false
true
false
false
false
true
3,471
1286C2
This problem is different with easy version only by constraints on total answers length It is an interactive problem Venya joined a tour to the madhouse, in which orderlies play with patients the following game. Orderlies pick a string $$$s$$$ of length $$$n$$$, consisting only of lowercase English letters. The player can ask two types of queries: ? l r – ask to list all substrings of $$$s[l..r]$$$. Substrings will be returned in random order, and in every substring, all characters will be randomly shuffled. ! s – guess the string picked by the orderlies. This query can be asked exactly once, after that the game will finish. If the string is guessed correctly, the player wins, otherwise he loses. The player can ask no more than $$$3$$$ queries of the first type. To make it easier for the orderlies, there is an additional limitation: the total number of returned substrings in all queries of the first type must not exceed $$$leftlceil 0.777(n+1)^2 ight ceil$$$ ($$$lceil x ceil$$$ is $$$x$$$ rounded up). Venya asked you to write a program, which will guess the string by interacting with the orderlies' program and acting by the game's rules. Your program should immediately terminate after guessing the string using a query of the second type. In case your program guessed the string incorrectly, or it violated the game rules, it will receive verdict Wrong answer. Note that in every test case the string is fixed beforehand and will not change during the game, which means that the interactor is not adaptive. Input First line contains number $$$n$$$ ($$$1 le n le 100$$$)xa0— the length of the picked string. Interaction You start the interaction by reading the number $$$n$$$. To ask a query about a substring from $$$l$$$ to $$$r$$$ inclusively ($$$1 le l le r le n$$$), you should output ? l r on a separate line. After this, all substrings of $$$s[l..r]$$$ will be returned in random order, each substring exactly once. In every returned substring all characters will be randomly shuffled. In the case, if you ask an incorrect query, ask more than $$$3$$$ queries of the first type or there will be more than $$$leftlceil 0.777(n+1)^2 ight ceil$$$ substrings returned in total, you will receive verdict Wrong answer. To guess the string $$$s$$$, you should output ! s on a separate line. After printing each query, do not forget to flush the output. Otherwise, you will get Idleness limit exceeded. To flush the output, you can 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. If you received - (dash) as an answer to any query, you need to terminate your program with exit code 0 (for example, by calling exit(0)). This means that there was an error in the interaction protocol. If you don't terminate with exit code 0, you can receive any unsuccessful verdict. Hack format To hack a solution, use the following format: The first line should contain one integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the length of the string, and the following line should contain the string $$$s$$$. Example Output ? 1 2 ? 3 4 ? 4 4 ! aabc
2,800
true
false
false
false
false
true
true
false
false
false
4,258
186B
Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules so that the event gets more interesting to watch. Each mushroom grower has a mushroom that he will grow on the competition. Under the new rules, the competition consists of two parts. The first part lasts _t_1 seconds and the second part lasts _t_2 seconds. The first and the second part are separated by a little break. After the starting whistle the first part of the contest starts, and all mushroom growers start growing mushrooms at once, each at his individual speed of _v__i_ meters per second. After _t_1 seconds, the mushroom growers stop growing mushrooms and go to have a break. During the break, for unexplained reasons, the growth of all mushrooms is reduced by _k_ percent. After the break the second part of the contest starts and all mushrooms growers at the same time continue to grow mushrooms, each at his individual speed of _u__i_ meters per second. After a _t_2 seconds after the end of the break, the competition ends. Note that the speeds before and after the break may vary. Before the match dwarf Pasha learned from all participants, what two speeds they have chosen. However, the participants did not want to disclose to him all their strategy and therefore, did not say in what order they will be using these speeds. That is, if a participant chose speeds _a__i_ and _b__i_, then there are two strategies: he either uses speed _a__i_ before the break and speed _b__i_ after it, or vice versa. Dwarf Pasha really wants to win the totalizer. He knows that each participant chooses the strategy that maximizes the height of the mushroom. Help Dwarf Pasha make the final table of competition results. The participants are sorted in the result table by the mushroom height (the participants with higher mushrooms follow earlier in the table). In case of equal mushroom heights, the participants are sorted by their numbers (the participants with a smaller number follow earlier). Input The first input line contains four integer numbers _n_, _t_1, _t_2, _k_ (1u2009≤u2009_n_,u2009_t_1,u2009_t_2u2009≤u20091000;xa01u2009≤u2009_k_u2009≤u2009100) — the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly. Each of the following _n_ lines contains two integers. The _i_-th (1u2009≤u2009_i_u2009≤u2009_n_) line contains space-separated integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u20091000) — the speeds which the participant number _i_ chose. Output Print the final results' table: _n_ lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate.
1,200
false
true
false
false
false
false
false
false
true
false
9,098
1976A
Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords. Monocarp decided that strong passwords should satisfy the following conditions: password should consist only of lowercase Latin letters and digits; there should be no digit that comes after a letter (so, after each letter, there is either another letter or the string ends); all digits should be sorted in the non-decreasing order; all letters should be sorted in the non-decreasing order. Note that it's allowed for the password to have only letters or only digits. Monocarp managed to implement the first condition, but he struggles with the remaining ones. Can you help him to verify the passwords? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 20$$$)xa0— the length of the password. The second line contains a string, consisting of exactly $$$n$$$ characters. Each character is either a lowercase Latin letter or a digit. Output For each testcase, print "YES" if the given password is strong and "NO" otherwise. Example Input 5 4 12ac 5 123wa 9 allllmost 5 ac123 6 011679 Note In the second testcase, the letters are not sorted in the non-decreasing order. In the fourth testcase, there is a digit that comes after a letterxa0— digit '1' after a letter 'c'.
800
false
false
true
false
false
false
false
false
true
false
433
621D
Wet Shark asked Rat Kwesh to generate three positive real numbers _x_, _y_ and _z_, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point. Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to earn a lot of cheese. He will hand the three numbers _x_, _y_ and _z_ to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options: 1. _a_1u2009=u2009_x__y__z_; 2. _a_2u2009=u2009_x__z__y_; 3. _a_3u2009=u2009(_x__y_)_z_; 4. _a_4u2009=u2009(_x__z_)_y_; 5. _a_5u2009=u2009_y__x__z_; 6. _a_6u2009=u2009_y__z__x_; 7. _a_7u2009=u2009(_y__x_)_z_; 8. _a_8u2009=u2009(_y__z_)_x_; 9. _a_9u2009=u2009_z__x__y_; 10. _a_10u2009=u2009_z__y__x_; 11. _a_11u2009=u2009(_z__x_)_y_; 12. _a_12u2009=u2009(_z__y_)_x_. Let _m_ be the maximum of all the _a__i_, and _c_ be the smallest index (from 1 to 12) such that _a__c_u2009=u2009_m_. Rat's goal is to find that _c_, and he asks you to help him. Rat Kwesh wants to see how much cheese he gets, so he you will have to print the expression corresponding to that _a__c_. Input The only line of the input contains three space-separated real numbers _x_, _y_ and _z_ (0.1u2009≤u2009_x_,u2009_y_,u2009_z_u2009≤u2009200.0). Each of _x_, _y_ and _z_ is given with exactly one digit after the decimal point. Output Find the maximum value of expression among _x__y__z_, _x__z__y_, (_x__y_)_z_, (_x__z_)_y_, _y__x__z_, _y__z__x_, (_y__x_)_z_, (_y__z_)_x_, _z__x__y_, _z__y__x_, (_z__x_)_y_, (_z__y_)_x_ and print the corresponding expression. If there are many maximums, print the one that comes first in the list. _x__y__z_ should be outputted as x^y^z (without brackets), and (_x__y_)_z_ should be outputted as (x^y)^z (quotes for clarity).
2,400
true
false
false
false
false
true
true
false
false
false
7,348
1680C
You are given a string $$$s$$$ consisting of characters 0 and/or 1. You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values: the number of characters 0 left in the string; the number of characters 1 removed from the string. What is the minimum cost of removal you can achieve? Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of one line containing the string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$), consisting of characters 0 and/or 1. The total length of strings $$$s$$$ in all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integer — the minimum cost of removal you can achieve. Example Input 5 101110110 1001001001001 0000111111 00000 1111 Note Consider the test cases of the example: 1. in the first test case, it's possible to remove two characters from the beginning and one character from the end. Only one 1 is deleted, only one 0 remains, so the cost is $$$1$$$; 2. in the second test case, it's possible to remove three characters from the beginning and six characters from the end. Two characters 0 remain, three characters 1 are deleted, so the cost is $$$3$$$; 3. in the third test case, it's optimal to remove four characters from the beginning; 4. in the fourth test case, it's optimal to remove the whole string; 5. in the fifth test case, it's optimal to leave the string as it is.
1,600
false
true
false
false
false
false
false
true
false
false
2,189
1418D
Vova decided to clean his room. The room can be represented as the coordinate axis $$$OX$$$. There are $$$n$$$ piles of trash in the room, coordinate of the $$$i$$$-th pile is the integer $$$p_i$$$. All piles have different coordinates. Let's define a total cleanup as the following process. The goal of this process is to collect all the piles in no more than two different $$$x$$$ coordinates. To achieve this goal, Vova can do several (possibly, zero) moves. During one move, he can choose some $$$x$$$ and move all piles from $$$x$$$ to $$$x+1$$$ or $$$x-1$$$ using his broom. Note that he can't choose how many piles he will move. Also, there are two types of queries: $$$0$$$ $$$x$$$ — remove a pile of trash from the coordinate $$$x$$$. It is guaranteed that there is a pile in the coordinate $$$x$$$ at this moment. $$$1$$$ $$$x$$$ — add a pile of trash to the coordinate $$$x$$$. It is guaranteed that there is no pile in the coordinate $$$x$$$ at this moment. Note that it is possible that there are zero piles of trash in the room at some moment. Vova wants to know the minimum number of moves he can spend if he wants to do a total cleanup before any queries. He also wants to know this number of moves after applying each query. Queries are applied in the given order. Note that the total cleanup doesn't actually happen and doesn't change the state of piles. It is only used to calculate the number of moves. For better understanding, please read the Notes section below to see an explanation for the first example. Input The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 10^5$$$) — the number of piles in the room before all queries and the number of queries, respectively. The second line of the input contains $$$n$$$ distinct integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le 10^9$$$), where $$$p_i$$$ is the coordinate of the $$$i$$$-th pile. The next $$$q$$$ lines describe queries. The $$$i$$$-th query is described with two integers $$$t_i$$$ and $$$x_i$$$ ($$$0 le t_i le 1; 1 le x_i le 10^9$$$), where $$$t_i$$$ is $$$0$$$ if you need to remove a pile from the coordinate $$$x_i$$$ and is $$$1$$$ if you need to add a pile to the coordinate $$$x_i$$$. It is guaranteed that for $$$t_i = 0$$$ there is such pile in the current set of piles and for $$$t_i = 1$$$ there is no such pile in the current set of piles. Note Consider the first example. Initially, the set of piles is $$$[1, 2, 6, 8, 10]$$$. The answer before the first query is $$$5$$$ because you can move all piles from $$$1$$$ to $$$2$$$ with one move, all piles from $$$10$$$ to $$$8$$$ with $$$2$$$ moves and all piles from $$$6$$$ to $$$8$$$ with $$$2$$$ moves. After the first query, the set becomes $$$[1, 2, 4, 6, 8, 10]$$$. Then the answer is $$$7$$$ because you can move all piles from $$$6$$$ to $$$4$$$ with $$$2$$$ moves, all piles from $$$4$$$ to $$$2$$$ with $$$2$$$ moves, all piles from $$$2$$$ to $$$1$$$ with $$$1$$$ move and all piles from $$$10$$$ to $$$8$$$ with $$$2$$$ moves. After the second query, the set of piles becomes $$$[1, 2, 4, 6, 8, 9, 10]$$$ and the answer is the same (and the previous sequence of moves can be applied to the current set of piles). After the third query, the set of piles becomes $$$[1, 2, 4, 8, 9, 10]$$$ and the answer is $$$5$$$ because you can move all piles from $$$1$$$ to $$$2$$$ with $$$1$$$ move, all piles from $$$2$$$ to $$$4$$$ with $$$2$$$ moves, all piles from $$$10$$$ to $$$9$$$ with $$$1$$$ move and all piles from $$$9$$$ to $$$8$$$ with $$$1$$$ move. After the fourth query, the set becomes $$$[1, 2, 4, 8, 9]$$$ and the answer is almost the same (the previous sequence of moves can be applied without moving piles from $$$10$$$). After the fifth query, the set becomes $$$[1, 2, 4, 8, 9, 100]$$$. You can move all piles from $$$1$$$ and further to $$$9$$$ and keep $$$100$$$ at its place. So the answer is $$$8$$$. After the sixth query, the set becomes $$$[1, 2, 4, 8, 9, 50, 100]$$$. The answer is $$$49$$$ and can be obtained with almost the same sequence of moves as after the previous query. The only difference is that you need to move all piles from $$$50$$$ to $$$9$$$ too.
2,100
false
false
true
false
true
false
false
false
false
false
3,591
877D
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks. Formally, her room can be represented as a field of _n_u2009×u2009_m_ cells, each cell of which is empty or littered with cans. Olya drank a lot of energy drink, so now she can run _k_ meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to _k_ meters in this direction. Of course, she can only run through empty cells. Now Olya needs to get from cell (_x_1,u2009_y_1) to cell (_x_2,u2009_y_2). How many seconds will it take her if she moves optimally? It's guaranteed that cells (_x_1,u2009_y_1) and (_x_2,u2009_y_2) are empty. These cells can coincide. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u20091000) — the sizes of the room and Olya's speed. Then _n_ lines follow containing _m_ characters each, the _i_-th of them contains on _j_-th position "#", if the cell (_i_,u2009_j_) is littered with cans, and "." otherwise. The last line contains four integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 (1u2009≤u2009_x_1,u2009_x_2u2009≤u2009_n_, 1u2009≤u2009_y_1,u2009_y_2u2009≤u2009_m_) — the coordinates of the first and the last cells. Output Print a single integer — the minimum time it will take Olya to get from (_x_1,u2009_y_1) to (_x_2,u2009_y_2). If it's impossible to get from (_x_1,u2009_y_1) to (_x_2,u2009_y_2), print -1. Examples Input 3 4 4 .... ###. .... 1 1 3 1 Input 3 4 1 .... ###. .... 1 1 3 1 Note In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second. In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds. Olya does not recommend drinking energy drinks and generally believes that this is bad.
2,100
false
false
false
false
true
false
false
false
false
true
6,237
1256D
You are given a binary string of length $$$n$$$ (i.u2009e. a string consisting of $$$n$$$ characters '0' and '1'). In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than $$$k$$$ moves? It is possible that you do not perform any moves at all. Note that you can swap the same pair of adjacent characters with indices $$$i$$$ and $$$i+1$$$ arbitrary (possibly, zero) number of times. Each such swap is considered a separate move. You have to answer $$$q$$$ independent test cases. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 10^4$$$) — the number of test cases. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^6, 1 le k le n^2$$$) — the length of the string and the number of moves you can perform. The second line of the test case contains one string consisting of $$$n$$$ characters '0' and '1'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ ($$$sum n le 10^6$$$). Output For each test case, print the answer on it: the lexicographically minimum possible string of length $$$n$$$ you can obtain from the given one if you can perform no more than $$$k$$$ moves. Example Input 3 8 5 11011010 7 9 1111100 7 11 1111100 Output 01011110 0101111 0011111 Note In the first example, you can change the string as follows: $$$1underline{10}11010 ightarrow underline{10}111010 ightarrow 0111underline{10}10 ightarrow 011underline{10}110 ightarrow 01underline{10}1110 ightarrow 01011110$$$. In the third example, there are enough operations to make the string sorted.
1,500
false
true
false
false
false
false
false
false
false
false
4,404
1197E
There are famous Russian nesting dolls named matryoshkas sold in one of the souvenir stores nearby, and you'd like to buy several of them. The store has $$$n$$$ different matryoshkas. Any matryoshka is a figure of volume $$$out_i$$$ with an empty space inside of volume $$$in_i$$$ (of course, $$$out_i > in_i$$$). You don't have much free space inside your bag, but, fortunately, you know that matryoshkas can be nested one inside another. Formally, let's call a set of matryoshkas nested if we can rearrange dolls in such a way, that the first doll can be nested inside the second one, the second doll — inside the third one and so on. Matryoshka $$$i$$$ can be nested inside matryoshka $$$j$$$ if $$$out_i le in_j$$$. So only the last doll will take space inside your bag. Let's call extra space of a nested set of dolls as a total volume of empty space inside this structure. Obviously, it's equal to $$$in_{i_1} + (in_{i_2} - out_{i_1}) + (in_{i_3} - out_{i_2}) + dots + (in_{i_k} - out_{i_{k-1}})$$$, where $$$i_1$$$, $$$i_2$$$, ..., $$$i_k$$$ are the indices of the chosen dolls in the order they are nested in each other. Finally, let's call a nested subset of the given sequence as big enough if there isn't any doll from the sequence that can be added to the nested subset without breaking its nested property. You want to buy many matryoshkas, so you should choose a big enough nested subset to buy it. But you will be disappointed if too much space in your bag will be wasted, so you want to choose a big enough subset so that its extra space is minimum possible among all big enough subsets. Now you wonder, how many different nested subsets meet these conditions (they are big enough, and there is no big enough subset such that its extra space is less than the extra space of the chosen subset). Two subsets are considered different if there exists at least one index $$$i$$$ such that one of the subsets contains the $$$i$$$-th doll, and another subset doesn't. Since the answer can be large, print it modulo $$$10^9 + 7$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of matryoshkas. The next $$$n$$$ lines contain a description of each doll: two integers $$$out_i$$$ and $$$in_i$$$ ($$$1 le in_i < out_i le 10^9$$$) — the outer and inners volumes of the $$$i$$$-th matryoshka. Note There are $$$6$$$ big enough nested subsets with minimum possible extra space in the example: $$${1, 5}$$$: we can't add any other matryoshka and keep it nested; it's extra space is $$$1$$$; $$${1, 6}$$$; $$${2, 4, 5}$$$; $$${2, 4, 6}$$$; $$${3, 4, 5}$$$; $$${3, 4, 6}$$$. There are no more "good" subsets because, for example, subset $$${6, 7}$$$ is not big enough (we can add the $$$4$$$-th matryoshka to it) or subset $$${4, 6, 7}$$$ has extra space equal to $$$2$$$.
2,300
false
false
false
true
true
false
false
true
true
false
4,711
342A
Problem - 342A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation *1200 No tag edit access → Contest materials ") positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three _a_,u2009_b_,u2009_c_ the following conditions held: _a_u2009<u2009_b_u2009<u2009_c_; _a_ divides _b_, _b_ divides _c_. Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has groups of three. Help Xenia, find the required partition or else say that it doesn't exist. Input The first line contains integer _n_ (3u2009≤u2009_n_u2009≤u200999999) — the number of elements in the sequence. The next line contains _n_ positive integers, each of them is at most 7. It is guaranteed that _n_ is divisible by 3. Output If the required partition exists, print groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them. If there is no solution, print -1. Examples Input 6 1 1 1 2 2 2 Output -1 Input 6 2 2 1 1 4 6 Output 1 2 4 1 2 6
1,200
false
true
true
false
false
false
false
false
false
false
8,468
1857D
Given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$. Elements of both arrays indexed from $$$1$$$ to $$$n$$$. You are constructing a directed graph, where edge from $$$u$$$ to $$$v$$$ ($$$u eq v$$$) exists if $$$a_u-a_v ge b_u-b_v$$$. A vertex $$$V$$$ is called strong if there exists a path from $$$V$$$ to all other vertices. A path in a directed graph is a chain of several vertices, connected by edges, such that moving from the vertex $$$u$$$, along the directions of the edges, the vertex $$$v$$$ can be reached. Your task is to find all strong vertices. For example, if $$$a=[3,1,2,4]$$$ and $$$b=[4,3,2,1]$$$, the graph will look like this: The graph has only one strong vertex with number $$$4$$$ Input The first line contains an integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 2cdot 10^5$$$)xa0— the length of $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2 dots a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1,b_2 dots b_n$$$ ($$$-10^9 le b_i le 10^9$$$)xa0— the array $$$b$$$. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output two lines: in the first line, output the number of strong vertices, and in the second line, output all strong vertices in ascending order. Example Input 5 4 3 1 2 4 4 3 2 1 5 1 2 4 1 2 5 2 3 3 1 2 1 2 2 1 3 0 2 1 1 3 2 3 5 7 4 -2 -3 -6 Output 1 4 2 3 5 1 2 3 1 2 3 2 2 3 Note The first sample is covered in the problem statement. For the second sample, the graph looks like this: The graph has two strong vertices with numbers $$$3$$$ and $$$5$$$. Note that there is a bidirectional edge between vertices $$$3$$$ and $$$5$$$. In the third sample, the vertices are connected by a single directed edge from vertex $$$2$$$ to vertex $$$1$$$, so the only strong vertex is $$$2$$$. In the fourth sample, all vertices are connected to each other by bidirectional edges, so there is a path from every vertex to any other vertex.
1,300
true
false
false
false
false
false
false
false
true
false
1,141
1303B
Your company was appointed to lay new asphalt on the highway of length $$$n$$$. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing. Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are $$$g$$$ days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next $$$b$$$ days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again $$$g$$$ good days, $$$b$$$ bad days and so on. You can be sure that you start repairing at the start of a good season, in other words, days $$$1, 2, dots, g$$$ are good. You don't really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the $$$n = 5$$$ then at least $$$3$$$ units of the highway should have high quality; if $$$n = 4$$$ then at least $$$2$$$ units should have high quality. What is the minimum number of days is needed to finish the repair of the whole highway? Input The first line contains a single integer $$$T$$$ ($$$1 le T le 10^4$$$) — the number of test cases. Next $$$T$$$ lines contain test cases — one per line. Each line contains three integers $$$n$$$, $$$g$$$ and $$$b$$$ ($$$1 le n, g, b le 10^9$$$) — the length of the highway and the number of good and bad days respectively. Output Print $$$T$$$ integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality. Example Input 3 5 1 1 8 10 10 1000000 1 1000000 Note In the first test case, you can just lay new asphalt each day, since days $$$1, 3, 5$$$ are good. In the second test case, you can also lay new asphalt each day, since days $$$1$$$-$$$8$$$ are good.
1,400
true
false
false
false
false
false
false
false
false
false
4,177
922D
Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner. While moving, the robot generates a string _t_ consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string _t_ as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (_i_,u2009_j_), that _i_u2009<u2009_j_ and and . The robot is off at the moment. Imp knows that it has a sequence of strings _t__i_ in its memory, and he can arbitrary change their order. When the robot is started, it generates the string _t_ as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation. Help Imp to find the maximum noise he can achieve by changing the order of the strings. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of strings in robot's memory. Next _n_ lines contain the strings _t_1,u2009_t_2,u2009...,u2009_t__n_, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105. Output Print a single integerxa0— the maxumum possible noise Imp can achieve by changing the order of the strings. Note The optimal concatenation in the first sample is _ssshhshhhs_.
1,800
false
true
false
false
false
false
false
false
true
false
6,026
267A
Problem - 267A - Codeforces =============== xa0 to pair (4,13), it transforms (5,5) to (0,5). You've got some number of pairs (_a__i_,u2009_b__i_). How many operations will be performed for each of them? Input The first line contains the number of pairs _n_ (1u2009u2009≤u2009u2009_n_u2009u2009≤u2009u20091000). Then follow _n_ lines, each line contains a pair of positive integers _a__i_,u2009_b__i_ (1u2009u2009≤u2009u2009_a__i_,u2009u2009_b__i_u2009u2009≤u2009u2009109). Output Print the sought number of operations for each pair on a single line. Examples Input 2 4 17 7 987654321 Output 8 141093479
900
true
false
false
false
false
false
false
false
false
false
8,766
962C
You are given a positive integer $$$n$$$, written without leading zeroes (for example, the number 04 is incorrect). In one operation you can delete any digit of the given integer so that the result remains a positive integer without leading zeros. Determine the minimum number of operations that you need to consistently apply to the given integer $$$n$$$ to make from it the square of some positive integer or report that it is impossible. An integer $$$x$$$ is the square of some positive integer if and only if $$$x=y^2$$$ for some positive integer $$$y$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^{9}$$$). The number is given without leading zeroes. Output If it is impossible to make the square of some positive integer from $$$n$$$, print -1. In the other case, print the minimal number of operations required to do it. Note In the first example we should delete from $$$8314$$$ the digits $$$3$$$ and $$$4$$$. After that $$$8314$$$ become equals to $$$81$$$, which is the square of the integer $$$9$$$. In the second example the given $$$625$$$ is the square of the integer $$$25$$$, so you should not delete anything. In the third example it is impossible to make the square from $$$333$$$, so the answer is -1.
1,400
true
false
true
false
false
false
true
false
false
false
5,854
875F
In a medieval kingdom, the economic crisis is raging. Milk drops fall, Economic indicators are deteriorating every day, money from the treasury disappear. To remedy the situation, King Charles Sunnyface decided make his _n_ sons-princes marry the brides with as big dowry as possible. In search of candidates, the king asked neighboring kingdoms, and after a while several delegations arrived with _m_ unmarried princesses. Receiving guests, Karl learned that the dowry of the _i_ th princess is _w__i_ of golden coins. Although the action takes place in the Middle Ages, progressive ideas are widespread in society, according to which no one can force a princess to marry a prince whom she does not like. Therefore, each princess has an opportunity to choose two princes, for each of which she is ready to become a wife. The princes were less fortunate, they will obey the will of their father in the matter of choosing a bride. Knowing the value of the dowry and the preferences of each princess, Charles wants to play weddings in such a way that the total dowry of the brides of all his sons would be as great as possible. At the same time to marry all the princes or princesses is not necessary. Each prince can marry no more than one princess, and vice versa, each princess can marry no more than one prince. Help the king to organize the marriage of his sons in the most profitable way for the treasury. Input The first line contains two integers _n_, _m_ (2u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_m_u2009≤u2009200u2009000)xa0— number of princes and princesses respectively. Each of following _m_ lines contains three integers _a__i_, _b__i_, _w__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_, 1u2009≤u2009_w__i_u2009≤u200910u2009000)xa0— number of princes, which _i_-th princess is ready to marry and the value of her dowry.
2,500
false
true
false
false
false
false
false
false
false
true
6,243
1548B
British mathematician John Littlewood once said about Indian mathematician Srinivasa Ramanujan that "every positive integer was one of his personal friends." It turns out that positive integers can also be friends with each other! You are given an array $$$a$$$ of distinct positive integers. Define a subarray $$$a_i, a_{i+1}, ldots, a_j$$$ to be a friend group if and only if there exists an integer $$$m ge 2$$$ such that $$$a_i bmod m = a_{i+1} bmod m = ldots = a_j bmod m$$$, where $$$x bmod y$$$ denotes the remainder when $$$x$$$ is divided by $$$y$$$. Your friend Gregor wants to know the size of the largest friend group in $$$a$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2cdot 10^4$$$). Each test case begins with a line containing the integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$), the size of the array $$$a$$$. The next line contains $$$n$$$ positive integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le {10}^{18}$$$), representing the contents of the array $$$a$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases is less than $$$2cdot 10^5$$$. Output Your output should consist of $$$t$$$ lines. Each line should consist of a single integer, the size of the largest friend group in $$$a$$$. Example Input 4 5 1 5 2 4 6 4 8 2 5 10 2 1000 2000 8 465 55 3 54 234 12 45 78 Note In the first test case, the array is $$$[1,5,2,4,6]$$$. The largest friend group is $$$[2,4,6]$$$, since all those numbers are congruent to $$$0$$$ modulo $$$2$$$, so $$$m=2$$$. In the second test case, the array is $$$[8,2,5,10]$$$. The largest friend group is $$$[8,2,5]$$$, since all those numbers are congruent to $$$2$$$ modulo $$$3$$$, so $$$m=3$$$. In the third case, the largest friend group is $$$[1000,2000]$$$. There are clearly many possible values of $$$m$$$ that work.
1,800
true
false
false
false
true
false
false
true
false
false
2,905
1045A
It is the year 2969. 1000 years have passed from the moon landing. Meanwhile, the humanity colonized the Hyperspace™ and lived in harmony. Until we realized that we were not alone. Not too far away from the Earth, the massive fleet of aliens' spaceships is preparing to attack the Earth. For the first time in a while, the humanity is in real danger. Crisis and panic are everywhere. The scientists from all around the solar system have met and discussed the possible solutions. However, no progress has been made. The Earth's last hope is YOU! Fortunately, the Earth is equipped with very powerful defense systems made by MDCS. There are $$$N$$$ aliens' spaceships which form the line. The defense system consists of three types of weapons: SQL rockets – every SQL rocket can destroy at most one spaceship in the given set. Cognition beams – every Cognition beam has an interval $$$[l,r]$$$ and can destroy at most one spaceship in that interval. OMG bazooka – every OMG bazooka has three possible targets, however, each bazooka can destroy either zero or exactly two spaceships. In addition, due to the smart targeting system, the sets of the three possible targets of any two different OMG bazookas are disjoint (that means that every ship is targeted with at most one OMG bazooka). Your task is to make a plan of the attack which will destroy the largest possible number of spaceships. Every destroyed spaceship should be destroyed with exactly one weapon. Input The first line contains two integers: the number of your weapons $$$N$$$ $$$(1leq Nleq 5000)$$$ and the number of spaceships $$$M$$$ $$$(1leq Mleq 5000)$$$. In the next $$$N$$$ lines, each line starts with one integer that represents type (either 0, 1 or 2). If the type is 0, then the weapon is SQL rocket, the rest of the line contains strictly positive number $$$K$$$ $$$(sum{K} leq 100 000)$$$ and array $$$k_i$$$ $$$(1leq k_ileq M)$$$ of $$$K$$$ integers. If the type is 1, then the weapon is Cognition beam, the rest of the line contains integers $$$l$$$ and $$$r$$$ $$$(1leq lleq rleq M)$$$. If the type is 2 then the weapon is OMG bazooka, the rest of the line contains distinct numbers $$$a$$$, $$$b$$$ and $$$c$$$ $$$ (1 leq a,b,c leq M)$$$. Output The first line should contain the maximum number of destroyed spaceships — $$$X$$$. In the next $$$X$$$ lines, every line should contain two numbers $$$A$$$ and $$$B$$$, where $$$A$$$ is an index of the weapon and $$$B$$$ is an index of the spaceship which was destroyed by the weapon $$$A$$$. Example Input 3 5 0 1 4 2 5 4 1 1 1 4 Note SQL rocket can destroy only 4th spaceship. OMG Bazooka can destroy two of 1st, 4th or 5th spaceship, and Cognition beam can destroy any spaceship from the interval $$$[1,4]$$$. The maximum number of destroyed spaceship is 4, and one possible plan is that SQL rocket should destroy 4th spaceship, OMG bazooka should destroy 1st and 5th spaceship and Cognition beam should destroy 2nd spaceship.
2,500
false
false
false
false
true
false
false
false
false
true
5,470
1282B2
This is the hard version of this problem. The only difference is the constraint on $$$k$$$ — the number of gifts in the offer. In this version: $$$2 le k le n$$$. Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very luckyxa0— today the offer "$$$k$$$ of goods for the price of one" is held in store. Using this offer, Vasya can buy exactly $$$k$$$ of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has. More formally, for each good, its price is determined by $$$a_i$$$xa0— the number of coins it costs. Initially, Vasya has $$$p$$$ coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary: Vasya can buy one good with the index $$$i$$$ if he currently has enough coins (i.e $$$p ge a_i$$$). After buying this good, the number of Vasya's coins will decrease by $$$a_i$$$, (i.e it becomes $$$p := p - a_i$$$). Vasya can buy a good with the index $$$i$$$, and also choose exactly $$$k-1$$$ goods, the price of which does not exceed $$$a_i$$$, if he currently has enough coins (i.e $$$p ge a_i$$$). Thus, he buys all these $$$k$$$ goods, and his number of coins decreases by $$$a_i$$$ (i.e it becomes $$$p := p - a_i$$$). Please note that each good can be bought no more than once. For example, if the store now has $$$n=5$$$ goods worth $$$a_1=2, a_2=4, a_3=3, a_4=5, a_5=7$$$, respectively, $$$k=2$$$, and Vasya has $$$6$$$ coins, then he can buy $$$3$$$ goods. A good with the index $$$1$$$ will be bought by Vasya without using the offer and he will pay $$$2$$$ coins. Goods with the indices $$$2$$$ and $$$3$$$ Vasya will buy using the offer and he will pay $$$4$$$ coins. It can be proved that Vasya can not buy more goods with six coins. Help Vasya to find out the maximum number of goods he can buy. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases in the test. The next lines contain a description of $$$t$$$ test cases. The first line of each test case contains three integers $$$n, p, k$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le p le 2cdot10^9$$$, $$$2 le k le n$$$)xa0— the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 10^4$$$)xa0— the prices of goods. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$.
1,600
false
true
false
true
false
false
false
false
true
false
4,284
1613A
Problem - 1613A - 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 *900 No tag edit access → Contest materials xa0— the number of testcases. The first line of each testcase contains two integers $$$x_1$$$ and $$$p_1$$$ ($$$1 le x_1 le 10^6; 0 le p_1 le 10^6$$$)xa0— the description of the first number. The second line of each testcase contains two integers $$$x_2$$$ and $$$p_2$$$ ($$$1 le x_2 le 10^6; 0 le p_2 le 10^6$$$)xa0— the description of the second number. Output For each testcase print the result of the comparison of the given two numbers. If the first number is smaller than the second one, print '<'. If the first number is greater than the second one, print '>'. If they are equal, print '='. Example Input 5 2 1 19 0 10 2 100 1 1999 0 2 3 1 0 1 0 99 0 1 2 Output > = < = < Note The comparisons in the example are: $$$20 > 19$$$, $$$1000 = 1000$$$, $$$1999 < 2000$$$, $$$1 = 1$$$, $$$99 < 100$$$.
900
true
false
true
false
false
false
false
false
false
false
2,576
601A
In Absurdistan, there are _n_ towns (numbered 1 through _n_) and _m_ bidirectional railways. There is also an absurdly simple road networkxa0— for each pair of different towns _x_ and _y_, there is a bidirectional road between towns _x_ and _y_ if and only if there is no railway between them. Travelling to a different town using one railway or one road always takes exactly one hour. A train and a bus leave town 1 at the same time. They both have the same destination, town _n_, and don't make any stops on the way (but they can wait in town _n_). The train can move only along railways and the bus can move only along roads. You've been asked to plan out routes for the vehicles; each route can use any road/railway multiple times. One of the most important aspects to consider is safetyxa0— in order to avoid accidents at railway crossings, the train and the bus must not arrive at the same town (except town _n_) simultaneously. Under these constraints, what is the minimum number of hours needed for both vehicles to reach town _n_ (the maximum of arrival times of the bus and the train)? Note, that bus and train are not required to arrive to the town _n_ at the same moment of time, but are allowed to do so. Input The first line of the input contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009400, 0u2009≤u2009_m_u2009≤u2009_n_(_n_u2009-u20091)u2009/u20092)xa0— the number of towns and the number of railways respectively. Each of the next _m_ lines contains two integers _u_ and _v_, denoting a railway between towns _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_, _u_u2009≠u2009_v_). You may assume that there is at most one railway connecting any two towns. Output Output one integerxa0— the smallest possible time of the later vehicle's arrival in town _n_. If it's impossible for at least one of the vehicles to reach town _n_, output u2009-u20091. Examples Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Note In the first sample, the train can take the route and the bus can take the route . Note that they can arrive at town 4 at the same time. In the second sample, Absurdistan is ruled by railwaymen. There are no roads, so there's no way for the bus to reach town 4.
1,600
false
false
false
false
false
false
false
false
false
true
7,440
1749C
Alice and Bob are playing a game. They have an array of positive integers $$$a$$$ of size $$$n$$$. Before starting the game, Alice chooses an integer $$$k ge 0$$$. The game lasts for $$$k$$$ stages, the stages are numbered from $$$1$$$ to $$$k$$$. During the $$$i$$$-th stage, Alice must remove an element from the array that is less than or equal to $$$k - i + 1$$$. After that, if the array is not empty, Bob must add $$$k - i + 1$$$ to an arbitrary element of the array. Note that both Alice's move and Bob's move are two parts of the same stage of the game. If Alice can't delete an element during some stage, she loses. If the $$$k$$$-th stage ends and Alice hasn't lost yet, she wins. Your task is to determine the maximum value of $$$k$$$ such that Alice can win if both players play optimally. Bob plays against Alice, so he tries to make her lose the game, if it's possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). Output For each test case, print one integerxa0— the maximum value of $$$k$$$ such that Alice can win if both players play optimally. Example Input 4 3 1 1 2 4 4 4 4 4 1 1 5 1 3 2 1 1
1,400
false
true
true
false
true
false
false
true
false
false
1,789
1899A
Problem - 1899A - 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 games math number theory *800 No tag edit access → Contest materials ") ") xa0— the number of test cases. The single line of each test case contains the integer $$$n$$$ ($$$1 leq n leq 1000$$$). Output For each test case, print "First" without quotes if Vanya wins, and "Second" without quotes if Vova wins. Example Input 6 1 3 5 100 999 1000 Output First Second First First Second First
800
true
false
false
false
false
false
false
false
false
false
922
739B
Alyona has a tree with _n_ vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex _i_ she wrote _a__i_. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges). Let's define _dist_(_v_,u2009_u_) as the sum of the integers written on the edges of the simple path from _v_ to _u_. The vertex _v_ controls the vertex _u_ (_v_u2009≠u2009_u_) if and only if _u_ is in the subtree of _v_ and _dist_(_v_,u2009_u_)u2009≤u2009_a__u_. Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex _v_ what is the number of vertices _u_ such that _v_ controls _u_. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u20092·105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the integers written in the vertices. The next (_n_u2009-u20091) lines contain two integers each. The _i_-th of these lines contains integers _p__i_ and _w__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_, 1u2009≤u2009_w__i_u2009≤u2009109)xa0— the parent of the (_i_u2009+u20091)-th vertex in the tree and the number written on the edge between _p__i_ and (_i_u2009+u20091). It is guaranteed that the given graph is a tree. Output Print _n_ integersxa0— the _i_-th of these numbers should be equal to the number of vertices that the _i_-th vertex controls. Examples Input 5 2 5 1 4 6 1 7 1 1 3 5 3 6 Input 5 9 7 8 6 5 1 1 2 1 3 1 4 1 Note In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1 controls the vertex 5).
1,900
false
false
false
false
true
false
false
true
false
true
6,842
842C
Problem - 842C - 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 dfs and similar graphs math number theory trees *2000 No tag edit access → Contest materials ") ") . Next line contains _n_ integer numbers _a__i_ (1u2009≤u2009_i_u2009≤u2009_n_, 1u2009≤u2009_a__i_u2009≤u20092·105). Each of next _n_u2009-u20091 lines contains two integer numbers _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_, _x_u2009≠u2009_y_), which means that there is an edge (_x_,u2009_y_) in the tree. Output Output _n_ numbers separated by spaces, where _i_-th number equals to maximum possible beauty of vertex _i_. Examples Input 2 6 2 1 2 Output 6 6 Input 3 6 2 3 1 2 1 3 Output 6 6 6 Input 1 10 Output 10
2,000
true
false
false
false
false
false
false
false
false
true
6,394
125D
An arithmetic progression is such a non-empty sequence of numbers where the difference between any two successive numbers is constant. This constant number is called common difference. For example, the sequence 3, 7, 11, 15 is an arithmetic progression. The definition implies that any sequences whose length equals 1 or 2 are arithmetic and all sequences whose length equals 0 are non-arithmetic. You are given a sequence of different integers _a_1,u2009_a_2,u2009...,u2009_a__n_. You should either split it into two arithmetic progressions or find out that the operation is impossible to perform. Splitting assigns each member of the given sequence to one of two progressions, but the relative order of numbers does not change. Splitting is an inverse operation to merging. Input The first line contains a positive integer _n_ (2u2009≤u2009_n_u2009≤u200930000), _n_ is the length of the given sequence. The second line contains elements of the given sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009108u2009≤u2009_a__i_u2009≤u2009108). The elements of the progression are different integers. Output Print the required arithmetic progressions, one per line. The progressions can be positioned in any order. Each progression should contain at least one number. If there's no solution, then print "No solution" (without the quotes)in the only line of the input file. If there are several solutions, print any of them. Note In the second sample another solution is also possible (number three can be assigned to the second progression): 1, 2 and 3, -2, -7.
2,200
false
true
false
false
false
true
false
false
false
false
9,379
1526E
Once upon a time, Oolimry saw a suffix array. He wondered how many strings can produce this suffix array. More formally, given a suffix array of length $$$n$$$ and having an alphabet size $$$k$$$, count the number of strings that produce such a suffix array. Let $$$s$$$ be a string of length $$$n$$$. Then the $$$i$$$-th suffix of $$$s$$$ is the substring $$$s[i ldots n-1]$$$. A suffix array is the array of integers that represent the starting indexes of all the suffixes of a given string, after the suffixes are sorted in the lexicographic order. For example, the suffix array of oolimry is $$$[3,2,4,1,0,5,6]$$$ as the array of sorted suffixes is $$$[ exttt{imry}, exttt{limry}, exttt{mry}, exttt{olimry}, exttt{oolimry}, exttt{ry}, exttt{y}]$$$. A string $$$x$$$ is lexicographically smaller than string $$$y$$$, if either $$$x$$$ is a prefix of $$$y$$$ (and $$$x eq y$$$), or there exists such $$$i$$$ that $$$x_i < y_i$$$, and for any $$$1leq j < i$$$ , $$$x_j = y_j$$$. Input The first line contain 2 integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 200000,1 leq k leq 200000$$$) — the length of the suffix array and the alphabet size respectively. The second line contains $$$n$$$ integers $$$s_0, s_1, s_2, ldots, s_{n-1}$$$ ($$$0 leq s_i leq n-1$$$) where $$$s_i$$$ is the $$$i$$$-th element of the suffix array i.e. the starting position of the $$$i$$$-th lexicographically smallest suffix. It is guaranteed that for all $$$0 leq i< j leq n-1$$$, $$$s_i eq s_j$$$. Note In the first test case, "abb" is the only possible solution. In the second test case, it can be easily shown no possible strings exist as all the letters have to be equal. In the fourth test case, one possible string is "ddbacef". Please remember to print your answers modulo $$$998244353$$$.
2,400
true
false
false
false
false
true
false
false
false
false
3,031
1149C
Owl Pacino has always been into trees — unweighted rooted trees in particular. He loves determining the diameter of every tree he sees — that is, the maximum length of any simple path in the tree. Owl Pacino's owl friends decided to present him the Tree Generator™ — a powerful machine creating rooted trees from their descriptions. An $$$n$$$-vertex rooted tree can be described by a bracket sequence of length $$$2(n - 1)$$$ in the following way: find any walk starting and finishing in the root that traverses each edge exactly twice — once down the tree, and later up the tree. Then follow the path and write down "(" (an opening parenthesis) if an edge is followed down the tree, and ")" (a closing parenthesis) otherwise. The following figure shows sample rooted trees and their descriptions: Owl wrote down the description of an $$$n$$$-vertex rooted tree. Then, he rewrote the description $$$q$$$ times. However, each time he wrote a new description, he picked two different characters in the description he wrote the last time, swapped them and wrote down the resulting string. He always made sure that each written string was the description of a rooted tree. Pacino then used Tree Generator™ for each description he wrote down. What is the diameter of each constructed tree? Input The first line of the input contains two integers $$$n, q$$$ ($$$3 le n le 100,000$$$, $$$1 le q le 100,000$$$) — the number of vertices in the tree and the number of changes to the tree description. The following line contains a description of the initial tree — a string of length $$$2(n-1)$$$ consisting of opening and closing parentheses. Each of the following $$$q$$$ lines describes a single change to the description and contains two space-separated integers $$$a_i, b_i$$$ ($$$2 leq a_i, b_i leq 2n-3$$$) which identify the indices of two brackets to be swapped. You can assume that the description will change after each query, and that after each change a tree can be constructed from the description. Output Output $$$q + 1$$$ integers — the diameter of each constructed tree, in the order their descriptions have been written down. Examples Input 5 5 (((()))) 4 5 3 4 5 6 3 6 2 5 Input 6 4 (((())())) 6 7 5 4 6 4 7 4 Note The following figure shows each constructed tree and its description in the first example test:
2,700
false
false
true
false
true
false
false
false
false
false
4,967
1603B
Problem - 1603B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms math number theory *1600 No tag edit access → Contest materials xa0— the number of test cases. The first and only line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$2 le x, y le 10^9$$$, both are even). Output For each test case, print a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^{18}$$$) that satisfies the condition mentioned in the statement. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints. Example Input 4 4 8 4 2 420 420 69420 42068 Output 4 10 420 9969128 Note In the first test case, $$$4 bmod 4 = 8 bmod 4 = 0$$$. In the second test case, $$$10 bmod 4 = 2 bmod 10 = 2$$$. In the third test case, $$$420 bmod 420 = 420 bmod 420 = 0$$$.
1,600
true
false
false
false
false
true
false
false
false
false
2,642
581C
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has _n_ different skills, each of which is characterized by an integer _a__i_ from 0 to 100. The higher the number _a__i_ is, the higher is the _i_-th skill of the character. The total rating of the character is calculated as the sum of the values u200bu200bof for all _i_ from 1 to _n_. The expression ⌊ _x_⌋ denotes the result of rounding the number _x_ down to the nearest integer. At the beginning of the game Petya got _k_ improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if _a_4u2009=u200946, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused. Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units. Input The first line of the input contains two positive integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_k_u2009≤u2009107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009100), where _a__i_ characterizes the level of the _i_-th skill of the character. Output The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using _k_ or less improvement units. Note In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to _lfloor_ _frac_{100}{10} _rfloor_u2009+u2009 _lfloor_ _frac_{100}{10} _rfloor_u2009=u200910u2009+u200910u2009=u2009 20. In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is . In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to .
1,400
true
false
true
false
false
false
false
false
true
false
7,521
445B
DZY loves chemistry, and he enjoys mixing chemicals. DZY has _n_ chemicals, and _m_ pairs of them will react. He wants to pour these chemicals into a test tube, and he needs to pour them in one by one, in any order. Let's consider the danger of a test tube. Danger of an empty test tube is 1. And every time when DZY pours a chemical, if there are already one or more chemicals in the test tube that can react with it, the danger of the test tube will be multiplied by 2. Otherwise the danger remains as it is. Find the maximum possible danger after pouring all the chemicals one by one in optimal order. Input The first line contains two space-separated integers _n_ and _m_ . Each of the next _m_ lines contains two space-separated integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009<u2009_y__i_u2009≤u2009_n_). These integers mean that the chemical _x__i_ will react with the chemical _y__i_. Each pair of chemicals will appear at most once in the input. Consider all the chemicals numbered from 1 to _n_ in some order. Output Print a single integer — the maximum possible danger. Note In the first sample, there's only one way to pour, and the danger won't increase. In the second sample, no matter we pour the 1st chemical first, or pour the 2nd chemical first, the answer is always 2. In the third sample, there are four ways to achieve the maximum possible danger: 2-1-3, 2-3-1, 1-2-3 and 3-2-1 (that is the numbers of the chemicals in order of pouring).
1,400
false
true
false
false
false
false
false
false
false
false
8,057
1525B
You are given a permutation $$$a$$$ consisting of $$$n$$$ numbers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array in which each element from $$$1$$$ to $$$n$$$ occurs exactly once). You can perform the following operation: choose some subarray (contiguous subsegment) of $$$a$$$ and rearrange the elements in it in any way you want. But this operation cannot be applied to the whole array. For example, if $$$a = [2, 1, 4, 5, 3]$$$ and we want to apply the operation to the subarray $$$a[2, 4]$$$ (the subarray containing all elements from the $$$2$$$-nd to the $$$4$$$-th), then after the operation, the array can become $$$a = [2, 5, 1, 4, 3]$$$ or, for example, $$$a = [2, 1, 5, 4, 3]$$$. Your task is to calculate the minimum number of operations described above to sort the permutation $$$a$$$ in ascending order. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0— the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$3 le n le 50$$$)xa0— the number of elements in the permutation. The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$xa0— the given permutation $$$a$$$. Output For each test case, output a single integerxa0— the minimum number of operations described above to sort the array $$$a$$$ in ascending order. Example Input 3 4 1 3 2 4 3 1 2 3 5 2 1 4 5 3 Note In the explanations, $$$a[i, j]$$$ defines the subarray of $$$a$$$ that starts from the $$$i$$$-th element and ends with the $$$j$$$-th element. In the first test case of the example, you can select the subarray $$$a[2, 3]$$$ and swap the elements in it. In the second test case of the example, the permutation is already sorted, so you don't need to apply any operations. In the third test case of the example, you can select the subarray $$$a[3, 5]$$$ and reorder the elements in it so $$$a$$$ becomes $$$[2, 1, 3, 4, 5]$$$, and then select the subarray $$$a[1, 2]$$$ and swap the elements in it, so $$$a$$$ becomes $$$[1, 2, 3, 4, 5]$$$.
900
false
true
false
false
false
true
false
false
false
false
3,041
638C
In Berland there are _n_ cities and _n_u2009-u20091 bidirectional roads. Each road connects some pair of cities, from any city you can get to any other one using only the given roads. In each city there is exactly one repair brigade. To repair some road, you need two teams based in the cities connected by the road to work simultaneously for one day. Both brigades repair one road for the whole day and cannot take part in repairing other roads on that day. But the repair brigade can do nothing on that day. Determine the minimum number of days needed to repair all the roads. The brigades cannot change the cities where they initially are. Input The first line of the input contains a positive integer _n_ (2u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of cities in Berland. Each of the next _n_u2009-u20091 lines contains two numbers _u__i_, _v__i_, meaning that the _i_-th road connects city _u__i_ and city _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_). Output First print number _k_xa0— the minimum number of days needed to repair all the roads in Berland. In next _k_ lines print the description of the roads that should be repaired on each of the _k_ days. On the _i_-th line print first number _d__i_ — the number of roads that should be repaired on the _i_-th day, and then _d__i_ space-separated integers — the numbers of the roads that should be repaired on the _i_-th day. The roads are numbered according to the order in the input, starting from one. If there are multiple variants, you can print any of them. Note In the first sample you can repair all the roads in two days, for example, if you repair roads 1 and 2 on the first day and road 3 — on the second day.
1,800
false
true
false
false
false
false
false
false
false
true
7,259
841A
Problem - 841A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation *900 No tag edit access → Contest materials — the number of baloons and friends. Next line contains string _s_ — colors of baloons. Output Answer to the task — «YES» or «NO» in a single line. You can choose the case (lower or upper) for each letter arbitrary. Examples Input 4 2 aabb Output YES Input 6 3 aacaab Output NO Note In the first sample Kefa can give 1-st and 3-rd baloon to the first friend, and 2-nd and 4-th to the second. In the second sample Kefa needs to give to all his friends baloons of color a, but one baloon will stay, thats why answer is «NO».
900
false
false
true
false
false
false
true
false
false
false
6,398
1183G
This problem is a version of problem D from the same contest with some additional constraints and tasks. There are $$$n$$$ candies in a candy box. The type of the $$$i$$$-th candy is $$$a_i$$$ ($$$1 le a_i le n$$$). You have to prepare a gift using some of these candies with the following restriction: the numbers of candies of each type presented in a gift should be all distinct (i.u2009e. for example, a gift having two candies of type $$$1$$$ and two candies of type $$$2$$$ is bad). It is possible that multiple types of candies are completely absent from the gift. It is also possible that not all candies of some types will be taken to a gift. You really like some of the candies and don't want to include them into the gift, but you want to eat them yourself instead. For each candy, a number $$$f_i$$$ is given, which is equal to $$$0$$$ if you really want to keep $$$i$$$-th candy for yourself, or $$$1$$$ if you don't mind including it into your gift. It is possible that two candies of the same type have different values of $$$f_i$$$. You want your gift to be as large as possible, but you don't want to include too many of the candies you want to eat into the gift. So, you want to calculate the maximum possible number of candies that can be included into a gift, and among all ways to choose maximum number of candies, you want to maximize the number of candies having $$$f_i = 1$$$ in your gift. You have to answer $$$q$$$ independent queries. If you are Python programmer, consider using PyPy instead of Python when you submit your code. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) — the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of candies. Then $$$n$$$ lines follow, each containing two integers $$$a_i$$$ and $$$f_i$$$ ($$$1 le a_i le n$$$, $$$0 le f_i le 1$$$), where $$$a_i$$$ is the type of the $$$i$$$-th candy, and $$$f_i$$$ denotes whether you want to keep the $$$i$$$-th candy for yourself ($$$0$$$ if you want to keep it, $$$1$$$ if you don't mind giving it away). It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$.
2,000
false
true
true
false
false
false
false
false
true
false
4,796
437A
Once upon a time a child got a test consisting of multiple-choice questions as homework. A multiple-choice question consists of four choices: A, B, C and D. Each choice has a description, and the child should find out the only one that is correct. Fortunately the child knows how to solve such complicated test. The child will follow the algorithm: If there is some choice whose description at least twice shorter than all other descriptions, or at least twice longer than all other descriptions, then the child thinks the choice is great. If there is exactly one great choice then the child chooses it. Otherwise the child chooses C (the child think it is the luckiest choice). You are given a multiple-choice questions, can you predict child's choose? Input The first line starts with "A." (without quotes), then followed the description of choice A. The next three lines contains the descriptions of the other choices in the same format. They are given in order: B, C, D. Please note, that the description goes after prefix "X.", so the prefix mustn't be counted in description's length. Each description is non-empty and consists of at most 100 characters. Each character can be either uppercase English letter or lowercase English letter, or "_". Output Print a single line with the child's choice: "A", "B", "C" or "D" (without quotes). Examples Input A.VFleaKing_is_the_author_of_this_problem B.Picks_is_the_author_of_this_problem C.Picking_is_the_author_of_this_problem D.Ftiasch_is_cute Input A.ab B.abcde C.ab D.abc Note In the first sample, the first choice has length 39, the second one has length 35, the third one has length 37, and the last one has length 15. The choice D (length 15) is twice shorter than all other choices', so it is great choice. There is no other great choices so the child will choose D. In the second sample, no choice is great, so the child will choose the luckiest choice C. In the third sample, the choice B (length 2) is twice longer than all other choices', so it is great choice. There is no other great choices so the child will choose B.
1,300
false
false
true
false
false
false
false
false
false
false
8,091
697A
Ted has a pineapple. This pineapple is able to bark like a bulldog! At time _t_ (in seconds) it barks for the first time. Then every _s_ seconds after it, it barks twice with 1 second interval. Thus it barks at times _t_, _t_u2009+u2009_s_, _t_u2009+u2009_s_u2009+u20091, _t_u2009+u20092_s_, _t_u2009+u20092_s_u2009+u20091, etc. Barney woke up in the morning and wants to eat the pineapple, but he can't eat it when it's barking. Barney plans to eat it at time _x_ (in seconds), so he asked you to tell him if it's gonna bark at that time. Input The first and only line of input contains three integers _t_, _s_ and _x_ (0u2009≤u2009_t_,u2009_x_u2009≤u2009109, 2u2009≤u2009_s_u2009≤u2009109)xa0— the time the pineapple barks for the first time, the pineapple barking interval, and the time Barney wants to eat the pineapple respectively. Output Print a single "YES" (without quotes) if the pineapple will bark at time _x_ or a single "NO" (without quotes) otherwise in the only line of output. Note In the first and the second sample cases pineapple will bark at moments 3, 13, 14, ..., so it won't bark at the moment 4 and will bark at the moment 3. In the third and fourth sample cases pineapple will bark at moments 3, 11, 12, 19, 20, 27, 28, 35, 36, 43, 44, 51, 52, 59, ..., so it will bark at both moments 51 and 52.
900
true
false
true
false
false
false
false
false
false
false
7,035
1605E
Jeevan has two arrays $$$a$$$ and $$$b$$$ of size $$$n$$$. He is fond of performing weird operations on arrays. This time, he comes up with two types of operations: Choose any $$$i$$$ ($$$1 le i le n$$$) and increment $$$a_j$$$ by $$$1$$$ for every $$$j$$$ which is a multiple of $$$i$$$ and $$$1 le j le n$$$. Choose any $$$i$$$ ($$$1 le i le n$$$) and decrement $$$a_j$$$ by $$$1$$$ for every $$$j$$$ which is a multiple of $$$i$$$ and $$$1 le j le n$$$. He wants to convert array $$$a$$$ into an array $$$b$$$ using the minimum total number of operations. However, Jeevan seems to have forgotten the value of $$$b_1$$$. So he makes some guesses. He will ask you $$$q$$$ questions corresponding to his $$$q$$$ guesses, the $$$i$$$-th of which is of the form: If $$$b_1 = x_i$$$, what is the minimum number of operations required to convert $$$a$$$ to $$$b$$$? Help him by answering each question. Input The first line contains a single integer $$$n$$$ $$$(1 le n le 2 cdot 10^{5})$$$ xa0— the size of arrays $$$a$$$ and $$$b$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 le a_i le 10^6)$$$. The third line contains $$$n$$$ integers $$$b_1, b_2, ..., b_n$$$ $$$(1 le b_i le 10^6$$$ for $$$i eq 1$$$; $$$b_1 = -1$$$, representing that the value of $$$b_1$$$ is unknown$$$)$$$. The fourth line contains a single integer $$$q$$$ $$$(1 le q le 2 cdot 10^{5})$$$ xa0— the number of questions. Each of the following $$$q$$$ lines contains a single integer $$$x_i$$$ $$$(1 le x_i le 10^6)$$$ xa0— representing the $$$i$$$-th question. Output Output $$$q$$$ integers xa0— the answers to each of his $$$q$$$ questions. Examples Input 6 2 5 4 1 3 6 -1 4 6 2 3 5 3 1 8 4 Note Consider the first test case. $$$b_1 = 1$$$: We need to convert $$$[3, 7] ightarrow [1, 5]$$$. We can perform the following operations:$$$[3, 7]$$$ $$$xrightarrow[ ext{decrease}]{ ext{i = 1}}$$$ $$$[2, 6]$$$ $$$xrightarrow[ ext{decrease}]{ ext{i = 1}}$$$ $$$[1, 5]$$$ Hence the answer is $$$2$$$. $$$b_1 = 4$$$: We need to convert $$$[3, 7] ightarrow [4, 5]$$$. We can perform the following operations:$$$[3, 7]$$$ $$$xrightarrow[ ext{decrease}]{ ext{i = 2}}$$$ $$$[3, 6]$$$ $$$xrightarrow[ ext{decrease}]{ ext{i = 2}}$$$ $$$[3, 5]$$$ $$$xrightarrow[ ext{increase}]{ ext{i = 1}}$$$ $$$[4, 6]$$$ $$$xrightarrow[ ext{decrease}]{ ext{i = 2}}$$$ $$$[4, 5]$$$ Hence the answer is $$$4$$$. $$$b_1 = 3$$$: We need to convert $$$[3, 7] ightarrow [3, 5]$$$. We can perform the following operations:$$$[3, 7]$$$ $$$xrightarrow[ ext{decrease}]{ ext{i = 2}}$$$ $$$[3, 6]$$$ $$$xrightarrow[ ext{decrease}]{ ext{i = 2}}$$$ $$$[3, 5]$$$ Hence the answer is $$$2$$$.
2,400
true
true
true
false
false
false
false
true
true
false
2,631
652C
You are given a permutation _p_ of length _n_. Also you are given _m_ foe pairs (_a__i_,u2009_b__i_) (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_). Your task is to count the number of different intervals (_x_,u2009_y_) (1u2009≤u2009_x_u2009≤u2009_y_u2009≤u2009_n_) that do not contain any foe pairs. So you shouldn't count intervals (_x_,u2009_y_) that contain at least one foe pair in it (the positions and order of the values from the foe pair are not important). Consider some example: _p_u2009=u2009[1,u20093,u20092,u20094] and foe pairs are {(3,u20092),u2009(4,u20092)}. The interval (1,u20093) is incorrect because it contains a foe pair (3,u20092). The interval (1,u20094) is also incorrect because it contains two foe pairs (3,u20092) and (4,u20092). But the interval (1,u20092) is correct because it doesn't contain any foe pair. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20093·105) — the length of the permutation _p_ and the number of foe pairs. The second line contains _n_ distinct integers _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_) — the elements of the permutation _p_. Each of the next _m_ lines contains two integers (_a__i_,u2009_b__i_) (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_) — the _i_-th foe pair. Note a foe pair can appear multiple times in the given list. Output Print the only integer _c_ — the number of different intervals (_x_,u2009_y_) that does not contain any foe pairs. Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. Examples Input 9 5 9 7 2 3 1 4 6 5 8 1 6 4 5 2 7 7 2 2 7 Note In the first example the intervals from the answer are (1,u20091), (1,u20092), (2,u20092), (3,u20093) and (4,u20094).
1,800
false
false
false
false
false
false
false
false
true
false
7,208
1671D
You are given a sequence of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. You are also given $$$x$$$ integers $$$1, 2, dots, x$$$. You are asked to insert each of the extra integers into the sequence $$$a$$$. Each integer can be inserted at the beginning of the sequence, at the end of the sequence, or between any elements of the sequence. The score of the resulting sequence $$$a'$$$ is the sum of absolute differences of adjacent elements in it $$$left(sum limits_{i=1}^{n+x-1} a'_i - a'_{i+1} ight)$$$. What is the smallest possible score of the resulting sequence $$$a'$$$? 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 two integers $$$n$$$ and $$$x$$$ ($$$1 le n, x le 2 cdot 10^5$$$)xa0— the length of the sequence and the number of extra integers. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the smallest sum of absolute differences of adjacent elements of the sequence after you insert the extra integers into it. Example Input 4 1 5 10 3 8 7 2 10 10 2 6 1 5 7 3 3 9 10 10 1 4 10 1 3 1 2 Note Here are the sequences with the smallest scores for the example. The underlined elements are the extra integers. Note that there exist other sequences with this smallest score. $$$underline{1}, underline{2}, underline{3}, underline{4}, underline{5}, 10$$$ $$$underline{7}, 7, underline{6}, underline{4}, 2, underline{2}, underline{1}, underline{3}, underline{5}, underline{8}, 10$$$ $$$6, underline{1}, 1, underline{2}, 5, 7, 3, 3, 9, 10, 10, 1$$$ $$$1, 3, underline{1}, 1, 2, underline{2}, underline{3}, underline{4}, underline{5}, underline{6}, underline{7}, underline{8}, underline{9}, underline{10}$$$
1,600
false
true
false
false
false
true
true
false
false
false
2,248
1013B
There is an array with _n_ elements _a_1,u2009_a_2,u2009...,u2009_a__n_ and the number _x_. In one operation you can select some _i_ (1u2009≤u2009_i_u2009≤u2009_n_) and replace element _a__i_ with _a__i_u2009&u2009_x_, where & denotes the . In other words, there should be at least two distinct indices _i_u2009≠u2009_j_ such that _a__i_u2009=u2009_a__j_. Determine whether it is possible to achieve and, if possible, the minimal number of operations to apply. Input The first line contains integers _n_ and _x_ (2u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_x_u2009≤u2009100u2009000), number of elements in the array and the number to and with. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009100u2009000), the elements of the array. Output Print a single integer denoting the minimal number of operations to do, or -1, if it is impossible. Note In the first example one can apply the operation to the last element of the array. That replaces 7 with 3, so we achieve the goal in one move. In the second example the array already has two equal elements. In the third example applying the operation won't change the array at all, so it is impossible to make some pair of elements equal.
1,200
false
true
false
false
false
false
false
false
false
false
5,620
1547C
Monocarp and Polycarp are learning new programming techniques. Now they decided to try pair programming. It's known that they have worked together on the same file for $$$n + m$$$ minutes. Every minute exactly one of them made one change to the file. Before they started, there were already $$$k$$$ lines written in the file. Every minute exactly one of them does one of two actions: adds a new line to the end of the file or changes one of its lines. Monocarp worked in total for $$$n$$$ minutes and performed the sequence of actions $$$[a_1, a_2, dots, a_n]$$$. If $$$a_i = 0$$$, then he adds a new line to the end of the file. If $$$a_i > 0$$$, then he changes the line with the number $$$a_i$$$. Monocarp performed actions strictly in this order: $$$a_1$$$, then $$$a_2$$$, ..., $$$a_n$$$. Polycarp worked in total for $$$m$$$ minutes and performed the sequence of actions $$$[b_1, b_2, dots, b_m]$$$. If $$$b_j = 0$$$, then he adds a new line to the end of the file. If $$$b_j > 0$$$, then he changes the line with the number $$$b_j$$$. Polycarp performed actions strictly in this order: $$$b_1$$$, then $$$b_2$$$, ..., $$$b_m$$$. Restore their common sequence of actions of length $$$n + m$$$ such that all actions would be correct — there should be no changes to lines that do not yet exist. Keep in mind that in the common sequence Monocarp's actions should form the subsequence $$$[a_1, a_2, dots, a_n]$$$ and Polycarp's — subsequence $$$[b_1, b_2, dots, b_m]$$$. They can replace each other at the computer any number of times. Let's look at an example. Suppose $$$k = 3$$$. Monocarp first changed the line with the number $$$2$$$ and then added a new line (thus, $$$n = 2, : a = [2, 0]$$$). Polycarp first added a new line and then changed the line with the number $$$5$$$ (thus, $$$m = 2, : b = [0, 5]$$$). Since the initial length of the file was $$$3$$$, in order for Polycarp to change line number $$$5$$$ two new lines must be added beforehand. Examples of correct sequences of changes, in this case, would be $$$[0, 2, 0, 5]$$$ and $$$[2, 0, 0, 5]$$$. Changes $$$[0, 0, 5, 2]$$$ (wrong order of actions) and $$$[0, 5, 2, 0]$$$ (line $$$5$$$ cannot be edited yet) are not correct. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$). Then $$$t$$$ test cases follow. Before each test case, there is an empty line. Each test case contains three lines. The first line contains three integers $$$k$$$, $$$n$$$, $$$m$$$ ($$$0 le k le 100$$$, $$$1 le n, m le 100$$$)xa0— the initial number of lines in file and lengths of Monocarp's and Polycarp's sequences of changes respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 300$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$0 le b_j le 300$$$).
1,100
false
true
false
false
false
false
false
false
false
false
2,911
483A
Your friend has recently learned about coprime numbers. A pair of numbers {_a_,u2009_b_} is called coprime if the maximum number that divides both _a_ and _b_ is equal to one. Your friend often comes up with different statements. He has recently supposed that if the pair (_a_,u2009_b_) is coprime and the pair (_b_,u2009_c_) is coprime, then the pair (_a_,u2009_c_) is coprime. You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (_a_,u2009_b_,u2009_c_), for which the statement is false, and the numbers meet the condition _l_u2009≤u2009_a_u2009<u2009_b_u2009<u2009_c_u2009≤u2009_r_. More specifically, you need to find three numbers (_a_,u2009_b_,u2009_c_), such that _l_u2009≤u2009_a_u2009<u2009_b_u2009<u2009_c_u2009≤u2009_r_, pairs (_a_,u2009_b_) and (_b_,u2009_c_) are coprime, and pair (_a_,u2009_c_) is not coprime. Output Print three positive space-separated integers _a_, _b_, _c_xa0— three distinct numbers (_a_,u2009_b_,u2009_c_) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order. If the counterexample does not exist, print the single number -1. Note In the first sample pair (2,u20094) is not coprime and pairs (2,u20093) and (3,u20094) are. In the second sample you cannot form a group of three distinct integers, so the answer is -1. In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.
1,100
true
false
true
false
false
false
true
false
false
false
7,904
1389C
Let's call left cyclic shift of some string $$$t_1 t_2 t_3 dots t_{n - 1} t_n$$$ as string $$$t_2 t_3 dots t_{n - 1} t_n t_1$$$. Analogically, let's call right cyclic shift of string $$$t$$$ as string $$$t_n t_1 t_2 t_3 dots t_{n - 1}$$$. Let's say string $$$t$$$ is good if its left cyclic shift is equal to its right cyclic shift. You are given string $$$s$$$ which consists of digits 0–9. What is the minimum number of characters you need to erase from $$$s$$$ to make it good? Input The first line contains single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Next $$$t$$$ lines contains test casesxa0— one per line. The first and only line of each test case contains string $$$s$$$ ($$$2 le s le 2 cdot 10^5$$$). Each character $$$s_i$$$ is digit 0–9. It's guaranteed that the total length of strings doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print the minimum number of characters you need to erase from $$$s$$$ to make it good. Example Input 3 95831 100120013 252525252525 Note In the first test case, you can erase any $$$3$$$ characters, for example, the $$$1$$$-st, the $$$3$$$-rd, and the $$$4$$$-th. You'll get string 51 and it is good. In the second test case, we can erase all characters except 0: the remaining string is 0000 and it's good. In the third test case, the given string $$$s$$$ is already good.
1,500
false
true
false
true
false
false
true
false
false
false
3,726
1750E
Daemon Targaryen decided to stop looking like a Metin2 character. He turned himself into the most beautiful thing, a bracket sequence. For a bracket sequence, we can do two kind of operations: Select one of its substrings$$$^dagger$$$ and cyclic shift it to the right. For example, after a cyclic shift to the right, "(())" will become ")(()"; Insert any bracket, opening '(' or closing ')', wherever you want in the sequence. We define the cost of a bracket sequence as the minimum number of such operations to make it balanced$$$^ddagger$$$. Given a bracket sequence $$$s$$$ of length $$$n$$$, find the sum of costs across all its $$$frac{n(n+1)}{2}$$$ non-empty substrings. Note that for each substring we calculate the cost independently. $$$^dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. $$$^ddagger$$$ A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters $$$+$$$ and $$$1$$$. For example, sequences "(())()", "()", and "(()(()))" are balanced, while ")(", "(()", and "(()))(" are not. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the length of the bracket sequence. The second line of each test case contains a string $$$s$$$, consisting only of characters '(' and ')', of length $$$n$$$ — the bracket sequence. It is guaranteed that sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, there is the only substring ")". Its cost is $$$1$$$ because we can insert '(' to the beginning of this substring and get a string "()", that is a balanced string. In the second test case, the cost of each substring of length one is $$$1$$$. The cost of a substring ")(" is $$$1$$$ because we can cyclically shift it to right and get a string "()". The cost of strings ")()" and "()(" is $$$1$$$ because its enough to insert one bracket to each of them. The cost of substring ")()(" is $$$1$$$ because we can cyclically shift it to right and get a string "()()". So there are $$$4 + 2 + 2 + 1 = 9$$$ substring of cost $$$1$$$ and $$$1$$$ substring of cost $$$0$$$. So the sum of the costs is $$$9$$$. In the third test case, "(", the cost is $$$1$$$; "()", the cost is $$$0$$$; "())", the cost is $$$1$$$; ")", the cost is $$$1$$$; "))", the cost is $$$2$$$; ")", the cost is $$$1$$$. So the sum of the costs is $$$6$$$.
2,400
false
true
false
true
true
false
false
true
false
false
1,781
1219G
It is Bubble Cup finals season and farmer Johnny Bubbles must harvest his bubbles. The bubbles are in a rectangular bubblefield formed of $$$N$$$ x $$$M$$$ square parcels divided into $$$N$$$ rows and $$$M$$$ columns. The parcel in $$$i^{th}$$$ row and $$$j^{th}$$$ column yields $$$A_{i,j}$$$ bubbles. Johnny Bubbles has available a very special self-driving bubble harvester that, once manually positioned at the beginning of a row or column, automatically harvests all the bubbles in that row or column. Once the harvester reaches the end of the row or column it stops and must be repositioned. The harvester can pass through any parcel any number of times, but it can collect bubbles from the parcel only once. Johnny is very busy farmer, so he is available to manually position the harvester at most four times per day. Johnny is also impatient, so he wants to harvest as many bubbles as possible on the first day. Please help Johnny to calculate what is the maximum number of bubbles he can collect on the first day. Input The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1$$$ $$$leq$$$ $$$N$$$, $$$M$$$ $$$leq$$$ $$$N$$$ $$$M$$$ $$$leq$$$ $$$10^{5}$$$) - the bubblefield size. Each of the next $$$N$$$ lines contains $$$M$$$ integers. The $$$j^{th}$$$ element in the $$$i^{th}$$$ line is $$$A_{i,j}$$$ ($$$0$$$ $$$leq$$$ $$$a_{i,j}$$$ $$$leq$$$ $$$10^{9}$$$) — the yield of the parcel located in the $$$i^{th}$$$ row and the $$$j^{th}$$$ column. Output Output contains one integer number - maximum number of the bubbles Johnny can harvest on the first day. Examples Input 5 5 0 9 2 7 0 9 0 3 0 5 0 8 0 3 1 6 7 4 3 9 3 6 4 1 0 Note In the first example, farmer Johnny can harvest all the bubbles by positioning the harvester on the first and the second row. In the second example, one way Johnny can harvest maximum number of bubbles is to position the harvester in the second row, the fourth row, the second column and the fourth column.
2,000
false
false
true
false
false
false
false
false
false
false
4,574
1468C
Monocarp and Polycarp are working as waiters in Berpizza, a pizzeria located near the center of Bertown. Since they are waiters, their job is to serve the customers, but they choose whom they serve first differently. At the start of the working day, there are no customers at the Berpizza. They come there one by one. When a customer comes into the pizzeria, she sits and waits for Monocarp or Polycarp to serve her. Monocarp has been working in Berpizza for just two weeks, so whenever he serves a customer, he simply chooses the one who came to Berpizza first, and serves that customer. On the other hand, Polycarp is an experienced waiter at Berpizza, and he knows which customers are going to spend a lot of money at the pizzeria (and which aren't) as soon as he sees them. For each customer, Polycarp estimates the amount of money this customer can spend, and when he serves a customer, he chooses the one that is expected to leave the most money at Berpizza (in case there are several such customers, he chooses the one who came first among them). Obviously, no customer can be served twice, so Monocarp and Polycarp choose which customer to serve only among those who haven't been served yet. When the number of customers gets really high, it becomes difficult for both Monocarp and Polycarp to choose the customer they are going to serve. Your task is to write a program that makes these choices for them. Formally, your program should be able to process three types of queries: $$$1$$$ $$$m$$$ — a customer comes to Berpizza, and Polycarp estimates the amount of money that they will spend as $$$m$$$; $$$2$$$ — Monocarp serves a customer which came to the pizzeria first; $$$3$$$ — Polycarp serves a customer which is expected to spend the largest amount of money at the pizzeria (if there are several such customers, the one that came to the pizzeria first is chosen). For each query of types $$$2$$$ and $$$3$$$, report the number of the customer who was served (the customers are numbered in the order they come to the pizzeria, starting from $$$1$$$). Input The first line contains one integer $$$q$$$ ($$$2 le q le 5 cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each describing a query in one of the following formats: $$$1$$$ $$$m$$$ ($$$1 le m le 5 cdot 10^5$$$) — a customer comes to Berpizza, and Polycarp estimates the amount of money that they will spend as $$$m$$$; $$$2$$$ — Monocarp serves a customer which came to the pizzeria first; $$$3$$$ — Polycarp serves a customer which is expected to spend the largest amount of money at the pizzeria (if there are multiple such customers, the one that came to the pizzeria first is chosen). Queries of type $$$2$$$ and $$$3$$$ are asked only when there exists at least one customer that hasn't been served yet. There is at least one query of type $$$2$$$ or $$$3$$$ in the input. Output For each query of type $$$2$$$ or $$$3$$$, print one integer — the number of the customer that has been served in that event. The customers are numbered in the order in which they come to the pizzeria, starting from $$$1$$$.
1,400
false
false
true
false
true
false
false
false
false
false
3,350
234G
Little time is left before Berland annual football championship. Therefore the coach of team "Losewille Rangers" decided to resume the practice, that were indefinitely interrupted for uncertain reasons. Overall there are _n_ players in "Losewille Rangers". Each player on the team has a number — a unique integer from 1 to _n_. To prepare for the championship, the coach Mr. Floppe decided to spend some number of practices. Mr. Floppe spent some long nights of his holiday planning how to conduct the practices. He came to a very complex practice system. Each practice consists of one game, all _n_ players of the team take part in the game. The players are sorted into two teams in some way. In this case, the teams may have different numbers of players, but each team must have at least one player. The coach wants to be sure that after the series of the practice sessions each pair of players had at least one practice, when they played in different teams. As the players' energy is limited, the coach wants to achieve the goal in the least number of practices. Help him to schedule the practices. Input A single input line contains integer _n_ (2u2009≤u2009_n_u2009≤u20091000). Output In the first line print _m_ — the minimum number of practices the coach will have to schedule. Then print the descriptions of the practices in _m_ lines. In the _i_-th of those lines print _f__i_ — the number of players in the first team during the _i_-th practice (1u2009≤u2009_f__i_u2009<u2009_n_), and _f__i_ numbers from 1 to _n_ — the numbers of players in the first team. The rest of the players will play in the second team during this practice. Separate numbers on a line with spaces. Print the numbers of the players in any order. If there are multiple optimal solutions, print any of them.
1,600
false
false
true
false
false
true
false
false
false
false
8,898
551D
Problem - 551D - 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 implementation math matrices number theory *2100 No tag edit access → Contest materials , operation means bitwise OR (in Pascal it is equivalent to , in C/C++/Java/Python it is equivalent to ). Because the answer can be quite large, calculate it modulo _m_. This time GukiZ hasn't come up with solution, and needs you to help him! Input First and the only line of input contains four integers _n_, _k_, _l_, _m_ (2u2009≤u2009_n_u2009≤u20091018, 0u2009≤u2009_k_u2009≤u20091018, 0u2009≤u2009_l_u2009≤u200964, 1u2009≤u2009_m_u2009≤u2009109u2009+u20097). Output In the single line print the number of arrays satisfying the condition above modulo _m_. Examples Input 2 1 2 10 Output 3 Input 2 1 1 3 Output 1 Input 3 3 2 10 Output 9 Note In the first sample, satisfying arrays are {1,u20091},u2009{3,u20091},u2009{1,u20093}. In the second sample, only satisfying array is {1,u20091}. In the third sample, satisfying arrays are {0,u20093,u20093},u2009{1,u20093,u20092},u2009{1,u20093,u20093},u2009{2,u20093,u20091},u2009{2,u20093,u20093},u2009{3,u20093,u20090},u2009{3,u20093,u20091},u2009{3,u20093,u20092},u2009{3,u20093,u20093}.
2,100
true
false
true
false
false
false
false
false
false
false
7,629
1943F
You are given a binary string$$$^dagger$$$ $$$s$$$ of length $$$n$$$. A binary string $$$p$$$ of the same length $$$n$$$ is called good if for every $$$i$$$ ($$$1 leq i leq n$$$), there exist indices $$$l$$$ and $$$r$$$ such that: $$$1 leq l leq i leq r leq n$$$ $$$s_i$$$ is a mode$$$^ddagger$$$ of the string $$$p_lp_{l+1}ldots p_r$$$ You are given another binary string $$$t$$$ of length $$$n$$$. Find the minimum Hamming distance$$$^S$$$ between $$$t$$$ and any good string $$$g$$$. $$$^dagger$$$ A binary string is a string that only consists of characters $$$mathtt{0}$$$ and $$$mathtt{1}$$$. $$$^ddagger$$$ Character $$$c$$$ is a mode of string $$$p$$$ of length $$$m$$$ if the number of occurrences of $$$c$$$ in $$$p$$$ is at least $$$lceil frac{m}{2} ceil$$$. For example, $$$mathtt{0}$$$ is a mode of $$$mathtt{010}$$$, $$$mathtt{1}$$$ is not a mode of $$$mathtt{010}$$$, and both $$$mathtt{0}$$$ and $$$mathtt{1}$$$ are modes of $$$mathtt{011010}$$$. $$$^S$$$ The Hamming distance of strings $$$a$$$ and $$$b$$$ of length $$$m$$$ is the number of indices $$$i$$$ such that $$$1 leq i leq m$$$ and $$$a_i eq b_i$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^4$$$)xa0— the length of the binary string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of characters 0 and 1. The third line of each test case contains a binary string $$$t$$$ of length $$$n$$$ consisting of characters 0 and 1. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$, with the additional assurance that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^8$$$ Output For each test case, print the minimum Hamming distance between $$$t$$$ and any good string $$$g$$$. Example Input 3 3 000 000 4 0000 1111 6 111111 000100 Note In the first test case, $$$g=mathtt{000}$$$ is a good string which has Hamming distance $$$0$$$ from $$$t$$$. In the second test case, $$$g=mathtt{0011}$$$ is a good string which has Hamming distance $$$2$$$ from $$$t$$$. It can be proven that there are no good strings with Hamming distance less than $$$2$$$ from $$$t$$$. In the third test case, $$$g=mathtt{001100}$$$ is a good string which has Hamming distance $$$1$$$ from $$$t$$$.
3,500
false
false
false
true
false
false
false
false
false
false
616
1266F
Let $$$G$$$ be a simple graph. Let $$$W$$$ be a non-empty subset of vertices. Then $$$W$$$ is almost-$$$k$$$-uniform if for each pair of distinct vertices $$$u,v in W$$$ the distance between $$$u$$$ and $$$v$$$ is either $$$k$$$ or $$$k+1$$$. You are given a tree on $$$n$$$ vertices. For each $$$i$$$ between $$$1$$$ and $$$n$$$, find the maximum size of an almost-$$$i$$$-uniform set. Input The first line contains a single integer $$$n$$$ ($$$2 leq n leq 5 cdot 10^5$$$)xa0– the number of vertices of the tree. Then $$$n-1$$$ lines follows, the $$$i$$$-th of which consisting of two space separated integers $$$u_i$$$, $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$) meaning that there is an edge between vertices $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given graph is tree. Output Output a single line containing $$$n$$$ space separated integers $$$a_i$$$, where $$$a_i$$$ is the maximum size of an almost-$$$i$$$-uniform set. Examples Input 6 1 2 1 3 1 4 4 5 4 6 Note Consider the first example. The only maximum almost-$$$1$$$-uniform set is $$${1, 2, 3, 4}$$$. One of the maximum almost-$$$2$$$-uniform sets is or $$${2, 3, 5}$$$, another one is $$${2, 3, 4}$$$. A maximum almost-$$$3$$$-uniform set is any pair of vertices on distance $$$3$$$. Any single vertex is an almost-$$$k$$$-uniform set for $$$k geq 1$$$. In the second sample there is an almost-$$$2$$$-uniform set of size $$$4$$$, and that is $$${2, 3, 5, 6}$$$.
2,900
false
false
false
false
false
false
false
false
false
true
4,366
1423F
A famous gang of pirates, Sea Dogs, has come back to their hideout from one of their extravagant plunders. They want to split their treasure fairly amongst themselves, that is why You, their trusted financial advisor, devised a game to help them: All of them take a sit at their round table, some of them with the golden coins they have just stolen. At each iteration of the game if one of them has equal or more than 2 coins, he is eligible to the splitting and he gives one coin to each pirate sitting next to him. If there are more candidates (pirates with equal or more than 2 coins) then You are the one that chooses which one of them will do the splitting in that iteration. The game ends when there are no more candidates eligible to do the splitting. Pirates can call it a day, only when the game ends. Since they are beings with a finite amount of time at their disposal, they would prefer if the game that they are playing can end after finite iterations, and if so, they call it a good game. On the other hand, if no matter how You do the splitting, the game cannot end in finite iterations, they call it a bad game. Can You help them figure out before they start playing if the game will be good or bad? Input The first line of input contains two integer numbers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^{9}$$$, $$$0 le k le 2cdot10^5$$$), where $$$n$$$ denotes total number of pirates and $$$k$$$ is the number of pirates that have any coins. The next $$$k$$$ lines of input contain integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i le n$$$, $$$1 le b_i le 10^{9}$$$), where $$$a_i$$$ denotes the index of the pirate sitting at the round table ($$$n$$$ and $$$1$$$ are neighbours) and $$$b_i$$$ the total number of coins that pirate $$$a_i$$$ has at the start of the game. Output Print $$$1$$$ if the game is a good game: There is a way to do the splitting so the game ends after finite number of iterations. Print $$$-1$$$ if the game is a bad game: No matter how You do the splitting the game does not end in finite number of iterations. Note In the third example the game has no end, because You always only have only one candidate, after whose splitting you end up in the same position as the starting one.
2,700
true
false
false
false
false
false
false
false
false
false
3,558
441C
Valera has got a rectangle table consisting of _n_ rows and _m_ columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row _x_ and column _y_ by a pair of integers (_x_,u2009_y_). Valera wants to place exactly _k_ tubes on his rectangle table. A tube is such sequence of table cells (_x_1,u2009_y_1), (_x_2,u2009_y_2), ..., (_x__r_,u2009_y__r_), that: _r_u2009≥u20092; for any integer _i_ (1u2009≤u2009_i_u2009≤u2009_r_u2009-u20091) the following equation _x__i_u2009-u2009_x__i_u2009+u20091u2009+u2009_y__i_u2009-u2009_y__i_u2009+u20091u2009=u20091 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence. Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube. Help Valera to arrange _k_ tubes on his rectangle table in a fancy manner. Input The first line contains three space-separated integers _n_,u2009_m_,u2009_k_ (2u2009≤u2009_n_,u2009_m_u2009≤u2009300; 2u2009≤u20092_k_u2009≤u2009_n_·_m_) — the number of rows, the number of columns and the number of tubes, correspondingly. Output Print _k_ lines. In the _i_-th line print the description of the _i_-th tube: first print integer _r__i_ (the number of tube cells), then print 2_r__i_ integers _x__i_1,u2009_y__i_1,u2009_x__i_2,u2009_y__i_2,u2009...,u2009_x__ir__i_,u2009_y__ir__i_ (the sequence of table cells). If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists. Examples Output 3 1 1 1 2 1 3 3 2 1 2 2 2 3 3 3 1 3 2 3 3 Output 6 1 1 1 2 1 3 2 3 2 2 2 1 Note Picture for the first sample: Picture for the second sample:
1,500
false
false
true
false
false
true
false
false
false
false
8,073
1358B
Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus. She has $$$n$$$ friends who are also grannies (Maria is not included in this number). The $$$i$$$-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least $$$a_i$$$ other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny $$$i$$$ agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to $$$a_i$$$. Grannies gather in the courtyard like that. Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is $$$1$$$). All the remaining $$$n$$$ grannies are still sitting at home. On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least $$$a_i$$$ other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time. She cannot deceive grannies, that is, the situation when the $$$i$$$-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than $$$a_i$$$ other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance. Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony! Consider an example: if $$$n=6$$$ and $$$a=[1,5,4,5,1,9]$$$, then: at the first step Maria can call grannies with numbers $$$1$$$ and $$$5$$$, each of them will see two grannies at the moment of going out into the yard (note that $$$a_1=1 le 2$$$ and $$$a_5=1 le 2$$$); at the second step, Maria can call grannies with numbers $$$2$$$, $$$3$$$ and $$$4$$$, each of them will see five grannies at the moment of going out into the yard (note that $$$a_2=5 le 5$$$, $$$a_3=4 le 5$$$ and $$$a_4=5 le 5$$$); the $$$6$$$-th granny cannot be called into the yard xa0— therefore, the answer is $$$6$$$ (Maria herself and another $$$5$$$ grannies). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. Then test cases follow. The first line of a test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of grannies (Maria is not included in this number). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2cdot10^5$$$). It is guaranteed that the sum of the values $$$n$$$ over all test cases of the input does not exceed $$$10^5$$$. Output For each test case, print a single integer $$$k$$$ ($$$1 le k le n + 1$$$) — the maximum possible number of grannies in the courtyard. Example Input 4 5 1 1 2 2 1 6 2 3 4 5 6 7 6 1 5 4 5 1 9 5 1 2 3 5 6 Note In the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard. In the second test case in the example, no one can be in the yard, so Maria will remain there alone. The third test case in the example is described in the details above. In the fourth test case in the example, on the first step Maria can call grannies with numbers $$$1$$$, $$$2$$$ and $$$3$$$. If on the second step Maria calls $$$4$$$ or $$$5$$$ (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can't call the $$$4$$$-th granny or the $$$5$$$-th granny separately (one of them). If she calls both: $$$4$$$ and $$$5$$$, then when they appear, they will see $$$4+1=5$$$ grannies. Despite the fact that it is enough for the $$$4$$$-th granny, the $$$5$$$-th granny is not satisfied. So, Maria cannot call both the $$$4$$$-th granny and the $$$5$$$-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total.
1,000
false
true
false
false
false
false
false
false
true
false
3,896
1948E
You are given two integers, $$$n$$$ and $$$k$$$. There is a graph on $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$, which initially has no edges. You have to assign each vertex an integer; let $$$a_i$$$ be the integer on the vertex $$$i$$$. All $$$a_i$$$ should be distinct integers from $$$1$$$ to $$$n$$$. After assigning integers, for every pair of vertices $$$(i, j)$$$, you add an edge between them if $$$i - j + a_i - a_j le k$$$. Your goal is to create a graph which can be partitioned into the minimum possible (for the given values of $$$n$$$ and $$$k$$$) number of cliques. Each vertex of the graph should belong to exactly one clique. Recall that a clique is a set of vertices such that every pair of vertices in it are connected with an edge. Since BledDest hasn't really brushed his programming skills up, he can't solve the problem "given a graph, partition it into the minimum number of cliques". So we also ask you to print the partition itself. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1600$$$) — the number of test cases. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 40$$$; $$$1 le k le 2n$$$). Output For each test case, print three lines: the first line should contain $$$n$$$ distinct integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$) — the values you assign to the vertices; the second line should contain one integer $$$q$$$ ($$$1 le q le n$$$) — the number of cliques you partition the graph into; the third line should contain $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le q$$$) — the partition of the graph into cliques. Where two vertices $$$u$$$ and $$$v$$$ are in the same clique if $$$c_u = c_v$$$. If there are multiple answers, print any of them. Example Output 2 1 1 1 1 3 1 5 2 4 2 1 1 2 1 2 1 2 3 4 5 6 7 8 1 1 1 1 1 1 1 1 1
2,100
false
true
true
false
false
true
true
false
false
true
595
1003E
You are given three integers $$$n$$$, $$$d$$$ and $$$k$$$. Your task is to construct an undirected tree on $$$n$$$ vertices with diameter $$$d$$$ and degree of each vertex at most $$$k$$$, or say that it is impossible. An undirected tree is a connected undirected graph with $$$n - 1$$$ edges. Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree. Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex $$$u$$$ it is the number of edges $$$(u, v)$$$ that belong to the tree, where $$$v$$$ is any other vertex of a tree). Input The first line of the input contains three integers $$$n$$$, $$$d$$$ and $$$k$$$ ($$$1 le n, d, k le 4 cdot 10^5$$$). Output If there is no tree satisfying the conditions above, print only one word "NO" (without quotes). Otherwise in the first line print "YES" (without quotes), and then print $$$n - 1$$$ lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from $$$1$$$ to $$$n$$$. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1 Examples Output YES 2 9 2 10 10 3 3 1 6 10 8 2 4 3 5 6 6 7 Output YES 2 5 7 2 3 7 3 1 1 6 8 7 4 3
2,100
false
false
false
false
false
true
false
false
false
true
5,670
320A
Problem - 320A - 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 *900 No tag edit access → Contest materials . This number doesn't contain leading zeros. Output Print "YES" if _n_ is a magic number or print "NO" if it's not. Examples Input 114114 Output YES Input 1111 Output YES Input 441231 Output NO
900
false
true
false
false
false
false
true
false
false
false
8,553
1956D
Nene gave you an array of integers $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$. You can perform the following operation no more than $$$5cdot 10^5$$$ times (possibly zero): Choose two integers $$$l$$$ and $$$r$$$ such that $$$1 le l le r le n$$$, compute $$$x$$$ as $$$operatorname{MEX}({a_l, a_{l+1}, ldots, a_r})$$$, and simultaneously set $$$a_l:=x, a_{l+1}:=x, ldots, a_r:=x$$$. Here, $$$operatorname{MEX}$$$ of a set of integers $$${c_1, c_2, ldots, c_k}$$$ is defined as the smallest non-negative integer $$$m$$$ which does not occur in the set $$$c$$$. Your goal is to maximize the sum of the elements of the array $$$a$$$. Find the maximum sum and construct a sequence of operations that achieves this sum. Note that you don't need to minimize the number of operations in this sequence, you only should use no more than $$$5cdot 10^5$$$ operations in your solution. Input The first line contains an integer $$$n$$$ ($$$1 le n le 18$$$)xa0— the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$0leq a_i leq 10^7$$$)xa0— the array $$$a$$$. Output In the first line, output two integers $$$s$$$ and $$$m$$$ ($$$0le mle 5cdot 10^5$$$)xa0— the maximum sum of elements of the array $$$a$$$ and the number of operations in your solution. In the $$$i$$$-th of the following $$$m$$$ lines, output two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$), representing the parameters of the $$$i$$$-th operation. It can be shown that the maximum sum of elements of the array $$$a$$$ can always be obtained in no more than $$$5 cdot 10^5$$$ operations. Note In the first example, after the operation with $$$l=1$$$ and $$$r=2$$$ the array $$$a$$$ becomes equal to $$$[2,2]$$$. It can be shown that it is impossible to achieve a larger sum of the elements of $$$a$$$, so the answer is $$$4$$$. In the second example, the initial sum of elements is $$$13$$$ which can be shown to be the largest. In the third example, the array $$$a$$$ changes as follows: after the first operation ($$$l=3$$$, $$$r=3$$$), the array $$$a$$$ becomes equal to $$$[1,100,0,1]$$$; after the second operation ($$$l=3$$$, $$$r=4$$$), the array $$$a$$$ becomes equal to $$$[1,100,2,2]$$$. It can be shown that it is impossible to achieve a larger sum of the elements of $$$a$$$, so the answer is $$$105$$$.
2,000
true
true
true
true
false
true
true
false
false
false
537
1854B
Andrea is playing the game Tanto Cuore. He has a deck of $$$n$$$ cards with values $$$a_1, ldots, a_n$$$ from top to bottom. Each card can be either locked or unlocked. Initially, only the topmost card is unlocked. The game proceeds in turns. In each turn, Andrea chooses an unlocked card in the deck — the value written on the card is $$$v$$$ — and performs exactly one of the following two operations: 1. Unlock the first $$$v$$$ locked cards in the deck from the top. If there are less than $$$v$$$ locked cards in the deck, then unlock all the locked cards. 2. Earn $$$v$$$ victory points. In both cases, after performing the operation, he removes the card from the deck. The game ends when all the cards remaining in the deck are locked, or there are no more cards in the deck. What is the maximum number of victory points Andrea can earn? Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the number of cards in the deck. The second line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$0 leq a_1, ldots, a_n leq n$$$) — the values of the cards in the deck. Output Output a single integer — the maximum number of victory points Andrea can earn. Note In the first sample test, the deck starts as [unlocked, locked]. Then, Andrea uses the first card to unlock the second card. Then he uses the second card to earn $$$2$$$ victory points. In the second sample test, Andrea can use the first card to unlock the second and the third card. Then he uses the second and the third card to earn $$$4+5=9$$$ victory points. In the third sample test, Andrea can't unlock any cards or gain any victory points with the first card.
2,200
false
false
false
true
false
false
true
false
false
false
1,157
1758C
Given two integers $$$n$$$ and $$$x$$$, a permutation$$$^{dagger}$$$ $$$p$$$ of length $$$n$$$ is called funny if $$$p_i$$$ is a multiple of $$$i$$$ for all $$$1 leq i leq n - 1$$$, $$$p_n = 1$$$, and $$$p_1 = x$$$. Find the lexicographically minimal$$$^{ddagger}$$$ funny permutation, or report that no such permutation exists. $$$^{dagger}$$$ A permutation of length $$$n$$$ is an array consisting of each of the integers from $$$1$$$ to $$$n$$$ exactly once. $$$^{ddagger}$$$ Let $$$a$$$ and $$$b$$$ be permutations of length $$$n$$$. Then $$$a$$$ is lexicographically smaller than $$$b$$$ if in the first position $$$i$$$ where $$$a$$$ and $$$b$$$ differ, $$$a_i < b_i$$$. A permutation is lexicographically minimal if it is lexicographically smaller than all other permutations. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$2 leq n leq 2 cdot 10^5$$$; $$$1 < x leq n$$$). The sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, if the answer exists, output $$$n$$$ distinct integers $$$p_1, p_2, dots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0— the lexicographically minimal funny permutation $$$p$$$. Otherwise, output $$$-1$$$. Note In the first test case, the permutation $$$[3,2,1]$$$ satisfies all the conditions: $$$p_1=3$$$, $$$p_3=1$$$, and: $$$p_1=3$$$ is a multiple of $$$1$$$. $$$p_2=2$$$ is a multiple of $$$2$$$. In the second test case, the permutation $$$[2,4,3,1]$$$ satisfies all the conditions: $$$p_1=2$$$, $$$p_4=1$$$, and: $$$p_1=2$$$ is a multiple of $$$1$$$. $$$p_2=4$$$ is a multiple of $$$2$$$. $$$p_3=3$$$ is a multiple of $$$3$$$. We can show that these permutations are lexicographically minimal. No such permutations exist in the third test case.
1,400
false
true
false
false
false
false
false
false
false
false
1,766
1706D2
This is the hard version of the problem. The only difference between the versions is the constraints on $$$n$$$, $$$k$$$, $$$a_i$$$, and the sum of $$$n$$$ over all test cases. You can make hacks only if both versions of the problem are solved. Note the unusual memory limit. You are given an array of integers $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$, and an integer $$$k$$$. The cost of an array of integers $$$p_1, p_2, ldots, p_n$$$ of length $$$n$$$ is $$$$$$maxlimits_{1 le i le n}left(left lfloor frac{a_i}{p_i} ight floor ight) - minlimits_{1 le i le n}left(left lfloor frac{a_i}{p_i} ight floor ight).$$$$$$ Here, $$$lfloor frac{x}{y} floor$$$ denotes the integer part of the division of $$$x$$$ by $$$y$$$. Find the minimum cost of an array $$$p$$$ such that $$$1 le p_i le k$$$ for all $$$1 le i le n$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_1 le a_2 le ldots le a_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 a single integerxa0— the minimum possible cost of an array $$$p$$$ satisfying the condition above. Example Input 7 5 2 4 5 6 8 11 5 12 4 5 6 8 11 3 1 2 9 15 7 3 2 3 5 5 6 9 10 6 56 54 286 527 1436 2450 2681 3 95 16 340 2241 2 2 1 3 Note In the first test case, the optimal array is $$$p = [1, 1, 1, 2, 2]$$$. The resulting array of values of $$$lfloor frac{a_i}{p_i} floor$$$ is $$$[4, 5, 6, 4, 5]$$$. The cost of $$$p$$$ is $$$maxlimits_{1 le i le n}(lfloor frac{a_i}{p_i} floor) - minlimits_{1 le i le n}(lfloor frac{a_i}{p_i} floor) = 6 - 4 = 2$$$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost. In the second test case, one of the optimal arrays is $$$p = [12, 12, 12, 12, 12]$$$, which results in all $$$lfloor frac{a_i}{p_i} floor$$$ being $$$0$$$. In the third test case, the only possible array is $$$p = [1, 1, 1]$$$.
2,400
true
true
false
true
true
true
true
false
false
false
2,036
65D
As you know, Hogwarts has four houses: Gryffindor, Hufflepuff, Ravenclaw and Slytherin. The sorting of the first-years into houses is done by the Sorting Hat. The pupils are called one by one in the alphabetical order, each of them should put a hat on his head and, after some thought, the hat solemnly announces the name of the house the student should enter. At that the Hat is believed to base its considerations on the student's personal qualities: it sends the brave and noble ones to Gryffindor, the smart and shrewd ones — to Ravenclaw, the persistent and honest ones — to Hufflepuff and the clever and cunning ones — to Slytherin. However, a first year student Hermione Granger got very concerned about the forthcoming sorting. She studied all the literature on the Sorting Hat and came to the conclusion that it is much simpler than that. If the relatives of the student have already studied at Hogwarts, the hat puts the student to the same house, where his family used to study. In controversial situations, when the relatives studied in different houses or when they were all Muggles like Hermione's parents, then the Hat sorts the student to the house, to which the least number of first years has been sent at that moment. If there are several such houses, the choice is given to the student himself. Then the student can choose any of the houses, to which the least number of first years has been sent so far. Hermione has already asked the students that are on the list before her about their relatives. Now she and her new friends Harry Potter and Ron Weasley want to find out into what house the Hat will put Hermione. Input The first input line contains an integer _n_ (1u2009≤u2009_n_u2009≤u200910000). It is the number of students who are in the list before Hermione. The next line contains _n_ symbols. If all the relatives of a student used to study in the same house, then the _i_-th character in the string coincides with the first letter of the name of this house. Otherwise, the _i_-th symbol is equal to "?". Output Print all the possible houses where Hermione can be sent. The names of the houses should be printed in the alphabetical order, one per line. Examples Output Gryffindor Ravenclaw Output Gryffindor Ravenclaw Slytherin Note Consider the second example. There are only two students before Hermione. The first student is sent to Hufflepuff. The second disciple is given the choice between the houses where the least number of students has been sent, i.e. Gryffindor, Slytherin and Ravenclaw. If he chooses Gryffindor, Hermione is forced to choose between Ravenclaw and Slytherin, if he chooses Ravenclaw, Hermione will choose between Gryffindor and Slytherin, if he chooses Slytherin, Hermione will choose between Gryffindor and Ravenclaw. In the end, the following situation is possible (it depends on the choice of the second student and Hermione). Hermione will end up 1) in Gryffindor, 2) in Ravenclaw, 3) in Slytherin. Note that, despite the fact that in neither case Hermione will be given a choice between all the three options, they are all possible and they should all be printed in the answer. Hermione will not, under any circumstances, end up in Hufflepuff.
2,200
false
false
false
false
false
false
true
false
false
false
9,643
1843E
You are given an array $$$a$$$ consisting of $$$n$$$ zeros. You are also given a set of $$$m$$$ not necessarily different segments. Each segment is defined by two numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) and represents a subarray $$$a_{l_i}, a_{l_i+1}, dots, a_{r_i}$$$ of the array $$$a$$$. Let's call the segment $$$l_i, r_i$$$ beautiful if the number of ones on this segment is strictly greater than the number of zeros. For example, if $$$a = [1, 0, 1, 0, 1]$$$, then the segment $$$[1, 5]$$$ is beautiful (the number of ones is $$$3$$$, the number of zeros is $$$2$$$), but the segment $$$[3, 4]$$$ is not is beautiful (the number of ones is $$$1$$$, the number of zeros is $$$1$$$). You also have $$$q$$$ changes. For each change you are given the number $$$1 le x le n$$$, which means that you must assign an element $$$a_x$$$ the value $$$1$$$. You have to find the first change after which at least one of $$$m$$$ given segments becomes beautiful, or report that none of them is beautiful after processing all $$$q$$$ changes. 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 two integers $$$n$$$ and $$$m$$$ ($$$1 le m le n le 10^5$$$)xa0— the size of the array $$$a$$$ and the number of segments, respectively. Then there are $$$m$$$ lines consisting of two numbers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$)xa0—the boundaries of the segments. The next line contains an integer $$$q$$$ ($$$1 le q le n$$$)xa0— the number of changes. The following $$$q$$$ lines each contain a single integer $$$x$$$ ($$$1 le x le n$$$)xa0— the index of the array element that needs to be set to $$$1$$$. It is guaranteed that indexes in queries are distinct. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. Output For each test case, output one integer xa0— the minimum change number after which at least one of the segments will be beautiful, or $$$-1$$$ if none of the segments will be beautiful. Note In the first case, after first 2 changes we won't have any beautiful segments, but after the third one on a segment $$$[1; 5]$$$ there will be 3 ones and only 2 zeros, so the answer is 3. In the second case, there won't be any beautiful segments.
1,600
false
false
false
false
true
false
true
true
false
false
1,226
1526F
This is an interactive problem. There is a secret permutation $$$p$$$ ($$$1$$$-indexed) of numbers from $$$1$$$ to $$$n$$$. More formally, for $$$1 leq i leq n$$$, $$$1 leq p[i] leq n$$$ and for $$$1 leq i < j leq n$$$, $$$p[i] eq p[j]$$$. It is known that $$$p[1]<p[2]$$$. In $$$1$$$ query, you give $$$3$$$ distinct integers $$$a,b,c$$$ ($$$1 leq a,b,c leq n$$$), and receive the median of $$${p[a]-p[b],p[b]-p[c],p[a]-p[c]}$$$. In this case, the median is the $$$2$$$-nd element ($$$1$$$-indexed) of the sequence when sorted in non-decreasing order. The median of $$${4,6,2}$$$ is $$$4$$$ and the median of $$${0,123,33}$$$ is $$$33$$$. Can you find the secret permutation in not more than $$$2n+420$$$ queries? Note: the grader is not adaptive: the permutation is fixed before any queries are made. Input The first line of input contains a single integer $$$t$$$ $$$(1 leq t leq 1000)$$$ — the number of testcases. The first line of each testcase consists of a single integer $$$n$$$ $$$(20 leq n leq 100000)$$$ — the length of the secret permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100000$$$. Interaction For each testcase, you begin the interaction by reading $$$n$$$. To perform a query, output "? a b c" where $$$a,b,c$$$ is the $$$3$$$ indices you want to use for the query. Numbers have to satisfy $$$1 leq a,b,c leq n$$$ and $$$a eq b$$$,$$$b eq c$$$,$$$a eq c$$$. For each query, you will receive a single integer $$$x$$$: the median of $$${p[a]-p[b],p[b]-p[c],p[a]-p[c]}$$$. In case your query is invalid or you asked more than $$$2n+420$$$ queries, the interactor will print "−1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you have determined the secret permutation, output "! p[1] p[2] ... p[n]". If the secret permutation is correct, the interactor will print "1". Otherwise, the interactor will print "-1" and will finish interaction. You will receive Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded verdict. 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. Hacks: To hack, use the following format of test: The first line should contain a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of testcases. The first line of each testcase should contain a single integer $$$n$$$ ($$$20 leq n leq 100000$$$) — the length of the secret permutation. The following line of should contain $$$n$$$ integers $$$p[1],p[2],p[3],ldots,p[n]$$$. $$$p[1]<p[2]$$$ and $$$p$$$ must be a permutation of integers from $$$1$$$ to $$$n$$$. You must ensure that the sum of $$$n$$$ over all testcases does not exceed $$$100000$$$. Example Output ? 1 5 2 ? 20 19 2 ! 9 10 19 7 16 18 11 14 15 6 20 8 17 4 5 3 12 2 13 1 Note The secret permutation is $$${9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1}$$$. For the first query, the values of $$$(a,b,c)$$$ is $$$(1,5,2)$$$. Since $$$p[1]=9$$$, $$$p[5]=16$$$ and $$$p[2]=10$$$. The return value is the median of $$${9-16,16-10,9-10}$$$ which is $$$6$$$. For the second query, the values of $$$(a,b,c)$$$ is $$$(20,19,2)$$$. Since $$$p[20]=1$$$, $$$p[19]=13$$$ and $$$p[2]=10$$$. The return value is the median of $$${1-13,13-10,1-10}$$$ which is $$$9$$$. By some miracle, we have figured out that the secret permutation is $$${9,10,19,7,16,18,11,14,15,6,20,8,17,4,5,3,12,2,13,1}$$$. We output it and receive $$$1$$$ from the interactor, meaning that we have guessed the secret permutation correctly.
3,000
false
false
false
false
false
true
false
false
false
false
3,030
821E
Okabe likes to take walks but knows that spies from the Organization could be anywhere; that's why he wants to know how many different walks he can take in his city safely. Okabe's city can be represented as all points (_x_,u2009_y_) such that _x_ and _y_ are non-negative. Okabe starts at the origin (point (0,u20090)), and needs to reach the point (_k_,u20090). If Okabe is currently at the point (_x_,u2009_y_), in one step he can go to (_x_u2009+u20091,u2009_y_u2009+u20091), (_x_u2009+u20091,u2009_y_), or (_x_u2009+u20091,u2009_y_u2009-u20091). Additionally, there are _n_ horizontal line segments, the _i_-th of which goes from _x_u2009=u2009_a__i_ to _x_u2009=u2009_b__i_ inclusive, and is at _y_u2009=u2009_c__i_. It is guaranteed that _a_1u2009=u20090, _a__n_u2009≤u2009_k_u2009≤u2009_b__n_, and _a__i_u2009=u2009_b__i_u2009-u20091 for 2u2009≤u2009_i_u2009≤u2009_n_. The _i_-th line segment forces Okabe to walk with _y_-value in the range 0u2009≤u2009_y_u2009≤u2009_c__i_ when his _x_ value satisfies _a__i_u2009≤u2009_x_u2009≤u2009_b__i_, or else he might be spied on. This also means he is required to be under two line segments when one segment ends and another begins. Okabe now wants to know how many walks there are from the origin to the point (_k_,u20090) satisfying these conditions, modulo 109u2009+u20097. Input The first line of input contains the integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u20091018)xa0— the number of segments and the destination _x_ coordinate. The next _n_ lines contain three space-separated integers _a__i_, _b__i_, and _c__i_ (0u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u20091018, 0u2009≤u2009_c__i_u2009≤u200915)xa0— the left and right ends of a segment, and its _y_ coordinate. It is guaranteed that _a_1u2009=u20090, _a__n_u2009≤u2009_k_u2009≤u2009_b__n_, and _a__i_u2009=u2009_b__i_u2009-u20091 for 2u2009≤u2009_i_u2009≤u2009_n_.
2,100
false
false
false
true
false
false
false
false
false
false
6,469
185E
Imagine the Cartesian coordinate system. There are _k_ different points containing subway stations. One can get from any subway station to any one instantly. That is, the duration of the transfer between any two subway stations can be considered equal to zero. You are allowed to travel only between subway stations, that is, you are not allowed to leave the subway somewhere in the middle of your path, in-between the stations. There are _n_ dwarves, they are represented by their coordinates on the plane. The dwarves want to come together and watch a soap opera at some integer point on the plane. For that, they choose the gathering point and start moving towards it simultaneously. In one second a dwarf can move from point (_x_,u2009_y_) to one of the following points: (_x_u2009-u20091,u2009_y_), (_x_u2009+u20091,u2009_y_), (_x_,u2009_y_u2009-u20091), (_x_,u2009_y_u2009+u20091). Besides, the dwarves can use the subway as many times as they want (the subway transfers the dwarves instantly). The dwarves do not interfere with each other as they move (that is, the dwarves move simultaneously and independently from each other). Help the dwarves and find the minimum time they need to gather at one point. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105;xa00u2009≤u2009_k_u2009≤u2009105) — the number of dwarves and the number of subway stations, correspondingly. The next _n_ lines contain the coordinates of the dwarves. The _i_-th line contains two space-separated integers _x__i_ and _y__i_ (_x__i_,u2009_y__i_u2009≤u2009108) — the coordinates of the _i_-th dwarf. It is guaranteed that all dwarves are located at different points. The next _k_ lines contain the coordinates of the subway stations. The _t_-th line contains two space-separated integers _x__t_ and _y__t_ (_x__t_,u2009_y__t_u2009≤u2009108) — the coordinates of the _t_-th subway station. It is guaranteed that all subway stations are located at different points. Output Print a single number — the minimum time, in which all dwarves can gather together at one point to watch the soap. Examples Input 2 2 5 -3 -4 -5 -4 0 -3 -2
3,000
false
false
false
false
true
false
false
true
false
false
9,100