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
1765G
This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java or Kotlin — System.out.flush(), and in Python — sys.stdout.flush(). The jury has a string $$$s$$$ consisting of characters 0 and/or 1. The first character of this string is 0. The length of this string is $$$n$$$. You have to guess this string. Let's denote $$$s[l..r]$$$ as the substring of $$$s$$$ from $$$l$$$ to $$$r$$$ (i.u2009e. $$$s[l..r]$$$ is the string $$$s_ls_{l+1} dots s_r$$$). Let the prefix function of the string $$$s$$$ be an array $$$[p_1, p_2, dots, p_n]$$$, where $$$p_i$$$ is the greatest integer $$$j in [0, i-1]$$$ such that $$$s[1..j] = s[i-j+1..i]$$$. Also, let the antiprefix function of the string $$$s$$$ be an array $$$[q_1, q_2, dots, q_n]$$$, where $$$q_i$$$ is the greatest integer $$$j in [0, i-1]$$$ such that $$$s[1..j]$$$ differs from $$$s[i-j+1..i]$$$ in every position. For example, for the string 011001, its prefix function is $$$[0, 0, 0, 1, 1, 2]$$$, and its antiprefix function is $$$[0, 1, 1, 2, 3, 4]$$$. You can ask queries of two types to guess the string $$$s$$$: $$$1$$$ $$$i$$$ — "what is the value of $$$p_i$$$?"; $$$2$$$ $$$i$$$ — "what is the value of $$$q_i$$$?". You have to guess the string by asking no more than $$$789$$$ queries. Note that giving the answer does not count as a query. In every test and in every test case, the string $$$s$$$ is fixed beforehand. Interaction Initially, the jury program sends one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. At the start of each test case, the jury program sends one integer $$$n$$$ ($$$2 le n le 1000$$$) — the length of the string. After that, your program can submit queries to the jury program by printing one of the following lines (do not forget to flush the output after printing a line!): $$$1$$$ $$$i$$$ — the query "what is the value of $$$p_i$$$?"; $$$2$$$ $$$i$$$ — the query "what is the value of $$$q_i$$$?". For every query, the jury prints one integer on a separate line. It is either: the answer for your query, if the query is correct and you haven't exceeded the query limit; or the integer $$$-1$$$, if your query is incorrect (for example, the constraint $$$1 le i le n$$$ is not met) or if you have asked too many queries while processing the current test case. To submit the answer, your program should send a line in the following format (do not forget to flush the output after printing a line!): $$$0$$$ $$$s$$$, where $$$s$$$ is a sequence of $$$n$$$ characters 0 and/or 1. If your guess is correct, the jury program will print one integer $$$1$$$ on a separate line, indicating that you may proceed to the next test case (or terminate the program, if it was the last test case) and that the number of queries you have asked is reset. If it is not correct, the jury program will print one integer $$$-1$$$ on a separate line. After your program receives $$$-1$$$ as the answer, it should immediately terminate. This will lead to your submission receiving the verdict "Wrong Answer". If your program does not terminate, the verdict of your submission is undefined. Note The example contains one possible way of interaction in a test where $$$t = 2$$$, and the strings guessed by the jury are 011001 and 00111. Note that everything after the // sign is a comment that explains which line means what in the interaction. The jury program won't print these comments in the actual problem, and you shouldn't print them. The empty lines are also added for your convenience, the jury program won't print them, and your solution should not print any empty lines.
2,600
false
false
false
false
false
true
false
false
false
false
1,711
854A
Petya is a big fan of mathematics, especially its part related to fractions. Recently he learned that a fraction is called proper iff its numerator is smaller than its denominator (_a_u2009<u2009_b_) and that the fraction is called irreducible if its numerator and its denominator are coprime (they do not have positive common divisors except 1). During his free time, Petya thinks about proper irreducible fractions and converts them to decimals using the calculator. One day he mistakenly pressed addition button (u2009+u2009) instead of division button (÷) and got sum of numerator and denominator that was equal to _n_ instead of the expected decimal notation. Petya wanted to restore the original fraction, but soon he realized that it might not be done uniquely. That's why he decided to determine maximum possible proper irreducible fraction such that sum of its numerator and denominator equals _n_. Help Petya deal with this problem. Input In the only line of input there is an integer _n_ (3u2009≤u2009_n_u2009≤u20091000), the sum of numerator and denominator of the fraction. Output Output two space-separated positive integers _a_ and _b_, numerator and denominator of the maximum possible proper irreducible fraction satisfying the given sum.
800
true
false
false
false
false
true
true
false
false
false
6,329
192B
In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of _n_ tiles that are lain in a row and are numbered from 1 to _n_ from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number _n_. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number _i_ (_i_u2009<u2009_n_u2009-u20091), you can reach the tiles number _i_u2009+u20091 or the tile number _i_u2009+u20092 from it (if you stand on the tile number _n_u2009-u20091, you can only reach tile number _n_). We can assume that all the opposition movements occur instantaneously. In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the _i_-th tile is destroyed after _a__i_ days of rain (on day _a__i_ tile isn't destroyed yet, and on day _a__i_u2009+u20091 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number _n_ is broken, or it is impossible to reach the tile number _n_ from the tile number 1 if we can walk on undestroyed tiles. The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number _n_ will be possible. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009103) — the boulevard's length in tiles. The second line contains _n_ space-separated integers _a__i_ — the number of days after which the _i_-th tile gets destroyed (1u2009≤u2009_a__i_u2009≤u2009103). Output Print a single number — the sought number of days. Note In the first sample the second tile gets destroyed after day three, and the only path left is 1u2009→u20093u2009→u20094. After day five there is a two-tile gap between the first and the last tile, you can't jump over it. In the second sample path 1u2009→u20093u2009→u20095 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.
1,100
false
false
true
false
false
false
true
false
false
false
9,071
183A
Sensation, sensation in the two-dimensional kingdom! The police have caught a highly dangerous outlaw, member of the notorious "Pihters" gang. The law department states that the outlaw was driving from the gang's headquarters in his car when he crashed into an ice cream stall. The stall, the car, and the headquarters each occupies exactly one point on the two-dimensional kingdom. The outlaw's car was equipped with a GPS transmitter. The transmitter showed that the car made exactly _n_ movements on its way from the headquarters to the stall. A movement can move the car from point (_x_,u2009_y_) to one of these four points: to point (_x_u2009-u20091,u2009_y_) which we will mark by letter "L", to point (_x_u2009+u20091,u2009_y_) — "R", to point (_x_,u2009_y_u2009-u20091) — "D", to point (_x_,u2009_y_u2009+u20091) — "U". The GPS transmitter is very inaccurate and it doesn't preserve the exact sequence of the car's movements. Instead, it keeps records of the car's possible movements. Each record is a string of one of these types: "UL", "UR", "DL", "DR" or "ULDR". Each such string means that the car made a single movement corresponding to one of the characters of the string. For example, string "UL" means that the car moved either "U", or "L". You've received the journal with the outlaw's possible movements from the headquarters to the stall. The journal records are given in a chronological order. Given that the ice-cream stall is located at point (0,u20090), your task is to print the number of different points that can contain the gang headquarters (that is, the number of different possible locations of the car's origin). Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the number of the car's movements from the headquarters to the stall. Each of the following _n_ lines describes the car's possible movements. It is guaranteed that each possible movement is one of the following strings: "UL", "UR", "DL", "DR" or "ULDR". All movements are given in chronological order. Please do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin and cout stream or the %I64d specifier. Note The figure below shows the nine possible positions of the gang headquarters from the first sample: For example, the following movements can get the car from point (1,u20090) to point (0,u20090):
1,700
true
false
false
false
false
true
false
false
false
false
9,109
1088C
You're given an array $$$a$$$ of length $$$n$$$. You can perform the following operations on it: choose an index $$$i$$$ $$$(1 le i le n)$$$, an integer $$$x$$$ $$$(0 le x le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j+x$$$ for all $$$(1 le j le i)$$$, which means add $$$x$$$ to all the elements in the prefix ending at $$$i$$$. choose an index $$$i$$$ $$$(1 le i le n)$$$, an integer $$$x$$$ $$$(1 le x le 10^6)$$$, and replace $$$a_j$$$ with $$$a_j % x$$$ for all $$$(1 le j le i)$$$, which means replace every element in the prefix ending at $$$i$$$ with the remainder after dividing it by $$$x$$$. Can you make the array strictly increasing in no more than $$$n+1$$$ operations? Input The first line contains an integer $$$n$$$ $$$(1 le n le 2000)$$$, the number of elements in the array $$$a$$$. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$dots$$$, $$$a_n$$$ $$$(0 le a_i le 10^5)$$$, the elements of the array $$$a$$$. Output On the first line, print the number of operations you wish to perform. On the next lines, you should print the operations. To print an adding operation, use the format "$$$1$$$ $$$i$$$ $$$x$$$"; to print a modding operation, use the format "$$$2$$$ $$$i$$$ $$$x$$$". If $$$i$$$ or $$$x$$$ don't satisfy the limitations above, or you use more than $$$n+1$$$ operations, you'll get wrong answer verdict. Note In the first sample, the array is already increasing so we don't need any operations. In the second sample: In the first step: the array becomes $$$[8,6,3]$$$. In the second step: the array becomes $$$[0,2,3]$$$.
1,400
true
true
false
false
false
true
false
false
false
false
5,289
1926E
Vladislav has $$$n$$$ cards numbered $$$1, 2, dots, n$$$. He wants to lay them down in a row as follows: First, he lays down all the odd-numbered cards from smallest to largest. Next, he lays down all cards that are twice an odd number from smallest to largest (i.e. $$$2$$$ multiplied by an odd number). Next, he lays down all cards that are $$$3$$$ times an odd number from smallest to largest (i.e. $$$3$$$ multiplied by an odd number). Next, he lays down all cards that are $$$4$$$ times an odd number from smallest to largest (i.e. $$$4$$$ multiplied by an odd number). And so on, until all cards are laid down. What is the $$$k$$$-th card he lays down in this process? Once Vladislav puts a card down, he cannot use that card again. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 5 cdot 10^4$$$)xa0— the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 10^9$$$)xa0— the number of cards Vlad has, and the position of the card you need to output. Output For each test case, output a single integerxa0— the $$$k$$$-th card Vladislav lays down. Example Input 11 7 1 7 2 7 3 7 4 7 5 7 6 7 7 1 1 34 14 84 19 1000000000 1000000000 Output 1 3 5 7 2 6 4 1 27 37 536870912 Note In the first seven test cases, $$$n=7$$$. Vladislav lays down the cards as follows: Firstxa0— all the odd-numbered cards in the order $$$1$$$, $$$3$$$, $$$5$$$, $$$7$$$. Nextxa0— all cards that are twice an odd number in the order $$$2$$$, $$$6$$$. Next, there are no remaining cards that are $$$3$$$ times an odd number. (Vladislav has only one of each card.) Nextxa0— all cards that are $$$4$$$ times an odd number, and there is only one such card: $$$4$$$. There are no more cards left, so Vladislav stops. Thus the order of cards is $$$1$$$, $$$3$$$, $$$5$$$, $$$7$$$, $$$2$$$, $$$6$$$, $$$4$$$.
1,500
true
false
true
true
true
false
false
true
false
false
733
469B
Little X and Little Z are good friends. They always chat online. But both of them have schedules. Little Z has fixed schedule. He always online at any moment of time between _a_1 and _b_1, between _a_2 and _b_2, ..., between _a__p_ and _b__p_ (all borders inclusive). But the schedule of Little X is quite strange, it depends on the time when he gets up. If he gets up at time 0, he will be online at any moment of time between _c_1 and _d_1, between _c_2 and _d_2, ..., between _c__q_ and _d__q_ (all borders inclusive). But if he gets up at time _t_, these segments will be shifted by _t_. They become [_c__i_u2009+u2009_t_,u2009_d__i_u2009+u2009_t_] (for all _i_). If at a moment of time, both Little X and Little Z are online simultaneosly, they can chat online happily. You know that Little X can get up at an integer moment of time between _l_ and _r_ (both borders inclusive). Also you know that Little X wants to get up at the moment of time, that is suitable for chatting with Little Z (they must have at least one common moment of time in schedules). How many integer moments of time from the segment [_l_,u2009_r_] suit for that? Input The first line contains four space-separated integers _p_,u2009_q_,u2009_l_,u2009_r_ (1u2009≤u2009u2009_p_,u2009_q_u2009≤u200950;xa00u2009≤u2009_l_u2009≤u2009_r_u2009≤u20091000). Each of the next _p_ lines contains two space-separated integers _a__i_,u2009_b__i_ (0u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u20091000). Each of the next _q_ lines contains two space-separated integers _c__j_,u2009_d__j_ (0u2009≤u2009_c__j_u2009<u2009_d__j_u2009≤u20091000). It's guaranteed that _b__i_u2009<u2009_a__i_u2009+u20091 and _d__j_u2009<u2009_c__j_u2009+u20091 for all valid _i_ and _j_. Output Output a single integer — the number of moments of time from the segment [_l_,u2009_r_] which suit for online conversation. Examples Input 2 3 0 20 15 17 23 26 1 4 7 11 15 17
1,300
false
false
true
false
false
false
false
false
false
false
7,961
48B
Vasya has a beautiful garden where wonderful fruit trees grow and yield fantastic harvest every year. But lately thieves started to sneak into the garden at nights and steal the fruit too often. Vasya can’t spend the nights in the garden and guard the fruit because there’s no house in the garden! Vasya had been saving in for some time and finally he decided to build the house. The rest is simple: he should choose in which part of the garden to build the house. In the evening he sat at his table and drew the garden’s plan. On the plan the garden is represented as a rectangular checkered field _n_u2009×u2009_m_ in size divided into squares whose side length is 1. In some squares Vasya marked the trees growing there (one shouldn’t plant the trees too close to each other that’s why one square contains no more than one tree). Vasya wants to find a rectangular land lot _a_u2009×u2009_b_ squares in size to build a house on, at that the land lot border should go along the lines of the grid that separates the squares. All the trees that grow on the building lot will have to be chopped off. Vasya loves his garden very much, so help him choose the building land lot location so that the number of chopped trees would be as little as possible. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200950) which represent the garden location. The next _n_ lines contain _m_ numbers 0 or 1, which describe the garden on the scheme. The zero means that a tree doesn’t grow on this square and the 1 means that there is a growing tree. The last line contains two integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u200950). Note that Vasya can choose for building an _a_u2009×u2009_b_ rectangle as well a _b_u2009×u2009_a_ one, i.e. the side of the lot with the length of _a_ can be located as parallel to the garden side with the length of _n_, as well as parallel to the garden side with the length of _m_. Output Print the minimum number of trees that needs to be chopped off to select a land lot _a_u2009×u2009_b_ in size to build a house on. It is guaranteed that at least one lot location can always be found, i. e. either _a_u2009≤u2009_n_ and _b_u2009≤u2009_m_, or _a_u2009≤u2009_m_ и _b_u2009≤u2009_n_. Examples Input 4 5 0 0 1 0 1 0 1 1 1 0 1 0 1 0 1 1 1 1 1 1 2 3 Note In the second example the upper left square is (1,1) and the lower right is (3,2).
1,200
false
false
true
false
false
false
true
false
false
false
9,736
1279F
New Year is getting near. So it's time to change handles on codeforces. Mishka wants to change his handle but in such a way that people would not forget who he is. To make it work, he only allowed to change letters case. More formally, during one handle change he can choose any segment of his handle $$$[i; i + l - 1]$$$ and apply tolower or toupper to all letters of his handle on this segment (more fomally, replace all uppercase letters with corresponding lowercase or vice versa). The length $$$l$$$ is fixed for all changes. Because it is not allowed to change codeforces handle too often, Mishka can perform at most $$$k$$$ such operations. What is the minimum value of $$$min(lower, upper)$$$ (where $$$lower$$$ is the number of lowercase letters, and $$$upper$$$ is the number of uppercase letters) can be obtained after optimal sequence of changes? Input The first line of the input contains three integers $$$n, k$$$ and $$$l$$$ ($$$1 le n, k, l le 10^6, l le n)$$$ — the length of Mishka's handle, the number of changes and the length of the segment. The second line of the input contains one string $$$s$$$, consisting of $$$n$$$ lowercase and uppercase Latin letters — Mishka's handle. Output Print one integer — the minimum value of $$$min(lower, upper)$$$ after that Mishka change his handle at most $$$k$$$ times in a way described in the problem statement. Examples Input 15 2 2 AaAaAAaaAAAAaaA Input 14 2 6 aBcdEFGHIJklMn
2,800
false
false
false
true
false
false
false
true
false
false
4,295
202B
A widely known among some people Belarusian sport programmer Lesha decided to make some money to buy a one square meter larger flat. To do this, he wants to make and carry out a Super Rated Match (SRM) on the site Torcoder.com. But there's a problem — a severe torcoder coordinator Ivan does not accept any Lesha's problem, calling each of them an offensive word "duped" (that is, duplicated). And one day they nearely quarrelled over yet another problem Ivan wouldn't accept. You are invited to act as a fair judge and determine whether the problem is indeed brand new, or Ivan is right and the problem bears some resemblance to those used in the previous SRMs. You are given the descriptions of Lesha's problem and each of Torcoder.com archive problems. The description of each problem is a sequence of words. Besides, it is guaranteed that Lesha's problem has no repeated words, while the description of an archive problem may contain any number of repeated words. The "similarity" between Lesha's problem and some archive problem can be found as follows. Among all permutations of words in Lesha's problem we choose the one that occurs in the archive problem as a subsequence. If there are multiple such permutations, we choose the one with the smallest number of inversions. Then the "similarity" of a problem can be written as , where _n_ is the number of words in Lesha's problem and _x_ is the number of inversions in the chosen permutation. Note that the "similarity" _p_ is always a positive integer. The problem is called brand new if there is not a single problem in Ivan's archive which contains a permutation of words from Lesha's problem as a subsequence. Help the boys and determine whether the proposed problem is new, or specify the problem from the archive which resembles Lesha's problem the most, otherwise. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20094) — the number of words in Lesha's problem. The second line contains _n_ space-separated words — the short description of the problem. The third line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u200910) — the number of problems in the Torcoder.com archive. Next _m_ lines contain the descriptions of the problems as "_k_ _s_1 _s_2 ... _s__k_", where _k_ (1u2009≤u2009_k_u2009≤u200920) is the number of words in the problem and _s__i_ is a word of the problem description. All words from all problem descriptions contain no more than 10 lowercase English letters. Output If Lesha's problem is brand new, print string "Brand new problem!" (without quotes). Otherwise, on the first line print the index of the archive problem which resembles Lesha's problem most. If there are multiple such problems, print the one with the smallest index. On the second line print a string consisting of characters [:, character repeated _p_ times, and characters :], where _p_ is the "similarity" between this problem and Lesha's one. The archive problems are numbered starting from one in the order in which they are given in the input. Examples Input 4 find the next palindrome 1 10 find the previous palindrome or print better luck next time Input 3 add two numbers 3 1 add 2 two two 3 numbers numbers numbers Input 4 these papers are formulas 3 6 what are these formulas and papers 5 papers are driving me crazy 4 crazy into the night Input 3 add two decimals 5 4 please two decimals add 5 decimals want to be added 4 two add decimals add 4 add one two three 7 one plus two plus three equals six Note Let us remind you that the number of inversions is the number of pairs of words that follow in the permutation not in their original order. Thus, for example, if the original problem is "add two numbers", then permutation "numbers add two" contains two inversions — pairs of words "numbers" and "add", "numbers" and "two". Sequence _b_1,u2009u2009_b_2,u2009u2009...,u2009u2009_b__k_ is a subsequence of sequence _a_1,u2009_a_2,u2009u2009...,u2009u2009_a__n_ if there exists such a set of indices 1u2009≤u2009_i_1u2009<u2009u2009_i_2u2009<u2009... u2009u2009<u2009_i__k_u2009≤u2009_n_ that _a__i__j_u2009u2009=u2009u2009_b__j_ (in other words, if sequence _b_ can be obtained from _a_ by deleting some of its elements). In the first test case the first problem contains the "find the palindrome next" permutation as a subsequence, in which the number of inversions equals 1 (words "palindrome" and "next"). In the second test case there is no problem that contains a permutation of words from Lesha's problem as a subsequence.
1,700
false
false
false
false
false
false
true
false
false
false
9,033
875E
Petya and Vasya got employed as couriers. During the working day they are to deliver packages to _n_ different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate _s_1, Vasya is at the point with the coordinate _s_2, and the clients are at the points _x_1,u2009_x_2,u2009...,u2009_x__n_ in the order of the required visit. The guys agree in advance who of them will deliver the package to which of the customers, and then they act as follows. When the package for the _i_-th client is delivered, the one who delivers the package to the (_i_u2009+u20091)-st client is sent to the path (it can be the same person who went to the point _x__i_, or the other). The friend who is not busy in delivering the current package, is standing still. To communicate with each other, the guys have got walkie-talkies. The walkie-talkies work rather poorly at great distances, so Petya and Vasya want to distribute the orders so that the maximum distance between them during the day is as low as possible. Help Petya and Vasya to minimize the maximum distance between them, observing all delivery rules. Input The first line contains three integers _n_, _s_1, _s_2 (1u2009≤u2009_n_u2009≤u2009100u2009000, 0u2009≤u2009_s_1,u2009_s_2u2009≤u2009109)xa0— number of points of delivery and starting positions of Petya and Vasya. The second line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_xa0— customers coordinates (0u2009≤u2009_x__i_u2009≤u2009109), in the order to make a delivery. It is guaranteed, that among the numbers _s_1,u2009_s_2,u2009_x_1,u2009...,u2009_x__n_ there are no two equal. Output Output the only integer, minimum possible maximal distance between couriers during delivery. Note In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point. In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Petya to the second and, finally, Vasya delivers the package to the third client. With this order of delivery, the distance between the couriers will never exceed 1. In the third test case only two variants are possible: if the delivery of a single package is carried out by Petya, the maximum distance between them will be 5u2009-u20092u2009=u20093. If Vasya will deliver the package, the maximum distance is 4u2009-u20092u2009=u20092. The latter method is optimal.
2,600
false
false
false
true
true
false
false
true
false
false
6,244
23A
Problem - 23A - Codeforces =============== xa0 . Input The first input line contains the string. It's guaranteed, that the string is non-empty, consists of lower-case Latin letters, and its length doesn't exceed 100. Output Output one number — length of the longest substring that can be met in the string at least twice. Examples Input abcd Output 0 Input ababa Output 3 Input zzz Output 2
1,200
false
true
false
false
false
false
true
false
false
false
9,883
1936F
You are given $$$n$$$ circles on the plane. The $$$i$$$-th of these circles is given by a tuple of integers $$$(x_i, y_i, r_i)$$$, where $$$(x_i, y_i)$$$ are the coordinates of its center, and $$$r_i$$$ is the radius of the circle. Please find a circle $$$C$$$ which meets the following conditions: $$$C$$$ is contained inside all $$$n$$$ circles given in the input. Among all circles $$$C$$$ that meet the first condition, the radius of the circle is maximum. Let the largest suitable circle have the radius of $$$a$$$. Your output $$$C$$$, described as $$$(x,y,r)$$$, will be accepted if it meets the following conditions: For each $$$i$$$, $$$sqrt{(x_i-x)^2+(y_i-y)^2}+ r le r_i+max(a,1)cdot 10^{-7}$$$. The absolute or relative error of $$$r$$$ does not exceed $$$10^{-7}$$$. Formally, your answer is accepted if and only if $$$frac{leftr - a ight}{max(1, a)} le 10^{-7}$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of circles. The $$$i$$$-th of the following $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$r_i$$$ ($$$-10^6 le x_i,y_i le 10^6$$$, $$$1 le r_i le 2 cdot 10^6$$$). It is guaranteed that there is a circle with a radius of at least $$$10^{-6}$$$ which is contained inside all $$$n$$$ circles. Output Output three real values, $$$x$$$, $$$y$$$, and $$$r$$$xa0— the coordinates of the center and the radius of the circle. Examples Input 4 1 1 3 -1 1 3 1 -1 2 -1 -1 2 Output 0.0000000000000000 -0.7637626158259733 0.9724747683480533 Input 4 41580 -23621 95642 -41580 -23621 95642 0 47821 95642 0 0 109750 Output 0.0000000000000000 0.0000000000000000 47821.0000000000000000 Note A two-dimensional plot depicting the first test case is given below. The output circle $$$C$$$ is dashed with blue lines. A two-dimensional plot depicting the second test case is given below. The output circle $$$C$$$ is dashed with blue lines.
3,300
false
false
false
false
false
false
false
true
false
false
663
1186C
Vus the Cossack has two binary strings, that is, strings that consist only of "0" and "1". We call these strings $$$a$$$ and $$$b$$$. It is known that $$$b leq a$$$, that is, the length of $$$b$$$ is at most the length of $$$a$$$. The Cossack considers every substring of length $$$b$$$ in string $$$a$$$. Let's call this substring $$$c$$$. He matches the corresponding characters in $$$b$$$ and $$$c$$$, after which he counts the number of positions where the two strings are different. We call this function $$$f(b, c)$$$. For example, let $$$b = 00110$$$, and $$$c = 11000$$$. In these strings, the first, second, third and fourth positions are different. Vus the Cossack counts the number of such substrings $$$c$$$ such that $$$f(b, c)$$$ is even. For example, let $$$a = 01100010$$$ and $$$b = 00110$$$. $$$a$$$ has four substrings of the length $$$b$$$: $$$01100$$$, $$$11000$$$, $$$10001$$$, $$$00010$$$. $$$f(00110, 01100) = 2$$$; $$$f(00110, 11000) = 4$$$; $$$f(00110, 10001) = 4$$$; $$$f(00110, 00010) = 1$$$. Since in three substrings, $$$f(b, c)$$$ is even, the answer is $$$3$$$. Vus can not find the answer for big strings. That is why he is asking you to help him. Note The first example is explained in the legend. In the second example, there are five substrings that satisfy us: $$$1010$$$, $$$0101$$$, $$$1111$$$, $$$1111$$$.
1,800
true
false
true
false
false
false
false
false
false
false
4,770
1063E
Oleg came to see the maze of mirrors. The maze is a $$$n$$$ by $$$n$$$ room in which each cell is either empty or contains a mirror connecting opposite corners of this cell. Mirrors in this maze reflect light in a perfect way, which causes the interesting visual effects and contributes to the loss of orientation in the maze. Oleg is a person of curious nature, so he decided to install $$$n$$$ lasers facing internal of the maze on the south wall of the maze. On the north wall of the maze, Oleg installed $$$n$$$ receivers, also facing internal of the maze. Let's number lasers and receivers from west to east with distinct integers from $$$1$$$ to $$$n$$$. Each laser sends a beam of some specific kind and receiver with number $$$a_i$$$ should receive the beam sent from laser number $$$i$$$. Since two lasers' beams can't come to the same receiver, these numbers form a permutationxa0— each of the receiver numbers occurs exactly once. You came to the maze together with Oleg. Help him to place the mirrors in the initially empty maze so that the maximum number of lasers' beams will come to the receivers they should. There are no mirrors outside the maze, so if the laser beam leaves the maze, it will not be able to go back. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 1000$$$)xa0— the size of the maze. The second line contains a permutation of $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$ defines the number of the receiver, to which the beam from $$$i$$$-th laser should come. Output In the first line print the maximum possible number of laser beams, which can come to the receivers they should. In the next $$$n$$$ lines of length $$$n$$$ print the arrangement of mirrors, causing such number of laser beams to come where they should. If the corresponding cell is empty, print ".", otherwise print "/" or "", depending on the orientation of the mirror. In your output north should be above, south should be below, and west and east should be on left and on the right respectively. It is allowed for laser beams to come not to the receivers they correspond to, but they are not counted in the answer. If there are multiple arrangements of mirrors leading to the optimal answerxa0— print any of them. Note The picture illustrates the arrangements of the mirrors in the first example.
3,000
true
false
false
false
false
true
false
false
false
false
5,392
845D
Polycarp has just attempted to pass the driving test. He ran over the straight road with the signs of four types. speed limit: this sign comes with a positive integer number — maximal speed of the car after the sign (cancel the action of the previous sign of this type); overtake is allowed: this sign means that after some car meets it, it can overtake any other car; no speed limit: this sign cancels speed limit if any (car can move with arbitrary speed after this sign); no overtake allowed: some car can't overtake any other car after this sign. Polycarp goes past the signs consequentially, each new sign cancels the action of all the previous signs of it's kind (speed limit/overtake). It is possible that two or more "no overtake allowed" signs go one after another with zero "overtake is allowed" signs between them. It works with "no speed limit" and "overtake is allowed" signs as well. In the beginning of the ride overtake is allowed and there is no speed limit. You are given the sequence of events in chronological order — events which happened to Polycarp during the ride. There are events of following types: 1. Polycarp changes the speed of his car to specified (this event comes with a positive integer number); 2. Polycarp's car overtakes the other car; 3. Polycarp's car goes past the "speed limit" sign (this sign comes with a positive integer); 4. Polycarp's car goes past the "overtake is allowed" sign; 5. Polycarp's car goes past the "no speed limit"; 6. Polycarp's car goes past the "no overtake allowed"; It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified). After the exam Polycarp can justify his rule violations by telling the driving instructor that he just didn't notice some of the signs. What is the minimal number of signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view? Input The first line contains one integer number _n_ (1u2009≤u2009_n_u2009≤u20092·105) — number of events. Each of the next _n_ lines starts with integer _t_ (1u2009≤u2009_t_u2009≤u20096) — the type of the event. An integer _s_ (1u2009≤u2009_s_u2009≤u2009300) follows in the query of the first and the third type (if it is the query of first type, then it's new speed of Polycarp's car, if it is the query of third type, then it's new speed limit). It is guaranteed that the first event in chronological order is the event of type 1 (Polycarp changed the speed of his car to specified). Output Print the minimal number of road signs Polycarp should say he didn't notice, so that he would make no rule violations from his point of view. Examples Input 11 1 100 3 70 4 2 3 120 5 3 120 6 1 150 4 3 300 Note In the first example Polycarp should say he didn't notice the "speed limit" sign with the limit of 70 and the second "speed limit" sign with the limit of 120. In the second example Polycarp didn't make any rule violation. In the third example Polycarp should say he didn't notice both "no overtake allowed" that came after "overtake is allowed" sign.
1,800
false
true
false
true
true
false
false
false
false
false
6,381
1787D
There are $$$n$$$ points $$$1,2,ldots,n$$$, each point $$$i$$$ has a number $$$a_i$$$ on it. You're playing a game on them. Initially, you are at point $$$1$$$. When you are at point $$$i$$$, take following steps: If $$$1le ile n$$$, go to $$$i+a_i$$$, Otherwise, the game ends. Before the game begins, you can choose two integers $$$x$$$ and $$$y$$$ satisfying $$$1le xle n$$$, $$$-n le y le n$$$ and replace $$$a_x$$$ with $$$y$$$ (set $$$a_x := y$$$). Find the number of distinct pairs $$$(x,y)$$$ such that the game that you start after making the change ends in a finite number of steps. Notice that you do not have to satisfy $$$a_x ot=y$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1le tle 10^4)$$$ — the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$) — the number of points. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$-n le a_i le n$$$) — the numbers on the axis. It's guaranteed that the sum of $$$n$$$ does not exceed $$$2cdot 10^5$$$. Note In the first test case, the pairs $$$(x,y)$$$ with which the game ends are $$$(1,-1)$$$ and $$$(1,1)$$$, corresponding to the routes $$$1 ightarrow 0$$$ and $$$1 ightarrow 2$$$. Note that $$$(1,2)$$$ is invalid since when $$$n=1$$$, $$$y=2$$$ violates $$$-nle yle n$$$. $$$(1,0)$$$ is also invalid since you will go from $$$1$$$ to $$$1$$$ forever. In the second test case, the pairs are $$$(1,-2),(1,-1),(1,2),(2,-2),(2,-1),(2,0),(2,1),(2,2)$$$. In the fourth test case, the pairs are $$$(1,-2),(1,-1),(1,1),(1,2),(2,-2),(2,1),(2,2)$$$.
1,900
false
false
true
false
false
false
false
false
false
true
1,560
459E
Pashmak's homework is a problem about graphs. Although he always tries to do his homework completely, he can't solve this problem. As you know, he's really weak at graph theory; so try to help him in solving the problem. You are given a weighted directed graph with _n_ vertices and _m_ edges. You need to find a path (perhaps, non-simple) with maximum number of edges, such that the weights of the edges increase along the path. In other words, each edge of the path must have strictly greater weight than the previous edge in the path. Help Pashmak, print the number of edges in the required path. Input The first line contains two integers _n_, _m_ (2u2009≤u2009_n_u2009≤u20093·105;xa01u2009≤u2009_m_u2009≤u2009_min_(_n_·(_n_u2009-u20091),u20093·105)). Then, _m_ lines follows. The _i_-th line contains three space separated integers: _u__i_, _v__i_, _w__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_;xa01u2009≤u2009_w__i_u2009≤u2009105) which indicates that there's a directed edge with weight _w__i_ from vertex _u__i_ to vertex _v__i_. It's guaranteed that the graph doesn't contain self-loops and multiple edges. Output Print a single integer — the answer to the problem. Examples Input 6 7 1 2 1 3 2 5 2 4 2 2 5 2 2 6 9 5 4 3 4 3 4 Note In the first sample the maximum trail can be any of this trails: . In the second sample the maximum trail is . In the third sample the maximum trail is .
1,900
false
false
false
true
false
false
false
false
true
false
8,002
1698G
There is a binary string $$$t$$$ of length $$$10^{100}$$$, and initally all of its bits are $$$ exttt{0}$$$. You are given a binary string $$$s$$$, and perform the following operation some times: Select some substring of $$$t$$$, and replace it with its XOR with $$$s$$$.$$$^dagger$$$ After several operations, the string $$$t$$$ has exactly two bits $$$ exttt{1}$$$; that is, there are exactly two distinct indices $$$p$$$ and $$$q$$$ such that the $$$p$$$-th and $$$q$$$-th bits of $$$t$$$ are $$$ exttt{1}$$$, and the rest of the bits are $$$ exttt{0}$$$. Find the lexicographically largest$$$^ddagger$$$ string $$$t$$$ satisfying these constraints, or report that no such string exists. $$$^dagger$$$ Formally, choose an index $$$i$$$ such that $$$0 leq i leq 10^{100}-s$$$. For all $$$1 leq j leq s$$$, if $$$s_j = exttt{1}$$$, then toggle $$$t_{i+j}$$$. That is, if $$$t_{i+j}= exttt{0}$$$, set $$$t_{i+j}= exttt{1}$$$. Otherwise if $$$t_{i+j}= exttt{1}$$$, set $$$t_{i+j}= exttt{0}$$$. $$$^ddagger$$$ A binary string $$$a$$$ is lexicographically larger than a binary string $$$b$$$ of the same length if in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a bit $$$ exttt{1}$$$ and the corresponding bit in $$$b$$$ is $$$ exttt{0}$$$. Output If no string $$$t$$$ exists as described in the statement, output -1. Otherwise, output the integers $$$p$$$ and $$$q$$$ ($$$1 leq p < q leq 10^{100}$$$) such that the $$$p$$$-th and $$$q$$$-th bits of the lexicographically maximal $$$t$$$ are $$$ exttt{1}$$$. Note In the first test, you can perform the following operations. $$$$$$ exttt{00000}ldots o color{red}{ exttt{1}} exttt{0000}ldots o exttt{1}color{red}{ exttt{1}} exttt{000}ldots$$$$$$ In the second test, you can perform the following operations. $$$$$$ exttt{00000}ldots o color{red}{ exttt{001}} exttt{00}ldots o exttt{0}color{red}{ exttt{011}} exttt{0}ldots$$$$$$ In the third test, you can perform the following operations. $$$$$$ exttt{00000}ldots o color{red}{ exttt{1111}} exttt{0}ldots o exttt{1}color{red}{ exttt{0001}}ldots$$$$$$ It can be proven that these strings $$$t$$$ are the lexicographically largest ones. In the fourth test, you can't make a single bit $$$ exttt{1}$$$, so it is impossible.
2,900
true
false
false
false
false
false
false
false
false
false
2,088
709B
Vasya takes part in the orienteering competition. There are _n_ checkpoints located along the line at coordinates _x_1,u2009_x_2,u2009...,u2009_x__n_. Vasya starts at the point with coordinate _a_. His goal is to visit at least _n_u2009-u20091 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order. Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value. Input The first line of the input contains two integers _n_ and _a_ (1u2009≤u2009_n_u2009≤u2009100u2009000, u2009-u20091u2009000u2009000u2009≤u2009_a_u2009≤u20091u2009000u2009000)xa0— the number of checkpoints and Vasya's starting position respectively. The second line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (u2009-u20091u2009000u2009000u2009≤u2009_x__i_u2009≤u20091u2009000u2009000)xa0— coordinates of the checkpoints. Output Print one integerxa0— the minimum distance Vasya has to travel in order to visit at least _n_u2009-u20091 checkpoint. Note In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12u2009-u200910u2009=u20092) and then proceed to the second one (distance is 12u2009-u20097u2009=u20095). The total distance is equal to 2u2009+u20095u2009=u20097. In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point u2009-u200910.
1,500
false
true
true
false
false
false
false
false
true
false
6,983
1889E
Consider two undirected graphs $$$G_1$$$ and $$$G_2$$$. Every node in $$$G_1$$$ and in $$$G_2$$$ has a label. Doremy calls $$$G_1$$$ and $$$G_2$$$ similar if and only if: The labels in $$$G_1$$$ are distinct, and the labels in $$$G_2$$$ are distinct. The set $$$S$$$ of labels in $$$G_1$$$ coincides with the set of labels in $$$G_2$$$. For every pair of two distinct labels $$$u$$$ and $$$v$$$ in $$$S$$$, the corresponding nodes are in the same connected component in $$$G_1$$$ if and only if they are in the same connected component in $$$G_2$$$. Now Doremy gives you two trees $$$T_1$$$ and $$$T_2$$$ with $$$n$$$ nodes, labeled from $$$1$$$ to $$$n$$$. You can do the following operation any number of times: Choose an edge set $$$E_1$$$ from $$$T_1$$$ and an edge set $$$E_2$$$ from $$$T_2$$$, such that $$$overline{E_1}$$$ and $$$overline{E_2}$$$ are similar. Here $$$overline{E}$$$ represents the graph which is given by only reserving the edge set $$$E$$$ from $$$T$$$ (i.e., the edge-induced subgraph). In other words, $$$overline{E}$$$ is obtained from $$$T$$$ by removing all edges not included in $$$E$$$ and further removing all isolated vertices. Swap the edge set $$$E_1$$$ in $$$T_1$$$ with the edge set $$$E_2$$$ in $$$T_2$$$. Now Doremy is wondering how many distinct $$$T_1$$$ you can get after any number of operations. Can you help her find the answer? Output the answer modulo $$$10^9+7$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 2cdot 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line contains an integer $$$n$$$ ($$$2le nle 10^5$$$)xa0— the number of nodes in the trees $$$T_1$$$ and $$$T_2$$$. Each of the following $$$n-1$$$ lines contain two integers $$$u,v$$$ ($$$1le u,vle n$$$), representing an undirected edge in $$$T_1$$$. It is guaranteed these edges form a tree. Each of the following $$$n-1$$$ lines contain two integers $$$u,v$$$ ($$$1le u,vle n$$$), representing an undirected edge in $$$T_2$$$. It is guaranteed these edges form a tree. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2cdot 10^5$$$. Output For each test case, you should output a single line with an integer, representing the number of distinct $$$T_1$$$ after any number of operations, modulo $$$10^9+7$$$. Example Input 3 2 1 2 2 1 3 1 3 2 3 2 3 2 1 4 1 2 2 3 3 4 4 2 2 1 1 3 Note In the first test case, there is at most one distinct $$$T_1$$$ having the only edge $$$(1,2)$$$. In the second test case, you can choose the edge set $$${(1,3),(2,3)}$$$ in $$$T_1$$$, the edge set $$${(1,2),(2,3)}$$$ in $$$T_2$$$ and swap them. So $$$T_1$$$ can be $$$1-3-2$$$ or $$$1-2-3$$$. In the third test case, there are $$$4$$$ distinct $$$T_1$$$, as the following pictures.
3,500
false
false
false
false
false
false
false
false
false
true
961
1089J
International Coding Procedures Company (ICPC) writes all its code in Jedi Script (JS) programming language. JS does not get compiled, but is delivered for execution in its source form. Sources contain comments, extra whitespace (including trailing and leading spaces), and other non-essential features that make them quite large but do not contribute to the semantics of the code, so the process of minification is performed on source files before their delivery to execution to compress sources while preserving their semantics. You are hired by ICPC to write JS minifier for ICPC. Fortunately, ICPC adheres to very strict programming practices and their JS sources are quite restricted in grammar. They work only on integer algorithms and do not use floating point numbers and strings. Every JS source contains a sequence of lines. Each line contains zero or more tokens that can be separated by spaces. On each line, a part of the line that starts with a hash character ('#' code 35), including the hash character itself, is treated as a comment and is ignored up to the end of the line. Each line is parsed into a sequence of tokens from left to right by repeatedly skipping spaces and finding the longest possible token starting at the current parsing position, thus transforming the source code into a sequence of tokens. All the possible tokens are listed below: A reserved token is any kind of operator, separator, literal, reserved word, or a name of a library function that should be preserved during the minification process. Reserved tokens are fixed strings of non-space ASCII characters that do not contain the hash character ('#' code 35). All reserved tokens are given as an input to the minification process. A number token consists of a sequence of digits, where a digit is a character from zero ('0') to nine ('9') inclusive. A word token consists of a sequence of characters from the following set: lowercase letters, uppercase letters, digits, underscore ('_' code 95), and dollar sign ('$' code 36). A word does not start with a digit. Note, that during parsing the longest sequence of characters that satisfies either a number or a word definition, but that appears in the list of reserved tokens, is considered to be a reserved token instead. During the minification process words are renamed in a systematic fashion using the following algorithm: 1. Take a list of words that consist only of lowercase letters ordered first by their length, then lexicographically: "a", "b", ..., "z", "aa", "ab", ..., excluding reserved tokens, since they are not considered to be words. This is the target word list. 2. Rename the first word encountered in the input token sequence to the first word in the target word list and all further occurrences of the same word in the input token sequence, too. Rename the second new word encountered in the input token sequence to the second word in the target word list, and so on. The goal of the minification process is to convert the given source to the shortest possible line (counting spaces) that still parses to the same sequence of tokens with the correspondingly renamed words using these JS parsing rules. Input The first line of the input contains a single integer $$$n$$$ ($$$0 le n le 40$$$)xa0— the number of reserved tokens. The second line of the input contains the list of reserved tokens separated by spaces without repetitions in the list. Each reserved token is at least one and at most 20 characters long and contains only characters with ASCII codes from 33 (exclamation mark) to 126 (tilde) inclusive, with exception of a hash character ('#' code 35). The third line of the input contains a single integer $$$m$$$ ($$$1 le m le 40$$$)xa0— the number of lines in the input source code. Next $$$m$$$ lines contain the input source, each source line is at most 80 characters long (counting leading and trailing spaces). Each line contains only characters with ASCII codes from 32 (space) to 126 (tilde) inclusive. The source code is valid and fully parses into a sequence of tokens. Output Write to the output a single line that is the result of the minification process on the input source code. The output source line shall parse to the same sequence of tokens as the input source with the correspondingly renamed words and shall contain the minimum possible number of spaces needed for that. If there are multiple ways to insert the minimum possible number of spaces into the output, use any way.
3,200
false
true
true
false
false
false
false
false
false
false
5,276
315A
Problem - 315A - 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 *1400 No tag edit access → Contest materials — the number of bottles. The next _n_ lines contain the bottles' description. The _i_-th line contains two integers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u20091000) — the description of the _i_-th bottle. Output In a single line print a single integer — the answer to the problem. Examples Input 4 1 1 2 2 3 3 4 4 Output 4 Input 4 1 2 2 3 3 4 4 1 Output 0
1,400
false
false
false
false
false
false
true
false
false
false
8,585
1900B
Laura is a girl who does not like combinatorics. Nemanja will try to convince her otherwise. Nemanja wrote some digits on the board. All of them are either $$$1$$$, $$$2$$$, or $$$3$$$. The number of digits $$$1$$$ is $$$a$$$. The number of digits $$$2$$$ is $$$b$$$ and the number of digits $$$3$$$ is $$$c$$$. He told Laura that in one operation she can do the following: Select two different digits and erase them from the board. After that, write the digit ($$$1$$$, $$$2$$$, or $$$3$$$) different from both erased digits. For example, let the digits be $$$1$$$, $$$1$$$, $$$1$$$, $$$2$$$, $$$3$$$, $$$3$$$. She can choose digits $$$1$$$ and $$$3$$$ and erase them. Then the board will look like this $$$1$$$, $$$1$$$, $$$2$$$, $$$3$$$. After that, she has to write another digit $$$2$$$, so at the end of the operation, the board will look like $$$1$$$, $$$1$$$, $$$2$$$, $$$3$$$, $$$2$$$. Nemanja asked her whether it was possible for only digits of one type to remain written on the board after some operations. If so, which digits can they be? Laura was unable to solve this problem and asked you for help. As an award for helping her, she will convince Nemanja to give you some points. 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 and only line of each test case contains three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 le a, b, c le 100$$$)xa0— the number of ones, number of twos, and number of threes, respectively. Output For each test case, output one line containing $$$3$$$ integers. The first one should be $$$1$$$ if it is possible that after some operations only digits $$$1$$$ remain on the board, and $$$0$$$ otherwise. Similarly, the second one should be $$$1$$$ if it is possible that after some operations only digits $$$2$$$ remain on the board, and $$$0$$$ otherwise. Similarly, the third one should be $$$1$$$ if it is possible that after some operations only digits $$$3$$$ remain on the board, and $$$0$$$ otherwise. Note In the first test case, Laura can remove digits $$$2$$$ and $$$3$$$ and write digit $$$1$$$. After that, the board will have $$$2$$$ digits $$$1$$$. She can make it have only digits $$$2$$$ or $$$3$$$ left by performing a similar operation. In the second test case, she can remove digits $$$1$$$ and $$$3$$$ and write a digit $$$2$$$. After performing that operation $$$2$$$ times, the board will have only digits $$$2$$$ left. It can be proven that there is no way to have only digits $$$1$$$ or only digits $$$3$$$ left. In the third test case, there is a sequence of operations that leaves only digits $$$1$$$ on the board. It can be proven that there is no way to have only digits $$$2$$$ or only digits $$$3$$$ left.
900
true
false
false
true
false
false
false
false
false
false
914
1669D
A row of $$$n$$$ cells is given, all initially white. Using a stamp, you can stamp any two neighboring cells such that one becomes red and the other becomes blue. A stamp can be rotated, i.e. it can be used in both ways: as $$$color{blue}{ exttt{B}}color{red}{ exttt{R}}$$$ and as $$$color{red}{ exttt{R}}color{blue}{ exttt{B}}$$$. During use, the stamp must completely fit on the given $$$n$$$ cells (it cannot be partially outside the cells). The stamp can be applied multiple times to the same cell. Each usage of the stamp recolors both cells that are under the stamp. For example, one possible sequence of stamps to make the picture $$$color{blue}{ exttt{B}}color{red}{ exttt{R}}color{blue}{ exttt{B}}color{blue}{ exttt{B}} exttt{W}$$$ could be $$$ exttt{WWWWW} o exttt{WW}color{brown}{underline{color{red}{ exttt{R}}color{blue}{ exttt{B}}}} exttt{W} o color{brown}{underline{color{blue}{ exttt{B}}color{red}{ exttt{R}}}}color{red}{ exttt{R}}color{blue}{ exttt{B}} exttt{W} o color{blue}{ exttt{B}}color{brown}{underline{color{red}{ exttt{R}}color{blue}{ exttt{B}}}}color{blue}{ exttt{B}} exttt{W}$$$. Here $$$ exttt{W}$$$, $$$color{red}{ exttt{R}}$$$, and $$$color{blue}{ exttt{B}}$$$ represent a white, red, or blue cell, respectively, and the cells that the stamp is used on are marked with an underline. Given a final picture, is it possible to make it using the stamp zero or more times? Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the length of the picture. The second line of each test case contains a string $$$s$$$xa0— the picture you need to make. It is guaranteed that the length of $$$s$$$ is $$$n$$$ and that $$$s$$$ only consists of the characters $$$ exttt{W}$$$, $$$ exttt{R}$$$, and $$$ exttt{B}$$$, representing a white, red, or blue cell, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output Output $$$t$$$ lines, each of which contains the answer to the corresponding test case. As an answer, output "YES" if it possible to make the picture using the stamp zero or more times, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 12 5 BRBBW 1 B 2 WB 2 RW 3 BRB 3 RBB 7 WWWWWWW 9 RBWBWRRBW 10 BRBRBRBRRB 12 BBBRWWRRRWBR 10 BRBRBRBRBW 5 RBWBW Output YES NO NO NO YES YES YES NO YES NO YES NO Note The first test case is explained in the statement. For the second, third, and fourth test cases, it is not possible to stamp a single cell, so the answer is "NO". For the fifth test case, you can use the stamp as follows: $$$ exttt{WWW} o exttt{W}color{brown}{underline{color{red}{ exttt{R}}color{blue}{ exttt{B}}}} o color{brown}{underline{color{blue}{ exttt{B}}color{red}{ exttt{R}}}}color{blue}{ exttt{B}}$$$. For the sixth test case, you can use the stamp as follows: $$$ exttt{WWW} o exttt{W}color{brown}{underline{color{red}{ exttt{R}}color{blue}{ exttt{B}}}} o color{brown}{underline{color{red}{ exttt{R}}color{blue}{ exttt{B}}}}color{blue}{ exttt{B}}$$$. For the seventh test case, you don't need to use the stamp at all.
1,100
false
false
true
false
false
false
false
false
false
false
2,262
959C
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an undirected tree consisting of _n_ nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (_u_,u2009_v_) that belongs to the tree, either _u_ is in the set, or _v_ is in the set, or both are in the set. Mahmoud has found the following algorithm: Root the tree at node 1. Count the number of nodes at an even depth. Let it be _evenCnt_. Count the number of nodes at an odd depth. Let it be _oddCnt_. The answer is the minimum between _evenCnt_ and _oddCnt_. The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0. Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of _n_ nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one. Input The only line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009105), the number of nodes in the desired trees. Output The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only. If the answer for a section exists, it should contain _n_u2009-u20091 lines, each containing 2 space-separated integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_), which means that there's an undirected edge between node _u_ and node _v_. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict. If there are multiple answers, you can print any of them. Examples Output 1 2 1 3 2 4 2 5 3 6 4 7 4 8 1 2 1 3 2 4 2 5 2 6 3 7 6 8 Note In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed u2009-u20091 in the first section, but notice that we printed this tree in the second section. In the second sample: In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: In the second tree, the algorithm will find an answer with 3 nodes which is correct:
1,500
false
false
false
false
false
true
false
false
false
false
5,875
45A
Problem - 45A - 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 *900 No tag edit access → Contest materials ") — the number of months left till the appearance of Codecraft III. Output Print starting from an uppercase letter the name of the month in which the continuation of Codeforces II will appear. The printed name must be contained in the list January, February, March, April, May, June, July, August, September, October, November, December. Examples Input November 3 Output February Input May 24 Output May
900
false
false
true
false
false
false
false
false
false
false
9,759
1681B
Monocarp has just learned a new card trick, and can't wait to present it to you. He shows you the entire deck of $$$n$$$ cards. You see that the values of cards from the topmost to the bottommost are integers $$$a_1, a_2, dots, a_n$$$, and all values are different. Then he asks you to shuffle the deck $$$m$$$ times. With the $$$j$$$-th shuffle, you should take $$$b_j$$$ topmost cards and move them under the remaining $$$(n - b_j)$$$ cards without changing the order. And then, using some magic, Monocarp tells you the topmost card of the deck. However, you are not really buying that magic. You tell him that you know the topmost card yourself. Can you surprise Monocarp and tell him the topmost card before he shows it? 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 2 cdot 10^5$$$)xa0— the number of cards in the deck. The second line contains $$$n$$$ pairwise distinct integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the values of the cards. The third line contains a single integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$)xa0— the number of shuffles. The fourth line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_j le n - 1$$$)xa0— the amount of cards that are moved on the $$$j$$$-th shuffle. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the value of the card on the top of the deck after the deck is shuffled $$$m$$$ times. Example Input 3 2 1 2 3 1 1 1 4 3 1 4 2 2 3 1 5 2 1 5 4 3 5 3 2 1 2 1 Note In the first testcase, each shuffle effectively swaps two cards. After three swaps, the deck will be $$$[2, 1]$$$. In the second testcase, the second shuffle cancels what the first shuffle did. First, three topmost cards went underneath the last card, then that card went back below the remaining three cards. So the deck remained unchanged from the initial onexa0— the topmost card has value $$$3$$$.
800
true
false
true
false
false
false
false
false
false
false
2,184
895B
Problem - 895B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search math sortings two pointers *1700 No tag edit access → Contest materials ") Editorial") such that _a__i_u2009≤u2009_a__j_ and there are exactly _k_ integers _y_ such that _a__i_u2009≤u2009_y_u2009≤u2009_a__j_ and _y_ is divisible by _x_. In this problem it is meant that pair (_i_,u2009_j_) is equal to (_j_,u2009_i_) only if _i_ is equal to _j_. For example pair (1,u20092) is not the same as (2,u20091). Input The first line contains 3 integers _n_,u2009_x_,u2009_k_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_x_u2009≤u2009109,u20090u2009≤u2009_k_u2009≤u2009109), where _n_ is the size of the array _a_ and _x_ and _k_ are numbers from the statement. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the elements of the array _a_. Output Print one integerxa0— the answer to the problem. Examples Input 4 2 1 1 3 5 7 Output 3 Input 4 2 0 5 3 1 7 Output 4 Input 5 3 1 3 3 3 3 3 Output 25 Note In first sample there are only three suitable pairs of indexesxa0— (1,u20092),u2009(2,u20093),u2009(3,u20094). In second sample there are four suitable pairs of indexes(1,u20091),u2009(2,u20092),u2009(3,u20093),u2009(4,u20094). In third sample every pair (_i_,u2009_j_) is suitable, so the answer is 5u2009*u20095u2009=u200925.
1,700
true
false
false
false
false
false
false
true
true
false
6,169
838B
You are given a directed weighted graph with _n_ nodes and 2_n_u2009-u20092 edges. The nodes are labeled from 1 to _n_, while the edges are labeled from 1 to 2_n_u2009-u20092. The graph's edges can be split into two parts. The first _n_u2009-u20091 edges will form a rooted spanning tree, with node 1 as the root. All these edges will point away from the root. The last _n_u2009-u20091 edges will be from node _i_ to node 1, for all 2u2009≤u2009_i_u2009≤u2009_n_. You are given _q_ queries. There are two types of queries 1 _i_ _w_: Change the weight of the _i_-th edge to _w_ 2 _u_ _v_: Print the length of the shortest path between nodes _u_ to _v_ Given these queries, print the shortest path lengths. Input The first line of input will contain two integers _n_,u2009_q_ (2u2009≤u2009_n_,u2009_q_u2009≤u2009200u2009000), the number of nodes, and the number of queries, respectively. The next 2_n_u2009-u20092 integers will contain 3 integers _a__i_,u2009_b__i_,u2009_c__i_, denoting a directed edge from node _a__i_ to node _b__i_ with weight _c__i_. The first _n_u2009-u20091 of these lines will describe a rooted spanning tree pointing away from node 1, while the last _n_u2009-u20091 of these lines will have _b__i_u2009=u20091. More specifically, The edges (_a_1,u2009_b_1),u2009(_a_2,u2009_b_2),u2009... (_a__n_u2009-u20091,u2009_b__n_u2009-u20091) will describe a rooted spanning tree pointing away from node 1. _b__j_u2009=u20091 for _n_u2009≤u2009_j_u2009≤u20092_n_u2009-u20092. _a__n_,u2009_a__n_u2009+u20091,u2009...,u2009_a_2_n_u2009-u20092 will be distinct and between 2 and _n_. The next _q_ lines will contain 3 integers, describing a query in the format described in the statement. All edge weights will be between 1 and 106. Output For each type 2 query, print the length of the shortest path in its own line. Example Input 5 9 1 3 1 3 2 2 1 4 3 3 5 4 5 1 5 3 1 6 2 1 7 4 1 8 2 1 1 2 1 3 2 3 5 2 5 2 1 1 100 2 1 3 1 8 30 2 4 2 2 2 4
2,100
false
false
false
false
true
false
false
false
false
false
6,413
835E
Pay attention: this problem is interactive. Penguin Xoriy came up with a new game recently. He has _n_ icicles numbered from 1 to _n_. Each icicle has a temperaturexa0— an integer from 1 to 109. Exactly two of these icicles are special: their temperature is _y_, while a temperature of all the others is _x_u2009≠u2009_y_. You have to find those special icicles. You can choose a non-empty subset of icicles and ask the penguin what is the bitwise exclusive OR (XOR) of the temperatures of the icicles in this subset. Note that you can't ask more than 19 questions. You are to find the special icicles. Input The first line contains three integers _n_, _x_, _y_ (2u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_x_,u2009_y_u2009≤u2009109, _x_u2009≠u2009_y_)xa0— the number of icicles, the temperature of non-special icicles and the temperature of the special icicles. Output To give your answer to the penguin you have to print character "!" (without quotes), then print two integers _p_1, _p_2 (_p_1u2009<u2009_p_2)xa0— the indexes of the special icicles in ascending order. Note that "!" and _p_1 should be separated by a space; the indexes should be separated by a space too. After you gave the answer your program should terminate immediately. Interaction To ask a question print character "?" (without quotes), an integer _c_ (1u2009≤u2009_c_u2009≤u2009_n_), and _c_ distinct integers _p_1, _p_2, ..., _p__c_ (1u2009≤u2009_p__i_u2009≤u2009_n_)xa0— the indexes of icicles that you want to know about. Note that "?" and _c_ should be separated by a space; the indexes should be separated by a space too. After you asked the question, read a single integerxa0— the answer. Note that you can't ask more than 19 questions. If you ask more than 19 questions or at least one incorrect question, your solution will get "Wrong answer". If at some moment your program reads u2009-u20091 as an answer, it should immediately exit (for example, by calling exit(0)). You will get "Wrong answer" in this case, it means that you asked more than 19 questions, or asked an invalid question. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including for the final answer . To flush you can use (just after printing): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; For other languages see the documentation. Hacking For hacking use the following format: _n_ _x_ _y_ _p_1 _p_2 Here 1u2009≤u2009_p_1u2009<u2009_p_2u2009≤u2009_n_ are the indexes of the special icicles. Contestant programs will not be able to see this input.
2,400
false
false
false
false
false
true
false
true
false
false
6,423
1385G
You are given a table $$$a$$$ of size $$$2 imes n$$$ (i.e. two rows and $$$n$$$ columns) consisting of integers from $$$1$$$ to $$$n$$$. In one move, you can choose some column $$$j$$$ ($$$1 le j le n$$$) and swap values $$$a_{1, j}$$$ and $$$a_{2, j}$$$ in it. Each column can be chosen no more than once. Your task is to find the minimum number of moves required to obtain permutations of size $$$n$$$ in both first and second rows of the table or determine if it is impossible to do that. You have to answer $$$t$$$ independent test cases. Recall that the permutation of size $$$n$$$ is such an array of size $$$n$$$ that contains each integer from $$$1$$$ to $$$n$$$ exactly once (the order of elements doesn't matter). 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 columns in the table. The second line of the test case contains $$$n$$$ integers $$$a_{1, 1}, a_{1, 2}, dots, a_{1, n}$$$ ($$$1 le a_{1, i} le n$$$), where $$$a_{1, i}$$$ is the $$$i$$$-th element of the first row of the table. The third line of the test case contains $$$n$$$ integers $$$a_{2, 1}, a_{2, 2}, dots, a_{2, n}$$$ ($$$1 le a_{2, i} le n$$$), where $$$a_{2, i}$$$ is the $$$i$$$-th element of the second row of the table. It is 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: -1 if it is impossible to obtain permutation of size $$$n$$$ in both first and the second rows of the table, or one integer $$$k$$$ in the first line, where $$$k$$$ is the minimum number of moves required to obtain permutations in both rows, and $$$k$$$ distinct integers $$$pos_1, pos_2, dots, pos_k$$$ in the second line ($$$1 le pos_i le n$$$) in any order — indices of columns in which you need to swap values to obtain permutations in both rows. If there are several answers, you can print any. Example Input 6 4 1 2 3 4 2 3 1 4 5 5 3 5 1 4 1 2 3 2 4 3 1 2 1 3 3 2 4 1 2 2 1 3 4 3 4 4 4 3 1 4 3 2 2 1 3 1 1 2 3 2 2 Output 0 2 2 3 1 1 2 3 4 2 3 4 -1
2,300
false
false
true
false
false
false
false
false
false
true
3,741
1109F
One fine day Sasha went to the park for a walk. In the park, he saw that his favorite bench is occupied, and he had to sit down on the neighboring one. He sat down and began to listen to the silence. Suddenly, he got a question: what if in different parts of the park, the silence sounds in different ways? So it was. Let's divide the park into $$$1 imes 1$$$ meter squares and call them cells, and numerate rows from $$$1$$$ to $$$n$$$ from up to down, and columns from $$$1$$$ to $$$m$$$ from left to right. And now, every cell can be described with a pair of two integers $$$(x, y)$$$, where $$$x$$$xa0— the number of the row, and $$$y$$$xa0— the number of the column. Sasha knows that the level of silence in the cell $$$(i, j)$$$ equals to $$$f_{i,j}$$$, and all $$$f_{i,j}$$$ form a permutation of numbers from $$$1$$$ to $$$n cdot m$$$. Sasha decided to count, how many are there pleasant segments of silence? Let's take some segment $$$[l ldots r]$$$. Denote $$$S$$$ as the set of cells $$$(i, j)$$$ that $$$l le f_{i,j} le r$$$. Then, the segment of silence $$$[l ldots r]$$$ is pleasant if there is only one simple path between every pair of cells from $$$S$$$ (path can't contain cells, which are not in $$$S$$$). In other words, set $$$S$$$ should look like a tree on a plain. Sasha has done this task pretty quickly, and called the algorithmxa0— "algorithm of silence's sounds". Time passed, and the only thing left from the algorithm is a legend. To prove the truthfulness of this story, you have to help Sasha and to find the number of different pleasant segments of silence. Two segments $$$[l_1 ldots r_1]$$$, $$$[l_2 ldots r_2]$$$ are different, if $$$l_1 eq l_2$$$ or $$$r_1 eq r_2$$$ or both at the same time. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 1000$$$, $$$1 le n cdot m le 2 cdot 10^5$$$)xa0— the size of the park. Each from next $$$n$$$ lines contains $$$m$$$ integers $$$f_{i,j}$$$ ($$$1 le f_{i,j} le n cdot m$$$)xa0— the level of silence in the cell with number $$$(i, j)$$$. It is guaranteed, that all $$$f_{i,j}$$$ are different. Note In the first example, all segments of silence are pleasant. In the second example, pleasant segments of silence are the following:
3,200
false
false
false
false
true
false
false
false
false
false
5,150
1536C
The tycoon of a winery empire in Mondstadt, unmatched in every possible way. A thinker in the Knights of Favonius with an exotic appearance. This time, the brothers are dealing with a strange piece of wood marked with their names. This plank of wood can be represented as a string of $$$n$$$ characters. Each character is either a 'D' or a 'K'. You want to make some number of cuts (possibly $$$0$$$) on this string, partitioning it into several contiguous pieces, each with length at least $$$1$$$. Both brothers act with dignity, so they want to split the wood as evenly as possible. They want to know the maximum number of pieces you can split the wood into such that the ratios of the number of occurrences of 'D' to the number of occurrences of 'K' in each chunk are the same. Kaeya, the curious thinker, is interested in the solution for multiple scenarios. He wants to know the answer for every prefix of the given string. Help him to solve this problem! For a string we define a ratio as $$$a:b$$$ where 'D' appears in it $$$a$$$ times, and 'K' appears $$$b$$$ times. Note that $$$a$$$ or $$$b$$$ can equal $$$0$$$, but not both. Ratios $$$a:b$$$ and $$$c:d$$$ are considered equal if and only if $$$acdot d = bcdot c$$$. For example, for the string 'DDD' the ratio will be $$$3:0$$$, for 'DKD' — $$$2:1$$$, for 'DKK' — $$$1:2$$$, and for 'KKKKDD' — $$$2:4$$$. Note that the ratios of the latter two strings are equal to each other, but they are not equal to the ratios of the first two strings. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 5 cdot 10^5$$$)xa0— the length of the wood. The second line of each test case contains a string $$$s$$$ of length $$$n$$$. Every character of $$$s$$$ will be either 'D' or 'K'. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output $$$n$$$ space separated integers. The $$$i$$$-th of these numbers should equal the answer for the prefix $$$s_{1},s_{2},dots,s_{i}$$$. Note For the first test case, there is no way to partition 'D' or 'DDK' into more than one block with equal ratios of numbers of 'D' and 'K', while you can split 'DD' into 'D' and 'D'. For the second test case, you can split each prefix of length $$$i$$$ into $$$i$$$ blocks 'D'.
1,500
false
false
false
true
true
false
false
false
false
false
2,966
70B
Fangy the little walrus, as all the modern walruses, loves to communicate via text messaging. One day he faced the following problem: When he sends large texts, they are split into parts each containing _n_ characters (which is the size of one text message). Thus, whole sentences and words get split! Fangy did not like it, so he faced the task of breaking the text into minimal messages on his own so that no sentence were broken into pieces when it is sent and the number of text messages to be sent would be minimal. If two consecutive sentences are in different messages, the space between them can be ignored (Fangy does not write this space). The little walrus's text looks in the following manner: TEXT ::= SENTENCE SENTENCE SPACE TEXT SENTENCE ::= WORD SPACE SENTENCE WORD END END ::= {'.', '?', '!'} WORD ::= LETTER LETTER WORD LETTER ::= {'a'..'z', 'A'..'Z'} SPACE ::= ' ' SPACE stands for the symbol of a space. So, how many messages did Fangy send? Input The first line contains an integer _n_, which is the size of one message (2u2009≤u2009_n_u2009≤u2009255). The second line contains the text. The length of the text does not exceed 104 characters. It is guaranteed that the text satisfies the above described format. Specifically, this implies that the text is not empty. Output On the first and only line print the number of text messages Fangy will need. If it is impossible to split the text, print "Impossible" without the quotes. Examples Input 25 Hello. I am a little walrus. Input 19 Hello! Do you like fish? Why? Note Let's take a look at the third sample. The text will be split into three messages: "Hello!", "Do you like fish?" and "Why?".
1,600
false
true
false
false
false
false
false
false
false
false
9,620
1943D1
This is the easy version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if both versions of the problem are solved. An array $$$b$$$ of $$$m$$$ non-negative integers is said to be good if all the elements of $$$b$$$ can be made equal to $$$0$$$ using the following operation some (possibly, zero) times: Select two distinct indices $$$l$$$ and $$$r$$$ ($$$1 leq l color{red}{<} r leq m$$$) and subtract $$$1$$$ from all $$$b_i$$$ such that $$$l leq i leq r$$$. You are given two positive integers $$$n$$$, $$$k$$$ and a prime number $$$p$$$. Over all $$$(k+1)^n$$$ arrays of length $$$n$$$ such that $$$0 leq a_i leq k$$$ for all $$$1 leq i leq n$$$, count the number of good arrays. Since the number might be too large, you are only required to find it modulo $$$p$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains three positive integers $$$n$$$, $$$k$$$ and $$$p$$$ ($$$3 leq n leq 400$$$, $$$1 leq k leq n$$$, $$$10^8 < p < 10^9$$$)xa0— the length of the array $$$a$$$, the upper bound on the elements of $$$a$$$ and modulus $$$p$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$2 cdot 10^5$$$, and $$$p$$$ is prime. Output For each test case, on a new line, output the number of good arrays modulo $$$p$$$. Example Input 4 3 1 998244853 4 1 998244353 3 2 998244353 343 343 998244353 Note In the first test case, the $$$4$$$ good arrays $$$a$$$ are: $$$[0,0,0]$$$; $$$[0,1,1]$$$; $$$[1,1,0]$$$; $$$[1,1,1]$$$.
2,400
true
false
false
true
false
false
true
false
false
false
620
1439A1
This is the easy 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 $$$3nm$$$ 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 3nm$$$)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 into 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,500
false
false
true
false
false
true
false
false
false
false
3,472
1150A
Welcome to Codeforces Stock Exchange! We're pretty limited now as we currently allow trading on one stock, Codeforces Ltd. We hope you'll still be able to make profit from the market! In the morning, there are $$$n$$$ opportunities to buy shares. The $$$i$$$-th of them allows to buy as many shares as you want, each at the price of $$$s_i$$$ bourles. In the evening, there are $$$m$$$ opportunities to sell shares. The $$$i$$$-th of them allows to sell as many shares as you want, each at the price of $$$b_i$$$ bourles. You can't sell more shares than you have. It's morning now and you possess $$$r$$$ bourles and no shares. What is the maximum number of bourles you can hold after the evening? Input The first line of the input contains three integers $$$n, m, r$$$ ($$$1 leq n leq 30$$$, $$$1 leq m leq 30$$$, $$$1 leq r leq 1000$$$) — the number of ways to buy the shares on the market, the number of ways to sell the shares on the market, and the number of bourles you hold now. The next line contains $$$n$$$ integers $$$s_1, s_2, dots, s_n$$$ ($$$1 leq s_i leq 1000$$$); $$$s_i$$$ indicates the opportunity to buy shares at the price of $$$s_i$$$ bourles. The following line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 leq b_i leq 1000$$$); $$$b_i$$$ indicates the opportunity to sell shares at the price of $$$b_i$$$ bourles. Output Output a single integer — the maximum number of bourles you can hold after the evening. Examples Input 3 4 11 4 2 5 4 4 5 4 Note In the first example test, you have $$$11$$$ bourles in the morning. It's optimal to buy $$$5$$$ shares of a stock at the price of $$$2$$$ bourles in the morning, and then to sell all of them at the price of $$$5$$$ bourles in the evening. It's easy to verify that you'll have $$$26$$$ bourles after the evening. In the second example test, it's optimal not to take any action.
800
false
true
true
false
false
false
false
false
false
false
4,964
1823E
Alice and Bob are playing a game on a graph. They have an undirected graph without self-loops and multiple edges. All vertices of the graph have degree equal to $$$2$$$. The graph may consist of several components. Note that if such graph has $$$n$$$ vertices, it will have exactly $$$n$$$ edges. Alice and Bob take turn. Alice goes first. In each turn, the player can choose $$$k$$$ ($$$l le k le r$$$; $$$l < r$$$) vertices that form a connected subgraph and erase these vertices from the graph, including all incident edges. The player who can't make a step loses. For example, suppose they are playing on the given graph with given $$$l = 2$$$ and $$$r = 3$$$: A valid vertex set for Alice to choose at the first move is one of the following: $$${1, 2}$$$ $$${1, 3}$$$ $$${2, 3}$$$ $$${4, 5}$$$ $$${4, 6}$$$ $$${5, 6}$$$ $$${1, 2, 3}$$$ $$${4, 5, 6}$$$ Suppose, Alice chooses subgraph $$${4, 6}$$$. Then a valid vertex set for Bob to choose at the first move is one of the following: $$${1, 2}$$$ $$${1, 3}$$$ $$${2, 3}$$$ $$${1, 2, 3}$$$ Suppose, Bob chooses subgraph $$${1, 2, 3}$$$. Alice can't make a move, so she loses. You are given a graph of size $$$n$$$ and integers $$$l$$$ and $$$r$$$. Who will win if both Alice and Bob play optimally. Input The first line contains three integers $$$n$$$, $$$l$$$ and $$$r$$$ ($$$3 le n le 2 cdot 10^5$$$; $$$1 le l < r le n$$$)xa0— the number of vertices in the graph, and the constraints on the number of vertices Alice or Bob can choose in one move. Next $$$n$$$ lines contains edges of the graph: one edge per line. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i eq v_i$$$)xa0— description of the $$$i$$$-th edge. It's guaranteed that the degree of each vertex of the given graph is equal to $$$2$$$. Note In the first test the same input as in legend is shown. In the second test the same graph as in legend is shown, but with $$$l = 1$$$ and $$$r = 2$$$.
2,500
true
false
false
true
false
false
true
false
false
true
1,339
926E
You are given a sequence of positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_. While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to _x_, delete both and insert a single integer _x_u2009+u20091 on their place. This way the number of elements in the sequence is decreased by 1 on each step. You stop performing the operation when there is no pair of equal consecutive elements. For example, if the initial sequence is [5,u20092,u20091,u20091,u20092,u20092], then after the first operation you get [5,u20092,u20092,u20092,u20092], after the second — [5,u20093,u20092,u20092], after the third — [5,u20093,u20093], and finally after the fourth you get [5,u20094]. After that there are no equal consecutive elements left in the sequence, so you stop the process. Determine the final sequence after you stop performing the operation. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u20092·105) — the number of elements in the sequence. The second line contains the sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output In the first line print a single integer _k_ — the number of elements in the sequence after you stop performing the operation. In the second line print _k_ integersxa0— the sequence after you stop performing the operation. Note The first example is described in the statements. In the second example the initial sequence is [1000000000,u20091000000000,u20091000000000,u20091000000000]. After the first operation the sequence is equal to [1000000001,u20091000000000,u20091000000000]. After the second operation the sequence is [1000000001,u20091000000001]. After the third operation the sequence is [1000000002]. In the third example there are no two equal consecutive elements initially, so the sequence does not change.
1,900
false
false
false
false
true
true
false
false
false
false
6,001
1237G
There are $$$n$$$ friends living on a circular street. The friends and their houses are numbered clockwise from $$$0$$$ to $$$n-1$$$. Initially person $$$i$$$ has $$$a_i$$$ stones. The friends want to make the distribution of stones among them perfectly balanced: everyone should possess the same number of stones. The only way to change the distribution of stones is by conducting meetings. During a meeting, people from exactly $$$k$$$ consecutive houses (remember that the street is circular) gather at the same place and bring all their stones with them. All brought stones may be redistributed among people attending the meeting arbitrarily. The total number of stones they possess before the meeting and after the meeting must stay the same. After the meeting, everyone returns to their home. Find a way to make the distribution of stones perfectly balanced conducting as few meetings as possible. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k < n le 10^5$$$), denoting the number of friends and the size of each meeting. The second line contains $$$n$$$ integers $$$a_0, a_1, ldots, a_{n-1}$$$ ($$$0 le a_i le 10^4$$$), denoting the number of stones people initially have. The sum of all $$$a_i$$$ is divisible by $$$n$$$. Output Output the minimum number of meetings $$$m$$$ ($$$m ge 0$$$), followed by $$$m$$$ descriptions of meetings in chronological order. The $$$i$$$-th description must consist of an integer $$$s_i$$$ ($$$0 le s_i < n$$$), followed by $$$k$$$ non-negative integers $$$b_{i, 0}, b_{i, 1}, ldots, b_{i, k-1}$$$ ($$$b_{i, j} ge 0$$$). Such a description denotes a meeting of people $$$s_i, (s_i + 1) bmod n, ldots, (s_i + k - 1) bmod n$$$, and $$$b_{i,j}$$$ denotes the number of stones person $$$(s_i + j) bmod n$$$ must have after the $$$i$$$-th meeting. The sum of $$$b_{i, j}$$$ must match the total number of stones owned by these people before the $$$i$$$-th meeting. We can show that a solution exists for any valid input, and any correct output contains at most $$$10^7$$$ non-whitespace characters. Examples Output 3 2 7 3 4 5 4 4 2 1 4 4 4 Input 11 4 1 0 1 0 0 4 4 2 4 3 3 Output 3 3 2 2 2 2 8 2 2 2 5 10 2 2 2 2 Note In the first example, the distribution of stones changes as follows: after the first meeting: $$$2$$$ $$$6$$$ $$$mathbf{7}$$$ $$$mathbf{3}$$$ $$$mathbf{4}$$$ $$$2$$$; after the second meeting: $$$mathbf{4}$$$ $$$mathbf{2}$$$ $$$7$$$ $$$3$$$ $$$4$$$ $$$mathbf{4}$$$; after the third meeting: $$$4$$$ $$$mathbf{4}$$$ $$$mathbf{4}$$$ $$$mathbf{4}$$$ $$$4$$$ $$$4$$$. In the second example, the distribution of stones changes as follows: after the first meeting: $$$1$$$ $$$0$$$ $$$1$$$ $$$mathbf{2}$$$ $$$mathbf{2}$$$ $$$mathbf{2}$$$ $$$mathbf{2}$$$ $$$2$$$ $$$4$$$ $$$3$$$ $$$3$$$; after the second meeting: $$$mathbf{5}$$$ $$$0$$$ $$$1$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$mathbf{2}$$$ $$$mathbf{2}$$$ $$$mathbf{2}$$$; after the third meeting: $$$mathbf{2}$$$ $$$mathbf{2}$$$ $$$mathbf{2}$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$mathbf{2}$$$.
3,500
false
true
false
true
true
false
false
false
false
false
4,505
1153A
It is raining heavily. But this is the first day for Serval, who just became 3 years old, to go to the kindergarten. Unfortunately, he lives far from kindergarten, and his father is too busy to drive him there. The only choice for this poor little boy is to wait for a bus on this rainy day. Under such circumstances, the poor boy will use the first bus he sees no matter where it goes. If several buses come at the same time, he will choose one randomly. Serval will go to the bus station at time $$$t$$$, and there are $$$n$$$ bus routes which stop at this station. For the $$$i$$$-th bus route, the first bus arrives at time $$$s_i$$$ minutes, and each bus of this route comes $$$d_i$$$ minutes later than the previous one. As Serval's best friend, you wonder which bus route will he get on. If several buses arrive at the same time, you can print any of them. Input The first line contains two space-separated integers $$$n$$$ and $$$t$$$ ($$$1leq nleq 100$$$, $$$1leq tleq 10^5$$$)xa0— the number of bus routes and the time Serval goes to the station. Each of the next $$$n$$$ lines contains two space-separated integers $$$s_i$$$ and $$$d_i$$$ ($$$1leq s_i,d_ileq 10^5$$$)xa0— the time when the first bus of this route arrives and the interval between two buses of this route. Output Print one numberxa0— what bus route Serval will use. If there are several possible answers, you can print any of them. Examples Input 5 5 3 3 2 5 5 6 4 9 6 1 Note In the first example, the first bus of the first route arrives at time $$$6$$$, and the first bus of the second route arrives at time $$$9$$$, so the first route is the answer. In the second example, a bus of the third route arrives at time $$$5$$$, so it is the answer. In the third example, buses of the first route come at times $$$2$$$, $$$4$$$, $$$6$$$, $$$8$$$, and so fourth, buses of the second route come at times $$$2$$$, $$$5$$$, $$$8$$$, and so fourth and buses of the third route come at times $$$2$$$, $$$6$$$, $$$10$$$, and so on, so $$$1$$$ and $$$2$$$ are both acceptable answers while $$$3$$$ is not.
1,000
true
false
false
false
false
false
true
false
false
false
4,949
348D
You've got a table of size _n_u2009×u2009_m_. We'll consider the table rows numbered from top to bottom 1 through _n_, and the columns numbered from left to right 1 through _m_. Then we'll denote the cell in row _x_ and column _y_ as (_x_,u2009_y_). Initially cell (1,u20091) contains two similar turtles. Both turtles want to get to cell (_n_,u2009_m_). Some cells of the table have obstacles but it is guaranteed that there aren't any obstacles in the upper left and lower right corner. A turtle (one or the other) can go from cell (_x_,u2009_y_) to one of two cells (_x_u2009+u20091,u2009_y_) and (_x_,u2009_y_u2009+u20091), as long as the required cell doesn't contain an obstacle. The turtles have had an argument so they don't want to have any chance of meeting each other along the way. Help them find the number of ways in which they can go from cell (1,u20091) to cell (_n_,u2009_m_). More formally, find the number of pairs of non-intersecting ways from cell (1,u20091) to cell (_n_,u2009_m_) modulo 1000000007 (109u2009+u20097). Two ways are called non-intersecting if they have exactly two common points — the starting point and the final point. Input The first line contains two integers _n_,u2009_m_ (2u2009≤u2009_n_,u2009_m_u2009≤u20093000). Each of the following _n_ lines contains _m_ characters describing the table. The empty cells are marked by characters ".", the cells with obstacles are marked by "#". It is guaranteed that the upper left and the lower right cells are empty. Output In a single line print a single integer — the number of pairs of non-intersecting paths from cell (1,u20091) to cell (_n_,u2009_m_) modulo 1000000007 (109u2009+u20097). Examples Input 4 5 ..... .###. .###. .....
2,500
false
false
false
true
false
false
false
false
false
false
8,439
727A
Vasily has a number _a_, which he wants to turn into a number _b_. For this purpose, he can do two types of operations: multiply the current number by 2 (that is, replace the number _x_ by 2·_x_); append the digit 1 to the right of current number (that is, replace the number _x_ by 10·_x_u2009+u20091). You need to help Vasily to transform the number _a_ into the number _b_ using only the operations described above, or find that it is impossible. Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform _a_ into _b_. Input The first line contains two positive integers _a_ and _b_ (1u2009≤u2009_a_u2009<u2009_b_u2009≤u2009109)xa0— the number which Vasily has and the number he wants to have. Output If there is no way to get _b_ from _a_, print "NO" (without quotes). Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer _k_xa0— the length of the transformation sequence. On the third line print the sequence of transformations _x_1,u2009_x_2,u2009...,u2009_x__k_, where: _x_1 should be equal to _a_, _x__k_ should be equal to _b_, _x__i_ should be obtained from _x__i_u2009-u20091 using any of two described operations (1u2009<u2009_i_u2009≤u2009_k_). If there are multiple answers, print any of them. Examples Output YES 5 100 200 2001 4002 40021
1,000
true
false
false
false
false
false
true
false
false
false
6,900
93A
Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well. This time Igor K. got disappointed in one of his hobbies: editing and voicing videos. Moreover, he got disappointed in it so much, that he decided to destroy his secret archive for good. Igor K. use Pindows XR operation system which represents files and folders by small icons. At that, _m_ icons can fit in a horizontal row in any window. Igor K.'s computer contains _n_ folders in the D: disk's root catalog. The folders are numbered from 1 to _n_ in the order from the left to the right and from top to bottom (see the images). At that the folders with secret videos have numbers from _a_ to _b_ inclusive. Igor K. wants to delete them forever, at that making as few frame selections as possible, and then pressing Shift+Delete exactly once. What is the minimum number of times Igor K. will have to select the folder in order to select folders from _a_ to _b_ and only them? Let us note that if some selected folder is selected repeatedly, then it is deselected. Each selection possesses the shape of some rectangle with sides parallel to the screen's borders. Input The only line contains four integers _n_, _m_, _a_, _b_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009109, 1u2009≤u2009_a_u2009≤u2009_b_u2009≤u2009_n_). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted. Output Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from _a_ to _b_. Note The images below illustrate statement tests. The first test: In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our second selection and folder 9 with our third, last selection. The second test: In this test we can first select all folders in the first row (2, 3, 4, 5), then — all other ones.
1,700
false
false
true
false
false
false
false
false
false
false
9,515
387E
George is a cat, so he loves playing very much. Vitaly put _n_ cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers from 1 to _n_. Then the _i_-th card from the left contains number _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_). Vitaly wants the row to have exactly _k_ cards left. He also wants the _i_-th card from left to have number _b__i_ written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation _n_u2009-u2009_k_ times. In one remove operation George can choose _w_ (1u2009≤u2009_w_; _w_ is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as _x_1,u2009_x_2,u2009...,u2009_x__w_ (from the left to the right). After that, George can remove the card _x__i_, such that _x__i_u2009≤u2009_x__j_ for each _j_ (1u2009≤u2009_j_u2009≤u2009_w_). After the described operation George gets _w_ pieces of sausage. George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question! Input The first line contains integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009106) — the initial and the final number of cards. The second line contains _n_ distinct space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_) — the initial row of cards. The third line contains _k_ space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__k_ — the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for _n_u2009-u2009_k_ times. Output Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well. Examples Input 10 5 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10
2,200
false
false
false
false
true
false
false
true
false
false
8,278
421A
Pasha has two hamsters: Arthur and Alexander. Pasha put _n_ apples in front of them. Pasha knows which apples Arthur likes. Similarly, Pasha knows which apples Alexander likes. Pasha doesn't want any conflict between the hamsters (as they may like the same apple), so he decided to distribute the apples between the hamsters on his own. He is going to give some apples to Arthur and some apples to Alexander. It doesn't matter how many apples each hamster gets but it is important that each hamster gets only the apples he likes. It is possible that somebody doesn't get any apples. Help Pasha distribute all the apples between the hamsters. Note that Pasha wants to distribute all the apples, not just some of them. Input The first line contains integers _n_, _a_, _b_ (1u2009≤u2009_n_u2009≤u2009100;xa01u2009≤u2009_a_,u2009_b_u2009≤u2009_n_) — the number of apples Pasha has, the number of apples Arthur likes and the number of apples Alexander likes, correspondingly. The next line contains _a_ distinct integers — the numbers of the apples Arthur likes. The next line contains _b_ distinct integers — the numbers of the apples Alexander likes. Assume that the apples are numbered from 1 to _n_. The input is such that the answer exists. Output Print _n_ characters, each of them equals either 1 or 2. If the _i_-h character equals 1, then the _i_-th apple should be given to Arthur, otherwise it should be given to Alexander. If there are multiple correct answers, you are allowed to print any of them.
800
false
false
true
false
false
true
false
false
false
false
8,145
1335D
You are given a correct solution of the sudoku puzzle. If you don't know what is the sudoku, you can read about it $$$ to any other number in range $$$[1; 9]$$$) to make it anti-sudoku. The anti-sudoku is the $$$9 imes 9$$$ field, in which: Any number in this field is in range $$$[1; 9]$$$; each row contains at least two equal elements; each column contains at least two equal elements; each $$$3 imes 3$$$ block (you can read what is the block in the link above) contains at least two equal elements. It is guaranteed that the answer exists. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of $$$9$$$ lines, each line consists of $$$9$$$ characters from $$$1$$$ to $$$9$$$ without any whitespaces — the correct solution of the sudoku puzzle. Output For each test case, print the answer — the initial field with at most $$$9$$$ changed elements so that the obtained field is anti-sudoku. If there are several solutions, you can print any. It is guaranteed that the answer exists. Example Input 1 154873296 386592714 729641835 863725149 975314628 412968357 631457982 598236471 247189563 Output 154873396 336592714 729645835 863725145 979314628 412958357 631457992 998236471 247789563
1,300
false
false
true
false
false
true
false
false
false
false
4,027
1215D
Monocarp and Bicarp live in Berland, where every bus ticket consists of $$$n$$$ digits ($$$n$$$ is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even. Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first $$$frac{n}{2}$$$ digits of this ticket is equal to the sum of the last $$$frac{n}{2}$$$ digits. Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from $$$0$$$ to $$$9$$$. The game ends when there are no erased digits in the ticket. If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally. Input The first line contains one even integer $$$n$$$ $$$(2 le n le 2 cdot 10^{5})$$$ — the number of digits in the ticket. The second line contains a string of $$$n$$$ digits and "?" characters — the ticket which Monocarp and Bicarp have found. If the $$$i$$$-th character is "?", then the $$$i$$$-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even. Output If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes). Note Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp. In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy.
1,700
true
true
false
false
false
false
false
false
false
false
4,600
39H
Problem - 39H - 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 *1300 No tag edit access → Contest materials ") : Tutorial") : Problem K Tutorial") . Help Petya make a multiplication table for numbers in the system of notations with the radix _k_. Input The first line contains a single integer _k_ (2u2009≤u2009_k_u2009≤u200910) — the radix of the system. Output Output the multiplication table for the system of notations with the radix _k_. The table must contain _k_u2009-u20091 rows and _k_u2009-u20091 columns. The element on the crossing of the _i_-th row and the _j_-th column is equal to the product of _i_ and _j_ in the system of notations with the radix _k_. Each line may have any number of spaces between the numbers (the extra spaces in the samples are put for clarity). Examples Input 10 Output 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 Input 3 Output 1 2 2 11
1,300
false
false
true
false
false
false
false
false
false
false
9,793
1584B
Problem - 1584B - Codeforces =============== xa0 ") ") have different colors. What is the minimum number of cells David will have to paint? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. The next lines contain descriptions of test cases. The only line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n, m leq 3 cdot 10^4$$$, $$$n cdot m geq 2$$$). Output For each test case print a single integer — the minimum number of cells David will have to paint blue. Example Input 4 1 3 2 2 2 5 3 5 Output 1 2 4 5 Note The following pictures show how the initial rectangle can be split and cells colored blue. In the first test case: In the second test case: In the third test case: In the fourth test case:
1,000
true
true
false
false
false
false
false
false
false
false
2,708
548B
Mike and some bears are playing a game just for fun. Mike is the judge. All bears except Mike are standing in an _n_u2009×u2009_m_ grid, there's exactly one bear in each cell. We denote the bear standing in column number _j_ of row number _i_ by (_i_,u2009_j_). Mike's hands are on his ears (since he's the judge) and each bear standing in the grid has hands either on his mouth or his eyes. They play for _q_ rounds. In each round, Mike chooses a bear (_i_,u2009_j_) and tells him to change his state i. e. if his hands are on his mouth, then he'll put his hands on his eyes or he'll put his hands on his mouth otherwise. After that, Mike wants to know the score of the bears. Score of the bears is the maximum over all rows of number of consecutive bears with hands on their eyes in that row. Since bears are lazy, Mike asked you for help. For each round, tell him the score of these bears after changing the state of a bear selected in that round. Input The first line of input contains three integers _n_, _m_ and _q_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009500 and 1u2009≤u2009_q_u2009≤u20095000). The next _n_ lines contain the grid description. There are _m_ integers separated by spaces in each line. Each of these numbers is either 0 (for mouth) or 1 (for eyes). The next _q_ lines contain the information about the rounds. Each of them contains two integers _i_ and _j_ (1u2009≤u2009_i_u2009≤u2009_n_ and 1u2009≤u2009_j_u2009≤u2009_m_), the row number and the column number of the bear changing his state. Output After each round, print the current score of the bears. Examples Input 5 4 5 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 0 0 0 1 1 1 4 1 1 4 2 4 3
1,400
false
true
true
true
false
false
true
false
false
false
7,646
26C
Problem - 26C - 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 constructive algorithms greedy implementation *2000 No tag edit access → Contest materials ") ") , _n_ and _m_ — the living room dimensions, _a_, _b_ and _c_ — amount of planks 1u2009×u20092, 2u2009×u20091 и 2u2009×u20092 respectively. It's not allowed to turn the planks. Output If it is not possible to parquet the room with such a set of planks, output IMPOSSIBLE. Otherwise output one of the possible ways to parquet the room — output _n_ lines with _m_ lower-case Latin letters each. Two squares with common sides should contain the same letters, if they belong to one and the same plank, and different letters otherwise. Different planks can be marked with one and the same letter (see examples). If the answer is not unique, output any. Examples Input 2 6 2 2 1 Output aabcca aabdda Input 1 1 100 100 100 Output IMPOSSIBLE Input 4 4 10 10 10 Output aabb aabb bbaa bbaa
2,000
false
true
true
false
false
true
false
false
false
false
9,866
1986F
You are given a connected undirected graph, the vertices of which are numbered with integers from $$$1$$$ to $$$n$$$. Your task is to minimize the number of pairs of vertices $$$1 leq u < v leq n$$$ between which there exists a path in this graph. To achieve this, you can remove exactly one edge from the graph. Find the smallest number of pairs of vertices! Input Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of sets of input data. Then follows their description. The first line of each set of input data contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10^5$$$, $$$n - 1 leq m leq min(10^5, frac{n cdot (n - 1)}{2})$$$)xa0— the number of vertices in the graph and the number of edges. Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n, u eq v$$$), indicating that there is an undirected edge in the graph between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given graph is connected and without multiple edges. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all sets of input data does not exceed $$$2 cdot 10^5$$$. Output For each set of input data, output the smallest number of pairs of reachable vertices, if exactly one edge can be removed. Note In the first set of input data, we will remove the single edge $$$(1, 2)$$$ and the only pair of vertices $$$(1, 2)$$$ will become unreachable from each other. In the second set of input data, no matter which edge we remove, all vertices will be reachable from each other. In the fourth set of input data, the graph looks like this initially. We will remove the edge $$$(3, 4)$$$ and then the only reachable pairs of vertices will be $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(4, 5)$$$, $$$(4, 6)$$$, $$$(5, 6)$$$. In the sixth set of input data, the graph looks like this initially. After removing the edge $$$(2, 4)$$$, the graph will look like this. Thus, there will be $$$21$$$ pairs of reachable vertices.
1,900
false
false
false
false
false
false
false
false
false
true
360
1661A
You are given two arrays of length $$$n$$$: $$$a_1, a_2, dots, a_n$$$ and $$$b_1, b_2, dots, b_n$$$. You can perform the following operation any number of times: 1. Choose integer index $$$i$$$ ($$$1 le i le n$$$); 2. Swap $$$a_i$$$ and $$$b_i$$$. What is the minimum possible sum $$$a_1 - a_2 + a_2 - a_3 + dots + a_{n-1} - a_n$$$ $$$+$$$ $$$b_1 - b_2 + b_2 - b_3 + dots + b_{n-1} - b_n$$$ (in other words, $$$sumlimits_{i=1}^{n - 1}{left(a_i - a_{i+1} + b_i - b_{i+1} ight)}$$$) you can achieve after performing several (possibly, zero) operations? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 4000$$$)xa0— the number of test cases. Then, $$$t$$$ test cases follow. The first line of each test case contains the single integer $$$n$$$ ($$$2 le n le 25$$$)xa0— the length of arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the array $$$b$$$. Output For each test case, print one integerxa0— the minimum possible sum $$$sumlimits_{i=1}^{n-1}{left(a_i - a_{i+1} + b_i - b_{i+1} ight)}$$$. Example Input 3 4 3 3 10 10 10 10 3 3 5 1 2 3 4 5 6 7 8 9 10 6 72 101 108 108 111 44 10 87 111 114 108 100 Note In the first test case, we can, for example, swap $$$a_3$$$ with $$$b_3$$$ and $$$a_4$$$ with $$$b_4$$$. We'll get arrays $$$a = [3, 3, 3, 3]$$$ and $$$b = [10, 10, 10, 10]$$$ with sum $$$3 cdot 3 - 3 + 3 cdot 10 - 10 = 0$$$. In the second test case, arrays already have minimum sum (described above) equal to $$$1 - 2 + dots + 4 - 5 + 6 - 7 + dots + 9 - 10$$$ $$$= 4 + 4 = 8$$$. In the third test case, we can, for example, swap $$$a_5$$$ and $$$b_5$$$.
800
true
true
false
false
false
false
false
false
false
false
2,319
1041E
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $$$1$$$ to $$$n$$$. For every edge $$$e$$$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $$$e$$$ (and only this edge) is erased from the tree. Monocarp has given you a list of $$$n - 1$$$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so. Input The first line contains one integer $$$n$$$ ($$$2 le n le 1,000$$$)xa0— the number of vertices in the tree. Each of the next $$$n-1$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ each ($$$1 le a_i < b_i le n$$$)xa0— the maximal indices of vertices in the components formed if the $$$i$$$-th edge is removed. Output If there is no such tree that can produce the given list of pairs, print "NO" (without quotes). Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $$$n - 1$$$ lines. Each of the last $$$n - 1$$$ lines should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$)xa0— vertices connected by an edge. Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order. Note Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs.
1,900
false
true
false
false
true
true
false
false
false
true
5,491
518C
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly _n_ applications, each application has its own icon. The icons are located on different screens, one screen contains _k_ icons. The icons from the first to the _k_-th one are located on the first screen, from the (_k_u2009+u20091)-th to the 2_k_-th ones are on the second screen and so on (the last screen may be partially empty). Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen _t_, Anya needs to make the following gestures: first she scrolls to the required screen number _t_, by making _t_u2009-u20091 gestures (if the icon is on the screen _t_), and then make another gesture — press the icon of the required application exactly once to launch it. After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1. All applications are numbered from 1 to _n_. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change. Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order? Note that one application may be launched multiple times. Input The first line of the input contains three numbers _n_,u2009_m_,u2009_k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u2009105)xa0—xa0the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen. The next line contains _n_ integers, permutation _a_1,u2009_a_2,u2009...,u2009_a__n_xa0—xa0the initial order of icons from left to right in the menu (from the first to the last one), _a__i_xa0—xa0 is the id of the application, whose icon goes _i_-th in the menu. Each integer from 1 to _n_ occurs exactly once among _a__i_. The third line contains _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_(1u2009≤u2009_b__i_u2009≤u2009_n_)xa0—xa0the ids of the launched applications in the planned order. One application may be launched multiple times. Output Print a single number — the number of gestures that Anya needs to make to launch all the applications in the desired order. Examples Input 8 3 3 1 2 3 4 5 6 7 8 7 8 1 Input 5 4 2 3 1 5 2 4 4 4 4 4 Note In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1,u20092,u20093, the second screen contains icons 4,u20095,u20096, the third screen contains icons 7,u20098. After application 7 is launched, we get the new arrangement of the iconsxa0—xa0(123)(457)(68). To launch it Anya makes 3 gestures. After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures. After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture. In total, Anya makes 7 gestures.
1,600
false
false
true
false
true
true
false
false
false
false
7,766
754D
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket. The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has _n_ discount coupons, the _i_-th of them can be used with products with ids ranging from _l__i_ to _r__i_, inclusive. Today Fedor wants to take exactly _k_ coupons with him. Fedor wants to choose the _k_ coupons in such a way that the number of such products _x_ that all coupons can be used with this product _x_ is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor! Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20093·105)xa0— the number of coupons Fedor has, and the number of coupons he wants to choose. Each of the next _n_ lines contains two integers _l__i_ and _r__i_ (u2009-u2009109u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009109)xa0— the description of the _i_-th coupon. The coupons can be equal. Output In the first line print single integerxa0— the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted. In the second line print _k_ distinct integers _p_1,u2009_p_2,u2009...,u2009_p__k_ (1u2009≤u2009_p__i_u2009≤u2009_n_)xa0— the ids of the coupons which Fedor should choose. If there are multiple answers, print any of them. Examples Input 4 2 1 100 40 70 120 130 125 180 Input 5 2 1 10 5 15 14 50 30 70 99 100 Note In the first example if we take the first two coupons then all the products with ids in range [40,u200970] can be bought with both coupons. There are 31 products in total. In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
2,100
false
true
false
false
true
false
false
true
true
false
6,779
1207C
You are responsible for installing a gas pipeline along a road. Let's consider the road (for simplicity) as a segment $$$[0, n]$$$ on $$$OX$$$ axis. The road can have several crossroads, but for simplicity, we'll denote each crossroad as an interval $$$(x, x + 1)$$$ with integer $$$x$$$. So we can represent the road as a binary string consisting of $$$n$$$ characters, where character 0 means that current interval doesn't contain a crossroad, and 1 means that there is a crossroad. Usually, we can install the pipeline along the road on height of $$$1$$$ unit with supporting pillars in each integer point (so, if we are responsible for $$$[0, n]$$$ road, we must install $$$n + 1$$$ pillars). But on crossroads we should lift the pipeline up to the height $$$2$$$, so the pipeline won't obstruct the way for cars. We can do so inserting several zig-zag-like lines. Each zig-zag can be represented as a segment $$$[x, x + 1]$$$ with integer $$$x$$$ consisting of three parts: $$$0.5$$$ units of horizontal pipe + $$$1$$$ unit of vertical pipe + $$$0.5$$$ of horizontal. Note that if pipeline is currently on height $$$2$$$, the pillars that support it should also have length equal to $$$2$$$ units. Each unit of gas pipeline costs us $$$a$$$ bourles, and each unit of pillar — $$$b$$$ bourles. So, it's not always optimal to make the whole pipeline on the height $$$2$$$. Find the shape of the pipeline with minimum possible cost and calculate that cost. Note that you must start and finish the pipeline on height $$$1$$$ and, also, it's guaranteed that the first and last characters of the input string are equal to 0. Input The fist line contains one integer $$$T$$$ ($$$1 le T le 100$$$) — the number of queries. Next $$$2 cdot T$$$ lines contain independent queries — one query per two lines. The first line contains three integers $$$n$$$, $$$a$$$, $$$b$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le a le 10^8$$$, $$$1 le b le 10^8$$$) — the length of the road, the cost of one unit of the pipeline and the cost of one unit of the pillar, respectively. The second line contains binary string $$$s$$$ ($$$s = n$$$, $$$s_i in {0, 1}$$$, $$$s_1 = s_n = 0$$$) — the description of the road. It's guaranteed that the total length of all strings $$$s$$$ doesn't exceed $$$2 cdot 10^5$$$. Note The optimal pipeline for the first query is shown at the picture above. The optimal pipeline for the second query is pictured below: The optimal (and the only possible) pipeline for the third query is shown below: The optimal pipeline for the fourth query is shown below:
1,500
false
true
false
true
false
false
false
false
false
false
4,659
64D
Problem - 64D - Codeforces =============== xa0 is for the second sister. And the less valuable present is for the youngest sister. If two (or three) presents have the same price, corresponding sisters may get the in a random way. Input The only line contains three integer numbers _a_1,u2009_a_2,u2009_a_3 (1u2009≤u2009_a_1,u2009_a_2,u2009_a_3u2009≤u2009100) — the prices of the presents. Output Print three numbers _i_1,u2009_i_2,u2009_i_3 (1u2009≤u2009_i_1,u2009_i_2,u2009_i_3u2009≤u20093), all of them should be distinct. The first number stands for the seniority of the sister which will get the first present (1 stands for the eldest, 3 for the youngest). The second and third numbers mean the seniority of the sisters which get the second and the third present respectively. If there are multiple answers, print any of them. Examples Input 11 13 1 Output 2 1 3 Input 30 10 30 Output 1 3 2 Note In the second sample another possible answer is "2 3 1".
1,800
false
true
false
false
false
false
false
false
false
false
9,652
1895C
Problem - 1895C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force dp hashing implementation math *1400 No tag edit access → Contest materials $$$ (for $$$1 le i, j le n$$$) are there such that $$$s_i + s_j$$$ is a lucky ticket? Note that it's possible that $$$i=j$$$. Here, the + operator denotes the concatenation of the two strings. For example, if $$$s_i$$$ is 13, and $$$s_j$$$ is 37, then $$$s_i + s_j$$$ is 1337. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of ticket pieces. The second line contains $$$n$$$ non-empty strings $$$s_1, s_2, dots, s_n$$$, each of length at most $$$5$$$ and consisting only of digits from $$$1$$$ to $$$9$$$. Output Print a single integerxa0— the number of pairs $$$(i, j)$$$ (for $$$1 le i, j le n$$$) such that $$$s_i + s_j$$$ is a lucky ticket. Examples Input 10 5 93746 59 3746 593 746 5937 46 59374 6 Output 20 Input 5 2 22 222 2222 22222 Output 13 Input 3 1 1 1 Output 9
1,400
true
false
true
true
false
false
true
false
false
false
942
1167C
In some social network, there are $$$n$$$ users communicating with each other in $$$m$$$ groups of friends. Let's analyze the process of distributing some news between users. Initially, some user $$$x$$$ receives the news from some source. Then he or she sends the news to his or her friends (two users are friends if there is at least one group such that both of them belong to this group). Friends continue sending the news to their friends, and so on. The process ends when there is no pair of friends such that one of them knows the news, and another one doesn't know. For each user $$$x$$$ you have to determine what is the number of users that will know the news if initially only user $$$x$$$ starts distributing it. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 5 cdot 10^5$$$) — the number of users and the number of groups of friends, respectively. Then $$$m$$$ lines follow, each describing a group of friends. The $$$i$$$-th line begins with integer $$$k_i$$$ ($$$0 le k_i le n$$$) — the number of users in the $$$i$$$-th group. Then $$$k_i$$$ distinct integers follow, denoting the users belonging to the $$$i$$$-th group. It is guaranteed that $$$sum limits_{i = 1}^{m} k_i le 5 cdot 10^5$$$. Output Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the number of users that will know the news if user $$$i$$$ starts distributing it. Example Input 7 5 3 2 5 4 0 2 1 2 1 1 2 6 7
1,400
false
false
false
false
false
false
false
false
false
true
4,881
348E
A long time ago there was a land called Dudeland. Dudeland consisted of _n_ towns connected with _n_u2009-u20091 bidirectonal roads. The towns are indexed from 1 to _n_ and one can reach any city from any other city if he moves along the roads of the country. There are _m_ monasteries in Dudeland located in _m_ different towns. In each monastery lives a pilgrim. At the beginning of the year, each pilgrim writes down which monastery is the farthest from the monastery he is living in. If there is more than one farthest monastery, he lists all of them. On the Big Lebowski day each pilgrim picks one town from his paper at random and starts walking to that town. Walter hates pilgrims and wants to make as many of them unhappy as possible by preventing them from finishing their journey. He plans to destroy exactly one town that does not contain a monastery. A pilgrim becomes unhappy if all monasteries in his list become unreachable from the monastery he is living in. You need to find the maximum number of pilgrims Walter can make unhappy. Also find the number of ways he can make this maximal number of pilgrims unhappy: the number of possible towns he can destroy. Input The first line contains two integers _n_ (3u2009≤u2009_n_u2009≤u2009105) and _m_ (2u2009≤u2009_m_u2009<u2009_n_). The next line contains _m_ distinct integers representing indices of towns that contain monasteries. Next _n_u2009-u20091 lines contain three integers each, _a__i_, _b__i_, _c__i_, indicating that there is an edge between towns _a__i_ and _b__i_ of length _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u20091u2009≤u2009_c__i_u2009≤u20091000,u2009_a__i_u2009≠u2009_b__i_). Output Output two integers: the maximum number of pilgrims Walter can make unhappy and the number of ways in which he can make his plan come true. Examples Input 8 5 7 2 5 4 8 1 2 1 2 3 2 1 4 1 4 5 2 1 6 1 6 7 8 6 8 10
2,800
false
false
false
true
false
false
false
false
false
false
8,438
1075B
Palo Alto is an unusual city because it is an endless coordinate line. It is also known for the office of Lyft Level 5. Lyft has become so popular so that it is now used by all $$$m$$$ taxi drivers in the city, who every day transport the rest of the city residentsxa0— $$$n$$$ riders. Each resident (including taxi drivers) of Palo-Alto lives in its unique location (there is no such pair of residents that their coordinates are the same). The Lyft system is very clever: when a rider calls a taxi, his call does not go to all taxi drivers, but only to the one that is the closest to that person. If there are multiple ones with the same distance, then to taxi driver with a smaller coordinate is selected. But one morning the taxi drivers wondered: how many riders are there that would call the given taxi driver if they were the first to order a taxi on that day? In other words, you need to find for each taxi driver $$$i$$$ the number $$$a_{i}$$$xa0— the number of riders that would call the $$$i$$$-th taxi driver when all drivers and riders are at their home? The taxi driver can neither transport himself nor other taxi drivers. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n,m le 10^5$$$)xa0— number of riders and taxi drivers. The second line contains $$$n + m$$$ integers $$$x_1, x_2, ldots, x_{n+m}$$$ ($$$1 le x_1 < x_2 < ldots < x_{n+m} le 10^9$$$), where $$$x_i$$$ is the coordinate where the $$$i$$$-th resident lives. The third line contains $$$n + m$$$ integers $$$t_1, t_2, ldots, t_{n+m}$$$ ($$$0 le t_i le 1$$$). If $$$t_i = 1$$$, then the $$$i$$$-th resident is a taxi driver, otherwise $$$t_i = 0$$$. It is guaranteed that the number of $$$i$$$ such that $$$t_i = 1$$$ is equal to $$$m$$$. Output Print $$$m$$$ integers $$$a_1, a_2, ldots, a_{m}$$$, where $$$a_i$$$ is the answer for the $$$i$$$-th taxi driver. The taxi driver has the number $$$i$$$ if among all the taxi drivers he lives in the $$$i$$$-th smallest coordinate (see examples for better understanding). Examples Input 3 2 2 3 4 5 6 1 0 0 0 1 Input 1 4 2 4 6 10 15 1 1 1 1 0 Note In the first example, we have only one taxi driver, which means an order from any of $$$n$$$ riders will go to him. In the second example, the first taxi driver lives at the point with the coordinate $$$2$$$, and the second one lives at the point with the coordinate $$$6$$$. Obviously, the nearest taxi driver to the rider who lives on the $$$3$$$ coordinate is the first one, and to the rider who lives on the coordinate $$$5$$$ is the second one. The rider who lives on the $$$4$$$ coordinate has the same distance to the first and the second taxi drivers, but since the first taxi driver has a smaller coordinate, the call from this rider will go to the first taxi driver. In the third example, we have one rider and the taxi driver nearest to him is the fourth one.
1,200
false
false
true
false
false
false
false
false
true
false
5,345
1250H
You have a set of birthday cake candles. Each of such candles represents a digit between $$$0$$$ and $$$9$$$, inclusive. Example of birthday cake candles. Let's denote the candle representing the digit $$$d$$$ as $$$d$$$-candle. Your set contains $$$c_0$$$ instances of $$$0$$$-candles, $$$c_1$$$ instances of $$$1$$$-candles and so on. So, the total number of candles is $$$c_0+c_1+dots+c_9$$$. These digits are needed to wish your cat a happy birthday. For each birthday, starting with the first, you want to compose the age of the cat using the digits from the set. Since you light candles for a very short time, candles don't have time to burn out. For this reason you can reuse candles an arbitrary number of times (therefore your set of candles never changes). For example, if you have one instance of each digit (i.e. $$$c_0=c_1=dots=c_9=1$$$), you can compose any number from $$$1$$$ to $$$10$$$ using this set, but you cannot compose $$$11$$$. You have to determine the first birthday, on which you cannot compose the age of the cat using the candles from your set. In other words, find the minimum number $$$y$$$ such that all numbers from $$$1$$$ to $$$y-1$$$ can be composed by digits from your set, but $$$y$$$ cannot be composed. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. The only line of each test case contains ten integer numbers $$$c_0, c_1, dots, c_9$$$ ($$$0 le c_i le 10^5$$$) — the number of $$$0$$$-candles, $$$1$$$-candles, $$$2$$$-candles and so on. It is guaranteed that the sum of all $$$c_i$$$ in the input does not exceed $$$10^6$$$. Output For each test case, output one integer in single line — the minimum age which cannot be composed by candles from your set. Please note that the age can be quite large (it may exceed the standard 64-bit integer types in your programming language).
1,500
true
false
false
false
false
false
false
false
false
false
4,448
2029D
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Refact.ai Match 1 (Codeforces Round 985) 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 data structures dfs and similar dsu graphs greedy trees *1900 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. Cool Graph time limit per test3 seconds memory limit per test512 megabytes You are given an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. You can perform the following operation at most $$$2cdot max(n,m)$$$ times: Choose three distinct vertices $$$a$$$, $$$b$$$, and $$$c$$$, then for each of the edges $$$(a,b)$$$, $$$(b,c)$$$, and $$$(c,a)$$$, do the following: If the edge does not exist, add it. On the contrary, if it exists, remove it. A graph is called cool if and only if one of the following holds: The graph has no edges, or The graph is a tree. You have to make the graph cool by performing the above operations. Note that you can use at most $$$2cdot max(n,m)$$$ operations. It can be shown that there always exists at least one solution. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3le nle 10^5$$$, $$$0le mle minleft(frac{n(n-1)}{2},2cdot 10^5 ight)$$$)xa0— the number of vertices and the number of edges. Then $$$m$$$ lines follow, the $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1le u_i,v_ile n$$$)xa0— the two nodes that the $$$i$$$-th edge connects. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$2cdot 10^5$$$. It is guaranteed that there are no self-loops or multiple-edges in the given graph. Output For each test case, in the first line output an integer $$$k$$$ ($$$0le kle 2cdot max(n, m)$$$)xa0— the number of operations. Then output $$$k$$$ lines, the $$$i$$$-th line containing three distinct integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1le a,b,cle n$$$)xa0— the three integers you choose in the $$$i$$$-th operation. If there are multiple solutions, you can output any of them. Example input 5 3 0 3 1 1 2 3 2 1 2 2 3 3 3 1 2 2 3 3 1 6 6 1 2 1 6 4 5 3 4 4 6 3 6 output 0 1 1 2 3 0 1 1 2 3 3 1 3 6 2 4 5 3 4 6 Note In the first test case, the graph is already cool because there are no edges. In the second test case, after performing the only operation, the graph becomes a tree, so it is cool. In the third test case, the graph is already cool because it is a tree. In the fourth test case, after performing the only operation, the graph has no edges, so it is cool. In the fifth test case: Operation Graph before the operation Graph after the operation $$$1$$$ $$$2$$$ $$$3$$$ Note that after the first operation, the graph has already become cool, and there are two extra operations. As the graph is still cool after the two extra operations, this is a valid answer. Codeforces (c)
1,900
false
true
false
false
true
true
false
false
false
true
94
729C
Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in _t_ minutes. There is a straight road of length _s_ from the service to the cinema. Let's introduce a coordinate system so that the car rental service is at the point 0, and the cinema is at the point _s_. There are _k_ gas stations along the road, and at each of them you can fill a car with any amount of fuel for free! Consider that this operation doesn't take any time, i.e. is carried out instantly. There are _n_ cars in the rental service, _i_-th of them is characterized with two integers _c__i_ and _v__i_xa0— the price of this car rent and the capacity of its fuel tank in liters. It's not allowed to fuel a car with more fuel than its tank capacity _v__i_. All cars are completely fueled at the car rental service. Each of the cars can be driven in one of two speed modes: normal or accelerated. In the normal mode a car covers 1 kilometer in 2 minutes, and consumes 1 liter of fuel. In the accelerated mode a car covers 1 kilometer in 1 minutes, but consumes 2 liters of fuel. The driving mode can be changed at any moment and any number of times. Your task is to choose a car with minimum price such that Vasya can reach the cinema before the show starts, i.e. not later than in _t_ minutes. Assume that all cars are completely fueled initially. Input The first line contains four positive integers _n_, _k_, _s_ and _t_ (1u2009≤u2009_n_u2009≤u20092·105, 1u2009≤u2009_k_u2009≤u20092·105, 2u2009≤u2009_s_u2009≤u2009109, 1u2009≤u2009_t_u2009≤u20092·109)xa0— the number of cars at the car rental service, the number of gas stations along the road, the length of the road and the time in which the film starts. Each of the next _n_ lines contains two positive integers _c__i_ and _v__i_ (1u2009≤u2009_c__i_,u2009_v__i_u2009≤u2009109)xa0— the price of the _i_-th car and its fuel tank capacity. The next line contains _k_ distinct integers _g_1,u2009_g_2,u2009...,u2009_g__k_ (1u2009≤u2009_g__i_u2009≤u2009_s_u2009-u20091)xa0— the positions of the gas stations on the road in arbitrary order. Output Print the minimum rent price of an appropriate car, i.e. such car that Vasya will be able to reach the cinema before the film starts (not later than in _t_ minutes). If there is no appropriate car, print -1. Examples Input 3 1 8 10 10 8 5 7 11 9 3 Input 2 2 10 18 10 4 20 6 5 3 Note In the first sample, Vasya can reach the cinema in time using the first or the third cars, but it would be cheaper to choose the first one. Its price is equal to 10, and the capacity of its fuel tank is 8. Then Vasya can drive to the first gas station in the accelerated mode in 3 minutes, spending 6 liters of fuel. After that he can full the tank and cover 2 kilometers in the normal mode in 4 minutes, spending 2 liters of fuel. Finally, he drives in the accelerated mode covering the remaining 3 kilometers in 3 minutes and spending 6 liters of fuel.
1,700
false
true
false
false
false
false
false
true
true
false
6,892
1000C
You are given $$$n$$$ segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide. Your task is the following: for every $$$k in [1..n]$$$, calculate the number of points with integer coordinates such that the number of segments that cover these points equals $$$k$$$. A segment with endpoints $$$l_i$$$ and $$$r_i$$$ covers point $$$x$$$ if and only if $$$l_i le x le r_i$$$. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of segments. The next $$$n$$$ lines contain segments. The $$$i$$$-th line contains a pair of integers $$$l_i, r_i$$$ ($$$0 le l_i le r_i le 10^{18}$$$) — the endpoints of the $$$i$$$-th segment. Output Print $$$n$$$ space separated integers $$$cnt_1, cnt_2, dots, cnt_n$$$, where $$$cnt_i$$$ is equal to the number of points such that the number of segments that cover these points equals to $$$i$$$. Note The picture describing the first example: Points with coordinates $$$[0, 4, 5, 6, 7, 8]$$$ are covered by one segment, points $$$[1, 2]$$$ are covered by two segments and point $$$[3]$$$ is covered by three segments. The picture describing the second example: Points $$$[1, 4, 5, 6, 7]$$$ are covered by one segment, points $$$[2, 3]$$$ are covered by two segments and there are no points covered by three segments.
1,700
false
false
true
false
true
false
false
false
true
false
5,703
769B
Polycarp studies at the university in the group which consists of _n_ students (including himself). All they are registrated in the social net "TheContacnt!". Not all students are equally sociable. About each student you know the value _a__i_ — the maximum number of messages which the _i_-th student is agree to send per day. The student can't send messages to himself. In early morning Polycarp knew important news that the programming credit will be tomorrow. For this reason it is necessary to urgently inform all groupmates about this news using private messages. Your task is to make a plan of using private messages, so that: the student _i_ sends no more than _a__i_ messages (for all _i_ from 1 to _n_); all students knew the news about the credit (initially only Polycarp knew it); the student can inform the other student only if he knows it himself. Let's consider that all students are numerated by distinct numbers from 1 to _n_, and Polycarp always has the number 1. In that task you shouldn't minimize the number of messages, the moment of time, when all knew about credit or some other parameters. Find any way how to use private messages which satisfies requirements above. Input The first line contains the positive integer _n_ (2u2009≤u2009_n_u2009≤u2009100) — the number of students. The second line contains the sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009100), where _a__i_ equals to the maximum number of messages which can the _i_-th student agree to send. Consider that Polycarp always has the number 1. Output Print -1 to the first line if it is impossible to inform all students about credit. Otherwise, in the first line print the integer _k_ — the number of messages which will be sent. In each of the next _k_ lines print two distinct integers _f_ and _t_, meaning that the student number _f_ sent the message with news to the student number _t_. All messages should be printed in chronological order. It means that the student, who is sending the message, must already know this news. It is assumed that students can receive repeated messages with news of the credit. If there are several answers, it is acceptable to print any of them. Examples Output 6 1 3 3 4 1 2 4 5 5 6 4 6 Note In the first test Polycarp (the student number 1) can send the message to the student number 2, who after that can send the message to students number 3 and 4. Thus, all students knew about the credit.
1,200
false
true
false
false
false
false
false
false
false
false
6,705
75D
Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't. This problem is similar to a standard problem but it has a different format and constraints. In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum. But in this problem you are given _n_ small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array. For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9. Can you help Mostafa solve this problem? Input The first line contains two integers _n_ and _m_, _n_ is the number of the small arrays (1u2009≤u2009_n_u2009≤u200950), and _m_ is the number of indexes in the big array (1u2009≤u2009_m_u2009≤u2009250000). Then follow _n_ lines, the _i_-th line starts with one integer _l_ which is the size of the _i_-th array (1u2009≤u2009_l_u2009≤u20095000), followed by _l_ integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains _m_ integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to _n_. The small arrays are numbered from 1 to _n_ in the same order as given in the input. Some of the given small arrays may not be used in big array. Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded. Output Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty. Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).
2,000
true
true
true
true
true
false
false
false
false
false
9,588
976C
Problem - 976C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation sortings *1500 No tag edit access → Contest materials — the number of segments. Each of the next _n_ lines contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009109) — the _i_-th segment. Output Print two distinct indices _i_ and _j_ such that segment _a__i_ lies within segment _a__j_. If there are multiple answers, print any of them. If no answer exists, print -1 -1. Examples Input 5 1 10 2 9 3 9 2 3 2 9 Output 2 1 Input 3 1 5 2 6 6 20 Output -1 -1 Note In the first example the following pairs are considered correct: (2,u20091),u2009(3,u20091),u2009(4,u20091),u2009(5,u20091) — not even touching borders; (3,u20092),u2009(4,u20092),u2009(3,u20095),u2009(4,u20095) — touch one border; (5,u20092),u2009(2,u20095) — match exactly.
1,500
false
true
true
false
false
false
false
false
true
false
5,828
793F
After hard work Igor decided to have some rest. He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium. Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippery. To help the snail Igor put some ropes on the tree, fixing the lower end of the _i_-th rope on the trunk on the height _l__i_ above the ground, and the higher end on the height _r__i_ above the ground. For some reason no two ropes share the same position of the higher end, i.e. all _r__i_ are distinct. Now Julia can move down at any place of the trunk, and also move up from the lower end of some rope to its higher end. Igor is proud of his work, and sometimes think about possible movements of the snail. Namely, he is interested in the following questions: «Suppose the snail is on the trunk at height _x_ now. What is the highest position on the trunk the snail can get on if it would never be lower than _x_ or higher than _y_?» Please note that Julia can't move from a rope to the trunk before it reaches the higher end of the rope, and Igor is interested in the highest position on the tree trunk. Igor is interested in many questions, and not always can answer them. Help him, write a program that answers these questions. Input The first line contains single integer _n_ (1u2009u2009≤u2009u2009_n_u2009≤u2009u2009100000)xa0— the height of the trunk. The second line contains single integer _m_ (1u2009u2009≤u2009u2009_m_u2009u2009≤u2009u2009100000)xa0— the number of ropes. The next _m_ lines contain information about the ropes. The _i_-th of these lines contains two integers _l__i_ and _r__i_ (1u2009u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_)xa0— the heights on which the lower and the higher ends of the _i_-th rope are fixed, respectively. It is guaranteed that all _r__i_ are distinct. The next line contains single integer _q_ (1u2009u2009≤u2009u2009_q_u2009u2009≤u2009u2009100000)xa0— the number of questions. The next _q_ lines contain information about the questions. Each of these lines contain two integers _x_ and _y_ (1u2009u2009≤u2009_x_u2009≤u2009_y_u2009≤u2009_n_), where _x_ is the height where Julia starts (and the height Julia can't get lower than), and _y_ is the height Julia can't get higher than. Output For each question print the maximum reachable for Julia height. Examples Input 8 4 1 2 3 4 2 5 6 7 5 1 2 1 4 1 6 2 7 6 8 Input 10 10 3 7 1 4 1 6 5 5 1 1 3 9 7 8 1 2 3 3 7 10 10 2 4 1 7 3 4 3 5 2 8 2 5 5 5 3 5 7 7 3 10 Note The picture of the first sample is on the left, the picture of the second sample is on the right. Ropes' colors are just for clarity, they don't mean anything.
3,000
false
false
false
true
true
false
false
false
false
false
6,602
558B
Problem - 558B - 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 *1300 No tag edit access → Contest materials ") Editorial") , the size of the array. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106), representing elements of the array. Output Output two integers _l_,u2009_r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_), the beginning and the end of the subsegment chosen respectively. If there are several possible answers you may output any of them. Examples Input 5 1 1 2 2 1 Output 1 5 Input 5 1 2 2 3 1 Output 2 3 Input 6 1 2 2 1 1 2 Output 1 5 Note A subsegment _B_ of an array _A_ from _l_ to _r_ is an array of size _r_u2009-u2009_l_u2009+u20091 where _B__i_u2009=u2009_A__l_u2009+u2009_i_u2009-u20091 for all 1u2009≤u2009_i_u2009≤u2009_r_u2009-u2009_l_u2009+u20091
1,300
false
false
true
false
false
false
false
false
false
false
7,602
1674F
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size $$$n imes m$$$ consisting of characters '.' (empty cell of the desktop) and '*' (an icon). The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement. In one move, you can take one icon and move it to any empty cell in the desktop. Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer $$$q$$$ queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon? Note that queries are permanent and change the state of the desktop. Input The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 le n, m le 1000; 1 le q le 2 cdot 10^5$$$) — the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively. The next $$$n$$$ lines contain the description of the desktop. The $$$i$$$-th of them contains $$$m$$$ characters '.' and '*' — the description of the $$$i$$$-th row of the desktop. The next $$$q$$$ lines describe queries. The $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le n; 1 le y_i le m$$$) — the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell). Output Print $$$q$$$ integers. The $$$i$$$-th of them should be the minimum number of moves required to make the desktop good after applying the first $$$i$$$ queries. Examples Input 4 4 8 ..** .*.. *... ...* 1 3 2 3 3 1 2 3 3 4 4 3 2 3 2 2 Input 2 5 5 *...* ***** 1 3 2 2 1 3 1 5 2 3
1,800
false
true
true
false
true
false
false
false
false
false
2,224
1508D
Based on a peculiar incident at basketball practice, Akari came up with the following competitive programming problem! You are given $$$n$$$ points on the plane, no three of which are collinear. The $$$i$$$-th point initially has a label $$$a_i$$$, in such a way that the labels $$$a_1, a_2, dots, a_n$$$ form a permutation of $$$1, 2, dots, n$$$. You are allowed to modify the labels through the following operation: 1. Choose two distinct integers $$$i$$$ and $$$j$$$ between $$$1$$$ and $$$n$$$. 2. Swap the labels of points $$$i$$$ and $$$j$$$, and finally 3. Draw the segment between points $$$i$$$ and $$$j$$$. A sequence of operations is valid if after applying all of the operations in the sequence in order, the $$$k$$$-th point ends up having the label $$$k$$$ for all $$$k$$$ between $$$1$$$ and $$$n$$$ inclusive, and the drawn segments don't intersect each other internally. Formally, if two of the segments intersect, then they must do so at a common endpoint of both segments. In particular, all drawn segments must be distinct. Find any valid sequence of operations, or say that none exist. Input The first line contains an integer $$$n$$$ ($$$3 le n le 2000$$$) xa0— the number of points. The $$$i$$$-th of the following $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$a_i$$$ ($$$-10^6 le x_i, y_i le 10^6$$$, $$$1 le a_i le n$$$), representing that the $$$i$$$-th point has coordinates $$$(x_i, y_i)$$$ and initially has label $$$a_i$$$. It is guaranteed that all points are distinct, no three points are collinear, and the labels $$$a_1, a_2, dots, a_n$$$ form a permutation of $$$1, 2, dots, n$$$. Output If it is impossible to perform a valid sequence of operations, print $$$-1$$$. Otherwise, print an integer $$$k$$$ ($$$0 le k le frac{n(n - 1)}{2}$$$) xa0— the number of operations to perform, followed by $$$k$$$ lines, each containing two integers $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$, $$$i eq j$$$) xa0— the indices of the points chosen for the operation. Note that you are not required to minimize or maximize the value of $$$k$$$. If there are multiple possible answers, you may print any of them. Examples Input 5 -1 -2 2 3 0 5 1 3 4 4 -3 3 5 2 1 Output 5 1 2 5 3 4 5 1 5 1 3 Input 3 5 4 1 0 0 2 -3 -2 3 Note The following animation showcases the first sample test case. The black numbers represent the indices of the points, while the boxed orange numbers represent their labels. In the second test case, all labels are already in their correct positions, so no operations are necessary.
3,000
false
false
false
false
false
true
false
false
true
false
3,133
997A
You've got a string $$$a_1, a_2, dots, a_n$$$, consisting of zeros and ones. Let's call a sequence of consecutive elements $$$a_i, a_{iu2009+u20091}, ldots,u2009a_j$$$ ($$$1lequ2009ilequ2009jlequ2009n$$$) a substring of string $$$a$$$. You can apply the following operations any number of times: Choose some substring of string $$$a$$$ (for example, you can choose entire string) and reverse it, paying $$$x$$$ coins for it (for example, «0101101» $$$ o$$$ «0111001»); Choose some substring of string $$$a$$$ (for example, you can choose entire string or just one symbol) and replace each symbol to the opposite one (zeros are replaced by ones, and onesxa0— by zeros), paying $$$y$$$ coins for it (for example, «0101101» $$$ o$$$ «0110001»). You can apply these operations in any order. It is allowed to apply the operations multiple times to the same substring. What is the minimum number of coins you need to spend to get a string consisting only of ones? Input The first line of input contains integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$1u2009lequ2009nu2009lequ2009300,000, 0 leq x, y leq 10^9$$$)xa0— length of the string, cost of the first operation (substring reverse) and cost of the second operation (inverting all elements of substring). The second line contains the string $$$a$$$ of length $$$n$$$, consisting of zeros and ones. Output Print a single integerxa0— the minimum total cost of operations you need to spend to get a string consisting only of ones. Print $$$0$$$, if you do not need to perform any operations. Note In the first sample, at first you need to reverse substring $$$[1 dots 2]$$$, and then you need to invert substring $$$[2 dots 5]$$$. Then the string was changed as follows: «01000» $$$ o$$$ «10000» $$$ o$$$ «11111». The total cost of operations is $$$1 + 10 = 11$$$. In the second sample, at first you need to invert substring $$$[1 dots 1]$$$, and then you need to invert substring $$$[3 dots 5]$$$. Then the string was changed as follows: «01000» $$$ o$$$ «11000» $$$ o$$$ «11111». The overall cost is $$$1 + 1 = 2$$$. In the third example, string already consists only of ones, so the answer is $$$0$$$.
1,500
true
true
true
false
false
false
true
false
false
false
5,718
385C
Recently, the bear started studying data structures and faced the following problem. You are given a sequence of integers _x_1,u2009_x_2,u2009...,u2009_x__n_ of length _n_ and _m_ queries, each of them is characterized by two integers _l__i_,u2009_r__i_. Let's introduce _f_(_p_) to represent the number of such indexes _k_, that _x__k_ is divisible by _p_. The answer to the query _l__i_,u2009_r__i_ is the sum: , where _S_(_l__i_,u2009_r__i_) is a set of prime numbers from segment [_l__i_,u2009_r__i_] (both borders are included in the segment). Help the bear cope with the problem. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009106). The second line contains _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (2u2009≤u2009_x__i_u2009≤u2009107). The numbers are not necessarily distinct. The third line contains integer _m_ (1u2009≤u2009_m_u2009≤u200950000). Each of the following _m_ lines contains a pair of space-separated integers, _l__i_ and _r__i_ (2u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u20092·109) — the numbers that characterize the current query. Output Print _m_ integers — the answers to the queries on the order the queries appear in the input. Examples Input 6 5 5 7 10 14 15 3 2 11 3 12 4 4 Input 7 2 3 5 7 11 4 8 2 8 10 2 123 Note Consider the first sample. Overall, the first sample has 3 queries. 1. The first query _l_u2009=u20092, _r_u2009=u200911 comes. You need to count _f_(2)u2009+u2009_f_(3)u2009+u2009_f_(5)u2009+u2009_f_(7)u2009+u2009_f_(11)u2009=u20092u2009+u20091u2009+u20094u2009+u20092u2009+u20090u2009=u20099. 2. The second query comes _l_u2009=u20093, _r_u2009=u200912. You need to count _f_(3)u2009+u2009_f_(5)u2009+u2009_f_(7)u2009+u2009_f_(11)u2009=u20091u2009+u20094u2009+u20092u2009+u20090u2009=u20097. 3. The third query comes _l_u2009=u20094, _r_u2009=u20094. As this interval has no prime numbers, then the sum equals 0.
1,700
true
false
true
true
true
false
true
true
false
false
8,289
1527B2
The only difference between the easy and hard versions is that the given string $$$s$$$ in the easy version is initially a palindrome, this condition is not always true for the hard version. A palindrome is a string that reads the same left to right and right to left. For example, "101101" is a palindrome, while "0101" is not. Alice and Bob are playing a game on a string $$$s$$$ of length $$$n$$$ consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first. In each turn, the player can perform one of the following operations: 1. Choose any $$$i$$$ ($$$1 le i le n$$$), where $$$s[i] =$$$ '0' and change $$$s[i]$$$ to '1'. Pay 1 dollar. 2. Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, "01001" becomes "10010" after reversing. The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$). Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^3$$$). The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting of the characters '0' and '1'. It is guaranteed that the string $$$s$$$ contains at least one '0'. Note that there is no limit on the sum of $$$n$$$ over test cases. Output For each test case print a single word in a new line: "ALICE", if Alice will win the game, "BOB", if Bob will win the game, "DRAW", if the game ends in a draw. Note In the first test case of example, in the $$$1$$$-st move, Alice will use the $$$2$$$-nd operation to reverse the string, since doing the $$$1$$$-st operation will result in her loss anyway. This also forces Bob to use the $$$1$$$-st operation. in the $$$2$$$-nd move, Bob has to perform the $$$1$$$-st operation, since the $$$2$$$-nd operation cannot be performed twice in a row. All characters of the string are '1', game over. Alice spends $$$0$$$ dollars while Bob spends $$$1$$$ dollar. Hence, Alice wins. In the second test case of example, in the $$$1$$$-st move Alice has to perform the $$$1$$$-st operation, since the string is currently a palindrome. in the $$$2$$$-nd move Bob reverses the string. in the $$$3$$$-rd move Alice again has to perform the $$$1$$$-st operation. All characters of the string are '1', game over. Alice spends $$$2$$$ dollars while Bob spends $$$0$$$ dollars. Hence, Bob wins.
1,900
false
false
false
false
false
true
false
false
false
false
3,027
1476A
Problem - 1476A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search constructive algorithms greedy math *1000 No tag edit access → Contest materials $$$ is divisible by $$$k$$$ and maximum element in $$$a$$$ is minimum possible. What is the minimum possible maximum element in $$$a$$$? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^9$$$; $$$1 le k le 10^9$$$). Output For each test case, print one integerxa0— the minimum possible maximum element in array $$$a$$$ such that the sum $$$(a_1 + dots + a_n)$$$ is divisible by $$$k$$$. Example Input 4 1 5 4 3 8 8 8 17 Output 5 2 1 3 Note In the first test case $$$n = 1$$$, so the array consists of one element $$$a_1$$$ and if we make $$$a_1 = 5$$$ it will be divisible by $$$k = 5$$$ and the minimum possible. In the second test case, we can create array $$$a =
1,000
true
true
false
false
false
true
false
true
false
false
3,297
888D
Problem - 888D - Codeforces =============== xa0 such that _p__i_u2009=u2009_i_. Your task is to count the number of almost identity permutations for given numbers _n_ and _k_. Input The first line contains two integers _n_ and _k_ (4u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_k_u2009≤u20094). Output Print the number of almost identity permutations for given _n_ and _k_. Examples Input 4 1 Output 1 Input 4 2 Output 7 Input 5 3 Output 31 Input 5 4 Output 76
1,600
true
false
false
true
false
false
false
false
false
false
6,193
1205D
You are given a tree with $$$n$$$ nodes. You have to write non-negative integers on its edges so that the following condition would be satisfied: For every two nodes $$$i$$$, $$$j$$$, look at the path between them and count the sum of numbers on the edges of this path. Write all obtained sums on the blackboard. Then every integer from $$$1$$$ to $$$lfloor frac{2n^2}{9} floor$$$ has to be written on the blackboard at least once. It is guaranteed that such an arrangement exists. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 1000$$$)xa0— the number of nodes. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$), meaning that there is an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that these edges form a tree. Output Output $$$n-1$$$ lines, each of form $$$u$$$ $$$v$$$ $$$x$$$ ($$$0 le x le 10^6$$$), which will mean that you wrote number $$$x$$$ on the edge between $$$u$$$, $$$v$$$. Set of edges $$$(u, v)$$$ has to coincide with the set of edges of the input graph, but you can output edges in any order. You can also output ends of edges in an order different from the order in input. Examples Output 2 1 1 5 2 1 3 1 3 4 1 6 Note In the first example, distance between nodes $$$1$$$ and $$$2$$$ is equal to $$$2$$$, between nodes $$$2$$$ and $$$3$$$ to $$$1$$$, between $$$1$$$ and $$$3$$$ to $$$3$$$. In the third example, numbers from $$$1$$$ to $$$9$$$ (inclusive) will be written on the blackboard, while we need just from $$$1$$$ to $$$5$$$ to pass the test.
2,700
false
false
false
false
false
true
false
false
false
false
4,666
1876G
There is an extremely hard video game that is one of Chaneka's favourite video games. One of the hardest levels in the game is called Clubstep. Clubstep consists of $$$n$$$ parts, numbered from $$$1$$$ to $$$n$$$. Chaneka has practised the level a good amount, so currently, her familiarity value with each part $$$i$$$ is $$$a_i$$$. After this, Chaneka can do several (possibly zero) attempts on Clubstep. In each attempt, she dies on one of the $$$n$$$ parts. If an attempt dies on part $$$p$$$, that means it only successfully passes through every part $$$k$$$ for all $$$1 leq k leq p-1$$$ and it does not reach any part $$$k$$$ for all $$$p+1 leq k leq n$$$. An attempt that dies on part $$$p$$$ takes $$$p$$$ seconds. It is known that Chaneka improves much more on the part she dies on than anything else. It is also known that during an attempt, Chaneka does not get to practise that much on the parts she does not reach. So, the effect of an attempt that dies on part $$$p$$$ is as follows: Chaneka's familiarity value with part $$$p$$$ increases by $$$2$$$. Chaneka's familiarity value with each part $$$k$$$ for all $$$1 leq k leq p-1$$$ increases by $$$1$$$. There will be $$$q$$$ questions. For the $$$j$$$-th question, you are given three integers $$$l_j$$$, $$$r_j$$$, and $$$x_j$$$. Then, you are asked to find out the minimum time (in seconds) for Chaneka to make it such that the familiarity value for every part $$$p$$$ ($$$l_j leq p leq r_j$$$) is at least $$$x_j$$$. Note that each question is independent, so the attempt Chaneka does on a question does not affect the familiarity values of any other questions. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 3cdot10^5$$$) — the number of parts in Clubstep. The second line contains $$$n$$$ integers $$$a_1,a_2,a_3,ldots,a_n$$$ ($$$1leq a_ileq10^9$$$) — Chaneka's familiarity value with each part. The third line contains a single integer $$$q$$$ ($$$1leq qleq3cdot10^5$$$) — the number of questions. The $$$j$$$-th of the next $$$q$$$ lines contains three integers $$$l_j$$$, $$$r_j$$$, and $$$x_j$$$ ($$$1leq l_jleq r_jleq n$$$; $$$1leq x_jleq10^9$$$) — the description of the $$$j$$$-th question. Note For the $$$1$$$-st question, one possible strategy is to do the following: 1. Do $$$1$$$ attempt that dies on part $$$1$$$. This takes $$$1$$$ second. The familiarity values become $$$[3, 3, 2, 1, 2]$$$. 2. Do $$$1$$$ attempt that dies on part $$$4$$$. This takes $$$4$$$ seconds. The familiarity values become $$$[4, 4, 3, 3, 2]$$$. 3. Do $$$2$$$ attempts that die on part $$$5$$$. This takes $$$10$$$ seconds. The familiarity values become $$$[6, 6, 5, 5, 6]$$$. The total time taken (in seconds) is $$$1+4+10=15$$$. For the $$$2$$$-nd question, one possible strategy is to do the following: 1. Do $$$1$$$ attempt that dies on part $$$3$$$. This takes $$$3$$$ seconds. The familiarity values become $$$[2, 4, 4, 1, 2]$$$. 2. Do $$$2$$$ attempts that die on part $$$4$$$. This takes $$$8$$$ seconds. The familiarity values become $$$[4, 6, 6, 5, 2]$$$. The total time taken (in seconds) is $$$3+8=11$$$.
3,500
false
true
false
false
true
false
true
true
false
false
1,020
1510K
The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation $$$p_i$$$ of numbers from 1 to $$$2n$$$. You can make two types of operations. 1. Swap $$$p_1$$$ and $$$p_2$$$, $$$p_3$$$ and $$$p_4$$$, ..., $$$p_{2n-1}$$$ and $$$p_{2n}$$$. 2. Swap $$$p_1$$$ and $$$p_{n+1}$$$, $$$p_2$$$ and $$$p_{n+2}$$$, ..., $$$p_{n}$$$ and $$$p_{2n}$$$. The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task. Input The first line contains the integer $$$n$$$ ($$$1le nle 1000$$$). The second line contains $$$2n$$$ integers $$$p_i$$$xa0— the permutation of numbers from 1 to $$$2n$$$. Output Print one integerxa0— the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print $$$-1$$$. Note In the first example, you can sort the permutation in three operations: 1. Make operation 1: $$$3, 6, 5, 2, 1, 4$$$. 2. Make operation 2: $$$2, 1, 4, 3, 6, 5$$$. 3. Make operation 1: $$$1, 2, 3, 4, 5, 6$$$.
1,200
false
false
true
false
false
false
true
false
false
true
3,117
1194E
There are $$$n$$$ segments drawn on a plane; the $$$i$$$-th segment connects two points ($$$x_{i, 1}$$$, $$$y_{i, 1}$$$) and ($$$x_{i, 2}$$$, $$$y_{i, 2}$$$). Each segment is non-degenerate, and is either horizontal or vertical — formally, for every $$$i in [1, n]$$$ either $$$x_{i, 1} = x_{i, 2}$$$ or $$$y_{i, 1} = y_{i, 2}$$$ (but only one of these conditions holds). Only segments of different types may intersect: no pair of horizontal segments shares any common points, and no pair of vertical segments shares any common points. We say that four segments having indices $$$h_1$$$, $$$h_2$$$, $$$v_1$$$ and $$$v_2$$$ such that $$$h_1 < h_2$$$ and $$$v_1 < v_2$$$ form a rectangle if the following conditions hold: segments $$$h_1$$$ and $$$h_2$$$ are horizontal; segments $$$v_1$$$ and $$$v_2$$$ are vertical; segment $$$h_1$$$ intersects with segment $$$v_1$$$; segment $$$h_2$$$ intersects with segment $$$v_1$$$; segment $$$h_1$$$ intersects with segment $$$v_2$$$; segment $$$h_2$$$ intersects with segment $$$v_2$$$. Please calculate the number of ways to choose four segments so they form a rectangle. Note that the conditions $$$h_1 < h_2$$$ and $$$v_1 < v_2$$$ should hold. Input The first line contains one integer $$$n$$$ ($$$1 le n le 5000$$$) — the number of segments. Then $$$n$$$ lines follow. The $$$i$$$-th line contains four integers $$$x_{i, 1}$$$, $$$y_{i, 1}$$$, $$$x_{i, 2}$$$ and $$$y_{i, 2}$$$ denoting the endpoints of the $$$i$$$-th segment. All coordinates of the endpoints are in the range $$$[-5000, 5000]$$$. It is guaranteed that each segment is non-degenerate and is either horizontal or vertical. Furthermore, if two segments share a common point, one of these segments is horizontal, and another one is vertical. Output Print one integer — the number of ways to choose four segments so they form a rectangle. Examples Input 7 -1 4 -1 -2 6 -1 -2 -1 -2 3 6 3 2 -2 2 4 4 -1 4 3 5 3 5 1 5 2 1 2 Input 5 1 5 1 0 0 1 5 1 5 4 0 4 4 2 4 0 4 3 4 5 Note The following pictures represent sample cases:
2,200
false
false
false
false
true
false
true
false
true
false
4,732
1452E
Berland regional ICPC contest has just ended. There were $$$m$$$ participants numbered from $$$1$$$ to $$$m$$$, who competed on a problemset of $$$n$$$ problems numbered from $$$1$$$ to $$$n$$$. Now the editorial is about to take place. There are two problem authors, each of them is going to tell the tutorial to exactly $$$k$$$ consecutive tasks of the problemset. The authors choose the segment of $$$k$$$ consecutive tasks for themselves independently of each other. The segments can coincide, intersect or not intersect at all. The $$$i$$$-th participant is interested in listening to the tutorial of all consecutive tasks from $$$l_i$$$ to $$$r_i$$$. Each participant always chooses to listen to only the problem author that tells the tutorials to the maximum number of tasks he is interested in. Let this maximum number be $$$a_i$$$. No participant can listen to both of the authors, even if their segments don't intersect. The authors want to choose the segments of $$$k$$$ consecutive tasks for themselves in such a way that the sum of $$$a_i$$$ over all participants is maximized. Input The first line contains three integers $$$n, m$$$ and $$$k$$$ ($$$1 le n, m le 2000$$$, $$$1 le k le n$$$)xa0— the number of problems, the number of participants and the length of the segment of tasks each of the problem authors plans to tell the tutorial to. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$)xa0— the segment of tasks the $$$i$$$-th participant is interested in listening to the tutorial to. Output Print a single integerxa0— the maximum sum of $$$a_i$$$ over all participants. Examples Input 10 5 3 1 3 2 4 6 9 6 9 1 8 Input 4 4 1 3 3 1 1 2 2 4 4 Input 5 4 5 1 2 2 3 3 4 4 5 Note In the first example the first author can tell the tutorial to problems from $$$1$$$ to $$$3$$$ and the second onexa0— from $$$6$$$ to $$$8$$$. That way the sequence of $$$a_i$$$ will be $$$[3, 2, 3, 3, 3]$$$. Notice that the last participant can't listen to both author, he only chooses the one that tells the maximum number of problems he's interested in. In the second example the first one can tell problems $$$2$$$ to $$$4$$$, the second onexa0— $$$4$$$ to $$$6$$$. In the third example the first one can tell problems $$$1$$$ to $$$1$$$, the second onexa0— $$$2$$$ to $$$2$$$. Or $$$4$$$ to $$$4$$$ and $$$3$$$ to $$$3$$$. Every pair of different problems will get the same sum of $$$2$$$. In the fourth example the first one can tell problems $$$1$$$ to $$$5$$$, the second onexa0— $$$1$$$ to $$$5$$$ as well.
2,500
false
true
false
true
false
false
true
false
true
false
3,417
1181E1
This problem differs from the next problem only in constraints. Petya decided to visit Byteland during the summer holidays. It turned out that the history of this country is quite unusual. Initially, there were $$$n$$$ different countries on the land that is now Berland. Each country had its own territory that was represented as a rectangle on the map. The sides of the rectangle were parallel to the axes, and the corners were located at points with integer coordinates. Territories of no two countries intersected, but it was possible that some territories touched each other. As time passed, sometimes two countries merged into one. It only happened if the union of their territories was also a rectangle. In the end only one country remainedxa0— Byteland. Initially, each country had a rectangular castle inside its territory. Its sides were parallel to the axes and its corners had integer coordinates. Some castles might touch the border of the corresponding country and sides or other castles. Miraculously, after all the unions the castles are still intact. Unfortunately, their locations are the only information we have to restore the initial territories of the countries. The possible formation of Byteland. The castles are shown in blue. Petya wonders why no information about the initial countries remained. He suspected that the whole story is a fake. You were recommended to him as a smart person. Please check whether or not there exists a possible set of initial territories that could make the story true. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 1000$$$)xa0— the number of countries and castles. Each of the next $$$n$$$ lines contains four integers $$$a_i, b_i, c_i, d_i$$$ ($$$0 leq a_i < c_i leq 10^9$$$, $$$0 leq b_i < d_i leq 10^9$$$)xa0— the coordinates of the $$$i$$$-th castle, where $$$(a_i, b_i)$$$ are the coordinates of the lower left corner and $$$(c_i, d_i)$$$ are the coordinates of the upper right corner. It is guaranteed, that no two castles intersect, however, they may touch. Output If there exists a possible set of territories that satisfies the story, print "YES", otherwise print "NO". You can print each letter in any case (upper or lower). Examples Input 4 0 0 1 2 0 2 1 3 1 0 2 1 1 1 2 3 Input 4 0 0 2 1 1 2 3 3 2 0 3 2 0 1 1 3 Note The castles in the first and second examples are shown on the pictures below.
2,500
false
false
false
false
false
false
true
false
true
false
4,810
995D
Allen and Bessie are playing a simple number game. They both know a function $$$f: {0, 1}^n o mathbb{R}$$$, i.xa0e. the function takes $$$n$$$ binary arguments and returns a real value. At the start of the game, the variables $$$x_1, x_2, dots, x_n$$$ are all set to $$$-1$$$. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an $$$i$$$ such that $$$x_i = -1$$$ and either setting $$$x_i o 0$$$ or $$$x_i o 1$$$. After $$$n$$$ rounds all variables are set, and the game value resolves to $$$f(x_1, x_2, dots, x_n)$$$. Allen wants to maximize the game value, and Bessie wants to minimize it. Your goal is to help Allen and Bessie find the expected game value! They will play $$$r+1$$$ times though, so between each game, exactly one value of $$$f$$$ changes. In other words, between rounds $$$i$$$ and $$$i+1$$$ for $$$1 le i le r$$$, $$$f(z_1, dots, z_n) o g_i$$$ for some $$$(z_1, dots, z_n) in {0, 1}^n$$$. You are to find the expected game value in the beginning and after each change. Input The first line contains two integers $$$n$$$ and $$$r$$$ ($$$1 le n le 18$$$, $$$0 le r le 2^{18}$$$). The next line contains $$$2^n$$$ integers $$$c_0, c_1, dots, c_{2^n-1}$$$ ($$$0 le c_i le 10^9$$$), denoting the initial values of $$$f$$$. More specifically, $$$f(x_0, x_1, dots, x_{n-1}) = c_x$$$, if $$$x = overline{x_{n-1} ldots x_0}$$$ in binary. Each of the next $$$r$$$ lines contains two integers $$$z$$$ and $$$g$$$ ($$$0 le z le 2^n - 1$$$, $$$0 le g le 10^9$$$). If $$$z = overline{z_{n-1} dots z_0}$$$ in binary, then this means to set $$$f(z_0, dots, z_{n-1}) o g$$$. Output Print $$$r+1$$$ lines, the $$$i$$$-th of which denotes the value of the game $$$f$$$ during the $$$i$$$-th round. Your answer must have absolute or relative error within $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$frac{a - b}{max{(1, b)}} le 10^{-6}$$$. Examples Output 1.500000 2.250000 3.250000 Note Consider the second test case. If Allen goes first, he will set $$$x_1 o 1$$$, so the final value will be $$$3$$$. If Bessie goes first, then she will set $$$x_1 o 0$$$ so the final value will be $$$2$$$. Thus the answer is $$$2.5$$$. In the third test case, the game value will always be $$$1$$$ regardless of Allen and Bessie's play.
2,500
true
false
false
false
false
false
false
false
false
false
5,723
1891D
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 907 (Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search brute force math *1900 No tag edit access → Contest materials Announcement Editorial PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. Suspicious logarithms time limit per test1 second memory limit per test256 megabytes Let $$$f$$$($$$x$$$) be the floor of the binary logarithm of $$$x$$$. In other words, $$$f$$$($$$x$$$) is largest non-negative integer $$$y$$$, such that $$$2^y$$$ does not exceed $$$x$$$. Let $$$g$$$($$$x$$$) be the floor of the logarithm of $$$x$$$ with base $$$f$$$($$$x$$$). In other words, $$$g$$$($$$x$$$) is the largest non-negative integer $$$z$$$, such that $$${f(x)}^{z}$$$ does not exceed $$$x$$$. You are given $$$q$$$ queries. The $$$i$$$-th query consists of two integers $$$l_i$$$ and $$$r_i$$$. The answer to the query is the sum of $$$g$$$($$$k$$$) across all integers $$$k$$$, such that $$$l_i leq k leq r_i$$$. Since the answers might be large, print them modulo $$${10^9 + 7}$$$. Input The first line contains a single integer $$$q$$$ — the number of queries ($$$1 leq q leq 10^5$$$). The next $$$q$$$ lines each contain two integers $$$l_i$$$ and $$$r_i$$$ — the bounds of the $$$i$$$-th query ($$$4 leq l_i leq r_i leq 10^{18}$$$). Output For each query, output the answer to the query modulo $$$10^9 + 7$$$. Example input 12 4 6 4 7 4 8 4 100000 179 1000000000000000000 57 179 4 201018959 7 201018960 729 50624 728 50624 728 50625 729 50625 output 6 8 9 348641 41949982 246 1 0 149688 149690 149694 149692 Note The table below contains the values of the functions $$$f$$$($$$x$$$) and $$$g$$$($$$x$$$) for all $$$x$$$ such that $$$1 leq x leq 8$$$. $$$x$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$4$$$ $$$5$$$ $$$6$$$ $$$7$$$ $$$8$$$ $$$f$$$ $$$0$$$ $$$1$$$ $$$1$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$3$$$ $$$g$$$ $$$-$$$ $$$-$$$ $$$-$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$2$$$ $$$1$$$ Codeforces (c)
1,900
true
false
false
false
false
false
true
true
false
false
954
72G
Problem - 72G - Codeforces =============== xa0 → 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 *special problem dp *1000 No tag edit access → Contest materials . Output Write a single integer. The _n_-th Fibonacci number. Examples Input 2 Output 2 Input 1 Output 1
1,000
false
false
false
true
false
false
false
false
false
false
9,605
864E
Polycarp is in really serious trouble — his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take _t__i_ seconds to save _i_-th item. In addition, for each item, he estimated the value of _d__i_ — the moment after which the item _i_ will be completely burned and will no longer be valuable for him at all. In particular, if _t__i_u2009≥u2009_d__i_, then _i_-th item cannot be saved. Given the values _p__i_ for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item _a_ first, and then item _b_, then the item _a_ will be saved in _t__a_ seconds, and the item _b_ — in _t__a_u2009+u2009_t__b_ seconds after fire started. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of items in Polycarp's house. Each of the following _n_ lines contains three integers _t__i_,u2009_d__i_,u2009_p__i_ (1u2009≤u2009_t__i_u2009≤u200920, 1u2009≤u2009_d__i_u2009≤u20092u2009000, 1u2009≤u2009_p__i_u2009≤u200920) — the time needed to save the item _i_, the time after which the item _i_ will burn completely and the value of item _i_. Output In the first line print the maximum possible total value of the set of saved items. In the second line print one integer _m_ — the number of items in the desired set. In the third line print _m_ distinct integers — numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them. Note In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6u2009+u20095u2009=u200911. In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time.
2,000
false
false
false
true
false
false
false
false
true
false
6,283
1051F
You are given a weighed undirected connected graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. You should answer $$$q$$$ queries, the $$$i$$$-th query is to find the shortest distance between vertices $$$u_i$$$ and $$$v_i$$$. Input The first line contains two integers $$$n$$$ and $$$m~(1 le n, m le 10^5, m - n le 20)$$$ — the number of vertices and edges in the graph. Next $$$m$$$ lines contain the edges: the $$$i$$$-th edge is a triple of integers $$$v_i, u_i, d_i~(1 le u_i, v_i le n, 1 le d_i le 10^9, u_i eq v_i)$$$. This triple means that there is an edge between vertices $$$u_i$$$ and $$$v_i$$$ of weight $$$d_i$$$. It is guaranteed that graph contains no self-loops and multiple edges. The next line contains a single integer $$$q~(1 le q le 10^5)$$$ — the number of queries. Each of the next $$$q$$$ lines contains two integers $$$u_i$$$ and $$$v_i~(1 le u_i, v_i le n)$$$ — descriptions of the queries. Pay attention to the restriction $$$m - n ~ le ~ 20$$$. Output Print $$$q$$$ lines. The $$$i$$$-th line should contain the answer to the $$$i$$$-th query — the shortest distance between vertices $$$u_i$$$ and $$$v_i$$$. Examples Input 3 3 1 2 3 2 3 1 3 1 5 3 1 2 1 3 2 3 Input 8 13 1 2 4 2 3 6 3 4 1 4 5 12 5 6 3 6 7 8 7 8 7 1 4 1 1 8 3 2 6 9 2 7 1 4 6 3 6 8 2 8 1 5 1 7 2 3 2 8 3 7 3 4 6 8 7 8
2,400
false
false
false
false
false
false
false
false
false
true
5,450
868D
You are given _n_ strings _s_1,u2009_s_2,u2009...,u2009_s__n_ consisting of characters 0 and 1. _m_ operations are performed, on each of them you concatenate two existing strings into a new one. On the _i_-th operation the concatenation _s__a__i__s__b__i_ is saved into a new string _s__n_u2009+u2009_i_ (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer _k_ such that all possible strings consisting of 0 and 1 of length _k_ (there are 2_k_ such strings) are substrings of the new string. If there is no such _k_, print 0. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of strings. The next _n_ lines contain strings _s_1,u2009_s_2,u2009...,u2009_s__n_ (1u2009≤u2009_s__i_u2009≤u2009100), one per line. The total length of strings is not greater than 100. The next line contains single integer _m_ (1u2009≤u2009_m_u2009≤u2009100)xa0— the number of operations. _m_ lines follow, each of them contains two integers _a__i_ abd _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_u2009+u2009_i_u2009-u20091)xa0— the number of strings that are concatenated to form _s__n_u2009+u2009_i_. Output Print _m_ lines, each should contain one integerxa0— the answer to the question after the corresponding operation. Example Input 5 01 10 101 11111 0 3 1 2 6 5 4 4 Note On the first operation, a new string "0110" is created. For _k_u2009=u20091 the two possible binary strings of length _k_ are "0" and "1", they are substrings of the new string. For _k_u2009=u20092 and greater there exist strings of length _k_ that do not appear in this string (for _k_u2009=u20092 such string is "00"). So the answer is 1. On the second operation the string "01100" is created. Now all strings of length _k_u2009=u20092 are present. On the third operation the string "1111111111" is created. There is no zero, so the answer is 0.
2,200
false
false
true
true
false
false
true
false
false
false
6,270
1303F
You are given a matrix $$$n imes m$$$, initially filled with zeroes. We define $$$a_{i, j}$$$ as the element in the $$$i$$$-th row and the $$$j$$$-th column of the matrix. Two cells of the matrix are connected if they share a side, and the elements in these cells are equal. Two cells of the matrix belong to the same connected component if there exists a sequence $$$s_1$$$, $$$s_2$$$, ..., $$$s_k$$$ such that $$$s_1$$$ is the first cell, $$$s_k$$$ is the second cell, and for every $$$i in [1, k - 1]$$$, $$$s_i$$$ and $$$s_{i + 1}$$$ are connected. You are given $$$q$$$ queries of the form $$$x_i$$$ $$$y_i$$$ $$$c_i$$$ ($$$i in [1, q]$$$). For every such query, you have to do the following: 1. replace the element $$$a_{x, y}$$$ with $$$c$$$; 2. count the number of connected components in the matrix. There is one additional constraint: for every $$$i in [1, q - 1]$$$, $$$c_i le c_{i + 1}$$$. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 le n, m le 300$$$, $$$1 le q le 2 cdot 10^6$$$) — the number of rows, the number of columns and the number of queries, respectively. Then $$$q$$$ lines follow, each representing a query. The $$$i$$$-th line contains three integers $$$x_i$$$, $$$y_i$$$ and $$$c_i$$$ ($$$1 le x_i le n$$$, $$$1 le y_i le m$$$, $$$1 le c_i le max(1000, lceil frac{2 cdot 10^6}{nm} ceil)$$$). For every $$$i in [1, q - 1]$$$, $$$c_i le c_{i + 1}$$$. Output Print $$$q$$$ integers, the $$$i$$$-th of them should be equal to the number of components in the matrix after the first $$$i$$$ queries are performed. Example Input 3 2 10 2 1 1 1 2 1 2 2 1 1 1 2 3 1 2 1 2 2 2 2 2 2 1 2 3 2 4 2 1 5 Output 2 4 3 3 4 4 4 2 2 4
2,800
false
false
true
false
false
false
false
false
false
false
4,173
1933C
You are given three positive integers $$$a$$$, $$$b$$$ and $$$l$$$ ($$$a,b,l>0$$$). It can be shown that there always exists a way to choose non-negative (i.e. $$$ge 0$$$) integers $$$k$$$, $$$x$$$, and $$$y$$$ such that $$$l = k cdot a^x cdot b^y$$$. Your task is to find the number of distinct possible values of $$$k$$$ across all such ways. Input The first line contains the integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The following $$$t$$$ lines contain three integers, $$$a$$$, $$$b$$$ and $$$l$$$ ($$$2 le a, b le 100$$$, $$$1 le l le 10^6$$$)xa0— description of a test case. Output Output $$$t$$$ lines, with the $$$i$$$-th ($$$1 le i le t$$$) line containing an integer, the answer to the $$$i$$$-th test case. Example Input 11 2 5 20 2 5 21 4 6 48 2 3 72 3 5 75 2 2 1024 3 7 83349 100 100 1000000 7 3 2 2 6 6 17 3 632043 Output 6 1 5 12 6 11 24 4 1 3 24 Note In the first test case, $$$a=2, b=5, l=20$$$. The possible values of $$$k$$$ (and corresponding $$$x,y$$$) are as follows: Choose $$$k = 1, x = 2, y = 1$$$. Then $$$k cdot a^x cdot b^y = 1 cdot 2^2 cdot 5^1 = 20 = l$$$. Choose $$$k = 2, x = 1, y = 1$$$. Then $$$k cdot a^x cdot b^y = 2 cdot 2^1 cdot 5^1 = 20 = l$$$. Choose $$$k = 4, x = 0, y = 1$$$. Then $$$k cdot a^x cdot b^y = 4 cdot 2^0 cdot 5^1 = 20 = l$$$. Choose $$$k = 5, x = 2, y = 0$$$. Then $$$k cdot a^x cdot b^y = 5 cdot 2^2 cdot 5^0 = 20 = l$$$. Choose $$$k = 10, x = 1, y = 0$$$. Then $$$k cdot a^x cdot b^y = 10 cdot 2^1 cdot 5^0 = 20 = l$$$. Choose $$$k = 20, x = 0, y = 0$$$. Then $$$k cdot a^x cdot b^y = 20 cdot 2^0 cdot 5^0 = 20 = l$$$. In the second test case, $$$a=2, b=5, l=21$$$. Note that $$$l = 21$$$ is not divisible by either $$$a = 2$$$ or $$$b = 5$$$. Therefore, we can only set $$$x = 0, y = 0$$$, which corresponds to $$$k = 21$$$. In the third test case, $$$a=4, b=6, l=48$$$. The possible values of $$$k$$$ (and corresponding $$$x,y$$$) are as follows: Choose $$$k = 2, x = 1, y = 1$$$. Then $$$k cdot a^x cdot b^y = 2 cdot 4^1 cdot 6^1 = 48 = l$$$. Choose $$$k = 3, x = 2, y = 0$$$. Then $$$k cdot a^x cdot b^y = 3 cdot 4^2 cdot 6^0 = 48 = l$$$. Choose $$$k = 8, x = 0, y = 1$$$. Then $$$k cdot a^x cdot b^y = 8 cdot 4^0 cdot 6^1 = 48 = l$$$. Choose $$$k = 12, x = 1, y = 0$$$. Then $$$k cdot a^x cdot b^y = 12 cdot 4^1 cdot 6^0 = 48 = l$$$. Choose $$$k = 48, x = 0, y = 0$$$. Then $$$k cdot a^x cdot b^y = 48 cdot 4^0 cdot 6^0 = 48 = l$$$.
1,100
true
false
true
false
false
false
true
false
false
false
685
1789A
Mocha likes arrays, and Serval gave her an array consisting of positive integers as a gift. Mocha thinks that for an array of positive integers $$$a$$$, it is good iff the =3$$$ is greater than its length $$$2$$$. $$$[1,2,4]$$$ is both good and beautiful, because all of its prefixes whose length is no less than $$$2$$$, which are $$$[1,2]$$$ and $$$[1,2,4]$$$, are both good. $$$[3,6,1]$$$ is good but not beautiful, because $$$[3,6]$$$ is not good. Now Mocha gives you the gift array $$$a$$$ of $$$n$$$ positive integers, and she wants to know whether array $$$a$$$ could become beautiful by reordering the elements in $$$a$$$. It is allowed to keep the array $$$a$$$ unchanged. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1leq tleq 500$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2leq nleq 100$$$) — the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq a_1,a_2,ldots,a_nleq 10^6$$$) — the elements of array $$$a$$$. Output For each test case, print Yes if it is possible to reorder the elements in $$$a$$$ to make it beautiful, and print No if not. You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). Example Input 6 2 3 6 3 1 2 4 3 3 6 1 3 15 35 21 4 35 10 35 14 5 1261 227821 143 4171 1941 Output No Yes Yes No Yes Yes Note In the first test case, neither $$$[3,6]$$$ nor $$$[6,3]$$$ are beautiful, so it's impossible to obtain a beautiful array by reordering the elements in $$$a$$$. In the second test case, $$$[1,2,4]$$$ is already beautiful. Keeping the array $$$a$$$ unchanged can obtain a beautiful array.
800
true
false
false
false
false
false
true
false
false
false
1,548
86C
Multidimensional spaces are completely out of style these days, unlike genetics problems" — thought physicist Woll and changed his subject of study to bioinformatics. Analysing results of sequencing he faced the following problem concerning DNA sequences. We will further think of a DNA sequence as an arbitrary string of uppercase letters "A", "C", "G" and "T" (of course, this is a simplified interpretation). Let _w_ be a long DNA sequence and _s_1,u2009_s_2,u2009...,u2009_s__m_ — collection of short DNA sequences. Let us say that the collection filters _w_ iff _w_ can be covered with the sequences from the collection. Certainly, substrings corresponding to the different positions of the string may intersect or even cover each other. More formally: denote by _w_ the length of _w_, let symbols of _w_ be numbered from 1 to _w_. Then for each position _i_ in _w_ there exist pair of indices _l_,u2009_r_ (1u2009≤u2009_l_u2009≤u2009_i_u2009≤u2009_r_u2009≤u2009_w_) such that the substring _w_[_l_xa0...xa0_r_] equals one of the elements _s_1,u2009_s_2,u2009...,u2009_s__m_ of the collection. Woll wants to calculate the number of DNA sequences of a given length filtered by a given collection, but he doesn't know how to deal with it. Help him! Your task is to find the number of different DNA sequences of length _n_ filtered by the collection {_s__i_}. Answer may appear very large, so output it modulo 1000000009. Input First line contains two integer numbers _n_ and _m_ (1u2009≤u2009_n_u2009≤u20091000,u20091u2009≤u2009_m_u2009≤u200910) — the length of the string and the number of sequences in the collection correspondently. Next _m_ lines contain the collection sequences _s__i_, one per line. Each _s__i_ is a nonempty string of length not greater than 10. All the strings consist of uppercase letters "A", "C", "G", "T". The collection may contain identical strings. Note In the first sample, a string has to be filtered by "A". Clearly, there is only one such string: "AA". In the second sample, there exist exactly two different strings satisfying the condition (see the pictures below).
2,500
false
false
false
true
false
false
false
false
false
false
9,539
455E
Problem - 455E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures *2900 No tag edit access → Contest materials u2009=u2009_a_
2,900
false
false
false
false
true
false
false
false
false
false
8,015
641A
Problem - 641A - Codeforces =============== xa0 ]( "VK Cup 2016 — Round 2 (announcement)") ") xa0— length of the strip. Next line contains a string of length _n_ which consists of characters "<" and ">" only, that provide the direction of the jump from the corresponding cell. Next line contains _n_ integers _d__i_ (1u2009≤u2009_d__i_u2009≤u2009109)xa0— the length of the jump from the _i_-th cell. Output Print "INFINITE" (without quotes) if grasshopper will continue his jumps forever. Otherwise print "FINITE" (without quotes). Examples Input 2 >< 1 2 Output FINITE Input 3 >>< 2 1 1 Output INFINITE Note In the first sample grasshopper starts from the first cell and jumps to the right on the next cell. When he is in the second cell he needs to jump two cells left so he will jump out of the strip. Second sample grasshopper path is 1 - 3 - 2 - 3 - 2 - 3 and so on. The path is infinite.
1,000
false
false
true
false
false
false
false
false
false
false
7,251
325A
Problem - 325A - Codeforces =============== xa0 . Your task is to determine if the rectangles form a square. In other words, determine if the set of points inside or on the border of at least one rectangle is precisely equal to the set of points inside or on the border of some square. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095). Next _n_ lines contain four integers each, describing a single rectangle: _x_1, _y_1, _x_2, _y_2 (0u2009≤u2009_x_1u2009<u2009_x_2u2009≤u200931400,u20090u2009≤u2009_y_1u2009<u2009_y_2u2009≤u200931400) — _x_1 and _x_2 are _x_-coordinates of the left and right edges of the rectangle, and _y_1 and _y_2 are _y_-coordinates of the bottom and top edges of the rectangle. No two rectangles overlap (that is, there are no points that belong to the interior of more than one rectangle). Output In a single line print "YES", if the given rectangles form a square, or "NO" otherwise. Examples Input 5 0 0 2 3 0 3 3 5 2 0 5 2 3 2 5 5 2 2 3 3 Output YES Input 4 0 0 2 3 0 3 3 5 2 0 5 2 3 2 5 5 Output NO
1,500
false
false
true
false
false
false
false
false
false
false
8,541
746A
Nikolay has _a_ lemons, _b_ apples and _c_ pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1:u20092:u20094. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruitsxa0— lemons, apples and pearsxa0— should be put in the compote as whole fruits. Your task is to determine the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. It is possible that Nikolay can't use any fruits, in this case print 0. Input The first line contains the positive integer _a_ (1u2009≤u2009_a_u2009≤u20091000)xa0— the number of lemons Nikolay has. The second line contains the positive integer _b_ (1u2009≤u2009_b_u2009≤u20091000)xa0— the number of apples Nikolay has. The third line contains the positive integer _c_ (1u2009≤u2009_c_u2009≤u20091000)xa0— the number of pears Nikolay has. Output Print the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. Note In the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1u2009+u20092u2009+u20094u2009=u20097. In the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3u2009+u20096u2009+u200912u2009=u200921. In the third example Nikolay don't have enough pears to cook any compote, so the answer is 0.
800
true
false
true
false
false
false
false
false
false
false
6,817