contestId
int64 0
1.01k
| name
stringlengths 2
58
| tags
sequencelengths 0
11
| title
stringclasses 523
values | time-limit
stringclasses 8
values | memory-limit
stringclasses 8
values | problem-description
stringlengths 0
7.15k
| input-specification
stringlengths 0
2.05k
| output-specification
stringlengths 0
1.5k
| demo-input
sequencelengths 0
7
| demo-output
sequencelengths 0
7
| note
stringlengths 0
5.24k
| test_cases
listlengths 0
402
| timeConsumedMillis
int64 0
8k
| memoryConsumedBytes
int64 0
537M
| score
float64 -1
3.99
| __index_level_0__
int64 0
621k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
109 | Lucky Tree | [
"dp",
"dsu",
"trees"
] | C. Lucky Tree | 2 | 256 | Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya encountered a tree with *n* vertexes. Besides, the tree was weighted, i. e. each edge of the tree has weight (a positive integer). An edge is lucky if its weight is a lucky number. Note that a tree with *n* vertexes is an undirected connected graph that has exactly *n*<=-<=1 edges.
Petya wondered how many vertex triples (*i*,<=*j*,<=*k*) exists that on the way from *i* to *j*, as well as on the way from *i* to *k* there must be at least one lucky edge (all three vertexes are pairwise distinct). The order of numbers in the triple matters, that is, the triple (1,<=2,<=3) is not equal to the triple (2,<=1,<=3) and is not equal to the triple (1,<=3,<=2).
Find how many such triples of vertexes exist. | The first line contains the single integer *n* (1<=β€<=*n*<=β€<=105) β the number of tree vertexes. Next *n*<=-<=1 lines contain three integers each: *u**i* *v**i* *w**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=1<=β€<=*w**i*<=β€<=109) β the pair of vertexes connected by the edge and the edge's weight. | On the single line print the single number β the answer.
Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is recommended to use the cin, cout streams or the %I64d specificator. | [
"4\n1 2 4\n3 1 2\n1 4 7\n",
"4\n1 2 4\n1 3 47\n1 4 7447\n"
] | [
"16\n",
"24\n"
] | The 16 triples of vertexes from the first sample are: (1,β2,β4),β(1,β4,β2),β(2,β1,β3),β(2,β1,β4),β(2,β3,β1),β(2,β3,β4),β(2,β4,β1),β(2,β4,β3),β(3,β2,β4),β(3,β4,β2),β(4,β1,β2),β(4,β1,β3),β(4,β2,β1),β(4,β2,β3),β(4,β3,β1),β(4,β3,β2).
In the second sample all the triples should be counted: 4Β·3Β·2β=β24. | [
{
"input": "4\n1 2 4\n3 1 2\n1 4 7",
"output": "16"
},
{
"input": "4\n1 2 4\n1 3 47\n1 4 7447",
"output": "24"
},
{
"input": "9\n1 2 7\n1 3 12\n4 1 2\n4 5 4\n4 6 47\n4 7 9\n5 8 2\n5 9 1",
"output": "282"
},
{
"input": "2\n1 2 7",
"output": "0"
},
{
"input": "2\n2 1 1000000000",
"output": "0"
},
{
"input": "9\n1 2 1\n1 3 7\n3 4 19\n3 5 2\n4 6 46\n7 4 25\n5 8 64\n5 9 73",
"output": "98"
},
{
"input": "7\n1 2 47\n1 3 9\n3 7 7\n3 4 2\n3 5 4\n3 6 1",
"output": "114"
},
{
"input": "5\n1 2 1\n2 3 1\n3 4 1\n4 5 2",
"output": "0"
},
{
"input": "7\n1 2 4\n2 3 7\n3 4 44\n4 5 47\n5 6 74\n6 7 77",
"output": "210"
},
{
"input": "5\n1 2 1000000000\n2 3 747774\n3 4 4\n4 5 8447854",
"output": "36"
},
{
"input": "5\n1 2 1\n2 3 4\n3 4 4\n4 5 1",
"output": "36"
},
{
"input": "9\n9 7 4\n7 2 10\n2 3 28\n2 1 1\n1 6 47\n1 5 7\n1 4 4\n1 8 2",
"output": "284"
},
{
"input": "10\n9 1 4\n9 2 7\n9 3 74447\n9 4 744\n9 5 777777777\n9 6 447477\n9 7 74\n9 8 777\n4 10 977",
"output": "688"
},
{
"input": "10\n9 2 6\n5 3 7\n7 8 9\n2 1 7\n8 6 3\n1 4 5\n3 10 7\n7 4 3\n6 3 5",
"output": "328"
},
{
"input": "10\n4 8 480392999\n3 4 32525297\n9 7 417904789\n6 2 836294777\n5 3 616099185\n1 7 830574407\n8 10 838073755\n6 10 547050646\n2 1 12607780",
"output": "0"
}
] | 466 | 2,867,200 | -1 | 116,334 |
180 | Cubes | [
"binary search",
"dp",
"two pointers"
] | null | null | Let's imagine that you're playing the following simple computer game. The screen displays *n* lined-up cubes. Each cube is painted one of *m* colors. You are allowed to delete not more than *k* cubes (that do not necessarily go one after another). After that, the remaining cubes join together (so that the gaps are closed) and the system counts the score. The number of points you score equals to the length of the maximum sequence of cubes of the same color that follow consecutively. Write a program that determines the maximum possible number of points you can score.
Remember, you may delete no more than *k* any cubes. It is allowed not to delete cubes at all. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*<=β€<=2Β·105,<=1<=β€<=*m*<=β€<=105,<=0<=β€<=*k*<=<<=*n*). The second line contains *n* integers from 1 to *m* β the numbers of cube colors. The numbers of colors are separated by single spaces. | Print the maximum possible number of points you can score. | [
"10 3 2\n1 2 1 1 3 2 1 1 2 2\n",
"10 2 2\n1 2 1 2 1 1 2 1 1 2\n",
"3 1 2\n1 1 1\n"
] | [
"4\n",
"5\n",
"3\n"
] | In the first sample you should delete the fifth and the sixth cubes.
In the second sample you should delete the fourth and the seventh cubes.
In the third sample you shouldn't delete any cubes. | [
{
"input": "10 3 2\n1 2 1 1 3 2 1 1 2 2",
"output": "4"
},
{
"input": "10 2 2\n1 2 1 2 1 1 2 1 1 2",
"output": "5"
},
{
"input": "3 1 2\n1 1 1",
"output": "3"
},
{
"input": "10 2 2\n1 1 1 2 1 2 1 2 1 1",
"output": "5"
},
{
"input": "1 1 0\n1",
"output": "1"
},
{
"input": "20 3 5\n2 2 3 1 2 2 3 3 3 2 1 2 3 1 1 3 3 3 2 3",
"output": "7"
},
{
"input": "20 2 5\n2 2 1 2 1 2 1 2 1 1 2 1 2 2 1 2 2 1 2 1",
"output": "7"
},
{
"input": "20 6 3\n4 1 2 6 3 3 2 5 2 5 2 1 1 4 1 2 2 1 1 4",
"output": "5"
},
{
"input": "30 5 8\n1 4 1 5 3 4 4 1 1 4 1 3 5 5 5 5 1 5 1 5 2 3 2 2 3 4 5 2 1 2",
"output": "7"
},
{
"input": "30 5 6\n4 2 2 1 3 4 2 3 2 4 3 1 1 4 4 3 5 1 4 5 5 1 2 2 1 2 4 4 1 2",
"output": "4"
},
{
"input": "100 10 15\n6 6 6 6 7 7 8 8 4 4 4 1 1 7 7 7 1 1 1 2 2 2 2 2 2 2 2 2 10 5 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 6 6 10 2 2 8 8 1 1 1 1 1 6 6 6 6 2 2 3 3 9 9 9 9 9 10 10 10 10 10 4 9 9 9 7 7 7 7 9 9 7 7 5 8 8 8 8 2",
"output": "25"
},
{
"input": "99 10 17\n3 2 2 9 7 10 10 10 10 6 6 6 3 7 3 3 7 2 2 2 2 2 10 10 2 2 7 7 7 7 1 8 8 8 8 10 9 10 10 10 5 5 2 2 5 5 5 1 4 9 9 2 2 3 3 2 2 9 9 9 9 9 9 9 7 4 8 8 4 8 8 10 10 4 5 9 9 10 5 5 5 5 5 8 8 8 8 2 2 2 2 1 8 8 5 10 10 2 2",
"output": "11"
},
{
"input": "94 10 20\n2 3 4 4 4 4 4 4 4 4 4 4 5 5 5 5 3 3 3 3 6 6 5 1 5 5 5 2 2 2 2 4 1 1 1 1 8 8 10 5 2 2 4 4 4 4 4 3 3 3 3 3 6 6 6 6 2 2 2 2 2 2 2 2 1 10 10 2 2 2 6 6 6 8 4 4 4 8 1 1 1 1 1 1 6 6 2 2 8 7 7 7 3 4",
"output": "13"
},
{
"input": "99 3 15\n2 2 2 2 2 2 3 3 1 1 1 1 1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 3 3 3 1 1 1 3 3 3 3 3 3 3 1 1 1 1 3 3 3 3 3 3 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 2 2 2 2 2",
"output": "27"
},
{
"input": "100 5 10\n4 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 4 4 4 2 2 2 3 3 3 3 3 3 3 4 4 4 3 3 2 1 1 1 2 3 3 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 4 4 4 4 5 4 4 4 4 4 4 4 3 3 3 3 3 3 3 3 3 5 5 5 3 3 4 3 3 3",
"output": "21"
},
{
"input": "98 4 20\n3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 2 2 2 2 2 2 2 1 1 1 3 3 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 2 2 2 2 2 2 2 3 3 1 1 2 2 2 2 3 3 3",
"output": "30"
},
{
"input": "92 5 40\n3 3 3 3 2 2 2 2 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 3 3 5 3 3 3 4 4 4 1 1 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 3 3 3 2 5 1 1 4 4 4 4 4 4 4 4 4 4 4 2 2 4 4 5 5 5 5 5 5 5 5 5 2 2 2 2 2",
"output": "33"
},
{
"input": "99 10 10\n9 9 9 10 10 10 9 9 9 9 9 2 2 10 10 10 10 10 3 3 5 10 10 2 2 3 3 6 1 1 1 1 1 1 7 7 7 7 7 4 4 6 6 6 8 9 9 9 2 2 9 9 5 5 5 5 1 10 7 7 9 9 9 5 6 6 6 6 8 8 4 1 3 3 3 3 3 3 9 9 4 1 1 7 1 1 1 3 3 3 3 3 3 10 9 10 9 8 9",
"output": "12"
},
{
"input": "95 10 30\n3 3 8 8 8 4 9 3 3 3 3 3 3 8 10 5 5 5 5 5 5 4 9 1 1 1 1 6 6 7 7 7 1 1 1 1 1 1 9 9 10 10 10 10 10 5 3 3 3 3 3 3 6 6 6 6 1 6 6 6 6 9 4 9 5 5 5 2 2 2 2 10 10 8 3 3 4 2 9 9 9 2 5 2 2 8 8 8 7 7 3 3 3 4 4",
"output": "11"
},
{
"input": "100 10 15\n7 7 3 6 6 6 8 8 8 8 8 8 8 8 8 8 8 5 5 1 9 9 9 9 9 9 9 9 2 2 2 4 7 7 8 2 2 2 2 2 2 8 8 7 7 2 2 2 7 7 7 4 4 4 4 4 4 4 4 4 4 7 7 7 7 7 7 7 7 2 2 2 6 6 3 3 3 3 3 3 1 1 1 1 1 1 4 4 4 4 1 1 1 1 5 4 5 6 6 6",
"output": "13"
}
] | 92 | 0 | 0 | 116,366 |
|
690 | Recover Polygon (easy) | [] | null | null | The zombies are gathering in their secret lair! Heidi will strike hard to destroy them once and for all. But there is a little problem... Before she can strike, she needs to know where the lair is. And the intel she has is not very good.
Heidi knows that the lair can be represented as a rectangle on a lattice, with sides parallel to the axes. Each vertex of the polygon occupies an integer point on the lattice. For each cell of the lattice, Heidi can check the level of Zombie Contamination. This level is an integer between 0 and 4, equal to the number of corners of the cell that are inside or on the border of the rectangle.
As a test, Heidi wants to check that her Zombie Contamination level checker works. Given the output of the checker, Heidi wants to know whether it could have been produced by a single non-zero area rectangular-shaped lair (with axis-parallel sides). | The first line of each test case contains one integer *N*, the size of the lattice grid (5<=β€<=*N*<=β€<=50). The next *N* lines each contain *N* characters, describing the level of Zombie Contamination of each cell in the lattice. Every character of every line is a digit between 0 and 4.
Cells are given in the same order as they are shown in the picture above: rows go in the decreasing value of *y* coordinate, and in one row cells go in the order of increasing *x* coordinate. This means that the first row corresponds to cells with coordinates (1,<=*N*),<=...,<=(*N*,<=*N*) and the last row corresponds to cells with coordinates (1,<=1),<=...,<=(*N*,<=1). | The first line of the output should contain Yes if there exists a single non-zero area rectangular lair with corners on the grid for which checking the levels of Zombie Contamination gives the results given in the input, and No otherwise. | [
"6\n000000\n000000\n012100\n024200\n012100\n000000\n"
] | [
"Yes\n"
] | The lair, if it exists, has to be rectangular (that is, have corners at some grid points with coordinates (*x*<sub class="lower-index">1</sub>,β*y*<sub class="lower-index">1</sub>), (*x*<sub class="lower-index">1</sub>,β*y*<sub class="lower-index">2</sub>), (*x*<sub class="lower-index">2</sub>,β*y*<sub class="lower-index">1</sub>), (*x*<sub class="lower-index">2</sub>,β*y*<sub class="lower-index">2</sub>)), has a non-zero area and be contained inside of the grid (that is, 0ββ€β*x*<sub class="lower-index">1</sub>β<β*x*<sub class="lower-index">2</sub>ββ€β*N*, 0ββ€β*y*<sub class="lower-index">1</sub>β<β*y*<sub class="lower-index">2</sub>ββ€β*N*), and result in the levels of Zombie Contamination as reported in the input. | [
{
"input": "6\n000000\n000000\n012100\n024200\n012100\n000000",
"output": "Yes"
},
{
"input": "6\n000000\n012210\n024420\n012210\n000000\n000000",
"output": "Yes"
},
{
"input": "6\n000100\n001210\n002420\n001210\n000000\n000000",
"output": "No"
},
{
"input": "10\n0000000000\n0122210000\n0244420100\n0122210000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000",
"output": "No"
},
{
"input": "10\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0000000000\n0012100000\n0024200000\n0012100000\n0000000000",
"output": "Yes"
},
{
"input": "9\n000000000\n000000000\n012221000\n024442000\n012221000\n000000000\n000000000\n000000010\n000000000",
"output": "No"
},
{
"input": "9\n000000000\n012222100\n024444200\n024444200\n024444200\n024444200\n024444200\n012222100\n000000000",
"output": "Yes"
},
{
"input": "8\n00000000\n00001210\n00002420\n00002020\n00001210\n00000000\n00000000\n00000000",
"output": "No"
},
{
"input": "8\n00000000\n00000000\n01210000\n02420000\n01210000\n00000000\n00000000\n00000000",
"output": "Yes"
},
{
"input": "7\n0000000\n0000000\n0000000\n1122210\n0244420\n0122210\n0000000",
"output": "No"
},
{
"input": "7\n0000000\n0012210\n0024420\n0012210\n0000000\n0000000\n0000000",
"output": "Yes"
},
{
"input": "6\n000000\n000000\n001100\n001200\n000000\n000000",
"output": "No"
},
{
"input": "6\n000000\n000000\n002200\n002200\n000000\n000000",
"output": "No"
},
{
"input": "6\n000000\n000000\n003300\n003300\n000000\n000000",
"output": "No"
},
{
"input": "6\n000000\n001100\n013310\n013310\n001100\n000000",
"output": "No"
}
] | 62 | 0 | 0 | 116,804 |
|
346 | Doodle Jump | [
"math",
"number theory"
] | null | null | In Doodle Jump the aim is to guide a four-legged creature called "The Doodler" up a never-ending series of platforms without falling. β Wikipedia.
It is a very popular game and xiaodao likes it very much. One day when playing the game she wondered whether there exists a platform that the doodler couldn't reach due to the limits of its jumping ability. Consider the following problem.
There are *n* platforms. The height of the *x*-th (1<=β€<=*x*<=β€<=*n*) platform is *a*Β·*x* mod *p*, where *a* and *p* are positive co-prime integers. The maximum possible height of a Doodler's jump is *h*. That is, it can jump from height *h*1 to height *h*2 (*h*1<=<<=*h*2) if *h*2<=-<=*h*1<=β€<=*h*. Initially, the Doodler is on the ground, the height of which is 0. The question is whether it can reach the highest platform or not.
For example, when *a*<==<=7, *n*<==<=4, *p*<==<=12, *h*<==<=2, the heights of the platforms are 7, 2, 9, 4 as in the picture below. With the first jump the Doodler can jump to the platform at height 2, with the second one the Doodler can jump to the platform at height 4, but then it can't jump to any of the higher platforms. So, it can't reach the highest platform.
User xiaodao thought about the problem for a long time but didn't solve it, so she asks you for help. Also, she has a lot of instances of the problem. Your task is solve all of these instances. | The first line contains an integer *t* (1<=β€<=*t*<=β€<=104) β the number of problem instances. Each of the next *t* lines contains four integers *a*, *n*, *p* and *h* (1<=β€<=*a*<=β€<=109, 1<=β€<=*n*<=<<=*p*<=β€<=109, 0<=β€<=*h*<=β€<=109). It's guaranteed that *a* and *p* are co-prime. | For each problem instance, if the Doodler can reach the highest platform, output "YES", otherwise output "NO". | [
"3\n7 4 12 2\n7 1 9 4\n7 4 12 3\n"
] | [
"NO\nNO\nYES\n"
] | none | [] | 2,000 | 102,400 | 0 | 116,910 |
|
546 | Soldier and Badges | [
"brute force",
"greedy",
"implementation",
"sortings"
] | null | null | Colonel has *n* badges. He wants to give one badge to every of his *n* soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin.
For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.
Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness. | First line of input consists of one integer *n* (1<=β€<=*n*<=β€<=3000).
Next line consists of *n* integers *a**i* (1<=β€<=*a**i*<=β€<=*n*), which stand for coolness factor of each badge. | Output single integer β minimum amount of coins the colonel has to pay. | [
"4\n1 3 1 4\n",
"5\n1 2 3 2 5\n"
] | [
"1",
"2"
] | In first sample test we can increase factor of first badge by 1.
In second sample test we can increase factors of the second and the third badge by 1. | [
{
"input": "4\n1 3 1 4",
"output": "1"
},
{
"input": "5\n1 2 3 2 5",
"output": "2"
},
{
"input": "5\n1 5 3 2 4",
"output": "0"
},
{
"input": "10\n1 1 2 3 4 5 6 7 8 9",
"output": "9"
},
{
"input": "11\n9 2 10 3 1 5 7 1 4 8 6",
"output": "10"
},
{
"input": "4\n4 3 2 2",
"output": "3"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "50\n49 37 30 2 18 48 14 48 50 27 1 43 46 5 21 28 44 2 24 17 41 38 25 18 43 28 25 21 28 23 26 27 4 31 50 18 23 11 13 28 44 47 1 26 43 25 22 46 32 45",
"output": "170"
},
{
"input": "50\n37 31 19 46 45 1 9 37 15 19 15 10 17 16 38 13 26 25 36 13 7 21 12 41 46 19 3 50 14 49 49 40 29 41 47 29 3 42 13 21 10 21 9 33 38 30 24 40 5 26",
"output": "135"
},
{
"input": "50\n18 13 50 12 23 29 31 44 28 29 33 31 17 38 27 37 36 34 40 4 27 2 8 27 50 27 21 28 11 13 47 25 15 26 9 15 22 3 22 45 9 12 5 5 46 44 23 34 12 25",
"output": "138"
},
{
"input": "50\n24 44 39 44 11 20 6 43 4 21 43 12 41 3 25 25 24 7 16 36 32 2 2 29 34 30 33 9 18 3 14 28 26 49 29 5 5 36 44 21 36 37 1 25 46 10 10 24 10 39",
"output": "128"
},
{
"input": "50\n7 5 18 2 7 12 8 20 41 4 7 3 7 10 22 1 19 9 20 10 23 3 6 3 30 13 6 18 3 3 18 38 9 7 2 1 2 5 25 10 13 1 8 34 1 26 13 8 13 2",
"output": "699"
},
{
"input": "50\n2 19 24 3 12 4 14 9 10 19 6 1 26 6 11 1 4 34 17 1 3 35 17 2 17 17 5 5 12 1 24 35 2 5 43 23 21 4 18 3 11 5 1 21 3 3 3 1 10 10",
"output": "692"
},
{
"input": "50\n2 2 4 19 5 7 2 35 3 12 1 18 17 16 40 4 15 36 1 11 13 3 14 1 4 10 1 12 43 7 9 9 4 3 28 9 12 12 1 33 3 23 11 24 20 20 2 4 26 4",
"output": "660"
},
{
"input": "50\n5 3 25 6 30 6 39 15 3 19 1 38 1 3 17 3 8 13 4 10 14 3 2 3 20 1 21 21 27 31 6 6 14 28 3 13 49 8 12 6 17 13 45 1 6 18 12 7 31 14",
"output": "574"
},
{
"input": "50\n10 25 27 13 28 35 40 39 3 6 18 29 44 1 26 2 45 36 9 46 41 12 33 19 8 22 15 48 34 20 11 32 1 47 43 23 7 5 14 30 31 21 38 42 24 49 4 37 16 17",
"output": "49"
},
{
"input": "50\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "1225"
},
{
"input": "50\n50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50",
"output": "1225"
},
{
"input": "3\n1 3 3",
"output": "1"
},
{
"input": "10\n4 4 4 4 4 4 5 5 5 5",
"output": "41"
},
{
"input": "4\n1 4 4 4",
"output": "3"
},
{
"input": "3\n1 1 1",
"output": "3"
},
{
"input": "3\n3 3 3",
"output": "3"
}
] | 30 | 0 | 0 | 116,973 |
|
294 | Shaass and Painter Robot | [
"brute force",
"implementation",
"number theory"
] | null | null | Shaass thinks a kitchen with all white floor tiles is so boring. His kitchen floor is made of *n*Β·*m* square tiles forming a *n*<=Γ<=*m* rectangle. Therefore he's decided to color some of the tiles in black so that the floor looks like a checkerboard, which is no two side-adjacent tiles should have the same color.
Shaass wants to use a painter robot to color the tiles. In the beginning the robot is standing in a border tile (*x**s*,<=*y**s*) facing a diagonal direction (i.e. upper-left, upper-right, down-left or down-right). As the robot walks in the kitchen he paints every tile he passes even if it's painted before. Painting each tile consumes one unit of black paint. If at any moment the robot hits a wall of the kitchen he changes his direction according the reflection rules. Note that a tile gets painted when the robot enters the tile from another tile, in other words changing direction in the same tile doesn't lead to any painting. The first tile the robot is standing on, is also painted.
The robot stops painting the first moment the floor is checkered. Given the dimensions of the kitchen and the position of the robot, find out the amount of paint the robot consumes before it stops painting the floor.
Let's consider an examples depicted below.
If the robot starts at tile number 1 (the tile (1,<=1)) of the left grid heading to down-right it'll pass tiles 1354236 and consumes 7 units of black paint on his way until he stops at tile number 6. But if it starts at tile number 1 in the right grid heading to down-right it will get stuck in a loop painting tiles 1, 2, and 3. | The first line of the input contains two integers *n* and *m*, (2<=β€<=*n*,<=*m*<=β€<=105). The second line contains two integers *x**s* and *y**s* (1<=β€<=*x**s*<=β€<=*n*,<=1<=β€<=*y**s*<=β€<=*m*) and the direction robot is facing initially. Direction is one of the strings: "UL" (upper-left direction), "UR" (upper-right), "DL" (down-left) or "DR" (down-right).
Note, that record (*x**s*,<=*y**s*) denotes the tile that is located at the *x**s*-th row from the top and at the *y**s*-th column from the left of the kitchen.
It's guaranteed that the starting position will be a border tile (a tile with less than four side-adjacent tiles). | Print the amount of paint the robot consumes to obtain a checkered kitchen floor. Or print -1 if it never happens.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specificator. | [
"3 4\n1 1 DR\n",
"3 4\n3 3 DR\n",
"3 3\n1 1 DR\n",
"3 3\n1 2 DL\n"
] | [
"7\n",
"11\n",
"-1\n",
"4\n"
] | none | [
{
"input": "3 4\n1 1 DR",
"output": "7"
},
{
"input": "3 4\n3 3 DR",
"output": "11"
},
{
"input": "3 3\n1 1 DR",
"output": "-1"
},
{
"input": "3 3\n1 2 DL",
"output": "4"
},
{
"input": "2 5\n2 3 DR",
"output": "7"
},
{
"input": "2 3\n2 3 DL",
"output": "3"
},
{
"input": "3 2\n3 1 DL",
"output": "3"
},
{
"input": "3 4\n1 1 DL",
"output": "7"
},
{
"input": "2 2\n2 2 UR",
"output": "2"
},
{
"input": "547 180\n96 1 UR",
"output": "135504"
},
{
"input": "657 213\n549 1 UL",
"output": "-1"
},
{
"input": "76 153\n1 74 UL",
"output": "11626"
},
{
"input": "80304 59860\n27585 59860 UR",
"output": "5862949615"
},
{
"input": "15954 45613\n15954 45142 UR",
"output": "-1"
},
{
"input": "66972 6779\n1 5667 UL",
"output": "778336963"
},
{
"input": "2622 82284\n2540 1 UL",
"output": "216075159"
},
{
"input": "48517 68036\n48517 31953 DR",
"output": "5618298349"
},
{
"input": "32132 32312\n1 2 DR",
"output": "1551509598"
},
{
"input": "100000 99999\n100000 1 DR",
"output": "9999700003"
},
{
"input": "100000 99999\n1 99999 DR",
"output": "9999700003"
},
{
"input": "84166 43542\n54265 43542 DR",
"output": "7214569537"
},
{
"input": "35185 19049\n31553 1 DR",
"output": "-1"
},
{
"input": "70834 99677\n1 23598 DL",
"output": "13418107690"
},
{
"input": "16730 65966\n12102 65966 DL",
"output": "1171670331"
},
{
"input": "52379 51718\n52379 807 UR",
"output": "3155460233"
},
{
"input": "93151 27224\n1 15542 UR",
"output": "3301236001"
},
{
"input": "39047 2730\n16937 1 UR",
"output": "195090753"
},
{
"input": "74696 83359\n74696 64325 UL",
"output": "11273566961"
},
{
"input": "99999 99999\n99999 99999 UR",
"output": "-1"
},
{
"input": "99999 99999\n99999 99998 UR",
"output": "-1"
},
{
"input": "2 2\n2 2 UR",
"output": "2"
},
{
"input": "2 2\n1 2 UR",
"output": "2"
},
{
"input": "9684 98536\n9684 80875 DL",
"output": "1908112615"
},
{
"input": "45333 74042\n1 43244 DL",
"output": "4559265901"
},
{
"input": "91228 54671\n1674 1 DL",
"output": "9870668501"
},
{
"input": "32001 30178\n32001 28119 UR",
"output": "1850848001"
},
{
"input": "77896 5684\n18467 5684 UR",
"output": "575273042"
},
{
"input": "13546 91435\n1002 1 UL",
"output": "-1"
},
{
"input": "64435 19953\n5705 19953 DR",
"output": "-1"
},
{
"input": "5208 582\n979 1 DR",
"output": "3617888"
},
{
"input": "51103 66871\n1 57465 DL",
"output": "-1"
},
{
"input": "86752 42377\n72660 42377 DL",
"output": "4398289793"
},
{
"input": "22402 28129\n22402 24340 DL",
"output": "-1"
},
{
"input": "73420 3635\n1 2940 UR",
"output": "332220976"
},
{
"input": "9070 74017\n4901 1 UR",
"output": "-1"
},
{
"input": "54965 59770\n54965 14083 UL",
"output": "4668092521"
},
{
"input": "95737 26059\n30821 26059 UL",
"output": "-1"
},
{
"input": "41633 11811\n5694 1 UL",
"output": "491662111"
},
{
"input": "93183 58113\n1 39140 DR",
"output": "5414953246"
},
{
"input": "28833 43865\n22853 1 DL",
"output": "-1"
},
{
"input": "74728 19372\n74728 18073 DL",
"output": "-1"
},
{
"input": "10378 85660\n7751 85660 DL",
"output": "-1"
},
{
"input": "61396 66289\n50382 1 UR",
"output": "-1"
},
{
"input": "97045 41795\n1 27338 UR",
"output": "4055829600"
},
{
"input": "32695 27547\n17514 27547 UL",
"output": "-1"
},
{
"input": "78590 3053\n78590 239 UL",
"output": "-1"
},
{
"input": "19363 78559\n1 55927 UL",
"output": "-1"
},
{
"input": "65258 49971\n20976 1 DR",
"output": "4857383821"
},
{
"input": "7593 1397\n1 1155 DL",
"output": "-1"
},
{
"input": "43242 82025\n19055 1 DL",
"output": "6371542297"
},
{
"input": "89137 57531\n89137 3552 DL",
"output": "5127940102"
},
{
"input": "29910 43284\n15956 43284 UR",
"output": "2517815394"
},
{
"input": "75805 18790\n46466 1 UR",
"output": "-1"
},
{
"input": "11455 85078\n11455 39494 UL",
"output": "-1"
},
{
"input": "47104 65707\n27797 65707 UL",
"output": "-1"
},
{
"input": "98122 41213\n19832 1 UL",
"output": "4804907081"
},
{
"input": "33772 26966\n1 6679 DR",
"output": "1005599068"
},
{
"input": "79667 2472\n62739 2472 DR",
"output": "301313741"
}
] | 60 | 0 | 0 | 117,186 |
|
246 | Blood Cousins Return | [
"binary search",
"data structures",
"dfs and similar",
"dp",
"sortings"
] | null | null | Polycarpus got hold of a family tree. The found tree describes the family relations of *n* people, numbered from 1 to *n*. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique.
We call the man with a number *a* a 1-ancestor of the man with a number *b*, if the man with a number *a* is a direct ancestor of the man with a number *b*.
We call the man with a number *a* a *k*-ancestor (*k*<=><=1) of the man with a number *b*, if the man with a number *b* has a 1-ancestor, and the man with a number *a* is a (*k*<=-<=1)-ancestor of the 1-ancestor of the man with a number *b*.
In the tree the family ties do not form cycles. In other words there isn't a person who is his own direct or indirect ancestor (that is, who is an *x*-ancestor of himself, for some *x*, *x*<=><=0).
We call a man with a number *a* the *k*-son of the man with a number *b*, if the man with a number *b* is a *k*-ancestor of the man with a number *a*.
Polycarpus is very much interested in how many sons and which sons each person has. He took a piece of paper and wrote *m* pairs of numbers *v**i*, *k**i*. Help him to learn for each pair *v**i*, *k**i* the number of distinct names among all names of the *k**i*-sons of the man with number *v**i*. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of people in the tree. Next *n* lines contain the description of people in the tree. The *i*-th line contains space-separated string *s**i* and integer *r**i* (0<=β€<=*r**i*<=β€<=*n*), where *s**i* is the name of the man with a number *i*, and *r**i* is either the number of the direct ancestor of the man with a number *i* or 0, if the man with a number *i* has no direct ancestor.
The next line contains a single integer *m* (1<=β€<=*m*<=β€<=105) β the number of Polycarpus's records. Next *m* lines contain space-separated pairs of integers. The *i*-th line contains integers *v**i*, *k**i* (1<=β€<=*v**i*,<=*k**i*<=β€<=*n*).
It is guaranteed that the family relationships do not form cycles. The names of all people are non-empty strings, consisting of no more than 20 lowercase English letters. | Print *m* whitespace-separated integers β the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input. | [
"6\npasha 0\ngerald 1\ngerald 1\nvalera 2\nigor 3\nolesya 1\n5\n1 1\n1 2\n1 3\n3 1\n6 1\n",
"6\nvalera 0\nvalera 1\nvalera 1\ngerald 0\nvalera 4\nkolya 4\n7\n1 1\n1 2\n2 1\n2 2\n4 1\n5 1\n6 1\n"
] | [
"2\n2\n0\n1\n0\n",
"1\n0\n0\n0\n2\n0\n0\n"
] | none | [
{
"input": "6\npasha 0\ngerald 1\ngerald 1\nvalera 2\nigor 3\nolesya 1\n5\n1 1\n1 2\n1 3\n3 1\n6 1",
"output": "2\n2\n0\n1\n0"
},
{
"input": "6\nvalera 0\nvalera 1\nvalera 1\ngerald 0\nvalera 4\nkolya 4\n7\n1 1\n1 2\n2 1\n2 2\n4 1\n5 1\n6 1",
"output": "1\n0\n0\n0\n2\n0\n0"
},
{
"input": "1\nc 0\n20\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0"
},
{
"input": "2\ndd 0\nh 0\n20\n2 1\n2 1\n1 1\n1 1\n1 1\n2 1\n2 1\n1 1\n1 1\n2 1\n2 1\n1 1\n1 1\n1 1\n1 1\n1 1\n2 1\n2 1\n1 1\n1 1",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0"
},
{
"input": "3\na 0\nhj 0\ng 2\n20\n1 1\n2 1\n2 1\n1 1\n2 1\n2 1\n1 1\n1 1\n1 1\n3 1\n2 1\n1 1\n2 1\n3 1\n3 1\n2 1\n3 1\n2 1\n1 1\n2 1",
"output": "0\n1\n1\n0\n1\n1\n0\n0\n0\n0\n1\n0\n1\n0\n0\n1\n0\n1\n0\n1"
},
{
"input": "3\ne 0\nb 0\nkg 0\n20\n1 1\n3 1\n3 1\n2 1\n1 1\n3 1\n2 1\n1 1\n2 1\n2 1\n2 1\n3 1\n3 1\n3 1\n1 1\n1 1\n2 1\n1 1\n2 1\n3 1",
"output": "0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0"
},
{
"input": "4\nbf 0\nc 0\njc 2\ni 3\n20\n3 1\n3 1\n4 1\n2 1\n1 1\n4 1\n4 1\n2 1\n2 1\n3 1\n4 1\n4 1\n1 1\n2 1\n4 1\n1 1\n1 1\n4 1\n1 1\n4 1",
"output": "1\n1\n0\n1\n0\n0\n0\n1\n1\n1\n0\n0\n0\n1\n0\n0\n0\n0\n0\n0"
},
{
"input": "4\nfh 0\neg 0\nc 1\nhb 3\n20\n4 1\n3 1\n1 1\n4 1\n2 1\n4 1\n3 1\n4 1\n3 1\n3 1\n2 1\n1 1\n4 1\n2 1\n2 1\n3 1\n3 1\n4 1\n1 1\n3 1",
"output": "0\n1\n1\n0\n0\n0\n1\n0\n1\n1\n0\n1\n0\n0\n0\n1\n1\n0\n1\n1"
}
] | 62 | 307,200 | -1 | 117,922 |
|
33 | Helper | [] | E. Helper | 2 | 256 | It's unbelievable, but an exam period has started at the OhWord University. It's even more unbelievable, that Valera got all the tests before the exam period for excellent work during the term. As now he's free, he wants to earn money by solving problems for his groupmates. He's made a *list* of subjects that he can help with. Having spoken with *n* of his groupmates, Valera found out the following information about them: what subject each of them passes, time of the exam and sum of money that each person is ready to pay for Valera's help.
Having this data, Valera's decided to draw up a timetable, according to which he will solve problems for his groupmates. For sure, Valera can't solve problems round the clock, that's why he's found for himself an optimum order of day and plans to stick to it during the whole exam period. Valera assigned time segments for sleep, breakfast, lunch and dinner. The rest of the time he can work.
Obviously, Valera can help a student with some subject, only if this subject is on the *list*. It happened, that all the students, to whom Valera spoke, have different, but one-type problems, that's why Valera can solve any problem of subject *list**i* in *t**i* minutes.
Moreover, if Valera starts working at some problem, he can break off only for sleep or meals, but he can't start a new problem, not having finished the current one. Having solved the problem, Valera can send it instantly to the corresponding student via the Internet.
If this student's exam hasn't started yet, he can make a crib, use it to pass the exam successfully, and pay Valera the promised sum. Since Valera has little time, he asks you to write a program that finds the order of solving problems, which can bring Valera maximum profit. | The first line contains integers *m*,<=*n*,<=*k* (1<=β€<=*m*,<=*n*<=β€<=100, 1<=β€<=*k*<=β€<=30) β amount of subjects on the *list*, amount of Valera's potential employers and the duration of the exam period in days.
The following *m* lines contain the names of subjects *list**i* (*list**i* is a non-empty string of at most 32 characters, consisting of lower case Latin letters). It's guaranteed that no two subjects are the same.
The (*m*<=+<=2)-th line contains *m* integers *t**i* (1<=β€<=*t**i*<=β€<=1000) β time in minutes that Valera spends to solve problems of the *i*-th subject. Then follow four lines, containing time segments for sleep, breakfast, lunch and dinner correspondingly.
Each line is in format H1:M1-H2:M2, where 00<=β€<= H1, H2 <=β€<=23, 00<=β€<= M1, M2 <=β€<=59. Time H1:M1 stands for the first minute of some Valera's action, and time H2:M2 stands for the last minute of this action. No two time segments cross. It's guaranteed that Valera goes to bed before midnight, gets up earlier than he has breakfast, finishes his breakfast before lunch, finishes his lunch before dinner, and finishes his dinner before midnight. All these actions last less than a day, but not less than one minute. Time of the beginning and time of the ending of each action are within one and the same day. But it's possible that Valera has no time for solving problems.
Then follow *n* lines, each containing the description of students. For each student the following is known: his exam subject *s**i* (*s**i* is a non-empty string of at most 32 characters, consisting of lower case Latin letters), index of the exam day *d**i* (1<=β€<=*d**i*<=β€<=*k*), the exam time *time**i*, and sum of money *c**i* (0<=β€<=*c**i*<=β€<=106, *c**i* β integer) that he's ready to pay for Valera's help. Exam time *time**i* is in the format HH:MM, where 00<=β€<= HH <=β€<=23, 00<=β€<= MM <=β€<=59. Valera will get money, if he finishes to solve the problem strictly before the corresponding student's exam begins. | In the first line output the maximum profit that Valera can get. The second line should contain number *p* β amount of problems that Valera is to solve. In the following *p* lines output the order of solving problems in chronological order in the following format: index of a student, to whom Valera is to help; index of the time, when Valera should start the problem; time, when Valera should start the problem (the first minute of his work); index of the day, when Valera should finish the problem; time, when Valera should finish the problem (the last minute of his work). To understand the output format better, study the sample tests. | [
"3 3 4\ncalculus\nalgebra\nhistory\n58 23 15\n00:00-08:15\n08:20-08:35\n09:30-10:25\n19:00-19:45\ncalculus 1 09:36 100\nenglish 4 21:15 5000\nhistory 1 19:50 50\n",
"2 2 1\nmatan\ncodeforces\n1 2\n00:00-08:00\n09:00-09:00\n12:00-12:00\n18:00-18:00\ncodeforces 1 08:04 2\nmatan 1 08:02 1\n",
"2 2 1\nmatan\ncodeforces\n2 2\n00:00-08:00\n09:00-09:00\n12:00-12:00\n18:00-18:00\ncodeforces 1 08:04 2\nmatan 1 08:03 1\n"
] | [
"150\n2\n1 1 08:16 1 09:29\n3 1 10:26 1 10:40\n",
"3\n2\n2 1 08:01 1 08:01\n1 1 08:02 1 08:03\n",
"2\n1\n1 1 08:01 1 08:02\n"
] | none | [] | 124 | 6,656,000 | 0 | 117,936 |
46 | Comb | [
"data structures",
"dp"
] | E. Comb | 1 | 256 | Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To her surprise she didn't find golden mountains there. Lara looked around and noticed on the floor a painted table *n*<=Γ<=*m* panels in size with integers written on the panels. There also was a huge number of stones lying by the wall. On the pillar near the table Lara found a guidance note which said that to get hold of the treasures one has to choose some non-zero number of the first panels in each row of the table and put stones on all those panels to push them down. After that she will receive a number of golden coins equal to the sum of numbers written on the chosen panels. Lara quickly made up her mind on how to arrange the stones and was about to start when she noticed an addition to the note in small font below. According to the addition, for the room ceiling not to crush and smash the adventurer, the chosen panels should form a comb. It was explained that the chosen panels form a comb when the sequence *c*1,<=*c*2,<=...,<=*c**n* made from the quantities of panels chosen in each table line satisfies the following property: *c*1<=><=*c*2<=<<=*c*3<=><=*c*4<=<<=..., i.e. the inequation mark interchanges between the neighboring elements. Now Lara is bewildered and doesn't know what to do. Help her to determine the largest number of coins she can get and survive at the same time. | The first line contains a pair of integers *n*,<=*m* (2<=β€<=*n*,<=*m*<=β€<=1500). Next *n* lines contain *m* integers each β that is the table itself. The absolute value of the numbers in the table does not exceed 10000. | Print the single number β the maximum number of coins Lara can get. | [
"2 2\n-1 2\n1 3\n"
] | [
"2\n"
] | none | [] | 46 | 0 | 0 | 117,967 |
309 | Morning run | [
"binary search",
"math",
"two pointers"
] | null | null | People like to be fit. That's why many of them are ready to wake up at dawn, go to the stadium and run. In this problem your task is to help a company design a new stadium.
The city of N has a shabby old stadium. Many people like it and every morning thousands of people come out to this stadium to run. The stadium can be represented as a circle, its length is exactly *l* meters with a marked start line. However, there can't be simultaneous start in the morning, so exactly at 7, each runner goes to his favorite spot on the stadium and starts running from there. Note that not everybody runs in the same manner as everybody else. Some people run in the clockwise direction, some of them run in the counter-clockwise direction. It mostly depends on the runner's mood in the morning, so you can assume that each running direction is equiprobable for each runner in any fixed morning.
The stadium is tiny and is in need of major repair, for right now there only is one running track! You can't get too playful on a single track, that's why all runners keep the same running speed β exactly 1 meter per a time unit. Nevertheless, the runners that choose different directions bump into each other as they meet.
The company wants to design a new stadium, but they first need to know how bad the old one is. For that they need the expectation of the number of bumpings by *t* time units after the running has begun. Help the company count the required expectation. Note that each runner chooses a direction equiprobably, independently from the others and then all runners start running simultaneously at 7 a.m. Assume that each runner runs for *t* time units without stopping. Consider the runners to bump at a certain moment if at that moment they found themselves at the same point in the stadium. A pair of runners can bump more than once. | The first line of the input contains three integers *n*, *l*, *t* (1<=β€<=*n*<=β€<=106,<=1<=β€<=*l*<=β€<=109,<=1<=β€<=*t*<=β€<=109). The next line contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a*1<=<<=*a*2<=<<=...<=<<=*a**n*<=<<=*l*), here *a**i* is the clockwise distance from the start line to the *i*-th runner's starting position. | Print a single real number β the answer to the problem with absolute or relative error of at most 10<=-<=6. | [
"2 5 1\n0 2\n",
"3 7 3\n0 1 6\n"
] | [
"0.2500000000\n",
"1.5000000000\n"
] | There are two runners in the first example. If the first runner run clockwise direction, then in 1 time unit he will be 1m away from the start line. If the second runner run counter-clockwise direction then in 1 time unit he will be also 1m away from the start line. And it is the only possible way to meet. We assume that each running direction is equiprobable, so the answer for the example is equal to 0.5Β·0.5β=β0.25. | [] | 0 | 0 | -1 | 118,499 |
|
509 | Pretty Song | [
"math",
"strings"
] | null | null | When Sasha was studying in the seventh grade, he started listening to music a lot. In order to evaluate which songs he likes more, he introduced the notion of the song's prettiness. The title of the song is a word consisting of uppercase Latin letters. The prettiness of the song is the prettiness of its title.
Let's define the simple prettiness of a word as the ratio of the number of vowels in the word to the number of all letters in the word.
Let's define the prettiness of a word as the sum of simple prettiness of all the substrings of the word.
More formally, let's define the function *vowel*(*c*) which is equal to 1, if *c* is a vowel, and to 0 otherwise. Let *s**i* be the *i*-th character of string *s*, and *s**i*..*j* be the substring of word *s*, staring at the *i*-th character and ending at the *j*-th character (*s**is**i*<=+<=1... *s**j*, *i*<=β€<=*j*).
Then the simple prettiness of *s* is defined by the formula:
The prettiness of *s* equals
Find the prettiness of the given song title.
We assume that the vowels are *I*,<=*E*,<=*A*,<=*O*,<=*U*,<=*Y*. | The input contains a single string *s* (1<=β€<=|*s*|<=β€<=5Β·105) β the title of the song. | Print the prettiness of the song with the absolute or relative error of at most 10<=-<=6. | [
"IEAIAIO\n",
"BYOB\n",
"YISVOWEL\n"
] | [
"28.0000000\n",
"5.8333333\n",
"17.0500000\n"
] | In the first sample all letters are vowels. The simple prettiness of each substring is 1. The word of length 7 has 28 substrings. So, the prettiness of the song equals to 28. | [
{
"input": "IEAIAIO",
"output": "28.0000000"
},
{
"input": "BYOB",
"output": "5.8333333"
},
{
"input": "YISVOWEL",
"output": "17.0500000"
},
{
"input": "EZYYOIYUZXEVRTOUYXIQ",
"output": "124.0168163"
},
{
"input": "MTOESEPRFEIWAIWLAFJMGBIQB",
"output": "127.2203685"
},
{
"input": "ZUBQNDCHHKWNWRVSDSRRRTGZDDPNTVJFKTCNGWND",
"output": "7.5320861"
},
{
"input": "POKKJNWMNCSXBJLDWVG",
"output": "6.0428477"
},
{
"input": "KZTNJQLPZVHZKZWIB",
"output": "5.8202815"
},
{
"input": "A",
"output": "1.0000000"
},
{
"input": "Z",
"output": "0.0000000"
}
] | 171 | 34,406,400 | 3 | 118,557 |
|
615 | Running Track | [
"dp",
"greedy",
"strings",
"trees"
] | null | null | A boy named Ayrat lives on planet AMI-1511. Each inhabitant of this planet has a talent. Specifically, Ayrat loves running, moreover, just running is not enough for him. He is dreaming of making running a real art.
First, he wants to construct the running track with coating *t*. On planet AMI-1511 the coating of the track is the sequence of colored blocks, where each block is denoted as the small English letter. Therefore, every coating can be treated as a string.
Unfortunately, blocks aren't freely sold to non-business customers, but Ayrat found an infinite number of coatings *s*. Also, he has scissors and glue. Ayrat is going to buy some coatings *s*, then cut out from each of them exactly one continuous piece (substring) and glue it to the end of his track coating. Moreover, he may choose to flip this block before glueing it. Ayrat want's to know the minimum number of coating *s* he needs to buy in order to get the coating *t* for his running track. Of course, he also want's to know some way to achieve the answer. | First line of the input contains the string *s*Β β the coating that is present in the shop. Second line contains the string *t*Β β the coating Ayrat wants to obtain. Both strings are non-empty, consist of only small English letters and their length doesn't exceed 2100. | The first line should contain the minimum needed number of coatings *n* or -1 if it's impossible to create the desired coating.
If the answer is not -1, then the following *n* lines should contain two integers *x**i* and *y**i*Β β numbers of ending blocks in the corresponding piece. If *x**i*<=β€<=*y**i* then this piece is used in the regular order, and if *x**i*<=><=*y**i* piece is used in the reversed order. Print the pieces in the order they should be glued to get the string *t*. | [
"abc\ncbaabc\n",
"aaabrytaaa\nayrat\n",
"ami\nno\n"
] | [
"2\n3 1\n1 3\n",
"3\n1 1\n6 5\n8 7\n",
"-1\n"
] | In the first sample string "cbaabc" = "cba" + "abc".
In the second sample: "ayrat" = "a" + "yr" + "at". | [
{
"input": "abc\ncbaabc",
"output": "2\n3 1\n1 3"
},
{
"input": "aaabrytaaa\nayrat",
"output": "3\n1 1\n6 5\n8 7"
},
{
"input": "ami\nno",
"output": "-1"
},
{
"input": "r\nr",
"output": "1\n1 1"
},
{
"input": "r\nb",
"output": "-1"
},
{
"input": "randb\nbandr",
"output": "3\n5 5\n2 4\n1 1"
},
{
"input": "aaaaaa\naaaaa",
"output": "1\n1 5"
},
{
"input": "aaaaaa\naaaaaaa",
"output": "2\n1 6\n1 1"
},
{
"input": "qwerty\nywertyrewqqq",
"output": "5\n6 6\n2 6\n4 1\n1 1\n1 1"
},
{
"input": "qwerty\nytrewq",
"output": "1\n6 1"
},
{
"input": "azaza\nzazaz",
"output": "2\n2 5\n2 2"
},
{
"input": "mnbvcxzlkjhgfdsapoiuytrewq\nqwertyuiopasdfghjklzxcvbnm",
"output": "1\n26 1"
},
{
"input": "imnothalfthemaniusedtobetheresashadowhangingovermeohyesterdaycamesuddenlywgk\nallmytroublesseemedsofarawaynowitlooksasthoughtheyreheretostayohibelieveinyesterday",
"output": "52\n7 8\n8 8\n2 2\n53 53\n5 5\n28 28\n4 4\n17 17\n23 23\n8 8\n29 30\n18 19\n12 13\n19 20\n18 18\n4 4\n9 9\n7 7\n28 28\n7 7\n37 37\n60 61\n3 4\n37 37\n1 1\n5 5\n8 8\n4 4\n4 4\n76 76\n30 32\n5 6\n4 4\n17 17\n41 41\n26 25\n11 12\n53 53\n28 26\n27 29\n21 22\n55 56\n60 61\n51 52\n1 1\n23 24\n8 8\n1 1\n47 46\n12 12\n42 43\n53 61"
},
{
"input": "woohoowellilieandimeasyallthetimebutimneversurewhyineedyoupleasedtomeetyouf\nwoohoowhenifeelheavymetalwoohooandimpinsandimneedles",
"output": "22\n1 7\n28 29\n52 51\n75 75\n53 54\n9 9\n28 29\n15 15\n41 41\n23 23\n19 20\n27 27\n24 25\n1 6\n15 19\n59 59\n51 52\n63 62\n16 19\n52 55\n60 61\n22 22"
},
{
"input": "woohoowhenifeelheavymetalwoohooandimpinsandimneedles\nwoohoowellilieandimeasyallthetimebutimneversurewhyineedyoupleasedtomeetyou",
"output": "-1"
},
{
"input": "hhhhhhh\nhhhhhhh",
"output": "1\n1 7"
},
{
"input": "mmjmmmjjmjmmmm\njmjmjmmjmmjjmj",
"output": "4\n8 11\n3 5\n3 5\n7 10"
},
{
"input": "mmlmllmllmlmlllmmmlmmmllmmlm\nzllmlllmlmmmllmmlllmllmlmlll",
"output": "-1"
},
{
"input": "klllklkllllkllllllkklkkkklklklklllkkkllklkklkklkllkllkkk\npkkkkklklklkkllllkllkkkllkkklkkllllkkkklllklllkllkklklll",
"output": "-1"
},
{
"input": "bcbbbccccbbbcbcccccbcbbbccbbcccccbcbcbbcbcbccbbbccccbcccbcbccccccccbcbcccccccccbcbbbccccbbccbcbbcbbccccbbccccbcb\nycccbcbccbcbbcbcbcbcbbccccbccccccbbcbcbbbccccccccccbcccbccbcbcbcbbbcccbcbbbcbccccbcbcbbcbccbbccbcbbcbccccccccccb",
"output": "-1"
},
{
"input": "jjjbjjbjbbbbbbjbjbbjbjbbbjbjbbjbbjbbjjbjbjjjbbbbjbjjjjbbbjbjjjjjbjbjbjjjbjjjjjjjjbbjbjbbjbbjbbbbbjjjbbjjbjjbbbbjbbjbbbbbjbbjjbjjbbjjjbjjbbbbjbjjbjbbjbbjbjbjbbbjjjjbjbjbbjbjjjjbbjbjbbbjjjjjbjjbjbjjjbjjjbbbjbjjbbbbbbbjjjjbbbbj\njjbbjbbjjjbjbbjjjjjbjbjjjbjbbbbjbbjbjjbjbbjbbbjjbjjbjbbbjbbjjbbjjjbbbjbbjbjjbbjjjjjjjbbbjjbbjjjjjbbbjjbbbjbbjjjbjbbbjjjjbbbjjjbbjjjjjbjbbbjjjjjjjjjbbbbbbbbbjjbjjbbbjbjjbjbjbjjjjjbjjbjbbjjjbjjjbjbbbbjbjjbbbjbjbjbbjbjbbbjjjbjb",
"output": "26\n38 31\n143 149\n61 68\n144 136\n139 151\n102 108\n22 27\n105 95\n149 142\n73 80\n211 206\n189 180\n22 27\n198 192\n214 222\n98 104\n62 51\n188 181\n214 205\n201 209\n68 58\n180 173\n198 192\n202 211\n163 172\n47 39"
},
{
"input": "aaaaaabaa\na",
"output": "1\n1 1"
},
{
"input": "bbbbbb\na",
"output": "-1"
},
{
"input": "bbaabaaaabaaaaaabbaaaa\naaabaaaaaaababbbaaaaaa",
"output": "4\n7 16\n4 6\n1 2\n10 16"
},
{
"input": "ltfqmwlfkswpmxi\nfkswpmi",
"output": "2\n8 13\n15 15"
},
{
"input": "abaaaabaababbaaaaaabaa\nbaaaabaababaabababaaaa",
"output": "3\n2 12\n8 12\n1 6"
},
{
"input": "ababaaaabaaaaaaaaaaaba\nbabaaabbaaaabbaaaabaaa",
"output": "4\n2 7\n2 2\n4 9\n4 12"
}
] | 140 | 3,072,000 | 3 | 118,786 |
|
0 | none | [
"none"
] | null | null | Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z".
The testtube is divided into *n* consecutive regions. Each region is occupied by a single colony of a certain bacteria type at any given moment. Hence, the population of the testtube at any moment can be described by a string of *n* Latin characters.
Sometimes a colony can decide to conquer another colony in one of the adjacent regions. When that happens, the attacked colony is immediately eliminated and replaced by a colony of the same type as the attacking colony, while the attacking colony keeps its type. Note that a colony can only attack its neighbours within the boundaries of the testtube. At any moment, at most one attack can take place.
For example, consider a testtube with population "babb". There are six options for an attack that may happen next:
- the first colony attacks the second colony (1<=β<=2), the resulting population is "bbbb";- 2<=β<=1, the result is "aabb";- 2<=β<=3, the result is "baab";- 3<=β<=2, the result is "bbbb" (note that the result is the same as the first option);- 3<=β<=4 or 4<=β<=3, the population does not change.
The pattern of attacks is rather unpredictable. Julia is now wondering how many different configurations of bacteria in the testtube she can obtain after a sequence of attacks takes place (it is possible that no attacks will happen at all). Since this number can be large, find it modulo 109<=+<=7. | The first line contains an integer *n*Β β the number of regions in the testtube (1<=β€<=*n*<=β€<=5<=000).
The second line contains *n* small Latin letters that describe the initial population of the testtube. | Print one numberΒ β the answer to the problem modulo 109<=+<=7. | [
"3\naaa\n",
"2\nab\n",
"4\nbabb\n",
"7\nabacaba\n"
] | [
"1\n",
"3\n",
"11\n",
"589\n"
] | In the first sample the population can never change since all bacteria are of the same type.
In the second sample three configurations are possible: "ab" (no attacks), "aa" (the first colony conquers the second colony), and "bb" (the second colony conquers the first colony).
To get the answer for the third sample, note that more than one attack can happen. | [] | 30 | 0 | 0 | 119,086 |
|
305 | Playing with String | [
"games"
] | null | null | Two people play the following string game. Initially the players have got some string *s*. The players move in turns, the player who cannot make a move loses.
Before the game began, the string is written on a piece of paper, one letter per cell.
A player's move is the sequence of actions:
1. The player chooses one of the available pieces of paper with some string written on it. Let's denote it is *t*. Note that initially, only one piece of paper is available. 1. The player chooses in the string *t*<==<=*t*1*t*2... *t*|*t*| character in position *i* (1<=β€<=*i*<=β€<=|*t*|) such that for some positive integer *l* (0<=<<=*i*<=-<=*l*;Β *i*<=+<=*l*<=β€<=|*t*|) the following equations hold: *t**i*<=-<=1<==<=*t**i*<=+<=1, *t**i*<=-<=2<==<=*t**i*<=+<=2, ..., *t**i*<=-<=*l*<==<=*t**i*<=+<=*l*. 1. Player cuts the cell with the chosen character. As a result of the operation, he gets three new pieces of paper, the first one will contain string *t*1*t*2... *t**i*<=-<=1, the second one will contain a string consisting of a single character *t**i*, the third one contains string *t**i*<=+<=1*t**i*<=+<=2... *t*|*t*|.
Your task is to determine the winner provided that both players play optimally well. If the first player wins, find the position of character that is optimal to cut in his first move. If there are multiple positions, print the minimal possible one. | The first line contains string *s* (1<=β€<=|*s*|<=β€<=5000). It is guaranteed that string *s* only contains lowercase English letters. | If the second player wins, print in the single line "Second" (without the quotes). Otherwise, print in the first line "First" (without the quotes), and in the second line print the minimal possible winning move β integer *i* (1<=β€<=*i*<=β€<=|*s*|). | [
"abacaba\n",
"abcde\n"
] | [
"First\n2\n",
"Second\n"
] | In the first sample the first player has multiple winning moves. But the minimum one is to cut the character in position 2.
In the second sample the first player has no available moves. | [
{
"input": "abacaba",
"output": "First\n2"
},
{
"input": "abcde",
"output": "Second"
},
{
"input": "aaaaa",
"output": "First\n3"
},
{
"input": "aaabbbbbbbbabaaabbaabbbbabbabaabaabbbaabbbbbbabbbabaabaaabaaaabbaaabbbbaabbbaaabababbbbabbabbabaaaaabababbbaabbbaabababaaabababbaaaaaaaabbbbabbabbbababbababbbaabbbbbbbabaababaaabababbabbaabbbbbaabaabaabbbabbabbbbabaabbabaaaabbbbaabaaabbaabbabbaaabbbbaaaaaaabbbbbbbbbbabaaabaabababaabababbbbbbbabaababababbbaababaaaabbaababaaabbaaaabbabbabaaaaaabbabbbbbababbabbabbaaababbbaaababbb",
"output": "Second"
},
{
"input": "abbabbaaababbbbbbabbbbaabbabaababaababbbabbbaaaaaabbabbabbaabbbbaaaabbabaababbbabbaabaabbbaabaaaabbbbbabbbbbaababbabaabaababbbbbbbaaaababbabbaababbabaabbbbbabbbbaabaaabbbababaaabbbbabaaaabbbbbbbbbabbbbababababbbbaabbababaaabbabbaaaabaaabbbbabbbaaaabbabbaabaaaabaaaaabaaabbbbaaabababaaabbbaabbababbaaabbbbaabaabbbaaaaaabbaaabbaabbbaaabbbbbabbbbaabababbbababaaabbbaabbbbbaababaaabaabbbbba",
"output": "First\n4"
},
{
"input": "hwlyeasmdahtbvvguqxrbkzzbladwolhqwnelgatnbcwxpcbzdsuriybdopjiljwyjldjvbcgwdlyrrsbpjkkembkxhsqraqzbtartvbptgsbcibfxqgiosreccmvkfvcxvxxejxtthxnaqhkmzihkjcehlubhdsyhpacwfqetifotriilokxgrjsfpwemrgiyjsbxpvvfiycbxzwcuhivdxvjgyaandwncxqkkrvhfwdnhxnbzilfpkbchsjrer",
"output": "Second"
},
{
"input": "gggggggggggggg",
"output": "First\n5"
},
{
"input": "tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogogog",
"output": "First\n6"
}
] | 78 | 0 | 0 | 119,116 |
|
720 | Array Covering | [
"data structures"
] | null | null | Misha has an array of integers of length *n*. He wants to choose *k* different continuous subarrays, so that each element of the array belongs to at least one of the chosen subarrays.
Misha wants to choose the subarrays in such a way that if he calculated the sum of elements for each subarray, and then add up all these sums, the resulting value was maximum possible. | The first line of input contains two integers: *n*, *k* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*k*<=β€<=*n*Β·(*n*<=+<=1)<=/<=2)Β β the number of elements in the array and the number of different subarrays that must be chosen.
The second line contains *n* integers *a**i* (<=-<=50<=000<=β€<=*a**i*<=β€<=50<=000)Β β the elements of the array. | Output one integerΒ β the maximum possible value Misha can get by choosing *k* different subarrays. | [
"5 4\n6 -4 -10 -4 7\n"
] | [
"11\n"
] | none | [] | 46 | 0 | 0 | 119,266 |
|
414 | Mashmokh's Designed Problem | [
"data structures"
] | null | null | After a lot of trying, Mashmokh designed a problem and it's your job to solve it.
You have a tree *T* with *n* vertices. Each vertex has a unique index from 1 to *n*. The root of *T* has index 1. For each vertex of this tree *v*, you are given a list of its children in a specific order. You must perform three types of query on this tree:
1. find distance (the number of edges in the shortest path) between *u* and *v*; 1. given *v* and *h*, disconnect *v* from its father and connect it to its *h*-th ancestor; more formally, let's denote the path from *v* to the root by *x*1,<=*x*2,<=...,<=*x**l*Β (*h*<=<<=*l*), so that *x*1<==<=*v* and *x**l* is root; disconnect *v* from its father (*x*2) and connect it to *x**h*<=+<=1; vertex *v* must be added to the end of the child-list of vertex *x**h*<=+<=1; 1. in the vertex sequence produced by calling function dfs(root) find the latest vertex that has distance *k* from the root.
The pseudo-code of function dfs(v): | The first line of input contains two space-separated integers *n*,<=*m*Β (2<=β€<=*n*<=β€<=105;Β 1<=β€<=*m*<=β€<=105), the number of vertices of *T* and number of queries to perform.
The *i*-th of the following *n* lines contains an integer *l**i*Β (0<=β€<=*l**i*<=β€<=*n*), number of *i*-th vertex's children. Then *l**i* space-separated integers follow, the *j*-th of them is the index of *j*-th child of *i*-th vertex. Note that the order of these vertices is important.
Each of the following *m* lines has one of the following format: "1 *v* *u*", "2 *v* *h*", or "3 *k*". The first number in the line is the type of query to perform according to the problem statement. The next numbers are description of the query.
It's guaranteed that all the queries are correct. For example, in the second-type query *h* is at least 2 and at most distance of *v* from root. Also in the third-type query there is at least one vertex with distance *k* from the root at the time the query is given. | For each query of the first or third type output one line containing the result of the query. | [
"4 9\n1 2\n1 3\n1 4\n0\n1 1 4\n2 4 2\n1 3 4\n3 1\n3 2\n2 3 2\n1 1 2\n3 1\n3 2\n",
"2 2\n1 2\n0\n1 2 1\n3 1\n"
] | [
"3\n2\n2\n4\n1\n3\n4\n",
"1\n2\n"
] | none | [] | 46 | 0 | 0 | 119,322 |
|
753 | Interactive Bulls and Cows (Hard) | [
"brute force",
"constructive algorithms",
"interactive"
] | null | null | The only difference from the previous problem is the constraint on the number of requests. In this problem your program should guess the answer doing at most 7 requests.
This problem is a little bit unusual. Here you are to implement an interaction with a testing system. That means that you can make queries and get responses in the online mode. Please be sure to use the stream flushing operation after each query's output in order not to leave part of your output in some buffer. For example, in C++ you've got to use the fflush(stdout) function, in Java β call System.out.flush(), and in Pascal β flush(output).
Bulls and Cows (also known as Cows and Bulls or Pigs and Bulls or Bulls and Cleots) is an old code-breaking paper and pencil game for two players, predating the similar commercially marketed board game Mastermind.
On a sheet of paper, the first player thinks a secret string. This string consists only of digits and has the length 4. The digits in the string must be all different, no two or more equal digits are allowed.
Then the second player tries to guess his opponent's string. For every guess the first player gives the number of matches. If the matching digits are on their right positions, they are "bulls", if on different positions, they are "cows". Thus a response is a pair of numbers β the number of "bulls" and the number of "cows". A try can contain equal digits.
More formally, let's the secret string is *s* and the second player are trying to guess it with a string *x*. The number of "bulls" is a number of such positions *i* (1<=β€<=*i*<=β€<=4) where *s*[*i*]<==<=*x*[*i*]. The number of "cows" is a number of such digits *c* that *s* contains *c* in the position *i* (i.e. *s*[*i*]<==<=*c*), *x* contains *c*, but *x*[*i*]<=β <=*c*.
For example, the secret string is "0427", the opponent's try is "0724", then the answer is 2 bulls and 2 cows (the bulls are "0" and "2", the cows are "4" and "7"). If the secret string is "0123", the opponent's try is "0330", then the answer is 1 bull and 1 cow.
In this problem you are to guess the string *s* that the system has chosen. You only know that the chosen string consists of 4 distinct digits.
You can make queries to the testing system, each query is the output of a single 4-digit string. The answer to the query is the number of bulls and number of cows. If the system's response equals "4 0", that means the interaction with your problem is over and the program must terminate. That is possible for two reasons β the program either guessed the number *x* or made an invalid action (for example, printed letters instead of digits).
Your program is allowed to do at most 7 queries.
You can hack solutions of other participants providing a 4-digit string containing distinct digits β the secret string. | To read answers to the queries, the program must use the standard input.
The program will receive pairs of non-negative integers in the input, one pair per line. The first number in a pair is a number of bulls and the second one is a number of cows of the string *s* and the string *x**i* printed by your program. If the system response equals "4 0", then your solution should terminate.
The testing system will let your program read the *i*-th pair of integers from the input only after your program displays the corresponding system query in the output: prints value *x**i* in a single line and executes operation flush. | The program must use the standard output to print queries.
Your program must output requests β 4-digit strings *x*1,<=*x*2,<=..., one per line. After the output of each line the program must execute flush operation. The program should read the answer to the query from the standard input.
Your program is allowed to do at most 7 queries. | [
"0 1\n2 0\n1 1\n0 4\n2 1\n4 0\n"
] | [
"8000\n0179\n3159\n3210\n0112\n0123"
] | The secret string *s* in the example is "0123". | [
{
"input": "0123",
"output": "1"
},
{
"input": "1234",
"output": "4"
},
{
"input": "9876",
"output": "5"
},
{
"input": "7158",
"output": "3"
},
{
"input": "7590",
"output": "7"
},
{
"input": "7325",
"output": "5"
},
{
"input": "7524",
"output": "5"
},
{
"input": "7269",
"output": "6"
},
{
"input": "7802",
"output": "5"
},
{
"input": "7436",
"output": "6"
},
{
"input": "7190",
"output": "5"
},
{
"input": "7390",
"output": "6"
},
{
"input": "2548",
"output": "5"
},
{
"input": "2193",
"output": "7"
},
{
"input": "2491",
"output": "6"
},
{
"input": "2469",
"output": "5"
},
{
"input": "2659",
"output": "5"
},
{
"input": "2405",
"output": "6"
},
{
"input": "2058",
"output": "5"
},
{
"input": "2580",
"output": "6"
},
{
"input": "2316",
"output": "4"
},
{
"input": "2516",
"output": "4"
},
{
"input": "8796",
"output": "6"
},
{
"input": "8534",
"output": "6"
},
{
"input": "9067",
"output": "7"
},
{
"input": "8712",
"output": "7"
},
{
"input": "9023",
"output": "7"
},
{
"input": "8645",
"output": "5"
},
{
"input": "8623",
"output": "5"
},
{
"input": "8923",
"output": "4"
},
{
"input": "8567",
"output": "6"
},
{
"input": "8756",
"output": "5"
},
{
"input": "0351",
"output": "4"
},
{
"input": "9863",
"output": "6"
},
{
"input": "0518",
"output": "5"
},
{
"input": "0263",
"output": "4"
},
{
"input": "0462",
"output": "5"
},
{
"input": "0429",
"output": "6"
},
{
"input": "0629",
"output": "5"
},
{
"input": "0374",
"output": "4"
},
{
"input": "0128",
"output": "6"
},
{
"input": "0541",
"output": "5"
},
{
"input": "1680",
"output": "5"
},
{
"input": "1648",
"output": "5"
},
{
"input": "1847",
"output": "4"
},
{
"input": "1592",
"output": "6"
},
{
"input": "1792",
"output": "5"
},
{
"input": "1759",
"output": "5"
},
{
"input": "1958",
"output": "5"
},
{
"input": "1704",
"output": "5"
},
{
"input": "1458",
"output": "4"
},
{
"input": "1870",
"output": "5"
},
{
"input": "3256",
"output": "6"
},
{
"input": "2978",
"output": "5"
},
{
"input": "3189",
"output": "4"
},
{
"input": "2934",
"output": "6"
},
{
"input": "3467",
"output": "5"
},
{
"input": "3102",
"output": "5"
},
{
"input": "3401",
"output": "5"
},
{
"input": "3056",
"output": "5"
},
{
"input": "3024",
"output": "5"
},
{
"input": "3214",
"output": "5"
},
{
"input": "9584",
"output": "6"
},
{
"input": "9340",
"output": "6"
},
{
"input": "9530",
"output": "6"
},
{
"input": "9274",
"output": "6"
},
{
"input": "9706",
"output": "6"
},
{
"input": "9451",
"output": "6"
},
{
"input": "9641",
"output": "6"
},
{
"input": "9618",
"output": "6"
},
{
"input": "9362",
"output": "5"
},
{
"input": "9562",
"output": "7"
},
{
"input": "1047",
"output": "4"
},
{
"input": "0781",
"output": "4"
},
{
"input": "0971",
"output": "6"
},
{
"input": "0947",
"output": "6"
},
{
"input": "1258",
"output": "5"
},
{
"input": "0893",
"output": "4"
},
{
"input": "1094",
"output": "6"
},
{
"input": "1072",
"output": "5"
},
{
"input": "0815",
"output": "6"
},
{
"input": "1026",
"output": "4"
},
{
"input": "2478",
"output": "5"
},
{
"input": "2134",
"output": "4"
},
{
"input": "2645",
"output": "5"
},
{
"input": "2389",
"output": "5"
},
{
"input": "2589",
"output": "6"
},
{
"input": "2345",
"output": "5"
},
{
"input": "2756",
"output": "5"
},
{
"input": "2501",
"output": "4"
},
{
"input": "2701",
"output": "5"
},
{
"input": "2456",
"output": "5"
},
{
"input": "3807",
"output": "6"
},
{
"input": "3561",
"output": "4"
},
{
"input": "3974",
"output": "4"
},
{
"input": "3719",
"output": "6"
},
{
"input": "3918",
"output": "6"
},
{
"input": "3895",
"output": "5"
},
{
"input": "4096",
"output": "5"
},
{
"input": "3840",
"output": "6"
},
{
"input": "4051",
"output": "4"
},
{
"input": "4018",
"output": "5"
},
{
"input": "0946",
"output": "5"
},
{
"input": "1257",
"output": "4"
},
{
"input": "0891",
"output": "6"
},
{
"input": "0635",
"output": "4"
},
{
"input": "1068",
"output": "5"
},
{
"input": "0813",
"output": "6"
},
{
"input": "1024",
"output": "3"
},
{
"input": "0746",
"output": "4"
},
{
"input": "1279",
"output": "6"
},
{
"input": "0924",
"output": "6"
},
{
"input": "2386",
"output": "4"
},
{
"input": "2586",
"output": "6"
},
{
"input": "2340",
"output": "4"
},
{
"input": "2197",
"output": "5"
},
{
"input": "2497",
"output": "5"
},
{
"input": "2153",
"output": "4"
},
{
"input": "2451",
"output": "3"
},
{
"input": "2410",
"output": "4"
},
{
"input": "2610",
"output": "4"
},
{
"input": "2365",
"output": "6"
},
{
"input": "3718",
"output": "6"
},
{
"input": "3917",
"output": "6"
},
{
"input": "3671",
"output": "5"
},
{
"input": "4095",
"output": "6"
},
{
"input": "3829",
"output": "5"
},
{
"input": "3582",
"output": "5"
},
{
"input": "4017",
"output": "6"
},
{
"input": "3750",
"output": "5"
},
{
"input": "3950",
"output": "6"
},
{
"input": "3694",
"output": "6"
},
{
"input": "0179",
"output": "5"
},
{
"input": "0379",
"output": "5"
},
{
"input": "0357",
"output": "4"
},
{
"input": "0547",
"output": "5"
},
{
"input": "0291",
"output": "7"
},
{
"input": "9824",
"output": "5"
},
{
"input": "0468",
"output": "4"
},
{
"input": "0214",
"output": "2"
},
{
"input": "0413",
"output": "3"
},
{
"input": "0379",
"output": "5"
},
{
"input": "1520",
"output": "3"
},
{
"input": "1943",
"output": "5"
},
{
"input": "1687",
"output": "4"
},
{
"input": "1876",
"output": "5"
},
{
"input": "1632",
"output": "4"
},
{
"input": "1598",
"output": "6"
},
{
"input": "1798",
"output": "6"
},
{
"input": "1543",
"output": "5"
},
{
"input": "1743",
"output": "5"
},
{
"input": "1720",
"output": "5"
},
{
"input": "2850",
"output": "5"
},
{
"input": "3284",
"output": "5"
},
{
"input": "3028",
"output": "6"
},
{
"input": "3218",
"output": "6"
},
{
"input": "3195",
"output": "6"
},
{
"input": "2938",
"output": "5"
},
{
"input": "3149",
"output": "5"
},
{
"input": "2873",
"output": "6"
},
{
"input": "3407",
"output": "6"
},
{
"input": "3061",
"output": "5"
},
{
"input": "4513",
"output": "6"
},
{
"input": "4713",
"output": "6"
},
{
"input": "4368",
"output": "6"
},
{
"input": "4658",
"output": "4"
},
{
"input": "4625",
"output": "4"
},
{
"input": "4279",
"output": "5"
},
{
"input": "4579",
"output": "4"
},
{
"input": "4215",
"output": "5"
},
{
"input": "4736",
"output": "6"
},
{
"input": "4390",
"output": "6"
},
{
"input": "0865",
"output": "5"
},
{
"input": "1076",
"output": "4"
},
{
"input": "0821",
"output": "6"
},
{
"input": "1354",
"output": "3"
},
{
"input": "0976",
"output": "5"
},
{
"input": "1287",
"output": "5"
},
{
"input": "0932",
"output": "7"
},
{
"input": "0897",
"output": "5"
},
{
"input": "1098",
"output": "5"
},
{
"input": "0843",
"output": "5"
},
{
"input": "2307",
"output": "5"
},
{
"input": "2507",
"output": "6"
},
{
"input": "2160",
"output": "4"
},
{
"input": "2683",
"output": "6"
},
{
"input": "2418",
"output": "5"
},
{
"input": "2618",
"output": "5"
},
{
"input": "2594",
"output": "5"
},
{
"input": "2349",
"output": "5"
},
{
"input": "2539",
"output": "6"
},
{
"input": "2184",
"output": "5"
},
{
"input": "3647",
"output": "6"
},
{
"input": "3846",
"output": "6"
},
{
"input": "3814",
"output": "6"
},
{
"input": "4025",
"output": "4"
},
{
"input": "3759",
"output": "5"
},
{
"input": "3958",
"output": "4"
},
{
"input": "3926",
"output": "6"
},
{
"input": "3680",
"output": "6"
},
{
"input": "3870",
"output": "5"
},
{
"input": "3847",
"output": "6"
},
{
"input": "0453",
"output": "3"
},
{
"input": "0421",
"output": "4"
},
{
"input": "0165",
"output": "4"
},
{
"input": "0365",
"output": "3"
},
{
"input": "9876",
"output": "5"
},
{
"input": "0532",
"output": "5"
},
{
"input": "0276",
"output": "4"
},
{
"input": "0476",
"output": "4"
},
{
"input": "0453",
"output": "3"
},
{
"input": "0643",
"output": "5"
},
{
"input": "2018",
"output": "6"
},
{
"input": "1752",
"output": "5"
},
{
"input": "1496",
"output": "5"
},
{
"input": "1695",
"output": "6"
},
{
"input": "1673",
"output": "3"
},
{
"input": "1863",
"output": "5"
},
{
"input": "1608",
"output": "4"
},
{
"input": "1807",
"output": "5"
},
{
"input": "1784",
"output": "6"
},
{
"input": "1974",
"output": "6"
},
{
"input": "3458",
"output": "4"
},
{
"input": "3092",
"output": "7"
},
{
"input": "2836",
"output": "5"
},
{
"input": "3269",
"output": "5"
},
{
"input": "3015",
"output": "5"
},
{
"input": "3205",
"output": "5"
},
{
"input": "2947",
"output": "6"
},
{
"input": "3480",
"output": "6"
},
{
"input": "3126",
"output": "4"
},
{
"input": "3416",
"output": "6"
},
{
"input": "9687",
"output": "5"
},
{
"input": "9432",
"output": "6"
},
{
"input": "9176",
"output": "5"
},
{
"input": "9610",
"output": "6"
},
{
"input": "9354",
"output": "5"
},
{
"input": "9543",
"output": "5"
},
{
"input": "9521",
"output": "6"
},
{
"input": "9721",
"output": "6"
},
{
"input": "9465",
"output": "6"
},
{
"input": "9654",
"output": "6"
},
{
"input": "1250",
"output": "3"
},
{
"input": "0874",
"output": "4"
},
{
"input": "0852",
"output": "5"
},
{
"input": "1063",
"output": "4"
},
{
"input": "0795",
"output": "5"
},
{
"input": "0985",
"output": "5"
},
{
"input": "0963",
"output": "5"
},
{
"input": "1274",
"output": "5"
},
{
"input": "0917",
"output": "6"
},
{
"input": "1208",
"output": "6"
},
{
"input": "2580",
"output": "6"
},
{
"input": "2548",
"output": "5"
},
{
"input": "2748",
"output": "5"
},
{
"input": "2491",
"output": "6"
},
{
"input": "2147",
"output": "4"
},
{
"input": "2659",
"output": "5"
},
{
"input": "2405",
"output": "6"
},
{
"input": "2604",
"output": "6"
},
{
"input": "2358",
"output": "5"
},
{
"input": "2780",
"output": "5"
},
{
"input": "8921",
"output": "6"
},
{
"input": "8796",
"output": "6"
},
{
"input": "9102",
"output": "7"
},
{
"input": "8734",
"output": "6"
},
{
"input": "8479",
"output": "6"
},
{
"input": "9023",
"output": "7"
},
{
"input": "8645",
"output": "5"
},
{
"input": "8945",
"output": "5"
},
{
"input": "8923",
"output": "4"
},
{
"input": "9134",
"output": "6"
},
{
"input": "0596",
"output": "5"
},
{
"input": "0351",
"output": "4"
},
{
"input": "0541",
"output": "5"
},
{
"input": "0285",
"output": "5"
},
{
"input": "0263",
"output": "4"
},
{
"input": "0462",
"output": "5"
},
{
"input": "0196",
"output": "5"
},
{
"input": "0396",
"output": "5"
},
{
"input": "0374",
"output": "4"
},
{
"input": "0573",
"output": "4"
},
{
"input": "1936",
"output": "6"
},
{
"input": "1680",
"output": "5"
},
{
"input": "1870",
"output": "5"
},
{
"input": "1847",
"output": "4"
},
{
"input": "1592",
"output": "6"
},
{
"input": "1792",
"output": "5"
},
{
"input": "1537",
"output": "5"
},
{
"input": "1958",
"output": "5"
},
{
"input": "1704",
"output": "5"
},
{
"input": "1904",
"output": "6"
},
{
"input": "3278",
"output": "4"
},
{
"input": "3024",
"output": "5"
},
{
"input": "3214",
"output": "5"
},
{
"input": "3189",
"output": "4"
},
{
"input": "2934",
"output": "6"
},
{
"input": "3145",
"output": "5"
},
{
"input": "3102",
"output": "5"
},
{
"input": "3401",
"output": "5"
},
{
"input": "3056",
"output": "5"
},
{
"input": "3256",
"output": "6"
},
{
"input": "9507",
"output": "7"
},
{
"input": "9251",
"output": "6"
},
{
"input": "9673",
"output": "5"
},
{
"input": "9418",
"output": "6"
},
{
"input": "9618",
"output": "6"
},
{
"input": "9584",
"output": "6"
},
{
"input": "9784",
"output": "5"
},
{
"input": "9530",
"output": "6"
},
{
"input": "9275",
"output": "6"
},
{
"input": "9708",
"output": "6"
},
{
"input": "0948",
"output": "5"
},
{
"input": "0926",
"output": "5"
},
{
"input": "1237",
"output": "5"
},
{
"input": "0861",
"output": "5"
},
{
"input": "1072",
"output": "5"
},
{
"input": "1048",
"output": "5"
},
{
"input": "1348",
"output": "6"
},
{
"input": "0972",
"output": "6"
},
{
"input": "1506",
"output": "5"
},
{
"input": "1259",
"output": "6"
},
{
"input": "7523",
"output": "5"
},
{
"input": "7268",
"output": "7"
},
{
"input": "7468",
"output": "5"
},
{
"input": "7213",
"output": "5"
},
{
"input": "7634",
"output": "7"
},
{
"input": "7389",
"output": "5"
},
{
"input": "7589",
"output": "4"
},
{
"input": "7324",
"output": "4"
},
{
"input": "7845",
"output": "5"
},
{
"input": "7501",
"output": "5"
},
{
"input": "8952",
"output": "6"
},
{
"input": "8609",
"output": "7"
},
{
"input": "8907",
"output": "4"
},
{
"input": "8764",
"output": "5"
},
{
"input": "9075",
"output": "6"
},
{
"input": "8720",
"output": "6"
},
{
"input": "9031",
"output": "7"
},
{
"input": "8975",
"output": "5"
},
{
"input": "9186",
"output": "5"
},
{
"input": "8931",
"output": "6"
},
{
"input": "0416",
"output": "4"
},
{
"input": "0159",
"output": "6"
},
{
"input": "0359",
"output": "6"
},
{
"input": "0327",
"output": "5"
},
{
"input": "0527",
"output": "5"
},
{
"input": "0271",
"output": "5"
},
{
"input": "0471",
"output": "4"
},
{
"input": "0438",
"output": "5"
},
{
"input": "0638",
"output": "4"
},
{
"input": "0382",
"output": "6"
},
{
"input": "1745",
"output": "4"
},
{
"input": "1489",
"output": "6"
},
{
"input": "1923",
"output": "7"
},
{
"input": "1657",
"output": "5"
},
{
"input": "1856",
"output": "5"
},
{
"input": "1602",
"output": "4"
},
{
"input": "2045",
"output": "5"
},
{
"input": "1768",
"output": "5"
},
{
"input": "1967",
"output": "5"
},
{
"input": "1723",
"output": "5"
},
{
"input": "8096",
"output": "5"
},
{
"input": "7831",
"output": "6"
},
{
"input": "8264",
"output": "6"
},
{
"input": "8019",
"output": "4"
},
{
"input": "8209",
"output": "5"
},
{
"input": "8175",
"output": "5"
},
{
"input": "8375",
"output": "6"
},
{
"input": "8130",
"output": "6"
},
{
"input": "8320",
"output": "6"
},
{
"input": "8296",
"output": "7"
},
{
"input": "9427",
"output": "5"
},
{
"input": "9405",
"output": "6"
},
{
"input": "9604",
"output": "7"
},
{
"input": "9348",
"output": "5"
},
{
"input": "9538",
"output": "6"
},
{
"input": "9516",
"output": "6"
},
{
"input": "9715",
"output": "5"
},
{
"input": "9460",
"output": "6"
},
{
"input": "9872",
"output": "6"
},
{
"input": "9627",
"output": "5"
},
{
"input": "1203",
"output": "4"
},
{
"input": "0845",
"output": "6"
},
{
"input": "1056",
"output": "5"
},
{
"input": "0789",
"output": "4"
},
{
"input": "1325",
"output": "3"
},
{
"input": "0957",
"output": "6"
},
{
"input": "1268",
"output": "5"
},
{
"input": "0913",
"output": "7"
},
{
"input": "1436",
"output": "3"
},
{
"input": "1079",
"output": "6"
},
{
"input": "7452",
"output": "5"
},
{
"input": "7642",
"output": "6"
},
{
"input": "7396",
"output": "5"
},
{
"input": "7364",
"output": "6"
},
{
"input": "7563",
"output": "6"
},
{
"input": "7309",
"output": "7"
},
{
"input": "7509",
"output": "6"
},
{
"input": "7485",
"output": "5"
},
{
"input": "7158",
"output": "3"
},
{
"input": "9431",
"output": "7"
}
] | 0 | 0 | -1 | 120,039 |
|
442 | Gena and Second Distance | [
"geometry"
] | null | null | Gena doesn't like geometry, so he asks you to solve this problem for him.
A rectangle with sides parallel to coordinate axes contains *n* dots. Let's consider some point of the plane. Let's count the distances from this point to the given *n* points. Let's sort these numbers in the non-decreasing order. We'll call the beauty of the point the second element of this array. If there are two mimimum elements in this array, the beaty will be equal to this minimum.
Find the maximum beauty of a point inside the given rectangle. | The first line contains three integers *w*,<=*h*,<=*n* (1<=β€<=*w*,<=*h*<=β€<=106,<=2<=β€<=*n*<=β€<=1000) β the lengths of the rectangle sides and the number of points. Next *n* lines contain two integers *x**i*,<=*y**i* (0<=β€<=*x**i*<=β€<=*w*,<=0<=β€<=*y**i*<=β€<=*h*) each β the coordinates of a point. It is possible that it will be coincident points. | Print a single number β the maximum beauty of a point with the absolute or relative error of at most 10<=-<=9. | [
"5 5 4\n0 0\n5 0\n0 5\n5 5\n",
"5 5 3\n4 0\n2 5\n4 1\n"
] | [
"4.99999999941792340\n",
"5.65685424744772010\n"
] | The point which beauty we need to find must have coordinates (*x*,β*y*), where 0ββ€β*x*ββ€β*w*,β0ββ€β*y*ββ€β*h*. Some of the *n* points can coincide. | [] | 46 | 0 | 0 | 120,238 |
|
855 | Helga Hufflepuff's Cup | [
"dp",
"trees"
] | null | null | Harry, Ron and Hermione have figured out that Helga Hufflepuff's cup is a horcrux. Through her encounter with Bellatrix Lestrange, Hermione came to know that the cup is present in Bellatrix's family vault in Gringott's Wizarding Bank.
The Wizarding bank is in the form of a tree with total *n* vaults where each vault has some type, denoted by a number between 1 to *m*. A tree is an undirected connected graph with no cycles.
The vaults with the highest security are of type *k*, and all vaults of type *k* have the highest security.
There can be at most *x* vaults of highest security.
Also, if a vault is of the highest security, its adjacent vaults are guaranteed to not be of the highest security and their type is guaranteed to be less than *k*.
Harry wants to consider every possibility so that he can easily find the best path to reach Bellatrix's vault. So, you have to tell him, given the tree structure of Gringotts, the number of possible ways of giving each vault a type such that the above conditions hold. | The first line of input contains two space separated integers, *n* and *m*Β β the number of vaults and the number of different vault types possible. (1<=β€<=*n*<=β€<=105,<=1<=β€<=*m*<=β€<=109).
Each of the next *n*<=-<=1 lines contain two space separated integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*) representing the *i*-th edge, which shows there is a path between the two vaults *u**i* and *v**i*. It is guaranteed that the given graph is a tree.
The last line of input contains two integers *k* and *x* (1<=β€<=*k*<=β€<=*m*,<=1<=β€<=*x*<=β€<=10), the type of the highest security vault and the maximum possible number of vaults of highest security. | Output a single integer, the number of ways of giving each vault a type following the conditions modulo 109<=+<=7. | [
"4 2\n1 2\n2 3\n1 4\n1 2\n",
"3 3\n1 2\n1 3\n2 1\n",
"3 1\n1 2\n1 3\n1 1\n"
] | [
"1\n",
"13\n",
"0\n"
] | In test case 1, we cannot have any vault of the highest security as its type is 1 implying that its adjacent vaults would have to have a vault type less than 1, which is not allowed. Thus, there is only one possible combination, in which all the vaults have type 2. | [
{
"input": "4 2\n1 2\n2 3\n1 4\n1 2",
"output": "1"
},
{
"input": "3 3\n1 2\n1 3\n2 1",
"output": "13"
},
{
"input": "3 1\n1 2\n1 3\n1 1",
"output": "0"
},
{
"input": "3 1000000000\n2 3\n3 1\n585430050 9",
"output": "91592837"
},
{
"input": "4 50000\n2 1\n4 2\n2 3\n42169 9",
"output": "542369366"
},
{
"input": "15 100000\n9 7\n15 13\n1 13\n14 5\n6 10\n5 12\n4 14\n4 6\n8 3\n8 2\n9 3\n10 15\n11 8\n10 3\n16283 7",
"output": "770195687"
}
] | 46 | 29,081,600 | 0 | 120,350 |
|
0 | none | [
"none"
] | null | null | The German University in Cairo (GUC) dorm houses are numbered from 1 to *n*. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle).
For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house.
In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap. | The first line contains two space-separated integers *n* and *p* (1<=β€<=*n*<=β€<=1000,<=0<=β€<=*p*<=β€<=*n*) β the number of houses and the number of pipes correspondingly.
Then *p* lines follow β the description of *p* pipes. The *i*-th line contains three integers *a**i* *b**i* *d**i*, indicating a pipe of diameter *d**i* going from house *a**i* to house *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*,<=1<=β€<=*d**i*<=β€<=106).
It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it. | Print integer *t* in the first line β the number of tank-tap pairs of houses.
For the next *t* lines, print 3 integers per line, separated by spaces: *tank**i*, *tap**i*, and *diameter**i*, where *tank**i*<=β <=*tap**i* (1<=β€<=*i*<=β€<=*t*). Here *tank**i* and *tap**i* are indexes of tank and tap houses respectively, and *diameter**i* is the maximum amount of water that can be conveyed. All the *t* lines should be ordered (increasingly) by *tank**i*. | [
"3 2\n1 2 10\n2 3 20\n",
"3 3\n1 2 20\n2 3 10\n3 1 5\n",
"4 2\n1 2 60\n3 4 50\n"
] | [
"1\n1 3 10\n",
"0\n",
"2\n1 2 60\n3 4 50\n"
] | none | [
{
"input": "3 2\n1 2 10\n2 3 20",
"output": "1\n1 3 10"
},
{
"input": "3 3\n1 2 20\n2 3 10\n3 1 5",
"output": "0"
},
{
"input": "4 2\n1 2 60\n3 4 50",
"output": "2\n1 2 60\n3 4 50"
},
{
"input": "10 10\n10 3 70\n1 9 98\n9 10 67\n5 2 78\n8 6 71\n4 8 95\n7 1 10\n2 5 73\n6 7 94\n3 4 23",
"output": "0"
},
{
"input": "7 5\n3 2 26\n4 6 84\n6 3 82\n5 1 57\n1 7 34",
"output": "2\n4 2 26\n5 7 34"
},
{
"input": "9 6\n7 4 98\n5 9 72\n4 6 10\n2 8 22\n9 7 17\n3 1 66",
"output": "3\n2 8 22\n3 1 66\n5 6 10"
},
{
"input": "8 6\n1 3 84\n8 4 34\n7 2 10\n6 8 8\n3 5 39\n2 7 8",
"output": "2\n1 5 39\n6 4 8"
},
{
"input": "10 8\n2 3 49\n4 8 26\n5 2 76\n3 5 94\n1 7 16\n10 9 77\n6 4 24\n7 1 7",
"output": "2\n6 8 24\n10 9 77"
},
{
"input": "6 5\n2 6 47\n3 4 27\n5 2 47\n4 1 62\n1 5 61",
"output": "1\n3 6 27"
},
{
"input": "5 4\n5 2 9\n4 1 94\n3 5 82\n2 3 58",
"output": "1\n4 1 94"
},
{
"input": "1000 0",
"output": "0"
},
{
"input": "2 2\n1 2 1\n2 1 1",
"output": "0"
},
{
"input": "44 42\n4 37 166\n34 25 47\n28 19 367\n20 14 811\n8 3 878\n39 1 925\n35 9 206\n32 18 841\n16 44 503\n5 20 426\n22 34 896\n44 43 471\n17 33 577\n40 22 317\n24 31 818\n37 11 292\n21 39 888\n6 8 983\n43 36 170\n11 21 662\n36 17 942\n18 7 356\n2 32 220\n12 5 774\n19 27 193\n13 40 63\n15 10 510\n30 35 869\n41 24 736\n42 4 180\n23 41 261\n9 28 501\n29 15 983\n10 30 638\n7 13 402\n26 12 754\n25 6 597\n27 29 57\n1 16 933\n31 42 135\n33 38 718\n14 23 361",
"output": "2\n2 3 47\n26 38 135"
},
{
"input": "35 33\n22 7 978\n5 6 566\n20 10 198\n6 17 170\n7 27 627\n24 19 659\n31 30 130\n34 8 365\n23 12 716\n4 29 217\n8 20 156\n26 35 142\n3 2 419\n15 1 448\n17 24 91\n18 33 962\n30 22 822\n9 16 847\n2 9 470\n10 25 981\n16 31 359\n19 28 283\n28 34 199\n11 5 660\n25 23 176\n29 18 235\n12 14 765\n14 11 81\n27 21 61\n21 13 651\n35 3 583\n1 32 767\n13 4 256",
"output": "2\n15 32 448\n26 33 61"
},
{
"input": "33 28\n12 15 574\n11 13 714\n13 33 62\n9 28 391\n22 19 235\n6 20 655\n23 9 25\n8 29 994\n21 30 133\n17 18 170\n32 7 470\n14 21 418\n7 31 431\n3 1 185\n1 14 538\n33 12 250\n31 22 694\n2 27 945\n16 26 584\n19 32 317\n27 2 904\n15 25 748\n29 3 754\n24 4 287\n18 10 775\n30 11 401\n10 8 653\n28 5 70",
"output": "5\n6 20 655\n16 26 584\n17 25 62\n23 5 25\n24 4 287"
},
{
"input": "3 1\n1 2 10",
"output": "1\n1 2 10"
},
{
"input": "1 0",
"output": "0"
},
{
"input": "2 0",
"output": "0"
},
{
"input": "3 0",
"output": "0"
}
] | 46 | 0 | 0 | 120,793 |
|
650 | Zip-line | [
"binary search",
"data structures",
"dp",
"hashing"
] | null | null | Vasya has decided to build a zip-line on trees of a nearby forest. He wants the line to be as long as possible but he doesn't remember exactly the heights of all trees in the forest. He is sure that he remembers correct heights of all trees except, possibly, one of them.
It is known that the forest consists of *n* trees staying in a row numbered from left to right with integers from 1 to *n*. According to Vasya, the height of the *i*-th tree is equal to *h**i*. The zip-line of length *k* should hang over *k* (1<=β€<=*k*<=β€<=*n*) trees *i*1,<=*i*2,<=...,<=*i**k* (*i*1<=<<=*i*2<=<<=...<=<<=*i**k*) such that their heights form an increasing sequence, that is *h**i*1<=<<=*h**i*2<=<<=...<=<<=*h**i**k*.
Petya had been in this forest together with Vasya, and he now has *q* assumptions about the mistake in Vasya's sequence *h*. His *i*-th assumption consists of two integers *a**i* and *b**i* indicating that, according to Petya, the height of the tree numbered *a**i* is actually equal to *b**i*. Note that Petya's assumptions are independent from each other.
Your task is to find the maximum length of a zip-line that can be built over the trees under each of the *q* assumptions.
In this problem the length of a zip line is considered equal to the number of trees that form this zip-line. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=400<=000)Β β the number of the trees in the forest and the number of Petya's assumptions, respectively.
The following line contains *n* integers *h**i* (1<=β€<=*h**i*<=β€<=109)Β β the heights of trees according to Vasya.
Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*<=β€<=*n*, 1<=β€<=*b**i*<=β€<=109). | For each of the Petya's assumptions output one integer, indicating the maximum length of a zip-line that can be built under this assumption. | [
"4 4\n1 2 3 4\n1 1\n1 4\n4 3\n4 5\n",
"4 2\n1 3 2 6\n3 5\n2 4\n"
] | [
"4\n3\n3\n4\n",
"4\n3\n"
] | Consider the first sample. The first assumption actually coincides with the height remembered by Vasya. In the second assumption the heights of the trees are (4,β2,β3,β4), in the third one they are (1,β2,β3,β3) and in the fourth one they are (1,β2,β3,β5). | [] | 3,000 | 28,262,400 | 0 | 120,838 |
|
566 | Logistical Questions | [
"dfs and similar",
"divide and conquer",
"trees"
] | null | null | Some country consists of *n* cities, connected by a railroad network. The transport communication of the country is so advanced that the network consists of a minimum required number of (*n*<=-<=1) bidirectional roads (in the other words, the graph of roads is a tree). The *i*-th road that directly connects cities *a**i* and *b**i*, has the length of *l**i* kilometers.
The transport network is served by a state transporting company FRR (Fabulous Rail Roads). In order to simplify the price policy, it offers a single ride fare on the train. In order to follow the route of length *t* kilometers, you need to pay burles. Note that it is forbidden to split a long route into short segments and pay them separately (a special railroad police, or RRP, controls that the law doesn't get violated).
A Large Software Company decided to organize a programming tournament. Having conducted several online rounds, the company employees determined a list of finalists and sent it to the logistical department to find a place where to conduct finals. The Large Software Company can easily organize the tournament finals in any of the *n* cities of the country, so the the main factor in choosing the city for the last stage of the tournament is the total cost of buying tickets for all the finalists. We know that the *i*-th city of the country has *w**i* cup finalists living there.
Help the company employees find the city such that the total cost of travel of all the participants to it is minimum. | The first line of the input contains number *n* (1<=β€<=*n*<=β€<=200<=000) β the number of cities in the country.
The next line contains *n* integers *w*1,<=*w*2,<=...,<=*w**n* (0<=β€<=*w**i*<=β€<=108) β the number of finalists living in each city of the country.
Next (*n*<=-<=1) lines contain the descriptions of the railroad, the *i*-th line contains three integers, *a**i*, *b**i*, *l**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*, 1<=β€<=*l**i*<=β€<=1000). | Print two numbers β an integer *f* that is the number of the optimal city to conduct the competition, and the real number *c*, equal to the minimum total cost of transporting all the finalists to the competition. Your answer will be considered correct if two conditions are fulfilled at the same time:
1. The absolute or relative error of the printed number *c* in comparison with the cost of setting up a final in city *f* doesn't exceed 10<=-<=6; 1. Absolute or relative error of the printed number *c* in comparison to the answer of the jury doesn't exceed 10<=-<=6.
If there are multiple answers, you are allowed to print any of them. | [
"5\n3 1 2 6 5\n1 2 3\n2 3 1\n4 3 9\n5 3 1\n",
"2\n5 5\n1 2 2\n"
] | [
"3 192.0",
"1 14.142135623730951000\n"
] | In the sample test an optimal variant of choosing a city to conduct the finals of the competition is 3. At such choice the cost of conducting is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/4219dfaeaed77b348dc7ca0c500739d1c0ff9e3f.png" style="max-width: 100.0%;max-height: 100.0%;"/> burles.
In the second sample test, whatever city you would choose, you will need to pay for the transport for five participants, so you will need to pay <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bec9396b77dab4487d19f32c85b0edb26924da8c.png" style="max-width: 100.0%;max-height: 100.0%;"/> burles for each one of them. | [] | 2,000 | 716,800 | 0 | 121,034 |
|
161 | Polycarpus the Safecracker | [
"brute force",
"dp"
] | null | null | Polycarpus has *t* safes. The password for each safe is a square matrix consisting of decimal digits '0' ... '9' (the sizes of passwords to the safes may vary). Alas, Polycarpus has forgotten all passwords, so now he has to restore them.
Polycarpus enjoys prime numbers, so when he chose the matrix passwords, he wrote a prime number in each row of each matrix. To his surprise, he found that all the matrices turned out to be symmetrical (that is, they remain the same after transposition). Now, years later, Polycarp was irritated to find out that he remembers only the prime numbers *p**i*, written in the first lines of the password matrices.
For each safe find the number of matrices which can be passwords to it.
The number of digits in *p**i* determines the number of rows and columns of the *i*-th matrix. One prime number can occur in several rows of the password matrix or in several matrices. The prime numbers that are written not in the first row of the matrix may have leading zeros. | The first line of the input contains an integer *t* (1<=β€<=*t*<=β€<=30) β the number of safes. Next *t* lines contain integers *p**i* (10<=β€<=*p**i*<=β€<=99999), *p**i* is a prime number written in the first row of the password matrix for the *i*-th safe. All *p**i*'s are written without leading zeros. | Print *t* numbers, the *i*-th of them should be the number of matrices that can be a password to the *i*-th safe. Print the numbers on separate lines. | [
"4\n11\n239\n401\n9001\n"
] | [
"4\n28\n61\n2834\n"
] | Here is a possible password matrix for the second safe:
Here is a possible password matrix for the fourth safe: | [] | 92 | 0 | 0 | 121,048 |
|
54 | Writing a Song | [
"brute force",
"dp",
"strings"
] | D. Writing a Song | 2 | 256 | One of the Hedgehog and his friend's favorite entertainments is to take some sentence or a song and replace half of the words (sometimes even all of them) with each other's names.
The friend's birthday is approaching and the Hedgehog decided to make a special present to his friend: a very long song, where his name will be repeated many times. But try as he might, he can't write a decent song!
The problem is that the Hedgehog has already decided how long the resulting sentence should be (i.e. how many letters it should contain) and in which positions in the sentence the friend's name should occur, and it must not occur in any other position in the sentence. Besides, the Hedgehog decided to limit himself to using only the first *K* letters of an English alphabet in this sentence (so it will be not even a sentence, but one long word).
The resulting problem is indeed quite complicated, that's why the Hedgehog asks you to help him and write a program that will make the desired word by the given name *P*, the length *N* of the required word, the given positions of the occurrences of the name *P* in the desired word and the alphabet's size *K*. Note that the occurrences of the name can overlap with each other. | The first line contains numbers *N* and *K* which are the length of the required string and the alphabet size accordingly. The limitations are: 1<=β€<=*N*<=β€<=100, 2<=β€<=*K*<=β€<=26.
The second line contains the name *P* which is a non-empty string whose length does not exceed *N* characters. The string consists only of the first *K* lowercase symbols of an English alphabet.
The third line contains the string of length *N*<=-<=*length*(*P*)<=+<=1, consisting only of numbers zero and one. A number one in the *i*-th position means that an occurrence of the name *P* should start from *i*-th position of the desired word, while a zero means that there is no occurrence starting here. | Print the desired word *S*. If there are several answers, print any of them.
If there is no solution, then print "No solution". | [
"5 2\naba\n101\n",
"5 2\na\n10001\n",
"6 2\nabba\n101\n"
] | [
"ababa",
"abbba",
"No solution"
] | none | [
{
"input": "5 2\naba\n101",
"output": "ababa"
},
{
"input": "5 2\na\n10001",
"output": "abbba"
},
{
"input": "6 2\nabba\n101",
"output": "No solution"
},
{
"input": "12 5\nabacaba\n010001",
"output": "aabacabacaba"
},
{
"input": "9 3\nac\n10100101",
"output": "acacaacac"
},
{
"input": "19 2\naababaaba\n10000100001",
"output": "aababaababaababaaba"
},
{
"input": "5 2\naaa\n101",
"output": "No solution"
},
{
"input": "10 2\naaa\n11000111",
"output": "aaaabaaaaa"
},
{
"input": "15 2\naaa\n0000000000111",
"output": "aabaabaabbaaaaa"
},
{
"input": "20 2\naba\n001000000000000000",
"output": "aaabaaaaaaaaaaaaaaaa"
},
{
"input": "10 2\naabb\n0000000",
"output": "aaaaaaaaaa"
},
{
"input": "15 2\naabb\n010000000100",
"output": "aaabbaaaaaabbaa"
},
{
"input": "20 2\nabbb\n00000000000100000",
"output": "aaaaaaaaaaaabbbaaaaa"
},
{
"input": "100 2\nbbaa\n0100000000000000000000001000000001000010000000000000000000001000000000000000000000000000000000000",
"output": "abbaaaaaaaaaaaaaaaaaaaaabbaaaaaaabbaaabbaaaaaaaaaaaaaaaaaaaabbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "10 5\nbeb\n00101000",
"output": "aabebebaaa"
},
{
"input": "15 5\nbcd\n0100010010010",
"output": "abcdabcdbcdbcda"
},
{
"input": "20 5\nece\n010101001001000010",
"output": "aecececeeceeceaaecea"
},
{
"input": "10 5\neded\n0010000",
"output": "aaededaaaa"
},
{
"input": "15 5\nbdda\n001000100000",
"output": "aabddabddaaaaaa"
},
{
"input": "20 5\nbeda\n00100001000100001",
"output": "aabedaabedabedaabeda"
},
{
"input": "100 5\nbcda\n0100000001000010000010000000100000001000000010000000100000001000000010000001000000010000010000001",
"output": "abcdaaaaabcdaabcdaaabcdaaaaabcdaaaaabcdaaaaabcdaaaaabcdaaaaabcdaaaaabcdaaaabcdaaaaabcdaaabcdaaaabcda"
},
{
"input": "10 5\nede\n00001010",
"output": "aaaaededea"
},
{
"input": "15 5\nece\n0100000010101",
"output": "aeceaaaaececece"
},
{
"input": "20 5\necc\n010000100100010000",
"output": "aeccaaecceccaeccaaaa"
},
{
"input": "10 5\nedda\n0010000",
"output": "aaeddaaaaa"
},
{
"input": "15 5\nbeba\n001000000010",
"output": "aabebaaaaabebaa"
},
{
"input": "20 5\nbcae\n00000010000000001",
"output": "aaaaaabcaeaaaaaabcae"
},
{
"input": "100 5\ncaae\n0100000000100010000001000000100001000010000100000100001000000001000100000001000001000100010000010",
"output": "acaaeaaaaacaaecaaeaaacaaeaaacaaeacaaeacaaeacaaeaacaaeacaaeaaaaacaaecaaeaaaacaaeaacaaecaaecaaeaacaaea"
},
{
"input": "10 2\naaa\n00000100",
"output": "aababaaaba"
},
{
"input": "15 2\naab\n1001000000100",
"output": "aabaabaaaaaabaa"
},
{
"input": "20 2\naaa\n010000000000000000",
"output": "baaabaabaabaabaabaab"
},
{
"input": "10 2\naaba\n0000000",
"output": "aaaaaaaaaa"
},
{
"input": "15 2\naaab\n000000000000",
"output": "aaaaaaaaaaaaaaa"
},
{
"input": "20 2\nabaa\n00000000000010000",
"output": "aaaaaaaaaaaaabaaaaaa"
},
{
"input": "100 2\nbaba\n0000000000001010000010000001000000000000001010000010000000000000000000000000000100000000000000000",
"output": "aaaaaaaaaaaabababaaababaaaababaaaaaaaaaaaabababaaababaaaaaaaaaaaaaaaaaaaaaaaaaababaaaaaaaaaaaaaaaaaa"
},
{
"input": "10 5\nada\n00000000",
"output": "aaaaaaaaaa"
},
{
"input": "15 5\nbcd\n0000000000000",
"output": "aaaaaaaaaaaaaaa"
},
{
"input": "20 5\ncdb\n000000000000000000",
"output": "aaaaaaaaaaaaaaaaaaaa"
},
{
"input": "10 5\nacec\n0000000",
"output": "aaaaaaaaaa"
},
{
"input": "15 5\nbbbc\n000000000000",
"output": "aaaaaaaaaaaaaaa"
},
{
"input": "20 5\nbbaa\n00000000000000000",
"output": "aaaaaaaaaaaaaaaaaaaa"
},
{
"input": "100 5\nbecb\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "14 2\naabbbaabbbaba\n00",
"output": "aaaaaaaaaaaaaa"
},
{
"input": "15 2\nbbbbabbabaaba\n000",
"output": "aaaaaaaaaaaaaaa"
},
{
"input": "20 2\naaabababaabaa\n00000000",
"output": "aaaaaaaaaaaaaaaaaaaa"
},
{
"input": "19 2\nbaaaabaababaab\n000000",
"output": "aaaaaaaaaaaaaaaaaaa"
},
{
"input": "15 2\nabababaaaaaabb\n00",
"output": "aaaaaaaaaaaaaaa"
},
{
"input": "20 2\nababaaabbababa\n0000000",
"output": "aaaaaaaaaaaaaaaaaaaa"
},
{
"input": "100 2\nabaaaabbaabbaa\n000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "14 5\ndaecaceaacbbe\n00",
"output": "aaaaaaaaaaaaaa"
},
{
"input": "15 5\nacedaacceccac\n000",
"output": "aaaaaaaaaaaaaaa"
},
{
"input": "20 5\naeadaabbbeabe\n00000000",
"output": "aaaaaaaaaaaaaaaaaaaa"
},
{
"input": "19 5\neeaaddeadcadbe\n000000",
"output": "aaaaaaaaaaaaaaaaaaa"
},
{
"input": "15 5\ncadedccceddbea\n00",
"output": "aaaaaaaaaaaaaaa"
},
{
"input": "20 5\ndaddcdadcadcce\n0000000",
"output": "aaaaaaaaaaaaaaaaaaaa"
},
{
"input": "100 5\nbdddeacceaecbc\n000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "7 2\naba\n10001",
"output": "abaaaba"
},
{
"input": "100 2\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n101010101010101010101010101010101010101010101010101",
"output": "No solution"
},
{
"input": "100 26\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n101010101010101010101010101010101010101010101010101",
"output": "No solution"
},
{
"input": "100 2\nbababababababababababababababababababababababababa\n100010001000100010001000100010001000100010001000100",
"output": "No solution"
},
{
"input": "100 26\nbababababababababababababababababababababababababa\n100010001000100010001000100010001000100010001000100",
"output": "No solution"
},
{
"input": "7 2\nbab\n10001",
"output": "babbbab"
},
{
"input": "7 2\nbab\n10101",
"output": "bababab"
},
{
"input": "7 2\naba\n10001",
"output": "abaaaba"
},
{
"input": "7 2\naba\n10101",
"output": "abababa"
},
{
"input": "100 2\nbab\n10001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "babbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbaba"
},
{
"input": "100 2\nbab\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
"output": "babababababababababababababababababababababababababababababababababababababababababababababababababa"
},
{
"input": "100 3\nbab\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
"output": "babababababababababababababababababababababababababababababababababababababababababababababababababa"
},
{
"input": "100 26\nbab\n10101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
"output": "babababababababababababababababababababababababababababababababababababababababababababababababababa"
},
{
"input": "100 2\nbab\n10001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "babbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbaba"
},
{
"input": "100 3\nbab\n10001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "babbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbaba"
},
{
"input": "100 26\nbab\n10001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "babbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbabbbaba"
},
{
"input": "100 2\na\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "100 2\na\n0111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
"output": "baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "100 2\na\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"
},
{
"input": "100 2\na\n1111111111101111111011011111011111111101111111111101111110111011111111011110111111011111011111111111",
"output": "aaaaaaaaaaabaaaaaaabaabaaaaabaaaaaaaaabaaaaaaaaaaabaaaaaabaaabaaaaaaaabaaaabaaaaaabaaaaabaaaaaaaaaaa"
},
{
"input": "100 2\na\n0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"output": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "100 2\na\n0000000000001100000000010000000000000000000000001000000000000100000010000000000000000000000000000000",
"output": "bbbbbbbbbbbbaabbbbbbbbbabbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbabbbbbbabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
},
{
"input": "100 2\naa\n000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000",
"output": "abababababababababababababababababaababababababababababababababababababababababababababababababababa"
},
{
"input": "100 2\naaaaaaa\n1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "100 2\naaaaaaa\n1111111111111111001111111111111111111111101111111110111101111111111111011111111111100000111111",
"output": "No solution"
},
{
"input": "100 2\naaaaaaa\n1111111111111111111111111111111100000000000000011111111111111111111111111111111111111111111111",
"output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaabbaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
},
{
"input": "100 2\nabaa\n1000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001",
"output": "abaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaa"
},
{
"input": "100 3\nabaa\n1000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001",
"output": "abaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaa"
},
{
"input": "100 26\nabaa\n1000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001000001",
"output": "abaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaa"
},
{
"input": "100 2\nabaa\n1000001000001000001000000000001000001000001000001000001000001000001000001000001000001000001000001",
"output": "abaaaaabaaaaabaaaaabaaaaaaaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaa"
},
{
"input": "100 2\nabaa\n1000001000001001001000001000001000001000001000001000001000001000001000001000001000001000001000001",
"output": "abaaaaabaaaaabaabaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaaaaabaa"
},
{
"input": "100 2\nabaa\n1000001000001001001000001000001000001001001000001001001000001001001000001000001000001001001000001",
"output": "abaaaaabaaaaabaabaabaaaaabaaaaabaaaaabaabaabaaaaabaabaabaaaaabaabaabaaaaabaaaaabaaaaabaabaabaaaaabaa"
},
{
"input": "100 3\nabacabacabacabacabacabacabacabacabacabacaba\n1000100010001000100010001000100010001000100010001000100010",
"output": "abacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabaa"
},
{
"input": "100 26\nabacabacabacabacabacabacabacabacabacabacaba\n1000100010001000100010001000100010001000100010001000100010",
"output": "abacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabaa"
},
{
"input": "100 3\nabacabacabacabacabacabacabacabacabacabacaba\n0001000100010001000100010001000100010001000100010001000100",
"output": "aaaabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabaaa"
},
{
"input": "100 3\nabacabacabacabacabacabacabacabacabacabacaba\n0100010001000100010001000100010001000100010001000100010001",
"output": "aabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacaba"
},
{
"input": "100 3\nabacabacabacabacabacabacabacabacabacabacaba\n0100010001000100010000000100010001000100010001000100010001",
"output": "No solution"
},
{
"input": "100 3\nabacabacabacabacabacabacabacabacabacabacaba\n0100010001000100010000000100000001000100010000000100000001",
"output": "No solution"
},
{
"input": "100 3\nabacabacabacaba\n10001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "abacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabaa"
},
{
"input": "100 3\nabacabacabacaba\n00010001000100010001000100010001000100010001000100010001000100010001000100010001000100",
"output": "aaaabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabacabaaa"
},
{
"input": "100 3\nabacabacabacaba\n10001000100010001000100010000000100010001000100010001000000000001000100010001000100000",
"output": "No solution"
},
{
"input": "100 3\nabacabacabacaba\n10001000100010001000100010001000101010001000100010001000100010001000100010001000100010",
"output": "No solution"
},
{
"input": "100 3\nbcbabcbabcbabcb\n10001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "bcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcba"
},
{
"input": "100 3\nbcbabcbabcbabcb\n10001000100010001000100000001000100010001000100010001000100010001000100010001000100010",
"output": "No solution"
},
{
"input": "100 3\nbcbabcbabcbabcb\n10001000100010001000100010000000100010001000100000001000100000001000100010000000100010",
"output": "No solution"
},
{
"input": "100 3\nbcbabcbabcbabcb\n00001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "aaaabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcbabcba"
},
{
"input": "100 26\nbcbabcbabcbabcb\n10001000100010001000100010001000100010001000100010000000100010001000100010000000100010",
"output": "No solution"
},
{
"input": "100 2\naba\n10001000100010001000100010001000100010001000100010001000100010001000100010001000100010001000100010",
"output": "abaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaa"
},
{
"input": "100 2\na\n1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
"output": "abababababababababababababababababababababababababababababababababababababababababababababababababab"
},
{
"input": "4 2\naa\n101",
"output": "No solution"
},
{
"input": "100 2\naa\n100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100100",
"output": "aabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaabaaba"
},
{
"input": "5 2\naaa\n101",
"output": "No solution"
},
{
"input": "8 2\nabab\n10001",
"output": "No solution"
},
{
"input": "100 2\nbaabaaabaabaa\n1000000100000010000001000000100000010000001000000100000010000001000000100000010000001000",
"output": "baabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaabaabaaaaa"
},
{
"input": "100 2\nbaabaabaabaa\n10000010000010000010000010000010000010000010000010000010000010000010000010000010000010000",
"output": "No solution"
},
{
"input": "100 3\nbaabaabaabaa\n00000000010000000000010000000000010000000000010000000000010000000000010000000000010000000",
"output": "No solution"
},
{
"input": "100 3\nbaabaaabaabaa\n0000000000100000000000010000000000001000000000000100000000000010000000000001000000000000",
"output": "aaaaaaaaaabaabaaabaabaabaabaaabaabaabaabaaabaabaabaabaaabaabaabaabaaabaabaabaabaaabaabaaaaaaaaaaaaaa"
},
{
"input": "7 2\nbab\n10001",
"output": "babbbab"
},
{
"input": "10 2\nb\n0000000000",
"output": "aaaaaaaaaa"
},
{
"input": "3 2\nbb\n00",
"output": "aaa"
},
{
"input": "7 2\naba\n10001",
"output": "abaaaba"
}
] | 92 | 0 | 0 | 121,772 |
611 | New Year and Three Musketeers | [
"data structures",
"greedy",
"sortings"
] | null | null | Do you know the story about the three musketeers? Anyway, you must help them now.
Richelimakieu is a cardinal in the city of Bearis. He found three brave warriors and called them the three musketeers. Athos has strength *a*, Borthos strength *b*, and Caramis has strength *c*.
The year 2015 is almost over and there are still *n* criminals to be defeated. The *i*-th criminal has strength *t**i*. It's hard to defeat strong criminalsΒ β maybe musketeers will have to fight together to achieve it.
Richelimakieu will coordinate musketeers' actions. In each hour each musketeer can either do nothing or be assigned to one criminal. Two or three musketeers can be assigned to the same criminal and then their strengths are summed up. A criminal can be defeated in exactly one hour (also if two or three musketeers fight him). Richelimakieu can't allow the situation where a criminal has strength bigger than the sum of strengths of musketeers fighting himΒ β a criminal would win then!
In other words, there are three ways to defeat a criminal.
- A musketeer of the strength *x* in one hour can defeat a criminal of the strength not greater than *x*. So, for example Athos in one hour can defeat criminal *i* only if *t**i*<=β€<=*a*. - Two musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of strengths of these two musketeers. So, for example Athos and Caramis in one hour can defeat criminal *i* only if *t**i*<=β€<=*a*<=+<=*c*. Note that the third remaining musketeer can either do nothing or fight some other criminal. - Similarly, all three musketeers can fight together and in one hour defeat a criminal of the strength not greater than the sum of musketeers' strengths, i.e. *t**i*<=β€<=*a*<=+<=*b*<=+<=*c*.
Richelimakieu doesn't want musketeers to fight during the New Year's Eve. Thus, he must coordinate their actions in order to minimize the number of hours till all criminals will be defeated.
Find the minimum number of hours to defeat all criminals. If musketeers can't defeat them all then print "-1" (without the quotes) instead. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=200<=000)Β β the number of criminals.
The second line contains three integers *a*, *b* and *c* (1<=β€<=*a*,<=*b*,<=*c*<=β€<=108)Β β strengths of musketeers.
The third line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=108)Β β strengths of criminals. | Print one line with the answer.
If it's impossible to defeat all criminals, print "-1" (without the quotes). Otherwise, print the minimum number of hours the three musketeers will spend on defeating all criminals. | [
"5\n10 20 30\n1 1 1 1 50\n",
"5\n10 20 30\n1 1 1 1 51\n",
"7\n30 20 10\n34 19 50 33 88 15 20\n",
"6\n10 5 10\n10 9 5 25 20 5\n"
] | [
"2\n",
"3\n",
"-1\n",
"3\n"
] | In the first sample Athos has strength 10, Borthos 20, and Caramis 30. They can defeat all criminals in two hours:
- Borthos and Caramis should together fight a criminal with strength 50. In the same hour Athos can fight one of four criminals with strength 1. - There are three criminals left, each with strength 1. Each musketeer can fight one criminal in the second hour.
In the second sample all three musketeers must together fight a criminal with strength 51. It takes one hour. In the second hour they can fight separately, each with one criminal. In the third hour one criminal is left and any of musketeers can fight him. | [
{
"input": "5\n10 20 30\n1 1 1 1 50",
"output": "2"
},
{
"input": "5\n10 20 30\n1 1 1 1 51",
"output": "3"
},
{
"input": "7\n30 20 10\n34 19 50 33 88 15 20",
"output": "-1"
},
{
"input": "6\n10 5 10\n10 9 5 25 20 5",
"output": "3"
},
{
"input": "9\n10 20 30\n5 5 5 5 5 5 45 45 45",
"output": "4"
},
{
"input": "3\n1 1 1\n2 2 2",
"output": "3"
},
{
"input": "3\n2 2 2\n1 1 1",
"output": "1"
},
{
"input": "20\n43 36 6\n6 49 22 40 17 48 44 19 26 38 23 2 19 31 48 25 11 2 6 4",
"output": "8"
},
{
"input": "15\n3 46 35\n16 34 37 33 47 4 3 36 20 43 17 2 24 10 30",
"output": "7"
},
{
"input": "30\n34938093 71279712 25853338\n29827587 21741565 52179990 30235076 83272806 10815432 98887688 94542207 99870240 97586453 21739186 30460781 75347784 50711354 12162179 74306503 97398492 88466481 52489587 67579359 53177356 75077523 86044366 14405531 73916272 78242091 49321886 41937821 89258359 51438752",
"output": "18"
},
{
"input": "5\n85660180 78921313 51382024\n32512548 57103200 41738413 42188816 57569714",
"output": "2"
},
{
"input": "1\n1 1 1\n1",
"output": "1"
},
{
"input": "1\n1 1 1\n100000000",
"output": "-1"
},
{
"input": "1\n100000000 100000000 100000000\n1",
"output": "1"
},
{
"input": "1\n100000000 100000000 100000000\n100000000",
"output": "1"
},
{
"input": "4\n6 7 8\n8 8 8 12",
"output": "2"
}
] | 0 | 0 | -1 | 122,412 |
|
372 | Counting Rectangles is Fun | [
"brute force",
"divide and conquer",
"dp"
] | null | null | There is an *n*<=Γ<=*m* rectangular grid, each cell of the grid contains a single integer: zero or one. Let's call the cell on the *i*-th row and the *j*-th column as (*i*,<=*j*).
Let's define a "rectangle" as four integers *a*,<=*b*,<=*c*,<=*d* (1<=β€<=*a*<=β€<=*c*<=β€<=*n*;Β 1<=β€<=*b*<=β€<=*d*<=β€<=*m*). Rectangle denotes a set of cells of the grid {(*x*,<=*y*)Β :<=Β *a*<=β€<=*x*<=β€<=*c*,<=*b*<=β€<=*y*<=β€<=*d*}. Let's define a "good rectangle" as a rectangle that includes only the cells with zeros.
You should answer the following *q* queries: calculate the number of good rectangles all of which cells are in the given rectangle. | There are three integers in the first line: *n*, *m* and *q* (1<=β€<=*n*,<=*m*<=β€<=40,<=1<=β€<=*q*<=β€<=3Β·105). Each of the next *n* lines contains *m* characters β the grid. Consider grid rows are numbered from top to bottom, and grid columns are numbered from left to right. Both columns and rows are numbered starting from 1.
Each of the next *q* lines contains a query β four integers that describe the current rectangle, *a*, *b*, *c*, *d* (1<=β€<=*a*<=β€<=*c*<=β€<=*n*;Β 1<=β€<=*b*<=β€<=*d*<=β€<=*m*). | For each query output an answer β a single integer in a separate line. | [
"5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3\n",
"4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7\n"
] | [
"10\n1\n7\n34\n5\n",
"3\n1\n16\n27\n52\n"
] | For the first example, there is a 5βΓβ5 rectangular grid, and the first, the second, and the third queries are represented in the following image.
- For the first query, there are 10 good rectangles, five 1βΓβ1, two 2βΓβ1, two 1βΓβ2, and one 1βΓβ3. - For the second query, there is only one 1βΓβ1 good rectangle. - For the third query, there are 7 good rectangles, four 1βΓβ1, two 2βΓβ1, and one 3βΓβ1. | [
{
"input": "5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3",
"output": "10\n1\n7\n34\n5"
},
{
"input": "4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7",
"output": "3\n1\n16\n27\n52"
},
{
"input": "10 10 10\n0010001010\n0011011010\n0100001110\n0010100000\n0000100011\n0100000001\n0000010100\n1000000010\n0000010000\n0110010000\n1 1 4 6\n2 6 3 10\n1 6 3 9\n6 5 8 5\n2 3 6 8\n4 7 5 8\n3 1 9 5\n2 5 4 10\n2 3 3 6\n7 4 9 5",
"output": "42\n5\n5\n6\n79\n9\n151\n27\n12\n18"
},
{
"input": "10 10 10\n0001000111\n1101000111\n1100101111\n1011101011\n1011110101\n1110101110\n1101101111\n1111111100\n0110101110\n1011101011\n6 3 7 9\n2 6 8 7\n9 7 10 8\n3 1 5 9\n1 6 9 10\n6 3 8 8\n1 6 9 10\n5 6 8 10\n8 8 10 8\n9 6 9 10",
"output": "5\n12\n1\n12\n29\n5\n29\n9\n1\n2"
},
{
"input": "3 2 8\n01\n00\n00\n3 2 3 2\n1 1 2 1\n2 1 3 1\n2 2 3 2\n1 1 2 2\n1 1 1 2\n1 1 3 1\n1 1 2 2",
"output": "1\n3\n3\n3\n5\n1\n6\n5"
},
{
"input": "2 3 8\n000\n001\n1 1 2 2\n1 3 2 3\n2 2 2 3\n1 1 2 2\n2 3 2 3\n2 1 2 2\n1 2 1 3\n1 2 2 2",
"output": "9\n1\n1\n9\n0\n3\n3\n3"
}
] | 61 | 2,867,200 | -1 | 122,416 |
|
175 | Gnomes of Might and Magic | [
"data structures",
"graphs",
"implementation",
"shortest paths"
] | null | null | Vasya plays a popular game the Gnomes of Might and Magic.
In this game Vasya manages the kingdom of gnomes, consisting of several castles, connected by bidirectional roads. The kingdom road network has a special form. The kingdom has *m* main castles *a*1,<=*a*2,<=...,<=*a**m*, which form the Good Path. This path consists of roads between the castles *a**i*,<=*a**i*<=+<=1 (1<=β€<=*i*<=<<=*m*) as well as the road between *a**m* and *a*1. There are no other roads between the castles of the Good Path.
In addition, for each pair of neighboring Good Path castles *u* and *v* there is exactly one Evil Shortcut β a path that goes along the roads leading from the first castle (*u*) to the second one (*v*) and not having any common vertexes with the Good Path except for the vertexes *u* and *v*. It is known that there are no other roads and castles in the kingdom there, that is, every road and every castle lies either on the Good Path or the Evil Shortcut (castles can lie in both of them). In addition, no two Evil Shortcuts have any common castles, different than the castles of the Good Path.
At the beginning of each week in the kingdom appears one very bad gnome who stands on one of the roads of the kingdom, and begins to rob the corovans going through this road. One road may accumulate multiple very bad gnomes. Vasya cares about his corovans, so sometimes he sends the Mission of Death from one castle to another.
Let's suggest that the Mission of Death should get from castle *s* to castle *t*. Then it will move from castle *s* to castle *t*, destroying all very bad gnomes, which are on the roads of the Mission's path. Vasya is so tough that his Mission of Death can destroy any number of gnomes on its way. However, Vasya is very kind, so he always chooses such path between castles *s* and *t*, following which he will destroy the smallest number of gnomes. If there are multiple such paths, then Vasya chooses the path that contains the smallest number of roads among them. If there are multiple such paths still, Vasya chooses the lexicographically minimal one among them.
Help Vasya to simulate the life of the kingdom in the Gnomes of Might and Magic game.
A path is a sequence of castles, such that each pair of the neighboring castles on the path is connected by a road. Also, path *x*1,<=*x*2,<=... ,<=*x**p* is lexicographically less than path *y*1,<=*y*2,<=... ,<=*y**q*, if either *p*<=<<=*q* and *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x**p*<==<=*y**p*, or exists such number *r* (*r*<=<<=*p*,<=*r*<=<<=*q*), that *x*1<==<=*y*1,<=*x*2<==<=*y*2,<=... ,<=*x**r*<==<=*y**r* and *x**r*<=+<=1<=<<=*y**r*<=+<=1. | The first line contains two integers *n* and *m* (3<=β€<=*m*<=β€<=*n*<=β€<=100000) β the number of castles in the kingdom, and the number of castles on the Good Path, respectively.
The second line contains *m* integers, which are numbers of Good Path castles (the castles are numbered from 1 to *n*) in the order of occurrence on the Path, starting with some castle. All Good Path castles are different.
Each of the following *m* lines describes an Evil Shortcut. First a line contains an integer *k**i* (3<=β€<=*k**i*<=β€<=100000) β the number of castles on the corresponding Evil Shortcut (with the two castles which are on the Good Path), followed by a *k**i* integers β number of castles in the order of occurrence in the given Shortcut. All castles in one Evil Shortcut are different. It is guaranteed that the first and the last castles from the Shortcut are on the Good Path and the first castles in the Evil Shortcuts form the Good Path and are presented in the same order in which the Path was represented on the second line.
The next line contains an integer *q* (1<=β€<=*q*<=β€<=100000) β the number of events in the life of the kingdom. Each of the following *q* lines describes a single event. An event is described by the symbol *c**j* and two numbers or castles *s**j* and *t**j* (the character and numbers of castles are separated by a single space). If the character of *c**j* is equal to "+" (a plus), it means that a very bad gnome (probably not the first one) has appeared on the road between castles *s**j* and *t**j*. If *c**j* equals "?" (a question), then Vasya sent a Mission of Death from castle *s**j* to castle *t**j*. It is guaranteed that for each request "+", the road between castles *s**j* and *t**j* exists. The events are given in chronological order, starting with the earliest one. Initially there are no very bad gnomes on the roads.
All numbers in all lines are separated by single spaces. It is guaranteed that all the given Evil Shortcuts and Good Path fit in the limitations given in the problem statement. | For each query "?" print a single number on a single line β the number of very bad gnomes destroyed by the corresponding Mission of Death. Print the answers to queries in the chronological order. | [
"6 3\n1 2 3\n3 1 4 2\n3 2 5 3\n3 3 6 1\n10\n+ 1 2\n+ 4 2\n+ 1 3\n+ 2 3\n? 1 2\n+ 2 5\n? 1 2\n? 1 2\n+ 1 2\n? 1 2\n"
] | [
"0\n1\n0\n1\n"
] | In the example after the first four requests there is only one path from castle 1 to castle 2, which does not contain roads with very bad gnomes: 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 6 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 3 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 5 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2.
After a gnome stood on the road (2, 5), the next Mission of Death moves along path 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 destroys the gnome, who was on the road (1, 2). The next Mission of Death follows the same path which is already free of gnomes.
After yet another gnome stood on the road (1, 2), the next Mission of Death goes on the path 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 kills the gnome. | [] | 60 | 0 | 0 | 122,889 |
|
273 | Dima and Figure | [
"dp"
] | null | null | Dima loves making pictures on a piece of squared paper. And yet more than that Dima loves the pictures that depict one of his favorite figures.
A piece of squared paper of size *n*<=Γ<=*m* is represented by a table, consisting of *n* rows and *m* columns. All squares are white on blank squared paper. Dima defines a picture as an image on a blank piece of paper, obtained by painting some squares black.
The picture portrays one of Dima's favorite figures, if the following conditions hold:
- The picture contains at least one painted cell; - All painted cells form a connected set, that is, you can get from any painted cell to any other one (you can move from one cell to a side-adjacent one); - The minimum number of moves needed to go from the painted cell at coordinates (*x*1,<=*y*1) to the painted cell at coordinates (*x*2,<=*y*2), moving only through the colored cells, equals |*x*1<=-<=*x*2|<=+<=|*y*1<=-<=*y*2|.
Now Dima is wondering: how many paintings are on an *n*<=Γ<=*m* piece of paper, that depict one of his favorite figures? Count this number modulo 1000000007Β (109<=+<=7). | The first line contains two integers *n* and *m* β the sizes of the piece of paper (1<=β€<=*n*,<=*m*<=β€<=150). | In a single line print the remainder after dividing the answer to the problem by number 1000000007Β (109<=+<=7). | [
"2 2\n",
"3 4\n"
] | [
"13\n",
"571\n"
] | none | [
{
"input": "2 2",
"output": "13"
},
{
"input": "3 4",
"output": "571"
},
{
"input": "2 8",
"output": "948"
},
{
"input": "5 1",
"output": "15"
},
{
"input": "10 5",
"output": "10779285"
},
{
"input": "9 9",
"output": "571345021"
},
{
"input": "3 5",
"output": "1615"
},
{
"input": "6 6",
"output": "839603"
},
{
"input": "2 8",
"output": "948"
},
{
"input": "2 2",
"output": "13"
},
{
"input": "146 93",
"output": "368200478"
},
{
"input": "28 37",
"output": "3734743"
},
{
"input": "142 55",
"output": "433027432"
},
{
"input": "3 4",
"output": "571"
},
{
"input": "143 83",
"output": "842711873"
},
{
"input": "22 117",
"output": "793127317"
},
{
"input": "69 96",
"output": "100750890"
},
{
"input": "48 127",
"output": "357232393"
},
{
"input": "72 139",
"output": "434779014"
},
{
"input": "70 113",
"output": "71753563"
},
{
"input": "18 50",
"output": "12437081"
},
{
"input": "86 145",
"output": "366385145"
},
{
"input": "54 112",
"output": "188504522"
},
{
"input": "123 34",
"output": "545361930"
},
{
"input": "124 15",
"output": "140640136"
},
{
"input": "142 62",
"output": "639963561"
},
{
"input": "54 119",
"output": "428252664"
},
{
"input": "48 45",
"output": "740225031"
},
{
"input": "113 58",
"output": "254095439"
},
{
"input": "88 110",
"output": "29408305"
},
{
"input": "24 142",
"output": "173373825"
},
{
"input": "80 29",
"output": "93626305"
},
{
"input": "17 36",
"output": "9671769"
},
{
"input": "141 43",
"output": "502872090"
},
{
"input": "139 107",
"output": "963974540"
},
{
"input": "41 93",
"output": "684818482"
},
{
"input": "65 149",
"output": "443564197"
},
{
"input": "147 106",
"output": "472939563"
},
{
"input": "141 130",
"output": "533425071"
},
{
"input": "150 150",
"output": "498249215"
},
{
"input": "1 2",
"output": "3"
},
{
"input": "2 1",
"output": "3"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "3 3",
"output": "167"
}
] | 60 | 0 | 0 | 122,898 |
|
136 | Ternary Logic | [
"implementation",
"math"
] | null | null | Little Petya very much likes computers. Recently he has received a new "Ternatron IV" as a gift from his mother. Unlike other modern computers, "Ternatron IV" operates with ternary and not binary logic. Petya immediately wondered how the *xor* operation is performed on this computer (and whether there is anything like it).
It turned out that the operation does exist (however, it is called *tor*) and it works like this. Suppose that we need to calculate the value of the expression *a* *tor* *b*. Both numbers *a* and *b* are written in the ternary notation one under the other one (*b* under *a*). If they have a different number of digits, then leading zeroes are added to the shorter number until the lengths are the same. Then the numbers are summed together digit by digit. The result of summing each two digits is calculated modulo 3. Note that there is no carry between digits (i. e. during this operation the digits aren't transferred). For example: 1410 *tor* 5010<==<=01123 *tor* 12123<==<=10213<==<=3410.
Petya wrote numbers *a* and *c* on a piece of paper. Help him find such number *b*, that *a* *tor* *b*<==<=*c*. If there are several such numbers, print the smallest one. | The first line contains two integers *a* and *c* (0<=β€<=*a*,<=*c*<=β€<=109). Both numbers are written in decimal notation. | Print the single integer *b*, such that *a* *tor* *b*<==<=*c*. If there are several possible numbers *b*, print the smallest one. You should print the number in decimal notation. | [
"14 34\n",
"50 34\n",
"387420489 225159023\n",
"5 5\n"
] | [
"50\n",
"14\n",
"1000000001\n",
"0\n"
] | none | [
{
"input": "14 34",
"output": "50"
},
{
"input": "50 34",
"output": "14"
},
{
"input": "387420489 225159023",
"output": "1000000001"
},
{
"input": "5 5",
"output": "0"
},
{
"input": "23476 23875625",
"output": "23860906"
},
{
"input": "11111 10101010",
"output": "10116146"
},
{
"input": "1 23865354",
"output": "23865356"
},
{
"input": "0 0",
"output": "0"
},
{
"input": "2376234 0",
"output": "4732515"
},
{
"input": "1 0",
"output": "2"
},
{
"input": "581130733 0",
"output": "1162261466"
},
{
"input": "581131733 1",
"output": "1162260467"
},
{
"input": "0 1000000000",
"output": "1000000000"
},
{
"input": "1000000000 0",
"output": "693711461"
},
{
"input": "1000000000 100000000",
"output": "650219958"
},
{
"input": "956747697 9487",
"output": "736688812"
},
{
"input": "229485033 8860",
"output": "308580772"
},
{
"input": "5341 813849430",
"output": "813850920"
},
{
"input": "227927516 956217829",
"output": "872370713"
},
{
"input": "390 380875228",
"output": "380874919"
},
{
"input": "336391083 911759145",
"output": "1135529718"
},
{
"input": "154618752 504073566",
"output": "753527130"
},
{
"input": "6436017 645491133",
"output": "639142839"
},
{
"input": "4232 755480607",
"output": "755485882"
},
{
"input": "19079106 69880743",
"output": "56293527"
},
{
"input": "460318555 440850074",
"output": "25179124"
},
{
"input": "227651149 379776728",
"output": "168088492"
},
{
"input": "621847819 8794",
"output": "1114556841"
},
{
"input": "827112516 566664600",
"output": "908742057"
},
{
"input": "460311350 820538776",
"output": "404875070"
},
{
"input": "276659168 241268656",
"output": "358409486"
},
{
"input": "9925 9952",
"output": "27"
},
{
"input": "830218526 438129941",
"output": "784719357"
},
{
"input": "630005197 848951646",
"output": "754665575"
},
{
"input": "123256190 174927955",
"output": "243699845"
},
{
"input": "937475611 769913258",
"output": "994719535"
},
{
"input": "561666539 29904379",
"output": "1152454076"
},
{
"input": "551731805 8515539",
"output": "1049769112"
},
{
"input": "6560 96330685",
"output": "96330968"
},
{
"input": "337894292 55",
"output": "243175169"
},
{
"input": "479225038 396637601",
"output": "47143216"
},
{
"input": "111174087 482024380",
"output": "430083082"
},
{
"input": "785233275 1523",
"output": "393767834"
},
{
"input": "47229813 6200",
"output": "89081162"
},
{
"input": "264662333 6952",
"output": "141903557"
},
{
"input": "523162963 922976263",
"output": "414184806"
},
{
"input": "6347 7416",
"output": "10549"
},
{
"input": "278014879 3453211",
"output": "171855414"
},
{
"input": "991084922 66",
"output": "690049933"
},
{
"input": "929361351 7373",
"output": "679915097"
},
{
"input": "532643581 213098335",
"output": "842718489"
},
{
"input": "69272798 718909239",
"output": "668771236"
},
{
"input": "440760623 316634331",
"output": "1052493562"
},
{
"input": "9001 9662",
"output": "1390"
},
{
"input": "417584836 896784933",
"output": "481392203"
},
{
"input": "640735701 335933492",
"output": "992169746"
},
{
"input": "5440 6647",
"output": "10711"
},
{
"input": "3545 6259",
"output": "3536"
},
{
"input": "847932562 1405",
"output": "488901051"
},
{
"input": "359103580 852",
"output": "201115550"
},
{
"input": "406369748 625641695",
"output": "221459919"
},
{
"input": "345157805 719310676",
"output": "504894191"
},
{
"input": "9150 823789822",
"output": "823781437"
},
{
"input": "8727 702561605",
"output": "702556127"
},
{
"input": "931392186 677650263",
"output": "923604336"
},
{
"input": "976954722 548418041",
"output": "862925051"
},
{
"input": "168971531 697371009",
"output": "588009082"
},
{
"input": "5849 7211",
"output": "10146"
},
{
"input": "934045591 4156",
"output": "661009836"
},
{
"input": "427471963 436868749",
"output": "67345761"
},
{
"input": "702754885 762686553",
"output": "81198815"
},
{
"input": "897312963 177161062",
"output": "620860447"
},
{
"input": "268520356 1999",
"output": "135088146"
},
{
"input": "635318406 289972012",
"output": "950864476"
},
{
"input": "237819544 904440360",
"output": "857959352"
},
{
"input": "44788825 4485",
"output": "89397617"
},
{
"input": "7376 994270908",
"output": "994283218"
},
{
"input": "893244884 654169485",
"output": "1095395095"
},
{
"input": "960725158 342144655",
"output": "548529624"
},
{
"input": "460645829 46697832",
"output": "792961330"
},
{
"input": "8389 172682371",
"output": "172696203"
},
{
"input": "294567098 631452590",
"output": "745235571"
},
{
"input": "5573 8790",
"output": "13021"
},
{
"input": "285938679 907528096",
"output": "1068058915"
},
{
"input": "774578699 101087409",
"output": "940495066"
},
{
"input": "153749013 598457896",
"output": "444892699"
},
{
"input": "364059865 346004232",
"output": "40934348"
},
{
"input": "237924125 573400957",
"output": "507664538"
},
{
"input": "987310001 827978268",
"output": "275919178"
},
{
"input": "922263603 387506683",
"output": "1064907553"
},
{
"input": "5712 384487208",
"output": "384482225"
},
{
"input": "9099 3208",
"output": "14035"
},
{
"input": "948688087 38251290",
"output": "768385433"
},
{
"input": "260153932 138945442",
"output": "271056231"
},
{
"input": "497129325 766959165",
"output": "276817557"
},
{
"input": "783390583 7679",
"output": "399664540"
},
{
"input": "657244587 28654748",
"output": "921153434"
},
{
"input": "455705795 757666961",
"output": "303798597"
},
{
"input": "815932189 211656771",
"output": "562850021"
},
{
"input": "511307975 307916669",
"output": "1137612240"
},
{
"input": "274842194 1000000000",
"output": "1162261466"
}
] | 124 | 0 | 3 | 123,455 |
|
838 | Future Failure | [
"dp",
"games"
] | null | null | Alice and Bob are playing a game with a string of characters, with Alice going first. The string consists *n* characters, each of which is one of the first *k* letters of the alphabet. On a playerβs turn, they can either arbitrarily permute the characters in the words, or delete exactly one character in the word (if there is at least one character). In addition, their resulting word cannot have appeared before throughout the entire game. The player unable to make a valid move loses the game.
Given *n*,<=*k*,<=*p*, find the number of words with exactly *n* characters consisting of the first *k* letters of the alphabet such that Alice will win if both Alice and Bob play optimally. Return this number modulo the prime number *p*. | The first line of input will contain three integers *n*,<=*k*,<=*p* (1<=β€<=*n*<=β€<=250<=000, 1<=β€<=*k*<=β€<=26, 108<=β€<=*p*<=β€<=109<=+<=100, *p* will be prime). | Print a single integer, the number of winning words for Alice, modulo *p*. | [
"4 2 100000007\n"
] | [
"14\n"
] | There are 14 strings that that Alice can win with. For example, some strings are "bbaa" and "baaa". Alice will lose on strings like "aaaa" or "bbbb". | [] | 46 | 0 | 0 | 123,481 |
|
107 | Darts | [
"geometry",
"probabilities"
] | E. Darts | 5 | 256 | The night after the graduation ceremony graduate students of German University in Cairo (GUC) are playing darts. As there's no real dart board available, the photographs of members of the GUC upper management are being used.
So, *n* rectangular photos are placed on the wall. They can overlap arbitrary and even coincide. The photos are not necessarily placed horizontally or vertically, they could also be rotated before being pinned to the wall.
The score of one dart throw is simply the number of photos the dart went through.
Fatma has made a throw but her score was not recorded. She only remembers that she did make it into at least one photo.
Assuming that the probability distribution of the throw is equal across the whole wall, what would be the expectation of Fatma's score? | The first line of input contains integer *n* (1<=β€<=*n*<=β€<=500) β the number of photos on the wall. Then follow *n* lines describing the photos, each containing 8 single-space-separated integers (coordinates of 4 vertices): *x*1, *y*1, *x*2, *y*2, *x*3, *y*3, *x*4, *y*4. Each photo is a rectangle with a nonzero area. The coordinates are integers, not exceeding 104 by absolute value. The coordinates of the rectangle are given in either clockwise or counterclockwise order. | Print the expected score of the throw. The answer will be accepted if it has absolute or relative error not exceeding 10<=-<=6. | [
"1\n0 0 0 2 2 2 2 0\n",
"1\n-1 0 0 1 1 0 0 -1\n",
"4\n0 0 0 1 3 1 3 0\n0 0 0 3 1 3 1 0\n3 3 2 3 2 0 3 0\n3 3 3 2 0 2 0 3\n",
"2\n-1 0 0 1 1 0 0 -1\n0 0 1 1 2 0 1 -1\n"
] | [
"1.0000000000\n",
"1.0000000000\n",
"1.5000000000\n",
"1.1428571429\n"
] | none | [] | 60 | 0 | 0 | 123,523 |
82 | General Mobilization | [
"data structures",
"dfs and similar",
"sortings"
] | C. General Mobilization | 2 | 256 | The Berland Kingdom is a set of *n* cities connected with each other with *n*<=-<=1 railways. Each road connects exactly two different cities. The capital is located in city 1. For each city there is a way to get from there to the capital by rail.
In the *i*-th city there is a soldier division number *i*, each division is characterized by a number of *a**i*. It represents the priority, the smaller the number, the higher the priority of this division. All values of *a**i* are different.
One day the Berland King Berl Great declared a general mobilization, and for that, each division should arrive in the capital. Every day from every city except the capital a train departs. So there are exactly *n*<=-<=1 departing trains each day. Each train moves toward the capital and finishes movement on the opposite endpoint of the railway on the next day. It has some finite capacity of *c**j*, expressed in the maximum number of divisions, which this train can transport in one go. Each train moves in the direction of reducing the distance to the capital. So each train passes exactly one railway moving from a city to the neighboring (where it stops) toward the capital.
In the first place among the divisions that are in the city, division with the smallest number of *a**i* get on the train, then with the next smallest and so on, until either the train is full or all the divisions are be loaded. So it is possible for a division to stay in a city for a several days.
The duration of train's progress from one city to another is always equal to 1 day. All divisions start moving at the same time and end up in the capital, from where they don't go anywhere else any more. Each division moves along a simple path from its city to the capital, regardless of how much time this journey will take.
Your goal is to find for each division, in how many days it will arrive to the capital of Berland. The countdown begins from day 0. | The first line contains the single integer *n* (1<=β€<=*n*<=β€<=5000). It is the number of cities in Berland. The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* represents the priority of the division, located in the city number *i*. All numbers *a*1,<=*a*2,<=...,<=*a**n* are different (1<=β€<=*a**i*<=β€<=109). Then *n*<=-<=1 lines contain the descriptions of the railway roads. Each description consists of three integers *v**j*,<=*u**j*,<=*c**j*, where *v**j*, *u**j* are number of cities connected by the *j*-th rail, and *c**j* stands for the maximum capacity of a train riding on this road (1<=β€<=*v**j*,<=*u**j*<=β€<=*n*,<=*v**j*<=β <=*u**j*, 1<=β€<=*c**j*<=β€<=*n*). | Print sequence *t*1,<=*t*2,<=...,<=*t**n*, where *t**i* stands for the number of days it takes for the division of city *i* to arrive to the capital. Separate numbers with spaces. | [
"4\n40 10 30 20\n1 2 1\n2 3 1\n4 2 1\n",
"5\n5 4 3 2 1\n1 2 1\n2 3 1\n2 4 1\n4 5 1\n"
] | [
"0 1 3 2 ",
"0 1 4 2 3 "
] | none | [] | 92 | 0 | 0 | 123,737 |
553 | Nudist Beach | [
"binary search",
"graphs",
"greedy"
] | null | null | Nudist Beach is planning a military operation to attack the Life Fibers. In this operation, they will attack and capture several cities which are currently under the control of the Life Fibers.
There are *n* cities, labeled from 1 to *n*, and *m* bidirectional roads between them. Currently, there are Life Fibers in every city. In addition, there are *k* cities that are fortresses of the Life Fibers that cannot be captured under any circumstances. So, the Nudist Beach can capture an arbitrary non-empty subset of cities with no fortresses.
After the operation, Nudist Beach will have to defend the captured cities from counterattack. If they capture a city and it is connected to many Life Fiber controlled cities, it will be easily defeated. So, Nudist Beach would like to capture a set of cities such that for each captured city the ratio of Nudist Beach controlled neighbors among all neighbors of that city is as high as possible.
More formally, they would like to capture a non-empty set of cities *S* with no fortresses of Life Fibers. The strength of a city is defined as (number of neighbors of *x* in *S*) / (total number of neighbors of *x*). Here, two cities are called neighbors if they are connnected with a road. The goal is to maximize the strength of the weakest city in *S*.
Given a description of the graph, and the cities with fortresses, find a non-empty subset that maximizes the strength of the weakest city. | The first line of input contains three integers *n*,<=*m*,<=*k* (2<=<=β€<=<=*n*<=<=β€<=100<=000, 1<=β€<=*m*<=β€<=100<=000, 1<=β€<=*k*<=β€<=*n*<=-<=1).
The second line of input contains *k* integers, representing the cities with fortresses. These cities will all be distinct.
The next *m* lines contain the roads. The *i*-th of these lines will have 2 integers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*). Every city will have at least one road adjacent to it.
There is no more than one road between each pair of the cities. | The first line should contain an integer *r*, denoting the size of an optimum set (1<=β€<=*r*<=β€<=*n*<=-<=*k*).
The second line should contain *r* integers, denoting the cities in the set. Cities may follow in an arbitrary order. This line should not contain any of the cities with fortresses.
If there are multiple possible answers, print any of them. | [
"9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9\n",
"10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4\n"
] | [
"3\n1 4 5\n",
"8\n1 5 4 8 10 6 3 7\n"
] | The first example case achieves a strength of 1/2. No other subset is strictly better.
The second example case achieves a strength of 1. Note that the subset doesn't necessarily have to be connected. | [
{
"input": "9 8 4\n3 9 6 8\n1 2\n1 3\n1 4\n1 5\n2 6\n2 7\n2 8\n2 9",
"output": "3\n5 1 4"
},
{
"input": "10 8 2\n2 9\n1 3\n2 9\n4 5\n5 6\n6 7\n7 8\n8 10\n10 4",
"output": "8\n3 10 6 1 5 8 7 4"
},
{
"input": "2 1 1\n1\n2 1",
"output": "1\n2"
}
] | 30 | 0 | 0 | 123,972 |
|
666 | Codeword | [
"combinatorics",
"strings"
] | null | null | The famous sculptor Cicasso is a Reberlandian spy!
These is breaking news in Berlandian papers today. And now the sculptor is hiding. This time you give the shelter to the maestro. You have a protected bunker and you provide it to your friend. You set the security system in such way that only you can open the bunker. To open it one should solve the problem which is hard for others but is simple for you.
Every day the bunker generates a codeword *s*. Every time someone wants to enter the bunker, integer *n* appears on the screen. As the answer one should enter another integer β the residue modulo 109<=+<=7 of the number of strings of length *n* that consist only of lowercase English letters and contain the string *s* as the subsequence.
The subsequence of string *a* is a string *b* that can be derived from the string *a* by removing some symbols from it (maybe none or all of them). In particular any string is the subsequence of itself. For example, the string "cfo" is the subsequence of the string "codeforces".
You haven't implemented the algorithm that calculates the correct answers yet and you should do that ASAP. | The first line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of the events in the test case.
The second line contains nonempty string *s* β the string generated by the bunker for the current day.
The next *m* lines contain the description of the events. The description starts from integer *t* β the type of the event.
If *t*<==<=1 consider a new day has come and now a new string *s* is used. In that case the same line contains a new value of the string *s*.
If *t*<==<=2 integer *n* is given (1<=β€<=*n*<=β€<=105). This event means that it's needed to find the answer for the current string *s* and the value *n*.
The sum of lengths of all generated strings doesn't exceed 105. All of the given strings consist only of lowercase English letters. | For each query of the type 2 print the answer modulo 109<=+<=7 on the separate line. | [
"3\na\n2 2\n1 bc\n2 5\n"
] | [
"51\n162626\n"
] | In the first event words of the form "a?" and "?a" are counted, where ? is an arbitrary symbol. There are 26 words of each of these types, but the word "aa" satisfies both patterns, so the answer is 51. | [] | 46 | 0 | 0 | 124,025 |
|
1,000 | We Need More Bosses | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Your friend is developing a computer game. He has already decided how the game world should look like β it should consist of $n$ locations connected by $m$ two-way passages. The passages are designed in such a way that it should be possible to get from any location to any other location.
Of course, some passages should be guarded by the monsters (if you just can go everywhere without any difficulties, then it's not fun, right?). Some crucial passages will be guarded by really fearsome monsters, requiring the hero to prepare for battle and designing his own tactics of defeating them (commonly these kinds of monsters are called bosses). And your friend wants you to help him place these bosses.
The game will start in location $s$ and end in location $t$, but these locations are not chosen yet. After choosing these locations, your friend will place a boss in each passage such that it is impossible to get from $s$ to $t$ without using this passage. Your friend wants to place as much bosses as possible (because more challenges means more fun, right?), so he asks you to help him determine the maximum possible number of bosses, considering that any location can be chosen as $s$ or as $t$. | The first line contains two integers $n$ and $m$ ($2 \le n \le 3 \cdot 10^5$, $n - 1 \le m \le 3 \cdot 10^5$) β the number of locations and passages, respectively.
Then $m$ lines follow, each containing two integers $x$ and $y$ ($1 \le x, y \le n$, $x \ne y$) describing the endpoints of one of the passages.
It is guaranteed that there is no pair of locations directly connected by two or more passages, and that any location is reachable from any other location. | Print one integer β the maximum number of bosses your friend can place, considering all possible choices for $s$ and $t$. | [
"5 5\n1 2\n2 3\n3 1\n4 1\n5 2\n",
"4 3\n1 2\n4 3\n3 2\n"
] | [
"2\n",
"3\n"
] | none | [
{
"input": "5 5\n1 2\n2 3\n3 1\n4 1\n5 2",
"output": "2"
},
{
"input": "4 3\n1 2\n4 3\n3 2",
"output": "3"
},
{
"input": "50 72\n35 38\n19 46\n35 12\n27 30\n23 41\n50 16\n31 6\n20 33\n38 1\n10 35\n13 43\n29 25\n25 4\n1 13\n4 20\n36 29\n13 47\n48 5\n30 21\n30 38\n28 50\n41 45\n25 43\n40 36\n19 47\n31 32\n26 28\n8 13\n43 24\n27 35\n14 29\n14 38\n50 9\n34 49\n12 34\n43 42\n41 15\n46 29\n48 11\n48 7\n40 29\n48 21\n18 26\n15 11\n12 39\n18 42\n27 12\n6 9\n19 37\n45 39\n5 36\n37 22\n18 49\n15 38\n24 42\n34 45\n43 3\n2 1\n18 48\n43 30\n26 10\n30 13\n2 27\n8 3\n22 42\n14 4\n14 27\n13 3\n14 10\n41 25\n17 50\n44 9",
"output": "8"
},
{
"input": "5 6\n1 5\n2 3\n3 5\n2 1\n2 5\n2 4",
"output": "1"
}
] | 342 | 16,179,200 | 0 | 124,111 |
|
785 | Anton and Permutation | [
"brute force",
"data structures"
] | null | null | Anton likes permutations, especially he likes to permute their elements. Note that a permutation of *n* elements is a sequence of numbers {*a*1,<=*a*2,<=...,<=*a**n*}, in which every number from 1 to *n* appears exactly once.
One day Anton got a new permutation and started to play with it. He does the following operation *q* times: he takes two elements of the permutation and swaps these elements. After each operation he asks his friend Vanya, how many inversions there are in the new permutation. The number of inversions in a permutation is the number of distinct pairs (*i*,<=*j*) such that 1<=β€<=*i*<=<<=*j*<=β€<=*n* and *a**i*<=><=*a**j*.
Vanya is tired of answering Anton's silly questions. So he asked you to write a program that would answer these questions instead of him.
Initially Anton's permutation was {1,<=2,<=...,<=*n*}, that is *a**i*<==<=*i* for all *i* such that 1<=β€<=*i*<=β€<=*n*. | The first line of the input contains two integers *n* and *q* (1<=β€<=*n*<=β€<=200<=000,<=1<=β€<=*q*<=β€<=50<=000)Β β the length of the permutation and the number of operations that Anton does.
Each of the following *q* lines of the input contains two integers *l**i* and *r**i* (1<=β€<=*l**i*,<=*r**i*<=β€<=*n*)Β β the indices of elements that Anton swaps during the *i*-th operation. Note that indices of elements that Anton swaps during the *i*-th operation can coincide. Elements in the permutation are numbered starting with one. | Output *q* lines. The *i*-th line of the output is the number of inversions in the Anton's permutation after the *i*-th operation. | [
"5 4\n4 5\n2 4\n2 5\n2 2\n",
"2 1\n2 1\n",
"6 7\n1 4\n3 5\n2 3\n3 3\n3 6\n2 1\n5 1\n"
] | [
"1\n4\n3\n3\n",
"1\n",
"5\n6\n7\n7\n10\n11\n8\n"
] | Consider the first sample.
After the first Anton's operation the permutation will be {1,β2,β3,β5,β4}. There is only one inversion in it: (4,β5).
After the second Anton's operation the permutation will be {1,β5,β3,β2,β4}. There are four inversions: (2,β3), (2,β4), (2,β5) and (3,β4).
After the third Anton's operation the permutation will be {1,β4,β3,β2,β5}. There are three inversions: (2,β3), (2,β4) and (3,β4).
After the fourth Anton's operation the permutation doesn't change, so there are still three inversions. | [
{
"input": "5 4\n4 5\n2 4\n2 5\n2 2",
"output": "1\n4\n3\n3"
},
{
"input": "2 1\n2 1",
"output": "1"
},
{
"input": "6 7\n1 4\n3 5\n2 3\n3 3\n3 6\n2 1\n5 1",
"output": "5\n6\n7\n7\n10\n11\n8"
},
{
"input": "42 42\n23 7\n9 6\n17 22\n19 23\n15 42\n14 10\n17 12\n2 25\n8 39\n13 9\n29 26\n17 2\n11 14\n33 20\n29 1\n4 28\n25 4\n37 32\n6 37\n30 42\n39 1\n12 29\n39 3\n6 41\n26 34\n11 21\n21 32\n20 35\n6 22\n32 35\n34 40\n33 14\n13 10\n1 2\n5 12\n21 9\n41 30\n5 27\n29 35\n22 32\n1 1\n2 10",
"output": "31\n34\n43\n40\n91\n98\n103\n146\n199\n202\n207\n196\n195\n214\n257\n294\n259\n266\n305\n302\n267\n248\n289\n306\n309\n316\n335\n338\n323\n328\n333\n342\n341\n342\n341\n356\n361\n390\n387\n384\n384\n383"
},
{
"input": "1 1\n1 1",
"output": "0"
},
{
"input": "200000 2\n42 199685\n65536 65536",
"output": "399285\n399285"
},
{
"input": "200000 1\n92734 60628",
"output": "64211"
},
{
"input": "50000 6\n15501 15522\n25641 25642\n37466 37572\n37500 37502\n49148 49222\n9584 9585",
"output": "41\n42\n253\n256\n403\n404"
}
] | 46 | 0 | 0 | 124,220 |
|
0 | none | [
"none"
] | null | null | Santa Claus has *n* tangerines, and the *i*-th of them consists of exactly *a**i* slices. Santa Claus came to a school which has *k* pupils. Santa decided to treat them with tangerines.
However, there can be too few tangerines to present at least one tangerine to each pupil. So Santa decided to divide tangerines into parts so that no one will be offended. In order to do this, he can divide a tangerine or any existing part into two smaller equal parts. If the number of slices in the part he wants to split is odd, then one of the resulting parts will have one slice more than the other. It's forbidden to divide a part consisting of only one slice.
Santa Claus wants to present to everyone either a whole tangerine or exactly one part of it (that also means that everyone must get a positive number of slices). One or several tangerines or their parts may stay with Santa.
Let *b**i* be the number of slices the *i*-th pupil has in the end. Let Santa's joy be the minimum among all *b**i*'s.
Your task is to find the maximum possible joy Santa can have after he treats everyone with tangerines (or their parts). | The first line contains two positive integers *n* and *k* (1<=β€<=*n*<=β€<=106, 1<=β€<=*k*<=β€<=2Β·109) denoting the number of tangerines and the number of pupils, respectively.
The second line consists of *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=107), where *a**i* stands for the number of slices the *i*-th tangerine consists of. | If there's no way to present a tangerine or a part of tangerine to everyone, print -1. Otherwise, print the maximum possible joy that Santa can have. | [
"3 2\n5 9 3\n",
"2 4\n12 14\n",
"2 3\n1 1\n"
] | [
"5\n",
"6\n",
"-1\n"
] | In the first example Santa should divide the second tangerine into two parts with 5 and 4 slices. After that he can present the part with 5 slices to the first pupil and the whole first tangerine (with 5 slices, too) to the second pupil.
In the second example Santa should divide both tangerines, so that he'll be able to present two parts with 6 slices and two parts with 7 slices.
In the third example Santa Claus can't present 2 slices to 3 pupils in such a way that everyone will have anything. | [
{
"input": "3 2\n5 9 3",
"output": "5"
},
{
"input": "2 4\n12 14",
"output": "6"
},
{
"input": "2 3\n1 1",
"output": "-1"
},
{
"input": "1 1\n10",
"output": "10"
},
{
"input": "2 2\n6 10",
"output": "6"
},
{
"input": "5 3\n199999 200000 199999 199999 200000",
"output": "199999"
},
{
"input": "10 2\n26 27 32 28 22 23 30 26 28 23",
"output": "30"
},
{
"input": "20 100\n52 55 53 54 48 47 50 45 52 51 51 51 47 45 50 51 53 55 51 52",
"output": "6"
},
{
"input": "30 10\n40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69",
"output": "60"
},
{
"input": "50 300\n9293 9540 6585 6756 215 6853 4819 3033 5529 8201 4072 4862 4071 5500 5717 229 7696 5440 4407 1108 1680 1974 5414 9053 8480 1030 5556 5551 4741 452 1045 2553 8944 7627 3737 3876 2846 3563 3246 8436 1075 2974 9410 1127 1968 830 1380 7371 6414 6384",
"output": "559"
},
{
"input": "100 400\n3640 1748 2277 2703 3458 3707 2414 806 2073 3951 1463 1352 1129 2788 2874 1391 1234 3643 3124 3823 488 538 3991 1014 2695 1660 3853 3843 1865 866 639 2134 1454 2174 759 567 1473 1535 747 1379 1239 2177 2190 960 1137 2916 2723 2148 2895 1187 3811 3058 2666 950 3836 758 918 3551 1122 3238 2174 3028 855 3172 3423 2763 3304 2218 1991 1635 3941 3705 2126 1386 956 2274 405 2391 1040 2632 3313 3557 1320 2786 2330 1840 1753 3525 1314 1719 1739 1260 1132 1626 1499 3963 3505 1577 3944 3359",
"output": "414"
},
{
"input": "4 5\n1 2 3 200000",
"output": "25000"
},
{
"input": "1 1\n10000000",
"output": "10000000"
},
{
"input": "100 5049\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100",
"output": "1"
},
{
"input": "1 200\n199",
"output": "-1"
},
{
"input": "1 3\n7",
"output": "2"
},
{
"input": "1 3\n31",
"output": "8"
},
{
"input": "1 3\n11",
"output": "3"
},
{
"input": "1 2\n2",
"output": "1"
},
{
"input": "1 3\n15",
"output": "4"
}
] | 1,231 | 268,390,400 | 0 | 124,254 |
|
306 | Optimizer | [
"data structures",
"greedy",
"sortings"
] | null | null | A process RAM is a sequence of bytes that are indexed from 1 to *n*. Polycarpus's program contains such instructions as "memset", that is, the operations of filling memory cells on a segment with some value. The details are: the code only contains *m* instructions that look like "set13 a_i l_i". Instruction *i* fills a continuous memory segment of length *l**i*, starting from cell number *a**i*, (that it cells with numbers *a**i*,<=*a**i*<=+<=1,<=...,<=*a**i*<=+<=*l**i*<=-<=1) with values 13.
In Polycarpus's code, the optimizer's task is to remove the maximum number of instructions from his code in such a way that the remaining instructions set value 13 in all the memory bytes that got this value from the code before the optimization. Also, the value 13 should be set only in the memory bytes that got this value from the code before the optimization. Your task is to implement the optimizer for such program. | The first line contains integers *n* and *m* (1<=β€<=*n*<=β€<=2Β·106,<=1<=β€<=*m*<=β€<=2Β·105) β the number of bytes (memory cells) and the number of instructions in Polycarpus's code. Then *m* lines follow, each line contains a pair of integers *a**i*, *l**i* (1<=β€<=*a**i*<=β€<=*n*,<=1<=β€<=*l**i*<=β€<=*n*<=-<=*a**i*<=+<=1). | Print in the first line the sought maximum number of instructions that can be removed from the code. In the second line print the numbers of the instructions. The instructions are numbered from 1 to *m* in the order they appeared in the input. If there are multiple solutions, print any of them. | [
"10 4\n3 3\n3 1\n4 1\n9 2\n",
"1 1\n1 1\n"
] | [
"2\n2 3 ",
"0\n"
] | none | [] | 122 | 0 | 0 | 124,293 |
|
643 | Bearish Fanpages | [] | null | null | There is a social website with *n* fanpages, numbered 1 through *n*. There are also *n* companies, and the *i*-th company owns the *i*-th fanpage.
Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow.
The website doesnβt allow a situation where *i* follows *j* and at the same time *j* follows *i*. Also, a fanpage can't follow itself.
Letβs say that fanpage *i* follows some other fanpage *j*0. Also, letβs say that *i* is followed by *k* other fanpages *j*1,<=*j*2,<=...,<=*j**k*. Then, when people visit fanpage *i* they see ads from *k*<=+<=2 distinct companies: *i*,<=*j*0,<=*j*1,<=...,<=*j**k*. Exactly *t**i* people subscribe (like) the *i*-th fanpage, and each of them will click exactly one add. For each of *k*<=+<=1 companies *j*0,<=*j*1,<=...,<=*j**k*, exactly people will click their ad. Remaining people will click an ad from company *i* (the owner of the fanpage).
The total income of the company is equal to the number of people who click ads from this copmany.
Limak and Radewoosh ask you for help. Initially, fanpage *i* follows fanpage *f**i*. Your task is to handle *q* queries of three types:
- 1 i jΒ β fanpage *i* follows fanpage *j* from now. It's guaranteed that *i* didn't follow *j* just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). - 2 iΒ β print the total income of the *i*-th company. - 3Β β print two integers: the smallest income of one company and the biggest income of one company. | The first line of the input contains two integers *n* and *q* (3<=β€<=*n*<=β€<=100<=000, 1<=β€<=*q*<=β€<=100<=000)Β β the number of fanpages and the number of queries, respectively.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=1012) where *t**i* denotes the number of people subscribing the *i*-th fanpage.
The third line contains *n* integers *f*1,<=*f*2,<=...,<=*f**n* (1<=β€<=*f**i*<=β€<=*n*). Initially, fanpage *i* follows fanpage *f**i*.
Then, *q* lines follow. The *i*-th of them describes the *i*-th query. The first number in the line is an integer *type**i* (1<=β€<=*type**i*<=β€<=3)Β β the type of the query.
There will be at most 50<=000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty).
It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. | For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. | [
"5 12\n10 20 30 40 50\n2 3 4 5 2\n2 1\n2 2\n2 3\n2 4\n2 5\n1 4 2\n2 1\n2 2\n2 3\n2 4\n2 5\n3\n"
] | [
"10\n36\n28\n40\n36\n9\n57\n27\n28\n29\n9 57\n"
] | In the sample test, there are 5 fanpages. The *i*-th of them has *i*Β·10 subscribers.
On drawings, numbers of subscribers are written in circles. An arrow from *A* to *B* means that *A* follows *B*.
The left drawing shows the initial situation. The first company gets income <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/aa287c04033fd5717b34060017a16ed1930b6a77.png" style="max-width: 100.0%;max-height: 100.0%;"/> from its own fanpage, and gets income <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/545b52a4f4acc0a81e4c81f01a57750f8d8a5045.png" style="max-width: 100.0%;max-height: 100.0%;"/> from the 2-nd fanpage. So, the total income is 5β+β5β=β10. After the first query ("2 1") you should print 10.
The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/08e222f472ebd19466616b7774c41626722a1995.png" style="max-width: 100.0%;max-height: 100.0%;"/> from the 2-nd fanpage. So, the total income is 5β+β4β=β9 now. | [] | 46 | 0 | 0 | 124,334 |
|
0 | none | [
"none"
] | null | null | You are given an undirected graph that consists of *n* vertices and *m* edges. Initially, each edge is colored either red or blue. Each turn a player picks a single vertex and switches the color of all edges incident to it. That is, all red edges with an endpoint in this vertex change the color to blue, while all blue edges with an endpoint in this vertex change the color to red.
Find the minimum possible number of moves required to make the colors of all edges equal. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100<=000)Β β the number of vertices and edges, respectively.
The following *m* lines provide the description of the edges, as the *i*-th of them contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*)Β β the indices of the vertices connected by the *i*-th edge, and a character *c**i* () providing the initial color of this edge. If *c**i* equals 'R', then this edge is initially colored red. Otherwise, *c**i* is equal to 'B' and this edge is initially colored blue. It's guaranteed that there are no self-loops and multiple edges. | If there is no way to make the colors of all edges equal output <=-<=1 in the only line of the output. Otherwise first output *k*Β β the minimum number of moves required to achieve the goal, then output *k* integers *a*1,<=*a*2,<=...,<=*a**k*, where *a**i* is equal to the index of the vertex that should be used at the *i*-th move.
If there are multiple optimal sequences of moves, output any of them. | [
"3 3\n1 2 B\n3 1 R\n3 2 B\n",
"6 5\n1 3 R\n2 3 R\n3 4 B\n4 5 R\n4 6 R\n",
"4 5\n1 2 R\n1 3 R\n2 3 B\n3 4 B\n1 4 B\n"
] | [
"1\n2 \n",
"2\n3 4 \n",
"-1\n"
] | none | [] | 46 | 0 | 0 | 124,367 |
|
0 | none | [
"none"
] | null | null | ΠΠ΄ΠΎΠ»Ρ Π΄ΠΎΡΠΎΠ³ΠΈ ΡΡΠΎΡΡ *n* ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ². ΠΠΎΡΠΎΠ³Π° ΠΏΡΠ΅Π΄ΡΡΠ°Π²Π»ΡΠ΅Ρ ΡΠΎΠ±ΠΎΠΉ ΠΏΡΡΠΌΡΡ, ΡΠ°Π·ΠΌΠ΅ΡΠ°ΠΌΠΈ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ² ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡΠ΅Π½Π΅Π±ΡΠ΅ΡΡ, ΡΡΠΈΡΠ°Ρ ΠΈΡ
ΡΠΎΡΠΊΠ°ΠΌΠΈ.
ΠΠΎΠ΄ΠΈΡΠ΅Π»Ρ Π°Π²ΡΠΎΠ±ΡΡΠ° ΠΠ°ΡΠΈΠ»ΠΈΠΉ, Π±Π»Π°Π³ΠΎΠ΄Π°ΡΡ ΠΌΠΎΠ±ΠΈΠ»ΡΠ½ΠΎΠΌΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ, Π·Π½Π°Π΅Ρ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠ° ΡΠΎΡΠΊΡ *x**i*, Π² ΠΊΠΎΡΠΎΡΠΎΠΉ ΡΠΎΡ ΡΡΠΎΠΈΡ. ΠΡΠΎΠΌΠ΅ ΡΠΎΠ³ΠΎ, ΠΎΠ½ Π·Π½Π°Π΅Ρ ΡΠ°ΡΡΡΠΎΡΠ½ΠΈΠ΅ *d**i*, ΠΊΠΎΡΠΎΡΠΎΠ΅ *i*-ΠΉ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊ Ρ
ΠΎΡΠ΅Ρ ΠΏΡΠΎΠ΅Ρ
Π°ΡΡ Π½Π° Π°Π²ΡΠΎΠ±ΡΡΠ΅. Π’Π°ΠΊΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ, *i*-ΠΉ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊ ΠΏΠ»Π°Π½ΠΈΡΡΠ΅Ρ Π²ΡΠΉΡΠΈ ΠΈΠ· Π°Π²ΡΠΎΠ±ΡΡΠ° Π² ΡΠΎΡΠΊΠ΅ *x**i*<=+<=*d**i*. Π’Π΅ΠΏΠ΅ΡΡ ΠΠ°ΡΠΈΠ»ΠΈΠΉ Ρ
ΠΎΡΠ΅Ρ ΡΠ΅ΡΠΈΡΡ, ΠΊΠΎΠ³ΠΎ ΠΈΠ· ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ² ΠΎΠ½ ΠΏΠΎΠ΄Π²Π΅Π·ΡΡ, Π° ΠΊΠΎΠ³ΠΎ ΠΎΡΡΠ°Π²ΠΈΡ ΠΏΡΠ»ΠΈΡΡΡΡ Ρ Π΄ΠΎΡΠΎΠ³ΠΈ.
ΠΠ°ΡΠΈΠ»ΠΈΠΉ ΡΠ΅ΡΠΈΠ», ΡΡΠΎ ΡΠ΅Π³ΠΎΠ΄Π½Ρ ΠΎΠ½ Π΄ΠΎΠ»ΠΆΠ΅Π½ Ρ
ΠΎΡΠΎΡΠΎ Π·Π°ΡΠ°Π±ΠΎΡΠ°ΡΡ, ΠΏΠΎΡΡΠΎΠΌΡ ΡΠ΅ΡΠΈΠ» ΠΏΠ΅ΡΠ΅Π²Π΅Π·ΡΠΈ Π² ΡΠΎΡΠ½ΠΎΡΡΠΈ *a* ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ². Π Π°Π²ΡΠΎΠΏΠ°ΡΠΊΠ΅ Π΅ΡΡΡ Π°Π²ΡΠΎΠ±ΡΡΡ Π»ΡΠ±ΡΡ
Π²ΠΈΠ΄ΠΎΠ². Π§Π΅ΠΌ Π±ΠΎΠ»ΡΡΠ΅ ΠΌΠ΅ΡΡ Π² Π°Π²ΡΠΎΠ±ΡΡΠ΅, ΡΠ΅ΠΌ Π΄ΠΎΡΠΎΠΆΠ΅ ΡΡΠΎΠΈΡ Π΅Π³ΠΎ Π°ΡΠ΅Π½Π΄Π°.
ΠΠΎΠΌΠΎΠ³ΠΈΡΠ΅ ΠΠ°ΡΠΈΠ»ΠΈΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΡΠΊΠΈΡ
ΠΌΠ΅ΡΡ Π² Π°Π²ΡΠΎΠ±ΡΡΠ΅, ΠΊΠΎΡΠΎΡΡΡ
Π±ΡΠ΄Π΅Ρ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ Π΄Π»Ρ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ·ΠΊΠΈ ΡΠΎΠ²Π½ΠΎ *a* ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ². ΠΠΈ Π² ΠΊΠ°ΠΊΠΎΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ Π²ΡΠ΅ΠΌΠ΅Π½ΠΈ Π² Π°Π²ΡΠΎΠ±ΡΡΠ΅ Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ² Π±ΠΎΠ»ΡΡΠ΅, ΡΠ΅ΠΌ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΡΠΊΠΈΡ
ΠΌΠ΅ΡΡ Π² Π°Π²ΡΠΎΠ±ΡΡΠ΅. ΠΠ°ΡΠΈΠ»ΠΈΠΉ ΡΠ°ΠΌ ΠΌΠΎΠΆΠ΅Ρ ΡΠ΅ΡΠΈΡΡ ΠΊΠ°ΠΊΠΎΠ΅ ΠΊΠΎΠ½ΠΊΡΠ΅ΡΠ½ΠΎ ΠΏΠΎΠ΄ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ ΠΈΠ· *a* ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ² ΠΎΠ½ ΠΏΠ΅ΡΠ΅Π²Π΅Π·ΡΡ Π½Π° Π°Π²ΡΠΎΠ±ΡΡΠ΅.
Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ Π°Π²ΡΠΎΠ±ΡΡ Π²ΡΠ΅Π³Π΄Π° Π΅Π΄Π΅Ρ ΡΠ»Π΅Π²Π° Π½Π°ΠΏΡΠ°Π²ΠΎ (ΠΎΡ ΠΌΠ΅Π½ΡΡΠΈΡ
ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°Ρ ΠΊ Π±ΠΎΠ»ΡΡΠΈΠΌ) ΠΈ Π½Π°ΡΠΈΠ½Π°Π΅Ρ ΡΠ²ΠΎΠΉ ΠΏΡΡΡ Π»Π΅Π²Π΅Π΅ ΡΠ°ΠΌΠΎΠ³ΠΎ Π»Π΅Π²ΠΎΡΡΠΎΡΡΠ΅Π³ΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠ°. ΠΡΠ»ΠΈ Π² ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅ ΠΊΠ°ΠΊΠΎΠΉ-ΡΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊ Π΄ΠΎΠ»ΠΆΠ΅Π½ Π²ΡΠΉΡΠΈ ΠΈΠ· Π°Π²ΡΠΎΠ±ΡΡΠ°, Π° Π΄ΡΡΠ³ΠΎΠΉ Π²ΠΎΠΉΡΠΈ, ΡΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ ΡΠ½Π°ΡΠ°Π»Π° ΠΏΡΠΎΠΈΠ·ΠΎΠΉΠ΄Π΅Ρ Π²ΡΡ
ΠΎΠ΄ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠ° ΠΈΠ· Π°Π²ΡΠΎΠ±ΡΡΠ°, Π° Π·Π°ΡΠ΅ΠΌ Π΄ΡΡΠ³ΠΎΠΉ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊ ΡΠΌΠΎΠΆΠ΅Ρ Π·Π°ΠΉΡΠΈ Π² Π°Π²ΡΠΎΠ±ΡΡ. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΠ΅Ρ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΠΏΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΡΡ
ΡΠΈΡΠ»Π° *n* ΠΈ *a* (1<=β€<=*a*<=β€<=*n*<=β€<=200<=000)Β β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ², ΡΡΠΎΡΡΠΈΡ
Π²Π΄ΠΎΠ»Ρ Π΄ΠΎΡΠΎΠ³ΠΈ, ΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ², ΠΊΠΎΡΠΎΡΡΡ
ΠΠ°ΡΠΈΠ»ΠΈΠΉ Ρ
ΠΎΡΠ΅Ρ ΠΏΠΎΠ΄Π²Π΅Π·ΡΠΈ.
Π ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
*n* ΡΡΡΠΎΠΊ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡΡΡ ΠΏΠΎ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° *x**i* ΠΈ *d**i* (1<=β€<=*x**i*,<=*d**i*<=β€<=109)Β β ΠΊΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΠ°, Π² ΠΊΠΎΡΠΎΡΠΎΠΉ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡ *i*-ΠΉ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊ, Π° ΡΠ°ΠΊΠΆΠ΅ ΡΠ°ΡΡΡΠΎΡΠ½ΠΈΠ΅, Π½Π° ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΎΠ½ Ρ
ΠΎΡΠ΅Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΡΠΈΡΡΡΡ Π½Π° Π°Π²ΡΠΎΠ±ΡΡΠ΅. ΠΠΎΠΎΡΠ΄ΠΈΠ½Π°ΡΡ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ² Π·Π°Π΄Π°Π½Ρ Π² ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. Π ΠΎΠ΄Π½ΠΎΠΉ ΡΠΎΡΠΊΠ΅ ΠΌΠΎΠ³ΡΡ Π½Π°Ρ
ΠΎΠ΄ΠΈΡΡΡΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ². | Π‘Π½Π°ΡΠ°Π»Π° Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎΒ β ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠ°ΡΡΠ°ΠΆΠΈΡΡΠΊΠΈΡ
ΠΌΠ΅ΡΡ Π² Π°Π²ΡΠΎΠ±ΡΡΠ΅, ΠΊΠΎΡΠΎΡΡΡ
Π±ΡΠ΄Π΅Ρ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ Π΄Π»Ρ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ·ΠΊΠΈ Ρ
ΠΎΡΡ Π±Ρ *a* ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ².
ΠΠ°ΡΠ΅ΠΌ Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ *a* ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π»Β β Π½ΠΎΠΌΠ΅ΡΠ° ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ², ΠΊΠΎΡΠΎΡΡΡ
ΠΏΠΎΠ΄Π²Π΅Π·ΡΡ ΠΠ°ΡΠΈΠ»ΠΈΠΉ. ΠΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΈ Π½ΡΠΌΠ΅ΡΡΡΡΡΡ, Π½Π°ΡΠΈΠ½Π°Ρ Ρ Π΅Π΄ΠΈΠ½ΠΈΡΡ, Π² ΡΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅, Π² ΠΊΠΎΡΠΎΡΠΎΠΌ Π·Π°Π΄Π°Π½Ρ Π²ΠΎ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
. ΠΠΎΠΌΠ΅ΡΠ° ΠΌΠΎΠΆΠ½ΠΎ Π²ΡΠ²ΠΎΠ΄ΠΈΡΡ Π² ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. ΠΡΠ»ΠΈ ΠΎΡΠ²Π΅ΡΠΎΠ² Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ, ΡΠ°Π·ΡΠ΅ΡΠ°Π΅ΡΡΡ Π²ΡΠ²Π΅ΡΡΠΈ Π»ΡΠ±ΠΎΠΉ ΠΈΠ· Π½ΠΈΡ
. | [
"3 2\n8 9\n3 5\n1 3\n",
"5 4\n20 40\n10 10\n15 5\n5 15\n20 30\n"
] | [
"1\n1 3 \n",
"2\n4 2 5 1\n"
] | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΠ΅ΡΡΠΎΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ ΠΎΠ΄Π½ΠΎΠΌΠ΅ΡΡΠ½ΠΎΠ³ΠΎ Π°Π²ΡΠΎΠ±ΡΡΠ°. Π ΠΏΡΠΈΠΌΠ΅ΡΡ, ΠΠ°ΡΠΈΠ»ΠΈΠΉ ΠΌΠΎΠΆΠ΅Ρ ΠΏΠΎΠ΄Π²Π΅Π·ΡΠΈ ΡΡΠ΅ΡΡΠ΅Π³ΠΎ ΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ², Π»ΠΈΠ±ΠΎ Π²ΡΠΎΡΠΎΠ³ΠΎ ΠΈ ΠΏΠ΅ΡΠ²ΠΎΠ³ΠΎ ΠΏΡΡΠ΅ΡΠ΅ΡΡΠ²Π΅Π½Π½ΠΈΠΊΠΎΠ². | [] | 0 | 0 | -1 | 124,606 |
|
0 | none | [
"none"
] | null | null | When the river brought Gerda to the house of the Old Lady who Knew Magic, this lady decided to make Gerda her daughter. She wants Gerda to forget about Kay, so she puts all the roses from the garden underground.
Mole, who lives in this garden, now can watch the roses without going up to the surface. Typical mole is blind, but this mole was granted as special vision by the Old Lady. He can watch any underground objects on any distance, even through the obstacles and other objects. However, the quality of the picture depends on the Manhattan distance to object being observed.
Mole wants to find an optimal point to watch roses, that is such point with integer coordinates that the maximum Manhattan distance to the rose is minimum possible.
As usual, he asks you to help.
Manhattan distance between points (*x*1,<=Β *y*1,<=Β *z*1) and (*x*2,<=Β *y*2,<=Β *z*2) is defined as |*x*1<=-<=*x*2|<=+<=|*y*1<=-<=*y*2|<=+<=|*z*1<=-<=*z*2|. | The first line of the input contains an integer *t* *t* (1<=β€<=*t*<=β€<=100<=000)Β β the number of test cases. Then follow exactly *t* blocks, each containing the description of exactly one test.
The first line of each block contains an integer *n**i* (1<=β€<=*n**i*<=β€<=100<=000)Β β the number of roses in the test. Then follow *n**i* lines, containing three integers eachΒ β the coordinates of the corresponding rose. Note that two or more roses may share the same position.
It's guaranteed that the sum of all *n**i* doesn't exceed 100<=000 and all coordinates are not greater than 1018 by their absolute value. | For each of *t* test cases print three integersΒ β the coordinates of the optimal point to watch roses. If there are many optimal answers, print any of them.
The coordinates of the optimal point may coincide with the coordinates of any rose. | [
"1\n5\n0 0 4\n0 0 -4\n0 4 0\n4 0 0\n1 1 1\n",
"2\n1\n3 5 9\n2\n3 5 9\n3 5 9\n"
] | [
"0 0 0\n",
"3 5 9\n3 5 9\n"
] | In the first sample, the maximum Manhattan distance from the point to the rose is equal to 4.
In the second sample, the maximum possible distance is 0. Note that the positions of the roses may coincide with each other and with the position of the optimal point. | [] | 30 | 0 | 0 | 124,889 |
|
513 | Subarray Cuts | [
"dp"
] | null | null | You are given an array of length *n* and a number *k*. Let's pick *k* non-overlapping non-empty subarrays of the initial array. Let *s**i* be the sum of the *i*-th subarray in order from left to right. Compute the maximum value of the following expression:
Here subarray is a contiguous part of an array. | The first line of input contains two integers *n* and *k*. The second line contains *n* integers β the elements of the array. The absolute values of elements do not exceed 104.
The problem consists of two subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows.
- In subproblem E1 (9 points), constraints 2<=β€<=*n*<=β€<=400, 2<=β€<=*k*<=β€<=*min*(*n*,<=50) will hold. - In subproblem E2 (12 points), constraints 2<=β€<=*n*<=β€<=30000, 2<=β€<=*k*<=β€<=*min*(*n*,<=200) will hold. | Output a single integer β the maximum possible value. | [
"5 3\n5 2 4 3 1\n",
"4 2\n7 4 3 7\n"
] | [
"12\n",
"8\n"
] | Consider the first sample test. The optimal solution is obtained if the first subarray contains the first element only, the second subarray spans the next three elements and the last subarray contains the last element only. The sums of these subarrays are 5, 9 and 1, correspondingly.
Consider the second sample test. In the optimal solution, the first subarray consists of the first two elements and the second subarray consists of the third element only. Note that the last element does not belong to any subarray in this solution. | [
{
"input": "5 3\n5 2 4 3 1",
"output": "12"
},
{
"input": "4 2\n7 4 3 7",
"output": "8"
},
{
"input": "9 6\n9 4 10 1 6 3 3 8 4",
"output": "55"
},
{
"input": "8 6\n2 5 3 4 2 10 9 3",
"output": "45"
},
{
"input": "9 2\n7 10 7 2 5 7 10 1 3",
"output": "47"
},
{
"input": "9 3\n3 9 4 10 6 7 7 2 10",
"output": "81"
},
{
"input": "8 4\n8 2 6 9 5 4 1 4",
"output": "51"
},
{
"input": "5 3\n3 3 1 9 8",
"output": "21"
},
{
"input": "6 4\n5 10 2 4 8 9",
"output": "32"
},
{
"input": "5 2\n9 7 6 5 7",
"output": "20"
},
{
"input": "5 4\n8 7 5 9 1",
"output": "22"
},
{
"input": "4 2\n7 3 4 7",
"output": "8"
},
{
"input": "8 5\n-630 2558 1163 2710 -1900 2192 7403 3484",
"output": "32998"
},
{
"input": "44 43\n-1130 -7388 9958 6381 2702 -1060 -8039 5345 -9269 -3034 -4405 -9862 -6168 -3043 1895 2321 -5991 5603 930 7299 7927 -9735 5116 7386 -3388 -1773 8806 2225 5171 1101 -1037 -7467 3593 343 2626 5769 -9959 2782 -2412 4431 -7497 -6156 4320 8096",
"output": "308698"
},
{
"input": "27 11\n-4254 3424 2945 727 -2975 -9294 7288 402 -847 8762 -9663 5097 7798 -7861 -9663 -1468 5360 9715 -2662 -5058 8183 -455 7857 -3442 -9308 -7636 -5262",
"output": "259702"
},
{
"input": "24 10\n-7932 4510 1624 -8602 -7916 5217 -2544 -9341 2858 -3382 5325 2210 -1688 -6225 6498 -1981 6403 -4954 -5050 -8673 -8054 -9216 4550 1339",
"output": "219007"
},
{
"input": "33 15\n-609 -2560 -311 -7812 -8492 -447 2865 1143 2253 -2710 -5293 -6690 -4853 3591 -9509 678 -8507 -6298 7374 7368 9663 -1634 -2175 4555 -4306 -7987 -2299 -68 -2694 3113 -1280 -6118 -4893",
"output": "265284"
},
{
"input": "50 7\n4761 -2433 5679 490 -4090 -7654 -4710 6229 -9337 2407 -7057 3837 -5209 -6533 665 -3366 -6460 1319 -5908 1076 -5051 -424 5063 -8802 -875 5383 8169 -805 9674 -3004 1408 4651 8634 7999 -4456 -8092 -219 8552 9064 -747 -4972 -3593 -9896 3975 1969 7854 -3891 -633 -9416 3531",
"output": "315455"
},
{
"input": "50 27\n-9505 -8834 -4682 7425 9904 -2710 -8847 4798 5110 -3014 2038 -2588 -8146 5629 9326 -9079 9649 -4676 1588 -5627 3011 -9726 -9101 -6474 -9674 297 -3465 -8222 -9857 -1118 1598 9727 -5554 2340 -9442 245 -4039 3793 4782 -5823 -1080 1975 6642 1862 -9044 -5623 -3292 8121 3302 7224",
"output": "514448"
},
{
"input": "50 21\n-6832 -6270 -195 8855 8678 8763 -8149 -710 -582 -8830 -417 3582 -3126 -2845 9839 5659 908 3309 -7641 -4408 -9574 9557 4609 8976 -4898 -9348 -6060 -2164 -5666 -1239 -7695 342 5832 -3636 7452 -5803 9774 2224 112 -8287 -7978 1082 -1361 7833 -5095 -1433 6322 8901 -4716 -547",
"output": "497668"
},
{
"input": "50 50\n-5947 -6208 -6087 -1360 5740 -3689 -5147 -2317 -6245 -789 -2619 -1479 9010 -6066 -5877 -7718 2441 -9103 8254 -7071 -2342 -9214 4236 8788 2180 1857 -3207 -9804 -9474 -6035 -8743 4033 3406 -8887 7610 5115 507 -8109 -5649 6485 1386 8188 -6163 6024 -6906 669 8510 -5656 6841 -5454",
"output": "353181"
},
{
"input": "50 50\n-287 -6668 9808 -6919 877 -5629 -4030 -2797 9318 2880 9458 -3706 8832 1885 -5297 5707 -5205 -7436 -8572 -9044 -2693 5524 -8862 9745 -1521 -6167 643 765 -5271 -1174 4533 -7350 -2937 -4770 1412 1343 7320 -9195 3850 7880 -3093 -1517 -2640 5560 5120 6680 -9421 -8883 1265 -8434",
"output": "357985"
}
] | 93 | 307,200 | -1 | 125,136 |
|
509 | Sums of Digits | [
"dp",
"greedy",
"implementation"
] | null | null | Vasya had a strictly increasing sequence of positive integers *a*1, ..., *a**n*. Vasya used it to build a new sequence *b*1, ..., *b**n*, where *b**i* is the sum of digits of *a**i*'s decimal representation. Then sequence *a**i* got lost and all that remained is sequence *b**i*.
Vasya wonders what the numbers *a**i* could be like. Of all the possible options he likes the one sequence with the minimum possible last number *a**n*. Help Vasya restore the initial sequence.
It is guaranteed that such a sequence always exists. | The first line contains a single integer number *n* (1<=β€<=*n*<=β€<=300).
Next *n* lines contain integer numbers *b*1, ..., *b**n* Β β the required sums of digits. All *b**i* belong to the range 1<=β€<=*b**i*<=β€<=300. | Print *n* integer numbers, one per lineΒ β the correct option for numbers *a**i*, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the *i*-th number should be equal to *b**i*.
If there are multiple sequences with least possible number *a**n*, print any of them. Print the numbers without leading zeroes. | [
"3\n1\n2\n3\n",
"3\n3\n2\n1\n"
] | [
"1\n2\n3\n",
"3\n11\n100\n"
] | none | [
{
"input": "3\n1\n2\n3",
"output": "1\n2\n3"
},
{
"input": "3\n3\n2\n1",
"output": "3\n11\n100"
},
{
"input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1",
"output": "1\n2\n3\n4\n5\n6\n7\n8\n9\n10"
},
{
"input": "10\n8\n8\n5\n1\n2\n7\n3\n8\n9\n4",
"output": "8\n17\n23\n100\n101\n106\n111\n116\n117\n121"
},
{
"input": "10\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1",
"output": "1\n10\n100\n1000\n10000\n100000\n1000000\n10000000\n100000000\n1000000000"
},
{
"input": "100\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n1",
"output": "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40\n41\n42\n43\n44\n45\n46\n47\n48\n49\n50\n51\n52\n53\n54\n55\n56\n57\n58\n59\n60\n61\n62\n63\n64\n65\n66\n67\n68\n69\n70\n71\n72\n73\n74\n75\n76\n77\n78\n79\n80\n81\n82\n83\n84\n85\n86\n87\n88\n89\n90\n91\n92\n93\n94\n95\n96\n97\n98\n99\n100"
},
{
"input": "100\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18\n18",
"output": "99\n189\n198\n279\n288\n297\n369\n378\n387\n396\n459\n468\n477\n486\n495\n549\n558\n567\n576\n585\n594\n639\n648\n657\n666\n675\n684\n693\n729\n738\n747\n756\n765\n774\n783\n792\n819\n828\n837\n846\n855\n864\n873\n882\n891\n909\n918\n927\n936\n945\n954\n963\n972\n981\n990\n1089\n1098\n1179\n1188\n1197\n1269\n1278\n1287\n1296\n1359\n1368\n1377\n1386\n1395\n1449\n1458\n1467\n1476\n1485\n1494\n1539\n1548\n1557\n1566\n1575\n1584\n1593\n1629\n1638\n1647\n1656\n1665\n1674\n1683\n1692\n1719\n1728\n1737\n1746\n175..."
},
{
"input": "1\n139",
"output": "4999999999999999"
},
{
"input": "1\n6",
"output": "6"
}
] | 93 | 0 | 0 | 125,149 |
|
187 | Weak Memory | [
"dfs and similar",
"dsu"
] | null | null | Zart PMP is qualified for ICPC World Finals in Harbin, China. After team excursion to Sun Island Park for snow sculpture art exposition, PMP should get back to buses before they leave. But the park is really big and he does not know how to find them.
The park has *n* intersections numbered 1 through *n*. There are *m* bidirectional roads that connect some pairs of these intersections. At *k* intersections, ICPC volunteers are helping the teams and showing them the way to their destinations. Locations of volunteers are fixed and distinct.
When PMP asks a volunteer the way to bus station, he/she can tell him the whole path. But the park is fully covered with ice and snow and everywhere looks almost the same. So PMP can only memorize at most *q* intersections after each question (excluding the intersection they are currently standing). He always tells volunteers about his weak memory and if there is no direct path of length (in number of roads) at most *q* that leads to bus station, the volunteer will guide PMP to another volunteer (who is at most *q* intersections away, of course). ICPC volunteers know the area very well and always tell PMP the best way. So if there exists a way to bus stations, PMP will definitely find it.
PMP's initial location is intersection *s* and the buses are at intersection *t*. There will always be a volunteer at intersection *s*. Your job is to find out the minimum *q* which guarantees that PMP can find the buses. | The first line contains three space-separated integers *n*,<=*m*,<=*k* (2<=β€<=*n*<=β€<=105,<=0<=β€<=*m*<=β€<=2Β·105,<=1<=β€<=*k*<=β€<=*n*) β the number of intersections, roads and volunteers, respectively. Next line contains *k* distinct space-separated integers between 1 and *n* inclusive β the numbers of cities where volunteers are located.
Next *m* lines describe the roads. The *i*-th of these lines contains two space-separated integers *u**i*,<=*v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=*u**i*<=β <=*v**i*) β two intersections that *i*-th road connects. There will be at most one road between any two intersections.
Last line of input contains two space-separated integers *s*,<=*t* (1<=β€<=*s*,<=*t*<=β€<=*n*,<=*s*<=β <=*t*) β the initial location of PMP and the location of the buses. It might not always be possible to reach *t* from *s*.
It is guaranteed that there is always a volunteer at intersection *s*. | Print on the only line the answer to the problem β the minimum value of *q* which guarantees that PMP can find the buses. If PMP cannot reach the buses at all, output -1 instead. | [
"6 6 3\n1 3 6\n1 2\n2 3\n4 2\n5 6\n4 5\n3 4\n1 6\n",
"6 5 3\n1 5 6\n1 2\n2 3\n3 4\n4 5\n6 3\n1 5\n"
] | [
"3\n",
"3\n"
] | The first sample is illustrated below. Blue intersections are where volunteers are located. If PMP goes in the path of dashed line, it can reach the buses with *q*β=β3:
In the second sample, PMP uses intersection 6 as an intermediate intersection, thus the answer is 3. | [] | 62 | 0 | 0 | 125,964 |
|
254 | Rats | [
"brute force",
"dfs and similar",
"graphs",
"implementation",
"shortest paths"
] | null | null | Rats have bred to hundreds and hundreds in the basement of the store, owned by Vasily Petrovich. Vasily Petrovich may have not noticed their presence, but they got into the habit of sneaking into the warehouse and stealing food from there. Vasily Petrovich cannot put up with it anymore, he has to destroy the rats in the basement. Since mousetraps are outdated and do not help, and rat poison can poison inattentive people as well as rats, he chose a radical way: to blow up two grenades in the basement (he does not have more).
In this problem, we will present the shop basement as a rectangular table of *n*<=Γ<=*m* cells. Some of the cells are occupied by walls, and the rest of them are empty. Vasily has been watching the rats and he found out that at a certain time they go to sleep, and all the time they sleep in the same places. He wants to blow up a grenade when this convenient time comes. On the plan of his basement, he marked cells with sleeping rats in them. Naturally, these cells are not occupied by walls.
Grenades can only blow up in a cell that is not occupied by a wall. The blast wave from a grenade distributes as follows. We assume that the grenade blast occurs at time 0. During this initial time only the cell where the grenade blew up gets 'clear'. If at time *t* some cell is clear, then at time *t*<=+<=1 those side-neighbouring cells which are not occupied by the walls get clear too (some of them could have been cleared before). The blast wave distributes for exactly *d* seconds, then it dies immediately.
Vasily Petrovich wonders, whether he can choose two cells to blast the grenades so as to clear all cells with sleeping rats. Write the program that finds it out. | The first line contains three integers *n*, *m* and *d*, separated by single spaces (4<=β€<=*n*,<=*m*<=β€<=1000,<=1<=β€<=*d*<=β€<=8). Next *n* lines contain the table that represents the basement plan. Each row of the table consists of *m* characters. Character "X" means that the corresponding cell is occupied by the wall, character "." represents a empty cell, character "R" represents a empty cell with sleeping rats.
It is guaranteed that the first and the last row, as well as the first and the last column consist of characters "X". The plan has at least two empty cells. There is at least one cell with sleeping rats. | If it is impossible to blow up all cells with sleeping rats, print a single integer -1. Otherwise, print four space-separated integers *r*1,<=*c*1,<=*r*2,<=*c*2, that mean that one grenade should go off in cell (*r*1,<=*c*1), and the other one β in cell (*r*2,<=*c*2).
Consider the table rows numbered from top to bottom from 1 to *n* and the table columns β from left to right from 1 to *m*. As *r*1 and *r*2 represent the row numbers, and *c*1 and *c*2 represent the column numbers in the table, they should fit the limits: 1<=β€<=*r*1,<=*r*2<=β€<=*n*,<=1<=β€<=*c*1,<=*c*2<=β€<=*m*. It is forbidden to blow a grenade twice in the same cell. The blast waves of the grenades can intersect. It is possible that one grenade blast destroys no rats, and the other one destroys all of them. | [
"4 4 1\nXXXX\nXR.X\nX.RX\nXXXX\n",
"9 14 5\nXXXXXXXXXXXXXX\nX....R...R...X\nX..R.........X\nX....RXR..R..X\nX..R...X.....X\nXR.R...X.....X\nX....XXR.....X\nX....R..R.R..X\nXXXXXXXXXXXXXX\n",
"7 7 1\nXXXXXXX\nX.R.R.X\nX.....X\nX..X..X\nX..R..X\nX....RX\nXXXXXXX\n"
] | [
"2 2 2 3\n",
"2 3 6 9\n",
"-1\n"
] | none | [
{
"input": "4 4 1\nXXXX\nXR.X\nX.RX\nXXXX",
"output": "2 2 2 3"
},
{
"input": "9 14 5\nXXXXXXXXXXXXXX\nX....R...R...X\nX..R.........X\nX....RXR..R..X\nX..R...X.....X\nXR.R...X.....X\nX....XXR.....X\nX....R..R.R..X\nXXXXXXXXXXXXXX",
"output": "2 3 6 9"
},
{
"input": "7 7 1\nXXXXXXX\nX.R.R.X\nX.....X\nX..X..X\nX..R..X\nX....RX\nXXXXXXX",
"output": "-1"
},
{
"input": "7 7 2\nXXXXXXX\nX.....X\nXRXR..X\nX.....X\nXR...RX\nXX....X\nXXXXXXX",
"output": "3 2 5 4"
},
{
"input": "7 7 3\nXXXXXXX\nX..RR.X\nX.R...X\nX.R...X\nX.R..RX\nX.....X\nXXXXXXX",
"output": "2 4 5 3"
},
{
"input": "7 7 4\nXXXXXXX\nX....RX\nXR.R..X\nX.....X\nX.R...X\nX..R..X\nXXXXXXX",
"output": "2 6 4 2"
},
{
"input": "7 7 5\nXXXXXXX\nX.....X\nX...R.X\nX.....X\nXR.R..X\nX.....X\nXXXXXXX",
"output": "3 5 2 2"
},
{
"input": "7 13 5\nXXXXXXXXXXXXX\nX.R..X..R...X\nX..X......X.X\nX...R..R.X.XX\nXR...RR..R..X\nX..R....R...X\nXXXXXXXXXXXXX",
"output": "2 3 2 8"
},
{
"input": "13 7 5\nXXXXXXX\nX.X..RX\nX.R..RX\nXR....X\nX...R.X\nX....XX\nX.R..RX\nX....RX\nXR.R.XX\nXR.XR.X\nXR....X\nX.....X\nXXXXXXX",
"output": "2 6 8 2"
},
{
"input": "13 15 5\nXXXXXXXXXXXXXXX\nX.............X\nX.R........R..X\nXR.....X...RRRX\nX....R...RRX..X\nX.R..R......R.X\nX...R.....R...X\nX..........XRRX\nX.......RR....X\nX......R......X\nX....X........X\nXX.XR.R....R..X\nXXXXXXXXXXXXXXX",
"output": "-1"
},
{
"input": "4 15 5\nXXXXXXXXXXXXXXX\nX......X......X\nX.........R.R.X\nXXXXXXXXXXXXXXX",
"output": "3 11 2 2"
},
{
"input": "19 7 5\nXXXXXXX\nX.R.X.X\nX...R.X\nX...X.X\nX...RRX\nX.....X\nX.....X\nX.....X\nX.....X\nX..RRXX\nX.....X\nX.....X\nX...R.X\nX.....X\nX...R.X\nX..R..X\nX.....X\nX.....X\nXXXXXXX",
"output": "3 3 11 4"
},
{
"input": "10 9 5\nXXXXXXXXX\nXRRRRRRRX\nXRRRRRRRX\nXRRRRRRRX\nXRRRRRRRX\nXRRRRRRRX\nXRRRRRRRX\nXRRRRRRRX\nXRRRRRRRX\nXXXXXXXXX",
"output": "3 4 7 5"
},
{
"input": "10 5 2\nXXXXX\nX..XX\nXR..X\nXRX.X\nX...X\nX.RRX\nXR.RX\nX.R.X\nX...X\nXXXXX",
"output": "3 2 6 3"
},
{
"input": "9 13 1\nXXXXXXXXXXXXX\nXX..X.X..XX.X\nXX..X....X.XX\nX..XRX.X.XXXX\nX...R..X....X\nX...X..X....X\nXX...RX.....X\nX....RX.X..XX\nXXXXXXXXXXXXX",
"output": "4 5 7 6"
},
{
"input": "20 20 4\nXXXXXXXXXXXXXXXXXXXX\nX........X...X..X..X\nX..X.XXXXXX....XX..X\nX.....X.....R.X.X.XX\nX..XX.X..X.X...XXX.X\nXX.....XX.XX....X..X\nX...........X...X.XX\nX...X.......X.XX.XXX\nXX...XXX.X...XX.X..X\nXX.X.......X..X.XX.X\nX...X......X.X.X...X\nX..X.XX..XXX.X.X.X.X\nX...XX.X.XXX.......X\nX.X..X....XXX.X...XX\nX.X...X.......X...XX\nX..X.X......XXX....X\nXX....X.X......X...X\nXX...............X.X\nX....X...X........XX\nXXXXXXXXXXXXXXXXXXXX",
"output": "4 13 2 2"
}
] | 46 | 0 | 0 | 126,017 |
|
750 | New Year and Binary Tree Paths | [
"bitmasks",
"brute force",
"combinatorics",
"dp"
] | null | null | The New Year tree is an infinite perfect binary tree rooted in the node 1. Each node *v* has two children: nodes indexed (2Β·*v*) and (2Β·*v*<=+<=1).
Polar bears love decorating the New Year tree and Limak is no exception. As he is only a little bear, he was told to decorate only one simple path between some pair of nodes. Though he was given an opportunity to pick the pair himself! Now he wants to know the number of unordered pairs of indices (*u*,<=*v*) (*u*<=β€<=*v*), such that the sum of indices of all nodes along the simple path between *u* and *v* (including endpoints) is equal to *s*. Can you help him and count this value? | The only line of the input contains a single integer *s* (1<=β€<=*s*<=β€<=1015). | Print one integer, denoting the number of unordered pairs of nodes indices defining simple paths with the sum of indices of vertices equal to *s*. | [
"10\n"
] | [
"4\n"
] | In sample test, there are 4 paths with the sum of indices equal to 10: | [] | 46 | 0 | 0 | 126,222 |
|
802 | Fake News (hard) | [
"string suffix structures"
] | null | null | Now that you have proposed a fake post for the HC2 Facebook page, Heidi wants to measure the quality of the post before actually posting it. She recently came across a (possibly fake) article about the impact of fractal structure on multimedia messages and she is now trying to measure the self-similarity of the message, which is defined as
where the sum is over all nonempty strings *p* and is the number of occurences of *p* in *s* as a substring. (Note that the sum is infinite, but it only has a finite number of nonzero summands.)
Heidi refuses to do anything else until she knows how to calculate this self-similarity. Could you please help her? (If you would like to instead convince Heidi that a finite string cannot be a fractal anyway β do not bother, we have already tried.) | The input starts with a line indicating the number of test cases *T* (1<=β€<=*T*<=β€<=10). After that, *T* test cases follow, each of which consists of one line containing a string *s* (1<=β€<=|*s*|<=β€<=100<=000) composed of lowercase letters (a-z). | Output *T* lines, every line containing one number β the answer to the corresponding test case. | [
"4\naa\nabcd\nccc\nabcc\n"
] | [
"5\n10\n14\n12\n"
] | A string *s* contains another string *p* as a substring if *p* is a contiguous subsequence of *s*. For example, ab is a substring of cab but not of acb. | [] | 30 | 0 | 0 | 126,508 |
|
216 | Spider's Web | [
"binary search",
"sortings",
"two pointers"
] | null | null | Paw the Spider is making a web. Web-making is a real art, Paw has been learning to do it his whole life. Let's consider the structure of the web.
There are *n* main threads going from the center of the web. All main threads are located in one plane and divide it into *n* equal infinite sectors. The sectors are indexed from 1 to *n* in the clockwise direction. Sectors *i* and *i*<=+<=1 are adjacent for every *i*, 1<=β€<=*i*<=<<=*n*. In addition, sectors 1 and *n* are also adjacent.
Some sectors have bridge threads. Each bridge connects the two main threads that make up this sector. The points at which the bridge is attached to the main threads will be called attachment points. Both attachment points of a bridge are at the same distance from the center of the web. At each attachment point exactly one bridge is attached. The bridges are adjacent if they are in the same sector, and there are no other bridges between them.
A cell of the web is a trapezoid, which is located in one of the sectors and is bounded by two main threads and two adjacent bridges. You can see that the sides of the cell may have the attachment points of bridges from adjacent sectors. If the number of attachment points on one side of the cell is not equal to the number of attachment points on the other side, it creates an imbalance of pulling forces on this cell and this may eventually destroy the entire web. We'll call such a cell unstable. The perfect web does not contain unstable cells.
Unstable cells are marked red in the figure. Stable cells are marked green.
Paw the Spider isn't a skillful webmaker yet, he is only learning to make perfect webs. Help Paw to determine the number of unstable cells in the web he has just spun. | The first line contains integer *n* (3<=β€<=*n*<=β€<=1000) β the number of main threads.
The *i*-th of following *n* lines describe the bridges located in the *i*-th sector: first it contains integer *k**i* (1<=β€<=*k**i*<=β€<=105) equal to the number of bridges in the given sector. Then follow *k**i* different integers *p**ij* (1<=β€<=*p**ij*<=β€<=105;Β 1<=β€<=*j*<=β€<=*k**i*). Number *p**ij* equals the distance from the attachment points of the *j*-th bridge of the *i*-th sector to the center of the web.
It is guaranteed that any two bridges between adjacent sectors are attached at a different distance from the center of the web. It is guaranteed that the total number of the bridges doesn't exceed 105. | Print a single integer β the number of unstable cells in Paw the Spider's web. | [
"7\n3 1 6 7\n4 3 5 2 9\n2 8 1\n4 3 7 6 4\n3 2 5 9\n3 6 3 8\n3 4 2 9\n"
] | [
"6"
] | none | [
{
"input": "7\n3 1 6 7\n4 3 5 2 9\n2 8 1\n4 3 7 6 4\n3 2 5 9\n3 6 3 8\n3 4 2 9",
"output": "6"
},
{
"input": "3\n1 1\n1 2\n1 3",
"output": "0"
},
{
"input": "3\n2 1 2\n2 3 4\n2 5 6",
"output": "0"
},
{
"input": "5\n3 2 4 10\n2 1 6\n2 8 7\n3 2 4 10\n2 1 6",
"output": "2"
},
{
"input": "3\n5 2 7 6 9 8\n4 10 1 5 4\n1 3",
"output": "2"
},
{
"input": "4\n5 2 6 10 8 3\n1 7\n2 10 4\n2 9 5",
"output": "5"
},
{
"input": "5\n3 12 10 19\n5 1 4 18 11 13\n4 17 15 2 6\n4 3 12 18 10\n4 2 8 5 9",
"output": "8"
},
{
"input": "20\n3 9 18 16\n6 13 5 11 3 15 7\n5 16 20 8 9 12\n7 15 19 3 7 10 14 1\n3 13 4 11\n6 15 8 14 3 17 1\n4 9 19 2 16\n3 3 10 14\n4 1 15 18 16\n3 3 19 6\n2 15 16\n7 12 14 1 2 10 6 11\n2 7 17\n5 2 12 1 19 6\n10 18 10 5 7 11 20 3 13 4 8\n6 6 2 14 15 9 16\n9 20 7 1 13 4 11 18 12 19\n5 2 5 6 14 8\n6 19 1 11 20 9 10\n4 12 7 5 8",
"output": "35"
},
{
"input": "20\n5 15 40 35 68 16\n6 70 19 28 69 63 85\n4 37 80 41 90\n7 11 74 35 94 55 59 64\n7 46 28 22 90 95 37 13\n5 62 45 87 50 94\n6 85 83 37 72 27 53\n6 46 56 59 71 49 33\n7 17 35 91 24 50 30 38\n2 75 12\n2 46 38\n1 61\n11 15 79 14 89 19 96 84 26 93 83 18\n1 75\n6 4 100 55 59 74 83\n5 48 44 37 12 6\n6 68 53 28 43 10 63\n4 30 49 86 93\n2 62 13\n7 25 81 8 94 10 96 71",
"output": "34"
},
{
"input": "10\n1 77966\n1 79480\n1 94920\n1 53920\n1 15585\n1 57339\n1 1585\n1 91802\n1 27934\n1 20354",
"output": "0"
}
] | 560 | 30,412,800 | 3 | 126,514 |
|
0 | none | [
"none"
] | null | null | You are given an infinite periodic array *a*0,<=*a*1,<=...,<=*a**n*<=-<=1,<=... with the period of length *n*. Formally, . A periodic subarray (*l*,<=*s*) (0<=β€<=*l*<=<<=*n*, 1<=β€<=*s*<=<<=*n*) of array *a* is an infinite periodic array with a period of length *s* that is a subsegment of array *a*, starting with position *l*.
A periodic subarray (*l*,<=*s*) is superior, if when attaching it to the array *a*, starting from index *l*, any element of the subarray is larger than or equal to the corresponding element of array *a*. An example of attaching is given on the figure (top β infinite array *a*, bottom β its periodic subarray (*l*,<=*s*)):
Find the number of distinct pairs (*l*,<=*s*), corresponding to the superior periodic arrays. | The first line contains number *n* (1<=β€<=*n*<=β€<=2Β·105). The second line contains *n* numbers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (1<=β€<=*a**i*<=β€<=106), separated by a space. | Print a single integer β the sought number of pairs. | [
"4\n7 1 2 3\n",
"2\n2 1\n",
"3\n1 1 1\n"
] | [
"2\n",
"1\n",
"6\n"
] | In the first sample the superior subarrays are (0, 1) and (3, 2).
Subarray (0, 1) is superior, as *a*<sub class="lower-index">0</sub>ββ₯β*a*<sub class="lower-index">0</sub>,β*a*<sub class="lower-index">0</sub>ββ₯β*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">0</sub>ββ₯β*a*<sub class="lower-index">2</sub>,β*a*<sub class="lower-index">0</sub>ββ₯β*a*<sub class="lower-index">3</sub>,β*a*<sub class="lower-index">0</sub>ββ₯β*a*<sub class="lower-index">0</sub>,β...
Subarray (3, 2) is superior *a*<sub class="lower-index">3</sub>ββ₯β*a*<sub class="lower-index">3</sub>,β*a*<sub class="lower-index">0</sub>ββ₯β*a*<sub class="lower-index">0</sub>,β*a*<sub class="lower-index">3</sub>ββ₯β*a*<sub class="lower-index">1</sub>,β*a*<sub class="lower-index">0</sub>ββ₯β*a*<sub class="lower-index">2</sub>,β*a*<sub class="lower-index">3</sub>ββ₯β*a*<sub class="lower-index">3</sub>,β...
In the third sample any pair of (*l*,β*s*) corresponds to a superior subarray as all the elements of an array are distinct. | [] | 124 | 3,481,600 | 0 | 126,570 |
|
869 | The Untended Antiquity | [
"data structures",
"hashing"
] | null | null | Adieu l'ami.
Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence.
The space is represented by a rectangular grid of *n*<=Γ<=*m* cells, arranged into *n* rows and *m* columns. The *c*-th cell in the *r*-th row is denoted by (*r*,<=*c*).
Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 *r*1 *c*1 *r*2 *c*2" means Oshino's placing barriers around a rectangle with two corners being (*r*1,<=*c*1) and (*r*2,<=*c*2) and sides parallel to squares sides. Similarly, "2 *r*1 *c*1 *r*2 *c*2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the *n*<=Γ<=*m* area.
Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 *r*1 *c*1 *r*2 *c*2" means that Koyomi tries to walk from (*r*1,<=*c*1) to (*r*2,<=*c*2) without crossing barriers.
And you're here to tell Koyomi the feasibility of each of his attempts. | The first line of input contains three space-separated integers *n*, *m* and *q* (1<=β€<=*n*,<=*m*<=β€<=2<=500, 1<=β€<=*q*<=β€<=100<=000) β the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively.
The following *q* lines each describes an action, containing five space-separated integers *t*, *r*1, *c*1, *r*2, *c*2 (1<=β€<=*t*<=β€<=3, 1<=β€<=*r*1,<=*r*2<=β€<=*n*, 1<=β€<=*c*1,<=*c*2<=β€<=*m*) β the type and two coordinates of an action. Additionally, the following holds depending on the value of *t*:
- If *t*<==<=1: 2<=β€<=*r*1<=β€<=*r*2<=β€<=*n*<=-<=1, 2<=β€<=*c*1<=β€<=*c*2<=β€<=*m*<=-<=1; - If *t*<==<=2: 2<=β€<=*r*1<=β€<=*r*2<=β€<=*n*<=-<=1, 2<=β€<=*c*1<=β€<=*c*2<=β€<=*m*<=-<=1, the specified group of barriers exist on the ground before the removal. - If *t*<==<=3: no extra restrictions. | For each of Koyomi's attempts (actions with *t*<==<=3), output one line β containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise. | [
"5 6 5\n1 2 2 4 5\n1 3 3 3 3\n3 4 4 1 1\n2 2 2 4 5\n3 1 1 4 4\n",
"2500 2500 8\n1 549 1279 1263 2189\n1 303 795 1888 2432\n1 2227 622 2418 1161\n3 771 2492 1335 1433\n1 2017 2100 2408 2160\n3 48 60 798 729\n1 347 708 1868 792\n3 1940 2080 377 1546\n"
] | [
"No\nYes\n",
"No\nYes\nNo\n"
] | For the first example, the situations of Koyomi's actions are illustrated below. | [
{
"input": "5 6 5\n1 2 2 4 5\n1 3 3 3 3\n3 4 4 1 1\n2 2 2 4 5\n3 1 1 4 4",
"output": "No\nYes"
},
{
"input": "2500 2500 8\n1 549 1279 1263 2189\n1 303 795 1888 2432\n1 2227 622 2418 1161\n3 771 2492 1335 1433\n1 2017 2100 2408 2160\n3 48 60 798 729\n1 347 708 1868 792\n3 1940 2080 377 1546",
"output": "No\nYes\nNo"
},
{
"input": "3 3 5\n3 1 3 1 3\n3 1 1 3 1\n1 2 2 2 2\n3 2 3 2 2\n3 1 1 2 2",
"output": "Yes\nYes\nNo\nNo"
},
{
"input": "3 4 5\n3 3 4 3 4\n1 2 2 2 3\n3 2 2 2 3\n3 3 3 2 4\n3 2 2 1 1",
"output": "Yes\nYes\nYes\nNo"
},
{
"input": "5 5 10\n1 3 2 4 2\n1 4 4 4 4\n1 2 4 2 4\n3 4 2 4 1\n3 2 2 5 3\n3 2 2 1 5\n3 3 2 5 1\n3 4 5 2 3\n3 1 5 4 2\n3 1 2 5 2",
"output": "No\nYes\nYes\nNo\nYes\nNo\nYes"
},
{
"input": "10 10 10\n1 5 7 8 8\n3 8 2 6 7\n1 5 4 9 4\n3 4 2 3 3\n1 2 3 3 4\n3 5 7 2 9\n1 3 6 9 9\n2 2 3 3 4\n3 1 2 2 2\n3 4 9 2 7",
"output": "No\nYes\nNo\nYes\nNo"
},
{
"input": "10 10 10\n1 5 4 8 5\n3 3 6 5 9\n3 3 10 1 1\n1 3 5 3 5\n1 3 7 8 8\n1 2 2 2 3\n3 9 7 7 3\n1 4 2 8 2\n3 9 2 9 3\n3 7 4 7 4",
"output": "Yes\nYes\nYes\nYes\nYes"
},
{
"input": "2450 100 20\n1 333 19 414 95\n1 1121 15 2270 28\n3 539 27 2026 84\n1 1286 78 2422 84\n1 717 3 1051 65\n2 1121 15 2270 28\n3 1127 42 42 92\n3 1701 70 503 6\n1 2008 7 2363 59\n3 2183 52 282 43\n1 530 7 642 32\n3 13 57 2127 1\n2 717 3 1051 65\n2 2008 7 2363 59\n1 1501 10 2213 66\n3 596 15 1215 15\n1 1016 13 1262 65\n1 815 23 841 39\n2 530 7 642 32\n3 1189 5 794 33",
"output": "Yes\nYes\nYes\nNo\nYes\nNo\nYes"
},
{
"input": "1 1 1\n3 1 1 1 1",
"output": "Yes"
}
] | 1,122 | 248,012,800 | -1 | 126,644 |
|
28 | Don't fear, DravDe is kind | [
"binary search",
"data structures",
"dp",
"hashing"
] | D. Don't fear, DravDe is kind | 2 | 256 | A motorcade of *n* trucks, driving from city Β«ZΒ» to city Β«ΠΒ», has approached a tunnel, known as Tunnel of Horror. Among truck drivers there were rumours about monster DravDe, who hunts for drivers in that tunnel. Some drivers fear to go first, others - to be the last, but let's consider the general case. Each truck is described with four numbers:
- *v* β value of the truck, of its passangers and cargo - *c* β amount of passanger on the truck, the driver included - *l* β total amount of people that should go into the tunnel before this truck, so that the driver can overcome his fear (Β«if the monster appears in front of the motorcade, he'll eat them firstΒ») - *r* β total amount of people that should follow this truck, so that the driver can overcome his fear (Β«if the monster appears behind the motorcade, he'll eat them firstΒ»).
Since the road is narrow, it's impossible to escape DravDe, if he appears from one side. Moreover, the motorcade can't be rearranged. The order of the trucks can't be changed, but it's possible to take any truck out of the motorcade, and leave it near the tunnel for an indefinite period. You, as the head of the motorcade, should remove some of the trucks so, that the rest of the motorcade can move into the tunnel and the total amount of the left trucks' values is maximal. | The first input line contains integer number *n* (1<=β€<=*n*<=β€<=105) β amount of trucks in the motorcade. The following *n* lines contain four integers each. Numbers in the *i*-th line: *v**i*,<=*c**i*,<=*l**i*,<=*r**i* (1<=β€<=*v**i*<=β€<=104,<=1<=β€<=*c**i*<=β€<=105,<=0<=β€<=*l**i*,<=*r**i*<=β€<=105) β describe the *i*-th truck. The trucks are numbered from 1, counting from the front of the motorcade. | In the first line output number *k* β amount of trucks that will drive into the tunnel. In the second line output *k* numbers β indexes of these trucks in ascending order. Don't forget please that you are not allowed to change the order of trucks. If the answer is not unique, output any. | [
"5\n1 1 0 3\n1 1 1 2\n1 1 2 1\n1 1 3 0\n2 1 3 0\n",
"5\n1 1 0 3\n10 1 2 1\n2 2 1 1\n10 1 1 2\n3 1 3 0\n"
] | [
"4\n1 2 3 5 \n",
"3\n1 3 5 \n"
] | none | [
{
"input": "5\n1 1 0 3\n1 1 1 2\n1 1 2 1\n1 1 3 0\n2 1 3 0",
"output": "4\n1 2 3 5 "
},
{
"input": "5\n1 1 0 3\n10 1 2 1\n2 2 1 1\n10 1 1 2\n3 1 3 0",
"output": "3\n1 3 5 "
},
{
"input": "10\n3007 1 0 12\n5126 1 1 11\n2245 2 2 9\n8547 1 4 8\n8531 1 5 7\n5017 1 6 6\n6222 1 7 5\n6748 1 8 4\n9401 2 9 2\n2940 2 11 0",
"output": "10\n1 2 3 4 5 6 7 8 9 10 "
},
{
"input": "10\n4053 1 0 13\n6467 2 1 11\n3901 1 3 10\n4711 2 4 8\n838 1 6 7\n7938 2 7 5\n7241 1 9 4\n8432 1 10 3\n1021 2 11 1\n84 1 13 0",
"output": "10\n1 2 3 4 5 6 7 8 9 10 "
},
{
"input": "10\n4337 1 0 9\n3010 1 0 9\n9655 1 1 8\n7734 1 2 7\n7462 3 3 4\n4791 2 2 6\n6816 2 6 2\n8395 1 8 1\n6900 1 9 0\n6358 2 3 5",
"output": "7\n1 3 4 5 7 8 9 "
},
{
"input": "20\n7892 2 0 24\n4201 1 2 23\n3667 1 3 22\n6388 1 4 21\n2406 1 5 20\n1148 1 6 19\n3958 1 7 18\n7363 1 8 17\n9025 2 9 15\n1909 1 11 14\n73 1 12 13\n5477 2 13 11\n1869 1 15 10\n107 1 16 9\n523 1 17 8\n6589 2 18 6\n6277 2 20 4\n3881 1 22 3\n5771 1 23 2\n622 2 24 0",
"output": "20\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 "
},
{
"input": "4\n5 3 0 1\n2 1 0 3\n2 2 1 1\n1 1 3 0",
"output": "2\n1 4 "
},
{
"input": "1\n1 1 1 1",
"output": "0"
},
{
"input": "4\n3 3 0 1\n2 1 0 3\n2 2 1 1\n1 1 3 0",
"output": "3\n2 3 4 "
},
{
"input": "4\n2 1 0 3\n2 2 1 1\n3 3 0 1\n1 1 3 0",
"output": "3\n1 2 4 "
},
{
"input": "4\n2 1 0 3\n2 2 1 1\n5 3 0 1\n1 1 3 0",
"output": "2\n3 4 "
},
{
"input": "4\n2 1 0 3\n3 3 0 1\n2 2 1 1\n1 1 3 0",
"output": "3\n1 3 4 "
},
{
"input": "1\n10000 100000 100000 100000",
"output": "0"
}
] | 92 | 0 | 0 | 128,150 |
486 | Palindrome Transformation | [
"brute force",
"greedy",
"implementation"
] | null | null | Nam is playing with a string on his computer. The string consists of *n* lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position *i* (1<=β€<=*i*<=β€<=*n*, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position *i*<=-<=1 if *i*<=><=1 or to the end of the string (i. e. position *n*) otherwise. The same holds when he presses the right arrow key (if *i*<==<=*n*, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position *p*.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome? | The first line contains two space-separated integers *n* (1<=β€<=*n*<=β€<=105) and *p* (1<=β€<=*p*<=β€<=*n*), the length of Nam's string and the initial position of the text cursor.
The next line contains *n* lowercase characters of Nam's string. | Print the minimum number of presses needed to change string into a palindrome. | [
"8 3\naeabcaez\n"
] | [
"6\n"
] | A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7cd1b7b2498296406fff738215b48ddd9a12898d.png" style="max-width: 100.0%;max-height: 100.0%;"/> (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:
The result, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/f998673d1ad2128f8e2de7ff01895c8ef9ee34ff.png" style="max-width: 100.0%;max-height: 100.0%;"/>, is now a palindrome. | [
{
"input": "8 3\naeabcaez",
"output": "6"
},
{
"input": "8 3\nabcddcbb",
"output": "3"
},
{
"input": "4 4\nrkoa",
"output": "14"
},
{
"input": "39 30\nyehuqwaffoiyxhkmdipxroolhahbhzprioobxfy",
"output": "138"
},
{
"input": "40 23\nvwjzsgpdsopsrpsyccavfkyyahdgkmdxrquhcplw",
"output": "169"
},
{
"input": "10 5\nabcdeedcba",
"output": "0"
},
{
"input": "5 5\npjfjb",
"output": "12"
},
{
"input": "57 9\nibkypcbtpdlhhpmghwrmuwaqoqxxexxqoqawumrwhgmphhldixezvfpqh",
"output": "55"
},
{
"input": "10 6\nabcdefdcba",
"output": "1"
},
{
"input": "167 152\nvqgjxbuxevpqbpnuyxktgpwdgyebnmrxbnitphshuloyykpgxakxadtguqskmhejndzptproeabnlvfwdyjiydfrjkxpvpbzwutsdpfawwcqqqirxwlkrectlnpdeccaoqetcaqcvyjtfoekyupvbsoiyldggycphddecbf",
"output": "666"
},
{
"input": "93 61\nuecrsqsoylbotwcujcsbjohlyjlpjsjsnvttpytrvztqtkpsdcrvsossimwmglumwzpouhaiqvowthzsyonxjjearhniq",
"output": "367"
},
{
"input": "63 4\nwzxjoumbtneztzheqznngprtcqjvawcycwavjqctrpgnnzqehztzentbmuojxzw",
"output": "0"
},
{
"input": "85 19\nblkimwzicvbdkwfodvigvmnujnotwuobkjvugbtaseebxvdiorffqnhllwtwdnfodkuvdofwkdbvcizwmiklb",
"output": "187"
},
{
"input": "198 3\ntuxqalctjyegbvouezfiqoeoazizhmjhpcmvyvjkyrgxkeupwcmvzcosdrrfgtdmxwfttxjxsbaspjwftgpnvsfyfqsrmyjmypdwonbzwsftepwtjlgbilhcsqyfzfzrfvrvfqiwoemthwvqptqnflqqspvqrnmvucnspexpijnivqpavqxjyucufcullevaedlvut",
"output": "692"
},
{
"input": "46 29\nxxzkzsxlyhotmfjpptrilatgtqpyshraiycmyzzlrcllvu",
"output": "168"
},
{
"input": "1 1\na",
"output": "0"
},
{
"input": "2 2\nat",
"output": "7"
},
{
"input": "10 4\nabcddddcef",
"output": "11"
},
{
"input": "8 8\naccedcba",
"output": "5"
},
{
"input": "1 1\nd",
"output": "0"
}
] | 46 | 0 | 0 | 128,287 |
|
723 | st-Spanning Tree | [
"dsu",
"graphs",
"greedy",
"implementation"
] | null | null | You are given an undirected connected graph consisting of *n* vertices and *m* edges. There are no loops and no multiple edges in the graph.
You are also given two distinct vertices *s* and *t*, and two values *d**s* and *d**t*. Your task is to build any spanning tree of the given graph (note that the graph is not weighted), such that the degree of the vertex *s* doesn't exceed *d**s*, and the degree of the vertex *t* doesn't exceed *d**t*, or determine, that there is no such spanning tree.
The spanning tree of the graph *G* is a subgraph which is a tree and contains all vertices of the graph *G*. In other words, it is a connected graph which contains *n*<=-<=1 edges and can be obtained by removing some of the edges from *G*.
The degree of a vertex is the number of edges incident to this vertex. | The first line of the input contains two integers *n* and *m* (2<=β€<=*n*<=β€<=200<=000, 1<=β€<=*m*<=β€<=*min*(400<=000,<=*n*Β·(*n*<=-<=1)<=/<=2))Β β the number of vertices and the number of edges in the graph.
The next *m* lines contain the descriptions of the graph's edges. Each of the lines contains two integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*, *u*<=β <=*v*)Β β the ends of the corresponding edge. It is guaranteed that the graph contains no loops and no multiple edges and that it is connected.
The last line contains four integers *s*, *t*, *d**s*, *d**t* (1<=β€<=*s*,<=*t*<=β€<=*n*, *s*<=β <=*t*, 1<=β€<=*d**s*,<=*d**t*<=β€<=*n*<=-<=1). | If the answer doesn't exist print "No" (without quotes) in the only line of the output.
Otherwise, in the first line print "Yes" (without quotes). In the each of the next (*n*<=-<=1) lines print two integers β the description of the edges of the spanning tree. Each of the edges of the spanning tree must be printed exactly once.
You can output edges in any order. You can output the ends of each edge in any order.
If there are several solutions, print any of them. | [
"3 3\n1 2\n2 3\n3 1\n1 2 1 1\n",
"7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4\n"
] | [
"Yes\n3 2\n1 3\n",
"Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1\n"
] | none | [
{
"input": "3 3\n1 2\n2 3\n3 1\n1 2 1 1",
"output": "Yes\n3 2\n1 3"
},
{
"input": "7 8\n7 4\n1 3\n5 4\n5 7\n3 2\n2 4\n6 1\n1 2\n6 4 1 4",
"output": "Yes\n1 3\n5 7\n3 2\n7 4\n2 4\n6 1"
},
{
"input": "10 15\n4 1\n5 10\n2 1\n5 7\n9 2\n4 6\n6 7\n9 1\n6 9\n8 4\n8 3\n9 8\n3 9\n2 3\n7 10\n10 1 2 3",
"output": "Yes\n5 7\n9 2\n4 6\n6 7\n6 9\n8 4\n8 3\n7 10\n1 2"
},
{
"input": "10 10\n1 3\n10 3\n6 1\n2 7\n1 7\n1 9\n9 5\n2 10\n10 8\n4 3\n9 5 2 4",
"output": "Yes\n1 3\n10 3\n6 1\n2 7\n1 7\n10 8\n4 3\n1 9\n9 5"
},
{
"input": "4 4\n1 4\n4 2\n1 3\n4 3\n3 4 3 3",
"output": "Yes\n2 4\n1 3\n3 4"
},
{
"input": "3 2\n3 1\n2 3\n1 2 1 1",
"output": "Yes\n3 2\n1 3"
},
{
"input": "2 1\n1 2\n1 2 1 1",
"output": "Yes\n1 2"
},
{
"input": "3 3\n2 3\n1 2\n1 3\n2 3 1 1",
"output": "Yes\n1 3\n2 1"
},
{
"input": "2 1\n2 1\n1 2 1 1",
"output": "Yes\n1 2"
},
{
"input": "3 3\n3 1\n3 2\n2 1\n1 3 1 1",
"output": "Yes\n2 3\n1 2"
},
{
"input": "2 1\n2 1\n2 1 1 1",
"output": "Yes\n2 1"
},
{
"input": "5 6\n4 1\n3 2\n1 2\n1 3\n5 1\n3 4\n5 4 1 1",
"output": "Yes\n3 2\n1 2\n3 4\n5 1"
},
{
"input": "44 63\n19 38\n36 18\n3 11\n30 44\n24 31\n1 8\n28 38\n38 30\n14 24\n36 34\n32 38\n39 34\n17 38\n35 22\n5 43\n20 10\n22 10\n9 39\n42 22\n20 32\n14 6\n38 42\n40 38\n1 7\n36 15\n3 37\n36 10\n36 33\n12 38\n33 23\n10 43\n26 36\n35 44\n16 20\n9 36\n3 36\n1 2\n36 27\n21 36\n36 13\n25 37\n29 30\n36 31\n5 28\n31 20\n35 41\n16 36\n36 25\n8 14\n17 39\n42 28\n34 5\n41 38\n1 36\n15 24\n3 16\n14 1\n44 23\n6 26\n30 4\n30 23\n38 36\n5 11\n25 4 12 10",
"output": "Yes\n19 38\n36 18\n3 11\n30 44\n24 31\n1 8\n28 38\n38 30\n14 24\n36 34\n32 38\n39 34\n17 38\n35 22\n5 43\n20 10\n22 10\n9 39\n42 22\n20 32\n14 6\n40 38\n1 7\n36 15\n3 37\n36 10\n36 33\n12 38\n33 23\n10 43\n26 36\n16 20\n3 36\n1 2\n36 27\n21 36\n36 13\n29 30\n36 31\n35 41\n8 14\n37 25\n4 30"
},
{
"input": "10 16\n6 1\n1 10\n6 5\n5 7\n5 2\n4 1\n1 3\n5 8\n1 8\n1 7\n4 5\n9 1\n10 5\n9 5\n3 5\n1 2\n1 5 4 5",
"output": "Yes\n2 1\n3 1\n4 1\n6 5\n7 5\n8 5\n9 5\n10 5\n1 6"
},
{
"input": "2 1\n1 2\n1 2 1 1",
"output": "Yes\n1 2"
},
{
"input": "2 1\n2 1\n1 2 1 1",
"output": "Yes\n1 2"
},
{
"input": "3 2\n2 1\n3 1\n1 2 1 1",
"output": "No"
},
{
"input": "3 2\n2 1\n3 1\n1 2 2 1",
"output": "Yes\n3 1\n1 2"
},
{
"input": "3 2\n2 1\n3 1\n1 2 1 2",
"output": "No"
},
{
"input": "3 2\n2 1\n3 1\n3 2 1 1",
"output": "Yes\n1 2\n3 1"
},
{
"input": "10 15\n3 2\n9 3\n9 4\n7 3\n9 1\n5 9\n9 8\n7 5\n5 2\n4 3\n6 8\n7 9\n9 10\n9 6\n5 6\n5 9 1 3",
"output": "No"
},
{
"input": "6 11\n1 5\n4 2\n5 2\n6 2\n4 6\n6 1\n3 5\n5 6\n1 2\n1 4\n1 3\n5 1 1 1",
"output": "No"
},
{
"input": "5 6\n3 1\n5 1\n3 5\n4 5\n1 2\n3 2\n1 3 1 1",
"output": "No"
},
{
"input": "15 15\n6 2\n7 5\n14 11\n8 10\n13 10\n10 9\n3 14\n9 6\n3 1\n5 6\n1 15\n2 12\n11 4\n2 3\n11 9\n9 2 2 2",
"output": "No"
},
{
"input": "5 5\n1 3\n1 4\n1 5\n2 3\n2 4\n1 2 2 2",
"output": "Yes\n5 1\n3 2\n4 2\n1 3"
},
{
"input": "23 32\n20 22\n17 21\n22 5\n11 13\n14 22\n1 16\n6 3\n8 5\n16 10\n10 22\n7 17\n21 1\n12 1\n14 19\n17 19\n12 17\n15 18\n9 19\n11 23\n12 2\n1 17\n14 4\n13 9\n23 1\n11 20\n20 16\n2 6\n7 23\n16 6\n18 14\n23 10\n5 3\n12 6 9 9",
"output": "Yes\n20 22\n17 21\n22 5\n11 13\n14 22\n1 16\n8 5\n16 10\n10 22\n7 17\n21 1\n14 19\n15 18\n9 19\n11 23\n14 4\n13 9\n18 14\n5 3\n2 12\n17 12\n6 2"
},
{
"input": "14 18\n6 9\n9 7\n6 10\n14 9\n5 9\n9 3\n3 1\n2 13\n13 8\n1 8\n11 3\n10 5\n12 5\n5 8\n13 7\n4 11\n2 5\n9 11\n8 3 2 2",
"output": "Yes\n6 9\n9 7\n6 10\n14 9\n5 9\n2 13\n12 5\n13 7\n4 11\n9 11\n1 8\n11 3\n8 5"
},
{
"input": "5 7\n1 3\n4 2\n5 1\n4 3\n2 3\n4 5\n1 2\n1 4 2 2",
"output": "Yes\n2 3\n3 1\n5 4\n1 5"
},
{
"input": "24 34\n21 22\n10 6\n22 5\n16 9\n18 11\n11 6\n24 6\n6 1\n18 2\n19 1\n21 14\n16 24\n13 18\n6 20\n17 18\n3 2\n21 3\n3 10\n14 16\n3 23\n6 13\n8 20\n1 9\n16 22\n22 1\n13 17\n12 4\n6 4\n5 3\n9 13\n23 15\n21 6\n21 7\n6 19\n4 6 2 3",
"output": "Yes\n21 22\n22 5\n16 9\n18 11\n18 2\n19 1\n21 14\n16 24\n13 18\n17 18\n3 2\n21 3\n3 10\n14 16\n3 23\n8 20\n1 9\n23 15\n21 7\n24 6\n12 4\n20 6\n4 6"
},
{
"input": "27 30\n17 27\n14 24\n1 17\n20 15\n15 21\n16 19\n6 13\n12 20\n3 16\n10 25\n7 26\n12 25\n17 26\n24 17\n2 6\n22 27\n22 9\n5 16\n3 25\n20 11\n23 24\n26 22\n11 13\n20 8\n17 11\n6 4\n22 25\n26 18\n9 6\n3 11\n19 16 5 20",
"output": "Yes\n17 27\n14 24\n1 17\n20 15\n15 21\n6 13\n12 20\n10 25\n7 26\n12 25\n17 26\n24 17\n2 6\n22 27\n22 9\n3 25\n20 11\n23 24\n11 13\n20 8\n17 11\n6 4\n26 18\n5 16\n3 16\n19 16"
},
{
"input": "30 32\n17 28\n7 28\n8 3\n15 25\n30 18\n2 12\n7 23\n15 29\n8 14\n7 24\n13 2\n9 1\n26 29\n9 21\n18 13\n22 6\n9 15\n23 30\n17 26\n9 26\n17 16\n5 2\n7 8\n7 5\n17 20\n29 10\n21 4\n5 19\n5 11\n29 6\n24 1\n24 27\n7 27 4 9",
"output": "Yes\n17 28\n8 3\n15 25\n30 18\n2 12\n15 29\n8 14\n13 2\n9 1\n26 29\n9 21\n18 13\n22 6\n9 15\n23 30\n17 26\n17 16\n5 2\n17 20\n29 10\n21 4\n5 19\n5 11\n29 6\n24 1\n23 7\n8 7\n28 7\n27 24"
},
{
"input": "17 27\n12 11\n5 12\n12 17\n7 14\n4 14\n17 8\n7 9\n15 13\n4 3\n13 17\n11 5\n15 8\n14 17\n8 13\n16 7\n8 3\n1 6\n8 7\n12 9\n7 11\n14 15\n3 7\n8 4\n7 2\n8 14\n14 10\n15 1\n15 6 2 3",
"output": "Yes\n12 11\n5 12\n12 17\n7 14\n4 14\n17 8\n7 9\n4 3\n13 17\n14 17\n16 7\n7 2\n14 10\n14 15\n1 6\n15 1"
}
] | 935 | 90,316,800 | 3 | 128,631 |
|
993 | The Moral Dilemma | [] | null | null | Hibiki and Dita are in love with each other, but belong to communities that are in a long lasting conflict. Hibiki is deeply concerned with the state of affairs, and wants to figure out if his relationship with Dita is an act of love or an act of treason.
Hibiki prepared several binary features his decision will depend on, and built a three layer logical circuit on top of them, each layer consisting of one or more [logic gates](https://en.wikipedia.org/wiki/Logic_gate). Each gate in the circuit is either "or", "and", "nor" (not or) or "nand" (not and). Each gate in the first layer is connected to exactly two features. Each gate in the second layer is connected to exactly two gates in the first layer. The third layer has only one "or" gate, which is connected to all the gates in the second layer (in other words, the entire circuit produces 1 if and only if at least one gate in the second layer produces 1).
The problem is, Hibiki knows very well that when the person is in love, his ability to think logically degrades drastically. In particular, it is well known that when a person in love evaluates a logical circuit in his mind, every gate evaluates to a value that is the opposite of what it was supposed to evaluate to. For example, "or" gates return 1 if and only if both inputs are zero, "t{nand}" gates produce 1 if and only if both inputs are one etc.
In particular, the "or" gate in the last layer also produces opposite results, and as such if Hibiki is in love, the entire circuit produces 1 if and only if all the gates on the second layer produced 0.
Hibiki canβt allow love to affect his decision. He wants to know what is the smallest number of gates that needs to be removed from the second layer so that the output of the circuit for all possible inputs doesn't depend on whether Hibiki is in love or not. | The first line contains three integers $n$, $m$, $k$ ($2 \le n, m \le 50$; $1 \le k \le 50$) β the number of input features, the number of gates in the first layer, and the number of gates in the second layer correspondingly.
The second line contains $m$ pairs of strings separated by spaces describing the first layer. The first string in each pair describes the gate ("and", "or", "nand" or "nor"), and the second string describes the two input features the gate is connected two as a string consisting of exactly $n$ characters, with exactly two characters (that correspond to the input features the gate is connected to) equal to 'x' and the remaining characters equal to ".'.
The third line contains $k$ pairs of strings separated by spaces describing the second layer in the same format, where the strings that describe the input parameters have length $m$ and correspond to the gates of the first layer. | Print the number of gates that need to be removed from the second layer so that the output of the remaining circuit doesn't depend on whether Hibiki is in love or not.
If no matter how many gates are removed the output of the circuit continues to depend on Hibiki's feelings, print $-1$. | [
"2 2 2\nand xx nand xx\nand xx or xx\n",
"3 2 2\nand xx. nor .xx\nand xx nor xx\n",
"4 4 5\nnor x..x and ..xx and xx.. nand xx..\nnand ..xx nor ..xx and xx.. nor ..xx or ..xx\n"
] | [
"1\n",
"-1\n",
"2\n"
] | In the first example the two gates in the first layer are connected to the same inputs, but first computes "and" while second computes "nand", and as such their output is always different no matter what the input is and whether Hibiki is in love or not. The second layer has "or" and "and" gates both connected to the two gates in the first layer. If Hibiki is not in love, the "and" gate will produce 0 and the "or" gate will produce 1 no matter what input features are equal to, with the final "or" gate in the third layer always producing the final answer of 1. If Hibiki is in love, "and" gate in the second layer will produce 1 and "or" gate will produce 0 no matter what the input is, with the final "or" gate in the third layer producing the final answer of 0. Thus, if both gates in the second layer are kept, the output of the circuit does depend on whether Hibiki is in love. If any of the two gates in the second layer is dropped, the output of the circuit will no longer depend on whether Hibiki is in love or not, and hence the answer is 1.
In the second example no matter what gates are left in the second layer, the output of the circuit will depend on whether Hibiki is in love or not.
In the third example if Hibiki keeps second, third and fourth gates in the second layer, the circuit will not depend on whether Hibiki is in love or not. Alternatively, he can keep the first and the last gates. The former requires removing two gates, the latter requires removing three gates, so the former is better, and the answer is 2. | [] | 61 | 0 | -1 | 128,652 |
|
540 | Infinite Inversions | [
"binary search",
"data structures",
"implementation",
"sortings",
"trees"
] | null | null | There is an infinite sequence consisting of all positive integers in the increasing order: *p*<==<={1,<=2,<=3,<=...}. We performed *n* swap operations with this sequence. A *swap*(*a*,<=*b*) is an operation of swapping the elements of the sequence on positions *a* and *b*. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (*i*,<=*j*), that *i*<=<<=*j* and *p**i*<=><=*p**j*. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of swap operations applied to the sequence.
Each of the next *n* lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=109, *a**i*<=β <=*b**i*)Β β the arguments of the swap operation. | Print a single integer β the number of inversions in the resulting sequence. | [
"2\n4 2\n1 4\n",
"3\n1 6\n3 4\n2 5\n"
] | [
"4\n",
"15\n"
] | In the first sample the sequence is being modified as follows: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/014a32f190673020641bf266b205f41f0a704f2d.png" style="max-width: 100.0%;max-height: 100.0%;"/>. It has 4 inversions formed by index pairs (1,β4), (2,β3), (2,β4) and (3,β4). | [
{
"input": "2\n4 2\n1 4",
"output": "4"
},
{
"input": "3\n1 6\n3 4\n2 5",
"output": "15"
},
{
"input": "1\n1000000000 1",
"output": "1999999997"
},
{
"input": "5\n2 5\n6 3\n4 6\n5 4\n2 5",
"output": "5"
},
{
"input": "4\n2 5\n4 3\n1 4\n6 2",
"output": "8"
},
{
"input": "3\n1 3\n4 6\n5 2",
"output": "7"
},
{
"input": "5\n1 1000000000\n2 999999999\n3 999999998\n4 999999997\n5 999999996",
"output": "9999999945"
},
{
"input": "30\n1 200000\n2 199999\n3 199998\n4 199997\n5 199996\n200001 399996\n200002 399997\n200003 399998\n200004 399999\n200005 400000\n400001 599998\n400002 599999\n400003 600000\n400004 599996\n400005 599995\n600001 800000\n600002 799999\n600003 799998\n600004 799997\n600005 799996\n600001 799998\n600002 799999\n600003 800000\n600004 799996\n600005 799997\n800001 999998\n800002 999997\n800003 999999\n800004 999996\n800005 1000000",
"output": "7999746"
}
] | 46 | 0 | -1 | 128,978 |
|
98 | Help Shrek and Donkey | [
"dp",
"games",
"math",
"probabilities"
] | E. Help Shrek and Donkey | 2 | 256 | Shrek and the Donkey (as you can guess, they also live in the far away kingdom) decided to play a card game called YAGame. The rules are very simple: initially Shrek holds *m* cards and the Donkey holds *n* cards (the players do not see each other's cards), and one more card lies on the table face down so that both players cannot see it as well. Thus, at the beginning of the game there are overall *m*<=+<=*n*<=+<=1 cards. Besides, the players know which cards the pack of cards consists of and their own cards (but they do not know which card lies on the table and which ones the other player has). The players move in turn and Shrek starts. During a move a player can:
- Try to guess which card is lying on the table. If he guesses correctly, the game ends and he wins. If his guess is wrong, the game also ends but this time the other player wins.- Name any card from the pack. If the other player has such card, he must show it and put it aside (so that this card is no longer used in the game). If the other player doesn't have such card, he says about that.
Help Shrek assuming the pills are good in quality and that both players using them start playing in the optimal manner. | The first line contains space-separated integers *m* and *n* (0<=β€<=*m*,<=*n*<=β€<=1000). | Print space-separated probabilities that Shrek wins and Donkey wins correspondingly; the absolute error should not exceed 10<=-<=9. | [
"0 3\n",
"1 0\n",
"1 1\n"
] | [
"0.25 0.75\n",
"1 0\n",
"0.5 0.5\n"
] | none | [
{
"input": "0 3",
"output": "0.2500000000000000 0.7500000000000000"
},
{
"input": "1 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "1 1",
"output": "0.5000000000000000 0.5000000000000000"
},
{
"input": "0 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "2 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "0 1",
"output": "0.5000000000000000 0.5000000000000000"
},
{
"input": "0 2",
"output": "0.3333333333333333 0.6666666666666667"
},
{
"input": "1 2",
"output": "0.5000000000000000 0.5000000000000000"
},
{
"input": "2 1",
"output": "0.6666666666666667 0.3333333333333333"
},
{
"input": "2 2",
"output": "0.5555555555555556 0.4444444444444445"
},
{
"input": "863 814",
"output": "0.5276200278934561 0.4723799721065439"
},
{
"input": "593 676",
"output": "0.4424015525217101 0.5575984474782899"
},
{
"input": "124 830",
"output": "0.0953414587193146 0.9046585412806855"
},
{
"input": "582 865",
"output": "0.3425732635446256 0.6574267364553744"
},
{
"input": "458 81",
"output": "0.8845491519873723 0.1154508480126277"
},
{
"input": "969 527",
"output": "0.7215811345992123 0.2784188654007878"
},
{
"input": "886 340",
"output": "0.7979217892875509 0.2020782107124492"
},
{
"input": "869 326",
"output": "0.8018568566832087 0.1981431433167913"
},
{
"input": "292 760",
"output": "0.2048964940792572 0.7951035059207428"
},
{
"input": "999 929",
"output": "0.5341268008319124 0.4658731991680876"
},
{
"input": "528 716",
"output": "0.3748204717131547 0.6251795282868453"
},
{
"input": "290 221",
"output": "0.6101220421882655 0.3898779578117345"
},
{
"input": "863 620",
"output": "0.6364521382599542 0.3635478617400458"
},
{
"input": "797 754",
"output": "0.5262188409949206 0.4737811590050795"
},
{
"input": "915 183",
"output": "0.8842865801642701 0.1157134198357299"
},
{
"input": "8 145",
"output": "0.1024062982090509 0.8975937017909491"
},
{
"input": "176 38",
"output": "0.8460331298315647 0.1539668701684353"
},
{
"input": "945 457",
"output": "0.7505320048782014 0.2494679951217987"
},
{
"input": "997 971",
"output": "0.5129154747745227 0.4870845252254772"
},
{
"input": "44 932",
"output": "0.0504572758127267 0.9495427241872734"
},
{
"input": "230 840",
"output": "0.1518922661058743 0.8481077338941256"
},
{
"input": "995 646",
"output": "0.6705883638280096 0.3294116361719904"
},
{
"input": "466 342",
"output": "0.6262428633441572 0.3737571366558427"
},
{
"input": "145 335",
"output": "0.2385967565862941 0.7614032434137059"
},
{
"input": "517 420",
"output": "0.5893460835202820 0.4106539164797181"
},
{
"input": "439 413",
"output": "0.5282979431644124 0.4717020568355876"
},
{
"input": "316 728",
"output": "0.2289941744785965 0.7710058255214035"
},
{
"input": "278 404",
"output": "0.3553552485610654 0.6446447514389346"
},
{
"input": "971 394",
"output": "0.7881032502577910 0.2118967497422090"
},
{
"input": "417 892",
"output": "0.2431983869698381 0.7568016130301619"
},
{
"input": "986 24",
"output": "0.9602291344083390 0.0397708655916610"
},
{
"input": "476 782",
"output": "0.3121578144498619 0.6878421855501382"
},
{
"input": "239 959",
"output": "0.1389465829388951 0.8610534170611048"
},
{
"input": "494 199",
"output": "0.7832163791968516 0.2167836208031484"
},
{
"input": "526 180",
"output": "0.8121571361695676 0.1878428638304324"
},
{
"input": "910 118",
"output": "0.9157417404948328 0.0842582595051672"
},
{
"input": "400 529",
"output": "0.3855660591427622 0.6144339408572379"
},
{
"input": "828 272",
"output": "0.8235052119879621 0.1764947880120379"
},
{
"input": "115 466",
"output": "0.1481169020875285 0.8518830979124715"
},
{
"input": "904 473",
"output": "0.7311570334143406 0.2688429665856594"
},
{
"input": "933 930",
"output": "0.5020349245008455 0.4979650754991545"
},
{
"input": "356 979",
"output": "0.1927153080820126 0.8072846919179874"
},
{
"input": "775 436",
"output": "0.7112726905771168 0.2887273094228831"
},
{
"input": "351 552",
"output": "0.3277875683152701 0.6722124316847299"
},
{
"input": "383 81",
"output": "0.8661760153892093 0.1338239846107907"
},
{
"input": "767 536",
"output": "0.6454773325210086 0.3545226674789914"
},
{
"input": "927 443",
"output": "0.7531609922031890 0.2468390077968110"
},
{
"input": "877 752",
"output": "0.5690585809577738 0.4309414190422261"
},
{
"input": "813 407",
"output": "0.7413726394428866 0.2586273605571135"
},
{
"input": "908 693",
"output": "0.6148786953306118 0.3851213046693882"
},
{
"input": "811 84",
"output": "0.9253912917590257 0.0746087082409743"
},
{
"input": "745 382",
"output": "0.7349508761172829 0.2650491238827171"
},
{
"input": "835 167",
"output": "0.8832155725457145 0.1167844274542855"
},
{
"input": "682 764",
"output": "0.4494589507746312 0.5505410492253688"
},
{
"input": "61 116",
"output": "0.3044180326794833 0.6955819673205167"
},
{
"input": "379 170",
"output": "0.7587191030678759 0.2412808969321241"
},
{
"input": "893 849",
"output": "0.5240293332658378 0.4759706667341623"
},
{
"input": "140 573",
"output": "0.1435260487631065 0.8564739512368935"
},
{
"input": "112 499",
"output": "0.1369030739746874 0.8630969260253126"
},
{
"input": "496 549",
"output": "0.4556254692028442 0.5443745307971558"
},
{
"input": "187 438",
"output": "0.2316723204029031 0.7683276795970969"
},
{
"input": "381 84",
"output": "0.8621493892197305 0.1378506107802695"
},
{
"input": "504 693",
"output": "0.3700982261347415 0.6299017738652585"
},
{
"input": "599 431",
"output": "0.6343903812186339 0.3656096187813661"
},
{
"input": "989 945",
"output": "0.5217673947588907 0.4782326052411093"
},
{
"input": "971 413",
"output": "0.7787240715751295 0.2212759284248705"
},
{
"input": "461 331",
"output": "0.6337385838340505 0.3662614161659495"
},
{
"input": "936 101",
"output": "0.9255659559790833 0.0744340440209166"
},
{
"input": "143 959",
"output": "0.0932096071577605 0.9067903928422395"
},
{
"input": "817 90",
"output": "0.9226954449092134 0.0773045550907866"
},
{
"input": "864 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "984 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "561 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "288 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "416 0",
"output": "1.0000000000000000 0.0000000000000000"
},
{
"input": "0 51",
"output": "0.0192307692307692 0.9807692307692307"
},
{
"input": "0 45",
"output": "0.0217391304347826 0.9782608695652174"
},
{
"input": "0 851",
"output": "0.0011737089201878 0.9988262910798123"
},
{
"input": "0 351",
"output": "0.0028409090909091 0.9971590909090909"
},
{
"input": "0 832",
"output": "0.0012004801920768 0.9987995198079231"
},
{
"input": "257 1000",
"output": "0.1421267475833342 0.8578732524166658"
},
{
"input": "327 1000",
"output": "0.1751041603600256 0.8248958396399744"
},
{
"input": "785 1000",
"output": "0.3964373963757117 0.6035626036242883"
},
{
"input": "314 1000",
"output": "0.1689449752379237 0.8310550247620764"
},
{
"input": "521 1000",
"output": "0.2681780053273835 0.7318219946726166"
},
{
"input": "1000 110",
"output": "0.9255370262407143 0.0744629737592857"
},
{
"input": "1000 493",
"output": "0.7463393524285148 0.2536606475714852"
},
{
"input": "1000 897",
"output": "0.5500860654670416 0.4499139345329584"
},
{
"input": "1000 32",
"output": "0.9575365847396112 0.0424634152603888"
},
{
"input": "1000 1000",
"output": "0.5004843592014406 0.4995156407985593"
}
] | 218 | 17,920,000 | 3.912121 | 129,468 |
380 | Sereja and Cinema | [
"combinatorics",
"math"
] | null | null | The cinema theater hall in Sereja's city is *n* seats lined up in front of one large screen. There are slots for personal possessions to the left and to the right of each seat. Any two adjacent seats have exactly one shared slot. The figure below shows the arrangement of seats and slots for *n*<==<=4.
Today it's the premiere of a movie called "Dry Hard". The tickets for all the seats have been sold. There is a very strict controller at the entrance to the theater, so all *n* people will come into the hall one by one. As soon as a person enters a cinema hall, he immediately (momentarily) takes his seat and occupies all empty slots to the left and to the right from him. If there are no empty slots, the man gets really upset and leaves.
People are not very constant, so it's hard to predict the order in which the viewers will enter the hall. For some seats, Sereja knows the number of the viewer (his number in the entering queue of the viewers) that will come and take this seat. For others, it can be any order.
Being a programmer and a mathematician, Sereja wonders: how many ways are there for the people to enter the hall, such that nobody gets upset? As the number can be quite large, print it modulo 1000000007 (109<=+<=7). | The first line contains integer *n* (1<=β€<=*n*<=β€<=105). The second line contains *n* integers, the *i*-th integer shows either the index of the person (index in the entering queue) with the ticket for the *i*-th seat or a 0, if his index is not known. It is guaranteed that all positive numbers in the second line are distinct.
You can assume that the index of the person who enters the cinema hall is a unique integer from 1 to *n*. The person who has index 1 comes first to the hall, the person who has index 2 comes second and so on. | In a single line print the remainder after dividing the answer by number 1000000007 (109<=+<=7). | [
"11\n0 0 0 0 0 0 0 0 0 0 0\n",
"6\n0 3 1 0 0 0\n"
] | [
"1024\n",
"3\n"
] | none | [] | 46 | 0 | 0 | 129,523 |
|
433 | Tachibana Kanade's Tofu | [
"dp"
] | null | null | Tachibana Kanade likes Mapo Tofu very much. One day, the canteen cooked all kinds of tofu to sell, but not all tofu is Mapo Tofu, only those spicy enough can be called Mapo Tofu.
Each piece of tofu in the canteen is given a *m*-based number, all numbers are in the range [*l*,<=*r*] (*l* and *r* being *m*-based numbers), and for every *m*-based integer in the range [*l*,<=*r*], there exists a piece of tofu with that number.
To judge what tofu is Mapo Tofu, Tachibana Kanade chose *n* *m*-based number strings, and assigned a value to each string. If a string appears in the number of a tofu, the value of the string will be added to the value of that tofu. If a string appears multiple times, then the value is also added that many times. Initially the value of each tofu is zero.
Tachibana Kanade considers tofu with values no more than *k* to be Mapo Tofu. So now Tachibana Kanade wants to know, how many pieces of tofu are Mapo Tofu? | The first line contains three integers *n*, *m* and *k*Β (1<=β€<=*n*<=β€<=200;Β 2<=β€<=*m*<=β€<=20;Β 1<=β€<=*k*<=β€<=500). Where *n* denotes the number of strings, *m* denotes the base used, and *k* denotes the limit of the value for Mapo Tofu.
The second line represents the number *l*. The first integer in the line is *len* (1<=β€<=*len*<=β€<=200), describing the length (number of digits in base *m*) of *l*. Then follow *len* integers *a*1,<=*a*2,<=...,<=*a**len*Β (0<=β€<=*a**i*<=<<=*m*;Β *a*1<=><=0) separated by spaces, representing the digits of *l*, with *a*1 being the highest digit and *a**len* being the lowest digit.
The third line represents the number *r* in the same format as *l*. It is guaranteed that 1<=β€<=*l*<=β€<=*r*.
Then follow *n* lines, each line describing a number string. The *i*-th line contains the *i*-th number string and *v**i* β the value of the *i*-th string (1<=β€<=*v**i*<=β€<=200). All number strings are described in almost the same format as *l*, the only difference is number strings may contain necessary leading zeros (see the first example). The sum of the lengths of all number strings does not exceed 200. | Output the number of pieces of Mapo Tofu modulo 1000000007 (109<=+<=7). The answer should be a decimal integer. | [
"2 10 1\n1 1\n3 1 0 0\n1 1 1\n1 0 1\n",
"2 10 12\n2 5 9\n6 6 3 5 4 9 7\n2 0 6 1\n3 6 7 2 1\n",
"4 2 6\n6 1 0 1 1 1 0\n6 1 1 0 1 0 0\n1 1 2\n3 0 1 0 5\n4 0 1 1 0 4\n3 1 0 1 2\n"
] | [
"97\n",
"635439\n",
"2\n"
] | In the first sample, 10, 11 and 100 are the only three decimal numbers in [1,β100] with a value greater than 1. Here the value of 1 is 1 but not 2, since numbers cannot contain leading zeros and thus cannot be written as "01".
In the second sample, no numbers in the given interval have a value greater than 12.
In the third sample, 110000 and 110001 are the only two binary numbers in the given interval with a value no greater than 6. | [] | 46 | 0 | 0 | 129,790 |
|
63 | Sweets Game | [
"bitmasks",
"dfs and similar",
"dp",
"games",
"implementation"
] | E. Sweets Game | 3 | 256 | Karlsson has visited Lillebror again. They found a box of chocolates and a big whipped cream cake at Lillebror's place. Karlsson immediately suggested to divide the sweets fairly between Lillebror and himself. Specifically, to play together a game he has just invented with the chocolates. The winner will get the cake as a reward.
The box of chocolates has the form of a hexagon. It contains 19 cells for the chocolates, some of which contain a chocolate. The players move in turns. During one move it is allowed to eat one or several chocolates that lay in the neighboring cells on one line, parallel to one of the box's sides. The picture below shows the examples of allowed moves and of an unacceptable one. The player who cannot make a move loses.
Karlsson makes the first move as he is Lillebror's guest and not vice versa. The players play optimally. Determine who will get the cake. | The input data contains 5 lines, containing 19 words consisting of one symbol. The word "O" means that the cell contains a chocolate and a "." stands for an empty cell. It is guaranteed that the box contains at least one chocolate. See the examples for better understanding. | If Karlsson gets the cake, print "Karlsson" (without the quotes), otherwise print "Lillebror" (yet again without the quotes). | [
". . .\n . . O .\n. . O O .\n . . . .\n . . .\n",
". . .\n . . . O\n. . . O .\n O . O .\n . O .\n"
] | [
"Lillebror",
"Karlsson"
] | none | [
{
"input": " . . .\n . . O .\n. . O O .\n . . . .\n . . .",
"output": "Lillebror"
},
{
"input": " . . .\n . . . O\n. . . O .\n O . O .\n . O .",
"output": "Karlsson"
},
{
"input": " . . .\n . . . .\n. . . . .\n . . O .\n . . .",
"output": "Karlsson"
},
{
"input": " . . .\n . . . .\n. . . . .\n O . . .\n . . O",
"output": "Lillebror"
},
{
"input": " . . .\n . O . .\n. . . O .\n . O . .\n . . .",
"output": "Karlsson"
},
{
"input": " . O O\n . . O .\n. . . . .\n . . . .\n . . .",
"output": "Lillebror"
},
{
"input": " . . O\n O . O .\n. . O . .\n . O . .\n O . .",
"output": "Karlsson"
},
{
"input": " . . .\n . . O .\n. O . O .\n O . . .\n . . .",
"output": "Lillebror"
},
{
"input": " O O .\n O O . .\n. . . . .\n . . . .\n . O .",
"output": "Lillebror"
},
{
"input": " . O O\n . O . O\n. O . . .\n . . . .\n . . .",
"output": "Karlsson"
},
{
"input": " . O O\n . O . O\n. . . . .\n . . . .\n . . O",
"output": "Lillebror"
},
{
"input": " O . .\n . O O O\n. O . . .\n . . . .\n . . .",
"output": "Karlsson"
},
{
"input": " . . .\n O . . O\n. . O . .\n . . O .\n . O .",
"output": "Karlsson"
},
{
"input": " . . .\n . O . .\n. O O . .\n . . O .\n . O .",
"output": "Karlsson"
},
{
"input": " . . O\n . . . O\n. . . O .\n . . O .\n . O .",
"output": "Karlsson"
},
{
"input": " . . .\n . . . .\nO . O . .\n O O . .\n O . .",
"output": "Lillebror"
},
{
"input": " . . .\n . . . .\n. O . O .\n . O O .\n O . .",
"output": "Lillebror"
},
{
"input": " O . .\n O O . .\n. . . . .\n O . O .\n . O .",
"output": "Karlsson"
},
{
"input": " . . .\n . . O O\n. . O . O\n . . O .\n . O .",
"output": "Karlsson"
},
{
"input": " . . O\n . O . O\n. . O O .\n . . O .\n . . .",
"output": "Karlsson"
},
{
"input": " . . O\n O . O O\nO O . . .\n . . . .\n . . .",
"output": "Lillebror"
},
{
"input": " . . .\n . O O .\nO . . O .\n . . . O\n . . O",
"output": "Lillebror"
},
{
"input": " . . .\n . . . O\n. . . O O\n . . O O\n . O O",
"output": "Karlsson"
},
{
"input": " . . .\n O . . O\n. O . O .\n . O O .\n . O .",
"output": "Lillebror"
},
{
"input": " . O .\n . . O .\n. O O . .\n O . O .\n . O .",
"output": "Lillebror"
},
{
"input": " O . O\n . . . .\nO . O . O\n . . . .\n O . O",
"output": "Karlsson"
},
{
"input": " . . .\n . O O O\n. O O . .\n O . . .\n O O .",
"output": "Karlsson"
},
{
"input": " . O O\n O O . O\nO O . O .\n . . . .\n . . .",
"output": "Lillebror"
},
{
"input": " . O O\n . O O O\n. . O O O\n . . . .\n . . .",
"output": "Lillebror"
},
{
"input": " . . .\n O O O .\nO O . O .\n O . O .\n . O .",
"output": "Karlsson"
},
{
"input": " O O O\n . O . O\nO O O O .\n . . . .\n . . .",
"output": "Lillebror"
},
{
"input": " . O O\n O . O O\nO O . . .\n . . . .\n O O O",
"output": "Karlsson"
},
{
"input": " O . O\n O . O .\nO . O . .\n O O . .\n O O O",
"output": "Karlsson"
},
{
"input": " . O O\n . O O O\n. . O . .\n O O . .\n O O O",
"output": "Karlsson"
},
{
"input": " . O O\n O O O O\nO . O O .\n O O . .\n . . .",
"output": "Lillebror"
},
{
"input": " . O .\n O . . .\nO O O O .\n O O O O\n . . O",
"output": "Lillebror"
},
{
"input": " O . O\n . O O O\n. . . . O\n O O O O\n . . O",
"output": "Lillebror"
},
{
"input": " . . O\n . O O .\nO . O . O\n O O O O\n . . O",
"output": "Lillebror"
},
{
"input": " . . .\n O O O .\n. O O O .\n O . O O\n O O O",
"output": "Karlsson"
},
{
"input": " O O O\n . O . O\nO O O O .\n . . O .\n O . O",
"output": "Karlsson"
},
{
"input": " O O O\n O O . O\nO O . O .\n . . O .\n O . O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO . . . O\n O . . O\n O . .",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\n. O O O .\n . O O .\n . . .",
"output": "Lillebror"
},
{
"input": " O . O\n O O O O\nO O . O O\n . O O .\n . . .",
"output": "Lillebror"
},
{
"input": " O O O\n O . . O\nO O O O O\n O . . .\n O . .",
"output": "Lillebror"
},
{
"input": " O O O\n . O O .\nO O . O O\n O . O .\n O . .",
"output": "Lillebror"
},
{
"input": " O O O\n O . . O\n. O O O .\n . . . O\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O . . O\nO . . . O\n O . . O\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O O O O\n. O . O O\n O . . .\n O O .",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO O O O .\n . . . .\n O O .",
"output": "Karlsson"
},
{
"input": " . . .\n . O O O\nO O O O O\n O O O O\n . O .",
"output": "Karlsson"
},
{
"input": " O O O\n . O O O\nO . . . O\n O O . O\n . O O",
"output": "Lillebror"
},
{
"input": " O . O\n O O O .\n. O O O .\n . O O .\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O O O O\n. . O . .\n O . . O\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O . O .\n. . . O O\n O O O O\n O O O",
"output": "Karlsson"
},
{
"input": " O O .\n O . O .\nO O O O O\n O O O O\n . . O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO O O O .\n O O O .\n . . .",
"output": "Karlsson"
},
{
"input": " . O O\n O O O O\nO O O O O\n O O O .\n . . .",
"output": "Lillebror"
},
{
"input": " O . O\n O O O O\n. O . O O\n O O . .\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O . O .\nO . . O O\n O . O O\n O O O",
"output": "Lillebror"
},
{
"input": " . O .\n O O . O\nO . O O O\n O O O O\n O O O",
"output": "Karlsson"
},
{
"input": " O O O\n O O . O\nO . O O O\n O . O O\n O O .",
"output": "Karlsson"
},
{
"input": " O O O\n O O . O\n. O . O O\n O O . O\n O O O",
"output": "Lillebror"
},
{
"input": " . O O\n O . O O\nO O . O O\n . O O O\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O O . O\nO . O O O\n O O O O\n . O O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO O . O O\n O . O O\n O O .",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO O O O O\n O O O O\n . . .",
"output": "Lillebror"
},
{
"input": " O O O\n O O . O\nO O . O O\n O . O O\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O O O O\nO . O O .\n . O O O\n O O O",
"output": "Lillebror"
},
{
"input": " O . O\n O O O O\nO O O O O\n . O O .\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O . O O\nO O O . O\n O O O O\n O O O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O .\nO O O O O\n O O O O\n . O O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O .\nO O O O O\n O O O O\n O O O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO O O O O\n O O O O\n . O O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO O O O O\n O . O O\n O O O",
"output": "Karlsson"
},
{
"input": " O O O\n O O O O\nO O . O O\n O O O O\n O O O",
"output": "Lillebror"
},
{
"input": " O O O\n O O O O\nO O O O O\n O O O O\n O O O",
"output": "Karlsson"
},
{
"input": " . . .\n O . . O\n. . . . .\n O . . O\n O O O",
"output": "Karlsson"
},
{
"input": " . O O\n O O . O\nO . O O .\n O O . O\n . O O",
"output": "Karlsson"
},
{
"input": " . O O\n . O . .\nO . O O .\n O O . O\n . . O",
"output": "Karlsson"
}
] | 0 | 0 | -1 | 129,867 |
374 | Inna and Dima | [
"dfs and similar",
"dp",
"graphs",
"implementation"
] | null | null | Inna and Dima bought a table of size *n*<=Γ<=*m* in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A".
Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows:
1. initially, Inna chooses some cell of the table where letter "D" is written; 1. then Inna can move to some side-adjacent table cell that contains letter "I"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter "M"; then she can go to a side-adjacent cell that contains letter "A". Then Inna assumes that she has gone through her sweetheart's name; 1. Inna's next move can be going to one of the side-adjacent table cells that contains letter "D" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter "D" she always goes to the letter "I", from the letter "I" she always goes the to letter "M", from the letter "M" she always goes to the letter "A", and from the letter "A" she always goes to the letter "D".
Depending on the choice of the initial table cell, Inna can go through name DIMA either an infinite number of times or some positive finite number of times or she can't go through his name once. Help Inna find out what maximum number of times she can go through name DIMA. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=103).
Then follow *n* lines that describe Inna and Dima's table. Each line contains *m* characters. Each character is one of the following four characters: "D", "I", "M", "A".
Note that it is not guaranteed that the table contains at least one letter "D". | If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer β the maximum number of times Inna can go through name DIMA. | [
"1 2\nDI\n",
"2 2\nMA\nID\n",
"5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID\n"
] | [
"Poor Dima!\n",
"Poor Inna!\n",
"4\n"
] | Notes to the samples:
In the first test sample, Inna cannot go through name DIMA a single time.
In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner.
In the third test sample the best strategy is to start from the cell in the upper left corner of the table. Starting from this cell, Inna can go through name DIMA four times. | [
{
"input": "1 2\nDI",
"output": "Poor Dima!"
},
{
"input": "2 2\nMA\nID",
"output": "Poor Inna!"
},
{
"input": "5 5\nDIMAD\nDIMAI\nDIMAM\nDDMAA\nAAMID",
"output": "4"
},
{
"input": "1 1\nI",
"output": "Poor Dima!"
},
{
"input": "5 5\nDIMAD\nADDDI\nMDDDM\nIDDDA\nDAMID",
"output": "Poor Inna!"
},
{
"input": "5 5\nAAAAA\nAAAAA\nAAAAA\nAAAAA\nAAAAA",
"output": "Poor Dima!"
},
{
"input": "1 1\nD",
"output": "Poor Dima!"
},
{
"input": "1 4\nDIMA",
"output": "1"
},
{
"input": "4 1\nD\nI\nM\nA",
"output": "1"
},
{
"input": "2 2\nDI\nAM",
"output": "Poor Inna!"
},
{
"input": "2 2\nDI\nMA",
"output": "Poor Dima!"
},
{
"input": "5 5\nDIADD\nDMADD\nDDDID\nAMMMD\nMIDAD",
"output": "3"
},
{
"input": "10 10\nDIDDIMDIDD\nDMDDAADIDD\nDADDDDDMDD\nDDDDDDDADD\nDIDDDDIDDD\nDMDDDDMDDD\nDADDDDADID\nDDIMDDDDMD\nDDAADDIAAD\nDDDDDDMADD",
"output": "4"
},
{
"input": "14 34\nDDDDDDDDDDDDIMADDDDDDDDDDDDDDDIMAD\nDDDDDDIMADDDDDDDDDDDDDDDDDDDDDIMAD\nDDDDDDDDDDDDDIDDDDDDDDDDDIDIMIDIMA\nDDDDDDIMDDDDDDDDDDDDDIDDDDDDDDDDDD\nDDDDDDDDDDDDDDDDDMADDDDDDDDDDDDDMD\nDDDDDDIMIDDDDDDDDDDDDIMIDIMIDDDIDD\nDDDDDDDDDDDDDDDDDMIMDDDDIMDDIMADIM\nDDDDDDDDDDDADIMADIDDDDDDIDIMADADDD\nDDDDDDDDIDDDDDDDDDDDDDDDDMADIMDDAM\nDMDDDDDDDDDDDDIMADIMDDDDDMADDDMIDI\nDDDDDDDDIMDDDDDDDDDDIDIMADIDDDDMAD\nDDDIDDDDDDDDDDMIDIMADADADIMADIMAAD\nDDDADDDDDDDDDIMIMADIDDMDMAMIDMDDDM\nDIDIDDDDDDIIAAMMAIMIDADAAAMDIDDDID",
"output": "Poor Inna!"
},
{
"input": "9 6\nDIMADD\nIDDDDD\nMDDDDD\nADDDDD\nDIMADD\nIDDDDD\nMDDDDD\nADDDDD\nDDDDDD",
"output": "2"
},
{
"input": "1 1\nM",
"output": "Poor Dima!"
},
{
"input": "1 1\nA",
"output": "Poor Dima!"
},
{
"input": "1 4\nIMAD",
"output": "Poor Dima!"
}
] | 1,000 | 66,764,800 | 0 | 129,924 |
|
704 | Black Widow | [
"dp",
"graphs",
"implementation",
"math"
] | null | null | Natalia Romanova is trying to test something on the new gun S.H.I.E.L.D gave her. In order to determine the result of the test, she needs to find the number of answers to a certain equation. The equation is of form:
Where represents logical OR and represents logical exclusive OR (XOR), and *v**i*,<=*j* are some boolean variables or their negations. Natalia calls the left side of the equation a XNF formula. Each statement in brackets is called a clause, and *v**i*,<=*j* are called literals.
In the equation Natalia has, the left side is actually a 2-XNF-2 containing variables *x*1,<=*x*2,<=...,<=*x**m* and their negations. An XNF formula is 2-XNF-2 if:
1. For each 1<=β€<=*i*<=β€<=*n*, *k**i*<=β€<=2, i.e. the size of each clause doesn't exceed two. 1. Each variable occurs in the formula at most two times (with negation and without negation in total). Please note that it's possible that a variable occurs twice but its negation doesn't occur in any clause (or vice versa).
Natalia is given a formula of *m* variables, consisting of *n* clauses. Please, make sure to check the samples in order to properly understand how the formula looks like.
Natalia is more into fight than theory, so she asked you to tell her the number of answers to this equation. More precisely, you need to find the number of ways to set *x*1,<=...,<=*x**m* with *true* and *false* (out of total of 2*m* ways) so that the equation is satisfied. Since this number can be extremely large, you need to print the answer modulo 109<=+<=7.
Please, note that some variable may appear twice in one clause, or not appear in the equation at all (but still, setting it to *false* or *true* gives different ways to set variables). | The first line of input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100<=000)Β β the number of clauses and the number of variables respectively.
The next *n* lines contain the formula. The *i*-th of them starts with an integer *k**i*Β β the number of literals in the *i*-th clause. It is followed by *k**i* non-zero integers *a**i*,<=1,<=...,<=*a**i*,<=*k**i*. If *a**i*,<=*j*<=><=0 then *v**i*,<=*j* is *x**a**i*,<=*j* otherwise it's negation of *x*<=-<=*a**i*,<=*j* (1<=β€<=*k**i*<=β€<=2, <=-<=*m*<=β€<=*a**i*,<=*j*<=β€<=*m*, *a**i*,<=*j*<=β <=0). | Print the answer modulo 1<=000<=000<=007 (109<=+<=7) in one line. | [
"6 7\n2 4 -2\n2 6 3\n2 -7 1\n2 -5 1\n2 3 6\n2 -2 -5\n",
"8 10\n1 -5\n2 4 -6\n2 -2 -6\n2 -7 9\n2 10 -1\n2 3 -1\n2 -8 9\n2 5 8\n",
"2 3\n2 1 1\n2 -3 3\n"
] | [
"48\n",
"544\n",
"4\n"
] | The equation in the first sample is:
The equation in the second sample is:
The equation in the third sample is: | [] | 46 | 0 | 0 | 130,400 |
|
774 | Perfectionist Arkadiy | [
"*special",
"number theory"
] | null | null | Arkadiy has lots square photos with size *a*<=Γ<=*a*. He wants to put some of them on a rectangular wall with size *h*<=Γ<=*w*.
The photos which Arkadiy will put on the wall must form a rectangular grid and the distances between neighboring vertically and horizontally photos and also the distances between outside rows and columns of photos to the nearest bound of the wall must be equal to *x*, where *x* is some non-negative real number. Look on the picture below for better understanding of the statement.
Arkadiy haven't chosen yet how many photos he would put on the wall, however, he want to put at least one photo. Your task is to determine the minimum value of *x* which can be obtained after putting photos, or report that there is no way to put positive number of photos and satisfy all the constraints. Suppose that Arkadiy has enough photos to make any valid arrangement according to the constraints.
Note that Arkadiy wants to put at least one photo on the wall. The photos should not overlap, should completely lie inside the wall bounds and should have sides parallel to the wall sides. | The first line contains three integers *a*, *h* and *w* (1<=β€<=*a*,<=*h*,<=*w*<=β€<=109) β the size of photos and the height and the width of the wall. | Print one non-negative real number β the minimum value of *x* which can be obtained after putting the photos on the wall. The absolute or the relative error of the answer must not exceed 10<=-<=6.
Print -1 if there is no way to put positive number of photos and satisfy the constraints. | [
"2 18 13\n",
"4 4 4\n",
"3 4 3\n"
] | [
"0.5\n",
"0\n",
"-1\n"
] | In the first example Arkadiy can put 7 rows of photos with 5 photos in each row, so the minimum value of *x* equals to 0.5.
In the second example Arkadiy can put only 1 photo which will take the whole wall, so the minimum value of *x* equals to 0.
In the third example there is no way to put positive number of photos and satisfy the constraints described in the statement, so the answer is -1. | [
{
"input": "2 18 13",
"output": "0.50000000000000000000"
},
{
"input": "4 4 4",
"output": "0.00000000000000000000"
},
{
"input": "3 4 3",
"output": "-1"
},
{
"input": "9 81 23",
"output": "-1"
},
{
"input": "11 21 21",
"output": "5.00000000000000000000"
},
{
"input": "55 178 996",
"output": "-1"
},
{
"input": "8 81 60",
"output": "-1"
},
{
"input": "3 1000000000 1000000000",
"output": "0.00000000299999980413"
},
{
"input": "1000000000 1000000000 1000000000",
"output": "0.00000000000000000000"
},
{
"input": "1 1000000000 1000000000",
"output": "0.00000000000000000000"
},
{
"input": "6 1000000000 1000000000",
"output": "0.00000002400000020941"
},
{
"input": "6 1000000000 956431324",
"output": "-1"
},
{
"input": "546 182989 371991",
"output": "235.00000000000000000000"
},
{
"input": "45 654489 357075",
"output": "1.50000000000000000000"
},
{
"input": "97259 999895180 999895180",
"output": "7.06740589436958543956"
},
{
"input": "453145 999531525 999531525",
"output": "157.20761559385573491454"
},
{
"input": "2233224 998602326 998602326",
"output": "783.92410714272409677505"
},
{
"input": "8710006 993275594 993275594",
"output": "2912.26086956448853015900"
},
{
"input": "599950915 648757793 648757793",
"output": "24403439.00000000000000000000"
},
{
"input": "85556375 910931345 910931345",
"output": "5033417.72727273404598236084"
},
{
"input": "263288720 933114664 933114664",
"output": "35812126.00000000000000000000"
},
{
"input": "1 1 1",
"output": "0.00000000000000000000"
},
{
"input": "2 1 1",
"output": "-1"
},
{
"input": "1000000000 1 1",
"output": "-1"
},
{
"input": "1000000000 1 1000000000",
"output": "-1"
},
{
"input": "1000000000 1000000000 1",
"output": "-1"
},
{
"input": "3 3 6",
"output": "0.00000000000000000000"
},
{
"input": "500000000 1000000000 1000000000",
"output": "0.00000000000000000000"
},
{
"input": "369635700 359542423 359542423",
"output": "-1"
},
{
"input": "9294381 967160417 967160417",
"output": "5188.50476190447807312012"
},
{
"input": "77810521 953603507 953603507",
"output": "1529019.61538460850715637207"
},
{
"input": "56392069 977149846 977149846",
"output": "1026926.27777777612209320068"
},
{
"input": "29940914 962870226 962870226",
"output": "144272.06060606241226196289"
},
{
"input": "98457054 957936620 957936620",
"output": "7182313.40000000596046447754"
},
{
"input": "26781706 947683080 947683080",
"output": "286760.27777777612209320068"
},
{
"input": "95297847 943912393 943912393",
"output": "8623177.00000000000000000000"
},
{
"input": "599950915 648757793 648757793",
"output": "24403439.00000000000000000000"
},
{
"input": "878532463 907519567 907519567",
"output": "14493552.00000000000000000000"
},
{
"input": "452081307 790635695 790635695",
"output": "169277194.00000000000000000000"
},
{
"input": "320597448 968719119 968719119",
"output": "1731693.75000000000000000000"
},
{
"input": "894146292 146802543 146802543",
"output": "-1"
},
{
"input": "322470944 972242878 972242878",
"output": "1207511.50000000000000000000"
},
{
"input": "896019789 208002095 208002095",
"output": "-1"
},
{
"input": "469568633 681052815 681052815",
"output": "105742091.00000000000000000000"
},
{
"input": "338084774 564168943 564168943",
"output": "113042084.50000000000000000000"
},
{
"input": "18926797 930932717 930932717",
"output": "70393.28000000119209289551"
},
{
"input": "234739357 906319479 906319479",
"output": "50525352.00000000000000000000"
},
{
"input": "488724368 443674657 443674657",
"output": "-1"
},
{
"input": "380555977 422333785 422333785",
"output": "20888904.00000000000000000000"
},
{
"input": "77 844667647 844667647",
"output": "0.00000492264608453752"
},
{
"input": "7 908904220 908904220",
"output": "0.00000000000000000000"
},
{
"input": "2 999999999 999999999",
"output": "0.00000000200000016548"
},
{
"input": "7 999999999 999999999",
"output": "0.00000003500000023138"
},
{
"input": "17 999999999 999999999",
"output": "0.00000010199999778138"
},
{
"input": "6 4 4",
"output": "-1"
}
] | 109 | 0 | 3 | 130,434 |
|
73 | Morrowindows | [
"math",
"number theory"
] | E. Morrowindows | 2 | 256 | Vasya plays The Elder Trolls III: Morrowindows. He has a huge list of items in the inventory, however, there is no limits on the size of things. Vasya does not know the total amount of items but he is sure that are not more than *x* and not less than 2 items in his inventory. A new patch for the game appeared to view inventory in *n* different modes. Displaying in mode *i* is a partition of all inventory items on pages, each of which (except for maybe the last one) shows exactly *a**i* items. In addition, each mode shows how many pages *b**i* is in a complete list. Great! Perhaps this information will be enough for Vasya to find the required number. Moreover, it is very interesting, what is the fewest number of modes in which Vasya can see inventory to determine the number of items in it?
Vasya cannot use the information that was received while looking on inventory in some mode for selection of next actions. I. e. Vasya chooses some set of modes first, and then sees all the results and determines the size.
Knowing the number of *a**i*, *x* and assuming that Vasya is very smart, check whether he can uniquely determine the number of items in his inventory, and how many modes he will need to do that if he knows numbers *a**i*, *x* and he is able to know number *b**i* after viewing items in mode *i*. | The first line contains two integers *n* and *x* (0<=β€<=*n*<=β€<=105,<=2<=β€<=*x*<=β€<=109). The second line contains integers *a**i* (1<=β€<=*a**i*<=β€<=109). Some numbers among all *a**i* may be equal. | Output the fewest amount of modes required to uniquely determine amount of items in the inventory. If there is no solution output <=-<=1. | [
"2 4\n2 3\n",
"1 4\n2\n"
] | [
"2\n",
"-1\n"
] | In the second example Vasya is not able to determine items count uniquely because 3 items, as well as 4 items, can be displayed on two pages. | [] | 92 | 0 | 0 | 130,548 |
718 | Andrew and Chemistry | [
"dp",
"hashing",
"trees"
] | null | null | During the chemistry lesson Andrew learned that the saturated hydrocarbons (alkanes) enter into radical chlorination reaction. Andrew is a very curious boy, so he wondered how many different products of the reaction may be forms for a given alkane. He managed to solve the task for small molecules, but for large ones he faced some difficulties and asks you to help.
Formally, you are given a tree consisting of *n* vertices, such that the degree of each vertex doesn't exceed 4. You have to count the number of distinct non-isomorphic trees that can be obtained by adding to this tree one new vertex and one new edge, such that the graph is still the tree and the degree of each vertex doesn't exceed 4.
Two trees are isomorphic if there exists a bijection *f*(*v*) such that vertices *u* and *v* are connected by an edge if and only if vertices *f*(*v*) and *f*(*u*) are connected by an edge. | The first line of the input contains an integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of vertices in the tree.
Then follow *n*<=-<=1 lines with edges descriptions. Each edge is given by two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*)Β β indices of vertices connected by an edge. It's guaranteed that the given graph is a tree and the degree of each vertex doesn't exceed 4. | Print one integerΒ β the answer to the question. | [
"4\n1 2\n2 3\n2 4\n",
"5\n1 2\n1 3\n1 4\n1 5\n",
"5\n2 5\n5 3\n4 3\n4 1\n"
] | [
"2\n",
"1\n",
"3\n"
] | In the first sample, one can add new vertex to any existing vertex, but the trees we obtain by adding a new vertex to vertices 1, 3 and 4 are isomorphic, thus the answer is 2.
In the second sample, one can't add new vertex to the first vertex, as its degree is already equal to four. Trees, obtained by adding a new vertex to vertices 2, 3, 4 and 5 are isomorphic, thus the answer is 1. | [] | 15 | 0 | 0 | 131,516 |
|
268 | Playlist | [
"math",
"probabilities",
"sortings"
] | null | null | Manao's friends often send him new songs. He never listens to them right away. Instead, he compiles them into a playlist. When he feels that his mind is open to new music, he opens the playlist and starts to listen to the songs.
Of course, there are some songs that Manao doesn't particuarly enjoy. To get more pleasure from the received songs, he invented the following procedure of listening to the playlist:
- If after listening to some song Manao realizes that he liked it, then he remembers it and starts to listen to the next unlistened song. - If after listening to some song Manao realizes that he did not like it, he listens to all the songs he liked up to this point and then begins to listen to the next unlistened song.
For example, if Manao has four songs in the playlist, A, B, C, D (in the corresponding order) and he is going to like songs A and C in the end, then the order of listening is the following:
1. Manao listens to A, he likes it, he remembers it. 1. Manao listens to B, he does not like it, so he listens to A, again. 1. Manao listens to C, he likes the song and he remembers it, too. 1. Manao listens to D, but does not enjoy it and re-listens to songs A and C.
That is, in the end Manao listens to song A three times, to song C twice and songs B and D once. Note that if Manao once liked a song, he will never dislike it on a subsequent listening.
Manao has received *n* songs: the *i*-th of them is *l**i* seconds long and Manao may like it with a probability of *p**i* percents. The songs could get on Manao's playlist in any order, so Manao wants to know the maximum expected value of the number of seconds after which the listening process will be over, for all possible permutations of the songs in the playlist. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=50000). The *i*-th of the following *n* lines contains two integers, separated by a single space β *l**i* and *p**i* (15<=β€<=*l**i*<=β€<=1000, 0<=β€<=*p**i*<=β€<=100) β the length of the *i*-th song in seconds and the probability that Manao will like the song, in percents. | In a single line print a single real number β the maximum expected listening time over all permutations of songs. The answer will be considered valid if the absolute or relative error does not exceed 10<=-<=9. | [
"3\n150 20\n150 50\n100 50\n",
"4\n300 0\n300 50\n240 50\n360 80\n"
] | [
"537.500000000\n",
"2121.000000000\n"
] | Consider the first test case. If Manao listens to the songs in the order in which they were originally compiled, the mathematical expectation will be equal to 467.5 seconds. The maximum expected value is obtained by putting the first song at the end of the playlist.
Consider the second test case. The song which is 360 seconds long should be listened to first. The song 300 seconds long which Manao will dislike for sure should be put in the end. | [] | 92 | 0 | 0 | 131,704 |
|
0 | none | [
"none"
] | null | null | Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.
Ostap's tree now has *n* vertices. He wants to paint some vertices of the tree black such that from any vertex *u* there is at least one black vertex *v* at distance no more than *k*. Distance between two vertices of the tree is the minimum possible number of edges of the path between them.
As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109<=+<=7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one. | The first line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100, 0<=β€<=*k*<=β€<=*min*(20,<=*n*<=-<=1))Β β the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for *k*.
Each of the next *n*<=-<=1 lines contain two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*)Β β indices of vertices, connected by the *i*-th edge. It's guaranteed that given graph is a tree. | Print one integerΒ β the remainder of division of the number of ways to paint the tree by 1<=000<=000<=007 (109<=+<=7). | [
"2 0\n1 2\n",
"2 1\n1 2\n",
"4 1\n1 2\n2 3\n3 4\n",
"7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7\n"
] | [
"1\n",
"3\n",
"9\n",
"91\n"
] | In the first sample, Ostap has to paint both vertices black.
In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.
In the third sample, the valid ways to paint vertices are: {1,β3}, {1,β4}, {2,β3}, {2,β4}, {1,β2,β3}, {1,β2,β4}, {1,β3,β4}, {2,β3,β4}, {1,β2,β3,β4}. | [
{
"input": "2 0\n1 2",
"output": "1"
},
{
"input": "2 1\n1 2",
"output": "3"
},
{
"input": "4 1\n1 2\n2 3\n3 4",
"output": "9"
},
{
"input": "7 2\n1 2\n2 3\n1 4\n4 5\n1 6\n6 7",
"output": "91"
},
{
"input": "1 0",
"output": "1"
},
{
"input": "10 0\n5 6\n4 3\n8 5\n3 7\n10 4\n1 2\n2 8\n10 1\n7 9",
"output": "1"
},
{
"input": "10 1\n6 8\n1 9\n1 5\n6 10\n3 9\n9 4\n10 9\n2 4\n7 4",
"output": "255"
},
{
"input": "10 2\n7 10\n2 1\n6 5\n10 6\n3 1\n10 4\n3 4\n9 3\n8 4",
"output": "705"
},
{
"input": "10 4\n4 6\n3 5\n7 3\n4 9\n10 2\n1 9\n4 8\n10 3\n8 7",
"output": "993"
},
{
"input": "10 3\n2 5\n4 8\n4 5\n6 8\n4 1\n5 10\n7 9\n4 7\n3 9",
"output": "945"
},
{
"input": "10 4\n9 6\n10 8\n9 2\n7 10\n3 7\n7 9\n10 4\n1 7\n9 5",
"output": "1023"
},
{
"input": "10 5\n6 4\n4 1\n7 5\n8 7\n2 6\n3 6\n8 6\n10 3\n4 9",
"output": "1023"
},
{
"input": "10 0\n9 10\n5 1\n10 5\n8 6\n8 2\n7 8\n8 1\n8 3\n4 3",
"output": "1"
},
{
"input": "10 1\n6 3\n1 5\n10 7\n4 10\n2 4\n5 9\n8 1\n1 3\n7 3",
"output": "279"
},
{
"input": "10 2\n1 4\n10 9\n1 5\n3 10\n2 10\n5 8\n6 1\n2 7\n6 10",
"output": "743"
},
{
"input": "10 9\n1 2\n3 1\n6 10\n6 1\n10 9\n4 5\n7 1\n5 6\n8 5",
"output": "1023"
},
{
"input": "5 1\n1 2\n4 3\n5 3\n2 3",
"output": "15"
},
{
"input": "63 5\n2 31\n5 41\n15 20\n38 12\n54 7\n31 54\n24 47\n13 19\n54 39\n57 50\n25 18\n59 11\n47 30\n60 49\n13 24\n7 11\n16 4\n28 59\n43 36\n3 33\n10 32\n40 48\n63 35\n49 56\n29 20\n44 18\n7 24\n49 18\n36 11\n9 13\n58 23\n34 47\n3 51\n5 61\n1 16\n26 6\n37 57\n26 8\n48 52\n25 43\n26 28\n22 39\n14 31\n44 21\n5 17\n33 23\n48 41\n42 44\n12 51\n59 50\n12 62\n25 41\n53 23\n16 28\n51 32\n63 27\n55 57\n33 20\n45 39\n32 46\n50 63\n3 43",
"output": "560665636"
},
{
"input": "64 3\n13 10\n60 54\n23 35\n4 41\n59 18\n41 36\n48 13\n64 8\n30 19\n53 3\n34 11\n24 27\n21 25\n5 55\n51 11\n3 45\n24 57\n47 38\n55 26\n8 20\n29 42\n44 3\n7 15\n9 1\n17 4\n41 9\n15 29\n60 13\n31 36\n53 52\n59 14\n12 52\n24 31\n47 28\n29 23\n2 49\n30 44\n28 31\n25 22\n9 58\n64 11\n45 35\n59 44\n62 8\n49 53\n32 48\n36 45\n4 56\n25 42\n42 33\n23 10\n48 39\n37 28\n10 64\n6 7\n30 63\n33 50\n40 7\n33 16\n49 46\n60 61\n52 43\n15 55",
"output": "257869945"
}
] | 30 | 0 | 0 | 131,745 |
|
725 | Family Photos | [
"games",
"greedy"
] | null | null | Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.
There are *n* stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks.
Each photo is described by two non-negative integers *a* and *b*, indicating that it is worth *a* units of happiness to Alice and *b* units of happiness to Bonnie. Values of *a* and *b* might differ for different photos.
It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively.
The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has *x* happiness and Bonnie has *y* happiness at the end, you should print *x*<=-<=*y*. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of two-photo stacks. Then follow *n* lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers *a*1, *b*1, *a*2 and *b*2, each not exceeding 109. *a*1 and *b*1 describe the top photo in the stack, while *a*2 and *b*2 describe the bottom photo in the stack. | Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally. | [
"2\n12 3 4 7\n1 15 9 1\n",
"2\n5 4 8 8\n4 12 14 0\n",
"1\n0 10 0 10\n"
] | [
"1\n",
"4\n",
"-10\n"
] | none | [
{
"input": "2\n12 3 4 7\n1 15 9 1",
"output": "1"
},
{
"input": "2\n5 4 8 8\n4 12 14 0",
"output": "4"
},
{
"input": "1\n0 10 0 10",
"output": "-10"
},
{
"input": "10\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000",
"output": "-10000000000"
},
{
"input": "10\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0",
"output": "10000000000"
},
{
"input": "10\n7 4 5 3\n6 7 10 1\n1 6 3 7\n3 4 6 0\n9 7 3 9\n9 1 10 10\n5 10 7 6\n8 5 1 9\n1 8 8 4\n7 5 9 4",
"output": "5"
},
{
"input": "10\n31181 6 72281 7\n65255 10 17867 2\n96212 8 32852 10\n69668 6 37236 1\n6031 7 61541 0\n12286 4 40227 2\n30573 9 36317 3\n97429 6 35399 9\n20907 5 19251 8\n86446 8 56301 10",
"output": "418488"
},
{
"input": "10\n3 86311 1 17635\n6 34036 8 37167\n4 53806 6 25588\n3 91425 4 86666\n6 50210 10 7332\n4 59721 10 697\n0 15949 2 66047\n0 68146 10 70354\n2 1075 0 23359\n5 48622 9 70531",
"output": "-382702"
},
{
"input": "1\n618436682 429762104 387908221 774440069",
"output": "-41853883"
},
{
"input": "2\n161177104 219082322 609600790 834825669\n28637550 764143871 832346520 978676472",
"output": "0"
},
{
"input": "2\n252323306 923185060 41887968 12082849\n956989832 311441362 23412620 77648740",
"output": "45134543"
},
{
"input": "3\n90031019 712505278 263580537 367435742\n66829732 915548158 31385852 432626559\n510612253 671761432 753952282 230959195",
"output": "-535068510"
},
{
"input": "4\n632771442 501825496 853869326 796417562\n250298560 151058732 702922596 492637085\n688226592 702784823 444110577 613300642\n809656720 636217627 270335668 655753596",
"output": "-104771122"
},
{
"input": "7\n479456257 682467462 202838061 802929167\n133179116 541657110 703996842 253385760\n559317856 865639323 321812660 580155067\n723036605 467995973 911771109 838534160\n732332969 59715289 216696388 60573917\n515702466 655741352 105418444 651954316\n884987644 907866122 399463262 952777963",
"output": "-253460781"
},
{
"input": "1\n0 0 0 0",
"output": "0"
}
] | 124 | 2,150,400 | -1 | 131,860 |
|
370 | Broken Monitor | [
"brute force",
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Innocentius has a problem β his computer monitor has broken. Now some of the pixels are "dead", that is, they are always black. As consequence, Innocentius can't play the usual computer games. He is recently playing the following game with his younger brother Polycarpus.
Innocentius is touch-typing a program that paints a white square one-pixel wide frame on the black screen. As the monitor is broken, some pixels that should be white remain black. Polycarpus should look at what the program displayed on the screen and guess the position and size of the frame Innocentius has painted. Polycarpus doesn't like the game but Innocentius persuaded brother to play as "the game is good for the imagination and attention".
Help Polycarpus, automatize his part in the gaming process. Write the code that finds such possible square frame that:
- the frame's width is 1 pixel, - the frame doesn't go beyond the borders of the screen, - all white pixels of the monitor are located on the frame, - of all frames that satisfy the previous three conditions, the required frame must have the smallest size.
Formally, a square frame is represented by such pixels of the solid square, that are on the square's border, that is, are not fully surrounded by the other pixels of the square. For example, if the frame's size is *d*<==<=3, then it consists of 8 pixels, if its size is *d*<==<=2, then it contains 4 pixels and if *d*<==<=1, then the frame is reduced to a single pixel. | The first line contains the resolution of the monitor as a pair of integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=2000). The next *n* lines contain exactly *m* characters each β the state of the monitor pixels at the moment of the game. Character "." (period, ASCII code 46) corresponds to the black pixel, and character "w" (lowercase English letter w) corresponds to the white pixel. It is guaranteed that at least one pixel of the monitor is white. | Print the monitor screen. Represent the sought frame by characters "+" (the "plus" character). The pixels that has become white during the game mustn't be changed. Print them as "w". If there are multiple possible ways to position the frame of the minimum size, print any of them.
If the required frame doesn't exist, then print a single line containing number -1. | [
"4 8\n..w..w..\n........\n........\n..w..w..\n",
"5 6\n......\n.w....\n......\n..w...\n......\n",
"2 4\n....\n.w..\n",
"2 6\nw..w.w\n...w..\n"
] | [
"..w++w..\n..+..+..\n..+..+..\n..w++w..\n",
"......\n+w+...\n+.+...\n++w...\n......\n",
"....\n.w..\n",
"-1\n"
] | In the first sample the required size of the optimal frame equals 4. In the second sample the size of the optimal frame equals 3. In the third sample, the size of the optimal frame is 1. In the fourth sample, the required frame doesn't exist. | [
{
"input": "4 8\n..w..w..\n........\n........\n..w..w..",
"output": "..w++w..\n..+..+..\n..+..+..\n..w++w.."
},
{
"input": "2 4\n....\n.w..",
"output": "....\n.w.."
},
{
"input": "2 6\nw..w.w\n...w..",
"output": "-1"
},
{
"input": "9 4\n....\n....\n....\n....\n....\n..w.\n....\n....\n.w..",
"output": "....\n....\n....\n....\n....\n++w+\n+..+\n+..+\n+w++"
},
{
"input": "1 1\nw",
"output": "w"
},
{
"input": "2 1\nw\n.",
"output": "w\n."
},
{
"input": "2 1\nw\nw",
"output": "-1"
},
{
"input": "1 2\nww",
"output": "-1"
},
{
"input": "2 2\nww\n..",
"output": "ww\n++"
},
{
"input": "2 2\n.w\n.w",
"output": "+w\n+w"
},
{
"input": "2 2\n..\nww",
"output": "++\nww"
},
{
"input": "2 2\nw.\nw.",
"output": "w+\nw+"
},
{
"input": "2 2\nw.\n.w",
"output": "w+\n+w"
},
{
"input": "2 2\n..\nw.",
"output": "..\nw."
},
{
"input": "3 3\n...\n..w\nw..",
"output": "+++\n+.w\nw++"
},
{
"input": "1 7\nw.....w",
"output": "-1"
},
{
"input": "6 9\n.w.......\n.........\n.........\n.........\n.w.......\n......w..",
"output": ".w+++++..\n.+....+..\n.+....+..\n.+....+..\n.w....+..\n.+++++w.."
},
{
"input": "6 9\n...ww....\n.........\n.........\n.........\n.........\n......w..",
"output": "...ww++++\n...+....+\n...+....+\n...+....+\n...+....+\n...+++w++"
},
{
"input": "6 9\n.......w.\n.........\n.........\n.........\n.........\n......w..",
"output": "..+++++w.\n..+....+.\n..+....+.\n..+....+.\n..+....+.\n..++++w+."
},
{
"input": "5 4\n....\nw...\n...w\n.w..\n..w.",
"output": "-1"
},
{
"input": "5 4\nwwww\nwwww\nwwww\nwwww\nwwww",
"output": "-1"
},
{
"input": "5 4\n..w.\n..ww\n.www\n.w..\nwwww",
"output": "-1"
},
{
"input": "5 4\nwwww\nw..w\nwwww\n.www\n..ww",
"output": "-1"
},
{
"input": "8 16\n................\n................\n................\n................\n............w...\n................\n................\n..............w.",
"output": "................\n................\n................\n................\n............w+++\n............+..+\n............+..+\n............++w+"
},
{
"input": "1 2\n.w",
"output": ".w"
},
{
"input": "2 2\n.w\n..",
"output": ".w\n.."
},
{
"input": "5 2\n..\n.w\nww\n..\n..",
"output": "..\n+w\nww\n..\n.."
},
{
"input": "6 2\nw.\n..\n..\n..\n..\n..",
"output": "w.\n..\n..\n..\n..\n.."
},
{
"input": "3 2\n..\n.w\n..",
"output": "..\n.w\n.."
},
{
"input": "4 2\nw.\n..\n..\n..",
"output": "w.\n..\n..\n.."
},
{
"input": "2 1\n.\nw",
"output": ".\nw"
},
{
"input": "6 1\n.\n.\nw\n.\n.\n.",
"output": ".\n.\nw\n.\n.\n."
},
{
"input": "1 3\n..w",
"output": "..w"
},
{
"input": "4 1\n.\nw\n.\n.",
"output": ".\nw\n.\n."
},
{
"input": "6 2\n..\n.w\n..\n..\n..\n..",
"output": "..\n.w\n..\n..\n..\n.."
},
{
"input": "2 1\nw\n.",
"output": "w\n."
},
{
"input": "5 1\n.\n.\n.\nw\n.",
"output": ".\n.\n.\nw\n."
},
{
"input": "1 5\n....w",
"output": "....w"
},
{
"input": "6 1\nw\n.\n.\n.\n.\n.",
"output": "w\n.\n.\n.\n.\n."
},
{
"input": "2 1\nw\n.",
"output": "w\n."
},
{
"input": "1 3\n.w.",
"output": ".w."
},
{
"input": "4 1\n.\n.\n.\nw",
"output": ".\n.\n.\nw"
},
{
"input": "4 2\n..\nw.\n.w\n..",
"output": "..\nw+\n+w\n.."
},
{
"input": "2 2\n..\nw.",
"output": "..\nw."
},
{
"input": "4 2\n..\n..\nw.\n..",
"output": "..\n..\nw.\n.."
},
{
"input": "1 6\n.....w",
"output": ".....w"
},
{
"input": "3 4\nw...\n..w.\n.ww.",
"output": "w++.\n+.w.\n+ww."
},
{
"input": "5 2\n..\n..\n..\n..\nw.",
"output": "..\n..\n..\n..\nw."
},
{
"input": "2 2\n..\nw.",
"output": "..\nw."
},
{
"input": "2 1\nw\n.",
"output": "w\n."
},
{
"input": "4 1\n.\n.\nw\n.",
"output": ".\n.\nw\n."
},
{
"input": "3 3\n...\n...\n.w.",
"output": "...\n...\n.w."
},
{
"input": "6 1\n.\nw\n.\n.\n.\n.",
"output": ".\nw\n.\n.\n.\n."
},
{
"input": "2 1\n.\nw",
"output": ".\nw"
},
{
"input": "1 3\n..w",
"output": "..w"
},
{
"input": "3 1\n.\n.\nw",
"output": ".\n.\nw"
},
{
"input": "6 1\n.\n.\n.\n.\n.\nw",
"output": ".\n.\n.\n.\n.\nw"
},
{
"input": "6 3\n...\n...\n...\n...\n...\n.w.",
"output": "...\n...\n...\n...\n...\n.w."
}
] | 1,465 | 35,123,200 | 3 | 132,299 |
|
615 | Hexagons | [
"binary search",
"implementation",
"math"
] | null | null | Ayrat is looking for the perfect code. He decided to start his search from an infinite field tiled by hexagons. For convenience the coordinate system is introduced, take a look at the picture to see how the coordinates of hexagon are defined:
Ayrat is searching through the field. He started at point (0,<=0) and is moving along the spiral (see second picture). Sometimes he forgets where he is now. Help Ayrat determine his location after *n* moves. | The only line of the input contains integer *n* (0<=β€<=*n*<=β€<=1018)Β β the number of Ayrat's moves. | Print two integers *x* and *y*Β β current coordinates of Ayrat coordinates. | [
"3\n",
"7\n"
] | [
"-2 0\n",
"3 2\n"
] | none | [
{
"input": "3",
"output": "-2 0"
},
{
"input": "7",
"output": "3 2"
},
{
"input": "39",
"output": "5 6"
},
{
"input": "14",
"output": "-2 -4"
},
{
"input": "94",
"output": "8 8"
},
{
"input": "60",
"output": "8 0"
},
{
"input": "60",
"output": "8 0"
},
{
"input": "59",
"output": "7 -2"
},
{
"input": "181994",
"output": "154 -492"
},
{
"input": "486639",
"output": "-33 806"
},
{
"input": "34514",
"output": "13 -214"
},
{
"input": "826594",
"output": "-769 562"
},
{
"input": "1000000000000000000",
"output": "-418284973 -1154700538"
},
{
"input": "854460",
"output": "414 1068"
},
{
"input": "164960",
"output": "458 -20"
},
{
"input": "618459",
"output": "-797 -222"
},
{
"input": "496181994",
"output": "21108 9228"
},
{
"input": "1000000000",
"output": "27596 -17836"
},
{
"input": "228939226",
"output": "1516 17472"
},
{
"input": "973034514",
"output": "27776 16488"
},
{
"input": "984826594",
"output": "22704 -27064"
},
{
"input": "19164960",
"output": "4864 384"
},
{
"input": "249781780",
"output": "2815 18250"
},
{
"input": "851838979",
"output": "8695 33702"
},
{
"input": "978618459",
"output": "-15591 -36122"
},
{
"input": "871854460",
"output": "31404 5384"
},
{
"input": "302486639",
"output": "11555 -17054"
},
{
"input": "0",
"output": "0 0"
},
{
"input": "1",
"output": "1 2"
},
{
"input": "2",
"output": "-1 2"
},
{
"input": "3",
"output": "-2 0"
},
{
"input": "4",
"output": "-1 -2"
},
{
"input": "5",
"output": "1 -2"
},
{
"input": "6",
"output": "2 0"
},
{
"input": "7",
"output": "3 2"
},
{
"input": "8",
"output": "2 4"
},
{
"input": "9",
"output": "0 4"
},
{
"input": "10",
"output": "-2 4"
},
{
"input": "11",
"output": "-3 2"
},
{
"input": "12",
"output": "-4 0"
},
{
"input": "13",
"output": "-3 -2"
},
{
"input": "14",
"output": "-2 -4"
},
{
"input": "15",
"output": "0 -4"
},
{
"input": "16",
"output": "2 -4"
},
{
"input": "17",
"output": "3 -2"
},
{
"input": "18",
"output": "4 0"
},
{
"input": "19",
"output": "5 2"
},
{
"input": "20",
"output": "4 4"
},
{
"input": "21",
"output": "3 6"
},
{
"input": "22",
"output": "1 6"
},
{
"input": "23",
"output": "-1 6"
},
{
"input": "24",
"output": "-3 6"
},
{
"input": "25",
"output": "-4 4"
},
{
"input": "26",
"output": "-5 2"
},
{
"input": "27",
"output": "-6 0"
},
{
"input": "28",
"output": "-5 -2"
},
{
"input": "29",
"output": "-4 -4"
},
{
"input": "30",
"output": "-3 -6"
},
{
"input": "257947185131120683",
"output": "-53995102 -586455096"
},
{
"input": "258773432604171403",
"output": "-438664202 297458800"
},
{
"input": "259599671487287531",
"output": "-252460838 -588330600"
},
{
"input": "260425914665370955",
"output": "-423141322 332249584"
},
{
"input": "261252157843454379",
"output": "-164822562 -590200144"
},
{
"input": "262078401021537803",
"output": "439863347 302538706"
},
{
"input": "262904639904653932",
"output": "-378326148 -427475264"
},
{
"input": "263730878787770060",
"output": "200309780 592993400"
},
{
"input": "264557126260820780",
"output": "489196540 209450068"
},
{
"input": "775736713043603670",
"output": "-794841963 -444342246"
},
{
"input": "776562956221687094",
"output": "-623135314 -788838484"
},
{
"input": "777389199399770518",
"output": "-328249537 -1018095738"
},
{
"input": "778215438282886646",
"output": "-719067659 -599137942"
},
{
"input": "779041681460970070",
"output": "-637165825 764022826"
},
{
"input": "779867924639053494",
"output": "559082192 -921270732"
},
{
"input": "780694167817136918",
"output": "7343027 1020257594"
},
{
"input": "781520406700253046",
"output": "-707743686 626107308"
},
{
"input": "782346645583369174",
"output": "797020774 -448632052"
},
{
"input": "783172893056419894",
"output": "604133660 -835484644"
},
{
"input": "294352484134170081",
"output": "-264428508 -626474244"
},
{
"input": "34761473798667069",
"output": "-107643660 215287324"
},
{
"input": "247761054921329978",
"output": "-287379568 574759144"
},
{
"input": "88904985049714519",
"output": "344296355 2"
},
{
"input": "64695994584418558",
"output": "146851396 293702780"
},
{
"input": "2999472947040002",
"output": "31620002 63239992"
},
{
"input": "134013960807648841",
"output": "-422711816 4"
},
{
"input": "27719767248080188",
"output": "-96124517 -192249026"
},
{
"input": "228296921967681448",
"output": "-275860421 551720850"
},
{
"input": "622704061396296670",
"output": "-911192665 10"
},
{
"input": "382830415035226081",
"output": "357225613 714451226"
},
{
"input": "175683606088259879",
"output": "-483988434 8"
},
{
"input": "533568904697339792",
"output": "-421730125 843460258"
},
{
"input": "281824423976299408",
"output": "-306498737 -612997466"
},
{
"input": "237223610332609448",
"output": "-281201952 -562403896"
},
{
"input": "82638676376847406",
"output": "-331941110 4"
},
{
"input": "358538881902627465",
"output": "-691412929 6"
},
{
"input": "1941943667672759",
"output": "-25442382 -50884744"
},
{
"input": "504819148029580024",
"output": "820421960 -4"
},
{
"input": "24271330411219667",
"output": "179893783 -2"
},
{
"input": "108364135632524999",
"output": "-380112498 8"
},
{
"input": "16796277375911920",
"output": "74824856 -149649712"
},
{
"input": "194403552286884865",
"output": "-509121532 4"
},
{
"input": "565840809656836956",
"output": "868593352 0"
},
{
"input": "39010293491965817",
"output": "-114032591 -228065170"
},
{
"input": "746407891412272132",
"output": "498801191 -997602386"
},
{
"input": "95626493228268863",
"output": "178537107 357074206"
},
{
"input": "385078658398478614",
"output": "358273010 -716546028"
},
{
"input": "177207687885798058",
"output": "486083238 -4"
},
{
"input": "536222521732590352",
"output": "-422777531 845555062"
},
{
"input": "1571429132955632",
"output": "45773778 4"
},
{
"input": "498549006180463098",
"output": "407655496 -815310984"
},
{
"input": "438594547809157461",
"output": "382358709 -764717418"
},
{
"input": "214071008058709620",
"output": "534254630 0"
},
{
"input": "599060227806517999",
"output": "-446863220 893726452"
},
{
"input": "329939015655396840",
"output": "-331631832 663263664"
},
{
"input": "281523482448806534",
"output": "306335045 612670094"
},
{
"input": "109561818187625921",
"output": "191103653 382207306"
},
{
"input": "412565943716413781",
"output": "370839563 741679126"
},
{
"input": "196006607922989510",
"output": "-255608161 511216338"
},
{
"input": "379604878823574823",
"output": "-355717526 711435056"
},
{
"input": "173500741457825598",
"output": "240486136 480972264"
},
{
"input": "138919367769131398",
"output": "-430378693 10"
},
{
"input": "29974778103430162",
"output": "99957958 199915904"
},
{
"input": "234685974076220810",
"output": "-279693865 559387730"
},
{
"input": "633227154929081648",
"output": "-459429777 -918859546"
},
{
"input": "58101264340386100",
"output": "-139165682 278331372"
},
{
"input": "1718550904886625",
"output": "23934291 -47868582"
},
{
"input": "124444652733481603",
"output": "203670197 -407340402"
},
{
"input": "441000740540275741",
"output": "-383406115 -766812218"
},
{
"input": "545168342596476149",
"output": "852579099 -2"
},
{
"input": "138919367769131403",
"output": "-430378698 0"
},
{
"input": "138919367984320752",
"output": "-215189349 -430378698"
},
{
"input": "1",
"output": "1 2"
},
{
"input": "2",
"output": "-1 2"
},
{
"input": "4",
"output": "-1 -2"
},
{
"input": "5",
"output": "1 -2"
},
{
"input": "6",
"output": "2 0"
}
] | 15 | 0 | 0 | 133,023 |
|
156 | Mrs. Hudson's Pancakes | [
"brute force",
"dp"
] | null | null | Mrs. Hudson hasn't made her famous pancakes for quite a while and finally she decided to make them again. She has learned *m* new recipes recently and she can't wait to try them. Those recipes are based on *n* special spices. Mrs. Hudson has these spices in the kitchen lying in jars numbered with integers from 0 to *n*<=-<=1 (each spice lies in an individual jar). Each jar also has the price of the corresponding spice inscribed β some integer *a**i*.
We know three values for the *i*-th pancake recipe: *d**i*, *s**i*, *c**i*. Here *d**i* and *c**i* are integers, and *s**i* is the pattern of some integer written in the numeral system with radix *d**i*. The pattern contains digits, Latin letters (to denote digits larger than nine) and question marks. Number *x* in the *d**i*-base numeral system matches the pattern *s**i*, if we can replace question marks in the pattern with digits and letters so that we obtain number *x* (leading zeroes aren't taken into consideration when performing the comparison). More formally: each question mark should be replaced by exactly one digit or exactly one letter. If after we replace all question marks we get a number with leading zeroes, we can delete these zeroes. For example, number 40A9875 in the 11-base numeral system matches the pattern "??4??987?", and number 4A9875 does not.
To make the pancakes by the *i*-th recipe, Mrs. Hudson should take all jars with numbers whose representation in the *d**i*-base numeral system matches the pattern *s**i*. The control number of the recipe (*z**i*) is defined as the sum of number *c**i* and the product of prices of all taken jars. More formally: (where *j* is all such numbers whose representation in the *d**i*-base numeral system matches the pattern *s**i*).
Mrs. Hudson isn't as interested in the control numbers as she is in their minimum prime divisors. Your task is: for each recipe *i* find the minimum prime divisor of number *z**i*. If this divisor exceeds 100, then you do not have to find it, print -1. | The first line contains the single integer *n* (1<=β€<=*n*<=β€<=104). The second line contains space-separated prices of the spices *a*0,<=*a*1,<=...,<=*a**n*<=-<=1, where *a**i* is an integer (1<=β€<=*a**i*<=β€<=1018).
The third line contains the single integer *m* (1<=β€<=*m*<=β€<=3Β·104) β the number of recipes Mrs. Hudson has learned.
Next *m* lines describe the recipes, one per line. First you are given an integer *d**i*, written in the decimal numeral system (2<=β€<=*d**i*<=β€<=16). Then after a space follows the *s**i* pattern β a string from 1 to 30 in length, inclusive, consisting of digits from "0" to "9", letters from "A" to "F" and signs "?". Letters from "A" to "F" should be considered as digits from 10 to 15 correspondingly. It is guaranteed that all digits of the pattern (including the digits that are represented by letters) are strictly less than *d**i*. Then after a space follows an integer *c**i*, written in the decimal numeral system (1<=β€<=*c**i*<=β€<=1018).
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++, in is preferred to use cin, cout, strings or the %I64d specificator instead. | For each recipe count by what minimum prime number the control number is divided and print this prime number on the single line. If this number turns out larger than 100, print -1. | [
"1\n1\n1\n2 ? 1\n",
"4\n2 3 5 7\n4\n2 ?0 11\n2 ?1 13\n2 0? 17\n2 1? 19\n",
"1\n1000000000000000000\n1\n16 ?????????????? 1\n"
] | [
"2\n",
"3\n2\n23\n2\n",
"-1\n"
] | In the first test any one-digit number in the binary system matches. The jar is only one and its price is equal to 1, the number *c* is also equal to 1, the control number equals 2. The minimal prime divisor of 2 is 2.
In the second test there are 4 jars with numbers from 0 to 3, and the prices are equal 2, 3, 5 and 7 correspondingly β the first four prime numbers. In all recipes numbers should be two-digit. In the first recipe the second digit always is 0, in the second recipe the second digit always is 1, in the third recipe the first digit must be 0, in the fourth recipe the first digit always is 1. Consequently, the control numbers ββare as follows: in the first recipe 2βΓβ5β+β11β=β21 (the minimum prime divisor is 3), in the second recipe 3βΓβ7β+β13β=β44 (the minimum prime divisor is 2), in the third recipe 2βΓβ3β+β17β=β23 (the minimum prime divisor is 23) and, finally, in the fourth recipe 5βΓβ7β+β19β=β54 (the minimum prime divisor is 2).
In the third test, the number should consist of fourteen digits and be recorded in a sixteen-base numeral system. Number 0 (the number of the single bottles) matches, the control number will be equal to 10<sup class="upper-index">18</sup>β+β1. The minimum prime divisor of this number is equal to 101 and you should print -1. | [] | 60 | 0 | 0 | 133,342 |
|
86 | Genetic engineering | [
"dp",
"string suffix structures",
"trees"
] | C. Genetic engineering | 2 | 256 | "Multidimensional spaces are completely out of style these days, unlike genetics problems" β thought physicist Woll and changed his subject of study to bioinformatics. Analysing results of sequencing he faced the following problem concerning DNA sequences. We will further think of a DNA sequence as an arbitrary string of uppercase letters "A", "C", "G" and "T" (of course, this is a simplified interpretation).
Let *w* be a long DNA sequence and *s*1,<=*s*2,<=...,<=*s**m* β collection of short DNA sequences. Let us say that the collection filters *w* iff *w* can be covered with the sequences from the collection. Certainly, substrings corresponding to the different positions of the string may intersect or even cover each other. More formally: denote by |*w*| the length of *w*, let symbols of *w* be numbered from 1 to |*w*|. Then for each position *i* in *w* there exist pair of indices *l*,<=*r* (1<=β€<=*l*<=β€<=*i*<=β€<=*r*<=β€<=|*w*|) such that the substring *w*[*l*Β ...Β *r*] equals one of the elements *s*1,<=*s*2,<=...,<=*s**m* of the collection.
Woll wants to calculate the number of DNA sequences of a given length filtered by a given collection, but he doesn't know how to deal with it. Help him! Your task is to find the number of different DNA sequences of length *n* filtered by the collection {*s**i*}.
Answer may appear very large, so output it modulo 1000000009. | First line contains two integer numbers *n* and *m* (1<=β€<=*n*<=β€<=1000,<=1<=β€<=*m*<=β€<=10) β the length of the string and the number of sequences in the collection correspondently.
Next *m* lines contain the collection sequences *s**i*, one per line. Each *s**i* is a nonempty string of length not greater than 10. All the strings consist of uppercase letters "A", "C", "G", "T". The collection may contain identical strings. | Output should contain a single integer β the number of strings filtered by the collection modulo 1000000009 (109<=+<=9). | [
"2 1\nA\n",
"6 2\nCAT\nTACT\n"
] | [
"1\n",
"2\n"
] | In the first sample, a string has to be filtered by "A". Clearly, there is only one such string: "AA".
In the second sample, there exist exactly two different strings satisfying the condition (see the pictures below). | [] | 60 | 0 | 0 | 133,567 |
938 | Shortest Path Queries | [
"bitmasks",
"data structures",
"dsu",
"graphs"
] | null | null | You are given an undirected connected graph with weighted edges. The length of some path between two vertices is the bitwise xor of weights of all edges belonging to this path (if some edge is traversed more than once, then it is included in bitwise xor the same number of times).
There are three types of queries you have to process:
- 1 *x* *y* *d* β add an edge connecting vertex *x* to vertex *y* with weight *d*. It is guaranteed that there is no edge connecting *x* to *y* before this query; - 2 *x* *y* β remove an edge connecting vertex *x* to vertex *y*. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query; - 3 *x* *y* β calculate the length of the shortest path (possibly non-simple) from vertex *x* to vertex *y*.
Print the answers for all queries of type 3. | The first line contains two numbers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=200000) β the number of vertices and the number of edges in the graph, respectively.
Then *m* lines follow denoting the edges of the graph. Each line contains three integers *x*, *y* and *d* (1<=β€<=*x*<=<<=*y*<=β€<=*n*, 0<=β€<=*d*<=β€<=230<=-<=1). Each pair (*x*,<=*y*) is listed at most once. The initial graph is connected.
Then one line follows, containing an integer *q* (1<=β€<=*q*<=β€<=200000) β the number of queries you have to process.
Then *q* lines follow, denoting queries in the following form:
- 1 *x* *y* *d* (1<=β€<=*x*<=<<=*y*<=β€<=*n*, 0<=β€<=*d*<=β€<=230<=-<=1) β add an edge connecting vertex *x* to vertex *y* with weight *d*. It is guaranteed that there is no edge connecting *x* to *y* before this query; - 2 *x* *y* (1<=β€<=*x*<=<<=*y*<=β€<=*n*) β remove an edge connecting vertex *x* to vertex *y*. It is guaranteed that there was such edge in the graph, and the graph stays connected after this query; - 3 *x* *y* (1<=β€<=*x*<=<<=*y*<=β€<=*n*) β calculate the length of the shortest path (possibly non-simple) from vertex *x* to vertex *y*.
It is guaranteed that at least one query has type 3. | Print the answers for all queries of type 3 in the order they appear in input. | [
"5 5\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 5 1\n5\n3 1 5\n1 1 3 1\n3 1 5\n2 1 5\n3 1 5\n"
] | [
"1\n1\n2\n"
] | none | [
{
"input": "5 5\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 5 1\n5\n3 1 5\n1 1 3 1\n3 1 5\n2 1 5\n3 1 5",
"output": "1\n1\n2"
}
] | 46 | 0 | 0 | 133,975 |
|
737 | Tanya is 5! | [
"graph matchings",
"graphs",
"greedy",
"schedules"
] | null | null | Tanya is now five so all her friends gathered together to celebrate her birthday. There are *n* children on the celebration, including Tanya.
The celebration is close to its end, and the last planned attraction is gaming machines. There are *m* machines in the hall, they are numbered 1 through *m*. Each of the children has a list of machines he wants to play on. Moreover, for each of the machines he knows the exact time he wants to play on it. For every machine, no more than one child can play on this machine at the same time.
It is evening already, so every adult wants to go home. To speed up the process, you can additionally rent second copies of each of the machines. To rent the second copy of the *j*-th machine, you have to pay *p**j* burles. After you rent a machine, you can use it for as long as you want.
How long it will take to make every child play according to his plan, if you have a budget of *b* burles for renting additional machines? There is only one copy of each machine, so it's impossible to rent a third machine of the same type.
The children can interrupt the game in any moment and continue it later. If the *i*-th child wants to play on the *j*-th machine, it is allowed after you rent the copy of the *j*-th machine that this child would play some part of the time on the *j*-th machine and some part of the time on its copy (each of these parts could be empty). The interruptions and changes take no time and can be performed in any integer moment of time. Of course, a child can't play on more than one machine at the same time.
Remember, that it is not needed to save money (no one saves money at the expense of children happiness!), it is needed to minimize the latest moment of time some child ends his game. | The first line contains three integers *n*, *m* and *b* (1<=β€<=*n*<=β€<=40, 1<=β€<=*m*<=β€<=10, 0<=β€<=*b*<=β€<=106)Β β the number of children, the number of gaming machines and the budget for renting additional machines.
The second line contains *m* integers *p*1,<=*p*2,<=...,<=*p**m* (1<=β€<=*p**j*<=β€<=106), where *p**j* is the rent price for the second copy of the *j*-th machine.
*n* lines follow, *i*-th of them describes the wishes of the *i*-th child. The line starts with an integer *k**i* (0<=β€<=*k**i*<=β€<=*m*)Β β the number of machines, the *i*-th child wants to play on. Then there are *k**i* pairs in the line, the *y*-th of them is *x**iy*, *t**iy*. It means that, the *i*-th child wants to play *t**iy* (1<=β€<=*t**iy*<=β€<=2500) minutes on the *x**iy*-th (1<=β€<=*x**iy*<=β€<=*m*) machine. In each of these *n* lines the values *x**iy* are distinct. | In the first line print the minimum time in which all the children can finish their games.
In the second line print a string of length *m* consisting of zeros and ones. The *j*-th character is '1', if the copy of *j*-th machine should be rated, and '0' otherwise.
In the third line print integer *g* (0<=β€<=*g*<=β€<=106)Β β the total number of time segments of continuous playing for all of the children. Then in *g* lines print the segments as four integers *i*, *j*, *s*, *d*, meaning that the *i*-th child was playing on the *j*-th machine or its copy from the time moment *s* (*s*<=β₯<=0) for *d* minutes (*d*<=β₯<=1). You can print these lines in arbitrary order.
If there are multiple answers, print any of them. | [
"2 2 100\n3 7\n2 1 3 2 1\n2 1 3 2 1\n",
"3 2 15\n11 7\n2 2 10 1 5\n1 2 20\n2 1 4 2 3\n"
] | [
"4\n10\n8\n1 1 0 1\n2 2 0 1\n1 1 1 1\n2 1 1 1\n2 1 2 1\n1 1 2 1\n1 2 3 1\n2 1 3 1\n",
"20\n01\n17\n2 2 0 4\n2 2 4 1\n1 1 5 2\n2 2 5 2\n1 2 7 5\n2 2 7 5\n2 2 12 1\n1 2 12 1\n3 1 13 4\n2 2 13 4\n1 2 13 4\n1 1 17 2\n3 2 17 2\n2 2 17 2\n1 1 19 1\n2 2 19 1\n3 2 19 1\n"
] | none | [] | 30 | 0 | 0 | 134,087 |
|
838 | Convex Countour | [
"dp"
] | null | null | You are given an strictly convex polygon with *n* vertices. It is guaranteed that no three points are collinear. You would like to take a maximum non intersecting path on the polygon vertices that visits each point at most once.
More specifically your path can be represented as some sequence of distinct polygon vertices. Your path is the straight line segments between adjacent vertices in order. These segments are not allowed to touch or intersect each other except at the vertices in your sequence.
Given the polygon, print the maximum length non-intersecting path that visits each point at most once. | The first line of input will contain a single integer *n* (2<=β€<=*n*<=β€<=2<=500), the number of points.
The next *n* lines will contain two integers *x**i*,<=*y**i* (|*x**i*|,<=|*y**i*|<=β€<=109), denoting the coordinates of the *i*-th vertex.
It is guaranteed that these points are listed in clockwise order. | Print a single floating point number, representing the longest non-intersecting path that visits the vertices at most once.
Your answer will be accepted if it has absolute or relative error at most 10<=-<=9. More specifically, if your answer is *a* and the jury answer is *b*, your answer will be accepted if . | [
"4\n0 0\n0 1\n1 1\n1 0\n"
] | [
"3.4142135624\n"
] | One optimal path is to visit points 0,1,3,2 in order. | [] | 0 | 0 | -1 | 134,320 |
|
666 | Forensic Examination | [
"data structures",
"string suffix structures"
] | null | null | The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.
Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure.
All the *m* leaflets that are brought by the spy are numbered from 1 to *m*. After that it's needed to get the answer to *q* queries of the following kind: "In which leaflet in the segment of numbers [*l*,<=*r*] the substring of the Absolutely Inadmissible Swearword [*p**l*,<=*p**r*] occurs more often?".
The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him! | The first line contains the string *s* (1<=β€<=|*s*|<=β€<=5Β·105) β the Absolutely Inadmissible Swearword. The string *s* consists of only lowercase English letters.
The second line contains the only integer *m* (1<=β€<=*m*<=β€<=5Β·104) β the number of texts of leaflets for expertise.
Each of the next *m* lines contains the only string *t**i* β the text of the *i*-th leaflet. The sum of lengths of all leaflet texts doesn't exceed 5Β·104. The text of the leaflets consists of only lowercase English letters.
The next line contains integer *q* (1<=β€<=*q*<=β€<=5Β·105) β the number of queries for expertise.
Finally, each of the last *q* lines contains four integers *l*, *r*, *p**l*, *p**r* (1<=β€<=*l*<=β€<=*r*<=β€<=*m*,<=1<=β€<=*p**l*<=β€<=*p**r*<=β€<=|*s*|), where |*s*| is the length of the Absolutely Inadmissible Swearword. | Print *q* lines. The *i*-th of them should contain two integers β the number of the text with the most occurences and the number of occurences of the substring [*p**l*,<=*p**r*] of the string *s*. If there are several text numbers print the smallest one. | [
"suffixtree\n3\nsuffixtreesareawesome\ncartesiantreeisworsethansegmenttree\nnyeeheeheee\n2\n1 2 1 10\n1 3 9 10\n"
] | [
"1 1\n3 4\n"
] | none | [] | 0 | 0 | -1 | 134,376 |
|
575 | Party | [
"bitmasks",
"brute force",
"graph matchings"
] | null | null | Note the unusual memory limit for the problem.
People working in MDCS (Microsoft Development Center Serbia) like partying. They usually go to night clubs on Friday and Saturday.
There are *N* people working in MDCS and there are *N* clubs in the city. Unfortunately, if there is more than one Microsoft employee in night club, level of coolness goes infinitely high and party is over, so club owners will never let more than one Microsoft employee enter their club in the same week (just to be sure).
You are organizing night life for Microsoft employees and you have statistics about how much every employee likes Friday and Saturday parties for all clubs.
You need to match people with clubs maximizing overall sum of their happiness (they are happy as much as they like the club), while half of people should go clubbing on Friday and the other half on Saturday. | The first line contains integer *N* β number of employees in MDCS.
Then an *N*<=Γ<=*N* matrix follows, where element in *i*-th row and *j*-th column is an integer number that represents how much *i*-th person likes *j*-th clubβs Friday party.
Then another *N*<=Γ<=*N* matrix follows, where element in *i*-th row and *j*-th column is an integer number that represents how much *i*-th person likes *j*-th clubβs Saturday party.
- 2<=β€<=*N*<=β€<=20 - *N* is even - 0<=β€<= level of likeness <=β€<=106 - All values are integers | Output should contain a single integer β maximum sum of happiness possible. | [
"4\n1 2 3 4\n2 3 4 1\n3 4 1 2\n4 1 2 3\n5 8 7 1\n6 9 81 3\n55 78 1 6\n1 1 1 1\n"
] | [
"167\n"
] | Here is how we matched people with clubs:
Friday: 1st person with 4th club (4 happiness) and 4th person with 1st club (4 happiness).
Saturday: 2nd person with 3rd club (81 happiness) and 3rd person with 2nd club (78 happiness).
4+4+81+78 = 167 | [] | 62 | 4,198,400 | 0 | 134,627 |
|
0 | none | [
"none"
] | null | null | Instructors of Some Informatics School make students go to bed.
The house contains *n* rooms, in each room exactly *b* students were supposed to sleep. However, at the time of curfew it happened that many students are not located in their assigned rooms. The rooms are arranged in a row and numbered from 1 to *n*. Initially, in *i*-th room there are *a**i* students. All students are currently somewhere in the house, therefore *a*1<=+<=*a*2<=+<=...<=+<=*a**n*<==<=*nb*. Also 2 instructors live in this house.
The process of curfew enforcement is the following. One instructor starts near room 1 and moves toward room *n*, while the second instructor starts near room *n* and moves toward room 1. After processing current room, each instructor moves on to the next one. Both instructors enter rooms and move simultaneously, if *n* is odd, then only the first instructor processes the middle room. When all rooms are processed, the process ends.
When an instructor processes a room, she counts the number of students in the room, then turns off the light, and locks the room. Also, if the number of students inside the processed room is not equal to *b*, the instructor writes down the number of this room into her notebook (and turns off the light, and locks the room). Instructors are in a hurry (to prepare the study plan for the next day), so they don't care about who is in the room, but only about the number of students.
While instructors are inside the rooms, students can run between rooms that are not locked and not being processed. A student can run by at most *d* rooms, that is she can move to a room with number that differs my at most *d*. Also, after (or instead of) running each student can hide under a bed in a room she is in. In this case the instructor will not count her during the processing. In each room any number of students can hide simultaneously.
Formally, here is what's happening:
- A curfew is announced, at this point in room *i* there are *a**i* students. - Each student can run to another room but not further than *d* rooms away from her initial room, or stay in place. After that each student can optionally hide under a bed. - Instructors enter room 1 and room *n*, they count students there and lock the room (after it no one can enter or leave this room). - Each student from rooms with numbers from 2 to *n*<=-<=1 can run to another room but not further than *d* rooms away from her current room, or stay in place. Each student can optionally hide under a bed. - Instructors move from room 1 to room 2 and from room *n* to room *n*<=-<=1. - This process continues until all rooms are processed.
Let *x*1 denote the number of rooms in which the first instructor counted the number of non-hidden students different from *b*, and *x*2 be the same number for the second instructor. Students know that the principal will only listen to one complaint, therefore they want to minimize the maximum of numbers *x**i*. Help them find this value if they use the optimal strategy. | The first line contains three integers *n*, *d* and *b* (2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*d*<=β€<=*n*<=-<=1, 1<=β€<=*b*<=β€<=10<=000), number of rooms in the house, running distance of a student, official number of students in a room.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=109), *i*-th of which stands for the number of students in the *i*-th room before curfew announcement.
It is guaranteed that *a*1<=+<=*a*2<=+<=...<=+<=*a**n*<==<=*nb*. | Output one integer, the minimal possible value of the maximum of *x**i*. | [
"5 1 1\n1 0 0 0 4\n",
"6 1 2\n3 8 0 1 0 0\n"
] | [
"1\n",
"2\n"
] | In the first sample the first three rooms are processed by the first instructor, and the last two are processed by the second instructor. One of the optimal strategies is the following: firstly three students run from room 5 to room 4, on the next stage two of them run to room 3, and one of those two hides under a bed. This way, the first instructor writes down room 2, and the second writes down nothing.
In the second sample one of the optimal strategies is the following: firstly all students in room 1 hide, all students from room 2 run to room 3. On the next stage one student runs from room 3 to room 4, and 5 students hide. This way, the first instructor writes down rooms 1 and 2, the second instructor writes down rooms 5 and 6. | [] | 15 | 0 | 0 | 134,637 |
|
217 | Alien DNA | [
"data structures",
"dsu",
"trees"
] | null | null | Professor Bajtocy is conducting experiments on alien DNA. He has discovered that it is subject to repetitive mutations β each mutation happens in the same way: some continuous subsequence of the alien DNA becomes active, copies itself, the copy gets mangled and inserts itself right after the original subsequence. The mangled copy of the activated continuous subsequence is formed by first joining all the elements at the even positions in that subsequence, and then joining all the elements at the odd ones at the end. That is, if the activated subsequence consists of 11 elements and represented as *s*1*s*2... *s*11, its mangled copy is *s*2*s*4*s*6*s*8*s*10*s*1*s*3*s*5*s*7*s*9*s*11.
For example, if the original sequence was "ACTGG" and the mutation happened on the segment [2,<=4] (that is the activated subsequence is "CTG"), the mutated DNA is: "ACTGTCGG". The mangled copy of the activated subsequence is marked with bold font.
Professor Bajtocy has written down the original DNA sequence and the mutations that sequentially happened to it, and he now asks you to recover the first *k* elements of the DNA sequence after all the mutations. | The first line of input contains the original DNA sequence, consisting only of letters "A", "C", "T" and "G" and not exceeding 3Β·106 in length.
The second line contains a single integer *k* (1<=β€<=*k*<=β€<=3Β·106).
The third line contains a single integer *n* (0<=β€<=*n*<=β€<=5000) β the number of mutations. The next *n* lines describe the mutations in chronological order β each mutation is described by two numbers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=109), meaning that the continuous subsequence [*l**i*,<=*r**i*] has become active and cloned itself, joining itself with the mangled copy.
It is guaranteed that the input data is correct, that is, no mutation acts on non-existing elements of the DNA sequence, and the resulting DNA sequence has at least *k* elements.
Assume that the DNA elements are indexed starting from 1 and that the notation [*l*,<=*r*] meaning the continuous subsequence of DNA sequence that consists of *r*<=-<=*l*<=+<=1 elements starting at the *l*-th DNA sequence element and ending at the *r*-th DNA sequence element. | Output a single line, containing the first *k* letters of the mutated DNA sequence. | [
"GAGA\n4\n0\n",
"ACGTACGT\n16\n2\n1 2\n2 8\n"
] | [
"GAGA\n",
"ACCAGTACCGACATCG\n"
] | In the second example, after the first mutation the sequence is "ACCAGTACGT". After the second mutation it's "ACCAGTACCGACATCGT". | [] | 60 | 0 | 0 | 134,856 |
|
821 | Okabe and City | [
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | Okabe likes to be able to walk through his city on a path lit by street lamps. That way, he doesn't get beaten up by schoolchildren.
Okabe's city is represented by a 2D grid of cells. Rows are numbered from 1 to *n* from top to bottom, and columns are numbered 1 to *m* from left to right. Exactly *k* cells in the city are lit by a street lamp. It's guaranteed that the top-left cell is lit.
Okabe starts his walk from the top-left cell, and wants to reach the bottom-right cell. Of course, Okabe will only walk on lit cells, and he can only move to adjacent cells in the up, down, left, and right directions. However, Okabe can also temporarily light all the cells in any single row or column at a time if he pays 1 coin, allowing him to walk through some cells not lit initially.
Note that Okabe can only light a single row or column at a time, and has to pay a coin every time he lights a new row or column. To change the row or column that is temporarily lit, he must stand at a cell that is lit initially. Also, once he removes his temporary light from a row or column, all cells in that row/column not initially lit are now not lit.
Help Okabe find the minimum number of coins he needs to pay to complete his walk! | The first line of input contains three space-separated integers *n*, *m*, and *k* (2<=β€<=*n*,<=*m*,<=*k*<=β€<=104).
Each of the next *k* lines contains two space-separated integers *r**i* and *c**i* (1<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*c**i*<=β€<=*m*)Β β the row and the column of the *i*-th lit cell.
It is guaranteed that all *k* lit cells are distinct. It is guaranteed that the top-left cell is lit. | Print the minimum number of coins Okabe needs to pay to complete his walk, or -1 if it's not possible. | [
"4 4 5\n1 1\n2 1\n2 3\n3 3\n4 3\n",
"5 5 4\n1 1\n2 1\n3 1\n3 2\n",
"2 2 4\n1 1\n1 2\n2 1\n2 2\n",
"5 5 4\n1 1\n2 2\n3 3\n4 4\n"
] | [
"2\n",
"-1\n",
"0\n",
"3\n"
] | In the first sample test, Okabe can take the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fd8d0b719801e9cfe8f6be340d76a554f6ffb66b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, paying only when moving to (2,β3) and (4,β4).
In the fourth sample, Okabe can take the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a91fe94734a09a4ba86d66070aa8cbeacdb2279f.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/2ee6688fe94ce8e9a746b8e479e906fe25ff4d86.png" style="max-width: 100.0%;max-height: 100.0%;"/>, paying when moving to (1,β2), (3,β4), and (5,β4). | [
{
"input": "4 4 5\n1 1\n2 1\n2 3\n3 3\n4 3",
"output": "2"
},
{
"input": "5 5 4\n1 1\n2 1\n3 1\n3 2",
"output": "-1"
},
{
"input": "2 2 4\n1 1\n1 2\n2 1\n2 2",
"output": "0"
},
{
"input": "5 5 4\n1 1\n2 2\n3 3\n4 4",
"output": "3"
},
{
"input": "7 10 53\n1 1\n7 5\n2 3\n5 3\n1 9\n2 10\n6 1\n7 6\n2 9\n3 3\n3 4\n5 1\n5 4\n1 10\n4 1\n3 10\n7 7\n7 8\n7 1\n1 8\n2 4\n6 5\n3 5\n6 3\n6 6\n2 8\n3 6\n5 2\n3 8\n3 9\n5 5\n7 2\n3 1\n4 8\n1 3\n4 6\n2 1\n7 4\n1 7\n4 9\n6 8\n4 4\n5 8\n5 9\n2 5\n2 6\n3 2\n1 4\n2 7\n7 9\n4 7\n2 2\n4 5",
"output": "1"
},
{
"input": "10 9 38\n1 1\n4 5\n1 8\n9 5\n3 4\n5 6\n1 7\n10 5\n5 7\n4 7\n4 6\n3 3\n6 6\n4 3\n9 6\n3 2\n4 2\n10 6\n2 2\n1 9\n2 9\n3 7\n3 9\n7 6\n3 8\n6 7\n10 7\n2 1\n6 8\n2 3\n3 1\n6 5\n5 5\n1 3\n5 8\n1 6\n4 9\n5 3",
"output": "2"
},
{
"input": "7 9 7\n1 1\n7 8\n3 2\n5 3\n2 8\n4 8\n3 6",
"output": "2"
},
{
"input": "7 9 48\n1 1\n3 9\n7 4\n7 8\n3 7\n4 9\n6 8\n5 9\n3 8\n5 8\n2 8\n7 3\n7 5\n6 5\n4 7\n7 2\n4 8\n2 7\n4 6\n6 6\n6 3\n6 9\n6 2\n6 4\n7 7\n5 7\n5 2\n5 5\n2 6\n7 1\n2 9\n3 6\n5 1\n3 5\n7 9\n3 4\n1 6\n1 5\n5 4\n7 6\n1 9\n1 4\n6 1\n4 1\n1 3\n1 2\n2 3\n4 5",
"output": "0"
},
{
"input": "6 7 38\n1 1\n3 6\n3 2\n1 4\n5 4\n1 5\n3 3\n3 7\n5 5\n2 3\n6 5\n2 2\n5 3\n6 6\n4 7\n6 3\n2 4\n3 1\n4 1\n5 6\n4 5\n6 7\n2 6\n4 6\n5 2\n1 6\n3 4\n1 7\n4 3\n2 1\n2 7\n1 2\n1 3\n3 5\n5 1\n5 7\n6 4\n2 5",
"output": "0"
},
{
"input": "6 8 43\n1 1\n6 2\n5 1\n1 7\n5 2\n1 8\n6 3\n2 7\n1 6\n2 6\n2 5\n5 3\n3 7\n4 7\n1 5\n4 1\n1 4\n3 5\n3 6\n4 3\n2 4\n3 1\n3 8\n4 6\n4 2\n4 8\n3 4\n1 3\n2 8\n6 1\n6 4\n2 1\n3 3\n5 8\n2 2\n2 3\n6 5\n6 8\n5 7\n1 2\n5 6\n5 4\n5 5",
"output": "0"
},
{
"input": "7 11 72\n1 1\n7 3\n5 3\n6 10\n5 4\n5 5\n4 3\n6 9\n4 2\n7 2\n5 2\n5 1\n6 11\n4 5\n4 6\n3 5\n4 4\n3 6\n6 1\n3 7\n5 11\n2 7\n7 9\n3 4\n3 8\n6 4\n3 3\n3 9\n2 3\n6 5\n6 8\n6 3\n2 6\n3 10\n2 8\n5 6\n6 2\n6 7\n4 9\n5 10\n2 9\n4 8\n1 6\n7 7\n7 5\n5 7\n2 4\n6 6\n2 2\n1 3\n1 7\n4 7\n1 8\n4 1\n1 9\n7 8\n4 11\n1 5\n7 11\n7 6\n1 10\n1 4\n5 9\n3 1\n1 11\n7 1\n4 10\n2 1\n1 2\n5 8\n2 10\n7 10",
"output": "0"
},
{
"input": "6 7 2\n1 1\n6 1",
"output": "2"
},
{
"input": "7 10 24\n1 1\n4 7\n7 1\n5 8\n5 3\n7 4\n5 7\n7 5\n4 8\n6 5\n7 6\n6 4\n5 2\n6 3\n4 2\n6 1\n3 8\n4 1\n6 2\n3 1\n5 5\n2 8\n4 3\n4 6",
"output": "2"
},
{
"input": "9 10 69\n1 1\n6 8\n1 8\n4 4\n2 1\n5 6\n1 4\n6 7\n5 8\n4 8\n7 8\n2 4\n1 3\n8 8\n8 7\n3 8\n8 9\n2 2\n4 9\n7 9\n1 7\n1 9\n4 5\n3 9\n8 10\n5 9\n3 1\n9 9\n7 10\n4 3\n1 10\n3 10\n2 5\n2 8\n4 10\n2 9\n4 1\n2 10\n1 6\n1 5\n5 7\n1 2\n3 4\n6 10\n2 6\n8 6\n4 6\n9 7\n5 5\n3 6\n4 7\n7 7\n6 6\n6 5\n5 4\n3 3\n7 6\n2 7\n9 6\n8 5\n3 7\n4 2\n9 10\n5 2\n9 8\n6 2\n5 10\n5 3\n5 1",
"output": "0"
},
{
"input": "6 11 35\n1 1\n6 9\n5 2\n5 4\n2 8\n3 2\n4 3\n6 2\n3 3\n6 4\n3 1\n2 9\n6 8\n5 5\n2 7\n6 1\n5 9\n4 5\n2 2\n2 6\n1 8\n4 4\n6 3\n2 10\n4 6\n3 4\n6 7\n4 1\n2 1\n5 6\n2 4\n6 10\n3 10\n6 5\n3 8",
"output": "1"
},
{
"input": "6 7 2\n1 1\n3 2",
"output": "-1"
},
{
"input": "5 10 48\n1 1\n3 7\n5 8\n5 2\n3 4\n5 7\n4 2\n4 8\n5 3\n3 6\n5 9\n2 4\n1 4\n4 4\n4 1\n4 3\n5 1\n3 3\n4 6\n3 5\n5 4\n2 5\n5 6\n1 3\n1 2\n2 3\n4 9\n3 8\n2 2\n1 5\n5 10\n1 6\n2 6\n5 5\n4 7\n4 5\n4 10\n3 2\n3 9\n1 7\n2 7\n2 1\n2 9\n3 10\n1 8\n3 1\n2 8\n2 10",
"output": "0"
},
{
"input": "7 7 18\n1 1\n5 4\n3 4\n6 7\n5 6\n7 7\n3 5\n6 6\n6 4\n4 5\n2 4\n2 3\n1 3\n5 7\n6 5\n2 2\n5 5\n3 6",
"output": "1"
},
{
"input": "7 8 50\n1 1\n3 1\n4 5\n2 7\n1 5\n5 8\n4 3\n5 3\n3 5\n5 4\n3 6\n4 6\n6 8\n4 7\n7 8\n7 7\n2 5\n6 4\n1 4\n3 2\n2 8\n5 2\n3 7\n1 3\n4 4\n3 3\n6 5\n6 7\n5 5\n1 8\n5 6\n2 3\n4 1\n7 4\n7 6\n4 8\n2 6\n6 6\n7 5\n1 2\n6 3\n5 1\n4 2\n3 8\n3 4\n7 3\n2 2\n7 2\n7 1\n6 2",
"output": "0"
},
{
"input": "10 7 68\n1 1\n9 2\n8 4\n4 1\n4 5\n2 6\n7 1\n3 6\n5 5\n7 4\n2 5\n6 4\n9 1\n9 4\n2 4\n8 3\n7 3\n3 5\n3 7\n8 2\n5 4\n10 4\n6 1\n2 7\n1 4\n6 3\n7 2\n10 3\n1 5\n4 7\n2 3\n5 3\n1 7\n4 3\n8 5\n5 7\n3 3\n5 6\n6 5\n9 5\n1 6\n5 2\n6 6\n10 1\n7 5\n10 2\n8 1\n1 3\n10 5\n8 6\n3 1\n6 2\n2 1\n3 2\n4 4\n1 2\n5 1\n4 2\n9 3\n6 7\n9 6\n2 2\n3 4\n8 7\n4 6\n10 6\n7 6\n7 7",
"output": "1"
},
{
"input": "9 10 3\n1 1\n6 6\n8 4",
"output": "-1"
},
{
"input": "6 8 30\n1 1\n2 5\n6 6\n3 8\n5 5\n3 2\n1 5\n3 1\n3 3\n5 6\n5 4\n4 4\n6 5\n6 7\n3 4\n4 6\n2 4\n5 3\n1 6\n4 1\n2 6\n5 7\n6 8\n2 8\n5 8\n4 3\n4 7\n3 5\n2 2\n4 8",
"output": "1"
},
{
"input": "9 10 55\n1 1\n3 7\n2 9\n2 4\n3 6\n1 4\n2 7\n2 10\n3 8\n4 8\n1 9\n3 5\n1 8\n1 7\n1 5\n3 10\n1 10\n4 5\n4 4\n3 9\n5 5\n4 7\n4 6\n5 7\n4 10\n1 6\n1 3\n6 7\n2 6\n7 7\n3 4\n4 9\n2 8\n3 3\n5 10\n5 8\n2 5\n7 8\n3 2\n8 8\n2 3\n6 10\n7 9\n2 2\n7 6\n3 1\n6 5\n4 1\n5 9\n6 9\n5 1\n1 2\n8 6\n5 6\n5 2",
"output": "1"
},
{
"input": "6 8 20\n1 1\n5 8\n2 3\n5 1\n6 4\n2 7\n4 4\n6 5\n4 1\n6 3\n2 4\n5 7\n4 5\n2 8\n3 7\n4 6\n6 2\n5 6\n6 7\n4 3",
"output": "2"
},
{
"input": "5 10 5\n1 1\n3 2\n5 8\n3 6\n1 8",
"output": "3"
},
{
"input": "3 4 2\n1 1\n2 1",
"output": "1"
},
{
"input": "5 6 4\n1 1\n2 1\n3 1\n4 1",
"output": "1"
},
{
"input": "5 6 5\n1 1\n1 2\n1 3\n1 4\n1 5",
"output": "1"
},
{
"input": "3 3 2\n1 1\n3 3",
"output": "1"
}
] | 78 | 23,142,400 | -1 | 135,323 |
|
396 | On Iteration of One Well-Known Function | [
"math"
] | null | null | Of course, many of you can calculate Ο(*n*) β the number of positive integers that are less than or equal to *n*, that are coprime with *n*. But what if we need to calculate Ο(Ο(...Ο(*n*))), where function Ο is taken *k* times and *n* is given in the canonical decomposition into prime factors?
You are given *n* and *k*, calculate the value of Ο(Ο(...Ο(*n*))). Print the result in the canonical decomposition into prime factors. | The first line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of distinct prime divisors in the canonical representaion of *n*.
Each of the next *m* lines contains a pair of space-separated integers *p**i*,<=*a**i* (2<=β€<=*p**i*<=β€<=106;Β 1<=β€<=*a**i*<=β€<=1017) β another prime divisor of number *n* and its power in the canonical representation. The sum of all *a**i* doesn't exceed 1017. Prime divisors in the input follow in the strictly increasing order.
The last line contains integer *k* (1<=β€<=*k*<=β€<=1018). | In the first line, print integer *w* β the number of distinct prime divisors of number Ο(Ο(...Ο(*n*))), where function Ο is taken *k* times.
Each of the next *w* lines must contain two space-separated integers *q**i*,<=*b**i* (*b**i*<=β₯<=1) β another prime divisor and its power in the canonical representaion of the result. Numbers *q**i* must go in the strictly increasing order. | [
"1\n7 1\n1\n",
"1\n7 1\n2\n",
"1\n2 100000000000000000\n10000000000000000\n"
] | [
"2\n2 1\n3 1\n",
"1\n2 1\n",
"1\n2 90000000000000000\n"
] | You can read about canonical representation of a positive integer here: http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic.
You can read about function Ο(*n*) here: http://en.wikipedia.org/wiki/Euler's_totient_function. | [] | 30 | 0 | 0 | 135,431 |
|
121 | Lucky Segments | [
"binary search",
"implementation",
"two pointers"
] | null | null | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya has *n* number segments [*l*1;Β *r*1], [*l*2;Β *r*2], ..., [*l**n*;Β *r**n*]. During one move Petya can take any segment (let it be segment number *i*) and replace it with segment [*l**i*<=+<=1;Β *r**i*<=+<=1] or [*l**i*<=-<=1;Β *r**i*<=-<=1]. In other words, during one move Petya can shift any segment to the left or to the right by a unit distance. Petya calls a number full if it belongs to each segment. That is, number *x* is full if for any *i* (1<=β€<=*i*<=β€<=*n*) the condition *l**i*<=β€<=*x*<=β€<=*r**i* is fulfilled.
Petya makes no more than *k* moves. After that he counts the quantity of full lucky numbers. Find the maximal quantity that he can get. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105, 1<=β€<=*k*<=β€<=1018) β the number of segments and the maximum number of moves. Next *n* lines contain pairs of integers *l**i* and *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=1018).
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use the %I64d specificator. | Print on the single line the single number β the answer to the problem. | [
"4 7\n1 4\n6 9\n4 7\n3 5\n",
"2 7\n40 45\n47 74\n"
] | [
"1\n",
"2\n"
] | In the first sample Petya shifts the second segment by two units to the left (it turns into [4;Β 7]), after that number 4 becomes full.
In the second sample Petya shifts the first segment by two units to the right (it turns into [42;Β 47]), and shifts the second segment by three units to the left (it turns into [44;Β 71]), after that numbers 44 and 47 become full. | [] | 93 | 2,150,400 | -1 | 135,532 |
|
0 | none | [
"none"
] | null | null | Polar bears like unique arrays β that is, arrays without repeated elements.
You have got a unique array *s* with length *n* containing non-negative integers. Since you are good friends with Alice and Bob, you decide to split the array in two. Precisely, you need to construct two arrays *a* and *b* that are also of length *n*, with the following conditions for all *i* (1<=β€<=*i*<=β€<=*n*):
- *a**i*,<=*b**i* are non-negative integers; - *s**i*<==<=*a**i*<=+<=*b**i* .
Ideally, *a* and *b* should also be unique arrays. However, life in the Arctic is hard and this is not always possible. Fortunately, Alice and Bob are still happy if their arrays are almost unique. We define an array of length *n* to be almost unique, if and only if it can be turned into a unique array by removing no more than entries.
For example, the array [1,<=2,<=1,<=3,<=2] is almost unique because after removing the first two entries, it becomes [1,<=3,<=2]. The array [1,<=2,<=1,<=3,<=1,<=2] is not almost unique because we need to remove at least 3 entries to turn it into a unique array.
So, your task is to split the given unique array *s* into two almost unique arrays *a* and *b*. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=105).
The second line contains *n* distinct integers *s*1,<=*s*2,<=... *s**n* (0<=β€<=*s**i*<=β€<=109). | If it is possible to make Alice and Bob happy (if you can split the given array), print "YES" (without quotes) in the first line. In the second line, print the array *a*. In the third line, print the array *b*. There may be more than one solution. Any of them will be accepted.
If it is impossible to split *s* into almost unique arrays *a* and *b*, print "NO" (without quotes) in the first line. | [
"6\n12 5 8 3 11 9\n"
] | [
"YES\n6 2 6 0 2 4\n6 3 2 3 9 5"
] | In the sample, we can remove the first two entries from *a* and the second entry from *b* to make them both unique. | [] | 92 | 0 | 0 | 136,056 |
|
0 | none | [
"none"
] | null | null | Stepan is a very experienced olympiad participant. He has *n* cups for Physics olympiads and *m* cups for Informatics olympiads. Each cup is characterized by two parameters β its significance *c**i* and width *w**i*.
Stepan decided to expose some of his cups on a shelf with width *d* in such a way, that:
- there is at least one Physics cup and at least one Informatics cup on the shelf, - the total width of the exposed cups does not exceed *d*, - from each subjects (Physics and Informatics) some of the most significant cups are exposed (i. e. if a cup for some subject with significance *x* is exposed, then all the cups for this subject with significance greater than *x* must be exposed too).
Your task is to determine the maximum possible total significance, which Stepan can get when he exposes cups on the shelf with width *d*, considering all the rules described above. The total significance is the sum of significances of all the exposed cups. | The first line contains three integers *n*, *m* and *d* (1<=β€<=*n*,<=*m*<=β€<=100<=000, 1<=β€<=*d*<=β€<=109) β the number of cups for Physics olympiads, the number of cups for Informatics olympiads and the width of the shelf.
Each of the following *n* lines contains two integers *c**i* and *w**i* (1<=β€<=*c**i*,<=*w**i*<=β€<=109) β significance and width of the *i*-th cup for Physics olympiads.
Each of the following *m* lines contains two integers *c**j* and *w**j* (1<=β€<=*c**j*,<=*w**j*<=β€<=109) β significance and width of the *j*-th cup for Informatics olympiads. | Print the maximum possible total significance, which Stepan can get exposing cups on the shelf with width *d*, considering all the rules described in the statement.
If there is no way to expose cups on the shelf, then print 0. | [
"3 1 8\n4 2\n5 5\n4 2\n3 2\n",
"4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4\n",
"2 2 2\n5 3\n6 3\n4 2\n8 1\n"
] | [
"8\n",
"11\n",
"0\n"
] | In the first example Stepan has only one Informatics cup which must be exposed on the shelf. Its significance equals 3 and width equals 2, so after Stepan exposes it, the width of free space on the shelf becomes equal to 6. Also, Stepan must expose the second Physics cup (which has width 5), because it is the most significant cup for Physics (its significance equals 5). After that Stepan can not expose more cups on the shelf, because there is no enough free space. Thus, the maximum total significance of exposed cups equals to 8. | [
{
"input": "3 1 8\n4 2\n5 5\n4 2\n3 2",
"output": "8"
},
{
"input": "4 3 12\n3 4\n2 4\n3 5\n3 4\n3 5\n5 2\n3 4",
"output": "11"
},
{
"input": "2 2 2\n5 3\n6 3\n4 2\n8 1",
"output": "0"
},
{
"input": "10 10 229\n15 17\n5 4\n4 15\n4 17\n15 11\n7 6\n5 19\n14 8\n4 1\n10 12\n20 13\n20 14\n16 13\n7 15\n2 16\n11 11\n19 20\n6 7\n4 11\n14 16",
"output": "198"
},
{
"input": "10 20 498\n40 12\n23 25\n20 9\n8 1\n23 8\n31 24\n33 2\n22 33\n4 13\n25 20\n40 5\n27 5\n17 6\n8 5\n4 19\n33 23\n30 19\n27 12\n13 22\n16 32\n28 36\n20 18\n36 38\n9 24\n21 35\n20 9\n33 29\n29 33\n18 25\n11 8",
"output": "644"
},
{
"input": "20 10 761\n42 41\n47 7\n35 6\n22 40\n15 2\n47 28\n46 47\n3 45\n12 19\n44 41\n46 2\n49 23\n9 8\n7 41\n5 3\n16 42\n12 50\n17 22\n25 9\n45 12\n41 44\n34 47\n33 35\n32 47\n49 6\n27 18\n43 36\n23 6\n39 22\n38 45",
"output": "900"
},
{
"input": "1 1 1000000000\n4 500000000\n6 500000000",
"output": "10"
},
{
"input": "4 2 8\n1000000000 2\n1000000000 2\n1000000000 2\n1000000000 2\n1000000000 2\n1000000000 2",
"output": "4000000000"
},
{
"input": "1 1 1000000000\n1 1000000000\n1 1000000000",
"output": "0"
},
{
"input": "1 1 1\n1 1\n1 1",
"output": "0"
}
] | 46 | 0 | 0 | 136,201 |
|
802 | Marmots (easy) | [
"math"
] | null | null | Heidi is a statistician to the core, and she likes to study the evolution of marmot populations in each of *V* (1<=β€<=*V*<=β€<=100) villages! So it comes that every spring, when Heidi sees the first snowdrops sprout in the meadows around her barn, she impatiently dons her snowshoes and sets out to the Alps, to welcome her friends the marmots to a new season of thrilling adventures.
Arriving in a village, Heidi asks each and every marmot she comes across for the number of inhabitants of that village. This year, the marmots decide to play an April Fools' joke on Heidi. Instead of consistently providing the exact number of inhabitants *P* (10<=β€<=*P*<=β€<=1000) of the village, they respond with a random non-negative integer *k*, drawn from one of two types of probability distributions:
- Poisson (d'avril) distribution: the probability of getting an answer *k* is for *k*<==<=0,<=1,<=2,<=3,<=..., - Uniform distribution: the probability of getting an answer *k* is for *k*<==<=0,<=1,<=2,<=...,<=2*P*.
Heidi collects exactly 250 answers per village. Every village follows either the Poisson or the uniform distribution. Heidi cannot tell marmots apart, so she may query some marmots several times, and each time the marmot will answer with a new number drawn from the village's distribution.
Can you help Heidi to find out whether a village follows a Poisson or a uniform distribution? | The first line of input will contain the number of villages *V* (1<=β€<=*V*<=β€<=100). The following *V* lines each describe one village. The description of each village consists of 250 space-separated integers *k*, drawn from one of the above distributions. | Output one line per village, in the same order as provided in the input. The village's line shall state poisson if the village's distribution is of the Poisson type, and uniform if the answer came from a uniform distribution. | [
"2\n92 100 99 109 93 105 103 106 101 99 ... (input is truncated)\n28 180 147 53 84 80 180 85 8 16 ... (input is truncated)"
] | [
"poisson\nuniform\n"
] | The full example input is visually represented below, along with the probability distribution function it was drawn from (the *y*-axis is labeled by its values multiplied by 250).
<img class="tex-graphics" src="https://espresso.codeforces.com/77563a6378b39d03eb21012c835c6e96df776b81.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 93 | 0 | -1 | 136,282 |
|
494 | Birthday | [
"data structures",
"dfs and similar",
"dp",
"trees"
] | null | null | Ali is Hamed's little brother and tomorrow is his birthday. Hamed wants his brother to earn his gift so he gave him a hard programming problem and told him if he can successfully solve it, he'll get him a brand new laptop. Ali is not yet a very talented programmer like Hamed and although he usually doesn't cheat but this time is an exception. It's about a brand new laptop. So he decided to secretly seek help from you. Please solve this problem for Ali.
An *n*-vertex weighted rooted tree is given. Vertex number 1 is a root of the tree. We define *d*(*u*,<=*v*) as the sum of edges weights on the shortest path between vertices *u* and *v*. Specifically we define *d*(*u*,<=*u*)<==<=0. Also let's define *S*(*v*) for each vertex *v* as a set containing all vertices *u* such that *d*(1,<=*u*)<==<=*d*(1,<=*v*)<=+<=*d*(*v*,<=*u*). Function *f*(*u*,<=*v*) is then defined using the following formula:
The goal is to calculate *f*(*u*,<=*v*) for each of the *q* given pair of vertices. As the answer can be rather large it's enough to print it modulo 109<=+<=7. | In the first line of input an integer *n* (1<=β€<=*n*<=β€<=105), number of vertices of the tree is given.
In each of the next *n*<=-<=1 lines three space-separated integers *a**i*,<=*b**i*,<=*c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, 1<=β€<=*c**i*<=β€<=109) are given indicating an edge between *a**i* and *b**i* with weight equal to *c**i*.
In the next line an integer *q* (1<=β€<=*q*<=β€<=105), number of vertex pairs, is given.
In each of the next *q* lines two space-separated integers *u**i*,<=*v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*) are given meaning that you must calculate *f*(*u**i*,<=*v**i*).
It is guaranteed that the given edges form a tree. | Output *q* lines. In the *i*-th line print the value of *f*(*u**i*,<=*v**i*) modulo 109<=+<=7. | [
"5\n1 2 1\n4 3 1\n3 5 1\n1 3 1\n5\n1 1\n1 5\n2 4\n2 1\n3 5\n",
"8\n1 2 100\n1 3 20\n2 4 2\n2 5 1\n3 6 1\n3 7 2\n6 8 5\n6\n1 8\n2 3\n5 8\n2 6\n4 7\n6 1\n"
] | [
"10\n1000000005\n1000000002\n23\n1000000002\n",
"999968753\n49796\n999961271\n999991235\n999958569\n45130\n"
] | none | [] | 46 | 0 | 0 | 136,992 |
|
538 | Summer Dichotomy | [
"2-sat",
"data structures",
"dfs and similar",
"greedy"
] | null | null | *T* students applied into the ZPP class of Summer Irrelevant School. The organizing committee of the school may enroll any number of them, but at least *t* students must be enrolled. The enrolled students should be divided into two groups in any manner (it is possible that one of the groups will be empty!)
During a shift the students from the ZPP grade are tutored by *n* teachers. Due to the nature of the educational process, each of the teachers should be assigned to exactly one of two groups (it is possible that no teacher will be assigned to some of the groups!). The *i*-th teacher is willing to work in a group as long as the group will have at least *l**i* and at most *r**i* students (otherwise it would be either too boring or too hard). Besides, some pairs of the teachers don't like each other other and therefore can not work in the same group; in total there are *m* pairs of conflicting teachers.
You, as the head teacher of Summer Irrelevant School, have got a difficult task: to determine how many students to enroll in each of the groups and in which group each teacher will teach. | The first line contains two space-separated integers, *t* and *T* (1<=β€<=*t*<=β€<=*T*<=β€<=109).
The second line contains two space-separated integers *n* and *m* (1<=β€<=*n*<=β€<=105, 0<=β€<=*m*<=β€<=105).
The *i*-th of the next *n* lines contain integers *l**i* and *r**i* (0<=β€<=*l**i*<=β€<=*r**i*<=β€<=109).
The next *m* lines describe the pairs of conflicting teachers. Each of these lines contain two space-separated integers β the indices of teachers in the pair. The teachers are indexed starting from one. It is guaranteed that no teacher has a conflict with himself and no pair of conflicting teachers occurs in the list more than once. | If the distribution is possible, print in the first line a single word 'POSSIBLE' (without the quotes). In the second line print two space-separated integers *n*1 and *n*2Β β the number of students in the first and second group, correspondingly, the contstraint *t*<=β€<=*n*1<=+<=*n*2<=β€<=*T* should be met. In the third line print *n* characters, the *i*-th of which should be 1 or 2, if the *i*-th teacher should be assigned to the first or second group, correspondingly. If there are multiple possible distributions of students and teachers in groups, you can print any of them.
If the sought distribution doesn't exist, print a single word 'IMPOSSIBLE' (without the quotes). | [
"10 20\n3 0\n3 6\n4 9\n16 25\n",
"1 10\n3 3\n0 10\n0 10\n0 10\n1 2\n1 3\n2 3\n"
] | [
"POSSIBLE\n4 16\n112\n",
"IMPOSSIBLE\n"
] | none | [] | 30 | 0 | 0 | 137,809 |
|
822 | My pretty girl Noora | [
"brute force",
"dp",
"greedy",
"math",
"number theory"
] | null | null | In Pavlopolis University where Noora studies it was decided to hold beauty contest "Miss Pavlopolis University". Let's describe the process of choosing the most beautiful girl in the university in more detail.
The contest is held in several stages. Suppose that exactly *n* girls participate in the competition initially. All the participants are divided into equal groups, *x* participants in each group. Furthermore the number *x* is chosen arbitrarily, i. e. on every stage number *x* can be different. Within each group the jury of the contest compares beauty of the girls in the format "each with each". In this way, if group consists of *x* girls, then comparisons occur. Then, from each group, the most beautiful participant is selected. Selected girls enter the next stage of the competition. Thus if *n* girls were divided into groups, *x* participants in each group, then exactly participants will enter the next stage. The contest continues until there is exactly one girl left who will be "Miss Pavlopolis University"
But for the jury this contest is a very tedious task. They would like to divide the girls into groups in each stage so that the total number of pairwise comparisons of the girls is as few as possible. Let *f*(*n*) be the minimal total number of comparisons that should be made to select the most beautiful participant, if we admit *n* girls to the first stage.
The organizers of the competition are insane. They give Noora three integers *t*, *l* and *r* and ask the poor girl to calculate the value of the following expression: *t*0Β·*f*(*l*)<=+<=*t*1Β·*f*(*l*<=+<=1)<=+<=...<=+<=*t**r*<=-<=*l*Β·*f*(*r*). However, since the value of this expression can be quite large the organizers ask her to calculate it modulo 109<=+<=7. If Noora can calculate the value of this expression the organizers promise her to help during the beauty contest. But the poor girl is not strong in mathematics, so she turned for help to Leha and he turned to you. | The first and single line contains three integers *t*, *l* and *r* (1<=β€<=*t*<=<<=109<=+<=7,<=2<=β€<=*l*<=β€<=*r*<=β€<=5Β·106). | In the first line print single integer β the value of the expression modulo 109<=+<=7. | [
"2 2 4\n"
] | [
"19\n"
] | Consider the sample.
It is necessary to find the value of <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3ecc798906ae9e9852061ba2dd5cf6b8fce7753b.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
*f*(2)β=β1. From two girls you can form only one group of two people, in which there will be one comparison.
*f*(3)β=β3. From three girls you can form only one group of three people, in which there will be three comparisons.
*f*(4)β=β3. From four girls you can form two groups of two girls each. Then at the first stage there will be two comparisons, one in each of the two groups. In the second stage there will be two girls and there will be one comparison between them. Total 2β+β1β=β3 comparisons. You can also leave all girls in same group in the first stage. Then <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fcc6c9e72a1525cc01abbfb89094669a9d37d3b1.png" style="max-width: 100.0%;max-height: 100.0%;"/> comparisons will occur. Obviously, it's better to split girls into groups in the first way.
Then the value of the expression is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/2c5a0f75c9d910ec77b2fe675e690de453060631.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "2 2 4",
"output": "19"
},
{
"input": "7 2444902 2613424",
"output": "619309304"
},
{
"input": "7 2055976 2242066",
"output": "231875164"
},
{
"input": "5 431999 611310",
"output": "160643716"
},
{
"input": "9 1621304 1742530",
"output": "797579313"
},
{
"input": "71 3517969 3712339",
"output": "757356821"
},
{
"input": "95 941230 1053167",
"output": "58879863"
},
{
"input": "242954372 1561852 4674408",
"output": "364019214"
},
{
"input": "844079775 8212 4470298",
"output": "274251678"
},
{
"input": "17205735 14 4402745",
"output": "674451355"
},
{
"input": "451693431 631385 4779609",
"output": "781257515"
},
{
"input": "613442910 7402 7402",
"output": "6850551"
},
{
"input": "491601604 602162 602166",
"output": "24135010"
},
{
"input": "959694842 4587040 4587045",
"output": "15329026"
},
{
"input": "837853536 2173078 2173083",
"output": "36877759"
},
{
"input": "716012230 2122082 2122082",
"output": "440093377"
},
{
"input": "184105462 3815622 3815624",
"output": "989719082"
},
{
"input": "1000000001 5183 4999917",
"output": "699161981"
},
{
"input": "1000000000 1993 4998555",
"output": "297786854"
},
{
"input": "1000000000 1392 4999781",
"output": "151816990"
},
{
"input": "1000000006 53 4998992",
"output": "782425426"
},
{
"input": "1000000002 4256 4995024",
"output": "310505869"
},
{
"input": "999999904 6415 4999188",
"output": "170765011"
},
{
"input": "999999955 6892 4998328",
"output": "81076280"
},
{
"input": "999999928 3488 4996752",
"output": "893238505"
},
{
"input": "999999901 7120 4998160",
"output": "588585403"
},
{
"input": "1000000005 7438 4997560",
"output": "120910289"
},
{
"input": "1000000005 2909 4998022",
"output": "594211094"
},
{
"input": "1000000006 2660 4996436",
"output": "571467456"
},
{
"input": "1000000005 1939 4997543",
"output": "514484581"
},
{
"input": "1000000006 1196 4997350",
"output": "314503046"
},
{
"input": "3831 1887 4997241",
"output": "993734171"
},
{
"input": "181 4532 4998093",
"output": "923863675"
},
{
"input": "44 6533 4999050",
"output": "877259888"
},
{
"input": "9 272 4998466",
"output": "620566639"
},
{
"input": "5 534 4999482",
"output": "593235954"
},
{
"input": "5 3712 4994488",
"output": "335285903"
},
{
"input": "4 215 4999653",
"output": "937577799"
},
{
"input": "3 6591 4997858",
"output": "956074596"
},
{
"input": "2 3582 4998198",
"output": "497270151"
},
{
"input": "1 2261 4995214",
"output": "212402781"
},
{
"input": "1 7428 4999995",
"output": "25702920"
},
{
"input": "41295176 2 5000000",
"output": "141314244"
},
{
"input": "919453876 2 5000000",
"output": "206837932"
},
{
"input": "387547108 2 5000000",
"output": "911516666"
},
{
"input": "265705802 2 5000000",
"output": "908282096"
},
{
"input": "1 1324492 4999959",
"output": "0"
},
{
"input": "1 921119 4997169",
"output": "0"
},
{
"input": "1 4617 163972",
"output": "0"
},
{
"input": "1 8100 20381",
"output": "1"
},
{
"input": "1 38729 4996263",
"output": "1"
},
{
"input": "1 577561 4999761",
"output": "10"
},
{
"input": "1 779839 4999657",
"output": "99999999"
},
{
"input": "1 469031 3245104",
"output": "99999999"
},
{
"input": "1 1237 70497",
"output": "1000000000"
},
{
"input": "1 2306415 4999972",
"output": "1000000000"
},
{
"input": "1 784471 4999165",
"output": "1000000000"
},
{
"input": "1 7095 92491",
"output": "1000000006"
},
{
"input": "1 117021 4996616",
"output": "1000000006"
},
{
"input": "1 763641 4456559",
"output": "1000000005"
},
{
"input": "1 2357683 4999977",
"output": "1000000005"
},
{
"input": "1000000006 2 5000000",
"output": "515776485"
},
{
"input": "1000000005 2 5000000",
"output": "879984701"
},
{
"input": "1 2 5000000",
"output": "920443997"
},
{
"input": "972344355 2 2",
"output": "1"
},
{
"input": "735 5000000 5000000",
"output": "5117185"
}
] | 982 | 82,329,600 | 0 | 137,869 |
|
724 | Uniformly Branched Trees | [
"combinatorics",
"dp",
"trees"
] | null | null | A tree is a connected graph without cycles.
Two trees, consisting of *n* vertices each, are called isomorphic if there exists a permutation *p*:<={1,<=...,<=*n*}<=β<={1,<=...,<=*n*} such that the edge (*u*,<=*v*) is present in the first tree if and only if the edge (*p**u*,<=*p**v*) is present in the second tree.
Vertex of the tree is called internal if its degree is greater than or equal to two.
Count the number of different non-isomorphic trees, consisting of *n* vertices, such that the degree of each internal vertex is exactly *d*. Print the answer over the given prime modulo *mod*. | The single line of the input contains three integers *n*, *d* andΒ *mod* (1<=β€<=*n*<=β€<=1000, 2<=β€<=*d*<=β€<=10, 108<=β€<=*mod*<=β€<=109) Β β the number of vertices in the tree, the degree of internal vertices and the prime modulo. | Print the number of trees over the modulo *mod*. | [
"5 2 433416647\n",
"10 3 409693891\n",
"65 4 177545087\n"
] | [
"1\n",
"2\n",
"910726\n"
] | none | [] | 30 | 0 | 0 | 137,931 |
|
404 | Marathon | [
"implementation",
"math"
] | null | null | Valera takes part in the Berland Marathon. The marathon race starts at the stadium that can be represented on the plane as a square whose lower left corner is located at point with coordinates (0,<=0) and the length of the side equals *a* meters. The sides of the square are parallel to coordinate axes.
As the length of the marathon race is very long, Valera needs to have extra drink during the race. The coach gives Valera a bottle of drink each *d* meters of the path. We know that Valera starts at the point with coordinates (0,<=0) and runs counter-clockwise. That is, when Valera covers *a* meters, he reaches the point with coordinates (*a*,<=0). We also know that the length of the marathon race equals *nd*<=+<=0.5 meters.
Help Valera's coach determine where he should be located to help Valera. Specifically, determine the coordinates of Valera's positions when he covers *d*,<=2Β·*d*,<=...,<=*n*Β·*d* meters. | The first line contains two space-separated real numbers *a* and *d* (1<=β€<=*a*,<=*d*<=β€<=105), given with precision till 4 decimal digits after the decimal point. Number *a* denotes the length of the square's side that describes the stadium. Number *d* shows that after each *d* meters Valera gets an extra drink.
The second line contains integer *n* (1<=β€<=*n*<=β€<=105) showing that Valera needs an extra drink *n* times. | Print *n* lines, each line should contain two real numbers *x**i* and *y**i*, separated by a space. Numbers *x**i* and *y**i* in the *i*-th line mean that Valera is at point with coordinates (*x**i*,<=*y**i*) after he covers *i*Β·*d* meters. Your solution will be considered correct if the absolute or relative error doesn't exceed 10<=-<=4.
Note, that this problem have huge amount of output data. Please, do not use cout stream for output in this problem. | [
"2 5\n2\n",
"4.147 2.8819\n6\n"
] | [
"1.0000000000 2.0000000000\n2.0000000000 0.0000000000\n",
"2.8819000000 0.0000000000\n4.1470000000 1.6168000000\n3.7953000000 4.1470000000\n0.9134000000 4.1470000000\n0.0000000000 2.1785000000\n0.7034000000 0.0000000000\n"
] | none | [
{
"input": "2 5\n2",
"output": "1.0000000000 2.0000000000\n2.0000000000 0.0000000000"
},
{
"input": "4.147 2.8819\n6",
"output": "2.8819000000 0.0000000000\n4.1470000000 1.6168000000\n3.7953000000 4.1470000000\n0.9134000000 4.1470000000\n0.0000000000 2.1785000000\n0.7034000000 0.0000000000"
},
{
"input": "16904.8597 21646.2846\n10",
"output": "16904.8597000000 4741.4249000000\n7422.0099000000 16904.8597000000\n0.0000000000 2680.5850000000\n16904.8597000000 2060.8399000000\n10102.5949000000 16904.8597000000\n0.0000000000 5361.1700000000\n16285.1146000000 0.0000000000\n12783.1799000000 16904.8597000000\n0.0000000000 8041.7550000000\n13604.5296000000 0.0000000000"
},
{
"input": "40356.3702 72886.7142\n100",
"output": "40356.3702000000 32530.3440000000\n0.0000000000 15652.0524000000\n40356.3702000000 16878.2916000000\n0.0000000000 31304.1048000000\n40356.3702000000 1226.2392000000\n6599.7870000000 40356.3702000000\n25930.5570000000 0.0000000000\n22251.8394000000 40356.3702000000\n10278.5046000000 0.0000000000\n37903.8918000000 40356.3702000000\n0.0000000000 5373.5478000000\n40356.3702000000 27156.7962000000\n0.0000000000 21025.6002000000\n40356.3702000000 11504.7438000000\n0.0000000000 36677.6526000000\n36209.0616000000 ..."
},
{
"input": "70092.4982 95833.7741\n1000",
"output": "70092.4982000000 25741.2759000000\n18609.9464000000 70092.4982000000\n7131.3295000000 0.0000000000\n70092.4982000000 32872.6054000000\n11478.6169000000 70092.4982000000\n14262.6590000000 0.0000000000\n70092.4982000000 40003.9349000000\n4347.2874000000 70092.4982000000\n21393.9885000000 0.0000000000\n70092.4982000000 47135.2644000000\n0.0000000000 67308.4561000000\n28525.3180000000 0.0000000000\n70092.4982000000 54266.5939000000\n0.0000000000 60177.1266000000\n35656.6475000000 0.0000000000\n70092.4982000000..."
},
{
"input": "39712.7105 19285.2846\n10000",
"output": "19285.2846000000 0.0000000000\n38570.5692000000 0.0000000000\n39712.7105000000 18143.1433000000\n39712.7105000000 37428.4279000000\n22711.7085000000 39712.7105000000\n3426.4239000000 39712.7105000000\n0.0000000000 23853.8498000000\n0.0000000000 4568.5652000000\n14716.7194000000 0.0000000000\n34002.0040000000 0.0000000000\n39712.7105000000 13574.5781000000\n39712.7105000000 32859.8627000000\n27280.2737000000 39712.7105000000\n7994.9891000000 39712.7105000000\n0.0000000000 28422.4150000000\n0.0000000000 9137..."
},
{
"input": "97591.0354 49021.4126\n100000",
"output": "49021.4126000000 0.0000000000\n97591.0354000000 451.7898000000\n97591.0354000000 49473.2024000000\n96687.4558000000 97591.0354000000\n47666.0432000000 97591.0354000000\n0.0000000000 96235.6660000000\n0.0000000000 47214.2534000000\n1807.1592000000 0.0000000000\n50828.5718000000 0.0000000000\n97591.0354000000 2258.9490000000\n97591.0354000000 51280.3616000000\n94880.2966000000 97591.0354000000\n45858.8840000000 97591.0354000000\n0.0000000000 94428.5068000000\n0.0000000000 45407.0942000000\n3614.3184000000 0...."
},
{
"input": "1 100000\n100000",
"output": "0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000..."
},
{
"input": "1 1\n1",
"output": "1.0000000000 0.0000000000"
},
{
"input": "99999.9999 99999.9998\n100000",
"output": "99999.9998000000 0.0000000000\n99999.9999000000 99999.9997000000\n0.0003000000 99999.9999000000\n0.0000000000 0.0004000000\n99999.9994000000 0.0000000000\n99999.9999000000 99999.9993000000\n0.0007000000 99999.9999000000\n0.0000000000 0.0008000000\n99999.9990000000 0.0000000000\n99999.9999000000 99999.9989000000\n0.0011000000 99999.9999000000\n0.0000000000 0.0012000000\n99999.9986000000 0.0000000000\n99999.9999000000 99999.9985000000\n0.0015000000 99999.9999000000\n0.0000000000 0.0016000000\n99999.998200000..."
},
{
"input": "1.0001 9999.9999\n100000",
"output": "-0.0000000000 1.0001000000\n1.0001000000 1.0001000000\n1.0001000000 0.0000000000\n-0.0000000000 0.0000000000\n0.0000000000 1.0001000000\n1.0001000000 1.0001000000\n1.0001000000 0.0000000000\n-0.0000000000 0.0000000000\n0.0000000000 1.0001000000\n1.0001000000 1.0001000000\n1.0001000000 0.0000000000\n-0.0000000000 0.0000000000\n0.0000000000 1.0001000000\n1.0001000000 1.0001000000\n1.0001000000 0.0000000000\n-0.0000000000 0.0000000000\n0.0000000000 1.0001000000\n1.0001000000 1.0001000000\n1.0001000000 0.00000..."
},
{
"input": "100000 1\n100000",
"output": "1.0000000000 0.0000000000\n2.0000000000 0.0000000000\n3.0000000000 0.0000000000\n4.0000000000 0.0000000000\n5.0000000000 0.0000000000\n6.0000000000 0.0000000000\n7.0000000000 0.0000000000\n8.0000000000 0.0000000000\n9.0000000000 0.0000000000\n10.0000000000 0.0000000000\n11.0000000000 0.0000000000\n12.0000000000 0.0000000000\n13.0000000000 0.0000000000\n14.0000000000 0.0000000000\n15.0000000000 0.0000000000\n16.0000000000 0.0000000000\n17.0000000000 0.0000000000\n18.0000000000 0.0000000000\n19.0000000000 0...."
},
{
"input": "100000 100000\n100000",
"output": "100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n..."
},
{
"input": "100000 100000\n100000",
"output": "100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n100000.0000000000 0.0000000000\n100000.0000000000 100000.0000000000\n0.0000000000 100000.0000000000\n0.0000000000 0.0000000000\n..."
},
{
"input": "1.6055 15170.3125\n100000",
"output": "1.5485000000 0.0000000000\n1.6055000000 1.4915000000\n0.1710000000 1.6055000000\n0.0000000000 0.2280000000\n1.3205000000 0.0000000000\n1.6055000000 1.2635000000\n0.3990000000 1.6055000000\n0.0000000000 0.4560000000\n1.0925000000 0.0000000000\n1.6055000000 1.0355000000\n0.6270000000 1.6055000000\n0.0000000000 0.6840000000\n0.8645000000 0.0000000000\n1.6055000000 0.8075000000\n0.8550000000 1.6055000000\n0.0000000000 0.9120000000\n0.6365000000 0.0000000000\n1.6055000000 0.5795000000\n1.0830000000 1.6055000000..."
},
{
"input": "2.5828 24165.5413\n100000",
"output": "0.8645000000 0.0000000000\n1.7290000000 0.0000000000\n2.5828000000 0.0107000000\n2.5828000000 0.8752000000\n2.5828000000 1.7397000000\n2.5614000000 2.5828000000\n1.6969000000 2.5828000000\n0.8324000000 2.5828000000\n0.0000000000 2.5507000000\n0.0000000000 1.6862000000\n0.0000000000 0.8217000000\n0.0428000000 0.0000000000\n0.9073000000 0.0000000000\n1.7718000000 0.0000000000\n2.5828000000 0.0535000000\n2.5828000000 0.9180000000\n2.5828000000 1.7825000000\n2.5186000000 2.5828000000\n1.6541000000 2.5828000000..."
},
{
"input": "2.9320 33160.7701\n100000",
"output": "2.9320000000 2.7821000000\n0.0000000000 0.2998000000\n2.9320000000 2.4823000000\n0.0000000000 0.5996000000\n2.9320000000 2.1825000000\n0.0000000000 0.8994000000\n2.9320000000 1.8827000000\n0.0000000000 1.1992000000\n2.9320000000 1.5829000000\n0.0000000000 1.4990000000\n2.9320000000 1.2831000000\n0.0000000000 1.7988000000\n2.9320000000 0.9833000000\n0.0000000000 2.0986000000\n2.9320000000 0.6835000000\n0.0000000000 2.3984000000\n2.9320000000 0.3837000000\n0.0000000000 2.6982000000\n2.9320000000 0.0839000000..."
},
{
"input": "71601.9916 1.6928\n100000",
"output": "1.6928000000 0.0000000000\n3.3856000000 0.0000000000\n5.0784000000 0.0000000000\n6.7712000000 0.0000000000\n8.4640000000 0.0000000000\n10.1568000000 0.0000000000\n11.8496000000 0.0000000000\n13.5424000000 0.0000000000\n15.2352000000 0.0000000000\n16.9280000000 0.0000000000\n18.6208000000 0.0000000000\n20.3136000000 0.0000000000\n22.0064000000 0.0000000000\n23.6992000000 0.0000000000\n25.3920000000 0.0000000000\n27.0848000000 0.0000000000\n28.7776000000 0.0000000000\n30.4704000000 0.0000000000\n32.163200000..."
},
{
"input": "15145.9844 2.4240\n100000",
"output": "2.4240000000 0.0000000000\n4.8480000000 0.0000000000\n7.2720000000 0.0000000000\n9.6960000000 0.0000000000\n12.1200000000 0.0000000000\n14.5440000000 0.0000000000\n16.9680000000 0.0000000000\n19.3920000000 0.0000000000\n21.8160000000 0.0000000000\n24.2400000000 0.0000000000\n26.6640000000 0.0000000000\n29.0880000000 0.0000000000\n31.5120000000 0.0000000000\n33.9360000000 0.0000000000\n36.3600000000 0.0000000000\n38.7840000000 0.0000000000\n41.2080000000 0.0000000000\n43.6320000000 0.0000000000\n46.05600000..."
},
{
"input": "58689.9772 5.1934\n100000",
"output": "5.1934000000 0.0000000000\n10.3868000000 0.0000000000\n15.5802000000 0.0000000000\n20.7736000000 0.0000000000\n25.9670000000 0.0000000000\n31.1604000000 0.0000000000\n36.3538000000 0.0000000000\n41.5472000000 0.0000000000\n46.7406000000 0.0000000000\n51.9340000000 0.0000000000\n57.1274000000 0.0000000000\n62.3208000000 0.0000000000\n67.5142000000 0.0000000000\n72.7076000000 0.0000000000\n77.9010000000 0.0000000000\n83.0944000000 0.0000000000\n88.2878000000 0.0000000000\n93.4812000000 0.0000000000\n98.67460..."
},
{
"input": "26244.1561 49410.8330\n100000",
"output": "26244.1561000000 23166.6769000000\n0.0000000000 6154.9584000000\n26244.1561000000 17011.7185000000\n0.0000000000 12309.9168000000\n26244.1561000000 10856.7601000000\n0.0000000000 18464.8752000000\n26244.1561000000 4701.8017000000\n0.0000000000 24619.8336000000\n24790.9994000000 0.0000000000\n4530.6359000000 26244.1561000000\n18636.0410000000 0.0000000000\n10685.5943000000 26244.1561000000\n12481.0826000000 0.0000000000\n16840.5527000000 26244.1561000000\n6326.1242000000 0.0000000000\n22995.5111000000 26244..."
},
{
"input": "26253.1158 32456.3935\n100000",
"output": "26253.1158000000 6203.2777000000\n13846.5604000000 26253.1158000000\n0.0000000000 7643.2827000000\n24813.1108000000 0.0000000000\n21489.8431000000 26253.1158000000\n0.0000000000 15286.5654000000\n17169.8281000000 0.0000000000\n26253.1158000000 23373.1058000000\n0.0000000000 22929.8481000000\n9526.5454000000 0.0000000000\n26253.1158000000 15729.8231000000\n4320.0150000000 26253.1158000000\n1883.2627000000 0.0000000000\n26253.1158000000 8086.5404000000\n11963.2977000000 26253.1158000000\n0.0000000000 5760.02..."
},
{
"input": "26262.0740 15501.9524\n100000",
"output": "15501.9524000000 0.0000000000\n26262.0740000000 4741.8308000000\n26262.0740000000 20243.7832000000\n16778.4124000000 26262.0740000000\n1276.4600000000 26262.0740000000\n0.0000000000 12036.5816000000\n3465.3708000000 0.0000000000\n18967.3232000000 0.0000000000\n26262.0740000000 8207.2016000000\n26262.0740000000 23709.1540000000\n13313.0416000000 26262.0740000000\n0.0000000000 24073.1632000000\n0.0000000000 8571.2108000000\n6930.7416000000 0.0000000000\n22432.6940000000 0.0000000000\n26262.0740000000 11672.5..."
},
{
"input": "22635.8777 74922.1758\n10",
"output": "0.0000000000 15621.3350000000\n8606.7923000000 22635.8777000000\n22635.8777000000 21043.6281000000\n22635.8777000000 5422.2931000000\n12436.8358000000 0.0000000000\n0.0000000000 3184.4992000000\n0.0000000000 18805.8342000000\n11791.2915000000 22635.8777000000\n22635.8777000000 17859.1289000000\n22635.8777000000 2237.7939000000"
},
{
"input": "5681.4396 74931.1355\n10",
"output": "5681.4396000000 1072.4207000000\n3536.5982000000 5681.4396000000\n0.0000000000 2464.1775000000\n4289.6828000000 0.0000000000\n5681.4396000000 5362.1035000000\n0.0000000000 4928.3550000000\n1825.5053000000 0.0000000000\n5681.4396000000 2897.9260000000\n1711.0929000000 5681.4396000000\n0.0000000000 638.6722000000"
},
{
"input": "52372.0072 97869.2372\n100",
"output": "52372.0072000000 45497.2300000000\n0.0000000000 13749.5544000000\n52372.0072000000 31747.6756000000\n0.0000000000 27499.1088000000\n52372.0072000000 17998.1212000000\n0.0000000000 41248.6632000000\n52372.0072000000 4248.5668000000\n2626.2104000000 52372.0072000000\n42871.0196000000 0.0000000000\n16375.7648000000 52372.0072000000\n29121.4652000000 0.0000000000\n30125.3192000000 52372.0072000000\n15371.9108000000 0.0000000000\n43874.8736000000 52372.0072000000\n1622.3564000000 0.0000000000\n52372.0072000000 ..."
},
{
"input": "35417.5676 97878.1954\n100",
"output": "8374.5074000000 35417.5676000000\n35417.5676000000 18668.5528000000\n10294.0454000000 0.0000000000\n0.0000000000 33498.0296000000\n35417.5676000000 28962.5982000000\n20588.0908000000 0.0000000000\n0.0000000000 23203.9842000000\n31578.4916000000 35417.5676000000\n30882.1362000000 0.0000000000\n0.0000000000 12909.9388000000\n21284.4462000000 35417.5676000000\n35417.5676000000 5758.6140000000\n0.0000000000 2615.8934000000\n10990.4008000000 35417.5676000000\n35417.5676000000 16052.6594000000\n7678.1520000000 0..."
},
{
"input": "21992.2179 21320.7493\n1000",
"output": "21320.7493000000 0.0000000000\n21992.2179000000 20649.2807000000\n2014.4058000000 21992.2179000000\n0.0000000000 2685.8744000000\n18634.8749000000 0.0000000000\n21992.2179000000 17963.4063000000\n4700.2802000000 21992.2179000000\n0.0000000000 5371.7488000000\n15949.0005000000 0.0000000000\n21992.2179000000 15277.5319000000\n7386.1546000000 21992.2179000000\n0.0000000000 8057.6232000000\n13263.1261000000 0.0000000000\n21992.2179000000 12591.6575000000\n10072.0290000000 21992.2179000000\n0.0000000000 10743.4..."
},
{
"input": "5037.7799 21329.7059\n1000",
"output": "1178.5863000000 0.0000000000\n2357.1726000000 0.0000000000\n3535.7589000000 0.0000000000\n4714.3452000000 0.0000000000\n5037.7799000000 855.1516000000\n5037.7799000000 2033.7379000000\n5037.7799000000 3212.3242000000\n5037.7799000000 4390.9105000000\n4506.0630000000 5037.7799000000\n3327.4767000000 5037.7799000000\n2148.8904000000 5037.7799000000\n970.3041000000 5037.7799000000\n0.0000000000 4829.4977000000\n0.0000000000 3650.9114000000\n0.0000000000 2472.3251000000\n0.0000000000 1293.7388000000\n0.0000000..."
},
{
"input": "79870.5443 51056.8773\n10000",
"output": "51056.8773000000 0.0000000000\n79870.5443000000 22243.2103000000\n79870.5443000000 73300.0876000000\n35384.1237000000 79870.5443000000\n0.0000000000 64197.7907000000\n0.0000000000 13140.9134000000\n37915.9639000000 0.0000000000\n79870.5443000000 9102.2969000000\n79870.5443000000 60159.1742000000\n48525.0371000000 79870.5443000000\n0.0000000000 77338.7041000000\n0.0000000000 26281.8268000000\n24775.0505000000 0.0000000000\n75831.9278000000 0.0000000000\n79870.5443000000 47018.2608000000\n61665.9505000000 79..."
},
{
"input": "62916.1032 51065.8339\n10000",
"output": "51065.8339000000 0.0000000000\n62916.1032000000 39215.5646000000\n35550.8079000000 62916.1032000000\n0.0000000000 47401.0772000000\n3664.7567000000 0.0000000000\n54730.5906000000 0.0000000000\n62916.1032000000 42880.3213000000\n31886.0512000000 62916.1032000000\n0.0000000000 43736.3205000000\n7329.5134000000 0.0000000000\n58395.3473000000 0.0000000000\n62916.1032000000 46545.0780000000\n28221.2945000000 62916.1032000000\n0.0000000000 40071.5638000000\n10994.2701000000 0.0000000000\n62060.1040000000 0.00000..."
},
{
"input": "79870.5443 51056.8773\n10000",
"output": "51056.8773000000 0.0000000000\n79870.5443000000 22243.2103000000\n79870.5443000000 73300.0876000000\n35384.1237000000 79870.5443000000\n0.0000000000 64197.7907000000\n0.0000000000 13140.9134000000\n37915.9639000000 0.0000000000\n79870.5443000000 9102.2969000000\n79870.5443000000 60159.1742000000\n48525.0371000000 79870.5443000000\n0.0000000000 77338.7041000000\n0.0000000000 26281.8268000000\n24775.0505000000 0.0000000000\n75831.9278000000 0.0000000000\n79870.5443000000 47018.2608000000\n61665.9505000000 79..."
},
{
"input": "62916.1032 51065.8339\n10000",
"output": "51065.8339000000 0.0000000000\n62916.1032000000 39215.5646000000\n35550.8079000000 62916.1032000000\n0.0000000000 47401.0772000000\n3664.7567000000 0.0000000000\n54730.5906000000 0.0000000000\n62916.1032000000 42880.3213000000\n31886.0512000000 62916.1032000000\n0.0000000000 43736.3205000000\n7329.5134000000 0.0000000000\n58395.3473000000 0.0000000000\n62916.1032000000 46545.0780000000\n28221.2945000000 62916.1032000000\n0.0000000000 40071.5638000000\n10994.2701000000 0.0000000000\n62060.1040000000 0.00000..."
},
{
"input": "3 99999.1\n100000",
"output": "3.0000000000 0.1000000000\n2.8000000000 3.0000000000\n0.0000000000 2.7000000000\n0.4000000000 0.0000000000\n3.0000000000 0.5000000000\n2.4000000000 3.0000000000\n0.0000000000 2.3000000000\n0.8000000000 0.0000000000\n3.0000000000 0.9000000000\n2.0000000000 3.0000000000\n0.0000000000 1.9000000000\n1.2000000000 0.0000000000\n3.0000000000 1.3000000000\n1.6000000000 3.0000000000\n0.0000000000 1.5000000000\n1.6000000000 0.0000000000\n3.0000000000 1.7000000000\n1.2000000000 3.0000000000\n0.0000000000 1.1000000000..."
},
{
"input": "3 99999.2\n100000",
"output": "3.0000000000 0.2000000000\n2.6000000000 3.0000000000\n0.0000000000 2.4000000000\n0.8000000000 0.0000000000\n3.0000000000 1.0000000000\n1.8000000000 3.0000000000\n0.0000000000 1.6000000000\n1.6000000000 0.0000000000\n3.0000000000 1.8000000000\n1.0000000000 3.0000000000\n0.0000000000 0.8000000000\n2.4000000000 0.0000000000\n3.0000000000 2.6000000000\n0.2000000000 3.0000000000\n0.0000000000 0.0000000000\n3.0000000000 0.2000000000\n2.6000000000 3.0000000000\n0.0000000000 2.4000000000\n0.8000000000 0.0000000000..."
},
{
"input": "3 99999.0001\n100000",
"output": "3.0000000000 0.0001000000\n2.9998000000 3.0000000000\n0.0000000000 2.9997000000\n0.0004000000 0.0000000000\n3.0000000000 0.0005000000\n2.9994000000 3.0000000000\n0.0000000000 2.9993000000\n0.0008000000 0.0000000000\n3.0000000000 0.0009000000\n2.9990000000 3.0000000000\n0.0000000000 2.9989000000\n0.0012000000 0.0000000000\n3.0000000000 0.0013000000\n2.9986000000 3.0000000000\n0.0000000000 2.9985000000\n0.0016000000 0.0000000000\n3.0000000000 0.0017000000\n2.9982000000 3.0000000000\n0.0000000000 2.9981000000..."
},
{
"input": "3 99999.0002\n100000",
"output": "3.0000000000 0.0002000000\n2.9996000000 3.0000000000\n0.0000000000 2.9994000000\n0.0008000000 0.0000000000\n3.0000000000 0.0010000000\n2.9988000000 3.0000000000\n0.0000000000 2.9986000000\n0.0016000000 0.0000000000\n3.0000000000 0.0018000000\n2.9980000000 3.0000000000\n0.0000000000 2.9978000000\n0.0024000000 0.0000000000\n3.0000000000 0.0026000000\n2.9972000000 3.0000000000\n0.0000000000 2.9970000000\n0.0032000000 0.0000000000\n3.0000000000 0.0034000000\n2.9964000000 3.0000000000\n0.0000000000 2.9962000000..."
},
{
"input": "100000 99999.0002\n100000",
"output": "99999.0002000000 0.0000000000\n100000.0000000000 99998.0004000000\n2.9994000000 100000.0000000000\n0.0000000000 3.9992000000\n99995.0010000000 0.0000000000\n100000.0000000000 99994.0012000000\n6.9986000000 100000.0000000000\n0.0000000000 7.9984000000\n99991.0018000000 0.0000000000\n100000.0000000000 99990.0020000000\n10.9978000000 100000.0000000000\n0.0000000000 11.9976000000\n99987.0026000000 0.0000000000\n100000.0000000000 99986.0028000000\n14.9970000000 100000.0000000000\n0.0000000000 15.9968000000\n999..."
},
{
"input": "99999.9998 99999.9999\n100000",
"output": "99999.9998000000 0.0001000000\n99999.9996000000 99999.9998000000\n0.0000000000 99999.9995000000\n0.0004000000 0.0000000000\n99999.9998000000 0.0005000000\n99999.9992000000 99999.9998000000\n0.0000000000 99999.9991000000\n0.0008000000 0.0000000000\n99999.9998000000 0.0009000000\n99999.9988000000 99999.9998000000\n0.0000000000 99999.9987000000\n0.0012000000 0.0000000000\n99999.9998000000 0.0013000000\n99999.9984000000 99999.9998000000\n0.0000000000 99999.9983000000\n0.0016000000 0.0000000000\n99999.999800000..."
},
{
"input": "1 9999.9999\n100000",
"output": "0.0000000000 0.0001000000\n0.0000000000 0.0002000000\n0.0000000000 0.0003000000\n0.0000000000 0.0004000000\n0.0000000000 0.0005000000\n0.0000000000 0.0006000000\n0.0000000000 0.0007000000\n0.0000000000 0.0008000000\n0.0000000000 0.0009000000\n0.0000000000 0.0010000000\n0.0000000000 0.0011000000\n0.0000000000 0.0012000000\n0.0000000000 0.0013000000\n0.0000000000 0.0014000000\n0.0000000000 0.0015000000\n0.0000000000 0.0016000000\n0.0000000000 0.0017000000\n0.0000000000 0.0018000000\n0.0000000000 0.0019000000..."
},
{
"input": "1.0001 9996.9996\n100000",
"output": "0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000\n0.0000000000 0.0000000000..."
},
{
"input": "1.0001 99999.9999\n100000",
"output": "0.9992000000 1.0001000000\n0.0018000000 0.0000000000\n0.9974000000 1.0001000000\n0.0036000000 0.0000000000\n0.9956000000 1.0001000000\n0.0054000000 0.0000000000\n0.9938000000 1.0001000000\n0.0072000000 0.0000000000\n0.9920000000 1.0001000000\n0.0090000000 0.0000000000\n0.9902000000 1.0001000000\n0.0108000000 0.0000000000\n0.9884000000 1.0001000000\n0.0126000000 0.0000000000\n0.9866000000 1.0001000000\n0.0144000000 0.0000000000\n0.9848000000 1.0001000000\n0.0162000000 0.0000000000\n0.9830000000 1.0001000000..."
},
{
"input": "1.0001 100000\n100000",
"output": "0.9991000000 1.0001000000\n0.0020000000 0.0000000000\n0.9971000000 1.0001000000\n0.0040000000 0.0000000000\n0.9951000000 1.0001000000\n0.0060000000 0.0000000000\n0.9931000000 1.0001000000\n0.0080000000 0.0000000000\n0.9911000000 1.0001000000\n0.0100000000 0.0000000000\n0.9891000000 1.0001000000\n0.0120000000 0.0000000000\n0.9871000000 1.0001000000\n0.0140000000 0.0000000000\n0.9851000000 1.0001000000\n0.0160000000 0.0000000000\n0.9831000000 1.0001000000\n0.0180000000 0.0000000000\n0.9811000000 1.0001000000..."
},
{
"input": "1.1 99999.9\n100000",
"output": "1.1000000000 0.0000000000\n1.1000000000 1.1000000000\n0.0000000000 1.1000000000\n-0.0000000000 0.0000000000\n1.1000000000 0.0000000000\n1.1000000000 1.1000000000\n0.0000000000 1.1000000000\n-0.0000000000 0.0000000000\n1.1000000000 0.0000000000\n1.1000000000 1.1000000000\n0.0000000000 1.1000000000\n-0.0000000000 0.0000000000\n1.1000000000 0.0000000000\n1.1000000000 1.1000000000\n0.0000000000 1.1000000000\n-0.0000000000 0.0000000000\n1.1000000000 0.0000000000\n1.1000000000 1.1000000000\n0.0000000000 1.100000..."
},
{
"input": "1.0001 99997\n100000",
"output": "0.0000000000 0.9988000000\n0.9975000000 1.0001000000\n1.0001000000 0.0039000000\n0.0052000000 0.0000000000\n0.0000000000 0.9936000000\n0.9923000000 1.0001000000\n1.0001000000 0.0091000000\n0.0104000000 0.0000000000\n0.0000000000 0.9884000000\n0.9871000000 1.0001000000\n1.0001000000 0.0143000000\n0.0156000000 0.0000000000\n0.0000000000 0.9832000000\n0.9819000000 1.0001000000\n1.0001000000 0.0195000000\n0.0208000000 0.0000000000\n0.0000000000 0.9780000000\n0.9767000000 1.0001000000\n1.0001000000 0.0247000000..."
},
{
"input": "1.1101 100000\n100000",
"output": "1.1101000000 1.0819000000\n0.0000000000 0.0564000000\n1.1101000000 1.0255000000\n0.0000000000 0.1128000000\n1.1101000000 0.9691000000\n0.0000000000 0.1692000000\n1.1101000000 0.9127000000\n0.0000000000 0.2256000000\n1.1101000000 0.8563000000\n0.0000000000 0.2820000000\n1.1101000000 0.7999000000\n0.0000000000 0.3384000000\n1.1101000000 0.7435000000\n0.0000000000 0.3948000000\n1.1101000000 0.6871000000\n0.0000000000 0.4512000000\n1.1101000000 0.6307000000\n0.0000000000 0.5076000000\n1.1101000000 0.5743000000..."
}
] | 202 | 13,312,000 | 0 | 138,035 |
|
256 | Liars and Serge | [
"dp"
] | null | null | There are *n* people, sitting in a line at the table. For each person we know that he always tells either the truth or lies.
Little Serge asked them: how many of you always tell the truth? Each of the people at the table knows everything (who is an honest person and who is a liar) about all the people at the table. The honest people are going to say the correct answer, the liars are going to say any integer from 1 to *n*, which is not the correct answer. Every liar chooses his answer, regardless of the other liars, so two distinct liars may give distinct answer.
Serge does not know any information about the people besides their answers to his question. He took a piece of paper and wrote *n* integers *a*1,<=*a*2,<=...,<=*a**n*, where *a**i* is the answer of the *i*-th person in the row. Given this sequence, Serge determined that exactly *k* people sitting at the table apparently lie.
Serge wonders, how many variants of people's answers (sequences of answers *a* of length *n*) there are where one can say that exactly *k* people sitting at the table apparently lie. As there can be rather many described variants of answers, count the remainder of dividing the number of the variants by 777777777. | The first line contains two integers *n*, *k*, (1<=β€<=*k*<=β€<=*n*<=β€<=28). It is guaranteed that *n* β is the power of number 2. | Print a single integer β the answer to the problem modulo 777777777. | [
"1 1\n",
"2 1\n"
] | [
"0\n",
"2\n"
] | none | [] | 92 | 0 | 0 | 139,170 |
|
424 | Colored Jenga | [
"dfs and similar",
"dp",
"probabilities"
] | null | null | Cold winter evenings in Tomsk are very boring β nobody wants be on the streets at such a time. Residents of Tomsk while away the time sitting in warm apartments, inventing a lot of different games. One of such games is 'Colored Jenga'.
This game requires wooden blocks of three colors: red, green and blue. A tower of *n* levels is made from them. Each level consists of three wooden blocks. The blocks in each level can be of arbitrary colors, but they are always located close and parallel to each other. An example of such a tower is shown in the figure.
The game is played by exactly one person. Every minute a player throws a special dice which has six sides. Two sides of the dice are green, two are blue, one is red and one is black. The dice shows each side equiprobably.
If the dice shows red, green or blue, the player must take any block of this color out of the tower at this minute so that the tower doesn't fall. If this is not possible, the player waits until the end of the minute, without touching the tower. He also has to wait until the end of the minute without touching the tower if the dice shows the black side. It is not allowed to take blocks from the top level of the tower (whether it is completed or not).
Once a player got a block out, he must put it on the top of the tower so as to form a new level or finish the upper level consisting of previously placed blocks. The newly constructed levels should have all the same properties as the initial levels. If the upper level is not completed, starting the new level is prohibited.
For the tower not to fall, in each of the levels except for the top, there should be at least one block. Moreover, if at some of these levels there is exactly one block left and this block is not the middle block, the tower falls.
The game ends at the moment when there is no block in the tower that you can take out so that the tower doesn't fall.
Here is a wonderful game invented by the residents of the city of Tomsk. I wonder for how many minutes can the game last if the player acts optimally well? If a player acts optimally well, then at any moment he tries to choose the block he takes out so as to minimize the expected number of the game duration.
Your task is to write a program that determines the expected number of the desired amount of minutes. | The first line of the input contains the only integer *n* (2<=β€<=*n*<=β€<=6) β the number of levels in the tower.
Then *n* lines follow, describing the levels of the tower from the bottom to the top (the first line is the top of the tower). Each level is described by three characters, the first and the third of them set the border blocks of the level and the second one is the middle block. The character that describes the block has one of the following values 'R' (a red block), 'G' (a green block) and 'B' (a blue block). | In the only line of the output print the sought mathematical expectation value. The answer will be considered correct if its relative or absolute error doesn't exceed 10<=-<=6. | [
"6\nRGB\nGRG\nBBB\nGGR\nBRG\nBRB\n"
] | [
"17.119213696601992\n"
] | none | [] | 31 | 0 | 0 | 139,343 |
|
594 | Cutting the Line | [
"string suffix structures",
"strings"
] | null | null | You are given a non-empty line *s* and an integer *k*. The following operation is performed with this line exactly once:
- A line is split into at most *k* non-empty substrings, i.e. string *s* is represented as a concatenation of a set of strings *s*<==<=*t*1<=+<=*t*2<=+<=...<=+<=*t**m*, 1<=β€<=*m*<=β€<=*k*. - Some of strings *t**i* are replaced by strings *t**i**r*, that is, their record from right to left. - The lines are concatenated back in the same order, we get string *s*'<==<=*t*'1*t*'2... *t*'*m*, where *t*'*i* equals *t**i* or *t**i**r*.
Your task is to determine the lexicographically smallest string that could be the result of applying the given operation to the string *s*. | The first line of the input contains string *s* (1<=β€<=|*s*|<=β€<=5<=000<=000), consisting of lowercase English letters. The second line contains integer *k* (1<=β€<=*k*<=β€<=|*s*|)Β β the maximum number of parts in the partition. | In the single line print the lexicographically minimum string *s*' which can be obtained as a result of performing the described operation. | [
"aba\n2\n",
"aaaabacaba\n2\n",
"bababa\n1\n",
"abacabadabacaba\n4\n"
] | [
"aab\n",
"aaaaabacab\n",
"ababab\n",
"aababacabacabad\n"
] | none | [] | 46 | 0 | 0 | 139,576 |
|
802 | April Fools' Problem (medium) | [
"binary search",
"flows",
"graphs"
] | null | null | The marmots need to prepare *k* problems for HC2 over *n* days. Each problem, once prepared, also has to be printed.
The preparation of a problem on day *i* (at most one per day) costs *a**i* CHF, and the printing of a problem on day *i* (also at most one per day) costs *b**i* CHF. Of course, a problem cannot be printed before it has been prepared (but doing both on the same day is fine).
What is the minimum cost of preparation and printing? | The first line of input contains two space-separated integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=2200). The second line contains *n* space-separated integers *a*1,<=...,<=*a**n* () β the preparation costs. The third line contains *n* space-separated integers *b*1,<=...,<=*b**n* () β the printing costs. | Output the minimum cost of preparation and printing *k* problems β that is, the minimum possible sum *a**i*1<=+<=*a**i*2<=+<=...<=+<=*a**i**k*<=+<=*b**j*1<=+<=*b**j*2<=+<=...<=+<=*b**j**k*, where 1<=β€<=*i*1<=<<=*i*2<=<<=...<=<<=*i**k*<=β€<=*n*, 1<=β€<=*j*1<=<<=*j*2<=<<=...<=<<=*j**k*<=β€<=*n* and *i*1<=β€<=*j*1, *i*2<=β€<=*j*2, ..., *i**k*<=β€<=*j**k*. | [
"8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1\n"
] | [
"32"
] | In the sample testcase, one optimum solution is to prepare the first problem on day 1 and print it on day 1, prepare the second problem on day 2 and print it on day 4, prepare the third problem on day 3 and print it on day 5, and prepare the fourth problem on day 6 and print it on day 8. | [
{
"input": "8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1",
"output": "32"
},
{
"input": "10 6\n60 8 63 72 1 100 23 59 71 59\n81 27 66 53 46 64 86 27 41 82",
"output": "472"
},
{
"input": "13 13\n93 19 58 34 96 7 35 46 60 5 36 40 41\n57 3 42 68 26 85 25 45 50 21 60 23 79",
"output": "1154"
},
{
"input": "12 1\n49 49 4 16 79 20 86 94 43 55 45 17\n15 36 51 20 83 6 83 80 72 22 66 100",
"output": "10"
},
{
"input": "1 1\n96\n86",
"output": "182"
}
] | 3,868 | 13,926,400 | 3 | 140,240 |
|
291 | Parallel Programming | [
"*special",
"greedy"
] | null | null | Polycarpus has a computer with *n* processors. Also, his computer has *n* memory cells. We'll consider the processors numbered by integers from 1 to *n* and that the memory cells are consecutively numbered by integers from 1 to *n*.
Polycarpus needs to come up with a parallel program model. For each memory cell number *i* this program must record the value *n*<=-<=*i* to this cell. In other words, for each cell you've got to find the distance to cell *n*.
Let's denote the value that is written in the *i*-th cell as *a**i*. Initially, *a**i*<==<=1 (1<=β€<=*i*<=<<=*n*) and *a**n*<==<=0. We will consider that only processor *i* can write values in the memory cell number *i*. All processors can read an information from some cell (several processors can read an information from some cell simultaneously).
The parallel program is executed in several steps. During each step we execute the parallel version of the increment operation. Executing the parallel version of the increment operation goes as follows:
1. Each processor independently of the other ones chooses some memory cell. Let's say that processor *i* has chosen a cell with number *c**i* (1<=β€<=*c**i*<=β€<=*n*). 1. All processors simultaneously execute operation *a**i*<==<=*a**i*<=+<=*a**c**i*.
Help Polycarpus come up with the parallel program model that is executed in exactly *k* steps. Calculate the operations that need to be executed. Note that after *k* steps for all *i*'s value *a**i* must be equal *n*<=-<=*i*. | The first line contains two space-separated integers *n* and *k* (1<=β€<=*n*<=β€<=104,<=1<=β€<=*k*<=β€<=20).
It is guaranteed that at the given *n* and *k* the required sequence of operations exists. | Print exactly *n*Β·*k* integers in *k* lines. In the first line print numbers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=*n*) for the first increment operation. In the second line print the numbers for the second increment operation. In the *k*-th line print the numbers for the *k*-th increment operation.
As a result of the printed operations for any *i* value *a**i* must equal *n*<=-<=*i*. | [
"1 1\n",
"3 2\n"
] | [
"1\n",
"2 3 3\n3 3 3\n"
] | none | [
{
"input": "1 1",
"output": "1"
},
{
"input": "3 2",
"output": "2 3 3\n3 3 3"
},
{
"input": "4 2",
"output": "2 3 4 4\n3 4 4 4"
},
{
"input": "2 1",
"output": "2 2"
},
{
"input": "2 20",
"output": "2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2\n2 2"
},
{
"input": "1 20",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1"
},
{
"input": "10000 20",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "10 4",
"output": "2 3 4 5 6 7 8 9 10 10\n3 4 5 6 7 8 9 10 10 10\n5 6 7 8 9 10 10 10 10 10\n9 10 10 10 10 10 10 10 10 10"
},
{
"input": "10 5",
"output": "2 3 4 5 6 7 8 9 10 10\n3 4 5 6 7 8 9 10 10 10\n5 6 7 8 9 10 10 10 10 10\n9 10 10 10 10 10 10 10 10 10\n10 10 10 10 10 10 10 10 10 10"
},
{
"input": "10 6",
"output": "2 3 4 5 6 7 8 9 10 10\n3 4 5 6 7 8 9 10 10 10\n5 6 7 8 9 10 10 10 10 10\n9 10 10 10 10 10 10 10 10 10\n10 10 10 10 10 10 10 10 10 10\n10 10 10 10 10 10 10 10 10 10"
},
{
"input": "128 7",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 128\n3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 ..."
},
{
"input": "127 7",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 127\n3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4..."
},
{
"input": "129 8",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 129\n3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38..."
},
{
"input": "130 8",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 130\n3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3..."
},
{
"input": "131 8",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 131\n3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 ..."
},
{
"input": "1024 10",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "1024 11",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "1023 10",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "10000 14",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "8192 13",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "8190 13",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "8192 14",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "8123 13",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "5000 13",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "2000 11",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "2198 14",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
},
{
"input": "2512 12",
"output": "2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 1..."
}
] | 124 | 716,800 | 0 | 140,477 |
|
538 | Demiurges Play Again | [
"dfs and similar",
"dp",
"math",
"trees"
] | null | null | Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game.
There is a rooted tree on *n* nodes, *m* of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to *m* are placed in such a way that each number appears exactly in one leaf.
Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well.
Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers. | The first line contains a single integer *n*Β β the number of nodes in the tree (1<=β€<=*n*<=β€<=2Β·105).
Each of the next *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*)Β β the ends of the edge of the tree; the edge leads from node *u**i* to node *v**i*. It is guaranteed that the described graph is a rooted tree, and the root is the node 1. | Print two space-separated integers β the maximum possible and the minimum possible result of the game. | [
"5\n1 2\n1 3\n2 4\n2 5\n",
"6\n1 2\n1 3\n3 4\n1 5\n5 6\n"
] | [
"3 2\n",
"3 3\n"
] | Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2.
In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3. | [
{
"input": "5\n1 2\n1 3\n2 4\n2 5",
"output": "3 2"
},
{
"input": "6\n1 2\n1 3\n3 4\n1 5\n5 6",
"output": "3 3"
},
{
"input": "1",
"output": "1 1"
},
{
"input": "2\n1 2",
"output": "1 1"
},
{
"input": "3\n1 2\n1 3",
"output": "2 2"
},
{
"input": "10\n1 2\n1 3\n3 4\n3 5\n4 6\n1 7\n3 8\n2 9\n6 10",
"output": "5 3"
},
{
"input": "50\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47\n47 48\n48 49\n49 50",
"output": "1 1"
},
{
"input": "22\n1 2\n2 3\n3 4\n1 5\n1 6\n1 7\n7 8\n8 9\n9 10\n10 11\n1 12\n12 13\n12 14\n14 15\n1 16\n16 17\n1 18\n18 19\n1 20\n20 21\n1 22",
"output": "10 9"
}
] | 108 | 20,172,800 | 0 | 140,582 |
|
731 | Funny Game | [
"dp",
"games"
] | null | null | Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them.
The game they came up with has the following rules. Initially, there are *n* stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first.
One move happens as follows. Lets say there are *m*<=β₯<=2 stickers on the wall. The player, who makes the current move, picks some integer *k* from 2 to *m* and takes *k* leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move.
Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent.
Given the integer *n* and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally. | The first line of input contains a single integer *n* (2<=β€<=*n*<=β€<=200<=000)Β β the number of stickers, initially located on the wall.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (<=-<=10<=000<=β€<=*a**i*<=β€<=10<=000)Β β the numbers on stickers in order from left to right. | Print one integerΒ β the difference between the Petya's score and Gena's score at the end of the game if both players play optimally. | [
"3\n2 4 8\n",
"4\n1 -7 -2 3\n"
] | [
"14\n",
"-3\n"
] | In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0.
In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value β-β8. On the second move Gena will take the remaining two stickers. The Petya's score is 1β+β(β-β7)β+β(β-β2)β=ββ-β8, Gena's score is (β-β8)β+β3β=ββ-β5, i.e. the score difference will be β-β3. | [
{
"input": "3\n2 4 8",
"output": "14"
},
{
"input": "4\n1 -7 -2 3",
"output": "-3"
},
{
"input": "10\n35 11 35 28 48 25 2 43 23 10",
"output": "260"
},
{
"input": "100\n437 89 481 95 29 326 10 304 97 414 52 46 106 181 385 173 337 148 437 133 52 136 86 250 289 61 480 314 166 69 275 486 117 129 353 412 382 469 290 479 388 231 7 462 247 432 488 146 466 422 369 336 148 460 418 356 303 149 421 146 233 224 432 239 392 409 172 331 152 433 345 205 451 138 273 284 48 109 294 281 468 301 337 176 137 52 216 79 431 141 147 240 107 184 393 459 286 123 297 160",
"output": "26149"
},
{
"input": "3\n2 1 2",
"output": "5"
},
{
"input": "101\n11 -250 -200 157 84 89 207 139 -76 -183 -26 -218 79 -122 244 -133 82 -64 38 131 184 -154 256 -250 -246 227 -57 -188 -208 -48 64 -163 213 -110 -17 -106 -96 198 19 -214 50 -117 -215 214 -254 185 -7 19 117 112 172 -229 66 -169 209 -110 122 223 0 -151 66 -154 20 77 -180 202 246 -209 24 -180 -3 10 -86 -26 50 29 225 47 -177 225 -189 -40 -114 -56 -50 70 -102 160 -26 167 48 188 -84 -194 -201 250 135 -174 -222 192 -64",
"output": "211"
},
{
"input": "102\n-70 -76 15 -32 -88 -26 75 23 92 -96 7 34 92 45 -62 -90 -26 78 -11 -63 34 -61 54 -32 -63 -70 38 73 22 97 -67 81 76 -10 -90 23 47 -23 31 25 -68 75 33 -71 95 57 -9 38 -22 39 68 -19 29 -67 41 75 13 36 5 3 -4 9 -9 -42 -72 51 -44 67 55 -1 30 -1 -9 101 39 -80 86 50 78 -81 11 -19 -63 72 -82 54 -18 -5 -101 22 50 3 26 -52 -83 -21 -9 -54 86 12 -21 99",
"output": "487"
},
{
"input": "103\n-26 87 -179 -82 156 68 -131 67 -203 166 -6 -3 99 176 97 -115 73 155 30 208 131 -106 -20 98 -77 -60 -152 -24 -158 185 193 112 86 -74 114 -185 49 162 -207 96 -70 -212 12 28 -19 73 -115 -169 169 -27 -183 112 -207 112 42 -107 31 -92 161 -84 181 21 189 190 -115 54 -138 140 169 161 -197 146 -25 44 95 -121 -19 -180 -5 172 -81 -51 -86 137 27 -152 17 -121 -177 113 94 -179 -11 -38 201 -155 -22 -104 -21 161 189 -60 115",
"output": "813"
},
{
"input": "104\n256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256",
"output": "26624"
},
{
"input": "105\n102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102",
"output": "10710"
},
{
"input": "106\n212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212",
"output": "22472"
},
{
"input": "107\n256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256",
"output": "256"
},
{
"input": "108\n102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102",
"output": "102"
},
{
"input": "109\n212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212",
"output": "212"
},
{
"input": "110\n-256 -149 -136 -33 -253 -141 -170 -253 0 -107 -141 -236 -65 -158 -84 -180 -97 -97 -223 -44 -232 -255 -108 -25 -49 -48 -212 -3 -232 -172 -231 -158 -23 -206 -198 -55 -36 -11 -169 -94 -190 -115 -116 -231 -155 -201 -155 -103 -242 -119 -136 -8 -2 -11 -69 -250 -51 -129 -155 -216 -107 -102 -186 -13 -78 -2 -238 -66 -29 -102 -249 -198 -151 -38 -3 -128 -130 -73 -236 -83 -28 -95 -140 -62 -24 -168 -199 -196 -28 -28 -6 -220 -247 -75 -200 -228 -109 -251 -76 -53 -43 -170 -213 -146 -68 -58 -58 -218 -186 -165",
"output": "165"
},
{
"input": "111\n-66 -39 -25 -78 -75 -44 -56 -89 -70 -7 -46 -70 -51 -36 -61 -95 -40 -84 -48 -8 -35 -37 -47 -35 -75 -87 -10 -101 -43 -70 -28 -50 -39 -13 -34 -40 -100 -70 -32 -12 -23 -62 -41 -94 -25 -30 -102 -32 -78 -10 -82 -71 -34 -2 -100 -60 -14 -17 -12 -57 -96 -27 -27 -23 -74 -60 -30 -38 -61 -95 -41 -73 -24 -76 -68 -29 -17 -75 -28 -86 -68 -25 -20 -68 -100 -44 -47 -8 -85 -84 -68 -92 -33 -9 -40 -83 -64 -2 -94 -66 -65 -46 -22 -41 -47 -24 -56 -91 -65 -63 -5",
"output": "5"
},
{
"input": "113\n154 110 128 156 88 54 172 96 93 13 108 219 37 34 44 153 73 23 0 210 85 18 243 147 174 182 153 196 200 223 162 151 237 148 174 86 181 1 17 187 81 175 46 253 131 44 145 184 53 164 97 220 94 0 8 157 225 50 90 186 79 67 199 108 159 86 173 181 208 182 17 254 82 61 64 7 29 112 156 105 175 91 165 229 162 101 3 62 154 32 13 133 116 185 237 94 67 171 23 123 249 255 135 23 126 115 175 73 128 16 88 139 78",
"output": "13598"
},
{
"input": "114\n46 7 39 21 44 31 49 57 26 22 86 45 66 72 96 15 77 38 92 88 50 68 30 55 20 5 15 11 26 66 94 74 43 73 35 7 11 36 26 74 86 52 14 5 91 71 3 75 22 7 10 97 42 41 52 80 97 31 45 59 53 85 87 63 42 51 98 61 26 96 65 22 47 0 36 27 35 69 81 58 9 43 7 98 27 56 101 2 31 82 48 100 77 77 42 61 6 32 69 30 102 64 51 64 20 24 76 87 63 52 73 41 5 34",
"output": "5672"
},
{
"input": "115\n176 163 163 37 7 157 82 29 153 189 174 103 105 90 49 63 88 151 198 31 178 110 15 188 20 181 167 118 133 203 121 150 201 103 205 160 103 91 177 133 107 147 11 11 199 137 139 153 29 94 81 143 185 137 101 71 26 14 123 73 72 134 149 51 175 71 41 155 111 146 61 140 82 75 134 107 142 95 159 132 5 76 32 133 71 129 207 212 77 173 185 123 174 53 88 44 105 37 115 204 172 4 207 118 28 134 207 50 194 40 54 95 47 39 70",
"output": "12880"
},
{
"input": "2\n-10000 -10000",
"output": "-20000"
},
{
"input": "4\n2 -10000 -10 4",
"output": "-4"
},
{
"input": "6\n-6000 -5000 -4000 -3000 -2000 -1000",
"output": "1000"
},
{
"input": "10\n-10000 -10000 100 100 100 100 100 100 100 100",
"output": "-100"
},
{
"input": "2\n1313 8442",
"output": "9755"
},
{
"input": "2\n5 -3",
"output": "2"
},
{
"input": "4\n1 5 -6 0",
"output": "6"
}
] | 171 | 27,443,200 | 3 | 141,265 |
|
610 | Vika and Segments | [
"constructive algorithms",
"data structures",
"geometry",
"two pointers"
] | null | null | Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew *n* black horizontal and vertical segments parallel to the coordinate axes. All segments have width equal to 1 square, that means every segment occupy some set of neighbouring squares situated in one row or one column.
Your task is to calculate the number of painted cells. If a cell was painted more than once, it should be calculated exactly once. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of segments drawn by Vika.
Each of the next *n* lines contains four integers *x*1, *y*1, *x*2 and *y*2 (<=-<=109<=β€<=*x*1,<=*y*1,<=*x*2,<=*y*2<=β€<=109)Β β the coordinates of the endpoints of the segments drawn by Vika. It is guaranteed that all the segments are parallel to coordinate axes. Segments may touch, overlap and even completely coincide. | Print the number of cells painted by Vika. If a cell was painted more than once, it should be calculated exactly once in the answer. | [
"3\n0 1 2 1\n1 4 1 2\n0 3 2 3\n",
"4\n-2 -1 2 -1\n2 1 -2 1\n-1 -2 -1 2\n1 2 1 -2\n"
] | [
"8\n",
"16\n"
] | In the first sample Vika will paint squares (0,β1), (1,β1), (2,β1), (1,β2), (1,β3), (1,β4), (0,β3) and (2,β3). | [
{
"input": "3\n0 1 2 1\n1 4 1 2\n0 3 2 3",
"output": "8"
},
{
"input": "4\n-2 -1 2 -1\n2 1 -2 1\n-1 -2 -1 2\n1 2 1 -2",
"output": "16"
},
{
"input": "1\n1 1 1 1",
"output": "1"
},
{
"input": "10\n-357884841 -999999905 -357884841 999999943\n-130177221 999999983 -130177221 -999999974\n627454332 999999936 627454332 -999999900\n999999986 366591992 -999999919 366591992\n488824292 999999952 488824292 -999999979\n-261575319 999999910 -261575319 -999999995\n837827059 -999999983 837827059 999999984\n-999999947 543634048 999999977 543634048\n512878899 -999999968 512878899 999999926\n239286254 -999999975 239286254 999999937",
"output": "19999999073"
},
{
"input": "4\n553245544 -999999997 553245544 -918743333\n999999988 -668043590 264717840 -668043590\n-999999961 121002405 999999920 121002405\n999999985 121822043 -796706494 121822043",
"output": "4613245176"
}
] | 1,044 | 268,390,400 | 0 | 141,321 |
|
0 | none | [
"none"
] | null | null | This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive.
Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast.
Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage.
The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be *n* noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1<=/<=*n*). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task. | The first line contains a single integer *n* from the problem's statement (1<=β€<=*n*<=β€<=10000). | Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes. | [
"2\n",
"3\n",
"4\n"
] | [
"1/1\n",
"8/3\n",
"2/1\n"
] | none | [
{
"input": "2",
"output": "1/1"
},
{
"input": "3",
"output": "8/3"
},
{
"input": "4",
"output": "2/1"
},
{
"input": "8",
"output": "3/1"
},
{
"input": "7",
"output": "24/7"
},
{
"input": "6",
"output": "11/3"
},
{
"input": "1",
"output": "0/1"
},
{
"input": "5",
"output": "18/5"
},
{
"input": "96",
"output": "23/3"
},
{
"input": "54",
"output": "377/57"
},
{
"input": "49",
"output": "1985714/299593"
},
{
"input": "57",
"output": "1118/171"
},
{
"input": "21",
"output": "38/7"
},
{
"input": "43",
"output": "896/129"
},
{
"input": "56",
"output": "45/7"
},
{
"input": "46",
"output": "13719/2047"
},
{
"input": "91",
"output": "704/91"
},
{
"input": "13",
"output": "306/65"
},
{
"input": "82",
"output": "7739/1025"
},
{
"input": "69",
"output": "32740246/4194303"
},
{
"input": "77",
"output": "8215881550/1073741823"
},
{
"input": "27",
"output": "320/57"
},
{
"input": "63",
"output": "128/21"
},
{
"input": "60",
"output": "94/15"
},
{
"input": "42",
"output": "45/7"
},
{
"input": "29",
"output": "89074/16385"
},
{
"input": "99",
"output": "82792/10923"
},
{
"input": "19",
"output": "2936/513"
},
{
"input": "89",
"output": "15942/2047"
},
{
"input": "356",
"output": "20036/2047"
},
{
"input": "377",
"output": "42877948701338/4398046511105"
},
{
"input": "376",
"output": "81794781/8388607"
},
{
"input": "199",
"output": "5416016912792671923933831206744/633825300114114700748351602687"
},
{
"input": "563",
"output": "13880251801665520090148870069821814422429790384486504048582895486382118580803670864520/1295112594817152713946307937882345937761604559368093014167874939825936190136805665451"
},
{
"input": "768",
"output": "32/3"
},
{
"input": "777",
"output": "242912325346/22906492245"
},
{
"input": "721",
"output": "24323770714557246/2251799813685247"
},
{
"input": "629",
"output": "49843473149688266962934/4722366482869645213695"
},
{
"input": "589",
"output": "13383001136884230836493585742/1237940039285380274899124223"
},
{
"input": "698",
"output": "261084475895343697268604849722313704014143081638278003/23945242826029513411849172299223580994042798784118785"
},
{
"input": "897",
"output": "6303910445469575479185224346195379698702/604946430081668379490443746545365709255"
},
{
"input": "100",
"output": "1548/205"
},
{
"input": "898",
"output": "54181807382553033285317903788494851/5192296858534827628530496329220097"
},
{
"input": "778",
"output": "266081190240146347461776796003542832080357458393960689731819/25108406941546723055343157692830665664409421777856138051585"
},
{
"input": "408",
"output": "481/51"
},
{
"input": "915",
"output": "24489920882263416/2329134352741105"
},
{
"input": "659",
"output": "11506864999305980525685467247724160971763964558240405270397991163704744844086671519176045618744739064/1093625362391505962186251113558810682676584715446606218212885303204976499599687961611756588511526913"
},
{
"input": "380",
"output": "17089848754/1762037865"
},
{
"input": "826",
"output": "28584112877549284603281069376719144007233968270323045/2660582536225501490205463588802620110449199864902087"
},
{
"input": "570",
"output": "81780152549/7635497415"
},
{
"input": "8947",
"output": "9221776965061394108063074537665193339719130234791259353035383369908276748232005329587397633526133839630977366288410166284245740877083371222474025826991630911939336371916388075479778450420492837649418370715068725438291562232309367997828789596177392059429297899551357796421577750411209045517378683903876507895356659650107620108241824167563644901510340265372019179673453724120802026261156279978925379653814720997930734349648232984115741566886698873835913569553263892609588155620638724704727598288959685621550249737..."
},
{
"input": "5379",
"output": "369970662679510300707473720654925140999880897391589444623141436368705516892112173326991585567498714963884175295883677699016/27543998699593691622996471168032126659516861790178384136860366469766907968179639409382883768321470201982967167709306596011"
},
{
"input": "2614",
"output": "467288820759363880397722656040592964020719930972683440703193351374700405080631777580381050558531644513848619882285655907104502715940077345333622944378242416293600447930921066507506568704500773773633/37375513539561023231108477793896786533525327931380202951304745106630862169773485150256437750311906506986637800026885384689161869077507588081685801531164378630160340372359290471078905382884178132993"
},
{
"input": "6212",
"output": "718828780749497450032379379564/52818775009509558395695966891"
},
{
"input": "3586",
"output": "1026714465140783036738474632330359205383783819874722347437634384119469704928190591460057095203615899196742515080053210107395/82631996098781074868989413504096379978550585370535152410581099409300723904538918228148651304964410605948901503127919788033"
},
{
"input": "6629",
"output": "2887307659130237302642554273423838808127851411579740408603950850448329124209429578113205979182558694432261838498919298006073282090488747091133824076367223385537560048772408256051559991222428913480943619030448796454451464748405569577077070495809049315150455270612613741854545427076463694803465943631755282225609026765330663439059215627258266275926212560658364641003632351846514061002732653254081394744335331237509798266695411546509090369792904212760685221160746857281893822469878668200610823671894328098385881817..."
},
{
"input": "9861",
"output": "48205401549715737511233606679265031152996036546952288925330285615396586708825956490117819026249283326464870369563186432330830706792989284262067110327971987394928549360770263191516012692848869076552182149927367930957872055698792311482405327082065553619653685393875355497796244989262571944371234203749880818792381890143341459002264984197001955297069409069640536538793687219440766877509028035223421720178766484630650779161811795215598122460003343408475412680746717692338/3290879900222849827608052539430658202162644..."
},
{
"input": "1649",
"output": "1103258059369546/93824992236885"
},
{
"input": "1108",
"output": "829108303866668/70368744177665"
},
{
"input": "6704",
"output": "3747509385261755254210316751920800389821077334621726013635892116/274250759553534340359161530426225084163789310938929977224768171"
},
{
"input": "8771",
"output": "3770311067804988086442280897982793233953991174834038671591744622664235928848771481024534828492198884087581040596792755307166862630362254070801530083149662062951269890316117808935983174156710048296936374305714276676638609402088826404322038255398708095256451326373150897864019805504212731596330281332804357336587199175247125958363190591680678576959242590696722761770737808874948948372052533640442104091203823298252400438183921494229420351844306480773904754871502489649028646657382266084070860349033005494965904934..."
},
{
"input": "4710",
"output": "62214231544449127/4503599627370495"
},
{
"input": "2337",
"output": "6429067313146769086191243390708972817399071295570593226/510831846955296286119449009050103061206246374061200725"
},
{
"input": "7708",
"output": "39413297508974631726391031213171923271142554912679270458193728426423636026646334915727139650713682692479792411879633606353758285856351703126/2977131414714805823690030317109266572712515013375254774912983855843898524112477893944078543723575564536883288499266264815757728270805630975"
},
{
"input": "5484",
"output": "211086977989859276474150/15111572745182864683827"
},
{
"input": "2050",
"output": "2663/205"
},
{
"input": "5157",
"output": "1244202699616577925257908854992502879105187462860652753500143979369064245723363816152901984642185318105227079924186855121298620779636669909099525176016720566980036738379840484201060840762226140891649195248344160034243456960969500434409559402400997785076262368243485151264623913446406300409714366263836544017408112602389622392168343772273329588989257854547075621647134412375571624421845467345018113608142941220069412256286427238705568845394057266549279844034564540504288723831871872613761385100219013571797574528..."
},
{
"input": "9556",
"output": "61105610582596188064013632976018238169594847263124154642386689594807697097885447064570207703083797375229286491941164437689434529990291086697099939303795410031817348526076989232509267027565641811597218817018817025267674646870116016782259903322684685324891049798873702815116504349778081974046190242424181610803910534907237209386333892868457625884598750479036332/4139057561631190052420042715397268145091328063809781231185874234711716512317727312355164552220203407039993009382683073987640607521830928131007598529502..."
},
{
"input": "7901",
"output": "1545248712235484231234951857976545065744617617020030658469109057466859986475148250693905886674893272489053581743278765525029576600572532534838775161015285299867097071351763023768221772211614326132946083723216283578235838641012739144142278748089419497887052400481010672846336748523045688232788476455926025596760805229671163941642019404158795532714920358340283273984860096000931574655035232114889550219531272686479161110317036826198314000803468166009351225032738229182153921483640756378743625620038865132469106047..."
},
{
"input": "8622",
"output": "2346970612497798292051684863778787677688918498337895665035233783242368128286257851467636458012981817792946447606933694463959158768680516918810044760234529638520868683195790661167605124671831213269266007988150361676380026247303195939614144448935172977317349504257500667344413574778276538084533093523373859439640543406392557232735743996270882880554311714917225257533788072187384837827098726745502145040757646538864148126423442625937245/15845010358053163603682882776020046370855826407419789577061413023873354878758..."
},
{
"input": "16",
"output": "4/1"
},
{
"input": "32",
"output": "5/1"
},
{
"input": "64",
"output": "6/1"
},
{
"input": "128",
"output": "7/1"
},
{
"input": "256",
"output": "8/1"
},
{
"input": "512",
"output": "9/1"
},
{
"input": "1024",
"output": "10/1"
},
{
"input": "2048",
"output": "11/1"
},
{
"input": "4096",
"output": "12/1"
},
{
"input": "8192",
"output": "13/1"
},
{
"input": "9749",
"output": "2442073850489536993602927585016307199165621395304075025177694875024679607796284569131767726963170591399402108675638309823508923120869023271471818205166195909414421298770225330343221707377312426015931814666620011704161259290684411521150904067521292013955185317667861253504565429204675752674949251324122812583751343895693341865871684651273036507250959013187746544944138162398891218537343348931977619498001506833868902121065893913936425349903046456157409756042496452120477911621545767772676861905004685673876448424..."
},
{
"input": "9803",
"output": "3273210180788326072665779956874407303695209430301336271835983946088930098968971592446649088493870209185601637297946849124915345946101994703881396454817629317067995483358046696632270413949397783371061147815617923539634120806955265009854911188530306501324761896490486400736980978270851173985761450205468514371802366150508965142349402634775190541932571215772934087049601511151015580218962449832712507548850463067009640345032653747650848021793733486635063672276271184797891474270402353352104959589784879937226619631..."
},
{
"input": "9851",
"output": "1826526826552054078590833159938859608660168689393622926697593614966325247523385486843148169222905802492343158938765988786790268008018346250425185590729454696317705467641275241528094330016670372495410419378141651879792828682641532533284092879418090040903735626785107378204659838740412715571880884453837366724444432895654221478996531206045405344762139424591887085118650734474741760565377558510969213435666224432175984910232854534478442066899535808519463012771143160247271088007881997468588868391395961360449311260..."
},
{
"input": "9859",
"output": "9737556487585955271085725726347548524587201515015415940974212696477918276264308244608931996051014559947727871548525843984646030062506833242765111803441650144268637762679345636011358150115775908768896005580214321628703868435548404239676226435996040392670484414990942576376634145721680636769353606720643265107519869112758891642047680407647874665865802380346668006876206490814588938569751811311435939234659864088396761136789798899162439354805109166450795279173094212597156554710639295669120966978022955523520243885..."
},
{
"input": "9883",
"output": "3585169013008320587733453934540655406968508071250655174069676351966247095891666666457951370861261022535907218197677774600785190516321049571099175528120462561383326438548822604717683501452327840790897918994708328294710567693091965543331903184071652577937298158191755916197924842106107578819028940776121678861464188820255086932973183740793372165945865009415954786595728774195980645745127346052687115724325004061229398519190748075580806663121266449693574278118081790297078495193488775980028423355059820522243653725..."
},
{
"input": "9901",
"output": "1838099364825091485842998238489502638352163208605775246129613825541675258139803932850287467779059152113767067936585018625986257156889365449198856216156597215096982506298229063072584364817360767528951048690402306736891920567120457283597208730483707878597646111737127186752299112571526574444743928839110241560725998077538870670908652964981237795923920133705668072188172055098674658571105524472734987571439013958589827622877679807533101349696441175687163721291688130495577638100992957486490904022264860238167307326..."
},
{
"input": "9907",
"output": "1633354865670482634892575473274177878269848372373759525765811325846788929611278968402771127221384695483915945198456285004476851582174958025985911899376499975337986661537173411180428490907671107896805361798951388912266345066652077866312114944286489782939084013056891167373799137114155515056010000276816186615290579398553645497061457349859874907706345855178899820840538136570385474191197090563464515778875629438803382571649568677198891486414476226932831825123576296441036124053704853315256369077062694497961110434..."
},
{
"input": "9923",
"output": "3760299283807515305392881669921685380087483626665387583310740045779392618490518061864438198412014466565364228291295311284131760183238506480296641521584717001189575119537493891940454933801650905249443352246953782854872613017048635048443614120554701976326536039594670095564945312513189153593550500517921950184962024588992584781239533465993501341159180970745350678104403343694528834994019085606787711931776684194936418570665879951352474415566347678136222813280481902061127946763865268688300550988394467968839427831..."
},
{
"input": "9941",
"output": "3848534499658219667049727329663074105337547166730018729906447543650907057565599099052599064685569838374730308744072576703962547115876952975258439787965733453851059665347937329225608515273601271650034928543860491219634012020498814661622840677950496926059282612940876829862296013101963794076610277533664472961283348371953697283523069103642217248197905842345904997732309684469494496541394358109215746512001993248263898375663673449385592381926788206401567212276907962968353724108187030299614264372186122509235461661..."
},
{
"input": "9949",
"output": "3077194576897431282984928614971803243979867746742349396701033435828399498432602651550466130139454008014151230282733882472359320178948173983098259522962570058314958623895543103765377873930945767189012935127692368514283490255979891724625021542152152743761996609145171758487359833408232379642800156557718632212879833607371512214716299467380836645557808645986902660996771461355482482475905883289978390754379236092379144196172866264243566348794250784667125654370098361595124994920872496780086986738805458903185126030..."
},
{
"input": "9991",
"output": "6382125632197575640171909108366323999672572433833222272392411654952806480364944179628596547334889065794512754663260535014179926913634410060297543083266944529654281991142775738499030710183320227853166673816892482763220210176769895648821886542105816/436994993873214129706097166956708350993678881411295357199729151951767944417616335439228580716318181998128654620651240845861768505204366709906692902245553277900892247131030458103436298545516643924637451297481464347472084863384057367177715867713535"
},
{
"input": "9992",
"output": "882713207347291313029281/60446290980731458735309"
},
{
"input": "9993",
"output": "12637244675354581352253260560857374/865382809755804604755082721536683"
},
{
"input": "9994",
"output": "3281117414508879498426129146296635638706673857559146714758804687655977321336441892014774756310161571021964653093136031059973674988535235552194295743721563055893425769046817776696216427896857321525164709814889404834572227298884316348149960471145881924249349826195785903333523568245055998853462086046866400139120072781311401680748431799894049040989328677849586317964939112513229643421712863477114408475285877161526889717094721450283390765651073229738491488236723709404792790505935059388430226031866694290751309103..."
},
{
"input": "9995",
"output": "1368770541403820619075110203708490210616145992745821521870208914365828115565556194877572535511077690510688277376757546565243584175363368143317667278940670502781186329534839008398699279841764334491329910860701074569229951248069967340109056226002539889667430100999595433067983400778886042165596127864919572486395941238704720403024794261441096255620000217687954366591408789194462597191661175824028310400352/93746319107175952683864071964918454730461746778024627464635174121600584812748548703282543376385452193806936..."
},
{
"input": "9996",
"output": "2016420396858486097238844042485568452071214924046/138111634978483258420134114867245645268334710595"
},
{
"input": "9997",
"output": "115045178372494165897872226686512107429178048300340407805913417043457084371526821355671616896548808082243735275331446/7880401239278895842455808020028722761015947854093089333589658680849144354299442122282853250976983128161325598061363"
},
{
"input": "9998",
"output": "4285402091468445389426164244750423198106729816197961669529078982307315890678968433526215498100171962888335447/293567822846729153486185074598667128421960318613539983838411371441526128139326055432962374798096087878991871"
},
{
"input": "9999",
"output": "396504919788033353440140876437127916065460257739670266843919813860579129943101204509709504/27160479684459814483579275845458375480686245248879150008481872658058417330177822749111965"
},
{
"input": "10000",
"output": "211285126026764324876224334024814529251789998319439411297242149907456038558/14474011154664524427946373126085988481658748083205070504932198000989141205"
}
] | 60 | 0 | 0 | 142,496 |
|
659 | Polycarp and Hay | [
"dfs and similar",
"dsu",
"graphs",
"greedy",
"sortings"
] | null | null | The farmer Polycarp has a warehouse with hay, which can be represented as an *n*<=Γ<=*m* rectangular table, where *n* is the number of rows, and *m* is the number of columns in the table. Each cell of the table contains a haystack. The height in meters of the hay located in the *i*-th row and the *j*-th column is equal to an integer *a**i*,<=*j* and coincides with the number of cubic meters of hay in the haystack, because all cells have the size of the base 1<=Γ<=1. Polycarp has decided to tidy up in the warehouse by removing an arbitrary integer amount of cubic meters of hay from the top of each stack. You can take different amounts of hay from different haystacks. Besides, it is allowed not to touch a stack at all, or, on the contrary, to remove it completely. If a stack is completely removed, the corresponding cell becomes empty and no longer contains the stack.
Polycarp wants the following requirements to hold after the reorganization:
- the total amount of hay remaining in the warehouse must be equal to *k*, - the heights of all stacks (i.e., cells containing a non-zero amount of hay) should be the same, - the height of at least one stack must remain the same as it was, - for the stability of the remaining structure all the stacks should form one connected region.
The two stacks are considered adjacent if they share a side in the table. The area is called connected if from any of the stack in the area you can get to any other stack in this area, moving only to adjacent stacks. In this case two adjacent stacks necessarily belong to the same area.
Help Polycarp complete this challenging task or inform that it is impossible. | The first line of the input contains three integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=1000) and *k* (1<=β€<=*k*<=β€<=1018)Β β the number of rows and columns of the rectangular table where heaps of hay are lain and the required total number cubic meters of hay after the reorganization.
Then *n* lines follow, each containing *m* positive integers *a**i*,<=*j* (1<=β€<=*a**i*,<=*j*<=β€<=109), where *a**i*,<=*j* is equal to the number of cubic meters of hay making the hay stack on the *i*-th row and *j*-th column of the table. | In the first line print "YES" (without quotes), if Polycarpus can perform the reorganisation and "NO" (without quotes) otherwise. If the answer is "YES" (without quotes), then in next *n* lines print *m* numbersΒ β the heights of the remaining hay stacks. All the remaining non-zero values should be equal, represent a connected area and at least one of these values shouldn't be altered.
If there are multiple answers, print any of them. | [
"2 3 35\n10 4 9\n9 9 7\n",
"4 4 50\n5 9 1 1\n5 1 1 5\n5 1 5 5\n5 5 7 1\n",
"2 4 12\n1 1 3 1\n1 6 2 4\n"
] | [
"YES\n7 0 7 \n7 7 7 \n",
"YES\n5 5 0 0 \n5 0 0 5 \n5 0 5 5 \n5 5 5 0 \n",
"NO\n"
] | In the first sample non-zero values make up a connected area, their values do not exceed the initial heights of hay stacks. All the non-zero values equal 7, and their number is 5, so the total volume of the remaining hay equals the required value *k*β=β7Β·5β=β35. At that the stack that is on the second line and third row remained unaltered. | [
{
"input": "2 3 35\n10 4 9\n9 9 7",
"output": "YES\n7 0 7 \n7 7 7 "
},
{
"input": "4 4 50\n5 9 1 1\n5 1 1 5\n5 1 5 5\n5 5 7 1",
"output": "YES\n5 5 0 0 \n5 0 0 5 \n5 0 5 5 \n5 5 5 0 "
},
{
"input": "2 4 12\n1 1 3 1\n1 6 2 4",
"output": "NO"
},
{
"input": "6 6 38\n1 100 1 100 1 100\n100 1 100 1 100 1\n1 100 1 100 1 100\n100 1 100 1 100 1\n1 100 1 100 1 100\n100 1 100 1 100 1",
"output": "NO"
},
{
"input": "6 6 300\n1 100 1 100 1 100\n100 1 100 1 100 1\n1 100 1 100 1 100\n100 1 100 1 100 1\n1 100 1 100 1 100\n100 1 100 1 100 1",
"output": "NO"
},
{
"input": "2 2 16\n5 5\n5 5",
"output": "NO"
},
{
"input": "5 5 27\n2 2 2 2 2\n2 2 9 2 2\n2 2 9 3 2\n2 2 9 2 2\n2 2 2 2 2",
"output": "YES\n0 0 0 0 0 \n0 0 9 0 0 \n0 0 9 0 0 \n0 0 9 0 0 \n0 0 0 0 0 "
},
{
"input": "1 1 1000000000000000000\n1000000000",
"output": "NO"
},
{
"input": "1 1 1\n1",
"output": "YES\n1 "
},
{
"input": "1 3 6\n3 2 2",
"output": "YES\n2 2 2 "
},
{
"input": "1 3 6\n2 3 2",
"output": "YES\n2 2 2 "
},
{
"input": "1 3 6\n2 2 3",
"output": "YES\n2 2 2 "
},
{
"input": "1 2 6\n2 3",
"output": "NO"
},
{
"input": "1 2 6\n3 2",
"output": "NO"
}
] | 2,917 | 130,355,200 | 0 | 142,649 |
Subsets and Splits