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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
736 | Permutations | [
"math",
"matrices"
] | null | null | Ostap Bender is worried that people started to forget that he is the Great Combinator. Now he wants to show them his skills in combinatorics. Now he studies the permutations of length *n*. He has a list of *m* valid pairs, pair *a**i* and *b**i* means that he is allowed to place integers *b**i* at position *a**i*.
He ... | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=2000, *n*<=≤<=*m*<=≤<=*min*(*n*2,<=500<=000)) — the number of elements in the permutation. Then follow *m* lines, each containing some valid pair (*a**i*,<=*b**i*) (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*). It's guaranteed that no pair occurs in the input twice and ... | Print *m* lines, one line for each valid pair. The *i*-th line should contain "YES" if after Ostap removes the *i*-th pair (and only it) the remaining number of valid permutations is odd. Otherwise, print «NO». | [
"2 3\n1 1\n1 2\n2 2\n",
"3 3\n1 1\n2 2\n3 3\n",
"3 7\n3 3\n3 1\n1 3\n1 1\n2 2\n1 2\n2 1\n"
] | [
"NO\nYES\nNO\n",
"NO\nNO\nNO\n",
"YES\nNO\nNO\nNO\nYES\nNO\nNO\n"
] | none | [] | 2,000 | 47,411,200 | 0 | 397,410 | |
526 | Spiders Evil Plan | [
"greedy",
"trees"
] | null | null | Spiders are Om Nom's old enemies. They love eating candies as much as he does and that's why they keep trying to keep the monster away from his favorite candies. They came up with an evil plan to trap Om Nom.
Let's consider a rope structure consisting of *n* nodes and *n*<=-<=1 ropes connecting the nodes. The structur... | The first line contains numbers *n* and *q* (1<=≤<=*n*,<=*q*<=≤<=105) — the number of nodes in the structure and the number of questions that the spiders want to ask you.
The next *n*<=-<=1 lines determine the rope structure. The *i*-th line contains three integers *u**i*,<=*v**i*,<=*l**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*... | For each question of the spiders print on a separate line a single integer *Ans**i* — the total length of the ropes covered with web in the optimal plan. | [
"6 3\n1 2 2\n2 3 2\n3 4 2\n4 6 1\n3 5 10\n3 1\n2 5\n1 1\n"
] | [
"14\n13\n17\n"
] | none | [] | 30 | 0 | 0 | 397,781 | |
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 w... | 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 ne... | 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... | [] | 31 | 0 | 0 | 397,864 | |
372 | Drawing Circles is Fun | [
"combinatorics",
"geometry"
] | null | null | There are a set of points *S* on the plane. This set doesn't contain the origin *O*(0,<=0), and for each two distinct points in the set *A* and *B*, the triangle *OAB* has strictly positive area.
Consider a set of pairs of points (*P*1,<=*P*2),<=(*P*3,<=*P*4),<=...,<=(*P*2*k*<=-<=1,<=*P*2*k*). We'll call the set good ... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=1000) — the number of points in *S*. Each of the next *n* lines contains four integers *a**i*,<=*b**i*,<=*c**i*,<=*d**i* (0<=≤<=|*a**i*|,<=|*c**i*|<=≤<=50; 1<=≤<=*b**i*,<=*d**i*<=≤<=50; (*a**i*,<=*c**i*)<=≠<=(0,<=0)). These integers represent a point .
No two... | Print a single integer — the answer to the problem modulo 1000000007 (109<=+<=7). | [
"10\n-46 46 0 36\n0 20 -24 48\n-50 50 -49 49\n-20 50 8 40\n-15 30 14 28\n4 10 -4 5\n6 15 8 10\n-20 50 -3 15\n4 34 -16 34\n16 34 2 17\n",
"10\n30 30 -26 26\n0 15 -36 36\n-28 28 -34 34\n10 10 0 4\n-8 20 40 50\n9 45 12 30\n6 15 7 35\n36 45 -8 20\n-16 34 -4 34\n4 34 8 17\n",
"10\n0 20 38 38\n-30 30 -13 13\n-11 11 1... | [
"2\n",
"4\n",
"10\n"
] | none | [] | 358 | 30,924,800 | 0 | 398,004 | |
209 | Trails and Glades | [
"constructive algorithms",
"dsu",
"graphs",
"greedy"
] | null | null | Vasya went for a walk in the park. The park has *n* glades, numbered from 1 to *n*. There are *m* trails between the glades. The trails are numbered from 1 to *m*, where the *i*-th trail connects glades *x**i* and *y**i*. The numbers of the connected glades may be the same (*x**i*<==<=*y**i*), which means that a trail ... | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=106; 0<=≤<=*m*<=≤<=106) — the number of glades in the park and the number of trails in the park, respectively. Next *m* lines specify the trails. The *i*-th line specifies the *i*-th trail as two space-separated numbers, *x**i*, *y**i* (1<=≤<=*x**i*,<=*y**... | Print the single integer — the answer to the problem. If Vasya's walk is possible without adding extra trails, print 0, otherwise print the minimum number of trails the authorities need to add to the park in order to make Vasya's walk possible. | [
"3 3\n1 2\n2 3\n3 1\n",
"2 5\n1 1\n1 2\n1 2\n2 2\n1 2\n"
] | [
"0\n",
"1\n"
] | In the first test case the described walk is possible without building extra trails. For example, let's first go on the first trail, then on the second one, and finally on the third one.
In the second test case the described walk is impossible without adding extra trails. To make the walk possible, it is enough to add... | [] | 46 | 0 | 0 | 399,360 | |
309 | Tennis Rackets | [
"brute force",
"geometry"
] | null | null | Professional sport is more than hard work. It also is the equipment, designed by top engineers. As an example, let's take tennis. Not only should you be in great shape, you also need an excellent racket! In this problem your task is to contribute to the development of tennis and to help to design a revolutionary new co... | The first and the only input line contains two integers *n*, *m* . | Print a single number — the answer to the problem. | [
"3 0\n",
"4 0\n",
"10 1\n",
"8 4\n"
] | [
"9\n",
"24\n",
"210\n",
"0\n"
] | For the following picture *n* = 8, *m* = 2. White circles are the holes for ventilation, red circles — holes for net stretching. One of the possible obtuse triangles is painted red. | [] | 30 | 0 | 0 | 400,544 | |
925 | Parametric Circulation | [
"binary search",
"flows"
] | null | null | Vova has recently learned what a circulaton in a graph is. Recall the definition: let $G = (V, E)$ be a directed graph. A circulation $f$ is such a collection of non-negative real numbers $f_e$ ($e \in E$), that for each vertex $v \in V$ the following conservation condition holds:
$$\sum\limits_{e \in \delta^{-}(v)} f... | The first line contains two integers $n$, $m$ ($1 \leq n \leq 1000$, $1 \leq m \leq 2000$).
Each of the next $m$ lines describes edges of the graph in the format $u_e$, $v_e$, $a_e$, $b_e$, $c_e$, $d_e$ ($1 \leq u_e, v_e \leq n$, $-10^4 \leq a_e, c_e \leq 10^4$, $0 \leq b_e, d_e \leq 10^4$), where $u_e$ and $v_e$ are ... | Print a single real integer — the probability of existence of $lr$-circulation in the graph, given that $t$ is chosen uniformly at random from the segment $[0, 1]$. Your answer is considered correct if its absolute difference from jury's answer is not greater than $10^{-6}$. | [
"3 3\n1 2 0 3 -4 7\n2 3 -2 5 1 6\n3 1 0 4 0 4\n"
] | [
"0.25"
] | In the first example the conservation condition allows only circulations with equal values $f_e$ for all three edges. The value of circulation on the last edge should be $4$ whatever $t$ is chosen, so the probability is
$$P(4 \in [3, -4t + 7]~~\&~~4 \in [-2t + 5, t + 6]) = 0.25$$ | [] | 30 | 0 | 0 | 400,606 | |
581 | Zublicanes and Mumocrates | [
"dp",
"trees",
"two pointers"
] | null | null | It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The election campaigns of both parties include numerous demonstrations on *n* main squares of the capital of Berland. Each of the *n* squares certainly can have demonstrations of only one party, otherwise it could lead to r... | The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=5000) — the number of squares in the capital of Berland.
Next *n*<=-<=1 lines contain the pairs of integers *x*,<=*y* (1<=≤<=*x*,<=*y*<=≤<=*n*,<=*x*<=≠<=*y*) — the numbers of the squares connected by the road. All squares are numbered with intege... | Print a single number — the minimum number of roads connecting the squares with demonstrations of different parties. | [
"8\n1 4\n2 4\n3 4\n6 5\n7 5\n8 5\n4 5\n",
"5\n1 2\n1 3\n1 4\n1 5\n"
] | [
"1\n",
"2\n"
] | none | [
{
"input": "8\n1 4\n2 4\n3 4\n6 5\n7 5\n8 5\n4 5",
"output": "1"
},
{
"input": "5\n1 2\n1 3\n1 4\n1 5",
"output": "2"
},
{
"input": "11\n1 7\n2 1\n2 9\n6 2\n7 10\n1 3\n5 2\n3 8\n8 11\n2 4",
"output": "2"
},
{
"input": "20\n2 18\n15 18\n18 4\n4 20\n20 6\n8 6\n1 8\n9 6\n11 9\n1... | 420 | 9,625,600 | 3 | 401,715 | |
582 | Boolean Function | [
"bitmasks",
"dp",
"expression parsing"
] | null | null | In this problem we consider Boolean functions of four variables *A*,<=*B*,<=*C*,<=*D*. Variables *A*,<=*B*,<=*C* and *D* are logical and can take values 0 or 1. We will define a function using the following grammar:
<expression> ::= <variable> | (<expression>) <operator> (<expression>)
&... | The first line contains expression *s* (1<=≤<=|*s*|<=≤<=500), where some characters of the operators and/or variables are replaced by character '?'.
The second line contains number *n* (0<=≤<=*n*<=≤<=24) — the number of integers sets for which we know the value of function *f*(*A*,<=*B*,<=*C*,<=*D*). Next *n* lines c... | In a single line print the answer to the problem. | [
"?\n2\n1 0 1 0 1\n0 1 1 0 1\n",
"(A)?(?)\n1\n1 1 0 0 0\n",
"((?)&(?))|((?)&(?))\n0\n",
"b\n1\n1 0 1 1 1\n"
] | [
"2\n",
"4\n",
"4096",
"1\n"
] | In the first sample the two valid expressions are 'C' and 'd'.
In the second sample the expressions look as follows: '(A)&(a)', '(A)&(b)', '(A)&(C)', '(A)&(D)'. | [] | 46 | 0 | 0 | 402,043 | |
269 | Maximum Waterfall | [
"data structures",
"dp",
"graphs",
"sortings"
] | null | null | Emuskald was hired to design an artificial waterfall according to the latest trends in landscape architecture. A modern artificial waterfall consists of multiple horizontal panels affixed to a wide flat wall. The water flows down the top of the wall from panel to panel until it reaches the bottom of the wall.
The wall... | The first line of input contains two space-separated integers *n* and *t* (1<=≤<=*n*<=≤<=105, 2<=≤<=*t*<=≤<=109), the number of the panels excluding the top and the bottom panels, and the height of the wall. Each of the *n* following lines contain three space-separated integers *h**i*, *l**i* and *r**i* (0<=<<=*h**i... | Output a single integer — the maximum possible amount of water flow in the desired waterfall. | [
"5 6\n4 1 6\n3 2 7\n5 9 11\n3 10 15\n1 13 16\n",
"6 5\n4 2 8\n3 1 2\n2 2 3\n2 6 12\n1 0 7\n1 8 11\n"
] | [
"4\n",
"2\n"
] | The first test case corresponds to the picture. | [] | 30 | 0 | 0 | 403,133 | |
508 | Arthur and Brackets | [
"dp",
"greedy"
] | null | null | Notice that the memory limit is non-standard.
Recently Arthur and Sasha have studied correct bracket sequences. Arthur understood this topic perfectly and become so amazed about correct bracket sequences, so he even got himself a favorite correct bracket sequence of length 2*n*. Unlike Arthur, Sasha understood the top... | The first line contains integer *n* (1<=≤<=*n*<=≤<=600), the number of opening brackets in Arthur's favorite correct bracket sequence.
Next *n* lines contain numbers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=<<=2*n*), representing the segment where lies the distance from the *i*-th opening bracket and the corres... | If it is possible to restore the correct bracket sequence by the given data, print any possible choice.
If Arthur got something wrong, and there are no sequences corresponding to the given information, print a single line "IMPOSSIBLE" (without the quotes). | [
"4\n1 1\n1 1\n1 1\n1 1\n",
"3\n5 5\n3 3\n1 1\n",
"3\n5 5\n3 3\n2 2\n",
"3\n2 3\n1 4\n1 4\n"
] | [
"()()()()\n",
"((()))\n",
"IMPOSSIBLE\n",
"(())()\n"
] | none | [
{
"input": "4\n1 1\n1 1\n1 1\n1 1",
"output": "()()()()"
},
{
"input": "3\n5 5\n3 3\n1 1",
"output": "((()))"
},
{
"input": "3\n5 5\n3 3\n2 2",
"output": "IMPOSSIBLE"
},
{
"input": "3\n2 3\n1 4\n1 4",
"output": "(())()"
},
{
"input": "6\n1 5\n2 4\n1 1\n1 1\n1 1\n1... | 46 | 204,800 | 3 | 403,379 | |
293 | Ksusha and Square | [
"geometry",
"math",
"probabilities",
"two pointers"
] | null | null | Ksusha is a vigorous mathematician. She is keen on absolutely incredible mathematical riddles.
Today Ksusha came across a convex polygon of non-zero area. She is now wondering: if she chooses a pair of distinct points uniformly among all integer points (points with integer coordinates) inside or on the border of the ... | The first line contains integer *n* (3<=≤<=*n*<=≤<=105) — the number of vertices of Ksusha's convex polygon. Next *n* lines contain the coordinates of the polygon vertices in clockwise or counterclockwise order. The *i*-th line contains integers *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=≤<=106) — the coordinates of the ver... | Print a single real number — the required expected area.
The answer will be considered correct if its absolute and relative error doesn't exceed 10<=-<=6. | [
"3\n0 0\n5 5\n5 0\n",
"4\n-1 3\n4 5\n6 2\n3 -5\n",
"3\n17 136\n859 937\n16 641\n"
] | [
"4.6666666667\n",
"8.1583333333\n",
"66811.3704155169\n"
] | none | [
{
"input": "3\n0 0\n5 5\n5 0",
"output": "4.6666666667"
},
{
"input": "4\n-1 3\n4 5\n6 2\n3 -5",
"output": "8.1583333333"
},
{
"input": "3\n17 136\n859 937\n16 641",
"output": "66811.3704155169"
},
{
"input": "6\n-29 6\n-28 27\n-2 18\n13 9\n20 -18\n19 -27",
"output": "308... | 92 | 0 | 0 | 403,465 | |
95 | Lucky Country | [
"dp",
"dsu",
"graphs"
] | E. Lucky Country | 1 | 256 | Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One night Petya was sleeping. He was dreaming of being the president of some island country. The count... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105). They are the number of islands and the number of roads correspondingly. Next *m* lines contain road descriptions. Each road is defined by the numbers of islands that it connects: that is, by two integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*n*). S... | If there's no solution, output the only number "-1" (without the quotes). Otherwise, output the minimum number of roads *r* that need to be built to get a lucky region. | [
"4 3\n1 2\n2 3\n1 3\n",
"5 4\n1 2\n3 4\n4 5\n3 5\n"
] | [
"1\n",
"-1\n"
] | none | [
{
"input": "4 3\n1 2\n2 3\n1 3",
"output": "1"
},
{
"input": "5 4\n1 2\n3 4\n4 5\n3 5",
"output": "-1"
},
{
"input": "7 6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7",
"output": "0"
},
{
"input": "7 5\n2 3\n3 4\n4 5\n3 6\n2 2",
"output": "2"
},
{
"input": "1 1\n1 1",
"outpu... | 30 | 307,200 | 0 | 403,910 |
0 | none | [
"none"
] | null | null | Little Artem is a very smart programmer. He knows many different difficult algorithms. Recently he has mastered in 2-SAT one.
In computer science, 2-satisfiability (abbreviated as 2-SAT) is the special case of the problem of determining whether a conjunction (logical AND) of disjunctions (logical OR) have a solution, ... | The first line of the input contains three integers *n*, *m*1 and *m*2 (1<=≤<=*n*<=≤<=1000, 1<=≤<=*m*1,<=*m*2<=≤<=*n*2) — the number of variables, the number of disjunctions in the first formula and the number of disjunctions in the second formula, respectively.
Next *m*1 lines contains the description of 2-SAT formul... | If both formulas share the same set of solutions, output a single word "SIMILAR" (without quotes). Otherwise output exactly *n* integers *x**i* () — any set of values *x* such that *f*(*x*)<=≠<=*g*(*x*). | [
"2 1 1\n1 2\n1 2\n",
"2 1 1\n1 2\n1 -2\n"
] | [
"SIMILAR\n",
"0 0 \n"
] | First sample has two equal formulas, so they are similar by definition.
In second sample if we compute first function with *x*<sub class="lower-index">1</sub> = 0 and *x*<sub class="lower-index">2</sub> = 0 we get the result 0, because <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/82cbca... | [] | 15 | 0 | 0 | 410,832 | |
538 | Berserk Robot | [
"constructive algorithms",
"math",
"sortings"
] | null | null | Help! A robot escaped our lab and we need help finding it.
The lab is at the point (0,<=0) of the coordinate plane, at time 0 the robot was there. The robot's movements are defined by a program — a string of length *l*, consisting of characters U, L, D, R. Each second the robot executes the next command in his progra... | The first line of the input contains two space-separated integers *n* and *l* (1<=≤<=*n*<=≤<=2·105, 1<=≤<=*l*<=≤<=2·106).
Next *n* lines contain three space-separated integers — *t**i*, *x**i*, *y**i* (1<=≤<=*t**i*<=≤<=1018, <=-<=1018<=≤<=*x**i*,<=*y**i*<=≤<=1018). The radar data is given chronologically, i.e. *t**i*<... | Print any of the possible programs that meet the data. If no program meets the data, print a single word 'NO' (without the quotes). | [
"3 3\n1 1 0\n2 1 -1\n3 0 -1\n",
"2 2\n1 1 0\n999 1 0\n",
"2 5\n10 10 0\n20 0 0\n"
] | [
"RDL\n",
"RL\n",
"NO\n"
] | none | [] | 46 | 0 | 0 | 412,582 | |
986 | Prince's Problem | [
"brute force",
"data structures",
"math",
"number theory",
"trees"
] | null | null | Let the main characters of this problem be personages from some recent movie. New Avengers seem to make a lot of buzz. I didn't watch any part of the franchise and don't know its heroes well, but it won't stop me from using them in this problem statement. So, Thanos and Dr. Strange are doing their superhero and supervi... | In the first line of input there is one integer $n$ ($1 \le n \le 10^{5}$) — the size of the tree.
In the next $n-1$ lines the edges of the tree are described. The $i$-th edge is described with two integers $u_{i}$ and $v_{i}$ ($1 \le u_{i}, v_{i} \le n$) and it connects the vertices $u_{i}$ and $v_{i}$. It is guarant... | Print $q$ numbers — the answers to the queries in the order they are given in the input. Print each answer modulo $10^9+7 = 1000000007$. Print each number on a separate line. | [
"4\n1 2\n1 3\n1 4\n6 4 9 5\n3\n2 3 6\n2 3 2\n3 4 7\n",
"6\n1 2\n2 3\n2 4\n1 5\n5 6\n100000 200000 500000 40000 800000 250000\n3\n3 5 10000000\n6 2 3500000\n4 1 64000\n"
] | [
"36\n4\n1\n",
"196000\n12250\n999998215\n"
] | none | [] | 78 | 0 | 0 | 414,078 | |
375 | Circling Round Treasures | [
"bitmasks",
"shortest paths"
] | null | null | You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure with a certain price, or a bomb, or an empty cell. Your initial position is also given to you.
You can go from one cell of the map to a side-adjacent one. At that, you are not allowed to go beyond the borders of the map, e... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=20) — the sizes of the table. Next *n* lines each contains *m* characters — the description of the table. The description means the following:
- character "B" is a cell with a bomb; - character "S" is the starting cell, you can assume that it's em... | Print a single integer — the maximum possible profit you can get. | [
"4 4\n....\n.S1.\n....\n....\n10\n",
"7 7\n.......\n.1###2.\n.#...#.\n.#.B.#.\n.3...4.\n..##...\n......S\n100\n100\n100\n100\n",
"7 8\n........\n........\n....1B..\n.S......\n....2...\n3.......\n........\n100\n-100\n100\n",
"1 1\nS\n"
] | [
"2\n",
"364\n",
"0\n",
"0\n"
] | In the first example the answer will look as follows.
In the second example the answer will look as follows.
In the third example you cannot get profit.
In the fourth example you cannot get profit as you cannot construct a closed path with more than one cell. | [] | 46 | 0 | 0 | 420,330 | |
449 | Jzzhu and Squares | [
"dp",
"math",
"number theory"
] | null | null | Jzzhu has two integers, *n* and *m*. He calls an integer point (*x*,<=*y*) of a plane special if 0<=≤<=*x*<=≤<=*n* and 0<=≤<=*y*<=≤<=*m*. Jzzhu defines a unit square as a square with corners at points (*x*,<=*y*), (*x*<=+<=1,<=*y*), (*x*<=+<=1,<=*y*<=+<=1), (*x*,<=*y*<=+<=1), where *x* and *y* are some integers.
Let's... | The first line contains a single integer *t* (1<=≤<=*t*<=≤<=105) — the number of tests.
Each of the next *t* lines contains the description of the test: two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106) — the value of variables for the current test. | For each test output the total number of dots modulo 1000000007 (109<=+<=7). | [
"4\n1 3\n2 2\n2 5\n3 4\n"
] | [
"3\n8\n26\n58\n"
] | none | [] | 30 | 0 | 0 | 421,752 | |
747 | Igor and Interesting Numbers | [
"brute force",
"combinatorics",
"dp",
"math"
] | null | null | Igor likes hexadecimal notation and considers positive integer in the hexadecimal notation interesting if each digit and each letter in it appears no more than *t* times. For example, if *t*<==<=3, then integers 13a13322, aaa, abcdef0123456789 are interesting, but numbers aaaa, abababab and 1000000 are not interesting.... | The first line contains the two integers *k* and *t* (1<=≤<=*k*<=≤<=2·109, 1<=≤<=*t*<=≤<=10) — the number of the required integer and the maximum number of times some integer or letter can appear in interesting integer.
It can be shown that the answer always exists for such constraints. | Print in the hexadecimal notation the only integer that is the *k*-th smallest interesting integer for Igor. | [
"17 1\n",
"1000000 2\n"
] | [
"12\n",
"fca2c\n"
] | The first 20 interesting integers if *t* = 1: 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, 10, 12, 13, 14, 15. So the answer for the first example equals 12. | [
{
"input": "17 1",
"output": "12"
},
{
"input": "1000000 2",
"output": "fca2c"
},
{
"input": "200000000 5",
"output": "bebc69a"
},
{
"input": "999999 3",
"output": "f467f"
},
{
"input": "40 1",
"output": "2a"
},
{
"input": "105 1",
"output": "6f"
... | 30 | 0 | 0 | 424,261 | |
471 | MUH and Lots and Lots of Segments | [
"data structures",
"dsu"
] | null | null | Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to do some painting. As they were trying to create their first masterpiece, they made a draft on a piece of paper. The draft consists of *n* segments. Each segment was either horizontal or vertical. Now the ... | The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of segments on the draft. The next *n* lines contain four integers each: *x*1, *y*1, *x*2, *y*2 (<=-<=109<=≤<=*x*1<=≤<=*x*2<=≤<=109; <=-<=109<=≤<=*y*1<=≤<=*y*2<=≤<=109) — the two startpoint and the two endpoint coordinates of a segment.... | Print a single integer — the maximum sum of lengths for the remaining segments. | [
"2\n0 0 0 1\n1 0 1 1\n",
"4\n0 0 1 0\n0 0 0 1\n1 -1 1 2\n0 1 1 1\n"
] | [
"1",
"5"
] | The shapes that you can get in the two given samples are:
In the first sample you need to delete any segment as the two segments together do not form a single connected shape.
In the second sample the initial segments form a cycle, there are four ways to break the cycle: delete the first, second or fourth segment alt... | [] | 46 | 0 | 0 | 424,399 | |
212 | Cutting a Fence | [
"binary search",
"data structures",
"dsu"
] | null | null | Vasya the carpenter has an estate that is separated from the wood by a fence. The fence consists of *n* planks put in a line. The fence is not closed in a circle. The planks are numbered from left to right from 1 to *n*, the *i*-th plank is of height *a**i*. All planks have the same width, the lower edge of each plank ... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=106) — the number of planks in the fence. The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) where *a**i* is the height of the *i*-th plank of the fence.
The third line contains an integer *m* (1<=≤<=*m*<=≤<=106)... | Print *m* whitespace-separated real numbers, the *i*-th number equals the expected value of the rectangle height, if its width in planks equals *k**i*. The value will be considered correct if its absolute or relative error doesn't exceed 10<=-<=9. | [
"3\n3 2 1\n3\n1 2 3\n",
"2\n1 1\n3\n1 2 1\n"
] | [
"2.000000000000000\n1.500000000000000\n1.000000000000000\n",
"1.000000000000000\n1.000000000000000\n1.000000000000000\n"
] | Let's consider the first sample test.
- There are three possible positions of the fence for *k*<sub class="lower-index">1</sub> = 1. For the first position (*x* = 1) the height is 3, for the second one (*x* = 2) the height is 2, for the third one (*x* = 3) the height is 1. As the fence position is chosen uniformly, ... | [] | 30 | 0 | 0 | 425,912 | |
87 | Mogohu-Rea Idol | [
"geometry"
] | E. Mogohu-Rea Idol | 3 | 256 | A long time ago somewhere in the depths of America existed a powerful tribe governed by the great leader Pinnie-the-Wooh. Once the tribe conquered three Maya cities. Pinnie-the-Wooh grew concerned: there had to be some control over the conquered territories. That's why he appealed to the priests of the supreme god Mogo... | First follow descriptions of the three cities, divided by empty lines. The descriptions are in the following format:
The first line contains an integer *n*, which represent the number of the polygon's vertexes (3<=≤<=*n*<=≤<=5·104). Next *n* lines contain two integers *x**i*, *y**i* each, they are the coordinates of t... | For each hill print on a single line "YES" (without the quotes) or "NO" (without the quotes), depending on whether the three sacrifice altars can be put to balance the idol or not. | [
"3\n0 0\n1 0\n1 1\n\n4\n8 8\n5 5\n6 4\n8 4\n\n3\n-1 -1\n-3 -1\n-2 -2\n\n5\n0 0\n2 1\n7 1\n1 1\n5 3\n"
] | [
"NO\nYES\nNO\nYES\nNO\n"
] | For the hill at (2, 1) the altars can be placed at the points (1, 0), (7, 5), ( - 2, - 2), for the hill at (1, 1) — at the points (0, 0), (6, 4), ( - 3, - 1). Many other groups of three points can do the trick. There are no suitable points for other hills. | [] | 30 | 0 | 0 | 426,356 |
730 | Roads Orientation Problem | [
"graphs"
] | null | null | Berland consists of *n* cities and *m* bidirectional roads connecting pairs of cities. There is no road connecting a city to itself, and between any pair of cities there is no more than one road. It is possible to reach any city from any other moving along roads.
Currently Mr. President is in the city *s* and his dest... | Each test in this problem contains one or more test cases to solve. The first line of the input contains positive number *T* — the number of cases to solve.
Each case starts with a line containing four integers *n*, *m*, *s* and *t* (2<=≤<=*n*<=≤<=4·105, 1<=≤<=*m*<=≤<=106, 1<=≤<=*s*,<=*t*<=≤<=*n*, *s*<=≠<=*t*) — the n... | For each case print "Yes" if the answer exists. In the following *m* lines print roads in the required directions. You can print roads in arbitrary order. If there are multiple answers, print any of them.
Print the only line "No" if there is no answer for a case. | [
"2\n4 4 1 2\n1 2\n2 3\n3 4\n4 1\n3 2 1 3\n3 1\n2 3\n"
] | [
"Yes\n1 2\n3 2\n4 3\n1 4\nNo\n"
] | none | [] | 46 | 0 | 0 | 427,425 | |
750 | New Year and Finding Roots | [
"constructive algorithms",
"implementation",
"interactive",
"trees"
] | null | null | This is an interactive problem. In the interaction section below you will find the information about flushing the output.
The New Year tree of height *h* is a perfect binary tree with vertices numbered 1 through 2*h*<=-<=1 in some order. In this problem we assume that *h* is at least 2. The drawing below shows one exa... | The first line of the input contains a single integer *t* (1<=≤<=*t*<=≤<=500) — the number of testcases.
At the beginning of each testcase you should read from the input a single integer *h* (2<=≤<=*h*<=≤<=7) — the height of the tree. You can't read the value of *h* in a next testcase until you answer a previous testc... | none | [
"1\n3\n3\n4 5 7\n2\n1 2\n1\n2\n",
"2\n2\n1\n3\n2\n1 2\n2\n1 2\n4\n3\n3 12 13\n"
] | [
"? 1\n? 5\n? 6\n! 5\n",
"? 1\n? 3\n? 3\n! 3\n? 6\n! 1\n"
] | In the first sample, a tree corresponds to the drawing from the statement.
In the second sample, there are two two testcases. A tree in the first testcase has height 2 and thus 3 vertices. A tree in the second testcase has height 4 and thus 15 vertices. You can see both trees on the drawing below. | [] | 46 | 0 | 0 | 428,611 | |
187 | Heaven Tour | [
"data structures",
"greedy"
] | null | null | The story was not finished as PMP thought. God offered him one more chance to reincarnate and come back to life. But before he can come back, God told him that PMP should ask *n* great men including prominent programmers about their life experiences.
The men are standing on a straight line. They are numbered 1 through... | The first line of input contains three space-separated integers *n*,<=*l*,<=*s* (2<=≤<=*n*<=≤<=105,<=0<=≤<=*l*<=<<=*n*,<=1<=≤<=*s*<=≤<=*n*) — the number of people to visit, the number left tickets PMP got, and initial location of PMP. Next line contains *n* space-separated integers. The *i*-th integer in this line i... | If PMP cannot visit all men with the tickets he got print -1 in the only line of output. Otherwise, in the first line you should print the minimum time PMP can visit all men. In the second line you should print *n*<=-<=1 integers that are the numbers of the men that PMP should visit in order in one optimal solution. If... | [
"5 2 2\n0 10 11 21 22\n",
"4 3 1\n0 1 2 3\n",
"7 3 2\n0 100 200 201 301 303 305\n"
] | [
"33\n1 3 5 4\n",
"-1\n",
"409\n1 3 4 7 6 5\n"
] | Let us remind here, a great contestant of all times, who left us about a year ago. May Renat Mullakhanov rest in peace. | [] | 92 | 0 | 0 | 432,001 | |
0 | none | [
"none"
] | null | null | Let's consider the following game. We have a rectangular field *n*<=×<=*m* in size. Some squares of the field contain chips.
Each chip has an arrow painted on it. Thus, each chip on the field points in one of the following directions: up, down, left or right.
The player may choose a chip and make a move with it.
The... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*,<=*n*<=×<=*m*<=≤<=5000). Then follow *n* lines containing *m* characters each — that is the game field description. "." means that this square is empty. "L", "R", "U", "D" mean that this square contains a chip and an arrow on it says left, right, up or d... | Print two numbers — the maximal number of points a player can get after a move and the number of moves that allow receiving this maximum number of points. | [
"4 4\nDRLD\nU.UL\n.UUR\nRDDL\n",
"3 5\n.D...\nRRRLL\n.U...\n"
] | [
"10 1",
"6 2"
] | In the first sample the maximum number of points is earned by the chip in the position (3, 3). You can see its progress at the following picture:
All other chips earn fewer points. | [] | 92 | 0 | 0 | 433,014 | |
514 | Darth Vader and Tree | [
"dp",
"matrices"
] | null | null | When Darth Vader gets bored, he sits down on the sofa, closes his eyes and thinks of an infinite rooted tree where each node has exactly *n* sons, at that for each node, the distance between it an its *i*-th left child equals to *d**i*. The Sith Lord loves counting the number of nodes in the tree that are at a distance... | The first line contains two space-separated integers *n* and *x* (1<=≤<=*n*<=≤<=105,<=0<=≤<=*x*<=≤<=109) — the number of children of each node and the distance from the root within the range of which you need to count the nodes.
The next line contains *n* space-separated integers *d**i* (1<=≤<=*d**i*<=≤<=100) — the le... | Print a single number — the number of vertexes in the tree at distance from the root equal to at most *x*. | [
"3 3\n1 2 3\n"
] | [
"8\n"
] | Pictures to the sample (the yellow color marks the nodes the distance to which is at most three) | [
{
"input": "3 3\n1 2 3",
"output": "8"
},
{
"input": "10 100000\n1 2 3 4 5 6 7 8 9 10",
"output": "334171350"
},
{
"input": "2 100\n1 2",
"output": "252403355"
},
{
"input": "10 100000\n1 1 2 2 3 3 4 4 5 5",
"output": "475936186"
},
{
"input": "5 100000\n1 1 1 1 1... | 62 | 0 | 0 | 434,589 | |
351 | Jeff and Removing Periods | [
"data structures"
] | null | null | Cosider a sequence, consisting of *n* integers: *a*1, *a*2, ..., *a**n*. Jeff can perform the following operation on sequence *a*:
- take three integers *v*, *t*, *k* (1<=≤<=*v*,<=*t*<=≤<=*n*; 0<=≤<=*k*; *v*<=+<=*tk*<=≤<=*n*), such that *a**v* = *a**v*<=+<=*t*, *a**v*<=+<=*t* = *a**v*<=+<=2*t*, ..., *a**v*<=+<=*t*(*k... | The first line contains integer *m* (1<=≤<=*m*<=≤<=105). The next line contains *m* integers *b*1, *b*2, ..., *b**m* (1<=≤<=*b**i*<=≤<=105).
The third line contains integer *q* (1<=≤<=*q*<=≤<=105) — the number of questions. The next *q* lines contain pairs of integers, *i*-th of them contains a pair of integers *l**i... | In *q* lines print the answers to Jeff's queries. Print the answers according to the order of questions in input. | [
"5\n2 2 1 1 2\n5\n1 5\n1 1\n2 2\n1 3\n2 3\n",
"10\n2 1 3 3 3 3 1 3 1 1\n10\n4 8\n2 10\n1 10\n4 4\n1 3\n2 4\n6 7\n1 9\n2 5\n1 1\n"
] | [
"2\n1\n1\n2\n2\n",
"2\n3\n3\n1\n3\n2\n2\n3\n2\n1\n"
] | none | [
{
"input": "5\n2 2 1 1 2\n5\n1 5\n1 1\n2 2\n1 3\n2 3",
"output": "2\n1\n1\n2\n2"
},
{
"input": "10\n2 1 3 3 3 3 1 3 1 1\n10\n4 8\n2 10\n1 10\n4 4\n1 3\n2 4\n6 7\n1 9\n2 5\n1 1",
"output": "2\n3\n3\n1\n3\n2\n2\n3\n2\n1"
},
{
"input": "15\n3 3 2 2 1 2 4 1 3 1 1 3 1 4 1\n15\n4 10\n8 9\n12 1... | 2,495 | 84,889,600 | 3 | 436,277 | |
348 | Pilgrims | [
"dfs and similar",
"dp",
"trees"
] | null | null | A long time ago there was a land called Dudeland. Dudeland consisted of *n* towns connected with *n*<=-<=1 bidirectonal roads. The towns are indexed from 1 to *n* and one can reach any city from any other city if he moves along the roads of the country. There are *m* monasteries in Dudeland located in *m* different tow... | The first line contains two integers *n* (3<=≤<=*n*<=≤<=105) and *m* (2<=≤<=*m*<=<<=*n*). The next line contains *m* distinct integers representing indices of towns that contain monasteries.
Next *n*<=-<=1 lines contain three integers each, *a**i*, *b**i*, *c**i*, indicating that there is an edge between towns *a**... | Output two integers: the maximum number of pilgrims Walter can make unhappy and the number of ways in which he can make his plan come true. | [
"8 5\n7 2 5 4 8\n1 2 1\n2 3 2\n1 4 1\n4 5 2\n1 6 1\n6 7 8\n6 8 10\n"
] | [
"5 1\n"
] | none | [] | 0 | 0 | -1 | 438,785 | |
460 | Roland and Rose | [
"brute force",
"geometry",
"math",
"sortings"
] | null | null | Roland loves growing flowers. He has recently grown a beautiful rose at point (0,<=0) of the Cartesian coordinate system. The rose is so beautiful that Roland is afraid that the evil forces can try and steal it.
To protect the rose, Roland wants to build *n* watch towers. Let's assume that a tower is a point on the p... | The first line contains two integers, *n* and *r* (2<=≤<=*n*<=≤<=8; 1<=≤<=*r*<=≤<=30). | In the first line print an integer — the maximum possible sum of squared distances. In the *i*-th of the following *n* lines print two integers, *x**i*,<=*y**i* — the coordinates of the *i*-th tower. Each tower must be inside or on the border of the circle with radius *r*. Note that there may be several towers located ... | [
"4 1\n",
"3 6\n"
] | [
"16\n0 1\n0 1\n0 -1\n0 -1\n",
"312\n0 6\n5 -3\n-5 -3\n"
] | none | [] | 46 | 0 | 0 | 439,228 | |
444 | DZY Loves Strings | [
"binary search",
"hashing",
"strings",
"two pointers"
] | null | null | DZY loves strings, and he enjoys collecting them.
In China, many people like to use strings containing their names' initials, for example: xyz, jcvb, dzy, dyh.
Once DZY found a lucky string *s*. A lot of pairs of good friends came to DZY when they heard about the news. The first member of the *i*-th pair has name *a*... | The first line contains a string *s* (1<=≤<=|*s*|<=≤<=50000).
The second line contains a non-negative integer *q* (0<=≤<=*q*<=≤<=100000) — the number of pairs. Each of the next *q* lines describes a pair, the line contains two space-separated strings *a**i* and *b**i* (1<=≤<=|*a**i*|,<=|*b**i*|<=≤<=4).
It is guarante... | For each pair, print a line containing a single integer — the minimum length of the required substring. If there is no such substring, output -1. | [
"xudyhduxyz\n3\nxyz xyz\ndyh xyz\ndzy xyz\n",
"abcabd\n3\na c\nab abc\nab d\n",
"baabcabaaa\n2\nabca baa\naa aba\n"
] | [
"3\n8\n-1\n",
"2\n3\n3\n",
"6\n4\n"
] | The shortest substrings in the first sample are: xyz, dyhduxyz.
The shortest substrings in the second sample are: ca, abc and abd.
The shortest substrings in the third sample are: baabca and abaa. | [] | 30 | 0 | 0 | 439,966 | |
67 | Save the City! | [
"geometry"
] | E. Save the City! | 1 | 256 | In the town of Aalam-Aara (meaning the Light of the Earth), previously there was no crime, no criminals but as the time progressed, sins started creeping into the hearts of once righteous people. Seeking solution to the problem, some of the elders found that as long as the corrupted part of population was kept away fro... | The first line of the test case will consist of the number of vertices *n* (3<=≤<=*n*<=≤<=1000).
Next *n* lines will contain the coordinates of the vertices in the clockwise order of the polygon. On the *i*-th line are integers *x**i* and *y**i* (0<=≤<=*x**i*,<=*y**i*<=≤<=106) separated by a space.
The endpoints of t... | Output consists of a single line containing the number of points where the watchtower can be set up. | [
"5\n4 8\n8 8\n9 4\n4 0\n0 4\n",
"5\n4 8\n5 8\n5 4\n7 4\n2 2\n"
] | [
"5\n",
"0\n"
] | Figure 2 shows the first test case. All the points in the figure are watchable from any point on fence *AB*. Since, *AB* has 5 integer coordinates, so answer is 5.
For case two, fence *CD* and *DE* are not completely visible, thus answer is 0. | [] | 186 | 307,200 | 0 | 440,018 |
730 | Bulmart | [
"binary search",
"dfs and similar"
] | null | null | A new trade empire is rising in Berland. Bulmart, an emerging trade giant, decided to dominate the market of ... shovels! And now almost every city in Berland has a Bulmart store, and some cities even have several of them! The only problem is, at the moment sales are ... let's say a little below estimates. Some people ... | The first line contains two integers *n*, *m* (1<=≤<=*n*<=≤<=5000, 0<=≤<=*m*<=≤<=*min*(5000,<=*n*·(*n*<=-<=1)<=/<=2)). Each of the next *m* lines contains two integers *x**e* and *y**e*, meaning that the *e*-th road connects cities *x**e* and *y**e* (1<=≤<=*x**e*,<=*y**e*<=≤<=*n*).
The next line contains a single inte... | Output *q* lines. On the *j*-th line, print an answer for the *j*-th query — the minimum amount of time needed to deliver *r**j* shovels to the customer in city *g**j* spending no more than *a**j* burles. Print -1 if there is no solution for the *j*-th query. | [
"6 4\n4 2\n5 4\n1 2\n3 2\n2\n4 1 2\n3 2 3\n6\n1 2 6\n2 3 7\n3 1 2\n4 3 8\n5 2 5\n6 1 10\n"
] | [
"2\n-1\n2\n2\n3\n-1\n"
] | none | [] | 46 | 307,200 | 0 | 441,266 | |
261 | Maxim and Calculator | [
"brute force",
"dp",
"two pointers"
] | null | null | Maxim has got a calculator. The calculator has two integer cells. Initially, the first cell contains number 1, and the second cell contains number 0. In one move you can perform one of the following operations:
1. Let's assume that at the current time the first cell contains number *a*, and the second cell contains n... | The first line contains three integers: *l*, *r*, *p* (2<=≤<=*l*<=≤<=*r*<=≤<=109,<=1<=≤<=*p*<=≤<=100).
The numbers in the line are separated by single spaces. | In a single line print a single integer — the answer to the problem. | [
"2 10 3\n",
"2 111 100\n",
"2 111 11\n"
] | [
"1\n",
"106\n",
"47\n"
] | none | [] | 92 | 0 | 0 | 443,531 | |
232 | Fence | [
"binary search",
"data structures",
"string suffix structures"
] | null | null | John Doe has a crooked fence, consisting of *n* rectangular planks, lined up from the left to the right: the plank that goes *i*-th (1<=≤<=*i*<=≤<=*n*) (from left to right) has width 1 and height *h**i*. We will assume that the plank that goes *i*-th (1<=≤<=*i*<=≤<=*n*) (from left to right) has index *i*.
A piece of t... | The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the number of wood planks in the fence. The second line contains *n* space-separated integers *h*1,<=*h*2,<=...,<=*h**n* (1<=≤<=*h**i*<=≤<=109) — the heights of fence planks.
The third line contains integer *q* (1<=≤<=*q*<=≤<=105) — the number of queries. Next ... | For each query on a single line print a single integer — the number of pieces of the fence that match the given one. Print the answers to the queries in the order, in which the queries are given in the input. | [
"10\n1 2 2 1 100 99 99 100 100 100\n6\n1 4\n1 2\n3 4\n1 5\n9 10\n10 10\n"
] | [
"1\n2\n2\n0\n2\n9\n"
] | none | [] | 62 | 0 | 0 | 445,096 | |
131 | Subway | [
"dfs and similar",
"graphs"
] | null | null | A subway scheme, classic for all Berland cities is represented by a set of *n* stations connected by *n* passages, each of which connects exactly two stations and does not pass through any others. Besides, in the classic scheme one can get from any station to any other one along the passages. The passages can be used t... | The first line contains an integer *n* (3<=≤<=*n*<=≤<=3000), *n* is the number of stations (and trains at the same time) in the subway scheme. Then *n* lines contain descriptions of the trains, one per line. Each line contains a pair of integers *x**i*,<=*y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*) and represents the presenc... | Print *n* numbers. Separate the numbers by spaces, the *i*-th one should be equal to the distance of the *i*-th station from the ringroad. For the ringroad stations print number 0. | [
"4\n1 3\n4 3\n4 2\n1 2\n",
"6\n1 2\n3 4\n6 4\n2 3\n1 3\n3 5\n"
] | [
"0 0 0 0 ",
"0 0 0 1 1 2 "
] | none | [] | 92 | 0 | 0 | 445,342 | |
48 | Galaxy Union | [
"dp",
"trees",
"two pointers"
] | G. Galaxy Union | 3 | 256 | In a far away galaxy there are *n* inhabited planets numbered with numbers from 1 to *n*. One day the presidents of all the *n* planets independently from each other came up with an idea of creating the Galaxy Union. Now they need to share this wonderful idea with their galaxymates, that’s why each president is busy wo... | The first line contains an integer *n* (3<=≤<=*n*<=≤<=200000) which represents the number of planets in the Galaxy and the number of communication channels equal to it. The next *n* lines contain three integers each *a**i*, *b**i* and *t**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i*<=≠<=*b**i*,<=1<=≤<=*t**i*<=≤<=103) tha... | In the first line output *n* integers — the durations of the supposed negotiations for each president. Separate the numbers by spaces. | [
"3\n1 2 3\n2 3 2\n1 3 1\n",
"3\n1 2 3\n2 3 2\n1 3 5\n",
"4\n1 2 3\n2 3 2\n3 4 1\n4 1 4\n"
] | [
"4 5 3\n",
"8 5 7\n",
"12 8 8 8\n"
] | none | [] | 62 | 0 | 0 | 446,289 |
367 | Sereja and Sets | [
"bitmasks",
"dfs and similar"
] | null | null | Sereja has *m* non-empty sets of integers *A*1,<=*A*2,<=...,<=*A**m*. What a lucky coincidence! The given sets are a partition of the set of all integers from 1 to *n*. In other words, for any integer *v* (1<=≤<=*v*<=≤<=*n*) there is exactly one set *A**t* such that . Also Sereja has integer *d*.
Sereja decided to cho... | The first line contains integers *n*, *m*, *d* (1<=≤<=*d*<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=20). The next *m* lines contain sets. The first number in the *i*-th line is *s**i* (1<=≤<=*s**i*<=≤<=*n*). This number denotes the size of the *i*-th set. Then the line contains *s**i* distinct integers from 1 to *n* — set *A**i*... | In a single line print the answer to the problem — the minimum value *k* at the right choice. | [
"3 2 2\n1 2\n2 1 3\n",
"5 1 1\n5 4 5 3 2 1\n",
"7 3 1\n4 1 3 5 7\n2 2 6\n1 4\n"
] | [
"1\n",
"1\n",
"3\n"
] | none | [] | 93 | 23,756,800 | 0 | 447,466 | |
0 | none | [
"none"
] | null | null | Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's descripti... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of operators. Then follow *n* lines with operators. Each operator is of one of two types: either "typedef *A* *B*", or "typeof *A*". In the first case the *B* type differs from void and errtype types, and besides, doesn't have any asterisks and amp... | For every typeof operator print on the single line the answer to that operator — the type that the given operator returned. | [
"5\ntypedef void* ptv\ntypeof ptv\ntypedef &&ptv node\ntypeof node\ntypeof &ptv\n",
"17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\ntypeof b\ntypedef b******* c\ntypeof c\n... | [
"void*\nerrtype\nvoid\n",
"void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype\n"
] | Let's look at the second sample.
After the first two queries typedef the b type is equivalent to void*, and с — to void**.
The next query typedef redefines b — it is now equal to &b = &void* = void. At that, the с type doesn't change.
After that the с type is defined as &&b* = &&void* = &... | [
{
"input": "5\ntypedef void* ptv\ntypeof ptv\ntypedef &&ptv node\ntypeof node\ntypeof &ptv",
"output": "void*\nerrtype\nvoid"
},
{
"input": "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\nty... | 60 | 0 | 0 | 447,931 | |
690 | Recover Polygon (medium) | [
"geometry"
] | null | null | Now that Heidi has made sure her Zombie Contamination level checker works, it's time to strike! This time, the zombie lair is a strictly convex polygon on the lattice. Each vertex of the polygon occupies a point on the lattice. For each cell of the lattice, Heidi knows the level of Zombie Contamination – the number of ... | The input contains multiple test cases.
The first line of each test case contains one integer *N*, the size of the lattice grid (5<=≤<=*N*<=≤<=500). The next *N* lines each contain *N* characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between... | For each test case, give the following output:
The first line of the output should contain one integer *V*, the number of vertices of the polygon that is the secret lair. The next *V* lines each should contain two integers, denoting the vertices of the polygon in the clockwise order, starting from the lexicographicall... | [
"8\n00000000\n00000110\n00012210\n01234200\n02444200\n01223200\n00001100\n00000000\n5\n00000\n01210\n02420\n01210\n00000\n7\n0000000\n0122100\n0134200\n0013200\n0002200\n0001100\n0000000\n0\n"
] | [
"4\n2 3\n2 4\n6 6\n5 2\n4\n2 2\n2 3\n3 3\n3 2\n3\n2 5\n4 5\n4 2\n"
] | It is guaranteed that the solution always exists and is unique. It is guaranteed that in the correct solution the coordinates of the polygon vertices are between 2 and *N* - 2. A vertex (*x*<sub class="lower-index">1</sub>, *y*<sub class="lower-index">1</sub>) is lexicographically smaller than vertex (*x*<sub class="lo... | [] | 93 | 204,800 | 0 | 448,189 | |
0 | none | [
"none"
] | null | null | You are given a connected weighted graph with *n* vertices and *m* edges. The graph doesn't contain loops nor multiple edges. Consider some edge with id *i*. Let's determine for this edge the maximum integer weight we can give to it so that it is contained in all minimum spanning trees of the graph if we don't change t... | The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=2·105, *n*<=-<=1<=≤<=*m*<=≤<=2·105), where *n* and *m* are the number of vertices and the number of edges in the graph, respectively.
Each of the next *m* lines contains three integers *u*, *v* and *c* (1<=≤<=*v*,<=*u*<=≤<=*n*, *v*<=≠<=*u*, 1<=≤<=*c*<=≤<=... | Print the answer for each edge in the order the edges are given in the input. If an edge is contained in every minimum spanning tree with any weight, print -1 as the answer. | [
"4 4\n1 2 2\n2 3 2\n3 4 2\n4 1 3\n",
"4 3\n1 2 2\n2 3 2\n3 4 2\n"
] | [
"2 2 2 1 ",
"-1 -1 -1 "
] | none | [] | 15 | 0 | 0 | 450,702 | |
578 | Mirror Box | [
"matrices",
"trees"
] | null | null | You are given a box full of mirrors. Box consists of grid of size *n*<=×<=*m*. Each cell of the grid contains a mirror put in the shape of '\' or '<=/<=' (45 degree to the horizontal or vertical line). But mirrors in some cells have been destroyed. You want to put new mirrors into these grids so that the following two ... | The first line contains three integers *n*, *m*, *MOD* (1<=≤<=*n*,<=*m*<=≤<=100, 3<=≤<=*MOD*<=≤<=109<=+<=7, *MOD* is prime), *m*, *n* indicates the dimensions of a box and *MOD* is the number to module the answer.
The following *n* lines each contains a string of length *m*. Each string contains only '<=/<=', '\', '*'... | Output the answer modulo *MOD*. | [
"2 2 1000000007\n*/\n/*\n",
"2 2 1000000007\n**\n\\\\",
"2 2 3\n**\n**\n"
] | [
"1\n",
"1",
"2\n"
] | The only way for sample 1 is shown on the left picture from the statement.
The only way for sample 2 is shown on the right picture from the statement.
For the third sample, there are 5 possibilities that are listed below:
1.
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8d9b0974a3bf5... | [] | 30 | 0 | 0 | 452,746 | |
379 | New Year Tree Decorations | [
"geometry",
"schedules",
"sortings"
] | null | null | Due to atheistic Soviet past, Christmas wasn't officially celebrated in Russia for most of the twentieth century. As a result, the Russian traditions for Christmas and New Year mixed into one event celebrated on the New Year but including the tree, a Santa-like 'Grandfather Frost', presents and huge family reunions and... | The first line contains two integers, *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=300). Each of the following *n* lines contains *k*<=+<=1 integers — the description of the polyline. If the *i*-th line contains ontegers *y**i*,<=0,<=*y**i*,<=1,<=...,<=*y**i*,<=*k*, that means that the polyline of the *i*-th piece goes through poi... | Print *n* real numbers — for each polyline, the area of its visible part.
The answer will be considered correct if its relative or absolute error do not exceed 10<=-<=4. | [
"2 2\n2 1 2\n1 2 1\n",
"1 1\n1 1\n",
"4 1\n2 7\n7 2\n5 5\n6 4\n"
] | [
"3.000000000000\n0.500000000000\n",
"1.000000000000\n",
"4.500000000000\n1.250000000000\n0.050000000000\n0.016666666667\n"
] | none | [] | 1,000 | 5,222,400 | 0 | 453,844 | |
204 | Little Elephant and Retro Strings | [
"dp"
] | null | null | The Little Elephant has found a ragged old black-and-white string *s* on the attic.
The characters of string *s* are numbered from the left to the right from 1 to |*s*|, where |*s*| is the length of the string. Let's denote the *i*-th character of string *s* as *s**i*. As the string is black-and-white, each character ... | The first line contains two space-separated integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=≤<=106). The second line contains string *s*. String *s* has length *n* and only consists of characters "W", "B" and "X". | On a single line print an integer — the answer to the problem modulo 1000000007 (109<=+<=7). | [
"3 2\nXXX\n",
"4 2\nXXXX\n",
"10 2\nXXBXXWXXXX\n"
] | [
"0\n",
"1\n",
"166\n"
] | none | [
{
"input": "3 2\nXXX",
"output": "0"
},
{
"input": "4 2\nXXXX",
"output": "1"
},
{
"input": "10 2\nXXBXXWXXXX",
"output": "166"
},
{
"input": "3 1\nXBX",
"output": "2"
},
{
"input": "1 1\nX",
"output": "0"
},
{
"input": "2 1\nXW",
"output": "1"
}... | 92 | 0 | 0 | 454,664 | |
611 | New Year and Forgotten Tree | [
"constructive algorithms",
"flows",
"graphs"
] | null | null | A tree is a connected undirected graph with *n*<=-<=1 edges, where *n* denotes the number of vertices. Vertices are numbered 1 through *n*.
Limak is a little polar bear. His bear family prepares a New Year tree every year. One year ago their tree was more awesome than usually. Thus, they decided to prepare the same tr... | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000) — the number of vertices.
Each of the next *n*<=-<=1 lines contains two space-separated non-empty strings, both consisting of questions marks only. No string has more characters than the number of digits in *n*. | If there is no tree matching Limak's records, print the only line with "-1" (without the quotes).
Otherwise, describe any tree matching Limak's notes. Print *n*<=-<=1 lines, each with two space-separated integers – indices of vertices connected by an edge. You can print edges in any order. | [
"12\n? ?\n? ?\n? ?\n? ??\n?? ?\n?? ??\n? ??\n? ?\n? ?\n? ?\n? ?\n",
"12\n?? ??\n? ?\n? ?\n? ??\n?? ?\n?? ??\n? ??\n? ?\n? ?\n?? ??\n? ?\n"
] | [
"3 1\n1 6\n9 1\n2 10\n1 7\n8 1\n1 4\n1 10\n5 1\n10 11\n12 1\n",
"-1\n"
] | none | [] | 30 | 0 | 0 | 455,803 | |
62 | World Evil | [
"dp",
"flows"
] | E. World Evil | 5 | 256 | As a result of Pinky and Brain's mysterious experiments in the Large Hadron Collider some portals or black holes opened to the parallel dimension. And the World Evil has crept to the veil between their world and ours. Brain quickly evaluated the situation and he understood that the more evil tentacles creep out and bec... | The first line of the input file contains two integers *n* and *m* (2<=≤<=*n*<=≤<=5, 2<=≤<=*m*<=≤<=105). They are the sizes of the Large Hadron Collider grid. The next *m*<=-<=1 lines contain *n* numbers each. They are the horizontal corridors' capacities. The next *m* lines contain *n* numbers each. They are the verti... | Print a single number, the number of the World Evil tentacles Pinky and Brain will command.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | [
"3 4\n4 4 4\n1 1 5\n5 5 3\n4 1 2\n1 3 1\n3 5 4\n1 4 3\n",
"2 2\n9 2\n2 3\n6 1\n"
] | [
"7\n",
"11\n"
] | none | [] | 92 | 0 | 0 | 459,259 |
446 | DZY Loves Games | [
"math",
"matrices",
"probabilities"
] | null | null | Today DZY begins to play an old game. In this game, he is in a big maze with *n* rooms connected by *m* corridors (each corridor allows to move in both directions). You can assume that all the rooms are connected with corridors directly or indirectly.
DZY has got lost in the maze. Currently he is in the first room and... | The first line contains three integers *n*,<=*m*,<=*k* (2<=≤<=*n*<=≤<=500; 1<=≤<=*m*<=≤<=105; 2<=≤<=*k*<=≤<=109).
The second line contains *n* integers, each of them is either 0 or 1. If the *i*-th number is 1, then the *i*-th room has a trap, otherwise it has not a trap. Please note, that the number of rooms with a t... | Print the only real number — the probability for DZY to open the bonus round. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=4. | [
"5 5 3\n0 0 1 0 1\n1 2\n2 3\n3 4\n4 5\n1 2\n",
"3 2 2\n0 1 1\n1 2\n2 3\n",
"2 1 3\n0 1\n1 2\n"
] | [
"0.25000000\n",
"-0.00000000\n",
"1.00000000\n"
] | none | [] | 30 | 0 | 0 | 460,553 | |
482 | Random Function and Tree | [
"combinatorics",
"dp",
"trees"
] | null | null | You have a rooted tree consisting of *n* vertices. Let's number them with integers from 1 to *n* inclusive. The root of the tree is the vertex 1. For each *i*<=><=1 direct parent of the vertex *i* is *p**i*. We say that vertex *i* is child for its direct parent *p**i*.
You have initially painted all the vertices wi... | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=105) — the number of vertexes in the tree.
The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≤<=*p**i*<=<<=*i*). Number *p**i* is the parent of vertex *i*. | Print a single integer — the answer to the problem modulo 1000000007 (109<=+<=7) | [
"4\n1 2 1\n",
"3\n1 1\n"
] | [
"8\n",
"5\n"
] | All possible coloring patterns of the first sample are given below. | [] | 46 | 0 | 0 | 461,522 | |
97 | Robot in Basement | [
"bitmasks",
"brute force",
"implementation"
] | D. Robot in Basement | 4 | 256 | The Professor has lost his home robot yet again. After some thinking Professor understood that he had left the robot in the basement.
The basement in Professor's house is represented by a rectangle *n*<=×<=*m*, split into 1<=×<=1 squares. Some squares are walls which are impassable; other squares are passable. You can... | The first line contains three integers *n*, *m* and *k* (3<=≤<=*n*,<=*m*<=≤<=150, 1<=≤<=*k*<=≤<=105). Next *n* lines contain *m* characters each — that is the Professor's basement's description: "#" stands for a wall, "." stands for a passable square and "E" stands for the exit from the basement (this square also is pa... | Print in the output file the length of the smallest possible prefix that will lead the robot to the exit square. In other words, wherever the robot had been positioned initially, it should be positioned in the exit square after all the commands from the prefix are fulfilled (during doing commands the robot can come and... | [
"5 5 7\n#####\n#...#\n#...#\n#E..#\n#####\nUULLDDR\n",
"5 5 7\n#####\n#.#.#\n#...#\n#E..#\n#####\nUULLDDR\n",
"5 3 2\n###\n#.#\n#.#\n#E#\n###\nDD\n"
] | [
"6\n",
"-1\n",
"2\n"
] | none | [] | 60 | 0 | 0 | 465,847 |
997 | Good Subsegments | [
"data structures"
] | null | null | A permutation $p$ of length $n$ is a sequence $p_1, p_2, \ldots, p_n$ consisting of $n$ distinct integers, each of which from $1$ to $n$ ($1 \leq p_i \leq n$) .
Let's call the subsegment $[l,r]$ of the permutation good if all numbers from the minimum on it to the maximum on this subsegment occur among the numbers $p_l... | The first line contains a single integer $n$ ($1 \leq n \leq 120000$) — the number of elements in the permutation.
The second line contains $n$ distinct integers $p_1, p_2, \ldots, p_n$ separated by spaces ($1 \leq p_i \leq n$).
The third line contains an integer $q$ ($1 \leq q \leq 120000$) — number of queries.
The... | Print a $q$ lines, $i$-th of them should contain a number of good subsegments of a segment, given in the $i$-th query. | [
"5\n1 3 2 5 4\n15\n1 1\n1 2\n1 3\n1 4\n1 5\n2 2\n2 3\n2 4\n2 5\n3 3\n3 4\n3 5\n4 4\n4 5\n5 5\n"
] | [
"1\n2\n5\n6\n10\n1\n3\n4\n7\n1\n2\n4\n1\n3\n1\n"
] | none | [] | 93 | 0 | -1 | 466,785 | |
513 | Scaygerboss | [
"flows"
] | null | null | Cthulhu decided to catch Scaygerboss. Scaygerboss found it out and is trying to hide in a pack of his scaygers. Each scayger except Scaygerboss is either a male or a female. Scaygerboss's gender is "other".
Scaygers are scattered on a two-dimensional map divided into cells. A scayger looks nerdy and loveable if it is ... | The first line contains 4 integers: *n*, *m*, *males*, *females* (0<=≤<=*males*,<=*females*<=≤<=*n*·*m*). *n* and *m* are dimensions of the map; *males* and *females* are numbers of male scaygers and female scaygers.
Next *n* lines describe the map. Each of these lines contains *m* characters. Character '.' stands for... | Output the minimum possible time it takes to make all scaygers look nerdy and loveable or -1 if it is impossible. | [
"4 4 2 3\n....\n.###\n####\n####\n2 1 1\n2 1 2\n2 1 2\n2 1 2\n2 1 2\n1 1 2\n",
"2 4 2 2\n....\n.###\n2 1 1\n2 1 2\n2 1 2\n2 1 2\n2 1 2\n"
] | [
"2\n",
"-1\n"
] | Consider the first sample test. The scaygers are hiding on a 4 by 4 map. Scaygerboss initially resides in the cell (2, 1) and can move between cells in 1 unit of time. There are also 2 male and 3 female scaygers on the map. One of the females initially is in the cell (1, 1), and all the other scaygers are in the cell (... | [] | 46 | 0 | 0 | 469,138 | |
487 | Conveyor Belts | [
"data structures"
] | null | null | Automatic Bakery of Cyberland (ABC) recently bought an *n*<=×<=*m* rectangle table. To serve the diners, ABC placed seats around the table. The size of each seat is equal to a unit square, so there are 2(*n*<=+<=*m*) seats in total.
ABC placed conveyor belts on each unit square on the table. There are three types of c... | The first line of input contains three integers *n*, *m* and *q* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=10,<=1<=≤<=*q*<=≤<=105), separated by a space.
Next *n* lines, each line contains *m* characters, describing the table. The characters can only be one of "<^>".
Next *q* lines, each line describes an event. The f... | For each event of type "A", output two integers *tx*, *ty* in a line, separated by a space, denoting the destination of (*x*,<=*y*) is (*tx*,<=*ty*).
If there is an infinite loop, you should output *tx*<==<=*ty*<==<=<=-<=1. | [
"2 2 3\n>>\n^^\nA 2 1\nC 1 2 <\nA 2 1\n",
"4 5 7\n><<^<\n^<^^>\n>>>^>\n>^>>^\nA 3 1\nA 2 2\nC 1 4 <\nA 3 1\nC 1 2 ^\nA 3 1\nA 2 2"
] | [
"1 3\n-1 -1\n",
"0 4\n-1 -1\n-1 -1\n0 2\n0 2\n"
] | For the first sample:
If the bread goes from (2, 1), it will go out of the table at (1, 3).
After changing the conveyor belt of (1, 2) to "<", when the bread goes from (2, 1) again, it will get stuck at "><", so output is ( - 1, - 1). | [] | 3,000 | 15,052,800 | 0 | 469,658 | |
650 | Clockwork Bomb | [
"data structures",
"dfs and similar",
"dsu",
"greedy",
"trees"
] | null | null | My name is James diGriz, I'm the most clever robber and treasure hunter in the whole galaxy. There are books written about my adventures and songs about my operations, though you were able to catch me up in a pretty awkward moment.
I was able to hide from cameras, outsmart all the guards and pass numerous traps, but w... | The first line of the input contains *n* (2<=≤<=*n*<=≤<=500<=000), the number of contacts.
Each of the following *n*<=-<=1 lines contains two of integers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=≠<=*y**i*) denoting the contacts currently connected by the *i*-th wire.
The remaining *n*<=-<=1 lines con... | The first line should contain *k* (*k*<=≥<=0) — the minimum number of moves of unplugging and plugging back some wire required to defuse the bomb.
In each of the following *k* lines output four integers *a**i*, *b**i*, *c**i*, *d**i* meaning that on the *i*-th step it is neccesary to unplug the wire connecting the con... | [
"3\n1 2\n2 3\n1 3\n3 2\n",
"4\n1 2\n2 3\n3 4\n2 4\n4 1\n1 3\n"
] | [
"1\n1 2 1 3\n",
"3\n1 2 1 3\n4 3 4 1\n2 3 2 4\n"
] | Picture with the clarification for the sample tests: | [] | 46 | 5,017,600 | 0 | 469,868 | |
472 | Design Tutorial: Change the Goal | [
"constructive algorithms",
"math",
"matrices"
] | null | null | There are some tasks which have the following structure: you are given a model, and you can do some operations, you should use these operations to achive the goal. One way to create a new task is to use the same model and same operations, but change the goal.
Let's have a try. I have created the following task for Top... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=10000). The second line contains *n* integers: *x*1 to *x**n* (0<=≤<=*x**i*<=≤<=109). The third line contains *n* integers: *y*1 to *y**n* (0<=≤<=*y**i*<=≤<=109). | If there is no solution, output -1.
If there is a solution, then in the first line output an integer *m* (0<=≤<=*m*<=≤<=1000000) – the number of assignments you need to perform. Then print *m* lines, each line should contain two integers *i* and *j* (1<=≤<=*i*,<=*j*<=≤<=*n*), which denote assignment *x**i* ^= *x**j*.
... | [
"2\n3 5\n6 0\n",
"5\n0 0 0 0 0\n1 2 3 4 5\n",
"3\n4 5 6\n1 2 3\n",
"3\n1 2 3\n4 5 6\n"
] | [
"2\n1 2\n2 2\n",
"-1\n",
"5\n3 1\n1 2\n2 2\n2 3\n3 1\n",
"-1\n"
] | Assignment *a* ^= *b* denotes assignment *a* = *a* ^ *b*, where operation "^" is bitwise XOR of two integers. | [] | 46 | 0 | 0 | 475,227 | |
217 | Formurosa | [
"divide and conquer",
"dp",
"expression parsing"
] | null | null | The Bytelandian Institute for Biological Research (BIBR) is investigating the properties of two species of bacteria, named simply 0 and 1. Even under a microscope, bacteria of those two species are very difficult to distinguish. In fact, the only thing the scientists possess that is able to differentiate between them i... | The first line of input contains a single integer *n* (2<=≤<=*n*<=≤<=106) — the number of colonies of bacteria.
The second line contains the formula describing the nutrition process of Formurosa. This line contains only characters «0», «1», «?», «|», «&», «^», «(», «)» and complies with the following grammar:
The... | If it is always possible to determine the species of each colony, output "YES" (without quotes). Otherwise, output "NO" (without quotes). | [
"2\n(?^?)\n",
"10\n?\n",
"2\n((?^?)&?)\n"
] | [
"NO\n",
"YES\n",
"YES\n"
] | none | [] | 30 | 0 | 0 | 475,336 | |
65 | Harry Potter and Moving Staircases | [
"dfs and similar",
"implementation"
] | E. Harry Potter and Moving Staircases | 2 | 256 | Harry Potter lost his Invisibility Cloak, running from the school caretaker Filch. Finding an invisible object is not an easy task. Fortunately, Harry has friends who are willing to help. Hermione Granger had read "The Invisibility Cloaks, and Everything about Them", as well as six volumes of "The Encyclopedia of Quick... | The first line contains integers *n* and *m* (1<=≤<=*n*<=≤<=100000, 0<=≤<=*m*<=≤<=200000), which are the number of floors and staircases in Hogwarts, respectively. The following *m* lines contain pairs of floors connected by staircases at the initial moment of time. | In the first line print "YES" (without the quotes) if Harry is able to search all the floors, and "NO" otherwise. If the answer is positive, then print on the second line the number of staircases that Ron and Hermione will have to shift. Further output should look like this:
Harry's moves
a staircase's move
Harry'... | [
"6 4\n1 2\n1 3\n2 3\n4 5\n",
"4 1\n1 2\n",
"5 5\n1 2\n1 3\n3 4\n3 5\n4 5\n"
] | [
"YES\n2\n3 1 2 3\n2 3 5\n3 5 4 5\n4 5 6\n3 6 5 3\n",
"NO\n",
"YES\n0\n6 1 2 1 3 4 5\n"
] | none | [] | 46 | 0 | 0 | 477,877 |
286 | Ladies' Shop | [
"constructive algorithms",
"fft",
"math"
] | null | null | A ladies' shop has recently opened in the city of Ultima Thule. To get ready for the opening, the shop bought *n* bags. Each bag is characterised by the total weight *a**i* of the items you can put there. The weird thing is, you cannot use these bags to put a set of items with the total weight strictly less than *a**i*... | The first line contains space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106). The second line contains *n* distinct space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a*1<=<<=*a*2<=<<=...<=<<=*a**n*<=≤<=*m*) — the bags' weight limits. | In the first line print "NO" (without the quotes) if there isn't set *p**i*, that would meet the conditions.
Otherwise, in the first line print "YES" (without the quotes), in the second line print an integer *k* (showing how many numbers are in the suitable set with the minimum number of weights), in the third line pr... | [
"6 10\n5 6 7 8 9 10\n",
"1 10\n1\n",
"1 10\n6\n"
] | [
"YES\n5\n5 6 7 8 9 \n",
"NO\n",
"YES\n1\n6 \n"
] | none | [] | 60 | 0 | 0 | 478,498 | |
356 | Xenia and String Problem | [
"dp",
"hashing",
"implementation",
"string suffix structures",
"strings"
] | null | null | Xenia the coder went to The Olympiad of Informatics and got a string problem. Unfortunately, Xenia isn't fabulous in string algorithms. Help her solve the problem.
String *s* is a sequence of characters *s*1*s*2... *s*|*s*|, where record |*s*| shows the length of the string.
Substring *s*[*i*... *j*] of string *s* i... | The first line contains a non-empty string *t* (1<=≤<=|*t*|<=≤<=105). String *t* only consists of lowercase English letters. | Print the sought maximum beauty value Xenia can get.
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. | [
"zzz\n",
"aba\n",
"abacaba\n",
"aaaaaa\n"
] | [
"12\n",
"12\n",
"83\n",
"15\n"
] | In the first test sample the given string can be transformed into string *p* = "zbz". Such string contains Gray strings as substrings *p*[1... 1], *p*[2... 2], *p*[3... 3] и *p*[1... 3]. In total, the beauty of string *p* gets equal to 1<sup class="upper-index">2</sup> + 1<sup class="upper-index">2</sup> + 1<sup class=... | [] | 46 | 0 | 0 | 479,796 | |
68 | Synchrophasotron | [
"brute force"
] | C. Synchrophasotron | 3 | 256 | For some experiments little Petya needs a synchrophasotron. He has already got the device, all that's left is to set the fuel supply. Fuel comes through a system of nodes numbered from 1 to *n* and connected by pipes. Pipes go from every node with smaller number to every node with greater number. Fuel can only flow thr... | First line contains integer *n* (2<=≤<=*n*<=≤<=6), which represents the number of nodes. Each of the next *n*(*n*<=-<=1)<=/<=2 lines contains five integers *s*,<=*f*,<=*l*,<=*h*,<=*a* that describe pipes — the first node of the pipe, the second node of the pipe, the minimum and the maximum amount of fuel that can flow ... | Output in the first line two space-separated numbers: the minimum possible amount of fuel that can flow into the synchrophasotron, and the maximum possible sum that needs to be paid in order for that amount of fuel to reach synchrophasotron. If there is no amount of fuel that can reach synchrophasotron, output "-1 -1".... | [
"2\n1 2 1 2 3\n",
"3\n1 2 1 2 3\n1 3 0 0 0\n2 3 3 4 5\n",
"4\n1 2 0 2 1\n2 3 0 2 1\n1 3 0 2 6\n1 4 0 0 1\n2 4 0 0 0\n3 4 2 3 0\n",
"3\n1 2 0 2 1\n1 3 1 2 1\n2 3 1 2 1\n"
] | [
"1 4\n",
"-1 -1\n",
"2 15\n",
"2 6\n"
] | In the first test, we can either pass 1 or 2 units of fuel from node 1 to node 2. The minimum possible amount is 1, it costs *a*<sub class="lower-index">12</sub> + 1<sup class="upper-index">2</sup> = 4.
In the second test, you can pass at most 2 units from node 1 to node 2, and at you have to pass at least 3 units fro... | [
{
"input": "2\n1 2 1 2 3",
"output": "1 4"
},
{
"input": "3\n1 2 1 2 3\n1 3 0 0 0\n2 3 3 4 5",
"output": "-1 -1"
},
{
"input": "4\n1 2 0 2 1\n2 3 0 2 1\n1 3 0 2 6\n1 4 0 0 1\n2 4 0 0 0\n3 4 2 3 0",
"output": "2 15"
},
{
"input": "3\n1 2 0 2 1\n1 3 1 2 1\n2 3 1 2 1",
"outp... | 62 | 0 | 0 | 480,553 |
253 | Table with Letters - 2 | [
"brute force",
"two pointers"
] | null | null | Vasya has recently started to learn English. Now he needs to remember how to write English letters. He isn't sure about some of them, so he decided to train a little.
He found a sheet of squared paper and began writing arbitrary English letters there. In the end Vasya wrote *n* lines containing *m* characters each. Th... | The first line contains three integers *n*,<=*m*,<=*k* (2<=≤<=*n*,<=*m*<=≤<=400; 0<=≤<=*k*<=≤<=*n*·*m*).
Next *n* lines contain *m* characters each — the given table. Each character of the table is a lowercase English letter. | Print a single integer — the number of required subtables. | [
"3 4 4\naabb\nbaab\nbaab\n",
"4 5 1\nababa\nccaca\nccacb\ncbabc\n"
] | [
"2\n",
"1\n"
] | There are two suitable subtables in the first sample: the first one's upper left corner is cell (2, 2) and lower right corner is cell (3, 3), the second one's upper left corner is cell (2, 1) and lower right corner is cell (3, 4). | [] | 92 | 0 | 0 | 480,635 | |
842 | Nikita and game | [
"binary search",
"dfs and similar",
"divide and conquer",
"graphs",
"trees"
] | null | null | Nikita plays a new computer game. There are *m* levels in this game. In the beginning of each level a new class appears in the game; this class is a child-class of the class *y**i* (and *y**i* is called parent-class for this new class). Thus, the classes form a tree. Initially there is only one class with index 1.
Cha... | First line contains one integer number *m* — number of queries (1<=≤<=*m*<=≤<=3·105).
Next *m* lines contain description of queries. *i* -th line (1<=≤<=*i*<=≤<=*m*) describes the *i* -th level and contains an integer *y**i* — the index of the parent-class of class with index *i*<=+<=1 (1<=≤<=*y**i*<=≤<=*i*). | Suppose that at *i* -th level the maximum cost of changing one class to another is *x*. For each level output the number of classes such that for each of these classes there exists some other class *y*, and the distance from this class to *y* is exactly *x*. | [
"4\n1\n1\n2\n1\n",
"4\n1\n1\n2\n3\n"
] | [
"2\n2\n2\n3\n",
"2\n2\n2\n2\n"
] | none | [] | 30 | 0 | 0 | 483,747 | |
238 | Meeting Her | [
"dp",
"graphs",
"shortest paths"
] | null | null | Urpal lives in a big city. He has planned to meet his lover tonight.
The city has *n* junctions numbered from 1 to *n*. The junctions are connected by *m* directed streets, all the roads have equal length. Urpal lives in junction *a* and the date is planned in a restaurant in junction *b*. He wants to use public tran... | The first line of the input contains four integers *n*, *m*, *a*, *b* (2<=≤<=*n*<=≤<=100; 0<=≤<=*m*<=≤<=*n*·(*n*<=-<=1); 1<=≤<=*a*,<=*b*<=≤<=*n*; *a*<=≠<=*b*).
The next *m* lines contain two integers each *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*; *u**i*<=≠<=*v**i*) describing a directed road from junction *u*... | In the only line of output print the minimum number of buses Urpal should get on on his way in the worst case. If it's not possible to reach the destination in the worst case print -1. | [
"7 8 1 7\n1 2\n1 3\n2 4\n3 4\n4 6\n4 5\n6 7\n5 7\n3\n2 7\n1 4\n5 7\n",
"4 4 1 2\n1 2\n1 3\n2 4\n3 4\n1\n1 4\n"
] | [
"2\n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 487,664 | |
601 | A Museum Robbery | [
"data structures",
"dp"
] | null | null | There's a famous museum in the city where Kleofáš lives. In the museum, *n* exhibits (numbered 1 through *n*) had been displayed for a long time; the *i*-th of those exhibits has value *v**i* and mass *w**i*.
Then, the museum was bought by a large financial group and started to vary the exhibits. At about the same ti... | The first line of the input contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=5000, 1<=≤<=*k*<=≤<=1000) — the initial number of exhibits in the museum and the maximum interesting mass of stolen exhibits.
Then, *n* lines follow. The *i*-th of them contains two space-separated positive integers *v**i* an... | As the number of values *s*(*m*) can get large, output the answers to events of type 3 in a special format.
For each event of type 3, consider the values *s*(*m*) computed for the question that Kleofáš asked in this event; print one line containing a single number
where *p*<==<=107<=+<=19 and *q*<==<=109<=+<=7.
Pri... | [
"3 10\n30 4\n60 6\n5 1\n9\n3\n1 42 5\n1 20 3\n3\n2 2\n2 4\n3\n1 40 6\n3\n",
"3 1000\n100 42\n100 47\n400 15\n4\n2 2\n2 1\n2 3\n3\n"
] | [
"556674384\n168191145\n947033915\n181541912\n",
"0\n"
] | In the first sample, the numbers of displayed exhibits and values *s*(1), ..., *s*(10) for individual events of type 3 are, in order:
The values of individual exhibits are *v*<sub class="lower-index">1</sub> = 30, *v*<sub class="lower-index">2</sub> = 60, *v*<sub class="lower-index">3</sub> = 5, *v*<sub class="lower-... | [] | 46 | 0 | 0 | 493,279 | |
50 | Bombing | [
"binary search",
"dp",
"probabilities"
] | D. Bombing | 2 | 256 | The commanding officers decided to drop a nuclear bomb on the enemy's forces. You are ordered to determine the power of the warhead that needs to be used.
The enemy has *N* strategically important objects. Their positions are known due to the intelligence service. The aim of the strike is to deactivate at least *K* im... | The first line contains an integer *N* which represents the number of the enemy's objects (1<=≤<=*N*<=≤<=100). The second line contains two integers: *K* is the required number of deactivated objects, and ε is the maximally permitted probability of not completing the task, given in per mils (1<=≤<=*K*<=≤<=*N*, 1<=≤<=ε<... | Print the sought estimated impact radius of the warhead. The absolute or relative measure of the inaccuracy of your answer should not exceed 10<=-<=6. | [
"1\n1 500\n5 5\n1 2\n",
"5\n3 100\n0 0\n3 4\n60 70\n100 100\n10 10\n5 12\n"
] | [
"3.84257761518762740\n",
"13.45126176453737600\n"
] | none | [] | 62 | 0 | 0 | 493,567 |
323 | Tournament-graph | [
"constructive algorithms",
"graphs"
] | null | null | In this problem you have to build tournament graph, consisting of *n* vertices, such, that for any oriented pair of vertices (*v*,<=*u*) (*v*<=≠<=*u*) there exists a path from vertex *v* to vertex *u* consisting of no more then two edges.
A directed graph without self-loops is a tournament, if there is exactly one edg... | The first line contains an integer *n* (3<=≤<=*n*<=≤<=1000), the number of the graph's vertices. | Print -1 if there is no graph, satisfying the described conditions.
Otherwise, print *n* lines with *n* integers in each. The numbers should be separated with spaces. That is adjacency matrix *a* of the found tournament. Consider the graph vertices to be numbered with integers from 1 to *n*. Then *a**v*,<=*u*<==<=0, i... | [
"3\n",
"4\n"
] | [
"0 1 0\n0 0 1\n1 0 0\n",
"-1\n"
] | none | [] | 30 | 0 | 0 | 493,838 | |
0 | none | [
"none"
] | null | null | The game of Egg Roulette is played between two players. Initially 2*R* raw eggs and 2*C* cooked eggs are placed randomly into a carton. The shells are left on so there is no way to distinguish a raw egg from a cooked egg. One at a time, a player will select an egg, and then smash the egg on his/her forehead. If the egg... | The first line of input will contain integers *R* and *C* (1<=≤<=*R*,<=*C*<=≤<=20,<=*R*<=+<=*C*<=≤<=30).
The second line of input will contain the string *S* of length 2(*R*<=+<=*C*) consisting only of characters 'A', 'B', '?'. | Print the number of valid orderings that minimize unfairness and match *S*. | [
"1 1\n??BB\n",
"2 4\n?BA??B??A???\n",
"4 14\n????A??BB?????????????AB????????????\n"
] | [
"0\n",
"1\n",
"314\n"
] | In the first test case, the minimum unfairness is 0, and the orderings that achieve it are "ABBA" and "BAAB", neither of which match *S*. Note that an ordering such as "ABBB" would also have an unfairness of 0, but is invalid because it does not contain the same number of 'A's as 'B's.
In the second example, the only ... | [] | 46 | 0 | 0 | 495,297 | |
891 | Sloth | [
"dfs and similar",
"dp",
"graph matchings",
"trees"
] | null | null | Sloth is bad, mkay? So we decided to prepare a problem to punish lazy guys.
You are given a tree, you should count the number of ways to remove an edge from it and then add an edge to it such that the final graph is a tree and has a perfect matching. Two ways of this operation are considered different if their removed... | The first line contains *n* (2<=≤<=*n*<=≤<=5·105) — the number of vertices.
Each of the next *n*<=-<=1 lines contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=*n*) — the endpoints of one edge. It's guaranteed that the graph is a tree. | Output a single integer — the answer to the problem. | [
"4\n1 2\n2 3\n3 4\n",
"5\n1 2\n2 3\n3 4\n3 5\n",
"8\n1 2\n2 3\n3 4\n1 5\n5 6\n6 7\n1 8\n"
] | [
"8\n",
"0\n",
"22\n"
] | In first sample, there are 8 ways:
- edge between 2 and 3 turns to edge between 1 and 3, - edge between 2 and 3 turns to edge between 1 and 4, - edge between 2 and 3 turns to edge between 2 and 3, - edge between 2 and 3 turns to edge between 2 and 4, - edge between 1 and 2 turns to edge between 1 and 2, - edge b... | [] | 30 | 0 | 0 | 495,490 | |
379 | New Year Tree | [
"data structures",
"divide and conquer",
"trees"
] | null | null | You are a programmer and you have a New Year Tree (not the traditional fur tree, though) — a tree of four vertices: one vertex of degree three (has number 1), connected with three leaves (their numbers are from 2 to 4).
On the New Year, programmers usually have fun. You decided to have fun as well by adding vertices t... | The first line contains integer *q* (1<=≤<=*q*<=≤<=5·105) — the number of operations. Each of the next *q* lines contains integer *v**i* (1<=≤<=*v**i*<=≤<=*n*) — the operation of adding leaves to vertex *v**i*. Variable *n* represents the number of vertices in the current tree.
It is guaranteed that all given operatio... | Print *q* integers — the diameter of the current tree after each operation. | [
"5\n2\n3\n4\n8\n5\n"
] | [
"3\n4\n4\n5\n6\n"
] | none | [] | 2,000 | 266,240,000 | 0 | 495,966 | |
57 | Journey | [
"dp",
"math"
] | D. Journey | 1 | 256 | Stewie the Rabbit explores a new parallel universe. This two dimensional universe has the shape of a rectangular grid, containing *n* lines and *m* columns. The universe is very small: one cell of the grid can only contain one particle. Each particle in this universe is either static or dynamic. Each static particle al... | The first line contains two space-separated integers: *n*,<=*m* (2<=≤<=*n*,<=*m*<=≤<=1000) which represent the sizes of the universe. The next *n* lines containing *m* symbols each describe the universe without dynamic particles — the *j*-th symbol of the *i*-th line equals to 'X' if the cell is occupied by a static pa... | You have to print on a single line a single number which is the average life span of a particle with an accuracy of at least 6 decimal places.
The answer will be accepted if it is within 10<=-<=6 of absolute or relative error from the correct answer. | [
"2 2\n..\n.X\n",
"3 3\n...\n.X.\n...\n"
] | [
"0.888888888889\n",
"2.000000000000\n"
] | none | [] | 46 | 0 | 0 | 496,717 |
0 | none | [
"none"
] | null | null | Misha has an array of *n* integers indexed by integers from 1 to *n*. Let's define palindrome degree of array *a* as the number of such index pairs (*l*,<=*r*)(1<=≤<=*l*<=≤<=*r*<=≤<=*n*), that the elements from the *l*-th to the *r*-th one inclusive can be rearranged in such a way that the whole array will be a palindr... | The first line contains integer *n* (1<=≤<=*n*<=≤<=105).
The second line contains *n* positive integers *a*[*i*] (1<=≤<=*a*[*i*]<=≤<=*n*), separated by spaces — the elements of Misha's array. | In a single line print the answer to the problem. | [
"3\n2 2 2\n",
"6\n3 6 5 3 3 5\n",
"5\n5 5 2 5 2\n"
] | [
"6\n",
"0\n",
"4\n"
] | In the first sample test any possible pair (*l*, *r*) meets the condition.
In the third sample test following pairs (1, 3), (1, 4), (1, 5), (2, 5) meet the condition. | [] | 46 | 0 | 0 | 497,082 | |
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 m... | 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* l... | 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 re... | [] | 77 | 0 | 0 | 507,788 | |
472 | Design Tutorial: Increase the Constraints | [
"bitmasks",
"data structures",
"fft"
] | null | null | There is a simple way to create hard tasks: take one simple problem as the query, and try to find an algorithm that can solve it faster than bruteforce. This kind of tasks usually appears in OI contest, and usually involves data structures.
Let's try to create a task, for example, we take the "Hamming distance problem... | The first line contains a string *a* (1<=≤<=|*a*|<=≤<=200000). The second line contains a string *b* (1<=≤<=|*b*|<=≤<=200000). Each character of both strings is either "0" or "1".
The third line contains an integer *q* (1<=≤<=*q*<=≤<=400000) — the number of queries. Each of the following *q* lines contains three integ... | Output *q* integers — the answers for the queries. | [
"101010\n11110000\n3\n0 0 3\n2 3 4\n5 7 1\n",
"10001010101011001010100101010011010\n101010100101001010100100101010\n5\n0 0 12\n3 9 7\n6 4 15\n12 15 10\n13 3 20\n"
] | [
"1\n1\n0\n",
"5\n4\n3\n5\n13\n"
] | none | [] | 61 | 0 | 0 | 508,555 | |
303 | Rotatable Number | [
"math",
"number theory"
] | null | null | Bike is a smart boy who loves math very much. He invented a number called "Rotatable Number" inspired by 142857.
As you can see, 142857 is a magic number because any of its rotatings can be got by multiplying that number by 1,<=2,<=...,<=6 (numbers from one to number's length). Rotating a number means putting its las... | The only line contains two space-separated integers *n*,<=*x* (1<=≤<=*n*<=≤<=5·106,<=2<=≤<=*x*<=≤<=109). | Print a single integer — the largest *b* you found. If no such *b* exists, print -1 instead. | [
"6 11\n",
"5 8\n"
] | [
"10\n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 509,737 | |
671 | Roads in Yusland | [
"data structures",
"dp",
"greedy"
] | null | null | Mayor of Yusland just won the lottery and decided to spent money on something good for town. For example, repair all the roads in the town.
Yusland consists of *n* intersections connected by *n*<=-<=1 bidirectional roads. One can travel from any intersection to any other intersection using only these roads.
There is ... | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=300<=000) — the number of cities in Yusland and the number of workers respectively.
Then follow *n*−1 line, each of them contains two integers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*) — indices of intersections connected by the... | If it's impossible to repair all roads then print <=-<=1. Otherwise print a single integer — minimum cost required to repair all roads using "RC company" workers. | [
"6 5\n1 2\n1 3\n3 4\n4 5\n4 6\n2 1 2\n3 1 4\n4 1 3\n5 3 1\n6 3 2\n"
] | [
"8\n"
] | In the first sample, we should choose workers with indices 1, 3, 4 and 5, some roads will be repaired more than once but it is OK. The cost will be equal to 2 + 3 + 1 + 2 = 8 coins. | [] | 46 | 0 | 0 | 509,822 | |
207 | Beaver's Calculator 1.0 | [
"greedy"
] | null | null | The Smart Beaver from ABBYY has once again surprised us! He has developed a new calculating device, which he called the "Beaver's Calculator 1.0". It is very peculiar and it is planned to be used in a variety of scientific problems.
To test it, the Smart Beaver invited *n* scientists, numbered from 1 to *n*. The *i*-t... | The first line contains integer *n* — the number of scientists. To lessen the size of the input, each of the next *n* lines contains five integers *k**i*, *a**i*,<=1, *x**i*, *y**i*, *m**i* (0<=≤<=*a**i*,<=1<=<<=*m**i*<=≤<=109, 1<=≤<=*x**i*,<=*y**i*<=≤<=109) — the number of problems of the *i*-th scientist, the reso... | On the first line print a single number — the number of "bad" pairs in the optimal order.
If the total number of problems does not exceed 200000, also print lines — the optimal order of the problems. On each of these lines print two integers separated by a single space — the required number of resources for the probl... | [
"2\n2 1 1 1 10\n2 3 1 1 10\n",
"2\n3 10 2 3 1000\n3 100 1 999 1000\n"
] | [
"0\n1 1\n2 1\n3 2\n4 2\n",
"2\n10 1\n23 1\n49 1\n100 2\n99 2\n98 2\n"
] | In the first sample *n* = 2, *k*<sub class="lower-index">1</sub> = 2, *a*<sub class="lower-index">1, 1</sub> = 1, *a*<sub class="lower-index">1, 2</sub> = 2, *k*<sub class="lower-index">2</sub> = 2, *a*<sub class="lower-index">2, 1</sub> = 3, *a*<sub class="lower-index">2, 2</sub> = 4. We've got two scientists, each of... | [] | 280 | 0 | 0 | 511,373 | |
240 | Road Repairs | [
"dfs and similar",
"graphs",
"greedy"
] | null | null | A country named Berland has *n* cities. They are numbered with integers from 1 to *n*. City with index 1 is the capital of the country. Some pairs of cities have monodirectional roads built between them. However, not all of them are in good condition. For each road we know whether it needs repairing or not. If a road n... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105) — the number of cities and the number of roads in Berland.
Next *m* lines contain three space-separated integers *a**i*,<=*b**i*,<=*c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i*<=≠<=*b**i*,<=0<=≤<=*c**i*<=≤<=1), describing the r... | If even after all roads are repaired, it is still impossible to get to some city from the capital, print <=-<=1. Otherwise, on the first line print the minimum number of roads that need to be repaired, and on the second line print the numbers of these roads, separated by single spaces.
The roads are numbered starting ... | [
"3 2\n1 3 0\n3 2 1\n",
"4 4\n2 3 0\n3 4 0\n4 1 0\n4 2 1\n",
"4 3\n1 2 0\n1 3 0\n1 4 0\n"
] | [
"1\n2\n",
"-1\n",
"0\n\n"
] | none | [] | 124 | 0 | 0 | 511,552 | |
802 | Send the Fool Further! (hard) | [
"dfs and similar",
"dp",
"math",
"trees"
] | null | null | Heidi is terrified by your estimate and she found it unrealistic that her friends would collaborate to drive her into debt. She expects that, actually, each person will just pick a random friend to send Heidi to. (This randomness assumption implies, however, that she can now visit the same friend an arbitrary number of... | The first line contains the number of friends *n* (3<=≤<=*n*<=≤<=105). The next *n*<=-<=1 lines each contain three space-separated integers *u*, *v* and *c* (0<=≤<=*u*,<=*v*<=≤<=*n*<=-<=1, 1<=≤<=*c*<=≤<=104) meaning that *u* and *v* are friends and the cost for traveling between *u* and *v* is *c* (paid every time!).
... | Assume that the expected cost of the trips is written as an irreducible fraction *a*<=/<=*b* (that is, *a* and *b* are coprime). Then Heidi, the weird cow that she is, asks you to output . (Output a single integer between 0 and 109<=+<=6.) | [
"3\n0 1 10\n0 2 20\n",
"4\n0 1 3\n0 2 9\n0 3 27\n",
"7\n0 1 3\n0 5 7\n1 2 2\n1 3 1\n1 4 5\n5 6 8\n",
"11\n1 0 6646\n2 0 8816\n3 2 9375\n4 2 5950\n5 1 8702\n6 2 2657\n7 2 885\n8 7 2660\n9 2 5369\n10 6 3798\n",
"6\n0 1 8\n0 2 24\n1 3 40\n1 4 16\n4 5 8\n"
] | [
"15\n",
"13\n",
"400000019\n",
"153869806\n",
"39\n"
] | In the first example, with probability 1 / 2 Heidi will go to 1 from 0, and with probability 1 / 2 she will go to 2. In the first case the cost would be 10, and in the second it would be 20. After reaching 1 or 2 she will stop, as 1 and 2 are leaves of the social tree. Hence, the expected cost she has to pay is 10·1 / ... | [] | 15 | 0 | 0 | 520,879 | |
822 | Madness | [
"constructive algorithms",
"dfs and similar",
"trees"
] | null | null | The second semester starts at the University of Pavlopolis. After vacation in Vičkopolis Noora needs to return to Pavlopolis and continue her study.
Sometimes (or quite often) there are teachers who do not like you. Incidentally Noora also has one such teacher. His name is Yury Dmitrievich and he teaches graph theory.... | The first line contains single integer *n* (2<=≤<=*n*<=≤<=100) — number of vertices in the given tree.
Each of next *n*<=-<=1 lines contains two integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*n*,<=*u*<=≠<=*v*) — vertices connected by an edge.
Guaranteed that input defines a valid tree. | In the first line print single integer *paths* — number of paths you want to choose.
In the next *paths* lines print path's descriptions:
1. Single integer *len* — number of edges in the current path. 1. *len* integers — indices of the edges in the path. The edges are numbered from 1 to *n*<=-<=1 in order they are ... | [
"3\n1 2\n2 3\n"
] | [
"2\n1 1 1 2 0.6666666666\n1 2 2 3 0.6666666666\n"
] | Consider an example.
In starting moment of time points are located as following:
<img class="tex-graphics" src="https://espresso.codeforces.com/9f5f0d40e52b34d3ff77ced248543d9c9b96e1ff.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The first path is highlighted in red, the second in blue, green circles represe... | [] | 30 | 0 | 0 | 525,765 | |
342 | Xenia and Dominoes | [
"bitmasks",
"dfs and similar",
"dp"
] | null | null | Xenia likes puzzles very much. She is especially fond of the puzzles that consist of domino pieces. Look at the picture that shows one of such puzzles.
A puzzle is a 3<=×<=*n* table with forbidden cells (black squares) containing dominoes (colored rectangles on the picture). A puzzle is called correct if it meets the ... | The first line contains integer *n* (3<=≤<=*n*<=≤<=104) — the puzzle's size. Each of the following three lines contains *n* characters — the description of the table. The *j*-th character of the *i*-th line equals "X" if the corresponding cell is forbidden; it equals ".", if the corresponding cell is non-forbidden and ... | Print a single number — the answer to the problem modulo 1000000007 (109<=+<=7). | [
"5\n....X\n.O...\n...X.\n",
"5\n.....\n.O...\n.....\n",
"3\n...\n...\n..O\n"
] | [
"1\n",
"2\n",
"4\n"
] | Two puzzles are considered distinct if there is a pair of cells that contain one domino in one puzzle and do not contain it in the other one. | [] | 92 | 0 | 0 | 527,764 | |
75 | Ship's Shortest Path | [
"geometry",
"shortest paths"
] | E. Ship's Shortest Path | 2 | 256 | You have got a new job, and it's very interesting, you are a ship captain. Your first task is to move your ship from one point to another point, and for sure you want to move it at the minimum cost.
And it's well known that the shortest distance between any 2 points is the length of the line segment between these 2 po... | The first line contains 4 integers, *xStart*, *yStart*, *xEnd* and *yEnd* (<=-<=100<=≤<=*xStart*,<=*yStart*,<=*xEnd*,<=*yEnd*<=≤<=100). The second line contains an integer *n*, which is the number of points in the polygon (3<=≤<=*n*<=≤<=30), followed by a line containing *n* pairs of integers *x* and *y*, which are the... | Print one line which contains the minimum possible cost. The absolute or relative error in the answer should not exceed 10<=-<=6. | [
"1 7 6 7\n4\n4 2 4 12 3 12 3 2\n",
"-1 0 2 0\n4\n0 0 1 0 1 1 0 1\n"
] | [
"6.000000000\n",
"3.000000000\n"
] | none | [
{
"input": "1 7 6 7\n4\n4 2 4 12 3 12 3 2",
"output": "6.000000000"
},
{
"input": "-1 0 2 0\n4\n0 0 1 0 1 1 0 1",
"output": "3.000000000"
},
{
"input": "-70 -86 31 -90\n5\n-92 -70 77 -51 99 23 72 52 -34 86",
"output": "101.079176886"
},
{
"input": "88 37 71 -14\n4\n-48 -22 61... | 312 | 0 | 0 | 527,879 |
391 | The Tournament | [
"brute force"
] | null | null | This problem consists of three subproblems: for solving subproblem C1 you will receive 4 points, for solving subproblem C2 you will receive 4 points, and for solving subproblem C3 you will receive 8 points.
Manao decided to pursue a fighter's career. He decided to begin with an ongoing tournament. Before Manao joined,... | The first line contains a pair of integers *n* and *k* (1<=≤<=*k*<=≤<=*n*<=+<=1). The *i*-th of the following *n* lines contains two integers separated by a single space — *p**i* and *e**i* (0<=≤<=*p**i*,<=*e**i*<=≤<=200000).
The problem consists of three subproblems. The subproblems have different constraints on the ... | Print a single number in a single line — the minimum amount of effort Manao needs to use to rank in the top *k*. If no amount of effort can earn Manao such a rank, output number -1. | [
"3 2\n1 1\n1 4\n2 2\n",
"2 1\n3 2\n4 0\n",
"5 2\n2 10\n2 10\n1 1\n3 1\n3 1\n"
] | [
"3\n",
"-1\n",
"12\n"
] | Consider the first test case. At the time when Manao joins the tournament, there are three fighters. The first of them has 1 tournament point and the victory against him requires 1 unit of effort. The second contestant also has 1 tournament point, but Manao needs 4 units of effort to defeat him. The third contestant ha... | [] | 108 | 7,270,400 | 3 | 529,941 | |
478 | Wavy numbers | [
"brute force",
"dfs and similar",
"meet-in-the-middle",
"sortings"
] | null | null | A wavy number is such positive integer that for any digit of its decimal representation except for the first one and the last one following condition holds: the digit is either strictly larger than both its adjacent digits or strictly less than both its adjacent digits. For example, numbers 35270, 102, 747, 20 and 3 ar... | The only line of input contains two integers *n* and *k*, separated by a single space (1<=≤<=*n*,<=*k*<=≤<=1014). | Your task is to output the only integer *r* — the answer to the given problem. If such number does not exist or it is larger than 1014, then print "-1" (minus one without the quotes) instead. | [
"123 4\n",
"100 1\n",
"97461 457\n"
] | [
"1845\n",
"-1\n",
"1805270103\n"
] | The values of the first four wavy numbers that are divisible by *n* for the first sample are: 492, 615, 738 and 1845. | [] | 46 | 0 | 0 | 530,852 | |
39 | Testing | [] | K. Testing | 2 | 64 | You take part in the testing of new weapon. For the testing a polygon was created. The polygon is a rectangular field *n*<=×<=*m* in size, divided into unit squares 1<=×<=1 in size. The polygon contains *k* objects, each of which is a rectangle with sides, parallel to the polygon sides and entirely occupying several un... | The first line has three integers *n*, *m* и *k* (1<=≤<=*n*,<=*m*<=≤<=1000, 1<=≤<=*k*<=≤<=90) — the sizes of the polygon and the number of objects on it respectively. Next *n* lines contain *m* symbols each and describe the polygon. The symbol "*" stands for a square occupied an object, whereas the symbol "." stands fo... | Output a single number — the number of different ways to hit a target. | [
"3 3 3\n*.*\n...\n*..\n",
"4 5 4\n.*.**\n...**\n**...\n...**\n",
"2 2 1\n.*\n..\n"
] | [
"21\n",
"38\n",
"4\n"
] | none | [] | 92 | 0 | 0 | 532,464 |
228 | The Road to Berland is Paved With Good Intentions | [
"2-sat",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Berland has *n* cities, some of them are connected by bidirectional roads. For each road we know whether it is asphalted or not.
The King of Berland Valera II wants to asphalt all roads of Berland, for that he gathered a group of workers. Every day Valera chooses exactly one city and orders the crew to asphalt all roa... | The first line contains two space-separated integers *n*,<=*m* — the number of cities and roads in Berland, correspondingly. Next *m* lines contain the descriptions of roads in Berland: the *i*-th line contains three space-separated integers *a**i*,<=*b**i*,<=*c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *a**i*<=≠<=*b**i*; 0... | In the first line print a single integer *x* (0<=≤<=*x*<=≤<=*n*) — the number of days needed to asphalt all roads. In the second line print *x* space-separated integers — the indexes of the cities to send the workers to. Print the cities in the order, in which Valera send the workers to asphalt roads. If there are mult... | [
"4 4\n1 2 1\n2 4 0\n4 3 1\n3 2 0\n",
"3 3\n1 2 0\n2 3 0\n3 1 0\n"
] | [
"4\n3 2 1 3\n",
"Impossible\n"
] | none | [
{
"input": "4 4\n1 2 1\n2 4 0\n4 3 1\n3 2 0",
"output": "4\n3 2 1 3"
},
{
"input": "3 3\n1 2 0\n2 3 0\n3 1 0",
"output": "Impossible"
},
{
"input": "4 5\n3 2 0\n1 4 0\n4 3 1\n3 1 0\n1 2 0",
"output": "Impossible"
},
{
"input": "2 1\n2 1 0",
"output": "1\n1 "
},
{
... | 108 | 6,758,400 | 3 | 533,263 | |
613 | Puzzle Lover | [
"dp",
"hashing",
"strings"
] | null | null | Oleg Petrov loves crossword puzzles and every Thursday he buys his favorite magazine with crosswords and other word puzzles. In the last magazine Oleg found a curious puzzle, and the magazine promised a valuable prize for it's solution. We give a formal description of the problem below.
The puzzle field consists of tw... | The first two lines contain the state of the field for the puzzle. Each of these non-empty lines contains exactly *n* small English letters.
The next line is left empty.
The next line is non-empty and contains word *w*, consisting of small English letters.
The length of each line doesn't exceed 2<=000. | Print a single integer — the number of distinct solutions for the puzzle modulo 109<=+<=7. | [
"code\nedoc\n\ncode\n",
"aaa\naaa\n\naa\n"
] | [
"4\n",
"14\n"
] | none | [] | 46 | 0 | 0 | 536,201 | |
196 | Opening Portals | [
"dsu",
"graphs",
"shortest paths"
] | null | null | Pavel plays a famous computer game. A player is responsible for a whole country and he can travel there freely, complete quests and earn experience.
This country has *n* cities connected by *m* bidirectional roads of different lengths so that it is possible to get from any city to any other one. There are portals in *... | The first line contains two space-separated integers *n* and *m* (1<=≤<=*n*<=≤<=105, 0<=≤<=*m*<=≤<=105) that show how many cities and roads are in the game.
Each of the next *m* lines contains the description of a road as three space-separated integers *x**i*, *y**i*, *w**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=≠<=... | Print a single number — the minimum time a player needs to open all portals.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3 3\n1 2 1\n1 3 1\n2 3 1\n3\n1 2 3\n",
"4 3\n1 2 1\n2 3 5\n2 4 10\n3\n2 3 4\n",
"4 3\n1 2 1000000000\n2 3 1000000000\n3 4 1000000000\n4\n1 2 3 4\n"
] | [
"2\n",
"16\n",
"3000000000\n"
] | In the second sample the player has to come to city 2, open a portal there, then go to city 3, open a portal there, teleport back to city 2 and finally finish the journey in city 4. | [] | 92 | 0 | 0 | 539,422 | |
793 | Oleg and chess | [
"data structures",
"divide and conquer",
"flows",
"graph matchings"
] | null | null | Oleg the bank client solves an interesting chess problem: place on *n*<=×<=*n* chessboard the maximum number of rooks so that they don't beat each other. Of course, no two rooks can share the same cell.
Remind that a rook standing in the cell (*a*,<=*b*) beats a rook standing in the cell (*x*,<=*y*) if and only if *a*... | The first line contains single integer *n* (1<=<=≤<=<=*n*<=≤<=<=10000) — the size of the board.
The second line contains single integer *q* (0<=<=≤<=<=*q*<=<=≤<=<=10000) — the number of deleted rectangles.
The next *q* lines contain the information about the deleted rectangles.
Each of these lines contains four inte... | In the only line print the maximum number of rooks Oleg can place on the board so that no two rooks beat each other. | [
"5\n5\n1 1 2 1\n1 3 1 5\n4 1 5 5\n2 5 2 5\n3 2 3 5\n",
"8\n4\n2 2 4 6\n1 8 1 8\n7 1 8 2\n5 4 6 8\n"
] | [
"3\n",
"8\n"
] | Here is the board and the example of rooks placement in the first example:
<img class="tex-graphics" src="https://espresso.codeforces.com/5455db405c55b6478627065011376e58257f95a0.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 46 | 0 | 0 | 540,695 | |
316 | PE Lesson | [
"dp",
"math"
] | null | null | Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning.... | The first line contains a single number *n* — the number of students in the line and the number of balls. The next line contains exactly *n* space-separated integers. Each number corresponds to a student in the line (the *i*-th number corresponds to the *i*-th from the left student in the line) and shows the number of ... | The output should contain a single integer — the number of variants of ball orders after the warm up exercise is complete. As the number can be rather large, print it modulo 1000000007 (109<=+<=7). | [
"5\n1 2 2 1 2\n",
"8\n1 2 2 1 2 1 1 2\n"
] | [
"120\n",
"16800\n"
] | none | [
{
"input": "5\n1 2 2 1 2",
"output": "120"
},
{
"input": "8\n1 2 2 1 2 1 1 2",
"output": "16800"
},
{
"input": "1\n2",
"output": "1"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "2\n2 2",
"output": "2"
},
{
"input": "2\n1 2",
"output": "2"
}... | 186 | 23,347,200 | 0 | 542,037 | |
175 | Plane of Tanks: Duel | [
"brute force",
"dp",
"math",
"probabilities"
] | null | null | Vasya plays the Plane of Tanks.
Tanks are described with the following attributes:
- the number of hit points; - the interval between two gun shots (the time required to recharge the gun); - the probability that the gun shot will not pierce armor of the enemy tank; - the damage to the enemy's tank.
The gun dama... | The first line contains five integer numbers separated with spaces describing Vasya's tank: the number of hit points *hp* (10<=≤<=*hp*<=≤<=200), the interval between two shots *dt* (1<=≤<=*dt*<=≤<=30), gun damage segment *l* and *r* (10<=≤<=*l*<=≤<=*r*<=≤<=100), the probability that the enemy's tank armor will not be p... | Print the only number with absolute or relative error no more than 10<=-<=4 — probability of Vasya's victory. | [
"100 3 50 50 0\n100 3 50 50 0\n",
"100 3 50 50 0\n100 2 48 50 0\n",
"100 3 50 50 0\n100 1 50 50 50\n"
] | [
"1.000000\n",
"0.888889\n",
"0.500000\n"
] | In the first example both tanks are destroyed at once after the second shot. The probability of destroying the enemy tank is 1.
In the second example Vasya's enemy tank fires the second shot before Vasya's tank, but has no time for the third shot. In order to destroy Vasya's tank it is necessary to fire two shots with... | [] | 60 | 0 | 0 | 543,145 | |
79 | Security System | [
"math"
] | E. Security System | 1 | 256 | Fox Ciel safely returned to her castle, but there was something wrong with the security system of the castle: sensors attached in the castle were covering her.
Ciel is at point (1,<=1) of the castle now, and wants to move to point (*n*,<=*n*), which is the position of her room. By one step, Ciel can move from point (*... | In the first line there are five integers *n*,<=*t*,<=*a*,<=*b*,<=*c* (2<=≤<=*n*<=≤<=2·105,<= 0<=≤<=*t*<=≤<=1014,<= 1<=≤<=*a*<=≤<=*n*<=-<=*c*<=+<=1,<= 1<=≤<=*b*<=≤<=*n*<=-<=*c*<=+<=1,<= 1<=≤<=*c*<=≤<=*n*).
Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin s... | If Ciel's objective is possible, output in first line 2*n*<=-<=2 characters that represent her feasible steps, where *i*-th character is R if *i*-th step is moving rightward, or U if moving upward. If there are several solution, output lexicographically first one. Character R is lexicographically earlier than the chara... | [
"5 25 2 4 1\n",
"3 6 1 2 2\n",
"3 5 1 2 2\n",
"20 492 11 4 8\n"
] | [
"RRUURURU\n",
"URUR\n",
"Impossible\n",
"RRRRRRRRRRRRRRRRUUUUURUUUUURRUUUUUUUUU\n"
] | The answers for the first sample and the second sample are shown on the picture: | [] | 30 | 0 | 0 | 545,245 |
487 | Tourists | [
"data structures",
"dfs and similar",
"graphs",
"trees"
] | null | null | There are *n* cities in Cyberland, numbered from 1 to *n*, connected by *m* bidirectional roads. The *j*-th road connects city *a**j* and *b**j*.
For tourists, souvenirs are sold in every city of Cyberland. In particular, city *i* sell it at a price of *w**i*.
Now there are *q* queries for you to handle. There are tw... | The first line of input contains three integers *n*,<=*m*,<=*q* (1<=≤<=*n*,<=*m*,<=*q*<=≤<=105), separated by a single space.
Next *n* lines contain integers *w**i* (1<=≤<=*w**i*<=≤<=109).
Next *m* lines contain pairs of space-separated integers *a**j* and *b**j* (1<=≤<=*a**j*,<=*b**j*<=≤<=*n*,<=*a**j*<=≠<=*b**j*).
... | For each query of type "A", output the corresponding answer. | [
"3 3 3\n1\n2\n3\n1 2\n2 3\n1 3\nA 2 3\nC 1 5\nA 2 3\n",
"7 9 4\n1\n2\n3\n4\n5\n6\n7\n1 2\n2 5\n1 5\n2 3\n3 4\n2 4\n5 6\n6 7\n5 7\nA 2 3\nA 6 4\nA 6 7\nA 3 3\n"
] | [
"1\n2\n",
"2\n1\n5\n3\n"
] | For the second sample, an optimal routes are:
From 2 to 3 it is [2, 3].
From 6 to 4 it is [6, 5, 1, 2, 4].
From 6 to 7 it is [6, 5, 7].
From 3 to 3 it is [3]. | [
{
"input": "3 3 3\n1\n2\n3\n1 2\n2 3\n1 3\nA 2 3\nC 1 5\nA 2 3",
"output": "1\n2"
},
{
"input": "7 9 4\n1\n2\n3\n4\n5\n6\n7\n1 2\n2 5\n1 5\n2 3\n3 4\n2 4\n5 6\n6 7\n5 7\nA 2 3\nA 6 4\nA 6 7\nA 3 3",
"output": "2\n1\n5\n3"
},
{
"input": "6 7 5\n4\n2\n1\n9\n7\n6\n2 1\n1 3\n2 3\n1 4\n5 1\n4... | 2,000 | 16,179,200 | 0 | 546,684 | |
1,004 | Sonya and Bitwise OR | [
"bitmasks",
"data structures",
"divide and conquer"
] | null | null | Sonya has an array $a_1, a_2, \ldots, a_n$ consisting of $n$ integers and also one non-negative integer $x$. She has to perform $m$ queries of two types:
- $1$ $i$ $y$: replace $i$-th element by value $y$, i.e. to perform an operation $a_{i}$ := $y$; - $2$ $l$ $r$: find the number of pairs ($L$, $R$) that $l\leq L\l... | The first line contains three integers $n$, $m$, and $x$ ($1\leq n, m\leq 10^5$, $0\leq x<2^{20}$) — the number of numbers, the number of queries, and the constant for all queries.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0\leq a_i<2^{20}$) — numbers of the array.
The following $m$ lines ... | For each query of type 2, print the number of subarrays such that the bitwise OR of all the numbers in the range is at least $x$. | [
"4 8 7\n0 3 6 1\n2 1 4\n2 3 4\n1 1 7\n2 1 4\n2 1 3\n2 1 1\n1 3 0\n2 1 4\n",
"5 5 7\n6 0 3 15 2\n2 1 5\n1 4 4\n2 1 5\n2 3 5\n2 1 4\n"
] | [
"5\n1\n7\n4\n1\n4\n",
"9\n7\n2\n4\n"
] | In the first example, there are an array [$0$, $3$, $6$, $1$] and queries:
1. on the segment [$1\ldots4$], you can choose pairs ($1$, $3$), ($1$, $4$), ($2$, $3$), ($2$, $4$), and ($3$, $4$); 1. on the segment [$3\ldots4$], you can choose pair ($3$, $4$); 1. the first number is being replacing by $7$, after this o... | [] | 46 | 0 | 0 | 549,560 | |
605 | Board Game | [
"data structures",
"dfs and similar"
] | null | null | You are playing a board card game. In this game the player has two characteristics, *x* and *y* — the white magic skill and the black magic skill, respectively. There are *n* spell cards lying on the table, each of them has four characteristics, *a**i*, *b**i*, *c**i* and *d**i*. In one move a player can pick one of th... | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of cards on the table.
Each of the next *n* lines contains four integers *a**i*, *b**i*, *c**i*, *d**i* (0<=≤<=*a**i*,<=*b**i*,<=*c**i*,<=*d**i*<=≤<=109) — the characteristics of the corresponding card. | In the first line print a single integer *k* — the minimum number of moves needed to cast the *n*-th spell and in the second line print *k* numbers — the indices of the cards in the order in which you should cast them. In case there are multiple possible solutions, print any of them.
If it is impossible to cast the *n... | [
"4\n0 0 3 4\n2 2 5 3\n4 1 1 7\n5 3 8 8\n",
"2\n0 0 4 6\n5 1 1000000000 1000000000\n"
] | [
"3\n1 2 4\n",
"-1\n"
] | none | [] | 30 | 0 | 0 | 549,710 | |
131 | Present to Mom | [
"binary search",
"two pointers"
] | null | null | How many stars are there in the sky? A young programmer Polycarpus can't get this question out of his head! He took a photo of the starry sky using his digital camera and now he analyzes the resulting monochrome digital picture. The picture is represented by a rectangular matrix consisting of *n* lines each containing ... | The first line of the input data contains three integers *n*,<=*m* and *k* (1<=≤<=*n*,<=*m*<=≤<=500;1<=≤<=*k*<=≤<=*nm*). Then follow *n* lines, containing the description of the given photo as a sequence of lines. Each line contains *m* characters '0' or '1'. | Print the required number of areas on the given photo. | [
"4 6 2\n111000\n111100\n011011\n000111\n",
"5 5 4\n11111\n11111\n11111\n11111\n11111\n"
] | [
"6\n",
"9\n"
] | We'll number the rows and columns below starting from 1, the coordinates (*p*, *q*) will denote a cell in row *p*, column *q*.
In the first sample Polycarpus should cut out any area containing a rectangle whose opposite corners lie in cells (1, 1) and (3, 4). Only rectangles with opposite corners in (1, 1) and (*x*, *... | [] | 31 | 0 | 0 | 551,426 | |
374 | Inna and Babies | [
"binary search",
"data structures",
"dsu",
"geometry",
"implementation"
] | null | null | Inna, Dima and Sereja are in one room together. It's cold outside, so Sereja suggested to play a board game called "Babies".
The babies playing board is an infinite plane containing *n* blue babies and *m* red ones. Each baby is a segment that grows in time. At time moment *t* the blue baby (*x*,<=*y*) is a blue segm... | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2000).
Next *n* lines contain the coordinates of the blue babies. The *i*-th line contains integers *x**i*,<=*y**i* — a baby's coordinates. Next *m* lines contain the coordinates of *m* red babies in the similar form.
All coordinates o... | In the single line print a single integer — the answer to the problem.
If the rectangle never appears on the plane, print "Poor Sereja!" without the quotes. | [
"2 2\n2 2\n5 5\n3 7\n5 1\n",
"3 2\n2 2\n3 2\n6 2\n4 2\n5 2\n"
] | [
"3\n",
"1\n"
] | none | [] | 30 | 0 | 0 | 552,773 | |
283 | Cow Tennis Tournament | [
"combinatorics",
"data structures",
"math"
] | null | null | Farmer John is hosting a tennis tournament with his *n* cows. Each cow has a skill level *s**i*, and no two cows having the same skill level. Every cow plays every other cow exactly once in the tournament, and each cow beats every cow with skill level lower than its own.
However, Farmer John thinks the tournament will... | On the first line are two space-separated integers, *n* and *k* (3<=≤<=*n*<=≤<=105; 0<=≤<=*k*<=≤<=105). On the next line are *n* space-separated distinct integers, *s*1,<=*s*2,<=...,<=*s**n* (1<=≤<=*s**i*<=≤<=109), denoting the skill levels of the cows. On the next *k* lines are two space separated integers, *a**i* and... | A single integer, containing the number of triples of cows (*p*,<=*q*,<=*r*) for which the final leaderboard shows that cow *p* beats cow *q*, cow *q* beats cow *r*, and cow *r* beats cow *p*.
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 ... | [
"3 2\n1 2 3\n1 2\n2 3\n",
"5 3\n5 9 4 1 7\n1 7\n2 8\n3 9\n"
] | [
"1\n",
"3\n"
] | In the first sample, cow 3 > cow 1, cow 3 > cow 2, and cow 2 > cow 1. However, the results between cows 1 and 2 and cows 2 and 3 are flipped, so now FJ's results show that cow 1 > cow 2, cow 2 > cow 3, and cow 3 > cow 1, so cows 1, 2, and 3 form a balanced triple. | [] | 62 | 0 | 0 | 555,141 | |
243 | Cubes | [
"data structures",
"dp",
"geometry",
"two pointers"
] | null | null | One day Petya got a set of wooden cubes as a present from his mom. Petya immediately built a whole city from these cubes.
The base of the city is an *n*<=×<=*n* square, divided into unit squares. The square's sides are parallel to the coordinate axes, the square's opposite corners have coordinates (0,<=0) and (*n*,<=*... | The first line contains three integers *n*, *v**x* and *v**y* (1<=≤<=*n*<=≤<=103, |*v**x*|,<=|*v**y*|<=≤<=|104|, |*v**x*|<=+<=|*v**y*|<=><=0).
Next *n* lines contain *n* integers each: the *j*-th integer in the *i*-th line *a**ij* (0<=≤<=*a**ij*<=≤<=109, 1<=≤<=*i*,<=*j*<=≤<=*n*) represents the height of the cube to... | Print a single integer — the number of visible cubes.
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. | [
"5 -1 2\n5 0 0 0 1\n0 0 0 0 2\n0 0 0 1 2\n0 0 0 0 2\n2 2 2 2 3\n",
"5 1 -2\n5 0 0 0 1\n0 0 0 0 2\n0 0 0 1 2\n0 0 0 0 2\n2 2 2 2 3\n"
] | [
"20",
"15"
] | none | [] | 92 | 0 | 0 | 555,558 | |
575 | Spectator Riots | [
"geometry"
] | null | null | It’s riot time on football stadium Ramacana! Raging fans have entered the field and the police find themselves in a difficult situation. The field can be represented as a square in the coordinate system defined by two diagonal vertices in (0,0) and (105, 105). The sides of that square are also considered to be inside t... | The first line contains the number of fans on the field, *N*. The next *N* lines contain three integers: *x**i* ,*y**i*, *v**i*. They are the *x*-coordinate, *y*-coordinate and speed of fan *i* at the beginning of the one second interval considered in the task.
- 3<=≤<=*N*<=≤<=105 - 0<=≤<=*x**i*,<=*y**i*<=≤<=105 - ... | You need to output the three points that camera needs to select. Print them in three lines, with every line containing the *x*-coordinate, then *y*-coordinate, separated by a single space. The order of points does not matter. | [
"3\n1 1 1\n1 1 1\n1 2 1\n"
] | [
"2 2\n2 1\n1 0\n"
] | none | [] | 46 | 0 | 0 | 559,371 | |
536 | Tavas on the Path | [
"data structures",
"divide and conquer",
"trees"
] | null | null | Tavas lives in Tavaspolis. Tavaspolis has *n* cities numbered from 1 to *n* connected by *n*<=-<=1 bidirectional roads. There exists a path between any two cities. Also each road has a length.
Tavas' favorite strings are binary strings (they contain only 0 and 1). For any binary string like *s*<==<=*s*1*s*2... *s**k*,... | The first line of input contains integers *n* and *q* (2<=≤<=*n*<=≤<=105 and 1<=≤<=*q*<=≤<=105).
The next line contains *n*<=-<=1 space separated integers *f*1,<=*f*2,<=...,<=*f**n*<=-<=1 (|*f**i*|<=≤<=1000).
The next *n*<=-<=1 lines contain the details of the roads. Each line contains integers *v*,<=*u* and *w* and ... | Print the answer of each query in a single line. | [
"2 3\n10\n1 2 3\n1 2 2\n1 2 3\n1 2 4\n",
"6 6\n-5 0 0 2 10\n1 2 1\n2 3 2\n3 4 5\n4 5 1\n5 6 5\n1 6 1\n1 6 2\n1 6 5\n3 6 5\n4 6 4\n1 4 2\n"
] | [
"10\n10\n0\n",
"10\n-5\n-10\n-10\n-5\n0\n"
] | none | [] | 30 | 0 | 0 | 570,064 | |
123 | Maze | [
"dfs and similar",
"dp",
"probabilities",
"trees"
] | null | null | A maze is represented by a tree (an undirected graph, where exactly one way exists between each pair of vertices). In the maze the entrance vertex and the exit vertex are chosen with some probability. The exit from the maze is sought by Deep First Search. If there are several possible ways to move, the move is chosen e... | The first line determines the number of vertices in the graph *n* (1<=≤<=*n*<=≤<=105). The next *n*<=-<=1 lines contain pairs of integers *a**i* and *b**i*, which show the existence of an edge between *a**i* and *b**i* vertices (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*). It is guaranteed that the given graph is a tree.
Next *n* ... | Print the expectation of the number of moves. The absolute or relative error should not exceed 10<=-<=9. | [
"2\n1 2\n0 1\n1 0\n",
"3\n1 2\n1 3\n1 0\n0 2\n0 3\n",
"7\n1 2\n1 3\n2 4\n2 5\n3 6\n3 7\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n"
] | [
"1.00000000000000000000\n",
"2.00000000000000000000\n",
"4.04081632653"
] | In the first sample the entrance vertex is always 1 and the exit vertex is always 2.
In the second sample the entrance vertex is always 1 and the exit vertex with the probability of 2/5 will be 2 of with the probability if 3/5 will be 3. The mathematical expectations for the exit vertices 2 and 3 will be equal (symmet... | [] | 92 | 0 | 0 | 570,868 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.