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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
983 | NN country | [
"binary search",
"data structures",
"trees"
] | null | null | In the NN country, there are $n$ cities, numbered from $1$ to $n$, and $n - 1$ roads, connecting them. There is a roads path between any two cities.
There are $m$ bidirectional bus routes between cities. Buses drive between two cities taking the shortest path with stops in every city they drive through. Travelling by bus, you can travel from any stop on the route to any other. You can travel between cities only by bus.
You are interested in $q$ questions: is it possible to get from one city to another and what is the minimum number of buses you need to use for it? | The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$)Β β the number of cities.
The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \le p_i < i$), where $p_i$ means that cities $p_i$ and $i$ are connected by road.
The third line contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$)Β β the number of bus routes.
Each of the next $m$ lines contains $2$ integers $a$ and $b$ ($1 \le a, b \le n$, $a \neq b$), meaning that there is a bus route between cities $a$ and $b$. It is possible that there is more than one route between two cities.
The next line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$)Β β the number of questions you are interested in.
Each of the next $q$ lines contains $2$ integers $v$ and $u$ ($1 \le v, u \le n$, $v \neq u$), meaning that you are interested if it is possible to get from city $v$ to city $u$ and what is the minimum number of buses you need to use for it. | Print the answer for each question on a separate line. If there is no way to get from one city to another, print $-1$. Otherwise print the minimum number of buses you have to use. | [
"7\n1 1 1 4 5 6\n4\n4 2\n5 4\n1 3\n6 7\n6\n4 5\n3 5\n7 2\n4 5\n3 2\n5 3\n",
"7\n1 1 2 3 4 1\n4\n4 7\n3 5\n7 6\n7 6\n6\n4 6\n3 1\n3 2\n2 7\n6 3\n5 3\n"
] | [
"1\n3\n-1\n1\n2\n3\n",
"1\n-1\n-1\n1\n-1\n1\n"
] | [] | 3,000 | 7,168,000 | 0 | 37,502 | ||
453 | Little Pony and Summer Sun Celebration | [
"constructive algorithms",
"dfs and similar",
"graphs"
] | null | null | Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration.
Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?
Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4*n* places. | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=105;Β 0<=β€<=*m*<=β€<=105) β the number of places and the number of roads in Ponyville. Each of the following *m* lines contains two integers *u**i*,<=*v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*;Β *u**i*<=β <=*v**i*), these integers describe a road between places *u**i* and *v**i*.
The next line contains *n* integers: *x*1,<=*x*2,<=...,<=*x**n* (0<=β€<=*x**i*<=β€<=1) β the parity of the number of times that each place must be visited. If *x**i*<==<=0, then the *i*-th place must be visited even number of times, else it must be visited odd number of times. | Output the number of visited places *k* in the first line (0<=β€<=*k*<=β€<=4*n*). Then output *k* integers β the numbers of places in the order of path. If *x**i*<==<=0, then the *i*-th place must appear in the path even number of times, else *i*-th place must appear in the path odd number of times. Note, that given road system has no self-loops, therefore any two neighbouring places in the path must be distinct.
If there is no required path, output -1. If there multiple possible paths, you can output any of them. | [
"3 2\n1 2\n2 3\n1 1 1\n",
"5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0\n",
"2 0\n0 0\n"
] | [
"3\n1 2 3\n",
"10\n2 1 3 4 5 4 5 4 3 1 ",
"0\n"
] | none | [
{
"input": "3 2\n1 2\n2 3\n1 1 1",
"output": "3\n1 2 3"
},
{
"input": "5 7\n1 2\n1 3\n1 4\n1 5\n3 4\n3 5\n4 5\n0 1 0 1 0",
"output": "10\n2 1 3 4 5 4 5 4 3 1 "
},
{
"input": "2 0\n0 0",
"output": "0"
},
{
"input": "10 10\n2 1\n2 3\n4 2\n4 5\n3 6\n5 7\n8 4\n4 9\n5 10\n4 7\n0 0... | 967 | 28,160,000 | 3 | 37,586 | |
0 | none | [
"none"
] | null | null | Gerald plays the following game. He has a checkered field of size *n*<=Γ<=*n* cells, where *m* various cells are banned. Before the game, he has to put a few chips on some border (but not corner) board cells. Then for *n*<=-<=1 minutes, Gerald every minute moves each chip into an adjacent cell. He moves each chip from its original edge to the opposite edge. Gerald loses in this game in each of the three cases:
- At least one of the chips at least once fell to the banned cell. - At least once two chips were on the same cell. - At least once two chips swapped in a minute (for example, if you stand two chips on two opposite border cells of a row with even length, this situation happens in the middle of the row).
In that case he loses and earns 0 points. When nothing like that happened, he wins and earns the number of points equal to the number of chips he managed to put on the board. Help Gerald earn the most points. | The first line contains two space-separated integers *n* and *m* (2<=β€<=*n*<=β€<=1000, 0<=β€<=*m*<=β€<=105) β the size of the field and the number of banned cells. Next *m* lines each contain two space-separated integers. Specifically, the *i*-th of these lines contains numbers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*) β the coordinates of the *i*-th banned cell. All given cells are distinct.
Consider the field rows numbered from top to bottom from 1 to *n*, and the columns β from left to right from 1 to *n*. | Print a single integer β the maximum points Gerald can earn in this game. | [
"3 1\n2 2\n",
"3 0\n",
"4 3\n3 1\n3 2\n3 3\n"
] | [
"0\n",
"1\n",
"1\n"
] | In the first test the answer equals zero as we can't put chips into the corner cells.
In the second sample we can place one chip into either cell (1, 2), or cell (3, 2), or cell (2, 1), or cell (2, 3). We cannot place two chips.
In the third sample we can only place one chip into either cell (2, 1), or cell (2, 4). | [
{
"input": "3 1\n2 2",
"output": "0"
},
{
"input": "3 0",
"output": "1"
},
{
"input": "4 3\n3 1\n3 2\n3 3",
"output": "1"
},
{
"input": "2 1\n1 1",
"output": "0"
},
{
"input": "2 3\n1 2\n2 1\n2 2",
"output": "0"
},
{
"input": "5 1\n3 2",
"output": ... | 156 | 0 | 0 | 37,587 | |
40 | Interesting Sequence | [
"math"
] | D. Interesting Sequence | 3 | 256 | Berland scientists noticed long ago that the world around them depends on Berland population. Due to persistent research in this area the scientists managed to find out that the Berland chronology starts from the moment when the first two people came to that land (it is considered to have happened in the first year). After one Berland year after the start of the chronology the population had already equaled 13 people (the second year). However, tracing the population number during the following years was an ultimately difficult task, still it was found out that if *d**i* β the number of people in Berland in the year of *i*, then either *d**i*<==<=12*d**i*<=-<=2, or *d**i*<==<=13*d**i*<=-<=1<=-<=12*d**i*<=-<=2. Of course no one knows how many people are living in Berland at the moment, but now we can tell if there could possibly be a year in which the country population equaled *A*. That's what we ask you to determine. Also, if possible, you have to find out in which years it could be (from the beginning of Berland chronology). Let's suppose that it could be in the years of *a*1,<=*a*2,<=...,<=*a**k*. Then you have to define how many residents could be in the country during those years apart from the *A* variant. Look at the examples for further explanation. | The first line contains integer *A* (1<=β€<=*A*<=<<=10300). It is guaranteed that the number doesn't contain leading zeros. | On the first output line print YES, if there could be a year in which the total population of the country equaled *A*, otherwise print NO.
If the answer is YES, then you also have to print number *k* β the number of years in which the population could equal *A*. On the next line you have to output precisely *k* space-separated numbers β *a*1,<=*a*2,<=...,<=*a**k*. Those numbers have to be output in the increasing order.
On the next line you should output number *p* β how many variants of the number of people could be in the years of *a*1,<=*a*2,<=...,<=*a**k*, apart from the *A* variant. On each of the next *p* lines you have to print one number β the sought number of residents. Those number also have to go in the increasing order.
If any number (or both of them) *k* or *p* exceeds 1000, then you have to print 1000 instead of it and only the first 1000 possible answers in the increasing order.
The numbers should have no leading zeros. | [
"2\n",
"3\n",
"13\n",
"1729\n"
] | [
"YES\n1\n1\n0\n",
"NO\n",
"YES\n1\n2\n0\n",
"YES\n1\n4\n1\n156\n"
] | none | [
{
"input": "2",
"output": "YES\n1\n1\n0"
},
{
"input": "3",
"output": "NO"
},
{
"input": "13",
"output": "YES\n1\n2\n0"
},
{
"input": "1729",
"output": "YES\n1\n4\n1\n156"
},
{
"input": "1",
"output": "NO"
},
{
"input": "156",
"output": "YES\n1\n4\... | 248 | 20,992,000 | -1 | 37,614 |
0 | none | [
"none"
] | null | null | Iahub is so happy about inventing bubble sort graphs that he's staying all day long at the office and writing permutations. Iahubina is angry that she is no more important for Iahub. When Iahub goes away, Iahubina comes to his office and sabotage his research work.
The girl finds an important permutation for the research. The permutation contains *n* distinct integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=*n*). She replaces some of permutation elements with -1 value as a revenge.
When Iahub finds out his important permutation is broken, he tries to recover it. The only thing he remembers about the permutation is it didn't have any fixed point. A fixed point for a permutation is an element *a**k* which has value equal to *k* (*a**k*<==<=*k*). Your job is to proof to Iahub that trying to recover it is not a good idea. Output the number of permutations which could be originally Iahub's important permutation, modulo 1000000007 (109<=+<=7). | The first line contains integer *n* (2<=β€<=*n*<=β€<=2000). On the second line, there are *n* integers, representing Iahub's important permutation after Iahubina replaces some values with -1.
It's guaranteed that there are no fixed points in the given permutation. Also, the given sequence contains at least two numbers -1 and each positive number occurs in the sequence at most once. It's guaranteed that there is at least one suitable permutation. | Output a single integer, the number of ways Iahub could recover his permutation, modulo 1000000007 (109<=+<=7). | [
"5\n-1 -1 4 3 -1\n"
] | [
"2\n"
] | For the first test example there are two permutations with no fixed points are [2, 5, 4, 3, 1] and [5, 1, 4, 3, 2]. Any other permutation would have at least one fixed point. | [
{
"input": "5\n-1 -1 4 3 -1",
"output": "2"
},
{
"input": "8\n2 4 5 3 -1 8 -1 6",
"output": "1"
},
{
"input": "7\n-1 -1 4 -1 7 1 6",
"output": "4"
},
{
"input": "6\n-1 -1 -1 -1 -1 -1",
"output": "265"
},
{
"input": "2\n-1 -1",
"output": "1"
},
{
"input... | 1,000 | 23,961,600 | 0 | 37,643 | |
363 | Fixing Typos | [
"greedy",
"implementation"
] | null | null | Many modern text editors automatically check the spelling of the user's text. Some editors even suggest how to correct typos.
In this problem your task to implement a small functionality to correct two types of typos in a word. We will assume that three identical letters together is a typo (for example, word "helllo" contains a typo). Besides, a couple of identical letters immediately followed by another couple of identical letters is a typo too (for example, words "helloo" and "wwaatt" contain typos).
Write a code that deletes the minimum number of letters from a word, correcting described typos in the word. You are allowed to delete letters from both ends and from the middle of the word. | The single line of the input contains word *s*, its length is from 1 to 200000 characters. The given word *s* consists of lowercase English letters. | Print such word *t* that it doesn't contain any typos described in the problem statement and is obtained from *s* by deleting the least number of letters.
If there are multiple solutions, print any of them. | [
"helloo\n",
"woooooow\n"
] | [
"hello\n",
"woow\n"
] | The second valid answer to the test from the statement is "heloo". | [
{
"input": "helloo",
"output": "hello"
},
{
"input": "woooooow",
"output": "woow"
},
{
"input": "aabbaa",
"output": "aabaa"
},
{
"input": "yesssssss",
"output": "yess"
},
{
"input": "aabbaabbaabbaabbaabbaabbcccccc",
"output": "aabaabaabaabaabaabcc"
},
{
... | 296 | 11,776,000 | 3 | 37,760 | |
200 | Cinema | [
"brute force",
"data structures"
] | null | null | The capital of Berland has the only movie theater in the country. Besides, it consists of only one room. The room is divided into *n* rows, each row consists of *m* seats.
There are *k* people lined up to the box office, each person wants to buy exactly one ticket for his own entertainment. Before the box office started selling tickets, each person found the seat that seemed best for him and remembered it as a pair of coordinates (*x**i*,<=*y**i*), where *x**i* is the row number, and *y**i* is the seat number in this row.
It is possible that some people have chosen the same place, then when some people see their favorite seat taken in the plan of empty seats in the theater, they choose and buy a ticket to another place. Each of them has the following logic: let's assume that he originally wanted to buy a ticket to seat (*x*1,<=*y*1), then when he comes to the box office, he chooses such empty seat (*x*2,<=*y*2), which satisfies the following conditions:
- the value of |*x*1<=-<=*x*2|<=+<=|*y*1<=-<=*y*2| is minimum - if the choice is not unique, then among the seats that satisfy the first condition, this person selects the one for which the value of *x*2 is minimum - if the choice is still not unique, among the seats that satisfy the first and second conditions, this person selects the one for which the value of *y*2 is minimum
Your task is to find the coordinates of a seat for each person. | The first input line contains three integers *n*, *m*, *k* (1<=β€<=*n*,<=*m*<=β€<=2000, 1<=β€<=*k*<=β€<=*min*(*n*Β·*m*,<=105) β the number of rows in the room, the number of seats in each row and the number of people in the line, correspondingly. Each of the next *k* lines contains two integers *x**i*, *y**i* (1<=β€<=*x**i*<=β€<=*n*, 1<=β€<=*y**i*<=β€<=*m*) β the coordinates of the seat each person has chosen. Numbers on the same line are separated by a space. The pairs of coordinates are located in the order, in which people stand in the line, starting from the head (the first person in the line who stands in front of the box office) to the tail (the last person in the line). | Print *k* lines, each containing a pair of integers. Print on the *i*-th line *x**i*,<=*y**i* β the coordinates of the seat, for which the person who stands *i*-th in the line will buy the ticket. | [
"3 4 6\n1 1\n1 1\n1 1\n1 2\n1 3\n1 3\n",
"4 3 12\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n"
] | [
"1 1\n1 2\n2 1\n1 3\n1 4\n2 3\n",
"2 2\n1 2\n2 1\n2 3\n3 2\n1 1\n1 3\n3 1\n3 3\n4 2\n4 1\n4 3\n"
] | none | [] | 1,500 | 38,604,800 | 0 | 37,874 | |
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\... | 623 | 0 | 3 | 37,892 | |
369 | Valera and Fools | [
"dfs and similar",
"dp",
"graphs",
"shortest paths"
] | null | null | One fine morning, *n* fools lined up in a row. After that, they numbered each other with numbers from 1 to *n*, inclusive. Each fool got a unique number. The fools decided not to change their numbers before the end of the fun.
Every fool has exactly *k* bullets and a pistol. In addition, the fool number *i* has probability of *p**i* (in percent) that he kills the fool he shoots at.
The fools decided to have several rounds of the fun. Each round of the fun looks like this: each currently living fool shoots at another living fool with the smallest number (a fool is not stupid enough to shoot at himself). All shots of the round are perfomed at one time (simultaneously). If there is exactly one living fool, he does not shoot.
Let's define a situation as the set of numbers of all the living fools at the some time. We say that a situation is possible if for some integer number *j* (0<=β€<=*j*<=β€<=*k*) there is a nonzero probability that after *j* rounds of the fun this situation will occur.
Valera knows numbers *p*1,<=*p*2,<=...,<=*p**n* and *k*. Help Valera determine the number of distinct possible situations. | The first line contains two integers *n*,<=*k* (1<=β€<=*n*,<=*k*<=β€<=3000) β the initial number of fools and the number of bullets for each fool.
The second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=β€<=*p**i*<=β€<=100) β the given probabilities (in percent). | Print a single number β the answer to the problem. | [
"3 3\n50 50 50\n",
"1 1\n100\n",
"2 1\n100 100\n",
"3 3\n0 0 0\n"
] | [
"7\n",
"1\n",
"2\n",
"1\n"
] | In the first sample, any situation is possible, except for situation {1,β2}.
In the second sample there is exactly one fool, so he does not make shots.
In the third sample the possible situations are {1,β2} (after zero rounds) and the "empty" situation {} (after one round).
In the fourth sample, the only possible situation is {1,β2,β3}. | [
{
"input": "3 3\n50 50 50",
"output": "7"
},
{
"input": "1 1\n100",
"output": "1"
},
{
"input": "2 1\n100 100",
"output": "2"
},
{
"input": "3 3\n0 0 0",
"output": "1"
},
{
"input": "5 2\n0 63 92 89 28",
"output": "5"
},
{
"input": "103 42\n78 30 16 12... | 30 | 0 | 0 | 37,952 | |
868 | Policeman and a Tree | [
"dp",
"graphs",
"trees"
] | null | null | You are given a tree (a connected non-oriented graph without cycles) with vertices numbered from 1 to *n*, and the length of the *i*-th edge is *w**i*. In the vertex *s* there is a policeman, in the vertices *x*1,<=*x*2,<=...,<=*x**m* (*x**j*<=β <=*s*) *m* criminals are located.
The policeman can walk along the edges with speed 1, the criminals can move with arbitrary large speed. If a criminal at some moment is at the same point as the policeman, he instantly gets caught by the policeman. Determine the time needed for the policeman to catch all criminals, assuming everybody behaves optimally (i.e. the criminals maximize that time, the policeman minimizes that time). Everybody knows positions of everybody else at any moment of time. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=50)Β β the number of vertices in the tree. The next *n*<=-<=1 lines contain three integers each: *u**i*, *v**i*, *w**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, 1<=β€<=*w**i*<=β€<=50) denoting edges and their lengths. It is guaranteed that the given graph is a tree.
The next line contains single integer *s* (1<=β€<=*s*<=β€<=*n*)Β β the number of vertex where the policeman starts.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=50)Β β the number of criminals. The next line contains *m* integers *x*1,<=*x*2,<=...,<=*x**m* (1<=β€<=*x**j*<=β€<=*n*, *x**j*<=β <=*s*)Β β the number of vertices where the criminals are located. *x**j* are not necessarily distinct. | If the policeman can't catch criminals, print single line "Terrorists win" (without quotes).
Otherwise, print single integerΒ β the time needed to catch all criminals. | [
"4\n1 2 2\n1 3 1\n1 4 1\n2\n4\n3 1 4 1\n",
"6\n1 2 3\n2 3 5\n3 4 1\n3 5 4\n2 6 3\n2\n3\n1 3 5\n"
] | [
"8\n",
"21\n"
] | In the first example one of the optimal scenarios is the following. The criminal number 2 moves to vertex 3, the criminal 4Β β to vertex 4. The policeman goes to vertex 4 and catches two criminals. After that the criminal number 1 moves to the vertex 2. The policeman goes to vertex 3 and catches criminal 2, then goes to the vertex 2 and catches the remaining criminal. | [] | 31 | 268,390,400 | 0 | 37,976 | |
982 | The Meeting Place Cannot Be Changed | [
"dfs and similar",
"graphs"
] | null | null | Petr is a detective in Braginsk. Somebody stole a huge amount of money from a bank and Petr is to catch him. Somebody told Petr that some luxurious car moves along the roads without stopping.
Petr knows that it is the robbers who drive the car. The roads in Braginsk are one-directional and each of them connects two intersections. Petr wants to select one intersection such that if the robbers continue to drive the roads indefinitely, they will sooner or later come to that intersection. The initial position of the robbers is unknown. Find such an intersection that fits the requirements. | The first line of the input contains two integers $n$ and $m$ ($2 \leq n \le 10^5$, $2 \leq m \leq 5 \cdot 10^5$)Β β the number of intersections and the number of directed roads in Braginsk, respectively.
Each of the next $m$ lines contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$, $u_i \ne v_i$)Β β the start and finish of the $i$-th directed road. It is guaranteed that the robbers can move along the roads indefinitely. | Print a single integer $k$Β β the intersection Petr needs to choose. If there are multiple answers, print any. If there are no such intersections, print $-1$. | [
"5 6\n1 2\n2 3\n3 1\n3 4\n4 5\n5 3\n",
"3 3\n1 2\n2 3\n3 1\n"
] | [
"3",
"1"
] | In the first example the robbers can move, for example, along the following routes: $(1-2-3-1)$, $(3-4-5-3)$, $(1-2-3-4-5-3-1)$. We can show that if Petr chooses the $3$-rd intersection, he will eventually meet the robbers independently of their route. | [] | 15 | 0 | 0 | 37,983 | |
43 | Race | [
"brute force",
"implementation",
"two pointers"
] | E. Race | 2 | 256 | Today *s* kilometer long auto race takes place in Berland. The track is represented by a straight line as long as *s* kilometers. There are *n* cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior β the system of segments on each of which the speed of the car is constant. The *j*-th segment of the *i*-th car is pair (*v**i*,<=*j*,<=*t**i*,<=*j*), where *v**i*,<=*j* is the car's speed on the whole segment in kilometers per hour and *t**i*,<=*j* is for how many hours the car had been driving at that speed. The segments are given in the order in which they are "being driven on" by the cars.
Your task is to find out how many times during the race some car managed to have a lead over another car. A lead is considered a situation when one car appears in front of another car. It is known, that all the leads happen instantly, i. e. there are no such time segment of positive length, during which some two cars drive "together". At one moment of time on one and the same point several leads may appear. In this case all of them should be taken individually. Meetings of cars at the start and finish are not considered to be counted as leads. | The first line contains two integers *n* and *s* (2<=β€<=*n*<=β€<=100,<=1<=β€<=*s*<=β€<=106) β the number of cars and the length of the track in kilometers. Then follow *n* lines β the description of the system of segments for each car. Every description starts with integer *k* (1<=β€<=*k*<=β€<=100) β the number of segments in the system. Then *k* space-separated pairs of integers are written. Each pair is the speed and time of the segment. These integers are positive and don't exceed 1000. It is guaranteed, that the sum of lengths of all segments (in kilometers) for each car equals to *s*; and all the leads happen instantly. | Print the single number β the number of times some car managed to take the lead over another car during the race. | [
"2 33\n2 5 1 2 14\n1 3 11\n",
"2 33\n2 1 3 10 3\n1 11 3\n",
"5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5\n"
] | [
"1\n",
"0\n",
"2\n"
] | none | [
{
"input": "2 33\n2 5 1 2 14\n1 3 11",
"output": "1"
},
{
"input": "2 33\n2 1 3 10 3\n1 11 3",
"output": "0"
},
{
"input": "5 33\n2 1 3 3 10\n1 11 3\n2 5 3 3 6\n2 3 1 10 3\n2 6 3 3 5",
"output": "2"
},
{
"input": "2 166755\n2 733 187 362 82\n3 813 147 565 57 557 27",
"out... | 186 | 70,451,200 | 0 | 38,027 |
808 | Selling Souvenirs | [
"binary search",
"dp",
"greedy",
"ternary search"
] | null | null | After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market.
This morning, as usual, Petya will come to the market. Petya has *n* different souvenirs to sell; *i*th souvenir is characterised by its weight *w**i* and cost *c**i*. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater than *m*, and total cost is maximum possible.
Help Petya to determine maximum possible total cost. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100000, 1<=β€<=*m*<=β€<=300000) β the number of Petya's souvenirs and total weight that he can carry to the market.
Then *n* lines follow. *i*th line contains two integers *w**i* and *c**i* (1<=β€<=*w**i*<=β€<=3, 1<=β€<=*c**i*<=β€<=109) β the weight and the cost of *i*th souvenir. | Print one number β maximum possible total cost of souvenirs that Petya can carry to the market. | [
"1 1\n2 1\n",
"2 2\n1 3\n2 2\n",
"4 3\n3 10\n2 7\n2 8\n1 1\n"
] | [
"0\n",
"3\n",
"10\n"
] | none | [
{
"input": "1 1\n2 1",
"output": "0"
},
{
"input": "2 2\n1 3\n2 2",
"output": "3"
},
{
"input": "4 3\n3 10\n2 7\n2 8\n1 1",
"output": "10"
},
{
"input": "5 5\n3 5\n2 6\n3 2\n1 1\n1 6",
"output": "13"
},
{
"input": "6 6\n1 6\n1 4\n1 8\n3 2\n3 2\n2 8",
"output":... | 62 | 204,800 | -1 | 38,089 | |
988 | Rain and Umbrellas | [
"dp"
] | null | null | Polycarp lives on a coordinate line at the point $x = 0$. He goes to his friend that lives at the point $x = a$. Polycarp can move only from left to right, he can pass one unit of length each second.
Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $n$ non-intersecting segments, the $i$-th segment which is in the rain is represented as $[l_i, r_i]$ ($0 \le l_i < r_i \le a$).
There are $m$ umbrellas lying on the line, the $i$-th umbrella is located at point $x_i$ ($0 \le x_i \le a$) and has weight $p_i$. When Polycarp begins his journey, he doesn't have any umbrellas.
During his journey from $x = 0$ to $x = a$ Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from $x$ to $x + 1$ if a segment $[x, x + 1]$ is in the rain (i.e. if there exists some $i$ such that $l_i \le x$ and $x + 1 \le r_i$).
The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain.
Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving.
Can Polycarp make his way from point $x = 0$ to point $x = a$? If yes, find the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally. | The first line contains three integers $a$, $n$ and $m$ ($1 \le a, m \le 2000, 1 \le n \le \lceil\frac{a}{2}\rceil$) β the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas.
Each of the next $n$ lines contains two integers $l_i$ and $r_i$ ($0 \le l_i < r_i \le a$) β the borders of the $i$-th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments $i$ and $j$ either $r_i < l_j$ or $r_j < l_i$.
Each of the next $m$ lines contains two integers $x_i$ and $p_i$ ($0 \le x_i \le a$, $1 \le p_i \le 10^5$) β the location and the weight of the $i$-th umbrella. | Print "-1" (without quotes) if Polycarp can't make his way from point $x = 0$ to point $x = a$. Otherwise print one integer β the minimum total fatigue after reaching $x = a$, if Polycarp picks up and throws away umbrellas optimally. | [
"10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2\n",
"10 1 1\n0 9\n0 5\n",
"10 1 1\n0 9\n1 5\n"
] | [
"14\n",
"45\n",
"-1\n"
] | In the first example the only possible strategy is to take the fourth umbrella at the point $x = 1$, keep it till the point $x = 7$ (the total fatigue at $x = 7$ will be equal to $12$), throw it away, move on from $x = 7$ to $x = 8$ without an umbrella, take the third umbrella at $x = 8$ and keep it till the end (the total fatigue at $x = 10$ will be equal to $14$).
In the second example the only possible strategy is to take the first umbrella, move with it till the point $x = 9$, throw it away and proceed without an umbrella till the end. | [
{
"input": "10 2 4\n3 7\n8 10\n0 10\n3 4\n8 1\n1 2",
"output": "14"
},
{
"input": "10 1 1\n0 9\n0 5",
"output": "45"
},
{
"input": "10 1 1\n0 9\n1 5",
"output": "-1"
},
{
"input": "1 1 1\n0 1\n1 100000",
"output": "-1"
},
{
"input": "1 1 1\n0 1\n0 100000",
"ou... | 155 | 4,710,400 | 3 | 38,121 | |
416 | Population Size | [
"greedy",
"implementation",
"math"
] | null | null | Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions.
Polycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence *a*1,<=*a*2,<=...,<=*a**n*, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8,<=6,<=4,<=2,<=1,<=4,<=7,<=10,<=2) can be considered as a sequence of three arithmetic progressions (8,<=6,<=4,<=2), (1,<=4,<=7,<=10) and (2), which are written one after another.
Unfortunately, Polycarpus may not have all the data for the *n* consecutive years (a census of the population doesn't occur every year, after all). For this reason, some values of *a**i* ββmay be unknown. Such values are represented by number -1.
For a given sequence *a*<==<=(*a*1,<=*a*2,<=...,<=*a**n*), which consists of positive integers and values ββ-1, find the minimum number of arithmetic progressions Polycarpus needs to get *a*. To get *a*, the progressions need to be written down one after the other. Values ββ-1 may correspond to an arbitrary positive integer and the values *a**i*<=><=0 must be equal to the corresponding elements of sought consecutive record of the progressions.
Let us remind you that a finite sequence *c* is called an arithmetic progression if the difference *c**i*<=+<=1<=-<=*c**i* of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of elements in the sequence. The second line contains integer values *a*1,<=*a*2,<=...,<=*a**n* separated by a space (1<=β€<=*a**i*<=β€<=109 or *a**i*<==<=<=-<=1). | Print the minimum number of arithmetic progressions that you need to write one after another to get sequence *a*. The positions marked as -1 in *a* can be represented by any positive integers. | [
"9\n8 6 4 2 1 4 7 10 2\n",
"9\n-1 6 -1 2 -1 4 7 -1 2\n",
"5\n-1 -1 -1 -1 -1\n",
"7\n-1 -1 4 5 1 2 3\n"
] | [
"3\n",
"3\n",
"1\n",
"2\n"
] | none | [
{
"input": "9\n8 6 4 2 1 4 7 10 2",
"output": "3"
},
{
"input": "9\n-1 6 -1 2 -1 4 7 -1 2",
"output": "3"
},
{
"input": "5\n-1 -1 -1 -1 -1",
"output": "1"
},
{
"input": "7\n-1 -1 4 5 1 2 3",
"output": "2"
},
{
"input": "1\n1",
"output": "1"
},
{
"input... | 31 | 512,000 | 0 | 38,276 | |
0 | none | [
"none"
] | null | null | Limak is a little grizzly bear. He will once attack Deerland but now he can only destroy trees in role-playing games. Limak starts with a tree with one vertex. The only vertex has index 1 and is a root of the tree.
Sometimes, a game chooses a subtree and allows Limak to attack it. When a subtree is attacked then each of its edges is destroyed with probability , independently of other edges. Then, Limak gets the penaltyΒ β an integer equal to the height of the subtree after the attack. The height is defined as the maximum number of edges on the path between the root of the subtree and any vertex in the subtree.
You must handle queries of two types.
- 1 v denotes a query of the first type. A new vertex appears and its parent is *v*. A new vertex has the next available index (so, new vertices will be numbered 2,<=3,<=...). - 2 v denotes a query of the second type. For a moment let's assume that the game allows Limak to attack a subtree rooted in *v*. Then, what would be the expected value of the penalty Limak gets after the attack?
In a query of the second type, Limak doesn't actually attack the subtree and thus the query doesn't affect next queries. | The first line of the input contains one integer *q* (1<=β€<=*q*<=β€<=500<=000)Β β the number of queries.
Then, *q* lines follow. The *i*-th of them contains two integers *type**i* and *v**i* (1<=β€<=*type**i*<=β€<=2). If *type**i*<==<=1 then *v**i* denotes a parent of a new vertex, while if *type**i*<==<=2 then you should print the answer for a subtree rooted in *v**i*.
It's guaranteed that there will be at least 1 query of the second type, that is, the output won't be empty.
It's guaranteed that just before the *i*-th query a vertex *v**i* already exists. | For each query of the second type print one real numberΒ βthe expected value of the penalty if Limak attacks the given subtree. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if . | [
"7\n1 1\n1 1\n2 1\n1 2\n1 3\n2 2\n2 1\n",
"8\n2 1\n1 1\n1 2\n1 3\n1 4\n2 1\n1 4\n2 1\n"
] | [
"0.7500000000\n0.5000000000\n1.1875000000\n",
"0.0000000000\n0.9375000000\n0.9687500000\n"
] | Below, you can see the drawing for the first sample. Red circles denote queries of the second type. | [] | 46 | 0 | 0 | 38,349 | |
623 | Transforming Sequence | [
"combinatorics",
"dp",
"fft",
"math"
] | null | null | Let's define the transformation *P* of a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* as *b*1,<=*b*2,<=...,<=*b**n*, where *b**i*<==<=*a*1Β |Β *a*2Β |Β ...Β |Β *a**i* for all *i*<==<=1,<=2,<=...,<=*n*, where | is the bitwise OR operation.
Vasya consequently applies the transformation *P* to all sequences of length *n* consisting of integers from 1 to 2*k*<=-<=1 inclusive. He wants to know how many of these sequences have such property that their transformation is a strictly increasing sequence. Help him to calculate this number modulo 109<=+<=7. | The only line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=1018,<=1<=β€<=*k*<=β€<=30<=000). | Print a single integerΒ β the answer to the problem modulo 109<=+<=7. | [
"1 2\n",
"2 3\n",
"3 3\n"
] | [
"3\n",
"30\n",
"48\n"
] | none | [] | 46 | 0 | 0 | 38,373 | |
379 | New Year Present | [
"constructive algorithms",
"implementation"
] | null | null | The New Year is coming! That's why many people today are busy preparing New Year presents. Vasily the Programmer is no exception.
Vasily knows that the best present is (no, it's not a contest) money. He's put *n* empty wallets from left to right in a row and decided how much money to put in what wallet. Vasily decided to put *a**i* coins to the *i*-th wallet from the left.
Vasily is a very busy man, so the money are sorted into the bags by his robot. Initially, the robot stands by the leftmost wallet in the row. The robot can follow instructions of three types: go to the wallet that is to the left of the current one (if such wallet exists), go to the wallet that is to the right of the current one (if such wallet exists), put a coin to the current wallet. Due to some technical malfunctions the robot cannot follow two "put a coin" instructions in a row.
Vasily doesn't want to wait for long, so he wants to write a program for the robot that contains at most 106 operations (not necessarily minimum in length) the robot can use to put coins into the wallets. Help him. | The first line contains integer *n* (2<=β€<=*n*<=β€<=300) β the number of wallets. The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=300).
It is guaranteed that at least one *a**i* is positive. | Print the sequence that consists of *k* (1<=β€<=*k*<=β€<=106) characters, each of them equals: "L", "R" or "P". Each character of the sequence is an instruction to the robot. Character "L" orders to move to the left, character "R" orders to move to the right, character "P" orders the robot to put a coin in the wallet. The robot is not allowed to go beyond the wallet line. In other words, you cannot give instructions "L" if the robot is at wallet 1, or "R" at wallet *n*.
As a result of the performed operations, the *i*-th wallet from the left must contain exactly *a**i* coins. If there are multiple answers, you can print any of them. | [
"2\n1 2\n",
"4\n0 2 0 2\n"
] | [
"PRPLRP",
"RPRRPLLPLRRRP"
] | none | [
{
"input": "2\n1 2",
"output": "PRPLRP"
},
{
"input": "4\n0 2 0 2",
"output": "RPRRPLLPLRRRP"
},
{
"input": "10\n2 3 4 0 0 1 1 3 4 2",
"output": "PRPRPRRRPRPRPRPRPLPLPLLLLLPLPLPRPRPRRRRRPRPRPLPLLLLLLPLL"
},
{
"input": "10\n0 0 0 0 0 0 0 0 1 0",
"output": "RRRRRRRRPR"
},... | 171 | 2,048,000 | -1 | 38,396 | |
590 | Top Secret Task | [
"dp"
] | null | null | A top-secret military base under the command of Colonel Zuev is expecting an inspection from the Ministry of Defence. According to the charter, each top-secret military base must include a top-secret troop that should... well, we cannot tell you exactly what it should do, it is a top secret troop at the end. The problem is that Zuev's base is missing this top-secret troop for some reasons.
The colonel decided to deal with the problem immediately and ordered to line up in a single line all *n* soldiers of the base entrusted to him. Zuev knows that the loquacity of the *i*-th soldier from the left is equal to *q**i*. Zuev wants to form the top-secret troop using *k* leftmost soldiers in the line, thus he wants their total loquacity to be as small as possible (as the troop should remain top-secret). To achieve this, he is going to choose a pair of consecutive soldiers and swap them. He intends to do so no more than *s* times. Note that any soldier can be a participant of such swaps for any number of times. The problem turned out to be unusual, and colonel Zuev asked you to help.
Determine, what is the minimum total loquacity of the first *k* soldiers in the line, that can be achieved by performing no more than *s* swaps of two consecutive soldiers. | The first line of the input contains three positive integers *n*, *k*, *s* (1<=β€<=*k*<=β€<=*n*<=β€<=150, 1<=β€<=*s*<=β€<=109)Β β the number of soldiers in the line, the size of the top-secret troop to be formed and the maximum possible number of swap operations of the consecutive pair of soldiers, respectively.
The second line of the input contains *n* integer *q**i* (1<=β€<=*q**i*<=β€<=1<=000<=000)Β β the values of loquacity of soldiers in order they follow in line from left to right. | Print a single integer β the minimum possible total loquacity of the top-secret troop. | [
"3 2 2\n2 4 1\n",
"5 4 2\n10 1 6 2 5\n",
"5 2 3\n3 1 4 2 5\n"
] | [
"3\n",
"18\n",
"3\n"
] | In the first sample Colonel has to swap second and third soldiers, he doesn't really need the remaining swap. The resulting soldiers order is: (2, 1, 4). Minimum possible summary loquacity of the secret troop is 3. In the second sample Colonel will perform swaps in the following order:
1. (10, 1, 6 β 2, 5) 1. (10, 1, 2, 6 β 5)
The resulting soldiers order is (10, 1, 2, 5, 6).
Minimum possible summary loquacity is equal to 18. | [] | 30 | 0 | 0 | 38,454 | |
724 | Goods transportation | [
"dp",
"flows",
"greedy"
] | null | null | There are *n* cities located along the one-way road. Cities are numbered from 1 to *n* in the direction of the road.
The *i*-th city had produced *p**i* units of goods. No more than *s**i* units of goods can be sold in the *i*-th city.
For each pair of cities *i* and *j* such that 1<=β€<=*i*<=<<=*j*<=β€<=*n* you can no more than once transport no more than *c* units of goods from the city *i* to the city *j*. Note that goods can only be transported from a city with a lesser index to the city with a larger index. You can transport goods between cities in any order.
Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations. | The first line of the input contains two integers *n* andΒ *c* (1<=β€<=*n*<=β€<=10<=000, 0<=β€<=*c*<=β€<=109)Β β the number of cities and the maximum amount of goods for a single transportation.
The second line contains *n* integers *p**i* (0<=β€<=*p**i*<=β€<=109)Β β the number of units of goods that were produced in each city.
The third line of input contains *n* integers *s**i* (0<=β€<=*s**i*<=β€<=109)Β β the number of units of goods that can be sold in each city. | Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations. | [
"3 0\n1 2 3\n3 2 1\n",
"5 1\n7 4 2 1 0\n1 2 3 4 5\n",
"4 3\n13 10 7 4\n4 7 10 13\n"
] | [
"4\n",
"12\n",
"34\n"
] | none | [
{
"input": "3 0\n1 2 3\n3 2 1",
"output": "4"
},
{
"input": "5 1\n7 4 2 1 0\n1 2 3 4 5",
"output": "12"
},
{
"input": "4 3\n13 10 7 4\n4 7 10 13",
"output": "34"
},
{
"input": "10 1\n0 2 1 1 0 2 5 2 5 5\n4 0 1 4 2 4 4 5 2 3",
"output": "18"
},
{
"input": "10 3\n10... | 46 | 4,915,200 | 0 | 38,488 | |
367 | Sereja ans Anagrams | [
"binary search",
"data structures"
] | null | null | Sereja has two sequences *a* and *b* and number *p*. Sequence *a* consists of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Similarly, sequence *b* consists of *m* integers *b*1,<=*b*2,<=...,<=*b**m*. As usual, Sereja studies the sequences he has. Today he wants to find the number of positions *q* (*q*<=+<=(*m*<=-<=1)Β·*p*<=β€<=*n*;Β *q*<=β₯<=1), such that sequence *b* can be obtained from sequence *a**q*,<=*a**q*<=+<=*p*,<=*a**q*<=+<=2*p*,<=...,<=*a**q*<=+<=(*m*<=-<=1)*p* by rearranging elements.
Sereja needs to rush to the gym, so he asked to find all the described positions of *q*. | The first line contains three integers *n*, *m* and *p* (1<=β€<=*n*,<=*m*<=β€<=2Β·105,<=1<=β€<=*p*<=β€<=2Β·105). The next line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109). The next line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=β€<=*b**i*<=β€<=109). | In the first line print the number of valid *q*s. In the second line, print the valid values in the increasing order. | [
"5 3 1\n1 2 3 2 1\n1 2 3\n",
"6 3 2\n1 3 2 2 3 1\n1 2 3\n"
] | [
"2\n1 3\n",
"2\n1 2\n"
] | none | [
{
"input": "5 3 1\n1 2 3 2 1\n1 2 3",
"output": "2\n1 3"
},
{
"input": "6 3 2\n1 3 2 2 3 1\n1 2 3",
"output": "2\n1 2"
},
{
"input": "68 16 3\n5 3 4 3 3 3 2 2 2 3 2 4 2 2 2 2 4 3 5 1 1 2 2 2 3 1 5 1 2 2 1 5 1 5 3 2 3 5 2 1 1 4 2 3 4 3 4 3 3 1 3 4 1 5 2 5 3 4 4 1 4 5 5 1 1 2 2 2\n5 4 4 3 ... | 452 | 27,750,400 | 3 | 38,601 | |
333 | Characteristics of Rectangles | [
"binary search",
"bitmasks",
"brute force",
"implementation",
"sortings"
] | null | null | Gerald found a table consisting of *n* rows and *m* columns. As a prominent expert on rectangular tables, he immediately counted the table's properties, that is, the minimum of the numbers in the corners of the table (minimum of four numbers). However, he did not like the final value β it seemed to be too small. And to make this value larger, he decided to crop the table a little: delete some columns on the left and some on the right, as well as some rows from the top and some from the bottom. Find what the maximum property of the table can be after such cropping. Note that the table should have at least two rows and at least two columns left in the end. The number of cropped rows or columns from each of the four sides can be zero. | The first line contains two space-separated integers *n* and *m* (2<=β€<=*n*,<=*m*<=β€<=1000). The following *n* lines describe the table. The *i*-th of these lines lists the space-separated integers *a**i*,<=1,<=*a**i*,<=2,<=...,<=*a**i*,<=*m* (0<=β€<=*a**i*,<=*j*<=β€<=109) β the *m* numbers standing in the *i*-th row of the table. | Print the answer to the problem. | [
"2 2\n1 2\n3 4\n",
"3 3\n1 0 0\n0 1 1\n1 0 0\n"
] | [
"1\n",
"0\n"
] | In the first test case Gerald cannot crop the table β table contains only two rows and only two columns.
In the second test case if we'll crop the table, the table will contain zero in some corner cell. Also initially it contains two zeros in the corner cells, so the answer is 0. | [
{
"input": "2 2\n1 2\n3 4",
"output": "1"
},
{
"input": "3 3\n1 0 0\n0 1 1\n1 0 0",
"output": "0"
},
{
"input": "2 2\n0 0\n0 0",
"output": "0"
},
{
"input": "2 2\n1000000000 1000000000\n1000000000 1000000000",
"output": "1000000000"
},
{
"input": "10 2\n1 20\n19 2... | 62 | 0 | 0 | 38,655 | |
771 | Bear and Tree Jumps | [
"dfs and similar",
"dp",
"trees"
] | null | null | A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them.
Limak is a little polar bear. He lives in a tree that consists of *n* vertices, numbered 1 through *n*.
Limak recently learned how to jump. He can jump from a vertex to any vertex within distance at most *k*.
For a pair of vertices (*s*,<=*t*) we define *f*(*s*,<=*t*) as the minimum number of jumps Limak needs to get from *s* to *t*. Your task is to find the sum of *f*(*s*,<=*t*) over all pairs of vertices (*s*,<=*t*) such that *s*<=<<=*t*. | The first line of the input contains two integers *n* and *k* (2<=β€<=*n*<=β€<=200<=000, 1<=β€<=*k*<=β€<=5)Β β the number of vertices in the tree and the maximum allowed jump distance respectively.
The next *n*<=-<=1 lines describe edges in the tree. The *i*-th of those lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*)Β β the indices on vertices connected with *i*-th edge.
It's guaranteed that the given edges form a tree. | Print one integer, denoting the sum of *f*(*s*,<=*t*) over all pairs of vertices (*s*,<=*t*) such that *s*<=<<=*t*. | [
"6 2\n1 2\n1 3\n2 4\n2 5\n4 6\n",
"13 3\n1 2\n3 2\n4 2\n5 2\n3 6\n10 6\n6 7\n6 13\n5 8\n5 9\n9 11\n11 12\n",
"3 5\n2 1\n3 1\n"
] | [
"20\n",
"114\n",
"3\n"
] | In the first sample, the given tree has 6 vertices and it's displayed on the drawing below. Limak can jump to any vertex within distance at most 2. For example, from the vertex 5 he can jump to any of vertices: 1, 2 and 4 (well, he can also jump to the vertex 5 itself).
There are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c0295201207e28a36e641d8cf599f45986059e71.png" style="max-width: 100.0%;max-height: 100.0%;"/> pairs of vertices (*s*,β*t*) such that *s*β<β*t*. For 5 of those pairs Limak would need two jumps: (1,β6),β(3,β4),β(3,β5),β(3,β6),β(5,β6). For other 10 pairs one jump is enough. So, the answer is 5Β·2β+β10Β·1β=β20.
In the third sample, Limak can jump between every two vertices directly. There are 3 pairs of vertices (*s*β<β*t*), so the answer is 3Β·1β=β3. | [
{
"input": "6 2\n1 2\n1 3\n2 4\n2 5\n4 6",
"output": "20"
},
{
"input": "13 3\n1 2\n3 2\n4 2\n5 2\n3 6\n10 6\n6 7\n6 13\n5 8\n5 9\n9 11\n11 12",
"output": "114"
},
{
"input": "3 5\n2 1\n3 1",
"output": "3"
},
{
"input": "2 1\n1 2",
"output": "1"
},
{
"input": "2 5... | 15 | 0 | 0 | 38,664 | |
553 | Love Triangles | [
"dfs and similar",
"dsu",
"graphs"
] | null | null | There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has *n* characters. The characters are labeled from 1 to *n*. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state).
You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A).
You are given a list of *m* known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo 1<=000<=000<=007. | The first line of input will contain two integers *n*,<=*m* (3<=β€<=*n*<=β€<=100<=000, 0<=β€<=*m*<=β€<=100<=000).
The next *m* lines will contain the description of the known relationships. The *i*-th line will contain three integers *a**i*,<=*b**i*,<=*c**i*. If *c**i* is 1, then *a**i* and *b**i* are in love, otherwise, they hate each other (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*, ).
Each pair of people will be described no more than once. | Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo 1<=000<=000<=007. | [
"3 0\n",
"4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0\n",
"4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1\n"
] | [
"4\n",
"1\n",
"0\n"
] | In the first sample, the four ways are to:
- Make everyone love each other - Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this).
In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other. | [
{
"input": "3 0",
"output": "4"
},
{
"input": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 0",
"output": "1"
},
{
"input": "4 4\n1 2 1\n2 3 1\n3 4 0\n4 1 1",
"output": "0"
},
{
"input": "100000 0",
"output": "303861760"
},
{
"input": "100 3\n1 2 0\n2 3 0\n3 1 0",
"output": ... | 264 | 25,497,600 | 3 | 38,708 | |
453 | Little Pony and Elements of Harmony | [
"dp",
"matrices"
] | null | null | The Elements of Harmony are six supernatural artifacts representing subjective aspects of harmony. They are arguably the most powerful force in Equestria. The inside of Elements of Harmony can be seen as a complete graph with *n* vertices labeled from 0 to *n*<=-<=1, where *n* is a power of two, equal to 2*m*.
The energy in Elements of Harmony is in constant movement. According to the ancient book, the energy of vertex *u* in time *i* (*e**i*[*u*]) equals to:
Here *b*[] is the transformation coefficient β an array of *m*<=+<=1 integers and *f*(*u*,<=*v*) is the number of ones in the binary representation of number (*u*Β *xor*Β *v*).
Given the transformation coefficient and the energy distribution at time 0 (*e*0[]). Help Twilight Sparkle predict the energy distribution at time *t* (*e**t*[]). The answer can be quite large, so output it modulo *p*. | The first line contains three integers *m*, *t* and *p* (1<=β€<=*m*<=β€<=20;Β 0<=β€<=*t*<=β€<=1018;Β 2<=β€<=*p*<=β€<=109). The following line contains *n* (*n*<==<=2*m*) integers *e*0[*i*] (1<=β€<=*e*0[*i*]<=β€<=109;Β 0<=β€<=*i*<=<<=*n*). The next line contains *m*<=+<=1 integers *b*[*i*] (0<=β€<=*b*[*i*]<=β€<=109;Β 0<=β€<=*i*<=β€<=*m*). | Output *n* lines, the *i*-th line must contain a single integer *e**t*[*i*] modulo *p*. | [
"2 2 10000\n4 1 2 3\n0 1 0\n"
] | [
"14\n6\n6\n14\n"
] | none | [] | 6,000 | 5,120,000 | 0 | 38,830 | |
802 | Marmots (easy) | [
"math"
] | null | null | Heidi is a statistician to the core, and she likes to study the evolution of marmot populations in each of *V* (1<=β€<=*V*<=β€<=100) villages! So it comes that every spring, when Heidi sees the first snowdrops sprout in the meadows around her barn, she impatiently dons her snowshoes and sets out to the Alps, to welcome her friends the marmots to a new season of thrilling adventures.
Arriving in a village, Heidi asks each and every marmot she comes across for the number of inhabitants of that village. This year, the marmots decide to play an April Fools' joke on Heidi. Instead of consistently providing the exact number of inhabitants *P* (10<=β€<=*P*<=β€<=1000) of the village, they respond with a random non-negative integer *k*, drawn from one of two types of probability distributions:
- Poisson (d'avril) distribution: the probability of getting an answer *k* is for *k*<==<=0,<=1,<=2,<=3,<=..., - Uniform distribution: the probability of getting an answer *k* is for *k*<==<=0,<=1,<=2,<=...,<=2*P*.
Heidi collects exactly 250 answers per village. Every village follows either the Poisson or the uniform distribution. Heidi cannot tell marmots apart, so she may query some marmots several times, and each time the marmot will answer with a new number drawn from the village's distribution.
Can you help Heidi to find out whether a village follows a Poisson or a uniform distribution? | The first line of input will contain the number of villages *V* (1<=β€<=*V*<=β€<=100). The following *V* lines each describe one village. The description of each village consists of 250 space-separated integers *k*, drawn from one of the above distributions. | Output one line per village, in the same order as provided in the input. The village's line shall state poisson if the village's distribution is of the Poisson type, and uniform if the answer came from a uniform distribution. | [
"2\n92 100 99 109 93 105 103 106 101 99 ... (input is truncated)\n28 180 147 53 84 80 180 85 8 16 ... (input is truncated)"
] | [
"poisson\nuniform\n"
] | The full example input is visually represented below, along with the probability distribution function it was drawn from (the *y*-axis is labeled by its values multiplied by 250).
<img class="tex-graphics" src="https://espresso.codeforces.com/77563a6378b39d03eb21012c835c6e96df776b81.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 30 | 0 | 0 | 38,874 | |
838 | Binary Blocks | [
"brute force"
] | null | null | You are given an image, that can be represented with a 2-d *n* by *m* grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer *k*<=><=1 and split the image into *k* by *k* blocks. If *n* and *m* are not divisible by *k*, the image is padded with only zeros on the right and bottom so that they are divisible by *k*. Each pixel in each individual block must have the same value. The given image may not be compressible in its current state. Find the minimum number of pixels you need to toggle (after padding) in order for the image to be compressible for some *k*. More specifically, the steps are to first choose *k*, then the image is padded with zeros, then, we can toggle the pixels so it is compressible for this *k*. The image must be compressible in that state. | The first line of input will contain two integers *n*,<=*m* (2<=β€<=*n*,<=*m*<=β€<=2<=500), the dimensions of the image.
The next *n* lines of input will contain a binary string with exactly *m* characters, representing the image. | Print a single integer, the minimum number of pixels needed to toggle to make the image compressible. | [
"3 5\n00100\n10110\n11001\n"
] | [
"5\n"
] | We first choose *k*β=β2.
The image is padded as follows:
We can toggle the image to look as follows:
We can see that this image is compressible for *k*β=β2. | [
{
"input": "3 5\n00100\n10110\n11001",
"output": "5"
}
] | 62 | 7,065,600 | -1 | 38,937 | |
144 | Anagram Search | [
"implementation",
"strings"
] | null | null | A string *t* is called an anagram of the string *s*, if it is possible to rearrange letters in *t* so that it is identical to the string *s*. For example, the string "aab" is an anagram of the string "aba" and the string "aaa" is not.
The string *t* is called a substring of the string *s* if it can be read starting from some position in the string *s*. For example, the string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
You are given a string *s*, consisting of lowercase Latin letters and characters "?". You are also given a string *p*, consisting of lowercase Latin letters only. Let's assume that a string is good if you can obtain an anagram of the string *p* from it, replacing the "?" characters by Latin letters. Each "?" can be replaced by exactly one character of the Latin alphabet. For example, if the string *p* = Β«abaΒ», then the string "a??" is good, and the string Β«?bcΒ» is not.
Your task is to find the number of good substrings of the string *s* (identical substrings must be counted in the answer several times). | The first line is non-empty string *s*, consisting of no more than 105 lowercase Latin letters and characters "?". The second line is non-empty string *p*, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string *p* can exceed the length of the string *s*. | Print the single number representing the number of good substrings of string *s*.
Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times. | [
"bb??x???\naab\n",
"ab?c\nacb\n"
] | [
"2\n",
"2\n"
] | Consider the first sample test. Here the string *s* has two good substrings: "b??" (after we replace the question marks we get "baa"), "???" (after we replace the question marks we get "baa").
Let's consider the second sample test. Here the string *s* has two good substrings: "ab?" ("?" can be replaced by "c"), "b?c" ("?" can be replaced by "a"). | [
{
"input": "bb??x???\naab",
"output": "2"
},
{
"input": "ab?c\nacb",
"output": "2"
},
{
"input": "ccaac\ncbcbca",
"output": "0"
},
{
"input": "?bba?\nbba",
"output": "3"
},
{
"input": "aaaaa??a?a\naaa",
"output": "8"
},
{
"input": "?bba?b?aaa\nabb",
... | 62 | 0 | 0 | 38,962 | |
677 | Vanya and Food Processor | [
"implementation",
"math"
] | null | null | Vanya smashes potato in a vertical food processor. At each moment of time the height of the potato in the processor doesn't exceed *h* and the processor smashes *k* centimeters of potato each second. If there are less than *k* centimeters remaining, than during this second processor smashes all the remaining potato.
Vanya has *n* pieces of potato, the height of the *i*-th piece is equal to *a**i*. He puts them in the food processor one by one starting from the piece number 1 and finishing with piece number *n*. Formally, each second the following happens:
1. If there is at least one piece of potato remaining, Vanya puts them in the processor one by one, until there is not enough space for the next piece. 1. Processor smashes *k* centimeters of potato (or just everything that is inside).
Provided the information about the parameter of the food processor and the size of each potato in a row, compute how long will it take for all the potato to become smashed. | The first line of the input contains integers *n*, *h* and *k* (1<=β€<=*n*<=β€<=100<=000,<=1<=β€<=*k*<=β€<=*h*<=β€<=109)Β β the number of pieces of potato, the height of the food processor and the amount of potato being smashed each second, respectively.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=*h*)Β β the heights of the pieces. | Print a single integerΒ β the number of seconds required to smash all the potatoes following the process described in the problem statement. | [
"5 6 3\n5 4 3 2 1\n",
"5 6 3\n5 5 5 5 5\n",
"5 6 3\n1 2 1 1 1\n"
] | [
"5\n",
"10\n",
"2\n"
] | Consider the first sample.
1. First Vanya puts the piece of potato of height 5 into processor. At the end of the second there is only amount of height 2 remaining inside. 1. Now Vanya puts the piece of potato of height 4. At the end of the second there is amount of height 3 remaining. 1. Vanya puts the piece of height 3 inside and again there are only 3 centimeters remaining at the end of this second. 1. Vanya finally puts the pieces of height 2 and 1 inside. At the end of the second the height of potato in the processor is equal to 3. 1. During this second processor finally smashes all the remaining potato and the process finishes.
In the second sample, Vanya puts the piece of height 5 inside and waits for 2 seconds while it is completely smashed. Then he repeats the same process for 4 other pieces. The total time is equal to 2Β·5β=β10 seconds.
In the third sample, Vanya simply puts all the potato inside the processor and waits 2 seconds. | [
{
"input": "5 6 3\n5 4 3 2 1",
"output": "5"
},
{
"input": "5 6 3\n5 5 5 5 5",
"output": "10"
},
{
"input": "5 6 3\n1 2 1 1 1",
"output": "2"
},
{
"input": "10 100 80\n76 75 73 71 76 74 73 70 78 75",
"output": "10"
},
{
"input": "10 100 88\n11 23 69 6 71 15 25 1 4... | 92 | 14,028,800 | 3 | 39,071 | |
288 | Polo the Penguin and Trees | [
"combinatorics",
"dfs and similar",
"trees"
] | null | null | Little penguin Polo has got a tree β a non-directed connected acyclic graph, containing *n* nodes and *n*<=-<=1 edges. We will consider the tree nodes numbered by integers from 1 to *n*.
Today Polo wonders, how to find the number of pairs of paths that don't have common nodes. More formally, he should find the number of groups of four integers *a*,<=*b*,<=*c* and *d* such that:
- 1<=β€<=*a*<=<<=*b*<=β€<=*n*; - 1<=β€<=*c*<=<<=*d*<=β€<=*n*; - there's no such node that lies on both the shortest path from node *a* to node *b* and from node *c* to node *d*.
The shortest path betweem two nodes is the path that is shortest in the number of edges.
Help Polo solve this problem. | The first line contains integer *n* (1<=β€<=*n*<=β€<=80000) β the number of tree nodes. Each of the following *n*<=-<=1 lines contains a pair of integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*;Β *u**i*<=β <=*v**i*) β the *i*-th edge of the tree.
It is guaranteed that the given graph is a tree. | In a single line print a single integer β the answer to the problem.
Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specificator. | [
"4\n1 2\n2 3\n3 4\n"
] | [
"2\n"
] | none | [] | 1,372 | 51,712,000 | -1 | 39,077 | |
36 | New Game with a Chess Piece | [
"games"
] | D. New Game with a Chess Piece | 2 | 64 | Petya and Vasya are inventing a new game that requires a rectangular board and one chess piece. At the beginning of the game the piece stands in the upper-left corner of the board. Two players move the piece in turns. Each turn the chess piece can be moved either one square to the right or one square down or jump *k* squares diagonally down and to the right. The player who canβt move the piece loses.
The guys havenβt yet thought what to call the game or the best size of the board for it. Your task is to write a program that can determine the outcome of the game depending on the board size. | The first input line contains two integers *t* and *k* (1<=β€<=*t*<=β€<=20, 1<=β€<=*k*<=β€<=109). Each of the following *t* lines contains two numbers *n*, *m* β the boardβs length and width (1<=β€<=*n*,<=*m*<=β€<=109). | Output *t* lines that can determine the outcomes of the game on every board. Write Β«+Β» if the first player is a winner, and Β«-Β» otherwise. | [
"10 2\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n3 1\n3 2\n3 3\n4 3\n"
] | [
"-\n+\n+\n-\n-\n+\n-\n+\n+\n+\n"
] | none | [
{
"input": "10 2\n1 1\n1 2\n2 1\n2 2\n1 3\n2 3\n3 1\n3 2\n3 3\n4 3",
"output": "-\n+\n+\n-\n-\n+\n-\n+\n+\n+"
},
{
"input": "20 2\n5 9\n6 7\n6 5\n9 5\n1 7\n7 5\n6 5\n2 10\n9 10\n5 5\n5 7\n3 3\n2 7\n6 1\n9 5\n1 1\n2 1\n5 8\n6 3\n2 9",
"output": "+\n+\n-\n+\n-\n+\n-\n-\n+\n+\n+\n+\n+\n+\n+\n-\n+\n... | 280 | 0 | 0 | 39,081 |
888 | Xor-MST | [
"bitmasks",
"constructive algorithms",
"data structures"
] | null | null | You are given a complete undirected graph with *n* vertices. A number *a**i* is assigned to each vertex, and the weight of an edge between vertices *i* and *j* is equal to *a**i*<=*xor*<=*a**j*.
Calculate the weight of the minimum spanning tree in this graph. | The first line contains *n* (1<=β€<=*n*<=β€<=200000) β the number of vertices in the graph.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (0<=β€<=*a**i*<=<<=230) β the numbers assigned to the vertices. | Print one number β the weight of the minimum spanning tree in the graph. | [
"5\n1 2 3 4 5\n",
"4\n1 2 3 4\n"
] | [
"8\n",
"8\n"
] | none | [
{
"input": "5\n1 2 3 4 5",
"output": "8"
},
{
"input": "4\n1 2 3 4",
"output": "8"
},
{
"input": "1\n1",
"output": "0"
}
] | 46 | 0 | 0 | 39,125 | |
725 | Messages on a Tree | [] | null | null | Alice and Bob are well-known for sending messages to each other. This time you have a rooted tree with Bob standing in the root node and copies of Alice standing in each of the other vertices. The root node has number 0, the rest are numbered 1 through *n*.
At some moments of time some copies of Alice want to send a message to Bob and receive an answer. We will call this copy the initiator. The process of sending a message contains several steps:
- The initiator sends the message to the person standing in the parent node and begins waiting for the answer. - When some copy of Alice receives a message from some of her children nodes, she sends the message to the person standing in the parent node and begins waiting for the answer. - When Bob receives a message from some of his child nodes, he immediately sends the answer to the child node where the message came from. - When some copy of Alice (except for initiator) receives an answer she is waiting for, she immediately sends it to the child vertex where the message came from. - When the initiator receives the answer she is waiting for, she doesn't send it to anybody. - There is a special case: a copy of Alice can't wait for two answers at the same time, so if some copy of Alice receives a message from her child node while she already waits for some answer, she rejects the message and sends a message saying this back to the child node where the message came from. Then the copy of Alice in the child vertex processes this answer as if it was from Bob. - The process of sending a message to a parent node or to a child node is instant but a receiver (a parent or a child) gets a message after 1 second.
If some copy of Alice receives several messages from child nodes at the same moment while she isn't waiting for an answer, she processes the message from the initiator with the smallest number and rejects all the rest. If some copy of Alice receives messages from children nodes and also receives the answer she is waiting for at the same instant, then Alice first processes the answer, then immediately continue as normal with the incoming messages.
You are given the moments of time when some copy of Alice becomes the initiator and sends a message to Bob. For each message, find the moment of time when the answer (either from Bob or some copy of Alice) will be received by the initiator.
You can assume that if Alice wants to send a message (i.e. become the initiator) while waiting for some answer, she immediately rejects the message and receives an answer from herself in no time. | The first line of input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=200<=000)Β β the number of nodes with Alices and the number of messages.
Second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=β€<=*p**i*<=<<=*i*). The integer *p**i* is the number of the parent node of node *i*.
The next *m* lines describe the messages. The *i*-th of them contains two integers *x**i* and *t**i* (1<=β€<=*x**i*<=β€<=*n*, 1<=β€<=*t**i*<=β€<=109)Β β the number of the vertex of the initiator of the *i*-th message and the time of the initiation (in seconds). The messages are given in order of increasing initiation time (i.e. *t**i*<=+<=1<=β₯<=*t**i* holds for 1<=β€<=*i*<=<<=*m*). The pairs (*x**i*,<=*t**i*) are distinct. | Print *m* integersΒ β the *i*-th of them is the moment of time when the answer for the *i*-th message will be received by the initiator. | [
"6 3\n0 1 2 3 2 5\n4 6\n6 9\n5 11\n",
"3 2\n0 1 1\n2 1\n3 1\n",
"8 3\n0 1 1 2 3 3 4 5\n6 1\n8 2\n4 5\n"
] | [
"14 13 11 ",
"5 3 ",
"7 6 11 "
] | In the first example the first message is initiated at the moment 6, reaches Bob at the moment 10, and the answer reaches the initiator at the moment 14. The second message reaches vertex 2 at the moment 11. At this moment the copy of Alice in this vertex is still waiting for the answer for the first message, so she rejects the second message. The answer reaches the initiator at the moment 13. The third message is not sent at all, because at the moment 11 Alice in vertex 5 is waiting for the answer for the second message.
In the second example the first message reaches Bob, the second is rejected by Alice in vertex 1. This is because the message with smaller initiator number has the priority.
In the third example the first and the third messages reach Bob, while the second message is rejected by Alice in vertex 3. | [] | 30 | 0 | 0 | 39,131 | |
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... | 46 | 4,812,800 | -1 | 39,390 | |
264 | Roadside Trees | [
"data structures",
"dp"
] | null | null | Squirrel Liss loves nuts. Liss asks you to plant some nut trees.
There are *n* positions (numbered 1 to *n* from west to east) to plant a tree along a street. Trees grow one meter per month. At the beginning of each month you should process one query. The query is one of the following types:
1. Plant a tree of height *h* at position *p*. 1. Cut down the *x*-th existent (not cut) tree from the west (where *x* is 1-indexed). When we cut the tree it drops down and takes all the available place at the position where it has stood. So no tree can be planted at this position anymore.
After processing each query, you should print the length of the longest increasing subsequence. A subset of existent trees is called an increasing subsequence if the height of the trees in the set is strictly increasing from west to east (for example, the westmost tree in the set must be the shortest in the set). The length of the increasing subsequence is the number of trees in it.
Note that Liss don't like the trees with the same heights, so it is guaranteed that at any time no two trees have the exactly same heights. | The first line contains two integers: *n* and *m* (1<=<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=2Β·105) β the number of positions and the number of queries.
Next *m* lines contains the information of queries by following formats:
- If the *i*-th query is type 1, the *i*-th line contains three integers: 1, *p**i*, and *h**i* (1<=β€<=*p**i*<=β€<=*n*,<=1<=β€<=*h**i*<=β€<=10), where *p**i* is the position of the new tree and *h**i* is the initial height of the new tree. - If the *i*-th query is type 2, the *i*-th line contains two integers: 2 and *x**i* (1<=β€<=*x**i*<=β€<=10), where the *x**i* is the index of the tree we want to cut.
The input is guaranteed to be correct, i.e.,
- For type 1 queries, *p**i* will be pairwise distinct. - For type 2 queries, *x**i* will be less than or equal to the current number of trees. - At any time no two trees have the exactly same heights.
In each line integers are separated by single spaces. | Print *m* integers β the length of the longest increasing subsequence after each query. Separate the numbers by whitespaces. | [
"4 6\n1 1 1\n1 4 4\n1 3 4\n2 2\n1 2 8\n2 3\n"
] | [
"1\n2\n3\n2\n2\n2\n"
] | States of street after each query you can see on the following animation:
If your browser doesn't support animation png, please see the gif version here: http://212.193.37.254/codeforces/images/162/roadtree.gif | [] | 124 | 0 | 0 | 39,451 | |
509 | Pretty Song | [
"math",
"strings"
] | null | null | When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song's prettiness. The title of the song is a word consisting of uppercase Latin letters. The prettiness of the song is the prettiness of its title.
Let's define the simple prettiness of a word as the ratio of the number of vowels in the word to the number of all letters in the word.
Let's define the prettiness of a word as the sum of simple prettiness of all the substrings of the word.
More formally, let's define the function *vowel*(*c*) which is equal to 1, if *c* is a vowel, and to 0 otherwise. Let *s**i* be the *i*-th character of string *s*, and *s**i*..*j* be the substring of word *s*, staring at the *i*-th character and ending at the *j*-th character (*s**is**i*<=+<=1... *s**j*, *i*<=β€<=*j*).
Then the simple prettiness of *s* is defined by the formula:
The prettiness of *s* equals
Find the prettiness of the given song title.
We assume that the vowels are *I*,<=*E*,<=*A*,<=*O*,<=*U*,<=*Y*. | The input contains a single string *s* (1<=β€<=|*s*|<=β€<=5Β·105) β the title of the song. | Print the prettiness of the song with the absolute or relative error of at most 10<=-<=6. | [
"IEAIAIO\n",
"BYOB\n",
"YISVOWEL\n"
] | [
"28.0000000\n",
"5.8333333\n",
"17.0500000\n"
] | In the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, the prettiness of the song equals to 28. | [
{
"input": "IEAIAIO",
"output": "28.0000000"
},
{
"input": "BYOB",
"output": "5.8333333"
},
{
"input": "YISVOWEL",
"output": "17.0500000"
},
{
"input": "EZYYOIYUZXEVRTOUYXIQ",
"output": "124.0168163"
},
{
"input": "MTOESEPRFEIWAIWLAFJMGBIQB",
"output": "127.22... | 77 | 13,516,800 | 3 | 39,455 | |
10 | Greedy Change | [
"constructive algorithms"
] | E. Greedy Change | 2 | 256 | Billy investigates the question of applying greedy algorithm to different spheres of life. At the moment he is studying the application of greedy algorithm to the problem about change. There is an amount of *n* coins of different face values, and the coins of each value are not limited in number. The task is to collect the sum *x* with the minimum amount of coins. Greedy algorithm with each its step takes the coin of the highest face value, not exceeding *x*. Obviously, if among the coins' face values exists the face value 1, any sum *x* can be collected with the help of greedy algorithm. However, greedy algorithm does not always give the optimal representation of the sum, i.e. the representation with the minimum amount of coins. For example, if there are face values {1,<=3,<=4} and it is asked to collect the sum 6, greedy algorithm will represent the sum as 4<=+<=1<=+<=1, while the optimal representation is 3<=+<=3, containing one coin less. By the given set of face values find out if there exist such a sum *x* that greedy algorithm will collect in a non-optimal way. If such a sum exists, find out the smallest of these sums. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=400) β the amount of the coins' face values. The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=109), describing the face values. It is guaranteed that *a*1<=><=*a*2<=><=...<=><=*a**n* and *a**n*<==<=1. | If greedy algorithm collects any sum in an optimal way, output -1. Otherwise output the smallest sum that greedy algorithm collects in a non-optimal way. | [
"5\n25 10 5 2 1\n",
"3\n4 3 1\n"
] | [
"-1\n",
"6\n"
] | none | [
{
"input": "5\n25 10 5 2 1",
"output": "-1"
},
{
"input": "3\n4 3 1",
"output": "6"
},
{
"input": "5\n9 8 5 2 1",
"output": "13"
},
{
"input": "5\n18 17 10 2 1",
"output": "27"
},
{
"input": "4\n73 70 33 1",
"output": "99"
},
{
"input": "4\n25 10 5 1",... | 2,000 | 0 | 0 | 39,476 |
505 | Mr. Kitayuta's Technology | [
"dfs and similar"
] | null | null | Shuseki Kingdom is the world's leading nation for innovation and technology. There are *n* cities in the kingdom, numbered from 1 to *n*.
Thanks to Mr. Kitayuta's research, it has finally become possible to construct teleportation pipes between two cities. A teleportation pipe will connect two cities unidirectionally, that is, a teleportation pipe from city *x* to city *y* cannot be used to travel from city *y* to city *x*. The transportation within each city is extremely developed, therefore if a pipe from city *x* to city *y* and a pipe from city *y* to city *z* are both constructed, people will be able to travel from city *x* to city *z* instantly.
Mr. Kitayuta is also involved in national politics. He considers that the transportation between the *m* pairs of city (*a**i*,<=*b**i*) (1<=β€<=*i*<=β€<=*m*) is important. He is planning to construct teleportation pipes so that for each important pair (*a**i*,<=*b**i*), it will be possible to travel from city *a**i* to city *b**i* by using one or more teleportation pipes (but not necessarily from city *b**i* to city *a**i*). Find the minimum number of teleportation pipes that need to be constructed. So far, no teleportation pipe has been constructed, and there is no other effective transportation between cities. | The first line contains two space-separated integers *n* and *m* (2<=β€<=*n*<=β€<=105,<=1<=β€<=*m*<=β€<=105), denoting the number of the cities in Shuseki Kingdom and the number of the important pairs, respectively.
The following *m* lines describe the important pairs. The *i*-th of them (1<=β€<=*i*<=β€<=*m*) contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*), denoting that it must be possible to travel from city *a**i* to city *b**i* by using one or more teleportation pipes (but not necessarily from city *b**i* to city *a**i*). It is guaranteed that all pairs (*a**i*,<=*b**i*) are distinct. | Print the minimum required number of teleportation pipes to fulfill Mr. Kitayuta's purpose. | [
"4 5\n1 2\n1 3\n1 4\n2 3\n2 4\n",
"4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4\n"
] | [
"3\n",
"4\n"
] | For the first sample, one of the optimal ways to construct pipes is shown in the image below:
For the second sample, one of the optimal ways is shown below: | [
{
"input": "4 5\n1 2\n1 3\n1 4\n2 3\n2 4",
"output": "3"
},
{
"input": "4 6\n1 2\n1 4\n2 3\n2 4\n3 2\n3 4",
"output": "4"
},
{
"input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4",
"output": "3"
},
{
"input": "3 6\n1 2\n1 3\n2 1\n2 3\n3 1\n3 2",
"output": "3"
},
{
"input"... | 31 | 0 | 0 | 39,508 | |
763 | Timofey and a flat tree | [
"data structures",
"graphs",
"hashing",
"shortest paths",
"trees"
] | null | null | Little Timofey has a big treeΒ β an undirected connected graph with *n* vertices and no simple cycles. He likes to walk along it. His tree is flat so when he walks along it he sees it entirely. Quite naturally, when he stands on a vertex, he sees the tree as a rooted tree with the root in this vertex.
Timofey assumes that the more non-isomorphic subtrees are there in the tree, the more beautiful the tree is. A subtree of a vertex is a subgraph containing this vertex and all its descendants. You should tell Timofey the vertex in which he should stand to see the most beautiful rooted tree.
Subtrees of vertices *u* and *v* are isomorphic if the number of children of *u* equals the number of children of *v*, and their children can be arranged in such a way that the subtree of the first son of *u* is isomorphic to the subtree of the first son of *v*, the subtree of the second son of *u* is isomorphic to the subtree of the second son of *v*, and so on. In particular, subtrees consisting of single vertex are isomorphic to each other. | First line contains single integer *n* (1<=β€<=*n*<=β€<=105)Β β number of vertices in the tree.
Each of the next *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=105, *u**i*<=β <=*v**i*), denoting the vertices the *i*-th edge connects.
It is guaranteed that the given graph is a tree. | Print single integerΒ β the index of the vertex in which Timofey should stand. If there are many answers, you can print any of them. | [
"3\n1 2\n2 3\n",
"7\n1 2\n4 2\n2 3\n5 6\n6 7\n3 7\n",
"10\n1 7\n1 8\n9 4\n5 1\n9 2\n3 5\n10 6\n10 9\n5 10\n"
] | [
"1\n",
"1\n",
"2\n"
] | In the first example we can stand in the vertex 1 or in the vertex 3 so that every subtree is non-isomorphic. If we stand in the vertex 2, then subtrees of vertices 1 and 3 are isomorphic.
In the second example, if we stand in the vertex 1, then only subtrees of vertices 4 and 5 are isomorphic.
In the third example, if we stand in the vertex 1, then subtrees of vertices 2, 3, 4, 6, 7 and 8 are isomorphic. If we stand in the vertex 2, than only subtrees of vertices 3, 4, 6, 7 and 8 are isomorphic. If we stand in the vertex 5, then subtrees of vertices 2, 3, 4, 6, 7 and 8 are isomorphic, and subtrees of vertices 1 and 9 are isomorphic as well: | [] | 30 | 0 | 0 | 39,613 | |
467 | Fedor and Essay | [
"dfs and similar",
"dp",
"graphs",
"hashing",
"strings"
] | null | null | After you had helped Fedor to find friends in the Β«Call of Soldiers 3Β» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language.
Fedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times.
As a result, Fedor wants to get an essay which contains as little letters Β«RΒ» (the case doesn't matter) as possible. If there are multiple essays with minimum number of Β«RΒ»s he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay.
Please note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG. | The first line contains a single integer *m* (1<=β€<=*m*<=β€<=105) β the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters.
The next line contains a single integer *n* (0<=β€<=*n*<=β€<=105) β the number of pairs of words in synonym dictionary. The *i*-th of the next *n* lines contains two space-separated non-empty words *x**i* and *y**i*. They mean that word *x**i* can be replaced with word *y**i* (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5Β·105 characters.
All the words at input can only consist of uppercase and lowercase letters of the English alphabet. | Print two integers β the minimum number of letters Β«RΒ» in an optimal essay and the minimum length of an optimal essay. | [
"3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y\n",
"2\nRuruRu fedya\n1\nruruRU fedor\n"
] | [
"2 6\n",
"1 10\n"
] | none | [
{
"input": "3\nAbRb r Zz\n4\nxR abRb\naA xr\nzz Z\nxr y",
"output": "2 6"
},
{
"input": "2\nRuruRu fedya\n1\nruruRU fedor",
"output": "1 10"
},
{
"input": "1\nffff\n1\nffff r",
"output": "0 4"
},
{
"input": "2\nYURA YUrA\n1\nyura fedya",
"output": "0 10"
},
{
"inp... | 31 | 1,638,400 | -1 | 39,656 | |
757 | Felicity's Big Secret Revealed | [
"bitmasks",
"dp"
] | null | null | The gym leaders were fascinated by the evolutions which took place at Felicity camp. So, they were curious to know about the secret behind evolving Pokemon.
The organizers of the camp gave the gym leaders a PokeBlock, a sequence of *n* ingredients. Each ingredient can be of type 0 or 1. Now the organizers told the gym leaders that to evolve a Pokemon of type *k* (*k*<=β₯<=2), they need to make a valid set of *k* cuts on the PokeBlock to get smaller blocks.
Suppose the given PokeBlock sequence is *b*0*b*1*b*2... *b**n*<=-<=1. You have a choice of making cuts at *n*<=+<=1 places, i.e., Before *b*0, between *b*0 and *b*1, between *b*1 and *b*2, ..., between *b**n*<=-<=2 and *b**n*<=-<=1, and after *b**n*<=-<=1.
The *n*<=+<=1 choices of making cuts are as follows (where a | denotes a possible cut):
Consider a sequence of *k* cuts. Now each pair of consecutive cuts will contain a binary string between them, formed from the ingredient types. The ingredients before the first cut and after the last cut are wasted, which is to say they are not considered. So there will be exactly *k*<=-<=1 such binary substrings. Every substring can be read as a binary number. Let *m* be the maximum number out of the obtained numbers. If all the obtained numbers are positive and the set of the obtained numbers contains all integers from 1 to *m*, then this set of cuts is said to be a valid set of cuts.
For example, suppose the given PokeBlock sequence is 101101001110 and we made 5 cuts in the following way:
So the 4 binary substrings obtained are: 11, 010, 01 and 1, which correspond to the numbers 3, 2, 1 and 1 respectively. Here *m*<==<=3, as it is the maximum value among the obtained numbers. And all the obtained numbers are positive and we have obtained all integers from 1 to *m*. Hence this set of cuts is a valid set of 5 cuts.
A Pokemon of type *k* will evolve only if the PokeBlock is cut using a valid set of *k* cuts. There can be many valid sets of the same size. Two valid sets of *k* cuts are considered different if there is a cut in one set which is not there in the other set.
Let *f*(*k*) denote the number of valid sets of *k* cuts. Find the value of . Since the value of *s* can be very large, output *s* modulo 109<=+<=7. | The input consists of two lines. The first line consists an integer *n* (1<=β€<=*n*<=β€<=75)Β β the length of the PokeBlock. The next line contains the PokeBlock, a binary string of length *n*. | Output a single integer, containing the answer to the problem, i.e., the value of *s* modulo 109<=+<=7. | [
"4\n1011\n",
"2\n10\n"
] | [
"10\n",
"1\n"
] | In the first sample, the sets of valid cuts are:
Size 2: |1|011, 1|01|1, 10|1|1, 101|1|.
Size 3: |1|01|1, |10|1|1, 10|1|1|, 1|01|1|.
Size 4: |10|1|1|, |1|01|1|.
Hence, *f*(2)β=β4, *f*(3)β=β4 and *f*(4)β=β2. So, the value of *s*β=β10.
In the second sample, the set of valid cuts is:
Size 2: |1|0.
Hence, *f*(2)β=β1 and *f*(3)β=β0. So, the value of *s*β=β1. | [
{
"input": "4\n1011",
"output": "10"
},
{
"input": "2\n10",
"output": "1"
},
{
"input": "7\n0110011",
"output": "28"
},
{
"input": "10\n0100011101",
"output": "80"
},
{
"input": "12\n010010101011",
"output": "298"
},
{
"input": "31\n1000000010111001111... | 30 | 0 | 0 | 39,660 | |
489 | Unbearable Controversy of Being | [
"brute force",
"combinatorics",
"dfs and similar",
"graphs"
] | null | null | Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It's no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!
Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections *a*, *b*, *c* and *d*, such that there are two paths from *a* to *c* β one through *b* and the other one through *d*, he calls the group a "damn rhombus". Note that pairs (*a*,<=*b*), (*b*,<=*c*), (*a*,<=*d*), (*d*,<=*c*) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below:
Other roads between any of the intersections don't make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him.
Given that the capital of Berland has *n* intersections and *m* roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.
When rhombi are compared, the order of intersections *b* and *d* doesn't matter. | The first line of the input contains a pair of integers *n*, *m* (1<=β€<=*n*<=β€<=3000,<=0<=β€<=*m*<=β€<=30000) β the number of intersections and roads, respectively. Next *m* lines list the roads, one per line. Each of the roads is given by a pair of integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*;*a**i*<=β <=*b**i*) β the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.
It is not guaranteed that you can get from any intersection to any other one. | Print the required number of "damn rhombi". | [
"5 4\n1 2\n2 3\n1 4\n4 3\n",
"4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3\n"
] | [
"1\n",
"12\n"
] | none | [
{
"input": "5 4\n1 2\n2 3\n1 4\n4 3",
"output": "1"
},
{
"input": "4 12\n1 2\n1 3\n1 4\n2 1\n2 3\n2 4\n3 1\n3 2\n3 4\n4 1\n4 2\n4 3",
"output": "12"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "10 20\n6 10\n4 2\n1 5\n6 1\n8 9\n1 3\n2 6\n9 7\n4 5\n3 7\n9 2\n3 9\n4 8\n1 10... | 1,000 | 81,920,000 | 0 | 39,706 | |
185 | Mushroom Scientists | [
"math",
"ternary search"
] | null | null | As you very well know, the whole Universe traditionally uses three-dimensional Cartesian system of coordinates. In this system each point corresponds to three real coordinates (*x*,<=*y*,<=*z*). In this coordinate system, the distance between the center of the Universe and the point is calculated by the following formula: . Mushroom scientists that work for the Great Mushroom King think that the Universe isn't exactly right and the distance from the center of the Universe to a point equals *x**a*Β·*y**b*Β·*z**c*.
To test the metric of mushroom scientists, the usual scientists offered them a task: find such *x*,<=*y*,<=*z* (0<=β€<=*x*,<=*y*,<=*z*;Β *x*<=+<=*y*<=+<=*z*<=β€<=*S*), that the distance between the center of the Universe and the point (*x*,<=*y*,<=*z*) is maximum possible in the metric of mushroom scientists. The mushroom scientists aren't good at maths, so they commissioned you to do the task.
Note that in this problem, it is considered that 00<==<=1. | The first line contains a single integer *S* (1<=β€<=*S*<=β€<=103) β the maximum sum of coordinates of the sought point.
The second line contains three space-separated integers *a*, *b*, *c* (0<=β€<=*a*,<=*b*,<=*c*<=β€<=103) β the numbers that describe the metric of mushroom scientists. | Print three real numbers β the coordinates of the point that reaches maximum value in the metrics of mushroom scientists. If there are multiple answers, print any of them that meets the limitations.
A natural logarithm of distance from the center of the Universe to the given point in the metric of mushroom scientists shouldn't differ from the natural logarithm of the maximum distance by more than 10<=-<=6. We think that *ln*(0)<==<=<=-<=β. | [
"3\n1 1 1\n",
"3\n2 0 0\n"
] | [
"1.0 1.0 1.0\n",
"3.0 0.0 0.0\n"
] | none | [
{
"input": "3\n1 1 1",
"output": "1.0 1.0 1.0"
},
{
"input": "3\n2 0 0",
"output": "3.0 0.0 0.0"
},
{
"input": "10\n1 6 3",
"output": "1.0 6.0 3.0"
},
{
"input": "9\n8 2 0",
"output": "7.2 1.8 0.0"
},
{
"input": "1\n0 9 2",
"output": "0.0 0.8181818181818182 0.... | 466 | 2,662,400 | -1 | 39,785 | |
906 | Seating of Students | [
"brute force",
"constructive algorithms",
"math"
] | null | null | Students went into a class to write a test and sat in some way. The teacher thought: "Probably they sat in this order to copy works of each other. I need to rearrange them in such a way that students that were neighbors are not neighbors in a new seating."
The class can be represented as a matrix with *n* rows and *m* columns with a student in each cell. Two students are neighbors if cells in which they sit have a common side.
Let's enumerate students from 1 to *n*Β·*m* in order of rows. So a student who initially sits in the cell in row *i* and column *j* has a number (*i*<=-<=1)Β·*m*<=+<=*j*. You have to find a matrix with *n* rows and *m* columns in which all numbers from 1 to *n*Β·*m* appear exactly once and adjacent numbers in the original matrix are not adjacent in it, or determine that there is no such matrix. | The only line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105; *n*Β·*m*<=β€<=105)Β β the number of rows and the number of columns in the required matrix. | If there is no such matrix, output "NO" (without quotes).
Otherwise in the first line output "YES" (without quotes), and in the next *n* lines output *m* integers which form the required matrix. | [
"2 4\n",
"2 1\n"
] | [
"YES\n5 4 7 2 \n3 6 1 8 \n",
"NO\n"
] | In the first test case the matrix initially looks like this:
It's easy to see that there are no two students that are adjacent in both matrices.
In the second test case there are only two possible seatings and in both of them students with numbers 1 and 2 are neighbors. | [
{
"input": "2 4",
"output": "YES\n5 4 7 2 \n3 6 1 8 "
},
{
"input": "2 1",
"output": "NO"
},
{
"input": "1 1",
"output": "YES\n1"
},
{
"input": "1 2",
"output": "NO"
},
{
"input": "1 3",
"output": "NO"
},
{
"input": "2 2",
"output": "NO"
},
{
... | 46 | 5,529,600 | 0 | 39,913 | |
535 | Tavas and Pashmaks | [
"geometry",
"math"
] | null | null | Tavas is a cheerleader in the new sports competition named "Pashmaks".
This competition consists of two part: swimming and then running. People will immediately start running *R* meters after they finished swimming exactly *S* meters. A winner is a such person that nobody else finishes running before him/her (there may be more than one winner).
Before the match starts, Tavas knows that there are *n* competitors registered for the match. Also, he knows that *i*-th person's swimming speed is *s**i* meters per second and his/her running speed is *r**i* meters per second. Unfortunately, he doesn't know the values of *R* and *S*, but he knows that they are real numbers greater than 0.
As a cheerleader, Tavas wants to know who to cheer up. So, he wants to know all people that might win. We consider a competitor might win if and only if there are some values of *R* and *S* such that with these values, (s)he will be a winner.
Tavas isn't really familiar with programming, so he asked you to help him. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=2<=Γ<=105).
The next *n* lines contain the details of competitors. *i*-th line contains two integers *s**i* and *r**i* (1<=β€<=*s**i*,<=*r**i*<=β€<=104). | In the first and the only line of output, print a sequence of numbers of possible winners in increasing order. | [
"3\n1 3\n2 2\n3 1\n",
"3\n1 2\n1 1\n2 1\n"
] | [
"1 2 3 \n",
"1 3 \n"
] | none | [] | 701 | 23,961,600 | 0 | 39,961 | |
131 | The World is a Theatre | [
"combinatorics",
"math"
] | null | null | There are *n* boys and *m* girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly *t* actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different.
Perform all calculations in the 64-bit type: long long for Π‘/Π‘++, int64 for Delphi and long for Java. | The only line of the input data contains three integers *n*, *m*, *t* (4<=β€<=*n*<=β€<=30,<=1<=β€<=*m*<=β€<=30,<=5<=β€<=*t*<=β€<=*n*<=+<=*m*). | Find the required number of ways.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. | [
"5 2 5\n",
"4 3 5\n"
] | [
"10\n",
"3\n"
] | none | [
{
"input": "5 2 5",
"output": "10"
},
{
"input": "4 3 5",
"output": "3"
},
{
"input": "4 1 5",
"output": "1"
},
{
"input": "7 3 6",
"output": "168"
},
{
"input": "30 30 30",
"output": "118264581548187697"
},
{
"input": "10 10 8",
"output": "84990"
... | 92 | 0 | 3 | 40,014 | |
0 | none | [
"none"
] | null | null | Sometime the classic solution are not powerful enough and we have to design our own. For the purpose of this problem you have to implement the part of the system of task scheduling.
Each task should be executed at some particular moments of time. In our system you may set the exact value for the second, minute, hour, day of the week, day and month, when the task should be executed. Moreover, one can set a special value -1 that means any value of this parameter is valid.
For example, if the parameter string is -1 59 23 -1 -1 -1, the problem will be executed every day at 23:59:00, 23:59:01, 23:59:02, ..., 23:59:59 (60 times in total).
Seconds, minutes and hours are numbered starting from zero, while day, months and days of the week are numbered starting from one. The first day of the week is Monday.
There is one special case that is treated separately. If both day of the week and day are given (i.e. differ from -1) to execute the task only one of these two (at least one, if both match this is fine too) parameters should match the current time (of course, all other parameters should match too). For example, the string of parameters 0 0 12 6 3 7 means that the task will be executed both on Saturday, July 2nd, 2016 and on Sunday, July 3rd, 2016 at noon.
One should not forget about the existence of the leap years. The year is leap if it's number is divisible by 400, or is not divisible by 100, but is divisible by 4. Each leap year has 366 days instead of usual 365, by extending February to 29 days rather than the common 28.
The current time is represented as the number of seconds passed after 00:00:00 January 1st, 1970 (Thursday).
You are given the string of six parameters, describing the moments of time the task should be executed. You are also given a number of moments of time. For each of them you have to find the first moment of time strictly greater than the current when the task will be executed. | The first line of the input contains six integers *s*, *m*, *h*, *day*, *date* and *month* (0<=β€<=*s*,<=*m*<=β€<=59, 0<=β€<=*h*<=β€<=23, 1<=β€<=*day*<=β€<=7, 1<=β€<=*date*<=β€<=31, 1<=β€<=*month*<=β€<=12). Each of the number can also be equal to <=-<=1. It's guaranteed, that there are infinitely many moments of time when this task should be executed.
Next line contains the only integer *n* (1<=β€<=*n*<=β€<=1000)Β β the number of moments of time you have to solve the problem for. Each of the next *n* lines contains a single integer *t**i* (0<=β€<=*t**i*<=β€<=1012). | Print *n* lines, the *i*-th of them should contain the first moment of time strictly greater than *t**i*, when the task should be executed. | [
"-1 59 23 -1 -1 -1\n6\n1467372658\n1467417540\n1467417541\n1467417598\n1467417599\n1467417600\n",
"0 0 12 6 3 7\n3\n1467372658\n1467460810\n1467547200\n"
] | [
"1467417540\n1467417541\n1467417542\n1467417599\n1467503940\n1467503940\n",
"1467460800\n1467547200\n1468065600\n"
] | The moment of time 1467372658 after the midnight of January 1st, 1970 is 11:30:58 July 1st, 2016. | [] | 61 | 512,000 | -1 | 40,051 | |
774 | Amusement Park | [
"*special",
"ternary search"
] | null | null | Pupils decided to go to amusement park. Some of them were with parents. In total, *n* people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.
Tickets for group of *x* people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is *c*1<=+<=*c*2Β·(*x*<=-<=1)2 (in particular, if the group consists of one person, then the price is *c*1).
All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group. | The first line contains three integers *n*, *c*1 and *c*2 (1<=β€<=*n*<=β€<=200<=000, 1<=β€<=*c*1,<=*c*2<=β€<=107)Β β the number of visitors and parameters for determining the ticket prices for a group.
The second line contains the string of length *n*, which consists of zeros and ones. If the *i*-th symbol of the string is zero, then the *i*-th visitor is a pupil, otherwise the *i*-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils. | Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once. | [
"3 4 1\n011\n",
"4 7 2\n1101\n"
] | [
"8\n",
"18\n"
] | In the first test one group of three people should go to the attraction. Then they have to pay 4β+β1β*β(3β-β1)<sup class="upper-index">2</sup>β=β8.
In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7β+β2β*β(2β-β1)<sup class="upper-index">2</sup>β=β9. Thus, the total price for two groups is 18. | [
{
"input": "3 4 1\n011",
"output": "8"
},
{
"input": "4 7 2\n1101",
"output": "18"
},
{
"input": "1 2 2\n1",
"output": "2"
},
{
"input": "2 3 10\n01",
"output": "13"
},
{
"input": "5 10 3\n11100",
"output": "35"
},
{
"input": "10 2 2\n1111101111",
... | 62 | 7,987,200 | 3 | 40,053 | |
388 | Fox and Card Game | [
"games",
"greedy",
"sortings"
] | null | null | Fox Ciel is playing a card game with her friend Fox Jiro. There are *n* piles of cards on the table. And there is a positive integer on each card.
The players take turns and Ciel takes the first turn. In Ciel's turn she takes a card from the top of any non-empty pile, and in Jiro's turn he takes a card from the bottom of any non-empty pile. Each player wants to maximize the total sum of the cards he took. The game ends when all piles become empty.
Suppose Ciel and Jiro play optimally, what is the score of the game? | The first line contain an integer *n* (1<=β€<=*n*<=β€<=100). Each of the next *n* lines contains a description of the pile: the first integer in the line is *s**i* (1<=β€<=*s**i*<=β€<=100) β the number of cards in the *i*-th pile; then follow *s**i* positive integers *c*1, *c*2, ..., *c**k*, ..., *c**s**i* (1<=β€<=*c**k*<=β€<=1000) β the sequence of the numbers on the cards listed from top of the current pile to bottom of the pile. | Print two integers: the sum of Ciel's cards and the sum of Jiro's cards if they play optimally. | [
"2\n1 100\n2 1 10\n",
"1\n9 2 8 6 5 9 4 7 1 3\n",
"3\n3 1 3 2\n3 5 4 6\n2 8 7\n",
"3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000\n"
] | [
"101 10\n",
"30 15\n",
"18 18\n",
"7000 7000\n"
] | In the first example, Ciel will take the cards with number 100 and 1, Jiro will take the card with number 10.
In the second example, Ciel will take cards with numbers 2, 8, 6, 5, 9 and Jiro will take cards with numbers 4, 7, 1, 3. | [
{
"input": "2\n1 100\n2 1 10",
"output": "101 10"
},
{
"input": "1\n9 2 8 6 5 9 4 7 1 3",
"output": "30 15"
},
{
"input": "3\n3 1 3 2\n3 5 4 6\n2 8 7",
"output": "18 18"
},
{
"input": "3\n3 1000 1000 1000\n6 1000 1000 1000 1000 1000 1000\n5 1000 1000 1000 1000 1000",
"out... | 155 | 716,800 | 3 | 40,104 | |
191 | Thwarting Demonstrations | [
"binary search",
"data structures",
"trees"
] | null | null | It is dark times in Berland. Berlyand opposition, funded from a neighboring state, has organized a demonstration in Berland capital Bertown. Through the work of intelligence we know that the demonstrations are planned to last for *k* days.
Fortunately, Berland has a special police unit, which can save the country. It has exactly *n* soldiers numbered from 1 to *n*. Berland general, the commander of the detachment, must schedule the detachment's work in these difficult *k* days. In each of these days, the general must send a certain number of police officers to disperse riots. Since the detachment is large and the general is not very smart, he can only select a set of all soldiers numbered from *l* to *r*, inclusive, where *l* and *r* are selected arbitrarily.
Now the general has exactly two problems. First, he cannot send the same group twice β then soldiers get bored and they rebel. Second, not all soldiers are equally reliable. Every soldier has a reliability of *a**i*. The reliability of the detachment is counted as the sum of reliabilities of soldiers in it. The reliability of a single soldier can be negative, then when you include him in the detachment, he will only spoil things. The general is distinguished by his great greed and shortsightedness, so each day he sends to the dissolution the most reliable group of soldiers possible (that is, of all the groups that have not been sent yet).
The Berland Government has decided to know what would be the minimum reliability of the detachment, sent to disperse the demonstrations during these *k* days. The general himself can not cope with such a difficult task. Help him to not embarrass himself in front of his superiors! | The first line contains two integers *n* and *k* β the number of soldiers in the detachment and the number of times somebody goes on duty.
The second line contains *n* space-separated integers *a**i*, their absolute value doesn't exceed 109 β the soldiers' reliabilities.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++, it is preferred to use cin, cout streams of the %I64d specifier. | Print a single number β the sought minimum reliability of the groups that go on duty during these *k* days. | [
"3 4\n1 4 2\n",
"4 6\n2 -1 2 -1\n",
"8 10\n1 -2 3 -4 5 -6 7 -8\n"
] | [
"4\n",
"1\n",
"2\n"
] | none | [
{
"input": "3 4\n1 4 2",
"output": "4"
},
{
"input": "4 6\n2 -1 2 -1",
"output": "1"
},
{
"input": "8 10\n1 -2 3 -4 5 -6 7 -8",
"output": "2"
},
{
"input": "10 13\n11 73 57 -34 61 38 -83 10 -88 -32",
"output": "99"
},
{
"input": "20 31\n19 38 -67 83 -83 79 98 -8 8... | 92 | 0 | 0 | 40,110 | |
846 | Monitor | [
"binary search",
"data structures"
] | null | null | Recently Luba bought a monitor. Monitor is a rectangular matrix of size *n*<=Γ<=*m*. But then she started to notice that some pixels cease to work properly. Luba thinks that the monitor will become broken the first moment when it contains a square *k*<=Γ<=*k* consisting entirely of broken pixels. She knows that *q* pixels are already broken, and for each of them she knows the moment when it stopped working. Help Luba to determine when the monitor became broken (or tell that it's still not broken even after all *q* pixels stopped working). | The first line contains four integer numbers *n*,<=*m*,<=*k*,<=*q*Β (1<=β€<=*n*,<=*m*<=β€<=500,<=1<=β€<=*k*<=β€<=*min*(*n*,<=*m*),<=0<=β€<=*q*<=β€<=*n*Β·*m*) β the length and width of the monitor, the size of a rectangle such that the monitor is broken if there is a broken rectangle with this size, and the number of broken pixels.
Each of next *q* lines contain three integer numbers *x**i*,<=*y**i*,<=*t**i*Β (1<=β€<=*x**i*<=β€<=*n*,<=1<=β€<=*y**i*<=β€<=*m*,<=0<=β€<=*t*<=β€<=109) β coordinates of *i*-th broken pixel (its row and column in matrix) and the moment it stopped working. Each pixel is listed at most once.
We consider that pixel is already broken at moment *t**i*. | Print one number β the minimum moment the monitor became broken, or "-1" if it's still not broken after these *q* pixels stopped working. | [
"2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2\n",
"3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100\n"
] | [
"8\n",
"-1\n"
] | none | [
{
"input": "2 3 2 5\n2 1 8\n2 2 8\n1 2 1\n1 3 4\n2 3 2",
"output": "8"
},
{
"input": "3 3 2 5\n1 2 2\n2 2 1\n2 3 5\n3 2 10\n2 1 100",
"output": "-1"
},
{
"input": "29 50 5 29\n21 42 1565821\n21 43 53275635\n21 44 2717830\n21 45 9579585\n21 46 20725775\n22 42 2568372\n22 43 9584662\n22 44... | 2,000 | 32,358,400 | 0 | 40,156 | |
949 | A Leapfrog in the Array | [
"constructive algorithms",
"math"
] | null | null | Dima is a beginner programmer. During his working process, he regularly has to repeat the following operation again and again: to remove every second element from the array. One day he has been bored with easy solutions of this problem, and he has come up with the following extravagant algorithm.
Let's consider that initially array contains *n* numbers from 1 to *n* and the number *i* is located in the cell with the index 2*i*<=-<=1 (Indices are numbered starting from one) and other cells of the array are empty. Each step Dima selects a non-empty array cell with the maximum index and moves the number written in it to the nearest empty cell to the left of the selected one. The process continues until all *n* numbers will appear in the first *n* cells of the array. For example if *n*<==<=4, the array is changing as follows:
You have to write a program that allows you to determine what number will be in the cell with index *x* (1<=β€<=*x*<=β€<=*n*) after Dima's algorithm finishes. | The first line contains two integers *n* and *q* (1<=β€<=*n*<=β€<=1018, 1<=β€<=*q*<=β€<=200<=000), the number of elements in the array and the number of queries for which it is needed to find the answer.
Next *q* lines contain integers *x**i* (1<=β€<=*x**i*<=β€<=*n*), the indices of cells for which it is necessary to output their content after Dima's algorithm finishes. | For each of *q* queries output one integer number, the value that will appear in the corresponding array cell after Dima's algorithm finishes. | [
"4 3\n2\n3\n4\n",
"13 4\n10\n5\n4\n8\n"
] | [
"3\n2\n4\n",
"13\n3\n8\n9\n"
] | The first example is shown in the picture.
In the second example the final array is [1,β12,β2,β8,β3,β11,β4,β9,β5,β13,β6,β10,β7]. | [
{
"input": "4 3\n2\n3\n4",
"output": "3\n2\n4"
},
{
"input": "13 4\n10\n5\n4\n8",
"output": "13\n3\n8\n9"
},
{
"input": "2 2\n1\n2",
"output": "1\n2"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "3 3\n3\n2\n1",
"output": "2\n3\n1"
},
{
"input": ... | 2,000 | 819,200 | 0 | 40,194 | |
455 | Function | [
"data structures"
] | null | null | Serega and Fedor play with functions. One day they came across a very interesting function. It looks like that:
- *f*(1,<=*j*)<==<=*a*[*j*], 1<=β€<=*j*<=β€<=*n*. - *f*(*i*,<=*j*)<==<=*min*(*f*(*i*<=-<=1,<=*j*),<=*f*(*i*<=-<=1,<=*j*<=-<=1))<=+<=*a*[*j*], 2<=β€<=*i*<=β€<=*n*, *i*<=β€<=*j*<=β€<=*n*.
Here *a* is an integer array of length *n*.
Serega and Fedya want to know what values this function takes at some points. But they don't want to calculate the values manually. So they ask you to help them. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the length of array *a*. The next line contains *n* integers: *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=β€<=*a*[*i*]<=β€<=104).
The next line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of queries. Each of the next *m* lines contains two integers: *x**i*, *y**i* (1<=β€<=*x**i*<=β€<=*y**i*<=β€<=*n*). Each line means that Fedor and Serega want to know the value of *f*(*x**i*,<=*y**i*). | Print *m* lines β the answers to the guys' queries. | [
"6\n2 2 3 4 3 4\n4\n4 5\n3 4\n3 4\n2 3\n",
"7\n1 3 2 3 4 0 2\n4\n4 5\n2 3\n1 4\n4 6\n"
] | [
"12\n9\n9\n5\n",
"11\n4\n3\n0\n"
] | none | [] | 31 | 0 | 0 | 40,236 | |
690 | Photographs (II) | [] | null | null | Zombies seem to have become much more intelligent lately β a few have somehow wandered into the base through the automatic gate. Heidi has had to beef up security, and a new gate has been installed. Unfortunately, now the questions being asked are more complicated, and even humans have trouble answering them. Can you still program the robot army to do this reliably?
The new questions are of the following form: a grayscale photograph has been divided into several horizontal pieces, which have been arbitrarily rearranged. The task is to assemble the original image back from these pieces (somewhat like in a jigsaw puzzle). To further delay the zombies, significant Gaussian-distributed noise has been added to the image. | The input format is the same as in the previous version, except that the first line of every question now contains three space-separated numbers *h*, *w* and *k* (1<=β€<=*h*,<=*w*<=β€<=600, 2<=β€<=*k*<=β€<=16) β the height (number of rows) and width (number of columns) of the photograph and the number of pieces, respectively. The number of pieces evenly divides the height, and each piece is of the same height *h*<=/<=*k*.
Again, there is only one input file to be processed, and the same resources are provided to you as in the previous version (except that now you are given all input images in .bmp format, rather than the first 50). | Your program should print *q* lines. The *i*-th line should contain your answer for the *i*-th question: a space-separated sequence of *k* numbers Ο1,<=Ο2,<=...,<=Ο*k* such that:
- Ο is a permutation of {1,<=2,<=...,<=*k*}, that is, each number from 1 to *k* appears exactly once in Ο, - for each *j*<==<=1,<=...,<=*k*, Ο*j* is the position (index), in the original image, of the piece which is at position *j* in the input image. (See the illustration below for clarity.)
The second image from the test set. If the three pieces in the original image are numbered 1, 2, 3 from top to bottom, then the numbering in the image on the right should be 2, 3, 1. The correct answer for this image is thus 2 3 1.
Again, your answers will be accepted if they conform to this format and if at least 75% of them are correct.
Again, you may process the input locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in). | [] | [] | The link to download all the necessary materials is http://assets.codeforces.com/files/690/medium_contestant_package.zip | [] | 46 | 0 | 0 | 40,247 | |
83 | Numbers | [
"dp",
"math",
"number theory"
] | D. Numbers | 3 | 256 | One quite ordinary day Valera went to school (there's nowhere else he should go on a week day). In a maths lesson his favorite teacher Ms. Evans told students about divisors. Despite the fact that Valera loved math, he didn't find this particular topic interesting. Even more, it seemed so boring that he fell asleep in the middle of a lesson. And only a loud ringing of a school bell could interrupt his sweet dream.
Of course, the valuable material and the teacher's explanations were lost. However, Valera will one way or another have to do the homework. As he does not know the new material absolutely, he cannot do the job himself. That's why he asked you to help. You're his best friend after all, you just cannot refuse to help.
Valera's home task has only one problem, which, though formulated in a very simple way, has not a trivial solution. Its statement looks as follows: if we consider all positive integers in the interval [*a*;*b*] then it is required to count the amount of such numbers in this interval that their smallest divisor will be a certain integer *k* (you do not have to consider divisor equal to one). In other words, you should count the amount of such numbers from the interval [*a*;*b*], that are not divisible by any number between 2 and *k*<=-<=1 and yet are divisible by *k*. | The first and only line contains three positive integers *a*, *b*, *k* (1<=β€<=*a*<=β€<=*b*<=β€<=2Β·109,<=2<=β€<=*k*<=β€<=2Β·109). | Print on a single line the answer to the given problem. | [
"1 10 2\n",
"12 23 3\n",
"6 19 5\n"
] | [
"5\n",
"2\n",
"0\n"
] | Comments to the samples from the statement:
In the first sample the answer is numbers 2,β4,β6,β8,β10.
In the second one β 15,β21
In the third one there are no such numbers. | [
{
"input": "1 10 2",
"output": "5"
},
{
"input": "12 23 3",
"output": "2"
},
{
"input": "6 19 5",
"output": "0"
},
{
"input": "1 80 7",
"output": "3"
},
{
"input": "100 1000 1009",
"output": "0"
},
{
"input": "11 124 11",
"output": "2"
},
{
... | 0 | 0 | -1 | 40,373 |
691 | Couple Cover | [
"brute force",
"dp",
"number theory"
] | null | null | Couple Cover, a wildly popular luck-based game, is about to begin! Two players must work together to construct a rectangle. A bag with *n* balls, each with an integer written on it, is placed on the table. The first player reaches in and grabs a ball randomly (all balls have equal probability of being chosen) β the number written on this ball is the rectangle's width in meters. This ball is not returned to the bag, and the second player reaches into the bag and grabs another ball β the number written on this ball is the rectangle's height in meters. If the area of the rectangle is greater than or equal some threshold *p* square meters, the players win. Otherwise, they lose.
The organizers of the game are trying to select an appropriate value for *p* so that the probability of a couple winning is not too high and not too low, but they are slow at counting, so they have hired you to answer some questions for them. You are given a list of the numbers written on the balls, the organizers would like to know how many winning pairs of balls exist for different values of *p*. Note that two pairs are different if either the first or the second ball is different between the two in pair, and two different balls with the same number are considered different. | The input begins with a single positive integer *n* in its own line (1<=β€<=*n*<=β€<=106).
The second line contains *n* positive integers β the *i*-th number in this line is equal to *a**i* (1<=β€<=*a**i*<=β€<=3Β·106), the number written on the *i*-th ball.
The next line contains an integer *m* (1<=β€<=*m*<=β€<=106), the number of questions you are being asked.
Then, the following line contains *m* positive integers β the *j*-th number in this line is equal to the value of *p* (1<=β€<=*p*<=β€<=3Β·106) in the *j*-th question you are being asked. | For each question, print the number of winning pairs of balls that exist for the given value of *p* in the separate line. | [
"5\n4 2 6 1 3\n4\n1 3 5 8\n",
"2\n5 6\n2\n30 31\n"
] | [
"20\n18\n14\n10\n",
"2\n0\n"
] | none | [
{
"input": "5\n4 2 6 1 3\n4\n1 3 5 8",
"output": "20\n18\n14\n10"
},
{
"input": "2\n5 6\n2\n30 31",
"output": "2\n0"
},
{
"input": "2\n2000000 2000000\n1\n2000000",
"output": "2"
},
{
"input": "1\n1\n1\n5",
"output": "0"
},
{
"input": "10\n18 34 3 49 40 50 53 30 2... | 3,000 | 71,372,800 | 0 | 40,426 | |
832 | Vasya and Shifts | [
"matrices"
] | null | null | Vasya has a set of 4*n* strings of equal length, consisting of lowercase English letters "a", "b", "c", "d" and "e". Moreover, the set is split into *n* groups of 4 equal strings each. Vasya also has one special string *a* of the same length, consisting of letters "a" only.
Vasya wants to obtain from string *a* some fixed string *b*, in order to do this, he can use the strings from his set in any order. When he uses some string *x*, each of the letters in string *a* replaces with the next letter in alphabet as many times as the alphabet position, counting from zero, of the corresponding letter in string *x*. Within this process the next letter in alphabet after "e" is "a".
For example, if some letter in *a* equals "b", and the letter on the same position in *x* equals "c", then the letter in *a* becomes equal "d", because "c" is the second alphabet letter, counting from zero. If some letter in *a* equals "e", and on the same position in *x* is "d", then the letter in *a* becomes "c". For example, if the string *a* equals "abcde", and string *x* equals "baddc", then *a* becomes "bbabb".
A used string disappears, but Vasya can use equal strings several times.
Vasya wants to know for *q* given strings *b*, how many ways there are to obtain from the string *a* string *b* using the given set of 4*n* strings? Two ways are different if the number of strings used from some group of 4 strings is different. Help Vasya compute the answers for these questions modulo 109<=+<=7. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=500)Β β the number of groups of four strings in the set, and the length of all strings.
Each of the next *n* lines contains a string *s* of length *m*, consisting of lowercase English letters "a", "b", "c", "d" and "e". This means that there is a group of four strings equal to *s*.
The next line contains single integer *q* (1<=β€<=*q*<=β€<=300)Β β the number of strings *b* Vasya is interested in.
Each of the next *q* strings contains a string *b* of length *m*, consisting of lowercase English letters "a", "b", "c", "d" and "e"Β β a string Vasya is interested in. | For each string Vasya is interested in print the number of ways to obtain it from string *a*, modulo 109<=+<=7. | [
"1 1\nb\n2\na\ne\n",
"2 4\naaaa\nbbbb\n1\ncccc\n"
] | [
"1\n1\n",
"5\n"
] | In the first example, we have 4 strings "b". Then we have the only way for each string *b*: select 0 strings "b" to get "a" and select 4 strings "b" to get "e", respectively. So, we have 1 way for each request.
In the second example, note that the choice of the string "aaaa" does not change anything, that is we can choose any amount of it (from 0 to 4, it's 5 different ways) and we have to select the line "bbbb" 2 times, since other variants do not fit. We get that we have 5 ways for the request. | [] | 31 | 0 | 0 | 40,458 | |
746 | Music in Car | [
"data structures",
"greedy",
"two pointers"
] | null | null | Sasha reaches the work by car. It takes exactly *k* minutes. On his way he listens to music. All songs in his playlist go one by one, after listening to the *i*-th song Sasha gets a pleasure which equals *a**i*. The *i*-th song lasts for *t**i* minutes.
Before the beginning of his way Sasha turns on some song *x* and then he listens to the songs one by one: at first, the song *x*, then the song (*x*<=+<=1), then the song number (*x*<=+<=2), and so on. He listens to songs until he reaches the work or until he listens to the last song in his playlist.
Sasha can listen to each song to the end or partly.
In the second case he listens to the song for integer number of minutes, at least half of the song's length. Formally, if the length of the song equals *d* minutes, Sasha listens to it for no less than minutes, then he immediately switches it to the next song (if there is such). For example, if the length of the song which Sasha wants to partly listen to, equals 5 minutes, then he should listen to it for at least 3 minutes, if the length of the song equals 8 minutes, then he should listen to it for at least 4 minutes.
It takes no time to switch a song.
Sasha wants to listen partly no more than *w* songs. If the last listened song plays for less than half of its length, then Sasha doesn't get pleasure from it and that song is not included to the list of partly listened songs. It is not allowed to skip songs. A pleasure from a song does not depend on the listening mode, for the *i*-th song this value equals *a**i*.
Help Sasha to choose such *x* and no more than *w* songs for partial listening to get the maximum pleasure. Write a program to find the maximum pleasure Sasha can get from the listening to the songs on his way to the work. | The first line contains three integers *n*, *w* and *k* (1<=β€<=*w*<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*k*<=β€<=2Β·109)Β β the number of songs in the playlist, the number of songs Sasha can listen to partly and time in minutes which Sasha needs to reach work.
The second line contains *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=104), where *a**i* equals the pleasure Sasha gets after listening to the *i*-th song.
The third line contains *n* positive integers *t*1,<=*t*2,<=...,<=*t**n* (2<=β€<=*t**i*<=β€<=104), where *t**i* equals the length of the *i*-th song in minutes. | Print the maximum pleasure Sasha can get after listening to the songs on the way to work. | [
"7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9\n",
"8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8\n",
"1 1 5\n6\n9\n",
"1 1 3\n4\n7\n"
] | [
"12\n",
"19\n",
"6\n",
"0\n"
] | In the first example Sasha needs to start listening from the song number 2. He should listen to it partly (for 4 minutes), then listen to the song number 3 to the end (for 3 minutes) and then partly listen to the song number 4 (for 3 minutes). After listening to these songs Sasha will get pleasure which equals 4β+β3β+β5β=β12. Sasha will not have time to listen to the song number 5 because he will spend 4β+β3β+β3β=β10 minutes listening to songs number 2, 3 and 4 and only 1 minute is left after that. | [
{
"input": "7 2 11\n3 4 3 5 1 4 6\n7 7 3 6 5 3 9",
"output": "12"
},
{
"input": "8 4 20\n5 6 4 3 7 5 4 1\n10 12 5 12 14 8 5 8",
"output": "19"
},
{
"input": "1 1 5\n6\n9",
"output": "6"
},
{
"input": "1 1 3\n4\n7",
"output": "0"
},
{
"input": "3 1 5\n2 5 3\n4 4 5"... | 46 | 0 | 0 | 40,497 | |
0 | none | [
"none"
] | null | null | Alexandra has a paper strip with *n* numbers on it. Let's call them *a**i* from left to right.
Now Alexandra wants to split it into some pieces (possibly 1). For each piece of strip, it must satisfy:
- Each piece should contain at least *l* numbers.- The difference between the maximal and the minimal number on the piece should be at most *s*.
Please help Alexandra to find the minimal number of pieces meeting the condition above. | The first line contains three space-separated integers *n*,<=*s*,<=*l* (1<=β€<=*n*<=β€<=105,<=0<=β€<=*s*<=β€<=109,<=1<=β€<=*l*<=β€<=105).
The second line contains *n* integers *a**i* separated by spaces (<=-<=109<=β€<=*a**i*<=β€<=109). | Output the minimal number of strip pieces.
If there are no ways to split the strip, output -1. | [
"7 2 2\n1 3 1 2 4 1 2\n",
"7 2 2\n1 100 1 100 1 100 1\n"
] | [
"3\n",
"-1\n"
] | For the first sample, we can split the strip into 3 pieces: [1,β3,β1],β[2,β4],β[1,β2].
For the second sample, we can't let 1 and 100 be on the same piece, so no solution exists. | [
{
"input": "7 2 2\n1 3 1 2 4 1 2",
"output": "3"
},
{
"input": "7 2 2\n1 100 1 100 1 100 1",
"output": "-1"
},
{
"input": "1 0 1\n0",
"output": "1"
},
{
"input": "6 565 2\n31 76 162 -182 -251 214",
"output": "1"
},
{
"input": "1 0 1\n0",
"output": "1"
},
{... | 62 | 0 | 0 | 40,499 | |
267 | Dominoes | [
"dfs and similar",
"graphs"
] | null | null | You have a set of dominoes. Each domino is a rectangular tile with a line dividing its face into two square ends. Can you put all dominoes in a line one by one from left to right so that any two dominoes touched with the sides that had the same number of points? You can rotate the dominoes, changing the left and the right side (domino "1-4" turns into "4-1"). | The first line contains number *n* (1<=<=β€<=<=*n*<=<=β€<=<=100). Next *n* lines contains the dominoes. Each of these lines contains two numbers β the number of points (spots) on the left and the right half, correspondingly. The numbers of points (spots) are non-negative integers from 0 to 6. | Print "No solution", if it is impossible to arrange the dominoes in the required manner. If the solution exists, then describe any way to arrange the dominoes. You put the dominoes from left to right. In each of *n* lines print the index of the domino to put in the corresponding position and then, after a space, character "+" (if you don't need to turn the domino) or "β" (if you need to turn it). | [
"5\n1 2\n2 4\n2 4\n6 4\n2 1\n"
] | [
"2 -\n1 -\n5 -\n3 +\n4 -\n"
] | none | [
{
"input": "5\n1 2\n2 4\n2 4\n6 4\n2 1",
"output": "2 -\n1 -\n5 -\n3 +\n4 -"
},
{
"input": "1\n0 0",
"output": "1 +"
},
{
"input": "1\n5 5",
"output": "1 +"
},
{
"input": "5\n0 0\n0 0\n0 0\n0 0\n0 0",
"output": "1 +\n2 +\n3 +\n4 +\n5 +"
},
{
"input": "4\n0 0\n0 0\... | 30 | 0 | -1 | 40,524 | |
717 | Underfail | [
"flows"
] | null | null | You have recently fallen through a hole and, after several hours of unconsciousness, have realized you are in an underground city. On one of your regular, daily walks through the unknown, you have encountered two unusually looking skeletons called Sanz and Pβpairus, who decided to accompany you and give you some puzzles for seemingly unknown reasons.
One day, Sanz has created a crossword for you. Not any kind of crossword, but a 1D crossword! You are given *m* words and a string of length *n*. You are also given an array *p*, which designates how much each word is worthΒ β the *i*-th word is worth *p**i* points. Whenever you find one of the *m* words in the string, you are given the corresponding number of points. Each position in the crossword can be used at most *x* times. A certain word can be counted at different places, but you cannot count the same appearance of a word multiple times. If a word is a substring of another word, you can count them both (presuming you havenβt used the positions more than *x* times).
In order to solve the puzzle, you need to tell Sanz whatβs the maximum achievable number of points in the crossword. There is no need to cover all postions, just get the maximal score! Crossword and words contain only lowercase English letters. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=500)Β β the length of the crossword. The second line contains the crossword string. The third line contains a single integer *m* (1<=β€<=*m*<=β€<=100)Β β the number of given words, and next *m* lines contain description of words: each line will have a string representing a non-empty word (its length doesn't exceed the length of the crossword) and integer *p**i* (0<=β€<=*p**i*<=β€<=100). Last line of the input will contain *x* (1<=β€<=*x*<=β€<=100)Β β maximum number of times a position in crossword can be used. | Output single integerΒ β maximum number of points you can get. | [
"6\nabacba\n2\naba 6\nba 3\n3\n"
] | [
"12\n"
] | For example, with the string "abacba", words "aba" (6 points) and "ba" (3 points), and *x*β=β3, you can get at most 12 points - the word "aba" appears once ("abacba"), while "ba" appears two times ("abacba"). Note that for *x*β=β1, you could get at most 9 points, since you wouldnβt be able to count both "aba" and the first appearance of "ba". | [
{
"input": "6\nabacba\n2\naba 6\nba 3\n3",
"output": "12"
},
{
"input": "6\nabacba\n2\naba 6\nba 3\n1",
"output": "9"
},
{
"input": "6\nabacba\n5\naba 6\nba 3\nbac 4\ncb 3\nc 6\n2",
"output": "21"
},
{
"input": "6\nabacba\n5\naba 6\nba 3\nbac 4\ncb 3\nc 6\n1",
"output": "... | 30 | 0 | 0 | 40,561 | |
245 | Suggested Friends | [
"brute force",
"graphs"
] | null | null | Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion.
Let's say that all friendship relationships in a social network are given as *m* username pairs *a**i*,<=*b**i* (*a**i*<=β <=*b**i*). Each pair *a**i*,<=*b**i* means that users *a**i* and *b**i* are friends. Friendship is symmetric, that is, if *a**i* is friends with *b**i*, then *b**i* is also friends with *a**i*. User *y* is a suggested friend for user *x*, if the following conditions are met:
1. *x*<=β <=*y*; 1. *x* and *y* aren't friends; 1. among all network users who meet the first two conditions, user *y* has most of all common friends with user *x*. User *z* is a common friend of user *x* and user *y* (*z*<=β <=*x*,<=*z*<=β <=*y*), if *x* and *z* are friends, and *y* and *z* are also friends.
Your task is to help Polycarpus to implement a mechanism for determining suggested friends. | The first line contains a single integer *m* (1<=β€<=*m*<=β€<=5000) β the number of pairs of friends in the social network. Next *m* lines contain pairs of names of the users who are friends with each other. The *i*-th line contains two space-separated names *a**i* and *b**i* (*a**i*<=β <=*b**i*). The users' names are non-empty and consist of at most 20 uppercase and lowercase English letters.
It is guaranteed that each pair of friends occurs only once in the input. For example, the input can't contain *x*, *y* and *y*, *x* at the same time. It is guaranteed that distinct users have distinct names. It is guaranteed that each social network user has at least one friend. The last thing guarantees that each username occurs at least once in the input. | In the first line print a single integer *n* β the number of network users. In next *n* lines print the number of suggested friends for each user. In the *i*-th line print the name of the user *c**i* and the number of his suggested friends *d**i* after a space.
You can print information about the users in any order. | [
"5\nMike Gerald\nKate Mike\nKate Tank\nGerald Tank\nGerald David\n",
"4\nvalera vanya\nvalera edik\npasha valera\nigor valera\n"
] | [
"5\nMike 1\nGerald 1\nKate 1\nTank 1\nDavid 2\n",
"5\nvalera 0\nvanya 3\nedik 3\npasha 3\nigor 3\n"
] | In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That's why David's suggested friends are users Mike and Tank. | [] | 92 | 0 | 0 | 40,591 | |
313 | Ilya and Roads | [
"dp"
] | null | null | Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as *n* holes in a row. We will consider the holes numbered from 1 to *n*, from left to right.
Ilya is really keep on helping his city. So, he wants to fix at least *k* holes (perharps he can fix more) on a single ZooVille road.
The city has *m* building companies, the *i*-th company needs *c**i* money units to fix a road segment containing holes with numbers of at least *l**i* and at most *r**i*. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment.
Determine the minimum money Ilya will need to fix at least *k* holes. | The first line contains three integers *n*,<=*m*,<=*k* (1<=β€<=*n*<=β€<=300,<=1<=β€<=*m*<=β€<=105,<=1<=β€<=*k*<=β€<=*n*). The next *m* lines contain the companies' description. The *i*-th line contains three integers *l**i*,<=*r**i*,<=*c**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*,<=1<=β€<=*c**i*<=β€<=109). | Print a single integer β the minimum money Ilya needs to fix at least *k* holes.
If it is impossible to fix at least *k* holes, print -1.
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. | [
"10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6\n",
"10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13\n",
"10 1 9\n5 10 14\n"
] | [
"17\n",
"2\n",
"-1\n"
] | none | [
{
"input": "10 4 6\n7 9 11\n6 9 13\n7 7 7\n3 5 6",
"output": "17"
},
{
"input": "10 7 1\n3 4 15\n8 9 8\n5 6 8\n9 10 6\n1 4 2\n1 4 10\n8 10 13",
"output": "2"
},
{
"input": "10 1 9\n5 10 14",
"output": "-1"
},
{
"input": "10 6 9\n6 8 7\n2 8 11\n2 6 10\n8 10 9\n2 5 8\n2 3 8",
... | 62 | 0 | 0 | 40,715 | |
863 | Yet Another Array Queries Problem | [
"data structures",
"implementation"
] | null | null | You are given an array *a* of size *n*, and *q* queries to it. There are queries of two types:
- 1 *l**i* *r**i* β perform a cyclic shift of the segment [*l**i*,<=*r**i*] to the right. That is, for every *x* such that *l**i*<=β€<=*x*<=<<=*r**i* new value of *a**x*<=+<=1 becomes equal to old value of *a**x*, and new value of *a**l**i* becomes equal to old value of *a**r**i*; - 2 *l**i* *r**i* β reverse the segment [*l**i*,<=*r**i*].
There are *m* important indices in the array *b*1, *b*2, ..., *b**m*. For each *i* such that 1<=β€<=*i*<=β€<=*m* you have to output the number that will have index *b**i* in the array after all queries are performed. | The first line contains three integer numbers *n*, *q* and *m* (1<=β€<=*n*,<=*q*<=β€<=2Β·105, 1<=β€<=*m*<=β€<=100).
The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109).
Then *q* lines follow. *i*-th of them contains three integer numbers *t**i*, *l**i*, *r**i*, where *t**i* is the type of *i*-th query, and [*l**i*,<=*r**i*] is the segment where this query is performed (1<=β€<=*t**i*<=β€<=2, 1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*).
The last line contains *m* integer numbers *b*1, *b*2, ..., *b**m* (1<=β€<=*b**i*<=β€<=*n*) β important indices of the array. | Print *m* numbers, *i*-th of which is equal to the number at index *b**i* after all queries are done. | [
"6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3\n"
] | [
"3 3 1 5 2 \n"
] | none | [
{
"input": "6 3 5\n1 2 3 4 5 6\n2 1 3\n2 3 6\n1 1 6\n2 2 1 5 3",
"output": "3 3 1 5 2 "
},
{
"input": "5 2 5\n64 3 4 665 2\n1 1 3\n2 1 5\n1 2 3 4 5",
"output": "2 665 3 64 4 "
},
{
"input": "1 1 1\n474812122\n2 1 1\n1",
"output": "474812122 "
}
] | 2,000 | 28,364,800 | 0 | 40,851 | |
54 | Cutting Jigsaw Puzzle | [
"hashing",
"implementation"
] | B. Cutting Jigsaw Puzzle | 2 | 256 | The Hedgehog recently remembered one of his favorite childhood activities, β solving puzzles, and got into it with new vigor. He would sit day in, day out with his friend buried into thousands of tiny pieces of the picture, looking for the required items one by one.
Soon the Hedgehog came up with a brilliant idea: instead of buying ready-made puzzles, one can take his own large piece of paper with some picture and cut it into many small rectangular pieces, then mix them and solve the resulting puzzle, trying to piece together the picture. The resulting task is even more challenging than the classic puzzle: now all the fragments have the same rectangular shape, and one can assemble the puzzle only relying on the picture drawn on the pieces.
All puzzle pieces turn out to be of the same size *X*<=Γ<=*Y*, because the picture is cut first by horizontal cuts with the pitch of *X*, then with vertical cuts with the pitch of *Y*. If we denote the initial size of the picture as *A*<=Γ<=*B*, then *A* must be divisible by *X* and *B* must be divisible by *Y* (*X* and *Y* are integer numbers).
However, not every such cutting of the picture will result in a good puzzle. The Hedgehog finds a puzzle good if no two pieces in it are the same (It is allowed to rotate the pieces when comparing them, but it is forbidden to turn them over).
Your task is to count for a given picture the number of good puzzles that you can make from it, and also to find the puzzle with the minimal piece size. | The first line contains two numbers *A* and *B* which are the sizes of the picture. They are positive integers not exceeding 20.
Then follow *A* lines containing *B* symbols each, describing the actual picture. The lines only contain uppercase English letters. | In the first line print the number of possible good puzzles (in other words, the number of pairs (*X*,<=*Y*) such that the puzzle with the corresponding element sizes will be good). This number should always be positive, because the whole picture is a good puzzle itself.
In the second line print two numbers β the sizes *X* and *Y* of the smallest possible element among all good puzzles. The comparison is made firstly by the area *XY* of one element and secondly β by the length *X*. | [
"2 4\nABDC\nABDC\n",
"2 6\nABCCBA\nABCCBA\n"
] | [
"3\n2 1\n",
"1\n2 6\n"
] | The picture in the first sample test has the following good puzzles: (2,β1), (2,β2), (2,β4). | [
{
"input": "2 4\nABDC\nABDC",
"output": "3\n2 1"
},
{
"input": "2 6\nABCCBA\nABCCBA",
"output": "1\n2 6"
},
{
"input": "2 2\nAB\nCD",
"output": "4\n1 1"
},
{
"input": "4 6\nABABAC\nBABABC\nABABAC\nCCCCCA",
"output": "4\n2 3"
},
{
"input": "1 12\nABAAADCAAABX",
... | 92 | 0 | 0 | 41,073 |
83 | Doctor | [
"binary search",
"math",
"sortings"
] | B. Doctor | 2 | 256 | There are *n* animals in the queue to Dr. Dolittle. When an animal comes into the office, the doctor examines him, gives prescriptions, appoints tests and may appoint extra examination. Doc knows all the forest animals perfectly well and therefore knows exactly that the animal number *i* in the queue will have to visit his office exactly *a**i* times. We will assume that an examination takes much more time than making tests and other extra procedures, and therefore we will assume that once an animal leaves the room, it immediately gets to the end of the queue to the doctor. Of course, if the animal has visited the doctor as many times as necessary, then it doesn't have to stand at the end of the queue and it immediately goes home.
Doctor plans to go home after receiving *k* animals, and therefore what the queue will look like at that moment is important for him. Since the doctor works long hours and she can't get distracted like that after all, she asked you to figure it out. | The first line of input data contains two space-separated integers *n* and *k* (1<=β€<=*n*<=β€<=105, 0<=β€<=*k*<=β€<=1014). In the second line are given space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109).
Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is recommended to use cin, cout streams (you can also use the %I64d specificator). | If the doctor will overall carry out less than *k* examinations, print a single number "-1" (without quotes). Otherwise, print the sequence of numbers β number of animals in the order in which they stand in the queue.
Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted. | [
"3 3\n1 2 1\n",
"4 10\n3 3 2 1\n",
"7 10\n1 3 3 1 2 3 1\n"
] | [
"2 ",
"-1\n",
"6 2 3 "
] | In the first sample test:
- Before examination: {1,β2,β3} - After the first examination: {2,β3} - After the second examination: {3,β2} - After the third examination: {2}
In the second sample test:
- Before examination: {1,β2,β3,β4,β5,β6,β7} - After the first examination: {2,β3,β4,β5,β6,β7} - After the second examination: {3,β4,β5,β6,β7,β2} - After the third examination: {4,β5,β6,β7,β2,β3} - After the fourth examination: {5,β6,β7,β2,β3} - After the fifth examination: {6,β7,β2,β3,β5} - After the sixth examination: {7,β2,β3,β5,β6} - After the seventh examination: {2,β3,β5,β6} - After the eighth examination: {3,β5,β6,β2} - After the ninth examination: {5,β6,β2,β3} - After the tenth examination: {6,β2,β3} | [
{
"input": "3 3\n1 2 1",
"output": "2 "
},
{
"input": "4 10\n3 3 2 1",
"output": "-1"
},
{
"input": "7 10\n1 3 3 1 2 3 1",
"output": "6 2 3 "
},
{
"input": "1 0\n1",
"output": "1 "
},
{
"input": "6 101\n9 78 54 62 2 91",
"output": "4 6 2 3 "
},
{
"inpu... | 2,000 | 12,595,200 | 0 | 41,080 |
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 | [] | 60 | 0 | 0 | 41,096 | |
557 | Vitaly and Cycle | [
"combinatorics",
"dfs and similar",
"graphs",
"math"
] | null | null | After Vitaly was expelled from the university, he became interested in the graph theory.
Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.
Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of *n* vertices and *m* edges, not necessarily connected, without parallel edges and loops. You need to find *t* β the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find *w* β the number of ways to add *t* edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.
Two ways to add edges to the graph are considered equal if they have the same sets of added edges.
Since Vitaly does not study at the university, he asked you to help him with this task. | The first line of the input contains two integers *n* and *m* (Β βΒ the number of vertices in the graph and the number of edges in the graph.
Next *m* lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers *a**i*, *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*)Β βΒ the vertices that are connected by the *i*-th edge. All numbers in the lines are separated by a single space.
It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected. | Print in the first line of the output two space-separated integers *t* and *w*Β βΒ the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this. | [
"4 4\n1 2\n1 3\n4 2\n4 3\n",
"3 3\n1 2\n2 3\n3 1\n",
"3 0\n"
] | [
"1 2\n",
"0 1\n",
"3 1\n"
] | The simple cycle is a cycle that doesn't contain any vertex twice. | [
{
"input": "4 4\n1 2\n1 3\n4 2\n4 3",
"output": "1 2"
},
{
"input": "3 3\n1 2\n2 3\n3 1",
"output": "0 1"
},
{
"input": "3 0",
"output": "3 1"
},
{
"input": "6 3\n1 2\n4 3\n6 5",
"output": "2 12"
},
{
"input": "100000 0",
"output": "3 166661666700000"
},
{... | 249 | 2,252,800 | 0 | 41,103 | |
172 | Calendar Reform | [
"*special",
"number theory"
] | null | null | Reforms have started in Berland again! At this time, the Parliament is discussing the reform of the calendar. To make the lives of citizens of Berland more varied, it was decided to change the calendar. As more and more people are complaining that "the years fly by...", it was decided that starting from the next year the number of days per year will begin to grow. So the coming year will have exactly *a* days, the next after coming year will have *a*<=+<=1 days, the next one will have *a*<=+<=2 days and so on. This schedule is planned for the coming *n* years (in the *n*-th year the length of the year will be equal *a*<=+<=*n*<=-<=1 day).
No one has yet decided what will become of months. An MP Palevny made the following proposal.
- The calendar for each month is comfortable to be printed on a square sheet of paper. We are proposed to make the number of days in each month be the square of some integer. The number of days per month should be the same for each month of any year, but may be different for different years. - The number of days in each year must be divisible by the number of days per month in this year. This rule ensures that the number of months in each year is an integer. - The number of days per month for each year must be chosen so as to save the maximum amount of paper to print the calendars. In other words, the number of days per month should be as much as possible.
These rules provide an unambiguous method for choosing the number of days in each month for any given year length. For example, according to Palevny's proposition, a year that consists of 108 days will have three months, 36 days each. The year that consists of 99 days will have 11 months, 9 days each, and a year of 365 days will have 365 months, one day each.
The proposal provoked heated discussion in the community, the famous mathematician Perelmanov quickly calculated that if the proposal is supported, then in a period of *n* years, beginning with the year that has *a* days, the country will spend *p* sheets of paper to print a set of calendars for these years. Perelmanov's calculations take into account the fact that the set will contain one calendar for each year and each month will be printed on a separate sheet.
Repeat Perelmanov's achievement and print the required number *p*. You are given positive integers *a* and *n*. Perelmanov warns you that your program should not work longer than four seconds at the maximum test. | The only input line contains a pair of integers *a*, *n* (1<=β€<=*a*,<=*n*<=β€<=107; *a*<=+<=*n*<=-<=1<=β€<=107). | Print the required number *p*.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use cin, cout streams or the %I64d specifier. | [
"25 3\n",
"50 5\n"
] | [
"30\n",
"125\n"
] | A note to the first sample test. A year of 25 days will consist of one month containing 25 days. A year of 26 days will consist of 26 months, one day each. A year of 27 days will have three months, 9 days each. | [
{
"input": "25 3",
"output": "30"
},
{
"input": "50 5",
"output": "125"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "1 2",
"output": "3"
},
{
"input": "1 10",
"output": "38"
},
{
"input": "1 5000000",
"output": "8224640917276"
},
{
"in... | 77 | 0 | -1 | 41,108 | |
925 | May Holidays | [
"data structures",
"trees"
] | null | null | It's May in Flatland, and there are $m$ days in this month. Despite the fact that May Holidays are canceled long time ago, employees of some software company still have a habit of taking short or long vacations in May.
Of course, not all managers of the company like this. There are $n$ employees in the company that form a tree-like structure of subordination: each employee has a unique integer id $i$ between $1$ and $n$, and each employee with id $i$ (except the head manager whose id is 1) has exactly one direct manager with id $p_i$. The structure of subordination is not cyclic, i.e. if we start moving from any employee to his direct manager, then we will eventually reach the head manager. We define that an employee $u$ is a subordinate of an employee $v$, if $v$ is a direct manager of $u$, or the direct manager of $u$ is a subordinate of $v$. Let $s_i$ be the number of subordinates the $i$-th employee has (for example, $s_1 = n - 1$, because all employees except himself are subordinates of the head manager).
Each employee $i$ has a bearing limit of $t_i$, which is an integer between $0$ and $s_i$. It denotes the maximum number of the subordinates of the $i$-th employee being on vacation at the same moment that he can bear. If at some moment strictly more than $t_i$ subordinates of the $i$-th employee are on vacation, and the $i$-th employee himself is not on a vacation, he becomes displeased.
In each of the $m$ days of May exactly one event of the following two types happens: either one employee leaves on a vacation at the beginning of the day, or one employee returns from a vacation in the beginning of the day. You know the sequence of events in the following $m$ days. Your task is to compute for each of the $m$ days the number of displeased employees on that day. | The first line contains two integers $n$ and $m$ ($2 \leq n, m \leq 10^5$) β the number of employees in the company and the number of days in May.
The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i \leq n$), denoting the direct managers of employees.
The third line contains $n$ integers $t_1, t_2, \ldots, t_n$ ($0 \leq t_i \leq s_i$), denoting the bearing limits of empoyees.
The fourth line contains $m$ integers $q_1, q_2, \ldots, q_m$ ($1 \leq |q_i| \leq n$, $q_i \ne 0$), denoting the events. If $q_i$ is positive, then the employee with id $q_i$ leaves for a vacation starting from this day, if $q_i$ is negative, then the employee $-q_i$ returns from a vacation starting from this day. In the beginning of May no employee is on vacation. It is guaranteed that if some employee leaves for a vacation, he is not on a vacation at the moment and vice versa. | Print a sequence of $m$ integers $a_1, a_2, \ldots, a_m$, where $a_i$ is the number of displeased employees on the $i$-th day. | [
"7 8\n4 5 1 1 5 5\n0 0 0 1 2 0 0\n2 6 3 7 -2 4 -3 1\n",
"5 6\n1 2 3 4\n4 0 0 1 0\n1 5 2 3 -5 -1\n"
] | [
"1 1 1 2 2 2 1 0\n",
"0 2 1 0 0 0\n"
] | In the first sample test after employee with id 2 leaves for a vacation at the first day, the head manager with id 1 becomes displeased as he does not want any of his subordinates to go for a vacation. At the fourth day employee with id 5 becomes displeased as his last remaining employee with id 7 leaves for a vacation. At the fifth day employee with id 2 returns from the vacation, but it does not affect the number of displeased employees as the employees 5 and 1 are still displeased. At the sixth day employee with id 3 returns back from the vacation, preventing the employee with id 5 from being displeased and at the last day the head manager with id 1 leaves for a vacation, leaving the company without the displeased people at all. | [] | 46 | 0 | 0 | 41,120 | |
173 | Deputies | [
"constructive algorithms",
"graphs",
"greedy",
"implementation"
] | null | null | The Trinitarian kingdom has exactly *n*<==<=3*k* cities. All of them are located on the shores of river Trissisipi, which flows through the whole kingdom. Some of the cities are located on one side of the river, and all the rest are on the other side.
Some cities are connected by bridges built between them. Each bridge connects two cities that are located on the opposite sides of the river. Between any two cities exists no more than one bridge.
The recently inaugurated King Tristan the Third is busy distributing his deputies among cities. In total there are *k* deputies and the king wants to commission each of them to control exactly three cities. However, no deputy can be entrusted to manage the cities, which are connected by a bridge β the deputy can set a too high fee for travelling over the bridge to benefit his pocket, which is bad for the reputation of the king.
Help King Tristan the Third distribute the deputies between the cities, if it is possible. | The first line contains two integers *n* and *m* β the number of cities and bridges (3<=β€<=*n*<=<<=105, *n*<==<=3*k*, 0<=β€<=*m*<=β€<=105). Next *m* lines describe the bridges. The *i*-th line contains two integers *a**i* and *b**i* β the numbers of cities that are connected by the *i*-th bridge (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*, 1<=β€<=*i*<=β€<=*m*).
It is guaranteed that no bridge connects a city with itself and that any two cities are connected with no more than one bridge. | If distributing the deputies in the required manner is impossible, print in a single line "NO" (without the quotes).
Otherwise, in the first line print "YES" (without the quotes), and in the second line print which deputy should be put in charge of each city. The *i*-th number should represent the number of the deputy (from 1 to *k*), who should be in charge of city numbered *i*-th in the input β overall there should be *n* numbers.
If there are multiple solutions, print any of them. | [
"6 6\n1 2\n4 1\n3 5\n6 5\n2 6\n4 6\n",
"3 1\n1 2\n"
] | [
"YES\n1 2 1 2 2 1 ",
"NO"
] | none | [] | 62 | 102,400 | 0 | 41,215 | |
732 | Cormen --- The Best Friend Of a Man | [
"dp",
"greedy"
] | null | null | Recently a dog was bought for Polycarp. The dog's name is Cormen. Now Polycarp has a lot of troubles. For example, Cormen likes going for a walk.
Empirically Polycarp learned that the dog needs at least *k* walks for any two consecutive days in order to feel good. For example, if *k*<==<=5 and yesterday Polycarp went for a walk with Cormen 2 times, today he has to go for a walk at least 3 times.
Polycarp analysed all his affairs over the next *n* days and made a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the number of times Polycarp will walk with the dog on the *i*-th day while doing all his affairs (for example, he has to go to a shop, throw out the trash, etc.).
Help Polycarp determine the minimum number of walks he needs to do additionaly in the next *n* days so that Cormen will feel good during all the *n* days. You can assume that on the day before the first day and on the day after the *n*-th day Polycarp will go for a walk with Cormen exactly *k* times.
Write a program that will find the minumum number of additional walks and the appropriate scheduleΒ β the sequence of integers *b*1,<=*b*2,<=...,<=*b**n* (*b**i*<=β₯<=*a**i*), where *b**i* means the total number of walks with the dog on the *i*-th day. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=500)Β β the number of days and the minimum number of walks with Cormen for any two consecutive days.
The second line contains integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=500)Β β the number of walks with Cormen on the *i*-th day which Polycarp has already planned. | In the first line print the smallest number of additional walks that Polycarp should do during the next *n* days so that Cormen will feel good during all days.
In the second line print *n* integers *b*1,<=*b*2,<=...,<=*b**n*, where *b**i*Β β the total number of walks on the *i*-th day according to the found solutions (*a**i*<=β€<=*b**i* for all *i* from 1 to *n*). If there are multiple solutions, print any of them. | [
"3 5\n2 0 1\n",
"3 1\n0 0 0\n",
"4 6\n2 4 3 5\n"
] | [
"4\n2 3 2\n",
"1\n0 1 0\n",
"0\n2 4 3 5\n"
] | none | [
{
"input": "3 5\n2 0 1",
"output": "4\n2 3 2"
},
{
"input": "3 1\n0 0 0",
"output": "1\n0 1 0"
},
{
"input": "4 6\n2 4 3 5",
"output": "0\n2 4 3 5"
},
{
"input": "5 1\n0 0 0 0 1",
"output": "2\n0 1 0 1 1"
},
{
"input": "10 500\n164 44 238 205 373 249 87 30 239 90"... | 46 | 0 | 0 | 41,235 | |
258 | Little Elephant and Elections | [
"brute force",
"combinatorics",
"dp"
] | null | null | There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.
Political parties find their number in the ballot highly important. Overall there are *m* possible numbers: 1,<=2,<=...,<=*m*. Each of these 7 parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.
The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties.
Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109<=+<=7). | A single line contains a single positive integer *m* (7<=β€<=*m*<=β€<=109) β the number of possible numbers in the ballot. | In a single line print a single integer β the answer to the problem modulo 1000000007 (109<=+<=7). | [
"7\n",
"8\n"
] | [
"0\n",
"1440\n"
] | none | [
{
"input": "7",
"output": "0"
},
{
"input": "8",
"output": "1440"
},
{
"input": "47",
"output": "907362803"
},
{
"input": "10",
"output": "40320"
},
{
"input": "9",
"output": "10080"
},
{
"input": "11",
"output": "120960"
},
{
"input": "25"... | 60 | 0 | 0 | 41,238 | |
380 | Sereja and Tree | [
"graphs",
"implementation"
] | null | null | Sereja adores trees. Today he came up with a revolutionary new type of binary root trees.
His new tree consists of *n* levels, each vertex is indexed by two integers: the number of the level and the number of the vertex on the current level. The tree root is at level 1, its index is (1,<=1). Here is a pseudo code of tree construction.
After the pseudo code is run, cell cnt[level] contains the number of vertices on level *level*. Cell left[level][position] contains the number of the vertex on the level *level*<=+<=1, which is the left child of the vertex with index (*level*,<=*position*), or it contains -1, if the vertex doesn't have a left child. Similarly, cell right[level][position] is responsible for the right child. You can see how the tree with *n*<==<=4 looks like in the notes.
Serja loves to make things complicated, so he first made a tree and then added an empty set *A*(*level*,<=*position*) for each vertex. Then Sereja executes *m* operations. Each operation is of one of the two following types:
- The format of the operation is "1 *t* *l* *r* *x*". For all vertices *level*,<=*position* (*level*<==<=*t*;Β *l*<=β€<=*position*<=β€<=*r*) add value *x* to set *A*(*level*,<=*position*). - The format of the operation is "2 *t* *v*". For vertex *level*,<=*position* (*level*<==<=*t*,<=*position*<==<=*v*), find the union of all sets of vertices that are in the subtree of vertex (*level*,<=*position*). Print the size of the union of these sets.
Help Sereja execute the operations. In this problem a set contains only distinct values like std::set in C++. | The first line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=7000).
Next *m* lines contain the descriptions of the operations. The operation of the first type is given by five integers: 1 *t* *l* *r* *x* (1<=β€<=*t*<=β€<=*n*;Β 1<=β€<=*l*<=β€<=*r*<=β€<=*cnt*[*t*];Β 1<=β€<=*x*<=β€<=106). The operation of the second type is given by three integers: 2 *t* *v* (1<=β€<=*t*<=β€<=*n*;Β 1<=β€<=*v*<=β€<=*cnt*[*t*]). | For each operation of the second type, print the answer on a single line. | [
"4 5\n1 4 4 7 1\n1 3 1 2 2\n2 1 1\n2 4 1\n2 3 3\n"
] | [
"2\n0\n1\n"
] | You can find the definitions that are used while working with root trees by this link: http://en.wikipedia.org/wiki/Tree_(graph_theory)
You can see an example of a constructed tree at *n*β=β4 below. | [] | 30 | 0 | -1 | 41,329 | |
0 | none | [
"none"
] | null | null | While Duff was resting in the beach, she accidentally found a strange array *b*0,<=*b*1,<=...,<=*b**l*<=-<=1 consisting of *l* positive integers. This array was strange because it was extremely long, but there was another (maybe shorter) array, *a*0,<=...,<=*a**n*<=-<=1 that *b* can be build from *a* with formula: *b**i*<==<=*a**i* *mod* *n* where *a* *mod* *b* denoted the remainder of dividing *a* by *b*.
Duff is so curious, she wants to know the number of subsequences of *b* like *b**i*1,<=*b**i*2,<=...,<=*b**i**x* (0<=β€<=*i*1<=<<=*i*2<=<<=...<=<<=*i**x*<=<<=*l*), such that:
- 1<=β€<=*x*<=β€<=*k* - For each 1<=β€<=*j*<=β€<=*x*<=-<=1, - For each 1<=β€<=*j*<=β€<=*x*<=-<=1, *b**i**j*<=β€<=*b**i**j*<=+<=1. i.e this subsequence is non-decreasing.
Since this number can be very large, she want to know it modulo 109<=+<=7.
Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number. | The first line of input contains three integers, *n*,<=*l* and *k* (1<=β€<=*n*,<=*k*, *n*<=Γ<=*k*<=β€<=106 and 1<=β€<=*l*<=β€<=1018).
The second line contains *n* space separated integers, *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (1<=β€<=*a**i*<=β€<=109 for each 0<=β€<=*i*<=β€<=*n*<=-<=1). | Print the answer modulo 1<=000<=000<=007 in one line. | [
"3 5 3\n5 9 1\n",
"5 10 3\n1 2 3 4 5\n"
] | [
"10\n",
"25\n"
] | In the first sample case, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d9d8fe92937aeef2bcddb9d213e5587f0f950087.png" style="max-width: 100.0%;max-height: 100.0%;"/>. So all such sequences are: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/54d7dd513b50dae3415992b37dcf6f8ff2f24a73.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fcd7414fd5b0b1459adc9dafca95e6d5a23b0d7d.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f6d2e0d0317c10a3c5213068fb1d1e1f53ff26eb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8f6627cad3dd113f47d84a1226f30fe423939757.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c6db71d99107f752b5caa1ba7e44af1d2302e5db.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a06ffb7bf9126fd634a790703c0bbfda00c60aa1.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b086d545b6c80640ea8558920709e3c51ba3bf43.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/661b7811fccac48a9e96733bc99c3dfec93ad6d7.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8756896f0d00888771e69f1a9ccddeee19d4a9ab.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e771f8119b1b8218c875759aa03e38312514c9a2.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3 5 3\n5 9 1",
"output": "10"
},
{
"input": "5 10 3\n1 2 3 4 5",
"output": "25"
},
{
"input": "1 1000000000000000000 1\n508953607",
"output": "49"
},
{
"input": "13 1984343432234 32\n347580985 506695806 506695806 42598441 347580985 720568974 208035957 385072757 42... | 2,000 | 26,316,800 | 0 | 41,407 | |
471 | MUH and House of Cards | [
"binary search",
"brute force",
"greedy",
"math"
] | null | null | Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of *n* playing cards. Let's describe the house they want to make:
1. The house consists of some non-zero number of floors. 1. Each floor consists of a non-zero number of rooms and the ceiling. A room is two cards that are leaned towards each other. The rooms are made in a row, each two adjoining rooms share a ceiling made by another card. 1. Each floor besides for the lowest one should contain less rooms than the floor below.
Please note that the house may end by the floor with more than one room, and in this case they also must be covered by the ceiling. Also, the number of rooms on the adjoining floors doesn't have to differ by one, the difference may be more.
While bears are practicing to put cards, Horace tries to figure out how many floors their house should consist of. The height of the house is the number of floors in it. It is possible that you can make a lot of different houses of different heights out of *n* cards. It seems that the elephant cannot solve this problem and he asks you to count the number of the distinct heights of the houses that they can make using exactly *n* cards. | The single line contains integer *n* (1<=β€<=*n*<=β€<=1012) β the number of cards. | Print the number of distinct heights that the houses made of exactly *n* cards can have. | [
"13\n",
"6\n"
] | [
"1",
"0"
] | In the first sample you can build only these two houses (remember, you must use all the cards):
Thus, 13 cards are enough only for two floor houses, so the answer is 1.
The six cards in the second sample are not enough to build any house. | [
{
"input": "13",
"output": "1"
},
{
"input": "6",
"output": "0"
},
{
"input": "26",
"output": "2"
},
{
"input": "1000000000000",
"output": "272165"
},
{
"input": "571684826707",
"output": "205784"
},
{
"input": "178573947413",
"output": "115012"
... | 109 | 0 | 3 | 41,485 | |
407 | Curious Array | [
"brute force",
"combinatorics",
"implementation",
"math"
] | null | null | You've got an array consisting of *n* integers: *a*[1],<=*a*[2],<=...,<=*a*[*n*]. Moreover, there are *m* queries, each query can be described by three integers *l**i*,<=*r**i*,<=*k**i*. Query *l**i*,<=*r**i*,<=*k**i* means that we should add to each element *a*[*j*], where *l**i*<=β€<=*j*<=β€<=*r**i*.
Record means the binomial coefficient, or the number of combinations from *y* elements into groups of *x* elements.
You need to fulfil consecutively all queries and then print the final array. | The first line contains integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=105).
The second line contains *n* integers *a*[1],<=*a*[2],<=...,<=*a*[*n*] (0<=β€<=*a**i*<=β€<=109)Β β the initial array.
Next *m* lines contain queries in the format *l**i*,<=*r**i*,<=*k**i*Β β to all elements of the segment *l**i*... *r**i* add number (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*; 0<=β€<=*k*<=β€<=100). | Print *n* integers: the *i*-th number is the value of element *a*[*i*] after all the queries. As the values can be rather large, print them modulo 1000000007 (109<=+<=7). | [
"5 1\n0 0 0 0 0\n1 5 0\n",
"10 2\n1 2 3 4 5 0 0 0 0 0\n1 6 1\n6 10 2\n"
] | [
"1 1 1 1 1\n",
"2 4 6 8 10 7 3 6 10 15\n"
] | none | [
{
"input": "5 1\n0 0 0 0 0\n1 5 0",
"output": "1 1 1 1 1"
},
{
"input": "10 2\n1 2 3 4 5 0 0 0 0 0\n1 6 1\n6 10 2",
"output": "2 4 6 8 10 7 3 6 10 15"
},
{
"input": "5 3\n0 0 0 0 0\n1 5 0\n1 5 1\n1 5 2",
"output": "3 6 10 15 21"
},
{
"input": "10 2\n0 0 0 0 0 0 0 0 0 0\n7 9 4... | 61 | 9,113,600 | -1 | 41,498 | |
164 | Ancient Berland Hieroglyphs | [
"two pointers"
] | null | null | Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them).
Polycarpus wants to save these pictures on his laptop, but the problem is, laptops do not allow to write hieroglyphs circles. So Polycarp had to break each circle and write down all of its hieroglyphs in a clockwise order in one line. A line obtained from the first circle will be called *a*, and the line obtained from the second one will be called *b*.
There are quite many ways to break hieroglyphic circles, so Polycarpus chooses the method, that makes the length of the largest substring of string *a*, which occurs as a subsequence in string *b*, maximum.
Help Polycarpus β find the maximum possible length of the desired substring (subsequence) if the first and the second circles are broken optimally.
The length of string *s* is the number of characters in it. If we denote the length of string *s* as |*s*|, we can write the string as *s*<==<=*s*1*s*2... *s*|*s*|.
A substring of *s* is a non-empty string *x*<==<=*s*[*a*... *b*]<==<=*s**a**s**a*<=+<=1... *s**b* (1<=β€<=*a*<=β€<=*b*<=β€<=|*s*|). For example, "code" and "force" are substrings of "codeforces", while "coders" is not.
A subsequence of *s* is a non-empty string *y*<==<=*s*[*p*1*p*2... *p*|*y*|]<==<=*s**p*1*s**p*2... *s**p*|*y*| (1<=β€<=*p*1<=<<=*p*2<=<<=...<=<<=*p*|*y*|<=β€<=|*s*|). For example, "coders" is a subsequence of "codeforces". | The first line contains two integers *l**a* and *l**b* (1<=β€<=*l**a*,<=*l**b*<=β€<=1000000) β the number of hieroglyphs in the first and second circles, respectively.
Below, due to difficulties with encoding of Berland hieroglyphs, they are given as integers from 1 to 106.
The second line contains *l**a* integers β the hieroglyphs in the first picture, in the clockwise order, starting with one of them.
The third line contains *l**b* integers β the hieroglyphs in the second picture, in the clockwise order, starting with one of them.
It is guaranteed that the first circle doesn't contain a hieroglyph, which occurs twice. The second circle also has this property. | Print a single number β the maximum length of the common substring and subsequence. If at any way of breaking the circles it does not exist, print 0. | [
"5 4\n1 2 3 4 5\n1 3 5 6\n",
"4 6\n1 3 5 2\n1 2 3 4 5 6\n",
"3 3\n1 2 3\n3 2 1\n"
] | [
"2\n",
"3\n",
"2\n"
] | In the first test Polycarpus picks a string that consists of hieroglyphs 5 and 1, and in the second sample β from hieroglyphs 1, 3 and 5. | [] | 31 | 0 | 0 | 41,545 | |
799 | Field expansion | [
"brute force",
"dp",
"meet-in-the-middle"
] | null | null | In one of the games Arkady is fond of the game process happens on a rectangular field. In the game process Arkady can buy extensions for his field, each extension enlarges one of the field sizes in a particular number of times. Formally, there are *n* extensions, the *i*-th of them multiplies the width or the length (by Arkady's choice) by *a**i*. Each extension can't be used more than once, the extensions can be used in any order.
Now Arkady's field has size *h*<=Γ<=*w*. He wants to enlarge it so that it is possible to place a rectangle of size *a*<=Γ<=*b* on it (along the width or along the length, with sides parallel to the field sides). Find the minimum number of extensions needed to reach Arkady's goal. | The first line contains five integers *a*, *b*, *h*, *w* and *n* (1<=β€<=*a*,<=*b*,<=*h*,<=*w*,<=*n*<=β€<=100<=000)Β β the sizes of the rectangle needed to be placed, the initial sizes of the field and the number of available extensions.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (2<=β€<=*a**i*<=β€<=100<=000), where *a**i* equals the integer a side multiplies by when the *i*-th extension is applied. | Print the minimum number of extensions needed to reach Arkady's goal. If it is not possible to place the rectangle on the field with all extensions, print -1. If the rectangle can be placed on the initial field, print 0. | [
"3 3 2 4 4\n2 5 4 10\n",
"3 3 3 3 5\n2 3 5 4 2\n",
"5 5 1 2 3\n2 2 3\n",
"3 4 1 1 3\n2 3 2\n"
] | [
"1\n",
"0\n",
"-1\n",
"3\n"
] | In the first example it is enough to use any of the extensions available. For example, we can enlarge *h* in 5 times using the second extension. Then *h* becomes equal 10 and it is now possible to place the rectangle on the field. | [
{
"input": "3 3 2 4 4\n2 5 4 10",
"output": "1"
},
{
"input": "3 3 3 3 5\n2 3 5 4 2",
"output": "0"
},
{
"input": "5 5 1 2 3\n2 2 3",
"output": "-1"
},
{
"input": "3 4 1 1 3\n2 3 2",
"output": "3"
},
{
"input": "572 540 6 2 12\n2 3 2 2 2 3 3 3 2 2 2 2",
"outpu... | 62 | 1,843,200 | 0 | 41,655 | |
472 | Design Tutorial: Make It Nondeterministic | [
"greedy"
] | null | null | A way to make a new task is to make it nondeterministic or probabilistic. For example, the hard task of Topcoder SRM 595, Constellation, is the probabilistic version of a convex hull.
Let's try to make a new task. Firstly we will use the following task. There are *n* people, sort them by their name. It is just an ordinary sorting problem, but we can make it more interesting by adding nondeterministic element. There are *n* people, each person will use either his/her first name or last name as a handle. Can the lexicographical order of the handles be exactly equal to the given permutation *p*?
More formally, if we denote the handle of the *i*-th person as *h**i*, then the following condition must hold: . | The first line contains an integer *n* (1<=β€<=*n*<=β€<=105) β the number of people.
The next *n* lines each contains two strings. The *i*-th line contains strings *f**i* and *s**i* (1<=β€<=|*f**i*|,<=|*s**i*|<=β€<=50) β the first name and last name of the *i*-th person. Each string consists only of lowercase English letters. All of the given 2*n* strings will be distinct.
The next line contains *n* distinct integers: *p*1,<=*p*2,<=...,<=*p**n* (1<=β€<=*p**i*<=β€<=*n*). | If it is possible, output "YES", otherwise output "NO". | [
"3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3\n",
"3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2\n",
"2\ngalileo galilei\nnicolaus copernicus\n2 1\n",
"10\nrean schwarzer\nfei claussell\nalisa reinford\neliot craig\nlaura arseid\njusis albarea\nmachias regnitz\nsara valestin\nemma... | [
"NO\n",
"YES\n",
"YES\n",
"NO\n",
"YES\n"
] | In example 1 and 2, we have 3 people: tourist, Petr and me (cgy4ever). You can see that whatever handle is chosen, I must be the first, then tourist and Petr must be the last.
In example 3, if Copernicus uses "copernicus" as his handle, everything will be alright. | [
{
"input": "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n1 2 3",
"output": "NO"
},
{
"input": "3\ngennady korotkevich\npetr mitrichev\ngaoyuan chen\n3 1 2",
"output": "YES"
},
{
"input": "2\ngalileo galilei\nnicolaus copernicus\n2 1",
"output": "YES"
},
{
"input": "1... | 61 | 0 | 0 | 41,731 | |
131 | Yet Another Task with Queens | [
"sortings"
] | null | null | A queen is the strongest chess piece. In modern chess the queen can move any number of squares in any horizontal, vertical or diagonal direction (considering that there're no other pieces on its way). The queen combines the options given to the rook and the bishop.
There are *m* queens on a square *n*<=Γ<=*n* chessboard. You know each queen's positions, the *i*-th queen is positioned in the square (*r**i*,<=*c**i*), where *r**i* is the board row number (numbered from the top to the bottom from 1 to *n*), and *c**i* is the board's column number (numbered from the left to the right from 1 to *n*). No two queens share the same position.
For each queen one can count *w* β the number of other queens that the given queen threatens (attacks). For a fixed attack direction only the first queen in this direction is under attack if there are many queens are on the ray of the attack. Obviously, for any queen *w* is between 0 and 8, inclusive.
Print the sequence *t*0,<=*t*1,<=...,<=*t*8, where *t**i* is the number of queens that threaten exactly *i* other queens, i.e. the number of queens that their *w* equals *i*. | The first line of the input contains a pair of integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=105), where *n* is the size of the board and *m* is the number of queens on the board. Then *m* following lines contain positions of the queens, one per line. Each line contains a pair of integers *r**i*,<=*c**i* (1<=β€<=*r**i*,<=*c**i*<=β€<=*n*) β the queen's position. No two queens stand on the same square. | Print the required sequence *t*0,<=*t*1,<=...,<=*t*8, separating the numbers with spaces. | [
"8 4\n4 3\n4 8\n6 5\n1 6\n",
"10 3\n1 1\n1 2\n1 3\n"
] | [
"0 3 0 1 0 0 0 0 0 ",
"0 2 1 0 0 0 0 0 0 "
] | none | [] | 92 | 0 | 0 | 41,798 | |
141 | Hopscotch | [
"geometry",
"math"
] | null | null | So nearly half of the winter is over and Maria is dreaming about summer. She's fed up with skates and sleds, she was dreaming about Hopscotch all night long. It's a very popular children's game. The game field, the court, looks as is shown in the figure (all blocks are square and are numbered from bottom to top, blocks in the same row are numbered from left to right). Let us describe the hopscotch with numbers that denote the number of squares in the row, staring from the lowest one: 1-1-2-1-2-1-2-(1-2)..., where then the period is repeated (1-2).
The coordinate system is defined as shown in the figure. Side of all the squares are equal and have length *a*.
Maria is a very smart and clever girl, and she is concerned with quite serious issues: if she throws a stone into a point with coordinates (*x*,<=*y*), then will she hit some square? If the answer is positive, you are also required to determine the number of the square.
It is believed that the stone has fallen into the square if it is located strictly inside it. In other words a stone that has fallen on the square border is not considered a to hit a square. | The only input line contains three integers: *a*, *x*, *y*, where *a* (1<=β€<=*a*<=β€<=100) is the side of the square, *x* and *y* (<=-<=106<=β€<=*x*<=β€<=106,<=0<=β€<=*y*<=β€<=106) are coordinates of the stone. | Print the number of the square, inside which the stone fell. If the stone is on a border of some stone or outside the court, print "-1" without the quotes. | [
"1 0 0\n",
"3 1 1\n",
"3 0 10\n",
"3 0 7\n",
"3 4 0\n"
] | [
"-1\n",
"1\n",
"5\n",
"-1\n",
"-1\n"
] | none | [
{
"input": "1 0 0",
"output": "-1"
},
{
"input": "3 1 1",
"output": "1"
},
{
"input": "3 0 10",
"output": "5"
},
{
"input": "3 0 7",
"output": "-1"
},
{
"input": "3 4 0",
"output": "-1"
},
{
"input": "9 3 2",
"output": "1"
},
{
"input": "10... | 92 | 0 | 0 | 41,799 | |
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. | [] | 2,000 | 171,110,400 | 0 | 41,878 | |
133 | Unary | [
"implementation"
] | null | null | Unary is a minimalistic Brainfuck dialect in which programs are written using only one token.
Brainfuck programs use 8 commands: "+", "-", "[", "]", "<", ">", "." and "," (their meaning is not important for the purposes of this problem). Unary programs are created from Brainfuck programs using the following algorithm. First, replace each command with a corresponding binary code, using the following conversion table:
- ">" <=β<= 1000, - "<" <=β<= 1001, - "+" <=β<= 1010, - "-" <=β<= 1011, - "." <=β<= 1100, - "," <=β<= 1101, - "[" <=β<= 1110, - "]" <=β<= 1111.
Next, concatenate the resulting binary codes into one binary number in the same order as in the program. Finally, write this number using unary numeral system β this is the Unary program equivalent to the original Brainfuck one.
You are given a Brainfuck program. Your task is to calculate the size of the equivalent Unary program, and print it modulo 1000003 (106<=+<=3). | The input will consist of a single line *p* which gives a Brainfuck program. String *p* will contain between 1 and 100 characters, inclusive. Each character of *p* will be "+", "-", "[", "]", "<", ">", "." or ",". | Output the size of the equivalent Unary program modulo 1000003 (106<=+<=3). | [
",.\n",
"++++[>,.<-]\n"
] | [
"220\n",
"61425\n"
] | To write a number *n* in unary numeral system, one simply has to write 1 *n* times. For example, 5 written in unary system will be 11111.
In the first example replacing Brainfuck commands with binary code will give us 1101 1100. After we concatenate the codes, we'll get 11011100 in binary system, or 220 in decimal. That's exactly the number of tokens in the equivalent Unary program. | [
{
"input": ",.",
"output": "220"
},
{
"input": "++++[>,.<-]",
"output": "61425"
},
{
"input": "[-],<],<<,<[,>,+>[[<>.,[>-[-[<><>><<<<]>,.-].>-[[>+,>,[,-,.-,-[[]>..<>,<[+,-<]-++.<+.]<,[[.<<-><<<],",
"output": "43789"
},
{
"input": "+",
"output": "10"
},
{
"input": ... | 124 | 0 | 3 | 41,883 | |
717 | Cowboy Beblop at his computer | [
"geometry"
] | null | null | Cowboy Beblop is a funny little boy who likes sitting at his computer. He somehow obtained two elastic hoops in the shape of 2D polygons, which are not necessarily convex. Since there's no gravity on his spaceship, the hoops are standing still in the air. Since the hoops are very elastic, Cowboy Beblop can stretch, rotate, translate or shorten their edges as much as he wants.
For both hoops, you are given the number of their vertices, as well as the position of each vertex, defined by the X , Y and Z coordinates. The vertices are given in the order they're connected: the 1stβ―vertex is connected to the 2nd, which is connected to the 3rd, etc., and the last vertex is connected to the first one. Two hoops are connected if it's impossible to pull them to infinity in different directions by manipulating their edges, without having their edges or vertices intersect at any point β just like when two links of a chain are connected. The polygons' edges do not intersect or overlap.
To make things easier, we say that two polygons are well-connected, if the edges of one polygon cross the area of the other polygon in two different directions (from the upper and lower sides of the plane defined by that polygon) a different number of times.
Cowboy Beblop is fascinated with the hoops he has obtained and he would like to know whether they are well-connected or not. Since heβs busy playing with his dog, Zwei, heβd like you to figure it out for him. He promised you some sweets if you help him! | The first line of input contains an integer *n* (3<=β€<=*n*<=β€<=100<=000), which denotes the number of edges of the first polygon. The next N lines each contain the integers *x*, *y* and *z* (<=-<=1<=000<=000<=β€<=*x*,<=*y*,<=*z*<=β€<=1<=000<=000)Β β coordinates of the vertices, in the manner mentioned above. The next line contains an integer *m* (3<=β€<=*m*<=β€<=100<=000) , denoting the number of edges of the second polygon, followed by *m* lines containing the coordinates of the second polygonβs vertices.
It is guaranteed that both polygons are simple (no self-intersections), and in general that the obtained polygonal lines do not intersect each other. Also, you can assume that no 3 consecutive points of a polygon lie on the same line. | Your output should contain only one line, with the words "YES" or "NO", depending on whether the two given polygons are well-connected. | [
"4\n0 0 0\n2 0 0\n2 2 0\n0 2 0\n4\n1 1 -1\n1 1 1\n1 3 1\n1 3 -1\n"
] | [
"YES\n"
] | On the picture below, the two polygons are well-connected, as the edges of the vertical polygon cross the area of the horizontal one exactly once in one direction (for example, from above to below), and zero times in the other (in this case, from below to above). Note that the polygons do not have to be parallel to any of the xy-,xz-,yz- planes in general. <img class="tex-graphics" src="https://espresso.codeforces.com/4b5198028f3c57ef65791f641cca363e82b1c219.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "4\n0 0 0\n2 0 0\n2 2 0\n0 2 0\n4\n1 1 -1\n1 1 1\n1 3 1\n1 3 -1",
"output": "YES"
},
{
"input": "4\n4 -2 0\n4 3 0\n-3 3 0\n-3 -2 0\n4\n6 -2 0\n3 2 2\n-3 7 0\n3 4 6",
"output": "NO"
},
{
"input": "4\n-6 6 0\n13 9 0\n15 -7 0\n-5 -5 0\n4\n2 0 4\n2 6 8\n2 12 1\n2 4 -4",
"outpu... | 93 | 204,800 | -1 | 41,956 | |
309 | Context Advertising | [
"dp",
"two pointers"
] | null | null | Advertising has become part of our routine. And now, in the era of progressive technologies, we need your ideas to make advertising better!
In this problem we'll look at a simplified version of context advertising. You've got a text, consisting of exactly *n* words. A standard advertising banner has exactly *r* lines, each line can contain at most *c* characters. The potential customer always likes it when they can see lots of advertising, so you should determine which maximum number of consecutive words from the text can be written on the banner. Single words in one line of the banner should be separated by spaces. You are allowed to insert more than one space at once. Note that you are not allowed to break the words, that is, each word in the text must occupy exactly one line in the banner. Besides, you cannot change the word order, that is, if you read the banner text consecutively, from top to bottom and from left to right, you should get some consecutive part of the advertisement text.
More formally, the statement can be written like that. Let's say that all words are indexed from 1 to *n* in the order in which they occur in the advertisement text. Then you have to choose all words, starting from some *i*-th one and ending with some *j*-th one (1<=β€<=*i*<=β€<=*j*<=β€<=*n*), so that all of them could be written on the banner. There must be as many words as possible. See the samples for clarifications. | The first input line contains three integers *n*, *r*, *c* (1<=β€<=*n*,<=*r*,<=*c*<=β€<=106;Β *r*<=Γ<=*c*<=β€<=106). The next line contains a text, consisting of *n* words. The words consist only of lowercase English letters and are not empty. The words in the lines are separated by single spaces. The total number of characters in all words doesn't exceed 5Β·106. | Print at most *r* lines, in each line print at most *c* characters β the optimal advertisement banner. If there are multiple advertisement banners, print any of them.
Note that some lines of the banner can be empty. You are allowed not to print such lines. | [
"9 4 12\nthis is a sample text for croc final round\n",
"9 1 9\nthis is a sample text for croc final round\n",
"6 2 3\ncroc a a a croc a\n",
"2 2 5\nfirst second\n"
] | [
"this is a\nsample text\nfor croc\nfinal round\n",
"this is a\n",
"a a\na\n",
"first\n"
] | none | [] | 46 | 0 | 0 | 42,029 | |
703 | Mishka and Interesting sum | [
"data structures"
] | null | null | Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers *a*1,<=*a*2,<=...,<=*a**n* of *n* elements!
Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process *m* queries.
Each query is processed in the following way:
1. Two integers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*) are specifiedΒ β bounds of query segment. 1. Integers, presented in array segment [*l*,<=<=*r*] (in sequence of integers *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r*) even number of times, are written down. 1. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are *x*1,<=*x*2,<=...,<=*x**k*, then Mishka wants to know the value , where Β β operator of exclusive bitwise OR.
Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented. | The first line of the input contains single integer *n* (1<=β€<=*n*<=β€<=1<=000<=000)Β β the number of elements in the array.
The second line of the input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109)Β β array elements.
The third line of the input contains single integer *m* (1<=β€<=*m*<=β€<=1<=000<=000)Β β the number of queries.
Each of the next *m* lines describes corresponding query by a pair of integers *l* and *r* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*)Β β the bounds of query segment. | Print *m* non-negative integersΒ β the answers for the queries in the order they appear in the input. | [
"3\n3 7 8\n1\n1 3\n",
"7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5\n"
] | [
"0\n",
"0\n3\n1\n3\n2\n"
] | In the second sample:
There is no integers in the segment of the first query, presented even number of times in the segmentΒ β the answer is 0.
In the second query there is only integer 3 is presented even number of timesΒ β the answer is 3.
In the third query only integer 1 is written downΒ β the answer is 1.
In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8f3bf9fdacb25bb19b17c017c532cd102cb4993c.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the fifth query 1 and 3 are written down. The answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bfacb216edbf76a77a805a5347ff40d88d70f384.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3\n3 7 8\n1\n1 3",
"output": "0"
},
{
"input": "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5",
"output": "0\n3\n1\n3\n2"
},
{
"input": "10\n1 2 4 1 1 1 1 1 1 4\n55\n5 8\n3 9\n6 8\n4 6\n4 10\n2 8\n1 5\n7 8\n8 9\n7 9\n5 6\n8 10\n9 9\n2 2\n3 3\n3 7\n1 8\n2 3\n4 9\n8 8\n10 10\n1 ... | 46 | 0 | 0 | 42,073 | |
916 | Jamie and Tree | [
"data structures",
"trees"
] | null | null | To your surprise, Jamie is the final boss! Ehehehe.
Jamie has given you a tree with *n* vertices, numbered from 1 to *n*. Initially, the root of the tree is the vertex with number 1. Also, each vertex has a value on it.
Jamie also gives you three types of queries on the tree:
1 *v*Β β Change the tree's root to vertex with number *v*.
2 *u* *v* *x*Β β For each vertex in the subtree of smallest size that contains *u* and *v*, add *x* to its value.
3 *v*Β β Find sum of values of vertices in the subtree of vertex with number *v*.
A subtree of vertex *v* is a set of vertices such that *v* lies on shortest path from this vertex to root of the tree. Pay attention that subtree of a vertex can change after changing the tree's root.
Show your strength in programming to Jamie by performing the queries accurately! | The first line of input contains two space-separated integers *n* and *q* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*q*<=β€<=105)Β β the number of vertices in the tree and the number of queries to process respectively.
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=108<=β€<=*a**i*<=β€<=108)Β β initial values of the vertices.
Next *n*<=-<=1 lines contains two space-separated integers *u**i*,<=*v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*) describing edge between vertices *u**i* and *v**i* in the tree.
The following *q* lines describe the queries.
Each query has one of following formats depending on its type:
1 *v* (1<=β€<=*v*<=β€<=*n*) for queries of the first type.
2 *u* *v* *x* (1<=β€<=*u*,<=*v*<=β€<=*n*,<=<=-<=108<=β€<=*x*<=β€<=108) for queries of the second type.
3 *v* (1<=β€<=*v*<=β€<=*n*) for queries of the third type.
All numbers in queries' descriptions are integers.
The queries must be carried out in the given order. It is guaranteed that the tree is valid. | For each query of the third type, output the required answer. It is guaranteed that at least one query of the third type is given by Jamie. | [
"6 7\n1 4 2 8 5 7\n1 2\n3 1\n4 3\n4 5\n3 6\n3 1\n2 4 6 3\n3 4\n1 6\n2 2 4 -5\n1 4\n3 3\n",
"4 6\n4 3 5 6\n1 2\n2 3\n3 4\n3 1\n1 3\n2 2 4 3\n1 1\n2 2 4 -3\n3 1\n"
] | [
"27\n19\n5\n",
"18\n21\n"
] | The following picture shows how the tree varies after the queries in the first sample. | [] | 46 | 0 | 0 | 42,082 | |
991 | Bus Number | [
"brute force",
"combinatorics",
"math"
] | null | null | This night wasn't easy on Vasya. His favorite team lost, and he didn't find himself victorious eitherΒ β although he played perfectly, his teammates let him down every time. He had to win at least one more time, but the losestreak only grew longer and longer... It's no wonder he didn't get any sleep this night at all.
In the morning, Vasya was waiting the bus to the university on the bus stop. Vasya's thoughts were hazy and so he couldn't remember the right bus' number quite right and got onto the bus with the number $n$.
In the bus, Vasya thought that he could get the order of the digits in the number of the bus wrong. Futhermore, he could "see" some digits several times, but the digits he saw were definitely in the real number of the bus. For example, if Vasya saw the number 2028, it could mean that the real bus number could be 2028, 8022, 2820 or just 820. However, numbers 80, 22208, 52 definitely couldn't be the number of the bus. Also, real bus number couldn't start with the digit 0, this meaning that, for example, number 082 couldn't be the real bus number too.
Given $n$, determine the total number of possible bus number variants. | The first line contains one integer $n$ ($1 \leq n \leq 10^{18}$)Β β the number of the bus that was seen by Vasya. It is guaranteed that this number does not start with $0$. | Output a single integerΒ β the amount of possible variants of the real bus number. | [
"97\n",
"2028\n"
] | [
"2\n",
"13\n"
] | In the first sample, only variants $97$ and $79$ are possible.
In the second sample, the variants (in the increasing order) are the following: $208$, $280$, $802$, $820$, $2028$, $2082$, $2208$, $2280$, $2802$, $2820$, $8022$, $8202$, $8220$. | [
{
"input": "97",
"output": "2"
},
{
"input": "2028",
"output": "13"
},
{
"input": "1",
"output": "1"
},
{
"input": "10",
"output": "1"
},
{
"input": "168",
"output": "6"
},
{
"input": "999999",
"output": "6"
},
{
"input": "98765432002345678... | 78 | 0 | 3 | 42,153 | |
883 | Road Widening | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Mayor of city S just hates trees and lawns. They take so much space and there could be a road on the place they occupy!
The Mayor thinks that one of the main city streets could be considerably widened on account of lawn nobody needs anyway. Moreover, that might help reduce the car jams which happen from time to time on the street.
The street is split into *n* equal length parts from left to right, the *i*-th part is characterized by two integers: width of road *s**i* and width of lawn *g**i*.
For each of *n* parts the Mayor should decide the size of lawn to demolish. For the *i*-th part he can reduce lawn width by integer *x**i* (0<=β€<=*x**i*<=β€<=*g**i*). After it new road width of the *i*-th part will be equal to *s*'*i*<==<=*s**i*<=+<=*x**i* and new lawn width will be equal to *g*'*i*<==<=*g**i*<=-<=*x**i*.
On the one hand, the Mayor wants to demolish as much lawn as possible (and replace it with road). On the other hand, he does not want to create a rapid widening or narrowing of the road, which would lead to car accidents. To avoid that, the Mayor decided that width of the road for consecutive parts should differ by at most 1, i.e. for each *i* (1<=β€<=*i*<=<<=*n*) the inequation |*s*'*i*<=+<=1<=-<=*s*'*i*|<=β€<=1 should hold. Initially this condition might not be true.
You need to find the the total width of lawns the Mayor will destroy according to his plan. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2Β·105) β number of parts of the street.
Each of the following *n* lines contains two integers *s**i*,<=*g**i* (1<=β€<=*s**i*<=β€<=106, 0<=β€<=*g**i*<=β€<=106) β current width of road and width of the lawn on the *i*-th part of the street. | In the first line print the total width of lawns which will be removed.
In the second line print *n* integers *s*'1,<=*s*'2,<=...,<=*s*'*n* (*s**i*<=β€<=*s*'*i*<=β€<=*s**i*<=+<=*g**i*) β new widths of the road starting from the first part and to the last.
If there is no solution, print the only integer -1 in the first line. | [
"3\n4 5\n4 5\n4 10\n",
"4\n1 100\n100 1\n1 100\n100 1\n",
"3\n1 1\n100 100\n1 1\n"
] | [
"16\n9 9 10 \n",
"202\n101 101 101 101 \n",
"-1\n"
] | none | [
{
"input": "3\n4 5\n4 5\n4 10",
"output": "16\n9 9 10 "
},
{
"input": "4\n1 100\n100 1\n1 100\n100 1",
"output": "202\n101 101 101 101 "
},
{
"input": "3\n1 1\n100 100\n1 1",
"output": "-1"
},
{
"input": "10\n21005 10850\n27020 13372\n28183 3724\n22874 13564\n27446 11493\n225... | 1,637 | 20,377,600 | 3 | 42,154 | |
631 | Product Sum | [
"data structures",
"dp",
"geometry"
] | null | null | Blake is the boss of Kris, however, this doesn't spoil their friendship. They often gather at the bar to talk about intriguing problems about maximising some values. This time the problem is really special.
You are given an array *a* of length *n*. The characteristic of this array is the value Β β the sum of the products of the values *a**i* by *i*. One may perform the following operation exactly once: pick some element of the array and move to any position. In particular, it's allowed to move the element to the beginning or to the end of the array. Also, it's allowed to put it back to the initial position. The goal is to get the array with the maximum possible value of characteristic. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=200<=000)Β β the size of the array *a*.
The second line contains *n* integers *a**i* (1<=β€<=*i*<=β€<=*n*, |*a**i*|<=β€<=1<=000<=000)Β β the elements of the array *a*. | Print a single integer β the maximum possible value of characteristic of *a* that can be obtained by performing no more than one move. | [
"4\n4 3 2 5\n",
"5\n1 1 2 7 1\n",
"3\n1 1 2\n"
] | [
"39",
"49",
"9"
] | In the first sample, one may pick the first element and place it before the third (before 5). Thus, the answer will be 3Β·1β+β2Β·2β+β4Β·3β+β5Β·4β=β39.
In the second sample, one may pick the fifth element of the array and place it before the third. The answer will be 1Β·1β+β1Β·2β+β1Β·3β+β2Β·4β+β7Β·5β=β49. | [
{
"input": "4\n4 3 2 5",
"output": "39"
},
{
"input": "5\n1 1 2 7 1",
"output": "49"
},
{
"input": "3\n1 1 2",
"output": "9"
},
{
"input": "5\n1 2 3 4 5",
"output": "55"
},
{
"input": "5\n-1 -2 -3 -4 -5",
"output": "-45"
},
{
"input": "4\n0 0 0 0",
... | 1,000 | 16,793,600 | 0 | 42,176 | |
875 | Delivery Club | [
"binary search",
"data structures",
"dp"
] | null | null | Petya and Vasya got employed as couriers. During the working day they are to deliver packages to *n* different points on the line. According to the company's internal rules, the delivery of packages must be carried out strictly in a certain order. Initially, Petya is at the point with the coordinate *s*1, Vasya is at the point with the coordinate *s*2, and the clients are at the points *x*1,<=*x*2,<=...,<=*x**n* in the order of the required visit.
The guys agree in advance who of them will deliver the package to which of the customers, and then they act as follows. When the package for the *i*-th client is delivered, the one who delivers the package to the (*i*<=+<=1)-st client is sent to the path (it can be the same person who went to the point *x**i*, or the other). The friend who is not busy in delivering the current package, is standing still.
To communicate with each other, the guys have got walkie-talkies. The walkie-talkies work rather poorly at great distances, so Petya and Vasya want to distribute the orders so that the maximum distance between them during the day is as low as possible. Help Petya and Vasya to minimize the maximum distance between them, observing all delivery rules. | The first line contains three integers *n*, *s*1, *s*2 (1<=β€<=*n*<=β€<=100<=000, 0<=β€<=*s*1,<=*s*2<=β€<=109)Β β number of points of delivery and starting positions of Petya and Vasya.
The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n*Β β customers coordinates (0<=β€<=*x**i*<=β€<=109), in the order to make a delivery.
It is guaranteed, that among the numbers *s*1,<=*s*2,<=*x*1,<=...,<=*x**n* there are no two equal. | Output the only integer, minimum possible maximal distance between couriers during delivery. | [
"2 0 10\n5 6\n",
"3 2 1\n3 4 5\n",
"1 4 5\n2\n"
] | [
"10\n",
"1\n",
"2\n"
] | In the first test case the initial distance between the couriers is 10. This value will be the answer, for example, Petya can perform both deliveries, and Vasya will remain at the starting point.
In the second test case you can optimally act, for example, like this: Vasya delivers the package to the first customer, Petya to the second and, finally, Vasya delivers the package to the third client. With this order of delivery, the distance between the couriers will never exceed 1.
In the third test case only two variants are possible: if the delivery of a single package is carried out by Petya, the maximum distance between them will be 5β-β2β=β3. If Vasya will deliver the package, the maximum distance is 4β-β2β=β2. The latter method is optimal. | [] | 30 | 0 | 0 | 42,198 | |
145 | Lucky Pair | [
"combinatorics",
"data structures",
"implementation"
] | null | null | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has an array *a* of *n* integers. The numbers in the array are numbered starting from 1. Unfortunately, Petya has been misbehaving and so, his parents don't allow him play with arrays that have many lucky numbers. It is guaranteed that no more than 1000 elements in the array *a* are lucky numbers.
Petya needs to find the number of pairs of non-intersecting segments [*l*1;*r*1] and [*l*2;*r*2] (1<=β€<=*l*1<=β€<=*r*1<=<<=*l*2<=β€<=*r*2<=β€<=*n*, all four numbers are integers) such that there's no such lucky number that occurs simultaneously in the subarray *a*[*l*1..*r*1] and in the subarray *a*[*l*2..*r*2]. Help Petya count the number of such pairs. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105) β the size of the array *a*. The second line contains *n* space-separated integers *a**i* (1<=β€<=*a**i*<=β€<=109) β array *a*. It is guaranteed that no more than 1000 elements in the array *a* are lucky numbers. | On the single line print the only number β the answer to the problem.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"4\n1 4 2 4\n",
"2\n4 7\n",
"4\n4 4 7 7\n"
] | [
"9\n",
"1\n",
"9\n"
] | The subarray *a*[*l*..*r*] is an array that consists of elements *a*<sub class="lower-index">*l*</sub>, *a*<sub class="lower-index">*l*β+β1</sub>, ..., *a*<sub class="lower-index">*r*</sub>.
In the first sample there are 9 possible pairs that satisfy the condition: [1,β1] and [2,β2], [1,β1] and [2,β3], [1,β1] and [2,β4], [1,β1] and [3,β3], [1,β1] and [3,β4], [1,β1] and [4,β4], [1,β2] and [3,β3], [2,β2] and [3,β3], [3,β3] and [4,β4].
In the second sample there is only one pair of segments β [1;1] and [2;2] and it satisfies the condition. | [] | 30 | 0 | -1 | 42,215 | |
190 | Counter Attack | [
"data structures",
"dsu",
"graphs",
"hashing",
"sortings"
] | null | null | Berland has managed to repel the flatlanders' attack and is now starting the counter attack.
Flatland has *n* cities, numbered from 1 to *n*, and some pairs of them are connected by bidirectional roads. The Flatlandian maps show roads between cities if and only if there is in fact no road between this pair of cities (we do not know whether is it a clever spy-proof strategy or just saving ink). In other words, if two cities are connected by a road on a flatland map, then there is in fact no road between them. The opposite situation is also true: if two cities are not connected by a road on a flatland map, then in fact, there is a road between them.
The berlanders got hold of a flatland map. Now Vasya the Corporal is commissioned by General Touristov to find all such groups of flatland cities, that in each group of cities you can get from any city to any other one, moving along the actual roads. Also the cities from different groups are unreachable from each other, moving along the actual roads. Indeed, destroying such groups one by one is much easier than surrounding all Flatland at once!
Help the corporal complete this task and finally become a sergeant! Don't forget that a flatland map shows a road between cities if and only if there is in fact no road between them. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*<=β€<=5Β·105,<=0<=β€<=*m*<=β€<=106) β the number of cities and the number of roads marked on the flatland map, correspondingly.
Next *m* lines contain descriptions of the cities on the map. The *i*-th line contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*) β the numbers of cities that are connected by the *i*-th road on the flatland map.
It is guaranteed that each pair of cities occurs in the input no more than once. | On the first line print number *k* β the number of groups of cities in Flatland, such that in each group you can get from any city to any other one by flatland roads. At the same time, the cities from different groups should be unreachable by flatland roads.
On each of the following *k* lines first print *t**i* (1<=β€<=*t**i*<=β€<=*n*) β the number of vertexes in the *i*-th group. Then print space-separated numbers of cities in the *i*-th group.
The order of printing groups and the order of printing numbers in the groups does not matter. The total sum *t**i* for all *k* groups must equal *n*. | [
"4 4\n1 2\n1 3\n4 2\n4 3\n",
"3 1\n1 2\n"
] | [
"2\n2 1 4 \n2 2 3 \n",
"1\n3 1 2 3 \n"
] | In the first sample there are roads only between pairs of cities 1-4 and 2-3.
In the second sample there is no road between cities 1 and 2, but still you can get from one city to the other one through city number 3. | [
{
"input": "4 4\n1 2\n1 3\n4 2\n4 3",
"output": "2\n2 1 4 \n2 2 3 "
},
{
"input": "3 1\n1 2",
"output": "1\n3 1 2 3 "
},
{
"input": "8 14\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n5 6\n6 7",
"output": "2\n2 1 2 \n6 3 4 5 6 7 8 "
},
{
"input": "6 9\n1 4\n1 5... | 60 | 0 | 0 | 42,274 | |
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... | 0 | 0 | -1 | 42,497 |
609 | Gadgets for dollars and pounds | [
"binary search",
"greedy",
"two pointers"
] | null | null | Nura wants to buy *k* gadgets. She has only *s* burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.
Nura can buy gadgets for *n* days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.
Each day (from 1 to *n*) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during *n* days.
Help Nura to find the minimum day index when she will have *k* gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to *m* in order of their appearing in input. | First line contains four integers *n*,<=*m*,<=*k*,<=*s* (1<=β€<=*n*<=β€<=2Β·105,<=1<=β€<=*k*<=β€<=*m*<=β€<=2Β·105,<=1<=β€<=*s*<=β€<=109) β number of days, total number and required number of gadgets, number of burles Nura has.
Second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=106) β the cost of one dollar in burles on *i*-th day.
Third line contains *n* integers *b**i* (1<=β€<=*b**i*<=β€<=106) β the cost of one pound in burles on *i*-th day.
Each of the next *m* lines contains two integers *t**i*,<=*c**i* (1<=β€<=*t**i*<=β€<=2,<=1<=β€<=*c**i*<=β€<=106) β type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds. | If Nura can't buy *k* gadgets print the only line with the number -1.
Otherwise the first line should contain integer *d* β the minimum day index, when Nura will have *k* gadgets. On each of the next *k* lines print two integers *q**i*,<=*d**i* β the number of gadget and the day gadget should be bought. All values *q**i* should be different, but the values *d**i* can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to *n*.
In case there are multiple possible solutions, print any of them. | [
"5 4 2 2\n1 2 3 2 1\n3 2 1 2 3\n1 1\n2 1\n1 2\n2 2\n",
"4 3 2 200\n69 70 71 72\n104 105 106 107\n1 1\n2 2\n1 2\n",
"4 3 1 1000000000\n900000 910000 940000 990000\n990000 999000 999900 999990\n1 87654\n2 76543\n1 65432\n"
] | [
"3\n1 1\n2 3\n",
"-1\n",
"-1\n"
] | none | [
{
"input": "5 4 2 2\n1 2 3 2 1\n3 2 1 2 3\n1 1\n2 1\n1 2\n2 2",
"output": "3\n1 1\n2 3"
},
{
"input": "4 3 2 200\n69 70 71 72\n104 105 106 107\n1 1\n2 2\n1 2",
"output": "-1"
},
{
"input": "4 3 1 1000000000\n900000 910000 940000 990000\n990000 999000 999900 999990\n1 87654\n2 76543\n1 65... | 31 | 102,400 | -1 | 42,688 | |
0 | none | [
"none"
] | null | null | It can be shown that any positive integer *x* can be uniquely represented as *x*<==<=1<=+<=2<=+<=4<=+<=...<=+<=2*k*<=-<=1<=+<=*r*, where *k* and *r* are integers, *k*<=β₯<=0, 0<=<<=*r*<=β€<=2*k*. Let's call that representation prairie partition of *x*.
For example, the prairie partitions of 12, 17, 7 and 1 are:
17<==<=1<=+<=2<=+<=4<=+<=8<=+<=2,
7<==<=1<=+<=2<=+<=4,
1<==<=1.
Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options! | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of numbers given from Alice to Borys.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1012; *a*1<=β€<=*a*2<=β€<=...<=β€<=*a**n*)Β β the numbers given from Alice to Borys. | Output, in increasing order, all possible values of *m* such that there exists a sequence of positive integers of length *m* such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.
If there are no such values of *m*, output a single integer -1. | [
"8\n1 1 2 2 3 4 5 8\n",
"6\n1 1 1 2 2 2\n",
"5\n1 2 4 4 4\n"
] | [
"2 \n",
"2 3 \n",
"-1\n"
] | In the first example, Alice could get the input sequence from [6,β20] as the original sequence.
In the second example, Alice's original sequence could be either [4,β5] or [3,β3,β3]. | [
{
"input": "8\n1 1 2 2 3 4 5 8",
"output": "2 "
},
{
"input": "6\n1 1 1 2 2 2",
"output": "2 3 "
},
{
"input": "5\n1 2 4 4 4",
"output": "-1"
},
{
"input": "20\n1 1 1 1 2 2 2 2 4 4 4 4 8 8 8 8 8 10 10 11",
"output": "4 "
},
{
"input": "20\n1 1 1 1 1 1 1 1 1 1 1 1 ... | 280 | 11,878,400 | 0 | 42,740 | |
553 | Kyoya and Permutation | [
"binary search",
"combinatorics",
"constructive algorithms",
"greedy",
"implementation",
"math"
] | null | null | Let's define the permutation of length *n* as an array *p*<==<=[*p*1,<=*p*2,<=...,<=*p**n*] consisting of *n* distinct integers from range from 1 to *n*. We say that this permutation maps value 1 into the value *p*1, value 2 into the value *p*2 and so on.
Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of *p* as a collection of cycles forming *p*. For example, permutation *p*<==<=[4,<=1,<=6,<=2,<=5,<=3] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place.
Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4,<=1,<=6,<=2,<=5,<=3] is (421)(5)(63).
Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4,<=1,<=6,<=2,<=5,<=3] will become [4,<=2,<=1,<=5,<=6,<=3].
Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length *n* that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers *n* and *k*, print the permutation that was *k*-th on Kyoya's list. | The first line will contain two integers *n*, *k* (1<=β€<=*n*<=β€<=50, 1<=β€<=*k*<=β€<=*min*{1018,<=*l*} where *l* is the length of the Kyoya's list). | Print *n* space-separated integers, representing the permutation that is the answer for the question. | [
"4 3\n",
"10 1\n"
] | [
"1 3 2 4\n",
"1 2 3 4 5 6 7 8 9 10\n"
] | The standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1,β2,β3,β4], while the second permutation would be [1,β2,β4,β3]. | [
{
"input": "4 3",
"output": "1 3 2 4"
},
{
"input": "10 1",
"output": "1 2 3 4 5 6 7 8 9 10"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "50 1",
"output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 ... | 0 | 0 | -1 | 42,783 | |
85 | Guard Towers | [
"binary search",
"dsu",
"geometry",
"graphs",
"sortings"
] | E. Guard Towers | 1 | 256 | In a far away kingdom lives a very greedy king. To defend his land, he built *n* guard towers. Apart from the towers the kingdom has two armies, each headed by a tyrannical and narcissistic general. The generals can't stand each other, specifically, they will never let soldiers of two armies be present in one tower.
During defence operations to manage a guard tower a general has to send part of his army to that tower. Each general asks some fee from the king for managing towers. As they live in a really far away kingdom, each general evaluates his fee in the following weird manner: he finds two remotest (the most distant) towers, where the soldiers of his army are situated and asks for the fee equal to the distance. Each tower is represented by a point on the plane with coordinates (*x*,<=*y*), and the distance between two points with coordinates (*x*1,<=*y*1) and (*x*2,<=*y*2) is determined in this kingdom as |*x*1<=-<=*x*2|<=+<=|*y*1<=-<=*y*2|.
The greedy king was not exactly satisfied with such a requirement from the generals, that's why he only agreed to pay one fee for two generals, equal to the maximum of two demanded fees. However, the king is still green with greed, and among all the ways to arrange towers between armies, he wants to find the cheapest one. Each tower should be occupied by soldiers of exactly one army.
He hired you for that. You should find the minimum amount of money that will be enough to pay the fees. And as the king is also very scrupulous, you should also count the number of arrangements that will cost the same amount of money. As their number can be quite large, it is enough for the king to know it as a remainder from dividing by 109<=+<=7.
Two arrangements are distinct if the sets of towers occupied by soldiers of the first general are distinct. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=5000), *n* is the number of guard towers. Then follow *n* lines, each of which contains two integers *x*,<=*y* β the coordinates of the *i*-th tower (0<=β€<=*x*,<=*y*<=β€<=5000). No two towers are present at one point.
Pretest 6 is one of the maximal tests for this problem. | Print on the first line the smallest possible amount of money that will be enough to pay fees to the generals.
Print on the second line the number of arrangements that can be carried out using the smallest possible fee. This number should be calculated modulo 1000000007 (109<=+<=7). | [
"2\n0 0\n1 1\n",
"4\n0 0\n0 1\n1 0\n1 1\n",
"3\n0 0\n1000 1000\n5000 5000\n"
] | [
"0\n2\n",
"1\n4\n",
"2000\n2\n"
] | In the first example there are only two towers, the distance between which is equal to 2. If we give both towers to one general, then we well have to pay 2 units of money. If each general receives a tower to manage, to fee will be equal to 0. That is the smallest possible fee. As you can easily see, we can obtain it in two ways. | [] | 46 | 0 | 0 | 42,831 |
398 | Painting The Wall | [
"dp",
"probabilities"
] | null | null | User ainta decided to paint a wall. The wall consists of *n*2 tiles, that are arranged in an *n*<=Γ<=*n* table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.
1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2. 1. User ainta choose any tile on the wall with uniform probability. 1. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it. 1. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1.
However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=2Β·103; 0<=β€<=*m*<=β€<=*min*(*n*2,<=2Β·104)) β the size of the wall and the number of painted cells.
Next *m* lines goes, each contains two integers *r**i* and *c**i* (1<=β€<=*r**i*,<=*c**i*<=β€<=*n*) β the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to *n*. Consider the columns of the table are numbered from 1 to *n*. | In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10<=-<=4 absolute or relative error. | [
"5 2\n2 3\n4 1\n",
"2 2\n1 1\n1 2\n",
"1 1\n1 1\n"
] | [
"11.7669491886\n",
"2.0000000000\n",
"0.0000000000\n"
] | none | [] | 93 | 0 | -1 | 42,968 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.