contestId
int64
0
1.01k
name
stringlengths
2
58
tags
listlengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
listlengths
0
7
demo-output
listlengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
105
Entertaining Geodetics
[ "brute force", "dsu", "implementation" ]
D. Entertaining Geodetics
1
256
The maps in the game are divided into square cells called Geo Panels. Some of these panels are painted. We shall assume that the Geo Panels without color are painted the transparent color. Besides, the map has so-called Geo Symbols. They look like pyramids of different colors (including Geo Symbols of the transparent...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=300) β€” the height and the width of the map (in cells). Then follow *n* line; each of them contains *m* numbers β€” the Geo Panels' colors. Then follow *n* more lines; each of them contains *m* numbers β€” the Geo Symbols' description. -1 means that the...
Print the single number β€” the total number of repaintings after the Geo Symbol is eliminated. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (you may also use the %I64d specificator).
[ "5 5\n9 0 1 1 0\n0 0 3 2 0\n1 1 1 3 0\n1 1 1 3 0\n0 1 2 0 3\n-1 1 -1 3 -1\n-1 -1 -1 0 -1\n-1 -1 -1 -1 -1\n-1 2 3 -1 -1\n-1 -1 -1 -1 2\n4 2\n" ]
[ "35" ]
All actions of the sample you can see on the following picture: http://assets.codeforces.com/images/geo_slow.gif
[]
46
0
0
38,012
0
none
[ "none" ]
null
null
I won't feel lonely, nor will I be sorrowful... not before everything is buried. A string of *n* beads is left as the message of leaving. The beads are numbered from 1 to *n* from left to right, each having a shape numbered by integers between 1 and *n* inclusive. Some beads may have the same shapes. The memory of a ...
The first line of input contains two space-separated integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100<=000) β€” the number of beads in the string, and the total number of changes and queries, respectively. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=*n*)Β β€” the initial shapes of beads ...
For each query, print one line with an integer β€” the memory of the recalled subsegment.
[ "7 6\n1 2 3 1 3 2 1\n2 3 7\n2 1 3\n1 7 2\n1 3 2\n2 1 6\n2 5 7\n", "7 5\n1 3 2 1 4 2 3\n1 1 4\n2 2 3\n1 1 7\n2 4 5\n1 1 7\n" ]
[ "5\n0\n7\n1\n", "0\n0\n" ]
The initial string of beads has shapes (1, 2, 3, 1, 3, 2, 1). Consider the changes and queries in their order: 1. 2 3 7: the memory of the subsegment [3, 7] is (7 - 4) + (6 - 6) + (5 - 3) = 5; 1. 2 1 3: the memory of the subsegment [1, 3] is (1 - 1) + (2 - 2) + (3 - 3) = 0; 1. 1 7 2: the shape of the 7-th bead ch...
[]
6,000
512,000
0
38,141
0
none
[ "none" ]
null
null
Arkady the air traffic controller is now working with *n* planes in the air. All planes move along a straight coordinate axis with Arkady's station being at point 0 on it. The *i*-th plane, small enough to be represented by a point, currently has a coordinate of *x**i* and is moving with speed *v**i*. It's guaranteed t...
The first line contains two integers *n* and *w* (1<=≀<=*n*<=≀<=100<=000, 0<=≀<=*w*<=&lt;<=105)Β β€” the number of planes and the maximum wind speed. The *i*-th of the next *n* lines contains two integers *x**i* and *v**i* (1<=≀<=|*x**i*|<=≀<=105, *w*<=+<=1<=≀<=|*v**i*|<=≀<=105, *x**i*Β·*v**i*<=&lt;<=0)Β β€” the initial posi...
Output a single integerΒ β€” the number of unordered pairs of planes that can contact Arkady at the same moment.
[ "5 1\n-3 2\n-3 3\n-1 2\n1 -3\n3 -5\n", "6 1\n-3 2\n-2 2\n-1 2\n1 -2\n2 -2\n3 -2\n" ]
[ "3\n", "9\n" ]
In the first example, the following 3 pairs of planes satisfy the requirements: - (2, 5) passes the station at time 3 / 4 with *v*<sub class="lower-index">*wind*</sub> = 1; - (3, 4) passes the station at time 2 / 5 with *v*<sub class="lower-index">*wind*</sub> = 1 / 2; - (3, 5) passes the station at time 4 / 7 wit...
[]
77
7,065,600
0
38,160
566
Restructuring Company
[ "data structures", "dsu" ]
null
null
Even the most successful company can go through a crisis period when you have to make a hard decision β€” to restructure, discard and merge departments, fire employees and do other unpleasant stuff. Let's consider the following model of a company. There are *n* people working for the Large Software Company. Each person ...
The first line of the input contains two integers *n* and *q* (1<=≀<=*n*<=≀<=200<=000, 1<=≀<=*q*<=≀<=500<=000) β€” the number of the employees of the company and the number of queries the crisis manager has. Next *q* lines contain the queries of the crisis manager. Each query looks like *type*Β *x*Β *y*, where . If *type*...
For each question of type 3 print "YES" or "NO" (without the quotes), depending on whether the corresponding people work in the same department.
[ "8 6\n3 2 5\n1 2 5\n3 2 5\n2 4 7\n2 1 2\n3 1 7\n" ]
[ "NO\nYES\nYES\n" ]
none
[ { "input": "8 6\n3 2 5\n1 2 5\n3 2 5\n2 4 7\n2 1 2\n3 1 7", "output": "NO\nYES\nYES" }, { "input": "1 1\n3 1 1", "output": "YES" }, { "input": "1 3\n1 1 1\n2 1 1\n3 1 1", "output": "YES" } ]
1,638
21,094,400
-1
38,162
24
Sequence of points
[ "geometry", "implementation", "math" ]
C. Sequence of points
2
256
You are given the following points with integer coordinates on the plane: *M*0,<=*A*0,<=*A*1,<=...,<=*A**n*<=-<=1, where *n* is odd number. Now we define the following infinite sequence of points *M**i*: *M**i* is symmetric to *M**i*<=-<=1 according (for every natural number *i*). Here point *B* is symmetric to *A* ac...
On the first line you will be given an integer *n* (1<=≀<=*n*<=≀<=105), which will be odd, and *j* (1<=≀<=*j*<=≀<=1018), where *j* is the index of the desired point. The next line contains two space separated integers, the coordinates of *M*0. After that *n* lines follow, where the *i*-th line contain the space separat...
On a single line output the coordinates of *M**j*, space separated.
[ "3 4\n0 0\n1 1\n2 3\n-5 3\n", "3 1\n5 5\n1000 1000\n-1000 1000\n3 100\n" ]
[ "14 0\n", "1995 1995\n" ]
none
[ { "input": "3 4\n0 0\n1 1\n2 3\n-5 3", "output": "14 0" }, { "input": "3 1\n5 5\n1000 1000\n-1000 1000\n3 100", "output": "1995 1995" }, { "input": "1 1\n-1000 -1000\n1000 1000", "output": "3000 3000" }, { "input": "1 1000000000000000000\n-1000 1000\n1000 -1000", "output"...
592
4,710,400
3.843226
38,177
363
Fixing Typos
[ "greedy", "implementation" ]
null
null
Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos. In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" ...
The single line of the input contains word *s*, its length is from 1 to 200000 characters. The given word *s* consists of lowercase English letters.
Print such word *t* that it doesn't contain any typos described in the problem statement and is obtained from *s* by deleting the least number of letters. If there are multiple solutions, print any of them.
[ "helloo\n", "woooooow\n" ]
[ "hello\n", "woow\n" ]
The second valid answer to the test from the statement is "heloo".
[ { "input": "helloo", "output": "hello" }, { "input": "woooooow", "output": "woow" }, { "input": "aabbaa", "output": "aabaa" }, { "input": "yesssssss", "output": "yess" }, { "input": "aabbaabbaabbaabbaabbaabbcccccc", "output": "aabaabaabaabaabaabcc" }, { ...
93
16,896,000
3
38,233
568
New Language
[ "2-sat", "greedy" ]
null
null
Living in Byteland was good enough to begin with, but the good king decided to please his subjects and to introduce a national language. He gathered the best of wise men, and sent an expedition to faraway countries, so that they would find out all about how a language should be designed. After some time, the wise men ...
The first line contains a single line consisting of letters 'V' (Vowel) and 'C' (Consonant), determining which letters are vowels and which letters are consonants. The length of this string *l* is the size of the alphabet of the new language (1<=≀<=*l*<=≀<=26). The first *l* letters of the English alphabet are used as ...
Print a smallest word of a language that is lexicographically not smaller than *s*. If such words does not exist (for example, if the language has no words at all), print "-1" (without the quotes).
[ "VC\n2 1\n1 V 2 C\naa\n", "VC\n2 1\n1 C 2 V\nbb\n", "VCC\n4 3\n1 C 2 V\n2 C 3 V\n3 V 4 V\nabac\n" ]
[ "ab\n", "-1\n", "acaa\n" ]
In the first test word "aa" is not a word of the language, but word "ab" is. In the second test out of all four possibilities only word "bb" is not a word of a language, but all other words are lexicographically less, so there is no answer. In the third test, due to the last rule, "abac" doesn't belong to the languag...
[]
0
0
-1
38,351
213
Relay Race
[ "dp" ]
null
null
Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of *n* meters. The given square is split into *n*<=Γ—<=*n* cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1,<=1), and Rubik stands in ...
The first line contains a single integer (1<=≀<=*n*<=≀<=300). The next *n* lines contain *n* integers each: the *j*-th number on the *i*-th line *a**i*,<=*j* (<=-<=1000<=≀<=*a**i*,<=*j*<=≀<=1000) is the number written in the cell with coordinates (*i*,<=*j*).
On a single line print a single number β€” the answer to the problem.
[ "1\n5\n", "2\n11 14\n16 12\n", "3\n25 16 25\n12 18 19\n11 13 8\n" ]
[ "5\n", "53\n", "136\n" ]
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample:
[ { "input": "1\n5", "output": "5" }, { "input": "2\n11 14\n16 12", "output": "53" }, { "input": "3\n25 16 25\n12 18 19\n11 13 8", "output": "136" }, { "input": "4\n35 2 38 10\n15 19 31 32\n21 19 22 15\n37 33 2 13", "output": "274" }, { "input": "5\n4 32 1 18 41\n47...
122
6,656,000
0
38,362
11
Forward, march!
[ "binary search", "dp", "greedy" ]
E. Forward, march!
1
64
Jack has become a soldier now. Unfortunately, he has trouble with the drill. Instead of marching beginning with the left foot and then changing legs with each step, as ordered, he keeps repeating a sequence of steps, in which he sometimes makes the wrong steps or β€” horror of horrors! β€” stops for a while. For example, i...
The first line of input contains a sequence consisting only of characters 'L', 'R' and 'X', where 'L' corresponds to a step with the left foot, 'R' β€” with the right foot, and 'X' β€” to a break. The length of the sequence will not exceed 106.
Output the maximum percentage of time that Jack can spend marching correctly, rounded down to exactly six digits after the decimal point.
[ "X\n", "LXRR\n" ]
[ "0.000000\n", "50.000000\n" ]
In the second example, if we add two breaks to receive LXXRXR, Jack will march: LXXRXRLXXRXRL... instead of LRLRLRLRLRLRL... and will make the correct step in half the cases. If we didn't add any breaks, the sequence would be incorrect β€” Jack can't step on his right foot twice in a row.
[ { "input": "X", "output": "0.000000" }, { "input": "LXRR", "output": "50.000000" }, { "input": "LXRR", "output": "50.000000" }, { "input": "LLXRXLXRLR", "output": "50.000000" }, { "input": "RXRRXXXRXXRR", "output": "37.500000" }, { "input": "LLLRLXRXLL...
748
49,152,000
0
38,484
280
Maximum Xor Secondary
[ "data structures", "implementation", "two pointers" ]
null
null
Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers *x*1,<=*x*2,<=...,<=*x**k* (*k*<=&gt;<=1) is such maximum element *x**j*, that the following inequality holds: . The lucky number of the sequence of distinct positive integers *x*1,<=*x*2,<...
The first line contains integer *n* (1<=&lt;<=*n*<=≀<=105). The second line contains *n* distinct integers *s*1,<=*s*2,<=...,<=*s**n* (1<=≀<=*s**i*<=≀<=109).
Print a single integer β€” the maximum lucky number among all lucky numbers of sequences *s*[*l*..*r*].
[ "5\n5 2 1 4 3\n", "5\n9 8 3 5 7\n" ]
[ "7\n", "15\n" ]
For the first sample you can choose *s*[4..5] = {4, 3} and its lucky number is (4Β *xor*Β 3) = 7. You can also choose *s*[1..2]. For the second sample you must choose *s*[2..5] = {8, 3, 5, 7}.
[ { "input": "5\n5 2 1 4 3", "output": "7" }, { "input": "5\n9 8 3 5 7", "output": "15" }, { "input": "10\n76969694 71698884 32888447 31877010 65564584 87864180 7850891 1505323 17879621 15722446", "output": "128869996" }, { "input": "10\n4547989 39261040 94929326 38131456 26174...
93
6,656,000
0
38,585
85
Petya and Tree
[ "binary search", "dfs and similar", "probabilities", "sortings", "trees" ]
C. Petya and Tree
3
256
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 h...
The first line contains an odd integer *n* (3<=≀<=*n*<=&lt;<=105), which represents the number of tree nodes. Next *n* lines contain node descriptions. The (*i*<=+<=1)-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...
Print *k* real numbers which are the expectations of answers for the keys specified in the input. The answer should differ from the correct one with the measure of absolute or relative error not exceeding 10<=-<=9.
[ "7\n-1 8\n1 4\n1 12\n2 2\n2 6\n3 10\n3 14\n1\n1\n", "3\n-1 5\n1 3\n1 7\n6\n1\n2\n4\n6\n8\n9\n" ]
[ "8.0000000000\n", "7.0000000000\n7.0000000000\n7.0000000000\n3.0000000000\n3.0000000000\n3.0000000000\n" ]
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.
[]
60
0
0
38,676
1,005
Median on Segments (General Case Edition)
[ "sortings" ]
null
null
You are given an integer sequence $a_1, a_2, \dots, a_n$. Find the number of pairs of indices $(l, r)$ ($1 \le l \le r \le n$) such that the value of median of $a_l, a_{l+1}, \dots, a_r$ is exactly the given number $m$. The median of a sequence is the value of an element which is in the middle of the sequence after s...
The first line contains integers $n$ and $m$ ($1 \le n,m \le 2\cdot10^5$) β€” the length of the given sequence and the required value of the median. The second line contains an integer sequence $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2\cdot10^5$).
Print the required number.
[ "5 4\n1 4 5 60 4\n", "3 1\n1 1 1\n", "15 2\n1 2 3 1 2 3 1 2 3 1 2 3 1 2 3\n" ]
[ "8\n", "6\n", "97\n" ]
In the first example, the suitable pairs of indices are: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 2)$, $(2, 3)$, $(2, 5)$, $(4, 5)$ and $(5, 5)$.
[ { "input": "5 4\n1 4 5 60 4", "output": "8" }, { "input": "3 1\n1 1 1", "output": "6" }, { "input": "15 2\n1 2 3 1 2 3 1 2 3 1 2 3 1 2 3", "output": "97" }, { "input": "1 1\n1", "output": "1" }, { "input": "2 1\n1 2", "output": "2" }, { "input": "2 1\n...
30
0
0
38,677
359
Pair of Numbers
[ "binary search", "brute force", "data structures", "math", "two pointers" ]
null
null
Simon has an array *a*1,<=*a*2,<=...,<=*a**n*, consisting of *n* positive integers. Today Simon asked you to find a pair of integers *l*,<=*r* (1<=≀<=*l*<=≀<=*r*<=≀<=*n*), such that the following conditions hold: 1. there is integer *j* (*l*<=≀<=*j*<=≀<=*r*), such that all integers *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r...
The first line contains integer *n* (1<=≀<=*n*<=≀<=3Β·105). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106).
Print two integers in the first line β€” the number of required pairs and the maximum value of *r*<=-<=*l*. On the following line print all *l* values from optimal pairs in increasing order.
[ "5\n4 6 9 3 6\n", "5\n1 3 5 7 9\n", "5\n2 3 5 7 11\n" ]
[ "1 3\n2 \n", "1 4\n1 \n", "5 0\n1 2 3 4 5 \n" ]
In the first sample the pair of numbers is right, as numbers 6, 9, 3 are divisible by 3. In the second sample all numbers are divisible by number 1. In the third sample all numbers are prime, so conditions 1 and 2 are true only for pairs of numbers (1, 1), (2, 2), (3, 3), (4, 4), (5, 5).
[ { "input": "5\n4 6 9 3 6", "output": "1 3\n2 " }, { "input": "5\n1 3 5 7 9", "output": "1 4\n1 " }, { "input": "5\n2 3 5 7 11", "output": "5 0\n1 2 3 4 5 " }, { "input": "1\n1343", "output": "1 0\n1 " }, { "input": "1\n1000000", "output": "1 0\n1 " }, { ...
295
7,577,600
0
38,792
862
Mahmoud and Ehab and the binary string
[ "binary search", "divide and conquer", "interactive" ]
null
null
Mahmoud and Ehab are in the fourth stage now. Dr. Evil has a hidden binary string of length *n*. He guarantees that there is at least one '0' symbol and at least one '1' symbol in it. Now he wants Mahmoud and Ehab to find a position of any '0' symbol and any '1' symbol. In order to do this, Mahmoud and Ehab can ask Dr...
The first line of input will contain a single integer *n* (2<=≀<=*n*<=≀<=1000)Β β€” the length of the hidden binary string.
To print the final answer, print "! pos0 pos1" (without quotes), where *pos*0 and *pos*1 are positions of some '0' and some '1' in the string (the string is 1-indexed). Don't forget to flush the output after printing the answer!
[ "3\n2\n1\n3\n2\n1\n0" ]
[ "? 000\n? 001\n? 010\n? 011\n? 100\n? 101\n! 2 1" ]
Hamming distance definition: [https://en.wikipedia.org/wiki/Hamming_distance](https://en.wikipedia.org/wiki/Hamming_distance) In the first test case the hidden binary string is 101, The first query is 000, so the Hamming distance is 2. In the second query the hidden string is still 101 and query is 001, so the Hamming...
[ { "input": "101", "output": "3" }, { "input": "0011001100", "output": "4" }, { "input": "01", "output": "2" }, { "input": "0010100101101100001101110001110011000010011011001110010011101010011010100101101001111010111001000100", "output": "8" }, { "input": "010101010...
46
204,800
0
38,808
938
Max History
[ "combinatorics", "math" ]
null
null
You are given an array *a* of length *n*. We define *f**a* the following way: - Initially *f**a*<==<=0, *M*<==<=1; - for every 2<=≀<=*i*<=≀<=*n* if *a**M*<=&lt;<=*a**i* then we set *f**a*<==<=*f**a*<=+<=*a**M* and then set *M*<==<=*i*. Calculate the sum of *f**a* over all *n*! permutations of the array *a* modulo 1...
The first line contains integer *n* (1<=≀<=*n*<=≀<=<=1 000 000) β€” the size of array *a*. Second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=<=*a**i*<=≀<=<=109).
Print the only integer, the sum of *f**a* over all *n*! permutations of the array *a* modulo 109<=+<=7.
[ "2\n1 3\n", "3\n1 1 2\n" ]
[ "1", "4" ]
For the second example all the permutations are: - *p* = [1, 2, 3] : *f*<sub class="lower-index">*a*</sub> is equal to 1; - *p* = [1, 3, 2] : *f*<sub class="lower-index">*a*</sub> is equal to 1; - *p* = [2, 1, 3] : *f*<sub class="lower-index">*a*</sub> is equal to 1; - *p* = [2, 3, 1] : *f*<sub class="lower-index"...
[ { "input": "2\n1 3", "output": "1" }, { "input": "3\n1 1 2", "output": "4" }, { "input": "6\n1 4 5 2 3 3", "output": "2928" }, { "input": "8\n8 7 5 4 6 6 6 6", "output": "351360" }, { "input": "8\n1 2 3 9 100 100 100 100", "output": "109296" }, { "inpu...
61
5,632,000
0
38,921
77
Heroes
[ "brute force", "implementation" ]
A. Heroes
2
256
The year of 2012 is coming... According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us...
The first line contains a single non-negative integer *n* (0<=≀<=*n*<=≀<=42) β€” amount of liking between the heroes. Next *n* lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p <=β‰ <= q). Every liking is described in the input exactly once, no hero likes himself. In the last line ...
Print two integers β€” the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team). When calculating the second answer, the team divisio...
[ "3\nTroll likes Dracul\nDracul likes Anka\nSnowy likes Hexadecimal\n210 200 180\n", "2\nAnka likes Chapay\nChapay likes Anka\n10000 50 50\n" ]
[ "30 3\n", "1950 2\n" ]
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
[ { "input": "3\nTroll likes Dracul\nDracul likes Anka\nSnowy likes Hexadecimal\n210 200 180", "output": "30 3" }, { "input": "2\nAnka likes Chapay\nChapay likes Anka\n10000 50 50", "output": "1950 2" }, { "input": "11\nSnowy likes Dracul\nAnka likes Dracul\nChapay likes Snowy\nHexadecimal...
404
3,891,200
3.891752
38,925
489
Hiking
[ "binary search", "dp" ]
null
null
A traveler is planning a water hike along the river. He noted the suitable rest points for the night and wrote out their distances from the starting point. Each of these locations is further characterized by its picturesqueness, so for the *i*-th rest point the distance from the start equals *x**i*, and its picturesque...
The first line of the input contains integers *n*,<=*l* (1<=≀<=*n*<=≀<=1000,<=1<=≀<=*l*<=≀<=105) β€” the number of rest points and the optimal length of one day path. Then *n* lines follow, each line describes one rest point as a pair of integers *x**i*,<=*b**i* (1<=≀<=*x**i*,<=*b**i*<=≀<=106). No two rest points have t...
Print the traveler's path as a sequence of the numbers of the resting points he used in the order he used them. Number the points from 1 to *n* in the order of increasing *x**i*. The last printed number must be equal to *n*.
[ "5 9\n10 10\n20 10\n30 1\n31 5\n40 10\n" ]
[ "1 2 4 5 " ]
In the sample test the minimum value of relative total frustration approximately equals 0.097549. This value can be calculated as <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bad16faba2aa8ac4e81ca909b5e927a7f644c23f.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "5 9\n10 10\n20 10\n30 1\n31 5\n40 10", "output": "1 2 4 5 " }, { "input": "1 20\n9 1", "output": "1 " }, { "input": "2 7\n1 9\n5 6", "output": "2 " }, { "input": "3 2\n2 6\n3 9\n6 8", "output": "1 2 3 " }, { "input": "4 3\n1 6\n5 10\n9 9\n10 6", "o...
919
15,257,600
3
38,980
883
Renovation
[ "constructive algorithms", "greedy", "sortings" ]
null
null
The mayor of the Berland city S sees the beauty differently than other city-dwellers. In particular, he does not understand at all, how antique houses can be nice-looking. So the mayor wants to demolish all ancient buildings in the city. The city S is going to host the football championship very soon. In order to make...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100<=000)Β β€” the number of months before the championship and the number of ancient buildings in the city S. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where *a**i* is the tranche of the *i*-...
Output single integerΒ β€” the maximal number of buildings the mayor can demolish.
[ "2 3\n2 4\n6 2 3\n1 3 2\n", "3 5\n5 3 1\n5 2 9 1 10\n4 2 1 3 10\n", "5 6\n6 3 2 4 3\n3 6 4 5 4 2\n1 4 3 2 5 3\n" ]
[ "2\n", "3\n", "6\n" ]
In the third example the mayor acts as follows. In the first month he obtains 6 burles tranche and demolishes buildings #2 (renovation cost 6, demolishing cost 4) and #4 (renovation cost 5, demolishing cost 2). He spends all the money on it. After getting the second month tranche of 3 burles, the mayor selects only b...
[ { "input": "2 3\n2 4\n6 2 3\n1 3 2", "output": "2" }, { "input": "3 5\n5 3 1\n5 2 9 1 10\n4 2 1 3 10", "output": "3" }, { "input": "5 6\n6 3 2 4 3\n3 6 4 5 4 2\n1 4 3 2 5 3", "output": "6" }, { "input": "1 5\n9\n1 2 3 4 5\n5 4 3 2 1", "output": "3" }, { "input": "...
46
0
0
39,019
26
Tickets
[ "combinatorics", "math", "probabilities" ]
D. Tickets
2
256
As a big fan of Formula One, Charlie is really happy with the fact that he has to organize ticket sells for the next Grand Prix race in his own city. Unfortunately, the finacial crisis is striking everywhere and all the banknotes left in his country are valued either 10 euros or 20 euros. The price of all tickets for t...
The input consist of a single line with three space separated integers, *n*, *m* and *k* (0<=≀<=*n*,<=*m*<=≀<=105, 0<=≀<=*k*<=≀<=10).
Output on a single line the desired probability with at least 4 digits after the decimal point.
[ "5 3 1\n", "0 5 5\n", "0 1 0\n" ]
[ "0.857143\n", "1\n", "0\n" ]
none
[ { "input": "5 3 1", "output": "0.857143" }, { "input": "0 5 5", "output": "1" }, { "input": "0 1 0", "output": "0" }, { "input": "95105 76851 10", "output": "0.904215" }, { "input": "60503 53620 1", "output": "0.214637" }, { "input": "25902 30390 2", ...
310
2,048,000
3.918685
39,026
348
Turtles
[ "dp", "matrices" ]
null
null
You've got a table of size *n*<=Γ—<=*m*. We'll consider the table rows numbered from top to bottom 1 through *n*, and the columns numbered from left to right 1 through *m*. Then we'll denote the cell in row *x* and column *y* as (*x*,<=*y*). Initially cell (1,<=1) contains two similar turtles. Both turtles want to get ...
The first line contains two integers *n*,<=*m* (2<=≀<=*n*,<=*m*<=≀<=3000). Each of the following *n* lines contains *m* characters describing the table. The empty cells are marked by characters ".", the cells with obstacles are marked by "#". It is guaranteed that the upper left and the lower right cells are empty.
In a single line print a single integer β€” the number of pairs of non-intersecting paths from cell (1,<=1) to cell (*n*,<=*m*) modulo 1000000007 (109<=+<=7).
[ "4 5\n.....\n.###.\n.###.\n.....\n", "2 3\n...\n...\n" ]
[ "1\n", "1\n" ]
none
[ { "input": "10 10\n.#.#.#.#..\n#...##.###\n...#...##.\n..##......\n#.###..#.#\n.###..#.#.\n...#...##.\n.....#.##.\n.#.#....##\n#....###..", "output": "0" }, { "input": "10 8\n.#######\n########\n########\n########\n########\n########\n########\n########\n########\n#######.", "output": "0" } ]
92
0
0
39,034
71
Round Table Knights
[ "dp", "math", "number theory" ]
C. Round Table Knights
0
256
There are *n* knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knight...
The first line contains number *n*, which is the number of knights at the round table (3<=≀<=*n*<=≀<=105). The second line contains space-separated moods of all the *n* knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
[ "3\n1 1 1\n", "6\n1 0 1 1 1 0\n", "6\n1 0 0 1 0 1\n" ]
[ "YES", "YES", "NO" ]
none
[ { "input": "3\n1 1 1", "output": "YES" }, { "input": "6\n1 0 1 1 1 0", "output": "YES" }, { "input": "6\n1 0 0 1 0 1", "output": "NO" }, { "input": "10\n1 0 1 1 1 0 1 0 1 0", "output": "YES" }, { "input": "15\n0 0 0 1 0 1 1 0 1 0 0 1 0 1 0", "output": "YES" ...
77
7,270,400
3
39,099
920
Tanks
[ "dp", "greedy", "implementation" ]
null
null
Petya sometimes has to water his field. To water the field, Petya needs a tank with exactly *V* ml of water. Petya has got *N* tanks, *i*-th of them initially containing *a**i* ml of water. The tanks are really large, any of them can contain any amount of water (no matter how large this amount is). Also Petya has got...
The first line contains 3 integers: *N* (2<=≀<=*N*<=≀<=5000), *K* (1<=≀<=*K*<=≀<=5000), and *V* (0<=≀<=*V*<=≀<=109) β€” the number of tanks, the maximum volume of water the scoop can contain, and the required amount of water in some tank, respectively. The second line contains *N* integers *a**i* (0<=≀<=*a**i*<=≀<=105),...
If it is impossible to obtain a tank with exactly *V* ml of water, print NO. Otherwise print YES in the first line, and beginning from the second line, print the sequence of operations in the following format: Each line has to contain 3 numbers denoting a compressed operation: "*cnt* *x* *y*" (1<=≀<=*cnt*<=≀<=109,<...
[ "2 3 5\n2 3\n", "2 3 4\n2 3\n", "5 2 0\n1 3 5 7 9\n" ]
[ "YES\n1 2 1\n", "NO\n", "YES\n2 2 1\n3 3 1\n4 4 1\n5 5 1\n" ]
none
[ { "input": "2 3 5\n2 3", "output": "YES\n1 2 1" }, { "input": "2 3 4\n2 3", "output": "NO" }, { "input": "5 2 0\n1 3 5 7 9", "output": "YES\n2 2 1\n3 3 1\n4 4 1\n5 5 1" }, { "input": "5 10 3\n3 4 5 6 7", "output": "YES\n1 3 2\n1 4 2\n1 5 2" }, { "input": "6 4 8\n5...
31
0
0
39,171
144
Competition
[ "data structures", "greedy" ]
null
null
The secondary diagonal of a square matrix is a diagonal going from the top right to the bottom left corner. Let's define an *n*-degree staircase as a square matrix *n*<=Γ—<=*n* containing no squares above the secondary diagonal (the picture below shows a 5-degree staircase). The squares of the *n*-degree staircase con...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=105). Then *m* lines contain coordinates of sportsmen on the staircase as pairs of integers *r**i*,<=*c**i* (1<=≀<=*r**i*,<=*c**i*<=≀<=*n*, *n*<=-<=*c**i*<=&lt;<=*r**i*), where *r**i* is the number of the staircase row, *c**i* is the number of the st...
In the first line print the number of the chosen sportsmen. In the second line print the numbers of chosen sportsmen in any order, separating the numbers with spaces. If there are several answers, you are permitted to print any of them. The sportsmen are numbered starting from one in the order in which they are given i...
[ "3 3\n2 3\n3 2\n3 3\n" ]
[ "3\n1 2 3 \n" ]
A note to the first sample.
[ { "input": "3 3\n2 3\n3 2\n3 3", "output": "3\n1 2 3 " }, { "input": "1 1\n1 1", "output": "1\n1 " }, { "input": "2 3\n1 2\n2 1\n2 2", "output": "2\n1 2 " }, { "input": "2 1\n1 2", "output": "1\n1 " }, { "input": "2 2\n1 2\n2 1", "output": "2\n1 2 " }, { ...
1,060
34,201,600
3
39,279
253
Printer
[ "binary search", "data structures", "implementation", "sortings" ]
null
null
Let's consider a network printer that functions like that. It starts working at time 0. In each second it can print one page of a text. At some moments of time the printer receives printing tasks. We know that a printer received *n* tasks. Let's number the tasks by consecutive integers from 1 to *n*. Then the task numb...
The first line contains integer *n* (1<=≀<=*n*<=≀<=50000). Next *n* lines describe the tasks. The *i*-th of these lines contains three integers *t**i*, *s**i* and *p**i*, separated by single spaces (0<=≀<=*t**i*<=≀<=109,<=1<=≀<=*s**i*,<=*p**i*<=≀<=109). Exactly one task (let's assume that his number is *x*) has number ...
In the first line print integer *p**x* β€” the priority of the task number *x* (1<=≀<=*p**x*<=≀<=109, remember that all priorities should be distinct). Then print *n* integers, the *i*-th of them represents the moment of time when the last page of the task number *i* finished printing. It is guaranteed that at least on...
[ "3\n4 3 -1\n0 2 2\n1 3 3\n7\n", "3\n3 1 2\n2 3 3\n3 1 -1\n4\n" ]
[ "4\n7 8 4\n", "4\n7 6 4\n" ]
Let's consider the first test case. Let's assume that the unknown priority equals 4, then the printer's actions for each second are as follows: - the beginning of the 1-st second (time 0). The queue has task 2. The printer prints the first page of this task; - the beginning of the 2-nd second (time 1). The queue has...
[]
92
0
0
39,427
582
Number of Binominal Coefficients
[ "dp", "math", "number theory" ]
null
null
For a given prime integer *p* and integers Ξ±,<=*A* calculate the number of pairs of integers (*n*,<=*k*), such that 0<=≀<=*k*<=≀<=*n*<=≀<=*A* and is divisible by *p*Ξ±. As the answer can be rather large, print the remainder of the answer moduly 109<=+<=7. Let us remind you that is the number of ways *k* objects can...
The first line contains two integers, *p* and Ξ± (1<=≀<=*p*,<=Ξ±<=≀<=109, *p* is prime). The second line contains the decimal record of integer *A* (0<=≀<=*A*<=&lt;<=101000) without leading zeroes.
In the single line print the answer to the problem.
[ "2 2\n7\n", "3 1\n9\n", "3 3\n9\n", "2 4\n5000\n" ]
[ "3\n", "17\n", "0\n", "8576851\n" ]
In the first sample three binominal coefficients divisible by 4 are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a4c2b94fb12d1298dafcd1d14d7e1f6a47500264.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/804...
[]
46
0
0
39,428
732
Sanatorium
[ "binary search", "constructive algorithms", "greedy", "implementation", "math" ]
null
null
Vasiliy spent his vacation in a sanatorium, came back and found that he completely forgot details of his vacation! Every day there was a breakfast, a dinner and a supper in a dining room of the sanatorium (of course, in this order). The only thing that Vasiliy has now is a card from the dining room contaning notes ho...
The only line contains three integers *b*, *d* and *s* (0<=≀<=*b*,<=*d*,<=*s*<=≀<=1018,<=<=*b*<=+<=*d*<=+<=*s*<=β‰₯<=1)Β β€” the number of breakfasts, dinners and suppers which Vasiliy had during his vacation in the sanatorium.
Print single integerΒ β€” the minimum possible number of meals which Vasiliy could have missed during his vacation.
[ "3 2 1\n", "1 0 0\n", "1 1 1\n", "1000000000000000000 0 1000000000000000000\n" ]
[ "1\n", "0\n", "0\n", "999999999999999999\n" ]
In the first sample, Vasiliy could have missed one supper, for example, in case he have arrived before breakfast, have been in the sanatorium for two days (including the day of arrival) and then have left after breakfast on the third day. In the second sample, Vasiliy could have arrived before breakfast, have had it,...
[ { "input": "3 2 1", "output": "1" }, { "input": "1 0 0", "output": "0" }, { "input": "1 1 1", "output": "0" }, { "input": "1000000000000000000 0 1000000000000000000", "output": "999999999999999999" }, { "input": "1000 0 0", "output": "1998" }, { "input...
46
0
3
39,509
908
New Year and Arbitrary Arrangement
[ "dp", "math", "probabilities" ]
null
null
You are given three integers *k*, *p**a* and *p**b*. You will construct a sequence with the following algorithm: Initially, start with the empty sequence. Each second, you do the following. With probability *p**a*<=/<=(*p**a*<=+<=*p**b*), add 'a' to the end of the sequence. Otherwise (with probability *p**b*<=/<=(*p**...
The first line will contain three integers integer *k*,<=*p**a*,<=*p**b* (1<=≀<=*k*<=≀<=1<=000, 1<=≀<=*p**a*,<=*p**b*<=≀<=1<=000<=000).
Print a single integer, the answer to the problem.
[ "1 1 1\n", "3 1 4\n" ]
[ "2\n", "370000006\n" ]
The first sample, we will keep appending to our sequence until we get the subsequence 'ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopp...
[ { "input": "1 1 1", "output": "2" }, { "input": "3 1 4", "output": "370000006" }, { "input": "1000 123456 654321", "output": "977760856" }, { "input": "305 337309 378395", "output": "174667130" }, { "input": "108 531040 908573", "output": "145579983" }, { ...
186
16,076,800
-1
39,546
292
SMSC
[ "implementation" ]
null
null
Some large corporation where Polycarpus works has its own short message service center (SMSC). The center's task is to send all sorts of crucial information. Polycarpus decided to check the efficiency of the SMSC. For that, he asked to give him the statistics of the performance of the SMSC for some period of time. In...
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=103) β€” the number of tasks of the SMSC. Next *n* lines contain the tasks' descriptions: the *i*-th line contains two space-separated integers *t**i* and *c**i* (1<=≀<=*t**i*,<=*c**i*<=≀<=106) β€” the time (the second) when the *i*-th task was received and the nu...
In a single line print two space-separated integers β€” the time when the last text message was sent and the maximum queue size at a certain moment of time.
[ "2\n1 1\n2 1\n", "1\n1000000 10\n", "3\n3 3\n4 3\n5 3\n" ]
[ "3 1\n", "1000010 10\n", "12 7\n" ]
In the first test sample: - second 1: the first message has appeared in the queue, the queue's size is 1; - second 2: the first message is sent, the second message has been received, the queue's size is 1; - second 3: the second message is sent, the queue's size is 0, Thus, the maximum size of the queue is 1, the...
[ { "input": "2\n1 1\n2 1", "output": "3 1" }, { "input": "1\n1000000 10", "output": "1000010 10" }, { "input": "3\n3 3\n4 3\n5 3", "output": "12 7" }, { "input": "1\n1 1", "output": "2 1" }, { "input": "2\n1 11\n100 10", "output": "110 11" }, { "input":...
60
0
0
39,607
47
Safe
[ "brute force" ]
D. Safe
5
256
Vasya tries to break in a safe. He knows that a code consists of *n* numbers, and every number is a 0 or a 1. Vasya has made *m* attempts to enter the code. After each attempt the system told him in how many position stand the right numbers. It is not said in which positions the wrong numbers stand. Vasya has been so u...
The first input line contains two integers *n* and *m* (6<=≀<=*n*<=≀<=35,<=1<=≀<=*m*<=≀<=10) which represent the number of numbers in the code and the number of attempts made by Vasya. Then follow *m* lines, each containing space-separated *s**i* and *c**i* which correspondingly indicate Vasya’s attempt (a line contain...
Print the single number which indicates how many possible code variants that do not contradict the *m* system responses are left.
[ "6 2\n000000 2\n010100 4\n", "6 3\n000000 2\n010100 4\n111100 0\n", "6 3\n000000 2\n010100 4\n111100 2\n" ]
[ "6\n", "0\n", "1\n" ]
none
[ { "input": "6 2\n000000 2\n010100 4", "output": "6" }, { "input": "6 3\n000000 2\n010100 4\n111100 0", "output": "0" }, { "input": "6 3\n000000 2\n010100 4\n111100 2", "output": "1" }, { "input": "6 1\n101011 2", "output": "15" }, { "input": "7 2\n1011111 2\n10011...
60
0
0
39,796
388
Fox and Perfect Sets
[ "math" ]
null
null
Fox Ciel studies number theory. She thinks a non-empty set *S* contains non-negative integers is perfect if and only if for any (*a* can be equal to *b*), . Where operation *xor* means exclusive or operation (http://en.wikipedia.org/wiki/Exclusive_or). Please calculate the number of perfect sets consisting of intege...
The first line contains an integer *k* (0<=≀<=*k*<=≀<=109).
Print a single integer β€” the number of required sets modulo 1000000007 (109<=+<=7).
[ "1\n", "2\n", "3\n", "4\n" ]
[ "2\n", "3\n", "5\n", "6\n" ]
In example 1, there are 2 such sets: {0} and {0, 1}. Note that {1} is not a perfect set since 1 xor 1 = 0 and {1} doesn't contain zero. In example 4, there are 6 such sets: {0}, {0, 1}, {0, 2}, {0, 3}, {0, 4} and {0, 1, 2, 3}.
[ { "input": "1", "output": "2" }, { "input": "2", "output": "3" }, { "input": "3", "output": "5" }, { "input": "4", "output": "6" }, { "input": "8", "output": "17" }, { "input": "5", "output": "8" }, { "input": "6", "output": "11" }, ...
62
20,172,800
0
39,821
327
Magic Five
[ "combinatorics", "math" ]
null
null
There is a long plate *s* containing *n* digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his "magic number" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros. Now Iahub wants to count the number of ...
In the first line you're given a string *a* (1<=≀<=|*a*|<=≀<=105), containing digits only. In the second line you're given an integer *k* (1<=≀<=*k*<=≀<=109). The plate *s* is formed by concatenating *k* copies of *a* together. That is *n*<==<=|*a*|Β·*k*.
Print a single integer β€” the required number of ways modulo 1000000007 (109<=+<=7).
[ "1256\n1\n", "13990\n2\n", "555\n2\n" ]
[ "4\n", "528\n", "63\n" ]
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125. In the second case, remember to concatenate the copies of *a*. The actual plate is 1399013990. In the third case, except deleting all digits, any choice will do. Therefore there are 2<sup class="upper-index">6<...
[ { "input": "1256\n1", "output": "4" }, { "input": "13990\n2", "output": "528" }, { "input": "555\n2", "output": "63" }, { "input": "14\n178", "output": "0" }, { "input": "277557766562106078327886194146355351781887756238383139670139581436190170050799912854698535037...
92
1,945,600
0
39,887
0
none
[ "none" ]
null
null
Anton came to a chocolate factory. There he found a working conveyor and decided to run on it from the beginning to the end. The conveyor is a looped belt with a total length of 2*l* meters, of which *l* meters are located on the surface and are arranged in a straight line. The part of the belt which turns at any mome...
The first line contains space-separated integers *n*, *l*, *v*1 and *v*2 (1<=≀<=*n*<=≀<=105, 1<=≀<=*l*,<=*v*1,<=*v*2<=≀<=109) β€” the number of the chocolates, the length of the conveyor's visible part, the conveyor's speed and Anton's speed. The second line contains a sequence of space-separated integers *a*1,<=*a*2,<=...
Print *n*<=+<=1 numbers (one per line): the probabilities that Anton picks up exactly *i* chocolates, for each *i* from 0 (the first line) to *n* (the last line). The answer will be considered correct if each number will have absolute or relative error of at most than 10<=-<=9.
[ "1 1 1 1\n0\n", "2 3 1 2\n2 5\n" ]
[ "0.75000000000000000000\n0.25000000000000000000\n", "0.33333333333333331000\n0.66666666666666663000\n0.00000000000000000000\n" ]
In the first sample test Anton can pick up a chocolate if by the moment he starts running its coordinate is less than 0.5; but if by the moment the boy starts running the chocolate's coordinate is greater than or equal to 0.5, then Anton won't be able to pick it up. As all positions of the belt are equiprobable, the pr...
[]
46
0
0
39,888
952
Puzzling Language
[ "constructive algorithms" ]
null
null
In this problem you will write a simple code generator for a 2D programming language derived from [Brainfuck](https://en.wikipedia.org/wiki/Brainfuck). The code in this language is a rectangular grid of characters '.' and 'X'. The code is converted to a Brainfuck program as follows: the characters are read in the usua...
The input consists of a single string of characters with ASCII codes between 33 ('!') and 122 ('z'), inclusive. The length of the string is between 1 and 10 characters, inclusive.
Output a program in the described language which, when executed, will print the given message.
[ "$$$" ]
[ ".......X.......\n......XXX......\n.....XXXXX.....\n....XXXXXXX....\n...XXXXXXXXX...\n..XXXXXXXXXXX..\n.XXXXXXXXXXXXX.\n...............\nX.............X\nX..............\nX..............\nX..............\n" ]
The example corresponds to the following Brainfuck program: The triangular block decrements the first memory cell and sets the value of the second memory cell to 36 - the ASCII code of '$' character. The next line after the triangular block moves the memory pointer to the second memory cell, and the next three lines p...
[ { "input": "$$$", "output": "..\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\n.X\nX.\...
30
0
0
39,918
451
Count Good Substrings
[ "math" ]
null
null
We call a string good, if after merging all the consecutive equal characters, the resulting string is palindrome. For example, "aabba" is good, because after the merging step it will become "aba". Given a string, you have to find two values: 1. the number of good substrings of even length; 1. the number of good sub...
The first line of the input contains a single string of length *n* (1<=≀<=*n*<=≀<=105). Each character of the string will be either 'a' or 'b'.
Print two space-separated integers: the number of good substrings of even length and the number of good substrings of odd length.
[ "bb\n", "baab\n", "babb\n", "babaa\n" ]
[ "1 2\n", "2 4\n", "2 5\n", "2 7\n" ]
In example 1, there are three good substrings ("b", "b", and "bb"). One of them has even length and two of them have odd length. In example 2, there are six good substrings (i.e. "b", "a", "a", "b", "aa", "baab"). Two of them have even length and four of them have odd length. In example 3, there are seven good substr...
[ { "input": "bb", "output": "1 2" }, { "input": "baab", "output": "2 4" }, { "input": "babb", "output": "2 5" }, { "input": "babaa", "output": "2 7" }, { "input": "baabbbb", "output": "7 11" }, { "input": "babbbbbaaabaabbabbabbababbaaba", "output": ...
2,000
10,956,800
0
39,927
566
Max and Min
[ "geometry" ]
null
null
Two kittens, Max and Min, play with a pair of non-negative integers *x* and *y*. As you can guess from their names, kitten Max loves to maximize and kitten Min loves to minimize. As part of this game Min wants to make sure that both numbers, *x* and *y* became negative at the same time, and kitten Max tries to prevent ...
The first line contains two integers, *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100<=000) β€” the number of pairs of numbers available to Max and Min, correspondingly. The second line contains two integers *x*, *y* (1<=≀<=*x*,<=*y*<=≀<=109) β€” the initial values of numbers with which the kittens are playing. Next *n* lines conta...
Print Β«MaxΒ» (without the quotes), if kitten Max wins, or "Min" (without the quotes), if kitten Min wins.
[ "2 2\n42 43\n2 3\n3 2\n3 10\n10 3\n", "1 1\n1 1\n3 4\n1 1\n" ]
[ "Min\n", "Max\n" ]
In the first test from the statement Min can respond to move (2, 3) by move (3, 10), and to move (3, 2) by move (10, 3). Thus, for each pair of Max and Min's moves the values of both numbers *x* and *y* will strictly decrease, ergo, Min will win sooner or later. In the second sample test after each pair of Max and Min...
[]
46
0
0
40,057
581
Kojiro and Furrari
[ "dp", "greedy" ]
null
null
Motorist Kojiro spent 10 years saving up for his favorite car brand, Furrari. Finally Kojiro's dream came true! Kojiro now wants to get to his girlfriend Johanna to show off his car to her. Kojiro wants to get to his girlfriend, so he will go to her along a coordinate line. For simplicity, we can assume that Kojiro is...
The first line of the input contains four positive integers *e*,<=*s*,<=*n*,<=*m* (1<=≀<=*e*,<=*s*<=≀<=109,<=1<=≀<=*n*,<=*m*<=≀<=2Β·105) β€” the coordinate of the point where Johanna is, the capacity of a Furrari tank, the number of gas stations and the number of starting points. Next *n* lines contain two integers each...
Print exactly *m* lines. The *i*-th of them should contain two integers β€” the minimum amount of gas of type Regular-92 and type Premium-95, if Kojiro starts at point *f**i*. First you need to minimize the first value. If there are multiple ways to do it, you need to also minimize the second value. If there is no way t...
[ "8 4 1 1\n2 4\n0\n", "9 3 2 3\n2 3\n1 6\n-1 0 1\n", "20 9 2 4\n1 5\n2 10\n-1 0 1 2\n" ]
[ "0 4\n", "-1 -1\n3 3\n3 2\n", "-1 -1\n-1 -1\n-1 -1\n-1 -1\n" ]
none
[]
62
9,523,200
-1
40,111
804
The same permutation
[ "constructive algorithms" ]
null
null
Seyyed and MoJaK are friends of Sajjad. Sajjad likes a permutation. Seyyed wants to change the permutation in a way that Sajjad won't like it. Seyyed thinks more swaps yield more probability to do that, so he makes MoJaK to perform a swap between every pair of positions (*i*,<=*j*), where *i*<=&lt;<=*j*, exactly once. ...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=1000)Β β€” the size of the permutation. As the permutation is not important, you can consider *a**i*<==<=*i*, where the permutation is *a*1,<=*a*2,<=...,<=*a**n*.
If it is not possible to swap all pairs of positions so that the permutation stays the same, print "NO", Otherwise print "YES", then print lines: the *i*-th of these lines should contain two integers *a* and *b* (*a*<=&lt;<=*b*)Β β€” the positions where the *i*-th swap is performed.
[ "3\n", "1\n" ]
[ "NO\n", "YES\n" ]
none
[ { "input": "3", "output": "NO" }, { "input": "1", "output": "YES" }, { "input": "5", "output": "YES\n3 5\n3 4\n4 5\n1 3\n2 4\n2 3\n1 4\n1 5\n1 2\n2 5" }, { "input": "6", "output": "NO" }, { "input": "7", "output": "NO" }, { "input": "8", "output": ...
30
0
0
40,113
140
New Year Cards
[ "brute force", "greedy", "implementation" ]
null
null
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has *n* friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to *n* in the order in which they send the cards. Le...
The first line contains an integer *n* (2<=≀<=*n*<=≀<=300) β€” the number of Alexander's friends, equal to the number of cards. Next *n* lines contain his friends' preference lists. Each list consists of *n* different integers from 1 to *n*. The last line contains Alexander's preference list in the same format.
Print *n* space-separated numbers: the *i*-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the *i*-th friend. If there are several solutions, print any of them.
[ "4\n1 2 3 4\n4 1 3 2\n4 3 1 2\n3 4 2 1\n3 1 2 4\n" ]
[ "2 1 1 4\n" ]
In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 1. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 1. Alex...
[ { "input": "4\n1 2 3 4\n4 1 3 2\n4 3 1 2\n3 4 2 1\n3 1 2 4", "output": "2 1 1 3" }, { "input": "2\n1 2\n2 1\n2 1", "output": "2 1" }, { "input": "3\n1 2 3\n2 3 1\n1 3 2\n3 2 1", "output": "2 3 1" }, { "input": "5\n1 4 2 3 5\n5 1 3 4 2\n3 2 4 1 5\n1 4 5 3 2\n5 2 3 4 1\n5 4 2 1...
186
4,710,400
3
40,139
976
Degree Set
[ "constructive algorithms", "graphs", "implementation" ]
null
null
You are given a sequence of *n* positive integers *d*1,<=*d*2,<=...,<=*d**n* (*d*1<=&lt;<=*d*2<=&lt;<=...<=&lt;<=*d**n*). Your task is to construct an undirected graph such that: - there are exactly *d**n*<=+<=1 vertices; - there are no self-loops; - there are no multiple edges; - there are no more than 106 edges;...
The first line contains one integer *n* (1<=≀<=*n*<=≀<=300) β€” the size of the degree set. The second line contains *n* integers *d*1,<=*d*2,<=...,<=*d**n* (1<=≀<=*d**i*<=≀<=1000, *d*1<=&lt;<=*d*2<=&lt;<=...<=&lt;<=*d**n*) β€” the degree set.
In the first line print one integer *m* (1<=≀<=*m*<=≀<=106) β€” the number of edges in the resulting graph. It is guaranteed that there exists such a graph that all the conditions hold and it contains no more than 106 edges. Each of the next *m* lines should contain two integers *v**i* and *u**i* (1<=≀<=*v**i*,<=*u**i*<...
[ "3\n2 3 4\n", "3\n1 2 3\n" ]
[ "8\n3 1\n4 2\n4 5\n2 5\n5 1\n3 2\n2 1\n5 3\n", "4\n1 2\n1 3\n1 4\n2 3\n" ]
none
[ { "input": "3\n2 3 4", "output": "8\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4" }, { "input": "3\n1 2 3", "output": "4\n1 2\n1 3\n1 4\n2 3" }, { "input": "4\n1 3 4 6", "output": "11\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 3\n2 4\n2 5\n3 4\n3 5" }, { "input": "1\n1", "output": "1\n1...
124
0
0
40,140
596
Wilbur and Strings
[ "dfs and similar", "dp", "graphs", "strings" ]
null
null
Wilbur the pig now wants to play with strings. He has found an *n* by *m* table consisting only of the digits from 0 to 9 where the rows are numbered 1 to *n* and the columns are numbered 1 to *m*. Wilbur starts at some square and makes certain moves. If he is at square (*x*, *y*) and the digit *d* (0<=≀<=*d*<=≀<=9) is...
The first line of the input consists of three integers *n*, *m*, and *q* (1<=≀<=*n*,<=*m*,<=*q*<=≀<=200)Β β€” the dimensions of the table and the number of strings to process, respectively. Each of the next *n* lines contains *m* digits from 0 and 9 giving the table itself. Then follow 10 lines. The *i*-th of them conta...
For each of the *q* strings, print "YES" if Wilbur can choose *x* and *y* in order to finish with this string after some finite number of moves. If it's impossible, than print "NO" for the corresponding string.
[ "1 1 2\n0\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n0000000000000\n2413423432432\n", "4 2 5\n01\n23\n45\n67\n0 1\n0 -1\n0 1\n0 -1\n0 1\n0 -1\n0 1\n0 -1\n0 1\n0 -1\n0000000000\n010101011101\n32232232322\n44343222342444324\n6767\n" ]
[ "YES\nNO\n", "YES\nYES\nYES\nNO\nYES\n" ]
In the first sample, there is a 1 by 1 table consisting of the only digit 0. The only move that can be made is staying on the square. The first string can be written on the white board by writing 0 repeatedly. The second string cannot be written as there is no 2 on the table.
[ { "input": "1 1 2\n0\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n0000000000000\n2413423432432", "output": "YES\nNO" }, { "input": "4 2 5\n01\n23\n45\n67\n0 1\n0 -1\n0 1\n0 -1\n0 1\n0 -1\n0 1\n0 -1\n0 1\n0 -1\n0000000000\n010101011101\n32232232322\n44343222342444324\n6767", "output": "YES\...
31
204,800
-1
40,224
675
Tree Construction
[ "data structures", "trees" ]
null
null
During the programming classes Vasya was assigned a difficult problem. However, he doesn't know how to code and was unable to find the solution in the Internet, so he asks you to help. You are given a sequence $a$, consisting of $n$ distinct integers, that is used to construct the binary search tree. Below is the form...
The first line of the input contains a single integer $n$ ($2 \leq n \leq 100\,000$)Β β€” the length of the sequence $a$. The second line contains $n$ distinct integers $a_i$ ($1 \leq a_i \leq 10^9$)Β β€” the sequence $a$ itself.
Output $n - 1$ integers. For all $i &gt; 1$ print the value written in the node that is the parent of the node with value $a_i$ in it.
[ "3\n1 2 3\n", "5\n4 2 3 1 6\n" ]
[ "1 2\n", "4 2 2 4\n" ]
none
[ { "input": "3\n1 2 3", "output": "1 2" }, { "input": "5\n4 2 3 1 6", "output": "4 2 2 4" }, { "input": "2\n1 2", "output": "1" }, { "input": "10\n991309218 517452607 870021923 978357992 136426010 10601767 302627526 883615372 163475700 600546765", "output": "991309218 5174...
46
4,608,000
0
40,303
551
GukiZ and Binary Operations
[ "combinatorics", "implementation", "math", "matrices", "number theory" ]
null
null
We all know that GukiZ often plays with arrays. Now he is thinking about this problem: how many arrays *a*, of length *n*, with non-negative elements strictly less then 2*l* meet the following condition: ? Here operation means bitwise AND (in Pascal it is equivalent to and, in C/C++/Java/Python it is equivalent to &...
First and the only line of input contains four integers *n*, *k*, *l*, *m* (2<=≀<=*n*<=≀<=1018, 0<=≀<=*k*<=≀<=1018, 0<=≀<=*l*<=≀<=64, 1<=≀<=*m*<=≀<=109<=+<=7).
In the single line print the number of arrays satisfying the condition above modulo *m*.
[ "2 1 2 10\n", "2 1 1 3\n", "3 3 2 10\n" ]
[ "3\n", "1\n", "9\n" ]
In the first sample, satisfying arrays are {1, 1}, {3, 1}, {1, 3}. In the second sample, only satisfying array is {1, 1}. In the third sample, satisfying arrays are {0, 3, 3}, {1, 3, 2}, {1, 3, 3}, {2, 3, 1}, {2, 3, 3}, {3, 3, 0}, {3, 3, 1}, {3, 3, 2}, {3, 3, 3}.
[ { "input": "2 1 2 10", "output": "3" }, { "input": "2 1 1 3", "output": "1" }, { "input": "3 3 2 10", "output": "9" }, { "input": "5135 42542 15 4354", "output": "0" }, { "input": "21 21 21 21", "output": "1" }, { "input": "2 0 0 5", "output": "1" ...
61
7,065,600
0
40,378
578
LCS Again
[ "dp", "greedy" ]
null
null
You are given a string *S* of length *n* with each character being one of the first *m* lowercase English letters. Calculate how many different strings *T* of length *n* composed from the first *m* lowercase English letters exist such that the length of LCS (longest common subsequence) between *S* and *T* is *n*<=-<=...
The first line contains two numbers *n* and *m* denoting the length of string *S* and number of first English lowercase characters forming the character set for strings (1<=≀<=*n*<=≀<=100<=000, 2<=≀<=*m*<=≀<=26). The second line contains string *S*.
Print the only line containing the answer.
[ "3 3\naaa\n", "3 3\naab\n", "1 2\na\n", "10 9\nabacadefgh\n" ]
[ "6\n", "11\n", "1\n", "789\n" ]
For the first sample, the 6 possible strings *T* are: aab, aac, aba, aca, baa, caa. For the second sample, the 11 possible strings *T* are: aaa, aac, aba, abb, abc, aca, acb, baa, bab, caa, cab. For the third sample, the only possible string *T* is b.
[ { "input": "3 3\naaa", "output": "6" }, { "input": "3 3\naab", "output": "11" }, { "input": "1 2\na", "output": "1" }, { "input": "10 9\nabacadefgh", "output": "789" }, { "input": "15 3\nabababababababa", "output": "345" }, { "input": "100 26\njysrixyp...
30
0
0
40,519
496
Distributing Parts
[ "greedy", "sortings" ]
null
null
You are an assistant director in a new musical play. The play consists of *n* musical parts, each part must be performed by exactly one actor. After the casting the director chose *m* actors who can take part in the play. Your task is to assign the parts to actors. However, there are several limitations. First, each a...
The first line contains a single integer *n*Β β€” the number of parts in the play (1<=≀<=*n*<=≀<=105). Next *n* lines contain two space-separated integers each, *a**j* and *b**j*Β β€” the range of notes for the *j*-th part (1<=≀<=*a**j*<=≀<=*b**j*<=≀<=109). The next line contains a single integer *m*Β β€” the number of actors...
If there is an assignment that meets all the criteria aboce, print a single word "YES" (without the quotes) in the first line. In the next line print *n* space-separated integers. The *i*-th integer should be the number of the actor who should perform the *i*-th part. If there are multiple correct assignments, print a...
[ "3\n1 3\n2 4\n3 5\n2\n1 4 2\n2 5 1\n", "3\n1 3\n2 4\n3 5\n2\n1 3 2\n2 5 1\n" ]
[ "YES\n1 1 2\n", "NO\n" ]
none
[]
46
0
0
40,579
542
Place Your Ad Here
[ "data structures", "sortings" ]
null
null
Ivan Anatolyevich's agency is starting to become famous in the town. They have already ordered and made *n* TV commercial videos. Each video is made in a special way: the colors and the soundtrack are adjusted to the time of the day and the viewers' mood. That's why the *i*-th video can only be shown within the time ...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=2Β·105)Β β€” the number of commercial videos and channels, respectively. Each of the following *n* lines contains two integers *l**i*, *r**i* (0<=≀<=*l**i*<=≀<=*r**i*<=≀<=109)Β β€” the segment of time when it is possible to show the corresponding video. E...
In the first line print an integer β€” the maximum possible efficiency of the broadcast. If there is no correct way to get a strictly positive efficiency, print a zero. If the maximum efficiency is strictly positive, in the second line also print the number of the video *i* (1<=≀<=*i*<=≀<=*n*) and the number of the TV c...
[ "2 3\n7 9\n1 4\n2 8 2\n0 4 1\n8 9 3\n", "1 1\n0 0\n1 1 10\n" ]
[ "4\n2 1\n", "0\n" ]
In the first sample test the most optimal solution is to show the second commercial using the first TV channel at time [2, 4]. The efficiency of such solution is equal to (4 - 2)Β·2 = 4. In the second sample test Ivan Anatolievich's wish does not meet the options of the TV channel, the segments do not intersect, so the...
[]
2,000
5,836,800
0
40,592
962
Simple Cycles Edges
[ "dfs and similar", "graphs", "trees" ]
null
null
You are given an undirected graph, consisting of $n$ vertices and $m$ edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself). A cycle in a graph is called a simple, if it conta...
The first line contain two integers $n$ and $m$ $(1 \le n \le 100\,000$, $0 \le m \le \min(n \cdot (n - 1) / 2, 100\,000))$ β€” the number of vertices and the number of edges. Each of the following $m$ lines contain two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) β€” the description of the edges.
In the first line print the number of edges, which belong to exactly one simple cycle. In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.
[ "3 3\n1 2\n2 3\n3 1\n", "6 7\n2 3\n3 4\n4 2\n1 2\n1 5\n5 6\n6 1\n", "5 6\n1 2\n2 3\n2 4\n4 3\n2 5\n5 3\n" ]
[ "3\n1 2 3 \n", "6\n1 2 3 5 6 7 \n", "0\n\n" ]
none
[ { "input": "3 3\n1 2\n2 3\n3 1", "output": "3\n1 2 3 " }, { "input": "6 7\n2 3\n3 4\n4 2\n1 2\n1 5\n5 6\n6 1", "output": "6\n1 2 3 5 6 7 " }, { "input": "5 6\n1 2\n2 3\n2 4\n4 3\n2 5\n5 3", "output": "0" }, { "input": "4 5\n1 2\n2 3\n3 4\n4 1\n1 3", "output": "0" }, {...
857
40,243,200
3
40,752
172
Calendar Reform
[ "*special", "number theory" ]
null
null
Reforms have started in Berland again! At this time, the Parliament is discussing the reform of the calendar. To make the lives of citizens of Berland more varied, it was decided to change the calendar. As more and more people are complaining that "the years fly by...", it was decided that starting from the next year t...
The only input line contains a pair of integers *a*, *n* (1<=≀<=*a*,<=*n*<=≀<=107; *a*<=+<=*n*<=-<=1<=≀<=107).
Print the required number *p*. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "25 3\n", "50 5\n" ]
[ "30\n", "125\n" ]
A note to the first sample test. A year of 25 days will consist of one month containing 25 days. A year of 26 days will consist of 26 months, one day each. A year of 27 days will have three months, 9 days each.
[ { "input": "25 3", "output": "30" }, { "input": "50 5", "output": "125" }, { "input": "1 1", "output": "1" }, { "input": "1 2", "output": "3" }, { "input": "1 10", "output": "38" }, { "input": "1 5000000", "output": "8224640917276" }, { "in...
156
0
3
40,792
215
Hot Days
[ "greedy" ]
null
null
The official capital and the cultural capital of Berland are connected by a single road running through *n* regions. Each region has a unique climate, so the *i*-th (1<=≀<=*i*<=≀<=*n*) region has a stable temperature of *t**i* degrees in summer. This summer a group of *m* schoolchildren wants to get from the official ...
The first input line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=105;Β 1<=≀<=*m*<=≀<=106) β€” the number of regions on the way and the number of schoolchildren in the group, correspondingly. Next *n* lines contain four integers each: the *i*-th line contains *t**i*, *T**i*, *x**i* and *cost**i* (1<=≀<=*t**i*,<=*T**i*...
Print the only integer β€” the minimum number of roubles the organizers will have to spend to transport all schoolchildren. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "2 10\n30 35 1 100\n20 35 10 10\n", "3 100\n10 30 1000 1\n5 10 1000 3\n10 40 1000 100000\n" ]
[ "120\n", "200065\n" ]
In the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will equal 30 + 10 = 40 degrees and each of 10 schoolchildren will ask for compensation. Only one bus will transport the group through the second region too, but the temperature inside won't ...
[ { "input": "2 10\n30 35 1 100\n20 35 10 10", "output": "120" }, { "input": "3 100\n10 30 1000 1\n5 10 1000 3\n10 40 1000 100000", "output": "200065" }, { "input": "10 1\n8 6 3 4\n9 10 7 7\n1 3 9 5\n10 9 4 2\n1 10 2 10\n1 1 8 5\n5 5 9 2\n5 8 4 3\n4 4 9 7\n5 7 5 10", "output": "88" }...
2,000
9,728,000
0
40,818
459
Pashmak and Parmida's problem
[ "data structures", "divide and conquer", "sortings" ]
null
null
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak. There is a sequence *a* that consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Let's denote *f*(*l*,<=*r*,<...
The first line of the input contains an integer *n* (1<=≀<=*n*<=≀<=106). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109).
Print a single integer β€” the answer to the problem.
[ "7\n1 2 1 1 2 2 1\n", "3\n1 1 1\n", "5\n1 2 3 4 5\n" ]
[ "8\n", "1\n", "0\n" ]
none
[ { "input": "7\n1 2 1 1 2 2 1", "output": "8" }, { "input": "3\n1 1 1", "output": "1" }, { "input": "5\n1 2 3 4 5", "output": "0" }, { "input": "24\n1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4", "output": "114" }, { "input": "1\n1", "output": "0" }, { ...
31
0
0
40,850
321
Ciel and Flipboard
[ "dp", "greedy", "math" ]
null
null
Fox Ciel has a board with *n* rows and *n* columns, there is one integer in each cell. It's known that *n* is an odd number, so let's introduce . Fox Ciel can do the following operation many times: she choose a sub-board with size *x* rows and *x* columns, then all numbers in it will be multiplied by -1. Return the m...
The first line contains an integer *n*, (1<=≀<=*n*<=≀<=33, and *n* is an odd integer) β€” the size of the board. Each of the next *n* lines contains *n* integers β€” the numbers in the board. Each number doesn't exceed 1000 by its absolute value.
Output a single integer: the maximal sum of numbers in the board that can be accomplished.
[ "3\n-1 -1 1\n-1 1 -1\n1 -1 -1\n", "5\n-2 0 0 0 -2\n0 -2 0 -2 0\n0 0 -2 0 0\n0 -2 0 -2 0\n-2 0 0 0 -2\n" ]
[ "9\n", "18\n" ]
In the first test, we can apply this operation twice: first on the top left 2 × 2 sub-board, then on the bottom right 2 × 2 sub-board. Then all numbers will become positive. <img class="tex-graphics" src="https://espresso.codeforces.com/c3426b4faf54a7f3b8dfc0e18e955f907760fe71.png" style="max-width: 100.0%;max-height:...
[]
280
1,433,600
0
41,083
632
Magic Matrix
[ "brute force", "divide and conquer", "graphs", "matrices", "trees" ]
null
null
You're given a matrix *A* of size *n*<=Γ—<=*n*. Let's call the matrix with nonnegative elements magic if it is symmetric (so *a**ij*<==<=*a**ji*), *a**ii*<==<=0 and *a**ij*<=≀<=*max*(*a**ik*,<=*a**jk*) for all triples *i*,<=*j*,<=*k*. Note that *i*,<=*j*,<=*k* do not need to be distinct. Determine if the matrix is mag...
The first line contains integer *n* (1<=≀<=*n*<=≀<=2500) β€” the size of the matrix *A*. Each of the next *n* lines contains *n* integers *a**ij* (0<=≀<=*a**ij*<=&lt;<=109) β€” the elements of the matrix *A*. Note that the given matrix not necessarily is symmetric and can be arbitrary.
Print ''MAGIC" (without quotes) if the given matrix *A* is magic. Otherwise print ''NOT MAGIC".
[ "3\n0 1 2\n1 0 2\n2 2 0\n", "2\n0 1\n2 3\n", "4\n0 1 2 3\n1 0 3 4\n2 3 0 5\n3 4 5 0\n" ]
[ "MAGIC\n", "NOT MAGIC\n", "NOT MAGIC\n" ]
none
[ { "input": "3\n0 1 2\n1 0 2\n2 2 0", "output": "MAGIC" }, { "input": "2\n0 1\n2 3", "output": "NOT MAGIC" }, { "input": "4\n0 1 2 3\n1 0 3 4\n2 3 0 5\n3 4 5 0", "output": "NOT MAGIC" }, { "input": "5\n0 2 5 9 5\n2 0 5 9 5\n5 5 0 9 4\n9 9 9 0 9\n5 5 4 9 0", "output": "MAGI...
61
0
0
41,126
818
Level Generation
[ "binary search", "math", "ternary search" ]
null
null
Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly *n**i* vertices in the graph representing level *i*, and the edges have to be bidirectional. ...
The first line of input file contains a positive integer *q* (1<=≀<=*q*<=≀<=100<=000) β€” the number of graphs Ivan needs to construct. Then *q* lines follow, *i*-th line contains one positive integer *n**i* (1<=≀<=*n**i*<=≀<=2Β·109) β€” the number of vertices in *i*-th graph. Note that in hacks you have to use *q*<==<=1.
Output *q* numbers, *i*-th of them must be equal to the maximum number of edges in *i*-th graph.
[ "3\n3\n4\n6\n" ]
[ "2\n3\n6\n" ]
In the first example it is possible to construct these graphs: 1. 1 - 2, 1 - 3; 1. 1 - 2, 1 - 3, 2 - 4; 1. 1 - 2, 1 - 3, 2 - 3, 1 - 4, 2 - 5, 3 - 6.
[ { "input": "3\n3\n4\n6", "output": "2\n3\n6" }, { "input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10", "output": "0\n1\n2\n3\n4\n6\n7\n8\n10\n12" }, { "input": "1\n212055293", "output": "424069398" }, { "input": "1\n508427854", "output": "1016791932" }, { "input": "1\n39...
998
6,963,200
3
41,134
989
A Shade of Moonlight
[ "binary search", "geometry", "math", "sortings", "two pointers" ]
null
null
"To curtain off the moonlight should be hardly possible; the shades present its mellow beauty and restful nature." Intonates Mino. "See? The clouds are coming." Kanno gazes into the distance. "That can't be better," Mino turns to Kanno. The sky can be seen as a one-dimensional axis. The moon is at the origin whose ...
The first line contains three space-separated integers $n$, $l$, and $w_\mathrm{max}$ ($1 \leq n \leq 10^5$, $1 \leq l, w_\mathrm{max} \leq 10^8$)Β β€” the number of clouds, the length of each cloud and the maximum wind speed, respectively. The $i$-th of the following $n$ lines contains two space-separated integers $x_i$...
Output one integerΒ β€” the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity $w$.
[ "5 1 2\n-2 1\n2 1\n3 -1\n5 -1\n7 -1\n", "4 10 1\n-20 1\n-10 -1\n0 1\n10 -1\n" ]
[ "4\n", "1\n" ]
In the first example, the initial positions and velocities of clouds are illustrated below. The pairs are: - $(1, 3)$, covering the moon at time $2.5$ with $w = -0.4$; - $(1, 4)$, covering the moon at time $3.5$ with $w = -0.6$; - $(1, 5)$, covering the moon at time $4.5$ with $w = -0.7$; - $(2, 5)$, covering th...
[ { "input": "5 1 2\n-2 1\n2 1\n3 -1\n5 -1\n7 -1", "output": "4" }, { "input": "4 10 1\n-20 1\n-10 -1\n0 1\n10 -1", "output": "1" }, { "input": "1 100000000 98765432\n73740702 1", "output": "0" }, { "input": "10 2 3\n-1 -1\n-4 1\n-6 -1\n1 1\n10 -1\n-8 -1\n6 1\n8 1\n4 -1\n-10 -1...
841
6,860,800
3
41,189
160
Unlucky Ticket
[ "greedy", "sortings" ]
null
null
Each of you probably has your personal experience of riding public transportation and buying tickets. After a person buys a ticket (which traditionally has an even number of digits), he usually checks whether the ticket is lucky. Let us remind you that a ticket is lucky if the sum of digits in its first half matches th...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=100). The second line contains a string that consists of 2*n* digits and defines your ticket.
In the first line print "YES" if the ticket meets the unluckiness criterion. Otherwise, print "NO" (without the quotes).
[ "2\n2421\n", "2\n0135\n", "2\n3754\n" ]
[ "YES\n", "YES\n", "NO\n" ]
none
[ { "input": "2\n2421", "output": "YES" }, { "input": "2\n0135", "output": "YES" }, { "input": "2\n3754", "output": "NO" }, { "input": "1\n33", "output": "NO" }, { "input": "2\n3444", "output": "NO" }, { "input": "3\n221323", "output": "YES" }, {...
92
0
0
41,389
626
Jerry's Protest
[ "brute force", "combinatorics", "dp", "probabilities" ]
null
null
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing *n* balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to...
The first line of input contains a single integer *n* (2<=≀<=*n*<=≀<=2000) β€” the number of balls in the jar. The second line contains *n* integers *a**i* (1<=≀<=*a**i*<=≀<=5000) β€” the number written on the *i*th ball. It is guaranteed that no two balls have the same number.
Print a single real value β€” the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury i...
[ "2\n1 2\n", "3\n1 2 10\n" ]
[ "0.0000000000\n", "0.0740740741\n" ]
In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes β€” 10 - 2, 10 ...
[ { "input": "2\n1 2", "output": "0.0000000000" }, { "input": "3\n1 2 10", "output": "0.0740740741" }, { "input": "3\n1 2 3", "output": "0.0000000000" }, { "input": "4\n2 4 1 3", "output": "0.0416666667" }, { "input": "11\n1 2 4 8 16 32 64 128 256 512 1024", "ou...
1,403
7,270,400
3
41,478
111
Petya and Coloring
[ "combinatorics", "dp" ]
D. Petya and Coloring
5
256
Little Petya loves counting. He wants to count the number of ways to paint a rectangular checkered board of size *n*<=Γ—<=*m* (*n* rows, *m* columns) in *k* colors. Besides, the coloring should have the following property: for any vertical line that passes along the grid lines and divides the board in two non-empty part...
The first line contains space-separated integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=1000,<=1<=≀<=*k*<=≀<=106) β€” the board's vertical and horizontal sizes and the number of colors respectively.
Print the answer to the problem. As the answer can be quite a large number, you should print it modulo 109<=+<=7 (1000000007).
[ "2 2 1\n", "2 2 2\n", "3 2 2\n" ]
[ "1\n", "8\n", "40\n" ]
none
[ { "input": "2 2 1", "output": "1" }, { "input": "2 2 2", "output": "8" }, { "input": "3 2 2", "output": "40" }, { "input": "7 8 15", "output": "422409918" }, { "input": "5 3 1", "output": "1" }, { "input": "5 100 1", "output": "1" }, { "inp...
4,242
15,155,200
3.547571
41,553
757
Felicity is Coming!
[ "data structures", "hashing", "sortings", "strings" ]
null
null
It's that time of the year, Felicity is around the corner and you can see people celebrating all around the Himalayan region. The Himalayan region has *n* gyms. The *i*-th gym has *g**i* Pokemon in it. There are *m* distinct Pokemon types in the Himalayan region numbered from 1 to *m*. There is a special evolution camp...
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=105, 1<=≀<=*m*<=≀<=106)Β β€” the number of gyms and the number of Pokemon types. The next *n* lines contain the description of Pokemons in the gyms. The *i*-th of these lines begins with the integer *g**i* (1<=≀<=*g**i*<=≀<=105)Β β€” the number of Pokemon in th...
Output the number of valid evolution plans modulo 109<=+<=7.
[ "2 3\n2 1 2\n2 2 3\n", "1 3\n3 1 2 3\n", "2 4\n2 1 2\n3 2 3 4\n", "2 2\n3 2 2 1\n2 1 2\n", "3 7\n2 1 2\n2 3 4\n3 5 6 7\n" ]
[ "1\n", "6\n", "2\n", "1\n", "24\n" ]
In the first case, the only possible evolution plan is: In the second case, any permutation of (1,  2,  3) is valid. In the third case, there are two possible plans: In the fourth case, the only possible evolution plan is:
[ { "input": "2 3\n2 1 2\n2 2 3", "output": "1" }, { "input": "1 3\n3 1 2 3", "output": "6" }, { "input": "2 4\n2 1 2\n3 2 3 4", "output": "2" }, { "input": "2 2\n3 2 2 1\n2 1 2", "output": "1" }, { "input": "3 7\n2 1 2\n2 3 4\n3 5 6 7", "output": "24" }, { ...
77
4,608,000
0
41,557
327
Block Tower
[ "constructive algorithms", "dfs and similar", "graphs" ]
null
null
After too much playing on paper, Iahub has switched to computer games. The game he plays is called "Block Towers". It is played in a rectangular grid with *n* rows and *m* columns (it contains *n*<=Γ—<=*m* cells). The goal of the game is to build your own city. Some cells in the grid are big holes, where Iahub can't bui...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=500). Each of the next *n* lines contains *m* characters, describing the grid. The *j*-th character in the *i*-th line is '.' if you're allowed to build at the cell with coordinates (*i*,<=*j*) a tower (empty cell) or '#' if there is a b...
Print an integer *k* in the first line (0<=≀<=*k*<=≀<=106) β€” the number of operations Iahub should perform to obtain optimal result. Each of the following *k* lines must contain a single operation in the following format: 1. Β«B x yΒ» (1<=≀<=*x*<=≀<=*n*,<=1<=≀<=*y*<=≀<=*m*) β€” building a blue tower at the cell (*x*,<=*...
[ "2 3\n..#\n.#.\n", "1 3\n...\n" ]
[ "4\nB 1 1\nR 1 2\nR 2 1\nB 2 3\n", "5\nB 1 1\nB 1 2\nR 1 3\nD 1 2\nR 1 2\n" ]
none
[]
124
0
0
41,591
54
Cutting Jigsaw Puzzle
[ "hashing", "implementation" ]
B. Cutting Jigsaw Puzzle
2
256
The Hedgehog recently remembered one of his favorite childhood activities, β€” solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one. Soon the Hedgehog came up with a brilliant idea: ins...
The first line contains two numbers *A* and *B* which are the sizes of the picture. They are positive integers not exceeding 20. Then follow *A* lines containing *B* symbols each, describing the actual picture. The lines only contain uppercase English letters.
In the first line print the number of possible good puzzles (in other words, the number of pairs (*X*,<=*Y*) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself. In the second line print two numbers β€” the siz...
[ "2 4\nABDC\nABDC\n", "2 6\nABCCBA\nABCCBA\n" ]
[ "3\n2 1\n", "1\n2 6\n" ]
The picture in the first sample test has the following good puzzles: (2, 1), (2, 2), (2, 4).
[ { "input": "2 4\nABDC\nABDC", "output": "3\n2 1" }, { "input": "2 6\nABCCBA\nABCCBA", "output": "1\n2 6" }, { "input": "2 2\nAB\nCD", "output": "4\n1 1" }, { "input": "4 6\nABABAC\nBABABC\nABABAC\nCCCCCA", "output": "4\n2 3" }, { "input": "1 12\nABAAADCAAABX", ...
62
0
-1
41,609
141
Queue
[ "constructive algorithms", "greedy", "sortings" ]
null
null
In the Main Berland Bank *n* people stand in a queue at the cashier, everyone knows his/her height *h**i*, and the heights of the other people in the queue. Each of them keeps in mind number *a**i* β€” how many people who are taller than him/her and stand in queue in front of him. After a while the cashier has a lunch b...
The first input line contains integer *n* β€” the number of people in the queue (1<=≀<=*n*<=≀<=3000). Then *n* lines contain descriptions of the people as "*name**i* *a**i*" (one description on one line), where *name**i* is a non-empty string consisting of lowercase Latin letters whose length does not exceed 10 character...
If there's no acceptable order of the people in the queue, print the single line containing "-1" without the quotes. Otherwise, print in *n* lines the people as "*name**i* *h**i*", where *h**i* is the integer from 1 to 109 (inclusive), the possible height of a man whose name is *name**i*. Print the people in the order ...
[ "4\na 0\nb 2\nc 0\nd 0\n", "4\nvasya 0\npetya 1\nmanya 3\ndunay 3\n" ]
[ "a 150\nc 170\nd 180\nb 160\n", "-1\n" ]
none
[ { "input": "4\na 0\nb 2\nc 0\nd 0", "output": "a 150\nc 170\nd 180\nb 160" }, { "input": "4\nvasya 0\npetya 1\nmanya 3\ndunay 3", "output": "-1" }, { "input": "1\nze 0", "output": "ze 150" }, { "input": "3\nl 0\nmybiqxmnqq 0\nsoxkyvgd 0", "output": "l 150\nmybiqxmnqq 160\...
30
0
0
41,611
0
none
[ "none" ]
null
null
Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them). Polycarpus wants to save these pictures o...
The first line contains two integers *l**a* and *l**b* (1<=≀<=*l**a*,<=*l**b*<=≀<=1000000) β€” the number of hieroglyphs in the first and second circles, respectively. Below, due to difficulties with encoding of Berland hieroglyphs, they are given as integers from 1 to 106. The second line contains *l**a* integers β€” th...
Print a single number β€” the maximum length of the common substring and subsequence. If at any way of breaking the circles it does not exist, print 0.
[ "5 4\n1 2 3 4 5\n1 3 5 6\n", "4 6\n1 3 5 2\n1 2 3 4 5 6\n", "3 3\n1 2 3\n3 2 1\n" ]
[ "2\n", "3\n", "2\n" ]
In the first test Polycarpus picks a string that consists of hieroglyphs 5 and 1, and in the second sample β€” from hieroglyphs 1, 3 and 5.
[ { "input": "5 4\n1 2 3 4 5\n1 3 5 6", "output": "2" }, { "input": "4 6\n1 3 5 2\n1 2 3 4 5 6", "output": "3" }, { "input": "3 3\n1 2 3\n3 2 1", "output": "2" }, { "input": "6 3\n1 2 3 4 5 6\n3 5 1", "output": "1" }, { "input": "1 1\n1\n2", "output": "0" }, ...
62
0
0
41,650
120
Minimum Sum
[ "divide and conquer", "geometry", "sortings" ]
null
null
You are given a set of *n* vectors on a plane. For each vector you are allowed to multiply any of its coordinates by -1. Thus, each vector *v**i*<==<=(*x**i*,<=*y**i*) can be transformed into one of the following four vectors: - *v**i*1<==<=(*x**i*,<=*y**i*), - *v**i*2<==<=(<=-<=*x**i*,<=*y**i*), - *v**i*3<==<=(*x*...
The first line contains a single integer *n* (2<=≀<=*n*<=≀<=105). Then *n* lines contain vectors as pairs of integers "*x**i* *y**i*" (<=-<=10000<=≀<=*x**i*,<=*y**i*<=≀<=10000), one pair per line.
Print on the first line four space-separated numbers "*i* *k*1 *j* *k*2" β€” the answer to the problem. If there are several variants the absolute value of whose sums is minimum, you can print any of them.
[ "5\n-7 -3\n9 0\n-8 6\n7 -8\n4 -5\n", "5\n3 2\n-4 7\n-6 0\n-8 4\n5 1\n" ]
[ "3 2 4 2\n", "3 4 5 4\n" ]
A sum of two vectors *v* = (*x*<sub class="lower-index">*v*</sub>, *y*<sub class="lower-index">*v*</sub>) and *u* = (*x*<sub class="lower-index">*u*</sub>, *y*<sub class="lower-index">*u*</sub>) is vector *s* = *v* + *u* = (*x*<sub class="lower-index">*v*</sub> + *x*<sub class="lower-index">*u*</sub>, *y*<sub class="lo...
[ { "input": "5\n-7 -3\n9 0\n-8 6\n7 -8\n4 -5", "output": "3 2 4 2" }, { "input": "5\n3 2\n-4 7\n-6 0\n-8 4\n5 1", "output": "3 4 5 4" }, { "input": "2\n-4 -2\n-2 1", "output": "1 4 2 3" }, { "input": "3\n-4 -3\n-4 -5\n-1 0", "output": "2 4 1 1" }, { "input": "10\n5...
216
2,355,200
-1
41,797
62
Tyndex.Brome
[ "binary search", "implementation" ]
B. Tyndex.Brome
2
256
Tyndex is again well ahead of the rivals! The reaction to the release of Zoozle Chrome browser was the release of a new browser Tyndex.Brome! The popularity of the new browser is growing daily. And the secret is not even the Tyndex.Bar installed (the Tyndex.Bar automatically fills the glass with the finest 1664 cognac...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*k*<=≀<=105). They are the number of potential addresses and the length of the address entered by the user. The next line contains *k* lowercase Latin letters. They are the address entered by the user (*s*). Each next *i*-th (1<=≀<=*i*<=≀<=*n*)...
On each *n* line of the output file print a single number: the value of the error function when the current potential address is chosen. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "2 10\ncodeforces\ncodeforces\ncodehorses\n", "9 9\nvkontakte\nvcontacte\nvkontrakte\nvkollapse\nvkrokodile\nvtopke\nvkapuste\nvpechke\nvk\nvcodeforcese\n" ]
[ "0\n12\n", "18\n14\n36\n47\n14\n29\n30\n0\n84\n" ]
none
[ { "input": "2 10\ncodeforces\ncodeforces\ncodehorses", "output": "0\n12" }, { "input": "9 9\nvkontakte\nvcontacte\nvkontrakte\nvkollapse\nvkrokodile\nvtopke\nvkapuste\nvpechke\nvk\nvcodeforcese", "output": "18\n14\n36\n47\n14\n29\n30\n0\n84" }, { "input": "3 3\nbyg\ndwg\nl\nx", "outp...
1,714
4,710,400
3.562726
41,876
226
Anniversary
[ "data structures", "implementation", "math", "matrices", "number theory" ]
null
null
There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathematician Leonardo Fibonacci. Of course, such important anniversary needs much preparations. Dima is sure that it'll be great to learn to solve the following problem by the Big Day: You're given a set *A*, consisting of numb...
The first line contains four space-separated integers *m*, *l*, *r* and *k* (1<=≀<=*m*<=≀<=109;Β 1<=≀<=*l*<=&lt;<=*r*<=≀<=1012;Β 2<=≀<=*k*<=≀<=*r*<=-<=*l*<=+<=1). Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
Print a single integer β€” the residue from dividing the sought greatest common divisor by *m*.
[ "10 1 8 2\n", "10 1 8 3\n" ]
[ "3\n", "1\n" ]
none
[ { "input": "10 1 8 2", "output": "3" }, { "input": "10 1 8 3", "output": "1" }, { "input": "10 1 20 2", "output": "5" }, { "input": "4 1 10 2", "output": "1" }, { "input": "1000000000 999000000000 1000000000000 1000000001", "output": "1" }, { "input": ...
248
1,843,200
3
41,972
709
Checkpoints
[ "greedy", "implementation", "sortings" ]
null
null
Vasya takes part in the orienteering competition. There are *n* checkpoints located along the line at coordinates *x*1,<=*x*2,<=...,<=*x**n*. Vasya starts at the point with coordinate *a*. His goal is to visit at least *n*<=-<=1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints...
The first line of the input contains two integers *n* and *a* (1<=≀<=*n*<=≀<=100<=000, <=-<=1<=000<=000<=≀<=*a*<=≀<=1<=000<=000)Β β€” the number of checkpoints and Vasya's starting position respectively. The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=1<=000<=000<=≀<=*x**i*<=≀<=1<=000<=000)Β β€” coord...
Print one integerΒ β€” the minimum distance Vasya has to travel in order to visit at least *n*<=-<=1 checkpoint.
[ "3 10\n1 7 12\n", "2 0\n11 -10\n", "5 0\n0 0 1000 0 0\n" ]
[ "7\n", "10\n", "0\n" ]
In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7. In the second sample it's enough to visit only one check...
[ { "input": "3 10\n1 7 12", "output": "7" }, { "input": "2 0\n11 -10", "output": "10" }, { "input": "5 0\n0 0 1000 0 0", "output": "0" }, { "input": "1 0\n0", "output": "0" }, { "input": "2 1\n4 -8", "output": "3" }, { "input": "3 4\n4 2 4", "output...
202
31,334,400
3
42,038
534
Berland Local Positioning System
[ "constructive algorithms", "greedy", "hashing", "implementation" ]
null
null
In Berland a bus travels along the main street of the capital. The street begins from the main square and looks like a very long segment. There are *n* bus stops located along the street, the *i*-th of them is located at the distance *a**i* from the central square, all distances are distinct, the stops are numbered in ...
The first line contains integer *n* (2<=≀<=*n*<=≀<=2Β·105) β€” the number of stops. The second line contains *n* integers (1<=≀<=*a**i*<=≀<=109) β€” the distance from the *i*-th stop to the central square. The numbers in the second line go in the increasing order. The third line contains integer *m* (1<=≀<=*m*<=≀<=4Β·105) ...
In the single line please print the distance covered by a bus. If it is impossible to determine it unambiguously, print <=-<=1.
[ "6\n2 3 5 7 11 13\n5\n3 4 5 5 6\n", "6\n2 3 5 7 11 13\n9\n1 2 2 3 3 4 5 5 6\n", "3\n10 200 300\n4\n1 2 2 3\n", "3\n1 2 3\n4\n1 2 2 3\n" ]
[ "10\n", "16\n", "-1\n", "3\n" ]
The first test from the statement demonstrates the first example shown in the statement of the problem. The second test from the statement demonstrates the second example shown in the statement of the problem. In the third sample there are two possible paths that have distinct lengths, consequently, the sought length...
[ { "input": "6\n2 3 5 7 11 13\n5\n3 4 5 5 6", "output": "10" }, { "input": "6\n2 3 5 7 11 13\n9\n1 2 2 3 3 4 5 5 6", "output": "16" }, { "input": "3\n10 200 300\n4\n1 2 2 3", "output": "-1" }, { "input": "3\n1 2 3\n4\n1 2 2 3", "output": "3" }, { "input": "2\n1 100...
31
0
0
42,043
954
Yet Another String Matching Problem
[ "fft", "math" ]
null
null
Suppose you have two strings *s* and *t*, and their length is equal. You may perform the following operation any number of times: choose two different characters *c*1 and *c*2, and replace every occurence of *c*1 in both strings with *c*2. Let's denote the distance between strings *s* and *t* as the minimum number of o...
The first line contains the string *S*, and the second β€” the string *T* (1<=≀<=|*T*|<=≀<=|*S*|<=≀<=125000). Both strings consist of lowercase Latin letters from a to f.
Print |*S*|<=-<=|*T*|<=+<=1 integers. The *i*-th of these integers must be equal to the distance between the substring of *S* beginning at *i*-th index with length |*T*| and the string *T*.
[ "abcdefa\nddcb\n" ]
[ "2 3 3 3 \n" ]
none
[ { "input": "abcdefa\nddcb", "output": "2 3 3 3 " } ]
30
0
0
42,190
0
none
[ "none" ]
null
null
Felix the Robot is preparing for a probability theory exam. Unfortunately, during the semester, he took a course of the belles-lettres instead of studying the subject, so now he does not know the answer to any of the upcoming exam's questions. One thing is for sure: Felix needs help! The exam for robots is an online e...
none
none
[]
[]
none
[]
295
5,836,800
-1
42,195
39
What Has Dirichlet Got to Do with That?
[ "dp", "games" ]
E. What Has Dirichlet Got to Do with That?
2
64
You all know the Dirichlet principle, the point of which is that if *n* boxes have no less than *n*<=+<=1 items, that leads to the existence of a box in which there are at least two items. Having heard of that principle, but having not mastered the technique of logical thinking, 8 year olds Stas and Masha invented a g...
The only input line has three integers *a*,<=*b*,<=*n* (1<=≀<=*a*<=≀<=10000, 1<=≀<=*b*<=≀<=30, 2<=≀<=*n*<=≀<=109) β€” the initial number of the boxes, the number of the items and the number which constrains the number of ways, respectively. Guaranteed that the initial number of ways is strictly less than *n*.
Output "Stas" if Masha wins. Output "Masha" if Stas wins. In case of a draw, output "Missing".
[ "2 2 10\n", "5 5 16808\n", "3 1 4\n", "1 4 10\n" ]
[ "Masha\n", "Masha\n", "Stas\n", "Missing\n" ]
In the second example the initial number of ways is equal to 3125. - If Stas increases the number of boxes, he will lose, as Masha may increase the number of boxes once more during her turn. After that any Stas's move will lead to defeat. - But if Stas increases the number of items, then any Masha's move will be lo...
[ { "input": "2 2 10", "output": "Masha" }, { "input": "5 5 16808", "output": "Masha" }, { "input": "3 1 4", "output": "Stas" }, { "input": "1 4 10", "output": "Missing" }, { "input": "1 1 2", "output": "Missing" }, { "input": "1 1 3", "output": "Mas...
92
0
0
42,270
0
none
[ "none" ]
null
null
In some country there are exactly *n* cities and *m* bidirectional roads connecting the cities. Cities are numbered with integers from 1 to *n*. If cities *a* and *b* are connected by a road, then in an hour you can go along this road either from city *a* to city *b*, or from city *b* to city *a*. The road network is s...
The first line contains two integers *n*, *m* (1<=≀<=*n*<=≀<=3000, )Β β€” the number of cities and roads in the country, respectively. Next *m* lines contain the descriptions of the roads as pairs of integers *a**i*, *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*, *a**i*<=β‰ <=*b**i*). It is guaranteed that the roads that are give...
Print a single number β€” the answer to the problem. If the it is impossible to meet the conditions, print -1.
[ "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 2\n", "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n2 4 2\n", "5 4\n1 2\n2 3\n3 4\n4 5\n1 3 2\n3 5 1\n" ]
[ "0\n", "1\n", "-1\n" ]
none
[]
46
0
0
42,312
733
Epidemic in Monstropolis
[ "constructive algorithms", "dp", "greedy", "two pointers" ]
null
null
There was an epidemic in Monstropolis and all monsters became sick. To recover, all monsters lined up in queue for an appointment to the only doctor in the city. Soon, monsters became hungry and began to eat each other. One monster can eat other monster if its weight is strictly greater than the weight of the monste...
The first line contains single integer *n* (1<=≀<=*n*<=≀<=500)Β β€” the number of monsters in the initial queue. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106)Β β€” the initial weights of the monsters. The third line contains single integer *k* (1<=≀<=*k*<=≀<=*n*)Β β€” the number of mo...
In case if no actions could lead to the final queue, print "NO" (without quotes) in the only line. Otherwise print "YES" (without quotes) in the first line. In the next *n*<=-<=*k* lines print actions in the chronological order. In each line print *x*Β β€” the index number of the monster in the current queue which eats ...
[ "6\n1 2 2 2 1 2\n2\n5 5\n", "5\n1 2 3 4 5\n1\n15\n", "5\n1 1 1 3 3\n3\n2 1 6\n" ]
[ "YES\n2 L\n1 R\n4 L\n3 L\n", "YES\n5 L\n4 L\n3 L\n2 L\n", "NO" ]
In the first example, initially there were *n* = 6 monsters, their weights are [1, 2, 2, 2, 1, 2] (in order of queue from the first monster to the last monster). The final queue should be [5, 5]. The following sequence of eatings leads to the final queue: - the second monster eats the monster to the left (i.e. the fi...
[ { "input": "6\n1 2 2 2 1 2\n2\n5 5", "output": "YES\n2 L\n1 R\n4 L\n3 L" }, { "input": "5\n1 2 3 4 5\n1\n15", "output": "YES\n5 L\n4 L\n3 L\n2 L" }, { "input": "5\n1 1 1 3 3\n3\n2 1 6", "output": "NO" }, { "input": "5\n1 1 1 1 2\n3\n1 1 4", "output": "YES\n5 L\n4 L" }, ...
46
0
3
42,445
317
Game with Powers
[ "dp", "games" ]
null
null
Vasya and Petya wrote down all integers from 1 to *n* to play the "powers" game (*n* can be quite large; however, Vasya and Petya are not confused by this fact). Players choose numbers in turn (Vasya chooses first). If some number *x* is chosen at the current turn, it is forbidden to choose *x* or all of its other pos...
Input contains single integer *n* (1<=≀<=*n*<=≀<=109).
Print the name of the winner β€” "Vasya" or "Petya" (without quotes).
[ "1\n", "2\n", "8\n" ]
[ "Vasya\n", "Petya\n", "Petya\n" ]
In the first sample Vasya will choose 1 and win immediately. In the second sample no matter which number Vasya chooses during his first turn, Petya can choose the remaining number and win.
[ { "input": "1", "output": "Vasya" }, { "input": "2", "output": "Petya" }, { "input": "8", "output": "Petya" }, { "input": "52", "output": "Petya" }, { "input": "53", "output": "Vasya" }, { "input": "3", "output": "Vasya" }, { "input": "4", ...
310
0
0
42,476
852
Digits
[ "brute force", "implementation", "math" ]
null
null
John gave Jack a very hard problem. He wrote a very big positive integer *A*0 on a piece of paper. The number is less than 10200000 . In each step, Jack is allowed to put '<=+<=' signs in between some of the digits (maybe none) of the current number and calculate the sum of the expression. He can perform the same proce...
First line contains a positive integer *N* (1<=≀<=*N*<=≀<=200000), representing the number of digits of *A*0. Second line contains a string of length *N* representing positive integer number *A*0. Each character is digit. There will be no leading zeros.
Output exactly three lines, the steps Jack needs to perform to solve the problem. You can output any sequence of steps which results in a single digit number (and is logically consistent). Every step consists of digits and '<=+<=' signs. Steps should not contain several '<=+<=' signs in a row, whitespaces, or '<=+<=' ...
[ "1\n1\n", "4\n5806\n" ]
[ "1\n1\n1\n", "5+8+0+6\n1+9\n1+0\n" ]
In the first sample, Jack can't put ' + ' signs anywhere, so he just writes 1 in each line and solves the problem. Here, solution is unique. In the second sample, Jack first puts ' + ' between every two consecutive digits, thus getting the result 5 + 8 + 0 + 6 = 19. He does the same on the second step, getting 1 + 9 =...
[]
1,000
1,843,200
0
42,494
0
none
[ "none" ]
null
null
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 bo...
The first line of input contains two integers *n* and *m*Β (1<=≀<=*n*,<=*m*<=≀<=105). The next line contains *m* integers separated by spaces: *a*1,<=*a*2,<=...,<=*a**m* (1<=≀<=*a**i*<=≀<=*n*).
Print a single integer β€” the minimum number of pages Ryouko needs to turn.
[ "4 6\n1 2 3 4 3 2\n", "10 5\n9 4 3 8 8\n" ]
[ "3\n", "6\n" ]
In the first sample, the optimal solution is to merge page 4 to 3, after merging sequence *a* becomes {1, 2, 3, 3, 3, 2}, so the number of pages Ryouko needs to turn is |1 - 2| + |2 - 3| + |3 - 3| + |3 - 3| + |3 - 2| = 3. In the second sample, optimal solution is achieved by merging page 9 to 4.
[ { "input": "4 6\n1 2 3 4 3 2", "output": "3" }, { "input": "10 5\n9 4 3 8 8", "output": "6" }, { "input": "5 10\n2 5 2 2 3 5 3 2 1 3", "output": "7" }, { "input": "10 20\n6 3 9 6 1 9 1 9 8 2 7 6 9 8 4 7 1 2 4 2", "output": "52" }, { "input": "100 100\n28 28 28 28 ...
62
0
0
42,792
375
Red and Black Tree
[ "dp", "implementation", "math" ]
null
null
You have a weighted tree, consisting of *n* vertices. Each vertex is either painted black or is painted red. A red and black tree is called beautiful, if for any its vertex we can find a black vertex at distance at most *x*. The distance between two nodes is the shortest path between them. You have a red and black tr...
The first line contains two integers *n* and *x* (2<=≀<=*n*<=≀<=500;Β 1<=≀<=*x*<=≀<=109). The next line contains *n* integers, each of them is either a zero or one. If the *i*-th number equals 1, then vertex *i* of the tree is black, otherwise vertex *i* is red. Next *n*<=-<=1 lines contain the tree edges. The *j*-th li...
Print a single integer β€” the minimum number of required swap operations. If it is impossible to get a beautiful tree at any number of operations, print -1.
[ "3 2\n1 0 0\n1 2 2\n2 3 2\n", "4 2\n0 1 0 0\n1 2 2\n2 3 2\n3 4 2\n" ]
[ "1\n", "-1\n" ]
none
[]
46
0
0
42,820
0
none
[ "none" ]
null
null
Mr. Kitayuta's garden is planted with *n* bamboos. (Bamboos are tall, fast-growing tropical plants with hollow stems.) At the moment, the height of the *i*-th bamboo is *h**i* meters, and it grows *a**i* meters at the end of each day. Actually, Mr. Kitayuta hates these bamboos. He once attempted to cut them down, but...
The first line of the input contains four space-separated integers *n*, *m*, *k* and *p* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*m*<=≀<=5000,<=1<=≀<=*k*<=≀<=10,<=1<=≀<=*p*<=≀<=109). They represent the number of the bamboos in Mr. Kitayuta's garden, the duration of Mr. Kitayuta's fight in days, the maximum number of times that Mr. ...
Print the lowest possible height of the tallest bamboo after *m* days.
[ "3 1 2 5\n10 10\n10 10\n15 2\n", "2 10 10 1000000000\n0 10\n0 10\n", "5 3 3 10\n9 5\n9 2\n4 7\n9 10\n3 8\n" ]
[ "17\n", "10\n", "14\n" ]
none
[]
46
0
0
42,914
804
Ice cream coloring
[ "constructive algorithms", "dfs and similar", "greedy" ]
null
null
Isart and Modsart were trying to solve an interesting problem when suddenly Kasra arrived. Breathless, he asked: "Can you solve a problem I'm stuck at all day?" We have a tree *T* with *n* vertices and *m* types of ice cream numerated from 1 to *m*. Each vertex *i* has a set of *s**i* types of ice cream. Vertices whic...
The first line contains two integer *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=3Β·105)Β β€” the number of vertices in *T* and the number of ice cream types. *n* lines follow, the *i*-th of these lines contain single integer *s**i* (0<=≀<=*s**i*<=≀<=3Β·105) and then *s**i* distinct integers, each between 1 and *m*Β β€” the types of ice ...
Print single integer *c* in the first lineΒ β€” the minimum number of colors to paint the vertices in graph *G*. In the second line print *m* integers, the *i*-th of which should be the color of the *i*-th vertex. The colors should be between 1 and *c*. If there are some answers, print any of them.
[ "3 3\n1 1\n2 2 3\n1 2\n1 2\n2 3\n", "4 5\n0\n1 1\n1 3\n3 2 4 5\n2 1\n3 2\n4 3\n" ]
[ "2\n1 1 2 ", "3\n1 1 1 2 3 " ]
In the first example the first type of ice cream is present in the first vertex only, so we can color it in any color. The second and the third ice cream are both presented in the second vertex, so we should paint them in different colors. In the second example the colors of the second, the fourth and the fifth ice cr...
[ { "input": "3 3\n1 1\n2 2 3\n1 2\n1 2\n2 3", "output": "2\n1 1 2 " }, { "input": "4 5\n0\n1 1\n1 3\n3 2 4 5\n2 1\n3 2\n4 3", "output": "3\n1 1 1 2 3 " }, { "input": "7 35\n3 17 20 32\n4 3 14 24 25\n4 4 10 17 26\n7 2 9 13 17 23 28 30\n9 1 2 7 8 13 16 18 33 35\n8 5 6 11 15 17 22 29 34\n5 1...
2,000
52,838,400
0
43,060
988
Rain and Umbrellas
[ "dp" ]
null
null
Polycarp lives on a coordinate line at the point $x = 0$. He goes to his friend that lives at the point $x = a$. Polycarp can move only from left to right, he can pass one unit of length each second. Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $n$ non-intersecting segments,...
The first line contains three integers $a$, $n$ and $m$ ($1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$) β€” the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $n$ lines contains two integers $l_i$ and $r_i$ ($0 \le l_i &lt; r_i \l...
Print "-1" (without quotes) if Polycarp can't make his way from point $x = 0$ to point $x = a$. Otherwise print one integer β€” the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally.
[ "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2\n", "10 1 1\n0 9\n0 5\n", "10 1 1\n0 9\n1 5\n" ]
[ "14\n", "45\n", "-1\n" ]
In the first example the only possible strategy is to take the fourth umbrella at the point $x = 1$, keep it till the point $x = 7$ (the total fatigue at $x = 7$ will be equal to $12$), throw it away, move on from $x = 7$ to $x = 8$ without an umbrella, take the third umbrella at $x = 8$ and keep it till the end (the t...
[ { "input": "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2", "output": "14" }, { "input": "10 1 1\n0 9\n0 5", "output": "45" }, { "input": "10 1 1\n0 9\n1 5", "output": "-1" }, { "input": "1 1 1\n0 1\n1 100000", "output": "-1" }, { "input": "1 1 1\n0 1\n0 100000", "ou...
217
9,420,800
3
43,155
788
Finding lines
[ "constructive algorithms", "divide and conquer", "interactive" ]
null
null
After some programming contest Roma decided to try himself in tourism. His home country Uzhlyandia is a Cartesian plane. He wants to walk along each of the Main Straight Lines in Uzhlyandia. It is known that each of these lines is a straight line parallel to one of the axes (i.e. it is described with the equation *x*<=...
There is no input initially. Your program should make queries to get information. It is guaranteed that the number of horizontal and vertical lines is at least 1 and less than or equal to 104 for each type.
none
[ "1\n1\n3\n2\n" ]
[ "0 1 2\n0 -2 -2\n0 5 6\n0 -2 2\n1 1 2\n2\n0 -3\n" ]
The example test is The minimum distances are: - from (1, 2) to *x* = 2; - from ( - 2,  - 2) to *y* =  - 3; - from (5, 6) to *x* = 2; - from ( - 2, 2) to *y* = 0.
[]
46
0
0
43,265
392
Tower of Hanoi
[ "dp" ]
null
null
The Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the pu...
Each of the first three lines contains three integers β€” matrix *t*. The *j*-th integer in the *i*-th line is *t**ij* (1<=≀<=*t**ij*<=≀<=10000;Β *i*<=β‰ <=*j*). The following line contains a single integer *n* (1<=≀<=*n*<=≀<=40) β€” the number of disks. It is guaranteed that for all *i* (1<=≀<=*i*<=≀<=3), *t**ii*<==<=0.
Print a single integer β€” the minimum cost of solving SmallY's puzzle.
[ "0 1 1\n1 0 1\n1 1 0\n3\n", "0 2 2\n1 0 100\n1 2 0\n3\n", "0 2 1\n1 0 100\n1 2 0\n5\n" ]
[ "7\n", "19\n", "87\n" ]
none
[ { "input": "0 1 1\n1 0 1\n1 1 0\n3", "output": "7" }, { "input": "0 2 2\n1 0 100\n1 2 0\n3", "output": "19" }, { "input": "0 2 1\n1 0 100\n1 2 0\n5", "output": "87" }, { "input": "0 5835 1487\n6637 0 9543\n6961 6820 0\n7", "output": "723638" }, { "input": "0 3287 ...
0
0
-1
43,396
702
Analysis of Pathes in Functional Graph
[ "data structures", "graphs" ]
null
null
You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to *n*<=-<=1. Graph is given as the array *f*0,<=*f*1,<=...,<=*f**n*<=-<=1, where *f**i* β€” the number of vertex to which goes the only arc from the vertex *i*. Besides you are giv...
The first line contains two integers *n*,<=*k* (1<=≀<=*n*<=≀<=105,<=1<=≀<=*k*<=≀<=1010). The second line contains the sequence *f*0,<=*f*1,<=...,<=*f**n*<=-<=1 (0<=≀<=*f**i*<=&lt;<=*n*) and the third β€” the sequence *w*0,<=*w*1,<=...,<=*w**n*<=-<=1 (0<=≀<=*w**i*<=≀<=108).
Print *n* lines, the pair of integers *s**i*, *m**i* in each line.
[ "7 3\n1 2 3 4 3 2 6\n6 3 1 4 2 2 3\n", "4 4\n0 1 2 3\n0 1 2 3\n", "5 3\n1 2 3 4 0\n4 1 2 14 3\n" ]
[ "10 1\n8 1\n7 1\n10 2\n8 2\n7 1\n9 3\n", "0 0\n4 1\n8 2\n12 3\n", "7 1\n17 1\n19 2\n21 3\n8 1\n" ]
none
[ { "input": "7 3\n1 2 3 4 3 2 6\n6 3 1 4 2 2 3", "output": "10 1\n8 1\n7 1\n10 2\n8 2\n7 1\n9 3" }, { "input": "4 4\n0 1 2 3\n0 1 2 3", "output": "0 0\n4 1\n8 2\n12 3" }, { "input": "5 3\n1 2 3 4 0\n4 1 2 14 3", "output": "7 1\n17 1\n19 2\n21 3\n8 1" }, { "input": "1 1\n0\n100...
46
0
0
43,464
878
Numbers on the blackboard
[ "combinatorics", "dp" ]
null
null
A sequence of *n* integers is written on a blackboard. Soon Sasha will come to the blackboard and start the following actions: let *x* and *y* be two adjacent numbers (*x* before *y*), then he can remove them and write *x*<=+<=2*y* instead of them. He will perform these operations until one number is left. Sasha likes ...
The first line contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=105) β€” the number of integers on the blackboard and the number of Nikita's options. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≀<=*a**i*<=≀<=109)Β β€” the sequence on the blackboard. Each of the next *q* lines contains t...
For each option output Sasha's result modulo 109<=+<=7.
[ "3 3\n1 2 3\n1 3\n1 2\n2 3\n", "3 1\n1 2 -3\n1 3\n", "4 2\n1 1 1 -1\n1 4\n3 4\n" ]
[ "17\n5\n8\n", "1000000006\n", "5\n1000000006\n" ]
In the second sample Nikita doesn't erase anything. Sasha first erases the numbers 1 and 2 and writes 5. Then he erases 5 and -3 and gets -1. -1 modulo 10<sup class="upper-index">9</sup> + 7 is 10<sup class="upper-index">9</sup> + 6.
[]
46
0
0
43,470
52
Right Triangles
[ "combinatorics" ]
B. Right Triangles
2
256
You are given a *n*<=Γ—<=*m* field consisting only of periods ('.') and asterisks ('*'). Your task is to count all right triangles with two sides parallel to the square sides, whose vertices are in the centers of '*'-cells. A right triangle is a triangle in which one angle is a right angle (that is, a 90 degree angle).
The first line contains two positive integer numbers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=1000). The following *n* lines consist of *m* characters each, describing the field. Only '.' and '*' are allowed.
Output a single number β€” total number of square triangles in the field. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
[ "2 2\n**\n*.\n", "3 4\n*..*\n.**.\n*.**\n" ]
[ "1\n", "9\n" ]
none
[ { "input": "2 2\n**\n*.", "output": "1" }, { "input": "3 4\n*..*\n.**.\n*.**", "output": "9" }, { "input": "3 2\n..\n..\n*.", "output": "0" }, { "input": "1 2\n**", "output": "0" }, { "input": "1 3\n*.*", "output": "0" }, { "input": "5 2\n*.\n**\n.*\n....
530
26,112,000
3.818863
43,503
932
Tree
[ "binary search", "dp", "trees" ]
null
null
You are given a node of the tree with index 1 and with weight 0. Let *cnt* be the number of nodes in the tree at any instant (initially, *cnt* is set to 1). Support *Q* queries of following two types: - Add a new node (index *cnt*<=+<=1) with weight *W* and add edge between node *R* and this node. - Output the max...
First line containing the number of queries *Q* (1<=≀<=*Q*<=≀<=400000). Let *last* be the answer for previous query of type 2 (initially *last* equals 0). Each of the next *Q* lines contains a query of following form: - 1 p q (1<=≀<=*p*,<=*q*<=≀<=1018): This is query of first type where and . It is guaranteed tha...
Output the answer to each query of second type in separate line.
[ "6\n1 1 1\n2 2 0\n2 2 1\n1 3 0\n2 2 0\n2 2 2\n", "6\n1 1 0\n2 2 0\n2 0 3\n1 0 2\n2 1 3\n2 1 6\n", "7\n1 1 2\n1 2 3\n2 3 3\n1 0 0\n1 5 1\n2 5 0\n2 4 0\n", "7\n1 1 3\n1 2 3\n2 3 4\n1 2 0\n1 5 3\n2 5 5\n2 7 22\n" ]
[ "0\n1\n1\n2\n", "2\n2\n3\n2\n", "1\n1\n2\n", "1\n2\n3\n" ]
In the first example, *last* = 0 - Query 1: 1 1 1, Node 2 with weight 1 is added to node 1. - Query 2: 2 2 0, No sequence of nodes starting at 2 has weight less than or equal to 0. *last* = 0 - Query 3: 2 2 1, Answer is 1 as sequence will be {2}. *last* = 1 - Query 4: 1 2 1, Node 3 with weight 1 is added to node...
[ { "input": "6\n1 1 1\n2 2 0\n2 2 1\n1 3 0\n2 2 0\n2 2 2", "output": "0\n1\n1\n2" }, { "input": "6\n1 1 0\n2 2 0\n2 0 3\n1 0 2\n2 1 3\n2 1 6", "output": "2\n2\n3\n2" }, { "input": "7\n1 1 2\n1 2 3\n2 3 3\n1 0 0\n1 5 1\n2 5 0\n2 4 0", "output": "1\n1\n2" }, { "input": "7\n1 1 3...
46
0
0
43,535
71
Nuclear Fusion
[ "bitmasks", "dp" ]
E. Nuclear Fusion
1
256
There is the following puzzle popular among nuclear physicists. A reactor contains a set of *n* atoms of some chemical elements. We shall understand the phrase "atomic number" as the number of this atom's element in the periodic table of the chemical elements. You are allowed to take any two different atoms and fuse ...
The first line contains two integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=17). The second line contains space-separated symbols of elements of *n* atoms, which are available from the start. The third line contains space-separated symbols of elements of *k* atoms which need to be the result of the fusion. The symbols of t...
If it is impossible to synthesize the required atoms, print "NO" without the quotes. Otherwise, print on the first line Β«YESΒ», and on the next *k* lines print the way of synthesizing each of *k* atoms as equations. Each equation has the following form: "*x*1+*x*2+...+*x**t*-&gt;*y**i*", where *x**j* is the symbol of th...
[ "10 3\nMn Co Li Mg C P F Zn Sc K\nSn Pt Y\n", "2 1\nH H\nHe\n", "2 2\nBk Fm\nCf Es\n" ]
[ "YES\nMn+C+K-&gt;Sn\nCo+Zn+Sc-&gt;Pt\nLi+Mg+P+F-&gt;Y\n", "YES\nH+H-&gt;He\n", "NO\n" ]
The reactions from the first example possess the following form (the atomic number is written below and to the left of the element): <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6f2ce1bed492cbe40ff1bb4600fe53aebc680ace.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="mi...
[ { "input": "10 3\nMn Co Li Mg C P F Zn Sc K\nSn Pt Y", "output": "YES\nCo+Mg->Y\nLi+P+F+Zn+Sc->Pt\nMn+C+K->Sn" }, { "input": "2 1\nH H\nHe", "output": "YES\nH+H->He" }, { "input": "2 2\nBk Fm\nCf Es", "output": "NO" }, { "input": "8 8\nTl Pb Bi Po Np Pu Am Cm\nAt Rn Fr Ra Ac ...
93
3,584,000
0
43,668
0
none
[ "none" ]
null
null
Π’Π°ΠΌ Π·Π°Π΄Π°Π½ΠΎ ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΎΠ΅ ΠΊΠ»Π΅Ρ‚Ρ‡Π°Ρ‚ΠΎΠ΅ ΠΏΠΎΠ»Π΅, состоящСС ΠΈΠ· *n* строк ΠΈ *m* столбцов. ПолС содСрТит Ρ†ΠΈΠΊΠ» ΠΈΠ· символов Β«*Β», Ρ‚Π°ΠΊΠΎΠΉ Ρ‡Ρ‚ΠΎ: - Ρ†ΠΈΠΊΠ» ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠ±ΠΎΠΉΡ‚ΠΈ, посСтив ΠΊΠ°ΠΆΠ΄ΡƒΡŽ Π΅Π³ΠΎ ΠΊΠ»Π΅Ρ‚ΠΊΡƒ Ρ€ΠΎΠ²Π½ΠΎ ΠΎΠ΄ΠΈΠ½ Ρ€Π°Π·, ΠΏΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π°ΡΡΡŒ ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ Ρ€Π°Π· Π²Π²Π΅Ρ€Ρ…/Π²Π½ΠΈΠ·/Π²ΠΏΡ€Π°Π²ΠΎ/Π²Π»Π΅Π²ΠΎ Π½Π° ΠΎΠ΄Π½Ρƒ ΠΊΠ»Π΅Ρ‚ΠΊΡƒ; - Ρ†ΠΈΠΊΠ» Π½Π΅ содСрТит самопСрСсСчСний ΠΈ самокасаний, Ρ‚ΠΎ Π΅ΡΡ‚ΡŒ Π΄Π²Π΅ ΠΊΠ»...
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΉ строкС Π²Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… записаны Π΄Π²Π° Ρ†Π΅Π»Ρ‹Ρ… числа *n* ΠΈ *m* (3<=≀<=*n*,<=*m*<=≀<=100) β€” количСство строк ΠΈ столбцов ΠΏΡ€ΡΠΌΠΎΡƒΠ³ΠΎΠ»ΡŒΠ½ΠΎΠ³ΠΎ ΠΊΠ»Π΅Ρ‚Ρ‡Π°Ρ‚ΠΎΠ³ΠΎ поля соотвСтствСнно. Π’ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΡ… *n* строках записаны ΠΏΠΎ *m* символов, ΠΊΠ°ΠΆΠ΄Ρ‹ΠΉ ΠΈΠ· ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Ρ… β€” Β«.Β», Β«*Β» ΠΈΠ»ΠΈ Β«SΒ». ГарантируСтся, Ρ‡Ρ‚ΠΎ ΠΎΡ‚Π»ΠΈΡ‡Π½Ρ‹Π΅ ΠΎΡ‚ Β«.Β» символы ΠΎΠ±Ρ€Π°Π·ΡƒΡŽΡ‚ Ρ†ΠΈΠΊΠ» Π±Π΅Π· с...
Π’ ΠΏΠ΅Ρ€Π²ΡƒΡŽ строку Π²Ρ‹Ρ…ΠΎΠ΄Π½Ρ‹Ρ… Π΄Π°Π½Π½Ρ‹Ρ… Π²Ρ‹Π²Π΅Π΄ΠΈΡ‚Π΅ ΠΈΡΠΊΠΎΠΌΡƒΡŽ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ ΠΊΠΎΠΌΠ°Π½Π΄ для Π ΠΎΠ±ΠΎΡ‚Π°. НаправлСниС ΠΎΠ±Ρ…ΠΎΠ΄Π° Ρ†ΠΈΠΊΠ»Π° Π ΠΎΠ±ΠΎΡ‚ΠΎΠΌ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ Π»ΡŽΠ±Ρ‹ΠΌ.
[ "3 3\n***\n*.*\n*S*\n", "6 7\n.***...\n.*.*...\n.*.S**.\n.*...**\n.*....*\n.******\n" ]
[ "LUURRDDL\n", "UULLDDDDDRRRRRUULULL\n" ]
Π’ ΠΏΠ΅Ρ€Π²ΠΎΠΌ тСстовом ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π΅ для ΠΎΠ±Ρ…ΠΎΠ΄Π° ΠΏΠΎ часовой стрСлкС ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ посСщСнных Ρ€ΠΎΠ±ΠΎΡ‚ΠΎΠΌ ΠΊΠ»Π΅Ρ‚ΠΎΠΊ выглядит ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠΌ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ: 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (3, 2); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (3, 1); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (2, 1); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (1, 1); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (1, 2); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (1, 3); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (2, 3); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (3, 3); 1. ΠΊΠ»Π΅Ρ‚ΠΊΠ° (3, 2).
[ { "input": "3 3\n***\n*.*\n*S*", "output": "LUURRDDL" }, { "input": "6 7\n.***...\n.*.*...\n.*.S**.\n.*...**\n.*....*\n.******", "output": "UULLDDDDDRRRRRUULULL" }, { "input": "100 3\n***\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\...
31
4,812,800
0
43,806
383
Volcanoes
[ "binary search", "implementation", "sortings", "two pointers" ]
null
null
Iahub got lost in a very big desert. The desert can be represented as a *n*<=Γ—<=*n* square matrix, where each cell is a zone of the desert. The cell (*i*,<=*j*) represents the cell at row *i* and column *j* (1<=≀<=*i*,<=*j*<=≀<=*n*). Iahub can go from one cell (*i*,<=*j*) only down or right, that is to cells (*i*<=+<=1...
The first line contains two integers *n* (1<=≀<=*n*<=≀<=109) and *m* (1<=≀<=*m*<=≀<=105). Each of the next *m* lines contains a pair of integers, *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=*n*), representing the coordinates of the volcanoes. Consider matrix rows are numbered from 1 to *n* from top to bottom, and matrix columns ...
Print one integer, the minimum time in which Iahub can arrive at cell (*n*,<=*n*). If no solution exists (there is no path to the final cell), print -1.
[ "4 2\n1 3\n1 4\n", "7 8\n1 6\n2 6\n3 5\n3 6\n4 3\n5 1\n5 2\n5 3\n", "2 2\n1 2\n2 1\n" ]
[ "6\n", "12\n", "-1\n" ]
Consider the first sample. A possible road is: (1, 1)  →  (1, 2)  →  (2, 2)  →  (2, 3)  →  (3, 3)  →  (3, 4)  →  (4, 4).
[ { "input": "4 2\n1 3\n1 4", "output": "6" }, { "input": "7 8\n1 6\n2 6\n3 5\n3 6\n4 3\n5 1\n5 2\n5 3", "output": "12" }, { "input": "2 2\n1 2\n2 1", "output": "-1" }, { "input": "1000000000 9\n1 2\n3 1\n3 2\n999999998 999999999\n999999998 1000000000\n999999999 999999999\n9999...
46
0
0
43,885
255
Code Parsing
[ "implementation" ]
null
null
Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string *s*, consisting of characters "x" and "y", and uses two following operations at runtime: 1. Find two consecutive characters in the string, such that the first of them equals "y", and the ...
The first line contains a non-empty string *s*. It is guaranteed that the string only consists of characters "x" and "y". It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string.
In the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string *s*.
[ "x\n", "yxyxy\n", "xxxxxy\n" ]
[ "x\n", "y\n", "xxxx\n" ]
In the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change. In the second test the transformation will be like this: 1. string "yxyxy" transforms into string "xyyxy"; 1. string "xyyxy" transforms into string "xyxyy"; 1. ...
[ { "input": "x", "output": "x" }, { "input": "yxyxy", "output": "y" }, { "input": "xxxxxy", "output": "xxxx" }, { "input": "yxyyxyyx", "output": "yy" }, { "input": "yxxyxyx", "output": "x" }, { "input": "xxx", "output": "xxx" }, { "input": "...
436
2,048,000
-1
44,012
0
none
[ "none" ]
null
null
Vasya and Kolya play a game with a string, using the following rules. Initially, Kolya creates a string *s*, consisting of small English letters, and uniformly at random chooses an integer *k* from a segment [0,<=*len*(*s*)<=-<=1]. He tells Vasya this string *s*, and then shifts it *k* letters to the left, i.Β e. create...
The only string contains the string *s* of length *l* (3<=≀<=*l*<=≀<=5000), consisting of small English letters only.
Print the only numberΒ β€” the answer for the problem. You answer is considered correct, if its absolute or relative error does not exceed 10<=-<=6. Formally, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct if
[ "technocup\n", "tictictactac\n", "bbaabaabbb\n" ]
[ "1.000000000000000\n", "0.333333333333333\n", "0.100000000000000\n" ]
In the first example Vasya can always open the second letter after opening the first letter, and the cyclic shift is always determined uniquely. In the second example if the first opened letter of *t* is "t" or "c", then Vasya can't guess the shift by opening only one other letter. On the other hand, if the first lett...
[ { "input": "technocup", "output": "1.000000000000000" }, { "input": "tictictactac", "output": "0.333333333333333" }, { "input": "bbaabaabbb", "output": "0.100000000000000" }, { "input": "cbbbbcaaca", "output": "0.800000000000000" }, { "input": "cadbcdddda", "o...
61
19,968,000
0
44,035
167
Wizards and Numbers
[ "games", "math" ]
null
null
In some country live wizards. They love playing with numbers. The blackboard has two numbers written on it β€” *a* and *b*. The order of the numbers is not important. Let's consider *a*<=≀<=*b* for the sake of definiteness. The players can cast one of the two spells in turns: - Replace *b* with *b*<=-<=*a**k*. Number...
The first line contains a single integer *t* β€” the number of input data sets (1<=≀<=*t*<=≀<=104). Each of the next *t* lines contains two integers *a*, *b* (0<=≀<=*a*,<=*b*<=≀<=1018). The numbers are separated by a space. Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred ...
For any of the *t* input sets print "First" (without the quotes) if the player who moves first wins. Print "Second" (without the quotes) if the player who moves second wins. Print the answers to different data sets on different lines in the order in which they are given in the input.
[ "4\n10 21\n31 10\n0 1\n10 30\n" ]
[ "First\nSecond\nSecond\nFirst\n" ]
In the first sample, the first player should go to (11,10). Then, after a single move of the second player to (1,10), he will take 10 modulo 1 and win. In the second sample the first player has two moves to (1,10) and (21,10). After both moves the second player can win. In the third sample, the first player has no mo...
[ { "input": "4\n10 21\n31 10\n0 1\n10 30", "output": "First\nSecond\nSecond\nFirst" }, { "input": "66\n7 0\n5 7\n1 3\n3 2\n3 5\n0 6\n1 2\n0 7\n4 5\n4 7\n5 1\n2 0\n4 0\n0 5\n3 6\n7 3\n6 0\n5 2\n6 6\n1 7\n5 6\n2 2\n3 4\n2 1\n5 3\n4 6\n6 2\n3 3\n100000000000 1000000000000000000\n0 1\n4 1\n2 6\n5 5\n4 3\...
62
0
0
44,036
189
Counting Rhombi
[ "brute force", "math" ]
null
null
You have two positive integers *w* and *h*. Your task is to count the number of rhombi which have the following properties: - Have positive area. - With vertices at integer points. - All vertices of the rhombi are located inside or on the border of the rectangle with vertices at points (0,<=0), (*w*,<=0), (*w*,<=*...
The first line contains two integers *w* and *h* (1<=≀<=*w*,<=*h*<=≀<=4000) β€” the rectangle's sizes.
Print a single number β€” the number of sought rhombi. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "2 2\n", "1 2\n" ]
[ "1\n", "0\n" ]
In the first example there exists only one such rhombus. Its vertices are located at points (1, 0), (2, 1), (1, 2), (0, 1).
[ { "input": "2 2", "output": "1" }, { "input": "1 2", "output": "0" }, { "input": "1 4000", "output": "0" }, { "input": "4000 1", "output": "0" }, { "input": "4000 4000", "output": "16000000000000" }, { "input": "15 10", "output": "1400" }, { ...
92
0
0
44,317
396
On Changing Tree
[ "data structures", "graphs", "trees" ]
null
null
You are given a rooted tree consisting of *n* vertices numbered from 1 to *n*. The root of the tree is a vertex number 1. Initially all vertices contain number 0. Then come *q* queries, each query has one of the two types: - The format of the query: 1 *v* *x* *k*. In response to the query, you need to add to the num...
The first line contains integer *n* (1<=≀<=*n*<=≀<=3Β·105) β€”Β the number of vertices in the tree. The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=... *p**n* (1<=≀<=*p**i*<=&lt;<=*i*), where *p**i* is the number of the vertex that is the parent of vertex *i* in the tree. The third line contains integer *q* (1<=...
For each query of the second type print on a single line the number written in the vertex from the query. Print the number modulo 1000000007 (109<=+<=7).
[ "3\n1 1\n3\n1 1 2 1\n2 1\n2 2\n" ]
[ "2\n1\n" ]
You can read about a rooted tree here: http://en.wikipedia.org/wiki/Tree_(graph_theory).
[ { "input": "3\n1 1\n3\n1 1 2 1\n2 1\n2 2", "output": "2\n1" }, { "input": "10\n1 2 3 4 4 3 3 6 7\n10\n1 6 13 98\n1 7 17 66\n1 5 32 39\n1 1 9 5\n1 7 27 11\n1 1 24 79\n1 5 87 86\n2 2\n1 5 9 38\n2 5", "output": "999999956\n999999832" }, { "input": "1\n\n1\n2 1", "output": "0" } ]
109
307,200
0
44,364
677
Vanya and Label
[ "bitmasks", "combinatorics", "implementation", "strings" ]
null
null
While walking down the street Vanya saw a label "Hide&amp;Seek". Because he is a programmer, he used &amp; as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string *s* and wants to know the number of pairs of words of length |*s*| (length of *s*), such ...
The only line of the input contains a single word *s* (1<=≀<=|*s*|<=≀<=100<=000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'.
Print a single integerΒ β€” the number of possible pairs of words, such that their bitwise AND is equal to string *s* modulo 109<=+<=7.
[ "z\n", "V_V\n", "Codeforces\n" ]
[ "3\n", "9\n", "130653412\n" ]
For a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia. In the first sample, there are 3 possible solutions: 1. *z*&amp;_ = 61&amp;63 = 61 = *z* 1. _&amp;*z* = 63&amp;61 = 61 = *z* 1. *z*&amp;*z* = 61&amp;61 = 61 = *z*
[ { "input": "z", "output": "3" }, { "input": "V_V", "output": "9" }, { "input": "Codeforces", "output": "130653412" }, { "input": "zHsIINYjVtU71kmM9E", "output": "130312847" }, { "input": "fRRNAdMvLFTX21T0FG5gyn7NG0SaIvzGG_g_SO", "output": "547121709" }, { ...
46
0
0
44,488
0
none
[ "none" ]
null
null
Imagine that you are in a building that has exactly *n* floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to *n*. Now you're on the floor number *a*. You are very bored, so you want to take the lift. Floor number *b* has a secret lab, the entry is forbidd...
The first line of the input contains four space-separated integers *n*, *a*, *b*, *k* (2<=≀<=*n*<=≀<=5000, 1<=≀<=*k*<=≀<=5000, 1<=≀<=*a*,<=*b*<=≀<=*n*, *a*<=β‰ <=*b*).
Print a single integer β€” the remainder after dividing the sought number of sequences by 1000000007 (109<=+<=7).
[ "5 2 4 1\n", "5 2 4 2\n", "5 3 4 1\n" ]
[ "2\n", "2\n", "0\n" ]
Two sequences *p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">2</sub>, ..., *p*<sub class="lower-index">*k*</sub> and *q*<sub class="lower-index">1</sub>, *q*<sub class="lower-index">2</sub>, ..., *q*<sub class="lower-index">*k*</sub> are distinct, if there is such integer *j* (1 ≀ *j* ≀ *k*), that *p*...
[ { "input": "5 2 4 1", "output": "2" }, { "input": "5 2 4 2", "output": "2" }, { "input": "5 3 4 1", "output": "0" }, { "input": "2 2 1 1", "output": "0" }, { "input": "10 1 10 2", "output": "44" }, { "input": "2222 1206 1425 2222", "output": "40257...
31
0
0
44,630
815
Karen and Test
[ "brute force", "combinatorics", "constructive algorithms", "math" ]
null
null
Karen has just arrived at school, and she has a math test today! The test is about basic addition and subtraction. Unfortunately, the teachers were too busy writing tasks for Codeforces rounds, and had no time to make an actual test. So, they just put one question in the test that is worth all the points. There are *...
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=200000), the number of numbers written on the first row. The next line contains *n* integers. Specifically, the *i*-th one among these is *a**i* (1<=≀<=*a**i*<=≀<=109), the *i*-th number on the first row.
Output a single integer on a line by itself, the number on the final row after performing the process above. Since this number can be quite large, print only the non-negative remainder after dividing it by 109<=+<=7.
[ "5\n3 6 9 12 15\n", "4\n3 7 5 2\n" ]
[ "36\n", "1000000006\n" ]
In the first test case, the numbers written on the first row are 3, 6, 9, 12 and 15. Karen performs the operations as follows: The non-negative remainder after dividing the final number by 10<sup class="upper-index">9</sup> + 7 is still 36, so this is the correct output. In the second test case, the numbers written ...
[ { "input": "5\n3 6 9 12 15", "output": "36" }, { "input": "4\n3 7 5 2", "output": "1000000006" }, { "input": "1\n1", "output": "1" }, { "input": "16\n985629174 189232688 48695377 692426437 952164554 243460498 173956955 210310239 237322183 96515847 678847559 682240199 49879255...
93
0
-1
44,707
939
Cutlet
[ "data structures", "dp" ]
null
null
Arkady wants to have a dinner. He has just returned from a shop where he has bought a semifinished cutlet. He only needs to fry it. The cutlet should be fried for 2*n* seconds, in particular, it should be fried for *n* seconds on one side and *n* seconds on the other side. Arkady has already got a frying pan and turn o...
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=100<=000, 1<=≀<=*k*<=≀<=100)Β β€” the number of seconds the cutlet should be cooked on each side and number of periods of time in which Arkady can flip it. The next *k* lines contain descriptions of these intervals. Each line contains two integers *l**i* and...
Output "Hungry" if Arkady won't be able to fry the cutlet for exactly *n* seconds on one side and exactly *n* seconds on the other side. Otherwise, output "Full" in the first line, and the minimum number of times he should flip the cutlet in the second line.
[ "10 2\n3 5\n11 13\n", "10 3\n3 5\n9 10\n11 13\n", "20 1\n3 19\n" ]
[ "Full\n2\n", "Full\n1\n", "Hungry\n" ]
In the first example Arkady should flip the cutlet in time moment 3 seconds after he starts cooking and in time moment 13 seconds after he starts cooking. In the second example, Arkady can flip the cutlet at 10 seconds after he starts cooking.
[ { "input": "10 2\n3 5\n11 13", "output": "Full\n2" }, { "input": "10 3\n3 5\n9 10\n11 13", "output": "Full\n1" }, { "input": "20 1\n3 19", "output": "Hungry" }, { "input": "10 1\n0 20", "output": "Full\n1" }, { "input": "10 1\n0 1", "output": "Hungry" }, {...
0
0
-1
44,712
394
Very Beautiful Number
[ "math" ]
null
null
Teacher thinks that we make a lot of progress. Now we are even allowed to use decimal notation instead of counting sticks. After the test the teacher promised to show us a "very beautiful number". But the problem is, he's left his paper with the number in the teachers' office. The teacher remembers that the "very beau...
The single line contains integers *p*, *x* (1<=≀<=*p*<=≀<=106,<=1<=≀<=*x*<=≀<=9).
If the teacher's made a mistake and such number doesn't exist, then print on a single line "Impossible" (without the quotes). Otherwise, print the "very beautiful number" without leading zeroes.
[ "6 5\n", "1 2\n", "6 4\n" ]
[ "142857", "Impossible\n", "102564" ]
Sample 1: 142857Β·5 = 714285. Sample 2: The number that consists of a single digit cannot stay what it is when multiplied by 2, thus, the answer to the test sample is "Impossible".
[ { "input": "6 5", "output": "142857" }, { "input": "1 2", "output": "Impossible" }, { "input": "6 4", "output": "102564" }, { "input": "11 1", "output": "11111111111" }, { "input": "42 5", "output": "102040816326530612244897959183673469387755" }, { "in...
1,000
9,420,800
0
44,761
0
none
[ "none" ]
null
null
You have an array of positive integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] and a set of bad prime numbers *b*1,<=*b*2,<=...,<=*b**m*. The prime numbers that do not occur in the set *b* are considered good. The beauty of array *a* is the sum , where function *f*(*s*) is determined as follows: - *f*(1)<==<=0; - Let's ass...
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=5000) showing how many numbers are in the array and how many bad prime numbers there are. The second line contains *n* space-separated integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (1<=≀<=*a*[*i*]<=≀<=109) β€” array *a*. The third line contains *m* space-...
Print a single integer β€” the answer to the problem.
[ "5 2\n4 20 34 10 10\n2 5\n", "4 5\n2 4 8 16\n3 5 7 11 17\n" ]
[ "-2\n", "10\n" ]
Note that the answer to the problem can be negative. The GCD(*x*<sub class="lower-index">1</sub>, *x*<sub class="lower-index">2</sub>, ..., *x*<sub class="lower-index">*k*</sub>) is the maximum positive integer that divides each *x*<sub class="lower-index">*i*</sub>.
[]
62
2,867,200
-1
44,797
766
Mahmoud and a Message
[ "brute force", "dp", "greedy", "strings" ]
null
null
Mahmoud wrote a message *s* of length *n*. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number *i* in the English...
The first line contains an integer *n* (1<=≀<=*n*<=≀<=103) denoting the length of the message. The second line contains the message *s* of length *n* that consists of lowercase English letters. The third line contains 26 integers *a*1,<=*a*2,<=...,<=*a*26 (1<=≀<=*a**x*<=≀<=103)Β β€” the maximum lengths of substring each...
Print three lines. In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 109<=<=+<=<=7. In the second line print the length of the longest substring over all the ways. In the third line print the minimum number of substrings over al...
[ "3\naab\n2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "10\nabcdeabcde\n5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n" ]
[ "3\n2\n2\n", "401\n4\n3\n" ]
In the first example the three ways to split the message are: - a|a|b - aa|b - a|ab The longest substrings are "aa" and "ab" of length 2. The minimum number of substrings is 2 in "a|ab" or "aa|b". Notice that "aab" is not a possible splitting because the letter 'a' appears in a substring of length 3, while *a*<...
[ { "input": "3\naab\n2 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "3\n2\n2" }, { "input": "10\nabcdeabcde\n5 5 5 5 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "401\n4\n3" }, { "input": "10\naaaaaaaaaa\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", ...
77
3,276,800
3
44,808
853
Lada Malina
[ "data structures", "geometry" ]
null
null
After long-term research and lots of experiments leading Megapolian automobile manufacturer Β«AutoVozΒ» released a brand new car model named Β«Lada MalinaΒ». One of the most impressive features of Β«Lada MalinaΒ» is its highly efficient environment-friendly engines. Consider car as a point in *Oxy* plane. Car is equipped wi...
The first line of input contains three integers *k*,<=*n*,<=*q* (2<=≀<=*k*<=≀<=10, 1<=≀<=*n*<=≀<=105, 1<=≀<=*q*<=≀<=105), the number of engines of Β«Lada MalinaΒ», number of factories producing Β«Lada MalinaΒ» and number of options of an exposition time and location respectively. The following *k* lines contain the descri...
For each possible option of the exposition output the number of cars that will be able to get to the exposition location by the moment of its beginning.
[ "2 4 1\n1 1\n-1 1\n2 3 1\n2 -2 1\n-2 1 1\n-2 -2 1\n0 0 2\n", "3 4 3\n2 0\n-1 1\n-1 -2\n-3 0 6\n1 -2 1\n-3 -7 3\n3 2 2\n-1 -4 1\n0 4 2\n6 0 1\n" ]
[ "3\n", "4\n9\n0\n" ]
Images describing sample tests are given below. Exposition options are denoted with crosses, factories are denoted with points. Each factory is labeled with a number of cars that it has. First sample test explanation: - Car from the first factory is not able to get to the exposition location in time. - Car from the...
[]
46
0
0
44,871