contestId
int64
0
1.01k
name
stringlengths
2
58
tags
sequencelengths
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
sequencelengths
0
7
demo-output
sequencelengths
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
901
Bipartite Segments
[ "binary search", "data structures", "dfs and similar", "dsu", "graphs", "two pointers" ]
null
null
You are given an undirected graph with *n* vertices. There are no edge-simple cycles with the even length in it. In other words, there are no cycles of even length that pass each edge at most once. Let's enumerate vertices from 1 to *n*. You have to answer *q* queries. Each query is described by a segment of vertices [*l*;<=*r*], and you have to count the number of its subsegments [*x*;<=*y*] (*l*<=≀<=*x*<=≀<=*y*<=≀<=*r*), such that if we delete all vertices except the segment of vertices [*x*;<=*y*] (including *x* and *y*) and edges between them, the resulting graph is bipartite.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=3Β·105, 1<=≀<=*m*<=≀<=3Β·105)Β β€” the number of vertices and the number of edges in the graph. The next *m* lines describe edges in the graph. The *i*-th of these lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*; *a**i*<=β‰ <=*b**i*), denoting an edge between vertices *a**i* and *b**i*. It is guaranteed that this graph does not contain edge-simple cycles of even length. The next line contains a single integer *q* (1<=≀<=*q*<=≀<=3Β·105)Β β€” the number of queries. The next *q* lines contain queries. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*)Β β€” the query parameters.
Print *q* numbers, each in new line: the *i*-th of them should be the number of subsegments [*x*;<=*y*] (*l**i*<=≀<=*x*<=≀<=*y*<=≀<=*r**i*), such that the graph that only includes vertices from segment [*x*;<=*y*] and edges between them is bipartite.
[ "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n3\n1 3\n4 6\n1 6\n", "8 9\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7\n7 8\n8 4\n7 2\n3\n1 8\n1 4\n3 8\n" ]
[ "5\n5\n14\n", "27\n8\n19\n" ]
The first example is shown on the picture below: <img class="tex-graphics" src="https://espresso.codeforces.com/01e1d1999228f416613ff64b5d0e0cf984f150b1.png" style="max-width: 100.0%;max-height: 100.0%;"/> For the first query, all subsegments of [1; 3], except this segment itself, are suitable. For the first query, all subsegments of [4; 6], except this segment itself, are suitable. For the third query, all subsegments of [1; 6] are suitable, except [1; 3], [1; 4], [1; 5], [1; 6], [2; 6], [3; 6], [4; 6]. The second example is shown on the picture below: <img class="tex-graphics" src="https://espresso.codeforces.com/09b9227070585b8d5a7dff3cbc5f8535c260a595.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n3\n1 3\n4 6\n1 6", "output": "5\n5\n14" }, { "input": "8 9\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7\n7 8\n8 4\n7 2\n3\n1 8\n1 4\n3 8", "output": "27\n8\n19" }, { "input": "12 12\n5 1\n5 11\n1 11\n8 11\n8 9\n5 12\n6 9\n7 11\n9 3\n9 10\n4 12\n10 2\n78\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n2 2\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n3 3\n3 4\n3 5\n3 6\n3 7\n3 8\n3 9\n3 10\n3 11\n3 12\n4 4\n4 5\n4 6\n4 7\n4 8\n4 9\n4 10\n4 11\n4 12\n5 5\n5 6\n5 7\n5 8\n5 9\n5 10\n5 11\n5 12\n6 6\n6 7\n6 8\n6 9\n6 10\n6 11\n6 12\n7 7\n7 8\n7 9\n7 10\n7 11\n7 12\n8 8\n8 9\n8 10\n8 11\n8 12\n9 9\n9 10\n9 11\n9 12\n10 10\n10 11\n10 12\n11 11\n11 12\n12 12", "output": "1\n3\n6\n10\n15\n21\n28\n36\n45\n55\n65\n76\n1\n3\n6\n10\n15\n21\n28\n36\n45\n55\n66\n1\n3\n6\n10\n15\n21\n28\n36\n45\n55\n1\n3\n6\n10\n15\n21\n28\n36\n45\n1\n3\n6\n10\n15\n21\n28\n36\n1\n3\n6\n10\n15\n21\n28\n1\n3\n6\n10\n15\n21\n1\n3\n6\n10\n15\n1\n3\n6\n10\n1\n3\n6\n1\n3\n1" } ]
2,000
137,318,400
0
17,096
847
Union of Doubly Linked Lists
[ "implementation" ]
null
null
Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle. In this problem you are given *n* memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to *n*. For each cell *i* you are given two values: - *l**i* β€” cell containing previous element for the element in the cell *i*; - *r**i* β€” cell containing next element for the element in the cell *i*. If cell *i* contains information about the element which has no previous element then *l**i*<==<=0. Similarly, if cell *i* contains information about the element which has no next element then *r**i*<==<=0. For example, for the picture above the values of *l* and *r* are the following: *l*1<==<=4, *r*1<==<=7; *l*2<==<=5, *r*2<==<=0; *l*3<==<=0, *r*3<==<=0; *l*4<==<=6, *r*4<==<=1; *l*5<==<=0, *r*5<==<=2; *l*6<==<=0, *r*6<==<=4; *l*7<==<=1, *r*7<==<=0. Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values *l**i*, *r**i*. Any other action, other than joining the beginning of one list to the end of another, can not be performed.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of memory cells where the doubly linked lists are located. Each of the following *n* lines contains two integers *l**i*, *r**i* (0<=≀<=*l**i*,<=*r**i*<=≀<=*n*) β€” the cells of the previous and the next element of list for cell *i*. Value *l**i*<==<=0 if element in cell *i* has no previous element in its list. Value *r**i*<==<=0 if element in cell *i* has no next element in its list. It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of *n* given cells.
Print *n* lines, the *i*-th line must contain two integers *l**i* and *r**i* β€” the cells of the previous and the next element of list for cell *i* after all lists from the input are united in a single list. If there are many solutions print any of them.
[ "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0\n" ]
[ "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0\n" ]
none
[ { "input": "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0", "output": "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0" }, { "input": "2\n2 0\n0 1", "output": "2 0\n0 1" }, { "input": "1\n0 0", "output": "0 0" }, { "input": "4\n0 2\n1 0\n0 4\n3 0", "output": "0 2\n1 3\n2 4\n3 0" }, { "input": "5\n0 0\n0 0\n0 0\n0 0\n0 0", "output": "0 2\n1 3\n2 4\n3 5\n4 0" }, { "input": "2\n0 0\n0 0", "output": "0 2\n1 0" }, { "input": "2\n0 2\n1 0", "output": "0 2\n1 0" }, { "input": "5\n5 3\n4 0\n1 4\n3 2\n0 1", "output": "5 3\n4 0\n1 4\n3 2\n0 1" }, { "input": "5\n2 0\n0 1\n0 4\n3 5\n4 0", "output": "2 3\n0 1\n1 4\n3 5\n4 0" }, { "input": "5\n3 4\n0 0\n0 1\n1 0\n0 0", "output": "3 4\n0 3\n2 1\n1 5\n4 0" }, { "input": "5\n3 0\n0 0\n0 1\n0 0\n0 0", "output": "3 4\n0 3\n2 1\n1 5\n4 0" }, { "input": "10\n7 5\n5 0\n4 7\n10 3\n1 2\n0 9\n3 1\n9 10\n6 8\n8 4", "output": "7 5\n5 0\n4 7\n10 3\n1 2\n0 9\n3 1\n9 10\n6 8\n8 4" }, { "input": "10\n6 2\n1 0\n9 4\n3 6\n10 8\n4 1\n0 10\n5 0\n0 3\n7 5", "output": "6 2\n1 0\n9 4\n3 6\n10 8\n4 1\n0 10\n5 9\n8 3\n7 5" }, { "input": "10\n0 9\n4 0\n5 0\n7 2\n0 3\n8 10\n0 4\n0 6\n1 0\n6 0", "output": "0 9\n4 8\n5 7\n7 2\n9 3\n8 10\n3 4\n2 6\n1 5\n6 0" }, { "input": "10\n7 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 1\n0 0\n0 0\n0 0", "output": "7 8\n0 3\n2 4\n3 5\n4 6\n5 7\n6 1\n1 9\n8 10\n9 0" }, { "input": "10\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0", "output": "0 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 0" }, { "input": "100\n0 0\n0 0\n0 0\n97 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 29\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n12 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 4\n0 0\n0 0\n0 0", "output": "0 2\n1 3\n2 5\n97 98\n3 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 29\n29 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 30\n12 13\n28 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75..." }, { "input": "100\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 80\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n21 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0", "output": "0 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 80\n80 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 7..." }, { "input": "100\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0", "output": "0 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 51\n50 52\n51 53\n52 54\n53 55\n54 56\n55 57\n56 58\n57 59\n58 60\n59 61\n60 62\n61 63\n62 64\n63 65\n64 66\n65 67\n66 68\n67 69\n68 70\n69 71\n70 72\n71 73\n72 74\n73 75\n74 76\n75 7..." } ]
77
0
0
17,121
631
Messenger
[ "data structures", "hashing", "implementation", "string suffix structures", "strings" ]
null
null
Each employee of the "Blake Techologies" company uses a special messaging app "Blake Messenger". All the stuff likes this app and uses it constantly. However, some important futures are missing. For example, many users want to be able to search through the message history. It was already announced that the new feature will appear in the nearest update, when developers faced some troubles that only you may help them to solve. All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of *n* blocks, each block containing only equal characters. One block may be described as a pair (*l**i*,<=*c**i*), where *l**i* is the length of the *i*-th block and *c**i* is the corresponding letter. Thus, the string *s* may be written as the sequence of pairs . Your task is to write the program, that given two compressed string *t* and *s* finds all occurrences of *s* in *t*. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that *p* is the starting position of some occurrence of *s* in *t* if and only if *t**p**t**p*<=+<=1...*t**p*<=+<=|*s*|<=-<=1<==<=*s*, where *t**i* is the *i*-th character of string *t*. Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as , , ...
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=200<=000)Β β€” the number of blocks in the strings *t* and *s*, respectively. The second line contains the descriptions of *n* parts of string *t* in the format "*l**i*-*c**i*" (1<=≀<=*l**i*<=≀<=1<=000<=000)Β β€” the length of the *i*-th part and the corresponding lowercase English letter. The second line contains the descriptions of *m* parts of string *s* in the format "*l**i*-*c**i*" (1<=≀<=*l**i*<=≀<=1<=000<=000)Β β€” the length of the *i*-th part and the corresponding lowercase English letter.
Print a single integerΒ β€” the number of occurrences of *s* in *t*.
[ "5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c\n", "6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a\n", "5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d\n" ]
[ "1", "6", "0" ]
In the first sample, *t* = "aaabbccccaaacc", and string *s* = "aabbc". The only occurrence of string *s* in string *t* starts at position *p* = 2. In the second sample, *t* = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and *s* = "aaa". The occurrences of *s* in *t* start at positions *p* = 1, *p* = 10, *p* = 11, *p* = 12, *p* = 13 and *p* = 14.
[ { "input": "5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c", "output": "1" }, { "input": "6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a", "output": "6" }, { "input": "5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d", "output": "0" }, { "input": "9 3\n1-h 1-e 2-l 1-o 1-w 1-o 1-r 1-l 1-d\n2-l 1-o 1-w", "output": "1" }, { "input": "5 3\n1-m 1-i 2-r 1-o 1-r\n1-m 1-i 1-r", "output": "1" }, { "input": "9 2\n1-a 2-b 1-o 1-k 1-l 1-m 1-a 3-b 3-z\n1-a 2-b", "output": "2" }, { "input": "10 3\n1-b 1-a 2-b 1-a 1-b 1-a 4-b 1-a 1-a 2-b\n1-b 1-a 1-b", "output": "3" }, { "input": "4 2\n7-a 3-b 2-c 11-a\n3-a 4-a", "output": "6" }, { "input": "4 3\n8-b 2-a 7-b 3-a\n3-b 2-b 1-a", "output": "2" }, { "input": "1 1\n12344-a\n12345-a", "output": "0" }, { "input": "1 1\n5352-k\n5234-j", "output": "0" }, { "input": "1 1\n6543-o\n34-o", "output": "6510" }, { "input": "1 1\n1-z\n1-z", "output": "1" }, { "input": "5 2\n7-a 6-b 6-a 5-b 2-b\n6-a 7-b", "output": "1" }, { "input": "10 3\n7-a 1-c 6-b 1-c 8-a 1-c 8-b 6-a 2-c 5-b\n5-a 1-c 4-b", "output": "2" }, { "input": "4 2\n10-c 3-c 2-d 8-a\n6-a 1-b", "output": "0" }, { "input": "4 1\n10-a 2-b 8-d 11-e\n1-c", "output": "0" }, { "input": "28 7\n1-a 1-b 1-c 1-d 1-e 1-f 1-t 1-a 1-b 1-c 1-d 1-e 1-f 1-j 1-a 1-b 1-c 1-d 1-e 1-f 1-g 1-a 1-b 1-c 1-d 1-e 1-f 2-g\n1-a 1-b 1-c 1-d 1-e 1-f 1-g", "output": "2" }, { "input": "10 3\n2-w 4-l 2-w 4-l 2-w 5-l 2-w 6-l 3-w 3-l\n2-l 2-w 2-l", "output": "3" }, { "input": "15 7\n1-b 1-a 1-b 1-c 1-b 1-a 1-b 1-c 1-b 1-a 1-b 1-c 1-b 1-a 1-b\n1-b 1-a 1-b 1-c 1-b 1-a 1-b", "output": "3" }, { "input": "15 7\n1-b 2-a 1-b 1-c 1-b 1-a 1-b 1-c 1-b 2-a 1-b 1-c 1-b 1-a 1-b\n1-b 2-a 1-b 1-c 1-b 1-a 1-b", "output": "2" }, { "input": "2 2\n1-a 1-b\n2-a 1-b", "output": "0" }, { "input": "8 5\n1-a 1-b 1-c 1-a 2-b 1-c 1-a 1-b\n1-a 1-b 1-c 1-a 1-b", "output": "1" }, { "input": "9 5\n7-a 6-b 7-a 6-b 7-a 6-b 8-a 6-b 7-a\n7-a 6-b 7-a 6-b 7-a", "output": "2" } ]
2,000
28,876,800
0
17,159
0
none
[ "none" ]
null
null
This is an interactive problem. In the output section below you will see the information about flushing the output. On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in Vičkopolis. Upon arrival, they left the car in a huge parking lot near the restaurant and hurried inside the building. In the restaurant a polite waiter immediately brought the menu to Leha and Noora, consisting of *n* dishes. It is interesting that all dishes in the menu are numbered with integers from 1 to *n*. After a little thought, the girl ordered exactly *k* different dishes from available in the menu. To pass the waiting time while the chefs prepare ordered dishes, the girl invited the hacker to play a game that will help them get to know each other better. The game itself is very simple: Noora wants Leha to guess any two dishes among all ordered. At the same time, she is ready to answer only one type of questions. Leha can say two numbers *x* and *y* (1<=≀<=*x*,<=*y*<=≀<=*n*). After that Noora chooses some dish *a* for the number *x* such that, at first, *a* is among the dishes Noora ordered (*x* can be equal to *a*), and, secondly, the value is the minimum possible. By the same rules the girl chooses dish *b* for *y*. After that Noora says Β«TAKΒ» to Leha, if , and Β«NIEΒ» otherwise. However, the restaurant is preparing quickly, so Leha has enough time to ask no more than 60 questions. After that he should name numbers of any two dishes Noora ordered. Help Leha to solve this problem!
There are two numbers *n* and *k* (2<=≀<=*k*<=≀<=*n*<=≀<=105) in the single line of input denoting the number of dishes in the menu and the number of dishes Noora ordered.
If you want to provide an answer, output a string of the form 2 *x* *y* (1<=≀<=*x*,<=*y*<=≀<=*n*,<=*x*<=β‰ <=*y*), if you think the dishes *x* and *y* was among dishes ordered by Noora. After that, flush the output and terminate your program.
[ "3 2\nNIE\nTAK\nNIE\nTAK\nTAK\nTAK\n" ]
[ "1 1 2\n1 2 1\n1 1 3\n1 3 1\n1 2 3\n1 3 2\n2 2 3\n" ]
There are three dishes in sample. Noora ordered dished numberes 2 and 3, which Leha should guess. If Noora receive requests for the first dish (*x* = 1), then she'll choose the second dish (*a* = 2) as the dish with the minimum value <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5a4a1705bc256e413564795cc8b70857f0ae44c.png" style="max-width: 100.0%;max-height: 100.0%;"/>. For the second (*x* = 2) and the third (*x* = 3) dishes themselves will be optimal, because in that case <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/79add195d023131e992b60a354fec89501759e75.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Let Leha asks Noora about the next couple of dishes: - *x* = 1, *y* = 2, then he'll recieve Β«NIEΒ» answer, because |1 - 2| &gt; |2 - 2| - *x* = 2, *y* = 1, then he'll recieve Β«TAKΒ» answer, because |2 - 2| ≀ |1 - 2| - *x* = 1, *y* = 3, then he'll recieve Β«NIEΒ» answer, because |1 - 2| &gt; |3 - 3| - *x* = 3, *y* = 1, then he'll recieve Β«TAKΒ» answer, because |3 - 3| ≀ |1 - 2| - *x* = 2, *y* = 3, then he'll recieve Β«TAKΒ» answer, because |2 - 2| ≀ |3 - 3| - *x* = 3, *y* = 2, then he'll recieve Β«TAKΒ» answer, because |3 - 3| ≀ |2 - 2| According to the available information, it is possible to say that Nura ordered dishes with numbers 2 and 3.
[]
93
0
-1
17,168
269
Magical Boxes
[ "greedy", "math" ]
null
null
Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes. From the top view each magical box looks like a square with side length equal to 2*k* (*k* is an integer, *k*<=β‰₯<=0) units. A magical box *v* can be put inside a magical box *u*, if side length of *v* is strictly less than the side length of *u*. In particular, Emuskald can put 4 boxes of side length 2*k*<=-<=1 into one box of side length 2*k*, or as in the following figure: Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes.
The first line of input contains an integer *n* (1<=≀<=*n*<=≀<=105), the number of different sizes of boxes Emuskald has. Each of following *n* lines contains two integers *k**i* and *a**i* (0<=≀<=*k**i*<=≀<=109, 1<=≀<=*a**i*<=≀<=109), which means that Emuskald has *a**i* boxes with side length 2*k**i*. It is guaranteed that all of *k**i* are distinct.
Output a single integer *p*, such that the smallest magical box that can contain all of Emuskald’s boxes has side length 2*p*.
[ "2\n0 3\n1 5\n", "1\n0 4\n", "2\n1 10\n2 2\n" ]
[ "3\n", "1\n", "3\n" ]
Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture. In the second test case, we can put all four small boxes into a box with side length 2.
[ { "input": "2\n0 3\n1 5", "output": "3" }, { "input": "1\n0 4", "output": "1" }, { "input": "2\n1 10\n2 2", "output": "3" }, { "input": "1\n0 1", "output": "1" }, { "input": "1\n1000000000 1000000000", "output": "1000000015" }, { "input": "1\n0 16", "output": "2" }, { "input": "1\n0 17", "output": "3" }, { "input": "2\n0 16\n1 4", "output": "2" }, { "input": "3\n0 20\n1 18\n2 4", "output": "4" }, { "input": "3\n0 64\n1 16\n2 4", "output": "3" }, { "input": "5\n1 1000000\n100 100\n101 9\n102 4\n103 8", "output": "105" }, { "input": "10\n11 69\n7 56\n8 48\n2 56\n12 6\n9 84\n1 81\n4 80\n3 9\n5 18", "output": "15" }, { "input": "15\n0 268435456\n1 67108864\n2 16777216\n3 4194304\n4 1048576\n5 262144\n6 65536\n7 16384\n8 4096\n9 1024\n10 256\n11 64\n12 16\n13 4\n14 1", "output": "15" }, { "input": "14\n0 268435456\n1 67108864\n2 16777216\n3 4194304\n4 1048576\n5 262144\n6 65536\n7 16384\n8 4096\n9 1024\n10 256\n11 64\n12 16\n13 4", "output": "14" }, { "input": "14\n0 268435456\n1 67108864\n2 16777216\n3 4194305\n4 1048576\n5 262144\n6 65536\n7 16384\n8 4096\n9 1024\n10 256\n11 64\n12 16\n13 4", "output": "15" }, { "input": "14\n0 268435457\n1 67108865\n2 16777217\n3 4194305\n4 1048577\n5 262145\n6 65537\n7 16383\n8 4097\n9 1025\n10 257\n11 65\n12 17\n13 4", "output": "15" }, { "input": "16\n1296 2\n1568 1\n7435 2\n3660 1\n6863 2\n886 2\n2596 1\n7239 1\n6146 1\n5634 1\n3119 2\n1166 2\n7610 2\n5992 1\n630 2\n8491 2", "output": "8492" }, { "input": "17\n8028 11\n4011 32\n8609 52\n1440 25\n6752 42\n536 47\n761 15\n2749 60\n5363 62\n7170 23\n9734 10\n8487 28\n6147 20\n5257 54\n821 49\n7219 19\n6150 43", "output": "9736" }, { "input": "15\n999999986 268435456\n999999987 67108864\n999999988 16777216\n999999989 4194304\n999999990 1048576\n999999991 262144\n999999992 65536\n999999993 16384\n999999994 4096\n999999995 1024\n999999996 256\n999999997 64\n999999998 16\n999999999 4\n1000000000 1", "output": "1000000001" }, { "input": "3\n1 4\n0 17\n2 4", "output": "3" }, { "input": "2\n0 13\n1 1", "output": "2" }, { "input": "2\n0 2\n1 4", "output": "2" }, { "input": "2\n100 52\n101 4", "output": "103" }, { "input": "10\n1 1\n10 1\n100 1\n1000 1\n10000 1\n100000 1\n1000000 1\n10000000 1\n100000000 1\n1000000000 1", "output": "1000000001" }, { "input": "1\n0 268435456", "output": "14" }, { "input": "1\n0 268435457", "output": "15" }, { "input": "1\n1 1000000000", "output": "16" }, { "input": "9\n0 268435456\n2 16777216\n3 4194304\n6 65536\n7 16384\n8 4096\n11 64\n12 16\n14 1", "output": "15" }, { "input": "8\n0 268435456\n2 16777216\n3 4194304\n6 65536\n7 16384\n8 4096\n11 64\n12 16", "output": "14" }, { "input": "8\n0 268435456\n2 16777216\n3 4195304\n6 65536\n7 16384\n8 4096\n11 64\n12 16", "output": "15" }, { "input": "7\n1 16777216\n2 1000000000\n5 65537\n6 16384\n7 4096\n10 64\n11 16", "output": "17" }, { "input": "10\n0 689790649\n10 494030130\n6 210447956\n3 993638872\n7 116080273\n8 802686533\n2 677383122\n9 936407280\n4 593615642\n1 853659941", "output": "25" }, { "input": "2\n0 16777216\n16 1", "output": "17" }, { "input": "1\n1 1", "output": "2" }, { "input": "2\n21 134217728\n1 1024", "output": "35" }, { "input": "2\n10 3\n25 747", "output": "30" } ]
62
0
0
17,228
65
Harry Potter and the Sorting Hat
[ "brute force", "dfs and similar", "hashing" ]
D. Harry Potter and the Sorting Hat
2
256
As you know, Hogwarts has four houses: Gryffindor, Hufflepuff, Ravenclaw and Slytherin. The sorting of the first-years into houses is done by the Sorting Hat. The pupils are called one by one in the alphabetical order, each of them should put a hat on his head and, after some thought, the hat solemnly announces the name of the house the student should enter. At that the Hat is believed to base its considerations on the student's personal qualities: it sends the brave and noble ones to Gryffindor, the smart and shrewd ones β€” to Ravenclaw, the persistent and honest ones β€” to Hufflepuff and the clever and cunning ones β€” to Slytherin. However, a first year student Hermione Granger got very concerned about the forthcoming sorting. She studied all the literature on the Sorting Hat and came to the conclusion that it is much simpler than that. If the relatives of the student have already studied at Hogwarts, the hat puts the student to the same house, where his family used to study. In controversial situations, when the relatives studied in different houses or when they were all Muggles like Hermione's parents, then the Hat sorts the student to the house, to which the least number of first years has been sent at that moment. If there are several such houses, the choice is given to the student himself. Then the student can choose any of the houses, to which the least number of first years has been sent so far. Hermione has already asked the students that are on the list before her about their relatives. Now she and her new friends Harry Potter and Ron Weasley want to find out into what house the Hat will put Hermione.
The first input line contains an integer *n* (1<=≀<=*n*<=≀<=10000). It is the number of students who are in the list before Hermione. The next line contains *n* symbols. If all the relatives of a student used to study in the same house, then the *i*-th character in the string coincides with the first letter of the name of this house. Otherwise, the *i*-th symbol is equal to "?".
Print all the possible houses where Hermione can be sent. The names of the houses should be printed in the alphabetical order, one per line.
[ "11\nG????SS???H\n", "2\nH?\n" ]
[ "Gryffindor\nRavenclaw\n", "Gryffindor\nRavenclaw\nSlytherin\n" ]
Consider the second example. There are only two students before Hermione. The first student is sent to Hufflepuff. The second disciple is given the choice between the houses where the least number of students has been sent, i.e. Gryffindor, Slytherin and Ravenclaw. If he chooses Gryffindor, Hermione is forced to choose between Ravenclaw and Slytherin, if he chooses Ravenclaw, Hermione will choose between Gryffindor and Slytherin, if he chooses Slytherin, Hermione will choose between Gryffindor and Ravenclaw. In the end, the following situation is possible (it depends on the choice of the second student and Hermione). Hermione will end up 1) in Gryffindor, 2) in Ravenclaw, 3) in Slytherin. Note that, despite the fact that in neither case Hermione will be given a choice between all the three options, they are all possible and they should all be printed in the answer. Hermione will not, under any circumstances, end up in Hufflepuff.
[ { "input": "11\nG????SS???H", "output": "Gryffindor\nRavenclaw" }, { "input": "2\nH?", "output": "Gryffindor\nRavenclaw\nSlytherin" }, { "input": "1\n?", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "1\nG", "output": "Hufflepuff\nRavenclaw\nSlytherin" }, { "input": "3\nGHS", "output": "Ravenclaw" }, { "input": "4\n????", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "5\nGH??S", "output": "Gryffindor\nHufflepuff\nRavenclaw" }, { "input": "5\nH?S?G", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "7\n???????", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "10\n??HS??HSGR", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "55\n??RS?HGH?S?GS?HRGHRHRR?G?GSGSSHGS?HRSHRSR?HGHGRH?GGGHHH", "output": "Ravenclaw\nSlytherin" }, { "input": "108\n??RS?HGH?S?GS?HRGHRHRR?G?GSGSSHGS?HRSHRSR?HGHGRH??????????????RS?HGH?S?GS?HRGHRHRR?G?GSGSSHGS?HRSHRSR?HGHGRH", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "20\n?HH?RH???HG??G?RG?H?", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "20\nGSGHGRHHGRHRSGRRRHGS", "output": "Slytherin" }, { "input": "50\nS?HRHSGHRHS?GRRG?GSHHR?RRRHG?S?S?G?GR????GG?HS?HRG", "output": "Hufflepuff\nRavenclaw\nSlytherin" }, { "input": "50\n?SR?SRR?RGSH?R?RSHRSRR?GS?H??GH??????G?HG??G??RSRH", "output": "Gryffindor\nHufflepuff\nSlytherin" }, { "input": "50\n?GGGS???????????S?????G????H??SRG?????????G???????", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "50\n?????????S??????????????S?????????????????GS??????", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "50\n?SRSRRSRSHRRH?GHSHGGRSH?G?HRHH??RG?RHSGHRRGGHSGHHR", "output": "Slytherin" }, { "input": "50\n?SHGHRRGHGSGSR?G?HHHHHSHGGSRSSGHH?RHRRSRSRRGHS?HSS", "output": "Gryffindor\nRavenclaw" }, { "input": "100\nGSHRR?SRRRHGHGRGRGHGGHRHR?SSGGHRSGSGGGRRRRSSG?RRSRSGSGHSSGHRHRHSSRHSRSR??SRHSSHRGG?RRGHSSHSRGHSSRRHG", "output": "Gryffindor\nHufflepuff" }, { "input": "100\nGHHRHGS??H?GR?G?G?HHSR?SG?SR?SSSSS?G?G?RG?SHHHGR??SRHGGSRSHS?HR?HSGGSSHRR?G?RR?G?SRGGHSSSR?SGSGGS???", "output": "Gryffindor\nHufflepuff\nRavenclaw" }, { "input": "100\n??RH?S?HH?RGHR?HS?RHH?GRGRRGSSRSG??S???HSSS?S?SH??RGGSSGRHGGG?SGR??HRGRGH?R?GSGHSRHHRHSS??S?HGHRHHSG", "output": "Gryffindor\nRavenclaw\nSlytherin" }, { "input": "100\n?RSGG??R?GGGRRGHHH?SGRGRSRHGGSRS??RS?GHSRRSRRGHH?HG?GRGSHS?GSHGSGRGRRHHGHRSRG?SSG?SRHS?S??RHS?HGH?SH", "output": "Gryffindor\nRavenclaw" }, { "input": "100\nRSS??RSGGH?RGHRG?SGSHSH?HRR?R?SSG?GRRSRR???SHRG??R?SG??GSR??HR?HSSRHHRGGR?G?GR??SH?R??SHHRS?HHHG??SH", "output": "Gryffindor\nRavenclaw\nSlytherin" }, { "input": "100\nR?H?GGGHG?H?RGSS?GS??S??HS?S??GRRH?HSGRHRS?GS??GRS?SGRHSGS?HHGS???HG?GG?HRGRRG?SSGH??R?GR?SR?GH?HHR?", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "100\nHGR?RHS?HSS?RRH?R?GS?GG??S????GG???GRR?HSGG?H?SS?RSG?G?H?RGH?RS?GRSHRH?SGH??G??H?G?H?G?GSH?SRRHSGRR?", "output": "Gryffindor\nHufflepuff\nSlytherin" }, { "input": "100\n??SRHG?R??????S????RHR?GHGHH??H?SSHRR??R?GR?S?HGGR?H??S??G???H???SSR?R??R??GSHSRS?H??SRS???????SR??R", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" }, { "input": "100\nHR????G?HHRGH??S?R?HH?GH?GSG?R???GHH?HS?S?S?GR??R?HRHRGG?G?????S?H??HSSS??G??SG???S?S?S?RH?HR?HHHG??", "output": "Gryffindor\nRavenclaw\nSlytherin" }, { "input": "100\nSHRSR??SRGGGS???GRSRRSS??S??SH?GSSR?G?RHS?R?SRS??SS?G?G?H?S?GHR?GSGR?GRHR?H??SG???SSRH?GR??SSS?SS??H", "output": "Gryffindor\nHufflepuff\nRavenclaw" }, { "input": "20\n?G?S???R?S??HRHH???R", "output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin" } ]
61
3,276,800
0
17,255
321
Ciel and Duel
[ "dp", "flows", "greedy" ]
null
null
Fox Ciel is playing a card game with her friend Jiro. Jiro has *n* cards, each one has two attributes: *position* (Attack or Defense) and *strength*. Fox Ciel has *m* cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack. Now is Ciel's battle phase, Ciel can do the following operation many times: 1. Choose one of her cards *X*. This card mustn't be chosen before. 1. If Jiro has no alive cards at that moment, he gets the damage equal to (*X*'s strength). Otherwise, Ciel needs to choose one Jiro's alive card *Y*, then: If *Y*'s position is Attack, then (*X*'s strength) <=β‰₯<= (*Y*'s strength) must hold. After this attack, card *Y* dies, and Jiro gets the damage equal to (*X*'s strength) - (*Y*'s strength). 1. If *Y*'s position is Defense, then (*X*'s strength) <=&gt;<= (*Y*'s strength) must hold. After this attack, card *Y* dies, but Jiro gets no damage. Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get.
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=100) β€” the number of cards Jiro and Ciel have. Each of the next *n* lines contains a string *position* and an integer *strength* (0<=≀<=*strength*<=≀<=8000) β€” the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense. Each of the next *m* lines contains an integer *strength* (0<=≀<=*strength*<=≀<=8000) β€” the strength of Ciel's current card.
Output an integer: the maximal damage Jiro can get.
[ "2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500\n", "3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001\n", "2 4\nDEF 0\nATK 0\n0\n0\n1\n1\n" ]
[ "3000\n", "992\n", "1\n" ]
In the first test case, Ciel has 3 cards with same *strength*. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500 - 2000 = 500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500 + 2500 = 3000. In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001 - 100) + (101 - 10) = 992. In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card.
[ { "input": "2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500", "output": "3000" }, { "input": "3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001", "output": "992" }, { "input": "2 4\nDEF 0\nATK 0\n0\n0\n1\n1", "output": "1" }, { "input": "1 1\nATK 100\n99", "output": "0" }, { "input": "4 8\nDEF 100\nDEF 200\nDEF 300\nATK 100\n100\n101\n201\n301\n1\n1\n1\n1", "output": "201" }, { "input": "3 4\nDEF 100\nATK 200\nDEF 300\n101\n201\n301\n1", "output": "101" }, { "input": "4 4\nDEF 0\nDEF 0\nDEF 0\nATK 100\n100\n100\n100\n100", "output": "0" }, { "input": "10 7\nATK 1\nATK 2\nATK 3\nATK 4\nATK 5\nATK 6\nATK 7\nDEF 8\nDEF 9\nDEF 10\n1\n2\n3\n4\n5\n6\n7", "output": "12" }, { "input": "5 6\nDEF 0\nDEF 0\nDEF 0\nDEF 0\nDEF 0\n1\n1\n1\n1\n1\n1", "output": "1" }, { "input": "17 42\nDEF 4824\nDEF 4258\nDEF 4496\nATK 3932\nDEF 6130\nDEF 4005\nATK 5807\nDEF 4434\nDEF 5122\nATK 3904\nDEF 4617\nDEF 5329\nDEF 6169\nATK 4046\nATK 3612\nATK 5689\nDEF 5226\n735\n1278\n38\n1556\n312\n271\n850\n1511\n1196\n811\n1192\n387\n1470\n1441\n1330\n797\n477\n207\n1119\n1311\n527\n97\n1153\n1197\n1558\n1394\n82\n619\n494\n777\n765\n487\n1236\n581\n1403\n1012\n144\n1537\n1282\n973\n1507\n928", "output": "0" }, { "input": "5 25\nDEF 1568\nDEF 5006\nATK 4756\nDEF 1289\nDEF 1747\n3547\n1688\n1816\n3028\n1786\n3186\n3631\n3422\n1413\n2527\n2487\n3099\n2074\n2059\n1590\n1321\n3666\n2017\n1452\n2943\n1996\n2475\n1071\n1677\n2163", "output": "0" }, { "input": "21 35\nDEF 5009\nATK 2263\nATK 1391\nATK 1458\nATK 1576\nATK 2211\nATK 1761\nATK 1234\nATK 2737\nATK 2624\nATK 1140\nATK 1815\nATK 1756\nATK 1597\nATK 2192\nATK 960\nATK 2024\nATK 1954\nATK 2286\nATK 1390\nDEF 5139\n923\n1310\n1111\n820\n1658\n1158\n1902\n1715\n915\n826\n1858\n968\n982\n914\n1830\n1315\n972\n1061\n1774\n1097\n1333\n1743\n1715\n1375\n1801\n1772\n1879\n1311\n785\n1739\n1240\n971\n1259\n1603\n1808", "output": "3878" }, { "input": "13 14\nATK 2896\nATK 2919\nATK 2117\nATK 2423\nATK 2636\nATK 2003\nATK 2614\nATK 2857\nATK 2326\nATK 2958\nATK 2768\nATK 3017\nATK 2788\n3245\n3274\n3035\n3113\n2982\n3312\n3129\n2934\n3427\n3316\n3232\n3368\n3314\n3040", "output": "10399" }, { "input": "25 28\nATK 1267\nDEF 1944\nATK 1244\nATK 1164\nATK 1131\nDEF 1589\nDEF 1116\nDEF 1903\nATK 1162\nATK 1058\nDEF 1291\nDEF 1199\nDEF 754\nDEF 1726\nDEF 1621\nATK 1210\nDEF 939\nDEF 919\nDEF 978\nDEF 1967\nATK 1179\nDEF 1981\nATK 1088\nDEF 404\nATK 1250\n2149\n1969\n2161\n1930\n2022\n1901\n1982\n2098\n1993\n1977\n2021\n2038\n1999\n1963\n1889\n1992\n2062\n2025\n2081\n1995\n1908\n2097\n2034\n1993\n2145\n2083\n2133\n2143", "output": "15496" }, { "input": "34 9\nDEF 7295\nDEF 7017\nDEF 7483\nDEF 7509\nDEF 7458\nDEF 7434\nDEF 6981\nDEF 7090\nDEF 7298\nDEF 7134\nATK 737\nDEF 7320\nDEF 7228\nDEF 7323\nATK 786\nDEF 6895\nDEF 7259\nDEF 6921\nDEF 7373\nDEF 7505\nDEF 7421\nDEF 6930\nDEF 6890\nDEF 7507\nDEF 6964\nDEF 7418\nDEF 7098\nDEF 6867\nDEF 7229\nDEF 7162\nDEF 6987\nDEF 7043\nDEF 7230\nDEF 7330\n3629\n4161\n2611\n4518\n2357\n2777\n1923\n1909\n1738", "output": "7156" }, { "input": "10 25\nATK 3519\nATK 2186\nATK 3219\nATK 3116\nATK 2170\nATK 3236\nATK 3013\nDEF 1188\nATK 1914\nATK 2838\n1335\n725\n752\n1254\n414\n1653\n439\n784\n649\n477\n759\n1666\n417\n1316\n392\n799\n534\n1402\n515\n1334\n1435\n898\n1214\n1427\n1820", "output": "0" }, { "input": "26 36\nATK 657\nATK 1366\nDEF 226\nATK 1170\nATK 969\nATK 1633\nATK 610\nATK 1386\nATK 740\nDEF 496\nATK 450\nATK 1480\nATK 1094\nATK 875\nATK 845\nATK 1012\nATK 1635\nATK 657\nATK 1534\nATK 1602\nATK 1581\nDEF 211\nATK 946\nATK 1281\nATK 843\nATK 1442\n6364\n7403\n2344\n426\n1895\n863\n6965\n5025\n1159\n1873\n6792\n3331\n2171\n529\n1862\n6415\n4427\n7408\n4164\n917\n5892\n5595\n4841\n5311\n5141\n1154\n6415\n4059\n3850\n1681\n6068\n5081\n2325\n5122\n6942\n3247", "output": "117431" }, { "input": "2 12\nATK 3626\nATK 2802\n1160\n4985\n2267\n673\n2085\n3288\n1391\n2846\n4602\n2088\n3058\n3223", "output": "25238" }, { "input": "14 18\nDEF 102\nATK 519\nATK 219\nATK 671\nATK 1016\nATK 674\nATK 590\nATK 1005\nATK 514\nATK 851\nATK 273\nATK 928\nATK 1023\nATK 209\n2204\n2239\n2193\n2221\n2203\n2211\n2224\n2221\n2218\n2186\n2204\n2195\n2202\n2203\n2217\n2201\n2213\n2192", "output": "29069" }, { "input": "30 28\nDEF 5209\nATK 82\nDEF 4211\nDEF 2850\nATK 79\nATK 79\nDEF 4092\nDEF 5021\nATK 80\nDEF 5554\nDEF 2737\nDEF 4188\nATK 83\nATK 80\nDEF 4756\nATK 76\nDEF 3928\nDEF 5290\nATK 82\nATK 77\nDEF 3921\nDEF 3352\nDEF 2653\nATK 74\nDEF 4489\nDEF 5143\nDEF 3212\nATK 79\nDEF 4177\nATK 75\n195\n504\n551\n660\n351\n252\n389\n676\n225\n757\n404\n734\n203\n532\n382\n272\n621\n537\n311\n588\n609\n774\n669\n399\n382\n308\n230\n648", "output": "6878" }, { "input": "6 45\nATK 2374\nATK 2298\nATK 2591\nATK 2383\nATK 2523\nATK 2587\n2899\n3569\n3034\n3728\n3331\n3323\n3901\n3905\n2655\n2959\n3438\n3477\n4190\n3024\n3952\n3413\n3970\n3079\n3306\n3005\n4148\n4267\n4129\n4112\n4388\n3392\n3344\n2602\n4300\n3464\n4142\n3469\n4367\n4530\n3032\n3290\n3009\n3049\n4467\n4256\n3423\n2917\n3627\n2759\n4287", "output": "146172" }, { "input": "39 22\nDEF 5748\nDEF 5028\nDEF 1873\nDEF 6817\nDEF 5727\nDEF 4386\nDEF 4549\nDEF 5498\nDEF 1506\nDEF 2805\nATK 3186\nDEF 6202\nDEF 2129\nDEF 1646\nDEF 5367\nDEF 5754\nDEF 6195\nDEF 2109\nDEF 1837\nDEF 6575\nDEF 2842\nDEF 2970\nDEF 4494\nATK 3300\nDEF 4290\nDEF 6751\nDEF 3802\nDEF 5067\nDEF 1463\nDEF 3643\nDEF 6442\nDEF 4856\nDEF 4226\nDEF 3835\nDEF 1790\nDEF 5415\nDEF 6668\nDEF 5320\nDEF 1787\n252\n237\n304\n525\n99\n322\n280\n341\n215\n132\n303\n436\n80\n283\n400\n192\n425\n513\n138\n427\n514\n470", "output": "0" }, { "input": "6 42\nDEF 88\nDEF 92\nDEF 108\nDEF 94\nDEF 96\nDEF 78\n437\n1623\n2354\n2090\n802\n2500\n1512\n2691\n1521\n1087\n1415\n2081\n670\n1955\n3107\n2991\n1865\n2727\n1422\n2345\n2754\n1226\n3153\n3025\n1094\n2943\n2516\n1770\n1401\n590\n3292\n979\n840\n746\n1767\n696\n620\n2533\n2364\n2550\n916\n625", "output": "71957" }, { "input": "18 48\nATK 5377\nATK 5244\nATK 5213\nATK 5410\nATK 5094\nATK 5755\nDEF 5425\nATK 5215\nATK 5126\nDEF 5080\nDEF 5491\nATK 5671\nDEF 5409\nATK 5564\nDEF 5518\nDEF 5374\nATK 5182\nATK 5764\n1620\n1321\n1639\n837\n1705\n1076\n1106\n1395\n1008\n1610\n1047\n1414\n1944\n926\n1681\n904\n813\n1880\n1175\n1988\n976\n1679\n1051\n1800\n1714\n934\n951\n1282\n1224\n977\n759\n901\n1581\n1567\n1411\n1563\n1917\n751\n723\n1793\n1637\n1949\n1395\n1752\n1326\n1259\n1535\n1127", "output": "0" }, { "input": "34 10\nDEF 1740\nDEF 2236\nATK 3210\nATK 3468\nATK 4789\nDEF 1392\nATK 3639\nATK 1789\nDEF 2107\nDEF 1301\nDEF 2047\nDEF 1892\nATK 4845\nATK 4182\nATK 4504\nDEF 1557\nDEF 1537\nDEF 910\nATK 1548\nATK 3045\nATK 2660\nDEF 2097\nATK 2157\nDEF 2299\nDEF 2282\nATK 1956\nDEF 1812\nATK 3347\nDEF 1714\nATK 5446\nDEF 1326\nATK 3275\nDEF 907\nATK 3655\n1316\n1332\n1283\n1176\n939\n1175\n944\n1433\n1435\n1165", "output": "0" }, { "input": "10 27\nATK 7277\nATK 6269\nATK 7618\nDEF 4805\nDEF 4837\nDEF 4798\nDEF 4012\nATK 6353\nATK 7690\nATK 7653\n4788\n4860\n4837\n4528\n4826\n4820\n4921\n4678\n4924\n5070\n4961\n5007\n4495\n4581\n4748\n4480\n5176\n4589\n4998\n4660\n4575\n5090\n4540\n4750\n5136\n5118\n4667", "output": "0" }, { "input": "22 37\nDEF 3258\nDEF 3379\nATK 883\nATK 3945\nATK 4382\nATK 554\nDEF 3374\nDEF 3051\nDEF 2943\nATK 462\nATK 5098\nDEF 2986\nDEF 2957\nATK 1267\nATK 1296\nATK 4178\nDEF 2805\nDEF 3388\nATK 957\nDEF 3102\nDEF 3121\nATK 2875\n1366\n665\n561\n2503\n1329\n2353\n2529\n2932\n940\n2044\n2483\n575\n1980\n2930\n926\n2894\n1395\n577\n2813\n529\n327\n2911\n455\n948\n1076\n1741\n2668\n536\n481\n980\n1208\n2680\n2036\n1618\n2718\n2280\n711", "output": "11779" }, { "input": "2 13\nDEF 4509\nDEF 4646\n4842\n4315\n5359\n3477\n5876\n5601\n3134\n5939\n6653\n5673\n4473\n2956\n4127", "output": "52224" }, { "input": "14 23\nDEF 2361\nDEF 2253\nDEF 2442\nATK 2530\nDEF 2608\nDEF 2717\nDEF 2274\nDEF 2308\nATK 1200\nDEF 2244\nDEF 2678\nDEF 2338\nDEF 2383\nDEF 2563\n2640\n6118\n2613\n3441\n3607\n5502\n4425\n4368\n4059\n4264\n3979\n5098\n2413\n3564\n6118\n6075\n6049\n2524\n5245\n5004\n5560\n2877\n3450", "output": "55832" }, { "input": "23 49\nATK 3263\nATK 2712\nATK 3221\nATK 4441\nATK 4225\nATK 2120\nATK 3062\nATK 2246\nATK 4263\nATK 2850\nATK 3491\nATK 4248\nATK 3650\nATK 4444\nATK 3509\nATK 3254\nATK 4073\nATK 4263\nATK 4278\nATK 4747\nATK 2581\nATK 3355\nATK 4180\n516\n469\n494\n521\n536\n586\n482\n571\n502\n515\n537\n513\n503\n482\n512\n615\n607\n574\n561\n561\n514\n511\n617\n491\n511\n616\n578\n464\n459\n591\n518\n586\n596\n612\n540\n599\n558\n539\n514\n524\n463\n609\n532\n616\n620\n615\n538\n539\n553", "output": "0" }, { "input": "39 11\nDEF 5456\nATK 801\nDEF 4013\nATK 798\nATK 1119\nDEF 2283\nDEF 2400\nDEF 3847\nDEF 5386\nDEF 2839\nDEF 3577\nDEF 4050\nDEF 5623\nATK 1061\nDEF 4331\nDEF 4036\nDEF 5138\nDEF 4552\nATK 929\nDEF 3221\nDEF 3645\nDEF 3523\nATK 1147\nDEF 3490\nATK 1030\nDEF 2689\nATK 1265\nDEF 2533\nDEF 3181\nDEF 5582\nATK 790\nDEF 5623\nATK 1254\nATK 1145\nDEF 2873\nDEF 4117\nDEF 2589\nDEF 5471\nDEF 2977\n2454\n5681\n6267\n2680\n5560\n5394\n5419\n4350\n3803\n6003\n5502", "output": "41774" }, { "input": "15 35\nATK 5598\nATK 6155\nDEF 511\nDEF 534\nATK 5999\nATK 5659\nATK 6185\nATK 6269\nATK 5959\nATK 6176\nDEF 520\nATK 5602\nDEF 517\nATK 6422\nATK 6185\n2108\n2446\n2176\n1828\n2460\n2800\n1842\n2936\n1918\n2980\n2271\n2436\n2993\n2462\n2571\n2907\n2136\n1810\n2079\n2863\n2094\n1887\n2194\n2727\n2589\n2843\n2141\n2552\n1824\n3038\n2113\n2198\n2075\n2012\n2708", "output": "0" }, { "input": "20 20\nDEF 6409\nDEF 6327\nATK 2541\nDEF 6395\nDEF 6301\nATK 3144\nATK 3419\nDEF 6386\nATK 2477\nDEF 6337\nDEF 6448\nATK 3157\nATK 1951\nDEF 6345\nDEF 6368\nDEF 6352\nDEF 6348\nDEF 6430\nDEF 6456\nDEF 6380\n3825\n3407\n3071\n1158\n2193\n385\n1657\n86\n493\n2168\n3457\n1679\n3928\n3006\n1122\n190\n135\n3597\n2907\n2394", "output": "4944" }, { "input": "36 30\nATK 116\nATK 120\nATK 122\nATK 120\nATK 116\nATK 118\nATK 123\nDEF 2564\nATK 123\nDEF 1810\nATK 124\nATK 120\nDEF 2598\nATK 119\nDEF 2103\nATK 123\nATK 118\nATK 118\nATK 123\nDEF 1988\nATK 122\nATK 120\nDEF 2494\nATK 122\nATK 124\nATK 117\nATK 121\nATK 118\nATK 117\nATK 122\nATK 119\nATK 122\nDEF 2484\nATK 118\nATK 117\nATK 120\n1012\n946\n1137\n1212\n1138\n1028\n1181\n981\n1039\n1007\n900\n947\n894\n979\n1021\n1096\n1200\n937\n957\n1211\n1031\n881\n1122\n967\n1024\n972\n1193\n1092\n1177\n1101", "output": "27020" } ]
186
0
0
17,304
220
Little Elephant and Inversions
[ "data structures", "two pointers" ]
null
null
The Little Elephant has array *a*, consisting of *n* positive integers, indexed from 1 to *n*. Let's denote the number with index *i* as *a**i*. The Little Elephant wants to count, how many pairs of integers *l* and *r* are there, such that 1<=≀<=*l*<=&lt;<=*r*<=≀<=*n* and sequence *b*<==<=*a*1*a*2... *a**l**a**r**a**r*<=+<=1... *a**n* has no more than *k* inversions. An inversion in sequence *b* is a pair of elements of the sequence *b*, that change their relative order after a stable sorting of the sequence. In other words, an inversion is a pair of integers *i* and *j*, such that 1<=≀<=*i*<=&lt;<=*j*<=≀<=|*b*| and *b**i*<=&gt;<=*b**j*, where |*b*| is the length of sequence *b*, and *b**j* is its *j*-th element. Help the Little Elephant and count the number of the described pairs.
The first line contains two integers *n* and *k* (2<=≀<=*n*<=≀<=105,<=0<=≀<=*k*<=≀<=1018) β€” the size of array *a* and the maximum allowed number of inversions respectively. The next line contains *n* positive integers, separated by single spaces, *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” elements of array *a*. 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.
In a single line print a single number β€” the answer to the problem.
[ "3 1\n1 3 2\n", "5 2\n1 3 2 1 7\n" ]
[ "3\n", "6\n" ]
none
[ { "input": "3 1\n1 3 2", "output": "3" }, { "input": "5 2\n1 3 2 1 7", "output": "6" }, { "input": "7 3\n1 7 6 4 9 5 3", "output": "6" }, { "input": "5 0\n1 2 3 4 5", "output": "10" }, { "input": "2 1\n2 1", "output": "1" }, { "input": "3 1000000000000\n3 2 1", "output": "3" }, { "input": "10 5\n1 4 4 2 3 7 6 5 1 2", "output": "10" }, { "input": "10 10\n7 5 5 5 9 10 9 8 7 5", "output": "22" }, { "input": "7 1\n10 38 46 40 88 5 94", "output": "6" }, { "input": "20 7\n5 10 23 7 24 7 15 11 13 18 18 18 8 20 5 16 7 25 2 22", "output": "17" }, { "input": "40 1000000000000000000\n83 35 47 18 96 63 24 91 15 100 40 23 20 34 65 22 52 87 55 19 11 73 45 28 60 61 24 42 30 43 65 75 31 84 100 12 69 98 49 25", "output": "780" }, { "input": "74 9\n23 15 38 22 47 8 2 38 17 3 39 10 33 26 19 27 11 15 42 18 44 22 47 44 18 29 25 6 4 44 12 44 30 26 5 41 18 6 17 15 40 28 44 37 27 32 31 13 1 13 3 19 39 3 27 23 25 27 30 31 14 42 31 3 1 26 40 42 26 33 43 11 33 15", "output": "15" }, { "input": "100 25\n457 226 629 226 147 284 72 23 247 76 972 829 173 591 581 373 533 474 521 219 181 278 890 27 345 478 595 678 971 157 776 156 970 955 584 786 692 774 228 459 118 419 671 24 128 76 677 66 923 800 870 124 420 528 609 131 88 118 19 807 288 973 896 305 268 486 830 149 529 399 894 71 101 971 620 671 367 206 252 103 905 528 429 185 768 103 407 371 376 807 638 44 193 835 134 542 47 50 238 668", "output": "45" }, { "input": "128 1\n96 56 71 72 37 31 41 55 63 79 64 59 32 8 55 17 25 97 11 38 100 76 80 99 1 17 23 8 42 33 28 86 60 10 11 7 93 49 96 90 2 94 21 53 88 39 64 81 8 55 67 42 69 79 9 13 66 72 36 3 10 61 15 56 6 75 63 87 87 49 71 22 74 7 53 52 17 13 97 69 69 31 66 14 72 84 18 59 61 1 96 86 61 52 73 7 13 59 66 83 19 88 25 59 12 28 61 100 20 54 25 14 27 18 89 23 8 24 2 50 70 32 100 58 76 90 91 78", "output": "1" }, { "input": "50 1000000000\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 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": "1225" }, { "input": "6 10\n4 3 2 4 1 1", "output": "10" }, { "input": "2 2\n1 2", "output": "1" }, { "input": "2 0\n2 1", "output": "0" } ]
218
0
0
17,323
1,007
Mini Metro
[ "dp" ]
null
null
In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are $n$ stations on the line, $a_i$ people are waiting for the train at the $i$-th station at the beginning of the game. The game starts at the beginning of the $0$-th hour. At the end of each hour (couple minutes before the end of the hour), $b_i$ people instantly arrive to the $i$-th station. If at some moment, the number of people at the $i$-th station is larger than $c_i$, you lose. A player has several trains which he can appoint to some hours. The capacity of each train is $k$ passengers. In the middle of the appointed hour, the train goes from the $1$-st to the $n$-th station, taking as many people at each station as it can accommodate. A train can not take people from the $i$-th station if there are people at the $i-1$-th station. If multiple trains are appointed to the same hour, their capacities are being added up and they are moving together. The player wants to stay in the game for $t$ hours. Determine the minimum number of trains he will need for it.
The first line contains three integers $n$, $t$, and $k$ ($1 \leq n, t \leq 200, 1 \leq k \leq 10^9$)Β β€” the number of stations on the line, hours we want to survive, and capacity of each train respectively. Each of the next $n$ lines contains three integers $a_i$, $b_i$, and $c_i$ ($0 \leq a_i, b_i \leq c_i \leq 10^9$)Β β€” number of people at the $i$-th station in the beginning of the game, number of people arriving to $i$-th station in the end of each hour and maximum number of people at the $i$-th station allowed respectively.
Output a single integer numberΒ β€” the answer to the problem.
[ "3 3 10\n2 4 10\n3 3 9\n4 2 8\n", "4 10 5\n1 1 1\n1 0 1\n0 5 8\n2 7 100\n" ]
[ "2\n", "12\n" ]
<img class="tex-graphics" src="https://espresso.codeforces.com/bfa11d535d9fc44e73f6f8280d06436e4e327753.png" style="max-width: 100.0%;max-height: 100.0%;"/> Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacities of the stations are 10, 9, and 8 respectively. One of the winning strategies is to appoint two trains to the first and the third hours. Then on the first hour, the train takes all of the people from the stations, and at the end of the hour, 4 people arrive at the first station, 3 on the second, and 2 on the third. In the second hour there are no trains appointed, and at the end of it, the same amount of people are arriving again. In the third hour, the train first takes 8 people from the first station, and when it arrives at the second station, it takes only 2 people because it can accommodate no more than 10 people. Then it passes by the third station because it is already full. After it, people arrive at the stations once more, and the game ends. As there was no such moment when the number of people at a station exceeded maximal capacity, we won using two trains.
[]
31
0
0
17,454
852
Product transformation
[ "combinatorics", "math", "number theory" ]
null
null
Consider an array *A* with *N* elements, all being the same integer *a*. Define the product transformation as a simultaneous update *A**i*<==<=*A**i*Β·*A**i*<=+<=1, that is multiplying each element to the element right to it for , with the last number *A**N* remaining the same. For example, if we start with an array *A* with *a*<==<=2 and *N*<==<=4, then after one product transformation *A*<==<=[4,<= 4,<= 4,<= 2], and after two product transformations *A*<==<=[16,<= 16,<= 8,<= 2]. Your simple task is to calculate the array *A* after *M* product transformations. Since the numbers can get quite big you should output them modulo *Q*.
The first and only line of input contains four integers *N*, *M*, *a*, *Q* (7<=≀<=*Q*<=≀<=109<=+<=123, 2<=≀<=*a*<=≀<=106<=+<=123, , is prime), where is the multiplicative order of the integer *a* modulo *Q*, see notes for definition.
You should output the array *A* from left to right.
[ "2 2 2 7\n" ]
[ "1 2 " ]
The multiplicative order of a number *a* modulo *Q* <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/38b13c1f6db75ae72784f8602e8230429b26cf2a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, is the smallest natural number *x* such that *a*<sup class="upper-index">*x*</sup> *mod* *Q* = 1. For example, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/64b53b8160eddc004a5b65223bf29dd636bb1832.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[]
140
40,243,200
0
17,473
915
Coprime Arrays
[ "math", "number theory" ]
null
null
Let's call an array *a* of size *n* coprime iff *gcd*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=1, where *gcd* is the greatest common divisor of the arguments. You are given two numbers *n* and *k*. For each *i* (1<=≀<=*i*<=≀<=*k*) you have to determine the number of coprime arrays *a* of size *n* such that for every *j* (1<=≀<=*j*<=≀<=*n*) 1<=≀<=*a**j*<=≀<=*i*. Since the answers can be very large, you have to calculate them modulo 109<=+<=7.
The first line contains two integers *n* and *k* (1<=≀<=*n*,<=*k*<=≀<=2Β·106) β€” the size of the desired arrays and the maximum upper bound on elements, respectively.
Since printing 2Β·106 numbers may take a lot of time, you have to output the answer in such a way: Let *b**i* be the number of coprime arrays with elements in range [1,<=*i*], taken modulo 109<=+<=7. You have to print , taken modulo 109<=+<=7. Here denotes bitwise xor operation (^ in C++ or Java, xor in Pascal).
[ "3 4\n", "2000000 8\n" ]
[ "82\n", "339310063\n" ]
Explanation of the example: Since the number of coprime arrays is large, we will list the arrays that are non-coprime, but contain only elements in range [1, *i*]: For *i* = 1, the only array is coprime. *b*<sub class="lower-index">1</sub> = 1. For *i* = 2, array [2, 2, 2] is not coprime. *b*<sub class="lower-index">2</sub> = 7. For *i* = 3, arrays [2, 2, 2] and [3, 3, 3] are not coprime. *b*<sub class="lower-index">3</sub> = 25. For *i* = 4, arrays [2, 2, 2], [3, 3, 3], [2, 2, 4], [2, 4, 2], [2, 4, 4], [4, 2, 2], [4, 2, 4], [4, 4, 2] and [4, 4, 4] are not coprime. *b*<sub class="lower-index">4</sub> = 55.
[ { "input": "3 4", "output": "82" }, { "input": "2000000 8", "output": "339310063" }, { "input": "1000 1000", "output": "293255159" }, { "input": "400000 400000", "output": "641589365" }, { "input": "1000 2000", "output": "946090030" }, { "input": "400000 800000", "output": "700177418" }, { "input": "1000 3000", "output": "599681537" }, { "input": "400000 1200000", "output": "599314521" }, { "input": "1000 4000", "output": "369962559" }, { "input": "400000 1600000", "output": "383930351" }, { "input": "1000 5000", "output": "792761486" }, { "input": "400000 2000000", "output": "581254555" }, { "input": "2000 1000", "output": "895983599" }, { "input": "800000 400000", "output": "194918098" }, { "input": "2000 2000", "output": "993145929" }, { "input": "800000 800000", "output": "959396582" }, { "input": "2000 3000", "output": "801808235" }, { "input": "800000 1200000", "output": "456936609" }, { "input": "2000 4000", "output": "531261925" }, { "input": "800000 1600000", "output": "58127171" }, { "input": "2000 5000", "output": "460721768" }, { "input": "800000 2000000", "output": "607152577" }, { "input": "3000 1000", "output": "547188062" }, { "input": "1200000 400000", "output": "535771854" }, { "input": "3000 2000", "output": "612556768" }, { "input": "1200000 800000", "output": "925258449" }, { "input": "3000 3000", "output": "113732600" }, { "input": "1200000 1200000", "output": "468573037" }, { "input": "3000 4000", "output": "81501584" }, { "input": "1200000 1600000", "output": "549284678" }, { "input": "3000 5000", "output": "674745410" }, { "input": "1200000 2000000", "output": "340808789" }, { "input": "4000 1000", "output": "312642124" }, { "input": "1600000 400000", "output": "180485439" }, { "input": "4000 2000", "output": "467116350" }, { "input": "1600000 800000", "output": "990942172" }, { "input": "4000 3000", "output": "441359443" }, { "input": "1600000 1200000", "output": "186789429" }, { "input": "4000 4000", "output": "927028696" }, { "input": "1600000 1600000", "output": "373782836" }, { "input": "4000 5000", "output": "107116959" }, { "input": "1600000 2000000", "output": "160805957" }, { "input": "5000 1000", "output": "834732252" }, { "input": "2000000 400000", "output": "313670498" }, { "input": "5000 2000", "output": "273324715" }, { "input": "2000000 800000", "output": "204745061" }, { "input": "5000 3000", "output": "649058184" }, { "input": "2000000 1200000", "output": "749062788" }, { "input": "5000 4000", "output": "332100759" }, { "input": "2000000 1600000", "output": "267687597" }, { "input": "5000 5000", "output": "785334012" }, { "input": "2000000 2000000", "output": "219072617" }, { "input": "1 1", "output": "0" }, { "input": "1 2000000", "output": "986000" }, { "input": "2000000 1", "output": "0" } ]
2,948
51,916,800
3
17,484
216
Hiring Staff
[ "greedy" ]
null
null
A new Berland businessman Vitaly is going to open a household appliances' store. All he's got to do now is to hire the staff. The store will work seven days a week, but not around the clock. Every day at least *k* people must work in the store. Berland has a law that determines the order of working days and non-working days. Namely, each employee must work for exactly *n* consecutive days, then rest for exactly *m* days, then work for *n* more days and rest for *m* more, and so on. Vitaly doesn't want to break the law. Fortunately, there is a loophole: the law comes into force on the day when the employee is hired. For example, if an employee is hired on day *x*, then he should work on days [*x*,<=*x*<=+<=1,<=...,<=*x*<=+<=*n*<=-<=1], [*x*<=+<=*m*<=+<=*n*,<=*x*<=+<=*m*<=+<=*n*<=+<=1,<=...,<=*x*<=+<=*m*<=+<=2*n*<=-<=1], and so on. Day *x* can be chosen arbitrarily by Vitaly. There is one more thing: the key to the store. Berland law prohibits making copies of keys, so there is only one key. Vitaly is planning to entrust the key to the store employees. At the same time on each day the key must be with an employee who works that day β€” otherwise on this day no one can get inside the store. During the day the key holder can give the key to another employee, if he also works that day. The key will handed to the first hired employee at his first working day. Each employee has to be paid salary. Therefore, Vitaly wants to hire as few employees as possible provided that the store can operate normally on each day from 1 to infinity. In other words, on each day with index from 1 to infinity, the store must have at least *k* working employees, and one of the working employees should have the key to the store. Help Vitaly and determine the minimum required number of employees, as well as days on which they should be hired.
The first line contains three integers *n*, *m* and *k* (1<=≀<=*m*<=≀<=*n*<=≀<=1000, *n*<=β‰ <=1, 1<=≀<=*k*<=≀<=1000).
In the first line print a single integer *z* β€” the minimum required number of employees. In the second line print *z* positive integers, separated by spaces: the *i*-th integer *a**i* (1<=≀<=*a**i*<=≀<=104) should represent the number of the day, on which Vitaly should hire the *i*-th employee. If there are multiple answers, print any of them.
[ "4 3 2\n", "3 3 1\n" ]
[ "4\n1 1 4 5", "3\n1 3 5" ]
none
[ { "input": "4 3 2", "output": "4\n1 1 4 5" }, { "input": "3 3 1", "output": "3\n1 3 5" }, { "input": "5 5 3", "output": "7\n1 1 1 5 6 6 7" }, { "input": "7 6 8", "output": "16\n1 1 1 1 1 1 1 1 7 7 7 7 7 7 7 8" }, { "input": "8 3 2", "output": "4\n1 1 8 9" }, { "input": "5 1 1", "output": "2\n1 5" }, { "input": "6 5 1", "output": "3\n1 6 7" }, { "input": "2 1 1", "output": "3\n1 2 3" }, { "input": "2 2 1", "output": "4\n1 2 3 4" }, { "input": "3 1 1", "output": "2\n1 3" }, { "input": "3 2 1", "output": "3\n1 3 4" }, { "input": "3 3 1", "output": "3\n1 3 5" }, { "input": "4 1 1", "output": "2\n1 4" }, { "input": "4 2 1", "output": "2\n1 4" }, { "input": "4 3 1", "output": "3\n1 4 5" }, { "input": "4 4 1", "output": "3\n1 4 6" }, { "input": "183 1 1", "output": "2\n1 183" }, { "input": "284 282 1", "output": "2\n1 284" }, { "input": "359 358 1", "output": "3\n1 359 360" }, { "input": "801 801 1", "output": "3\n1 801 803" }, { "input": "1000 1 1", "output": "2\n1 1000" }, { "input": "1000 999 1", "output": "3\n1 1000 1001" }, { "input": "1000 1000 1", "output": "3\n1 1000 1002" }, { "input": "2 1 2", "output": "4\n1 1 2 3" }, { "input": "2 2 2", "output": "5\n1 1 2 3 4" }, { "input": "3 1 2", "output": "4\n1 1 3 4" }, { "input": "3 2 2", "output": "4\n1 1 3 4" }, { "input": "3 3 2", "output": "5\n1 1 3 4 5" }, { "input": "4 1 2", "output": "4\n1 1 4 5" }, { "input": "4 2 2", "output": "4\n1 1 4 5" }, { "input": "4 3 2", "output": "4\n1 1 4 5" }, { "input": "4 4 2", "output": "5\n1 1 4 5 6" }, { "input": "17 1 3", "output": "6\n1 1 1 17 17 18" }, { "input": "38 15 10", "output": "20\n1 1 1 1 1 1 1 1 1 1 38 38 38 38 38 38 38 38 38 39" }, { "input": "193 192 113", "output": "226\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 193 ..." }, { "input": "384 384 384", "output": "769\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ..." }, { "input": "1000 1 2", "output": "4\n1 1 1000 1001" }, { "input": "1000 485 2", "output": "4\n1 1 1000 1001" }, { "input": "1000 999 2", "output": "4\n1 1 1000 1001" }, { "input": "1000 1000 2", "output": "5\n1 1 1000 1001 1002" }, { "input": "1000 1 183", "output": "366\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000 ..." }, { "input": "1000 2 855", "output": "1710\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "1000 245 583", "output": "1166\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "1000 999 475", "output": "950\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ..." }, { "input": "1000 1000 334", "output": "669\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ..." }, { "input": "2 1 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "2 2 1000", "output": "2001\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "3 1 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "3 2 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "3 3 1000", "output": "2001\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "4 1 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "4 2 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "4 3 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "4 4 1000", "output": "2001\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "385 1 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "283 34 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "347 346 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "485 485 1000", "output": "2001\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "1000 999 1000", "output": "2000\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "1000 1000 1000", "output": "2001\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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1..." }, { "input": "2 2 5", "output": "11\n1 1 1 1 1 2 3 3 3 3 4" } ]
1,122
307,200
3
17,574
87
Interesting Game
[ "dp", "games", "math" ]
C. Interesting Game
2
256
Two best friends Serozha and Gena play a game. Initially there is one pile consisting of *n* stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of *a*1<=&gt;<=*a*2<=&gt;<=...<=&gt;<=*a**k*<=&gt;<=0 stones. The piles should meet the condition *a*1<=-<=*a*2<==<=*a*2<=-<=*a*3<==<=...<==<=*a**k*<=-<=1<=-<=*a**k*<==<=1. Naturally, the number of piles *k* should be no less than two. The friends play in turns. The player who cannot make a move loses. Serozha makes the first move. Who will win if both players play in the optimal way?
The single line contains a single integer *n* (1<=≀<=*n*<=≀<=105).
If Serozha wins, print *k*, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game. If Gena wins, print "-1" (without the quotes).
[ "3\n", "6\n", "100\n" ]
[ "2\n", "-1\n", "8\n" ]
none
[ { "input": "3", "output": "2" }, { "input": "6", "output": "-1" }, { "input": "100", "output": "8" }, { "input": "33", "output": "2" }, { "input": "23", "output": "-1" }, { "input": "35", "output": "-1" }, { "input": "15", "output": "2" }, { "input": "99", "output": "2" }, { "input": "46", "output": "4" }, { "input": "78", "output": "4" }, { "input": "627", "output": "2" }, { "input": "250", "output": "5" }, { "input": "873", "output": "18" }, { "input": "871", "output": "-1" }, { "input": "684", "output": "-1" }, { "input": "303", "output": "2" }, { "input": "93764", "output": "-1" }, { "input": "39509", "output": "-1" }, { "input": "70878", "output": "-1" }, { "input": "7578", "output": "3" }, { "input": "31893", "output": "3" }, { "input": "57113", "output": "2" }, { "input": "66873", "output": "2" }, { "input": "9564", "output": "3" }, { "input": "42237", "output": "18" }, { "input": "92763", "output": "22" }, { "input": "38798", "output": "76" }, { "input": "63359", "output": "34" }, { "input": "573", "output": "3" }, { "input": "60879", "output": "2" }, { "input": "67341", "output": "2" }, { "input": "15748", "output": "8" }, { "input": "42602", "output": "17" }, { "input": "67817", "output": "73" }, { "input": "81207", "output": "6" }, { "input": "8149", "output": "2" }, { "input": "95298", "output": "4" }, { "input": "41385", "output": "15" }, { "input": "27443", "output": "2" }, { "input": "74424", "output": "21" }, { "input": "35708", "output": "-1" }, { "input": "36655", "output": "-1" }, { "input": "34378", "output": "-1" }, { "input": "63478", "output": "-1" }, { "input": "42863", "output": "-1" }, { "input": "19715", "output": "-1" }, { "input": "37317", "output": "-1" }, { "input": "96992", "output": "-1" }, { "input": "56056", "output": "-1" }, { "input": "45899", "output": "-1" }, { "input": "1", "output": "-1" }, { "input": "100000", "output": "-1" }, { "input": "56", "output": "-1" }, { "input": "38", "output": "-1" }, { "input": "1515", "output": "2" } ]
2,000
2,355,200
0
17,598
14
Two Paths
[ "dfs and similar", "dp", "graphs", "shortest paths", "trees", "two pointers" ]
D. Two Paths
2
64
As you know, Bob's brother lives in Flatland. In Flatland there are *n* cities, connected by *n*<=-<=1 two-way roads. The cities are numbered from 1 to *n*. You can get from one city to another moving along the roads. The Β«Two PathsΒ» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities). It is known that the profit, the Β«Two PathsΒ» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company.
The first line contains an integer *n* (2<=≀<=*n*<=≀<=200), where *n* is the amount of cities in the country. The following *n*<=-<=1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road *a**i*,<=*b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*).
Output the maximum possible profit.
[ "4\n1 2\n2 3\n3 4\n", "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n", "6\n1 2\n2 3\n2 4\n5 4\n6 4\n" ]
[ "1\n", "0\n", "4\n" ]
none
[ { "input": "4\n1 2\n2 3\n3 4", "output": "1" }, { "input": "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7", "output": "0" }, { "input": "6\n1 2\n2 3\n2 4\n5 4\n6 4", "output": "4" }, { "input": "2\n2 1", "output": "0" }, { "input": "3\n3 1\n1 2", "output": "0" }, { "input": "3\n1 3\n2 1", "output": "0" }, { "input": "4\n4 2\n2 3\n2 1", "output": "0" }, { "input": "4\n2 3\n1 3\n2 4", "output": "1" }, { "input": "4\n3 2\n3 4\n1 4", "output": "1" }, { "input": "5\n1 5\n5 2\n4 2\n3 1", "output": "2" }, { "input": "5\n2 4\n2 5\n1 5\n2 3", "output": "2" }, { "input": "5\n1 2\n5 1\n3 2\n3 4", "output": "2" }, { "input": "5\n5 3\n3 1\n4 1\n4 2", "output": "2" }, { "input": "6\n1 2\n2 5\n4 5\n4 6\n3 2", "output": "4" }, { "input": "7\n1 6\n4 6\n5 6\n6 7\n2 6\n3 7", "output": "2" }, { "input": "8\n7 2\n7 1\n6 7\n4 1\n7 3\n6 8\n2 5", "output": "4" }, { "input": "8\n8 6\n1 8\n7 8\n3 1\n2 6\n5 3\n8 4", "output": "6" }, { "input": "9\n8 4\n7 8\n6 4\n8 3\n1 4\n3 9\n5 7\n2 5", "output": "10" }, { "input": "9\n4 7\n5 4\n2 7\n5 6\n3 7\n7 1\n9 2\n8 3", "output": "8" }, { "input": "10\n7 6\n6 8\n10 7\n5 10\n5 3\n2 8\n4 5\n1 7\n4 9", "output": "12" }, { "input": "10\n10 7\n7 5\n10 8\n6 5\n7 2\n9 7\n1 10\n3 5\n4 10", "output": "6" }, { "input": "15\n15 1\n15 10\n11 1\n1 13\n10 12\n1 8\n15 9\n14 13\n10 2\n7 10\n5 15\n8 4\n11 3\n6 15", "output": "12" }, { "input": "15\n10 12\n12 4\n15 12\n15 6\n5 15\n10 1\n8 15\n13 12\n14 6\n8 3\n11 5\n12 7\n15 9\n2 7", "output": "16" }, { "input": "15\n13 14\n10 14\n5 10\n10 6\n9 10\n10 7\n15 6\n8 7\n2 6\n1 10\n3 1\n3 11\n4 3\n14 12", "output": "10" }, { "input": "30\n2 4\n14 2\n2 3\n23 14\n30 2\n6 14\n13 4\n24 30\n17 30\n25 2\n26 23\n28 3\n6 8\n23 29\n18 25\n10 2\n25 7\n9 26\n6 27\n13 12\n22 3\n1 28\n11 10\n25 20\n30 19\n16 14\n22 5\n21 30\n15 18", "output": "30" }, { "input": "50\n7 34\n5 34\n5 11\n11 23\n42 5\n41 11\n12 41\n41 49\n1 49\n12 6\n7 15\n17 42\n20 6\n17 46\n20 19\n46 22\n46 40\n44 40\n43 46\n22 8\n17 29\n44 18\n31 18\n46 9\n7 16\n32 11\n13 41\n20 36\n34 25\n46 28\n39 34\n30 42\n11 47\n45 15\n37 17\n4 23\n35 17\n17 48\n2 17\n34 24\n1 10\n21 5\n2 3\n50 16\n33 5\n20 14\n26 19\n16 27\n38 43", "output": "88" }, { "input": "5\n1 2\n2 3\n3 4\n4 5", "output": "2" } ]
92
0
0
17,613
24
F1 Champions
[ "implementation" ]
B. F1 Champions
2
256
Formula One championship consists of series of races called Grand Prix. After every race drivers receive points according to their final position. Only the top 10 drivers receive points in the following order 25, 18, 15, 12, 10, 8, 6, 4, 2, 1. At the conclusion of the championship the driver with most points is the champion. If there is a tie, champion is the one with most wins (i.e. first places). If a tie still exists, it is chosen the one with most second places, and so on, until there are no more place to use for compare. Last year another scoring system was proposed but rejected. In it the champion is the one with most wins. If there is tie, champion is the one with most points. If a tie still exists it is proceeded the same way as in the original scoring system, that is comparing number of second, third, forth, and so on, places. You are given the result of all races during the season and you are to determine the champion according to both scoring systems. It is guaranteed, that both systems will produce unique champion.
The first line contain integer *t* (1<=≀<=*t*<=≀<=20), where *t* is the number of races. After that all races are described one by one. Every race description start with an integer *n* (1<=≀<=*n*<=≀<=50) on a line of itself, where *n* is the number of clasified drivers in the given race. After that *n* lines follow with the classification for the race, each containing the name of a driver. The names of drivers are given in order from the first to the last place. The name of the driver consists of lowercase and uppercase English letters and has length at most 50 characters. Comparing of names should be case-sensetive.
Your output should contain exactly two line. On the first line is the name of the champion according to the original rule, and on the second line the name of the champion according to the alternative rule.
[ "3\n3\nHamilton\nVettel\nWebber\n2\nWebber\nVettel\n2\nHamilton\nVettel\n", "2\n7\nProst\nSurtees\nNakajima\nSchumacher\nButton\nDeLaRosa\nBuemi\n8\nAlonso\nProst\nNinoFarina\nJimClark\nDeLaRosa\nNakajima\nPatrese\nSurtees\n" ]
[ "Vettel\nHamilton\n", "Prost\nProst\n" ]
It is not guaranteed that the same drivers participate in all races. For the championship consider every driver that has participated in at least one race. The total number of drivers during the whole season is not more then 50.
[ { "input": "3\n3\nHamilton\nVettel\nWebber\n2\nWebber\nVettel\n2\nHamilton\nVettel", "output": "Vettel\nHamilton" }, { "input": "2\n7\nProst\nSurtees\nNakajima\nSchumacher\nButton\nDeLaRosa\nBuemi\n8\nAlonso\nProst\nNinoFarina\nJimClark\nDeLaRosa\nNakajima\nPatrese\nSurtees", "output": "Prost\nProst" }, { "input": "5\n3\nWebber\nTrulli\nJones\n9\nVilleneuve\nBerger\nJimClark\nReneArnoux\nKubica\nJones\nScheckter\nKobayashi\nJamesHunt\n10\nTrulli\nJackBrabham\nKobayashi\nNakajima\nAndretti\nScheckter\nDeLaRosa\nReneArnoux\nKovalainen\nJones\n3\nBerger\nJimClark\nTrulli\n15\nNakajima\nVilleneuve\nBerger\nTrulli\nJamesHunt\nMassa\nReneArnoux\nKubica\nJimClark\nDeLaRosa\nJackBrabham\nHill\nKobayashi\ndiGrassi\nJones", "output": "Trulli\nTrulli" }, { "input": "2\n18\nKubica\nHawthorn\nSurtees\nBerger\nKobayashi\nWebber\nJackBrabham\nJimClark\nPatrese\nJones\nHakkinen\nJackieStewert\nMensel\nSenna\nHamilton\nAlonso\nHulkenberg\nBarichelo\n9\nHawthorn\nSutil\nBarichelo\nJackieStewert\nJones\nScheckter\nPiquet\nLiuzzi\nKovalainen", "output": "Hawthorn\nHawthorn" }, { "input": "6\n2\nAlbertoAscari\nHamilton\n5\nScheckter\nAlguersuari\nVettel\nPetrov\nProst\n5\nAlbertoAscari\nScheckter\nAlguersuari\nVettel\nHamilton\n3\nScheckter\nHamilton\nAlguersuari\n3\nAlbertoAscari\nScheckter\nProst\n4\nAlbertoAscari\nAlguersuari\nScheckter\nHamilton", "output": "Scheckter\nAlbertoAscari" }, { "input": "15\n3\nSenna\nFittipaldi\nLauda\n2\nSenna\nFittipaldi\n2\nFittipaldi\nLauda\n2\nLauda\nSenna\n3\nFittipaldi\nSenna\nLauda\n1\nSenna\n3\nSenna\nFittipaldi\nLauda\n1\nLauda\n2\nLauda\nSenna\n3\nLauda\nSenna\nFittipaldi\n2\nLauda\nSenna\n1\nSenna\n3\nFittipaldi\nLauda\nSenna\n3\nLauda\nFittipaldi\nSenna\n3\nLauda\nSenna\nFittipaldi", "output": "Senna\nLauda" }, { "input": "11\n4\nLauda\nRosberg\nBerger\nBarichelo\n4\nGlock\nPatrese\nBarichelo\nLauda\n7\nGlock\nBarichelo\nSchumacher\nLauda\nPatrese\nBerger\nRosberg\n4\nBerger\nRosberg\nPatrese\nBarichelo\n3\nSchumacher\nGlock\nPatrese\n1\nBarichelo\n1\nBarichelo\n5\nRosberg\nBerger\nGlock\nBarichelo\nPatrese\n1\nBerger\n3\nBerger\nSchumacher\nLauda\n4\nRosberg\nSchumacher\nBarichelo\nLauda", "output": "Barichelo\nBerger" }, { "input": "2\n11\nHamilton\nVettel\nWebber\nButton\nKubica\nPetrov\nRoseberg\nSchumacher\nAlonson\nMassa\nSenna\n10\nSenna\nKobayashi\nDeLaRosa\nHakkinen\nRaikkonen\nProst\nJimClark\nNakajima\nBerger\nAlbertoAscari", "output": "Senna\nSenna" }, { "input": "2\n10\nHamilton\nVettel\nWebber\nButton\nKubica\nPetrov\nRoseberg\nSchumacher\nAlonson\nMassa\n11\nSenna\nKobayashi\nDeLaRosa\nHakkinen\nRaikkonen\nProst\nJimClark\nNakajima\nBerger\nAlbertoAscari\nHamilton", "output": "Hamilton\nHamilton" }, { "input": "1\n50\nJamesHunt\nNakajima\nHakkinen\nScheckter\nJones\nHill\nHawthorn\nVettel\nHulkenberg\nSutil\nGlock\nWebber\nAlbertoAscari\nMensel\nBuemi\nKubica\ndiGrassi\nJimClark\nKovalainen\nTrulli\nBarichelo\nSurtees\nJackieStewert\nLauda\nRaikkonen\nVilleneuve\nFangio\nButton\nDeLaRosa\nProst\nRosberg\nAlonso\nBerger\nPatrese\nHamilton\nJochenRindt\nNinoFarina\nKobayashi\nFittipaldi\nAlguersuari\nDennyHulme\nSchumacher\nPetrov\nPiquet\nAndretti\nJackBrabham\nMassa\nSenna\nLiuzzi\nReneArnoux", "output": "JamesHunt\nJamesHunt" }, { "input": "1\n1\nA", "output": "A\nA" }, { "input": "2\n1\nA\n1\nA", "output": "A\nA" }, { "input": "2\n2\nA\nB\n2\nA\nC", "output": "A\nA" }, { "input": "3\n2\nA\nB\n2\nA\nD\n2\nA\nC", "output": "A\nA" }, { "input": "20\n4\nab\nb\nba\na\n4\na\nba\nb\nab\n4\nb\nba\nab\na\n4\nab\nb\na\nba\n4\nab\nb\nba\na\n4\nb\na\nab\nba\n4\nba\nb\nab\na\n4\nba\nab\na\nb\n4\nab\nb\na\nba\n4\nb\nba\nab\na\n4\na\nab\nba\nb\n4\na\nab\nb\nba\n4\nab\na\nba\nb\n4\nba\nb\na\nab\n4\na\nab\nba\nb\n4\nb\nba\na\nab\n4\nb\nab\na\nba\n4\na\nb\nba\nab\n4\nba\nb\na\nab\n4\nb\na\nba\nab", "output": "b\nb" }, { "input": "20\n4\na\nba\nb\nab\n4\nb\nba\nab\na\n4\nab\nb\na\nba\n4\nab\nb\nba\na\n4\nb\na\nab\nba\n4\nba\nb\nab\na\n4\nba\nab\na\nb\n4\nab\nb\na\nba\n4\nb\nba\nab\na\n4\na\nab\nba\nb\n4\na\nab\nb\nba\n4\nab\na\nba\nb\n4\nba\nb\na\nab\n4\na\nab\nba\nb\n4\nb\nba\na\nab\n4\nb\nab\na\nba\n4\na\nb\nba\nab\n4\nba\nb\na\nab\n4\nb\na\nba\nab\n4\nba\nab\na\nb", "output": "b\nb" }, { "input": "20\n4\nb\nba\nab\na\n4\nba\nab\na\nb\n4\nab\na\nb\nba\n4\nb\na\nab\nba\n4\na\nb\nab\nba\n4\nba\nab\nb\na\n4\nab\nb\na\nba\n4\nab\na\nba\nb\n4\nab\nba\na\nb\n4\nb\na\nab\nba\n4\nba\nb\na\nab\n4\na\nb\nba\nab\n4\nab\na\nb\nba\n4\nba\nb\nab\na\n4\nba\nab\na\nb\n4\nb\nba\nab\na\n4\na\nab\nba\nb\n4\nab\nba\nb\na\n4\nba\na\nab\nb\n4\nb\nba\na\nab", "output": "ab\nab" }, { "input": "20\n4\nba\nab\nb\na\n4\nab\nb\nba\na\n4\nb\na\nba\nab\n4\na\nba\nb\nab\n4\na\nab\nb\nba\n4\nab\nba\na\nb\n4\na\nba\nb\nab\n4\nb\na\nba\nab\n4\nb\nab\na\nba\n4\na\nab\nb\nba\n4\na\nb\nba\nab\n4\nab\nb\nba\na\n4\na\nb\nab\nba\n4\nb\nab\nba\na\n4\nab\nba\nb\na\n4\nb\nab\nba\na\n4\nab\nba\nb\na\n4\nab\na\nb\nba\n4\nb\nab\na\nba\n4\nba\nab\na\nb", "output": "ab\nab" }, { "input": "20\n4\nb\nab\nba\na\n4\nb\nab\nba\na\n4\na\nba\nb\nab\n4\nab\nb\nba\na\n4\na\nab\nb\nba\n4\nb\nab\nba\na\n4\nba\nb\na\nab\n4\nab\nb\nba\na\n4\na\nba\nab\nb\n4\nab\na\nba\nb\n4\nb\na\nba\nab\n4\nab\nb\na\nba\n4\na\nb\nab\nba\n4\na\nba\nb\nab\n4\nab\nb\nba\na\n4\nab\na\nb\nba\n4\nb\nab\nba\na\n4\nab\nba\nb\na\n4\nb\na\nba\nab\n4\na\nb\nba\nab", "output": "b\nab" }, { "input": "20\n4\nab\nb\nba\na\n4\nba\nb\nab\na\n4\na\nba\nb\nab\n4\nab\nba\nb\na\n4\nba\nb\na\nab\n4\nb\na\nba\nab\n4\nba\na\nab\nb\n4\nab\nb\na\nba\n4\nb\na\nba\nab\n4\nb\nba\nab\na\n4\nab\nb\na\nba\n4\nb\nba\na\nab\n4\na\nba\nb\nab\n4\nab\nba\nb\na\n4\nb\nab\na\nba\n4\nba\nb\na\nab\n4\nba\nab\nb\na\n4\nab\nb\na\nba\n4\nab\nb\na\nba\n4\nab\nb\na\nba", "output": "b\nab" }, { "input": "20\n4\na\nab\nb\nba\n4\nab\nb\na\nba\n4\nb\na\nab\nba\n4\nab\nba\na\nb\n4\nb\na\nba\nab\n4\nab\nba\na\nb\n4\nba\nb\na\nab\n4\nba\nb\na\nab\n4\nab\na\nb\nba\n4\nb\nab\nba\na\n4\na\nab\nb\nba\n4\nb\nab\nba\na\n4\nba\nab\na\nb\n4\nba\nab\nb\na\n4\nb\nab\nba\na\n4\nb\nab\na\nba\n4\nab\nb\na\nba\n4\na\nba\nab\nb\n4\nb\nba\nab\na\n4\nab\nb\na\nba", "output": "ab\nb" } ]
154
0
3.9615
17,632
525
Anya and Cubes
[ "binary search", "bitmasks", "brute force", "dp", "math", "meet-in-the-middle" ]
null
null
Anya loves to fold and stick. Today she decided to do just that. Anya has *n* cubes lying in a line and numbered from 1 to *n* from left to right, with natural numbers written on them. She also has *k* stickers with exclamation marks. We know that the number of stickers does not exceed the number of cubes. Anya can stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120. You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most *k* exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to *S*. Anya can stick at most one exclamation mark on each cube. Can you do it? Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks.
The first line of the input contains three space-separated integers *n*, *k* and *S* (1<=≀<=*n*<=≀<=25, 0<=≀<=*k*<=≀<=*n*, 1<=≀<=*S*<=≀<=1016)Β β€”Β the number of cubes and the number of stickers that Anya has, and the sum that she needs to get. The second line contains *n* positive integers *a**i* (1<=≀<=*a**i*<=≀<=109)Β β€”Β the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one. Multiple cubes can contain the same numbers.
Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number *S*.
[ "2 2 30\n4 3\n", "2 2 7\n4 3\n", "3 1 1\n1 1 1\n" ]
[ "1\n", "1\n", "6\n" ]
In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them. In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them. In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six.
[]
30
0
0
17,671
0
none
[ "none" ]
null
null
For a connected undirected weighted graph *G*, MST (minimum spanning tree) is a subgraph of *G* that contains all of *G*'s vertices, is a tree, and sum of its edges is minimum possible. You are given a graph *G*. If you run a MST algorithm on graph it would give you only one MST and it causes other edges to become jealous. You are given some queries, each query contains a set of edges of graph *G*, and you should determine whether there is a MST containing all these edges or not.
The first line contains two integers *n*, *m* (2<=<=≀<=*n*,<=*m*<=<=≀<=5Β·105, *n*<=-<=1<=≀<=*m*)Β β€” the number of vertices and edges in the graph and the number of queries. The *i*-th of the next *m* lines contains three integers *u**i*, *v**i*, *w**i* (*u**i*<=β‰ <=*v**i*, 1<=≀<=*w**i*<=≀<=5Β·105)Β β€” the endpoints and weight of the *i*-th edge. There can be more than one edges between two vertices. It's guaranteed that the given graph is connected. The next line contains a single integer *q* (1<=≀<=*q*<=≀<=5Β·105)Β β€” the number of queries. *q* lines follow, the *i*-th of them contains the *i*-th query. It starts with an integer *k**i* (1<=≀<=*k**i*<=≀<=*n*<=-<=1)Β β€” the size of edges subset and continues with *k**i* distinct space-separated integers from 1 to *m*Β β€” the indices of the edges. It is guaranteed that the sum of *k**i* for 1<=≀<=*i*<=≀<=*q* does not exceed 5Β·105.
For each query you should print "YES" (without quotes) if there's a MST containing these edges and "NO" (of course without quotes again) otherwise.
[ "5 7\n1 2 2\n1 3 2\n2 3 1\n2 4 1\n3 4 1\n3 5 2\n4 5 2\n4\n2 3 4\n3 3 4 5\n2 1 7\n2 1 2\n" ]
[ "YES\nNO\nYES\nNO\n" ]
This is the graph of sample: Weight of minimum spanning tree on this graph is 6. MST with edges (1, 3, 4, 6), contains all of edges from the first query, so answer on the first query is "YES". Edges from the second query form a cycle of length 3, so there is no spanning tree including these three edges. Thus, answer is "NO".
[]
46
0
0
17,674
270
Multithreading
[ "data structures", "greedy", "implementation" ]
null
null
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of *n* different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the *i*-th place in the list there is a thread that was at the *a**i*-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread *x* surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: 1. thread *x* is not updated (it has no new messages); 1. the list order 1, 2, ..., *n* changes to *a*1, *a*2, ..., *a**n*.
The first line of input contains an integer *n*, the number of threads (1<=≀<=*n*<=≀<=105). The next line contains a list of *n* space-separated integers *a*1, *a*2, ..., *a**n* where *a**i* (1<=≀<=*a**i*<=≀<=*n*) is the old position of the *i*-th thread in the new list. It is guaranteed that all of the *a**i* are distinct.
Output a single integer β€” the number of threads that surely contain a new message.
[ "5\n5 2 1 3 4\n", "3\n1 2 3\n", "4\n4 3 2 1\n" ]
[ "2\n", "0\n", "3\n" ]
In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
[ { "input": "5\n5 2 1 3 4", "output": "2" }, { "input": "3\n1 2 3", "output": "0" }, { "input": "4\n4 3 2 1", "output": "3" }, { "input": "5\n1 2 5 3 4", "output": "3" }, { "input": "1\n1", "output": "0" }, { "input": "2\n1 2", "output": "0" }, { "input": "2\n2 1", "output": "1" }, { "input": "3\n2 1 3", "output": "1" }, { "input": "6\n4 5 6 1 2 3", "output": "3" }, { "input": "10\n10 5 9 4 1 8 3 7 2 6", "output": "8" }, { "input": "3\n1 3 2", "output": "2" }, { "input": "3\n3 1 2", "output": "1" }, { "input": "4\n1 2 3 4", "output": "0" }, { "input": "4\n2 3 1 4", "output": "2" }, { "input": "6\n3 2 1 6 4 5", "output": "4" }, { "input": "7\n2 3 4 5 6 7 1", "output": "6" }, { "input": "8\n2 6 8 3 1 4 7 5", "output": "7" }, { "input": "9\n6 7 1 2 3 5 4 8 9", "output": "6" }, { "input": "10\n1 2 3 4 5 6 7 8 9 10", "output": "0" }, { "input": "10\n6 1 2 3 4 5 7 8 9 10", "output": "1" }, { "input": "10\n5 8 4 9 6 1 2 3 7 10", "output": "5" }, { "input": "10\n4 2 6 9 5 3 8 1 7 10", "output": "7" }, { "input": "10\n8 2 7 1 5 9 3 4 10 6", "output": "9" }, { "input": "67\n45 48 40 32 11 36 18 47 56 3 22 27 37 12 25 8 57 66 50 41 49 42 30 28 14 62 43 51 9 63 13 1 2 4 5 6 7 10 15 16 17 19 20 21 23 24 26 29 31 33 34 35 38 39 44 46 52 53 54 55 58 59 60 61 64 65 67", "output": "31" }, { "input": "132\n13 7 33 124 118 76 94 92 16 107 130 1 46 58 28 119 42 53 102 81 99 29 57 70 125 45 100 68 10 63 34 38 19 49 56 30 103 72 106 3 121 110 78 2 31 129 128 24 77 61 87 47 15 21 88 60 5 101 82 108 84 41 86 66 79 75 54 97 55 12 69 44 83 131 9 95 11 85 52 35 115 80 111 27 109 36 39 104 105 62 32 40 98 50 64 114 120 59 20 74 51 48 14 4 127 22 18 71 65 116 6 8 17 23 25 26 37 43 67 73 89 90 91 93 96 112 113 117 122 123 126 132", "output": "110" }, { "input": "100\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100", "output": "0" }, { "input": "100\n2 4 82 12 47 63 52 91 87 45 53 1 17 25 64 50 9 13 22 54 21 30 43 24 38 33 68 11 41 78 99 23 28 18 58 67 79 10 71 56 49 61 26 29 59 20 90 74 5 75 3 6 7 8 14 15 16 19 27 31 32 34 35 36 37 39 40 42 44 46 48 51 55 57 60 62 65 66 69 70 72 73 76 77 80 81 83 84 85 86 88 89 92 93 94 95 96 97 98 100", "output": "50" } ]
154
12,390,400
3
17,679
774
Repairing Of String
[ "*special", "constructive algorithms" ]
null
null
Stepan had a favorite string *s* which consisted of the lowercase letters of the Latin alphabet. After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers *c*1,<=*c*2,<=...,<=*c**n*, where *n* equals the length of the string *s*, and *c**i* equals the number of substrings in the string *s* with the length *i*, consisting of the same letters. The substring is a sequence of consecutive characters in the string *s*. For example, if the Stepan's favorite string is equal to "tttesst", the sequence *c* looks like: *c*<==<=[7,<=3,<=1,<=0,<=0,<=0,<=0]. Stepan asks you to help to repair his favorite string *s* according to the given sequence *c*1,<=*c*2,<=...,<=*c**n*.
The first line contains the integer *n* (1<=≀<=*n*<=≀<=2000) β€” the length of the Stepan's favorite string. The second line contains the sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (0<=≀<=*c**i*<=≀<=2000), where *c**i* equals the number of substrings of the string *s* with the length *i*, consisting of the same letters. It is guaranteed that the input data is such that the answer always exists.
Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet.
[ "6\n6 3 1 0 0 0\n", "4\n4 0 0 0\n" ]
[ "kkrrrq", "abcd\n" ]
In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3).
[ { "input": "6\n6 3 1 0 0 0", "output": "aaabbc" }, { "input": "4\n4 0 0 0", "output": "abcd" }, { "input": "1\n1", "output": "a" }, { "input": "5\n5 0 0 0 0", "output": "abcde" }, { "input": "10\n10 8 7 6 5 4 3 2 1 0", "output": "aaaaaaaaab" }, { "input": "20\n20 16 12 8 5 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "aaaaaaabbbbbcccccddd" }, { "input": "99\n99 26 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstu" }, { "input": "200\n200 180 160 140 122 106 92 79 69 60 52 45 38 32 26 20 14 8 4 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "aaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbccccccccccccccccccddddddddddddddddddeeeeeeeeeeeeeeeeefffffffffffffffffgggggggggggghhhhhhhhhhiiiiiiiiijjjjjjjjkkkkkkklllllllmmmmmmmnnnnnnooooopppppqqqqrrrrsssttt" } ]
62
5,529,600
-1
17,707
0
none
[ "none" ]
null
null
You have multiset of *n* strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position *i* and some letter *c* of the English alphabet, such that this string is the only string in the multiset that has letter *c* in position *i*. For example, a multiset of strings {"abc", "aba", "adc", "ada"} are not easy to remember. And multiset {"abc", "ada", "ssa"} is easy to remember because: - the first string is the only string that has character *c* in position 3; - the second string is the only string that has character *d* in position 2; - the third string is the only string that has character *s* in position 2. You want to change your multiset a little so that it is easy to remember. For *a**ij* coins, you can change character in the *j*-th position of the *i*-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember.
The first line contains two integers *n*, *m* (1<=≀<=*n*,<=*m*<=≀<=20)Β β€” the number of strings in the multiset and the length of the strings respectively. Next *n* lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is *m*. Next *n* lines contain *m* integers each, the *i*-th of them contains integers *a**i*1,<=*a**i*2,<=...,<=*a**im* (0<=≀<=*a**ij*<=≀<=106).
Print a single number β€” the answer to the problem.
[ "4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n", "4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10\n", "3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1\n" ]
[ "3\n", "2\n", "0\n" ]
none
[ { "input": "4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1", "output": "3" }, { "input": "4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10", "output": "2" }, { "input": "3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1", "output": "0" }, { "input": "5 2\naa\naa\nab\nbb\nbb\n1 100\n100 100\n1 1\n100 100\n100 1", "output": "4" }, { "input": "3 3\nabc\nabc\nabc\n1 100 100\n100 1 100\n100 100 1", "output": "3" }, { "input": "18 3\nfbn\nlkj\nilm\ngfl\ndim\nbef\ncfi\nbma\neak\nkab\nbcn\nebc\nmfh\ncgi\ndeb\nfge\nfce\nglg\n543010 452044 432237\n533026 367079 978125\n571867 7573 259887\n523171 80963 129140\n727509 334751 399501\n656779 1472 523915\n803488 31561 922147\n488639 399532 725926\n301194 418928 306345\n500339 934078 810234\n621770 32854 324219\n35994 611153 973418\n22056 398091 505664\n594841 92510 294841\n285643 766895 214579\n789288 110084 241557\n803788 561404 814295\n454273 109684 485963", "output": "482616" }, { "input": "20 2\ned\nci\ngg\nib\nae\ndd\nka\nce\naf\ngb\nag\nke\ngj\nab\nie\nif\ngb\nkd\njg\neg\n52414 63820\n271794 291903\n838068 130592\n833667 287689\n270004 427864\n609044 320678\n358266 462930\n649990 731767\n805366 699807\n346704 829870\n3088 685256\n841621 526249\n880833 98228\n395075 685300\n693991 152955\n203957 482069\n61588 604920\n869639 800204\n460571 166336\n96179 163290", "output": "3086607" }, { "input": "14 4\neeac\neded\ndaea\nbdcc\nddba\nbbed\nbece\nbade\nbcde\naeaa\nbdac\neeeb\nbdcc\nedbb\n581667 582851 517604 898386\n791884 352385 258349 327447\n676328 157596 467774 112698\n45777 747499 235807 527714\n403040 861287 118443 362125\n263912 559519 730246 226455\n253277 156730 908225 39214\n106859 902383 881709 628437\n654953 279126 620775 491356\n326884 277262 143979 572860\n678172 916575 323805 344966\n788158 845192 910173 583941\n793949 346044 197488 869580\n752215 693122 61084 269351", "output": "1464749" }, { "input": "9 6\nfaggcc\nfaaabb\ngcbeab\nfcfccc\nggfbda\ncdfdca\nafgbfe\ngdfabb\ndfceca\n820666 301493 605608 275034 608561 41415\n38810 371880 894378 995636 233081 97716\n729450 719972 502653 951544 136420 297684\n141643 410761 189875 96642 313469 90309\n327239 909031 256744 641859 919511 632267\n274015 319092 647885 117871 244700 23850\n694455 42862 757559 76928 529026 404811\n169808 705232 622067 341005 732346 273726\n902764 775615 14975 694559 746539 949558", "output": "169808" }, { "input": "20 2\nef\naf\njn\nep\nma\nfl\nls\nja\ndf\nmn\noi\nhl\ncp\nki\nsm\nbr\nkh\nbh\nss\nsn\n994001 86606\n449283 850926\n420642 431355\n661713 265900\n311094 693311\n839793 582465\n218329 404955\n826100 437982\n490781 884023\n543933 661317\n732465 182697\n930091 431572\n899562 219773\n183082 851452\n187385 138584\n208770 505758\n32329 783088\n381254 60719\n81287 322375\n613255 515667", "output": "1399835" }, { "input": "14 3\nach\nema\nnia\nhdb\njdg\nmim\nihn\nlnb\nmea\nkbe\ndhf\nfdm\nihg\ndle\n354035 675597 288100\n834061 16574 159776\n624990 190507 971363\n783873 552492 865853\n23679 404670 333364\n419925 809048 493997\n614400 59207 604857\n291672 219123 261918\n812836 873570 862136\n168628 673613 981460\n811468 388422 605280\n625385 834937 352820\n604930 141476 454908\n638553 112928 340904", "output": "331983" }, { "input": "15 2\nfb\nii\nfh\nca\nhh\nid\nee\ndf\ncb\nah\ndd\neg\nfh\nbf\nfa\n760810 556244\n809829 634461\n387951 792353\n418763 335686\n72714 935647\n998352 931571\n933518 39685\n839205 685365\n478841 923156\n136274 626301\n62152 942551\n652306 897289\n985857 313305\n783929 451818\n614967 944901", "output": "2465167" }, { "input": "11 5\ngbacd\nadgcb\nfegcg\nadegd\necbef\ngaefc\ncfedb\naggcc\ncaeee\ngccbf\nbggfe\n25774 574140 364457 220351 328678\n332976 664320 153656 745374 268524\n976642 503249 891782 487119 60381\n342599 115529 926636 508909 173932\n873361 128784 330500 590205 224003\n744056 583317 746463 96586 490744\n576114 237562 483180 227779 850753\n767095 762144 485730 712642 641107\n754411 247671 390338 690181 587283\n127688 79591 62996 314500 601391\n278604 260035 971558 902808 119517", "output": "591667" }, { "input": "1 1\na\n10", "output": "0" }, { "input": "1 2\nav\n10 10", "output": "0" }, { "input": "1 19\naaaaaaaaaaaaaaaaaaa\n774231 725127 560557 436525 348529 840998 841465 338905 610008 346645 913975 616398 718745 2592 2734 126306 120664 449 493046", "output": "0" }, { "input": "20 1\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\na\n279537\n272065\n761854\n491903\n772493\n866813\n232656\n138771\n331568\n965561\n251399\n68795\n243844\n195442\n763171\n612582\n137653\n698277\n826156\n42642", "output": "7987621" }, { "input": "20 2\naa\nab\nba\nab\nba\nab\nbb\nba\naa\nba\nbb\nab\nab\nab\nba\naa\naa\nbb\nba\nba\n929272 190859\n322406 81721\n397851 916762\n556069 810114\n510543 404765\n6399 192522\n610299 679861\n251511 669405\n313342 57477\n515837 179823\n576007 834991\n750083 929304\n904444 761847\n551657 584593\n402463 483607\n128054 501477\n484233 342956\n779001 138132\n815387 273216\n717650 927079", "output": "7378839" }, { "input": "3 20\naaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaa\n127368 467778 376749 367872 201445 632491 733098 177711 678142 3202 840006 769776 381139 365553 457474 206005 99000 226191 795686 589632\n785172 213443 482364 357721 191016 596022 882906 601573 917829 966436 129825 285602 706196 722082 206626 338767 795346 360434 596067 839588\n350648 821766 692816 249289 50316 993641 378347 192555 299095 692681 595706 90452 626278 159574 303025 263819 544206 818035 213523 721078", "output": "183343" }, { "input": "1 20\naaaaaaaaaaaaaaaaaaaa\n924705 786913 546594 427698 741583 189683 354089 380570 10694 41046 656300 416271 467032 627269 387941 432085 400210 97543 279672 409814", "output": "0" }, { "input": "2 20\naabbaaaaabbbbbbaabbb\naabaabbbabbaabaaabaa\n129031 157657 633540 354251 528473 948025 107960 614275 976567 779835 628647 672528 278433 254595 676151 992850 419435 163397 816037 682878\n97253 367892 938199 150466 687355 886265 900135 530415 409478 505794 468915 83346 765920 348268 600319 334416 410150 728362 239354 368428", "output": "0" } ]
124
0
0
17,747
297
Parity Game
[ "constructive algorithms" ]
null
null
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") *a* and *b*. Then you try to turn *a* into *b* using two types of operations: - Write *parity*(*a*) to the end of *a*. For example, . - Remove the first character of *a*. For example, . You cannot perform this operation if *a* is empty. You can use as many operations as you want. The problem is, is it possible to turn *a* into *b*? The *parity* of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
The first line contains the string *a* and the second line contains the string *b* (1<=≀<=|*a*|,<=|*b*|<=≀<=1000). Both strings contain only the characters "0" and "1". Here |*x*| denotes the length of the string *x*.
Print "YES" (without quotes) if it is possible to turn *a* into *b*, and "NO" (without quotes) otherwise.
[ "01011\n0110\n", "0011\n1110\n" ]
[ "YES\n", "NO\n" ]
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
[ { "input": "01011\n0110", "output": "YES" }, { "input": "0011\n1110", "output": "NO" }, { "input": "11111\n111111", "output": "YES" }, { "input": "0110011\n01100110", "output": "YES" }, { "input": "10000100\n011110", "output": "NO" }, { "input": "1\n0", "output": "YES" }, { "input": "0\n000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", "output": "NO" }, { "input": "0\n100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "NO" }, { "input": "1\n1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "YES" }, { "input": "11\n110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "YES" }, { "input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n11", "output": "YES" }, { "input": "11\n111", "output": "NO" }, { "input": "1\n1", "output": "YES" }, { "input": "1\n0", "output": "YES" } ]
92
0
0
17,833
846
Random Query
[ "data structures", "math", "probabilities", "two pointers" ]
null
null
You are given an array *a* consisting of *n* positive integers. You pick two integer numbers *l* and *r* from 1 to *n*, inclusive (numbers are picked randomly, equiprobably and independently). If *l*<=&gt;<=*r*, then you swap values of *l* and *r*. You have to calculate the expected value of the number of unique elements in segment of the array from index *l* to index *r*, inclusive (1-indexed).
The first line contains one integer number *n* (1<=≀<=*n*<=≀<=106). The second line contains *n* integer numbers *a*1, *a*2, ... *a**n* (1<=≀<=*a**i*<=≀<=106) β€” elements of the array.
Print one number β€” the expected number of unique elements in chosen segment. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=4 β€” formally, the answer is correct if , where *x* is jury's answer, and *y* is your answer.
[ "2\n1 2\n", "2\n2 2\n" ]
[ "1.500000\n", "1.000000\n" ]
none
[ { "input": "2\n1 2", "output": "1.500000" }, { "input": "2\n2 2", "output": "1.000000" }, { "input": "10\n9 6 8 5 5 2 8 9 2 2", "output": "3.100000" }, { "input": "20\n49 33 9 8 50 21 12 44 23 39 24 10 17 4 17 40 24 19 27 21", "output": "7.010000" }, { "input": "1\n1000000", "output": "1.000000" } ]
1,201
91,033,600
3
17,854
245
Restoring Table
[ "constructive algorithms", "greedy" ]
null
null
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation. For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. He also wrote a square matrix *b* of size *n*<=Γ—<=*n*. The element of matrix *b* that sits in the *i*-th row in the *j*-th column (we'll denote it as *b**ij*) equals: - the "bitwise AND" of numbers *a**i* and *a**j* (that is, *b**ij*<==<=*a**i*Β &amp;Β *a**j*), if *i*<=β‰ <=*j*; - -1, if *i*<==<=*j*. Having written out matrix *b*, Polycarpus got very happy and wiped *a* off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly. Help Polycarpus, given matrix *b*, restore the sequence of numbers *a*1,<=*a*2,<=...,<=*a**n*, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100) β€” the size of square matrix *b*. Next *n* lines contain matrix *b*. The *i*-th of these lines contains *n* space-separated integers: the *j*-th number represents the element of matrix *b**ij*. It is guaranteed, that for all *i* (1<=≀<=*i*<=≀<=*n*) the following condition fulfills: *b**ii* = -1. It is guaranteed that for all *i*,<=*j* (1<=≀<=*i*,<=*j*<=≀<=*n*;Β *i*<=β‰ <=*j*) the following condition fulfills: 0<=≀<=*b**ij*<=≀<=109, *b**ij*<==<=*b**ji*.
Print *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=109) β€” the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces. It is guaranteed that there is sequence *a* that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
[ "1\n-1\n", "3\n-1 18 0\n18 -1 0\n0 0 -1\n", "4\n-1 128 128 128\n128 -1 148 160\n128 148 -1 128\n128 160 128 -1\n" ]
[ "0 ", "18 18 0 ", "128 180 148 160 " ]
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation.
[ { "input": "1\n-1", "output": "0 " }, { "input": "3\n-1 18 0\n18 -1 0\n0 0 -1", "output": "18 18 0 " }, { "input": "4\n-1 128 128 128\n128 -1 148 160\n128 148 -1 128\n128 160 128 -1", "output": "128 180 148 160 " }, { "input": "5\n-1 0 0 0 0\n0 -1 1 0 0\n0 1 -1 0 0\n0 0 0 -1 0\n0 0 0 0 -1", "output": "0 1 1 0 0 " }, { "input": "6\n-1 1835024 1966227 34816 68550800 34832\n1835024 -1 18632728 306185992 324272924 289412624\n1966227 18632728 -1 40 555155640 16846864\n34816 306185992 40 -1 306185000 272666176\n68550800 324272924 555155640 306185000 -1 289481232\n34832 289412624 16846864 272666176 289481232 -1", "output": "69109907 324818716 555700411 306220904 928457660 289521232 " }, { "input": "7\n-1 1000000000 999999488 999999488 1000000000 1000000000 999999488\n1000000000 -1 999999488 999999488 1000000000 1000000000 999999488\n999999488 999999488 -1 999999999 999999488 999999488 999999999\n999999488 999999488 999999999 -1 999999488 999999488 999999999\n1000000000 1000000000 999999488 999999488 -1 1000000000 999999488\n1000000000 1000000000 999999488 999999488 1000000000 -1 999999488\n999999488 999999488 999999999 999999999 999999488 999999488 -1", "output": "1000000000 1000000000 999999999 999999999 1000000000 1000000000 999999999 " }, { "input": "8\n-1 56086 2560 35584 6402 18688 22530 256\n56086 -1 2697 35592 6410 18696 22667 257\n2560 2697 -1 10824 10280 10248 10377 8193\n35584 35592 10824 -1 76040 76040 10248 73984\n6402 6410 10280 76040 -1 76040 14346 73984\n18688 18696 10248 76040 76040 -1 26632 73984\n22530 22667 10377 10248 14346 26632 -1 9217\n256 257 8193 73984 73984 73984 9217 -1", "output": "56086 56223 10985 109384 80170 92424 31883 75009 " }, { "input": "9\n-1 0 0 2 0 2 10 2 0\n0 -1 17 16 16 17 0 17 16\n0 17 -1 16 16 17 0 17 16\n2 16 16 -1 16 18 2 18 16\n0 16 16 16 -1 16 0 16 16\n2 17 17 18 16 -1 2 19 16\n10 0 0 2 0 2 -1 2 0\n2 17 17 18 16 19 2 -1 16\n0 16 16 16 16 16 0 16 -1", "output": "10 17 17 18 16 19 10 19 16 " }, { "input": "10\n-1 16 16 0 0 0 0 16 16 16\n16 -1 16 3 3 2 0 17 18 16\n16 16 -1 0 0 0 0 16 16 16\n0 3 0 -1 15 10 12 1 2 0\n0 3 0 15 -1 10 12 1 2 0\n0 2 0 10 10 -1 8 0 2 0\n0 0 0 12 12 8 -1 0 0 0\n16 17 16 1 1 0 0 -1 16 16\n16 18 16 2 2 2 0 16 -1 16\n16 16 16 0 0 0 0 16 16 -1", "output": "16 19 16 15 15 10 12 17 18 16 " }, { "input": "2\n-1 0\n0 -1", "output": "0 0 " } ]
92
0
3
17,864
0
none
[ "none" ]
null
null
Pavel cooks barbecue. There are *n* skewers, they lay on a brazier in a row, each on one of *n* positions. Pavel wants each skewer to be cooked some time in every of *n* positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation *p* and a sequence *b*1,<=*b*2,<=...,<=*b**n*, consisting of zeros and ones. Each second Pavel move skewer on position *i* to position *p**i*, and if *b**i* equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation *p* and sequence *b* suits Pavel. What is the minimum total number of elements in the given permutation *p* and the given sequence *b* he needs to change so that every skewer will visit each of 2*n* placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation *p* and a sequence *b* suit him if there is an integer *k* (*k*<=β‰₯<=2*n*), so that after *k* seconds each skewer visits each of the 2*n* placements. It can be shown that some suitable pair of permutation *p* and sequence *b* exists for any *n*.
The first line contain the integer *n* (1<=≀<=*n*<=≀<=2Β·105)Β β€” the number of skewers. The second line contains a sequence of integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*)Β β€” the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence *b*1,<=*b*2,<=...,<=*b**n* consisting of zeros and ones, according to which Pavel wants to reverse the skewers.
Print single integerΒ β€” the minimum total number of elements in the given permutation *p* and the given sequence *b* he needs to change so that every skewer will visit each of 2*n* placements.
[ "4\n4 3 2 1\n0 1 1 1\n", "3\n2 3 1\n0 0 0\n" ]
[ "2\n", "1\n" ]
In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of *b* to 1.
[ { "input": "4\n4 3 2 1\n0 1 1 1", "output": "2" }, { "input": "3\n2 3 1\n0 0 0", "output": "1" }, { "input": "1\n1\n0", "output": "1" }, { "input": "2\n1 2\n0 0", "output": "3" }, { "input": "2\n2 1\n0 0", "output": "1" }, { "input": "2\n1 2\n0 1", "output": "2" }, { "input": "2\n2 1\n1 0", "output": "0" }, { "input": "2\n1 2\n1 1", "output": "3" }, { "input": "2\n2 1\n1 1", "output": "1" }, { "input": "5\n2 1 3 4 5\n1 0 0 0 1", "output": "5" }, { "input": "10\n4 10 5 1 6 8 9 2 3 7\n0 1 0 0 1 0 0 1 0 0", "output": "2" }, { "input": "20\n10 15 20 17 8 1 14 6 3 13 19 2 16 12 4 5 11 7 9 18\n0 0 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1 0 0", "output": "3" }, { "input": "100\n87 69 49 86 96 12 10 79 29 66 48 77 73 62 70 52 22 28 97 35 91 5 33 82 65 85 68 80 64 8 38 23 94 34 75 53 57 6 100 2 56 50 55 58 74 9 18 44 40 3 43 45 99 51 21 92 89 36 88 54 42 14 78 71 25 76 13 11 27 72 7 32 93 46 83 30 26 37 39 31 95 59 47 24 67 16 4 15 1 98 19 81 84 61 90 41 17 20 63 60\n1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "4" }, { "input": "1\n1\n1", "output": "0" }, { "input": "2\n1 2\n1 0", "output": "2" }, { "input": "2\n2 1\n0 1", "output": "0" }, { "input": "3\n1 2 3\n0 0 0", "output": "4" }, { "input": "3\n1 2 3\n1 0 0", "output": "3" }, { "input": "3\n1 2 3\n0 1 0", "output": "3" }, { "input": "3\n1 2 3\n1 1 0", "output": "4" }, { "input": "3\n1 2 3\n0 0 1", "output": "3" }, { "input": "3\n1 2 3\n1 0 1", "output": "4" }, { "input": "3\n1 2 3\n0 1 1", "output": "4" }, { "input": "3\n1 2 3\n1 1 1", "output": "3" }, { "input": "3\n1 3 2\n0 0 0", "output": "3" }, { "input": "3\n1 3 2\n1 0 0", "output": "2" }, { "input": "3\n1 3 2\n0 1 0", "output": "2" }, { "input": "3\n1 3 2\n1 1 0", "output": "3" }, { "input": "3\n1 3 2\n0 0 1", "output": "2" }, { "input": "3\n1 3 2\n1 0 1", "output": "3" }, { "input": "3\n1 3 2\n0 1 1", "output": "3" }, { "input": "3\n1 3 2\n1 1 1", "output": "2" }, { "input": "3\n2 1 3\n0 0 0", "output": "3" }, { "input": "3\n2 1 3\n1 0 0", "output": "2" }, { "input": "3\n2 1 3\n0 1 0", "output": "2" }, { "input": "3\n2 1 3\n1 1 0", "output": "3" }, { "input": "3\n2 1 3\n0 0 1", "output": "2" }, { "input": "3\n2 1 3\n1 0 1", "output": "3" }, { "input": "3\n2 1 3\n0 1 1", "output": "3" }, { "input": "3\n2 1 3\n1 1 1", "output": "2" }, { "input": "3\n2 3 1\n0 0 0", "output": "1" }, { "input": "3\n2 3 1\n1 0 0", "output": "0" }, { "input": "3\n2 3 1\n0 1 0", "output": "0" }, { "input": "3\n2 3 1\n1 1 0", "output": "1" }, { "input": "3\n2 3 1\n0 0 1", "output": "0" }, { "input": "3\n2 3 1\n1 0 1", "output": "1" }, { "input": "3\n2 3 1\n0 1 1", "output": "1" }, { "input": "3\n2 3 1\n1 1 1", "output": "0" }, { "input": "3\n3 1 2\n0 0 0", "output": "1" }, { "input": "3\n3 1 2\n1 0 0", "output": "0" }, { "input": "3\n3 1 2\n0 1 0", "output": "0" }, { "input": "3\n3 1 2\n1 1 0", "output": "1" }, { "input": "3\n3 1 2\n0 0 1", "output": "0" }, { "input": "3\n3 1 2\n1 0 1", "output": "1" }, { "input": "3\n3 1 2\n0 1 1", "output": "1" }, { "input": "3\n3 1 2\n1 1 1", "output": "0" }, { "input": "3\n3 2 1\n0 0 0", "output": "3" }, { "input": "3\n3 2 1\n1 0 0", "output": "2" }, { "input": "3\n3 2 1\n0 1 0", "output": "2" }, { "input": "3\n3 2 1\n1 1 0", "output": "3" }, { "input": "3\n3 2 1\n0 0 1", "output": "2" }, { "input": "3\n3 2 1\n1 0 1", "output": "3" }, { "input": "3\n3 2 1\n0 1 1", "output": "3" }, { "input": "3\n3 2 1\n1 1 1", "output": "2" } ]
202
6,041,600
-1
17,888
827
Dirty Arkady's Kitchen
[ "data structures", "dp", "graphs", "shortest paths" ]
null
null
Arkady likes to walk around his kitchen. His labyrinthine kitchen consists of several important places connected with passages. Unfortunately it happens that these passages are flooded with milk so that it's impossible to pass through them. Namely, it's possible to pass through each passage in any direction only during some time interval. The lengths of all passages are equal and Arkady makes through them in one second. For security reasons, Arkady can never stop, also, he can't change direction while going through a passage. In other words, if he starts walking in some passage, he should reach its end and immediately leave the end. Today Arkady needs to quickly reach important place *n* from place 1. He plans to exit the place 1 at time moment 0 and reach the place *n* as early as he can. Please find the minimum time he should spend on his way.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=5Β·105, 0<=≀<=*m*<=≀<=5Β·105)Β β€” the number of important places and the number of passages, respectively. After that, *m* lines follow, each of them describe one passage. Each line contains four integers *a*, *b*, *l* and *r* (1<=≀<=*a*,<=*b*<=≀<=*n*, *a*<=β‰ <=*b*, 0<=≀<=*l*<=&lt;<=*r*<=≀<=109)Β β€” the places the passage connects and the time segment during which it's possible to use this passage.
Print one integerΒ β€” minimum time Arkady should spend to reach the destination. If he can't reach the place *n*, print -1.
[ "5 6\n1 2 0 1\n2 5 2 3\n2 5 0 1\n1 3 0 1\n3 4 1 2\n4 5 2 3\n", "2 1\n1 2 1 100\n" ]
[ "3\n", "-1\n" ]
In the first example Arkady should go through important places 1 → 3 → 4 → 5. In the second example Arkady can't start his walk because at time moment 0 it's impossible to use the only passage.
[]
389
10,137,600
-1
17,905
0
none
[ "none" ]
null
null
A tree is a graph with *n* vertices and exactly *n*<=-<=1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices. A subtree of a tree *T* is a tree with both vertices and edges as subsets of vertices and edges of *T*. You're given a tree with *n* vertices. Consider its vertices numbered with integers from 1 to *n*. Additionally an integer is written on every vertex of this tree. Initially the integer written on the *i*-th vertex is equal to *v**i*. In one move you can apply the following operation: 1. Select the subtree of the given tree that includes the vertex with number 1. 1. Increase (or decrease) by one all the integers which are written on the vertices of that subtree. Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero.
The first line of the input contains *n* (1<=≀<=*n*<=≀<=105). Each of the next *n*<=-<=1 lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*;Β *a**i*<=β‰ <=*b**i*) indicating there's an edge between vertices *a**i* and *b**i*. It's guaranteed that the input graph is a tree. The last line of the input contains a list of *n* space-separated integers *v*1,<=*v*2,<=...,<=*v**n* (|*v**i*|<=≀<=109).
Print the minimum number of operations needed to solve the task. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "3\n1 2\n1 3\n1 -1 1\n" ]
[ "3\n" ]
none
[ { "input": "3\n1 2\n1 3\n1 -1 1", "output": "3" }, { "input": "5\n2 3\n4 5\n2 5\n1 3\n0 2 1 4 3", "output": "8" }, { "input": "10\n5 6\n8 2\n9 3\n4 1\n6 10\n9 8\n7 10\n7 4\n5 2\n0 -6 -9 -1 -5 -4 -2 -7 -8 -3", "output": "18" }, { "input": "5\n3 1\n2 4\n3 4\n2 5\n0 -3 -1 2 4", "output": "20" }, { "input": "12\n1 6\n10 1\n4 1\n7 1\n1 2\n5 1\n1 8\n1 11\n3 1\n12 1\n9 1\n580660007 861441526 -264928594 488291045 253254575 -974301934 709266786 926718320 87511873 514836444 -702876508 848928657", "output": "2529263875" } ]
964
46,387,200
3
17,908
46
Hercule Poirot Problem
[ "dsu", "graphs" ]
F. Hercule Poirot Problem
2
256
Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has *n* rooms and *m* doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could - open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side); - move freely from a room to a room if a corresponding door is open; - give keys to one another, being in one room. "Little grey matter" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying.
The first line contains three preset integers *n*, *m* ΠΈ *k* (1<=≀<=*n*,<=*m*,<=*k*<=≀<=1000) β€” the number of rooms, the number of doors and the number of house residents respectively. The next *m* lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to *n*. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next *k* lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space β€” the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to *m* in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every *m* keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next *k* lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every *m* keys occurs exactly once.
Print "YES" (without quotes) if the second arrangement can result from the first one, otherwise, print "NO".
[ "2 1 2\n1 2\nDmitry 1 1 1\nNatalia 2 0\nNatalia 1 1 1\nDmitry 2 0\n", "4 4 3\n1 3\n1 2\n2 3\n3 4\nArtem 1 1 4\nDmitry 1 1 2\nEdvard 4 2 1 3\nArtem 2 0\nDmitry 1 0\nEdvard 4 4 1 2 3 4\n" ]
[ "YES\n", "NO\n" ]
none
[ { "input": "2 1 2\n1 2\nDmitry 1 1 1\nNatalia 2 0\nNatalia 1 1 1\nDmitry 2 0", "output": "YES" }, { "input": "4 4 3\n1 3\n1 2\n2 3\n3 4\nArtem 1 1 4\nDmitry 1 1 2\nEdvard 4 2 1 3\nArtem 2 0\nDmitry 1 0\nEdvard 4 4 1 2 3 4", "output": "NO" }, { "input": "2 1 1\n2 1\nabsgdf 1 1 1\nabsgdf 1 1 1", "output": "YES" }, { "input": "2 1 1\n2 1\nabsgdf 2 1 1\nabsgdf 1 1 1", "output": "YES" }, { "input": "3 3 3\n1 2\n2 3\n3 1\na 1 1 1\nb 2 1 3\nc 3 1 2\na 3 1 3\nb 1 0\nc 2 2 1 2", "output": "YES" }, { "input": "3 3 3\n1 2\n2 3\n3 1\na 1 1 1\nb 2 1 3\nc 3 1 2\nb 1 1 2\nc 2 1 3\na 3 1 1", "output": "NO" }, { "input": "3 3 3\n1 2\n2 3\n3 1\nb 1 1 2\nc 2 1 3\na 3 1 1\na 3 1 3\nb 1 0\nc 2 2 1 2", "output": "NO" }, { "input": "4 5 3\n1 2\n2 3\n2 4\n1 3\n1 3\na 1 2 4 3\nb 1 0\nc 4 3 1 2 5\na 1 2 4 3\nb 1 1 5\nc 4 2 1 2", "output": "NO" }, { "input": "2 2 2\n1 2\n2 1\nA 1 1 2\nB 1 1 1\nA 1 0\nB 2 2 1 2", "output": "YES" }, { "input": "4 2 4\n2 1\n4 3\na 1 1 1\nb 2 1 2\nc 3 0\nd 4 0\na 2 1 2\nb 1 1 1\nc 3 0\nd 4 0", "output": "YES" }, { "input": "4 2 4\n2 1\n4 3\na 1 1 1\nb 2 1 2\nc 3 0\nd 4 0\na 2 1 2\nb 1 1 1\nc 4 0\nd 3 0", "output": "NO" }, { "input": "6 3 4\n1 2\n3 4\n5 6\na 1 2 1 3\nb 3 1 2\nc 5 0\nd 6 0\na 2 2 1 3\nb 4 1 2\nc 5 0\nd 6 0", "output": "YES" }, { "input": "6 3 4\n1 2\n3 4\n5 6\na 1 2 1 3\nb 3 1 2\nc 5 0\nd 6 0\na 2 1 1\nb 4 2 2 3\nc 5 0\nd 6 0", "output": "NO" }, { "input": "10 20 5\n2 1\n3 1\n4 2\n5 1\n6 5\n7 2\n8 5\n9 8\n10 6\n2 6\n3 5\n3 4\n10 5\n8 3\n9 4\n10 8\n9 2\n6 3\n3 8\n9 8\nDKkXdT 10 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\nOzvgPXMzAr 5 0\nbjac 3 0\ncBPbJtoND 6 0\nw 5 0\nbjac 4 20 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20\nOzvgPXMzAr 2 0\ncBPbJtoND 5 0\nw 10 0\nDKkXdT 4 0", "output": "YES" }, { "input": "10 9 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\nInCeN 1 0\nIzHqPceNhj 2 1 9\neH 3 1 8\nJvgBsNFi 4 1 7\nBA 5 1 6\nRrjSTXJzhL 6 1 5\nDMx 7 1 4\nJzt 8 1 3\nhxBRlDlqwD 9 1 2\nImWeEPkggZ 10 1 1\neH 1 0\nImWeEPkggZ 2 1 9\nDMx 3 1 8\nIzHqPceNhj 4 1 7\nInCeN 5 1 6\nJvgBsNFi 6 1 5\nRrjSTXJzhL 7 1 4\nJzt 8 1 3\nhxBRlDlqwD 9 1 2\nBA 10 1 1", "output": "YES" }, { "input": "11 10 11\n1 2\n2 11\n3 4\n4 11\n5 6\n6 11\n7 8\n8 11\n9 10\n10 11\na 1 1 9\nb 2 1 10\nc 3 0\nd 4 1 1\ne 5 1 3\nf 6 1 4\ng 7 1 5\nh 8 1 6\ni 9 1 7\nj 10 1 8\nk 11 1 2\na 1 0\nb 2 0\nc 3 0\nd 4 0\ne 5 0\nf 6 0\ng 7 0\nh 8 0\ni 9 0\nj 10 0\nk 11 10 1 2 3 4 5 6 7 8 9 10", "output": "YES" }, { "input": "11 10 11\n1 2\n2 11\n3 4\n4 11\n5 6\n6 11\n7 8\n8 11\n9 10\n10 11\na 1 0\nb 2 0\nc 3 0\nd 4 0\ne 5 0\nf 6 0\ng 7 0\nh 8 0\ni 9 0\nj 10 0\nk 11 10 1 2 3 4 5 6 7 8 9 10\na 1 1 9\nb 2 1 10\nc 3 0\nd 4 1 1\ne 5 1 3\nf 6 1 4\ng 7 1 5\nh 8 1 6\ni 9 1 7\nj 10 1 8\nk 11 1 2", "output": "YES" }, { "input": "7 8 2\n3 7\n7 6\n1 2\n4 5\n1 3\n5 6\n4 6\n2 3\na 7 4 3 8 5 2\nb 7 4 4 6 7 1\na 1 4 3 8 5 2\nb 4 4 4 6 7 1", "output": "NO" }, { "input": "7 8 2\n3 7\n7 6\n1 2\n4 5\n1 3\n5 6\n4 6\n2 3\na 1 4 3 8 5 2\nb 4 4 4 6 7 1\na 3 4 3 8 5 2\nb 6 4 4 6 7 1", "output": "YES" }, { "input": "7 7 2\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 1\na 1 4 1 3 5 7\nb 2 3 2 4 6\na 7 4 1 3 5 7\nb 4 3 2 4 6", "output": "NO" }, { "input": "7 7 2\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 1\na 1 4 1 3 5 7\nb 2 3 2 4 6\na 4 4 1 3 5 7\nb 4 3 2 4 6", "output": "YES" }, { "input": "10 20 10\n1 10\n5 10\n2 9\n2 6\n9 6\n3 9\n7 1\n10 5\n7 8\n8 7\n2 3\n9 6\n1 6\n5 3\n4 3\n3 7\n8 2\n6 4\n2 3\n4 1\nTjOMmYPRUY 7 3 1 3 9\nj 6 2 11 15\nBanBSrUA 4 2 19 20\ncSWZxzR 8 1 8\nzVoRlNgt 10 2 6 7\nWLGaq 1 3 10 13 17\nKahHtTDj 1 2 2 14\nrUFZmkpI 6 1 16\ni 4 3 4 5 12\nKLGiua 6 1 18\nWLGaq 10 0\ncSWZxzR 3 1 4\nj 2 3 8 13 17\nrUFZmkpI 7 2 1 19\nKahHtTDj 5 1 16\nKLGiua 6 2 2 12\nBanBSrUA 2 4 6 7 10 15\nTjOMmYPRUY 9 3 3 9 18\nzVoRlNgt 10 2 11 20\ni 2 2 5 14", "output": "YES" }, { "input": "10 10 10\n7 10\n1 2\n4 3\n7 4\n9 2\n8 7\n10 8\n6 10\n5 2\n6 10\nznkkxCkkxv 9 1 10\nP 5 1 7\nKOF 1 2 3 9\nwYtfFWkb 3 0\nZPJiebeu 9 1 6\ndgzAhKY 4 0\nayqPf 3 0\nxFSb 9 0\nreYnbMDm 10 1 1\nydSIPy 6 4 2 4 5 8\nKOF 10 1 4\nayqPf 7 1 7\nreYnbMDm 4 1 5\nwYtfFWkb 5 1 6\nznkkxCkkxv 9 2 1 9\nydSIPy 10 0\nP 10 1 8\ndgzAhKY 7 1 2\nZPJiebeu 8 1 3\nxFSb 7 1 10", "output": "NO" }, { "input": "5 8 5\n1 3\n4 2\n1 3\n4 2\n1 5\n5 3\n2 5\n2 5\nTaMmKIk 1 0\nvrLryIxio 1 3 1 6 5\nGFKONi 2 1 4\nTzRVfh 3 2 3 8\nqp 4 2 7 2\nTaMmKIk 1 2 7 4\nvrLryIxio 1 3 1 6 5\nTzRVfh 3 1 8\nqp 4 1 3\nGFKONi 4 1 2", "output": "YES" }, { "input": "10 9 10\n4 7\n4 8\n8 3\n9 2\n8 3\n8 1\n6 9\n4 7\n7 4\nznkkxCkkxv 1 2 3 6\nQlf 3 0\nKOF 5 2 1 8\ndgzAhKY 5 0\nwYtfFWkb 6 2 4 7\nLbYfZPhWd 9 0\nP 10 1 9\nZPJiebeu 10 1 5\nayqPf 10 0\nzmZLwuf 10 1 2\nwYtfFWkb 2 2 7 4\nQlf 3 1 2\nznkkxCkkxv 3 3 6 3 9\nKOF 5 0\ndgzAhKY 5 0\nLbYfZPhWd 9 0\nP 10 1 5\nZPJiebeu 10 0\nayqPf 10 0\nzmZLwuf 10 2 1 8", "output": "NO" } ]
92
0
0
17,927
730
Expression Queries
[ "data structures" ]
null
null
A simplified arithmetic expression (SAE) is an arithmetic expression defined by the following grammar: - &lt;SAE&gt; ::= &lt;Number&gt; | &lt;SAE&gt;+&lt;SAE&gt; | &lt;SAE&gt;*&lt;SAE&gt; | (&lt;SAE&gt;) - &lt;Number&gt; ::= &lt;Digit&gt; | &lt;Digit&gt;&lt;Number&gt; - &lt;Digit&gt; ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 In other words it's a correct arithmetic expression that is allowed to contain brackets, numbers (possibly with leading zeros), multiplications and additions. For example expressions "(0+01)", "0" and "1*(0)" are simplified arithmetic expressions, but expressions "2-1", "+1" and "1+2)" are not. Given a string *s*1*s*2...*s*|*s*| that represents a SAE; *s**i* denotes the *i*-th character of the string which can be either a digit ('0'-'9'), a plus sign ('+'), a multiplication sign ('*'), an opening round bracket '(' or a closing round bracket ')'. A part *s**l**s**l*<=+<=1...*s**r* of this string is called a sub-expression if and only if it is a SAE. You task is to answer *m* queries, each of which is a pair of integers *l**i*, *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=|*s*|). For each query determine whether the corresponding part of the given string is a sub-expression and in case it's a sub-expression calculate its value modulo 1000000007Β (109<=+<=7). The values should be calculated using standard operator priorities.
The first line of the input contains non-empty string *s* (1<=≀<=|*s*|<=≀<=4Β·105) which represents a correct SAE. Each character of the string can be one of the following characters: '*', '+', '(', ')' or a digit ('0'-'9'). The expression might contain extra-huge numbers. The second line contains an integer *m* (1<=≀<=*m*<=≀<=4Β·105) which is the number of queries. Each of the next *m* lines contains two space-separated integers *l**i*, *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=|*s*|) β€” the *i*-th query.
The *i*-th number of output should be the answer for the *i*-th query. If the *i*-th query corresponds to a valid sub-expression output the value of the sub-expression modulo 1000000007Β (109<=+<=7). Otherwise output -1 as an answer for the query. Print numbers on separate lines.
[ "((1+2)*3+101*2)\n6\n8 14\n1 6\n2 10\n11 14\n5 5\n4 5\n", "(01)\n1\n1 4\n" ]
[ "205\n-1\n10\n2\n2\n-1\n", "1\n" ]
none
[]
3,993
921,600
0
17,932
40
Repaintings
[ "math" ]
B. Repaintings
2
256
A chessboard *n*<=Γ—<=*m* in size is given. During the zero minute we repaint all the black squares to the 0 color. During the *i*-th minute we repaint to the *i* color the initially black squares that have exactly four corner-adjacent squares painted *i*<=-<=1 (all such squares are repainted simultaneously). This process continues ad infinitum. You have to figure out how many squares we repainted exactly *x* times. The upper left square of the board has to be assumed to be always black. Two squares are called corner-adjacent, if they have exactly one common point.
The first line contains integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=5000). The second line contains integer *x* (1<=≀<=*x*<=≀<=109).
Print how many squares will be painted exactly *x* times.
[ "3 3\n1\n", "3 3\n2\n", "1 1\n1\n" ]
[ "4\n", "1\n", "1\n" ]
none
[ { "input": "3 3\n1", "output": "4" }, { "input": "3 3\n2", "output": "1" }, { "input": "1 1\n1", "output": "1" }, { "input": "8 8\n8", "output": "0" }, { "input": "9 10\n1", "output": "17" }, { "input": "9 9\n3", "output": "8" }, { "input": "10 9\n4", "output": "5" }, { "input": "1 5000\n3", "output": "0" }, { "input": "5000 1\n3", "output": "0" }, { "input": "4999 1\n7", "output": "0" }, { "input": "1 4999\n2309", "output": "0" }, { "input": "1 4999\n1000000", "output": "0" }, { "input": "1 1\n200", "output": "0" }, { "input": "5000 5000\n1000000000", "output": "0" }, { "input": "7 7\n777", "output": "0" }, { "input": "126 4125\n52", "output": "4045" }, { "input": "1755 2051\n1", "output": "3804" }, { "input": "3385 4978\n192", "output": "7597" }, { "input": "3663 2904\n1149", "output": "1973" }, { "input": "293 2183\n60", "output": "2238" }, { "input": "1922 109\n41", "output": "1869" }, { "input": "3552 3036\n199", "output": "5794" }, { "input": "182 2314\n54", "output": "2282" }, { "input": "1812 240\n9", "output": "2018" }, { "input": "1595 2881\n710", "output": "1638" }, { "input": "4694 685\n208", "output": "4549" }, { "input": "2793 4840\n901", "output": "4031" }, { "input": "892 3996\n288", "output": "3738" }, { "input": "3990 1800\n171", "output": "5108" }, { "input": "2089 955\n476", "output": "1142" }, { "input": "188 3759\n53", "output": "3737" }, { "input": "3287 2915\n538", "output": "4052" }, { "input": "2738 718\n308", "output": "2226" }, { "input": "837 4874\n208", "output": "4881" }, { "input": "991 2301\n291", "output": "2130" }, { "input": "2016 4549\n433", "output": "4835" }, { "input": "3042 1798\n93", "output": "4470" }, { "input": "419 4046\n174", "output": "3771" }, { "input": "1444 2646\n660", "output": "1452" }, { "input": "2470 4895\n421", "output": "5683" }, { "input": "4847 2143\n827", "output": "3684" }, { "input": "873 744\n42", "output": "1451" }, { "input": "3250 2992\n127", "output": "5736" }, { "input": "4275 240\n16", "output": "4453" }, { "input": "4035 369\n26", "output": "4302" }, { "input": "4339 2062\n462", "output": "4555" }, { "input": "4643 3755\n1381", "output": "2876" }, { "input": "3595 448\n110", "output": "3605" }, { "input": "3899 2141\n428", "output": "4330" }, { "input": "4202 3834\n1478", "output": "2126" }, { "input": "3154 527\n112", "output": "3235" }, { "input": "3458 2220\n526", "output": "3576" }, { "input": "3762 3914\n1073", "output": "3386" }, { "input": "2714 607\n189", "output": "2567" }, { "input": "3432 4788\n1203", "output": "3410" }, { "input": "1662 926\n452", "output": "782" }, { "input": "4892 712\n340", "output": "4246" }, { "input": "3122 1850\n201", "output": "4170" }, { "input": "1353 2988\n589", "output": "1987" }, { "input": "4583 2774\n1206", "output": "2535" }, { "input": "2813 3911\n560", "output": "4486" }, { "input": "1043 49\n10", "output": "1054" }, { "input": "4273 4835\n159", "output": "8474" }, { "input": "2504 973\n201", "output": "2675" }, { "input": "2828 4208\n912", "output": "3390" }, { "input": "10 10\n1", "output": "18" }, { "input": "10 10\n2", "output": "14" }, { "input": "10 10\n3", "output": "10" }, { "input": "10 10\n4", "output": "6" }, { "input": "10 10\n5", "output": "2" } ]
92
0
3.977
18,055
159
Friends or Not
[ "*special", "greedy", "implementation" ]
null
null
Polycarpus has a hobby β€” he develops an unusual social network. His work is almost completed, and there is only one more module to implement β€” the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user *A* sent user *B* a message at time *t*1, and user *B* sent user *A* a message at time *t*2. If 0<=&lt;<=*t*2<=-<=*t*1<=≀<=*d*, then user *B*'s message was an answer to user *A*'s one. Users *A* and *B* are considered to be friends if *A* answered at least one *B*'s message or *B* answered at least one *A*'s message. You are given the log of messages in chronological order and a number *d*. Find all pairs of users who will be considered to be friends.
The first line of the input contains two integers *n* and *d* (1<=≀<=*n*,<=*d*<=≀<=1000). The next *n* lines contain the messages log. The *i*-th line contains one line of the log formatted as "*A**i* *B**i* *t**i*" (without the quotes), which means that user *A**i* sent a message to user *B**i* at time *t**i* (1<=≀<=*i*<=≀<=*n*). *A**i* and *B**i* are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and *t**i* is an integer (0<=≀<=*t**i*<=≀<=10000). It is guaranteed that the lines are given in non-decreasing order of *t**i*'s and that no user sent a message to himself. The elements in the lines are separated by single spaces.
In the first line print integer *k* β€” the number of pairs of friends. In the next *k* lines print pairs of friends as "*A**i* *B**i*" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once.
[ "4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4\n", "1 1000\na b 0\n" ]
[ "1\npetya vasya\n", "0\n" ]
In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second.
[ { "input": "4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4", "output": "1\npetya vasya" }, { "input": "1 1000\na b 0", "output": "0" }, { "input": "2 1\na b 0\nb a 0", "output": "0" }, { "input": "3 1\na b 1\nb c 2\nc d 3", "output": "0" }, { "input": "10 2\nlutdc xfavzancwrokyzzkpco 0\nxfavzancwrokyzzkpco lutdc 1\nlutdc vydvatbnibttqgn 2\nxfavzancwrokyzzkpco vydvatbnibttqgn 2\nvydvatbnibttqgn lutdc 3\nlutdc xfavzancwrokyzzkpco 4\nlutdc vydvatbnibttqgn 5\nlutdc vydvatbnibttqgn 6\nlutdc xfavzancwrokyzzkpco 6\nvydvatbnibttqgn xfavzancwrokyzzkpco 6", "output": "2\nlutdc vydvatbnibttqgn\nlutdc xfavzancwrokyzzkpco" }, { "input": "10 2\nrvmykneiddpqyf jdhmt 0\nwcsjvh jdhmt 0\njdhmt rvmykneiddpqyf 1\nrvmykneiddpqyf jdhmt 1\nwcsjvh rvmykneiddpqyf 2\nrvmykneiddpqyf jdhmt 2\njdhmt rvmykneiddpqyf 3\njdhmt wcsjvh 5\njdhmt wcsjvh 5\nrvmykneiddpqyf jdhmt 6", "output": "1\njdhmt rvmykneiddpqyf" }, { "input": "10 2\nliazxawm spxwktiqjgs 0\nnolq liazxawm 1\nliazxawm nolq 2\nliazxawm spxwktiqjgs 2\nnolq liazxawm 3\nspxwktiqjgs liazxawm 3\nspxwktiqjgs liazxawm 3\nspxwktiqjgs liazxawm 3\nspxwktiqjgs nolq 3\nnolq spxwktiqjgs 4", "output": "3\nliazxawm nolq\nliazxawm spxwktiqjgs\nnolq spxwktiqjgs" }, { "input": "10 2\nfxn ipntr 0\nipntr fxn 1\nfxn ipntr 1\npfvpfteadph ipntr 2\nfxn pfvpfteadph 4\nipntr fxn 4\npfvpfteadph fxn 5\nfxn pfvpfteadph 5\npfvpfteadph ipntr 6\nipntr pfvpfteadph 6", "output": "2\nfxn ipntr\nfxn pfvpfteadph" }, { "input": "10 2\nyltec xnzdtcgzxqqltvpfr 0\nfxxhcmbzzg xnzdtcgzxqqltvpfr 0\nfxxhcmbzzg xnzdtcgzxqqltvpfr 0\nfxxhcmbzzg yltec 1\nfxxhcmbzzg xnzdtcgzxqqltvpfr 2\nfxxhcmbzzg yltec 2\nyltec fxxhcmbzzg 3\nyltec xnzdtcgzxqqltvpfr 3\nyltec xnzdtcgzxqqltvpfr 5\nfxxhcmbzzg yltec 6", "output": "1\nfxxhcmbzzg yltec" }, { "input": "10 2\nrclgdpxdefqu abrfhwigaihoqq 0\nabrfhwigaihoqq rclgdpxdefqu 1\nrclgdpxdefqu bvkfwutdtvxgvx 1\nrclgdpxdefqu abrfhwigaihoqq 1\nabrfhwigaihoqq bvkfwutdtvxgvx 2\nbvkfwutdtvxgvx abrfhwigaihoqq 2\nbvkfwutdtvxgvx abrfhwigaihoqq 3\nabrfhwigaihoqq rclgdpxdefqu 5\nabrfhwigaihoqq rclgdpxdefqu 6\nrclgdpxdefqu bvkfwutdtvxgvx 6", "output": "2\nabrfhwigaihoqq bvkfwutdtvxgvx\nabrfhwigaihoqq rclgdpxdefqu" }, { "input": "3 1\na b 1\na b 2\nb a 2", "output": "1\na b" } ]
124
0
-1
18,226
120
Three Sons
[ "brute force" ]
null
null
Three sons inherited from their father a rectangular corn fiend divided into *n*<=Γ—<=*m* squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing *A*, *B* and *C* tons of corn. The field should be divided by two parallel lines. The lines should be parallel to one side of the field and to each other. The lines should go strictly between the squares of the field. Each resulting part of the field should consist of at least one square. Your task is to find the number of ways to divide the field as is described above, that is, to mark two lines, dividing the field in three parts so that on one of the resulting parts grew *A* tons of corn, *B* on another one and *C* on the remaining one.
The first line contains space-separated integers *n* and *m* β€” the sizes of the original (1<=≀<=*n*,<=*m*<=≀<=50,<=*max*(*n*,<=*m*)<=β‰₯<=3). Then the field's description follows: *n* lines, each containing *m* space-separated integers *c**ij*, (0<=≀<=*c**ij*<=≀<=100) β€” the number of tons of corn each square contains. The last line contains space-separated integers *A*,<=*B*,<=*C* (0<=≀<=*A*,<=*B*,<=*C*<=≀<=106).
Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained *A* tons of corn, another one contained *B* tons, and the remaining one contained *C* tons. If no such way exists, print 0.
[ "3 3\n1 1 1\n1 1 1\n1 1 1\n3 3 3\n", "2 5\n1 1 1 1 1\n2 2 2 2 2\n3 6 6\n", "3 3\n1 2 3\n3 1 2\n2 3 1\n5 6 7\n" ]
[ "2\n", "3\n", "0\n" ]
The lines dividing the field can be horizontal or vertical, but they should be parallel to each other.
[ { "input": "3 3\n1 1 1\n1 1 1\n1 1 1\n3 3 3", "output": "2" }, { "input": "2 5\n1 1 1 1 1\n2 2 2 2 2\n3 6 6", "output": "3" }, { "input": "3 3\n1 2 3\n3 1 2\n2 3 1\n5 6 7", "output": "0" }, { "input": "3 3\n0 0 0\n0 0 1\n1 1 0\n2 1 0", "output": "1" }, { "input": "3 3\n0 0 0\n0 1 0\n0 0 0\n1 0 0", "output": "2" }, { "input": "3 2\n0 0\n0 2\n0 0\n2 0 0", "output": "1" }, { "input": "3 2\n0 1\n2 1\n0 1\n3 1 1", "output": "1" }, { "input": "5 10\n0 1 4 4 4 1 4 0 0 4\n1 1 2 0 4 4 2 2 0 3\n3 2 4 0 3 0 1 3 1 0\n4 1 2 3 0 2 0 2 0 1\n4 4 4 0 2 4 3 1 3 2\n10 78 12", "output": "2" }, { "input": "5 10\n0 0 0 0 0 0 0 0 0 0\n0 2 0 1 0 5 0 3 0 4\n0 0 0 0 0 0 0 0 0 0\n0 2 0 4 0 3 0 3 0 2\n0 0 0 0 0 0 0 0 0 0\n0 15 14", "output": "5" }, { "input": "10 10\n2 0 1 5 5 0 4 1 2 0\n3 5 2 5 4 0 2 3 4 0\n4 0 3 0 5 1 2 3 4 4\n1 3 2 0 5 2 4 3 5 0\n5 0 1 5 1 4 4 2 1 2\n3 2 0 4 0 0 0 1 2 4\n3 3 2 2 5 5 2 0 4 3\n3 0 2 5 4 2 5 3 1 4\n2 3 0 3 1 1 0 0 1 4\n3 2 3 5 3 1 0 3 5 5\n45 124 74", "output": "3" }, { "input": "10 15\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0", "output": "127" }, { "input": "10 15\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 0 0", "output": "127" }, { "input": "10 10\n0 0 0 0 0 0 0 0 0 0\n0 6 0 6 0 1 0 4 0 1\n0 0 0 0 0 0 0 0 0 0\n0 4 0 1 0 3 0 4 0 1\n0 0 0 0 0 0 0 0 0 0\n0 3 0 5 0 1 0 2 0 5\n0 0 0 0 0 0 0 0 0 0\n0 1 0 6 0 3 0 4 0 5\n0 0 0 0 0 0 0 0 0 0\n0 3 0 6 0 2 0 4 0 6\n0 69 18", "output": "6" }, { "input": "10 15\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n5 20 10", "output": "24" }, { "input": "10 15\n2 91 9 36 34 23 88 86 19 91 29 41 39 47 9\n66 63 69 60 73 19 93 78 15 38 70 39 36 4 49\n93 4 25 32 31 49 33 76 22 83 60 49 47 27 20\n34 9 29 85 84 59 8 87 92 5 44 4 60 63 74\n92 50 27 78 62 88 2 50 95 29 37 3 42 72 80\n41 72 35 1 35 85 42 64 41 79 58 2 41 4 11\n2 3 19 77 97 52 74 37 16 58 57 44 14 94 41\n5 47 75 72 49 52 41 48 59 56 38 54 81 9 50\n54 6 46 52 39 79 30 52 79 68 76 40 44 38 68\n7 12 11 87 78 23 47 14 27 83 83 14 84 54 69\n289 5446 1371", "output": "0" }, { "input": "30 2\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0\n0 0 0", "output": "406" }, { "input": "30 2\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n0 0\n0 1\n12 2 1", "output": "24" }, { "input": "30 2\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n34 34\n1836 68 136", "output": "6" }, { "input": "40 4\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n0 0 0 0\n0 1 0 1\n34 4 2", "output": "24" }, { "input": "40 4\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0 0\n0 0 0", "output": "744" } ]
60
6,963,200
-1
18,369
955
Heaps
[ "dp", "trees" ]
null
null
You're given a tree with *n* vertices rooted at 1. We say that there's a *k*-ary heap of depth *m* located at *u* if the following holds: - For *m*<==<=1 *u* itself is a *k*-ary heap of depth 1. - For *m*<=&gt;<=1 vertex *u* is a *k*-ary heap of depth *m* if at least *k* of its children are *k*-ary heaps of depth at least *m*<=-<=1. Denote *dp**k*(*u*) as maximum depth of *k*-ary heap in the subtree of *u* (including *u*). Your goal is to compute .
The first line contains an integer *n* denoting the size of the tree (2<=≀<=*n*<=≀<=3Β·105). The next *n*<=-<=1 lines contain two integers *u*, *v* each, describing vertices connected by *i*-th edge. It's guaranteed that the given configuration forms a tree.
Output the answer to the task.
[ "4\n1 3\n2 3\n4 3\n", "4\n1 2\n2 3\n3 4\n" ]
[ "21\n", "22\n" ]
Consider sample case one. For *k* β‰₯ 3 all *dp*<sub class="lower-index">*k*</sub> will be equal to 1. For *k* = 2 *dp*<sub class="lower-index">*k*</sub> is 2 if <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0a527e618740bc1e83327ce591b684aa4351c914.png" style="max-width: 100.0%;max-height: 100.0%;"/> and 1 otherwise. For *k* = 1 *dp*<sub class="lower-index">*k*</sub> values are (3, 1, 2, 1) respectively. To sum up, 4Β·1 + 4Β·1 + 2Β·2 + 2Β·1 + 3 + 1 + 2 + 1 = 21.
[]
30
0
0
18,371
954
Matrix Walk
[ "implementation" ]
null
null
There is a matrix *A* of size *x*<=Γ—<=*y* filled with integers. For every , *A**i*,<=*j*<==<=*y*(*i*<=-<=1)<=+<=*j*. Obviously, every integer from [1..*xy*] occurs exactly once in this matrix. You have traversed some path in this matrix. Your path can be described as a sequence of visited cells *a*1, *a*2, ..., *a**n* denoting that you started in the cell containing the number *a*1, then moved to the cell with the number *a*2, and so on. From the cell located in *i*-th line and *j*-th column (we denote this cell as (*i*,<=*j*)) you can move into one of the following cells: 1. (*i*<=+<=1,<=*j*) β€” only if *i*<=&lt;<=*x*; 1. (*i*,<=*j*<=+<=1) β€” only if *j*<=&lt;<=*y*; 1. (*i*<=-<=1,<=*j*) β€” only if *i*<=&gt;<=1; 1. (*i*,<=*j*<=-<=1) β€” only if *j*<=&gt;<=1. Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know *x* and *y* exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer *a*1, then move to the cell containing *a*2 (in one step), then move to the cell containing *a*3 (also in one step) and so on. Can you choose *x* and *y* so that they don't contradict with your sequence of moves?
The first line contains one integer number *n* (1<=≀<=*n*<=≀<=200000) β€” the number of cells you visited on your path (if some cell is visited twice, then it's listed twice). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=109) β€” the integers in the cells on your path.
If all possible values of *x* and *y* such that 1<=≀<=*x*,<=*y*<=≀<=109 contradict with the information about your path, print NO. Otherwise, print YES in the first line, and in the second line print the values *x* and *y* such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109.
[ "8\n1 2 3 6 9 8 5 2\n", "6\n1 2 1 2 5 3\n", "2\n1 10\n" ]
[ "YES\n3 3\n", "NO\n", "YES\n4 9\n" ]
The matrix and the path on it in the first test looks like this: Also there exist multiple correct answers for both the first and the third examples.
[ { "input": "8\n1 2 3 6 9 8 5 2", "output": "YES\n1000000000 3" }, { "input": "6\n1 2 1 2 5 3", "output": "NO" }, { "input": "2\n1 10", "output": "YES\n1000000000 9" }, { "input": "3\n1 2 2", "output": "NO" }, { "input": "1\n1", "output": "YES\n1000000000 1" }, { "input": "1\n6", "output": "YES\n1000000000 1" }, { "input": "2\n2 3", "output": "YES\n1000000000 1" }, { "input": "2\n1000000000 1", "output": "YES\n1000000000 999999999" }, { "input": "4\n3 2 4 2", "output": "NO" }, { "input": "5\n1 2 5 4 3", "output": "NO" }, { "input": "2\n1 1", "output": "NO" }, { "input": "3\n1 3 4", "output": "YES\n1000000000 2" }, { "input": "1\n1000000000", "output": "YES\n1000000000 1" }, { "input": "6\n1 4 5 6 3 2", "output": "YES\n1000000000 3" }, { "input": "8\n1 2 3 6 9 8 7 6", "output": "NO" }, { "input": "9\n4 3 2 1 5 6 7 8 9", "output": "NO" }, { "input": "8\n2 5 8 9 6 3 2 1", "output": "YES\n1000000000 3" }, { "input": "4\n1 2 1 3", "output": "YES\n1000000000 2" }, { "input": "3\n1 4 3", "output": "NO" }, { "input": "5\n1 2 3 4 1", "output": "NO" }, { "input": "6\n3 5 2 1 2 1", "output": "NO" }, { "input": "2\n1000000000 999999999", "output": "YES\n1000000000 1" }, { "input": "3\n2 4 3", "output": "YES\n1000000000 2" }, { "input": "5\n1 2 3 2 4", "output": "NO" }, { "input": "6\n10 8 6 4 2 1", "output": "YES\n1000000000 2" }, { "input": "7\n1 4 7 8 9 10 11", "output": "NO" }, { "input": "8\n1 2 3 2 3 4 3 8", "output": "YES\n1000000000 5" }, { "input": "4\n3 4 3 5", "output": "YES\n1000000000 2" }, { "input": "4\n1 4 3 2", "output": "NO" }, { "input": "13\n1 3 4 6 5 3 1 2 4 6 5 3 1", "output": "YES\n1000000000 2" }, { "input": "3\n1 3 2", "output": "NO" }, { "input": "6\n4 3 6 9 8 7", "output": "NO" }, { "input": "6\n1 2 4 3 5 6", "output": "YES\n1000000000 2" }, { "input": "5\n1 2 4 3 1", "output": "YES\n1000000000 2" }, { "input": "5\n1 2 4 3 5", "output": "YES\n1000000000 2" }, { "input": "5\n3 6 5 4 3", "output": "NO" }, { "input": "37\n94 7 32 29 57 22 11 70 57 61 12 75 93 24 4 47 98 43 99 22 50 32 37 64 80 9 40 87 38 70 17 41 77 76 20 66 48", "output": "NO" }, { "input": "2\n99999999 100000000", "output": "YES\n1000000000 1" }, { "input": "5\n3 4 5 6 2", "output": "NO" }, { "input": "6\n3 8 7 6 5 4", "output": "NO" }, { "input": "10\n999999999 999999998 999999997 999999996 999999995 999999994 999999993 999999992 999999991 999999990", "output": "YES\n1000000000 1" }, { "input": "2\n1000000000 999999998", "output": "YES\n1000000000 2" }, { "input": "5\n8 9 10 14 13", "output": "NO" }, { "input": "4\n1 3 2 4", "output": "NO" }, { "input": "4\n2 3 5 6", "output": "NO" }, { "input": "5\n1 2 3 4 2", "output": "NO" }, { "input": "3\n5 6 4", "output": "YES\n1000000000 2" }, { "input": "1\n1000000", "output": "YES\n1000000000 1" }, { "input": "3\n9 10 1", "output": "NO" }, { "input": "28\n1 3 5 7 9 10 8 6 4 2 1 2 4 3 5 6 8 7 9 10 8 7 5 6 4 3 1 2", "output": "YES\n1000000000 2" }, { "input": "5\n3 4 5 6 9", "output": "NO" }, { "input": "3\n6 8 7", "output": "YES\n1000000000 2" }, { "input": "1\n100000000", "output": "YES\n1000000000 1" }, { "input": "6\n2 4 6 5 6 5", "output": "YES\n1000000000 2" }, { "input": "2\n999999999 1000000000", "output": "YES\n1000000000 1" }, { "input": "4\n3 6 7 8", "output": "NO" }, { "input": "23\n92 34 58 40 76 3 38 66 76 23 85 36 47 43 22 46 98 72 97 80 57 77 96", "output": "NO" }, { "input": "3\n6 7 4", "output": "NO" }, { "input": "4\n1 2 4 3", "output": "YES\n1000000000 2" }, { "input": "3\n3 2 4", "output": "NO" }, { "input": "5\n1 4 3 2 1", "output": "NO" }, { "input": "5\n1 3 5 4 3", "output": "NO" }, { "input": "3\n19260816 19260817 19260818", "output": "YES\n1000000000 1" }, { "input": "3\n1 3 6", "output": "NO" }, { "input": "2\n999999998 1000000000", "output": "YES\n1000000000 2" }, { "input": "8\n2 4 6 5 6 5 3 4", "output": "YES\n1000000000 2" }, { "input": "3\n4 3 6", "output": "NO" }, { "input": "2\n246642 246641", "output": "YES\n1000000000 1" }, { "input": "3\n9 7 5", "output": "YES\n1000000000 2" }, { "input": "10\n1 2 1 2 1 2 1 2 1 2", "output": "YES\n1000000000 1" }, { "input": "5\n1 3 5 7 8", "output": "YES\n1000000000 2" }, { "input": "4\n1 10 9 10", "output": "NO" }, { "input": "2\n2 4", "output": "YES\n1000000000 2" }, { "input": "8\n1 2 4 3 5 6 8 7", "output": "YES\n1000000000 2" }, { "input": "3\n4 3 2", "output": "YES\n1000000000 1" }, { "input": "3\n3 2 1", "output": "YES\n1000000000 1" }, { "input": "4\n999 1000 2000 2001", "output": "NO" }, { "input": "3\n4 2 5", "output": "NO" }, { "input": "2\n500000000 1000000000", "output": "YES\n1000000000 500000000" }, { "input": "3\n4 5 7", "output": "NO" }, { "input": "5\n1 3 4 5 4", "output": "NO" }, { "input": "7\n550 555 554 553 554 555 560", "output": "YES\n1000000000 5" } ]
124
26,828,800
3
18,446
43
Lucky Tickets
[ "greedy" ]
C. Lucky Tickets
2
256
Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didn’t think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2*t* pieces he ended up with *t* tickets, each of which was lucky. When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest. Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123. What maximum number of tickets could Vasya get after that?
The first line contains integer *n* (1<=≀<=*n*<=≀<=104) β€” the number of pieces. The second line contains *n* space-separated numbers *a**i* (1<=≀<=*a**i*<=≀<=108) β€” the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide.
Print the single number β€” the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together.
[ "3\n123 123 99\n", "6\n1 1 1 23 10 3\n" ]
[ "1\n", "1\n" ]
none
[ { "input": "3\n123 123 99", "output": "1" }, { "input": "6\n1 1 1 23 10 3", "output": "1" }, { "input": "3\n43440907 58238452 82582355", "output": "1" }, { "input": "4\n31450303 81222872 67526764 17516401", "output": "1" }, { "input": "5\n83280 20492640 21552119 7655071 47966344", "output": "2" }, { "input": "6\n94861402 89285133 30745405 41537407 90189008 83594323", "output": "1" }, { "input": "7\n95136773 99982752 97528336 79027944 96847471 96928960 89423004", "output": "2" }, { "input": "1\n19938466", "output": "0" }, { "input": "2\n55431511 35254032", "output": "0" }, { "input": "2\n28732939 23941418", "output": "1" }, { "input": "10\n77241684 71795210 50866429 35232438 22664883 56785812 91050433 75677099 84393937 43832346", "output": "4" } ]
92
409,600
3.976237
18,499
387
George and Cards
[ "binary search", "data structures" ]
null
null
George is a cat, so he loves playing very much. Vitaly put *n* cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers from 1 to *n*. Then the *i*-th card from the left contains number *p**i* (1<=≀<=*p**i*<=≀<=*n*). Vitaly wants the row to have exactly *k* cards left. He also wants the *i*-th card from left to have number *b**i* written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation *n*<=-<=*k* times. In one remove operation George can choose *w* (1<=≀<=*w*; *w* is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as *x*1,<=*x*2,<=...,<=*x**w* (from the left to the right). After that, George can remove the card *x**i*, such that *x**i*<=≀<=*x**j* for each *j* (1<=≀<=*j*<=≀<=*w*). After the described operation George gets *w* pieces of sausage. George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question!
The first line contains integers *n* and *k* (1<=≀<=*k*<=≀<=*n*<=≀<=106) β€” the initial and the final number of cards. The second line contains *n* distinct space-separated integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≀<=*p**i*<=≀<=*n*) β€” the initial row of cards. The third line contains *k* space-separated integers *b*1,<=*b*2,<=...,<=*b**k* β€” the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for *n*<=-<=*k* times.
Print a single integer β€” the maximum number of pieces of sausage that George can get if he acts optimally well.
[ "3 2\n2 1 3\n1 3\n", "10 5\n1 2 3 4 5 6 7 8 9 10\n2 4 6 8 10\n" ]
[ "1\n", "30\n" ]
none
[]
62
0
0
18,527
0
none
[ "none" ]
null
null
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward). Ivan has beads of *n* colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace.
The first line of the input contains a single number *n* (1<=≀<=*n*<=≀<=26) β€” the number of colors of beads. The second line contains after *n* positive integers *a**i* Β  β€” the quantity of beads of *i*-th color. It is guaranteed that the sum of *a**i* is at least 2 and does not exceed 100<=000.
In the first line print a single numberΒ β€” the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace. Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point.
[ "3\n4 2 1\n", "1\n4\n", "2\n1 1\n" ]
[ "1\nabacaba", "4\naaaa\n", "0\nab\n" ]
In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture. In the second sample there is only one way to compose a necklace.
[ { "input": "3\n4 2 1", "output": "1\naabcbaa" }, { "input": "1\n4", "output": "4\naaaa" }, { "input": "2\n1 1", "output": "0\nab" }, { "input": "1\n2", "output": "2\naa" }, { "input": "1\n3", "output": "3\naaa" }, { "input": "1\n5", "output": "5\naaaaa" }, { "input": "2\n2 2", "output": "2\nabba" }, { "input": "3\n1 2 4", "output": "1\nbccaccb" }, { "input": "3\n3 3 3", "output": "0\naaabbbccc" }, { "input": "3\n3 3 6", "output": "0\naaabbbcccccc" }, { "input": "3\n6 6 6", "output": "6\nabccbaabccbaabccba" }, { "input": "3\n6 6 9", "output": "3\nabcccbaabcccbaabcccba" }, { "input": "26\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", "output": "0\nabcdefghijklmnopqrstuvwxyz" }, { "input": "3\n7 7 21", "output": "0\naaaaaaabbbbbbbccccccccccccccccccccc" }, { "input": "2\n95 50", "output": "5\nbbbbbaaaaaaaaaaaaaaaaaaabbbbbbbbbbaaaaaaaaaaaaaaaaaaabbbbbbbbbbaaaaaaaaaaaaaaaaaaabbbbbbbbbbaaaaaaaaaaaaaaaaaaabbbbbbbbbbaaaaaaaaaaaaaaaaaaabbbbb" }, { "input": "3\n30 30 15", "output": "15\nabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcbaabcba" }, { "input": "3\n1 50 70", "output": "1\nbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccacccccccccccccccccccccccccccccccccccbbbbbbbbbbbbbbbbbbbbbbbbb" }, { "input": "2\n70 10", "output": "10\naaaabaaaaaabaaaaaaaabaaaaaabaaaaaaaabaaaaaabaaaaaaaabaaaaaabaaaaaaaabaaaaaabaaaa" }, { "input": "10\n100 100 100 50 20 300 400 300 350 320", "output": "10\naaaaabbbbbcccccefffffffffffffffgggggggggggggggggggghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjdddiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiddjjjjjjjjjjjjjjjjhhhhhhhhhhhhhhhggggggggggggggggggggfffffffffffffffecccccbbbbbaaaaaaaaaabbbbbcccccefffffffffffffffgggggggggggggggggggghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjddiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiidddjjjjjjjjjjjjjjjjhhhhhhhhhhhhhhhggggggggggggggggggggfffffffffffffffecccccbbbbbaaaaaaaaaabbbbbcccccefffffffffffffffgggggggggggggggggggghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjdddiiiiiiiiiiiiii..." }, { "input": "4\n1100 220 66 11", "output": "11\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbcccdcccbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbcccdcccbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbcccdcccbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbcccdcccbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "1\n100000", "output": "100000\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "2\n50000 50000", "output": "50000\nabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabbaabba..." }, { "input": "3\n33333 33333 33333", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "3\n33332 33333 33332", "output": "1\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "20\n12264 5236 1834 7238 560 938 6209 91 1540 2821 35 6097 12593 1092 6181 7553 4095 280 8568 2723", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "26\n4779 4128 2559 3834 6519 3420 1989 5679 2301 189 453 576 2787 648 492 1551 2796 10713 438 288 9900 2811 990 2250 1320 8367", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "21\n500 8500 5600 500 9500 1200 1500 12500 400 1000 1700 2100 600 6000 2500 5100 4100 6300 400 2300 1800", "output": "100\nuuuuuuuuuccccccccccccccccccccccccccccssffffffiijjjjjmmmnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnkkkkkkkkkttttttttttttrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrqqqqqqqqqqqqqqqqqqqqqppppppppppppppppppppppppppooooooooooooolllllllllllaaahhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhggggggggeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeedddbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbddeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeggggggghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh..." }, { "input": "18\n369 2583 4551 615 369 369 2091 3813 1845 369 615 861 1599 984 3567 2337 123 1599", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb..." }, { "input": "26\n1790 5549 5191 1969 6623 1969 3759 9129 1969 11277 8771 2685 5191 895 179 2685 1611 179 1969 8055 1969 2685 537 2327 716 3401", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "15\n4400 2750 18150 8250 2200 4950 1100 3850 10450 2750 3850 9900 4400 10450 11000", "output": "550\naaaaeeglllllllllmmmmoooooooooobbbcccccccccccccccccddddddddfffffhhhhiiiiiiiiiijjjkkkknnnnnnnnnnnnnnnnnnnkkkjjiiiiiiiiihhhffffdddddddccccccccccccccccbboooooooooommmmlllllllllgeeaaaaaaaaeeglllllllllmmmmoooooooooobbccccccccccccccccdddddddffffhhhiiiiiiiiijjkkknnnnnnnnnnnnnnnnnnnkkkkjjjiiiiiiiiiihhhhfffffddddddddcccccccccccccccccbbboooooooooommmmlllllllllgeeaaaaaaaaeeglllllllllmmmmoooooooooobbbcccccccccccccccccddddddddfffffhhhhiiiiiiiiiijjjkkkknnnnnnnnnnnnnnnnnnnkkkjjiiiiiiiiihhhffffdddddddccccccccccccccccb..." }, { "input": "3\n4648 56552 5992", "output": "8\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb..." }, { "input": "3\n7733 11132 22649", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "7\n10010 13013 5005 11011 33033 5005 3003", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." }, { "input": "23\n4431 567 2079 567 231 1071 567 609 987 819 2205 1365 1323 5691 1155 2457 3045 105 21 777 6804 4767 3969", "output": "0\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa..." } ]
46
0
0
18,544
57
Array
[ "combinatorics", "math" ]
C. Array
2
256
Chris the Rabbit has been interested in arrays ever since he was a child. At the moment he is researching arrays with the length of *n*, containing only integers from 1 to *n*. He is not good at math, that's why some simple things drive him crazy. For example, yesterday he grew keen on counting how many different beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions: - each elements, starting from the second one, is no more than the preceding one - each element, starting from the second one, is no less than the preceding one Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last.
The single line contains an integer *n* which is the size of the array (1<=≀<=*n*<=≀<=105).
You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007.
[ "2\n", "3\n" ]
[ "4\n", "17\n" ]
none
[ { "input": "2", "output": "4" }, { "input": "3", "output": "17" }, { "input": "12", "output": "2704144" }, { "input": "19", "output": "345263536" }, { "input": "20", "output": "846527841" }, { "input": "26", "output": "529476652" }, { "input": "35", "output": "358906180" }, { "input": "38", "output": "917151454" }, { "input": "42", "output": "769030659" }, { "input": "82", "output": "105516606" }, { "input": "388", "output": "121470312" }, { "input": "691", "output": "66828054" }, { "input": "1000", "output": "72474738" }, { "input": "1300", "output": "13198519" }, { "input": "1589", "output": "910090838" }, { "input": "1885", "output": "80236682" }, { "input": "2197", "output": "649466350" }, { "input": "2490", "output": "150738377" }, { "input": "2798", "output": "671813603" }, { "input": "49948", "output": "188470824" }, { "input": "52402", "output": "68720508" }, { "input": "54904", "output": "917915735" }, { "input": "57500", "output": "540890446" }, { "input": "59913", "output": "836170548" }, { "input": "62467", "output": "407412105" }, { "input": "64922", "output": "124840329" }, { "input": "67491", "output": "448912826" }, { "input": "69942", "output": "474688044" }, { "input": "72484", "output": "895032755" }, { "input": "74977", "output": "502088741" }, { "input": "77461", "output": "228321485" }, { "input": "79964", "output": "729228388" }, { "input": "82463", "output": "466136228" }, { "input": "84999", "output": "200164009" }, { "input": "87440", "output": "926716958" }, { "input": "89915", "output": "457356022" }, { "input": "92481", "output": "287683730" }, { "input": "94962", "output": "44271116" }, { "input": "97469", "output": "568733613" }, { "input": "99925", "output": "414342728" }, { "input": "1662", "output": "487795363" }, { "input": "44892", "output": "657147284" }, { "input": "88122", "output": "891210641" }, { "input": "31353", "output": "990883671" }, { "input": "74583", "output": "385361995" }, { "input": "17813", "output": "54476064" }, { "input": "61043", "output": "75760676" }, { "input": "4273", "output": "154508332" }, { "input": "47504", "output": "469586508" }, { "input": "67828", "output": "564997335" }, { "input": "100000", "output": "879367333" }, { "input": "99999", "output": "690990293" }, { "input": "99998", "output": "37611412" }, { "input": "1", "output": "1" } ]
2,000
6,041,600
0
18,580
615
Longtail Hedgehog
[ "dp", "graphs" ]
null
null
This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of *n* points connected by *m* segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: 1. Only segments already presented on the picture can be painted; 1. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; 1. The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get. Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications.
First line of the input contains two integers *n* and *m*(2<=≀<=*n*<=≀<=100<=000, 1<=≀<=*m*<=≀<=200<=000)Β β€” the number of points and the number segments on the picture respectively. Then follow *m* lines, each containing two integers *u**i* and *v**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, *u**i*<=β‰ <=*v**i*)Β β€” the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points.
Print the maximum possible value of the hedgehog's beauty.
[ "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7\n", "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n" ]
[ "9\n", "12\n" ]
The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3Β·3 = 9. <img class="tex-graphics" src="https://espresso.codeforces.com/b3601595b30564928c0ce72c4d371941925f12e3.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7", "output": "9" }, { "input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4", "output": "12" }, { "input": "5 7\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2", "output": "9" }, { "input": "5 9\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2\n1 5\n2 5", "output": "16" }, { "input": "10 10\n6 3\n2 9\n9 4\n4 5\n10 3\n8 3\n10 5\n7 6\n1 4\n6 8", "output": "8" }, { "input": "100 50\n66 3\n92 79\n9 44\n84 45\n30 63\n30 20\n33 86\n8 83\n40 75\n7 36\n91 4\n76 88\n77 76\n28 27\n6 52\n41 57\n8 23\n34 75\n50 15\n86 68\n36 98\n30 84\n37 62\n22 4\n6 45\n72 80\n98 74\n78 84\n1 54\n99 27\n84 91\n78 7\n80 61\n67 48\n51 52\n36 72\n97 87\n25 17\n20 80\n20 39\n72 5\n21 77\n48 1\n63 21\n92 45\n34 93\n28 84\n3 91\n56 99\n7 53", "output": "15" }, { "input": "5 8\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2\n1 5", "output": "12" }, { "input": "2 1\n1 2", "output": "2" }, { "input": "10 9\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10", "output": "9" }, { "input": "5 4\n1 2\n1 3\n1 4\n1 5", "output": "4" }, { "input": "6 5\n1 2\n1 3\n1 4\n1 5\n1 6", "output": "5" } ]
3,000
114,176,000
0
18,677
51
Cheaterius's Problem
[ "implementation" ]
A. Cheaterius's Problem
2
256
Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. The technology of their making is kept secret. But we know that throughout long nights Cheaterius glues together domino pairs with super glue to get squares 2<=Γ—<=2 which are the Cheaterius' magic amulets! After a hard night Cheaterius made *n* amulets. Everyone of them represents a square 2<=Γ—<=2, every quarter contains 1 to 6 dots. Now he wants sort them into piles, every pile must contain similar amulets. Two amulets are called similar if they can be rotated by 90, 180 or 270 degrees so that the following condition is met: the numbers of dots in the corresponding quarters should be the same. It is forbidden to turn over the amulets. Write a program that by the given amulets will find the number of piles on Cheaterius' desk.
The first line contains an integer *n* (1<=≀<=*n*<=≀<=1000), where *n* is the number of amulets. Then the amulet's descriptions are contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of amulets the line "**" is located.
Print the required number of piles.
[ "4\n31\n23\n**\n31\n23\n**\n13\n32\n**\n32\n13\n", "4\n51\n26\n**\n54\n35\n**\n25\n61\n**\n45\n53\n" ]
[ "1\n", "2\n" ]
none
[ { "input": "4\n31\n23\n**\n31\n23\n**\n13\n32\n**\n32\n13", "output": "1" }, { "input": "4\n51\n26\n**\n54\n35\n**\n25\n61\n**\n45\n53", "output": "2" }, { "input": "4\n56\n61\n**\n31\n31\n**\n33\n11\n**\n11\n33", "output": "2" }, { "input": "4\n36\n44\n**\n32\n46\n**\n66\n41\n**\n64\n34", "output": "3" }, { "input": "3\n63\n63\n**\n66\n33\n**\n36\n36", "output": "1" }, { "input": "3\n11\n54\n**\n42\n63\n**\n42\n63", "output": "2" }, { "input": "3\n14\n54\n**\n45\n41\n**\n12\n22", "output": "2" }, { "input": "7\n21\n33\n**\n33\n12\n**\n32\n31\n**\n21\n33\n**\n33\n12\n**\n32\n31\n**\n13\n23", "output": "1" } ]
92
0
3.977
18,683
80
Depression
[ "geometry", "math" ]
B. Depression
1
256
Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels! There was a beauty named Belle. Once she had violated the Beast's order and visited the West Wing. After that she was banished from the castle... Everybody was upset. The beautiful Belle was upset, so was the Beast, so was Lumiere the candlestick. But the worst thing was that Cogsworth was upset. Cogsworth is not a human, but is the mantel clock, which was often used as an alarm clock. Due to Cogsworth's frustration all the inhabitants of the castle were in trouble: now they could not determine when it was time to drink morning tea, and when it was time for an evening stroll. Fortunately, deep in the basement are lying digital clock showing the time in the format HH:MM. Now the residents of the castle face a difficult task. They should turn Cogsworth's hour and minute mustache hands in such a way, that Cogsworth began to show the correct time. Moreover they need to find turn angles in degrees for each mustache hands. The initial time showed by Cogsworth is 12:00. You can only rotate the hands forward, that is, as is shown in the picture: As since there are many ways too select such angles because of full rotations, choose the smallest angles in the right (non-negative) direction. Note that Cogsworth's hour and minute mustache hands move evenly and continuously. Hands are moving independently, so when turning one hand the other hand remains standing still.
The only line of input contains current time according to the digital clock, formatted as HH:MM (00<=≀<=HH<=≀<=23, 00<=≀<=MM<=≀<=59). The mantel clock initially shows 12:00. Pretests contain times of the beginning of some morning TV programs of the Channel One Russia.
Print two numbers *x* and *y* β€” the angles of turning the hour and minute hands, respectively (0<=≀<=*x*,<=*y*<=&lt;<=360). The absolute or relative error in the answer should not exceed 10<=-<=9.
[ "12:00\n", "04:30\n", "08:17\n" ]
[ "0 0", "135 180", "248.5 102" ]
A note to the second example: the hour hand will be positioned exactly in the middle, between 4 and 5.
[ { "input": "12:00", "output": "0 0" }, { "input": "04:30", "output": "135 180" }, { "input": "08:17", "output": "248.5 102" }, { "input": "07:20", "output": "220 120" }, { "input": "09:55", "output": "297.5 330" }, { "input": "11:59", "output": "359.5 354" }, { "input": "01:23", "output": "41.5 138" }, { "input": "11:00", "output": "330 0" }, { "input": "00:00", "output": "0 0" }, { "input": "12:30", "output": "15 180" }, { "input": "23:59", "output": "359.5 354" }, { "input": "20:00", "output": "240 0" }, { "input": "23:45", "output": "352.5 270" }, { "input": "07:14", "output": "217 84" }, { "input": "15:15", "output": "97.5 90" }, { "input": "16:05", "output": "122.5 30" }, { "input": "19:55", "output": "237.5 330" }, { "input": "23:58", "output": "359 348" }, { "input": "14:33", "output": "76.5 198" }, { "input": "18:00", "output": "180 0" }, { "input": "00:24", "output": "12 144" }, { "input": "19:45", "output": "232.5 270" }, { "input": "20:05", "output": "242.5 30" }, { "input": "21:13", "output": "276.5 78" }, { "input": "08:04", "output": "242 24" }, { "input": "07:40", "output": "230 240" }, { "input": "19:12", "output": "216 72" }, { "input": "06:36", "output": "198 216" }, { "input": "06:16", "output": "188 96" }, { "input": "11:49", "output": "354.5 294" }, { "input": "18:45", "output": "202.5 270" }, { "input": "06:44", "output": "202 264" }, { "input": "20:45", "output": "262.5 270" }, { "input": "17:48", "output": "174 288" }, { "input": "08:09", "output": "244.5 54" }, { "input": "04:12", "output": "126 72" }, { "input": "21:30", "output": "285 180" }, { "input": "12:56", "output": "28 336" }, { "input": "00:43", "output": "21.5 258" }, { "input": "02:54", "output": "87 324" }, { "input": "02:29", "output": "74.5 174" }, { "input": "17:24", "output": "162 144" }, { "input": "14:27", "output": "73.5 162" }, { "input": "16:37", "output": "138.5 222" }, { "input": "14:40", "output": "80 240" }, { "input": "01:00", "output": "30 0" }, { "input": "21:40", "output": "290 240" }, { "input": "08:47", "output": "263.5 282" }, { "input": "11:30", "output": "345 180" }, { "input": "08:31", "output": "255.5 186" }, { "input": "04:11", "output": "125.5 66" }, { "input": "19:27", "output": "223.5 162" }, { "input": "10:54", "output": "327 324" }, { "input": "01:20", "output": "40 120" }, { "input": "12:33", "output": "16.5 198" }, { "input": "11:32", "output": "346 192" }, { "input": "15:36", "output": "108 216" }, { "input": "23:45", "output": "352.5 270" }, { "input": "20:12", "output": "246 72" }, { "input": "15:28", "output": "104 168" }, { "input": "00:01", "output": "0.5 6" }, { "input": "12:01", "output": "0.5 6" }, { "input": "23:01", "output": "330.5 6" }, { "input": "00:59", "output": "29.5 354" }, { "input": "12:59", "output": "29.5 354" }, { "input": "23:00", "output": "330 0" } ]
155
20,172,800
3.884925
18,722
193
Fibonacci Number
[ "brute force", "math", "matrices" ]
null
null
John Doe has a list of all Fibonacci numbers modulo 1013. This list is infinite, it starts with numbers 0 and 1. Each number in the list, apart from the first two, is a sum of previous two modulo 1013. That is, John's list is made from the Fibonacci numbers' list by replacing each number there by the remainder when divided by 1013. John got interested in number *f* (0<=≀<=*f*<=&lt;<=1013) and now wants to find its first occurrence in the list given above. Help John and find the number of the first occurence of number *f* in the list or otherwise state that number *f* does not occur in the list. The numeration in John's list starts from zero. There, the 0-th position is the number 0, the 1-st position is the number 1, the 2-nd position is the number 1, the 3-rd position is the number 2, the 4-th position is the number 3 and so on. Thus, the beginning of the list looks like this: 0,<=1,<=1,<=2,<=3,<=5,<=8,<=13,<=21,<=...
The first line contains the single integer *f* (0<=≀<=*f*<=&lt;<=1013) β€” the number, which position in the list we should find. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Print a single number β€” the number of the first occurrence of the given number in John's list. If this number doesn't occur in John's list, print -1.
[ "13\n", "377\n" ]
[ "7\n", "14\n" ]
none
[ { "input": "13", "output": "7" }, { "input": "377", "output": "14" }, { "input": "2406684390626", "output": "999999" }, { "input": "1", "output": "1" }, { "input": "3705587146357", "output": "3224323" }, { "input": "2644848607501", "output": "4999" }, { "input": "3153355924376", "output": "2500000030002" }, { "input": "2029910151951", "output": "14000000000902" }, { "input": "9673339843751", "output": "14000000000002" }, { "input": "9673339843751", "output": "14000000000002" }, { "input": "9137820308201", "output": "7153729197299" }, { "input": "5673339843751", "output": "11000000000002" }, { "input": "1800000000001", "output": "2699999999999" }, { "input": "5794082000001", "output": "899972999999" }, { "input": "6138242440179", "output": "14000000000092" }, { "input": "7402222686319", "output": "9525991302838" }, { "input": "2524707127593", "output": "310860593773" }, { "input": "2372721962933", "output": "5538764813213" }, { "input": "6052638322329", "output": "2730957676958" }, { "input": "2145870521291", "output": "8642598169768" }, { "input": "9342998561506", "output": "1569702903681" }, { "input": "4461969564061", "output": "3883677670028" }, { "input": "2755560887426", "output": "57704852301" }, { "input": "53824509026", "output": "895481947599" }, { "input": "6651238230626", "output": "9999" }, { "input": "3408709136249", "output": "9998" }, { "input": "78474174626", "output": "999" }, { "input": "8784097568833", "output": "9887" }, { "input": "8791215445823", "output": "9886" }, { "input": "1275196590901", "output": "1000099" }, { "input": "111", "output": "239196208822" }, { "input": "112", "output": "3676929870516" }, { "input": "113", "output": "4106406311593" }, { "input": "4917874132879", "output": "10929066223558" }, { "input": "4444938954466", "output": "839816181759" }, { "input": "4", "output": "-1" }, { "input": "5", "output": "5" }, { "input": "6", "output": "-1" }, { "input": "7", "output": "9366795780274" }, { "input": "9", "output": "327828114109" }, { "input": "10", "output": "-1" }, { "input": "11", "output": "7294553741128" }, { "input": "12", "output": "-1" }, { "input": "13", "output": "7" }, { "input": "14", "output": "-1" }, { "input": "15", "output": "12634170740230" }, { "input": "16", "output": "1877819665068" }, { "input": "17", "output": "5459611452263" }, { "input": "18", "output": "-1" }, { "input": "19", "output": "2703748564012" }, { "input": "20", "output": "-1" }, { "input": "21", "output": "8" }, { "input": "8", "output": "6" }, { "input": "9999999999999", "output": "14999999999998" }, { "input": "9999999999997", "output": "979091474417" }, { "input": "9999999999992", "output": "7499999999994" }, { "input": "9999999999979", "output": "14999999999992" }, { "input": "9999999999945", "output": "719336987555" }, { "input": "9999999999856", "output": "7499999999988" }, { "input": "9972900390626", "output": "999999999999" }, { "input": "9999999999998", "output": "-1" } ]
92
0
0
18,725
291
Tree-String Problem
[ "*special", "dfs and similar", "hashing", "strings" ]
null
null
A rooted tree is a non-directed connected graph without any cycles with a distinguished vertex, which is called the tree root. Consider the vertices of a rooted tree, that consists of *n* vertices, numbered from 1 to *n*. In this problem the tree root is the vertex number 1. Let's represent the length of the shortest by the number of edges path in the tree between vertices *v* and *u* as *d*(*v*,<=*u*). A parent of vertex *v* in the rooted tree with the root in vertex *r* (*v*<=β‰ <=*r*) is vertex *p**v*, such that *d*(*r*,<=*p**v*)<=+<=1<==<=*d*(*r*,<=*v*) and *d*(*p**v*,<=*v*)<==<=1. For example, on the picture the parent of vertex *v*<==<=5 is vertex *p*5<==<=2. One day Polycarpus came across a rooted tree, consisting of *n* vertices. The tree wasn't exactly ordinary: it had strings written on its edges. Polycarpus positioned the tree on the plane so as to make all edges lead from top to bottom if you go from the vertex parent to the vertex (see the picture). For any edge that lead from vertex *p**v* to vertex *v* (1<=&lt;<=*v*<=≀<=*n*), he knows string *s**v* that is written on it. All strings are written on the edges from top to bottom. For example, on the picture *s*7="ba". The characters in the strings are numbered starting from 0. Polycarpus defines the position in this tree as a specific letter on a specific string. The position is written as a pair of integers (*v*,<=*x*) that means that the position is the *x*-th letter of the string *s**v* (1<=&lt;<=*v*<=≀<=*n*, 0<=≀<=*x*<=&lt;<=|*s**v*|), where |*s**v*| is the length of string *s**v*. For example, the highlighted letters are positions (2,<=1) and (3,<=1). Let's consider the pair of positions (*v*,<=*x*) and (*u*,<=*y*) in Polycarpus' tree, such that the way from the first position to the second goes down on each step. We will consider that the pair of such positions defines string *z*. String *z* consists of all letters on the way from (*v*,<=*x*) to (*u*,<=*y*), written in the order of this path. For example, in the picture the highlighted positions define string "bacaba". Polycarpus has a string *t*, he wants to know the number of pairs of positions that define string *t*. Note that the way from the first position to the second in the pair must go down everywhere. Help him with this challenging tree-string problem!
The first line contains integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of vertices of Polycarpus's tree. Next *n*<=-<=1 lines contain the tree edges. The *i*-th of them contains number *p**i*<=+<=1 and string *s**i*<=+<=1 (1<=≀<=*p**i*<=+<=1<=≀<=*n*;Β *p**i*<=+<=1<=β‰ <=(*i*<=+<=1)). String *s**i*<=+<=1 is non-empty and consists of lowercase English letters. The last line contains string *t*. String *t* consists of lowercase English letters, its length is at least 2. It is guaranteed that the input contains at most 3Β·105 English letters.
Print a single integer β€” the required number. Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\naba\n", "7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\nbacaba\n" ]
[ "6\n", "4\n" ]
In the first test case string "aba" is determined by the pairs of positions: (2, 0) and (5, 0); (5, 2) and (6, 1); (5, 2) and (3, 1); (4, 0) and (4, 2); (4, 4) and (4, 6); (3, 3) and (3, 5). Note that the string is not defined by the pair of positions (7, 1) and (5, 0), as the way between them doesn't always go down.
[ { "input": "7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\naba", "output": "6" }, { "input": "7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\nbacaba", "output": "4" }, { "input": "4\n1 a\n4 b\n1 b\naab", "output": "0" }, { "input": "4\n1 a\n1 b\n1 a\nbba", "output": "0" }, { "input": "4\n1 b\n2 b\n3 a\naba", "output": "0" }, { "input": "20\n20 a\n2 cb\n11 cab\n13 bac\n18 a\n3 bb\n1 a\n10 ab\n1 aba\n12 ca\n18 b\n18 acb\n10 aac\n1 a\n2 bacb\n8 ac\n8 ba\n12 ca\n1 ba\nabaab", "output": "1" }, { "input": "21\n13 c\n8 ab\n15 a\n9 b\n13 aa\n8 ca\n1 ba\n20 ac\n17 a\n8 ba\n4 b\n9 ab\n4 acb\n3 a\n18 ab\n4 aaab\n15 a\n16 ca\n11 b\n6 aa\nbababb", "output": "0" }, { "input": "22\n1 bacabaaab\n1 bac\n1 acabb\n1 ac\n1 abaaba\n1 aba\n1 cabaabaa\n1 ba\n1 bacac\n1 abc\n1 aaac\n1 aaaab\n1 acabacaca\n1 ba\n1 baa\n1 baca\n1 babaca\n1 aacabb\n1 acaa\n1 cacabab\n1 acaa\naabaabaa", "output": "0" }, { "input": "6\n4 baab\n6 acab\n1 aaab\n2 acabb\n4 aca\nbbaab", "output": "1" }, { "input": "10\n1 c\n6 aaaa\n5 bac\n10 a\n10 ac\n2 bac\n4 a\n2 b\n1 abbb\nabbba", "output": "2" }, { "input": "10\n10 aaab\n7 a\n5 ba\n9 b\n2 aca\n10 b\n3 bb\n1 acaa\n4 ba\nbabab", "output": "2" }, { "input": "7\n5 aba\n1 abacaab\n2 ac\n3 ab\n5 acab\n2 aa\nababa", "output": "3" }, { "input": "7\n1 abac\n5 c\n6 abb\n1 acaaba\n5 aba\n6 baa\nbaaba", "output": "1" } ]
46
0
0
18,743
850
Arpa and a game with Mojtaba
[ "bitmasks", "dp", "games" ]
null
null
Mojtaba and Arpa are playing a game. They have a list of *n* numbers in the game. In a player's turn, he chooses a number *p**k* (where *p* is a prime number and *k* is a positive integer) such that *p**k* divides at least one number in the list. For each number in the list divisible by *p**k*, call it *x*, the player will delete *x* and add to the list. The player who can not make a valid choice of *p* and *k* loses. Mojtaba starts the game and the players alternatively make moves. Determine which one of players will be the winner if both players play optimally.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100)Β β€” the number of elements in the list. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109)Β β€” the elements of the list.
If Mojtaba wins, print "Mojtaba", otherwise print "Arpa" (without quotes). You can print each letter in any case (upper or lower).
[ "4\n1 1 1 1\n", "4\n1 1 17 17\n", "4\n1 1 17 289\n", "5\n1 2 3 4 5\n" ]
[ "Arpa\n", "Mojtaba\n", "Arpa\n", "Arpa\n" ]
In the first sample test, Mojtaba can't move. In the second sample test, Mojtaba chooses *p* = 17 and *k* = 1, then the list changes to [1, 1, 1, 1]. In the third sample test, if Mojtaba chooses *p* = 17 and *k* = 1, then Arpa chooses *p* = 17 and *k* = 1 and wins, if Mojtaba chooses *p* = 17 and *k* = 2, then Arpa chooses *p* = 17 and *k* = 1 and wins.
[ { "input": "4\n1 1 1 1", "output": "Arpa" }, { "input": "4\n1 1 17 17", "output": "Mojtaba" }, { "input": "4\n1 1 17 289", "output": "Arpa" }, { "input": "5\n1 2 3 4 5", "output": "Arpa" }, { "input": "10\n10 14 16 9 17 13 12 4 6 10", "output": "Mojtaba" }, { "input": "10\n13 13 18 3 8 9 19 12 20 14", "output": "Mojtaba" }, { "input": "10\n10 18 16 10 8 20 8 4 4 2", "output": "Mojtaba" }, { "input": "10\n1 4 15 1 16 14 7 17 11 8", "output": "Arpa" }, { "input": "10\n9 19 4 1 20 7 19 18 11 11", "output": "Mojtaba" }, { "input": "100\n80 35 113 179 195 92 143 152 125 55 68 121 71 147 172 153 87 68 143 133 32 153 177 173 183 100 59 55 63 189 63 44 78 15 143 105 62 98 22 8 197 119 77 108 85 79 56 160 149 157 39 129 70 79 118 15 110 17 157 81 184 1 160 126 35 108 15 28 63 128 24 132 179 160 104 164 49 76 30 148 144 38 112 10 65 109 68 142 35 174 89 118 24 46 171 35 53 169 154 18", "output": "Mojtaba" }, { "input": "100\n62 159 35 165 55 25 182 120 76 176 86 188 122 23 12 142 44 156 173 105 95 83 87 128 166 163 144 157 30 198 31 13 99 197 57 114 34 42 173 15 197 61 160 8 138 104 43 199 52 19 56 40 65 152 64 166 106 88 192 107 6 156 46 36 87 92 65 123 43 124 199 140 164 114 157 64 177 2 115 141 179 194 125 67 160 62 83 32 44 101 193 166 99 162 192 120 112 28 51 56", "output": "Mojtaba" }, { "input": "100\n10 30 91 164 105 103 4 116 77 36 118 158 136 161 28 35 119 148 16 47 116 18 13 124 103 96 132 119 160 147 128 98 143 96 130 129 133 45 37 133 192 22 35 4 75 89 110 54 147 2 64 66 123 136 12 183 161 118 50 131 39 147 143 16 43 146 98 42 191 155 96 18 169 176 170 102 172 9 130 62 22 32 121 153 24 150 100 102 1 52 2 76 147 139 72 10 21 37 157 23", "output": "Mojtaba" }, { "input": "100\n199 89 78 3 1 171 187 132 20 81 88 51 7 175 181 92 75 196 71 17 200 27 117 112 182 51 43 64 189 136 130 24 125 87 38 185 198 6 175 63 178 65 33 91 22 6 180 100 21 11 164 1 101 26 1 97 71 76 65 163 3 27 81 110 114 38 160 42 90 65 189 181 198 66 3 152 83 125 84 72 181 193 75 197 184 161 192 181 38 172 88 106 112 6 67 120 85 181 148 88", "output": "Mojtaba" }, { "input": "100\n44 188 149 152 200 131 152 1 15 101 15 64 51 48 5 189 65 105 90 143 60 54 82 149 66 89 191 182 46 92 51 19 77 88 175 192 89 4 135 126 89 68 116 92 159 71 160 97 200 89 109 100 120 95 183 85 187 125 93 65 101 51 172 65 195 4 108 123 152 30 9 88 33 124 91 28 49 79 78 2 30 169 115 198 130 16 165 120 163 121 45 31 107 83 47 164 200 112 83 59", "output": "Mojtaba" }, { "input": "21\n128 32 131072 16 64 536870912 4 524288 33554432 16384 8 256 1048576 2048 2 32768 268435456 1 16777216 8388608 134217728", "output": "Mojtaba" }, { "input": "13\n134217728 32768 536870912 524288 16777216 16384 1048576 33554432 8388608 268435456 512 131072 2048", "output": "Mojtaba" }, { "input": "29\n2 2097152 67108864 262144 1 16384 4096 4 65536 256 1024 8388608 16 4194304 134217728 64 512 33554432 8 128 268435456 524288 32 2048 32768 8192 131072 16777216 1048576", "output": "Mojtaba" }, { "input": "6\n9 5 1 1 8 1", "output": "Arpa" }, { "input": "26\n1 48828125 81 59049 256 16 9 3 6561 2048 512 128 1024 2 25 3125 390625 177147 1 19683 64 32 1 4 15625 9765625", "output": "Arpa" }, { "input": "15\n2048 5 1 19683 9765625 3125 177147 125 2187 48828125 6561 512 1 1 390625", "output": "Arpa" }, { "input": "1\n984711052", "output": "Mojtaba" }, { "input": "1\n270983380", "output": "Mojtaba" }, { "input": "1\n887864471", "output": "Mojtaba" }, { "input": "1\n234923095", "output": "Arpa" }, { "input": "1\n877739788", "output": "Mojtaba" }, { "input": "1\n534306180", "output": "Mojtaba" }, { "input": "1\n365210472", "output": "Mojtaba" }, { "input": "1\n616751420", "output": "Mojtaba" }, { "input": "19\n1 2048 1048576 524288 16 128 32 2 16384 131072 32768 4 33554432 134217728 268435456 8 8388608 536870912 16777216", "output": "Arpa" }, { "input": "59\n1953125 14348907 823543 11 64 1048576 9765625 16777216 19487171 2187 5 117649 40353607 48828125 531441 5764801 729 49 32768 371293 43046721 1771561 1 25 13 4826809 1 6561 2197 536870912 62748517 524288 4782969 128 59049 177147 16384 1 27 19683 9 1594323 1331 33554432 129140163 1 2048 268435456 1 8388608 625 131072 343 3 2401 1 390625 134217728 4", "output": "Arpa" }, { "input": "53\n131072 64 1594323 49 25 129140163 4 729 1771561 1 1331 33554432 531441 128 4782969 16777216 2187 32768 19487171 48828125 134217728 59049 16384 6561 1048576 1 3 9 177147 9765625 1 390625 11 27 1953125 5 19683 2401 2048 117649 524288 343 40353607 1 43046721 8388608 5764801 14348907 625 823543 268435456 1 536870912", "output": "Arpa" }, { "input": "55\n1 161051 121 14641 4782969 177147 5 1771561 1594323 1953125 524288 5764801 9765625 49 1 390625 823543 128 2187 268435456 8388608 117649 1048576 59049 43046721 2048 16777216 2401 536870912 4 19487171 9 40353607 14348907 33554432 131072 3 16384 27 134217728 1 64 32768 1 531441 48828125 19683 625 343 1331 25 129140163 729 6561 1", "output": "Mojtaba" }, { "input": "34\n1 64 390625 33554432 9 1 524288 387420489 536870912 8388608 2048 244140625 129140163 59049 4 16384 9765625 43046721 131072 2187 48828125 1 25 16777216 1048576 268435456 19683 32768 4782969 81 5 128 3 134217728", "output": "Arpa" }, { "input": "44\n390625 16807 7 1 131072 536870912 43046721 125 2187 134217728 32768 19487171 16384 2401 5 16777216 524288 343 1048576 9765625 244140625 33554432 81 4782969 59049 1331 129140163 387420489 282475249 1 48828125 2048 1 1 25 11 40353607 3 268435456 19683 214358881 9 1 8388608", "output": "Mojtaba" }, { "input": "51\n33554432 268435456 25 9765625 1 536870912 5 8 1 27 32768 121 19683 40353607 1 128 4782969 1 4 3 8388608 161051 2187 282475249 1048576 2 3125 16807 387420489 1771561 11 625 16 43046721 214358881 16384 16777216 59049 32 343 134217728 390625 2048 1 9 524288 244140625 131072 7 48828125 129140163", "output": "Arpa" }, { "input": "55\n5 1 161051 343 134217728 4782969 131072 815730721 1 16384 40353607 2187 3 16807 16 2197 8388608 59049 282475249 244140625 1 7 387420489 390625 9 268435456 1 214358881 1771561 121 524288 27 11 48828125 33554432 32768 169 25 625 8 16777216 9765625 128 129140163 43046721 2048 536870912 2 3125 19683 1048576 4 1 1 32", "output": "Arpa" }, { "input": "52\n169 8388608 48828125 32768 387420489 1 214358881 815730721 8 4782969 16384 27 390625 1 268435456 2187 40353607 59049 15625 282475249 536870912 125 2197 1 121 134217728 19683 1 32 1 625 3 244140625 2 524288 131072 25 9765625 43046721 7 1771561 16777216 1048576 9 16807 343 161051 11 2048 33554432 1 129140163", "output": "Arpa" }, { "input": "63\n16807 1 1419857 59049 1 2187 6859 1 33554432 129140163 11 1 16777216 3 9765625 1331 2197 268435456 2 169 390625 343 1048576 536870912 19683 125 16384 27 40353607 815730721 32 130321 43046721 524288 17 8388608 7 4782969 15625 282475249 134217728 1 25 4913 9 19 131072 244140625 625 2476099 1 2048 214358881 32768 1 24137569 48828125 387420489 8 19487171 361 1 47045881", "output": "Arpa" }, { "input": "38\n524288 27 131072 256 64 15625 729 2048 1048576 387420489 4782969 1 33554432 625 16777216 32768 4 243 9 1 9765625 390625 1 19683 8388608 16384 59049 8 48828125 536870912 244140625 134217728 2 5 129140163 25 43046721 268435456", "output": "Arpa" }, { "input": "56\n49 24137569 1048576 16384 4782969 2 4913 1 59049 16777216 625 121 1 19683 19487171 524288 43046721 1419857 125 13 282475249 15625 1 1 1 131072 2476099 9765625 2197 390625 19 6859 268435456 536870912 243 32768 40353607 8 2401 1024 1 17 2187 129140163 47045881 25 387420489 1 244140625 33554432 214358881 815730721 8388608 48828125 134217728 1", "output": "Arpa" } ]
109
512,000
0
18,788
652
Nested Segments
[ "data structures", "sortings" ]
null
null
You are given *n* segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=2Β·105) β€” the number of segments on a line. Each of the next *n* lines contains two integers *l**i* and *r**i* (<=-<=109<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=109) β€” the coordinates of the left and the right ends of the *i*-th segment. It is guaranteed that there are no ends of some segments that coincide.
Print *n* lines. The *j*-th of them should contain the only integer *a**j* β€” the number of segments contained in the *j*-th segment.
[ "4\n1 8\n2 3\n4 7\n5 6\n", "3\n3 4\n1 5\n2 6\n" ]
[ "3\n0\n1\n0\n", "0\n1\n1\n" ]
none
[ { "input": "4\n1 8\n2 3\n4 7\n5 6", "output": "3\n0\n1\n0" }, { "input": "3\n3 4\n1 5\n2 6", "output": "0\n1\n1" }, { "input": "1\n-1000000000 1000000000", "output": "0" }, { "input": "2\n-1000000000 999999999\n-999999999 1000000000", "output": "0\n0" }, { "input": "2\n-1000000000 1000000000\n-999999999 999999999", "output": "1\n0" }, { "input": "3\n-999999997 999999997\n-999999998 999999998\n-999999999 999999999", "output": "0\n1\n2" }, { "input": "3\n-999999999 999999997\n-999999998 999999998\n-999999997 999999999", "output": "0\n0\n0" }, { "input": "7\n1 14\n2 7\n3 4\n5 6\n8 13\n9 10\n11 12", "output": "6\n2\n0\n0\n2\n0\n0" }, { "input": "8\n1 16\n2 7\n3 4\n5 6\n8 9\n10 15\n11 12\n13 14", "output": "7\n2\n0\n0\n0\n2\n0\n0" }, { "input": "8\n1 16\n2 7\n3 4\n5 8\n6 9\n10 15\n11 13\n12 14", "output": "7\n1\n0\n0\n0\n2\n0\n0" }, { "input": "10\n-3 -1\n-10 4\n0 6\n-4 -2\n1 3\n2 9\n5 10\n-7 -6\n-8 8\n-9 7", "output": "0\n4\n1\n0\n0\n0\n0\n0\n5\n5" }, { "input": "1\n-1 0", "output": "0" }, { "input": "10\n-513515548 596545048\n-864922524 -143639540\n-186185108 253442195\n-325311097 557247880\n-843432193 -793445411\n-589321824 602462994\n-980740122 -845522939\n-20465341 192085177\n363969852 718880403\n-797865714 644017524", "output": "3\n1\n1\n2\n0\n4\n0\n0\n0\n5" }, { "input": "10\n-128739791 -39063859\n-103449295 927503025\n95979137 136886112\n-204849739 909268860\n-172975545 140068443\n-795229308 -495885136\n204945615 853645963\n-333018842 605274895\n-580345079 953606768\n335800547 686581467", "output": "0\n3\n0\n5\n2\n0\n1\n3\n8\n0" }, { "input": "10\n-317257964 738085350\n-989968614 735410270\n-870119900 197330499\n335597813 781352203\n-109119439 116242045\n604692366 819316837\n269683555 704665430\n-510921778 563710518\n340538827 963493707\n-925778993 739625150", "output": "2\n4\n1\n0\n0\n0\n0\n1\n1\n5" }, { "input": "10\n-644829480 485279434\n-54170850 756394598\n-103264442 39454200\n-12413554 577524970\n-909155364 193038318\n362781551 722749393\n-766782831 137745252\n811928942 870183784\n-43075984 270955026\n-159109789 303861267", "output": "3\n3\n0\n0\n2\n0\n1\n0\n0\n2" }, { "input": "10\n68572123 724714249\n104192140 802439320\n-890197541 150898768\n-912647426 208916264\n-942374086 -391426562\n-865405682 -491142593\n-623954751 425004801\n368985304 960241411\n-492740831 54907671\n-553720998 567496293", "output": "0\n0\n2\n3\n1\n0\n1\n0\n0\n1" } ]
62
4,608,000
0
18,790
710
String Set Queries
[ "brute force", "data structures", "hashing", "interactive", "string suffix structures", "strings" ]
null
null
You should process *m* queries over a set *D* of strings. Each query is one of three kinds: 1. Add a string *s* to the set *D*. It is guaranteed that the string *s* was not added before. 1. Delete a string *s* from the set *D*. It is guaranteed that the string *s* is in the set *D*. 1. For the given string *s* find the number of occurrences of the strings from the set *D*. If some string *p* from *D* has several occurrences in *s* you should count all of them. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query of the third type. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program.
The first line contains integer *m* (1<=≀<=*m*<=≀<=3Β·105) β€” the number of queries. Each of the next *m* lines contains integer *t* (1<=≀<=*t*<=≀<=3) and nonempty string *s* β€” the kind of the query and the string to process. All strings consist of only lowercase English letters. The sum of lengths of all strings in the input will not exceed 3Β·105.
For each query of the third kind print the only integer *c* β€” the desired number of occurrences in the string *s*.
[ "5\n1 abc\n3 abcabc\n2 abc\n1 aba\n3 abababc\n", "10\n1 abc\n1 bcd\n1 abcd\n3 abcd\n2 abcd\n3 abcd\n2 bcd\n3 abcd\n2 abc\n3 abcd\n" ]
[ "2\n2\n", "3\n2\n1\n0\n" ]
none
[ { "input": "5\n1 abc\n3 abcabc\n2 abc\n1 aba\n3 abababc", "output": "2\n2" }, { "input": "10\n1 abc\n1 bcd\n1 abcd\n3 abcd\n2 abcd\n3 abcd\n2 bcd\n3 abcd\n2 abc\n3 abcd", "output": "3\n2\n1\n0" }, { "input": "2\n1 abbaaabbbababbaaabbbbb\n3 bbbbbbabbbabaabbbbabbb", "output": "0" }, { "input": "2\n1 bbbbbaabbbbbbbabbbaabb\n3 abbabbbaaabaabbbbabbab", "output": "0" }, { "input": "2\n1 aaabebeaaabaaaa\n3 aacaaaadeaaaaaa", "output": "0" }, { "input": "4\n1 wovyprjafpq\n1 lyaemuhgqhp\n2 lyaemuhgqhp\n3 lyaemuhgqhpwovyprjafpq", "output": "1" }, { "input": "4\n1 qfxnsawaokn\n1 lyaemuhgqhp\n2 lyaemuhgqhp\n3 lyaemuhgqhpqfxnsawaokn", "output": "1" }, { "input": "4\n1 jrmvunvtqxg\n1 lyaemuhgqhp\n2 lyaemuhgqhp\n3 lyaemuhgqhpjrmvunvtqxg", "output": "1" }, { "input": "4\n1 auxdlrnqrot\n1 phqghumeayl\n2 phqghumeayl\n3 auxdlrnqrotphqghumeayl", "output": "1" }, { "input": "2\n1 bbbbbbbaabbbabbababbbb\n3 abaabaabbabababbbbbabb", "output": "0" }, { "input": "4\n1 tjcnazfiaur\n1 phqghumeayl\n2 phqghumeayl\n3 tjcnazfiaur", "output": "1" }, { "input": "4\n1 wovyprjafpq\n1 lyaemuhgqhp\n3 wovyprjafpq\n3 wovyprjafpqlyaemuhgqhp", "output": "1\n2" }, { "input": "4\n1 mscphnkknharkkcnm\n1 fxdflnlyaemuhgqhp\n2 mscphnkknharkkcnm\n3 fxdflnlyaemuhgqhp", "output": "1" }, { "input": "4\n1 mscphnkknharkkcnm\n1 fxdflnlyaemuhgqhp\n3 mscphnkknharkkcnm\n3 fxdflnlyaemuhgqhpmscphnkknharkkcnm", "output": "1\n2" }, { "input": "2\n1 abbbabbabaabbaabbbbbbb\n3 bbbabaabbbbbabbbababbb", "output": "0" }, { "input": "2\n1 aabbbbaabbaabbaabbaabb\n3 bbaaaabbbbbbabbbabbbab", "output": "0" }, { "input": "2\n1 bababbbbbbbbbbaabbabbb\n3 abbbbbbbbbaabbbbbbbabb", "output": "0" }, { "input": "2\n1 bbbbabbababababbabbbbb\n3 abbababbbbbbabbbbabbbb", "output": "0" }, { "input": "2\n1 aaaaaabbabbbbbaabaaabb\n3 bbbbbbbababbbbbbabbbab", "output": "0" } ]
3,000
15,155,200
0
18,812
785
Anton and Classes
[ "greedy", "sortings" ]
null
null
Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes. Anton has *n* variants when he will attend chess classes, *i*-th variant is given by a period of time (*l*1,<=*i*,<=*r*1,<=*i*). Also he has *m* variants when he will attend programming classes, *i*-th variant is given by a period of time (*l*2,<=*i*,<=*r*2,<=*i*). Anton needs to choose exactly one of *n* possible periods of time when he will attend chess classes and exactly one of *m* possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal. The distance between periods (*l*1,<=*r*1) and (*l*2,<=*r*2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |*i*<=-<=*j*|, where *l*1<=≀<=*i*<=≀<=*r*1 and *l*2<=≀<=*j*<=≀<=*r*2. In particular, when the periods intersect, the distance between them is 0. Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number!
The first line of the input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of time periods when Anton can attend chess classes. Each of the following *n* lines of the input contains two integers *l*1,<=*i* and *r*1,<=*i* (1<=≀<=*l*1,<=*i*<=≀<=*r*1,<=*i*<=≀<=109)Β β€” the *i*-th variant of a period of time when Anton can attend chess classes. The following line of the input contains a single integer *m* (1<=≀<=*m*<=≀<=200<=000)Β β€” the number of time periods when Anton can attend programming classes. Each of the following *m* lines of the input contains two integers *l*2,<=*i* and *r*2,<=*i* (1<=≀<=*l*2,<=*i*<=≀<=*r*2,<=*i*<=≀<=109)Β β€” the *i*-th variant of a period of time when Anton can attend programming classes.
Output one integerΒ β€” the maximal possible distance between time periods.
[ "3\n1 5\n2 6\n2 3\n2\n2 4\n6 8\n", "3\n1 5\n2 6\n3 7\n2\n2 4\n1 4\n" ]
[ "3\n", "0\n" ]
In the first sample Anton can attend chess classes in the period (2, 3) and attend programming classes in the period (6, 8). It's not hard to see that in this case the distance between the periods will be equal to 3. In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0.
[ { "input": "3\n1 5\n2 6\n2 3\n2\n2 4\n6 8", "output": "3" }, { "input": "3\n1 5\n2 6\n3 7\n2\n2 4\n1 4", "output": "0" }, { "input": "20\n13 141\n57 144\n82 124\n16 23\n18 44\n64 65\n117 133\n84 117\n77 142\n40 119\n105 120\n71 92\n5 142\n48 132\n106 121\n5 80\n45 92\n66 81\n7 93\n27 71\n3\n75 96\n127 140\n54 74", "output": "104" }, { "input": "10\n16 16\n20 20\n13 13\n31 31\n42 42\n70 70\n64 64\n63 63\n53 53\n94 94\n8\n3 3\n63 63\n9 9\n25 25\n11 11\n93 93\n47 47\n3 3", "output": "91" }, { "input": "1\n45888636 261444238\n1\n244581813 591222338", "output": "0" }, { "input": "1\n166903016 182235583\n1\n254223764 902875046", "output": "71988181" }, { "input": "1\n1 1\n1\n1000000000 1000000000", "output": "999999999" }, { "input": "1\n1000000000 1000000000\n1\n1 1", "output": "999999999" }, { "input": "1\n1000000000 1000000000\n1\n1000000000 1000000000", "output": "0" }, { "input": "6\n2 96\n47 81\n3 17\n52 52\n50 105\n1 44\n4\n40 44\n59 104\n37 52\n2 28", "output": "42" }, { "input": "4\n528617953 528617953\n102289603 102289603\n123305570 123305570\n481177982 597599007\n1\n239413975 695033059", "output": "137124372" }, { "input": "7\n617905528 617905554\n617905546 617905557\n617905562 617905564\n617905918 617906372\n617905539 617905561\n617905516 617905581\n617905538 617905546\n9\n617905517 617905586\n617905524 617905579\n617905555 617905580\n617905537 617905584\n617905556 617905557\n617905514 617905526\n617905544 617905579\n617905258 617905514\n617905569 617905573", "output": "404" }, { "input": "5\n999612104 999858319\n68705639 989393889\n297814302 732073321\n577979321 991069087\n601930055 838139173\n14\n109756300 291701768\n2296272 497162877\n3869085 255543683\n662920943 820993688\n54005870 912134860\n1052 70512\n477043210 648640912\n233115268 920170255\n575163323 756904529\n183450026 469145373\n359987405 795448062\n287873006 872825189\n360460166 737511078\n76784767 806771748", "output": "999541592" }, { "input": "1\n1 100000000\n1\n200000000 200000010", "output": "100000000" }, { "input": "1\n999999995 999999996\n1\n999999998 999999999", "output": "2" }, { "input": "1\n10 100\n1\n2 5", "output": "5" }, { "input": "1\n999999992 999999993\n1\n999999996 999999997", "output": "3" }, { "input": "1\n999999997 999999997\n1\n999999999 999999999", "output": "2" }, { "input": "1\n999999999 999999999\n1\n1000000000 1000000000", "output": "1" }, { "input": "1\n1 1000000000\n1\n1000000000 1000000000", "output": "0" }, { "input": "1\n1000000000 1000000000\n1\n999999999 999999999", "output": "1" }, { "input": "1\n100000000 100000001\n1\n100000009 100000011", "output": "8" }, { "input": "1\n5 5\n1\n6 6", "output": "1" }, { "input": "1\n1000000000 1000000000\n1\n1000000000 1000000000", "output": "0" }, { "input": "1\n200000000 200000001\n1\n200000000 200000001", "output": "0" }, { "input": "1\n2 6\n1\n4 8", "output": "0" } ]
2,308
12,185,600
3
18,827
467
Fedor and New Game
[ "bitmasks", "brute force", "constructive algorithms", "implementation" ]
null
null
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game Β«Call of Soldiers 3Β». The game has (*m*<=+<=1) players and *n* types of soldiers in total. Players Β«Call of Soldiers 3Β» are numbered form 1 to (*m*<=+<=1). Types of soldiers are numbered from 0 to *n*<=-<=1. Each player has an army. Army of the *i*-th player can be described by non-negative integer *x**i*. Consider binary representation of *x**i*: if the *j*-th bit of number *x**i* equal to one, then the army of the *i*-th player has soldiers of the *j*-th type. Fedor is the (*m*<=+<=1)-th player of the game. He assume that two players can become friends if their armies differ in at most *k* types of soldiers (in other words, binary representations of the corresponding numbers differ in at most *k* bits). Help Fedor and count how many players can become his friends.
The first line contains three integers *n*, *m*, *k* (1<=≀<=*k*<=≀<=*n*<=≀<=20;Β 1<=≀<=*m*<=≀<=1000). The *i*-th of the next (*m*<=+<=1) lines contains a single integer *x**i* (1<=≀<=*x**i*<=≀<=2*n*<=-<=1), that describes the *i*-th player's army. We remind you that Fedor is the (*m*<=+<=1)-th player.
Print a single integer β€” the number of Fedor's potential friends.
[ "7 3 1\n8\n5\n111\n17\n", "3 3 3\n1\n2\n3\n4\n" ]
[ "0\n", "3\n" ]
none
[ { "input": "7 3 1\n8\n5\n111\n17", "output": "0" }, { "input": "3 3 3\n1\n2\n3\n4", "output": "3" }, { "input": "4 2 2\n5\n6\n7", "output": "2" }, { "input": "4 7 4\n9\n10\n5\n12\n4\n12\n7\n10", "output": "7" }, { "input": "2 7 2\n1\n1\n1\n1\n1\n1\n1\n1", "output": "7" }, { "input": "6 8 2\n46\n59\n38\n5\n13\n54\n26\n62\n18", "output": "2" }, { "input": "5 6 3\n16\n9\n24\n5\n17\n27\n20", "output": "4" }, { "input": "7 9 1\n27\n7\n12\n29\n51\n67\n9\n112\n62\n68", "output": "0" }, { "input": "9 20 2\n197\n45\n217\n9\n156\n335\n21\n90\n33\n292\n151\n153\n289\n483\n362\n509\n234\n115\n343\n341\n50", "output": "1" }, { "input": "13 27 7\n1893\n370\n7320\n5753\n4418\n2146\n2702\n5013\n5959\n1929\n7611\n7013\n540\n3454\n4271\n2887\n4724\n3520\n2111\n5845\n1868\n7659\n3040\n3775\n6507\n4917\n7506\n2515", "output": "20" }, { "input": "1 1 1\n1\n1", "output": "1" }, { "input": "20 1 1\n1000\n2", "output": "0" }, { "input": "3 3 3\n3\n3\n3\n7", "output": "3" }, { "input": "2 2 1\n2\n1\n1", "output": "1" }, { "input": "3 3 2\n7\n7\n6\n2", "output": "3" }, { "input": "20 1 1\n9999\n9997", "output": "1" }, { "input": "5 4 1\n1\n2\n4\n8\n16", "output": "0" }, { "input": "5 2 2\n9\n7\n10", "output": "1" } ]
93
2,457,600
3
18,829
82
Sets
[ "constructive algorithms", "hashing", "implementation" ]
B. Sets
2
256
Little Vasya likes very much to play with sets consisting of positive integers. To make the game more interesting, Vasya chose *n* non-empty sets in such a way, that no two of them have common elements. One day he wanted to show his friends just how interesting playing with numbers is. For that he wrote out all possible unions of two different sets on *n*Β·(*n*<=-<=1)<=/<=2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order. For example, if *n*<==<=4, and the actual sets have the following form {1,<=3}, {5}, {2,<=4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers: - 2,<=7,<=4. - 1,<=7,<=3; - 5,<=4,<=2; - 1,<=3,<=5; - 3,<=1,<=2,<=4; - 5,<=7. Then Vasya showed the pieces of paper to his friends, but kept the *n* sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper?
The first input file line contains a number *n* (2<=≀<=*n*<=≀<=200), *n* is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on *n*Β·(*n*<=-<=1)<=/<=2 lines. Each set starts with the number *k**i* (2<=≀<=*k**i*<=≀<=200), which is the number of numbers written of the *i*-th piece of paper, and then follow *k**i* numbers *a**ij* (1<=≀<=*a**ij*<=≀<=200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from *n* non-intersecting sets.
Print on *n* lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them. It is guaranteed that there is a solution.
[ "4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7\n", "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2\n", "3\n2 1 2\n2 1 3\n2 2 3\n" ]
[ "1 7 \n2 2 4 \n2 1 3 \n1 5 \n", "3 7 8 9 \n2 6 100 \n1 1 \n1 2 \n", "1 1 \n1 2 \n1 3 \n" ]
none
[ { "input": "4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7", "output": "1 7 \n2 2 4 \n2 1 3 \n1 5 " }, { "input": "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2", "output": "3 7 8 9 \n2 6 100 \n1 1 \n1 2 " }, { "input": "3\n2 1 2\n2 1 3\n2 2 3", "output": "1 1 \n1 2 \n1 3 " }, { "input": "3\n2 1 2\n10 1 90 80 70 60 50 40 30 20 10\n10 2 10 20 30 40 50 60 70 80 90", "output": "1 1 \n1 2 \n9 10 20 30 40 50 60 70 80 90 " }, { "input": "4\n4 56 44 53 43\n3 109 44 43\n3 109 56 53\n3 43 62 44\n3 62 56 53\n2 109 62", "output": "2 43 44 \n2 53 56 \n1 109 \n1 62 " }, { "input": "10\n2 32 157\n2 86 157\n2 86 32\n2 154 157\n2 32 154\n2 86 154\n2 157 38\n2 32 38\n2 38 86\n2 38 154\n2 69 157\n2 69 32\n2 69 86\n2 69 154\n2 38 69\n2 172 157\n2 32 172\n2 86 172\n2 172 154\n2 172 38\n2 69 172\n2 157 110\n2 32 110\n2 86 110\n2 154 110\n2 110 38\n2 110 69\n2 172 110\n2 12 157\n2 12 32\n2 12 86\n2 12 154\n2 38 12\n2 12 69\n2 12 172\n2 110 12\n2 157 39\n2 32 39\n2 86 39\n2 39 154\n2 39 38\n2 69 39\n2 172 39\n2 39 110\n2 12 39", "output": "1 157 \n1 32 \n1 86 \n1 154 \n1 38 \n1 69 \n1 172 \n1 110 \n1 12 \n1 39 " }, { "input": "2\n2 1 2", "output": "1 2\n1 1" }, { "input": "2\n10 1 2 3 4 5 6 7 8 9 10", "output": "1 10\n9 1 2 3 4 5 6 7 8 9" } ]
1,466
2,457,600
0
18,838
12
Start of the session
[ "constructive algorithms" ]
E. Start of the season
2
256
Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size *n*<=Γ—<=*n* (*n* is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to *n*<=-<=1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix.
The first line contains one integer *n* (2<=≀<=*n*<=≀<=1000), *n* is even.
Output *n* lines with *n* numbers each β€” the required matrix. Separate numbers with spaces. If there are several solutions, output any.
[ "2\n", "4\n" ]
[ "0 1\n1 0\n", "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0\n" ]
none
[ { "input": "2", "output": "0 1\n1 0" }, { "input": "4", "output": "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0" }, { "input": "6", "output": "0 1 4 2 5 3\n1 0 2 5 3 4\n4 2 0 3 1 5\n2 5 3 0 4 1\n5 3 1 4 0 2\n3 4 5 1 2 0" }, { "input": "8", "output": "0 1 5 2 6 3 7 4\n1 0 2 6 3 7 4 5\n5 2 0 3 7 4 1 6\n2 6 3 0 4 1 5 7\n6 3 7 4 0 5 2 1\n3 7 4 1 5 0 6 2\n7 4 1 5 2 6 0 3\n4 5 6 7 1 2 3 0" }, { "input": "10", "output": "0 1 6 2 7 3 8 4 9 5\n1 0 2 7 3 8 4 9 5 6\n6 2 0 3 8 4 9 5 1 7\n2 7 3 0 4 9 5 1 6 8\n7 3 8 4 0 5 1 6 2 9\n3 8 4 9 5 0 6 2 7 1\n8 4 9 5 1 6 0 7 3 2\n4 9 5 1 6 2 7 0 8 3\n9 5 1 6 2 7 3 8 0 4\n5 6 7 8 9 1 2 3 4 0" }, { "input": "12", "output": "0 1 7 2 8 3 9 4 10 5 11 6\n1 0 2 8 3 9 4 10 5 11 6 7\n7 2 0 3 9 4 10 5 11 6 1 8\n2 8 3 0 4 10 5 11 6 1 7 9\n8 3 9 4 0 5 11 6 1 7 2 10\n3 9 4 10 5 0 6 1 7 2 8 11\n9 4 10 5 11 6 0 7 2 8 3 1\n4 10 5 11 6 1 7 0 8 3 9 2\n10 5 11 6 1 7 2 8 0 9 4 3\n5 11 6 1 7 2 8 3 9 0 10 4\n11 6 1 7 2 8 3 9 4 10 0 5\n6 7 8 9 10 11 1 2 3 4 5 0" }, { "input": "14", "output": "0 1 8 2 9 3 10 4 11 5 12 6 13 7\n1 0 2 9 3 10 4 11 5 12 6 13 7 8\n8 2 0 3 10 4 11 5 12 6 13 7 1 9\n2 9 3 0 4 11 5 12 6 13 7 1 8 10\n9 3 10 4 0 5 12 6 13 7 1 8 2 11\n3 10 4 11 5 0 6 13 7 1 8 2 9 12\n10 4 11 5 12 6 0 7 1 8 2 9 3 13\n4 11 5 12 6 13 7 0 8 2 9 3 10 1\n11 5 12 6 13 7 1 8 0 9 3 10 4 2\n5 12 6 13 7 1 8 2 9 0 10 4 11 3\n12 6 13 7 1 8 2 9 3 10 0 11 5 4\n6 13 7 1 8 2 9 3 10 4 11 0 12 5\n13 7 1 8 2 9 3 10 4 11 5 12 0 6\n7 8 9 10 11 12 13 1 2 3 4 5 6 0" }, { "input": "16", "output": "0 1 9 2 10 3 11 4 12 5 13 6 14 7 15 8\n1 0 2 10 3 11 4 12 5 13 6 14 7 15 8 9\n9 2 0 3 11 4 12 5 13 6 14 7 15 8 1 10\n2 10 3 0 4 12 5 13 6 14 7 15 8 1 9 11\n10 3 11 4 0 5 13 6 14 7 15 8 1 9 2 12\n3 11 4 12 5 0 6 14 7 15 8 1 9 2 10 13\n11 4 12 5 13 6 0 7 15 8 1 9 2 10 3 14\n4 12 5 13 6 14 7 0 8 1 9 2 10 3 11 15\n12 5 13 6 14 7 15 8 0 9 2 10 3 11 4 1\n5 13 6 14 7 15 8 1 9 0 10 3 11 4 12 2\n13 6 14 7 15 8 1 9 2 10 0 11 4 12 5 3\n6 14 7 15 8 1 9 2 10 3 11 0 12 5 13 4\n14 7 15 8 1 9 2 10 3 11 4 12 0 13 6 5\n7 15..." }, { "input": "18", "output": "0 1 10 2 11 3 12 4 13 5 14 6 15 7 16 8 17 9\n1 0 2 11 3 12 4 13 5 14 6 15 7 16 8 17 9 10\n10 2 0 3 12 4 13 5 14 6 15 7 16 8 17 9 1 11\n2 11 3 0 4 13 5 14 6 15 7 16 8 17 9 1 10 12\n11 3 12 4 0 5 14 6 15 7 16 8 17 9 1 10 2 13\n3 12 4 13 5 0 6 15 7 16 8 17 9 1 10 2 11 14\n12 4 13 5 14 6 0 7 16 8 17 9 1 10 2 11 3 15\n4 13 5 14 6 15 7 0 8 17 9 1 10 2 11 3 12 16\n13 5 14 6 15 7 16 8 0 9 1 10 2 11 3 12 4 17\n5 14 6 15 7 16 8 17 9 0 10 2 11 3 12 4 13 1\n14 6 15 7 16 8 17 9 1 10 0 11 3 12 4 13 5 2\n6 15 7 16 8 17 9..." }, { "input": "20", "output": "0 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10\n1 0 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 11\n11 2 0 3 13 4 14 5 15 6 16 7 17 8 18 9 19 10 1 12\n2 12 3 0 4 14 5 15 6 16 7 17 8 18 9 19 10 1 11 13\n12 3 13 4 0 5 15 6 16 7 17 8 18 9 19 10 1 11 2 14\n3 13 4 14 5 0 6 16 7 17 8 18 9 19 10 1 11 2 12 15\n13 4 14 5 15 6 0 7 17 8 18 9 19 10 1 11 2 12 3 16\n4 14 5 15 6 16 7 0 8 18 9 19 10 1 11 2 12 3 13 17\n14 5 15 6 16 7 17 8 0 9 19 10 1 11 2 12 3 13 4 18\n5 15 6 16 7 17 8 18 9 0 10 1 11 2 12 3 13 4 14 19\n1..." }, { "input": "32", "output": "0 1 17 2 18 3 19 4 20 5 21 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 16\n1 0 2 18 3 19 4 20 5 21 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 16 17\n17 2 0 3 19 4 20 5 21 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 16 1 18\n2 18 3 0 4 20 5 21 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 16 1 17 19\n18 3 19 4 0 5 21 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 16 1 17 2 20\n3 19 4 20 5 0 6 22 7 23 8 24 9 25 10 26 11 27 12 28 13 29 14 30 15 31 16 1 1..." }, { "input": "40", "output": "0 1 21 2 22 3 23 4 24 5 25 6 26 7 27 8 28 9 29 10 30 11 31 12 32 13 33 14 34 15 35 16 36 17 37 18 38 19 39 20\n1 0 2 22 3 23 4 24 5 25 6 26 7 27 8 28 9 29 10 30 11 31 12 32 13 33 14 34 15 35 16 36 17 37 18 38 19 39 20 21\n21 2 0 3 23 4 24 5 25 6 26 7 27 8 28 9 29 10 30 11 31 12 32 13 33 14 34 15 35 16 36 17 37 18 38 19 39 20 1 22\n2 22 3 0 4 24 5 25 6 26 7 27 8 28 9 29 10 30 11 31 12 32 13 33 14 34 15 35 16 36 17 37 18 38 19 39 20 1 21 23\n22 3 23 4 0 5 25 6 26 7 27 8 28 9 29 10 30 11 31 12 32 13 33 14 34 ..." }, { "input": "666", "output": "0 1 334 2 335 3 336 4 337 5 338 6 339 7 340 8 341 9 342 10 343 11 344 12 345 13 346 14 347 15 348 16 349 17 350 18 351 19 352 20 353 21 354 22 355 23 356 24 357 25 358 26 359 27 360 28 361 29 362 30 363 31 364 32 365 33 366 34 367 35 368 36 369 37 370 38 371 39 372 40 373 41 374 42 375 43 376 44 377 45 378 46 379 47 380 48 381 49 382 50 383 51 384 52 385 53 386 54 387 55 388 56 389 57 390 58 391 59 392 60 393 61 394 62 395 63 396 64 397 65 398 66 399 67 400 68 401 69 402 70 403 71 404 72 405 73 406 74 407 ..." }, { "input": "66", "output": "0 1 34 2 35 3 36 4 37 5 38 6 39 7 40 8 41 9 42 10 43 11 44 12 45 13 46 14 47 15 48 16 49 17 50 18 51 19 52 20 53 21 54 22 55 23 56 24 57 25 58 26 59 27 60 28 61 29 62 30 63 31 64 32 65 33\n1 0 2 35 3 36 4 37 5 38 6 39 7 40 8 41 9 42 10 43 11 44 12 45 13 46 14 47 15 48 16 49 17 50 18 51 19 52 20 53 21 54 22 55 23 56 24 57 25 58 26 59 27 60 28 61 29 62 30 63 31 64 32 65 33 34\n34 2 0 3 36 4 37 5 38 6 39 7 40 8 41 9 42 10 43 11 44 12 45 13 46 14 47 15 48 16 49 17 50 18 51 19 52 20 53 21 54 22 55 23 56 24 57 2..." }, { "input": "80", "output": "0 1 41 2 42 3 43 4 44 5 45 6 46 7 47 8 48 9 49 10 50 11 51 12 52 13 53 14 54 15 55 16 56 17 57 18 58 19 59 20 60 21 61 22 62 23 63 24 64 25 65 26 66 27 67 28 68 29 69 30 70 31 71 32 72 33 73 34 74 35 75 36 76 37 77 38 78 39 79 40\n1 0 2 42 3 43 4 44 5 45 6 46 7 47 8 48 9 49 10 50 11 51 12 52 13 53 14 54 15 55 16 56 17 57 18 58 19 59 20 60 21 61 22 62 23 63 24 64 25 65 26 66 27 67 28 68 29 69 30 70 31 71 32 72 33 73 34 74 35 75 36 76 37 77 38 78 39 79 40 41\n41 2 0 3 43 4 44 5 45 6 46 7 47 8 48 9 49 10 50 1..." }, { "input": "88", "output": "0 1 45 2 46 3 47 4 48 5 49 6 50 7 51 8 52 9 53 10 54 11 55 12 56 13 57 14 58 15 59 16 60 17 61 18 62 19 63 20 64 21 65 22 66 23 67 24 68 25 69 26 70 27 71 28 72 29 73 30 74 31 75 32 76 33 77 34 78 35 79 36 80 37 81 38 82 39 83 40 84 41 85 42 86 43 87 44\n1 0 2 46 3 47 4 48 5 49 6 50 7 51 8 52 9 53 10 54 11 55 12 56 13 57 14 58 15 59 16 60 17 61 18 62 19 63 20 64 21 65 22 66 23 67 24 68 25 69 26 70 27 71 28 72 29 73 30 74 31 75 32 76 33 77 34 78 35 79 36 80 37 81 38 82 39 83 40 84 41 85 42 86 43 87 44 45\n4..." }, { "input": "96", "output": "0 1 49 2 50 3 51 4 52 5 53 6 54 7 55 8 56 9 57 10 58 11 59 12 60 13 61 14 62 15 63 16 64 17 65 18 66 19 67 20 68 21 69 22 70 23 71 24 72 25 73 26 74 27 75 28 76 29 77 30 78 31 79 32 80 33 81 34 82 35 83 36 84 37 85 38 86 39 87 40 88 41 89 42 90 43 91 44 92 45 93 46 94 47 95 48\n1 0 2 50 3 51 4 52 5 53 6 54 7 55 8 56 9 57 10 58 11 59 12 60 13 61 14 62 15 63 16 64 17 65 18 66 19 67 20 68 21 69 22 70 23 71 24 72 25 73 26 74 27 75 28 76 29 77 30 78 31 79 32 80 33 81 34 82 35 83 36 84 37 85 38 86 39 87 40 88 41..." }, { "input": "100", "output": "0 1 51 2 52 3 53 4 54 5 55 6 56 7 57 8 58 9 59 10 60 11 61 12 62 13 63 14 64 15 65 16 66 17 67 18 68 19 69 20 70 21 71 22 72 23 73 24 74 25 75 26 76 27 77 28 78 29 79 30 80 31 81 32 82 33 83 34 84 35 85 36 86 37 87 38 88 39 89 40 90 41 91 42 92 43 93 44 94 45 95 46 96 47 97 48 98 49 99 50\n1 0 2 52 3 53 4 54 5 55 6 56 7 57 8 58 9 59 10 60 11 61 12 62 13 63 14 64 15 65 16 66 17 67 18 68 19 69 20 70 21 71 22 72 23 73 24 74 25 75 26 76 27 77 28 78 29 79 30 80 31 81 32 82 33 83 34 84 35 85 36 86 37 87 38 88 39..." }, { "input": "128", "output": "0 1 65 2 66 3 67 4 68 5 69 6 70 7 71 8 72 9 73 10 74 11 75 12 76 13 77 14 78 15 79 16 80 17 81 18 82 19 83 20 84 21 85 22 86 23 87 24 88 25 89 26 90 27 91 28 92 29 93 30 94 31 95 32 96 33 97 34 98 35 99 36 100 37 101 38 102 39 103 40 104 41 105 42 106 43 107 44 108 45 109 46 110 47 111 48 112 49 113 50 114 51 115 52 116 53 117 54 118 55 119 56 120 57 121 58 122 59 123 60 124 61 125 62 126 63 127 64\n1 0 2 66 3 67 4 68 5 69 6 70 7 71 8 72 9 73 10 74 11 75 12 76 13 77 14 78 15 79 16 80 17 81 18 82 19 83 20 8..." }, { "input": "144", "output": "0 1 73 2 74 3 75 4 76 5 77 6 78 7 79 8 80 9 81 10 82 11 83 12 84 13 85 14 86 15 87 16 88 17 89 18 90 19 91 20 92 21 93 22 94 23 95 24 96 25 97 26 98 27 99 28 100 29 101 30 102 31 103 32 104 33 105 34 106 35 107 36 108 37 109 38 110 39 111 40 112 41 113 42 114 43 115 44 116 45 117 46 118 47 119 48 120 49 121 50 122 51 123 52 124 53 125 54 126 55 127 56 128 57 129 58 130 59 131 60 132 61 133 62 134 63 135 64 136 65 137 66 138 67 139 68 140 69 141 70 142 71 143 72\n1 0 2 74 3 75 4 76 5 77 6 78 7 79 8 80 9 81 ..." }, { "input": "250", "output": "0 1 126 2 127 3 128 4 129 5 130 6 131 7 132 8 133 9 134 10 135 11 136 12 137 13 138 14 139 15 140 16 141 17 142 18 143 19 144 20 145 21 146 22 147 23 148 24 149 25 150 26 151 27 152 28 153 29 154 30 155 31 156 32 157 33 158 34 159 35 160 36 161 37 162 38 163 39 164 40 165 41 166 42 167 43 168 44 169 45 170 46 171 47 172 48 173 49 174 50 175 51 176 52 177 53 178 54 179 55 180 56 181 57 182 58 183 59 184 60 185 61 186 62 187 63 188 64 189 65 190 66 191 67 192 68 193 69 194 70 195 71 196 72 197 73 198 74 199 ..." }, { "input": "284", "output": "0 1 143 2 144 3 145 4 146 5 147 6 148 7 149 8 150 9 151 10 152 11 153 12 154 13 155 14 156 15 157 16 158 17 159 18 160 19 161 20 162 21 163 22 164 23 165 24 166 25 167 26 168 27 169 28 170 29 171 30 172 31 173 32 174 33 175 34 176 35 177 36 178 37 179 38 180 39 181 40 182 41 183 42 184 43 185 44 186 45 187 46 188 47 189 48 190 49 191 50 192 51 193 52 194 53 195 54 196 55 197 56 198 57 199 58 200 59 201 60 202 61 203 62 204 63 205 64 206 65 207 66 208 67 209 68 210 69 211 70 212 71 213 72 214 73 215 74 216 ..." }, { "input": "332", "output": "0 1 167 2 168 3 169 4 170 5 171 6 172 7 173 8 174 9 175 10 176 11 177 12 178 13 179 14 180 15 181 16 182 17 183 18 184 19 185 20 186 21 187 22 188 23 189 24 190 25 191 26 192 27 193 28 194 29 195 30 196 31 197 32 198 33 199 34 200 35 201 36 202 37 203 38 204 39 205 40 206 41 207 42 208 43 209 44 210 45 211 46 212 47 213 48 214 49 215 50 216 51 217 52 218 53 219 54 220 55 221 56 222 57 223 58 224 59 225 60 226 61 227 62 228 63 229 64 230 65 231 66 232 67 233 68 234 69 235 70 236 71 237 72 238 73 239 74 240 ..." }, { "input": "400", "output": "0 1 201 2 202 3 203 4 204 5 205 6 206 7 207 8 208 9 209 10 210 11 211 12 212 13 213 14 214 15 215 16 216 17 217 18 218 19 219 20 220 21 221 22 222 23 223 24 224 25 225 26 226 27 227 28 228 29 229 30 230 31 231 32 232 33 233 34 234 35 235 36 236 37 237 38 238 39 239 40 240 41 241 42 242 43 243 44 244 45 245 46 246 47 247 48 248 49 249 50 250 51 251 52 252 53 253 54 254 55 255 56 256 57 257 58 258 59 259 60 260 61 261 62 262 63 263 64 264 65 265 66 266 67 267 68 268 69 269 70 270 71 271 72 272 73 273 74 274 ..." }, { "input": "600", "output": "0 1 301 2 302 3 303 4 304 5 305 6 306 7 307 8 308 9 309 10 310 11 311 12 312 13 313 14 314 15 315 16 316 17 317 18 318 19 319 20 320 21 321 22 322 23 323 24 324 25 325 26 326 27 327 28 328 29 329 30 330 31 331 32 332 33 333 34 334 35 335 36 336 37 337 38 338 39 339 40 340 41 341 42 342 43 343 44 344 45 345 46 346 47 347 48 348 49 349 50 350 51 351 52 352 53 353 54 354 55 355 56 356 57 357 58 358 59 359 60 360 61 361 62 362 63 363 64 364 65 365 66 366 67 367 68 368 69 369 70 370 71 371 72 372 73 373 74 374 ..." }, { "input": "700", "output": "0 1 351 2 352 3 353 4 354 5 355 6 356 7 357 8 358 9 359 10 360 11 361 12 362 13 363 14 364 15 365 16 366 17 367 18 368 19 369 20 370 21 371 22 372 23 373 24 374 25 375 26 376 27 377 28 378 29 379 30 380 31 381 32 382 33 383 34 384 35 385 36 386 37 387 38 388 39 389 40 390 41 391 42 392 43 393 44 394 45 395 46 396 47 397 48 398 49 399 50 400 51 401 52 402 53 403 54 404 55 405 56 406 57 407 58 408 59 409 60 410 61 411 62 412 63 413 64 414 65 415 66 416 67 417 68 418 69 419 70 420 71 421 72 422 73 423 74 424 ..." }, { "input": "780", "output": "0 1 391 2 392 3 393 4 394 5 395 6 396 7 397 8 398 9 399 10 400 11 401 12 402 13 403 14 404 15 405 16 406 17 407 18 408 19 409 20 410 21 411 22 412 23 413 24 414 25 415 26 416 27 417 28 418 29 419 30 420 31 421 32 422 33 423 34 424 35 425 36 426 37 427 38 428 39 429 40 430 41 431 42 432 43 433 44 434 45 435 46 436 47 437 48 438 49 439 50 440 51 441 52 442 53 443 54 444 55 445 56 446 57 447 58 448 59 449 60 450 61 451 62 452 63 453 64 454 65 455 66 456 67 457 68 458 69 459 70 460 71 461 72 462 73 463 74 464 ..." }, { "input": "846", "output": "0 1 424 2 425 3 426 4 427 5 428 6 429 7 430 8 431 9 432 10 433 11 434 12 435 13 436 14 437 15 438 16 439 17 440 18 441 19 442 20 443 21 444 22 445 23 446 24 447 25 448 26 449 27 450 28 451 29 452 30 453 31 454 32 455 33 456 34 457 35 458 36 459 37 460 38 461 39 462 40 463 41 464 42 465 43 466 44 467 45 468 46 469 47 470 48 471 49 472 50 473 51 474 52 475 53 476 54 477 55 478 56 479 57 480 58 481 59 482 60 483 61 484 62 485 63 486 64 487 65 488 66 489 67 490 68 491 69 492 70 493 71 494 72 495 73 496 74 497 ..." }, { "input": "902", "output": "0 1 452 2 453 3 454 4 455 5 456 6 457 7 458 8 459 9 460 10 461 11 462 12 463 13 464 14 465 15 466 16 467 17 468 18 469 19 470 20 471 21 472 22 473 23 474 24 475 25 476 26 477 27 478 28 479 29 480 30 481 31 482 32 483 33 484 34 485 35 486 36 487 37 488 38 489 39 490 40 491 41 492 42 493 43 494 44 495 45 496 46 497 47 498 48 499 49 500 50 501 51 502 52 503 53 504 54 505 55 506 56 507 57 508 58 509 59 510 60 511 61 512 62 513 63 514 64 515 65 516 66 517 67 518 68 519 69 520 70 521 71 522 72 523 73 524 74 525 ..." }, { "input": "1000", "output": "0 1 501 2 502 3 503 4 504 5 505 6 506 7 507 8 508 9 509 10 510 11 511 12 512 13 513 14 514 15 515 16 516 17 517 18 518 19 519 20 520 21 521 22 522 23 523 24 524 25 525 26 526 27 527 28 528 29 529 30 530 31 531 32 532 33 533 34 534 35 535 36 536 37 537 38 538 39 539 40 540 41 541 42 542 43 543 44 544 45 545 46 546 47 547 48 548 49 549 50 550 51 551 52 552 53 553 54 554 55 555 56 556 57 557 58 558 59 559 60 560 61 561 62 562 63 563 64 564 65 565 66 566 67 567 68 568 69 569 70 570 71 571 72 572 73 573 74 574 ..." } ]
92
0
0
18,844
0
none
[ "none" ]
null
null
Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contributionΒ β€” an integer number, not necessarily positive. There are *n* registered users and the *i*-th of them has contribution *t**i*. Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments. - Limak can spend *b* minutes to read one blog and upvote it. Author's contribution will be increased by 5. - Limak can spend *c* minutes to read one comment and upvote it. Author's contribution will be increased by 1. Note that it's possible that Limak reads blogs faster than comments. Limak likes ties. He thinks it would be awesome to see a tie between at least *k* registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value *x* that at least *k* registered users have contribution exactly *x*. How much time does Limak need to achieve his goal?
The first line contains four integers *n*, *k*, *b* and *c* (2<=≀<=*k*<=≀<=*n*<=≀<=200<=000,<=1<=≀<=*b*,<=*c*<=≀<=1000)Β β€” the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively. The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (|*t**i*|<=≀<=109) where *t**i* denotes contribution of the *i*-th registered user.
Print the minimum number of minutes Limak will spend to get a tie between at least *k* registered users.
[ "4 3 100 30\n12 2 6 1\n", "4 3 30 100\n12 2 6 1\n", "6 2 987 789\n-8 42 -4 -65 -8 -8\n" ]
[ "220\n", "190\n", "0\n" ]
In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows. - He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6. - Then he spends 4Β·30 = 120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1). In the given scenario, Limak spends 100 + 4Β·30 = 220 minutes and after that each of users 2, 3, 4 has contribution 6. In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100 + 3Β·30 = 190 minutes: - Spend 2Β·30 = 60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12. - Spend 30 + 100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6 + 5 + 1 = 12.
[]
46
0
0
18,869
670
Cinema
[ "implementation", "sortings" ]
null
null
Moscow is hosting a major international conference, which is attended by *n* scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109. In the evening after the conference, all *n* scientists decided to go to the cinema. There are *m* movies in the cinema they came to. Each of the movies is characterized by two distinct numbersΒ β€” the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different). Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists.
The first line of the input contains a positive integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of scientists. The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109), where *a**i* is the index of a language, which the *i*-th scientist knows. The third line contains a positive integer *m* (1<=≀<=*m*<=≀<=200<=000)Β β€” the number of movies in the cinema. The fourth line contains *m* positive integers *b*1,<=*b*2,<=...,<=*b**m* (1<=≀<=*b**j*<=≀<=109), where *b**j* is the index of the audio language of the *j*-th movie. The fifth line contains *m* positive integers *c*1,<=*c*2,<=...,<=*c**m* (1<=≀<=*c**j*<=≀<=109), where *c**j* is the index of subtitles language of the *j*-th movie. It is guaranteed that audio languages and subtitles language are different for each movie, that is *b**j*<=β‰ <=*c**j*.
Print the single integerΒ β€” the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists. If there are several possible answers print any of them.
[ "3\n2 3 2\n2\n3 2\n2 3\n", "6\n6 3 1 1 3 7\n5\n1 2 3 4 5\n2 3 4 5 1\n" ]
[ "2\n", "1\n" ]
In the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied. In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied.
[ { "input": "3\n2 3 2\n2\n3 2\n2 3", "output": "2" }, { "input": "6\n6 3 1 1 3 7\n5\n1 2 3 4 5\n2 3 4 5 1", "output": "1" }, { "input": "1\n10\n1\n10\n3", "output": "1" }, { "input": "2\n1 6\n1\n6\n1", "output": "1" }, { "input": "1\n5\n2\n2 2\n5 5", "output": "1" }, { "input": "2\n4 4\n2\n4 7\n7 5", "output": "1" }, { "input": "10\n3 1 8 8 1 1 5 1 3 5\n2\n1 4\n3 1", "output": "1" }, { "input": "10\n7 6 1 2 7 3 9 7 7 9\n10\n2 9 6 5 9 3 10 3 1 6\n4 6 7 9 7 4 1 9 2 5", "output": "5" }, { "input": "20\n2 2 1 6 6 5 10 2 5 5 4 8 6 8 8 10 2 1 5 6\n20\n1 9 1 1 5 1 9 10 1 10 9 9 8 7 5 1 1 10 2 7\n3 5 9 10 10 8 2 9 6 7 8 8 6 5 3 4 6 3 7 4", "output": "5" }, { "input": "2\n10 8\n10\n8 3 10 8 8 8 3 8 8 8\n10 10 8 3 10 3 8 3 10 10", "output": "1" }, { "input": "5\n9 9 2 4 2\n5\n4 1 1 1 2\n2 4 4 9 7", "output": "5" }, { "input": "1\n320994883\n1\n332200603\n942930950", "output": "1" }, { "input": "3\n1000000000 1 1000000000\n2\n1000000000 1\n1 1000000000", "output": "1" }, { "input": "5\n1 2 3 3 4\n2\n1 2\n3 4", "output": "1" }, { "input": "3\n1 3 2\n2\n1 3\n2 4", "output": "1" }, { "input": "5\n2 2 3 3 4\n3\n5 2 2\n6 3 4", "output": "2" }, { "input": "2\n1 2\n2\n3 3\n4 1", "output": "2" }, { "input": "12\n3 2 1 1 4 4 5 1 6 6 7 7\n4\n3 2 4 7\n2 1 5 6", "output": "4" }, { "input": "14\n1 1 2 2 2 3 3 3 4 5 5 5 6 6\n3\n1 3 5\n2 6 4", "output": "2" } ]
561
69,836,800
3
18,901
107
Basketball Team
[ "combinatorics", "dp", "math", "probabilities" ]
B. Basketball Team
1
256
As a German University in Cairo (GUC) student and a basketball player, Herr Wafa was delighted once he heard the news. GUC is finally participating in the Annual Basketball Competition (ABC). A team is to be formed of *n* players, all of which are GUC students. However, the team might have players belonging to different departments. There are *m* departments in GUC, numbered from 1 to *m*. Herr Wafa's department has number *h*. For each department *i*, Herr Wafa knows number *s**i* β€” how many students who play basketball belong to this department. Herr Wafa was also able to guarantee a spot on the team, using his special powers. But since he hates floating-point numbers, he needs your help at finding the probability that he will have at least one teammate belonging to his department. Note that every possible team containing Herr Wafa is equally probable. Consider all the students different from each other.
The first line contains three integers *n*, *m* and *h* (1<=≀<=*n*<=≀<=100,<=1<=≀<=*m*<=≀<=1000,<=1<=≀<=*h*<=≀<=*m*) β€” the number of players on the team, the number of departments in GUC and Herr Wafa's department, correspondingly. The second line contains a single-space-separated list of *m* integers *s**i* (1<=≀<=*s**i*<=≀<=100), denoting the number of students in the *i*-th department. Note that *s**h* includes Herr Wafa.
Print the probability that Herr Wafa will have at least one teammate from his department. If there is not enough basketball players in GUC to participate in ABC, print -1. The answer will be accepted if it has absolute or relative error not exceeding 10<=-<=6.
[ "3 2 1\n2 1\n", "3 2 1\n1 1\n", "3 2 1\n2 2\n" ]
[ "1\n", "-1\n", "0.666667\n" ]
In the first example all 3 players (2 from department 1 and 1 from department 2) must be chosen for the team. Both players from Wafa's departments will be chosen, so he's guaranteed to have a teammate from his department. In the second example, there are not enough players. In the third example, there are three possibilities to compose the team containing Herr Wafa. In two of them the other player from Herr Wafa's department is part of the team.
[ { "input": "3 2 1\n2 1", "output": "1" }, { "input": "3 2 1\n1 1", "output": "-1" }, { "input": "3 2 1\n2 2", "output": "0.666667" }, { "input": "3 2 1\n1 2", "output": "0.000000" }, { "input": "6 5 3\n5 2 3 10 5", "output": "0.380435" }, { "input": "7 10 6\n9 10 2 3 3 6 9 9 3 7", "output": "0.420946" }, { "input": "17 5 1\n10 4 9 6 2", "output": "0.999860" }, { "input": "5 8 3\n9 7 2 5 2 10 3 4", "output": "0.097561" }, { "input": "14 8 4\n6 2 10 6 2 8 4 2", "output": "0.885750" }, { "input": "14 9 9\n9 4 7 2 1 2 4 3 9", "output": "0.971132" }, { "input": "46 73 68\n4 2 6 4 1 9 8 10 7 8 7 2 6 4 7 9 7 9 9 1 5 1 5 1 8 2 10 2 1 7 10 2 8 3 5 3 8 9 10 5 3 4 10 4 9 6 8 1 1 6 3 1 9 6 9 4 4 3 4 5 8 1 6 2 4 10 5 7 2 6 7 4 2", "output": "0.525158" }, { "input": "24 55 54\n8 3 6 4 8 9 10 2 2 6 6 8 3 4 5 6 6 6 10 4 8 2 3 2 2 2 10 7 10 1 6 1 6 8 10 9 2 8 9 6 6 4 1 2 7 2 2 9 3 7 3 7 6 8 4", "output": "0.433479" }, { "input": "63 25 24\n6 7 7 1 2 5 5 9 9 1 9 8 1 2 10 10 5 10 2 9 5 4 9 5 7", "output": "0.891560" }, { "input": "44 94 2\n2 4 10 9 5 1 9 8 1 3 6 5 5 9 4 6 6 2 6 2 4 5 7 3 8 6 5 10 2 1 1 9 1 9 3 1 9 6 2 4 9 7 4 6 1 4 5 2 7 8 2 1 1 1 4 2 5 5 5 8 2 8 2 1 1 8 1 7 7 7 1 2 5 3 8 9 8 7 2 10 5 2 2 8 9 1 4 7 7 2 6 2 8 5", "output": "0.259627" }, { "input": "44 35 7\n10 2 2 6 4 2 8 3 10 1 9 9 7 9 10 6 6 1 4 5 7 4 9 7 10 10 7 9 6 1 7 7 2 10 7", "output": "0.793743" }, { "input": "27 47 44\n8 5 2 5 10 6 7 9 5 10 8 5 9 5 10 5 10 8 5 1 1 2 2 10 3 2 5 9 6 3 3 1 5 4 10 5 2 2 4 4 4 4 4 1 1 3 7", "output": "0.000000" }, { "input": "21 67 49\n4 4 3 5 7 5 10 2 8 5 2 2 6 3 6 2 8 6 2 6 2 9 3 3 4 1 9 9 3 3 6 3 6 7 8 9 10 6 10 5 1 5 2 3 3 9 10 5 10 7 1 6 4 5 4 7 8 5 4 2 9 3 3 5 7 1 10", "output": "0.414860" }, { "input": "42 71 67\n2 1 4 1 10 5 1 8 8 5 2 1 1 7 2 2 8 10 8 2 10 8 2 2 9 6 5 10 7 1 7 2 10 3 5 6 10 10 4 6 10 5 6 6 9 4 1 6 1 8 10 6 1 5 3 2 4 1 8 5 10 10 9 3 10 7 5 9 1 9 3", "output": "0.362240" }, { "input": "50 93 28\n2 5 9 5 5 8 1 3 9 2 7 10 3 1 10 10 8 5 2 7 5 4 3 9 5 2 8 9 10 8 2 7 8 9 8 1 9 8 4 3 3 6 10 10 1 2 10 1 8 10 5 8 5 2 4 1 5 6 9 8 6 7 4 6 6 1 5 1 4 6 8 4 1 7 2 8 7 5 1 3 3 7 4 2 1 5 7 5 8 3 8 7 2", "output": "0.563739" }, { "input": "33 90 4\n5 10 2 3 9 6 9 3 3 8 6 4 8 4 9 3 5 9 5 6 4 1 10 6 4 5 4 5 9 5 7 1 3 9 6 6 5 6 2 4 8 7 8 5 4 5 10 9 3 1 1 8 6 9 5 1 5 9 4 6 6 4 9 4 5 7 3 7 9 1 5 6 4 1 1 4 2 4 4 2 6 4 5 5 4 9 1 10 2 2", "output": "0.132213" }, { "input": "65 173 136\n26 18 8 11 1 22 44 6 15 22 13 49 30 36 37 41 25 27 9 36 36 1 45 20 7 47 28 30 30 21 33 32 9 11 16 5 19 12 44 40 25 40 32 36 15 34 4 43 28 19 29 33 7 11 18 13 40 18 10 26 1 48 20 38 1 20 34 8 46 8 32 35 16 49 26 36 11 16 4 29 35 44 14 21 22 42 10 1 3 12 35 30 14 45 2 24 32 15 2 28 35 17 48 31 7 26 44 43 37 4 14 26 25 41 18 40 15 32 16 7 40 22 43 8 25 21 35 21 47 45 7 21 50 38 23 13 4 49 10 27 31 38 43 40 10 24 39 35 31 33 9 6 15 18 2 14 20 14 12 12 29 47 9 49 25 17 41 35 9 40 19 50 34", "output": "0.165731" }, { "input": "77 155 26\n15 18 38 46 13 15 43 37 36 28 22 26 9 46 14 32 20 11 8 28 20 42 38 40 31 20 2 43 1 42 25 28 40 47 6 50 42 45 36 28 38 43 31 14 9 22 49 4 41 9 24 35 38 40 19 31 4 9 13 19 15 48 2 34 46 49 41 15 13 29 15 24 15 50 8 26 10 23 24 15 2 46 47 46 25 36 41 29 44 36 24 22 41 7 48 17 42 41 4 46 15 26 48 27 35 19 35 22 47 7 40 1 15 46 6 34 44 6 9 5 29 24 5 25 12 38 46 10 35 12 8 15 1 9 1 16 2 12 24 31 37 49 27 41 33 5 26 48 42 37 20 18 49 40 16", "output": "0.299854" }, { "input": "67 108 14\n33 40 13 10 26 31 27 24 48 1 42 28 38 29 9 28 48 41 12 19 27 50 6 45 46 7 34 47 8 18 40 27 42 4 33 3 10 25 10 29 39 3 5 39 1 17 40 10 6 8 41 50 27 43 40 42 43 25 18 34 6 15 5 9 11 37 13 4 16 25 49 33 14 40 13 16 50 24 4 43 45 12 31 38 40 36 3 4 4 19 18 12 20 44 4 44 8 50 21 5 44 34 9 9 6 39 43 21", "output": "0.504558" }, { "input": "82 135 73\n22 18 8 45 35 8 19 46 40 6 30 40 10 41 43 38 41 40 1 43 19 23 5 13 29 16 30 9 4 42 42 3 24 16 21 26 5 4 24 24 31 30 1 10 45 50 33 21 21 47 42 37 47 15 30 23 4 2 28 15 38 33 45 30 31 32 6 14 6 4 39 12 50 29 26 45 19 12 40 4 33 9 16 12 44 36 47 42 43 17 18 12 12 42 45 38 6 10 19 10 14 31 6 21 2 15 21 26 5 3 3 6 6 22 44 48 9 11 33 31 34 43 39 40 48 26 1 29 48 11 22 38 23 11 20", "output": "0.706768" }, { "input": "73 121 102\n11 21 12 1 48 30 22 42 42 35 33 12 23 11 27 15 50 49 24 2 48 2 21 32 16 48 36 26 32 13 38 46 36 15 27 24 7 21 43 49 19 13 3 41 35 17 5 22 42 19 37 20 40 42 11 31 48 16 21 5 42 23 29 44 9 30 46 21 44 27 9 17 39 24 30 33 48 3 43 18 16 18 17 46 19 26 37 5 24 36 42 12 18 29 7 49 1 9 27 12 21 29 19 38 6 19 43 46 33 42 9 30 19 38 25 10 44 23 50 25 46", "output": "0.470538" }, { "input": "50 113 86\n2 17 43 22 48 40 42 47 32 29 10 4 9 14 20 50 8 29 12 11 50 41 3 22 30 4 48 37 27 19 50 50 23 34 13 21 3 36 31 39 22 27 7 21 31 21 14 18 36 19 27 42 19 8 5 41 7 8 22 40 38 32 44 25 21 48 4 12 10 16 23 30 25 41 16 45 3 26 19 34 34 25 26 6 9 21 46 33 36 45 3 13 28 44 30 29 22 41 20 1 20 38 4 33 36 15 41 18 13 11 13 18 6", "output": "0.298885" }, { "input": "74 146 112\n10 31 40 32 9 17 31 26 32 7 20 18 50 10 15 28 6 41 21 27 11 5 14 36 48 45 10 42 45 40 4 11 41 23 47 31 34 4 42 49 48 9 37 34 25 27 30 27 44 33 30 25 22 13 25 41 8 34 32 22 11 12 32 9 37 9 42 7 37 13 20 40 28 26 2 6 2 49 41 46 11 9 32 18 43 28 39 48 45 36 18 10 28 35 26 5 20 12 16 2 34 28 31 13 18 39 40 1 39 12 33 31 1 31 46 1 47 38 39 49 32 12 2 8 16 27 48 41 16 27 38 42 21 27 26 8 31 41 20 43 47 5 39 25 47 34", "output": "0.437111" }, { "input": "78 124 41\n5 28 46 46 13 48 36 2 28 31 31 12 9 28 40 35 34 50 50 30 17 11 6 36 16 30 29 8 18 16 21 8 15 30 29 20 12 5 29 20 11 44 12 42 49 10 11 7 25 15 2 38 30 29 17 34 4 5 44 49 25 15 16 33 26 8 8 34 21 9 33 16 14 8 43 50 45 17 15 43 44 22 37 36 22 47 6 13 49 48 37 44 50 9 35 13 38 31 15 6 35 48 22 14 18 8 40 18 4 23 2 26 41 41 27 40 43 33 2 17 11 40 42 32", "output": "0.218709" }, { "input": "51 153 26\n19 32 28 7 25 50 22 31 29 39 5 4 28 26 24 1 19 23 36 2 50 50 33 28 15 17 31 35 10 40 16 7 6 43 50 29 20 25 31 37 10 18 38 38 44 30 36 47 37 6 16 48 41 49 14 16 30 7 29 42 36 8 31 37 26 15 43 42 32 3 46 12 16 37 33 12 18 16 15 14 46 11 2 50 34 34 34 32 28 24 44 12 9 38 35 12 11 15 2 6 28 35 14 46 25 30 9 1 26 5 35 26 4 32 2 30 36 29 22 4 5 1 44 38 6 48 48 6 43 45 24 19 44 18 37 18 40 45 25 35 20 27 21 29 43 18 26 46 22 39 29 41 1", "output": "0.183488" }, { "input": "100 10 5\n10 8 7 5 8 1 2 4 3 10", "output": "-1" }, { "input": "100 10 8\n1 8 9 7 6 4 4 6 8 5", "output": "-1" }, { "input": "1 1 1\n1", "output": "0.000000" }, { "input": "1 1 1\n2", "output": "0.000000" }, { "input": "1 1 1\n100", "output": "0.000000" }, { "input": "100 1 1\n100", "output": "1" }, { "input": "99 1 1\n100", "output": "1" }, { "input": "100 2 1\n100 1", "output": "1" } ]
109
12,492,800
0
18,935
2
Commentator problem
[ "geometry" ]
C. Commentator problem
1
64
The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered. Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other.
The input data consists of three lines, each of them describes the position of one stadium. The lines have the format *x*,<=<=*y*,<=<=*r*, where (*x*,<=*y*) are the coordinates of the stadium's center (<=-<=<=103<=≀<=*x*,<=<=*y*<=≀<=103), and *r* (1<=≀<=*r*<=<=≀<=103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line.
Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank.
[ "0 0 10\n60 0 10\n30 30 10\n" ]
[ "30.00000 0.00000\n" ]
none
[ { "input": "0 0 10\n60 0 10\n30 30 10", "output": "30.00000 0.00000" }, { "input": "0 0 10\n100 100 10\n200 0 20", "output": "60.76252 39.23748" }, { "input": "0 0 10\n300 300 11\n500 -500 12", "output": "348.52046 -94.13524" }, { "input": "0 0 10\n300 300 12\n500 -500 14", "output": "311.34912 -88.13335" }, { "input": "0 0 10\n300 300 13\n500 -500 16", "output": "282.61216 -82.24022" }, { "input": "0 0 10\n300 300 15\n500 -500 20", "output": "240.32114 -71.20545" }, { "input": "0 0 10\n300 300 20\n500 -500 40", "output": "154.91933 22.54033" }, { "input": "0 0 10\n300 300 21\n500 -500 42", "output": "148.30948 23.53393" }, { "input": "0 0 10\n300 300 22\n500 -500 44", "output": "142.20438 24.52486" }, { "input": "0 0 30\n300 300 30\n500 -500 20", "output": "469.05250 -169.05250" }, { "input": "0 0 10\n200 0 20\n100 100 10", "output": "60.76252 39.23748" }, { "input": "18 28 24\n192 393 12\n1000 1000 29", "output": "504.34452 425.35835" }, { "input": "614 163 21\n613 -468 18\n-749 679 25", "output": "-214.30328 -350.95260" } ]
140
1,331,200
3.920082
18,940
962
Visible Black Areas
[ "data structures", "dsu", "geometry", "trees" ]
null
null
Petya has a polygon consisting of $n$ vertices. All sides of the Petya's polygon are parallel to the coordinate axes, and each two adjacent sides of the Petya's polygon are perpendicular. It is guaranteed that the polygon is simple, that is, it doesn't have self-intersections and self-touches. All internal area of the polygon (borders are not included) was painted in black color by Petya. Also, Petya has a rectangular window, defined by its coordinates, through which he looks at the polygon. A rectangular window can not be moved. The sides of the rectangular window are parallel to the coordinate axes. Determine the number of black connected areas of Petya's polygon, which can be seen through the rectangular window.
The first line contain four integers $x_1, y_1, x_2, y_2$ ($x_1 &lt; x_2$, $y_2 &lt; y_1$) β€” the coordinates of top-left and bottom-right corners of the rectangular window. The second line contains a single integer $n$ ($4 \le n \le 15\,000$) β€” the number of vertices in Petya's polygon. Each of the following $n$ lines contains two integers β€” the coordinates of vertices of the Petya's polygon in counterclockwise order. Guaranteed, that the given polygon satisfies the conditions described in the statement. All coordinates of the rectangular window and all coordinates of the vertices of the polygon are non-negative and do not exceed $15\,000$.
Print the number of black connected areas of Petya's polygon, which can be seen through the rectangular window.
[ "5 7 16 3\n16\n0 0\n18 0\n18 6\n16 6\n16 1\n10 1\n10 4\n7 4\n7 2\n2 2\n2 6\n12 6\n12 12\n10 12\n10 8\n0 8\n" ]
[ "2" ]
The example corresponds to the picture above.
[ { "input": "5 7 16 3\n16\n0 0\n18 0\n18 6\n16 6\n16 1\n10 1\n10 4\n7 4\n7 2\n2 2\n2 6\n12 6\n12 12\n10 12\n10 8\n0 8", "output": "2" }, { "input": "4 5 6 3\n12\n1 1\n8 1\n8 7\n3 7\n3 6\n7 6\n7 2\n2 2\n2 8\n4 8\n4 9\n1 9", "output": "0" }, { "input": "0 10 10 0\n12\n1 1\n8 1\n8 7\n3 7\n3 6\n7 6\n7 2\n2 2\n2 8\n4 8\n4 9\n1 9", "output": "1" }, { "input": "10 20 20 10\n4\n10 10\n20 10\n20 20\n10 20", "output": "1" }, { "input": "0 7 7 0\n36\n0 0\n2 0\n2 1\n3 1\n3 0\n4 0\n4 1\n5 1\n5 0\n7 0\n7 2\n6 2\n6 3\n7 3\n7 4\n6 4\n6 5\n7 5\n7 7\n5 7\n5 6\n4 6\n4 7\n3 7\n3 6\n2 6\n2 7\n0 7\n0 5\n1 5\n1 4\n0 4\n0 3\n1 3\n1 2\n0 2", "output": "1" }, { "input": "11 15 18 12\n38\n19 10\n19 17\n10 17\n10 12\n12 12\n12 13\n11 13\n11 14\n12 14\n12 15\n11 15\n11 16\n13 16\n13 14\n14 14\n14 15\n15 15\n15 14\n16 14\n16 16\n18 16\n18 15\n17 15\n17 14\n18 14\n18 13\n17 13\n17 12\n18 12\n18 11\n16 11\n16 13\n15 13\n15 12\n14 12\n14 13\n13 13\n13 10", "output": "8" }, { "input": "2 7 8 2\n14\n5 3\n7 3\n7 1\n9 1\n9 6\n1 6\n1 5\n4 5\n4 1\n3 1\n3 3\n1 3\n1 0\n5 0", "output": "2" }, { "input": "14 34 36 4\n62\n16 18\n13 18\n13 14\n18 14\n18 20\n11 20\n11 12\n20 12\n20 22\n9 22\n9 10\n22 10\n22 24\n7 24\n7 8\n24 8\n24 26\n5 26\n5 6\n26 6\n26 28\n3 28\n3 4\n28 4\n28 30\n1 30\n1 2\n30 2\n30 32\n0 32\n0 31\n29 31\n29 3\n2 3\n2 29\n27 29\n27 5\n4 5\n4 27\n25 27\n25 7\n6 7\n6 25\n23 25\n23 9\n8 9\n8 23\n21 23\n21 11\n10 11\n10 21\n19 21\n19 13\n12 13\n12 19\n17 19\n17 15\n14 15\n14 17\n15 17\n15 16\n16 16", "output": "8" }, { "input": "0 10000 10000 0\n4\n0 0\n10000 0\n10000 10000\n0 10000", "output": "1" }, { "input": "0 2 5 1\n16\n0 0\n1 0\n1 2\n2 2\n2 0\n5 0\n5 2\n6 2\n6 0\n7 0\n7 3\n4 3\n4 1\n3 1\n3 3\n0 3", "output": "3" }, { "input": "2 7 6 6\n4\n7 2\n7 7\n1 7\n1 2", "output": "1" }, { "input": "3 7 15 5\n4\n20 3\n20 19\n1 19\n1 3", "output": "1" } ]
46
6,963,200
0
18,955
609
Minimum spanning tree for each edge
[ "data structures", "dfs and similar", "dsu", "graphs", "trees" ]
null
null
Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains *n* vertices and *m* edges. For each edge (*u*,<=*v*) find the minimal possible weight of the spanning tree that contains the edge (*u*,<=*v*). The weight of the spanning tree is the sum of weights of all edges included in spanning tree.
First line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=2Β·105,<=*n*<=-<=1<=≀<=*m*<=≀<=2Β·105) β€” the number of vertices and edges in graph. Each of the next *m* lines contains three integers *u**i*,<=*v**i*,<=*w**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*,<=*u**i*<=β‰ <=*v**i*,<=1<=≀<=*w**i*<=≀<=109) β€” the endpoints of the *i*-th edge and its weight.
Print *m* lines. *i*-th line should contain the minimal possible weight of the spanning tree that contains *i*-th edge. The edges are numbered from 1 to *m* in order of their appearing in input.
[ "5 7\n1 2 3\n1 3 1\n1 4 5\n2 3 2\n2 5 3\n3 4 2\n4 5 4\n" ]
[ "9\n8\n11\n8\n8\n8\n9\n" ]
none
[ { "input": "5 7\n1 2 3\n1 3 1\n1 4 5\n2 3 2\n2 5 3\n3 4 2\n4 5 4", "output": "9\n8\n11\n8\n8\n8\n9" }, { "input": "2 1\n1 2 42", "output": "42" }, { "input": "3 3\n1 2 10\n2 3 20\n3 1 40", "output": "30\n30\n50" }, { "input": "4 6\n1 2 999999001\n1 3 999999003\n1 4 999999009\n2 3 999999027\n2 4 999999243\n3 4 999999729", "output": "2999997013\n2999997013\n2999997013\n2999997037\n2999997247\n2999997733" }, { "input": "8 10\n2 5 4\n7 5 2\n7 3 28\n4 5 14\n3 2 15\n1 2 3\n6 2 5\n2 8 17\n4 6 2\n1 4 10", "output": "48\n48\n61\n57\n48\n48\n48\n48\n48\n53" }, { "input": "8 10\n8 7 11\n3 5 23\n2 1 23\n7 2 13\n6 4 18\n1 4 20\n8 4 17\n2 8 8\n3 2 9\n5 6 29", "output": "106\n106\n109\n108\n106\n106\n106\n106\n106\n112" }, { "input": "7 14\n2 4 25\n6 4 5\n5 6 3\n5 7 9\n6 1 17\n4 7 6\n5 4 25\n1 2 23\n2 3 15\n5 1 10\n7 6 21\n3 7 5\n5 3 4\n5 2 15", "output": "52\n42\n42\n46\n49\n43\n62\n50\n42\n42\n58\n42\n42\n42" }, { "input": "7 10\n2 1 12\n3 1 10\n3 4 5\n6 4 6\n7 4 20\n5 4 17\n3 2 5\n7 5 8\n3 6 16\n2 5 21", "output": "53\n51\n51\n51\n54\n51\n51\n51\n61\n55" }, { "input": "10 10\n9 4 16\n6 1 4\n5 4 4\n1 2 11\n8 2 22\n5 10 29\n7 5 24\n2 4 15\n1 3 7\n7 9 24", "output": "132\n132\n132\n132\n132\n132\n132\n132\n132\n132" } ]
2,000
227,225,600
0
18,957
731
Coupons and Discounts
[ "constructive algorithms", "greedy" ]
null
null
The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition. Teams plan to train for *n* times during *n* consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be *a**i* teams on the *i*-th day. There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total). As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days. Sereja wants to order exactly *a**i* pizzas on the *i*-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day *n*.
The first line of input contains a single integer *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the number of training sessions. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=≀<=*a**i*<=≀<=10<=000)Β β€” the number of teams that will be present on each of the days.
If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).
[ "4\n1 2 1 2\n", "3\n1 0 1\n" ]
[ "YES\n", "NO\n" ]
In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample. In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.
[ { "input": "4\n1 2 1 2", "output": "YES" }, { "input": "3\n1 0 1", "output": "NO" }, { "input": "3\n1 3 1", "output": "NO" }, { "input": "3\n2 0 2", "output": "YES" }, { "input": "1\n179", "output": "NO" }, { "input": "10\n0 0 5 9 9 3 0 0 0 10", "output": "YES" }, { "input": "3\n3 2 3", "output": "YES" }, { "input": "1\n0", "output": "YES" }, { "input": "2\n0 0", "output": "YES" }, { "input": "10\n0 0 0 0 0 0 0 0 0 0", "output": "YES" }, { "input": "1\n1", "output": "NO" }, { "input": "1\n2", "output": "YES" }, { "input": "1\n3", "output": "NO" }, { "input": "1\n10000", "output": "YES" }, { "input": "2\n10000 10000", "output": "YES" }, { "input": "3\n2 2 2", "output": "YES" }, { "input": "10\n3 3 3 2 2 2 2 2 2 3", "output": "YES" }, { "input": "100\n2 3 2 3 3 3 3 3 3 2 2 2 2 2 2 3 2 3 3 2 3 2 3 2 2 3 3 3 3 3 2 2 2 2 3 2 3 3 2 2 3 2 3 3 3 3 2 2 3 3 3 3 3 2 3 3 3 2 2 2 2 3 2 2 2 2 3 2 2 3 2 2 2 3 2 2 3 2 2 2 3 3 3 2 2 2 2 3 2 2 3 3 3 2 2 2 2 2 3 3", "output": "NO" }, { "input": "3\n0 0 1", "output": "NO" }, { "input": "10\n1 0 1 1 0 1 1 0 1 0", "output": "NO" }, { "input": "100\n1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 0 0 1 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 1 0 1 1 1 1 1 0 1 1 0 0 0 1 0 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1", "output": "NO" }, { "input": "10\n8 4 0 0 6 1 9 8 0 6", "output": "YES" }, { "input": "100\n44 0 0 0 16 0 0 0 0 77 9 0 94 0 78 0 0 50 55 35 0 35 88 27 0 0 86 0 0 56 0 0 17 23 0 22 54 36 0 0 94 36 0 22 0 0 0 0 0 0 0 82 0 0 50 0 6 0 0 44 80 0 0 0 98 0 0 0 0 92 0 56 0 16 0 14 0 37 89 0 62 3 83 0 0 0 80 0 92 58 92 0 0 0 57 79 0 0 0 42", "output": "YES" }, { "input": "100\n37 92 14 95 3 37 0 0 0 84 27 33 0 0 0 74 74 0 35 72 46 29 8 92 1 76 47 0 38 82 0 81 54 7 61 46 91 0 86 0 80 0 0 98 88 0 4 0 0 52 0 0 82 0 33 35 0 36 58 52 1 50 29 0 0 24 0 69 97 65 13 0 30 0 14 66 47 94 22 24 8 92 67 0 34 0 0 0 84 85 50 33 0 99 67 73 21 0 0 62", "output": "YES" }, { "input": "100\n56 22 13 79 28 73 16 55 34 0 97 19 22 36 22 80 30 19 36 92 9 38 24 10 61 43 19 12 18 34 21 36 1 17 0 97 72 37 74 70 51 34 33 87 27 33 45 97 38 56 2 32 88 92 64 51 74 94 86 98 57 62 83 3 87 61 9 65 57 13 64 10 50 35 7 75 41 3 70 66 6 55 69 42 91 75 14 22 68 93 2 53 22 98 45 2 78 58 18 13", "output": "YES" }, { "input": "2\n1 4", "output": "NO" }, { "input": "4\n2 1 1 2", "output": "YES" }, { "input": "5\n1 1 1 0 1", "output": "NO" }, { "input": "4\n1 0 2 2", "output": "NO" }, { "input": "3\n3 2 1", "output": "YES" }, { "input": "2\n1 0", "output": "NO" }, { "input": "2\n1 2", "output": "NO" }, { "input": "3\n2 1 1", "output": "YES" }, { "input": "3\n3 0 0", "output": "NO" }, { "input": "9\n6 3 5 9 0 3 1 9 6", "output": "NO" }, { "input": "4\n1 0 1 1", "output": "NO" }, { "input": "4\n1 1 1 0", "output": "NO" }, { "input": "2\n1 5", "output": "YES" }, { "input": "3\n1 0 2", "output": "NO" }, { "input": "3\n1 2 2", "output": "NO" }, { "input": "3\n1 2 1", "output": "YES" }, { "input": "3\n1 4 1", "output": "YES" }, { "input": "3\n3 2 2", "output": "NO" } ]
46
6,963,200
0
18,992
295
Yaroslav and Points
[ "data structures" ]
null
null
Yaroslav has *n* points that lie on the *Ox* axis. The coordinate of the first point is *x*1, the coordinate of the second point is *x*2, ..., the coordinate of the *n*-th point is β€” *x**n*. Now Yaroslav wants to execute *m* queries, each of them is of one of the two following types: 1. Move the *p**j*-th point from position *x**p**j* to position *x**p**j*<=+<=*d**j*. At that, it is guaranteed that after executing such query all coordinates of the points will be distinct. 1. Count the sum of distances between all pairs of points that lie on the segment [*l**j*,<=*r**j*] (*l**j*<=≀<=*r**j*). In other words, you should count the sum of: . Help Yaroslav.
The first line contains integer *n* β€” the number of points (1<=≀<=*n*<=≀<=105). The second line contains distinct integers *x*1,<=*x*2,<=...,<=*x**n* β€” the coordinates of points (|*x**i*|<=≀<=109). The third line contains integer *m* β€” the number of queries (1<=≀<=*m*<=≀<=105). The next *m* lines contain the queries. The *j*-th line first contains integer *t**j* (1<=≀<=*t**j*<=≀<=2) β€” the query type. If *t**j*<==<=1, then it is followed by two integers *p**j* and *d**j* (1<=≀<=*p**j*<=≀<=*n*,<=|*d**j*|<=≀<=1000). If *t**j*<==<=2, then it is followed by two integers *l**j* and *r**j* (<=-<=109<=≀<=*l**j*<=≀<=*r**j*<=≀<=109). It is guaranteed that at any moment all the points have distinct coordinates.
For each type 2 query print the answer on a single line. Print the answers in the order, in which the queries follow in the input. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier.
[ "8\n36 50 28 -75 40 -60 -95 -48\n20\n2 -61 29\n1 5 -53\n1 1 429\n1 5 130\n2 -101 -71\n2 -69 53\n1 1 404\n1 5 518\n2 -101 53\n2 50 872\n1 1 -207\n2 -99 -40\n1 7 -389\n1 6 -171\n1 2 464\n1 7 -707\n1 1 -730\n1 1 560\n2 635 644\n1 7 -677\n" ]
[ "176\n20\n406\n1046\n1638\n156\n0\n" ]
none
[]
92
0
0
19,001
575
Bots
[ "combinatorics", "dp", "math", "number theory" ]
null
null
Sasha and Ira are two best friends. But they aren’t just friends, they are software engineers and experts in artificial intelligence. They are developing an algorithm for two bots playing a two-player game. The game is cooperative and turn based. In each turn, one of the players makes a move (it doesn’t matter which player, it's possible that players turns do not alternate). Algorithm for bots that Sasha and Ira are developing works by keeping track of the state the game is in. Each time either bot makes a move, the state changes. And, since the game is very dynamic, it will never go back to the state it was already in at any point in the past. Sasha and Ira are perfectionists and want their algorithm to have an optimal winning strategy. They have noticed that in the optimal winning strategy, both bots make exactly *N* moves each. But, in order to find the optimal strategy, their algorithm needs to analyze all possible states of the game (they haven’t learned about alpha-beta pruning yet) and pick the best sequence of moves. They are worried about the efficiency of their algorithm and are wondering what is the total number of states of the game that need to be analyzed?
The first and only line contains integer N. - 1<=≀<=*N*<=≀<=106
Output should contain a single integer – number of possible states modulo 109<=+<=7.
[ "2\n" ]
[ "19\n" ]
Start: Game is in state A. - Turn 1: Either bot can make a move (first bot is red and second bot is blue), so there are two possible states after the first turn – B and C. - Turn 2: In both states B and C, either bot can again make a turn, so the list of possible states is expanded to include D, E, F and G. - Turn 3: Red bot already did N=2 moves when in state D, so it cannot make any more moves there. It can make moves when in state E, F and G, so states I, K and M are added to the list. Similarly, blue bot cannot make a move when in state G, but can when in D, E and F, so states H, J and L are added. - Turn 4: Red bot already did N=2 moves when in states H, I and K, so it can only make moves when in J, L and M, so states P, R and S are added. Blue bot cannot make a move when in states J, L and M, but only when in H, I and K, so states N, O and Q are added. Overall, there are 19 possible states of the game their algorithm needs to analyze. <img class="tex-graphics" src="https://espresso.codeforces.com/3d9ef2ab59018319b986a58a65362116fa5be88d.png" style="max-width: 100.0%;max-height: 100.0%;"/>
[ { "input": "2", "output": "19" }, { "input": "1", "output": "5" }, { "input": "3", "output": "69" }, { "input": "4", "output": "251" }, { "input": "5", "output": "923" }, { "input": "6", "output": "3431" }, { "input": "7", "output": "12869" }, { "input": "8", "output": "48619" }, { "input": "9", "output": "184755" }, { "input": "10", "output": "705431" }, { "input": "99", "output": "407336794" }, { "input": "999", "output": "72475737" }, { "input": "9999", "output": "703593269" }, { "input": "99999", "output": "879467332" }, { "input": "999999", "output": "192151599" }, { "input": "524287", "output": "295397547" }, { "input": "131071", "output": "920253602" }, { "input": "178481", "output": "845172388" }, { "input": "524288", "output": "250289717" }, { "input": "1000000", "output": "627314155" } ]
1,500
307,200
0
19,003
595
Pasha and Phone
[ "binary search", "math" ]
null
null
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly *n* digits. Also Pasha has a number *k* and two sequences of length *n*<=/<=*k* (*n* is divisible by *k*) *a*1,<=*a*2,<=...,<=*a**n*<=/<=*k* and *b*1,<=*b*2,<=...,<=*b**n*<=/<=*k*. Let's split the phone number into blocks of length *k*. The first block will be formed by digits from the phone number that are on positions 1, 2,..., *k*, the second block will be formed by digits from the phone number that are on positions *k*<=+<=1, *k*<=+<=2, ..., 2Β·*k* and so on. Pasha considers a phone number good, if the *i*-th block doesn't start from the digit *b**i* and is divisible by *a**i* if represented as an integer. To represent the block of length *k* as an integer, let's write it out as a sequence *c*1, *c*2,...,*c**k*. Then the integer is calculated as the result of the expression *c*1Β·10*k*<=-<=1<=+<=*c*2Β·10*k*<=-<=2<=+<=...<=+<=*c**k*. Pasha asks you to calculate the number of good phone numbers of length *n*, for the given *k*, *a**i* and *b**i*. As this number can be too big, print it modulo 109<=+<=7.
The first line of the input contains two integers *n* and *k* (1<=≀<=*n*<=≀<=100<=000, 1<=≀<=*k*<=≀<=*min*(*n*,<=9))Β β€” the length of all phone numbers and the length of each block, respectively. It is guaranteed that *n* is divisible by *k*. The second line of the input contains *n*<=/<=*k* space-separated positive integersΒ β€” sequence *a*1,<=*a*2,<=...,<=*a**n*<=/<=*k* (1<=≀<=*a**i*<=&lt;<=10*k*). The third line of the input contains *n*<=/<=*k* space-separated positive integersΒ β€” sequence *b*1,<=*b*2,<=...,<=*b**n*<=/<=*k* (0<=≀<=*b**i*<=≀<=9).
Print a single integerΒ β€” the number of good phone numbers of length *n* modulo 109<=+<=7.
[ "6 2\n38 56 49\n7 3 4\n", "8 2\n1 22 3 44\n5 4 3 2\n" ]
[ "8\n", "32400\n" ]
In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
[ { "input": "6 2\n38 56 49\n7 3 4", "output": "8" }, { "input": "8 2\n1 22 3 44\n5 4 3 2", "output": "32400" }, { "input": "2 1\n9 9\n9 9", "output": "1" }, { "input": "2 1\n9 9\n0 9", "output": "1" }, { "input": "4 1\n4 3 2 1\n1 2 3 4", "output": "540" }, { "input": "18 9\n2 3\n0 4", "output": "505000007" }, { "input": "4 4\n1122\n2", "output": "8" }, { "input": "10 5\n8378 11089\n7 5", "output": "99" }, { "input": "10 5\n52057 11807\n0 1", "output": "8" }, { "input": "10 1\n3 1 1 4 8 7 5 6 4 1\n0 0 0 5 5 6 8 8 4 0", "output": "209952" }, { "input": "100 4\n388 2056 122 1525 2912 1465 3066 257 5708 3604 3039 6183 3035 626 1389 5393 3321 3175 2922 2024 3837 437 5836 2376 1599\n6 5 5 2 9 6 8 3 5 0 6 0 1 8 5 3 5 2 3 0 5 6 6 7 3", "output": "652599557" }, { "input": "100 1\n5 3 1 5 6 2 4 8 3 3 1 1 2 8 2 3 8 2 5 2 6 2 3 5 2 1 2 1 2 8 4 3 3 5 1 4 2 2 2 5 8 2 2 6 2 9 2 4 1 8 1 5 5 6 6 1 2 7 3 3 4 2 4 1 2 6 6 4 9 4 3 2 3 8 2 3 1 4 1 4 1 3 5 3 5 5 2 3 4 1 1 8 1 5 6 9 4 2 5 1\n6 0 4 5 3 1 0 7 5 3 9 4 5 4 0 2 1 6 2 2 4 3 1 9 5 9 2 2 6 8 6 5 9 6 4 9 9 7 5 4 5 6 0 3 2 0 8 0 3 9 5 3 8 0 9 3 6 2 9 5 9 3 2 2 2 2 0 8 1 2 9 0 9 8 0 3 2 0 7 9 4 3 7 2 3 1 8 9 8 2 6 0 3 2 9 8 9 2 3 4", "output": "27157528" }, { "input": "100 5\n5302 4362 11965 14930 11312 33797 17413 17850 79562 17981 28002 40852 173 23022 55762 13013 79597 29597 31944 32384\n9 8 7 0 6 6 7 7 5 9 1 3 4 8 7 1 1 6 4 4", "output": "885507108" }, { "input": "1 1\n2\n0", "output": "4" } ]
139
31,027,200
3
19,027
799
Beautiful fountains rows
[ "data structures" ]
null
null
Butler Ostin wants to show Arkady that rows of odd number of fountains are beautiful, while rows of even number of fountains are not. The butler wants to show Arkady *n* gardens. Each garden is a row of *m* cells, the *i*-th garden has one fountain in each of the cells between *l**i* and *r**i* inclusive, and there are no more fountains in that garden. The issue is that some of the gardens contain even number of fountains, it is wrong to show them to Arkady. Ostin wants to choose two integers *a*<=≀<=*b* and show only part of each of the gardens that starts at cell *a* and ends at cell *b*. Of course, only such segments suit Ostin that each garden has either zero or odd number of fountains on this segment. Also, it is necessary that at least one garden has at least one fountain on the segment from *a* to *b*. Help Ostin to find the total length of all such segments, i.e. sum up the value (*b*<=-<=*a*<=+<=1) for each suitable pair (*a*,<=*b*).
The first line contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=2Β·105)Β β€” the number of gardens and the length of each garden. *n* lines follow. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*m*)Β β€” the bounds of the segment that contains fountains in the *i*-th garden.
Print one integer: the total length of all suitable segments.
[ "1 5\n2 4\n", "3 6\n2 4\n3 6\n4 4\n" ]
[ "23\n", "19\n" ]
In the first example the following pairs suit Ostin: (*a*, *b*): (1, 2), (1, 4), (1, 5), (2, 2), (2, 4), (2, 5), (3, 3), (4, 4), (4, 5). In the second example the following pairs suit Ostin: (*a*, *b*): (1, 2), (1, 5), (2, 2), (2, 5), (3, 3), (4, 4), (4, 6), (5, 5), (6, 6).
[]
62
0
0
19,044
0
none
[ "none" ]
null
null
In this problem you need to write job distribution subsystem for testing submissions. Invokers (simplistically) are components of testing system, which test given submission on single test and give back a verdict. In this problem two verdicts are possible β€” OK (test passed) or RJ (rejected, test failed). Job distribution subsystem will be named scheduler. You have to implement it. For each problem two parameters are given: number of tests in a problem and time limit for a problem. We will consider that the system works discreetly and one tick equals 10 milliseconds. If some event occupied in time moment not divisible by 10 ms then scheduler will get it in the nearest next tick. After the end of the contest (i.e., a week after its start) the last solution you sent (having positive score) will be chosen to be launched on the final tests. The final tests are confidential and distinct from those that will be used during the competition. The total number of points that will be scored in the final tests will determine the winner of the competition. Specially for this round we implemented feature to submit ZIP-archive with multiple source files. All files should be in root of ZIP-file, no directories are allowed. You can use this feature for: - Java 8: main should be in a class Main of default package; - GNU C++ 11: exactly one file should contain the entry point main, all files to compile should have extension cpp. You will be receiving submissions and invoker verdicts in interactive mode. Please be sure to use the stream flushing operation after each of your moves to prevent output buffering. For example, you can use "fflush(stdout)" in C or C++, "System.out.flush()" in Java and "flush(output)" in Pascal. The problem is estimated as follows: let *a**i* is a full testing time (including expectations) of the *i*-th submission, that is time, passed from the moment of receiving the submission until the moment when it is fully tested. Let's find , where *n* is the number of submissions to test. The exact number of points for single test is calculated by the formula , where *A* is a *r* for some simple scheduler, written by jury, and *B* is a *r* for your scheduler. Materials for local testing will be published soon. The package with materials will contain an interactor and a set of test data for the interactor.
On the start your program reads the following data from standard input: - in the first line you are given an integer number *t* (1<=≀<=*t*<=≀<=500) β€” the number of available invokers (all invokers work simultaneously and independently, free invoker starts testing a job as soon as it is appointed to him); - in the second line you are given an integer number *p* (1<=≀<=*p*<=≀<=10000) β€” the number of problems, submissions to which are expected to be tested. Further *p* lines with description of problems are followed. Each description contains problem time limit (integer number between 250 and 30000) and the number of tests (from 1 to 1000). The following data is given in interactive mode. It means that a following block of data will be available only after your scheduler writes information of its behavior on the previous tick. Firstly, block of data about new submissions, needed to be tested, follows for a tick. Each submission is described by single integer number β€” index of the problem (from 0 to *p*<=-<=1). The value -1 means that there are no new submissions, needed to be tested, in this tick. Further block of testing results (from invokers) in this tick follows. These lines contain three elements: index of submission, a number of test and the verdict of test (OK or RJ). The block of testing results ends with a line "-1 -1". Consider that problems and tests are numbered (from zero) in the order of its appearance. Total number of submissions for one test does not exceed 20000.
After reading the data about the next tick your scheduler can decide to send submissions for exact testing. The submission can be tested in any test at any time. Your scheduler must output a couple of integers β€” index of submission and a number of test in a single line in order to start a test for the submission. If there are a free invoker, a testing of a submission immediately begins (invoker becomes busy until the result comes back). Otherwise, your request will be ignored. When you are done sending requests, output line "-1 -1". You can simply maintain the amount of free invokers in your program, reducing each time variable *t* while sending the task to invokers (if *t*<==<=0, there is no need to send) and increasing *t*, if invoker returns a verdict. It is considered that the submission was tested if it was tested on all tests of the problem, or if it was tested on all tests up to first RJ verdict (inclusive). Interactor breaks out at a time when all the planned submissions will be tested completely. In this case your submission should stop, as soon as it finds closing of input data stream.
[ "1\n1\n500 2\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n0\n-1\n-1 -1\n0\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n0 0 OK\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n0 1 OK\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n1 0 RJ\n-1 -1\n" ]
[ "-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n0 0\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n0 1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n1 0\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n1 1\n-1 -1\n" ]
Materials and tests are available now at [http://assets.codeforces.com/files/vk/vkcup-2016-wr2-materials-v1.tar.gz](//assets.codeforces.com/files/vk/vkcup-2016-wr2-materials-v1.tar.gz). Please read README.txt to learn how to test your solution with the interactor.
[]
0
0
-1
19,052
954
Runner's Problem
[ "dp", "matrices", "sortings" ]
null
null
You are running through a rectangular field. This field can be represented as a matrix with 3 rows and *m* columns. (*i*,<=*j*) denotes a cell belonging to *i*-th row and *j*-th column. You start in (2,<=1) and have to end your path in (2,<=*m*). From the cell (*i*,<=*j*) you may advance to: - (*i*<=-<=1,<=*j*<=+<=1) β€” only if *i*<=&gt;<=1, - (*i*,<=*j*<=+<=1), or - (*i*<=+<=1,<=*j*<=+<=1) β€” only if *i*<=&lt;<=3. However, there are *n* obstacles blocking your path. *k*-th obstacle is denoted by three integers *a**k*, *l**k* and *r**k*, and it forbids entering any cell (*a**k*,<=*j*) such that *l**k*<=≀<=*j*<=≀<=*r**k*. You have to calculate the number of different paths from (2,<=1) to (2,<=*m*), and print it modulo 109<=+<=7.
The first line contains two integers *n* and *m* (1<=≀<=*n*<=≀<=104, 3<=≀<=*m*<=≀<=1018) β€” the number of obstacles and the number of columns in the matrix, respectively. Then *n* lines follow, each containing three integers *a**k*, *l**k* and *r**k* (1<=≀<=*a**k*<=≀<=3, 2<=≀<=*l**k*<=≀<=*r**k*<=≀<=*m*<=-<=1) denoting an obstacle blocking every cell (*a**k*,<=*j*) such that *l**k*<=≀<=*j*<=≀<=*r**k*. Some cells may be blocked by multiple obstacles.
Print the number of different paths from (2,<=1) to (2,<=*m*), taken modulo 109<=+<=7. If it is impossible to get from (2,<=1) to (2,<=*m*), then the number of paths is 0.
[ "2 5\n1 3 4\n2 2 3\n" ]
[ "2\n" ]
none
[ { "input": "2 5\n1 3 4\n2 2 3", "output": "2" }, { "input": "50 100\n3 24 49\n2 10 12\n1 87 92\n2 19 60\n2 53 79\n3 65 82\n3 10 46\n1 46 86\n2 55 84\n1 50 53\n3 80 81\n3 66 70\n2 35 52\n1 63 69\n2 65 87\n3 68 75\n1 33 42\n1 56 90\n3 73 93\n2 20 26\n2 42 80\n2 83 87\n3 99 99\n1 14 79\n2 94 97\n1 66 85\n1 7 73\n1 50 50\n2 16 40\n2 76 94\n1 71 98\n1 99 99\n1 61 87\n3 98 98\n2 11 41\n3 67 78\n1 31 58\n3 81 85\n1 81 94\n3 41 83\n3 46 65\n1 94 94\n3 31 38\n1 19 35\n3 50 54\n3 85 90\n3 47 63\n3 62 87\n1 18 75\n1 30 41", "output": "0" }, { "input": "50 100\n1 71 96\n2 34 52\n2 16 95\n1 54 55\n1 65 85\n1 76 92\n2 19 91\n1 26 43\n2 83 95\n2 70 88\n2 67 88\n1 9 75\n2 4 50\n2 9 11\n1 77 92\n1 28 58\n1 23 72\n1 24 75\n2 12 50\n1 54 55\n2 45 93\n1 88 93\n2 98 99\n1 40 58\n2 40 42\n1 16 61\n2 94 94\n1 82 86\n2 81 85\n2 46 46\n2 88 97\n2 6 86\n1 30 86\n2 87 96\n1 44 50\n2 43 88\n1 29 98\n1 39 76\n1 78 94\n1 6 69\n2 92 95\n1 40 68\n1 97 99\n1 85 85\n1 69 74\n1 23 51\n1 34 66\n2 70 98\n2 94 97\n1 54 73", "output": "5" } ]
46
0
0
19,146
842
Vitya and Strange Lesson
[ "binary search", "data structures" ]
null
null
Today at the lesson Vitya learned a very interesting functionΒ β€” mex. Mex of a sequence of numbers is the minimum non-negative number that is not present in the sequence as element. For example, *mex*([4,<=33,<=0,<=1,<=1,<=5])<==<=2 and *mex*([1,<=2,<=3])<==<=0. Vitya quickly understood all tasks of the teacher, but can you do the same? You are given an array consisting of *n* non-negative integers, and *m* queries. Each query is characterized by one number *x* and consists of the following consecutive steps: - Perform the bitwise addition operation modulo 2 (xor) of each array element with the number *x*. - Find mex of the resulting array. Note that after each query the array changes.
First line contains two integer numbers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=3Β·105)Β β€” number of elements in array and number of queries. Next line contains *n* integer numbers *a**i* (0<=≀<=*a**i*<=≀<=3Β·105)Β β€” elements of then array. Each of next *m* lines contains queryΒ β€” one integer number *x* (0<=≀<=*x*<=≀<=3Β·105).
For each query print the answer on a separate line.
[ "2 2\n1 3\n1\n3\n", "4 3\n0 1 5 6\n1\n2\n4\n", "5 4\n0 1 5 6 7\n1\n1\n4\n5\n" ]
[ "1\n0\n", "2\n0\n0\n", "2\n2\n0\n2\n" ]
none
[ { "input": "2 2\n1 3\n1\n3", "output": "1\n0" }, { "input": "4 3\n0 1 5 6\n1\n2\n4", "output": "2\n0\n0" }, { "input": "5 4\n0 1 5 6 7\n1\n1\n4\n5", "output": "2\n2\n0\n2" }, { "input": "5 5\n1 2 3 4 5\n1\n2\n3\n4\n5", "output": "1\n3\n0\n2\n1" }, { "input": "9 3\n2 3 4 5 6 7 8 9 10\n1\n2\n3", "output": "0\n2\n0" }, { "input": "10 30\n0 0 0 0 0 0 0 0 0 0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0", "output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1" }, { "input": "17 30\n4194 1990 2257 1363 2798 386 3311 3152 1808 1453 3874 4388 1268 3924 3799 1269 968\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8\n8", "output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0" } ]
2,000
6,656,000
0
19,178
940
Machine Learning
[ "brute force", "data structures" ]
null
null
You come home and fell some unpleasant smell. Where is it coming from? You are given an array *a*. You have to answer the following queries: 1. You are given two integers *l* and *r*. Let *c**i* be the number of occurrences of *i* in *a**l*:<=*r*, where *a**l*:<=*r* is the subarray of *a* from *l*-th element to *r*-th inclusive. Find the Mex of {*c*0,<=*c*1,<=...,<=*c*109} 1. You are given two integers *p* to *x*. Change *a**p* to *x*. The Mex of a multiset of numbers is the smallest non-negative integer not in the set. Note that in this problem all elements of *a* are positive, which means that *c*0 = 0 and 0 is never the answer for the query of the second type.
The first line of input contains two integers *n* and *q* (1<=≀<=*n*,<=*q*<=≀<=100<=000)Β β€” the length of the array and the number of queries respectively. The second line of input contains *n* integersΒ β€” *a*1, *a*2, ..., *a**n* (1<=≀<=*a**i*<=≀<=109). Each of the next *q* lines describes a single query. The first type of query is described by three integers *t**i*<==<=1, *l**i*, *r**i*, where 1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*Β β€” the bounds of the subarray. The second type of query is described by three integers *t**i*<==<=2, *p**i*, *x**i*, where 1<=≀<=*p**i*<=≀<=*n* is the index of the element, which must be changed and 1<=≀<=*x**i*<=≀<=109 is the new value.
For each query of the first type output a single integer Β β€” the Mex of {*c*0,<=*c*1,<=...,<=*c*109}.
[ "10 4\n1 2 3 1 1 2 2 2 9 9\n1 1 1\n1 2 8\n2 7 1\n1 2 8\n" ]
[ "2\n3\n2\n" ]
The subarray of the first query consists of the single elementΒ β€” 1. The subarray of the second query consists of four 2s, one 3 and two 1s. The subarray of the fourth query consists of three 1s, three 2s and one 3.
[ { "input": "10 4\n1 2 3 1 1 2 2 2 9 9\n1 1 1\n1 2 8\n2 7 1\n1 2 8", "output": "2\n3\n2" } ]
4,000
135,168,000
0
19,239
594
REQ
[ "data structures", "number theory" ]
null
null
Today on a math lesson the teacher told Vovochka that the Euler function of a positive integer Ο†(*n*) is an arithmetic function that counts the positive integers less than or equal to n that are relatively prime to n. The number 1 is coprime to all the positive integers and Ο†(1)<==<=1. Now the teacher gave Vovochka an array of *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* and a task to process *q* queries *l**i* *r**i*Β β€” to calculate and print modulo 109<=+<=7. As it is too hard for a second grade school student, you've decided to help Vovochka.
The first line of the input contains number *n* (1<=≀<=*n*<=≀<=200<=000)Β β€” the length of the array given to Vovochka. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=106). The third line contains integer *q* (1<=≀<=*q*<=≀<=200<=000)Β β€” the number of queries. Next *q* lines contain the queries, one per line. Each query is defined by the boundaries of the segment *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*).
Print *q* numbers β€” the value of the Euler function for each query, calculated modulo 109<=+<=7.
[ "10\n1 2 3 4 5 6 7 8 9 10\n7\n1 1\n3 8\n5 6\n4 8\n8 10\n7 9\n7 10\n", "7\n24 63 13 52 6 10 1\n6\n3 5\n4 7\n1 7\n2 4\n3 6\n2 6\n" ]
[ "1\n4608\n8\n1536\n192\n144\n1152\n", "1248\n768\n12939264\n11232\n9984\n539136\n" ]
In the second sample the values are calculated like that: - Ο†(13Β·52Β·6) = φ(4056) = 1248 - Ο†(52Β·6Β·10Β·1) = φ(3120) = 768 - Ο†(24Β·63Β·13Β·52Β·6Β·10Β·1) = φ(61326720) = 12939264 - Ο†(63Β·13Β·52) = φ(42588) = 11232 - Ο†(13Β·52Β·6Β·10) = φ(40560) = 9984 - Ο†(63Β·13Β·52Β·6Β·10) = φ(2555280) = 539136
[ { "input": "10\n1 2 3 4 5 6 7 8 9 10\n7\n1 1\n3 8\n5 6\n4 8\n8 10\n7 9\n7 10", "output": "1\n4608\n8\n1536\n192\n144\n1152" }, { "input": "7\n24 63 13 52 6 10 1\n6\n3 5\n4 7\n1 7\n2 4\n3 6\n2 6", "output": "1248\n768\n12939264\n11232\n9984\n539136" }, { "input": "5\n2 3 4 5 6\n5\n2 5\n2 2\n1 5\n2 3\n2 4", "output": "96\n2\n192\n4\n16" }, { "input": "1\n560149\n1\n1 1", "output": "560148" }, { "input": "10\n570570 570570 930930 870870 570570 903210 746130 930930 930930 930930\n10\n1 1\n2 8\n7 9\n2 6\n6 8\n4 8\n1 1\n9 10\n3 3\n1 4", "output": "103680\n318621451\n527979517\n716367138\n457727889\n629095486\n103680\n864702880\n172800\n916551950" }, { "input": "19\n315553 551648 583644 900821 913348 429897 180052 889652 759534 685141 15340 659749 413620 137295 192336 421022 546366 612600 683011\n20\n18 18\n12 15\n7 16\n7 15\n2 10\n14 18\n9 12\n13 17\n2 16\n1 5\n3 19\n3 3\n3 4\n11 13\n4 5\n2 14\n5 11\n12 19\n1 15\n5 10", "output": "163200\n475366378\n597854865\n929642857\n563596944\n545464902\n728372354\n960791317\n764355760\n562848199\n914318373\n183040\n886091652\n310208405\n379268163\n629833585\n962167533\n771170864\n239595114\n712695808" }, { "input": "20\n39820 179494 514786 504333 809650 354659 812783 502726 477018 706364 79176 236379 55592 596907 287364 653769 899015 214093 636223 250607\n18\n6 10\n2 5\n6 8\n4 9\n5 5\n8 16\n11 12\n9 18\n10 18\n11 17\n11 20\n14 20\n1 13\n7 20\n15 20\n4 19\n8 12\n19 20", "output": "793095787\n43455937\n900236543\n394081652\n323840\n713146045\n787112932\n643607188\n375506593\n860078464\n766900035\n349331076\n525610850\n729079241\n78928241\n222023397\n728062803\n107480655" }, { "input": "19\n710357 781854 569626 616635 721994 711144 82545 3451 937228 207998 780960 722017 324806 270002 904330 744586 275574 464202 436073\n19\n4 6\n7 10\n10 15\n2 6\n1 2\n1 16\n18 18\n4 9\n11 12\n9 16\n2 3\n4 5\n5 18\n7 14\n7 18\n10 17\n12 19\n10 11\n14 17", "output": "983951760\n133287506\n309859795\n387114647\n587906079\n939595516\n138240\n636049617\n444381172\n528204840\n623754811\n107652476\n500324047\n475248994\n844724352\n28447096\n461076071\n454035204\n923802677" }, { "input": "10\n528985 936188 742768 646102 635926 310713 99394 440480 200430 551266\n20\n4 10\n1 4\n4 6\n1 3\n2 10\n3 4\n9 10\n6 9\n1 1\n2 5\n3 6\n5 6\n6 10\n6 6\n1 7\n10 10\n1 5\n5 6\n3 9\n9 10", "output": "580148919\n187209938\n648424595\n677944904\n919654504\n354852993\n619742545\n277531939\n414000\n244529692\n163892742\n606755434\n830608793\n184320\n765107277\n256608\n291779355\n606755434\n814151120\n619742545" }, { "input": "20\n793398 752408 238352 775625 929132 465822 849561 810507 156687 817866 327920 915769 678672 487682 163959 476796 479909 204624 943318 892191\n10\n6 7\n14 15\n8 20\n5 17\n2 2\n11 20\n3 6\n7 11\n17 19\n13 17", "output": "266167550\n687257446\n36761289\n888773112\n373248\n307299290\n201982287\n550471414\n730015749\n721510471" }, { "input": "17\n86982 341510 779926 700440 866320 790548 406470 808470 503034 604968 41910 251490 830620 484120 901230 831720 685146\n20\n6 10\n13 15\n4 16\n3 7\n14 17\n6 15\n10 17\n2 15\n12 12\n8 14\n9 11\n1 6\n6 11\n4 7\n5 6\n3 14\n7 10\n6 10\n5 10\n9 12", "output": "816011795\n891384963\n141343300\n635651671\n871641405\n89941064\n458061543\n994311786\n65600\n174800928\n306426074\n555497637\n770198597\n338938634\n229723320\n970467650\n199055370\n816011795\n333295911\n813676340" }, { "input": "18\n603330 813450 435540 889980 949410 507210 774060 396270 735930 286230 850080 695640 71610 761838 352716 188790 503580 256620\n18\n4 17\n9 18\n1 7\n6 9\n1 13\n3 9\n4 17\n6 13\n5 17\n3 12\n13 16\n4 6\n13 18\n4 17\n7 11\n1 17\n13 17\n5 10", "output": "962117190\n49281424\n235819299\n957293612\n135996759\n584808383\n962117190\n82912\n420330761\n293495233\n302593786\n930936619\n931753978\n962117190\n695072904\n798965379\n194405686\n998949772" }, { "input": "20\n746130 870870 870870 690690 930930 690690 746130 570570 881790 746130 746130 570570 881790 930930 570570 570570 510510 870870 903210 881790\n20\n1 9\n4 19\n1 9\n4 20\n14 15\n15 17\n7 13\n2 15\n2 5\n15 17\n5 12\n12 20\n6 14\n2 19\n5 11\n8 18\n11 13\n2 10\n7 14\n6 10", "output": "909089325\n734423493\n909089325\n287359221\n405311349\n863882737\n133535169\n64838050\n510994565\n863882737\n165833490\n879414760\n312123490\n563002853\n392449949\n230934252\n321916940\n909089325\n832909993\n107657898" }, { "input": "18\n900606 871752 108024 697506 784490 116616 806799 762210 655910 853074 62031 10062 516768 135924 778845 683422 228124 261205\n17\n9 18\n2 3\n6 9\n5 6\n1 12\n3 17\n3 8\n4 17\n6 8\n4 12\n12 17\n10 13\n15 18\n2 10\n6 14\n4 15\n1 13", "output": "79284990\n858732362\n668434638\n230963060\n924418212\n37057644\n862952010\n490988331\n333898554\n421160259\n43038327\n804491893\n808552201\n786261546\n565340524\n572509844\n538200736" }, { "input": "20\n664950 643110 737490 671160 794430 366366 549010 757302 425334 397670 531930 506220 548730 938630 899130 380190 726180 463980 450870 398580\n10\n10 11\n8 11\n1 9\n8 8\n2 7\n11 20\n1 14\n5 13\n3 9\n14 19", "output": "809192701\n530233436\n910177950\n186624\n399661455\n596733551\n989131896\n92282202\n302488769\n271705538" }, { "input": "10\n566566 98532 543270 424914 66430 253590 728916 377580 240465 279370\n11\n1 10\n5 8\n7 10\n7 9\n3 3\n1 9\n2 3\n6 9\n2 9\n1 3\n6 7", "output": "216676960\n346424055\n142252585\n200377928\n114048\n804712412\n116513722\n740913213\n703384006\n502260687\n375647443" }, { "input": "18\n400200 839190 358190 826182 102718 615020 194370 272370 871194 678405 418860 552420 814980 218730 727272 552270 18564 843486\n19\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18\n1 18", "output": "494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828\n494432828" }, { "input": "20\n881790 570570 870870 510510 881790 746130 930930 746130 903210 881790 881790 881790 903210 903210 690690 881790 903210 930930 903210 746130\n17\n2 19\n2 20\n1 19\n1 20\n2 19\n1 19\n1 20\n2 20\n1 19\n2 20\n2 19\n1 19\n2 20\n2 19\n1 20\n1 20\n1 20", "output": "217845625\n155043463\n92324092\n774281765\n217845625\n92324092\n774281765\n155043463\n92324092\n155043463\n217845625\n92324092\n155043463\n217845625\n774281765\n774281765\n774281765" } ]
3,000
307,200
0
19,329
65
Harry Potter and the History of Magic
[ "brute force", "greedy", "implementation" ]
B. Harry Potter and the History of Magic
1
256
The History of Magic is perhaps the most boring subject in the Hogwarts school of Witchcraft and Wizardry. Harry Potter is usually asleep during history lessons, and his magical quill writes the lectures for him. Professor Binns, the history of magic teacher, lectures in such a boring and monotonous voice, that he has a soporific effect even on the quill. That's why the quill often makes mistakes, especially in dates. So, at the end of the semester Professor Binns decided to collect the students' parchments with notes and check them. Ron Weasley is in a panic: Harry's notes may contain errors, but at least he has some notes, whereas Ron does not have any. Ronald also has been sleeping during the lectures and his quill had been eaten by his rat Scabbers. Hermione Granger refused to give Ron her notes, because, in her opinion, everyone should learn on their own. Therefore, Ron has no choice but to copy Harry's notes. Due to the quill's errors Harry's dates are absolutely confused: the years of goblin rebellions and other important events for the wizarding world do not follow in order, and sometimes even dates from the future occur. Now Ron wants to change some of the digits while he copies the notes so that the dates were in the chronological (i.e. non-decreasing) order and so that the notes did not have any dates strictly later than 2011, or strictly before than 1000. To make the resulting sequence as close as possible to the one dictated by Professor Binns, Ron will change no more than one digit in each date into other digit. Help him do it.
The first input line contains an integer *n* (1<=≀<=*n*<=≀<=1000). It represents the number of dates in Harry's notes. Next *n* lines contain the actual dates *y*1, *y*2, ..., *y**n*, each line contains a date. Each date is a four-digit integer (1000<=≀<=*y**i*<=≀<=9999).
Print *n* numbers *z*1, *z*2, ..., *z**n* (1000<=≀<=*z**i*<=≀<=2011). They are Ron's resulting dates. Print each number on a single line. Numbers *z**i* must form the non-decreasing sequence. Each number *z**i* should differ from the corresponding date *y**i* in no more than one digit. It is not allowed to change the first digit of a number into 0. If there are several possible solutions, print any of them. If there's no solution, print "No solution" (without the quotes).
[ "3\n1875\n1936\n1721\n", "4\n9999\n2000\n3000\n3011\n", "3\n1999\n5055\n2000\n" ]
[ "1835\n1836\n1921\n", "1999\n2000\n2000\n2011\n", "No solution\n" ]
none
[ { "input": "3\n1875\n1936\n1721", "output": "1075\n1136\n1221" }, { "input": "4\n9999\n2000\n3000\n3011", "output": "1999\n2000\n2000\n2011" }, { "input": "3\n1999\n5055\n2000", "output": "No solution" }, { "input": "2\n2037\n2025", "output": "1037\n2005" }, { "input": "1\n1234", "output": "1034" }, { "input": "1\n9876", "output": "1876" }, { "input": "2\n9988\n8899", "output": "No solution" }, { "input": "3\n1095\n1094\n1095", "output": "1005\n1014\n1015" }, { "input": "5\n5555\n4444\n3333\n2222\n1111", "output": "No solution" }, { "input": "3\n2010\n2011\n2012", "output": "1010\n1011\n1012" }, { "input": "5\n1901\n1166\n1308\n1037\n1808", "output": "1001\n1066\n1108\n1137\n1208" }, { "input": "5\n1612\n7835\n8183\n3368\n1685", "output": "No solution" }, { "input": "10\n1501\n1617\n1368\n1737\n1800\n1272\n1019\n1545\n1035\n1302", "output": "1001\n1017\n1068\n1137\n1200\n1202\n1219\n1245\n1335\n1342" }, { "input": "10\n7577\n1411\n1864\n1604\n1589\n1343\n6832\n1648\n1222\n1832", "output": "1577\n1611\n1664\n1664\n1689\n1743\n1832\n1848\n1922\n1932" }, { "input": "10\n1110\n1278\n1283\n7758\n1183\n1214\n2970\n1398\n7515\n1005", "output": "No solution" }, { "input": "15\n2003\n1991\n1741\n1348\n1258\n1964\n1411\n1431\n1780\n1701\n1787\n1094\n1108\n1074\n1942", "output": "1003\n1091\n1141\n1148\n1158\n1164\n1211\n1231\n1280\n1301\n1387\n1394\n1408\n1474\n1542" }, { "input": "20\n1749\n1792\n1703\n1011\n1289\n1066\n1947\n1354\n1693\n1806\n1645\n1292\n1718\n1981\n1197\n1471\n1603\n1325\n1057\n1552", "output": "1049\n1092\n1103\n1111\n1189\n1266\n1347\n1350\n1393\n1406\n1445\n1492\n1518\n1581\n1597\n1671\n1673\n1725\n1757\n1852" }, { "input": "20\n1639\n1437\n1054\n1010\n1872\n1942\n1315\n1437\n1226\n1893\n1712\n1024\n1410\n1691\n1188\n1056\n1642\n1100\n1893\n1192", "output": "No solution" }, { "input": "20\n1025\n1000\n1026\n1085\n1354\n1783\n3490\n1512\n1553\n1682\n1695\n1654\n1679\n1304\n1574\n1814\n1854\n1804\n1928\n1949", "output": "1005\n1005\n1006\n1015\n1054\n1083\n1490\n1502\n1503\n1582\n1595\n1604\n1609\n1704\n1774\n1804\n1804\n1804\n1828\n1849" }, { "input": "20\n1011\n1157\n2181\n6218\n1766\n8319\n1364\n6428\n1476\n4417\n6618\n1629\n1747\n1786\n1787\n2830\n7671\n1953\n1275\n1099", "output": "No solution" }, { "input": "50\n1230\n6040\n1035\n1973\n9096\n5133\n1146\n1164\n9195\n5211\n6212\n1313\n1953\n1560\n1382\n2324\n1343\n1481\n1555\n1363\n1487\n1414\n1525\n1564\n1561\n9585\n7590\n1663\n5625\n1630\n1630\n3644\n1164\n1665\n7678\n1282\n1626\n1798\n9755\n7801\n8809\n1762\n1867\n1861\n1826\n1809\n8902\n1033\n1774\n9978", "output": "1030\n1040\n1045\n1073\n1096\n1133\n1136\n1144\n1195\n1211\n1212\n1213\n1253\n1260\n1282\n1324\n1333\n1381\n1455\n1463\n1467\n1474\n1505\n1514\n1521\n1585\n1590\n1603\n1625\n1630\n1630\n1644\n1664\n1664\n1678\n1682\n1686\n1698\n1755\n1801\n1809\n1862\n1862\n1862\n1866\n1869\n1902\n1933\n1974\n1978" }, { "input": "10\n1014\n1140\n1692\n1644\n3647\n1716\n4821\n9839\n2882\n1664", "output": "1004\n1040\n1092\n1144\n1647\n1706\n1821\n1839\n1882\n1964" }, { "input": "10\n1075\n1133\n1393\n1350\n1369\n1403\n2643\n1653\n1756\n7811", "output": "1005\n1033\n1093\n1150\n1169\n1203\n1643\n1643\n1656\n1811" }, { "input": "10\n6025\n1522\n1835\n2142\n1414\n9547\n1456\n6784\n4984\n3992", "output": "1025\n1122\n1135\n1142\n1214\n1547\n1556\n1784\n1984\n1992" }, { "input": "10\n1074\n1547\n1554\n1581\n1170\n8683\n1434\n4750\n1866\n1051", "output": "1004\n1047\n1054\n1081\n1100\n1683\n1734\n1750\n1766\n1851" }, { "input": "10\n2008\n3007\n4066\n1017\n1920\n1113\n1317\n4746\n1972\n1598", "output": "No solution" }, { "input": "10\n1171\n1275\n1680\n7300\n4742\n2517\n7980\n1852\n1993\n5004", "output": "No solution" }, { "input": "2\n1999\n1000", "output": "1099\n1100" }, { "input": "2\n2004\n1000", "output": "1004\n1004" }, { "input": "2\n2099\n1000", "output": "1099\n1100" }, { "input": "12\n1000\n1002\n1021\n1006\n1001\n1036\n1038\n1039\n1098\n1097\n1029\n1053", "output": "1000\n1000\n1001\n1001\n1001\n1006\n1008\n1009\n1018\n1027\n1027\n1033" }, { "input": "2\n1011\n1000", "output": "1001\n1001" }, { "input": "3\n1012\n1101\n1000", "output": "1002\n1100\n1100" }, { "input": "3\n2000\n3999\n6011", "output": "1000\n1999\n2011" } ]
93
307,200
0
19,348
0
none
[ "none" ]
null
null
In some country live wizards. They love to ride trolleybuses. A city in this country has a trolleybus depot with *n* trolleybuses. Every day the trolleybuses leave the depot, one by one and go to the final station. The final station is at a distance of *d* meters from the depot. We know for the *i*-th trolleybus that it leaves at the moment of time *t**i* seconds, can go at a speed of no greater than *v**i* meters per second, and accelerate with an acceleration no greater than *a* meters per second squared. A trolleybus can decelerate as quickly as you want (magic!). It can change its acceleration as fast as you want, as well. Note that the maximum acceleration is the same for all trolleys. Despite the magic the trolleys are still powered by an electric circuit and cannot overtake each other (the wires are to blame, of course). If a trolleybus catches up with another one, they go together one right after the other until they arrive at the final station. Also, the drivers are driving so as to arrive at the final station as quickly as possible. You, as head of the trolleybuses' fans' club, are to determine for each trolley the minimum time by which it can reach the final station. At the time of arrival at the destination station the trolleybus does not necessarily have zero speed. When a trolley is leaving the depot, its speed is considered equal to zero. From the point of view of physics, the trolleybuses can be considered as material points, and also we should ignore the impact on the speed of a trolley bus by everything, except for the acceleration and deceleration provided by the engine.
The first input line contains three space-separated integers *n*, *a*, *d* (1<=≀<=*n*<=≀<=105, 1<=≀<=*a*,<=*d*<=≀<=106) β€” the number of trolleybuses, their maximum acceleration and the distance from the depot to the final station, correspondingly. Next *n* lines contain pairs of integers *t**i* *v**i* (0<=≀<=*t*1<=&lt;<=*t*2...<=&lt;<=*t**n*<=-<=1<=&lt;<=*t**n*<=≀<=106, 1<=≀<=*v**i*<=≀<=106) β€” the time when the *i*-th trolleybus leaves the depot and its maximum speed, correspondingly. The numbers in the lines are separated by spaces.
For each trolleybus print a single line the time it arrives to the final station. Print the times for the trolleybuses in the order in which the trolleybuses are given in the input. The answer will be accepted if the absolute or relative error doesn't exceed 10<=-<=4.
[ "3 10 10000\n0 10\n5 11\n1000 1\n", "1 2 26\n28 29\n" ]
[ "1000.5000000000\n1000.5000000000\n11000.0500000000\n", "33.0990195136\n" ]
In the first sample the second trolleybus will catch up with the first one, that will happen at distance 510.5 meters from the depot. The trolleybuses will go the remaining 9489.5 meters together at speed 10 meters per second. As a result, both trolleybuses will arrive to the final station by the moment of time 1000.5 seconds. The third trolleybus will not catch up with them. It will arrive to the final station by the moment of time 11000.05 seconds.
[ { "input": "3 10 10000\n0 10\n5 11\n1000 1", "output": "1000.5000000000\n1000.5000000000\n11000.0500000000" }, { "input": "1 2 26\n28 29", "output": "33.0990195136" }, { "input": "7 8 3\n1 3\n5 26\n7 3\n10 15\n18 7\n21 17\n23 21", "output": "2.1875000000\n5.8660254038\n8.1875000000\n10.8660254038\n18.8660254038\n21.8660254038\n23.8660254038" }, { "input": "3 6 6\n2 10\n14 19\n18 14", "output": "3.4142135624\n15.4142135624\n19.4142135624" }, { "input": "10 7 8\n2 4\n3 13\n4 7\n5 1\n9 16\n10 9\n12 18\n16 4\n17 16\n20 6", "output": "4.2857142857\n4.5118578920\n5.6428571429\n13.0714285714\n13.0714285714\n13.0714285714\n13.5118578920\n18.2857142857\n18.5118578920\n21.7619047619" }, { "input": "8 4 13\n0 18\n6 24\n10 25\n11 5\n12 18\n20 22\n21 8\n22 12", "output": "2.5495097568\n8.5495097568\n12.5495097568\n14.2250000000\n14.5495097568\n22.5495097568\n23.6250000000\n24.5495097568" }, { "input": "1 2 7\n20 13", "output": "22.6457513111" }, { "input": "3 3 3\n13 1\n18 12\n19 2", "output": "16.1666666667\n19.4142135624\n20.8333333333" }, { "input": "8 7 21\n2 11\n3 4\n4 3\n9 23\n15 9\n16 5\n22 17\n24 10", "output": "4.6948051948\n8.5357142857\n11.2142857143\n11.4494897428\n17.9761904762\n20.5571428571\n24.4495798319\n26.8142857143" }, { "input": "3 6 19\n12 3\n20 24\n30 2", "output": "18.5833333333\n22.5166114784\n39.6666666667" }, { "input": "4 5 14\n11 1\n16 20\n17 15\n21 7", "output": "25.1000000000\n25.1000000000\n25.1000000000\n25.1000000000" }, { "input": "1 1 722397\n556297 454495", "output": "557498.9958402590" }, { "input": "1 100000 363166\n560443 753304", "output": "560445.6950547304" }, { "input": "1 124232 477338\n899117 898233", "output": "899119.7721151346" }, { "input": "1 1000000 1000000\n0 1000000", "output": "1.5000000000" }, { "input": "1 1 1\n0 1000000", "output": "1.4142135624" } ]
1,000
6,963,200
0
19,359
404
Restore Graph
[ "dfs and similar", "graphs", "sortings" ]
null
null
Valera had an undirected connected graph without self-loops and multiple edges consisting of *n* vertices. The graph had an interesting property: there were at most *k* edges adjacent to each of its vertices. For convenience, we will assume that the graph vertices were indexed by integers from 1 to *n*. One day Valera counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array *d*. Thus, element *d*[*i*] of the array shows the shortest distance from the vertex Valera chose to vertex number *i*. Then something irreparable terrible happened. Valera lost the initial graph. However, he still has the array *d*. Help him restore the lost graph.
The first line contains two space-separated integers *n* and *k* (1<=≀<=*k*<=&lt;<=*n*<=≀<=105). Number *n* shows the number of vertices in the original graph. Number *k* shows that at most *k* edges were adjacent to each vertex in the original graph. The second line contains space-separated integers *d*[1],<=*d*[2],<=...,<=*d*[*n*] (0<=≀<=*d*[*i*]<=&lt;<=*n*). Number *d*[*i*] shows the shortest distance from the vertex Valera chose to the vertex number *i*.
If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer *m* (0<=≀<=*m*<=≀<=106) β€” the number of edges in the found graph. In each of the next *m* lines print two space-separated integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*;Β *a**i*<=β‰ <=*b**i*), denoting the edge that connects vertices with numbers *a**i* and *b**i*. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them.
[ "3 2\n0 1 1\n", "4 2\n2 0 1 3\n", "3 1\n0 0 0\n" ]
[ "3\n1 2\n1 3\n3 2\n", "3\n1 3\n1 4\n2 3\n", "-1\n" ]
none
[ { "input": "3 2\n0 1 1", "output": "2\n1 2\n1 3" }, { "input": "4 2\n2 0 1 3", "output": "3\n1 3\n1 4\n2 3" }, { "input": "3 1\n0 0 0", "output": "-1" }, { "input": "5 3\n0 2 1 2 1", "output": "4\n1 3\n1 5\n2 5\n4 5" }, { "input": "7 3\n2 2 0 1 3 2 1", "output": "6\n1 7\n2 7\n3 4\n3 7\n4 6\n5 6" }, { "input": "9 4\n2 1 1 3 1 2 2 1 0", "output": "8\n1 8\n2 9\n3 9\n4 7\n5 9\n6 8\n7 8\n8 9" }, { "input": "3 1\n0 1 2", "output": "-1" }, { "input": "2 1\n1 0", "output": "1\n1 2" }, { "input": "2 1\n1 1", "output": "-1" }, { "input": "2 1\n0 0", "output": "-1" }, { "input": "4 2\n0 1 0 1", "output": "-1" }, { "input": "5 3\n3 1 4 2 1", "output": "-1" }, { "input": "5 4\n1 2 2 2 3", "output": "-1" }, { "input": "4 3\n1 1 1 2", "output": "-1" }, { "input": "4 3\n1 2 2 3", "output": "-1" }, { "input": "4 3\n1 2 3 3", "output": "-1" }, { "input": "7 6\n1 1 2 2 3 3 3", "output": "-1" }, { "input": "10 5\n0 1 1 1 1 1 1 2 2 2", "output": "-1" }, { "input": "5 4\n0 1 1 1 4", "output": "-1" } ]
77
0
0
19,389
631
Print Check
[ "constructive algorithms", "implementation" ]
null
null
Kris works in a large company "Blake Technologies". As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing. Printer works with a rectangular sheet of paper of size *n*<=Γ—<=*m*. Consider the list as a table consisting of *n* rows and *m* columns. Rows are numbered from top to bottom with integers from 1 to *n*, while columns are numbered from left to right with integers from 1 to *m*. Initially, all cells are painted in color 0. Your program has to support two operations: 1. Paint all cells in row *r**i* in color *a**i*; 1. Paint all cells in column *c**i* in color *a**i*. If during some operation *i* there is a cell that have already been painted, the color of this cell also changes to *a**i*. Your program has to print the resulting table after *k* operation.
The first line of the input contains three integers *n*, *m* and *k* (1<=<=≀<=<=*n*,<=<=*m*<=<=≀<=5000, *n*Β·*m*<=≀<=100<=000, 1<=≀<=*k*<=≀<=100<=000)Β β€” the dimensions of the sheet and the number of operations, respectively. Each of the next *k* lines contains the description of exactly one query: - 1Β *r**i*Β *a**i* (1<=≀<=*r**i*<=≀<=*n*, 1<=≀<=*a**i*<=≀<=109), means that row *r**i* is painted in color *a**i*; - 2Β *c**i*Β *a**i* (1<=≀<=*c**i*<=≀<=*m*, 1<=≀<=*a**i*<=≀<=109), means that column *c**i* is painted in color *a**i*.
Print *n* lines containing *m* integers eachΒ β€” the resulting table after all operations are applied.
[ "3 3 3\n1 1 3\n2 2 1\n1 2 2\n", "5 3 5\n1 1 1\n1 3 1\n1 5 1\n2 1 1\n2 3 1\n" ]
[ "3 1 3 \n2 2 2 \n0 1 0 \n", "1 1 1 \n1 0 1 \n1 1 1 \n1 0 1 \n1 1 1 \n" ]
The figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray.
[ { "input": "3 3 3\n1 1 3\n2 2 1\n1 2 2", "output": "3 1 3 \n2 2 2 \n0 1 0 " }, { "input": "5 3 5\n1 1 1\n1 3 1\n1 5 1\n2 1 1\n2 3 1", "output": "1 1 1 \n1 0 1 \n1 1 1 \n1 0 1 \n1 1 1 " }, { "input": "5 5 4\n1 2 1\n1 4 1\n2 2 1\n2 4 1", "output": "0 1 0 1 0 \n1 1 1 1 1 \n0 1 0 1 0 \n1 1 1 1 1 \n0 1 0 1 0 " }, { "input": "4 6 8\n1 2 1\n2 2 2\n2 5 2\n1 1 1\n1 4 1\n1 3 2\n2 1 1\n2 6 1", "output": "1 1 1 1 1 1 \n1 2 1 1 2 1 \n1 2 2 2 2 1 \n1 1 1 1 1 1 " }, { "input": "2 2 3\n1 1 1\n1 2 1\n2 1 2", "output": "2 1 \n2 1 " }, { "input": "1 2 4\n1 1 1\n2 1 2\n2 2 3\n1 1 4", "output": "4 4 " }, { "input": "2 1 5\n1 1 7\n1 2 77\n2 1 777\n1 1 77\n1 2 7", "output": "77 \n7 " }, { "input": "2 1 1\n1 2 1000000000", "output": "0 \n1000000000 " }, { "input": "1 2 1\n2 2 1000000000", "output": "0 1000000000 " }, { "input": "160 600 1\n1 132 589472344", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..." }, { "input": "600 160 1\n1 124 542622711", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 \n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..." }, { "input": "10 1 1\n2 1 1000000000", "output": "1000000000 \n1000000000 \n1000000000 \n1000000000 \n1000000000 \n1000000000 \n1000000000 \n1000000000 \n1000000000 \n1000000000 " }, { "input": "1 10 1\n1 1 1000000000", "output": "1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 " }, { "input": "5000 20 15\n2 13 447246914\n2 10 89345638\n2 6 393683717\n2 1 62225152\n2 12 990340161\n2 4 227462932\n1 4011 327145900\n1 1915 981331082\n1 802 437883065\n2 11 205232924\n2 15 303578182\n1 2667 835309719\n2 3 550440583\n1 3852 766318960\n2 5 204700467", "output": "62225152 0 550440583 227462932 204700467 393683717 0 0 0 89345638 205232924 990340161 447246914 0 303578182 0 0 0 0 0 \n62225152 0 550440583 227462932 204700467 393683717 0 0 0 89345638 205232924 990340161 447246914 0 303578182 0 0 0 0 0 \n62225152 0 550440583 227462932 204700467 393683717 0 0 0 89345638 205232924 990340161 447246914 0 303578182 0 0 0 0 0 \n62225152 0 550440583 227462932 204700467 393683717 0 0 0 89345638 205232924 990340161 447246914 0 303578182 0 0 0 0 0 \n62225152 0 550440583 227462932 ..." }, { "input": "20 5000 15\n1 14 94104058\n1 3 170575572\n1 12 613859505\n2 4797 363375206\n1 10 554079405\n2 4481 223609505\n2 4974 385117513\n1 7 363246561\n2 2503 128936152\n1 2 152944073\n1 14 25797327\n1 20 258831369\n1 10 453164078\n2 2159 753235825\n2 1823 827844660", "output": "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0..." }, { "input": "1 1 1\n1 1 1", "output": "1 " }, { "input": "1 1 1\n1 1 1000000000", "output": "1000000000 " }, { "input": "1 1 2\n1 1 123\n1 1 321", "output": "321 " }, { "input": "1 1 2\n1 1 321\n1 1 123", "output": "123 " } ]
1,000
614,400
0
19,432
864
Fire
[ "dp", "sortings" ]
null
null
Polycarp is in really serious trouble β€” his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take *t**i* seconds to save *i*-th item. In addition, for each item, he estimated the value of *d**i* β€” the moment after which the item *i* will be completely burned and will no longer be valuable for him at all. In particular, if *t**i*<=β‰₯<=*d**i*, then *i*-th item cannot be saved. Given the values *p**i* for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item *a* first, and then item *b*, then the item *a* will be saved in *t**a* seconds, and the item *b* β€” in *t**a*<=+<=*t**b* seconds after fire started.
The first line contains a single integer *n* (1<=≀<=*n*<=≀<=100) β€” the number of items in Polycarp's house. Each of the following *n* lines contains three integers *t**i*,<=*d**i*,<=*p**i* (1<=≀<=*t**i*<=≀<=20, 1<=≀<=*d**i*<=≀<=2<=000, 1<=≀<=*p**i*<=≀<=20) β€” the time needed to save the item *i*, the time after which the item *i* will burn completely and the value of item *i*.
In the first line print the maximum possible total value of the set of saved items. In the second line print one integer *m* β€” the number of items in the desired set. In the third line print *m* distinct integers β€” numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them.
[ "3\n3 7 4\n2 6 5\n3 7 6\n", "2\n5 6 1\n3 3 5\n" ]
[ "11\n2\n2 3 \n", "1\n1\n1 \n" ]
In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6 + 5 = 11. In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time.
[ { "input": "3\n3 7 4\n2 6 5\n3 7 6", "output": "11\n2\n2 3 " }, { "input": "2\n5 6 1\n3 3 5", "output": "1\n1\n1 " }, { "input": "9\n13 18 14\n8 59 20\n9 51 2\n18 32 15\n1 70 18\n14 81 14\n10 88 16\n18 52 3\n1 50 6", "output": "106\n8\n1 4 9 8 2 5 6 7 " }, { "input": "5\n12 44 17\n10 12 11\n16 46 5\n17 55 5\n6 60 2", "output": "35\n4\n2 1 3 5 " }, { "input": "6\n18 85 3\n16 91 20\n12 92 11\n20 86 20\n15 43 4\n16 88 7", "output": "62\n5\n5 4 6 2 3 " }, { "input": "2\n12 13 2\n1 9 3", "output": "3\n1\n2 " }, { "input": "3\n14 42 16\n13 40 1\n17 33 5", "output": "21\n2\n3 1 " }, { "input": "4\n5 6 3\n17 22 14\n4 8 15\n13 2 18", "output": "29\n2\n3 2 " }, { "input": "5\n17 15 17\n10 14 3\n12 7 4\n20 21 13\n18 17 7", "output": "13\n1\n4 " }, { "input": "7\n14 23 4\n9 48 18\n14 29 2\n16 19 12\n6 49 18\n12 36 2\n7 26 8", "output": "56\n4\n4 7 2 5 " }, { "input": "10\n19 81 5\n7 109 8\n7 61 6\n16 74 16\n14 94 2\n3 118 20\n14 113 3\n8 70 6\n17 112 5\n15 111 6", "output": "75\n9\n3 8 4 1 2 10 9 7 6 " }, { "input": "12\n8 135 20\n14 120 14\n19 125 5\n5 137 19\n12 107 9\n20 136 12\n7 121 7\n3 93 2\n20 80 15\n20 114 11\n5 135 10\n4 122 6", "output": "128\n11\n9 5 10 2 7 12 3 11 1 6 4 " }, { "input": "20\n15 185 14\n19 197 20\n3 109 13\n1 206 20\n1 191 7\n7 202 6\n17 107 5\n2 105 11\n13 178 9\n2 209 6\n15 207 15\n12 200 5\n16 60 13\n19 125 19\n12 103 3\n4 88 13\n15 166 3\n18 154 12\n5 122 2\n15 116 4", "output": "198\n19\n13 16 15 8 7 3 20 14 18 17 9 1 5 2 12 6 4 11 10 " }, { "input": "30\n15 217 19\n3 129 4\n6 277 3\n10 253 11\n4 212 4\n11 184 17\n16 125 11\n16 211 1\n8 14 17\n13 225 2\n12 275 10\n6 101 19\n7 68 5\n15 226 19\n6 36 11\n5 243 13\n12 215 11\n14 230 5\n10 183 5\n8 149 10\n9 99 4\n19 122 11\n7 83 5\n11 169 18\n10 273 7\n2 36 11\n1 243 18\n12 187 2\n5 152 7\n6 200 17", "output": "296\n29\n9 26 15 13 23 21 12 22 7 2 20 29 24 19 6 28 30 5 17 1 10 14 18 27 16 4 25 11 3 " }, { "input": "40\n12 276 8\n7 312 8\n17 291 10\n14 266 2\n10 67 2\n11 133 4\n3 335 13\n10 69 6\n4 365 17\n11 367 9\n9 450 18\n8 169 16\n6 285 14\n11 22 14\n19 39 7\n1 113 14\n13 259 4\n13 123 7\n12 334 15\n18 32 12\n10 329 14\n20 19 13\n2 193 9\n19 300 12\n11 239 18\n20 347 13\n17 212 14\n10 23 18\n15 43 9\n3 197 16\n3 203 13\n17 249 20\n5 374 11\n9 140 3\n16 239 9\n15 78 14\n17 334 3\n20 446 3\n11 268 14\n6 305 18", "output": "410\n36\n14 28 29 5 8 36 16 18 6 34 12 23 30 31 27 25 35 32 17 39 1 13 3 24 40 2 21 19 37 7 26 9 10 33 38 11 " }, { "input": "50\n5 289 6\n16 126 16\n12 16 13\n12 71 8\n4 6 5\n9 57 4\n13 120 16\n3 54 17\n5 374 3\n7 345 9\n18 74 6\n10 17 6\n11 38 8\n19 40 2\n8 49 12\n20 6 1\n14 159 19\n6 90 10\n8 24 15\n12 2 18\n17 215 13\n8 91 19\n15 60 20\n14 247 14\n5 177 20\n11 90 9\n2 107 17\n11 8 10\n16 66 9\n15 2 7\n13 46 9\n12 139 4\n6 64 12\n11 39 4\n3 170 2\n15 178 13\n20 70 19\n10 162 7\n8 183 14\n12 8 16\n20 23 14\n1 33 7\n17 187 8\n3 45 8\n19 221 3\n19 132 6\n2 78 3\n8 75 9\n17 33 19\n19 31 12", "output": "326\n26\n19 42 49 44 15 8 23 33 48 47 18 22 27 7 2 17 38 25 36 39 43 21 24 1 10 9 " }, { "input": "1\n20 20 20", "output": "0\n0" }, { "input": "1\n20 21 20", "output": "20\n1\n1 " }, { "input": "2\n3 3 2\n3 4 1", "output": "1\n1\n2 " }, { "input": "2\n20 40 20\n20 41 20", "output": "40\n2\n1 2 " }, { "input": "2\n20 41 20\n20 40 20", "output": "40\n2\n2 1 " }, { "input": "2\n20 40 20\n20 40 20", "output": "20\n1\n2 " } ]
124
6,144,000
3
19,492
847
Sum of Nestings
[ "constructive algorithms" ]
null
null
Recall that the bracket sequence is considered regular if it is possible to insert symbols '+' and '1' into it so that the result is a correct arithmetic expression. For example, a sequence "(()())" is regular, because we can get correct arithmetic expression insering symbols '+' and '1': "((1+1)+(1+1))". Also the following sequences are regular: "()()()", "(())" and "()". The following sequences are not regular bracket sequences: ")(", "(()" and "())(()". In this problem you are given two integers *n* and *k*. Your task is to construct a regular bracket sequence consisting of round brackets with length 2Β·*n* with total sum of nesting of all opening brackets equals to exactly *k*. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded. For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1.
The first line contains two integers *n* and *k* (1<=≀<=*n*<=≀<=3Β·105, 0<=≀<=*k*<=≀<=1018)Β β€” the number of opening brackets and needed total nesting.
Print the required regular bracket sequence consisting of round brackets. If there is no solution print "Impossible" (without quotes).
[ "3 1\n", "4 6\n", "2 5\n" ]
[ "()(())", "(((())))", "Impossible\n" ]
The first example is examined in the statement. In the second example the answer is "(((())))". The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0 + 1 + 2 + 3 = 6. In the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())".
[ { "input": "3 1", "output": "()(())" }, { "input": "4 6", "output": "(((())))" }, { "input": "2 5", "output": "Impossible" }, { "input": "1 0", "output": "()" }, { "input": "2 0", "output": "()()" }, { "input": "2 1", "output": "(())" }, { "input": "3 0", "output": "()()()" }, { "input": "10 42", "output": "(((((((()()())))))))" }, { "input": "3 2", "output": "(()())" }, { "input": "3 3", "output": "((()))" }, { "input": "4 1", "output": "()()(())" }, { "input": "4 2", "output": "()(()())" }, { "input": "4 0", "output": "()()()()" }, { "input": "4 3", "output": "(()()())" }, { "input": "4 4", "output": "(()(()))" }, { "input": "4 5", "output": "((()()))" }, { "input": "20 132", "output": "((((((()(((()()()()()()()()()())))))))))" }, { "input": "5 0", "output": "()()()()()" }, { "input": "5 1", "output": "()()()(())" }, { "input": "5 2", "output": "()()(()())" }, { "input": "5 3", "output": "()(()()())" }, { "input": "5 4", "output": "(()()()())" }, { "input": "5 5", "output": "()((()()))" }, { "input": "5 6", "output": "(()(()()))" }, { "input": "5 7", "output": "((()()()))" }, { "input": "5 8", "output": "((()(())))" }, { "input": "5 9", "output": "(((()())))" }, { "input": "5 10", "output": "((((()))))" }, { "input": "5 11", "output": "Impossible" }, { "input": "50 282", "output": "()()()()()((()(((((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()())))))))" }, { "input": "100 4298", "output": "((((((((((((((((((((((((((((((((((((((((((()((((((((((((((((((((((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))" }, { "input": "201 19557", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()(((((((((((((((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))" }, { "input": "301 43259", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))..." }, { "input": "333 3286", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()((((((((()(((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()..." }, { "input": "400 14432", "output": "(((((((((((()(((((((((((((((((((((((((((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "500 12169", "output": "(((((((((((((((((((()((((((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()..." }, { "input": "1000 101063", "output": "()(((((((((((((((((((((((((((((((((((((((((((((((((((((((()((((((((((((((((((((((((((((((((((((((((((((((((((((()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()..." }, { "input": "5001 12502500", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "9000 0", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "9999 41526212", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "299999 2887574325", "output": "()()()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "299999 24023579789", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "299999 26952312018", "output": "()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "299999 8515952136", "output": "()()()()()()()()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "299999 35062652872", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "299999 21396945540", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "299999 43798620158", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 0", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "300000 0", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "300000 4241009937", "output": "()()()()()()()()()()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 6782741206", "output": "()()()()()()()()()()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 1446225055", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 12099664325", "output": "()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 18442545357", "output": "()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 41656958056", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 42589173119", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 39226420886", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 40158635949", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 37728098779", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 43643072541", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 44754406973", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 44999849986", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "10 48", "output": "Impossible" }, { "input": "100 4952", "output": "Impossible" }, { "input": "1000 499505", "output": "Impossible" }, { "input": "5001 12502504", "output": "Impossible" }, { "input": "9999 49985003", "output": "Impossible" }, { "input": "300000 44999850003", "output": "Impossible" }, { "input": "10 46", "output": "Impossible" }, { "input": "100 4955", "output": "Impossible" }, { "input": "1000 499503", "output": "Impossible" }, { "input": "5001 12502503", "output": "Impossible" }, { "input": "9999 49985002", "output": "Impossible" }, { "input": "300000 44999850002", "output": "Impossible" }, { "input": "300000 1000000000000000000", "output": "Impossible" }, { "input": "300000 44999850000", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 44999850001", "output": "Impossible" }, { "input": "300000 44999849999", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 44999849998", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 44999849997", "output": "(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "300000 0", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "300000 1", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "300000 2", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "300000 3", "output": "()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()(..." }, { "input": "1 1", "output": "Impossible" }, { "input": "1 2", "output": "Impossible" }, { "input": "1 1000000000000000000", "output": "Impossible" }, { "input": "2 2", "output": "Impossible" }, { "input": "2 3", "output": "Impossible" }, { "input": "300000 14999849999", "output": "()()(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((..." }, { "input": "2 1000000000000000000", "output": "Impossible" }, { "input": "299999 1000000000000000000", "output": "Impossible" } ]
62
5,529,600
0
19,495
226
Noble Knight's Path
[ "data structures", "trees" ]
null
null
In Berland each feudal owns exactly one castle and each castle belongs to exactly one feudal. Each feudal, except one (the King) is subordinate to another feudal. A feudal can have any number of vassals (subordinates). Some castles are connected by roads, it is allowed to move along the roads in both ways. Two castles have a road between them if and only if the owner of one of these castles is a direct subordinate to the other owner. Each year exactly one of these two events may happen in Berland. 1. The barbarians attacked castle *c*. The interesting fact is, the barbarians never attacked the same castle twice throughout the whole Berlandian history. 1. A noble knight sets off on a journey from castle *a* to castle *b* (provided that on his path he encounters each castle not more than once). Let's consider the second event in detail. As the journey from *a* to *b* is not short, then the knight might want to stop at a castle he encounters on his way to have some rest. However, he can't stop at just any castle: his nobility doesn't let him stay in the castle that has been desecrated by the enemy's stench. A castle is desecrated if and only if it has been attacked after the year of *y*. So, the knight chooses the *k*-th castle he encounters, starting from *a* (castles *a* and *b* aren't taken into consideration), that hasn't been attacked in years from *y*<=+<=1 till current year. The knights don't remember which castles were attacked on what years, so he asked the court scholar, aka you to help them. You've got a sequence of events in the Berland history. Tell each knight, in what city he should stop or else deliver the sad news β€” that the path from city *a* to city *b* has less than *k* cities that meet his requirements, so the knight won't be able to rest.
The first input line contains integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of feudals. The next line contains *n* space-separated integers: the *i*-th integer shows either the number of the *i*-th feudal's master, or a 0, if the *i*-th feudal is the King. The third line contains integer *m* (1<=≀<=*m*<=≀<=105) β€” the number of queries. Then follow *m* lines that describe the events. The *i*-th line (the lines are indexed starting from 1) contains the description of the event that occurred in year *i*. Each event is characterised by type *t**i* (1<=≀<=*t**i*<=≀<=2). The description of the first type event looks as two space-separated integers *t**i* *c**i* (*t**i*<==<=1;Β 1<=≀<=*c**i*<=≀<=*n*), where *c**i* is the number of the castle that was attacked by the barbarians in the *i*-th year. The description of the second type contains five space-separated integers: *t**i* *a**i* *b**i* *k**i* *y**i* (*t**i*<==<=2;Β 1<=≀<=*a**i*,<=*b**i*,<=*k**i*<=≀<=*n*;Β *a**i*<=β‰ <=*b**i*;Β 0<=≀<=*y**i*<=&lt;<=*i*), where *a**i* is the number of the castle from which the knight is setting off, *b**i* is the number of the castle to which the knight is going, *k**i* and *y**i* are the *k* and *y* from the second event's description. You can consider the feudals indexed from 1 to *n*. It is guaranteed that there is only one king among the feudals. It is guaranteed that for the first type events all values *c**i* are different.
For each second type event print an integer β€” the number of the castle where the knight must stay to rest, or -1, if he will have to cover the distance from *a**i* to *b**i* without a rest. Separate the answers by whitespaces. Print the answers in the order, in which the second type events are given in the input.
[ "3\n0 1 2\n5\n2 1 3 1 0\n1 2\n2 1 3 1 0\n2 1 3 1 1\n2 1 3 1 2\n", "6\n2 5 2 2 0 5\n3\n2 1 6 2 0\n1 2\n2 4 5 1 0\n" ]
[ "2\n-1\n-1\n2\n", "5\n-1\n" ]
In the first sample there is only castle 2 on the knight's way from castle 1 to castle 3. When the knight covers the path 1 - 3 for the first time, castle 2 won't be desecrated by an enemy and the knight will stay there. In the second year the castle 2 will become desecrated, so the knight won't have anywhere to stay for the next two years (as finding a castle that hasn't been desecrated from years 1 and 2, correspondingly, is important for him). In the fifth year the knight won't consider the castle 2 desecrated, so he will stay there again.
[]
124
0
0
19,572
600
Area of Two Circles' Intersection
[ "geometry" ]
null
null
You are given two circles. Find the area of their intersection.
The first line contains three integers *x*1,<=*y*1,<=*r*1 (<=-<=109<=≀<=*x*1,<=*y*1<=≀<=109,<=1<=≀<=*r*1<=≀<=109) β€” the position of the center and the radius of the first circle. The second line contains three integers *x*2,<=*y*2,<=*r*2 (<=-<=109<=≀<=*x*2,<=*y*2<=≀<=109,<=1<=≀<=*r*2<=≀<=109) β€” the position of the center and the radius of the second circle.
Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6.
[ "0 0 4\n6 0 4\n", "0 0 5\n11 0 5\n" ]
[ "7.25298806364175601379\n", "0.00000000000000000000\n" ]
none
[ { "input": "0 0 4\n6 0 4", "output": "7.25298806364175601379" }, { "input": "0 0 5\n11 0 5", "output": "0.00000000000000000000" }, { "input": "0 0 10\n9 0 1", "output": "3.14159265358979311600" }, { "input": "0 0 2\n2 2 2", "output": "2.28318530717958647659" }, { "input": "0 0 10\n5 0 5", "output": "78.53981633974482789995" }, { "input": "-9 8 7\n-9 8 5", "output": "78.53981633974482789995" }, { "input": "-60 -85 95\n-69 -94 95", "output": "25936.37843115316246844770" }, { "input": "159 111 998\n161 121 1023", "output": "3129038.84934604830277748988" }, { "input": "6008 8591 6693\n5310 8351 7192", "output": "138921450.46886559338599909097" }, { "input": "-13563 -6901 22958\n-19316 -16534 18514", "output": "868466038.83295116270892322063" }, { "input": "-875463 79216 524620\n-891344 76571 536598", "output": "862534134678.47474157810211181641" }, { "input": "-8907963 -8149654 8808560\n-8893489 -8125053 8830600", "output": "243706233220003.66226196289062500000" }, { "input": "-56452806 56199829 45467742\n-56397667 56292048 45489064", "output": "6487743741270471.46582031250000000000" }, { "input": "-11786939 388749051 844435993\n-11696460 388789113 844535886", "output": "2240182216213578196.25000000000000000000" }, { "input": "-944341103 -3062765 891990581\n-943884414 -3338765 891882754", "output": "2498325849744150942.00000000000000000000" }, { "input": "808468733 166975547 650132512\n807140196 169714842 655993403", "output": "1327864139649690571.00000000000000000000" }, { "input": "-16 -107 146\n75 25 19", "output": "75.73941676175987183783" }, { "input": "468534418 -876402362 779510\n392125478 -856995174 1", "output": "0.00000000000000000000" }, { "input": "368831644 125127030 959524552\n690900461 -368007601 1000000000", "output": "1877639096067727828.75000000000000000000" }, { "input": "638572730 86093565 553198855\n-151099010 -5582761 1000000000", "output": "648156847022339121.87500000000000000000" }, { "input": "567845488 379750385 112902105\n567845488 379750385 112902105", "output": "40045521256826535.57031250000000000000" }, { "input": "817163584 -145230792 164258581\n826720200 -149804696 98", "output": "30171.85584507637308604444" }, { "input": "-812130546 -209199732 799576707\n-728169661 -278950375 4385", "output": "60407250.40157159973750822246" }, { "input": "-36140638 -933845433 250828868\n90789911 -245130908 328547", "output": "0.00000000000000000000" }, { "input": "34537868 -531411810 591044372\n34536968 -531411968 58", "output": "10568.31768667606404221715" }, { "input": "-410889750 -716765873 303980004\n-410889749 -716765874 7", "output": "153.93804002589986268390" }, { "input": "-304 -310 476\n120 -294 1", "output": "3.14159265358979311600" }, { "input": "-999999999 0 1000000000\n999999999 0 1000000000", "output": "119256.95877838134765625000" }, { "input": "-1000000000 0 1000000000\n999999999 0 1000000000", "output": "42163.70213317871093750000" }, { "input": "-99999999 0 100000000\n99999999 0 100000000", "output": "37712.36160683631896972656" }, { "input": "-999999999 0 1000000000\n999999999 1 1000000000", "output": "119256.95874786376953125000" }, { "input": "-1000000000 0 999999999\n999999997 0 999999999", "output": "42163.70211410522460937500" }, { "input": "0 1000000000 1\n0 0 1000000000", "output": "1.57079632649338855020" }, { "input": "10000000 0 10000001\n-10000000 0 10000000", "output": "4216.37028734199702739716" }, { "input": "1000000000 0 1000000000\n-999999999 1 1000000000", "output": "42163.70212173461914062500" }, { "input": "44721 999999999 400000000\n0 0 600000000", "output": "0.00188343226909637451" }, { "input": "-1000000000 1 1000000000\n999999998 0 1000000000", "output": "119256.95874786376953125000" }, { "input": "0 0 500000000\n431276 999999907 500000000", "output": "0.33492207527160644531" }, { "input": "1000000000 0 1000000000\n-999999998 -87334 1000000000", "output": "1199.53919601440429687500" }, { "input": "0 0 10\n0 0 25", "output": "314.15926535897931159980" }, { "input": "0 0 1000000000\n707106781 707106781 1", "output": "2.09224628662147114737" }, { "input": "100 10 10\n100 20 10", "output": "122.83696986087568455565" }, { "input": "1000000000 0 1000000000\n-999999998 -88334 1000000000", "output": "461.20431423187255859375" }, { "input": "0 0 999999999\n1000000000 0 2", "output": "2.45673939563023624650" }, { "input": "-99999999 0 100000000\n99999999 1 100000000", "output": "37712.36153602600097656250" }, { "input": "1000000000 0 1000000000\n-999999999 60333 1000000000", "output": "1138.08371162414550781250" }, { "input": "1000000000 0 1000000000\n-999999999 58333 1000000000", "output": "2432.73669052124023437500" }, { "input": "1000000000 0 1000000000\n-999999998 -85334 1000000000", "output": "3207.25725555419921875000" }, { "input": "0 0 1000000000\n999999999 1 2", "output": "10.10963121370591567653" }, { "input": "0 0 1000000000\n999999998 0 3", "output": "25.17685179846658691770" }, { "input": "141 9999 5000\n0 0 5000", "output": "0.04272695172407026121" }, { "input": "-1000000000 0 1000000000\n999999998 0 1000000000", "output": "119256.95877838134765625000" }, { "input": "0 0 10\n1 0 10", "output": "294.16760182010623145277" }, { "input": "0 0 1000000000\n707106782 707106781 2", "output": "4.52465731000908907454" } ]
77
3,481,600
-1
19,604
15
Triangles
[ "combinatorics", "dp" ]
E. Triangles
1
64
Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has *n* levels, where *n* is an even number. In the local council you were given an area map, where the granny's house is marked by point *H*, parts of dense forest are marked grey (see the picture to understand better). After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics: - it starts and ends in the same place β€” the granny's house; - the route goes along the forest paths only (these are the segments marked black in the picture); - the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); - the route cannot cross itself; - there shouldn't be any part of dense forest within the part marked out by this route; You should find the amount of such suitable oriented routes modulo 1000000009. The example of the area map for *n*<==<=12 is given in the picture. Since the map has a regular structure, you can construct it for other *n* by analogy using the example.
The input data contain the only even integer *n* (2<=≀<=*n*<=≀<=106).
Output the only number β€” the amount of Peter's routes modulo 1000000009.
[ "2\n", "4\n" ]
[ "10\n", "74\n" ]
none
[ { "input": "2", "output": "10" }, { "input": "4", "output": "74" }, { "input": "6", "output": "1354" }, { "input": "8", "output": "163594" }, { "input": "10", "output": "122492554" }, { "input": "966", "output": "154440215" }, { "input": "9158", "output": "481655864" }, { "input": "95576", "output": "52134285" }, { "input": "804974", "output": "830331886" }, { "input": "635758", "output": "524558133" }, { "input": "982894", "output": "610514474" }, { "input": "813678", "output": "195839973" }, { "input": "524288", "output": "858669864" }, { "input": "908550", "output": "307673807" }, { "input": "988794", "output": "57571447" }, { "input": "939636", "output": "402023314" }, { "input": "956022", "output": "864671299" }, { "input": "972408", "output": "377259665" }, { "input": "988794", "output": "57571447" }, { "input": "905180", "output": "247958166" }, { "input": "921566", "output": "838186871" }, { "input": "975778", "output": "995284797" }, { "input": "992164", "output": "184482083" }, { "input": "908550", "output": "307673807" }, { "input": "924936", "output": "867859567" }, { "input": "939636", "output": "402023314" }, { "input": "992164", "output": "184482083" }, { "input": "906864", "output": "748483990" }, { "input": "921566", "output": "838186871" }, { "input": "974094", "output": "9604500" }, { "input": "38", "output": "913760132" }, { "input": "94", "output": "45354328" }, { "input": "48", "output": "980993190" }, { "input": "54", "output": "111356740" }, { "input": "8", "output": "163594" }, { "input": "14", "output": "809112458" }, { "input": "68", "output": "940618832" }, { "input": "24", "output": "697629445" }, { "input": "30", "output": "698593821" }, { "input": "84", "output": "222712293" }, { "input": "988794", "output": "57571447" }, { "input": "941322", "output": "359985814" }, { "input": "956022", "output": "864671299" }, { "input": "908550", "output": "307673807" }, { "input": "923250", "output": "463682665" }, { "input": "939636", "output": "402023314" }, { "input": "999994", "output": "381170049" }, { "input": "999996", "output": "629260868" }, { "input": "999998", "output": "336683304" }, { "input": "1000000", "output": "958220352" }, { "input": "140", "output": "256406051" }, { "input": "168", "output": "816814227" }, { "input": "160", "output": "176472603" }, { "input": "188", "output": "249468441" }, { "input": "180", "output": "320858917" }, { "input": "448", "output": "511315133" }, { "input": "410", "output": "485353876" }, { "input": "372", "output": "993326044" }, { "input": "726", "output": "827884602" }, { "input": "716", "output": "687598196" } ]
92
0
0
19,608
724
Ray Tracing
[ "greedy", "hashing", "implementation", "math", "number theory", "sortings" ]
null
null
There are *k* sensors located in the rectangular room of size *n*<=Γ—<=*m* meters. The *i*-th sensor is located at point (*x**i*,<=*y**i*). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0,<=0) and (*n*,<=*m*). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0,<=0) the laser ray is released in the direction of point (1,<=1). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1,<=1) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print <=-<=1 for such sensors.
The first line of the input contains three integers *n*, *m* and *k* (2<=≀<=*n*,<=*m*<=≀<=100<=000, 1<=≀<=*k*<=≀<=100<=000)Β β€” lengths of the room's walls and the number of sensors. Each of the following *k* lines contains two integers *x**i* and *y**i* (1<=≀<=*x**i*<=≀<=*n*<=-<=1, 1<=≀<=*y**i*<=≀<=*m*<=-<=1)Β β€” coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
Print *k* integers. The *i*-th of them should be equal to the number of seconds when the ray first passes through the point where the *i*-th sensor is located, or <=-<=1 if this will never happen.
[ "3 3 4\n1 1\n1 2\n2 1\n2 2\n", "3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3\n", "7 4 5\n1 3\n2 2\n5 1\n5 3\n4 3\n" ]
[ "1\n-1\n-1\n2\n", "1\n-1\n-1\n2\n5\n-1\n", "13\n2\n9\n5\n-1\n" ]
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
[ { "input": "3 3 4\n1 1\n1 2\n2 1\n2 2", "output": "1\n-1\n-1\n2" }, { "input": "3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3", "output": "1\n-1\n-1\n2\n5\n-1" }, { "input": "7 4 5\n1 3\n2 2\n5 1\n5 3\n4 3", "output": "13\n2\n9\n5\n-1" }, { "input": "10 10 10\n3 8\n1 7\n2 3\n4 2\n4 8\n3 3\n2 8\n5 5\n6 3\n3 1", "output": "-1\n-1\n-1\n-1\n-1\n3\n-1\n5\n-1\n-1" } ]
2,000
4,710,400
0
19,647
9
Interestring graph and Apples
[ "dfs and similar", "dsu", "graphs" ]
E. Interesting Graph and Apples
1
64
Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life Β«interesting graph and applesΒ». An undirected graph is called interesting, if each of its vertices belongs to one cycle only β€” a funny ring β€” and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too. She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (*x*1,<=*y*1),<=(*x*2,<=*y*2),<=...,<=(*x**n*,<=*y**n*), where *x**i*<=≀<=*y**i*, is lexicographically smaller than the set (*u*1,<=*v*1),<=(*u*2,<=*v*2),<=...,<=(*u**n*,<=*v**n*), where *u**i*<=≀<=*v**i*, provided that the sequence of integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**n*,<=*y**n* is lexicographically smaller than the sequence *u*1,<=*v*1,<=*u*2,<=*v*2,<=...,<=*u**n*,<=*v**n*. If you do not cope, Hexadecimal will eat you. ...eat you alive.
The first line of the input data contains a pair of integers *n* and *m* (1<=≀<=*n*<=≀<=50, 0<=≀<=*m*<=≀<=2500) β€” the amount of vertices and edges respectively. The following lines contain pairs of numbers *x**i* and *y**i* (1<=≀<=*x**i*, *y**i*<=≀<=*n*) β€” the vertices that are already connected by edges. The initial graph may contain multiple edges and loops.
In the first line output Β«YESΒ» or Β«NOΒ»: if it is possible or not to construct an interesting graph. If the answer is Β«YESΒ», in the second line output *k* β€” the amount of edges that should be added to the initial graph. Finally, output *k* lines: pairs of vertices *x**j* and *y**j*, between which edges should be drawn. The result may contain multiple edges and loops. *k* can be equal to zero.
[ "3 2\n1 2\n2 3\n" ]
[ "YES\n1\n1 3\n" ]
none
[ { "input": "3 2\n1 2\n2 3", "output": "YES\n1\n1 3" }, { "input": "1 1\n1 1", "output": "YES\n0" }, { "input": "1 2\n1 1\n1 1", "output": "NO" }, { "input": "1 3\n1 1\n1 1\n1 1", "output": "NO" }, { "input": "2 0", "output": "YES\n2\n1 2\n1 2" }, { "input": "2 1\n1 1", "output": "NO" }, { "input": "2 1\n2 2", "output": "NO" }, { "input": "2 1\n2 1", "output": "YES\n1\n1 2" }, { "input": "2 2\n1 1\n2 2", "output": "NO" }, { "input": "2 2\n1 2\n2 1", "output": "YES\n0" }, { "input": "2 3\n1 1\n1 2\n2 1", "output": "NO" }, { "input": "3 2\n1 2\n1 2", "output": "NO" }, { "input": "1 0", "output": "YES\n1\n1 1" }, { "input": "4 3\n1 2\n1 3\n1 4", "output": "NO" }, { "input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 1", "output": "NO" }, { "input": "49 0", "output": "YES\n49\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 49" }, { "input": "50 0", "output": "YES\n50\n1 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 50" }, { "input": "50 1\n2 3", "output": "YES\n49\n1 2\n1 4\n3 5\n4 6\n5 7\n6 8\n7 9\n8 10\n9 11\n10 12\n11 13\n12 14\n13 15\n14 16\n15 17\n16 18\n17 19\n18 20\n19 21\n20 22\n21 23\n22 24\n23 25\n24 26\n25 27\n26 28\n27 29\n28 30\n29 31\n30 32\n31 33\n32 34\n33 35\n34 36\n35 37\n36 38\n37 39\n38 40\n39 41\n40 42\n41 43\n42 44\n43 45\n44 46\n45 47\n46 48\n47 49\n48 50\n49 50" }, { "input": "3 2\n1 2\n2 3", "output": "YES\n1\n1 3" }, { "input": "5 3\n3 5\n4 2\n5 1", "output": "YES\n2\n1 2\n3 4" }, { "input": "6 6\n4 3\n3 5\n6 4\n1 6\n2 1\n5 2", "output": "YES\n0" }, { "input": "7 4\n3 2\n2 6\n6 7\n1 5", "output": "YES\n3\n1 3\n4 5\n4 7" }, { "input": "8 5\n4 7\n3 6\n8 3\n6 5\n1 2", "output": "YES\n3\n1 4\n2 5\n7 8" }, { "input": "9 5\n5 2\n4 6\n8 4\n1 8\n2 1", "output": "YES\n4\n3 5\n3 7\n6 9\n7 9" }, { "input": "3 3\n1 3\n2 1\n3 2", "output": "YES\n0" }, { "input": "4 3\n1 2\n4 1\n2 3", "output": "YES\n1\n3 4" }, { "input": "5 2\n1 5\n5 4", "output": "YES\n3\n1 2\n2 3\n3 4" }, { "input": "6 3\n3 4\n1 3\n2 5", "output": "YES\n3\n1 2\n4 6\n5 6" }, { "input": "7 6\n5 6\n2 7\n7 3\n4 1\n1 5\n3 4", "output": "YES\n1\n2 6" }, { "input": "4 1\n3 1", "output": "YES\n3\n1 2\n2 4\n3 4" }, { "input": "8 3\n3 8\n2 6\n1 7", "output": "YES\n5\n1 2\n3 4\n4 5\n5 6\n7 8" }, { "input": "9 4\n7 6\n2 8\n3 5\n8 3", "output": "YES\n5\n1 2\n1 4\n4 6\n5 9\n7 9" }, { "input": "45 22\n15 23\n14 30\n5 44\n43 21\n24 17\n37 38\n40 9\n41 43\n7 4\n38 22\n26 18\n44 41\n42 11\n4 33\n35 24\n36 15\n19 1\n1 37\n9 35\n12 40\n31 29\n18 25", "output": "YES\n23\n2 3\n2 5\n3 6\n6 7\n8 10\n8 11\n10 12\n13 14\n13 16\n16 17\n19 20\n20 21\n22 23\n25 27\n26 28\n27 29\n28 30\n31 32\n32 33\n34 36\n34 39\n39 45\n42 45" }, { "input": "46 25\n44 40\n25 10\n28 44\n26 4\n38 7\n27 3\n46 8\n32 28\n22 20\n14 33\n30 14\n12 23\n13 30\n40 18\n37 35\n10 16\n23 22\n3 46\n36 24\n19 12\n18 42\n11 34\n34 36\n9 32\n24 19", "output": "YES\n21\n1 2\n1 4\n2 5\n5 6\n6 7\n8 9\n11 13\n15 16\n15 17\n17 20\n21 25\n21 26\n27 29\n29 31\n31 33\n35 38\n37 39\n39 41\n41 43\n42 45\n43 45" }, { "input": "47 26\n24 2\n13 24\n25 14\n35 6\n4 10\n11 18\n29 41\n37 13\n38 3\n2 31\n30 29\n6 42\n33 25\n41 45\n40 8\n28 47\n43 39\n39 38\n1 5\n45 22\n19 21\n18 37\n36 17\n27 28\n16 11\n12 30", "output": "YES\n21\n1 3\n4 5\n7 8\n7 9\n9 10\n12 14\n15 16\n15 17\n19 20\n20 22\n21 23\n23 26\n26 27\n31 32\n32 33\n34 35\n34 36\n40 42\n43 44\n44 46\n46 47" }, { "input": "48 26\n27 5\n13 21\n14 20\n41 31\n4 26\n21 39\n31 17\n18 4\n42 2\n28 43\n11 23\n35 22\n34 18\n23 15\n10 13\n7 48\n5 44\n19 25\n12 7\n15 27\n39 41\n33 10\n45 40\n20 42\n29 38\n17 28", "output": "YES\n22\n1 2\n1 3\n3 6\n6 8\n8 9\n9 11\n12 14\n16 19\n16 22\n24 25\n24 26\n29 30\n30 32\n32 33\n34 36\n35 37\n36 38\n37 40\n43 44\n45 46\n46 47\n47 48" }, { "input": "49 26\n33 34\n43 21\n26 27\n46 33\n32 47\n6 3\n44 14\n34 42\n4 8\n27 29\n12 4\n42 7\n22 16\n5 31\n35 24\n39 40\n20 12\n17 44\n8 18\n38 26\n48 39\n31 17\n9 19\n10 23\n1 30\n49 38", "output": "YES\n23\n1 2\n2 3\n5 6\n7 9\n10 11\n11 13\n13 14\n15 16\n15 18\n19 20\n21 22\n23 24\n25 28\n25 29\n28 30\n32 35\n36 37\n36 40\n37 41\n41 43\n45 46\n45 47\n48 49" }, { "input": "50 21\n27 16\n42 35\n15 28\n46 17\n30 39\n47 18\n35 25\n26 24\n24 30\n28 41\n40 38\n11 21\n33 20\n43 10\n37 14\n1 43\n32 49\n49 6\n10 45\n21 50\n39 3", "output": "YES\n29\n1 2\n2 3\n4 5\n4 6\n5 7\n7 8\n8 9\n9 11\n12 13\n12 14\n13 15\n16 17\n18 19\n19 20\n22 23\n22 25\n23 26\n27 29\n29 31\n31 32\n33 34\n34 36\n36 37\n38 41\n40 42\n44 45\n44 46\n47 48\n48 50" }, { "input": "30 21\n6 14\n19 17\n25 20\n28 10\n10 3\n24 23\n22 13\n1 7\n11 26\n12 1\n16 8\n14 9\n30 15\n4 27\n13 21\n20 12\n24 14\n19 10\n7 10\n16 8\n26 11", "output": "NO" }, { "input": "31 24\n6 25\n8 13\n29 20\n13 5\n26 8\n16 9\n31 2\n22 7\n24 21\n28 18\n9 12\n27 14\n20 24\n23 10\n10 27\n15 1\n21 28\n11 16\n12 29\n8 7\n10 28\n27 19\n17 3\n23 16", "output": "NO" }, { "input": "5 2\n1 3\n4 1", "output": "YES\n3\n2 3\n2 5\n4 5" }, { "input": "32 24\n9 15\n32 16\n26 7\n15 8\n30 21\n23 14\n22 17\n14 29\n19 1\n24 31\n3 22\n20 9\n5 23\n10 3\n27 24\n1 30\n8 18\n23 28\n14 4\n27 10\n11 9\n11 24\n11 18\n17 6", "output": "NO" }, { "input": "33 19\n27 23\n17 16\n20 33\n3 11\n1 31\n26 24\n25 10\n21 15\n14 9\n12 4\n29 2\n7 21\n32 13\n33 6\n5 26\n13 28\n6 22\n3 24\n27 19", "output": "YES\n14\n1 2\n4 5\n7 8\n8 9\n10 11\n12 14\n15 16\n17 18\n18 19\n20 23\n22 28\n25 29\n30 31\n30 32" }, { "input": "34 18\n9 14\n30 23\n19 3\n34 19\n26 2\n31 28\n7 21\n20 27\n16 15\n18 20\n5 34\n17 22\n10 12\n6 4\n8 32\n29 24\n24 10\n34 22", "output": "NO" }, { "input": "35 28\n6 24\n35 10\n14 19\n30 34\n29 23\n21 16\n34 5\n22 6\n7 35\n13 29\n27 3\n8 27\n5 15\n26 11\n19 1\n31 28\n17 31\n18 20\n12 32\n4 17\n10 4\n32 8\n35 18\n9 5\n33 30\n24 25\n12 12\n34 3", "output": "NO" }, { "input": "36 23\n27 31\n33 14\n17 24\n14 25\n3 8\n1 21\n24 27\n13 26\n23 6\n35 22\n34 33\n36 4\n19 16\n18 15\n32 36\n5 7\n20 30\n21 11\n11 27\n8 23\n6 10\n4 31\n15 31", "output": "NO" }, { "input": "37 22\n2 15\n37 11\n14 29\n9 37\n15 23\n24 35\n18 3\n23 12\n34 33\n4 19\n22 14\n21 26\n28 27\n12 36\n8 6\n26 28\n31 1\n29 5\n27 25\n17 10\n33 18\n35 20", "output": "YES\n15\n1 2\n3 4\n5 6\n7 8\n7 9\n10 11\n13 16\n13 17\n16 19\n20 21\n22 24\n25 30\n30 31\n32 34\n32 36" }, { "input": "38 30\n21 36\n20 21\n9 11\n27 10\n25 20\n33 16\n11 23\n31 4\n13 22\n36 27\n32 37\n12 6\n35 31\n5 34\n6 14\n7 38\n26 18\n4 24\n18 5\n23 17\n29 28\n38 13\n10 30\n18 3\n15 25\n1 24\n22 22\n17 22\n36 18\n23 13", "output": "NO" }, { "input": "39 25\n8 23\n27 38\n6 32\n20 33\n7 34\n22 26\n32 12\n23 2\n28 20\n33 35\n18 10\n1 21\n11 18\n39 28\n17 9\n36 8\n15 17\n14 1\n19 24\n37 30\n21 39\n38 13\n28 5\n36 30\n33 13", "output": "NO" }, { "input": "40 29\n23 2\n40 16\n35 31\n2 40\n39 35\n18 11\n21 7\n3 6\n15 5\n4 18\n17 19\n8 34\n16 17\n9 39\n37 21\n19 26\n26 36\n33 4\n10 9\n34 22\n13 20\n32 40\n35 11\n5 12\n14 5\n5 24\n40 6\n32 35\n21 21", "output": "NO" }, { "input": "41 28\n6 28\n1 38\n11 7\n12 26\n10 36\n9 21\n8 3\n2 20\n33 32\n21 40\n34 10\n22 15\n30 22\n5 12\n19 35\n13 6\n31 37\n25 4\n15 23\n37 33\n19 19\n20 6\n14 8\n9 12\n27 33\n28 27\n37 11\n36 20", "output": "NO" }, { "input": "6 1\n4 1", "output": "YES\n5\n1 2\n2 3\n3 5\n4 6\n5 6" }, { "input": "42 28\n7 19\n15 24\n3 42\n18 5\n32 27\n26 20\n40 30\n35 2\n14 8\n22 10\n36 4\n16 14\n21 29\n37 40\n2 12\n30 21\n19 17\n39 34\n31 28\n20 3\n4 33\n11 42\n26 21\n9 10\n4 32\n6 1\n1 14\n14 12", "output": "NO" }, { "input": "43 36\n3 24\n25 36\n36 11\n12 38\n11 32\n15 3\n8 9\n2 17\n5 40\n21 37\n39 20\n28 30\n16 22\n27 13\n31 6\n24 39\n34 19\n35 18\n43 21\n41 4\n7 31\n33 26\n6 5\n42 27\n29 2\n30 10\n40 1\n1 29\n20 14\n40 29\n29 6\n26 27\n37 21\n19 9\n31 4\n19 38", "output": "NO" }, { "input": "44 31\n28 26\n5 36\n9 37\n36 29\n26 5\n25 42\n30 22\n29 3\n35 10\n44 28\n18 13\n16 6\n3 33\n22 9\n4 15\n27 19\n17 11\n19 41\n11 25\n10 30\n2 34\n12 7\n37 31\n16 40\n25 24\n28 44\n41 37\n21 21\n12 28\n20 23\n20 17", "output": "NO" }, { "input": "45 20\n37 5\n41 6\n13 22\n28 24\n30 10\n39 35\n5 20\n38 32\n26 1\n23 37\n35 17\n21 12\n7 8\n1 7\n4 16\n8 40\n44 3\n27 23\n19 2\n33 27", "output": "YES\n25\n2 3\n4 6\n9 10\n9 11\n11 12\n13 14\n14 15\n15 16\n17 18\n18 19\n20 21\n22 24\n25 26\n25 28\n29 30\n29 31\n31 32\n33 34\n34 36\n36 39\n38 40\n41 42\n42 43\n43 45\n44 45" }, { "input": "46 24\n24 43\n38 20\n8 38\n22 13\n25 24\n40 35\n21 10\n7 39\n18 5\n33 19\n26 7\n1 27\n43 26\n9 17\n3 44\n44 14\n20 11\n5 2\n15 32\n23 8\n10 37\n27 23\n43 23\n33 25", "output": "NO" }, { "input": "47 36\n29 31\n25 45\n39 46\n12 19\n31 21\n4 41\n5 38\n33 3\n21 39\n40 1\n1 47\n35 12\n42 10\n2 4\n6 35\n17 16\n22 28\n14 22\n41 25\n10 14\n34 37\n27 20\n44 27\n20 2\n3 17\n45 13\n18 34\n47 15\n10 44\n25 15\n12 23\n27 17\n15 38\n17 32\n29 31\n3 39", "output": "NO" }, { "input": "48 32\n45 23\n17 3\n2 48\n47 20\n27 18\n13 28\n18 26\n26 21\n48 31\n21 9\n43 19\n34 43\n10 36\n14 17\n6 12\n3 11\n15 1\n23 37\n37 13\n42 40\n35 5\n16 7\n40 44\n4 29\n24 25\n5 16\n31 45\n39 22\n46 34\n22 30\n28 33\n33 41", "output": "YES\n16\n1 2\n4 6\n7 8\n8 9\n10 11\n12 14\n15 19\n20 24\n25 27\n29 30\n32 35\n32 36\n38 39\n38 41\n42 46\n44 47" }, { "input": "49 29\n43 18\n44 26\n49 31\n37 19\n20 16\n18 22\n30 5\n7 28\n12 2\n31 11\n27 43\n25 9\n19 4\n35 25\n4 30\n6 27\n46 41\n38 23\n17 37\n13 8\n11 38\n29 20\n40 10\n22 29\n36 7\n17 36\n35 48\n41 36\n39 27", "output": "NO" }, { "input": "50 27\n10 7\n32 9\n17 33\n25 34\n47 28\n23 16\n15 46\n41 50\n18 24\n27 19\n35 36\n19 38\n50 31\n31 40\n4 14\n1 11\n6 48\n33 35\n36 30\n39 12\n28 45\n2 1\n22 13\n3 49\n29 36\n7 34\n36 8", "output": "NO" }, { "input": "7 3\n7 4\n5 2\n1 3", "output": "YES\n4\n1 2\n3 4\n5 6\n6 7" }, { "input": "8 4\n1 7\n2 4\n6 2\n5 8", "output": "YES\n4\n1 3\n3 4\n5 6\n7 8" }, { "input": "9 2\n2 5\n1 6", "output": "YES\n7\n1 2\n3 4\n3 5\n4 7\n6 8\n7 9\n8 9" }, { "input": "3 2\n3 2\n2 1", "output": "YES\n1\n1 3" }, { "input": "4 2\n3 1\n4 2", "output": "YES\n2\n1 2\n3 4" } ]
278
0
0
19,702
665
Four Divisors
[ "data structures", "dp", "math", "number theory", "sortings", "two pointers" ]
null
null
If an integer *a* is divisible by another integer *b*, then *b* is called the divisor of *a*. For example: 12 has positive 6 divisors. They are 1, 2, 3, 4, 6 and 12. Let’s define a function *D*(*n*) β€” number of integers between 1 and *n* (inclusive) which has exactly four positive divisors. Between 1 and 10 only the integers 6, 8 and 10 has exactly four positive divisors. So, *D*(10)<==<=3. You are given an integer *n*. You have to calculate *D*(*n*).
The only line contains integer *n* (1<=≀<=*n*<=≀<=1011) β€” the parameter from the problem statement.
Print the only integer *c* β€” the number of integers between 1 and *n* with exactly four divisors.
[ "10\n", "20\n" ]
[ "3\n", "5\n" ]
none
[ { "input": "10", "output": "3" }, { "input": "20", "output": "5" }, { "input": "1", "output": "0" }, { "input": "27", "output": "9" }, { "input": "100", "output": "32" }, { "input": "1000", "output": "292" }, { "input": "10000", "output": "2608" }, { "input": "100000", "output": "23327" }, { "input": "1000000", "output": "209892" }, { "input": "100000000", "output": "17426119" }, { "input": "1000000000", "output": "160785303" }, { "input": "10000000000", "output": "1493767176" }, { "input": "100000000000", "output": "13959963675" }, { "input": "353964", "output": "77787" }, { "input": "437388", "output": "95228" }, { "input": "553516", "output": "119256" }, { "input": "636940", "output": "136364" }, { "input": "753068", "output": "160058" }, { "input": "803788", "output": "170304" }, { "input": "24403439", "output": "4484483" }, { "input": "907519567", "output": "146391993" }, { "input": "790635695", "output": "128128652" }, { "input": "968719119", "output": "155921638" }, { "input": "146802543", "output": "25227297" }, { "input": "324885967", "output": "54280184" }, { "input": "448485", "output": "97564" }, { "input": "324885968", "output": "54280184" } ]
46
4,915,200
0
19,766
995
Game
[ "math" ]
null
null
Allen and Bessie are playing a simple number game. They both know a function $f: \{0, 1\}^n \to \mathbb{R}$, i.Β e. the function takes $n$ binary arguments and returns a real value. At the start of the game, the variables $x_1, x_2, \dots, x_n$ are all set to $-1$. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an $i$ such that $x_i = -1$ and either setting $x_i \to 0$ or $x_i \to 1$. After $n$ rounds all variables are set, and the game value resolves to $f(x_1, x_2, \dots, x_n)$. Allen wants to maximize the game value, and Bessie wants to minimize it. Your goal is to help Allen and Bessie find the expected game value! They will play $r+1$ times though, so between each game, exactly one value of $f$ changes. In other words, between rounds $i$ and $i+1$ for $1 \le i \le r$, $f(z_1, \dots, z_n) \to g_i$ for some $(z_1, \dots, z_n) \in \{0, 1\}^n$. You are to find the expected game value in the beginning and after each change.
The first line contains two integers $n$ and $r$ ($1 \le n \le 18$, $0 \le r \le 2^{18}$). The next line contains $2^n$ integers $c_0, c_1, \dots, c_{2^n-1}$ ($0 \le c_i \le 10^9$), denoting the initial values of $f$. More specifically, $f(x_0, x_1, \dots, x_{n-1}) = c_x$, if $x = \overline{x_{n-1} \ldots x_0}$ in binary. Each of the next $r$ lines contains two integers $z$ and $g$ ($0 \le z \le 2^n - 1$, $0 \le g \le 10^9$). If $z = \overline{z_{n-1} \dots z_0}$ in binary, then this means to set $f(z_0, \dots, z_{n-1}) \to g$.
Print $r+1$ lines, the $i$-th of which denotes the value of the game $f$ during the $i$-th round. Your answer must have absolute or relative error within $10^{-6}$. Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is considered correct if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$.
[ "2 2\n0 1 2 3\n2 5\n0 4\n", "1 0\n2 3\n", "2 0\n1 1 1 1\n" ]
[ "1.500000\n2.250000\n3.250000\n", "2.500000\n", "1.000000\n" ]
Consider the second test case. If Allen goes first, he will set $x_1 \to 1$, so the final value will be $3$. If Bessie goes first, then she will set $x_1 \to 0$ so the final value will be $2$. Thus the answer is $2.5$. In the third test case, the game value will always be $1$ regardless of Allen and Bessie's play.
[ { "input": "2 2\n0 1 2 3\n2 5\n0 4", "output": "1.500000\n2.250000\n3.250000" }, { "input": "1 0\n2 3", "output": "2.500000" }, { "input": "2 0\n1 1 1 1", "output": "1.000000" } ]
30
0
0
19,770
846
Four Segments
[ "brute force", "data structures", "dp" ]
null
null
You are given an array of *n* integer numbers. Let *sum*(*l*,<=*r*) be the sum of all numbers on positions from *l* to *r* non-inclusive (*l*-th element is counted, *r*-th element is not counted). For indices *l* and *r* holds 0<=≀<=*l*<=≀<=*r*<=≀<=*n*. Indices in array are numbered from 0. For example, if *a*<==<=[<=-<=5,<=3,<=9,<=4], then *sum*(0,<=1)<==<=<=-<=5, *sum*(0,<=2)<==<=<=-<=2, *sum*(1,<=4)<==<=16 and *sum*(*i*,<=*i*)<==<=0 for each *i* from 0 to 4. Choose the indices of three delimiters *delim*0, *delim*1, *delim*2 (0<=≀<=*delim*0<=≀<=*delim*1<=≀<=*delim*2<=≀<=*n*) and divide the array in such a way that the value of *res*<==<=*sum*(0,<=*delim*0) - *sum*(*delim*0,<=*delim*1) + *sum*(*delim*1,<=*delim*2) - *sum*(*delim*2,<=*n*) is maximal. Note that some of the expressions *sum*(*l*,<=*r*) can correspond to empty segments (if *l*<==<=*r* for some segment).
The first line contains one integer number *n* (1<=≀<=*n*<=≀<=5000). The second line contains *n* numbers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (<=-<=109<=≀<=*a**i*<=≀<=109).
Choose three indices so that the value of *res* is maximal. If there are multiple answers, print any of them.
[ "3\n-1 2 3\n", "4\n0 0 -1 0\n", "1\n10000\n" ]
[ "0 1 3\n", "0 0 0\n", "1 1 1\n" ]
none
[ { "input": "3\n-1 2 3", "output": "0 1 3" }, { "input": "4\n0 0 -1 0", "output": "0 0 0" }, { "input": "1\n10000", "output": "0 0 1" }, { "input": "1\n-1", "output": "0 0 0" }, { "input": "1\n0", "output": "0 0 0" }, { "input": "10\n0 0 0 0 0 0 0 0 0 0", "output": "0 0 0" }, { "input": "100\n-1 -1 0 1 -1 0 0 -1 -1 1 1 0 1 0 -1 1 0 0 -1 0 0 0 1 0 0 1 1 1 1 0 -1 -1 0 0 0 1 1 -1 0 0 1 1 1 0 -1 -1 0 -1 0 -1 -1 0 1 0 1 -1 1 -1 -1 -1 0 0 -1 0 -1 -1 0 -1 1 1 -1 1 0 1 -1 -1 1 1 -1 1 0 -1 1 -1 1 1 1 1 1 -1 0 1 0 0 -1 0 1 1 -1 -1", "output": "43 68 98" }, { "input": "100\n2 1 -2 -1 -2 0 -1 -2 1 2 0 1 0 -2 -1 -2 0 2 0 1 -2 -2 2 0 -2 2 -2 0 2 0 2 0 -1 0 -2 2 -1 -1 -2 -1 0 -2 2 0 -2 -2 -2 -1 1 0 -2 -1 2 -1 -2 1 -1 1 1 2 -2 1 -2 1 2 2 -2 1 -2 0 -1 -1 -2 -2 1 0 -1 -1 2 0 2 0 -1 2 1 -1 2 0 2 1 1 1 -1 -1 1 -2 0 0 -2 0", "output": "2 78 92" }, { "input": "10\n-6 -4 -7 0 7 9 8 3 8 7", "output": "0 3 10" }, { "input": "4\n-1 1 -2 -2", "output": "0 1 2" }, { "input": "2\n-3 -1", "output": "0 0 0" } ]
62
0
0
19,782
486
OR in Matrix
[ "greedy", "hashing", "implementation" ]
null
null
Let's define logical *OR* as an operation on two logical values (i. e. values that belong to the set {0,<=1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical *OR* of three or more logical values in the same manner: where is equal to 1 if some *a**i*<==<=1, otherwise it is equal to 0. Nam has a matrix *A* consisting of *m* rows and *n* columns. The rows are numbered from 1 to *m*, columns are numbered from 1 to *n*. Element at row *i* (1<=≀<=*i*<=≀<=*m*) and column *j* (1<=≀<=*j*<=≀<=*n*) is denoted as *A**ij*. All elements of *A* are either 0 or 1. From matrix *A*, Nam creates another matrix *B* of the same size using formula: . (*B**ij* is *OR* of all elements in row *i* and column *j* of matrix *A*) Nam gives you matrix *B* and challenges you to guess matrix *A*. Although Nam is smart, he could probably make a mistake while calculating matrix *B*, since size of *A* can be large.
The first line contains two integer *m* and *n* (1<=≀<=*m*,<=*n*<=≀<=100), number of rows and number of columns of matrices respectively. The next *m* lines each contain *n* integers separated by spaces describing rows of matrix *B* (each element of *B* is either 0 or 1).
In the first line, print "NO" if Nam has made a mistake when calculating *B*, otherwise print "YES". If the first line is "YES", then also print *m* rows consisting of *n* integers representing matrix *A* that can produce given matrix *B*. If there are several solutions print any one.
[ "2 2\n1 0\n0 0\n", "2 3\n1 1 1\n1 1 1\n", "2 3\n0 1 0\n1 1 1\n" ]
[ "NO\n", "YES\n1 1 1\n1 1 1\n", "YES\n0 0 0\n0 1 0\n" ]
none
[ { "input": "2 2\n1 0\n0 0", "output": "NO" }, { "input": "2 3\n1 1 1\n1 1 1", "output": "YES\n1 1 1\n1 1 1" }, { "input": "2 3\n0 1 0\n1 1 1", "output": "YES\n0 0 0\n0 1 0" }, { "input": "5 5\n1 1 1 1 1\n1 0 0 0 0\n1 0 0 0 0\n1 0 0 0 0\n1 0 0 0 0", "output": "YES\n1 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0\n0 0 0 0 0" }, { "input": "5 5\n1 1 1 0 1\n1 1 0 0 1\n0 0 1 1 1\n1 1 1 1 0\n1 0 1 1 1", "output": "NO" }, { "input": "5 6\n1 0 0 0 1 1\n1 1 1 1 1 1\n1 1 1 1 1 1\n1 0 0 0 1 1\n1 0 0 0 1 1", "output": "YES\n0 0 0 0 0 0\n1 0 0 0 1 1\n1 0 0 0 1 1\n0 0 0 0 0 0\n0 0 0 0 0 0" }, { "input": "5 6\n1 1 1 1 0 1\n1 1 1 1 0 1\n1 1 1 0 1 1\n1 1 0 1 1 1\n0 0 1 1 1 0", "output": "NO" }, { "input": "7 10\n1 0 1 0 0 0 1 0 1 0\n1 0 1 0 0 0 1 0 1 0\n1 1 1 1 1 1 1 1 1 1\n1 0 1 0 0 0 1 0 1 0\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1\n1 1 1 1 1 1 1 1 1 1", "output": "YES\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n1 0 1 0 0 0 1 0 1 0\n0 0 0 0 0 0 0 0 0 0\n1 0 1 0 0 0 1 0 1 0\n1 0 1 0 0 0 1 0 1 0\n1 0 1 0 0 0 1 0 1 0" }, { "input": "8 2\n0 1\n0 1\n1 0\n0 1\n0 1\n0 1\n0 1\n0 1", "output": "NO" }, { "input": "1 1\n0", "output": "YES\n0" }, { "input": "1 1\n1", "output": "YES\n1" }, { "input": "3 3\n1 0 0\n1 0 0\n1 0 0", "output": "NO" }, { "input": "3 2\n1 0\n1 0\n0 0", "output": "NO" }, { "input": "2 2\n0 0\n0 0", "output": "YES\n0 0\n0 0" }, { "input": "3 3\n0 0 0\n0 0 0\n0 0 0", "output": "YES\n0 0 0\n0 0 0\n0 0 0" }, { "input": "3 2\n1 0\n1 0\n1 0", "output": "NO" }, { "input": "1 2\n1 0", "output": "NO" }, { "input": "3 3\n0 1 0\n0 1 0\n0 1 0", "output": "NO" }, { "input": "3 3\n1 1 1\n0 0 0\n0 0 0", "output": "NO" }, { "input": "3 3\n1 0 1\n0 0 1\n1 1 1", "output": "NO" }, { "input": "1 3\n0 1 1", "output": "NO" }, { "input": "2 3\n0 1 0\n0 1 1", "output": "NO" }, { "input": "2 3\n0 0 0\n0 0 0", "output": "YES\n0 0 0\n0 0 0" }, { "input": "6 6\n0 0 1 1 0 0\n0 0 1 1 0 0\n1 1 1 1 1 1\n1 1 1 1 1 1\n0 0 1 1 0 0\n0 0 1 1 0 1", "output": "NO" }, { "input": "2 3\n0 0 0\n1 1 1", "output": "NO" }, { "input": "2 2\n1 1\n0 0", "output": "NO" }, { "input": "5 5\n0 1 0 0 0\n1 1 1 1 1\n0 1 0 0 0\n0 1 0 0 0\n0 1 0 0 1", "output": "NO" }, { "input": "3 3\n1 1 1\n1 1 0\n1 0 0", "output": "NO" } ]
1,000
819,200
0
19,827
838
Airplane Arrangements
[ "math", "number theory" ]
null
null
There is an airplane which has *n* rows from front to back. There will be *m* people boarding this airplane. This airplane has an entrance at the very front and very back of the plane. Each person has some assigned seat. It is possible for multiple people to have the same assigned seat. The people will then board the plane one by one starting with person 1. Each person can independently choose either the front entrance or back entrance to enter the plane. When a person walks into the plane, they walk directly to their assigned seat and will try to sit in it. If it is occupied, they will continue walking in the direction they walked in until they are at empty seat - they will take the earliest empty seat that they can find. If they get to the end of the row without finding a seat, they will be angry. Find the number of ways to assign tickets to the passengers and board the plane without anyone getting angry. Two ways are different if there exists a passenger who chose a different entrance in both ways, or the assigned seat is different. Print this count modulo 109<=+<=7.
The first line of input will contain two integers *n*,<=*m* (1<=≀<=*m*<=≀<=*n*<=≀<=1<=000<=000), the number of seats, and the number of passengers, respectively.
Print a single number, the number of ways, modulo 109<=+<=7.
[ "3 3\n" ]
[ "128\n" ]
Here, we will denote a passenger by which seat they were assigned, and which side they came from (either "F" or "B" for front or back, respectively). For example, one valid way is 3B, 3B, 3B (i.e. all passengers were assigned seat 3 and came from the back entrance). Another valid way would be 2F, 1B, 3F. One invalid way would be 2B, 2B, 2B, since the third passenger would get to the front without finding a seat.
[ { "input": "3 3", "output": "128" }, { "input": "1000000 1000000", "output": "233176135" }, { "input": "1000000 500000", "output": "211837745" }, { "input": "1 1", "output": "2" }, { "input": "10 1", "output": "20" }, { "input": "285042 201091", "output": "348727840" }, { "input": "896437 604720", "output": "531995995" }, { "input": "284114 73851", "output": "935093233" }, { "input": "541826 316395", "output": "365726326" }, { "input": "353093 96536", "output": "708633906" }, { "input": "540898 158491", "output": "698076231" }, { "input": "858309 773589", "output": "875072331" }, { "input": "56322 42432", "output": "905316418" }, { "input": "461466 56468", "output": "616418222" }, { "input": "29102 1503", "output": "211174820" }, { "input": "42800 41731", "output": "178922948" }, { "input": "235175 92933", "output": "704139178" }, { "input": "921643 744360", "output": "959987426" }, { "input": "619924 583916", "output": "765568563" }, { "input": "43657 852", "output": "898633472" }, { "input": "4672 3086", "output": "648722588" }, { "input": "197047 148580", "output": "132050966" }, { "input": "693851 210584", "output": "800890261" }, { "input": "951563 122804", "output": "202475849" }, { "input": "175236 173750", "output": "291135880" }, { "input": "784160 282537", "output": "252488614" }, { "input": "976535 433238", "output": "30881486" }, { "input": "827825 745802", "output": "28515641" }, { "input": "361284 5729", "output": "121235105" }, { "input": "189791 36882", "output": "503014832" }, { "input": "84609 75872", "output": "860171419" }, { "input": "938407 501656", "output": "321500030" }, { "input": "600033 306982", "output": "214582457" }, { "input": "857745 223544", "output": "778808942" }, { "input": "321370 271684", "output": "624745554" }, { "input": "41872 1808", "output": "389891349" }, { "input": "234247 67712", "output": "610314478" }, { "input": "734006 258894", "output": "822257297" }, { "input": "991718 318936", "output": "688082968" }, { "input": "335925 159533", "output": "401609204" }, { "input": "3745 1612", "output": "100232679" }, { "input": "196119 47809", "output": "831275903" }, { "input": "506214 320883", "output": "31547174" }, { "input": "960651 256313", "output": "500076538" }, { "input": "107210 13886", "output": "179122019" }, { "input": "124763 65049", "output": "454953468" }, { "input": "491959 252209", "output": "696157573" }, { "input": "684333 613651", "output": "980362331" }, { "input": "897899 478680", "output": "889928809" }, { "input": "69082 16337", "output": "24188373" }, { "input": "261457 212062", "output": "866036254" }, { "input": "453831 290298", "output": "552613881" }, { "input": "646205 361804", "output": "801930294" }, { "input": "838580 174298", "output": "488250696" }, { "input": "364922 343089", "output": "140158453" }, { "input": "30945 5665", "output": "758927360" }, { "input": "749671 469976", "output": "673292024" }, { "input": "942045 878421", "output": "214250096" }, { "input": "287729 11831", "output": "625218018" }, { "input": "144546 128076", "output": "232200563" }, { "input": "519169 430233", "output": "44864151" }, { "input": "711543 136245", "output": "40200989" }, { "input": "903917 186673", "output": "762310964" }, { "input": "252919 105355", "output": "941982792" }, { "input": "106282 12802", "output": "237272767" } ]
140
23,142,400
3
19,836
877
Olya and Energy Drinks
[ "data structures", "dfs and similar", "graphs", "shortest paths" ]
null
null
Olya loves energy drinks. She loves them so much that her room is full of empty cans from energy drinks. Formally, her room can be represented as a field of *n*<=Γ—<=*m* cells, each cell of which is empty or littered with cans. Olya drank a lot of energy drink, so now she can run *k* meters per second. Each second she chooses one of the four directions (up, down, left or right) and runs from 1 to *k* meters in this direction. Of course, she can only run through empty cells. Now Olya needs to get from cell (*x*1,<=*y*1) to cell (*x*2,<=*y*2). How many seconds will it take her if she moves optimally? It's guaranteed that cells (*x*1,<=*y*1) and (*x*2,<=*y*2) are empty. These cells can coincide.
The first line contains three integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*,<=*k*<=≀<=1000) β€” the sizes of the room and Olya's speed. Then *n* lines follow containing *m* characters each, the *i*-th of them contains on *j*-th position "#", if the cell (*i*,<=*j*) is littered with cans, and "." otherwise. The last line contains four integers *x*1,<=*y*1,<=*x*2,<=*y*2 (1<=≀<=*x*1,<=*x*2<=≀<=*n*, 1<=≀<=*y*1,<=*y*2<=≀<=*m*) β€” the coordinates of the first and the last cells.
Print a single integer β€” the minimum time it will take Olya to get from (*x*1,<=*y*1) to (*x*2,<=*y*2). If it's impossible to get from (*x*1,<=*y*1) to (*x*2,<=*y*2), print -1.
[ "3 4 4\n....\n###.\n....\n1 1 3 1\n", "3 4 1\n....\n###.\n....\n1 1 3 1\n", "2 2 1\n.#\n#.\n1 1 2 2\n" ]
[ "3", "8", "-1" ]
In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second. In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds. Olya does not recommend drinking energy drinks and generally believes that this is bad.
[ { "input": "3 4 4\n....\n###.\n....\n1 1 3 1", "output": "3" }, { "input": "3 4 1\n....\n###.\n....\n1 1 3 1", "output": "8" }, { "input": "2 2 1\n.#\n#.\n1 1 2 2", "output": "-1" }, { "input": "10 10 1\n##########\n#.........\n#.#######.\n#.#.....#.\n#.#.###.#.\n#.#.#.#.#.\n#.#.#.#.#.\n#.#.#...#.\n#.#.#####.\n#.#.......\n6 6 10 2", "output": "48" }, { "input": "10 10 3\n##########\n##########\n##########\n##########\n##########\n##########\n##########\n#########.\n#########.\n####..###.\n10 6 10 5", "output": "1" }, { "input": "10 10 3\n...##..#..\n#.#..#...#\n..#.##.#..\n##..#..#.#\n..#...##..\n.#.#.#....\n#......#..\n.#.####.##\n......#...\n.#.##...##\n4 6 8 1", "output": "7" }, { "input": "10 10 1000\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n7 6 1 3", "output": "2" }, { "input": "10 10 1000\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n..........\n.....#....\n3 9 5 1", "output": "2" }, { "input": "10 10 10\n...#......\n##.#.#####\n...#...###\n.#####.###\n...#...###\n##.#.#####\n...#...###\n.#####.###\n.......###\n##########\n1 1 1 10", "output": "17" }, { "input": "2 5 5\n#####\n##.##\n2 3 2 3", "output": "0" }, { "input": "4 4 4\n...#\n.#.#\n....\n##..\n1 1 3 4", "output": "2" }, { "input": "6 6 100\n....##\n.##.##\n.##.##\n......\n###.##\n###.##\n1 1 4 6", "output": "2" } ]
576
37,888,000
0
19,854
333
Summer Earnings
[ "binary search", "bitmasks", "brute force", "geometry", "sortings" ]
null
null
Many schoolchildren look for a job for the summer, and one day, when Gerald was still a schoolboy, he also decided to work in the summer. But as Gerald was quite an unusual schoolboy, he found quite unusual work. A certain Company agreed to pay him a certain sum of money if he draws them three identical circles on a plane. The circles must not interfere with each other (but they may touch each other). He can choose the centers of the circles only from the *n* options granted by the Company. He is free to choose the radius of the circles himself (all three radiuses must be equal), but please note that the larger the radius is, the more he gets paid. Help Gerald earn as much as possible.
The first line contains a single integer *n* β€” the number of centers (3<=≀<=*n*<=≀<=3000). The following *n* lines each contain two integers *x**i*,<=*y**i* (<=-<=104<=≀<=*x**i*,<=*y**i*<=≀<=104) β€” the coordinates of potential circle centers, provided by the Company. All given points are distinct.
Print a single real number β€” maximum possible radius of circles. The answer will be accepted if its relative or absolute error doesn't exceed 10<=-<=6.
[ "3\n0 1\n1 0\n1 1\n", "7\n2 -3\n-2 -3\n3 0\n-3 -1\n1 -2\n2 -2\n-1 0\n" ]
[ "0.50000000000000000000\n", "1.58113883008418980000\n" ]
none
[]
3,368
268,390,400
0
19,857
847
Dog Show
[ "constructive algorithms", "data structures", "greedy" ]
null
null
A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win! On the show a dog needs to eat as many bowls of dog food as possible (bottomless stomach helps here). Dogs compete separately of each other and the rules are as follows: At the start of the show the dog and the bowls are located on a line. The dog starts at position *x*<==<=0 and *n* bowls are located at positions *x*<==<=1,<=*x*<==<=2,<=...,<=*x*<==<=*n*. The bowls are numbered from 1 to *n* from left to right. After the show starts the dog immediately begins to run to the right to the first bowl. The food inside bowls is not ready for eating at the start because it is too hot (dog's self-preservation instinct prevents eating). More formally, the dog can eat from the *i*-th bowl after *t**i* seconds from the start of the show or later. It takes dog 1 second to move from the position *x* to the position *x*<=+<=1. The dog is not allowed to move to the left, the dog runs only to the right with the constant speed 1 distance unit per second. When the dog reaches a bowl (say, the bowl *i*), the following cases are possible: - the food had cooled down (i.e. it passed at least *t**i* seconds from the show start): the dog immediately eats the food and runs to the right without any stop, - the food is hot (i.e. it passed less than *t**i* seconds from the show start): the dog has two options: to wait for the *i*-th bowl, eat the food and continue to run at the moment *t**i* or to skip the *i*-th bowl and continue to run to the right without any stop. After *T* seconds from the start the show ends. If the dog reaches a bowl of food at moment *T* the dog can not eat it. The show stops before *T* seconds if the dog had run to the right of the last bowl. You need to help your dog create a strategy with which the maximum possible number of bowls of food will be eaten in *T* seconds.
Two integer numbers are given in the first line - *n* and *T* (1<=≀<=*n*<=≀<=200<=000, 1<=≀<=*T*<=≀<=2Β·109) β€” the number of bowls of food and the time when the dog is stopped. On the next line numbers *t*1,<=*t*2,<=...,<=*t**n* (1<=≀<=*t**i*<=≀<=109) are given, where *t**i* is the moment of time when the *i*-th bowl of food is ready for eating.
Output a single integer β€” the maximum number of bowls of food the dog will be able to eat in *T* seconds.
[ "3 5\n1 5 3\n", "1 2\n1\n", "1 1\n1\n" ]
[ "2\n", "1\n", "0\n" ]
In the first example the dog should skip the second bowl to eat from the two bowls (the first and the third).
[ { "input": "3 5\n1 5 3", "output": "2" }, { "input": "1 2\n1", "output": "1" }, { "input": "1 1\n1", "output": "0" }, { "input": "1 1\n2", "output": "0" }, { "input": "2 2\n2 3", "output": "0" }, { "input": "2 3\n2 1", "output": "1" }, { "input": "3 3\n2 3 2", "output": "1" }, { "input": "3 2\n2 3 4", "output": "0" }, { "input": "3 4\n2 1 2", "output": "2" }, { "input": "4 4\n2 1 2 3", "output": "2" }, { "input": "4 3\n2 1 2 3", "output": "1" }, { "input": "4 6\n2 3 4 5", "output": "4" }, { "input": "5 5\n2 1 2 3 4", "output": "3" }, { "input": "5 3\n2 3 2 1 2", "output": "1" }, { "input": "5 7\n2 1 2 3 4", "output": "5" }, { "input": "6 6\n2 3 2 3 4 3", "output": "4" }, { "input": "6 4\n2 3 2 3 4 3", "output": "2" }, { "input": "6 9\n2 1 2 1 2 3", "output": "6" }, { "input": "7 7\n2 3 4 5 6 5 6", "output": "5" }, { "input": "7 4\n2 1 2 3 2 3 2", "output": "2" }, { "input": "7 10\n2 3 4 3 2 3 2", "output": "7" }, { "input": "8 8\n2 3 2 3 4 5 4 5", "output": "6" }, { "input": "8 5\n2 3 2 3 4 3 4 3", "output": "3" }, { "input": "8 12\n2 3 2 3 4 3 4 3", "output": "8" }, { "input": "9 9\n2 3 4 5 4 5 6 7 6", "output": "7" }, { "input": "9 5\n2 3 4 3 2 3 4 5 6", "output": "3" }, { "input": "9 13\n2 1 2 3 4 5 4 5 6", "output": "9" }, { "input": "10 10\n2 1 2 3 4 3 4 3 4 3", "output": "8" }, { "input": "10 6\n2 3 4 3 4 5 6 7 6 7", "output": "4" }, { "input": "10 15\n2 1 2 1 2 3 4 5 6 7", "output": "10" }, { "input": "11 11\n2 3 4 5 6 5 4 5 4 3 4", "output": "9" }, { "input": "11 6\n2 3 4 3 4 3 4 5 4 3 2", "output": "4" }, { "input": "11 16\n2 3 2 1 2 3 4 5 4 3 4", "output": "11" }, { "input": "12 12\n2 3 4 5 6 7 6 7 8 9 10 11", "output": "10" }, { "input": "12 7\n2 3 4 3 4 3 2 3 4 3 4 5", "output": "5" }, { "input": "12 18\n2 1 2 3 4 5 6 5 6 5 6 5", "output": "12" }, { "input": "13 13\n2 1 2 3 4 3 2 3 4 5 6 5 4", "output": "11" }, { "input": "13 7\n2 1 2 3 2 3 2 3 4 3 4 5 6", "output": "5" }, { "input": "13 19\n2 3 4 5 6 5 4 5 6 7 8 9 8", "output": "13" }, { "input": "14 14\n2 3 4 5 6 7 8 9 10 11 12 13 14 15", "output": "12" }, { "input": "14 8\n2 3 4 5 6 7 8 7 6 7 8 9 10 9", "output": "6" }, { "input": "14 21\n2 1 2 3 4 5 6 5 6 7 8 9 8 9", "output": "14" }, { "input": "15 15\n2 3 4 3 2 3 4 3 4 3 4 5 6 5 6", "output": "13" }, { "input": "15 8\n2 3 2 1 2 1 2 3 2 3 4 3 4 5 4", "output": "6" }, { "input": "15 22\n2 3 2 3 2 3 4 5 6 7 6 7 8 9 10", "output": "15" }, { "input": "16 16\n2 1 2 3 2 3 4 5 6 5 4 5 6 5 6 7", "output": "14" }, { "input": "16 9\n2 3 4 5 4 3 4 5 6 7 8 7 8 9 10 11", "output": "7" }, { "input": "16 24\n2 3 4 5 6 5 6 7 6 7 8 9 10 11 12 13", "output": "16" }, { "input": "17 17\n2 3 2 1 2 3 4 5 6 7 8 9 10 11 12 11 12", "output": "15" }, { "input": "17 9\n2 3 4 5 6 7 8 9 10 11 10 11 10 11 12 13 12", "output": "7" }, { "input": "17 25\n2 1 2 1 2 3 2 3 2 1 2 1 2 1 2 1 2", "output": "17" }, { "input": "18 18\n2 3 4 5 4 5 6 5 6 7 6 7 6 5 6 7 8 7", "output": "16" }, { "input": "18 10\n2 3 4 3 4 3 4 5 6 5 6 7 8 9 10 9 8 9", "output": "8" }, { "input": "18 27\n2 3 4 3 4 5 6 7 8 9 10 9 8 9 8 9 10 9", "output": "18" }, { "input": "19 19\n2 1 2 3 4 5 4 5 6 7 6 7 8 9 10 11 12 11 12", "output": "17" }, { "input": "19 10\n2 1 2 3 4 3 4 3 2 3 4 3 4 3 4 5 6 5 4", "output": "8" }, { "input": "19 28\n2 3 4 3 4 5 6 5 6 5 6 7 8 7 8 9 10 11 12", "output": "19" }, { "input": "20 20\n2 1 2 3 2 1 2 3 4 3 2 3 4 5 6 7 8 9 8 9", "output": "18" }, { "input": "20 11\n2 3 4 5 6 7 6 5 6 7 8 9 10 11 12 11 12 13 12 11", "output": "9" }, { "input": "20 30\n2 3 2 3 4 5 6 5 6 7 6 7 8 9 8 7 8 9 10 11", "output": "20" }, { "input": "1 1\n2", "output": "0" }, { "input": "2 2\n2 3", "output": "0" }, { "input": "2 3\n2 3", "output": "1" }, { "input": "3 3\n2 1 2", "output": "1" }, { "input": "3 2\n2 1 2", "output": "0" }, { "input": "3 4\n2 1 2", "output": "2" }, { "input": "4 4\n2 3 2 3", "output": "2" }, { "input": "4 3\n2 1 2 3", "output": "1" }, { "input": "4 6\n2 3 4 5", "output": "4" }, { "input": "5 5\n2 1 2 3 4", "output": "3" }, { "input": "5 3\n2 3 4 5 6", "output": "1" }, { "input": "5 7\n2 3 4 5 6", "output": "5" }, { "input": "6 6\n2 1 2 3 4 5", "output": "4" }, { "input": "6 4\n2 3 4 5 6 7", "output": "2" }, { "input": "6 9\n2 3 4 5 6 7", "output": "6" }, { "input": "7 7\n2 1 2 1 2 3 4", "output": "5" }, { "input": "7 4\n2 3 4 5 4 5 6", "output": "2" }, { "input": "7 10\n2 1 2 3 2 3 4", "output": "7" }, { "input": "8 8\n2 3 2 3 2 3 4 5", "output": "6" }, { "input": "8 5\n2 3 4 3 2 3 4 3", "output": "3" }, { "input": "8 12\n2 3 4 3 2 3 4 3", "output": "8" }, { "input": "9 9\n2 1 2 3 4 5 6 5 6", "output": "7" }, { "input": "9 5\n2 1 2 3 4 3 2 3 4", "output": "3" }, { "input": "9 13\n2 3 4 5 6 5 6 7 8", "output": "9" }, { "input": "10 10\n2 3 4 3 4 5 6 7 6 7", "output": "8" }, { "input": "10 6\n2 3 4 5 6 7 8 9 10 11", "output": "4" }, { "input": "10 15\n2 3 4 5 6 7 8 9 10 11", "output": "10" }, { "input": "11 11\n2 3 4 5 6 7 8 9 10 11 12", "output": "9" }, { "input": "11 6\n2 3 4 5 6 7 8 7 8 9 8", "output": "4" }, { "input": "11 16\n2 3 4 5 6 5 6 5 6 5 6", "output": "11" }, { "input": "12 12\n2 1 2 3 4 5 6 7 8 7 6 5", "output": "10" }, { "input": "12 7\n2 3 4 5 6 7 8 9 10 11 10 11", "output": "5" }, { "input": "12 18\n2 1 2 3 2 3 2 1 2 3 2 3", "output": "12" }, { "input": "13 13\n2 3 4 5 6 7 8 7 6 7 8 9 10", "output": "11" }, { "input": "13 7\n2 3 4 5 6 7 8 9 10 11 12 13 14", "output": "5" }, { "input": "13 19\n2 3 4 5 6 5 6 7 6 7 8 9 8", "output": "13" }, { "input": "14 14\n2 3 4 5 6 5 4 5 6 7 8 7 8 9", "output": "12" }, { "input": "14 8\n2 3 4 5 6 7 6 7 8 7 8 9 10 11", "output": "6" }, { "input": "14 21\n2 1 2 3 4 5 4 5 4 5 4 3 4 5", "output": "14" }, { "input": "15 15\n2 1 2 3 2 3 4 5 6 5 6 5 6 5 6", "output": "13" }, { "input": "15 8\n2 3 4 3 4 5 6 7 8 7 6 5 6 7 8", "output": "6" }, { "input": "15 22\n2 3 2 1 2 3 4 5 6 7 8 9 10 9 10", "output": "15" }, { "input": "16 16\n2 3 4 5 6 5 6 7 8 7 6 7 8 9 10 11", "output": "14" }, { "input": "16 9\n2 1 2 3 4 5 6 5 4 5 6 7 8 9 10 11", "output": "7" }, { "input": "16 24\n2 3 4 5 6 7 8 9 10 9 10 9 10 11 12 13", "output": "16" }, { "input": "17 17\n2 3 2 3 4 3 4 5 6 7 8 9 8 7 6 7 8", "output": "15" }, { "input": "17 9\n2 1 2 3 4 3 4 5 6 7 8 9 10 11 10 11 12", "output": "7" }, { "input": "17 25\n2 3 4 3 2 3 2 1 2 3 4 5 4 5 4 5 6", "output": "17" }, { "input": "18 18\n2 3 2 3 4 5 6 5 6 7 8 9 10 11 12 13 14 15", "output": "16" }, { "input": "18 10\n2 3 4 5 6 7 8 9 10 11 12 13 12 11 10 9 10 11", "output": "8" }, { "input": "18 27\n2 3 4 5 6 7 8 9 10 9 10 9 10 11 10 9 10 11", "output": "18" }, { "input": "19 19\n2 3 4 5 6 5 4 5 6 7 8 9 10 11 12 13 14 15 16", "output": "17" }, { "input": "19 10\n2 1 2 3 4 3 4 5 4 5 6 7 8 9 10 11 12 13 14", "output": "8" }, { "input": "19 28\n2 1 2 3 4 5 4 5 6 7 8 9 8 9 10 9 8 9 8", "output": "19" }, { "input": "20 20\n2 3 4 5 6 7 8 9 10 11 12 11 12 13 14 15 16 17 18 19", "output": "18" }, { "input": "20 11\n2 3 2 3 4 5 6 5 6 7 8 7 6 7 8 7 8 9 8 9", "output": "9" }, { "input": "20 30\n2 3 4 5 4 5 4 5 6 7 8 9 10 11 12 13 14 15 16 17", "output": "20" }, { "input": "100 180\n150 52 127 175 146 138 25 71 192 108 142 79 196 129 23 44 92 11 63 198 197 65 47 144 141 158 142 41 1 102 113 50 171 97 75 31 199 24 17 59 138 53 37 123 64 103 156 141 33 186 150 10 103 29 2 182 38 85 155 73 136 175 83 93 20 59 11 87 178 92 132 11 6 99 109 193 135 132 57 36 123 152 36 80 9 137 122 131 122 108 44 84 180 65 192 192 29 150 147 20", "output": "68" }, { "input": "100 154\n132 88 72 98 184 47 176 56 68 168 137 88 188 140 198 18 162 139 94 133 90 91 37 156 196 28 186 1 51 47 4 92 18 51 37 121 86 195 153 195 183 191 15 24 104 174 94 83 102 61 131 40 149 46 22 112 13 136 133 177 3 175 160 152 172 48 44 174 77 100 155 157 167 174 64 109 118 194 120 7 8 179 36 149 58 145 163 163 45 14 164 111 176 196 42 161 71 148 192 38", "output": "44" }, { "input": "7 11\n3 7 10 13 9 12 4", "output": "3" }, { "input": "10 20\n5 12 21 14 23 17 24 11 25 22", "output": "5" } ]
77
0
0
19,860
568
Symmetric and Transitive
[ "combinatorics", "dp", "math" ]
null
null
Little Johnny has recently learned about set theory. Now he is studying binary relations. You've probably heard the term "equivalence relation". These relations are very important in many areas of mathematics. For example, the equality of the two numbers is an equivalence relation. A set ρ of pairs (*a*,<=*b*) of elements of some set *A* is called a binary relation on set *A*. For two elements *a* and *b* of the set *A* we say that they are in relation ρ, if pair , in this case we use a notation . Binary relation is equivalence relation, if: 1. It is reflexive (for any *a* it is true that );1. It is symmetric (for any *a*, *b* it is true that if , then );1. It is transitive (if and , than ). Little Johnny is not completely a fool and he noticed that the first condition is not necessary! Here is his "proof": Take any two elements, *a* and *b*. If , then (according to property (2)), which means (according to property (3)). It's very simple, isn't it? However, you noticed that Johnny's "proof" is wrong, and decided to show him a lot of examples that prove him wrong. Here's your task: count the number of binary relations over a set of size *n* such that they are symmetric, transitive, but not an equivalence relations (i.e. they are not reflexive). Since their number may be very large (not 0, according to Little Johnny), print the remainder of integer division of this number by 109<=+<=7.
A single line contains a single integer *n* (1<=≀<=*n*<=≀<=4000).
In a single line print the answer to the problem modulo 109<=+<=7.
[ "1\n", "2\n", "3\n" ]
[ "1\n", "3\n", "10\n" ]
If *n* = 1 there is only one such relationΒ β€” an empty one, i.e. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8891a227c918474e5d76377d4644cd7cc01e1ffd.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In other words, for a single element *x* of set *A* the following is hold: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/947c6cf761375432db9bd77796bccc89f1f1546d.png" style="max-width: 100.0%;max-height: 100.0%;"/>. If *n* = 2 there are three such relations. Let's assume that set *A* consists of two elements, *x* and *y*. Then the valid relations are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8891a227c918474e5d76377d4644cd7cc01e1ffd.png" style="max-width: 100.0%;max-height: 100.0%;"/>, ρ = {(*x*, *x*)}, ρ = {(*y*, *y*)}. It is easy to see that the three listed binary relations are symmetric and transitive relations, but they are not equivalence relations.
[ { "input": "1", "output": "1" }, { "input": "2", "output": "3" }, { "input": "3", "output": "10" }, { "input": "4", "output": "37" }, { "input": "5", "output": "151" }, { "input": "6", "output": "674" }, { "input": "7", "output": "3263" }, { "input": "8", "output": "17007" }, { "input": "9", "output": "94828" }, { "input": "10", "output": "562595" }, { "input": "42", "output": "738186543" }, { "input": "2000", "output": "323848720" }, { "input": "4000", "output": "341934157" }, { "input": "2345", "output": "832335061" }, { "input": "2500", "output": "544067513" }, { "input": "2780", "output": "951043097" }, { "input": "2999", "output": "634360769" }, { "input": "3000", "output": "949793998" }, { "input": "20", "output": "654959364" }, { "input": "76", "output": "130527569" }, { "input": "133", "output": "334338018" }, { "input": "345", "output": "838683603" }, { "input": "555", "output": "31983119" }, { "input": "666", "output": "86247911" }, { "input": "777", "output": "765401747" }, { "input": "999", "output": "867937200" }, { "input": "1234", "output": "845807965" }, { "input": "1730", "output": "730878735" }, { "input": "3333", "output": "938772236" }, { "input": "3555", "output": "810675957" }, { "input": "3789", "output": "397160465" }, { "input": "3999", "output": "124834909" } ]
0
0
-1
19,868
226
Naughty Stone Piles
[ "greedy" ]
null
null
There are *n* piles of stones of sizes *a*1,<=*a*2,<=...,<=*a**n* lying on the table in front of you. During one move you can take one pile and add it to the other. As you add pile *i* to pile *j*, the size of pile *j* increases by the current size of pile *i*, and pile *i* stops existing. The cost of the adding operation equals the size of the added pile. Your task is to determine the minimum cost at which you can gather all stones in one pile. To add some challenge, the stone piles built up conspiracy and decided that each pile will let you add to it not more than *k* times (after that it can only be added to another pile). Moreover, the piles decided to puzzle you completely and told you *q* variants (not necessarily distinct) of what *k* might equal. Your task is to find the minimum cost for each of *q* variants.
The first line contains integer *n* (1<=≀<=*n*<=≀<=105) β€” the number of stone piles. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109) β€” the initial sizes of the stone piles. The third line contains integer *q* (1<=≀<=*q*<=≀<=105) β€” the number of queries. The last line contains *q* space-separated integers *k*1,<=*k*2,<=...,<=*k**q* (1<=≀<=*k**i*<=≀<=105) β€” the values of number *k* for distinct queries. Note that numbers *k**i* can repeat.
Print *q* whitespace-separated integers β€” the answers to the queries in the order, in which the queries are given in the input. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "5\n2 3 4 1 1\n2\n2 3\n" ]
[ "9 8 " ]
In the first sample one way to get the optimal answer goes like this: we add in turns the 4-th and the 5-th piles to the 2-nd one; then we add the 1-st pile to the 3-rd one; we add the 2-nd pile to the 3-rd one. The first two operations cost 1 each; the third one costs 2, the fourth one costs 5 (the size of the 2-nd pile after the first two operations is not 3, it already is 5). In the second sample you can add the 2-nd pile to the 3-rd one (the operations costs 3); then the 1-st one to the 3-th one (the cost is 2); then the 5-th one to the 4-th one (the costs is 1); and at last, the 4-th one to the 3-rd one (the cost is 2).
[ { "input": "5\n2 3 4 1 1\n2\n2 3", "output": "9 8 " }, { "input": "2\n2 9\n5\n4 10 7 3 4", "output": "2 2 2 2 2 " }, { "input": "1\n7\n4\n6 2 3 3", "output": "0 0 0 0 " }, { "input": "2\n7 10\n2\n2 4", "output": "7 7 " }, { "input": "1\n10\n5\n5 3 7 7 1", "output": "0 0 0 0 0 " }, { "input": "1\n2\n5\n7 3 9 8 1", "output": "0 0 0 0 0 " }, { "input": "4\n8 10 4 4\n3\n7 8 1", "output": "16 16 28 " }, { "input": "2\n7 9\n1\n9", "output": "7 " }, { "input": "3\n4 5 4\n2\n10 2", "output": "8 8 " }, { "input": "3\n1 6 8\n1\n6", "output": "7 " }, { "input": "2\n9 3\n1\n6", "output": "3 " }, { "input": "5\n9 5 7 3 3\n1\n3", "output": "21 " }, { "input": "2\n7 4\n1\n7", "output": "4 " }, { "input": "4\n7 4 1 7\n3\n6 8 3", "output": "12 12 12 " }, { "input": "3\n3 7 3\n1\n5", "output": "6 " }, { "input": "1\n3\n1\n2", "output": "0 " }, { "input": "1\n1\n3\n2 1 10", "output": "0 0 0 " } ]
717
12,492,800
3
19,920
718
Sasha and Array
[ "data structures", "math", "matrices" ]
null
null
Sasha has an array of integers *a*1,<=*a*2,<=...,<=*a**n*. You have to perform *m* queries. There might be queries of two types: 1. 1 l r xΒ β€” increase all integers on the segment from *l* to *r* by values *x*; 1. 2 l rΒ β€” find , where *f*(*x*) is the *x*-th Fibonacci number. As this number may be large, you only have to find it modulo 109<=+<=7. In this problem we define Fibonacci numbers as follows: *f*(1)<==<=1, *f*(2)<==<=1, *f*(*x*)<==<=*f*(*x*<=-<=1)<=+<=*f*(*x*<=-<=2) for all *x*<=&gt;<=2. Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?
The first line of the input contains two integers *n* and *m* (1<=≀<=*n*<=≀<=100<=000, 1<=≀<=*m*<=≀<=100<=000)Β β€” the number of elements in the array and the number of queries respectively. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≀<=*a**i*<=≀<=109). Then follow *m* lines with queries descriptions. Each of them contains integers *tp**i*, *l**i*, *r**i* and may be *x**i* (1<=≀<=*tp**i*<=≀<=2, 1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*, 1<=≀<=*x**i*<=≀<=109). Here *tp**i*<==<=1 corresponds to the queries of the first type and *tp**i* corresponds to the queries of the second type. It's guaranteed that the input will contains at least one query of the second type.
For each query of the second type print the answer modulo 109<=+<=7.
[ "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5\n" ]
[ "5\n7\n9\n" ]
Initially, array *a* is equal to 1, 1, 2, 1, 1. The answer for the first query of the second type is *f*(1) + *f*(1) + *f*(2) + *f*(1) + *f*(1) = 1 + 1 + 1 + 1 + 1 = 5. After the query 1 2 4 2 array *a* is equal to 1, 3, 4, 3, 1. The answer for the second query of the second type is *f*(3) + *f*(4) + *f*(3) = 2 + 3 + 2 = 7. The answer for the third query of the second type is *f*(1) + *f*(3) + *f*(4) + *f*(3) + *f*(1) = 1 + 2 + 3 + 2 + 1 = 9.
[ { "input": "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5", "output": "5\n7\n9" }, { "input": "2 3\n1 3\n2 1 1\n1 1 2 3\n1 1 2 2", "output": "1" }, { "input": "7 4\n2 2 1 1 3 3 2\n2 1 5\n2 6 7\n1 3 4 3\n2 6 6", "output": "6\n3\n2" }, { "input": "9 4\n2 1 2 3 3 3 2 1 3\n2 1 8\n1 7 7 3\n1 1 3 1\n1 3 5 2", "output": "11" }, { "input": "18 19\n2 2 2 2 1 1 1 1 1 2 1 2 2 1 2 1 2 2\n2 4 14\n2 16 16\n2 5 8\n1 1 6 2\n2 14 14\n2 1 3\n2 4 5\n2 11 13\n2 18 18\n1 8 15 2\n2 17 18\n2 14 16\n1 3 10 2\n2 1 9\n1 3 18 1\n1 17 18 1\n2 17 17\n1 15 15 1\n1 3 18 2", "output": "11\n1\n4\n1\n9\n5\n3\n1\n2\n6\n44\n3" }, { "input": "18 31\n1 1 2 2 2 1 2 1 2 2 2 2 1 1 1 2 2 1\n1 4 7 2\n1 9 10 1\n1 2 4 2\n1 15 18 2\n1 3 10 2\n1 7 8 1\n1 13 18 2\n1 15 17 2\n1 16 17 1\n1 16 16 2\n1 10 12 1\n1 4 13 2\n1 3 8 1\n1 7 9 1\n1 13 18 2\n1 3 14 2\n1 2 2 1\n1 14 17 1\n1 9 18 1\n1 7 17 1\n1 2 5 1\n1 8 13 2\n1 12 18 1\n2 13 14\n2 15 18\n1 14 14 2\n1 8 14 2\n1 3 11 2\n1 17 17 2\n1 7 10 2\n2 3 12", "output": "466\n2474\n22934" }, { "input": "17 30\n8 10 7 8 7 9 10 6 7 3 7 2 8 8 10 9 8\n1 16 16 5\n2 13 13\n1 16 17 4\n1 1 17 2\n2 17 17\n1 2 10 10\n1 4 8 3\n1 17 17 4\n1 13 15 6\n1 10 16 2\n2 13 14\n2 12 14\n1 6 11 5\n2 4 14\n1 12 16 7\n1 8 8 4\n1 1 16 9\n1 17 17 6\n1 7 8 7\n1 7 10 1\n1 11 14 6\n1 6 10 8\n1 9 14 8\n1 16 16 9\n1 6 13 10\n1 9 13 10\n2 8 8\n1 5 12 6\n1 8 11 10\n1 6 15 7", "output": "21\n377\n5168\n5176\n1584272\n680057396" }, { "input": "1 1\n1\n2 1 1", "output": "1" }, { "input": "1 2\n1000000000\n1 1 1 1000000000\n2 1 1", "output": "999999020" } ]
5,000
70,553,600
0
19,953
27
Ring Road 2
[ "2-sat", "dfs and similar", "dsu", "graphs" ]
D. Ring Road 2
2
256
It is well known that Berland has *n* cities, which form the Silver ring β€” cities *i* and *i*<=+<=1 (1<=≀<=*i*<=&lt;<=*n*) are connected by a road, as well as the cities *n* and 1. The goverment have decided to build *m* new roads. The list of the roads to build was prepared. Each road will connect two cities. Each road should be a curve which lies inside or outside the ring. New roads will have no common points with the ring (except the endpoints of the road). Now the designers of the constructing plan wonder if it is possible to build the roads in such a way that no two roads intersect (note that the roads may intersect at their endpoints). If it is possible to do, which roads should be inside the ring, and which should be outside?
The first line contains two integers *n* and *m* (4<=≀<=*n*<=≀<=100,<=1<=≀<=*m*<=≀<=100). Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=*a**i*<=β‰ <=*b**i*). No two cities will be connected by more than one road in the list. The list will not contain the roads which exist in the Silver ring.
If it is impossible to build the roads in such a way that no two roads intersect, output Impossible. Otherwise print *m* characters. *i*-th character should be i, if the road should be inside the ring, and o if the road should be outside the ring. If there are several solutions, output any of them.
[ "4 2\n1 3\n2 4\n", "6 3\n1 3\n3 5\n5 1\n" ]
[ "io\n", "ooo\n" ]
none
[ { "input": "4 1\n4 2", "output": "o" }, { "input": "4 2\n1 3\n2 4", "output": "io" }, { "input": "5 1\n3 5", "output": "o" }, { "input": "5 2\n2 4\n4 1", "output": "oo" }, { "input": "5 3\n4 2\n1 3\n5 2", "output": "oio" }, { "input": "5 4\n1 3\n3 5\n1 4\n2 4", "output": "iioo" }, { "input": "6 1\n6 2", "output": "o" }, { "input": "6 2\n3 5\n2 4", "output": "oi" }, { "input": "6 3\n5 1\n4 6\n3 1", "output": "oio" }, { "input": "6 4\n6 3\n1 3\n6 4\n5 3", "output": "oooi" }, { "input": "6 5\n5 3\n4 1\n2 6\n5 1\n5 2", "output": "ioioi" }, { "input": "6 6\n1 4\n4 6\n2 6\n2 4\n5 3\n6 3", "output": "iioioo" }, { "input": "7 1\n4 7", "output": "o" }, { "input": "7 3\n2 7\n1 5\n5 7", "output": "oio" }, { "input": "7 5\n3 7\n5 7\n6 4\n4 1\n3 1", "output": "iiooo" }, { "input": "7 8\n4 7\n3 5\n7 2\n2 4\n4 6\n1 5\n1 3\n7 5", "output": "oioooiii" }, { "input": "5 4\n2 4\n2 5\n3 1\n5 3", "output": "iioo" }, { "input": "10 4\n6 9\n10 2\n4 8\n2 9", "output": "ooio" }, { "input": "17 10\n4 11\n7 16\n8 5\n12 15\n7 4\n1 12\n11 1\n15 1\n7 14\n2 9", "output": "Impossible" }, { "input": "100 50\n79 24\n65 69\n44 6\n49 53\n26 22\n24 76\n10 2\n19 44\n44 10\n87 82\n91 87\n23 92\n69 66\n95 30\n90 24\n95 20\n76 36\n82 24\n63 80\n70 72\n34 76\n42 40\n17 100\n66 64\n85 96\n34 30\n36 69\n45 56\n36 62\n26 38\n31 28\n79 64\n78 69\n41 62\n36 40\n64 67\n21 13\n90 88\n28 30\n59 6\n59 56\n63 1\n24 91\n12 19\n100 80\n98 95\n75 78\n54 44\n81 97\n20 38", "output": "Impossible" }, { "input": "20 14\n18 3\n15 12\n8 15\n17 7\n16 14\n15 7\n18 5\n6 17\n4 18\n4 2\n20 2\n9 19\n20 7\n16 18", "output": "ooooiooooioiii" }, { "input": "10 7\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9", "output": "ooooooo" }, { "input": "6 3\n1 4\n2 5\n3 6", "output": "Impossible" }, { "input": "7 9\n4 7\n3 5\n7 2\n2 4\n4 6\n1 5\n1 3\n7 5\n2 5", "output": "Impossible" } ]
280
0
0
19,979
767
Cartons of milk
[ "binary search", "data structures", "greedy", "sortings", "two pointers" ]
null
null
Olya likes milk very much. She drinks *k* cartons of milk each day if she has at least *k* and drinks all of them if she doesn't. But there's an issueΒ β€” expiration dates. Each carton has a date after which you can't drink it (you still can drink it exactly at the date written on the carton). Due to this, if Olya's fridge contains a carton past its expiry date, she throws it away. Olya hates throwing out cartons, so when she drinks a carton, she chooses the one which expires the fastest. It's easy to understand that this strategy minimizes the amount of cartons thrown out and lets her avoid it if it's even possible. The main issue Olya has is the one of buying new cartons. Currently, there are *n* cartons of milk in Olya's fridge, for each one an expiration date is known (how soon does it expire, measured in days). In the shop that Olya visited there are *m* cartons, and the expiration date is known for each of those cartons as well. Find the maximum number of cartons Olya can buy so that she wouldn't have to throw away any cartons. Assume that Olya drank no cartons today.
In the first line there are three integers *n*, *m*, *k* (1<=≀<=*n*,<=*m*<=≀<=106, 1<=≀<=*k*<=≀<=*n*<=+<=*m*)Β β€” the amount of cartons in Olya's fridge, the amount of cartons in the shop and the number of cartons Olya drinks each day. In the second line there are *n* integers *f*1,<=*f*2,<=...,<=*f**n* (0<=≀<=*f**i*<=≀<=107)Β β€” expiration dates of the cartons in Olya's fridge. The expiration date is expressed by the number of days the drinking of this carton can be delayed. For example, a 0 expiration date means it must be drunk today, 1Β β€” no later than tomorrow, etc. In the third line there are *m* integers *s*1,<=*s*2,<=...,<=*s**m* (0<=≀<=*s**i*<=≀<=107)Β β€” expiration dates of the cartons in the shop in a similar format.
If there's no way for Olya to drink the cartons she already has in her fridge, print -1. Otherwise, in the first line print the maximum number *x* of cartons which Olya can buy so that she wouldn't have to throw a carton away. The next line should contain exactly *x* integersΒ β€” the numbers of the cartons that should be bought (cartons are numbered in an order in which they are written in the input, starting with 1). Numbers should not repeat, but can be in arbitrary order. If there are multiple correct answers, print any of them.
[ "3 6 2\n1 0 1\n2 0 2 0 0 2\n", "3 1 2\n0 0 0\n1\n", "2 1 2\n0 1\n0\n" ]
[ "3\n1 2 3", "-1", "1\n1 " ]
In the first example *k* = 2 and Olya has three cartons with expiry dates 0, 1 and 1 (they expire today, tomorrow and tomorrow), and the shop has 3 cartons with expiry date 0 and 3 cartons with expiry date 2. Olya can buy three cartons, for example, one with the expiry date 0 and two with expiry date 2. In the second example all three cartons Olya owns expire today and it means she would have to throw packets away regardless of whether she buys an extra one or not. In the third example Olya would drink *k* = 2 cartons today (one she alreay has in her fridge and one from the shop) and the remaining one tomorrow.
[]
46
0
0
19,985
997
Cycles in product
[ "combinatorics", "divide and conquer", "trees" ]
null
null
Consider a tree (that is, an undirected connected graph without loops) $T_1$ and a tree $T_2$. Let's define their cartesian product $T_1 \times T_2$ in a following way. Let $V$ be the set of vertices in $T_1$ and $U$ be the set of vertices in $T_2$. Then the set of vertices of graph $T_1 \times T_2$ is $V \times U$, that is, a set of ordered pairs of vertices, where the first vertex in pair is from $V$ and the secondΒ β€” from $U$. Let's draw the following edges: - Between $(v, u_1)$ and $(v, u_2)$ there is an undirected edge, if $u_1$ and $u_2$ are adjacent in $U$. - Similarly, between $(v_1, u)$ and $(v_2, u)$ there is an undirected edge, if $v_1$ and $v_2$ are adjacent in $V$. Please see the notes section for the pictures of products of trees in the sample tests. Let's examine the graph $T_1 \times T_2$. How much cycles (not necessarily simple) of length $k$ it contains? Since this number can be very large, print it modulo $998244353$. The sequence of vertices $w_1$, $w_2$, ..., $w_k$, where $w_i \in V \times U$ called cycle, if any neighboring vertices are adjacent and $w_1$ is adjacent to $w_k$. Cycles that differ only by the cyclic shift or direction of traversal are still considered different.
First line of input contains three integersΒ β€” $n_1$, $n_2$ and $k$ ($2 \le n_1, n_2 \le 4000$, $2 \le k \le 75$)Β β€” number of vertices in the first tree, number of vertices in the second tree and the cycle length respectively. Then follow $n_1 - 1$ lines describing the first tree. Each of this lines contains two integersΒ β€” $v_i, u_i$ ($1 \le v_i, u_i \le n_1$), which define edges of the first tree. Then follow $n_2 - 1$ lines, which describe the second tree in the same format. It is guaranteed, that given graphs are trees.
Print one integerΒ β€” number of cycles modulo $998244353$.
[ "2 2 2\n1 2\n1 2\n", "2 2 4\n1 2\n1 2\n", "2 3 4\n1 2\n1 2\n1 3\n", "4 2 2\n1 2\n1 3\n1 4\n1 2\n" ]
[ "8\n", "32\n", "70\n", "20\n" ]
The following three pictures illustrate graph, which are products of the trees from sample tests. In the first example, the list of cycles of length $2$ is as follows: - Β«ABΒ», Β«BAΒ» - Β«BCΒ», Β«CBΒ» - Β«ADΒ», Β«DAΒ» - Β«CDΒ», Β«DCΒ»
[]
77
0
0
20,012
348
Apple Tree
[ "dfs and similar", "number theory", "trees" ]
null
null
You are given a rooted tree with *n* vertices. In each leaf vertex there's a single integer β€” the number of apples in this vertex. The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf. A tree is balanced if for every vertex *v* of the tree all its subtrees, corresponding to the children of vertex *v*, are of equal weight. Count the minimum number of apples that you need to remove from the tree (specifically, from some of its leaves) in order to make the tree balanced. Notice that you can always achieve the goal by just removing all apples.
The first line contains integer *n* (2<=≀<=*n*<=≀<=105), showing the number of vertices in the tree. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≀<=*a**i*<=≀<=108), *a**i* is the number of apples in the vertex number *i*. The number of apples in non-leaf vertices is guaranteed to be zero. Then follow *n*<=-<=1 lines, describing the tree edges. Each line contains a pair of integers *x**i*,<=*y**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*,<=*x**i*<=β‰ <=*y**i*) β€” the vertices connected by an edge. The vertices are indexed from 1 to *n*. Vertex 1 is the root.
Print a single integer β€” the minimum number of apples to remove in order to make the tree balanced. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier.
[ "6\n0 0 12 13 5 6\n1 2\n1 3\n1 4\n2 5\n2 6\n" ]
[ "6" ]
none
[]
2,000
17,612,800
0
20,027
113
Sleeping
[ "combinatorics", "implementation", "math" ]
E. Sleeping
2
256
One day Vasya was lying in bed watching his electronic clock to fall asleep quicker. Vasya lives in a strange country, where days have *h* hours, and every hour has *m* minutes. Clock shows time in decimal number system, in format H:M, where the string H always has a fixed length equal to the number of digits in the decimal representation of number *h*<=-<=1. To achieve this, leading zeros are added if necessary. The string M has a similar format, and its length is always equal to the number of digits in the decimal representation of number *m*<=-<=1. For example, if *h*<==<=17, *m*<==<=1000, then time equal to 13 hours and 75 minutes will be displayed as "13:075". Vasya had been watching the clock from *h*1 hours *m*1 minutes to *h*2 hours *m*2 minutes inclusive, and then he fell asleep. Now he asks you to count how many times he saw the moment at which at least *k* digits changed on the clock simultaneously. For example, when switching 04:19 <=β†’<= 04:20 two digits change. When switching 23:59 <=β†’<= 00:00, four digits change. Consider that Vasya has been watching the clock for strictly less than one day. Note that the last time Vasya saw on the clock before falling asleep was "h2:m2". That is, Vasya didn't see the moment at which time "h2:m2" switched to the next value.
The first line of the input file contains three space-separated integers *h*, *m* and *k* (2<=≀<=*h*,<=*m*<=≀<=109, 1<=≀<=*k*<=≀<=20). The second line contains space-separated integers *h*1, *m*1 (0<=≀<=*h*1<=&lt;<=*h*, 0<=≀<=*m*1<=&lt;<=*m*). The third line contains space-separated integers *h*2, *m*2 (0<=≀<=*h*2<=&lt;<=*h*, 0<=≀<=*m*2<=&lt;<=*m*).
Print a single number β€” the number of times Vasya saw the moment of changing at least *k* digits simultaneously. Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin stream (also you may use the %I64d specificator).
[ "5 5 2\n4 4\n2 1\n", "24 60 1\n0 0\n23 59\n", "24 60 3\n23 59\n23 59\n" ]
[ "3\n", "1439\n", "0\n" ]
In the first example Vasya will see the following moments of time: 4:4 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 0:0  →  0:1  →  0:2  →  0:3  →  0:4 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1:0  →  1:1  →  1:2  →  1:3  →  1:4 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2:0  →  2:1  →  2:2  →  2:3  →  2:4. Double arrow (<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/>) marks the sought moments of time (in this example β€” when Vasya sees two numbers changing simultaneously). In the second example *k* = 1. Any switching time can be accepted, since during switching of the clock at least one digit is changed. Total switching equals to 24Β·60 = 1440, but Vasya have not seen one of them β€” the switching of 23:59 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 00:00. In the third example Vasya fell asleep immediately after he began to look at the clock, so he did not see any change.
[]
0
0
-1
20,076
348
Subset Sums
[ "brute force", "data structures" ]
null
null
You are given an array *a*1,<=*a*2,<=...,<=*a**n* and *m* sets *S*1,<=*S*2,<=...,<=*S**m* of indices of elements of this array. Let's denote *S**k*<==<={*S**k*,<=*i*}Β (1<=≀<=*i*<=≀<=|*S**k*|). In other words, *S**k*,<=*i* is some element from set *S**k*. In this problem you have to answer *q* queries of the two types: 1. Find the sum of elements with indices from set *S**k*: . The query format is "? k". 1. Add number *x* to all elements at indices from set *S**k*: *a**S**k*,<=*i* is replaced by *a**S**k*,<=*i*<=+<=*x* for all *i* (1<=≀<=*i*<=≀<=|*S**k*|). The query format is "+ k x". After each first type query print the required sum.
The first line contains integers *n*,<=*m*,<=*q* (1<=≀<=*n*,<=*m*,<=*q*<=≀<=105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=≀<=108) β€” elements of array *a*. Each of the following *m* lines describes one set of indices. The *k*-th line first contains a positive integer, representing the number of elements in set (|*S**k*|), then follow |*S**k*| distinct integers *S**k*,<=1,<=*S**k*,<=2,<=...,<=*S**k*,<=|*S**k*| (1<=≀<=*S**k*,<=*i*<=≀<=*n*) β€” elements of set *S**k*. The next *q* lines contain queries. Each query looks like either "? k" or "+ k x" and sits on a single line. For all queries the following limits are held: 1<=≀<=*k*<=≀<=*m*, |*x*|<=≀<=108. The queries are given in order they need to be answered. It is guaranteed that the sum of sizes of all sets *S**k* doesn't exceed 105.
After each first type query print the required sum on a single line. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
[ "5 3 5\n5 -5 5 1 -4\n2 1 2\n4 2 1 4 5\n2 2 5\n? 2\n+ 3 4\n? 1\n+ 2 1\n? 2\n" ]
[ "-3\n4\n9\n" ]
none
[ { "input": "5 3 5\n5 -5 5 1 -4\n2 1 2\n4 2 1 4 5\n2 2 5\n? 2\n+ 3 4\n? 1\n+ 2 1\n? 2", "output": "-3\n4\n9" }, { "input": "10 10 10\n0 0 0 1 -5 8 7 2 9 -2\n8 1 9 7 8 2 5 10 3\n2 7 10\n8 8 7 1 4 2 5 10 9\n2 7 5\n8 9 3 6 4 8 1 2 10\n2 9 2\n8 4 2 9 1 10 7 5 3\n2 3 6\n8 2 8 5 9 4 1 6 10\n2 5 2\n? 1\n? 3\n? 5\n? 4\n+ 9 0\n+ 2 -5\n+ 4 7\n+ 5 -1\n? 2\n? 7", "output": "11\n12\n18\n2\n1\n8" }, { "input": "1 1 1\n0\n1 1\n? 1", "output": "0" }, { "input": "1 1 1\n100000000\n1 1\n? 1", "output": "100000000" } ]
154
2,867,200
-1
20,110
455
A Lot of Games
[ "dfs and similar", "dp", "games", "implementation", "strings", "trees" ]
null
null
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players. Given a group of *n* non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move. Andrew and Alex decided to play this game *k* times. The player who is the loser of the *i*-th game makes the first move in the (*i*<=+<=1)-th game. Guys decided that the winner of all games is the player who wins the last (*k*-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him.
The first line contains two integers, *n* and *k* (1<=≀<=*n*<=≀<=105; 1<=≀<=*k*<=≀<=109). Each of the next *n* lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.
If the player who moves first wins, print "First", otherwise print "Second" (without the quotes).
[ "2 3\na\nb\n", "3 1\na\nb\nc\n", "1 2\nab\n" ]
[ "First\n", "First\n", "Second\n" ]
none
[ { "input": "2 3\na\nb", "output": "First" }, { "input": "3 1\na\nb\nc", "output": "First" }, { "input": "1 2\nab", "output": "Second" }, { "input": "5 6\nabas\ndsfdf\nabacaba\ndartsidius\nkolobok", "output": "Second" }, { "input": "4 2\naaaa\nbbbb\nccccc\ndumbavumba", "output": "First" }, { "input": "3 8\nso\nbad\ntest", "output": "First" }, { "input": "5 2\nwelcome\nto\nthe\nmatrix\nneo", "output": "First" }, { "input": "6 4\ndog\ncat\ncow\nhot\nice\nlol", "output": "Second" }, { "input": "4 8\nla\na\nz\nka", "output": "First" }, { "input": "3 2\nop\nhop\ncop", "output": "First" }, { "input": "3 3\nabasdfabab\nabaaasdfdsf\nasdfaba", "output": "Second" }, { "input": "2 2\naba\naa", "output": "Second" }, { "input": "4 1\naa\naba\nba\nbba", "output": "Second" }, { "input": "1 3\nab", "output": "Second" }, { "input": "3 3\naa\nabb\ncc", "output": "Second" } ]
46
0
0
20,134
91
Queue
[ "binary search", "data structures" ]
B. Queue
2
256
There are *n* walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the *n*-th walrus stands at the beginning of the queue. The *i*-th walrus has the age equal to *a**i*. The *i*-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such *j* (*i*<=&lt;<=*j*), that *a**i*<=&gt;<=*a**j*. The displeasure of the *i*-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the *i*-th one. That is, the further that young walrus stands from him, the stronger the displeasure is. The airport manager asked you to count for each of *n* walruses in the queue his displeasure.
The first line contains an integer *n* (2<=≀<=*n*<=≀<=105) β€” the number of walruses in the queue. The second line contains integers *a**i* (1<=≀<=*a**i*<=≀<=109). Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one.
Print *n* numbers: if the *i*-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the *i*-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus.
[ "6\n10 8 5 3 50 45\n", "7\n10 4 6 3 2 8 15\n", "5\n10 3 1 10 11\n" ]
[ "2 1 0 -1 0 -1 ", "4 2 1 0 -1 -1 -1 ", "1 0 -1 -1 -1 " ]
none
[ { "input": "6\n10 8 5 3 50 45", "output": "2 1 0 -1 0 -1 " }, { "input": "7\n10 4 6 3 2 8 15", "output": "4 2 1 0 -1 -1 -1 " }, { "input": "5\n10 3 1 10 11", "output": "1 0 -1 -1 -1 " }, { "input": "13\n18 9 8 9 23 20 18 18 33 25 31 37 36", "output": "2 0 -1 -1 2 1 -1 -1 1 -1 -1 0 -1 " }, { "input": "10\n15 21 17 22 27 21 31 26 32 30", "output": "-1 0 -1 1 2 -1 2 -1 0 -1 " }, { "input": "10\n18 20 18 17 17 13 22 20 34 29", "output": "4 3 2 1 0 -1 0 -1 0 -1 " }, { "input": "13\n16 14 12 9 11 28 30 21 35 30 32 31 43", "output": "3 2 1 -1 -1 1 0 -1 2 -1 0 -1 -1 " }, { "input": "15\n18 6 18 21 14 20 13 9 18 20 28 13 19 25 21", "output": "10 -1 8 8 6 6 0 -1 2 2 3 -1 -1 0 -1 " }, { "input": "11\n15 17 18 18 26 22 23 33 33 21 29", "output": "-1 -1 -1 -1 4 3 2 2 1 -1 -1 " }, { "input": "15\n14 4 5 12 6 19 14 19 12 22 23 17 14 21 27", "output": "7 -1 -1 0 -1 6 1 4 -1 3 2 0 -1 -1 -1 " }, { "input": "2\n1 1000000000", "output": "-1 -1 " }, { "input": "2\n1000000000 1", "output": "0 -1 " }, { "input": "5\n15 1 8 15 3", "output": "3 -1 1 0 -1 " }, { "input": "12\n5 1 2 5 100 1 1000 100 10000 20000 10000 20000", "output": "4 -1 2 1 0 -1 0 -1 -1 0 -1 -1 " } ]
124
0
0
20,175
53
Blog Photo
[ "binary search", "implementation" ]
B. Blog Photo
2
256
One popular blog site edits the uploaded photos like this. It cuts a rectangular area out of them so that the ratio of height to width (i.e. the *height*<=/<=*width* quotient) can vary from 0.8 to 1.25 inclusively. Besides, at least one side of the cut area should have a size, equal to some power of number 2 (2*x* for some integer *x*). If those rules don't indicate the size of the cut are clearly, then the way with which the cut part possesses the largest area is chosen. Of course, both sides of the cut area should be integer. If there are several answers to this problem, you should choose the answer with the maximal height.
The first line contains a pair of integers *h* and *w* (1<=≀<=*h*,<=*w*<=≀<=109) which are the height and width of the uploaded photo in pixels.
Print two integers which are the height and width of the cut area.
[ "2 1\n", "2 2\n", "5 5\n" ]
[ "1 1\n", "2 2\n", "5 4\n" ]
none
[ { "input": "2 1", "output": "1 1" }, { "input": "2 2", "output": "2 2" }, { "input": "5 5", "output": "5 4" }, { "input": "9 10", "output": "8 10" }, { "input": "15 13", "output": "10 8" }, { "input": "47 46", "output": "40 32" }, { "input": "99 100", "output": "80 64" }, { "input": "939 887", "output": "640 512" }, { "input": "4774 4806", "output": "4096 4806" }, { "input": "39271 49032", "output": "32768 40960" }, { "input": "483242 484564", "output": "327680 262144" }, { "input": "4939191 4587461", "output": "4939191 4194304" }, { "input": "9909199 9945873", "output": "8388608 9945873" }, { "input": "49829224 49889315", "output": "41943040 33554432" }, { "input": "49728622 49605627", "output": "41943040 33554432" }, { "input": "49934587 49239195", "output": "41943040 33554432" }, { "input": "48298903 49928606", "output": "41943040 33554432" }, { "input": "49874820 49474021", "output": "41943040 33554432" }, { "input": "48945079 49798393", "output": "41943040 33554432" }, { "input": "99692141 99232337", "output": "83886080 67108864" }, { "input": "998557701 924591072", "output": "671088640 536870912" }, { "input": "644590722 593296648", "output": "644590722 536870912" }, { "input": "792322809 775058858", "output": "671088640 536870912" }, { "input": "971840165 826141527", "output": "671088640 536870912" }, { "input": "944976601 976175854", "output": "671088640 536870912" }, { "input": "1000000000 1000000000", "output": "671088640 536870912" } ]
186
0
3.9535
20,278
425
Sereja and Table
[ "bitmasks", "greedy" ]
null
null
Sereja has an *n*<=Γ—<=*m* rectangular table *a*, each cell of the table contains a zero or a number one. Sereja wants his table to meet the following requirement: each connected component of the same values forms a rectangle with sides parallel to the sides of the table. Rectangles should be filled with cells, that is, if a component form a rectangle of size *h*<=Γ—<=*w*, then the component must contain exactly *hw* cells. A connected component of the same values is a set of cells of the table that meet the following conditions: - every two cells of the set have the same value; - the cells of the set form a connected region on the table (two cells are connected if they are adjacent in some row or some column of the table); - it is impossible to add any cell to the set unless we violate the two previous conditions. Can Sereja change the values of at most *k* cells of the table so that the table met the described requirement? What minimum number of table cells should he change in this case?
The first line contains integers *n*, *m* and *k* (1<=≀<=*n*,<=*m*<=≀<=100;Β 1<=≀<=*k*<=≀<=10). Next *n* lines describe the table *a*: the *i*-th of them contains *m* integers *a**i*1,<=*a**i*2,<=...,<=*a**im* (0<=≀<=*a**i*,<=*j*<=≀<=1) β€” the values in the cells of the *i*-th row.
Print -1, if it is impossible to meet the requirement. Otherwise, print the minimum number of cells which should be changed.
[ "5 5 2\n1 1 1 1 1\n1 1 1 1 1\n1 1 0 1 1\n1 1 1 1 1\n1 1 1 1 1\n", "3 4 1\n1 0 0 0\n0 1 1 1\n1 1 1 0\n", "3 4 1\n1 0 0 1\n0 1 1 0\n1 0 0 1\n" ]
[ "1\n", "-1\n", "0\n" ]
none
[ { "input": "5 5 2\n1 1 1 1 1\n1 1 1 1 1\n1 1 0 1 1\n1 1 1 1 1\n1 1 1 1 1", "output": "1" }, { "input": "3 4 1\n1 0 0 0\n0 1 1 1\n1 1 1 0", "output": "-1" }, { "input": "3 4 1\n1 0 0 1\n0 1 1 0\n1 0 0 1", "output": "0" }, { "input": "8 1 4\n0\n0\n0\n1\n0\n1\n1\n0", "output": "0" }, { "input": "3 10 7\n0 1 0 0 1 0 1 0 0 0\n0 0 1 1 0 0 0 1 0 1\n1 0 1 1 1 0 1 1 0 0", "output": "6" }, { "input": "4 9 7\n0 0 0 1 0 1 1 0 0\n1 1 1 0 0 0 0 1 1\n1 1 0 0 1 1 0 1 0\n0 0 0 1 0 1 0 0 0", "output": "5" }, { "input": "9 2 5\n0 1\n0 1\n1 1\n0 1\n0 1\n1 0\n1 1\n1 0\n1 1", "output": "3" }, { "input": "10 7 8\n1 0 1 0 1 1 0\n0 1 0 1 0 0 1\n1 0 1 0 1 1 0\n0 1 0 1 0 0 1\n1 0 1 0 1 1 0\n1 0 1 0 1 1 0\n1 0 1 0 1 1 0\n1 0 1 0 1 1 0\n0 1 0 1 0 0 1\n0 1 0 1 0 0 1", "output": "0" }, { "input": "9 2 10\n1 0\n0 1\n1 0\n1 1\n0 1\n1 0\n1 0\n1 1\n0 1", "output": "2" }, { "input": "4 6 3\n1 0 0 1 0 0\n0 1 1 0 1 1\n1 0 0 1 0 0\n0 1 1 0 1 1", "output": "0" }, { "input": "4 4 5\n1 0 1 0\n0 1 0 1\n0 1 0 1\n0 1 0 0", "output": "1" }, { "input": "6 4 10\n0 1 0 0\n1 1 1 0\n0 1 1 0\n0 1 0 0\n0 1 0 0\n0 0 0 0", "output": "4" }, { "input": "1 9 2\n1 0 1 0 0 0 0 1 0", "output": "0" }, { "input": "3 63 4\n0 0 0 0 0 1 0 0 1 0 1 1 0 0 1 1 1 1 1 1 0 0 0 1 0 0 1 1 0 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 1 0 1 1 0 0 1 0 0 0 1 0 1 1 1 1 1\n1 1 0 1 1 0 1 1 0 1 0 0 1 1 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 0 0 0 1 1 0 0 1 1 1 1 1 1 0 1 0 1 0 1 0 0 1 1 0 1 1 1 1 1 0 1 1 0 0\n1 1 1 1 1 0 1 1 0 1 0 0 1 1 0 0 0 0 0 0 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 0 0 0", "output": "-1" }, { "input": "1 40 4\n1 0 0 0 1 1 1 0 1 1 0 0 1 1 0 0 1 1 0 1 1 1 1 0 1 0 0 1 1 0 0 1 0 0 0 1 1 1 1 0", "output": "0" }, { "input": "1 12 7\n0 0 0 1 0 0 1 1 1 1 0 1", "output": "0" }, { "input": "4 35 6\n1 1 0 1 1 0 1 1 1 0 0 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0\n0 0 1 0 0 1 0 0 0 1 0 1 1 0 0 1 0 1 1 0 0 1 1 1 1 0 0 1 0 1 1 1 1 1 1\n1 0 0 1 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0\n0 0 1 0 0 1 0 0 0 1 0 1 1 0 0 1 0 1 1 0 0 1 1 1 1 0 0 1 0 1 1 1 1 1 1", "output": "5" }, { "input": "5 38 9\n0 1 0 0 1 1 0 0 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 1 0 0 0\n0 1 0 0 1 1 0 0 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0 1 1 0 0 0 1 1 0 1 0 0 1 0 0 0\n1 0 1 1 0 0 1 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 0 0 1 1 1 0 1 0 1 1 0 1 1 1\n1 0 1 1 0 0 1 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1\n1 0 1 1 0 0 1 1 0 1 1 1 1 0 0 0 0 1 1 1 0 0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1", "output": "2" }, { "input": "2 75 7\n0 0 1 0 0 0 1 1 0 1 1 1 0 1 1 0 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 1 0 1 1 1 0 0 0 1 1 1 0 0 0 1 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 0 0 1 1 1 1\n1 1 0 1 1 1 0 0 1 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 1 1 1 0 0 1 1 0 1 0 0 0 1 1 1 0 0 0 1 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 1", "output": "4" }, { "input": "21 10 8\n1 1 1 0 0 1 1 1 1 1\n1 1 1 0 0 1 1 1 1 1\n1 1 1 0 0 1 1 1 1 1\n1 1 1 0 0 1 1 1 1 1\n1 1 1 0 0 1 1 1 1 1\n0 0 1 1 1 0 0 0 0 0\n0 0 0 1 1 0 0 0 0 0\n1 1 1 0 0 1 1 1 1 1\n0 0 0 1 1 0 0 0 0 0\n1 1 1 0 0 1 1 1 1 1\n1 1 1 0 0 1 1 1 1 1\n0 0 0 1 1 0 0 0 0 0\n1 0 1 0 0 1 1 1 1 1\n0 1 0 1 1 0 0 0 0 0\n0 0 0 1 1 0 0 0 0 0\n0 0 0 1 1 0 0 0 0 0\n0 0 0 1 1 0 0 0 0 0\n1 1 1 0 1 1 1 1 1 1\n0 0 0 0 1 0 0 0 0 0\n1 1 1 0 1 1 1 1 1 1\n1 1 1 0 0 1 1 1 1 1", "output": "6" }, { "input": "11 9 9\n0 0 0 0 0 0 1 1 0\n0 0 0 0 0 0 1 1 0\n0 0 0 0 0 0 1 0 0\n1 1 1 1 1 1 0 0 1\n1 1 1 1 1 1 0 0 1\n1 1 1 1 1 1 0 0 1\n0 0 0 0 0 0 1 1 0\n0 0 0 0 0 0 1 1 0\n0 0 0 0 0 0 1 1 0\n1 1 1 1 1 1 0 0 1\n0 0 0 0 0 0 1 1 0", "output": "1" }, { "input": "37 4 7\n1 0 0 1\n0 1 0 1\n0 1 1 1\n1 0 0 0\n0 1 1 1\n0 1 1 1\n1 0 1 0\n1 0 0 0\n1 0 0 0\n1 0 0 0\n0 1 1 1\n0 1 1 1\n1 0 0 0\n0 1 1 0\n0 1 1 1\n0 1 1 1\n0 1 1 0\n1 0 0 0\n1 0 0 0\n0 1 1 1\n0 1 1 1\n1 0 0 0\n1 1 1 1\n1 1 1 1\n1 1 0 0\n0 1 1 1\n0 1 0 1\n0 1 1 1\n0 1 1 1\n1 1 0 0\n1 0 0 0\n0 0 1 1\n0 1 1 1\n1 0 0 0\n1 0 0 0\n1 0 0 0\n0 0 0 0", "output": "-1" }, { "input": "1 1 1\n1", "output": "0" }, { "input": "2 2 1\n1 1\n1 0", "output": "1" }, { "input": "3 3 1\n1 1 1\n1 0 1\n1 1 0", "output": "-1" }, { "input": "3 3 2\n1 1 1\n1 0 1\n1 1 0", "output": "2" }, { "input": "9 9 10\n0 0 0 0 0 0 1 0 0\n1 1 1 1 1 1 1 1 1\n0 0 0 0 0 0 0 0 0\n1 1 1 1 1 1 1 1 1\n1 1 1 0 1 1 1 1 1\n1 1 1 1 1 1 1 1 1\n0 0 0 0 1 0 1 0 0\n0 0 0 0 1 0 0 0 0\n0 0 0 0 1 0 0 0 0", "output": "6" }, { "input": "9 9 10\n0 0 0 0 0 0 1 0 1\n1 1 1 1 1 1 1 1 1\n0 0 0 0 0 0 1 0 0\n1 1 1 1 1 0 1 1 1\n1 1 1 0 0 1 1 1 1\n1 1 1 0 1 1 1 1 1\n0 0 1 0 1 0 1 0 0\n0 0 0 0 1 0 0 0 0\n0 0 0 0 1 0 0 0 0", "output": "-1" }, { "input": "10 10 10\n1 0 0 0 0 0 0 0 0 0\n0 1 0 0 0 0 0 0 0 0\n0 0 1 0 0 0 0 0 0 0\n0 0 0 1 0 0 0 0 0 0\n0 0 0 0 1 0 0 0 0 0\n0 0 0 0 0 1 0 0 0 0\n0 0 0 0 0 0 1 0 0 0\n0 0 0 0 0 0 0 1 0 0\n0 0 0 0 0 0 0 0 1 0\n0 0 0 0 0 0 0 0 0 1", "output": "10" }, { "input": "10 10 9\n0 0 0 0 0 0 0 0 0 0\n0 1 0 0 0 0 0 0 0 0\n0 0 1 0 0 0 0 0 0 0\n0 0 0 1 0 0 0 0 0 0\n0 0 0 0 1 0 0 0 0 0\n0 0 0 0 0 1 0 0 0 0\n0 0 0 0 0 0 1 0 0 0\n0 0 0 0 0 0 0 1 0 0\n0 0 0 0 0 0 0 0 1 0\n0 0 0 0 0 0 0 0 0 1", "output": "9" }, { "input": "10 10 8\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 1 0 0 0 0 0 0 0\n0 0 0 1 0 0 0 0 0 0\n0 0 0 0 1 0 0 0 0 0\n0 0 0 0 0 1 0 0 0 0\n0 0 0 0 0 0 1 0 0 0\n0 0 0 0 0 0 0 1 0 0\n0 0 0 0 0 0 0 0 1 0\n0 0 0 0 0 0 0 0 0 1", "output": "8" }, { "input": "10 10 7\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 1 0 0 0 0 0 0\n0 0 0 0 1 0 0 0 0 0\n0 0 0 0 0 1 0 0 0 0\n0 0 0 0 0 0 1 0 0 0\n0 0 0 0 0 0 0 1 0 0\n0 0 0 0 0 0 0 0 1 0\n0 0 0 0 0 0 0 0 0 1", "output": "7" }, { "input": "10 10 6\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 1 0 0 0 0 0\n0 0 0 0 0 1 0 0 0 0\n0 0 0 0 0 0 1 0 0 0\n0 0 0 0 0 0 0 1 0 0\n0 0 0 0 0 0 0 0 1 0\n0 0 0 0 0 0 0 0 0 1", "output": "6" }, { "input": "10 10 1\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 0\n0 0 0 0 0 0 0 0 0 1", "output": "1" }, { "input": "4 4 6\n1 1 1 0\n1 1 0 1\n1 0 1 1\n0 1 1 1", "output": "4" }, { "input": "100 2 10\n0 1\n1 0\n1 0\n1 0\n1 0\n1 0\n1 0\n0 1\n0 1\n1 0\n1 0\n0 1\n1 0\n0 1\n0 1\n1 0\n0 1\n0 1\n1 0\n1 0\n1 0\n0 1\n1 0\n0 1\n1 0\n1 0\n1 0\n0 1\n1 0\n1 0\n1 0\n1 0\n1 0\n1 0\n0 1\n0 1\n0 1\n0 1\n0 1\n1 0\n0 1\n0 1\n1 0\n1 0\n0 1\n0 1\n0 1\n1 0\n1 0\n0 1\n0 1\n0 1\n0 1\n1 0\n0 1\n1 0\n1 0\n0 1\n1 0\n1 0\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1\n1 0\n0 1\n1 0\n1 0\n1 0\n1 0\n1 0\n0 1\n0 1\n0 1\n1 0\n0 1\n1 0\n1 0\n1 0\n0 1\n1 0\n1 0\n1 0\n1 0\n0 1\n0 1\n0 1\n0 1\n1 0\n1 0\n1 0\n0 1\n1 0\n0 1\n0 1\n0 1\n0 1\n1 0", "output": "0" }, { "input": "5 5 5\n0 1 1 1 1\n1 0 1 1 1\n1 1 0 1 1\n1 1 1 0 1\n1 1 1 1 0", "output": "5" }, { "input": "5 5 10\n1 1 1 1 0\n1 1 1 0 1\n1 1 0 1 1\n1 0 1 1 1\n0 1 1 1 1", "output": "5" }, { "input": "5 5 5\n1 1 1 1 0\n1 1 1 0 1\n1 1 0 1 1\n1 0 1 1 1\n0 1 1 1 1", "output": "5" }, { "input": "4 4 4\n0 1 1 1\n1 0 1 1\n1 1 0 1\n1 1 1 0", "output": "4" } ]
108
409,600
3
20,317
747
Winter Is Coming
[ "dp", "greedy", "sortings" ]
null
null
The winter in Berland lasts *n* days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than *k* days at any average air temperature. After *k* days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these *k* days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.
The first line contains two positive integers *n* and *k* (1<=≀<=*n*<=≀<=2Β·105, 0<=≀<=*k*<=≀<=*n*)Β β€” the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than *k* days in total. The second line contains a sequence of *n* integers *t*1,<=*t*2,<=...,<=*t**n* (<=-<=20<=≀<=*t**i*<=≀<=20)Β β€” the average air temperature in the *i*-th winter day.
Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.
[ "4 3\n-5 20 -3 0\n", "4 2\n-5 20 -3 0\n", "10 6\n2 -5 1 3 0 0 -4 -3 1 0\n" ]
[ "2\n", "4\n", "3\n" ]
In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
[ { "input": "4 3\n-5 20 -3 0", "output": "2" }, { "input": "4 2\n-5 20 -3 0", "output": "4" }, { "input": "10 6\n2 -5 1 3 0 0 -4 -3 1 0", "output": "3" }, { "input": "4 4\n-5 20 -3 0", "output": "1" }, { "input": "4 1\n-5 20 -3 0", "output": "-1" }, { "input": "1 0\n-13", "output": "-1" }, { "input": "2 0\n-12 -13", "output": "-1" }, { "input": "3 1\n9 -16 -7", "output": "-1" }, { "input": "5 5\n-15 -10 -20 -19 -14", "output": "1" }, { "input": "7 3\n-2 -14 3 -17 -20 -13 -17", "output": "-1" }, { "input": "10 10\n-9 4 -3 16 -15 12 -12 8 -14 15", "output": "1" }, { "input": "30 9\n12 8 -20 0 11 -17 -11 -6 -2 -18 -19 -19 -18 -12 -17 8 10 -17 10 -9 7 1 -10 -11 -17 -2 -12 -9 -8 6", "output": "-1" }, { "input": "50 3\n6 20 17 19 15 17 3 17 5 16 20 18 9 19 18 18 2 -3 11 11 5 15 4 18 16 16 19 11 20 17 2 1 11 14 18 -8 13 17 19 9 9 20 19 20 19 5 12 19 6 9", "output": "4" }, { "input": "100 50\n-7 -3 9 2 16 -19 0 -10 3 -11 17 7 -7 -10 -14 -14 -7 -15 -15 -8 8 -18 -17 -5 -19 -15 -14 0 8 -3 -19 -13 -3 11 -3 -16 16 -16 -12 -2 -17 7 -16 -14 -10 0 -10 -18 -16 -11 -2 -12 -15 -8 -1 -11 -3 -17 -14 -6 -9 -15 -14 -11 -20 -20 -4 -20 -8 -2 0 -2 -20 17 -17 2 0 1 2 6 -5 -13 -16 -5 -11 0 16 -16 -4 -18 -18 -8 12 8 0 -12 -5 -7 -16 -15", "output": "-1" }, { "input": "10 10\n-3 -3 -3 -3 -3 -3 -3 -3 -3 -4", "output": "1" }, { "input": "10 0\n2 2 2 2 2 2 2 2 2 0", "output": "0" }, { "input": "10 5\n-3 3 -3 3 -3 3 -3 3 -3 3", "output": "10" }, { "input": "17 17\n-16 -19 10 -15 6 -11 -11 2 -17 -3 7 -5 -8 1 -20 -8 -11", "output": "1" }, { "input": "9 8\n12 20 0 19 20 14 7 17 12", "output": "0" }, { "input": "10 10\n-13 -9 -8 -20 -10 -12 -17 7 -15 -16", "output": "1" }, { "input": "15 15\n-14 -15 -8 -12 -10 -20 -14 -2 -1 2 -20 -15 5 -1 -9", "output": "1" }, { "input": "1 1\n11", "output": "0" }, { "input": "14 11\n10 12 9 12 -2 15 1 17 8 17 18 7 10 14", "output": "1" }, { "input": "1 1\n12", "output": "0" }, { "input": "1 1\n-1", "output": "1" }, { "input": "1 0\n1", "output": "0" }, { "input": "1 0\n0", "output": "0" }, { "input": "1 0\n-1", "output": "-1" }, { "input": "2 1\n-1 1", "output": "2" }, { "input": "1 1\n1", "output": "0" }, { "input": "8 3\n14 9 10 1 2 -1 6 13", "output": "1" }, { "input": "3 3\n0 0 0", "output": "0" }, { "input": "11 7\n0 0 -1 -1 0 0 0 -1 -1 0 0", "output": "2" }, { "input": "7 5\n-1 1 1 1 -1 1 1", "output": "2" }, { "input": "3 3\n1 2 3", "output": "0" }, { "input": "5 4\n-1 1 1 -1 1", "output": "2" }, { "input": "3 3\n1 1 1", "output": "0" }, { "input": "5 4\n-1 0 0 -1 0", "output": "2" } ]
202
17,305,600
0
20,369
0
none
[ "none" ]
null
null
Stepan has a set of *n* strings. Also, he has a favorite string *s*. Stepan wants to do the following. He will take some strings of his set and write them down one after another. It is possible that he will take some strings more than once, and will not take some of them at all. Your task is to determine the minimum number of strings in the set which Stepan needs to take and write so that the string *s* appears as a subsequence in the resulting written down string. For example, in the string "abcd" strings "ad", "acd", "abcd" appear as subsequences, and strings "ba", "abdc" don't appear as subsequences.
The first line contains the integer *n* (1<=≀<=*n*<=≀<=50) β€” the number of strings in Stepan's set. The next *n* lines contain *n* non-empty strings consisting of lowercase letters of the English alphabet. The length of each of these strings does not exceed 50 symbols. It is possible that some strings from Stepan's set are the same. The next line contains the non-empty string *s*, consisting of lowercase letters of the English alphabet β€” Stepan's favorite string. The length of this string doesn't exceed 2500 symbols.
Print the minimum number of strings which Stepan should take from the set and write them down one after another so that the string *s* appears as a subsequence in the resulting written down string. Each string from the set should be counted as many times as Stepan takes it from the set. If the answer doesn't exsist, print -1.
[ "3\na\naa\na\naaa\n", "4\nab\naab\naa\nbb\nbaaab\n", "2\naaa\nbbb\naaacbbb\n" ]
[ "2\n", "3\n", "-1\n" ]
In the first test, Stepan can take, for example, the third and the second strings from the set, write them down, and get exactly his favorite string. In the second example Stepan can take, for example, the second, the third and again the second strings from the set and write them down. Then he will get a string "aabaaaab", in which his favorite string "baaab" is a subsequence. In the third test Stepan can not get his favorite string, because it contains the letter "c", which is not presented in any of the strings in the set.
[]
46
0
0
20,482
985
Isomorphic Strings
[ "hashing", "strings" ]
null
null
You are given a string *s* of length *n* consisting of lowercase English letters. For two given strings *s* and *t*, say *S* is the set of distinct characters of *s* and *T* is the set of distinct characters of *t*. The strings *s* and *t* are isomorphic if their lengths are equal and there is a one-to-one mapping (bijection) *f* between *S* and *T* for which *f*(*s**i*)<==<=*t**i*. Formally: 1. *f*(*s**i*)<==<=*t**i* for any index *i*, 1. for any character there is exactly one character that *f*(*x*)<==<=*y*, 1. for any character there is exactly one character that *f*(*x*)<==<=*y*. For example, the strings "aababc" and "bbcbcz" are isomorphic. Also the strings "aaaww" and "wwwaa" are isomorphic. The following pairs of strings are not isomorphic: "aab" and "bbb", "test" and "best". You have to handle *m* queries characterized by three integers *x*,<=*y*,<=*len* (1<=≀<=*x*,<=*y*<=≀<=*n*<=-<=*len*<=+<=1). For each query check if two substrings *s*[*x*... *x*<=+<=*len*<=-<=1] and *s*[*y*... *y*<=+<=*len*<=-<=1] are isomorphic.
The first line contains two space-separated integers *n* and *m* (1<=≀<=*n*<=≀<=2Β·105, 1<=≀<=*m*<=≀<=2Β·105) β€” the length of the string *s* and the number of queries. The second line contains string *s* consisting of *n* lowercase English letters. The following *m* lines contain a single query on each line: *x**i*, *y**i* and *len**i* (1<=≀<=*x**i*,<=*y**i*<=≀<=*n*, 1<=≀<=*len**i*<=≀<=*n*<=-<=*max*(*x**i*,<=*y**i*)<=+<=1) β€” the description of the pair of the substrings to check.
For each query in a separate line print "YES" if substrings *s*[*x**i*... *x**i*<=+<=*len**i*<=-<=1] and *s*[*y**i*... *y**i*<=+<=*len**i*<=-<=1] are isomorphic and "NO" otherwise.
[ "7 4\nabacaba\n1 1 1\n1 4 2\n2 1 3\n2 4 3\n" ]
[ "YES\nYES\nNO\nYES\n" ]
The queries in the example are following: 1. substrings "a" and "a" are isomorphic: *f*(*a*) = *a*; 1. substrings "ab" and "ca" are isomorphic: *f*(*a*) = *c*, *f*(*b*) = *a*; 1. substrings "bac" and "aba" are not isomorphic since *f*(*b*) and *f*(*c*) must be equal to *a* at same time; 1. substrings "bac" and "cab" are isomorphic: *f*(*b*) = *c*, *f*(*a*) = *a*, *f*(*c*) = *b*.
[ { "input": "7 4\nabacaba\n1 1 1\n1 4 2\n2 1 3\n2 4 3", "output": "YES\nYES\nNO\nYES" }, { "input": "1 2\nz\n1 1 1\n1 1 1", "output": "YES\nYES" }, { "input": "36 4\naababcbbcbczaaawwwwwaaaabbbbtestbest\n1 7 6\n13 18 5\n23 26 3\n29 33 4", "output": "YES\nYES\nNO\nNO" }, { "input": "4 1\nabac\n1 2 3", "output": "NO" }, { "input": "5 10\ncaabc\n5 4 1\n5 4 1\n1 5 1\n4 2 2\n1 3 1\n5 5 1\n4 4 1\n4 3 2\n1 4 1\n5 1 1", "output": "YES\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES\nYES" }, { "input": "10 30\naccbaaccac\n6 8 3\n2 9 1\n6 8 3\n10 5 1\n6 2 2\n1 4 7\n3 10 1\n6 9 1\n7 7 3\n7 3 2\n8 4 1\n4 9 1\n7 6 4\n6 8 2\n10 2 1\n1 3 5\n5 10 1\n7 10 1\n4 1 2\n8 5 3\n9 8 1\n8 6 2\n6 9 2\n10 3 1\n3 9 1\n5 5 5\n1 5 6\n4 2 6\n10 7 1\n9 8 2", "output": "NO\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nYES\nYES" }, { "input": "64 1\nabbbbbbaababaaabbabbaabababbabbabbbbbbaabbaaabbbabbbaaaabaaabaaa\n1 33 32", "output": "NO" }, { "input": "64 1\naabbbabaabaaaababbaabbbabbaaababababbbbaabaaaaababbaaababbbabbab\n1 33 32", "output": "NO" }, { "input": "64 1\nbbabaabbabbaaaaabaaabbbbbbbbbbabbaaabbbbbbbbabaabaababaaabbabaaa\n1 33 32", "output": "NO" }, { "input": "64 1\nbbbbaababaabbbabbaaaabbaaaaaaaaaaaaabbabbbbbbaababbabbababbbabaa\n1 33 32", "output": "NO" }, { "input": "64 1\nababbaaabbaabaababbbbbbbbbaabbaabaaabbaabbaaabbabaabaabbaababbba\n1 33 32", "output": "NO" }, { "input": "64 1\nbaabbaababbbbbaaaaaaabaabbbbbbbaaabbbaaaaaababbabbaaabbbbabaaaab\n1 33 32", "output": "NO" }, { "input": "64 1\naabbaabababbaababababbabbabaaabbbbbaaaababbaaabbbaabbaaaabbaaaba\n1 33 32", "output": "NO" }, { "input": "64 1\naaaaaaabaabaabaababbbaabbabbabaabbbbaabaaabbabbaabaaabbabbbbaaba\n1 33 32", "output": "NO" }, { "input": "40 1\nbabbaaaabaabbbaabbaaabbbabbbbbaaaabaaaab\n1 21 20", "output": "NO" }, { "input": "80 1\nabaabbbbaaaaabbabbaaaabbaabaaaaababababaaabaabaaaaababbabbaaaaabbbabbbbaaaaabbbb\n1 41 40", "output": "NO" }, { "input": "24 1\nccacbbabcacabababaacaacc\n1 13 12", "output": "NO" }, { "input": "80 1\naaaabaaaaababbbbbbaabbbabbbbbbaaaaabaaaaabbabbbababaabbbbbbabbababbbbabbaabbabbb\n1 41 40", "output": "NO" }, { "input": "80 1\naaabbbaababbabbbbaabbaabbbaaabbabaabaaabbabaaaaabbbbbaaabaaabbbabaaababbbbbbabba\n1 41 40", "output": "NO" }, { "input": "80 1\naabbbbbaaabaaabbabaabaababbabbbbabbaaabababbabaaaaabbaaaabaabaabbaaaaabaaaabaaba\n1 41 40", "output": "NO" } ]
93
716,800
-1
20,519