contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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... | 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*)... | 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, ... | [
{
"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... | 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 eleme... | 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**... | 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"
},
{
"i... | 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 ... | 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... | 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*β=... | [
{
"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... | 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... | 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/e5a4a1705... | [] | 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 pu... | 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 guarantee... | 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",
... | 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 nam... | 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... | 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... | [
{
"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\nSlyth... | 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 follo... | 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,... | 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 damag... | [
{
"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"
},
{... | 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*<=<<=*r*<=β€<=*n* and sequence *b*<==<=*a*1*a*2... *a**l**a**r**a**... | 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*... | 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... | 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 ea... | 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$... | 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 capacit... | [] | 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... | 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... | [] | 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<=β€<... | 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"... | [
{
"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": "4000... | 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-worki... | 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 ... | [
"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"
},... | 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<=><=*a*2<=><=...<=><=*a**k*<=><=0 stones. The piles should meet the condition *a*1<=-<=... | 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"... | 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.... | 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"
},
{
... | 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 cha... | 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 wit... | 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\nP... | 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 s... | 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)... | 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 s... | [] | 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 jea... | 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 wei... | 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... | [] | 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 threa... | 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 dist... | 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 thi... | [
{
"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"
},
... | 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*, whe... | 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 let... | 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), an... | [
{
"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"
},
{
"inpu... | 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 pos... | 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 e... | 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"
},
{
"in... | 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 *... | 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"... | 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*<=><=*r*, then you swap values of *l* and *r*. You have to calculate the expected value of the number of unique elemen... | 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... | 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 ... | 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 fol... | 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 a... | [
"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 ... | 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 ... | 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* c... | 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",
... | 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... | 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*<=β ... | 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 ... | 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 ... | 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",
... | 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 onl... | 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... | 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... | 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:
- <SAE> ::= <Number> | <SAE>+<SAE> | <SAE>*<SAE> | (<SAE>) - <Number> ::= <Digit> | <Digit><Number> - <Digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 |... | 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<=β€<... | 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 proce... | 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":... | 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 ... | 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<=β€<=*... | 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":... | 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 ... | 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. Th... | 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": ... | 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*<=><=1 vertex *u* is a *k*-ary heap of depth *m* if at least *k* of its children are *k*-ary heaps of depth a... | 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... | [] | 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**... | 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 t... | [
"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"
... | 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 a... | 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 o... | 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 7... | 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... | 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,... | 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).
... | 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 cyc... | [
"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\na... | 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 beaut... | 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"
},
{
"inpu... | 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... | 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 o... | 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="... | [
{
"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"... | 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 throu... | 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... | 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 ... | 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*<=<<=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": "35... | 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 div... | The first line contains the single integer *f* (0<=β€<=*f*<=<<=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"... | 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 ... | 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 con... | 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... | 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... | 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 c... | [
{
"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"
},
... | 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*<=<<=*r**i*<=β€<=109) β the coordinates of the left and the right ends of the *i*-th segment. It is guaranteed that there are n... | 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"... | 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 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 th... | 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"
},
... | 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 programmin... | 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... | 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\... | 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... | 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",
"out... | 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 possib... | 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... | 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... | [
"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,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... | 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 ... | 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... | 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.
T... | 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 incre... | [] | 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 g... | 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 positi... | 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 al... | [
"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 ... | [
{
"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": ... | 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 differ... | 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<=β€<=*... | 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 possi... | [
{
"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... | 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 cho... | 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 ar... | 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",
... | 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 ... | The first line contain four integers $x_1, y_1, x_2, y_2$ ($x_1 < x_2$, $y_2 < 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$ lin... | 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 ... | 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... | 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*... | 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\... | 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. Thu... | 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 n... | [
{
"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",
"o... | 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 ... | 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. ... | 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 pl... | 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... | [
{
"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": "12... | 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*. Le... | 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 in... | 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"
},
... | 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 ar... | 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 gar... | 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 distribu... | 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 se... | 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 th... | [
"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... | 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 ... | 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 8... | 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 ca... | 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\... | 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*... | 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 ty... | 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... | 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... | 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 ... | 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 ... | 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 f... | [
"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"
},
{
"in... | 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 ... | 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<=<... | 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 ... | [
{
"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\n... | 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... | The first line contains two space-separated integers *n* and *k* (1<=β€<=*k*<=<<=*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],<... | 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*,<=... | [
"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... | 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 pr... | 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* ... | 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,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 long... | 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 t... | 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 whi... | [
"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 save... | [
{
"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 4... | 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 foll... | 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 impossi... | [
{
"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": "(())"
},
{
... | 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 castle... | 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) β th... | 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 f... | [] | 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 cen... | 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"
},
{
... | 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* level... | 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": "9... | 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 paralle... | 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)Β β coordin... | 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... | [
{
"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\n... | 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 f... | 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 g... | 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. ... | [
"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"
},
{
"in... | 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... | 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... | 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 Bes... | 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 bin... | 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*<==<=[<... | 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... | 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*<==... | 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... | 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... | 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 ... | [
{
"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",
"o... | 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... | 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 c... | 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 ener... | [
{
"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... | 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 pl... | 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 poss... | 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 o... | 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"
},
{
"i... | 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 elem... | 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 alig... | [
{
"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"... | 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 opera... | 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 las... | 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 pi... | [
{
"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",
"out... | 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... | 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 descrip... | 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... | [
{
"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\n... | 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*<=<<=*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 roa... | 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 wh... | 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... | 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 frid... | 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*<=β€... | 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 b... | [
"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 ... | [] | 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$, ... | 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 intege... | 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... | 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 ... | 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 d... | 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<=<<=*h*, 0<=β€<=*m*1<=<<=*m*). The third line contains space-separated integers *h*2, *m*2 (0<=β€<=*h*2<=<... | 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" sr... | [] | 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:... | 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 t... | 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... | 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 ... | 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\ndumbavum... | 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... | 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 youn... | 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 ... | 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 ... | 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"... | 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,... | 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... | 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 ... | 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* integ... | 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 befor... | [
{
"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"
},
{
"... | 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... | 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 se... | 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, ... | [
"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 "aabaa... | [] | 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 (bi... | 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... | 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 "c... | [
{
"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"
},
{
"inp... | 93 | 716,800 | -1 | 20,519 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.