contestId
int64
0
1.01k
name
stringlengths
2
58
tags
sequencelengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
sequencelengths
0
7
demo-output
sequencelengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
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 knows that the number of permutations that use only valid pairs is odd. Now, for each pair he wants to find out, will the number of valid permutations be odd if he removes this pair (and only it) from the list.
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 that the total number of valid permutations (i.e. using only allowed pairs position-elements) is odd.
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 structure is connected, thus, the ropes and the nodes form a tree. Each rope of the formed structure is associated with its length. A candy is tied to node *x* of the structure. Om Nom really wants to eat this candy. The *y* spiders are trying to stop him from doing it. They decided to entangle the candy and some part of the structure into a web, thus attaching the candy to as large as possible part of the rope structure. Each spider can use his web to cover all ropes on the path between two arbitrary nodes *a* and *b*. Thus, *y* spiders can cover the set of ropes which is a union of *y* paths in the given tree. These *y* paths can arbitrarily intersect each other. The spiders want the following conditions to be hold: - the node containing the candy is adjacent to at least one rope covered with a web - the ropes covered with the web form a connected structure (what's the idea of covering with a web the ropes that are not connected with the candy?) - the total length of the ropes covered with web is as large as possible The spiders haven't yet decided to what node of the structure they will tie the candy and how many spiders will cover the structure with web, so they asked you to help them. Help them calculate the optimal plan for multiple values of *x* and *y*.
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*<=≀<=*n*, *u**i*<=β‰ <=*v**i*, 1<=≀<=*l**i*<=≀<=1000), showing that there is a rope of length *l**i* between nodes *u**i* and *v**i*. Next *q* lines describe the spiders' questions. As they want you to answer their question online, they encoded their messages in a special manner. Each of the next *q* lines contains two numbers *x**i*,<=*y**i*. In the first question of the spiders *x*<==<=*x*1,<=*y*<==<=*y*1. To calculate values *x* and *y* in the spiders' *i*-th (2<=≀<=*i*<=≀<=*q*) question, you need to use the following formulas: where *Ans**i*<=-<=1 is the total length of the ropes covered by a web in the answer for the (*i*<=-<=1)-th question. The following inequality holds: 1<=≀<=*x**i*,<=*y**i*<=≀<=*n*.
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 with speed 1, the criminals can move with arbitrary large speed. If a criminal at some moment is at the same point as the policeman, he instantly gets caught by the policeman. Determine the time needed for the policeman to catch all criminals, assuming everybody behaves optimally (i.e. the criminals maximize that time, the policeman minimizes that time). Everybody knows positions of everybody else at any moment of time.
The first line contains single integer *n* (1<=≀<=*n*<=≀<=50)Β β€” the number of vertices in the tree. The next *n*<=-<=1 lines contain three integers each: *u**i*, *v**i*, *w**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, 1<=≀<=*w**i*<=≀<=50) denoting edges and their lengths. It is guaranteed that the given graph is a tree. The next line contains single integer *s* (1<=≀<=*s*<=≀<=*n*)Β β€” the number of vertex where the policeman starts. The next line contains single integer *m* (1<=≀<=*m*<=≀<=50)Β β€” the number of criminals. The next line contains *m* integers *x*1,<=*x*2,<=...,<=*x**m* (1<=≀<=*x**j*<=≀<=*n*, *x**j*<=β‰ <=*s*)Β β€” the number of vertices where the criminals are located. *x**j* are not necessarily distinct.
If the policeman can't catch criminals, print single line "Terrorists win" (without quotes). Otherwise, print single integerΒ β€” the time needed to catch all criminals.
[ "4\n1 2 2\n1 3 1\n1 4 1\n2\n4\n3 1 4 1\n", "6\n1 2 3\n2 3 5\n3 4 1\n3 5 4\n2 6 3\n2\n3\n1 3 5\n" ]
[ "8\n", "21\n" ]
In the first example one of the optimal scenarios is the following. The criminal number 2 moves to vertex 3, the criminal 4Β β€” to vertex 4. The policeman goes to vertex 4 and catches two criminals. After that the criminal number 1 moves to the vertex 2. The policeman goes to vertex 3 and catches criminal 2, then goes to the vertex 2 and catches the remaining criminal.
[]
31
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 if and only if: - *k*<=β‰₯<=2. - All *P**i* are distinct, and each *P**i* is an element of *S*. - For any two pairs (*P*2*i*<=-<=1,<=*P*2*i*) and (*P*2*j*<=-<=1,<=*P*2*j*), the circumcircles of triangles *OP*2*i*<=-<=1*P*2*j*<=-<=1 and *OP*2*i**P*2*j* have a single common point, and the circumcircle of triangles *OP*2*i*<=-<=1*P*2*j* and *OP*2*i**P*2*j*<=-<=1 have a single common point. Calculate the number of good sets of pairs modulo 1000000007 (109<=+<=7).
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 points coincide.
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 16 16\n30 30 0 37\n6 30 -4 10\n6 15 12 15\n-4 5 -10 25\n-16 20 4 10\n8 17 -2 17\n16 34 2 17\n" ]
[ "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 connects a glade to itself. Also, two glades may have several non-intersecting trails between them. Vasya is on glade 1, he wants to walk on all trails of the park exactly once, so that he can eventually return to glade 1. Unfortunately, Vasya does not know whether this walk is possible or not. Help Vasya, determine whether the walk is possible or not. If such walk is impossible, find the minimum number of trails the authorities need to add to the park in order to make the described walk possible. Vasya can shift from one trail to another one only on glades. He can move on the trails in both directions. If Vasya started going on the trail that connects glades *a* and *b*, from glade *a*, then he must finish this trail on glade *b*.
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**i*<=≀<=*n*) β€” the numbers of the glades connected by this trail.
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 one trail, for example, between glades number one and two.
[]
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 concept of a racket! The concept is a triangular racket. Ant it should be not just any triangle, but a regular one. As soon as you've chosen the shape, you need to stretch the net. By the time you came the rocket had *n* holes drilled on each of its sides. The holes divide each side into equal *n*<=+<=1 parts. At that, the *m* closest to each apex holes on each side are made for better ventilation only and you cannot stretch the net through them. The next revolutionary idea as to stretch the net as obtuse triangles through the holes, so that for each triangle all apexes lay on different sides. Moreover, you need the net to be stretched along every possible obtuse triangle. That's where we need your help β€” help us to count the number of triangles the net is going to consist of. Two triangles are considered to be different if their pictures on the fixed at some position racket are different.
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_e = \sum\limits_{e \in \delta^{+}(v)} f_e$$ where $\delta^{+}(v)$ is the set of edges that end in the vertex $v$, and $\delta^{-}(v)$ is the set of edges that start in the vertex $v$. In other words, for each vertex the total incoming flow should be equal to the total outcoming flow. Let a $lr$-circulation be such a circulation $f$ that for each edge the condition $l_e \leq f_e \leq r_e$ holds, where $l_e$ and $r_e$ for each edge $e \in E$ are two non-negative real numbers denoting the lower and upper bounds on the value of the circulation on this edge $e$. Vova can't stop thinking about applications of a new topic. Right now he thinks about the following natural question: let the graph be fixed, and each value $l_e$ and $r_e$ be a linear function of a real variable $t$: $$l_e(t) = a_e t + b_e$$ $$r_e(t) = c_e t + d_e$$ Note that $t$ is the same for all edges. Let $t$ be chosen at random from uniform distribution on a segment $[0, 1]$. What is the probability of existence of $lr$-circulation in the graph?
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 the startpoint and the endpoint of the edge $e$, and the remaining 4 integers describe the linear functions for the upper and lower bound of circulation. It is guaranteed that for any $t \in [0, 1]$ and for any edge $e \in E$ the following condition holds $0 \leq l_e(t) \leq r_e(t) \leq 10^4$.
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]~~\&amp;~~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 riots. On the other hand, both parties have applied to host a huge number of demonstrations, so that on all squares demonstrations must be held. Now the capital management will distribute the area between the two parties. Some pairs of squares are connected by (*n*<=-<=1) bidirectional roads such that between any pair of squares there is a unique way to get from one square to another. Some squares are on the outskirts of the capital meaning that they are connected by a road with only one other square, such squares are called dead end squares. The mayor of the capital instructed to distribute all the squares between the parties so that the dead end squares had the same number of demonstrations of the first and the second party. It is guaranteed that the number of dead end squares of the city is even. To prevent possible conflicts between the zublicanes and the mumocrates it was decided to minimize the number of roads connecting the squares with the distinct parties. You, as a developer of the department of distributing squares, should determine this smallest number.
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 integers from 1 to *n*. It is guaranteed that the number of dead end squares of the city is even.
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\n11 12\n19 4\n3 9\n9 7\n7 13\n10 3\n16 20\n1 5\n5 17\n10 14", "output": "1" }, { "input": "11\n9 2\n9 4\n8 9\n7 9\n3 9\n5 9\n6 9\n10 9\n1 9\n11 9", "output": "5" }, { "input": "15\n4 12\n1 12\n1 6\n9 1\n7 4\n12 5\n15 9\n11 1\n13 9\n14 9\n9 2\n3 5\n10 2\n3 8", "output": "1" }, { "input": "16\n15 6\n5 9\n3 15\n9 11\n7 15\n1 2\n14 6\n8 9\n14 12\n10 16\n3 13\n8 1\n3 1\n9 4\n10 1", "output": "2" }, { "input": "17\n15 6\n2 8\n15 2\n8 3\n16 4\n13 7\n11 5\n10 1\n2 12\n16 8\n12 9\n11 8\n1 8\n5 17\n13 11\n14 13", "output": "2" }, { "input": "18\n5 7\n3 9\n16 17\n18 13\n3 15\n3 18\n17 11\n12 8\n1 2\n5 16\n17 4\n1 4\n8 1\n6 5\n4 18\n10 5\n14 17", "output": "1" }, { "input": "19\n3 19\n13 19\n11 6\n15 19\n7 14\n12 18\n8 16\n7 4\n11 12\n7 10\n11 14\n2 17\n9 7\n3 11\n2 7\n1 7\n16 2\n5 17", "output": "2" }, { "input": "21\n3 19\n3 17\n4 3\n11 3\n3 16\n8 3\n7 3\n3 21\n3 9\n13 3\n18 3\n12 3\n3 1\n10 3\n3 5\n3 20\n3 14\n3 2\n6 3\n3 15", "output": "10" }, { "input": "21\n7 8\n20 7\n7 6\n14 7\n3 7\n7 19\n7 9\n2 7\n4 7\n16 7\n7 5\n7 13\n7 10\n15 7\n7 11\n7 17\n7 21\n18 7\n7 1\n7 12", "output": "10" }, { "input": "23\n3 9\n9 15\n9 19\n10 9\n9 4\n9 8\n1 9\n9 6\n9 21\n9 18\n20 9\n9 13\n9 23\n14 9\n5 9\n12 9\n9 16\n2 9\n11 9\n17 9\n7 9\n22 9", "output": "11" }, { "input": "23\n15 6\n15 23\n15 10\n11 15\n15 19\n15 7\n12 15\n15 8\n4 15\n22 15\n20 15\n17 15\n3 15\n15 13\n21 15\n15 16\n9 15\n5 15\n14 15\n1 15\n15 18\n2 15", "output": "11" } ]
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: &lt;expression&gt; ::= &lt;variable&gt; | (&lt;expression&gt;) &lt;operator&gt; (&lt;expression&gt;) &lt;variable&gt; ::= 'A' | 'B' | 'C' | 'D' | 'a' | 'b' | 'c' | 'd' &lt;operator&gt; ::= '&amp;' | '|' Here large letters *A*,<=*B*,<=*C*,<=*D* represent variables, and small letters represent their negations. For example, if *A*<==<=1, then character 'A' corresponds to value 1, and value character 'a' corresponds to value 0. Here character '&amp;' corresponds to the operation of logical AND, character '|' corresponds to the operation of logical OR. You are given expression *s*, defining function *f*, where some operations and variables are missing. Also you know the values of the function *f*(*A*,<=*B*,<=*C*,<=*D*) for some *n* distinct sets of variable values. Count the number of ways to restore the elements that are missing in the expression so that the resulting expression corresponded to the given information about function *f* in the given variable sets. As the value of the result can be rather large, print its remainder modulo 109<=+<=7.
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 contain the descriptions of the sets: the *i*-th of them contains five integers *a**i*,<=*b**i*,<=*c**i*,<=*d**i*,<=*e**i* (0<=≀<=*a**i*,<=*b**i*,<=*c**i*,<=*d**i*,<=*e**i*<=≀<=1), separated by spaces and meaning that *f*(*a**i*,<=*b**i*,<=*c**i*,<=*d**i*)<==<=*e**i*. It is guaranteed that all the tuples (*a**i*,<=*b**i*,<=*c**i*,<=*d**i*) are distinct.
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", "((?)&amp;(?))|((?)&amp;(?))\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)&amp;(a)', '(A)&amp;(b)', '(A)&amp;(C)', '(A)&amp;(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 has height *t* and has *n* panels on the wall. Each panel is a horizontal segment at height *h**i* which begins at *l**i* and ends at *r**i*. The *i*-th panel connects the points (*l**i*,<=*h**i*) and (*r**i*,<=*h**i*) of the plane. The top of the wall can be considered a panel connecting the points (<=-<=109,<=*t*) and (109,<=*t*). Similarly, the bottom of the wall can be considered a panel connecting the points (<=-<=109,<=0) and (109,<=0). No two panels share a common point. Emuskald knows that for the waterfall to be aesthetically pleasing, it can flow from panel *i* to panel *j* () only if the following conditions hold: 1. *max*(*l**i*,<=*l**j*)<=&lt;<=*min*(*r**i*,<=*r**j*) (horizontal projections of the panels overlap); 1. *h**j*<=&lt;<=*h**i* (panel *j* is below panel *i*); 1. there is no such panel *k* (*h**j*<=&lt;<=*h**k*<=&lt;<=*h**i*) that the first two conditions hold for the pairs (*i*,<=*k*) and (*k*,<=*j*). Then the flow for is equal to *min*(*r**i*,<=*r**j*)<=-<=*max*(*l**i*,<=*l**j*), the length of their horizontal projection overlap. Emuskald has decided that in his waterfall the water will flow in a single path from top to bottom. If water flows to a panel (except the bottom of the wall), the water will fall further to exactly one lower panel. The total amount of water flow in the waterfall is then defined as the minimum horizontal projection overlap between two consecutive panels in the path of the waterfall. Formally: 1. the waterfall consists of a single path of panels ; 1. the flow of the waterfall is the minimum flow in the path . To make a truly great waterfall Emuskald must maximize this water flow, but there are too many panels and he is having a hard time planning his creation. Below is an example of a waterfall Emuskald wants: Help Emuskald maintain his reputation and find the value of the maximum possible water flow.
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<=&lt;<=*h**i*<=&lt;<=*t*, <=-<=109<=≀<=*l**i*<=&lt;<=*r**i*<=≀<=109), the height, left and right ends of the *i*-th panel segment. It is guaranteed that no two segments share a common point.
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 topic very badly, and broke Arthur's favorite correct bracket sequence just to spite him. All Arthur remembers about his favorite sequence is for each opening parenthesis ('(') the approximate distance to the corresponding closing one (')'). For the *i*-th opening bracket he remembers the segment [*l**i*,<=*r**i*], containing the distance to the corresponding closing bracket. Formally speaking, for the *i*-th opening bracket (in order from left to right) we know that the difference of its position and the position of the corresponding closing bracket belongs to the segment [*l**i*,<=*r**i*]. Help Arthur restore his favorite correct bracket sequence!
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*<=&lt;<=2*n*), representing the segment where lies the distance from the *i*-th opening bracket and the corresponding closing one. The descriptions of the segments are given in the order in which the opening brackets occur in Arthur's favorite sequence if we list them from left to right.
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 1", "output": "()(())()()()" }, { "input": "3\n1 4\n2 2\n1 1", "output": "IMPOSSIBLE" }, { "input": "5\n1 3\n2 5\n3 4\n1 2\n1 9", "output": "()((()))()" }, { "input": "2\n3 3\n3 3", "output": "IMPOSSIBLE" }, { "input": "1\n1 1", "output": "()" }, { "input": "10\n9 9\n5 5\n1 1\n1 1\n13 13\n11 11\n1 1\n5 5\n1 1\n1 1", "output": "IMPOSSIBLE" }, { "input": "50\n97 97\n61 61\n59 59\n29 29\n13 13\n7 7\n5 5\n3 3\n1 1\n3 3\n1 1\n13 13\n11 11\n9 9\n3 3\n1 1\n1 1\n1 1\n27 27\n25 25\n1 1\n21 21\n19 19\n17 17\n15 15\n5 5\n1 1\n1 1\n7 7\n1 1\n3 3\n1 1\n15 15\n13 13\n1 1\n9 9\n7 7\n5 5\n3 3\n1 1\n15 15\n5 5\n1 1\n1 1\n3 3\n1 1\n1 1\n1 1\n1 1\n1 1", "output": "((((((((())))(()))((((())()()))))((()(((((()())(()(()))))))))))((()((((()))))))((()())(())()())())()" }, { "input": "1\n1 1", "output": "()" }, { "input": "2\n2 2\n1 1", "output": "IMPOSSIBLE" }, { "input": "3\n1 5\n1 5\n1 3", "output": "()()()" }, { "input": "4\n4 4\n1 1\n2 2\n2 2", "output": "IMPOSSIBLE" }, { "input": "5\n2 9\n3 8\n5 5\n1 9\n1 9", "output": "(((()())))" }, { "input": "6\n11 11\n2 2\n1 1\n1 1\n1 6\n2 6", "output": "IMPOSSIBLE" }, { "input": "7\n2 2\n7 7\n6 7\n1 3\n2 2\n3 3\n2 2", "output": "IMPOSSIBLE" }, { "input": "8\n10 10\n9 10\n1 1\n2 2\n1 1\n1 1\n8 9\n1 3", "output": "IMPOSSIBLE" }, { "input": "9\n11 13\n3 4\n3 3\n3 3\n2 2\n2 3\n3 4\n1 4\n3 3", "output": "IMPOSSIBLE" }, { "input": "10\n5 11\n4 4\n2 2\n3 7\n4 7\n2 5\n2 7\n2 6\n1 6\n3 4", "output": "IMPOSSIBLE" } ]
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 polygon and then draws a square with two opposite vertices lying in the chosen points, what will the expectation of this square's area be? A pair of distinct points is chosen uniformly among all pairs of distinct points, located inside or on the border of the polygon. Pairs of points *p*,<=*q* (*p*<=β‰ <=*q*) and *q*,<=*p* are considered the same. Help Ksusha! Count the required expectation.
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 vertex that goes *i*-th in that order.
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.2856838963" }, { "input": "7\n-29 5\n-20 13\n-4 22\n8 20\n21 9\n16 -29\n-23 -30", "output": "339.3631202393" }, { "input": "8\n-18 -20\n16 -28\n29 -29\n29 25\n15 27\n-19 30\n-28 18\n-30 -16", "output": "499.5660003682" }, { "input": "6\n13 -22\n26 -11\n30 12\n22 19\n-10 24\n-29 6", "output": "293.0788486799" }, { "input": "6\n13 -22\n26 -11\n30 12\n22 19\n-10 24\n-29 6", "output": "293.0788486799" }, { "input": "6\n16 -23\n28 -4\n24 4\n14 14\n-13 5\n-27 -4", "output": "209.4794108807" }, { "input": "9\n-30 -17\n-30 -12\n-28 24\n-21 29\n8 29\n18 14\n26 -11\n9 -29\n-25 -30", "output": "451.0823539138" }, { "input": "4\n-1000000 -1000000\n-1000000 1000000\n1000000 1000000\n1000000 -1000000", "output": "666667333333.4891400000" }, { "input": "4\n1000 1000\n-1000 1000\n-1000 -1000\n1000 -1000", "output": "667333.5000000012" }, { "input": "6\n-30 30\n21 30\n29 5\n27 -7\n13 -20\n-24 -30", "output": "470.6151913267" }, { "input": "7\n-16 -30\n18 -12\n23 -1\n-3 5\n-20 5\n-22 1\n-23 -27", "output": "206.0253957422" }, { "input": "5\n-30 3\n-27 27\n25 19\n22 11\n-9 -19", "output": "269.9315435441" }, { "input": "5\n-12 -21\n25 12\n2 28\n-25 24\n-26 -16", "output": "281.2625079157" }, { "input": "6\n-14 2\n11 28\n15 -2\n15 -28\n13 -30\n9 -26", "output": "200.7414038240" }, { "input": "4\n-4 -20\n10 6\n-7 23\n-27 -17", "output": "165.4239773277" }, { "input": "4\n-13 -21\n22 -14\n19 -2\n-20 27", "output": "216.0781961373" }, { "input": "8\n22 -28\n29 -1\n28 9\n26 14\n19 22\n-3 29\n-9 26\n-29 -14", "output": "364.5501756826" }, { "input": "21\n-999896 -977584\n-981090 -998920\n-955737 -999433\n-836670 -999598\n-131267 -999903\n873713 -999850\n988083 -997441\n997685 -976930\n998079 -967700\n999138 -906399\n999967 -779494\n999875 608972\n998802 997105\n997089 999540\n891311 999873\n46007 999962\n-991551 999757\n-999301 968587\n-999579 875737\n-999730 549856\n-999960 51168", "output": "666126548753.4987800000" }, { "input": "28\n-999858 -784428\n-999517 -939769\n-999016 -999730\n-947991 -999977\n583432 -999987\n867592 -999817\n964501 -998866\n996003 -995533\n998716 -984290\n999618 -770526\n999783 -567026\n999971 -186389\n999952 107304\n999801 823582\n998874 906964\n997851 952716\n995210 986155\n990112 999169\n958740 999772\n855833 999915\n-86160 999966\n-983466 999718\n-986557 998983\n-992779 997480\n-999153 910765\n-999678 633173\n-999792 567889\n-999874 -13740", "output": "666134075600.9393300000" }, { "input": "23\n-998149 -915915\n-995073 -944069\n-987635 -988407\n-951050 -993464\n-922353 -996259\n-897593 -998630\n-395571 -999848\n891364 -999885\n998896 -999857\n999954 -241069\n999992 665232\n999954 972998\n997503 995272\n918826 998906\n805190 999725\n-160875 999945\n-868222 999408\n-910092 998323\n-989088 995823\n-996325 994987\n-999234 862952\n-999462 618604\n-999999 -892066", "output": "665633654239.2847900000" }, { "input": "28\n-999992 -308892\n-999961 -775849\n-999942 -893083\n-999456 -939329\n-999012 -979265\n-997835 -988395\n-989576 -999615\n-707413 -999804\n-356518 -999960\n425690 -999948\n816619 -999822\n996426 -999657\n998911 -998620\n999895 -957899\n999936 -732657\n999772 776916\n999714 916237\n997146 988196\n996127 992122\n985658 999257\n878494 999690\n700248 999995\n-73464 999954\n-462535 999888\n-996558 999759\n-999198 954171\n-999766 940472\n-1000000 740830", "output": "666355240785.8035900000" }, { "input": "31\n-999895 -680066\n-999346 -911069\n-997066 -995888\n-933871 -999458\n-859686 -999993\n915737 -999963\n980078 -999252\n991475 -998112\n997928 -987831\n998589 -957777\n999526 -904903\n999931 -256314\n999918 618991\n999004 979093\n994226 996559\n992378 997893\n985535 998354\n842908 999545\n497158 999818\n58368 999831\n-524382 999768\n-870707 999721\n-896847 999681\n-930692 999452\n-960753 999218\n-994745 997730\n-998380 996409\n-999326 941484\n-999695 880174\n-999953 516095\n-999959 432711", "output": "666145135876.4797400000" }, { "input": "29\n-999821 -886199\n-996548 -965159\n-995362 -987300\n-981634 -998332\n-908076 -999145\n-527101 -999912\n446850 -999985\n816225 -999959\n938653 -998846\n974582 -992905\n997351 -984725\n998758 -966579\n999674 -891109\n999991 -582065\n999947 570950\n999860 851333\n999509 915545\n998980 973970\n998488 994168\n990799 998749\n736579 999807\n176123 999895\n-889019 999998\n-919642 999796\n-952356 998373\n-974729 997284\n-994908 993778\n-999862 963244\n-999863 157789", "output": "666005823973.8554700000" }, { "input": "26\n-999979 -518088\n-999921 118990\n-999886 336599\n-999749 963071\n-999613 971778\n-997505 993563\n-988075 999871\n-724057 999931\n338004 999963\n939763 999692\n979017 999536\n997778 999063\n999010 994592\n999549 991252\n999866 130647\n999941 -152592\n999901 -975591\n998258 -992117\n992764 -999373\n845158 -999949\n592531 -999958\n-810995 -999934\n-988512 -999694\n-995382 -992825\n-997701 -979912\n-999550 -923898", "output": "666361287504.7816200000" }, { "input": "21\n-999986 557566\n-999907 933716\n-999221 982044\n-991618 996837\n-989578 998869\n-959290 999999\n941636 999993\n982544 999663\n999879 996043\n999991 618961\n999916 -733361\n999799 -901915\n999501 -995358\n995904 -999369\n883475 -999921\n355631 -999988\n-521129 -999838\n-777567 -999730\n-996849 -999383\n-999891 -926971\n-999957 -555457", "output": "666453766184.6261000000" }, { "input": "28\n-999950 -887483\n-999941 -898020\n-999541 -954162\n-998380 -979624\n-996809 -989471\n-974206 -998846\n-814103 -999939\n-562253 -999986\n38935 -999954\n949101 -999859\n967558 -998977\n974753 -998193\n980440 -995185\n995399 -975723\n999674 -949939\n999839 -915815\n999975 -114127\n999981 971763\n999561 997999\n989473 999261\n917401 999944\n-468594 999853\n-693444 999675\n-914976 999234\n-997478 996529\n-999955 951845\n-999982 144675\n-999992 -224003", "output": "666225623675.0697000000" }, { "input": "3\n217949 72327\n20730 141135\n667478 391012", "output": "22999661597.9778060000" }, { "input": "6\n-476965 -162114\n-528389 -150910\n-582349 -113696\n-668732 17055\n-505653 187057\n-320725 378075", "output": "19121483829.0815280000" }, { "input": "7\n682441 915628\n544253 794138\n613454 689307\n748362 488382\n785654 635758\n813511 747405\n740114 896236", "output": "11812099113.1968730000" }, { "input": "11\n-66147 342486\n-84392 250625\n-79482 200009\n114303 -75951\n143385 -63417\n228821 15223\n234858 79257\n222911 116017\n174646 182146\n101613 255473\n27282 308745", "output": "15436765129.4917930000" } ]
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 country is represented by islands connected by two-way roads. Between some islands there is no road way, even through other islands, that's why the country is divided into several regions. More formally, each island belongs to exactly one region, there is a path between any two islands located in the same region; there is no path between any two islands from different regions. A region is lucky if the amount of islands in it is a lucky number. As a real president, Petya first decided to build a presidential palace. Being a lucky numbers' fan, Petya wants to position his palace in one of the lucky regions. However, it is possible that initially the country has no such regions. In this case Petya can build additional roads between different regions, thus joining them. Find the minimum number of roads needed to build to create a lucky region.
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*). Some roads can connect an island with itself; there can be more than one road between a pair of islands. Numbers in each line are separated by exactly one space character.
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", "output": "-1" }, { "input": "2 3\n1 2\n2 1\n1 2", "output": "-1" }, { "input": "10 1\n1 10", "output": "2" }, { "input": "4 6\n1 2\n1 3\n1 4\n3 4\n2 4\n2 3", "output": "0" }, { "input": "16 14\n2 3\n2 4\n4 3\n5 6\n7 6\n8 9\n12 13\n12 14\n15 14\n13 15\n13 14\n12 15\n16 13\n15 16", "output": "1" }, { "input": "15 14\n2 3\n2 4\n3 4\n5 6\n6 7\n8 9\n8 10\n8 11\n12 13\n12 14\n14 15\n13 15\n12 15\n13 14", "output": "0" }, { "input": "4 1\n3 3", "output": "3" }, { "input": "7 1\n5 5", "output": "3" }, { "input": "47 10\n1 1\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10", "output": "3" }, { "input": "47 45\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47", "output": "1" }, { "input": "20 10\n13 7\n19 1\n5 10\n3 2\n6 19\n3 12\n12 10\n4 3\n5 7\n11 9", "output": "1" }, { "input": "20 50\n1 13\n1 13\n11 4\n15 8\n8 4\n4 1\n19 12\n15 16\n9 20\n2 16\n9 11\n17 6\n2 11\n9 6\n3 7\n4 14\n14 6\n9 4\n4 16\n11 7\n14 7\n15 2\n12 12\n15 13\n6 8\n5 9\n20 19\n20 14\n12 8\n11 11\n12 10\n20 19\n5 18\n5 19\n10 13\n17 13\n2 15\n17 12\n9 7\n7 6\n19 18\n13 3\n11 19\n3 7\n15 11\n15 15\n5 10\n13 8\n17 14\n2 2", "output": "-1" }, { "input": "20 1\n5 15", "output": "2" }, { "input": "20 5\n10 19\n11 20\n14 3\n16 15\n19 1", "output": "1" }, { "input": "20 30\n17 4\n10 17\n12 3\n9 11\n17 2\n13 12\n6 11\n14 19\n11 14\n6 19\n18 3\n15 1\n10 18\n3 20\n16 15\n9 6\n13 12\n17 5\n5 13\n20 18\n8 19\n13 1\n2 5\n12 15\n5 19\n6 13\n15 16\n19 19\n19 15\n16 6", "output": "-1" }, { "input": "20 15\n10 9\n4 14\n16 13\n13 5\n9 10\n20 13\n13 11\n16 14\n14 9\n14 20\n1 19\n8 20\n7 12\n3 20\n7 17", "output": "1" }, { "input": "30 20\n29 21\n22 2\n8 18\n16 15\n9 12\n28 12\n24 1\n15 23\n1 14\n18 3\n14 10\n5 8\n19 4\n13 29\n15 29\n30 24\n28 25\n17 3\n8 26\n27 20", "output": "0" }, { "input": "30 4\n4 28\n7 18\n16 17\n16 11", "output": "1" }, { "input": "47 20\n41 32\n5 36\n4 14\n15 45\n10 36\n24 7\n12 25\n12 43\n31 19\n28 11\n41 43\n9 39\n33 20\n33 28\n28 1\n39 19\n8 20\n36 42\n22 45\n29 7", "output": "0" }, { "input": "50 50\n17 30\n29 29\n50 42\n15 18\n34 10\n30 3\n44 11\n4 35\n42 8\n14 41\n30 4\n11 1\n3 23\n7 28\n35 6\n24 37\n6 12\n8 7\n36 40\n41 26\n13 46\n15 40\n32 34\n15 28\n46 31\n36 25\n38 43\n16 19\n35 9\n6 21\n16 42\n38 43\n32 12\n12 32\n43 21\n16 35\n3 11\n24 13\n36 24\n43 22\n29 36\n18 15\n29 47\n30 31\n4 7\n18 42\n9 48\n17 32\n42 9\n2 6", "output": "1" }, { "input": "64 18\n35 37\n30 35\n56 39\n18 46\n26 35\n39 48\n50 27\n5 12\n28 54\n18 51\n31 60\n1 63\n5 51\n20 5\n23 53\n64 11\n17 50\n14 27", "output": "0" }, { "input": "100 40\n13 92\n21 56\n53 71\n55 8\n35 39\n45 54\n84 14\n16 15\n15 7\n75 31\n76 5\n32 70\n31 71\n77 67\n38 10\n27 48\n24 28\n33 19\n61 70\n26 74\n11 85\n20 15\n67 17\n37 38\n20 66\n8 31\n62 8\n2 7\n71 90\n91 67\n55 35\n7 57\n10 22\n85 64\n18 61\n75 23\n64 50\n30 99\n27 45\n49 42", "output": "0" }, { "input": "100 3\n54 47\n85 25\n59 47", "output": "1" }, { "input": "1000 1\n908 655", "output": "2" }, { "input": "1000 10\n117 228\n530 348\n119 116\n239 658\n102 791\n736 786\n617 192\n396 109\n286 509\n754 207", "output": "1" }, { "input": "5000 24\n4725 3848\n4135 4259\n2109 3306\n213 3847\n1627 3379\n4290 2314\n9 774\n3174 4435\n1778 3821\n1128 1465\n3011 74\n501 2861\n2935 1995\n989 4268\n3944 1154\n3172 1888\n3817 2992\n398 4722\n4873 4958\n1196 4398\n3441 1536\n4259 3474\n4559 1598\n2163 2612", "output": "1" }, { "input": "100000 1\n16796 47477", "output": "2" }, { "input": "20 19\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20", "output": "-1" }, { "input": "7 6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7", "output": "0" }, { "input": "20 17\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19", "output": "-1" }, { "input": "47 42\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n37 38\n38 39\n39 40\n40 41\n42 43\n43 44\n44 45\n45 46", "output": "4" }, { "input": "7 5\n1 2\n2 3\n3 4\n4 5\n6 7", "output": "1" }, { "input": "4 2\n2 3\n3 4", "output": "1" } ]
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, in which all disjunctions consist of no more than two arguments (variables). For the purpose of this problem we consider only 2-SAT formulas where each disjunction consists of exactly two arguments. Consider the following 2-SAT problem as an example: . Note that there might be negations in 2-SAT formula (like for *x*1 and for *x*4). Artem now tries to solve as many problems with 2-SAT as possible. He found a very interesting one, which he can not solve yet. Of course, he asks you to help him. The problem is: given two 2-SAT formulas *f* and *g*, determine whether their sets of possible solutions are the same. Otherwise, find any variables assignment *x* such that *f*(*x*)<=β‰ <=*g*(*x*).
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 formula *f*. The description consists of exactly *m*1 pairs of integers *x**i* (<=-<=*n*<=≀<=*x**i*<=≀<=*n*,<=*x**i*<=β‰ <=0) each on separate line, where *x**i*<=&gt;<=0 corresponds to the variable without negation, while *x**i*<=&lt;<=0 corresponds to the variable with negation. Each pair gives a single disjunction. Next *m*2 lines contains formula *g* in the similar format.
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/82cbca427149bc7516804c97b5963cbfddd08900.png" style="max-width: 100.0%;max-height: 100.0%;"/>. But the second formula is 1, because <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c8a53f3cfcb77e8a34515ff1420d44e5e2c3db0b.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[]
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 program: if the current coordinates of the robot are (*x*,<=*y*), then commands U, L, D, R move it to cells (*x*,<=*y*<=+<=1), (*x*<=-<=1,<=*y*), (*x*,<=*y*<=-<=1), (*x*<=+<=1,<=*y*) respectively. The execution of the program started at time 0. The program is looped, i.e. each *l* seconds of executing the program start again from the first character. Unfortunately, we don't know what program was loaded into the robot when he left the lab. Our radars managed to find out the position of the robot at *n* moments of time: we know that at the moment of time *t**i* the robot is at the point (*x**i*,<=*y**i*). Given this data, either help to determine what program could be loaded into the robot, or determine that no possible program meets the data and the robot must have broken down.
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*<=&lt;<=*t**i*<=+<=1 for all *i* from 1 to *n*<=-<=1.
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 supervillain stuff, but then suddenly they stumble across a regular competitive programming problem. You are given a tree with $n$ vertices. In each vertex $v$ there is positive integer $a_{v}$. You have to answer $q$ queries. Each query has a from $u$ $v$ $x$. You have to calculate $\prod_{w \in P} gcd(x, a_{w}) \mod (10^{9} + 7)$, where $P$ is a set of vertices on path from $u$ to $v$. In other words, you are to calculate the product of $gcd(x, a_{w})$ for all vertices $w$ on the path from $u$ to $v$. As it might be large, compute it modulo $10^9+7$. Here $gcd(s, t)$ denotes the greatest common divisor of $s$ and $t$. Note that the numbers in vertices do not change after queries. I suppose that you are more interested in superhero business of Thanos and Dr. Strange than in them solving the problem. So you are invited to solve this problem instead of them.
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 guaranteed that graph with these edges is a tree. In the next line there are $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_{v} \le 10^{7}$). In the next line there is one integer $q$ ($1 \le q \le 10^{5}$)Β β€” the number of queries. And in the next $q$ lines the queries are described. Each query is described with three integers $u_{i}$, $v_{i}$ and $x_{i}$ ($1 \le u_{i}, v_{i} \le n$, $1 \le x_{i} \le 10^{7}$).
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, enter the cells with treasures, obstacles and bombs. To pick the treasures, you need to build a closed path (starting and ending in the starting cell). The closed path mustn't contain any cells with bombs inside. Let's assume that the sum of the treasures' values that are located inside the closed path equals *v*, and besides, you've made *k* single moves (from one cell to another) while you were going through the path, then such path brings you the profit of *v*<=-<=*k* rubles. Your task is to build a closed path that doesn't contain any bombs and brings maximum profit. Note that the path can have self-intersections. In order to determine if a cell lies inside a path or not, use the following algorithm: 1. Assume that the table cells are points on the plane (the table cell on the intersection of the *i*-th column and the *j*-th row is point (*i*,<=*j*)). And the given path is a closed polyline that goes through these points. 1. You need to find out if the point *p* of the table that is not crossed by the polyline lies inside the polyline. 1. Let's draw a ray that starts from point *p* and does not intersect other points of the table (such ray must exist). 1. Let's count the number of segments of the polyline that intersect the painted ray. If this number is odd, we assume that point *p* (and consequently, the table cell) lie inside the polyline (path). Otherwise, we assume that it lies outside.
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 empty; - digit *c* (1-8) is treasure with index *c*; - character "." is an empty cell; - character "#" is an obstacle. Assume that the map has *t* treasures. Next *t* lines contain the prices of the treasures. The *i*-th line contains the price of the treasure with index *i*, *v**i* (<=-<=200<=≀<=*v**i*<=≀<=200). It is guaranteed that the treasures are numbered from 1 to *t*. It is guaranteed that the map has not more than 8 objects in total. Objects are bombs and treasures. It is guaranteed that the map has exactly one character "S".
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 look at all the squares (their sides not necessarily parallel to the coordinate axes) with corners at the special points. For each such square Jzzhu paints a dot in every unit square that is fully inside it. After that some unit squares can contain several dots. Now Jzzhu wonders, how many dots he has painted on the plane. Find this number modulo 1000000007 (109<=+<=7).
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. Your task is to find the *k*-th smallest interesting for Igor integer in the hexadecimal notation. The integer should not contain leading zeros.
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" }, { "input": "200000000 10", "output": "bebc200" }, { "input": "1 1", "output": "1" }, { "input": "2 1", "output": "2" }, { "input": "3 1", "output": "3" }, { "input": "4 1", "output": "4" }, { "input": "6 1", "output": "6" }, { "input": "7 1", "output": "7" }, { "input": "8 1", "output": "8" }, { "input": "9 1", "output": "9" }, { "input": "10 1", "output": "a" }, { "input": "12 1", "output": "c" }, { "input": "18 1", "output": "13" }, { "input": "1167959139 3", "output": "4683acbb" }, { "input": "100 1", "output": "6a" }, { "input": "641009859 3", "output": "26a9469b" }, { "input": "1524125987 8", "output": "5ad85123" }, { "input": "1000 1", "output": "4a3" }, { "input": "2000 1", "output": "95b" }, { "input": "5000 1", "output": "19dc" }, { "input": "702209411 8", "output": "29dadd83" }, { "input": "1585325539 3", "output": "5fb74be8" }, { "input": "1058376259 9", "output": "3f158a43" }, { "input": "50000 1", "output": "13a5b" }, { "input": "70000 1", "output": "1cb74" }, { "input": "90000 1", "output": "26eaf" }, { "input": "100000 1", "output": "2b7d4" }, { "input": "1941492387 8", "output": "73b8d2a3" }, { "input": "1000000 1", "output": "256a18" }, { "input": "2000000 1", "output": "50f4d8" }, { "input": "3000000 1", "output": "7d90a8" }, { "input": "4000000 1", "output": "a92d5b" }, { "input": "824608515 4", "output": "312dc989" }, { "input": "900000000 1", "output": "2681c493a" }, { "input": "1000000000 1", "output": "2c40e8619" }, { "input": "1300000000 1", "output": "3e8d62fa5" }, { "input": "1500000000 1", "output": "4b1cda965" }, { "input": "1700000000 1", "output": "57ba24938" }, { "input": "1900000000 1", "output": "6349ae508" }, { "input": "2000000000 1", "output": "6a07d23f8" }, { "input": "44340 1", "output": "fedc" }, { "input": "1999999999 1", "output": "6a07d23f5" }, { "input": "546481140 1", "output": "fedcba98" }, { "input": "546481139 1", "output": "fedcba97" }, { "input": "546481138 1", "output": "fedcba96" }, { "input": "546481137 1", "output": "fedcba95" }, { "input": "546481136 1", "output": "fedcba94" }, { "input": "546481135 1", "output": "fedcba93" }, { "input": "546481134 1", "output": "fedcba92" }, { "input": "546481141 1", "output": "102345678" }, { "input": "546481142 1", "output": "102345679" }, { "input": "546481143 1", "output": "10234567a" }, { "input": "546481144 1", "output": "10234567b" }, { "input": "546481145 1", "output": "10234567c" }, { "input": "546481146 1", "output": "10234567d" }, { "input": "546481147 1", "output": "10234567e" }, { "input": "2691939 4", "output": "29141a" }, { "input": "1802030518 10", "output": "6b68cdb6" }, { "input": "685146646 10", "output": "28d68216" }, { "input": "1863230070 5", "output": "6f0f3e77" }, { "input": "1041313494 5", "output": "3e117ed6" }, { "input": "219396918 2", "output": "ea9f693" }, { "input": "1102513046 6", "output": "41b70514" }, { "input": "1985629174 6", "output": "765a4ee7" }, { "input": "1458679894 10", "output": "56f1b056" }, { "input": "341796022 10", "output": "145f64b6" }, { "input": "1519879446 7", "output": "5a97851b" }, { "input": "1452405440 3", "output": "57b44cc7" }, { "input": "200000000 2", "output": "d5e0c94" }, { "input": "200000000 3", "output": "c013b12" }, { "input": "200000000 4", "output": "bec960c" }, { "input": "200000000 6", "output": "bebc20b" }, { "input": "200000000 7", "output": "bebc200" }, { "input": "200000000 8", "output": "bebc200" }, { "input": "200000000 9", "output": "bebc200" }, { "input": "567234712 4", "output": "21d3aa38" }, { "input": "385192384 9", "output": "16f591c0" }, { "input": "574832319 3", "output": "22a95637" }, { "input": "1500000000 4", "output": "59776758" }, { "input": "1999999999 2", "output": "8e535823" }, { "input": "123456789 2", "output": "83b4f3b" }, { "input": "3544444 8", "output": "36157c" }, { "input": "234566 10", "output": "39446" }, { "input": "23444234 8", "output": "165bb0a" }, { "input": "1000034 1", "output": "256a79" }, { "input": "324234 5", "output": "4f28a" }, { "input": "1994456432 4", "output": "76f4e197" }, { "input": "1234543238 8", "output": "4995a286" }, { "input": "1988888884 3", "output": "7821526a" }, { "input": "1933456288 4", "output": "7351a5a9" }, { "input": "1243123 5", "output": "12f7f4" }, { "input": "1265 2", "output": "4f5" }, { "input": "3245457 4", "output": "31865a" }, { "input": "1999999999 10", "output": "773593ff" }, { "input": "1988976545 10", "output": "768d5fa1" }, { "input": "400000 4", "output": "61a85" }, { "input": "1231234 3", "output": "12d4a0" } ]
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 friends want to simplify the draft by deleting some segments or parts of segments so that the final masterpiece meets three conditions: 1. Horace wants to be able to paint the whole picture in one stroke: by putting the brush on the paper and never taking it off until the picture is ready. The brush can paint the same place multiple times. That's why all the remaining segments must form a single connected shape. 1. Menshykov wants the resulting shape to be simple. He defines a simple shape as a shape that doesn't contain any cycles. 1. Initially all the segment on the draft have integer startpoint and endpoint coordinates. Uslada doesn't like real coordinates and she wants this condition to be fulfilled after all the changes. As in other parts the draft is already beautiful, the friends decided to delete such parts of the draft that the sum of lengths of the remaining segments is as large as possible. Your task is to count this maximum sum of the lengths that remain after all the extra segments are removed.
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. All segments are non-degenerative and either are strictly horizontal or strictly vertical. No two horizontal segments share common points. No two vertical segments share common points.
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 altogether or delete the middle of the third segment. The last way is shown on the picture.
[]
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 is located at the ground level. Recently a local newspaper "Malevich and Life" wrote that the most fashionable way to decorate a fence in the summer is to draw a fuchsia-colored rectangle on it, the lower side of the rectangle must be located at the lower edge of the fence. Vasya is delighted with this idea! He immediately bought some fuchsia-colored paint and began to decide what kind of the rectangle he should paint. Vasya is sure that the rectangle should cover *k* consecutive planks. In other words, he will paint planks number *x*, *x*<=+<=1, ..., *x*<=+<=*k*<=-<=1 for some *x* (1<=≀<=*x*<=≀<=*n*<=-<=*k*<=+<=1). He wants to paint the rectangle of maximal area, so the rectangle height equals *min* *a**i* for *x*<=≀<=*i*<=≀<=*x*<=+<=*k*<=-<=1, *x* is the number of the first colored plank. Vasya has already made up his mind that the rectangle width can be equal to one of numbers of the sequence *k*1,<=*k*2,<=...,<=*k**m*. For each *k**i* he wants to know the expected height of the painted rectangle, provided that he selects *x* for such fence uniformly among all *n*<=-<=*k**i*<=+<=1 possible values. Help him to find the expected heights.
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) and the next line contains *m* space-separated integers *k*1,<=*k*2,<=...,<=*k**m* (1<=≀<=*k**i*<=≀<=*n*) where *k**i* is the width of the desired fuchsia-colored rectangle in planks.
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, the expected height of the fence equals <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0ed28200fa09ec6d42acd5ca3000c84f76b97429.png" style="max-width: 100.0%;max-height: 100.0%;"/>; - There are two possible positions of the fence for *k*<sub class="lower-index">2</sub> = 2. For the first position (*x* = 1) the height is 2, for the second one (*x* = 2) the height is 1. The expected height of the fence equals <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3b5954017d5a606755151a3722724884260d7e16.png" style="max-width: 100.0%;max-height: 100.0%;"/>; - There is the only possible position of the fence for *k*<sub class="lower-index">3</sub> = 3. The expected height of the fence equals 1.
[]
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 Mogohu-Rea for help. The priests conveyed the god's will to him: to control these three cities he should put an idol to Mogohu-Rea β€” that will create a religious field over the cities. However, the idol is so powerful that it can easily drive the people around it mad unless it is balanced by exactly three sacrifice altars, placed one in each city. To balance the idol the altars should be placed so that the center of mass of the system of these three points coincided with the idol. When counting the center of mass consider that all the altars have the same mass. Now Pinnie-the-Wooh is thinking where to put the idol. He has a list of hills, that are suitable to put an idol there. Help him to identify on which of them you can put an idol without risking to fry off the brains of the cities' population with the religious field. Each city has a shape of a convex polygon such that no three vertexes lie on a straight line. The cities can intersect. Each altar should be attached to the city through a special ceremony, besides, it must be situated on the city's territory (possibly at the border). Thus, there may be several altars on a city's territory, but exactly one of them will be attached to the city. The altars, the idol and the hills are points on the plane, some of them may coincide. The hills are taken into consideration independently from each other, the altars' location for different hills may also be different.
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 the polygon's *i*-th vertex in the counterclockwise order. After the cities' description follows the integer *m* (1<=≀<=*m*<=≀<=105), which represents the number of hills. Next *m* lines each contain two integers *x**j*, *y**j*, they are the coordinates of the *j*-th hill. All the coordinates in the input data do not exceed 5Β·108 in the absolute value.
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 destination is the city *t*. He plans to move along roads from *s* to *t* (*s*<=β‰ <=*t*). That's why Ministry of Fools and Roads has difficult days. The minister is afraid that Mr. President could get into a traffic jam or get lost. Who knows what else can happen! To be sure that everything goes as planned, the minister decided to temporarily make all roads one-way. So each road will be oriented in one of two possible directions. The following conditions must be satisfied: - There should be no cycles along roads after orientation. - The city *s* should be the only such city that all its roads are oriented out (i.e. there are no ingoing roads to the city *s* and the city *s* is the only such city). - The city *t* should be the only such city that all its roads are oriented in (i.e. there are no outgoing roads from the city *t* and the city *t* is the only such city). Help the minister solve his problem. Write a program to find any such orientation of all roads or report that no solution exists.
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 number of cities, the number of roads and indices of departure and destination cities. The cities are numbered from 1 to *n*. The following *m* lines contain roads, one road per line. Each road is given as two integer numbers *x**j*, *y**j* (1<=≀<=*x**j*,<=*y**j*<=≀<=*n*, *x**j*<=β‰ <=*y**j*), which means that the *j*-th road connects cities *x**j* and *y**j*. There is at most one road between any pair of cities. It is possible to reach any city from any other moving along roads. The sum of values *n* over all cases in a test doesn't exceed 4Β·105. The sum of values *m* over all cases in a test doesn't exceed 106.
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 example New Year tree of height 3: Polar bears love decorating the New Year tree and Limak is no exception. To decorate the tree, he must first find its root, i.e. a vertex with exactly two neighbours (assuming that *h*<=β‰₯<=2). It won't be easy because Limak is a little bear and he doesn't even see the whole tree. Can you help him? There are *t* testcases. In each testcase, you should first read *h* from the input. Then you can ask at most 16 questions of format "? x" (without quotes), where *x* is an integer between 1 and 2*h*<=-<=1, inclusive. As a reply you will get the list of neighbours of vertex *x* (more details in the "Interaction" section below). For example, for a tree on the drawing above after asking "? 1" you would get a response with 3 neighbours: 4, 5 and 7. Your goal is to find the index of the root *y* and print it in the format "! y". You will be able to read *h* for a next testcase only after printing the answer in a previous testcase and flushing the output. Each tree is fixed from the beginning and it doesn't change during your questions.
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 testcase.
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 *n* from left to right. The coordinate of the *i*-th man is *x**i* (*x**i*<=&lt;<=*x**i*<=+<=1,<=*i*<=&lt;<=*n*). PMP should visit all these people one by one in arbitrary order. Each men should be visited exactly once. At the beginning of his tour, he starts at location of *s*-th man and asks him about his experiences. Each time PMP wants to change his location, he should give a ticket to an angel and the angel carries him to his destination. Angels take PMP from one location, fly to his destination and put him down there. Nobody else is visited in this movement. Moving from *i*-th man to *j*-th man, takes |*x**i*<=-<=*x**j*| time. PMP can get back to life as soon as he visits all men. There are two types of angels: Some angels are going to the right and they only accept right tickets. Others are going the left and they only accept left tickets. There are an unlimited number of angels of each type. PMP has *l* left tickets and *n*<=-<=1<=-<=*l* right tickets. PMP wants to get back to life as soon as possible to be able to compete in this year's final instead of the final he missed last year. He wants to know the quickest way to visit all the men exactly once. He also needs to know the exact sequence moves he should make.
The first line of input contains three space-separated integers *n*,<=*l*,<=*s* (2<=≀<=*n*<=≀<=105,<=0<=≀<=*l*<=&lt;<=*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 is *x**i* (0<==<=*x*1<=&lt;<=*x*2<=&lt;<=...<=&lt;<=*x**n*<=≀<=109) β€” the location of *i*-th man.
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 there are multiple answers, output any of them. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "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 move is the following sequence of actions. The chosen chip is marked as the current one. After that the player checks whether there are more chips in the same row (or in the same column) with the current one that are pointed by the arrow on the current chip. If there is at least one chip then the closest of them is marked as the new current chip and the former current chip is removed from the field. After that the check is repeated. This process can be repeated several times. If a new chip is not found, then the current chip is removed from the field and the player's move ends. By the end of a move the player receives several points equal to the number of the deleted chips. By the given initial chip arrangement determine the maximum number of points that a player can receive during one move. Also determine the number of such moves.
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 down correspondingly. It is guaranteed that a field has at least one chip.
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 at most *x* from the root. The distance is the sum of the lengths of edges on the path between nodes. But he has got used to this activity and even grew bored of it. 'Why does he do that, then?' β€” you may ask. It's just that he feels superior knowing that only he can solve this problem. Do you want to challenge Darth Vader himself? Count the required number of nodes. As the answer can be rather large, find it modulo 109<=+<=7.
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 length of the edge that connects each node with its *i*-th child.
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", "output": "943217271" }, { "input": "1 0\n1", "output": "1" }, { "input": "2 1\n1 1", "output": "3" }, { "input": "2 1\n2 2", "output": "1" }, { "input": "86 95031\n1 1 2 1 1 2 1 1 1 1 2 2 2 1 1 1 2 2 1 2 1 1 1 2 1 1 1 1 1 2 2 1 1 1 2 1 1 2 2 1 1 1 2 2 2 2 1 1 1 2 2 2 2 2 1 1 2 1 2 1 2 2 1 2 2 1 1 2 1 1 1 1 1 2 1 2 2 1 1 1 1 2 2 2 2 1", "output": "1000000006" }, { "input": "97 78153\n2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2", "output": "0" }, { "input": "39 93585\n2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 3 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2", "output": "1000000006" }, { "input": "73 80734\n2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 3 2 2 2 2 2 2 2 3 2 2 2 2 2 2 2", "output": "1000000006" }, { "input": "41 26406\n3 3 2 3 3 2 1 3 3 2 2 2 2 2 3 3 2 3 2 2 3 3 3 2 2 2 2 2 2 3 3 2 1 3 2 3 3 2 3 3 3", "output": "0" } ]
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*<=-<=1) = *a**v*<=+<=*tk*; - remove elements *a**v*, *a**v*<=+<=*t*, ..., *a**v*<=+<=*t*Β·*k* from the sequence *a*, the remaining elements should be reindexed *a*1,<=*a*2,<=...,<=*a**n*<=-<=*k*<=-<=1. - permute in some order the remaining elements of sequence *a*. A beauty of a sequence *a* is the minimum number of operations that is needed to delete all elements from sequence *a*. Jeff's written down a sequence of *m* integers *b*1, *b*2, ..., *b**m*. Now he wants to ask *q* questions. Each question can be described with two integers *l**i*,<=*r**i*. The answer to the question is the beauty of sequence *b**l**i*, *b**l**i*<=+<=1, ..., *b**r**i*. You are given the sequence *b* and all questions. Help Jeff, answer all his questions.
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*, *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*m*) β€” the description of *i*-th question.
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 12\n7 9\n11 12\n1 1\n4 10\n12 13\n2 2\n3 5\n5 6\n1 1\n2 3\n2 4\n7 14", "output": "4\n2\n1\n3\n2\n1\n4\n2\n1\n2\n2\n1\n2\n2\n3" }, { "input": "20\n3 2 2 1 4 4 1 2 3 1 1 5 5 3 4 4 1 2 3 1\n20\n13 17\n2 2\n5 5\n19 20\n5 9\n10 14\n2 2\n7 15\n10 12\n16 17\n9 9\n2 3\n4 4\n1 4\n11 19\n1 2\n1 18\n3 13\n10 18\n3 9", "output": "4\n1\n1\n2\n4\n3\n1\n5\n2\n2\n1\n1\n1\n3\n5\n2\n5\n5\n5\n4" }, { "input": "25\n3 1 3 4 1 3 5 3 6 6 2 1 1 1 6 1 3 4 2 6 6 1 5 6 6\n25\n1 1\n10 12\n1 6\n1 8\n17 18\n2 20\n1 2\n1 1\n8 17\n11 19\n3 5\n17 25\n14 19\n5 12\n1 6\n1 17\n14 14\n9 13\n10 14\n18 22\n6 22\n10 11\n13 20\n3 4\n17 21", "output": "1\n3\n3\n4\n2\n6\n2\n1\n4\n5\n3\n6\n5\n5\n3\n6\n1\n3\n3\n4\n6\n2\n5\n2\n4" }, { "input": "30\n5 2 2 6 5 7 1 4 7 4 7 4 5 5 2 5 3 6 7 4 1 5 4 1 2 1 1 7 2 1\n30\n1 23\n19 23\n10 14\n8 22\n6 16\n4 13\n13 25\n9 14\n16 16\n7 7\n11 11\n17 30\n17 29\n8 9\n9 9\n1 1\n1 11\n14 17\n8 25\n6 6\n16 26\n7 24\n10 24\n1 16\n2 13\n14 22\n5 14\n14 19\n8 23\n18 19", "output": "7\n4\n3\n7\n5\n5\n7\n3\n1\n1\n1\n7\n7\n2\n1\n1\n6\n3\n7\n1\n7\n7\n7\n6\n6\n7\n4\n5\n7\n2" }, { "input": "35\n8 6 1 3 8 8 1 6 5 6 5 2 8 2 3 5 5 2 1 6 5 8 1 4 5 1 7 8 6 5 1 3 6 7 6\n35\n18 32\n13 23\n20 22\n11 15\n8 8\n2 13\n2 3\n8 14\n17 26\n7 14\n16 32\n15 16\n14 35\n2 14\n15 20\n10 14\n4 19\n13 17\n9 13\n4 4\n18 24\n9 18\n8 8\n2 17\n4 6\n3 16\n30 31\n1 35\n2 12\n22 24\n17 17\n21 27\n3 5\n1 29\n12 12", "output": "8\n6\n3\n4\n1\n6\n2\n4\n6\n5\n8\n2\n8\n6\n5\n4\n6\n4\n4\n1\n6\n5\n1\n6\n2\n6\n2\n8\n6\n3\n1\n5\n3\n8\n1" }, { "input": "40\n7 5 2 8 5 4 1 5 6 7 7 9 9 2 1 6 2 5 4 7 6 1 6 7 5 9 4 8 4 6 8 1 7 2 3 7 4 5 2 9\n40\n25 36\n21 39\n1 26\n2 21\n11 31\n12 33\n4 4\n2 8\n6 6\n2 9\n2 24\n23 23\n25 39\n3 11\n10 21\n11 24\n39 39\n25 29\n3 7\n1 7\n1 2\n15 36\n5 34\n31 33\n6 30\n8 13\n10 37\n33 35\n14 39\n2 12\n11 12\n6 11\n33 34\n17 23\n17 35\n4 5\n17 29\n10 19\n2 25\n12 30", "output": "9\n9\n8\n8\n8\n8\n1\n5\n1\n6\n8\n1\n9\n7\n7\n7\n1\n4\n5\n6\n2\n9\n8\n3\n8\n4\n9\n3\n9\n8\n2\n5\n2\n6\n9\n2\n8\n7\n8\n8" }, { "input": "45\n10 9 3 4 5 1 3 4 2 10 9 10 9 10 2 4 6 2 5 3 6 4 9 10 3 9 8 1 2 5 9 2 10 4 6 10 8 10 9 1 2 5 8 6 6\n45\n27 31\n7 13\n1 10\n6 22\n14 36\n17 18\n26 44\n27 34\n14 39\n29 35\n33 44\n29 42\n7 10\n2 2\n12 30\n1 2\n2 32\n1 1\n14 42\n5 8\n16 36\n1 37\n17 23\n6 7\n16 25\n1 22\n40 44\n15 27\n12 28\n19 20\n12 14\n1 42\n6 19\n7 13\n27 38\n7 8\n29 38\n6 11\n4 17\n1 34\n4 5\n9 16\n4 8\n11 42\n9 20", "output": "5\n5\n7\n8\n9\n2\n8\n7\n9\n6\n8\n8\n4\n1\n9\n2\n9\n1\n9\n4\n9\n9\n6\n2\n7\n8\n5\n8\n9\n2\n2\n9\n8\n5\n8\n2\n7\n6\n8\n9\n2\n4\n4\n9\n7" }, { "input": "50\n1 4 10 6 8 5 3 7 3 2 9 4 5 11 11 5 11 10 2 1 2 4 1 2 10 4 2 1 3 8 3 3 5 11 7 7 9 6 6 3 6 10 5 1 11 10 2 1 4 9\n50\n26 27\n25 50\n2 44\n6 20\n1 17\n9 15\n6 10\n13 40\n4 9\n2 11\n4 11\n7 46\n2 3\n16 23\n2 28\n24 45\n25 32\n25 29\n8 9\n5 10\n6 20\n12 17\n19 38\n12 48\n8 32\n11 24\n13 21\n6 41\n16 28\n9 28\n42 47\n8 27\n1 4\n5 9\n2 44\n25 29\n25 43\n2 16\n33 35\n2 2\n7 21\n20 41\n20 30\n23 43\n20 27\n2 2\n1 34\n21 23\n19 19\n39 39", "output": "2\n11\n11\n9\n11\n6\n4\n11\n5\n9\n7\n11\n2\n6\n11\n11\n6\n5\n2\n5\n9\n3\n11\n11\n10\n7\n5\n11\n6\n8\n5\n9\n4\n4\n11\n5\n11\n10\n3\n1\n9\n11\n6\n11\n4\n1\n11\n3\n1\n1" }, { "input": "10\n1 2 3 1 2 3 1 1 1 100000\n5\n1 10\n2 9\n3 8\n1 7\n1 3", "output": "4\n3\n3\n3\n3" }, { "input": "1\n1\n1\n1 1", "output": "1" }, { "input": "1\n100000\n5\n1 1\n1 1\n1 1\n1 1\n1 1", "output": "1\n1\n1\n1\n1" } ]
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 towns. In each monastery lives a pilgrim. At the beginning of the year, each pilgrim writes down which monastery is the farthest from the monastery he is living in. If there is more than one farthest monastery, he lists all of them. On the Big Lebowski day each pilgrim picks one town from his paper at random and starts walking to that town. Walter hates pilgrims and wants to make as many of them unhappy as possible by preventing them from finishing their journey. He plans to destroy exactly one town that does not contain a monastery. A pilgrim becomes unhappy if all monasteries in his list become unreachable from the monastery he is living in. You need to find the maximum number of pilgrims Walter can make unhappy. Also find the number of ways he can make this maximal number of pilgrims unhappy: the number of possible towns he can destroy.
The first line contains two integers *n* (3<=≀<=*n*<=≀<=105) and *m* (2<=≀<=*m*<=&lt;<=*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**i* and *b**i* of length *c**i* (1<=≀<=*a**i*,<=*b**i*<=≀<=*n*,<=1<=≀<=*c**i*<=≀<=1000,<=*a**i*<=β‰ <=*b**i*).
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 plane at the distance of at most *r* from the rose. Besides, Roland assumes that the towers should be built at points with integer coordinates and the sum of squares of distances between all pairs of towers must be as large as possible. Note, that Roland may build several towers at the same point, also he may build some of them at point (0,<=0). Help Roland build the towers at the integer points so that the sum of squares of distances between all towers is maximum possible. Note that the distance in this problem is defined as the Euclidian distance between points.
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 at the same point of the plane, also some towers can be located at point (0,<=0). If there are multiple valid optimal arrangements, choose any of them.
[ "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**i*, the second one has name *b**i*. Each pair wondered if there is a substring of the lucky string containing both of their names. If so, they want to find the one with minimum length, which can give them good luck and make their friendship last forever. Please help DZY for each pair find the minimum length of the substring of *s* that contains both *a**i* and *b**i*, or point out that such substring doesn't exist. A substring of *s* is a string *s**l**s**l*<=+<=1... *s**r* for some integers *l*,<=*r* (1<=≀<=*l*<=≀<=*r*<=≀<=|*s*|). The length of such the substring is (*r*<=-<=*l*<=+<=1). A string *p* contains some another string *q* if there is a substring of *p* equal to *q*.
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 guaranteed that all the strings only consist of lowercase English letters.
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 from the uncorrupted part, the crimes could be stopped. So, they are trying to set up a compound where they can keep the corrupted people. To ensure that the criminals don't escape the compound, a watchtower needs to be set up, so that they can be watched. Since the people of Aalam-Aara aren't very rich, they met up with a merchant from some rich town who agreed to sell them a land-plot which has already a straight line fence *AB* along which a few points are set up where they can put up a watchtower. Your task is to help them find out the number of points on that fence where the tower can be put up, so that all the criminals can be watched from there. Only one watchtower can be set up. A criminal is watchable from the watchtower if the line of visibility from the watchtower to him doesn't cross the plot-edges at any point between him and the tower i.e. as shown in figure 1 below, points *X*, *Y*, *C* and *A* are visible from point *B* but the points *E* and *D* are not. Assume that the land plot is in the shape of a polygon and coordinate axes have been setup such that the fence *AB* is parallel to *x*-axis and the points where the watchtower can be set up are the integer points on the line. For example, in given figure 2, watchtower can be setup on any of five integer points on *AB* i.e. (4,<=8), (5,<=8), (6,<=8), (7,<=8) or (8,<=8). You can assume that no three consecutive points are collinear and all the corner points other than *A* and *B*, lie towards same side of fence *AB*. The given polygon doesn't contain self-intersections.
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 the fence *AB* are the first two points, (*x*1,<=*y*1) and (*x*2,<=*y*2).
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 even say that shovels retail market is too small for such a big company to make a profit. But the company management believes in the future of that market and seeks new ways to increase income. There are *n* cities in Berland connected with *m* bi-directional roads. All roads have equal lengths. It can happen that it is impossible to reach a city from another city using only roads. There is no road which connects a city to itself. Any pair of cities can be connected by at most one road. There are *w* Bulmart stores in Berland. Each of them is described by three numbers: - *c**i* β€” the number of city where the *i*-th store is located (a city can have no stores at all or have several of them), - *k**i* β€” the number of shovels in the *i*-th store, - *p**i* β€” the price of a single shovel in the *i*-th store (in burles). The latest idea of the Bulmart management is to create a program which will help customers get shovels as fast as possible for affordable budget. Formally, the program has to find the minimum amount of time needed to deliver *r**j* shovels to the customer in the city *g**j* for the total cost of no more than *a**j* burles. The delivery time between any two adjacent cities is equal to 1. If shovels are delivered from several cities, the delivery time is equal to the arrival time of the last package. The delivery itself is free of charge. The program needs to find answers to *q* such queries. Each query has to be processed independently from others, i.e. a query does not change number of shovels in stores for the next queries.
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 integer *w* (1<=≀<=*w*<=≀<=5000) β€” the total number of Bulmart stores in Berland. Each of the next *w* lines contains three integers describing the *i*-th store: *c**i*,<=*k**i*,<=*p**i* (1<=≀<=*c**i*<=≀<=*n*,<=1<=≀<=*k**i*,<=*p**i*<=≀<=2Β·105). The next line contains a single integer *q* (1<=≀<=*q*<=≀<=1000) β€” the number of queries. Each of the next *q* lines contains three integers describing the *j*-th query: *g**j*,<=*r**j* and *a**j* (1<=≀<=*g**j*<=≀<=*n*, 1<=≀<=*r**j*,<=*a**j*<=≀<=109)
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 number *b*. Write to the second cell number *b*<=+<=1; 1. Let's assume that at the current time the first cell contains number *a*, and the second cell contains number *b*. Write to the first cell number *a*Β·*b*. Maxim is wondering, how many integers *x* (*l*<=≀<=*x*<=≀<=*r*) are there, such that we can write the number *x* to the first cell of the calculator, having performed at most *p* moves.
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 the fence from *l* to *r* (1<=≀<=*l*<=≀<=*r*<=≀<=*n*) is a sequence of planks of wood with indices from *l* to *r* inclusive, that is, planks with indices *l*,<=*l*<=+<=1,<=...,<=*r*. The width of the piece of the fence from *l* to *r* is value *r*<=-<=*l*<=+<=1. Two pieces of the fence from *l*1 to *r*1 and from *l*2 to *r*2 are called matching, if the following conditions hold: - the pieces do not intersect, that is, there isn't a single plank, such that it occurs in both pieces of the fence; - the pieces are of the same width; - for all *i* (0<=≀<=*i*<=≀<=*r*1<=-<=*l*1) the following condition holds: *h**l*1<=+<=*i*<=+<=*h**l*2<=+<=*i*<==<=*h**l*1<=+<=*h**l*2. John chose a few pieces of the fence and now wants to know how many distinct matching pieces are for each of them. Two pieces of the fence are distinct if there is a plank, which belongs to one of them and does not belong to the other one.
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 *q* lines contain two space-separated integers *l**i* and *r**i* (1<=≀<=*l**i*<=≀<=*r**i*<=≀<=*n*) β€” the boundaries of the *i*-th piece of the fence.
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 to move in both directions. Between each pair of stations there is no more than one passage. Berland mathematicians have recently proved a theorem that states that any classic scheme has a ringroad. There can be only one ringroad. In other words, in any classic scheme one can find the only scheme consisting of stations (where any two neighbouring ones are linked by a passage) and this cycle doesn't contain any station more than once. This invention had a powerful social impact as now the stations could be compared according to their distance from the ringroad. For example, a citizen could say "I live in three passages from the ringroad" and another one could reply "you loser, I live in one passage from the ringroad". The Internet soon got filled with applications that promised to count the distance from the station to the ringroad (send a text message to a short number...). The Berland government decided to put an end to these disturbances and start to control the situation. You are requested to write a program that can determine the remoteness from the ringroad for each station by the city subway scheme.
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 presence of a passage from station *x**i* to station *y**i*. The stations are numbered from 1 to *n* in an arbitrary order. It is guaranteed that *x**i*<=β‰ <=*y**i* and that no pair of stations contain more than one passage. The passages can be used to travel both ways. It is guaranteed that the given description represents a classic subway scheme.
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 working out a project of negotiating with the other presidents. For negotiations between some pairs of the planets there are bidirectional communication channels, each of which is characterized with "dial duration" *t**i* which, as a rule, takes several hours and exceeds the call duration greatly. Overall the galaxy has *n* communication channels and they unite all the planets into a uniform network. That means that it is possible to phone to any planet *v* from any planet *u*, perhaps, using some transitional planets *v*1, *v*2, ..., *v**m* via the existing channels between *u* and *v*1, *v*1 and *v*2, ..., *v**m*<=-<=1 and *v**m*, *v**m* and *v*. At that the dial duration from *u* to *v* will be equal to the sum of dial durations of the used channels. So, every president has to talk one by one to the presidents of all the rest *n*<=-<=1 planets. At that the negotiations take place strictly consecutively, and until the negotiations with a planet stop, the dial to another one does not begin. As the matter is urgent, from the different ways to call the needed planet every time the quickest one is chosen. Little time is needed to assure another president on the importance of the Galaxy Union, that’s why the duration of the negotiations with each planet can be considered equal to the dial duration time for those planets. As the presidents know nothing about each other’s plans, they do not take into consideration the possibility that, for example, the sought president may call himself or already know about the founding of the Galaxy Union from other sources. The governments of all the *n* planets asked you to work out the negotiation plans. First you are to find out for every president how much time his supposed negotiations will take.
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) that represent the numbers of planet joined by a communication channel and its "dial duration". There can be no more than one communication channel between a pair of planets.
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 choose some sets from the sets he has. Let's suppose that *i*1,<=*i*2,<=...,<=*i**k* (1<=≀<=*i*1<=&lt;<=*i*2<=&lt;<=...<=&lt;<=*i**k*<=≀<=*m*) are indexes of the chosen sets. Then let's define an array of integers *b*, sorted in ascending order, as a union of the chosen sets, that is, . We'll represent the element with number *j* in this array (in ascending order) as *b**j*. Sereja considers his choice of sets correct, if the following conditions are met: Sereja wants to know what is the minimum number of sets (*k*) that he can choose so that his choice will be correct. Help him with that.
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*. It is guaranteed that the sets form partition of all integers from 1 to *n*.
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 &amp;K*. The &amp;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 description below carefully and follow it and not the similar rules in real programming languages. There is a very powerful system of pointers on &amp;K* β€” you can add an asterisk to the right of the existing type *X* β€” that will result in new type *X*<=*<=. That is called pointer-definition operation. Also, there is the operation that does the opposite β€” to any type of *X*, which is a pointer, you can add an ampersand β€” that will result in a type &amp;*X*, to which refers *X*. That is called a dereference operation. The &amp;K* language has only two basic data types β€” void and errtype. Also, the language has operators typedef and typeof. - The operator "typedef *A* *B*" defines a new data type *B*, which is equivalent to *A*. *A* can have asterisks and ampersands, and *B* cannot have them. For example, the operator typedef void** ptptvoid will create a new type ptptvoid, that can be used as void**.- The operator "typeof *A*" returns type of *A*, brought to void, that is, returns the type void**...*, equivalent to it with the necessary number of asterisks (the number can possibly be zero). That is, having defined the ptptvoid type, as shown above, the typeof ptptvoid operator will return void**. An attempt of dereferencing of the void type will lead to an error: to a special data type errtype. For errtype the following equation holds true: errtype*<==<=&amp;errtype<==<=errtype. An attempt to use the data type that hasn't been defined before that will also lead to the errtype. Using typedef, we can define one type several times. Of all the definitions only the last one is valid. However, all the types that have been defined earlier using this type do not change. Let us also note that the dereference operation has the lower priority that the pointer operation, in other words &amp;*T*<=*<= is always equal to *T*. Note, that the operators are executed consecutively one by one. If we have two operators "typedef &amp;void a" and "typedef a* b", then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &amp;void* = void (see sample 2). Vasya does not yet fully understand this powerful technology, that's why he asked you to help him. Write a program that analyzes these operators.
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 ampersands. All the data type names are non-empty lines of no more than 20 lowercase Latin letters. The number of asterisks and ampersands separately in one type in any operator does not exceed 10, however if we bring some types to void with several asterisks, their number may exceed 10.
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 &amp;&amp;ptv node\ntypeof node\ntypeof &amp;ptv\n", "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &amp;b b\ntypeof b\ntypeof c\ntypedef &amp;&amp;b* c\ntypeof c\ntypedef &amp;b* c\ntypeof c\ntypedef &amp;void b\ntypeof b\ntypedef b******* c\ntypeof c\ntypedef &amp;&amp;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 &amp;b = &amp;void* = void. At that, the с type doesn't change. After that the с type is defined as &amp;&amp;b* = &amp;&amp;void* = &amp;void = errtype. It doesn't influence the b type, that's why the next typedef defines c as &amp;void* = void. Then the b type is again redefined as &amp;void = errtype. Please note that the c type in the next query is defined exactly as errtype******* = errtype, and not &amp;void******* = void******. The same happens in the last typedef.
[ { "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\ntypeof b\ntypedef b******* c\ntypeof c\ntypedef &&b* c\ntypeof c", "output": "void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype" }, { "input": "10\ntypeof void\ntypedef void voiddd\ntypeof &&&&&voiddd*********\ntypeof &&&&&voidddd*********\ntypedef aaaa bbbb\ntypeof bbbb\ntypeof aaaa\ntypedef void** aaaa\ntypeof aaaa\ntypeof bbbb", "output": "void\nvoid****\nerrtype\nerrtype\nerrtype\nvoid**\nerrtype" }, { "input": "10\ntypedef &errtype********* xekls\ntypeof &xekls*\ntypedef xekls*** xekls\ntypeof &xekls********\ntypedef &void*** xekls\ntypeof &xekls\ntypedef &errtype******* dwkmly\ntypeof &&dwkmly******\ntypedef void******* zkpahsnsumbnnzi\ntypeof zkpahsnsumbnnzi*", "output": "errtype\nerrtype\nvoid*\nerrtype\nvoid********" }, { "input": "10\ntypedef errtype** ucywcaykzh\ntypeof &ucywcaykzh*********\ntypedef &&&&&&void********* ucywcaykzh\ntypeof &&&ucywcaykzh******\ntypedef &errtype vfqmtssewklwhiukrz\ntypeof &&vfqmtssewklwhiukrz********\ntypedef &errtype********** xvhxopvh\ntypeof &xvhxopvh\ntypedef &void****** kieypzcclmsvce\ntypeof &&&&&kieypzcclmsvce**", "output": "errtype\nvoid******\nerrtype\nerrtype\nvoid**" }, { "input": "10\ntypedef &&&&&void* mbmrmohbydctgukqbueu\ntypeof &mbmrmohbydctgukqbueu*******\ntypedef &&mbmrmohbydctgukqbueu******* xollxoxrhif\ntypeof xollxoxrhif********\ntypedef &&&&void** zijybfnyxmodoahvvikd\ntypeof zijybfnyxmodoahvvikd****\ntypedef &void**** to\ntypeof &to*******\ntypedef &void******* yhhoqgpnysxvft\ntypeof &&&yhhoqgpnysxvft*******", "output": "errtype\nerrtype\nerrtype\nvoid*********\nvoid**********" }, { "input": "10\ntypedef &&&&void******* xqldryeid\ntypeof &xqldryeid*\ntypedef &void****** frgqt\ntypeof &&frgqt*********\ntypedef &void******* xqldryeid\ntypeof xqldryeid*\ntypedef errtype* xqldryeid\ntypeof &xqldryeid****\ntypedef &&&&&xqldryeid***** cuyhdvkkfyjzjmdkgcf\ntypeof cuyhdvkkfyjzjmdkgcf********", "output": "void***\nvoid************\nvoid*******\nerrtype\nerrtype" }, { "input": "10\ntypedef void**** iizqen\ntypeof iizqen****\ntypedef &void**** gdq\ntypeof &&gdq*********\ntypedef &&errtype******** lhndwyohjckrcew\ntypeof &lhndwyohjckrcew*\ntypedef &&&&void********** ccuoxbgeui\ntypeof ccuoxbgeui\ntypedef &&&&gdq******** gdq\ntypeof gdq******", "output": "void********\nvoid**********\nerrtype\nvoid******\nvoid*************" }, { "input": "10\ntypedef &void**** youdyfpinzk\ntypeof &youdyfpinzk***\ntypedef &&&&youdyfpinzk****** nfbsgpjzhxzskmxc\ntypeof nfbsgpjzhxzskmxc***\ntypedef &&&void*** puerqioirikxej\ntypeof &puerqioirikxej********\ntypedef &puerqioirikxej******** xzgzsamjdufuyxz\ntypeof &xzgzsamjdufuyxz******\ntypedef &&&&xzgzsamjdufuyxz******* hbyqffrbitdgott\ntypeof hbyqffrbitdgott*****", "output": "void*****\nvoid********\nvoid*******\nvoid************\nvoid***************" }, { "input": "10\ntypedef &errtype*** oomxdcottaxn\ntypeof &oomxdcottaxn*********\ntypedef oomxdcottaxn**** bqbigpn\ntypeof &&bqbigpn**********\ntypedef &&&void******** ilrltx\ntypeof &&ilrltx**********\ntypedef void*** yo\ntypeof yo**********\ntypedef oomxdcottaxn*** bqbigpn\ntypeof &bqbigpn****", "output": "errtype\nerrtype\nvoid*************\nvoid*************\nerrtype" }, { "input": "15\ntypedef &void pt\ntypeof pt\ntypeof pt*\ntypedef pt**** err\ntypeof err\ntypeof &err\ntypeof err*\ntypedef &void*** pt\ntypeof err\ntypeof &err\ntypeof err*\ntypeof pt\ntypeof pt*\ntypeof &&pt*\ntypeof &&&pt", "output": "errtype\nerrtype\nerrtype\nerrtype\nerrtype\nerrtype\nerrtype\nerrtype\nvoid**\nvoid***\nvoid*\nerrtype" }, { "input": "1\ntypeof a", "output": "errtype" }, { "input": "2\ntypedef void errtypea\ntypeof errtypea", "output": "void" }, { "input": "4\ntypedef void voida\ntypedef voida* voidb\ntypedef voidb* voidc\ntypeof voidc", "output": "void**" } ]
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 corners of the cell that are inside or on the border of the lair. Given this information, Heidi wants to know the exact shape of the lair to rain destruction on the zombies. Help her!
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 0 and 4. Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of *y* coordinate, and in one row cells go in the order of increasing *x* coordinate. This means that the first row corresponds to cells with coordinates (1,<=*N*),<=...,<=(*N*,<=*N*) and the last row corresponds to cells with coordinates (1,<=1),<=...,<=(*N*,<=1). The last line of the file contains a zero. This line should not be treated as a test case. The sum of the *N* values for all tests in one file will not exceed 5000.
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 lexicographically smallest vertex.
[ "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="lower-index">2</sub>, *y*<sub class="lower-index">2</sub>) if *x*<sub class="lower-index">1</sub> &lt; *x*<sub class="lower-index">2</sub> or <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f45db1409d1cd11215311a368a0683868ef22505.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[]
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 the other weights. You are to determine this maximum weight described above for each edge. You should calculate the answer for each edge independently, it means there can't be two edges with changed weights at the same time.
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*<=≀<=109) meaning that there is an edge between vertices *u* and *v* with weight *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 conditions are satisfied: 1. If you put a light ray horizontally/vertically into the middle of any unit segment that is side of some border cell, the light will go out from the neighboring unit segment to the segment you put the ray in.1. each unit segment of the grid of the mirror box can be penetrated by at least one light ray horizontally/vertically put into the box according to the rules of the previous paragraph After you tried putting some mirrors, you find out that there are many ways of doing so. How many possible ways are there? The answer might be large, so please find the result modulo prime number *MOD*.
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 '<=/<=', '\', '*', where '*' denotes that the mirror in that grid has been destroyed. It is guaranteed that the number of '*' is no more than 200.
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/8d9b0974a3bf579f6c12ba1ba0aa3796ea59060e.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f2ee1062a283d466b9df9ba3fa7c40522b51114e.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4a86df1213fb62655431f8fa11acc9097af41cd8.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f2ee1062a283d466b9df9ba3fa7c40522b51114e.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4ae9fe994b7cb99b6fd878d2ecb5479a2b0c079d.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4a86df1213fb62655431f8fa11acc9097af41cd8.png" style="max-width: 100.0%;max-height: 100.0%;"/> 4. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4ae9fe994b7cb99b6fd878d2ecb5479a2b0c079d.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8d9b0974a3bf579f6c12ba1ba0aa3796ea59060e.png" style="max-width: 100.0%;max-height: 100.0%;"/> 5. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f2ee1062a283d466b9df9ba3fa7c40522b51114e.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4ae9fe994b7cb99b6fd878d2ecb5479a2b0c079d.png" style="max-width: 100.0%;max-height: 100.0%;"/> The answer is then module by 3 so the output should be 2.
[]
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 dinner parties all over the country. Bying a Tree at the New Year and installing it in the house is a tradition. Usually the whole family decorates the tree on the New Year Eve. We hope that Codeforces is a big and loving family, so in this problem we are going to decorate a tree as well. So, our decoration consists of *n* pieces, each piece is a piece of colored paper, its border is a closed polyline of a special shape. The pieces go one by one as is shown on the picture. The *i*-th piece is a polyline that goes through points: (0,<=0), (0,<=*y*0), (1,<=*y*1), (2,<=*y*2), ..., (*k*,<=*y**k*), (*k*,<=0). The width of each piece equals *k*. The piece number 1 (shown red on the figure) is the outer piece (we see it completely), piece number 2 (shown yellow) follows it (we don't see it completely as it is partially closed by the first piece) and so on. The programmers are quite curious guys, so the moment we hung a decoration on the New Year tree we started to wonder: what area of each piece can people see?
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 points (0,<=0), (0,<=*y**i*,<=0), (1,<=*y**i*,<=1), (2,<=*y**i*,<=2), ..., (*k*,<=*y**i*,<=*k*), (*k*,<=0) (1<=≀<=*y**i*,<=*j*<=≀<=1000).
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 of the string is either letter "B", or letter "W". Unfortunately, the string is very old and some characters are damaged. The damaged positions are denoted as "X". The Little Elephant in determined to restore the string and hang it on the wall. For that he needs to replace each character "X" by a "B" or a "W". The string must look good on the wall, so it must be beautiful. The Little Elephant considers a string beautiful if it has two non-intersecting substrings of a given length *k*, such that the left one fully consists of characters "B", and the right one fully consists of characters "W". More formally, there are four integers *a*,<=*b*,<=*c*,<=*d* (1<=≀<=*a*<=≀<=*b*<=&lt;<=*c*<=≀<=*d*<=≀<=|*s*|;Β *b*<=-<=*a*<=+<=1<==<=*d*<=-<=*c*<=+<=1<==<=*k*) such that *s**i* = "B" (*a*<=≀<=*i*<=≀<=*b*) and *s**j* = "W" (*c*<=≀<=*j*<=≀<=*d*). Help the Little Elephant find the number of different beautiful strings he can obtain from string *s*. Two strings are considered different if there is such position, where the character in the first string differs from the corresponding character in the second string. If this string doesn't contain characters Β«XΒ» and it is already beautiful β€” the answer is 1. As the answer can be rather large, print it modulo 1000000007 (109<=+<=7).
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" }, { "input": "2 1\nWX", "output": "0" }, { "input": "10 3\nXBXBXWXBXW", "output": "4" }, { "input": "10 1\nXXXWXBXBXX", "output": "126" }, { "input": "20 2\nXXXBXWBBBBWXWWWXWWXB", "output": "128" }, { "input": "20 1\nXBBBWBXXBWXWWWBWWXXW", "output": "64" }, { "input": "10 3\nBXBWBWWWWX", "output": "2" }, { "input": "10 3\nWWXBWXXWWW", "output": "0" }, { "input": "20 3\nBWWWWWXBWXXXXBBWBBWX", "output": "0" }, { "input": "20 4\nWWWWXWBXBBXWWXWXBWWW", "output": "8" }, { "input": "20 5\nXBBXXXXXXXBWWXBWBWWX", "output": "4" }, { "input": "20 6\nXXBXBXXBWWWWXWWXBBBW", "output": "10" }, { "input": "20 7\nBWXBXBWWXXBBXBBWXWBW", "output": "0" }, { "input": "25 7\nXWBXWBXWXWBWXBWWXBWXXXXBB", "output": "0" }, { "input": "300 7\nXXWXWWXXWWWBBXXXWWBWBWXWWWXXWBXXBXWWXXXWXWXBXWBWWWXBBWBXBXWXWBBBBXWBXWBBBXBXXXXBWXXXWBBXWXXBBWBBXBBXWXBXWBWXBWXXXXWXBWBBWXWXWXWWBWWWXXWWBBBXBXBWXBXXWWWBWWWBXWWXXWXWBWWWBWWWXXBBWXWXWXXWXXXXBWBXXBXBBBXBBWWXWBBXXWBBXBBXXBWXXWBWWBXWXXWWXXBXBWBWXWBBBWXXWXXWBWBBXWWBBWXXXBBWWXXXXBBBBBBWWBBWBWBXXWWXXBWXWWBB", "output": "469056403" }, { "input": "17 3\nBBBWXXWBXBXWXBXBW", "output": "28" }, { "input": "100 20\nWXWXWWBWBBXXXWWBBXXWBWWWWXXBWXXBXXBXXBXXWXXXXWBBWWWBXWWBXWXWWBXXWXWXWWXXWWWXXXBXBWXWXBXXXWXXWWXWWBWW", "output": "0" }, { "input": "100 3\nXXXXXWXXXXBXXWXXBXXBXXXXXXXXXXXXXXXXWWXXWXXWXXXXWWXXXXXWXXXXBWXXXXWXBXXXWXXXBWXXWXXXWXXXXWWWXXXXXWXX", "output": "951778854" }, { "input": "100 25\nBXWWXWBWWWWWBXBBWXWWXBBWBWWWWWWWXWXWXXXWWWXXXXBXBBXWXWWXBBXWBXWXWXXXXXWBBXWXXWXWWWBXXWBXWWXBWWBXBXXB", "output": "0" }, { "input": "100 20\nWBBXWBBWWWXBXWWWWWBBBWWBWWBWWWWXWXXBWWBWXWBBXWBWBXWXWXWWBWWWBBXWWBXBBBWBXXWWBXXWWXWXWXBBBWXXWWBWBWXW", "output": "0" }, { "input": "100 1\nXWXWXWWXXWWXWXXWWXXBXBXWWXXXXWBWWXWXWWXBXWXBWXWWWBWXXWXBBXWXWXWWBXXWBBBWWWXXXWBWXWXWBWBWBWXXWXBXXWWX", "output": "23240159" }, { "input": "100 1\nWBWWWXWWWXWBWXWXWXXWWBWBXWWBBWWWXXWBWWBWWXWWXBBBWBWWWWXBWWWWWXXWWWWBBBWBBBWWWXWWXWBWWBWWWBWWWBWXXWXX", "output": "1048576" }, { "input": "100 20\nWWXWWXXXXBBXWBXXXXWXXWXXXXXXXBBXXXXXXXWWXXWXXXXBWBXXXXXXXXXXWXWXXXXWXXXWXXWXXXXBXBXXBXXXXXWBXWBXXXXB", "output": "0" }, { "input": "101 18\nWXXBXXWXXXXXXXXXXXXBBXXBBXXWXBXXBXXXXXXXXBXXXXWBBBXWXXWXWXWXWXWXBXBXXXXXWBXXWXBXXXXXXWXBBXXXXWWXXBWBW", "output": "0" }, { "input": "128 100\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "output": "0" }, { "input": "100 21\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "output": "135234318" }, { "input": "256 100\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", "output": "817953325" }, { "input": "300 200\nWXWWWWXWWBXXXBWWXXXWBWWXXWXXXXWWXXXWXXXXWWWXWBWXWWXWXBWXWWWWBWBBWWWWXWXXXXWWXBXWXWWWWWXXXXXWBXWBXWWWWXXXWXBWXBWXWXXXWXWXXWWXXWXWWWWWXWXWWXWWXXWXWXWWXWXXXXWWWXWWWXXWWWBXXWXWWWXWBWXWWXWWXXXWWXXXWXWXXBWXXXBBWWWXXXXWWXWWXWXWWWWWWWXXWXXWWWWBWWWBWWWWXBXWWWWXXXXWXXXXWBXBXWWWXBWXWXXXWXXXXWWXXWXWWXWBXWXBXWWW", "output": "0" }, { "input": "4 1\nXXXW", "output": "7" }, { "input": "4 2\nXBXW", "output": "1" }, { "input": "2 1\nBW", "output": "1" }, { "input": "2 2\nXX", "output": "0" }, { "input": "3 1\nXBX", "output": "2" }, { "input": "3 2\nXWW", "output": "0" }, { "input": "3 1\nWWW", "output": "0" }, { "input": "3 1\nXBW", "output": "2" }, { "input": "3 1\nWBX", "output": "1" }, { "input": "2 1\nWB", "output": "0" }, { "input": "4 1\nXXWW", "output": "3" }, { "input": "4 2\nXXBW", "output": "0" }, { "input": "4 1\nXXXX", "output": "11" } ]
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 tree in the next year. Limak was responsible for remembering that tree. It would be hard to remember a whole tree. Limak decided to describe it in his notebook instead. He took a pen and wrote *n*<=-<=1 lines, each with two integersΒ β€” indices of two vertices connected by an edge. Now, the New Year is just around the corner and Limak is asked to reconstruct that tree. Of course, there is a problem. He was a very little bear a year ago, and he didn't know digits and the alphabet, so he just replaced each digit with a question markΒ β€” the only character he knew. That means, for any vertex index in his notes he knows only the number of digits in it. At least he knows there were no leading zeroes. Limak doesn't want to disappoint everyone. Please, take his notes and reconstruct a New Year tree. Find any tree matching Limak's records and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1" (without the quotes).
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 become free, the higher is the possibility that Brain will rule the world. The collider's constriction is a rectangular grid rolled into a cylinder and consisting of *n* rows and *m* columns such as is shown in the picture below: In this example *n*<==<=4, *m*<==<=5. Dotted lines are corridores that close each column to a ring, i. e. connect the *n*-th and the 1-th rows of the grid. In the leftmost column of the grid the portals are situated and the tentacles of the World Evil are ready to creep out from there. In the rightmost column the exit doors are located. The tentacles can only get out through those doors. The segments joining the nodes of the grid are corridors. Brain would be glad to let all the tentacles out but he faces a problem: the infinite number of tentacles can creep out of the portals, every tentacle possesses infinite length and some width and the volume of the corridors are, unfortunately, quite limited. Brain could approximately evaluate the maximal number of tentacles that will be able to crawl through every corridor. Now help the mice to determine the maximal number of tentacles of the World Evil that will crawl out of the Large Hadron Collider.
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 vertical corridors' capacities. Corridors are described from left to right and from top to bottom. Every *n*-th vertical corridor connects nodes of the *n*-th and 1-th rows. A corridor's capacity is a non-negative integer that does not exceed 109.
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 has *k* lives. He will act like the follows: - Firstly he will randomly pick one of the corridors going from his current room. Each outgoing corridor has the same probability to be picked. - Then he will go through the corridor and then the process repeats. There are some rooms which have traps in them. The first room definitely has no trap, the *n*-th room definitely has a trap. Each time DZY enters one of these rooms, he will lost one life. Now, DZY knows that if he enters the *n*-th room with exactly 2 lives, firstly he will lost one live, but then he will open a bonus round. He wants to know the probability for him to open the bonus round. Please, help him.
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 trap is no more than 101. It is guaranteed that the first room has no trap, and the *n*-th room has a trap. Then *m* lines follows. Each of them contains two integers *u**i*,<=*v**i*Β (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*;Β *u**i*<=β‰ <=*v**i*), meaning that current corridor connects two rooms *u**i* and *v**i*. It is guaranteed that the corridor system is connected.
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*<=&gt;<=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 with red color. You like to repaint some vertices of the tree. To perform painting you use the function paint that you call with the root of the tree as an argument. Here is the pseudocode of this function: As a result of this function, some vertices may change their colors to white or black and some of them may remain red. Your task is to determine the number of distinct possible colorings of the vertices of the tree. We will assume that the coloring is possible if there is a nonzero probability to get this coloring with a single call of *paint*(1). We assume that the colorings are different if there is a pair of vertices that are painted with different colors in these colorings. Since the required number may be very large, find its remainder of division by 1000000007 (109<=+<=7).
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*<=&lt;<=*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 get from any passable square to any other passable square moving through edge-adjacent passable squares. One passable square is the exit from the basement. The robot is placed exactly in one passable square. Also the robot may be placed in the exit square. Professor is scared of going to the dark basement looking for the robot at night. However, he has a basement plan and the robot's remote control. Using the remote, Professor can send signals to the robot to shift one square left, right, up or down. When the robot receives a signal, it moves in the required direction if the robot's neighboring square in the given direction is passable. Otherwise, the robot stays idle. Professor wrote a sequence of *k* commands on a piece of paper. He thinks that the sequence can lead the robot out of the basement, wherever it's initial position might be. Professor programmed another robot to press the required buttons on the remote according to the notes on the piece of paper. Professor was just about to run the program and go to bed, when he had an epiphany. Executing each command takes some energy and Professor doesn't want to get huge electricity bill at the end of the month. That's why he wants to find in the sequence he has written out the minimal possible prefix that would guarantee to lead the robot out to the exit after the prefix is fulfilled. And that's the problem Professor challenges you with at this late hour.
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 passable). It is possible to get from each passable square to the exit, all squares located by the *n*<=Γ—<=*m* rectangle's perimeter are the walls. Exactly one square is the exit from the basement. The last line contains *k* characters, the description of the sequence of commands that Professor has written out on a piece of paper. "L", "R", "U", "D" stand for commands left, right, up and down correspondingly.
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 leave the exit square, but only the last position of the robot is interesting for us). If Professor is mistaken and no prefix (including the whole sequence) can bring the robot to the exit, print "-1" (without the quotes). If there is the only passable square and it is the exit, print "0" (without the quotes).
[ "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, p_{l+1}, \dots, p_r$. For example, good segments of permutation $[1, 3, 2, 5, 4]$ are: - $[1, 1]$, - $[1, 3]$, - $[1, 5]$, - $[2, 2]$, - $[2, 3]$, - $[2, 5]$, - $[3, 3]$, - $[4, 4]$, - $[4, 5]$, - $[5, 5]$. You are given a permutation $p_1, p_2, \ldots, p_n$. You need to answer $q$ queries of the form: find the number of good subsegments of the given segment of permutation. In other words, to answer one query, you need to calculate the number of good subsegments $[x \dots y]$ for some given segment $[l \dots r]$, such that $l \leq x \leq y \leq r$.
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 following $q$ lines describe queries, each line contains a pair of integers $l$, $r$ separated by space ($1 \leq l \leq r \leq n$).
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 staying in the same cell with exactly one scayger of a gender that is different from its own gender. Cthulhu will not be able to catch Scaygerboss if all the scaygers on the map look nerdy and loveable. The scaygers can move around at different speeds. For each scayger, we are given the time it takes this scayger to move from a cell to an adjacent cell. Cells are adjacent if they share a common side. At any point of time, each cell that does not contain an obstacle can be occupied by an arbitrary number of scaygers. Scaygers cannot move to cells with obstacles. Calculate minimal time in order to make all scaygers look nerdy and loveable if they move optimally toward this goal.
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 a free cell; character '#' stands for a cell with an obstacle. The next line contains 3 integers *r*, *c*, and *t* (1<=≀<=*r*<=≀<=*n*, 1<=≀<=*c*<=≀<=*m*, 1<=≀<=*t*<=≀<=109): the current coordinates of Scaygerboss and the time it takes Scaygerboss to move to an adjacent cell. The next *males* lines contain coordinates and times of male scaygers in the same format as for Scaygerboss. The next *females* lines contain coordinates and times of female scaygers in the same format as for Scaygerboss. (The coordinates and times adhere to the same limits as for Scaygerboss.) All scaygers reside in cells without obstacles. The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. - In subproblem F1 (14 points), the constraints 1<=≀<=*n*,<=*m*<=≀<=11 will hold. - In subproblem F2 (6 points), the constraints 1<=≀<=*n*,<=*m*<=≀<=22 will hold.
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 (2, 1). All the scaygers move between cells in 2 units of time. If Scaygerboss and the female scayger from the cell (1, 1) move to the cell (1, 2), and a male and a female scayger from those residing in the cell (2, 1) move to the cell (1, 1), then all the scaygers will look nerdy and lovable in 2 units of time.
[]
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 conveyor belts: "^", "&lt;" and "&gt;". A "^" belt can bring things upwards. "&lt;" can bring leftwards and "&gt;" can bring rightwards. Let's number the rows with 1 to *n* from top to bottom, the columns with 1 to *m* from left to right. We consider the seats above and below the top of the table are rows 0 and *n*<=+<=1 respectively. Also we define seats to the left of the table and to the right of the table to be column 0 and *m*<=+<=1. Due to the conveyor belts direction restriction there are currently no way for a diner sitting in the row *n*<=+<=1 to be served. Given the initial table, there will be *q* events in order. There are two types of events: - "A *x* *y*" means, a piece of bread will appear at row *x* and column *y* (we will denote such position as (*x*,<=*y*)). The bread will follow the conveyor belt, until arriving at a seat of a diner. It is possible that the bread gets stuck in an infinite loop. Your task is to simulate the process, and output the final position of the bread, or determine that there will be an infinite loop. - "C *x* *y* *c*" means that the type of the conveyor belt at (*x*,<=*y*) is changed to *c*. Queries are performed separately meaning that even if the bread got stuck in an infinite loop, it won't affect further queries.
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 "&lt;^&gt;". Next *q* lines, each line describes an event. The format is "C x y c" or "A x y" (Consecutive elements are separated by a space). It's guaranteed that 1<=≀<=*x*<=≀<=*n*,<=1<=≀<=*y*<=≀<=*m*. *c* is a character from the set "&lt;^&gt;". There are at most 10000 queries of "C" type.
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&gt;&gt;\n^^\nA 2 1\nC 1 2 &lt;\nA 2 1\n", "4 5 7\n&gt;&lt;&lt;^&lt;\n^&lt;^^&gt;\n&gt;&gt;&gt;^&gt;\n&gt;^&gt;&gt;^\nA 3 1\nA 2 2\nC 1 4 &lt;\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 "&lt;", when the bread goes from (2, 1) again, it will get stuck at "&gt;&lt;", 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 when I finally reached the treasure box and opened it, I have accidentally started the clockwork bomb! Luckily, I have met such kind of bombs before and I know that the clockwork mechanism can be stopped by connecting contacts with wires on the control panel of the bomb in a certain manner. I see *n* contacts connected by *n*<=-<=1 wires. Contacts are numbered with integers from 1 to *n*. Bomb has a security mechanism that ensures the following condition: if there exist *k*<=β‰₯<=2 contacts *c*1,<=*c*2,<=...,<=*c**k* forming a circuit, i. e. there exist *k* distinct wires between contacts *c*1 and *c*2, *c*2 and *c*3, ..., *c**k* and *c*1, then the bomb immediately explodes and my story ends here. In particular, if two contacts are connected by more than one wire they form a circuit of length 2. It is also prohibited to connect a contact with itself. On the other hand, if I disconnect more than one wire (i. e. at some moment there will be no more than *n*<=-<=2 wires in the scheme) then the other security check fails and the bomb also explodes. So, the only thing I can do is to unplug some wire and plug it into a new place ensuring the fact that no circuits appear. I know how I should put the wires in order to stop the clockwork. But my time is running out! Help me get out of this alive: find the sequence of operations each of which consists of unplugging some wire and putting it into another place so that the bomb is defused.
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 contain the description of the sought scheme in the same format. It is guaranteed that the starting and the ending schemes are correct (i. e. do not contain cicuits nor wires connecting contact with itself).
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 contacts *a**i* and *b**i* and plug it to the contacts *c**i* and *d**i*. Of course the wire connecting contacts *a**i* and *b**i* should be present in the scheme. If there is no correct sequence transforming the existing scheme into the sought one, output -1.
[ "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 Topcoder SRM 557 Div1-Hard: you are given *n* integers *x*1,<=*x*2,<=...,<=*x**n*. You are allowed to perform the assignments (as many as you want) of the following form *x**i* ^= *x**j* (in the original task *i* and *j* must be different, but in this task we allow *i* to equal *j*). The goal is to maximize the sum of all *x**i*. Now we just change the goal. You are also given *n* integers *y*1,<=*y*2,<=...,<=*y**n*. You should make *x*1,<=*x*2,<=...,<=*x**n* exactly equal to *y*1,<=*y*2,<=...,<=*y**n*. In other words, for each *i* number *x**i* should be equal to *y**i*.
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*. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations.
[ "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 is a plant called Formurosa. If the scientists place a sample of colonies of bacteria on each on Formurosa's leaves, it will activate a complicated nutrition process. During that process color of Formurosa changes to reflect the result of a β€” possibly very complicated β€” logical formula on the species of bacteria, involving constants and the operators | (OR), &amp; (AND) and ^ (XOR). If it is 0, the plant will turn red, otherwise β€” it will turn blue. For example, if the nutrition process of Formurosa is described by the formula: (((?^?)|?)&amp;(1^?)); then Formurosa has four leaves (the "?" signs denote the leaves). If we place 0,<=1,<=0,<=0 on the respective leaves, the result of the nutrition process will be (((0^1)|0)&amp;(1^0))<==<=1, therefore the plant will turn blue. The scientists have *n* colonies of bacteria. They do not know their types; the only thing they know for sure is that not all colonies are of the same type. They want to attempt to determine the bacteria's species by repeated evaluations with Formurosa. During each evaluation they must place exactly one sample on every leaf of the plant. However, they may use multiple samples of one colony during a single evaluation; they can even cover the whole plant with bacteria from one colony! Is it possible for them to always determine the species of each colony, no matter what they are (assuming they are not all the same)?
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Β», Β«?Β», Β«|Β», Β«&amp;Β», Β«^Β», Β«(Β», Β«)Β» and complies with the following grammar: The formula consists of no more than 106 characters.
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((?^?)&amp;?)\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 Search of Shortest Paths in Graphs, Network Flows, the Maximal Increasing Subsequences and Other Magical Objects". She has already developed a search algorithm for the invisibility cloak in complex dynamic systems (Hogwarts is one of them). Hogwarts consists of *n* floors, numbered by integers from 1 to *n*. Some pairs of floors are connected by staircases. The staircases may change its position, moving exactly one end. Formally the situation is like this: if a staircase connects the floors *a* and *b*, then in one move it may modify its position so as to connect the floors *a* and *c* or *b* and *c*, where *c* is any floor different from *a* and *b*. Under no circumstances the staircase can connect a floor with itself. At the same time there can be multiple stairs between a pair of floors. Initially, Harry is on the floor with the number 1. He does not remember on what floor he has lost the cloak and wants to look for it on each of the floors. Therefore, his goal is to visit each of *n* floors at least once. Harry can visit the floors in any order and finish the searching at any floor. Nowadays the staircases move quite rarely. However, Ron and Hermione are willing to put a spell on any of them to help Harry find the cloak. To cause less suspicion, the three friends plan to move the staircases one by one, and no more than once for each staircase. In between shifting the staircases Harry will be able to move about the floors, reachable at the moment from the staircases, and look for his Invisibility Cloak. It is assumed that during all this time the staircases will not move spontaneously. Help the three friends to compose a searching plan. If there are several variants to solve the problem, any valid option (not necessarily the optimal one) will be accepted.
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's moves a staircase's move ... a staircase's move Harry's moves Each "Harry's move" should be represented as a list of floors in the order in which they have been visited. The total amount of elements of these lists must not exceed 106. When you print each list, first print the number of elements in it, and then in the same line print the actual space-separated elements. The first number in the first list should be the number 1 (the floor, from which Harry begins to search). Any list except the first one might contain the zero number of elements. Note that Harry can visit some floors again, but must visit all *n* floors at least once. Two consecutively visited floors must be directly connected by a staircase (at the time Harry goes from one of them to the other one). No two floors that are visited consequtively can be equal. In the description of a "staircase's move" indicate the number of staircase (the staircases are numbered from 1 to *m* in the order in which they are given in the input data) and its new location (two numbers of the connected floors in any order). Any staircase can be moved at most once. If there are several solutions, output any.
[ "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*. However the weights of the items that will be sold in the shop haven't yet been defined. That's what you should determine right now. Your task is to find the set of the items' weights *p*1,<=*p*2,<=...,<=*p**k* (1<=≀<=*p*1<=&lt;<=*p*2<=&lt;<=...<=&lt;<=*p**k*), such that: 1. Any bag will be used. That is, for any *i* (1<=≀<=*i*<=≀<=*n*) there will be such set of items that their total weight will equal *a**i*. We assume that there is the infinite number of items of any weight. You can put multiple items of the same weight in one bag. 1. For any set of items that have total weight less than or equal to *m*, there is a bag into which you can put this set. Similarly, a set of items can contain multiple items of the same weight. 1. Of all sets of the items' weights that satisfy points 1 and 2, find the set with the minimum number of weights. In other words, value *k* should be as small as possible. Find and print the required set.
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<=&lt;<=*a*2<=&lt;<=...<=&lt;<=*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 print *k* space-separated integers *p*1,<=*p*2,<=...,<=*p**k* (1<=≀<=*p*1<=&lt;<=*p*2<=&lt;<=...<=&lt;<=*p**k*). If there are multiple solutions, print any of them.
[ "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* is string *s**i**s**i*<=+<=1... *s**j*. String *s* is a Gray string, if it meets the conditions: - the length of string |*s*| is odd; - character occurs exactly once in the string; - either |*s*|<==<=1, or substrings and are the same and are Gray strings. For example, strings "abacaba", "xzx", "g" are Gray strings and strings "aaa", "xz", "abaxcbc" are not. The beauty of string *p* is the sum of the squares of the lengths of all substrings of string *p* that are Gray strings. In other words, consider all pairs of values *i*,<=*j* (1<=≀<=*i*<=≀<=*j*<=≀<=|*p*|). If substring *p*[*i*... *j*] is a Gray string, you should add (*j*<=-<=*i*<=+<=1)2 to the beauty. Xenia has got string *t* consisting of lowercase English letters. She is allowed to replace at most one letter of the string by any other English letter. The task is to get a string of maximum beauty.
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="upper-index">2</sup> + 3<sup class="upper-index">2</sup> = 12. You can't obtain a more beautiful string. In the second test case it is not necessary to perform any operation. The initial string has the maximum possible beauty.
[]
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 through pipes in direction from node with smaller number to node with greater number. Any amount of fuel can enter through the first node and the last node is connected directly to the synchrophasotron. It is known that every pipe has three attributes: the minimum amount of fuel that should go through it, the maximum amount of fuel that can possibly go through it and the cost of pipe activation. If *c**ij* units of fuel (*c**ij*<=&gt;<=0) flow from node *i* to node *j*, it will cost *a**ij*<=+<=*c**ij*2 tugriks (*a**ij* is the cost of pipe activation), and if fuel doesn't flow through the pipe, it doesn't cost anything. Only integer number of units of fuel can flow through each pipe. Constraints on the minimal and the maximal fuel capacity of a pipe take place always, not only if it is active. You may assume that the pipe is active if and only if the flow through it is strictly greater than zero. Petya doesn't want the pipe system to be overloaded, so he wants to find the minimal amount of fuel, that, having entered the first node, can reach the synchrophasotron. Besides that he wants to impress the sponsors, so the sum of money needed to be paid for fuel to go through each pipe, must be as big as possible.
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 through the pipe and the the activation cost, respectively. (1<=≀<=*s*<=&lt;<=*f*<=≀<=*n*,<=0<=≀<=*l*<=≀<=*h*<=≀<=5,<=0<=≀<=*a*<=≀<=6). It is guaranteed that for each pair of nodes with distinct numbers there will be exactly one pipe between them described in the input.
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". The amount of fuel which will flow into synchrophasotron is not neccessary positive. It could be equal to zero if the minimum constraint of every pipe is equal to zero.
[ "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 from node 2 to node 3. It is impossible. In the third test, the minimum possible amount is 2. You can pass each unit of fuel through two different paths: either 1-&gt;2-&gt;3-&gt;4 or 1-&gt;3-&gt;4. If you use the first path twice, it will cost *a*<sub class="lower-index">12</sub> + 2<sup class="upper-index">2</sup> + *a*<sub class="lower-index">23</sub> + 2<sup class="upper-index">2</sup> + *a*<sub class="lower-index">34</sub> + 2<sup class="upper-index">2</sup>=14. If you use the second path twice, it will cost *a*<sub class="lower-index">13</sub> + 2<sup class="upper-index">2</sup> + *a*<sub class="lower-index">34</sub> + 2<sup class="upper-index">2</sup>=14. However, if you use each path (allowing one unit of fuel go through pipes 1-&gt;2, 2-&gt;3, 1-&gt;3, and two units go through 3-&gt;4) it will cost *a*<sub class="lower-index">12</sub> + 1<sup class="upper-index">2</sup> + *a*<sub class="lower-index">23</sub> + 1<sup class="upper-index">2</sup> + *a*<sub class="lower-index">13</sub> + 1<sup class="upper-index">2</sup> + *a*<sub class="lower-index">34</sub> + 2<sup class="upper-index">2</sup>=15 and it is the maximum possible cost. Also note that since no fuel flows from node 1 to node 4, activation cost for that pipe is not added to the answer.
[ { "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", "output": "2 6" }, { "input": "4\n3 4 2 2 6\n2 3 1 1 6\n1 2 1 3 1\n1 4 2 3 4\n2 4 0 3 2\n1 3 1 2 5", "output": "4 33" }, { "input": "6\n1 2 5 5 0\n1 3 0 5 0\n1 4 0 5 0\n1 5 0 5 0\n1 6 5 5 0\n2 3 0 5 0\n2 4 0 5 0\n2 5 0 5 0\n2 6 5 5 0\n3 4 0 5 0\n3 5 0 5 0\n3 6 5 5 0\n4 5 0 5 0\n4 6 5 5 0\n5 6 5 5 0", "output": "25 225" }, { "input": "4\n2 3 1 2 3\n2 4 0 2 0\n1 3 0 1 6\n1 4 0 0 5\n3 4 0 2 2\n1 2 0 1 3", "output": "1 11" }, { "input": "2\n1 2 0 0 6", "output": "0 0" }, { "input": "2\n1 2 0 0 2", "output": "0 0" }, { "input": "2\n1 2 0 0 5", "output": "0 0" }, { "input": "2\n1 2 0 1 5", "output": "0 0" }, { "input": "2\n1 2 0 1 0", "output": "0 0" }, { "input": "2\n1 2 1 2 0", "output": "1 1" }, { "input": "2\n1 2 0 3 2", "output": "0 0" }, { "input": "2\n1 2 0 2 6", "output": "0 0" }, { "input": "2\n1 2 2 2 5", "output": "2 9" }, { "input": "2\n1 2 1 3 1", "output": "1 2" }, { "input": "2\n1 2 0 5 1", "output": "0 0" }, { "input": "2\n1 2 0 3 4", "output": "0 0" }, { "input": "3\n1 2 0 0 3\n2 3 0 0 2\n1 3 0 0 0", "output": "0 0" }, { "input": "3\n1 2 0 0 1\n1 3 0 0 3\n2 3 0 0 4", "output": "0 0" }, { "input": "3\n1 2 0 1 0\n2 3 0 0 6\n1 3 0 0 0", "output": "0 0" }, { "input": "3\n1 3 0 1 2\n1 2 0 0 5\n2 3 1 1 6", "output": "-1 -1" }, { "input": "3\n1 2 0 1 3\n1 3 1 2 5\n2 3 1 2 4", "output": "2 15" }, { "input": "3\n1 2 1 2 6\n1 3 0 2 4\n2 3 0 2 0", "output": "1 8" }, { "input": "3\n2 3 0 2 5\n1 3 1 2 0\n1 2 1 1 2", "output": "2 10" }, { "input": "3\n2 3 1 1 5\n1 2 0 1 4\n1 3 1 2 2", "output": "2 14" }, { "input": "3\n1 2 0 4 6\n1 3 0 2 2\n2 3 1 4 1", "output": "1 9" }, { "input": "3\n2 3 1 2 4\n1 2 2 2 5\n1 3 0 3 5", "output": "2 17" }, { "input": "3\n1 2 0 5 3\n1 3 2 4 3\n2 3 2 3 4", "output": "4 22" }, { "input": "3\n2 3 4 5 3\n1 3 0 5 2\n1 2 1 3 2", "output": "-1 -1" }, { "input": "4\n1 3 0 0 4\n2 4 0 0 5\n3 4 0 0 2\n2 3 0 0 0\n1 2 0 0 1\n1 4 0 0 4", "output": "0 0" }, { "input": "4\n1 4 0 0 0\n1 3 0 0 0\n2 4 0 0 6\n3 4 0 0 0\n1 2 0 0 1\n2 3 0 0 3", "output": "0 0" }, { "input": "4\n2 4 1 1 5\n1 3 0 1 0\n1 4 0 0 3\n3 4 0 0 0\n1 2 1 1 1\n2 3 1 1 4", "output": "-1 -1" }, { "input": "4\n2 3 0 1 6\n1 4 1 1 5\n1 2 0 1 4\n2 4 0 0 0\n1 3 0 0 0\n3 4 0 1 6", "output": "1 6" }, { "input": "4\n2 3 0 2 3\n2 4 1 2 1\n1 2 0 1 1\n1 4 2 2 3\n1 3 0 2 3\n3 4 0 2 3", "output": "3 11" }, { "input": "4\n1 4 1 2 5\n3 4 0 1 6\n2 4 1 2 4\n2 3 0 2 4\n1 2 0 2 2\n1 3 0 2 6", "output": "2 14" }, { "input": "4\n1 3 0 2 1\n3 4 3 3 4\n2 4 0 3 6\n1 4 1 1 0\n1 2 1 1 4\n2 3 0 1 6", "output": "4 31" }, { "input": "4\n3 4 1 2 5\n1 3 2 3 6\n2 3 1 3 0\n1 4 0 1 0\n1 2 0 1 1\n2 4 0 1 0", "output": "-1 -1" }, { "input": "4\n3 4 2 4 5\n2 3 0 0 1\n1 2 1 4 1\n1 4 2 4 5\n2 4 4 4 5\n1 3 0 4 5", "output": "8 65" }, { "input": "4\n1 2 0 2 3\n1 4 1 2 6\n3 4 2 4 4\n2 4 4 4 2\n1 3 2 4 3\n2 3 0 2 3", "output": "-1 -1" }, { "input": "4\n2 4 2 4 6\n2 3 0 2 2\n1 4 1 2 4\n1 2 3 5 3\n1 3 1 4 5\n3 4 3 5 0", "output": "6 55" }, { "input": "4\n2 4 2 4 2\n2 3 2 2 4\n1 3 2 5 6\n1 2 2 2 1\n1 4 1 5 6\n3 4 2 2 5", "output": "-1 -1" }, { "input": "5\n1 5 0 0 3\n3 5 0 0 2\n1 3 0 0 4\n3 4 0 0 0\n2 3 0 0 6\n1 4 0 0 1\n4 5 0 0 1\n1 2 0 0 3\n2 5 0 0 6\n2 4 0 0 4", "output": "0 0" }, { "input": "5\n4 5 0 0 4\n2 5 0 0 3\n1 5 0 0 4\n1 2 0 0 3\n1 4 0 0 3\n1 3 0 0 4\n2 4 0 0 5\n3 5 0 0 0\n2 3 0 0 0\n3 4 0 0 3", "output": "0 0" }, { "input": "5\n1 3 0 1 0\n1 5 0 1 1\n3 4 1 1 2\n4 5 0 0 1\n3 5 0 1 2\n2 4 0 1 4\n1 4 0 0 6\n2 3 1 1 4\n1 2 0 0 5\n2 5 0 1 0", "output": "-1 -1" }, { "input": "5\n3 5 1 1 0\n2 3 1 1 1\n2 4 0 1 1\n1 4 1 1 1\n1 5 0 0 4\n1 3 0 1 5\n4 5 0 1 3\n2 5 1 1 1\n3 4 0 1 6\n1 2 0 1 1", "output": "-1 -1" }, { "input": "5\n2 3 1 2 3\n1 5 0 1 5\n1 2 0 1 1\n2 5 2 2 5\n1 3 0 1 1\n3 5 0 2 5\n4 5 0 1 2\n1 4 0 1 3\n3 4 2 2 0\n2 4 0 2 2", "output": "-1 -1" }, { "input": "5\n4 5 0 1 0\n2 4 2 2 3\n3 5 0 1 6\n1 3 0 1 6\n1 4 1 1 5\n1 5 0 0 0\n2 3 0 2 6\n2 5 2 2 3\n1 2 1 1 6\n3 4 0 1 0", "output": "-1 -1" }, { "input": "5\n1 4 0 1 5\n2 5 1 1 6\n1 3 1 3 3\n3 5 0 1 5\n1 5 2 3 3\n2 3 0 3 2\n2 4 2 2 1\n1 2 3 3 5\n3 4 1 3 4\n4 5 2 3 1", "output": "6 52" }, { "input": "5\n2 4 0 2 1\n3 4 1 3 6\n1 2 2 3 0\n2 5 0 3 5\n4 5 0 3 2\n1 5 1 3 5\n1 4 2 2 5\n1 3 2 3 6\n2 3 0 1 2\n3 5 0 1 0", "output": "7 57" }, { "input": "5\n4 5 1 3 2\n1 5 2 3 1\n1 3 0 4 0\n2 3 3 3 0\n3 5 3 3 2\n1 4 3 4 2\n2 4 1 3 2\n2 5 0 3 4\n1 2 3 4 0\n3 4 0 1 4", "output": "-1 -1" }, { "input": "5\n2 5 0 3 4\n2 4 4 4 5\n1 3 3 4 1\n2 3 1 4 3\n1 2 1 3 1\n1 4 0 0 5\n4 5 1 4 6\n3 4 0 4 4\n3 5 0 3 0\n1 5 1 3 5", "output": "-1 -1" }, { "input": "5\n1 3 3 5 6\n1 5 0 5 0\n3 4 0 0 2\n1 2 2 4 0\n3 5 3 5 4\n4 5 2 3 2\n2 5 1 3 0\n1 4 0 4 6\n2 4 3 4 0\n2 3 0 2 1", "output": "7 65" }, { "input": "5\n3 4 1 5 5\n1 2 4 4 3\n2 4 0 4 5\n2 3 5 5 4\n4 5 4 5 5\n1 5 4 5 5\n1 3 0 1 1\n1 4 2 3 1\n2 5 2 5 2\n3 5 1 2 1", "output": "-1 -1" }, { "input": "6\n2 5 0 0 3\n1 2 0 0 0\n3 6 0 0 3\n2 3 0 0 1\n1 5 0 0 6\n3 4 0 0 3\n1 4 0 0 4\n1 3 0 0 0\n2 6 0 0 6\n1 6 0 0 1\n4 6 0 0 2\n3 5 0 0 6\n4 5 0 0 1\n5 6 0 0 2\n2 4 0 0 3", "output": "0 0" }, { "input": "6\n3 5 0 0 1\n3 6 0 0 2\n1 5 0 0 1\n2 3 0 0 3\n4 5 0 0 6\n1 6 0 0 2\n1 3 0 0 5\n4 6 0 0 5\n5 6 0 0 2\n2 5 0 0 3\n3 4 0 0 0\n2 6 0 0 0\n1 4 0 0 4\n2 4 0 0 6\n1 2 0 0 0", "output": "0 0" }, { "input": "6\n1 3 0 1 1\n2 5 0 0 5\n1 6 0 0 1\n4 5 0 1 0\n2 6 0 1 5\n2 3 0 1 2\n5 6 1 1 3\n1 4 0 1 2\n1 5 0 1 3\n1 2 0 0 5\n4 6 0 0 5\n3 4 0 1 5\n2 4 0 1 1\n3 5 0 1 5\n3 6 0 0 5", "output": "1 13" }, { "input": "6\n2 4 0 1 3\n3 6 1 1 0\n1 6 0 1 6\n4 6 0 1 4\n1 3 1 1 1\n2 6 0 1 4\n3 5 0 0 3\n3 4 0 1 0\n1 5 1 1 1\n2 5 1 1 5\n4 5 0 0 5\n1 4 0 1 6\n2 3 0 1 4\n5 6 1 1 1\n1 2 0 1 4", "output": "-1 -1" }, { "input": "6\n5 6 2 2 6\n2 5 1 2 2\n1 4 0 1 3\n1 3 2 2 5\n3 5 1 2 6\n1 6 0 1 5\n1 5 0 0 5\n4 5 0 1 5\n2 4 0 0 5\n2 6 0 1 4\n3 4 1 1 5\n4 6 0 2 3\n3 6 0 0 6\n2 3 0 0 4\n1 2 0 1 1", "output": "3 41" }, { "input": "6\n5 6 1 1 6\n1 4 1 1 1\n1 2 0 0 4\n2 6 1 2 3\n2 3 0 0 4\n1 5 0 0 5\n2 5 0 1 4\n2 4 0 2 2\n3 6 0 2 3\n4 6 0 2 4\n1 3 0 2 2\n1 6 2 2 0\n3 4 1 2 3\n4 5 0 1 6\n3 5 1 1 0", "output": "-1 -1" }, { "input": "6\n2 6 0 3 3\n5 6 0 2 0\n1 4 0 2 0\n4 5 1 3 2\n1 2 0 1 2\n1 6 0 3 6\n1 3 0 3 5\n1 5 1 3 0\n2 4 3 3 5\n3 5 0 1 3\n4 6 1 2 4\n3 4 1 1 2\n3 6 0 3 4\n2 5 0 1 5\n2 3 1 1 4", "output": "-1 -1" }, { "input": "6\n2 4 1 2 3\n1 5 1 3 0\n4 6 1 3 3\n1 4 2 2 0\n4 5 2 2 2\n3 6 0 2 2\n2 5 1 2 4\n3 5 2 3 5\n1 2 0 2 2\n5 6 1 2 5\n2 6 3 3 1\n1 3 1 2 3\n1 6 2 2 6\n3 4 1 2 2\n2 3 0 3 6", "output": "-1 -1" }, { "input": "6\n3 6 0 1 0\n4 6 2 4 3\n2 3 2 3 1\n1 4 4 4 3\n1 2 2 3 6\n5 6 0 2 5\n1 6 2 4 3\n1 5 0 4 3\n4 5 0 4 2\n2 4 3 3 6\n3 4 3 4 1\n3 5 0 4 1\n2 6 2 4 0\n2 5 1 3 1\n1 3 0 2 3", "output": "-1 -1" }, { "input": "6\n2 4 0 1 1\n2 3 0 4 4\n1 6 1 4 2\n4 5 3 3 3\n3 4 1 2 0\n1 3 0 2 5\n1 2 3 4 1\n4 6 4 4 2\n2 5 0 2 2\n3 6 2 3 2\n5 6 0 3 1\n1 5 1 4 2\n3 5 1 4 5\n1 4 0 1 6\n2 6 1 3 3", "output": "-1 -1" }, { "input": "6\n4 6 4 5 2\n2 4 1 2 4\n5 6 0 1 0\n3 5 0 1 2\n3 4 1 5 2\n2 6 0 4 0\n1 2 1 2 4\n1 4 0 3 6\n1 3 1 3 4\n2 5 3 4 4\n1 6 0 1 6\n2 3 0 4 1\n3 6 1 2 4\n4 5 1 3 2\n1 5 0 4 2", "output": "-1 -1" }, { "input": "6\n1 3 3 5 5\n5 6 0 0 5\n1 6 1 1 6\n3 5 4 5 6\n3 6 0 0 4\n4 6 5 5 0\n2 6 2 3 1\n1 5 0 4 5\n4 5 3 3 2\n1 2 1 5 5\n2 3 3 5 5\n2 5 2 2 1\n2 4 0 4 1\n1 4 4 4 2\n3 4 1 1 3", "output": "-1 -1" }, { "input": "6\n1 2 0 5 0\n1 3 0 5 0\n1 4 0 5 0\n1 5 0 5 0\n1 6 0 5 0\n2 3 0 5 0\n2 4 0 5 0\n2 5 0 5 0\n2 6 0 5 0\n3 4 0 5 0\n3 5 0 5 0\n3 6 0 5 0\n4 5 0 5 0\n4 6 0 5 0\n5 6 0 5 0", "output": "0 0" }, { "input": "6\n3 4 4 5 4\n2 4 1 4 1\n5 6 0 3 5\n1 2 5 5 1\n2 5 0 4 3\n4 6 0 5 1\n4 5 2 3 2\n1 6 3 4 6\n1 4 1 4 6\n2 3 2 4 6\n1 5 0 5 5\n3 6 0 0 1\n2 6 1 2 6\n1 3 0 5 2\n3 5 1 2 5", "output": "11 141" }, { "input": "6\n5 6 0 1 4\n1 6 2 5 1\n1 4 2 3 6\n4 6 3 5 5\n3 4 1 2 5\n1 5 0 2 6\n1 3 0 1 4\n4 5 0 1 4\n2 3 0 1 3\n2 5 0 5 2\n1 2 1 4 2\n3 6 1 5 0\n2 4 1 5 2\n3 5 0 3 0\n2 6 1 5 2", "output": "8 72" }, { "input": "6\n2 4 1 3 1\n1 3 4 5 4\n2 5 0 1 2\n3 4 0 4 5\n1 5 2 4 2\n1 2 1 5 3\n2 3 0 5 1\n5 6 1 5 4\n2 6 2 4 3\n4 5 2 2 4\n4 6 1 1 1\n1 6 2 4 1\n1 4 2 3 5\n3 6 2 3 3\n3 5 0 4 5", "output": "13 118" }, { "input": "6\n4 6 0 1 5\n3 4 0 1 0\n4 5 0 5 0\n2 5 1 4 4\n1 4 2 4 6\n3 5 1 5 6\n2 3 4 4 4\n2 6 0 0 4\n1 5 1 5 0\n2 4 0 1 0\n1 3 2 2 0\n1 2 2 5 4\n3 6 0 5 4\n1 6 2 5 1\n5 6 1 4 0", "output": "12 133" }, { "input": "6\n1 2 1 2 2\n1 3 1 3 5\n1 4 3 3 3\n1 5 1 1 1\n1 6 3 5 6\n2 3 1 5 5\n2 4 3 3 0\n2 5 0 1 6\n2 6 4 5 5\n3 4 2 3 0\n3 5 1 2 6\n3 6 1 1 2\n4 5 0 1 0\n4 6 4 4 4\n5 6 2 5 5", "output": "-1 -1" }, { "input": "6\n1 2 0 5 0\n1 3 0 5 0\n1 4 0 5 0\n1 5 0 5 0\n1 6 0 5 0\n2 3 0 5 0\n2 4 0 5 0\n2 5 0 5 0\n2 6 0 5 0\n3 4 0 5 0\n3 5 0 5 0\n3 6 0 5 0\n4 5 0 5 0\n4 6 0 5 0\n5 6 1 5 0", "output": "1 5" }, { "input": "6\n1 2 0 5 0\n1 3 0 5 0\n1 4 0 5 0\n1 5 0 5 0\n1 6 0 5 0\n2 3 0 5 0\n2 4 0 5 0\n2 5 0 5 0\n2 6 0 5 0\n3 4 0 5 0\n3 5 0 5 0\n3 6 0 5 0\n4 5 0 5 0\n4 6 0 5 0\n5 6 5 5 0", "output": "5 125" }, { "input": "6\n1 2 1 5 0\n1 3 0 5 0\n1 4 0 5 0\n1 5 0 5 0\n1 6 0 5 0\n2 3 0 5 0\n2 4 0 5 0\n2 5 0 5 0\n2 6 0 5 0\n3 4 0 5 0\n3 5 0 5 0\n3 6 0 5 0\n4 5 0 5 0\n4 6 0 5 0\n5 6 0 5 0", "output": "1 5" }, { "input": "6\n1 2 5 5 0\n1 3 0 5 0\n1 4 0 5 0\n1 5 0 5 0\n1 6 0 5 0\n2 3 0 5 0\n2 4 0 5 0\n2 5 0 5 0\n2 6 0 5 0\n3 4 0 5 0\n3 5 0 5 0\n3 6 0 5 0\n4 5 0 5 0\n4 6 0 5 0\n5 6 0 5 0", "output": "5 125" }, { "input": "6\n1 2 0 5 0\n1 3 0 5 0\n1 4 0 5 0\n1 5 0 5 0\n1 6 5 5 0\n2 3 0 5 0\n2 4 0 5 0\n2 5 0 5 0\n2 6 5 5 0\n3 4 0 5 0\n3 5 0 5 1\n3 6 5 5 0\n4 5 0 5 0\n4 6 5 5 0\n5 6 5 5 0", "output": "25 225" }, { "input": "6\n1 2 5 5 0\n1 3 5 5 0\n1 4 4 5 0\n1 5 5 5 0\n1 6 5 5 3\n2 3 0 5 0\n2 4 0 5 2\n2 5 0 5 0\n2 6 0 5 0\n3 4 1 5 4\n3 5 0 5 0\n3 6 0 5 1\n4 5 0 5 0\n4 6 1 5 0\n5 6 0 5 6", "output": "24 223" }, { "input": "6\n1 2 0 0 0\n1 3 0 0 0\n1 4 0 0 0\n1 5 0 0 0\n1 6 0 0 0\n2 3 0 0 0\n2 4 0 0 0\n2 5 0 0 0\n2 6 0 0 0\n3 4 0 0 0\n3 5 0 0 0\n3 6 0 0 0\n4 5 0 0 0\n4 6 0 0 0\n5 6 0 0 0", "output": "0 0" }, { "input": "6\n1 2 0 5 6\n1 3 0 5 6\n1 4 0 5 6\n1 5 0 5 6\n1 6 0 5 6\n2 3 0 5 6\n2 4 0 5 6\n2 5 0 5 6\n2 6 0 5 6\n3 4 0 5 6\n3 5 0 5 6\n3 6 0 5 6\n4 5 0 5 6\n4 6 0 5 6\n5 6 0 5 6", "output": "0 0" }, { "input": "2\n1 2 0 0 0", "output": "0 0" } ]
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. Thus, he got a rectangular *n*<=Γ—<=*m* table, each cell of the table contained some English letter. Let's number the table rows from top to bottom with integers from 1 to *n*, and columns β€” from left to right with integers from 1 to *m*. After that Vasya looked at the resulting rectangular table and wondered, how many subtables are there, that matches both following conditions: - the subtable contains at most *k* cells with "a" letter; - all letters, located in all four corner cells of the subtable, are equal. Formally, a subtable's definition is as follows. It is defined by four integers *x*1,<=*y*1,<=*x*2,<=*y*2 such that 1<=≀<=*x*1<=&lt;<=*x*2<=≀<=*n*, 1<=≀<=*y*1<=&lt;<=*y*2<=≀<=*m*. Then the subtable contains all such cells (*x*,<=*y*) (*x* is the row number, *y* is the column number), for which the following inequality holds *x*1<=≀<=*x*<=≀<=*x*2,<=*y*1<=≀<=*y*<=≀<=*y*2. The corner cells of the table are cells (*x*1,<=*y*1), (*x*1,<=*y*2), (*x*2,<=*y*1), (*x*2,<=*y*2). Vasya is already too tired after he's been writing letters to a piece of paper. That's why he asks you to count the value he is interested in.
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. Changing the class to its neighbour (child-class or parent-class) in the tree costs 1 coin. You can not change the class back. The cost of changing the class *a* to the class *b* is equal to the total cost of class changes on the path from *a* to *b* in the class tree. 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*.
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 transportation to get to junction *b*. There are *k* bus transportation companies. At the beginning of every second, a bus from the *i*-th company chooses a random shortest path between junction *s**i* and junction *t**i* and passes through it. There might be no path from *s**i* to *t**i*. In that case no bus will leave from *s**i* to *t**i*. If a bus passes through a junction where Urpal stands, he can get on the bus. He can also get off the bus at any junction along the path. Now Urpal wants to know if it's possible to go to the date using public transportation in a finite amount of time (the time of travel is the sum of length of the traveled roads) and what is the minimum number of buses he should take in the worst case. At any moment Urpal knows only his own position and the place where the date will be. When he gets on the bus he knows only the index of the company of this bus. Of course Urpal knows the city map and the the pairs (*s**i*,<=*t**i*) for each company. Note that Urpal doesn't know buses velocity.
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**i* to junction *v**i*. All roads in the input will be distinct. The next line contains an integer *k* (0<=≀<=*k*<=≀<=100). There will be *k* lines after this, each containing two integers *s**i* and *t**i* (1<=≀<=*s**i*,<=*t**i*<=≀<=*n*;Β *s**i*<=β‰ <=*t**i*) saying there is a bus route starting at *s**i* and ending at *t**i*. Please note that there might be no path from *s**i* to *t**i*, this case is described in the problem statement.
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 time, KleofΓ‘Ε‘... gained interest in the museum, so to say. You should process *q* events of three types: - type 1 β€” the museum displays an exhibit with value *v* and mass *w*; the exhibit displayed in the *i*-th event of this type is numbered *n*<=+<=*i* (see sample explanation for more details)- type 2 β€” the museum removes the exhibit with number *x* and stores it safely in its vault- type 3 β€” KleofΓ‘Ε‘ visits the museum and wonders (for no important reason at all, of course): if there was a robbery and exhibits with total mass at most *m* were stolen, what would their maximum possible total value be? For each event of type 3, let *s*(*m*) be the maximum possible total value of stolen exhibits with total mass <=≀<=*m*. Formally, let *D* be the set of numbers of all exhibits that are currently displayed (so initially *D* = {1, ..., n}). Let *P*(*D*) be the set of all subsets of *D* and let Then, *s*(*m*) is defined as Compute *s*(*m*) for each . Note that the output follows a special format.
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* and *w**i* (1<=≀<=*v**i*<=≀<=1<=000<=000, 1<=≀<=*w**i*<=≀<=1000)Β β€” the value and mass of the *i*-th exhibit. The next line contains a single integer *q* (1<=≀<=*q*<=≀<=30<=000)Β β€” the number of events. Each of the next *q* lines contains the description of one event in the following format: - 1 *v* *w* β€” an event of type 1, a new exhibit with value *v* and mass *w* has been added (1<=≀<=*v*<=≀<=1<=000<=000, 1<=≀<=*w*<=≀<=1000)- 2 *x* β€” an event of type 2, the exhibit with number *x* has been removed; it's guaranteed that the removed exhibit had been displayed at that time- 3 β€” an event of type 3, KleofΓ‘Ε‘ visits the museum and asks his question There will be at most 10<=000 events of type 1 and at least one event of type 3.
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. Print the answers to events of type 3 in the order in which they appear in the input.
[ "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-index">4</sub> = 42, *v*<sub class="lower-index">5</sub> = 20, *v*<sub class="lower-index">6</sub> = 40 and their masses are *w*<sub class="lower-index">1</sub> = 4, *w*<sub class="lower-index">2</sub> = 6, *w*<sub class="lower-index">3</sub> = 1, *w*<sub class="lower-index">4</sub> = 5, *w*<sub class="lower-index">5</sub> = 3, *w*<sub class="lower-index">6</sub> = 6. In the second sample, the only question is asked after removing all exhibits, so *s*(*m*) = 0 for any *m*.
[]
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* important objects of the enemy. The bombing impact point is already determined and has coordinates of [*X*0; *Y*0]. The nuclear warhead is marked by the estimated impact radius *R*<=β‰₯<=0. All the buildings that are located closer than *R* to the bombing epicentre will be destroyed. All the buildings that are located further than *R* from the epicentre, can also be deactivated with some degree of probability. Let's assume that *D* is the distance between a building and the epicentre. This building's deactivation probability *P*(*D*,<=*R*) is calculated according to the following formula: If the estimated impact radius of the warhead is equal to zero, then all the buildings located in the impact point will be completely demolished and all the rest of important objects will not be damaged. The commanding officers want the probability of failing the task to be no more than Ξ΅. Nuclear warheads are too expensive a luxury, that's why you have to minimise the estimated impact radius of the warhead.
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<=≀<=Ξ΅<=≀<=999). The third line contains *X*0 and *Y*0 which are the coordinates of the strike impact point. The next *N* lines contain two numbers *X**i* and *Y**i* each which are the coordinates of every strategically important object. All the coordinates are integer, their absolute values do not exceed 1000. Let us remind you that there are a thousand per mils in unity (number one). There can be several objects in one point.
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 edge between any two distinct vertices (in one out of two possible directions).
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, if there is no edge from *v* to *u*, and *a**v*,<=*u*<==<=1 if there is one. As the output graph has to be a tournament, following equalities must be satisfied: - *a**v*,<=*u*<=+<=*a**u*,<=*v*<==<=1 for each *v*,<=*u* (1<=≀<=*v*,<=*u*<=≀<=*n*;Β *v*<=β‰ <=*u*); - *a**v*,<=*v*<==<=0 for each *v* (1<=≀<=*v*<=≀<=*n*).
[ "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 was cooked, not much happens, but if the egg was raw, it will make quite the mess. This continues until one player has broken *R* raw eggs, at which point that player is declared the loser and the other player wins. The order in which players take turns can be described as a string of 'A' and 'B' characters, where the *i*-th character tells which player should choose the *i*-th egg. Traditionally, players take turns going one after the other. That is, they follow the ordering "ABABAB...". This isn't very fair though, because the second player will win more often than the first. We'd like you to find a better ordering for the players to take their turns. Let's define the unfairness of an ordering as the absolute difference between the first player's win probability and the second player's win probability. We're interested in orderings that minimize the unfairness. We only consider an ordering valid if it contains the same number of 'A's as 'B's. You will also be given a string *S* of length 2(*R*<=+<=*C*) containing only 'A', 'B', and '?' characters. An ordering is said to match *S* if it only differs from *S* in positions where *S* contains a '?'. Of the valid orderings that minimize unfairness, how many match *S*?
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 matching ordering is "BBAAABABABBA".
[]
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 edges or their added edges aren't the same. The removed edge and the added edge can be equal. A perfect matching is a subset of edges such that each vertex is an endpoint of exactly one of these edges.
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 between 1 and 2 turns to edge between 1 and 4, - edge between 3 and 4 turns to edge between 1 and 4, <li> edge between 3 and 4 turns to edge between 3 and 4. <ul>
[]
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 to the tree. One adding operation looks as follows: - First we choose some leaf of the tree with number *v*. - Let's mark the number of vertices on the tree at this moment by variable *n*, then two vertexes are added to the tree, their numbers are *n*<=+<=1 and *n*<=+<=2, also you get new edges, one between vertices *v* and *n*<=+<=1 and one between vertices *v* and *n*<=+<=2. Your task is not just to model the process of adding vertices to the tree, but after each adding operation print the diameter of the current tree. Come on, let's solve the New Year problem!
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 operations are correct.
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 always remains in one and the same position. Due to unintelligible gravitation laws no two static particles in the parallel universe can be present in one column or row, and they also can't be present in the diagonally adjacent cells. A dynamic particle appears in a random empty cell, randomly chooses the destination cell (destination cell may coincide with the start cell, see the samples) and moves there along the shortest path through the cells, unoccupied by the static particles. All empty cells have the same probability of being selected as the beginning or end of the path. Having reached the destination cell, the particle disappears. Only one dynamic particle can exist at one moment of time. This particle can move from a cell to a cell if they have an adjacent side, and this transition takes exactly one galactic second. Stewie got interested in what is the average lifespan of one particle in the given universe.
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 particle, and to '.' if it is empty. It is guaranteed that the described universe satisfies the properties described above, that is no two static particles can be in one column or in one row, besides, they can't be positioned in the diagonally adjacent cells.
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 palindrome. In other words, pair (*l*,<=*r*) should meet the condition that after some rearranging of numbers on positions from *l* to *r*, inclusive (it is allowed not to rearrange the numbers at all), for any 1<=≀<=*i*<=≀<=*n* following condition holds: *a*[*i*]<==<=*a*[*n*<=-<=*i*<=+<=1]. Your task is to find the palindrome degree of Misha's array.
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 message to Bob and receive an answer. We will call this copy the initiator. The process of sending a message contains several steps: - The initiator sends the message to the person standing in the parent node and begins waiting for the answer. - When some copy of Alice receives a message from some of her children nodes, she sends the message to the person standing in the parent node and begins waiting for the answer. - When Bob receives a message from some of his child nodes, he immediately sends the answer to the child node where the message came from. - When some copy of Alice (except for initiator) receives an answer she is waiting for, she immediately sends it to the child vertex where the message came from. - When the initiator receives the answer she is waiting for, she doesn't send it to anybody. - There is a special case: a copy of Alice can't wait for two answers at the same time, so if some copy of Alice receives a message from her child node while she already waits for some answer, she rejects the message and sends a message saying this back to the child node where the message came from. Then the copy of Alice in the child vertex processes this answer as if it was from Bob. - The process of sending a message to a parent node or to a child node is instant but a receiver (a parent or a child) gets a message after 1 second. If some copy of Alice receives several messages from child nodes at the same moment while she isn't waiting for an answer, she processes the message from the initiator with the smallest number and rejects all the rest. If some copy of Alice receives messages from children nodes and also receives the answer she is waiting for at the same instant, then Alice first processes the answer, then immediately continue as normal with the incoming messages. You are given the moments of time when some copy of Alice becomes the initiator and sends a message to Bob. For each message, find the moment of time when the answer (either from Bob or some copy of Alice) will be received by the initiator. You can assume that if Alice wants to send a message (i.e. become the initiator) while waiting for some answer, she immediately rejects the message and receives an answer from herself in no time.
The first line of input contains two integers *n* and *m* (1<=≀<=*n*,<=*m*<=≀<=200<=000)Β β€” the number of nodes with Alices and the number of messages. Second line contains *n* integers *p*1,<=*p*2,<=...,<=*p**n* (0<=≀<=*p**i*<=&lt;<=*i*). The integer *p**i* is the number of the parent node of node *i*. The next *m* lines describe the messages. The *i*-th of them contains two integers *x**i* and *t**i* (1<=≀<=*x**i*<=≀<=*n*, 1<=≀<=*t**i*<=≀<=109)Β β€” the number of the vertex of the initiator of the *i*-th message and the time of the initiation (in seconds). The messages are given in order of increasing initiation time (i.e. *t**i*<=+<=1<=β‰₯<=*t**i* holds for 1<=≀<=*i*<=&lt;<=*m*). The pairs (*x**i*,<=*t**i*) are distinct.
Print *m* integersΒ β€” the *i*-th of them is the moment of time when the answer for the *i*-th message will be received by the initiator.
[ "6 3\n0 1 2 3 2 5\n4 6\n6 9\n5 11\n", "3 2\n0 1 1\n2 1\n3 1\n", "8 3\n0 1 1 2 3 3 4 5\n6 1\n8 2\n4 5\n" ]
[ "14 13 11 ", "5 3 ", "7 6 11 " ]
In the first example the first message is initiated at the moment 6, reaches Bob at the moment 10, and the answer reaches the initiator at the moment 14. The second message reaches vertex 2 at the moment 11. At this moment the copy of Alice in this vertex is still waiting for the answer for the first message, so she rejects the second message. The answer reaches the initiator at the moment 13. The third message is not sent at all, because at the moment 11 Alice in vertex 5 is waiting for the answer for the second message. In the second example the first message reaches Bob, the second is rejected by Alice in vertex 1. This is because the message with smaller initiator number has the priority. In the third example the first and the third messages reach Bob, while the second message is rejected by Alice in vertex 3.
[]
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": for two binary strings *s* and *t* with the same length, the Hamming distance between them is the number of positions at which the corresponding symbols are different. For example, the Hamming distance between "00111" and "10101" is 2 (the different symbols are marked with bold). We use the Hamming distance problem as a query in the following way: you are given two strings *a* and *b* and several queries. Each query will be: what is the Hamming distance between two strings *a**p*1*a**p*1<=+<=1...*a**p*1<=+<=*len*<=-<=1 and *b**p*2*b**p*2<=+<=1...*b**p*2<=+<=*len*<=-<=1? Note, that in this problem the strings are zero-based, that is *s*<==<=*s*0*s*1... *s*|*s*|<=-<=1.
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 integers: *p*1, *p*2 and *len* (0<=≀<=*p*1<=≀<=|*a*|<=-<=*len*;Β 0<=≀<=*p*2<=≀<=|*b*|<=-<=*len*), these numbers denote the parameters of the current query.
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 last several digit into first. For example, by rotating number 12345 you can obtain any numbers: 12345,<=51234,<=45123,<=34512,<=23451. It's worth mentioning that leading-zeroes are allowed. So both 4500123 and 0123450 can be obtained by rotating 0012345. You can see why 142857 satisfies the condition. All of the 6 equations are under base 10. - 142857Β·1<==<=142857; - 142857Β·2<==<=285714; - 142857Β·3<==<=428571; - 142857Β·4<==<=571428; - 142857Β·5<==<=714285; - 142857Β·6<==<=857142. Now, Bike has a problem. He extends "Rotatable Number" under any base *b*. As is mentioned above, 142857 is a "Rotatable Number" under base 10. Another example is 0011 under base 2. All of the 4 equations are under base 2. - 0011Β·1<==<=0011; - 0011Β·10<==<=0110; - 0011Β·11<==<=1001; - 0011Β·100<==<=1100. So, he wants to find the largest *b* (1<=&lt;<=*b*<=&lt;<=*x*) so that there is a positive "Rotatable Number" (leading-zeroes allowed) of length *n* under base *b*. Note that any time you multiply a rotatable number by numbers from 1 to its length you should get a rotating of that number.
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 only one road repairing company in town, named "RC company". Company's center is located at the intersection 1. RC company doesn't repair roads you tell them. Instead, they have workers at some intersections, who can repair only some specific paths. The *i*-th worker can be paid *c**i* coins and then he repairs all roads on a path from *u**i* to some *v**i* that lies on the path from *u**i* to intersection 1. Mayor asks you to choose the cheapest way to hire some subset of workers in order to repair all the roads in Yusland. It's allowed that some roads will be repaired more than once. If it's impossible to repair all roads print <=-<=1.
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 *i*-th road. Last *m* lines provide the description of workers, each line containing three integers *u**i*, *v**i* and *c**i* (1<=≀<=*u**i*,<=*v**i*<=≀<=*n*, 1<=≀<=*c**i*<=≀<=109). This means that the *i*-th worker can repair all roads on the path from *v**i* to *u**i* for *c**i* coins. It's guaranteed that *v**i* lies on the path from *u**i* to 1. Note that *v**i* and *u**i* may coincide.
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*-th scientist brought *k**i* calculating problems for the device developed by the Smart Beaver from ABBYY. The problems of the *i*-th scientist are numbered from 1 to *k**i*, and they must be calculated sequentially in the described order, since calculating each problem heavily depends on the results of calculating of the previous ones. Each problem of each of the *n* scientists is described by one integer *a**i*,<=*j*, where *i* (1<=≀<=*i*<=≀<=*n*) is the number of the scientist, *j* (1<=≀<=*j*<=≀<=*k**i*) is the number of the problem, and *a**i*,<=*j* is the number of resource units the calculating device needs to solve this problem. The calculating device that is developed by the Smart Beaver is pretty unusual. It solves problems sequentially, one after another. After some problem is solved and before the next one is considered, the calculating device allocates or frees resources. The most expensive operation for the calculating device is freeing resources, which works much slower than allocating them. It is therefore desirable that each next problem for the calculating device requires no less resources than the previous one. You are given the information about the problems the scientists offered for the testing. You need to arrange these problems in such an order that the number of adjacent "bad" pairs of problems in this list is minimum possible. We will call two consecutive problems in this list a "bad pair" if the problem that is performed first requires more resources than the one that goes after it. Do not forget that the problems of the same scientist must be solved in a fixed order.
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<=&lt;<=*m**i*<=≀<=109, 1<=≀<=*x**i*,<=*y**i*<=≀<=109) β€” the number of problems of the *i*-th scientist, the resources the first problem requires and three parameters that generate the subsequent values of *a**i*,<=*j*. For all *j* from 2 to *k**i*, inclusive, you should calculate value *a**i*,<=*j* by formula *a**i*,<=*j*<==<=(*a**i*,<=*j*<=-<=1<=*<=*x**i*<=+<=*y**i*) *mod* *m**i*, where *a* *mod* *b* is the operation of taking the remainder of division of number *a* by number *b*. To get the full points for the first group of tests it is sufficient to solve the problem with *n*<==<=2, 1<=≀<=*k**i*<=≀<=2000. To get the full points for the second group of tests it is sufficient to solve the problem with *n*<==<=2, 1<=≀<=*k**i*<=≀<=200000. To get the full points for the third group of tests it is sufficient to solve the problem with 1<=≀<=*n*<=≀<=5000, 1<=≀<=*k**i*<=≀<=5000.
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 problem and the number of the scientist who offered this problem, respectively. The scientists are numbered from 1 to *n* in the order of input.
[ "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 them has two calculating problems. The problems of the first scientist require 1 and 2 resource units, the problems of the second one require 3 and 4 resource units. Let's list all possible variants of the calculating order (each problem is characterized only by the number of resource units it requires): (1, 2, 3, 4), (1, 3, 2, 4), (3, 1, 2, 4), (1, 3, 4, 2), (3, 4, 1, 2), (3, 1, 4, 2). Sequence of problems (1, 3, 2, 4) has one "bad" pair (3 and 2), (3, 1, 4, 2) has two "bad" pairs (3 and 1, 4 and 2), and (1, 2, 3, 4) has no "bad" pairs.
[]
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 needs repairing, then it is forbidden to use it. However, the Berland government can repair the road so that it can be used. Right now Berland is being threatened by the war with the neighbouring state. So the capital officials decided to send a military squad to each city. The squads can move only along the existing roads, as there's no time or money to build new roads. However, some roads will probably have to be repaired in order to get to some cities. Of course the country needs much resources to defeat the enemy, so you want to be careful with what you're going to throw the forces on. That's why the Berland government wants to repair the minimum number of roads that is enough for the military troops to get to any city from the capital, driving along good or repaired roads. Your task is to help the Berland government and to find out, which roads need to be repaired.
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 road from city *a**i* to city *b**i*. If *c**i* equals 0, than the given road is in a good condition. If *c**i* equals 1, then it needs to be repaired. It is guaranteed that there is not more than one road between the cities in each direction.
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 from 1 in the order, in which they are given in the input. If there are multiple sets, consisting of the minimum number of roads to repair to make travelling to any city from the capital possible, print any of them. If it is possible to reach any city, driving along the roads that already are in a good condition, print 0 in the only output line.
[ "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 times...) Moreover, if a person only has one friend in common with Heidi (i.e., if that person is in a leaf of the tree), then that person will not send Heidi back (so that Heidi's travel will end at some point). Heidi also found unrealistic the assumption that she can make all the travels in one day. Therefore now she assumes that every time she travels a route between two friends, she needs to buy a new ticket. She wants to know: how much should she expect to spend on the trips? For what it's worth, Heidi knows that Jenny has at least two friends.
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!). It is again guaranteed that the social network of the input forms a tree.
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 / 2 + 20Β·1 / 2 = 15. In the third example, the expected cost is 81 / 5. You should output 400000019. In her travels, Heidi has learned an intriguing fact about the structure of her social network. She tells you the following: The mysterious determinant that you might be wondering about is such that it does not cause strange errors in your reasonable solution... Did we mention that Heidi is a weird cow?
[]
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. Yury Dmitrievich doesn't like Noora, so he always gives the girl the most difficult tasks. So it happened this time. The teacher gives Noora a tree with *n* vertices. Vertices are numbered with integers from 1 to *n*. The length of all the edges of this tree is 1. Noora chooses a set of simple paths that pairwise don't intersect in edges. However each vertex should belong to at least one of the selected path. For each of the selected paths, the following is done: 1. We choose exactly one edge (*u*,<=*v*) that belongs to the path. 1. On the selected edge (*u*,<=*v*) there is a point at some selected distance *x* from the vertex *u* and at distance 1<=-<=*x* from vertex *v*. But the distance *x* chosen by Noora arbitrarily, i. e. it can be different for different edges. 1. One of the vertices *u* or *v* is selected. The point will start moving to the selected vertex. Let us explain how the point moves by example. Suppose that the path consists of two edges (*v*1,<=*v*2) and (*v*2,<=*v*3), the point initially stands on the edge (*v*1,<=*v*2) and begins its movement to the vertex *v*1. Then the point will reach *v*1, then "turn around", because the end of the path was reached, further it will move in another direction to vertex *v*2, then to vertex *v*3, then "turn around" again, then move to *v*2 and so on. The speed of the points is 1 edge per second. For example, for 0.5 second the point moves to the length of the half of an edge. A stopwatch is placed at each vertex of the tree. The time that the stopwatches indicate at start time is 0 seconds. Then at the starting moment of time, all points simultaneously start moving from the selected positions to selected directions along the selected paths, and stopwatches are simultaneously started. When one of the points reaches the vertex *v*, the stopwatch at the vertex *v* is automatically reset, i.e. it starts counting the time from zero. Denote by *res**v* the maximal time that the stopwatch at the vertex *v* will show if the point movement continues infinitely. Noora is asked to select paths and points on them so that *res*1 is as minimal as possible. If there are several solutions to do this, it is necessary to minimize *res*2, then *res*3, *res*4,<=...,<=*res**n*. Help Noora complete the teacher's task. For the better understanding of the statement, see the explanation for the example.
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 given in input. 1. Two integers *u* and *v* β€” means that you put point on the edge between vertices *u* and *v* (obviously the edge should belong to the path) and a point will start moving to the vertex *v*. Note that order of printing of the edge's ends is important. For example if you print "1 2" (without quotes), then point will start moving to vertex 2; but if you print "2 1" (without quotes), then point will start moving to vertex 1. 1. Single real number *x* (0<=≀<=*x*<=≀<=1) β€” distance between point and vertex *u* (the same vertex that you print first in the third paragraph).
[ "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 represent chosen points, and brown numbers inside vertices β€” current time at stopwatch. Purple arrows represent direction in which points will move. In 0.(3) seconds points will be located in following way (before stopwatch reset): <img class="tex-graphics" src="https://espresso.codeforces.com/987880919ff352d5460fb4f49b977a967a7b4681.png" style="max-width: 100.0%;max-height: 100.0%;"/> After stopwatch reset: <img class="tex-graphics" src="https://espresso.codeforces.com/dea53d29e0d6d59a1735b83aff4a03415e7626b5.png" style="max-width: 100.0%;max-height: 100.0%;"/> In 1.0 second after the start of moving: <img class="tex-graphics" src="https://espresso.codeforces.com/c7bfb06aff382bb7931e6c9bf645646e28d19b7b.png" style="max-width: 100.0%;max-height: 100.0%;"/> In 1.(3) seconds after the start of moving (after stopwatch reset): <img class="tex-graphics" src="https://espresso.codeforces.com/aa4614b2596118d470533c00595e0284ce21b803.png" style="max-width: 100.0%;max-height: 100.0%;"/> Finally, in 2 seconds after the start of moving points return to their initial positions. <img class="tex-graphics" src="https://espresso.codeforces.com/85f53958e316d96ff71cee6907bc2ea164c02fa3.png" style="max-width: 100.0%;max-height: 100.0%;"/> This process will continue infinitely.
[]
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 following conditions: - each domino occupies exactly two non-forbidden cells of the table; - no two dominoes occupy the same table cell; - exactly one non-forbidden cell of the table is unoccupied by any domino (it is marked by a circle in the picture). To solve the puzzle, you need multiple steps to transport an empty cell from the starting position to some specified position. A move is transporting a domino to the empty cell, provided that the puzzle stays correct. The horizontal dominoes can be moved only horizontally, and vertical dominoes can be moved only vertically. You can't rotate dominoes. The picture shows a probable move. Xenia has a 3<=Γ—<=*n* table with forbidden cells and a cell marked with a circle. Also, Xenia has very many identical dominoes. Now Xenia is wondering, how many distinct correct puzzles she can make if she puts dominoes on the existing table. Also, Xenia wants the circle-marked cell to be empty in the resulting puzzle. The puzzle must contain at least one move. Help Xenia, count the described number of puzzles. As the described number can be rather large, print the remainder after dividing it by 1000000007 (109<=+<=7).
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 "O", if the corresponding cell is marked with a circle. It is guaranteed that exactly one cell in the table is marked with a circle. It is guaranteed that all cells of a given table having at least one common point with the marked cell is non-forbidden.
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 points. But unfortunately there is an island in the sea, so sometimes you won't be able to move your ship in the line segment between the 2 points. You can only move to safe points. A point is called safe if it's on the line segment between the start and end points, or if it's on the island's edge. But you are too lucky, you have got some clever and strong workers and they can help you in your trip, they can help you move the ship in the sea and they will take 1 Egyptian pound for each moving unit in the sea, and they can carry the ship (yes, they are very strong) and walk on the island and they will take 2 Egyptian pounds for each moving unit in the island. The money which you will give to them will be divided between all workers, so the number of workers does not matter here. You can move your ship on the island edge, and it will be considered moving in the sea. Now you have a sea map, and you have to decide what is the minimum cost for your trip. Your starting point is (*xStart*, *yStart*), and the end point is (*xEnd*, *yEnd*), both points will be different. The island will be a convex polygon and there will be no more than 2 polygon points on the same line, also the starting and the end points won't be inside or on the boundary of the island. The points for the polygon will be given in the anti-clockwise order.
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 coordinates of the points (<=-<=100<=≀<=*x*,<=*y*<=≀<=100), the polygon points will be distinct.
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 -47 48 72 -81 92", "output": "53.758720223" }, { "input": "-60 -52 68 -91\n5\n7 -95 70 -47 62 18 14 49 -33 -3", "output": "154.850322435" }, { "input": "-14 74 90 -37\n5\n-30 -72 41 -2 75 36 79 100 -83 25", "output": "227.828503094" }, { "input": "56 35 4 23\n4\n-55 -95 38 -67 -24 -9 -53 -57", "output": "53.366656257" }, { "input": "-12 84 -89 83\n3\n-23 -24 50 -15 -8 21", "output": "77.006493233" }, { "input": "84 -49 75 -39\n3\n-87 -67 -12 -58 97 79", "output": "13.453624047" }, { "input": "-96 -1 -83 -3\n3\n-60 -43 100 -78 43 93", "output": "13.152946438" }, { "input": "-52 53 -28 -97\n3\n69 -89 -6 -42 -54 -45", "output": "159.809688284" }, { "input": "0 3 10 3\n3\n1 0 9 0 5 3", "output": "10.000000000" }, { "input": "0 0 90 0\n4\n10 0 20 -90 30 0 20 90", "output": "110.000000000" }, { "input": "0 0 90 0\n3\n10 0 20 -90 20 90", "output": "100.000000000" }, { "input": "90 0 0 0\n3\n10 0 20 -90 20 90", "output": "100.000000000" }, { "input": "10 0 0 0\n3\n1 0 9 0 5 3", "output": "10.000000000" }, { "input": "10 3 0 3\n3\n1 0 9 0 5 3", "output": "10.000000000" }, { "input": "90 0 0 0\n4\n10 0 20 -90 30 0 20 90", "output": "110.000000000" }, { "input": "-90 50 90 50\n3\n0 0 1 100 -1 100", "output": "181.000000000" }, { "input": "90 50 -90 50\n3\n0 0 1 100 -1 100", "output": "181.000000000" }, { "input": "-90 20 90 20\n3\n0 0 1 100 -1 100", "output": "180.400000000" }, { "input": "90 20 -90 20\n3\n0 0 1 100 -1 100", "output": "180.400000000" }, { "input": "-90 85 90 85\n3\n0 0 1 100 -1 100", "output": "181.700000000" }, { "input": "90 85 -90 85\n3\n0 0 1 100 -1 100", "output": "181.700000000" }, { "input": "-90 0 90 0\n3\n0 0 1 100 -1 100", "output": "180.000000000" }, { "input": "90 0 -90 0\n3\n0 0 1 100 -1 100", "output": "180.000000000" }, { "input": "-90 100 90 100\n3\n0 0 1 100 -1 100", "output": "180.000000000" }, { "input": "90 100 -90 100\n3\n0 0 1 100 -1 100", "output": "180.000000000" }, { "input": "-100 0 100 0\n4\n-99 -100 99 -100 99 100 -99 100", "output": "398.000000000" }, { "input": "100 0 -100 0\n4\n-99 -100 99 -100 99 100 -99 100", "output": "398.000000000" }, { "input": "0 0 100 100\n4\n1 1 5 1 5 5 1 5", "output": "143.764501988" }, { "input": "100 100 0 0\n4\n1 1 5 1 5 5 1 5", "output": "143.764501988" }, { "input": "-100 -100 100 100\n4\n1 1 5 1 5 5 1 5", "output": "285.185858225" }, { "input": "100 100 -100 -100\n4\n1 1 5 1 5 5 1 5", "output": "285.185858225" }, { "input": "-81 -27 75 -82\n4\n-55 -46 69 -90 29 89 -47 1", "output": "190.443063616" }, { "input": "29 -100 -73 -94\n6\n-93 -71 77 -52 99 23 72 52 7 87 -35 86", "output": "102.176318196" }, { "input": "82 33 56 42\n4\n-49 -23 32 24 48 72 -82 92", "output": "27.513632984" }, { "input": "-60 -83 76 64\n6\n-87 -45 7 -96 70 -48 86 10 53 94 -93 88", "output": "280.878028960" }, { "input": "-96 -3 21 -99\n5\n-98 -93 21 -60 75 36 79 100 -84 25", "output": "226.904588381" }, { "input": "-55 -94 -91 61\n6\n-98 -81 -74 -83 68 -94 100 -60 74 95 -70 88", "output": "195.315879140" }, { "input": "88 -87 -97 38\n6\n-94 -38 -76 -97 37 -90 66 -66 65 90 -67 57", "output": "328.143362779" }, { "input": "-54 30 89 -90\n4\n-34 -15 86 35 -58 87 -62 68", "output": "205.893134381" }, { "input": "71 62 -57 -87\n8\n-40 -73 -33 -73 43 -10 12 70 -22 92 -28 93 -80 51 -85 30", "output": "221.662477051" }, { "input": "-1 -97 -73 -60\n8\n-96 -2 -87 -32 -67 -62 2 -96 22 54 -46 95 -66 84 -87 62", "output": "80.950602221" }, { "input": "57 -90 -91 75\n7\n-99 -65 -33 -94 73 -64 89 12 -54 82 -67 66 -100 15", "output": "327.077960334" }, { "input": "-93 -52 70 -78\n3\n-2 -96 -1 -34 -71 22", "output": "184.795870489" }, { "input": "91 -84 -72 -87\n8\n-70 -37 -61 -71 -33 -95 30 -66 73 72 43 90 28 94 -63 93", "output": "168.169409318" }, { "input": "-46 16 78 -31\n4\n-66 -80 -65 -88 49 -83 1 10", "output": "145.181758543" }, { "input": "3 -91 26 -47\n3\n-60 -73 93 -57 7 -48", "output": "66.751150896" }, { "input": "99 61 97 99\n3\n-45 -86 -1 32 -98 1", "output": "38.052595181" }, { "input": "76 -89 -50 -72\n8\n-67 -56 -56 -70 -9 -81 39 -62 85 -19 96 27 6 63 -68 64", "output": "128.204246559" }, { "input": "90 -83 -59 76\n7\n-96 -46 -92 -73 -29 -93 96 42 68 94 13 95 -70 57", "output": "341.689549922" }, { "input": "83 -46 -94 34\n4\n-96 8 38 -78 60 61 -14 83", "output": "295.663552722" }, { "input": "57 -100 -96 -79\n8\n-98 -66 -53 -83 69 -90 97 -47 97 59 84 87 -9 94 -81 91", "output": "154.434452115" }, { "input": "74 -91 84 90\n9\n-93 -71 18 -95 77 -52 99 23 72 52 7 87 -35 86 -56 85 -87 82", "output": "191.685835036" }, { "input": "30 95 21 -96\n11\n-94 -43 -80 -76 55 -98 81 -97 82 -94 97 -8 81 25 39 73 16 82 -37 90 -91 80", "output": "283.479788431" }, { "input": "-99 73 84 -68\n8\n-94 -76 -19 -96 5 -97 25 -96 98 -9 26 97 -68 78 -84 53", "output": "353.066163584" }, { "input": "10 96 86 -36\n10\n-98 -14 -82 -78 7 -84 91 -76 77 -3 71 28 40 56 20 65 -71 96 -91 39", "output": "159.480894135" }, { "input": "57 32 -39 -98\n7\n-82 -50 -66 -95 57 -62 91 -37 85 -4 -25 96 -82 11", "output": "233.232680735" }, { "input": "-89 99 -54 -97\n8\n-93 -47 -91 -84 -49 -98 51 -93 98 -84 95 58 50 95 -71 97", "output": "224.569042413" }, { "input": "-60 57 -97 -63\n5\n-89 -89 65 -75 81 16 58 79 -52 -1", "output": "125.574678976" }, { "input": "49 -90 -43 96\n9\n-79 -91 -34 -94 89 -73 99 -4 100 65 14 88 -82 98 -89 31 -91 9", "output": "342.151735319" }, { "input": "-81 -74 91 76\n11\n-98 -66 -53 -83 57 -100 69 -90 97 -47 100 2 97 59 84 87 57 93 -9 94 -81 91", "output": "333.901907972" }, { "input": "-80 -69 -74 95\n7\n-49 -23 -38 -45 12 -90 63 -73 83 -24 48 72 -82 92", "output": "174.223898716" }, { "input": "23 93 99 80\n11\n-95 -75 -87 -91 7 -96 45 -93 93 -88 97 -70 82 84 63 92 53 94 -93 88 -100 26", "output": "79.106932193" }, { "input": "-81 100 95 -83\n8\n-98 -93 -47 -88 57 -77 78 -52 99 24 79 100 -6 100 -97 84", "output": "365.652074449" }, { "input": "-100 -9 -93 8\n11\n-98 -35 -93 -78 -92 -79 -56 -96 1 -100 83 -85 99 -82 97 60 71 99 -43 99 -70 96", "output": "18.384776311" }, { "input": "26 -98 -36 97\n9\n-98 -62 -97 -96 20 -87 76 -73 87 -64 96 23 92 84 -37 97 -91 87", "output": "359.016873597" }, { "input": "-95 -40 -50 99\n12\n-88 -85 -85 -100 -7 -100 85 -96 91 -88 93 -82 97 6 97 79 54 87 -12 98 -95 98 -98 -1", "output": "184.211579590" }, { "input": "93 100 -76 -79\n14\n-93 -38 -92 -66 -65 -84 -11 -99 -5 -99 70 -97 99 -96 100 -79 98 14 95 74 77 97 -12 99 -62 86 -92 49", "output": "350.462979994" }, { "input": "-99 21 79 90\n10\n-99 -65 -77 -78 -47 -93 17 -95 28 -93 89 -65 98 58 65 96 -28 98 -98 96", "output": "256.793571462" }, { "input": "-17 96 -77 23\n8\n-64 -58 -56 -97 97 -86 99 -66 93 68 9 97 -53 50 -72 32", "output": "95.366388515" }, { "input": "9 94 77 -73\n7\n-100 -67 -78 -71 64 -83 89 -30 54 39 -26 97 -75 87", "output": "198.561805973" }, { "input": "-83 -20 32 85\n4\n-97 -63 -15 -44 -15 31 -54 21", "output": "160.338221322" }, { "input": "0 0 10 0\n3\n1 0 9 0 5 3", "output": "10.000000000" }, { "input": "-2 -2 2 2\n4\n-1 -1 1 -1 1 1 -1 1", "output": "6.828427125" }, { "input": "-2 2 2 2\n4\n-1 -1 1 -1 1 1 -1 1", "output": "4.000000000" }, { "input": "-2 2 2 -2\n4\n-1 -1 1 -1 1 1 -1 1", "output": "6.828427125" }, { "input": "15 15 0 0\n3\n-24 25 -66 66 -14 5", "output": "21.213203436" }, { "input": "100 100 100 99\n3\n-100 -100 99 -100 99 100", "output": "1.000000000" }, { "input": "100 100 -100 -100\n3\n0 0 1 0 1 1", "output": "282.842712475" }, { "input": "100 100 -100 -100\n3\n0 0 1 -1 1 0", "output": "282.842712475" }, { "input": "-100 100 100 -100\n3\n100 100 99 100 100 99", "output": "282.842712475" }, { "input": "100 87 33 94\n13\n-97 -70 -87 -93 -85 -95 89 -98 99 -72 98 80 81 85 9 98 -26 96 -47 94 -59 91 -78 78 -90 49", "output": "67.364679172" }, { "input": "-98 -88 -68 -92\n11\n-93 -57 -70 -87 -62 -92 -31 -99 98 -95 100 79 98 100 -46 95 -63 87 -77 79 -90 68", "output": "30.265491901" }, { "input": "70 91 85 94\n13\n-97 -70 -87 -93 -85 -95 89 -98 99 -72 98 80 81 85 9 98 -26 96 -47 94 -59 91 -78 78 -90 49", "output": "15.297058541" }, { "input": "94 -63 93 -24\n10\n-94 -89 -91 -95 -4 -100 81 -98 93 -80 92 17 82 90 -43 100 -86 93 -97 -10", "output": "39.012818406" }, { "input": "100 100 -100 -100\n3\n0 0 1 1 0 1", "output": "282.842712475" }, { "input": "100 100 -100 -100\n3\n0 0 1 2 0 1", "output": "282.842712475" }, { "input": "100 100 -100 -100\n3\n0 0 1 0 2 1", "output": "282.842712475" }, { "input": "0 0 3 3\n4\n1 1 4 1 4 2 0 2", "output": "5.656854249" }, { "input": "2 2 -2 -2\n4\n-1 -1 1 -1 1 1 -1 1", "output": "6.828427125" } ]
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, there were *n* contestants in the tournament, numbered from 1 to *n*. Each of them had already obtained some amount of tournament points, namely the *i*-th fighter had *p**i* points. Manao is going to engage in a single fight against each contestant. Each of Manao's fights ends in either a win or a loss. A win grants Manao one point, and a loss grants Manao's opponent one point. For each *i*, Manao estimated the amount of effort *e**i* he needs to invest to win against the *i*-th contestant. Losing a fight costs no effort. After Manao finishes all of his fights, the ranklist will be determined, with 1 being the best rank and *n*<=+<=1 being the worst. The contestants will be ranked in descending order of their tournament points. The contestants with the same number of points as Manao will be ranked better than him if they won the match against him and worse otherwise. The exact mechanism of breaking ties for other fighters is not relevant here. Manao's objective is to have rank *k* or better. Determine the minimum total amount of effort he needs to invest in order to fulfill this goal, if it is possible.
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 input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. - In subproblem C1 (4 points), the constraint 1<=≀<=*n*<=≀<=15 will hold. - In subproblem C2 (4 points), the constraint 1<=≀<=*n*<=≀<=100 will hold. - In subproblem C3 (8 points), the constraint 1<=≀<=*n*<=≀<=200000 will hold.
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 has 2 points and victory against him costs Manao 2 units of effort. Manao's goal is top be in top 2. The optimal decision is to win against fighters 1 and 3, after which Manao, fighter 2, and fighter 3 will all have 2 points. Manao will rank better than fighter 3 and worse than fighter 2, thus finishing in second place. Consider the second test case. Even if Manao wins against both opponents, he will still rank third.
[]
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 are wavy and numbers 123, 1000 and 2212 are not. The task is to find the *k*-th smallest wavy number *r* that is divisible by *n* for the given integer values *n* and *k*. You are to write a program that will find the value of *r* if it doesn't exceed 1014.
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 unit squares. The objects don't intersect and don't touch each other. The principle according to which the weapon works is highly secret. You only know that one can use it to strike any rectangular area whose area is not equal to zero with sides, parallel to the sides of the polygon. The area must completely cover some of the unit squares into which the polygon is divided and it must not touch the other squares. Of course the area mustn't cross the polygon border. Your task is as follows: you should hit no less than one and no more than three rectangular objects. Each object must either lay completely inside the area (in that case it is considered to be hit), or lay completely outside the area. Find the number of ways of hitting.
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 for an empty space. The symbols "*" form exactly *k* rectangular connected areas that meet the requirements of the task.
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 roads that come from the city. The valiant crew fulfilled the King's order in a day, then workers went home. Unfortunately, not everything is as great as Valera II would like. The main part of the group were gastarbeiters β€” illegal immigrants who are enthusiastic but not exactly good at understanding orders in Berlandian. Therefore, having received orders to asphalt the roads coming from some of the city, the group asphalted all non-asphalted roads coming from the city, and vice versa, took the asphalt from the roads that had it. Upon learning of this progress, Valera II was very upset, but since it was too late to change anything, he asked you to make a program that determines whether you can in some way asphalt Berlandian roads in at most *n* days. Help the king.
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<=≀<=*c**i*<=≀<=1). The first two integers (*a**i*,<=*b**i*) are indexes of the cities that are connected by the *i*-th road, the third integer (*c**i*) equals 1, if the road was initially asphalted, and 0 otherwise. Consider the cities in Berland indexed from 1 to *n*, and the roads indexed from 1 to *m*. It is guaranteed that between two Berlandian cities there is not more than one road.
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 multiple solutions, print any of them. If there's no way to asphalt all roads, print "Impossible" (without the quotes).
[ "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 " }, { "input": "6 13\n4 6 0\n4 2 1\n6 2 0\n5 4 1\n1 3 1\n5 6 1\n1 5 1\n1 4 1\n2 3 1\n1 2 1\n3 6 0\n6 1 1\n2 5 1", "output": "Impossible" }, { "input": "7 13\n5 7 1\n1 2 0\n5 3 0\n6 1 1\n6 3 0\n2 7 0\n7 1 1\n6 4 1\n6 2 1\n4 7 1\n2 5 0\n1 5 0\n7 3 0", "output": "Impossible" }, { "input": "16 20\n8 5 1\n12 11 1\n13 2 1\n3 7 0\n6 8 0\n16 6 0\n7 14 0\n13 12 1\n8 15 1\n14 5 0\n12 10 0\n11 16 1\n11 9 0\n2 15 1\n12 1 0\n9 5 0\n14 15 0\n3 14 1\n2 8 1\n1 14 1", "output": "6\n1 3 6 9 10 14 " }, { "input": "15 33\n12 4 1\n8 10 1\n6 13 0\n2 6 1\n10 7 0\n1 14 1\n3 9 0\n3 1 0\n7 12 1\n13 5 0\n10 4 0\n1 7 1\n11 15 0\n2 3 0\n9 8 1\n6 9 1\n9 4 1\n4 8 1\n7 8 0\n2 13 1\n15 8 0\n12 15 0\n14 4 0\n1 11 1\n4 2 0\n13 11 0\n13 3 0\n14 7 1\n12 11 1\n13 8 0\n2 8 1\n9 5 1\n12 2 1", "output": "Impossible" }, { "input": "14 10\n12 11 1\n7 6 1\n7 9 1\n6 9 1\n5 4 0\n2 7 1\n1 8 1\n1 4 0\n13 5 0\n8 4 0", "output": "3\n1 5 8 " }, { "input": "18 32\n10 11 1\n16 7 1\n18 10 0\n15 2 1\n14 2 0\n4 9 1\n10 16 1\n13 17 0\n8 3 1\n14 8 1\n17 11 0\n16 9 1\n12 1 1\n5 11 0\n16 13 0\n18 4 0\n5 7 0\n4 13 1\n17 1 0\n8 12 1\n10 1 0\n8 18 1\n5 9 0\n2 3 0\n3 15 1\n11 4 1\n14 11 1\n7 6 0\n1 2 0\n6 10 0\n3 1 1\n13 2 0", "output": "Impossible" }, { "input": "19 40\n10 13 0\n3 9 0\n5 12 0\n8 16 0\n4 10 1\n2 5 0\n17 6 1\n5 13 0\n5 11 0\n12 11 0\n17 16 0\n2 9 1\n6 9 0\n7 9 0\n10 14 1\n14 2 1\n12 6 1\n6 1 0\n11 17 0\n13 19 0\n16 14 1\n7 11 1\n18 6 0\n18 12 1\n2 15 1\n8 12 0\n9 1 1\n2 19 1\n17 2 0\n5 7 0\n6 2 1\n13 7 0\n9 14 0\n13 8 0\n11 1 1\n9 15 0\n18 4 0\n18 7 0\n1 14 0\n3 12 1", "output": "Impossible" }, { "input": "20 40\n5 16 0\n6 10 0\n16 12 0\n13 15 1\n18 13 1\n15 18 1\n4 13 0\n12 10 1\n13 9 1\n19 5 0\n18 10 0\n8 18 0\n3 15 0\n12 13 0\n3 20 1\n11 15 1\n15 1 1\n3 2 1\n12 9 0\n10 16 0\n5 7 1\n7 6 0\n5 20 1\n20 18 0\n20 10 1\n16 15 1\n7 1 0\n3 8 1\n3 10 1\n9 7 0\n9 20 0\n17 1 1\n2 1 0\n2 19 0\n8 16 0\n11 7 0\n6 20 0\n16 2 0\n10 15 0\n10 19 0", "output": "10\n1 6 9 11 13 15 16 17 18 19 " }, { "input": "11 18\n10 2 0\n9 10 0\n3 10 1\n9 4 1\n6 10 0\n5 7 1\n3 1 0\n7 2 1\n8 2 1\n10 7 0\n6 8 1\n5 2 1\n10 11 0\n4 3 0\n7 8 1\n4 5 1\n7 11 1\n9 7 1", "output": "2\n3 10 " }, { "input": "100 49\n9 51 0\n59 35 0\n42 4 1\n18 70 1\n95 44 0\n8 57 0\n38 1 1\n33 56 1\n44 30 1\n62 98 0\n99 7 0\n9 71 1\n63 76 0\n45 22 1\n24 43 0\n97 3 1\n23 71 1\n98 8 1\n67 69 1\n39 15 1\n71 74 1\n9 85 1\n85 91 1\n93 40 0\n76 70 0\n22 5 1\n36 29 1\n10 80 1\n38 12 0\n68 86 0\n53 25 0\n2 39 1\n50 43 1\n60 76 0\n96 36 0\n95 34 0\n31 4 1\n82 76 1\n50 63 0\n4 72 0\n32 63 0\n26 7 0\n76 96 1\n10 97 1\n91 58 0\n4 10 0\n83 12 1\n97 80 1\n48 12 0", "output": "25\n1 4 7 18 24 25 29 30 31 34 35 36 38 40 42 44 48 51 57 58 60 62 63 68 70 " }, { "input": "100 41\n3 89 1\n31 33 0\n88 83 0\n45 97 0\n32 86 0\n19 12 1\n75 74 0\n74 10 0\n69 73 1\n69 80 0\n97 61 1\n73 68 0\n70 51 1\n56 92 1\n95 42 0\n69 51 1\n2 51 0\n72 38 1\n86 10 1\n35 50 1\n81 58 0\n34 8 0\n93 82 1\n81 84 1\n68 61 0\n69 20 0\n77 25 1\n81 13 0\n21 20 1\n46 5 1\n71 60 1\n46 65 1\n29 54 1\n11 55 1\n16 81 0\n77 30 1\n68 45 1\n74 84 0\n89 1 1\n70 83 1\n18 4 0", "output": "16\n2 4 8 13 16 20 21 31 32 42 45 58 68 74 80 88 " }, { "input": "4 2\n1 2 0\n3 4 0", "output": "2\n1 3 " } ]
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 two rows, each row contains *n* cells. Each cell contains exactly one small English letter. You also are given a word *w*, which consists of *k* small English letters. A solution of the puzzle is a sequence of field cells *c*1, ..., *c**k*, such that: - For all *i* from 1 to *k* the letter written in the cell *c**i* matches the letter *w**i*;- All the cells in the sequence are pairwise distinct;- For all *i* from 1 to *k*<=-<=1 cells *c**i* and *c**i*<=+<=1 have a common side. Oleg Petrov quickly found a solution for the puzzle. Now he wonders, how many distinct solutions are there for this puzzle. Oleg Petrov doesn't like too large numbers, so calculate the answer modulo 109<=+<=7. Two solutions *c**i* and *c*'*i* are considered distinct if the sequences of cells do not match in at least one position, that is there is such *j* in range from 1 to *k*, such that *c**j*<=β‰ <=*c*'*j*.
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 *k* of these cities. At the beginning of the game all portals are closed. When a player visits a portal city, the portal opens. Strange as it is, one can teleport from an open portal to an open one. The teleportation takes no time and that enables the player to travel quickly between rather remote regions of the country. At the beginning of the game Pavel is in city number 1. He wants to open all portals as quickly as possible. How much time will he need for that?
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*<=β‰ <=*y**i*, 1<=≀<=*w**i*<=≀<=109) β€” the numbers of the cities connected by the *i*-th road and the time needed to go from one city to the other one by this road. Any two cities are connected by no more than one road. It is guaranteed that we can get from any city to any other one, moving along the roads of the country. The next line contains integer *k* (1<=≀<=*k*<=≀<=*n*) β€” the number of portals. The next line contains *k* space-separated integers *p*1, *p*2, ..., *p**k* β€” numbers of the cities with installed portals. Each city has no more than one portal.
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*<==<=*x* or *b*<==<=*y*. Unfortunately (of fortunately?) for Oleg the answer in this problem was always *n*, so the task bored Oleg soon. He decided to make it more difficult by removing some cells from the board. If a cell is deleted, Oleg can't put a rook there, but rooks do beat each other "through" deleted cells. Oleg deletes the cells in groups, namely, he repeatedly choose a rectangle with sides parallel to the board sides and deletes all the cells inside the rectangle. Formally, if he chooses a rectangle, lower left cell of which has coordinates (*x*1,<=*y*1), and upper right cell of which has coordinates (*x*2,<=*y*2), then he deletes all such cells with coordinates (*x*,<=*y*) that *x*1<=≀<=*x*<=≀<=*x*2 and *y*1<=≀<=*y*<=≀<=*y*2. It is guaranteed that no cell is deleted twice, i.e. the chosen rectangles do not intersect. This version of the problem Oleg can't solve, and his friend Igor is busy at a conference, so he can't help Oleg. You are the last hope for Oleg! Help him: given the size of the board and the deleted rectangles find the maximum possible number of rooks that could be placed on the board so that no two rooks beat each other.
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 integers *x*1, *y*1, *x*2 and *y*2 (1<=<=≀<=*x*1<=≀<=*x*2<=≀<=*n*, 1<=<=≀<=*y*1<=≀<=*y*2<=≀<=*n*)Β β€” the coordinates of the lower left and the upper right cells of a deleted rectangle. If is guaranteed that the rectangles do not intersect.
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 balls are numbered from 1 to *n* (by the demand of the inventory commission). After receiving the balls the students perform the warm-up exercise. The exercise takes place in a few throws. For each throw the teacher chooses any two arbitrary different students who will participate in it. The selected students throw their balls to each other. Thus, after each throw the students remain in their positions, and the two balls are swapped. In this case there was a throw between the students, who were holding the 2-nd and the 4-th balls. Since the warm-up has many exercises, each of them can only continue for little time. Therefore, for each student we know the maximum number of throws he can participate in. For this lessons maximum number of throws will be 1 or 2. Note that after all phases of the considered exercise any ball can end up with any student. Smart Beaver decided to formalize it and introduced the concept of the "ball order". The ball order is a sequence of *n* numbers that correspond to the order of balls in the line. The first number will match the number of the ball of the first from the left student in the line, the second number will match the ball of the second student, and so on. For example, in figure 2 the order of the balls was (1, 2, 3, 4, 5), and after the throw it was (1, 4, 3, 2, 5). Smart beaver knows the number of students and for each student he knows the maximum number of throws in which he can participate. And now he is wondering: what is the number of distinct ways of ball orders by the end of the exercise.
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 throws he can participate in. The input limits for scoring 30 points are (subproblem D1): - 1<=≀<=*n*<=≀<=10. The input limits for scoring 70 points are (subproblems D1+D2): - 1<=≀<=*n*<=≀<=500. The input limits for scoring 100 points are (subproblems D1+D2+D3): - 1<=≀<=*n*<=≀<=1000000.
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" }, { "input": "2\n1 1", "output": "2" }, { "input": "10\n2 2 2 2 2 2 1 2 1 1", "output": "2419200" }, { "input": "10\n2 1 1 2 1 2 1 2 2 1", "output": "786240" }, { "input": "10\n1 1 1 1 1 1 1 1 1 1", "output": "9496" }, { "input": "10\n1 1 1 1 2 1 1 1 1 1", "output": "26200" }, { "input": "10\n2 1 2 2 2 2 2 2 2 2", "output": "3628800" }, { "input": "10\n2 2 2 2 2 2 2 2 2 2", "output": "3628800" }, { "input": "10\n1 1 1 1 1 2 1 1 2 1", "output": "68760" }, { "input": "9\n1 1 1 2 2 1 1 1 1", "output": "16704" }, { "input": "100\n2 2 2 2 1 1 2 2 1 2 2 1 1 2 2 2 2 2 2 1 1 2 2 2 2 1 2 1 1 2 2 1 2 2 1 2 2 2 2 2 2 1 2 2 2 2 2 1 2 1 2 2 2 2 2 2 2 2 1 2 2 2 2 2 1 2 1 1 2 1 1 2 2 2 2 1 2 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 2", "output": "834603967" }, { "input": "200\n1 2 1 1 2 2 1 1 1 1 1 1 2 1 2 2 2 2 2 1 2 2 1 1 2 2 2 2 2 1 2 1 1 2 2 2 1 2 2 2 1 1 2 2 2 1 2 2 1 2 1 2 2 1 2 2 2 2 2 1 2 2 1 2 1 1 2 2 2 2 2 2 2 1 2 2 2 1 1 2 2 2 2 1 1 2 2 1 1 1 2 2 1 1 2 2 1 1 2 2 2 1 2 2 2 1 2 1 2 2 2 2 2 2 2 1 1 1 2 2 2 2 2 2 2 2 1 1 2 2 2 2 2 1 2 1 2 1 1 1 2 2 2 1 2 1 2 2 1 2 2 1 2 1 2 1 1 2 2 1 2 2 1 2 1 1 2 1 1 2 2 2 2 1 2 1 1 1 1 1 1 1 1 2 1 2 1 1 2 1 2 1 2 2 1 2 1 1 1 2", "output": "83780780" } ]
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 damage is described with a segment [*l*,<=*r*], where *l* and *r* are integer numbers. The potential gun damage *x* is chosen with equal probability among all integer numbers of the segment [*l*,<=*r*]. If the shot pierces the armor of an enemy's tank then the enemy loses *x* hit points. If the number of hit points becomes non-positive then the enemy tank is considered destroyed. It is possible that the shot does not pierce the armor of a tank. In this case the number of hit points doesn't change. The probability that the armor will not be pierced is considered as the shooting tank attribute and does not depend on players' behavior. The victory is near and there is only one enemy tank left. Vasya is ready for the battle β€” one more battle between the Good and the Evil is inevitable! Two enemies saw each other and each of them fired a shot at the same moment... The last battle has begun! Help Vasya to determine what is the probability that he will win the battle by destroying the enemy tank? If both tanks are destroyed (after simultaneous shots), then Vasya is considered a winner. You can assume that each player fires a shot just after the gun recharge and each tank has infinite number of ammo.
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 pierced *p* (0<=≀<=*p*<=≀<=100) (percents). The second line describes the tank of Vasya's enemy in the same format.
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 damage 50. The probability of that event is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5b8cbead818d4c86d1b85c58f6f83a955392b329.png" style="max-width: 100.0%;max-height: 100.0%;"/> = <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3aa776870a184225f95e5a8de47bdeadf28fa805.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Otherwise, Vasya wins. In the third example Vasya's enemy tank fires three shots with probability of armor piercing 0.5. In order to destroy Vasya's tank it is necessary that at least 2 of 3 shots pierce the armor of Vasya's tank. The probability of this event is 0.5.
[]
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 (*x*,<=*y*) to either (*x*<=+<=1,<=*y*) (rightward) or (*x*,<=*y*<=+<=1) (upward). In her castle, *c*2 sensors are set at points (*a*<=+<=*i*,<=*b*<=+<=*j*) (for every integer *i* and *j* such that: 0<=≀<=*i*<=&lt;<=*c*,<=0<=≀<=*j*<=&lt;<=*c*). Each sensor has a count value and decreases its count value every time Ciel moves. Initially, the count value of each sensor is *t*. Every time Ciel moves to point (*x*,<=*y*), the count value of a sensor at point (*u*,<=*v*) decreases by (|*u*<=-<=*x*|<=+<=|*v*<=-<=*y*|). When the count value of some sensor becomes strictly less than 0, the sensor will catch Ciel as a suspicious individual! Determine whether Ciel can move from (1,<=1) to (*n*,<=*n*) without being caught by a sensor, and if it is possible, output her steps. Assume that Ciel can move to every point even if there is a censor on the 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 stream (also you may use the %I64d specificator).
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 character U. If her objective is impossible, output Impossible.
[ "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 two types of queries: - "C *a* *w*": The price in city *a* is changed to *w*.- "A *a* *b*": Now a tourist will travel from city *a* to *b*. He will choose a route, he also doesn't want to visit a city twice. He will buy souvenirs at the city where the souvenirs are the cheapest (possibly exactly at city *a* or *b*). You should output the minimum possible price that he can buy the souvenirs during his travel. More formally, we can define routes as follow: - A route is a sequence of cities [*x*1,<=*x*2,<=...,<=*x**k*], where *k* is a certain positive integer.- For any 1<=≀<=*i*<=&lt;<=*j*<=≀<=*k*,<=*x**i*<=β‰ <=*x**j*.- For any 1<=≀<=*i*<=&lt;<=*k*, there is a road connecting *x**i* and *x**i*<=+<=1.- The minimum price of the route is *min*(*w**x*1,<=*w**x*2,<=...,<=*w**x**k*).- The required answer is the minimum value of the minimum prices of all valid routes from *a* to *b*.
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*). It is guaranteed that there is at most one road connecting the same pair of cities. There is always at least one valid route between any two cities. Next *q* lines each describe a query. The format is "C *a* *w*" or "A *a* *b*" (1<=≀<=*a*,<=*b*<=≀<=*n*,<=1<=≀<=*w*<=≀<=109).
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 5\n4 6\nC 6 2\nA 5 4\nA 5 6\nA 4 1\nC 1 5", "output": "4\n2\n4" }, { "input": "9 9 2\n2\n1\n8\n7\n7\n6\n7\n8\n10\n2 1\n1 7\n3 2\n4 3\n5 4\n5 6\n6 7\n4 8\n2 9\nC 2 4\nA 4 3", "output": "2" }, { "input": "5 6 6\n618977315\n274056818\n29588291\n424089927\n955162385\n2 1\n3 1\n3 2\n4 1\n5 1\n4 5\nA 3 1\nC 2 99159078\nA 3 1\nA 1 2\nA 2 2\nC 5 329955586", "output": "29588291\n29588291\n29588291\n99159078" }, { "input": "5 6 6\n124820425\n698207180\n616852002\n952907056\n696244135\n2 1\n1 3\n2 3\n1 4\n1 5\n5 4\nC 1 794202849\nA 3 1\nC 3 881984762\nA 4 1\nA 4 3\nA 3 4", "output": "616852002\n696244135\n696244135\n696244135" }, { "input": "9 12 12\n143171545\n629420297\n228288417\n529363375\n635420451\n749686836\n939343781\n970697126\n468856358\n2 1\n3 1\n3 2\n2 4\n5 2\n5 4\n6 5\n7 5\n7 6\n7 8\n7 9\n8 9\nC 9 227315421\nC 9 29228306\nA 8 4\nC 2 328781042\nC 5 656724208\nA 4 7\nA 4 4\nC 4 176212145\nC 9 69177357\nC 4 867393574\nC 8 541752763\nA 6 7", "output": "29228306\n328781042\n529363375\n656724208" }, { "input": "8 12 12\n143171545\n629420297\n228288417\n529363375\n635420451\n749686836\n939343781\n970697126\n1 2\n3 1\n3 2\n3 4\n3 5\n5 4\n6 5\n7 5\n7 6\n8 7\n7 1\n8 1\nA 5 7\nC 2 748240057\nC 1 117696912\nC 2 712069587\nA 8 1\nA 8 4\nA 8 8\nA 1 3\nA 5 1\nC 6 514703751\nA 3 7\nC 2 689032050", "output": "143171545\n117696912\n117696912\n970697126\n117696912\n117696912\n117696912" }, { "input": "11 10 11\n168416829\n247680897\n543015760\n694470709\n207722433\n166154497\n173991772\n592570924\n526914705\n148263494\n331109771\n2 1\n2 3\n3 4\n4 5\n6 5\n7 6\n8 7\n8 9\n10 9\n10 11\nA 8 10\nC 1 722476895\nA 3 5\nA 10 10\nC 5 372352248\nA 1 5\nA 6 9\nA 1 4\nC 11 583062476\nC 10 894427427\nC 6 410694418", "output": "148263494\n207722433\n148263494\n247680897\n166154497\n247680897" }, { "input": "13 13 13\n1180062\n809165625\n198328320\n579614756\n798375445\n393766650\n488716103\n185321\n909870046\n678057777\n762960310\n109455607\n34214450\n13 1\n1 2\n2 3\n4 3\n4 5\n6 5\n6 7\n8 7\n9 8\n9 10\n10 11\n12 11\n12 13\nC 8 346790012\nC 10 43312658\nA 8 11\nC 10 141932552\nC 9 348956056\nC 11 168966323\nA 9 8\nC 3 82437975\nC 12 773834608\nC 6 906216632\nC 2 116767622\nA 7 5\nA 3 5", "output": "1180062\n1180062\n1180062\n1180062" }, { "input": "17 16 17\n406384223\n77359273\n803920734\n909711362\n124905660\n143958252\n562875165\n665671011\n350687352\n882870535\n771885580\n935734323\n330484629\n268849742\n708711867\n67281248\n175120843\n1 2\n1 3\n1 4\n5 1\n1 6\n7 1\n8 1\n9 1\n10 1\n11 1\n1 12\n13 1\n14 1\n15 1\n1 16\n1 17\nC 2 479615141\nA 9 9\nA 15 1\nC 10 673514009\nC 5 959984957\nC 4 960283357\nA 5 13\nA 1 14\nA 11 1\nC 14 737794791\nC 1 138944365\nA 13 3\nA 10 1\nC 6 433580974\nA 7 3\nC 7 841165917\nA 14 5", "output": "350687352\n406384223\n330484629\n268849742\n406384223\n138944365\n138944365\n138944365\n138944365" }, { "input": "14 13 14\n565045326\n442424341\n246080343\n449574683\n738926143\n580184823\n515917116\n129120968\n513672580\n940695463\n904014028\n817155014\n432023819\n882120662\n1 2\n1 3\n2 4\n5 1\n6 1\n2 7\n7 8\n4 9\n4 10\n7 11\n12 1\n13 2\n5 14\nA 14 4\nC 7 336451374\nA 12 2\nA 11 6\nC 10 587185819\nA 11 1\nC 3 495392854\nC 13 527875170\nC 12 27145555\nC 12 255001950\nC 13 583910432\nA 11 12\nA 9 9\nC 3 192594547", "output": "442424341\n442424341\n336451374\n336451374\n255001950\n513672580" }, { "input": "14 13 14\n565045326\n442424341\n246080343\n449574683\n738926143\n580184823\n515917116\n129120968\n513672580\n940695463\n904014028\n817155014\n432023819\n882120662\n1 2\n1 3\n2 4\n5 1\n6 1\n2 7\n7 8\n4 9\n4 10\n7 11\n12 1\n13 2\n5 14\nA 4 2\nA 7 14\nA 7 2\nA 2 2\nA 2 12\nA 11 6\nA 1 7\nA 12 10\nA 11 1\nA 12 9\nA 11 3\nA 13 2\nA 7 1\nA 11 12", "output": "442424341\n442424341\n442424341\n442424341\n442424341\n442424341\n442424341\n442424341\n442424341\n442424341\n246080343\n432023819\n442424341\n442424341" }, { "input": "9 10 20\n725514155\n139059185\n845219142\n62528090\n359447816\n594676316\n130573598\n637054610\n889929556\n1 2\n1 5\n3 2\n4 3\n5 4\n6 4\n5 7\n5 9\n7 8\n9 8\nA 5 3\nC 1 299123056\nA 4 3\nA 7 9\nA 1 3\nC 1 977424768\nA 7 2\nC 1 14397410\nA 3 2\nA 4 3\nC 2 371790984\nA 1 9\nA 6 3\nA 7 3\nA 7 6\nA 2 6\nC 3 412013208\nC 8 109308692\nA 8 1\nC 4 15914749", "output": "62528090\n62528090\n130573598\n62528090\n62528090\n14397410\n14397410\n14397410\n14397410\n14397410\n14397410\n14397410\n14397410" }, { "input": "6 7 20\n136100754\n464703381\n503267626\n518409487\n881695099\n317555540\n1 2\n3 1\n3 2\n4 3\n3 5\n5 4\n6 1\nA 5 6\nC 5 496993813\nA 3 1\nC 4 836074856\nC 3 40217114\nC 5 41131955\nA 1 5\nA 4 2\nA 3 3\nA 3 1\nC 4 478319989\nC 4 27297711\nC 4 83870057\nC 6 987388864\nC 3 821342180\nC 3 526577833\nC 4 152107643\nA 3 6\nC 6 9683741\nC 4 914297322", "output": "136100754\n136100754\n40217114\n40217114\n40217114\n40217114\n136100754" } ]
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\leq R\leq r$ and bitwise OR of all integers in the range $[L, R]$ is at least $x$ (note that $x$ is a constant for all queries). Can you help Sonya perform all her queries? Bitwise OR is a binary operation on a pair of non-negative integers. To calculate the bitwise OR of two numbers, you need to write both numbers in binary notation. The result is a number, in binary, which contains a one in each digit if there is a one in the binary notation of at least one of the two numbers. For example, $10$ OR $19$ = $1010_2$ OR $10011_2$ = $11011_2$ = $27$.
The first line contains three integers $n$, $m$, and $x$ ($1\leq n, m\leq 10^5$, $0\leq x&lt;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&lt;2^{20}$)Β β€” numbers of the array. The following $m$ lines each describe an query. A line has one of the following formats: - $1$ $i$ $y$ ($1\leq i\leq n$, $0\leq y&lt;2^{20}$), meaning that you have to replace $a_{i}$ by $y$; - $2$ $l$ $r$ ($1\leq l\leq r\leq n$), meaning that you have to find the number of subarrays on the segment from $l$ to $r$ that the bitwise OR of all numbers there is at least $x$.
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 operation, the array will consist of [$7$, $3$, $6$, $1$]; 1. on the segment [$1\ldots4$], you can choose pairs ($1$, $1$), ($1$, $2$), ($1$, $3$), ($1$, $4$), ($2$, $3$), ($2$, $4$), and ($3$, $4$); 1. on the segment [$1\ldots3$], you can choose pairs ($1$, $1$), ($1$, $2$), ($1$, $3$), and ($2$, $3$); 1. on the segment [$1\ldots1$], you can choose pair ($1$, $1$); 1. the third number is being replacing by $0$, after this operation, the array will consist of [$7$, $3$, $0$, $1$]; 1. on the segment [$1\ldots4$], you can choose pairs ($1$, $1$), ($1$, $2$), ($1$, $3$), and ($1$, $4$). In the second example, there are an array [$6$, $0$, $3$, $15$, $2$] are queries: 1. on the segment [$1\ldots5$], you can choose pairs ($1$, $3$), ($1$, $4$), ($1$, $5$), ($2$, $4$), ($2$, $5$), ($3$, $4$), ($3$, $5$), ($4$, $4$), and ($4$, $5$); 1. the fourth number is being replacing by $4$, after this operation, the array will consist of [$6$, $0$, $3$, $4$, $2$]; 1. on the segment [$1\ldots5$], you can choose pairs ($1$, $3$), ($1$, $4$), ($1$, $5$), ($2$, $4$), ($2$, $5$), ($3$, $4$), and ($3$, $5$); 1. on the segment [$3\ldots5$], you can choose pairs ($3$, $4$) and ($3$, $5$); 1. on the segment [$1\ldots4$], you can choose pairs ($1$, $3$), ($1$, $4$), ($2$, $4$), and ($3$, $4$).
[]
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 the cards and cast the spell written on it, but only if first two of it's characteristics meet the requirement *a**i*<=≀<=*x* and *b**i*<=≀<=*y*, i.e. if the player has enough magic skill to cast this spell. However, after casting the spell the characteristics of a player change and become equal to *x*<==<=*c**i* and *y*<==<=*d**i*. At the beginning of the game both characteristics of a player are equal to zero. The goal of the game is to cast the *n*-th spell. Your task is to make it in as few moves as possible. You are allowed to use spell in any order and any number of times (for example, you may not use some spells at all).
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*-th spell, print <=-<=1.
[ "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 *m* characters. A character equals '1', if the corresponding photo pixel is white and '0', if it is black. Polycarpus thinks that he has found a star on the photo if he finds a white pixel surrounded by four side-neighboring pixels that are also white: Polycarpus whats to cut out a rectangular area from the photo and give his mom as a present. This area should contain no less than *k* stars. The stars can intersect, have shared white pixels on the photo. The boy will cut out the rectangular area so that its borders will be parallel to the sides of the photo and the cuts will go straight between the pixel borders. Now Polycarpus keeps wondering how many ways there are to cut an area out of the photo so that it met the conditions given above. Help Polycarpus find this number.
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*, *y*), where *x* β‰₯ 3 and *y* β‰₯ 4 fit the conditions. In the second sample any rectangle whose each side is no less than four, will do. The possible rectangle sizes are 4 × 4, 4 × 5, 5 × 4 and 5 × 5. Such figures can be cut in 4 ways, 2 ways, 2 ways and 1 way correspondingly. Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use cin, cout streams or the %I64d specificator.
[]
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 segment with ends at points (*x*<=-<=*t*,<=*y*<=+<=*t*), (*x*<=+<=*t*,<=*y*<=-<=*t*). Similarly, at time *t* the red baby (*x*,<=*y*) is a red segment with ends at points (*x*<=+<=*t*,<=*y*<=+<=*t*), (*x*<=-<=*t*,<=*y*<=-<=*t*) of the plane. Initially, at time *t*<==<=0 all babies are points on the plane. The goal of the game is to find the first integer moment of time when the plane contains a rectangle of a non-zero area which sides are fully covered by some babies. A side may be covered by multiple babies. More formally, each point of each side of the rectangle should be covered by at least one baby of any color. At that, you must assume that the babies are closed segments, that is, they contain their endpoints. You are given the positions of all babies β€” help Inna and Dima to find the required moment of time.
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 of the input don't exceed 106 in their absolute value. Note that all babies stand in distinct points.
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 be demoralizing for the weakest cows who lose most or all of their matches, so he wants to flip some of the results. In particular, at *k* different instances, he will take two integers *a**i*,<=*b**i* (*a**i*<=&lt;<=*b**i*) and flip all the results between cows with skill level between *a**i* and *b**i* inclusive. That is, for any pair *x*,<=*y* he will change the result of the match on the final scoreboard (so if *x* won the match, the scoreboard will now display that *y* won the match, and vice versa). It is possible that Farmer John will change the result of a match multiple times. It is not guaranteed that *a**i* and *b**i* are equal to some cow's skill level. Farmer John wants to determine how balanced he made the tournament results look. In particular, he wants to count 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*. Help him determine this number. Note that two triples are considered different if they do not contain the same set of cows (i.e. if there is a cow in one triple that is not in the other).
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 *b**i* (1<=≀<=*a**i*<=&lt;<=*b**i*<=≀<=109) representing the changes Farmer John made to the scoreboard in the order he makes it.
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 the %I64d specifier.
[ "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 &gt; cow 1, cow 3 &gt; cow 2, and cow 2 &gt; 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 &gt; cow 2, cow 2 &gt; cow 3, and cow 3 &gt; 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*,<=*n*). On each of the unit squares Petya built a tower of wooden cubes. The side of a wooden cube also has a unit length. After that Petya went an infinitely large distance away from his masterpiece and looked at it in the direction of vector *v*<==<=(*v**x*,<=*v**y*,<=0). Petya wonders, how many distinct cubes are visible from this position. Help him, find this number. Each cube includes the border. We think that a cube is visible if there is a ray emanating from some point *p*, belonging to the cube, in the direction of vector <=-<=*v*, that doesn't contain any points, belonging to other cubes.
The first line contains three integers *n*, *v**x* and *v**y* (1<=≀<=*n*<=≀<=103, |*v**x*|,<=|*v**y*|<=≀<=|104|, |*v**x*|<=+<=|*v**y*|<=&gt;<=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 tower that stands on the unit square with opposite corners at points (*i*<=-<=1,<=*j*<=-<=1) and (*i*,<=*j*).
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 the field, everything else is outside. In the beginning, there are *N* fans on the field. For each fan we are given his speed, an integer *v**i* as well as his integer coordinates (*x**i*, *y**i*). A fan with those coordinates might move and after one second he might be at any point (*x**i*<=+<=*p*, *y**i*<=+<=*q*) where 0<=≀<=|*p*|<=+<=|*q*|<=≀<=*v**i*. *p*, *q* are both integers. Points that go outside of the square that represents the field are excluded and all others have equal probability of being the location of that specific fan after one second. Andrej, a young and promising police officer, has sent a flying drone to take a photo of the riot from above. The drone’s camera works like this: 1. It selects three points with integer coordinates such that there is a chance of a fan appearing there after one second. They must not be collinear or the camera won’t work. It is guaranteed that not all of the initial positions of fans will be on the same line. 1. Camera focuses those points and creates a circle that passes through those three points. A photo is taken after one second (one second after the initial state). 1. Everything that is on the circle or inside it at the moment of taking the photo (one second after focusing the points) will be on the photo. Your goal is to select those three points so that the expected number of fans seen on the photo is maximized. If there are more such selections, select those three points that give the circle with largest radius among them. If there are still more suitable selections, any one of them will be accepted. If your answer follows conditions above and radius of circle you return is smaller then the optimal one by 0.01, your output will be considered as correct. No test will have optimal radius bigger than 1010.
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 - 0<=≀<=*v**i*<=≀<=1000 - All numbers are integers
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*, *T*(*s*) is its *Goodness*. *T*(*s*) can be calculated as follows: Consider there are exactly *m* blocks of 1s in this string (a block of 1s in *s* is a maximal consecutive substring of *s* that only contains 1) with lengths *x*1,<=*x*2,<=...,<=*x**m*. Define where *f* is a given sequence (if *m*<==<=0, then *T*(*s*)<==<=0). Tavas loves queries. He asks you to answer *q* queries. In each query he gives you numbers *v*,<=*u*,<=*l* and you should print following number: Consider the roads on the path from city *v* to city *u*: *e*1,<=*e*2,<=...,<=*e**x*. Build the binary string *b* of length *x* such that: *b**i*<==<=1 if and only if *l*<=≀<=*w*(*e**i*) where *w*(*e*) is the length of road *e*. You should print *T*(*b*) for this query.
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 it means that there's a road between cities *v* and *u* of length *w* (1<=≀<=*v*,<=*u*<=≀<=*n* and 1<=≀<=*w*<=≀<=109). The next *q* lines contain the details of the queries. Each line contains integers *v*,<=*u*,<=*l* (1<=≀<=*v*,<=*u*<=≀<=*n*, *v*<=β‰ <=*u* and 1<=≀<=*l*<=≀<=109).
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 equiprobably. Consider the following pseudo-code: *V*(*x*) is the list vertices adjacent to *x*. The *flag* array is initially filled as FALSE. *DFS* initially starts with a parameter of an entrance vertex. When the search is finished, variable *count* will contain the number of moves. Your task is to count the mathematical expectation of the number of moves one has to do to exit the maze.
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* lines contain pairs of non-negative numbers *x**i* and *y**i*, which represent the probability of choosing the *i*-th vertex as an entrance and exit correspondingly. The probabilities to choose vertex *i* as an entrance and an exit equal and correspondingly. The sum of all *x**i* and the sum of all *y**i* are positive and do not exceed 106.
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 (symmetrical cases). During the first move one can go to the exit vertex with the probability of 0.5 or to go to a vertex that's not the exit vertex with the probability of 0.5. In the first case the number of moves equals 1, in the second one it equals 3. The total mathematical expectation is counted as 2 / 5 × (1 × 0.5 + 3 × 0.5) + 3 / 5 × (1 × 0.5 + 3 × 0.5)
[]
92
0
0
570,868