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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | none | [
"none"
] | null | null | One must train much to do well on wizardry contests. So, there are numerous wizardry schools and magic fees.
One of such magic schools consists of *n* tours. A winner of each tour gets a huge prize. The school is organised quite far away, so one will have to take all the prizes home in one go. And the bags that you've brought with you have space for no more than *k* huge prizes.
Besides the fact that you want to take all the prizes home, you also want to perform well. You will consider your performance good if you win at least *l* tours.
In fact, years of organizing contests proved to the organizers that transporting huge prizes is an issue for the participants. Alas, no one has ever invented a spell that would shrink the prizes... So, here's the solution: for some tours the winner gets a bag instead of a huge prize. Each bag is characterized by number *a**i* β the number of huge prizes that will fit into it.
You already know the subject of all tours, so you can estimate the probability *p**i* of winning the *i*-th tour. You cannot skip the tour under any circumstances.
Find the probability that you will perform well on the contest and will be able to take all won prizes home (that is, that you will be able to fit all the huge prizes that you won into the bags that you either won or brought from home). | The first line contains three integers *n*, *l*, *k* (1<=β€<=*n*<=β€<=200,<=0<=β€<=*l*,<=*k*<=β€<=200) β the number of tours, the minimum number of tours to win, and the number of prizes that you can fit in the bags brought from home, correspondingly.
The second line contains *n* space-separated integers, *p**i* (0<=β€<=*p**i*<=β€<=100) β the probability to win the *i*-th tour, in percents.
The third line contains *n* space-separated integers, *a**i* (1<=β€<=*a**i*<=β€<=200) β the capacity of the bag that will be awarded to you for winning the *i*-th tour, or else -1, if the prize for the *i*-th tour is a huge prize and not a bag. | Print a single real number β the answer to the problem. The answer will be accepted if the absolute or relative error does not exceed 10<=-<=6. | [
"3 1 0\n10 20 30\n-1 -1 2\n",
"1 1 1\n100\n123\n"
] | [
"0.300000000000\n",
"1.000000000000\n"
] | In the first sample we need either win no tour or win the third one. If we win nothing we wouldn't perform well. So, we must to win the third tour. Other conditions will be satisfied in this case. Probability of wining the third tour is 0.3.
In the second sample we win the only tour with probability 1.0, and go back home with bag for it. | [
{
"input": "3 1 0\n10 20 30\n-1 -1 2",
"output": "0.300000000000"
},
{
"input": "1 1 1\n100\n123",
"output": "1.000000000000"
},
{
"input": "5 1 2\n36 44 13 83 63\n-1 2 -1 2 1",
"output": "0.980387276800"
},
{
"input": "9 9 2\n91 96 99 60 42 67 46 39 62\n5 -1 2 -1 -1 -1 7 -1 ... | 186 | 102,400 | 0 | 28,134 | |
802 | April Fools' Problem (hard) | [
"binary search",
"data structures",
"flows"
] | null | null | The plans for HC2 are rather far-fetched: we are just over 500 000 days away from HC2 3387, for example, and accordingly we are planning to have a couple hundred thousand problems in that edition (we hope that programming contests will become wildly more popular). The marmots need to get to work, and they could use a good plan... | Same as the medium version, but the limits have changed: 1<=β€<=*k*<=β€<=*n*<=β€<=500<=000. | Same as the medium version. | [
"8 4\n3 8 7 9 9 4 6 8\n2 5 9 4 3 8 9 1\n"
] | [
"32"
] | none | [] | 0 | 0 | -1 | 28,223 | |
113 | Petr# | [
"brute force",
"data structures",
"hashing",
"strings"
] | B. Petr# | 2 | 256 | Long ago, when Petya was a schoolboy, he was very much interested in the Petr# language grammar. During one lesson Petya got interested in the following question: how many different continuous substrings starting with the *s**begin* and ending with the *s**end* (it is possible *s**begin*<==<=*s**end*), the given string *t* has. Substrings are different if and only if their contents aren't equal, their positions of occurence don't matter. Petya wasn't quite good at math, that's why he couldn't count this number. Help him! | The input file consists of three lines. The first line contains string *t*. The second and the third lines contain the *s**begin* and *s**end* identificators, correspondingly. All three lines are non-empty strings consisting of lowercase Latin letters. The length of each string doesn't exceed 2000 characters. | Output the only number β the amount of different substrings of *t* that start with *s**begin* and end with *s**end*. | [
"round\nro\nou\n",
"codeforces\ncode\nforca\n",
"abababab\na\nb\n",
"aba\nab\nba\n"
] | [
"1\n",
"0\n",
"4\n",
"1\n"
] | In the third sample there are four appropriate different substrings. They are: ab, abab, ababab, abababab.
In the fourth sample identificators intersect. | [
{
"input": "round\nro\nou",
"output": "1"
},
{
"input": "codeforces\ncode\nforca",
"output": "0"
},
{
"input": "abababab\na\nb",
"output": "4"
},
{
"input": "aba\nab\nba",
"output": "1"
},
{
"input": "abcdefghijklmnopqrstuvwxyz\nabc\nxyz",
"output": "1"
},
... | 154 | 6,041,600 | 0 | 28,242 |
792 | Colored Balls | [
"greedy",
"math",
"number theory"
] | null | null | There are *n* boxes with colored balls on the table. Colors are numbered from 1 to *n*. *i*-th box contains *a**i* balls, all of which have color *i*. You have to write a program that will divide all balls into sets such that:
- each ball belongs to exactly one of the sets, - there are no empty sets, - there is no set containing two (or more) balls of different colors (each set contains only balls of one color), - there are no two sets such that the difference between their sizes is greater than 1.
Print the minimum possible number of sets. | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=500).
The second line contains *n* integer numbers *a*1,<=*a*2,<=... ,<=*a**n* (1<=β€<=*a**i*<=β€<=109). | Print one integer number β the minimum possible number of sets. | [
"3\n4 7 8\n",
"2\n2 7\n"
] | [
"5\n",
"4\n"
] | In the first example the balls can be divided into sets like that: one set with 4 balls of the first color, two sets with 3 and 4 balls, respectively, of the second color, and two sets with 4 balls of the third color. | [
{
"input": "3\n4 7 8",
"output": "5"
},
{
"input": "2\n2 7",
"output": "4"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "1\n1000000000",
"output": "1"
},
{
"input": "2\n1000000000 1",
"output": "500000001"
},
{
"input": "2\n9 6",
"output": "5"... | 1,000 | 7,065,600 | 0 | 28,269 | |
557 | Vitaly and Cycle | [
"combinatorics",
"dfs and similar",
"graphs",
"math"
] | null | null | After Vitaly was expelled from the university, he became interested in the graph theory.
Vitaly especially liked the cycles of an odd length in which each vertex occurs at most once.
Vitaly was wondering how to solve the following problem. You are given an undirected graph consisting of *n* vertices and *m* edges, not necessarily connected, without parallel edges and loops. You need to find *t* β the minimum number of edges that must be added to the given graph in order to form a simple cycle of an odd length, consisting of more than one vertex. Moreover, he must find *w* β the number of ways to add *t* edges in order to form a cycle of an odd length (consisting of more than one vertex). It is prohibited to add loops or parallel edges.
Two ways to add edges to the graph are considered equal if they have the same sets of added edges.
Since Vitaly does not study at the university, he asked you to help him with this task. | The first line of the input contains two integers *n* and *m* (Β βΒ the number of vertices in the graph and the number of edges in the graph.
Next *m* lines contain the descriptions of the edges of the graph, one edge per line. Each edge is given by a pair of integers *a**i*, *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*)Β βΒ the vertices that are connected by the *i*-th edge. All numbers in the lines are separated by a single space.
It is guaranteed that the given graph doesn't contain any loops and parallel edges. The graph isn't necessarily connected. | Print in the first line of the output two space-separated integers *t* and *w*Β βΒ the minimum number of edges that should be added to the graph to form a simple cycle of an odd length consisting of more than one vertex where each vertex occurs at most once, and the number of ways to do this. | [
"4 4\n1 2\n1 3\n4 2\n4 3\n",
"3 3\n1 2\n2 3\n3 1\n",
"3 0\n"
] | [
"1 2\n",
"0 1\n",
"3 1\n"
] | The simple cycle is a cycle that doesn't contain any vertex twice. | [
{
"input": "4 4\n1 2\n1 3\n4 2\n4 3",
"output": "1 2"
},
{
"input": "3 3\n1 2\n2 3\n3 1",
"output": "0 1"
},
{
"input": "3 0",
"output": "3 1"
},
{
"input": "6 3\n1 2\n4 3\n6 5",
"output": "2 12"
},
{
"input": "100000 0",
"output": "3 166661666700000"
},
{... | 514 | 10,649,600 | 3 | 28,346 | |
811 | Vladik and Memorable Trip | [
"dp",
"implementation"
] | null | null | Vladik often travels by trains. He remembered some of his trips especially well and I would like to tell you about one of these trips:
Vladik is at initial train station, and now *n* people (including Vladik) want to get on the train. They are already lined up in some order, and for each of them the city code *a**i* is known (the code of the city in which they are going to).
Train chief selects some number of disjoint segments of the original sequence of people (covering entire sequence by segments is not necessary). People who are in the same segment will be in the same train carriage. The segments are selected in such way that if at least one person travels to the city *x*, then all people who are going to city *x* should be in the same railway carriage. This means that they canβt belong to different segments. Note, that all people who travel to the city *x*, either go to it and in the same railway carriage, or do not go anywhere at all.
Comfort of a train trip with people on segment from position *l* to position *r* is equal to XOR of all distinct codes of cities for people on the segment from position *l* to position *r*. XOR operation also known as exclusive OR.
Total comfort of a train trip is equal to sum of comfort for each segment.
Help Vladik to know maximal possible total comfort. | First line contains single integer *n* (1<=β€<=*n*<=β€<=5000)Β β number of people.
Second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=5000), where *a**i* denotes code of the city to which *i*-th person is going. | The output should contain a single integerΒ β maximal possible total comfort. | [
"6\n4 4 2 5 2 3\n",
"9\n5 1 3 1 5 2 4 2 5\n"
] | [
"14\n",
"9\n"
] | In the first test case best partition into segments is: [4,β4] [2,β5,β2] [3], answer is calculated as follows: 4β+β(2 *xor* 5)β+β3β=β4β+β7β+β3β=β14
In the second test case best partition into segments is: 5 1 [3] 1 5 [2,β4,β2] 5, answer calculated as follows: 3β+β(2 *xor* 4)β=β3β+β6β=β9. | [
{
"input": "6\n4 4 2 5 2 3",
"output": "14"
},
{
"input": "9\n5 1 3 1 5 2 4 2 5",
"output": "9"
},
{
"input": "5\n1558 4081 3591 1700 3232",
"output": "14162"
},
{
"input": "10\n3838 1368 4825 2068 4755 2048 1342 4909 2837 4854",
"output": "32844"
},
{
"input": "1... | 93 | 0 | 0 | 28,409 | |
620 | New Year Tree | [
"bitmasks",
"data structures",
"trees"
] | null | null | The New Year holidays are over, but Resha doesn't want to throw away the New Year tree. He invited his best friends Kerim and Gural to help him to redecorate the New Year tree.
The New Year tree is an undirected tree with *n* vertices and root in the vertex 1.
You should process the queries of the two types:
1. Change the colours of all vertices in the subtree of the vertex *v* to the colour *c*. 1. Find the number of different colours in the subtree of the vertex *v*. | The first line contains two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=4Β·105) β the number of vertices in the tree and the number of the queries.
The second line contains *n* integers *c**i* (1<=β€<=*c**i*<=β€<=60) β the colour of the *i*-th vertex.
Each of the next *n*<=-<=1 lines contains two integers *x**j*,<=*y**j* (1<=β€<=*x**j*,<=*y**j*<=β€<=*n*) β the vertices of the *j*-th edge. It is guaranteed that you are given correct undirected tree.
The last *m* lines contains the description of the queries. Each description starts with the integer *t**k* (1<=β€<=*t**k*<=β€<=2) β the type of the *k*-th query. For the queries of the first type then follows two integers *v**k*,<=*c**k* (1<=β€<=*v**k*<=β€<=*n*,<=1<=β€<=*c**k*<=β€<=60) β the number of the vertex whose subtree will be recoloured with the colour *c**k*. For the queries of the second type then follows integer *v**k* (1<=β€<=*v**k*<=β€<=*n*) β the number of the vertex for which subtree you should find the number of different colours. | For each query of the second type print the integer *a* β the number of different colours in the subtree of the vertex given in the query.
Each of the numbers should be printed on a separate line in order of query appearing in the input. | [
"7 10\n1 1 1 1 1 1 1\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n1 3 2\n2 1\n1 4 3\n2 1\n1 2 5\n2 1\n1 6 4\n2 1\n2 2\n2 3\n",
"23 30\n1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n4 9\n4 10\n4 11\n6 12\n6 13\n7 14\n7 15\n7 16\n8 17\n8 18\n10 19\n10 20\n10 21\n11 22\n11 23\n2 1\n2 5\n2 6\n2 ... | [
"2\n3\n4\n5\n1\n2\n",
"6\n1\n3\n3\n2\n1\n2\n3\n5\n5\n1\n2\n2\n1\n1\n1\n2\n3\n"
] | none | [
{
"input": "7 10\n1 1 1 1 1 1 1\n1 2\n1 3\n1 4\n3 5\n3 6\n3 7\n1 3 2\n2 1\n1 4 3\n2 1\n1 2 5\n2 1\n1 6 4\n2 1\n2 2\n2 3",
"output": "2\n3\n4\n5\n1\n2"
},
{
"input": "23 30\n1 2 2 6 5 3 2 1 1 1 2 4 5 3 4 4 3 3 3 3 3 4 6\n1 2\n1 3\n1 4\n2 5\n2 6\n3 7\n3 8\n4 9\n4 10\n4 11\n6 12\n6 13\n7 14\n7 15\n7 16... | 3,000 | 36,352,000 | 0 | 28,443 | |
160 | Edges in MST | [
"dfs and similar",
"dsu",
"graphs",
"sortings"
] | null | null | You are given a connected weighted undirected graph without any loops and multiple edges.
Let us remind you that a graph's spanning tree is defined as an acyclic connected subgraph of the given graph that includes all of the graph's vertexes. The weight of a tree is defined as the sum of weights of the edges that the given tree contains. The minimum spanning tree (MST) of a graph is defined as the graph's spanning tree having the minimum possible weight. For any connected graph obviously exists the minimum spanning tree, but in the general case, a graph's minimum spanning tree is not unique.
Your task is to determine the following for each edge of the given graph: whether it is either included in any MST, or included at least in one MST, or not included in any MST. | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=105, ) β the number of the graph's vertexes and edges, correspondingly. Then follow *m* lines, each of them contains three integers β the description of the graph's edges as "*a**i* *b**i* *w**i*" (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=1<=β€<=*w**i*<=β€<=106,<=*a**i*<=β <=*b**i*), where *a**i* and *b**i* are the numbers of vertexes connected by the *i*-th edge, *w**i* is the edge's weight. It is guaranteed that the graph is connected and doesn't contain loops or multiple edges. | Print *m* lines β the answers for all edges. If the *i*-th edge is included in any MST, print "any"; if the *i*-th edge is included at least in one MST, print "at least one"; if the *i*-th edge isn't included in any MST, print "none". Print the answers for the edges in the order in which the edges are specified in the input. | [
"4 5\n1 2 101\n1 3 100\n2 3 2\n2 4 2\n3 4 1\n",
"3 3\n1 2 1\n2 3 1\n1 3 2\n",
"3 3\n1 2 1\n2 3 1\n1 3 1\n"
] | [
"none\nany\nat least one\nat least one\nany\n",
"any\nany\nnone\n",
"at least one\nat least one\nat least one\n"
] | In the second sample the MST is unique for the given graph: it contains two first edges.
In the third sample any two edges form the MST for the given graph. That means that each edge is included at least in one MST. | [] | 30 | 102,400 | 0 | 28,446 | |
722 | Research Rover | [
"combinatorics",
"dp"
] | null | null | Unfortunately, the formal description of the task turned out to be too long, so here is the legend.
Research rover finally reached the surface of Mars and is ready to complete its mission. Unfortunately, due to the mistake in the navigation system design, the rover is located in the wrong place.
The rover will operate on the grid consisting of *n* rows and *m* columns. We will define as (*r*,<=*c*) the cell located in the row *r* and column *c*. From each cell the rover is able to move to any cell that share a side with the current one.
The rover is currently located at cell (1,<=1) and has to move to the cell (*n*,<=*m*). It will randomly follow some shortest path between these two cells. Each possible way is chosen equiprobably.
The cargo section of the rover contains the battery required to conduct the research. Initially, the battery charge is equal to *s* units of energy.
Some of the cells contain anomaly. Each time the rover gets to the cell with anomaly, the battery looses half of its charge rounded down. Formally, if the charge was equal to *x* before the rover gets to the cell with anomaly, the charge will change to .
While the rover picks a random shortest path to proceed, compute the expected value of the battery charge after it reaches cell (*n*,<=*m*). If the cells (1,<=1) and (*n*,<=*m*) contain anomaly, they also affect the charge of the battery. | The first line of the input contains four integers *n*, *m*, *k* and *s* (1<=β€<=*n*,<=*m*<=β€<=100<=000, 0<=β€<=*k*<=β€<=2000, 1<=β€<=*s*<=β€<=1<=000<=000)Β β the number of rows and columns of the field, the number of cells with anomaly and the initial charge of the battery respectively.
The follow *k* lines containing two integers *r**i* and *c**i* (1<=β€<=*r**i*<=β€<=*n*, 1<=β€<=*c**i*<=β€<=*m*)Β β coordinates of the cells, containing anomaly. It's guaranteed that each cell appears in this list no more than once. | The answer can always be represented as an irreducible fraction . Print the only integer *P*Β·*Q*<=-<=1 modulo 109<=+<=7. | [
"3 3 2 11\n2 1\n2 3\n",
"4 5 3 17\n1 2\n3 3\n4 1\n",
"1 6 2 15\n1 1\n1 5\n"
] | [
"333333342\n",
"514285727\n",
"4\n"
] | In the first sample, the rover picks one of the following six routes:
1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c9ec158c30775d6289140a3854e05168b09af399.png" style="max-width: 100.0%;max-height: 100.0%;"/>, after passing cell (2,β3) charge is equal to 6. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/fcc490c05d2acb731046a7c4c861f4c9ebff3633.png" style="max-width: 100.0%;max-height: 100.0%;"/>, after passing cell (2,β3) charge is equal to 6. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8d5828aadc35714d7a3453c40de81ad186e87ab3.png" style="max-width: 100.0%;max-height: 100.0%;"/>, charge remains unchanged and equals 11. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3bc680a61ca3712bbbec0eb682f3af16ab7664a2.png" style="max-width: 100.0%;max-height: 100.0%;"/>, after passing cells (2,β1) and (2,β3) charge equals 6 and then 3. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/9a87005ef2b3eb1efc9e486e608fadf3a5b557fa.png" style="max-width: 100.0%;max-height: 100.0%;"/>, after passing cell (2,β1) charge is equal to 6. 1. <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/342ddbff927874c07e9d307d73383aa50f2117b6.png" style="max-width: 100.0%;max-height: 100.0%;"/>, after passing cell (2,β1) charge is equal to 6.
Expected value of the battery charge is calculated by the following formula:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7ddca3ad80b71df649b7beb01944a4ad8f863265.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
Thus *P*β=β19, and *Q*β=β3.
3<sup class="upper-index">β-β1</sup> modulo 10<sup class="upper-index">9</sup>β+β7 equals 333333336.
19Β·333333336β=β333333342Β (*mod*Β 10<sup class="upper-index">9</sup>β+β7) | [] | 30 | 0 | 0 | 28,467 | |
558 | A Simple Task | [
"data structures",
"sortings",
"strings"
] | null | null | This task is very simple. Given a string *S* of length *n* and *q* queries each query is on the format *i* *j* *k* which means sort the substring consisting of the characters from *i* to *j* in non-decreasing order if *k*<==<=1 or in non-increasing order if *k*<==<=0.
Output the final string after applying the queries. | The first line will contain two integers *n*,<=*q* (1<=β€<=*n*<=β€<=105, 0<=β€<=*q*<=β€<=50<=000), the length of the string and the number of queries respectively.
Next line contains a string *S* itself. It contains only lowercase English letters.
Next *q* lines will contain three integers each *i*,<=*j*,<=*k* (1<=β€<=*i*<=β€<=*j*<=β€<=*n*, ). | Output one line, the string *S* after applying the queries. | [
"10 5\nabacdabcda\n7 10 0\n5 8 1\n1 4 0\n3 6 0\n7 10 1\n",
"10 1\nagjucbvdfk\n1 10 1\n"
] | [
"cbcaaaabdd",
"abcdfgjkuv"
] | First sample test explanation:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3ac4e8cc7e335675a4a2b7b4758bfb3865377cea.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a90b5b03cf59288d8861f0142ecbdf6b12f69e5c.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1f482a91a275b6bce07eaed85312eac0cfcc6ccf.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/33b1a4a924f4bd562551ba4e40309f180dbe22e0.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bddc77fd5b02858eb2ff29819cd16a93dbd241e6.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "10 5\nabacdabcda\n7 10 0\n5 8 1\n1 4 0\n3 6 0\n7 10 1",
"output": "cbcaaaabdd"
},
{
"input": "10 1\nagjucbvdfk\n1 10 1",
"output": "abcdfgjkuv"
},
{
"input": "10 6\nrmaahmdmuo\n1 3 1\n4 6 0\n5 6 1\n7 8 0\n8 10 0\n8 9 1",
"output": "amrmahmoud"
},
{
"input": "10 5\... | 5,000 | 8,499,200 | 0 | 28,473 | |
508 | Anya and Ghosts | [
"constructive algorithms",
"greedy"
] | null | null | Anya loves to watch horror movies. In the best traditions of horror, she will be visited by *m* ghosts tonight. Anya has lots of candles prepared for the visits, each candle can produce light for exactly *t* seconds. It takes the girl one second to light one candle. More formally, Anya can spend one second to light one candle, then this candle burns for exactly *t* seconds and then goes out and can no longer be used.
For each of the *m* ghosts Anya knows the time at which it comes: the *i*-th visit will happen *w**i* seconds after midnight, all *w**i*'s are distinct. Each visit lasts exactly one second.
What is the minimum number of candles Anya should use so that during each visit, at least *r* candles are burning? Anya can start to light a candle at any time that is integer number of seconds from midnight, possibly, at the time before midnight. That means, she can start to light a candle integer number of seconds before midnight or integer number of seconds after a midnight, or in other words in any integer moment of time. | The first line contains three integers *m*, *t*, *r* (1<=β€<=*m*,<=*t*,<=*r*<=β€<=300), representing the number of ghosts to visit Anya, the duration of a candle's burning and the minimum number of candles that should burn during each visit.
The next line contains *m* space-separated numbers *w**i* (1<=β€<=*i*<=β€<=*m*, 1<=β€<=*w**i*<=β€<=300), the *i*-th of them repesents at what second after the midnight the *i*-th ghost will come. All *w**i*'s are distinct, they follow in the strictly increasing order. | If it is possible to make at least *r* candles burn during each visit, then print the minimum number of candles that Anya needs to light for that.
If that is impossible, print <=-<=1. | [
"1 8 3\n10\n",
"2 10 1\n5 8\n",
"1 1 3\n10\n"
] | [
"3\n",
"1\n",
"-1\n"
] | Anya can start lighting a candle in the same second with ghost visit. But this candle isn't counted as burning at this visit.
It takes exactly one second to light up a candle and only after that second this candle is considered burning; it means that if Anya starts lighting candle at moment x, candle is buring from second x + 1 to second x + t inclusively.
In the first sample test three candles are enough. For example, Anya can start lighting them at the 3-rd, 5-th and 7-th seconds after the midnight.
In the second sample test one candle is enough. For example, Anya can start lighting it one second before the midnight.
In the third sample test the answer is β-β1, since during each second at most one candle can burn but Anya needs three candles to light up the room at the moment when the ghost comes. | [
{
"input": "1 8 3\n10",
"output": "3"
},
{
"input": "2 10 1\n5 8",
"output": "1"
},
{
"input": "1 1 3\n10",
"output": "-1"
},
{
"input": "21 79 1\n13 42 51 60 69 77 94 103 144 189 196 203 210 215 217 222 224 234 240 260 282",
"output": "4"
},
{
"input": "125 92 2\... | 30 | 0 | -1 | 28,482 | |
893 | Counting Arrays | [
"combinatorics",
"dp",
"math",
"number theory"
] | null | null | You are given two positive integer numbers *x* and *y*. An array *F* is called an *y*-factorization of *x* iff the following conditions are met:
- There are *y* elements in *F*, and all of them are integer numbers; - .
You have to count the number of pairwise distinct arrays that are *y*-factorizations of *x*. Two arrays *A* and *B* are considered different iff there exists at least one index *i* (1<=β€<=*i*<=β€<=*y*) such that *A**i*<=β <=*B**i*. Since the answer can be very large, print it modulo 109<=+<=7. | The first line contains one integer *q* (1<=β€<=*q*<=β€<=105) β the number of testcases to solve.
Then *q* lines follow, each containing two integers *x**i* and *y**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=106). Each of these lines represents a testcase. | Print *q* integers. *i*-th integer has to be equal to the number of *y**i*-factorizations of *x**i* modulo 109<=+<=7. | [
"2\n6 3\n4 2\n"
] | [
"36\n6\n"
] | In the second testcase of the example there are six *y*-factorizations:
- {β-β4,ββ-β1}; - {β-β2,ββ-β2}; - {β-β1,ββ-β4}; - {1,β4}; - {2,β2}; - {4,β1}. | [
{
"input": "2\n6 3\n4 2",
"output": "36\n6"
},
{
"input": "1\n524288 1000000",
"output": "645043186"
},
{
"input": "1\n65536 1000000",
"output": "928522471"
},
{
"input": "1\n5612 11399",
"output": "215664246"
}
] | 1,497 | 181,555,200 | 3 | 28,483 | |
653 | Delivery Bears | [
"binary search",
"flows",
"graphs"
] | null | null | Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city.
In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with *n* nodes and *m* edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node *n*. The total weight that travels across a particular edge must not exceed the weight capacity of that edge.
Niwel has exactly *x* bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like.
Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight. | The first line contains three integers *n*, *m* and *x* (2<=β€<=*n*<=β€<=50, 1<=β€<=*m*<=β€<=500, 1<=β€<=*x*<=β€<=100<=000)Β β the number of nodes, the number of directed edges and the number of bears, respectively.
Each of the following *m* lines contains three integers *a**i*, *b**i* and *c**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*, 1<=β€<=*c**i*<=β€<=1<=000<=000). This represents a directed edge from node *a**i* to *b**i* with weight capacity *c**i*. There are no self loops and no multiple edges from one city to the other city. More formally, for each *i* and *j* that *i*<=β <=*j* it's guaranteed that *a**i*<=β <=*a**j* or *b**i*<=β <=*b**j*. It is also guaranteed that there is at least one path from node 1 to node *n*. | Print one real value on a single lineΒ β the maximum amount of weight Niwel can deliver if he uses exactly *x* bears. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct if . | [
"4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2\n",
"5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30\n"
] | [
"1.5000000000\n",
"10.2222222222\n"
] | In the first sample, Niwel has three bears. Two bears can choose the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7c0aa60a06309ef607b7159fd7f3687ea0d943ce.png" style="max-width: 100.0%;max-height: 100.0%;"/>, while one bear can choose the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a26c2f3e93c9d9be6c21cb5d2bd6ac1f99f4ff55.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Even though the bear that goes on the path <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a26c2f3e93c9d9be6c21cb5d2bd6ac1f99f4ff55.png" style="max-width: 100.0%;max-height: 100.0%;"/> can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day. | [
{
"input": "4 4 3\n1 2 2\n2 4 1\n1 3 1\n3 4 2",
"output": "1.5000000000"
},
{
"input": "5 11 23\n1 2 3\n2 3 4\n3 4 5\n4 5 6\n1 3 4\n2 4 5\n3 5 6\n1 4 2\n2 5 3\n1 5 2\n3 2 30",
"output": "10.2222222222"
},
{
"input": "10 16 63\n1 2 1\n2 10 1\n1 3 1\n3 10 1\n1 4 1\n4 10 1\n1 5 1\n5 10 1\n1... | 108 | 307,200 | 0 | 28,535 | |
260 | Dividing Kingdom | [
"binary search",
"brute force",
"data structures"
] | null | null | A country called Flatland is an infinite two-dimensional plane. Flatland has *n* cities, each of them is a point on the plane.
Flatland is ruled by king Circle IV. Circle IV has 9 sons. He wants to give each of his sons part of Flatland to rule. For that, he wants to draw four distinct straight lines, such that two of them are parallel to the *Ox* axis, and two others are parallel to the *Oy* axis. At that, no straight line can go through any city. Thus, Flatland will be divided into 9 parts, and each son will be given exactly one of these parts. Circle IV thought a little, evaluated his sons' obedience and decided that the *i*-th son should get the part of Flatland that has exactly *a**i* cities.
Help Circle find such four straight lines that if we divide Flatland into 9 parts by these lines, the resulting parts can be given to the sons so that son number *i* got the part of Flatland which contains *a**i* cities. | The first line contains integer *n* (9<=β€<=*n*<=β€<=105) β the number of cities in Flatland. Next *n* lines each contain two space-separated integers: *x**i*,<=*y**i* (<=-<=109<=β€<=*x**i*,<=*y**i*<=β€<=109) β the coordinates of the *i*-th city. No two cities are located at the same point. The last line contains nine space-separated integers: . | If there is no solution, print a single integer -1.
Otherwise, print in the first line two distinct real space-separated numbers: *x*1,<=*x*2 β the abscissas of the straight lines that are parallel to the *Oy* axis. And in the second line print two distinct real space-separated numbers: *y*1,<=*y*2 β the ordinates of the straight lines, parallel to the *Ox*. If there are multiple solutions, print any of them.
When the answer is being checked, a city is considered to lie on a straight line, if the distance between the city and the line doesn't exceed 10<=-<=6. Two straight lines are considered the same if the distance between them doesn't exceed 10<=-<=6. | [
"9\n1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2\n3 3\n1 1 1 1 1 1 1 1 1\n",
"15\n4 4\n-1 -3\n1 5\n3 -4\n-4 4\n-1 1\n3 -3\n-4 -5\n-3 3\n3 2\n4 1\n-4 2\n-2 -5\n-3 4\n-1 4\n2 1 2 1 2 1 3 2 1\n",
"10\n-2 10\n6 0\n-16 -6\n-4 13\n-4 -2\n-17 -10\n9 15\n18 16\n-5 2\n10 -5\n2 1 1 1 1 1 1 1 1\n"
] | [
"1.5000000000 2.5000000000\n1.5000000000 2.5000000000\n",
"-3.5000000000 2.0000000000\n3.5000000000 -1.0000000000\n",
"-1\n"
] | The solution for the first sample test is shown below:
The solution for the second sample test is shown below:
There is no solution for the third sample test. | [] | 124 | 20,070,400 | 0 | 28,608 | |
175 | Plane of Tanks: Pro | [
"implementation"
] | null | null | Vasya has been playing Plane of Tanks with his friends the whole year. Now it is time to divide the participants into several categories depending on their results.
A player is given a non-negative integer number of points in each round of the Plane of Tanks. Vasya wrote results for each round of the last year. He has *n* records in total.
In order to determine a player's category consider the best result obtained by the player and the best results of other players. The player belongs to category:
- "noob" β if more than 50% of players have better results; - "random" β if his result is not worse than the result that 50% of players have, but more than 20% of players have better results; - "average" β if his result is not worse than the result that 80% of players have, but more than 10% of players have better results; - "hardcore" β if his result is not worse than the result that 90% of players have, but more than 1% of players have better results; - "pro" β if his result is not worse than the result that 99% of players have.
When the percentage is calculated the player himself is taken into account. That means that if two players played the game and the first one gained 100 points and the second one 1000 points, then the first player's result is not worse than the result that 50% of players have, and the second one is not worse than the result that 100% of players have.
Vasya gave you the last year Plane of Tanks results. Help Vasya determine each player's category. | The first line contains the only integer number *n* (1<=β€<=*n*<=β€<=1000) β a number of records with the players' results.
Each of the next *n* lines contains a player's name and the amount of points, obtained by the player for the round, separated with a space. The name contains not less than 1 and no more than 10 characters. The name consists of lowercase Latin letters only. It is guaranteed that any two different players have different names. The amount of points, obtained by the player for the round, is a non-negative integer number and does not exceed 1000. | Print on the first line the number *m* β the number of players, who participated in one round at least.
Each one of the next *m* lines should contain a player name and a category he belongs to, separated with space. Category can be one of the following: "noob", "random", "average", "hardcore" or "pro" (without quotes). The name of each player should be printed only once. Player names with respective categories can be printed in an arbitrary order. | [
"5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250\n",
"3\nvasya 200\nkolya 1000\nvasya 1000\n"
] | [
"4\nartem noob\nigor pro\nkolya random\nvasya random\n",
"2\nkolya pro\nvasya pro\n"
] | In the first example the best result, obtained by artem is not worse than the result that 25% of players have (his own result), so he belongs to category "noob". vasya and kolya have best results not worse than the results that 75% players have (both of them and artem), so they belong to category "random". igor has best result not worse than the result that 100% of players have (all other players and himself), so he belongs to category "pro".
In the second example both players have the same amount of points, so they have results not worse than 100% players have, so they belong to category "pro". | [
{
"input": "5\nvasya 100\nvasya 200\nartem 100\nkolya 200\nigor 250",
"output": "4\nartem noob\nigor pro\nkolya random\nvasya random"
},
{
"input": "3\nvasya 200\nkolya 1000\nvasya 1000",
"output": "2\nkolya pro\nvasya pro"
},
{
"input": "1\nvasya 1000",
"output": "1\nvasya pro"
},... | 434 | 204,800 | 3 | 28,663 | |
856 | To Play or not to Play | [
"greedy"
] | null | null | Vasya and Petya are playing an online game. As most online games, it has hero progress system that allows players to gain experience that make their heroes stronger. Of course, Vasya would like to get as many experience points as possible. After careful study of experience points allocation, he found out that if he plays the game alone, he gets one experience point each second. However, if two players are playing together, and their current experience values differ by at most *C* points, they can boost their progress, and each of them gets 2 experience points each second.
Since Vasya and Petya are middle school students, their parents don't allow them to play all the day around. Each of the friends has his own schedule: Vasya can only play during intervals [*a*1;*b*1],<=[*a*2;*b*2],<=...,<=[*a**n*;*b**n*], and Petya can only play during intervals [*c*1;*d*1],<=[*c*2;*d*2],<=...,<=[*c**m*;*d**m*]. All time periods are given in seconds from the current moment. Vasya is good in math, so he has noticed that sometimes it can be profitable not to play alone, because experience difference could become too big, and progress would not be boosted even when played together.
Now they would like to create such schedule of playing that Vasya's final experience was greatest possible. The current players experience is the same. Petya is not so concerned about his experience, so he is ready to cooperate and play when needed to maximize Vasya's experience. | The first line of input data contains integers *n*, *m* and *C* β the number of intervals when Vasya can play, the number of intervals when Petya can play, and the maximal difference in experience level when playing together still gives a progress boost (1<=β€<=*n*,<=*m*<=β€<=2Β·105, 0<=β€<=*C*<=β€<=1018).
The following *n* lines contain two integers each: *a**i*,<=*b**i*Β β intervals when Vasya can play (0<=β€<=*a**i*<=<<=*b**i*<=β€<=1018, *b**i*<=<<=*a**i*<=+<=1).
The following *m* lines contain two integers each: *c**i*,<=*d**i*Β β intervals when Petya can play (0<=β€<=*c**i*<=<<=*d**i*<=β€<=1018, *d**i*<=<<=*c**i*<=+<=1). | Output one integerΒ β the maximal experience that Vasya can have in the end, if both players try to maximize this value. | [
"2 1 5\n1 7\n10 20\n10 20\n",
"1 2 5\n0 100\n20 60\n85 90\n"
] | [
"25\n",
"125\n"
] | none | [] | 62 | 204,800 | 0 | 28,674 | |
518 | Pasha and Pipe | [
"binary search",
"brute force",
"combinatorics",
"dp",
"implementation"
] | null | null | On a certain meeting of a ruling party "A" minister Pavel suggested to improve the sewer system and to create a new pipe in the city.
The city is an *n*<=Γ<=*m* rectangular squared field. Each square of the field is either empty (then the pipe can go in it), or occupied (the pipe cannot go in such square). Empty squares are denoted by character '.', occupied squares are denoted by character '#'.
The pipe must meet the following criteria:
- the pipe is a polyline of width 1, - the pipe goes in empty squares, - the pipe starts from the edge of the field, but not from a corner square, - the pipe ends at the edge of the field but not in a corner square, - the pipe has at most 2 turns (90 degrees), - the border squares of the field must share exactly two squares with the pipe, - if the pipe looks like a single segment, then the end points of the pipe must lie on distinct edges of the field, - for each non-border square of the pipe there are exacly two side-adjacent squares that also belong to the pipe, - for each border square of the pipe there is exactly one side-adjacent cell that also belongs to the pipe.
Here are some samples of allowed piping routes:
Here are some samples of forbidden piping routes:
In these samples the pipes are represented by characters '<=*<='.
You were asked to write a program that calculates the number of distinct ways to make exactly one pipe in the city.
The two ways to make a pipe are considered distinct if they are distinct in at least one square. | The first line of the input contains two integers *n*,<=*m* (2<=β€<=*n*,<=*m*<=β€<=2000)Β βΒ the height and width of Berland map.
Each of the next *n* lines contains *m* characters β the map of the city.
If the square of the map is marked by character '.', then the square is empty and the pipe can through it.
If the square of the map is marked by character '#', then the square is full and the pipe can't through it. | In the first line of the output print a single integer β the number of distinct ways to create a pipe. | [
"3 3\n...\n..#\n...\n",
"4 2\n..\n..\n..\n..\n",
"4 5\n#...#\n#...#\n###.#\n###.#\n"
] | [
"3",
"2\n",
"4"
] | In the first sample there are 3 ways to make a pipe (the squares of the pipe are marked by characters 'β*β'): | [] | 46 | 0 | 0 | 28,767 | |
873 | Strange Game On Matrix | [
"greedy",
"two pointers"
] | null | null | Ivan is playing a strange game.
He has a matrix *a* with *n* rows and *m* columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows:
1. Initially Ivan's score is 0; 1. In each column, Ivan will find the topmost 1 (that is, if the current column is *j*, then he will find minimum *i* such that *a**i*,<=*j*<==<=1). If there are no 1's in the column, this column is skipped; 1. Ivan will look at the next *min*(*k*,<=*n*<=-<=*i*<=+<=1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score.
Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score. | The first line contains three integer numbers *n*, *m* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=100, 1<=β€<=*m*<=β€<=100).
Then *n* lines follow, *i*-th of them contains *m* integer numbers β the elements of *i*-th row of matrix *a*. Each number is either 0 or 1. | Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score. | [
"4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1\n",
"3 2 1\n1 0\n0 1\n0 0\n"
] | [
"4 1\n",
"2 0\n"
] | In the first example Ivan will replace the element *a*<sub class="lower-index">1,β2</sub>. | [
{
"input": "4 3 2\n0 1 0\n1 0 1\n0 1 0\n1 1 1",
"output": "4 1"
},
{
"input": "3 2 1\n1 0\n0 1\n0 0",
"output": "2 0"
},
{
"input": "3 4 2\n0 1 1 1\n1 0 1 1\n1 0 0 1",
"output": "7 0"
},
{
"input": "3 57 3\n1 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1 1 1... | 62 | 0 | 0 | 28,771 | |
303 | Random Ranking | [
"dp",
"math",
"probabilities"
] | null | null | Imagine a real contest or exam of *n* participants. Every participant will get a particular score. We can predict the standings board more or less, if we do some statistics on their previous performance.
Let's say the score of the participants will be uniformly distributed in interval [*l**i*,<=*r**i*] (the score can be a real number). Can you predict the standings board according to these data? In other words you should say for each participant the probability that he gets some fixed place in the scoreboard. The participants are sorted by increasing of their scores in the scoreboard. So, the participant with the largest score gets the last place. | The first line contains integer *n* (1<=<=β€<=*n*<=<=β€<=80), showing how many participants we have. Each of the next *n* lines contains our predictions, the *i*-th line contains a pair of integers *l**i*,<=*r**i* (0<=β€<=*l**i*<=<<=*r**i*<=β€<=109) as the distributed interval for participant *i*.
Consider the participants numbered from 1 to *n* in some way. | Output a distributed matrix *a* of order *n*. The element *a**ij* of the matrix is the probability that participant *i* has rank *j*.
Your answer will considered correct if it has at most 10<=-<=6 absolute or relative error. | [
"2\n1 6\n4 9\n",
"8\n0 2\n1 3\n2 4\n3 5\n4 6\n5 7\n6 8\n7 9\n"
] | [
"0.9200000000 0.080 \n0.080 0.9200000000 \n",
"0.875 0.125 0 0 0 0 0 0 \n0.125 0.750 0.125 0 0 0 0 0 \n0 0.125 0.750 0.125 0 0 0 0 \n0 0 0.125 0.750 0.125 0 0 0 \n0 0 0 0.125 0.750 0.125 0 0 \n0 0 0 0 0.125 0.750 0.125 0 \n0 0 0 0 0 0.125 0.750 0.125 \n0 0 0 0 0 0 0.125 0.875 \n"
] | The score probability distribution is continuous, which means, there is no possibility for a draw. | [] | 92 | 0 | 0 | 28,793 | |
0 | none | [
"none"
] | null | null | There are *n* walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the *n*-th walrus stands at the beginning of the queue. The *i*-th walrus has the age equal to *a**i*.
The *i*-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such *j* (*i*<=<<=*j*), that *a**i*<=><=*a**j*. The displeasure of the *i*-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the *i*-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of *n* walruses in the queue his displeasure. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105) β the number of walruses in the queue. The second line contains integers *a**i* (1<=β€<=*a**i*<=β€<=109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one. | Print *n* numbers: if the *i*-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the *i*-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus. | [
"6\n10 8 5 3 50 45\n",
"7\n10 4 6 3 2 8 15\n",
"5\n10 3 1 10 11\n"
] | [
"2 1 0 -1 0 -1 ",
"4 2 1 0 -1 -1 -1 ",
"1 0 -1 -1 -1 "
] | none | [
{
"input": "6\n10 8 5 3 50 45",
"output": "2 1 0 -1 0 -1 "
},
{
"input": "7\n10 4 6 3 2 8 15",
"output": "4 2 1 0 -1 -1 -1 "
},
{
"input": "5\n10 3 1 10 11",
"output": "1 0 -1 -1 -1 "
},
{
"input": "13\n18 9 8 9 23 20 18 18 33 25 31 37 36",
"output": "2 0 -1 -1 2 1 -1 -1 ... | 248 | 0 | 0 | 28,876 | |
28 | DravDe saves the world | [
"geometry",
"math"
] | E. DravDe saves the world | 1 | 256 | How horrible! The empire of galactic chickens tries to conquer a beautiful city "Z", they have built a huge incubator that produces millions of chicken soldiers a day, and fenced it around. The huge incubator looks like a polygon on the plane *Oxy* with *n* vertices. Naturally, DravDe can't keep still, he wants to destroy the chicken empire. For sure, he will start with the incubator.
DravDe is strictly outside the incubator's territory in point *A*(*x**a*,<=*y**a*), and wants to get inside and kill all the chickens working there. But it takes a lot of doing! The problem is that recently DravDe went roller skating and has broken both his legs. He will get to the incubator's territory in his jet airplane LEVAP-41.
LEVAP-41 flies at speed *V*(*x**v*,<=*y**v*,<=*z**v*). DravDe can get on the plane in point *A*, fly for some time, and then air drop himself. DravDe is very heavy, that's why he falls vertically at speed *F**down*, but in each point of his free fall DravDe can open his parachute, and from that moment he starts to fall at the wind speed *U*(*x**u*,<=*y**u*,<=*z**u*) until he lands. Unfortunately, DravDe isn't good at mathematics. Would you help poor world's saviour find such an air dropping plan, that allows him to land on the incubator's territory? If the answer is not unique, DravDe wants to find the plan with the minimum time of his flight on the plane. If the answers are still multiple, he wants to find the one with the minimum time of his free fall before opening his parachute | The first line contains the number *n* (3<=β€<=*n*<=β€<=104) β the amount of vertices of the fence. Then there follow *n* lines containing the coordinates of these vertices (two integer numbers *x**i*,<=*y**i*) in clockwise or counter-clockwise order. It's guaranteed, that the fence does not contain self-intersections.
The following four lines contain coordinates of point *A*(*x**a*,<=*y**a*), speeds *V*(*x**v*,<=*y**v*,<=*z**v*), *F**down* and speed *U*(*x**u*,<=*y**u*,<=*z**u*). All the input numbers are integer. All the coordinates don't exceed 104 in absolute value. It's guaranteed, that *z**v*<=><=0 and *F**down*,<=*z**u*<=<<=0, and point *A* is strictly outside the incubator's territory. | In the first line output two numbers *t*1,<=*t*2 such, that if DravDe air drops at time *t*1 (counting from the beginning of the flight), he lands on the incubator's territory (landing on the border is regarder as landing on the territory). If DravDe doesn't open his parachute, the second number should be equal to the duration of DravDe's falling down. If it's impossible for DravDe to get to the incubator's territory, output -1 -1. If the answer is not unique, output the answer with the minimum *t*1. If the answers are still multiple, output the answer with the minimum *t*2. Your answer must have an absolute or relative error less than 10<=-<=6. | [
"4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 0 1\n-1\n0 1 -1\n",
"4\n0 0\n0 1\n1 1\n1 0\n0 -1\n-1 -1 1\n-1\n0 1 -1\n",
"4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 1 1\n-1\n1 1 -1\n"
] | [
"1.00000000 0.00000000\n",
"-1.00000000 -1.00000000\n",
"0.50000000 0.00000000\n"
] | none | [
{
"input": "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 0 1\n-1\n0 1 -1",
"output": "1.00000000 0.00000000"
},
{
"input": "4\n0 0\n0 1\n1 1\n1 0\n0 -1\n-1 -1 1\n-1\n0 1 -1",
"output": "-1.00000000 -1.00000000"
},
{
"input": "4\n0 0\n1 0\n1 1\n0 1\n0 -1\n1 1 1\n-1\n1 1 -1",
"output": "0.50000000 0... | 30 | 0 | 0 | 28,899 |
38 | Smart Boy | [
"dp",
"games",
"strings"
] | F. Smart Boy | 4 | 256 | Once Petya and Vasya invented a new game and called it "Smart Boy". They located a certain set of words β the dictionary β for the game. It is admissible for the dictionary to contain similar words.
The rules of the game are as follows: first the first player chooses any letter (a word as long as 1) from any word from the dictionary and writes it down on a piece of paper. The second player adds some other letter to this one's initial or final position, thus making a word as long as 2, then it's the first player's turn again, he adds a letter in the beginning or in the end thus making a word as long as 3 and so on. But the player mustn't break one condition: the newly created word must be a substring of a word from a dictionary. The player who can't add a letter to the current word without breaking the condition loses.
Also if by the end of a turn a certain string *s* is written on paper, then the player, whose turn it just has been, gets a number of points according to the formula:
where
- is a sequence number of symbol *c* in Latin alphabet, numbered starting from 1. For example, , and . - is the number of words from the dictionary where the line *s* occurs as a substring at least once.
Your task is to learn who will win the game and what the final score will be. Every player plays optimally and most of all tries to win, then β to maximize the number of his points, then β to minimize the number of the points of the opponent. | The first input line contains an integer *n* which is the number of words in the located dictionary (1<=β€<=*n*<=β€<=30). The *n* lines contain the words from the dictionary β one word is written on one line. Those lines are nonempty, consisting of Latin lower-case characters no longer than 30 characters. Equal words can be in the list of words. | On the first output line print a line "First" or "Second" which means who will win the game. On the second line output the number of points of the first player and the number of points of the second player after the game ends. Separate the numbers by a single space. | [
"2\naba\nabac\n",
"3\nartem\nnik\nmax\n"
] | [
"Second\n29 35\n",
"First\n2403 1882\n"
] | none | [
{
"input": "2\naba\nabac",
"output": "Second\n29 35"
},
{
"input": "3\nartem\nnik\nmax",
"output": "First\n2403 1882"
},
{
"input": "1\njyi",
"output": "First\n1727 876"
},
{
"input": "2\naz\nkagim",
"output": "First\n1082 678"
},
{
"input": "3\nskz\nsauy\nrxu",
... | 248 | 8,806,400 | 0 | 28,901 |
498 | Traffic Jams in the Land | [
"data structures",
"dp",
"number theory"
] | null | null | Some country consists of (*n*<=+<=1) cities, located along a straight highway. Let's number the cities with consecutive integers from 1 to *n*<=+<=1 in the order they occur along the highway. Thus, the cities are connected by *n* segments of the highway, the *i*-th segment connects cities number *i* and *i*<=+<=1. Every segment of the highway is associated with a positive integer *a**i*<=><=1 β the period of traffic jams appearance on it.
In order to get from city *x* to city *y* (*x*<=<<=*y*), some drivers use the following tactics.
Initially the driver is in city *x* and the current time *t* equals zero. Until the driver arrives in city *y*, he perfors the following actions:
- if the current time *t* is a multiple of *a**x*, then the segment of the highway number *x* is now having traffic problems and the driver stays in the current city for one unit of time (formally speaking, we assign *t*<==<=*t*<=+<=1); - if the current time *t* is not a multiple of *a**x*, then the segment of the highway number *x* is now clear and that's why the driver uses one unit of time to move to city *x*<=+<=1 (formally, we assign *t*<==<=*t*<=+<=1 and *x*<==<=*x*<=+<=1).
You are developing a new traffic control system. You want to consecutively process *q* queries of two types:
1. determine the final value of time *t* after the ride from city *x* to city *y* (*x*<=<<=*y*) assuming that we apply the tactics that is described above. Note that for each query *t* is being reset to 0. 1. replace the period of traffic jams appearing on the segment number *x* by value *y* (formally, assign *a**x*<==<=*y*).
Write a code that will effectively process the queries given above. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the number of highway segments that connect the *n*<=+<=1 cities.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (2<=β€<=*a**i*<=β€<=6) β the periods of traffic jams appearance on segments of the highway.
The next line contains a single integer *q* (1<=β€<=*q*<=β€<=105) β the number of queries to process.
The next *q* lines contain the descriptions of the queries in the format *c*, *x*, *y* (*c* β the query type).
If *c* is character 'A', then your task is to process a query of the first type. In this case the following constraints are satisfied: 1<=β€<=*x*<=<<=*y*<=β€<=*n*<=+<=1.
If *c* is character 'C', then you need to process a query of the second type. In such case, the following constraints are satisfied: 1<=β€<=*x*<=β€<=*n*, 2<=β€<=*y*<=β€<=6. | For each query of the first type output a single integer β the final value of time *t* after driving from city *x* to city *y*. Process the queries in the order in which they are given in the input. | [
"10\n2 5 3 2 3 5 3 4 2 4\n10\nC 10 6\nA 2 6\nA 1 3\nC 3 4\nA 3 11\nA 4 9\nA 5 6\nC 7 3\nA 8 10\nA 2 5\n"
] | [
"5\n3\n14\n6\n2\n4\n4\n"
] | none | [] | 30 | 0 | 0 | 28,909 | |
765 | Math, math everywhere | [
"brute force",
"dp",
"math",
"meet-in-the-middle",
"number theory"
] | null | null | If you have gone that far, you'll probably skip unnecessary legends anyway...
You are given a binary string and an integer . Find the number of integers *k*, 0<=β€<=*k*<=<<=*N*, such that for all *i*<==<=0, 1, ..., *m*<=-<=1 | In the first line of input there is a string *s* consisting of 0's and 1's (1<=β€<=|*s*|<=β€<=40).
In the next line of input there is an integer *n* (1<=β€<=*n*<=β€<=5Β·105).
Each of the next *n* lines contains two space-separated integers *p**i*, Ξ±*i* (1<=β€<=*p**i*,<=Ξ±*i*<=β€<=109, *p**i* is prime). All *p**i* are distinct. | A single integerΒ β the answer to the problem. | [
"1\n2\n2 1\n3 1\n",
"01\n2\n3 2\n5 1\n",
"1011\n1\n3 1000000000\n"
] | [
"2\n",
"15\n",
"411979884\n"
] | none | [] | 124 | 0 | 0 | 28,984 | |
571 | Geometric Progressions | [
"math"
] | null | null | Geometric progression with the first element *a* and common ratio *b* is a sequence of numbers *a*,<=*ab*,<=*ab*2,<=*ab*3,<=....
You are given *n* integer geometric progressions. Your task is to find the smallest integer *x*, that is the element of all the given progressions, or else state that such integer does not exist. | The first line contains integer (1<=β€<=*n*<=β€<=100) β the number of geometric progressions.
Next *n* lines contain pairs of integers *a*,<=*b* (1<=β€<=*a*,<=*b*<=β€<=109), that are the first element and the common ratio of the corresponding geometric progression. | If the intersection of all progressions is empty, then print <=-<=1, otherwise print the remainder of the minimal positive integer number belonging to all progressions modulo 1000000007 (109<=+<=7). | [
"2\n2 2\n4 1\n",
"2\n2 2\n3 3\n"
] | [
"4\n",
"-1\n"
] | In the second sample test one of the progressions contains only powers of two, the other one contains only powers of three. | [
{
"input": "2\n2 2\n4 1",
"output": "4"
},
{
"input": "2\n2 2\n3 3",
"output": "-1"
},
{
"input": "3\n1 4\n2 5\n4 2",
"output": "-1"
},
{
"input": "2\n1 4\n2 6",
"output": "-1"
},
{
"input": "2\n1 6\n2 6",
"output": "-1"
},
{
"input": "2\n1 6\n1 12",
... | 62 | 307,200 | 0 | 28,999 | |
566 | Clique in the Divisibility Graph | [
"dp",
"math",
"number theory"
] | null | null | As you must know, the maximum clique problem in an arbitrary graph is *NP*-hard. Nevertheless, for some graphs of specific kinds it can be solved effectively.
Just in case, let us remind you that a clique in a non-directed graph is a subset of the vertices of a graph, such that any two vertices of this subset are connected by an edge. In particular, an empty set of vertexes and a set consisting of a single vertex, are cliques.
Let's define a divisibility graph for a set of positive integers *A*<==<={*a*1,<=*a*2,<=...,<=*a**n*} as follows. The vertices of the given graph are numbers from set *A*, and two numbers *a**i* and *a**j* (*i*<=β <=*j*) are connected by an edge if and only if either *a**i* is divisible by *a**j*, or *a**j* is divisible by *a**i*.
You are given a set of non-negative integers *A*. Determine the size of a maximum clique in a divisibility graph for set *A*. | The first line contains integer *n* (1<=β€<=*n*<=β€<=106), that sets the size of set *A*.
The second line contains *n* distinct positive integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=106) β elements of subset *A*. The numbers in the line follow in the ascending order. | Print a single number β the maximum size of a clique in a divisibility graph for set *A*. | [
"8\n3 4 6 8 10 18 21 24\n"
] | [
"3\n"
] | In the first sample test a clique of size 3 is, for example, a subset of vertexes {3,β6,β18}. A clique of a larger size doesn't exist in this graph. | [
{
"input": "8\n3 4 6 8 10 18 21 24",
"output": "3"
},
{
"input": "5\n2 3 4 8 16",
"output": "4"
},
{
"input": "2\n10 20",
"output": "2"
},
{
"input": "2\n10 21",
"output": "1"
},
{
"input": "5\n250000 333333 500000 666666 1000000",
"output": "3"
},
{
"... | 46 | 0 | 0 | 29,008 | |
593 | Beautiful Function | [
"constructive algorithms",
"math"
] | null | null | Every day Ruslan tried to count sheep to fall asleep, but this didn't help. Now he has found a more interesting thing to do. First, he thinks of some set of circles on a plane, and then tries to choose a beautiful set of points, such that there is at least one point from the set inside or on the border of each of the imagined circles.
Yesterday Ruslan tried to solve this problem for the case when the set of points is considered beautiful if it is given as (*x**t*<==<=*f*(*t*),<=*y**t*<==<=*g*(*t*)), where argument *t* takes all integer values from 0 to 50. Moreover, *f*(*t*) and *g*(*t*) should be correct functions.
Assume that *w*(*t*) and *h*(*t*) are some correct functions, and *c* is an integer ranging from 0 to 50. The function *s*(*t*) is correct if it's obtained by one of the following rules:
1. *s*(*t*)<==<=*abs*(*w*(*t*)), where *abs*(*x*) means taking the absolute value of a number *x*, i.e. |*x*|;1. *s*(*t*)<==<=(*w*(*t*)<=+<=*h*(*t*));1. *s*(*t*)<==<=(*w*(*t*)<=-<=*h*(*t*));1. *s*(*t*)<==<=(*w*(*t*)<=*<=*h*(*t*)), where <=*<= means multiplication, i.e. (*w*(*t*)Β·*h*(*t*));1. *s*(*t*)<==<=*c*;1. *s*(*t*)<==<=*t*;
Yesterday Ruslan thought on and on, but he could not cope with the task. Now he asks you to write a program that computes the appropriate *f*(*t*) and *g*(*t*) for any set of at most 50 circles.
In each of the functions *f*(*t*) and *g*(*t*) you are allowed to use no more than 50 multiplications. The length of any function should not exceed 100Β·*n* characters. The function should not contain spaces.
Ruslan can't keep big numbers in his memory, so you should choose *f*(*t*) and *g*(*t*), such that for all integer *t* from 0 to 50 value of *f*(*t*) and *g*(*t*) and all the intermediate calculations won't exceed 109 by their absolute value. | The first line of the input contains number *n* (1<=β€<=*n*<=β€<=50)Β β the number of circles Ruslan thinks of. Next follow *n* lines, each of them containing three integers *x**i*, *y**i* and *r**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=50, 2<=β€<=*r**i*<=β€<=50)Β β the coordinates of the center and the raduis of the *i*-th circle. | In the first line print a correct function *f*(*t*). In the second line print a correct function *g*(*t*). The set of the points (*x**t*<==<=*f*(*t*),<=*y**t*<==<=*g*(*t*)) (0<=β€<=*t*<=β€<=50) must satisfy the condition, that there is at least one point inside or on the border of each of the circles, Ruslan thinks of at the beginning. | [
"3\n0 10 4\n10 0 4\n20 10 4\n"
] | [
"t \nabs((t-10))"
] | Correct functions:
1. 101. (1+2)1. ((*t*-3)+(*t**4))1. *abs*((*t*-10))1. (*abs*((((23-*t*)*(*t***t*))+((45+12)*(*t***t*))))*((5**t*)+((12**t*)-13)))1. *abs*((*t*-(*abs*((*t**31))+14))))
Incorrect functions:
1. 3+5+7 (not enough brackets, it should be ((3+5)+7) or (3+(5+7))) 1. *abs*(*t*-3) (not enough brackets, it should be *abs*((*t*-3))1. 2+(2-3 (one bracket too many)1. 1(*t*+5) (no arithmetic operation between 1 and the bracket)1. 5000*5000 (the number exceeds the maximum) | [
{
"input": "3\n0 10 4\n10 0 4\n20 10 4",
"output": "(((0*((1-abs((t-0)))+abs((abs((t-0))-1))))+(5*((1-abs((t-1)))+abs((abs((t-1))-1)))))+(10*((1-abs((t-2)))+abs((abs((t-2))-1)))))\n(((5*((1-abs((t-0)))+abs((abs((t-0))-1))))+(0*((1-abs((t-1)))+abs((abs((t-1))-1)))))+(5*((1-abs((t-2)))+abs((abs((t-2))-1)))))"... | 156 | 20,172,800 | 0 | 29,025 | |
398 | Painting The Wall | [
"dp",
"probabilities"
] | null | null | User ainta decided to paint a wall. The wall consists of *n*2 tiles, that are arranged in an *n*<=Γ<=*n* table. Some tiles are painted, and the others are not. As he wants to paint it beautifully, he will follow the rules below.
1. Firstly user ainta looks at the wall. If there is at least one painted cell on each row and at least one painted cell on each column, he stops coloring. Otherwise, he goes to step 2. 1. User ainta choose any tile on the wall with uniform probability. 1. If the tile he has chosen is not painted, he paints the tile. Otherwise, he ignores it. 1. Then he takes a rest for one minute even if he doesn't paint the tile. And then ainta goes to step 1.
However ainta is worried if it would take too much time to finish this work. So he wants to calculate the expected time needed to paint the wall by the method above. Help him find the expected time. You can assume that choosing and painting any tile consumes no time at all. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=2Β·103; 0<=β€<=*m*<=β€<=*min*(*n*2,<=2Β·104)) β the size of the wall and the number of painted cells.
Next *m* lines goes, each contains two integers *r**i* and *c**i* (1<=β€<=*r**i*,<=*c**i*<=β€<=*n*) β the position of the painted cell. It is guaranteed that the positions are all distinct. Consider the rows of the table are numbered from 1 to *n*. Consider the columns of the table are numbered from 1 to *n*. | In a single line print the expected time to paint the wall in minutes. Your answer will be considered correct if it has at most 10<=-<=4 absolute or relative error. | [
"5 2\n2 3\n4 1\n",
"2 2\n1 1\n1 2\n",
"1 1\n1 1\n"
] | [
"11.7669491886\n",
"2.0000000000\n",
"0.0000000000\n"
] | none | [] | 623 | 512,000 | 0 | 29,063 | |
0 | none | [
"none"
] | null | null | BigData Inc. is a corporation that has *n* data centers indexed from 1 to *n* that are located all over the world. These data centers provide storage for client data (you can figure out that client data is really big!).
Main feature of services offered by BigData Inc. is the access availability guarantee even under the circumstances of any data center having an outage. Such a guarantee is ensured by using the two-way replication. Two-way replication is such an approach for data storage that any piece of data is represented by two identical copies that are stored in two different data centers.
For each of *m* company clients, let us denote indices of two different data centers storing this client data as *c**i*,<=1 and *c**i*,<=2.
In order to keep data centers operational and safe, the software running on data center computers is being updated regularly. Release cycle of BigData Inc. is one day meaning that the new version of software is being deployed to the data center computers each day.
Data center software update is a non-trivial long process, that is why there is a special hour-long time frame that is dedicated for data center maintenance. During the maintenance period, data center computers are installing software updates, and thus they may be unavailable. Consider the day to be exactly *h* hours long. For each data center there is an integer *u**j* (0<=β€<=*u**j*<=β€<=*h*<=-<=1) defining the index of an hour of day, such that during this hour data center *j* is unavailable due to maintenance.
Summing up everything above, the condition *u**c**i*,<=1<=β <=*u**c**i*,<=2 should hold for each client, or otherwise his data may be unaccessible while data centers that store it are under maintenance.
Due to occasional timezone change in different cities all over the world, the maintenance time in some of the data centers may change by one hour sometimes. Company should be prepared for such situation, that is why they decided to conduct an experiment, choosing some non-empty subset of data centers, and shifting the maintenance time for them by an hour later (i.e. if *u**j*<==<=*h*<=-<=1, then the new maintenance hour would become 0, otherwise it would become *u**j*<=+<=1). Nonetheless, such an experiment should not break the accessibility guarantees, meaning that data of any client should be still available during any hour of a day after the data center maintenance times are changed.
Such an experiment would provide useful insights, but changing update time is quite an expensive procedure, that is why the company asked you to find out the minimum number of data centers that have to be included in an experiment in order to keep the data accessibility guarantees. | The first line of input contains three integers *n*, *m* and *h* (2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=100<=000, 2<=β€<=*h*<=β€<=100<=000), the number of company data centers, number of clients and the day length of day measured in hours.
The second line of input contains *n* integers *u*1,<=*u*2,<=...,<=*u**n* (0<=β€<=*u**j*<=<<=*h*), *j*-th of these numbers is an index of a maintenance hour for data center *j*.
Each of the next *m* lines contains two integers *c**i*,<=1 and *c**i*,<=2 (1<=β€<=*c**i*,<=1,<=*c**i*,<=2<=β€<=*n*, *c**i*,<=1<=β <=*c**i*,<=2), defining the data center indices containing the data of client *i*.
It is guaranteed that the given maintenance schedule allows each client to access at least one copy of his data at any moment of day. | In the first line print the minimum possible number of data centers *k* (1<=β€<=*k*<=β€<=*n*) that have to be included in an experiment in order to keep the data available for any client.
In the second line print *k* distinct integers *x*1,<=*x*2,<=...,<=*x**k* (1<=β€<=*x**i*<=β€<=*n*), the indices of data centers whose maintenance time will be shifted by one hour later. Data center indices may be printed in any order.
If there are several possible answers, it is allowed to print any of them. It is guaranteed that at there is at least one valid choice of data centers. | [
"3 3 5\n4 4 0\n1 3\n3 2\n3 1\n",
"4 5 4\n2 1 0 3\n4 3\n3 2\n1 2\n1 4\n1 3\n"
] | [
"1\n3 ",
"4\n1 2 3 4 "
] | Consider the first sample test. The given answer is the only way to conduct an experiment involving the only data center. In such a scenario the third data center has a maintenance during the hour 1, and no two data centers storing the information of the same client have maintenance at the same hour.
On the other hand, for example, if we shift the maintenance time on hour later for the first data center, then the data of clients 1 and 3 will be unavailable during the hour 0. | [
{
"input": "3 3 5\n4 4 0\n1 3\n3 2\n3 1",
"output": "1\n3 "
},
{
"input": "4 5 4\n2 1 0 3\n4 3\n3 2\n1 2\n1 4\n1 3",
"output": "4\n1 2 3 4 "
},
{
"input": "5 5 4\n0 1 2 3 3\n1 2\n2 3\n3 4\n4 1\n3 5",
"output": "1\n5 "
},
{
"input": "2 1 2\n1 0\n1 2",
"output": "2\n1 2 "
... | 155 | 512,000 | 0 | 29,083 | |
417 | Cunning Gena | [
"bitmasks",
"dp",
"greedy",
"sortings"
] | null | null | A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his *n* friends that they will solve the problems for him.
The participants are offered *m* problems on the contest. For each friend, Gena knows what problems he can solve. But Gena's friends won't agree to help Gena for nothing: the *i*-th friend asks Gena *x**i* rubles for his help in solving all the problems he can. Also, the friend agreed to write a code for Gena only if Gena's computer is connected to at least *k**i* monitors, each monitor costs *b* rubles.
Gena is careful with money, so he wants to spend as little money as possible to solve all the problems. Help Gena, tell him how to spend the smallest possible amount of money. Initially, there's no monitors connected to Gena's computer. | The first line contains three integers *n*, *m* and *b* (1<=β€<=*n*<=β€<=100; 1<=β€<=*m*<=β€<=20; 1<=β€<=*b*<=β€<=109)Β β the number of Gena's friends, the number of problems and the cost of a single monitor.
The following 2*n* lines describe the friends. Lines number 2*i* and (2*i*<=+<=1) contain the information about the *i*-th friend. The 2*i*-th line contains three integers *x**i*, *k**i* and *m**i* (1<=β€<=*x**i*<=β€<=109; 1<=β€<=*k**i*<=β€<=109; 1<=β€<=*m**i*<=β€<=*m*)Β β the desired amount of money, monitors and the number of problems the friend can solve. The (2*i*<=+<=1)-th line contains *m**i* distinct positive integersΒ β the numbers of problems that the *i*-th friend can solve. The problems are numbered from 1 to *m*. | Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved. | [
"2 2 1\n100 1 1\n2\n100 2 1\n1\n",
"3 2 5\n100 1 1\n1\n100 1 1\n2\n200 1 2\n1 2\n",
"1 2 1\n1 1 1\n1\n"
] | [
"202\n",
"205\n",
"-1\n"
] | none | [
{
"input": "2 2 1\n100 1 1\n2\n100 2 1\n1",
"output": "202"
},
{
"input": "3 2 5\n100 1 1\n1\n100 1 1\n2\n200 1 2\n1 2",
"output": "205"
},
{
"input": "1 2 1\n1 1 1\n1",
"output": "-1"
},
{
"input": "4 2 1\n62 91 1\n1\n2 18 1\n1\n33 76 1\n1\n23 58 1\n1",
"output": "-1"
... | 1,000 | 9,216,000 | 0 | 29,098 | |
689 | Mike and Geometry Problem | [
"combinatorics",
"data structures",
"dp",
"geometry",
"implementation"
] | null | null | Mike wants to prepare for IMO but he doesn't know geometry, so his teacher gave him an interesting geometry problem. Let's define *f*([*l*,<=*r*])<==<=*r*<=-<=*l*<=+<=1 to be the number of integer points in the segment [*l*,<=*r*] with *l*<=β€<=*r* (say that ). You are given two integers *n* and *k* and *n* closed intervals [*l**i*,<=*r**i*] on *OX* axis and you have to find:
In other words, you should find the sum of the number of integer points in the intersection of any *k* of the segments.
As the answer may be very large, output it modulo 1000000007 (109<=+<=7).
Mike can't solve this problem so he needs your help. You will help him, won't you? | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=200<=000)Β β the number of segments and the number of segments in intersection groups respectively.
Then *n* lines follow, the *i*-th line contains two integers *l**i*,<=*r**i* (<=-<=109<=β€<=*l**i*<=β€<=*r**i*<=β€<=109), describing *i*-th segment bounds. | Print one integer numberΒ β the answer to Mike's problem modulo 1000000007 (109<=+<=7) in the only line. | [
"3 2\n1 2\n1 3\n2 3\n",
"3 3\n1 3\n1 3\n1 3\n",
"3 1\n1 2\n2 3\n3 4\n"
] | [
"5\n",
"3\n",
"6\n"
] | In the first example:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/099f833590960ffc5dafcbc207172a93605c44a8.png" style="max-width: 100.0%;max-height: 100.0%;"/>;
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6f56e6e99db45efff1c9be404aeb569c7e2bbb1d.png" style="max-width: 100.0%;max-height: 100.0%;"/>;
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/eaff785f85562a9ff263ebbbfafe78a6b2581a57.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
So the answer is 2β+β1β+β2β=β5. | [
{
"input": "3 2\n1 2\n1 3\n2 3",
"output": "5"
},
{
"input": "3 3\n1 3\n1 3\n1 3",
"output": "3"
},
{
"input": "3 1\n1 2\n2 3\n3 4",
"output": "6"
},
{
"input": "1 1\n45 70",
"output": "26"
},
{
"input": "1 1\n-35 -8",
"output": "28"
},
{
"input": "1 1... | 1,513 | 48,128,000 | 3 | 29,125 | |
359 | Permutation | [
"constructive algorithms",
"dp",
"math"
] | null | null | A permutation *p* is an ordered group of numbers *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*, consisting of *n* distinct positive integers, each is no more than *n*. We'll define number *n* as the length of permutation *p*1,<=<=<=*p*2,<=<=<=...,<=<=<=*p**n*.
Simon has a positive integer *n* and a non-negative integer *k*, such that 2*k*<=β€<=*n*. Help him find permutation *a* of length 2*n*, such that it meets this equation: . | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=50000, 0<=β€<=2*k*<=β€<=*n*). | Print 2*n* integers *a*1,<=*a*2,<=...,<=*a*2*n* β the required permutation *a*. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them. | [
"1 0\n",
"2 1\n",
"4 0\n"
] | [
"1 2",
"3 2 1 4\n",
"2 7 4 6 1 3 5 8\n"
] | Record |*x*| represents the absolute value of number *x*.
In the first sample |1β-β2|β-β|1β-β2|β=β0.
In the second sample |3β-β2|β+β|1β-β4|β-β|3β-β2β+β1β-β4|β=β1β+β3β-β2β=β2.
In the third sample |2β-β7|β+β|4β-β6|β+β|1β-β3|β+β|5β-β8|β-β|2β-β7β+β4β-β6β+β1β-β3β+β5β-β8|β=β12β-β12β=β0. | [
{
"input": "1 0",
"output": "1 2"
},
{
"input": "2 1",
"output": "3 2 1 4"
},
{
"input": "4 0",
"output": "2 7 4 6 1 3 5 8"
},
{
"input": "50000 0",
"output": "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 4... | 109 | 5,836,800 | 3 | 29,200 | |
0 | none | [
"none"
] | null | null | Given an *n*<=Γ<=*n* table *T* consisting of lowercase English letters. We'll consider some string *s* good if the table contains a correct path corresponding to the given string. In other words, good strings are all strings we can obtain by moving from the left upper cell of the table only to the right and down. Here's the formal definition of correct paths:
Consider rows of the table are numbered from 1 to *n* from top to bottom, and columns of the table are numbered from 1 to *n* from left to the right. Cell (*r*,<=*c*) is a cell of table *T* on the *r*-th row and in the *c*-th column. This cell corresponds to letter *T**r*,<=*c*.
A path of length *k* is a sequence of table cells [(*r*1,<=*c*1),<=(*r*2,<=*c*2),<=...,<=(*r**k*,<=*c**k*)]. The following paths are correct:
1. There is only one correct path of length 1, that is, consisting of a single cell: [(1,<=1)]; 1. Let's assume that [(*r*1,<=*c*1),<=...,<=(*r**m*,<=*c**m*)] is a correct path of length *m*, then paths [(*r*1,<=*c*1),<=...,<=(*r**m*,<=*c**m*),<=(*r**m*<=+<=1,<=*c**m*)] and [(*r*1,<=*c*1),<=...,<=(*r**m*,<=*c**m*),<=(*r**m*,<=*c**m*<=+<=1)] are correct paths of length *m*<=+<=1.
We should assume that a path [(*r*1,<=*c*1),<=(*r*2,<=*c*2),<=...,<=(*r**k*,<=*c**k*)] corresponds to a string of length *k*: *T**r*1,<=*c*1<=+<=*T**r*2,<=*c*2<=+<=...<=+<=*T**r**k*,<=*c**k*.
Two players play the following game: initially they have an empty string. Then the players take turns to add a letter to the end of the string. After each move (adding a new letter) the resulting string must be good. The game ends after 2*n*<=-<=1 turns. A player wins by the following scenario:
1. If the resulting string has strictly more letters "a" than letters "b", then the first player wins; 1. If the resulting string has strictly more letters "b" than letters "a", then the second player wins; 1. If the resulting string has the same number of letters "a" and "b", then the players end the game with a draw.
Your task is to determine the result of the game provided that both players played optimally well. | The first line contains a single number *n* (1<=β€<=*n*<=β€<=20).
Next *n* lines contain *n* lowercase English letters each β table *T*. | In a single line print string "FIRST", if the first player wins, "SECOND", if the second player wins and "DRAW", if the game ends with a draw. | [
"2\nab\ncd\n",
"2\nxa\nay\n",
"3\naab\nbcb\nbac\n"
] | [
"DRAW\n",
"FIRST\n",
"DRAW\n"
] | Consider the first sample:
Good strings are strings: a, ab, ac, abd, acd.
The first player moves first and adds letter a to the string, as there is only one good string of length 1. Then the second player can add b or c and the game will end with strings abd or acd, correspondingly. In the first case it will be a draw (the string has one a and one b), in the second case the first player wins. Naturally, in this case the second player prefers to choose letter b and end the game with a draw.
Consider the second sample:
Good strings are: x, xa, xay.
We can see that the game will end with string xay and the first player wins. | [] | 46 | 307,200 | 0 | 29,306 | |
715 | Digit Tree | [
"dfs and similar",
"divide and conquer",
"dsu",
"trees"
] | null | null | ZS the Coder has a large tree. It can be represented as an undirected connected graph of *n* vertices numbered from 0 to *n*<=-<=1 and *n*<=-<=1 edges between them. There is a single nonzero digit written on each edge.
One day, ZS the Coder was bored and decided to investigate some properties of the tree. He chose a positive integer *M*, which is coprime to 10, i.e. .
ZS consider an ordered pair of distinct vertices (*u*,<=*v*) interesting when if he would follow the shortest path from vertex *u* to vertex *v* and write down all the digits he encounters on his path in the same order, he will get a decimal representaion of an integer divisible by *M*.
Formally, ZS consider an ordered pair of distinct vertices (*u*,<=*v*) interesting if the following states true:
- Let *a*1<==<=*u*,<=*a*2,<=...,<=*a**k*<==<=*v* be the sequence of vertices on the shortest path from *u* to *v* in the order of encountering them; - Let *d**i* (1<=β€<=*i*<=<<=*k*) be the digit written on the edge between vertices *a**i* and *a**i*<=+<=1; - The integer is divisible by *M*.
Help ZS the Coder find the number of interesting pairs! | The first line of the input contains two integers, *n* and *M* (2<=β€<=*n*<=β€<=100<=000,<=1<=β€<=*M*<=β€<=109, )Β β the number of vertices and the number ZS has chosen respectively.
The next *n*<=-<=1 lines contain three integers each. *i*-th of them contains *u**i*,<=*v**i* and *w**i*, denoting an edge between vertices *u**i* and *v**i* with digit *w**i* written on it (0<=β€<=*u**i*,<=*v**i*<=<<=*n*,<=<=1<=β€<=*w**i*<=β€<=9). | Print a single integerΒ β the number of interesting (by ZS the Coder's consideration) pairs. | [
"6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7\n",
"5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3\n"
] | [
"7\n",
"8\n"
] | In the first sample case, the interesting pairs are (0,β4),β(1,β2),β(1,β5),β(3,β2),β(2,β5),β(5,β2),β(3,β5). The numbers that are formed by these pairs are 14,β21,β217,β91,β7,β7,β917 respectively, which are all multiples of 7. Note that (2,β5) and (5,β2) are considered different.
In the second sample case, the interesting pairs are (4,β0),β(0,β4),β(3,β2),β(2,β3),β(0,β1),β(1,β0),β(4,β1),β(1,β4), and 6 of these pairs give the number 33 while 2 of them give the number 3333, which are all multiples of 11. | [
{
"input": "6 7\n0 1 2\n4 2 4\n2 0 1\n3 0 9\n2 5 7",
"output": "7"
},
{
"input": "5 11\n1 2 3\n2 0 3\n3 0 3\n4 3 3",
"output": "8"
},
{
"input": "4 3\n0 1 4\n1 2 4\n2 3 4",
"output": "2"
},
{
"input": "2 7\n1 0 9",
"output": "0"
},
{
"input": "2 7\n1 0 7",
"ou... | 46 | 0 | 0 | 29,434 | |
12 | Ball | [
"data structures",
"sortings"
] | D. Ball | 2 | 256 | *N* ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of the *i*-th lady by *B**i*, her intellect by *I**i* and her richness by *R**i*. Then *i*-th lady is a probable self-murderer if there is some *j*-th lady that *B**i*<=<<=*B**j*,<=*I**i*<=<<=*I**j*,<=*R**i*<=<<=*R**j*. Find the number of probable self-murderers. | The first line contains one integer *N* (1<=β€<=*N*<=β€<=500000). The second line contains *N* integer numbers *B**i*, separated by single spaces. The third and the fourth lines contain sequences *I**i* and *R**i* in the same format. It is guaranteed that 0<=β€<=*B**i*,<=*I**i*,<=*R**i*<=β€<=109. | Output the answer to the problem. | [
"3\n1 4 2\n4 3 2\n2 5 3\n"
] | [
"1\n"
] | none | [
{
"input": "3\n1 4 2\n4 3 2\n2 5 3",
"output": "1"
},
{
"input": "5\n2 8 10 0 7\n7 7 3 0 10\n2 8 3 2 2",
"output": "1"
},
{
"input": "5\n3 0 0 2 0\n7 10 7 4 0\n9 1 6 1 9",
"output": "1"
},
{
"input": "5\n5 4 0 2 5\n8 3 1 0 10\n4 5 0 0 5",
"output": "2"
},
{
"input... | 31 | 0 | 0 | 29,501 |
788 | Weird journey | [
"combinatorics",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs"
] | null | null | Little boy Igor wants to become a traveller. At first, he decided to visit all the cities of his motherlandΒ β Uzhlyandia.
It is widely known that Uzhlyandia has *n* cities connected with *m* bidirectional roads. Also, there are no two roads in the country that connect the same pair of cities, but roads starting and ending in the same city can exist. Igor wants to plan his journey beforehand. Boy thinks a path is good if the path goes over *m*<=-<=2 roads twice, and over the other 2 exactly once. The good path can start and finish in any city of Uzhlyandia.
Now he wants to know how many different good paths are in Uzhlyandia. Two paths are considered different if the sets of roads the paths goes over exactly once differ. Help IgorΒ β calculate the number of good paths. | The first line contains two integers *n*, *m* (1<=β€<=*n*,<=*m*<=β€<=106)Β β the number of cities and roads in Uzhlyandia, respectively.
Each of the next *m* lines contains two integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*) that mean that there is road between cities *u* and *v*.
It is guaranteed that no road will be given in the input twice. That also means that for every city there is no more than one road that connects the city to itself. | Print out the only integerΒ β the number of good paths in Uzhlyandia. | [
"5 4\n1 2\n1 3\n1 4\n1 5\n",
"5 3\n1 2\n2 3\n4 5\n",
"2 2\n1 1\n1 2\n"
] | [
"6",
"0",
"1"
] | In first sample test case the good paths are:
- 2βββ1βββ3βββ1βββ4βββ1βββ5, - 2βββ1βββ3βββ1βββ5βββ1βββ4, - 2βββ1βββ4βββ1βββ5βββ1βββ3, - 3βββ1βββ2βββ1βββ4βββ1βββ5, - 3βββ1βββ2βββ1βββ5βββ1βββ4, - 4βββ1βββ2βββ1βββ3βββ1βββ5.
There are good paths that are same with displayed above, because the sets of roads they pass over once are same:
- 2βββ1βββ4βββ1βββ3βββ1βββ5, - 2βββ1βββ5βββ1βββ3βββ1βββ4, - 2βββ1βββ5βββ1βββ4βββ1βββ3, - 3βββ1βββ4βββ1βββ2βββ1βββ5, - 3βββ1βββ5βββ1βββ2βββ1βββ4, - 4βββ1βββ3βββ1βββ2βββ1βββ5, - and all the paths in the other direction.
Thus, the answer is 6.
In the second test case, Igor simply can not walk by all the roads.
In the third case, Igor walks once over every road. | [
{
"input": "5 4\n1 2\n1 3\n1 4\n1 5",
"output": "6"
},
{
"input": "5 3\n1 2\n2 3\n4 5",
"output": "0"
},
{
"input": "2 2\n1 1\n1 2",
"output": "1"
},
{
"input": "4 5\n1 4\n3 4\n1 2\n2 3\n2 4",
"output": "8"
},
{
"input": "7 13\n6 7\n2 7\n3 7\n4 3\n5 2\n1 4\n7 7\n3... | 31 | 4,608,000 | 0 | 29,580 | |
939 | Love Rescue | [
"dfs and similar",
"dsu",
"graphs",
"greedy",
"strings"
] | null | null | Valya and Tolya are an ideal pair, but they quarrel sometimes. Recently, Valya took offense at her boyfriend because he came to her in t-shirt with lettering that differs from lettering on her pullover. Now she doesn't want to see him and Tolya is seating at his room and crying at her photos all day long.
This story could be very sad but fairy godmother (Tolya's grandmother) decided to help them and restore their relationship. She secretly took Tolya's t-shirt and Valya's pullover and wants to make the letterings on them same. In order to do this, for one unit of mana she can buy a spell that can change some letters on the clothes. Your task is calculate the minimum amount of mana that Tolya's grandmother should spend to rescue love of Tolya and Valya.
More formally, letterings on Tolya's t-shirt and Valya's pullover are two strings with same length *n* consisting only of lowercase English letters. Using one unit of mana, grandmother can buy a spell of form (*c*1,<=*c*2) (where *c*1 and *c*2 are some lowercase English letters), which can arbitrary number of times transform a single letter *c*1 to *c*2 and vise-versa on both Tolya's t-shirt and Valya's pullover. You should find the minimum amount of mana that grandmother should spend to buy a set of spells that can make the letterings equal. In addition you should output the required set of spells. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the length of the letterings.
The second line contains a string with length *n*, consisting of lowercase English lettersΒ β the lettering on Valya's pullover.
The third line contains the lettering on Tolya's t-shirt in the same format. | In the first line output a single integerΒ β the minimum amount of mana *t* required for rescuing love of Valya and Tolya.
In the next *t* lines output pairs of space-separated lowercase English lettersΒ β spells that Tolya's grandmother should buy. Spells and letters in spells can be printed in any order.
If there are many optimal answers, output any. | [
"3\nabb\ndad\n",
"8\ndrpepper\ncocacola\n"
] | [
"2\na d\nb a",
"7\nl e\ne d\nd c\nc p\np o\no r\nr a\n"
] | In first example it's enough to buy two spells: ('a','d') and ('b','a'). Then first letters will coincide when we will replace letter 'a' with 'd'. Second letters will coincide when we will replace 'b' with 'a'. Third letters will coincide when we will at first replace 'b' with 'a' and then 'a' with 'd'. | [
{
"input": "3\nabb\ndad",
"output": "2\nb d\nd a"
},
{
"input": "8\ndrpepper\ncocacola",
"output": "7\nl e\ne d\nd c\nc p\np o\no r\nr a"
},
{
"input": "1\nh\np",
"output": "1\np h"
},
{
"input": "2\nxc\nda",
"output": "2\nc a\nx d"
},
{
"input": "3\nbab\naab",
... | 124 | 29,081,600 | 3 | 29,598 | |
962 | Byteland, Berland and Disputed Cities | [
"constructive algorithms",
"greedy"
] | null | null | The cities of Byteland and Berland are located on the axis $Ox$. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $Ox$ there are three types of cities:
- the cities of Byteland, - the cities of Berland, - disputed cities.
Recently, the project BNET has been launched β a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.
The countries agreed to connect the pairs of cities with BNET cables in such a way that:
- If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables, - If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables.
Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.
The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.
Each city is a point on the line $Ox$. It is technically possible to connect the cities $a$ and $b$ with a cable so that the city $c$ ($a < c < b$) is not connected to this cable, where $a$, $b$ and $c$ are simultaneously coordinates of the cities $a$, $b$ and $c$. | The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^{5}$) β the number of cities.
The following $n$ lines contains an integer $x_i$ and the letter $c_i$ ($-10^{9} \le x_i \le 10^{9}$) β the coordinate of the city and its type. If the city belongs to Byteland, $c_i$ equals to 'B'. If the city belongs to Berland, $c_i$ equals to Β«RΒ». If the city is disputed, $c_i$ equals to 'P'.
All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates. | Print the minimal total length of such set of cables, that if we delete all Berland cities ($c_i$='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities ($c_i$='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. | [
"4\n-5 R\n0 P\n3 P\n7 B\n",
"5\n10 R\n14 B\n16 B\n21 R\n32 R\n"
] | [
"12\n",
"24\n"
] | In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be $5 + 3 + 4 = 12$.
In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates $10, 21, 32$, so to connect them you need two cables of length $11$ and $11$. The cities of Byteland have coordinates $14$ and $16$, so to connect them you need one cable of length $2$. Thus, the total length of all cables is $11 + 11 + 2 = 24$. | [
{
"input": "4\n-5 R\n0 P\n3 P\n7 B",
"output": "12"
},
{
"input": "5\n10 R\n14 B\n16 B\n21 R\n32 R",
"output": "24"
},
{
"input": "10\n66 R\n67 R\n72 R\n73 R\n76 R\n78 B\n79 B\n83 B\n84 B\n85 P",
"output": "26"
},
{
"input": "10\n61 R\n64 R\n68 R\n71 R\n72 R\n73 R\n74 P\n86 P... | 108 | 0 | 0 | 29,669 | |
114 | PFAST Inc. | [
"bitmasks",
"brute force",
"graphs"
] | null | null | When little Petya grew up and entered the university, he started to take part in ΠΠ‘Π contests. Later he realized that he doesn't like how the ΠΠ‘Π contests are organised: the team could only have three members (and he couldn't take all his friends to the competitions and distribute the tasks between the team members efficiently), so he decided to organize his own contests PFAST Inc. β Petr and Friends Are Solving Tasks Corporation. PFAST Inc. rules allow a team to have unlimited number of members.
To make this format of contests popular he organised his own tournament. To create the team he will prepare for the contest organised by the PFAST Inc. rules, he chose several volunteers (up to 16 people) and decided to compile a team from them. Petya understands perfectly that if a team has two people that don't get on well, then the team will perform poorly. Put together a team with as many players as possible given that all players should get on well with each other. | The first line contains two integer numbers *n* (1<=β€<=*n*<=β€<=16) β the number of volunteers, and *m* () β the number of pairs that do not get on. Next *n* lines contain the volunteers' names (each name is a non-empty string consisting of no more than 10 uppercase and/or lowercase Latin letters). Next *m* lines contain two names β the names of the volunteers who do not get on. The names in pair are separated with a single space. Each pair of volunteers who do not get on occurs exactly once. The strings are case-sensitive. All *n* names are distinct. | The first output line should contain the single number *k* β the number of people in the sought team. Next *k* lines should contain the names of the sought team's participants in the lexicographical order. If there are several variants to solve the problem, print any of them. Petya might not be a member of the sought team. | [
"3 1\nPetya\nVasya\nMasha\nPetya Vasya\n",
"3 0\nPasha\nLesha\nVanya\n"
] | [
"2\nMasha\nPetya\n",
"3\nLesha\nPasha\nVanya\n"
] | none | [
{
"input": "3 1\nPetya\nVasya\nMasha\nPetya Vasya",
"output": "2\nMasha\nPetya"
},
{
"input": "3 0\nPasha\nLesha\nVanya",
"output": "3\nLesha\nPasha\nVanya"
},
{
"input": "7 12\nPasha\nLesha\nVanya\nTaras\nNikita\nSergey\nAndrey\nPasha Taras\nPasha Nikita\nPasha Andrey\nPasha Sergey\nLes... | 92 | 4,812,800 | 0 | 29,691 | |
0 | none | [
"none"
] | null | null | You are given an integer *m*, and a list of *n* distinct integers between 0 and *m*<=-<=1.
You would like to construct a sequence satisfying the properties:
- Each element is an integer between 0 and *m*<=-<=1, inclusive. - All prefix products of the sequence modulo *m* are distinct. - No prefix product modulo *m* appears as an element of the input list. - The length of the sequence is maximized.
Construct any sequence satisfying the properties above. | The first line of input contains two integers *n* and *m* (0<=β€<=*n*<=<<=*m*<=β€<=200<=000)Β β the number of forbidden prefix products and the modulus.
If *n* is non-zero, the next line of input contains *n* distinct integers between 0 and *m*<=-<=1, the forbidden prefix products. If *n* is zero, this line doesn't exist. | On the first line, print the number *k*, denoting the length of your sequence.
On the second line, print *k* space separated integers, denoting your sequence. | [
"0 5\n",
"3 10\n2 9 1\n"
] | [
"5\n1 2 4 3 0\n",
"6\n3 9 2 9 8 0\n"
] | For the first case, the prefix products of this sequence modulo *m* are [1,β2,β3,β4,β0].
For the second case, the prefix products of this sequence modulo *m* are [3,β7,β4,β6,β8,β0]. | [
{
"input": "0 5",
"output": "5\n1 2 4 3 0"
},
{
"input": "3 10\n2 9 1",
"output": "6\n3 9 2 9 8 0"
},
{
"input": "0 1",
"output": "1\n0"
},
{
"input": "0 720",
"output": "397\n1 7 413 263 389 467 77 283 299 187 293 563 269 47 677 463 599 367 173 143 149 347 557 643 179 54... | 31 | 0 | 0 | 29,709 | |
44 | Cola | [
"implementation"
] | B. Cola | 2 | 256 | To celebrate the opening of the Winter Computer School the organizers decided to buy in *n* liters of cola. However, an unexpected difficulty occurred in the shop: it turned out that cola is sold in bottles 0.5, 1 and 2 liters in volume. At that, there are exactly *a* bottles 0.5 in volume, *b* one-liter bottles and *c* of two-liter ones. The organizers have enough money to buy any amount of cola. What did cause the heated arguments was how many bottles of every kind to buy, as this question is pivotal for the distribution of cola among the participants (and organizers as well).
Thus, while the organizers are having the argument, discussing different variants of buying cola, the Winter School can't start. Your task is to count the number of all the possible ways to buy exactly *n* liters of cola and persuade the organizers that this number is too large, and if they keep on arguing, then the Winter Computer School will have to be organized in summer.
All the bottles of cola are considered indistinguishable, i.e. two variants of buying are different from each other only if they differ in the number of bottles of at least one kind. | The first line contains four integers β *n*, *a*, *b*, *c* (1<=β€<=*n*<=β€<=10000, 0<=β€<=*a*,<=*b*,<=*c*<=β€<=5000). | Print the unique number β the solution to the problem. If it is impossible to buy exactly *n* liters of cola, print 0. | [
"10 5 5 5\n",
"3 0 0 2\n"
] | [
"9\n",
"0\n"
] | none | [
{
"input": "10 5 5 5",
"output": "9"
},
{
"input": "3 0 0 2",
"output": "0"
},
{
"input": "1 0 0 0",
"output": "0"
},
{
"input": "1 1 0 0",
"output": "0"
},
{
"input": "1 2 0 0",
"output": "1"
},
{
"input": "1 0 1 0",
"output": "1"
},
{
"in... | 2,000 | 22,118,400 | 0 | 29,724 |
0 | none | [
"none"
] | null | null | Welcome to another task about breaking the code lock! Explorers Whitfield and Martin came across an unusual safe, inside of which, according to rumors, there are untold riches, among which one can find the solution of the problem of discrete logarithm!
Of course, there is a code lock is installed on the safe. The lock has a screen that displays a string of *n* lowercase Latin letters. Initially, the screen displays string *s*. Whitfield and Martin found out that the safe will open when string *t* will be displayed on the screen.
The string on the screen can be changed using the operation Β«shift *x*Β». In order to apply this operation, explorers choose an integer *x* from 0 to *n* inclusive. After that, the current string *p*<==<=Ξ±Ξ² changes to Ξ²*R*Ξ±, where the length of Ξ² is *x*, and the length of Ξ± is *n*<=-<=*x*. In other words, the suffix of the length *x* of string *p* is reversed and moved to the beginning of the string. For example, after the operation Β«shift 4Β» the string Β«abcacbΒ» will be changed with string Β«bcacab Β», since Ξ±<==<=ab, Ξ²<==<=cacb, Ξ²*R*<==<=bcac.
Explorers are afraid that if they apply too many operations Β«shiftΒ», the lock will be locked forever. They ask you to find a way to get the string *t* on the screen, using no more than 6100 operations. | The first line contains an integer *n*, the length of the strings *s* and *t* (1<=β€<=*n*<=β€<=2<=000).
After that, there are two strings *s* and *t*, consisting of *n* lowercase Latin letters each. | If it is impossible to get string *t* from string *s* using no more than 6100 operations Β«shiftΒ», print a single number <=-<=1.
Otherwise, in the first line output the number of operations *k* (0<=β€<=*k*<=β€<=6100). In the next line output *k* numbers *x**i* corresponding to the operations Β«shift *x**i*Β» (0<=β€<=*x**i*<=β€<=*n*) in the order in which they should be applied. | [
"6\nabacbb\nbabcba\n",
"3\naba\nbba\n"
] | [
"4\n6 3 2 3\n",
"-1\n"
] | none | [
{
"input": "6\nabacbb\nbabcba",
"output": "13\n2 6 1 4 0 3 6 3 1 1 1 5 6 "
},
{
"input": "3\naba\nbba",
"output": "-1"
},
{
"input": "1\nw\nw",
"output": "2\n0 1 "
},
{
"input": "2\nvb\nvb",
"output": "2\n1 1 "
},
{
"input": "7\nvhypflg\nvprhfly",
"output": "-... | 93 | 6,246,400 | 3 | 29,787 | |
590 | Birthday | [
"graph matchings",
"strings"
] | null | null | Today is birthday of a Little DashaΒ β she is now 8 years old! On this occasion, each of her *n* friends and relatives gave her a ribbon with a greeting written on it, and, as it turned out, all the greetings are different. Dasha gathered all the ribbons and decided to throw away some of them in order to make the remaining set stylish. The birthday girl considers a set of ribbons stylish if no greeting written on some ribbon is a substring of another greeting written on some other ribbon. Let us recall that the substring of the string *s* is a continuous segment of *s*.
Help Dasha to keep as many ribbons as possible, so that she could brag about them to all of her friends. Dasha cannot rotate or flip ribbons, that is, each greeting can be read in a single way given in the input. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=750)Β β the number of Dasha's relatives and friends.
Each of the next *n* lines contains exactly one greeting. Each greeting consists of characters 'a' and 'b' only.
The total length of all greetings won't exceed 10<=000<=000 characters. | In the first line print the maximum size of the stylish set. In the second line print the numbers of ribbons involved in it, assuming that they are numbered from 1 to *n* in the order they appear in the input. If there are several stylish sets of the maximum size, print any of them. | [
"5\nabab\naba\naabab\nababb\nbab\n"
] | [
"2\n2 5\n"
] | In the sample, the answer that keeps ribbons 3 and 4 is also considered correct. | [] | 46 | 0 | 0 | 29,788 | |
39 | Moon Craters | [
"dp",
"sortings"
] | C. Moon Craters | 1 | 256 | There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes.
An extraterrestrial intelligence research specialist professor Okulov (the namesake of the Okulov, the author of famous textbooks on programming) put forward an alternate hypothesis. Guess what kind of a hypothesis it was ββ sure, the one including extraterrestrial mind involvement. Now the professor is looking for proofs of his hypothesis.
Professor has data from the moon robot that moves linearly in one direction along the Moon surface. The moon craters are circular in form with integer-valued radii. The moon robot records only the craters whose centers lay on his path and sends to the Earth the information on the distance from the centers of the craters to the initial point of its path and on the radii of the craters.
According to the theory of professor Okulov two craters made by an extraterrestrial intelligence for the aims yet unknown either are fully enclosed one in the other or do not intersect at all. Internal or external tangency is acceptable. However the experimental data from the moon robot do not confirm this theory! Nevertheless, professor Okulov is hopeful. He perfectly understands that to create any logical theory one has to ignore some data that are wrong due to faulty measuring (or skillful disguise by the extraterrestrial intelligence that will be sooner or later found by professor Okulov!) Thatβs why Okulov wants to choose among the available crater descriptions the largest set that would satisfy his theory. | The first line has an integer *n* (1<=β€<=*n*<=β€<=2000) β the number of discovered craters. The next *n* lines contain crater descriptions in the "*c**i* *r**i*" format, where *c**i* is the coordinate of the center of the crater on the moon robotβs path, *r**i* is the radius of the crater. All the numbers *c**i* and *r**i* are positive integers not exceeding 109. No two craters coincide. | In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to *n* in the order in which they were given in the input data. The numbers may be output in any order. If the result is not unique, output any. | [
"4\n1 1\n2 2\n4 1\n5 1\n"
] | [
"3\n1 2 4\n"
] | none | [] | 62 | 0 | 0 | 29,801 |
279 | Beautiful Decomposition | [
"dp",
"games",
"greedy",
"number theory"
] | null | null | Valera considers a number beautiful, if it equals 2*k* or -2*k* for some integer *k* (*k*<=β₯<=0). Recently, the math teacher asked Valera to represent number *n* as the sum of beautiful numbers. As Valera is really greedy, he wants to complete the task using as few beautiful numbers as possible.
Help Valera and find, how many numbers he is going to need. In other words, if you look at all decompositions of the number *n* into beautiful summands, you need to find the size of the decomposition which has the fewest summands. | The first line contains string *s* (1<=β€<=|*s*|<=β€<=106), that is the binary representation of number *n* without leading zeroes (*n*<=><=0). | Print a single integer β the minimum amount of beautiful numbers that give a total of *n*. | [
"10\n",
"111\n",
"1101101\n"
] | [
"1\n",
"2\n",
"4\n"
] | In the first sample *n*β=β2 is a beautiful number.
In the second sample *n*β=β7 and Valera can decompose it into sum 2<sup class="upper-index">3</sup>β+β(β-β2<sup class="upper-index">0</sup>).
In the third sample *n*β=β109 can be decomposed into the sum of four summands as follows: 2<sup class="upper-index">7</sup>β+β(β-β2<sup class="upper-index">4</sup>)β+β(β-β2<sup class="upper-index">2</sup>)β+β2<sup class="upper-index">0</sup>. | [] | 30 | 0 | 0 | 29,840 | |
496 | Tennis Game | [
"binary search"
] | null | null | Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores *t* points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of *s* sets, he wins the match and the match is over. Here *s* and *t* are some positive integer numbers.
To spice it up, Petya and Gena choose new numbers *s* and *t* before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores *t* points and the match is over as soon as one of the players wins *s* sets.
Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers *s* and *t* for the given match are also lost. The players now wonder what values of *s* and *t* might be. Can you determine all the possible options? | The first line contains a single integer *n*Β β the length of the sequence of games (1<=β€<=*n*<=β€<=105).
The second line contains *n* space-separated integers *a**i*. If *a**i*<==<=1, then the *i*-th serve was won by Petya, if *a**i*<==<=2, then the *i*-th serve was won by Gena.
It is not guaranteed that at least one option for numbers *s* and *t* corresponds to the given record. | In the first line print a single number *k*Β β the number of options for numbers *s* and *t*.
In each of the following *k* lines print two integers *s**i* and *t**i*Β β the option for numbers *s* and *t*. Print the options in the order of increasing *s**i*, and for equal *s**i*Β β in the order of increasing *t**i*. | [
"5\n1 2 1 2 1\n",
"4\n1 1 1 1\n",
"4\n1 2 1 2\n",
"8\n2 1 2 1 1 1 1 1\n"
] | [
"2\n1 3\n3 1\n",
"3\n1 4\n2 2\n4 1\n",
"0\n",
"3\n1 6\n2 3\n6 1\n"
] | none | [
{
"input": "5\n1 2 1 2 1",
"output": "2\n1 3\n3 1"
},
{
"input": "4\n1 1 1 1",
"output": "3\n1 4\n2 2\n4 1"
},
{
"input": "4\n1 2 1 2",
"output": "0"
},
{
"input": "8\n2 1 2 1 1 1 1 1",
"output": "3\n1 6\n2 3\n6 1"
},
{
"input": "14\n2 1 2 1 1 1 1 2 1 1 2 1 2 1",
... | 77 | 6,963,200 | 0 | 29,893 | |
571 | Lengthening Sticks | [
"combinatorics",
"implementation",
"math"
] | null | null | You are given three sticks with positive integer lengths of *a*,<=*b*, and *c* centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most *l* centimeters. In particular, it is allowed not to increase the length of any stick.
Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them. | The single line contains 4 integers *a*,<=*b*,<=*c*,<=*l* (1<=β€<=*a*,<=*b*,<=*c*<=β€<=3Β·105, 0<=β€<=*l*<=β€<=3Β·105). | Print a single integer β the number of ways to increase the sizes of the sticks by the total of at most *l* centimeters, so that you can make a non-degenerate triangle from it. | [
"1 1 1 2\n",
"1 2 3 1\n",
"10 2 1 7\n"
] | [
"4\n",
"2\n",
"0\n"
] | In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter.
In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions. | [
{
"input": "1 1 1 2",
"output": "4"
},
{
"input": "1 2 3 1",
"output": "2"
},
{
"input": "10 2 1 7",
"output": "0"
},
{
"input": "1 2 1 5",
"output": "20"
},
{
"input": "10 15 17 10",
"output": "281"
},
{
"input": "5 5 5 10000",
"output": "41841675... | 701 | 4,608,000 | 3 | 29,975 | |
702 | Road to Post Office | [
"math"
] | null | null | Vasiliy has a car and he wants to get from home to the post office. The distance which he needs to pass equals to *d* kilometers.
Vasiliy's car is not new β it breaks after driven every *k* kilometers and Vasiliy needs *t* seconds to repair it. After repairing his car Vasiliy can drive again (but after *k* kilometers it will break again, and so on). In the beginning of the trip the car is just from repair station.
To drive one kilometer on car Vasiliy spends *a* seconds, to walk one kilometer on foot he needs *b* seconds (*a*<=<<=*b*).
Your task is to find minimal time after which Vasiliy will be able to reach the post office. Consider that in every moment of time Vasiliy can left his car and start to go on foot. | The first line contains 5 positive integers *d*,<=*k*,<=*a*,<=*b*,<=*t* (1<=β€<=*d*<=β€<=1012; 1<=β€<=*k*,<=*a*,<=*b*,<=*t*<=β€<=106; *a*<=<<=*b*), where:
- *d* β the distance from home to the post office; - *k* β the distance, which car is able to drive before breaking; - *a* β the time, which Vasiliy spends to drive 1 kilometer on his car; - *b* β the time, which Vasiliy spends to walk 1 kilometer on foot; - *t* β the time, which Vasiliy spends to repair his car. | Print the minimal time after which Vasiliy will be able to reach the post office. | [
"5 2 1 4 10\n",
"5 2 1 4 5\n"
] | [
"14\n",
"13\n"
] | In the first example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds) and then to walk on foot 3 kilometers (in 12 seconds). So the answer equals to 14 seconds.
In the second example Vasiliy needs to drive the first 2 kilometers on the car (in 2 seconds), then repair his car (in 5 seconds) and drive 2 kilometers more on the car (in 2 seconds). After that he needs to walk on foot 1 kilometer (in 4 seconds). So the answer equals to 13 seconds. | [
{
"input": "5 2 1 4 10",
"output": "14"
},
{
"input": "5 2 1 4 5",
"output": "13"
},
{
"input": "1 1 1 2 1",
"output": "1"
},
{
"input": "1000000000000 1000000 999999 1000000 1000000",
"output": "999999999999000000"
},
{
"input": "997167959139 199252 232602 952690... | 77 | 0 | 3 | 30,038 | |
985 | Pencils and Boxes | [
"binary search",
"data structures",
"dp",
"greedy",
"two pointers"
] | null | null | Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence *a*1,<=*a*2,<=...,<=*a**n* of *n* integer numbers β saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that:
- Each pencil belongs to exactly one box; - Each non-empty box has at least *k* pencils in it; - If pencils *i* and *j* belong to the same box, then |*a**i*<=-<=*a**j*|<=β€<=*d*, where |*x*| means absolute value of *x*. Note that the opposite is optional, there can be pencils *i* and *j* such that |*a**i*<=-<=*a**j*|<=β€<=*d* and they belong to different boxes.
Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO". | The first line contains three integer numbers *n*, *k* and *d* (1<=β€<=*k*<=β€<=*n*<=β€<=5Β·105, 0<=β€<=*d*<=β€<=109) β the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively.
The second line contains *n* integer numbers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β saturation of color of each pencil. | Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO". | [
"6 3 10\n7 2 7 7 4 2\n",
"6 2 3\n4 5 3 13 4 10\n",
"3 2 5\n10 16 22\n"
] | [
"YES\n",
"YES\n",
"NO\n"
] | In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10.
In the second example you can split pencils of saturations [4,β5,β3,β4] into 2 boxes of size 2 and put the remaining ones into another box. | [
{
"input": "6 3 10\n7 2 7 7 4 2",
"output": "YES"
},
{
"input": "6 2 3\n4 5 3 13 4 10",
"output": "YES"
},
{
"input": "3 2 5\n10 16 22",
"output": "NO"
},
{
"input": "8 7 13\n52 85 14 52 92 33 80 85",
"output": "NO"
},
{
"input": "6 4 0\n1 3 2 4 2 1",
"output"... | 1,107 | 48,640,000 | -1 | 30,042 | |
585 | Digits of Number Pi | [
"dp",
"implementation",
"strings"
] | null | null | Vasily has recently learned about the amazing properties of number Ο. In one of the articles it has been hypothesized that, whatever the sequence of numbers we have, in some position, this sequence is found among the digits of number Ο. Thus, if you take, for example, the epic novel "War and Peace" of famous Russian author Leo Tolstoy, and encode it with numbers, then we will find the novel among the characters of number Ο.
Vasily was absolutely delighted with this, because it means that all the books, songs and programs have already been written and encoded in the digits of Ο. Vasily is, of course, a bit wary that this is only a hypothesis and it hasn't been proved, so he decided to check it out.
To do this, Vasily downloaded from the Internet the archive with the sequence of digits of number Ο, starting with a certain position, and began to check the different strings of digits on the presence in the downloaded archive. Vasily quickly found short strings of digits, but each time he took a longer string, it turned out that it is not in the archive. Vasily came up with a definition that a string of length *d* is a half-occurrence if it contains a substring of length of at least , which occurs in the archive.
To complete the investigation, Vasily took 2 large numbers *x*,<=*y* (*x*<=β€<=*y*) with the same number of digits and now he wants to find the number of numbers in the interval from *x* to *y*, which are half-occurrences in the archive. Help Vasily calculate this value modulo 109<=+<=7. | The first line contains string *s* consisting of decimal digits (1<=β€<=|*s*|<=β€<=1000) that Vasily will use to search substrings in. According to hypothesis, this sequence of digis indeed occurs in the decimal representation of Ο, although we can't guarantee that.
The second and third lines contain two positive integers *x*,<=*y* of the same length *d* (*x*<=β€<=*y*, 2<=β€<=*d*<=β€<=50). Numbers *x*,<=*y* do not contain leading zeroes. | Print how many numbers in the segment from *x* to *y* that are half-occurrences in *s* modulo 109<=+<=7. | [
"02\n10\n19\n",
"023456789\n10\n19\n",
"31415926535\n10\n29\n"
] | [
"2\n",
"9\n",
"20\n"
] | none | [] | 62 | 8,806,400 | -1 | 30,131 | |
727 | T-shirts Distribution | [
"constructive algorithms",
"flows",
"greedy"
] | null | null | The organizers of a programming contest have decided to present t-shirts to participants. There are six different t-shirts sizes in this problem: S, M, L, XL, XXL, XXXL (sizes are listed in increasing order). The t-shirts are already prepared. For each size from S to XXXL you are given the number of t-shirts of this size.
During the registration, the organizers asked each of the *n* participants about the t-shirt size he wants. If a participant hesitated between two sizes, he could specify two neighboring sizesΒ β this means that any of these two sizes suits him.
Write a program that will determine whether it is possible to present a t-shirt to each participant of the competition, or not. Of course, each participant should get a t-shirt of proper size:
- the size he wanted, if he specified one size; - any of the two neibouring sizes, if he specified two sizes.
If it is possible, the program should find any valid distribution of the t-shirts. | The first line of the input contains six non-negative integersΒ β the number of t-shirts of each size. The numbers are given for the sizes S, M, L, XL, XXL, XXXL, respectively. The total number of t-shirts doesn't exceed 100<=000.
The second line contains positive integer *n* (1<=β€<=*n*<=β€<=100<=000)Β β the number of participants.
The following *n* lines contain the sizes specified by the participants, one line per participant. The *i*-th line contains information provided by the *i*-th participant: single size or two sizes separated by comma (without any spaces). If there are two sizes, the sizes are written in increasing order. It is guaranteed that two sizes separated by comma are neighboring. | If it is not possible to present a t-shirt to each participant, print Β«NOΒ» (without quotes).
Otherwise, print *n*<=+<=1 lines. In the first line print Β«YESΒ» (without quotes). In the following *n* lines print the t-shirt sizes the orginizers should give to participants, one per line. The order of the participants should be the same as in the input.
If there are multiple solutions, print any of them. | [
"0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL\n",
"1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL\n"
] | [
"YES\nXL\nM\nXXL\n",
"NO\n"
] | none | [
{
"input": "0 1 0 1 1 0\n3\nXL\nS,M\nXL,XXL",
"output": "YES\nXL\nM\nXXL"
},
{
"input": "1 1 2 0 1 1\n5\nS\nM\nS,M\nXXL,XXXL\nXL,XXL",
"output": "NO"
},
{
"input": "1 2 4 4 1 1\n10\nXL\nXL\nS,M\nL\nM,L\nL\nS,M\nM\nXL,XXL\nXL",
"output": "YES\nXL\nXL\nS\nL\nL\nL\nM\nM\nXL\nXL"
},
... | 888 | 10,240,000 | 0 | 30,163 | |
131 | The World is a Theatre | [
"combinatorics",
"math"
] | null | null | There are *n* boys and *m* girls attending a theatre club. To set a play "The Big Bang Theory", they need to choose a group containing exactly *t* actors containing no less than 4 boys and no less than one girl. How many ways are there to choose a group? Of course, the variants that only differ in the composition of the troupe are considered different.
Perform all calculations in the 64-bit type: long long for Π‘/Π‘++, int64 for Delphi and long for Java. | The only line of the input data contains three integers *n*, *m*, *t* (4<=β€<=*n*<=β€<=30,<=1<=β€<=*m*<=β€<=30,<=5<=β€<=*t*<=β€<=*n*<=+<=*m*). | Find the required number of ways.
Please do not use the %lld specificator to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. | [
"5 2 5\n",
"4 3 5\n"
] | [
"10\n",
"3\n"
] | none | [
{
"input": "5 2 5",
"output": "10"
},
{
"input": "4 3 5",
"output": "3"
},
{
"input": "4 1 5",
"output": "1"
},
{
"input": "7 3 6",
"output": "168"
},
{
"input": "30 30 30",
"output": "118264581548187697"
},
{
"input": "10 10 8",
"output": "84990"
... | 2,000 | 13,209,600 | 0 | 30,174 | |
137 | Last Chance | [
"data structures",
"implementation",
"strings"
] | null | null | Having read half of the book called "Storm and Calm" on the IT lesson, Innocentius was absolutely determined to finish the book on the maths lessons. All was fine until the math teacher Ms. Watkins saw Innocentius reading fiction books instead of solving equations of the fifth degree. As during the last maths class Innocentius suggested the algorithm of solving equations of the fifth degree in the general case, Ms. Watkins had no other choice but to give him a new task.
The teacher asked to write consecutively (without spaces) all words from the "Storm and Calm" in one long string *s*. She thought that a string is good if the number of vowels in the string is no more than twice more than the number of consonants. That is, the string with *v* vowels and *c* consonants is good if and only if *v*<=β€<=2*c*.
The task Innocentius had to solve turned out to be rather simple: he should find the number of the longest good substrings of the string *s*. | The only input line contains a non-empty string *s* consisting of no more than 2Β·105 uppercase and lowercase Latin letters. We shall regard letters "a", "e", "i", "o", "u" and their uppercase variants as vowels. | Print on a single line two numbers without a space: the maximum length of a good substring and the number of good substrings with this length. If no good substring exists, print "No solution" without the quotes.
Two substrings are considered different if their positions of occurrence are different. So if some string occurs more than once, then it should be counted more than once. | [
"Abo\n",
"OEIS\n",
"auBAAbeelii\n",
"AaaBRAaaCAaaDAaaBRAaa\n",
"EA\n"
] | [
"3 1\n",
"3 1\n",
"9 3\n",
"18 4\n",
"No solution\n"
] | In the first sample there is only one longest good substring: "Abo" itself. The other good substrings are "b", "Ab", "bo", but these substrings have shorter length.
In the second sample there is only one longest good substring: "EIS". The other good substrings are: "S", "IS". | [
{
"input": "Abo",
"output": "3 1"
},
{
"input": "OEIS",
"output": "3 1"
},
{
"input": "auBAAbeelii",
"output": "9 3"
},
{
"input": "AaaBRAaaCAaaDAaaBRAaa",
"output": "18 4"
},
{
"input": "EA",
"output": "No solution"
},
{
"input": "BBBAABAABAABBBB",
... | 60 | 0 | 0 | 30,175 | |
0 | none | [
"none"
] | null | null | Throughout Igor K.'s life he has had many situations worthy of attention. We remember the story with the virus, the story of his mathematical career and of course, his famous programming achievements. However, one does not always adopt new hobbies, one can quit something as well.
This time Igor K. got disappointed in one of his hobbies: editing and voicing videos. Moreover, he got disappointed in it so much, that he decided to destroy his secret archive for good.
Igor K. use Pindows XR operation system which represents files and folders by small icons. At that, *m* icons can fit in a horizontal row in any window.
Igor K.'s computer contains *n* folders in the D: disk's root catalog. The folders are numbered from 1 to *n* in the order from the left to the right and from top to bottom (see the images). At that the folders with secret videos have numbers from *a* to *b* inclusive. Igor K. wants to delete them forever, at that making as few frame selections as possible, and then pressing Shift+Delete exactly once. What is the minimum number of times Igor K. will have to select the folder in order to select folders from *a* to *b* and only them? Let us note that if some selected folder is selected repeatedly, then it is deselected. Each selection possesses the shape of some rectangle with sides parallel to the screen's borders. | The only line contains four integers *n*, *m*, *a*, *b* (1<=β€<=*n*,<=*m*<=β€<=109, 1<=β€<=*a*<=β€<=*b*<=β€<=*n*). They are the number of folders in Igor K.'s computer, the width of a window and the numbers of the first and the last folders that need to be deleted. | Print a single number: the least possible number of times Igor K. will have to select the folders using frames to select only the folders with numbers from *a* to *b*. | [
"11 4 3 9\n",
"20 5 2 20\n"
] | [
"3\n",
"2\n"
] | The images below illustrate statement tests.
The first test:
<img class="tex-graphics" src="https://espresso.codeforces.com/a0e4ba690dd16e3c68210a28afd82020b23fb605.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In this test we can select folders 3 and 4 with out first selection, folders 5, 6, 7, 8 with our second selection and folder 9 with our third, last selection.
The second test:
<img class="tex-graphics" src="https://espresso.codeforces.com/289e2666a3d8b3dfe5b22ff3d88976df711640f7.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In this test we can first select all folders in the first row (2, 3, 4, 5), then β all other ones. | [
{
"input": "11 4 3 9",
"output": "3"
},
{
"input": "20 5 2 20",
"output": "2"
},
{
"input": "1 1 1 1",
"output": "1"
},
{
"input": "26 5 2 18",
"output": "3"
},
{
"input": "21 5 1 15",
"output": "1"
},
{
"input": "21 5 1 21",
"output": "1"
},
{... | 340 | 0 | 0 | 30,204 | |
776 | The Holmes Children | [
"math",
"number theory"
] | null | null | The Holmes children are fighting over who amongst them is the cleverest.
Mycroft asked Sherlock and Eurus to find value of *f*(*n*), where *f*(1)<==<=1 and for *n*<=β₯<=2, *f*(*n*) is the number of distinct ordered positive integer pairs (*x*,<=*y*) that satisfy *x*<=+<=*y*<==<=*n* and *gcd*(*x*,<=*y*)<==<=1. The integer *gcd*(*a*,<=*b*) is the greatest common divisor of *a* and *b*.
Sherlock said that solving this was child's play and asked Mycroft to instead get the value of . Summation is done over all positive integers *d* that divide *n*.
Eurus was quietly observing all this and finally came up with her problem to astonish both Sherlock and Mycroft.
She defined a *k*-composite function *F**k*(*n*) recursively as follows:
She wants them to tell the value of *F**k*(*n*) modulo 1000000007. | A single line of input contains two space separated integers *n* (1<=β€<=*n*<=β€<=1012) and *k* (1<=β€<=*k*<=β€<=1012) indicating that Eurus asks Sherlock and Mycroft to find the value of *F**k*(*n*) modulo 1000000007. | Output a single integerΒ β the value of *F**k*(*n*) modulo 1000000007. | [
"7 1\n",
"10 2\n"
] | [
"6",
"4"
] | In the first case, there are 6 distinct ordered pairs (1,β6), (2,β5), (3,β4), (4,β3), (5,β2) and (6,β1) satisfying *x*β+β*y*β=β7 and *gcd*(*x*,β*y*)β=β1. Hence, *f*(7)β=β6. So, *F*<sub class="lower-index">1</sub>(7)β=β*f*(*g*(7))β=β*f*(*f*(7)β+β*f*(1))β=β*f*(6β+β1)β=β*f*(7)β=β6. | [
{
"input": "7 1",
"output": "6"
},
{
"input": "10 2",
"output": "4"
},
{
"input": "640 15",
"output": "2"
},
{
"input": "641 17",
"output": "2"
},
{
"input": "641 2000",
"output": "1"
},
{
"input": "961 2",
"output": "930"
},
{
"input": "52... | 2,000 | 307,200 | 0 | 30,219 | |
906 | Power Tower | [
"chinese remainder theorem",
"math",
"number theory"
] | null | null | Priests of the Quetzalcoatl cult want to build a tower to represent a power of their god. Tower is usually made of power-charged rocks. It is built with the help of rare magic by levitating the current top of tower and adding rocks at its bottom. If top, which is built from *k*<=-<=1 rocks, possesses power *p* and we want to add the rock charged with power *w**k* then value of power of a new tower will be {*w**k*}*p*.
Rocks are added from the last to the first. That is for sequence *w*1,<=...,<=*w**m* value of power will be
After tower is built, its power may be extremely large. But still priests want to get some information about it, namely they want to know a number called cumulative power which is the true value of power taken modulo *m*. Priests have *n* rocks numbered from 1 to *n*. They ask you to calculate which value of cumulative power will the tower possess if they will build it from rocks numbered *l*,<=*l*<=+<=1,<=...,<=*r*. | First line of input contains two integers *n* (1<=β€<=*n*<=β€<=105) and *m* (1<=β€<=*m*<=β€<=109).
Second line of input contains *n* integers *w**k* (1<=β€<=*w**k*<=β€<=109) which is the power of rocks that priests have.
Third line of input contains single integer *q* (1<=β€<=*q*<=β€<=105) which is amount of queries from priests to you.
*k**th* of next *q* lines contains two integers *l**k* and *r**k* (1<=β€<=*l**k*<=β€<=*r**k*<=β€<=*n*). | Output *q* integers. *k*-th of them must be the amount of cumulative power the tower will have if is built from rocks *l**k*,<=*l**k*<=+<=1,<=...,<=*r**k*. | [
"6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6\n"
] | [
"1\n1\n2\n4\n256\n3\n27\n597484987\n"
] | 3<sup class="upper-index">27</sup>β=β7625597484987 | [
{
"input": "6 1000000000\n1 2 2 3 3 3\n8\n1 1\n1 6\n2 2\n2 3\n2 4\n4 4\n4 5\n4 6",
"output": "1\n1\n2\n4\n256\n3\n27\n597484987"
},
{
"input": "10 20\n792708224 4633945 600798790 384332600 283309209 762285205 750900274 160512987 390669628 205259431\n10\n5 9\n10 10\n8 10\n7 10\n7 10\n10 10\n4 4\n10 1... | 4,500 | 9,113,600 | 0 | 30,275 | |
0 | none | [
"none"
] | null | null | Treeland is a country in which there are *n* towns connected by *n*<=-<=1 two-way road such that it's possible to get from any town to any other town.
In Treeland there are 2*k* universities which are located in different towns.
Recently, the president signed the decree to connect universities by high-speed network.The Ministry of Education understood the decree in its own way and decided that it was enough to connect each university with another one by using a cable. Formally, the decree will be done!
To have the maximum sum in the budget, the Ministry decided to divide universities into pairs so that the total length of the required cable will be maximum. In other words, the total distance between universities in *k* pairs should be as large as possible.
Help the Ministry to find the maximum total distance. Of course, each university should be present in only one pair. Consider that all roads have the same length which is equal to 1. | The first line of the input contains two integers *n* and *k* (2<=β€<=*n*<=β€<=200<=000, 1<=β€<=*k*<=β€<=*n*<=/<=2)Β β the number of towns in Treeland and the number of university pairs. Consider that towns are numbered from 1 to *n*.
The second line contains 2*k* distinct integers *u*1,<=*u*2,<=...,<=*u*2*k* (1<=β€<=*u**i*<=β€<=*n*)Β β indices of towns in which universities are located.
The next *n*<=-<=1 line contains the description of roads. Each line contains the pair of integers *x**j* and *y**j* (1<=β€<=*x**j*,<=*y**j*<=β€<=*n*), which means that the *j*-th road connects towns *x**j* and *y**j*. All of them are two-way roads. You can move from any town to any other using only these roads. | Print the maximum possible sum of distances in the division of universities into *k* pairs. | [
"7 2\n1 5 6 2\n1 3\n3 2\n4 5\n3 7\n4 3\n4 6\n",
"9 3\n3 2 1 6 5 9\n8 9\n3 2\n2 7\n3 4\n7 6\n4 5\n2 1\n2 8\n"
] | [
"6\n",
"9\n"
] | The figure below shows one of possible division into pairs in the first test. If you connect universities number 1 and 6 (marked in red) and universities number 2 and 5 (marked in blue) by using the cable, the total distance will equal 6 which will be the maximum sum in this example. | [
{
"input": "7 2\n1 5 6 2\n1 3\n3 2\n4 5\n3 7\n4 3\n4 6",
"output": "6"
},
{
"input": "9 3\n3 2 1 6 5 9\n8 9\n3 2\n2 7\n3 4\n7 6\n4 5\n2 1\n2 8",
"output": "9"
},
{
"input": "41 3\n11 10 15 35 34 6\n28 2\n25 3\n9 4\n11 5\n7 6\n24 7\n19 8\n1 9\n34 10\n23 11\n17 12\n32 13\n32 14\n32 15\n33 ... | 77 | 0 | 0 | 30,278 | |
925 | Big Secret | [
"constructive algorithms",
"math"
] | null | null | Vitya has learned that the answer for The Ultimate Question of Life, the Universe, and Everything is not the integer 54 42, but an increasing integer sequence $a_1, \ldots, a_n$. In order to not reveal the secret earlier than needed, Vitya encrypted the answer and obtained the sequence $b_1, \ldots, b_n$ using the following rules:
- $b_1 = a_1$;- $b_i = a_i \oplus a_{i - 1}$ for all $i$ from 2 to $n$, where $x \oplus y$ is the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of $x$ and $y$.
It is easy to see that the original sequence can be obtained using the rule $a_i = b_1 \oplus \ldots \oplus b_i$.
However, some time later Vitya discovered that the integers $b_i$ in the cypher got shuffled, and it can happen that when decrypted using the rule mentioned above, it can produce a sequence that is not increasing. In order to save his reputation in the scientific community, Vasya decided to find some permutation of integers $b_i$ so that the sequence $a_i = b_1 \oplus \ldots \oplus b_i$ is strictly increasing. Help him find such a permutation or determine that it is impossible. | The first line contains a single integer $n$ ($1 \leq n \leq 10^5$).
The second line contains $n$ integers $b_1, \ldots, b_n$ ($1 \leq b_i < 2^{60}$). | If there are no valid permutations, print a single line containing "No".
Otherwise in the first line print the word "Yes", and in the second line print integers $b'_1, \ldots, b'_n$Β β a valid permutation of integers $b_i$. The unordered multisets $\{b_1, \ldots, b_n\}$ and $\{b'_1, \ldots, b'_n\}$ should be equal, i.Β e. for each integer $x$ the number of occurrences of $x$ in the first multiset should be equal to the number of occurrences of $x$ in the second multiset. Apart from this, the sequence $a_i = b'_1 \oplus \ldots \oplus b'_i$ should be strictly increasing.
If there are multiple answers, print any of them. | [
"3\n1 2 3\n",
"6\n4 7 7 12 31 61\n"
] | [
"No\n",
"Yes\n4 12 7 31 7 61 \n"
] | In the first example no permutation is valid.
In the second example the given answer lead to the sequence $a_1 = 4$, $a_2 = 8$, $a_3 = 15$, $a_4 = 16$, $a_5 = 23$, $a_6 = 42$. | [
{
"input": "3\n1 2 3",
"output": "No"
},
{
"input": "6\n4 7 7 12 31 61",
"output": "Yes\n4 12 7 31 7 61 "
},
{
"input": "1\n4",
"output": "Yes\n4 "
},
{
"input": "2\n531 108",
"output": "Yes\n108 531 "
},
{
"input": "5\n3 1 1 7 1",
"output": "Yes\n1 3 1 7 1 "
... | 61 | 0 | 0 | 30,281 | |
171 | Mysterious numbers - 2 | [
"*special"
] | null | null | The only line of input contains three integers *a*1,<=*a*2,<=*a*3 (1<=β€<=*a*1,<=*a*2,<=*a*3<=β€<=20), separated by spaces.
Output a single integer. | The only line of input contains three integers *a*1,<=*a*2,<=*a*3 (1<=β€<=*a*1,<=*a*2,<=*a*3<=β€<=20), separated by spaces. | Output a single integer. | [
"2 3 2\n",
"13 14 1\n",
"14 5 9\n",
"17 18 3\n"
] | [
"5\n",
"14\n",
"464\n",
"53\n"
] | none | [
{
"input": "2 3 2",
"output": "5"
},
{
"input": "13 14 1",
"output": "14"
},
{
"input": "14 5 9",
"output": "464"
},
{
"input": "17 18 3",
"output": "53"
},
{
"input": "1 1 1",
"output": "1"
},
{
"input": "4 6 7",
"output": "110"
},
{
"inpu... | 92 | 0 | 3 | 30,317 | |
846 | Math Show | [
"brute force",
"greedy"
] | null | null | Polycarp takes part in a math show. He is given *n* tasks, each consists of *k* subtasks, numbered 1 through *k*. It takes him *t**j* minutes to solve the *j*-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.
By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all *k* of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is *k*<=+<=1.
Polycarp has *M* minutes of time. What is the maximum number of points he can earn? | The first line contains three integer numbers *n*, *k* and *M* (1<=β€<=*n*<=β€<=45, 1<=β€<=*k*<=β€<=45, 0<=β€<=*M*<=β€<=2Β·109).
The second line contains *k* integer numbers, values *t**j* (1<=β€<=*t**j*<=β€<=1000000), where *t**j* is the time in minutes required to solve *j*-th subtask of any task. | Print the maximum amount of points Polycarp can earn in *M* minutes. | [
"3 4 11\n1 2 3 4\n",
"5 5 10\n1 2 4 8 16\n"
] | [
"6\n",
"7\n"
] | In the first example Polycarp can complete the first task and spend 1β+β2β+β3β+β4β=β10 minutes. He also has the time to solve one subtask of the second task in one minute.
In the second example Polycarp can solve the first subtask of all five tasks and spend 5Β·1β=β5 minutes. Also he can solve the second subtasks of two tasks and spend 2Β·2β=β4 minutes. Thus, he earns 5β+β2β=β7 points in total. | [
{
"input": "3 4 11\n1 2 3 4",
"output": "6"
},
{
"input": "5 5 10\n1 2 4 8 16",
"output": "7"
},
{
"input": "1 1 0\n2",
"output": "0"
},
{
"input": "1 1 1\n1",
"output": "2"
},
{
"input": "2 1 0\n2",
"output": "0"
},
{
"input": "2 2 2\n2 3",
"outpu... | 62 | 0 | 0 | 30,324 | |
590 | Top Secret Task | [
"dp"
] | null | null | A top-secret military base under the command of Colonel Zuev is expecting an inspection from the Ministry of Defence. According to the charter, each top-secret military base must include a top-secret troop that should... well, we cannot tell you exactly what it should do, it is a top secret troop at the end. The problem is that Zuev's base is missing this top-secret troop for some reasons.
The colonel decided to deal with the problem immediately and ordered to line up in a single line all *n* soldiers of the base entrusted to him. Zuev knows that the loquacity of the *i*-th soldier from the left is equal to *q**i*. Zuev wants to form the top-secret troop using *k* leftmost soldiers in the line, thus he wants their total loquacity to be as small as possible (as the troop should remain top-secret). To achieve this, he is going to choose a pair of consecutive soldiers and swap them. He intends to do so no more than *s* times. Note that any soldier can be a participant of such swaps for any number of times. The problem turned out to be unusual, and colonel Zuev asked you to help.
Determine, what is the minimum total loquacity of the first *k* soldiers in the line, that can be achieved by performing no more than *s* swaps of two consecutive soldiers. | The first line of the input contains three positive integers *n*, *k*, *s* (1<=β€<=*k*<=β€<=*n*<=β€<=150, 1<=β€<=*s*<=β€<=109)Β β the number of soldiers in the line, the size of the top-secret troop to be formed and the maximum possible number of swap operations of the consecutive pair of soldiers, respectively.
The second line of the input contains *n* integer *q**i* (1<=β€<=*q**i*<=β€<=1<=000<=000)Β β the values of loquacity of soldiers in order they follow in line from left to right. | Print a single integer β the minimum possible total loquacity of the top-secret troop. | [
"3 2 2\n2 4 1\n",
"5 4 2\n10 1 6 2 5\n",
"5 2 3\n3 1 4 2 5\n"
] | [
"3\n",
"18\n",
"3\n"
] | In the first sample Colonel has to swap second and third soldiers, he doesn't really need the remaining swap. The resulting soldiers order is: (2, 1, 4). Minimum possible summary loquacity of the secret troop is 3. In the second sample Colonel will perform swaps in the following order:
1. (10, 1, 6 β 2, 5) 1. (10, 1, 2, 6 β 5)
The resulting soldiers order is (10, 1, 2, 5, 6).
Minimum possible summary loquacity is equal to 18. | [] | 30 | 0 | 0 | 30,359 | |
0 | none | [
"none"
] | null | null | Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out *n* numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect.
The lesson was long, and Vasya has written all the correct ways to place exactly *k* pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109<=+<=7. Help him! | The first line contains two integers, *n* and *k* (0<=β€<=*k*<=<<=*n*<=β€<=105).
The second line contains a string consisting of *n* digits. | Print the answer to the problem modulo 109<=+<=7. | [
"3 1\n108\n",
"3 2\n108\n"
] | [
"27",
"9"
] | In the first sample the result equals (1β+β08)β+β(10β+β8)β=β27.
In the second sample the result equals 1β+β0β+β8β=β9. | [
{
"input": "3 1\n108",
"output": "27"
},
{
"input": "3 2\n108",
"output": "9"
},
{
"input": "1 0\n5",
"output": "5"
},
{
"input": "5 2\n39923",
"output": "2667"
},
{
"input": "6 3\n967181",
"output": "3506"
},
{
"input": "7 1\n2178766",
"output": "... | 1,684 | 7,065,600 | 3 | 30,367 | |
0 | none | [
"none"
] | null | null | After the Search Ultimate program that searched for strings in a text failed, Igor K. got to think: "Why on Earth does my program work so slowly?" As he double-checked his code, he said: "My code contains no errors, yet I know how we will improve Search Ultimate!" and took a large book from the shelves. The book read "Azembler. Principally New Approach".
Having carefully thumbed through the book, Igor K. realised that, as it turns out, you can multiply the numbers dozens of times faster. "Search Ultimate will be faster than it has ever been!" β the fellow shouted happily and set to work.
Let us now clarify what Igor's idea was. The thing is that the code that was generated by a compiler was far from perfect. Standard multiplying does work slower than with the trick the book mentioned.
The Azembler language operates with 26 registers (eax, ebx, ..., ezx) and two commands:
- [*x*] β returns the value located in the address *x*. For example, [eax] returns the value that was located in the address, equal to the value in the register eax. - lea *x*, *y* β assigns to the register *x*, indicated as the first operand, the second operand's address. Thus, for example, the "lea ebx, [eax]" command will write in the ebx register the content of the eax register: first the [eax] operation will be fulfilled, the result of it will be some value that lies in the address written in eax. But we do not need the value β the next operation will be lea, that will take the [eax] address, i.e., the value in the eax register, and will write it in ebx.
On the first thought the second operation seems meaningless, but as it turns out, it is acceptable to write the operation as
lea ecx, [eax + ebx],
lea ecx, [k*eax]
or even
lea ecx, [ebx + k*eax],
where k = 1, 2, 4 or 8.
As a result, the register ecx will be equal to the numbers eax + ebx, k*eax and ebx + k*eax correspondingly. However, such operation is fulfilled many times, dozens of times faster that the usual multiplying of numbers. And using several such operations, one can very quickly multiply some number by some other one. Of course, instead of eax, ebx and ecx you are allowed to use any registers.
For example, let the eax register contain some number that we should multiply by 41. It takes us 2 lines:
lea ebx, [eax + 4*eax] // now ebx = 5*eax
lea eax, [eax + 8*ebx] // now eax = eax + 8*ebx = 41*eax
Igor K. got interested in the following question: what is the minimum number of lea operations needed to multiply by the given number *n* and how to do it? Your task is to help him.
Consider that at the initial moment of time eax contains a number that Igor K. was about to multiply by *n*, and the registers from ebx to ezx contain number 0. At the final moment of time the result can be located in any register. | The input data contain the only integer *n* (1<=β€<=*n*<=β€<=255), which Igor K. is about to multiply. | On the first line print number *p*, which represents the minimum number of lea operations, needed to do that. Then print the program consisting of *p* commands, performing the operations. It is guaranteed that such program exists for any *n* from 1 to 255.
Use precisely the following format of commands (here *k* is equal to 1, 2, 4 or 8, and *x*, *y* and *z* are any, even coinciding registers):
lea x, [y]
lea x, [y + z]
lea x, [k*y]
lea x, [y + k*z]
Please note that extra spaces at the end of a command are unacceptable. | [
"41\n",
"2\n",
"4\n"
] | [
"2\nlea ebx, [eax + 4*eax]\nlea ecx, [eax + 8*ebx]\n",
"1\nlea ebx, [eax + eax]\n",
"1\nlea ebx, [4*eax]\n"
] | none | [] | 280 | 1,228,800 | 0 | 30,493 | |
678 | Another Sith Tournament | [
"bitmasks",
"dp",
"math",
"probabilities"
] | null | null | The rules of Sith Tournament are well known to everyone. *n* Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive.
Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=18)Β β the number of participants of the Sith Tournament.
Each of the next *n* lines contains *n* real numbers, which form a matrix *p**ij* (0<=β€<=*p**ij*<=β€<=1). Each its element *p**ij* is the probability that the *i*-th participant defeats the *j*-th in a duel.
The elements on the main diagonal *p**ii* are equal to zero. For all different *i*, *j* the equality *p**ij*<=+<=*p**ji*<==<=1 holds. All probabilities are given with no more than six decimal places.
Jedi Ivan is the number 1 in the list of the participants. | Output a real numberΒ β the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10<=-<=6. | [
"3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0\n"
] | [
"0.680000000000000\n"
] | none | [
{
"input": "3\n0.0 0.5 0.8\n0.5 0.0 0.4\n0.2 0.6 0.0",
"output": "0.680000000000000"
},
{
"input": "1\n0.0",
"output": "1.000000000000000"
},
{
"input": "2\n0.00 0.75\n0.25 0.00",
"output": "0.750000000000000"
},
{
"input": "4\n0.0 0.6 0.5 0.4\n0.4 0.0 0.3 0.8\n0.5 0.7 0.0 0.... | 2,136 | 98,406,400 | 0 | 30,594 | |
95 | Horse Races | [
"dp",
"math"
] | D. Horse Races | 2 | 256 | Petya likes horse racing very much. Horses numbered from *l* to *r* take part in the races. Petya wants to evaluate the probability of victory; for some reason, to do that he needs to know the amount of nearly lucky horses' numbers. A nearly lucky number is an integer number that has at least two lucky digits the distance between which does not exceed *k*. Petya learned from some of his mates from Lviv that lucky digits are digits 4 and 7. The distance between the digits is the absolute difference between their positions in the number of a horse. For example, if *k*<==<=2, then numbers 412395497, 404, 4070400000070004007 are nearly lucky and numbers 4, 4123954997, 4007000040070004007 are not.
Petya prepared *t* intervals [*l**i*,<=*r**i*] and invented number *k*, common for all of them. Your task is to find how many nearly happy numbers there are in each of these segments. Since the answers can be quite large, output them modulo 1000000007 (109<=+<=7). | The first line contains two integers *t* and *k* (1<=β€<=*t*,<=*k*<=β€<=1000) β the number of segments and the distance between the numbers correspondingly. Next *t* lines contain pairs of integers *l**i* and *r**i* (1<=β€<=*l*<=β€<=*r*<=β€<=101000). All numbers are given without the leading zeroes. Numbers in each line are separated by exactly one space character. | Output *t* lines. In each line print one integer β the answer for the corresponding segment modulo 1000000007 (109<=+<=7). | [
"1 2\n1 100\n",
"1 2\n70 77\n",
"2 1\n1 20\n80 100\n"
] | [
"4\n",
"2\n",
"0\n0\n"
] | In the first sample, the four nearly lucky numbers are 44, 47, 74, 77.
In the second sample, only 74 and 77 are in the given segment. | [] | 92 | 0 | 0 | 30,595 |
708 | Student's Camp | [
"dp",
"math"
] | null | null | Alex studied well and won the trip to student camp Alushta, located on the seashore.
Unfortunately, it's the period of the strong winds now and there is a chance the camp will be destroyed! Camp building can be represented as the rectangle of *n*<=+<=2 concrete blocks height and *m* blocks width.
Every day there is a breeze blowing from the sea. Each block, except for the blocks of the upper and lower levers, such that there is no block to the left of it is destroyed with the probability . Similarly, each night the breeze blows in the direction to the sea. Thus, each block (again, except for the blocks of the upper and lower levers) such that there is no block to the right of it is destroyed with the same probability *p*. Note, that blocks of the upper and lower level are indestructible, so there are only *n*Β·*m* blocks that can be destroyed.
The period of the strong winds will last for *k* days and *k* nights. If during this period the building will split in at least two connected components, it will collapse and Alex will have to find another place to spend summer.
Find the probability that Alex won't have to look for other opportunities and will be able to spend the summer in this camp. | The first line of the input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1500) that define the size of the destructible part of building.
The second line of the input contains two integers *a* and *b* (1<=β€<=*a*<=β€<=*b*<=β€<=109) that define the probability *p*. It's guaranteed that integers *a* and *b* are coprime.
The third line contains a single integer *k* (0<=β€<=*k*<=β€<=100<=000)Β β the number of days and nights strong wind will blow for. | Consider the answer as an irreducible fraction is equal to . Print one integer equal to . It's guaranteed that within the given constraints . | [
"2 2\n1 2\n1\n",
"5 1\n3 10\n1\n",
"3 3\n1 10\n5\n"
] | [
"937500007\n",
"95964640\n",
"927188454\n"
] | In the first sample, each of the four blocks is destroyed with the probability <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/eb946338365d9781f7d2e9ec692c26702d0ae3a7.png" style="max-width: 100.0%;max-height: 100.0%;"/>. There are 7 scenarios that result in building not collapsing, and the probability we are looking for is equal to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d952828d9a444bd03168321a86984408b0e10b27.png" style="max-width: 100.0%;max-height: 100.0%;"/>, so you should print <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7f4efe64265b7f037ad984f8fd40cb932822d500.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 46 | 0 | 0 | 30,662 | |
329 | Graph Reconstruction | [
"constructive algorithms"
] | null | null | I have an undirected graph consisting of *n* nodes, numbered 1 through *n*. Each node has at most two incident edges. For each pair of nodes, there is at most an edge connecting them. No edge connects a node to itself.
I would like to create a new graph in such a way that:
- The new graph consists of the same number of nodes and edges as the old graph. - The properties in the first paragraph still hold. - For each two nodes *u* and *v*, if there is an edge connecting them in the old graph, there is no edge connecting them in the new graph.
Help me construct the new graph, or tell me if it is impossible. | The first line consists of two space-separated integers: *n* and *m* (1<=β€<=*m*<=β€<=*n*<=β€<=105), denoting the number of nodes and edges, respectively. Then *m* lines follow. Each of the *m* lines consists of two space-separated integers *u* and *v* (1<=β€<=*u*,<=*v*<=β€<=*n*;Β *u*<=β <=*v*), denoting an edge between nodes *u* and *v*. | If it is not possible to construct a new graph with the mentioned properties, output a single line consisting of -1. Otherwise, output exactly *m* lines. Each line should contain a description of edge in the same way as used in the input format. | [
"8 7\n1 2\n2 3\n4 5\n5 6\n6 8\n8 7\n7 4\n",
"3 2\n1 2\n2 3\n",
"5 4\n1 2\n2 3\n3 4\n4 1\n"
] | [
"1 4\n4 6\n1 6\n2 7\n7 5\n8 5\n2 8\n",
"-1\n",
"1 3\n3 5\n5 2\n2 4\n"
] | The old graph of the first example:
<img class="tex-graphics" src="https://espresso.codeforces.com/1a5d4ab85ef86541ea9bea88ee537f6852ca2194.png" style="max-width: 100.0%;max-height: 100.0%;"/>
A possible new graph for the first example:
<img class="tex-graphics" src="https://espresso.codeforces.com/8a2d63a60d51967903043452c9d1fe4dd6385753.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In the second example, we cannot create any new graph.
The old graph of the third example:
<img class="tex-graphics" src="https://espresso.codeforces.com/22079249a5965faa550b830e5827cde2910342f3.png" style="max-width: 100.0%;max-height: 100.0%;"/>
A possible new graph for the third example:
<img class="tex-graphics" src="https://espresso.codeforces.com/69fb5a55e3d0dde42a4ba4131e82d463f782fe9e.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [] | 3,000 | 12,902,400 | 0 | 30,749 | |
29 | Quarrel | [
"graphs",
"shortest paths"
] | E. Quarrel | 1 | 256 | Friends Alex and Bob live in Bertown. In this town there are *n* crossroads, some of them are connected by bidirectional roads of equal length. Bob lives in a house at the crossroads number 1, Alex β in a house at the crossroads number *n*.
One day Alex and Bob had a big quarrel, and they refused to see each other. It occurred that today Bob needs to get from his house to the crossroads *n* and Alex needs to get from his house to the crossroads 1. And they don't want to meet at any of the crossroads, but they can meet in the middle of the street, when passing it in opposite directions. Alex and Bob asked you, as their mutual friend, to help them with this difficult task.
Find for Alex and Bob such routes with equal number of streets that the guys can follow these routes and never appear at the same crossroads at the same time. They are allowed to meet in the middle of the street when moving toward each other (see Sample 1). Among all possible routes, select such that the number of streets in it is the least possible. Until both guys reach their destinations, none of them can stay without moving.
The guys are moving simultaneously with equal speeds, i.e. it is possible that when one of them reaches some of the crossroads, the other one leaves it. For example, Alex can move from crossroad 1 to crossroad 2, while Bob moves from crossroad 2 to crossroad 3.
If the required routes don't exist, your program should output -1. | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=500,<=1<=β€<=*m*<=β€<=10000) β the amount of crossroads and the amount of roads. Each of the following *m* lines contains two integers β the numbers of crossroads connected by the road. It is guaranteed that no road connects a crossroads with itself and no two crossroads are connected by more than one road. | If the required routes don't exist, output -1. Otherwise, the first line should contain integer *k* β the length of shortest routes (the length of the route is the amount of roads in it). The next line should contain *k*<=+<=1 integers β Bob's route, i.e. the numbers of *k*<=+<=1 crossroads passed by Bob. The last line should contain Alex's route in the same format. If there are several optimal solutions, output any of them. | [
"2 1\n1 2\n",
"7 5\n1 2\n2 7\n7 6\n2 3\n3 4\n",
"7 6\n1 2\n2 7\n7 6\n2 3\n3 4\n1 5\n"
] | [
"1\n1 2 \n2 1 \n",
"-1\n",
"6\n1 2 3 4 3 2 7 \n7 6 7 2 1 5 1 \n"
] | none | [
{
"input": "2 1\n1 2",
"output": "1\n1 2 \n2 1 "
},
{
"input": "7 5\n1 2\n2 7\n7 6\n2 3\n3 4",
"output": "-1"
},
{
"input": "7 6\n1 2\n2 7\n7 6\n2 3\n3 4\n1 5",
"output": "6\n1 2 3 4 3 2 7 \n7 6 7 2 1 5 1 "
},
{
"input": "6 10\n3 6\n3 5\n1 3\n2 6\n5 4\n6 4\n6 5\n5 1\n2 3\n1 2... | 92 | 0 | 0 | 30,802 |
8 | Looking for Order | [
"bitmasks",
"dp"
] | C. Looking for Order | 4 | 512 | Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess β all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl cannot carry more than two objects at a time, and cannot move the handbag. Also, if he has taken an object, she cannot put it anywhere except her handbag β her inherent sense of order does not let her do so.
You are given the coordinates of the handbag and the coordinates of the objects in some Π‘artesian coordinate system. It is known that the girl covers the distance between any two objects in the time equal to the squared length of the segment between the points of the objects. It is also known that initially the coordinates of the girl and the handbag are the same. You are asked to find such an order of actions, that the girl can put all the objects back into her handbag in a minimum time period. | The first line of the input file contains the handbag's coordinates *x**s*,<=*y**s*. The second line contains number *n* (1<=β€<=*n*<=β€<=24) β the amount of objects the girl has. The following *n* lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are different. All the numbers are integer. | In the first line output the only number β the minimum time the girl needs to put the objects into her handbag.
In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to *n*), the handbag's point is described by number 0. The path should start and end in the handbag's point. If there are several optimal paths, print any of them. | [
"0 0\n2\n1 1\n-1 1\n",
"1 1\n3\n4 3\n3 4\n0 0\n"
] | [
"8\n0 1 2 0 \n",
"32\n0 1 2 0 3 0 \n"
] | none | [
{
"input": "0 0\n2\n1 1\n-1 1",
"output": "8\n0 1 2 0 "
},
{
"input": "1 1\n3\n4 3\n3 4\n0 0",
"output": "32\n0 1 2 0 3 0 "
},
{
"input": "-3 4\n1\n2 2",
"output": "58\n0 1 0 "
},
{
"input": "7 -7\n2\n3 1\n-3 8",
"output": "490\n0 1 2 0 "
},
{
"input": "3 -9\n3\n0... | 61 | 0 | 0 | 30,803 |
353 | Find Maximum | [
"implementation",
"math",
"number theory"
] | null | null | Valera has array *a*, consisting of *n* integers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1, and function *f*(*x*), taking an integer from 0 to 2*n*<=-<=1 as its single argument. Value *f*(*x*) is calculated by formula , where value *bit*(*i*) equals one if the binary representation of number *x* contains a 1 on the *i*-th position, and zero otherwise.
For example, if *n*<==<=4 and *x*<==<=11 (11<==<=20<=+<=21<=+<=23), then *f*(*x*)<==<=*a*0<=+<=*a*1<=+<=*a*3.
Help Valera find the maximum of function *f*(*x*) among all *x*, for which an inequality holds: 0<=β€<=*x*<=β€<=*m*. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of array elements. The next line contains *n* space-separated integers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (0<=β€<=*a**i*<=β€<=104) β elements of array *a*.
The third line contains a sequence of digits zero and one without spaces *s*0*s*1... *s**n*<=-<=1 β the binary representation of number *m*. Number *m* equals . | Print a single integer β the maximum value of function *f*(*x*) for all . | [
"2\n3 8\n10\n",
"5\n17 0 10 2 1\n11010\n"
] | [
"3\n",
"27\n"
] | In the first test case *m*β=β2<sup class="upper-index">0</sup>β=β1,β*f*(0)β=β0,β*f*(1)β=β*a*<sub class="lower-index">0</sub>β=β3.
In the second sample *m*β=β2<sup class="upper-index">0</sup>β+β2<sup class="upper-index">1</sup>β+β2<sup class="upper-index">3</sup>β=β11, the maximum value of function equals *f*(5)β=β*a*<sub class="lower-index">0</sub>β+β*a*<sub class="lower-index">2</sub>β=β17β+β10β=β27. | [
{
"input": "2\n3 8\n10",
"output": "3"
},
{
"input": "5\n17 0 10 2 1\n11010",
"output": "27"
},
{
"input": "18\n4382 3975 9055 7554 8395 204 5313 5739 1555 2306 5423 828 8108 9736 2683 7940 1249 5495\n110001100101110111",
"output": "88691"
},
{
"input": "43\n475 2165 8771 714... | 434 | 32,768,000 | -1 | 30,840 | |
883 | Palindromic Cut | [
"brute force",
"implementation",
"strings"
] | null | null | Kolya has a string *s* of length *n* consisting of lowercase and uppercase Latin letters and digits.
He wants to rearrange the symbols in *s* and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forward, such as madam or racecar.
Your task is to help Kolya and determine the minimum number of palindromes of equal lengths to cut *s* into, if it is allowed to rearrange letters in *s* before cuttings. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=4Β·105) β the length of string *s*.
The second line contains a string *s* of length *n* consisting of lowercase and uppercase Latin letters and digits. | Print to the first line an integer *k* β minimum number of palindromes into which you can cut a given string.
Print to the second line *k* strings β the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length. | [
"6\naabaac\n",
"8\n0rTrT022\n",
"2\naA\n"
] | [
"2\naba aca ",
"1\n02TrrT20 ",
"2\na A \n"
] | none | [
{
"input": "6\naabaac",
"output": "2\naba aca "
},
{
"input": "8\n0rTrT022",
"output": "1\n02TrrT20 "
},
{
"input": "2\naA",
"output": "2\na A "
},
{
"input": "1\ns",
"output": "1\ns "
},
{
"input": "10\n6IIC6CCIIC",
"output": "1\n6CCIIIICC6 "
},
{
"in... | 61 | 5,529,600 | 0 | 30,899 | |
372 | Watching Fireworks is Fun | [
"data structures",
"dp",
"math"
] | null | null | A festival will be held in a town's main street. There are *n* sections in the main street. The sections are numbered 1 through *n* from left to right. The distance between each adjacent sections is 1.
In the festival *m* fireworks will be launched. The *i*-th (1<=β€<=*i*<=β€<=*m*) launching is on time *t**i* at section *a**i*. If you are at section *x* (1<=β€<=*x*<=β€<=*n*) at the time of *i*-th launching, you'll gain happiness value *b**i*<=-<=|*a**i*<=-<=*x*| (note that the happiness value might be a negative value).
You can move up to *d* length units in a unit time interval, but it's prohibited to go out of the main street. Also you can be in an arbitrary section at initial time moment (time equals to 1), and want to maximize the sum of happiness that can be gained from watching fireworks. Find the maximum total happiness.
Note that two or more fireworks can be launched at the same time. | The first line contains three integers *n*, *m*, *d* (1<=β€<=*n*<=β€<=150000;Β 1<=β€<=*m*<=β€<=300;Β 1<=β€<=*d*<=β€<=*n*).
Each of the next *m* lines contains integers *a**i*, *b**i*, *t**i* (1<=β€<=*a**i*<=β€<=*n*;Β 1<=β€<=*b**i*<=β€<=109;Β 1<=β€<=*t**i*<=β€<=109). The *i*-th line contains description of the *i*-th launching.
It is guaranteed that the condition *t**i*<=β€<=*t**i*<=+<=1 (1<=β€<=*i*<=<<=*m*) will be satisfied. | Print a single integer β the maximum sum of happiness that you can gain from watching all the fireworks.
Please, do not write 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. | [
"50 3 1\n49 1 1\n26 1 4\n6 1 10\n",
"10 2 1\n1 1000 4\n9 1000 4\n"
] | [
"-31\n",
"1992\n"
] | none | [
{
"input": "50 3 1\n49 1 1\n26 1 4\n6 1 10",
"output": "-31"
},
{
"input": "10 2 1\n1 1000 4\n9 1000 4",
"output": "1992"
},
{
"input": "30 8 2\n15 97 3\n18 64 10\n20 14 20\n16 18 36\n10 23 45\n12 60 53\n17 93 71\n11 49 85",
"output": "418"
},
{
"input": "100 20 5\n47 93 3\n6... | 109 | 9,420,800 | -1 | 30,975 | |
306 | Polygon | [
"constructive algorithms",
"geometry"
] | null | null | Polycarpus loves convex polygons, especially if all their angles are the same and all their sides are different. Draw for him any such polygon with the given number of vertexes. | The input contains a single integer *n* (3<=β€<=*n*<=β€<=100) β the number of the polygon vertexes. | Print *n* lines, containing the coordinates of the vertexes of the *n*-gon "*x**i* *y**i*" in the counter clockwise order. The coordinates of the vertexes shouldn't exceed 106 in their absolute value. The side lengths should fit within limits [1,<=1000] (not necessarily integer). Mutual comparing sides and angles of your polygon during the test will go with precision of 10<=-<=3.
If there is no solution, print "No solution" (without the quotes). | [
"8\n"
] | [
"1.000 0.000\n7.000 0.000\n9.000 2.000\n9.000 3.000\n5.000 7.000\n3.000 7.000\n0.000 4.000\n0.000 1.000\n"
] | none | [] | 92 | 0 | 0 | 31,055 | |
164 | Minimum Diameter | [
"binary search",
"brute force"
] | null | null | You are given *n* points on the plane. You need to delete exactly *k* of them (*k*<=<<=*n*) so that the diameter of the set of the remaining *n*<=-<=*k* points were as small as possible. The diameter of a set of points is the maximum pairwise distance between the points of the set. The diameter of a one point set equals zero. | The first input line contains a pair of integers *n*,<=*k* (2<=β€<=*n*<=β€<=1000, 1<=β€<=*k*<=β€<=30, *k*<=<<=*n*) β the numbers of points on the plane and the number of points to delete, correspondingly.
Next *n* lines describe the points, one per line. Each description consists of a pair of integers *x**i*,<=*y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=32000) β the coordinates of the *i*-th point. The given points can coincide. | Print *k* different space-separated integers from 1 to *n* β the numbers of points to delete. The points are numbered in the order, in which they are given in the input from 1 to *n*. You can print the numbers in any order. If there are multiple solutions, print any of them. | [
"5 2\n1 2\n0 0\n2 2\n1 1\n3 3\n",
"4 1\n0 0\n0 0\n1 1\n1 1\n"
] | [
"5 2",
"3"
] | none | [] | 30 | 0 | 0 | 31,076 | |
471 | MUH and House of Cards | [
"binary search",
"brute force",
"greedy",
"math"
] | null | null | Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of *n* playing cards. Let's describe the house they want to make:
1. The house consists of some non-zero number of floors. 1. Each floor consists of a non-zero number of rooms and the ceiling. A room is two cards that are leaned towards each other. The rooms are made in a row, each two adjoining rooms share a ceiling made by another card. 1. Each floor besides for the lowest one should contain less rooms than the floor below.
Please note that the house may end by the floor with more than one room, and in this case they also must be covered by the ceiling. Also, the number of rooms on the adjoining floors doesn't have to differ by one, the difference may be more.
While bears are practicing to put cards, Horace tries to figure out how many floors their house should consist of. The height of the house is the number of floors in it. It is possible that you can make a lot of different houses of different heights out of *n* cards. It seems that the elephant cannot solve this problem and he asks you to count the number of the distinct heights of the houses that they can make using exactly *n* cards. | The single line contains integer *n* (1<=β€<=*n*<=β€<=1012) β the number of cards. | Print the number of distinct heights that the houses made of exactly *n* cards can have. | [
"13\n",
"6\n"
] | [
"1",
"0"
] | In the first sample you can build only these two houses (remember, you must use all the cards):
Thus, 13 cards are enough only for two floor houses, so the answer is 1.
The six cards in the second sample are not enough to build any house. | [
{
"input": "13",
"output": "1"
},
{
"input": "6",
"output": "0"
},
{
"input": "26",
"output": "2"
},
{
"input": "1000000000000",
"output": "272165"
},
{
"input": "571684826707",
"output": "205784"
},
{
"input": "178573947413",
"output": "115012"
... | 935 | 4,608,000 | 3 | 31,100 | |
908 | New Year and Rainbow Roads | [
"graphs",
"greedy",
"implementation"
] | null | null | Roy and Biv have a set of *n* points on the infinite number line.
Each point has one of 3 colors: red, green, or blue.
Roy and Biv would like to connect all the points with some edges. Edges can be drawn between any of the two of the given points. The cost of an edge is equal to the distance between the two points it connects.
They want to do this in such a way that they will both see that all the points are connected (either directly or indirectly).
However, there is a catch: Roy cannot see the color red and Biv cannot see the color blue.
Therefore, they have to choose the edges in such a way that if all the red points are removed, the remaining blue and green points are connected (and similarly, if all the blue points are removed, the remaining red and green points are connected).
Help them compute the minimum cost way to choose edges to satisfy the above constraints. | The first line will contain an integer *n* (1<=β€<=*n*<=β€<=300<=000), the number of points.
The next *n* lines will contain two tokens *p**i* and *c**i* (*p**i* is an integer, 1<=β€<=*p**i*<=β€<=109, *c**i* is a uppercase English letter 'R', 'G' or 'B'), denoting the position of the *i*-th point and the color of the *i*-th point. 'R' means red, 'G' denotes green, and 'B' means blue. The positions will be in strictly increasing order. | Print a single integer, the minimum cost way to solve the problem. | [
"4\n1 G\n5 R\n10 B\n15 G\n",
"4\n1 G\n2 R\n3 B\n10 G\n"
] | [
"23\n",
"12\n"
] | In the first sample, it is optimal to draw edges between the points (1,2), (1,4), (3,4). These have costs 4, 14, 5, respectively. | [
{
"input": "4\n1 G\n5 R\n10 B\n15 G",
"output": "23"
},
{
"input": "4\n1 G\n2 R\n3 B\n10 G",
"output": "12"
},
{
"input": "4\n1 G\n123123 R\n987987987 B\n1000000000 G",
"output": "1012135134"
},
{
"input": "1\n3 R",
"output": "0"
}
] | 46 | 5,529,600 | 0 | 31,101 | |
853 | Michael and Charging Stations | [
"binary search",
"dp",
"greedy"
] | null | null | Michael has just bought a new electric car for moving across city. Michael does not like to overwork, so each day he drives to only one of two his jobs.
Michael's day starts from charging his electric car for getting to the work and back. He spends 1000 burles on charge if he goes to the first job, and 2000 burles if he goes to the second job.
On a charging station he uses there is a loyalty program that involves bonus cards. Bonus card may have some non-negative amount of bonus burles. Each time customer is going to buy something for the price of *x* burles, he is allowed to pay an amount of *y* (0<=β€<=*y*<=β€<=*x*) burles that does not exceed the bonus card balance with bonus burles. In this case he pays *x*<=-<=*y* burles with cash, and the balance on the bonus card is decreased by *y* bonus burles.
If customer pays whole price with cash (i.e., *y*<==<=0) then 10% of price is returned back to the bonus card. This means that bonus card balance increases by bonus burles. Initially the bonus card balance is equal to 0 bonus burles.
Michael has planned next *n* days and he knows how much does the charge cost on each of those days. Help Michael determine the minimum amount of burles in cash he has to spend with optimal use of bonus card. Assume that Michael is able to cover any part of the price with cash in any day. It is not necessary to spend all bonus burles at the end of the given period. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=300<=000), the number of days Michael has planned.
Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (*a**i*<==<=1000 or *a**i*<==<=2000) with *a**i* denoting the charging cost at the day *i*. | Output the minimum amount of burles Michael has to spend. | [
"3\n1000 2000 1000\n",
"6\n2000 2000 2000 2000 2000 1000\n"
] | [
"3700\n",
"10000\n"
] | In the first sample case the most optimal way for Michael is to pay for the first two days spending 3000 burles and get 300 bonus burles as return. After that he is able to pay only 700 burles for the third days, covering the rest of the price with bonus burles.
In the second sample case the most optimal way for Michael is to pay the whole price for the first five days, getting 1000 bonus burles as return and being able to use them on the last day without paying anything in cash. | [] | 202 | 25,190,400 | 0 | 31,111 | |
797 | Array Queries | [
"brute force",
"data structures",
"dp"
] | null | null | *a* is an array of *n* positive integers, all of which are not greater than *n*.
You have to process *q* queries to this array. Each query is represented by two numbers *p* and *k*. Several operations are performed in each query; each operation changes *p* to *p*<=+<=*a**p*<=+<=*k*. There operations are applied until *p* becomes greater than *n*. The answer to the query is the number of performed operations. | The first line contains one integer *n* (1<=β€<=*n*<=β€<=100000).
The second line contains *n* integers β elements of *a* (1<=β€<=*a**i*<=β€<=*n* for each *i* from 1 to *n*).
The third line containts one integer *q* (1<=β€<=*q*<=β€<=100000).
Then *q* lines follow. Each line contains the values of *p* and *k* for corresponding query (1<=β€<=*p*,<=*k*<=β€<=*n*). | Print *q* integers, *i*th integer must be equal to the answer to *i*th query. | [
"3\n1 1 1\n3\n1 1\n2 1\n3 1\n"
] | [
"2\n1\n1\n"
] | Consider first example:
In first query after first operation *p*β=β3, after second operation *p*β=β5.
In next two queries *p* is greater than *n* after the first operation. | [
{
"input": "3\n1 1 1\n3\n1 1\n2 1\n3 1",
"output": "2\n1\n1"
},
{
"input": "10\n3 5 4 3 7 10 6 7 2 3\n10\n4 5\n2 10\n4 6\n9 9\n9 2\n5 1\n6 4\n1 1\n5 6\n6 4",
"output": "1\n1\n1\n1\n1\n1\n1\n2\n1\n1"
},
{
"input": "50\n6 2 5 6 10 2 5 8 9 2 9 5 10 4 3 6 10 6 1 1 3 7 2 1 7 8 5 9 6 2 7 6 1 7... | 77 | 0 | 0 | 31,349 | |
769 | Cycle In Maze | [
"*special",
"dfs and similar",
"graphs",
"greedy",
"shortest paths"
] | null | null | The Robot is in a rectangular maze of size *n*<=Γ<=*m*. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol "L"), right (the symbol "R"), up (the symbol "U") or down (the symbol "D"). The Robot can move to the cell only if it is empty. Initially, the Robot is in the empty cell.
Your task is to find lexicographically minimal Robot's cycle with length exactly *k*, which begins and ends in the cell where the Robot was initially. It is allowed to the Robot to visit any cell many times (including starting).
Consider that Robot's way is given as a line which consists of symbols "L", "R", "U" and "D". For example, if firstly the Robot goes down, then left, then right and up, it means that his way is written as "DLRU".
In this task you don't need to minimize the length of the way. Find the minimum lexicographical (in alphabet order as in the dictionary) line which satisfies requirements above. | The first line contains three integers *n*, *m* and *k* (1<=β€<=*n*,<=*m*<=β€<=1000, 1<=β€<=*k*<=β€<=106) β the size of the maze and the length of the cycle.
Each of the following *n* lines contains *m* symbols β the description of the maze. If the symbol equals to "." the current cell is empty. If the symbol equals to "*" the current cell is occupied by an obstacle. If the symbol equals to "X" then initially the Robot is in this cell and it is empty. It is guaranteed that the symbol "X" is found in the maze exactly once. | Print the lexicographically minimum Robot's way with the length exactly *k*, which starts and ends in the cell where initially Robot is. If there is no such way, print "IMPOSSIBLE"(without quotes). | [
"2 3 2\n.**\nX..\n",
"5 6 14\n..***.\n*...X.\n..*...\n..*.**\n....*.\n",
"3 3 4\n***\n*X*\n***\n"
] | [
"RL\n",
"DLDDLLLRRRUURU\n",
"IMPOSSIBLE\n"
] | In the first sample two cyclic ways for the Robot with the length 2 exist β "UD" and "RL". The second cycle is lexicographically less.
In the second sample the Robot should move in the following way: down, left, down, down, left, left, left, right, right, right, up, up, right, up.
In the third sample the Robot can't move to the neighboring cells, because they are occupied by obstacles. | [
{
"input": "2 3 2\n.**\nX..",
"output": "RL"
},
{
"input": "5 6 14\n..***.\n*...X.\n..*...\n..*.**\n....*.",
"output": "DLDDLLLRRRUURU"
},
{
"input": "3 3 4\n***\n*X*\n***",
"output": "IMPOSSIBLE"
},
{
"input": "1 1 1\nX",
"output": "IMPOSSIBLE"
},
{
"input": "1 2... | 46 | 0 | -1 | 31,504 | |
86 | Tetris revisited | [
"constructive algorithms",
"graph matchings",
"greedy",
"math"
] | B. Tetris revisited | 1 | 256 | Physicist Woll likes to play one relaxing game in between his search of the theory of everything.
Game interface consists of a rectangular *n*<=Γ<=*m* playing field and a dashboard. Initially some cells of the playing field are filled while others are empty. Dashboard contains images of all various connected (we mean connectivity by side) figures of 2, 3, 4 and 5 cells, with all their rotations and reflections. Player can copy any figure from the dashboard and place it anywhere at the still empty cells of the playing field. Of course any figure can be used as many times as needed.
Woll's aim is to fill the whole field in such a way that there are no empty cells left, and also... just have some fun.
Every initially empty cell should be filled with exactly one cell of some figure. Every figure should be entirely inside the board.
In the picture black cells stand for initially filled cells of the field, and one-colour regions represent the figures. | First line contains integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=1000) β the height and the width of the field correspondingly. Next *n* lines contain *m* symbols each. They represent the field in a natural way: *j*-th character of the *i*-th line is "#" if the corresponding cell is filled, and "." if it is empty. | If there is no chance to win the game output the only number "-1" (without the quotes). Otherwise output any filling of the field by the figures in the following format: each figure should be represented by some digit and figures that touch each other by side should be represented by distinct digits. Every initially filled cell should be represented by "#". | [
"2 3\n...\n#.#\n",
"3 3\n.#.\n...\n..#\n",
"3 3\n...\n.##\n.#.\n",
"1 2\n##\n"
] | [
"000\n#0#\n",
"5#1\n511\n55#\n",
"-1\n",
"##\n"
] | In the third sample, there is no way to fill a cell with no empty neighbours.
In the forth sample, Woll does not have to fill anything, so we should output the field from the input. | [] | 31 | 0 | 0 | 31,558 |
413 | Maze 2D | [
"data structures",
"divide and conquer"
] | null | null | The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2<=Γ<=*n* maze.
Imagine a maze that looks like a 2<=Γ<=*n* rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other.
Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a 2<=Γ<=*n* maze. | The first line contains two integers, *n* and *m* (1<=β€<=*n*<=β€<=2Β·105;Β 1<=β€<=*m*<=β€<=2Β·105) β the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains *n* characters, each character equals either '.' (empty cell), or 'X' (obstacle).
Each of the next *m* lines contains two integers *v**i* and *u**i* (1<=β€<=*v**i*,<=*u**i*<=β€<=2*n*) β the description of the *i*-th request. Numbers *v**i*, *u**i* mean that you need to print the value of the shortest path from the cell of the maze number *v**i* to the cell number *u**i*. We assume that the cells of the first line of the maze are numbered from 1 to *n*, from left to right, and the cells of the second line are numbered from *n*<=+<=1 to 2*n* from left to right. It is guaranteed that both given cells are empty. | Print *m* lines. In the *i*-th line print the answer to the *i*-th request β either the size of the shortest path or -1, if we can't reach the second cell from the first one. | [
"4 7\n.X..\n...X\n5 1\n1 3\n7 7\n1 4\n6 1\n4 7\n5 7\n",
"10 3\nX...X..X..\n..X...X..X\n11 7\n7 18\n18 10\n"
] | [
"1\n4\n0\n5\n2\n2\n2\n",
"9\n-1\n3\n"
] | none | [
{
"input": "4 7\n.X..\n...X\n5 1\n1 3\n7 7\n1 4\n6 1\n4 7\n5 7",
"output": "1\n4\n0\n5\n2\n2\n2"
},
{
"input": "10 3\nX...X..X..\n..X...X..X\n11 7\n7 18\n18 10",
"output": "9\n-1\n3"
},
{
"input": "1 1\n.\n.\n1 2",
"output": "1"
},
{
"input": "2 1\n..\n.X\n1 2",
"output":... | 2,000 | 0 | 0 | 31,613 | |
896 | Willem, Chtholly and Seniorious | [
"data structures",
"probabilities"
] | null | null | β Willem...
β What's the matter?
β It seems that there's something wrong with Seniorious...
β I'll have a look...
Seniorious is made by linking special talismans in particular order.
After over 500 years, the carillon is now in bad condition, so Willem decides to examine it thoroughly.
Seniorious has *n* pieces of talisman. Willem puts them in a line, the *i*-th of which is an integer *a**i*.
In order to maintain it, Willem needs to perform *m* operations.
There are four types of operations:
- 1 *l* *r* *x*: For each *i* such that *l*<=β€<=*i*<=β€<=*r*, assign *a**i*<=+<=*x* to *a**i*.- 2 *l* *r* *x*: For each *i* such that *l*<=β€<=*i*<=β€<=*r*, assign *x* to *a**i*.- 3 *l* *r* *x*: Print the *x*-th smallest number in the index range [*l*,<=*r*], i.e. the element at the *x*-th position if all the elements *a**i* such that *l*<=β€<=*i*<=β€<=*r* are taken and sorted into an array of non-decreasing integers. It's guaranteed that 1<=β€<=*x*<=β€<=*r*<=-<=*l*<=+<=1.- 4 *l* *r* *x* *y*: Print the sum of the *x*-th power of *a**i* such that *l*<=β€<=*i*<=β€<=*r*, modulo *y*, i.e. . | The only line contains four integers *n*,<=*m*,<=*seed*,<=*v**max* (1<=β€<=*n*,<=*m*<=β€<=105,<=0<=β€<=*seed*<=<<=109<=+<=7,<=1<=β€<=*vmax*<=β€<=109).
The initial values and operations are generated using following pseudo code:
Here *op* is the type of the operation mentioned in the legend. | For each operation of types 3 or 4, output a line containing the answer. | [
"10 10 7 9\n",
"10 10 9 9\n"
] | [
"2\n1\n0\n3\n",
"1\n1\n3\n3\n"
] | In the first example, the initial array is {8,β9,β7,β2,β3,β1,β5,β6,β4,β8}.
The operations are:
- 2 6 7 9 - 1 3 10 8 - 4 4 6 2 4 - 1 4 5 8 - 2 1 7 1 - 4 7 9 4 4 - 1 2 7 9 - 4 5 8 1 1 - 2 5 7 5 - 4 3 10 8 5 | [
{
"input": "10 10 7 9",
"output": "2\n1\n0\n3"
},
{
"input": "10 10 9 9",
"output": "1\n1\n3\n3"
},
{
"input": "1000 1000 658073485 946088556",
"output": "375432604\n52885108\n732131239\n335583873\n375432604\n582199284\n235058938\n682619432\n62026709\n631048460\n51394660\n25596188\n2... | 0 | 0 | -1 | 31,657 | |
913 | Logical Expression | [
"bitmasks",
"dp",
"shortest paths"
] | null | null | You are given a boolean function of three variables which is defined by its truth table. You need to find an expression of minimum length that equals to this function. The expression may consist of:
- Operation AND ('&', ASCII code 38) - Operation OR ('|', ASCII code 124) - Operation NOT ('!', ASCII code 33) - Variables x, y and z (ASCII codes 120-122) - Parentheses ('(', ASCII code 40, and ')', ASCII code 41)
If more than one expression of minimum length exists, you should find the lexicographically smallest one.
Operations have standard priority. NOT has the highest priority, then AND goes, and OR has the lowest priority. The expression should satisfy the following grammar:
E ::= E '|' T | T
T ::= T '&' F | F
F ::= '!' F | '(' E ')' | 'x' | 'y' | 'z' | The first line contains one integer *n*Β β the number of functions in the input (1<=β€<=*n*<=β€<=10<=000).
The following *n* lines contain descriptions of functions, the *i*-th of them contains a string of length 8 that consists of digits 0 and 1Β β the truth table of the *i*-th function. The digit on position *j* (0<=β€<=*j*<=<<=8) equals to the value of the function in case of , and . | You should output *n* lines, the *i*-th line should contain the expression of minimum length which equals to the *i*-th function. If there is more than one such expression, output the lexicographically smallest of them. Expressions should satisfy the given grammar and shouldn't contain white spaces. | [
"4\n00110011\n00000111\n11110000\n00011111\n"
] | [
"y\n(y|z)&x\n!x\nx|y&z\n"
] | The truth table for the second function:
<img class="tex-graphics" src="https://espresso.codeforces.com/2b70451f45cd74ee2be475affd7c407d7ed6d5fd.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "4\n00110011\n00000111\n11110000\n00011111",
"output": "y\n(y|z)&x\n!x\nx|y&z"
},
{
"input": "1\n11001110",
"output": "!y|!z&x"
},
{
"input": "2\n11001110\n01001001",
"output": "!y|!z&x\n!(!x&!z|x&z|y)|x&y&z"
},
{
"input": "3\n10001001\n10111011\n10111101",
"ou... | 748 | 10,956,800 | -1 | 31,658 | |
935 | Fifa and Fafa | [
"geometry"
] | null | null | Fifa and Fafa are sharing a flat. Fifa loves video games and wants to download a new soccer game. Unfortunately, Fafa heavily uses the internet which consumes the quota. Fifa can access the internet through his Wi-Fi access point. This access point can be accessed within a range of *r* meters (this range can be chosen by Fifa) from its position. Fifa must put the access point inside the flat which has a circular shape of radius *R*. Fifa wants to minimize the area that is not covered by the access point inside the flat without letting Fafa or anyone outside the flat to get access to the internet.
The world is represented as an infinite 2D plane. The flat is centered at (*x*1,<=*y*1) and has radius *R* and Fafa's laptop is located at (*x*2,<=*y*2), not necessarily inside the flat. Find the position and the radius chosen by Fifa for his access point which minimizes the uncovered area. | The single line of the input contains 5 space-separated integers *R*,<=*x*1,<=*y*1,<=*x*2,<=*y*2 (1<=β€<=*R*<=β€<=105, |*x*1|,<=|*y*1|,<=|*x*2|,<=|*y*2|<=β€<=105). | Print three space-separated numbers *x**ap*,<=*y**ap*,<=*r* where (*x**ap*,<=*y**ap*) is the position which Fifa chose for the access point and *r* is the radius of its range.
Your answer will be considered correct if the radius does not differ from optimal more than 10<=-<=6 absolutely or relatively, and also the radius you printed can be changed by no more than 10<=-<=6 (absolutely or relatively) in such a way that all points outside the flat and Fafa's laptop position are outside circle of the access point range. | [
"5 3 3 1 1\n",
"10 5 5 5 15\n"
] | [
"3.7677669529663684 3.7677669529663684 3.914213562373095\n",
"5.0 5.0 10.0\n"
] | none | [
{
"input": "5 3 3 1 1",
"output": "3.7677669529663684 3.7677669529663684 3.914213562373095"
},
{
"input": "10 5 5 5 15",
"output": "5.0 5.0 10.0"
},
{
"input": "5 0 0 0 7",
"output": "0 0 5"
},
{
"input": "10 0 0 0 0",
"output": "5.0 0.0 5.0"
},
{
"input": "100000... | 31 | 0 | 0 | 31,680 | |
772 | Verifying Kingdom | [
"binary search",
"divide and conquer",
"interactive",
"trees"
] | null | null | This is an interactive problem.
The judge has a hidden rooted full binary tree with *n* leaves. A full binary tree is one where every node has either 0 or 2 children. The nodes with 0 children are called the leaves of the tree. Since this is a full binary tree, there are exactly 2*n*<=-<=1 nodes in the tree. The leaves of the judge's tree has labels from 1 to *n*. You would like to reconstruct a tree that is isomorphic to the judge's tree. To do this, you can ask some questions.
A question consists of printing the label of three distinct leaves *a*1,<=*a*2,<=*a*3. Let the depth of a node be the shortest distance from the node to the root of the tree. Let *LCA*(*a*,<=*b*) denote the node with maximum depth that is a common ancestor of the nodes *a* and *b*.
Consider *X*<==<=*LCA*(*a*1,<=*a*2),<=*Y*<==<=*LCA*(*a*2,<=*a*3),<=*Z*<==<=*LCA*(*a*3,<=*a*1). The judge will tell you which one of *X*,<=*Y*,<=*Z* has the maximum depth. Note, this pair is uniquely determined since the tree is a binary tree; there can't be any ties.
More specifically, if *X* (or *Y*, *Z* respectively) maximizes the depth, the judge will respond with the string "X" (or "Y", "Z" respectively).
You may only ask at most 10Β·*n* questions. | The first line of input will contain a single integer *n* (3<=β€<=*n*<=β€<=1<=000)Β β the number of leaves in the tree. | To print the final answer, print out the string "-1" on its own line. Then, the next line should contain 2*n*<=-<=1 integers. The *i*-th integer should be the parent of the *i*-th node, or -1, if it is the root.
Your answer will be judged correct if your output is isomorphic to the judge's tree. In particular, the labels of the leaves do not need to be labeled from 1 to *n*. Here, isomorphic means that there exists a permutation Ο such that node *i* is the parent of node *j* in the judge tree if and only node Ο(*i*) is the parent of node Ο(*j*) in your tree. | [
"5\nX\nZ\nY\nY\nX\n"
] | [
"1 4 2\n1 2 4\n2 4 1\n2 3 5\n2 4 3\n-1\n-1 1 1 2 2 3 3 6 6\n"
] | For the first sample, the judge has the hidden tree:
<img class="tex-graphics" src="https://espresso.codeforces.com/2c9cae7de194cc1bc508ea7243ff4d0d509d34bd.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Here is a more readable format of the interaction: | [] | 77 | 2,867,200 | 0 | 31,704 | |
643 | Bear and Two Paths | [
"constructive algorithms",
"graphs"
] | null | null | Bearland has *n* cities, numbered 1 through *n*. Cities are connected via bidirectional roads. Each road connects two distinct cities. No two roads connect the same pair of cities.
Bear Limak was once in a city *a* and he wanted to go to a city *b*. There was no direct connection so he decided to take a long walk, visiting each city exactly once. Formally:
- There is no road between *a* and *b*. - There exists a sequence (path) of *n* distinct cities *v*1,<=*v*2,<=...,<=*v**n* that *v*1<==<=*a*, *v**n*<==<=*b* and there is a road between *v**i* and *v**i*<=+<=1 for .
On the other day, the similar thing happened. Limak wanted to travel between a city *c* and a city *d*. There is no road between them but there exists a sequence of *n* distinct cities *u*1,<=*u*2,<=...,<=*u**n* that *u*1<==<=*c*, *u**n*<==<=*d* and there is a road between *u**i* and *u**i*<=+<=1 for .
Also, Limak thinks that there are at most *k* roads in Bearland. He wonders whether he remembers everything correctly.
Given *n*, *k* and four distinct cities *a*, *b*, *c*, *d*, can you find possible paths (*v*1,<=...,<=*v**n*) and (*u*1,<=...,<=*u**n*) to satisfy all the given conditions? Find any solution or print -1 if it's impossible. | The first line of the input contains two integers *n* and *k* (4<=β€<=*n*<=β€<=1000, *n*<=-<=1<=β€<=*k*<=β€<=2*n*<=-<=2)Β β the number of cities and the maximum allowed number of roads, respectively.
The second line contains four distinct integers *a*, *b*, *c* and *d* (1<=β€<=*a*,<=*b*,<=*c*,<=*d*<=β€<=*n*). | Print -1 if it's impossible to satisfy all the given conditions. Otherwise, print two lines with paths descriptions. The first of these two lines should contain *n* distinct integers *v*1,<=*v*2,<=...,<=*v**n* where *v*1<==<=*a* and *v**n*<==<=*b*. The second line should contain *n* distinct integers *u*1,<=*u*2,<=...,<=*u**n* where *u*1<==<=*c* and *u**n*<==<=*d*.
Two paths generate at most 2*n*<=-<=2 roads: (*v*1,<=*v*2),<=(*v*2,<=*v*3),<=...,<=(*v**n*<=-<=1,<=*v**n*),<=(*u*1,<=*u*2),<=(*u*2,<=*u*3),<=...,<=(*u**n*<=-<=1,<=*u**n*). Your answer will be considered wrong if contains more than *k* distinct roads or any other condition breaks. Note that (*x*,<=*y*) and (*y*,<=*x*) are the same road. | [
"7 11\n2 4 7 3\n",
"1000 999\n10 20 30 40\n"
] | [
"2 7 1 3 6 5 4\n7 1 5 4 6 2 3\n",
"-1\n"
] | In the first sample test, there should be 7 cities and at most 11 roads. The provided sample solution generates 10 roads, as in the drawing. You can also see a simple path of length *n* between 2 and 4, and a path between 7 and 3. | [
{
"input": "7 11\n2 4 7 3",
"output": "2 7 1 3 6 5 4\n7 1 5 4 6 2 3"
},
{
"input": "1000 999\n10 20 30 40",
"output": "-1"
},
{
"input": "4 4\n1 2 3 4",
"output": "-1"
},
{
"input": "5 6\n5 2 4 1",
"output": "5 4 3 1 2\n4 5 3 2 1"
},
{
"input": "57 88\n54 30 5 43"... | 93 | 2,048,000 | 0 | 31,764 | |
0 | none | [
"none"
] | null | null | Little Artem is given a graph, constructed as follows: start with some *k*-clique, then add new vertices one by one, connecting them to *k* already existing vertices that form a *k*-clique.
Artem wants to count the number of spanning trees in this graph modulo 109<=+<=7. | First line of the input contains two integers *n* and *k* (1<=β€<=*n*<=β€<=10<=000, 1<=β€<=*k*<=β€<=*min*(*n*,<=5))Β β the total size of the graph and the size of the initial clique, respectively.
Next *n*<=-<=*k* lines describe *k*<=+<=1-th, *k*<=+<=2-th, ..., *i*-th, ..., *n*-th vertices by listing *k* distinct vertex indices 1<=β€<=*a**ij*<=<<=*i* it is connected to. It is guaranteed that those vertices form a k-clique. | Output a single integerΒ β the number of spanning trees in the given graph modulo 109<=+<=7. | [
"3 2\n1 2\n",
"4 3\n1 2 3\n"
] | [
"3\n",
"16\n"
] | none | [] | 46 | 0 | 0 | 31,789 | |
377 | Captains Mode | [
"bitmasks",
"dp",
"games"
] | null | null | Kostya is a progamer specializing in the discipline of Dota 2. Valve Corporation, the developer of this game, has recently released a new patch which turned the balance of the game upside down. Kostya, as the captain of the team, realizes that the greatest responsibility lies on him, so he wants to resort to the analysis of innovations patch from the mathematical point of view to choose the best heroes for his team in every game.
A Dota 2 match involves two teams, each of them must choose some heroes that the players of the team are going to play for, and it is forbidden to choose the same hero several times, even in different teams. In large electronic sports competitions where Kostya's team is going to participate, the matches are held in the Captains Mode. In this mode the captains select the heroes by making one of two possible actions in a certain, predetermined order: pick or ban.
- To pick a hero for the team. After the captain picks, the picked hero goes to his team (later one of a team members will play it) and can no longer be selected by any of the teams. - To ban a hero. After the ban the hero is not sent to any of the teams, but it still can no longer be selected by any of the teams.
The team captain may miss a pick or a ban. If he misses a pick, a random hero is added to his team from those that were available at that moment, and if he misses a ban, no hero is banned, as if there was no ban.
Kostya has already identified the strength of all the heroes based on the new patch fixes. Of course, Kostya knows the order of picks and bans. The strength of a team is the sum of the strengths of the team's heroes and both teams that participate in the match seek to maximize the difference in strengths in their favor. Help Kostya determine what team, the first one or the second one, has advantage in the match, and how large the advantage is. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=100)Β β the number of heroes in Dota 2.
The second line contains *n* integers *s*1, *s*2, ..., *s**n* (1<=β€<=*s**i*<=β€<=106)Β β the strengths of all the heroes.
The third line contains a single integer *m* (2<=β€<=*m*<=β€<=*min*(*n*,<=20))Β β the number of actions the captains of the team must perform.
Next *m* lines look like "*action*Β *team*", where *action* is the needed action: a pick (represented as a "p") or a ban (represented as a "b"), and *team* is the number of the team that needs to perform the action (number 1 or 2).
It is guaranteed that each team makes at least one pick. Besides, each team has the same number of picks and the same number of bans. | Print a single integerΒ β the difference between the strength of the first team and the strength of the second team if the captains of both teams will act optimally well. | [
"2\n2 1\n2\np 1\np 2\n",
"6\n6 4 5 4 5 5\n4\nb 2\np 1\nb 1\np 2\n",
"4\n1 2 3 4\n4\np 2\nb 2\np 1\nb 1\n"
] | [
"1\n",
"0\n",
"-2\n"
] | none | [
{
"input": "2\n2 1\n2\np 1\np 2",
"output": "1"
},
{
"input": "6\n6 4 5 4 5 5\n4\nb 2\np 1\nb 1\np 2",
"output": "0"
},
{
"input": "4\n1 2 3 4\n4\np 2\nb 2\np 1\nb 1",
"output": "-2"
},
{
"input": "4\n1 2 3 5\n4\nb 2\np 1\np 2\nb 1",
"output": "1"
},
{
"input": "6... | 61 | 0 | 0 | 31,916 | |
0 | none | [
"none"
] | null | null | Polycarpus has a chessboard of size *n*<=Γ<=*m*, where *k* rooks are placed. Polycarpus hasn't yet invented the rules of the game he will play. However, he has already allocated *q* rectangular areas of special strategic importance on the board, they must be protected well. According to Polycarpus, a rectangular area of ββthe board is well protected if all its vacant squares can be beaten by the rooks that stand on this area. The rooks on the rest of the board do not affect the area's defense. The position of the rooks is fixed and cannot be changed. We remind you that the the rook beats the squares located on the same vertical or horizontal line with it, if there are no other pieces between the square and the rook. Help Polycarpus determine whether all strategically important areas are protected. | The first line contains four integers *n*, *m*, *k* and *q* (1<=β€<=*n*,<=*m*<=β€<=100<=000, 1<=β€<=*k*,<=*q*<=β€<=200<=000) β the sizes of the board, the number of rooks and the number of strategically important sites. We will consider that the cells of the board are numbered by integers from 1 to *n* horizontally and from 1 to *m* vertically. Next *k* lines contain pairs of integers "*x* *y*", describing the positions of the rooks (1<=β€<=*x*<=β€<=*n*, 1<=β€<=*y*<=β€<=*m*). It is guaranteed that all the rooks are in distinct squares. Next *q* lines describe the strategically important areas as groups of four integers "*x*1 *y*1 *x*2 *y*2" (1<=β€<=*x*1<=β€<=*x*2<=β€<=*n*, 1<=β€<=*y*1<=β€<=*y*2<=β€<=*m*). The corresponding rectangle area consists of cells (*x*,<=*y*), for which *x*1<=β€<=*x*<=β€<=*x*2, *y*1<=β€<=*y*<=β€<=*y*2. Strategically important areas can intersect of coincide. | Print *q* lines. For each strategically important site print "YES" if it is well defended and "NO" otherwise. | [
"4 3 3 3\n1 1\n3 2\n2 3\n2 3 2 3\n2 1 3 3\n1 2 2 3\n"
] | [
"YES\nYES\nNO\n"
] | Picture to the sample: <img class="tex-graphics" src="https://espresso.codeforces.com/4b760b396c0058262fe776c85e60c5effd77ec1a.png" style="max-width: 100.0%;max-height: 100.0%;"/> For the last area the answer is "NO", because cell (1,β2) cannot be hit by a rook. | [] | 30 | 0 | 0 | 31,979 | |
625 | Finals in arithmetic | [
"constructive algorithms",
"implementation",
"math"
] | null | null | Vitya is studying in the third grade. During the last math lesson all the pupils wrote on arithmetic quiz. Vitya is a clever boy, so he managed to finish all the tasks pretty fast and Oksana Fillipovna gave him a new one, that is much harder.
Let's denote a flip operation of an integer as follows: number is considered in decimal notation and then reverted. If there are any leading zeroes afterwards, they are thrown away. For example, if we flip 123 the result is the integer 321, but flipping 130 we obtain 31, and by flipping 31 we come to 13.
Oksana Fillipovna picked some number *a* without leading zeroes, and flipped it to get number *a**r*. Then she summed *a* and *a**r*, and told Vitya the resulting value *n*. His goal is to find any valid *a*.
As Oksana Fillipovna picked some small integers as *a* and *a**r*, Vitya managed to find the answer pretty fast and became interested in finding some general algorithm to deal with this problem. Now, he wants you to write the program that for given *n* finds any *a* without leading zeroes, such that *a*<=+<=*a**r*<==<=*n* or determine that such *a* doesn't exist. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=10100<=000). | If there is no such positive integer *a* without leading zeroes that *a*<=+<=*a**r*<==<=*n* then print 0. Otherwise, print any valid *a*. If there are many possible answers, you are allowed to pick any. | [
"4\n",
"11\n",
"5\n",
"33\n"
] | [
"2\n",
"10\n",
"0\n",
"21\n"
] | In the first sample 4β=β2β+β2, *a*β=β2 is the only possibility.
In the second sample 11β=β10β+β1, *a*β=β10Β β the only valid solution. Note, that *a*β=β01 is incorrect, because *a* can't have leading zeroes.
It's easy to check that there is no suitable *a* in the third sample.
In the fourth sample 33β=β30β+β3β=β12β+β21, so there are three possibilities for *a*: *a*β=β30, *a*β=β12, *a*β=β21. Any of these is considered to be correct answer. | [
{
"input": "4",
"output": "2"
},
{
"input": "11",
"output": "10"
},
{
"input": "5",
"output": "0"
},
{
"input": "33",
"output": "21"
},
{
"input": "1",
"output": "0"
},
{
"input": "99",
"output": "54"
},
{
"input": "100",
"output": "0"
... | 62 | 307,200 | 0 | 32,041 | |
234 | Cinema | [
"implementation"
] | null | null | Overall there are *m* actors in Berland. Each actor has a personal identifier β an integer from 1 to *m* (distinct actors have distinct identifiers). Vasya likes to watch Berland movies with Berland actors, and he has *k* favorite actors. He watched the movie trailers for the next month and wrote the following information for every movie: the movie title, the number of actors who starred in it, and the identifiers of these actors. Besides, he managed to copy the movie titles and how many actors starred there, but he didn't manage to write down the identifiers of some actors. Vasya looks at his records and wonders which movies may be his favourite, and which ones may not be. Once Vasya learns the exact cast of all movies, his favorite movies will be determined as follows: a movie becomes favorite movie, if no other movie from Vasya's list has more favorite actors.
Help the boy to determine the following for each movie:
- whether it surely will be his favourite movie;- whether it surely won't be his favourite movie; - can either be favourite or not. | The first line of the input contains two integers *m* and *k* (1<=β€<=*m*<=β€<=100,<=1<=β€<=*k*<=β€<=*m*) β the number of actors in Berland and the number of Vasya's favourite actors.
The second line contains *k* distinct integers *a**i* (1<=β€<=*a**i*<=β€<=*m*) β the identifiers of Vasya's favourite actors.
The third line contains a single integer *n* (1<=β€<=*n*<=β€<=100) β the number of movies in Vasya's list.
Then follow *n* blocks of lines, each block contains a movie's description. The *i*-th movie's description contains three lines:
- the first line contains string *s**i* (*s**i* consists of lowercase English letters and can have the length of from 1 to 10 characters, inclusive) β the movie's title, - the second line contains a non-negative integer *d**i* (1<=β€<=*d**i*<=β€<=*m*) β the number of actors who starred in this movie,- the third line has *d**i* integers *b**i*,<=*j* (0<=β€<=*b**i*,<=*j*<=β€<=*m*) β the identifiers of the actors who star in this movie. If *b**i*,<=*j*<==<=0, than Vasya doesn't remember the identifier of the *j*-th actor. It is guaranteed that the list of actors for a movie doesn't contain the same actors.
All movies have distinct names. The numbers on the lines are separated by single spaces. | Print *n* lines in the output. In the *i*-th line print:
- 0, if the *i*-th movie will surely be the favourite; - 1, if the *i*-th movie won't surely be the favourite; - 2, if the *i*-th movie can either be favourite, or not favourite. | [
"5 3\n1 2 3\n6\nfirstfilm\n3\n0 0 0\nsecondfilm\n4\n0 0 4 5\nthirdfilm\n1\n2\nfourthfilm\n1\n5\nfifthfilm\n1\n4\nsixthfilm\n2\n1 0\n",
"5 3\n1 3 5\n4\njumanji\n3\n0 0 0\ntheeagle\n5\n1 2 3 4 0\nmatrix\n3\n2 4 0\nsourcecode\n2\n2 4\n"
] | [
"2\n2\n1\n1\n1\n2\n",
"2\n0\n1\n1\n"
] | Note to the second sample:
- Movie jumanji can theoretically have from 1 to 3 Vasya's favourite actors. - Movie theeagle has all three favourite actors, as the actor Vasya failed to remember, can only have identifier 5. - Movie matrix can have exactly one favourite actor. - Movie sourcecode doesn't have any favourite actors.
Thus, movie theeagle will surely be favourite, movies matrix and sourcecode won't surely be favourite, and movie jumanji can be either favourite (if it has all three favourite actors), or not favourite. | [
{
"input": "5 3\n1 2 3\n6\nfirstfilm\n3\n0 0 0\nsecondfilm\n4\n0 0 4 5\nthirdfilm\n1\n2\nfourthfilm\n1\n5\nfifthfilm\n1\n4\nsixthfilm\n2\n1 0",
"output": "2\n2\n1\n1\n1\n2"
},
{
"input": "5 3\n1 3 5\n4\njumanji\n3\n0 0 0\ntheeagle\n5\n1 2 3 4 0\nmatrix\n3\n2 4 0\nsourcecode\n2\n2 4",
"output": "... | 92 | 6,963,200 | -1 | 32,114 | |
62 | Inquisition | [
"geometry",
"implementation",
"sortings"
] | C. Inquisition | 3 | 256 | In Medieval times existed the tradition of burning witches at steaks together with their pets, black cats. By the end of the 15-th century the population of black cats ceased to exist. The difficulty of the situation led to creating the EIC - the Emergency Inquisitory Commission.
The resolution #666 says that a white cat is considered black when and only when the perimeter of its black spots exceeds the acceptable norm. But what does the acceptable norm equal to? Every inquisitor will choose it himself depending on the situation. And your task is to find the perimeter of black spots on the cat's fur.
The very same resolution says that the cat's fur is a white square with the length of 105. During the measurement of spots it is customary to put the lower left corner of the fur into the origin of axes (0;0) and the upper right one β to the point with coordinates (105;105). The cats' spots are nondegenerate triangles. The spots can intersect and overlap with each other, but it is guaranteed that each pair of the triangular spots' sides have no more than one common point.
We'll regard the perimeter in this problem as the total length of the boarders where a cat's fur changes color. | The first input line contains a single integer *n* (0<=β€<=*n*<=β€<=100). It is the number of spots on the cat's fur. The *i*-th of the last *n* lines contains 6 integers: *x*1*i*, *y*1*i*, *x*2*i*, *y*2*i*, *x*3*i*, *y*3*i*. They are the coordinates of the *i*-th triangular spot (0<=<<=*x**ji*,<=*y**ji*<=<<=105). | Print a single number, the answer to the problem, perimeter of the union of triangles. Your answer should differ from the correct one in no more than 10<=-<=6. | [
"1\n1 1 2 1 1 2\n",
"3\n3 3 10 3 3 10\n1 1 9 4 5 6\n2 2 11 7 6 11\n"
] | [
"3.4142135624\n",
"37.7044021497\n"
] | none | [] | 92 | 0 | 0 | 32,133 |
383 | Antimatter | [
"dp"
] | null | null | Iahub accidentally discovered a secret lab. He found there *n* devices ordered in a line, numbered from 1 to *n* from left to right. Each device *i* (1<=β€<=*i*<=β€<=*n*) can create either *a**i* units of matter or *a**i* units of antimatter.
Iahub wants to choose some contiguous subarray of devices in the lab, specify the production mode for each of them (produce matter or antimatter) and finally take a photo of it. However he will be successful only if the amounts of matter and antimatter produced in the selected subarray will be the same (otherwise there would be overflowing matter or antimatter in the photo).
You are requested to compute the number of different ways Iahub can successful take a photo. A photo is different than another if it represents another subarray, or if at least one device of the subarray is set to produce matter in one of the photos and antimatter in the other one. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=1000). The second line contains *n* integers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=1000).
The sum *a*1<=+<=*a*2<=+<=...<=+<=*a**n* will be less than or equal to 10000. | Output a single integer, the number of ways Iahub can take a photo, modulo 1000000007 (109<=+<=7). | [
"4\n1 1 1 1\n"
] | [
"12\n"
] | The possible photos are [1+, 2-], [1-, 2+], [2+, 3-], [2-, 3+], [3+, 4-], [3-, 4+], [1+, 2+, 3-, 4-], [1+, 2-, 3+, 4-], [1+, 2-, 3-, 4+], [1-, 2+, 3+, 4-], [1-, 2+, 3-, 4+] and [1-, 2-, 3+, 4+], where "*i*+" means that the *i*-th element produces matter, and "*i*-" means that the *i*-th element produces antimatter. | [
{
"input": "4\n1 1 1 1",
"output": "12"
},
{
"input": "10\n16 9 9 11 10 12 9 6 10 8",
"output": "86"
},
{
"input": "50\n2 1 5 2 1 3 1 2 3 2 1 1 5 2 2 2 3 2 1 2 2 2 3 3 1 3 1 1 2 2 2 2 1 2 3 1 2 4 1 1 1 3 2 1 1 1 3 2 1 3",
"output": "115119382"
},
{
"input": "100\n8 3 3 7 3 6 ... | 1,000 | 13,619,200 | 0 | 32,204 | |
109 | Lucky Probability | [
"brute force",
"probabilities"
] | B. Lucky Probability | 2 | 256 | Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Petya and his friend Vasya play an interesting game. Petya randomly chooses an integer *p* from the interval [*p**l*,<=*p**r*] and Vasya chooses an integer *v* from the interval [*v**l*,<=*v**r*] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [*min*(*v*,<=*p*),<=*max*(*v*,<=*p*)] contains exactly *k* lucky numbers. | The single line contains five integers *p**l*, *p**r*, *v**l*, *v**r* and *k* (1<=β€<=*p**l*<=β€<=*p**r*<=β€<=109,<=1<=β€<=*v**l*<=β€<=*v**r*<=β€<=109,<=1<=β€<=*k*<=β€<=1000). | On the single line print the result with an absolute error of no more than 10<=-<=9. | [
"1 10 1 10 2\n",
"5 6 8 10 1\n"
] | [
"0.320000000000\n",
"1.000000000000\n"
] | Consider that [*a*,β*b*] denotes an interval of integers; this interval includes the boundaries. That is, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/18b4a6012d95ad18891561410f0314497a578d63.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In first case there are 32 suitable pairs: (1,β7),β(1,β8),β(1,β9),β(1,β10),β(2,β7),β(2,β8),β(2,β9),β(2,β10),β(3,β7),β(3,β8),β(3,β9),β(3,β10),β(4,β7),β(4,β8),β(4,β9),β(4,β10),β(7,β1),β(7,β2),β(7,β3),β(7,β4),β(8,β1),β(8,β2),β(8,β3),β(8,β4),β(9,β1),β(9,β2),β(9,β3),β(9,β4),β(10,β1),β(10,β2),β(10,β3),β(10,β4). Total number of possible pairs is 10Β·10β=β100, so answer is 32β/β100.
In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number. | [
{
"input": "1 10 1 10 2",
"output": "0.320000000000"
},
{
"input": "5 6 8 10 1",
"output": "1.000000000000"
},
{
"input": "1 20 100 120 5",
"output": "0.150000000000"
},
{
"input": "1 10 1 10 3",
"output": "0.000000000000"
},
{
"input": "1 100 1 100 2",
"outpu... | 92 | 0 | 0 | 32,240 |
621 | Rat Kwesh and Cheese | [
"brute force",
"constructive algorithms",
"math"
] | null | null | Wet Shark asked Rat Kwesh to generate three positive real numbers *x*, *y* and *z*, from 0.1 to 200.0, inclusive. Wet Krash wants to impress Wet Shark, so all generated numbers will have exactly one digit after the decimal point.
Wet Shark knows Rat Kwesh will want a lot of cheese. So he will give the Rat an opportunity to earn a lot of cheese. He will hand the three numbers *x*, *y* and *z* to Rat Kwesh, and Rat Kwesh will pick one of the these twelve options:
1. *a*1<==<=*x**y**z*; 1. *a*2<==<=*x**z**y*; 1. *a*3<==<=(*x**y*)*z*; 1. *a*4<==<=(*x**z*)*y*; 1. *a*5<==<=*y**x**z*; 1. *a*6<==<=*y**z**x*; 1. *a*7<==<=(*y**x*)*z*; 1. *a*8<==<=(*y**z*)*x*; 1. *a*9<==<=*z**x**y*; 1. *a*10<==<=*z**y**x*; 1. *a*11<==<=(*z**x*)*y*; 1. *a*12<==<=(*z**y*)*x*.
Let *m* be the maximum of all the *a**i*, and *c* be the smallest index (from 1 to 12) such that *a**c*<==<=*m*. Rat's goal is to find that *c*, and he asks you to help him. Rat Kwesh wants to see how much cheese he gets, so he you will have to print the expression corresponding to that *a**c*. | The only line of the input contains three space-separated real numbers *x*, *y* and *z* (0.1<=β€<=*x*,<=*y*,<=*z*<=β€<=200.0). Each of *x*, *y* and *z* is given with exactly one digit after the decimal point. | Find the maximum value of expression among *x**y**z*, *x**z**y*, (*x**y*)*z*, (*x**z*)*y*, *y**x**z*, *y**z**x*, (*y**x*)*z*, (*y**z*)*x*, *z**x**y*, *z**y**x*, (*z**x*)*y*, (*z**y*)*x* and print the corresponding expression. If there are many maximums, print the one that comes first in the list.
*x**y**z* should be outputted as x^y^z (without brackets), and (*x**y*)*z* should be outputted as (x^y)^z (quotes for clarity). | [
"1.1 3.4 2.5\n",
"2.0 2.0 2.0\n",
"1.9 1.8 1.7\n"
] | [
"z^y^x\n",
"x^y^z\n",
"(x^y)^z\n"
] | none | [
{
"input": "1.1 3.4 2.5",
"output": "z^y^x"
},
{
"input": "2.0 2.0 2.0",
"output": "x^y^z"
},
{
"input": "1.9 1.8 1.7",
"output": "(x^y)^z"
},
{
"input": "2.0 2.1 2.2",
"output": "x^z^y"
},
{
"input": "1.5 1.7 2.5",
"output": "(z^x)^y"
},
{
"input": "1... | 0 | 0 | -1 | 32,253 | |
452 | Three strings | [
"data structures",
"dsu",
"string suffix structures",
"strings"
] | null | null | You are given three strings (*s*1,<=*s*2,<=*s*3). For each integer *l* (1<=β€<=*l*<=β€<=*min*(|*s*1|,<=|*s*2|,<=|*s*3|) you need to find how many triples (*i*1,<=*i*2,<=*i*3) exist such that three strings *s**k*[*i**k*... *i**k*<=+<=*l*<=-<=1] (*k*<==<=1,<=2,<=3) are pairwise equal. Print all found numbers modulo 1000000007Β (109<=+<=7).
See notes if you are not sure about some of the denotions used in the statement. | First three lines contain three non-empty input strings. The sum of lengths of all strings is no more than 3Β·105. All strings consist only of lowercase English letters. | You need to output *min*(|*s*1|,<=|*s*2|,<=|*s*3|) numbers separated by spaces β answers for the problem modulo 1000000007Β (109<=+<=7). | [
"abc\nbc\ncbc\n",
"abacaba\nabac\nabcd\n"
] | [
"3 1 \n",
"11 2 0 0 \n"
] | Consider a string *t*β=β*t*<sub class="lower-index">1</sub>*t*<sub class="lower-index">2</sub>... *t*<sub class="lower-index">|*t*|</sub>, where *t*<sub class="lower-index">*i*</sub> denotes the *i*-th character of the string, and |*t*| denotes the length of the string.
Then *t*[*i*... *j*] (1ββ€β*i*ββ€β*j*ββ€β|*t*|) represents the string *t*<sub class="lower-index">*i*</sub>*t*<sub class="lower-index">*i*β+β1</sub>... *t*<sub class="lower-index">*j*</sub> (substring of *t* from position *i* to position *j* inclusive). | [] | 46 | 0 | 0 | 32,256 | |
1,000 | Yet Another Problem On a Subsequence | [
"combinatorics",
"dp"
] | null | null | The sequence of integers $a_1, a_2, \dots, a_k$ is called a good array if $a_1 = k - 1$ and $a_1 > 0$. For example, the sequences $[3, -1, 44, 0], [1, -99]$ are good arrays, and the sequences $[3, 7, 8], [2, 5, 4, 1], [0]$ β are not.
A sequence of integers is called good if it can be divided into a positive number of good arrays. Each good array should be a subsegment of sequence and each element of the sequence should belong to exactly one array. For example, the sequences $[2, -3, 0, 1, 4]$, $[1, 2, 3, -3, -9, 4]$ are good, and the sequences $[2, -3, 0, 1]$, $[1, 2, 3, -3 -9, 4, 1]$ β are not.
For a given sequence of numbers, count the number of its subsequences that are good sequences, and print the number of such subsequences modulo 998244353. | The first line contains the number $n~(1 \le n \le 10^3)$ β the length of the initial sequence. The following line contains $n$ integers $a_1, a_2, \dots, a_n~(-10^9 \le a_i \le 10^9)$ β the sequence itself. | In the single line output one integer β the number of subsequences of the original sequence that are good sequences, taken modulo 998244353. | [
"3\n2 1 1\n",
"4\n1 1 1 1\n"
] | [
"2\n",
"7\n"
] | In the first test case, two good subsequences β $[a_1, a_2, a_3]$ and $[a_2, a_3]$.
In the second test case, seven good subsequences β $[a_1, a_2, a_3, a_4], [a_1, a_2], [a_1, a_3], [a_1, a_4], [a_2, a_3], [a_2, a_4]$ and $[a_3, a_4]$. | [
{
"input": "3\n2 1 1",
"output": "2"
},
{
"input": "4\n1 1 1 1",
"output": "7"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
}
] | 93 | 3,584,000 | 0 | 32,272 | |
741 | Arpa's loud Owf and Mehrdad's evil plan | [
"dfs and similar",
"math"
] | null | null | As you have noticed, there are lovely girls in Arpaβs land.
People in Arpa's land are numbered from 1 to *n*. Everyone has exactly one crush, *i*-th person's crush is person with the number *crush**i*.
Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows.
The game consists of rounds. Assume person *x* wants to start a round, he calls *crush**x* and says: "Oww...wwf" (the letter w is repeated *t* times) and cuts off the phone immediately. If *t*<=><=1 then *crush**x* calls *crush**crush**x* and says: "Oww...wwf" (the letter w is repeated *t*<=-<=1 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (*t*<==<=1). This person is called the Joon-Joon of the round. There can't be two rounds at the same time.
Mehrdad has an evil plan to make the game more funny, he wants to find smallest *t* (*t*<=β₯<=1) such that for each person *x*, if *x* starts some round and *y* becomes the Joon-Joon of the round, then by starting from *y*, *x* would become the Joon-Joon of the round. Find such *t* for Mehrdad if it's possible.
Some strange fact in Arpa's land is that someone can be himself's crush (i.e. *crush**i*<==<=*i*). | The first line of input contains integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of people in Arpa's land.
The second line contains *n* integers, *i*-th of them is *crush**i* (1<=β€<=*crush**i*<=β€<=*n*)Β β the number of *i*-th person's crush. | If there is no *t* satisfying the condition, print -1. Otherwise print such smallest *t*. | [
"4\n2 3 1 4\n",
"4\n4 4 4 4\n",
"4\n2 1 4 3\n"
] | [
"3\n",
"-1\n",
"1\n"
] | In the first sample suppose *t*β=β3.
If the first person starts some round:
The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if *x* is 1.
The process is similar for the second and the third person.
If the fourth person starts some round:
The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when *x* is 4.
In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa. | [
{
"input": "4\n2 3 1 4",
"output": "3"
},
{
"input": "4\n4 4 4 4",
"output": "-1"
},
{
"input": "4\n2 1 4 3",
"output": "1"
},
{
"input": "5\n2 4 3 1 2",
"output": "-1"
},
{
"input": "5\n2 2 4 4 5",
"output": "-1"
},
{
"input": "5\n2 4 5 4 2",
"out... | 93 | 0 | 3 | 32,308 | |
120 | Brevity is Soul of Wit | [
"graph matchings"
] | null | null | As we communicate, we learn much new information. However, the process of communication takes too much time. It becomes clear if we look at the words we use in our everyday speech.
We can list many simple words consisting of many letters: "information", "technologies", "university", "construction", "conservatoire", "refrigerator", "stopwatch", "windowsill", "electricity", "government" and so on. Of course, we can continue listing those words ad infinitum.
Fortunately, the solution for that problem has been found. To make our speech clear and brief, we should replace the initial words with those that resemble them but are much shorter. This idea hasn't been brought into life yet, that's why you are chosen to improve the situation.
Let's consider the following formal model of transforming words: we shall assume that one can use *n* words in a chat. For each words we shall introduce a notion of its shorter variant. We shall define shorter variant of an arbitrary word *s* as such word *t*, that meets the following conditions:
- it occurs in *s* as a subsequence, - its length ranges from one to four characters.
In other words, the word *t* consists at least of one and at most of four characters that occur in the same order in the word *s*. Note that those characters do not necessarily follow in *s* immediately one after another. You are allowed not to shorten the initial word if its length does not exceed four characters.
You are given a list of *n* different words. Your task is to find a set of their shortened variants. The shortened variants of all words from the list should be different. | The first line of the input file contains the only integer *n* (1<=β€<=*n*<=β€<=200). Then *n* lines contain a set of different non-empty words that consist of lowercase Latin letters. The length of each word does not exceed 10 characters. | If the solution exists, print in the output file exactly *n* lines, where the *i*-th line represents the shortened variant of the *i*-th word from the initial set. If there are several variants to solve the problem, print any of them. If there is no solution, print -1. | [
"6\nprivet\nspasibo\ncodeforces\njava\nmarmelad\nnormalno\n",
"5\naaa\naa\na\naaaa\naaaaa\n"
] | [
"pret\nsps\ncdfs\njava\nmama\nnorm\n",
"-1\n"
] | none | [
{
"input": "6\nprivet\nspasibo\ncodeforces\njava\nmarmelad\nnormalno",
"output": "pr\np\ne\nv\nre\nr"
},
{
"input": "5\naaa\naa\na\naaaa\naaaaa",
"output": "-1"
},
{
"input": "26\naaaaa\naaaab\naaaac\naaaad\naaaae\naaaaf\naaaag\naaaah\naaaai\naaaaj\naaaak\naaaal\naaaam\naaaan\naaaao\naaa... | 62 | 0 | 0 | 32,349 | |
335 | Banana | [
"binary search",
"constructive algorithms",
"greedy"
] | null | null | Piegirl is buying stickers for a project. Stickers come on sheets, and each sheet of stickers contains exactly *n* stickers. Each sticker has exactly one character printed on it, so a sheet of stickers can be described by a string of length *n*. Piegirl wants to create a string *s* using stickers. She may buy as many sheets of stickers as she wants, and may specify any string of length *n* for the sheets, but all the sheets must be identical, so the string is the same for all sheets. Once she attains the sheets of stickers, she will take some of the stickers from the sheets and arrange (in any order) them to form *s*. Determine the minimum number of sheets she has to buy, and provide a string describing a possible sheet of stickers she should buy. | The first line contains string *s* (1<=β€<=|*s*|<=β€<=1000), consisting of lowercase English characters only. The second line contains an integer *n* (1<=β€<=*n*<=β€<=1000). | On the first line, print the minimum number of sheets Piegirl has to buy. On the second line, print a string consisting of *n* lower case English characters. This string should describe a sheet of stickers that Piegirl can buy in order to minimize the number of sheets. If Piegirl cannot possibly form the string *s*, print instead a single line with the number -1. | [
"banana\n4\n",
"banana\n3\n",
"banana\n2\n"
] | [
"2\nbaan\n",
"3\nnab\n",
"-1\n"
] | In the second example, Piegirl can order 3 sheets of stickers with the characters "nab". She can take characters "nab" from the first sheet, "na" from the second, and "a" from the third, and arrange them to from "banana". | [
{
"input": "banana\n4",
"output": "2\nbaan"
},
{
"input": "banana\n3",
"output": "3\nnab"
},
{
"input": "banana\n2",
"output": "-1"
},
{
"input": "p\n1000",
"output": "1\npaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... | 92 | 0 | 0 | 32,424 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.