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 [*l*;<=*r*], and you have to count the number of its subsegments [*x*;<=*y*] (*l*<=β€<=*x*<=β€<=*y*<=β€<=*r*), such that if we delete all vertices except the segment of vertices [*x*;<=*y*] (including *x* and *y*) and edges between them, the resulting graph is bipartite. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=3Β·105, 1<=β€<=*m*<=β€<=3Β·105)Β β the number of vertices and the number of edges in the graph.
The next *m* lines describe edges in the graph. The *i*-th of these lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*; *a**i*<=β <=*b**i*), denoting an edge between vertices *a**i* and *b**i*. It is guaranteed that this graph does not contain edge-simple cycles of even length.
The next line contains a single integer *q* (1<=β€<=*q*<=β€<=3Β·105)Β β the number of queries.
The next *q* lines contain queries. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*)Β β the query parameters. | Print *q* numbers, each in new line: the *i*-th of them should be the number of subsegments [*x*;<=*y*] (*l**i*<=β€<=*x*<=β€<=*y*<=β€<=*r**i*), such that the graph that only includes vertices from segment [*x*;<=*y*] and edges between them is bipartite. | [
"6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n3\n1 3\n4 6\n1 6\n",
"8 9\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7\n7 8\n8 4\n7 2\n3\n1 8\n1 4\n3 8\n"
] | [
"5\n5\n14\n",
"27\n8\n19\n"
] | The first example is shown on the picture below:
<img class="tex-graphics" src="https://espresso.codeforces.com/01e1d1999228f416613ff64b5d0e0cf984f150b1.png" style="max-width: 100.0%;max-height: 100.0%;"/>
For the first query, all subsegments of [1;β3], except this segment itself, are suitable.
For the first query, all subsegments of [4;β6], except this segment itself, are suitable.
For the third query, all subsegments of [1;β6] are suitable, except [1;β3], [1;β4], [1;β5], [1;β6], [2;β6], [3;β6], [4;β6].
The second example is shown on the picture below:
<img class="tex-graphics" src="https://espresso.codeforces.com/09b9227070585b8d5a7dff3cbc5f8535c260a595.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n3\n1 3\n4 6\n1 6",
"output": "5\n5\n14"
},
{
"input": "8 9\n1 2\n2 3\n3 1\n4 5\n5 6\n6 7\n7 8\n8 4\n7 2\n3\n1 8\n1 4\n3 8",
"output": "27\n8\n19"
},
{
"input": "12 12\n5 1\n5 11\n1 11\n8 11\n8 9\n5 12\n6 9\n7 11\n9 3\n9 10\n4 12\n10 2\n78\n1... | 2,000 | 137,318,400 | 0 | 17,096 | |
847 | Union of Doubly Linked Lists | [
"implementation"
] | null | null | Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle.
In this problem you are given *n* memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from 1 to *n*.
For each cell *i* you are given two values:
- *l**i* β cell containing previous element for the element in the cell *i*; - *r**i* β cell containing next element for the element in the cell *i*.
If cell *i* contains information about the element which has no previous element then *l**i*<==<=0. Similarly, if cell *i* contains information about the element which has no next element then *r**i*<==<=0.
For example, for the picture above the values of *l* and *r* are the following: *l*1<==<=4, *r*1<==<=7; *l*2<==<=5, *r*2<==<=0; *l*3<==<=0, *r*3<==<=0; *l*4<==<=6, *r*4<==<=1; *l*5<==<=0, *r*5<==<=2; *l*6<==<=0, *r*6<==<=4; *l*7<==<=1, *r*7<==<=0.
Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values *l**i*, *r**i*.
Any other action, other than joining the beginning of one list to the end of another, can not be performed. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of memory cells where the doubly linked lists are located.
Each of the following *n* lines contains two integers *l**i*, *r**i* (0<=β€<=*l**i*,<=*r**i*<=β€<=*n*) β the cells of the previous and the next element of list for cell *i*. Value *l**i*<==<=0 if element in cell *i* has no previous element in its list. Value *r**i*<==<=0 if element in cell *i* has no next element in its list.
It is guaranteed that the input contains the correct description of a single or more doubly linked lists. All lists have linear structure: each element of list except the first has exactly one previous element; each element of list except the last has exactly one next element. Each memory cell contains information about one element from some list, each element of each list written in one of *n* given cells. | Print *n* lines, the *i*-th line must contain two integers *l**i* and *r**i* β the cells of the previous and the next element of list for cell *i* after all lists from the input are united in a single list. If there are many solutions print any of them. | [
"7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0\n"
] | [
"4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0\n"
] | none | [
{
"input": "7\n4 7\n5 0\n0 0\n6 1\n0 2\n0 4\n1 0",
"output": "4 7\n5 6\n0 5\n6 1\n3 2\n2 4\n1 0"
},
{
"input": "2\n2 0\n0 1",
"output": "2 0\n0 1"
},
{
"input": "1\n0 0",
"output": "0 0"
},
{
"input": "4\n0 2\n1 0\n0 4\n3 0",
"output": "0 2\n1 3\n2 4\n3 0"
},
{
"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 will appear in the nearest update, when developers faced some troubles that only you may help them to solve.
All the messages are represented as a strings consisting of only lowercase English letters. In order to reduce the network load strings are represented in the special compressed form. Compression algorithm works as follows: string is represented as a concatenation of *n* blocks, each block containing only equal characters. One block may be described as a pair (*l**i*,<=*c**i*), where *l**i* is the length of the *i*-th block and *c**i* is the corresponding letter. Thus, the string *s* may be written as the sequence of pairs .
Your task is to write the program, that given two compressed string *t* and *s* finds all occurrences of *s* in *t*. Developers know that there may be many such occurrences, so they only ask you to find the number of them. Note that *p* is the starting position of some occurrence of *s* in *t* if and only if *t**p**t**p*<=+<=1...*t**p*<=+<=|*s*|<=-<=1<==<=*s*, where *t**i* is the *i*-th character of string *t*.
Note that the way to represent the string in compressed form may not be unique. For example string "aaaa" may be given as , , ... | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=200<=000)Β β the number of blocks in the strings *t* and *s*, respectively.
The second line contains the descriptions of *n* parts of string *t* in the format "*l**i*-*c**i*" (1<=β€<=*l**i*<=β€<=1<=000<=000)Β β the length of the *i*-th part and the corresponding lowercase English letter.
The second line contains the descriptions of *m* parts of string *s* in the format "*l**i*-*c**i*" (1<=β€<=*l**i*<=β€<=1<=000<=000)Β β the length of the *i*-th part and the corresponding lowercase English letter. | Print a single integerΒ β the number of occurrences of *s* in *t*. | [
"5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c\n",
"6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a\n",
"5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d\n"
] | [
"1",
"6",
"0"
] | In the first sample, *t* = "aaabbccccaaacc", and string *s* = "aabbc". The only occurrence of string *s* in string *t* starts at position *p*β=β2.
In the second sample, *t* = "aaabbbbbbaaaaaaacccceeeeeeeeaa", and *s* = "aaa". The occurrences of *s* in *t* start at positions *p*β=β1, *p*β=β10, *p*β=β11, *p*β=β12, *p*β=β13 and *p*β=β14. | [
{
"input": "5 3\n3-a 2-b 4-c 3-a 2-c\n2-a 2-b 1-c",
"output": "1"
},
{
"input": "6 1\n3-a 6-b 7-a 4-c 8-e 2-a\n3-a",
"output": "6"
},
{
"input": "5 5\n1-h 1-e 1-l 1-l 1-o\n1-w 1-o 1-r 1-l 1-d",
"output": "0"
},
{
"input": "9 3\n1-h 1-e 2-l 1-o 1-w 1-o 1-r 1-l 1-d\n2-l 1-o 1-w... | 2,000 | 28,876,800 | 0 | 17,159 | |
0 | none | [
"none"
] | null | null | This is an interactive problem. In the output section below you will see the information about flushing the output.
On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in ViΔkopolis. Upon arrival, they left the car in a huge parking lot near the restaurant and hurried inside the building.
In the restaurant a polite waiter immediately brought the menu to Leha and Noora, consisting of *n* dishes. It is interesting that all dishes in the menu are numbered with integers from 1 to *n*. After a little thought, the girl ordered exactly *k* different dishes from available in the menu. To pass the waiting time while the chefs prepare ordered dishes, the girl invited the hacker to play a game that will help them get to know each other better.
The game itself is very simple: Noora wants Leha to guess any two dishes among all ordered. At the same time, she is ready to answer only one type of questions. Leha can say two numbers *x* and *y* (1<=β€<=*x*,<=*y*<=β€<=*n*). After that Noora chooses some dish *a* for the number *x* such that, at first, *a* is among the dishes Noora ordered (*x* can be equal to *a*), and, secondly, the value is the minimum possible. By the same rules the girl chooses dish *b* for *y*. After that Noora says Β«TAKΒ» to Leha, if , and Β«NIEΒ» otherwise. However, the restaurant is preparing quickly, so Leha has enough time to ask no more than 60 questions. After that he should name numbers of any two dishes Noora ordered.
Help Leha to solve this problem! | There are two numbers *n* and *k* (2<=β€<=*k*<=β€<=*n*<=β€<=105) in the single line of input denoting the number of dishes in the menu and the number of dishes Noora ordered. | If you want to provide an answer, output a string of the form 2 *x* *y* (1<=β€<=*x*,<=*y*<=β€<=*n*,<=*x*<=β <=*y*), if you think the dishes *x* and *y* was among dishes ordered by Noora. After that, flush the output and terminate your program. | [
"3 2\nNIE\nTAK\nNIE\nTAK\nTAK\nTAK\n"
] | [
"1 1 2\n1 2 1\n1 1 3\n1 3 1\n1 2 3\n1 3 2\n2 2 3\n"
] | There are three dishes in sample. Noora ordered dished numberes 2 and 3, which Leha should guess. If Noora receive requests for the first dish (*x*β=β1), then she'll choose the second dish (*a*β=β2) as the dish with the minimum value <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5a4a1705bc256e413564795cc8b70857f0ae44c.png" style="max-width: 100.0%;max-height: 100.0%;"/>. For the second (*x*β=β2) and the third (*x*β=β3) dishes themselves will be optimal, because in that case <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/79add195d023131e992b60a354fec89501759e75.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
Let Leha asks Noora about the next couple of dishes:
- *x*β=β1, *y*β=β2, then he'll recieve Β«NIEΒ» answer, because |1β-β2|β>β|2β-β2| - *x*β=β2, *y*β=β1, then he'll recieve Β«TAKΒ» answer, because |2β-β2|ββ€β|1β-β2| - *x*β=β1, *y*β=β3, then he'll recieve Β«NIEΒ» answer, because |1β-β2|β>β|3β-β3| - *x*β=β3, *y*β=β1, then he'll recieve Β«TAKΒ» answer, because |3β-β3|ββ€β|1β-β2| - *x*β=β2, *y*β=β3, then he'll recieve Β«TAKΒ» answer, because |2β-β2|ββ€β|3β-β3| - *x*β=β3, *y*β=β2, then he'll recieve Β«TAKΒ» answer, because |3β-β3|ββ€β|2β-β2|
According to the available information, it is possible to say that Nura ordered dishes with numbers 2 and 3. | [] | 93 | 0 | -1 | 17,168 | |
269 | Magical Boxes | [
"greedy",
"math"
] | null | null | Emuskald is a well-known illusionist. One of his trademark tricks involves a set of magical boxes. The essence of the trick is in packing the boxes inside other boxes.
From the top view each magical box looks like a square with side length equal to 2*k* (*k* is an integer, *k*<=β₯<=0) units. A magical box *v* can be put inside a magical box *u*, if side length of *v* is strictly less than the side length of *u*. In particular, Emuskald can put 4 boxes of side length 2*k*<=-<=1 into one box of side length 2*k*, or as in the following figure:
Emuskald is about to go on tour performing around the world, and needs to pack his magical boxes for the trip. He has decided that the best way to pack them would be inside another magical box, but magical boxes are quite expensive to make. Help him find the smallest magical box that can fit all his boxes. | The first line of input contains an integer *n* (1<=β€<=*n*<=β€<=105), the number of different sizes of boxes Emuskald has. Each of following *n* lines contains two integers *k**i* and *a**i* (0<=β€<=*k**i*<=β€<=109, 1<=β€<=*a**i*<=β€<=109), which means that Emuskald has *a**i* boxes with side length 2*k**i*. It is guaranteed that all of *k**i* are distinct. | Output a single integer *p*, such that the smallest magical box that can contain all of Emuskaldβs boxes has side length 2*p*. | [
"2\n0 3\n1 5\n",
"1\n0 4\n",
"2\n1 10\n2 2\n"
] | [
"3\n",
"1\n",
"3\n"
] | Picture explanation. If we have 3 boxes with side length 2 and 5 boxes with side length 1, then we can put all these boxes inside a box with side length 4, for example, as shown in the picture.
In the second test case, we can put all four small boxes into a box with side length 2. | [
{
"input": "2\n0 3\n1 5",
"output": "3"
},
{
"input": "1\n0 4",
"output": "1"
},
{
"input": "2\n1 10\n2 2",
"output": "3"
},
{
"input": "1\n0 1",
"output": "1"
},
{
"input": "1\n1000000000 1000000000",
"output": "1000000015"
},
{
"input": "1\n0 16",
... | 62 | 0 | 0 | 17,228 | |
65 | Harry Potter and the Sorting Hat | [
"brute force",
"dfs and similar",
"hashing"
] | D. Harry Potter and the Sorting Hat | 2 | 256 | As you know, Hogwarts has four houses: Gryffindor, Hufflepuff, Ravenclaw and Slytherin. The sorting of the first-years into houses is done by the Sorting Hat. The pupils are called one by one in the alphabetical order, each of them should put a hat on his head and, after some thought, the hat solemnly announces the name of the house the student should enter.
At that the Hat is believed to base its considerations on the student's personal qualities: it sends the brave and noble ones to Gryffindor, the smart and shrewd ones β to Ravenclaw, the persistent and honest ones β to Hufflepuff and the clever and cunning ones β to Slytherin. However, a first year student Hermione Granger got very concerned about the forthcoming sorting. She studied all the literature on the Sorting Hat and came to the conclusion that it is much simpler than that. If the relatives of the student have already studied at Hogwarts, the hat puts the student to the same house, where his family used to study. In controversial situations, when the relatives studied in different houses or when they were all Muggles like Hermione's parents, then the Hat sorts the student to the house, to which the least number of first years has been sent at that moment. If there are several such houses, the choice is given to the student himself. Then the student can choose any of the houses, to which the least number of first years has been sent so far.
Hermione has already asked the students that are on the list before her about their relatives. Now she and her new friends Harry Potter and Ron Weasley want to find out into what house the Hat will put Hermione. | The first input line contains an integer *n* (1<=β€<=*n*<=β€<=10000). It is the number of students who are in the list before Hermione. The next line contains *n* symbols. If all the relatives of a student used to study in the same house, then the *i*-th character in the string coincides with the first letter of the name of this house. Otherwise, the *i*-th symbol is equal to "?". | Print all the possible houses where Hermione can be sent. The names of the houses should be printed in the alphabetical order, one per line. | [
"11\nG????SS???H\n",
"2\nH?\n"
] | [
"Gryffindor\nRavenclaw\n",
"Gryffindor\nRavenclaw\nSlytherin\n"
] | Consider the second example. There are only two students before Hermione. The first student is sent to Hufflepuff. The second disciple is given the choice between the houses where the least number of students has been sent, i.e. Gryffindor, Slytherin and Ravenclaw. If he chooses Gryffindor, Hermione is forced to choose between Ravenclaw and Slytherin, if he chooses Ravenclaw, Hermione will choose between Gryffindor and Slytherin, if he chooses Slytherin, Hermione will choose between Gryffindor and Ravenclaw. In the end, the following situation is possible (it depends on the choice of the second student and Hermione). Hermione will end up 1) in Gryffindor, 2) in Ravenclaw, 3) in Slytherin. Note that, despite the fact that in neither case Hermione will be given a choice between all the three options, they are all possible and they should all be printed in the answer. Hermione will not, under any circumstances, end up in Hufflepuff. | [
{
"input": "11\nG????SS???H",
"output": "Gryffindor\nRavenclaw"
},
{
"input": "2\nH?",
"output": "Gryffindor\nRavenclaw\nSlytherin"
},
{
"input": "1\n?",
"output": "Gryffindor\nHufflepuff\nRavenclaw\nSlytherin"
},
{
"input": "1\nG",
"output": "Hufflepuff\nRavenclaw\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 following operation many times:
1. Choose one of her cards *X*. This card mustn't be chosen before. 1. If Jiro has no alive cards at that moment, he gets the damage equal to (*X*'s strength). Otherwise, Ciel needs to choose one Jiro's alive card *Y*, then: If *Y*'s position is Attack, then (*X*'s strength) <=β₯<= (*Y*'s strength) must hold. After this attack, card *Y* dies, and Jiro gets the damage equal to (*X*'s strength) - (*Y*'s strength). 1. If *Y*'s position is Defense, then (*X*'s strength) <=><= (*Y*'s strength) must hold. After this attack, card *Y* dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100) β the number of cards Jiro and Ciel have.
Each of the next *n* lines contains a string *position* and an integer *strength* (0<=β€<=*strength*<=β€<=8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next *m* lines contains an integer *strength* (0<=β€<=*strength*<=β€<=8000) β the strength of Ciel's current card. | Output an integer: the maximal damage Jiro can get. | [
"2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500\n",
"3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001\n",
"2 4\nDEF 0\nATK 0\n0\n0\n1\n1\n"
] | [
"3000\n",
"992\n",
"1\n"
] | In the first test case, Ciel has 3 cards with same *strength*. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500β-β2000β=β500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500β+β2500β=β3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001β-β100)β+β(101β-β10)β=β992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | [
{
"input": "2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500",
"output": "3000"
},
{
"input": "3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001",
"output": "992"
},
{
"input": "2 4\nDEF 0\nATK 0\n0\n0\n1\n1",
"output": "1"
},
{
"input": "1 1\nATK 100\n99",
"output": "0"
},
{... | 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**r*<=+<=1... *a**n* has no more than *k* inversions.
An inversion in sequence *b* is a pair of elements of the sequence *b*, that change their relative order after a stable sorting of the sequence. In other words, an inversion is a pair of integers *i* and *j*, such that 1<=β€<=*i*<=<<=*j*<=β€<=|*b*| and *b**i*<=><=*b**j*, where |*b*| is the length of sequence *b*, and *b**j* is its *j*-th element.
Help the Little Elephant and count the number of the described pairs. | The first line contains two integers *n* and *k* (2<=β€<=*n*<=β€<=105,<=0<=β€<=*k*<=β€<=1018) β the size of array *a* and the maximum allowed number of inversions respectively. The next line contains *n* positive integers, separated by single spaces, *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β elements of array *a*.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. | In a single line print a single number β the answer to the problem. | [
"3 1\n1 3 2\n",
"5 2\n1 3 2 1 7\n"
] | [
"3\n",
"6\n"
] | none | [
{
"input": "3 1\n1 3 2",
"output": "3"
},
{
"input": "5 2\n1 3 2 1 7",
"output": "6"
},
{
"input": "7 3\n1 7 6 4 9 5 3",
"output": "6"
},
{
"input": "5 0\n1 2 3 4 5",
"output": "10"
},
{
"input": "2 1\n2 1",
"output": "1"
},
{
"input": "3 1000000000000... | 218 | 0 | 0 | 17,323 | |
1,007 | Mini Metro | [
"dp"
] | null | null | In a simplified version of a "Mini Metro" game, there is only one subway line, and all the trains go in the same direction. There are $n$ stations on the line, $a_i$ people are waiting for the train at the $i$-th station at the beginning of the game. The game starts at the beginning of the $0$-th hour. At the end of each hour (couple minutes before the end of the hour), $b_i$ people instantly arrive to the $i$-th station. If at some moment, the number of people at the $i$-th station is larger than $c_i$, you lose.
A player has several trains which he can appoint to some hours. The capacity of each train is $k$ passengers. In the middle of the appointed hour, the train goes from the $1$-st to the $n$-th station, taking as many people at each station as it can accommodate. A train can not take people from the $i$-th station if there are people at the $i-1$-th station.
If multiple trains are appointed to the same hour, their capacities are being added up and they are moving together.
The player wants to stay in the game for $t$ hours. Determine the minimum number of trains he will need for it. | The first line contains three integers $n$, $t$, and $k$ ($1 \leq n, t \leq 200, 1 \leq k \leq 10^9$)Β β the number of stations on the line, hours we want to survive, and capacity of each train respectively.
Each of the next $n$ lines contains three integers $a_i$, $b_i$, and $c_i$ ($0 \leq a_i, b_i \leq c_i \leq 10^9$)Β β number of people at the $i$-th station in the beginning of the game, number of people arriving to $i$-th station in the end of each hour and maximum number of people at the $i$-th station allowed respectively. | Output a single integer numberΒ β the answer to the problem. | [
"3 3 10\n2 4 10\n3 3 9\n4 2 8\n",
"4 10 5\n1 1 1\n1 0 1\n0 5 8\n2 7 100\n"
] | [
"2\n",
"12\n"
] | <img class="tex-graphics" src="https://espresso.codeforces.com/bfa11d535d9fc44e73f6f8280d06436e4e327753.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Let's look at the sample. There are three stations, on the first, there are initially 2 people, 3 people on the second, and 4 people on the third. Maximal capacities of the stations are 10, 9, and 8 respectively.
One of the winning strategies is to appoint two trains to the first and the third hours. Then on the first hour, the train takes all of the people from the stations, and at the end of the hour, 4 people arrive at the first station, 3 on the second, and 2 on the third.
In the second hour there are no trains appointed, and at the end of it, the same amount of people are arriving again.
In the third hour, the train first takes 8 people from the first station, and when it arrives at the second station, it takes only 2 people because it can accommodate no more than 10 people. Then it passes by the third station because it is already full. After it, people arrive at the stations once more, and the game ends.
As there was no such moment when the number of people at a station exceeded maximal capacity, we won using two trains. | [] | 31 | 0 | 0 | 17,454 | |
852 | Product transformation | [
"combinatorics",
"math",
"number theory"
] | null | null | Consider an array *A* with *N* elements, all being the same integer *a*.
Define the product transformation as a simultaneous update *A**i*<==<=*A**i*Β·*A**i*<=+<=1, that is multiplying each element to the element right to it for , with the last number *A**N* remaining the same. For example, if we start with an array *A* with *a*<==<=2 and *N*<==<=4, then after one product transformation *A*<==<=[4,<= 4,<= 4,<= 2], and after two product transformations *A*<==<=[16,<= 16,<= 8,<= 2].
Your simple task is to calculate the array *A* after *M* product transformations. Since the numbers can get quite big you should output them modulo *Q*. | The first and only line of input contains four integers *N*, *M*, *a*, *Q* (7<=β€<=*Q*<=β€<=109<=+<=123, 2<=β€<=*a*<=β€<=106<=+<=123, , is prime), where is the multiplicative order of the integer *a* modulo *Q*, see notes for definition. | You should output the array *A* from left to right. | [
"2 2 2 7\n"
] | [
"1 2 "
] | The multiplicative order of a number *a* modulo *Q* <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/38b13c1f6db75ae72784f8602e8230429b26cf2a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, is the smallest natural number *x* such that *a*<sup class="upper-index">*x*</sup> *mod* *Q*β=β1. For example, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/64b53b8160eddc004a5b65223bf29dd636bb1832.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [] | 140 | 40,243,200 | 0 | 17,473 | |
915 | Coprime Arrays | [
"math",
"number theory"
] | null | null | Let's call an array *a* of size *n* coprime iff *gcd*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=1, where *gcd* is the greatest common divisor of the arguments.
You are given two numbers *n* and *k*. For each *i* (1<=β€<=*i*<=β€<=*k*) you have to determine the number of coprime arrays *a* of size *n* such that for every *j* (1<=β€<=*j*<=β€<=*n*) 1<=β€<=*a**j*<=β€<=*i*. Since the answers can be very large, you have to calculate them modulo 109<=+<=7. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=2Β·106) β the size of the desired arrays and the maximum upper bound on elements, respectively. | Since printing 2Β·106 numbers may take a lot of time, you have to output the answer in such a way:
Let *b**i* be the number of coprime arrays with elements in range [1,<=*i*], taken modulo 109<=+<=7. You have to print , taken modulo 109<=+<=7. Here denotes bitwise xor operation (^ in C++ or Java, xor in Pascal). | [
"3 4\n",
"2000000 8\n"
] | [
"82\n",
"339310063\n"
] | Explanation of the example:
Since the number of coprime arrays is large, we will list the arrays that are non-coprime, but contain only elements in range [1,β*i*]:
For *i*β=β1, the only array is coprime. *b*<sub class="lower-index">1</sub>β=β1.
For *i*β=β2, array [2,β2,β2] is not coprime. *b*<sub class="lower-index">2</sub>β=β7.
For *i*β=β3, arrays [2,β2,β2] and [3,β3,β3] are not coprime. *b*<sub class="lower-index">3</sub>β=β25.
For *i*β=β4, arrays [2,β2,β2], [3,β3,β3], [2,β2,β4], [2,β4,β2], [2,β4,β4], [4,β2,β2], [4,β2,β4], [4,β4,β2] and [4,β4,β4] are not coprime. *b*<sub class="lower-index">4</sub>β=β55. | [
{
"input": "3 4",
"output": "82"
},
{
"input": "2000000 8",
"output": "339310063"
},
{
"input": "1000 1000",
"output": "293255159"
},
{
"input": "400000 400000",
"output": "641589365"
},
{
"input": "1000 2000",
"output": "946090030"
},
{
"input": "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-working days. Namely, each employee must work for exactly *n* consecutive days, then rest for exactly *m* days, then work for *n* more days and rest for *m* more, and so on. Vitaly doesn't want to break the law. Fortunately, there is a loophole: the law comes into force on the day when the employee is hired. For example, if an employee is hired on day *x*, then he should work on days [*x*,<=*x*<=+<=1,<=...,<=*x*<=+<=*n*<=-<=1], [*x*<=+<=*m*<=+<=*n*,<=*x*<=+<=*m*<=+<=*n*<=+<=1,<=...,<=*x*<=+<=*m*<=+<=2*n*<=-<=1], and so on. Day *x* can be chosen arbitrarily by Vitaly.
There is one more thing: the key to the store. Berland law prohibits making copies of keys, so there is only one key. Vitaly is planning to entrust the key to the store employees. At the same time on each day the key must be with an employee who works that day β otherwise on this day no one can get inside the store. During the day the key holder can give the key to another employee, if he also works that day. The key will handed to the first hired employee at his first working day.
Each employee has to be paid salary. Therefore, Vitaly wants to hire as few employees as possible provided that the store can operate normally on each day from 1 to infinity. In other words, on each day with index from 1 to infinity, the store must have at least *k* working employees, and one of the working employees should have the key to the store.
Help Vitaly and determine the minimum required number of employees, as well as days on which they should be hired. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*m*<=β€<=*n*<=β€<=1000, *n*<=β <=1, 1<=β€<=*k*<=β€<=1000). | In the first line print a single integer *z* β the minimum required number of employees.
In the second line print *z* positive integers, separated by spaces: the *i*-th integer *a**i* (1<=β€<=*a**i*<=β€<=104) should represent the number of the day, on which Vitaly should hire the *i*-th employee.
If there are multiple answers, print any of them. | [
"4 3 2\n",
"3 3 1\n"
] | [
"4\n1 1 4 5",
"3\n1 3 5"
] | none | [
{
"input": "4 3 2",
"output": "4\n1 1 4 5"
},
{
"input": "3 3 1",
"output": "3\n1 3 5"
},
{
"input": "5 5 3",
"output": "7\n1 1 1 5 6 6 7"
},
{
"input": "7 6 8",
"output": "16\n1 1 1 1 1 1 1 1 7 7 7 7 7 7 7 8"
},
{
"input": "8 3 2",
"output": "4\n1 1 8 9"
},... | 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<=-<=*a*2<==<=*a*2<=-<=*a*3<==<=...<==<=*a**k*<=-<=1<=-<=*a**k*<==<=1. Naturally, the number of piles *k* should be no less than two.
The friends play in turns. The player who cannot make a move loses. Serozha makes the first move. Who will win if both players play in the optimal way? | The single line contains a single integer *n* (1<=β€<=*n*<=β€<=105). | If Serozha wins, print *k*, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game.
If Gena wins, print "-1" (without the quotes). | [
"3\n",
"6\n",
"100\n"
] | [
"2\n",
"-1\n",
"8\n"
] | none | [
{
"input": "3",
"output": "2"
},
{
"input": "6",
"output": "-1"
},
{
"input": "100",
"output": "8"
},
{
"input": "33",
"output": "2"
},
{
"input": "23",
"output": "-1"
},
{
"input": "35",
"output": "-1"
},
{
"input": "15",
"output": "2"... | 2,000 | 2,355,200 | 0 | 17,598 |
14 | Two Paths | [
"dfs and similar",
"dp",
"graphs",
"shortest paths",
"trees",
"two pointers"
] | D. Two Paths | 2 | 64 | As you know, Bob's brother lives in Flatland. In Flatland there are *n* cities, connected by *n*<=-<=1 two-way roads. The cities are numbered from 1 to *n*. You can get from one city to another moving along the roads.
The Β«Two PathsΒ» company, where Bob's brother works, has won a tender to repair two paths in Flatland. A path is a sequence of different cities, connected sequentially by roads. The company is allowed to choose by itself the paths to repair. The only condition they have to meet is that the two paths shouldn't cross (i.e. shouldn't have common cities).
It is known that the profit, the Β«Two PathsΒ» company will get, equals the product of the lengths of the two paths. Let's consider the length of each road equals 1, and the length of a path equals the amount of roads in it. Find the maximum possible profit for the company. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=200), where *n* is the amount of cities in the country. The following *n*<=-<=1 lines contain the information about the roads. Each line contains a pair of numbers of the cities, connected by the road *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*). | Output the maximum possible profit. | [
"4\n1 2\n2 3\n3 4\n",
"7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n",
"6\n1 2\n2 3\n2 4\n5 4\n6 4\n"
] | [
"1\n",
"0\n",
"4\n"
] | none | [
{
"input": "4\n1 2\n2 3\n3 4",
"output": "1"
},
{
"input": "7\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7",
"output": "0"
},
{
"input": "6\n1 2\n2 3\n2 4\n5 4\n6 4",
"output": "4"
},
{
"input": "2\n2 1",
"output": "0"
},
{
"input": "3\n3 1\n1 2",
"output": "0"
},
{
... | 92 | 0 | 0 | 17,613 |
24 | F1 Champions | [
"implementation"
] | B. F1 Champions | 2 | 256 | Formula One championship consists of series of races called Grand Prix. After every race drivers receive points according to their final position. Only the top 10 drivers receive points in the following order 25, 18, 15, 12, 10, 8, 6, 4, 2, 1. At the conclusion of the championship the driver with most points is the champion. If there is a tie, champion is the one with most wins (i.e. first places). If a tie still exists, it is chosen the one with most second places, and so on, until there are no more place to use for compare.
Last year another scoring system was proposed but rejected. In it the champion is the one with most wins. If there is tie, champion is the one with most points. If a tie still exists it is proceeded the same way as in the original scoring system, that is comparing number of second, third, forth, and so on, places.
You are given the result of all races during the season and you are to determine the champion according to both scoring systems. It is guaranteed, that both systems will produce unique champion. | The first line contain integer *t* (1<=β€<=*t*<=β€<=20), where *t* is the number of races. After that all races are described one by one. Every race description start with an integer *n* (1<=β€<=*n*<=β€<=50) on a line of itself, where *n* is the number of clasified drivers in the given race. After that *n* lines follow with the classification for the race, each containing the name of a driver. The names of drivers are given in order from the first to the last place. The name of the driver consists of lowercase and uppercase English letters and has length at most 50 characters. Comparing of names should be case-sensetive. | Your output should contain exactly two line. On the first line is the name of the champion according to the original rule, and on the second line the name of the champion according to the alternative rule. | [
"3\n3\nHamilton\nVettel\nWebber\n2\nWebber\nVettel\n2\nHamilton\nVettel\n",
"2\n7\nProst\nSurtees\nNakajima\nSchumacher\nButton\nDeLaRosa\nBuemi\n8\nAlonso\nProst\nNinoFarina\nJimClark\nDeLaRosa\nNakajima\nPatrese\nSurtees\n"
] | [
"Vettel\nHamilton\n",
"Prost\nProst\n"
] | It is not guaranteed that the same drivers participate in all races. For the championship consider every driver that has participated in at least one race. The total number of drivers during the whole season is not more then 50. | [
{
"input": "3\n3\nHamilton\nVettel\nWebber\n2\nWebber\nVettel\n2\nHamilton\nVettel",
"output": "Vettel\nHamilton"
},
{
"input": "2\n7\nProst\nSurtees\nNakajima\nSchumacher\nButton\nDeLaRosa\nBuemi\n8\nAlonso\nProst\nNinoFarina\nJimClark\nDeLaRosa\nNakajima\nPatrese\nSurtees",
"output": "Prost\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 stick an exclamation mark on the cube and get the factorial of the number written on the cube. For example, if a cube reads 5, then after the sticking it reads 5!, which equals 120.
You need to help Anya count how many ways there are to choose some of the cubes and stick on some of the chosen cubes at most *k* exclamation marks so that the sum of the numbers written on the chosen cubes after the sticking becomes equal to *S*. Anya can stick at most one exclamation mark on each cube. Can you do it?
Two ways are considered the same if they have the same set of chosen cubes and the same set of cubes with exclamation marks. | The first line of the input contains three space-separated integers *n*, *k* and *S* (1<=β€<=*n*<=β€<=25, 0<=β€<=*k*<=β€<=*n*, 1<=β€<=*S*<=β€<=1016)Β βΒ the number of cubes and the number of stickers that Anya has, and the sum that she needs to get.
The second line contains *n* positive integers *a**i* (1<=β€<=*a**i*<=β€<=109)Β βΒ the numbers, written on the cubes. The cubes in the input are described in the order from left to right, starting from the first one.
Multiple cubes can contain the same numbers. | Output the number of ways to choose some number of cubes and stick exclamation marks on some of them so that the sum of the numbers became equal to the given number *S*. | [
"2 2 30\n4 3\n",
"2 2 7\n4 3\n",
"3 1 1\n1 1 1\n"
] | [
"1\n",
"1\n",
"6\n"
] | In the first sample the only way is to choose both cubes and stick an exclamation mark on each of them.
In the second sample the only way is to choose both cubes but don't stick an exclamation mark on any of them.
In the third sample it is possible to choose any of the cubes in three ways, and also we may choose to stick or not to stick the exclamation mark on it. So, the total number of ways is six. | [] | 30 | 0 | 0 | 17,671 | |
0 | none | [
"none"
] | null | null | For a connected undirected weighted graph *G*, MST (minimum spanning tree) is a subgraph of *G* that contains all of *G*'s vertices, is a tree, and sum of its edges is minimum possible.
You are given a graph *G*. If you run a MST algorithm on graph it would give you only one MST and it causes other edges to become jealous. You are given some queries, each query contains a set of edges of graph *G*, and you should determine whether there is a MST containing all these edges or not. | The first line contains two integers *n*, *m* (2<=<=β€<=*n*,<=*m*<=<=β€<=5Β·105, *n*<=-<=1<=β€<=*m*)Β β the number of vertices and edges in the graph and the number of queries.
The *i*-th of the next *m* lines contains three integers *u**i*, *v**i*, *w**i* (*u**i*<=β <=*v**i*, 1<=β€<=*w**i*<=β€<=5Β·105)Β β the endpoints and weight of the *i*-th edge. There can be more than one edges between two vertices. It's guaranteed that the given graph is connected.
The next line contains a single integer *q* (1<=β€<=*q*<=β€<=5Β·105)Β β the number of queries.
*q* lines follow, the *i*-th of them contains the *i*-th query. It starts with an integer *k**i* (1<=β€<=*k**i*<=β€<=*n*<=-<=1)Β β the size of edges subset and continues with *k**i* distinct space-separated integers from 1 to *m*Β β the indices of the edges. It is guaranteed that the sum of *k**i* for 1<=β€<=*i*<=β€<=*q* does not exceed 5Β·105. | For each query you should print "YES" (without quotes) if there's a MST containing these edges and "NO" (of course without quotes again) otherwise. | [
"5 7\n1 2 2\n1 3 2\n2 3 1\n2 4 1\n3 4 1\n3 5 2\n4 5 2\n4\n2 3 4\n3 3 4 5\n2 1 7\n2 1 2\n"
] | [
"YES\nNO\nYES\nNO\n"
] | This is the graph of sample:
Weight of minimum spanning tree on this graph is 6.
MST with edges (1,β3,β4,β6), contains all of edges from the first query, so answer on the first query is "YES".
Edges from the second query form a cycle of length 3, so there is no spanning tree including these three edges. Thus, answer is "NO". | [] | 46 | 0 | 0 | 17,674 | |
270 | Multithreading | [
"data structures",
"greedy",
"implementation"
] | null | null | Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages.
Recent actions shows a list of *n* different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time.
Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the *i*-th place in the list there is a thread that was at the *a**i*-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages.
Help Emuskald find out the number of threads that surely have new messages. A thread *x* surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold:
1. thread *x* is not updated (it has no new messages); 1. the list order 1, 2, ..., *n* changes to *a*1, *a*2, ..., *a**n*. | The first line of input contains an integer *n*, the number of threads (1<=β€<=*n*<=β€<=105). The next line contains a list of *n* space-separated integers *a*1, *a*2, ..., *a**n* where *a**i* (1<=β€<=*a**i*<=β€<=*n*) is the old position of the *i*-th thread in the new list. It is guaranteed that all of the *a**i* are distinct. | Output a single integer β the number of threads that surely contain a new message. | [
"5\n5 2 1 3 4\n",
"3\n1 2 3\n",
"4\n4 3 2 1\n"
] | [
"2\n",
"0\n",
"3\n"
] | In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages.
In the second test case, there may be no new messages at all, since the thread order hasn't changed.
In the third test case, only thread 1 can contain no new messages. | [
{
"input": "5\n5 2 1 3 4",
"output": "2"
},
{
"input": "3\n1 2 3",
"output": "0"
},
{
"input": "4\n4 3 2 1",
"output": "3"
},
{
"input": "5\n1 2 5 3 4",
"output": "3"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n1 2",
"output": "0"
},
... | 154 | 12,390,400 | 3 | 17,679 | |
774 | Repairing Of String | [
"*special",
"constructive algorithms"
] | null | null | Stepan had a favorite string *s* which consisted of the lowercase letters of the Latin alphabet.
After graduation, he decided to remember it, but it was a long time ago, so he can't now remember it. But Stepan remembers some information about the string, namely the sequence of integers *c*1,<=*c*2,<=...,<=*c**n*, where *n* equals the length of the string *s*, and *c**i* equals the number of substrings in the string *s* with the length *i*, consisting of the same letters. The substring is a sequence of consecutive characters in the string *s*.
For example, if the Stepan's favorite string is equal to "tttesst", the sequence *c* looks like: *c*<==<=[7,<=3,<=1,<=0,<=0,<=0,<=0].
Stepan asks you to help to repair his favorite string *s* according to the given sequence *c*1,<=*c*2,<=...,<=*c**n*. | The first line contains the integer *n* (1<=β€<=*n*<=β€<=2000) β the length of the Stepan's favorite string.
The second line contains the sequence of integers *c*1,<=*c*2,<=...,<=*c**n* (0<=β€<=*c**i*<=β€<=2000), where *c**i* equals the number of substrings of the string *s* with the length *i*, consisting of the same letters.
It is guaranteed that the input data is such that the answer always exists. | Print the repaired Stepan's favorite string. If there are several answers, it is allowed to print any of them. The string should contain only lowercase letters of the English alphabet. | [
"6\n6 3 1 0 0 0\n",
"4\n4 0 0 0\n"
] | [
"kkrrrq",
"abcd\n"
] | In the first test Stepan's favorite string, for example, can be the string "kkrrrq", because it contains 6 substrings with the length 1, consisting of identical letters (they begin in positions 1, 2, 3, 4, 5 and 6), 3 substrings with the length 2, consisting of identical letters (they begin in positions 1, 3 and 4), and 1 substring with the length 3, consisting of identical letters (it begins in the position 3). | [
{
"input": "6\n6 3 1 0 0 0",
"output": "aaabbc"
},
{
"input": "4\n4 0 0 0",
"output": "abcd"
},
{
"input": "1\n1",
"output": "a"
},
{
"input": "5\n5 0 0 0 0",
"output": "abcde"
},
{
"input": "10\n10 8 7 6 5 4 3 2 1 0",
"output": "aaaaaaaaab"
},
{
"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 position *i*.
For example, a multiset of strings {"abc", "aba", "adc", "ada"} are not easy to remember. And multiset {"abc", "ada", "ssa"} is easy to remember because:
- the first string is the only string that has character *c* in position 3; - the second string is the only string that has character *d* in position 2; - the third string is the only string that has character *s* in position 2.
You want to change your multiset a little so that it is easy to remember. For *a**ij* coins, you can change character in the *j*-th position of the *i*-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember. | The first line contains two integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=20)Β β the number of strings in the multiset and the length of the strings respectively. Next *n* lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is *m*.
Next *n* lines contain *m* integers each, the *i*-th of them contains integers *a**i*1,<=*a**i*2,<=...,<=*a**im* (0<=β€<=*a**ij*<=β€<=106). | Print a single number β the answer to the problem. | [
"4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n",
"4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10\n",
"3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1\n"
] | [
"3\n",
"2\n",
"0\n"
] | none | [
{
"input": "4 5\nabcde\nabcde\nabcde\nabcde\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1\n1 1 1 1 1",
"output": "3"
},
{
"input": "4 3\nabc\naba\nadc\nada\n10 10 10\n10 1 10\n10 10 10\n10 1 10",
"output": "2"
},
{
"input": "3 3\nabc\nada\nssa\n1 1 1\n1 1 1\n1 1 1",
"output": "0"
},
{
"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 *parity*(*a*) to the end of *a*. For example, . - Remove the first character of *a*. For example, . You cannot perform this operation if *a* is empty.
You can use as many operations as you want. The problem is, is it possible to turn *a* into *b*?
The *parity* of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise. | The first line contains the string *a* and the second line contains the string *b* (1<=β€<=|*a*|,<=|*b*|<=β€<=1000). Both strings contain only the characters "0" and "1". Here |*x*| denotes the length of the string *x*. | Print "YES" (without quotes) if it is possible to turn *a* into *b*, and "NO" (without quotes) otherwise. | [
"01011\n0110\n",
"0011\n1110\n"
] | [
"YES\n",
"NO\n"
] | In the first sample, the steps are as follows: 01011βββ1011βββ011βββ0110 | [
{
"input": "01011\n0110",
"output": "YES"
},
{
"input": "0011\n1110",
"output": "NO"
},
{
"input": "11111\n111111",
"output": "YES"
},
{
"input": "0110011\n01100110",
"output": "YES"
},
{
"input": "10000100\n011110",
"output": "NO"
},
{
"input": "1\n0"... | 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 elements in segment of the array from index *l* to index *r*, inclusive (1-indexed). | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=106). The second line contains *n* integer numbers *a*1, *a*2, ... *a**n* (1<=β€<=*a**i*<=β€<=106) β elements of the array. | Print one number β the expected number of unique elements in chosen segment.
Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=4 β formally, the answer is correct if , where *x* is jury's answer, and *y* is your answer. | [
"2\n1 2\n",
"2\n2 2\n"
] | [
"1.500000\n",
"1.000000\n"
] | none | [
{
"input": "2\n1 2",
"output": "1.500000"
},
{
"input": "2\n2 2",
"output": "1.000000"
},
{
"input": "10\n9 6 8 5 5 2 8 9 2 2",
"output": "3.100000"
},
{
"input": "20\n49 33 9 8 50 21 12 44 23 39 24 10 17 4 17 40 24 19 27 21",
"output": "7.010000"
},
{
"input": "1... | 1,201 | 91,033,600 | 3 | 17,854 | |
245 | Restoring Table | [
"constructive algorithms",
"greedy"
] | null | null | Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers *a*1,<=*a*2,<=...,<=*a**n*. He also wrote a square matrix *b* of size *n*<=Γ<=*n*. The element of matrix *b* that sits in the *i*-th row in the *j*-th column (we'll denote it as *b**ij*) equals:
- the "bitwise AND" of numbers *a**i* and *a**j* (that is, *b**ij*<==<=*a**i*Β &Β *a**j*), if *i*<=β <=*j*; - -1, if *i*<==<=*j*.
Having written out matrix *b*, Polycarpus got very happy and wiped *a* off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus' calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won't prove that he can count correctly.
Help Polycarpus, given matrix *b*, restore the sequence of numbers *a*1,<=*a*2,<=...,<=*a**n*, that he has removed from the board. Polycarpus doesn't like large numbers, so any number in the restored sequence mustn't exceed 109. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the size of square matrix *b*. Next *n* lines contain matrix *b*. The *i*-th of these lines contains *n* space-separated integers: the *j*-th number represents the element of matrix *b**ij*. It is guaranteed, that for all *i* (1<=β€<=*i*<=β€<=*n*) the following condition fulfills: *b**ii* = -1. It is guaranteed that for all *i*,<=*j* (1<=β€<=*i*,<=*j*<=β€<=*n*;Β *i*<=β <=*j*) the following condition fulfills: 0<=β€<=*b**ij*<=β€<=109, *b**ij*<==<=*b**ji*. | Print *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109) β the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence *a* that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them. | [
"1\n-1\n",
"3\n-1 18 0\n18 -1 0\n0 0 -1\n",
"4\n-1 128 128 128\n128 -1 148 160\n128 148 -1 128\n128 160 128 -1\n"
] | [
"0 ",
"18 18 0 ",
"128 180 148 160 "
] | If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation. | [
{
"input": "1\n-1",
"output": "0 "
},
{
"input": "3\n-1 18 0\n18 -1 0\n0 0 -1",
"output": "18 18 0 "
},
{
"input": "4\n-1 128 128 128\n128 -1 148 160\n128 148 -1 128\n128 160 128 -1",
"output": "128 180 148 160 "
},
{
"input": "5\n-1 0 0 0 0\n0 -1 1 0 0\n0 1 -1 0 0\n0 0 0 -1 ... | 92 | 0 | 3 | 17,864 | |
0 | none | [
"none"
] | null | null | Pavel cooks barbecue. There are *n* skewers, they lay on a brazier in a row, each on one of *n* positions. Pavel wants each skewer to be cooked some time in every of *n* positions in two directions: in the one it was directed originally and in the reversed direction.
Pavel has a plan: a permutation *p* and a sequence *b*1,<=*b*2,<=...,<=*b**n*, consisting of zeros and ones. Each second Pavel move skewer on position *i* to position *p**i*, and if *b**i* equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions.
Unfortunately, not every pair of permutation *p* and sequence *b* suits Pavel. What is the minimum total number of elements in the given permutation *p* and the given sequence *b* he needs to change so that every skewer will visit each of 2*n* placements? Note that after changing the permutation should remain a permutation as well.
There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation *p* and a sequence *b* suit him if there is an integer *k* (*k*<=β₯<=2*n*), so that after *k* seconds each skewer visits each of the 2*n* placements.
It can be shown that some suitable pair of permutation *p* and sequence *b* exists for any *n*. | The first line contain the integer *n* (1<=β€<=*n*<=β€<=2Β·105)Β β the number of skewers.
The second line contains a sequence of integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*)Β β the permutation, according to which Pavel wants to move the skewers.
The third line contains a sequence *b*1,<=*b*2,<=...,<=*b**n* consisting of zeros and ones, according to which Pavel wants to reverse the skewers. | Print single integerΒ β the minimum total number of elements in the given permutation *p* and the given sequence *b* he needs to change so that every skewer will visit each of 2*n* placements. | [
"4\n4 3 2 1\n0 1 1 1\n",
"3\n2 3 1\n0 0 0\n"
] | [
"2\n",
"1\n"
] | In the first example Pavel can change the permutation to 4,β3,β1,β2.
In the second example Pavel can change any element of *b* to 1. | [
{
"input": "4\n4 3 2 1\n0 1 1 1",
"output": "2"
},
{
"input": "3\n2 3 1\n0 0 0",
"output": "1"
},
{
"input": "1\n1\n0",
"output": "1"
},
{
"input": "2\n1 2\n0 0",
"output": "3"
},
{
"input": "2\n2 1\n0 0",
"output": "1"
},
{
"input": "2\n1 2\n0 1",
... | 202 | 6,041,600 | -1 | 17,888 | |
827 | Dirty Arkady's Kitchen | [
"data structures",
"dp",
"graphs",
"shortest paths"
] | null | null | Arkady likes to walk around his kitchen. His labyrinthine kitchen consists of several important places connected with passages. Unfortunately it happens that these passages are flooded with milk so that it's impossible to pass through them. Namely, it's possible to pass through each passage in any direction only during some time interval.
The lengths of all passages are equal and Arkady makes through them in one second. For security reasons, Arkady can never stop, also, he can't change direction while going through a passage. In other words, if he starts walking in some passage, he should reach its end and immediately leave the end.
Today Arkady needs to quickly reach important place *n* from place 1. He plans to exit the place 1 at time moment 0 and reach the place *n* as early as he can. Please find the minimum time he should spend on his way. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=5Β·105, 0<=β€<=*m*<=β€<=5Β·105)Β β the number of important places and the number of passages, respectively.
After that, *m* lines follow, each of them describe one passage. Each line contains four integers *a*, *b*, *l* and *r* (1<=β€<=*a*,<=*b*<=β€<=*n*, *a*<=β <=*b*, 0<=β€<=*l*<=<<=*r*<=β€<=109)Β β the places the passage connects and the time segment during which it's possible to use this passage. | Print one integerΒ β minimum time Arkady should spend to reach the destination. If he can't reach the place *n*, print -1. | [
"5 6\n1 2 0 1\n2 5 2 3\n2 5 0 1\n1 3 0 1\n3 4 1 2\n4 5 2 3\n",
"2 1\n1 2 1 100\n"
] | [
"3\n",
"-1\n"
] | In the first example Arkady should go through important places 1βββ3βββ4βββ5.
In the second example Arkady can't start his walk because at time moment 0 it's impossible to use the only passage. | [] | 389 | 10,137,600 | -1 | 17,905 | |
0 | none | [
"none"
] | null | null | A tree is a graph with *n* vertices and exactly *n*<=-<=1 edges; this graph should meet the following condition: there exists exactly one shortest (by number of edges) path between any pair of its vertices.
A subtree of a tree *T* is a tree with both vertices and edges as subsets of vertices and edges of *T*.
You're given a tree with *n* vertices. Consider its vertices numbered with integers from 1 to *n*. Additionally an integer is written on every vertex of this tree. Initially the integer written on the *i*-th vertex is equal to *v**i*. In one move you can apply the following operation:
1. Select the subtree of the given tree that includes the vertex with number 1. 1. Increase (or decrease) by one all the integers which are written on the vertices of that subtree.
Calculate the minimum number of moves that is required to make all the integers written on the vertices of the given tree equal to zero. | The first line of the input contains *n* (1<=β€<=*n*<=β€<=105). Each of the next *n*<=-<=1 lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*;Β *a**i*<=β <=*b**i*) indicating there's an edge between vertices *a**i* and *b**i*. It's guaranteed that the input graph is a tree.
The last line of the input contains a list of *n* space-separated integers *v*1,<=*v*2,<=...,<=*v**n* (|*v**i*|<=β€<=109). | Print the minimum number of operations needed to solve the task.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3\n1 2\n1 3\n1 -1 1\n"
] | [
"3\n"
] | none | [
{
"input": "3\n1 2\n1 3\n1 -1 1",
"output": "3"
},
{
"input": "5\n2 3\n4 5\n2 5\n1 3\n0 2 1 4 3",
"output": "8"
},
{
"input": "10\n5 6\n8 2\n9 3\n4 1\n6 10\n9 8\n7 10\n7 4\n5 2\n0 -6 -9 -1 -5 -4 -2 -7 -8 -3",
"output": "18"
},
{
"input": "5\n3 1\n2 4\n3 4\n2 5\n0 -3 -1 2 4",
... | 964 | 46,387,200 | 3 | 17,908 | |
46 | Hercule Poirot Problem | [
"dsu",
"graphs"
] | F. Hercule Poirot Problem | 2 | 256 | Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books.
You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has *n* rooms and *m* doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could
- open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side); - move freely from a room to a room if a corresponding door is open; - give keys to one another, being in one room.
"Little grey matter" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying. | The first line contains three preset integers *n*, *m* ΠΈ *k* (1<=β€<=*n*,<=*m*,<=*k*<=β€<=1000) β the number of rooms, the number of doors and the number of house residents respectively. The next *m* lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to *n*. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next *k* lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space β the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to *m* in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every *m* keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next *k* lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every *m* keys occurs exactly once. | Print "YES" (without quotes) if the second arrangement can result from the first one, otherwise, print "NO". | [
"2 1 2\n1 2\nDmitry 1 1 1\nNatalia 2 0\nNatalia 1 1 1\nDmitry 2 0\n",
"4 4 3\n1 3\n1 2\n2 3\n3 4\nArtem 1 1 4\nDmitry 1 1 2\nEdvard 4 2 1 3\nArtem 2 0\nDmitry 1 0\nEdvard 4 4 1 2 3 4\n"
] | [
"YES\n",
"NO\n"
] | none | [
{
"input": "2 1 2\n1 2\nDmitry 1 1 1\nNatalia 2 0\nNatalia 1 1 1\nDmitry 2 0",
"output": "YES"
},
{
"input": "4 4 3\n1 3\n1 2\n2 3\n3 4\nArtem 1 1 4\nDmitry 1 1 2\nEdvard 4 2 1 3\nArtem 2 0\nDmitry 1 0\nEdvard 4 4 1 2 3 4",
"output": "NO"
},
{
"input": "2 1 1\n2 1\nabsgdf 1 1 1\nabsgdf 1... | 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 | 7 | 8 | 9
In other words it's a correct arithmetic expression that is allowed to contain brackets, numbers (possibly with leading zeros), multiplications and additions. For example expressions "(0+01)", "0" and "1*(0)" are simplified arithmetic expressions, but expressions "2-1", "+1" and "1+2)" are not.
Given a string *s*1*s*2...*s*|*s*| that represents a SAE; *s**i* denotes the *i*-th character of the string which can be either a digit ('0'-'9'), a plus sign ('+'), a multiplication sign ('*'), an opening round bracket '(' or a closing round bracket ')'.
A part *s**l**s**l*<=+<=1...*s**r* of this string is called a sub-expression if and only if it is a SAE.
You task is to answer *m* queries, each of which is a pair of integers *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=|*s*|). For each query determine whether the corresponding part of the given string is a sub-expression and in case it's a sub-expression calculate its value modulo 1000000007Β (109<=+<=7). The values should be calculated using standard operator priorities. | The first line of the input contains non-empty string *s* (1<=β€<=|*s*|<=β€<=4Β·105) which represents a correct SAE. Each character of the string can be one of the following characters: '*', '+', '(', ')' or a digit ('0'-'9'). The expression might contain extra-huge numbers.
The second line contains an integer *m* (1<=β€<=*m*<=β€<=4Β·105) which is the number of queries. Each of the next *m* lines contains two space-separated integers *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=|*s*|) β the *i*-th query. | The *i*-th number of output should be the answer for the *i*-th query. If the *i*-th query corresponds to a valid sub-expression output the value of the sub-expression modulo 1000000007Β (109<=+<=7). Otherwise output -1 as an answer for the query. Print numbers on separate lines. | [
"((1+2)*3+101*2)\n6\n8 14\n1 6\n2 10\n11 14\n5 5\n4 5\n",
"(01)\n1\n1 4\n"
] | [
"205\n-1\n10\n2\n2\n-1\n",
"1\n"
] | none | [] | 3,993 | 921,600 | 0 | 17,932 | |
40 | Repaintings | [
"math"
] | B. Repaintings | 2 | 256 | A chessboard *n*<=Γ<=*m* in size is given. During the zero minute we repaint all the black squares to the 0 color. During the *i*-th minute we repaint to the *i* color the initially black squares that have exactly four corner-adjacent squares painted *i*<=-<=1 (all such squares are repainted simultaneously). This process continues ad infinitum. You have to figure out how many squares we repainted exactly *x* times.
The upper left square of the board has to be assumed to be always black. Two squares are called corner-adjacent, if they have exactly one common point. | The first line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=5000). The second line contains integer *x* (1<=β€<=*x*<=β€<=109). | Print how many squares will be painted exactly *x* times. | [
"3 3\n1\n",
"3 3\n2\n",
"1 1\n1\n"
] | [
"4\n",
"1\n",
"1\n"
] | none | [
{
"input": "3 3\n1",
"output": "4"
},
{
"input": "3 3\n2",
"output": "1"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "8 8\n8",
"output": "0"
},
{
"input": "9 10\n1",
"output": "17"
},
{
"input": "9 9\n3",
"output": "8"
},
{
"input":... | 92 | 0 | 3.977 | 18,055 |
159 | Friends or Not | [
"*special",
"greedy",
"implementation"
] | null | null | Polycarpus has a hobby β he develops an unusual social network. His work is almost completed, and there is only one more module to implement β the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user *A* sent user *B* a message at time *t*1, and user *B* sent user *A* a message at time *t*2. If 0<=<<=*t*2<=-<=*t*1<=β€<=*d*, then user *B*'s message was an answer to user *A*'s one. Users *A* and *B* are considered to be friends if *A* answered at least one *B*'s message or *B* answered at least one *A*'s message.
You are given the log of messages in chronological order and a number *d*. Find all pairs of users who will be considered to be friends. | The first line of the input contains two integers *n* and *d* (1<=β€<=*n*,<=*d*<=β€<=1000). The next *n* lines contain the messages log. The *i*-th line contains one line of the log formatted as "*A**i* *B**i* *t**i*" (without the quotes), which means that user *A**i* sent a message to user *B**i* at time *t**i* (1<=β€<=*i*<=β€<=*n*). *A**i* and *B**i* are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and *t**i* is an integer (0<=β€<=*t**i*<=β€<=10000). It is guaranteed that the lines are given in non-decreasing order of *t**i*'s and that no user sent a message to himself. The elements in the lines are separated by single spaces. | In the first line print integer *k* β the number of pairs of friends. In the next *k* lines print pairs of friends as "*A**i* *B**i*" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. | [
"4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4\n",
"1 1000\na b 0\n"
] | [
"1\npetya vasya\n",
"0\n"
] | In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. | [
{
"input": "4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4",
"output": "1\npetya vasya"
},
{
"input": "1 1000\na b 0",
"output": "0"
},
{
"input": "2 1\na b 0\nb a 0",
"output": "0"
},
{
"input": "3 1\na b 1\nb c 2\nc d 3",
"output": "0"
},
{
"input":... | 124 | 0 | -1 | 18,226 | |
120 | Three Sons | [
"brute force"
] | null | null | Three sons inherited from their father a rectangular corn fiend divided into *n*<=Γ<=*m* squares. For each square we know how many tons of corn grows on it. The father, an old farmer did not love all three sons equally, which is why he bequeathed to divide his field into three parts containing *A*, *B* and *C* tons of corn.
The field should be divided by two parallel lines. The lines should be parallel to one side of the field and to each other. The lines should go strictly between the squares of the field. Each resulting part of the field should consist of at least one square.
Your task is to find the number of ways to divide the field as is described above, that is, to mark two lines, dividing the field in three parts so that on one of the resulting parts grew *A* tons of corn, *B* on another one and *C* on the remaining one. | The first line contains space-separated integers *n* and *m* β the sizes of the original (1<=β€<=*n*,<=*m*<=β€<=50,<=*max*(*n*,<=*m*)<=β₯<=3). Then the field's description follows: *n* lines, each containing *m* space-separated integers *c**ij*, (0<=β€<=*c**ij*<=β€<=100) β the number of tons of corn each square contains. The last line contains space-separated integers *A*,<=*B*,<=*C* (0<=β€<=*A*,<=*B*,<=*C*<=β€<=106). | Print the answer to the problem: the number of ways to divide the father's field so that one of the resulting parts contained *A* tons of corn, another one contained *B* tons, and the remaining one contained *C* tons. If no such way exists, print 0. | [
"3 3\n1 1 1\n1 1 1\n1 1 1\n3 3 3\n",
"2 5\n1 1 1 1 1\n2 2 2 2 2\n3 6 6\n",
"3 3\n1 2 3\n3 1 2\n2 3 1\n5 6 7\n"
] | [
"2\n",
"3\n",
"0\n"
] | The lines dividing the field can be horizontal or vertical, but they should be parallel to each other. | [
{
"input": "3 3\n1 1 1\n1 1 1\n1 1 1\n3 3 3",
"output": "2"
},
{
"input": "2 5\n1 1 1 1 1\n2 2 2 2 2\n3 6 6",
"output": "3"
},
{
"input": "3 3\n1 2 3\n3 1 2\n2 3 1\n5 6 7",
"output": "0"
},
{
"input": "3 3\n0 0 0\n0 0 1\n1 1 0\n2 1 0",
"output": "1"
},
{
"input": ... | 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 at least *m*<=-<=1.
Denote *dp**k*(*u*) as maximum depth of *k*-ary heap in the subtree of *u* (including *u*). Your goal is to compute . | The first line contains an integer *n* denoting the size of the tree (2<=β€<=*n*<=β€<=3Β·105).
The next *n*<=-<=1 lines contain two integers *u*, *v* each, describing vertices connected by *i*-th edge.
It's guaranteed that the given configuration forms a tree. | Output the answer to the task. | [
"4\n1 3\n2 3\n4 3\n",
"4\n1 2\n2 3\n3 4\n"
] | [
"21\n",
"22\n"
] | Consider sample case one.
For *k*ββ₯β3 all *dp*<sub class="lower-index">*k*</sub> will be equal to 1.
For *k*β=β2 *dp*<sub class="lower-index">*k*</sub> is 2 if <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0a527e618740bc1e83327ce591b684aa4351c914.png" style="max-width: 100.0%;max-height: 100.0%;"/> and 1 otherwise.
For *k*β=β1 *dp*<sub class="lower-index">*k*</sub> values are (3,β1,β2,β1) respectively.
To sum up, 4Β·1β+β4Β·1β+β2Β·2β+β2Β·1β+β3β+β1β+β2β+β1β=β21. | [] | 30 | 0 | 0 | 18,371 | |
954 | Matrix Walk | [
"implementation"
] | null | null | There is a matrix *A* of size *x*<=Γ<=*y* filled with integers. For every , *A**i*,<=*j*<==<=*y*(*i*<=-<=1)<=+<=*j*. Obviously, every integer from [1..*xy*] occurs exactly once in this matrix.
You have traversed some path in this matrix. Your path can be described as a sequence of visited cells *a*1, *a*2, ..., *a**n* denoting that you started in the cell containing the number *a*1, then moved to the cell with the number *a*2, and so on.
From the cell located in *i*-th line and *j*-th column (we denote this cell as (*i*,<=*j*)) you can move into one of the following cells:
1. (*i*<=+<=1,<=*j*) β only if *i*<=<<=*x*; 1. (*i*,<=*j*<=+<=1) β only if *j*<=<<=*y*; 1. (*i*<=-<=1,<=*j*) β only if *i*<=><=1; 1. (*i*,<=*j*<=-<=1) β only if *j*<=><=1.
Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know *x* and *y* exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer *a*1, then move to the cell containing *a*2 (in one step), then move to the cell containing *a*3 (also in one step) and so on. Can you choose *x* and *y* so that they don't contradict with your sequence of moves? | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=200000) β the number of cells you visited on your path (if some cell is visited twice, then it's listed twice).
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109) β the integers in the cells on your path. | If all possible values of *x* and *y* such that 1<=β€<=*x*,<=*y*<=β€<=109 contradict with the information about your path, print NO.
Otherwise, print YES in the first line, and in the second line print the values *x* and *y* such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109. | [
"8\n1 2 3 6 9 8 5 2\n",
"6\n1 2 1 2 5 3\n",
"2\n1 10\n"
] | [
"YES\n3 3\n",
"NO\n",
"YES\n4 9\n"
] | The matrix and the path on it in the first test looks like this:
Also there exist multiple correct answers for both the first and the third examples. | [
{
"input": "8\n1 2 3 6 9 8 5 2",
"output": "YES\n1000000000 3"
},
{
"input": "6\n1 2 1 2 5 3",
"output": "NO"
},
{
"input": "2\n1 10",
"output": "YES\n1000000000 9"
},
{
"input": "3\n1 2 2",
"output": "NO"
},
{
"input": "1\n1",
"output": "YES\n1000000000 1"
... | 124 | 26,828,800 | 3 | 18,446 | |
43 | Lucky Tickets | [
"greedy"
] | C. Lucky Tickets | 2 | 256 | Vasya thinks that lucky tickets are the tickets whose numbers are divisible by 3. He gathered quite a large collection of such tickets but one day his younger brother Leonid was having a sulk and decided to destroy the collection. First he tore every ticket exactly in two, but he didnβt think it was enough and Leonid also threw part of the pieces away. Having seen this, Vasya got terrified but still tried to restore the collection. He chose several piece pairs and glued each pair together so that each pair formed a lucky ticket. The rest of the pieces Vasya threw away reluctantly. Thus, after the gluing of the 2*t* pieces he ended up with *t* tickets, each of which was lucky.
When Leonid tore the tickets in two pieces, one piece contained the first several letters of his number and the second piece contained the rest.
Vasya can glue every pair of pieces in any way he likes, but it is important that he gets a lucky ticket in the end. For example, pieces 123 and 99 can be glued in two ways: 12399 and 99123.
What maximum number of tickets could Vasya get after that? | The first line contains integer *n* (1<=β€<=*n*<=β€<=104) β the number of pieces. The second line contains *n* space-separated numbers *a**i* (1<=β€<=*a**i*<=β€<=108) β the numbers on the pieces. Vasya can only glue the pieces in pairs. Even if the number of a piece is already lucky, Vasya should glue the piece with some other one for it to count as lucky. Vasya does not have to use all the pieces. The numbers on the pieces an on the resulting tickets may coincide. | Print the single number β the maximum number of lucky tickets that will be able to be restored. Don't forget that every lucky ticket is made of exactly two pieces glued together. | [
"3\n123 123 99\n",
"6\n1 1 1 23 10 3\n"
] | [
"1\n",
"1\n"
] | none | [
{
"input": "3\n123 123 99",
"output": "1"
},
{
"input": "6\n1 1 1 23 10 3",
"output": "1"
},
{
"input": "3\n43440907 58238452 82582355",
"output": "1"
},
{
"input": "4\n31450303 81222872 67526764 17516401",
"output": "1"
},
{
"input": "5\n83280 20492640 21552119 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**i* (1<=β€<=*p**i*<=β€<=*n*).
Vitaly wants the row to have exactly *k* cards left. He also wants the *i*-th card from left to have number *b**i* written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation *n*<=-<=*k* times.
In one remove operation George can choose *w* (1<=β€<=*w*; *w* is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as *x*1,<=*x*2,<=...,<=*x**w* (from the left to the right). After that, George can remove the card *x**i*, such that *x**i*<=β€<=*x**j* for each *j* (1<=β€<=*j*<=β€<=*w*). After the described operation George gets *w* pieces of sausage.
George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question! | The first line contains integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=106) β the initial and the final number of cards.
The second line contains *n* distinct space-separated integers *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*) β the initial row of cards.
The third line contains *k* space-separated integers *b*1,<=*b*2,<=...,<=*b**k* β the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for *n*<=-<=*k* times. | Print a single integer β the maximum number of pieces of sausage that George can get if he acts optimally well. | [
"3 2\n2 1 3\n1 3\n",
"10 5\n1 2 3 4 5 6 7 8 9 10\n2 4 6 8 10\n"
] | [
"1\n",
"30\n"
] | none | [] | 62 | 0 | 0 | 18,527 | |
0 | none | [
"none"
] | null | null | Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward).
Ivan has beads of *n* colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace. | The first line of the input contains a single number *n* (1<=β€<=*n*<=β€<=26) β the number of colors of beads. The second line contains after *n* positive integers *a**i* Β β the quantity of beads of *i*-th color. It is guaranteed that the sum of *a**i* is at least 2 and does not exceed 100<=000. | In the first line print a single numberΒ β the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace.
Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point. | [
"3\n4 2 1\n",
"1\n4\n",
"2\n1 1\n"
] | [
"1\nabacaba",
"4\naaaa\n",
"0\nab\n"
] | In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture.
In the second sample there is only one way to compose a necklace. | [
{
"input": "3\n4 2 1",
"output": "1\naabcbaa"
},
{
"input": "1\n4",
"output": "4\naaaa"
},
{
"input": "2\n1 1",
"output": "0\nab"
},
{
"input": "1\n2",
"output": "2\naa"
},
{
"input": "1\n3",
"output": "3\naaa"
},
{
"input": "1\n5",
"output": "5\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 beautiful arrays there are. Chris thinks that an array is beautiful if it meets one of the two conditions:
- each elements, starting from the second one, is no more than the preceding one - each element, starting from the second one, is no less than the preceding one
Having got absolutely mad at himself and at math, Chris came to Stewie and Brian to ask them for help. However, they only laughed at him and said that the answer is too simple and not interesting. Help Chris the Rabbit to find the answer at last. | The single line contains an integer *n* which is the size of the array (1<=β€<=*n*<=β€<=105). | You must print the answer on a single line. As it can be rather long, you should print it modulo 1000000007. | [
"2\n",
"3\n"
] | [
"4\n",
"17\n"
] | none | [
{
"input": "2",
"output": "4"
},
{
"input": "3",
"output": "17"
},
{
"input": "12",
"output": "2704144"
},
{
"input": "19",
"output": "345263536"
},
{
"input": "20",
"output": "846527841"
},
{
"input": "26",
"output": "529476652"
},
{
"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 hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:
1. Only segments already presented on the picture can be painted; 1. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; 1. The numbers of points from the beginning of the tail to the end should strictly increase.
Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.
Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications. | First line of the input contains two integers *n* and *m*(2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=200<=000)Β β the number of points and the number segments on the picture respectively.
Then follow *m* lines, each containing two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*)Β β the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points. | Print the maximum possible value of the hedgehog's beauty. | [
"8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7\n",
"4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n"
] | [
"9\n",
"12\n"
] | The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3Β·3β=β9.
<img class="tex-graphics" src="https://espresso.codeforces.com/b3601595b30564928c0ce72c4d371941925f12e3.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7",
"output": "9"
},
{
"input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4",
"output": "12"
},
{
"input": "5 7\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2",
"output": "9"
},
{
"input": "5 9\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2\n1 5\n2 5",
"output"... | 3,000 | 114,176,000 | 0 | 18,677 | |
51 | Cheaterius's Problem | [
"implementation"
] | A. Cheaterius's Problem | 2 | 256 | Cheaterius is a famous in all the Berland astrologist, magician and wizard, and he also is a liar and a cheater. One of his latest inventions is Cheaterius' amulets! They bring luck and wealth, but are rather expensive. Cheaterius makes them himself. The technology of their making is kept secret. But we know that throughout long nights Cheaterius glues together domino pairs with super glue to get squares 2<=Γ<=2 which are the Cheaterius' magic amulets!
After a hard night Cheaterius made *n* amulets. Everyone of them represents a square 2<=Γ<=2, every quarter contains 1 to 6 dots. Now he wants sort them into piles, every pile must contain similar amulets. Two amulets are called similar if they can be rotated by 90, 180 or 270 degrees so that the following condition is met: the numbers of dots in the corresponding quarters should be the same. It is forbidden to turn over the amulets.
Write a program that by the given amulets will find the number of piles on Cheaterius' desk. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=1000), where *n* is the number of amulets. Then the amulet's descriptions are contained. Every description occupies two lines and contains two numbers (from 1 to 6) in each line. Between every pair of amulets the line "**" is located. | Print the required number of piles. | [
"4\n31\n23\n**\n31\n23\n**\n13\n32\n**\n32\n13\n",
"4\n51\n26\n**\n54\n35\n**\n25\n61\n**\n45\n53\n"
] | [
"1\n",
"2\n"
] | none | [
{
"input": "4\n31\n23\n**\n31\n23\n**\n13\n32\n**\n32\n13",
"output": "1"
},
{
"input": "4\n51\n26\n**\n54\n35\n**\n25\n61\n**\n45\n53",
"output": "2"
},
{
"input": "4\n56\n61\n**\n31\n31\n**\n33\n11\n**\n11\n33",
"output": "2"
},
{
"input": "4\n36\n44\n**\n32\n46\n**\n66\n41... | 92 | 0 | 3.977 | 18,683 |
80 | Depression | [
"geometry",
"math"
] | B. Depression | 1 | 256 | Do you remember a kind cartoon "Beauty and the Beast"? No, no, there was no firing from machine guns or radiation mutants time-travels!
There was a beauty named Belle. Once she had violated the Beast's order and visited the West Wing. After that she was banished from the castle...
Everybody was upset. The beautiful Belle was upset, so was the Beast, so was Lumiere the candlestick. But the worst thing was that Cogsworth was upset. Cogsworth is not a human, but is the mantel clock, which was often used as an alarm clock.
Due to Cogsworth's frustration all the inhabitants of the castle were in trouble: now they could not determine when it was time to drink morning tea, and when it was time for an evening stroll.
Fortunately, deep in the basement are lying digital clock showing the time in the format HH:MM. Now the residents of the castle face a difficult task. They should turn Cogsworth's hour and minute mustache hands in such a way, that Cogsworth began to show the correct time. Moreover they need to find turn angles in degrees for each mustache hands. The initial time showed by Cogsworth is 12:00.
You can only rotate the hands forward, that is, as is shown in the picture:
As since there are many ways too select such angles because of full rotations, choose the smallest angles in the right (non-negative) direction.
Note that Cogsworth's hour and minute mustache hands move evenly and continuously. Hands are moving independently, so when turning one hand the other hand remains standing still. | The only line of input contains current time according to the digital clock, formatted as HH:MM (00<=β€<=HH<=β€<=23, 00<=β€<=MM<=β€<=59). The mantel clock initially shows 12:00.
Pretests contain times of the beginning of some morning TV programs of the Channel One Russia. | Print two numbers *x* and *y* β the angles of turning the hour and minute hands, respectively (0<=β€<=*x*,<=*y*<=<<=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 divided by 1013.
John got interested in number *f* (0<=β€<=*f*<=<<=1013) and now wants to find its first occurrence in the list given above. Help John and find the number of the first occurence of number *f* in the list or otherwise state that number *f* does not occur in the list.
The numeration in John's list starts from zero. There, the 0-th position is the number 0, the 1-st position is the number 1, the 2-nd position is the number 1, the 3-rd position is the number 2, the 4-th position is the number 3 and so on. Thus, the beginning of the list looks like this: 0,<=1,<=1,<=2,<=3,<=5,<=8,<=13,<=21,<=... | The first line contains the single integer *f* (0<=β€<=*f*<=<<=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 by the number of edges path in the tree between vertices *v* and *u* as *d*(*v*,<=*u*).
A parent of vertex *v* in the rooted tree with the root in vertex *r* (*v*<=β <=*r*) is vertex *p**v*, such that *d*(*r*,<=*p**v*)<=+<=1<==<=*d*(*r*,<=*v*) and *d*(*p**v*,<=*v*)<==<=1. For example, on the picture the parent of vertex *v*<==<=5 is vertex *p*5<==<=2.
One day Polycarpus came across a rooted tree, consisting of *n* vertices. The tree wasn't exactly ordinary: it had strings written on its edges. Polycarpus positioned the tree on the plane so as to make all edges lead from top to bottom if you go from the vertex parent to the vertex (see the picture). For any edge that lead from vertex *p**v* to vertex *v* (1<=<<=*v*<=β€<=*n*), he knows string *s**v* that is written on it. All strings are written on the edges from top to bottom. For example, on the picture *s*7="ba". The characters in the strings are numbered starting from 0.
Polycarpus defines the position in this tree as a specific letter on a specific string. The position is written as a pair of integers (*v*,<=*x*) that means that the position is the *x*-th letter of the string *s**v* (1<=<<=*v*<=β€<=*n*, 0<=β€<=*x*<=<<=|*s**v*|), where |*s**v*| is the length of string *s**v*. For example, the highlighted letters are positions (2,<=1) and (3,<=1).
Let's consider the pair of positions (*v*,<=*x*) and (*u*,<=*y*) in Polycarpus' tree, such that the way from the first position to the second goes down on each step. We will consider that the pair of such positions defines string *z*. String *z* consists of all letters on the way from (*v*,<=*x*) to (*u*,<=*y*), written in the order of this path. For example, in the picture the highlighted positions define string "bacaba".
Polycarpus has a string *t*, he wants to know the number of pairs of positions that define string *t*. Note that the way from the first position to the second in the pair must go down everywhere. Help him with this challenging tree-string problem! | The first line contains integer *n* (2<=β€<=*n*<=β€<=105) β the number of vertices of Polycarpus's tree. Next *n*<=-<=1 lines contain the tree edges. The *i*-th of them contains number *p**i*<=+<=1 and string *s**i*<=+<=1 (1<=β€<=*p**i*<=+<=1<=β€<=*n*;Β *p**i*<=+<=1<=β <=(*i*<=+<=1)). String *s**i*<=+<=1 is non-empty and consists of lowercase English letters. The last line contains string *t*. String *t* consists of lowercase English letters, its length is at least 2.
It is guaranteed that the input contains at most 3Β·105 English letters. | Print a single integer β the required number.
Please, do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\naba\n",
"7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\nbacaba\n"
] | [
"6\n",
"4\n"
] | In the first test case string "aba" is determined by the pairs of positions: (2, 0) and (5, 0); (5, 2) and (6, 1); (5, 2) and (3, 1); (4, 0) and (4, 2); (4, 4) and (4, 6); (3, 3) and (3, 5).
Note that the string is not defined by the pair of positions (7, 1) and (5, 0), as the way between them doesn't always go down. | [
{
"input": "7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\naba",
"output": "6"
},
{
"input": "7\n1 ab\n5 bacaba\n1 abacaba\n2 aca\n5 ba\n2 ba\nbacaba",
"output": "4"
},
{
"input": "4\n1 a\n4 b\n1 b\naab",
"output": "0"
},
{
"input": "4\n1 a\n1 b\n1 a\nbba",
"output": "0... | 46 | 0 | 0 | 18,743 | |
850 | Arpa and a game with Mojtaba | [
"bitmasks",
"dp",
"games"
] | null | null | Mojtaba and Arpa are playing a game. They have a list of *n* numbers in the game.
In a player's turn, he chooses a number *p**k* (where *p* is a prime number and *k* is a positive integer) such that *p**k* divides at least one number in the list. For each number in the list divisible by *p**k*, call it *x*, the player will delete *x* and add to the list. The player who can not make a valid choice of *p* and *k* loses.
Mojtaba starts the game and the players alternatively make moves. Determine which one of players will be the winner if both players play optimally. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of elements in the list.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109)Β β the elements of the list. | If Mojtaba wins, print "Mojtaba", otherwise print "Arpa" (without quotes).
You can print each letter in any case (upper or lower). | [
"4\n1 1 1 1\n",
"4\n1 1 17 17\n",
"4\n1 1 17 289\n",
"5\n1 2 3 4 5\n"
] | [
"Arpa\n",
"Mojtaba\n",
"Arpa\n",
"Arpa\n"
] | In the first sample test, Mojtaba can't move.
In the second sample test, Mojtaba chooses *p*β=β17 and *k*β=β1, then the list changes to [1,β1,β1,β1].
In the third sample test, if Mojtaba chooses *p*β=β17 and *k*β=β1, then Arpa chooses *p*β=β17 and *k*β=β1 and wins, if Mojtaba chooses *p*β=β17 and *k*β=β2, then Arpa chooses *p*β=β17 and *k*β=β1 and wins. | [
{
"input": "4\n1 1 1 1",
"output": "Arpa"
},
{
"input": "4\n1 1 17 17",
"output": "Mojtaba"
},
{
"input": "4\n1 1 17 289",
"output": "Arpa"
},
{
"input": "5\n1 2 3 4 5",
"output": "Arpa"
},
{
"input": "10\n10 14 16 9 17 13 12 4 6 10",
"output": "Mojtaba"
},
... | 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 no ends of some segments that coincide. | Print *n* lines. The *j*-th of them should contain the only integer *a**j* β the number of segments contained in the *j*-th segment. | [
"4\n1 8\n2 3\n4 7\n5 6\n",
"3\n3 4\n1 5\n2 6\n"
] | [
"3\n0\n1\n0\n",
"0\n1\n1\n"
] | none | [
{
"input": "4\n1 8\n2 3\n4 7\n5 6",
"output": "3\n0\n1\n0"
},
{
"input": "3\n3 4\n1 5\n2 6",
"output": "0\n1\n1"
},
{
"input": "1\n-1000000000 1000000000",
"output": "0"
},
{
"input": "2\n-1000000000 999999999\n-999999999 1000000000",
"output": "0\n0"
},
{
"input"... | 62 | 4,608,000 | 0 | 18,790 | |
710 | String Set Queries | [
"brute force",
"data structures",
"hashing",
"interactive",
"string suffix structures",
"strings"
] | null | null | You should process *m* queries over a set *D* of strings. Each query is one of three kinds:
1. Add a string *s* to the set *D*. It is guaranteed that the string *s* was not added before. 1. Delete a string *s* from the set *D*. It is guaranteed that the string *s* is in the set *D*. 1. For the given string *s* find the number of occurrences of the strings from the set *D*. If some string *p* from *D* has several occurrences in *s* you should count all of them.
Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query of the third type. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program. | The first line contains integer *m* (1<=β€<=*m*<=β€<=3Β·105) β the number of queries.
Each of the next *m* lines contains integer *t* (1<=β€<=*t*<=β€<=3) and nonempty string *s* β the kind of the query and the string to process. All strings consist of only lowercase English letters.
The sum of lengths of all strings in the input will not exceed 3Β·105. | For each query of the third kind print the only integer *c* β the desired number of occurrences in the string *s*. | [
"5\n1 abc\n3 abcabc\n2 abc\n1 aba\n3 abababc\n",
"10\n1 abc\n1 bcd\n1 abcd\n3 abcd\n2 abcd\n3 abcd\n2 bcd\n3 abcd\n2 abc\n3 abcd\n"
] | [
"2\n2\n",
"3\n2\n1\n0\n"
] | none | [
{
"input": "5\n1 abc\n3 abcabc\n2 abc\n1 aba\n3 abababc",
"output": "2\n2"
},
{
"input": "10\n1 abc\n1 bcd\n1 abcd\n3 abcd\n2 abcd\n3 abcd\n2 bcd\n3 abcd\n2 abc\n3 abcd",
"output": "3\n2\n1\n0"
},
{
"input": "2\n1 abbaaabbbababbaaabbbbb\n3 bbbbbbabbbabaabbbbabbb",
"output": "0"
},
... | 3,000 | 15,155,200 | 0 | 18,812 | |
785 | Anton and Classes | [
"greedy",
"sortings"
] | null | null | Anton likes to play chess. Also he likes to do programming. No wonder that he decided to attend chess classes and programming classes.
Anton has *n* variants when he will attend chess classes, *i*-th variant is given by a period of time (*l*1,<=*i*,<=*r*1,<=*i*). Also he has *m* variants when he will attend programming classes, *i*-th variant is given by a period of time (*l*2,<=*i*,<=*r*2,<=*i*).
Anton needs to choose exactly one of *n* possible periods of time when he will attend chess classes and exactly one of *m* possible periods of time when he will attend programming classes. He wants to have a rest between classes, so from all the possible pairs of the periods he wants to choose the one where the distance between the periods is maximal.
The distance between periods (*l*1,<=*r*1) and (*l*2,<=*r*2) is the minimal possible distance between a point in the first period and a point in the second period, that is the minimal possible |*i*<=-<=*j*|, where *l*1<=β€<=*i*<=β€<=*r*1 and *l*2<=β€<=*j*<=β€<=*r*2. In particular, when the periods intersect, the distance between them is 0.
Anton wants to know how much time his rest between the classes will last in the best case. Help Anton and find this number! | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of time periods when Anton can attend chess classes.
Each of the following *n* lines of the input contains two integers *l*1,<=*i* and *r*1,<=*i* (1<=β€<=*l*1,<=*i*<=β€<=*r*1,<=*i*<=β€<=109)Β β the *i*-th variant of a period of time when Anton can attend chess classes.
The following line of the input contains a single integer *m* (1<=β€<=*m*<=β€<=200<=000)Β β the number of time periods when Anton can attend programming classes.
Each of the following *m* lines of the input contains two integers *l*2,<=*i* and *r*2,<=*i* (1<=β€<=*l*2,<=*i*<=β€<=*r*2,<=*i*<=β€<=109)Β β the *i*-th variant of a period of time when Anton can attend programming classes. | Output one integerΒ β the maximal possible distance between time periods. | [
"3\n1 5\n2 6\n2 3\n2\n2 4\n6 8\n",
"3\n1 5\n2 6\n3 7\n2\n2 4\n1 4\n"
] | [
"3\n",
"0\n"
] | In the first sample Anton can attend chess classes in the period (2,β3) and attend programming classes in the period (6,β8). It's not hard to see that in this case the distance between the periods will be equal to 3.
In the second sample if he chooses any pair of periods, they will intersect. So the answer is 0. | [
{
"input": "3\n1 5\n2 6\n2 3\n2\n2 4\n6 8",
"output": "3"
},
{
"input": "3\n1 5\n2 6\n3 7\n2\n2 4\n1 4",
"output": "0"
},
{
"input": "20\n13 141\n57 144\n82 124\n16 23\n18 44\n64 65\n117 133\n84 117\n77 142\n40 119\n105 120\n71 92\n5 142\n48 132\n106 121\n5 80\n45 92\n66 81\n7 93\n27 71\... | 2,308 | 12,185,600 | 3 | 18,827 | |
467 | Fedor and New Game | [
"bitmasks",
"brute force",
"constructive algorithms",
"implementation"
] | null | null | After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play a new computer game Β«Call of Soldiers 3Β».
The game has (*m*<=+<=1) players and *n* types of soldiers in total. Players Β«Call of Soldiers 3Β» are numbered form 1 to (*m*<=+<=1). Types of soldiers are numbered from 0 to *n*<=-<=1. Each player has an army. Army of the *i*-th player can be described by non-negative integer *x**i*. Consider binary representation of *x**i*: if the *j*-th bit of number *x**i* equal to one, then the army of the *i*-th player has soldiers of the *j*-th type.
Fedor is the (*m*<=+<=1)-th player of the game. He assume that two players can become friends if their armies differ in at most *k* types of soldiers (in other words, binary representations of the corresponding numbers differ in at most *k* bits). Help Fedor and count how many players can become his friends. | The first line contains three integers *n*, *m*, *k* (1<=β€<=*k*<=β€<=*n*<=β€<=20;Β 1<=β€<=*m*<=β€<=1000).
The *i*-th of the next (*m*<=+<=1) lines contains a single integer *x**i* (1<=β€<=*x**i*<=β€<=2*n*<=-<=1), that describes the *i*-th player's army. We remind you that Fedor is the (*m*<=+<=1)-th player. | Print a single integer β the number of Fedor's potential friends. | [
"7 3 1\n8\n5\n111\n17\n",
"3 3 3\n1\n2\n3\n4\n"
] | [
"0\n",
"3\n"
] | none | [
{
"input": "7 3 1\n8\n5\n111\n17",
"output": "0"
},
{
"input": "3 3 3\n1\n2\n3\n4",
"output": "3"
},
{
"input": "4 2 2\n5\n6\n7",
"output": "2"
},
{
"input": "4 7 4\n9\n10\n5\n12\n4\n12\n7\n10",
"output": "7"
},
{
"input": "2 7 2\n1\n1\n1\n1\n1\n1\n1\n1",
"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 possible unions of two different sets on *n*Β·(*n*<=-<=1)<=/<=2 pieces of paper. Then he shuffled the pieces of paper. He had written out the numbers in the unions in an arbitrary order.
For example, if *n*<==<=4, and the actual sets have the following form {1,<=3}, {5}, {2,<=4}, {7}, then the number of set pairs equals to six. The six pieces of paper can contain the following numbers:
- 2,<=7,<=4. - 1,<=7,<=3; - 5,<=4,<=2; - 1,<=3,<=5; - 3,<=1,<=2,<=4; - 5,<=7.
Then Vasya showed the pieces of paper to his friends, but kept the *n* sets secret from them. His friends managed to calculate which sets Vasya had thought of in the first place. And how about you, can you restore the sets by the given pieces of paper? | The first input file line contains a number *n* (2<=β€<=*n*<=β€<=200), *n* is the number of sets at Vasya's disposal. Then follow sets of numbers from the pieces of paper written on *n*Β·(*n*<=-<=1)<=/<=2 lines. Each set starts with the number *k**i* (2<=β€<=*k**i*<=β€<=200), which is the number of numbers written of the *i*-th piece of paper, and then follow *k**i* numbers *a**ij* (1<=β€<=*a**ij*<=β€<=200). All the numbers on the lines are separated by exactly one space. It is guaranteed that the input data is constructed according to the above given rules from *n* non-intersecting sets. | Print on *n* lines Vasya's sets' description. The first number on the line shows how many numbers the current set has. Then the set should be recorded by listing its elements. Separate the numbers by spaces. Each number and each set should be printed exactly once. Print the sets and the numbers in the sets in any order. If there are several answers to that problem, print any of them.
It is guaranteed that there is a solution. | [
"4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7\n",
"4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2\n",
"3\n2 1 2\n2 1 3\n2 2 3\n"
] | [
"1 7 \n2 2 4 \n2 1 3 \n1 5 \n",
"3 7 8 9 \n2 6 100 \n1 1 \n1 2 \n",
"1 1 \n1 2 \n1 3 \n"
] | none | [
{
"input": "4\n3 2 7 4\n3 1 7 3\n3 5 4 2\n3 1 3 5\n4 3 1 2 4\n2 5 7",
"output": "1 7 \n2 2 4 \n2 1 3 \n1 5 "
},
{
"input": "4\n5 6 7 8 9 100\n4 7 8 9 1\n4 7 8 9 2\n3 1 6 100\n3 2 6 100\n2 1 2",
"output": "3 7 8 9 \n2 6 100 \n1 1 \n1 2 "
},
{
"input": "3\n2 1 2\n2 1 3\n2 2 3",
"output... | 1,466 | 2,457,600 | 0 | 18,838 |
12 | Start of the session | [
"constructive algorithms"
] | E. Start of the season | 2 | 256 | Before the start of the football season in Berland a strange magic ritual is held. The most experienced magicians have to find a magic matrix of the size *n*<=Γ<=*n* (*n* is even number). Gods will never allow to start the championship without it. Matrix should contain integers from 0 to *n*<=-<=1, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. | The first line contains one integer *n* (2<=β€<=*n*<=β€<=1000), *n* is even. | Output *n* lines with *n* numbers each β the required matrix. Separate numbers with spaces. If there are several solutions, output any. | [
"2\n",
"4\n"
] | [
"0 1\n1 0\n",
"0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0\n"
] | none | [
{
"input": "2",
"output": "0 1\n1 0"
},
{
"input": "4",
"output": "0 1 3 2\n1 0 2 3\n3 2 0 1\n2 3 1 0"
},
{
"input": "6",
"output": "0 1 4 2 5 3\n1 0 2 5 3 4\n4 2 0 3 1 5\n2 5 3 0 4 1\n5 3 1 4 0 2\n3 4 5 1 2 0"
},
{
"input": "8",
"output": "0 1 5 2 6 3 7 4\n1 0 2 6 3 7 4 ... | 92 | 0 | 0 | 18,844 |
0 | none | [
"none"
] | null | null | Codeforces is a wonderful platform and one its feature shows how much someone contributes to the community. Every registered user has contributionΒ β an integer number, not necessarily positive. There are *n* registered users and the *i*-th of them has contribution *t**i*.
Limak is a little polar bear and he's new into competitive programming. He doesn't even have an account in Codeforces but he is able to upvote existing blogs and comments. We assume that every registered user has infinitely many blogs and comments.
- Limak can spend *b* minutes to read one blog and upvote it. Author's contribution will be increased by 5. - Limak can spend *c* minutes to read one comment and upvote it. Author's contribution will be increased by 1.
Note that it's possible that Limak reads blogs faster than comments.
Limak likes ties. He thinks it would be awesome to see a tie between at least *k* registered users. To make it happen he is going to spend some time on reading and upvoting. After that, there should exist an integer value *x* that at least *k* registered users have contribution exactly *x*.
How much time does Limak need to achieve his goal? | The first line contains four integers *n*, *k*, *b* and *c* (2<=β€<=*k*<=β€<=*n*<=β€<=200<=000,<=1<=β€<=*b*,<=*c*<=β€<=1000)Β β the number of registered users, the required minimum number of users with the same contribution, time needed to read and upvote a blog, and time needed to read and upvote a comment, respectively.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (|*t**i*|<=β€<=109) where *t**i* denotes contribution of the *i*-th registered user. | Print the minimum number of minutes Limak will spend to get a tie between at least *k* registered users. | [
"4 3 100 30\n12 2 6 1\n",
"4 3 30 100\n12 2 6 1\n",
"6 2 987 789\n-8 42 -4 -65 -8 -8\n"
] | [
"220\n",
"190\n",
"0\n"
] | In the first sample, there are 4 registered users and Limak wants a tie between at least 3 of them. Limak should behave as follows.
- He spends 100 minutes to read one blog of the 4-th user and increase his contribution from 1 to 6. - Then he spends 4Β·30β=β120 minutes to read four comments of the 2-nd user and increase his contribution from 2 to 6 (four times it was increaded by 1).
In the given scenario, Limak spends 100β+β4Β·30β=β220 minutes and after that each of users 2,β3,β4 has contribution 6.
In the second sample, Limak needs 30 minutes to read a blog and 100 minutes to read a comment. This time he can get 3 users with contribution equal to 12 by spending 100β+β3Β·30β=β190 minutes:
- Spend 2Β·30β=β60 minutes to read two blogs of the 1-st user to increase his contribution from 2 to 12. - Spend 30β+β100 minutes to read one blog and one comment of the 3-rd user. His contribution will change from 6 to 6β+β5β+β1β=β12. | [] | 46 | 0 | 0 | 18,869 | |
670 | Cinema | [
"implementation",
"sortings"
] | null | null | Moscow is hosting a major international conference, which is attended by *n* scientists from different countries. Each of the scientists knows exactly one language. For convenience, we enumerate all languages of the world with integers from 1 to 109.
In the evening after the conference, all *n* scientists decided to go to the cinema. There are *m* movies in the cinema they came to. Each of the movies is characterized by two distinct numbersΒ β the index of audio language and the index of subtitles language. The scientist, who came to the movie, will be very pleased if he knows the audio language of the movie, will be almost satisfied if he knows the language of subtitles and will be not satisfied if he does not know neither one nor the other (note that the audio language and the subtitles language for each movie are always different).
Scientists decided to go together to the same movie. You have to help them choose the movie, such that the number of very pleased scientists is maximum possible. If there are several such movies, select among them one that will maximize the number of almost satisfied scientists. | The first line of the input contains a positive integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of scientists.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109), where *a**i* is the index of a language, which the *i*-th scientist knows.
The third line contains a positive integer *m* (1<=β€<=*m*<=β€<=200<=000)Β β the number of movies in the cinema.
The fourth line contains *m* positive integers *b*1,<=*b*2,<=...,<=*b**m* (1<=β€<=*b**j*<=β€<=109), where *b**j* is the index of the audio language of the *j*-th movie.
The fifth line contains *m* positive integers *c*1,<=*c*2,<=...,<=*c**m* (1<=β€<=*c**j*<=β€<=109), where *c**j* is the index of subtitles language of the *j*-th movie.
It is guaranteed that audio languages and subtitles language are different for each movie, that is *b**j*<=β <=*c**j*. | Print the single integerΒ β the index of a movie to which scientists should go. After viewing this movie the number of very pleased scientists should be maximum possible. If in the cinema there are several such movies, you need to choose among them one, after viewing which there will be the maximum possible number of almost satisfied scientists.
If there are several possible answers print any of them. | [
"3\n2 3 2\n2\n3 2\n2 3\n",
"6\n6 3 1 1 3 7\n5\n1 2 3 4 5\n2 3 4 5 1\n"
] | [
"2\n",
"1\n"
] | In the first sample, scientists must go to the movie with the index 2, as in such case the 1-th and the 3-rd scientists will be very pleased and the 2-nd scientist will be almost satisfied.
In the second test case scientists can go either to the movie with the index 1 or the index 3. After viewing any of these movies exactly two scientists will be very pleased and all the others will be not satisfied. | [
{
"input": "3\n2 3 2\n2\n3 2\n2 3",
"output": "2"
},
{
"input": "6\n6 3 1 1 3 7\n5\n1 2 3 4 5\n2 3 4 5 1",
"output": "1"
},
{
"input": "1\n10\n1\n10\n3",
"output": "1"
},
{
"input": "2\n1 6\n1\n6\n1",
"output": "1"
},
{
"input": "1\n5\n2\n2 2\n5 5",
"output": ... | 561 | 69,836,800 | 3 | 18,901 | |
107 | Basketball Team | [
"combinatorics",
"dp",
"math",
"probabilities"
] | B. Basketball Team | 1 | 256 | As a German University in Cairo (GUC) student and a basketball player, Herr Wafa was delighted once he heard the news. GUC is finally participating in the Annual Basketball Competition (ABC).
A team is to be formed of *n* players, all of which are GUC students. However, the team might have players belonging to different departments. There are *m* departments in GUC, numbered from 1 to *m*. Herr Wafa's department has number *h*. For each department *i*, Herr Wafa knows number *s**i* β how many students who play basketball belong to this department.
Herr Wafa was also able to guarantee a spot on the team, using his special powers. But since he hates floating-point numbers, he needs your help at finding the probability that he will have at least one teammate belonging to his department.
Note that every possible team containing Herr Wafa is equally probable. Consider all the students different from each other. | The first line contains three integers *n*, *m* and *h* (1<=β€<=*n*<=β€<=100,<=1<=β€<=*m*<=β€<=1000,<=1<=β€<=*h*<=β€<=*m*) β the number of players on the team, the number of departments in GUC and Herr Wafa's department, correspondingly.
The second line contains a single-space-separated list of *m* integers *s**i* (1<=β€<=*s**i*<=β€<=100), denoting the number of students in the *i*-th department. Note that *s**h* includes Herr Wafa. | Print the probability that Herr Wafa will have at least one teammate from his department. If there is not enough basketball players in GUC to participate in ABC, print -1. The answer will be accepted if it has absolute or relative error not exceeding 10<=-<=6. | [
"3 2 1\n2 1\n",
"3 2 1\n1 1\n",
"3 2 1\n2 2\n"
] | [
"1\n",
"-1\n",
"0.666667\n"
] | In the first example all 3 players (2 from department 1 and 1 from department 2) must be chosen for the team. Both players from Wafa's departments will be chosen, so he's guaranteed to have a teammate from his department.
In the second example, there are not enough players.
In the third example, there are three possibilities to compose the team containing Herr Wafa. In two of them the other player from Herr Wafa's department is part of the team. | [
{
"input": "3 2 1\n2 1",
"output": "1"
},
{
"input": "3 2 1\n1 1",
"output": "-1"
},
{
"input": "3 2 1\n2 2",
"output": "0.666667"
},
{
"input": "3 2 1\n1 2",
"output": "0.000000"
},
{
"input": "6 5 3\n5 2 3 10 5",
"output": "0.380435"
},
{
"input": "7... | 109 | 12,492,800 | 0 | 18,935 |
2 | Commentator problem | [
"geometry"
] | C. Commentator problem | 1 | 64 | The Olympic Games in Bercouver are in full swing now. Here everyone has their own objectives: sportsmen compete for medals, and sport commentators compete for more convenient positions to give a running commentary. Today the main sport events take place at three round stadiums, and the commentator's objective is to choose the best point of observation, that is to say the point from where all the three stadiums can be observed. As all the sport competitions are of the same importance, the stadiums should be observed at the same angle. If the number of points meeting the conditions is more than one, the point with the maximum angle of observation is prefered.
Would you, please, help the famous Berland commentator G. Berniev to find the best point of observation. It should be noted, that the stadiums do not hide each other, the commentator can easily see one stadium through the other. | The input data consists of three lines, each of them describes the position of one stadium. The lines have the format *x*,<=<=*y*,<=<=*r*, where (*x*,<=*y*) are the coordinates of the stadium's center (<=-<=<=103<=β€<=*x*,<=<=*y*<=β€<=103), and *r* (1<=β€<=*r*<=<=β€<=103) is its radius. All the numbers in the input data are integer, stadiums do not have common points, and their centers are not on the same line. | Print the coordinates of the required point with five digits after the decimal point. If there is no answer meeting the conditions, the program shouldn't print anything. The output data should be left blank. | [
"0 0 10\n60 0 10\n30 30 10\n"
] | [
"30.00000 0.00000\n"
] | none | [
{
"input": "0 0 10\n60 0 10\n30 30 10",
"output": "30.00000 0.00000"
},
{
"input": "0 0 10\n100 100 10\n200 0 20",
"output": "60.76252 39.23748"
},
{
"input": "0 0 10\n300 300 11\n500 -500 12",
"output": "348.52046 -94.13524"
},
{
"input": "0 0 10\n300 300 12\n500 -500 14",
... | 140 | 1,331,200 | 3.920082 | 18,940 |
962 | Visible Black Areas | [
"data structures",
"dsu",
"geometry",
"trees"
] | null | null | Petya has a polygon consisting of $n$ vertices. All sides of the Petya's polygon are parallel to the coordinate axes, and each two adjacent sides of the Petya's polygon are perpendicular. It is guaranteed that the polygon is simple, that is, it doesn't have self-intersections and self-touches. All internal area of the polygon (borders are not included) was painted in black color by Petya.
Also, Petya has a rectangular window, defined by its coordinates, through which he looks at the polygon. A rectangular window can not be moved. The sides of the rectangular window are parallel to the coordinate axes.
Determine the number of black connected areas of Petya's polygon, which can be seen through the rectangular window. | The first line contain four integers $x_1, y_1, x_2, y_2$ ($x_1 < 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$ lines contains two integers β the coordinates of vertices of the Petya's polygon in counterclockwise order. Guaranteed, that the given polygon satisfies the conditions described in the statement.
All coordinates of the rectangular window and all coordinates of the vertices of the polygon are non-negative and do not exceed $15\,000$. | Print the number of black connected areas of Petya's polygon, which can be seen through the rectangular window. | [
"5 7 16 3\n16\n0 0\n18 0\n18 6\n16 6\n16 1\n10 1\n10 4\n7 4\n7 2\n2 2\n2 6\n12 6\n12 12\n10 12\n10 8\n0 8\n"
] | [
"2"
] | The example corresponds to the picture above. | [
{
"input": "5 7 16 3\n16\n0 0\n18 0\n18 6\n16 6\n16 1\n10 1\n10 4\n7 4\n7 2\n2 2\n2 6\n12 6\n12 12\n10 12\n10 8\n0 8",
"output": "2"
},
{
"input": "4 5 6 3\n12\n1 1\n8 1\n8 7\n3 7\n3 6\n7 6\n7 2\n2 2\n2 8\n4 8\n4 9\n1 9",
"output": "0"
},
{
"input": "0 10 10 0\n12\n1 1\n8 1\n8 7\n3 7\n3 ... | 46 | 6,963,200 | 0 | 18,955 | |
609 | Minimum spanning tree for each edge | [
"data structures",
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | Connected undirected weighted graph without self-loops and multiple edges is given. Graph contains *n* vertices and *m* edges.
For each edge (*u*,<=*v*) find the minimal possible weight of the spanning tree that contains the edge (*u*,<=*v*).
The weight of the spanning tree is the sum of weights of all edges included in spanning tree. | First line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=2Β·105,<=*n*<=-<=1<=β€<=*m*<=β€<=2Β·105) β the number of vertices and edges in graph.
Each of the next *m* lines contains three integers *u**i*,<=*v**i*,<=*w**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=*u**i*<=β <=*v**i*,<=1<=β€<=*w**i*<=β€<=109) β the endpoints of the *i*-th edge and its weight. | Print *m* lines. *i*-th line should contain the minimal possible weight of the spanning tree that contains *i*-th edge.
The edges are numbered from 1 to *m* in order of their appearing in input. | [
"5 7\n1 2 3\n1 3 1\n1 4 5\n2 3 2\n2 5 3\n3 4 2\n4 5 4\n"
] | [
"9\n8\n11\n8\n8\n8\n9\n"
] | none | [
{
"input": "5 7\n1 2 3\n1 3 1\n1 4 5\n2 3 2\n2 5 3\n3 4 2\n4 5 4",
"output": "9\n8\n11\n8\n8\n8\n9"
},
{
"input": "2 1\n1 2 42",
"output": "42"
},
{
"input": "3 3\n1 2 10\n2 3 20\n3 1 40",
"output": "30\n30\n50"
},
{
"input": "4 6\n1 2 999999001\n1 3 999999003\n1 4 999999009\... | 2,000 | 227,225,600 | 0 | 18,957 | |
731 | Coupons and Discounts | [
"constructive algorithms",
"greedy"
] | null | null | The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.
Teams plan to train for *n* times during *n* consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be *a**i* teams on the *i*-th day.
There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).
As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.
Sereja wants to order exactly *a**i* pizzas on the *i*-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day *n*. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of training sessions.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=β€<=*a**i*<=β€<=10<=000)Β β the number of teams that will be present on each of the days. | If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes). | [
"4\n1 2 1 2\n",
"3\n1 0 1\n"
] | [
"YES\n",
"NO\n"
] | In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.
In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days. | [
{
"input": "4\n1 2 1 2",
"output": "YES"
},
{
"input": "3\n1 0 1",
"output": "NO"
},
{
"input": "3\n1 3 1",
"output": "NO"
},
{
"input": "3\n2 0 2",
"output": "YES"
},
{
"input": "1\n179",
"output": "NO"
},
{
"input": "10\n0 0 5 9 9 3 0 0 0 10",
"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 position *x**p**j* to position *x**p**j*<=+<=*d**j*. At that, it is guaranteed that after executing such query all coordinates of the points will be distinct. 1. Count the sum of distances between all pairs of points that lie on the segment [*l**j*,<=*r**j*] (*l**j*<=β€<=*r**j*). In other words, you should count the sum of: .
Help Yaroslav. | The first line contains integer *n* β the number of points (1<=β€<=*n*<=β€<=105). The second line contains distinct integers *x*1,<=*x*2,<=...,<=*x**n* β the coordinates of points (|*x**i*|<=β€<=109).
The third line contains integer *m* β the number of queries (1<=β€<=*m*<=β€<=105). The next *m* lines contain the queries. The *j*-th line first contains integer *t**j* (1<=β€<=*t**j*<=β€<=2) β the query type. If *t**j*<==<=1, then it is followed by two integers *p**j* and *d**j* (1<=β€<=*p**j*<=β€<=*n*,<=|*d**j*|<=β€<=1000). If *t**j*<==<=2, then it is followed by two integers *l**j* and *r**j* (<=-<=109<=β€<=*l**j*<=β€<=*r**j*<=β€<=109).
It is guaranteed that at any moment all the points have distinct coordinates. | For each type 2 query print the answer on a single line. Print the answers in the order, in which the queries follow in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams of the %I64d specifier. | [
"8\n36 50 28 -75 40 -60 -95 -48\n20\n2 -61 29\n1 5 -53\n1 1 429\n1 5 130\n2 -101 -71\n2 -69 53\n1 1 404\n1 5 518\n2 -101 53\n2 50 872\n1 1 -207\n2 -99 -40\n1 7 -389\n1 6 -171\n1 2 464\n1 7 -707\n1 1 -730\n1 1 560\n2 635 644\n1 7 -677\n"
] | [
"176\n20\n406\n1046\n1638\n156\n0\n"
] | none | [] | 92 | 0 | 0 | 19,001 | |
575 | Bots | [
"combinatorics",
"dp",
"math",
"number theory"
] | null | null | Sasha and Ira are two best friends. But they arenβt just friends, they are software engineers and experts in artificial intelligence. They are developing an algorithm for two bots playing a two-player game. The game is cooperative and turn based. In each turn, one of the players makes a move (it doesnβt matter which player, it's possible that players turns do not alternate).
Algorithm for bots that Sasha and Ira are developing works by keeping track of the state the game is in. Each time either bot makes a move, the state changes. And, since the game is very dynamic, it will never go back to the state it was already in at any point in the past.
Sasha and Ira are perfectionists and want their algorithm to have an optimal winning strategy. They have noticed that in the optimal winning strategy, both bots make exactly *N* moves each. But, in order to find the optimal strategy, their algorithm needs to analyze all possible states of the game (they havenβt learned about alpha-beta pruning yet) and pick the best sequence of moves.
They are worried about the efficiency of their algorithm and are wondering what is the total number of states of the game that need to be analyzed? | The first and only line contains integer N.
- 1<=β€<=*N*<=β€<=106 | Output should contain a single integer β number of possible states modulo 109<=+<=7. | [
"2\n"
] | [
"19\n"
] | Start: Game is in state A.
- Turn 1: Either bot can make a move (first bot is red and second bot is blue), so there are two possible states after the first turn β B and C. - Turn 2: In both states B and C, either bot can again make a turn, so the list of possible states is expanded to include D, E, F and G. - Turn 3: Red bot already did N=2 moves when in state D, so it cannot make any more moves there. It can make moves when in state E, F and G, so states I, K and M are added to the list. Similarly, blue bot cannot make a move when in state G, but can when in D, E and F, so states H, J and L are added. - Turn 4: Red bot already did N=2 moves when in states H, I and K, so it can only make moves when in J, L and M, so states P, R and S are added. Blue bot cannot make a move when in states J, L and M, but only when in H, I and K, so states N, O and Q are added.
Overall, there are 19 possible states of the game their algorithm needs to analyze.
<img class="tex-graphics" src="https://espresso.codeforces.com/3d9ef2ab59018319b986a58a65362116fa5be88d.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "2",
"output": "19"
},
{
"input": "1",
"output": "5"
},
{
"input": "3",
"output": "69"
},
{
"input": "4",
"output": "251"
},
{
"input": "5",
"output": "923"
},
{
"input": "6",
"output": "3431"
},
{
"input": "7",
"output": "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*. Let's split the phone number into blocks of length *k*. The first block will be formed by digits from the phone number that are on positions 1, 2,..., *k*, the second block will be formed by digits from the phone number that are on positions *k*<=+<=1, *k*<=+<=2, ..., 2Β·*k* and so on. Pasha considers a phone number good, if the *i*-th block doesn't start from the digit *b**i* and is divisible by *a**i* if represented as an integer.
To represent the block of length *k* as an integer, let's write it out as a sequence *c*1, *c*2,...,*c**k*. Then the integer is calculated as the result of the expression *c*1Β·10*k*<=-<=1<=+<=*c*2Β·10*k*<=-<=2<=+<=...<=+<=*c**k*.
Pasha asks you to calculate the number of good phone numbers of length *n*, for the given *k*, *a**i* and *b**i*. As this number can be too big, print it modulo 109<=+<=7. | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*k*<=β€<=*min*(*n*,<=9))Β β the length of all phone numbers and the length of each block, respectively. It is guaranteed that *n* is divisible by *k*.
The second line of the input contains *n*<=/<=*k* space-separated positive integersΒ β sequence *a*1,<=*a*2,<=...,<=*a**n*<=/<=*k* (1<=β€<=*a**i*<=<<=10*k*).
The third line of the input contains *n*<=/<=*k* space-separated positive integersΒ β sequence *b*1,<=*b*2,<=...,<=*b**n*<=/<=*k* (0<=β€<=*b**i*<=β€<=9). | Print a single integerΒ β the number of good phone numbers of length *n* modulo 109<=+<=7. | [
"6 2\n38 56 49\n7 3 4\n",
"8 2\n1 22 3 44\n5 4 3 2\n"
] | [
"8\n",
"32400\n"
] | In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698. | [
{
"input": "6 2\n38 56 49\n7 3 4",
"output": "8"
},
{
"input": "8 2\n1 22 3 44\n5 4 3 2",
"output": "32400"
},
{
"input": "2 1\n9 9\n9 9",
"output": "1"
},
{
"input": "2 1\n9 9\n0 9",
"output": "1"
},
{
"input": "4 1\n4 3 2 1\n1 2 3 4",
"output": "540"
},
... | 139 | 31,027,200 | 3 | 19,027 | |
799 | Beautiful fountains rows | [
"data structures"
] | null | null | Butler Ostin wants to show Arkady that rows of odd number of fountains are beautiful, while rows of even number of fountains are not.
The butler wants to show Arkady *n* gardens. Each garden is a row of *m* cells, the *i*-th garden has one fountain in each of the cells between *l**i* and *r**i* inclusive, and there are no more fountains in that garden. The issue is that some of the gardens contain even number of fountains, it is wrong to show them to Arkady.
Ostin wants to choose two integers *a*<=β€<=*b* and show only part of each of the gardens that starts at cell *a* and ends at cell *b*. Of course, only such segments suit Ostin that each garden has either zero or odd number of fountains on this segment. Also, it is necessary that at least one garden has at least one fountain on the segment from *a* to *b*.
Help Ostin to find the total length of all such segments, i.e. sum up the value (*b*<=-<=*a*<=+<=1) for each suitable pair (*a*,<=*b*). | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=2Β·105)Β β the number of gardens and the length of each garden.
*n* lines follow. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*m*)Β β the bounds of the segment that contains fountains in the *i*-th garden. | Print one integer: the total length of all suitable segments. | [
"1 5\n2 4\n",
"3 6\n2 4\n3 6\n4 4\n"
] | [
"23\n",
"19\n"
] | In the first example the following pairs suit Ostin: (*a*,β*b*): (1,β2), (1,β4), (1,β5), (2,β2), (2,β4), (2,β5), (3,β3), (4,β4), (4,β5).
In the second example the following pairs suit Ostin: (*a*,β*b*): (1,β2), (1,β5), (2,β2), (2,β5), (3,β3), (4,β4), (4,β6), (5,β5), (6,β6). | [] | 62 | 0 | 0 | 19,044 | |
0 | none | [
"none"
] | null | null | In this problem you need to write job distribution subsystem for testing submissions.
Invokers (simplistically) are components of testing system, which test given submission on single test and give back a verdict. In this problem two verdicts are possible β OK (test passed) or RJ (rejected, test failed).
Job distribution subsystem will be named scheduler. You have to implement it.
For each problem two parameters are given: number of tests in a problem and time limit for a problem.
We will consider that the system works discreetly and one tick equals 10 milliseconds. If some event occupied in time moment not divisible by 10 ms then scheduler will get it in the nearest next tick.
After the end of the contest (i.e., a week after its start) the last solution you sent (having positive score) will be chosen to be launched on the final tests. The final tests are confidential and distinct from those that will be used during the competition. The total number of points that will be scored in the final tests will determine the winner of the competition.
Specially for this round we implemented feature to submit ZIP-archive with multiple source files. All files should be in root of ZIP-file, no directories are allowed. You can use this feature for:
- Java 8: main should be in a class Main of default package; - GNU C++ 11: exactly one file should contain the entry point main, all files to compile should have extension cpp.
You will be receiving submissions and invoker verdicts in interactive mode. Please be sure to use the stream flushing operation after each of your moves to prevent output buffering. For example, you can use "fflush(stdout)" in C or C++, "System.out.flush()" in Java and "flush(output)" in Pascal.
The problem is estimated as follows: let *a**i* is a full testing time (including expectations) of the *i*-th submission, that is time, passed from the moment of receiving the submission until the moment when it is fully tested. Let's find , where *n* is the number of submissions to test. The exact number of points for single test is calculated by the formula , where *A* is a *r* for some simple scheduler, written by jury, and *B* is a *r* for your scheduler.
Materials for local testing will be published soon. The package with materials will contain an interactor and a set of test data for the interactor. | On the start your program reads the following data from standard input:
- in the first line you are given an integer number *t* (1<=β€<=*t*<=β€<=500) β the number of available invokers (all invokers work simultaneously and independently, free invoker starts testing a job as soon as it is appointed to him); - in the second line you are given an integer number *p* (1<=β€<=*p*<=β€<=10000) β the number of problems, submissions to which are expected to be tested.
Further *p* lines with description of problems are followed. Each description contains problem time limit (integer number between 250 and 30000) and the number of tests (from 1 to 1000).
The following data is given in interactive mode. It means that a following block of data will be available only after your scheduler writes information of its behavior on the previous tick. Firstly, block of data about new submissions, needed to be tested, follows for a tick. Each submission is described by single integer number β index of the problem (from 0 to *p*<=-<=1). The value -1 means that there are no new submissions, needed to be tested, in this tick.
Further block of testing results (from invokers) in this tick follows. These lines contain three elements: index of submission, a number of test and the verdict of test (OK or RJ). The block of testing results ends with a line "-1 -1".
Consider that problems and tests are numbered (from zero) in the order of its appearance. Total number of submissions for one test does not exceed 20000. | After reading the data about the next tick your scheduler can decide to send submissions for exact testing. The submission can be tested in any test at any time. Your scheduler must output a couple of integers β index of submission and a number of test in a single line in order to start a test for the submission. If there are a free invoker, a testing of a submission immediately begins (invoker becomes busy until the result comes back). Otherwise, your request will be ignored. When you are done sending requests, output line "-1 -1".
You can simply maintain the amount of free invokers in your program, reducing each time variable *t* while sending the task to invokers (if *t*<==<=0, there is no need to send) and increasing *t*, if invoker returns a verdict.
It is considered that the submission was tested if it was tested on all tests of the problem, or if it was tested on all tests up to first RJ verdict (inclusive).
Interactor breaks out at a time when all the planned submissions will be tested completely. In this case your submission should stop, as soon as it finds closing of input data stream. | [
"1\n1\n500 2\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n0\n-1\n-1 -1\n0\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n0 0 OK\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n0 1 OK\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n-1 -1\n-1\n1 0 RJ\n-1 -1\n"
] | [
"-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n0 0\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n0 1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n1 0\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n-1 -1\n1 1\n-1 -1\n"
] | Materials and tests are available now at [http://assets.codeforces.com/files/vk/vkcup-2016-wr2-materials-v1.tar.gz](//assets.codeforces.com/files/vk/vkcup-2016-wr2-materials-v1.tar.gz). Please read README.txt to learn how to test your solution with the interactor. | [] | 0 | 0 | -1 | 19,052 | |
954 | Runner's Problem | [
"dp",
"matrices",
"sortings"
] | null | null | You are running through a rectangular field. This field can be represented as a matrix with 3 rows and *m* columns. (*i*,<=*j*) denotes a cell belonging to *i*-th row and *j*-th column.
You start in (2,<=1) and have to end your path in (2,<=*m*). From the cell (*i*,<=*j*) you may advance to:
- (*i*<=-<=1,<=*j*<=+<=1) β only if *i*<=><=1, - (*i*,<=*j*<=+<=1), or - (*i*<=+<=1,<=*j*<=+<=1) β only if *i*<=<<=3.
However, there are *n* obstacles blocking your path. *k*-th obstacle is denoted by three integers *a**k*, *l**k* and *r**k*, and it forbids entering any cell (*a**k*,<=*j*) such that *l**k*<=β€<=*j*<=β€<=*r**k*.
You have to calculate the number of different paths from (2,<=1) to (2,<=*m*), and print it modulo 109<=+<=7. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=104, 3<=β€<=*m*<=β€<=1018) β the number of obstacles and the number of columns in the matrix, respectively.
Then *n* lines follow, each containing three integers *a**k*, *l**k* and *r**k* (1<=β€<=*a**k*<=β€<=3, 2<=β€<=*l**k*<=β€<=*r**k*<=β€<=*m*<=-<=1) denoting an obstacle blocking every cell (*a**k*,<=*j*) such that *l**k*<=β€<=*j*<=β€<=*r**k*. Some cells may be blocked by multiple obstacles. | Print the number of different paths from (2,<=1) to (2,<=*m*), taken modulo 109<=+<=7. If it is impossible to get from (2,<=1) to (2,<=*m*), then the number of paths is 0. | [
"2 5\n1 3 4\n2 2 3\n"
] | [
"2\n"
] | none | [
{
"input": "2 5\n1 3 4\n2 2 3",
"output": "2"
},
{
"input": "50 100\n3 24 49\n2 10 12\n1 87 92\n2 19 60\n2 53 79\n3 65 82\n3 10 46\n1 46 86\n2 55 84\n1 50 53\n3 80 81\n3 66 70\n2 35 52\n1 63 69\n2 65 87\n3 68 75\n1 33 42\n1 56 90\n3 73 93\n2 20 26\n2 42 80\n2 83 87\n3 99 99\n1 14 79\n2 94 97\n1 66 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 can you do the same?
You are given an array consisting of *n* non-negative integers, and *m* queries. Each query is characterized by one number *x* and consists of the following consecutive steps:
- Perform the bitwise addition operation modulo 2 (xor) of each array element with the number *x*. - Find mex of the resulting array.
Note that after each query the array changes. | First line contains two integer numbers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=3Β·105)Β β number of elements in array and number of queries.
Next line contains *n* integer numbers *a**i* (0<=β€<=*a**i*<=β€<=3Β·105)Β β elements of then array.
Each of next *m* lines contains queryΒ β one integer number *x* (0<=β€<=*x*<=β€<=3Β·105). | For each query print the answer on a separate line. | [
"2 2\n1 3\n1\n3\n",
"4 3\n0 1 5 6\n1\n2\n4\n",
"5 4\n0 1 5 6 7\n1\n1\n4\n5\n"
] | [
"1\n0\n",
"2\n0\n0\n",
"2\n2\n0\n2\n"
] | none | [
{
"input": "2 2\n1 3\n1\n3",
"output": "1\n0"
},
{
"input": "4 3\n0 1 5 6\n1\n2\n4",
"output": "2\n0\n0"
},
{
"input": "5 4\n0 1 5 6 7\n1\n1\n4\n5",
"output": "2\n2\n0\n2"
},
{
"input": "5 5\n1 2 3 4 5\n1\n2\n3\n4\n5",
"output": "1\n3\n0\n2\n1"
},
{
"input": "9 3\... | 2,000 | 6,656,000 | 0 | 19,178 | |
940 | Machine Learning | [
"brute force",
"data structures"
] | null | null | You come home and fell some unpleasant smell. Where is it coming from?
You are given an array *a*. You have to answer the following queries:
1. You are given two integers *l* and *r*. Let *c**i* be the number of occurrences of *i* in *a**l*:<=*r*, where *a**l*:<=*r* is the subarray of *a* from *l*-th element to *r*-th inclusive. Find the Mex of {*c*0,<=*c*1,<=...,<=*c*109} 1. You are given two integers *p* to *x*. Change *a**p* to *x*.
The Mex of a multiset of numbers is the smallest non-negative integer not in the set.
Note that in this problem all elements of *a* are positive, which means that *c*0 = 0 and 0 is never the answer for the query of the second type. | The first line of input contains two integers *n* and *q* (1<=β€<=*n*,<=*q*<=β€<=100<=000)Β β the length of the array and the number of queries respectively.
The second line of input contains *n* integersΒ β *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109).
Each of the next *q* lines describes a single query.
The first type of query is described by three integers *t**i*<==<=1, *l**i*, *r**i*, where 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*Β β the bounds of the subarray.
The second type of query is described by three integers *t**i*<==<=2, *p**i*, *x**i*, where 1<=β€<=*p**i*<=β€<=*n* is the index of the element, which must be changed and 1<=β€<=*x**i*<=β€<=109 is the new value. | For each query of the first type output a single integer Β β the Mex of {*c*0,<=*c*1,<=...,<=*c*109}. | [
"10 4\n1 2 3 1 1 2 2 2 9 9\n1 1 1\n1 2 8\n2 7 1\n1 2 8\n"
] | [
"2\n3\n2\n"
] | The subarray of the first query consists of the single elementΒ β 1.
The subarray of the second query consists of four 2s, one 3 and two 1s.
The subarray of the fourth query consists of three 1s, three 2s and one 3. | [
{
"input": "10 4\n1 2 3 1 1 2 2 2 9 9\n1 1 1\n1 2 8\n2 7 1\n1 2 8",
"output": "2\n3\n2"
}
] | 4,000 | 135,168,000 | 0 | 19,239 | |
594 | REQ | [
"data structures",
"number theory"
] | null | null | Today on a math lesson the teacher told Vovochka that the Euler function of a positive integer Ο(*n*) is an arithmetic function that counts the positive integers less than or equal to n that are relatively prime to n. The number 1 is coprime to all the positive integers and Ο(1)<==<=1.
Now the teacher gave Vovochka an array of *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* and a task to process *q* queries *l**i* *r**i*Β β to calculate and print modulo 109<=+<=7. As it is too hard for a second grade school student, you've decided to help Vovochka. | The first line of the input contains number *n* (1<=β€<=*n*<=β€<=200<=000)Β β the length of the array given to Vovochka. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106).
The third line contains integer *q* (1<=β€<=*q*<=β€<=200<=000)Β β the number of queries. Next *q* lines contain the queries, one per line. Each query is defined by the boundaries of the segment *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*). | Print *q* numbers β the value of the Euler function for each query, calculated modulo 109<=+<=7. | [
"10\n1 2 3 4 5 6 7 8 9 10\n7\n1 1\n3 8\n5 6\n4 8\n8 10\n7 9\n7 10\n",
"7\n24 63 13 52 6 10 1\n6\n3 5\n4 7\n1 7\n2 4\n3 6\n2 6\n"
] | [
"1\n4608\n8\n1536\n192\n144\n1152\n",
"1248\n768\n12939264\n11232\n9984\n539136\n"
] | In the second sample the values are calculated like that:
- Ο(13Β·52Β·6)β=βΟ(4056)β=β1248 - Ο(52Β·6Β·10Β·1)β=βΟ(3120)β=β768 - Ο(24Β·63Β·13Β·52Β·6Β·10Β·1)β=βΟ(61326720)β=β12939264 - Ο(63Β·13Β·52)β=βΟ(42588)β=β11232 - Ο(13Β·52Β·6Β·10)β=βΟ(40560)β=β9984 - Ο(63Β·13Β·52Β·6Β·10)β=βΟ(2555280)β=β539136 | [
{
"input": "10\n1 2 3 4 5 6 7 8 9 10\n7\n1 1\n3 8\n5 6\n4 8\n8 10\n7 9\n7 10",
"output": "1\n4608\n8\n1536\n192\n144\n1152"
},
{
"input": "7\n24 63 13 52 6 10 1\n6\n3 5\n4 7\n1 7\n2 4\n3 6\n2 6",
"output": "1248\n768\n12939264\n11232\n9984\n539136"
},
{
"input": "5\n2 3 4 5 6\n5\n2 5\n2 ... | 3,000 | 307,200 | 0 | 19,329 | |
65 | Harry Potter and the History of Magic | [
"brute force",
"greedy",
"implementation"
] | B. Harry Potter and the History of Magic | 1 | 256 | The History of Magic is perhaps the most boring subject in the Hogwarts school of Witchcraft and Wizardry. Harry Potter is usually asleep during history lessons, and his magical quill writes the lectures for him. Professor Binns, the history of magic teacher, lectures in such a boring and monotonous voice, that he has a soporific effect even on the quill. That's why the quill often makes mistakes, especially in dates.
So, at the end of the semester Professor Binns decided to collect the students' parchments with notes and check them. Ron Weasley is in a panic: Harry's notes may contain errors, but at least he has some notes, whereas Ron does not have any. Ronald also has been sleeping during the lectures and his quill had been eaten by his rat Scabbers. Hermione Granger refused to give Ron her notes, because, in her opinion, everyone should learn on their own. Therefore, Ron has no choice but to copy Harry's notes.
Due to the quill's errors Harry's dates are absolutely confused: the years of goblin rebellions and other important events for the wizarding world do not follow in order, and sometimes even dates from the future occur. Now Ron wants to change some of the digits while he copies the notes so that the dates were in the chronological (i.e. non-decreasing) order and so that the notes did not have any dates strictly later than 2011, or strictly before than 1000. To make the resulting sequence as close as possible to the one dictated by Professor Binns, Ron will change no more than one digit in each date into other digit. Help him do it. | The first input line contains an integer *n* (1<=β€<=*n*<=β€<=1000). It represents the number of dates in Harry's notes. Next *n* lines contain the actual dates *y*1, *y*2, ..., *y**n*, each line contains a date. Each date is a four-digit integer (1000<=β€<=*y**i*<=β€<=9999). | Print *n* numbers *z*1, *z*2, ..., *z**n* (1000<=β€<=*z**i*<=β€<=2011). They are Ron's resulting dates. Print each number on a single line. Numbers *z**i* must form the non-decreasing sequence. Each number *z**i* should differ from the corresponding date *y**i* in no more than one digit. It is not allowed to change the first digit of a number into 0. If there are several possible solutions, print any of them. If there's no solution, print "No solution" (without the quotes). | [
"3\n1875\n1936\n1721\n",
"4\n9999\n2000\n3000\n3011\n",
"3\n1999\n5055\n2000\n"
] | [
"1835\n1836\n1921\n",
"1999\n2000\n2000\n2011\n",
"No solution\n"
] | none | [
{
"input": "3\n1875\n1936\n1721",
"output": "1075\n1136\n1221"
},
{
"input": "4\n9999\n2000\n3000\n3011",
"output": "1999\n2000\n2000\n2011"
},
{
"input": "3\n1999\n5055\n2000",
"output": "No solution"
},
{
"input": "2\n2037\n2025",
"output": "1037\n2005"
},
{
"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 it leaves at the moment of time *t**i* seconds, can go at a speed of no greater than *v**i* meters per second, and accelerate with an acceleration no greater than *a* meters per second squared. A trolleybus can decelerate as quickly as you want (magic!). It can change its acceleration as fast as you want, as well. Note that the maximum acceleration is the same for all trolleys.
Despite the magic the trolleys are still powered by an electric circuit and cannot overtake each other (the wires are to blame, of course). If a trolleybus catches up with another one, they go together one right after the other until they arrive at the final station. Also, the drivers are driving so as to arrive at the final station as quickly as possible.
You, as head of the trolleybuses' fans' club, are to determine for each trolley the minimum time by which it can reach the final station. At the time of arrival at the destination station the trolleybus does not necessarily have zero speed. When a trolley is leaving the depot, its speed is considered equal to zero. From the point of view of physics, the trolleybuses can be considered as material points, and also we should ignore the impact on the speed of a trolley bus by everything, except for the acceleration and deceleration provided by the engine. | The first input line contains three space-separated integers *n*, *a*, *d* (1<=β€<=*n*<=β€<=105, 1<=β€<=*a*,<=*d*<=β€<=106) β the number of trolleybuses, their maximum acceleration and the distance from the depot to the final station, correspondingly.
Next *n* lines contain pairs of integers *t**i* *v**i* (0<=β€<=*t*1<=<<=*t*2...<=<<=*t**n*<=-<=1<=<<=*t**n*<=β€<=106, 1<=β€<=*v**i*<=β€<=106) β the time when the *i*-th trolleybus leaves the depot and its maximum speed, correspondingly. The numbers in the lines are separated by spaces. | For each trolleybus print a single line the time it arrives to the final station. Print the times for the trolleybuses in the order in which the trolleybuses are given in the input. The answer will be accepted if the absolute or relative error doesn't exceed 10<=-<=4. | [
"3 10 10000\n0 10\n5 11\n1000 1\n",
"1 2 26\n28 29\n"
] | [
"1000.5000000000\n1000.5000000000\n11000.0500000000\n",
"33.0990195136\n"
] | In the first sample the second trolleybus will catch up with the first one, that will happen at distance 510.5 meters from the depot. The trolleybuses will go the remaining 9489.5 meters together at speed 10 meters per second. As a result, both trolleybuses will arrive to the final station by the moment of time 1000.5 seconds. The third trolleybus will not catch up with them. It will arrive to the final station by the moment of time 11000.05 seconds. | [
{
"input": "3 10 10000\n0 10\n5 11\n1000 1",
"output": "1000.5000000000\n1000.5000000000\n11000.0500000000"
},
{
"input": "1 2 26\n28 29",
"output": "33.0990195136"
},
{
"input": "7 8 3\n1 3\n5 26\n7 3\n10 15\n18 7\n21 17\n23 21",
"output": "2.1875000000\n5.8660254038\n8.1875000000\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 counted the shortest distances from one of the graph vertices to all other ones and wrote them out in array *d*. Thus, element *d*[*i*] of the array shows the shortest distance from the vertex Valera chose to vertex number *i*.
Then something irreparable terrible happened. Valera lost the initial graph. However, he still has the array *d*. Help him restore the lost graph. | The first line contains two space-separated integers *n* and *k* (1<=β€<=*k*<=<<=*n*<=β€<=105). Number *n* shows the number of vertices in the original graph. Number *k* shows that at most *k* edges were adjacent to each vertex in the original graph.
The second line contains space-separated integers *d*[1],<=*d*[2],<=...,<=*d*[*n*] (0<=β€<=*d*[*i*]<=<<=*n*). Number *d*[*i*] shows the shortest distance from the vertex Valera chose to the vertex number *i*. | If Valera made a mistake in his notes and the required graph doesn't exist, print in the first line number -1. Otherwise, in the first line print integer *m* (0<=β€<=*m*<=β€<=106) β the number of edges in the found graph.
In each of the next *m* lines print two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*;Β *a**i*<=β <=*b**i*), denoting the edge that connects vertices with numbers *a**i* and *b**i*. The graph shouldn't contain self-loops and multiple edges. If there are multiple possible answers, print any of them. | [
"3 2\n0 1 1\n",
"4 2\n2 0 1 3\n",
"3 1\n0 0 0\n"
] | [
"3\n1 2\n1 3\n3 2\n",
"3\n1 3\n1 4\n2 3\n",
"-1\n"
] | none | [
{
"input": "3 2\n0 1 1",
"output": "2\n1 2\n1 3"
},
{
"input": "4 2\n2 0 1 3",
"output": "3\n1 3\n1 4\n2 3"
},
{
"input": "3 1\n0 0 0",
"output": "-1"
},
{
"input": "5 3\n0 2 1 2 1",
"output": "4\n1 3\n1 5\n2 5\n4 5"
},
{
"input": "7 3\n2 2 0 1 3 2 1",
"output... | 77 | 0 | 0 | 19,389 | |
631 | Print Check | [
"constructive algorithms",
"implementation"
] | null | null | Kris works in a large company "Blake Technologies". As a best engineer of the company he was assigned a task to develop a printer that will be able to print horizontal and vertical strips. First prototype is already built and Kris wants to tests it. He wants you to implement the program that checks the result of the printing.
Printer works with a rectangular sheet of paper of size *n*<=Γ<=*m*. Consider the list as a table consisting of *n* rows and *m* columns. Rows are numbered from top to bottom with integers from 1 to *n*, while columns are numbered from left to right with integers from 1 to *m*. Initially, all cells are painted in color 0.
Your program has to support two operations:
1. Paint all cells in row *r**i* in color *a**i*; 1. Paint all cells in column *c**i* in color *a**i*.
If during some operation *i* there is a cell that have already been painted, the color of this cell also changes to *a**i*.
Your program has to print the resulting table after *k* operation. | The first line of the input contains three integers *n*, *m* and *k* (1<=<=β€<=<=*n*,<=<=*m*<=<=β€<=5000, *n*Β·*m*<=β€<=100<=000, 1<=β€<=*k*<=β€<=100<=000)Β β the dimensions of the sheet and the number of operations, respectively.
Each of the next *k* lines contains the description of exactly one query:
- 1Β *r**i*Β *a**i* (1<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*a**i*<=β€<=109), means that row *r**i* is painted in color *a**i*; - 2Β *c**i*Β *a**i* (1<=β€<=*c**i*<=β€<=*m*, 1<=β€<=*a**i*<=β€<=109), means that column *c**i* is painted in color *a**i*. | Print *n* lines containing *m* integers eachΒ β the resulting table after all operations are applied. | [
"3 3 3\n1 1 3\n2 2 1\n1 2 2\n",
"5 3 5\n1 1 1\n1 3 1\n1 5 1\n2 1 1\n2 3 1\n"
] | [
"3 1 3 \n2 2 2 \n0 1 0 \n",
"1 1 1 \n1 0 1 \n1 1 1 \n1 0 1 \n1 1 1 \n"
] | The figure below shows all three operations for the first sample step by step. The cells that were painted on the corresponding step are marked gray. | [
{
"input": "3 3 3\n1 1 3\n2 2 1\n1 2 2",
"output": "3 1 3 \n2 2 2 \n0 1 0 "
},
{
"input": "5 3 5\n1 1 1\n1 3 1\n1 5 1\n2 1 1\n2 3 1",
"output": "1 1 1 \n1 0 1 \n1 1 1 \n1 0 1 \n1 1 1 "
},
{
"input": "5 5 4\n1 2 1\n1 4 1\n2 2 1\n2 4 1",
"output": "0 1 0 1 0 \n1 1 1 1 1 \n0 1 0 1 0 \n1... | 1,000 | 614,400 | 0 | 19,432 | |
864 | Fire | [
"dp",
"sortings"
] | null | null | Polycarp is in really serious trouble β his house is on fire! It's time to save the most valuable items. Polycarp estimated that it would take *t**i* seconds to save *i*-th item. In addition, for each item, he estimated the value of *d**i* β the moment after which the item *i* will be completely burned and will no longer be valuable for him at all. In particular, if *t**i*<=β₯<=*d**i*, then *i*-th item cannot be saved.
Given the values *p**i* for each of the items, find a set of items that Polycarp can save such that the total value of this items is maximum possible. Polycarp saves the items one after another. For example, if he takes item *a* first, and then item *b*, then the item *a* will be saved in *t**a* seconds, and the item *b* β in *t**a*<=+<=*t**b* seconds after fire started. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of items in Polycarp's house.
Each of the following *n* lines contains three integers *t**i*,<=*d**i*,<=*p**i* (1<=β€<=*t**i*<=β€<=20, 1<=β€<=*d**i*<=β€<=2<=000, 1<=β€<=*p**i*<=β€<=20) β the time needed to save the item *i*, the time after which the item *i* will burn completely and the value of item *i*. | In the first line print the maximum possible total value of the set of saved items. In the second line print one integer *m* β the number of items in the desired set. In the third line print *m* distinct integers β numbers of the saved items in the order Polycarp saves them. Items are 1-indexed in the same order in which they appear in the input. If there are several answers, print any of them. | [
"3\n3 7 4\n2 6 5\n3 7 6\n",
"2\n5 6 1\n3 3 5\n"
] | [
"11\n2\n2 3 \n",
"1\n1\n1 \n"
] | In the first example Polycarp will have time to save any two items, but in order to maximize the total value of the saved items, he must save the second and the third item. For example, he can firstly save the third item in 3 seconds, and then save the second item in another 2 seconds. Thus, the total value of the saved items will be 6β+β5β=β11.
In the second example Polycarp can save only the first item, since even if he immediately starts saving the second item, he can save it in 3 seconds, but this item will already be completely burned by this time. | [
{
"input": "3\n3 7 4\n2 6 5\n3 7 6",
"output": "11\n2\n2 3 "
},
{
"input": "2\n5 6 1\n3 3 5",
"output": "1\n1\n1 "
},
{
"input": "9\n13 18 14\n8 59 20\n9 51 2\n18 32 15\n1 70 18\n14 81 14\n10 88 16\n18 52 3\n1 50 6",
"output": "106\n8\n1 4 9 8 2 5 6 7 "
},
{
"input": "5\n12 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 following sequences are regular: "()()()", "(())" and "()". The following sequences are not regular bracket sequences: ")(", "(()" and "())(()".
In this problem you are given two integers *n* and *k*. Your task is to construct a regular bracket sequence consisting of round brackets with length 2Β·*n* with total sum of nesting of all opening brackets equals to exactly *k*. The nesting of a single opening bracket equals to the number of pairs of brackets in which current opening bracket is embedded.
For example, in the sequence "()(())" the nesting of first opening bracket equals to 0, the nesting of the second opening bracket equals to 0 and the nesting of the third opening bracket equal to 1. So the total sum of nestings equals to 1. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=3Β·105, 0<=β€<=*k*<=β€<=1018)Β β the number of opening brackets and needed total nesting. | Print the required regular bracket sequence consisting of round brackets.
If there is no solution print "Impossible" (without quotes). | [
"3 1\n",
"4 6\n",
"2 5\n"
] | [
"()(())",
"(((())))",
"Impossible\n"
] | The first example is examined in the statement.
In the second example the answer is "(((())))". The nesting of the first opening bracket is 0, the nesting of the second is 1, the nesting of the third is 2, the nesting of fourth is 3. So the total sum of nestings equals to 0β+β1β+β2β+β3β=β6.
In the third it is impossible to construct a regular bracket sequence, because the maximum possible total sum of nestings for two opening brackets equals to 1. This total sum of nestings is obtained for the sequence "(())". | [
{
"input": "3 1",
"output": "()(())"
},
{
"input": "4 6",
"output": "(((())))"
},
{
"input": "2 5",
"output": "Impossible"
},
{
"input": "1 0",
"output": "()"
},
{
"input": "2 0",
"output": "()()"
},
{
"input": "2 1",
"output": "(())"
},
{
... | 62 | 5,529,600 | 0 | 19,495 | |
226 | Noble Knight's Path | [
"data structures",
"trees"
] | null | null | In Berland each feudal owns exactly one castle and each castle belongs to exactly one feudal.
Each feudal, except one (the King) is subordinate to another feudal. A feudal can have any number of vassals (subordinates).
Some castles are connected by roads, it is allowed to move along the roads in both ways. Two castles have a road between them if and only if the owner of one of these castles is a direct subordinate to the other owner.
Each year exactly one of these two events may happen in Berland.
1. The barbarians attacked castle *c*. The interesting fact is, the barbarians never attacked the same castle twice throughout the whole Berlandian history. 1. A noble knight sets off on a journey from castle *a* to castle *b* (provided that on his path he encounters each castle not more than once).
Let's consider the second event in detail. As the journey from *a* to *b* is not short, then the knight might want to stop at a castle he encounters on his way to have some rest. However, he can't stop at just any castle: his nobility doesn't let him stay in the castle that has been desecrated by the enemy's stench. A castle is desecrated if and only if it has been attacked after the year of *y*. So, the knight chooses the *k*-th castle he encounters, starting from *a* (castles *a* and *b* aren't taken into consideration), that hasn't been attacked in years from *y*<=+<=1 till current year.
The knights don't remember which castles were attacked on what years, so he asked the court scholar, aka you to help them. You've got a sequence of events in the Berland history. Tell each knight, in what city he should stop or else deliver the sad news β that the path from city *a* to city *b* has less than *k* cities that meet his requirements, so the knight won't be able to rest. | The first input line contains integer *n* (2<=β€<=*n*<=β€<=105) β the number of feudals.
The next line contains *n* space-separated integers: the *i*-th integer shows either the number of the *i*-th feudal's master, or a 0, if the *i*-th feudal is the King.
The third line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of queries.
Then follow *m* lines that describe the events. The *i*-th line (the lines are indexed starting from 1) contains the description of the event that occurred in year *i*. Each event is characterised by type *t**i* (1<=β€<=*t**i*<=β€<=2). The description of the first type event looks as two space-separated integers *t**i* *c**i* (*t**i*<==<=1;Β 1<=β€<=*c**i*<=β€<=*n*), where *c**i* is the number of the castle that was attacked by the barbarians in the *i*-th year. The description of the second type contains five space-separated integers: *t**i* *a**i* *b**i* *k**i* *y**i* (*t**i*<==<=2;Β 1<=β€<=*a**i*,<=*b**i*,<=*k**i*<=β€<=*n*;Β *a**i*<=β <=*b**i*;Β 0<=β€<=*y**i*<=<<=*i*), where *a**i* is the number of the castle from which the knight is setting off, *b**i* is the number of the castle to which the knight is going, *k**i* and *y**i* are the *k* and *y* from the second event's description.
You can consider the feudals indexed from 1 to *n*. It is guaranteed that there is only one king among the feudals. It is guaranteed that for the first type events all values *c**i* are different. | For each second type event print an integer β the number of the castle where the knight must stay to rest, or -1, if he will have to cover the distance from *a**i* to *b**i* without a rest. Separate the answers by whitespaces.
Print the answers in the order, in which the second type events are given in the input. | [
"3\n0 1 2\n5\n2 1 3 1 0\n1 2\n2 1 3 1 0\n2 1 3 1 1\n2 1 3 1 2\n",
"6\n2 5 2 2 0 5\n3\n2 1 6 2 0\n1 2\n2 4 5 1 0\n"
] | [
"2\n-1\n-1\n2\n",
"5\n-1\n"
] | In the first sample there is only castle 2 on the knight's way from castle 1 to castle 3. When the knight covers the path 1β-β3 for the first time, castle 2 won't be desecrated by an enemy and the knight will stay there. In the second year the castle 2 will become desecrated, so the knight won't have anywhere to stay for the next two years (as finding a castle that hasn't been desecrated from years 1 and 2, correspondingly, is important for him). In the fifth year the knight won't consider the castle 2 desecrated, so he will stay there again. | [] | 124 | 0 | 0 | 19,572 | |
600 | Area of Two Circles' Intersection | [
"geometry"
] | null | null | You are given two circles. Find the area of their intersection. | The first line contains three integers *x*1,<=*y*1,<=*r*1 (<=-<=109<=β€<=*x*1,<=*y*1<=β€<=109,<=1<=β€<=*r*1<=β€<=109) β the position of the center and the radius of the first circle.
The second line contains three integers *x*2,<=*y*2,<=*r*2 (<=-<=109<=β€<=*x*2,<=*y*2<=β€<=109,<=1<=β€<=*r*2<=β€<=109) β the position of the center and the radius of the second circle. | Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10<=-<=6. | [
"0 0 4\n6 0 4\n",
"0 0 5\n11 0 5\n"
] | [
"7.25298806364175601379\n",
"0.00000000000000000000\n"
] | none | [
{
"input": "0 0 4\n6 0 4",
"output": "7.25298806364175601379"
},
{
"input": "0 0 5\n11 0 5",
"output": "0.00000000000000000000"
},
{
"input": "0 0 10\n9 0 1",
"output": "3.14159265358979311600"
},
{
"input": "0 0 2\n2 2 2",
"output": "2.28318530717958647659"
},
{
... | 77 | 3,481,600 | -1 | 19,604 | |
15 | Triangles | [
"combinatorics",
"dp"
] | E. Triangles | 1 | 64 | Last summer Peter was at his granny's in the country, when a wolf attacked sheep in the nearby forest. Now he fears to walk through the forest, to walk round the forest, even to get out of the house. He explains this not by the fear of the wolf, but by a strange, in his opinion, pattern of the forest that has *n* levels, where *n* is an even number.
In the local council you were given an area map, where the granny's house is marked by point *H*, parts of dense forest are marked grey (see the picture to understand better).
After a long time at home Peter decided to yield to his granny's persuasions and step out for a breath of fresh air. Being prudent, Peter plans the route beforehand. The route, that Peter considers the most suitable, has the following characteristics:
- it starts and ends in the same place β the granny's house; - the route goes along the forest paths only (these are the segments marked black in the picture); - the route has positive length (to step out for a breath of fresh air Peter has to cover some distance anyway); - the route cannot cross itself; - there shouldn't be any part of dense forest within the part marked out by this route;
You should find the amount of such suitable oriented routes modulo 1000000009.
The example of the area map for *n*<==<=12 is given in the picture. Since the map has a regular structure, you can construct it for other *n* by analogy using the example. | The input data contain the only even integer *n* (2<=β€<=*n*<=β€<=106). | Output the only number β the amount of Peter's routes modulo 1000000009. | [
"2\n",
"4\n"
] | [
"10\n",
"74\n"
] | none | [
{
"input": "2",
"output": "10"
},
{
"input": "4",
"output": "74"
},
{
"input": "6",
"output": "1354"
},
{
"input": "8",
"output": "163594"
},
{
"input": "10",
"output": "122492554"
},
{
"input": "966",
"output": "154440215"
},
{
"input": "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 parallel to coordinate axes.
At the moment 0, from the point (0,<=0) the laser ray is released in the direction of point (1,<=1). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1,<=1) in exactly one second after the start.
When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.
For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print <=-<=1 for such sensors. | The first line of the input contains three integers *n*, *m* and *k* (2<=β€<=*n*,<=*m*<=β€<=100<=000, 1<=β€<=*k*<=β€<=100<=000)Β β lengths of the room's walls and the number of sensors.
Each of the following *k* lines contains two integers *x**i* and *y**i* (1<=β€<=*x**i*<=β€<=*n*<=-<=1, 1<=β€<=*y**i*<=β€<=*m*<=-<=1)Β β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. | Print *k* integers. The *i*-th of them should be equal to the number of seconds when the ray first passes through the point where the *i*-th sensor is located, or <=-<=1 if this will never happen. | [
"3 3 4\n1 1\n1 2\n2 1\n2 2\n",
"3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3\n",
"7 4 5\n1 3\n2 2\n5 1\n5 3\n4 3\n"
] | [
"1\n-1\n-1\n2\n",
"1\n-1\n-1\n2\n5\n-1\n",
"13\n2\n9\n5\n-1\n"
] | In the first sample, the ray will consequently pass through the points (0,β0), (1,β1), (2,β2), (3,β3). Thus, it will stop at the point (3,β3) after 3 seconds.
In the second sample, the ray will consequently pass through the following points: (0,β0), (1,β1), (2,β2), (3,β3), (2,β4), (1,β3), (0,β2), (1,β1), (2,β0), (3,β1), (2,β2), (1,β3), (0,β4). The ray will stop at the point (0,β4) after 12 seconds. It will reflect at the points (3,β3), (2,β4), (0,β2), (2,β0) and (3,β1). | [
{
"input": "3 3 4\n1 1\n1 2\n2 1\n2 2",
"output": "1\n-1\n-1\n2"
},
{
"input": "3 4 6\n1 1\n2 1\n1 2\n2 2\n1 3\n2 3",
"output": "1\n-1\n-1\n2\n5\n-1"
},
{
"input": "7 4 5\n1 3\n2 2\n5 1\n5 3\n4 3",
"output": "13\n2\n9\n5\n-1"
},
{
"input": "10 10 10\n3 8\n1 7\n2 3\n4 2\n4 8\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 funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too.
She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (*x*1,<=*y*1),<=(*x*2,<=*y*2),<=...,<=(*x**n*,<=*y**n*), where *x**i*<=β€<=*y**i*, is lexicographically smaller than the set (*u*1,<=*v*1),<=(*u*2,<=*v*2),<=...,<=(*u**n*,<=*v**n*), where *u**i*<=β€<=*v**i*, provided that the sequence of integers *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**n*,<=*y**n* is lexicographically smaller than the sequence *u*1,<=*v*1,<=*u*2,<=*v*2,<=...,<=*u**n*,<=*v**n*. If you do not cope, Hexadecimal will eat you. ...eat you alive. | The first line of the input data contains a pair of integers *n* and *m* (1<=β€<=*n*<=β€<=50, 0<=β€<=*m*<=β€<=2500) β the amount of vertices and edges respectively. The following lines contain pairs of numbers *x**i* and *y**i* (1<=β€<=*x**i*, *y**i*<=β€<=*n*) β the vertices that are already connected by edges. The initial graph may contain multiple edges and loops. | In the first line output Β«YESΒ» or Β«NOΒ»: if it is possible or not to construct an interesting graph. If the answer is Β«YESΒ», in the second line output *k* β the amount of edges that should be added to the initial graph. Finally, output *k* lines: pairs of vertices *x**j* and *y**j*, between which edges should be drawn. The result may contain multiple edges and loops. *k* can be equal to zero. | [
"3 2\n1 2\n2 3\n"
] | [
"YES\n1\n1 3\n"
] | none | [
{
"input": "3 2\n1 2\n2 3",
"output": "YES\n1\n1 3"
},
{
"input": "1 1\n1 1",
"output": "YES\n0"
},
{
"input": "1 2\n1 1\n1 1",
"output": "NO"
},
{
"input": "1 3\n1 1\n1 1\n1 1",
"output": "NO"
},
{
"input": "2 0",
"output": "YES\n2\n1 2\n1 2"
},
{
"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 integers 6, 8 and 10 has exactly four positive divisors. So, *D*(10)<==<=3.
You are given an integer *n*. You have to calculate *D*(*n*). | The only line contains integer *n* (1<=β€<=*n*<=β€<=1011) β the parameter from the problem statement. | Print the only integer *c* β the number of integers between 1 and *n* with exactly four divisors. | [
"10\n",
"20\n"
] | [
"3\n",
"5\n"
] | none | [
{
"input": "10",
"output": "3"
},
{
"input": "20",
"output": "5"
},
{
"input": "1",
"output": "0"
},
{
"input": "27",
"output": "9"
},
{
"input": "100",
"output": "32"
},
{
"input": "1000",
"output": "292"
},
{
"input": "10000",
"output... | 46 | 4,915,200 | 0 | 19,766 | |
995 | Game | [
"math"
] | null | null | Allen and Bessie are playing a simple number game. They both know a function $f: \{0, 1\}^n \to \mathbb{R}$, i.Β e. the function takes $n$ binary arguments and returns a real value. At the start of the game, the variables $x_1, x_2, \dots, x_n$ are all set to $-1$. Each round, with equal probability, one of Allen or Bessie gets to make a move. A move consists of picking an $i$ such that $x_i = -1$ and either setting $x_i \to 0$ or $x_i \to 1$.
After $n$ rounds all variables are set, and the game value resolves to $f(x_1, x_2, \dots, x_n)$. Allen wants to maximize the game value, and Bessie wants to minimize it.
Your goal is to help Allen and Bessie find the expected game value! They will play $r+1$ times though, so between each game, exactly one value of $f$ changes. In other words, between rounds $i$ and $i+1$ for $1 \le i \le r$, $f(z_1, \dots, z_n) \to g_i$ for some $(z_1, \dots, z_n) \in \{0, 1\}^n$. You are to find the expected game value in the beginning and after each change. | The first line contains two integers $n$ and $r$ ($1 \le n \le 18$, $0 \le r \le 2^{18}$).
The next line contains $2^n$ integers $c_0, c_1, \dots, c_{2^n-1}$ ($0 \le c_i \le 10^9$), denoting the initial values of $f$. More specifically, $f(x_0, x_1, \dots, x_{n-1}) = c_x$, if $x = \overline{x_{n-1} \ldots x_0}$ in binary.
Each of the next $r$ lines contains two integers $z$ and $g$ ($0 \le z \le 2^n - 1$, $0 \le g \le 10^9$). If $z = \overline{z_{n-1} \dots z_0}$ in binary, then this means to set $f(z_0, \dots, z_{n-1}) \to g$. | Print $r+1$ lines, the $i$-th of which denotes the value of the game $f$ during the $i$-th round. Your answer must have absolute or relative error within $10^{-6}$.
Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is considered correct if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$. | [
"2 2\n0 1 2 3\n2 5\n0 4\n",
"1 0\n2 3\n",
"2 0\n1 1 1 1\n"
] | [
"1.500000\n2.250000\n3.250000\n",
"2.500000\n",
"1.000000\n"
] | Consider the second test case. If Allen goes first, he will set $x_1 \to 1$, so the final value will be $3$. If Bessie goes first, then she will set $x_1 \to 0$ so the final value will be $2$. Thus the answer is $2.5$.
In the third test case, the game value will always be $1$ regardless of Allen and Bessie's play. | [
{
"input": "2 2\n0 1 2 3\n2 5\n0 4",
"output": "1.500000\n2.250000\n3.250000"
},
{
"input": "1 0\n2 3",
"output": "2.500000"
},
{
"input": "2 0\n1 1 1 1",
"output": "1.000000"
}
] | 30 | 0 | 0 | 19,770 | |
846 | Four Segments | [
"brute force",
"data structures",
"dp"
] | null | null | You are given an array of *n* integer numbers. Let *sum*(*l*,<=*r*) be the sum of all numbers on positions from *l* to *r* non-inclusive (*l*-th element is counted, *r*-th element is not counted). For indices *l* and *r* holds 0<=β€<=*l*<=β€<=*r*<=β€<=*n*. Indices in array are numbered from 0.
For example, if *a*<==<=[<=-<=5,<=3,<=9,<=4], then *sum*(0,<=1)<==<=<=-<=5, *sum*(0,<=2)<==<=<=-<=2, *sum*(1,<=4)<==<=16 and *sum*(*i*,<=*i*)<==<=0 for each *i* from 0 to 4.
Choose the indices of three delimiters *delim*0, *delim*1, *delim*2 (0<=β€<=*delim*0<=β€<=*delim*1<=β€<=*delim*2<=β€<=*n*) and divide the array in such a way that the value of *res*<==<=*sum*(0,<=*delim*0) - *sum*(*delim*0,<=*delim*1) + *sum*(*delim*1,<=*delim*2) - *sum*(*delim*2,<=*n*) is maximal.
Note that some of the expressions *sum*(*l*,<=*r*) can correspond to empty segments (if *l*<==<=*r* for some segment). | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=5000).
The second line contains *n* numbers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (<=-<=109<=β€<=*a**i*<=β€<=109). | Choose three indices so that the value of *res* is maximal. If there are multiple answers, print any of them. | [
"3\n-1 2 3\n",
"4\n0 0 -1 0\n",
"1\n10000\n"
] | [
"0 1 3\n",
"0 0 0\n",
"1 1 1\n"
] | none | [
{
"input": "3\n-1 2 3",
"output": "0 1 3"
},
{
"input": "4\n0 0 -1 0",
"output": "0 0 0"
},
{
"input": "1\n10000",
"output": "0 0 1"
},
{
"input": "1\n-1",
"output": "0 0 0"
},
{
"input": "1\n0",
"output": "0 0 0"
},
{
"input": "10\n0 0 0 0 0 0 0 0 0 0... | 62 | 0 | 0 | 19,782 | |
486 | OR in Matrix | [
"greedy",
"hashing",
"implementation"
] | null | null | Let's define logical *OR* as an operation on two logical values (i. e. values that belong to the set {0,<=1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical *OR* of three or more logical values in the same manner:
where is equal to 1 if some *a**i*<==<=1, otherwise it is equal to 0.
Nam has a matrix *A* consisting of *m* rows and *n* columns. The rows are numbered from 1 to *m*, columns are numbered from 1 to *n*. Element at row *i* (1<=β€<=*i*<=β€<=*m*) and column *j* (1<=β€<=*j*<=β€<=*n*) is denoted as *A**ij*. All elements of *A* are either 0 or 1. From matrix *A*, Nam creates another matrix *B* of the same size using formula:
.
(*B**ij* is *OR* of all elements in row *i* and column *j* of matrix *A*)
Nam gives you matrix *B* and challenges you to guess matrix *A*. Although Nam is smart, he could probably make a mistake while calculating matrix *B*, since size of *A* can be large. | The first line contains two integer *m* and *n* (1<=β€<=*m*,<=*n*<=β€<=100), number of rows and number of columns of matrices respectively.
The next *m* lines each contain *n* integers separated by spaces describing rows of matrix *B* (each element of *B* is either 0 or 1). | In the first line, print "NO" if Nam has made a mistake when calculating *B*, otherwise print "YES". If the first line is "YES", then also print *m* rows consisting of *n* integers representing matrix *A* that can produce given matrix *B*. If there are several solutions print any one. | [
"2 2\n1 0\n0 0\n",
"2 3\n1 1 1\n1 1 1\n",
"2 3\n0 1 0\n1 1 1\n"
] | [
"NO\n",
"YES\n1 1 1\n1 1 1\n",
"YES\n0 0 0\n0 1 0\n"
] | none | [
{
"input": "2 2\n1 0\n0 0",
"output": "NO"
},
{
"input": "2 3\n1 1 1\n1 1 1",
"output": "YES\n1 1 1\n1 1 1"
},
{
"input": "2 3\n0 1 0\n1 1 1",
"output": "YES\n0 0 0\n0 1 0"
},
{
"input": "5 5\n1 1 1 1 1\n1 0 0 0 0\n1 0 0 0 0\n1 0 0 0 0\n1 0 0 0 0",
"output": "YES\n1 0 0 0... | 1,000 | 819,200 | 0 | 19,827 | |
838 | Airplane Arrangements | [
"math",
"number theory"
] | null | null | There is an airplane which has *n* rows from front to back. There will be *m* people boarding this airplane.
This airplane has an entrance at the very front and very back of the plane.
Each person has some assigned seat. It is possible for multiple people to have the same assigned seat. The people will then board the plane one by one starting with person 1. Each person can independently choose either the front entrance or back entrance to enter the plane.
When a person walks into the plane, they walk directly to their assigned seat and will try to sit in it. If it is occupied, they will continue walking in the direction they walked in until they are at empty seat - they will take the earliest empty seat that they can find. If they get to the end of the row without finding a seat, they will be angry.
Find the number of ways to assign tickets to the passengers and board the plane without anyone getting angry. Two ways are different if there exists a passenger who chose a different entrance in both ways, or the assigned seat is different. Print this count modulo 109<=+<=7. | The first line of input will contain two integers *n*,<=*m* (1<=β€<=*m*<=β€<=*n*<=β€<=1<=000<=000), the number of seats, and the number of passengers, respectively. | Print a single number, the number of ways, modulo 109<=+<=7. | [
"3 3\n"
] | [
"128\n"
] | Here, we will denote a passenger by which seat they were assigned, and which side they came from (either "F" or "B" for front or back, respectively).
For example, one valid way is 3B, 3B, 3B (i.e. all passengers were assigned seat 3 and came from the back entrance). Another valid way would be 2F, 1B, 3F.
One invalid way would be 2B, 2B, 2B, since the third passenger would get to the front without finding a seat. | [
{
"input": "3 3",
"output": "128"
},
{
"input": "1000000 1000000",
"output": "233176135"
},
{
"input": "1000000 500000",
"output": "211837745"
},
{
"input": "1 1",
"output": "2"
},
{
"input": "10 1",
"output": "20"
},
{
"input": "285042 201091",
"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 chooses one of the four directions (up, down, left or right) and runs from 1 to *k* meters in this direction. Of course, she can only run through empty cells.
Now Olya needs to get from cell (*x*1,<=*y*1) to cell (*x*2,<=*y*2). How many seconds will it take her if she moves optimally?
It's guaranteed that cells (*x*1,<=*y*1) and (*x*2,<=*y*2) are empty. These cells can coincide. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*,<=*k*<=β€<=1000) β the sizes of the room and Olya's speed.
Then *n* lines follow containing *m* characters each, the *i*-th of them contains on *j*-th position "#", if the cell (*i*,<=*j*) is littered with cans, and "." otherwise.
The last line contains four integers *x*1,<=*y*1,<=*x*2,<=*y*2 (1<=β€<=*x*1,<=*x*2<=β€<=*n*, 1<=β€<=*y*1,<=*y*2<=β€<=*m*) β the coordinates of the first and the last cells. | Print a single integer β the minimum time it will take Olya to get from (*x*1,<=*y*1) to (*x*2,<=*y*2).
If it's impossible to get from (*x*1,<=*y*1) to (*x*2,<=*y*2), print -1. | [
"3 4 4\n....\n###.\n....\n1 1 3 1\n",
"3 4 1\n....\n###.\n....\n1 1 3 1\n",
"2 2 1\n.#\n#.\n1 1 2 2\n"
] | [
"3",
"8",
"-1"
] | In the first sample Olya should run 3 meters to the right in the first second, 2 meters down in the second second and 3 meters to the left in the third second.
In second sample Olya should run to the right for 3 seconds, then down for 2 seconds and then to the left for 3 seconds.
Olya does not recommend drinking energy drinks and generally believes that this is bad. | [
{
"input": "3 4 4\n....\n###.\n....\n1 1 3 1",
"output": "3"
},
{
"input": "3 4 1\n....\n###.\n....\n1 1 3 1",
"output": "8"
},
{
"input": "2 2 1\n.#\n#.\n1 1 2 2",
"output": "-1"
},
{
"input": "10 10 1\n##########\n#.........\n#.#######.\n#.#.....#.\n#.#.###.#.\n#.#.#.#.#.\n... | 576 | 37,888,000 | 0 | 19,854 | |
333 | Summer Earnings | [
"binary search",
"bitmasks",
"brute force",
"geometry",
"sortings"
] | null | null | Many schoolchildren look for a job for the summer, and one day, when Gerald was still a schoolboy, he also decided to work in the summer. But as Gerald was quite an unusual schoolboy, he found quite unusual work. A certain Company agreed to pay him a certain sum of money if he draws them three identical circles on a plane. The circles must not interfere with each other (but they may touch each other). He can choose the centers of the circles only from the *n* options granted by the Company. He is free to choose the radius of the circles himself (all three radiuses must be equal), but please note that the larger the radius is, the more he gets paid.
Help Gerald earn as much as possible. | The first line contains a single integer *n* β the number of centers (3<=β€<=*n*<=β€<=3000). The following *n* lines each contain two integers *x**i*,<=*y**i* (<=-<=104<=β€<=*x**i*,<=*y**i*<=β€<=104) β the coordinates of potential circle centers, provided by the Company.
All given points are distinct. | Print a single real number β maximum possible radius of circles. The answer will be accepted if its relative or absolute error doesn't exceed 10<=-<=6. | [
"3\n0 1\n1 0\n1 1\n",
"7\n2 -3\n-2 -3\n3 0\n-3 -1\n1 -2\n2 -2\n-1 0\n"
] | [
"0.50000000000000000000\n",
"1.58113883008418980000\n"
] | none | [] | 3,368 | 268,390,400 | 0 | 19,857 | |
847 | Dog Show | [
"constructive algorithms",
"data structures",
"greedy"
] | null | null | A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win!
On the show a dog needs to eat as many bowls of dog food as possible (bottomless stomach helps here). Dogs compete separately of each other and the rules are as follows:
At the start of the show the dog and the bowls are located on a line. The dog starts at position *x*<==<=0 and *n* bowls are located at positions *x*<==<=1,<=*x*<==<=2,<=...,<=*x*<==<=*n*. The bowls are numbered from 1 to *n* from left to right. After the show starts the dog immediately begins to run to the right to the first bowl.
The food inside bowls is not ready for eating at the start because it is too hot (dog's self-preservation instinct prevents eating). More formally, the dog can eat from the *i*-th bowl after *t**i* seconds from the start of the show or later.
It takes dog 1 second to move from the position *x* to the position *x*<=+<=1. The dog is not allowed to move to the left, the dog runs only to the right with the constant speed 1 distance unit per second. When the dog reaches a bowl (say, the bowl *i*), the following cases are possible:
- the food had cooled down (i.e. it passed at least *t**i* seconds from the show start): the dog immediately eats the food and runs to the right without any stop, - the food is hot (i.e. it passed less than *t**i* seconds from the show start): the dog has two options: to wait for the *i*-th bowl, eat the food and continue to run at the moment *t**i* or to skip the *i*-th bowl and continue to run to the right without any stop.
After *T* seconds from the start the show ends. If the dog reaches a bowl of food at moment *T* the dog can not eat it. The show stops before *T* seconds if the dog had run to the right of the last bowl.
You need to help your dog create a strategy with which the maximum possible number of bowls of food will be eaten in *T* seconds. | Two integer numbers are given in the first line - *n* and *T* (1<=β€<=*n*<=β€<=200<=000, 1<=β€<=*T*<=β€<=2Β·109) β the number of bowls of food and the time when the dog is stopped.
On the next line numbers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=109) are given, where *t**i* is the moment of time when the *i*-th bowl of food is ready for eating. | Output a single integer β the maximum number of bowls of food the dog will be able to eat in *T* seconds. | [
"3 5\n1 5 3\n",
"1 2\n1\n",
"1 1\n1\n"
] | [
"2\n",
"1\n",
"0\n"
] | In the first example the dog should skip the second bowl to eat from the two bowls (the first and the third). | [
{
"input": "3 5\n1 5 3",
"output": "2"
},
{
"input": "1 2\n1",
"output": "1"
},
{
"input": "1 1\n1",
"output": "0"
},
{
"input": "1 1\n2",
"output": "0"
},
{
"input": "2 2\n2 3",
"output": "0"
},
{
"input": "2 3\n2 1",
"output": "1"
},
{
"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 elements of some set *A* is called a binary relation on set *A*. For two elements *a* and *b* of the set *A* we say that they are in relation Ο, if pair , in this case we use a notation .
Binary relation is equivalence relation, if:
1. It is reflexive (for any *a* it is true that );1. It is symmetric (for any *a*, *b* it is true that if , then );1. It is transitive (if and , than ).
Little Johnny is not completely a fool and he noticed that the first condition is not necessary! Here is his "proof":
Take any two elements, *a* and *b*. If , then (according to property (2)), which means (according to property (3)).
It's very simple, isn't it? However, you noticed that Johnny's "proof" is wrong, and decided to show him a lot of examples that prove him wrong.
Here's your task: count the number of binary relations over a set of size *n* such that they are symmetric, transitive, but not an equivalence relations (i.e. they are not reflexive).
Since their number may be very large (not 0, according to Little Johnny), print the remainder of integer division of this number by 109<=+<=7. | A single line contains a single integer *n* (1<=β€<=*n*<=β€<=4000). | In a single line print the answer to the problem modulo 109<=+<=7. | [
"1\n",
"2\n",
"3\n"
] | [
"1\n",
"3\n",
"10\n"
] | If *n*β=β1 there is only one such relationΒ β an empty one, i.e. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8891a227c918474e5d76377d4644cd7cc01e1ffd.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In other words, for a single element *x* of set *A* the following is hold: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/947c6cf761375432db9bd77796bccc89f1f1546d.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
If *n*β=β2 there are three such relations. Let's assume that set *A* consists of two elements, *x* and *y*. Then the valid relations are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8891a227c918474e5d76377d4644cd7cc01e1ffd.png" style="max-width: 100.0%;max-height: 100.0%;"/>, Οβ=β{(*x*,β*x*)}, Οβ=β{(*y*,β*y*)}. It is easy to see that the three listed binary relations are symmetric and transitive relations, but they are not equivalence relations. | [
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "3"
},
{
"input": "3",
"output": "10"
},
{
"input": "4",
"output": "37"
},
{
"input": "5",
"output": "151"
},
{
"input": "6",
"output": "674"
},
{
"input": "7",
"output": "3263"... | 0 | 0 | -1 | 19,868 | |
226 | Naughty Stone Piles | [
"greedy"
] | null | null | There are *n* piles of stones of sizes *a*1,<=*a*2,<=...,<=*a**n* lying on the table in front of you.
During one move you can take one pile and add it to the other. As you add pile *i* to pile *j*, the size of pile *j* increases by the current size of pile *i*, and pile *i* stops existing. The cost of the adding operation equals the size of the added pile.
Your task is to determine the minimum cost at which you can gather all stones in one pile.
To add some challenge, the stone piles built up conspiracy and decided that each pile will let you add to it not more than *k* times (after that it can only be added to another pile).
Moreover, the piles decided to puzzle you completely and told you *q* variants (not necessarily distinct) of what *k* might equal.
Your task is to find the minimum cost for each of *q* variants. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of stone piles. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β the initial sizes of the stone piles.
The third line contains integer *q* (1<=β€<=*q*<=β€<=105) β the number of queries. The last line contains *q* space-separated integers *k*1,<=*k*2,<=...,<=*k**q* (1<=β€<=*k**i*<=β€<=105) β the values of number *k* for distinct queries. Note that numbers *k**i* can repeat. | Print *q* whitespace-separated integers β the answers to the queries in the order, in which the queries are given in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"5\n2 3 4 1 1\n2\n2 3\n"
] | [
"9 8 "
] | In the first sample one way to get the optimal answer goes like this: we add in turns the 4-th and the 5-th piles to the 2-nd one; then we add the 1-st pile to the 3-rd one; we add the 2-nd pile to the 3-rd one. The first two operations cost 1 each; the third one costs 2, the fourth one costs 5 (the size of the 2-nd pile after the first two operations is not 3, it already is 5).
In the second sample you can add the 2-nd pile to the 3-rd one (the operations costs 3); then the 1-st one to the 3-th one (the cost is 2); then the 5-th one to the 4-th one (the costs is 1); and at last, the 4-th one to the 3-rd one (the cost is 2). | [
{
"input": "5\n2 3 4 1 1\n2\n2 3",
"output": "9 8 "
},
{
"input": "2\n2 9\n5\n4 10 7 3 4",
"output": "2 2 2 2 2 "
},
{
"input": "1\n7\n4\n6 2 3 3",
"output": "0 0 0 0 "
},
{
"input": "2\n7 10\n2\n2 4",
"output": "7 7 "
},
{
"input": "1\n10\n5\n5 3 7 7 1",
"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 to find it modulo 109<=+<=7.
In this problem we define Fibonacci numbers as follows: *f*(1)<==<=1, *f*(2)<==<=1, *f*(*x*)<==<=*f*(*x*<=-<=1)<=+<=*f*(*x*<=-<=2) for all *x*<=><=2.
Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha? | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=100<=000)Β β the number of elements in the array and the number of queries respectively.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109).
Then follow *m* lines with queries descriptions. Each of them contains integers *tp**i*, *l**i*, *r**i* and may be *x**i* (1<=β€<=*tp**i*<=β€<=2, 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*x**i*<=β€<=109). Here *tp**i*<==<=1 corresponds to the queries of the first type and *tp**i* corresponds to the queries of the second type.
It's guaranteed that the input will contains at least one query of the second type. | For each query of the second type print the answer modulo 109<=+<=7. | [
"5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5\n"
] | [
"5\n7\n9\n"
] | Initially, array *a* is equal to 1, 1, 2, 1, 1.
The answer for the first query of the second type is *f*(1)β+β*f*(1)β+β*f*(2)β+β*f*(1)β+β*f*(1)β=β1β+β1β+β1β+β1β+β1β=β5.
After the query 1 2 4 2 array *a* is equal to 1, 3, 4, 3, 1.
The answer for the second query of the second type is *f*(3)β+β*f*(4)β+β*f*(3)β=β2β+β3β+β2β=β7.
The answer for the third query of the second type is *f*(1)β+β*f*(3)β+β*f*(4)β+β*f*(3)β+β*f*(1)β=β1β+β2β+β3β+β2β+β1β=β9. | [
{
"input": "5 4\n1 1 2 1 1\n2 1 5\n1 2 4 2\n2 2 4\n2 1 5",
"output": "5\n7\n9"
},
{
"input": "2 3\n1 3\n2 1 1\n1 1 2 3\n1 1 2 2",
"output": "1"
},
{
"input": "7 4\n2 2 1 1 3 3 2\n2 1 5\n2 6 7\n1 3 4 3\n2 6 6",
"output": "6\n3\n2"
},
{
"input": "9 4\n2 1 2 3 3 3 2 1 3\n2 1 8\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 road should be a curve which lies inside or outside the ring. New roads will have no common points with the ring (except the endpoints of the road).
Now the designers of the constructing plan wonder if it is possible to build the roads in such a way that no two roads intersect (note that the roads may intersect at their endpoints). If it is possible to do, which roads should be inside the ring, and which should be outside? | The first line contains two integers *n* and *m* (4<=β€<=*n*<=β€<=100,<=1<=β€<=*m*<=β€<=100). Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*). No two cities will be connected by more than one road in the list. The list will not contain the roads which exist in the Silver ring. | If it is impossible to build the roads in such a way that no two roads intersect, output Impossible. Otherwise print *m* characters. *i*-th character should be i, if the road should be inside the ring, and o if the road should be outside the ring. If there are several solutions, output any of them. | [
"4 2\n1 3\n2 4\n",
"6 3\n1 3\n3 5\n5 1\n"
] | [
"io\n",
"ooo\n"
] | none | [
{
"input": "4 1\n4 2",
"output": "o"
},
{
"input": "4 2\n1 3\n2 4",
"output": "io"
},
{
"input": "5 1\n3 5",
"output": "o"
},
{
"input": "5 2\n2 4\n4 1",
"output": "oo"
},
{
"input": "5 3\n4 2\n1 3\n5 2",
"output": "oio"
},
{
"input": "5 4\n1 3\n3 5\n1... | 280 | 0 | 0 | 19,979 |
767 | Cartons of milk | [
"binary search",
"data structures",
"greedy",
"sortings",
"two pointers"
] | null | null | Olya likes milk very much. She drinks *k* cartons of milk each day if she has at least *k* and drinks all of them if she doesn't. But there's an issueΒ β expiration dates. Each carton has a date after which you can't drink it (you still can drink it exactly at the date written on the carton). Due to this, if Olya's fridge contains a carton past its expiry date, she throws it away.
Olya hates throwing out cartons, so when she drinks a carton, she chooses the one which expires the fastest. It's easy to understand that this strategy minimizes the amount of cartons thrown out and lets her avoid it if it's even possible.
The main issue Olya has is the one of buying new cartons. Currently, there are *n* cartons of milk in Olya's fridge, for each one an expiration date is known (how soon does it expire, measured in days). In the shop that Olya visited there are *m* cartons, and the expiration date is known for each of those cartons as well.
Find the maximum number of cartons Olya can buy so that she wouldn't have to throw away any cartons. Assume that Olya drank no cartons today. | In the first line there are three integers *n*, *m*, *k* (1<=β€<=*n*,<=*m*<=β€<=106, 1<=β€<=*k*<=β€<=*n*<=+<=*m*)Β β the amount of cartons in Olya's fridge, the amount of cartons in the shop and the number of cartons Olya drinks each day.
In the second line there are *n* integers *f*1,<=*f*2,<=...,<=*f**n* (0<=β€<=*f**i*<=β€<=107)Β β expiration dates of the cartons in Olya's fridge. The expiration date is expressed by the number of days the drinking of this carton can be delayed. For example, a 0 expiration date means it must be drunk today, 1Β β no later than tomorrow, etc.
In the third line there are *m* integers *s*1,<=*s*2,<=...,<=*s**m* (0<=β€<=*s**i*<=β€<=107)Β β expiration dates of the cartons in the shop in a similar format. | If there's no way for Olya to drink the cartons she already has in her fridge, print -1.
Otherwise, in the first line print the maximum number *x* of cartons which Olya can buy so that she wouldn't have to throw a carton away. The next line should contain exactly *x* integersΒ β the numbers of the cartons that should be bought (cartons are numbered in an order in which they are written in the input, starting with 1). Numbers should not repeat, but can be in arbitrary order. If there are multiple correct answers, print any of them. | [
"3 6 2\n1 0 1\n2 0 2 0 0 2\n",
"3 1 2\n0 0 0\n1\n",
"2 1 2\n0 1\n0\n"
] | [
"3\n1 2 3",
"-1",
"1\n1 "
] | In the first example *k*β=β2 and Olya has three cartons with expiry dates 0, 1 and 1 (they expire today, tomorrow and tomorrow), and the shop has 3 cartons with expiry date 0 and 3 cartons with expiry date 2. Olya can buy three cartons, for example, one with the expiry date 0 and two with expiry date 2.
In the second example all three cartons Olya owns expire today and it means she would have to throw packets away regardless of whether she buys an extra one or not.
In the third example Olya would drink *k*β=β2 cartons today (one she alreay has in her fridge and one from the shop) and the remaining one tomorrow. | [] | 46 | 0 | 0 | 19,985 | |
997 | Cycles in product | [
"combinatorics",
"divide and conquer",
"trees"
] | null | null | Consider a tree (that is, an undirected connected graph without loops) $T_1$ and a tree $T_2$. Let's define their cartesian product $T_1 \times T_2$ in a following way.
Let $V$ be the set of vertices in $T_1$ and $U$ be the set of vertices in $T_2$.
Then the set of vertices of graph $T_1 \times T_2$ is $V \times U$, that is, a set of ordered pairs of vertices, where the first vertex in pair is from $V$ and the secondΒ β from $U$.
Let's draw the following edges:
- Between $(v, u_1)$ and $(v, u_2)$ there is an undirected edge, if $u_1$ and $u_2$ are adjacent in $U$. - Similarly, between $(v_1, u)$ and $(v_2, u)$ there is an undirected edge, if $v_1$ and $v_2$ are adjacent in $V$.
Please see the notes section for the pictures of products of trees in the sample tests.
Let's examine the graph $T_1 \times T_2$. How much cycles (not necessarily simple) of length $k$ it contains? Since this number can be very large, print it modulo $998244353$.
The sequence of vertices $w_1$, $w_2$, ..., $w_k$, where $w_i \in V \times U$ called cycle, if any neighboring vertices are adjacent and $w_1$ is adjacent to $w_k$. Cycles that differ only by the cyclic shift or direction of traversal are still considered different. | First line of input contains three integersΒ β $n_1$, $n_2$ and $k$ ($2 \le n_1, n_2 \le 4000$, $2 \le k \le 75$)Β β number of vertices in the first tree, number of vertices in the second tree and the cycle length respectively.
Then follow $n_1 - 1$ lines describing the first tree. Each of this lines contains two integersΒ β $v_i, u_i$ ($1 \le v_i, u_i \le n_1$), which define edges of the first tree.
Then follow $n_2 - 1$ lines, which describe the second tree in the same format.
It is guaranteed, that given graphs are trees. | Print one integerΒ β number of cycles modulo $998244353$. | [
"2 2 2\n1 2\n1 2\n",
"2 2 4\n1 2\n1 2\n",
"2 3 4\n1 2\n1 2\n1 3\n",
"4 2 2\n1 2\n1 3\n1 4\n1 2\n"
] | [
"8\n",
"32\n",
"70\n",
"20\n"
] | The following three pictures illustrate graph, which are products of the trees from sample tests.
In the first example, the list of cycles of length $2$ is as follows:
- Β«ABΒ», Β«BAΒ» - Β«BCΒ», Β«CBΒ» - Β«ADΒ», Β«DAΒ» - Β«CDΒ», Β«DCΒ» | [] | 77 | 0 | 0 | 20,012 | |
348 | Apple Tree | [
"dfs and similar",
"number theory",
"trees"
] | null | null | You are given a rooted tree with *n* vertices. In each leaf vertex there's a single integer β the number of apples in this vertex.
The weight of a subtree is the sum of all numbers in this subtree leaves. For instance, the weight of a subtree that corresponds to some leaf is the number written in the leaf.
A tree is balanced if for every vertex *v* of the tree all its subtrees, corresponding to the children of vertex *v*, are of equal weight.
Count the minimum number of apples that you need to remove from the tree (specifically, from some of its leaves) in order to make the tree balanced. Notice that you can always achieve the goal by just removing all apples. | The first line contains integer *n* (2<=β€<=*n*<=β€<=105), showing the number of vertices in the tree. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=108), *a**i* is the number of apples in the vertex number *i*. The number of apples in non-leaf vertices is guaranteed to be zero.
Then follow *n*<=-<=1 lines, describing the tree edges. Each line contains a pair of integers *x**i*,<=*y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*,<=*x**i*<=β <=*y**i*) β the vertices connected by an edge.
The vertices are indexed from 1 to *n*. Vertex 1 is the root. | Print a single integer β the minimum number of apples to remove in order to make the tree balanced.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the sin, cout streams cin, cout or the %I64d specifier. | [
"6\n0 0 12 13 5 6\n1 2\n1 3\n1 4\n2 5\n2 6\n"
] | [
"6"
] | none | [] | 2,000 | 17,612,800 | 0 | 20,027 | |
113 | Sleeping | [
"combinatorics",
"implementation",
"math"
] | E. Sleeping | 2 | 256 | One day Vasya was lying in bed watching his electronic clock to fall asleep quicker.
Vasya lives in a strange country, where days have *h* hours, and every hour has *m* minutes. Clock shows time in decimal number system, in format H:M, where the string H always has a fixed length equal to the number of digits in the decimal representation of number *h*<=-<=1. To achieve this, leading zeros are added if necessary. The string M has a similar format, and its length is always equal to the number of digits in the decimal representation of number *m*<=-<=1. For example, if *h*<==<=17, *m*<==<=1000, then time equal to 13 hours and 75 minutes will be displayed as "13:075".
Vasya had been watching the clock from *h*1 hours *m*1 minutes to *h*2 hours *m*2 minutes inclusive, and then he fell asleep. Now he asks you to count how many times he saw the moment at which at least *k* digits changed on the clock simultaneously.
For example, when switching 04:19 <=β<= 04:20 two digits change. When switching 23:59 <=β<= 00:00, four digits change.
Consider that Vasya has been watching the clock for strictly less than one day. Note that the last time Vasya saw on the clock before falling asleep was "h2:m2". That is, Vasya didn't see the moment at which time "h2:m2" switched to the next value. | The first line of the input file contains three space-separated integers *h*, *m* and *k* (2<=β€<=*h*,<=*m*<=β€<=109, 1<=β€<=*k*<=β€<=20). The second line contains space-separated integers *h*1, *m*1 (0<=β€<=*h*1<=<<=*h*, 0<=β€<=*m*1<=<<=*m*). The third line contains space-separated integers *h*2, *m*2 (0<=β€<=*h*2<=<<=*h*, 0<=β€<=*m*2<=<<=*m*). | Print a single number β the number of times Vasya saw the moment of changing at least *k* digits simultaneously.
Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin stream (also you may use the %I64d specificator). | [
"5 5 2\n4 4\n2 1\n",
"24 60 1\n0 0\n23 59\n",
"24 60 3\n23 59\n23 59\n"
] | [
"3\n",
"1439\n",
"0\n"
] | In the first example Vasya will see the following moments of time: 4:4 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 0:0 βββ 0:1 βββ 0:2 βββ 0:3 βββ 0:4 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1:0 βββ 1:1 βββ 1:2 βββ 1:3 βββ 1:4 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2:0 βββ 2:1 βββ 2:2 βββ 2:3 βββ 2:4. Double arrow (<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/>) marks the sought moments of time (in this example β when Vasya sees two numbers changing simultaneously).
In the second example *k*β=β1. Any switching time can be accepted, since during switching of the clock at least one digit is changed. Total switching equals to 24Β·60β=β1440, but Vasya have not seen one of them β the switching of 23:59 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4d97e684117250a9afe9be022ab8a63653dd15aa.png" style="max-width: 100.0%;max-height: 100.0%;"/> 00:00.
In the third example Vasya fell asleep immediately after he began to look at the clock, so he did not see any change. | [] | 0 | 0 | -1 | 20,076 |
348 | Subset Sums | [
"brute force",
"data structures"
] | null | null | You are given an array *a*1,<=*a*2,<=...,<=*a**n* and *m* sets *S*1,<=*S*2,<=...,<=*S**m* of indices of elements of this array. Let's denote *S**k*<==<={*S**k*,<=*i*}Β (1<=β€<=*i*<=β€<=|*S**k*|). In other words, *S**k*,<=*i* is some element from set *S**k*.
In this problem you have to answer *q* queries of the two types:
1. Find the sum of elements with indices from set *S**k*: . The query format is "? k". 1. Add number *x* to all elements at indices from set *S**k*: *a**S**k*,<=*i* is replaced by *a**S**k*,<=*i*<=+<=*x* for all *i* (1<=β€<=*i*<=β€<=|*S**k*|). The query format is "+ k x".
After each first type query print the required sum. | The first line contains integers *n*,<=*m*,<=*q* (1<=β€<=*n*,<=*m*,<=*q*<=β€<=105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (|*a**i*|<=β€<=108) β elements of array *a*.
Each of the following *m* lines describes one set of indices. The *k*-th line first contains a positive integer, representing the number of elements in set (|*S**k*|), then follow |*S**k*| distinct integers *S**k*,<=1,<=*S**k*,<=2,<=...,<=*S**k*,<=|*S**k*| (1<=β€<=*S**k*,<=*i*<=β€<=*n*) β elements of set *S**k*.
The next *q* lines contain queries. Each query looks like either "? k" or "+ k x" and sits on a single line. For all queries the following limits are held: 1<=β€<=*k*<=β€<=*m*, |*x*|<=β€<=108. The queries are given in order they need to be answered.
It is guaranteed that the sum of sizes of all sets *S**k* doesn't exceed 105. | After each first type query print the required sum on a single line.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"5 3 5\n5 -5 5 1 -4\n2 1 2\n4 2 1 4 5\n2 2 5\n? 2\n+ 3 4\n? 1\n+ 2 1\n? 2\n"
] | [
"-3\n4\n9\n"
] | none | [
{
"input": "5 3 5\n5 -5 5 1 -4\n2 1 2\n4 2 1 4 5\n2 2 5\n? 2\n+ 3 4\n? 1\n+ 2 1\n? 2",
"output": "-3\n4\n9"
},
{
"input": "10 10 10\n0 0 0 1 -5 8 7 2 9 -2\n8 1 9 7 8 2 5 10 3\n2 7 10\n8 8 7 1 4 2 5 10 9\n2 7 5\n8 9 3 6 4 8 1 2 10\n2 9 2\n8 4 2 9 1 10 7 5 3\n2 3 6\n8 2 8 5 9 4 1 6 10\n2 5 2\n? 1\n? 3... | 154 | 2,867,200 | -1 | 20,110 | |
455 | A Lot of Games | [
"dfs and similar",
"dp",
"games",
"implementation",
"strings",
"trees"
] | null | null | Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players.
Given a group of *n* non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resulting word must be prefix of at least one string from the group. A player loses if he cannot move.
Andrew and Alex decided to play this game *k* times. The player who is the loser of the *i*-th game makes the first move in the (*i*<=+<=1)-th game. Guys decided that the winner of all games is the player who wins the last (*k*-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him. | The first line contains two integers, *n* and *k* (1<=β€<=*n*<=β€<=105; 1<=β€<=*k*<=β€<=109).
Each of the next *n* lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters. | If the player who moves first wins, print "First", otherwise print "Second" (without the quotes). | [
"2 3\na\nb\n",
"3 1\na\nb\nc\n",
"1 2\nab\n"
] | [
"First\n",
"First\n",
"Second\n"
] | none | [
{
"input": "2 3\na\nb",
"output": "First"
},
{
"input": "3 1\na\nb\nc",
"output": "First"
},
{
"input": "1 2\nab",
"output": "Second"
},
{
"input": "5 6\nabas\ndsfdf\nabacaba\ndartsidius\nkolobok",
"output": "Second"
},
{
"input": "4 2\naaaa\nbbbb\nccccc\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 walrus standing in front of him, that is, if exists such *j* (*i*<=<<=*j*), that *a**i*<=><=*a**j*. The displeasure of the *i*-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the *i*-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of *n* walruses in the queue his displeasure. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105) β the number of walruses in the queue. The second line contains integers *a**i* (1<=β€<=*a**i*<=β€<=109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one. | Print *n* numbers: if the *i*-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the *i*-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus. | [
"6\n10 8 5 3 50 45\n",
"7\n10 4 6 3 2 8 15\n",
"5\n10 3 1 10 11\n"
] | [
"2 1 0 -1 0 -1 ",
"4 2 1 0 -1 -1 -1 ",
"1 0 -1 -1 -1 "
] | none | [
{
"input": "6\n10 8 5 3 50 45",
"output": "2 1 0 -1 0 -1 "
},
{
"input": "7\n10 4 6 3 2 8 15",
"output": "4 2 1 0 -1 -1 -1 "
},
{
"input": "5\n10 3 1 10 11",
"output": "1 0 -1 -1 -1 "
},
{
"input": "13\n18 9 8 9 23 20 18 18 33 25 31 37 36",
"output": "2 0 -1 -1 2 1 -1 -1 ... | 124 | 0 | 0 | 20,175 |
53 | Blog Photo | [
"binary search",
"implementation"
] | B. Blog Photo | 2 | 256 | One popular blog site edits the uploaded photos like this. It cuts a rectangular area out of them so that the ratio of height to width (i.e. the *height*<=/<=*width* quotient) can vary from 0.8 to 1.25 inclusively. Besides, at least one side of the cut area should have a size, equal to some power of number 2 (2*x* for some integer *x*). If those rules don't indicate the size of the cut are clearly, then the way with which the cut part possesses the largest area is chosen. Of course, both sides of the cut area should be integer. If there are several answers to this problem, you should choose the answer with the maximal height. | The first line contains a pair of integers *h* and *w* (1<=β€<=*h*,<=*w*<=β€<=109) which are the height and width of the uploaded photo in pixels. | Print two integers which are the height and width of the cut area. | [
"2 1\n",
"2 2\n",
"5 5\n"
] | [
"1 1\n",
"2 2\n",
"5 4\n"
] | none | [
{
"input": "2 1",
"output": "1 1"
},
{
"input": "2 2",
"output": "2 2"
},
{
"input": "5 5",
"output": "5 4"
},
{
"input": "9 10",
"output": "8 10"
},
{
"input": "15 13",
"output": "10 8"
},
{
"input": "47 46",
"output": "40 32"
},
{
"input"... | 186 | 0 | 3.9535 | 20,278 |
425 | Sereja and Table | [
"bitmasks",
"greedy"
] | null | null | Sereja has an *n*<=Γ<=*m* rectangular table *a*, each cell of the table contains a zero or a number one. Sereja wants his table to meet the following requirement: each connected component of the same values forms a rectangle with sides parallel to the sides of the table. Rectangles should be filled with cells, that is, if a component form a rectangle of size *h*<=Γ<=*w*, then the component must contain exactly *hw* cells.
A connected component of the same values is a set of cells of the table that meet the following conditions:
- every two cells of the set have the same value; - the cells of the set form a connected region on the table (two cells are connected if they are adjacent in some row or some column of the table); - it is impossible to add any cell to the set unless we violate the two previous conditions.
Can Sereja change the values of at most *k* cells of the table so that the table met the described requirement? What minimum number of table cells should he change in this case? | The first line contains integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=100;Β 1<=β€<=*k*<=β€<=10). Next *n* lines describe the table *a*: the *i*-th of them contains *m* integers *a**i*1,<=*a**i*2,<=...,<=*a**im* (0<=β€<=*a**i*,<=*j*<=β€<=1) β the values in the cells of the *i*-th row. | Print -1, if it is impossible to meet the requirement. Otherwise, print the minimum number of cells which should be changed. | [
"5 5 2\n1 1 1 1 1\n1 1 1 1 1\n1 1 0 1 1\n1 1 1 1 1\n1 1 1 1 1\n",
"3 4 1\n1 0 0 0\n0 1 1 1\n1 1 1 0\n",
"3 4 1\n1 0 0 1\n0 1 1 0\n1 0 0 1\n"
] | [
"1\n",
"-1\n",
"0\n"
] | none | [
{
"input": "5 5 2\n1 1 1 1 1\n1 1 1 1 1\n1 1 0 1 1\n1 1 1 1 1\n1 1 1 1 1",
"output": "1"
},
{
"input": "3 4 1\n1 0 0 0\n0 1 1 1\n1 1 1 0",
"output": "-1"
},
{
"input": "3 4 1\n1 0 0 1\n0 1 1 0\n1 0 0 1",
"output": "0"
},
{
"input": "8 1 4\n0\n0\n0\n1\n0\n1\n1\n0",
"output... | 108 | 409,600 | 3 | 20,317 | |
747 | Winter Is Coming | [
"dp",
"greedy",
"sortings"
] | null | null | The winter in Berland lasts *n* days. For each day we know the forecast for the average air temperature that day.
Vasya has a new set of winter tires which allows him to drive safely no more than *k* days at any average air temperature. After *k* days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these *k* days form a continuous segment of days.
Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative.
Vasya can change summer tires to winter tires and vice versa at the beginning of any day.
Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. | The first line contains two positive integers *n* and *k* (1<=β€<=*n*<=β€<=2Β·105, 0<=β€<=*k*<=β€<=*n*)Β β the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than *k* days in total.
The second line contains a sequence of *n* integers *t*1,<=*t*2,<=...,<=*t**n* (<=-<=20<=β€<=*t**i*<=β€<=20)Β β the average air temperature in the *i*-th winter day. | Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. | [
"4 3\n-5 20 -3 0\n",
"4 2\n-5 20 -3 0\n",
"10 6\n2 -5 1 3 0 0 -4 -3 1 0\n"
] | [
"2\n",
"4\n",
"3\n"
] | In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two.
In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four. | [
{
"input": "4 3\n-5 20 -3 0",
"output": "2"
},
{
"input": "4 2\n-5 20 -3 0",
"output": "4"
},
{
"input": "10 6\n2 -5 1 3 0 0 -4 -3 1 0",
"output": "3"
},
{
"input": "4 4\n-5 20 -3 0",
"output": "1"
},
{
"input": "4 1\n-5 20 -3 0",
"output": "-1"
},
{
"... | 202 | 17,305,600 | 0 | 20,369 | |
0 | none | [
"none"
] | null | null | Stepan has a set of *n* strings. Also, he has a favorite string *s*.
Stepan wants to do the following. He will take some strings of his set and write them down one after another. It is possible that he will take some strings more than once, and will not take some of them at all.
Your task is to determine the minimum number of strings in the set which Stepan needs to take and write so that the string *s* appears as a subsequence in the resulting written down string.
For example, in the string "abcd" strings "ad", "acd", "abcd" appear as subsequences, and strings "ba", "abdc" don't appear as subsequences. | The first line contains the integer *n* (1<=β€<=*n*<=β€<=50) β the number of strings in Stepan's set.
The next *n* lines contain *n* non-empty strings consisting of lowercase letters of the English alphabet. The length of each of these strings does not exceed 50 symbols. It is possible that some strings from Stepan's set are the same.
The next line contains the non-empty string *s*, consisting of lowercase letters of the English alphabet β Stepan's favorite string. The length of this string doesn't exceed 2500 symbols. | Print the minimum number of strings which Stepan should take from the set and write them down one after another so that the string *s* appears as a subsequence in the resulting written down string. Each string from the set should be counted as many times as Stepan takes it from the set.
If the answer doesn't exsist, print -1. | [
"3\na\naa\na\naaa\n",
"4\nab\naab\naa\nbb\nbaaab\n",
"2\naaa\nbbb\naaacbbb\n"
] | [
"2\n",
"3\n",
"-1\n"
] | In the first test, Stepan can take, for example, the third and the second strings from the set, write them down, and get exactly his favorite string.
In the second example Stepan can take, for example, the second, the third and again the second strings from the set and write them down. Then he will get a string "aabaaaab", in which his favorite string "baaab" is a subsequence.
In the third test Stepan can not get his favorite string, because it contains the letter "c", which is not presented in any of the strings in the set. | [] | 46 | 0 | 0 | 20,482 | |
985 | Isomorphic Strings | [
"hashing",
"strings"
] | null | null | You are given a string *s* of length *n* consisting of lowercase English letters.
For two given strings *s* and *t*, say *S* is the set of distinct characters of *s* and *T* is the set of distinct characters of *t*. The strings *s* and *t* are isomorphic if their lengths are equal and there is a one-to-one mapping (bijection) *f* between *S* and *T* for which *f*(*s**i*)<==<=*t**i*. Formally:
1. *f*(*s**i*)<==<=*t**i* for any index *i*, 1. for any character there is exactly one character that *f*(*x*)<==<=*y*, 1. for any character there is exactly one character that *f*(*x*)<==<=*y*.
For example, the strings "aababc" and "bbcbcz" are isomorphic. Also the strings "aaaww" and "wwwaa" are isomorphic. The following pairs of strings are not isomorphic: "aab" and "bbb", "test" and "best".
You have to handle *m* queries characterized by three integers *x*,<=*y*,<=*len* (1<=β€<=*x*,<=*y*<=β€<=*n*<=-<=*len*<=+<=1). For each query check if two substrings *s*[*x*... *x*<=+<=*len*<=-<=1] and *s*[*y*... *y*<=+<=*len*<=-<=1] are isomorphic. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*m*<=β€<=2Β·105) β the length of the string *s* and the number of queries.
The second line contains string *s* consisting of *n* lowercase English letters.
The following *m* lines contain a single query on each line: *x**i*, *y**i* and *len**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*, 1<=β€<=*len**i*<=β€<=*n*<=-<=*max*(*x**i*,<=*y**i*)<=+<=1) β the description of the pair of the substrings to check. | For each query in a separate line print "YES" if substrings *s*[*x**i*... *x**i*<=+<=*len**i*<=-<=1] and *s*[*y**i*... *y**i*<=+<=*len**i*<=-<=1] are isomorphic and "NO" otherwise. | [
"7 4\nabacaba\n1 1 1\n1 4 2\n2 1 3\n2 4 3\n"
] | [
"YES\nYES\nNO\nYES\n"
] | The queries in the example are following:
1. substrings "a" and "a" are isomorphic: *f*(*a*)β=β*a*; 1. substrings "ab" and "ca" are isomorphic: *f*(*a*)β=β*c*, *f*(*b*)β=β*a*; 1. substrings "bac" and "aba" are not isomorphic since *f*(*b*) and *f*(*c*) must be equal to *a* at same time; 1. substrings "bac" and "cab" are isomorphic: *f*(*b*)β=β*c*, *f*(*a*)β=β*a*, *f*(*c*)β=β*b*. | [
{
"input": "7 4\nabacaba\n1 1 1\n1 4 2\n2 1 3\n2 4 3",
"output": "YES\nYES\nNO\nYES"
},
{
"input": "1 2\nz\n1 1 1\n1 1 1",
"output": "YES\nYES"
},
{
"input": "36 4\naababcbbcbczaaawwwwwaaaabbbbtestbest\n1 7 6\n13 18 5\n23 26 3\n29 33 4",
"output": "YES\nYES\nNO\nNO"
},
{
"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.