contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
733 | Sleep in Class | [
"constructive algorithms",
"data structures",
"math",
"two pointers"
] | null | null | The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs.
The stairs consists of *n* steps. The steps are numbered from bottom to top, it means that the lowest step has number 1, and the highest step has number *n*. Above each of them there is a pointer with the direction (up or down) Olga should move from this step. As soon as Olga goes to the next step, the direction of the pointer (above the step she leaves) changes. It means that the direction "up" changes to "down", the direction "down" — to the direction "up".
Olga always moves to the next step in the direction which is shown on the pointer above the step.
If Olga moves beyond the stairs, she will fall and wake up. Moving beyond the stairs is a moving down from the first step or moving up from the last one (it means the *n*-th) step.
In one second Olga moves one step up or down according to the direction of the pointer which is located above the step on which Olga had been at the beginning of the second.
For each step find the duration of the dream if Olga was at this step at the beginning of the dream.
Olga's fall also takes one second, so if she was on the first step and went down, she would wake up in the next second. | The first line contains single integer *n* (1<=≤<=*n*<=≤<=106) — the number of steps on the stairs.
The second line contains a string *s* with the length *n* — it denotes the initial direction of pointers on the stairs. The *i*-th character of string *s* denotes the direction of the pointer above *i*-th step, and is either 'U' (it means that this pointer is directed up), or 'D' (it means this pointed is directed down).
The pointers are given in order from bottom to top. | Print *n* numbers, the *i*-th of which is equal either to the duration of Olga's dream or to <=-<=1 if Olga never goes beyond the stairs, if in the beginning of sleep she was on the *i*-th step. | [
"3\nUUD\n",
"10\nUUDUDUUDDU\n"
] | [
"5 6 3 ",
"5 12 23 34 36 27 18 11 6 1 "
] | none | [
{
"input": "3\nUUD",
"output": "5 6 3 "
},
{
"input": "10\nUUDUDUUDDU",
"output": "5 12 23 34 36 27 18 11 6 1 "
},
{
"input": "10\nDUDDUUDUDD",
"output": "1 4 7 14 23 32 30 19 12 5 "
},
{
"input": "1\nD",
"output": "1 "
},
{
"input": "2\nDU",
"output": "1 1 "
... | 2,000 | 262,144,000 | 0 | 60,871 | |
0 | none | [
"none"
] | null | null | A divisor tree is a rooted tree that meets the following conditions:
- Each vertex of the tree contains a positive integer number. - The numbers written in the leaves of the tree are prime numbers. - For any inner vertex, the number within it is equal to the product of the numbers written in its children.
Manao has *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n*. He tries to build a divisor tree which contains each of these numbers. That is, for each *a**i*, there should be at least one vertex in the tree which contains *a**i*. Manao loves compact style, but his trees are too large. Help Manao determine the minimum possible number of vertices in the divisor tree sought. | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=8). The second line contains *n* distinct space-separated integers *a**i* (2<=≤<=*a**i*<=≤<=1012). | Print a single integer — the minimum number of vertices in the divisor tree that contains each of the numbers *a**i*. | [
"2\n6 10\n",
"4\n6 72 8 4\n",
"1\n7\n"
] | [
"7\n",
"12\n",
"1\n"
] | Sample 1. The smallest divisor tree looks this way: <img class="tex-graphics" src="https://espresso.codeforces.com/8b607704e04231c95d3ba0058b84ab4e784560cf.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Sample 2. In this case you can build the following divisor tree: <img class="tex-graphics" src="https://espresso.codeforces.com/e5f63d35ad6398cefa2fae49ed39d5d4fad2e07d.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Sample 3. Note that the tree can consist of a single vertex. | [] | 46 | 0 | 0 | 61,092 | |
316 | Summer Homework | [
"brute force",
"data structures"
] | null | null | By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher:
You are given a sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. Your task is to perform on it *m* consecutive operations of the following type:
1. For given numbers *x**i* and *v**i* assign value *v**i* to element *a**x**i*. 1. For given numbers *l**i* and *r**i* you've got to calculate sum , where *f*0<==<=*f*1<==<=1 and at *i*<=≥<=2: *f**i*<==<=*f**i*<=-<=1<=+<=*f**i*<=-<=2. 1. For a group of three numbers *l**i* *r**i* *d**i* you should increase value *a**x* by *d**i* for all *x* (*l**i*<=≤<=*x*<=≤<=*r**i*).
Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2·105) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=105). Then follow *m* lines, each describes an operation. Each line starts with an integer *t**i* (1<=≤<=*t**i*<=≤<=3) — the operation type:
- if *t**i*<==<=1, then next follow two integers *x**i* *v**i* (1<=≤<=*x**i*<=≤<=*n*,<=0<=≤<=*v**i*<=≤<=105); - if *t**i*<==<=2, then next follow two integers *l**i* *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*); - if *t**i*<==<=3, then next follow three integers *l**i* *r**i* *d**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*,<=0<=≤<=*d**i*<=≤<=105).
The input limits for scoring 30 points are (subproblem E1):
- It is guaranteed that *n* does not exceed 100, *m* does not exceed 10000 and there will be no queries of the 3-rd type.
The input limits for scoring 70 points are (subproblems E1+E2):
- It is guaranteed that there will be queries of the 1-st and 2-nd type only.
The input limits for scoring 100 points are (subproblems E1+E2+E3):
- No extra limitations. | For each query print the calculated sum modulo 1000000000 (109). | [
"5 5\n1 3 1 2 4\n2 1 4\n2 1 5\n2 2 4\n1 3 10\n2 1 5\n",
"5 4\n1 3 1 2 4\n3 1 4 1\n2 2 4\n1 2 10\n2 1 5\n"
] | [
"12\n32\n8\n50\n",
"12\n45\n"
] | none | [
{
"input": "5 5\n1 3 1 2 4\n2 1 4\n2 1 5\n2 2 4\n1 3 10\n2 1 5",
"output": "12\n32\n8\n50"
},
{
"input": "1 3\n2\n2 1 1\n1 1 3\n2 1 1",
"output": "2\n3"
},
{
"input": "11 11\n6 1 9 0 2 9 1 6 2 8 0\n2 9 9\n1 9 0\n1 1 8\n2 2 5\n2 7 11\n2 2 8\n1 3 2\n1 10 0\n2 1 8\n2 9 11\n1 9 7",
"outp... | 154 | 2,969,600 | 0 | 61,230 | |
145 | Lucky Queries | [
"data structures"
] | null | null | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya brought home string *s* with the length of *n*. The string only consists of lucky digits. The digits are numbered from the left to the right starting with 1. Now Petya should execute *m* queries of the following form:
- switch *l* *r* — "switch" digits (i.e. replace them with their opposites) at all positions with indexes from *l* to *r*, inclusive: each digit 4 is replaced with 7 and each digit 7 is replaced with 4 (1<=≤<=*l*<=≤<=*r*<=≤<=*n*); - count — find and print on the screen the length of the longest non-decreasing subsequence of string *s*.
Subsequence of a string *s* is a string that can be obtained from *s* by removing zero or more of its elements. A string is called non-decreasing if each successive digit is not less than the previous one.
Help Petya process the requests. | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=106,<=1<=≤<=*m*<=≤<=3·105) — the length of the string *s* and the number of queries correspondingly. The second line contains *n* lucky digits without spaces — Petya's initial string. Next *m* lines contain queries in the form described in the statement. | For each query count print an answer on a single line. | [
"2 3\n47\ncount\nswitch 1 2\ncount\n",
"3 5\n747\ncount\nswitch 1 1\ncount\nswitch 1 3\ncount\n"
] | [
"2\n1\n",
"2\n3\n2\n"
] | In the first sample the chronology of string *s* after some operations are fulfilled is as follows (the sought maximum subsequence is marked with bold):
1. 47 1. 74 1. 74 1. 747 1. 447 1. 447 1. 774 1. 774 | [] | 154 | 6,963,200 | -1 | 61,657 | |
637 | Running with Obstacles | [
"*special",
"data structures",
"dp",
"greedy"
] | null | null | A sportsman starts from point *x**start*<==<=0 and runs to point with coordinate *x**finish*<==<=*m* (on a straight line). Also, the sportsman can jump — to jump, he should first take a run of length of not less than *s* meters (in this case for these *s* meters his path should have no obstacles), and after that he can jump over a length of not more than *d* meters. Running and jumping is permitted only in the direction from left to right. He can start andfinish a jump only at the points with integer coordinates in which there are no obstacles. To overcome some obstacle, it is necessary to land at a point which is strictly to the right of this obstacle.
On the way of an athlete are *n* obstacles at coordinates *x*1,<=*x*2,<=...,<=*x**n*. He cannot go over the obstacles, he can only jump over them. Your task is to determine whether the athlete will be able to get to the finish point. | The first line of the input containsd four integers *n*, *m*, *s* and *d* (1<=≤<=*n*<=≤<=200<=000, 2<=≤<=*m*<=≤<=109, 1<=≤<=*s*,<=*d*<=≤<=109) — the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly.
The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*m*<=-<=1) — the coordinates of the obstacles. It is guaranteed that the starting and finishing point have no obstacles, also no point can have more than one obstacle, The coordinates of the obstacles are given in an arbitrary order. | If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes).
If the athlete can get from start to finish, print any way to do this in the following format:
- print a line of form "RUN X>" (where "X" should be a positive integer), if the athlete should run for "X" more meters; - print a line of form "JUMP Y" (where "Y" should be a positive integer), if the sportsman starts a jump and should remain in air for "Y" more meters.
All commands "RUN" and "JUMP" should strictly alternate, starting with "RUN", besides, they should be printed chronologically. It is not allowed to jump over the finishing point but it is allowed to land there after a jump. The athlete should stop as soon as he reaches finish. | [
"3 10 1 3\n3 4 7\n",
"2 9 2 3\n6 4\n"
] | [
"RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2\n",
"IMPOSSIBLE\n"
] | none | [
{
"input": "3 10 1 3\n3 4 7",
"output": "RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2"
},
{
"input": "2 9 2 3\n6 4",
"output": "IMPOSSIBLE"
},
{
"input": "10 100 2 8\n93 35 24 87 39 46 86 37 73 33",
"output": "RUN 23\nJUMP 2\nRUN 7\nJUMP 8\nRUN 5\nJUMP 2\nRUN 25\nJUMP 2\nRUN 11\nJUMP 3\nRUN 4... | 2,000 | 16,691,200 | 0 | 61,686 | |
825 | Tree Queries | [
"dfs and similar",
"graphs",
"trees"
] | null | null | You are given a tree consisting of *n* vertices (numbered from 1 to *n*). Initially all vertices are white. You have to process *q* queries of two different types:
1. 1 *x* — change the color of vertex *x* to black. It is guaranteed that the first query will be of this type. 1. 2 *x* — for the vertex *x*, find the minimum index *y* such that the vertex with index *y* belongs to the simple path from *x* to some black vertex (a simple path never visits any vertex more than once).
For each query of type 2 print the answer to it.
Note that the queries are given in modified way. | The first line contains two numbers *n* and *q* (3<=≤<=*n*,<=*q*<=≤<=106).
Then *n*<=-<=1 lines follow, each line containing two numbers *x**i* and *y**i* (1<=≤<=*x**i*<=<<=*y**i*<=≤<=*n*) and representing the edge between vertices *x**i* and *y**i*.
It is guaranteed that these edges form a tree.
Then *q* lines follow. Each line contains two integers *t**i* and *z**i*, where *t**i* is the type of *i*th query, and *z**i* can be used to restore *x**i* for this query in this way: you have to keep track of the answer to the last query of type 2 (let's call this answer *last*, and initially *last*<==<=0); then *x**i*<==<=(*z**i*<=+<=*last*)<=*mod*<=*n*<=+<=1.
It is guaranteed that the first query is of type 1, and there is at least one query of type 2. | For each query of type 2 output the answer to it. | [
"4 6\n1 2\n2 3\n3 4\n1 2\n1 2\n2 2\n1 3\n2 2\n2 2\n"
] | [
"3\n2\n1\n"
] | none | [
{
"input": "4 6\n1 2\n2 3\n3 4\n1 2\n1 2\n2 2\n1 3\n2 2\n2 2",
"output": "3\n2\n1"
},
{
"input": "10 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n1 3\n2 10\n1 8\n1 7\n1 6\n1 4\n2 9\n2 7\n1 5\n1 5",
"output": "1\n1\n4"
},
{
"input": "10 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 1... | 2,479 | 225,177,600 | 3 | 61,736 | |
453 | Little Pony and Elements of Harmony | [
"dp",
"matrices"
] | null | null | The Elements of Harmony are six supernatural artifacts representing subjective aspects of harmony. They are arguably the most powerful force in Equestria. The inside of Elements of Harmony can be seen as a complete graph with *n* vertices labeled from 0 to *n*<=-<=1, where *n* is a power of two, equal to 2*m*.
The energy in Elements of Harmony is in constant movement. According to the ancient book, the energy of vertex *u* in time *i* (*e**i*[*u*]) equals to:
Here *b*[] is the transformation coefficient — an array of *m*<=+<=1 integers and *f*(*u*,<=*v*) is the number of ones in the binary representation of number (*u* *xor* *v*).
Given the transformation coefficient and the energy distribution at time 0 (*e*0[]). Help Twilight Sparkle predict the energy distribution at time *t* (*e**t*[]). The answer can be quite large, so output it modulo *p*. | The first line contains three integers *m*, *t* and *p* (1<=≤<=*m*<=≤<=20; 0<=≤<=*t*<=≤<=1018; 2<=≤<=*p*<=≤<=109). The following line contains *n* (*n*<==<=2*m*) integers *e*0[*i*] (1<=≤<=*e*0[*i*]<=≤<=109; 0<=≤<=*i*<=<<=*n*). The next line contains *m*<=+<=1 integers *b*[*i*] (0<=≤<=*b*[*i*]<=≤<=109; 0<=≤<=*i*<=≤<=*m*). | Output *n* lines, the *i*-th line must contain a single integer *e**t*[*i*] modulo *p*. | [
"2 2 10000\n4 1 2 3\n0 1 0\n"
] | [
"14\n6\n6\n14\n"
] | none | [] | 30 | 0 | 0 | 61,861 | |
139 | Wallpaper | [
"implementation",
"math"
] | null | null | Having bought his own apartment, Boris decided to paper the walls in every room. Boris's flat has *n* rooms, each of which has the form of a rectangular parallelepiped. For every room we known its length, width and height of the walls in meters (different rooms can have different dimensions, including height).
Boris chose *m* types of wallpaper to paper the walls of the rooms with (but it is not necessary to use all the types). Each type of wallpaper is sold in rolls of a fixed length and width (the length, naturally, shows how long the unfolded roll will be). In addition, for each type we know the price of one roll of this type.
The wallpaper of each type contains strips running along the length of the roll. When gluing the strips must be located strictly vertically (so the roll cannot be rotated, even if the length is less than the width). Besides, a roll can be cut in an arbitrary manner, but the joints of glued pieces should also be vertical. In addition, each room should be papered by only one type of wallpaper. And pieces of the same roll cannot be used to paper different rooms. That is, for each room the rolls are purchased separately. Also, some rolls can be used not completely.
After buying an apartment Boris is short of cash, so he wants to spend the minimum money on wallpaper. Help him. | The first line contains a positive integer *n* (1<=≤<=*n*<=≤<=500) — the number of rooms in Boris's apartment.
Each of the next *n* lines contains three space-separated positive integers — the length, width and height of the walls in a given room in meters, respectively.
The next line contains a positive integer *m* (1<=≤<=*m*<=≤<=500) — the number of available wallpaper types.
Each of the following *m* lines contains three space-separated positive integers — the length and width in meters of a given wallpaper and the price of one roll, respectively.
All numbers in the input data do not exceed 500. It is guaranteed that each room can be papered using these types of wallpaper. | Print a single number — the minimum total cost of the rolls. | [
"1\n5 5 3\n3\n10 1 100\n15 2 320\n3 19 500\n"
] | [
"640\n"
] | Note to the sample:
The total length of the walls (the perimeter) of the room is 20 m.
One roll of the first type can be cut into pieces to get three vertical 1 meter wide strips, ergo you need 7 rolls of this type, the price equals 700.
A roll of the second type can be cut into pieces to get five 2 meter wide strips, we need 2 rolls, the price is 640.
One roll of the third type can immediately paper 19 meters out of 20, but we cannot use other types and we have to buy a second roll, the price is 1000. | [
{
"input": "1\n5 5 3\n3\n10 1 100\n15 2 320\n3 19 500",
"output": "640"
},
{
"input": "1\n9 10 7\n1\n7 1 3",
"output": "114"
},
{
"input": "1\n6 9 5\n3\n8 5 10\n8 5 2\n6 3 7",
"output": "12"
},
{
"input": "1\n3 3 10\n3\n5 5 1\n9 9 2\n10 1 500",
"output": "6000"
},
{
... | 154 | 2,867,200 | -1 | 61,889 | |
935 | Fafa and Ancient Mathematics | [
"dfs and similar",
"dp",
"trees"
] | null | null | Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression.
An Ahmes arithmetic expression can be defined as:
- "*d*" is an Ahmes arithmetic expression, where *d* is a one-digit positive integer; - "(*E*1<=*op*<=*E*2)" is an Ahmes arithmetic expression, where *E*1 and *E*2 are valid Ahmes arithmetic expressions (without spaces) and *op* is either plus (<=+<=) or minus (<=-<=).
On his trip to Egypt, Fafa found a piece of papyrus paper having one of these Ahmes arithmetic expressions written on it. Being very ancient, the papyrus piece was very worn out. As a result, all the operators were erased, keeping only the numbers and the brackets. Since Fafa loves mathematics, he decided to challenge himself with the following task:
Given the number of plus and minus operators in the original expression, find out the maximum possible value for the expression on the papyrus paper after putting the plus and minus operators in the place of the original erased operators. | The first line contains a string *E* (1<=≤<=|*E*|<=≤<=104) — a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'.
The second line contains two space-separated integers *P* and *M* (0<=≤<=*min*(*P*,<=*M*)<=≤<=100) — the number of plus and minus operators, respectively.
It is guaranteed that *P*<=+<=*M*<==<= the number of erased operators. | Print one line containing the answer to the problem. | [
"(1?1)\n1 0\n",
"(2?(1?2))\n1 1\n",
"((1?(5?7))?((6?2)?7))\n3 2\n",
"((1?(5?7))?((6?2)?7))\n2 3\n"
] | [
"2\n",
"1\n",
"18\n",
"16\n"
] | - The first sample will be (1 + 1) = 2. - The second sample will be (2 + (1 - 2)) = 1. - The third sample will be ((1 - (5 - 7)) + ((6 + 2) + 7)) = 18. - The fourth sample will be ((1 + (5 + 7)) - ((6 - 2) - 7)) = 16. | [
{
"input": "(1?1)\n1 0",
"output": "2"
},
{
"input": "(2?(1?2))\n1 1",
"output": "1"
},
{
"input": "((1?(5?7))?((6?2)?7))\n3 2",
"output": "18"
},
{
"input": "((1?(5?7))?((6?2)?7))\n2 3",
"output": "16"
},
{
"input": "((4?3)?(((2?(4?((4?((2?(3?(7?3)))?(((((3?(6?2)... | 46 | 0 | 0 | 61,901 | |
327 | Axis Walking | [
"bitmasks",
"combinatorics",
"constructive algorithms",
"dp",
"meet-in-the-middle"
] | null | null | Iahub wants to meet his girlfriend Iahubina. They both live in *Ox* axis (the horizontal axis). Iahub lives at point 0 and Iahubina at point *d*.
Iahub has *n* positive integers *a*1, *a*2, ..., *a**n*. The sum of those numbers is *d*. Suppose *p*1, *p*2, ..., *p**n* is a permutation of {1,<=2,<=...,<=*n*}. Then, let *b*1<==<=*a**p*1, *b*2<==<=*a**p*2 and so on. The array b is called a "route". There are *n*! different routes, one for each permutation *p*.
Iahub's travel schedule is: he walks *b*1 steps on *Ox* axis, then he makes a break in point *b*1. Then, he walks *b*2 more steps on *Ox* axis and makes a break in point *b*1<=+<=*b*2. Similarly, at *j*-th (1<=≤<=*j*<=≤<=*n*) time he walks *b**j* more steps on *Ox* axis and makes a break in point *b*1<=+<=*b*2<=+<=...<=+<=*b**j*.
Iahub is very superstitious and has *k* integers which give him bad luck. He calls a route "good" if he never makes a break in a point corresponding to one of those *k* numbers. For his own curiosity, answer how many good routes he can make, modulo 1000000007 (109<=+<=7). | The first line contains an integer *n* (1<=≤<=*n*<=≤<=24). The following line contains *n* integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
The third line contains integer *k* (0<=≤<=*k*<=≤<=2). The fourth line contains *k* positive integers, representing the numbers that give Iahub bad luck. Each of these numbers does not exceed 109. | Output a single integer — the answer of Iahub's dilemma modulo 1000000007 (109<=+<=7). | [
"3\n2 3 5\n2\n5 7\n",
"3\n2 2 2\n2\n1 3\n"
] | [
"1\n",
"6\n"
] | In the first case consider six possible orderings:
- [2, 3, 5]. Iahub will stop at position 2, 5 and 10. Among them, 5 is bad luck for him. - [2, 5, 3]. Iahub will stop at position 2, 7 and 10. Among them, 7 is bad luck for him. - [3, 2, 5]. He will stop at the unlucky 5. - [3, 5, 2]. This is a valid ordering. - [5, 2, 3]. He got unlucky twice (5 and 7). - [5, 3, 2]. Iahub would reject, as it sends him to position 5.
In the second case, note that it is possible that two different ways have the identical set of stopping. In fact, all six possible ways have the same stops: [2, 4, 6], so there's no bad luck for Iahub. | [] | 278 | 10,752,000 | 0 | 61,921 | |
852 | Exploration plan | [
"binary search",
"flows",
"graph matchings",
"shortest paths"
] | null | null | The competitors of Bubble Cup X gathered after the competition and discussed what is the best way to get to know the host country and its cities.
After exploring the map of Serbia for a while, the competitors came up with the following facts: the country has *V* cities which are indexed with numbers from 1 to *V*, and there are *E* bi-directional roads that connect the cites. Each road has a weight (the time needed to cross that road). There are *N* teams at the Bubble Cup and the competitors came up with the following plan: each of the *N* teams will start their journey in one of the *V* cities, and some of the teams share the starting position.
They want to find the shortest time *T*, such that every team can move in these *T* minutes, and the number of different cities they end up in is at least *K* (because they will only get to know the cities they end up in). A team doesn't have to be on the move all the time, if they like it in a particular city, they can stay there and wait for the time to pass.
Please help the competitors to determine the shortest time *T* so it's possible for them to end up in at least *K* different cities or print -1 if that is impossible no matter how they move.
Note that there can exist multiple roads between some cities. | The first line contains four integers: *V*, *E*, *N* and *K* (1<=≤<=<=*V*<=<=≤<=<=600,<= 1<=<=≤<=<=*E*<=<=≤<=<=20000,<= 1<=<=≤<=<=*N*<=<=≤<=<=*min*(*V*,<=200),<= 1<=<=≤<=<=*K*<=<=≤<=<=*N*), number of cities, number of roads, number of teams and the smallest number of different cities they need to end up in, respectively.
The second line contains *N* integers, the cities where the teams start their journey.
Next *E* lines contain information about the roads in following format: *A**i* *B**i* *T**i* (1<=≤<=*A**i*,<=*B**i*<=≤<=*V*,<= 1<=≤<=*T**i*<=≤<=10000), which means that there is a road connecting cities *A**i* and *B**i*, and you need *T**i* minutes to cross that road. | Output a single integer that represents the minimal time the teams can move for, such that they end up in at least *K* different cities or output -1 if there is no solution.
If the solution exists, result will be no greater than 1731311. | [
"6 7 5 4\n5 5 2 2 5\n1 3 3\n1 5 2\n1 6 5\n2 5 4\n2 6 7\n3 4 11\n3 5 3\n"
] | [
"3"
] | Three teams start from city 5, and two teams start from city 2. If they agree to move for 3 minutes, one possible situation would be the following: Two teams in city 2, one team in city 5, one team in city 3 , and one team in city 1. And we see that there are four different cities the teams end their journey at. | [
{
"input": "6 7 5 4\n5 5 2 2 5\n1 3 3\n1 5 2\n1 6 5\n2 5 4\n2 6 7\n3 4 11\n3 5 3",
"output": "3"
},
{
"input": "5 6 4 1\n4 4 4 4\n5 4 40\n3 2 989\n1 2 29\n4 3 18\n2 5 697\n4 3 51",
"output": "0"
},
{
"input": "15 6 9 8\n3 4 2 7 7 4 3 3 2\n13 2 9569\n15 1 8936\n12 5 3842\n13 4 5637\n12 2 ... | 15 | 0 | 0 | 62,015 | |
555 | Case of Chocolate | [
"data structures"
] | null | null | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an *n*<=×<=*n* table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to *n* from left to right and the rows are numbered from top to bottom. Let's call the anti-diagonal to be a diagonal that goes the lower left corner to the upper right corner of the table. First Andrewid eats all the pieces lying below the anti-diagonal. Then he performs the following *q* actions with the remaining triangular part: first, he chooses a piece on the anti-diagonal and either direction 'up' or 'left', and then he begins to eat all the pieces starting from the selected cell, moving in the selected direction until he reaches the already eaten piece or chocolate bar edge.
After each action, he wants to know how many pieces he ate as a result of this action. | The first line contains integers *n* (1<=≤<=*n*<=≤<=109) and *q* (1<=≤<=*q*<=≤<=2·105) — the size of the chocolate bar and the number of actions.
Next *q* lines contain the descriptions of the actions: the *i*-th of them contains numbers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=+<=*y**i*<==<=*n*<=+<=1) — the numbers of the column and row of the chosen cell and the character that represents the direction (L — left, U — up). | Print *q* lines, the *i*-th of them should contain the number of eaten pieces as a result of the *i*-th action. | [
"6 5\n3 4 U\n6 1 L\n2 5 L\n1 6 U\n4 3 U\n",
"10 6\n2 9 U\n10 1 U\n1 10 U\n8 3 L\n10 1 L\n6 5 U\n"
] | [
"4\n3\n2\n1\n2\n",
"9\n1\n10\n6\n0\n2\n"
] | Pictures to the sample tests:
<img class="tex-graphics" src="https://espresso.codeforces.com/2ce2eba5359eb520eb9b09725b638508b03473a8.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers of the action as a result of which these pieces were eaten.
In the second sample test the Andrewid tries to start eating chocolate for the second time during his fifth action, starting from the cell at the intersection of the 10-th column and the 1-st row, but this cell is already empty, so he does not eat anything. | [] | 46 | 0 | -1 | 62,218 | |
217 | Bitonix' Patrol | [
"bitmasks",
"brute force",
"combinatorics",
"dfs and similar",
"math"
] | null | null | Byteland is trying to send a space mission onto the Bit-X planet. Their task is complicated by the fact that the orbit of the planet is regularly patrolled by Captain Bitonix, the leader of the space forces of Bit-X.
There are *n* stations around Bit-X numbered clockwise from 1 to *n*. The stations are evenly placed on a circular orbit, so the stations number *i* and *i*<=+<=1 (1<=≤<=*i*<=<<=*n*), and the stations number 1 and *n*, are neighboring. The distance between every pair of adjacent stations is equal to *m* space miles. To go on a patrol, Captain Bitonix jumps in his rocket at one of the stations and flies in a circle, covering a distance of at least one space mile, before finishing in some (perhaps the starting) station.
Bitonix' rocket moves by burning fuel tanks. After Bitonix attaches an *x*-liter fuel tank and chooses the direction (clockwise or counter-clockwise), the rocket flies exactly *x* space miles along a circular orbit in the chosen direction. Note that the rocket has no brakes; it is not possible for the rocket to stop before depleting a fuel tank.
For example, assume that *n*<==<=3 and *m*<==<=60 and Bitonix has fuel tanks with volumes of 10, 60, 90 and 100 liters. If Bitonix starts from station 1, uses the 100-liter fuel tank to go clockwise, then uses the 90-liter fuel tank to go clockwise, and then uses the 10-liter fuel tank to go counterclockwise, he will finish back at station 1. This constitutes a valid patrol. Note that Bitonix does not have to use all available fuel tanks. Another valid option for Bitonix in this example would be to simply use the 60-liter fuel tank to fly to either station 2 or 3.
However, if *n* was equal to 3, *m* was equal to 60 and the only fuel tanks available to Bitonix were one 10-liter tank and one 100-liter tank, he would have no way of completing a valid patrol (he wouldn't be able to finish any patrol exactly at the station).
The Byteland space agency wants to destroy some of Captain Bitonix' fuel tanks so that he cannot to complete any valid patrol. Find how many different subsets of the tanks the agency can destroy to prevent Captain Bitonix from completing a patrol and output the answer modulo 1000000007 (109<=+<=7). | The first line of the input contains three integers *n* (2<=≤<=*n*<=≤<=1000) — the number of stations, *m* (1<=≤<=*m*<=≤<=120) — the distance between adjacent stations, and *t* (1<=≤<=*t*<=≤<=10000) — the number of fuel tanks owned by Captain Bitonix.
The second line of the input contains *t* space-separated integers between 1 and 109, inclusive — the volumes of Bitonix' fuel tanks. | Output a single number — the number of distinct subsets of tanks that the Bytelandian space agency can destroy in order to prevent Captain Bitonix from completing a patrol, modulo 109<=+<=7. | [
"7 6 5\n5 4 12 6 5\n",
"3 60 2\n10 100\n"
] | [
"6\n",
"4\n"
] | All the fuel tanks are distinct, even if some of them have the same capacity. | [] | 312 | 9,932,800 | 0 | 62,265 | |
758 | Broken Tree | [
"dfs and similar",
"dp",
"graphs",
"greedy",
"trees"
] | null | null | You are given a tree that has *n* vertices, which are numbered from 1 to *n*, where the vertex number one is the root. Each edge has weight *w**i* and strength *p**i*.
Botanist Innokentiy, who is the only member of the jury of the Olympiad in Informatics, doesn't like broken trees.
The tree is broken if there is such an edge the strength of which is less than the sum of weight of subtree's edges to which it leads.
It is allowed to reduce weight of any edge by arbitrary integer value, but then the strength of its edge is reduced by the same value. It means if the weight of the edge is 10, and the strength is 12, then by the reducing the weight by 7 its weight will equal 3, and the strength will equal 5.
It is not allowed to increase the weight of the edge.
Your task is to get the tree, which is not broken, by reducing the weight of edges of the given tree, and also all edged should have the positive weight, moreover, the total weight of all edges should be as large as possible.
It is obvious that the strength of edges can not be negative, however it can equal zero if the weight of the subtree equals zero. | The first line contains the integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of vertices in the tree. The next *n*<=-<=1 lines contains the description of edges. Each line contains four integers *x*, *y*, *w*, *p* (1<=≤<=*x*,<=*y*<=≤<=*n*,<=1<=≤<=*w*<=≤<=109,<=0<=≤<=*p*<=≤<=109), where *x* and *y* — vertices which connect the edge (the vertex number *x* is the parent of the vertex number *y*), *w* and *p* are the weight and the strength of the edge, accordingly. It is guaranteed that the edges describe the tree with the root in the vertex 1. | If it is impossible to get unbroken tree from the given tree, print -1 in the only line.
Otherwise, the output data should contain *n* lines:
In the first line print the number *n* — the number of vertices on the tree.
In the next *n*<=-<=1 lines print the description of edges of the resulting tree. Each line should contain four integers *x*, *y*, *w*, *p* (1<=≤<=*x*,<=*y*<=≤<=*n*,<=1<=≤<=*w*<=≤<=109,<=0<=≤<=*p*<=≤<=109), where *x* and *y* — vertices, which the edge connects (the vertex number *x* is the parent of the vertex number *y*), *w* and *p* are the new weight and the strength of the edge, accordingly.
Print edges in the same order as they are given in input data: the first two integers of each line should not be changed. | [
"3\n1 3 5 7\n3 2 4 3\n",
"4\n1 3 2 3\n3 4 5 1\n3 2 3 3\n",
"5\n1 2 2 4\n2 4 1 9\n4 5 5 6\n4 3 4 8\n",
"7\n1 2 5 2\n2 3 4 3\n1 4 3 7\n4 5 4 1\n4 6 3 2\n6 7 1 6\n"
] | [
"3\n1 3 5 7\n3 2 4 3\n",
"-1",
"5\n1 2 2 4\n2 4 1 9\n4 5 1 2\n4 3 2 6\n",
"7\n1 2 5 2\n2 3 2 1\n1 4 3 7\n4 5 3 0\n4 6 3 2\n6 7 1 6\n"
] | none | [] | 2,261 | 122,265,600 | -1 | 62,395 | |
703 | Mishka and Interesting sum | [
"data structures"
] | null | null | Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers *a*1,<=*a*2,<=...,<=*a**n* of *n* elements!
Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. Right because of that she invited you to visit her and asked you to process *m* queries.
Each query is processed in the following way:
1. Two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) are specified — bounds of query segment. 1. Integers, presented in array segment [*l*,<=<=*r*] (in sequence of integers *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r*) even number of times, are written down. 1. XOR-sum of written down integers is calculated, and this value is the answer for a query. Formally, if integers written down in point 2 are *x*1,<=*x*2,<=...,<=*x**k*, then Mishka wants to know the value , where — operator of exclusive bitwise OR.
Since only the little bears know the definition of array beauty, all you are to do is to answer each of queries presented. | The first line of the input contains single integer *n* (1<=≤<=*n*<=≤<=1<=000<=000) — the number of elements in the array.
The second line of the input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — array elements.
The third line of the input contains single integer *m* (1<=≤<=*m*<=≤<=1<=000<=000) — the number of queries.
Each of the next *m* lines describes corresponding query by a pair of integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) — the bounds of query segment. | Print *m* non-negative integers — the answers for the queries in the order they appear in the input. | [
"3\n3 7 8\n1\n1 3\n",
"7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5\n"
] | [
"0\n",
"0\n3\n1\n3\n2\n"
] | In the second sample:
There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.
In the second query there is only integer 3 is presented even number of times — the answer is 3.
In the third query only integer 1 is written down — the answer is 1.
In the fourth query all array elements are considered. Only 1 and 2 are presented there even number of times. The answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8f3bf9fdacb25bb19b17c017c532cd102cb4993c.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the fifth query 1 and 3 are written down. The answer is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bfacb216edbf76a77a805a5347ff40d88d70f384.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "3\n3 7 8\n1\n1 3",
"output": "0"
},
{
"input": "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5",
"output": "0\n3\n1\n3\n2"
},
{
"input": "10\n1 2 4 1 1 1 1 1 1 4\n55\n5 8\n3 9\n6 8\n4 6\n4 10\n2 8\n1 5\n7 8\n8 9\n7 9\n5 6\n8 10\n9 9\n2 2\n3 3\n3 7\n1 8\n2 3\n4 9\n8 8\n10 10\n1 ... | 0 | 0 | -1 | 62,513 | |
0 | none | [
"none"
] | null | null | Programmer Sasha is a student at MIPT (Moscow Institute of Physics and Technology) and he needs to make a laboratory work to pass his finals.
A laboratory unit is a plane with standard coordinate axes marked on it. Physicists from Moscow Institute of Physics and Technology charged the axes by large electric charges: axis *X* is positive and axis *Y* is negative.
Experienced laboratory worker marked *n* points with integer coordinates (*x**i*,<=*y**i*) on the plane and stopped the time. Sasha should use "atomic tweezers" to place elementary particles in these points. He has an unlimited number of electrons (negatively charged elementary particles) and protons (positively charged elementary particles). He can put either an electron or a proton at each marked point. As soon as all marked points are filled with particles, laboratory worker will turn on the time again and the particles will come in motion and after some time they will stabilize in equilibrium. The objective of the laboratory work is to arrange the particles in such a way, that the diameter of the resulting state (the maximum distance between the pairs of points of the set) is as small as possible.
Since Sasha is a programmer, he naively thinks that all the particles will simply "fall" into their projections on the corresponding axes: electrons will fall on axis *X*, while protons will fall on axis *Y*. As we are programmers too, we will consider the same model as Sasha. That is, a particle gets from point (*x*,<=*y*) to point (*x*,<=0) if it is an electron and to point (0,<=*y*) if it is a proton.
As the laboratory has high background radiation and Sasha takes care of his laptop, he did not take it with him, and now he can't write a program that computes the minimum possible diameter of the resulting set. Therefore, you will have to do it for him.
Print a square of the minimum possible diameter of the set. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of points marked on the plane.
Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=108<=≤<=*x**i*,<=*y**i*<=≤<=108) — the coordinates of the *i*-th point. It is guaranteed that no two points coincide. | Print a single integer — the square of the minimum possible diameter of the set. | [
"3\n1 10\n1 20\n1 30\n",
"2\n1 10\n10 1\n"
] | [
"0\n",
"2\n"
] | In the first sample Sasha puts electrons at all points, all particles eventually fall at a single point (1, 0).
In the second sample Sasha puts an electron at point (1, 10), and a proton at point (10, 1). The result is a set of two points (1, 0) and (0, 1), which has a diameter of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bfe16f27ebc966df6f10ba356a1547b6e7242dd7.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [] | 62 | 0 | 0 | 62,554 | |
843 | Maximum Flow | [
"flows",
"graphs"
] | null | null | You are given a directed graph, consisting of *n* vertices and *m* edges. The vertices *s* and *t* are marked as source and sink correspondingly. Additionally, there are no edges ending at *s* and there are no edges beginning in *t*.
The graph was constructed in a following way: initially each edge had capacity *c**i*<=><=0. A maximum flow with source at *s* and sink at *t* was constructed in this flow network. Let's denote *f**i* as the value of flow passing through edge with index *i*. Next, all capacities *c**i* and flow value *f**i* were erased. Instead, indicators *g**i* were written on edges — if flow value passing through edge *i* was positive, i.e. 1 if *f**i*<=><=0 and 0 otherwise.
Using the graph and values *g**i*, find out what is the minimum possible number of edges in the initial flow network that could be saturated (the passing flow is equal to capacity, i.e. *f**i*<==<=*c**i*). Also construct the corresponding flow network with maximum flow in it.
A flow in directed graph is described by flow values *f**i* on each of the edges so that the following conditions are satisfied:
- for each vertex, except source and sink, total incoming flow and total outcoming flow are equal, - for each edge 0<=≤<=*f**i*<=≤<=*c**i*
A flow is maximum if the difference between the sum of flow values on edges from the source, and the sum of flow values on edges to the source (there are no such in this problem), is maximum possible. | The first line of input data contains four positive integers *n*,<=*m*,<=*s*,<=*t* (2<=≤<=*n*<=≤<=100, 1<=≤<=*m*<=≤<=1000, 1<=≤<=*s*,<=*t*<=≤<=*n*, *s*<=≠<=*t*) — the number of vertices, the number of edges, index of source vertex and index of sink vertex correspondingly.
Each of next *m* lines of input data contain non-negative integers *u**i*, *v**i*, *g**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, ) — the beginning of edge *i*, the end of edge *i* and indicator, which equals to 1 if flow value passing through edge *i* was positive and 0 if not.
It's guaranteed that no edge connects vertex with itself. Also it's guaranteed that there are no more than one edge between each ordered pair of vertices and that there exists at least one network flow that satisfies all the constrains from input data. | In the first line print single non-negative integer *k* — minimum number of edges, which should be saturated in maximum flow.
In each of next *m* lines print two integers *f**i*,<=*c**i* (1<=≤<=*c**i*<=≤<=109, 0<=≤<=*f**i*<=≤<=*c**i*) — the flow value passing through edge *i* and capacity of edge *i*.
This data should form a correct maximum flow in flow network. Also there must be exactly *k* edges with statement *f**i*<==<=*c**i* satisfied. Also statement *f**i*<=><=0 must be true if and only if *g**i*<==<=1.
If there are several possible answers, print any of them. | [
"5 6 1 5\n1 2 1\n2 3 1\n3 5 1\n1 4 1\n4 3 0\n4 5 1\n"
] | [
"2\n3 3\n3 8\n3 4\n4 4\n0 5\n4 9\n"
] | The illustration for second sample case. The saturated edges are marked dark, while edges with *g*<sub class="lower-index">*i*</sub> = 0 are marked with dotted line. The integer on edge is the index of this edge in input list. <img class="tex-graphics" src="https://espresso.codeforces.com/a80cb6067d5d58458863f416f6ac95ad991a308a.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 31 | 0 | -1 | 62,633 | |
396 | On Sum of Number of Inversions in Permutations | [
"combinatorics",
"math"
] | null | null | You are given a permutation *p*. Calculate the total number of inversions in all permutations that lexicographically do not exceed the given one.
As this number can be very large, print it modulo 1000000007 (109<=+<=7). | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=106) — the length of the permutation. The second line contains *n* distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*). | Print a single number — the answer to the problem modulo 1000000007 (109<=+<=7). | [
"2\n2 1\n",
"3\n2 1 3\n"
] | [
"1\n",
"2\n"
] | Permutation *p* of length *n* is the sequence that consists of *n* distinct integers, each of them is from 1 to *n*.
An inversion of permutation *p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">2</sub>, ..., *p*<sub class="lower-index">*n*</sub> is a pair of indexes (*i*, *j*), such that *i* < *j* and *p*<sub class="lower-index">*i*</sub> > *p*<sub class="lower-index">*j*</sub>.
Permutation *a* do not exceed permutation *b* lexicographically, if either *a* = *b* or there exists such number *i*, for which the following logical condition fulfills: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ecb7310db358bbe288e686f1c88c3bb3da967725.png" style="max-width: 100.0%;max-height: 100.0%;"/> AND (*a*<sub class="lower-index">*i*</sub> < *b*<sub class="lower-index">*i*</sub>). | [] | 46 | 4,608,000 | -1 | 62,638 | |
0 | none | [
"none"
] | null | null | Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string *s* initially.
Evolution of the species is described as a sequence of changes in the DNA. Every change is a change of some nucleotide, for example, the following change can happen in DNA strand "AAGC": the second nucleotide can change to "T" so that the resulting DNA strand is "ATGC".
Scientists know that some segments of the DNA strand can be affected by some unknown infections. They can represent an infection as a sequence of nucleotides. Scientists are interested if there are any changes caused by some infections. Thus they sometimes want to know the value of impact of some infection to some segment of the DNA. This value is computed as follows:
- Let the infection be represented as a string *e*, and let scientists be interested in DNA strand segment starting from position *l* to position *r*, inclusive. - Prefix of the string *eee*... (i.e. the string that consists of infinitely many repeats of string *e*) is written under the string *s* from position *l* to position *r*, inclusive. - The value of impact is the number of positions where letter of string *s* coincided with the letter written under it.
Being a developer, Innokenty is interested in bioinformatics also, so the scientists asked him for help. Innokenty is busy preparing VK Cup, so he decided to delegate the problem to the competitors. Help the scientists! | The first line contains the string *s* (1<=≤<=|*s*|<=≤<=105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".
The next line contains single integer *q* (1<=≤<=*q*<=≤<=105) — the number of events.
After that, *q* lines follow, each describes one event. Each of the lines has one of two formats:
- 1 x c, where *x* is an integer (1<=≤<=*x*<=≤<=|*s*|), and *c* is a letter "A", "T", "G" or "C", which means that there is a change in the DNA: the nucleotide at position *x* is now *c*. - 2 l r e, where *l*, *r* are integers (1<=≤<=*l*<=≤<=*r*<=≤<=|*s*|), and *e* is a string of letters "A", "T", "G" and "C" (1<=≤<=|*e*|<=≤<=10), which means that scientists are interested in the value of impact of infection *e* to the segment of DNA strand from position *l* to position *r*, inclusive. | For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA. | [
"ATGCATGC\n4\n2 1 8 ATGC\n2 2 6 TTT\n1 4 T\n2 2 6 TA\n",
"GAGTTGTTAA\n6\n2 3 4 TATGGTG\n1 1 T\n1 6 G\n2 5 9 AGTAATA\n1 10 G\n2 2 6 TTGT\n"
] | [
"8\n2\n4\n",
"0\n3\n1\n"
] | Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matches. | [] | 46 | 0 | 0 | 62,643 | |
917 | MADMAX | [
"dfs and similar",
"dp",
"games",
"graphs"
] | null | null | As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with *n* vertices and *m* edges. There's a character written on each edge, a lowercase English letter.
Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex *v* to vertex *u* if there's an outgoing edge from *v* to *u*). If the player moves his/her marble from vertex *v* to vertex *u*, the "character" of that round is the character written on the edge from *v* to *u*. There's one additional rule; the ASCII code of character of round *i* should be greater than or equal to the ASCII code of character of round *i*<=-<=1 (for *i*<=><=1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can't make a move loses the game. The marbles may be at the same vertex at the same time.
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don't have time to play. So they asked you, if they both play optimally, who wins the game?
You have to determine the winner of the game for all initial positions of the marbles. | The first line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=100, ).
The next *m* lines contain the edges. Each line contains two integers *v*, *u* and a lowercase English letter *c*, meaning there's an edge from *v* to *u* written *c* on it (1<=≤<=*v*,<=*u*<=≤<=*n*, *v*<=≠<=*u*). There's at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic. | Print *n* lines, a string of length *n* in each one. The *j*-th character in *i*-th line should be 'A' if Max will win the game in case her marble is initially at vertex *i* and Lucas's marble is initially at vertex *j*, and 'B' otherwise. | [
"4 4\n1 2 b\n1 3 a\n2 4 c\n3 4 b\n",
"5 8\n5 3 h\n1 2 c\n3 1 c\n3 2 r\n5 1 r\n4 3 z\n5 4 r\n5 2 h\n"
] | [
"BAAA\nABAA\nBBBA\nBBBB\n",
"BABBB\nBBBBB\nAABBB\nAAABA\nAAAAB\n"
] | Here's the graph in the first sample test case:
Here's the graph in the second sample test case: | [
{
"input": "4 4\n1 2 b\n1 3 a\n2 4 c\n3 4 b",
"output": "BAAA\nABAA\nBBBA\nBBBB"
},
{
"input": "5 8\n5 3 h\n1 2 c\n3 1 c\n3 2 r\n5 1 r\n4 3 z\n5 4 r\n5 2 h",
"output": "BABBB\nBBBBB\nAABBB\nAAABA\nAAAAB"
},
{
"input": "2 1\n1 2 q",
"output": "BA\nBB"
},
{
"input": "8 20\n2 4 ... | 530 | 2,355,200 | 3 | 62,702 | |
632 | Longest Subsequence | [
"brute force",
"math",
"number theory"
] | null | null | You are given array *a* with *n* elements and the number *m*. Consider some subsequence of *a* and the value of least common multiple (LCM) of its elements. Denote LCM as *l*. Find any longest subsequence of *a* with the value *l*<=≤<=*m*.
A subsequence of *a* is an array we can get by erasing some elements of *a*. It is allowed to erase zero or all elements.
The LCM of an empty array equals 1. | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106) — the size of the array *a* and the parameter from the problem statement.
The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=109) — the elements of *a*. | In the first line print two integers *l* and *k**max* (1<=≤<=*l*<=≤<=*m*,<=0<=≤<=*k**max*<=≤<=*n*) — the value of LCM and the number of elements in optimal subsequence.
In the second line print *k**max* integers — the positions of the elements from the optimal subsequence in the ascending order.
Note that you can find and print any subsequence with the maximum length. | [
"7 8\n6 2 9 2 7 2 3\n",
"6 4\n2 2 2 3 3 3\n"
] | [
"6 5\n1 2 4 6 7\n",
"2 3\n1 2 3\n"
] | none | [
{
"input": "7 8\n6 2 9 2 7 2 3",
"output": "6 5\n1 2 4 6 7"
},
{
"input": "6 4\n2 2 2 3 3 3",
"output": "2 3\n1 2 3"
},
{
"input": "10 50\n39 22 60 88 11 65 41 85 65 100",
"output": "22 2\n2 5"
},
{
"input": "100 343\n999 284 486 785 176 742 856 415 992 601 600 122 460 214 33... | 217 | 22,220,800 | -1 | 62,710 | |
261 | Maxim and Increasing Subsequence | [
"dp"
] | null | null | Maxim loves sequences, especially those that strictly increase. He is wondering, what is the length of the longest increasing subsequence of the given sequence *a*?
Sequence *a* is given as follows:
- the length of the sequence equals *n*<=×<=*t*; - (1<=≤<=*i*<=≤<=*n*<=×<=*t*), where operation means taking the remainder after dividing number *x* by number *y*.
Sequence *s*1,<=<=*s*2,<=<=...,<=<=*s**r* of length *r* is a subsequence of sequence *a*1,<=<=*a*2,<=<=...,<=<=*a**n*, if there is such increasing sequence of indexes *i*1,<=*i*2,<=...,<=*i**r* (1<=<=≤<=<=*i*1<=<=<<=<=*i*2<=<=<<=... <=<=<<=<=*i**r*<=<=≤<=<=*n*), that *a**i**j*<=<==<=<=*s**j*. In other words, the subsequence can be obtained from the sequence by crossing out some elements.
Sequence *s*1,<=<=*s*2,<=<=...,<=<=*s**r* is increasing, if the following inequality holds: *s*1<=<<=*s*2<=<<=<=...<=<<=<=*s**r*.
Maxim have *k* variants of the sequence *a*. Help Maxim to determine for each sequence the length of the longest increasing subsequence. | The first line contains four integers *k*, *n*, *maxb* and *t* (1<=≤<=*k*<=≤<=10; 1<=≤<=*n*,<=*maxb*<=≤<=105; 1<=≤<=*t*<=≤<=109; *n*<=×<=*maxb*<=≤<=2·107). Each of the next *k* lines contain *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≤<=*b**i*<=≤<=*maxb*).
Note that for each variant of the sequence *a* the values *n*, *maxb* and *t* coincide, the only arrays *b*s differ.
The numbers in the lines are separated by single spaces. | Print *k* integers — the answers for the variants of the sequence *a*. Print the answers in the order the variants follow in the input. | [
"3 3 5 2\n3 2 1\n1 2 3\n2 3 1\n"
] | [
"2\n3\n3\n"
] | none | [] | 60 | 0 | 0 | 62,842 | |
208 | Solitaire | [
"dfs and similar",
"dp"
] | null | null | A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules:
- A deck of *n* cards is carefully shuffled, then all *n* cards are put on the table in a line from left to right; - Before each move the table has several piles of cards lying in a line (initially there are *n* piles, each pile has one card). Let's number the piles from left to right, from 1 to *x*. During one move, a player can take the whole pile with the maximum number *x* (that is the rightmost of remaining) and put it on the top of pile *x*<=-<=1 (if it exists) or on the top of pile *x*<=-<=3 (if it exists). The player can put one pile on top of another one only if the piles' top cards have the same suits or values. Please note that if pile *x* goes on top of pile *y*, then the top card of pile *x* becomes the top card of the resulting pile. Also note that each move decreases the total number of piles by 1; - The solitaire is considered completed if all cards are in the same pile.
Vasya has already shuffled the cards and put them on the table, help him understand whether completing this solitaire is possible or not. | The first input line contains a single integer *n* (1<=≤<=*n*<=≤<=52) — the number of cards in Vasya's deck. The next line contains *n* space-separated strings *c*1,<=*c*2,<=...,<=*c**n*, where string *c**i* describes the *i*-th card on the table. Each string *c**i* consists of exactly two characters, the first one represents the card's value, the second one represents its suit. Cards on the table are numbered from left to right.
A card's value is specified by one of these characters: "2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A". A card's suit is specified by one of these characters: "S", "D", "H", "C".
It is not guaranteed that the deck has all possible cards. Also, the cards in Vasya's deck can repeat. | On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. | [
"4\n2S 2S 2C 2C\n",
"2\n3S 2C\n"
] | [
"YES\n",
"NO\n"
] | In the first sample you can act like that:
- put the 4-th pile on the 1-st one; - put the 3-rd pile on the 2-nd one; - put the 2-nd pile on the 1-st one.
In the second sample there is no way to complete the solitaire. | [
{
"input": "4\n2S 2S 2C 2C",
"output": "YES"
},
{
"input": "2\n3S 2C",
"output": "NO"
},
{
"input": "5\n2S 2S 4S 3S 2S",
"output": "YES"
},
{
"input": "5\n5S 5S 7S 4S 3H",
"output": "NO"
},
{
"input": "5\n7S 7S 4S 8H 4H",
"output": "NO"
},
{
"input": "... | 46 | 409,600 | 3 | 62,863 | |
0 | none | [
"none"
] | null | null | User ainta likes trees. This time he is going to make an undirected tree with *n* vertices numbered by integers from 1 to *n*. The tree is weighted, so each edge of the tree will have some integer weight.
Also he has an array *t*: *t*[1],<=*t*[2],<=...,<=*t*[*n*]. At first all the elements of the array are initialized to 0. Then for each edge connecting vertices *u* and *v* (*u*<=<<=*v*) of the tree with weight *c*, ainta adds value *c* to the elements *t*[*u*],<=*t*[*u*<=+<=1],<=...,<=*t*[*v*<=-<=1],<=*t*[*v*] of array *t*.
Let's assume that *d*(*u*,<=*v*) is the total weight of edges on the shortest path between vertex *u* and vertex *v*. User ainta calls a pair of integers *x*,<=*y* (1<=≤<=*x*<=<<=*y*<=≤<=*n*) good if and only if *d*(*x*,<=*y*)<==<=*t*[*x*]<=+<=*t*[*x*<=+<=1]<=+<=...<=+<=*t*[*y*<=-<=1]<=+<=*t*[*y*].
User ainta wants to make at least good pairs, but he couldn't make a proper tree. Help ainta to find such a tree. | The first line contains a single integer *n* (5<=≤<=*n*<=≤<=105). | Print *n*<=-<=1 lines containing the description of the edges. The *i*-th line should contain three space-separated integers *u**i*,<=*v**i*,<=*c**i* (1<=≤<=*u**i*<=<<=*v**i*<=≤<=*n*; 1<=≤<=*c**i*<=≤<=105) — two vertices connected by the edge, and the weight of the edge.
Next print lines containing the good pairs. The *k*-th line should contain two space-separated integers *x**k* and *y**k* (1<=≤<=*x**k*<=<<=*y**k*<=≤<=*n*). Of course, *x**k*,<=*y**k* must be a good pair. All pairs should be distinct — that is, for all *j*,<=*k* , *x**j*<=≠<=*x**k* or *y**j*<=≠<=*y**k* must be satisfied.
If there are many correct solutions, print any of them. | [
"7"
] | [
"1 4 1\n1 2 2\n2 3 5\n3 5 3\n2 6 2\n6 7 3\n4 5\n5 6\n5 7"
] | ⌊*x*⌋ is the largest integer not greater than *x*.
You can find the definition of a tree by the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory)
You can also find the definition of the shortest path by the following link: http://en.wikipedia.org/wiki/Shortest_path_problem
The tree and the array *t* in the sample output look like this: | [] | 30 | 0 | 0 | 62,934 | |
123 | Squares | [
"math"
] | null | null | You are given an infinite checkered field. You should get from a square (*x*1; *y*1) to a square (*x*2; *y*2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any square, you can move to any other side-neighboring one.
A square (*x*; *y*) is considered bad, if at least one of the two conditions is fulfilled:
- |*x*<=+<=*y*|<=≡<=0 (*mod* 2*a*),- |*x*<=-<=*y*|<=≡<=0 (*mod* 2*b*).
Your task is to find the minimum number of bad cells one will have to visit on the way from (*x*1; *y*1) to (*x*2; *y*2). | The only line contains integers *a*, *b*, *x*1, *y*1, *x*2 and *y*2 — the parameters of the bad squares, the coordinates of the initial and the final squares correspondingly (2<=≤<=*a*,<=*b*<=≤<=109 and |*x*1|,|*y*1|,|*x*2|,|*y*2|<=≤<=109). It is guaranteed that the initial and the final square aren't bad. | Print a single number — the minimum number of bad cells that one will have to visit in order to travel from square (*x*1; *y*1) to square (*x*2; *y*2). | [
"2 2 1 0 0 1\n",
"2 2 10 11 0 1\n",
"2 4 3 -1 3 7\n"
] | [
"1\n",
"5\n",
"2\n"
] | In the third sample one of the possible paths in (3;-1)->(3;0)->(3;1)->(3;2)->(4;2)->(4;3)->(4;4)->(4;5)->(4;6)->(4;7)->(3;7). Squares (3;1) and (4;4) are bad. | [
{
"input": "2 2 1 0 0 1",
"output": "1"
},
{
"input": "2 2 10 11 0 1",
"output": "5"
},
{
"input": "2 4 3 -1 3 7",
"output": "2"
},
{
"input": "2 2 9 10 -10 -11",
"output": "10"
},
{
"input": "3 2 -11 -10 10 11",
"output": "7"
},
{
"input": "3 2 11 -12... | 154 | 0 | 0 | 63,004 | |
466 | Wonder Room | [
"brute force",
"math"
] | null | null | The start of the new academic year brought about the problem of accommodation students into dormitories. One of such dormitories has a *a*<=×<=*b* square meter wonder room. The caretaker wants to accommodate exactly *n* students there. But the law says that there must be at least 6 square meters per student in a room (that is, the room for *n* students must have the area of at least 6*n* square meters). The caretaker can enlarge any (possibly both) side of the room by an arbitrary positive integer of meters. Help him change the room so as all *n* students could live in it and the total area of the room was as small as possible. | The first line contains three space-separated integers *n*, *a* and *b* (1<=≤<=*n*,<=*a*,<=*b*<=≤<=109) — the number of students and the sizes of the room. | Print three integers *s*, *a*1 and *b*1 (*a*<=≤<=*a*1; *b*<=≤<=*b*1) — the final area of the room and its sizes. If there are multiple optimal solutions, print any of them. | [
"3 3 5\n",
"2 4 4\n"
] | [
"18\n3 6\n",
"16\n4 4\n"
] | none | [
{
"input": "3 3 5",
"output": "18\n3 6"
},
{
"input": "2 4 4",
"output": "16\n4 4"
},
{
"input": "1 1 1",
"output": "6\n1 6"
},
{
"input": "1 1000000000 1000000000",
"output": "1000000000000000000\n1000000000 1000000000"
},
{
"input": "8 7 5",
"output": "48\n8... | 46 | 0 | 0 | 63,014 | |
617 | Chocolate | [
"combinatorics"
] | null | null | Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces.
You are asked to calculate the number of ways he can do it. Two ways to break chocolate are considered distinct if one of them contains a break between some two adjacent pieces and the other one doesn't.
Please note, that if Bob doesn't make any breaks, all the bar will form one piece and it still has to have exactly one nut. | The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of pieces in the chocolate bar.
The second line contains *n* integers *a**i* (0<=≤<=*a**i*<=≤<=1), where 0 represents a piece without the nut and 1 stands for a piece with the nut. | Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut. | [
"3\n0 1 0\n",
"5\n1 0 1 0 1\n"
] | [
"1\n",
"4\n"
] | In the first sample there is exactly one nut, so the number of ways equals 1 — Bob shouldn't make any breaks.
In the second sample you can break the bar in four ways:
10|10|1
1|010|1
10|1|01
1|01|01 | [
{
"input": "3\n0 1 0",
"output": "1"
},
{
"input": "5\n1 0 1 0 1",
"output": "4"
},
{
"input": "10\n0 0 1 0 0 0 1 1 0 1",
"output": "8"
},
{
"input": "20\n0 0 0 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 0",
"output": "24"
},
{
"input": "50\n0 1 1 1 1 1 1 0 1 1 0 1 1 1 0 0 1... | 77 | 2,150,400 | -1 | 63,080 | |
329 | Evil | [
"math"
] | null | null | There are *n* cities on a two dimensional Cartesian plane. The distance between two cities is equal to the Manhattan distance between them (see the Notes for definition). A Hamiltonian cycle of the cities is defined as a permutation of all *n* cities. The length of this Hamiltonian cycle is defined as the sum of the distances between adjacent cities in the permutation plus the distance between the first and final city in the permutation. Please compute the longest possible length of a Hamiltonian cycle of the given cities. | The first line contains an integer *n* (3<=≤<=*n*<=≤<=105). Then *n* lines follow, each consisting of two integers *x**i* and *y**i* (0<=≤<=*x**i*,<=*y**i*<=≤<=109), denoting the coordinates of a city. All given points will be distinct. | A single line denoting the longest possible length of a Hamiltonian cycle of the given cities. You should not output the cycle, only its length.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"4\n1 1\n1 2\n2 1\n2 2\n"
] | [
"6\n"
] | In the example, one of the possible Hamiltonian cycles with length 6 is (1, 1) (1, 2) (2, 1) (2, 2). There does not exist any other Hamiltonian cycle with a length greater than 6.
The Manhattan distance between two cities (*x*<sub class="lower-index">*i*</sub>, *y*<sub class="lower-index">*i*</sub>) and (*x*<sub class="lower-index">*j*</sub>, *y*<sub class="lower-index">*j*</sub>) is |*x*<sub class="lower-index">*i*</sub> - *x*<sub class="lower-index">*j*</sub>| + |*y*<sub class="lower-index">*i*</sub> - *y*<sub class="lower-index">*j*</sub>|. | [] | 3,000 | 67,379,200 | 0 | 63,176 | |
293 | Close Vertices | [
"data structures",
"divide and conquer",
"trees"
] | null | null | You've got a weighted tree, consisting of *n* vertices. Each edge has a non-negative weight. The length of the path between any two vertices of the tree is the number of edges in the path. The weight of the path is the total weight of all edges it contains.
Two vertices are close if there exists a path of length at most *l* between them and a path of weight at most *w* between them. Count the number of pairs of vertices *v*,<=*u* (*v*<=<<=*u*), such that vertices *v* and *u* are close. | The first line contains three integers *n*, *l* and *w* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*l*<=≤<=*n*,<=0<=≤<=*w*<=≤<=109). The next *n*<=-<=1 lines contain the descriptions of the tree edges. The *i*-th line contains two integers *p**i*,<=*w**i* (1<=≤<=*p**i*<=<<=(*i*<=+<=1),<=0<=≤<=*w**i*<=≤<=104), that mean that the *i*-th edge connects vertex (*i*<=+<=1) and *p**i* and has weight *w**i*.
Consider the tree vertices indexed from 1 to *n* in some way. | Print a single integer — the number of close pairs.
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. | [
"4 4 6\n1 3\n1 4\n1 3\n",
"6 2 17\n1 3\n2 5\n2 13\n1 6\n5 9\n"
] | [
"4\n",
"9\n"
] | none | [] | 60 | 0 | 0 | 63,183 | |
796 | Exam Cheating | [
"binary search",
"dp"
] | null | null | Zane and Zane's crush have just decided to date! However, the girl is having a problem with her Physics final exam, and needs your help.
There are *n* questions, numbered from 1 to *n*. Question *i* comes before question *i*<=+<=1 (1<=≤<=*i*<=<<=*n*). Each of the questions cannot be guessed on, due to the huge penalty for wrong answers. The girl luckily sits in the middle of two geniuses, so she is going to cheat.
However, the geniuses have limitations. Each of them may or may not know the answers to some questions. Anyway, it is safe to assume that the answers on their answer sheets are absolutely correct.
To make sure she will not get caught by the proctor, the girl will glance at most *p* times, each time looking at no more than *k* consecutive questions on one of the two geniuses' answer sheet. When the girl looks at some question on an answer sheet, she copies the answer to that question if it is on that answer sheet, or does nothing otherwise.
Help the girl find the maximum number of questions she can get correct. | The first line contains three integers *n*, *p*, and *k* (1<=≤<=*n*,<=*p*<=≤<=1,<=000, 1<=≤<=*k*<=≤<=*min*(*n*,<=50)) — the number of questions, the maximum number of times the girl can glance, and the maximum number of consecutive questions that can be looked at in one time glancing, respectively.
The second line starts with one integer *r* (0<=≤<=*r*<=≤<=*n*), denoting the number of questions the first genius has answered on his answer sheet. Then follow *r* integers *a*1,<=*a*2,<=...,<=*a**r* (1<=≤<=*a**i*<=≤<=*n*) — the answered questions, given in a strictly-increasing order (that is, *a**i*<=<<=*a**i*<=+<=1).
The third line starts with one integer *s* (0<=≤<=*s*<=≤<=*n*), denoting the number of questions the second genius has answered on his answer sheet. Then follow *s* integers *b*1,<=*b*2,<=...,<=*b**s* (1<=≤<=*b**i*<=≤<=*n*) — the answered questions, given in a strictly-increasing order (that is, *b**i*<=<<=*b**i*<=+<=1). | Print one integer — the maximum number of questions the girl can answer correctly. | [
"6 2 3\n3 1 3 6\n4 1 2 5 6\n",
"8 3 3\n4 1 3 5 6\n5 2 4 6 7 8\n"
] | [
"4",
"7"
] | Let (*x*, *l*, *r*) denote the action of looking at all questions *i* such that *l* ≤ *i* ≤ *r* on the answer sheet of the *x*-th genius.
In the first sample, the girl could get 4 questions correct by performing sequence of actions (1, 1, 3) and (2, 5, 6).
In the second sample, the girl could perform sequence of actions (1, 3, 5), (2, 2, 4), and (2, 6, 8) to get 7 questions correct. | [] | 62 | 716,800 | 0 | 63,418 | |
140 | New Year Contest | [
"greedy",
"sortings"
] | null | null | As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are *n* problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the *i*-th problem will take *a**i* minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the *i*-th problem always equals *a**i* minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of the problems. The next line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=720) — each number shows how much time in minutes Gennady will spend writing a solution to the problem. | Print two integers — the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy. | [
"3\n30 330 720\n"
] | [
"2 10\n"
] | In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
Competitions by the given rules are held annually on the site http://b23.ru/3wvc | [
{
"input": "3\n30 330 720",
"output": "2 10"
},
{
"input": "1\n720",
"output": "0 0"
},
{
"input": "5\n100 200 300 400 500",
"output": "3 250"
},
{
"input": "7\n120 110 100 110 120 120 50",
"output": "6 420"
},
{
"input": "3\n350 340 360",
"output": "2 340"
... | 124 | 307,200 | 3 | 63,588 | |
297 | Color the Carpet | [
"constructive algorithms"
] | null | null | Even polar bears feel cold when lying on the ice. Therefore, a polar bear Alice is going to make a carpet. The carpet can be viewed as a grid with height *h* and width *w*. Then the grid is divided into *h*<=×<=*w* squares. Alice is going to assign one of *k* different colors to each square. The colors are numbered from 1 to *k*. She may choose not to use all of the colors.
However, there are some restrictions. For every two adjacent squares (squares that shares an edge) *x* and *y*, there is a color constraint in one of the forms:
- *color*(*x*)<==<=*color*(*y*), or - *color*(*x*)<=≠<=*color*(*y*).
Example of the color constraints:
Ideally, Alice wants to satisfy all color constraints. But again, life in the Arctic is hard. It is not always possible to satisfy all color constraints. Fortunately, she will still be happy if at least of the color constraints are satisfied.
If she has 4 colors she can color the carpet in the following way:
And she is happy because of the color constraints are satisfied, and . Your task is to help her color the carpet. | The first line contains three integers *h*,<=*w*,<=*k* (2<=≤<=*h*,<=*w*<=≤<=1000,<=1<=≤<=*k*<=≤<=*w*·*h*).
The next 2*h*<=-<=1 lines describe the color constraints from top to bottom, left to right. They contain *w*<=-<=1,<=*w*,<=*w*<=-<=1,<=*w*,<=...,<=*w*<=-<=1 characters respectively. Each color constraint is represented by a character "E" or "N", where "E" means "<==<=" and "N" means "<=≠<=".
The color constraints listed in the order they are depicted on the picture. | If there is a coloring that satisfies at least of the color constraints, print "YES" (without quotes) in the first line. In each of the next *h* lines, print *w* integers describing the coloring.
Otherwise, print "NO" (without quotes). | [
"3 4 4\nENE\nNNEE\nNEE\nENEN\nENN\n"
] | [
"YES\n1 1 2 2\n3 4 1 1\n3 3 2 4"
] | none | [] | 154 | 307,200 | 0 | 63,902 | |
226 | The table | [
"constructive algorithms",
"greedy"
] | null | null | Harry Potter has a difficult homework. Given a rectangular table, consisting of *n*<=×<=*m* cells. Each cell of the table contains the integer. Harry knows how to use two spells: the first spell change the sign of the integers in the selected row, the second — in the selected column. Harry's task is to make non-negative the sum of the numbers in each row and each column using these spells.
Alone, the boy can not cope. Help the young magician! | The first line contains two integers *n* and *m* (1<=≤<=*n*,<= *m*<=≤<=100) — the number of rows and the number of columns.
Next *n* lines follow, each contains *m* integers: *j*-th integer in the *i*-th line is *a**i*,<=*j* (|*a**i*,<=*j*|<=≤<=100), the number in the *i*-th row and *j*-th column of the table.
The rows of the table numbered from 1 to *n*. The columns of the table numbered from 1 to *m*. | In the first line print the number *a* — the number of required applications of the first spell. Next print *a* space-separated integers — the row numbers, you want to apply a spell. These row numbers must be distinct!
In the second line print the number *b* — the number of required applications of the second spell. Next print *b* space-separated integers — the column numbers, you want to apply a spell. These column numbers must be distinct!
If there are several solutions are allowed to print any of them. | [
"4 1\n-1\n-1\n-1\n-1\n",
"2 4\n-1 -1 -1 2\n1 1 1 1\n"
] | [
"4 1 2 3 4 \n0 \n",
"1 1 \n1 4 \n"
] | none | [
{
"input": "4 1\n-1\n-1\n-1\n-1",
"output": "4 1 2 3 4 \n0 "
},
{
"input": "2 4\n-1 -1 -1 2\n1 1 1 1",
"output": "1 1 \n1 4 "
},
{
"input": "10 5\n1 7 1 6 -3\n8 -8 0 -7 -8\n7 -10 -8 -3 6\n-3 0 -9 0 -3\n-1 5 -2 -9 10\n-2 9 2 0 7\n5 0 -1 -10 6\n7 -8 -3 -9 1\n-5 10 -10 5 9\n-7 4 -8 0 -4",
... | 0 | 0 | -1 | 63,984 | |
546 | Soldier and Number Game | [
"constructive algorithms",
"dp",
"math",
"number theory"
] | null | null | Two soldiers are playing a game. At the beginning first of them chooses a positive integer *n* and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer *x*<=><=1, such that *n* is divisible by *x* and replacing *n* with *n*<=/<=*x*. When *n* becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.
To make the game more interesting, first soldier chooses *n* of form *a*!<=/<=*b*! for some positive integer *a* and *b* (*a*<=≥<=*b*). Here by *k*! we denote the factorial of *k* that is defined as a product of all positive integers not large than *k*.
What is the maximum possible score of the second soldier? | First line of input consists of single integer *t* (1<=≤<=*t*<=≤<=1<=000<=000) denoting number of games soldiers play.
Then follow *t* lines, each contains pair of integers *a* and *b* (1<=≤<=*b*<=≤<=*a*<=≤<=5<=000<=000) defining the value of *n* for a game. | For each game output a maximum score that the second soldier can get. | [
"2\n3 1\n6 3\n"
] | [
"2\n5\n"
] | none | [
{
"input": "2\n3 1\n6 3",
"output": "2\n5"
},
{
"input": "8\n7 1\n263 262\n1000003 1000002\n5000000 4999995\n2 1\n7 4\n12345 12345\n10 1",
"output": "8\n1\n1\n23\n1\n4\n0\n15"
},
{
"input": "3\n1 1\n5000000 1\n5000000 5000000",
"output": "0\n18703742\n0"
}
] | 3,000 | 42,086,400 | 0 | 64,107 | |
576 | Invariance of Tree | [
"constructive algorithms",
"dfs and similar",
"greedy",
"trees"
] | null | null | A tree of size *n* is an undirected connected graph consisting of *n* vertices without cycles.
Consider some tree with *n* vertices. We call a tree invariant relative to permutation *p*<==<=*p*1*p*2... *p**n*, if for any two vertices of the tree *u* and *v* the condition holds: "vertices *u* and *v* are connected by an edge if and only if vertices *p**u* and *p**v* are connected by an edge".
You are given permutation *p* of size *n*. Find some tree size *n*, invariant relative to the given permutation. | The first line contains number *n* (1<=≤<=*n*<=≤<=105) — the size of the permutation (also equal to the size of the sought tree).
The second line contains permutation *p**i* (1<=≤<=*p**i*<=≤<=*n*). | If the sought tree does not exist, print "NO" (without the quotes).
Otherwise, print "YES", and then print *n*<=-<=1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter.
If there are multiple solutions, output any of them. | [
"4\n4 3 2 1\n",
"3\n3 1 2\n"
] | [
"YES\n4 1\n4 2\n1 3\n",
"NO\n"
] | In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.
It can be shown that in the second sample test no tree satisfies the given condition. | [
{
"input": "4\n4 3 2 1",
"output": "YES\n4 1\n4 2\n1 3"
},
{
"input": "3\n3 1 2",
"output": "NO"
},
{
"input": "3\n3 2 1",
"output": "YES\n2 1\n2 3"
},
{
"input": "4\n3 4 1 2",
"output": "YES\n4 2\n4 1\n2 3"
},
{
"input": "5\n5 3 2 1 4",
"output": "NO"
},
... | 46 | 512,000 | 0 | 64,177 | |
622 | Ants in Leaves | [
"dfs and similar",
"greedy",
"sortings",
"trees"
] | null | null | Tree is a connected graph without cycles. A leaf of a tree is any vertex connected with exactly one other vertex.
You are given a tree with *n* vertices and a root in the vertex 1. There is an ant in each leaf of the tree. In one second some ants can simultaneously go to the parent vertex from the vertex they were in. No two ants can be in the same vertex simultaneously except for the root of the tree.
Find the minimal time required for all ants to be in the root of the tree. Note that at start the ants are only in the leaves of the tree. | The first line contains integer *n* (2<=≤<=*n*<=≤<=5·105) — the number of vertices in the tree.
Each of the next *n*<=-<=1 lines contains two integers *x**i*,<=*y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*) — the ends of the *i*-th edge. It is guaranteed that you are given the correct undirected tree. | Print the only integer *t* — the minimal time required for all ants to be in the root of the tree. | [
"12\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n3 9\n8 10\n8 11\n8 12\n",
"2\n2 1\n"
] | [
"6\n",
"1\n"
] | none | [
{
"input": "12\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n3 9\n8 10\n8 11\n8 12",
"output": "6"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "10\n4 10\n6 10\n10 5\n10 7\n8 10\n4 2\n9 10\n4 1\n3 10",
"output": "8"
},
{
"input":... | 77 | 7,168,000 | 0 | 64,423 | |
438 | The Child and Binary Tree | [
"combinatorics",
"divide and conquer",
"fft",
"number theory"
] | null | null | Our child likes computer science very much, especially he likes binary trees.
Consider the sequence of *n* distinct positive integers: *c*1,<=*c*2,<=...,<=*c**n*. The child calls a vertex-weighted rooted binary tree good if and only if for every vertex *v*, the weight of *v* is in the set {*c*1,<=*c*2,<=...,<=*c**n*}. Also our child thinks that the weight of a vertex-weighted tree is the sum of all vertices' weights.
Given an integer *m*, can you for all *s* (1<=≤<=*s*<=≤<=*m*) calculate the number of good vertex-weighted rooted binary trees with weight *s*? Please, check the samples for better understanding what trees are considered different.
We only want to know the answer modulo 998244353 (7<=×<=17<=×<=223<=+<=1, a prime number). | The first line contains two integers *n*,<=*m* (1<=≤<=*n*<=≤<=105; 1<=≤<=*m*<=≤<=105). The second line contains *n* space-separated pairwise distinct integers *c*1,<=*c*2,<=...,<=*c**n*. (1<=≤<=*c**i*<=≤<=105). | Print *m* lines, each line containing a single integer. The *i*-th line must contain the number of good vertex-weighted rooted binary trees whose weight exactly equal to *i*. Print the answers modulo 998244353 (7<=×<=17<=×<=223<=+<=1, a prime number). | [
"2 3\n1 2\n",
"3 10\n9 4 3\n",
"5 10\n13 10 6 4 15\n"
] | [
"1\n3\n9\n",
"0\n0\n1\n1\n0\n2\n4\n2\n6\n15\n",
"0\n0\n0\n1\n0\n1\n0\n2\n0\n5\n"
] | In the first example, there are 9 good vertex-weighted rooted binary trees whose weight exactly equal to 3: | [
{
"input": "2 3\n1 2",
"output": "1\n3\n9"
},
{
"input": "3 10\n9 4 3",
"output": "0\n0\n1\n1\n0\n2\n4\n2\n6\n15"
},
{
"input": "5 10\n13 10 6 4 15",
"output": "0\n0\n0\n1\n0\n1\n0\n2\n0\n5"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "10 100\n9 95 37 56 3... | 389 | 29,286,400 | 3 | 64,451 | |
847 | Travel Cards | [
"greedy",
"implementation",
"sortings"
] | null | null | In the evening Polycarp decided to analyze his today's travel expenses on public transport.
The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop to the other and back. There is at most one bus running between a pair of stops.
Polycarp made *n* trips on buses. About each trip the stop where he started the trip and the the stop where he finished are known. The trips follow in the chronological order in Polycarp's notes.
It is known that one trip on any bus costs *a* burles. In case when passenger makes a transshipment the cost of trip decreases to *b* burles (*b*<=<<=*a*). A passenger makes a transshipment if the stop on which he boards the bus coincides with the stop where he left the previous bus. Obviously, the first trip can not be made with transshipment.
For example, if Polycarp made three consecutive trips: "BerBank" "University", "University" "BerMall", "University" "BerBank", then he payed *a*<=+<=*b*<=+<=*a*<==<=2*a*<=+<=*b* burles. From the BerBank he arrived to the University, where he made transshipment to the other bus and departed to the BerMall. Then he walked to the University and returned to the BerBank by bus.
Also Polycarp can buy no more than *k* travel cards. Each travel card costs *f* burles. The travel card for a single bus route makes free of charge any trip by this route (in both directions). Once purchased, a travel card can be used any number of times in any direction.
What is the smallest amount of money Polycarp could have spent today if he can buy no more than *k* travel cards? | The first line contains five integers *n*,<=*a*,<=*b*,<=*k*,<=*f* (1<=≤<=*n*<=≤<=300, 1<=≤<=*b*<=<<=*a*<=≤<=100, 0<=≤<=*k*<=≤<=300, 1<=≤<=*f*<=≤<=1000) where:
- *n* — the number of Polycarp trips, - *a* — the cost of a regualar single trip, - *b* — the cost of a trip after a transshipment, - *k* — the maximum number of travel cards Polycarp can buy, - *f* — the cost of a single travel card.
The following *n* lines describe the trips in the chronological order. Each line contains exactly two different words separated by a single space — the name of the start stop and the name of the finish stop of the trip. All names consist of uppercase and lowercase English letters and have lengths between 1 to 20 letters inclusive. Uppercase and lowercase letters should be considered different. | Print the smallest amount of money Polycarp could have spent today, if he can purchase no more than *k* travel cards. | [
"3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank\n",
"4 2 1 300 1000\na A\nA aa\naa AA\nAA a\n"
] | [
"11\n",
"5\n"
] | In the first example Polycarp can buy travel card for the route "BerBank <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ecc94b07e73defe233bfe831f3977337706a2d27.png" style="max-width: 100.0%;max-height: 100.0%;"/> University" and spend 8 burles. Note that his second trip "University" <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> "BerMall" was made after transshipment, so for this trip Polycarp payed 3 burles. So the minimum total sum equals to 8 + 3 = 11 burles.
In the second example it doesn't make sense to buy travel cards. Note that each of Polycarp trip (except the first) was made with transshipment. So the minimum total sum equals to 2 + 1 + 1 + 1 = 5 burles. | [
{
"input": "3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank",
"output": "11"
},
{
"input": "4 2 1 300 1000\na A\nA aa\naa AA\nAA a",
"output": "5"
},
{
"input": "2 2 1 0 1\naca BCBA\nBCBA aca",
"output": "3"
},
{
"input": "2 2 1 2 1\nBDDB C\nC BDDB",
... | 46 | 5,632,000 | -1 | 64,994 | |
125 | Two progressions | [
"constructive algorithms",
"greedy"
] | null | null | An arithmetic progression is such a non-empty sequence of numbers where the difference between any two successive numbers is constant. This constant number is called common difference. For example, the sequence 3, 7, 11, 15 is an arithmetic progression. The definition implies that any sequences whose length equals 1 or 2 are arithmetic and all sequences whose length equals 0 are non-arithmetic.
You are given a sequence of different integers *a*1,<=*a*2,<=...,<=*a**n*. You should either split it into two arithmetic progressions or find out that the operation is impossible to perform. Splitting assigns each member of the given sequence to one of two progressions, but the relative order of numbers does not change. Splitting is an inverse operation to merging. | The first line contains a positive integer *n* (2<=≤<=*n*<=≤<=30000), *n* is the length of the given sequence. The second line contains elements of the given sequence *a*1,<=*a*2,<=...,<=*a**n* (<=-<=108<=≤<=*a**i*<=≤<=108). The elements of the progression are different integers. | Print the required arithmetic progressions, one per line. The progressions can be positioned in any order. Each progression should contain at least one number. If there's no solution, then print "No solution" (without the quotes)in the only line of the input file. If there are several solutions, print any of them. | [
"6\n4 1 2 7 3 10\n",
"5\n1 2 3 -2 -7\n"
] | [
"1 2 3 \n4 7 10 \n",
"1 2 3 \n-2 -7 \n"
] | In the second sample another solution is also possible (number three can be assigned to the second progression): 1, 2 and 3, -2, -7. | [] | 0 | 0 | -1 | 65,038 | |
911 | Mass Change Queries | [
"data structures"
] | null | null | You are given an array *a* consisting of *n* integers. You have to process *q* queries to this array; each query is given as four numbers *l*, *r*, *x* and *y*, denoting that for every *i* such that *l*<=≤<=*i*<=≤<=*r* and *a**i*<==<=*x* you have to set *a**i* equal to *y*.
Print the array after all queries are processed. | The first line contains one integer *n* (1<=≤<=*n*<=≤<=200000) — the size of array *a*.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=100) — the elements of array *a*.
The third line contains one integer *q* (1<=≤<=*q*<=≤<=200000) — the number of queries you have to process.
Then *q* lines follow. *i*-th line contains four integers *l*, *r*, *x* and *y* denoting *i*-th query (1<=≤<=*l*<=≤<=*r*<=≤<=*n*, 1<=≤<=*x*,<=*y*<=≤<=100). | Print *n* integers — elements of array *a* after all changes are made. | [
"5\n1 2 3 4 5\n3\n3 5 3 5\n1 5 5 1\n1 5 1 5\n"
] | [
"5 2 5 4 5 "
] | none | [
{
"input": "5\n1 2 3 4 5\n3\n3 5 3 5\n1 5 5 1\n1 5 1 5",
"output": "5 2 5 4 5 "
},
{
"input": "5\n1 2 3 4 5\n1\n1 1 1 1",
"output": "1 2 3 4 5 "
},
{
"input": "1\n1\n1\n1 1 1 1",
"output": "1 "
},
{
"input": "1\n2\n5\n1 1 5 6\n1 1 8 4\n1 1 5 8\n1 1 7 1\n1 1 6 3",
"output"... | 46 | 0 | 0 | 65,060 | |
87 | Vasya and Types | [
"implementation",
"strings"
] | B. Vasya and Types | 2 | 256 | Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's description below carefully and follow it and not the similar rules in real programming languages.
There is a very powerful system of pointers on &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 &*X*, to which refers *X*. That is called a dereference operation.
The &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*<==<=&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 &*T*<=*<= is always equal to *T*.
Note, that the operators are executed consecutively one by one. If we have two operators "typedef &void a" and "typedef a* b", then at first a becomes errtype, and after that b becomes errtype* = errtype, but not &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 &&ptv node\ntypeof node\ntypeof &ptv\n",
"17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\ntypeof b\ntypedef b******* c\ntypeof c\n... | [
"void*\nerrtype\nvoid\n",
"void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype\n"
] | Let's look at the second sample.
After the first two queries typedef the b type is equivalent to void*, and с — to void**.
The next query typedef redefines b — it is now equal to &b = &void* = void. At that, the с type doesn't change.
After that the с type is defined as &&b* = &&void* = &void = errtype. It doesn't influence the b type, that's why the next typedef defines c as &void* = void.
Then the b type is again redefined as &void = errtype.
Please note that the c type in the next query is defined exactly as errtype******* = errtype, and not &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\nty... | 248 | 0 | 3.938 | 65,066 |
325 | Monsters and Diamonds | [
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | Piegirl has found a monster and a book about monsters and pies. When she is reading the book, she found out that there are *n* types of monsters, each with an ID between 1 and *n*. If you feed a pie to a monster, the monster will split into some number of monsters (possibly zero), and at least one colorful diamond. Monsters may be able to split in multiple ways.
At the begining Piegirl has exactly one monster. She begins by feeding the monster a pie. She continues feeding pies to monsters until no more monsters are left. Then she collects all the diamonds that were created.
You will be given a list of split rules describing the way in which the various monsters can split. Every monster can split in at least one way, and if a monster can split in multiple ways then each time when it splits Piegirl can choose the way it splits.
For each monster, determine the smallest and the largest number of diamonds Piegirl can possibly collect, if initially she has a single instance of that monster. Piegirl has an unlimited supply of pies. | The first line contains two integers: *m* and *n* (1<=≤<=*m*,<=*n*<=≤<=105), the number of possible splits and the number of different monster types. Each of the following *m* lines contains a split rule. Each split rule starts with an integer (a monster ID) *m**i* (1<=≤<=*m**i*<=≤<=*n*), and a positive integer *l**i* indicating the number of monsters and diamonds the current monster can split into. This is followed by *l**i* integers, with positive integers representing a monster ID and -1 representing a diamond.
Each monster will have at least one split rule. Each split rule will have at least one diamond. The sum of *l**i* across all split rules will be at most 105. | For each monster, in order of their IDs, print a line with two integers: the smallest and the largest number of diamonds that can possibly be collected by starting with that monster. If Piegirl cannot possibly end up in a state without monsters, print -1 for both smallest and the largest value. If she can collect an arbitrarily large number of diamonds, print -2 as the largest number of diamonds.
If any number in output exceeds 314000000 (but is finite), print 314000000 instead of that number. | [
"6 4\n1 3 -1 1 -1\n1 2 -1 -1\n2 3 -1 3 -1\n2 3 -1 -1 -1\n3 2 -1 -1\n4 2 4 -1\n",
"3 2\n1 2 1 -1\n2 2 -1 -1\n2 3 2 1 -1\n"
] | [
"2 -2\n3 4\n2 2\n-1 -1\n",
"-1 -1\n2 2\n"
] | none | [
{
"input": "6 4\n1 3 -1 1 -1\n1 2 -1 -1\n2 3 -1 3 -1\n2 3 -1 -1 -1\n3 2 -1 -1\n4 2 4 -1",
"output": "2 -2\n3 4\n2 2\n-1 -1"
},
{
"input": "3 2\n1 2 1 -1\n2 2 -1 -1\n2 3 2 1 -1",
"output": "-1 -1\n2 2"
},
{
"input": "2 1\n1 3 -1 1 -1\n1 5 -1 -1 -1 -1 -1",
"output": "5 -2"
},
{
... | 62 | 0 | 0 | 65,093 | |
461 | Appleman and Complicated Task | [
"dsu",
"math"
] | null | null | Toastman came up with a very complicated task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?
Given a *n*<=×<=*n* checkerboard. Each cell of the board has either character 'x', or character 'o', or nothing. How many ways to fill all the empty cells with 'x' or 'o' (each cell must contain only one character in the end) are there, such that for each cell the number of adjacent cells with 'o' will be even? Find the number of ways modulo 1000000007 (109<=+<=7). Two cells of the board are adjacent if they share a side. | The first line contains two integers *n*, *k* (1<=≤<=*n*,<=*k*<=≤<=105) — the size of the board, and the number of cells that has characters initially.
Then *k* lines follows. The *i*-th line contains two integers and a character: *a**i*, *b**i*, *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *c**i* is either 'o' or 'x'). This line means: there is a character *c**i* in the cell that is located on the intersection of the *a**i*-th row and *b**i*-th column. All the given cells are distinct.
Consider that the rows are numbered from 1 to *n* from top to bottom. Analogically, the columns are numbered from 1 to *n* from left to right. | Print a single integer — the answer to the problem. | [
"3 2\n1 1 x\n2 2 o\n",
"4 3\n2 4 x\n3 4 x\n3 2 x\n"
] | [
"2\n",
"2\n"
] | In the first example there are two ways: | [] | 46 | 0 | 0 | 65,142 | |
42 | Safe cracking | [
"brute force",
"constructive algorithms"
] | C. Safe cracking | 2 | 256 | Right now you are to solve a very, very simple problem — to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both by one; you may choose two adjacent even numbers and divide both by two. Nothing else. Crack the safe! | The single line of the input contains four space-separated integer positive numbers not greater than 109 each — four numbers on the circle in consecutive order. | The output should contain "-1" (quotes for clarity) if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To make things clear, assume numbers stand on positions 1 through 4. Each operation is encoded by two symbols. If the following operation is dividing then first symbol is '/'; otherwise it's '+' (addition). The second symbol is the position of the first number in pair in consecutive order. (see samples for clarification).
If there are several solutions, output any of them. | [
"1 1 1 1\n",
"1 2 4 2\n",
"3 3 1 1\n",
"2 1 2 4\n"
] | [
"",
"/2\n/3\n",
"+1\n/1\n/1\n",
"/3\n/4\n"
] | none | [
{
"input": "1 2 4 2",
"output": "/2\n/3"
},
{
"input": "3 3 1 1",
"output": "+1\n/1\n/1"
},
{
"input": "2 1 2 4",
"output": "/3\n/4"
},
{
"input": "5 5 1 1",
"output": "+1\n/1\n+2\n/2\n+4\n/4\n/1"
},
{
"input": "4 4 4 4",
"output": "/1\n/2\n/3\n/4"
},
{
... | 310 | 20,172,800 | 3.884925 | 65,317 |
144 | Anagram Search | [
"implementation",
"strings"
] | null | null | A string *t* is called an anagram of the string *s*, if it is possible to rearrange letters in *t* so that it is identical to the string *s*. For example, the string "aab" is an anagram of the string "aba" and the string "aaa" is not.
The string *t* is called a substring of the string *s* if it can be read starting from some position in the string *s*. For example, the string "aba" has six substrings: "a", "b", "a", "ab", "ba", "aba".
You are given a string *s*, consisting of lowercase Latin letters and characters "?". You are also given a string *p*, consisting of lowercase Latin letters only. Let's assume that a string is good if you can obtain an anagram of the string *p* from it, replacing the "?" characters by Latin letters. Each "?" can be replaced by exactly one character of the Latin alphabet. For example, if the string *p* = «aba», then the string "a??" is good, and the string «?bc» is not.
Your task is to find the number of good substrings of the string *s* (identical substrings must be counted in the answer several times). | The first line is non-empty string *s*, consisting of no more than 105 lowercase Latin letters and characters "?". The second line is non-empty string *p*, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string *p* can exceed the length of the string *s*. | Print the single number representing the number of good substrings of string *s*.
Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times. | [
"bb??x???\naab\n",
"ab?c\nacb\n"
] | [
"2\n",
"2\n"
] | Consider the first sample test. Here the string *s* has two good substrings: "b??" (after we replace the question marks we get "baa"), "???" (after we replace the question marks we get "baa").
Let's consider the second sample test. Here the string *s* has two good substrings: "ab?" ("?" can be replaced by "c"), "b?c" ("?" can be replaced by "a"). | [
{
"input": "bb??x???\naab",
"output": "2"
},
{
"input": "ab?c\nacb",
"output": "2"
},
{
"input": "ccaac\ncbcbca",
"output": "0"
},
{
"input": "?bba?\nbba",
"output": "3"
},
{
"input": "aaaaa??a?a\naaa",
"output": "8"
},
{
"input": "?bba?b?aaa\nabb",
... | 434 | 33,484,800 | 3 | 65,388 | |
142 | Help Shrek and Donkey 2 | [
"games"
] | null | null | Having learned (not without some help from the Codeforces participants) to play the card game from the previous round optimally, Shrek and Donkey (as you may remember, they too live now in the Kingdom of Far Far Away) have decided to quit the boring card games and play with toy soldiers.
The rules of the game are as follows: there is a battlefield, its size equals *n*<=×<=*m* squares, some squares contain the toy soldiers (the green ones belong to Shrek and the red ones belong to Donkey). Besides, each of the *n* lines of the area contains not more than two soldiers. During a move a players should select not less than 1 and not more than *k* soldiers belonging to him and make them either attack or retreat.
An attack is moving all of the selected soldiers along the lines on which they stand in the direction of an enemy soldier, if he is in this line. If this line doesn't have an enemy soldier, then the selected soldier on this line can move in any direction during the player's move. Each selected soldier has to move at least by one cell. Different soldiers can move by a different number of cells. During the attack the soldiers are not allowed to cross the cells where other soldiers stand (or stood immediately before the attack). It is also not allowed to go beyond the battlefield or finish the attack in the cells, where other soldiers stand (or stood immediately before attack).
A retreat is moving all of the selected soldiers along the lines on which they stand in the direction from an enemy soldier, if he is in this line. The other rules repeat the rules of the attack.
For example, let's suppose that the original battlefield had the form (here symbols "G" mark Shrek's green soldiers and symbols "R" mark Donkey's red ones):
Let's suppose that *k*<==<=2 and Shrek moves first. If he decides to attack, then after his move the battlefield can look like that:
If in the previous example Shrek decides to retreat, then after his move the battlefield can look like that:
On the other hand, the followings fields cannot result from Shrek's correct move:
Shrek starts the game. To make a move means to attack or to retreat by the rules. A player who cannot make a move loses and his opponent is the winner. Determine the winner of the given toy soldier game if Shrek and Donkey continue to be under the yellow pills from the last rounds' problem. Thus, they always play optimally (that is, they try to win if it is possible, or finish the game in a draw, by ensuring that it lasts forever, if they cannot win). | The first line contains space-separated integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=100). Then *n* lines contain *m* characters each. These characters belong to the set {"-", "G", "R"}, denoting, respectively, a battlefield's free cell, a cell occupied by Shrek's soldiers and a cell occupied by Donkey's soldiers.
It is guaranteed that each line contains no more than two soldiers. | Print "First" (without the quotes) if Shrek wins in the given Toy Soldier game. If Donkey wins, print "Second" (without the quotes). If the game continues forever, print "Draw" (also without the quotes). | [
"2 3 1\nR-G\nRG-\n",
"3 3 2\nG-R\nR-G\nG-R\n",
"2 3 1\n-R-\n-G-\n",
"2 5 2\n-G-R-\n-R-G-\n"
] | [
"First\n",
"Second\n",
"Draw\n",
"First\n"
] | none | [
{
"input": "2 3 1\nR-G\nRG-",
"output": "First"
},
{
"input": "3 3 2\nG-R\nR-G\nG-R",
"output": "Second"
},
{
"input": "2 3 1\n-R-\n-G-",
"output": "Draw"
},
{
"input": "2 5 2\n-G-R-\n-R-G-",
"output": "First"
},
{
"input": "3 3 1\nG-R\nR-G\nG-R",
"output": "F... | 310 | 21,504,000 | 3 | 65,445 | |
545 | Paths and Trees | [
"graphs",
"greedy",
"shortest paths"
] | null | null | Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume that we are given a connected weighted undirected graph *G*<==<=(*V*,<=*E*) (here *V* is the set of vertices, *E* is the set of edges). The shortest-path tree from vertex *u* is such graph *G*1<==<=(*V*,<=*E*1) that is a tree with the set of edges *E*1 that is the subset of the set of edges of the initial graph *E*, and the lengths of the shortest paths from *u* to any vertex to *G* and to *G*1 are the same.
You are given a connected weighted undirected graph *G* and vertex *u*. Your task is to find the shortest-path tree of the given graph from vertex *u*, the total weight of whose edges is minimum possible. | The first line contains two numbers, *n* and *m* (1<=≤<=*n*<=≤<=3·105, 0<=≤<=*m*<=≤<=3·105) — the number of vertices and edges of the graph, respectively.
Next *m* lines contain three integers each, representing an edge — *u**i*,<=*v**i*,<=*w**i* — the numbers of vertices connected by an edge and the weight of the edge (*u**i*<=≠<=*v**i*,<=1<=≤<=*w**i*<=≤<=109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.
The last line of the input contains integer *u* (1<=≤<=*u*<=≤<=*n*) — the number of the start vertex. | In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.
If there are multiple answers, print any of them. | [
"3 3\n1 2 1\n2 3 1\n1 3 2\n3\n",
"4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4\n"
] | [
"2\n1 2 \n",
"4\n2 3 4 \n"
] | In the first sample there are two possible shortest path trees:
- with edges 1 – 3 and 2 – 3 (the total weight is 3); - with edges 1 – 2 and 2 – 3 (the total weight is 2);
And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1. | [
{
"input": "3 3\n1 2 1\n2 3 1\n1 3 2\n3",
"output": "2\n1 2 "
},
{
"input": "4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4",
"output": "4\n2 3 4 "
},
{
"input": "4 5\n1 2 1\n1 3 1\n2 4 1\n3 4 1\n2 3 10\n1",
"output": "3\n1 2 3 "
},
{
"input": "6 8\n1 2 30\n1 3 20\n2 3 50\n4 2 100\n2 5 4... | 108 | 0 | 0 | 65,746 | |
746 | Numbers Exchange | [
"greedy",
"implementation",
"math"
] | null | null | Eugeny has *n* cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct.
Nikolay has *m* cards, distinct numbers from 1 to *m* are written on them, one per card. It means that Nikolay has exactly one card with number 1, exactly one card with number 2 and so on.
A single exchange is a process in which Eugeny gives one card to Nikolay and takes another one from those Nikolay has. Your task is to find the minimum number of card exchanges and determine which cards Eugeny should exchange. | The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=2·105, 1<=≤<=*m*<=≤<=109) — the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that *n* is even.
The second line contains a sequence of *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the numbers on Eugeny's cards. | If there is no answer, print -1.
Otherwise, in the first line print the minimum number of exchanges. In the second line print *n* integers — Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the *i*-th card wasn't exchanged then the *i*-th number should coincide with the number from the input data. Otherwise, it is considered that this card was exchanged, and the *i*-th number should be equal to the number on the card it was exchanged to.
If there are multiple answers, it is allowed to print any of them. | [
"6 2\n5 6 7 9 4 5\n",
"8 6\n7 7 7 7 8 8 8 8\n",
"4 1\n4 2 1 10\n"
] | [
"1\n5 6 7 9 4 2 \n",
"6\n7 2 4 6 8 1 3 5 \n",
"-1\n"
] | none | [
{
"input": "6 2\n5 6 7 9 4 5",
"output": "1\n5 6 7 9 4 2 "
},
{
"input": "8 6\n7 7 7 7 8 8 8 8",
"output": "6\n7 2 4 6 8 1 3 5 "
},
{
"input": "4 1\n4 2 1 10",
"output": "-1"
},
{
"input": "10 10\n12 13 10 20 13 10 19 15 21 11",
"output": "2\n12 13 10 20 2 4 19 15 21 11 "... | 374 | 34,406,400 | 3 | 66,224 | |
396 | On Number of Decompositions into Multipliers | [
"combinatorics",
"math",
"number theory"
] | null | null | You are given an integer *m* as a product of integers *a*1,<=*a*2,<=... *a**n* . Your task is to find the number of distinct decompositions of number *m* into the product of *n* ordered positive integers.
Decomposition into *n* products, given in the input, must also be considered in the answer. As the answer can be very large, print it modulo 1000000007 (109<=+<=7). | The first line contains positive integer *n* (1<=≤<=*n*<=≤<=500). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). | In a single line print a single number *k* — the number of distinct decompositions of number *m* into *n* ordered multipliers modulo 1000000007 (109<=+<=7). | [
"1\n15\n",
"3\n1 1 2\n",
"2\n5 7\n"
] | [
"1\n",
"3\n",
"4\n"
] | In the second sample, the get a decomposition of number 2, you need any one number out of three to equal 2, and the rest to equal 1.
In the third sample, the possible ways of decomposing into ordered multipliers are [7,5], [5,7], [1,35], [35,1].
A decomposition of positive integer *m* into *n* ordered multipliers is a cortege of positive integers *b* = {*b*<sub class="lower-index">1</sub>, *b*<sub class="lower-index">2</sub>, ... *b*<sub class="lower-index">*n*</sub>} such that <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/607dd79be814c0a988453395ca6d82109b016083.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Two decompositions *b* and *c* are considered different, if there exists index *i* such that *b*<sub class="lower-index">*i*</sub> ≠ *c*<sub class="lower-index">*i*</sub>. | [
{
"input": "1\n15",
"output": "1"
},
{
"input": "3\n1 1 2",
"output": "3"
},
{
"input": "2\n5 7",
"output": "4"
},
{
"input": "2\n5 10",
"output": "6"
},
{
"input": "3\n1 30 1",
"output": "27"
},
{
"input": "2\n1000000000 1000000000",
"output": "36... | 124 | 0 | 0 | 66,519 | |
200 | Tractor College | [
"implementation",
"math",
"number theory",
"ternary search"
] | null | null | While most students still sit their exams, the tractor college has completed the summer exam session. In fact, students study only one subject at this college — the Art of Operating a Tractor. Therefore, at the end of a term a student gets only one mark, a three (satisfactory), a four (good) or a five (excellent). Those who score lower marks are unfortunately expelled.
The college has *n* students, and oddly enough, each of them can be on scholarship. The size of the scholarships varies each term. Since the end-of-the-term exam has just ended, it's time to determine the size of the scholarship to the end of next term.
The monthly budget for the scholarships of the Tractor college is *s* rubles. To distribute the budget optimally, you must follow these rules:
- The students who received the same mark for the exam, should receive the same scholarship;- Let us denote the size of the scholarship (in roubles) for students who have received marks 3, 4 and 5 for the exam, as *k*3, *k*4 and *k*5, respectively. The values *k*3, *k*4 and *k*5 must be integers and satisfy the inequalities 0<=≤<=*k*3<=≤<=*k*4<=≤<=*k*5;- Let's assume that *c*3, *c*4, *c*5 show how many students received marks 3, 4 and 5 for the exam, respectively. The budget of the scholarship should be fully spent on them, that is, *c*3·*k*3<=+<=*c*4·*k*4<=+<=*c*5·*k*5<==<=*s*;- Let's introduce function — the value that shows how well the scholarships are distributed between students. In the optimal distribution function *f*(*k*3,<=*k*4,<=*k*5) takes the minimum possible value.
Given the results of the exam, and the budget size *s*, you have to find the optimal distribution of the scholarship. | The first line has two integers *n*, *s* (3<=≤<=*n*<=≤<=300,<=1<=≤<=*s*<=≤<=3·105) — the number of students and the budget size for the scholarship, respectively. The second line contains *n* integers, where the *i*-th number represents the mark that the *i*-th student got for the exam. It is guaranteed that at each mark was given to at least one student. | On a single line print three integers *k*3, *k*4 and *k*5 — the sought values that represent the optimal distribution of the scholarships. If there are multiple optimal answers, print any of them. If there is no answer, print -1. | [
"5 11\n3 4 3 5 5\n",
"6 15\n5 3 3 4 4 5\n"
] | [
"1 3 3\n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 66,552 | |
847 | Berland Elections | [
"greedy",
"sortings"
] | null | null | The elections to Berland parliament are happening today. Voting is in full swing!
Totally there are *n* candidates, they are numbered from 1 to *n*. Based on election results *k* (1<=≤<=*k*<=≤<=*n*) top candidates will take seats in the parliament.
After the end of the voting the number of votes for each candidate is calculated. In the resulting table the candidates are ordered by the number of votes. In case of tie (equal number of votes) they are ordered by the time of the last vote given. The candidate with ealier last vote stands higher in the resulting table.
So in the resulting table candidates are sorted by the number of votes (more votes stand for the higher place) and if two candidates have equal number of votes they are sorted by the time of last vote (earlier last vote stands for the higher place).
There is no way for a candidate with zero votes to take a seat in the parliament. So it is possible that less than *k* candidates will take a seat in the parliament.
In Berland there are *m* citizens who can vote. Each of them will vote for some candidate. Each citizen will give a vote to exactly one of *n* candidates. There is no option "against everyone" on the elections. It is not accepted to spoil bulletins or not to go to elections. So each of *m* citizens will vote for exactly one of *n* candidates.
At the moment *a* citizens have voted already (1<=≤<=*a*<=≤<=*m*). This is an open election, so for each citizen it is known the candidate for which the citizen has voted. Formally, the *j*-th citizen voted for the candidate *g**j*. The citizens who already voted are numbered in chronological order; i.e. the (*j*<=+<=1)-th citizen voted after the *j*-th.
The remaining *m*<=-<=*a* citizens will vote before the end of elections, each of them will vote for one of *n* candidates.
Your task is to determine for each of *n* candidates one of the three possible outcomes:
- a candidate will be elected to the parliament regardless of votes of the remaining *m*<=-<=*a* citizens; - a candidate has chance to be elected to the parliament after all *n* citizens have voted; - a candidate has no chances to be elected to the parliament regardless of votes of the remaining *m*<=-<=*a* citizens. | The first line contains four integers *n*, *k*, *m* and *a* (1<=≤<=*k*<=≤<=*n*<=≤<=100, 1<=≤<=*m*<=≤<=100, 1<=≤<=*a*<=≤<=*m*) — the number of candidates, the number of seats in the parliament, the number of Berland citizens and the number of citizens who already have voted.
The second line contains a sequence of *a* integers *g*1,<=*g*2,<=...,<=*g**a* (1<=≤<=*g**j*<=≤<=*n*), where *g**j* is the candidate for which the *j*-th citizen has voted. Citizens who already voted are numbered in increasing order of voting times. | Print the sequence consisting of *n* integers *r*1,<=*r*2,<=...,<=*r**n* where:
- *r**i*<==<=1 means that the *i*-th candidate is guaranteed to take seat in the parliament regardless of votes of the remaining *m*<=-<=*a* citizens; - *r**i*<==<=2 means that the *i*-th candidate has a chance to take a seat in the parliament, i.e. the remaining *m*<=-<=*a* citizens can vote in such a way that the candidate will take a seat in the parliament; - *r**i*<==<=3 means that the *i*-th candidate will not take a seat in the parliament regardless of votes of the remaining *m*<=-<=*a* citizens. | [
"3 1 5 4\n1 2 1 3\n",
"3 1 5 3\n1 3 1\n",
"3 2 5 3\n1 3 1\n"
] | [
"1 3 3 ",
"2 3 2 ",
"1 2 2 "
] | none | [
{
"input": "3 1 5 4\n1 2 1 3",
"output": "1 3 3 "
},
{
"input": "3 1 5 3\n1 3 1",
"output": "2 3 2 "
},
{
"input": "3 2 5 3\n1 3 1",
"output": "1 2 2 "
},
{
"input": "1 1 1 1\n1",
"output": "1 "
},
{
"input": "1 1 1 1\n1",
"output": "1 "
},
{
"input": ... | 139 | 2,252,800 | 0 | 66,748 | |
0 | none | [
"none"
] | null | null | Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.
Artem wants to create a basic multiset of integers. He wants these structure to support operations of three types:
1. Add integer to the multiset. Note that the difference between set and multiset is that multiset may store several instances of one integer. 1. Remove integer from the multiset. Only one instance of this integer is removed. Artem doesn't want to handle any exceptions, so he assumes that every time remove operation is called, that integer is presented in the multiset. 1. Count the number of instances of the given integer that are stored in the multiset.
But what about time machine? Artem doesn't simply apply operations to the multiset one by one, he now travels to different moments of time and apply his operation there. Consider the following example.
- First Artem adds integer 5 to the multiset at the 1-st moment of time. - Then Artem adds integer 3 to the multiset at the moment 5. - Then Artem asks how many 5 are there in the multiset at moment 6. The answer is 1. - Then Artem returns back in time and asks how many integers 3 are there in the set at moment 4. Since 3 was added only at moment 5, the number of integers 3 at moment 4 equals to 0. - Then Artem goes back in time again and removes 5 from the multiset at moment 3. - Finally Artyom asks at moment 7 how many integers 5 are there in the set. The result is 0, since we have removed 5 at the moment 3.
Note that Artem dislikes exceptions so much that he assures that after each change he makes all delete operations are applied only to element that is present in the multiset. The answer to the query of the third type is computed at the moment Artem makes the corresponding query and are not affected in any way by future changes he makes.
Help Artem implement time travellers multiset. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of Artem's queries.
Then follow *n* lines with queries descriptions. Each of them contains three integers *a**i*, *t**i* and *x**i* (1<=≤<=*a**i*<=≤<=3, 1<=≤<=*t**i*,<=*x**i*<=≤<=109) — type of the query, moment of time Artem travels to in order to execute this query and the value of the query itself, respectively. It's guaranteed that all moments of time are distinct and that after each operation is applied all operations of the first and second types are consistent. | For each ask operation output the number of instances of integer being queried at the given moment of time. | [
"6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5\n",
"3\n1 1 1\n2 2 1\n3 3 1\n"
] | [
"1\n2\n1\n",
"0\n"
] | none | [] | 78 | 6,656,000 | -1 | 66,899 | |
958 | Guard Duty (medium) | [
"binary search",
"dp",
"greedy",
"sortings"
] | null | null | Princess Heidi decided to give orders to all her *K* Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at *N* specific moments *t*1,<=*t*2,<=...,<=*t**N*. The meetings with commanders must therefore start and stop at those times. Namely, each commander will board her ship at some time *t**i* and disembark at some later time *t**j*. Of course, Heidi needs to meet with all commanders, and no two meetings can be held during the same time. Two commanders cannot even meet at the beginnings/endings of the hyperspace jumps, because too many ships in one position could give out their coordinates to the enemy.
Your task is to find minimum time that Princess Heidi has to spend on meetings, with her schedule satisfying the conditions above. | The first line contains two integers *K*, *N* (2<=≤<=2*K*<=≤<=*N*<=≤<=500000, *K*<=≤<=5000). The second line contains *N* distinct integers *t*1,<=*t*2,<=...,<=*t**N* (1<=≤<=*t**i*<=≤<=109) representing the times when Heidi leaves hyperspace. | Output only one integer: the minimum time spent on meetings. | [
"2 5\n1 4 6 7 12\n",
"3 6\n6 3 4 2 5 1\n",
"4 12\n15 7 4 19 3 30 14 1 5 23 17 25\n"
] | [
"4\n",
"3\n",
"6\n"
] | In the first example, there are five valid schedules: [1, 4], [6, 7] with total time 4, [1, 4], [6, 12] with total time 9, [1, 4], [7, 12] with total time 8, [1, 6], [7, 12] with total time 10, and [4, 6], [7, 12] with total time 7. So the answer is 4.
In the second example, there is only 1 valid schedule: [1, 2], [3, 4], [5, 6].
For the third example, one possible schedule with total time 6 is: [1, 3], [4, 5], [14, 15], [23, 25]. | [] | 530 | 75,980,800 | -1 | 67,034 | |
311 | Fetch the Treasure | [
"brute force",
"data structures",
"graphs",
"shortest paths"
] | null | null | Rainbow built *h* cells in a row that are numbered from 1 to *h* from left to right. There are *n* cells with treasure. We call each of these *n* cells "Treasure Cell". The *i*-th "Treasure Cell" is the *a**i*-th cell and the value of treasure in it is *c**i* dollars.
Then, Freda went in the first cell. For now, she can go just *k* cells forward, or return to the first cell. That means Freda was able to reach the 1st, (*k*<=+<=1)-th, (2·*k*<=+<=1)-th, (3·*k*<=+<=1)-th cells and so on.
Then Rainbow gave Freda *m* operations. Each operation is one of the following three types:
1. Add another method *x*: she can also go just *x* cells forward at any moment. For example, initially she has only one method *k*. If at some moment she has methods *a*1,<=*a*2,<=...,<=*a**r* then she can reach all the cells with number in form , where *v**i* — some non-negative integer. 1. Reduce the value of the treasure in the *x*-th "Treasure Cell" by *y* dollars. In other words, to apply assignment *c**x*<==<=*c**x*<=-<=*y*. 1. Ask the value of the most valuable treasure among the cells Freda can reach. If Freda cannot reach any cell with the treasure then consider the value of the most valuable treasure equal to 0, and do nothing. Otherwise take the most valuable treasure away. If several "Treasure Cells" have the most valuable treasure, take the "Treasure Cell" with the minimum number (not necessarily with the minimum number of cell). After that the total number of cells with a treasure is decreased by one.
As a programmer, you are asked by Freda to write a program to answer each query. | The first line of the input contains four integers: *h* (1<=≤<=*h*<=≤<=1018),<=*n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=105) and *k* (1<=≤<=*k*<=≤<=104).
Each of the next *n* lines contains two integers: *a**i* (1<=≤<=*a**i*<=≤<=*h*),<=*c**i* (1<=≤<=*c**i*<=≤<=109). That means the *i*-th "Treasure Cell" is the *a**i*-th cell and cost of the treasure in that cell is *c**i* dollars. All the *a**i* are distinct.
Each of the next *m* lines is in one of the three following formats:
- "1 *x*" — an operation of type 1, 1<=≤<=*x*<=≤<=*h*; - "2 *x* *y*" — an operation of type 2, 1<=≤<=*x*<=≤<=*n*,<=0<=≤<=*y*<=<<=*c**x*; - "3" — an operation of type 3.
There are at most 20 operations of type 1. It's guaranteed that at any moment treasure in each cell has positive value. It's guaranteed that all operations is correct (no operation can decrease the value of the taken tresure).
Please, do not use the %lld specifier to read 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | For each operation of type 3, output an integer indicates the value (in dollars) of the most valuable treasure among the "Treasure Cells" Freda can reach. If there is no such treasure, output 0. | [
"10 3 5 2\n5 50\n7 60\n8 100\n2 2 5\n3\n1 3\n3\n3\n"
] | [
"55\n100\n50\n"
] | In the sample, there are 10 cells and 3 "Treasure Cells". The first "Treasure Cell" is cell 5, having 50 dollars tresure in it. The second "Treasure Cell" is cell 7, having 60 dollars tresure in it. The third "Treasure Cell" is cell 8, having 100 dollars tresure in it.
At first, Freda can only reach cell 1, 3, 5, 7 and 9. In the first operation, we reduce the value in the second "Treasure Cell" from 60 to 55. Then the most valuable treasure among the "Treasure Cells" she can reach is max(50, 55) = 55. After the third operation, she can also go 3 cells forward each step, being able to reach cell 1, 3, 4, 5, 6, 7, 8, 9, 10. So the most valuable tresure is 100.
Noticed that she took the 55 dollars and 100 dollars treasure away, so the last answer is 50. | [] | 46 | 0 | 0 | 67,059 | |
798 | Mike and gcd problem | [
"dp",
"greedy",
"number theory"
] | null | null | Mike has a sequence *A*<==<=[*a*1,<=*a*2,<=...,<=*a**n*] of length *n*. He considers the sequence *B*<==<=[*b*1,<=*b*2,<=...,<=*b**n*] beautiful if the *gcd* of all its elements is bigger than 1, i.e. .
Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index *i* (1<=≤<=*i*<=<<=*n*), delete numbers *a**i*,<=*a**i*<=+<=1 and put numbers *a**i*<=-<=*a**i*<=+<=1,<=*a**i*<=+<=*a**i*<=+<=1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence *A* beautiful if it's possible, or tell him that it is impossible to do so.
is the biggest non-negative number *d* such that *d* divides *b**i* for every *i* (1<=≤<=*i*<=≤<=*n*). | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=100<=000) — length of sequence *A*.
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — elements of sequence *A*. | Output on the first line "YES" (without quotes) if it is possible to make sequence *A* beautiful by performing operations described above, and "NO" (without quotes) otherwise.
If the answer was "YES", output the minimal number of moves needed to make sequence *A* beautiful. | [
"2\n1 1\n",
"3\n6 2 4\n",
"2\n1 3\n"
] | [
"YES\n1\n",
"YES\n0\n",
"YES\n1\n"
] | In the first example you can simply make one move to obtain sequence [0, 2] with <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6ddb490b8e898198d30c56c6edce2cf15e6f998a.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example the *gcd* of the sequence is already greater than 1. | [
{
"input": "2\n1 1",
"output": "YES\n1"
},
{
"input": "3\n6 2 4",
"output": "YES\n0"
},
{
"input": "2\n1 3",
"output": "YES\n1"
},
{
"input": "9\n57 30 28 81 88 32 3 42 25",
"output": "YES\n8"
},
{
"input": "2\n8 77",
"output": "YES\n2"
},
{
"input": "... | 155 | 18,124,800 | 3 | 67,195 | |
627 | Preorder Test | [
"binary search",
"dfs and similar",
"dp",
"graphs",
"greedy",
"trees"
] | null | null | For his computer science class, Jacob builds a model tree with sticks and balls containing *n* nodes in the shape of a tree. Jacob has spent *a**i* minutes building the *i*-th ball in the tree.
Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough time to search his whole tree to determine this; Jacob knows that she will examine the first *k* nodes in a DFS-order traversal of the tree. She will then assign Jacob a grade equal to the minimum *a**i* she finds among those *k* nodes.
Though Jacob does not have enough time to rebuild his model, he can choose the root node that his teacher starts from. Furthermore, he can rearrange the list of neighbors of each node in any order he likes. Help Jacob find the best grade he can get on this assignment.
A DFS-order traversal is an ordering of the nodes of a rooted tree, built by a recursive DFS-procedure initially called on the root of the tree. When called on a given node *v*, the procedure does the following:
1. Print *v*. 1. Traverse the list of neighbors of the node *v* in order and iteratively call DFS-procedure on each one. Do not call DFS-procedure on node *u* if you came to node *v* directly from *u*. | The first line of the input contains two positive integers, *n* and *k* (2<=≤<=*n*<=≤<=200<=000, 1<=≤<=*k*<=≤<=*n*) — the number of balls in Jacob's tree and the number of balls the teacher will inspect.
The second line contains *n* integers, *a**i* (1<=≤<=*a**i*<=≤<=1<=000<=000), the time Jacob used to build the *i*-th ball.
Each of the next *n*<=-<=1 lines contains two integers *u**i*, *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*) representing a connection in Jacob's tree between balls *u**i* and *v**i*. | Print a single integer — the maximum grade Jacob can get by picking the right root of the tree and rearranging the list of neighbors. | [
"5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3\n",
"4 2\n1 5 5 5\n1 2\n1 3\n1 4\n"
] | [
"3\n",
"1\n"
] | In the first sample, Jacob can root the tree at node 2 and order 2's neighbors in the order 4, 1, 5 (all other nodes have at most two neighbors). The resulting preorder traversal is 2, 4, 1, 3, 5, and the minimum *a*<sub class="lower-index">*i*</sub> of the first 3 nodes is 3.
In the second sample, it is clear that any preorder traversal will contain node 1 as either its first or second node, so Jacob cannot do better than a grade of 1. | [
{
"input": "5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3",
"output": "3"
},
{
"input": "4 2\n1 5 5 5\n1 2\n1 3\n1 4",
"output": "1"
},
{
"input": "2 1\n1 100000\n2 1",
"output": "100000"
},
{
"input": "2 2\n1 1000000\n1 2",
"output": "1"
},
{
"input": "10 4\n104325 153357 2... | 139 | 0 | 0 | 67,613 | |
49 | Common ancestor | [
"dp"
] | E. Common ancestor | 5 | 256 | The DNA sequence for every living creature in Berland can be represented as a non-empty line consisting of lowercase Latin letters. Berland scientists found out that all the creatures evolve by stages. During one stage exactly one symbol of the DNA line is replaced by exactly two other ones. At that overall there are *n* permissible substitutions. The substitution *a**i*->*b**i**c**i* means that any one symbol *a**i* can be replaced with two symbols *b**i**c**i*. Every substitution could happen an unlimited number of times.
They say that two creatures with DNA sequences *s*1 and *s*2 can have a common ancestor if there exists such a DNA sequence *s*3 that throughout evolution it can result in *s*1 and *s*2, perhaps after a different number of stages. Your task is to find out by the given *s*1 and *s*2 whether the creatures possessing such DNA sequences can have a common ancestor. If the answer is positive, you have to find the length of the shortest sequence of the common ancestor’s DNA. | The first line contains a non-empty DNA sequence *s*1, the second line contains a non-empty DNA sequence *s*2. The lengths of these lines do not exceed 50, the lines contain only lowercase Latin letters. The third line contains an integer *n* (0<=≤<=*n*<=≤<=50) — the number of permissible substitutions. Then follow *n* lines each of which describes a substitution in the format *a**i*->*b**i**c**i*. The characters *a**i*, *b**i*, and *c**i* are lowercase Latin letters. Lines *s*1 and *s*2 can coincide, the list of substitutions can contain similar substitutions. | If *s*1 and *s*2 cannot have a common ancestor, print -1. Otherwise print the length of the shortest sequence *s*3, from which *s*1 and *s*2 could have evolved. | [
"ababa\naba\n2\nc->ba\nc->cc\n",
"ababa\naba\n7\nc->ba\nc->cc\ne->ab\nz->ea\nb->ba\nd->dd\nd->ab\n",
"ababa\naba\n1\nc->ba\n"
] | [
"2\n",
"1\n",
"-1\n"
] | none | [
{
"input": "ababa\naba\n2\nc->ba\nc->cc",
"output": "2"
},
{
"input": "ababa\naba\n7\nc->ba\nc->cc\ne->ab\nz->ea\nb->ba\nd->dd\nd->ab",
"output": "1"
},
{
"input": "ababa\naba\n1\nc->ba",
"output": "-1"
},
{
"input": "mvo\nmx\n5\nu->dl\nt->fb\nx->vo\nn->kq\na->ca",
"outpu... | 2,556 | 1,331,200 | 3.74192 | 67,617 |
115 | Plumber | [
"math"
] | null | null | Little John aspires to become a plumber! Today he has drawn a grid consisting of *n* rows and *m* columns, consisting of *n*<=×<=*m* square cells.
In each cell he will draw a pipe segment. He can only draw four types of segments numbered from 1 to 4, illustrated as follows:
Each pipe segment has two ends, illustrated by the arrows in the picture above. For example, segment 1 has ends at top and left side of it.
Little John considers the piping system to be leaking if there is at least one pipe segment inside the grid whose end is not connected to another pipe's end or to the border of the grid. The image below shows an example of leaking and non-leaking systems of size 1<=×<=2.
Now, you will be given the grid that has been partially filled by Little John. Each cell will either contain one of the four segments above, or be empty. Find the number of possible different non-leaking final systems after Little John finishes filling all of the empty cells with pipe segments. Print this number modulo 1000003 (106<=+<=3).
Note that rotations or flipping of the grid are not allowed and so two configurations that are identical only when one of them has been rotated or flipped either horizontally or vertically are considered two different configurations. | The first line will contain two single-space separated integers *n* and *m* (1<=≤<=*n*,<=*m*,<=*n*·*m*<=≤<=5·105) — the number of rows and columns respectively. Then *n* lines follow, each contains exactly *m* characters — the description of the grid. Each character describes a cell and is either one of these:
- "1" - "4" — a pipe segment of one of four types as described above - "." — an empty cell | Print a single integer denoting the number of possible final non-leaking pipe systems modulo 1000003 (106<=+<=3). If there are no such configurations, print 0. | [
"2 2\n13\n..\n",
"3 1\n1\n4\n.\n",
"2 2\n3.\n.1\n"
] | [
"2\n",
"0\n",
"1\n"
] | For the first example, the initial configuration of the grid is as follows.
The only two possible final non-leaking pipe configurations are as follows:
For the second example, the initial grid is already leaking, so there will be no final grid that is non-leaking.
For the final example, there's only one possible non-leaking final grid as follows. | [
{
"input": "2 2\n13\n..",
"output": "2"
},
{
"input": "3 1\n1\n4\n.",
"output": "0"
},
{
"input": "2 2\n3.\n.1",
"output": "1"
},
{
"input": "2 2\n..\n..",
"output": "16"
},
{
"input": "2 2\n32\n41",
"output": "1"
},
{
"input": "4 4\n....\n.3.3\n....\n... | 60 | 0 | 0 | 67,661 | |
718 | Matvey's Birthday | [
"bitmasks",
"graphs"
] | null | null | Today is Matvey's birthday. He never knows what to ask as a present so friends gave him a string *s* of length *n*. This string consists of only first eight English letters: 'a', 'b', ..., 'h'.
First question that comes to mind is: who might ever need some string? Matvey is a special boy so he instantly found what to do with this string. He used it to build an undirected graph where vertices correspond to position in the string and there is an edge between distinct positions *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=*n*) if at least one of the following conditions hold:
1. *a* and *b* are neighbouring, i.e. |*a*<=-<=*b*|<==<=1. 1. Positions *a* and *b* contain equal characters, i.e. *s**a*<==<=*s**b*.
Then Matvey decided to find the diameter of this graph. Diameter is a maximum distance (length of the shortest path) among all pairs of vertices. Also, Matvey wants to find the number of pairs of vertices such that the distance between them is equal to the diameter of the graph. As he is very cool and experienced programmer he managed to solve this problem very fast. Will you do the same? | The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=100<=000) — the length of the string.
The second line contains the string *s* itself. It's guaranteed that *s* consists of only first eight letters of English alphabet. | Print two integers — the diameter of the graph and the number of pairs of positions with the distance equal to the diameter. | [
"3\nabc\n",
"7\naaabaaa\n"
] | [
"2 1\n",
"2 4\n"
] | Consider the second sample.
The maximum distance is 2. It's obtained for pairs (1, 4), (2, 4), (4, 6) and (4, 7). | [] | 46 | 5,529,600 | -1 | 67,748 | |
593 | Strange Calculation and Cats | [
"dp",
"matrices"
] | null | null | Gosha's universe is a table consisting of *n* rows and *m* columns. Both the rows and columns are numbered with consecutive integers starting with 1. We will use (*r*,<=*c*) to denote a cell located in the row *r* and column *c*.
Gosha is often invited somewhere. Every time he gets an invitation, he first calculates the number of ways to get to this place, and only then he goes. Gosha's house is located in the cell (1,<=1).
At any moment of time, Gosha moves from the cell he is currently located in to a cell adjacent to it (two cells are adjacent if they share a common side). Of course, the movement is possible only if such a cell exists, i.e. Gosha will not go beyond the boundaries of the table. Thus, from the cell (*r*,<=*c*) he is able to make a move to one of the cells (*r*<=-<=1,<=*c*), (*r*,<=*c*<=-<=1), (*r*<=+<=1,<=*c*), (*r*,<=*c*<=+<=1). Also, Ghosha can skip a move and stay in the current cell (*r*,<=*c*).
Besides the love of strange calculations, Gosha is allergic to cats, so he never goes to the cell that has a cat in it. Gosha knows exactly where and when he will be invited and the schedule of cats travelling along the table. Formally, he has *q* records, the *i*-th of them has one of the following forms:
- 1, *x**i*, *y**i*, *t**i* — Gosha is invited to come to cell (*x**i*,<=*y**i*) at the moment of time *t**i*. It is guaranteed that there is no cat inside cell (*x**i*,<=*y**i*) at this moment of time. - 2, *x**i*, *y**i*, *t**i* — at the moment *t**i* a cat appears in cell (*x**i*,<=*y**i*). It is guaranteed that no other cat is located in this cell (*x**i*,<=*y**i*) at that moment of time. - 3, *x**i*, *y**i*, *t**i* — at the moment *t**i* a cat leaves cell (*x**i*,<=*y**i*). It is guaranteed that there is cat located in the cell (*x**i*,<=*y**i*).
Gosha plans to accept only one invitation, but he has not yet decided, which particular one. In order to make this decision, he asks you to calculate for each of the invitations *i* the number of ways to get to the cell (*x**i*,<=*y**i*) at the moment *t**i*. For every invitation, assume that Gosha he starts moving from cell (1,<=1) at the moment 1.
Moving between two neighboring cells takes Gosha exactly one unit of tim. In particular, this means that Gosha can come into the cell only if a cat sitting in it leaves the moment when Gosha begins his movement from the neighboring cell, and if none of the cats comes to the cell at the time when Gosha is in it.
Two ways to go from cell (1,<=1) to cell (*x*,<=*y*) at time *t* are considered distinct if for at least one moment of time from 1 to *t* Gosha's positions are distinct for the two ways at this moment. Note, that during this travel Gosha is allowed to visit both (1,<=1) and (*x*,<=*y*) multiple times. Since the number of ways can be quite large, print it modulo 109<=+<=7. | The first line of the input contains three positive integers *n*, *m* and *q* (1<=≤<=*n*·*m*<=≤<=20,<=1<=≤<=*q*<=≤<=10<=000) — the number of rows and columns in the table and the number of events respectively.
Next *q* lines describe the events, each description contains four integers *tp**i*, *x**i*, *y**i* and *t**i* (1<=≤<=*tp*<=≤<=3,<=1<=≤<=*x*<=≤<=*n*,<=1<=≤<=*y*<=≤<=*m*,<=2<=≤<=*t*<=≤<=109) — the type of the event (1 if Gosha gets an invitation, 2 if a cat comes to the cell and 3 if a cat leaves the cell), the coordinates of the cell where the action takes place and the moment of time at which the action takes place respectively.
It is guaranteed that the queries are given in the chronological order, i.e. *t**i*<=<<=*t**i*<=+<=1. | For each invitation *i* (that is, *tp**i*<==<=1) calculate the number of ways to get to cell (*x**i*,<=*y**i*) at the moment of time *t**i*. Respond to the invitations chronologically, that is, in the order they appear in the input. | [
"1 3 3\n2 1 2 3\n3 1 2 5\n1 1 1 7\n",
"3 3 3\n2 2 2 2\n1 3 3 5\n1 3 3 7\n",
"4 5 5\n2 2 5 3\n2 2 4 6\n3 2 4 9\n1 4 4 13\n1 4 4 15\n"
] | [
"5\n",
"2\n42\n",
"490902\n10598759\n"
] | Explanation of the first sample. Each picture specifies the number of ways to arrive at the cell at the appropriate time. (X stands for a cell blocked at this particular moment of time) | [
{
"input": "1 3 3\n2 1 2 3\n3 1 2 5\n1 1 1 7",
"output": "5"
},
{
"input": "3 3 3\n2 2 2 2\n1 3 3 5\n1 3 3 7",
"output": "2\n42"
},
{
"input": "4 5 5\n2 2 5 3\n2 2 4 6\n3 2 4 9\n1 4 4 13\n1 4 4 15",
"output": "490902\n10598759"
},
{
"input": "1 1 1\n1 1 1 2",
"output": "1... | 30 | 0 | 0 | 67,789 | |
652 | Pursuit For Artifacts | [
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.
In that country there are *n* islands and *m* bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact.
At the start Johnny is in the island *a* and the artifact-dealer is in the island *b* (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult.
Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island.
Determine if Johnny can find some artifact and sell it. | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=3·105, 0<=≤<=*m*<=≤<=3·105) — the number of islands and bridges in the game.
Each of the next *m* lines contains the description of the bridge — three integers *x**i*, *y**i*, *z**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=≠<=*y**i*, 0<=≤<=*z**i*<=≤<=1), where *x**i* and *y**i* are the islands connected by the *i*-th bridge, *z**i* equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands.
The last line contains two integers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=*n*) — the islands where are Johnny and the artifact-dealer respectively. | If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes). | [
"6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6\n",
"5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4\n",
"5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6",
"output": "YES"
},
{
"input": "5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4",
"output": "NO"
},
{
"input": "5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2",
"output": "YES"
},
{
"input": "1 0\n1 1",
"outp... | 61 | 1,126,400 | 0 | 67,838 | |
0 | none | [
"none"
] | null | null | Is there anything better than going to the zoo after a tiresome week at work? No wonder Grisha feels the same while spending the entire weekend accompanied by pretty striped zebras.
Inspired by this adventure and an accidentally found plasticine pack (represented as a sequence of black and white stripes), Grisha now wants to select several consequent (contiguous) pieces of alternating colors to create a zebra. Let's call the number of selected pieces the length of the zebra.
Before assembling the zebra Grisha can make the following operation $0$ or more times. He splits the sequence in some place into two parts, then reverses each of them and sticks them together again. For example, if Grisha has pieces in the order "bwbbw" (here 'b' denotes a black strip, and 'w' denotes a white strip), then he can split the sequence as bw|bbw (here the vertical bar represents the cut), reverse both parts and obtain "wbwbb".
Determine the maximum possible length of the zebra that Grisha can produce. | The only line contains a string $s$ ($1 \le |s| \le 10^5$, where $|s|$ denotes the length of the string $s$) comprised of lowercase English letters 'b' and 'w' only, where 'w' denotes a white piece and 'b' denotes a black piece. | Print a single integer — the maximum possible zebra length. | [
"bwwwbwwbw\n",
"bwwbwwb\n"
] | [
"5\n",
"3\n"
] | In the first example one of the possible sequence of operations is bwwwbww|bw $\to$ w|wbwwwbwb $\to$ wbwbwwwbw, that gives the answer equal to $5$.
In the second example no operation can increase the answer. | [
{
"input": "bwwwbwwbw",
"output": "5"
},
{
"input": "bwwbwwb",
"output": "3"
},
{
"input": "bwb",
"output": "3"
},
{
"input": "bbbbwbwwbbwwwwwbbbwb",
"output": "4"
},
{
"input": "bbwwwbwwbwwbbwwwwwbbbbbbwbwwwwbbwwbbwbbwbwwwbwwwwbwwbbwwwbbwbbwwbbwbwwwwwwwbbwwbbwwbb... | 93 | 0 | 0 | 67,910 | |
203 | Hit Ball | [
"geometry",
"implementation",
"math"
] | null | null | When Valera was playing football on a stadium, it suddenly began to rain. Valera hid in the corridor under the grandstand not to get wet. However, the desire to play was so great that he decided to train his hitting the ball right in this corridor. Valera went back far enough, put the ball and hit it. The ball bounced off the walls, the ceiling and the floor corridor and finally hit the exit door. As the ball was wet, it left a spot on the door. Now Valera wants to know the coordinates for this spot.
Let's describe the event more formally. The ball will be considered a point in space. The door of the corridor will be considered a rectangle located on plane *xOz*, such that the lower left corner of the door is located at point (0,<=0,<=0), and the upper right corner is located at point (*a*,<=0,<=*b*) . The corridor will be considered as a rectangular parallelepiped, infinite in the direction of increasing coordinates of *y*. In this corridor the floor will be considered as plane *xOy*, and the ceiling as plane, parallel to *xOy* and passing through point (*a*,<=0,<=*b*). We will also assume that one of the walls is plane *yOz*, and the other wall is plane, parallel to *yOz* and passing through point (*a*,<=0,<=*b*).
We'll say that the ball hit the door when its coordinate *y* was equal to 0. Thus the coordinates of the spot are point (*x*0,<=0,<=*z*0), where 0<=≤<=*x*0<=≤<=*a*,<=0<=≤<=*z*0<=≤<=*b*. To hit the ball, Valera steps away from the door at distance *m* and puts the ball in the center of the corridor at point . After the hit the ball flies at speed (*v**x*,<=*v**y*,<=*v**z*). This means that if the ball has coordinates (*x*,<=*y*,<=*z*), then after one second it will have coordinates (*x*<=+<=*v**x*,<=*y*<=+<=*v**y*,<=*z*<=+<=*v**z*).
See image in notes for clarification.
When the ball collides with the ceiling, the floor or a wall of the corridor, it bounces off in accordance with the laws of reflection (the angle of incidence equals the angle of reflection). In the problem we consider the ideal physical model, so we can assume that there is no air resistance, friction force, or any loss of energy. | The first line contains three space-separated integers *a*,<=*b*,<=*m* (1<=≤<=*a*,<=*b*,<=*m*<=≤<=100). The first two integers specify point (*a*,<=0,<=*b*), through which the ceiling and one of the corridor walls pass. The third integer is the distance at which Valera went away from the door.
The second line has three space-separated integers *v**x*,<=*v**y*,<=*v**z* (|*v**x*|,<=|*v**y*|,<=|*v**z*|<=≤<=100,<=*v**y*<=<<=0,<=*v**z*<=≥<=0) — the speed of the ball after the hit.
It is guaranteed that the ball hits the door. | Print two real numbers *x*0,<=*z*0 — the *x* and *z* coordinates of point (*x*0,<=0,<=*z*0), at which the ball hits the exit door. The answer will be considered correct, if its absolute or relative error does not exceed 10<=<=-<=6. | [
"7 2 11\n3 -11 2\n",
"7 2 11\n4 -3 3\n"
] | [
"6.5000000000 2.0000000000\n",
"4.1666666667 1.0000000000\n"
] | <img class="tex-graphics" src="https://espresso.codeforces.com/0b96c99a50a7ff8657d6301992a0fe440badfb7b.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "7 2 11\n3 -11 2",
"output": "6.5000000000 2.0000000000"
},
{
"input": "7 2 11\n4 -3 3",
"output": "4.1666666667 1.0000000000"
},
{
"input": "44 94 98\n-17 -64 9",
"output": "4.0312500000 13.7812500000"
},
{
"input": "41 4 58\n81 -9 65",
"output": "31.500000000... | 186 | 0 | 0 | 67,929 | |
24 | Broken robot | [
"dp",
"math",
"probabilities"
] | D. Broken robot | 2 | 256 | You received as a gift a very clever robot walking on a rectangular board. Unfortunately, you understood that it is broken and behaves rather strangely (randomly). The board consists of *N* rows and *M* columns of cells. The robot is initially at some cell on the *i*-th row and the *j*-th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost (*N*-th) row. The robot can stay at it's current cell, move to the left, move to the right, or move to the cell below the current. If the robot is in the leftmost column it cannot move to the left, and if it is in the rightmost column it cannot move to the right. At every step all possible moves are equally probable. Return the expected number of step to reach the bottommost row. | On the first line you will be given two space separated integers *N* and *M* (1<=≤<=*N*,<=*M*<=≤<=1000). On the second line you will be given another two space separated integers *i* and *j* (1<=≤<=*i*<=≤<=*N*,<=1<=≤<=*j*<=≤<=*M*) — the number of the initial row and the number of the initial column. Note that, (1,<=1) is the upper left corner of the board and (*N*,<=*M*) is the bottom right corner. | Output the expected number of steps on a line of itself with at least 4 digits after the decimal point. | [
"10 10\n10 4\n",
"10 14\n5 14\n"
] | [
"0.0000000000\n",
"18.0038068653\n"
] | none | [
{
"input": "10 10\n10 4",
"output": "0.0000000000"
},
{
"input": "10 14\n5 14",
"output": "18.0038068653"
},
{
"input": "126 125\n115 22",
"output": "43.9999127943"
},
{
"input": "755 51\n205 12",
"output": "2178.8368031733"
},
{
"input": "385 978\n344 18",
"o... | 92 | 0 | 0 | 67,961 |
555 | Case of Computer Network | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible attack by hackers on a major computer network.
In this network are *n* vertices, some pairs of vertices are connected by *m* undirected channels. It is planned to transfer *q* important messages via this network, the *i*-th of which must be sent from vertex *s**i* to vertex *d**i* via one or more channels, perhaps through some intermediate vertices.
To protect against attacks a special algorithm was developed. Unfortunately it can be applied only to the network containing directed channels. Therefore, as new channels can't be created, it was decided for each of the existing undirected channels to enable them to transmit data only in one of the two directions.
Your task is to determine whether it is possible so to choose the direction for each channel so that each of the *q* messages could be successfully transmitted. | The first line contains three integers *n*, *m* and *q* (1<=≤<=*n*,<=*m*,<=*q*<=≤<=2·105) — the number of nodes, channels and important messages.
Next *m* lines contain two integers each, *v**i* and *u**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*, *v**i*<=≠<=*u**i*), that means that between nodes *v**i* and *u**i* is a channel. Between a pair of nodes can exist more than one channel.
Next *q* lines contain two integers *s**i* and *d**i* (1<=≤<=*s**i*,<=*d**i*<=≤<=*n*, *s**i*<=≠<=*d**i*) — the numbers of the nodes of the source and destination of the corresponding message.
It is not guaranteed that in it initially possible to transmit all the messages. | If a solution exists, print on a single line "Yes" (without the quotes). Otherwise, print "No" (without the quotes). | [
"4 4 2\n1 2\n1 3\n2 3\n3 4\n1 3\n4 2\n",
"3 2 2\n1 2\n3 2\n1 3\n2 1\n",
"3 3 2\n1 2\n1 2\n3 2\n1 3\n2 1\n"
] | [
"Yes\n",
"No\n",
"Yes\n"
] | In the first sample test you can assign directions, for example, as follows: 1 → 2, 1 → 3, 3 → 2, 4 → 3. Then the path for for the first message will be 1 → 3, and for the second one — 4 → 3 → 2.
In the third sample test you can assign directions, for example, as follows: 1 → 2, 2 → 1, 2 → 3. Then the path for the first message will be 1 → 2 → 3, and for the second one — 2 → 1. | [] | 155 | 9,830,400 | 0 | 67,995 | |
690 | Collective Mindsets (hard) | [] | null | null | Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the *N* attendees love to play a very risky game...
Every zombie gets a number *n**i* (1<=≤<=*n**i*<=≤<=*N*) written on his forehead. Although no zombie can see his own number, he can see the numbers written on the foreheads of all *N*<=-<=1 fellows. Note that not all numbers have to be unique (they can even all be the same). From this point on, no more communication between zombies is allowed. Observation is the only key to success. When the cuckoo clock strikes midnight, all attendees have to simultaneously guess the number on their own forehead. If at least one of them guesses his number correctly, all zombies survive and go home happily. On the other hand, if not a single attendee manages to guess his number correctly, all of them are doomed to die!
Zombies aren't very bright creatures though, and Heidi has to act fast if she does not want to jeopardize her life. She has one single option: by performing some quick surgery on the brain she managed to get from the chest, she has the ability to remotely reprogram the decision-making strategy of all attendees for their upcoming midnight game! Can you suggest a sound strategy to Heidi which, given the rules of the game, ensures that at least one attendee will guess his own number correctly, for any possible sequence of numbers on the foreheads?
Given a zombie's rank *R* and the *N*<=-<=1 numbers *n**i* on the other attendees' foreheads, your program will have to return the number that the zombie of rank *R* shall guess. Those answers define your strategy, and we will check if it is flawless or not. | The first line of input contains a single integer *T* (1<=≤<=*T*<=≤<=50000): the number of scenarios for which you have to make a guess.
The *T* scenarios follow, described on two lines each:
- The first line holds two integers, *N* (2<=≤<=*N*<=≤<=6), the number of attendees, and *R* (1<=≤<=*R*<=≤<=*N*), the rank of the zombie who has to make the guess. - The second line lists *N*<=-<=1 integers: the numbers on the foreheads of all other attendees, listed in increasing order of the attendees' rank. (Every zombie knows the rank of every other zombie.) | For every scenario, output a single integer: the number that the zombie of rank *R* shall guess, based on the numbers *n**i* on his *N*<=-<=1 fellows' foreheads. | [
"4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2\n",
"2\n5 2\n2 2 2 2\n6 4\n3 2 6 1 2\n"
] | [
"1\n2\n2\n1\n",
"5\n2\n"
] | For instance, if there were *N* = 2 two attendees, a successful strategy could be:
- The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. - The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1. | [
{
"input": "4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2",
"output": "1\n2\n2\n1"
},
{
"input": "4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2",
"output": "1\n2\n2\n1"
},
{
"input": "27\n3 1\n1 1\n3 2\n1 1\n3 3\n1 1\n3 1\n2 1\n3 2\n2 1\n3 3\n2 1\n3 1\n3 1\n3 2\n3 1\n3 3\n3 1\n3 1\n1 2\n3 2\n1 2\n3 3\n1 2\n3 1\... | 951 | 0 | 3 | 68,070 | |
627 | Island Puzzle | [
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | A remote island chain contains *n* islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of *n*<=-<=1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.
The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.
It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position. | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000) — the total number of islands.
The second line contains *n* space-separated integers *a**i* (0<=≤<=*a**i*<=≤<=*n*<=-<=1) — the statue currently located on the *i*-th island. If *a**i*<==<=0, then the island has no statue. It is guaranteed that the *a**i* are distinct.
The third line contains *n* space-separated integers *b**i* (0<=≤<=*b**i*<=≤<=*n*<=-<=1) — the desired statues of the *i*-th island. Once again, *b**i*<==<=0 indicates the island desires no statue. It is guaranteed that the *b**i* are distinct.
The next *n*<=-<=1 lines each contain two distinct space-separated integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*) — the endpoints of the *i*-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input. | Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 *t*, where *t* is the number of moves necessary to perform the rearrangement.
Otherwise, print *u*, *v*, and *t* (1<=≤<=*u*<=<<=*v*<=≤<=*n*) — the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.
If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing <=-<=1. | [
"3\n1 0 2\n2 0 1\n1 2\n2 3\n",
"2\n1 0\n0 1\n1 2\n",
"4\n0 1 2 3\n0 2 3 1\n1 2\n1 3\n1 4\n"
] | [
"1 3 3\n",
"0 1\n",
"-1\n"
] | In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.
In the third sample, no added bridge and subsequent movements result in the desired position. | [] | 0 | 0 | -1 | 68,445 | |
832 | Misha, Grisha and Underground | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Misha and Grisha are funny boys, so they like to use new underground. The underground has *n* stations connected with *n*<=-<=1 routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station *s* to station *f* by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including *s* and *f*). After that on the same day at evening Grisha will ride from station *t* to station *f* by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean.
The boys have already chosen three stations *a*, *b* and *c* for each of several following days, one of them should be station *s* on that day, another should be station *f*, and the remaining should be station *t*. They became interested how they should choose these stations *s*, *f*, *t* so that the number Grisha will count is as large as possible. They asked you for help. | The first line contains two integers *n* and *q* (2<=≤<=*n*<=≤<=105, 1<=≤<=*q*<=≤<=105) — the number of stations and the number of days.
The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*). The integer *p**i* means that there is a route between stations *p**i* and *i*. It is guaranteed that it's possible to reach every station from any other.
The next *q* lines contains three integers *a*, *b* and *c* each (1<=≤<=*a*,<=*b*,<=*c*<=≤<=*n*) — the ids of stations chosen by boys for some day. Note that some of these ids could be same. | Print *q* lines. In the *i*-th of these lines print the maximum possible number Grisha can get counting when the stations *s*, *t* and *f* are chosen optimally from the three stations on the *i*-th day. | [
"3 2\n1 1\n1 2 3\n2 3 3\n",
"4 1\n1 2 3\n1 2 3\n"
] | [
"2\n3\n",
"2\n"
] | In the first example on the first day if *s* = 1, *f* = 2, *t* = 3, Misha would go on the route 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2, and Grisha would go on the route 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2. He would see the text at the stations 1 and 2. On the second day, if *s* = 3, *f* = 2, *t* = 3, both boys would go on the route 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2. Grisha would see the text at 3 stations.
In the second examle if *s* = 1, *f* = 3, *t* = 2, Misha would go on the route 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3, and Grisha would go on the route 2 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3 and would see the text at both stations. | [
{
"input": "3 2\n1 1\n1 2 3\n2 3 3",
"output": "2\n3"
},
{
"input": "4 1\n1 2 3\n1 2 3",
"output": "2"
},
{
"input": "2 4\n1\n1 1 1\n1 1 2\n1 2 2\n2 2 2",
"output": "1\n2\n2\n1"
},
{
"input": "5 20\n4 1 1 4\n2 2 5\n3 2 5\n2 3 4\n4 2 5\n4 1 2\n5 3 1\n2 1 2\n4 3 2\n1 3 3\n4 2 5... | 1,325 | 40,550,400 | 3 | 68,786 | |
191 | Fools and Roads | [
"data structures",
"dfs and similar",
"trees"
] | null | null | They say that Berland has exactly two problems, fools and roads. Besides, Berland has *n* cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each pair of cities there is no more than one simple path (or else the fools would get lost).
But that is not the end of Berland's special features. In this country fools sometimes visit each other and thus spoil the roads. The fools aren't very smart, so they always use only the simple paths.
A simple path is the path which goes through every Berland city not more than once.
The Berland government knows the paths which the fools use. Help the government count for each road, how many distinct fools can go on it.
Note how the fools' paths are given in the input. | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=105) — the number of cities.
Each of the next *n*<=-<=1 lines contains two space-separated integers *u**i*,<=*v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*), that means that there is a road connecting cities *u**i* and *v**i*.
The next line contains integer *k* (0<=≤<=*k*<=≤<=105) — the number of pairs of fools who visit each other.
Next *k* lines contain two space-separated numbers. The *i*-th line (*i*<=><=0) contains numbers *a**i*,<=*b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*). That means that the fool number 2*i*<=-<=1 lives in city *a**i* and visits the fool number 2*i*, who lives in city *b**i*. The given pairs describe simple paths, because between every pair of cities there is only one simple path. | Print *n*<=-<=1 integer. The integers should be separated by spaces. The *i*-th number should equal the number of fools who can go on the *i*-th road. The roads are numbered starting from one in the order, in which they occur in the input. | [
"5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5\n",
"5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5\n"
] | [
"2 1 1 1 \n",
"3 1 1 1 \n"
] | In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones.
In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one goes fool number 1. | [
{
"input": "5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5",
"output": "2 1 1 1 "
},
{
"input": "5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5",
"output": "3 1 1 1 "
}
] | 0 | 0 | -1 | 68,907 | |
150 | Freezing with Style | [
"binary search",
"data structures",
"divide and conquer",
"trees"
] | null | null | This winter is so... well, you've got the idea :-) The Nvodsk road system can be represented as *n* junctions connected with *n*<=-<=1 bidirectional roads so that there is a path between any two junctions. The organizers of some event want to choose a place to accommodate the participants (junction *v*), and the place to set up the contests (junction *u*). Besides, at the one hand, they want the participants to walk about the city and see the neighbourhood (that's why the distance between *v* and *u* should be no less than *l*). On the other hand, they don't want the participants to freeze (so the distance between *v* and *u* should be no more than *r*). Besides, for every street we know its beauty — some integer from 0 to 109. Your task is to choose the path that fits in the length limits and has the largest average beauty. We shall define the average beauty as a median of sequence of the beauties of all roads along the path.
We can put it more formally like that: let there be a path with the length *k*. Let *a**i* be a non-decreasing sequence that contains exactly *k* elements. Each number occurs there exactly the number of times a road with such beauty occurs along on path. We will represent the path median as number *a*⌊*k*<=/<=2⌋, assuming that indexation starting from zero is used. ⌊*x*⌋ — is number *х*, rounded down to the nearest integer.
For example, if *a*<==<={0,<=5,<=12}, then the median equals to 5, and if *a*<==<={0,<=5,<=7,<=12}, then the median is number 7.
It is guaranteed that there will be at least one path with the suitable quantity of roads. | The first line contains three integers *n*, *l*, *r* (1<=≤<=*l*<=≤<=*r*<=<<=*n*<=≤<=105).
Next *n*<=-<=1 lines contain descriptions of roads of the Nvodsk, each line contains three integers *a**i*, *b**i*, *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, 0<=≤<=*c**i*<=≤<=109, *a**i*<=≠<=*b**i*) — junctions *a**i* and *b**i* are connected with a street whose beauty equals *c**i*. | Print two integers — numbers of the junctions, where to accommodate the participants and set up the contests, correspondingly. If there are multiple optimal variants, print any of them. | [
"6 3 4\n1 2 1\n2 3 1\n3 4 1\n4 5 1\n5 6 1\n",
"6 3 4\n1 2 1\n2 3 1\n3 4 1\n4 5 2\n5 6 2\n",
"5 1 4\n1 2 1\n1 3 4\n3 4 7\n3 5 2\n",
"8 3 6\n1 2 9\n2 3 7\n3 4 7\n4 5 8\n5 8 2\n3 6 3\n2 7 4\n"
] | [
"4 1\n",
"6 3\n",
"4 3\n",
"5 1\n"
] | In the first sample all roads have the same beauty. That means that all paths of the positive length have the same median. Thus, any path with length from 3 to 4, inclusive will be valid for us.
In the second sample the city looks like that: 1 - 2 - 3 - 4 - 5 - 6. Two last roads are more valuable and we should choose any path that contains both of them and has the suitable length. It is either the path between 2 and 6 or the path between 3 and 6. | [] | 60 | 0 | -1 | 68,973 | |
990 | GCD Counting | [
"divide and conquer",
"dp",
"dsu",
"number theory",
"trees"
] | null | null | You are given a tree consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is equal to $a_i$.
Let's denote the function $g(x, y)$ as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex $x$ to vertex $y$ (including these two vertices).
For every integer from $1$ to $2 \cdot 10^5$ you have to count the number of pairs $(x, y)$ $(1 \le x \le y \le n)$ such that $g(x, y)$ is equal to this number. | The first line contains one integer $n$ — the number of vertices $(1 \le n \le 2 \cdot 10^5)$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 2 \cdot 10^5)$ — the numbers written on vertices.
Then $n - 1$ lines follow, each containing two integers $x$ and $y$ $(1 \le x, y \le n, x \ne y)$ denoting an edge connecting vertex $x$ with vertex $y$. It is guaranteed that these edges form a tree. | For every integer $i$ from $1$ to $2 \cdot 10^5$ do the following: if there is no pair $(x, y)$ such that $x \le y$ and $g(x, y) = i$, don't output anything. Otherwise output two integers: $i$ and the number of aforementioned pairs. You have to consider the values of $i$ in ascending order.
See the examples for better understanding. | [
"3\n1 2 3\n1 2\n2 3\n",
"6\n1 2 4 8 16 32\n1 6\n6 3\n3 4\n4 2\n6 5\n",
"4\n9 16 144 6\n1 3\n2 3\n4 3\n"
] | [
"1 4\n2 1\n3 1\n",
"1 6\n2 5\n4 6\n8 1\n16 2\n32 1\n",
"1 1\n2 1\n3 1\n6 2\n9 2\n16 2\n144 1\n"
] | none | [
{
"input": "3\n1 2 3\n1 2\n2 3",
"output": "1 4\n2 1\n3 1"
},
{
"input": "6\n1 2 4 8 16 32\n1 6\n6 3\n3 4\n4 2\n6 5",
"output": "1 6\n2 5\n4 6\n8 1\n16 2\n32 1"
},
{
"input": "4\n9 16 144 6\n1 3\n2 3\n4 3",
"output": "1 1\n2 1\n3 1\n6 2\n9 2\n16 2\n144 1"
},
{
"input": "3\n1 ... | 0 | 0 | -1 | 69,014 | |
845 | Fire in the City | [
"binary search",
"data structures"
] | null | null | The capital of Berland looks like a rectangle of size *n*<=×<=*m* of the square blocks of same size.
Fire!
It is known that *k*<=+<=1 blocks got caught on fire (*k*<=+<=1<=≤<=*n*·*m*). Those blocks are centers of ignition. Moreover positions of *k* of these centers are known and one of these stays unknown. All *k*<=+<=1 positions are distinct.
The fire goes the following way: during the zero minute of fire only these *k*<=+<=1 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner.
Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of *k* blocks (centers of ignition) are known and (*k*<=+<=1)-th can be positioned in any other block.
Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city. | The first line contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=109, 1<=≤<=*k*<=≤<=500).
Each of the next *k* lines contain two integers *x**i* and *y**i* (1<=≤<=*x**i*<=≤<=*n*, 1<=≤<=*y**i*<=≤<=*m*) — coordinates of the *i*-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct. | Print the minimal time it takes the fire to lighten up the whole city (in minutes). | [
"7 7 3\n1 2\n2 1\n5 5\n",
"10 5 1\n3 3\n"
] | [
"3\n",
"2\n"
] | In the first example the last block can have coordinates (4, 4).
In the second example the last block can have coordinates (8, 3). | [
{
"input": "7 7 3\n1 2\n2 1\n5 5",
"output": "3"
},
{
"input": "10 5 1\n3 3",
"output": "2"
},
{
"input": "4 5 19\n1 1\n1 2\n1 3\n1 4\n1 5\n2 1\n2 2\n2 3\n2 5\n3 1\n3 2\n3 3\n3 4\n3 5\n4 1\n4 2\n4 3\n4 4\n4 5",
"output": "0"
},
{
"input": "5 5 24\n1 2\n1 3\n1 4\n1 5\n2 1\n2 2... | 77 | 1,331,200 | 0 | 69,047 | |
235 | Cyclical Quest | [
"data structures",
"string suffix structures",
"strings"
] | null | null | Some days ago, WJMZBMR learned how to answer the query "how many times does a string *x* occur in a string *s*" quickly by preprocessing the string *s*. But now he wants to make it harder.
So he wants to ask "how many consecutive substrings of *s* are cyclical isomorphic to a given string *x*". You are given string *s* and *n* strings *x**i*, for each string *x**i* find, how many consecutive substrings of *s* are cyclical isomorphic to *x**i*.
Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string "abcde" can be rotated to string "deabc". We can take characters "abc" from the beginning and put them at the end of "de". | The first line contains a non-empty string *s*. The length of string *s* is not greater than 106 characters.
The second line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of queries. Then *n* lines follow: the *i*-th line contains the string *x**i* — the string for the *i*-th query. The total length of *x**i* is less than or equal to 106 characters.
In this problem, strings only consist of lowercase English letters. | For each query *x**i* print a single integer that shows how many consecutive substrings of *s* are cyclical isomorphic to *x**i*. Print the answers to the queries in the order they are given in the input. | [
"baabaabaaa\n5\na\nba\nbaa\naabaa\naaba\n",
"aabbaa\n3\naa\naabb\nabba\n"
] | [
"7\n5\n7\n3\n5\n",
"2\n3\n3\n"
] | none | [] | 46 | 0 | 0 | 69,103 | |
903 | Clear The Matrix | [
"bitmasks",
"dp"
] | null | null | You are given a matrix *f* with 4 rows and *n* columns. Each element of the matrix is either an asterisk (*) or a dot (.).
You may perform the following operation arbitrary number of times: choose a square submatrix of *f* with size *k*<=×<=*k* (where 1<=≤<=*k*<=≤<=4) and replace each element of the chosen submatrix with a dot. Choosing a submatrix of size *k*<=×<=*k* costs *a**k* coins.
What is the minimum number of coins you have to pay to replace all asterisks with dots? | The first line contains one integer *n* (4<=≤<=*n*<=≤<=1000) — the number of columns in *f*.
The second line contains 4 integers *a*1, *a*2, *a*3, *a*4 (1<=≤<=*a**i*<=≤<=1000) — the cost to replace the square submatrix of size 1<=×<=1, 2<=×<=2, 3<=×<=3 or 4<=×<=4, respectively.
Then four lines follow, each containing *n* characters and denoting a row of matrix *f*. Each character is either a dot or an asterisk. | Print one integer — the minimum number of coins to replace all asterisks with dots. | [
"4\n1 10 8 20\n***.\n***.\n***.\n...*\n",
"7\n2 1 8 2\n.***...\n.***..*\n.***...\n....*..\n",
"4\n10 10 1 10\n***.\n*..*\n*..*\n.***\n"
] | [
"9\n",
"3\n",
"2\n"
] | In the first example you can spend 8 coins to replace the submatrix 3 × 3 in the top-left corner, and 1 coin to replace the 1 × 1 submatrix in the bottom-right corner.
In the second example the best option is to replace the 4 × 4 submatrix containing columns 2 – 5, and the 2 × 2 submatrix consisting of rows 2 – 3 and columns 6 – 7.
In the third example you can select submatrix 3 × 3 in the top-left corner and then submatrix 3 × 3 consisting of rows 2 – 4 and columns 2 – 4. | [
{
"input": "4\n1 10 8 20\n***.\n***.\n***.\n...*",
"output": "9"
},
{
"input": "7\n2 1 8 2\n.***...\n.***..*\n.***...\n....*..",
"output": "3"
},
{
"input": "4\n10 10 1 10\n***.\n*..*\n*..*\n.***",
"output": "2"
},
{
"input": "5\n4 3 4 4\n.....\n****.\n..***\n...**",
"out... | 1,000 | 70,246,400 | 0 | 69,123 | |
452 | Washer, Dryer, Folder | [
"greedy",
"implementation"
] | null | null | You have *k* pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has *n*1 washing machines, *n*2 drying machines and *n*3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine.
It takes *t*1 minutes to wash one piece of laundry in a washing machine, *t*2 minutes to dry it in a drying machine, and *t*3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have. | The only line of the input contains seven integers: *k*,<=*n*1,<=*n*2,<=*n*3,<=*t*1,<=*t*2,<=*t*3 (1<=≤<=*k*<=≤<=104; 1<=≤<=*n*1,<=*n*2,<=*n*3,<=*t*1,<=*t*2,<=*t*3<=≤<=1000). | Print one integer — smallest number of minutes to do all your laundry. | [
"1 1 1 1 5 5 5\n",
"8 4 3 2 10 5 2\n"
] | [
"15\n",
"32\n"
] | In the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.
In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0, 0, 2, 5, 10, 10, 12 and 15 minutes respectively. The last piece of laundry will be ready after 15 + 10 + 5 + 2 = 32 minutes. | [
{
"input": "1 1 1 1 5 5 5",
"output": "15"
},
{
"input": "8 4 3 2 10 5 2",
"output": "32"
},
{
"input": "10000 1000 1000 1000 1000 1000 1000",
"output": "12000"
},
{
"input": "8 3 4 3 10 9 4",
"output": "43"
},
{
"input": "5 2 4 5 9 9 6",
"output": "42"
},
... | 1,000 | 307,200 | 0 | 69,290 | |
73 | LionAge II | [
"dp"
] | C. LionAge II | 2 | 256 | Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character — non-empty string *s*, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided to change no more than *k* letters of the character name so that the new name sounded as good as possible. Euphony of the line is defined as follows: for each pair of adjacent letters *x* and *y* (*x* immediately precedes *y*) the bonus *c*(*x*,<=*y*) is added to the result. Your task is to determine what the greatest Euphony can be obtained by changing at most *k* letters in the name of the Vasya's character. | The first line contains character's name *s* and an integer number *k* (0<=≤<=*k*<=≤<=100). The length of the nonempty string *s* does not exceed 100. The second line contains an integer number *n* (0<=≤<=*n*<=≤<=676) — amount of pairs of letters, giving bonus to the euphony. The next *n* lines contain description of these pairs «*x* *y* *c*», which means that sequence *xy* gives bonus *c* (*x*,<=*y* — lowercase Latin letters, <=-<=1000<=≤<=*c*<=≤<=1000). It is guaranteed that no pair *x* *y* mentioned twice in the input data. | Output the only number — maximum possible euphony оf the new character's name. | [
"winner 4\n4\ns e 7\no s 8\nl o 13\no o 8\n",
"abcdef 1\n5\na b -10\nb c 5\nc d 5\nd e 5\ne f 5\n"
] | [
"36",
"20"
] | In the first example the most euphony name will be *looser*. It is easy to calculate that its euphony is 36. | [
{
"input": "winner 4\n4\ns e 7\no s 8\nl o 13\no o 8",
"output": "36"
},
{
"input": "abcdef 1\n5\na b -10\nb c 5\nc d 5\nd e 5\ne f 5",
"output": "20"
},
{
"input": "akcbd 2\n3\na d 55\nb z 100\nb c 50",
"output": "155"
},
{
"input": "adcbd 1\n3\na d 55\nb z 100\nb c 50",
... | 2,000 | 6,246,400 | 0 | 69,328 |
700 | Huffman Coding on Segment | [
"data structures",
"greedy"
] | null | null | Alice wants to send an important message to Bob. Message *a*<==<=(*a*1,<=...,<=*a**n*) is a sequence of positive integers (characters).
To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function *f*, that maps each letter that appears in the string to some binary string (that is, string consisting of characters '0' and '1' only) such that for each pair of different characters *a**i* and *a**j* string *f*(*a**i*) is not a prefix of *f*(*a**j*) (and vice versa). The result of the encoding of the message *a*1,<=*a*2,<=...,<=*a**n* is the concatenation of the encoding of each character, that is the string *f*(*a*1)*f*(*a*2)... *f*(*a**n*). Huffman codes are very useful, as the compressed message can be easily and uniquely decompressed, if the function *f* is given. Code is usually chosen in order to minimize the total length of the compressed message, i.e. the length of the string *f*(*a*1)*f*(*a*2)... *f*(*a**n*).
Because of security issues Alice doesn't want to send the whole message. Instead, she picks some substrings of the message and wants to send them separately. For each of the given substrings *a**l**i*... *a**r**i* she wants to know the minimum possible length of the Huffman coding. Help her solve this problem. | The first line of the input contains the single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the length of the initial message. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=100<=000) — characters of the message.
Next line contains the single integer *q* (1<=≤<=*q*<=≤<=100<=000) — the number of queries.
Then follow *q* lines with queries descriptions. The *i*-th of these lines contains two integers *l**i* and *r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*) — the position of the left and right ends of the *i*-th substring respectively. Positions are numbered from 1. Substrings may overlap in any way. The same substring may appear in the input more than once. | Print *q* lines. Each line should contain a single integer — the minimum possible length of the Huffman encoding of the substring *a**l**i*... *a**r**i*. | [
"7\n1 2 1 3 1 2 1\n5\n1 7\n1 3\n3 5\n2 4\n4 4\n"
] | [
"10\n3\n3\n5\n0\n"
] | In the first query, one of the optimal ways to encode the substring is to map 1 to "0", 2 to "10" and 3 to "11".
Note that it is correct to map the letter to the empty substring (as in the fifth query from the sample). | [] | 108 | 921,600 | -1 | 69,337 | |
811 | Vladik and Favorite Game | [
"constructive algorithms",
"dfs and similar",
"graphs",
"interactive"
] | null | null | This is an interactive problem.
Vladik has favorite game, in which he plays all his free time.
Game field could be represented as *n*<=×<=*m* matrix which consists of cells of three types:
- «.» — normal cell, player can visit it. - «F» — finish cell, player has to finish his way there to win. There is exactly one cell of this type. - «*» — dangerous cell, if player comes to this cell, he loses.
Initially player is located in the left top cell with coordinates (1,<=1).
Player has access to 4 buttons "U", "D", "L", "R", each of them move player up, down, left and right directions respectively.
But it’s not that easy! Sometimes friends play game and change functions of buttons. Function of buttons "L" and "R" could have been swapped, also functions of buttons "U" and "D" could have been swapped. Note that functions of buttons can be changed only at the beginning of the game.
Help Vladik win the game! | First line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100) — number of rows and columns respectively.
Each of next *n* lines contains *m* characters describing corresponding row of field. Set of characters in field is described above.
Guaranteed that cell with coordinates (1,<=1) is normal and there is at least one way from initial cell to finish cell without dangerous cells. | none | [
"4 3\n...\n**.\nF*.\n...\n1 1\n1 2\n1 3\n1 3\n2 3\n3 3\n4 3\n4 2\n4 1\n3 1\n"
] | [
"R\nL\nL\nD\nU\nU\nU\nR\nR\nD\n"
] | In first test case all four directions swapped with their opposite directions. Protocol of interaction In more convenient form:
<img class="tex-graphics" src="https://espresso.codeforces.com/d9b78340a3793547cc8ccc2c1c34484f3cfe0453.png" style="max-width: 100.0%;max-height: 100.0%;"/>
This test could be presenter for hack in following way: | [] | 31 | 0 | 0 | 69,380 | |
906 | Reverses | [
"dp",
"string suffix structures",
"strings"
] | null | null | Hurricane came to Berland and to suburbs Stringsvill. You are going to it to check if it's all right with you favorite string. Hurrinace broke it a bit by reversing some of its non-intersecting substrings. You have a photo of this string before hurricane and you want to restore it to original state using reversing minimum possible number of its substrings and find out which substrings you should reverse.
You are given a string *s* — original state of your string and string *t* — state of the string after hurricane. You should select *k* non-intersecting substrings of *t* in such a way that after reverse of these substrings string will be equal *s* and *k* is minimum possible. | First line of input contains string *s* and second line contains string *t*. Both strings have same length and consist of lowercase English letters. 1<=≤<=|*s*|<==<=|*t*|<=≤<=5·105 | In first line print *k* — minimum number of substrings you should reverse. Next output *k* lines. Each line should contain two integers *l**i*, *r**i* meaning that you should reverse substring from symbol number *l**i* to symbol *r**i* (strings are 1-indexed). These substrings shouldn't intersect. If there are multiple answers print any. If it's impossible to restore string output -1. | [
"abcxxxdef\ncbaxxxfed\n"
] | [
"2\n7 9\n1 3\n"
] | none | [] | 77 | 0 | 0 | 69,409 | |
724 | Goods transportation | [
"dp",
"flows",
"greedy"
] | null | null | There are *n* cities located along the one-way road. Cities are numbered from 1 to *n* in the direction of the road.
The *i*-th city had produced *p**i* units of goods. No more than *s**i* units of goods can be sold in the *i*-th city.
For each pair of cities *i* and *j* such that 1<=≤<=*i*<=<<=*j*<=≤<=*n* you can no more than once transport no more than *c* units of goods from the city *i* to the city *j*. Note that goods can only be transported from a city with a lesser index to the city with a larger index. You can transport goods between cities in any order.
Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations. | The first line of the input contains two integers *n* and *c* (1<=≤<=*n*<=≤<=10<=000, 0<=≤<=*c*<=≤<=109) — the number of cities and the maximum amount of goods for a single transportation.
The second line contains *n* integers *p**i* (0<=≤<=*p**i*<=≤<=109) — the number of units of goods that were produced in each city.
The third line of input contains *n* integers *s**i* (0<=≤<=*s**i*<=≤<=109) — the number of units of goods that can be sold in each city. | Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations. | [
"3 0\n1 2 3\n3 2 1\n",
"5 1\n7 4 2 1 0\n1 2 3 4 5\n",
"4 3\n13 10 7 4\n4 7 10 13\n"
] | [
"4\n",
"12\n",
"34\n"
] | none | [
{
"input": "3 0\n1 2 3\n3 2 1",
"output": "4"
},
{
"input": "5 1\n7 4 2 1 0\n1 2 3 4 5",
"output": "12"
},
{
"input": "4 3\n13 10 7 4\n4 7 10 13",
"output": "34"
},
{
"input": "10 1\n0 2 1 1 0 2 5 2 5 5\n4 0 1 4 2 4 4 5 2 3",
"output": "18"
},
{
"input": "10 3\n10... | 61 | 204,800 | -1 | 69,472 | |
16 | Fish | [
"bitmasks",
"dp",
"probabilities"
] | E. Fish | 3 | 128 | *n* fish, numbered from 1 to *n*, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability *a**ij*, and the second will eat up the first with the probability *a**ji*<==<=1<=-<=*a**ij*. The described process goes on until there are at least two fish in the lake. For each fish find out the probability that it will survive to be the last in the lake. | The first line contains integer *n* (1<=≤<=*n*<=≤<=18) — the amount of fish in the lake. Then there follow *n* lines with *n* real numbers each — matrix *a*. *a**ij* (0<=≤<=*a**ij*<=≤<=1) — the probability that fish with index *i* eats up fish with index *j*. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: *a**ij*<==<=1<=-<=*a**ji*. All real numbers are given with not more than 6 characters after the decimal point. | Output *n* space-separated real numbers accurate to not less than 6 decimal places. Number with index *i* should be equal to the probability that fish with index *i* will survive to be the last in the lake. | [
"2\n0 0.5\n0.5 0\n",
"5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0\n"
] | [
"0.500000 0.500000 ",
"1.000000 0.000000 0.000000 0.000000 0.000000 "
] | none | [
{
"input": "2\n0 0.5\n0.5 0",
"output": "0.500000 0.500000 "
},
{
"input": "4\n0 0.5 0.5 0.5\n0.5 0 0.5 0.5\n0.5 0.5 0 0.5\n0.5 0.5 0.5 0",
"output": "0.250000 0.250000 0.250000 0.250000 "
},
{
"input": "5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0",
... | 934 | 4,300,800 | 3.828312 | 69,503 |
763 | Timofey and a flat tree | [
"data structures",
"graphs",
"hashing",
"shortest paths",
"trees"
] | null | null | Little Timofey has a big tree — an undirected connected graph with *n* vertices and no simple cycles. He likes to walk along it. His tree is flat so when he walks along it he sees it entirely. Quite naturally, when he stands on a vertex, he sees the tree as a rooted tree with the root in this vertex.
Timofey assumes that the more non-isomorphic subtrees are there in the tree, the more beautiful the tree is. A subtree of a vertex is a subgraph containing this vertex and all its descendants. You should tell Timofey the vertex in which he should stand to see the most beautiful rooted tree.
Subtrees of vertices *u* and *v* are isomorphic if the number of children of *u* equals the number of children of *v*, and their children can be arranged in such a way that the subtree of the first son of *u* is isomorphic to the subtree of the first son of *v*, the subtree of the second son of *u* is isomorphic to the subtree of the second son of *v*, and so on. In particular, subtrees consisting of single vertex are isomorphic to each other. | First line contains single integer *n* (1<=≤<=*n*<=≤<=105) — number of vertices in the tree.
Each of the next *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=105, *u**i*<=≠<=*v**i*), denoting the vertices the *i*-th edge connects.
It is guaranteed that the given graph is a tree. | Print single integer — the index of the vertex in which Timofey should stand. If there are many answers, you can print any of them. | [
"3\n1 2\n2 3\n",
"7\n1 2\n4 2\n2 3\n5 6\n6 7\n3 7\n",
"10\n1 7\n1 8\n9 4\n5 1\n9 2\n3 5\n10 6\n10 9\n5 10\n"
] | [
"1\n",
"1\n",
"2\n"
] | In the first example we can stand in the vertex 1 or in the vertex 3 so that every subtree is non-isomorphic. If we stand in the vertex 2, then subtrees of vertices 1 and 3 are isomorphic.
In the second example, if we stand in the vertex 1, then only subtrees of vertices 4 and 5 are isomorphic.
In the third example, if we stand in the vertex 1, then subtrees of vertices 2, 3, 4, 6, 7 and 8 are isomorphic. If we stand in the vertex 2, than only subtrees of vertices 3, 4, 6, 7 and 8 are isomorphic. If we stand in the vertex 5, then subtrees of vertices 2, 3, 4, 6, 7 and 8 are isomorphic, and subtrees of vertices 1 and 9 are isomorphic as well: | [] | 46 | 0 | 0 | 69,514 | |
506 | Mr. Kitayuta's Colorful Graph | [
"brute force",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Mr. Kitayuta has just bought an undirected graph with *n* vertices and *m* edges. The vertices of the graph are numbered from 1 to *n*. Each edge, namely edge *i*, has a color *c**i*, connecting vertex *a**i* and *b**i*.
Mr. Kitayuta wants you to process the following *q* queries.
In the *i*-th query, he gives you two integers - *u**i* and *v**i*.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex *u**i* and vertex *v**i* directly or indirectly. | The first line of the input contains space-separated two integers - *n* and *m*(2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105), denoting the number of the vertices and the number of the edges, respectively.
The next *m* lines contain space-separated three integers - *a**i*, *b**i*(1<=≤<=*a**i*<=<<=*b**i*<=≤<=*n*) and *c**i*(1<=≤<=*c**i*<=≤<=*m*). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if *i*<=≠<=*j*,<=(*a**i*,<=*b**i*,<=*c**i*)<=≠<=(*a**j*,<=*b**j*,<=*c**j*).
The next line contains a integer- *q*(1<=≤<=*q*<=≤<=105), denoting the number of the queries.
Then follows *q* lines, containing space-separated two integers - *u**i* and *v**i*(1<=≤<=*u**i*,<=*v**i*<=≤<=*n*). It is guaranteed that *u**i*<=≠<=*v**i*. | For each query, print the answer in a separate line. | [
"4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n",
"5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n"
] | [
"2\n1\n0\n",
"1\n1\n1\n1\n2\n"
] | Let's consider the first sample.
- Vertex 1 and vertex 2 are connected by color 1 and 2. - Vertex 3 and vertex 4 are connected by color 3. - Vertex 1 and vertex 4 are not connected by any single color. | [
{
"input": "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4",
"output": "2\n1\n0"
},
{
"input": "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4",
"output": "1\n1\n1\n1\n2"
},
{
"input": "2 1\n1 2 1\n1\n1 2",
"output": "1"
},
{
"input... | 30 | 0 | 0 | 69,567 | |
63 | Dividing Island | [
"constructive algorithms"
] | D. Dividing Island | 2 | 256 | A revolution took place on the Buka Island. New government replaced the old one. The new government includes *n* parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island.
The island can be conventionally represented as two rectangles *a*<=×<=*b* and *c*<=×<=*d* unit squares in size correspondingly. The rectangles are located close to each other. At that, one of the sides with the length of *a* and one of the sides with the length of *c* lie on one line. You can see this in more details on the picture.
The *i*-th party is entitled to a part of the island equal to *x**i* unit squares. Every such part should fully cover several squares of the island (it is not allowed to cover the squares partially) and be a connected figure. A "connected figure" presupposes that from any square of this party one can move to any other square of the same party moving through edge-adjacent squares also belonging to that party.
Your task is to divide the island between parties. | The first line contains 5 space-separated integers — *a*, *b*, *c*, *d* and *n* (1<=≤<=*a*,<=*b*,<=*c*,<=*d*<=≤<=50, *b*<=≠<=*d*, 1<=≤<=*n*<=≤<=26). The second line contains *n* space-separated numbers. The *i*-th of them is equal to number *x**i* (1<=≤<=*x**i*<=≤<=*a*<=×<=*b*<=+<=*c*<=×<=*d*). It is guaranteed that . | If dividing the island between parties in the required manner is impossible, print "NO" (without the quotes). Otherwise, print "YES" (also without the quotes) and, starting from the next line, print *max*(*b*,<=*d*) lines each containing *a*<=+<=*c* characters. To mark what square should belong to what party, use lowercase Latin letters. For the party that is first in order in the input data, use "a", for the second one use "b" and so on. Use "." for the squares that belong to the sea. The first symbol of the second line of the output data should correspond to the square that belongs to the rectangle *a*<=×<=*b*. The last symbol of the second line should correspond to the square that belongs to the rectangle *c*<=×<=*d*.
If there are several solutions output any. | [
"3 4 2 2 3\n5 8 3\n",
"3 2 1 4 4\n1 2 3 4\n"
] | [
"YES\naaabb\naabbb\ncbb..\nccb..\n",
"YES\nabbd\ncccd\n...d\n...d\n"
] | none | [
{
"input": "3 4 2 2 3\n5 8 3",
"output": "YES\nbbbbc\nbbbcc\naab..\naaa.."
},
{
"input": "3 2 1 4 4\n1 2 3 4",
"output": "YES\ncccd\nbbad\n...d\n...d"
},
{
"input": "1 2 1 1 1\n3",
"output": "YES\naa\na."
},
{
"input": "1 2 1 3 2\n3 2",
"output": "YES\naa\nab\n.b"
},
... | 468 | 22,323,200 | -1 | 69,585 |
625 | Frog Fights | [
"data structures",
"greedy"
] | null | null | Ostap Bender recently visited frog farm and was inspired to create his own frog game.
Number of frogs are places on a cyclic gameboard, divided into *m* cells. Cells are numbered from 1 to *m*, but the board is cyclic, so cell number 1 goes right after the cell number *m* in the direction of movement. *i*-th frog during its turn can jump for *a**i* cells.
Frogs move in turns, game starts with a move by frog 1. On its turn *i*-th frog moves *a**i* cells forward, knocking out all the frogs on its way. If there is a frog in the last cell of the path of the *i*-th frog, that frog is also knocked out. After this the value *a**i* is decreased by the number of frogs that were knocked out during this turn. If *a**i* is zero or goes negative, then *i*-th frog doesn't make moves anymore.
After frog number 1 finishes its turn, frog number 2 starts to move, then frog number 3 and so on. After the frog number *n* makes its move, frog 1 starts to move again, then frog 2 and so on this process goes forever. If some frog was already knocked out from the board, we consider that it skips all its moves.
Help Ostap to identify, what frogs will stay on the board at the end of a game? | First line of the input contains two integers *n* and *m* (1<=≤<=*n*<=≤<=100000,<=1<=≤<=*m*<=≤<=109,<=*n*<=≤<=*m*) — number of frogs and gameboard size, respectively.
Following *n* lines contains frogs descriptions — two integers *p**i* and *a**i* (1<=≤<=*p**i*,<=*a**i*<=≤<=*m*) — the number of cell occupied by *i*-th frog initially and initial jump length. All *p**i* are guaranteed to be distinct. | In the first line output number of frogs on the final gameboard. In the second line output their numbers in any order. | [
"3 5\n2 1\n5 3\n4 3\n",
"5 6\n1 2\n3 4\n2 5\n5 1\n6 1\n"
] | [
"1\n3 ",
"2\n1 4 "
] | In the first sample first frog jumps 1 cell and finishes in cell number 3. Second frog jumps for 3 cells and finishes on cell number 3, knocking out frog number 1. Current jump length for frog number 2 is now 2. Third frog jumps to cell 2, then second frog jumps to cell 5. Third frog in turn finishes in cell 5 and removes frog 2 from the gameboard. Now, it's the only remaining frog in the game.
In the second sample first frog jumps 2 cells and knocks out frogs in cells 2 and 3. Its value *a*<sub class="lower-index">*i*</sub> is now 0. Then fourth frog jumps and knocks out fifth frog and its *a*<sub class="lower-index">*i*</sub> is now 0 too. These two frogs will remains on the gameboard forever. | [] | 0 | 0 | -1 | 69,610 | |
97 | Superset | [
"constructive algorithms",
"divide and conquer"
] | B. Superset | 2 | 256 | A set of points on a plane is called good, if for any two points at least one of the three conditions is true:
- those two points lie on same horizontal line; - those two points lie on same vertical line; - the rectangle, with corners in these two points, contains inside or on its borders at least one point of the set, other than these two. We mean here a rectangle with sides parallel to coordinates' axes, the so-called bounding box of the two points.
You are given a set consisting of *n* points on a plane. Find any good superset of the given set whose size would not exceed 2·105 points. | The first line contains an integer *n* (1<=≤<=*n*<=≤<=104) — the number of points in the initial set. Next *n* lines describe the set's points. Each line contains two integers *x**i* and *y**i* (<=-<=109<=≤<=*x**i*,<=*y**i*<=≤<=109) — a corresponding point's coordinates. It is guaranteed that all the points are different. | Print on the first line the number of points *m* (*n*<=≤<=*m*<=≤<=2·105) in a good superset, print on next *m* lines the points. The absolute value of the points' coordinates should not exceed 109. Note that you should not minimize *m*, it is enough to find any good superset of the given set, whose size does not exceed 2·105.
All points in the superset should have integer coordinates. | [
"2\n1 1\n2 2\n"
] | [
"3\n1 1\n2 2\n1 2\n"
] | none | [] | 0 | 0 | -1 | 69,800 |
676 | Theseus and labyrinth | [
"graphs",
"implementation",
"shortest paths"
] | null | null | Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size *n*<=×<=*m* and consists of blocks of size 1<=×<=1.
Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its position in the labyrinth. Also, each block has some number of doors (possibly none). In one minute, Theseus can either push the button in order to rotate all the blocks 90 degrees clockwise or pass to the neighbouring block. Theseus can go from block *A* to some neighbouring block *B* only if block *A* has a door that leads to block *B* and block *B* has a door that leads to block *A*.
Theseus found an entrance to labyrinth and is now located in block (*x**T*,<=*y**T*) — the block in the row *x**T* and column *y**T*. Theseus know that the Minotaur is hiding in block (*x**M*,<=*y**M*) and wants to know the minimum number of minutes required to get there.
Theseus is a hero, not a programmer, so he asks you to help him. | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the number of rows and the number of columns in labyrinth, respectively.
Each of the following *n* lines contains *m* characters, describing the blocks of the labyrinth. The possible characters are:
- «+» means this block has 4 doors (one door to each neighbouring block); - «-» means this block has 2 doors — to the left and to the right neighbours; - «|» means this block has 2 doors — to the top and to the bottom neighbours; - «^» means this block has 1 door — to the top neighbour; - «>» means this block has 1 door — to the right neighbour; - «<» means this block has 1 door — to the left neighbour; - «v» means this block has 1 door — to the bottom neighbour;- «L» means this block has 3 doors — to all neighbours except left one; - «R» means this block has 3 doors — to all neighbours except right one; - «U» means this block has 3 doors — to all neighbours except top one; - «D» means this block has 3 doors — to all neighbours except bottom one;- «*» means this block is a wall and has no doors.
Left, right, top and bottom are defined from representing labyrinth as a table, where rows are numbered from 1 to *n* from top to bottom and columns are numbered from 1 to *m* from left to right.
Next line contains two integers — coordinates of the block (*x**T*,<=*y**T*) (1<=≤<=*x**T*<=≤<=*n*, 1<=≤<=*y**T*<=≤<=*m*), where Theseus is initially located.
Last line contains two integers — coordinates of the block (*x**M*,<=*y**M*) (1<=≤<=*x**M*<=≤<=*n*, 1<=≤<=*y**M*<=≤<=*m*), where Minotaur hides.
It's guaranteed that both the block where Theseus starts and the block where Minotaur is hiding have at least one door. Theseus and Minotaur may be initially located at the same block. | If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding. | [
"2 2\n+*\n*U\n1 1\n2 2\n",
"2 3\n<><\n><>\n1 1\n2 1\n"
] | [
"-1",
"4"
] | Assume that Theseus starts at the block (*x*<sub class="lower-index">*T*</sub>, *y*<sub class="lower-index">*T*</sub>) at the moment 0. | [
{
"input": "2 2\n+*\n*U\n1 1\n2 2",
"output": "-1"
},
{
"input": "2 3\n<><\n><>\n1 1\n2 1",
"output": "4"
},
{
"input": "3 3\n->v\n*+|\n+*^\n3 3\n1 1",
"output": "6"
},
{
"input": "10 10\n><URRD>>+-\n>+vLLDL-v*\n*+R^v+UUR<\n<DDU>R||RR\nRL*v^UvD|R\nR>U<>DRv|R\n-D^+U^-|UD\nD^>L... | 77 | 3,891,200 | -1 | 70,012 | |
850 | Rainbow Balls | [
"math"
] | null | null | You have a bag of balls of *n* different colors. You have *a**i* balls of the *i*-th color.
While there are at least two different colored balls in the bag, perform the following steps:
- Take out two random balls without replacement one by one. These balls might be the same color. - Color the second ball to the color of the first ball. You are not allowed to switch the order of the balls in this step. - Place both balls back in the bag. - All these actions take exactly one second.
Let *M*<==<=109<=+<=7. It can be proven that the expected amount of time needed before you stop can be represented as a rational number , where *P* and *Q* are coprime integers and where *Q* is not divisible by *M*. Return the value . | The first line of input will contain a single integer *n* (1<=≤<=*n*<=≤<=2<=500) — the number of colors.
The next line of input will contain *n* space separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=105) — the number of balls of each color. | Print a single integer, the answer to the problem. | [
"2\n1 1\n",
"3\n1 2 3\n"
] | [
"1\n",
"750000026\n"
] | In the first sample, no matter what happens, the balls will become the same color after one step.
For the second sample, we have 6 balls. Let’s label the balls from 1 to 6, and without loss of generality, let’s say balls 1,2,3 are initially color 1, balls 4,5 are color 2, and ball 6 are color 3.
Here is an example of how these steps can go:
- We choose ball 5 and ball 6. Ball 6 then becomes color 2. - We choose ball 4 and ball 5. Ball 5 remains the same color (color 2). - We choose ball 1 and ball 5. Ball 5 becomes color 1. - We choose ball 6 and ball 5. Ball 5 becomes color 2. - We choose ball 3 and ball 4. Ball 4 becomes color 1. - We choose ball 4 and ball 6. Ball 6 becomes color 1. - We choose ball 2 and ball 5. Ball 5 becomes color 1.
It can be shown that the answer to this case is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/2e0a7aea86073f5641f26b7fb3a89382142e4fb0.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [] | 46 | 0 | 0 | 70,089 | |
830 | Bamboo Partition | [
"brute force",
"data structures",
"implementation",
"math",
"number theory",
"sortings",
"two pointers"
] | null | null | Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are *n* bamboos in a row, and the *i*-th from the left is *a**i* meters high.
Vladimir has just planted *n* bamboos in a row, each of which has height 0 meters right now, but they grow 1 meter each day. In order to make the partition nice Vladimir can cut each bamboo once at any height (no greater that the height of the bamboo), and then the bamboo will stop growing.
Vladimir wants to check the bamboos each *d* days (i.e. *d* days after he planted, then after 2*d* days and so on), and cut the bamboos that reached the required height. Vladimir wants the total length of bamboo parts he will cut off to be no greater than *k* meters.
What is the maximum value *d* he can choose so that he can achieve what he wants without cutting off more than *k* meters of bamboo? | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=1011) — the number of bamboos and the maximum total length of cut parts, in meters.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the required heights of bamboos, in meters. | Print a single integer — the maximum value of *d* such that Vladimir can reach his goal. | [
"3 4\n1 3 5\n",
"3 40\n10 30 50\n"
] | [
"3\n",
"32\n"
] | In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters. | [
{
"input": "3 4\n1 3 5",
"output": "3"
},
{
"input": "3 40\n10 30 50",
"output": "32"
},
{
"input": "20 53\n32 64 20 41 97 50 20 66 68 22 60 74 61 97 54 80 30 72 59 18",
"output": "6"
},
{
"input": "100 82\n51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 2... | 2,000 | 4,608,000 | 0 | 70,221 | |
930 | Coins Exhibition | [
"data structures",
"dp",
"math"
] | null | null | Arkady and Kirill visited an exhibition of rare coins. The coins were located in a row and enumerated from left to right from 1 to *k*, each coin either was laid with its obverse (front) side up, or with its reverse (back) side up.
Arkady and Kirill made some photos of the coins, each photo contained a segment of neighboring coins. Akrady is interested in obverses, so on each photo made by him there is at least one coin with obverse side up. On the contrary, Kirill is interested in reverses, so on each photo made by him there is at least one coin with its reverse side up.
The photos are lost now, but Arkady and Kirill still remember the bounds of the segments of coins each photo contained. Given this information, compute the remainder of division by 109<=+<=7 of the number of ways to choose the upper side of each coin in such a way, that on each Arkady's photo there is at least one coin with obverse side up, and on each Kirill's photo there is at least one coin with reverse side up. | The first line contains three integers *k*, *n* and *m* (1<=≤<=*k*<=≤<=109, 0<=≤<=*n*,<=*m*<=≤<=105) — the total number of coins, the number of photos made by Arkady, and the number of photos made by Kirill, respectively.
The next *n* lines contain the descriptions of Arkady's photos, one per line. Each of these lines contains two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*k*), meaning that among coins from the *l*-th to the *r*-th there should be at least one with obverse side up.
The next *m* lines contain the descriptions of Kirill's photos, one per line. Each of these lines contains two integers *l* and *r* (1<=≤<=*l*<=≤<=*r*<=≤<=*k*), meaning that among coins from the *l*-th to the *r*-th there should be at least one with reverse side up. | Print the only line — the number of ways to choose the side for each coin modulo 109<=+<=7<==<=1000000007. | [
"5 2 2\n1 3\n3 5\n2 2\n4 5\n",
"5 3 2\n1 3\n2 2\n3 5\n2 2\n4 5\n",
"60 5 7\n1 3\n50 60\n1 60\n30 45\n20 40\n4 5\n6 37\n5 18\n50 55\n22 27\n25 31\n44 45\n"
] | [
"8\n",
"0\n",
"732658600\n"
] | In the first example the following ways are possible ('O' — obverse, 'R' — reverse side):
- OROOR, - ORORO, - ORORR, - RROOR, - RRORO, - RRORR, - ORROR, - ORRRO.
In the second example the information is contradictory: the second coin should have obverse and reverse sides up at the same time, that is impossible. So, the answer is 0. | [] | 31 | 0 | 0 | 70,363 | |
107 | Crime Management | [
"dp",
"graphs",
"matrices"
] | D. Crime Management | 5 | 256 | Zeyad wants to commit *n* crimes in Egypt and not be punished at the end. There are several types of crimes. For example, bribery is a crime but is not considered such when repeated twice. Therefore, bribery is not considered a crime when repeated an even number of times. Speeding is a crime, but is not considered such when repeated a number of times which is a multiple of five.
More specifically, *c* conditions on crime repetitions are known. Each condition describes the crime type *t**i* and its multiplicity *m**i*. If the number of times Zeyad committed the crime *t**i* is a multiple of *m**i*, Zeyad will not be punished for crime *t**i*. Some crimes may be listed more than once. In this case fulfilling at least one condition for this crime is enough to not be punished for it. Of course, if for certain crime the number of times Zeyad committed it is zero, he is innocent with respect to this crime.
Now Zeyad is interested in a number of ways he can commit exactly *n* crimes without any punishment.
The order of commiting the crimes matters. More formally, two ways, sequences *w*1 and *w*2, of committing *n* crimes are equal if *w*1*i*<==<=*w*2*i*, for all 1<=≤<=*i*<=≤<=*n*. | The first line contains two integers *n* and *c* (0<=≤<=*n*<=≤<=1018,<=0<=≤<=*c*<=≤<=1000) — the number of crimes Zeyad would like to commit and the number of conditions he is aware of.
Then the definitions for *c* conditions follow. There are 26 types of crimes. Each crime definition consists of crime type — a capital Latin letter — and its multiplicity.
The multiplicity of each crime is a positive integer number and the product of all multiplicities does not exceed 123. Some conditions may be repeated in the input more than once.
Crime of multiplicity 1 is not yielding any punishment regardless of the number of times it was committed. The strictness of the law is compensated by the fact that it's non-mandatory.
Obviously, if some crime is not listed in the set of conditions, then Zeyad will not consider it, as committing it would unavoidably lead to the punishment.
Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin stream (you may also use the %I64d specificator). | Output the number of different ways Zeyad can commit exactly *n* crimes with no punishment modulo 12345. | [
"5 2\nA 1\nB 2\n",
"6 3\nA 1\nB 2\nC 3\n",
"8 3\nA 2\nA 3\nB 2\n"
] | [
"16\n",
"113\n",
"128\n"
] | In the first test case, the 16 ways are: AAAAA, AAABB, AABAB, AABBA, ABAAB, ABABA, ABBAA, BAAAB, BAABA, BABAA, BBAAA, ABBBB, BABBB, BBABB, BBBAB, BBBBA. | [] | 60 | 0 | 0 | 70,406 |
261 | Maxim and Matrix | [
"constructive algorithms",
"dp",
"math"
] | null | null | Maxim loves to fill in a matrix in a special manner. Here is a pseudocode of filling in a matrix of size (*m*<=+<=1)<=×<=(*m*<=+<=1):
Maxim asks you to count, how many numbers *m* (1<=≤<=*m*<=≤<=*n*) are there, such that the sum of values in the cells in the row number *m*<=+<=1 of the resulting matrix equals *t*.
Expression (*x* *xor* *y*) means applying the operation of bitwise excluding "OR" to numbers *x* and *y*. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by character "^", in Pascal — by "xor". | A single line contains two integers *n* and *t* (1<=≤<=*n*,<=*t*<=≤<=1012,<=*t*<=≤<=*n*<=+<=1).
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. | In a single line print a single integer — the answer to the problem. | [
"1 1\n",
"3 2\n",
"3 3\n",
"1000000000000 1048576\n"
] | [
"1\n",
"1\n",
"0\n",
"118606527258\n"
] | none | [
{
"input": "1 1",
"output": "1"
},
{
"input": "3 2",
"output": "1"
},
{
"input": "3 3",
"output": "0"
},
{
"input": "1000000000000 1048576",
"output": "118606527258"
},
{
"input": "35 4",
"output": "11"
},
{
"input": "70 32",
"output": "1"
},
{... | 62 | 0 | 0 | 70,420 | |
626 | Group Projects | [
"dp"
] | null | null | There are *n* students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the *i*-th student *a**i* minutes to finish his/her independent piece.
If students work at different paces, it can be frustrating for the faster students and stressful for the slower ones. In particular, the imbalance of a group is defined as the maximum *a**i* in the group minus the minimum *a**i* in the group. Note that a group containing a single student has an imbalance of 0. How many ways are there for the students to divide into groups so that the total imbalance of all groups is at most *k*?
Two divisions are considered distinct if there exists a pair of students who work in the same group in one division but different groups in the other. | The first line contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=200, 0<=≤<=*k*<=≤<=1000) — the number of students and the maximum total imbalance allowed, respectively.
The second line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=500) — the time it takes the *i*-th student to complete his/her independent piece of work. | Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109<=+<=7. | [
"3 2\n2 4 5\n",
"4 3\n7 8 9 10\n",
"4 0\n5 10 20 21\n"
] | [
"3\n",
"13\n",
"1\n"
] | In the first sample, we have three options:
- The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2. - The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1. - All three students form their own groups. Total imbalance is 0.
In the third sample, the total imbalance must be 0, so each student must work individually. | [
{
"input": "3 2\n2 4 5",
"output": "3"
},
{
"input": "4 3\n7 8 9 10",
"output": "13"
},
{
"input": "4 0\n5 10 20 21",
"output": "1"
},
{
"input": "20 1000\n50 50 100 100 150 150 200 200 250 250 300 300 350 350 400 400 450 450 500 500",
"output": "97456952"
},
{
"i... | 30 | 0 | 0 | 70,517 | |
603 | Ruminations on Ruminants | [
"geometry",
"math"
] | null | null | Kevin Sun is ruminating on the origin of cows while standing at the origin of the Cartesian plane. He notices *n* lines on the plane, each representable by an equation of the form *ax*<=+<=*by*<==<=*c*. He also observes that no two lines are parallel and that no three lines pass through the same point.
For each triple (*i*,<=*j*,<=*k*) such that 1<=≤<=*i*<=<<=*j*<=<<=*k*<=≤<=*n*, Kevin considers the triangle formed by the three lines . He calls a triangle original if the circumcircle of that triangle passes through the origin. Since Kevin believes that the circles of bovine life are tied directly to such triangles, he wants to know the number of original triangles formed by unordered triples of distinct lines.
Recall that the circumcircle of a triangle is the circle which passes through all the vertices of that triangle. | The first line of the input contains a single integer *n* (3<=≤<=*n*<=≤<=2000), the number of lines.
The next *n* lines describe lines . The *i*-th of these lines contains three space-separated integers *a**i*,<=*b**i*,<=*c**i* (|*a**i*|,<=|*b**i*|,<=|*c**i*|<=≤<=10<=000,<=*a**i*2<=+<=*b**i*2<=><=0), representing the equation *a**i**x*<=+<=*b**i**y*<==<=*c**i* of line . | Print a single integer, the number of triples (*i*,<=*j*,<=*k*) with *i*<=<<=*j*<=<<=*k* such that lines form an original triangle. | [
"4\n1 0 0\n0 1 0\n1 1 -1\n1 -1 2\n",
"3\n0 1 1\n1 1 2\n1 -1 -2\n"
] | [
"2\n",
"1\n"
] | Note that in the first sample, some of the lines pass through the origin.
In the second sample, there is exactly one triple of lines: *y* = 1, *x* + *y* = 2, *x* - *y* = - 2. The triangle they form has vertices (0, 2), (1, 1), ( - 1, 1). The circumcircle of this triangle has equation *x*<sup class="upper-index">2</sup> + (*y* - 1)<sup class="upper-index">2</sup> = 1. This indeed passes through (0, 0). | [] | 31 | 0 | 0 | 70,632 | |
681 | Gifts by the List | [
"constructive algorithms",
"dfs and similar",
"graphs",
"trees"
] | null | null | Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are *n* men in Sasha's family, so let's number them with integers from 1 to *n*.
Each man has at most one father but may have arbitrary number of sons.
Man number *A* is considered to be the ancestor of the man number *B* if at least one of the following conditions is satisfied:
- *A*<==<=*B*; - the man number *A* is the father of the man number *B*; - there is a man number *C*, such that the man number *A* is his ancestor and the man number *C* is the father of the man number *B*.
Of course, if the man number *A* is an ancestor of the man number *B* and *A*<=≠<=*B*, then the man number *B* is not an ancestor of the man number *A*.
The tradition of the Sasha's family is to give gifts at the Man's Day. Because giving gifts in a normal way is boring, each year the following happens.
1. A list of candidates is prepared, containing some (possibly all) of the *n* men in some order. 1. Each of the *n* men decides to give a gift. 1. In order to choose a person to give a gift to, man *A* looks through the list and picks the first man *B* in the list, such that *B* is an ancestor of *A* and gives him a gift. Note that according to definition it may happen that a person gives a gift to himself. 1. If there is no ancestor of a person in the list, he becomes sad and leaves the celebration without giving a gift to anyone.
This year you have decided to help in organizing celebration and asked each of the *n* men, who do they want to give presents to (this person is chosen only among ancestors). Are you able to make a list of candidates, such that all the wishes will be satisfied if they give gifts according to the process described above? | In the first line of the input two integers *n* and *m* (0<=≤<=*m*<=<<=*n*<=≤<=100<=000) are given — the number of the men in the Sasha's family and the number of family relations in it respectively.
The next *m* lines describe family relations: the (*i*<=+<=1)*th* line consists of pair of integers *p**i* and *q**i* (1<=≤<=*p**i*,<=*q**i*<=≤<=*n*, *p**i*<=≠<=*q**i*) meaning that the man numbered *p**i* is the father of the man numbered *q**i*. It is guaranteed that every pair of numbers appears at most once, that among every pair of two different men at least one of them is not an ancestor of another and that every man has at most one father.
The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*), *i**th* of which means that the man numbered *i* wants to give a gift to the man numbered *a**i*. It is guaranteed that for every 1<=≤<=*i*<=≤<=*n* the man numbered *a**i* is an ancestor of the man numbered *i*. | Print an integer *k* (1<=≤<=*k*<=≤<=*n*) — the number of the men in the list of candidates, in the first line.
Print then *k* pairwise different positive integers not exceeding *n* — the numbers of the men in the list in an order satisfying every of the men's wishes, one per line.
If there are more than one appropriate lists, print any of them. If there is no appropriate list print <=-<=1 in the only line. | [
"3 2\n1 2\n2 3\n1 2 1\n",
"4 2\n1 2\n3 4\n1 2 3 3\n"
] | [
"-1",
"3\n2\n1\n3\n"
] | The first sample explanation:
- if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (*a*<sub class="lower-index">1</sub> = *a*<sub class="lower-index">3</sub> = 1); - if there would be no 2 in the list then the second man wish would not be satisfied (*a*<sub class="lower-index">2</sub> = 2); - if 1 would stay before 2 in the answer then the second man would have to give his gift to the first man, but he wants to give it to himself (*a*<sub class="lower-index">2</sub> = 2). - if, at the other hand, the man numbered 2 would stay before the man numbered 1, then the third man would have to give his gift to the second man, but not to the first (*a*<sub class="lower-index">3</sub> = 1). | [
{
"input": "3 2\n1 2\n2 3\n1 2 1",
"output": "-1"
},
{
"input": "4 2\n1 2\n3 4\n1 2 3 3",
"output": "3\n2\n1\n3"
},
{
"input": "1 0\n1",
"output": "1\n1"
},
{
"input": "2 1\n2 1\n2 2",
"output": "1\n2"
},
{
"input": "2 1\n2 1\n1 2",
"output": "2\n1\n2"
},
... | 31 | 102,400 | 0 | 70,903 | |
623 | Birthday | [
"greedy",
"math",
"probabilities"
] | null | null | A MIPT student named Misha has a birthday today, and he decided to celebrate it in his country house in suburban Moscow. *n* friends came by, and after a typical party they decided to play blind man's buff.
The birthday boy gets blindfolded and the other players scatter around the house. The game is played in several rounds. In each round, Misha catches exactly one of his friends and has to guess who it is. The probability of catching the *i*-th friend does not change between rounds and is equal to *p**i* percent (as we know, it is directly proportional to the amount of alcohol consumed by the *i*-th friend) and *p*1<=+<=*p*2<=+<=...<=+<=*p**n*<==<=100 holds. Misha has no information about who he caught. After Misha makes an attempt to guess the caught person, the round ends. Even then, Misha isn't told whether he guessed correctly, and a new round begins.
The game ends when Misha guesses every friend at least once, that is, there exists such set of rounds *k*1,<=*k*2,<=...,<=*k**n*, that during round number *k**i* Misha caught the *i*-th friend and guessed him. Misha wants to minimize the expectation of the number of rounds of the game. Despite the fact that at any point in the game Misha has no information about who he has already guessed, his friends are honest, and if they see that the condition for the end of the game is fulfilled, the game ends immediately. Find the expectation of the number of rounds in the game if Misha plays optimally. | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100) — the number of Misha's friends.
The second line contains *n* integers *p**i* (), giving the probability to catch the *i*-th friend in one particular round in percent. | Print a single real value — the expectation of the number of rounds provided that Misha plays optimally. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if . | [
"2\n50 50\n",
"4\n50 20 20 10\n"
] | [
"5.0000000000\n",
"39.2846263444\n"
] | The optimal strategy in the first sample is to guess friends alternately. | [
{
"input": "2\n50 50",
"output": "5.0000000000"
},
{
"input": "4\n50 20 20 10",
"output": "39.2846263444"
},
{
"input": "1\n100",
"output": "1.0000000000"
},
{
"input": "2\n41 59",
"output": "5.0966935927"
},
{
"input": "6\n14 14 18 21 20 13",
"output": "83.75... | 498 | 921,600 | 0 | 71,079 | |
60 | Mushroom Strife | [
"brute force",
"dfs and similar"
] | C. Mushroom Strife | 2 | 256 | Pasha and Akim were making a forest map — the lawns were the graph's vertexes and the roads joining the lawns were its edges. They decided to encode the number of laughy mushrooms on every lawn in the following way: on every edge between two lawns they wrote two numbers, the greatest common divisor (GCD) and the least common multiple (LCM) of the number of mushrooms on these lawns. But one day Pasha and Akim had an argument about the laughy mushrooms and tore the map. Pasha was left with just some part of it, containing only *m* roads. Your task is to help Pasha — use the map he has to restore the number of mushrooms on every lawn. As the result is not necessarily unique, help Pasha to restore any one or report that such arrangement of mushrooms does not exist. It is guaranteed that the numbers on the roads on the initial map were no less that 1 and did not exceed 106. | The first line contains two numbers *n* and *m* () which are the numbers of lawns and roads we know about. Each of the following *m* lines contains four numbers which are the numbers of lawns the road connects, the GCD and the LCM of the numbers of mushrooms on these lawns (1<=≤<=*GCD*,<=*LCM*<=≤<=106).
It is guaranteed, that no road connects lawn to itself, and no two lawns are connected by more than one road. | The answer should contain "YES" or "NO" on the first line, saying whether it is possible or not to perform the arrangement. If the answer is "YES", print on the following line *n* numbers which are the numbers of mushrooms on the corresponding lawns. | [
"1 0\n",
"2 1\n1 2 1 3\n",
"3 2\n3 2 1 2\n3 1 1 10\n",
"2 1\n1 2 3 7\n"
] | [
"YES\n1 ",
"YES\n1 3 ",
"YES\n5 1 2 ",
"NO\n"
] | none | [
{
"input": "1 0",
"output": "YES\n1 "
},
{
"input": "2 1\n1 2 1 3",
"output": "YES\n1 3 "
},
{
"input": "3 2\n3 2 1 2\n3 1 1 10",
"output": "YES\n5 1 2 "
},
{
"input": "2 1\n1 2 3 7",
"output": "NO"
},
{
"input": "3 2\n3 2 1 34\n3 1 1 26",
"output": "YES\n13 1... | 62 | 0 | 0 | 71,274 |
119 | Education Reform | [
"dp"
] | null | null | Yet another education system reform has been carried out in Berland recently. The innovations are as follows:
An academic year now consists of *n* days. Each day pupils study exactly one of *m* subjects, besides, each subject is studied for no more than one day. After the lessons of the *i*-th subject pupils get the home task that contains no less than *a**i* and no more than *b**i* exercises. Besides, each subject has a special attribute, the complexity (*c**i*). A school can make its own timetable, considering the following conditions are satisfied:
- the timetable should contain the subjects in the order of the complexity's strict increasing; - each day, except for the first one, the task should contain either *k* times more exercises, or more by *k* compared to the previous day (more formally: let's call the number of home task exercises in the *i*-th day as *x**i*, then for each *i* (1<=<<=*i*<=≤<=*n*): either *x**i*<==<=*k*<=+<=*x**i*<=-<=1 or *x**i*<==<=*k*·*x**i*<=-<=1 must be true); - the total number of exercises in all home tasks should be maximal possible.
All limitations are separately set for each school.
It turned out that in many cases *a**i* and *b**i* reach 1016 (however, as the Berland Minister of Education is famous for his love to half-measures, the value of *b**i*<=-<=*a**i* doesn't exceed 100). That also happened in the Berland School №256. Nevertheless, you as the school's principal still have to work out the timetable for the next academic year... | The first line contains three integers *n*, *m*, *k* (1<=≤<=*n*<=≤<=*m*<=≤<=50, 1<=≤<=*k*<=≤<=100) which represent the number of days in an academic year, the number of subjects and the *k* parameter correspondingly. Each of the following *m* lines contains the description of a subject as three integers *a**i*, *b**i*, *c**i* (1<=≤<=*a**i*<=≤<=*b**i*<=≤<=1016, *b**i*<=-<=*a**i*<=≤<=100, 1<=≤<=*c**i*<=≤<=100) — two limitations to the number of exercises on the *i*-th subject and the complexity of the *i*-th subject, correspondingly. Distinct subjects can have the same complexity. The subjects are numbered with integers from 1 to *m*.
Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is preferred to use the cin stream or the %I64d specificator. | If no valid solution exists, print the single word "NO" (without the quotes). Otherwise, the first line should contain the word "YES" (without the quotes) and the next *n* lines should contain any timetable that satisfies all the conditions. The *i*<=+<=1-th line should contain two positive integers: the number of the subject to study on the *i*-th day and the number of home task exercises given for this subject. The timetable should contain exactly *n* subjects. | [
"4 5 2\n1 10 1\n1 10 2\n1 10 3\n1 20 4\n1 100 5\n",
"3 4 3\n1 3 1\n2 4 4\n2 3 3\n2 2 2\n"
] | [
"YES\n2 8\n3 10\n4 20\n5 40\n",
"NO"
] | none | [] | 15 | 0 | 0 | 71,305 | |
0 | none | [
"none"
] | null | null | As a German University in Cairo (GUC) student and a basketball player, Herr Wafa was delighted once he heard the news. GUC is finally participating in the Annual Basketball Competition (ABC).
A team is to be formed of *n* players, all of which are GUC students. However, the team might have players belonging to different departments. There are *m* departments in GUC, numbered from 1 to *m*. Herr Wafa's department has number *h*. For each department *i*, Herr Wafa knows number *s**i* — how many students who play basketball belong to this department.
Herr Wafa was also able to guarantee a spot on the team, using his special powers. But since he hates floating-point numbers, he needs your help at finding the probability that he will have at least one teammate belonging to his department.
Note that every possible team containing Herr Wafa is equally probable. Consider all the students different from each other. | The first line contains three integers *n*, *m* and *h* (1<=≤<=*n*<=≤<=100,<=1<=≤<=*m*<=≤<=1000,<=1<=≤<=*h*<=≤<=*m*) — the number of players on the team, the number of departments in GUC and Herr Wafa's department, correspondingly.
The second line contains a single-space-separated list of *m* integers *s**i* (1<=≤<=*s**i*<=≤<=100), denoting the number of students in the *i*-th department. Note that *s**h* includes Herr Wafa. | Print the probability that Herr Wafa will have at least one teammate from his department. If there is not enough basketball players in GUC to participate in ABC, print -1. The answer will be accepted if it has absolute or relative error not exceeding 10<=-<=6. | [
"3 2 1\n2 1\n",
"3 2 1\n1 1\n",
"3 2 1\n2 2\n"
] | [
"1\n",
"-1\n",
"0.666667\n"
] | In the first example all 3 players (2 from department 1 and 1 from department 2) must be chosen for the team. Both players from Wafa's departments will be chosen, so he's guaranteed to have a teammate from his department.
In the second example, there are not enough players.
In the third example, there are three possibilities to compose the team containing Herr Wafa. In two of them the other player from Herr Wafa's department is part of the team. | [
{
"input": "3 2 1\n2 1",
"output": "1"
},
{
"input": "3 2 1\n1 1",
"output": "-1"
},
{
"input": "3 2 1\n2 2",
"output": "0.666667"
},
{
"input": "3 2 1\n1 2",
"output": "0.000000"
},
{
"input": "6 5 3\n5 2 3 10 5",
"output": "0.380435"
},
{
"input": "7... | 202 | 2,048,000 | -1 | 71,317 | |
38 | Vasya the Architect | [
"implementation"
] | D. Vasya the Architect | 2 | 256 | Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart.
Let us consider the building process. Vasya takes a brick and puts it on top of the already built tower so that the sides of the brick are parallel to the sides of the bricks he has already used. Let's introduce a Cartesian coordinate system on the horizontal plane, where Vasya puts the first brick. Then the projection of brick number *i* on the plane is a square with sides parallel to the axes of coordinates with opposite corners in points (*x**i*,<=1,<=*y**i*,<=1) and (*x**i*,<=2,<=*y**i*,<=2). The bricks are cast from homogeneous plastic and the weight of a brick *a*<=×<=*a*<=×<=*a* is *a*3 grams.
It is guaranteed that Vasya puts any brick except the first one on the previous one, that is the area of intersection of the upper side of the previous brick and the lower side of the next brick is always positive.
We (Vasya included) live in a normal world where the laws of physical statics work. And that is why, perhaps, if we put yet another brick, the tower will collapse under its own weight. Vasya puts the cubes consecutively one on top of the other until at least one cube loses the balance and falls down. If it happens, Vasya gets upset and stops the construction. Print the number of bricks in the maximal stable tower, that is the maximal number *m* satisfying the condition that all the towers consisting of bricks 1, 2, ..., *k* for every integer *k* from 1 to *m* remain stable. | The first input file contains an integer *n* (1<=≤<=*n*<=≤<=100) which is the number of bricks. Each of the next *n* lines contains four numbers *x**i*,<=1,<=*y**i*,<=1,<=*x**i*,<=2,<=*y**i*,<=2 (*x**i*,<=1<=≠<=*x**i*,<=2,<=|*x**i*,<=1<=-<=*x**i*,<=2|<==<=|*y**i*,<=1<=-<=*y**i*,<=2|) which are the coordinates of the opposite angles of the base of the brick number *i*. The coordinates are integers and their absolute value does not exceed 50.
The cubes are given in the order Vasya puts them. It is guaranteed that the area of intersection of the upper side of the brick number *i*<=-<=1 and the lower side of the brick number *i* is strictly strictly greater than zero for all *i*<=≥<=2. | Print the number of bricks in the maximal stable tower. | [
"2\n0 0 3 3\n1 0 4 3\n",
"2\n0 0 3 3\n2 0 5 3\n",
"3\n0 0 3 3\n1 0 4 3\n2 0 5 3\n"
] | [
"2\n",
"1\n",
"3\n"
] | none | [
{
"input": "2\n0 0 3 3\n1 0 4 3",
"output": "2"
},
{
"input": "2\n0 0 3 3\n2 0 5 3",
"output": "1"
},
{
"input": "3\n0 0 3 3\n1 0 4 3\n2 0 5 3",
"output": "3"
},
{
"input": "5\n7 -10 -8 5\n4 -7 -5 2\n2 -5 -3 0\n-9 48 50 -11\n50 -4 -2 48",
"output": "3"
},
{
"input... | 278 | 3,686,400 | 0 | 71,452 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.