contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
733 | Sleep in Class | [
"constructive algorithms",
"data structures",
"math",
"two pointers"
] | null | null | The academic year has just begun, but lessons and olympiads have already occupied all the free time. It is not a surprise that today Olga fell asleep on the Literature. She had a dream in which she was on a stairs.
The stairs consists of *n* steps. The steps are numbered from bottom to top, it means that the lowest s... | The first line contains single integer *n* (1<=≤<=*n*<=≤<=106) — the number of steps on the stairs.
The second line contains a string *s* with the length *n* — it denotes the initial direction of pointers on the stairs. The *i*-th character of string *s* denotes the direction of the pointer above *i*-th step, and is e... | Print *n* numbers, the *i*-th of which is equal either to the duration of Olga's dream or to <=-<=1 if Olga never goes beyond the stairs, if in the beginning of sleep she was on the *i*-th step. | [
"3\nUUD\n",
"10\nUUDUDUUDDU\n"
] | [
"5 6 3 ",
"5 12 23 34 36 27 18 11 6 1 "
] | none | [
{
"input": "3\nUUD",
"output": "5 6 3 "
},
{
"input": "10\nUUDUDUUDDU",
"output": "5 12 23 34 36 27 18 11 6 1 "
},
{
"input": "10\nDUDDUUDUDD",
"output": "1 4 7 14 23 32 30 19 12 5 "
},
{
"input": "1\nD",
"output": "1 "
},
{
"input": "2\nDU",
"output": "1 1 "
... | 2,000 | 262,144,000 | 0 | 60,871 | |
0 | none | [
"none"
] | null | null | A divisor tree is a rooted tree that meets the following conditions:
- Each vertex of the tree contains a positive integer number. - The numbers written in the leaves of the tree are prime numbers. - For any inner vertex, the number within it is equal to the product of the numbers written in its children.
Manao h... | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=8). The second line contains *n* distinct space-separated integers *a**i* (2<=≤<=*a**i*<=≤<=1012). | Print a single integer — the minimum number of vertices in the divisor tree that contains each of the numbers *a**i*. | [
"2\n6 10\n",
"4\n6 72 8 4\n",
"1\n7\n"
] | [
"7\n",
"12\n",
"1\n"
] | Sample 1. The smallest divisor tree looks this way: <img class="tex-graphics" src="https://espresso.codeforces.com/8b607704e04231c95d3ba0058b84ab4e784560cf.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Sample 2. In this case you can build the following divisor tree: <img class="tex-graphics" src="https://espres... | [] | 46 | 0 | 0 | 61,092 | |
316 | Summer Homework | [
"brute force",
"data structures"
] | null | null | By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher:
You are given a sequence of integers *a*1,<=*a*2,<=...,<=*a**n*. Your task is to perform on it *m* consecutive operations of the following type:
1. For given numbers *x**i* and *v**i* assign valu... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=2·105) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=105). Then follow *m* lines, each describes an operation. Each line star... | For each query print the calculated sum modulo 1000000000 (109). | [
"5 5\n1 3 1 2 4\n2 1 4\n2 1 5\n2 2 4\n1 3 10\n2 1 5\n",
"5 4\n1 3 1 2 4\n3 1 4 1\n2 2 4\n1 2 10\n2 1 5\n"
] | [
"12\n32\n8\n50\n",
"12\n45\n"
] | none | [
{
"input": "5 5\n1 3 1 2 4\n2 1 4\n2 1 5\n2 2 4\n1 3 10\n2 1 5",
"output": "12\n32\n8\n50"
},
{
"input": "1 3\n2\n2 1 1\n1 1 3\n2 1 1",
"output": "2\n3"
},
{
"input": "11 11\n6 1 9 0 2 9 1 6 2 8 0\n2 9 9\n1 9 0\n1 1 8\n2 2 5\n2 7 11\n2 2 8\n1 3 2\n1 10 0\n2 1 8\n2 9 11\n1 9 7",
"outp... | 154 | 2,969,600 | 0 | 61,230 | |
145 | Lucky Queries | [
"data structures"
] | null | null | Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya brought home string *s* with the length of *n*. The string only consists of lucky digits. Th... | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=106,<=1<=≤<=*m*<=≤<=3·105) — the length of the string *s* and the number of queries correspondingly. The second line contains *n* lucky digits without spaces — Petya's initial string. Next *m* lines contain queries in the form described in the statement. | For each query count print an answer on a single line. | [
"2 3\n47\ncount\nswitch 1 2\ncount\n",
"3 5\n747\ncount\nswitch 1 1\ncount\nswitch 1 3\ncount\n"
] | [
"2\n1\n",
"2\n3\n2\n"
] | In the first sample the chronology of string *s* after some operations are fulfilled is as follows (the sought maximum subsequence is marked with bold):
1. 47 1. 74 1. 74 1. 747 1. 447 1. 447 1. 774 1. 774 | [] | 154 | 6,963,200 | -1 | 61,657 | |
637 | Running with Obstacles | [
"*special",
"data structures",
"dp",
"greedy"
] | null | null | A sportsman starts from point *x**start*<==<=0 and runs to point with coordinate *x**finish*<==<=*m* (on a straight line). Also, the sportsman can jump — to jump, he should first take a run of length of not less than *s* meters (in this case for these *s* meters his path should have no obstacles), and after that he can... | The first line of the input containsd four integers *n*, *m*, *s* and *d* (1<=≤<=*n*<=≤<=200<=000, 2<=≤<=*m*<=≤<=109, 1<=≤<=*s*,<=*d*<=≤<=109) — the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly.
... | If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes).
If the athlete can get from start to finish, print any way to do this in the following format:
- print a line of form "RUN X>" (where "X" should be a positive integer), if the athlete should ru... | [
"3 10 1 3\n3 4 7\n",
"2 9 2 3\n6 4\n"
] | [
"RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2\n",
"IMPOSSIBLE\n"
] | none | [
{
"input": "3 10 1 3\n3 4 7",
"output": "RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2"
},
{
"input": "2 9 2 3\n6 4",
"output": "IMPOSSIBLE"
},
{
"input": "10 100 2 8\n93 35 24 87 39 46 86 37 73 33",
"output": "RUN 23\nJUMP 2\nRUN 7\nJUMP 8\nRUN 5\nJUMP 2\nRUN 25\nJUMP 2\nRUN 11\nJUMP 3\nRUN 4... | 2,000 | 16,691,200 | 0 | 61,686 | |
825 | Tree Queries | [
"dfs and similar",
"graphs",
"trees"
] | null | null | You are given a tree consisting of *n* vertices (numbered from 1 to *n*). Initially all vertices are white. You have to process *q* queries of two different types:
1. 1 *x* — change the color of vertex *x* to black. It is guaranteed that the first query will be of this type. 1. 2 *x* — for the vertex *x*, find the m... | The first line contains two numbers *n* and *q* (3<=≤<=*n*,<=*q*<=≤<=106).
Then *n*<=-<=1 lines follow, each line containing two numbers *x**i* and *y**i* (1<=≤<=*x**i*<=<<=*y**i*<=≤<=*n*) and representing the edge between vertices *x**i* and *y**i*.
It is guaranteed that these edges form a tree.
Then *q* lines f... | For each query of type 2 output the answer to it. | [
"4 6\n1 2\n2 3\n3 4\n1 2\n1 2\n2 2\n1 3\n2 2\n2 2\n"
] | [
"3\n2\n1\n"
] | none | [
{
"input": "4 6\n1 2\n2 3\n3 4\n1 2\n1 2\n2 2\n1 3\n2 2\n2 2",
"output": "3\n2\n1"
},
{
"input": "10 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n1 3\n2 10\n1 8\n1 7\n1 6\n1 4\n2 9\n2 7\n1 5\n1 5",
"output": "1\n1\n4"
},
{
"input": "10 10\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 1... | 2,479 | 225,177,600 | 3 | 61,736 | |
453 | Little Pony and Elements of Harmony | [
"dp",
"matrices"
] | null | null | The Elements of Harmony are six supernatural artifacts representing subjective aspects of harmony. They are arguably the most powerful force in Equestria. The inside of Elements of Harmony can be seen as a complete graph with *n* vertices labeled from 0 to *n*<=-<=1, where *n* is a power of two, equal to 2*m*.
The ene... | The first line contains three integers *m*, *t* and *p* (1<=≤<=*m*<=≤<=20; 0<=≤<=*t*<=≤<=1018; 2<=≤<=*p*<=≤<=109). The following line contains *n* (*n*<==<=2*m*) integers *e*0[*i*] (1<=≤<=*e*0[*i*]<=≤<=109; 0<=≤<=*i*<=<<=*n*). The next line contains *m*<=+<=1 integers *b*[*i*] (0<=≤<=*b*[*i*]<=≤<=109; 0<=≤<=*i*<=≤<=... | Output *n* lines, the *i*-th line must contain a single integer *e**t*[*i*] modulo *p*. | [
"2 2 10000\n4 1 2 3\n0 1 0\n"
] | [
"14\n6\n6\n14\n"
] | none | [] | 30 | 0 | 0 | 61,861 | |
139 | Wallpaper | [
"implementation",
"math"
] | null | null | Having bought his own apartment, Boris decided to paper the walls in every room. Boris's flat has *n* rooms, each of which has the form of a rectangular parallelepiped. For every room we known its length, width and height of the walls in meters (different rooms can have different dimensions, including height).
Boris c... | The first line contains a positive integer *n* (1<=≤<=*n*<=≤<=500) — the number of rooms in Boris's apartment.
Each of the next *n* lines contains three space-separated positive integers — the length, width and height of the walls in a given room in meters, respectively.
The next line contains a positive integer *m* ... | Print a single number — the minimum total cost of the rolls. | [
"1\n5 5 3\n3\n10 1 100\n15 2 320\n3 19 500\n"
] | [
"640\n"
] | Note to the sample:
The total length of the walls (the perimeter) of the room is 20 m.
One roll of the first type can be cut into pieces to get three vertical 1 meter wide strips, ergo you need 7 rolls of this type, the price equals 700.
A roll of the second type can be cut into pieces to get five 2 meter wide strip... | [
{
"input": "1\n5 5 3\n3\n10 1 100\n15 2 320\n3 19 500",
"output": "640"
},
{
"input": "1\n9 10 7\n1\n7 1 3",
"output": "114"
},
{
"input": "1\n6 9 5\n3\n8 5 10\n8 5 2\n6 3 7",
"output": "12"
},
{
"input": "1\n3 3 10\n3\n5 5 1\n9 9 2\n10 1 500",
"output": "6000"
},
{
... | 154 | 2,867,200 | -1 | 61,889 | |
935 | Fafa and Ancient Mathematics | [
"dfs and similar",
"dp",
"trees"
] | null | null | Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression.
An Ahmes arithmetic expression can be defined as:
- "*d*" is an Ahmes arithmetic... | The first line contains a string *E* (1<=≤<=|*E*|<=≤<=104) — a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'.
The second line contains two space-separated integers *P* and *M* (0<=≤<=*min*(*P*,<=*M*)<=≤<=100) — the number of plus and minus operators, respectively.
It is guaranteed... | Print one line containing the answer to the problem. | [
"(1?1)\n1 0\n",
"(2?(1?2))\n1 1\n",
"((1?(5?7))?((6?2)?7))\n3 2\n",
"((1?(5?7))?((6?2)?7))\n2 3\n"
] | [
"2\n",
"1\n",
"18\n",
"16\n"
] | - The first sample will be (1 + 1) = 2. - The second sample will be (2 + (1 - 2)) = 1. - The third sample will be ((1 - (5 - 7)) + ((6 + 2) + 7)) = 18. - The fourth sample will be ((1 + (5 + 7)) - ((6 - 2) - 7)) = 16. | [
{
"input": "(1?1)\n1 0",
"output": "2"
},
{
"input": "(2?(1?2))\n1 1",
"output": "1"
},
{
"input": "((1?(5?7))?((6?2)?7))\n3 2",
"output": "18"
},
{
"input": "((1?(5?7))?((6?2)?7))\n2 3",
"output": "16"
},
{
"input": "((4?3)?(((2?(4?((4?((2?(3?(7?3)))?(((((3?(6?2)... | 46 | 0 | 0 | 61,901 | |
327 | Axis Walking | [
"bitmasks",
"combinatorics",
"constructive algorithms",
"dp",
"meet-in-the-middle"
] | null | null | Iahub wants to meet his girlfriend Iahubina. They both live in *Ox* axis (the horizontal axis). Iahub lives at point 0 and Iahubina at point *d*.
Iahub has *n* positive integers *a*1, *a*2, ..., *a**n*. The sum of those numbers is *d*. Suppose *p*1, *p*2, ..., *p**n* is a permutation of {1,<=2,<=...,<=*n*}. Then, let ... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=24). The following line contains *n* integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
The third line contains integer *k* (0<=≤<=*k*<=≤<=2). The fourth line contains *k* positive integers, representing the numbers that give Iahub bad luck. Each of thes... | Output a single integer — the answer of Iahub's dilemma modulo 1000000007 (109<=+<=7). | [
"3\n2 3 5\n2\n5 7\n",
"3\n2 2 2\n2\n1 3\n"
] | [
"1\n",
"6\n"
] | In the first case consider six possible orderings:
- [2, 3, 5]. Iahub will stop at position 2, 5 and 10. Among them, 5 is bad luck for him. - [2, 5, 3]. Iahub will stop at position 2, 7 and 10. Among them, 7 is bad luck for him. - [3, 2, 5]. He will stop at the unlucky 5. - [3, 5, 2]. This is a valid ordering. - ... | [] | 278 | 10,752,000 | 0 | 61,921 | |
852 | Exploration plan | [
"binary search",
"flows",
"graph matchings",
"shortest paths"
] | null | null | The competitors of Bubble Cup X gathered after the competition and discussed what is the best way to get to know the host country and its cities.
After exploring the map of Serbia for a while, the competitors came up with the following facts: the country has *V* cities which are indexed with numbers from 1 to *V*, and... | The first line contains four integers: *V*, *E*, *N* and *K* (1<=≤<=<=*V*<=<=≤<=<=600,<= 1<=<=≤<=<=*E*<=<=≤<=<=20000,<= 1<=<=≤<=<=*N*<=<=≤<=<=*min*(*V*,<=200),<= 1<=<=≤<=<=*K*<=<=≤<=<=*N*), number of cities, number of roads, number of teams and the smallest number of different cities they need to end up in, respectivel... | Output a single integer that represents the minimal time the teams can move for, such that they end up in at least *K* different cities or output -1 if there is no solution.
If the solution exists, result will be no greater than 1731311. | [
"6 7 5 4\n5 5 2 2 5\n1 3 3\n1 5 2\n1 6 5\n2 5 4\n2 6 7\n3 4 11\n3 5 3\n"
] | [
"3"
] | Three teams start from city 5, and two teams start from city 2. If they agree to move for 3 minutes, one possible situation would be the following: Two teams in city 2, one team in city 5, one team in city 3 , and one team in city 1. And we see that there are four different cities the teams end their journey at. | [
{
"input": "6 7 5 4\n5 5 2 2 5\n1 3 3\n1 5 2\n1 6 5\n2 5 4\n2 6 7\n3 4 11\n3 5 3",
"output": "3"
},
{
"input": "5 6 4 1\n4 4 4 4\n5 4 40\n3 2 989\n1 2 29\n4 3 18\n2 5 697\n4 3 51",
"output": "0"
},
{
"input": "15 6 9 8\n3 4 2 7 7 4 3 3 2\n13 2 9569\n15 1 8936\n12 5 3842\n13 4 5637\n12 2 ... | 15 | 0 | 0 | 62,015 | |
555 | Case of Chocolate | [
"data structures"
] | null | null | Andrewid the Android is a galaxy-known detective. Now he does not investigate any case and is eating chocolate out of boredom.
A bar of chocolate can be presented as an *n*<=×<=*n* table, where each cell represents one piece of chocolate. The columns of the table are numbered from 1 to *n* from left to right and the r... | The first line contains integers *n* (1<=≤<=*n*<=≤<=109) and *q* (1<=≤<=*q*<=≤<=2·105) — the size of the chocolate bar and the number of actions.
Next *q* lines contain the descriptions of the actions: the *i*-th of them contains numbers *x**i* and *y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=+<=*y**i*<==<=*n*<=+<=1... | Print *q* lines, the *i*-th of them should contain the number of eaten pieces as a result of the *i*-th action. | [
"6 5\n3 4 U\n6 1 L\n2 5 L\n1 6 U\n4 3 U\n",
"10 6\n2 9 U\n10 1 U\n1 10 U\n8 3 L\n10 1 L\n6 5 U\n"
] | [
"4\n3\n2\n1\n2\n",
"9\n1\n10\n6\n0\n2\n"
] | Pictures to the sample tests:
<img class="tex-graphics" src="https://espresso.codeforces.com/2ce2eba5359eb520eb9b09725b638508b03473a8.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The pieces that were eaten in the same action are painted the same color. The pieces lying on the anti-diagonal contain the numbers... | [] | 46 | 0 | -1 | 62,218 | |
217 | Bitonix' Patrol | [
"bitmasks",
"brute force",
"combinatorics",
"dfs and similar",
"math"
] | null | null | Byteland is trying to send a space mission onto the Bit-X planet. Their task is complicated by the fact that the orbit of the planet is regularly patrolled by Captain Bitonix, the leader of the space forces of Bit-X.
There are *n* stations around Bit-X numbered clockwise from 1 to *n*. The stations are evenly placed o... | The first line of the input contains three integers *n* (2<=≤<=*n*<=≤<=1000) — the number of stations, *m* (1<=≤<=*m*<=≤<=120) — the distance between adjacent stations, and *t* (1<=≤<=*t*<=≤<=10000) — the number of fuel tanks owned by Captain Bitonix.
The second line of the input contains *t* space-separated integers ... | Output a single number — the number of distinct subsets of tanks that the Bytelandian space agency can destroy in order to prevent Captain Bitonix from completing a patrol, modulo 109<=+<=7. | [
"7 6 5\n5 4 12 6 5\n",
"3 60 2\n10 100\n"
] | [
"6\n",
"4\n"
] | All the fuel tanks are distinct, even if some of them have the same capacity. | [] | 312 | 9,932,800 | 0 | 62,265 | |
758 | Broken Tree | [
"dfs and similar",
"dp",
"graphs",
"greedy",
"trees"
] | null | null | You are given a tree that has *n* vertices, which are numbered from 1 to *n*, where the vertex number one is the root. Each edge has weight *w**i* and strength *p**i*.
Botanist Innokentiy, who is the only member of the jury of the Olympiad in Informatics, doesn't like broken trees.
The tree is broken if there is suc... | The first line contains the integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of vertices in the tree. The next *n*<=-<=1 lines contains the description of edges. Each line contains four integers *x*, *y*, *w*, *p* (1<=≤<=*x*,<=*y*<=≤<=*n*,<=1<=≤<=*w*<=≤<=109,<=0<=≤<=*p*<=≤<=109), where *x* and *y* — vertices which connec... | If it is impossible to get unbroken tree from the given tree, print -1 in the only line.
Otherwise, the output data should contain *n* lines:
In the first line print the number *n* — the number of vertices on the tree.
In the next *n*<=-<=1 lines print the description of edges of the resulting tree. Each line shoul... | [
"3\n1 3 5 7\n3 2 4 3\n",
"4\n1 3 2 3\n3 4 5 1\n3 2 3 3\n",
"5\n1 2 2 4\n2 4 1 9\n4 5 5 6\n4 3 4 8\n",
"7\n1 2 5 2\n2 3 4 3\n1 4 3 7\n4 5 4 1\n4 6 3 2\n6 7 1 6\n"
] | [
"3\n1 3 5 7\n3 2 4 3\n",
"-1",
"5\n1 2 2 4\n2 4 1 9\n4 5 1 2\n4 3 2 6\n",
"7\n1 2 5 2\n2 3 2 1\n1 4 3 7\n4 5 3 0\n4 6 3 2\n6 7 1 6\n"
] | none | [] | 2,261 | 122,265,600 | -1 | 62,395 | |
703 | Mishka and Interesting sum | [
"data structures"
] | null | null | Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her with array of non-negative integers *a*1,<=*a*2,<=...,<=*a**n* of *n* elements!
Mishka loved the array and she instantly decided to determine its beauty value, but she is too little and can't process large arrays. ... | The first line of the input contains single integer *n* (1<=≤<=*n*<=≤<=1<=000<=000) — the number of elements in the array.
The second line of the input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — array elements.
The third line of the input contains single integer *m* (1<=≤<=*m*<=≤<=1<=00... | Print *m* non-negative integers — the answers for the queries in the order they appear in the input. | [
"3\n3 7 8\n1\n1 3\n",
"7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5\n"
] | [
"0\n",
"0\n3\n1\n3\n2\n"
] | In the second sample:
There is no integers in the segment of the first query, presented even number of times in the segment — the answer is 0.
In the second query there is only integer 3 is presented even number of times — the answer is 3.
In the third query only integer 1 is written down — the answer is 1.
In the ... | [
{
"input": "3\n3 7 8\n1\n1 3",
"output": "0"
},
{
"input": "7\n1 2 1 3 3 2 3\n5\n4 7\n4 5\n1 3\n1 7\n1 5",
"output": "0\n3\n1\n3\n2"
},
{
"input": "10\n1 2 4 1 1 1 1 1 1 4\n55\n5 8\n3 9\n6 8\n4 6\n4 10\n2 8\n1 5\n7 8\n8 9\n7 9\n5 6\n8 10\n9 9\n2 2\n3 3\n3 7\n1 8\n2 3\n4 9\n8 8\n10 10\n1 ... | 0 | 0 | -1 | 62,513 | |
0 | none | [
"none"
] | null | null | Programmer Sasha is a student at MIPT (Moscow Institute of Physics and Technology) and he needs to make a laboratory work to pass his finals.
A laboratory unit is a plane with standard coordinate axes marked on it. Physicists from Moscow Institute of Physics and Technology charged the axes by large electric charges: a... | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of points marked on the plane.
Each of the next *n* lines contains two integers *x**i* and *y**i* (<=-<=108<=≤<=*x**i*,<=*y**i*<=≤<=108) — the coordinates of the *i*-th point. It is guaranteed that no two points coincide. | Print a single integer — the square of the minimum possible diameter of the set. | [
"3\n1 10\n1 20\n1 30\n",
"2\n1 10\n10 1\n"
] | [
"0\n",
"2\n"
] | In the first sample Sasha puts electrons at all points, all particles eventually fall at a single point (1, 0).
In the second sample Sasha puts an electron at point (1, 10), and a proton at point (10, 1). The result is a set of two points (1, 0) and (0, 1), which has a diameter of <img align="middle" class="tex-formul... | [] | 62 | 0 | 0 | 62,554 | |
843 | Maximum Flow | [
"flows",
"graphs"
] | null | null | You are given a directed graph, consisting of *n* vertices and *m* edges. The vertices *s* and *t* are marked as source and sink correspondingly. Additionally, there are no edges ending at *s* and there are no edges beginning in *t*.
The graph was constructed in a following way: initially each edge had capacity *c**i*... | The first line of input data contains four positive integers *n*,<=*m*,<=*s*,<=*t* (2<=≤<=*n*<=≤<=100, 1<=≤<=*m*<=≤<=1000, 1<=≤<=*s*,<=*t*<=≤<=*n*, *s*<=≠<=*t*) — the number of vertices, the number of edges, index of source vertex and index of sink vertex correspondingly.
Each of next *m* lines of input data contain n... | In the first line print single non-negative integer *k* — minimum number of edges, which should be saturated in maximum flow.
In each of next *m* lines print two integers *f**i*,<=*c**i* (1<=≤<=*c**i*<=≤<=109, 0<=≤<=*f**i*<=≤<=*c**i*) — the flow value passing through edge *i* and capacity of edge *i*.
This data shou... | [
"5 6 1 5\n1 2 1\n2 3 1\n3 5 1\n1 4 1\n4 3 0\n4 5 1\n"
] | [
"2\n3 3\n3 8\n3 4\n4 4\n0 5\n4 9\n"
] | The illustration for second sample case. The saturated edges are marked dark, while edges with *g*<sub class="lower-index">*i*</sub> = 0 are marked with dotted line. The integer on edge is the index of this edge in input list. <img class="tex-graphics" src="https://espresso.codeforces.com/a80cb6067d5d58458863f416f6ac95... | [] | 31 | 0 | -1 | 62,633 | |
396 | On Sum of Number of Inversions in Permutations | [
"combinatorics",
"math"
] | null | null | You are given a permutation *p*. Calculate the total number of inversions in all permutations that lexicographically do not exceed the given one.
As this number can be very large, print it modulo 1000000007 (109<=+<=7). | The first line contains a single integer *n* (1<=≤<=*n*<=≤<=106) — the length of the permutation. The second line contains *n* distinct integers *p*1,<=*p*2,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*). | Print a single number — the answer to the problem modulo 1000000007 (109<=+<=7). | [
"2\n2 1\n",
"3\n2 1 3\n"
] | [
"1\n",
"2\n"
] | Permutation *p* of length *n* is the sequence that consists of *n* distinct integers, each of them is from 1 to *n*.
An inversion of permutation *p*<sub class="lower-index">1</sub>, *p*<sub class="lower-index">2</sub>, ..., *p*<sub class="lower-index">*n*</sub> is a pair of indexes (*i*, *j*), such that *i* < *j* a... | [] | 46 | 4,608,000 | -1 | 62,638 | |
0 | none | [
"none"
] | null | null | Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string *s* initially.
Evolution of the species is described as a sequence of changes... | The first line contains the string *s* (1<=≤<=|*s*|<=≤<=105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".
The next line contains single integer *q* (1<=≤<=*q*<=≤<=105) — the number of events.
After that, *q* lines follow, each describes one event. Each of t... | For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA. | [
"ATGCATGC\n4\n2 1 8 ATGC\n2 2 6 TTT\n1 4 T\n2 2 6 TA\n",
"GAGTTGTTAA\n6\n2 3 4 TATGGTG\n1 1 T\n1 6 G\n2 5 9 AGTAATA\n1 10 G\n2 2 6 TTGT\n"
] | [
"8\n2\n4\n",
"0\n3\n1\n"
] | Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matche... | [] | 46 | 0 | 0 | 62,643 | |
917 | MADMAX | [
"dfs and similar",
"dp",
"games",
"graphs"
] | null | null | As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she's not the best at games. The game is played on a directed acyclic graph (a DAG) with *n* vertices and *m* edges. There's a character written on each edge, a l... | The first line of input contains two integers *n* and *m* (2<=≤<=*n*<=≤<=100, ).
The next *m* lines contain the edges. Each line contains two integers *v*, *u* and a lowercase English letter *c*, meaning there's an edge from *v* to *u* written *c* on it (1<=≤<=*v*,<=*u*<=≤<=*n*, *v*<=≠<=*u*). There's at most one edge ... | Print *n* lines, a string of length *n* in each one. The *j*-th character in *i*-th line should be 'A' if Max will win the game in case her marble is initially at vertex *i* and Lucas's marble is initially at vertex *j*, and 'B' otherwise. | [
"4 4\n1 2 b\n1 3 a\n2 4 c\n3 4 b\n",
"5 8\n5 3 h\n1 2 c\n3 1 c\n3 2 r\n5 1 r\n4 3 z\n5 4 r\n5 2 h\n"
] | [
"BAAA\nABAA\nBBBA\nBBBB\n",
"BABBB\nBBBBB\nAABBB\nAAABA\nAAAAB\n"
] | Here's the graph in the first sample test case:
Here's the graph in the second sample test case: | [
{
"input": "4 4\n1 2 b\n1 3 a\n2 4 c\n3 4 b",
"output": "BAAA\nABAA\nBBBA\nBBBB"
},
{
"input": "5 8\n5 3 h\n1 2 c\n3 1 c\n3 2 r\n5 1 r\n4 3 z\n5 4 r\n5 2 h",
"output": "BABBB\nBBBBB\nAABBB\nAAABA\nAAAAB"
},
{
"input": "2 1\n1 2 q",
"output": "BA\nBB"
},
{
"input": "8 20\n2 4 ... | 530 | 2,355,200 | 3 | 62,702 | |
632 | Longest Subsequence | [
"brute force",
"math",
"number theory"
] | null | null | You are given array *a* with *n* elements and the number *m*. Consider some subsequence of *a* and the value of least common multiple (LCM) of its elements. Denote LCM as *l*. Find any longest subsequence of *a* with the value *l*<=≤<=*m*.
A subsequence of *a* is an array we can get by erasing some elements of *a*. It... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=106) — the size of the array *a* and the parameter from the problem statement.
The second line contains *n* integers *a**i* (1<=≤<=*a**i*<=≤<=109) — the elements of *a*. | In the first line print two integers *l* and *k**max* (1<=≤<=*l*<=≤<=*m*,<=0<=≤<=*k**max*<=≤<=*n*) — the value of LCM and the number of elements in optimal subsequence.
In the second line print *k**max* integers — the positions of the elements from the optimal subsequence in the ascending order.
Note that you can fin... | [
"7 8\n6 2 9 2 7 2 3\n",
"6 4\n2 2 2 3 3 3\n"
] | [
"6 5\n1 2 4 6 7\n",
"2 3\n1 2 3\n"
] | none | [
{
"input": "7 8\n6 2 9 2 7 2 3",
"output": "6 5\n1 2 4 6 7"
},
{
"input": "6 4\n2 2 2 3 3 3",
"output": "2 3\n1 2 3"
},
{
"input": "10 50\n39 22 60 88 11 65 41 85 65 100",
"output": "22 2\n2 5"
},
{
"input": "100 343\n999 284 486 785 176 742 856 415 992 601 600 122 460 214 33... | 217 | 22,220,800 | -1 | 62,710 | |
261 | Maxim and Increasing Subsequence | [
"dp"
] | null | null | Maxim loves sequences, especially those that strictly increase. He is wondering, what is the length of the longest increasing subsequence of the given sequence *a*?
Sequence *a* is given as follows:
- the length of the sequence equals *n*<=×<=*t*; - (1<=≤<=*i*<=≤<=*n*<=×<=*t*), where operation means taking the r... | The first line contains four integers *k*, *n*, *maxb* and *t* (1<=≤<=*k*<=≤<=10; 1<=≤<=*n*,<=*maxb*<=≤<=105; 1<=≤<=*t*<=≤<=109; *n*<=×<=*maxb*<=≤<=2·107). Each of the next *k* lines contain *n* integers *b*1,<=*b*2,<=...,<=*b**n* (1<=≤<=*b**i*<=≤<=*maxb*).
Note that for each variant of the sequence *a* the values *n... | Print *k* integers — the answers for the variants of the sequence *a*. Print the answers in the order the variants follow in the input. | [
"3 3 5 2\n3 2 1\n1 2 3\n2 3 1\n"
] | [
"2\n3\n3\n"
] | none | [] | 60 | 0 | 0 | 62,842 | |
208 | Solitaire | [
"dfs and similar",
"dp"
] | null | null | A boy named Vasya wants to play an old Russian solitaire called "Accordion". In this solitaire, the player must observe the following rules:
- A deck of *n* cards is carefully shuffled, then all *n* cards are put on the table in a line from left to right; - Before each move the table has several piles of cards lying... | The first input line contains a single integer *n* (1<=≤<=*n*<=≤<=52) — the number of cards in Vasya's deck. The next line contains *n* space-separated strings *c*1,<=*c*2,<=...,<=*c**n*, where string *c**i* describes the *i*-th card on the table. Each string *c**i* consists of exactly two characters, the first one rep... | On a single line print the answer to the problem: string "YES" (without the quotes) if completing the solitaire is possible, string "NO" (without the quotes) otherwise. | [
"4\n2S 2S 2C 2C\n",
"2\n3S 2C\n"
] | [
"YES\n",
"NO\n"
] | In the first sample you can act like that:
- put the 4-th pile on the 1-st one; - put the 3-rd pile on the 2-nd one; - put the 2-nd pile on the 1-st one.
In the second sample there is no way to complete the solitaire. | [
{
"input": "4\n2S 2S 2C 2C",
"output": "YES"
},
{
"input": "2\n3S 2C",
"output": "NO"
},
{
"input": "5\n2S 2S 4S 3S 2S",
"output": "YES"
},
{
"input": "5\n5S 5S 7S 4S 3H",
"output": "NO"
},
{
"input": "5\n7S 7S 4S 8H 4H",
"output": "NO"
},
{
"input": "... | 46 | 409,600 | 3 | 62,863 | |
0 | none | [
"none"
] | null | null | User ainta likes trees. This time he is going to make an undirected tree with *n* vertices numbered by integers from 1 to *n*. The tree is weighted, so each edge of the tree will have some integer weight.
Also he has an array *t*: *t*[1],<=*t*[2],<=...,<=*t*[*n*]. At first all the elements of the array are initialized... | The first line contains a single integer *n* (5<=≤<=*n*<=≤<=105). | Print *n*<=-<=1 lines containing the description of the edges. The *i*-th line should contain three space-separated integers *u**i*,<=*v**i*,<=*c**i* (1<=≤<=*u**i*<=<<=*v**i*<=≤<=*n*; 1<=≤<=*c**i*<=≤<=105) — two vertices connected by the edge, and the weight of the edge.
Next print lines containing the good pairs.... | [
"7"
] | [
"1 4 1\n1 2 2\n2 3 5\n3 5 3\n2 6 2\n6 7 3\n4 5\n5 6\n5 7"
] | ⌊*x*⌋ is the largest integer not greater than *x*.
You can find the definition of a tree by the following link: http://en.wikipedia.org/wiki/Tree_(graph_theory)
You can also find the definition of the shortest path by the following link: http://en.wikipedia.org/wiki/Shortest_path_problem
The tree and the array *t* i... | [] | 30 | 0 | 0 | 62,934 | |
123 | Squares | [
"math"
] | null | null | You are given an infinite checkered field. You should get from a square (*x*1; *y*1) to a square (*x*2; *y*2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any square, you can move to any other side-neighboring one.
A square (*x*; ... | The only line contains integers *a*, *b*, *x*1, *y*1, *x*2 and *y*2 — the parameters of the bad squares, the coordinates of the initial and the final squares correspondingly (2<=≤<=*a*,<=*b*<=≤<=109 and |*x*1|,|*y*1|,|*x*2|,|*y*2|<=≤<=109). It is guaranteed that the initial and the final square aren't bad. | Print a single number — the minimum number of bad cells that one will have to visit in order to travel from square (*x*1; *y*1) to square (*x*2; *y*2). | [
"2 2 1 0 0 1\n",
"2 2 10 11 0 1\n",
"2 4 3 -1 3 7\n"
] | [
"1\n",
"5\n",
"2\n"
] | In the third sample one of the possible paths in (3;-1)->(3;0)->(3;1)->(3;2)->(4;2)->(4;3)->(4;4)->(4;5)->(4;6)->(4;7)->(3;7). Squares (3;1) and (4;4) are bad. | [
{
"input": "2 2 1 0 0 1",
"output": "1"
},
{
"input": "2 2 10 11 0 1",
"output": "5"
},
{
"input": "2 4 3 -1 3 7",
"output": "2"
},
{
"input": "2 2 9 10 -10 -11",
"output": "10"
},
{
"input": "3 2 -11 -10 10 11",
"output": "7"
},
{
"input": "3 2 11 -12... | 154 | 0 | 0 | 63,004 | |
466 | Wonder Room | [
"brute force",
"math"
] | null | null | The start of the new academic year brought about the problem of accommodation students into dormitories. One of such dormitories has a *a*<=×<=*b* square meter wonder room. The caretaker wants to accommodate exactly *n* students there. But the law says that there must be at least 6 square meters per student in a room (... | The first line contains three space-separated integers *n*, *a* and *b* (1<=≤<=*n*,<=*a*,<=*b*<=≤<=109) — the number of students and the sizes of the room. | Print three integers *s*, *a*1 and *b*1 (*a*<=≤<=*a*1; *b*<=≤<=*b*1) — the final area of the room and its sizes. If there are multiple optimal solutions, print any of them. | [
"3 3 5\n",
"2 4 4\n"
] | [
"18\n3 6\n",
"16\n4 4\n"
] | none | [
{
"input": "3 3 5",
"output": "18\n3 6"
},
{
"input": "2 4 4",
"output": "16\n4 4"
},
{
"input": "1 1 1",
"output": "6\n1 6"
},
{
"input": "1 1000000000 1000000000",
"output": "1000000000000000000\n1000000000 1000000000"
},
{
"input": "8 7 5",
"output": "48\n8... | 46 | 0 | 0 | 63,014 | |
617 | Chocolate | [
"combinatorics"
] | null | null | Bob loves everything sweet. His favorite chocolate bar consists of pieces, each piece may contain a nut. Bob wants to break the bar of chocolate into multiple pieces so that each part would contain exactly one nut and any break line goes between two adjacent pieces.
You are asked to calculate the number of ways he can... | The first line of the input contains integer *n* (1<=≤<=*n*<=≤<=100) — the number of pieces in the chocolate bar.
The second line contains *n* integers *a**i* (0<=≤<=*a**i*<=≤<=1), where 0 represents a piece without the nut and 1 stands for a piece with the nut. | Print the number of ways to break the chocolate into multiple parts so that each part would contain exactly one nut. | [
"3\n0 1 0\n",
"5\n1 0 1 0 1\n"
] | [
"1\n",
"4\n"
] | In the first sample there is exactly one nut, so the number of ways equals 1 — Bob shouldn't make any breaks.
In the second sample you can break the bar in four ways:
10|10|1
1|010|1
10|1|01
1|01|01 | [
{
"input": "3\n0 1 0",
"output": "1"
},
{
"input": "5\n1 0 1 0 1",
"output": "4"
},
{
"input": "10\n0 0 1 0 0 0 1 1 0 1",
"output": "8"
},
{
"input": "20\n0 0 0 0 1 1 1 0 1 0 0 1 0 1 1 0 1 1 1 0",
"output": "24"
},
{
"input": "50\n0 1 1 1 1 1 1 0 1 1 0 1 1 1 0 0 1... | 77 | 2,150,400 | -1 | 63,080 | |
329 | Evil | [
"math"
] | null | null | There are *n* cities on a two dimensional Cartesian plane. The distance between two cities is equal to the Manhattan distance between them (see the Notes for definition). A Hamiltonian cycle of the cities is defined as a permutation of all *n* cities. The length of this Hamiltonian cycle is defined as the sum of the di... | The first line contains an integer *n* (3<=≤<=*n*<=≤<=105). Then *n* lines follow, each consisting of two integers *x**i* and *y**i* (0<=≤<=*x**i*,<=*y**i*<=≤<=109), denoting the coordinates of a city. All given points will be distinct. | A single line denoting the longest possible length of a Hamiltonian cycle of the given cities. You should not output the cycle, only its length.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"4\n1 1\n1 2\n2 1\n2 2\n"
] | [
"6\n"
] | In the example, one of the possible Hamiltonian cycles with length 6 is (1, 1) (1, 2) (2, 1) (2, 2). There does not exist any other Hamiltonian cycle with a length greater than 6.
The Manhattan distance between two cities (*x*<sub class="lower-index">*i*</sub>, *y*<sub class="lower-index">*i*</sub>) and (*x*<sub class... | [] | 3,000 | 67,379,200 | 0 | 63,176 | |
293 | Close Vertices | [
"data structures",
"divide and conquer",
"trees"
] | null | null | You've got a weighted tree, consisting of *n* vertices. Each edge has a non-negative weight. The length of the path between any two vertices of the tree is the number of edges in the path. The weight of the path is the total weight of all edges it contains.
Two vertices are close if there exists a path of length at m... | The first line contains three integers *n*, *l* and *w* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*l*<=≤<=*n*,<=0<=≤<=*w*<=≤<=109). The next *n*<=-<=1 lines contain the descriptions of the tree edges. The *i*-th line contains two integers *p**i*,<=*w**i* (1<=≤<=*p**i*<=<<=(*i*<=+<=1),<=0<=≤<=*w**i*<=≤<=104), that mean that the *i*... | Print a single integer — the number of close pairs.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"4 4 6\n1 3\n1 4\n1 3\n",
"6 2 17\n1 3\n2 5\n2 13\n1 6\n5 9\n"
] | [
"4\n",
"9\n"
] | none | [] | 60 | 0 | 0 | 63,183 | |
796 | Exam Cheating | [
"binary search",
"dp"
] | null | null | Zane and Zane's crush have just decided to date! However, the girl is having a problem with her Physics final exam, and needs your help.
There are *n* questions, numbered from 1 to *n*. Question *i* comes before question *i*<=+<=1 (1<=≤<=*i*<=<<=*n*). Each of the questions cannot be guessed on, due to the huge pena... | The first line contains three integers *n*, *p*, and *k* (1<=≤<=*n*,<=*p*<=≤<=1,<=000, 1<=≤<=*k*<=≤<=*min*(*n*,<=50)) — the number of questions, the maximum number of times the girl can glance, and the maximum number of consecutive questions that can be looked at in one time glancing, respectively.
The second line sta... | Print one integer — the maximum number of questions the girl can answer correctly. | [
"6 2 3\n3 1 3 6\n4 1 2 5 6\n",
"8 3 3\n4 1 3 5 6\n5 2 4 6 7 8\n"
] | [
"4",
"7"
] | Let (*x*, *l*, *r*) denote the action of looking at all questions *i* such that *l* ≤ *i* ≤ *r* on the answer sheet of the *x*-th genius.
In the first sample, the girl could get 4 questions correct by performing sequence of actions (1, 1, 3) and (2, 5, 6).
In the second sample, the girl could perform sequence of acti... | [] | 62 | 716,800 | 0 | 63,418 | |
140 | New Year Contest | [
"greedy",
"sortings"
] | null | null | As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are *n* problems for the contest. The penalty time ... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of the problems. The next line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=720) — each number shows how much time in minutes Gennady will spend writing a solution to the problem. | Print two integers — the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy. | [
"3\n30 330 720\n"
] | [
"2 10\n"
] | In the sample, one of Gennady's possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the ti... | [
{
"input": "3\n30 330 720",
"output": "2 10"
},
{
"input": "1\n720",
"output": "0 0"
},
{
"input": "5\n100 200 300 400 500",
"output": "3 250"
},
{
"input": "7\n120 110 100 110 120 120 50",
"output": "6 420"
},
{
"input": "3\n350 340 360",
"output": "2 340"
... | 124 | 307,200 | 3 | 63,588 | |
297 | Color the Carpet | [
"constructive algorithms"
] | null | null | Even polar bears feel cold when lying on the ice. Therefore, a polar bear Alice is going to make a carpet. The carpet can be viewed as a grid with height *h* and width *w*. Then the grid is divided into *h*<=×<=*w* squares. Alice is going to assign one of *k* different colors to each square. The colors are numbered fro... | The first line contains three integers *h*,<=*w*,<=*k* (2<=≤<=*h*,<=*w*<=≤<=1000,<=1<=≤<=*k*<=≤<=*w*·*h*).
The next 2*h*<=-<=1 lines describe the color constraints from top to bottom, left to right. They contain *w*<=-<=1,<=*w*,<=*w*<=-<=1,<=*w*,<=...,<=*w*<=-<=1 characters respectively. Each color constraint is repre... | If there is a coloring that satisfies at least of the color constraints, print "YES" (without quotes) in the first line. In each of the next *h* lines, print *w* integers describing the coloring.
Otherwise, print "NO" (without quotes). | [
"3 4 4\nENE\nNNEE\nNEE\nENEN\nENN\n"
] | [
"YES\n1 1 2 2\n3 4 1 1\n3 3 2 4"
] | none | [] | 154 | 307,200 | 0 | 63,902 | |
226 | The table | [
"constructive algorithms",
"greedy"
] | null | null | Harry Potter has a difficult homework. Given a rectangular table, consisting of *n*<=×<=*m* cells. Each cell of the table contains the integer. Harry knows how to use two spells: the first spell change the sign of the integers in the selected row, the second — in the selected column. Harry's task is to make non-negativ... | The first line contains two integers *n* and *m* (1<=≤<=*n*,<= *m*<=≤<=100) — the number of rows and the number of columns.
Next *n* lines follow, each contains *m* integers: *j*-th integer in the *i*-th line is *a**i*,<=*j* (|*a**i*,<=*j*|<=≤<=100), the number in the *i*-th row and *j*-th column of the table.
The r... | In the first line print the number *a* — the number of required applications of the first spell. Next print *a* space-separated integers — the row numbers, you want to apply a spell. These row numbers must be distinct!
In the second line print the number *b* — the number of required applications of the second spell. N... | [
"4 1\n-1\n-1\n-1\n-1\n",
"2 4\n-1 -1 -1 2\n1 1 1 1\n"
] | [
"4 1 2 3 4 \n0 \n",
"1 1 \n1 4 \n"
] | none | [
{
"input": "4 1\n-1\n-1\n-1\n-1",
"output": "4 1 2 3 4 \n0 "
},
{
"input": "2 4\n-1 -1 -1 2\n1 1 1 1",
"output": "1 1 \n1 4 "
},
{
"input": "10 5\n1 7 1 6 -3\n8 -8 0 -7 -8\n7 -10 -8 -3 6\n-3 0 -9 0 -3\n-1 5 -2 -9 10\n-2 9 2 0 7\n5 0 -1 -10 6\n7 -8 -3 -9 1\n-5 10 -10 5 9\n-7 4 -8 0 -4",
... | 0 | 0 | -1 | 63,984 | |
546 | Soldier and Number Game | [
"constructive algorithms",
"dp",
"math",
"number theory"
] | null | null | Two soldiers are playing a game. At the beginning first of them chooses a positive integer *n* and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer *x*<=><=1, such that *n* is divisible by *x* and replacing *n* wit... | First line of input consists of single integer *t* (1<=≤<=*t*<=≤<=1<=000<=000) denoting number of games soldiers play.
Then follow *t* lines, each contains pair of integers *a* and *b* (1<=≤<=*b*<=≤<=*a*<=≤<=5<=000<=000) defining the value of *n* for a game. | For each game output a maximum score that the second soldier can get. | [
"2\n3 1\n6 3\n"
] | [
"2\n5\n"
] | none | [
{
"input": "2\n3 1\n6 3",
"output": "2\n5"
},
{
"input": "8\n7 1\n263 262\n1000003 1000002\n5000000 4999995\n2 1\n7 4\n12345 12345\n10 1",
"output": "8\n1\n1\n23\n1\n4\n0\n15"
},
{
"input": "3\n1 1\n5000000 1\n5000000 5000000",
"output": "0\n18703742\n0"
}
] | 3,000 | 42,086,400 | 0 | 64,107 | |
576 | Invariance of Tree | [
"constructive algorithms",
"dfs and similar",
"greedy",
"trees"
] | null | null | A tree of size *n* is an undirected connected graph consisting of *n* vertices without cycles.
Consider some tree with *n* vertices. We call a tree invariant relative to permutation *p*<==<=*p*1*p*2... *p**n*, if for any two vertices of the tree *u* and *v* the condition holds: "vertices *u* and *v* are connected by a... | The first line contains number *n* (1<=≤<=*n*<=≤<=105) — the size of the permutation (also equal to the size of the sought tree).
The second line contains permutation *p**i* (1<=≤<=*p**i*<=≤<=*n*). | If the sought tree does not exist, print "NO" (without the quotes).
Otherwise, print "YES", and then print *n*<=-<=1 lines, each of which contains two integers — the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices wi... | [
"4\n4 3 2 1\n",
"3\n3 1 2\n"
] | [
"YES\n4 1\n4 2\n1 3\n",
"NO\n"
] | In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.
It can be shown that in the second sample test no tree satisfies the given condition. | [
{
"input": "4\n4 3 2 1",
"output": "YES\n4 1\n4 2\n1 3"
},
{
"input": "3\n3 1 2",
"output": "NO"
},
{
"input": "3\n3 2 1",
"output": "YES\n2 1\n2 3"
},
{
"input": "4\n3 4 1 2",
"output": "YES\n4 2\n4 1\n2 3"
},
{
"input": "5\n5 3 2 1 4",
"output": "NO"
},
... | 46 | 512,000 | 0 | 64,177 | |
622 | Ants in Leaves | [
"dfs and similar",
"greedy",
"sortings",
"trees"
] | null | null | Tree is a connected graph without cycles. A leaf of a tree is any vertex connected with exactly one other vertex.
You are given a tree with *n* vertices and a root in the vertex 1. There is an ant in each leaf of the tree. In one second some ants can simultaneously go to the parent vertex from the vertex they were in.... | The first line contains integer *n* (2<=≤<=*n*<=≤<=5·105) — the number of vertices in the tree.
Each of the next *n*<=-<=1 lines contains two integers *x**i*,<=*y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*) — the ends of the *i*-th edge. It is guaranteed that you are given the correct undirected tree. | Print the only integer *t* — the minimal time required for all ants to be in the root of the tree. | [
"12\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n3 9\n8 10\n8 11\n8 12\n",
"2\n2 1\n"
] | [
"6\n",
"1\n"
] | none | [
{
"input": "12\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n3 9\n8 10\n8 11\n8 12",
"output": "6"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "10\n4 10\n6 10\n10 5\n10 7\n8 10\n4 2\n9 10\n4 1\n3 10",
"output": "8"
},
{
"input":... | 77 | 7,168,000 | 0 | 64,423 | |
438 | The Child and Binary Tree | [
"combinatorics",
"divide and conquer",
"fft",
"number theory"
] | null | null | Our child likes computer science very much, especially he likes binary trees.
Consider the sequence of *n* distinct positive integers: *c*1,<=*c*2,<=...,<=*c**n*. The child calls a vertex-weighted rooted binary tree good if and only if for every vertex *v*, the weight of *v* is in the set {*c*1,<=*c*2,<=...,<=*c**n*}.... | The first line contains two integers *n*,<=*m* (1<=≤<=*n*<=≤<=105; 1<=≤<=*m*<=≤<=105). The second line contains *n* space-separated pairwise distinct integers *c*1,<=*c*2,<=...,<=*c**n*. (1<=≤<=*c**i*<=≤<=105). | Print *m* lines, each line containing a single integer. The *i*-th line must contain the number of good vertex-weighted rooted binary trees whose weight exactly equal to *i*. Print the answers modulo 998244353 (7<=×<=17<=×<=223<=+<=1, a prime number). | [
"2 3\n1 2\n",
"3 10\n9 4 3\n",
"5 10\n13 10 6 4 15\n"
] | [
"1\n3\n9\n",
"0\n0\n1\n1\n0\n2\n4\n2\n6\n15\n",
"0\n0\n0\n1\n0\n1\n0\n2\n0\n5\n"
] | In the first example, there are 9 good vertex-weighted rooted binary trees whose weight exactly equal to 3: | [
{
"input": "2 3\n1 2",
"output": "1\n3\n9"
},
{
"input": "3 10\n9 4 3",
"output": "0\n0\n1\n1\n0\n2\n4\n2\n6\n15"
},
{
"input": "5 10\n13 10 6 4 15",
"output": "0\n0\n0\n1\n0\n1\n0\n2\n0\n5"
},
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "10 100\n9 95 37 56 3... | 389 | 29,286,400 | 3 | 64,451 | |
847 | Travel Cards | [
"greedy",
"implementation",
"sortings"
] | null | null | In the evening Polycarp decided to analyze his today's travel expenses on public transport.
The bus system in the capital of Berland is arranged in such a way that each bus runs along the route between two stops. Each bus has no intermediate stops. So each of the buses continuously runs along the route from one stop t... | The first line contains five integers *n*,<=*a*,<=*b*,<=*k*,<=*f* (1<=≤<=*n*<=≤<=300, 1<=≤<=*b*<=<<=*a*<=≤<=100, 0<=≤<=*k*<=≤<=300, 1<=≤<=*f*<=≤<=1000) where:
- *n* — the number of Polycarp trips, - *a* — the cost of a regualar single trip, - *b* — the cost of a trip after a transshipment, - *k* — the maximum n... | Print the smallest amount of money Polycarp could have spent today, if he can purchase no more than *k* travel cards. | [
"3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank\n",
"4 2 1 300 1000\na A\nA aa\naa AA\nAA a\n"
] | [
"11\n",
"5\n"
] | In the first example Polycarp can buy travel card for the route "BerBank <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ecc94b07e73defe233bfe831f3977337706a2d27.png" style="max-width: 100.0%;max-height: 100.0%;"/> University" and spend 8 burles. Note that his second trip "University" <img ... | [
{
"input": "3 5 3 1 8\nBerBank University\nUniversity BerMall\nUniversity BerBank",
"output": "11"
},
{
"input": "4 2 1 300 1000\na A\nA aa\naa AA\nAA a",
"output": "5"
},
{
"input": "2 2 1 0 1\naca BCBA\nBCBA aca",
"output": "3"
},
{
"input": "2 2 1 2 1\nBDDB C\nC BDDB",
... | 46 | 5,632,000 | -1 | 64,994 | |
125 | Two progressions | [
"constructive algorithms",
"greedy"
] | null | null | An arithmetic progression is such a non-empty sequence of numbers where the difference between any two successive numbers is constant. This constant number is called common difference. For example, the sequence 3, 7, 11, 15 is an arithmetic progression. The definition implies that any sequences whose length equals 1 or... | The first line contains a positive integer *n* (2<=≤<=*n*<=≤<=30000), *n* is the length of the given sequence. The second line contains elements of the given sequence *a*1,<=*a*2,<=...,<=*a**n* (<=-<=108<=≤<=*a**i*<=≤<=108). The elements of the progression are different integers. | Print the required arithmetic progressions, one per line. The progressions can be positioned in any order. Each progression should contain at least one number. If there's no solution, then print "No solution" (without the quotes)in the only line of the input file. If there are several solutions, print any of them. | [
"6\n4 1 2 7 3 10\n",
"5\n1 2 3 -2 -7\n"
] | [
"1 2 3 \n4 7 10 \n",
"1 2 3 \n-2 -7 \n"
] | In the second sample another solution is also possible (number three can be assigned to the second progression): 1, 2 and 3, -2, -7. | [] | 0 | 0 | -1 | 65,038 | |
911 | Mass Change Queries | [
"data structures"
] | null | null | You are given an array *a* consisting of *n* integers. You have to process *q* queries to this array; each query is given as four numbers *l*, *r*, *x* and *y*, denoting that for every *i* such that *l*<=≤<=*i*<=≤<=*r* and *a**i*<==<=*x* you have to set *a**i* equal to *y*.
Print the array after all queries are proces... | The first line contains one integer *n* (1<=≤<=*n*<=≤<=200000) — the size of array *a*.
The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=100) — the elements of array *a*.
The third line contains one integer *q* (1<=≤<=*q*<=≤<=200000) — the number of queries you have to process.
Then *q... | Print *n* integers — elements of array *a* after all changes are made. | [
"5\n1 2 3 4 5\n3\n3 5 3 5\n1 5 5 1\n1 5 1 5\n"
] | [
"5 2 5 4 5 "
] | none | [
{
"input": "5\n1 2 3 4 5\n3\n3 5 3 5\n1 5 5 1\n1 5 1 5",
"output": "5 2 5 4 5 "
},
{
"input": "5\n1 2 3 4 5\n1\n1 1 1 1",
"output": "1 2 3 4 5 "
},
{
"input": "1\n1\n1\n1 1 1 1",
"output": "1 "
},
{
"input": "1\n2\n5\n1 1 5 6\n1 1 8 4\n1 1 5 8\n1 1 7 1\n1 1 6 3",
"output"... | 46 | 0 | 0 | 65,060 | |
87 | Vasya and Types | [
"implementation",
"strings"
] | B. Vasya and Types | 2 | 256 | Programmer Vasya is studying a new programming language &K*. The &K* language resembles the languages of the C family in its syntax. However, it is more powerful, which is why the rules of the actual C-like languages are unapplicable to it. To fully understand the statement, please read the language's descripti... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=100) — the number of operators. Then follow *n* lines with operators. Each operator is of one of two types: either "typedef *A* *B*", or "typeof *A*". In the first case the *B* type differs from void and errtype types, and besides, doesn't have any asterisks and amp... | For every typeof operator print on the single line the answer to that operator — the type that the given operator returned. | [
"5\ntypedef void* ptv\ntypeof ptv\ntypedef &&ptv node\ntypeof node\ntypeof &ptv\n",
"17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\ntypeof b\ntypedef b******* c\ntypeof c\n... | [
"void*\nerrtype\nvoid\n",
"void*\nvoid**\nvoid\nvoid**\nerrtype\nvoid\nerrtype\nerrtype\nerrtype\n"
] | Let's look at the second sample.
After the first two queries typedef the b type is equivalent to void*, and с — to void**.
The next query typedef redefines b — it is now equal to &b = &void* = void. At that, the с type doesn't change.
After that the с type is defined as &&b* = &&void* = &... | [
{
"input": "5\ntypedef void* ptv\ntypeof ptv\ntypedef &&ptv node\ntypeof node\ntypeof &ptv",
"output": "void*\nerrtype\nvoid"
},
{
"input": "17\ntypedef void* b\ntypedef b* c\ntypeof b\ntypeof c\ntypedef &b b\ntypeof b\ntypeof c\ntypedef &&b* c\ntypeof c\ntypedef &b* c\ntypeof c\ntypedef &void b\nty... | 248 | 0 | 3.938 | 65,066 |
325 | Monsters and Diamonds | [
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | Piegirl has found a monster and a book about monsters and pies. When she is reading the book, she found out that there are *n* types of monsters, each with an ID between 1 and *n*. If you feed a pie to a monster, the monster will split into some number of monsters (possibly zero), and at least one colorful diamond. Mon... | The first line contains two integers: *m* and *n* (1<=≤<=*m*,<=*n*<=≤<=105), the number of possible splits and the number of different monster types. Each of the following *m* lines contains a split rule. Each split rule starts with an integer (a monster ID) *m**i* (1<=≤<=*m**i*<=≤<=*n*), and a positive integer *l**i* ... | For each monster, in order of their IDs, print a line with two integers: the smallest and the largest number of diamonds that can possibly be collected by starting with that monster. If Piegirl cannot possibly end up in a state without monsters, print -1 for both smallest and the largest value. If she can collect an ar... | [
"6 4\n1 3 -1 1 -1\n1 2 -1 -1\n2 3 -1 3 -1\n2 3 -1 -1 -1\n3 2 -1 -1\n4 2 4 -1\n",
"3 2\n1 2 1 -1\n2 2 -1 -1\n2 3 2 1 -1\n"
] | [
"2 -2\n3 4\n2 2\n-1 -1\n",
"-1 -1\n2 2\n"
] | none | [
{
"input": "6 4\n1 3 -1 1 -1\n1 2 -1 -1\n2 3 -1 3 -1\n2 3 -1 -1 -1\n3 2 -1 -1\n4 2 4 -1",
"output": "2 -2\n3 4\n2 2\n-1 -1"
},
{
"input": "3 2\n1 2 1 -1\n2 2 -1 -1\n2 3 2 1 -1",
"output": "-1 -1\n2 2"
},
{
"input": "2 1\n1 3 -1 1 -1\n1 5 -1 -1 -1 -1 -1",
"output": "5 -2"
},
{
... | 62 | 0 | 0 | 65,093 | |
461 | Appleman and Complicated Task | [
"dsu",
"math"
] | null | null | Toastman came up with a very complicated task. He gives it to Appleman, but Appleman doesn't know how to solve it. Can you help him?
Given a *n*<=×<=*n* checkerboard. Each cell of the board has either character 'x', or character 'o', or nothing. How many ways to fill all the empty cells with 'x' or 'o' (each cell must... | The first line contains two integers *n*, *k* (1<=≤<=*n*,<=*k*<=≤<=105) — the size of the board, and the number of cells that has characters initially.
Then *k* lines follows. The *i*-th line contains two integers and a character: *a**i*, *b**i*, *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*; *c**i* is either 'o' or 'x'). Th... | Print a single integer — the answer to the problem. | [
"3 2\n1 1 x\n2 2 o\n",
"4 3\n2 4 x\n3 4 x\n3 2 x\n"
] | [
"2\n",
"2\n"
] | In the first example there are two ways: | [] | 46 | 0 | 0 | 65,142 | |
42 | Safe cracking | [
"brute force",
"constructive algorithms"
] | C. Safe cracking | 2 | 256 | Right now you are to solve a very, very simple problem — to crack the safe. Four positive integers stand one by one on a circle protecting the safe. You know that to unlock this striking safe you have to make all four numbers equal to one. Operations are as follows: you may choose two adjacent numbers and increase both... | The single line of the input contains four space-separated integer positive numbers not greater than 109 each — four numbers on the circle in consecutive order. | The output should contain "-1" (quotes for clarity) if the safe is secure, that is it's impossible to crack it. Otherwise, output should contain the sequence of operations (one operations per line) leading to unlocking the safe. You don't have to minimize the number of operations, but it should not exceed 1000. To make... | [
"1 1 1 1\n",
"1 2 4 2\n",
"3 3 1 1\n",
"2 1 2 4\n"
] | [
"",
"/2\n/3\n",
"+1\n/1\n/1\n",
"/3\n/4\n"
] | none | [
{
"input": "1 2 4 2",
"output": "/2\n/3"
},
{
"input": "3 3 1 1",
"output": "+1\n/1\n/1"
},
{
"input": "2 1 2 4",
"output": "/3\n/4"
},
{
"input": "5 5 1 1",
"output": "+1\n/1\n+2\n/2\n+4\n/4\n/1"
},
{
"input": "4 4 4 4",
"output": "/1\n/2\n/3\n/4"
},
{
... | 310 | 20,172,800 | 3.884925 | 65,317 |
144 | Anagram Search | [
"implementation",
"strings"
] | null | null | A string *t* is called an anagram of the string *s*, if it is possible to rearrange letters in *t* so that it is identical to the string *s*. For example, the string "aab" is an anagram of the string "aba" and the string "aaa" is not.
The string *t* is called a substring of the string *s* if it can be read starting fr... | The first line is non-empty string *s*, consisting of no more than 105 lowercase Latin letters and characters "?". The second line is non-empty string *p*, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string *p* can exceed the length of the string *s*. | Print the single number representing the number of good substrings of string *s*.
Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times. | [
"bb??x???\naab\n",
"ab?c\nacb\n"
] | [
"2\n",
"2\n"
] | Consider the first sample test. Here the string *s* has two good substrings: "b??" (after we replace the question marks we get "baa"), "???" (after we replace the question marks we get "baa").
Let's consider the second sample test. Here the string *s* has two good substrings: "ab?" ("?" can be replaced by "c"), "b?c" ... | [
{
"input": "bb??x???\naab",
"output": "2"
},
{
"input": "ab?c\nacb",
"output": "2"
},
{
"input": "ccaac\ncbcbca",
"output": "0"
},
{
"input": "?bba?\nbba",
"output": "3"
},
{
"input": "aaaaa??a?a\naaa",
"output": "8"
},
{
"input": "?bba?b?aaa\nabb",
... | 434 | 33,484,800 | 3 | 65,388 | |
142 | Help Shrek and Donkey 2 | [
"games"
] | null | null | Having learned (not without some help from the Codeforces participants) to play the card game from the previous round optimally, Shrek and Donkey (as you may remember, they too live now in the Kingdom of Far Far Away) have decided to quit the boring card games and play with toy soldiers.
The rules of the game are as f... | The first line contains space-separated integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=100). Then *n* lines contain *m* characters each. These characters belong to the set {"-", "G", "R"}, denoting, respectively, a battlefield's free cell, a cell occupied by Shrek's soldiers and a cell occupied by Donkey's soldie... | Print "First" (without the quotes) if Shrek wins in the given Toy Soldier game. If Donkey wins, print "Second" (without the quotes). If the game continues forever, print "Draw" (also without the quotes). | [
"2 3 1\nR-G\nRG-\n",
"3 3 2\nG-R\nR-G\nG-R\n",
"2 3 1\n-R-\n-G-\n",
"2 5 2\n-G-R-\n-R-G-\n"
] | [
"First\n",
"Second\n",
"Draw\n",
"First\n"
] | none | [
{
"input": "2 3 1\nR-G\nRG-",
"output": "First"
},
{
"input": "3 3 2\nG-R\nR-G\nG-R",
"output": "Second"
},
{
"input": "2 3 1\n-R-\n-G-",
"output": "Draw"
},
{
"input": "2 5 2\n-G-R-\n-R-G-",
"output": "First"
},
{
"input": "3 3 1\nG-R\nR-G\nG-R",
"output": "F... | 310 | 21,504,000 | 3 | 65,445 | |
545 | Paths and Trees | [
"graphs",
"greedy",
"shortest paths"
] | null | null | Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.
Let's assume that we are given a c... | The first line contains two numbers, *n* and *m* (1<=≤<=*n*<=≤<=3·105, 0<=≤<=*m*<=≤<=3·105) — the number of vertices and edges of the graph, respectively.
Next *m* lines contain three integers each, representing an edge — *u**i*,<=*v**i*,<=*w**i* — the numbers of vertices connected by an edge and the weight of the edg... | In the first line print the minimum total weight of the edges of the tree.
In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.
If there are... | [
"3 3\n1 2 1\n2 3 1\n1 3 2\n3\n",
"4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4\n"
] | [
"2\n1 2 \n",
"4\n2 3 4 \n"
] | In the first sample there are two possible shortest path trees:
- with edges 1 – 3 and 2 – 3 (the total weight is 3); - with edges 1 – 2 and 2 – 3 (the total weight is 2);
And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 i... | [
{
"input": "3 3\n1 2 1\n2 3 1\n1 3 2\n3",
"output": "2\n1 2 "
},
{
"input": "4 4\n1 2 1\n2 3 1\n3 4 1\n4 1 2\n4",
"output": "4\n2 3 4 "
},
{
"input": "4 5\n1 2 1\n1 3 1\n2 4 1\n3 4 1\n2 3 10\n1",
"output": "3\n1 2 3 "
},
{
"input": "6 8\n1 2 30\n1 3 20\n2 3 50\n4 2 100\n2 5 4... | 108 | 0 | 0 | 65,746 | |
746 | Numbers Exchange | [
"greedy",
"implementation",
"math"
] | null | null | Eugeny has *n* cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct.
Nikolay has *m* cards, distinct numbers from 1 to *m* are wr... | The first line contains two integers *n* and *m* (2<=≤<=*n*<=≤<=2·105, 1<=≤<=*m*<=≤<=109) — the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that *n* is even.
The second line contains a sequence of *n* positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the numbers... | If there is no answer, print -1.
Otherwise, in the first line print the minimum number of exchanges. In the second line print *n* integers — Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the *i*-th card wasn't exchanged then the *i*-... | [
"6 2\n5 6 7 9 4 5\n",
"8 6\n7 7 7 7 8 8 8 8\n",
"4 1\n4 2 1 10\n"
] | [
"1\n5 6 7 9 4 2 \n",
"6\n7 2 4 6 8 1 3 5 \n",
"-1\n"
] | none | [
{
"input": "6 2\n5 6 7 9 4 5",
"output": "1\n5 6 7 9 4 2 "
},
{
"input": "8 6\n7 7 7 7 8 8 8 8",
"output": "6\n7 2 4 6 8 1 3 5 "
},
{
"input": "4 1\n4 2 1 10",
"output": "-1"
},
{
"input": "10 10\n12 13 10 20 13 10 19 15 21 11",
"output": "2\n12 13 10 20 2 4 19 15 21 11 "... | 374 | 34,406,400 | 3 | 66,224 | |
396 | On Number of Decompositions into Multipliers | [
"combinatorics",
"math",
"number theory"
] | null | null | You are given an integer *m* as a product of integers *a*1,<=*a*2,<=... *a**n* . Your task is to find the number of distinct decompositions of number *m* into the product of *n* ordered positive integers.
Decomposition into *n* products, given in the input, must also be considered in the answer. As the answer can be v... | The first line contains positive integer *n* (1<=≤<=*n*<=≤<=500). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109). | In a single line print a single number *k* — the number of distinct decompositions of number *m* into *n* ordered multipliers modulo 1000000007 (109<=+<=7). | [
"1\n15\n",
"3\n1 1 2\n",
"2\n5 7\n"
] | [
"1\n",
"3\n",
"4\n"
] | In the second sample, the get a decomposition of number 2, you need any one number out of three to equal 2, and the rest to equal 1.
In the third sample, the possible ways of decomposing into ordered multipliers are [7,5], [5,7], [1,35], [35,1].
A decomposition of positive integer *m* into *n* ordered multipliers is ... | [
{
"input": "1\n15",
"output": "1"
},
{
"input": "3\n1 1 2",
"output": "3"
},
{
"input": "2\n5 7",
"output": "4"
},
{
"input": "2\n5 10",
"output": "6"
},
{
"input": "3\n1 30 1",
"output": "27"
},
{
"input": "2\n1000000000 1000000000",
"output": "36... | 124 | 0 | 0 | 66,519 | |
200 | Tractor College | [
"implementation",
"math",
"number theory",
"ternary search"
] | null | null | While most students still sit their exams, the tractor college has completed the summer exam session. In fact, students study only one subject at this college — the Art of Operating a Tractor. Therefore, at the end of a term a student gets only one mark, a three (satisfactory), a four (good) or a five (excellent). Thos... | The first line has two integers *n*, *s* (3<=≤<=*n*<=≤<=300,<=1<=≤<=*s*<=≤<=3·105) — the number of students and the budget size for the scholarship, respectively. The second line contains *n* integers, where the *i*-th number represents the mark that the *i*-th student got for the exam. It is guaranteed that at each ma... | On a single line print three integers *k*3, *k*4 and *k*5 — the sought values that represent the optimal distribution of the scholarships. If there are multiple optimal answers, print any of them. If there is no answer, print -1. | [
"5 11\n3 4 3 5 5\n",
"6 15\n5 3 3 4 4 5\n"
] | [
"1 3 3\n",
"-1\n"
] | none | [] | 92 | 0 | 0 | 66,552 | |
847 | Berland Elections | [
"greedy",
"sortings"
] | null | null | The elections to Berland parliament are happening today. Voting is in full swing!
Totally there are *n* candidates, they are numbered from 1 to *n*. Based on election results *k* (1<=≤<=*k*<=≤<=*n*) top candidates will take seats in the parliament.
After the end of the voting the number of votes for each candidate is... | The first line contains four integers *n*, *k*, *m* and *a* (1<=≤<=*k*<=≤<=*n*<=≤<=100, 1<=≤<=*m*<=≤<=100, 1<=≤<=*a*<=≤<=*m*) — the number of candidates, the number of seats in the parliament, the number of Berland citizens and the number of citizens who already have voted.
The second line contains a sequence of *a* i... | Print the sequence consisting of *n* integers *r*1,<=*r*2,<=...,<=*r**n* where:
- *r**i*<==<=1 means that the *i*-th candidate is guaranteed to take seat in the parliament regardless of votes of the remaining *m*<=-<=*a* citizens; - *r**i*<==<=2 means that the *i*-th candidate has a chance to take a seat in the parl... | [
"3 1 5 4\n1 2 1 3\n",
"3 1 5 3\n1 3 1\n",
"3 2 5 3\n1 3 1\n"
] | [
"1 3 3 ",
"2 3 2 ",
"1 2 2 "
] | none | [
{
"input": "3 1 5 4\n1 2 1 3",
"output": "1 3 3 "
},
{
"input": "3 1 5 3\n1 3 1",
"output": "2 3 2 "
},
{
"input": "3 2 5 3\n1 3 1",
"output": "1 2 2 "
},
{
"input": "1 1 1 1\n1",
"output": "1 "
},
{
"input": "1 1 1 1\n1",
"output": "1 "
},
{
"input": ... | 139 | 2,252,800 | 0 | 66,748 | |
0 | none | [
"none"
] | null | null | Little Artem has invented a time machine! He could go anywhere in time, but all his thoughts of course are with computer science. He wants to apply this time machine to a well-known data structure: multiset.
Artem wants to create a basic multiset of integers. He wants these structure to support operations of three typ... | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of Artem's queries.
Then follow *n* lines with queries descriptions. Each of them contains three integers *a**i*, *t**i* and *x**i* (1<=≤<=*a**i*<=≤<=3, 1<=≤<=*t**i*,<=*x**i*<=≤<=109) — type of the query, moment of time Art... | For each ask operation output the number of instances of integer being queried at the given moment of time. | [
"6\n1 1 5\n3 5 5\n1 2 5\n3 6 5\n2 3 5\n3 7 5\n",
"3\n1 1 1\n2 2 1\n3 3 1\n"
] | [
"1\n2\n1\n",
"0\n"
] | none | [] | 78 | 6,656,000 | -1 | 66,899 | |
958 | Guard Duty (medium) | [
"binary search",
"dp",
"greedy",
"sortings"
] | null | null | Princess Heidi decided to give orders to all her *K* Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at *N* specific moments *t*1,<=*t*2,<=...,<=*t**N*. The meetings with commanders must therefore start and stop at those times. Namely, each commande... | The first line contains two integers *K*, *N* (2<=≤<=2*K*<=≤<=*N*<=≤<=500000, *K*<=≤<=5000). The second line contains *N* distinct integers *t*1,<=*t*2,<=...,<=*t**N* (1<=≤<=*t**i*<=≤<=109) representing the times when Heidi leaves hyperspace. | Output only one integer: the minimum time spent on meetings. | [
"2 5\n1 4 6 7 12\n",
"3 6\n6 3 4 2 5 1\n",
"4 12\n15 7 4 19 3 30 14 1 5 23 17 25\n"
] | [
"4\n",
"3\n",
"6\n"
] | In the first example, there are five valid schedules: [1, 4], [6, 7] with total time 4, [1, 4], [6, 12] with total time 9, [1, 4], [7, 12] with total time 8, [1, 6], [7, 12] with total time 10, and [4, 6], [7, 12] with total time 7. So the answer is 4.
In the second example, there is only 1 valid schedule: [1, 2], [3,... | [] | 530 | 75,980,800 | -1 | 67,034 | |
311 | Fetch the Treasure | [
"brute force",
"data structures",
"graphs",
"shortest paths"
] | null | null | Rainbow built *h* cells in a row that are numbered from 1 to *h* from left to right. There are *n* cells with treasure. We call each of these *n* cells "Treasure Cell". The *i*-th "Treasure Cell" is the *a**i*-th cell and the value of treasure in it is *c**i* dollars.
Then, Freda went in the first cell. For now, she c... | The first line of the input contains four integers: *h* (1<=≤<=*h*<=≤<=1018),<=*n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=105) and *k* (1<=≤<=*k*<=≤<=104).
Each of the next *n* lines contains two integers: *a**i* (1<=≤<=*a**i*<=≤<=*h*),<=*c**i* (1<=≤<=*c**i*<=≤<=109). That means the *i*-th "Treasure Cell" is the *a**i*-th cell an... | For each operation of type 3, output an integer indicates the value (in dollars) of the most valuable treasure among the "Treasure Cells" Freda can reach. If there is no such treasure, output 0. | [
"10 3 5 2\n5 50\n7 60\n8 100\n2 2 5\n3\n1 3\n3\n3\n"
] | [
"55\n100\n50\n"
] | In the sample, there are 10 cells and 3 "Treasure Cells". The first "Treasure Cell" is cell 5, having 50 dollars tresure in it. The second "Treasure Cell" is cell 7, having 60 dollars tresure in it. The third "Treasure Cell" is cell 8, having 100 dollars tresure in it.
At first, Freda can only reach cell 1, 3, 5, 7 an... | [] | 46 | 0 | 0 | 67,059 | |
798 | Mike and gcd problem | [
"dp",
"greedy",
"number theory"
] | null | null | Mike has a sequence *A*<==<=[*a*1,<=*a*2,<=...,<=*a**n*] of length *n*. He considers the sequence *B*<==<=[*b*1,<=*b*2,<=...,<=*b**n*] beautiful if the *gcd* of all its elements is bigger than 1, i.e. .
Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index *i* (1<=≤<=*i*<... | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=100<=000) — length of sequence *A*.
The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — elements of sequence *A*. | Output on the first line "YES" (without quotes) if it is possible to make sequence *A* beautiful by performing operations described above, and "NO" (without quotes) otherwise.
If the answer was "YES", output the minimal number of moves needed to make sequence *A* beautiful. | [
"2\n1 1\n",
"3\n6 2 4\n",
"2\n1 3\n"
] | [
"YES\n1\n",
"YES\n0\n",
"YES\n1\n"
] | In the first example you can simply make one move to obtain sequence [0, 2] with <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6ddb490b8e898198d30c56c6edce2cf15e6f998a.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example the *gcd* of the sequence is already greater... | [
{
"input": "2\n1 1",
"output": "YES\n1"
},
{
"input": "3\n6 2 4",
"output": "YES\n0"
},
{
"input": "2\n1 3",
"output": "YES\n1"
},
{
"input": "9\n57 30 28 81 88 32 3 42 25",
"output": "YES\n8"
},
{
"input": "2\n8 77",
"output": "YES\n2"
},
{
"input": "... | 155 | 18,124,800 | 3 | 67,195 | |
627 | Preorder Test | [
"binary search",
"dfs and similar",
"dp",
"graphs",
"greedy",
"trees"
] | null | null | For his computer science class, Jacob builds a model tree with sticks and balls containing *n* nodes in the shape of a tree. Jacob has spent *a**i* minutes building the *i*-th ball in the tree.
Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough ... | The first line of the input contains two positive integers, *n* and *k* (2<=≤<=*n*<=≤<=200<=000, 1<=≤<=*k*<=≤<=*n*) — the number of balls in Jacob's tree and the number of balls the teacher will inspect.
The second line contains *n* integers, *a**i* (1<=≤<=*a**i*<=≤<=1<=000<=000), the time Jacob used to build the *i*-... | Print a single integer — the maximum grade Jacob can get by picking the right root of the tree and rearranging the list of neighbors. | [
"5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3\n",
"4 2\n1 5 5 5\n1 2\n1 3\n1 4\n"
] | [
"3\n",
"1\n"
] | In the first sample, Jacob can root the tree at node 2 and order 2's neighbors in the order 4, 1, 5 (all other nodes have at most two neighbors). The resulting preorder traversal is 2, 4, 1, 3, 5, and the minimum *a*<sub class="lower-index">*i*</sub> of the first 3 nodes is 3.
In the second sample, it is clear that an... | [
{
"input": "5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3",
"output": "3"
},
{
"input": "4 2\n1 5 5 5\n1 2\n1 3\n1 4",
"output": "1"
},
{
"input": "2 1\n1 100000\n2 1",
"output": "100000"
},
{
"input": "2 2\n1 1000000\n1 2",
"output": "1"
},
{
"input": "10 4\n104325 153357 2... | 139 | 0 | 0 | 67,613 | |
49 | Common ancestor | [
"dp"
] | E. Common ancestor | 5 | 256 | The DNA sequence for every living creature in Berland can be represented as a non-empty line consisting of lowercase Latin letters. Berland scientists found out that all the creatures evolve by stages. During one stage exactly one symbol of the DNA line is replaced by exactly two other ones. At that overall there are *... | The first line contains a non-empty DNA sequence *s*1, the second line contains a non-empty DNA sequence *s*2. The lengths of these lines do not exceed 50, the lines contain only lowercase Latin letters. The third line contains an integer *n* (0<=≤<=*n*<=≤<=50) — the number of permissible substitutions. Then follow *n*... | If *s*1 and *s*2 cannot have a common ancestor, print -1. Otherwise print the length of the shortest sequence *s*3, from which *s*1 and *s*2 could have evolved. | [
"ababa\naba\n2\nc->ba\nc->cc\n",
"ababa\naba\n7\nc->ba\nc->cc\ne->ab\nz->ea\nb->ba\nd->dd\nd->ab\n",
"ababa\naba\n1\nc->ba\n"
] | [
"2\n",
"1\n",
"-1\n"
] | none | [
{
"input": "ababa\naba\n2\nc->ba\nc->cc",
"output": "2"
},
{
"input": "ababa\naba\n7\nc->ba\nc->cc\ne->ab\nz->ea\nb->ba\nd->dd\nd->ab",
"output": "1"
},
{
"input": "ababa\naba\n1\nc->ba",
"output": "-1"
},
{
"input": "mvo\nmx\n5\nu->dl\nt->fb\nx->vo\nn->kq\na->ca",
"outpu... | 2,556 | 1,331,200 | 3.74192 | 67,617 |
115 | Plumber | [
"math"
] | null | null | Little John aspires to become a plumber! Today he has drawn a grid consisting of *n* rows and *m* columns, consisting of *n*<=×<=*m* square cells.
In each cell he will draw a pipe segment. He can only draw four types of segments numbered from 1 to 4, illustrated as follows:
Each pipe segment has two ends, illustrated... | The first line will contain two single-space separated integers *n* and *m* (1<=≤<=*n*,<=*m*,<=*n*·*m*<=≤<=5·105) — the number of rows and columns respectively. Then *n* lines follow, each contains exactly *m* characters — the description of the grid. Each character describes a cell and is either one of these:
- "1"... | Print a single integer denoting the number of possible final non-leaking pipe systems modulo 1000003 (106<=+<=3). If there are no such configurations, print 0. | [
"2 2\n13\n..\n",
"3 1\n1\n4\n.\n",
"2 2\n3.\n.1\n"
] | [
"2\n",
"0\n",
"1\n"
] | For the first example, the initial configuration of the grid is as follows.
The only two possible final non-leaking pipe configurations are as follows:
For the second example, the initial grid is already leaking, so there will be no final grid that is non-leaking.
For the final example, there's only one possible no... | [
{
"input": "2 2\n13\n..",
"output": "2"
},
{
"input": "3 1\n1\n4\n.",
"output": "0"
},
{
"input": "2 2\n3.\n.1",
"output": "1"
},
{
"input": "2 2\n..\n..",
"output": "16"
},
{
"input": "2 2\n32\n41",
"output": "1"
},
{
"input": "4 4\n....\n.3.3\n....\n... | 60 | 0 | 0 | 67,661 | |
718 | Matvey's Birthday | [
"bitmasks",
"graphs"
] | null | null | Today is Matvey's birthday. He never knows what to ask as a present so friends gave him a string *s* of length *n*. This string consists of only first eight English letters: 'a', 'b', ..., 'h'.
First question that comes to mind is: who might ever need some string? Matvey is a special boy so he instantly found what to ... | The first line of the input contains a single integer *n* (2<=≤<=*n*<=≤<=100<=000) — the length of the string.
The second line contains the string *s* itself. It's guaranteed that *s* consists of only first eight letters of English alphabet. | Print two integers — the diameter of the graph and the number of pairs of positions with the distance equal to the diameter. | [
"3\nabc\n",
"7\naaabaaa\n"
] | [
"2 1\n",
"2 4\n"
] | Consider the second sample.
The maximum distance is 2. It's obtained for pairs (1, 4), (2, 4), (4, 6) and (4, 7). | [] | 46 | 5,529,600 | -1 | 67,748 | |
593 | Strange Calculation and Cats | [
"dp",
"matrices"
] | null | null | Gosha's universe is a table consisting of *n* rows and *m* columns. Both the rows and columns are numbered with consecutive integers starting with 1. We will use (*r*,<=*c*) to denote a cell located in the row *r* and column *c*.
Gosha is often invited somewhere. Every time he gets an invitation, he first calculates t... | The first line of the input contains three positive integers *n*, *m* and *q* (1<=≤<=*n*·*m*<=≤<=20,<=1<=≤<=*q*<=≤<=10<=000) — the number of rows and columns in the table and the number of events respectively.
Next *q* lines describe the events, each description contains four integers *tp**i*, *x**i*, *y**i* and *t**i... | For each invitation *i* (that is, *tp**i*<==<=1) calculate the number of ways to get to cell (*x**i*,<=*y**i*) at the moment of time *t**i*. Respond to the invitations chronologically, that is, in the order they appear in the input. | [
"1 3 3\n2 1 2 3\n3 1 2 5\n1 1 1 7\n",
"3 3 3\n2 2 2 2\n1 3 3 5\n1 3 3 7\n",
"4 5 5\n2 2 5 3\n2 2 4 6\n3 2 4 9\n1 4 4 13\n1 4 4 15\n"
] | [
"5\n",
"2\n42\n",
"490902\n10598759\n"
] | Explanation of the first sample. Each picture specifies the number of ways to arrive at the cell at the appropriate time. (X stands for a cell blocked at this particular moment of time) | [
{
"input": "1 3 3\n2 1 2 3\n3 1 2 5\n1 1 1 7",
"output": "5"
},
{
"input": "3 3 3\n2 2 2 2\n1 3 3 5\n1 3 3 7",
"output": "2\n42"
},
{
"input": "4 5 5\n2 2 5 3\n2 2 4 6\n3 2 4 9\n1 4 4 13\n1 4 4 15",
"output": "490902\n10598759"
},
{
"input": "1 1 1\n1 1 1 2",
"output": "1... | 30 | 0 | 0 | 67,789 | |
652 | Pursuit For Artifacts | [
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.
In that country there are *n* islands and *m* bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerfu... | The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=3·105, 0<=≤<=*m*<=≤<=3·105) — the number of islands and bridges in the game.
Each of the next *m* lines contains the description of the bridge — three integers *x**i*, *y**i*, *z**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=≠<=*y**i*, 0<=≤<=*z**i*<=≤<=1), ... | If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes). | [
"6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6\n",
"5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4\n",
"5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6",
"output": "YES"
},
{
"input": "5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4",
"output": "NO"
},
{
"input": "5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2",
"output": "YES"
},
{
"input": "1 0\n1 1",
"outp... | 61 | 1,126,400 | 0 | 67,838 | |
0 | none | [
"none"
] | null | null | Is there anything better than going to the zoo after a tiresome week at work? No wonder Grisha feels the same while spending the entire weekend accompanied by pretty striped zebras.
Inspired by this adventure and an accidentally found plasticine pack (represented as a sequence of black and white stripes), Grisha now ... | The only line contains a string $s$ ($1 \le |s| \le 10^5$, where $|s|$ denotes the length of the string $s$) comprised of lowercase English letters 'b' and 'w' only, where 'w' denotes a white piece and 'b' denotes a black piece. | Print a single integer — the maximum possible zebra length. | [
"bwwwbwwbw\n",
"bwwbwwb\n"
] | [
"5\n",
"3\n"
] | In the first example one of the possible sequence of operations is bwwwbww|bw $\to$ w|wbwwwbwb $\to$ wbwbwwwbw, that gives the answer equal to $5$.
In the second example no operation can increase the answer. | [
{
"input": "bwwwbwwbw",
"output": "5"
},
{
"input": "bwwbwwb",
"output": "3"
},
{
"input": "bwb",
"output": "3"
},
{
"input": "bbbbwbwwbbwwwwwbbbwb",
"output": "4"
},
{
"input": "bbwwwbwwbwwbbwwwwwbbbbbbwbwwwwbbwwbbwbbwbwwwbwwwwbwwbbwwwbbwbbwwbbwbwwwwwwwbbwwbbwwbb... | 93 | 0 | 0 | 67,910 | |
203 | Hit Ball | [
"geometry",
"implementation",
"math"
] | null | null | When Valera was playing football on a stadium, it suddenly began to rain. Valera hid in the corridor under the grandstand not to get wet. However, the desire to play was so great that he decided to train his hitting the ball right in this corridor. Valera went back far enough, put the ball and hit it. The ball bounced ... | The first line contains three space-separated integers *a*,<=*b*,<=*m* (1<=≤<=*a*,<=*b*,<=*m*<=≤<=100). The first two integers specify point (*a*,<=0,<=*b*), through which the ceiling and one of the corridor walls pass. The third integer is the distance at which Valera went away from the door.
The second line has thre... | Print two real numbers *x*0,<=*z*0 — the *x* and *z* coordinates of point (*x*0,<=0,<=*z*0), at which the ball hits the exit door. The answer will be considered correct, if its absolute or relative error does not exceed 10<=<=-<=6. | [
"7 2 11\n3 -11 2\n",
"7 2 11\n4 -3 3\n"
] | [
"6.5000000000 2.0000000000\n",
"4.1666666667 1.0000000000\n"
] | <img class="tex-graphics" src="https://espresso.codeforces.com/0b96c99a50a7ff8657d6301992a0fe440badfb7b.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "7 2 11\n3 -11 2",
"output": "6.5000000000 2.0000000000"
},
{
"input": "7 2 11\n4 -3 3",
"output": "4.1666666667 1.0000000000"
},
{
"input": "44 94 98\n-17 -64 9",
"output": "4.0312500000 13.7812500000"
},
{
"input": "41 4 58\n81 -9 65",
"output": "31.500000000... | 186 | 0 | 0 | 67,929 | |
24 | Broken robot | [
"dp",
"math",
"probabilities"
] | D. Broken robot | 2 | 256 | You received as a gift a very clever robot walking on a rectangular board. Unfortunately, you understood that it is broken and behaves rather strangely (randomly). The board consists of *N* rows and *M* columns of cells. The robot is initially at some cell on the *i*-th row and the *j*-th column. Then at every step the... | On the first line you will be given two space separated integers *N* and *M* (1<=≤<=*N*,<=*M*<=≤<=1000). On the second line you will be given another two space separated integers *i* and *j* (1<=≤<=*i*<=≤<=*N*,<=1<=≤<=*j*<=≤<=*M*) — the number of the initial row and the number of the initial column. Note that, (1,<=1) ... | Output the expected number of steps on a line of itself with at least 4 digits after the decimal point. | [
"10 10\n10 4\n",
"10 14\n5 14\n"
] | [
"0.0000000000\n",
"18.0038068653\n"
] | none | [
{
"input": "10 10\n10 4",
"output": "0.0000000000"
},
{
"input": "10 14\n5 14",
"output": "18.0038068653"
},
{
"input": "126 125\n115 22",
"output": "43.9999127943"
},
{
"input": "755 51\n205 12",
"output": "2178.8368031733"
},
{
"input": "385 978\n344 18",
"o... | 92 | 0 | 0 | 67,961 |
555 | Case of Computer Network | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible attack by hackers on a major computer network.
In this network are *n* vertices, some pairs of vertices are connected by *m* undirected channels. It is planned to transfer *q* important messages via this network, the *i*... | The first line contains three integers *n*, *m* and *q* (1<=≤<=*n*,<=*m*,<=*q*<=≤<=2·105) — the number of nodes, channels and important messages.
Next *m* lines contain two integers each, *v**i* and *u**i* (1<=≤<=*v**i*,<=*u**i*<=≤<=*n*, *v**i*<=≠<=*u**i*), that means that between nodes *v**i* and *u**i* is a channel.... | If a solution exists, print on a single line "Yes" (without the quotes). Otherwise, print "No" (without the quotes). | [
"4 4 2\n1 2\n1 3\n2 3\n3 4\n1 3\n4 2\n",
"3 2 2\n1 2\n3 2\n1 3\n2 1\n",
"3 3 2\n1 2\n1 2\n3 2\n1 3\n2 1\n"
] | [
"Yes\n",
"No\n",
"Yes\n"
] | In the first sample test you can assign directions, for example, as follows: 1 → 2, 1 → 3, 3 → 2, 4 → 3. Then the path for for the first message will be 1 → 3, and for the second one — 4 → 3 → 2.
In the third sample test you can assign directions, for example, as follows: 1 → 2, 2 → 1, 2 → 3. Then the path for the fir... | [] | 155 | 9,830,400 | 0 | 67,995 | |
690 | Collective Mindsets (hard) | [] | null | null | Heidi got one brain, thumbs up! But the evening isn't over yet and one more challenge awaits our dauntless agent: after dinner, at precisely midnight, the *N* attendees love to play a very risky game...
Every zombie gets a number *n**i* (1<=≤<=*n**i*<=≤<=*N*) written on his forehead. Although no zombie can see his own... | The first line of input contains a single integer *T* (1<=≤<=*T*<=≤<=50000): the number of scenarios for which you have to make a guess.
The *T* scenarios follow, described on two lines each:
- The first line holds two integers, *N* (2<=≤<=*N*<=≤<=6), the number of attendees, and *R* (1<=≤<=*R*<=≤<=*N*), the rank o... | For every scenario, output a single integer: the number that the zombie of rank *R* shall guess, based on the numbers *n**i* on his *N*<=-<=1 fellows' foreheads. | [
"4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2\n",
"2\n5 2\n2 2 2 2\n6 4\n3 2 6 1 2\n"
] | [
"1\n2\n2\n1\n",
"5\n2\n"
] | For instance, if there were *N* = 2 two attendees, a successful strategy could be:
- The zombie of rank 1 always guesses the number he sees on the forehead of the zombie of rank 2. - The zombie of rank 2 always guesses the opposite of the number he sees on the forehead of the zombie of rank 1. | [
{
"input": "4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2",
"output": "1\n2\n2\n1"
},
{
"input": "4\n2 1\n1\n2 2\n1\n2 1\n2\n2 2\n2",
"output": "1\n2\n2\n1"
},
{
"input": "27\n3 1\n1 1\n3 2\n1 1\n3 3\n1 1\n3 1\n2 1\n3 2\n2 1\n3 3\n2 1\n3 1\n3 1\n3 2\n3 1\n3 3\n3 1\n3 1\n1 2\n3 2\n1 2\n3 3\n1 2\n3 1\... | 951 | 0 | 3 | 68,070 | |
627 | Island Puzzle | [
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | A remote island chain contains *n* islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of *n*<=-<=1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each isla... | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000) — the total number of islands.
The second line contains *n* space-separated integers *a**i* (0<=≤<=*a**i*<=≤<=*n*<=-<=1) — the statue currently located on the *i*-th island. If *a**i*<==<=0, then the island has no statue. It is guaranteed that the *... | Print a single line of integers:
If the rearrangement can be done in the existing network, output 0 *t*, where *t* is the number of moves necessary to perform the rearrangement.
Otherwise, print *u*, *v*, and *t* (1<=≤<=*u*<=<<=*v*<=≤<=*n*) — the two endpoints of the new bridge, and the minimum number of statue mo... | [
"3\n1 0 2\n2 0 1\n1 2\n2 3\n",
"2\n1 0\n0 1\n1 2\n",
"4\n0 1 2 3\n0 2 3 1\n1 2\n1 3\n1 4\n"
] | [
"1 3 3\n",
"0 1\n",
"-1\n"
] | In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.
In the second sample, ... | [] | 0 | 0 | -1 | 68,445 | |
832 | Misha, Grisha and Underground | [
"dfs and similar",
"graphs",
"trees"
] | null | null | Misha and Grisha are funny boys, so they like to use new underground. The underground has *n* stations connected with *n*<=-<=1 routes so that each route connects two stations, and it is possible to reach every station from any other.
The boys decided to have fun and came up with a plan. Namely, in some day in the mor... | The first line contains two integers *n* and *q* (2<=≤<=*n*<=≤<=105, 1<=≤<=*q*<=≤<=105) — the number of stations and the number of days.
The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=≤<=*p**i*<=≤<=*n*). The integer *p**i* means that there is a route between stations *p**i* and *i*. It is g... | Print *q* lines. In the *i*-th of these lines print the maximum possible number Grisha can get counting when the stations *s*, *t* and *f* are chosen optimally from the three stations on the *i*-th day. | [
"3 2\n1 1\n1 2 3\n2 3 3\n",
"4 1\n1 2 3\n1 2 3\n"
] | [
"2\n3\n",
"2\n"
] | In the first example on the first day if *s* = 1, *f* = 2, *t* = 3, Misha would go on the route 1 <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/70a0795f45d32287dba0eb83fc4a3f470c6e5537.png" style="max-width: 100.0%;max-height: 100.0%;"/> 2, and Grisha would go on the route 3 <img align="m... | [
{
"input": "3 2\n1 1\n1 2 3\n2 3 3",
"output": "2\n3"
},
{
"input": "4 1\n1 2 3\n1 2 3",
"output": "2"
},
{
"input": "2 4\n1\n1 1 1\n1 1 2\n1 2 2\n2 2 2",
"output": "1\n2\n2\n1"
},
{
"input": "5 20\n4 1 1 4\n2 2 5\n3 2 5\n2 3 4\n4 2 5\n4 1 2\n5 3 1\n2 1 2\n4 3 2\n1 3 3\n4 2 5... | 1,325 | 40,550,400 | 3 | 68,786 | |
191 | Fools and Roads | [
"data structures",
"dfs and similar",
"trees"
] | null | null | They say that Berland has exactly two problems, fools and roads. Besides, Berland has *n* cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between eac... | The first line contains a single integer *n* (2<=≤<=*n*<=≤<=105) — the number of cities.
Each of the next *n*<=-<=1 lines contains two space-separated integers *u**i*,<=*v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*), that means that there is a road connecting cities *u**i* and *v**i*.
The next line conta... | Print *n*<=-<=1 integer. The integers should be separated by spaces. The *i*-th number should equal the number of fools who can go on the *i*-th road. The roads are numbered starting from one in the order, in which they occur in the input. | [
"5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5\n",
"5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5\n"
] | [
"2 1 1 1 \n",
"3 1 1 1 \n"
] | In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones.
In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one... | [
{
"input": "5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5",
"output": "2 1 1 1 "
},
{
"input": "5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5",
"output": "3 1 1 1 "
}
] | 0 | 0 | -1 | 68,907 | |
150 | Freezing with Style | [
"binary search",
"data structures",
"divide and conquer",
"trees"
] | null | null | This winter is so... well, you've got the idea :-) The Nvodsk road system can be represented as *n* junctions connected with *n*<=-<=1 bidirectional roads so that there is a path between any two junctions. The organizers of some event want to choose a place to accommodate the participants (junction *v*), and the place ... | The first line contains three integers *n*, *l*, *r* (1<=≤<=*l*<=≤<=*r*<=<<=*n*<=≤<=105).
Next *n*<=-<=1 lines contain descriptions of roads of the Nvodsk, each line contains three integers *a**i*, *b**i*, *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, 0<=≤<=*c**i*<=≤<=109, *a**i*<=≠<=*b**i*) — junctions *a**i* and *b**i* ... | Print two integers — numbers of the junctions, where to accommodate the participants and set up the contests, correspondingly. If there are multiple optimal variants, print any of them. | [
"6 3 4\n1 2 1\n2 3 1\n3 4 1\n4 5 1\n5 6 1\n",
"6 3 4\n1 2 1\n2 3 1\n3 4 1\n4 5 2\n5 6 2\n",
"5 1 4\n1 2 1\n1 3 4\n3 4 7\n3 5 2\n",
"8 3 6\n1 2 9\n2 3 7\n3 4 7\n4 5 8\n5 8 2\n3 6 3\n2 7 4\n"
] | [
"4 1\n",
"6 3\n",
"4 3\n",
"5 1\n"
] | In the first sample all roads have the same beauty. That means that all paths of the positive length have the same median. Thus, any path with length from 3 to 4, inclusive will be valid for us.
In the second sample the city looks like that: 1 - 2 - 3 - 4 - 5 - 6. Two last roads are more valuable and we should choose ... | [] | 60 | 0 | -1 | 68,973 | |
990 | GCD Counting | [
"divide and conquer",
"dp",
"dsu",
"number theory",
"trees"
] | null | null | You are given a tree consisting of $n$ vertices. A number is written on each vertex; the number on vertex $i$ is equal to $a_i$.
Let's denote the function $g(x, y)$ as the greatest common divisor of the numbers written on the vertices belonging to the simple path from vertex $x$ to vertex $y$ (including these two vert... | The first line contains one integer $n$ — the number of vertices $(1 \le n \le 2 \cdot 10^5)$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ $(1 \le a_i \le 2 \cdot 10^5)$ — the numbers written on vertices.
Then $n - 1$ lines follow, each containing two integers $x$ and $y$ $(1 \le x, y \le n, x \ne ... | For every integer $i$ from $1$ to $2 \cdot 10^5$ do the following: if there is no pair $(x, y)$ such that $x \le y$ and $g(x, y) = i$, don't output anything. Otherwise output two integers: $i$ and the number of aforementioned pairs. You have to consider the values of $i$ in ascending order.
See the examples for better... | [
"3\n1 2 3\n1 2\n2 3\n",
"6\n1 2 4 8 16 32\n1 6\n6 3\n3 4\n4 2\n6 5\n",
"4\n9 16 144 6\n1 3\n2 3\n4 3\n"
] | [
"1 4\n2 1\n3 1\n",
"1 6\n2 5\n4 6\n8 1\n16 2\n32 1\n",
"1 1\n2 1\n3 1\n6 2\n9 2\n16 2\n144 1\n"
] | none | [
{
"input": "3\n1 2 3\n1 2\n2 3",
"output": "1 4\n2 1\n3 1"
},
{
"input": "6\n1 2 4 8 16 32\n1 6\n6 3\n3 4\n4 2\n6 5",
"output": "1 6\n2 5\n4 6\n8 1\n16 2\n32 1"
},
{
"input": "4\n9 16 144 6\n1 3\n2 3\n4 3",
"output": "1 1\n2 1\n3 1\n6 2\n9 2\n16 2\n144 1"
},
{
"input": "3\n1 ... | 0 | 0 | -1 | 69,014 | |
845 | Fire in the City | [
"binary search",
"data structures"
] | null | null | The capital of Berland looks like a rectangle of size *n*<=×<=*m* of the square blocks of same size.
Fire!
It is known that *k*<=+<=1 blocks got caught on fire (*k*<=+<=1<=≤<=*n*·*m*). Those blocks are centers of ignition. Moreover positions of *k* of these centers are known and one of these stays unknown. All *k*<=+... | The first line contains three integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=109, 1<=≤<=*k*<=≤<=500).
Each of the next *k* lines contain two integers *x**i* and *y**i* (1<=≤<=*x**i*<=≤<=*n*, 1<=≤<=*y**i*<=≤<=*m*) — coordinates of the *i*-th center of ignition. It is guaranteed that the locations of all centers of igni... | Print the minimal time it takes the fire to lighten up the whole city (in minutes). | [
"7 7 3\n1 2\n2 1\n5 5\n",
"10 5 1\n3 3\n"
] | [
"3\n",
"2\n"
] | In the first example the last block can have coordinates (4, 4).
In the second example the last block can have coordinates (8, 3). | [
{
"input": "7 7 3\n1 2\n2 1\n5 5",
"output": "3"
},
{
"input": "10 5 1\n3 3",
"output": "2"
},
{
"input": "4 5 19\n1 1\n1 2\n1 3\n1 4\n1 5\n2 1\n2 2\n2 3\n2 5\n3 1\n3 2\n3 3\n3 4\n3 5\n4 1\n4 2\n4 3\n4 4\n4 5",
"output": "0"
},
{
"input": "5 5 24\n1 2\n1 3\n1 4\n1 5\n2 1\n2 2... | 77 | 1,331,200 | 0 | 69,047 | |
235 | Cyclical Quest | [
"data structures",
"string suffix structures",
"strings"
] | null | null | Some days ago, WJMZBMR learned how to answer the query "how many times does a string *x* occur in a string *s*" quickly by preprocessing the string *s*. But now he wants to make it harder.
So he wants to ask "how many consecutive substrings of *s* are cyclical isomorphic to a given string *x*". You are given string *s... | The first line contains a non-empty string *s*. The length of string *s* is not greater than 106 characters.
The second line contains an integer *n* (1<=≤<=*n*<=≤<=105) — the number of queries. Then *n* lines follow: the *i*-th line contains the string *x**i* — the string for the *i*-th query. The total length of *x**... | For each query *x**i* print a single integer that shows how many consecutive substrings of *s* are cyclical isomorphic to *x**i*. Print the answers to the queries in the order they are given in the input. | [
"baabaabaaa\n5\na\nba\nbaa\naabaa\naaba\n",
"aabbaa\n3\naa\naabb\nabba\n"
] | [
"7\n5\n7\n3\n5\n",
"2\n3\n3\n"
] | none | [] | 46 | 0 | 0 | 69,103 | |
903 | Clear The Matrix | [
"bitmasks",
"dp"
] | null | null | You are given a matrix *f* with 4 rows and *n* columns. Each element of the matrix is either an asterisk (*) or a dot (.).
You may perform the following operation arbitrary number of times: choose a square submatrix of *f* with size *k*<=×<=*k* (where 1<=≤<=*k*<=≤<=4) and replace each element of the chosen submatrix w... | The first line contains one integer *n* (4<=≤<=*n*<=≤<=1000) — the number of columns in *f*.
The second line contains 4 integers *a*1, *a*2, *a*3, *a*4 (1<=≤<=*a**i*<=≤<=1000) — the cost to replace the square submatrix of size 1<=×<=1, 2<=×<=2, 3<=×<=3 or 4<=×<=4, respectively.
Then four lines follow, each containing... | Print one integer — the minimum number of coins to replace all asterisks with dots. | [
"4\n1 10 8 20\n***.\n***.\n***.\n...*\n",
"7\n2 1 8 2\n.***...\n.***..*\n.***...\n....*..\n",
"4\n10 10 1 10\n***.\n*..*\n*..*\n.***\n"
] | [
"9\n",
"3\n",
"2\n"
] | In the first example you can spend 8 coins to replace the submatrix 3 × 3 in the top-left corner, and 1 coin to replace the 1 × 1 submatrix in the bottom-right corner.
In the second example the best option is to replace the 4 × 4 submatrix containing columns 2 – 5, and the 2 × 2 submatrix consisting of rows 2 – 3 and ... | [
{
"input": "4\n1 10 8 20\n***.\n***.\n***.\n...*",
"output": "9"
},
{
"input": "7\n2 1 8 2\n.***...\n.***..*\n.***...\n....*..",
"output": "3"
},
{
"input": "4\n10 10 1 10\n***.\n*..*\n*..*\n.***",
"output": "2"
},
{
"input": "5\n4 3 4 4\n.....\n****.\n..***\n...**",
"out... | 1,000 | 70,246,400 | 0 | 69,123 | |
452 | Washer, Dryer, Folder | [
"greedy",
"implementation"
] | null | null | You have *k* pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has *n*1 washing machines, *n*2 drying machines and *n*3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it ... | The only line of the input contains seven integers: *k*,<=*n*1,<=*n*2,<=*n*3,<=*t*1,<=*t*2,<=*t*3 (1<=≤<=*k*<=≤<=104; 1<=≤<=*n*1,<=*n*2,<=*n*3,<=*t*1,<=*t*2,<=*t*3<=≤<=1000). | Print one integer — smallest number of minutes to do all your laundry. | [
"1 1 1 1 5 5 5\n",
"8 4 3 2 10 5 2\n"
] | [
"15\n",
"32\n"
] | In the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it.
In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried... | [
{
"input": "1 1 1 1 5 5 5",
"output": "15"
},
{
"input": "8 4 3 2 10 5 2",
"output": "32"
},
{
"input": "10000 1000 1000 1000 1000 1000 1000",
"output": "12000"
},
{
"input": "8 3 4 3 10 9 4",
"output": "43"
},
{
"input": "5 2 4 5 9 9 6",
"output": "42"
},
... | 1,000 | 307,200 | 0 | 69,290 | |
73 | LionAge II | [
"dp"
] | C. LionAge II | 2 | 256 | Vasya plays the LionAge II. He was bored of playing with a stupid computer, so he installed this popular MMORPG, to fight with his friends. Vasya came up with the name of his character — non-empty string *s*, consisting of a lowercase Latin letters. However, in order not to put up a front of friends, Vasya has decided ... | The first line contains character's name *s* and an integer number *k* (0<=≤<=*k*<=≤<=100). The length of the nonempty string *s* does not exceed 100. The second line contains an integer number *n* (0<=≤<=*n*<=≤<=676) — amount of pairs of letters, giving bonus to the euphony. The next *n* lines contain description of t... | Output the only number — maximum possible euphony оf the new character's name. | [
"winner 4\n4\ns e 7\no s 8\nl o 13\no o 8\n",
"abcdef 1\n5\na b -10\nb c 5\nc d 5\nd e 5\ne f 5\n"
] | [
"36",
"20"
] | In the first example the most euphony name will be *looser*. It is easy to calculate that its euphony is 36. | [
{
"input": "winner 4\n4\ns e 7\no s 8\nl o 13\no o 8",
"output": "36"
},
{
"input": "abcdef 1\n5\na b -10\nb c 5\nc d 5\nd e 5\ne f 5",
"output": "20"
},
{
"input": "akcbd 2\n3\na d 55\nb z 100\nb c 50",
"output": "155"
},
{
"input": "adcbd 1\n3\na d 55\nb z 100\nb c 50",
... | 2,000 | 6,246,400 | 0 | 69,328 |
700 | Huffman Coding on Segment | [
"data structures",
"greedy"
] | null | null | Alice wants to send an important message to Bob. Message *a*<==<=(*a*1,<=...,<=*a**n*) is a sequence of positive integers (characters).
To compress the message Alice wants to use binary Huffman coding. We recall that binary Huffman code, or binary prefix code is a function *f*, that maps each letter that appears in th... | The first line of the input contains the single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the length of the initial message. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=100<=000) — characters of the message.
Next line contains the single integer *q* (1<=≤<=*q*<=≤<=100<=000) — the nu... | Print *q* lines. Each line should contain a single integer — the minimum possible length of the Huffman encoding of the substring *a**l**i*... *a**r**i*. | [
"7\n1 2 1 3 1 2 1\n5\n1 7\n1 3\n3 5\n2 4\n4 4\n"
] | [
"10\n3\n3\n5\n0\n"
] | In the first query, one of the optimal ways to encode the substring is to map 1 to "0", 2 to "10" and 3 to "11".
Note that it is correct to map the letter to the empty substring (as in the fifth query from the sample). | [] | 108 | 921,600 | -1 | 69,337 | |
811 | Vladik and Favorite Game | [
"constructive algorithms",
"dfs and similar",
"graphs",
"interactive"
] | null | null | This is an interactive problem.
Vladik has favorite game, in which he plays all his free time.
Game field could be represented as *n*<=×<=*m* matrix which consists of cells of three types:
- «.» — normal cell, player can visit it. - «F» — finish cell, player has to finish his way there to win. There is exactly on... | First line contains two space-separated integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100) — number of rows and columns respectively.
Each of next *n* lines contains *m* characters describing corresponding row of field. Set of characters in field is described above.
Guaranteed that cell with coordinates (1,<=1) is normal... | none | [
"4 3\n...\n**.\nF*.\n...\n1 1\n1 2\n1 3\n1 3\n2 3\n3 3\n4 3\n4 2\n4 1\n3 1\n"
] | [
"R\nL\nL\nD\nU\nU\nU\nR\nR\nD\n"
] | In first test case all four directions swapped with their opposite directions. Protocol of interaction In more convenient form:
<img class="tex-graphics" src="https://espresso.codeforces.com/d9b78340a3793547cc8ccc2c1c34484f3cfe0453.png" style="max-width: 100.0%;max-height: 100.0%;"/>
This test could be presenter for ... | [] | 31 | 0 | 0 | 69,380 | |
906 | Reverses | [
"dp",
"string suffix structures",
"strings"
] | null | null | Hurricane came to Berland and to suburbs Stringsvill. You are going to it to check if it's all right with you favorite string. Hurrinace broke it a bit by reversing some of its non-intersecting substrings. You have a photo of this string before hurricane and you want to restore it to original state using reversing mini... | First line of input contains string *s* and second line contains string *t*. Both strings have same length and consist of lowercase English letters. 1<=≤<=|*s*|<==<=|*t*|<=≤<=5·105 | In first line print *k* — minimum number of substrings you should reverse. Next output *k* lines. Each line should contain two integers *l**i*, *r**i* meaning that you should reverse substring from symbol number *l**i* to symbol *r**i* (strings are 1-indexed). These substrings shouldn't intersect. If there are multiple... | [
"abcxxxdef\ncbaxxxfed\n"
] | [
"2\n7 9\n1 3\n"
] | none | [] | 77 | 0 | 0 | 69,409 | |
724 | Goods transportation | [
"dp",
"flows",
"greedy"
] | null | null | There are *n* cities located along the one-way road. Cities are numbered from 1 to *n* in the direction of the road.
The *i*-th city had produced *p**i* units of goods. No more than *s**i* units of goods can be sold in the *i*-th city.
For each pair of cities *i* and *j* such that 1<=≤<=*i*<=<<=*j*<=≤<=*n* you can... | The first line of the input contains two integers *n* and *c* (1<=≤<=*n*<=≤<=10<=000, 0<=≤<=*c*<=≤<=109) — the number of cities and the maximum amount of goods for a single transportation.
The second line contains *n* integers *p**i* (0<=≤<=*p**i*<=≤<=109) — the number of units of goods that were produced in each city... | Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations. | [
"3 0\n1 2 3\n3 2 1\n",
"5 1\n7 4 2 1 0\n1 2 3 4 5\n",
"4 3\n13 10 7 4\n4 7 10 13\n"
] | [
"4\n",
"12\n",
"34\n"
] | none | [
{
"input": "3 0\n1 2 3\n3 2 1",
"output": "4"
},
{
"input": "5 1\n7 4 2 1 0\n1 2 3 4 5",
"output": "12"
},
{
"input": "4 3\n13 10 7 4\n4 7 10 13",
"output": "34"
},
{
"input": "10 1\n0 2 1 1 0 2 5 2 5 5\n4 0 1 4 2 4 4 5 2 3",
"output": "18"
},
{
"input": "10 3\n10... | 61 | 204,800 | -1 | 69,472 | |
16 | Fish | [
"bitmasks",
"dp",
"probabilities"
] | E. Fish | 3 | 128 | *n* fish, numbered from 1 to *n*, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability *a**ij*, and the second will eat up the first with the probability *a**ji*<==<... | The first line contains integer *n* (1<=≤<=*n*<=≤<=18) — the amount of fish in the lake. Then there follow *n* lines with *n* real numbers each — matrix *a*. *a**ij* (0<=≤<=*a**ij*<=≤<=1) — the probability that fish with index *i* eats up fish with index *j*. It's guaranteed that the main diagonal contains zeros only, ... | Output *n* space-separated real numbers accurate to not less than 6 decimal places. Number with index *i* should be equal to the probability that fish with index *i* will survive to be the last in the lake. | [
"2\n0 0.5\n0.5 0\n",
"5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0\n"
] | [
"0.500000 0.500000 ",
"1.000000 0.000000 0.000000 0.000000 0.000000 "
] | none | [
{
"input": "2\n0 0.5\n0.5 0",
"output": "0.500000 0.500000 "
},
{
"input": "4\n0 0.5 0.5 0.5\n0.5 0 0.5 0.5\n0.5 0.5 0 0.5\n0.5 0.5 0.5 0",
"output": "0.250000 0.250000 0.250000 0.250000 "
},
{
"input": "5\n0 1 1 1 1\n0 0 0.5 0.5 0.5\n0 0.5 0 0.5 0.5\n0 0.5 0.5 0 0.5\n0 0.5 0.5 0.5 0",
... | 934 | 4,300,800 | 3.828312 | 69,503 |
763 | Timofey and a flat tree | [
"data structures",
"graphs",
"hashing",
"shortest paths",
"trees"
] | null | null | Little Timofey has a big tree — an undirected connected graph with *n* vertices and no simple cycles. He likes to walk along it. His tree is flat so when he walks along it he sees it entirely. Quite naturally, when he stands on a vertex, he sees the tree as a rooted tree with the root in this vertex.
Timofey assumes t... | First line contains single integer *n* (1<=≤<=*n*<=≤<=105) — number of vertices in the tree.
Each of the next *n*<=-<=1 lines contains two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=105, *u**i*<=≠<=*v**i*), denoting the vertices the *i*-th edge connects.
It is guaranteed that the given graph is a tree. | Print single integer — the index of the vertex in which Timofey should stand. If there are many answers, you can print any of them. | [
"3\n1 2\n2 3\n",
"7\n1 2\n4 2\n2 3\n5 6\n6 7\n3 7\n",
"10\n1 7\n1 8\n9 4\n5 1\n9 2\n3 5\n10 6\n10 9\n5 10\n"
] | [
"1\n",
"1\n",
"2\n"
] | In the first example we can stand in the vertex 1 or in the vertex 3 so that every subtree is non-isomorphic. If we stand in the vertex 2, then subtrees of vertices 1 and 3 are isomorphic.
In the second example, if we stand in the vertex 1, then only subtrees of vertices 4 and 5 are isomorphic.
In the third example, ... | [] | 46 | 0 | 0 | 69,514 | |
506 | Mr. Kitayuta's Colorful Graph | [
"brute force",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Mr. Kitayuta has just bought an undirected graph with *n* vertices and *m* edges. The vertices of the graph are numbered from 1 to *n*. Each edge, namely edge *i*, has a color *c**i*, connecting vertex *a**i* and *b**i*.
Mr. Kitayuta wants you to process the following *q* queries.
In the *i*-th query, he gives you tw... | The first line of the input contains space-separated two integers - *n* and *m*(2<=≤<=*n*<=≤<=105,<=1<=≤<=*m*<=≤<=105), denoting the number of the vertices and the number of the edges, respectively.
The next *m* lines contain space-separated three integers - *a**i*, *b**i*(1<=≤<=*a**i*<=<<=*b**i*<=≤<=*n*) and *c**i... | For each query, print the answer in a separate line. | [
"4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4\n",
"5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4\n"
] | [
"2\n1\n0\n",
"1\n1\n1\n1\n2\n"
] | Let's consider the first sample.
- Vertex 1 and vertex 2 are connected by color 1 and 2. - Vertex 3 and vertex 4 are connected by color 3. - Vertex 1 and vertex 4 are not connected by any single color. | [
{
"input": "4 5\n1 2 1\n1 2 2\n2 3 1\n2 3 3\n2 4 3\n3\n1 2\n3 4\n1 4",
"output": "2\n1\n0"
},
{
"input": "5 7\n1 5 1\n2 5 1\n3 5 1\n4 5 1\n1 2 2\n2 3 2\n3 4 2\n5\n1 5\n5 1\n2 5\n1 5\n1 4",
"output": "1\n1\n1\n1\n2"
},
{
"input": "2 1\n1 2 1\n1\n1 2",
"output": "1"
},
{
"input... | 30 | 0 | 0 | 69,567 | |
63 | Dividing Island | [
"constructive algorithms"
] | D. Dividing Island | 2 | 256 | A revolution took place on the Buka Island. New government replaced the old one. The new government includes *n* parties and each of them is entitled to some part of the island according to their contribution to the revolution. However, they can't divide the island.
The island can be conventionally represented as two ... | The first line contains 5 space-separated integers — *a*, *b*, *c*, *d* and *n* (1<=≤<=*a*,<=*b*,<=*c*,<=*d*<=≤<=50, *b*<=≠<=*d*, 1<=≤<=*n*<=≤<=26). The second line contains *n* space-separated numbers. The *i*-th of them is equal to number *x**i* (1<=≤<=*x**i*<=≤<=*a*<=×<=*b*<=+<=*c*<=×<=*d*). It is guaranteed that . | If dividing the island between parties in the required manner is impossible, print "NO" (without the quotes). Otherwise, print "YES" (also without the quotes) and, starting from the next line, print *max*(*b*,<=*d*) lines each containing *a*<=+<=*c* characters. To mark what square should belong to what party, use lower... | [
"3 4 2 2 3\n5 8 3\n",
"3 2 1 4 4\n1 2 3 4\n"
] | [
"YES\naaabb\naabbb\ncbb..\nccb..\n",
"YES\nabbd\ncccd\n...d\n...d\n"
] | none | [
{
"input": "3 4 2 2 3\n5 8 3",
"output": "YES\nbbbbc\nbbbcc\naab..\naaa.."
},
{
"input": "3 2 1 4 4\n1 2 3 4",
"output": "YES\ncccd\nbbad\n...d\n...d"
},
{
"input": "1 2 1 1 1\n3",
"output": "YES\naa\na."
},
{
"input": "1 2 1 3 2\n3 2",
"output": "YES\naa\nab\n.b"
},
... | 468 | 22,323,200 | -1 | 69,585 |
625 | Frog Fights | [
"data structures",
"greedy"
] | null | null | Ostap Bender recently visited frog farm and was inspired to create his own frog game.
Number of frogs are places on a cyclic gameboard, divided into *m* cells. Cells are numbered from 1 to *m*, but the board is cyclic, so cell number 1 goes right after the cell number *m* in the direction of movement. *i*-th frog duri... | First line of the input contains two integers *n* and *m* (1<=≤<=*n*<=≤<=100000,<=1<=≤<=*m*<=≤<=109,<=*n*<=≤<=*m*) — number of frogs and gameboard size, respectively.
Following *n* lines contains frogs descriptions — two integers *p**i* and *a**i* (1<=≤<=*p**i*,<=*a**i*<=≤<=*m*) — the number of cell occupied by *i*-th... | In the first line output number of frogs on the final gameboard. In the second line output their numbers in any order. | [
"3 5\n2 1\n5 3\n4 3\n",
"5 6\n1 2\n3 4\n2 5\n5 1\n6 1\n"
] | [
"1\n3 ",
"2\n1 4 "
] | In the first sample first frog jumps 1 cell and finishes in cell number 3. Second frog jumps for 3 cells and finishes on cell number 3, knocking out frog number 1. Current jump length for frog number 2 is now 2. Third frog jumps to cell 2, then second frog jumps to cell 5. Third frog in turn finishes in cell 5 and remo... | [] | 0 | 0 | -1 | 69,610 | |
97 | Superset | [
"constructive algorithms",
"divide and conquer"
] | B. Superset | 2 | 256 | A set of points on a plane is called good, if for any two points at least one of the three conditions is true:
- those two points lie on same horizontal line; - those two points lie on same vertical line; - the rectangle, with corners in these two points, contains inside or on its borders at least one point of the s... | The first line contains an integer *n* (1<=≤<=*n*<=≤<=104) — the number of points in the initial set. Next *n* lines describe the set's points. Each line contains two integers *x**i* and *y**i* (<=-<=109<=≤<=*x**i*,<=*y**i*<=≤<=109) — a corresponding point's coordinates. It is guaranteed that all the points are differe... | Print on the first line the number of points *m* (*n*<=≤<=*m*<=≤<=2·105) in a good superset, print on next *m* lines the points. The absolute value of the points' coordinates should not exceed 109. Note that you should not minimize *m*, it is enough to find any good superset of the given set, whose size does not exceed... | [
"2\n1 1\n2 2\n"
] | [
"3\n1 1\n2 2\n1 2\n"
] | none | [] | 0 | 0 | -1 | 69,800 |
676 | Theseus and labyrinth | [
"graphs",
"implementation",
"shortest paths"
] | null | null | Theseus has just arrived to Crete to fight Minotaur. He found a labyrinth that has a form of a rectangular field of size *n*<=×<=*m* and consists of blocks of size 1<=×<=1.
Each block of the labyrinth has a button that rotates all blocks 90 degrees clockwise. Each block rotates around its center and doesn't change its... | The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000) — the number of rows and the number of columns in labyrinth, respectively.
Each of the following *n* lines contains *m* characters, describing the blocks of the labyrinth. The possible characters are:
- «+» means this block has ... | If Theseus is not able to get to Minotaur, then print -1 in the only line of the output. Otherwise, print the minimum number of minutes required to get to the block where Minotaur is hiding. | [
"2 2\n+*\n*U\n1 1\n2 2\n",
"2 3\n<><\n><>\n1 1\n2 1\n"
] | [
"-1",
"4"
] | Assume that Theseus starts at the block (*x*<sub class="lower-index">*T*</sub>, *y*<sub class="lower-index">*T*</sub>) at the moment 0. | [
{
"input": "2 2\n+*\n*U\n1 1\n2 2",
"output": "-1"
},
{
"input": "2 3\n<><\n><>\n1 1\n2 1",
"output": "4"
},
{
"input": "3 3\n->v\n*+|\n+*^\n3 3\n1 1",
"output": "6"
},
{
"input": "10 10\n><URRD>>+-\n>+vLLDL-v*\n*+R^v+UUR<\n<DDU>R||RR\nRL*v^UvD|R\nR>U<>DRv|R\n-D^+U^-|UD\nD^>L... | 77 | 3,891,200 | -1 | 70,012 | |
850 | Rainbow Balls | [
"math"
] | null | null | You have a bag of balls of *n* different colors. You have *a**i* balls of the *i*-th color.
While there are at least two different colored balls in the bag, perform the following steps:
- Take out two random balls without replacement one by one. These balls might be the same color. - Color the second ball to the c... | The first line of input will contain a single integer *n* (1<=≤<=*n*<=≤<=2<=500) — the number of colors.
The next line of input will contain *n* space separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=105) — the number of balls of each color. | Print a single integer, the answer to the problem. | [
"2\n1 1\n",
"3\n1 2 3\n"
] | [
"1\n",
"750000026\n"
] | In the first sample, no matter what happens, the balls will become the same color after one step.
For the second sample, we have 6 balls. Let’s label the balls from 1 to 6, and without loss of generality, let’s say balls 1,2,3 are initially color 1, balls 4,5 are color 2, and ball 6 are color 3.
Here is an example of... | [] | 46 | 0 | 0 | 70,089 | |
830 | Bamboo Partition | [
"brute force",
"data structures",
"implementation",
"math",
"number theory",
"sortings",
"two pointers"
] | null | null | Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are *n* bamboos in a row, and the *i*-th from the left is *a**i* meters high.
Vladimir has just planted *n* bamboos in a ro... | The first line contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100, 1<=≤<=*k*<=≤<=1011) — the number of bamboos and the maximum total length of cut parts, in meters.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109) — the required heights of bamboos, in meters. | Print a single integer — the maximum value of *d* such that Vladimir can reach his goal. | [
"3 4\n1 3 5\n",
"3 40\n10 30 50\n"
] | [
"3\n",
"32\n"
] | In the first example Vladimir can check bamboos each 3 days. Then he will cut the first and the second bamboos after 3 days, and the third bamboo after 6 days. The total length of cut parts is 2 + 0 + 1 = 3 meters. | [
{
"input": "3 4\n1 3 5",
"output": "3"
},
{
"input": "3 40\n10 30 50",
"output": "32"
},
{
"input": "20 53\n32 64 20 41 97 50 20 66 68 22 60 74 61 97 54 80 30 72 59 18",
"output": "6"
},
{
"input": "100 82\n51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 2... | 2,000 | 4,608,000 | 0 | 70,221 | |
930 | Coins Exhibition | [
"data structures",
"dp",
"math"
] | null | null | Arkady and Kirill visited an exhibition of rare coins. The coins were located in a row and enumerated from left to right from 1 to *k*, each coin either was laid with its obverse (front) side up, or with its reverse (back) side up.
Arkady and Kirill made some photos of the coins, each photo contained a segment of neig... | The first line contains three integers *k*, *n* and *m* (1<=≤<=*k*<=≤<=109, 0<=≤<=*n*,<=*m*<=≤<=105) — the total number of coins, the number of photos made by Arkady, and the number of photos made by Kirill, respectively.
The next *n* lines contain the descriptions of Arkady's photos, one per line. Each of these lines... | Print the only line — the number of ways to choose the side for each coin modulo 109<=+<=7<==<=1000000007. | [
"5 2 2\n1 3\n3 5\n2 2\n4 5\n",
"5 3 2\n1 3\n2 2\n3 5\n2 2\n4 5\n",
"60 5 7\n1 3\n50 60\n1 60\n30 45\n20 40\n4 5\n6 37\n5 18\n50 55\n22 27\n25 31\n44 45\n"
] | [
"8\n",
"0\n",
"732658600\n"
] | In the first example the following ways are possible ('O' — obverse, 'R' — reverse side):
- OROOR, - ORORO, - ORORR, - RROOR, - RRORO, - RRORR, - ORROR, - ORRRO.
In the second example the information is contradictory: the second coin should have obverse and reverse sides up at the same time, that is impossib... | [] | 31 | 0 | 0 | 70,363 | |
107 | Crime Management | [
"dp",
"graphs",
"matrices"
] | D. Crime Management | 5 | 256 | Zeyad wants to commit *n* crimes in Egypt and not be punished at the end. There are several types of crimes. For example, bribery is a crime but is not considered such when repeated twice. Therefore, bribery is not considered a crime when repeated an even number of times. Speeding is a crime, but is not considered such... | The first line contains two integers *n* and *c* (0<=≤<=*n*<=≤<=1018,<=0<=≤<=*c*<=≤<=1000) — the number of crimes Zeyad would like to commit and the number of conditions he is aware of.
Then the definitions for *c* conditions follow. There are 26 types of crimes. Each crime definition consists of crime type — a capita... | Output the number of different ways Zeyad can commit exactly *n* crimes with no punishment modulo 12345. | [
"5 2\nA 1\nB 2\n",
"6 3\nA 1\nB 2\nC 3\n",
"8 3\nA 2\nA 3\nB 2\n"
] | [
"16\n",
"113\n",
"128\n"
] | In the first test case, the 16 ways are: AAAAA, AAABB, AABAB, AABBA, ABAAB, ABABA, ABBAA, BAAAB, BAABA, BABAA, BBAAA, ABBBB, BABBB, BBABB, BBBAB, BBBBA. | [] | 60 | 0 | 0 | 70,406 |
261 | Maxim and Matrix | [
"constructive algorithms",
"dp",
"math"
] | null | null | Maxim loves to fill in a matrix in a special manner. Here is a pseudocode of filling in a matrix of size (*m*<=+<=1)<=×<=(*m*<=+<=1):
Maxim asks you to count, how many numbers *m* (1<=≤<=*m*<=≤<=*n*) are there, such that the sum of values in the cells in the row number *m*<=+<=1 of the resulting matrix equals *t*.
... | A single line contains two integers *n* and *t* (1<=≤<=*n*,<=*t*<=≤<=1012,<=*t*<=≤<=*n*<=+<=1).
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. | In a single line print a single integer — the answer to the problem. | [
"1 1\n",
"3 2\n",
"3 3\n",
"1000000000000 1048576\n"
] | [
"1\n",
"1\n",
"0\n",
"118606527258\n"
] | none | [
{
"input": "1 1",
"output": "1"
},
{
"input": "3 2",
"output": "1"
},
{
"input": "3 3",
"output": "0"
},
{
"input": "1000000000000 1048576",
"output": "118606527258"
},
{
"input": "35 4",
"output": "11"
},
{
"input": "70 32",
"output": "1"
},
{... | 62 | 0 | 0 | 70,420 | |
626 | Group Projects | [
"dp"
] | null | null | There are *n* students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the *i*-th student *a**i* minutes to finish his/her independent piece.
If students work at differen... | The first line contains two space-separated integers *n* and *k* (1<=≤<=*n*<=≤<=200, 0<=≤<=*k*<=≤<=1000) — the number of students and the maximum total imbalance allowed, respectively.
The second line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=500) — the time it takes the *i*-th student to complete... | Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109<=+<=7. | [
"3 2\n2 4 5\n",
"4 3\n7 8 9 10\n",
"4 0\n5 10 20 21\n"
] | [
"3\n",
"13\n",
"1\n"
] | In the first sample, we have three options:
- The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2. - The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1. - All three students form their own groups. T... | [
{
"input": "3 2\n2 4 5",
"output": "3"
},
{
"input": "4 3\n7 8 9 10",
"output": "13"
},
{
"input": "4 0\n5 10 20 21",
"output": "1"
},
{
"input": "20 1000\n50 50 100 100 150 150 200 200 250 250 300 300 350 350 400 400 450 450 500 500",
"output": "97456952"
},
{
"i... | 30 | 0 | 0 | 70,517 | |
603 | Ruminations on Ruminants | [
"geometry",
"math"
] | null | null | Kevin Sun is ruminating on the origin of cows while standing at the origin of the Cartesian plane. He notices *n* lines on the plane, each representable by an equation of the form *ax*<=+<=*by*<==<=*c*. He also observes that no two lines are parallel and that no three lines pass through the same point.
For each tripl... | The first line of the input contains a single integer *n* (3<=≤<=*n*<=≤<=2000), the number of lines.
The next *n* lines describe lines . The *i*-th of these lines contains three space-separated integers *a**i*,<=*b**i*,<=*c**i* (|*a**i*|,<=|*b**i*|,<=|*c**i*|<=≤<=10<=000,<=*a**i*2<=+<=*b**i*2<=><=0), representing t... | Print a single integer, the number of triples (*i*,<=*j*,<=*k*) with *i*<=<<=*j*<=<<=*k* such that lines form an original triangle. | [
"4\n1 0 0\n0 1 0\n1 1 -1\n1 -1 2\n",
"3\n0 1 1\n1 1 2\n1 -1 -2\n"
] | [
"2\n",
"1\n"
] | Note that in the first sample, some of the lines pass through the origin.
In the second sample, there is exactly one triple of lines: *y* = 1, *x* + *y* = 2, *x* - *y* = - 2. The triangle they form has vertices (0, 2), (1, 1), ( - 1, 1). The circumcircle of this triangle has equation *x*<sup class="upper-index">2</su... | [] | 31 | 0 | 0 | 70,632 | |
681 | Gifts by the List | [
"constructive algorithms",
"dfs and similar",
"graphs",
"trees"
] | null | null | Sasha lives in a big happy family. At the Man's Day all the men of the family gather to celebrate it following their own traditions. There are *n* men in Sasha's family, so let's number them with integers from 1 to *n*.
Each man has at most one father but may have arbitrary number of sons.
Man number *A* is considere... | In the first line of the input two integers *n* and *m* (0<=≤<=*m*<=<<=*n*<=≤<=100<=000) are given — the number of the men in the Sasha's family and the number of family relations in it respectively.
The next *m* lines describe family relations: the (*i*<=+<=1)*th* line consists of pair of integers *p**i* and *q**i... | Print an integer *k* (1<=≤<=*k*<=≤<=*n*) — the number of the men in the list of candidates, in the first line.
Print then *k* pairwise different positive integers not exceeding *n* — the numbers of the men in the list in an order satisfying every of the men's wishes, one per line.
If there are more than one appropria... | [
"3 2\n1 2\n2 3\n1 2 1\n",
"4 2\n1 2\n3 4\n1 2 3 3\n"
] | [
"-1",
"3\n2\n1\n3\n"
] | The first sample explanation:
- if there would be no 1 in the list then the first and the third man's wishes would not be satisfied (*a*<sub class="lower-index">1</sub> = *a*<sub class="lower-index">3</sub> = 1); - if there would be no 2 in the list then the second man wish would not be satisfied (*a*<sub class="lo... | [
{
"input": "3 2\n1 2\n2 3\n1 2 1",
"output": "-1"
},
{
"input": "4 2\n1 2\n3 4\n1 2 3 3",
"output": "3\n2\n1\n3"
},
{
"input": "1 0\n1",
"output": "1\n1"
},
{
"input": "2 1\n2 1\n2 2",
"output": "1\n2"
},
{
"input": "2 1\n2 1\n1 2",
"output": "2\n1\n2"
},
... | 31 | 102,400 | 0 | 70,903 | |
623 | Birthday | [
"greedy",
"math",
"probabilities"
] | null | null | A MIPT student named Misha has a birthday today, and he decided to celebrate it in his country house in suburban Moscow. *n* friends came by, and after a typical party they decided to play blind man's buff.
The birthday boy gets blindfolded and the other players scatter around the house. The game is played in several ... | The first line of the input contains a single integer *n* (1<=≤<=*n*<=≤<=100) — the number of Misha's friends.
The second line contains *n* integers *p**i* (), giving the probability to catch the *i*-th friend in one particular round in percent. | Print a single real value — the expectation of the number of rounds provided that Misha plays optimally. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider... | [
"2\n50 50\n",
"4\n50 20 20 10\n"
] | [
"5.0000000000\n",
"39.2846263444\n"
] | The optimal strategy in the first sample is to guess friends alternately. | [
{
"input": "2\n50 50",
"output": "5.0000000000"
},
{
"input": "4\n50 20 20 10",
"output": "39.2846263444"
},
{
"input": "1\n100",
"output": "1.0000000000"
},
{
"input": "2\n41 59",
"output": "5.0966935927"
},
{
"input": "6\n14 14 18 21 20 13",
"output": "83.75... | 498 | 921,600 | 0 | 71,079 | |
60 | Mushroom Strife | [
"brute force",
"dfs and similar"
] | C. Mushroom Strife | 2 | 256 | Pasha and Akim were making a forest map — the lawns were the graph's vertexes and the roads joining the lawns were its edges. They decided to encode the number of laughy mushrooms on every lawn in the following way: on every edge between two lawns they wrote two numbers, the greatest common divisor (GCD) and the least ... | The first line contains two numbers *n* and *m* () which are the numbers of lawns and roads we know about. Each of the following *m* lines contains four numbers which are the numbers of lawns the road connects, the GCD and the LCM of the numbers of mushrooms on these lawns (1<=≤<=*GCD*,<=*LCM*<=≤<=106).
It is guarante... | The answer should contain "YES" or "NO" on the first line, saying whether it is possible or not to perform the arrangement. If the answer is "YES", print on the following line *n* numbers which are the numbers of mushrooms on the corresponding lawns. | [
"1 0\n",
"2 1\n1 2 1 3\n",
"3 2\n3 2 1 2\n3 1 1 10\n",
"2 1\n1 2 3 7\n"
] | [
"YES\n1 ",
"YES\n1 3 ",
"YES\n5 1 2 ",
"NO\n"
] | none | [
{
"input": "1 0",
"output": "YES\n1 "
},
{
"input": "2 1\n1 2 1 3",
"output": "YES\n1 3 "
},
{
"input": "3 2\n3 2 1 2\n3 1 1 10",
"output": "YES\n5 1 2 "
},
{
"input": "2 1\n1 2 3 7",
"output": "NO"
},
{
"input": "3 2\n3 2 1 34\n3 1 1 26",
"output": "YES\n13 1... | 62 | 0 | 0 | 71,274 |
119 | Education Reform | [
"dp"
] | null | null | Yet another education system reform has been carried out in Berland recently. The innovations are as follows:
An academic year now consists of *n* days. Each day pupils study exactly one of *m* subjects, besides, each subject is studied for no more than one day. After the lessons of the *i*-th subject pupils get the h... | The first line contains three integers *n*, *m*, *k* (1<=≤<=*n*<=≤<=*m*<=≤<=50, 1<=≤<=*k*<=≤<=100) which represent the number of days in an academic year, the number of subjects and the *k* parameter correspondingly. Each of the following *m* lines contains the description of a subject as three integers *a**i*, *b**i*,... | If no valid solution exists, print the single word "NO" (without the quotes). Otherwise, the first line should contain the word "YES" (without the quotes) and the next *n* lines should contain any timetable that satisfies all the conditions. The *i*<=+<=1-th line should contain two positive integers: the number of the ... | [
"4 5 2\n1 10 1\n1 10 2\n1 10 3\n1 20 4\n1 100 5\n",
"3 4 3\n1 3 1\n2 4 4\n2 3 3\n2 2 2\n"
] | [
"YES\n2 8\n3 10\n4 20\n5 40\n",
"NO"
] | none | [] | 15 | 0 | 0 | 71,305 | |
0 | none | [
"none"
] | null | null | As a German University in Cairo (GUC) student and a basketball player, Herr Wafa was delighted once he heard the news. GUC is finally participating in the Annual Basketball Competition (ABC).
A team is to be formed of *n* players, all of which are GUC students. However, the team might have players belonging to differ... | The first line contains three integers *n*, *m* and *h* (1<=≤<=*n*<=≤<=100,<=1<=≤<=*m*<=≤<=1000,<=1<=≤<=*h*<=≤<=*m*) — the number of players on the team, the number of departments in GUC and Herr Wafa's department, correspondingly.
The second line contains a single-space-separated list of *m* integers *s**i* (1<=≤<=*... | Print the probability that Herr Wafa will have at least one teammate from his department. If there is not enough basketball players in GUC to participate in ABC, print -1. The answer will be accepted if it has absolute or relative error not exceeding 10<=-<=6. | [
"3 2 1\n2 1\n",
"3 2 1\n1 1\n",
"3 2 1\n2 2\n"
] | [
"1\n",
"-1\n",
"0.666667\n"
] | In the first example all 3 players (2 from department 1 and 1 from department 2) must be chosen for the team. Both players from Wafa's departments will be chosen, so he's guaranteed to have a teammate from his department.
In the second example, there are not enough players.
In the third example, there are three possi... | [
{
"input": "3 2 1\n2 1",
"output": "1"
},
{
"input": "3 2 1\n1 1",
"output": "-1"
},
{
"input": "3 2 1\n2 2",
"output": "0.666667"
},
{
"input": "3 2 1\n1 2",
"output": "0.000000"
},
{
"input": "6 5 3\n5 2 3 10 5",
"output": "0.380435"
},
{
"input": "7... | 202 | 2,048,000 | -1 | 71,317 | |
38 | Vasya the Architect | [
"implementation"
] | D. Vasya the Architect | 2 | 256 | Once Vasya played bricks. All the bricks in the set had regular cubical shape. Vasya vas a talented architect, however the tower he built kept falling apart.
Let us consider the building process. Vasya takes a brick and puts it on top of the already built tower so that the sides of the brick are parallel to the sides ... | The first input file contains an integer *n* (1<=≤<=*n*<=≤<=100) which is the number of bricks. Each of the next *n* lines contains four numbers *x**i*,<=1,<=*y**i*,<=1,<=*x**i*,<=2,<=*y**i*,<=2 (*x**i*,<=1<=≠<=*x**i*,<=2,<=|*x**i*,<=1<=-<=*x**i*,<=2|<==<=|*y**i*,<=1<=-<=*y**i*,<=2|) which are the coordinates of the op... | Print the number of bricks in the maximal stable tower. | [
"2\n0 0 3 3\n1 0 4 3\n",
"2\n0 0 3 3\n2 0 5 3\n",
"3\n0 0 3 3\n1 0 4 3\n2 0 5 3\n"
] | [
"2\n",
"1\n",
"3\n"
] | none | [
{
"input": "2\n0 0 3 3\n1 0 4 3",
"output": "2"
},
{
"input": "2\n0 0 3 3\n2 0 5 3",
"output": "1"
},
{
"input": "3\n0 0 3 3\n1 0 4 3\n2 0 5 3",
"output": "3"
},
{
"input": "5\n7 -10 -8 5\n4 -7 -5 2\n2 -5 -3 0\n-9 48 50 -11\n50 -4 -2 48",
"output": "3"
},
{
"input... | 278 | 3,686,400 | 0 | 71,452 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.