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
1954E
There are $$$n$$$ monsters standing in a row. The $$$i$$$-th monster has $$$a_i$$$ health points. Every second, you can choose one alive monster and launch a chain lightning at it. The lightning deals $$$k$$$ damage to it, and also spreads to the left (towards decreasing $$$i$$$) and to the right (towards increasing $$$i$$$) to alive monsters, dealing $$$k$$$ damage to each. When the lightning reaches a dead monster or the beginning/end of the row, it stops. A monster is considered alive if its health points are strictly greater than $$$0$$$. For example, consider the following scenario: there are three monsters with health equal to $$$[5, 2, 7]$$$, and $$$k = 3$$$. You can kill them all in $$$4$$$ seconds: launch a chain lightning at the $$$3$$$-rd monster, then their health values are $$$[2, -1, 4]$$$; launch a chain lightning at the $$$1$$$-st monster, then their health values are $$$[-1, -1, 4]$$$; launch a chain lightning at the $$$3$$$-rd monster, then their health values are $$$[-1, -1, 1]$$$; launch a chain lightning at the $$$3$$$-th monster, then their health values are $$$[-1, -1, -2]$$$. For each $$$k$$$ from $$$1$$$ to $$$max(a_1, a_2, dots, a_n)$$$, calculate the minimum number of seconds it takes to kill all the monsters. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β€” the number of monsters. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^5$$$)xa0β€” the health points of the $$$i$$$-th monster. Output For each $$$k$$$ from $$$1$$$ to $$$max(a_1, a_2, dots, a_n)$$$, output the minimum number of seconds it takes to kill all the monsters. Examples Input 10 1 9 7 6 2 4 7 8 1 3
2,200
true
true
true
false
true
false
false
true
false
false
550
1878F
Vasilije is a smart student and his discrete mathematics teacher Sonja taught him number theory very well. He gave Ognjen a positive integer $$$n$$$. Denote $$$d(n)$$$ as the number of positive integer divisors of $$$n$$$, and denote $$$gcd(a, b)$$$ as the largest integer $$$g$$$ such that $$$a$$$ is divisible by $$$g$$$ and $$$b$$$ is divisible by $$$g$$$. After that, he gave Ognjen $$$q$$$ queries, and there are $$$2$$$ types of queries. $$$1$$$, $$$x$$$xa0β€” set $$$n$$$ to $$$n cdot x$$$, and then answer the following question: does there exist a positive integer $$$a$$$ such that $$$gcd(a, n) = 1$$$, and $$$d(n cdot a) = n$$$? $$$2$$$xa0β€” reset $$$n$$$ to its initial value (before any queries). Note that $$$n$$$ does not get back to its initial value after the type 1 query. Since Ognjen is afraid of number theory, Vasilije promised him that after each query, $$$d(n) le 10^9$$$, however, even with that constraint, he still needs your help with this problem. Input The first line contains a positive integer $$$t$$$, ($$$1 le t le 100$$$)xa0β€” the number of test cases. The first line of each test case contains $$$2$$$ integers, $$$n$$$ and $$$q$$$ ($$$1 le n le 10^{6}$$$, $$$1le q le 1000$$$)xa0β€” the number $$$n$$$ and the number of queries. The following $$$q$$$ lines contain an integer $$$k$$$ ($$$1 le k le 2$$$), if $$$k=1$$$ then there is another integer in this line $$$x$$$ ($$$1 le x le 10^6$$$)xa0β€” the description of the queries. It is guaranteed that, for the given input, $$$d(n)$$$ does not exceed $$$10^9$$$ at any point. It is guaranteed that the sum of $$$q$$$ over all test cases doesn't exceed $$$10^3$$$. Output For each type 1 query, you should output "YES" if there exist such positive integer $$$a$$$ that $$$gcd(a, n) = 1$$$ and $$$d(n cdot a)=n$$$, and "NO" if he can't. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 7 1 5 1 1 1 2 2 1 8 1 9 20 4 1 3 2 1 7 1 12 16 10 1 6 1 6 1 10 1 9 1 1 1 9 1 7 1 3 1 2 1 10 9 1 1 3 8 1 1 2 8 3 1 5 1 8 1 10 11 5 1 8 1 2 1 1 1 3 1 1 Output YES YES YES YES YES NO YES YES NO YES YES YES NO YES NO YES YES NO NO YES NO NO YES NO NO NO NO Note In the first test case, we initially have $$$n=1$$$. After the first query: $$$n=1$$$, $$$d(n)=1$$$, so by taking $$$a = 1$$$, $$$d(n cdot a) = n$$$, and the answer to this query is "YES". After the second query: $$$n=2$$$, $$$d(n)=2$$$, we can, again, take $$$a = 1$$$, $$$d(n cdot a) = n$$$, and the answer to this query is "YES". After the third query $$$n=1$$$, and this is a type $$$2$$$ query so we don't answer it. After the fourth query: $$$n=8$$$, and by taking $$$a=3$$$, $$$d(n cdot a) = d(24) = 8 = n$$$, so the answer is "YES". After the fifth query: $$$n=72$$$, now we can take $$$a=637$$$ to get $$$n cdot a = 45864$$$, and $$$d(n cdot a) = 72 = n$$$, so the answer is "YES". In the second test case, we initially have $$$n=20$$$. After the first query: $$$n=60$$$, and the answer is "YES". After the second query: $$$n=20$$$, this is a type $$$2$$$ query, so we don't answer it. After the third query: $$$n=140$$$, and it can be proven that no matter which positive integer $$$a$$$ we take, $$$d(n cdot a)$$$ will never be equal to $$$n$$$, so the answer to this query is "NO". After the fourth query: $$$n=1680$$$. It can be proven that there exists a positive integer $$$a$$$, such that $$$d(n cdot a) = n$$$, so the answer is "YES".
1,900
true
false
false
false
false
false
true
false
false
false
1,012
1419A
Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play $$$t$$$ matches of a digit game... In each of $$$t$$$ matches of the digit game, a positive integer is generated. It consists of $$$n$$$ digits. The digits of this integer are numerated from $$$1$$$ to $$$n$$$ from the highest-order digit to the lowest-order digit. After this integer is announced, the match starts. Agents play in turns. Raze starts. In one turn an agent can choose any unmarked digit and mark it. Raze can choose digits on odd positions, but can not choose digits on even positions. Breach can choose digits on even positions, but can not choose digits on odd positions. The match ends, when there is only one unmarked digit left. If the single last digit is odd, then Raze wins, else Breach wins. It can be proved, that before the end of the match (for every initial integer with $$$n$$$ digits) each agent has an ability to make a turn, i.e. there is at least one unmarked digit, that stands on a position of required parity. For each of $$$t$$$ matches find out, which agent wins, if both of them want to win and play optimally. Input First line of input contains an integer $$$t$$$ $$$(1 le t le 100)$$$ xa0β€” the number of matches. The first line of each match description contains an integer $$$n$$$ $$$(1 le n le 10^3)$$$ xa0β€” the number of digits of the generated number. The second line of each match description contains an $$$n$$$-digit positive integer without leading zeros. Output For each match print $$$1$$$, if Raze wins, and $$$2$$$, if Breach wins. Example Input 4 1 2 1 3 3 102 4 2069 Note In the first match no one can make a turn, the only digit left is $$$2$$$, it's even, so Breach wins. In the second match the only digit left is $$$3$$$, it's odd, so Raze wins. In the third match Raze can mark the last digit, after that Breach can only mark $$$0$$$. $$$1$$$ will be the last digit left, it's odd, so Raze wins. In the fourth match no matter how Raze plays, Breach can mark $$$9$$$, and in the end there will be digit $$$0$$$. It's even, so Breach wins.
900
false
true
true
false
false
false
false
false
false
false
3,587
879B
Problem - 879B - 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 implementation *1200 No tag edit access β†’ Contest materials xa0β€” the number of people and the number of wins. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009_n_) β€” powers of the player. It's guaranteed that this line contains a valid permutation, i.e. all _a__i_ are distinct. Output Output a single integer β€” power of the winner. Examples Input 2 2 1 2 Output 2 Input 4 2 3 1 2 4 Output 3 Input 6 2 6 5 3 1 2 4 Output 6 Input 2 10000000000 2 1 Output 2 Note Games in the second sample: 3 plays with 1. 3 wins. 1 goes to the end of the line. 3 plays with 2. 3 wins. He wins twice in a row. He becomes the winner.
1,200
false
false
true
false
true
false
false
false
false
false
6,228
1156C
Problem - 1156C - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search greedy sortings ternary search two pointers *2000 No tag edit access β†’ Contest materials β€” the number of points and the constraint on the distance between matched points, respectively. The second line contains $$$n$$$ integers $$$x_1$$$, $$$x_2$$$, ..., $$$x_n$$$ ($$$1 le x_i le 10^9$$$). Output Print one integer β€” the maximum number of pairs of points you can match with each other. Examples Input 4 2 1 3 3 7 Output 2 Input 5 5 10 9 5 8 7 Output 1 Note In the first example, you may match point $$$1$$$ with point $$$2$$$ ($$$3 - 1 ge 2$$$), and point $$$3$$$ with point $$$4$$$ ($$$7 - 3 ge 2$$$). In the second example, you may match point $$$1$$$ with point $$$3$$$ ($$$5 - 10 ge 5$$$).
2,000
false
true
false
false
false
false
false
true
true
false
4,928
584A
Problem - 584A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math *1000 No tag edit access β†’ Contest materials β€” the length of the number and the number it should be divisible by. Output Print one such positive number without leading zeroes, β€” the answer to the problem, or u2009-u20091, if such number doesn't exist. If there are multiple possible answers, you are allowed to print any of them. Examples Input 3 2 Output 712
1,000
true
false
false
false
false
false
false
false
false
false
7,510
1670D
Sehr Sus is an infinite hexagonal grid as pictured below, controlled by MennaFadali, ZerooCool and Hosssam. They love equilateral triangles and want to create $$$n$$$ equilateral triangles on the grid by adding some straight lines. The triangles must all be empty from the inside (in other words, no straight line or hexagon edge should pass through any of the triangles). You are allowed to add straight lines parallel to the edges of the hexagons. Given $$$n$$$, what is the minimum number of lines you need to add to create at least $$$n$$$ equilateral triangles as described? Adding two red lines results in two new yellow equilateral triangles. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) β€” the number of test cases. Then $$$t$$$ test cases follow. Each test case contains a single integer $$$n$$$ ($$$1 le n le 10^{9}$$$) β€” the required number of equilateral triangles. Output For each test case, print the minimum number of lines needed to have $$$n$$$ or more equilateral triangles. Note In the first and second test cases only 2 lines are needed. After adding the first line, no equilateral triangles will be created no matter where it is added. But after adding the second line, two more triangles will be created at once. In the third test case, the minimum needed is 3 lines as shown below.
1,700
true
true
true
false
false
false
true
true
false
false
2,254
1371B
# Magical Calendar Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes A competitive eater, Alice is scheduling some practices for an eating contest on a magical calendar. The calendar is unusual because a week contains not necessarily 7 days! In detail, she can choose any integer k which satisfies 1 ≀ k ≀ r, and set k days as the number of days in a week. Alice is going to paint some n consecutive days on this calendar. On this calendar, dates are written from the left cell to the right cell in a week. If a date reaches the last day of a week, the next day’s cell is the leftmost cell in the next (under) row. She wants to make all of the painted cells to be connected by side . It means, that for any two painted cells there should exist at least one sequence of painted cells, started in one of these cells, and ended in another, such that any two consecutive cells in this sequence are connected by side. Alice is considering the shape of the painted cells. Two shapes are the same if there exists a way to make them exactly overlapped using only parallel moves, parallel to the calendar’s sides .For example, in the picture, a week has 4 days and Alice paints 5 consecutive days. [1] and [2] are different shapes, but [1] and [3] are equal shapes. Alice wants to know how many possible shapes exists if she set how many days a week has and choose consecutive n days and paints them in calendar started in one of the days of the week . As was said before, she considers only shapes, there all cells are connected by side. # Input The input consists of multiple test cases. The first line contains a single integer t (1 ≀ t ≀ 1000 ) x16 the number of test cases. Next t lines contain descriptions of test cases. For each test case, the only line contains two integers n, r (1 ≀ n ≀ 10 9, 1 ≀ r ≀ 10 9). # Output For each test case, print a single integer x16 the answer to the problem. Please note, that the answer for some test cases won’t fit into 32 -bit integer type, so you should use at least 64 -bit integer type in your programming language. Page 1 of 2 Example standard input standard output 53 4 3 2 3 1 13 7 1010000 9999999 43128 510049495001 # Note In the first test case, Alice can set 1, 2, 3 or 4 days as the number of days in a week. There are 6 possible paintings shown in the picture, but there are only 4 different shapes. So, the answer is 4. Notice that the last example in the picture is an invalid painting because all cells are not connected by sides. In the last test case, be careful with the overflow issue, described in the output format. Page 2 of 2
1,200
true
false
false
false
false
false
false
false
false
false
3,813
303A
Bike is interested in permutations. A permutation of length _n_ is an integer sequence such that each integer from 0 to (_n_u2009-u20091) appears exactly once in it. For example, [0,u20092,u20091] is a permutation of length 3 while both [0,u20092,u20092] and [1,u20092,u20093] is not. A permutation triple of permutations of length _n_ (_a_,u2009_b_,u2009_c_) is called a Lucky Permutation Triple if and only if . The sign _a__i_ denotes the _i_-th element of permutation _a_. The modular equality described above denotes that the remainders after dividing _a__i_u2009+u2009_b__i_ by _n_ and dividing _c__i_ by _n_ are equal. Now, he has an integer _n_ and wants to find a Lucky Permutation Triple. Could you please help him? Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009105). Output If no Lucky Permutation Triple of length _n_ exists print -1. Otherwise, you need to print three lines. Each line contains _n_ space-seperated integers. The first line must contain permutation _a_, the second line β€” permutation _b_, the third β€” permutation _c_. If there are multiple solutions, print any of them. Examples Output 1 4 3 2 0 1 0 2 4 3 2 4 0 1 3 Note In Sample 1, the permutation triple ([1,u20094,u20093,u20092,u20090],u2009[1,u20090,u20092,u20094,u20093],u2009[2,u20094,u20090,u20091,u20093]) is Lucky Permutation Triple, as following holds: ; ; ; ; . In Sample 2, you can easily notice that no lucky permutation triple exists.
1,300
true
false
true
false
false
true
false
false
false
false
8,623
100C
Problem - 100C - Codeforces =============== xa0 . These numbers will not have any leading zeros. Output Write sum of the two integers. Do not put any leading zeros. Examples Input 2 3 Output 5 Input 1390 2011 Output 3401 Input 12345 54321 Output 66666
1,400
false
false
true
false
false
false
false
false
false
false
9,487
1503D
There is a deck of $$$n$$$ cards. The $$$i$$$-th card has a number $$$a_i$$$ on the front and a number $$$b_i$$$ on the back. Every integer between $$$1$$$ and $$$2n$$$ appears exactly once on the cards. A deck is called sorted if the front values are in increasing order and the back values are in decreasing order. That is, if $$$a_i< a_{i+1}$$$ and $$$b_i> b_{i+1}$$$ for all $$$1le i<n$$$. To flip a card $$$i$$$ means swapping the values of $$$a_i$$$ and $$$b_i$$$. You must flip some subset of cards (possibly, none), then put all the cards in any order you like. What is the minimum number of cards you must flip in order to sort the deck? Input The first line contains a single integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$) β€” the number of cards. The next $$$n$$$ lines describe the cards. The $$$i$$$-th of these lines contains two integers $$$a_i, b_i$$$ ($$$1le a_i, b_ile 2n$$$). Every integer between $$$1$$$ and $$$2n$$$ appears exactly once. Output If it is impossible to sort the deck, output "-1". Otherwise, output the minimum number of flips required to sort the deck. Examples Input 5 3 10 6 4 1 9 5 8 2 7 Note In the first test case, we flip the cards $$$(1, 9)$$$ and $$$(2, 7)$$$. The deck is then ordered $$$(3,10), (5,8), (6,4), (7,2), (9,1)$$$. It is sorted because $$$3<5<6<7<9$$$ and $$$10>8>4>2>1$$$. In the second test case, it is impossible to sort the deck.
2,600
false
true
false
false
true
true
false
false
true
false
3,157
770D
A sequence of square brackets is regular if by inserting symbols "+" and "1" into it, you can get a regular mathematical expression from it. For example, sequences "[[]][]", "[]" and "[[][[]]]" β€” are regular, at the same time "][", "[[]" and "[[]]][" β€” are irregular. Draw the given sequence using a minimalistic pseudographics in the strip of the lowest possible height β€” use symbols '+', '-' and ''. For example, the sequence "[[][]][]" should be represented as: +- -++- -+ +- -++- -+ +- -++- -+ +- -++- -+ Each bracket should be represented with the hepl of one or more symbols '' (the vertical part) and symbols '+' and '-' as on the example which is given above. Brackets should be drawn without spaces one by one, only dividing pairs of consecutive pairwise brackets with a single-space bar (so that the two brackets do not visually merge into one symbol). The image should have the minimum possible height. The enclosed bracket is always smaller than the surrounding bracket, but each bracket separately strives to maximize the height of the image. So the pair of final brackets in the example above occupies the entire height of the image. Study carefully the examples below, they adequately explain the condition of the problem. Pay attention that in this problem the answer (the image) is unique. Input The first line contains an even integer _n_ (2u2009≀u2009_n_u2009≀u2009100) β€” the length of the sequence of brackets. The second line contains the sequence of brackets β€” these are _n_ symbols "[" and "]". It is guaranteed that the given sequence of brackets is regular.
1,400
false
false
true
false
false
false
false
false
false
false
6,699
1366E
# Two Arrays Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes You are given two arrays a1, a 2, . . . , a n and b1, b 2, . . . , b m. Array b is sorted in ascending order ( bi < b i+1 for each i from 1 to m βˆ’ 1). You have to divide the array a into m consecutive subarrays so that, for each i from 1 to m, the minimum on the i-th subarray is equal to bi. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of a compose the first subarray, the next several elements of a compose the second subarray, and so on. For example, if a = [12 , 10 , 20 , 20 , 25 , 30] and b = [10 , 20 , 30] then there are two good partitions of array a:1. [12 , 10 , 20] , [20 , 25] , [30] ;2. [12 , 10] , [20 , 20 , 25] , [30] .You have to calculate the number of ways to divide the array a. Since the number can be pretty large print it modulo 998244353 . # Input The first line contains two integers n and m (1 ≀ n, m ≀ 2Β·10 5) x16 the length of arrays a and b respectively. The second line contains n integers a1, a 2, . . . , a n (1 ≀ ai ≀ 10 9) x16 the array a.The third line contains m integers b1, b 2, . . . , b m (1 ≀ bi ≀ 10 9; bi < b i+1 ) x16 the array b. # Output In only line print one integer x16 the number of ways to divide the array a modulo 998244353 . # Examples standard input standard output 6 3 12 10 20 20 25 30 10 20 30 24 2 1 3 3 7 3 7 08 2 1 2 2 2 2 2 2 2 1 2 7 Page 1 of 1
2,100
false
false
false
true
false
true
true
true
false
false
3,846
1954A
Alice and Bob have bought a ribbon consisting of $$$n$$$ parts. Now they want to paint it. First, Alice will paint every part of the ribbon into one of $$$m$$$ colors. For each part, she can choose its color arbitrarily. Then, Bob will choose at most $$$k$$$ parts of the ribbon and repaint them into the same color (he chooses the affected parts and the color arbitrarily). Bob would like all parts to have the same color. However, Alice thinks that this is too dull, so she wants to paint the ribbon in such a way that Bob cannot make all parts have the same color. Is it possible to paint the ribbon in such a way? Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) β€” the number of test cases. Each test case consists of one line containing three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le m, k le n le 50$$$) β€” the number of parts, the number of colors and the number of parts Bob can repaint, respectively. Output For each test case, print YES if Alice can paint the ribbon so that Bob cannot make all parts have the same color. Otherwise, print NO. You can print every letter in any register. For example, Yes, yes, yEs will all be recognized as positive answer. Example Input 5 1 1 1 5 1 1 5 2 1 5 2 2 5 5 3 Note In the first test case, a ribbon consists of $$$1$$$ part. So all its parts will always have the same color. In the second test case, there is only $$$1$$$ color. In the third test case, Alice can paint the ribbon as follows: $$$[1, 2, 1, 2, 1]$$$. It's impossible to change the color of at most $$$1$$$ part so that all parts have the same color. In the fourth test case, no matter how Alice paints the ribbon, Bob will always be able to repaint $$$2$$$ parts so that all parts have the same color. In the fifth test case, Alice can paint the ribbon as follows: $$$[1, 2, 3, 4, 5]$$$. It's impossible to change the color of at most $$$3$$$ parts so that all parts have the same color.
900
true
true
false
false
false
true
false
false
false
false
554
1348E
Phoenix is picking berries in his backyard. There are $$$n$$$ shrubs, and each shrub has $$$a_i$$$ red berries and $$$b_i$$$ blue berries. Each basket can contain $$$k$$$ berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a basket must be from the same shrub or/and have the same color. For example, if there are two shrubs with $$$5$$$ red and $$$2$$$ blue berries in the first shrub and $$$2$$$ red and $$$1$$$ blue berries in the second shrub then Phoenix can fill $$$2$$$ baskets of capacity $$$4$$$ completely: the first basket will contain $$$3$$$ red and $$$1$$$ blue berries from the first shrub; the second basket will contain the $$$2$$$ remaining red berries from the first shrub and $$$2$$$ red berries from the second shrub. Help Phoenix determine the maximum number of baskets he can fill completely! Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$ 1le n, k le 500$$$)xa0β€” the number of shrubs and the basket capacity, respectively. The $$$i$$$-th of the next $$$n$$$ lines contain two integers $$$a_i$$$ and $$$b_i$$$ ($$$0 le a_i, b_i le 10^9$$$)xa0β€” the number of red and blue berries in the $$$i$$$-th shrub, respectively. Output Output one integerxa0β€” the maximum number of baskets that Phoenix can fill completely. Note The first example is described above. In the second example, Phoenix can fill one basket fully using all the berries from the first (and only) shrub. In the third example, Phoenix cannot fill any basket completely because there are less than $$$5$$$ berries in each shrub, less than $$$5$$$ total red berries, and less than $$$5$$$ total blue berries. In the fourth example, Phoenix can put all the red berries into baskets, leaving an extra blue berry behind.
2,400
true
true
false
true
false
false
true
false
false
false
3,966
331B1
The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily! There are _n_ beavers, each of them has a unique id from 1 to _n_. Consider a permutation _a_1,u2009_a_2,u2009...,u2009_a__n_ of _n_ these beavers. Beavershave 5000 needs one session to shave beavers with ids from _x_ to _y_ (inclusive) if and only if there are such indices _i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__k_, that _a__i_1u2009=u2009_x_, _a__i_2u2009=u2009_x_u2009+u20091, ..., _a__i__k_u2009-u20091u2009=u2009_y_u2009-u20091, _a__i__k_u2009=u2009_y_. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1,u20092,u20093,u2009...,u2009_n_. If we can't shave beavers from _x_ to _y_ in one session, then we can split these beavers into groups [_x_,u2009_p_1], [_p_1u2009+u20091,u2009_p_2], ..., [_p__m_u2009+u20091,u2009_y_] (_x_u2009≀u2009_p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__m_u2009<u2009_y_), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs _m_u2009+u20091 working sessions to shave beavers from _x_ to _y_. All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types: what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from _x_ to _y_, inclusive? two beavers on positions _x_ and _y_ (the beavers _a__x_ and _a__y_) swapped. You can assume that any beaver can be shaved any number of times. Input The first line contains integer _n_ β€” the total number of beavers, 2u2009≀u2009_n_. The second line contains _n_ space-separated integers β€” the initial beaver permutation. The third line contains integer _q_ β€” the number of queries, 1u2009≀u2009_q_u2009≀u2009105. The next _q_ lines contain the queries. Each query _i_ looks as _p__i_ _x__i_ _y__i_, where _p__i_ is the query type (1 is to shave beavers from _x__i_ to _y__i_, inclusive, 2 is to swap beavers on positions _x__i_ and _y__i_). All queries meet the condition: 1u2009≀u2009_x__i_u2009<u2009_y__i_u2009≀u2009_n_. to get 30 points, you need to solve the problem with constraints: _n_u2009≀u2009100 (subproblem B1); to get 100 points, you need to solve the problem with constraints: _n_u2009≀u20093Β·105 (subproblems B1+B2). Note that the number of queries _q_ is limited 1u2009≀u2009_q_u2009≀u2009105 in both subproblem B1 and subproblem B2.
1,700
false
false
true
false
false
false
false
false
false
false
8,520
1118B
Tanya has $$$n$$$ candies numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candy has the weight $$$a_i$$$. She plans to eat exactly $$$n-1$$$ candies and give the remaining candy to her dad. Tanya eats candies in order of increasing their numbers, exactly one candy per day. Your task is to find the number of such candies $$$i$$$ (let's call these candies good) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note that at first, she will give the candy, after it she will eat the remaining candies one by one. For example, $$$n=4$$$ and weights are $$$[1, 4, 3, 3]$$$. Consider all possible cases to give a candy to dad: Tanya gives the $$$1$$$-st candy to dad ($$$a_1=1$$$), the remaining candies are $$$[4, 3, 3]$$$. She will eat $$$a_2=4$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$4+3=7$$$ and in even days she will eat $$$3$$$. Since $$$7 e 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$2$$$-nd candy to dad ($$$a_2=4$$$), the remaining candies are $$$[1, 3, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_3=3$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$3$$$. Since $$$4 e 3$$$ this case shouldn't be counted to the answer (this candy isn't good). Tanya gives the $$$3$$$-rd candy to dad ($$$a_3=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_4=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). Tanya gives the $$$4$$$-th candy to dad ($$$a_4=3$$$), the remaining candies are $$$[1, 4, 3]$$$. She will eat $$$a_1=1$$$ in the first day, $$$a_2=4$$$ in the second day, $$$a_3=3$$$ in the third day. So in odd days she will eat $$$1+3=4$$$ and in even days she will eat $$$4$$$. Since $$$4 = 4$$$ this case should be counted to the answer (this candy is good). In total there $$$2$$$ cases which should counted (these candies are good), so the answer is $$$2$$$. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the number of candies. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^4$$$), where $$$a_i$$$ is the weight of the $$$i$$$-th candy. Output Print one integer β€” the number of such candies $$$i$$$ (good candies) that if dad gets the $$$i$$$-th candy then the sum of weights of candies Tanya eats in even days will be equal to the sum of weights of candies Tanya eats in odd days. Note In the first example indices of good candies are $$$[1, 2]$$$. In the second example indices of good candies are $$$[2, 3]$$$. In the third example indices of good candies are $$$[4, 5, 9]$$$.
1,200
false
false
true
false
false
false
false
false
false
false
5,101
1822D
A permutation is a sequence $$$n$$$ integers, where each integer from $$$1$$$ to $$$n$$$ appears exactly once. For example, $$$[1]$$$, $$$[3,5,2,1,4]$$$, $$$[1,3,2]$$$ are permutations, while $$$[2,3,2]$$$, $$$[4,3,1]$$$, $$$[0]$$$ are not. Given a permutation $$$a$$$, we construct an array $$$b$$$, where $$$b_i = (a_1 + a_2 +~dots~+ a_i) bmod n$$$. A permutation of numbers $$$[a_1, a_2, dots, a_n]$$$ is called a super-permutation if $$$[b_1 + 1, b_2 + 1, dots, b_n + 1]$$$ is also a permutation of length $$$n$$$. Grisha became interested whether a super-permutation of length $$$n$$$ exists. Help him solve this non-trivial problem. Output any super-permutation of length $$$n$$$, if it exists. Otherwise, output $$$-1$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of the test cases follows. Each test case consists of a single line containing one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the length of the desired permutation. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output in a separate line: $$$n$$$ integersxa0β€” a super-permutation of length $$$n$$$, if it exists. $$$-1$$$, otherwise. If there are several suitable permutations, output any of them.
1,200
true
false
false
false
false
true
false
false
false
false
1,348
1214B
There are $$$b$$$ boys and $$$g$$$ girls participating in Olympiad of Metropolises. There will be a board games tournament in the evening and $$$n$$$ participants have accepted the invitation. The organizers do not know how many boys and girls are among them. Organizers are preparing red badges for girls and blue ones for boys. Vasya prepared $$$n+1$$$ decks of badges. The $$$i$$$-th (where $$$i$$$ is from $$$0$$$ to $$$n$$$, inclusive) deck contains $$$i$$$ blue badges and $$$n-i$$$ red ones. The total number of badges in any deck is exactly $$$n$$$. Determine the minimum number of decks among these $$$n+1$$$ that Vasya should take, so that there will be a suitable deck no matter how many girls and boys there will be among the participants of the tournament. Input The first line contains an integer $$$b$$$ ($$$1 le b le 300$$$), the number of boys. The second line contains an integer $$$g$$$ ($$$1 le g le 300$$$), the number of girls. The third line contains an integer $$$n$$$ ($$$1 le n le b + g$$$), the number of the board games tournament participants. Output Output the only integer, the minimum number of badge decks that Vasya could take. Note In the first example, each of 4 decks should be taken: (0 blue, 3 red), (1 blue, 2 red), (2 blue, 1 red), (3 blue, 0 red). In the second example, 4 decks should be taken: (2 blue, 3 red), (3 blue, 2 red), (4 blue, 1 red), (5 blue, 0 red). Piles (0 blue, 5 red) and (1 blue, 4 red) can not be used.
1,100
true
false
false
false
false
false
true
false
false
false
4,610
1798F
Grandfather Ahmed's School has $$$n+1$$$ students. The students are divided into $$$k$$$ classes, and $$$s_i$$$ students study in the $$$i$$$-th class. So, $$$s_1 + s_2 + ldots + s_k = n+1$$$. Due to the upcoming April Fools' Day, all students will receive gifts! Grandfather Ahmed planned to order $$$n+1$$$ boxes of gifts. Each box can contain one or more gifts. He plans to distribute the boxes between classes so that the following conditions are satisfied: 1. Class number $$$i$$$ receives exactly $$$s_i$$$ boxes (so that each student can open exactly one box). 2. The total number of gifts in the boxes received by the $$$i$$$-th class should be a multiple of $$$s_i$$$ (it should be possible to equally distribute the gifts among the $$$s_i$$$ students of this class). Unfortunately, Grandfather Ahmed ordered only $$$n$$$ boxes with gifts, the $$$i$$$-th of which contains $$$a_i$$$ gifts. Ahmed has to buy the missing gift box, and the number of gifts in the box should be an integer between $$$1$$$ and $$$10^6$$$. Help Ahmed to determine, how many gifts should the missing box contain, and build a suitable distribution of boxes to classes, or report that this is impossible. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 200$$$, $$$k le n + 1$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0β€” the number of gifts in the available boxes. The third line contains $$$k$$$ integers $$$s_1, s_2, ldots, s_k$$$ ($$$1 le s_i le n+1$$$)xa0β€” the number of students in classes. It is guaranteed that $$$sum s_i = n+1$$$. Output If there is no way to buy the remaining box, output the integer $$$-1$$$ in a single line. Otherwise, in the first line, output a single integer $$$s$$$xa0β€” the number of gifts in the box that Grandfather Ahmed should buy ($$$1 le s le 10^6$$$). Next, in $$$k$$$ lines, print the distribution of boxes to classes. In the $$$i$$$-th line print $$$s_i$$$ integersxa0β€” the sizes of the boxes that should be sent to the $$$i$$$-th class. If there are multiple solutions, print any of them. Examples Input 18 4 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 6 1 9 3 Output 9 7 1 7 6 5 4 9 1 2 3 8 3 2 9 8 9 6 5 4 Note In the first test, Grandfather Ahmed can buy a box with just $$$1$$$ gift. After that, two boxes with $$$7$$$ gifts are sent to the first class. $$$7 + 7 = 14$$$ is divisible by $$$2$$$. And the second class gets boxes with $$$1, 7, 127$$$ gifts. $$$1 + 7 + 127 = 135$$$ is evenly divisible by $$$3$$$. In the second test, the classes have sizes $$$6$$$, $$$1$$$, $$$9$$$, and $$$3$$$. We show that the available boxes are enough to distribute into classes with sizes $$$6$$$, $$$9$$$, $$$3$$$, and in the class with size $$$1$$$, you can buy a box of any size. In class with size $$$6$$$ we send boxes with sizes $$$7$$$, $$$1$$$, $$$7$$$, $$$6$$$, $$$5$$$, $$$4$$$. $$$7 + 1 + 7 + 6 + 5 + 4 = 30$$$ is divisible by $$$6$$$. In class with size $$$9$$$ we send boxes with sizes $$$1$$$, $$$2$$$, $$$3$$$, $$$8$$$, $$$3$$$, $$$2$$$, $$$9$$$, $$$8$$$, $$$9$$$. $$$1 + 2 + 3 + 8 + 3 + 2 + 9 + 8 + 9 = 45$$$ is divisible by $$$9$$$. The remaining boxes ($$$6$$$, $$$5$$$, $$$4$$$) are sent to the class with size $$$3$$$. $$$6 + 5 + 4 = 15$$$ is divisible by $$$3$$$.
2,500
true
false
false
true
false
false
false
false
false
false
1,485
1093C
Mishka is trying really hard to avoid being kicked out of the university. In particular, he was doing absolutely nothing for the whole semester, miraculously passed some exams so that just one is left. There were $$$n$$$ classes of that subject during the semester and on $$$i$$$-th class professor mentioned some non-negative integer $$$a_i$$$ to the students. It turned out, the exam was to tell the whole sequence back to the professor. Sounds easy enough for those who attended every class, doesn't it? Obviously Mishka didn't attend any classes. However, professor left some clues on the values of $$$a$$$ to help out students like Mishka: $$$a$$$ was sorted in non-decreasing order ($$$a_1 le a_2 le dots le a_n$$$); $$$n$$$ was even; the following sequence $$$b$$$, consisting of $$$frac n 2$$$ elements, was formed and given out to students: $$$b_i = a_i + a_{n - i + 1}$$$. Professor also mentioned that any sequence $$$a$$$, which produces sequence $$$b$$$ with the presented technique, will be acceptable. Help Mishka to pass that last exam. Restore any sorted sequence $$$a$$$ of non-negative integers, which produces sequence $$$b$$$ with the presented technique. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) β€” the length of sequence $$$a$$$. $$$n$$$ is always even. The second line contains $$$frac n 2$$$ integers $$$b_1, b_2, dots, b_{frac n 2}$$$ ($$$0 le b_i le 10^{18}$$$) β€” sequence $$$b$$$, where $$$b_i = a_i + a_{n - i + 1}$$$. It is guaranteed that there exists at least one correct sequence $$$a$$$, which produces the given sequence $$$b$$$. Output Print $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^{18}$$$) in a single line. $$$a_1 le a_2 le dots le a_n$$$ should be satisfied. $$$b_i = a_i + a_{n - i + 1}$$$ should be satisfied for all valid $$$i$$$.
1,300
false
true
false
false
false
false
false
false
false
false
5,242
1280B
You are an all-powerful being and you have created a rectangular world. In fact, your world is so bland that it could be represented by a $$$r imes c$$$ grid. Each cell on the grid represents a country. Each country has a dominant religion. There are only two religions in your world. One of the religions is called Beingawesomeism, who do good for the sake of being good. The other religion is called Pushingittoofarism, who do murders for the sake of being bad. Oh, and you are actually not really all-powerful. You just have one power, which you can use infinitely many times! Your power involves missionary groups. When a missionary group of a certain country, say $$$a$$$, passes by another country $$$b$$$, they change the dominant religion of country $$$b$$$ to the dominant religion of country $$$a$$$. In particular, a single use of your power is this: You choose a horizontal $$$1 imes x$$$ subgrid or a vertical $$$x imes 1$$$ subgrid. That value of $$$x$$$ is up to you; You choose a direction $$$d$$$. If you chose a horizontal subgrid, your choices will either be NORTH or SOUTH. If you choose a vertical subgrid, your choices will either be EAST or WEST; You choose the number $$$s$$$ of steps; You command each country in the subgrid to send a missionary group that will travel $$$s$$$ steps towards direction $$$d$$$. In each step, they will visit (and in effect convert the dominant religion of) all $$$s$$$ countries they pass through, as detailed above. The parameters $$$x$$$, $$$d$$$, $$$s$$$ must be chosen in such a way that any of the missionary groups won't leave the grid. The following image illustrates one possible single usage of your power. Here, A represents a country with dominant religion Beingawesomeism and P represents a country with dominant religion Pushingittoofarism. Here, we've chosen a $$$1 imes 4$$$ subgrid, the direction NORTH, and $$$s = 2$$$ steps. You are a being which believes in free will, for the most part. However, you just really want to stop receiving murders that are attributed to your name. Hence, you decide to use your powers and try to make Beingawesomeism the dominant religion in every country. What is the minimum number of usages of your power needed to convert everyone to Beingawesomeism? With god, nothing is impossible. But maybe you're not god? If it is impossible to make Beingawesomeism the dominant religion in all countries, you must also admit your mortality and say so. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 2cdot 10^4$$$) denoting the number of test cases. The first line of each test case contains two space-separated integers $$$r$$$ and $$$c$$$ denoting the dimensions of the grid ($$$1 le r, c le 60$$$). The next $$$r$$$ lines each contains $$$c$$$ characters describing the dominant religions in the countries. In particular, the $$$j$$$-th character in the $$$i$$$-th line describes the dominant religion in the country at the cell with row $$$i$$$ and column $$$j$$$, where: "A" means that the dominant religion is Beingawesomeism; "P" means that the dominant religion is Pushingittoofarism. It is guaranteed that the grid will only contain "A" or "P" characters. It is guaranteed that the sum of the $$$r cdot c$$$ in a single file is at most $$$3 cdot 10^6$$$. Output For each test case, output a single line containing the minimum number of usages of your power needed to convert everyone to Beingawesomeism, or the string "MORTAL" (without quotes) if it is impossible to do so. Note In the first test case, it can be done in two usages, as follows: Usage 1: Usage 2: In the second test case, it can be done with just one usage of the power. In the third test case, it is impossible to convert everyone to Beingawesomeism, so the answer is "MORTAL".
1,800
true
false
true
false
false
false
false
false
false
false
4,293
1196B
You are given an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$. You want to split it into exactly $$$k$$$ non-empty non-intersecting subsegments such that each subsegment has odd sum (i.u2009e. for each subsegment, the sum of all elements that belong to this subsegment is odd). It is impossible to rearrange (shuffle) the elements of a given array. Each of the $$$n$$$ elements of the array $$$a$$$ must belong to exactly one of the $$$k$$$ subsegments. Let's see some examples of dividing the array of length $$$5$$$ into $$$3$$$ subsegments (not necessarily with odd sums): $$$[1, 2, 3, 4, 5]$$$ is the initial array, then all possible ways to divide it into $$$3$$$ non-empty non-intersecting subsegments are described below: $$$[1], [2], [3, 4, 5]$$$; $$$[1], [2, 3], [4, 5]$$$; $$$[1], [2, 3, 4], [5]$$$; $$$[1, 2], [3], [4, 5]$$$; $$$[1, 2], [3, 4], [5]$$$; $$$[1, 2, 3], [4], [5]$$$. Of course, it can be impossible to divide the initial array into exactly $$$k$$$ subsegments in such a way that each of them will have odd sum of elements. In this case print "NO". Otherwise, print "YES" and any possible division of the array. See the output format for the detailed explanation. You have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) β€” the number of queries. Then $$$q$$$ queries follow. The first line of the query contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$) β€” the number of elements in the array and the number of subsegments, respectively. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each query, print the answer to it. If it is impossible to divide the initial array into exactly $$$k$$$ subsegments in such a way that each of them will have odd sum of elements, print "NO" in the first line. Otherwise, print "YES" in the first line and any possible division of the array in the second line. The division can be represented as $$$k$$$ integers $$$r_1$$$, $$$r_2$$$, ..., $$$r_k$$$ such that $$$1 le r_1 < r_2 < dots < r_k = n$$$, where $$$r_j$$$ is the right border of the $$$j$$$-th segment (the index of the last element that belongs to the $$$j$$$-th segment), so the array is divided into subsegments $$$[1; r_1], [r_1 + 1; r_2], [r_2 + 1, r_3], dots, [r_{k - 1} + 1, n]$$$. Note that $$$r_k$$$ is always $$$n$$$ but you should print it anyway. Example Input 3 5 3 7 18 3 14 1 5 4 1 2 3 4 5 6 2 1 2 8 4 10 2
1,200
true
false
false
false
false
true
false
false
false
false
4,721
1731F
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 841 (Div. 2) and Divide by Zero 2022 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 dp fft math *2500 No tag edit access β†’ Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST F. Function Sum time limit per test4 seconds memory limit per test256 megabytes Suppose you have an integer array $$$a_1, a_2, dots, a_n$$$. Let $$$operatorname{lsl}(i)$$$ be the number of indices $$$j$$$ ($$$1 le j < i$$$) such that $$$a_j < a_i$$$. Analogically, let $$$operatorname{grr}(i)$$$ be the number of indices $$$j$$$ ($$$i < j le n$$$) such that $$$a_j > a_i$$$. Let's name position $$$i$$$ good in the array $$$a$$$ if $$$operatorname{lsl}(i) < operatorname{grr}(i)$$$. Finally, let's define a function $$$f$$$ on array $$$a$$$ $$$f(a)$$$ as the sum of all $$$a_i$$$ such that $$$i$$$ is good in $$$a$$$. Given two integers $$$n$$$ and $$$k$$$, find the sum of $$$f(a)$$$ over all arrays $$$a$$$ of size $$$n$$$ such that $$$1 leq a_i leq k$$$ for all $$$1 leq i leq n$$$ modulo $$$998,244,353$$$. Input The first and only line contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 50$$$; $$$2 leq k < 998,244,353$$$). Output Output a single integerxa0β€” the sum of $$$f$$$ over all arrays $$$a$$$ of size $$$n$$$ modulo $$$998,244,353$$$. Examples input 3 3 output 28 input 5 6 output 34475 input 12 30 output 920711694 Note In the first test case: $$$f([1,1,1]) = 0$$$ $$$f([2,2,3]) = 2 + 2 = 4$$$ $$$f([1,1,2]) = 1 + 1 = 2$$$ $$$f([2,3,1]) = 2$$$ $$$f([1,1,3]) = 1 + 1 = 2$$$ $$$f([2,3,2]) = 2$$$ $$$f([1,2,1]) = 1$$$ $$$f([2,3,3]) = 2$$$ $$$f([1,2,2]) = 1$$$ $$$f([3,1,1]) = 0$$$ $$$f([1,2,3]) = 1$$$ $$$f([3,1,2]) = 1$$$ $$$f([1,3,1]) = 1$$$ $$$f([3,1,3]) = 1$$$ $$$f([1,3,2]) = 1$$$ $$$f([3,2,1]) = 0$$$ $$$f([1,3,3]) = 1$$$ $$$f([3,2,2]) = 0$$$ $$$f([2,1,1]) = 0$$$ $$$f([3,2,3]) = 2$$$ $$$f([2,1,2]) = 1$$$ $$$f([3,3,1]) = 0$$$ $$$f([2,1,3]) = 2 + 1 = 3$$$ $$$f([3,3,2]) = 0$$$ $$$f([2,2,1]) = 0$$$ $$$f([3,3,3]) = 0$$$ $$$f([2,2,2]) = 0$$$ Adding up all of these values, we get $$$28$$$ as the answer. Codeforces (c)
2,500
true
false
false
true
false
false
true
false
false
false
1,900
1498D
You have a malfunctioning microwave in which you want to put some bananas. You have $$$n$$$ time-steps before the microwave stops working completely. At each time-step, it displays a new operation. Let $$$k$$$ be the number of bananas in the microwave currently. Initially, $$$k = 0$$$. In the $$$i$$$-th operation, you are given three parameters $$$t_i$$$, $$$x_i$$$, $$$y_i$$$ in the input. Based on the value of $$$t_i$$$, you must do one of the following: Type 1: ($$$t_i=1$$$, $$$x_i$$$, $$$y_i$$$) β€” pick an $$$a_i$$$, such that $$$0 le a_i le y_i$$$, and perform the following update $$$a_i$$$ times: $$$k:=lceil (k + x_i) ceil$$$. Type 2: ($$$t_i=2$$$, $$$x_i$$$, $$$y_i$$$) β€” pick an $$$a_i$$$, such that $$$0 le a_i le y_i$$$, and perform the following update $$$a_i$$$ times: $$$k:=lceil (k cdot x_i) ceil$$$. Note that $$$x_i$$$ can be a fractional value. See input format for more details. Also, $$$lceil x ceil$$$ is the smallest integer $$$ge x$$$. At the $$$i$$$-th time-step, you must apply the $$$i$$$-th operation exactly once. For each $$$j$$$ such that $$$1 le j le m$$$, output the earliest time-step at which you can create exactly $$$j$$$ bananas. If you cannot create exactly $$$j$$$ bananas, output $$$-1$$$. Input The first line contains two space-separated integers $$$n$$$ $$$(1 le n le 200)$$$ and $$$m$$$ $$$(2 le m le 10^5)$$$. Then, $$$n$$$ lines follow, where the $$$i$$$-th line denotes the operation for the $$$i$$$-th timestep. Each such line contains three space-separated integers $$$t_i$$$, $$$x'_i$$$ and $$$y_i$$$ ($$$1 le t_i le 2$$$, $$$1le y_ile m$$$). Note that you are given $$$x'_i$$$, which is $$$10^5 cdot x_i$$$. Thus, to obtain $$$x_i$$$, use the formula $$$x_i= dfrac{x'_i} {10^5}$$$. For type 1 operations, $$$1 le x'_i le 10^5 cdot m$$$, and for type 2 operations, $$$10^5 < x'_i le 10^5 cdot m$$$. Output Print $$$m$$$ integers, where the $$$i$$$-th integer is the earliest time-step when you can obtain exactly $$$i$$$ bananas (or $$$-1$$$ if it is impossible). Examples Input 3 20 1 300000 2 2 400000 2 1 1000000 3 Output -1 -1 1 -1 -1 1 -1 -1 -1 3 -1 2 3 -1 -1 3 -1 -1 -1 3 Input 3 20 1 399999 2 2 412345 2 1 1000001 3 Output -1 -1 -1 1 -1 -1 -1 1 -1 -1 3 -1 -1 -1 3 -1 2 -1 3 -1 Note In the first sample input, let us see how to create $$$16$$$ number of bananas in three timesteps. Initially, $$$k=0$$$. In timestep 1, we choose $$$a_1=2$$$, so we apply the type 1 update β€” $$$k := lceil(k+3) ceil$$$ β€” two times. Hence, $$$k$$$ is now 6. In timestep 2, we choose $$$a_2=0$$$, hence value of $$$k$$$ remains unchanged. In timestep 3, we choose $$$a_3=1$$$, so we are applying the type 1 update $$$k:= lceil(k+10) ceil$$$ once. Hence, $$$k$$$ is now 16. It can be shown that $$$k=16$$$ cannot be reached in fewer than three timesteps with the given operations. In the second sample input, let us see how to create $$$17$$$ number of bananas in two timesteps. Initially, $$$k=0$$$. In timestep 1, we choose $$$a_1=1$$$, so we apply the type 1 update β€” $$$k := lceil(k+3.99999) ceil$$$ β€” once. Hence, $$$k$$$ is now 4. In timestep 2, we choose $$$a_2=1$$$, so we apply the type 2 update β€” $$$k := lceil(kcdot 4.12345) ceil$$$ β€” once. Hence, $$$k$$$ is now 17. It can be shown that $$$k=17$$$ cannot be reached in fewer than two timesteps with the given operations.
2,200
false
false
true
true
false
false
false
false
false
true
3,178
1791C
Timur initially had a binary string$$$^{dagger}$$$ $$$s$$$ (possibly of length $$$0$$$). He performed the following operation several (possibly zero) times: Add $$$ exttt{0}$$$ to one end of the string and $$$ exttt{1}$$$ to the other end of the string. For example, starting from the string $$$ exttt{1011}$$$, you can obtain either $$$color{red}{ exttt{0}} exttt{1011}color{red}{ exttt{1}}$$$ or $$$color{red}{ exttt{1}} exttt{1011}color{red}{ exttt{0}}$$$. You are given Timur's final string. What is the length of the shortest possible string he could have started with? $$$^{dagger}$$$ A binary string is a string (possibly the empty string) whose characters are either $$$ exttt{0}$$$ or $$$ exttt{1}$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β€” the number of testcases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2000$$$)xa0β€” the length of Timur's final string. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of characters $$$ exttt{0}$$$ or $$$ exttt{1}$$$, denoting the final string. Output For each test case, output a single nonnegative integerxa0β€” the shortest possible length of Timur's original string. Note that Timur's original string could have been empty, in which case you should output $$$0$$$. Note In the first test case, the shortest possible string Timur started with is $$$ exttt{0}$$$, and he performed the following operation: $$$ exttt{0} o color{red}{ exttt{1}} exttt{0}color{red}{ exttt{0}}$$$. In the second test case, the shortest possible string Timur started with is $$$ exttt{11}$$$, and he performed the following operation: $$$ exttt{11} o color{red}{ exttt{0}} exttt{11}color{red}{ exttt{1}}$$$. In the third test case, the shortest possible string Timur started with is $$$ exttt{10101}$$$, and he didn't perform any operations. In the fourth test case, the shortest possible string Timur started with is the empty string (which we denote by $$$varepsilon$$$), and he performed the following operations: $$$varepsilon o color{red}{ exttt{1}} exttt{}color{red}{ exttt{0}} o color{red}{ exttt{0}} exttt{10}color{red}{ exttt{1}} o color{red}{ exttt{1}} exttt{0101}color{red}{ exttt{0}}$$$. In the fifth test case, the shortest possible string Timur started with is $$$ exttt{101}$$$, and he performed the following operations: $$$ exttt{101} o color{red}{ exttt{0}} exttt{101}color{red}{ exttt{1}} o color{red}{ exttt{1}} exttt{01011}color{red}{ exttt{0}}$$$.
800
false
false
true
false
false
false
false
false
false
false
1,533
429B
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix _a_ with _n_ lines and _m_ columns. Let number _a_[_i_][_j_] represents the calories burned by performing workout at the cell of gym in the _i_-th line and the _j_-th column. Iahub starts with workout located at line 1 and column 1. He needs to finish with workout _a_[_n_][_m_]. After finishing workout _a_[_i_][_j_], he can go to workout _a_[_i_u2009+u20091][_j_] or _a_[_i_][_j_u2009+u20091]. Similarly, Iahubina starts with workout _a_[_n_][1] and she needs to finish with workout _a_[1][_m_]. After finishing workout from cell _a_[_i_][_j_], she goes to either _a_[_i_][_j_u2009+u20091] or _a_[_i_u2009-u20091][_j_]. There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout. If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs. Input The first line of the input contains two integers _n_ and _m_ (3u2009≀u2009_n_,u2009_m_u2009≀u20091000). Each of the next _n_ lines contains _m_ integers: _j_-th number from _i_-th line denotes element _a_[_i_][_j_] (0u2009≀u2009_a_[_i_][_j_]u2009≀u2009105). Output The output contains a single number β€” the maximum total gain possible. Examples Input 3 3 100 100 100 100 1 100 100 100 100 Note Iahub will choose exercises _a_[1][1]u2009β†’u2009_a_[1][2]u2009β†’u2009_a_[2][2]u2009β†’u2009_a_[3][2]u2009β†’u2009_a_[3][3]. Iahubina will choose exercises _a_[3][1]u2009β†’u2009_a_[2][1]u2009β†’u2009_a_[2][2]u2009β†’u2009_a_[2][3]u2009β†’u2009_a_[1][3].
1,600
false
false
false
true
false
false
false
false
false
false
8,125
1571C
Let's say that two strings $$$s$$$ and $$$t$$$ rhyme if both strings have length at least $$$k$$$, and their last $$$k$$$ characters are equal. For example, if $$$k = 3$$$, the strings abcd and cebcd rhyme, the strings ab and ab don't rhyme, the strings aaaa and aaaaa rhyme, the strings abcd and abce don't rhyme. You have $$$n$$$ pairs of strings $$$(s_i, t_i)$$$, and for each pair of strings you know, should they rhyme or should not. Find all possible non-negative integer values for $$$k$$$ such that pairs that have to rhyme, rhyme and pairs that must not rhyme, don't rhyme. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β€” the number of string pairs. Next $$$n$$$ lines contains descriptions of pairsxa0β€” one per line. The $$$i$$$-th line contains space-separated strings $$$s_i$$$ and $$$t_i$$$ and marker $$$r_i$$$. Strings are non-empty, consist of lowercase Latin letters and each have length at most $$$2 cdot 10^5$$$. The marker $$$r_i$$$ equals to $$$1$$$ if strings have to rhyme, or $$$0$$$ if they must not rhyme. It's guaranteed that for each test case there is at least one pair with $$$r_i$$$ equal to $$$1$$$ and that the total length of all strings over all test cases doesn't exceed $$$4 cdot 10^5$$$. Output For each test case, firstly print integer $$$m$$$xa0β€” the number of possible non-negative integer values of $$$k$$$ such that pairs that have to rhyme, rhyme and pairs that must not rhyme, don't rhyme. Next, print all these values of $$$k$$$ (without repetitions). You can print them in any order. Example Input 3 1 kotlin heroes 1 2 join kotlin 1 episode eight 0 4 abc abcdef 0 xyz zzz 1 aaa bba 0 c d 0 Note In the first test case, if $$$k$$$ is at least $$$1$$$ then kotlin and heroes don't rhyme. In the second test case, for $$$k = 2$$$ join and kotlin rhyme, and episode and eight don't rhyme.
1,800
false
false
true
false
false
false
false
false
false
false
2,790
1327G
You are given a string $$$S$$$ and an array of strings $$$[t_1, t_2, dots, t_k]$$$. Each string $$$t_i$$$ consists of lowercase Latin letters from a to n; $$$S$$$ consists of lowercase Latin letters from a to n and no more than $$$14$$$ question marks. Each string $$$t_i$$$ has its cost $$$c_i$$$ β€” an integer number. The value of some string $$$T$$$ is calculated as $$$sumlimits_{i = 1}^{k} F(T, t_i) cdot c_i$$$, where $$$F(T, t_i)$$$ is the number of occurences of string $$$t_i$$$ in $$$T$$$ as a substring. For example, $$$F( ext{aaabaaa}, ext{aa}) = 4$$$. You have to replace all question marks in $$$S$$$ with pairwise distinct lowercase Latin letters from a to n so the value of $$$S$$$ is maximum possible. Input The first line contains one integer $$$k$$$ ($$$1 le k le 1000$$$) β€” the number of strings in the array $$$[t_1, t_2, dots, t_k]$$$. Then $$$k$$$ lines follow, each containing one string $$$t_i$$$ (consisting of lowercase Latin letters from a to n) and one integer $$$c_i$$$ ($$$1 le t_i le 1000$$$, $$$-10^6 le c_i le 10^6$$$). The sum of lengths of all strings $$$t_i$$$ does not exceed $$$1000$$$. The last line contains one string $$$S$$$ ($$$1 le S le 4 cdot 10^5$$$) consisting of lowercase Latin letters from a to n and question marks. The number of question marks in $$$S$$$ is not greater than $$$14$$$. Output Print one integer β€” the maximum value of $$$S$$$ after replacing all question marks with pairwise distinct lowercase Latin letters from a to n. Examples Input 4 abc -10 a 1 b 1 c 3 ?b? Input 1 a -1 ????????????? Input 1 a -1 ??????????????
2,800
false
false
false
true
false
false
false
false
false
false
4,072
902A
Pig is visiting a friend. Pig's house is located at point 0, and his friend's house is located at point _m_ on an axis. Pig can use teleports to move along the axis. To use a teleport, Pig should come to a certain point (where the teleport is located) and choose where to move: for each teleport there is the rightmost point it can move Pig to, this point is known as the limit of the teleport. Formally, a teleport located at point _x_ with limit _y_ can move Pig from point _x_ to any point within the segment [_x_;u2009_y_], including the bounds. Determine if Pig can visit the friend using teleports only, or he should use his car. Input The first line contains two integers _n_ and _m_ (1u2009≀u2009_n_u2009≀u2009100,u20091u2009≀u2009_m_u2009≀u2009100)xa0β€” the number of teleports and the location of the friend's house. The next _n_ lines contain information about teleports. The _i_-th of these lines contains two integers _a__i_ and _b__i_ (0u2009≀u2009_a__i_u2009≀u2009_b__i_u2009≀u2009_m_), where _a__i_ is the location of the _i_-th teleport, and _b__i_ is its limit. It is guaranteed that _a__i_u2009β‰₯u2009_a__i_u2009-u20091 for every _i_ (2u2009≀u2009_i_u2009≀u2009_n_). Output Print "YES" if there is a path from Pig's house to his friend's house that uses only teleports, and "NO" otherwise. You can print each letter in arbitrary case (upper or lower). Note The first example is shown on the picture below: Pig can use the first teleport from his house (point 0) to reach point 2, then using the second teleport go from point 2 to point 3, then using the third teleport go from point 3 to point 5, where his friend lives. The second example is shown on the picture below: You can see that there is no path from Pig's house to his friend's house that uses only teleports.
1,100
false
true
true
false
false
false
false
false
false
false
6,136
421D
Recently a serious bug has been found in the FOS code. The head of the F company wants to find the culprit and punish him. For that, he set up an organizational meeting, the issue is: who's bugged the code? Each of the _n_ coders on the meeting said: 'I know for sure that either _x_ or _y_ did it!' The head of the company decided to choose two suspects and invite them to his office. Naturally, he should consider the coders' opinions. That's why the head wants to make such a choice that at least _p_ of _n_ coders agreed with it. A coder agrees with the choice of two suspects if at least one of the two people that he named at the meeting was chosen as a suspect. In how many ways can the head of F choose two suspects? Note that even if some coder was chosen as a suspect, he can agree with the head's choice if he named the other chosen coder at the meeting. Input The first line contains integers _n_ and _p_ (3u2009≀u2009_n_u2009≀u20093Β·105;xa00u2009≀u2009_p_u2009≀u2009_n_) β€” the number of coders in the F company and the minimum number of agreed people. Each of the next _n_ lines contains two integers _x__i_, _y__i_ (1u2009≀u2009_x__i_,u2009_y__i_u2009≀u2009_n_) β€” the numbers of coders named by the _i_-th coder. It is guaranteed that _x__i_u2009β‰ u2009_i_,u2009xa0_y__i_u2009β‰ u2009_i_,u2009xa0_x__i_u2009β‰ u2009_y__i_. Output Print a single integer β€” the number of possible two-suspect sets. Note that the order of the suspects doesn't matter, that is, sets (1,u20092) and (2,u20091) are considered identical. Examples Input 8 6 5 6 5 7 5 8 6 2 2 1 7 3 1 3 1 4
1,900
false
false
false
false
true
false
false
true
true
false
8,144
1821F
There is a beautiful alley with trees in front of a shopping mall. Unfortunately, it has to go to make space for the parking lot. The trees on the alley all grow in a single line. There are $$$n$$$ spots for trees, index $$$0$$$ is the shopping mall, index $$$n+1$$$ is the road and indices from $$$1$$$ to $$$n$$$ are the spots for trees. Some of them are takenxa0β€” there grow trees of the same height $$$k$$$. No more than one tree grows in each spot. When you chop down a tree in the spot $$$x$$$, you can make it fall either left or right. If it falls to the left, it takes up spots from $$$x-k$$$ to $$$x$$$, inclusive. If it falls to the right, it takes up spots from $$$x$$$ to $$$x+k$$$, inclusive. Let $$$m$$$ trees on the alley grow in some spots $$$x_1, x_2, dots, x_m$$$. Let an alley be called unfortunate if all $$$m$$$ trees can be chopped down in such a way that: no tree falls on the shopping mall or the road; each spot is taken up by no more than one fallen tree. Calculate the number of different unfortunate alleys with $$$m$$$ trees of height $$$k$$$. Two alleys are considered different if there is a spot $$$y$$$ such that a tree grows in $$$y$$$ on the first alley and doesn't grow in $$$y$$$ on the second alley. Output the number modulo $$$998,244,353$$$. Input The only line contains three integers $$$n, m$$$ and $$$k$$$ ($$$1 le m, k le n le 3 cdot 10^5$$$)xa0β€” the number of spots for the trees, the number of trees and the height of each tree. Output Print a single integerxa0β€” the number of different unfortunate alleys with $$$m$$$ trees of height $$$k$$$, modulo $$$998,244,353$$$.
2,600
true
false
false
true
false
false
false
false
false
false
1,352
196B
We've got a rectangular _n_u2009Γ—u2009_m_-cell maze. Each cell is either passable, or is a wall (impassable). A little boy found the maze and cyclically tiled a plane with it so that the plane became an infinite maze. Now on this plane cell (_x_,u2009_y_) is a wall if and only if cell is a wall. In this problem is a remainder of dividing number _a_ by number _b_. The little boy stood at some cell on the plane and he wondered whether he can walk infinitely far away from his starting position. From cell (_x_,u2009_y_) he can go to one of the following cells: (_x_,u2009_y_u2009-u20091), (_x_,u2009_y_u2009+u20091), (_x_u2009-u20091,u2009_y_) and (_x_u2009+u20091,u2009_y_), provided that the cell he goes to is not a wall. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u20091500) β€” the height and the width of the maze that the boy used to cyclically tile the plane. Each of the next _n_ lines contains _m_ characters β€” the description of the labyrinth. Each character is either a "#", that marks a wall, a ".", that marks a passable cell, or an "S", that marks the little boy's starting point. The starting point is a passable cell. It is guaranteed that character "S" occurs exactly once in the input. Output Print "Yes" (without the quotes), if the little boy can walk infinitely far from the starting point. Otherwise, print "No" (without the quotes). Examples Input 5 4 ##.# ##S# #..# #.## #..# Input 5 4 ##.# ##S# #..# ..#. #.## Note In the first sample the little boy can go up for infinitely long as there is a "clear path" that goes vertically. He just needs to repeat the following steps infinitely: up, up, left, up, up, right, up. In the second sample the vertical path is blocked. The path to the left doesn't work, too β€” the next "copy" of the maze traps the boy.
2,000
false
false
false
false
false
false
false
false
false
true
9,057
1130A
You are given an array of $$$n$$$ integers: $$$a_1, a_2, ldots, a_n$$$. Your task is to find some non-zero integer $$$d$$$ ($$$-10^3 leq d leq 10^3$$$) such that, after each number in the array is divided by $$$d$$$, the number of positive numbers that are presented in the array is greater than or equal to half of the array size (i.e., at least $$$lceilfrac{n}{2} ceil$$$). Note that those positive numbers do not need to be an integer (e.g., a $$$2.5$$$ counts as a positive number). If there are multiple values of $$$d$$$ that satisfy the condition, you may print any of them. In case that there is no such $$$d$$$, print a single integer $$$0$$$. Recall that $$$lceil x ceil$$$ represents the smallest integer that is not less than $$$x$$$ and that zero ($$$0$$$) is neither positive nor negative. Input The first line contains one integer $$$n$$$ ($$$1 le n le 100$$$)xa0β€” the number of elements in the array. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^3 le a_i le 10^3$$$). Output Print one integer $$$d$$$ ($$$-10^3 leq d leq 10^3$$$ and $$$d eq 0$$$) that satisfies the given condition. If there are multiple values of $$$d$$$ that satisfy the condition, you may print any of them. In case that there is no such $$$d$$$, print a single integer $$$0$$$. Note In the first sample, $$$n = 5$$$, so we need at least $$$lceilfrac{5}{2} ceil = 3$$$ positive numbers after division. If $$$d = 4$$$, the array after division is $$$[2.5, 0, -1.75, 0.5, 1.5]$$$, in which there are $$$3$$$ positive numbers (namely: $$$2.5$$$, $$$0.5$$$, and $$$1.5$$$). In the second sample, there is no valid $$$d$$$, so $$$0$$$ should be printed.
800
false
false
true
false
false
false
false
false
false
false
5,071
526E
Optimizing the amount of data transmitted via a network is an important and interesting part of developing any network application. In one secret game developed deep in the ZeptoLab company, the game universe consists of _n_ levels, located in a circle. You can get from level _i_ to levels _i_u2009-u20091 and _i_u2009+u20091, also you can get from level 1 to level _n_ and vice versa. The map of the _i_-th level description size is _a__i_ bytes. In order to reduce the transmitted traffic, the game gets levels as follows. All the levels on the server are divided into _m_ groups and each time a player finds himself on one of the levels of a certain group for the first time, the server sends all levels of the group to the game client as a single packet. Thus, when a player travels inside the levels of a single group, the application doesn't need any new information. Due to the technical limitations the packet can contain an arbitrary number of levels but their total size mustn't exceed _b_ bytes, where _b_ is some positive integer constant. Usual situation is that players finish levels one by one, that's why a decision was made to split _n_ levels into _m_ groups so that each group was a continuous segment containing multiple neighboring levels (also, the group can have two adjacent levels, _n_ and 1). Specifically, if the descriptions of all levels have the total weight of at most _b_ bytes, then they can all be united into one group to be sent in a single packet. Determine, what minimum number of groups do you need to make in order to organize the levels of the game observing the conditions above? As developing a game is a long process and technology never stagnates, it is yet impossible to predict exactly what value will take constant value _b_ limiting the packet size when the game is out. That's why the developers ask you to find the answer for multiple values of _b_. Input The first line contains two integers _n_, _q_ (2u2009≀u2009_n_u2009≀u2009106, 1u2009≀u2009_q_u2009≀u200950) β€” the number of levels in the game universe and the number of distinct values of _b_ that you need to process. The second line contains _n_ integers _a__i_ (1u2009≀u2009_a__i_u2009≀u2009109) β€” the sizes of the levels in bytes. The next _q_ lines contain integers _b__j_ (), determining the values of constant _b_, for which you need to determine the answer. Output For each value of _k__j_ from the input print on a single line integer _m__j_ (1u2009≀u2009_m__j_u2009≀u2009_n_), determining the minimum number of groups to divide game levels into for transmission via network observing the given conditions.
2,400
false
false
true
true
false
false
false
false
false
false
7,727
1744C
You find yourself on an unusual crossroad with a weird traffic light. That traffic light has three possible colors: red (r), yellow (y), green (g). It is known that the traffic light repeats its colors every $$$n$$$ seconds and at the $$$i$$$-th second the color $$$s_i$$$ is on. That way, the order of the colors is described by a string. For example, if $$$s=$$$"rggry", then the traffic light works as the following: red-green-green-red-yellow-red-green-green-red-yellow- ... and so on. More formally, you are given a string $$$s_1, s_2, ldots, s_n$$$ of length $$$n$$$. At the first second the color $$$s_1$$$ is on, at the second β€” $$$s_2$$$, ..., at the $$$n$$$-th second the color $$$s_n$$$ is on, at the $$$n + 1$$$-st second the color $$$s_1$$$ is on and so on. You need to cross the road and that can only be done when the green color is on. You know which color is on the traffic light at the moment, but you don't know the current moment of time. You need to find the minimum amount of time in which you are guaranteed to cross the road. You can assume that you cross the road immediately. For example, with $$$s=$$$"rggry" and the current color r there are two options: either the green color will be on after $$$1$$$ second, or after $$$3$$$. That way, the answer is equal to $$$3$$$ β€” that is the number of seconds that we are guaranteed to cross the road, if the current color is r. Input The first line contains a single integer $$$t$$$ $$$(1 leq t leq 10^4$$$) β€” the number of test cases. Then the description of the test cases follows. The first line of each test case contains an integer $$$n$$$ and a symbol $$$c$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$c$$$ is one of allowed traffic light colors r, y or g)β€” the length of the string $$$s$$$ and the current color of the traffic light. The second line of each test case contains a string $$$s$$$ of the length $$$n$$$, consisting of the letters r, y and g. It is guaranteed that the symbol g is in the string $$$s$$$ and the symbol $$$c$$$ is in the string $$$s$$$. It is guaranteed, that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Note The first test case is explained in the statement. In the second test case the green color is on so you can cross the road immediately. In the third test case, if the red color was on at the second second, then we would wait for the green color for one second, and if the red light was on at the first second, then we would wait for the green light for two seconds. In the fourth test case the longest we would wait for the green color is if we wait for it starting from the fifth second.
1,000
false
false
true
false
false
false
false
true
false
false
1,815
48A
Uncle Fyodor, Matroskin the Cat and Sharic the Dog live their simple but happy lives in Prostokvashino. Sometimes they receive parcels from Uncle Fyodor’s parents and sometimes from anonymous benefactors, in which case it is hard to determine to which one of them the package has been sent. A photographic rifle is obviously for Sharic who loves hunting and fish is for Matroskin, but for whom was a new video game console meant? Every one of the three friends claimed that the present is for him and nearly quarreled. Uncle Fyodor had an idea how to solve the problem justly: they should suppose that the console was sent to all three of them and play it in turns. Everybody got relieved but then yet another burning problem popped up β€” who will play first? This time Matroskin came up with a brilliant solution, suggesting the most fair way to find it out: play rock-paper-scissors together. The rules of the game are very simple. On the count of three every player shows a combination with his hand (or paw). The combination corresponds to one of three things: a rock, scissors or paper. Some of the gestures win over some other ones according to well-known rules: the rock breaks the scissors, the scissors cut the paper, and the paper gets wrapped over the stone. Usually there are two players. Yet there are three friends, that’s why they decided to choose the winner like that: If someone shows the gesture that wins over the other two players, then that player wins. Otherwise, another game round is required. Write a program that will determine the winner by the gestures they have shown. Input The first input line contains the name of the gesture that Uncle Fyodor showed, the second line shows which gesture Matroskin showed and the third line shows Sharic’s gesture. Output Print "F" (without quotes) if Uncle Fyodor wins. Print "M" if Matroskin wins and "S" if Sharic wins. If it is impossible to find the winner, print "?".
900
false
false
true
false
false
false
false
false
false
false
9,737
1875C
Jellyfish has $$$n$$$ green apple pieces. Each green apple piece weighs $$$1~ ext{kg}$$$. Jellyfish wants to divide these green apple pieces equally among $$$m$$$ people. Jellyfish has a magic knife. Each time Jellyfish can choose one piece of green apple and divide it into two smaller pieces, with each piece having half the weight of the original piece. Jellyfish wants to know the minimum number of operations needed such that she can divide the green apple pieces such that the total weight of apple pieces received by each person is the same. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$). The description of the test cases follows. The first and only line of each test case contains two integers, $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^9$$$)xa0β€” the number of the green apple pieces initially and the number of the people. Output For each test case, output a single integerxa0β€” the minimum number of operations required to divide all the green apples equally among the $$$m$$$ people. If this cannot be achieved using a finite number of operations, output $$$-1$$$ instead. Note In the first test case, we do not need to divide the apple pieces. Each person will receive $$$2$$$ pieces of $$$1~ ext{kg}$$$ and the total weight of apple pieces received by each person is $$$2~ ext{kg}$$$. In the second test case, it is impossible to divide the apple equally using a finite number of operations. In the third test case, we can divide two of the apple pieces of weight $$$1~ ext{kg}$$$, getting $$$4$$$ apple pieces of weight $$$0.5~ ext{kg}$$$. Each person will receive $$$1$$$ apple piece of weight $$$0.5~ ext{kg}$$$ and $$$2$$$ apple pieces of weight $$$1~ ext{kg}$$$. The total weight of apple pieces received by each person is $$$2.5~ ext{kg}$$$. In the fourth test case, we first divide all $$$3$$$ of the apple pieces of weight $$$1~ ext{kg}$$$, getting $$$6$$$ pieces of weight $$$0.5~ ext{kg}$$$. Then, we further divide two of the apple pieces of weight $$$0.5~ ext{kg}$$$, getting $$$4$$$ pieces of weight $$$0.25~ ext{kg}$$$. Each person will receive $$$1$$$ apple piece of weight $$$0.5~ ext{kg}$$$ and $$$1$$$ apple piece of weight $$$0.25~ ext{kg}$$$. The total weight of apples received by each person is $$$0.75~ ext{kg}$$$.
1,400
true
true
false
false
false
false
false
false
false
false
1,028
633F
Alice and Bob have a tree (undirected acyclic connected graph). There are _a__i_ chocolates waiting to be picked up in the _i_-th vertex of the tree. First, they choose two different vertices as their starting positions (Alice chooses first) and take all the chocolates contained in them. Then, they alternate their moves, selecting one vertex at a time and collecting all chocolates from this node. To make things more interesting, they decided that one can select a vertex only if he/she selected a vertex adjacent to that one at his/her previous turn and this vertex has not been already chosen by any of them during other move. If at any moment one of them is not able to select the node that satisfy all the rules, he/she will skip his turns and let the other person pick chocolates as long as he/she can. This goes on until both of them cannot pick chocolates any further. Due to their greed for chocolates, they want to collect as many chocolates as possible. However, as they are friends they only care about the total number of chocolates they obtain together. What is the maximum total number of chocolates they may pick? Input The first line of the input contains the single integer _n_ (2u2009≀u2009nu2009≀u2009100u2009000)xa0β€” the number of vertices in the tree. The second line contains _n_ integers _a__i_ (1u2009≀u2009_a__i_u2009≀u2009109), _i_-th of these numbers stands for the number of chocolates stored at the node _i_. Then follow _n_u2009-u20091 lines that describe the tree. Each of them contains two integers _u__i_ and _v__i_ (1u2009≀u2009_u__i_,u2009_v__i_u2009≀u2009_n_)xa0β€” indices of vertices connected by the _i_-th edge. Output Print the number of chocolates Alice and Bob can collect together if they behave optimally. Examples Input 9 1 2 3 4 5 6 7 8 9 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 Note In the first sample, Alice may start at the vertex 9 and Bob at vertex 8. Alice will select vertex 1 and Bob has no options now. Alice selects the vertex 7 and they both stop. In the second sample, both of them will pick either of the nodes alternately.
2,600
false
false
false
true
false
false
false
false
false
true
7,270
448A
Problem - 448A - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags implementation *800 No tag edit access β†’ Contest materials . The second line contains integers _b_1, _b_2 and _b_3 (0u2009≀u2009_b_1,u2009_b_2,u2009_b_3u2009≀u2009100). The third line contains integer _n_ (1u2009≀u2009_n_u2009≀u2009100). The numbers in the lines are separated by single spaces. Output Print "YES" (without the quotes) if all the rewards can be put on the shelves in the described manner. Otherwise, print "NO" (without the quotes). Examples Input 1 1 1 1 1 1 4 Output YES Input 1 1 3 2 3 4 2 Output YES Input 1 0 0 1 0 0 1 Output NO
800
false
false
true
false
false
false
false
false
false
false
8,049
280E
Problem - 280E - 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 implementation math *3000 No tag edit access β†’ Contest materials . You've also got two integers _a_ and _b_ (_a_u2009≀u2009_b_;xa0_a_Β·(_n_u2009-u20091)u2009<u2009_q_). Your task is to transform sequence _x_1,u2009_x_2,u2009...,u2009_x__n_ into some sequence _y_1,u2009_y_2,u2009...,u2009_y__n_ (1u2009≀u2009_y__i_u2009≀u2009_q_;xa0_a_u2009≀u2009_y__i_u2009+u20091u2009-u2009_y__i_u2009≀u2009_b_). The transformation price is the following sum: . Your task is to choose such sequence _y_ that minimizes the described transformation price. Input The first line contains four integers _n_,u2009_q_,u2009_a_,u2009_b_ (2u2009≀u2009_n_u2009≀u20096000;xa01u2009≀u2009_q_,u2009_a_,u2009_b_u2009≀u2009109;xa0_a_Β·(_n_u2009-u20091)u2009<u2009_q_;xa0_a_u2009≀u2009_b_). The second line contains a non-decreasing integer sequence _x_1,u2009_x_2,u2009...,u2009_x__n_ (1u2009≀u2009_x_1u2009≀u2009_x_2u2009≀u2009...u2009≀u2009_x__n_u2009≀u2009_q_). Output In the first line print _n_ real numbers β€” the sought sequence _y_1,u2009_y_2,u2009...,u2009_y__n_ (1u2009≀u2009_y__i_u2009≀u2009_q_;xa0_a_u2009≀u2009_y__i_u2009+u20091u2009-u2009_y__i_u2009≀u2009_b_). In the second line print the minimum transformation price, that is, . If there are multiple optimal answers you can print any of them. The answer will be considered correct if the absolute or relative error doesn't exceed 10u2009-u20096. Examples Input 3 6 2 2 1 4 6 Output 1.666667 3.666667 5.666667 0.666667 Input 10 100000 8714 9344 3378 14705 17588 22672 32405 34309 37446 51327 81228 94982 Output 1.000000 8715.000000 17429.000000 26143.000000 34857.000000 43571.000000 52285.000000 61629.000000 70973.000000 80317.000000 797708674.000000
3,000
true
false
true
true
true
false
true
false
false
false
8,711
131E
A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop. There are _m_ queens on a square _n_u2009Γ—u2009_n_ chessboard. You know each queen's positions, the _i_-th queen is positioned in the square (_r__i_,u2009_c__i_), where _r__i_ is the board row number (numbered from the top to the bottom from 1 to _n_), and _c__i_ is the board's column number (numbered from the left to the right from 1 to _n_). No two queens share the same position. For each queen one can count _w_ β€” the number of other queens that the given queen threatens (attacks). For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack. Obviously, for any queen _w_ is between 0 and 8, inclusive. Print the sequence _t_0,u2009_t_1,u2009...,u2009_t_8, where _t__i_ is the number of queens that threaten exactly _i_ other queens, i.e. the number of queens that their _w_ equals _i_. Input The first line of the input contains a pair of integers _n_,u2009_m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009105), where _n_ is the size of the board and _m_ is the number of queens on the board. Then _m_ following lines contain positions of the queens, one per line. Each line contains a pair of integers _r__i_,u2009_c__i_ (1u2009≀u2009_r__i_,u2009_c__i_u2009≀u2009_n_) β€” the queen's position. No two queens stand on the same square. Output Print the required sequence _t_0,u2009_t_1,u2009...,u2009_t_8, separating the numbers with spaces.
1,700
false
false
false
false
false
false
false
false
true
false
9,349
85C
One night, having had a hard day at work, Petya saw a nightmare. There was a binary search tree in the dream. But it was not the actual tree that scared Petya. The horrifying thing was that Petya couldn't search for elements in this tree. Petya tried many times to choose key and look for it in the tree, and each time he arrived at a wrong place. Petya has been racking his brains for long, choosing keys many times, but the result was no better. But the moment before Petya would start to despair, he had an epiphany: every time he was looking for keys, the tree didn't have the key, and occured exactly one mistake. "That's not a problem!", thought Petya. "Why not count the expectation value of an element, which is found when I search for the key". The moment he was about to do just that, however, Petya suddenly woke up. Thus, you are given a binary search tree, that is a tree containing some number written in the node. This number is called the node key. The number of children of every node of the tree is equal either to 0 or to 2. The nodes that have 0 children are called leaves and the nodes that have 2 children, are called inner. An inner node has the left child, that is the child whose key is less than the current node's key, and the right child, whose key is more than the current node's key. Also, a key of any node is strictly larger than all the keys of the left subtree of the node and strictly smaller than all the keys of the right subtree of the node. Also you are given a set of search keys, all of which are distinct and differ from the node keys contained in the tree. For each key from the set its search in the tree is realised. The search is arranged like this: initially we are located in the tree root, if the key of the current node is larger that our search key, then we move to the left child of the node, otherwise we go to the right child of the node and the process is repeated. As it is guaranteed that the search key is not contained in the tree, the search will always finish in some leaf. The key lying in the leaf is declared the search result. It is known for sure that during the search we make a mistake in comparing exactly once, that is we go the wrong way, but we won't make any mistakes later. All possible mistakes are equiprobable, that is we should consider all such searches where exactly one mistake occurs. Your task is to find the expectation (the average value) of the search result for every search key, considering that exactly one mistake occurs in the search. That is, for a set of paths containing exactly one mistake in the given key search, you should count the average value of keys containing in the leaves of those paths. Input The first line contains an odd integer _n_ (3u2009≀u2009_n_u2009<u2009105), which represents the number of tree nodes. Next _n_ lines contain node descriptions. The (_i_u2009+u20091)-th line contains two space-separated integers. The first number is the number of parent of the _i_-st node and the second number is the key lying in the _i_-th node. The next line contains an integer _k_ (1u2009≀u2009_k_u2009≀u2009105), which represents the number of keys for which you should count the average value of search results containing one mistake. Next _k_ lines contain the actual keys, one key per line. All node keys and all search keys are positive integers, not exceeding 109. All _n_u2009+u2009_k_ keys are distinct. All nodes are numbered from 1 to _n_. For the tree root "-1" (without the quote) will be given instead of the parent's node number. It is guaranteed that the correct binary search tree is given. For each node except for the root, it could be determined according to its key whether it is the left child or the right one. Note In the first sample the search of key 1 with one error results in two paths in the trees: (1, 2, 5) and (1, 3, 6), in parentheses are listed numbers of nodes from the root to a leaf. The keys in the leaves of those paths are equal to 6 and 10 correspondingly, that's why the answer is equal to 8.
2,200
false
false
false
false
false
false
false
true
true
false
9,544
340E
Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work. The girl finds an important permutation for the research. The permutation contains _n_ distinct integers _a_1, _a_2, ..., _a__n_ (1u2009≀u2009_a__i_u2009≀u2009_n_). She replaces some of permutation elements with -1 value as a revenge. When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element _a__k_ which has value equal to _k_ (_a__k_u2009=u2009_k_). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109u2009+u20097). Input The first line contains integer _n_ (2u2009≀u2009_n_u2009≀u20092000). On the second line, there are _n_ integers, representing Iahub's important permutation after Iahubina replaces some values with -1. It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation. Output Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109u2009+u20097). Note For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point.
2,000
true
false
false
false
false
false
false
false
false
false
8,471
1978D
Elections are taking place in Berland. There are $$$n$$$ candidates participating in the elections, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th candidate has $$$a_i$$$ fans who will vote for him. Additionally, there are $$$c$$$ people who are undecided about their favorite candidate, let's call them undecided. Undecided people will vote for the candidate with the lowest number. The candidate who receives the maximum number of votes wins the elections, and if multiple candidates receive the same maximum number of votes, the candidate with the lowest number among them wins. You found these elections too boring and predictable, so you decided to exclude some candidates from them. If you do not allow candidate number $$$i$$$ to participate in the elections, all $$$a_i$$$ of his fans will become undecided, and will vote for the candidate with the lowest number. You are curious to find, for each $$$i$$$ from $$$1$$$ to $$$n$$$, the minimum number of candidates that need to be excluded from the elections for candidate number $$$i$$$ to win the elections. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le c le 10^9$$$) β€” the number of candidates in the elections and the number of undecided people. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$) β€” the number of fans for each candidate. 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 $$$n$$$ integers, the $$$i$$$-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number $$$i$$$ to win. Example Input 5 3 1 2 0 3 2 3 0 10 5 3 5 4 3 2 1 4 5 3 10 7 1 6 0 2 2 2 3 3 3 Output 0 1 2 1 0 0 1 2 3 4 1 0 2 3 1 1 2 0 4 5 Note In the first test case: If all candidates are allowed, candidate number $$$1$$$ will receive $$$3$$$ votes ($$$1$$$ undecided person will vote for him), candidate number $$$2$$$ will receive $$$0$$$ votes, and candidate number $$$3$$$ will receive $$$3$$$ votes. Therefore, candidate number $$$1$$$ wins (he received the same number of votes as candidate $$$3$$$, but his number is lower), so the answer for him is $$$0$$$. If candidate number $$$1$$$ is not allowed, his $$$2$$$ fans will become undecided. Then candidate number $$$2$$$ will receive $$$3$$$ votes ($$$3$$$ undecided people will vote for him) and candidate number $$$3$$$ will receive $$$3$$$ votes. Therefore, candidate number $$$2$$$ wins (he received the same number of votes as candidate $$$3$$$, but his number is lower), so the answer for him is $$$1$$$. If candidates with numbers $$$1$$$ and $$$2$$$ are not allowed, candidate number $$$3$$$ wins, so the answer for him is $$$2$$$. In the second test case, candidate number $$$1$$$ will win if candidate number $$$2$$$ is not allowed to participate.
1,600
true
true
true
false
true
false
false
false
false
false
419
1682B
You are given a permutation $$$p$$$ of integers from $$$0$$$ to $$$n-1$$$ (each of them occurs exactly once). Initially, the permutation is not sorted (that is, $$$p_i>p_{i+1}$$$ for at least one $$$1 le i le n - 1$$$). The permutation is called $$$X$$$-sortable for some non-negative integer $$$X$$$ if it is possible to sort the permutation by performing the operation below some finite number of times: Choose two indices $$$i$$$ and $$$j$$$ $$$(1 le i lt j le n)$$$ such that $$$p_i & p_j = X$$$. Swap $$$p_i$$$ and $$$p_j$$$. Here $$$&$$$ denotes the $$$ xa0β€” the number of test cases. Description of test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(2 le n le 2 cdot 10^5)$$$ xa0β€” the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, ..., p_n$$$ ($$$0 le p_i le n-1$$$, all $$$p_i$$$ are distinct) xa0β€” the elements of $$$p$$$. It is guaranteed that $$$p$$$ is not sorted. It is guaranteed that the sum of $$$n$$$ over all cases does not exceed $$$2 cdot 10^5$$$. Output For each test case output a single integer β€” the maximum value of $$$X$$$ such that $$$p$$$ is $$$X$$$-sortable. Example Input 4 4 0 1 3 2 2 1 0 7 0 1 2 3 5 6 4 5 0 3 2 1 4 Note In the first test case, the only $$$X$$$ for which the permutation is $$$X$$$-sortable are $$$X = 0$$$ and $$$X = 2$$$, maximum of which is $$$2$$$. Sorting using $$$X = 0$$$: Swap $$$p_1$$$ and $$$p_4$$$, $$$p = [2, 1, 3, 0]$$$. Swap $$$p_3$$$ and $$$p_4$$$, $$$p = [2, 1, 0, 3]$$$. Swap $$$p_1$$$ and $$$p_3$$$, $$$p = [0, 1, 2, 3]$$$. Sorting using $$$X = 2$$$: Swap $$$p_3$$$ and $$$p_4$$$, $$$p = [0, 1, 2, 3]$$$. In the second test case, we must swap $$$p_1$$$ and $$$p_2$$$ which is possible only with $$$X = 0$$$.
1,100
false
false
false
false
false
true
false
false
true
false
2,178
50D
The commanding officers decided to drop a nuclear bomb on the enemy's forces. You are ordered to determine the power of the warhead that needs to be used. The enemy has _N_ strategically important objects. Their positions are known due to the intelligence service. The aim of the strike is to deactivate at least _K_ important objects of the enemy. The bombing impact point is already determined and has coordinates of [_X_0; _Y_0]. The nuclear warhead is marked by the estimated impact radius _R_u2009β‰₯u20090. All the buildings that are located closer than _R_ to the bombing epicentre will be destroyed. All the buildings that are located further than _R_ from the epicentre, can also be deactivated with some degree of probability. Let's assume that _D_ is the distance between a building and the epicentre. This building's deactivation probability _P_(_D_,u2009_R_) is calculated according to the following formula: We should regard as _e__a_, where _e_u2009β‰ˆu20092.7182818284590452353602874713527 If the estimated impact radius of the warhead is equal to zero, then all the buildings located in the impact point will be completely demolished and all the rest of important objects will not be damaged. The commanding officers want the probability of failing the task to be no more than Ξ΅. Nuclear warheads are too expensive a luxury, that's why you have to minimise the estimated impact radius of the warhead. Input The first line contains an integer _N_ which represents the number of the enemy's objects (1u2009≀u2009_N_u2009≀u2009100). The second line contains two integers: _K_ is the required number of deactivated objects, and Ξ΅ is the maximally permitted probability of not completing the task, given in per mils (1u2009≀u2009_K_u2009≀u2009_N_, 1u2009≀u2009Ξ΅u2009≀u2009999). The third line contains _X_0 and _Y_0 which are the coordinates of the strike impact point. The next _N_ lines contain two numbers _X__i_ and _Y__i_ each which are the coordinates of every strategically important object. All the coordinates are integer, their absolute values do not exceed 1000. Let us remind you that there are a thousand per mils in unity (number one). There can be several objects in one point. Output Print the sought estimated impact radius of the warhead. The absolute or relative measure of the inaccuracy of your answer should not exceed 10u2009-u20096. Examples Output 3.84257761518762740 Input 5 3 100 0 0 3 4 60 70 100 100 10 10 5 12 Output 13.45126176453737600
2,100
false
false
false
true
false
false
false
true
false
false
9,721
1699E
Ibti was thinking about a good title for this problem that would fit the round theme (numerus ternarium). He immediately thought about the third derivative, but that was pretty lame so he decided to include the best band in the world β€” - min(a_i)$$$. Find the minimum possible balance after performing any number (possible zero) of operations. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0β€” the number of test cases. The second line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^6$$$, $$$1 le m le 5 cdot 10^6$$$)xa0β€” the initial size of the multiset, and the maximum value of an element. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le m$$$)xa0β€” the elements in the initial multiset. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^6$$$ and the sum of $$$m$$$ across all test cases does not exceed $$$5 cdot 10^6$$$. Note In the first test case, we can apply the operation on each of the $$$4$$$s with $$$(p,q) = (2,2)$$$ and make the multiset $$${2,2,2,2,2,2,2}$$$ with balance $$$max({2,2,2,2,2,2,2}) - min({2,2,2,2,2,2,2}) = 0$$$. It is obvious we cannot make this balance less than $$$0$$$. In the second test case, we can apply an operation on $$$12$$$ with $$$(p,q) = (3,4)$$$. After this our multiset will be $$${3,4,2,3}$$$. We can make one more operation on $$$4$$$ with $$$(p,q) = (2,2)$$$, making the multiset $$${3,2,2,2,3}$$$ with balance equal to $$$1$$$. In the third test case, we can apply an operation on $$$35$$$ with $$$(p,q) = (5,7)$$$. The final multiset is $$${6,5,7}$$$ and has a balance equal to $$$7-5 = 2$$$. In the forth test case, we cannot apply any operation, so the balance is $$$5 - 1 = 4$$$.
2,600
true
true
false
true
true
false
false
false
false
false
2,083
2018B
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? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the number of cities. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β€” the deadlines for conquering the cities. 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 integer: the number of starting cities that allow you to win. Example Input 3 6 6 3 3 3 5 5 6 5 6 4 1 4 5 9 8 6 4 2 1 3 5 7 9 Note In the first test case, cities $$$2$$$, $$$3$$$, and $$$4$$$ are good starting cities. In the second test case, there are no good starting cities. In the third test case, the only good starting city is city $$$5$$$.
1,900
false
true
true
true
true
false
false
true
false
false
162
847L
In the computer network of the Berland State University there are _n_ routers numbered from 1 to _n_. Some pairs of routers are connected by patch cords. Information can be transmitted over patch cords in both direction. The network is arranged in such a way that communication between any two routers (directly or through other routers) is possible. There are no cycles in the network, so there is only one path between each pair of routers over patch cords. Unfortunately, the exact topology of the network was lost by administrators. In order to restore it, the following auxiliary information was collected. For each patch cord _p_, directly connected to the router _i_, list of routers located behind the patch cord _p_ relatively _i_ is known. In other words, all routers path from which to the router _i_ goes through _p_ are known. So for each router _i_ there are _k__i_ lists, where _k__i_ is the number of patch cords connected to _i_. For example, let the network consists of three routers connected in chain 1u2009-u20092u2009-u20093. Then: the router 1: for the single patch cord connected to the first router there is a single list containing two routers: 2 and 3; the router 2: for each of the patch cords connected to the second router there is a list: one list contains the router 1 and the other β€” the router 3; the router 3: for the single patch cord connected to the third router there is a single list containing two routers: 1 and 2. Your task is to help administrators to restore the network topology, i. e. to identify all pairs of routers directly connected by a patch cord. Input The first line contains a single integer _n_ (2u2009≀u2009_n_u2009≀u20091000) β€” the number of routers in the network. The _i_-th of the following _n_ lines contains a description of the lists for the router _i_. The description of each list begins with the number of routers in it. Then the symbol ':' follows, and after that the numbers of routers from the list are given. This numbers are separated by comma. Lists are separated by symbol '-'. It is guaranteed, that for each router _i_ the total number of routers in its lists equals to _n_u2009-u20091 and all the numbers in lists of each router are distinct. For each router _i_ lists do not contain the number _i_. Output Print -1 if no solution exists. In the other case print to the first line _n_u2009-u20091 β€” the total number of patch cords in the network. In each of the following _n_u2009-u20091 lines print two integers β€” the routers which are directly connected by a patch cord. Information about each patch cord must be printed exactly once. Patch cords and routers can be printed in arbitrary order. Examples Input 5 4:2,5,3,4 1:4-1:1-2:5,3 4:4,5,2,1 4:2,1,3,5 1:3-3:4,2,1 Input 3 1:2-1:3 1:1-1:3 1:1-1:2 Note The first example is analyzed in the statement. The answer to the second example is shown on the picture. The first router has one list, which contains all other routers. The second router has three lists: the first β€” the single router 4, the second β€” the single router 1, the third β€” two routers 3 and 5. The third router has one list, which contains all other routers. The fourth router also has one list, which contains all other routers. The fifth router has two lists: the first β€” the single router 3, the second β€” three routers 1, 2 and 4.
2,400
false
false
false
false
false
true
false
false
false
true
6,360
551C
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way. In total there are _n_ piles of boxes, arranged in a line, from left to right, _i_-th pile (1u2009≀u2009_i_u2009≀u2009_n_) containing _a__i_ boxes. Luckily, _m_ students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are: 1. If _i_u2009β‰ u2009_n_, move from pile _i_ to pile _i_u2009+u20091; 2. If pile located at the position of student is not empty, remove one box from it. GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time _t_ in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after _t_ seconds, but all the boxes must be removed. Input The first line contains two integers _n_ and _m_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009105), the number of piles of boxes and the number of GukiZ's students. The second line contains _n_ integers _a_1,u2009_a_2,u2009... _a__n_ (0u2009≀u2009_a__i_u2009≀u2009109) where _a__i_ represents the number of boxes on _i_-th pile. It's guaranteed that at least one pile of is non-empty. Output In a single line, print one number, minimum time needed to remove all the boxes in seconds. Note First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second), then move to the second pile (1 second) and finally remove the box from second pile (1 second). Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds. Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile. Process will be over in 5 seconds, when removing the boxes from the last pile is finished.
2,200
false
true
false
false
false
false
false
true
false
false
7,630
1307E
After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass! On the field, there is a row of $$$n$$$ units of grass, each with a sweetness $$$s_i$$$. Farmer John has $$$m$$$ cows, each with a favorite sweetness $$$f_i$$$ and a hunger value $$$h_i$$$. He would like to pick two disjoint subsets of cows to line up on the left and right side of the grass row. There is no restriction on how many cows must be on either side. The cows will be treated in the following manner: The cows from the left and right side will take turns feeding in an order decided by Farmer John. When a cow feeds, it walks towards the other end without changing direction and eats grass of its favorite sweetness until it eats $$$h_i$$$ units. The moment a cow eats $$$h_i$$$ units, it will fall asleep there, preventing further cows from passing it from both directions. If it encounters another sleeping cow or reaches the end of the grass row, it will get upset. Farmer John absolutely does not want any cows to get upset. Note that grass does not grow back. Also, to prevent cows from getting upset, not every cow has to feed since FJ can choose a subset of them. Surprisingly, FJ has determined that sleeping cows are the most satisfied. If FJ orders optimally, what is the maximum number of sleeping cows that can result, and how many ways can FJ choose the subset of cows on the left and right side to achieve that maximum number of sleeping cows (modulo $$$10^9+7$$$)? The order in which FJ sends the cows does not matter as long as no cows get upset. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 5000$$$, $$$1 le m le 5000$$$) xa0β€” the number of units of grass and the number of cows. The second line contains $$$n$$$ integers $$$s_1, s_2, ldots, s_n$$$ ($$$1 le s_i le n$$$) xa0β€” the sweetness values of the grass. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$f_i$$$ and $$$h_i$$$ ($$$1 le f_i, h_i le n$$$) xa0β€” the favorite sweetness and hunger value of the $$$i$$$-th cow. No two cows have the same hunger and favorite sweetness simultaneously. Output Output two integers xa0β€” the maximum number of sleeping cows that can result and the number of ways modulo $$$10^9+7$$$. Examples Input 5 2 1 1 1 1 1 1 2 1 3 Input 5 2 1 1 1 1 1 1 2 1 4 Note In the first example, FJ can line up the cows as follows to achieve $$$2$$$ sleeping cows: Cow $$$1$$$ is lined up on the left side and cow $$$2$$$ is lined up on the right side. Cow $$$2$$$ is lined up on the left side and cow $$$1$$$ is lined up on the right side. In the second example, FJ can line up the cows as follows to achieve $$$1$$$ sleeping cow: Cow $$$1$$$ is lined up on the left side. Cow $$$2$$$ is lined up on the left side. Cow $$$1$$$ is lined up on the right side. Cow $$$2$$$ is lined up on the right side. In the third example, FJ can line up the cows as follows to achieve $$$2$$$ sleeping cows: Cow $$$1$$$ and $$$2$$$ are lined up on the left side. Cow $$$1$$$ and $$$2$$$ are lined up on the right side. Cow $$$1$$$ is lined up on the left side and cow $$$2$$$ is lined up on the right side. Cow $$$1$$$ is lined up on the right side and cow $$$2$$$ is lined up on the left side. In the fourth example, FJ cannot end up with any sleeping cows, so there will be no cows lined up on either side.
2,500
true
true
true
true
false
false
false
true
false
false
4,152
946D
Ivan is a student at Berland State University (BSU). There are _n_ days in Berland week, and each of these days Ivan might have some classes at the university. There are _m_ working hours during each Berland day, and each lesson at the university lasts exactly one hour. If at some day Ivan's first lesson is during _i_-th hour, and last lesson is during _j_-th hour, then he spends _j_u2009-u2009_i_u2009+u20091 hours in the university during this day. If there are no lessons during some day, then Ivan stays at home and therefore spends 0 hours in the university. Ivan doesn't like to spend a lot of time in the university, so he has decided to skip some lessons. He cannot skip more than _k_ lessons during the week. After deciding which lessons he should skip and which he should attend, every day Ivan will enter the university right before the start of the first lesson he does not skip, and leave it after the end of the last lesson he decides to attend. If Ivan skips all lessons during some day, he doesn't go to the university that day at all. Given _n_, _m_, _k_ and Ivan's timetable, can you determine the minimum number of hours he has to spend in the university during one week, if he cannot skip more than _k_ lessons? Input The first line contains three integers _n_, _m_ and _k_ (1u2009≀u2009_n_,u2009_m_u2009≀u2009500, 0u2009≀u2009_k_u2009≀u2009500) β€” the number of days in the Berland week, the number of working hours during each day, and the number of lessons Ivan can skip, respectively. Then _n_ lines follow, _i_-th line containing a binary string of _m_ characters. If _j_-th character in _i_-th line is 1, then Ivan has a lesson on _i_-th day during _j_-th hour (if it is 0, there is no such lesson). Output Print the minimum number of hours Ivan has to spend in the university during the week if he skips not more than _k_ lessons. Note In the first example Ivan can skip any of two lessons during the first day, so he spends 1 hour during the first day and 4 hours during the second day. In the second example Ivan can't skip any lessons, so he spends 4 hours every day.
1,800
false
false
false
true
false
false
false
false
false
false
5,929
1886E
Monocarp is a team leader in a massive IT company. There are $$$m$$$ projects his team of programmers has to complete, numbered from $$$1$$$ to $$$m$$$. The $$$i$$$-th project has a difficulty level $$$b_i$$$. There are $$$n$$$ programmers in the team, numbered from $$$1$$$ to $$$n$$$. The $$$j$$$-th programmer has a stress tolerance level $$$a_j$$$. Monocarp wants to assign the programmers to the projects in such a way that: each programmer is assigned to no more than one project; each project has at least one programmer assigned to it; let $$$k$$$ programmers be assigned to the $$$i$$$-th project; then all the assigned programmers have to have a stress tolerance level greater than or equal to $$$frac{b_i}{k}$$$. Help Monocarp to find a valid assignment. If there are multiple answers, print any of them. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le m le 20$$$)xa0β€” the number of programmers and the number of projects. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” the stress tolerance level of each programmer. The third line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_i le 10^9$$$)xa0β€” the difficulty level of each project. Output If there is no valid assignment, print "NO". Otherwise, in the first line, print "YES". In the $$$i$$$-th of the next $$$m$$$ lines, print the list of the programmers assigned to the $$$i$$$-th project: first, the number of programmers, then their indices in an arbitrary order. If there are multiple answers, print any of them. Examples Input 5 3 4 6 100 5 1 50 1 12 Output YES 1 3 1 5 3 2 4 1 Input 5 3 3 6 100 5 1 50 1 12 Output YES 1 5 3 1 2 3 1 4 Input 5 1 10 20 30 40 50 4
2,400
true
true
false
true
false
true
false
false
true
false
973
1985G
Problem - 1985G - 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 math number theory *1600 No tag edit access β†’ Contest materials ") Editorial") $$$ represent the sum of digits of $$$n$$$. For how many integers $$$n$$$ where $$$10^{l} leq n < 10^{r}$$$ satisfy $$$D(k cdot n) = k cdot D(n)$$$? Output the answer modulo $$$10^9+7$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0– the number of test cases. Each test case contains three integers $$$l$$$, $$$r$$$, and $$$k$$$ ($$$0 leq l < r leq 10^9$$$, $$$1 leq k leq 10^9$$$). Output For each test case, output an integer, the answer, modulo $$$10^9 + 7$$$. Example Input 6 0 1 4 0 2 7 1 2 1 1 2 3 582 74663 3 0 3 1 Output 2 3 90 12 974995667 999 Note For the first test case, the only values of $$$n$$$ that satisfy the condition are $$$1$$$ and $$$2$$$. For the second test case, the only values of $$$n$$$ that satisfy the condition are $$$1$$$, $$$10$$$, and $$$11$$$. For the third test case, all values of $$$n$$$ between $$$10$$$ inclusive and $$$100$$$ exclusive satisfy the condition.
1,600
true
false
false
false
false
false
false
false
false
false
368
982D
For long time scientists study the behavior of sharks. Sharks, as many other species, alternate short movements in a certain location and long movements between locations. Max is a young biologist. For $$$n$$$ days he watched a specific shark, and now he knows the distance the shark traveled in each of the days. All the distances are distinct. Max wants to know now how many locations the shark visited. He assumed there is such an integer $$$k$$$ that if the shark in some day traveled the distance strictly less than $$$k$$$, then it didn't change the location; otherwise, if in one day the shark traveled the distance greater than or equal to $$$k$$$; then it was changing a location in that day. Note that it is possible that the shark changed a location for several consecutive days, in each of them the shark traveled the distance at least $$$k$$$. The shark never returned to the same location after it has moved from it. Thus, in the sequence of $$$n$$$ days we can find consecutive nonempty segments when the shark traveled the distance less than $$$k$$$ in each of the days: each such segment corresponds to one location. Max wants to choose such $$$k$$$ that the lengths of all such segments are equal. Find such integer $$$k$$$, that the number of locations is as large as possible. If there are several such $$$k$$$, print the smallest one. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) β€” the number of days. The second line contains $$$n$$$ distinct positive integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) β€” the distance traveled in each of the day. Output Print a single integer $$$k$$$, such that 1. the shark was in each location the same number of days, 2. the number of locations is maximum possible satisfying the first condition, 3. $$$k$$$ is smallest possible satisfying the first and second conditions. Note In the first example the shark travels inside a location on days $$$1$$$ and $$$2$$$ (first location), then on $$$4$$$-th and $$$5$$$-th days (second location), then on $$$7$$$-th and $$$8$$$-th days (third location). There are three locations in total. In the second example the shark only moves inside a location on the $$$2$$$-nd day, so there is only one location.
1,900
false
false
false
false
true
false
true
false
false
false
5,789
793D
Bankopolis is an incredible city in which all the _n_ crossroads are located on a straight line and numbered from 1 to _n_ along it. On each crossroad there is a bank office. The crossroads are connected with _m_ oriented bicycle lanes (the _i_-th lane goes from crossroad _u__i_ to crossroad _v__i_), the difficulty of each of the lanes is known. Oleg the bank client wants to gift happiness and joy to the bank employees. He wants to visit exactly _k_ offices, in each of them he wants to gift presents to the employees. The problem is that Oleg don't want to see the reaction on his gifts, so he can't use a bicycle lane which passes near the office in which he has already presented his gifts (formally, the _i_-th lane passes near the office on the _x_-th crossroad if and only if _min_(_u__i_,u2009_v__i_)u2009<u2009_x_u2009<u2009_max_(_u__i_,u2009_v__i_))). Of course, in each of the offices Oleg can present gifts exactly once. Oleg is going to use exactly _k_u2009-u20091 bicycle lane to move between offices. Oleg can start his path from any office and finish it in any office. Oleg wants to choose such a path among possible ones that the total difficulty of the lanes he will use is minimum possible. Find this minimum possible total difficulty. Input The first line contains two integers _n_ and _k_ (1u2009≀u2009_n_,u2009_k_u2009≀u200980)xa0β€” the number of crossroads (and offices) and the number of offices Oleg wants to visit. The second line contains single integer _m_ (0u2009≀u2009_m_u2009≀u20092000)xa0β€” the number of bicycle lanes in Bankopolis. The next _m_ lines contain information about the lanes. The _i_-th of these lines contains three integers _u__i_, _v__i_ and _c__i_ (1u2009≀u2009_u__i_,u2009_v__i_u2009≀u2009_n_, 1u2009≀u2009_c__i_u2009≀u20091000), denoting the crossroads connected by the _i_-th road and its difficulty. Output In the only line print the minimum possible total difficulty of the lanes in a valid path, or -1 if there are no valid paths. Examples Input 7 4 4 1 6 2 6 2 2 2 4 2 2 7 1 Input 4 3 4 2 1 2 1 3 2 3 4 2 4 1 1 Note In the first example Oleg visiting banks by path 1u2009β†’u20096u2009β†’u20092u2009β†’u20094. Path 1u2009β†’u20096u2009β†’u20092u2009β†’u20097 with smaller difficulity is incorrect because crossroad 2u2009β†’u20097 passes near already visited office on the crossroad 6. In the second example Oleg can visit banks by path 4u2009β†’u20091u2009β†’u20093.
2,100
false
false
false
true
false
false
false
false
false
true
6,604
755G
Problem - 755G - Codeforces =============== xa0 , denoting the number of Balls and the maximim number of groups, respectively. Output You should output a sequence of _k_ values. The _i_-th of them should represent the sought number of divisions into exactly _i_ groups, according to PolandBall's rules. Examples Input 3 3 Output 5 5 1 Input 1 1 Output 1 Input 5 10 Output 9 25 25 9 1 0 0 0 0 0 Note In the first sample case we can divide Balls into groups as follows: {1}, {2}, {3}, {12}, {23}. {12}{3}, {1}{23}, {1}{2}, {1}{3}, {2}{3}. {1}{2}{3}. Therefore, output is: 5 5 1.
3,200
true
false
false
true
false
false
false
false
false
false
6,771
1165C
Problem - 1165C - 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 *1300 No tag edit access β†’ Contest materials ") Editorial") a string good if its length is even, and every character in odd position of this string is different from the next character (the first character is different from the second, the third is different from the fourth, and so on). For example, the strings good, string and xyyx are good strings, and the strings bad, aa and aabc are not good. Note that the empty string is considered good. You are given a string $$$s$$$, you have to delete minimum number of characters from this string so that it becomes good. Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the number of characters in $$$s$$$. The second line contains the string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters. Output In the first line, print one integer $$$k$$$ ($$$0 le k le n$$$) β€” the minimum number of characters you have to delete from $$$s$$$ to make it good. In the second line, print the resulting string $$$s$$$. If it is empty, you may leave the second line blank, or not print it at all. Examples Input 4 good Output 0 good Input 4 aabc Output 2 ab Input 3 aaa Output 3
1,300
false
true
false
false
false
false
false
false
false
false
4,894
1252B
The new ICPC town has $$$N$$$ junctions (numbered from $$$1$$$ to $$$N$$$) which are connected by $$$N-1$$$ roads. It is possible from one junction to go to any other junctions by going through one or more roads. To make sure all the junctions are well-maintained, the government environment agency is planning to deploy their newest advanced cleaning robots. In addition to its cleaning ability, each robot is also equipped with a movement ability such that it can move from one junction to any other junctions connected by roads. However, as you might have guessed, such robots are not cheap. Therefore, the agency is considering the following deployment plan. Let $$$T_k$$$ be the set of junctions which should be cleaned by the $$$k^{th}$$$ robot (also known as, the robot's task), and $$$T_k ge 1$$$ be the number of junctions in $$$T_k$$$. The junctions in $$$T_k$$$ form a path, i.e. there exists a sequence of $$$v_1, v_2, dots, v_{T_k}$$$ where $$$v_i in T_k$$$ and $$$v_i eq v_j$$$ for all $$$i eq j$$$ such that each adjacent junction in this sequence is connected by a road. The union of $$$T$$$ for all robots is equal to the set of all junctions in ICPC town. On the other hand, no two robots share a common junction, i.e. $$$T_i cap T_j = emptyset$$$ if $$$i eq j$$$. To avoid complaints from citizens for an inefficient operation, the deployment plan should be irreducible; in other words, there should be no two robots, $$$i$$$ and $$$j$$$, such that $$$T_i cup T_j$$$ forms a (longer) path. Note that the agency does not care whether the number of robots being used is minimized as long as all the tasks are irreducible. Your task in this problem is to count the number of feasible deployment plan given the town's layout. A plan is feasible if and only if it satisfies all the above-mentioned requirements. For example, let $$$N = 6$$$ and the roads are $$${(1,3),(2,3),(3,4),(4,5),(4,6)}$$$. There are $$$5$$$ feasible deployment plans as shown in the following figure. The first plan uses $$$2$$$ robots (labeled as A and B in the figure) to clean $$${1,2,3}$$$ and $$${4,5,6}$$$. The second plan uses $$$3$$$ robots (labeled as A, B, and C in the figure) to clean $$${1,3,4,6}$$$, $$${2}$$$, and $$${5}$$$. The third plan uses $$$3$$$ robots to clean $$${1,3,4,5}$$$, $$${2}$$$, and $$${6}$$$. The fourth plan uses $$$3$$$ robots to clean $$${1}$$$, $$${2,3,4,6}$$$, and $$${5}$$$. The fifth plan uses $$$3$$$ robots to clean $$${1}$$$, $$${2,3,4,5}$$$, and $$${6}$$$. No other plans are feasible in this case. For example, the plan $$${{1,3},{2},{4,5,6}}$$$ is not feasible as the task $$${1,3}$$$ and $$${2}$$$ can be combined into a longer path $$${1,3,2}$$$. The plan $$${{1,2,3,4},{5},{6}}$$$ is also not feasible as $$${1,2,3,4}$$$ is not a path.
2,300
false
false
false
true
false
false
false
false
false
false
4,433
1914D
Winter holidays are coming up. They are going to last for $$$n$$$ days. During the holidays, Monocarp wants to try all of these activities exactly once with his friends: go skiing; watch a movie in a cinema; play board games. Monocarp knows that, on the $$$i$$$-th day, exactly $$$a_i$$$ friends will join him for skiing, $$$b_i$$$ friends will join him for a movie and $$$c_i$$$ friends will join him for board games. Monocarp also knows that he can't try more than one activity in a single day. Thus, he asks you to help him choose three distinct days $$$x, y, z$$$ in such a way that the total number of friends to join him for the activities ($$$a_x + b_y + c_z$$$) is maximized. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$3 le n le 10^5$$$)xa0β€” the duration of the winter holidays in days. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^8$$$)xa0β€” the number of friends that will join Monocarp for skiing on the $$$i$$$-th day. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^8$$$)xa0β€” the number of friends that will join Monocarp for a movie on the $$$i$$$-th day. The fourth line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^8$$$)xa0β€” the number of friends that will join Monocarp for board games on the $$$i$$$-th day. The sum of $$$n$$$ over all testcases doesn't exceed $$$10^5$$$. Output For each testcase, print a single integerxa0β€” the maximum total number of friends that can join Monocarp for the activities on three distinct days. Example Input 4 3 1 10 1 10 1 1 1 1 10 4 30 20 10 1 30 5 15 20 30 25 10 10 10 5 19 12 3 18 18 6 17 10 13 15 17 19 11 16 3 11 17 17 17 1 17 18 10 15 8 17 3 13 12 10 17 5 4 18 12 4 11 2 16 16 8 4 14 19 3 12 6 7 5 16 3 4 8 11 10 8 10 2 20 3 Note In the first testcase, Monocarp can choose day $$$2$$$ for skiing, day $$$1$$$ for a movie and day $$$3$$$ for board games. This way, $$$a_2 = 10$$$ friends will join him for skiing, $$$b_1 = 10$$$ friends will join him for a movie and $$$c_3 = 10$$$ friends will join him for board games. The total number of friends is $$$30$$$. In the second testcase, Monocarp can choose day $$$1$$$ for skiing, day $$$4$$$ for a movie and day $$$2$$$ for board games. $$$30 + 20 + 25 = 75$$$ friends in total. Note that Monocarp can't choose day $$$1$$$ for all activities, because he can't try more than one activity in a single day. In the third testcase, Monocarp can choose day $$$2$$$ for skiing, day $$$3$$$ for a movie and day $$$7$$$ for board games. $$$19 + 19 + 17 = 55$$$ friends in total. In the fourth testcase, Monocarp can choose day $$$1$$$ for skiing, day $$$4$$$ for a movie and day $$$9$$$ for board games. $$$17 + 19 + 20 = 56$$$ friends in total.
1,200
false
true
true
true
false
false
true
false
true
false
816
850C
Problem - 850C - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags bitmasks dp games *2200 No tag edit access β†’ Contest materials ") such that _p__k_ divides at least one number in the list. For each number in the list divisible by _p__k_, call it _x_, the player will delete _x_ and add to the list. The player who can not make a valid choice of _p_ and _k_ loses. Mojtaba starts the game and the players alternatively make moves. Determine which one of players will be the winner if both players play optimally. Input The first line contains a single integer _n_ (1u2009≀u2009_n_u2009≀u2009100)xa0β€” the number of elements in the list. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≀u2009_a__i_u2009≀u2009109)xa0β€” the elements of the list. Output If Mojtaba wins, print "Mojtaba", otherwise print "Arpa" (without quotes). You can print each letter in any case (upper or lower). Examples Input 4 1 1 1 1 Output Arpa Input 4 1 1 17 17 Output Mojtaba Input 4 1 1 17 289 Output Arpa Input 5 1 2 3 4 5 Output Arpa Note In the first sample test, Mojtaba can't move. In the second sample test, Mojtaba chooses _p_u2009=u200917 and _k_u2009=u20091, then the list changes to
2,200
false
false
false
true
false
false
false
false
false
false
6,349
1499F
You are given an integer $$$k$$$ and an undirected tree, consisting of $$$n$$$ vertices. The length of a simple path (a path in which each vertex appears at most once) between some pair of vertices is the number of edges in this path. A diameter of a tree is the maximum length of a simple path between all pairs of vertices of this tree. You are about to remove a set of edges from the tree. The tree splits into multiple smaller trees when the edges are removed. The set of edges is valid if all the resulting trees have diameter less than or equal to $$$k$$$. Two sets of edges are different if there is an edge such that it appears in only one of the sets. Count the number of valid sets of edges modulo $$$998,244,353$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 5000$$$, $$$0 le k le n - 1$$$)xa0β€” the number of vertices of the tree and the maximum allowed diameter, respectively. Each of the next $$$n-1$$$ lines contains a description of an edge: two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$, $$$v eq u$$$). The given edges form a tree. Output Print a single integerxa0β€” the number of valid sets of edges modulo $$$998,244,353$$$. Examples Input 6 2 1 6 2 4 2 6 3 6 5 6 Input 6 3 1 2 1 5 2 3 3 4 5 6 Note In the first example the diameter of the given tree is already less than or equal to $$$k$$$. Thus, you can choose any set of edges to remove and the resulting trees will have diameter less than or equal to $$$k$$$. There are $$$2^3$$$ sets, including the empty one. In the second example you have to remove the only edge. Otherwise, the diameter will be $$$1$$$, which is greater than $$$0$$$. Here are the trees for the third and the fourth examples:
2,400
false
false
false
true
false
false
false
false
false
false
3,170
1854A2
The only difference between the two versions of this problem is the constraint on the maximum number of operations. 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$$$ of integers (positive, negative or $$$0$$$). You can perform multiple operations on the array (possibly $$$0$$$ operations). In one operation, you choose $$$i, j$$$ ($$$1 leq i, j leq n$$$, they can be equal) and set $$$a_i := a_i + a_j$$$ (i.e., add $$$a_j$$$ to $$$a_i$$$). Make the array non-decreasing (i.e., $$$a_i leq a_{i+1}$$$ for $$$1 leq i leq n-1$$$) in at most $$$31$$$ operations. You do not need to minimize the number of operations. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line contains a single integer $$$n$$$ ($$$1 le n le 20$$$) β€” the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-20 le a_i le 20$$$) β€” the array before performing the operations. Output For each test case, output your operations in the following format. The first line should contain an integer $$$k$$$ ($$$0 le k le 31$$$) β€” the number of operations. The next $$$k$$$ lines represent the $$$k$$$ operations in order. Each of these $$$k$$$ lines should contain two integers $$$i$$$ and $$$j$$$ ($$$1 leq i, j leq n$$$) β€” the corresponding operation consists in adding $$$a_j$$$ to $$$a_i$$$. After all the operations, the array $$$a_1, a_2,dots, a_n$$$ must be non-decreasing. Example Input 10 2 2 1 4 1 2 -10 3 5 2 1 1 1 1 8 0 0 0 0 0 0 0 0 5 1 2 -4 3 -10 10 11 12 13 14 15 -15 -16 -17 -18 -19 7 1 9 3 -4 -3 -2 -1 3 10 9 8 20 1 -14 2 -10 6 -5 10 -13 10 7 -14 19 -5 19 1 18 -16 -7 12 8 20 -15 -17 -13 8 14 -13 10 -4 11 -4 -16 -6 15 -4 -2 7 -9 5 -5 17 Output 1 2 1 3 4 4 4 4 3 4 4 2 1 3 1 4 1 5 1 0 7 3 4 3 4 5 4 5 4 5 4 5 4 5 4 15 6 1 6 1 6 1 7 2 7 2 7 2 8 3 8 3 8 3 9 4 9 4 9 4 10 5 10 5 10 5 8 3 4 3 4 2 4 2 4 2 4 2 4 1 4 1 4 3 2 1 3 1 3 1 31 14 1 18 7 13 11 15 11 6 4 5 17 19 6 19 12 10 5 11 12 1 17 15 19 16 10 14 2 16 11 20 7 7 6 9 5 3 6 6 14 17 18 18 14 12 3 17 16 8 18 13 16 9 8 14 8 16 2 11 8 12 7 31 5 12 19 13 9 1 5 17 18 19 6 16 15 8 6 9 15 14 7 10 19 7 17 20 14 4 15 20 4 3 1 8 16 12 16 15 5 6 12 10 11 15 20 3 20 19 13 14 11 14 18 10 7 3 12 17 4 7 13 2 11 13 Note In the first test case, by adding $$$a_1 = 2$$$ to $$$a_2$$$, we get the array $$$[2, 3]$$$ which is non-decreasing. In the second test case, the array changes as: $$$[1, 2, -10, 3]$$$ $$$[1, 2, -10, 6]$$$ $$$[1, 2, -10, 12]$$$ $$$[1, 2, 2, 12]$$$ In the third test case, the final array is $$$[2, 3, 3, 3, 3]$$$.
1,900
true
false
false
false
false
true
false
false
false
false
1,158
1511E
You have a large rectangular board which is divided into $$$n imes m$$$ cells (the board has $$$n$$$ rows and $$$m$$$ columns). Each cell is either white or black. You paint each white cell either red or blue. Obviously, the number of different ways to paint them is $$$2^w$$$, where $$$w$$$ is the number of white cells. After painting the white cells of the board, you want to place the maximum number of dominoes on it, according to the following rules: each domino covers two adjacent cells; each cell is covered by at most one domino; if a domino is placed horizontally (it covers two adjacent cells in one of the rows), it should cover only red cells; if a domino is placed vertically (it covers two adjacent cells in one of the columns), it should cover only blue cells. Let the value of the board be the maximum number of dominoes you can place. Calculate the sum of values of the board over all $$$2^w$$$ possible ways to paint it. Since it can be huge, print it modulo $$$998,244,353$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 3 cdot 10^5$$$; $$$nm le 3 cdot 10^5$$$) β€” the number of rows and columns, respectively. Then $$$n$$$ lines follow, each line contains a string of $$$m$$$ characters. The $$$j$$$-th character in the $$$i$$$-th string is if the $$$j$$$-th cell in the $$$i$$$-th row is black; otherwise, that character is o. Output Print one integer β€” the sum of values of the board over all $$$2^w$$$ possible ways to paint it, taken modulo $$$998,244,353$$$.
2,100
true
true
false
true
false
false
false
false
false
false
3,112
1991C
You are given an array $$$a$$$ of $$$n$$$ integers. In one operation, you will perform the following two-step move: 1. Choose an integer $$$x$$$ ($$$0 le x le 10^{9}$$$). 2. Replace each $$$a_i$$$ with $$$a_i - x$$$, where $$$v$$$ denotes the xa0β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0β€” the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer $$$-1$$$ if it is impossible to make all array elements equal to $$$0$$$ in at most $$$40$$$ operations. Otherwise, output two lines. The first line of output should contain a single integer $$$k$$$ ($$$0 le k le 40$$$)xa0β€” the number of operations. The second line of output should contain $$$k$$$ integers $$$x_1, x_2, ldots, x_k$$$ ($$$0 le x_i le 10^{9}$$$)xa0β€” the sequence of operations, denoting that on the $$$i$$$-th operation, you chose $$$x=x_i$$$. If there are multiple solutions, output any of them. You do not need to minimize the number of operations. Example Input 5 1 5 2 0 0 3 4 6 8 4 80 40 20 10 5 1 2 3 4 5 Output 1 5 0 3 6 1 1 7 60 40 20 10 30 25 5 -1 Note In the first test case, we can perform only one operation by choosing $$$x = 5$$$, changing the array from $$$[5]$$$ to $$$[0]$$$. In the second test case, no operations are needed because all elements of the array are already $$$0$$$. In the third test case, we can choose $$$x = 6$$$ to change the array from $$$[4, 6, 8]$$$ to $$$[2, 0, 2]$$$, then choose $$$x = 1$$$ to change it to $$$[1, 1, 1]$$$, and finally choose $$$x = 1$$$ again to change the array into $$$[0, 0, 0]$$$. In the fourth test case, we can make all elements $$$0$$$ by following the operation sequence $$$(60, 40, 20, 10, 30, 25, 5)$$$. In the fifth test case, it can be shown that it is impossible to make all elements $$$0$$$ in at most $$$40$$$ operations. Therefore, the output is $$$-1$$$.
1,300
true
true
false
false
false
true
false
false
false
false
326
1142B
Recently Lynyrd and Skynyrd went to a shop where Lynyrd bought a permutation $$$p$$$ of length $$$n$$$, and Skynyrd bought an array $$$a$$$ of length $$$m$$$, consisting of integers from $$$1$$$ to $$$n$$$. Lynyrd and Skynyrd became bored, so they asked you $$$q$$$ queries, each of which has the following form: "does the subsegment of $$$a$$$ from the $$$l$$$-th to the $$$r$$$-th positions, inclusive, have a subsequence that is a cyclic shift of $$$p$$$?" Please answer the queries. A permutation of length $$$n$$$ is a sequence of $$$n$$$ integers such that each integer from $$$1$$$ to $$$n$$$ appears exactly once in it. A cyclic shift of a permutation $$$(p_1, p_2, ldots, p_n)$$$ is a permutation $$$(p_i, p_{i + 1}, ldots, p_{n}, p_1, p_2, ldots, p_{i - 1})$$$ for some $$$i$$$ from $$$1$$$ to $$$n$$$. For example, a permutation $$$(2, 1, 3)$$$ has three distinct cyclic shifts: $$$(2, 1, 3)$$$, $$$(1, 3, 2)$$$, $$$(3, 2, 1)$$$. A subsequence of a subsegment of array $$$a$$$ from the $$$l$$$-th to the $$$r$$$-th positions, inclusive, is a sequence $$$a_{i_1}, a_{i_2}, ldots, a_{i_k}$$$ for some $$$i_1, i_2, ldots, i_k$$$ such that $$$l leq i_1 < i_2 < ldots < i_k leq r$$$. Input The first line contains three integers $$$n$$$, $$$m$$$, $$$q$$$ ($$$1 le n, m, q le 2 cdot 10^5$$$)xa0β€” the length of the permutation $$$p$$$, the length of the array $$$a$$$ and the number of queries. The next line contains $$$n$$$ integers from $$$1$$$ to $$$n$$$, where the $$$i$$$-th of them is the $$$i$$$-th element of the permutation. Each integer from $$$1$$$ to $$$n$$$ appears exactly once. The next line contains $$$m$$$ integers from $$$1$$$ to $$$n$$$, the $$$i$$$-th of them is the $$$i$$$-th element of the array $$$a$$$. The next $$$q$$$ lines describe queries. The $$$i$$$-th of these lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le m$$$), meaning that the $$$i$$$-th query is about the subsegment of the array from the $$$l_i$$$-th to the $$$r_i$$$-th positions, inclusive. Output Print a single string of length $$$q$$$, consisting of $$$0$$$ and $$$1$$$, the digit on the $$$i$$$-th positions should be $$$1$$$, if the subsegment of array $$$a$$$ from the $$$l_i$$$-th to the $$$r_i$$$-th positions, inclusive, contains a subsequence that is a cyclic shift of $$$p$$$, and $$$0$$$ otherwise. Examples Input 3 6 3 2 1 3 1 2 3 1 2 3 1 5 2 6 3 5 Input 2 4 3 2 1 1 1 2 2 1 2 2 3 3 4 Note In the first example the segment from the $$$1$$$-st to the $$$5$$$-th positions is $$$1, 2, 3, 1, 2$$$. There is a subsequence $$$1, 3, 2$$$ that is a cyclic shift of the permutation. The subsegment from the $$$2$$$-nd to the $$$6$$$-th positions also contains a subsequence $$$2, 1, 3$$$ that is equal to the permutation. The subsegment from the $$$3$$$-rd to the $$$5$$$-th positions is $$$3, 1, 2$$$, there is only one subsequence of length $$$3$$$ ($$$3, 1, 2$$$), but it is not a cyclic shift of the permutation. In the second example the possible cyclic shifts are $$$1, 2$$$ and $$$2, 1$$$. The subsegment from the $$$1$$$-st to the $$$2$$$-nd positions is $$$1, 1$$$, its subsequences are not cyclic shifts of the permutation. The subsegment from the $$$2$$$-nd to the $$$3$$$-rd positions is $$$1, 2$$$, it coincides with the permutation. The subsegment from the $$$3$$$ to the $$$4$$$ positions is $$$2, 2$$$, its subsequences are not cyclic shifts of the permutation.
2,000
true
false
false
true
true
false
false
false
false
false
5,012
1928C
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 924 (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 brute force math number theory *1600 No tag edit access β†’ Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST C. Physical Education Lesson time limit per test1 second memory limit per test256 megabytes In a well-known school, a physical education lesson took place. As usual, everyone was lined up and asked to settle in "the first–$$$k$$$-th" position. As is known, settling in "the first–$$$k$$$-th" position occurs as follows: the first $$$k$$$ people have numbers $$$1, 2, 3, ldots, k$$$, the next $$$k - 2$$$ people have numbers $$$k - 1, k - 2, ldots, 2$$$, the next $$$k$$$ people have numbers $$$1, 2, 3, ldots, k$$$, and so on. Thus, the settling repeats every $$$2k - 2$$$ positions. Examples of settling are given in the "Note" section. The boy Vasya constantly forgets everything. For example, he forgot the number $$$k$$$ described above. But he remembers the position he occupied in the line, as well as the number he received during the settling. Help Vasya understand how many natural numbers $$$k$$$ fit under the given constraints. Note that the settling exists if and only if $$$k > 1$$$. In particular, this means that the settling does not exist for $$$k = 1$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$) β€” the number of test cases. This is followed by the description of the test cases. The only line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 le x < n le 10^9$$$) β€” Vasya's position in the line and the number Vasya received during the settling. Output For each test case, output a single integer β€” the number of different $$$k$$$ that fit under the given constraints. It can be proven that under the given constraints, the answer is finite. Example input 5 10 2 3 1 76 4 100 99 1000000000 500000000 output 4 1 9 0 1 Note In the first test case, $$$k$$$ equals $$$2, 3, 5, 6$$$ are suitable. An example of settling for these $$$k$$$: $$$k$$$ / β„– $$$1$$$ $$$2$$$ $$$3$$$ $$$4$$$ $$$5$$$ $$$6$$$ $$$7$$$ $$$8$$$ $$$9$$$ $$$10$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$5$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$4$$$ $$$5$$$ $$$4$$$ $$$3$$$ $$$2$$$ $$$1$$$ $$$2$$$ $$$6$$$ $$$1$$$ $$$2$$$ $$$3$$$ $$$4$$$ $$$5$$$ $$$6$$$ $$$5$$$ $$$4$$$ $$$3$$$ $$$2$$$ In the second test case, $$$k = 2$$$ is suitable. Codeforces (c)
1,600
true
false
false
false
false
false
true
false
false
false
721
538E
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game. There is a rooted tree on _n_ nodes, _m_ of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to _m_ are placed in such a way that each number appears exactly in one leaf. Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well. Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers. Input The first line contains a single integer _n_xa0β€” the number of nodes in the tree (1u2009≀u2009_n_u2009≀u20092Β·105). Each of the next _n_u2009-u20091 lines contains two integers _u__i_ and _v__i_ (1u2009≀u2009_u__i_,u2009_v__i_u2009≀u2009_n_)xa0β€” the ends of the edge of the tree; the edge leads from node _u__i_ to node _v__i_. It is guaranteed that the described graph is a rooted tree, and the root is the node 1. Note Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2. In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3.
2,200
true
false
false
true
false
false
false
false
false
false
7,684
1697F
You are asked to build an array $$$a$$$, consisting of $$$n$$$ integers, each element should be from $$$1$$$ to $$$k$$$. The array should be non-decreasing ($$$a_i le a_{i+1}$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$). You are also given additional constraints on it. Each constraint is of one of three following types: $$$1~i~x$$$: $$$a_i$$$ should not be equal to $$$x$$$; $$$2~i~j~x$$$: $$$a_i + a_j$$$ should be less than or equal to $$$x$$$; $$$3~i~j~x$$$: $$$a_i + a_j$$$ should be greater than or equal to $$$x$$$. Build any non-decreasing array that satisfies all constraints or report that no such array exists. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of testcases. The first line of each testcase contains three integers $$$n, m$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^4$$$; $$$0 le m le 2 cdot 10^4$$$; $$$2 le k le 10$$$). The $$$i$$$-th of the next $$$m$$$ lines contains a description of a constraint. Each constraint is of one of three following types: $$$1~i~x$$$ ($$$1 le i le n$$$; $$$1 le x le k$$$): $$$a_i$$$ should not be equal to $$$x$$$; $$$2~i~j~x$$$ ($$$1 le i < j le n$$$; $$$2 le x le 2 cdot k$$$): $$$a_i + a_j$$$ should be less than or equal to $$$x$$$; $$$3~i~j~x$$$ ($$$1 le i < j le n$$$; $$$2 le x le 2 cdot k$$$): $$$a_i + a_j$$$ should be greater than or equal to $$$x$$$. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^4$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 cdot 10^4$$$. Output For each testcase, determine if there exists a non-decreasing array that satisfies all conditions. If there is no such array, then print -1. Otherwise, print any valid arrayxa0β€” $$$n$$$ integers from $$$1$$$ to $$$k$$$. Example Input 4 4 0 4 2 2 3 3 1 2 3 1 2 2 3 3 2 1 1 1 2 2 3 2 3 2 3 2 5 5 5 3 2 5 7 2 4 5 10 3 4 5 6 3 3 4 7 2 1 5 7 Output 1 2 3 4 1 3 -1 1 2 2 5 5
2,800
false
false
true
false
false
true
false
false
false
true
2,095
1359C
There are two infinite sources of water: hot water of temperature $$$h$$$; cold water of temperature $$$c$$$ ($$$c < h$$$). You perform the following procedure of alternating moves: 1. take one cup of the hot water and pour it into an infinitely deep barrel; 2. take one cup of the cold water and pour it into an infinitely deep barrel; 3. take one cup of the hot water $$$dots$$$ 4. and so on $$$dots$$$ Note that you always start with the cup of hot water. The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups. You want to achieve a temperature as close as possible to $$$t$$$. So if the temperature in the barrel is $$$t_b$$$, then the absolute difference of $$$t_b$$$ and $$$t$$$ ($$$t_b - t$$$) should be as small as possible. How many cups should you pour into the barrel, so that the temperature in it is as close as possible to $$$t$$$? If there are multiple answers with the minimum absolute difference, then print the smallest of them. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 3 cdot 10^4$$$)xa0β€” the number of testcases. Each of the next $$$T$$$ lines contains three integers $$$h$$$, $$$c$$$ and $$$t$$$ ($$$1 le c < h le 10^6$$$; $$$c le t le h$$$)xa0β€” the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel. Output For each testcase print a single positive integerxa0β€” the minimum number of cups required to be poured into the barrel to achieve the closest temperature to $$$t$$$. Example Input 3 30 10 20 41 15 30 18 13 18 Note In the first testcase the temperature after $$$2$$$ poured cups: $$$1$$$ hot and $$$1$$$ cold is exactly $$$20$$$. So that is the closest we can achieve. In the second testcase the temperature after $$$7$$$ poured cups: $$$4$$$ hot and $$$3$$$ cold is about $$$29.857$$$. Pouring more water won't get us closer to $$$t$$$ than that. In the third testcase the temperature after $$$1$$$ poured cup: $$$1$$$ hot is $$$18$$$. That's exactly equal to $$$t$$$.
1,700
true
false
false
false
false
false
false
true
false
false
3,889
1991H
Alice and Bob are playing a game with $$$n$$$ piles of stones, where the $$$i$$$-th pile has $$$a_i$$$ stones. Players take turns making moves, with Alice going first. On each move, the player does the following three-step process: 1. Choose an integer $$$k$$$ ($$$1 leq k leq frac n 2$$$). Note that the value of $$$k$$$ can be different for different moves. 2. Remove $$$k$$$ piles of stones. 3. Choose another $$$k$$$ piles of stones and split each pile into two piles. The number of stones in each new pile must be a prime number. The player who is unable to make a move loses. Determine who will win if both players play optimally. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β€” the number of piles of stones. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$)xa0β€” the number of stones in 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 "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise. You can output each letter in any case (upper or lower). For example, the strings "alIcE", "Alice", and "alice" will all be considered identical. Note In the first test case, there are $$$2$$$ piles of stones with $$$2$$$ and $$$1$$$ stones respectively. Since neither $$$1$$$ nor $$$2$$$ can be split into two prime numbers, Alice cannot make a move, so Bob wins. In the second test case, there are $$$3$$$ piles of stones with $$$3$$$, $$$5$$$, and $$$7$$$ stones respectively. Alice can choose $$$k = 1$$$, remove the pile of $$$7$$$ stones, and then split the pile of $$$5$$$ stones into two piles of prime numbers of stones, $$$2$$$ and $$$3$$$. Then, the piles consist of $$$3$$$ piles of stones with $$$3$$$, $$$2$$$, and $$$3$$$ stones respectively, leaving Bob with no valid moves, so Alice wins. In the third test case, there are $$$4$$$ piles of stones with $$$4$$$, $$$6$$$, $$$8$$$, and $$$10$$$ stones respectively. Alice can choose $$$k = 2$$$, removing two piles of $$$8$$$ and $$$10$$$ stones. She splits the pile of $$$4$$$ stones into two piles of prime numbers of stones, $$$2$$$ and $$$2$$$, and the pile of $$$6$$$ stones into two piles of $$$3$$$ and $$$3$$$ stones. Then, Bob has no valid moves, so Alice wins. In the fourth test case, there are $$$5$$$ piles of stones, each containing $$$8$$$ stones. It can be shown that if both players play optimally, Bob will win.
3,300
true
false
false
true
false
false
false
false
false
false
321
1626E
You are given a tree consisting of $$$n$$$ vertices. Some of the vertices (at least two) are black, all the other vertices are white. You place a chip on one of the vertices of the tree, and then perform the following operations: let the current vertex where the chip is located is $$$x$$$. You choose a black vertex $$$y$$$, and then move the chip along the first edge on the simple path from $$$x$$$ to $$$y$$$. You are not allowed to choose the same black vertex $$$y$$$ in two operations in a row (i.u2009e., for every two consecutive operations, the chosen black vertex should be different). You end your operations when the chip moves to the black vertex (if it is initially placed in a black vertex, you don't perform the operations at all), or when the number of performed operations exceeds $$$100^{500}$$$. For every vertex $$$i$$$, you have to determine if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex, if the chip is initially placed on the vertex $$$i$$$. Input The first line contains one integer $$$n$$$ ($$$3 le n le 3 cdot 10^5$$$) β€” the number of vertices in the tree. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$0 le c_i le 1$$$), where $$$c_i = 0$$$ means that the $$$i$$$-th vertex is white, and $$$c_i = 1$$$ means that the $$$i$$$-th vertex is black. At least two values of $$$c_i$$$ are equal to $$$1$$$. Then $$$n-1$$$ lines follow, each of them contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i e v_i$$$) β€” the endpoints of some edge. These edges form a tree. Output Print $$$n$$$ integers. The $$$i$$$-th integer should be equal to $$$1$$$ if there exists a (possibly empty) sequence of operations that moves the chip to some black vertex if it is placed on the vertex $$$i$$$, and $$$0$$$ if no such sequence of operations exists. Example Input 8 0 1 0 0 0 0 1 0 8 6 2 5 7 8 6 5 4 5 6 1 7 3
2,400
false
true
false
false
false
false
false
false
false
false
2,483
1105C
Problem - 1105C - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags combinatorics dp math *1500 No tag edit access β†’ Contest materials . The sum of all the elements was divisible by $$$3$$$. Unfortunately, Ayoub has lost his array, but he remembers the size of the array $$$n$$$ and the numbers $$$l$$$ and $$$r$$$, so he asked you to find the number of ways to restore the array. Since the answer could be very large, print it modulo $$$10^9 + 7$$$ (i.e. the remainder when dividing by $$$10^9 + 7$$$). In case there are no satisfying arrays (Ayoub has a wrong memory), print $$$0$$$. Input The first and only line contains three integers $$$n$$$, $$$l$$$ and $$$r$$$ ($$$1 le n le 2 cdot 10^5 , 1 le l le r le 10^9$$$)xa0β€” the size of the lost array and the range of numbers in the array. Output Print the remainder when dividing by $$$10^9 + 7$$$ the number of ways to restore the array. Examples Input 2 1 3 Output 3 Input 3 2 2 Output 1 Input 9 9 99 Output 711426616 Note In the first example, the possible arrays are : $$$
1,500
true
false
false
true
false
false
false
false
false
false
5,178
1623B
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 763 (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 brute force dfs and similar implementation sortings *1100 No tag edit access β†’ Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST B. Game on Ranges time limit per test1 second memory limit per test256 megabytes Alice and Bob play the following game. Alice has a set $$$S$$$ of disjoint ranges of integers, initially containing only one range $$$[1, n]$$$. In one turn, Alice picks a range $$$[l, r]$$$ from the set $$$S$$$ and asks Bob to pick a number in the range. Bob chooses a number $$$d$$$ ($$$l le d le r$$$). Then Alice removes $$$[l, r]$$$ from $$$S$$$ and puts into the set $$$S$$$ the range $$$[l, d - 1]$$$ (if $$$l le d - 1$$$) and the range $$$[d + 1, r]$$$ (if $$$d + 1 le r$$$). The game ends when the set $$$S$$$ is empty. We can show that the number of turns in each game is exactly $$$n$$$. After playing the game, Alice remembers all the ranges $$$[l, r]$$$ she picked from the set $$$S$$$, but Bob does not remember any of the numbers that he picked. But Bob is smart, and he knows he can find out his numbers $$$d$$$ from Alice's ranges, and so he asks you for help with your programming skill. Given the list of ranges that Alice has picked ($$$[l, r]$$$), for each range, help Bob find the number $$$d$$$ that Bob has picked. We can show that there is always a unique way for Bob to choose his number for a list of valid ranges picked by Alice. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 1000$$$). Each of the next $$$n$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$), denoting the range $$$[l, r]$$$ that Alice picked at some point. Note that the ranges are given in no particular order. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$, and the ranges for each test case are from a valid game. Output For each test case print $$$n$$$ lines. Each line should contain three integers $$$l$$$, $$$r$$$, and $$$d$$$, denoting that for Alice's range $$$[l, r]$$$ Bob picked the number $$$d$$$. You can print the lines in any order. We can show that the answer is unique. It is not required to print a new line after each test case. The new lines in the output of the example are for readability only. Example input 4 1 1 1 3 1 3 2 3 2 2 6 1 1 3 5 4 4 3 6 4 5 1 6 5 1 5 1 2 4 5 2 2 4 4 output 1 1 1 1 3 1 2 2 2 2 3 3 1 1 1 3 5 3 4 4 4 3 6 6 4 5 5 1 6 2 1 5 3 1 2 1 4 5 5 2 2 2 4 4 4 Note In the first test case, there is only 1 range $$$[1, 1]$$$. There was only one range $$$[1, 1]$$$ for Alice to pick, and there was only one number $$$1$$$ for Bob to pick. In the second test case, $$$n = 3$$$. Initially, the set contains only one range $$$[1, 3]$$$. Alice picked the range $$$[1, 3]$$$. Bob picked the number $$$1$$$. Then Alice put the range $$$[2, 3]$$$ back to the set, which after this turn is the only range in the set. Alice picked the range $$$[2, 3]$$$. Bob picked the number $$$3$$$. Then Alice put the range $$$[2, 2]$$$ back to the set. Alice picked the range $$$[2, 2]$$$. Bob picked the number $$$2$$$. The game ended. In the fourth test case, the game was played with $$$n = 5$$$. Initially, the set contains only one range $$$[1, 5]$$$. The game's turn is described in the following table. Game turn Alice's picked range Bob's picked number The range set after Before the game start $$$ { [1, 5] } $$$ 1 $$$[1, 5]$$$ $$$3$$$ $$$ { [1, 2], [4, 5] }$$$ 2 $$$[1, 2]$$$ $$$1$$$ $$$ { [2, 2], [4, 5] } $$$ 3 $$$[4, 5]$$$ $$$5$$$ $$$ { [2, 2], [4, 4] } $$$ 4 $$$[2, 2]$$$ $$$2$$$ $$$ { [4, 4] } $$$ 5 $$$[4, 4]$$$ $$$4$$$ $$$ { } $$$ (empty set) Codeforces (c)
1,100
false
false
true
false
false
false
true
false
true
false
2,504
1351B
Vasya claims that he had a paper square. He cut it into two rectangular parts using one vertical or horizontal cut. Then Vasya informed you the dimensions of these two rectangular parts. You need to check whether Vasya originally had a square. In other words, check if it is possible to make a square using two given rectangles. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β€” the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given in two lines. The first line contains two integers $$$a_1$$$ and $$$b_1$$$ ($$$1 le a_1, b_1 le 100$$$) β€” the dimensions of the first one obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length). The second line contains two integers $$$a_2$$$ and $$$b_2$$$ ($$$1 le a_2, b_2 le 100$$$) β€” the dimensions of the second obtained after cutting rectangle. The sizes are given in random order (that is, it is not known which of the numbers is the width, and which of the numbers is the length). Output Print $$$t$$$ answers, each of which is a string "YES" (in the case of a positive answer) or "NO" (in the case of a negative answer). The letters in words can be printed in any case (upper or lower). Example Input 3 2 3 3 1 3 2 1 3 3 3 1 3
900
true
false
true
false
false
false
true
false
false
false
3,954
1329A
Dreamoon likes coloring cells very much. There is a row of $$$n$$$ cells. Initially, all cells are empty (don't contain any color). Cells are numbered from $$$1$$$ to $$$n$$$. You are given an integer $$$m$$$ and $$$m$$$ integers $$$l_1, l_2, ldots, l_m$$$ ($$$1 le l_i le n$$$) Dreamoon will perform $$$m$$$ operations. In $$$i$$$-th operation, Dreamoon will choose a number $$$p_i$$$ from range $$$[1, n-l_i+1]$$$ (inclusive) and will paint all cells from $$$p_i$$$ to $$$p_i+l_i-1$$$ (inclusive) in $$$i$$$-th color. Note that cells may be colored more one than once, in this case, cell will have the color from the latest operation. Dreamoon hopes that after these $$$m$$$ operations, all colors will appear at least once and all cells will be colored. Please help Dreamoon to choose $$$p_i$$$ in each operation to satisfy all constraints. Input The first line contains two integers $$$n,m$$$ ($$$1 leq m leq n leq 100,000$$$). The second line contains $$$m$$$ integers $$$l_1, l_2, ldots, l_m$$$ ($$$1 leq l_i leq n$$$). Output If it's impossible to perform $$$m$$$ operations to satisfy all constraints, print "'-1" (without quotes). Otherwise, print $$$m$$$ integers $$$p_1, p_2, ldots, p_m$$$ ($$$1 leq p_i leq n - l_i + 1$$$), after these $$$m$$$ operations, all colors should appear at least once and all cells should be colored. If there are several possible solutions, you can print any.
1,800
true
true
true
false
false
true
false
false
false
false
4,065
1707C
You are given a connected undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. The weight of the $$$i$$$-th edge is $$$i$$$. Here is a wrong algorithm of finding a of a graph: vis := an array of length n s := a set of edgesfunction dfs(u): vis[u] := true iterate through each edge (u, v) in the order from smallest to largest edge weight if vis[v] = false add edge (u, v) into the set (s) dfs(v) function findMST(u): reset all elements of (vis) to false reset the edge set (s) to empty dfs(u) return the edge set (s) Each of the calls findMST(1), findMST(2), ..., findMST(n) gives you a spanning tree of the graph. Determine which of these trees are minimum spanning trees. Input The first line of the input contains two integers $$$n$$$, $$$m$$$ ($$$2le nle 10^5$$$, $$$n-1le mle 2cdot 10^5$$$)xa0β€” the number of vertices and the number of edges in the graph. Each of the following $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1le u_i, v_ile n$$$, $$$u_i e v_i$$$), describing an undirected edge $$$(u_i,v_i)$$$ in the graph. The $$$i$$$-th edge in the input has weight $$$i$$$. It is guaranteed that the graph is connected and there is at most one edge between any pair of vertices. Output You need to output a binary string $$$s$$$, where $$$s_i=1$$$ if findMST(i) creates an MST, and $$$s_i = 0$$$ otherwise. Examples Input 5 5 1 2 3 5 1 3 3 2 4 2 Input 10 11 1 2 2 5 3 4 4 2 8 1 4 5 10 5 9 5 8 2 5 7 4 6 Note Here is the graph given in the first example. There is only one minimum spanning tree in this graph. A minimum spanning tree is $$$(1,2),(3,5),(1,3),(2,4)$$$ which has weight $$$1+2+3+5=11$$$. Here is a part of the process of calling findMST(1): reset the array vis and the edge set s; calling dfs(1); vis[1] := true; iterate through each edge $$$(1,2),(1,3)$$$; add edge $$$(1,2)$$$ into the edge set s, calling dfs(2): vis[2] := true iterate through each edge $$$(2,1),(2,3),(2,4)$$$; because vis[1] = true, ignore the edge $$$(2,1)$$$; add edge $$$(2,3)$$$ into the edge set s, calling dfs(3): ... In the end, it will select edges $$$(1,2),(2,3),(3,5),(2,4)$$$ with total weight $$$1+4+2+5=12>11$$$, so findMST(1) does not find a minimum spanning tree. It can be shown that the other trees are all MSTs, so the answer is 01111.
2,400
false
true
false
false
false
false
false
false
true
true
2,032
142B
Once upon a time in the Kingdom of Far Far Away lived Sir Lancelot, the chief Royal General. He was very proud of his men and he liked to invite the King to come and watch drill exercises which demonstrated the fighting techniques and tactics of the squad he was in charge of. But time went by and one day Sir Lancelot had a major argument with the Fairy Godmother (there were rumors that the argument occurred after the general spoke badly of the Godmother's flying techniques. That seemed to hurt the Fairy Godmother very deeply). As the result of the argument, the Godmother put a rather strange curse upon the general. It sounded all complicated and quite harmless: "If the squared distance between some two soldiers equals to 5, then those soldiers will conflict with each other!" The drill exercises are held on a rectangular _n_u2009Γ—u2009_m_ field, split into _nm_ square 1u2009Γ—u20091 segments for each soldier. Thus, the square of the distance between the soldiers that stand on squares (_x_1,u2009_y_1) and (_x_2,u2009_y_2) equals exactly (_x_1u2009-u2009_x_2)2u2009+u2009(_y_1u2009-u2009_y_2)2. Now not all _nm_ squad soldiers can participate in the drill exercises as it was before the Fairy Godmother's curse. Unless, of course, the general wants the soldiers to fight with each other or even worse... For example, if he puts a soldier in the square (2,u20092), then he cannot put soldiers in the squares (1,u20094), (3,u20094), (4,u20091) and (4,u20093) β€” each of them will conflict with the soldier in the square (2,u20092). Your task is to help the general. You are given the size of the drill exercise field. You are asked to calculate the maximum number of soldiers that can be simultaneously positioned on this field, so that no two soldiers fall under the Fairy Godmother's curse. Note In the first sample test Sir Lancelot can place his 4 soldiers on the 2u2009Γ—u20094 court as follows (the soldiers' locations are marked with gray circles on the scheme): In the second sample test he can place 6 soldiers on the 3u2009Γ—u20094 site in the following manner:
1,800
false
true
true
false
false
true
false
false
false
false
9,306
1406A
Given a set of integers (it can contain equal elements). You have to split it into two subsets $$$A$$$ and $$$B$$$ (both of them can contain equal elements or be empty). You have to maximize the value of $$$mex(A)+mex(B)$$$. Here $$$mex$$$ of a set denotes the smallest non-negative integer that doesn't exist in the set. For example: $$$mex({1,4,0,2,2,1})=3$$$ $$$mex({3,3,2,1,3,0,0})=4$$$ $$$mex(varnothing)=0$$$ ($$$mex$$$ for empty set) The set is splitted into two subsets $$$A$$$ and $$$B$$$ if for any integer number $$$x$$$ the number of occurrences of $$$x$$$ into this set is equal to the sum of the number of occurrences of $$$x$$$ into $$$A$$$ and the number of occurrences of $$$x$$$ into $$$B$$$. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1leq tleq 100$$$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1leq nleq 100$$$) β€” the size of the set. The second line of each testcase contains $$$n$$$ integers $$$a_1,a_2,dots a_n$$$ ($$$0leq a_ileq 100$$$) β€” the numbers in the set. Output For each test case, print the maximum value of $$$mex(A)+mex(B)$$$. Example Input 4 6 0 2 1 5 0 1 3 0 1 2 4 0 2 0 1 6 1 2 3 4 5 6 Note In the first test case, $$$A=left{0,1,2 ight},B=left{0,1,5 ight}$$$ is a possible choice. In the second test case, $$$A=left{0,1,2 ight},B=varnothing$$$ is a possible choice. In the third test case, $$$A=left{0,1,2 ight},B=left{0 ight}$$$ is a possible choice. In the fourth test case, $$$A=left{1,3,5 ight},B=left{2,4,6 ight}$$$ is a possible choice.
900
true
true
true
false
false
false
false
false
false
false
3,646
1914G2
The easy and hard versions of this problem differ only in the constraints on $$$n$$$. In the hard version, the sum of values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Furthermore, there are no additional constraints on the value of $$$n$$$ in a single test case. There are $$$2n$$$ light bulbs arranged in a row. Each light bulb has a color from $$$1$$$ to $$$n$$$ (exactly two light bulbs for each color). Initially, all light bulbs are turned off. You choose a set of light bulbs $$$S$$$ that you initially turn on. After that, you can perform the following operations in any order any number of times: choose two light bulbs $$$i$$$ and $$$j$$$ of the same color, exactly one of which is on, and turn on the second one; choose three light bulbs $$$i, j, k$$$, such that both light bulbs $$$i$$$ and $$$k$$$ are on and have the same color, and the light bulb $$$j$$$ is between them ($$$i < j < k$$$), and turn on the light bulb $$$j$$$. You want to choose a set of light bulbs $$$S$$$ that you initially turn on in such a way that by performing the described operations, you can ensure that all light bulbs are turned on. Calculate two numbers: the minimum size of the set $$$S$$$ that you initially turn on; the number of sets $$$S$$$ of minimum size (taken modulo $$$998244353$$$). Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0β€” the number of pairs of light bulbs. The second line of each test case contains $$$2n$$$ integers $$$c_1, c_2, dots, c_{2n}$$$ ($$$1 le c_i le n$$$), where $$$c_i$$$ is the color of the $$$i$$$-th light bulb. For each color from $$$1$$$ to $$$n$$$, exactly two light bulbs have this color. Additional constraint on the input: the sum of values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two integers: the minimum size of the set $$$S$$$ that you initially turn on; the number of sets $$$S$$$ of minimum size (taken modulo $$$998244353$$$). Example Input 4 2 2 2 1 1 2 1 2 2 1 2 1 2 1 2 5 3 4 4 5 3 1 1 5 2 2
2,300
false
false
false
true
true
false
false
false
false
true
811
1364B
Given a permutation $$$p$$$ of length $$$n$$$, find its subsequence $$$s_1$$$, $$$s_2$$$, $$$ldots$$$, $$$s_k$$$ of length at least $$$2$$$ such that: $$$s_1-s_2+s_2-s_3+ldots+s_{k-1}-s_k$$$ is as big as possible over all subsequences of $$$p$$$ with length at least $$$2$$$. Among all such subsequences, choose the one whose length, $$$k$$$, is as small as possible. If multiple subsequences satisfy these conditions, you are allowed to find any of them. A sequence $$$a$$$ is a subsequence of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some (possibly, zero or all) elements. A permutation of length $$$n$$$ is an array of length $$$n$$$ in which every element from $$$1$$$ to $$$n$$$ occurs exactly once. Input The first line contains an integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$)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 le n le 10^5$$$)xa0β€” the length of the permutation $$$p$$$. The second line of each test case contains $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$ldots$$$, $$$p_{n}$$$ ($$$1 le p_i le n$$$, $$$p_i$$$ are distinct)xa0β€” the elements of the permutation $$$p$$$. The sum of $$$n$$$ across the test cases doesn't exceed $$$10^5$$$. Output For each test case, the first line should contain the length of the found subsequence, $$$k$$$. The second line should contain $$$s_1$$$, $$$s_2$$$, $$$ldots$$$, $$$s_k$$$xa0β€” its elements. If multiple subsequences satisfy these conditions, you are allowed to find any of them. Note In the first test case, there are $$$4$$$ subsequences of length at least $$$2$$$: $$$[3,2]$$$ which gives us $$$3-2=1$$$. $$$[3,1]$$$ which gives us $$$3-1=2$$$. $$$[2,1]$$$ which gives us $$$2-1=1$$$. $$$[3,2,1]$$$ which gives us $$$3-2+2-1=2$$$. So the answer is either $$$[3,1]$$$ or $$$[3,2,1]$$$. Since we want the subsequence to be as short as possible, the answer is $$$[3,1]$$$.
1,300
false
true
false
false
false
false
false
false
false
false
3,861
483B
You have two friends. You want to present each of them several positive integers. You want to present _cnt_1 numbers to the first friend and _cnt_2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends. In addition, the first friend does not like the numbers that are divisible without remainder by prime number _x_. The second one does not like the numbers that are divisible without remainder by prime number _y_. Of course, you're not going to present your friends numbers they don't like. Your task is to find such minimum number _v_, that you can form presents using numbers from a set 1,u20092,u2009...,u2009_v_. Of course you may choose not to present some numbers at all. A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself. Input The only line contains four positive integers _cnt_1, _cnt_2, _x_, _y_ (1u2009≀u2009_cnt_1,u2009_cnt_2u2009<u2009109; _cnt_1u2009+u2009_cnt_2u2009≀u2009109; 2u2009≀u2009_x_u2009<u2009_y_u2009≀u20093Β·104)xa0β€” the numbers that are described in the statement. It is guaranteed that numbers _x_, _y_ are prime. Output Print a single integer β€” the answer to the problem. Note In the first sample you give the set of numbers {1,u20093,u20095} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1,u20093,u20095} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend. In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1,u20092,u20094} to the second friend. Thus, the answer to the problem is 4.
1,800
true
false
false
false
false
false
false
true
false
false
7,903
1822G1
This is the easy version of the problem. The only difference is that in this version, $$$a_i le 10^6$$$. For a given sequence of $$$n$$$ integers $$$a$$$, a triple $$$(i, j, k)$$$ is called magic if: $$$1 le i, j, k le n$$$. $$$i$$$, $$$j$$$, $$$k$$$ are pairwise distinct. there exists a positive integer $$$b$$$ such that $$$a_i cdot b = a_j$$$ and $$$a_j cdot b = a_k$$$. Kolya received a sequence of integers $$$a$$$ as a gift and now wants to count the number of magic triples for it. Help him with this task! Note that there are no constraints on the order of integers $$$i$$$, $$$j$$$ and $$$k$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β€” the number of test cases. The description of the test cases follows. The first line of the test case contains a single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$)xa0β€” the length of the sequence. The second line of the test contains $$$n$$$ integers $$$a_1, a_2, a_3, dots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0β€” the elements of the sequence $$$a$$$. 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 number of magic triples for the sequence $$$a$$$. Example Input 7 5 1 7 7 2 7 3 6 2 18 9 1 2 3 4 5 6 7 8 9 4 1000 993 986 179 7 1 10 100 1000 10000 100000 1000000 8 1 1 2 2 4 4 8 8 9 1 1 1 2 2 2 4 4 4 Note In the first example, there are $$$6$$$ magic triples for the sequence $$$a$$$xa0β€” $$$(2, 3, 5)$$$, $$$(2, 5, 3)$$$, $$$(3, 2, 5)$$$, $$$(3, 5, 2)$$$, $$$(5, 2, 3)$$$, $$$(5, 3, 2)$$$. In the second example, there is a single magic triple for the sequence $$$a$$$xa0β€” $$$(2, 1, 3)$$$.
1,700
true
false
false
false
true
false
true
false
false
false
1,345
471E
Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to do some painting. As they were trying to create their first masterpiece, they made a draft on a piece of paper. The draft consists of _n_ segments. Each segment was either horizontal or vertical. Now the friends want to simplify the draft by deleting some segments or parts of segments so that the final masterpiece meets three conditions: 1. Horace wants to be able to paint the whole picture in one stroke: by putting the brush on the paper and never taking it off until the picture is ready. The brush can paint the same place multiple times. That's why all the remaining segments must form a single connected shape. 2. Menshykov wants the resulting shape to be simple. He defines a simple shape as a shape that doesn't contain any cycles. 3. Initially all the segment on the draft have integer startpoint and endpoint coordinates. Uslada doesn't like real coordinates and she wants this condition to be fulfilled after all the changes. As in other parts the draft is already beautiful, the friends decided to delete such parts of the draft that the sum of lengths of the remaining segments is as large as possible. Your task is to count this maximum sum of the lengths that remain after all the extra segments are removed. Input The first line of the input contains integer _n_ (1u2009≀u2009_n_u2009≀u20092Β·105) β€” the number of segments on the draft. The next _n_ lines contain four integers each: _x_1, _y_1, _x_2, _y_2 (u2009-u2009109u2009≀u2009_x_1u2009≀u2009_x_2u2009≀u2009109;xa0u2009-u2009109u2009≀u2009_y_1u2009≀u2009_y_2u2009≀u2009109) β€” the two startpoint and the two endpoint coordinates of a segment. All segments are non-degenerative and either are strictly horizontal or strictly vertical. No two horizontal segments share common points. No two vertical segments share common points. Output Print a single integer β€” the maximum sum of lengths for the remaining segments. Examples Input 4 0 0 1 0 0 0 0 1 1 -1 1 2 0 1 1 1 Note The shapes that you can get in the two given samples are: In the first sample you need to delete any segment as the two segments together do not form a single connected shape. In the second sample the initial segments form a cycle, there are four ways to break the cycle: delete the first, second or fourth segment altogether or delete the middle of the third segment. The last way is shown on the picture.
2,700
false
false
false
false
true
false
false
false
false
false
7,948
383E
Iahubina is tired of so many complicated languages, so she decided to invent a new, simple language. She already made a dictionary consisting of _n_ 3-words. A 3-word is a sequence of exactly 3 lowercase letters of the first 24 letters of the English alphabet (_a_ to _x_). She decided that some of the letters are vowels, and all the others are consonants. The whole language is based on a simple rule: any word that contains at least one vowel is correct. Iahubina forgot which letters are the vowels, and wants to find some possible correct sets of vowels. She asks Iahub questions. In each question, she will give Iahub a set of letters considered vowels (in this question). For each question she wants to know how many words of the dictionary are correct, considering the given set of vowels. Iahubina wants to know the _xor_ of the squared answers to all the possible questions. There are 224 different questions, they are all subsets of the set of the first 24 letters of the English alphabet. Help Iahub find that number. Input The first line contains one integer, _n_ (1u2009≀u2009_n_u2009≀u2009104). Each of the next _n_ lines contains a 3-word consisting of 3 lowercase letters. There will be no two identical 3-words. Output Print one number, the _xor_ of the squared answers to the queries.
2,700
false
false
false
true
false
false
false
false
false
false
8,294
1503F
A balanced bracket sequence is defined as an integer sequence that can be built with the following rules: The empty sequence is balanced. If $$$[a_1,ldots,a_n]$$$ and $$$[b_1,ldots, b_m]$$$ are balanced, then their concatenation $$$[a_1,ldots,a_n,b_1,ldots,b_m]$$$ is balanced. If $$$x$$$ is a positive integer and $$$[a_1,ldots,a_n]$$$ is balanced, then $$$[x,a_1,ldots,a_n,-x]$$$ is balanced. The positive numbers can be imagined as opening brackets and the negative numbers as closing brackets, where matching brackets must have the same type (absolute value). For example, $$$[1, 2, -2, -1]$$$ and $$$[1, 3, -3, 2, -2, -1]$$$ are balanced, but $$$[1, 2, -1, -2]$$$ and $$$[-1, 1]$$$ are not balanced. There are $$$2n$$$ cards. Each card has a number on the front and a number on the back. Each integer $$$1,-1,2,-2,ldots,n,-n$$$ appears exactly once on the front of some card and exactly once on the back of some (not necessarily the same) card. You can reorder the cards however you like. You are not allowed to flip cards, so numbers cannot move between the front and back. Your task is to order the cards so that the sequences given by the front numbers and the back numbers are both balanced, or report that it is impossible. Input The first line contains a single integer $$$n$$$ ($$$1le nle 2cdot 10^5$$$) β€” the number of bracket types, and half the number of cards. The next $$$2n$$$ lines describe the cards. The $$$i$$$-th of these lines contains two integers $$$a_i$$$, $$$b_i$$$ ($$$-nle a_i,b_ile n$$$, $$$a_i e 0$$$, $$$b_i e 0$$$) β€” the numbers on the front and back of the $$$i$$$-th card, respectively. Every integer $$$1,-1,2,-2,ldots,n,-n$$$ appears exactly once as $$$a_i$$$ and exactly once as $$$b_i$$$. Output On the first line, output "YES" if it's possible to reorder these cards to satisfy the condition. Otherwise, output "NO". You can print each letter in any case (upper or lower). If it is possible, on the next $$$2n$$$ lines output the cards in an order so that the front and back are both balanced. If there are multiple solutions, you may output any. Examples Input 5 1 3 -3 -5 4 -3 2 2 -1 -4 -2 5 3 -1 5 1 -4 4 -5 -2 Output YES 1 3 4 -3 -4 4 -1 -4 5 1 3 -1 2 2 -2 5 -3 -5 -5 -2 Input 2 1 1 -1 2 2 -1 -2 -2 Note In the first test case, the front numbers create the balanced sequence $$$[1,4,-4,-1,5,3,2,-2,-3,-5]$$$ and the back numbers create the balanced sequence $$$[3,-3,4,-4,1,-1,2,5,-5,-2]$$$. In the second test case, the cards are given in an order so that the front numbers are balanced, but the back numbers create the unbalanced sequence $$$[1,2,-1,-2]$$$. If we swapped the second and third cards, we would balance the back numbers and unbalance the front numbers. But there is no order that balances both.
3,500
false
false
true
false
true
true
false
false
false
true
3,155
1370E
Naman has two binary strings $$$s$$$ and $$$t$$$ of length $$$n$$$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $$$s$$$ into $$$t$$$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $$$s$$$ and rotate it clockwise once. For example, if $$$s = 1 extbf{1}101 extbf{00}$$$, he can choose a subsequence corresponding to indices ($$$1$$$-based) $$${2, 6, 7 }$$$ and rotate them clockwise. The resulting string would then be $$$s = 1 extbf{0}101 extbf{10}$$$. A string $$$a$$$ is said to be a subsequence of string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $$$c$$$ of size $$$k$$$ is to perform an operation which sets $$$c_1:=c_k, c_2:=c_1, c_3:=c_2, ldots, c_k:=c_{k-1}$$$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $$$s$$$ into $$$t$$$ or say that it is impossible. Input The first line contains a single integer $$$n$$$ $$$(1 le n le 10^6)$$$xa0β€” the length of the strings. The second line contains the binary string $$$s$$$ of length $$$n$$$. The third line contains the binary string $$$t$$$ of length $$$n$$$. Output If it is impossible to convert $$$s$$$ to $$$t$$$ after any number of operations, print $$$-1$$$. Otherwise, print the minimum number of operations required. Examples Input 10 1111100000 0000011111 Input 10 1111100000 1111100001 Note In the first test, Naman can choose the subsequence corresponding to indices $$${2, 6}$$$ and rotate it once to convert $$$s$$$ into $$$t$$$. In the second test, he can rotate the subsequence corresponding to all indices $$$5$$$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $$$s$$$ into $$$t$$$.
2,100
false
true
false
false
true
true
false
true
false
false
3,817
1672E
This is an interactive problem. There are $$$n$$$ words in a text editor. The $$$i$$$-th word has length $$$l_i$$$ ($$$1 leq l_i leq 2000$$$). The array $$$l$$$ is hidden and only known by the grader. The text editor displays words in lines, splitting each two words in a line with at least one space. Note that a line does not have to end with a space. Let the height of the text editor refer to the number of lines used. For the given width, the text editor will display words in such a way that the height is minimized. More formally, suppose that the text editor has width $$$w$$$. Let $$$a$$$ be an array of length $$$k+1$$$ where $$$1=a_1 < a_2 < ldots < a_{k+1}=n+1$$$. $$$a$$$ is a valid array if for all $$$1 leq i leq k$$$, $$$l_{a_i}+1+l_{a_i+1}+1+ldots+1+l_{a_{i+1}-1} leq w$$$. Then the height of the text editor is the minimum $$$k$$$ over all valid arrays. Note that if $$$w < max(l_i)$$$, the text editor cannot display all the words properly and will crash, and the height of the text editor will be $$$0$$$ instead. You can ask $$$n+30$$$ queries. In one query, you provide a width $$$w$$$. Then, the grader will return the height $$$h_w$$$ of the text editor when its width is $$$w$$$. Find the minimum area of the text editor, which is the minimum value of $$$w cdot h_w$$$ over all $$$w$$$ for which $$$h_w eq 0$$$. The lengths are fixed in advance. In other words, the interactor is not adaptive. Input The first and only line of input contains a single integer $$$n$$$ ($$$1 leq n leq 2000$$$) xa0β€” the number of words on the text editor. It is guaranteed that the hidden lengths $$$l_i$$$ satisfy $$$1 leq l_i leq 2000$$$. Interaction Begin the interaction by reading $$$n$$$. To make a query, print "? $$$w$$$" (without quotes, $$$1 leq w leq 10^9$$$). Then you should read our response from standard input, that is, $$$h_w$$$. If your program has made an invalid query or has run out of tries, the interactor will terminate immediately and your program will get a verdict Wrong answer. To give the final answer, print "! $$$area$$$" (without the quotes). Note that giving this answer is not counted towards the limit of $$$n+30$$$ queries. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks The first line of input must contain a single integer $$$n$$$ ($$$1 leq n leq 2000$$$) β€” the number of words in the text editor. The second line of input must contain exactly $$$n$$$ space-separated integers $$$l_1,l_2,ldots,l_n$$$ ($$$1 leq l_i leq 2000$$$). Note In the first test case, the words are $$${ exttt{glory}, exttt{to}, exttt{ukraine}, exttt{and}, exttt{anton}, exttt{trygub}}$$$, so $$$l={5,2,7,3,5,6}$$$. If $$$w=1$$$, then the text editor is not able to display all words properly and will crash. The height of the text editor is $$$h_1=0$$$, so the grader will return $$$0$$$. If $$$w=9$$$, then a possible way that the words will be displayed on the text editor is: $$$ exttt{glory__to}$$$ $$$ exttt{ukraine__}$$$ $$$ exttt{and_anton}$$$ $$$ exttt{__trygub_}$$$ The height of the text editor is $$$h_{9}=4$$$, so the grader will return $$$4$$$. If $$$w=16$$$, then a possible way that the words will be displayed on the text editor is: $$$ exttt{glory_to_ukraine}$$$ $$$ exttt{and_anton_trygub}$$$ The height of the text editor is $$$h_{16}=2$$$, so the grader will return $$$2$$$. We have somehow figured out that the minimum area of the text editor is $$$32$$$, so we answer it.
2,200
false
true
false
false
false
true
false
true
false
false
2,241
501E
Problem - 501E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags binary search combinatorics implementation *2500 No tag edit access β†’ Contest materials (1u2009≀u2009_l_u2009≀u2009_r_u2009≀u2009_n_), that the elements from the _l_-th to the _r_-th one inclusive can be rearranged in such a way that the whole array will be a palindrome. In other words, pair (_l_,u2009_r_) should meet the condition that after some rearranging of numbers on positions from _l_ to _r_, inclusive (it is allowed not to rearrange the numbers at all), for any 1u2009≀u2009_i_u2009≀u2009_n_ following condition holds: _a_
2,500
false
false
true
false
false
false
false
true
false
false
7,826
509E
Problem - 509E - Codeforces =============== xa0 ]( --- Finished β†’ Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β†’ Problem tags math strings *2000 No tag edit access β†’ Contest materials + qualifying round for the WCC") (Second Winter Computer Camp Selection 2015)") which is equal to 1, if _c_ is a vowel, and to 0 otherwise. Let _s__i_ be the _i_-th character of string _s_, and _s__i_.._j_ be the substring of word _s_, staring at the _i_-th character and ending at the _j_-th character (_s__is__i_u2009+u20091... _s__j_, _i_u2009≀u2009_j_). Then the simple prettiness of _s_ is defined by the formula: The prettiness of _s_ equals Find the prettiness of the given song title. We assume that the vowels are _I_,u2009_E_,u2009_A_,u2009_O_,u2009_U_,u2009_Y_. Input The input contains a single string _s_ (1u2009≀u2009_s_u2009≀u20095Β·105) β€” the title of the song. Output Print the prettiness of the song with the absolute or relative error of at most 10u2009-u20096. Examples Input IEAIAIO Output 28.0000000 Input BYOB Output 5.8333333 Input YISVOWEL Output 17.0500000 Note In the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, the prettiness of the song equals to 28.
2,000
true
false
false
false
false
false
false
false
false
false
7,802
519D
A and B are preparing themselves for programming contests. After several years of doing sports programming and solving many problems that require calculating all sorts of abstract objects, A and B also developed rather peculiar tastes. A likes lowercase letters of the Latin alphabet. He has assigned to each letter a number that shows how much he likes that letter (he has assigned negative numbers to the letters he dislikes). B likes substrings. He especially likes the ones that start and end with the same letter (their length must exceed one). Also, A and B have a string _s_. Now they are trying to find out how many substrings _t_ of a string _s_ are interesting to B (that is, _t_ starts and ends with the same letter and its length is larger than one), and also the sum of values of all letters (assigned by A), except for the first and the last one is equal to zero. Naturally, A and B have quickly found the number of substrings _t_ that are interesting to them. Can you do it? Input The first line contains 26 integers _x__a_,u2009_x__b_,u2009...,u2009_x__z_ (u2009-u2009105u2009≀u2009_x__i_u2009≀u2009105) β€” the value assigned to letters _a_,u2009_b_,u2009_c_,u2009...,u2009_z_ respectively. The second line contains string _s_ of length between 1 and 105 characters, consisting of Lating lowercase lettersβ€” the string for which you need to calculate the answer. Output Print the answer to the problem. Examples Input 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1 xabcab Input 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 7 1 1 1 8 1 1 1 1 1 1 aaa Note In the first sample test strings satisfying the condition above are _abca_ and _bcab_. In the second sample test strings satisfying the condition above are two occurences of _aa_.
1,800
false
false
false
true
true
false
false
false
false
false
7,759
1042E
Vasya has got a magic matrix $$$a$$$ of size $$$n imes m$$$. The rows of the matrix are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Let $$$a_{ij}$$$ be the element in the intersection of the $$$i$$$-th row and the $$$j$$$-th column. Vasya has also got a chip. Initially, the chip is in the intersection of the $$$r$$$-th row and the $$$c$$$-th column (that is, in the element $$$a_{rc}$$$). Vasya performs the following process as long as possible: among all elements of the matrix having their value less than the value of the element with the chip in it, Vasya randomly and equiprobably chooses one element and moves his chip to this element. After moving the chip, he adds to his score the square of the Euclidean distance between these elements (that is, between the element in which the chip is now and the element the chip was moved from). The process ends when there are no elements having their values less than the value of the element with the chip in it. Euclidean distance between matrix elements with coordinates $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is equal to $$$sqrt{(i_1-i_2)^2 + (j_1-j_2)^2}$$$. Calculate the expected value of the Vasya's final score. It can be shown that the answer can be represented as $$$frac{P}{Q}$$$, where $$$P$$$ and $$$Q$$$ are coprime integer numbers, and $$$Q otequiv 0~(mod ~ 998244353)$$$. Print the value $$$P cdot Q^{-1}$$$ modulo $$$998244353$$$. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ $$$(1 le n, m le 1,000)$$$ β€” the number of rows and the number of columns in the matrix $$$a$$$. The following $$$n$$$ lines contain description of the matrix $$$a$$$. The $$$i$$$-th line contains $$$m$$$ integers $$$a_{i1}, a_{i2}, dots, a_{im} ~ (0 le a_{ij} le 10^9)$$$. The following line contains two integers $$$r$$$ and $$$c$$$ $$$(1 le r le n, 1 le c le m)$$$ β€” the index of row and the index of column where the chip is now.
2,300
true
false
false
true
false
false
false
false
false
false
5,485
1256B
You are given a permutation of length $$$n$$$. Recall that the permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2, 3, 1, 5, 4]$$$ is a permutation, but $$$[1, 2, 2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1, 3, 4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). You can perform at most $$$n-1$$$ operations with the given permutation (it is possible that you don't perform any operations at all). The $$$i$$$-th operation allows you to swap elements of the given permutation on positions $$$i$$$ and $$$i+1$$$. Each operation can be performed at most once. The operations can be performed in arbitrary order. Your task is to find the lexicographically minimum possible permutation obtained by performing some of the given operations in some order. You can see the definition of the lexicographical order in the notes section. You have to answer $$$q$$$ independent test cases. For example, let's consider the permutation $$$[5, 4, 1, 3, 2]$$$. The minimum possible permutation we can obtain is $$$[1, 5, 2, 4, 3]$$$ and we can do it in the following way: 1. perform the second operation (swap the second and the third elements) and obtain the permutation $$$[5, 1, 4, 3, 2]$$$; 2. perform the fourth operation (swap the fourth and the fifth elements) and obtain the permutation $$$[5, 1, 4, 2, 3]$$$; 3. perform the third operation (swap the third and the fourth elements) and obtain the permutation $$$[5, 1, 2, 4, 3]$$$. 4. perform the first operation (swap the first and the second elements) and obtain the permutation $$$[1, 5, 2, 4, 3]$$$; Another example is $$$[1, 2, 4, 3]$$$. The minimum possible permutation we can obtain is $$$[1, 2, 3, 4]$$$ by performing the third operation (swap the third and the fourth elements). Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 100$$$) β€” the number of test cases. Then $$$q$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 100$$$) β€” the number of elements in the permutation. The second line of the test case contains $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ β€” the given permutation.
1,400
false
true
false
false
false
false
false
false
false
false
4,406
1066F
Maksim walks on a Cartesian plane. Initially, he stands at the point $$$(0, 0)$$$ and in one move he can go to any of four adjacent points (left, right, up, down). For example, if Maksim is currently at the point $$$(0, 0)$$$, he can go to any of the following points in one move: $$$(1, 0)$$$; $$$(0, 1)$$$; $$$(-1, 0)$$$; $$$(0, -1)$$$. There are also $$$n$$$ distinct key points at this plane. The $$$i$$$-th point is $$$p_i = (x_i, y_i)$$$. It is guaranteed that $$$0 le x_i$$$ and $$$0 le y_i$$$ and there is no key point $$$(0, 0)$$$. Let the first level points be such points that $$$max(x_i, y_i) = 1$$$, the second level points be such points that $$$max(x_i, y_i) = 2$$$ and so on. Maksim wants to visit all the key points. But he shouldn't visit points of level $$$i + 1$$$ if he does not visit all the points of level $$$i$$$. He starts visiting the points from the minimum level of point from the given set. The distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$x_1 - x_2 + y_1 - y_2$$$ where $$$v$$$ is the absolute value of $$$v$$$. Maksim wants to visit all the key points in such a way that the total distance he walks will be minimum possible. Your task is to find this distance. If you are Python programmer, consider using PyPy instead of Python when you submit your code. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) β€” the number of key points. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$0 le x_i, y_i le 10^9$$$) β€” $$$x$$$-coordinate of the key point $$$p_i$$$ and $$$y$$$-coordinate of the key point $$$p_i$$$. It is guaranteed that all the points are distinct and the point $$$(0, 0)$$$ is not in this set. Note The picture corresponding to the first example: There is one of the possible answers of length $$$15$$$. The picture corresponding to the second example: There is one of the possible answers of length $$$9$$$.
2,100
false
false
false
true
false
false
false
false
false
false
5,376
1083A
The Fair Nut is going to travel to the Tree Country, in which there are $$$n$$$ cities. Most of the land of this country is covered by forest. Furthermore, the local road system forms a tree (connected graph without cycles). Nut wants to rent a car in the city $$$u$$$ and go by a simple path to city $$$v$$$. He hasn't determined the path, so it's time to do it. Note that chosen path can consist of only one vertex. A filling station is located in every city. Because of strange law, Nut can buy only $$$w_i$$$ liters of gasoline in the $$$i$$$-th city. We can assume, that he has infinite money. Each road has a length, and as soon as Nut drives through this road, the amount of gasoline decreases by length. Of course, Nut can't choose a path, which consists of roads, where he runs out of gasoline. He can buy gasoline in every visited city, even in the first and the last. He also wants to find the maximum amount of gasoline that he can have at the end of the path. Help him: count it. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 3 cdot 10^5$$$)xa0β€” the number of cities. The second line contains $$$n$$$ integers $$$w_1, w_2, ldots, w_n$$$ ($$$0 leq w_{i} leq 10^9$$$)xa0β€” the maximum amounts of liters of gasoline that Nut can buy in cities. Each of the next $$$n - 1$$$ lines describes road and contains three integers $$$u$$$, $$$v$$$, $$$c$$$ ($$$1 leq u, v leq n$$$, $$$1 leq c leq 10^9$$$, $$$u e v$$$), where $$$u$$$ and $$$v$$$xa0β€” cities that are connected by this road and $$$c$$$xa0β€” its length. It is guaranteed that graph of road connectivity is a tree. Output Print one numberxa0β€” the maximum amount of gasoline that he can have at the end of the path. Examples Input 5 6 3 2 5 0 1 2 10 2 3 3 2 4 1 1 5 1 Note The optimal way in the first example is $$$2 o 1 o 3$$$. The optimal way in the second example is $$$2 o 4$$$.
1,800
false
false
false
true
true
false
false
false
false
false
5,308
1956B
You and Nene are playing a card game. The deck with $$$2n$$$ cards is used to play this game. Each card has an integer from $$$1$$$ to $$$n$$$ on it, and each of integers $$$1$$$ through $$$n$$$ appears exactly on $$$2$$$ cards. Additionally, there is a table where cards are placed during the game (initially, the table is empty). In the beginning of the game, these $$$2n$$$ cards are distributed between you and Nene so that each player receives $$$n$$$ cards. After it, you and Nene alternatively take $$$2n$$$ turns, i.e. each person takes $$$n$$$ turns, starting with you. On each turn: The player whose turn is it selects one of the cards in his hand. Let $$$x$$$ be the number on it. The player whose turn is it receives $$$1$$$ point if there is already a card with the integer $$$x$$$ on the table (otherwise, he receives no points). After it, he places the selected card with the integer $$$x$$$ on the table. Note that turns are made publicly: each player can see all the cards on the table at each moment. Nene is very smart so she always selects cards optimally in order to maximize her score in the end of the game (after $$$2n$$$ rounds). If she has several optimal moves, she selects the move that minimizes your score in the end of the game. More formally, Nene always takes turns optimally in order to maximize her score in the end of the game in the first place and to minimize your score in the end of the game in the second place. Assuming that the cards are already distributed and cards in your hand have integers $$$a_1, a_2, ldots, a_n$$$ written on them, what is the maximum number of points you can get by taking your turns optimally? 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 test cases follows. The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β€” the number of cards you and Nene receive in the beginning of the game. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β€” the integers on the cards in your hand. It is guaranteed that each integer from $$$1$$$ through $$$n$$$ appears in the sequence $$$a_1, a_2, ldots, a_n$$$ at most $$$2$$$ times. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output one integer: the maximum number of points you can get. Example Input 5 4 1 1 2 3 8 7 4 1 2 8 8 5 5 8 7 1 4 5 3 4 2 6 3 1 2 3 1 1 Note In the first test case, the integers written on your cards are $$$1$$$, $$$1$$$, $$$2$$$ and $$$3$$$. The integers written on Nene's cards are $$$2$$$, $$$3$$$, $$$4$$$ and $$$4$$$. The game may proceed as follows: 1. You select one of the cards with an integer $$$1$$$ written on it and place it on the table. 2. Nene selects one of the cards with an integer $$$4$$$ written on it and places it on the table. 3. You select the card with an integer $$$1$$$ written on it, receive $$$1$$$ point, and place the selected card on the table. 4. Nene selects the card with an integer $$$4$$$ written on it, receive $$$1$$$ point, and places the selected card on the table. 5. You select the card with an integer $$$2$$$ written on it and place it on the table. 6. Nene selects the card with an integer $$$2$$$ written on it, receive $$$1$$$ point, and places the selected card on the table. 7. You select the card with an integer $$$3$$$ written on it and place it on the table. 8. Nene selects the card with an integer $$$3$$$ written on it, receive $$$1$$$ point, and places the selected card on the table. At the end of the game, you scored $$$1$$$ point, and Nene scored $$$3$$$. It can be shown that you cannot score more than $$$1$$$ point if Nene plays optimally, so the answer is $$$1$$$. In the second test case, if both players play optimally, you score $$$2$$$ points and Nene scores $$$6$$$ points.
800
false
true
false
false
false
false
false
false
false
false
539
1889B
Doremy lives in a country consisting of $$$n$$$ cities numbered from $$$1$$$ to $$$n$$$, with $$$a_i$$$ people living in the $$$i$$$-th city. It can be modeled as an undirected graph with $$$n$$$ nodes. Initially, there are no edges in the graph. Now Doremy wants to make the graph connected. To do this, she can add an edge between $$$i$$$ and $$$j$$$ if $$$$$$ sum_{k in S} a_k ge icdot j cdot c, $$$$$$ where $$$S$$$ is the set of all the nodes that are currently in the same connected component of either $$$i$$$ or $$$j$$$, and $$$c$$$ is a given constant. Can Doremy make the graph connected? Two nodes $$$(i, j)$$$ are in the same connected component if there exists a path from $$$i$$$ to $$$j$$$. A graph is connected if all its nodes are in the same connected component. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β€” the number of test cases. The description of the test cases follows. The first line contains two integers $$$n$$$, $$$c$$$ ($$$2le nle 2cdot 10^5$$$, $$$1 le c le 10^6$$$)xa0β€” the number of nodes and the constant. The second line of each test case contains $$$ n $$$ integers $$$ a_1,a_2,ldots,a_n $$$ ($$$0 le a_i le 10^{12}$$$)xa0β€” the number of people living in the $$$i$$$-th city. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print "YES" (without quotes), if it is possible to make the graph connected, and "NO" (without quotes) otherwise. You can print letters in any case (upper or lower). Example Input 7 4 10 0 20 15 10 2 1 1 1 5 1 0 1 0 4 199 5 2 1 1 3 1 1 5 5 5 6 1 10 2 5 1000000 1000000000000 1000000000000 1000000000000 1000000000000 1000000000000 3 1 0 0 2 Output Yes Yes Yes No No Yes No Note In the first test case, Doremy can add edges in the following order: 1. Add $$$(1,2)$$$. This operation is valid because $$$a_1 + a_2 = 20 ge icdot j cdot c = 20$$$. 2. Add $$$(1,3)$$$. This operation is valid because $$$a_1 + a_2 + a_3 = 35 ge i cdot j cdot c = 30$$$. 3. Add $$$(1,4)$$$. This operation is valid because $$$a_1 + a_2 + a_3 + a_4 = 45 ge i cdot j cdot c = 40$$$. In the second test case, Doremy can add edge $$$(1,2)$$$ because $$$a_1 + a_2 =2 ge 1 cdot 2 cdot 1$$$. After that, the graph is connected. In the third test case, Doremy can add edges in the order $$$(5,4)$$$, $$$(5,3)$$$, $$$(5,2)$$$ and $$$(5,1)$$$. In the fourth test case, Doremy cannot add any edge at all.
1,700
true
true
false
false
false
true
false
false
true
false
965
1977C
Nikita is a student passionate about number theory and algorithms. He faces an interesting problem related to an array of numbers. Suppose Nikita has an array of integers $$$a$$$ of length $$$n$$$. He will call a subsequence$$$^dagger$$$ of the array special if its elements, without changing the order of the remaining elements. For example, $$$[5,2,3]$$$ is a subsequence of $$$[1,5,7,8,2,4,3]$$$. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2000$$$)xa0β€” the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β€” the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output a single integerxa0β€” the length of the longest special subsequence of $$$a$$$. Example Input 6 5 1 2 4 8 16 6 3 2 10 20 60 1 7 2 3 4 6 12 100003 1200036 9 2 42 7 3 6 7 7 1 6 8 4 99 57 179 10203 2 11 40812 1 1 Note In the first test case, the LCM of any non-empty subsequence is contained in $$$a$$$, so the answer is $$$0$$$. In the second test case, we can take the subsequence $$$[3, 2, 10, 1]$$$, its LCM is equal to $$$30$$$, which is not contained in $$$a$$$. In the third test case, we can take the subsequence $$$[2, 3, 6, 100,003]$$$, its LCM is equal to $$$600,018$$$, which is not contained in $$$a$$$.
1,900
true
true
false
true
true
false
true
false
true
false
425
433C
Ryouko is an extremely forgetful girl, she could even forget something that has just happened. So in order to remember, she takes a notebook with her, called Ryouko's Memory Note. She writes what she sees and what she hears on the notebook, and the notebook became her memory. Though Ryouko is forgetful, she is also born with superb analyzing abilities. However, analyzing depends greatly on gathered information, in other words, memory. So she has to shuffle through her notebook whenever she needs to analyze, which is tough work. Ryouko's notebook consists of _n_ pages, numbered from 1 to _n_. To make life (and this problem) easier, we consider that to turn from page _x_ to page _y_, _x_u2009-u2009_y_ pages should be turned. During analyzing, Ryouko needs _m_ pieces of information, the _i_-th piece of information is on page _a__i_. Information must be read from the notebook in order, so the total number of pages that Ryouko needs to turn is . Ryouko wants to decrease the number of pages that need to be turned. In order to achieve this, she can merge two pages of her notebook. If Ryouko merges page _x_ to page _y_, she would copy all the information on page _x_ to _y_xa0(1u2009≀u2009_x_,u2009_y_u2009≀u2009_n_), and consequently, all elements in sequence _a_ that was _x_ would become _y_. Note that _x_ can be equal to _y_, in which case no changes take place. Please tell Ryouko the minimum number of pages that she needs to turn. Note she can apply the described operation at most once before the reading. Note that the answer can exceed 32-bit integers. Input The first line of input contains two integers _n_ and _m_xa0(1u2009≀u2009_n_,u2009_m_u2009≀u2009105). The next line contains _m_ integers separated by spaces: _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009≀u2009_a__i_u2009≀u2009_n_). Note In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence _a_ becomes {1,u20092,u20093,u20093,u20093,u20092}, so the number of pages Ryouko needs to turn is 1u2009-u20092u2009+u20092u2009-u20093u2009+u20093u2009-u20093u2009+u20093u2009-u20093u2009+u20093u2009-u20092u2009=u20093. In the second sample, optimal solution is achieved by merging page 9 to 4.
1,800
true
false
true
false
false
false
false
false
true
false
8,107