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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1925A | You are given two positive integers $$$n$$$ and $$$k$$$. Your task is to find a string $$$s$$$ such that all possible strings of length $$$n$$$ that can be formed using the first $$$k$$$ lowercase English alphabets occur as a subsequence of $$$s$$$. If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them. Note: A string $$$a$$$ is called a subsequence of another string $$$b$$$ if $$$a$$$ can be obtained by deleting some (possibly zero) characters from $$$b$$$ without changing the order of the remaining characters. Input The first line of input contains a single integer $$$t$$$ ($$$1leq tleq 676$$$) denoting the number of test cases. Each test case consists of a single line of input containing two integers $$$n$$$ ($$$1leq nleq 26$$$) and $$$k$$$ ($$$1leq kleq 26$$$). Output For each test case, print a single line containing a single string $$$s$$$ which satisfies the above property. If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them. Note For the first test case, there are two strings of length $$$1$$$ which can be formed using the first $$$2$$$ lowercase English alphabets, and they are present in $$$s$$$ as a subsequence as follows: $$$ exttt{a}: {color{red}{ exttt{a}}} exttt{b}$$$ $$$ exttt{b}: exttt{a}{color{red}{ exttt{b}}}$$$ For the second test case, there is only one string of length $$$2$$$ which can be formed using the first lowercase English alphabet, and it is present in $$$s$$$ as a subsequence as follows: $$$ exttt{aa}: {color{red}{ exttt{aa}}}$$$ For the third test case, there are $$$4$$$ strings of length $$$2$$$ which can be formed using the first $$$2$$$ lowercase English alphabets, and they are present in $$$s$$$ as a subsequence as follows: $$$ exttt{aa}: exttt{b}{color{red}{ exttt{aa}}} exttt{b}$$$ $$$ exttt{ab}: exttt{ba}{color{red}{ exttt{ab}}}$$$ $$$ exttt{ba}: {color{red}{ exttt{ba}}} exttt{ab}$$$ $$$ exttt{bb}: {color{red}{ exttt{b}}} exttt{aa}{color{red}{ exttt{b}}}$$$ For the fourth test case, there are $$$9$$$ strings of length $$$2$$$ which can be formed using the first $$$3$$$ lowercase English alphabets, and they are present in $$$s$$$ as a subsequence as follows: $$$ exttt{aa}: {color{red}{ exttt{a}}} exttt{bcb}{color{red}{ exttt{a}}} exttt{c}$$$ $$$ exttt{ab}: {color{red}{ exttt{ab}}} exttt{cbac}$$$ $$$ exttt{ac}: exttt{abcb}{color{red}{ exttt{ac}}}$$$ $$$ exttt{ba}: exttt{abc}{color{red}{ exttt{ba}}} exttt{c}$$$ $$$ exttt{bb}: exttt{a}{color{red}{ exttt{b}}} exttt{c}{color{red}{ exttt{b}}} exttt{ac}$$$ $$$ exttt{bc}: exttt{a}{color{red}{ exttt{bc}}} exttt{bac}$$$ $$$ exttt{ca}: exttt{ab}{color{red}{ exttt{c}}} exttt{b}{color{red}{ exttt{a}}} exttt{c}$$$ $$$ exttt{cb}: exttt{ab}{color{red}{ exttt{cb}}} exttt{ac}$$$ $$$ exttt{cc}: exttt{ab}{color{red}{ exttt{c}}} exttt{ba}{color{red}{ exttt{c}}}$$$ | 800 | false | true | false | false | false | true | false | false | false | false | 740 |
754E | Dasha is fond of challenging puzzles: Rubik's Cube 3u2009×u20093u2009×u20093, 4u2009×u20094u2009×u20094, 5u2009×u20095u2009×u20095 and so on. This time she has a cyclic table of size _n_u2009×u2009_m_, and each cell of the table contains a lowercase English letter. Each cell has coordinates (_i_,u2009_j_) (0u2009≤u2009_i_u2009<u2009_n_, 0u2009≤u2009_j_u2009<u2009_m_). The table is cyclic means that to the right of cell (_i_,u2009_j_) there is the cell , and to the down there is the cell . Dasha has a pattern as well. A pattern is a non-cyclic table of size _r_u2009×u2009_c_. Each cell is either a lowercase English letter or a question mark. Each cell has coordinates (_i_,u2009_j_) (0u2009≤u2009_i_u2009<u2009_r_, 0u2009≤u2009_j_u2009<u2009_c_). The goal of the puzzle is to find all the appearance positions of the pattern in the cyclic table. We say that the cell (_i_,u2009_j_) of cyclic table is an appearance position, if for every pair (_x_,u2009_y_) such that 0u2009≤u2009_x_u2009<u2009_r_ and 0u2009≤u2009_y_u2009<u2009_c_ one of the following conditions holds: There is a question mark in the cell (_x_,u2009_y_) of the pattern, or The cell of the cyclic table equals to the cell (_x_,u2009_y_) of the pattern. Dasha solved this puzzle in no time, as well as all the others she ever tried. Can you solve it?. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009400)xa0— the cyclic table sizes. Each of the next _n_ lines contains a string of _m_ lowercase English charactersxa0— the description of the cyclic table. The next line contains two integers _r_ and _c_ (1u2009≤u2009_r_,u2009_c_u2009≤u2009400)xa0— the sizes of the pattern. Each of the next _r_ lines contains a string of _c_ lowercase English letter and/or characters '?'xa0— the description of the pattern. Output Print _n_ lines. Each of the _n_ lines should contain _m_ characters. Each of the characters should equal '0' or '1'. The _j_-th character of the _i_-th (0-indexed) line should be equal to '1', in case the cell (_i_,u2009_j_) is an appearance position, otherwise it should be equal to '0'. Examples Input 5 7 qcezchs hhedywq wikywqy qckrqzt bqexcxz 3 2 ?? yw ?q Output 0000100 0001001 0000000 0000000 0000000 Input 10 10 fwtoayylhw yyaryyjawr ywrdzwhscy hnsyyxiphn bnjwzyyjvo kkjgseenwn gvmiflpcsy lxvkwrobwu wyybbcocyy yysijsvqry 2 2 ?? yy Output 1000100000 0000000001 0001000000 0000010000 0000000000 0000000000 0000000000 0100000010 1000000001 0000010000 Input 8 6 ibrgxl xqdcsg okbcgi tvpetc xgxxig igghzo lmlaza gpswzv 1 4 gx?? Output 000100 000001 000000 000000 010001 000000 000000 000000 | 2,600 | false | false | false | false | false | false | true | false | false | false | 6,778 |
1359D | Alice and Bob are playing yet another card game. This time the rules are the following. There are $$$n$$$ cards lying in a row in front of them. The $$$i$$$-th card has value $$$a_i$$$. First, Alice chooses a non-empty consecutive segment of cards $$$[l; r]$$$ ($$$l le r$$$). After that Bob removes a single card $$$j$$$ from that segment $$$(l le j le r)$$$. The score of the game is the total value of the remaining cards on the segment $$$(a_l + a_{l + 1} + dots + a_{j - 1} + a_{j + 1} + dots + a_{r - 1} + a_r)$$$. In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is $$$0$$$. Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible. What segment should Alice choose so that the score is maximum possible? Output the maximum score. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of cards. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-30 le a_i le 30$$$) — the values on the cards. Output Print a single integer — the final score of the game. Examples Input 8 5 2 5 3 -30 -30 6 9 Note In the first example Alice chooses a segment $$$[1;5]$$$ — the entire row of cards. Bob removes card $$$3$$$ with the value $$$10$$$ from the segment. Thus, the final score is $$$5 + (-2) + (-1) + 4 = 6$$$. In the second example Alice chooses a segment $$$[1;4]$$$, so that Bob removes either card $$$1$$$ or $$$3$$$ with the value $$$5$$$, making the answer $$$5 + 2 + 3 = 10$$$. In the third example Alice can choose any of the segments of length $$$1$$$: $$$[1;1]$$$, $$$[2;2]$$$ or $$$[3;3]$$$. Bob removes the only card, so the score is $$$0$$$. If Alice chooses some other segment then the answer will be less than $$$0$$$. | 2,000 | false | false | true | true | true | false | false | false | false | false | 3,888 |
1758F | A binary string is a string where every character is $$$ exttt{0}$$$ or $$$ exttt{1}$$$. Call a binary string decent if it has an equal number of $$$ exttt{0}$$$s and $$$ exttt{1}$$$s. Initially, you have an infinite binary string $$$t$$$ whose characters are all $$$ exttt{0}$$$s. You are given a sequence $$$a$$$ of $$$n$$$ updates, where $$$a_i$$$ indicates that the character at index $$$a_i$$$ will be flipped ($$$ exttt{0} leftrightarrow exttt{1}$$$). You need to keep and modify after each update a set $$$S$$$ of disjoint ranges such that: for each range $$$[l,r]$$$, the substring $$$t_l dots t_r$$$ is a decent binary string, and for all indices $$$i$$$ such that $$$t_i = exttt{1}$$$, there exists $$$[l,r]$$$ in $$$S$$$ such that $$$l leq i leq r$$$. You only need to output the ranges that are added to or removed from $$$S$$$ after each update. You can only add or remove ranges from $$$S$$$ at most $$$mathbf{10^6}$$$ times. More formally, let $$$S_i$$$ be the set of ranges after the $$$i$$$-th update, where $$$S_0 = varnothing$$$ (the empty set). Define $$$X_i$$$ to be the set of ranges removed after update $$$i$$$, and $$$Y_i$$$ to be the set of ranges added after update $$$i$$$. Then for $$$1 leq i leq n$$$, $$$S_i = (S_{i - 1} setminus X_i) cup Y_i$$$. The following should hold for all $$$1 leq i leq n$$$: $$$forall a,b in S_i, (a eq b) ightarrow (a cap b = varnothing)$$$; $$$X_i subseteq S_{i - 1}$$$; $$$(S_{i-1} setminus X_i) cap Y_i = varnothing$$$; $$$displaystylesum_{i = 1}^n {(X_i + Y_i)} leq 10^6$$$. Output After the $$$i$$$-th update, first output a single integer $$$x_i$$$ — the number of ranges to be removed from $$$S$$$ after update $$$i$$$. In the following $$$x_i$$$ lines, output two integers $$$l$$$ and $$$r$$$ ($$$1 leq l < r leq 10^6$$$), which denotes that the range $$$[l,r]$$$ should be removed from $$$S$$$. Each of these ranges should be distinct and be part of $$$S$$$. In the next line, output a single integer $$$y_i$$$ — the number of ranges to be added to $$$S$$$ after update $$$i$$$. In the following $$$y_i$$$ lines, output two integers $$$l$$$ and $$$r$$$ ($$$1 leq l < r leq 10^6$$$), which denotes that the range $$$[l,r]$$$ should be added to $$$S$$$. Each of these ranges should be distinct and not be part of $$$S$$$. The total number of removals and additions across all updates must not exceed $$$mathbf{10^6}$$$. After processing the removals and additions for each update, all the ranges in $$$S$$$ should be disjoint and cover all ones. It can be proven that an answer always exists under these constraints. Note Line breaks are provided in the sample only for the sake of clarity, and you don't need to print them in your output. After the first update, the set of indices where $$$a_i = 1$$$ is $$${1}$$$. The interval $$$[1, 2]$$$ is added, so $$$S_1 = {[1, 2]}$$$, which has one $$$ exttt{0}$$$ and one $$$ exttt{1}$$$. After the second update, the set of indices where $$$a_i = 1$$$ is $$${1, 6}$$$. The interval $$$[5, 6]$$$ is added, so $$$S_2 = {[1, 2], [5, 6]}$$$, each of which has one $$$ exttt{0}$$$ and one $$$ exttt{1}$$$. After the third update, the set of indices where $$$a_i = 1$$$ is $$${1, 5, 6}$$$. The interval $$$[5, 6]$$$ is removed and the intervals $$$[4, 5]$$$ and $$$[6, 7]$$$ are added, so $$$S_3 = {[1, 2], [4, 5], [6, 7]}$$$, each of which has one $$$ exttt{0}$$$ and one $$$ exttt{1}$$$. After the fourth update, the set of indices where $$$a_i = 1$$$ is $$${1, 6}$$$. The interval $$$[4, 5]$$$ is removed, so $$$S_4 = {[1, 2], [6, 7]}$$$, each of which has one $$$ exttt{0}$$$ and one $$$ exttt{1}$$$. After the fifth update, the set of indices where $$$a_i = 1$$$ is $$${1}$$$. The interval $$$[6, 7]$$$ is removed, so $$$S_5 = {[1, 2]}$$$, which has one $$$ exttt{0}$$$ and one $$$ exttt{1}$$$. | 3,000 | false | false | false | false | true | true | false | false | false | false | 1,763 |
1957A | You are given $$$n$$$ sticks of lengths $$$a_1, a_2, ldots, a_n$$$. Find the maximum number of regular (equal-sided) polygons you can construct simultaneously, such that: Each side of a polygon is formed by exactly one stick. No stick is used in more than $$$1$$$ polygon. Note: Sticks cannot be broken. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the number of sticks available. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 100$$$)xa0— the stick lengths. Output For each test case, output a single integer on a new linexa0— the maximum number of regular (equal-sided) polygons you can make simultaneously from the sticks available. Example Input 4 1 1 2 1 1 6 2 2 3 3 3 3 9 4 2 2 2 2 4 2 4 4 Note In the first test case, we only have one stick, hence we can't form any polygon. In the second test case, the two sticks aren't enough to form a polygon either. In the third test case, we can use the $$$4$$$ sticks of length $$$3$$$ to create a square. In the fourth test case, we can make a pentagon with side length $$$2$$$, and a square of side length $$$4$$$. | 800 | false | true | false | false | false | true | false | false | false | false | 533 |
2014B | In Sherwood, the trees are our shelter, and we are all children of the forest. The Major Oak in Sherwood is known for its majestic foliage, which provided shelter to Robin Hood and his band of merry men and women. The Major Oak grows $$$i^i$$$ new leaves in the $$$i$$$-th year. It starts with $$$1$$$ leaf in year $$$1$$$. Leaves last for $$$k$$$ years on the tree. In other words, leaves grown in year $$$i$$$ last between years $$$i$$$ and $$$i+k-1$$$ inclusive. Robin considers even numbers lucky. Help Robin determine whether the Major Oak will have an even number of leaves in year $$$n$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each test case consists of two integers $$$n$$$, $$$k$$$ ($$$1 le n le 10^9$$$, $$$1 le k le n$$$)xa0— the requested year and the number of years during which the leaves remain. Output For each test case, output one line, "YES" if in year $$$n$$$ the Major Oak will have an even number of leaves and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Note In the first test case, there is only $$$1$$$ leaf. In the second test case, $$$k=1$$$, so in the $$$2$$$-nd year there will be $$$2^2=4$$$ leaves. In the third test case, $$$k=2$$$, so in the $$$2$$$-nd year there will be $$$1+2^2=5$$$ leaves. In the fourth test case, $$$k=2$$$, so in the $$$3$$$-rd year there will be $$$2^2 + 3^3 = 4 + 27 = 31$$$ leaves. | 800 | true | false | false | false | false | false | false | false | false | false | 170 |
16D | The main server of Gomble company received a log of one top-secret process, the name of which can't be revealed. The log was written in the following format: «[date:time]: message», where for each «[date:time]» value existed not more than 10 lines. All the files were encoded in a very complicated manner, and only one programmer — Alex — managed to decode them. The code was so complicated that Alex needed four weeks to decode it. Right after the decoding process was finished, all the files were deleted. But after the files deletion, Alex noticed that he saved the recordings in format «[time]: message». So, information about the dates was lost. However, as the lines were added into the log in chronological order, it's not difficult to say if the recordings could appear during one day or not. It is possible also to find the minimum amount of days during which the log was written. So, to make up for his mistake Alex has to find the minimum amount of days covered by the log. Note that Alex doesn't have to find the minimum amount of days between the beginning and the end of the logging, he has to find the minimum amount of dates in which records could be done. (See Sample test 2 for further clarifications). We should remind you that the process made not more than 10 recordings in a minute. Consider that a midnight belongs to coming day. Input The first input line contains number _n_ (1u2009≤u2009_n_u2009≤u2009100). The following _n_ lines contain recordings in format «[time]: message», where time is given in format «hh:mm x.m.». For hh two-digit numbers from 01 to 12 are used, for mm two-digit numbers from 00 to 59 are used, and x is either character «a» or character «p». A message is a non-empty sequence of Latin letters and/or spaces, it doesn't start or end with a space. The length of each message doesn't exceed 20. Output Output one number — the minimum amount of days covered by the log. Examples Input 5 [05:00 a.m.]: Server is started [05:00 a.m.]: Rescan initialized [01:13 p.m.]: Request processed [01:10 p.m.]: Request processed [11:40 p.m.]: Rescan completed Input 3 [09:00 a.m.]: User logged in [08:00 a.m.]: User logged in [07:00 a.m.]: User logged in Note Formally the 12-hour time format is described at: problem authors recommend you to look through these descriptions before you start with the problem. | 1,900 | false | false | true | false | false | false | false | false | false | false | 9,912 |
1866I | Given a chessboard of size $$$N imes M$$$ ($$$N$$$ rows and $$$M$$$ columns). Each row is numbered from $$$1$$$ to $$$N$$$ from top to bottom and each column is numbered from $$$1$$$ to $$$M$$$ from left to right. The tile in row $$$r$$$ and column $$$c$$$ is denoted as $$$(r,c)$$$. There exists $$$K$$$ distinct special tiles on the chessboard with the $$$i$$$-th special tile being tile $$$(X_i,Y_i)$$$. It is guaranteed that tile $$$(1,1)$$$ is not a special tile. A new chess piece has been invented, which is the castle. The castle moves similarly to a rook in regular chess, but slightly differently. In one move, a castle that is on some tile can only move to another tile in the same row or in the same column, but only in the right direction or the down direction. Formally, in one move, the castle on tile $$$(r,c)$$$ can only move to tile $$$(r',c')$$$ if and only if one of the following two conditions is satisfied: $$$r'=r$$$ and $$$c'>c$$$. $$$c'=c$$$ and $$$r'>r$$$. Chaneka and Bhinneka will play a game. In the beginning of the game, there is a castle in tile $$$(1,1)$$$. The two players will play alternatingly with Chaneka going first. In one turn, the player on that turn must move the castle following the movement rules of the castle. If a player moves the castle to a special tile on her turn, then that player wins the game and the game ends. If on a turn, the castle cannot be moved, the player on that turn loses and the game ends. Given the size of the board and the locations of each special tile. Determine the winner of this game if Chaneka and Bhinneka plays optimally. Input The first line contains three integers $$$N$$$, $$$M$$$, and $$$K$$$ ($$$1 leq N,M leq 2 cdot 10^5$$$; $$$0 leq K leq min(N imes M - 1, 2cdot10^5)$$$) — the size of the chessboard and the number of special tiles. The $$$i$$$-th of the next $$$K$$$ lines contains two integers $$$X_i$$$ and $$$Y_i$$$ ($$$1leq X_ileq N$$$; $$$1leq Y_ileq M$$$; $$$(X_i, Y_i) eq (1,1)$$$) — the location of each special tile. The special tiles are pairwise distinct. Output Output Chaneka if Chaneka is the winner, output Bhinneka if Bhinneka is the winner. Note In the first example, the following is an illustration of the chessboard in the beginning of the game. Chaneka can move the castle to special tile $$$(1,3)$$$ or $$$(1,5)$$$ directly on her first turn. Therefore, Chaneka is the winner. In the second example, the following is an illustration of the chessboard in the beginning of the game. Chaneka can only move the castle to tile $$$(1, 2)$$$ or $$$(2, 1)$$$ on her first turn. Whatever Chaneka does, Bhinneka will be able to directly move the castle to tile $$$(2, 2)$$$. After that, on Chaneka's turn, she cannot move the castle, so Chaneka loses. Therefore, Bhinneka is the winner. | 2,300 | false | false | false | true | false | false | false | false | false | false | 1,080 |
407A | Problem - 407A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force geometry implementation math *1600 No tag edit access → Contest materials , separated by a single space. Output In the first line print either "YES" or "NO" (without the quotes) depending on whether the required location exists. If it does, print in the next three lines three pairs of integers — the coordinates of the triangle vertices, one pair per line. The coordinates must be integers, not exceeding 109 in their absolute value. Examples Input 1 1 Output NO Input 5 5 Output YES 2 1 5 5 -2 4 Input 5 10 Output YES -10 4 -2 -2 1 2 | 1,600 | true | false | true | false | false | false | true | false | false | false | 8,198 |
626F | There are _n_ students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the _i_-th student _a__i_ minutes to finish his/her independent piece. If students work at different paces, it can be frustrating for the faster students and stressful for the slower ones. In particular, the imbalance of a group is defined as the maximum _a__i_ in the group minus the minimum _a__i_ in the group. Note that a group containing a single student has an imbalance of 0. How many ways are there for the students to divide into groups so that the total imbalance of all groups is at most _k_? Two divisions are considered distinct if there exists a pair of students who work in the same group in one division but different groups in the other. Input The first line contains two space-separated integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009200, 0u2009≤u2009_k_u2009≤u20091000)xa0— the number of students and the maximum total imbalance allowed, respectively. The second line contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009500)xa0— the time it takes the _i_-th student to complete his/her independent piece of work. Output Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109u2009+u20097. Note In the first sample, we have three options: The first and second students form a group, and the third student forms a group. Total imbalance is 2u2009+u20090u2009=u20092. The first student forms a group, and the second and third students form a group. Total imbalance is 0u2009+u20091u2009=u20091. All three students form their own groups. Total imbalance is 0. In the third sample, the total imbalance must be 0, so each student must work individually. | 2,400 | false | false | false | true | false | false | false | false | false | false | 7,323 |
1392C | Omkar is building a waterslide in his water park, and he needs your help to ensure that he does it as efficiently as possible. Omkar currently has $$$n$$$ supports arranged in a line, the $$$i$$$-th of which has height $$$a_i$$$. Omkar wants to build his waterslide from the right to the left, so his supports must be nondecreasing in height in order to support the waterslide. In $$$1$$$ operation, Omkar can do the following: take any contiguous subsegment of supports which is nondecreasing by heights and add $$$1$$$ to each of their heights. Help Omkar find the minimum number of operations he needs to perform to make his supports able to support his waterslide! An array $$$b$$$ is a subsegment of an array $$$c$$$ if $$$b$$$ can be obtained from $$$c$$$ by deletion of several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end. An array $$$b_1, b_2, dots, b_n$$$ is called nondecreasing if $$$b_ile b_{i+1}$$$ for every $$$i$$$ from $$$1$$$ to $$$n-1$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 100$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of supports Omkar has. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ $$$(0 leq a_{i} leq 10^9)$$$xa0— the heights of the supports. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the minimum number of operations Omkar needs to perform to make his supports able to support his waterslide. Example Input 3 4 5 3 2 5 5 1 2 3 5 3 3 1 1 1 Note The subarray with which Omkar performs the operation is bolded. In the first test case: First operation: $$$[5, 3, extbf{2}, 5] o [5, 3, extbf{3}, 5]$$$ Second operation: $$$[5, extbf{3}, extbf{3}, 5] o [5, extbf{4}, extbf{4}, 5]$$$ Third operation: $$$[5, extbf{4}, extbf{4}, 5] o [5, extbf{5}, extbf{5}, 5]$$$ In the third test case, the array is already nondecreasing, so Omkar does $$$0$$$ operations. | 1,200 | false | true | true | false | false | false | false | false | false | false | 3,714 |
274B | A tree is a graph with _n_ vertices and exactly _n_u2009-u20091 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices. A subtree of a tree _T_ is a tree with both vertices and edges as subsets of vertices and edges of _T_. You're given a tree with _n_ vertices. Consider its vertices numbered with integers from 1 to _n_. Additionally an integer is written on every vertex of this tree. Initially the integer written on the _i_-th vertex is equal to _v__i_. In one move you can apply the following operation: 1. Select the subtree of the given tree that includes the vertex with number 1. 2. Increase (or decrease) by one all the integers which are written on the vertices of that subtree. Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero. Input The first line of the input contains _n_ (1u2009≤u2009_n_u2009≤u2009105). Each of the next _n_u2009-u20091 lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa0_a__i_u2009≠u2009_b__i_) indicating there's an edge between vertices _a__i_ and _b__i_. It's guaranteed that the input graph is a tree. The last line of the input contains a list of _n_ space-separated integers _v_1,u2009_v_2,u2009...,u2009_v__n_ (_v__i_u2009≤u2009109). Output Print the minimum number of operations needed to solve the task. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | 1,800 | false | true | false | true | false | false | false | false | false | false | 8,738 |
48E | Once upon a time in a kingdom far, far away… Okay, let’s start at the point where Ivan the Fool met Gorynych the Dragon. Ivan took out his magic sword and the battle began. First Gorynych had _h_ heads and _t_ tails. With each strike of the sword Ivan can either cut off several heads (from 1 to _n_, but not more than Gorynych has at the moment), or several tails (from 1 to _m_, but not more than Gorynych has at the moment). At the same time, horrible though it seems, Gorynych the Dragon can also grow new heads and tails. And the number of growing heads and tails is determined uniquely by the number of heads or tails cut by the current strike. When the total number of heads and tails exceeds _R_, Gorynych the Dragon strikes its final blow and destroys Ivan the Fool. That’s why Ivan aims to cut off all the dragon’s heads and tails as quickly as possible and win. The events can also develop in a third way: neither of the opponents can win over the other one and they will continue fighting forever. The tale goes like this; easy to say, hard to do. Your task is to write a program that will determine the battle’s outcome. Consider that Ivan strikes consecutively. After each blow Gorynych grows a number of new heads and tails depending on the number of cut ones. Gorynych the Dragon is defeated if after the blow he loses all his heads and tails and can’t grow new ones. Ivan fights in the optimal way (fools are lucky), i.e. if Ivan can win, he wins having struck the least number of blows; if it is impossible to defeat Gorynych, but is possible to resist him for an infinitely long period of time, then that’s the strategy Ivan chooses; if Gorynych wins in any case, Ivan aims to resist him for as long as possible. Input The first line contains three integers _h_, _t_ and _R_ (0u2009≤u2009_h_,u2009_t_,u2009_R_u2009≤u2009200, 0u2009<u2009_h_u2009+u2009_t_u2009≤u2009_R_) which represent the initial numbers of Gorynych’s heads and tails and the largest total number of heads and tails with which Gorynych the Dragon does not yet attack. The next line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009200). The next _n_ contain pairs of non-negative numbers "_h__i_ _t__i_" which represent the number of heads and the number of tails correspondingly, that will grow if Gorynych has _i_ heads (1u2009≤u2009_i_u2009≤u2009_n_) cut. The next line contains an integer _m_ (1u2009≤u2009_m_u2009≤u2009200) and then — the description of Gorynych’s behavior when his tails are cut off in the format identical to the one described above. All the numbers in the input file do not exceed 200. Output Print "Ivan" (without quotes) in the first line if Ivan wins, or "Zmey" (that means a dragon in Russian) if Gorynych the Dragon wins. In the second line print a single integer which represents the number of blows Ivan makes. If the battle will continue forever, print in the first line "Draw". Examples Input 2 2 4 2 1 0 0 1 3 0 1 0 1 0 0 | 2,100 | false | false | false | true | false | false | false | false | false | true | 9,733 |
1592A | One day, Ahmed_Hossam went to Hemose and said "Let's solve a gym contest!". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him? There is an Agent in Valorant, and he has $$$n$$$ weapons. The $$$i$$$-th weapon has a damage value $$$a_i$$$, and the Agent will face an enemy whose health value is $$$H$$$. The Agent will perform one or more moves until the enemy dies. In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to $$$0$$$. However, not everything is so easy: the Agent can't choose the same weapon for $$$2$$$ times in a row. What is the minimum number of times that the Agent will need to use the weapons to kill the enemy? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 leq t leq 10^5)$$$. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$H$$$ $$$(2 leq n leq 10^3, 1 leq H leq 10^9)$$$ — the number of available weapons and the initial health value of the enemy. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ $$$(1 leq a_i leq 10^9)$$$ — the damage values of the weapons. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integer — the minimum number of times that the Agent will have to use the weapons to kill the enemy. Example Input 3 2 4 3 7 2 6 4 2 3 11 2 1 7 Note In the first test case, the Agent can use the second weapon, making health value of the enemy equal to $$$4-7=-3$$$. $$$-3 le 0$$$, so the enemy is dead, and using weapon $$$1$$$ time was enough. In the second test case, the Agent can use the first weapon first, and then the second one. After this, the health of enemy will drop to $$$6-4-2 = 0$$$, meaning he would be killed after using weapons $$$2$$$ times. In the third test case, the Agent can use the weapons in order (third, first, third), decreasing the health value of enemy to $$$11 - 7 - 2 - 7 = -5$$$ after using the weapons $$$3$$$ times. Note that we can't kill the enemy by using the third weapon twice, as even though $$$11-7-7<0$$$, it's not allowed to use the same weapon twice in a row. | 800 | true | true | false | false | false | false | false | true | true | false | 2,693 |
1850C | On an $$$8 imes 8$$$ grid of dots, a word consisting of lowercase Latin letters is written vertically in one column, from top to bottom. What is it? Input The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Each test case consists of $$$8$$$ lines, each containing $$$8$$$ characters. Each character in the grid is either $$$ exttt{.}$$$ (representing a dot) or a lowercase Latin letter ($$$ exttt{a}$$$–$$$ exttt{z}$$$). The word lies entirely in a single column and is continuous from the beginning to the ending (without gaps). See the sample input for better understanding. Output For each test case, output a single line containing the word made up of lowercase Latin letters ($$$ exttt{a}$$$–$$$ exttt{z}$$$) that is written vertically in one column from top to bottom. Example Input 5 ........ ........ ........ ........ ...i.... ........ ........ ........ ........ .l...... .o...... .s...... .t...... ........ ........ ........ ........ ........ ........ ........ ......t. ......h. ......e. ........ ........ ........ ........ ........ .......g .......a .......m .......e a....... a....... a....... a....... a....... a....... a....... a....... Output i lost the game aaaaaaaa | 800 | false | false | true | false | false | false | false | false | false | false | 1,180 |
1311F | There are $$$n$$$ points on a coordinate axis $$$OX$$$. The $$$i$$$-th point is located at the integer point $$$x_i$$$ and has a speed $$$v_i$$$. It is guaranteed that no two points occupy the same coordinate. All $$$n$$$ points move with the constant speed, the coordinate of the $$$i$$$-th point at the moment $$$t$$$ ($$$t$$$ can be non-integer) is calculated as $$$x_i + t cdot v_i$$$. Consider two points $$$i$$$ and $$$j$$$. Let $$$d(i, j)$$$ be the minimum possible distance between these two points over any possible moments of time (even non-integer). It means that if two points $$$i$$$ and $$$j$$$ coincide at some moment, the value $$$d(i, j)$$$ will be $$$0$$$. Your task is to calculate the value $$$sumlimits_{1 le i < j le n}$$$ $$$d(i, j)$$$ (the sum of minimum distances over all pairs of points). Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of points. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 le x_i le 10^8$$$), where $$$x_i$$$ is the initial coordinate of the $$$i$$$-th point. It is guaranteed that all $$$x_i$$$ are distinct. The third line of the input contains $$$n$$$ integers $$$v_1, v_2, dots, v_n$$$ ($$$-10^8 le v_i le 10^8$$$), where $$$v_i$$$ is the speed of the $$$i$$$-th point. Output Print one integer — the value $$$sumlimits_{1 le i < j le n}$$$ $$$d(i, j)$$$ (the sum of minimum distances over all pairs of points). Examples Input 5 2 1 4 3 5 2 2 2 3 4 | 1,900 | false | false | true | false | true | false | false | false | true | false | 4,138 |
776E | The Holmes children are fighting over who amongst them is the cleverest. Mycroft asked Sherlock and Eurus to find value of _f_(_n_), where _f_(1)u2009=u20091 and for _n_u2009≥u20092, _f_(_n_) is the number of distinct ordered positive integer pairs (_x_,u2009_y_) that satisfy _x_u2009+u2009_y_u2009=u2009_n_ and _gcd_(_x_,u2009_y_)u2009=u20091. The integer _gcd_(_a_,u2009_b_) is the greatest common divisor of _a_ and _b_. Sherlock said that solving this was child's play and asked Mycroft to instead get the value of . Summation is done over all positive integers _d_ that divide _n_. Eurus was quietly observing all this and finally came up with her problem to astonish both Sherlock and Mycroft. She defined a _k_-composite function _F__k_(_n_) recursively as follows: She wants them to tell the value of _F__k_(_n_) modulo 1000000007. Input A single line of input contains two space separated integers _n_ (1u2009≤u2009_n_u2009≤u20091012) and _k_ (1u2009≤u2009_k_u2009≤u20091012) indicating that Eurus asks Sherlock and Mycroft to find the value of _F__k_(_n_) modulo 1000000007. Output Output a single integerxa0— the value of _F__k_(_n_) modulo 1000000007. Note In the first case, there are 6 distinct ordered pairs (1,u20096), (2,u20095), (3,u20094), (4,u20093), (5,u20092) and (6,u20091) satisfying _x_u2009+u2009_y_u2009=u20097 and _gcd_(_x_,u2009_y_)u2009=u20091. Hence, _f_(7)u2009=u20096. So, _F_1(7)u2009=u2009_f_(_g_(7))u2009=u2009_f_(_f_(7)u2009+u2009_f_(1))u2009=u2009_f_(6u2009+u20091)u2009=u2009_f_(7)u2009=u20096. | 2,100 | true | false | false | false | false | false | false | false | false | false | 6,664 |
1738G | You are given an $$$n imes n$$$ grid. We write $$$(i, j)$$$ to denote the cell in the $$$i$$$-th row and $$$j$$$-th column. For each cell, you are told whether yon can delete it or not. Given an integer $$$k$$$, you are asked to delete exactly $$$(n-k+1)^2$$$ cells from the grid such that the following condition holds. You cannot find $$$k$$$ not deleted cells $$$(x_1, y_1), (x_2, y_2), dots, (x_k, y_k)$$$ that are strictly increasing, i.e., $$$x_i < x_{i+1}$$$ and $$$y_i < y_{i+1}$$$ for all $$$1 leq i < k$$$. Your task is to find a solution, or report that it is impossible. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^5$$$) — the number of test cases. The following lines contain the description of each test case. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq k leq n leq 1000$$$). Then $$$n$$$ lines follow. The $$$i$$$-th line contains a binary string $$$s_i$$$ of length $$$n$$$. The $$$j$$$-th character of $$$s_i$$$ is 1 if you can delete cell $$$(i, j)$$$, and 0 otherwise. It's guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, if there is no way to delete exactly $$$(n-k+1)^2$$$ cells to meet the condition, output "NO" (without quotes). Otherwise, output "YES" (without quotes). Then, output $$$n$$$ lines. The $$$i$$$-th line should contain a binary string $$$t_i$$$ of length $$$n$$$. The $$$j$$$-th character of $$$t_i$$$ is 0 if cell $$$(i, j)$$$ is deleted, and 1 otherwise. If there are multiple solutions, you can output any of them. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Input 4 2 2 10 01 4 3 1110 0101 1010 0111 5 5 01111 10111 11011 11101 11110 5 2 10000 01111 01111 01111 01111 Output YES 01 11 YES 0011 1111 1111 1100 NO YES 01111 11000 10000 10000 10000 Note For the first test case, you only have to delete cell $$$(1, 1)$$$. For the second test case, you could choose to delete cells $$$(1,1)$$$, $$$(1,2)$$$, $$$(4,3)$$$ and $$$(4,4)$$$. For the third test case, it is no solution because the cells in the diagonal will always form a strictly increasing sequence of length $$$5$$$. | 2,900 | true | true | false | true | false | true | false | false | false | false | 1,855 |
300C | Problem - 300C - 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 combinatorics *1800 No tag edit access → Contest materials ") ") . A number's length is the number of digits in its decimal representation without leading zeroes. Input The first line contains three integers: _a_, _b_, _n_ (1u2009≤u2009_a_u2009<u2009_b_u2009≤u20099,u20091u2009≤u2009_n_u2009≤u2009106). Output Print a single integer — the answer to the problem modulo 1000000007 (109u2009+u20097). Examples Input 1 3 3 Output 1 Input 2 3 10 Output 165 | 1,800 | false | false | false | false | false | false | true | false | false | false | 8,633 |
1512B | There is a square field of size $$$n imes n$$$ in which two cells are marked. These cells can be in the same row or column. You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes. For example, if $$$n=4$$$ and a rectangular field looks like this (there are asterisks in the marked cells): $$$$$$ begin{matrix} . & . & & . . & . & . & . & . & . & . . & . & . & . end{matrix} $$$$$$ Then you can mark two more cells as follows $$$$$$ begin{matrix} & . & & . . & . & . & . & . & & . . & . & . & . end{matrix} $$$$$$ If there are several possible solutions, then print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 400$$$). Then $$$t$$$ test cases follow. The first row of each test case contains a single integer $$$n$$$ ($$$2 le n le 400$$$)xa0— the number of rows and columns in the table. The following $$$n$$$ lines each contain $$$n$$$ characters '.' or '*' denoting empty and marked cells, respectively. It is guaranteed that the sums of $$$n$$$ for all test cases do not exceed $$$400$$$. It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column. It is guaranteed that the solution exists. Output For each test case, output $$$n$$$ rows of $$$n$$$ charactersxa0— a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them. Example Input 6 4 ..*. .... *... .... 2 *. .* 2 .* .* 3 *.* ... ... 5 ..... ..*.. ..... .*... ..... 4 .... .... *... *... Output *.*. .... *.*. .... ** ** ** ** *.* *.* ... ..... .**.. ..... .**.. ..... .... .... **.. **.. | 800 | false | false | true | false | false | false | false | false | false | false | 3,108 |
667A | A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition — when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do. Today everything had changed quietly. You went on the street with a cup contained water, your favorite drink. In a moment when you were drinking a water you noticed that the process became quite long: the cup still contained water because of rain. You decided to make a formal model of what was happening and to find if it was possible to drink all water in that situation. Thus, your cup is a cylinder with diameter equals _d_ centimeters. Initial level of water in cup equals _h_ centimeters from the bottom. You drink a water with a speed equals _v_ milliliters per second. But rain goes with such speed that if you do not drink a water from the cup, the level of water increases on _e_ centimeters per second. The process of drinking water from the cup and the addition of rain to the cup goes evenly and continuously. Find the time needed to make the cup empty or find that it will never happen. It is guaranteed that if it is possible to drink all water, it will happen not later than after 104 seconds. Note one milliliter equals to one cubic centimeter. Input The only line of the input contains four integer numbers _d_,u2009_h_,u2009_v_,u2009_e_ (1u2009≤u2009_d_,u2009_h_,u2009_v_,u2009_e_u2009≤u2009104), where: _d_ — the diameter of your cylindrical cup, _h_ — the initial level of water in the cup, _v_ — the speed of drinking process from the cup in milliliters per second, _e_ — the growth of water because of rain if you do not drink from the cup. Output If it is impossible to make the cup empty, print "NO" (without quotes). Otherwise print "YES" (without quotes) in the first line. In the second line print a real number — time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20094. It is guaranteed that if the answer exists, it doesn't exceed 104. Note In the first example the water fills the cup faster than you can drink from it. In the second example area of the cup's bottom equals to , thus we can conclude that you decrease the level of water by centimeters per second. At the same time water level increases by 1 centimeter per second due to rain. Thus, cup will be empty in seconds. | 1,100 | true | false | false | false | false | false | false | false | false | false | 7,150 |
2018F2 | This is the medium version of the problem. In the three versions, the constraints on $$$n$$$ and the time limit are different. You can make hacks only if all the versions of the problem are solved. This is the statement of Problem D1B: There are $$$n$$$ cities in a row, numbered $$$1, 2, ldots, n$$$ left to right. At time $$$1$$$, you conquer exactly one city, called the starting city. At time $$$2, 3, ldots, n$$$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $$$i$$$, you conquer city $$$i$$$ at a time no later than $$$a_i$$$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each $$$0 leq k leq n$$$, count the number of arrays of positive integers $$$a_1, a_2, ldots, a_n$$$ such that $$$1 leq a_i leq n$$$ for each $$$1 leq i leq n$$$; the answer to Problem D1B is $$$k$$$. The answer can be very large, so you have to calculate it modulo a given prime $$$p$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The only line of each test case contains two integers $$$n$$$, $$$p$$$ ($$$1 le n le 500$$$, $$$10^8 leq p leq 10^9$$$, $$$p$$$ is prime)xa0— the number of cities and the modulo. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output For each test case, output $$$n+1$$$ integers: the $$$i$$$-th integer should be the number of arrays that satisfy the conditions for $$$k = i-1$$$. Example Input 11 1 998244353 2 998244353 3 998244353 4 998244353 5 998244353 6 998244353 7 998244353 8 998244353 9 998244353 10 102275857 10 999662017 Output 0 1 1 2 1 14 7 4 2 183 34 19 16 4 2624 209 112 120 48 12 42605 1546 793 992 468 216 36 785910 13327 6556 9190 4672 2880 864 144 16382863 130922 61939 94992 50100 36960 14256 4608 576 382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 Note In the first test case, arrays with $$$1$$$ good starting city: $$$[1]$$$. In the second test case, arrays with $$$0$$$ good starting cities: $$$[1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2]$$$, $$$[2, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2]$$$. In the third test case, arrays with $$$0$$$ good starting cities: $$$[1, 1, 1]$$$, $$$[1, 1, 2]$$$, $$$[1, 1, 3]$$$, $$$[1, 2, 1]$$$, $$$[1, 2, 2]$$$, $$$[1, 3, 1]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 1]$$$, $$$[2, 1, 2]$$$, $$$[2, 2, 1]$$$, $$$[2, 2, 2]$$$, $$$[2, 3, 1]$$$, $$$[2, 3, 2]$$$, $$$[3, 1, 1]$$$; arrays with $$$1$$$ good starting city: $$$[1, 2, 3]$$$, $$$[1, 3, 3]$$$, $$$[2, 1, 3]$$$, $$$[3, 1, 2]$$$, $$$[3, 1, 3]$$$, $$$[3, 2, 1]$$$, $$$[3, 3, 1]$$$; arrays with $$$2$$$ good starting cities: $$$[2, 2, 3]$$$, $$$[2, 3, 3]$$$, $$$[3, 2, 2]$$$, $$$[3, 3, 2]$$$; arrays with $$$3$$$ good starting cities: $$$[3, 2, 3]$$$, $$$[3, 3, 3]$$$. | 3,000 | true | true | false | true | false | false | false | false | false | false | 156 |
1304A | Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other. He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position $$$x$$$, and the shorter rabbit is currently on position $$$y$$$ ($$$x lt y$$$). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by $$$a$$$, and the shorter rabbit hops to the negative direction by $$$b$$$. For example, let's say $$$x=0$$$, $$$y=10$$$, $$$a=2$$$, and $$$b=3$$$. At the $$$1$$$-st second, each rabbit will be at position $$$2$$$ and $$$7$$$. At the $$$2$$$-nd second, both rabbits will be at position $$$4$$$. Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let's find a moment in time (in seconds) after which the rabbits will be at the same point. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Each test case contains exactly one line. The line consists of four integers $$$x$$$, $$$y$$$, $$$a$$$, $$$b$$$ ($$$0 le x lt y le 10^9$$$, $$$1 le a,b le 10^9$$$) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively. Output For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position. If the two rabbits will never be at the same position simultaneously, print $$$-1$$$. Example Input 5 0 10 2 3 0 10 3 3 900000000 1000000000 1 9999999 1 2 1 1 1 3 1 1 Note The first case is explained in the description. In the second case, each rabbit will be at position $$$3$$$ and $$$7$$$ respectively at the $$$1$$$-st second. But in the $$$2$$$-nd second they will be at $$$6$$$ and $$$4$$$ respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward. | 800 | true | false | false | false | false | false | false | false | false | false | 4,171 |
193C | Hamming distance between strings _a_ and _b_ of equal length (denoted by _h_(_a_,u2009_b_)) is equal to the number of distinct integers _i_ (1u2009≤u2009_i_u2009≤u2009_a_), such that _a__i_u2009≠u2009_b__i_, where _a__i_ is the _i_-th symbol of string _a_, _b__i_ is the _i_-th symbol of string _b_. For example, the Hamming distance between strings "aba" and "bba" equals 1, they have different first symbols. For strings "bbba" and "aaab" the Hamming distance equals 4. John Doe had a paper on which four strings of equal length _s_1, _s_2, _s_3 and _s_4 were written. Each string _s__i_ consisted only of lowercase letters "a" and "b". John found the Hamming distances between all pairs of strings he had. Then he lost the paper with the strings but he didn't lose the Hamming distances between all pairs. Help John restore the strings; find some four strings _s_'1, _s_'2, _s_'3,u2009_s_'4 of equal length that consist only of lowercase letters "a" and "b", such that the pairwise Hamming distances between them are the same as between John's strings. More formally, set _s_'_i_ must satisfy the condition . To make the strings easier to put down on a piece of paper, you should choose among all suitable sets of strings the one that has strings of minimum length. Input The first line contains space-separated integers _h_(_s_1,u2009_s_2), _h_(_s_1,u2009_s_3), _h_(_s_1,u2009_s_4). The second line contains space-separated integers _h_(_s_2,u2009_s_3) and _h_(_s_2,u2009_s_4). The third line contains the single integer _h_(_s_3,u2009_s_4). All given integers _h_(_s__i_,u2009_s__j_) are non-negative and do not exceed 105. It is guaranteed that at least one number _h_(_s__i_,u2009_s__j_) is positive. Output Print -1 if there's no suitable set of strings. Otherwise print on the first line number _len_ — the length of each string. On the _i_-th of the next four lines print string _s_'_i_. If there are multiple sets with the minimum length of the strings, print any of them. | 2,400 | true | true | false | false | false | true | false | false | false | false | 9,068 |
1583H | Omkar is hosting tours of his country, Omkarland! There are $$$n$$$ cities in Omkarland, and, rather curiously, there are exactly $$$n-1$$$ bidirectional roads connecting the cities to each other. It is guaranteed that you can reach any city from any other city through the road network. Every city has an enjoyment value $$$e$$$. Each road has a capacity $$$c$$$, denoting the maximum number of vehicles that can be on it, and an associated toll $$$t$$$. However, the toll system in Omkarland has an interesting quirk: if a vehicle travels on multiple roads on a single journey, they pay only the highest toll of any single road on which they traveled. (In other words, they pay $$$max t$$$ over all the roads on which they traveled.) If a vehicle traverses no roads, they pay $$$0$$$ toll. Omkar has decided to host $$$q$$$ tour groups. Each tour group consists of $$$v$$$ vehicles starting at city $$$x$$$. (Keep in mind that a tour group with $$$v$$$ vehicles can travel only on roads with capacity $$$geq v$$$.) Being the tour organizer, Omkar wants his groups to have as much fun as they possibly can, but also must reimburse his groups for the tolls that they have to pay. Thus, for each tour group, Omkar wants to know two things: first, what is the enjoyment value of the city $$$y$$$ with maximum enjoyment value that the tour group can reach from their starting city, and second, how much per vehicle will Omkar have to pay to reimburse the entire group for their trip from $$$x$$$ to $$$y$$$? (This trip from $$$x$$$ to $$$y$$$ will always be on the shortest path from $$$x$$$ to $$$y$$$.) In the case that there are multiple reachable cities with the maximum enjoyment value, Omkar will let his tour group choose which one they want to go to. Therefore, to prepare for all possible scenarios, he wants to know the amount of money per vehicle that he needs to guarantee that he can reimburse the group regardless of which city they choose. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 leq n leq 2 cdot 10^5$$$, $$$1 leq q leq 2 cdot 10^5$$$), representing the number of cities and the number of groups, respectively. The next line contains $$$n$$$ integers $$$e_1, e_2, ldots, e_n$$$ ($$$1 leq e_i leq 10^9$$$), where $$$e_i$$$ represents the enjoyment value for city $$$i$$$. The next $$$n-1$$$ lines each contain four integers $$$a$$$, $$$b$$$, $$$c$$$, and $$$t$$$ ($$$1 leq a,b leq n$$$, $$$1 leq c leq 10^9$$$, $$$1 leq t leq 10^9$$$), representing an road between city $$$a$$$ and city $$$b$$$ with capacity $$$c$$$ and toll $$$t$$$. The next $$$q$$$ lines each contain two integers $$$v$$$ and $$$x$$$ ($$$1 leq v leq 10^9$$$, $$$1 leq x leq n$$$), representing the number of vehicles in the tour group and the starting city, respectively. Output Output $$$q$$$ lines. The $$$i$$$-th line should contain two integers: the highest possible enjoyment value of a city reachable by the $$$i$$$-th tour group, and the amount of money per vehicle Omkar needs to guarantee that he can reimburse the $$$i$$$-th tour group. Examples Input 5 3 2 2 3 3 3 1 2 4 7 1 3 2 8 2 4 8 2 2 5 1 1 1 3 9 5 6 2 Input 5 5 1 2 3 4 5 1 2 4 1 1 3 3 1 1 4 2 1 2 5 1 1 5 1 4 1 3 1 2 1 1 1 Output 1 0 2 1 3 1 4 1 5 1 Input 5 5 1 2 2 2 2 1 2 5 8 1 3 6 3 1 4 4 5 1 5 7 1 4 1 5 1 6 1 7 1 8 1 Output 2 8 2 8 2 3 2 1 1 0 Note A map of the first sample is shown below. For the nodes, unbolded numbers represent indices and bolded numbers represent enjoyment values. For the edges, unbolded numbers represent capacities and bolded numbers represent tolls. For the first query, a tour group of size $$$1$$$ starting at city $$$3$$$ can reach cities $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, and $$$5$$$. Thus, the largest enjoyment value that they can reach is $$$3$$$. If the tour group chooses to go to city $$$4$$$, Omkar will have to pay $$$8$$$ per vehicle, which is the maximum. For the second query, a tour group of size $$$9$$$ starting at city $$$5$$$ can reach only city $$$5$$$. Thus, the largest reachable enjoyment value is still $$$3$$$, and Omkar will pay $$$0$$$ per vehicle. For the third query, a tour group of size $$$6$$$ starting at city $$$2$$$ can reach cities $$$2$$$ and $$$4$$$. The largest reachable enjoyment value is again $$$3$$$. If the tour group chooses to go to city $$$4$$$, Omkar will have to pay $$$2$$$ per vehicle, which is the maximum. A map of the second sample is shown below: For the first query, a tour group of size $$$5$$$ starting at city $$$1$$$ can only reach city $$$1$$$. Thus, their maximum enjoyment value is $$$1$$$ and the cost Omkar will have to pay is $$$0$$$ per vehicle. For the second query, a tour group of size $$$4$$$ starting at city $$$1$$$ can reach cities $$$1$$$ and $$$2$$$. Thus, their maximum enjoyment value is $$$2$$$ and Omkar will pay $$$1$$$ per vehicle. For the third query, a tour group of size $$$3$$$ starting at city $$$1$$$ can reach cities $$$1$$$, $$$2$$$, and $$$3$$$. Thus, their maximum enjoyment value is $$$3$$$ and Omkar will pay $$$1$$$ per vehicle. For the fourth query, a tour group of size $$$2$$$ starting at city $$$1$$$ can reach cities $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$. Thus, their maximum enjoyment value is $$$4$$$ and Omkar will pay $$$1$$$ per vehicle. For the fifth query, a tour group of size $$$1$$$ starting at city $$$1$$$ can reach cities $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, and $$$5$$$. Thus, their maximum enjoyment value is $$$5$$$ and Omkar will pay $$$1$$$ per vehicle. | 3,300 | false | false | false | false | true | false | false | false | true | false | 2,710 |
1540C1 | This is the easy version of the problem. The only difference is that in this version $$$q = 1$$$. You can make hacks only if both versions of the problem are solved. There is a process that takes place on arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ and length $$$n-1$$$ respectively. The process is an infinite sequence of operations. Each operation is as follows: First, choose a random integer $$$i$$$ ($$$1 le i le n-1$$$). Then, simultaneously set $$$a_i = minleft(a_i, frac{a_i+a_{i+1}-b_i}{2} ight)$$$ and $$$a_{i+1} = maxleft(a_{i+1}, frac{a_i+a_{i+1}+b_i}{2} ight)$$$ without any rounding (so values may become non-integer). See notes for an example of an operation. It can be proven that array $$$a$$$ converges, i.xa0e. for each $$$i$$$ there exists a limit $$$a_i$$$ converges to. Let function $$$F(a, b)$$$ return the value $$$a_1$$$ converges to after a process on $$$a$$$ and $$$b$$$. You are given array $$$b$$$, but not array $$$a$$$. However, you are given a third array $$$c$$$. Array $$$a$$$ is good if it contains only integers and satisfies $$$0 leq a_i leq c_i$$$ for $$$1 leq i leq n$$$. Your task is to count the number of good arrays $$$a$$$ where $$$F(a, b) geq x$$$ for $$$q$$$ values of $$$x$$$. Since the number of arrays can be very large, print it modulo $$$10^9+7$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100$$$). The second line contains $$$n$$$ integers $$$c_1, c_2 ldots, c_n$$$ ($$$0 le c_i le 100$$$). The third line contains $$$n-1$$$ integers $$$b_1, b_2, ldots, b_{n-1}$$$ ($$$0 le b_i le 100$$$). The fourth line contains a single integer $$$q$$$ ($$$q=1$$$). The fifth line contains $$$q$$$ space separated integers $$$x_1, x_2, ldots, x_q$$$ ($$$-10^5 le x_i le 10^5$$$). Output Output $$$q$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th query, i.xa0e. the number of good arrays $$$a$$$ where $$$F(a, b) geq x_i$$$ modulo $$$10^9+7$$$. Note The following explanation assumes $$$b = [2, 1]$$$ and $$$c=[2, 3, 4]$$$ (as in the sample). Examples of arrays $$$a$$$ that are not good: $$$a = [3, 2, 3]$$$ is not good because $$$a_1 > c_1$$$; $$$a = [0, -1, 3]$$$ is not good because $$$a_2 < 0$$$. One possible good array $$$a$$$ is $$$[0, 2, 4]$$$. We can show that no operation has any effect on this array, so $$$F(a, b) = a_1 = 0$$$. Another possible good array $$$a$$$ is $$$[0, 1, 4]$$$. In a single operation with $$$i = 1$$$, we set $$$a_1 = min(frac{0+1-2}{2}, 0)$$$ and $$$a_2 = max(frac{0+1+2}{2}, 1)$$$. So, after a single operation with $$$i = 1$$$, $$$a$$$ becomes equal to $$$[-frac{1}{2}, frac{3}{2}, 4]$$$. We can show that no operation has any effect on this array, so $$$F(a, b) = -frac{1}{2}$$$. | 2,700 | true | false | false | true | false | false | false | false | false | false | 2,940 |
1063C | This is an interactive problem. In good old times dwarves tried to develop extrasensory abilities: Exactly _n_ dwarves entered completely dark cave. Each dwarf received a hatxa0— white or black. While in cave, none of the dwarves was able to see either his own hat or hats of other Dwarves. Dwarves went out of the cave to the meadow and sat at an arbitrary place one after the other. When a dwarf leaves the cave, he sees the colors of all hats of all dwarves that are seating on the meadow (i.e. left the cave before him). However, he is not able to see the color of his own hat and none of the dwarves can give him this information. The task for dwarves was to got diverged into two partsxa0— one with dwarves with white hats and one with black hats. After many centuries, dwarves finally managed to select the right place on the meadow without error. Will you be able to repeat their success? You are asked to successively name _n_ different integer points on the plane. After naming each new point you will be given its colorxa0— black or white. Your task is to ensure that the named points can be split by a line in such a way that all points of one color lie on the same side from the line and points of different colors lie on different sides. Moreover, no points can belong to the line. Also, you need to report any such line at the end of the process. In this problem, the interactor is adaptivexa0— the colors of the points in the tests are not fixed beforehand and the jury program can select them arbitrarily, in particular, depending on your program output. Interaction The first line of the standard input stream contains an integer _n_ (1u2009≤u2009_n_u2009≤u200930)xa0— the number of points your program should name. Then _n_ times your program must print two integer coordinates _x_ and _y_ (0u2009≤u2009_x_u2009≤u2009109, 0u2009≤u2009_y_u2009≤u2009109). All points you print must be distinct. In response to each coordinate pair your program will receive the string "black", if the point is black, or "white", if the point is white. When all _n_ points are processed, you need to print four integers _x_1, _y_1, _x_2 and _y_2 (0u2009≤u2009_x_1,u2009_y_1u2009≤u2009109, 0u2009≤u2009_x_2,u2009_y_2u2009≤u2009109)xa0— coordinates of points (_x_1,u2009_y_1) and (_x_2,u2009_y_2), which form a line, which separates _n_ points into black and white. Points (_x_1,u2009_y_1) and (_x_2,u2009_y_2) should not coincide. Hacks To hack solution use the following format. The first line must contain word "hack", the second line should contain the number _n_ and the last line should contain the sequence of 0 and 1xa0— colors of points, which will be reported to the solution. Unlike the jury tests, colors of points in hacks are always fixed in advance. Of course, the hacked solution wouldn't be able to get the information about the colors in advance. For example, the hack corresponding to sample test will look like this: hack 5 0 0 1 1 0 Note In the sample input and output values are aligned only for simplicity of interpreting them chronologically. In real interaction no "extra" line breaks should appear. The following picture illustrates the first test. | 1,900 | false | false | false | false | false | true | false | true | false | false | 5,394 |
1379F2 | Note that the difference between easy and hard versions is that in hard version unavailable cells can become available again and in easy version can't. You can make hacks only if all versions are solved. Ildar and Ivan are tired of chess, but they really like the chessboard, so they invented a new game. The field is a chessboard $$$2n imes 2m$$$: it has $$$2n$$$ rows, $$$2m$$$ columns, and the cell in row $$$i$$$ and column $$$j$$$ is colored white if $$$i+j$$$ is even, and is colored black otherwise. The game proceeds as follows: Ildar marks some of the white cells of the chessboard as unavailable, and asks Ivan to place $$$n imes m$$$ kings on the remaining white cells in such way, so that there are no kings attacking each other. A king can attack another king if they are located in the adjacent cells, sharing an edge or a corner. Ildar would like to explore different combinations of cells. Initially all cells are marked as available, and then he has $$$q$$$ queries. In each query he either marks a cell as unavailable, or marks the previously unavailable cell as available. After each query he would like to know whether it is possible to place the kings on the available cells in a desired way. Please help him! Input The first line of input contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$1 leq n, m, q leq 200,000$$$)xa0— the size of the board and the number of queries. $$$q$$$ lines follow, each of them contains a description of a query: two integers $$$i$$$ and $$$j$$$, denoting a white cell on the board ($$$1 leq i leq 2n$$$, $$$1 leq j leq 2m$$$, $$$i + j$$$ is even). If the cell $$$(i, j)$$$ was available before the query, then it becomes unavailable. Otherwise, if the cell was unavailable, it becomes available. Output Output $$$q$$$ lines, $$$i$$$-th line should contain answer for a board after $$$i$$$ queries of Ildar. This line should contain "YES" if it is possible to place the kings on the available cells in the desired way, or "NO" otherwise. Examples Input 3 2 10 4 2 6 4 1 3 4 2 6 4 2 2 2 4 1 3 4 4 3 1 Output YES YES NO NO YES YES NO YES YES NO Note In the first example case after the second query only cells $$$(1, 1)$$$ and $$$(1, 5)$$$ are unavailable. Then Ivan can place three kings on cells $$$(2, 2)$$$, $$$(2, 4)$$$ and $$$(2, 6)$$$. After the third query three cells $$$(1, 1)$$$, $$$(1, 5)$$$ and $$$(2, 4)$$$ are unavailable, so there remain only 3 available cells: $$$(2, 2)$$$, $$$(1, 3)$$$ and $$$(2, 6)$$$. Ivan can not put 3 kings on those cells, because kings on cells $$$(2, 2)$$$ and $$$(1, 3)$$$ attack each other, since these cells share a corner. | 2,800 | false | false | false | false | true | false | false | false | false | false | 3,772 |
1346F | You're at the last mission in one very old and very popular strategy game Dune II: Battle For Arrakis. The map of the mission can be represented as a rectangular matrix of size $$$n imes m$$$. Initially, there are $$$a_{i, j}$$$ units of your army in the cell $$$(i, j)$$$. You want to prepare for the final battle, so you want to move all your army into exactly one cell of the map (i.e. $$$nm-1$$$ cells should contain $$$0$$$ units of the army and the remaining cell should contain the entire army). To do this, you can do some (possibly, zero) number of moves. During one move, you can select exactly one unit from some cell and move it to one of the adjacent by side cells. I.e. from the cell $$$(i, j)$$$ you can move the unit to cells: $$$(i - 1, j)$$$; $$$(i, j - 1)$$$; $$$(i + 1, j)$$$; $$$(i, j + 1)$$$. Of course, you want to move all your army into exactly one cell as fast as possible. So, you want to know the minimum number of moves you need to do that. And, of course, life goes on, so the situation on the map changes. There are $$$q$$$ updates, the $$$i$$$-th update is denoted by three integers $$$x, y, z$$$. This update affects the army in the cell $$$(x, y)$$$: after this update, the number of units in the cell $$$(x, y)$$$ becomes $$$z$$$ (i.e. you replace $$$a_{x, y}$$$ with $$$z$$$). Also, you want to determine, for each $$$i$$$, the minimum number of moves needed to move your entire army into exactly one cell with the first $$$i$$$ updates applied to the initial map. In other words, the map after the $$$i$$$-th update equals the initial map with the first $$$i$$$ updates applied to it. Input The first line of the input contains three integers $$$n, m$$$ and $$$q$$$ ($$$1 le n, m le 1000; 1 le q le 5000$$$)xa0— the size of the matrix and the number of updates correspondingly. The next $$$n$$$ lines contain $$$m$$$ integers each, where the $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i, j}$$$ ($$$1 le a_{i, j} le 10^9$$$)xa0— the number of units in the cell $$$(i, j)$$$. The next $$$q$$$ lines contain three integers each, where the $$$i$$$-th line contains three integers $$$x_i, y_i$$$ and $$$z_i$$$ ($$$1 le x_i le n; 1 le y_i le m; 1 le z_i le 10^9$$$)xa0— the cell in which the number of units updates and the new number of units in this cell correspondingly. Output Print $$$q+1$$$ integers $$$r_0, r_1, r_2, dots, r_n$$$, where $$$r_0$$$ is the minimum number of moves you need to move all your army into exactly one cell, and $$$r_i$$$ for all $$$i$$$ from $$$1$$$ to $$$q$$$ is the minimum number of moves you need to move all your army into exactly one cell after the first $$$i$$$ updates. | 2,000 | true | true | false | false | true | false | false | false | false | false | 3,974 |
954H | Problem - 954H - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp *2500 No tag edit access → Contest materials as depth of node _x_: depth of the root is 1, depth of any other node _x_ is _d_(_y_)u2009+u20091, where _y_ is a parent of _x_. The tree has the following property: every node _x_ with _d_(_x_)u2009=u2009_i_ has exactly _a__i_ children. Maximum possible depth of a node is _n_, and _a__n_u2009=u20090. We define _f__k_ as the number of unordered pairs of vertices in the tree such that the number of edges on the simple path between them is equal to _k_. Calculate _f__k_ modulo 109u2009+u20097 for every 1u2009≤u2009_k_u2009≤u20092_n_u2009-u20092. Input The first line of input contains an integer _n_ (2u2009u2009≤u2009u2009_n_u2009u2009≤u2009u20095u2009000) — the maximum depth of a node. The second line of input contains _n_u2009-u20091 integers _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_u2009-u20091 (2u2009≤u2009u2009_a__i_u2009u2009≤u2009109), where _a__i_ is the number of children of every node _x_ such that _d_(_x_)u2009=u2009_i_. Since _a__n_u2009=u20090, it is not given in the input. Output Print 2_n_u2009-u20092 numbers. The _k_-th of these numbers must be equal to _f__k_ modulo 109u2009+u20097. Examples Input 4 2 2 2 Output 14 19 20 20 16 16 Input 3 2 3 Output 8 13 6 9 Note This the tree from the first sample: | 2,500 | false | false | false | true | false | false | false | false | false | false | 5,902 |
15D | There is an area map that is a rectangular matrix _n_u2009×u2009_m_, each cell of the matrix contains the average height of a corresponding area part. Peter works for a company that has to build several cities within this area, each of the cities will occupy a rectangle _a_u2009×u2009_b_ cells on the map. To start construction works in a particular place Peter needs to remove excess ground from the construction site where a new city will be built. To do so he chooses a cell of the minimum height within this site, and removes excess ground from other cells of the site down to this minimum level. Let's consider that to lower the ground level from _h_2 to _h_1 (_h_1u2009≤u2009_h_2) they need to remove _h_2u2009-u2009_h_1 ground units. Let's call a site's position optimal, if the amount of the ground removed from this site is minimal compared to other possible positions. Peter constructs cities according to the following algorithm: from all the optimum site's positions he chooses the uppermost one. If this position is not unique, he chooses the leftmost one. Then he builds a city on this site. Peter repeats this process untill he can build at least one more city. For sure, he cannot carry out construction works on the occupied cells. Would you, please, help Peter place cities according to the algorithm? Input The first line contains four space-separated integers: map sizes _n_, _m_ and city sizes _a_, _b_ (1u2009≤u2009_a_u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_b_u2009≤u2009_m_u2009≤u20091000). Then there follow _n_ lines, each contains _m_ non-negative space-separated numbers, describing the height matrix. Each number doesn't exceed 109. Output In the first line output _k_ — the amount of constructed cities. In each of the following _k_ lines output 3 space-separated numbers — the row number and the column number of the upper-left corner of a subsequent construction site, and the amount of the ground to remove from it. Output the sites in the order of their building up. Examples Input 4 4 2 2 1 5 3 4 2 7 6 1 1 1 2 2 2 2 1 2 | 2,500 | false | false | true | false | true | false | false | false | true | false | 9,917 |
675B | Problem - 675B - 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 constructive algorithms math *1400 No tag edit access → Contest materials xa0— maximum possible value of an integer in the cell and four integers that Vasya remembers. Output Print one integerxa0— the number of distinct valid squares. Examples Input 2 1 1 1 2 Output 2 Input 3 3 1 2 3 Output 6 Note Below are all the possible paintings for the first sample. In the second sample, only paintings displayed below satisfy all the rules. | 1,400 | true | false | false | false | false | true | true | false | false | false | 7,130 |
1165E | You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$. Let's define a function $$$f(l, r) = sumlimits_{l le i le r} a_i cdot b_i$$$. Your task is to reorder the elements (choose an arbitrary order of elements) of the array $$$b$$$ to minimize the value of $$$sumlimits_{1 le l le r le n} f(l, r)$$$. Since the answer can be very large, you have to print it modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of elements in $$$a$$$ and $$$b$$$. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. The third line of the input contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_j le 10^6$$$), where $$$b_j$$$ is the $$$j$$$-th element of $$$b$$$. Output Print one integer — the minimum possible value of $$$sumlimits_{1 le l le r le n} f(l, r)$$$ after rearranging elements of $$$b$$$, taken modulo $$$998244353$$$. Note that you should minimize the answer but not its remainder. Examples Input 5 1 8 7 2 4 9 7 2 9 3 | 1,600 | true | true | false | false | false | false | false | false | true | false | 4,892 |
840C | Problem - 840C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp *2500 No tag edit access → Contest materials — length of the array. Next line contains _n_ integers _a_1,u2009_a_2,u2009... ,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — found array. Output Output single integer — number of right permutations modulo 109u2009+u20097. Examples Input 3 1 2 4 Output 2 Input 7 5 2 4 2 4 1 1 Output 144 Note For first example: | 2,500 | false | false | false | true | false | false | false | false | false | false | 6,401 |
1726H | Mainak has a convex polygon $$$mathcal P$$$ with $$$n$$$ vertices labelled as $$$A_1, A_2, ldots, A_n$$$ in a counter-clockwise fashion. The coordinates of the $$$i$$$-th point $$$A_i$$$ are given by $$$(x_i, y_i)$$$, where $$$x_i$$$ and $$$y_i$$$ are both integers. Further, it is known that the interior angle at $$$A_i$$$ is either a right angle or a proper obtuse angle. Formally it is known that: $$$90 ^ circ le angle A_{i - 1}A_{i}A_{i + 1} < 180 ^ circ$$$, $$$forall i in {1, 2, ldots, n}$$$ where we conventionally consider $$$A_0 = A_n$$$ and $$$A_{n + 1} = A_1$$$. Mainak's friend insisted that all points $$$Q$$$ such that there exists a chord of the polygon $$$mathcal P$$$ passing through $$$Q$$$ with length not exceeding $$$1$$$, must be coloured $$$color{red}{ ext{red}}$$$. Mainak wants you to find the area of the coloured region formed by the $$$color{red}{ ext{red}}$$$ points. Formally, determine the area of the region $$$mathcal S = {Q in mathcal{P}$$$ $$$Q ext{ is coloured } color{red}{ ext{red}}}$$$. Recall that a chord of a polygon is a line segment between two points lying on the boundary (i.e. vertices or points on edges) of the polygon. Input The first line contains an integer $$$n$$$ ($$$4 le n le 5000$$$) — the number of vertices of a polygon $$$mathcal P$$$. The $$$i$$$-th line of the next $$$n$$$ lines contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^9 le x_i, y_i le 10^9$$$) — the coordinates of $$$A_i$$$. Additional constraint on the input: The vertices form a convex polygon and are listed in counter-clockwise order. It is also guaranteed that all interior angles are in the range $$$[90^circ ; 180^circ )$$$. Output Print the area of the region coloured in $$$color{red}{ ext{red}}$$$. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-4}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$frac{a - b}{max{(1, b)}} le 10^{-4}$$$. Examples Input 5 -3 3 3 1 4 2 -1 9 -2 9 Note In the first example, the polygon $$$mathcal P$$$ can be visualised on the Cartesian Plane as: | 3,500 | true | false | true | false | false | false | false | true | false | false | 1,926 |
1263E | The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text. Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left. Initially, the cursor is in the first (leftmost) character. Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position. Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence. Formally, correct text (CT) must satisfy the following rules: any line without brackets is CT (the line can contain whitespaces); If the first character of the string — is (, the last — is ), and all the rest form a CT, then the whole line is a CT; two consecutively written CT is also CT. Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me). The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command. The correspondence of commands and characters is as follows: L — move the cursor one character to the left (remains in place if it already points to the first character); R — move the cursor one character to the right; any lowercase Latin letter or bracket (( or )) — write the entered character to the position where the cursor is now. For a complete understanding, take a look at the first example and its illustrations in the note below. You are given a string containing the characters that the user entered. For the brackets coloring module's work, after each command you need to: check if the current text in the editor is a correct text; if it is, print the least number of colors that required, to color all brackets. If two pairs of brackets are nested (the first in the second or vice versa), then these pairs of brackets should be painted in different colors. If two pairs of brackets are not nested, then they can be painted in different or the same colors. For example, for the bracket sequence ()(())()() the least number of colors is $$$2$$$, and for the bracket sequence (()(()())())(()) — is $$$3$$$. Write a program that prints the minimal number of colors after processing each command. | 2,100 | false | false | true | false | true | false | false | false | false | false | 4,383 |
1054H | Problem - 1054H - Codeforces =============== xa0 . Next line contains exactly $$$n$$$ integers $$$a_i$$$ and defines the array $$$a$$$ ($$$0 le a_i le 1000$$$). Last line contains exactly $$$m$$$ integers $$$b_i$$$ and defines the array $$$b$$$ ($$$0 le b_i le 1000$$$). Output Print one integerxa0— value of the sum modulo $$$490019$$$. Examples Input 2 2 3 0 1 0 1 Output 3 Input 3 4 1 1 1 1 1 1 1 1 Output 12 Input 2 3 3 1 2 3 4 5 Output 65652 Note In the first example, the only non-zero summand corresponds to $$$i = 1$$$, $$$j = 1$$$ and is equal to $$$1 cdot 1 cdot 3^1 = 3$$$. In the second example, all summands are equal to $$$1$$$. | 3,500 | true | false | false | false | false | false | false | false | false | false | 5,440 |
354D | Vasya and Petya are using an interesting data storing structure: a pyramid. The pyramid consists of _n_ rows, the _i_-th row contains _i_ cells. Each row is shifted half a cell to the left relative to the previous row. The cells are numbered by integers from 1 to as shown on the picture below. An example of a pyramid at _n_u2009=u20095 is: This data structure can perform operations of two types: 1. Change the value of a specific cell. It is described by three integers: "_t_u2002_i_u2002_v_", where _t_u2009=u20091 (the type of operation), _i_ — the number of the cell to change and _v_ the value to assign to the cell. 2. Change the value of some subpyramid. The picture shows a highlighted subpyramid with the top in cell 5. It is described by _s_u2009+u20092 numbers: "_t_u2002_i_u2002_v_1u2002_v_2u2002...u2002_v__s_", where _t_u2009=u20092, _i_ — the number of the top cell of the pyramid, _s_ — the size of the subpyramid (the number of cells it has), _v__j_ — the value you should assign to the _j_-th cell of the subpyramid. Formally: a subpyramid with top at the _i_-th cell of the _k_-th row (the 5-th cell is the second cell of the third row) will contain cells from rows from _k_ to _n_, the (_k_u2009+u2009_p_)-th row contains cells from the _i_-th to the (_i_u2009+u2009_p_)-th (0u2009≤u2009_p_u2009≤u2009_n_u2009-u2009_k_). Vasya and Petya had two identical pyramids. Vasya changed some cells in his pyramid and he now wants to send his changes to Petya. For that, he wants to find a sequence of operations at which Petya can repeat all Vasya's changes. Among all possible sequences, Vasya has to pick the minimum one (the one that contains the fewest numbers). You have a pyramid of _n_ rows with _k_ changed cells. Find the sequence of operations which result in each of the _k_ changed cells being changed by at least one operation. Among all the possible sequences pick the one that contains the fewest numbers. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009105). The next _k_ lines contain the coordinates of the modified cells _r__i_ and _c__i_ (1u2009≤u2009_c__i_u2009≤u2009_r__i_u2009≤u2009_n_) — the row and the cell's number in the row. All cells are distinct. Output Print a single number showing how many numbers the final sequence has. Examples Input 7 11 2 2 3 1 4 3 5 1 5 2 5 5 6 4 7 2 7 3 7 4 7 5 Note One of the possible solutions of the first sample consists of two operations: 2u20024u2002_v_4u2002_v_7u2002_v_8 2u20026u2002_v_6u2002_v_9u2002_v_10 The picture shows the changed cells color-highlighted. The subpyramid used by the first operation is highlighted blue and the subpyramid used by the first operation is highlighted yellow: | 2,900 | false | false | false | true | false | false | false | false | false | false | 8,415 |
814A | A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed. To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long time, but now Kaiki provides an opportunity. Hitagi's sequence _a_ has a length of _n_. Lost elements in it are denoted by zeros. Kaiki provides another sequence _b_, whose length _k_ equals the number of lost elements in _a_ (i.e. the number of zeros). Hitagi is to replace each zero in _a_ with an element from _b_ so that each element in _b_ should be used exactly once. Hitagi knows, however, that, apart from 0, no integer occurs in _a_ and _b_ more than once in total. If the resulting sequence is not an increasing sequence, then it has the power to recover Hitagi from the oddity. You are to determine whether this is possible, or Kaiki's sequence is just another fake. In other words, you should detect whether it is possible to replace each zero in _a_ with an integer from _b_ so that each integer from _b_ is used exactly once, and the resulting sequence is not increasing. Input The first line of input contains two space-separated positive integers _n_ (2u2009≤u2009_n_u2009≤u2009100) and _k_ (1u2009≤u2009_k_u2009≤u2009_n_) — the lengths of sequence _a_ and _b_ respectively. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009200) — Hitagi's broken sequence with exactly _k_ zero elements. The third line contains _k_ space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__k_ (1u2009≤u2009_b__i_u2009≤u2009200) — the elements to fill into Hitagi's sequence. Input guarantees that apart from 0, no integer occurs in _a_ and _b_ more than once in total. Output Output "Yes" if it's possible to replace zeros in _a_ with elements in _b_ and make the resulting sequence not increasing, and "No" otherwise. Examples Input 7 7 0 0 0 0 0 0 0 1 2 3 4 5 6 7 Note In the first sample: Sequence _a_ is 11,u20090,u20090,u200914. Two of the elements are lost, and the candidates in _b_ are 5 and 4. There are two possible resulting sequences: 11,u20095,u20094,u200914 and 11,u20094,u20095,u200914, both of which fulfill the requirements. Thus the answer is "Yes". In the second sample, the only possible resulting sequence is 2,u20093,u20095,u20098,u20099,u200910, which is an increasing sequence and therefore invalid. | 900 | false | true | true | false | false | true | false | false | true | false | 6,505 |
239B | There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. Current character pointer (CP); Direction pointer (DP) which can point left or right; Initially CP points to the leftmost character of the sequence and DP points to the right. We repeat the following steps until the first moment that CP points to somewhere outside the sequence. If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence. If at any moment the CP goes outside of the sequence the execution is terminated. It's obvious the every program in this language terminates after some steps. We have a sequence _s_1,u2009_s_2,u2009...,u2009_s__n_ of "<", ">" and digits. You should answer _q_ queries. Each query gives you _l_ and _r_ and asks how many of each digit will be printed if we run the sequence _s__l_,u2009_s__l_u2009+u20091,u2009...,u2009_s__r_ as an independent program in this language. Input The first line of input contains two integers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009100) — represents the length of the sequence _s_ and the number of queries. The second line contains _s_, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of _s_ are not separated with spaces. The next _q_ lines each contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_) — the _i_-th query. Output For each query print 10 space separated integers: _x_0,u2009_x_1,u2009...,u2009_x_9 where _x__i_ equals the number of times the interpreter prints _i_ while running the corresponding program. Print answers to the queries in the order they are given in input. Examples Input 7 4 1>3>22< 1 3 4 7 7 7 1 7 Output 0 1 0 1 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 2 1 0 0 0 0 0 0 | 1,500 | false | false | true | false | false | false | true | false | false | false | 8,878 |
1991E | This is an interactive problem. Consider an undirected connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Each vertex can be colored with one of three colors: $$$1$$$, $$$2$$$, or $$$3$$$. Initially, all vertices are uncolored. Alice and Bob are playing a game consisting of $$$n$$$ rounds. In each round, the following two-step process happens: 1. Alice chooses two different colors. 2. Bob chooses an uncolored vertex and colors it with one of the two colors chosen by Alice. Alice wins if there exists an edge connecting two vertices of the same color. Otherwise, Bob wins. You are given the graph. Your task is to decide which player you wish to play as and win the game. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 le n le 10^4$$$, $$$n - 1 le m le min(frac{n cdot (n - 1)}{2}, 10^4)$$$)xa0— the number of vertices and the number of edges in the graph, respectively. Each of the next $$$m$$$ lines of each test case contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n$$$)xa0— the edges of the graph. It is guaranteed that the graph is connected and there are no multiple edges or self-loops. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$10^4$$$. Interaction For each test case, you need to output a single line containing either "Alice" or "Bob", representing the player you choose. Then for each of the following $$$n$$$ rounds, the following two-step process happens: 1. Alice (either you or the interactor) will output two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 3$$$, $$$a eq b$$$)xa0— the colors chosen by Alice. 2. Bob (either you or the interactor) will output two integers $$$i$$$ and $$$c$$$ ($$$1 le i le n$$$, $$$c = a$$$ or $$$c = b$$$)xa0— the vertex and the color chosen by Bob. Vertex $$$i$$$ must be a previously uncolored vertex. If any of your outputs are invalid, the jury will output "-1" and you will receive a Wrong Answer verdict. At the end of all $$$n$$$ turns, if you have lost the game, the jury will output "-1" and you will receive a Wrong Answer verdict. If your program has received a $$$-1$$$ instead of a valid value, it must terminate immediately. Otherwise, you may receive an arbitrary verdict because your solution might be reading from a closed stream. Note that if you are playing as Alice, and there already exists an edge connected two vertices of the same color, the interactor will not terminate early and you will keep playing all $$$n$$$ rounds. After outputting, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. In this problem, hacks are disabled. Example Input 2 3 3 1 2 2 3 3 1 3 1 2 2 1 1 4 4 1 2 2 3 3 4 4 1 2 3 1 2 2 1 3 1 Output Alice 3 1 1 2 2 1 Bob 1 2 2 1 4 1 3 3 Note Note that the sample test cases are example games and do not necessarily represent the optimal strategy for both players. In the first test case, you choose to play as Alice. 1. Alice chooses two colors: $$$3$$$ and $$$1$$$. Bob chooses vertex $$$3$$$ and colors it with color $$$1$$$. 2. Alice chooses two colors: $$$1$$$ and $$$2$$$. Bob chooses vertex $$$2$$$ and colors it with color $$$2$$$. 3. Alice chooses two colors: $$$2$$$ and $$$1$$$. Bob chooses vertex $$$1$$$ and colors it with color $$$1$$$. Alice wins because the edge $$$(3, 1)$$$ connects two vertices of the same color. In the second test case, you choose to play as Bob. 1. Alice chooses two colors: $$$2$$$ and $$$3$$$. Bob chooses vertex $$$1$$$ and colors it with color $$$2$$$. 2. Alice chooses two colors: $$$1$$$ and $$$2$$$. Bob chooses vertex $$$2$$$ and colors it with color $$$1$$$. 3. Alice chooses two colors: $$$2$$$ and $$$1$$$. Bob chooses vertex $$$4$$$ and colors it with color $$$1$$$. 4. Alice chooses two colors: $$$3$$$ and $$$1$$$. Bob chooses vertex $$$3$$$ and colors it with color $$$3$$$. Bob wins because there are no edges with vertices of the same color. | 1,900 | false | true | false | false | false | true | false | false | false | true | 324 |
257C | Problem - 257C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force geometry math *1800 No tag edit access → Contest materials ") Разбор Задач") — the number of mannequins. Next _n_ lines contain two space-separated integers each: _x__i_,u2009_y__i_ (_x__i_,u2009_y__i_u2009≤u20091000) — the coordinates of the _i_-th mannequin. It is guaranteed that the origin of the coordinates has no mannequin. It is guaranteed that no two mannequins are located in the same point on the plane. Output Print a single real number — the value of the sought angle in degrees. The answer will be considered valid if the relative or absolute error doesn't exceed 10u2009-u20096. Examples Input 2 2 0 0 2 Output 90.0000000000 Input 3 2 0 0 2 -2 2 Output 135.0000000000 Input 4 2 0 0 2 -2 0 0 -2 Output 270.0000000000 Input 2 2 1 1 2 Output 36.8698976458 Note Solution for the first sample test is shown below: Solution for the second sample test is shown below: Solution for the third sample test is shown below: Solution for the fourth sample test is shown below: | 1,800 | true | false | false | false | false | false | true | false | false | false | 8,805 |
156C | Sherlock Holmes found a mysterious correspondence of two VIPs and made up his mind to read it. But there is a problem! The correspondence turned out to be encrypted. The detective tried really hard to decipher the correspondence, but he couldn't understand anything. At last, after some thought, he thought of something. Let's say there is a word _s_, consisting of _s_ lowercase Latin letters. Then for one operation you can choose a certain position _p_ (1u2009≤u2009_p_u2009<u2009_s_) and perform one of the following actions: either replace letter _s__p_ with the one that alphabetically follows it and replace letter _s__p_u2009+u20091 with the one that alphabetically precedes it; or replace letter _s__p_ with the one that alphabetically precedes it and replace letter _s__p_u2009+u20091 with the one that alphabetically follows it. Let us note that letter "z" doesn't have a defined following letter and letter "a" doesn't have a defined preceding letter. That's why the corresponding changes are not acceptable. If the operation requires performing at least one unacceptable change, then such operation cannot be performed. Two words coincide in their meaning iff one of them can be transformed into the other one as a result of zero or more operations. Sherlock Holmes needs to learn to quickly determine the following for each word: how many words can exist that coincide in their meaning with the given word, but differs from the given word in at least one character? Count this number for him modulo 1000000007 (109u2009+u20097). Input The input data contains several tests. The first line contains the only integer _t_ (1u2009≤u2009_t_u2009≤u2009104) — the number of tests. Next _t_ lines contain the words, one per line. Each word consists of lowercase Latin letters and has length from 1 to 100, inclusive. Lengths of words can differ. Output For each word you should print the number of different other words that coincide with it in their meaning — not from the words listed in the input data, but from all possible words. As the sought number can be very large, print its value modulo 1000000007 (109u2009+u20097). Note Some explanations about the operation: Note that for each letter, we can clearly define the letter that follows it. Letter "b" alphabetically follows letter "a", letter "c" follows letter "b", ..., "z" follows letter "y". Preceding letters are defined in the similar manner: letter "y" precedes letter "z", ..., "a" precedes letter "b". Note that the operation never changes a word's length. In the first sample you can obtain the only other word "ba". In the second sample you cannot obtain any other word, so the correct answer is 0. Consider the third sample. One operation can transform word "klmbfxzb" into word "klmcexzb": we should choose _p_u2009=u20094, and replace the fourth letter with the following one ("b" u2009→u2009 "c"), and the fifth one — with the preceding one ("f" u2009→u2009 "e"). Also, we can obtain many other words from this one. An operation can transform word "ya" only into one other word "xb". Word "ya" coincides in its meaning with words "xb", "wc", "vd", ..., "ay" (overall there are 24 other words). The word "klmbfxzb has many more variants — there are 3320092814 other words that coincide with in the meaning. So the answer for the first word equals 24 and for the second one equals 320092793 — the number 3320092814 modulo 109u2009+u20097 | 2,000 | false | false | false | true | false | false | false | false | false | false | 9,250 |
1370A | Problem - 1370A - 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 math number theory *800 No tag edit access → Contest materials . Among all pairs of distinct integers in this range, find the maximum possible greatest common divisor of integers in pair. Formally, find the maximum value of $$$mathrm{gcd}(a, b)$$$, where $$$1 leq a < b leq n$$$. The greatest common divisor, $$$mathrm{gcd}(a, b)$$$, of two positive integers $$$a$$$ and $$$b$$$ is the biggest integer that is a divisor of both $$$a$$$ and $$$b$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$) xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 10^6$$$). Output For each test case, output the maximum value of $$$mathrm{gcd}(a, b)$$$ among all $$$1 leq a < b leq n$$$. Example Input 2 3 5 Output 1 2 Note In the first test case, $$$mathrm{gcd}(1, 2) = mathrm{gcd}(2, 3) = mathrm{gcd}(1, 3) = 1$$$. In the second test case, $$$2$$$ is the maximum possible value, corresponding to $$$mathrm{gcd}(2, 4)$$$. | 800 | true | true | true | false | false | false | false | false | false | false | 3,821 |
1366F | You are given a simple weighted connected undirected graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. A path in the graph of length $$$k$$$ is a sequence of $$$k+1$$$ vertices $$$v_1, v_2, dots, v_{k+1}$$$ such that for each $$$i$$$ $$$(1 le i le k)$$$ the edge $$$(v_i, v_{i+1})$$$ is present in the graph. A path from some vertex $$$v$$$ also has vertex $$$v_1=v$$$. Note that edges and vertices are allowed to be included in the path multiple times. The weight of the path is the total weight of edges in it. For each $$$i$$$ from $$$1$$$ to $$$q$$$ consider a path from vertex $$$1$$$ of length $$$i$$$ of the maximum weight. What is the sum of weights of these $$$q$$$ paths? Answer can be quite large, so print it modulo $$$10^9+7$$$. Input The first line contains a three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$2 le n le 2000$$$; $$$n - 1 le m le 2000$$$; $$$m le q le 10^9$$$)xa0— the number of vertices in the graph, the number of edges in the graph and the number of lengths that should be included in the answer. Each of the next $$$m$$$ lines contains a description of an edge: three integers $$$v$$$, $$$u$$$, $$$w$$$ ($$$1 le v, u le n$$$; $$$1 le w le 10^6$$$)xa0— two vertices $$$v$$$ and $$$u$$$ are connected by an undirected edge with weight $$$w$$$. The graph contains no loops and no multiple edges. It is guaranteed that the given edges form a connected graph. Output Print a single integerxa0— the sum of the weights of the paths from vertex $$$1$$$ of maximum weights of lengths $$$1, 2, dots, q$$$ modulo $$$10^9+7$$$. Examples Input 7 8 25 1 2 1 2 3 10 3 4 2 1 5 2 5 6 7 6 4 15 5 3 1 1 7 3 Input 15 15 23 13 10 12 11 14 12 2 15 5 4 10 8 10 2 4 10 7 5 3 10 1 5 6 11 1 13 8 9 15 4 4 2 9 11 15 1 11 12 14 10 8 12 3 6 11 Input 5 10 10000000 2 4 798 1 5 824 5 2 558 4 1 288 3 4 1890 3 1 134 2 3 1485 4 5 284 3 5 1025 1 2 649 Note Here is the graph for the first example: Some maximum weight paths are: length $$$1$$$: edges $$$(1, 7)$$$xa0— weight $$$3$$$; length $$$2$$$: edges $$$(1, 2), (2, 3)$$$xa0— weight $$$1+10=11$$$; length $$$3$$$: edges $$$(1, 5), (5, 6), (6, 4)$$$xa0— weight $$$2+7+15=24$$$; length $$$4$$$: edges $$$(1, 5), (5, 6), (6, 4), (6, 4)$$$xa0— weight $$$2+7+15+15=39$$$; $$$dots$$$ So the answer is the sum of $$$25$$$ terms: $$$3+11+24+39+dots$$$ In the second example the maximum weight paths have weights $$$4$$$, $$$8$$$, $$$12$$$, $$$16$$$ and $$$20$$$. | 2,700 | false | false | false | true | false | false | false | true | false | true | 3,845 |
513G1 | You are given a permutation of _n_ numbers _p_1,u2009_p_2,u2009...,u2009_p__n_. We perform _k_ operations of the following type: choose uniformly at random two indices _l_ and _r_ (_l_u2009≤u2009_r_) and reverse the order of the elements _p__l_,u2009_p__l_u2009+u20091,u2009...,u2009_p__r_. Your task is to find the expected value of the number of inversions in the resulting permutation. Input The first line of input contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u2009109). The next line contains _n_ integers _p_1,u2009_p_2,u2009...,u2009_p__n_ — the given permutation. All _p__i_ are different and in range from 1 to _n_. The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem G1 (3 points), the constraints 1u2009≤u2009_n_u2009≤u20096, 1u2009≤u2009_k_u2009≤u20094 will hold. In subproblem G2 (5 points), the constraints 1u2009≤u2009_n_u2009≤u200930, 1u2009≤u2009_k_u2009≤u2009200 will hold. In subproblem G3 (16 points), the constraints 1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u2009109 will hold. Output Output the answer with absolute or relative error no more than 1_e_u2009-u20099. Note Consider the first sample test. We will randomly pick an interval of the permutation (1,u20092,u20093) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability we will inverse the first two elements' order and obtain the permutation (2,u20091,u20093) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1,u20093,u20092) with one inversion. Finally, with probability the randomly picked interval will contain all elements, leading to the permutation (3,u20092,u20091) with 3 inversions. Hence, the expected number of inversions is equal to . | 1,800 | false | false | false | true | false | false | true | false | false | false | 7,783 |
1400D | Problem - 1400D - 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 combinatorics data structures math two pointers *1900 No tag edit access → Contest materials ") $$$ such that: $$$1 le i < j < k < l le n$$$; $$$a_i = a_k$$$ and $$$a_j = a_l$$$; Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$4 le n le 3000$$$)xa0— the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the array $$$a$$$. It's guaranteed that the sum of $$$n$$$ in one test doesn't exceed $$$3000$$$. Output For each test case, print the number of described tuples. Example Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 Note In the first test case, for any four indices $$$i < j < k < l$$$ are valid, so the answer is the number of tuples. In the second test case, there are $$$2$$$ valid tuples: $$$(1, 2, 4, 6)$$$: $$$a_1 = a_4$$$ and $$$a_2 = a_6$$$; $$$(1, 3, 4, 6)$$$: $$$a_1 = a_4$$$ and $$$a_3 = a_6$$$. | 1,900 | true | false | false | false | true | false | true | false | false | false | 3,669 |
1153C | Serval soon said goodbye to Japari kindergarten, and began his life in Japari Primary School. In his favorite math class, the teacher taught him the following interesting definitions. A parenthesis sequence is a string, containing only characters "(" and ")". A correct parenthesis sequence is a parenthesis sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, parenthesis sequences "()()", "(())" are correct (the resulting expressions are: "(1+1)+(1+1)", "((1+1)+1)"), while ")(" and ")" are not. Note that the empty string is a correct parenthesis sequence by definition. We define that $$$s$$$ as the length of string $$$s$$$. A strict prefix $$$s[1dots l]$$$ $$$(1leq l< s)$$$ of a string $$$s = s_1s_2dots s_{s}$$$ is string $$$s_1s_2dots s_l$$$. Note that the empty string and the whole string are not strict prefixes of any string by the definition. Having learned these definitions, he comes up with a new problem. He writes down a string $$$s$$$ containing only characters "(", ")" and "?". And what he is going to do, is to replace each of the "?" in $$$s$$$ independently by one of "(" and ")" to make all strict prefixes of the new sequence not a correct parenthesis sequence, while the new sequence should be a correct parenthesis sequence. After all, he is just a primary school student so this problem is too hard for him to solve. As his best friend, can you help him to replace the question marks? If there are many solutions, any of them is acceptable. Input The first line contains a single integer $$$s$$$ ($$$1leq sleq 3 cdot 10^5$$$), the length of the string. The second line contains a string $$$s$$$, containing only "(", ")" and "?". Output A single line contains a string representing the answer. If there are many solutions, any of them is acceptable. If there is no answer, print a single line containing ":(" (without the quotes). | 1,700 | false | true | false | false | false | false | false | false | false | false | 4,947 |
2005E1 | This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $$$a$$$ and a matrix $$$b$$$ of integers with $$$n$$$ rows and $$$m$$$ columns, numbered from $$$1$$$. The cell in the $$$i$$$-th row and the $$$j$$$-th column is $$$(i, j)$$$. They are looking for the elements of $$$a$$$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $$$a$$$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $$$(r, c)$$$. The next player has to choose his cell in the submatrix starting at $$$(r + 1, c + 1)$$$ and ending in $$$(n, m)$$$ (the submatrix can be empty if $$$r=n$$$ or $$$c=m$$$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally. Input The first line of the input contains $$$t$$$ ($$$1 le t le 300$$$)xa0– the number of test cases. The first line of each test case contains three integers $$$l$$$, $$$n$$$, and $$$m$$$ ($$$1 le l, n, m le 300$$$)xa0– the size of the array and the sizes of the matrix. The second line contains $$$l$$$ integers $$$a_1, a_2, a_3, ldots a_l$$$ ($$$1 le a_i le min(7, n cdot m)$$$)xa0– the elements of the array $$$a$$$. The $$$i$$$-th of the last $$$n$$$ lines contains $$$m$$$ integers $$$b_{i,1}, b_{i,2}, b_{i,3}, ldots b_{i,m}$$$ ($$$1 le b_{i,j} le min(7, n cdot m)$$$)xa0– representing the $$$i$$$-th row of the matrix. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$10^5$$$. It is guaranteed that the sum of $$$l$$$ over all test cases does not exceed $$$300$$$. Output You should output $$$t$$$ lines, the $$$i$$$-th of them containing a character representing the answer of the $$$i$$$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes). Example Input 3 2 2 3 1 2 1 3 5 4 5 2 2 2 4 1 2 1 1 3 2 4 2 5 1 2 4 2 1 2 3 4 5 5 5 5 5 5 Note In the first example, Tsovak starts by looking for $$$1$$$. There is only one occurrence of $$$1$$$ at $$$(1,1)$$$, so he chooses it. Then Narek needs to look for $$$2$$$ in the submatrix of $$$(2, 2)$$$, which consists of just the last two elements: $$$5$$$ and $$$2$$$. He chooses $$$2$$$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $$$1$$$. There is a $$$1$$$ at the cell $$$(n,m)$$$, so he chooses that cell. Then, since the submatrix of $$$(n + 1, m + 1)$$$ is empty, Narek cannot find $$$2$$$, so he loses. | 2,100 | false | true | true | true | false | false | false | false | false | false | 219 |
1845D | You are developing a rating system for an online game. Every time a player participates in a match, the player's rating changes depending on the results. Initially, the player's rating is $$$0$$$. There are $$$n$$$ matches; after the $$$i$$$-th match, the rating change is equal to $$$a_i$$$ (the rating increases by $$$a_i$$$ if $$$a_i$$$ is positive, or decreases by $$$a_i$$$ if it's negative. There are no zeros in the sequence $$$a$$$). The system has an additional rule: for a fixed integer $$$k$$$, if a player's rating has reached the value $$$k$$$, it will never fall below it. Formally, if a player's rating at least $$$k$$$, and a rating change would make it less than $$$k$$$, then the rating will decrease to exactly $$$k$$$. Your task is to determine the value $$$k$$$ in such a way that the player's rating after all $$$n$$$ matches is the maximum possible (among all integer values of $$$k$$$). If there are multiple possible answers, you can print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the number of matches. The second line contains $$$n$$$ integer $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$; $$$a_i e 0$$$)xa0— the rating change after the $$$i$$$-th match. The sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print one integer $$$m$$$ ($$$-10^{18} le m le 10^{18}$$$)xa0— the value of $$$k$$$ such that the rating of the player will be the maximum possible when using this value. It can be shown that at least one of the optimal answers meets the constraint $$$-10^{18} le m le 10^{18}$$$. Example Input 4 4 3 -2 1 2 3 -1 -2 -1 2 4 2 7 5 1 -3 2 -1 -2 2 Note In the first example, if $$$k=3$$$, then the rating changes as follows: $$$0 ightarrow 3 ightarrow 3 ightarrow 4 ightarrow 6$$$. In the second example, if $$$k=0$$$, then the rating changes as follows: $$$0 ightarrow 0 ightarrow 0 ightarrow 0$$$. In the third example, if $$$k=25$$$, then the rating changes as follows: $$$0 ightarrow 4 ightarrow 6$$$. In the fourth example, if $$$k=6$$$, then the rating changes as follows: $$$0 ightarrow 5 ightarrow 6 ightarrow 6 ightarrow 8 ightarrow 7 ightarrow 6 ightarrow 8$$$. | 1,800 | true | true | false | true | true | false | true | true | false | false | 1,211 |
1545F | AquaMoon has three integer arrays $$$a$$$, $$$b$$$, $$$c$$$ of length $$$n$$$, where $$$1 leq a_i, b_i, c_i leq n$$$ for all $$$i$$$. In order to accelerate her potato farming, she organizes her farm in a manner based on these three arrays. She is now going to complete $$$m$$$ operations to count how many potatoes she can get. Each operation will have one of the two types: 1. AquaMoon reorganizes their farm and makes the $$$k$$$-th element of the array $$$a$$$ equal to $$$x$$$. In other words, perform the assignment $$$a_k := x$$$. 2. Given a positive integer $$$r$$$, AquaMoon receives a potato for each triplet $$$(i,j,k)$$$, such that $$$1le i<j<kle r$$$, and $$$b_{a_i}=a_j=c_{a_k}$$$. Count the number of such triplets. As AquaMoon is busy finding the library, help her complete all of their operations. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$1le nle 2cdot10^5$$$, $$$1le mle 5cdot10^4$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots,a_n$$$ ($$$1le a_ile n$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, dots,b_n$$$ ($$$1le b_ile n$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, dots,c_n$$$ ($$$1le c_ile n$$$). The next $$$m$$$ lines describe operations, the $$$i$$$-th line describes the $$$i$$$-th operation in one of these two formats: "$$$1 k x$$$" ($$$1le k,xle n$$$), representing an operation of the first type. "$$$2 r$$$" ($$$1le rle n$$$), representing an operation of the second type. It is guaranteed that there is at least one operation of the second type. Output For each operation of the second type print the answer. Example Input 5 4 1 2 3 4 5 2 3 4 5 1 5 1 2 3 4 2 5 1 2 3 2 4 2 5 Note For the first operation, the triplets are: $$$i=1$$$, $$$j=2$$$, $$$k=3$$$ $$$i=2$$$, $$$j=3$$$, $$$k=4$$$ $$$i=3$$$, $$$j=4$$$, $$$k=5$$$ There is no satisfying triplet for the third operation. For the fourth operation, the triplets are: $$$i=2$$$, $$$j=4$$$, $$$k=5$$$ $$$i=3$$$, $$$j=4$$$, $$$k=5$$$ | 3,500 | false | false | false | true | true | false | true | false | false | false | 2,916 |
1368A | Leo has developed a new programming language C+=. In C+=, integer variables can only be changed with a "+=" operation that adds the right-hand side value to the left-hand side variable. For example, performing "a += b" when a = $$$2$$$, b = $$$3$$$ changes the value of a to $$$5$$$ (the value of b does not change). In a prototype program Leo has two integer variables a and b, initialized with some positive values. He can perform any number of operations "a += b" or "b += a". Leo wants to test handling large integers, so he wants to make the value of either a or b strictly greater than a given value $$$n$$$. What is the smallest number of operations he has to perform? Input The first line contains a single integer $$$T$$$ ($$$1 leq T leq 100$$$)xa0— the number of test cases. Each of the following $$$T$$$ lines describes a single test case, and contains three integers $$$a, b, n$$$ ($$$1 leq a, b leq n leq 10^9$$$)xa0— initial values of a and b, and the value one of the variables has to exceed, respectively. Output For each test case print a single integerxa0— the smallest number of operations needed. Separate answers with line breaks. Note In the first case we cannot make a variable exceed $$$3$$$ in one operation. One way of achieving this in two operations is to perform "b += a" twice. | 800 | true | true | true | false | false | false | true | false | false | false | 3,836 |
1991I | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Pinely Round 4 (Div. 1 + Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms games graph matchings greedy interactive *3500 No tag edit access → Contest materials Announcement (en) Tutorial #1 (en) Tutorial #2 (en) Video Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST I. Grid Game time limit per test5 seconds memory limit per test256 megabytes This is an interactive problem. You are given a grid with $$$n$$$ rows and $$$m$$$ columns. You need to fill each cell with a unique integer from $$$1$$$ to $$$n cdot m$$$. After filling the grid, you will play a game on this grid against the interactor. Players take turns selecting one of the previously unselected cells from the grid, with the interactor going first. On the first turn, the interactor can choose any cell from the grid. After that, any chosen cell must be orthogonally adjacent to at least one previously selected cell. Two cells are considered orthogonally adjacent if they share an edge. The game continues until all cells have been selected. Your goal is to let the sum of numbers in the cells selected by you be strictly less than the sum of numbers in the cells selected by the interactor. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The description of test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$4 le n, m le 10$$$)xa0— the number of rows and columns in the grid. Interaction First, output $$$n$$$ lines, each containing $$$m$$$ integers, representing the integers that you filled in the grid. Each integer from $$$1$$$ to $$$n cdot m$$$ should appear exactly once. Then, the game begins. The interactor and you take turns outputting coordinates to select a cell for $$$n imes m$$$ turns, with the interactor starting first. On each player's (either you or the interactor) turn, the player will output two integers $$$i$$$ and $$$j$$$ ($$$1 le i le n$$$, $$$1 le j le m$$$), denoting that the player has selected the cell on the $$$i$$$-th row and $$$j$$$-th column. This cell should not have been selected in a previous round. Additionally, if it is not the first turn, the cell must be orthogonally adjacent to at least one previously selected cell. If any of your outputs are invalid, the jury will output "-1" and you will receive a Wrong Answer verdict. At the end of all $$$n cdot m$$$ turns, if the sum of numbers in the cells selected by you is not strictly less than the sum of numbers in the cells selected by the interactor, the jury will output "-1" and you will receive a Wrong Answer verdict. If your program has received a Wrong Answer verdict, it must terminate immediately. Otherwise, you may receive an arbitrary verdict because your solution might be reading from a closed stream. After outputting, do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. In this problem, hacks are disabled. Example input 1 4 4 3 4 4 4 4 2 4 1 1 4 1 2 2 2 2 1 output 2 3 4 10 12 6 11 15 5 13 16 8 9 7 1 14 2 4 4 3 3 3 3 1 1 3 1 1 2 3 3 2 Note Note that this is an example game and does not necessarily represent the optimal strategy for both players. First, we fill a $$$4 imes 4$$$ grid with unique integers from $$$1$$$ to $$$16$$$ in the following way: $$$2$$$ $$$3$$$ $$$4$$$ $$$10$$$ $$$12$$$ $$$6$$$ $$$11$$$ $$$15$$$ $$$5$$$ $$$13$$$ $$$16$$$ $$$8$$$ $$$9$$$ $$$7$$$ $$$1$$$ $$$14$$$ Next, the game begins. The interactor first selects $$$(3, 4)$$$, which is the number $$$8$$$. For this selection, the interactor could choose any number. From the next selection onwards, each chosen number has to be adjacent to any previously selected number. We select $$$(2, 4)$$$, which is the number $$$15$$$, adjacent to $$$(3, 4)$$$. The interactor selects $$$(4, 4)$$$, which is the number $$$14$$$, adjacent to $$$(3, 4)$$$. We select $$$(4, 3)$$$, which is the number $$$1$$$, adjacent to $$$(4, 4)$$$. $$$ldots$$$ This is continued until all numbers are selected. In the end, the numbers we selected were $$$[15, 1, 16, 5, 4, 2, 11, 13]$$$, and the numbers selected by the interactor were $$$[8, 14, 7, 9, 10, 3, 6, 12]$$$. The sum of the numbers we selected is $$$67$$$, which is less than the sum of the numbers selected by the interactor $$$69$$$. Therefore, we have won this game. Codeforces (c) | 3,500 | false | true | false | false | false | true | false | false | false | false | 320 |
1721F | You are given a bipartite graph with $$$n_1$$$ vertices in the first part, $$$n_2$$$ vertices in the second part, and $$$m$$$ edges. The maximum matching in this graph is the maximum possible (by size) subset of edges of this graph such that no vertex is incident to more than one chosen edge. You have to process two types of queries to this graph: $$$1$$$ — remove the minimum possible number of vertices from this graph so that the size of the maximum matching gets reduced exactly by $$$1$$$, and print the vertices that you have removed. Then, find any maximum matching in this graph and print the sum of indices of edges belonging to this matching; $$$2$$$ — query of this type will be asked only after a query of type $$$1$$$. As the answer to this query, you have to print the edges forming the maximum matching you have chosen in the previous query. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program. Input The first line contains four integers $$$n_1$$$, $$$n_2$$$, $$$m$$$ and $$$q$$$ ($$$1 le n_1, n_2 le 2 cdot 10^5$$$; $$$1 le m le min(n_1 cdot n_2, 2 cdot 10^5)$$$; $$$1 le q le 2 cdot 10^5$$$). Then $$$m$$$ lines follow. The $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le n_1$$$; $$$1 le y_i le n_2$$$) meaning that the $$$i$$$-th edge connects the vertex $$$x_i$$$ in the first part and the vertex $$$y_i$$$ in the second part. There are no pairs of vertices that are connected by more than one edge. Then $$$q$$$ lines follow. The $$$i$$$-th of them contains one integer, $$$1$$$ or $$$2$$$, denoting the $$$i$$$-th query. Additional constraints on queries: the number of queries of type $$$1$$$ won't exceed the size of the maximum matching in the initial graph; the number of queries of type $$$2$$$ won't exceed $$$3$$$; each query of type $$$2$$$ is preceded by a query of type $$$1$$$; your solution is allowed to read the $$$i$$$-th query only after printing the answer for the $$$(i-1)$$$-th query and flushing the output. Output For a query of type $$$1$$$, print the answer in three lines as follows: the first line should contain the number of vertices you remove; the second line should contain the indices of vertices you remove, as follows: if you remove the vertex $$$x$$$ from the left part, print $$$x$$$; if you remove the vertex $$$y$$$ from the right part, print $$$-y$$$ (negative index); the third line should contain the sum of indices of edges in some maximum matching in the resulting graph. The edges are numbered from $$$1$$$ to $$$m$$$. For a query of type $$$2$$$, print the answer in two lines as follows: the first line should contain the size of the maximum matching; the second line should contain the indices of the edges belonging to the maximum matching. Note that the sum of these indices should be equal to the number you printed at the end of the previous query of type $$$1$$$; After printing the answer to a query, don't forget to flush the output. Example Input 3 4 4 4 2 2 1 3 2 1 3 4 1 2 1 2 Output 1 -4 3 === 2 1 2 === 1 2 2 === 1 2 Note In this problem, you may receive the verdict "Idleness Limit Exceeded" since it is in online mode. If it happens, it means that either the output format is wrong, or you don't meet some constraint of the problem. You may treat this verdict as "Wrong Answer". For your convenience, the output for queries in the example is separated by the line ===. Don't print this line in your program, it is done only to make sure that it's easy to distinguish between answers for different queries in the statement. | 2,800 | false | false | false | false | false | true | true | false | false | true | 1,954 |
995E | Problem - 995E - Codeforces =============== xa0 ]( "Codeforces Round #492 . $$$p$$$ is guaranteed to be prime. Output On the first line, print a single integer $$$ell$$$, the number of button presses. On the second line, print integers $$$c_1, dots, c_ell$$$, the button presses. For $$$1 le i le ell$$$, $$$1 le c_i le 3$$$. We can show that the answer always exists. Examples Input 1 3 5 Output 2 1 1 Input 3 2 5 Output 1 3 Note In the first example the integer on the screen changes as $$$1 o 2 o 3$$$. In the second example the integer on the screen changes as $$$3 o 2$$$. | 2,700 | false | false | false | false | false | false | false | false | false | true | 5,722 |
1698A | There is an array $$$a$$$ with $$$n-1$$$ integers. Let $$$x$$$ be the , and then the elements are shuffled. You are given the newly formed array $$$a$$$. What is $$$x$$$? If there are multiple possible values of $$$x$$$, you can output any of them. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 100$$$)xa0— the number of integers in the resulting array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 127$$$)xa0— the elements of the newly formed array $$$a$$$. Additional constraint on the input: the array $$$a$$$ is made by the process described in the statement; that is, some value of $$$x$$$ exists. Output For each test case, output a single integerxa0— the value of $$$x$$$, as described in the statement. If there are multiple possible values of $$$x$$$, output any of them. Note In the first test case, one possible array $$$a$$$ is $$$a=[2, 5, 4]$$$. Then $$$x = 2 oplus 5 oplus 4 = 3$$$ ($$$oplus$$$ denotes the bitwise XOR), so the new array is $$$[2, 5, 4, 3]$$$. Afterwards, the array is shuffled to form $$$[4, 3, 2, 5]$$$. In the second test case, one possible array $$$a$$$ is $$$a=[1, 10, 6, 10]$$$. Then $$$x = 1 oplus 10 oplus 6 oplus 10 = 7$$$, so the new array is $$$[1, 10, 6, 10, 7]$$$. Afterwards, the array is shuffled to form $$$[6, 1, 10, 7, 10]$$$. In the third test case, all elements of the array are equal to $$$6$$$, so $$$x=6$$$. In the fourth test case, one possible array $$$a$$$ is $$$a=[100, 100]$$$. Then $$$x = 100 oplus 100 = 0$$$, so the new array is $$$[100, 100, 0]$$$. Afterwards, the array is shuffled to form $$$[100, 100, 0]$$$. (Note that after the shuffle, the array can remain the same.) | 800 | false | false | false | false | false | false | true | false | false | false | 2,094 |
1446D2 | This is the hard version of the problem. The difference between the versions is in the constraints on the array elements. You can make hacks only if all versions of the problem are solved. You are given an array $$$[a_1, a_2, dots, a_n]$$$. Your goal is to find the length of the longest subarray of this array such that the most frequent value in it is not unique. In other words, you are looking for a subarray such that if the most frequent value occurs $$$f$$$ times in this subarray, then at least $$$2$$$ different values should occur exactly $$$f$$$ times. An array $$$c$$$ is a subarray of an array $$$d$$$ if $$$c$$$ can be obtained from $$$d$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 200,000$$$)xa0— the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— elements of the array. Output You should output exactly one integer xa0— the length of the longest subarray of the array whose most frequent value is not unique. If there is no such subarray, output $$$0$$$. Examples Input 10 1 1 1 5 4 1 3 1 2 2 Note In the first sample, the subarray $$$[1, 1, 2, 2, 3, 3]$$$ is good, but $$$[1, 1, 2, 2, 3, 3, 3]$$$ isn't: in the latter there are $$$3$$$ occurrences of number $$$3$$$, and no other element appears $$$3$$$ times. | 3,000 | false | true | false | false | true | false | false | false | false | false | 3,443 |
1536D | Uh oh! Ray lost his array yet again! However, Omkar might be able to help because he thinks he has found the OmkArray of Ray's array. The OmkArray of an array $$$a$$$ with elements $$$a_1, a_2, ldots, a_{2k-1}$$$, is the array $$$b$$$ with elements $$$b_1, b_2, ldots, b_{k}$$$ such that $$$b_i$$$ is equal to the median of $$$a_1, a_2, ldots, a_{2i-1}$$$ for all $$$i$$$. Omkar has found an array $$$b$$$ of size $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$-10^9 leq b_i leq 10^9$$$). Given this array $$$b$$$, Ray wants to test Omkar's claim and see if $$$b$$$ actually is an OmkArray of some array $$$a$$$. Can you help Ray? The median of a set of numbers $$$a_1, a_2, ldots, a_{2i-1}$$$ is the number $$$c_{i}$$$ where $$$c_{1}, c_{2}, ldots, c_{2i-1}$$$ represents $$$a_1, a_2, ldots, a_{2i-1}$$$ sorted in nondecreasing order. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) — the length of the array $$$b$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$-10^9 leq b_i leq 10^9$$$) — the elements of $$$b$$$. It is guaranteed the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one line containing YES if there exists an array $$$a$$$ such that $$$b_i$$$ is the median of $$$a_1, a_2, dots, a_{2i-1}$$$ for all $$$i$$$, and NO otherwise. The case of letters in YES and NO do not matter (so yEs and No will also be accepted). Note In the second case of the first sample, the array $$$[4]$$$ will generate an OmkArray of $$$[4]$$$, as the median of the first element is $$$4$$$. In the fourth case of the first sample, the array $$$[3, 2, 5]$$$ will generate an OmkArray of $$$[3, 3]$$$, as the median of $$$3$$$ is $$$3$$$ and the median of $$$2, 3, 5$$$ is $$$3$$$. In the fifth case of the first sample, the array $$$[2, 1, 0, 3, 4, 4, 3]$$$ will generate an OmkArray of $$$[2, 1, 2, 3]$$$ as the median of $$$2$$$ is $$$2$$$ the median of $$$0, 1, 2$$$ is $$$1$$$ the median of $$$0, 1, 2, 3, 4$$$ is $$$2$$$ and the median of $$$0, 1, 2, 3, 3, 4, 4$$$ is $$$3$$$. In the second case of the second sample, the array $$$[1, 0, 4, 3, 5, -2, -2, -2, -4, -3, -4, -1, 5]$$$ will generate an OmkArray of $$$[1, 1, 3, 1, 0, -2, -1]$$$, as the median of $$$1$$$ is $$$1$$$ the median of $$$0, 1, 4$$$ is $$$1$$$ the median of $$$0, 1, 3, 4, 5$$$ is $$$3$$$ the median of $$$-2, -2, 0, 1, 3, 4, 5$$$ is $$$1$$$ the median of $$$-4, -2, -2, -2, 0, 1, 3, 4, 5$$$ is $$$0$$$ the median of $$$-4, -4, -3, -2, -2, -2, 0, 1, 3, 4, 5$$$ is $$$-2$$$ and the median of $$$-4, -4, -3, -2, -2, -2, -1, 0, 1, 3, 4, 5, 5$$$ is $$$-1$$$ For all cases where the answer is NO, it can be proven that it is impossible to find an array $$$a$$$ such that $$$b$$$ is the OmkArray of $$$a$$$. | 2,000 | false | true | true | false | true | false | false | false | false | false | 2,965 |
1172A | Nauuo is a girl who loves playing cards. One day she was playing cards but found that the cards were mixed with some empty ones. There are $$$n$$$ cards numbered from $$$1$$$ to $$$n$$$, and they were mixed with another $$$n$$$ empty cards. She piled up the $$$2n$$$ cards and drew $$$n$$$ of them. The $$$n$$$ cards in Nauuo's hands are given. The remaining $$$n$$$ cards in the pile are also given in the order from top to bottom. In one operation she can choose a card in her hands and play it — put it at the bottom of the pile, then draw the top card from the pile. Nauuo wants to make the $$$n$$$ numbered cards piled up in increasing order (the $$$i$$$-th card in the pile from top to bottom is the card $$$i$$$) as quickly as possible. Can you tell her the minimum number of operations? Input The first line contains a single integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$) — the number of numbered cards. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$0le a_ile n$$$) — the initial cards in Nauuo's hands. $$$0$$$ represents an empty card. The third line contains $$$n$$$ integers $$$b_1,b_2,ldots,b_n$$$ ($$$0le b_ile n$$$) — the initial cards in the pile, given in order from top to bottom. $$$0$$$ represents an empty card. It is guaranteed that each number from $$$1$$$ to $$$n$$$ appears exactly once, either in $$$a_{1..n}$$$ or $$$b_{1..n}$$$. Output The output contains a single integer — the minimum number of operations to make the $$$n$$$ numbered cards piled up in increasing order. Examples Input 11 0 0 0 5 0 0 0 4 0 0 11 9 2 6 0 8 1 7 0 3 0 10 Note Example 1 We can play the card $$$2$$$ and draw the card $$$3$$$ in the first operation. After that, we have $$$[0,3,0]$$$ in hands and the cards in the pile are $$$[0,1,2]$$$ from top to bottom. Then, we play the card $$$3$$$ in the second operation. The cards in the pile are $$$[1,2,3]$$$, in which the cards are piled up in increasing order. Example 2 Play an empty card and draw the card $$$1$$$, then play $$$1$$$, $$$2$$$, $$$3$$$ in order. | 1,800 | false | true | true | false | false | false | false | false | false | false | 4,860 |
740A | Little girl Alyona is in a shop to buy some copybooks for school. She study four subjects so she wants to have equal number of copybooks for each of the subjects. There are three types of copybook's packs in the shop: it is possible to buy one copybook for _a_ rubles, a pack of two copybooks for _b_ rubles, and a pack of three copybooks for _c_ rubles. Alyona already has _n_ copybooks. What is the minimum amount of rubles she should pay to buy such number of copybooks _k_ that _n_u2009+u2009_k_ is divisible by 4? There are infinitely many packs of any type in the shop. Alyona can buy packs of different type in the same purchase. Input The only line contains 4 integers _n_, _a_, _b_, _c_ (1u2009≤u2009_n_,u2009_a_,u2009_b_,u2009_c_u2009≤u2009109). Output Print the minimum amount of rubles she should pay to buy such number of copybooks _k_ that _n_u2009+u2009_k_ is divisible by 4. Examples Input 999999999 1000000000 1000000000 1000000000 Note In the first example Alyona can buy 3 packs of 1 copybook for 3_a_u2009=u20093 rubles in total. After that she will have 4 copybooks which she can split between the subjects equally. In the second example Alyuna can buy a pack of 2 copybooks for _b_u2009=u20091 ruble. She will have 8 copybooks in total. In the third example Alyona can split the copybooks she already has between the 4 subject equally, so she doesn't need to buy anything. In the fourth example Alyona should buy one pack of one copybook. | 1,300 | false | false | true | false | false | false | true | false | false | false | 6,838 |
1474A | In the $$$2022$$$ year, Mike found two binary integers $$$a$$$ and $$$b$$$ of length $$$n$$$ (both of them are written only by digits $$$0$$$ and $$$1$$$) that can have leading zeroes. In order not to forget them, he wanted to construct integer $$$d$$$ in the following way: he creates an integer $$$c$$$ as a result of bitwise summing of $$$a$$$ and $$$b$$$ without transferring carry, so $$$c$$$ may have one or more $$$2$$$-s. For example, the result of bitwise summing of $$$0110$$$ and $$$1101$$$ is $$$1211$$$ or the sum of $$$011000$$$ and $$$011000$$$ is $$$022000$$$; after that Mike replaces equal consecutive digits in $$$c$$$ by one digit, thus getting $$$d$$$. In the cases above after this operation, $$$1211$$$ becomes $$$121$$$ and $$$022000$$$ becomes $$$020$$$ (so, $$$d$$$ won't have equal consecutive digits). Unfortunately, Mike lost integer $$$a$$$ before he could calculate $$$d$$$ himself. Now, to cheer him up, you want to find any binary integer $$$a$$$ of length $$$n$$$ such that $$$d$$$ will be maximum possible as integer. Maximum possible as integer means that $$$102 > 21$$$, $$$012 < 101$$$, $$$021 = 21$$$ and so on. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains the integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the length of $$$a$$$ and $$$b$$$. The second line of each test case contains binary integer $$$b$$$ of length $$$n$$$. The integer $$$b$$$ consists only of digits $$$0$$$ and $$$1$$$. It is guaranteed that the total sum of $$$n$$$ over all $$$t$$$ test cases doesn't exceed $$$10^5$$$. Output For each test case output one binary integer $$$a$$$ of length $$$n$$$. Note, that $$$a$$$ or $$$b$$$ may have leading zeroes but must have the same length $$$n$$$. Example Input 5 1 0 3 011 3 110 6 111000 6 001011 Output 1 110 100 101101 101110 Note In the first test case, $$$b = 0$$$ and choosing $$$a = 1$$$ gives $$$d = 1$$$ as a result. In the second test case, $$$b = 011$$$ so: if you choose $$$a = 000$$$, $$$c$$$ will be equal to $$$011$$$, so $$$d = 01$$$; if you choose $$$a = 111$$$, $$$c$$$ will be equal to $$$122$$$, so $$$d = 12$$$; if you choose $$$a = 010$$$, you'll get $$$d = 021$$$. If you select $$$a = 110$$$, you'll get $$$d = 121$$$. We can show that answer $$$a = 110$$$ is optimal and $$$d = 121$$$ is maximum possible. In the third test case, $$$b = 110$$$. If you choose $$$a = 100$$$, you'll get $$$d = 210$$$ and it's the maximum possible $$$d$$$. In the fourth test case, $$$b = 111000$$$. If you choose $$$a = 101101$$$, you'll get $$$d = 212101$$$ and it's maximum possible $$$d$$$. In the fifth test case, $$$b = 001011$$$. If you choose $$$a = 101110$$$, you'll get $$$d = 102121$$$ and it's maximum possible $$$d$$$. | 800 | false | true | false | false | false | false | false | false | false | false | 3,310 |
1267L | Problem - 1267L - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms strings *1800 No tag edit access → Contest materials xa0— the total number of words, the length of each word, and the index of the word Lucy wants to minimize. The next line contains a string of $$$n cdot l$$$ lowercase letters of the English alphabet. Output Output $$$n$$$ words of $$$l$$$ letters each, one per line, using the letters from the input. Words must be sorted in the lexicographic order, and the $$$k$$$-th of them must be lexicographically as small as possible. If there are multiple answers with the smallest $$$k$$$-th word, output any of them. Examples Input 3 2 2 abcdef Output af bc ed Input 2 3 1 abcabc Output aab bcc | 1,800 | false | false | false | false | false | true | false | false | false | false | 4,352 |
1698B | There are $$$n$$$ piles of sand where the $$$i$$$-th pile has $$$a_i$$$ blocks of sand. The $$$i$$$-th pile is called too tall if $$$1 < i < n$$$ and $$$a_i > a_{i-1} + a_{i+1}$$$. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.) You are given an integer $$$k$$$. An operation consists of picking $$$k$$$ consecutive piles of sand and adding one unit of sand to them all. Formally, pick $$$1 leq l,r leq n$$$ such that $$$r-l+1=k$$$. Then for all $$$l leq i leq r$$$, update $$$a_i gets a_i+1$$$. What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations? Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 leq n leq 2 cdot 10^5$$$; $$$1 leq k leq n$$$)xa0— the number of piles of sand and the size of the operation, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the sizes of the piles. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integerxa0— the maximum number of piles that are simultaneously too tall after some (possibly zero) operations. Example Input 3 5 2 2 9 2 4 1 4 4 1 3 2 1 3 1 1 3 1 Note In the first test case, we can perform the following three operations: Add one unit of sand to piles $$$1$$$ and $$$2$$$: $$$[color{red}{3}, color{red}{10}, 2, 4, 1]$$$. Add one unit of sand to piles $$$4$$$ and $$$5$$$: $$$[3, 10, 2, color{red}{5}, color{red}{2}]$$$. Add one unit of sand to piles $$$3$$$ and $$$4$$$: $$$[3, 10, color{red}{3}, color{red}{6}, 2]$$$. Now piles $$$2$$$ and $$$4$$$ are too tall, so in this case the answer is $$$2$$$. It can be shown that it is impossible to make more than $$$2$$$ piles too tall. In the second test case, any operation will increase all piles by $$$1$$$ unit, so the number of too tall piles will always be $$$0$$$. In the third test case, we can increase any pile by $$$1$$$ unit of sand. It can be shown that the maximum number of too tall piles is $$$1$$$. | 800 | false | true | true | false | false | true | false | false | false | false | 2,093 |
610B | Vika has _n_ jars with paints of distinct colors. All the jars are numbered from 1 to _n_ and the _i_-th jar contains _a__i_ liters of paint of color _i_. Vika also has an infinitely long rectangular piece of paper of width 1, consisting of squares of size 1u2009×u20091. Squares are numbered 1, 2, 3 and so on. Vika decided that she will start painting squares one by one from left to right, starting from the square number 1 and some arbitrary color. If the square was painted in color _x_, then the next square will be painted in color _x_u2009+u20091. In case of _x_u2009=u2009_n_, next square is painted in color 1. If there is no more paint of the color Vika wants to use now, then she stops. Square is always painted in only one color, and it takes exactly 1 liter of paint. Your task is to calculate the maximum number of squares that might be painted, if Vika chooses right color to paint the first square. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of jars with colors Vika has. The second line of the input contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is equal to the number of liters of paint in the _i_-th jar, i.e. the number of liters of color _i_ that Vika has. Output The only line of the output should contain a single integerxa0— the maximum number of squares that Vika can paint if she follows the rules described above. Note In the first sample the best strategy is to start painting using color 4. Then the squares will be painted in the following colors (from left to right): 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5. In the second sample Vika can start to paint using any color. In the third sample Vika should start painting using color number 5. | 1,300 | false | false | true | false | false | true | false | false | false | false | 7,405 |
1981D | Turtle just learned how to multiply two integers in his math class, and he was very excited. Then Piggy gave him an integer $$$n$$$, and asked him to construct a sequence $$$a_1, a_2, ldots, a_n$$$ consisting of integers which satisfied the following conditions: For all $$$1 le i le n$$$, $$$1 le a_i le 3 cdot 10^5$$$. For all $$$1 le i < j le n - 1$$$, $$$a_i cdot a_{i + 1} e a_j cdot a_{j + 1}$$$. Of all such sequences, Piggy asked Turtle to find the one with the minimum number of distinct elements. Turtle definitely could not solve the problem, so please help him! Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^6$$$) — the length of the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ — the elements of the sequence $$$a$$$. If there are multiple answers, print any of them. Example Output 114514 114514 1 2 2 3 3 4 4 Note In the third test case, $$$a = [3, 4, 2, 6]$$$ violates the second condition since $$$a_1 cdot a_2 = a_3 cdot a_4$$$. $$$a = [2, 3, 4, 4]$$$ satisfy the conditions but its number of distinct elements isn't minimum. | 2,400 | false | false | false | false | false | true | false | false | false | true | 399 |
1210A | Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every $$$a$$$ and $$$b$$$ such that $$$1 leq a leq b leq 6$$$, there is exactly one domino with $$$a$$$ dots on one half and $$$b$$$ dots on the other half. The set contains exactly $$$21$$$ dominoes. Here is an exact illustration of his set: Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph. When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots. How many dominoes at most can Anadi place on the edges of his graph? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 7$$$, $$$0 leq m leq frac{ncdot(n-1)}{2}$$$) — the number of vertices and the number of edges in the graph. The next $$$m$$$ lines contain two integers each. Integers in the $$$i$$$-th line are $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a, b leq n$$$, $$$a eq b$$$) and denote that there is an edge which connects vertices $$$a_i$$$ and $$$b_i$$$. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices. Output Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph. Examples Input 7 21 1 2 1 3 1 4 1 5 1 6 1 7 2 3 2 4 2 5 2 6 2 7 3 4 3 5 3 6 3 7 4 5 4 6 4 7 5 6 5 7 6 7 Note Here is an illustration of Anadi's graph from the first sample test: And here is one of the ways to place a domino on each of its edges: Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex $$$1$$$ have three dots. | 1,700 | false | false | false | false | false | false | true | false | false | true | 4,636 |
1415C | You're creating a game level for some mobile game. The level should contain some number of cells aligned in a row from left to right and numbered with consecutive integers starting from $$$1$$$, and in each cell you can either put a platform or leave it empty. In order to pass a level, a player must throw a ball from the left so that it first lands on a platform in the cell $$$p$$$, then bounces off it, then bounces off a platform in the cell $$$(p + k)$$$, then a platform in the cell $$$(p + 2k)$$$, and so on every $$$k$$$-th platform until it goes farther than the last cell. If any of these cells has no platform, you can't pass the level with these $$$p$$$ and $$$k$$$. You already have some level pattern $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, ..., $$$a_n$$$, where $$$a_i = 0$$$ means there is no platform in the cell $$$i$$$, and $$$a_i = 1$$$ means there is one. You want to modify it so that the level can be passed with given $$$p$$$ and $$$k$$$. In $$$x$$$ seconds you can add a platform in some empty cell. In $$$y$$$ seconds you can remove the first cell completely, reducing the number of cells by one, and renumerating the other cells keeping their order. You can't do any other operation. You can not reduce the number of cells to less than $$$p$$$. Illustration for the third example test case. Crosses mark deleted cells. Blue platform is the newly added. What is the minimum number of seconds you need to make this level passable with given $$$p$$$ and $$$k$$$? Input The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$p$$$, and $$$k$$$ ($$$1 le p le n le 10^5$$$, $$$1 le k le n$$$)xa0— the number of cells you have, the first cell that should contain a platform, and the period of ball bouncing required. The second line of each test case contains a string $$$a_1 a_2 a_3 ldots a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$)xa0— the initial pattern written without spaces. The last line of each test case contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le 10^4$$$)xa0— the time required to add a platform and to remove the first cell correspondingly. The sum of $$$n$$$ over test cases does not exceed $$$10^5$$$. Output For each test case output a single integerxa0— the minimum number of seconds you need to modify the level accordingly. It can be shown that it is always possible to make the level passable. Example Input 3 10 3 2 0101010101 2 2 5 4 1 00000 2 10 11 2 3 10110011000 4 3 Note In the first test case it's best to just remove the first cell, after that all required platforms are in their places: 0101010101. The stroked out digit is removed, the bold ones are where platforms should be located. The time required is $$$y = 2$$$. In the second test case it's best to add a platform to both cells $$$4$$$ and $$$5$$$: 00000 $$$ o$$$ 00011. The time required is $$$x cdot 2 = 4$$$. In the third test case it's best to to remove the first cell twice and then add a platform to the cell which was initially $$$10$$$-th: 10110011000 $$$ o$$$ 10110011010. The time required is $$$y cdot 2 + x = 10$$$. | 1,400 | false | false | true | true | false | false | true | false | false | false | 3,606 |
271B | You've got an _n_u2009×u2009_m_ matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times. You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not. A matrix is prime if at least one of the two following conditions fulfills: the matrix has a row with prime numbers only; the matrix has a column with prime numbers only; Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got. Input The first line contains two integers _n_,u2009_m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009500) — the number of rows and columns in the matrix, correspondingly. Each of the following _n_ lines contains _m_ integers — the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105. The numbers in the lines are separated by single spaces. Output Print a single integer — the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0. Note In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3. In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2. In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2. | 1,300 | true | false | false | false | false | false | true | true | false | false | 8,750 |
1550E | You are given a string $$$s$$$ of length $$$n$$$. Each character is either one of the first $$$k$$$ lowercase Latin letters or a question mark. You are asked to replace every question mark with one of the first $$$k$$$ lowercase Latin letters in such a way that the following value is maximized. Let $$$f_i$$$ be the maximum length substring of string $$$s$$$, which consists entirely of the $$$i$$$-th Latin letter. A substring of a string is a contiguous subsequence of that string. If the $$$i$$$-th letter doesn't appear in a string, then $$$f_i$$$ is equal to $$$0$$$. The value of a string $$$s$$$ is the minimum value among $$$f_i$$$ for all $$$i$$$ from $$$1$$$ to $$$k$$$. What is the maximum value the string can have? Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le k le 17$$$)xa0— the length of the string and the number of first Latin letters used. The second line contains a string $$$s$$$, consisting of $$$n$$$ characters. Each character is either one of the first $$$k$$$ lowercase Latin letters or a question mark. Output Print a single integerxa0— the maximum value of the string after every question mark is replaced with one of the first $$$k$$$ lowercase Latin letters. Examples Input 15 3 ??b?babbc??b?aa Note In the first example the question marks can be replaced in the following way: "aaaababbbb". $$$f_1 = 4$$$, $$$f_2 = 4$$$, thus the answer is $$$4$$$. Replacing it like this is also possible: "aaaabbbbbb". That way $$$f_1 = 4$$$, $$$f_2 = 6$$$, however, the minimum of them is still $$$4$$$. In the second example one of the possible strings is "aabbccdda". In the third example at least one letter won't appear in the string, thus, the minimum of values $$$f_i$$$ is always $$$0$$$. | 2,500 | false | false | false | true | false | false | true | true | false | false | 2,894 |
67C | You are given a sequence of balls _A_ by your teacher, each labeled with a lowercase Latin letter 'a'-'z'. You don't like the given sequence. You want to change it into a new sequence, _B_ that suits you better. So, you allow yourself four operations: You can insert any ball with any label into the sequence at any position. You can delete (remove) any ball from any position. You can replace any ball with any other ball. You can exchange (swap) two adjacent balls. Your teacher now places time constraints on each operation, meaning that an operation can only be performed in certain time. So, the first operation takes time _t__i_, the second one takes _t__d_, the third one takes _t__r_ and the fourth one takes _t__e_. Also, it is given that 2·_t__e_u2009≥u2009_t__i_u2009+u2009_t__d_. Find the minimal time to convert the sequence _A_ to the sequence _B_. Input The first line contains four space-separated integers _t__i_,u2009_t__d_,u2009_t__r_,u2009_t__e_ (0u2009<u2009_t__i_,u2009_t__d_,u2009_t__r_,u2009_t__e_u2009≤u2009100). The following two lines contain sequences _A_ and _B_ on separate lines. The length of each line is between 1 and 4000 characters inclusive. Output Print a single integer representing minimum time to convert _A_ into _B_. Examples Input 1 1 1 1 youshouldnot thoushaltnot Note In the second sample, you could delete the ball labeled 'a' from the first position and then insert another 'a' at the new second position with total time 6. However exchanging the balls give total time 3. | 2,600 | false | false | false | true | false | false | false | false | false | false | 9,634 |
1029D | You are given an array $$$a$$$, consisting of $$$n$$$ positive integers. Let's call a concatenation of numbers $$$x$$$ and $$$y$$$ the number that is obtained by writing down numbers $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, a concatenation of numbers $$$12$$$ and $$$3456$$$ is a number $$$123456$$$. Count the number of ordered pairs of positions $$$(i, j)$$$ ($$$i eq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$2 le k le 10^9$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). Output Print a single integer — the number of ordered pairs of positions $$$(i, j)$$$ ($$$i eq j$$$) in array $$$a$$$ such that the concatenation of $$$a_i$$$ and $$$a_j$$$ is divisible by $$$k$$$. Note In the first example pairs $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 1)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ suffice. They produce numbers $$$451$$$, $$$4510$$$, $$$110$$$, $$$1045$$$, $$$1012$$$, $$$121$$$, $$$1210$$$, respectively, each of them is divisible by $$$11$$$. In the second example all $$$n(n - 1)$$$ pairs suffice. In the third example no pair is sufficient. | 1,900 | true | false | true | false | false | false | false | false | false | false | 5,558 |
380A | Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm. Sereja takes a blank piece of paper. Then he starts writing out the sequence in _m_ stages. Each time he either adds a new number to the end of the sequence or takes _l_ first elements of the current sequence and adds them _c_ times to the end. More formally, if we represent the current sequence as _a_1,u2009_a_2,u2009...,u2009_a__n_, then after we apply the described operation, the sequence transforms into _a_1,u2009_a_2,u2009...,u2009_a__n_[,u2009_a_1,u2009_a_2,u2009...,u2009_a__l_] (the block in the square brackets must be repeated _c_ times). A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja. Input The first line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of stages to build a sequence. Next _m_ lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer _x__i_ (1u2009≤u2009_x__i_u2009≤u2009105) — the number to add. Type 2 means copying a prefix of length _l__i_ to the end _c__i_ times, in this case the line further contains two integers _l__i_,u2009_c__i_ (1u2009≤u2009_l__i_u2009≤u2009105,u20091u2009≤u2009_c__i_u2009≤u2009104), _l__i_ is the length of the prefix, _c__i_ is the number of copyings. It is guaranteed that the length of prefix _l__i_ is never larger than the current length of the sequence. The next line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1 from the beginning to the end of the sequence. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Output Print the elements that Sereja is interested in, in the order in which their numbers occur in the input. Examples Input 6 1 1 1 2 2 2 1 1 3 2 5 2 1 4 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output 1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4 | 1,600 | false | false | false | false | false | false | true | true | false | false | 8,310 |
1076F | Vova has taken his summer practice this year and now he should write a report on how it went. Vova has already drawn all the tables and wrote down all the formulas. Moreover, he has already decided that the report will consist of exactly $$$n$$$ pages and the $$$i$$$-th page will include $$$x_i$$$ tables and $$$y_i$$$ formulas. The pages are numbered from $$$1$$$ to $$$n$$$. Vova fills the pages one after another, he can't go filling page $$$i + 1$$$ before finishing page $$$i$$$ and he can't skip pages. However, if he draws strictly more than $$$k$$$ tables in a row or writes strictly more than $$$k$$$ formulas in a row then he will get bored. Vova wants to rearrange tables and formulas in each page in such a way that he doesn't get bored in the process. Vova can't move some table or some formula to another page. Note that the count doesn't reset on the start of the new page. For example, if the page ends with $$$3$$$ tables and the next page starts with $$$5$$$ tables, then it's counted as $$$8$$$ tables in a row. Help Vova to determine if he can rearrange tables and formulas on each page in such a way that there is no more than $$$k$$$ tables in a row and no more than $$$k$$$ formulas in a row. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 3 cdot 10^5$$$, $$$1 le k le 10^6$$$). The second line contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 le x_i le 10^6$$$) — the number of tables on the $$$i$$$-th page. The third line contains $$$n$$$ integers $$$y_1, y_2, dots, y_n$$$ ($$$1 le y_i le 10^6$$$) — the number of formulas on the $$$i$$$-th page. Output Print "YES" if Vova can rearrange tables and formulas on each page in such a way that there is no more than $$$k$$$ tables in a row and no more than $$$k$$$ formulas in a row. Otherwise print "NO". Examples Input 4 1 4 1 10 1 3 2 10 1 Note In the first example the only option to rearrange everything is the following (let table be 'T' and formula be 'F'): page $$$1$$$: "TTFTTFT" page $$$2$$$: "TFTTFTT" That way all blocks of tables have length $$$2$$$. In the second example there is no way to fit everything in such a way that there are no more than $$$2$$$ tables in a row and $$$2$$$ formulas in a row. | 2,500 | false | true | false | true | false | false | false | false | false | false | 5,339 |
1324F | You are given a tree consisting of $$$n$$$ vertices. A tree is a connected undirected graph with $$$n-1$$$ edges. Each vertex $$$v$$$ of this tree has a color assigned to it ($$$a_v = 1$$$ if the vertex $$$v$$$ is white and $$$0$$$ if the vertex $$$v$$$ is black). You have to solve the following problem for each vertex $$$v$$$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $$$v$$$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $$$cnt_w$$$ white vertices and $$$cnt_b$$$ black vertices, you have to maximize $$$cnt_w - cnt_b$$$. Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of vertices in the tree. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 1$$$), where $$$a_i$$$ is the color of the $$$i$$$-th vertex. Each of the next $$$n-1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the labels of vertices it connects $$$(1 le u_i, v_i le n, u_i e v_i$$$). It is guaranteed that the given edges form a tree. Output Print $$$n$$$ integers $$$res_1, res_2, dots, res_n$$$, where $$$res_i$$$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $$$i$$$. Examples Input 9 0 1 1 1 0 0 0 0 1 1 2 1 3 3 4 3 5 2 6 4 7 6 8 5 9 Input 4 0 0 1 0 1 2 1 3 1 4 Note The first example is shown below: The black vertices have bold borders. In the second example, the best subtree for vertices $$$2, 3$$$ and $$$4$$$ are vertices $$$2, 3$$$ and $$$4$$$ correspondingly. And the best subtree for the vertex $$$1$$$ is the subtree consisting of vertices $$$1$$$ and $$$3$$$. | 1,800 | false | false | false | true | false | false | false | false | false | true | 4,094 |
370D | Innocentius has a problem — his computer monitor has broken. Now some of the pixels are "dead", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus. Innocentius is touch-typing a program that paints a white square one-pixel wide frame on the black screen. As the monitor is broken, some pixels that should be white remain black. Polycarpus should look at what the program displayed on the screen and guess the position and size of the frame Innocentius has painted. Polycarpus doesn't like the game but Innocentius persuaded brother to play as "the game is good for the imagination and attention". Help Polycarpus, automatize his part in the gaming process. Write the code that finds such possible square frame that: the frame's width is 1 pixel, the frame doesn't go beyond the borders of the screen, all white pixels of the monitor are located on the frame, of all frames that satisfy the previous three conditions, the required frame must have the smallest size. Formally, a square frame is represented by such pixels of the solid square, that are on the square's border, that is, are not fully surrounded by the other pixels of the square. For example, if the frame's size is _d_u2009=u20093, then it consists of 8 pixels, if its size is _d_u2009=u20092, then it contains 4 pixels and if _d_u2009=u20091, then the frame is reduced to a single pixel. Input The first line contains the resolution of the monitor as a pair of integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092000). The next _n_ lines contain exactly _m_ characters each — the state of the monitor pixels at the moment of the game. Character "." (period, ASCII code 46) corresponds to the black pixel, and character "w" (lowercase English letter w) corresponds to the white pixel. It is guaranteed that at least one pixel of the monitor is white. Output Print the monitor screen. Represent the sought frame by characters "+" (the "plus" character). The pixels that has become white during the game mustn't be changed. Print them as "w". If there are multiple possible ways to position the frame of the minimum size, print any of them. If the required frame doesn't exist, then print a single line containing number -1. Note In the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist. | 2,100 | false | true | true | false | false | true | true | false | false | false | 8,350 |
1107F | Vasya wants to buy himself a nice new car. Unfortunately, he lacks some money. Currently he has exactly 0 burles. However, the local bank has $$$n$$$ credit offers. Each offer can be described with three numbers $$$a_i$$$, $$$b_i$$$ and $$$k_i$$$. Offers are numbered from $$$1$$$ to $$$n$$$. If Vasya takes the $$$i$$$-th offer, then the bank gives him $$$a_i$$$ burles at the beginning of the month and then Vasya pays bank $$$b_i$$$ burles at the end of each month for the next $$$k_i$$$ months (including the month he activated the offer). Vasya can take the offers any order he wants. Each month Vasya can take no more than one credit offer. Also each credit offer can not be used more than once. Several credits can be active at the same time. It implies that Vasya pays bank the sum of $$$b_i$$$ over all the $$$i$$$ of active credits at the end of each month. Vasya wants to buy a car in the middle of some month. He just takes all the money he currently has and buys the car of that exact price. Vasya don't really care what he'll have to pay the bank back after he buys a car. He just goes out of the country on his car so that the bank can't find him anymore. What is the maximum price that car can have? Input The first line contains one integer $$$n$$$ ($$$1 le n le 500$$$) — the number of credit offers. Each of the next $$$n$$$ lines contains three integers $$$a_i$$$, $$$b_i$$$ and $$$k_i$$$ ($$$1 le a_i, b_i, k_i le 10^9$$$). Output Print one integer — the maximum price of the car. Examples Input 4 10 9 2 20 33 1 30 115 1 5 3 2 Input 3 40 1 2 1000 1100 5 300 2 1 Note In the first example, the following sequence of offers taken is optimal: 4 $$$ ightarrow$$$ 3. The amount of burles Vasya has changes the following way: 5 $$$ ightarrow$$$ 32 $$$ ightarrow$$$ -86 $$$ ightarrow$$$ .... He takes the money he has in the middle of the second month (32 burles) and buys the car. The negative amount of money means that Vasya has to pay the bank that amount of burles. In the second example, the following sequence of offers taken is optimal: 3 $$$ ightarrow$$$ 1 $$$ ightarrow$$$ 2. The amount of burles Vasya has changes the following way: 0 $$$ ightarrow$$$ 300 $$$ ightarrow$$$ 338 $$$ ightarrow$$$ 1337 $$$ ightarrow$$$ 236 $$$ ightarrow$$$ -866 $$$ ightarrow$$$ .... | 2,600 | false | false | false | true | false | false | false | false | true | true | 5,164 |
1214G | Recently biologists came to a fascinating conclusion about how to find a chameleon mood. Consider chameleon body to be a rectangular table $$$n imes m$$$, each cell of which may be green or blue and may change between these two colors. We will denote as $$$(x, y)$$$ ($$$1 leq x leq n$$$, $$$1 leq y leq m$$$) the cell in row $$$x$$$ and column $$$y$$$. Let us define a chameleon good mood certificate to be four cells which are corners of some subrectangle of the table, such that colors in opposite cells among these four are similar, and at the same time not all of the four cell colors are similar. Formally, it is a group of four cells $$$(x_1, y_1)$$$, $$$(x_1, y_2)$$$, $$$(x_2, y_1)$$$, $$$(x_2, y_2)$$$ for some $$$1 leq x_1 < x_2 leq n$$$, $$$1 leq y_1 < y_2 leq m$$$, that colors of $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ coincide and colors of $$$(x_1, y_2)$$$ and $$$(x_2, y_1)$$$ coincide, but not all of the four cells share the same color. It was found that whenever such four cells are present, chameleon is in good mood, and vice versa: if there are no such four cells, chameleon is in bad mood. You are asked to help scientists write a program determining the mood of chameleon. Let us consider that initially all cells of chameleon are green. After that chameleon coloring may change several times. On one change, colors of contiguous segment of some table row are replaced with the opposite. Formally, each color change is defined by three integers $$$a$$$, $$$l$$$, $$$r$$$ ($$$1 leq a leq n$$$, $$$1 leq l leq r leq m$$$). On such change colors of all cells $$$(a, b)$$$ such that $$$l leq b leq r$$$ are replaced with the opposite. Write a program that reports mood of the chameleon after each change. Additionally, if the chameleon mood is good, program should find out any four numbers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ such that four cells $$$(x_1, y_1)$$$, $$$(x_1, y_2)$$$, $$$(x_2, y_1)$$$, $$$(x_2, y_2)$$$ are the good mood certificate. Input The first line of input contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$1 leq n, m leq 2000$$$, $$$1 leq q leq 500,000$$$), the sizes of the table and the number of changes respectively. Each of the following $$$q$$$ lines contains 3 integers $$$a_i$$$, $$$l_i$$$, $$$r_i$$$ ($$$1 leq a_i leq n$$$, $$$1 leq l_i leq r_i leq m$$$), describing $$$i$$$-th coloring change. Output Print $$$q$$$ lines. In the $$$i$$$-th line report the chameleon mood after first $$$i$$$ color changes for all $$$1 leq i leq q$$$. If chameleon is in bad mood, print the only integer $$$-1$$$. Otherwise, print four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$1 leq x_1 < x_2 leq n$$$, $$$1 leq y_1 < y_2 leq m$$$) such that four cells $$$(x_1, y_1)$$$, $$$(x_1, y_2)$$$, $$$(x_2, y_1)$$$, $$$(x_2, y_2)$$$ are the good mood certificate. If there are several ways to choose such four integers, print any valid one. | 3,200 | false | false | false | false | true | false | false | false | false | false | 4,605 |
1999D | Slavic has a very tough exam and needs your help in order to pass it. Here is the question he is struggling with: There exists a string $$$s$$$, which consists of lowercase English letters and possibly zero or more "?". Slavic is asked to change each "?" to a lowercase English letter such that string $$$t$$$ becomes a subsequence (not necessarily continuous) of the string $$$s$$$. Output any such string, or say that it is impossible in case no string that respects the conditions exists. Input The first line contains a single integer $$$T$$$ ($$$1 leq T leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single string $$$s$$$ ($$$1 leq s leq 2 cdot 10^5$$$, and $$$s$$$ consists only of lowercase English letters and "?"-s) xa0– the original string you have. The second line of each test case contains a single string $$$t$$$ ($$$1 leq t leq s$$$, and $$$t$$$ consists only of lowercase English letters) xa0– the string that should be a subsequence of string $$$s$$$. The sum of $$$s$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$, where $$$x$$$ denotes the length of the string $$$x$$$. Output For each test case, if no such string exists as described in the statement, output "NO" (without quotes). Otherwise, output "YES" (without quotes). Then, output one linexa0— the string that respects all conditions. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response). If multiple answers are possible, you can output any of them. Example Input 5 ????? xbx ab??e abcde ayy?x a ab??e dac paiu mom Output YES xabax YES abcde YES ayyyx NO NO | 1,100 | false | true | true | false | false | false | false | false | false | false | 268 |
79D | Finally Fox Ciel arrived in front of her castle! She have to type a password to enter her castle. An input device attached to her castle is a bit unusual. The input device is a 1u2009×u2009_n_ rectangle divided into _n_ square panels. They are numbered 1 to _n_ from left to right. Each panel has a state either ON or OFF. Initially all panels are in the OFF state. She can enter her castle if and only if _x_1-th, _x_2-th, ..., _x__k_-th panels are in the ON state and other panels are in the OFF state. She is given an array _a_1, ..., _a__l_. In each move, she can perform the following operation: choose an index _i_ (1u2009≤u2009_i_u2009≤u2009_l_), choose consecutive _a__i_ panels, and flip the states of those panels (i.e. ONu2009→u2009OFF, OFFu2009→u2009ON). Unfortunately she forgets how to type the password with only above operations. Determine the minimal number of operations required to enter her castle. Input The first line contains three integers _n_, _k_ and _l_ (1u2009≤u2009_n_u2009≤u200910000,u20091u2009≤u2009_k_u2009≤u200910,u20091u2009≤u2009_l_u2009≤u2009100), separated by single spaces. The second line contains _k_ integers _x_1, ..., _x__k_ (1u2009≤u2009_x_1u2009<u2009_x_2u2009<u2009...u2009<u2009_x__k_u2009≤u2009_n_), separated by single spaces. The third line contains _l_ integers _a_1, ..., _a__l_ (1u2009≤u2009_a__i_u2009≤u2009_n_), separated by single spaces. It is possible that some elements of the array _a__i_ are equal value. Output Print the minimal number of moves required to type the password. If it's impossible, print -1. Examples Input 10 8 2 1 2 3 5 6 7 8 9 3 5 Note One possible way to type the password in the first example is following: In the first move, choose 1st, 2nd, 3rd panels and flip those panels. In the second move, choose 5th, 6th, 7th, 8th, 9th panels and flip those panels. | 2,800 | false | false | false | true | false | false | false | false | false | false | 9,567 |
821A | Okabe needs to renovate the Future Gadget Laboratory after he tried doing some crazy experiments! The lab is represented as an _n_ by _n_ square grid of integers. A good lab is defined as a lab in which every number not equal to 1 can be expressed as the sum of a number in the same row and a number in the same column. In other words, for every _x_,u2009_y_ such that 1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_ and _a__x_,u2009_y_u2009≠u20091, there should exist two indices _s_ and _t_ so that _a__x_,u2009_y_u2009=u2009_a__x_,u2009_s_u2009+u2009_a__t_,u2009_y_, where _a__i_,u2009_j_ denotes the integer in _i_-th row and _j_-th column. Help Okabe determine whether a given lab is good! Input The first line of input contains the integer _n_ (1u2009≤u2009_n_u2009≤u200950)xa0— the size of the lab. The next _n_ lines contain _n_ space-separated integers denoting a row of the grid. The _j_-th integer in the _i_-th row is _a__i_,u2009_j_ (1u2009≤u2009_a__i_,u2009_j_u2009≤u2009105). Output Print "Yes" if the given lab is good and "No" otherwise. You can output each letter in upper or lower case. Note In the first sample test, the 6 in the bottom left corner is valid because it is the sum of the 2 above it and the 4 on the right. The same holds for every number not equal to 1 in this table, so the answer is "Yes". In the second sample test, the 5 cannot be formed as the sum of an integer in the same row and an integer in the same column. Thus the answer is "No". | 800 | false | false | true | false | false | false | false | false | false | false | 6,473 |
1995D | You're a linguist studying a mysterious ancient language. You know that 1. Its words consist only of the first $$$c$$$ letters of the Latin alphabet. 2. Each word has a case which can be unambiguously determined by its last letter (different letters correspond to different cases). For example, words "ABACABA" and "ABA" (if they exist) have the same case in this language because they both have the same ending 'A', whereas "ALICE" and "BOB" have different cases. If the language does not have a case corresponding to some letter, it means that the word cannot end with this letter. 3. The length of each word is $$$k$$$ or less. You have a single text written in this language. Unfortunately, as the language is really ancient, spaces between words are missing and all letters are uppercase. You wonder what is the minimum number of cases the language can have. Can you find this out? Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0— the number of test cases. It is followed by descriptions of the test cases. The first line of each test case contains three integers $$$n$$$, $$$c$$$, $$$k$$$ ($$$1 le k le n le 2^{18}$$$, $$$1 le c le 18$$$)xa0— the length of the text, the number of letters in the language, and the maximum length of the word. The second line contains a string of $$$n$$$ charactersxa0— the text itself. Each character is one of the first $$$c$$$ uppercase letters of the Latin alphabet. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2^{18}$$$ and the sum of $$$2^c$$$ over all test cases does not exceed $$$2^{18}$$$. Output For each test case, output a single line consisting of a single integerxa0— the minimum number of cases in the language. Example Input 7 5 5 1 ABCDE 3 1 2 AAA 3 2 2 AAB 10 2 2 ABABABABAB 4 4 4 DCBA 1 17 1 Q 9 3 2 ABCABCABC Note In the first test case, there must be five cases in the language (for each of the letters 'A', 'B', 'C', 'D', and 'E' there must be a case that has a corresponding ending). In the fourth test case, one case with ending 'B' is sufficient. | 2,300 | false | false | false | true | false | false | true | false | false | false | 293 |
1443E | A permutation is a sequence of integers from $$$1$$$ to $$$n$$$ of length $$$n$$$ containing each number exactly once. For example, $$$[1]$$$, $$$[4, 3, 5, 1, 2]$$$, $$$[3, 2, 1]$$$xa0— are permutations, and $$$[1, 1]$$$, $$$[4, 3, 1]$$$, $$$[2, 3, 4]$$$xa0— no. Permutation $$$a$$$ is lexicographically smaller than permutation $$$b$$$ (they have the same length $$$n$$$), if in the first index $$$i$$$ in which they differ, $$$a[i] < b[i]$$$. For example, the permutation $$$[1, 3, 2, 4]$$$ is lexicographically smaller than the permutation $$$[1, 3, 4, 2]$$$, because the first two elements are equal, and the third element in the first permutation is smaller than in the second. The next permutation for a permutation $$$a$$$ of length $$$n$$$xa0— is the lexicographically smallest permutation $$$b$$$ of length $$$n$$$ that lexicographically larger than $$$a$$$. For example: for permutation $$$[2, 1, 4, 3]$$$ the next permutation is $$$[2, 3, 1, 4]$$$; for permutation $$$[1, 2, 3]$$$ the next permutation is $$$[1, 3, 2]$$$; for permutation $$$[2, 1]$$$ next permutation does not exist. You are given the number $$$n$$$xa0— the length of the initial permutation. The initial permutation has the form $$$a = [1, 2, ldots, n]$$$. In other words, $$$a[i] = i$$$ ($$$1 le i le n$$$). You need to process $$$q$$$ queries of two types: $$$1$$$ $$$l$$$ $$$r$$$: query for the sum of all elements on the segment $$$[l, r]$$$. More formally, you need to find $$$a[l] + a[l + 1] + ldots + a[r]$$$. $$$2$$$ $$$x$$$: $$$x$$$ times replace the current permutation with the next permutation. For example, if $$$x=2$$$ and the current permutation has the form $$$[1, 3, 4, 2]$$$, then we should perform such a chain of replacements $$$[1, 3, 4, 2] ightarrow [1, 4, 2, 3] ightarrow [1, 4, 3, 2]$$$. For each query of the $$$1$$$-st type output the required sum. Input The first line contains two integers $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) and $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$), where $$$n$$$xa0— the length of the initial permutation, and $$$q$$$xa0— the number of queries. The next $$$q$$$ lines contain a single query of the $$$1$$$-st or $$$2$$$-nd type. The $$$1$$$-st type query consists of three integers $$$1$$$, $$$l$$$ and $$$r$$$ $$$(1 le l le r le n)$$$, the $$$2$$$-nd type query consists of two integers $$$2$$$ and $$$x$$$ $$$(1 le x le 10^5)$$$. It is guaranteed that all requests of the $$$2$$$-nd type are possible to process. | 2,400 | true | false | false | false | false | false | true | false | false | false | 3,455 |
1605D | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 754 (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 bitmasks constructive algorithms dfs and similar games greedy implementation trees *2100 No tag edit access → Contest materials announcement (en) tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. Treelabeling time limit per test2 seconds memory limit per test256 megabytes Eikooc and Sushi play a game. The game is played on a tree having $$$n$$$ nodes numbered $$$1$$$ to $$$n$$$. Recall that a tree having $$$n$$$ nodes is an undirected, connected graph with $$$n-1$$$ edges. They take turns alternately moving a token on the tree. Eikooc makes the first move, placing the token on any node of her choice. Sushi makes the next move, followed by Eikooc, followed by Sushi, and so on. In each turn after the first, a player must move the token to a node $$$u$$$ such that $$$u$$$ is adjacent to the node $$$v$$$ the token is currently on $$$u$$$ has not been visited before $$$u oplus v leq min(u, v)$$$ Here $$$x oplus y$$$ denotes the bitwise XOR operation on integers $$$x$$$ and $$$y$$$. Both the players play optimally. The player who is unable to make a move loses. The following are examples which demonstrate the rules of the game. Suppose Eikooc starts the game by placing the token at node $$$4$$$. Sushi then moves the token to node $$$6$$$, which is unvisited and adjacent to $$$4$$$. It also holds that $$$6 oplus 4 = 2 leq min(6, 4)$$$. In the next turn, Eikooc moves the token to node $$$5$$$, which is unvisited and adjacent to $$$6$$$. It holds that $$$5 oplus 6 = 3 leq min(5, 6)$$$. Sushi has no more moves to play, so she loses. Suppose Eikooc starts the game by placing the token at node $$$3$$$. Sushi moves the token to node $$$2$$$, which is unvisited and adjacent to $$$3$$$. It also holds that $$$3 oplus 2 = 1 leq min(3, 2)$$$. Eikooc cannot move the token to node $$$6$$$ since $$$6 oplus 2 = 4 leq min(6, 2)$$$. Since Eikooc has no moves to play, she loses. Before the game begins, Eikooc decides to sneakily relabel the nodes of the tree in her favour. Formally, a relabeling is a permutation $$$p$$$ of length $$$n$$$ (sequence of $$$n$$$ integers wherein each integer from $$$1$$$ to $$$n$$$ occurs exactly once) where $$$p_i$$$ denotes the new numbering of node $$$i$$$. She wants to maximize the number of nodes she can choose in the first turn which will guarantee her a win. Help Eikooc find any relabeling which will help her do so. Input The first line contains a single integer $$$t~(1 le t le 10^5)$$$ xa0— the number of test cases. The description of each test case is as follows. The first line of each test case contains an integer $$$n~(1 le n le 2 cdot 10^5)$$$ xa0— the number of nodes in the tree. The next $$$n-1$$$ lines contain two integers $$$u$$$ and $$$v$$$ $$$(1 le u, v le n; u eq v)$$$ xa0— denoting an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print any suitable relabeling xa0— a permutation of length $$$n$$$ which maximizes the number of nodes that can be chosen in the first turn that guarantee a win for Eikooc. If there are multiple such relabelings, you may print any of them. Example input 3 1 2 1 2 3 1 2 1 3 output 1 2 1 1 2 3 Note In the first test case, Eikooc has only one choice. Sushi will have no moves to play after Eikooc chooses this node and Eikooc will win. In the second test case, $$$1 oplus 2 = 3 leq min(1, 2)$$$. Hence, after Eikooc picks either of the nodes, Sushi will have no moves to play and Eikooc will win. Both $$${1, 2}$$$ and $$${2, 1}$$$ are optimal relabelings. Codeforces (c) | 2,100 | false | true | true | false | false | true | false | false | false | false | 2,632 |
1994B | Vova really loves the . Recently, when he was going to sleep, he came up with a fun game. At the beginning of the game, Vova chooses two binary sequences $$$s$$$ and $$$t$$$ of length $$$n$$$ and gives them to Vanya. A binary sequence is a sequence consisting only of the numbers $$$0$$$ and $$$1$$$. Vanya can choose integers $$$l, r$$$ such that $$$1 leq l leq r leq n$$$, and for all $$$l leq i leq r$$$ simultaneously replace $$$s_i$$$ with $$$s_i oplus s_{i - l + 1}$$$, where $$$s_i$$$ is the $$$i$$$-th element of the sequence $$$s$$$. In order for the game to be interesting, there must be a possibility to win. Vanya wins if, with an unlimited number of actions, he can obtain the sequence $$$t$$$ from the sequence $$$s$$$. Determine if the game will be interesting for the sequences $$$s$$$ and $$$t$$$. Input Each test consists of multiple test cases. The first line contains an integer $$$q$$$ ($$$1 le q le 10^{4}$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the length of the sequences $$$s$$$ and $$$t$$$. The second line of each test case contains a binary sequence $$$s$$$ of length $$$n$$$. The third line of each test case contains a binary sequence $$$t$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "Yes" if the game will be interesting, otherwise output "No". You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 6 1 0 1 7 0110100 0110100 9 100101010 101111110 4 0011 1011 4 0100 0001 8 10110111 01100000 Output NO YES YES NO YES YES Note In the first test case, Vanya will not be able to change the sequence $$$s$$$ with the only possible action of choosing $$$l = r = 1$$$. In the second test case, the sequences $$$s$$$ and $$$t$$$ are already equal. In the third test case, Vanya can act as follows: 1. Choose $$$l = 3$$$ and $$$r = 5$$$, then $$$s$$$ will become $$$mathtt{101101010}$$$. 2. Choose $$$l = 5$$$ and $$$r = 6$$$, then $$$s$$$ will become $$$mathtt{101111010}$$$. 3. Choose $$$l = 7$$$ and $$$r = 7$$$, then $$$s$$$ will become $$$mathtt{101111110}$$$. | 1,100 | true | true | false | false | false | true | false | false | false | false | 304 |
49D | Problem - 49D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force dp implementation *1800 No tag edit access → Contest materials ") which represents the stripe’s length. The second line contains exactly _n_ symbols — the line’s initial coloring. 0 corresponds to a white square, 1 corresponds to a black one. Output If Petya cannot win with such an initial coloring, print -1. Otherwise print the minimum number of moves Petya needs to win. Examples Input 6 111010 Output 1 Input 5 10001 Output 1 Input 7 1100010 Output 2 Input 5 00100 Output 2 Note In the first sample Petya can take squares 1 and 2. He repaints square 1 to black and square 2 to white. In the second sample Petya can take squares 2 and 3. He repaints square 2 to white and square 3 to black. | 1,800 | false | false | true | true | false | false | true | false | false | false | 9,726 |
1479E | In Homer's school, there are $$$n$$$ students who love clubs. Initially, there are $$$m$$$ clubs, and each of the $$$n$$$ students is in exactly one club. In other words, there are $$$a_i$$$ students in the $$$i$$$-th club for $$$1 leq i leq m$$$ and $$$a_1+a_2+dots+a_m = n$$$. The $$$n$$$ students are so unfriendly that every day one of them (chosen uniformly at random from all of the $$$n$$$ students) gets angry. The student who gets angry will do one of the following things. With probability $$$frac 1 2$$$, he leaves his current club, then creates a new club himself and joins it. There is only one student (himself) in the new club he creates. With probability $$$frac 1 2$$$, he does not create new clubs. In this case, he changes his club to a new one (possibly the same club he is in currently) with probability proportional to the number of students in it. Formally, suppose there are $$$k$$$ clubs and there are $$$b_i$$$ students in the $$$i$$$-th club for $$$1 leq i leq k$$$ (before the student gets angry). He leaves his current club, and then joins the $$$i$$$-th club with probability $$$frac {b_i} {n}$$$. We note that when a club becomes empty, students will never join it because any student who gets angry will join an empty club with probability $$$0$$$ according to the above statement. Homer wonders the expected number of days until every student is in the same club for the first time. We can prove that the answer can be represented as a rational number $$$frac p q$$$ with $$$gcd(p, q) = 1$$$. Therefore, you are asked to find the value of $$$pq^{-1} bmod 998,244,353$$$. It can be shown that $$$q bmod 998,244,353 eq 0$$$ under the given constraints of the problem. Input The first line contains an integer $$$m$$$ ($$$1 leq m leq 1000$$$) — the number of clubs initially. The second line contains $$$m$$$ integers $$$a_1, a_2, dots, a_m$$$ ($$$1 leq a_i leq 4 cdot 10^8$$$) with $$$1 leq a_1+a_2+dots+a_m leq 4 cdot 10^8$$$, where $$$a_i$$$ denotes the number of students in the $$$i$$$-th club initially. Output Print one integer — the expected number of days until every student is in the same club for the first time, modulo $$$998,244,353$$$. Note In the first example, no matter which student gets angry, the two students will become in the same club with probability $$$frac 1 4$$$. So the expected number of days until every student is in the same club should be $$$4$$$. In the second example, we note that in the first day: The only student in the first club will get angry with probability $$$frac 1 3$$$. If he gets angry, then he will create a new club and join it with probability $$$frac 1 2$$$ (In this case, there will be three clubs which have $$$0, 1, 2$$$ students in it, respectively), leave his current club and join the second club with probability $$$frac 1 2 cdot frac 2 3 = frac 1 3$$$, or stay still with probability $$$frac 1 2 cdot frac 1 3 = frac 1 6$$$; Each of the two students in the second club will get angry with probability $$$frac 1 3$$$. If one of them gets angry, then he will create a new club and join it with probability $$$frac 1 2$$$, leave his current club and join the second club with probability $$$frac 1 2 cdot frac 1 3 = frac 1 6$$$, or stay still with probability $$$frac 1 2 cdot frac 2 3 = frac 1 3$$$. In the fourth example, there is only one club initially. That is, every student has already been in the same club. So the answer is $$$0$$$. | 3,500 | true | false | false | true | false | false | false | false | false | false | 3,276 |
263E | Problem - 263E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force data structures dp *2500 No tag edit access → Contest materials ") and the _j_-th column (1u2009≤u2009_j_u2009≤u2009_m_) there is a non-negative integer _a__i_,u2009_j_. Besides, you've got a non-negative integer _k_. Your task is to find such pair of integers (_a_,u2009_b_) that meets these conditions: _k_u2009≤u2009_a_u2009≤u2009_n_u2009-u2009_k_u2009+u20091; _k_u2009≤u2009_b_u2009≤u2009_m_u2009-u2009_k_u2009+u20091; let's denote the maximum of the function among all integers _x_ and _y_, that satisfy the inequalities _k_u2009≤u2009_x_u2009≤u2009_n_u2009-u2009_k_u2009+u20091 and _k_u2009≤u2009_y_u2009≤u2009_m_u2009-u2009_k_u2009+u20091, as _mval_; for the required pair of numbers the following equation must hold _f_(_a_,u2009_b_)u2009=u2009_mval_. Input The first line contains three space-separated integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000, ). Next _n_ lines each contains _m_ integers: the _j_-th number on the _i_-th line equals _a__i_,u2009_j_ (0u2009≤u2009_a__i_,u2009_j_u2009≤u2009106). The numbers in the lines are separated by spaces. Output Print the required pair of integers _a_ and _b_. Separate the numbers by a space. If there are multiple correct answers, you are allowed to print any of them. Examples Input 4 4 2 1 2 3 4 1 1 1 1 2 2 2 2 4 3 2 1 Output 3 2 Input 5 7 3 8 2 3 4 2 3 3 3 4 6 2 3 4 6 8 7 6 8 4 5 7 1 2 3 2 1 3 2 4 5 3 2 1 2 1 Output 3 3 | 2,500 | false | false | false | true | true | false | true | false | false | false | 8,779 |
762E | In the lattice points of the coordinate line there are _n_ radio stations, the _i_-th of which is described by three integers: _x__i_ — the coordinate of the _i_-th station on the line, _r__i_ — the broadcasting range of the _i_-th station, _f__i_ — the broadcasting frequency of the _i_-th station. We will say that two radio stations with numbers _i_ and _j_ reach each other, if the broadcasting range of each of them is more or equal to the distance between them. In other words _min_(_r__i_,u2009_r__j_)u2009≥u2009_x__i_u2009-u2009_x__j_. Let's call a pair of radio stations (_i_,u2009_j_) bad if _i_u2009<u2009_j_, stations _i_ and _j_ reach each other and they are close in frequency, that is, _f__i_u2009-u2009_f__j_u2009≤u2009_k_. Find the number of bad pairs of radio stations. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_k_u2009≤u200910) — the number of radio stations and the maximum difference in the frequencies for the pair of stations that reach each other to be considered bad. In the next _n_ lines follow the descriptions of radio stations. Each line contains three integers _x__i_, _r__i_ and _f__i_ (1u2009≤u2009_x__i_,u2009_r__i_u2009≤u2009109, 1u2009≤u2009_f__i_u2009≤u2009104) — the coordinate of the _i_-th radio station, it's broadcasting range and it's broadcasting frequency. No two radio stations will share a coordinate. Output Output the number of bad pairs of radio stations. Examples Input 3 2 1 3 10 3 2 5 4 10 8 Input 3 3 1 3 10 3 2 5 4 10 8 Input 5 1 1 3 2 2 2 4 3 2 1 4 2 1 5 3 3 Input 5 1 1 5 2 2 5 4 3 5 1 4 5 1 5 5 3 | 2,200 | false | false | false | false | true | false | false | true | false | false | 6,739 |
400C | Inna and Dima decided to surprise Sereja. They brought a really huge candy matrix, it's big even for Sereja! Let's number the rows of the giant matrix from 1 to _n_ from top to bottom and the columns — from 1 to _m_, from left to right. We'll represent the cell on the intersection of the _i_-th row and _j_-th column as (_i_,u2009_j_). Just as is expected, some cells of the giant candy matrix contain candies. Overall the matrix has _p_ candies: the _k_-th candy is at cell (_x__k_,u2009_y__k_). The time moved closer to dinner and Inna was already going to eat _p_ of her favourite sweets from the matrix, when suddenly Sereja (for the reason he didn't share with anyone) rotated the matrix _x_ times clockwise by 90 degrees. Then he performed the horizontal rotate of the matrix _y_ times. And then he rotated the matrix _z_ times counterclockwise by 90 degrees. The figure below shows how the rotates of the matrix looks like. Inna got really upset, but Duma suddenly understood two things: the candies didn't get damaged and he remembered which cells contained Inna's favourite sweets before Sereja's strange actions. Help guys to find the new coordinates in the candy matrix after the transformation Sereja made! Input The first line of the input contains fix integers _n_, _m_, _x_, _y_, _z_, _p_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009109;xa00u2009≤u2009_x_,u2009_y_,u2009_z_u2009≤u2009109;xa01u2009≤u2009_p_u2009≤u2009105). Each of the following _p_ lines contains two integers _x__k_, _y__k_ (1u2009≤u2009_x__k_u2009≤u2009_n_;xa01u2009≤u2009_y__k_u2009≤u2009_m_) — the initial coordinates of the _k_-th candy. Two candies can lie on the same cell. Output For each of the _p_ candies, print on a single line its space-separated new coordinates. Examples Input 3 3 3 1 1 9 1 1 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Output 1 3 1 2 1 1 2 3 2 2 2 1 3 3 3 2 3 1 Note Just for clarity. Horizontal rotating is like a mirroring of the matrix. For matrix: QWER REWQ ASDF -> FDSA ZXCV VCXZ | 1,500 | true | false | true | false | false | false | false | false | false | false | 8,225 |
822A | Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vičkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vičkopolis. He almost even fell into a depression from boredom! Leha came up with a task for himself to relax a little. He chooses two integers _A_ and _B_ and then calculates the greatest common divisor of integers "_A_ factorial" and "_B_ factorial". Formally the hacker wants to find out GCD(_A_!,u2009_B_!). It's well known that the factorial of an integer _x_ is a product of all positive integers less than or equal to _x_. Thus _x_!u2009=u20091·2·3·...·(_x_u2009-u20091)·_x_. For example 4!u2009=u20091·2·3·4u2009=u200924. Recall that GCD(_x_,u2009_y_) is the largest positive integer _q_ that divides (without a remainder) both _x_ and _y_. Leha has learned how to solve this task very effective. You are able to cope with it not worse, aren't you? Input The first and single line contains two integers _A_ and _B_ (1u2009≤u2009_A_,u2009_B_u2009≤u2009109,u2009_min_(_A_,u2009_B_)u2009≤u200912). Output Print a single integer denoting the greatest common divisor of integers _A_! and _B_!. Note Consider the sample. 4!u2009=u20091·2·3·4u2009=u200924. 3!u2009=u20091·2·3u2009=u20096. The greatest common divisor of integers 24 and 6 is exactly 6. | 800 | true | false | true | false | false | false | false | false | false | false | 6,468 |
55D | Problem - 55D - Codeforces =============== xa0 . Each of the next _t_ lines contains two natural numbers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u20099u2009·1018). Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). Output Output should contain _t_ numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from _l__i_ to _r__i_, inclusively). Examples Input 1 1 9 Output 9 Input 1 12 15 Output 2 | 2,500 | false | false | false | true | false | false | false | false | false | false | 9,697 |
154B | By 2312 there were _n_ Large Hadron Colliders in the inhabited part of the universe. Each of them corresponded to a single natural number from 1 to _n_. However, scientists did not know what activating several colliders simultaneously could cause, so the colliders were deactivated. In 2312 there was a startling discovery: a collider's activity is safe if and only if all numbers of activated colliders are pairwise relatively prime to each other (two numbers are relatively prime if their greatest common divisor equals 1)! If two colliders with relatively nonprime numbers are activated, it will cause a global collapse. Upon learning this, physicists rushed to turn the colliders on and off and carry out all sorts of experiments. To make sure than the scientists' quickness doesn't end with big trouble, the Large Hadron Colliders' Large Remote Control was created. You are commissioned to write the software for the remote (well, you do not expect anybody to operate it manually, do you?). Initially, all colliders are deactivated. Your program receives multiple requests of the form "activate/deactivate the _i_-th collider". The program should handle requests in the order of receiving them. The program should print the processed results in the format described below. To the request of "+ i" (that is, to activate the _i_-th collider), the program should print exactly one of the following responses: "Success" if the activation was successful. "Already on", if the _i_-th collider was already activated before the request. "Conflict with j", if there is a conflict with the _j_-th collider (that is, the _j_-th collider is on, and numbers _i_ and _j_ are not relatively prime). In this case, the _i_-th collider shouldn't be activated. If a conflict occurs with several colliders simultaneously, you should print the number of any of them. The request of "- i" (that is, to deactivate the _i_-th collider), should receive one of the following responses from the program: "Success", if the deactivation was successful. "Already off", if the _i_-th collider was already deactivated before the request. You don't need to print quotes in the output of the responses to the requests. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of colliders and the number of requests, correspondingly. Next _m_ lines contain numbers of requests, one per line, in the form of either "+ i" (without the quotes) — activate the _i_-th collider, or "-xa0i" (without the quotes) — deactivate the _i_-th collider (1u2009≤u2009_i_u2009≤u2009_n_). Output Print _m_ lines — the results of executing requests in the above given format. The requests should be processed in the order, in which they are given in the input. Don't forget that the responses to the requests should be printed without quotes. | 1,600 | true | false | false | false | false | false | false | false | false | false | 9,258 |
940A | We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round. The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1,u20093,u20092,u20091} is 2. Diameter of multiset consisting of one point is 0. You are given _n_ points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed _d_? Input The first line contains two integers _n_ and _d_ (1u2009≤u2009_n_u2009≤u2009100,u20090u2009≤u2009_d_u2009≤u2009100)xa0— the amount of points and the maximum allowed diameter respectively. The second line contains _n_ space separated integers (1u2009≤u2009_x__i_u2009≤u2009100)xa0— the coordinates of the points. Output Output a single integerxa0— the minimum number of points you have to remove. Note In the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2u2009-u20091u2009=u20091. In the second test case the diameter is equal to 0, so its is unnecessary to remove any points. In the third test case the optimal strategy is to remove points with coordinates 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6u2009-u20093u2009=u20093. | 1,200 | false | true | false | false | false | false | true | false | true | false | 5,938 |
340B | Problem - 340B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force geometry *2100 No tag edit access → Contest materials and four vertices (also called corners). Please note that a quadrilateral doesn't have to be convex. A special quadrilateral is one which has all four vertices in the set of special points. Given the set of special points, please calculate the maximal area of a special quadrilateral. Input The first line contains integer _n_ (4u2009≤u2009_n_u2009≤u2009300). Each of the next _n_ lines contains two integers: _x__i_, _y__i_ (u2009-u20091000u2009≤u2009_x__i_,u2009_y__i_u2009≤u20091000) — the cartesian coordinates of _i_th special point. It is guaranteed that no three points are on the same line. It is guaranteed that no two points coincide. Output Output a single real number — the maximal area of a special quadrilateral. The answer will be considered correct if its absolute or relative error does't exceed 10u2009-u20099. Examples Input 5 0 0 0 4 4 0 4 4 2 3 Output 16.000000 Note In the test example we can choose first 4 points to be the vertices of the quadrilateral. They form a square by side 4, so the area is 4·4u2009=u200916. | 2,100 | false | false | false | false | false | false | true | false | false | false | 8,474 |
1996F | Sparkle gives you two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$. Initially, your score is $$$0$$$. In one operation, you can choose an integer $$$i$$$ and add $$$a_i$$$ to your score. Then, you must set $$$a_i$$$ = $$$max(0, a_i - b_i)$$$. You only have time to perform $$$k$$$ operations before Sparkle sets off a nuclear bomb! What is the maximum score you can acquire after $$$k$$$ operations? Input The first line contains $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of test cases. The first line of each test case contains $$$n$$$ and $$$k$$$ ($$$1 leq n leq 2 cdot 10^5, 1 leq k leq 10^9$$$) — the length of the arrays and the number of operations you can perform. The following line contains $$$n$$$ integers $$$a_1, a_2, ... a_n$$$ ($$$1 leq a_i leq 10^9$$$). The following line contains $$$n$$$ integers $$$b_1, b_2, ... b_n$$$ ($$$1 leq b_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output an integer, the maximum score you can acquire after $$$k$$$ operations. Example Input 5 3 4 5 6 7 2 3 4 5 9 32 52 68 64 14 18 14 53 24 8 5 1000 1 2 3 4 5 5 4 3 2 1 1 1000000 1000000 1 10 6 3 3 5 10 6 8 6 8 7 7 6 1 7 4 1 1 8 9 3 1 Output 21 349 27 500000500000 47 | 1,900 | true | true | false | false | false | false | false | true | false | false | 285 |
1156F | You have a bag which contains $$$n$$$ cards. There is a number written on each card; the number on $$$i$$$-th card is $$$a_i$$$. You are playing the following game. During each turn, you choose and remove a random card from the bag (all cards that are still left inside the bag are chosen equiprobably). Nothing else happens during the first turn — but during the next turns, after removing a card (let the number on it be $$$x$$$), you compare it with the card that was removed during the previous turn (let the number on it be $$$y$$$). Possible outcomes are: if $$$x < y$$$, the game ends and you lose; if $$$x = y$$$, the game ends and you win; if $$$x > y$$$, the game continues. If there are no cards left in the bag, you lose. Cards are not returned into the bag after you remove them. You have to calculate the probability of winning in this game. It can be shown that it is in the form of $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q eq 0$$$, $$$P le Q$$$. Output the value of $$$P cdot Q^{−1} ~(mod ~~ 998244353)$$$. Input The first line contains one integer $$$n$$$ ($$$2 le n le 5000$$$) — the number of cards in the bag. The second live contains $$$n$$$ integers $$$a_1, a_2, dots a_n$$$ ($$$1 le a_i le n$$$) — the $$$i$$$-th integer is the number written on the $$$i$$$-th card. Output Print one integer — the probability of winning in this game modulo $$$998244353$$$. Note In the first test case the probability of winning is $$$frac{1}{10}$$$. In the second test case the probability of winning is $$$1$$$. In the third test case the probability of winning is $$$0$$$. In the fourth test case the probability of winning is $$$frac{1}{4}$$$. | 2,300 | true | false | false | true | false | false | false | false | false | false | 4,925 |
712C | Memory is now interested in the de-evolution of objects, specifically triangles. He starts with an equilateral triangle of side length _x_, and he wishes to perform operations to obtain an equilateral triangle of side length _y_. In a single second, he can modify the length of a single side of the current triangle such that it remains a non-degenerate triangle (triangle of positive area). At any moment of time, the length of each side should be integer. What is the minimum number of seconds required for Memory to obtain the equilateral triangle of side length _y_? Input The first and only line contains two integers _x_ and _y_ (3u2009≤u2009_y_u2009<u2009_x_u2009≤u2009100u2009000)xa0— the starting and ending equilateral triangle side lengths respectively. Output Print a single integerxa0— the minimum number of seconds required for Memory to obtain the equilateral triangle of side length _y_ if he starts with the equilateral triangle of side length _x_. Note In the first sample test, Memory starts with an equilateral triangle of side length 6 and wants one of side length 3. Denote a triangle with sides _a_, _b_, and _c_ as (_a_,u2009_b_,u2009_c_). Then, Memory can do . In the second sample test, Memory can do . In the third sample test, Memory can do: . | 1,600 | true | true | false | false | false | false | false | false | false | false | 6,969 |
680A | A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer. Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards. He is allowed to at most once discard two or three cards with the same number. Of course, he won't discard cards if it's impossible to choose two or three cards with the same number. Given five numbers written on cards, cay you find the minimum sum of numbers on remaining cards? Input The only line of the input contains five integers _t_1, _t_2, _t_3, _t_4 and _t_5 (1u2009≤u2009_t__i_u2009≤u2009100)xa0— numbers written on cards. Output Print the minimum possible sum of numbers written on remaining cards. Note In the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following. Do nothing and the sum would be 7u2009+u20093u2009+u20097u2009+u20093u2009+u200920u2009=u200940. Remove two cards with a number 7. The remaining sum would be 3u2009+u20093u2009+u200920u2009=u200926. Remove two cards with a number 3. The remaining sum would be 7u2009+u20097u2009+u200920u2009=u200934. You are asked to minimize the sum so the answer is 26. In the second sample, it's impossible to find two or three cards with the same number. Hence, Limak does nothing and the sum is 7u2009+u20099u2009+u20091u2009+u20093u2009+u20098u2009=u200928. In the third sample, all cards have the same number. It's optimal to discard any three cards. The sum of two remaining numbers is 10u2009+u200910u2009=u200920. | 800 | false | false | true | false | false | true | false | false | false | false | 7,105 |
504E | Misha has a tree with characters written on the vertices. He can choose two vertices _s_ and _t_ of this tree and write down characters of vertices lying on a path from _s_ to _t_. We'll say that such string corresponds to pair (_s_,u2009_t_). Misha has _m_ queries of type: you are given 4 vertices _a_, _b_, _c_, _d_; you need to find the largest common prefix of the strings that correspond to pairs (_a_,u2009_b_) and (_c_,u2009_d_). Your task is to help him. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009300u2009000) — the number of vertices in the tree. Next follows a line consisting of _n_ small English letters. The _i_-th character of the string corresponds to the character written on the _i_-th vertex. Next _n_u2009-u20091 lines contain information about edges. An edge is defined by a pair of integers _u_, _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_, _u_u2009≠u2009_v_), separated by spaces. The next line contains integer _m_ (1u2009≤u2009_m_u2009≤u20091u2009000u2009000) — the number of queries. Next _m_ lines contain information about queries. A query is defined by four integers _a_, _b_, _c_, _d_ (1u2009≤u2009_a_,u2009_b_,u2009_c_,u2009_d_u2009≤u2009_n_), separated by spaces. Output For each query print the length of the largest common prefix on a separate line. Examples Input 6 bbbabb 2 1 3 2 4 3 5 2 6 5 6 2 5 3 1 1 5 2 3 5 6 5 6 6 3 4 1 6 2 3 4 2 2 4 5 | 3,000 | false | false | false | false | false | false | false | true | false | false | 7,824 |
338E | Problem - 338E - 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 *2600 No tag edit access → Contest materials answer = 0 for i = 1 to n-len+1 answer = answer + f(a | 2,600 | false | false | false | false | true | false | false | false | false | false | 8,481 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.