contestId
int64
0
1.01k
name
stringlengths
2
58
tags
sequencelengths
0
11
title
stringclasses
523 values
time-limit
stringclasses
8 values
memory-limit
stringclasses
8 values
problem-description
stringlengths
0
7.15k
input-specification
stringlengths
0
2.05k
output-specification
stringlengths
0
1.5k
demo-input
sequencelengths
0
7
demo-output
sequencelengths
0
7
note
stringlengths
0
5.24k
test_cases
listlengths
0
402
timeConsumedMillis
int64
0
8k
memoryConsumedBytes
int64
0
537M
score
float64
-1
3.99
__index_level_0__
int64
0
621k
3
Least Cost Bracket Sequence
[ "greedy" ]
D. Least Cost Bracket Sequence
1
64
This is yet another problem on regular bracket sequences. A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence. For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest.
The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5·104. Then there follow *m* lines, where *m* is the number of characters "?" in the pattern. Each line contains two integer numbers *a**i* and *b**i* (1<=≤<=*a**i*,<=<=*b**i*<=≤<=106), where *a**i* is the cost of replacing the *i*-th character "?" with an opening bracket, and *b**i* — with a closing one.
Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second. Print -1, if there is no answer. If the answer is not unique, print any of them.
[ "(??)\n1 2\n2 8\n" ]
[ "4\n()()\n" ]
none
[ { "input": "(??)\n1 2\n2 8", "output": "4\n()()" }, { "input": "??\n1 1\n1 1", "output": "2\n()" }, { "input": "(???\n1 1\n1 1\n1 1", "output": "3\n(())" }, { "input": "(??)\n2 1\n1 1", "output": "2\n()()" }, { "input": "(???)?\n3 3\n3 1\n3 3\n2 3", "output": "10\n(()())" }, { "input": "((????\n3 2\n3 2\n1 1\n2 3", "output": "8\n(())()" }, { "input": "???())\n2 4\n3 3\n4 1", "output": "6\n(()())" }, { "input": "((????\n3 5\n4 1\n2 2\n1 5", "output": "11\n((()))" }, { "input": "?(?)(???\n2 3\n2 2\n3 2\n3 1\n3 1", "output": "8\n((()()))" }, { "input": "(??????)\n1 1\n3 3\n3 3\n3 2\n1 3\n3 3", "output": "13\n((())())" }, { "input": "?????)??\n2 3\n2 1\n1 3\n5 1\n3 3\n1 3\n3 2", "output": "11\n()()()()" }, { "input": "?)???(??\n1 4\n3 4\n2 4\n2 5\n3 3\n3 1", "output": "14\n()()(())" }, { "input": "???(??))\n2 1\n2 1\n2 1\n1 2\n2 1", "output": "7\n(()(()))" }, { "input": "??(()??)\n3 2\n3 3\n1 3\n2 2", "output": "9\n()(()())" }, { "input": "????(???\n2 2\n1 3\n1 3\n3 3\n4 1\n4 4\n2 4", "output": "16\n((()()))" }, { "input": "?(??????\n1 5\n2 4\n4 4\n4 3\n4 5\n5 4\n2 3", "output": "21\n((())())" }, { "input": "???????)\n6 3\n5 3\n4 1\n1 4\n4 1\n2 6\n4 3", "output": "19\n(()()())" }, { "input": "??????)?\n2 2\n4 2\n3 5\n3 2\n7 4\n6 2\n1 6", "output": "24\n(((())))" }, { "input": "?((?)?)?\n1 2\n4 2\n1 3\n1 2", "output": "6\n((())())" }, { "input": "??(????)\n3 2\n1 4\n4 4\n2 3\n2 3\n2 4", "output": "16\n((()))()" }, { "input": "???(?)??(??)?)(?(?????????(?()????)(????(?)????)???)??))(?(?????????))???(??)?????))???????(????????\n9 10\n6 3\n8 2\n9 10\n9 3\n6 2\n8 5\n6 7\n2 6\n7 8\n6 10\n1 7\n1 7\n10 7\n10 7\n8 4\n5 9\n9 3\n3 10\n1 10\n8 2\n8 8\n4 8\n6 6\n4 10\n4 5\n5 2\n5 6\n7 7\n7 3\n10 1\n1 4\n5 10\n3 2\n2 8\n8 9\n6 5\n8 6\n3 4\n8 6\n8 5\n7 7\n10 9\n5 5\n2 1\n2 7\n2 3\n5 10\n9 7\n1 9\n10 9\n4 5\n8 2\n2 5\n6 7\n3 6\n4 2\n2 5\n3 9\n4 4\n6 3\n4 9\n3 1\n5 7\n8 7\n6 9\n5 3\n6 4\n8 3\n5 8\n8 4\n7 6\n1 4", "output": "309\n(()(()))()()()(((((()))()(((())((()((()((()))(())(()))))((())))))((()))()(())((()())())()()(()))()))" }, { "input": "(?(((???))(??)?)?))))(?)????(()()???(?)????(??(??????)()(????(?)))))??(???(??)?(??)????????(????(?()\n39 78\n1 83\n2 35\n28 89\n53 53\n96 67\n16 46\n43 28\n25 73\n8 97\n57 41\n15 25\n47 49\n23 18\n97 77\n38 33\n68 80\n38 98\n62 8\n61 79\n84 50\n71 48\n12 16\n97 95\n16 70\n72 58\n55 85\n88 42\n49 56\n39 63\n51 100\n41 15\n97 17\n71 63\n21 44\n1 41\n22 14\n42 65\n88 33\n57 95\n57 28\n59 8\n56 42\n18 99\n43 6\n75 93\n34 23\n62 57\n62 71\n67 92\n91 60\n49 58\n97 14\n75 68\n20 9\n55 98\n12 3", "output": "2140\n(((((((())(())())))))(()()(((()())))(()()()()(((()()()()((())())))))((()()(()))()())())(()(())))()()" }, { "input": "(())()", "output": "0\n(())()" }, { "input": "?(?(??\n1 1\n2 2\n1 1\n1 1", "output": "5\n(()())" }, { "input": "(????(\n1 1\n2 1\n2 1\n3 3", "output": "-1" }, { "input": "(?(???\n2 3\n1 1\n3 3\n1 4", "output": "10\n((()))" }, { "input": "))))))", "output": "-1" }, { "input": ")?)??)\n4 4\n3 5\n3 6", "output": "-1" }, { "input": "((((((", "output": "-1" }, { "input": "((((((", "output": "-1" }, { "input": "()()()", "output": "0\n()()()" }, { "input": "????((\n7 6\n1 10\n9 8\n4 4", "output": "-1" }, { "input": "))))))", "output": "-1" }, { "input": "))))))", "output": "-1" }, { "input": "((((((", "output": "-1" }, { "input": "((()))", "output": "0\n((()))" }, { "input": "?))?))\n9 13\n8 11", "output": "-1" }, { "input": "))))))", "output": "-1" }, { "input": "?(?)?)\n6 14\n8 6\n4 3", "output": "16\n(())()" }, { "input": "?(?(((\n8 7\n17 15", "output": "-1" }, { "input": "))))))", "output": "-1" } ]
124
0
0
17,575
912
Fishes
[ "data structures", "graphs", "greedy", "probabilities", "shortest paths" ]
null
null
While Grisha was celebrating New Year with Ded Moroz, Misha gifted Sasha a small rectangular pond of size *n*<=×<=*m*, divided into cells of size 1<=×<=1, inhabited by tiny evil fishes (no more than one fish per cell, otherwise they'll strife!). The gift bundle also includes a square scoop of size *r*<=×<=*r*, designed for fishing. If the lower-left corner of the scoop-net is located at cell (*x*,<=*y*), all fishes inside the square (*x*,<=*y*)...(*x*<=+<=*r*<=-<=1,<=*y*<=+<=*r*<=-<=1) get caught. Note that the scoop-net should lie completely inside the pond when used. Unfortunately, Sasha is not that skilled in fishing and hence throws the scoop randomly. In order to not frustrate Sasha, Misha decided to release *k* fishes into the empty pond in such a way that the expected value of the number of caught fishes is as high as possible. Help Misha! In other words, put *k* fishes in the pond into distinct cells in such a way that when the scoop-net is placed into a random position among (*n*<=-<=*r*<=+<=1)·(*m*<=-<=*r*<=+<=1) possible positions, the average number of caught fishes is as high as possible.
The only line contains four integers *n*,<=*m*,<=*r*,<=*k* (1<=≤<=*n*,<=*m*<=≤<=105, 1<=≤<=*r*<=≤<=*min*(*n*,<=*m*), 1<=≤<=*k*<=≤<=*min*(*n*·*m*,<=105)).
Print a single number — the maximum possible expected number of caught fishes. You answer is considered correct, is its absolute or relative error does not exceed 10<=-<=9. Namely, let your answer be *a*, and the jury's answer be *b*. Your answer is considered correct, if .
[ "3 3 2 3\n", "12 17 9 40\n" ]
[ "2.0000000000\n", "32.8333333333\n" ]
In the first example you can put the fishes in cells (2, 1), (2, 2), (2, 3). In this case, for any of four possible positions of the scoop-net (highlighted with light green), the number of fishes inside is equal to two, and so is the expected value.
[ { "input": "3 3 2 3", "output": "2.0000000000" }, { "input": "12 17 9 40", "output": "32.8333333333" }, { "input": "1 1 1 1", "output": "1.0000000000" }, { "input": "10 10 5 100", "output": "25.0000000000" }, { "input": "7 1 1 4", "output": "0.5714285714" }, { "input": "1000 1000 59 100000", "output": "392.2854657164" }, { "input": "100000 100000 1000 100000", "output": "10.2028343872" }, { "input": "100000 100000 100000 100000", "output": "100000.0000000000" }, { "input": "3 100000 2 88700", "output": "1.7740177402" }, { "input": "200 100000 55 100000", "output": "20.7303724833" }, { "input": "74 2 1 36", "output": "0.2432432432" }, { "input": "2 59 1 112", "output": "0.9491525424" }, { "input": "96 33 24 928", "output": "299.8356164384" }, { "input": "20 78 8 997", "output": "55.2026002167" }, { "input": "44 22 13 515", "output": "139.7312500000" }, { "input": "68 66 33 2353", "output": "930.8962418301" }, { "input": "96 7 4 534", "output": "14.6129032258" }, { "input": "20 59 2 88", "output": "0.3194192377" }, { "input": "45 3 1 35", "output": "0.2592592593" }, { "input": "69 48 18 167", "output": "33.5657568238" }, { "input": "34994 5189 2572 83748", "output": "6526.6982502848" }, { "input": "51122 86737 45712 45929", "output": "45929.0000000000" }, { "input": "1308 96219 150 13599", "output": "2.7480097244" }, { "input": "84732 45064 24231 99973", "output": "40039.1022280255" }, { "input": "860 93908 193 29450", "output": "17.5230504355" }, { "input": "51580 42753 1589 91632", "output": "112.4258885780" }, { "input": "67708 58893 32854 21110", "output": "19898.0903744083" }, { "input": "51133 7737 2779 83291", "output": "2682.4996497742" }, { "input": "67261 65094 36712 36961", "output": "36961.0000000000" }, { "input": "17981 81234 438 66438", "output": "8.9916815389" }, { "input": "86852 96025 86852 82059", "output": "82059.0000000000" }, { "input": "96877 86600 86600 94110", "output": "94110.0000000000" }, { "input": "93240 88881 88881 94245", "output": "94245.0000000000" }, { "input": "90706 97197 90706 96593", "output": "96593.0000000000" }, { "input": "93289 95214 93289 96084", "output": "96084.0000000000" }, { "input": "90755 85790 85790 98432", "output": "98432.0000000000" }, { "input": "80779 83807 80779 97924", "output": "97924.0000000000" }, { "input": "98246 89266 89266 80270", "output": "80270.0000000000" }, { "input": "80828 99843 80828 99763", "output": "99763.0000000000" }, { "input": "98295 88157 88157 82110", "output": "82110.0000000000" }, { "input": "52329 55202 45142 8532", "output": "8532.0000000000" }, { "input": "68457 4046 983 38009", "output": "177.6486146644" }, { "input": "18644 46233 17019 62575", "output": "36452.6416224542" }, { "input": "2068 62373 1084 92053", "output": "1628.0869962473" }, { "input": "18196 43921 15918 54235", "output": "30828.1934723611" }, { "input": "68916 60062 7636 83712", "output": "1519.2830994297" }, { "input": "85044 8906 4115 45894", "output": "2003.8686025940" }, { "input": "68468 33559 15324 99563", "output": "24124.0525512989" }, { "input": "84596 49699 46033 61745", "output": "61745.0000000000" }, { "input": "35316 31247 27829 91223", "output": "91223.0000000000" }, { "input": "51444 47388 21532 20700", "output": "12407.9770445558" }, { "input": "67572 96232 61366 50178", "output": "50178.0000000000" }, { "input": "17759 38418 4313 7448", "output": "302.0942031080" }, { "input": "1183 87263 148 4221", "output": "1.0244272005" }, { "input": "84607 36107 29486 33699", "output": "18026.3545226951" }, { "input": "68031 52248 51042 20073", "output": "20073.0000000000" }, { "input": "84159 1092 683 49550", "output": "405.4128682152" }, { "input": "67583 49936 46141 11732", "output": "11732.0000000000" }, { "input": "51007 74589 14733 41209", "output": "4119.5718891113" }, { "input": "34431 23433 19371 27583", "output": "27583.0000000000" }, { "input": "52 45 38 49", "output": "49.0000000000" }, { "input": "80 89 32 26", "output": "9.3680506685" }, { "input": "74 63 30 92", "output": "53.8823529412" }, { "input": "2 20 1 14", "output": "0.3500000000" }, { "input": "22 60 13 48", "output": "13.0000000000" }, { "input": "50 5 4 29", "output": "2.4680851064" }, { "input": "74 49 48 99", "output": "99.0000000000" }, { "input": "2 89 2 80", "output": "1.8181818182" }, { "input": "22 38 9 58", "output": "11.1857142857" }, { "input": "50 86 11 36", "output": "1.4328947368" }, { "input": "98155 95063 95062 98875", "output": "98875.0000000000" }, { "input": "95606 98563 95342 99783", "output": "99783.0000000000" }, { "input": "95468 97642 95176 95192", "output": "95192.0000000000" }, { "input": "96394 96141 96028 96100", "output": "96100.0000000000" }, { "input": "100000 1 1 99999", "output": "0.9999900000" }, { "input": "100000 100000 1 100000", "output": "0.0000100000" }, { "input": "100000 100000 100000 1", "output": "1.0000000000" }, { "input": "88888 99999 77777 1", "output": "1.0000000000" }, { "input": "77777 99999 77777 7", "output": "7.0000000000" }, { "input": "100000 100000 1 1", "output": "0.0000000001" }, { "input": "3689 2691 1885 47808", "output": "47808.0000000000" }, { "input": "3907 4563 2248 99346", "output": "95853.0468547766" }, { "input": "1785 3525 1785 82536", "output": "82534.7300402068" }, { "input": "4761 2433 2433 46586", "output": "46586.0000000000" }, { "input": "3106 2359 1558 16919", "output": "16897.4346155270" }, { "input": "3291 1679 1679 70630", "output": "70630.0000000000" }, { "input": "6439 5463 3240 91287", "output": "91097.0460375450" }, { "input": "10000 10000 3000 100000", "output": "18362.1002496817" }, { "input": "7159 6332 3563 82463", "output": "81427.6340771341" }, { "input": "8402 6135 4222 85384", "output": "85384.0000000000" }, { "input": "8946 8108 4509 80203", "output": "80188.7715868009" }, { "input": "9199 8137 4561 82660", "output": "81268.3728190748" }, { "input": "8208 8895 4508 97736", "output": "97736.0000000000" }, { "input": "9513 11191 5633 90250", "output": "90250.0000000000" }, { "input": "11562 20387 10218 95794", "output": "95794.0000000000" }, { "input": "80000 80000 40000 100000", "output": "99625.0947119987" }, { "input": "7 100 4 12", "output": "0.4948453608" } ]
217
6,348,800
3
17,592
71
Round Table Knights
[ "dp", "math", "number theory" ]
C. Round Table Knights
0
256
There are *n* knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood. Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular polygon can be found. On all vertices of the polygon knights in a good mood should be located. Otherwise, the next month will bring misfortunes. A convex polygon is regular if all its sides have same length and all his angles are equal. In this problem we consider only regular polygons with at least 3 vertices, i. e. only nondegenerated. On a picture below some examples of such polygons are present. Green points mean knights in a good mood. Red points mean ones in a bad mood. King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
The first line contains number *n*, which is the number of knights at the round table (3<=≤<=*n*<=≤<=105). The second line contains space-separated moods of all the *n* knights in the order of passing them around the table. "1" means that the knight is in a good mood an "0" means that he is in a bad mood.
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
[ "3\n1 1 1\n", "6\n1 0 1 1 1 0\n", "6\n1 0 0 1 0 1\n" ]
[ "YES", "YES", "NO" ]
none
[ { "input": "3\n1 1 1", "output": "YES" }, { "input": "6\n1 0 1 1 1 0", "output": "YES" }, { "input": "6\n1 0 0 1 0 1", "output": "NO" }, { "input": "10\n1 0 1 1 1 0 1 0 1 0", "output": "YES" }, { "input": "15\n0 0 0 1 0 1 1 0 1 0 0 1 0 1 0", "output": "YES" }, { "input": "29\n0 1 0 0 0 1 1 1 1 0 0 1 0 0 0 1 1 1 1 0 1 1 0 1 0 0 1 1 1", "output": "NO" }, { "input": "77\n0 1 0 1 0 0 1 0 0 0 1 1 0 1 1 0 0 1 0 0 1 0 0 1 0 0 1 1 1 1 1 1 1 0 1 0 0 0 0 1 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 0 1 0 1 0 1 0 1 1 1", "output": "YES" }, { "input": "99\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "YES" }, { "input": "18\n0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 1 0", "output": "NO" }, { "input": "3\n0 0 0", "output": "NO" }, { "input": "3\n0 0 1", "output": "NO" }, { "input": "4\n1 0 1 0", "output": "NO" }, { "input": "4\n0 1 0 1", "output": "NO" }, { "input": "4\n1 1 0 0", "output": "NO" }, { "input": "4\n1 1 1 1", "output": "YES" }, { "input": "4\n0 0 0 0", "output": "NO" }, { "input": "4\n1 0 1 1", "output": "NO" }, { "input": "5\n1 0 1 1 0", "output": "NO" }, { "input": "5\n1 1 1 1 1", "output": "YES" }, { "input": "6\n0 0 1 0 0 1", "output": "NO" }, { "input": "6\n0 1 0 0 0 0", "output": "NO" }, { "input": "7\n0 0 1 0 0 0 1", "output": "NO" }, { "input": "7\n1 1 1 1 1 1 1", "output": "YES" }, { "input": "8\n1 0 1 0 1 0 1 0", "output": "YES" }, { "input": "15\n0 0 1 0 0 1 0 0 1 0 0 1 0 0 1", "output": "YES" }, { "input": "30\n1 0 0 0 1 0 1 1 1 1 0 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 0 0", "output": "YES" }, { "input": "100\n1 0 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 1 1 0 1 0 0 1 1 1 1 1 1 0 0 0 0 1 1 1 0 1 1 1 1 1 0 0 1 1 0 0 0 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 1 0 0 1 1 1 0 0", "output": "YES" }, { "input": "113\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "output": "YES" } ]
61
2,867,200
-1
17,617
426
Sereja and Mirroring
[ "implementation" ]
null
null
Let's assume that we are given a matrix *b* of size *x*<=×<=*y*, let's determine the operation of mirroring matrix *b*. The mirroring of matrix *b* is a 2*x*<=×<=*y* matrix *c* which has the following properties: - the upper half of matrix *c* (rows with numbers from 1 to *x*) exactly matches *b*; - the lower half of matrix *c* (rows with numbers from *x*<=+<=1 to 2*x*) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows *x* and *x*<=+<=1). Sereja has an *n*<=×<=*m* matrix *a*. He wants to find such matrix *b*, that it can be transformed into matrix *a*, if we'll perform on it several (possibly zero) mirrorings. What minimum number of rows can such matrix contain?
The first line contains two integers, *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100). Each of the next *n* lines contains *m* integers — the elements of matrix *a*. The *i*-th line contains integers *a**i*1,<=*a**i*2,<=...,<=*a**im* (0<=≤<=*a**ij*<=≤<=1) — the *i*-th row of the matrix *a*.
In the single line, print the answer to the problem — the minimum number of rows of matrix *b*.
[ "4 3\n0 0 1\n1 1 0\n1 1 0\n0 0 1\n", "3 3\n0 0 0\n0 0 0\n0 0 0\n", "8 1\n0\n1\n1\n0\n0\n1\n1\n0\n" ]
[ "2\n", "3\n", "2\n" ]
In the first test sample the answer is a 2 × 3 matrix *b*: If we perform a mirroring operation with this matrix, we get the matrix *a* that is given in the input:
[ { "input": "4 3\n0 0 1\n1 1 0\n1 1 0\n0 0 1", "output": "2" }, { "input": "3 3\n0 0 0\n0 0 0\n0 0 0", "output": "3" }, { "input": "8 1\n0\n1\n1\n0\n0\n1\n1\n0", "output": "2" }, { "input": "10 4\n0 0 1 0\n0 0 1 0\n1 1 0 1\n0 0 1 1\n1 0 1 0\n1 0 1 0\n0 0 1 1\n1 1 0 1\n0 0 1 0\n0 0 1 0", "output": "5" }, { "input": "10 3\n0 0 0\n1 1 1\n1 1 0\n0 0 0\n0 1 1\n0 1 1\n0 0 0\n1 1 0\n1 1 1\n0 0 0", "output": "5" }, { "input": "8 4\n1 0 0 0\n1 1 0 0\n1 0 0 1\n1 1 1 1\n0 0 1 1\n0 1 0 1\n0 1 1 1\n1 0 0 0", "output": "8" }, { "input": "2 9\n1 0 0 1 1 1 0 1 0\n1 0 0 1 0 0 0 1 1", "output": "2" }, { "input": "10 3\n0 1 0\n1 1 1\n1 0 1\n0 0 1\n1 0 1\n1 0 0\n1 1 0\n1 1 1\n1 0 1\n0 0 1", "output": "10" }, { "input": "8 4\n1 1 0 1\n0 0 0 0\n0 0 0 0\n1 1 0 1\n1 1 0 1\n0 0 0 0\n0 0 0 0\n1 1 0 1", "output": "2" }, { "input": "8 7\n1 1 0 0 1 1 0\n1 1 0 0 1 1 0\n1 1 0 0 1 1 0\n1 1 0 0 1 1 0\n1 1 0 0 1 1 0\n1 1 0 0 1 1 0\n1 1 0 0 1 1 0\n1 1 0 0 1 1 0", "output": "1" }, { "input": "6 5\n0 0 1 0 1\n1 0 0 1 0\n1 1 1 0 0\n1 0 1 1 0\n0 0 0 0 0\n1 0 1 0 0", "output": "6" }, { "input": "1 69\n0 0 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 1 1 0 1 0 0 1 0 0 0 1 1 1 1 1 1 1 1 0 1 0", "output": "1" }, { "input": "8 20\n0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0\n1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 0 1 1 1 1\n1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 0 1 1 1 1\n0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0\n0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0\n1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 0 1 1 1 1\n1 0 1 0 1 0 0 0 0 1 0 1 0 1 1 0 1 1 1 1\n0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 1 1 0", "output": "2" }, { "input": "1 1\n0", "output": "1" }, { "input": "1 1\n1", "output": "1" }, { "input": "2 2\n1 0\n0 1", "output": "2" }, { "input": "2 2\n0 1\n0 1", "output": "1" }, { "input": "1 2\n0 1", "output": "1" }, { "input": "1 100\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "1" }, { "input": "1 100\n1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1", "output": "1" }, { "input": "1 100\n0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "1" }, { "input": "100 1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0", "output": "25" }, { "input": "100 1\n1\n1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n1\n1", "output": "50" }, { "input": "100 1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n1\n0\n0\n0", "output": "100" }, { "input": "8 1\n1\n0\n0\n1\n1\n0\n1\n1", "output": "8" }, { "input": "6 1\n0\n0\n0\n0\n0\n0", "output": "3" }, { "input": "10 2\n1 1\n0 0\n0 0\n1 1\n0 0\n0 0\n1 1\n0 0\n0 0\n1 1", "output": "5" }, { "input": "4 2\n1 1\n0 0\n0 0\n0 0", "output": "4" }, { "input": "6 3\n1 1 1\n0 0 0\n1 1 1\n1 1 1\n0 0 0\n1 1 1", "output": "3" }, { "input": "6 3\n1 1 1\n1 0 1\n1 1 1\n1 1 1\n1 0 1\n1 1 1", "output": "3" }, { "input": "6 3\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1", "output": "3" }, { "input": "4 3\n1 0 1\n0 1 0\n1 0 1\n1 0 1", "output": "4" }, { "input": "6 1\n1\n1\n1\n1\n1\n1", "output": "3" }, { "input": "10 1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1", "output": "5" }, { "input": "3 1\n1\n1\n1", "output": "3" }, { "input": "6 3\n0 0 0\n0 0 0\n0 0 0\n0 0 0\n0 0 0\n0 0 0", "output": "3" }, { "input": "6 1\n1\n0\n1\n1\n0\n1", "output": "3" }, { "input": "6 6\n0 0 0 0 0 0\n0 0 0 0 0 0\n0 0 0 0 0 0\n0 0 0 0 0 0\n0 0 0 0 0 0\n0 0 0 0 0 0", "output": "3" }, { "input": "3 1\n1\n0\n1", "output": "3" }, { "input": "12 3\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1", "output": "3" }, { "input": "12 1\n0\n1\n0\n0\n1\n0\n0\n1\n0\n0\n1\n0", "output": "3" }, { "input": "2 3\n0 0 0\n0 0 0", "output": "1" }, { "input": "3 3\n1 1 1\n1 1 1\n0 0 0", "output": "3" }, { "input": "10 1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0", "output": "5" }, { "input": "12 1\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0", "output": "3" }, { "input": "6 2\n1 1\n1 1\n1 1\n1 1\n1 1\n1 1", "output": "3" }, { "input": "6 3\n1 1 0\n0 0 0\n1 1 0\n1 1 0\n0 0 0\n1 1 0", "output": "3" }, { "input": "6 2\n0 1\n0 1\n0 1\n0 1\n0 1\n0 1", "output": "3" }, { "input": "12 1\n1\n1\n0\n0\n1\n1\n1\n1\n0\n0\n1\n1", "output": "3" }, { "input": "6 2\n1 0\n1 0\n1 0\n1 0\n1 0\n1 0", "output": "3" } ]
77
0
3
17,622
923
Perfect Security
[ "data structures", "greedy", "strings", "trees" ]
null
null
Alice has a very important message *M* consisting of some non-negative integers that she wants to keep secret from Eve. Alice knows that the only theoretically secure cipher is one-time pad. Alice generates a random key *K* of the length equal to the message's length. Alice computes the bitwise xor of each element of the message and the key (, where denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)) and stores this encrypted message *A*. Alice is smart. Be like Alice. For example, Alice may have wanted to store a message *M*<==<=(0,<=15,<=9,<=18). She generated a key *K*<==<=(16,<=7,<=6,<=3). The encrypted message is thus *A*<==<=(16,<=8,<=15,<=17). Alice realised that she cannot store the key with the encrypted message. Alice sent her key *K* to Bob and deleted her own copy. Alice is smart. Really, be like Alice. Bob realised that the encrypted message is only secure as long as the key is secret. Bob thus randomly permuted the key before storing it. Bob thinks that this way, even if Eve gets both the encrypted message and the key, she will not be able to read the message. Bob is not smart. Don't be like Bob. In the above example, Bob may have, for instance, selected a permutation (3,<=4,<=1,<=2) and stored the permuted key *P*<==<=(6,<=3,<=16,<=7). One year has passed and Alice wants to decrypt her message. Only now Bob has realised that this is impossible. As he has permuted the key randomly, the message is lost forever. Did we mention that Bob isn't smart? Bob wants to salvage at least some information from the message. Since he is not so smart, he asks for your help. You know the encrypted message *A* and the permuted key *P*. What is the lexicographically smallest message that could have resulted in the given encrypted text? More precisely, for given *A* and *P*, find the lexicographically smallest message *O*, for which there exists a permutation π such that for every *i*. Note that the sequence *S* is lexicographically smaller than the sequence *T*, if there is an index *i* such that *S**i*<=&lt;<=*T**i* and for all *j*<=&lt;<=*i* the condition *S**j*<==<=*T**j* holds.
The first line contains a single integer *N* (1<=≤<=*N*<=≤<=300000), the length of the message. The second line contains *N* integers *A*1,<=*A*2,<=...,<=*A**N* (0<=≤<=*A**i*<=&lt;<=230) representing the encrypted message. The third line contains *N* integers *P*1,<=*P*2,<=...,<=*P**N* (0<=≤<=*P**i*<=&lt;<=230) representing the permuted encryption key.
Output a single line with *N* integers, the lexicographically smallest possible message *O*. Note that all its elements should be non-negative.
[ "3\n8 4 13\n17 2 7\n", "5\n12 7 87 22 11\n18 39 9 12 16\n", "10\n331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951\n226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667\n" ]
[ "10 3 28\n", "0 14 69 6 44\n", "128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284\n" ]
In the first case, the solution is (10, 3, 28), since <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a896b30a69636d1bfbfa981eae10650f5fee843c.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e383e4333ea37c4652ce2ac1ccfc2cfcf96e0896.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c24ed3c6f88805eb3710487b3fe07ff64034151a.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Other possible permutations of key yield messages (25, 6, 10), (25, 3, 15), (10, 21, 10), (15, 21, 15) and (15, 6, 28), which are all lexicographically larger than the solution.
[ { "input": "3\n8 4 13\n17 2 7", "output": "10 3 28" }, { "input": "5\n12 7 87 22 11\n18 39 9 12 16", "output": "0 14 69 6 44" }, { "input": "10\n331415699 278745619 998190004 423175621 42983144 166555524 843586353 802130100 337889448 685310951\n226011312 266003835 342809544 504667531 529814910 684873393 817026985 844010788 993949858 1031395667", "output": "128965467 243912600 4281110 112029883 223689619 76924724 429589 119397893 613490433 362863284" }, { "input": "5\n134 246 57 176 239\n14 83 97 175 187", "output": "41 77 55 209 188" }, { "input": "10\n241 187 20 18 151 144 238 193 86 63\n18 69 86 91 111 118 124 172 227 253", "output": "12 23 6 68 116 203 129 132 32 67" }, { "input": "4\n0 0 0 0\n0 0 0 0", "output": "0 0 0 0" }, { "input": "4\n5 5 3 3\n5 3 3 7", "output": "0 2 0 0" } ]
3,500
53,043,200
0
17,629
0
none
[ "none" ]
null
null
Вам задано прямоугольное клетчатое поле, состоящее из *n* строк и *m* столбцов. Поле содержит цикл из символов «*», такой что: - цикл можно обойти, посетив каждую его клетку ровно один раз, перемещаясь каждый раз вверх/вниз/вправо/влево на одну клетку; - цикл не содержит самопересечений и самокасаний, то есть две клетки цикла соседствуют по стороне тогда и только тогда, когда они соседние при перемещении вдоль цикла (самокасание по углу тоже запрещено). Ниже изображены несколько примеров допустимых циклов: Все клетки поля, отличные от цикла, содержат символ «.». Цикл на поле ровно один. Посещать клетки, отличные от цикла, Роботу нельзя. В одной из клеток цикла находится Робот. Эта клетка помечена символом «S». Найдите последовательность команд для Робота, чтобы обойти цикл. Каждая из четырёх возможных команд кодируется буквой и обозначает перемещение Робота на одну клетку: - «U» — сдвинуться на клетку вверх, - «R» — сдвинуться на клетку вправо, - «D» — сдвинуться на клетку вниз, - «L» — сдвинуться на клетку влево. Робот должен обойти цикл, побывав в каждой его клетке ровно один раз (кроме стартовой точки — в ней он начинает и заканчивает свой путь). Найдите искомую последовательность команд, допускается любое направление обхода цикла.
В первой строке входных данных записаны два целых числа *n* и *m* (3<=≤<=*n*,<=*m*<=≤<=100) — количество строк и столбцов прямоугольного клетчатого поля соответственно. В следующих *n* строках записаны по *m* символов, каждый из которых — «.», «*» или «S». Гарантируется, что отличные от «.» символы образуют цикл без самопересечений и самокасаний. Также гарантируется, что на поле ровно одна клетка содержит «S» и что она принадлежит циклу. Робот не может посещать клетки, помеченные символом «.».
В первую строку выходных данных выведите искомую последовательность команд для Робота. Направление обхода цикла Роботом может быть любым.
[ "3 3\n***\n*.*\n*S*\n", "6 7\n.***...\n.*.*...\n.*.S**.\n.*...**\n.*....*\n.******\n" ]
[ "LUURRDDL\n", "UULLDDDDDRRRRRUULULL\n" ]
В первом тестовом примере для обхода по часовой стрелке последовательность посещенных роботом клеток выглядит следующим образом: 1. клетка (3, 2); 1. клетка (3, 1); 1. клетка (2, 1); 1. клетка (1, 1); 1. клетка (1, 2); 1. клетка (1, 3); 1. клетка (2, 3); 1. клетка (3, 3); 1. клетка (3, 2).
[ { "input": "3 3\n***\n*.*\n*S*", "output": "LUURRDDL" }, { "input": "6 7\n.***...\n.*.*...\n.*.S**.\n.*...**\n.*....*\n.******", "output": "UULLDDDDDRRRRRUULULL" }, { "input": "100 3\n***\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\nS.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n*.*\n***", "output": "UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUURRDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDLLUUUUUUUUUUUUUUUUUU" }, { "input": "3 100\n****************************************************************************************************\n*..................................................................................................*\n**********************************************************************************S*****************", "output": "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLUURRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRDDLLLLLLLLLLLLLLLLL" } ]
93
23,142,400
-1
17,644
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
17,678
0
none
[ "none" ]
null
null
Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Petya loves long lucky numbers very much. He is interested in the minimum lucky number *d* that meets some condition. Let *cnt*(*x*) be the number of occurrences of number *x* in number *d* as a substring. For example, if *d*<==<=747747, then *cnt*(4)<==<=2, *cnt*(7)<==<=4, *cnt*(47)<==<=2, *cnt*(74)<==<=2. Petya wants the following condition to fulfil simultaneously: *cnt*(4)<==<=*a*1, *cnt*(7)<==<=*a*2, *cnt*(47)<==<=*a*3, *cnt*(74)<==<=*a*4. Petya is not interested in the occurrences of other numbers. Help him cope with this task.
The single line contains four integers *a*1, *a*2, *a*3 and *a*4 (1<=≤<=*a*1,<=*a*2,<=*a*3,<=*a*4<=≤<=106).
On the single line print without leading zeroes the answer to the problem — the minimum lucky number *d* such, that *cnt*(4)<==<=*a*1, *cnt*(7)<==<=*a*2, *cnt*(47)<==<=*a*3, *cnt*(74)<==<=*a*4. If such number does not exist, print the single number "-1" (without the quotes).
[ "2 2 1 1\n", "4 7 3 1\n" ]
[ "4774\n", "-1\n" ]
none
[ { "input": "2 2 1 1", "output": "4774" }, { "input": "4 7 3 1", "output": "-1" }, { "input": "4 7 4 7", "output": "-1" }, { "input": "1 1 1 1", "output": "-1" }, { "input": "2 2 1 2", "output": "7474" }, { "input": "2 1 2 1", "output": "-1" }, { "input": "2 2 2 1", "output": "4747" }, { "input": "3 3 1 1", "output": "447774" }, { "input": "3 2 1 2", "output": "74474" }, { "input": "2 1 3 1", "output": "-1" }, { "input": "7 7 1 1", "output": "44444477777774" }, { "input": "4 7 2 1", "output": "44474777777" }, { "input": "3 3 2 2", "output": "474774" }, { "input": "4 3 2 1", "output": "4447477" }, { "input": "4 4 2 2", "output": "44747774" }, { "input": "1000000 1000000 1 1", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 1 1 1", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "10 10 3 5", "output": "-1" }, { "input": "10 10 3 7", "output": "-1" }, { "input": "1 7 1 1", "output": "74777777" }, { "input": "8 3 2 1", "output": "44444447477" }, { "input": "1000000 1000000 1000000 1000000", "output": "-1" }, { "input": "4584 45854 25 685", "output": "-1" }, { "input": "1 1 1 2", "output": "-1" }, { "input": "3 1000000 3 1", "output": "-1" }, { "input": "4 58458 2 1", "output": "4447477777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777..." }, { "input": "845 8549 54 8", "output": "-1" }, { "input": "1000000 1000000 100000 100000", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 1000000 499 500", "output": "7444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 1000000 100000 100001", "output": "7444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "845488 44884 9945 9944", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "7 7 4 5", "output": "74447474747774" }, { "input": "7 8 5 4", "output": "444747474747777" }, { "input": "1000000 1000000 400000 400001", "output": "7444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "4585 4588 98 99", "output": "7444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 2 1 1", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "2 100000 1 2", "output": "7477777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777..." }, { "input": "86451 754 85 84", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "98654 4844 1001 1000", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "5 5 1 2", "output": "7444477774" }, { "input": "5 5 2 1", "output": "4444747777" }, { "input": "4 4 1 1", "output": "44477774" }, { "input": "4 4 2 2", "output": "44747774" }, { "input": "4 4 3 3", "output": "47474774" }, { "input": "10 9 4 5", "output": "7444444747474777774" }, { "input": "100 100 4 5", "output": "74444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444447474747777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777774" }, { "input": "1000000 1000000 1 2", "output": "7444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 1000000 47 46", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 1000000 999999 1000000", "output": "7474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747..." }, { "input": "54585 4584 458 954", "output": "-1" }, { "input": "6 6 1 3", "output": "-1" }, { "input": "6 6 2 3", "output": "744447477774" }, { "input": "6 10 2 1", "output": "4444474777777777" }, { "input": "7 3 1 1", "output": "4444447774" }, { "input": "47 74 8 9", "output": "7444444444444444444444444444444444444444747474747474747777777777777777777777777777777777777777777777777777777777777777774" }, { "input": "458445 445 6 7", "output": "7444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 1000000 10000 10001", "output": "7444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "1000000 1000000 978 977", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "7 1000 1 2", "output": "7444444777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777..." }, { "input": "1 10 1 1", "output": "74777777777" }, { "input": "70 60 20 21", "output": "7444444444444444444444444444444444444444444444444447474747474747474747474747474747474747477777777777777777777777777777777777777774" }, { "input": "57 59 3 4", "output": "74444444444444444444444444444444444444444444444444444447474777777777777777777777777777777777777777777777777777777774" }, { "input": "10 10 5 5", "output": "44444747474747777774" }, { "input": "69 84 25 24", "output": "444444444444444444444444444444444444444444444747474747474747474747474747474747474747474747474777777777777777777777777777777777777777777777777777777777777" }, { "input": "25 94 11 12", "output": "74444444444444474747474747474747474777777777777777777777777777777777777777777777777777777777777777777777777777777777774" }, { "input": "1 1000000 4 5", "output": "-1" }, { "input": "7 1000000 2 3", "output": "7444447477777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777..." }, { "input": "1000000 1 1 1", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "45 65 31 32", "output": "74444444444444474747474747474747474747474747474747474747474747474747474747477777777777777777777777777777777774" }, { "input": "31 32 30 31", "output": "747474747474747474747474747474747474747474747474747474747474774" }, { "input": "1 1000000 1 1", "output": "7477777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777..." }, { "input": "2 1000000 1 1", "output": "4777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777..." }, { "input": "50000 1000000 1 1", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "74544 1 1 1", "output": "4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444..." }, { "input": "2 2 3 4", "output": "-1" }, { "input": "1 1000 2 1", "output": "-1" }, { "input": "1 10 1 2", "output": "-1" }, { "input": "1000000 1000000 1000000 999999", "output": "4747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474..." }, { "input": "1 2 1 1", "output": "747" }, { "input": "2 1 1 1", "output": "474" }, { "input": "2 2 2 1", "output": "4747" }, { "input": "2 2 2 2", "output": "-1" }, { "input": "1 3 1 1", "output": "7477" }, { "input": "1 4 1 1", "output": "74777" }, { "input": "2 3 2 2", "output": "74747" }, { "input": "2 5 2 2", "output": "7474777" }, { "input": "1 2 2 1", "output": "-1" }, { "input": "10 100 2 2", "output": "44444444747777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777774" }, { "input": "3 4 3 3", "output": "7474747" }, { "input": "30 30 29 29", "output": "474747474747474747474747474747474747474747474747474747474774" }, { "input": "10 10 1 2", "output": "74444444447777777774" }, { "input": "999999 1000000 999999 999999", "output": "7474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747474747..." }, { "input": "10 11 1 1", "output": "444444444777777777774" }, { "input": "10 10 2 2", "output": "44444444747777777774" }, { "input": "100 200 5 5", "output": "444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444447474747477777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777774" }, { "input": "10 11 10 10", "output": "747474747474747474747" }, { "input": "5 10 2 3", "output": "744474777777774" }, { "input": "6 8 1 1", "output": "44444777777774" }, { "input": "1 9 2 2", "output": "-1" } ]
218
307,200
0
17,687
13
Sequence
[ "dp", "sortings" ]
C. Sequence
1
64
Little Petya likes to play very much. And most of all he likes to play the following game: He is given a sequence of *N* integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help. The sequence *a* is called non-decreasing if *a*1<=≤<=*a*2<=≤<=...<=≤<=*a**N* holds, where *N* is the length of the sequence.
The first line of the input contains single integer *N* (1<=≤<=*N*<=≤<=5000) — the length of the initial sequence. The following *N* lines contain one integer each — elements of the sequence. These numbers do not exceed 109 by absolute value.
Output one integer — minimum number of steps required to achieve the goal.
[ "5\n3 2 -1 2 11\n", "5\n2 1 1 1 1\n" ]
[ "4\n", "1\n" ]
none
[ { "input": "5\n3 2 -1 2 11", "output": "4" }, { "input": "5\n2 1 1 1 1", "output": "1" }, { "input": "5\n0 0 0 0 0", "output": "0" }, { "input": "1\n11", "output": "0" }, { "input": "2\n10 2", "output": "8" }, { "input": "6\n1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000", "output": "6000000000" }, { "input": "7\n1000000000 -1000000000 1000000000 -1000000000 1000000000 -1000000000 1000000000", "output": "6000000000" }, { "input": "10\n3 0 5 10 10 9 10 8 8 1", "output": "16" }, { "input": "20\n5 5 6 5 10 9 4 0 0 0 7 7 7 5 6 7 5 6 5 6", "output": "33" }, { "input": "20\n10 6 1 9 10 10 7 0 10 5 9 1 5 7 4 9 3 9 7 2", "output": "55" }, { "input": "20\n0 0 0 1 5 10 7 7 8 7 66 64 62 65 39 38 72 69 71 72", "output": "63" }, { "input": "5\n1 2 3 1 1", "output": "3" } ]
92
0
0
17,706
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" }, { "input": "101", "output": "100" }, { "input": "111", "output": "0" }, { "input": "121", "output": "65" }, { "input": "161", "output": "130" }, { "input": "165", "output": "87" }, { "input": "1430", "output": "0" }, { "input": "32822", "output": "0" }, { "input": "42914", "output": "0" }, { "input": "67075", "output": "0" }, { "input": "794397", "output": "0" }, { "input": "870968", "output": "0" }, { "input": "990089", "output": "0" }, { "input": "686686", "output": "343343" }, { "input": "928818", "output": "468954" }, { "input": "165355", "output": "85697" }, { "input": "365662", "output": "183281" }, { "input": "30092", "output": "15541" }, { "input": "95948", "output": "47974" }, { "input": "189108", "output": "95049" }, { "input": "970068", "output": "485484" }, { "input": "230021", "output": "130990" }, { "input": "999999", "output": "500994" }, { "input": "199998", "output": "99999" }, { "input": "119801", "output": "109900" }, { "input": "100001", "output": "100000" }, { "input": "891297", "output": "0" }, { "input": "110401", "output": "0" }, { "input": "177067", "output": "0" }, { "input": "1000000", "output": "0" }, { "input": "201262002", "output": "0" }, { "input": "813594318", "output": "0" }, { "input": "643303246", "output": "0" }, { "input": "2277107722", "output": "0" }, { "input": "1094093901", "output": "0" }, { "input": "1063002601", "output": "0" }, { "input": "5593333955", "output": "3000033952" }, { "input": "1624637326", "output": "814719908" }, { "input": "8292112917", "output": "4292199993" }, { "input": "9012332098", "output": "4512399944" }, { "input": "1432011233", "output": "732109996" }, { "input": "1898999897", "output": "998999998" }, { "input": "9009890098", "output": "4510089944" }, { "input": "4321001234", "output": "2200001212" }, { "input": "1738464936", "output": "869281968" }, { "input": "4602332064", "output": "2301211032" }, { "input": "1001760001", "output": "1000790000" }, { "input": "1000000001", "output": "1000000000" }, { "input": "6149019415", "output": "0" }, { "input": "7280320916", "output": "0" }, { "input": "1111334001", "output": "0" }, { "input": "6762116775", "output": "0" }, { "input": "10000000000", "output": "0" }, { "input": "3031371285404035821731303", "output": "0" }, { "input": "3390771275149315721770933", "output": "0" }, { "input": "8344403107710167013044438", "output": "0" }, { "input": "398213879352153978312893", "output": "0" }, { "input": "319183517960959715381913", "output": "0" }, { "input": "447341993380073399143744", "output": "0" }, { "input": "2300941052398832501490032", "output": "1200000000009832501490011" }, { "input": "1931812635088771537217148", "output": "970812706008970929999069" }, { "input": "2442173122931392213712431", "output": "1442173122935999999999990" }, { "input": "1098765432101101234567900", "output": "550000000000101234567845" }, { "input": "1123456789876678987654320", "output": "563456789876999999999955" }, { "input": "9009900990099009900990098", "output": "4510001000104999899989944" }, { "input": "9012320990123209901232098", "output": "4512321000126999899999944" }, { "input": "1789878987898898789878986", "output": "899878987898999999999988" }, { "input": "1625573270595486073374436", "output": "812786635305982536687218" }, { "input": "1000000177157517600000001", "output": "1000000077158999900000000" }, { "input": "1000000000000000000000001", "output": "1000000000000000000000000" }, { "input": "4038996154923294516988304", "output": "0" }, { "input": "3454001245690964432004542", "output": "0" }, { "input": "5200592971632471682861014", "output": "0" }, { "input": "7899445300286737036548887", "output": "0" }, { "input": "10000000000000000000000000", "output": "0" } ]
2,000
1,331,200
0
17,708
711
ZS and The Birthday Paradox
[ "math", "number theory", "probabilities" ]
null
null
ZS the Coder has recently found an interesting concept called the Birthday Paradox. It states that given a random set of 23 people, there is around 50% chance that some two of them share the same birthday. ZS the Coder finds this very interesting, and decides to test this with the inhabitants of Udayland. In Udayland, there are 2*n* days in a year. ZS the Coder wants to interview *k* people from Udayland, each of them has birthday in one of 2*n* days (each day with equal probability). He is interested in the probability of at least two of them have the birthday at the same day. ZS the Coder knows that the answer can be written as an irreducible fraction . He wants to find the values of *A* and *B* (he does not like to deal with floating point numbers). Can you help him?
The first and only line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=1018,<=2<=≤<=*k*<=≤<=1018), meaning that there are 2*n* days in a year and that ZS the Coder wants to interview exactly *k* people.
If the probability of at least two *k* people having the same birthday in 2*n* days long year equals (*A*<=≥<=0, *B*<=≥<=1, ), print the *A* and *B* in a single line. Since these numbers may be too large, print them modulo 106<=+<=3. Note that *A* and *B* must be coprime before their remainders modulo 106<=+<=3 are taken.
[ "3 2\n", "1 3\n", "4 3\n" ]
[ "1 8", "1 1", "23 128" ]
In the first sample case, there are 2<sup class="upper-index">3</sup> = 8 days in Udayland. The probability that 2 people have the same birthday among 2 people is clearly <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5947a169159fe867f85f3fd8b9690019b48152f5.png" style="max-width: 100.0%;max-height: 100.0%;"/>, so *A* = 1, *B* = 8. In the second sample case, there are only 2<sup class="upper-index">1</sup> = 2 days in Udayland, but there are 3 people, so it is guaranteed that two of them have the same birthday. Thus, the probability is 1 and *A* = *B* = 1.
[ { "input": "3 2", "output": "1 8" }, { "input": "1 3", "output": "1 1" }, { "input": "4 3", "output": "23 128" }, { "input": "1000000000000000000 1000000000000000000", "output": "906300 906300" }, { "input": "59 576460752303423489", "output": "1 1" }, { "input": "1234567891234 100005", "output": "173817 722464" }, { "input": "2 4", "output": "29 32" }, { "input": "59 576460752303423488", "output": "840218 840218" }, { "input": "2016 2016", "output": "1564 227035" }, { "input": "2016 2017", "output": "360153 815112" }, { "input": "468804735183774830 244864585447548924", "output": "365451 365451" }, { "input": "172714899512474455 414514930706102803", "output": "626500 626500" }, { "input": "876625063841174080 360793239109880865", "output": "34117 34117" }, { "input": "70181875975239647 504898544415017211", "output": "79176 79176" }, { "input": "364505998666117889 208660487087853057", "output": "83777 83777" }, { "input": "648371335753080490 787441", "output": "228932 228932" }, { "input": "841928147887146057 620004", "output": "151333 51640" }, { "input": "545838312215845682 715670", "output": "156176 156176" }, { "input": "473120513399321115 489435", "output": "57896 535051" }, { "input": "17922687587622540 3728", "output": "478998 792943" }, { "input": "211479504016655403 861717213151744108", "output": "196797 196797" }, { "input": "718716873663426516 872259572564867078", "output": "401470 401470" }, { "input": "422627037992126141 41909917823420958", "output": "268735 268735" }, { "input": "616183854421159004 962643186273781485", "output": "149006 149006" }, { "input": "160986032904427725 153429", "output": "100374 100374" }, { "input": "88268234087903158 290389", "output": "566668 88331" }, { "input": "58453009367192916 164246", "output": "317900 341568" }, { "input": "565690379013964030 914981", "output": "547343 547343" }, { "input": "269600543342663655 10645", "output": "913809 282202" }, { "input": "37774758680708184 156713778825283978", "output": "73122 73122" }, { "input": "231331570814773750 77447051570611803", "output": "578654 578654" }, { "input": "935241735143473375 247097392534198386", "output": "181888 181888" }, { "input": "639151895177205704 416747737792752265", "output": "135045 135045" }, { "input": "412663884364501543 401745061547424998", "output": "228503 228503" }, { "input": "180838095407578776 715935", "output": "378695 378695" }, { "input": "884748259736278401 407112", "output": "25714 811489" }, { "input": "78305076165311264 280970", "output": "293282 624669" }, { "input": "782215240494010889 417929", "output": "665887 270857" }, { "input": "486125404822710514 109107", "output": "832669 164722" }, { "input": "57626821183859235 372443612949184377", "output": "802451 802451" }, { "input": "27811605053083586 516548918254320722", "output": "894732 894732" }, { "input": "955093801941591723 462827230953066080", "output": "999170 999170" }, { "input": "659003966270291348 426245", "output": "795318 278062" }, { "input": "852560778404356914 258808", "output": "775128 775128" }, { "input": "397362961182592931 814397", "output": "155345 155345" }, { "input": "904600330829364045 969618", "output": "245893 245893" }, { "input": "98157142963429612 169605644318211774", "output": "409023 409023" }, { "input": "802067302997161941 115883952721989836", "output": "928705 928705" }, { "input": "505977467325861565 285534302275511011", "output": "782797 782797" }, { "input": "274151686958873391 747281437213482980", "output": "977029 977029" }, { "input": "467708499092938957 59762", "output": "283212 204310" }, { "input": "751573831884934263 851791", "output": "905743 905743" }, { "input": "455483991918666592 947456", "output": "570626 570626" }, { "input": "649040812642666750 821314", "output": "57323 57323" }, { "input": "417215023685743983 376900", "output": "122689 122689" }, { "input": "121125188014443608 400338158982406735", "output": "199488 199488" }, { "input": "314682004443476471 544443468582510377", "output": "279665 279665" }, { "input": "821919374090247584 554985827995633347", "output": "854880 854880" }, { "input": "525829538418947209 501264136399411409", "output": "715564 715564" }, { "input": "426597183791521709 928925", "output": "835709 835709" }, { "input": "620154000220554572 802783", "output": "163153 163153" }, { "input": "324064160254286900 898448", "output": "18338 18338" }, { "input": "831301534196025310 690475", "output": "964028 964028" }, { "input": "24858346330090877 523038", "output": "5846 5846" }, { "input": "569660524813359598 814752357830129986", "output": "780635 780635" }, { "input": "496942725996835031 761030666233908048", "output": "746587 746587" }, { "input": "467127505571092085 905135971539044394", "output": "608084 608084" }, { "input": "394409702459600222 851414284237789752", "output": "419420 419420" }, { "input": "703820075205013062 862025309890418636", "output": "982260 982260" }, { "input": "471994290543057591 972026", "output": "215668 215668" }, { "input": "665551106972090453 845883", "output": "623684 623684" }, { "input": "369461267005822782 537061", "output": "97003 97003" }, { "input": "73371431334522407 674020", "output": "899111 372106" }, { "input": "266928247763555270 547878", "output": "817352 54712" }, { "input": "615057631564895479 807178821338760482", "output": "52078 52078" }, { "input": "318967795893595104 976829166597314361", "output": "750015 750015" }, { "input": "512524612322627967 897562435047674890", "output": "614855 614855" }, { "input": "216434772356360296 67212780306228770", "output": "995572 995572" }, { "input": "13491088710006829 715337619732144903", "output": "719453 719453" }, { "input": "688519152023104450 70486", "output": "476402 371144" }, { "input": "685403173770208801 962607", "output": "135409 135409" }, { "input": "389313338098908426 99564", "output": "205907 386429" }, { "input": "93223502427608051 790744", "output": "983387 983387" }, { "input": "286780314561673617 664601", "output": "654850 654850" }, { "input": "831582488749975043 182016637013124494", "output": "159828 159828" }, { "input": "758864689933450475 128294949711869852", "output": "37325 37325" }, { "input": "532376674825779019 113292273466542585", "output": "36122 36122" }, { "input": "236286839154478644 282942618725096464", "output": "187677 187677" }, { "input": "940197003483178269 77403", "output": "119089 181418" }, { "input": "708371214526255502 632992", "output": "615316 615316" }, { "input": "901928035250255660 465555", "output": "586380 781987" }, { "input": "605838195283987989 198026", "output": "929969 156402" }, { "input": "15266076338626979 913942576088954168", "output": "506165 506165" }, { "input": "83260344505016157 935999340494020219", "output": "138293 138293" }, { "input": "851434559843060686 397746475431992189", "output": "314138 314138" }, { "input": "555344724171760311 567396824985513364", "output": "666610 666610" }, { "input": "748901536305825878 347728", "output": "80599 80599" }, { "input": "452811696339558207 443394", "output": "474530 348263" }, { "input": "960049070281296616 235421", "output": "274784 325200" }, { "input": "728223285619341145 791009", "output": "764528 274644" }, { "input": "698408060898630904 50803201495883240", "output": "750308 750308" }, { "input": "625690262082106337 220453546754437119", "output": "741435 741435" }, { "input": "329600422115838666 166731855158215181", "output": "242921 242921" }, { "input": "523157242839838824 310837164758318823", "output": "726051 726051" }, { "input": "871286622346211738 836848346410668404", "output": "530710 530710" }, { "input": "575196786674911363 36374", "output": "88076 806040" }, { "input": "768753603103944226 868940", "output": "118118 118118" }, { "input": "472663767432643850 601411", "output": "203104 203104" }, { "input": "176573931761343475 697077", "output": "389281 749563" }, { "input": "301399940652446487 937011639371661304", "output": "165989 165989" }, { "input": "494956757081479349 760223", "output": "586955 423513" }, { "input": "198866921410178974 492694", "output": "847137 847137" }, { "input": "902777085738878599 348432", "output": "396798 564327" }, { "input": "96333897872944166 462217", "output": "367832 367832" }, { "input": "864508113210988695 17803", "output": "107443 838933" }, { "input": "371745482857759808 590068361140585059", "output": "748215 748215" }, { "input": "341930258137049567 734173670740688701", "output": "21530 21530" }, { "input": "269212459320525000 680451979144466763", "output": "868951 868951" }, { "input": "973122623649224625 850102328697987938", "output": "781676 781676" }, { "input": "517924802132493346 67413", "output": "954073 995488" }, { "input": "711481618561526208 858685", "output": "929035 929035" }, { "input": "218718983913330026 55198", "output": "99469 89622" }, { "input": "922629148242029651 787671", "output": "164442 164442" }, { "input": "116185964671062513 620234", "output": "798435 622171" }, { "input": "884360180009107043 795255840146329784", "output": "541758 541758" }, { "input": "588270344337806667 964906185404883662", "output": "544853 544853" }, { "input": "781827160766839530 885639453855244191", "output": "627074 627074" }, { "input": "91237529217285074 672878442653097259", "output": "988072 988072" }, { "input": "859411744555329603 932262", "output": "859175 859175" }, { "input": "563321908884029228 664734", "output": "883734 883734" }, { "input": "756878725313062090 497297", "output": "641345 641345" }, { "input": "460788885346794419 634257", "output": "660266 660266" }, { "input": "164699049675494044 325434", "output": "170498 994561" }, { "input": "500001 1000002", "output": "998979 999491" }, { "input": "1000003 1000002", "output": "256 256" }, { "input": "1000002 1000003", "output": "256 256" }, { "input": "1000002 1000003", "output": "256 256" }, { "input": "1000002 1000002", "output": "512 512" }, { "input": "500001 1000003", "output": "256 256" } ]
514
61,440,000
0
17,712
325
The Red Button
[ "combinatorics", "dfs and similar", "dsu", "graphs", "greedy" ]
null
null
Piegirl found the red button. You have one last chance to change the inevitable end. The circuit under the button consists of *n* nodes, numbered from 0 to *n* - 1. In order to deactivate the button, the *n* nodes must be disarmed in a particular order. Node 0 must be disarmed first. After disarming node *i*, the next node to be disarmed must be either node (2·*i*) modulo *n* or node (2·*i*)<=+<=1 modulo *n*. The last node to be disarmed must be node 0. Node 0 must be disarmed twice, but all other nodes must be disarmed exactly once. Your task is to find any such order and print it. If there is no such order, print -1.
Input consists of a single integer *n* (2<=≤<=*n*<=≤<=105).
Print an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them.
[ "2\n", "3\n", "4\n", "16\n" ]
[ "0 1 0\n", "-1", "0 1 3 2 0\n", "0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0\n" ]
none
[ { "input": "2", "output": "0 1 0" }, { "input": "3", "output": "-1" }, { "input": "4", "output": "0 1 3 2 0" }, { "input": "16", "output": "0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0" }, { "input": "5", "output": "-1" }, { "input": "7", "output": "-1" }, { "input": "32", "output": "0 1 2 4 8 17 3 6 12 25 18 5 10 20 9 19 7 14 29 26 21 11 22 13 27 23 15 31 30 28 24 16 0" }, { "input": "255", "output": "-1" }, { "input": "65536", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32769 3 6 12 24 48 96 192 384 768 1536 3072 6144 12288 24576 49153 32770 5 10 20 40 80 160 320 640 1280 2560 5120 10240 20480 40960 16385 32771 7 14 28 56 112 224 448 896 1792 3584 7168 14336 28672 57345 49154 32772 9 18 36 72 144 288 576 1152 2304 4608 9216 18432 36864 8193 16386 32773 11 22 44 88 176 352 704 1408 2816 5632 11264 22528 45056 24577 49155 32774 13 26 52 104 208 416 832 1664 3328 6656 13312 26624 53248 40961 16387 32775 15 30 60 120 24..." }, { "input": "99999", "output": "-1" }, { "input": "9", "output": "-1" }, { "input": "6", "output": "0 1 2 5 4 3 0" }, { "input": "8", "output": "0 1 2 5 3 7 6 4 0" }, { "input": "10", "output": "0 1 2 4 9 8 6 3 7 5 0" }, { "input": "12", "output": "0 1 2 4 8 5 11 10 9 7 3 6 0" }, { "input": "20", "output": "0 1 2 4 8 16 12 5 11 3 6 13 7 14 9 19 18 17 15 10 0" }, { "input": "25", "output": "-1" }, { "input": "30", "output": "0 1 2 4 8 16 3 6 12 24 19 9 18 7 14 29 28 26 23 17 5 10 21 13 27 25 20 11 22 15 0" }, { "input": "32", "output": "0 1 2 4 8 17 3 6 12 25 18 5 10 20 9 19 7 14 29 26 21 11 22 13 27 23 15 31 30 28 24 16 0" }, { "input": "45", "output": "-1" }, { "input": "50", "output": "0 1 2 4 8 16 32 14 28 6 12 24 49 48 46 42 34 18 36 22 44 39 29 9 19 38 26 3 7 15 30 10 20 40 31 13 27 5 11 23 47 45 41 33 17 35 21 43 37 25 0" }, { "input": "100", "output": "0 1 2 4 8 16 32 64 28 56 12 24 48 96 92 84 68 36 72 44 88 76 52 5 10 20 40 80 60 21 42 85 70 41 82 65 30 61 22 45 90 81 62 25 51 3 6 13 26 53 7 14 29 58 17 34 69 38 77 54 9 18 37 74 49 99 98 97 94 89 79 59 19 39 78 57 15 31 63 27 55 11 23 46 93 86 73 47 95 91 83 66 33 67 35 71 43 87 75 50 0" }, { "input": "126", "output": "0 1 2 4 8 16 32 64 3 6 12 24 48 96 66 7 14 28 56 112 98 70 15 30 60 120 114 102 78 31 62 125 124 122 118 110 95 65 5 10 20 40 80 35 71 17 34 68 11 22 44 88 50 100 74 23 46 92 58 116 106 87 49 99 72 18 37 75 25 51 103 81 36 73 21 42 85 45 90 54 109 93 61 123 121 117 108 91 57 115 104 83 41 82 39 79 33 67 9 19 38 77 29 59 119 113 101 76 27 55 111 97 69 13 26 53 107 89 52 105 84 43 86 47 94 63 0" }, { "input": "513", "output": "-1" }, { "input": "514", "output": "0 1 2 4 8 16 32 64 128 256 513 512 510 506 498 482 450 386 258 3 6 12 24 48 96 192 384 254 508 502 490 466 418 322 130 260 7 14 28 56 112 224 448 382 250 500 486 458 402 290 66 132 264 15 30 60 120 240 480 446 378 242 484 454 394 274 34 68 136 272 31 62 124 248 496 478 442 370 226 452 390 266 18 36 72 144 288 63 126 252 504 494 474 434 354 194 388 262 10 20 40 80 160 321 129 259 5 11 22 44 88 176 352 190 380 246 492 470 426 338 162 324 134 268 23 46 92 184 368 222 444 374 234 468 422 330 146 292 70 140 280..." }, { "input": "800", "output": "0 1 2 4 8 16 32 64 128 256 512 224 448 96 192 384 768 736 672 544 288 576 352 704 608 416 33 66 132 264 528 257 514 228 456 112 225 450 100 200 401 3 6 12 24 48 97 194 388 776 752 705 610 420 40 80 160 320 640 480 161 322 644 488 176 353 706 612 424 49 98 196 392 784 769 738 676 552 304 609 418 36 72 144 289 578 356 712 624 449 99 198 396 792 785 770 740 680 560 321 642 484 168 336 673 546 292 584 368 737 674 548 296 592 385 771 742 684 568 337 675 550 300 601 402 5 10 20 41 82 164 328 656 513 226 452 104 ..." }, { "input": "1000", "output": "0 1 2 4 8 16 32 64 128 256 512 24 48 96 192 384 768 536 72 144 288 576 152 304 608 216 432 864 728 456 912 824 648 296 592 184 368 736 472 944 888 776 552 104 208 416 832 664 328 656 312 624 248 496 992 984 968 936 872 744 488 976 952 904 808 616 232 464 928 856 712 424 848 696 392 784 568 136 272 544 88 176 352 704 408 816 632 264 528 56 112 224 448 896 792 584 168 336 672 344 688 376 752 504 9 18 36 73 146 292 585 170 340 680 360 720 440 880 760 520 40 80 160 320 640 280 560 120 240 480 960 920 840 681 3..." }, { "input": "2500", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 1596 692 1384 268 536 1072 2144 1788 1076 2152 1804 1108 2216 1932 1364 228 456 912 1824 1148 2296 2092 1684 868 1736 972 1944 1388 276 552 1104 2208 1916 1332 164 328 656 1312 124 248 496 992 1984 1468 436 872 1744 988 1976 1452 404 808 1616 732 1464 428 856 1712 924 1848 1196 2392 2284 2068 1636 772 1544 588 1176 2352 2204 1908 1316 132 264 528 1056 2112 1724 948 1896 1292 84 168 336 672 1344 188 376 752 1504 508 1016 2032 1564 628 1256 12 24 48 96 192 384 768 153..." }, { "input": "6400", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 1792 3584 768 1536 3072 6144 5888 5376 4352 2304 4608 2816 5632 4864 3328 257 514 1028 2056 4112 1824 3648 896 1793 3586 772 1544 3088 6176 5952 5504 4609 2818 5636 4872 3344 288 576 1152 2305 4610 2820 5640 4880 3360 320 640 1280 2560 5120 3840 1281 2562 5124 3848 1296 2592 5184 3968 1537 3074 6148 5896 5392 4384 2368 4736 3073 6146 5892 5384 4368 2336 4672 2944 5889 5378 4356 2312 4624 2848 5696 4992 3585 770 1540 3080 6160 5920 5440 4480 2561 5122 3844 1288 ..." }, { "input": "23105", "output": "-1" }, { "input": "24002", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 8766 17532 11062 22124 20246 16490 8978 17956 11910 23820 23638 23274 22546 21090 18178 12354 706 1412 2824 5648 11296 22592 21182 18362 12722 1442 2884 5768 11536 23072 22142 20282 16562 9122 18244 12486 970 1940 3880 7760 15520 7038 14076 4150 8300 16600 9198 18396 12790 1578 3156 6312 12624 1246 2492 4984 9968 19936 15870 7738 15476 6950 13900 3798 7596 15192 6382 12764 1526 3052 6104 12208 414 828 1656 3312 6624 13248 2494 4988 9976 19952 15902 7..." }, { "input": "29024", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 3744 7488 14976 928 1856 3712 7424 14848 672 1344 2688 5376 10752 21504 13984 27968 26912 24800 20576 12128 24256 19488 9952 19904 10784 21568 14112 28224 27424 25824 22624 16224 3424 6848 13696 27392 25760 22496 15968 2912 5824 11648 23296 17568 6112 12224 24448 19872 10720 21440 13856 27712 26400 23776 18528 8032 16064 3104 6208 12416 24832 20640 12256 24512 20000 10976 21952 14880 736 1472 2944 5888 11776 23552 18080 7136 14272 28544 28064 27104 2..." }, { "input": "36002", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 29534 23066 10130 20260 4518 9036 18072 142 284 568 1136 2272 4544 9088 18176 350 700 1400 2800 5600 11200 22400 8798 17596 35192 34382 32762 29522 23042 10082 20164 4326 8652 17304 34608 33214 30426 24850 13698 27396 18790 1578 3156 6312 12624 25248 14494 28988 21974 7946 15892 31784 27566 19130 2258 4516 9032 18064 126 252 504 1008 2016 4032 8064 16128 32256 28510 21018 6034 12068 24136 12270 24540 13078 26156 16310 32620 29238 22474 8946 178..." }, { "input": "55555", "output": "-1" }, { "input": "65534", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 3 6 12 24 48 96 192 384 768 1536 3072 6144 12288 24576 49152 32770 7 14 28 56 112 224 448 896 1792 3584 7168 14336 28672 57344 49154 32774 15 30 60 120 240 480 960 1920 3840 7680 15360 30720 61440 57346 49158 32782 31 62 124 248 496 992 1984 3968 7936 15872 31744 63488 61442 57350 49166 32798 63 126 252 504 1008 2016 4032 8064 16128 32256 64512 63490 61446 57358 49182 32830 127 254 508 1016 2032 4064 8128 16256 32512 65024 64514 63494 61454 573..." }, { "input": "77776", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 53296 28816 57632 37488 74976 72176 66576 55376 32976 65952 54128 30480 60960 44144 10512 21024 42048 6320 12640 25280 50560 23344 46688 15600 31200 62400 47024 16272 32544 65088 52400 27024 54048 30320 60640 43504 9232 18464 36928 73856 69936 62096 46416 15056 30112 60224 42672 7568 15136 30272 60544 43312 8848 17696 35392 70784 63792 49808 21840 43680 9584 19168 38336 76672 75568 73360 68944 60112 42448 7120 14240 28480 56960 36144 7228..." }, { "input": "88888", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 42184 84368 79848 70808 52728 16568 33136 66272 43656 87312 85736 82584 76280 63672 38456 76912 64936 40984 81968 75048 61208 33528 67056 45224 1560 3120 6240 12480 24960 49920 10952 21904 43808 87616 86344 83800 78712 68536 48184 7480 14960 29920 59840 30792 61584 34280 68560 48232 7576 15152 30304 60608 32328 64656 40424 80848 72808 56728 24568 49136 9384 18768 37536 75072 61256 33624 67248 45608 2328 4656 9312 18624 37248 74496 60104 3..." }, { "input": "99494", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 31578 63156 26818 53636 7778 15556 31112 62224 24954 49908 322 644 1288 2576 5152 10304 20608 41216 82432 65370 31246 62492 25490 50980 2466 4932 9864 19728 39456 78912 58330 17166 34332 68664 37834 75668 51842 4190 8380 16760 33520 67040 34586 69172 38850 77700 55906 12318 24636 49272 98544 97594 95694 91894 84294 69094 38694 77388 55282 11070 22140 44280 88560 77626 55758 12022 24044 48088 96176 92858 86222 72950 46406 92812 86130 72766..." }, { "input": "99998", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 31074 62148 24298 48596 97192 94386 88774 77550 55102 10206 20412 40824 81648 63298 26598 53196 6394 12788 25576 51152 2306 4612 9224 18448 36896 73792 47586 95172 90346 80694 61390 22782 45564 91128 82258 64518 29038 58076 16154 32308 64616 29234 58468 16938 33876 67752 35506 71012 42026 84052 68106 36214 72428 44858 89716 79434 58870 17742 35484 70968 41938 83876 67754 35510 71020 42042 84084 68170 36342 72684 45370 90740 81482 62966 25..." }, { "input": "90248", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 40824 81648 73048 55848 21448 42896 85792 81336 72424 54600 18952 37904 75808 61368 32488 64976 39704 79408 68568 46888 3528 7056 14112 28224 56448 22648 45296 344 688 1376 2752 5504 11008 22016 44032 88064 85880 81512 72776 55304 20360 40720 81440 72632 55016 19784 39568 79136 68024 45800 1352 2704 5408 10816 21632 43264 86528 82808 75368 60488 30728 61456 32664 65328 40408 80816 71384 52520 14792 29584 59168 28088 56176 22104 44208 8841..." }, { "input": "99994", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 31078 62156 24318 48636 97272 94550 89106 78218 56442 12890 25780 51560 3126 6252 12504 25008 50016 38 76 152 304 608 1216 2432 4864 9728 19456 38912 77824 55654 11314 22628 45256 90512 81030 62066 24138 48276 96552 93110 86226 72458 44922 89844 79694 59394 18794 37588 75176 50358 722 1444 2888 5776 11552 23104 46208 92416 84838 69682 39370 78740 57486 14978 29956 59912 19830 39660 79320 58646 17298 34596 69192 38390 76780 53566 7138 1427..." }, { "input": "100000", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 31072 62144 24288 48576 97152 94304 88608 77216 54432 8864 17728 35456 70912 41824 83648 67296 34592 69184 38368 76736 53472 6944 13888 27776 55552 11104 22208 44416 88832 77664 55328 10656 21312 42624 85248 70496 40992 81984 63968 27936 55872 11744 23488 46976 93952 87904 75808 51616 3232 6464 12928 25856 51712 3424 6848 13696 27392 54784 9568 19136 38272 76544 53088 6176 12352 24704 49408 98816 97632 95264 90528 81056 62112 24224 48448 ..." }, { "input": "98300", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 32772 65544 32788 65576 32852 65704 33108 66216 34132 68264 38228 76456 54612 10924 21848 43696 87392 76484 54668 11036 22072 44144 88288 78276 58252 18204 36408 72816 47332 94664 91028 83756 69212 40124 80248 62196 26092 52184 6068 12136 24272 48544 97088 95876 93452 88604 78908 59516 20732 41464 82928 67556 36812 73624 48948 97896 97492 96684 95068 91836 85372 72444 46588 93176 88052 77804 57308 16316 32632 65264 32228 64456 30612 61224..." }, { "input": "95324", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 35748 71496 47668 12 24 48 96 192 384 768 1536 3072 6144 12288 24576 49152 2980 5960 11920 23840 47680 36 72 144 288 576 1152 2304 4608 9216 18432 36864 73728 52132 8940 17880 35760 71520 47716 108 216 432 864 1728 3456 6912 13824 27648 55296 15268 30536 61072 26820 53640 11956 23912 47824 324 648 1296 2592 5184 10368 20736 41472 82944 70564 45804 91608 87892 80460 65596 35868 71736 48148 972 1944 3888 7776 15552 31104 62208 29092 58184 2..." }, { "input": "87380", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 43692 5 10 20 40 80 160 320 640 1280 2560 5120 10240 20480 40960 81920 76460 65540 43700 21 42 84 168 336 672 1344 2688 5376 10752 21504 43008 86016 84652 81924 76468 65556 43732 85 170 340 680 1360 2720 5440 10880 21760 43520 87040 86700 86020 84660 81940 76500 65620 43860 341 682 1364 2728 5456 10912 21824 43648 87296 87212 87044 86708 86036 84692 82004 76628 65876 44372 1365 2730 5460 10920 21840 43680 87360 87340 87300 87220 87060 867..." }, { "input": "86036", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 45036 4036 8072 16144 32288 64576 43116 196 392 784 1568 3136 6272 12544 25088 50176 14316 28632 57264 28492 56984 27932 55864 25692 51384 16732 33464 66928 47820 9604 19208 38416 76832 67628 49220 12404 24808 49616 13196 26392 52784 19532 39064 78128 70220 54404 22772 45544 5052 10104 20208 40416 80832 75628 65220 44404 2772 5544 11088 22176 44352 2668 5336 10672 21344 42688 85376 84716 83396 80756 75476 64916 43796 1556 3112 6224 12448 ..." }, { "input": "81914", "output": "0 1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 49158 16402 32804 65608 49302 16690 33380 66760 51606 21298 42596 3278 6556 13112 26224 52448 22982 45964 10014 20028 40056 80112 78310 74706 67498 53082 24250 48500 15086 30172 60344 38774 77548 73182 64450 46986 12058 24116 48232 14550 29100 58200 34486 68972 56030 30146 60292 38670 77340 72766 63618 45322 8730 17460 34920 69840 57766 33618 67236 52558 23202 46404 10894 21788 43576 5238 10476 20952 41904 1894 3788 7576 15152 30304 60608..." } ]
60
0
0
17,719
467
Alex and Complicated Task
[ "data structures", "dp", "greedy" ]
null
null
After you have read all the problems, probably, you think Alex is genius person. That's true! One day he came up with the following task. Given a sequence of integer numbers *a*1,<=*a*2,<=...,<=*a**n*. You are to find a longest sequence *b*1,<=*b*2,<=...,<=*b*4*m*, that satisfies the following conditions: - *b*4*k*<=+<=1<==<=*b*4*k*<=+<=3 for all valid integer *k*; - *b*4*k*<=+<=2<==<=*b*4*k*<=+<=4 for all valid integer *k*; - sequence *b* is subsequence of *a* (not necessarily contiguous subsequence). And finally... Alex had given this complicated task to George, and George gave it to you. Help George to cope with the task.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=5·105). The next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
In the first line print a single integer 4*m* — the maximal possible length of required sequence *b*. In the second line print 4*m* integers *b*1,<=*b*2,<=...,<=*b*4*m*, that is required sequence. If there are multiple optimal answers you may print any of them.
[ "4\n3 5 3 5\n", "10\n35 1 2 1 2 35 100 200 100 200\n" ]
[ "4\n3 5 3 5\n", "8\n1 2 1 2 100 200 100 200\n" ]
none
[ { "input": "4\n3 5 3 5", "output": "4\n3 5 3 5" }, { "input": "10\n35 1 2 1 2 35 100 200 100 200", "output": "8\n1 2 1 2 100 200 100 200" }, { "input": "9\n20 12 9 8 13 7 4 9 8", "output": "4\n9 8 9 8" }, { "input": "14\n15 11 1 16 12 10 8 2 13 14 10 8 2 18", "output": "4\n10 8 10 8" }, { "input": "9\n10 8 15 3 3 10 3 20 3", "output": "4\n10 3 10 3" }, { "input": "8\n1 1 1 1 1 1 1 1", "output": "8\n1 1 1 1 1 1 1 1" }, { "input": "14\n1 2 3 4 5 6 7 8 9 10 11 12 13 14", "output": "0" }, { "input": "20\n1 2 4 5 1 2 4 5 3 4 5 1 1 100 1 100 100 100 19 20", "output": "12\n1 2 1 2 4 5 4 5 1 100 1 100" }, { "input": "58\n95 96 61 103 28 89 92 36 32 68 90 48 67 85 69 30 10 97 62 76 62 47 15 107 68 49 61 20 104 104 91 90 51 82 30 44 92 31 79 74 91 101 34 79 53 73 107 98 43 107 18 54 93 36 23 53 12 30", "output": "4\n61 90 61 90" }, { "input": "85\n78 69 59 16 74 20 73 56 16 84 42 19 66 82 34 66 40 43 51 64 37 62 96 26 102 109 94 59 45 12 45 34 96 51 103 22 38 27 91 39 81 100 105 94 64 41 47 53 95 81 107 80 33 97 103 79 69 106 69 57 40 68 97 104 34 50 104 38 63 64 86 27 89 75 78 108 35 69 68 44 67 33 67 77 66", "output": "8\n59 34 59 34 103 97 103 97" }, { "input": "1\n1", "output": "0" }, { "input": "2\n1 2", "output": "0" }, { "input": "3\n1 2 3", "output": "0" }, { "input": "4\n1 2 3 4", "output": "0" }, { "input": "6\n1 2 3 4 1 4", "output": "4\n1 4 1 4" }, { "input": "1\n96952033", "output": "0" }, { "input": "2\n28402090 334590052", "output": "0" }, { "input": "10\n10 20 20 10 30 20 10 10 20 30", "output": "4\n10 20 10 20" }, { "input": "10\n1 2 3 44 55 44 55 3 2 1", "output": "4\n44 55 44 55" }, { "input": "5\n1 2 3 2 1", "output": "0" }, { "input": "92\n24 26 21 15 29 22 26 5 6 17 28 17 9 21 16 8 21 26 5 20 4 10 28 8 24 25 14 9 9 28 24 23 4 20 18 3 25 6 18 24 5 21 20 13 10 23 12 29 29 24 25 3 9 16 25 3 16 26 12 10 11 19 6 26 10 12 24 28 23 4 10 6 7 18 21 19 24 15 16 5 10 3 29 5 18 17 19 1 16 2 6 15", "output": "24\n26 21 26 21 8 28 8 28 24 20 24 20 25 3 25 3 26 10 26 10 24 10 24 10" }, { "input": "21\n23 25 11 24 2 21 23 12 1 26 29 6 17 24 21 11 12 27 13 8 27", "output": "4\n23 24 23 24" }, { "input": "79\n23 18 18 26 11 29 18 29 1 12 7 20 20 11 7 5 3 15 29 19 20 27 25 21 1 19 25 26 17 20 7 21 1 25 19 29 1 6 9 16 17 7 11 26 12 22 2 4 5 23 1 4 13 5 12 28 24 13 21 8 21 4 20 5 29 25 9 13 24 11 7 16 5 9 3 22 16 24 3", "output": "24\n18 29 18 29 7 20 7 20 25 21 25 21 1 4 1 4 13 5 13 5 9 16 9 16" }, { "input": "1\n1", "output": "0" }, { "input": "3\n17 14 3", "output": "0" }, { "input": "9\n1 9 13 7 54 7 9 13 1", "output": "4\n9 13 9 13" } ]
61
2,867,200
-1
17,766
1,007
Pave the Parallelepiped
[ "bitmasks", "brute force", "combinatorics", "math", "number theory" ]
null
null
You are given a rectangular parallelepiped with sides of positive integer lengths $A$, $B$ and $C$. Find the number of different groups of three integers ($a$, $b$, $c$) such that $1\leq a\leq b\leq c$ and parallelepiped $A\times B\times C$ can be paved with parallelepipeds $a\times b\times c$. Note, that all small parallelepipeds have to be rotated in the same direction. For example, parallelepiped $1\times 5\times 6$ can be divided into parallelepipeds $1\times 3\times 5$, but can not be divided into parallelepipeds $1\times 2\times 3$.
The first line contains a single integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. Each of the next $t$ lines contains three integers $A$, $B$ and $C$ ($1 \leq A, B, C \leq 10^5$) — the sizes of the parallelepiped.
For each test case, print the number of different groups of three points that satisfy all given conditions.
[ "4\n1 1 1\n1 6 1\n2 2 2\n100 100 100\n" ]
[ "1\n4\n4\n165\n" ]
In the first test case, rectangular parallelepiped $(1, 1, 1)$ can be only divided into rectangular parallelepiped with sizes $(1, 1, 1)$. In the second test case, rectangular parallelepiped $(1, 6, 1)$ can be divided into rectangular parallelepipeds with sizes $(1, 1, 1)$, $(1, 1, 2)$, $(1, 1, 3)$ and $(1, 1, 6)$. In the third test case, rectangular parallelepiped $(2, 2, 2)$ can be divided into rectangular parallelepipeds with sizes $(1, 1, 1)$, $(1, 1, 2)$, $(1, 2, 2)$ and $(2, 2, 2)$.
[ { "input": "4\n1 1 1\n1 6 1\n2 2 2\n100 100 100", "output": "1\n4\n4\n165" }, { "input": "10\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1\n1 1 1", "output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n1" }, { "input": "10\n9 6 8\n5 5 2\n8 9 2\n2 7 9\n6 4 10\n1 1 8\n2 8 1\n10 6 3\n7 5 2\n9 5 4", "output": "41\n6\n21\n12\n39\n4\n7\n26\n8\n18" }, { "input": "1\n100000 100000 100000", "output": "8436" } ]
998
8,396,800
3
17,828
725
Family Photos
[ "games", "greedy" ]
null
null
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first. There are *n* stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks. Each photo is described by two non-negative integers *a* and *b*, indicating that it is worth *a* units of happiness to Alice and *b* units of happiness to Bonnie. Values of *a* and *b* might differ for different photos. It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively. The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has *x* happiness and Bonnie has *y* happiness at the end, you should print *x*<=-<=*y*.
The first line of input contains a single integer *n* (1<=≤<=*n*<=≤<=100<=000) — the number of two-photo stacks. Then follow *n* lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers *a*1, *b*1, *a*2 and *b*2, each not exceeding 109. *a*1 and *b*1 describe the top photo in the stack, while *a*2 and *b*2 describe the bottom photo in the stack.
Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.
[ "2\n12 3 4 7\n1 15 9 1\n", "2\n5 4 8 8\n4 12 14 0\n", "1\n0 10 0 10\n" ]
[ "1\n", "4\n", "-10\n" ]
none
[ { "input": "2\n12 3 4 7\n1 15 9 1", "output": "1" }, { "input": "2\n5 4 8 8\n4 12 14 0", "output": "4" }, { "input": "1\n0 10 0 10", "output": "-10" }, { "input": "10\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000\n0 1000000000 0 1000000000", "output": "-10000000000" }, { "input": "10\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0\n1000000000 0 1000000000 0", "output": "10000000000" }, { "input": "10\n7 4 5 3\n6 7 10 1\n1 6 3 7\n3 4 6 0\n9 7 3 9\n9 1 10 10\n5 10 7 6\n8 5 1 9\n1 8 8 4\n7 5 9 4", "output": "5" }, { "input": "10\n31181 6 72281 7\n65255 10 17867 2\n96212 8 32852 10\n69668 6 37236 1\n6031 7 61541 0\n12286 4 40227 2\n30573 9 36317 3\n97429 6 35399 9\n20907 5 19251 8\n86446 8 56301 10", "output": "418488" }, { "input": "10\n3 86311 1 17635\n6 34036 8 37167\n4 53806 6 25588\n3 91425 4 86666\n6 50210 10 7332\n4 59721 10 697\n0 15949 2 66047\n0 68146 10 70354\n2 1075 0 23359\n5 48622 9 70531", "output": "-382702" }, { "input": "1\n618436682 429762104 387908221 774440069", "output": "-41853883" }, { "input": "2\n161177104 219082322 609600790 834825669\n28637550 764143871 832346520 978676472", "output": "0" }, { "input": "2\n252323306 923185060 41887968 12082849\n956989832 311441362 23412620 77648740", "output": "45134543" }, { "input": "3\n90031019 712505278 263580537 367435742\n66829732 915548158 31385852 432626559\n510612253 671761432 753952282 230959195", "output": "-535068510" }, { "input": "4\n632771442 501825496 853869326 796417562\n250298560 151058732 702922596 492637085\n688226592 702784823 444110577 613300642\n809656720 636217627 270335668 655753596", "output": "-104771122" }, { "input": "7\n479456257 682467462 202838061 802929167\n133179116 541657110 703996842 253385760\n559317856 865639323 321812660 580155067\n723036605 467995973 911771109 838534160\n732332969 59715289 216696388 60573917\n515702466 655741352 105418444 651954316\n884987644 907866122 399463262 952777963", "output": "-253460781" }, { "input": "1\n0 0 0 0", "output": "0" } ]
421
6,246,400
3
17,865
271
Prime Matrix
[ "binary search", "brute force", "math", "number theory" ]
null
null
You've got an *n*<=×<=*m* matrix. The matrix consists of integers. In one move, you can apply a single transformation to the matrix: choose an arbitrary element of the matrix and increase it by 1. Each element can be increased an arbitrary number of times. You are really curious about prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors: itself and number one. For example, numbers 2, 3, 5 are prime and numbers 1, 4, 6 are not. A matrix is prime if at least one of the two following conditions fulfills: - the matrix has a row with prime numbers only; - the matrix has a column with prime numbers only; Your task is to count the minimum number of moves needed to get a prime matrix from the one you've got.
The first line contains two integers *n*,<=*m* (1<=≤<=*n*,<=*m*<=≤<=500) — the number of rows and columns in the matrix, correspondingly. Each of the following *n* lines contains *m* integers — the initial matrix. All matrix elements are positive integers. All numbers in the initial matrix do not exceed 105. The numbers in the lines are separated by single spaces.
Print a single integer — the minimum number of moves needed to get a prime matrix from the one you've got. If you've got a prime matrix, print 0.
[ "3 3\n1 2 3\n5 6 1\n4 4 1\n", "2 3\n4 8 8\n9 2 9\n", "2 2\n1 3\n4 2\n" ]
[ "1\n", "3\n", "0\n" ]
In the first sample you need to increase number 1 in cell (1, 1). Thus, the first row will consist of prime numbers: 2, 2, 3. In the second sample you need to increase number 8 in cell (1, 2) three times. Thus, the second column will consist of prime numbers: 11, 2. In the third sample you don't have to do anything as the second column already consists of prime numbers: 3, 2.
[ { "input": "3 3\n1 2 3\n5 6 1\n4 4 1", "output": "1" }, { "input": "2 3\n4 8 8\n9 2 9", "output": "3" }, { "input": "2 2\n1 3\n4 2", "output": "0" }, { "input": "1 1\n14", "output": "3" }, { "input": "5 3\n2 14 8\n8 8 2\n8 10 10\n1 2 1\n100 100 8", "output": "2" }, { "input": "6 5\n8 8 8 8 20\n13 8 16 13 9\n11 5 3 14 18\n13 8 6 18 6\n6 8 9 14 7\n2 14 14 9 3", "output": "4" }, { "input": "5 7\n9 5 3 9 2 8 2\n15 7 9 8 18 4 6\n2 9 14 3 6 13 18\n4 13 3 14 11 13 8\n14 10 1 14 18 5 6", "output": "3" }, { "input": "3 3\n1 7 6\n4 6 2\n3 3 3", "output": "0" }, { "input": "1 5\n14 14 8 14 14", "output": "3" }, { "input": "10 10\n20 53 6 97 74 12 92 48 13 97\n96 47 32 75 21 69 75 95 54 1\n97 36 1 41 87 1 23 39 44 27\n97 73 1 1 26 6 3 48 69 5\n70 93 42 35 5 12 22 33 29 13\n96 8 13 69 40 91 5 19 16 33\n61 92 54 82 60 24 63 64 59 65\n76 44 60 37 29 16 50 74 59 59\n77 58 95 30 52 25 34 2 49 92\n37 65 12 77 68 33 30 39 16 85", "output": "11" }, { "input": "10 1\n19\n9\n14\n14\n19\n5\n5\n18\n10\n17", "output": "0" }, { "input": "8 5\n11 29 11 1 27\n24 16 17 4 19\n13 4 4 20 7\n26 18 23 11 13\n22 20 14 20 16\n18 22 1 22 24\n22 5 11 14 26\n30 16 25 6 9", "output": "3" }, { "input": "5 9\n14 19 30 23 24 27 24 25 4\n12 43 42 30 49 34 33 33 1\n21 41 29 33 31 30 10 41 40\n19 24 14 40 16 34 42 26 19\n3 30 27 32 44 34 3 8 42", "output": "4" }, { "input": "1 1\n99992", "output": "11" }, { "input": "1 1\n100000", "output": "3" }, { "input": "2 2\n10000 99999\n10000 99999", "output": "8" }, { "input": "4 3\n2 1 1\n2 1 1\n2 1 1\n1 1 1", "output": "1" }, { "input": "1 1\n20000", "output": "11" } ]
2,000
13,619,200
0
17,879
49
Sum
[ "math" ]
B. Sum
2
256
Vasya studies positional numeral systems. Unfortunately, he often forgets to write the base of notation in which the expression is written. Once he saw a note in his notebook saying *a*<=+<=*b*<==<=?, and that the base of the positional notation wasn’t written anywhere. Now Vasya has to choose a base *p* and regard the expression as written in the base *p* positional notation. Vasya understood that he can get different results with different bases, and some bases are even invalid. For example, expression 78<=+<=87 in the base 16 positional notation is equal to *FF*16, in the base 15 positional notation it is equal to 11015, in the base 10 one — to 16510, in the base 9 one — to 1769, and in the base 8 or lesser-based positional notations the expression is invalid as all the numbers should be strictly less than the positional notation base. Vasya got interested in what is the length of the longest possible expression value. Help him to find this length. The length of a number should be understood as the number of numeric characters in it. For example, the length of the longest answer for 78<=+<=87<==<=? is 3. It is calculated like that in the base 15 (11015), base 10 (16510), base 9 (1769) positional notations, for example, and in some other ones.
The first letter contains two space-separated numbers *a* and *b* (1<=≤<=*a*,<=*b*<=≤<=1000) which represent the given summands.
Print a single number — the length of the longest answer.
[ "78 87\n", "1 1\n" ]
[ "3\n", "2\n" ]
none
[ { "input": "78 87", "output": "3" }, { "input": "1 1", "output": "2" }, { "input": "9 7", "output": "2" }, { "input": "11 11", "output": "3" }, { "input": "43 21", "output": "3" }, { "input": "84 89", "output": "3" }, { "input": "12 34", "output": "3" }, { "input": "99 11", "output": "3" }, { "input": "11 99", "output": "3" }, { "input": "99 99", "output": "3" }, { "input": "1 2", "output": "2" }, { "input": "1 3", "output": "2" }, { "input": "2 1", "output": "2" }, { "input": "2 2", "output": "2" }, { "input": "2 3", "output": "2" }, { "input": "3 1", "output": "2" }, { "input": "3 2", "output": "2" }, { "input": "3 3", "output": "2" }, { "input": "1 466", "output": "3" }, { "input": "1 1000", "output": "4" }, { "input": "1 999", "output": "4" }, { "input": "149 1", "output": "3" }, { "input": "999 1", "output": "4" }, { "input": "1000 1", "output": "4" }, { "input": "998 998", "output": "4" }, { "input": "998 999", "output": "4" }, { "input": "998 1000", "output": "4" }, { "input": "999 998", "output": "4" }, { "input": "999 999", "output": "4" }, { "input": "999 1000", "output": "4" }, { "input": "1000 998", "output": "4" }, { "input": "1000 999", "output": "4" }, { "input": "1000 1000", "output": "5" }, { "input": "1000 539", "output": "4" }, { "input": "999 619", "output": "4" }, { "input": "2 511", "output": "3" }, { "input": "877 2", "output": "3" }, { "input": "379 999", "output": "4" }, { "input": "247 1000", "output": "4" }, { "input": "555 555", "output": "4" }, { "input": "208 997", "output": "4" }, { "input": "633 581", "output": "4" }, { "input": "411 517", "output": "4" }, { "input": "836 101", "output": "4" }, { "input": "262 685", "output": "4" }, { "input": "39 269", "output": "3" }, { "input": "464 205", "output": "4" }, { "input": "890 789", "output": "4" }, { "input": "667 373", "output": "4" }, { "input": "840 975", "output": "4" }, { "input": "810 413", "output": "4" }, { "input": "133 851", "output": "4" }, { "input": "104 938", "output": "4" }, { "input": "427 376", "output": "4" }, { "input": "398 815", "output": "4" }, { "input": "721 253", "output": "4" }, { "input": "692 339", "output": "4" }, { "input": "15 778", "output": "3" }, { "input": "986 216", "output": "4" }, { "input": "450 277", "output": "3" }, { "input": "333 1", "output": "4" }, { "input": "499 499", "output": "3" }, { "input": "79 1", "output": "2" }, { "input": "87 1", "output": "2" }, { "input": "47 8", "output": "2" }, { "input": "87 8", "output": "3" }, { "input": "1 11", "output": "3" } ]
154
0
3.9615
17,945
729
Subordinates
[ "constructive algorithms", "data structures", "graphs", "greedy", "sortings" ]
null
null
There are *n* workers in a company, each of them has a unique id from 1 to *n*. Exaclty one of them is a chief, his id is *s*. Each worker except the chief has exactly one immediate superior. There was a request to each of the workers to tell how how many superiors (not only immediate). Worker's superiors are his immediate superior, the immediate superior of the his immediate superior, and so on. For example, if there are three workers in the company, from which the first is the chief, the second worker's immediate superior is the first, the third worker's immediate superior is the second, then the third worker has two superiors, one of them is immediate and one not immediate. The chief is a superior to all the workers except himself. Some of the workers were in a hurry and made a mistake. You are to find the minimum number of workers that could make a mistake.
The first line contains two positive integers *n* and *s* (1<=≤<=*n*<=≤<=2·105, 1<=≤<=*s*<=≤<=*n*) — the number of workers and the id of the chief. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=*n*<=-<=1), where *a**i* is the number of superiors (not only immediate) the worker with id *i* reported about.
Print the minimum number of workers that could make a mistake.
[ "3 2\n2 0 2\n", "5 3\n1 0 0 4 1\n" ]
[ "1\n", "2\n" ]
In the first example it is possible that only the first worker made a mistake. Then: - the immediate superior of the first worker is the second worker, - the immediate superior of the third worker is the first worker, - the second worker is the chief.
[ { "input": "3 2\n2 0 2", "output": "1" }, { "input": "5 3\n1 0 0 4 1", "output": "2" }, { "input": "1 1\n0", "output": "0" }, { "input": "2 1\n0 0", "output": "1" }, { "input": "2 1\n0 1", "output": "0" }, { "input": "2 1\n1 0", "output": "2" }, { "input": "2 1\n1 1", "output": "1" }, { "input": "2 2\n0 0", "output": "1" }, { "input": "2 2\n0 1", "output": "2" }, { "input": "9 1\n0 1 1 1 1 1 6 7 8", "output": "3" }, { "input": "9 1\n0 1 1 1 1 5 6 7 8", "output": "3" }, { "input": "6 1\n0 1 2 2 0 0", "output": "2" }, { "input": "2 2\n1 1", "output": "1" }, { "input": "2 2\n1 0", "output": "0" }, { "input": "3 1\n0 1 2", "output": "0" }, { "input": "3 1\n2 1 1", "output": "1" }, { "input": "3 1\n0 0 2", "output": "1" }, { "input": "3 2\n2 0 1", "output": "0" }, { "input": "3 2\n2 2 1", "output": "1" }, { "input": "3 2\n2 1 1", "output": "1" }, { "input": "3 3\n1 1 0", "output": "0" }, { "input": "3 3\n2 1 2", "output": "1" }, { "input": "3 3\n2 1 0", "output": "0" }, { "input": "3 2\n2 2 2", "output": "2" }, { "input": "5 5\n0 1 1 0 0", "output": "2" }, { "input": "7 1\n4 4 6 6 6 6 5", "output": "4" }, { "input": "10 6\n3 0 0 0 0 0 0 1 0 0", "output": "7" }, { "input": "5 1\n0 0 1 3 4", "output": "1" }, { "input": "9 1\n0 0 0 2 5 5 5 5 5", "output": "3" }, { "input": "6 1\n5 2 1 3 3 1", "output": "1" }, { "input": "3 1\n1 2 2", "output": "2" } ]
62
0
0
17,960
592
BCPC
[ "binary search", "geometry", "two pointers" ]
null
null
BCPC stands for Byteforces Collegiate Programming Contest, and is the most famous competition in Byteforces. BCPC is a team competition. Each team is composed by a coach and three contestants. Blenda is the coach of the Bit State University(BSU), and she is very strict selecting the members of her team. In BSU there are *n* students numbered from 1 to *n*. Since all BSU students are infinitely smart, the only important parameters for Blenda are their reading and writing speed. After a careful measuring, Blenda have found that the *i*-th student have a reading speed equal to *r**i* (words per minute), and a writing speed of *w**i* (symbols per minute). Since BSU students are very smart, the measured speeds are sometimes very big and Blenda have decided to subtract some constant value *c* from all the values of reading speed and some value *d* from all the values of writing speed. Therefore she considers *r**i*'<==<=*r**i*<=-<=*c* and *w**i*'<==<=*w**i*<=-<=*d*. The student *i* is said to overwhelm the student *j* if and only if *r**i*'·*w**j*'<=&gt;<=*r**j*'·*w**i*'. Blenda doesn’t like fights in teams, so she thinks that a team consisting of three distinct students *i*,<=*j* and *k* is good if *i* overwhelms *j*, *j* overwhelms *k*, and *k* overwhelms *i*. Yes, the relation of overwhelming is not transitive as it often happens in real life. Since Blenda is busy preparing a training camp in Codeforces, you are given a task to calculate the number of different good teams in BSU. Two teams are considered to be different if there is at least one student that is present in one team but is not present in the other. In other words, two teams are different if the sets of students that form these teams are different.
In the first line of the input three integers *n*, *c* and *d* (3<=≤<=*n*<=≤<=345678,<=1<=≤<=*c*,<=*d*<=≤<=109) are written. They denote the number of students Blenda can use to form teams, the value subtracted from all reading speeds and the value subtracted from all writing speeds respectively. Each of the next *n* lines contains two integers *r**i* and *w**i* (0<=&lt;<=*r**i*,<=*w**i*<=≤<=109,<=|*r**i*<=-<=*c*|<=+<=|*w**i*<=-<=*d*|<=&gt;<=0). There are no two students, such that both their reading and writing speeds coincide, i.e. for every *i*<=≠<=*j* condition |*r**i*<=-<=*r**j*|<=+<=|*w**i*<=-<=*w**j*|<=&gt;<=0 holds.
Print the number of different teams in BSU, that are good according to Blenda's definition.
[ "5 2 2\n1 1\n4 1\n2 3\n3 2\n3 4\n", "7 6 6\n3 2\n1 7\n5 7\n3 7\n6 4\n8 9\n8 5\n" ]
[ "4\n", "11\n" ]
In the first sample the following teams are good: (*i* = 1, *j* = 2, *k* = 3), (*i* = 2, *j* = 5, *k* = 1), (*i* = 1, *j* = 4, *k* = 3), (*i* = 5, *j* = 1, *k* = 4). Note, that for example the team (*i* = 3, *j* = 1, *k* = 2) is also good, but is considered to be the same as the team (*i* = 1, *j* = 2, *k* = 3).
[]
4,000
307,200
0
17,982
709
Checkpoints
[ "greedy", "implementation", "sortings" ]
null
null
Vasya takes part in the orienteering competition. There are *n* checkpoints located along the line at coordinates *x*1,<=*x*2,<=...,<=*x**n*. Vasya starts at the point with coordinate *a*. His goal is to visit at least *n*<=-<=1 checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order. Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.
The first line of the input contains two integers *n* and *a* (1<=≤<=*n*<=≤<=100<=000, <=-<=1<=000<=000<=≤<=*a*<=≤<=1<=000<=000) — the number of checkpoints and Vasya's starting position respectively. The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (<=-<=1<=000<=000<=≤<=*x**i*<=≤<=1<=000<=000) — coordinates of the checkpoints.
Print one integer — the minimum distance Vasya has to travel in order to visit at least *n*<=-<=1 checkpoint.
[ "3 10\n1 7 12\n", "2 0\n11 -10\n", "5 0\n0 0 1000 0 0\n" ]
[ "7\n", "10\n", "0\n" ]
In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is 12 - 10 = 2) and then proceed to the second one (distance is 12 - 7 = 5). The total distance is equal to 2 + 5 = 7. In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point  - 10.
[ { "input": "3 10\n1 7 12", "output": "7" }, { "input": "2 0\n11 -10", "output": "10" }, { "input": "5 0\n0 0 1000 0 0", "output": "0" }, { "input": "1 0\n0", "output": "0" }, { "input": "2 1\n4 -8", "output": "3" }, { "input": "3 4\n4 2 4", "output": "0" }, { "input": "4 7\n8 -4 -10 6", "output": "13" }, { "input": "5 7\n8 3 1 6 4", "output": "6" }, { "input": "6 -9\n-10 -7 4 -7 0 3", "output": "13" }, { "input": "9 -3\n-10 -7 -3 -4 7 -6 10 -10 -7", "output": "24" }, { "input": "18 8\n19 18 20 11 16 12 20 17 15 11 16 13 20 13 14 16 10 12", "output": "12" }, { "input": "26 3\n20 15 19 20 17 10 15 20 16 14 19 12 18 15 14 16 13 13 20 12 12 13 12 18 15 11", "output": "17" }, { "input": "40 4\n10 13 15 17 13 12 16 20 14 17 11 11 18 12 18 19 19 16 13 13 12 15 14 12 10 15 16 11 13 12 12 15 10 17 13 10 12 19 14 15", "output": "15" }, { "input": "44 4\n11 15 10 19 14 13 14 11 11 13 13 14 15 17 18 19 11 13 14 17 12 16 19 19 13 20 14 12 13 14 12 13 14 14 10 20 17 16 12 11 14 19 11 12", "output": "16" }, { "input": "51 5\n20 20 10 17 11 15 12 11 12 19 17 12 16 11 18 11 14 16 11 19 13 13 20 14 18 13 20 18 13 15 12 12 10 11 13 19 12 11 14 17 14 19 18 18 14 13 10 12 16 18 20", "output": "15" }, { "input": "65 13\n14 10 16 19 12 12 10 14 20 11 12 11 17 12 11 14 20 20 16 12 17 14 11 20 10 11 10 13 10 20 19 14 14 11 10 18 10 18 13 10 20 20 10 13 16 10 12 10 12 17 16 18 10 10 12 16 10 13 15 20 13 12 19 20 16", "output": "13" }, { "input": "73 14\n11 19 16 17 10 14 15 13 19 15 12 10 17 13 11 17 12 20 17 10 18 18 12 12 16 15 16 19 10 14 19 16 17 18 17 18 16 20 18 16 19 20 20 20 18 19 11 12 11 15 13 16 12 18 11 20 19 10 19 16 14 11 18 14 13 19 16 11 11 10 12 16 18", "output": "14" }, { "input": "87 1\n16 10 11 15 11 16 16 16 16 14 14 12 14 10 18 10 18 13 18 11 11 19 13 11 19 17 14 20 10 12 16 15 18 16 19 13 10 19 10 15 11 17 13 16 13 15 13 10 12 13 20 10 18 19 11 20 14 14 12 14 17 10 15 11 17 13 16 19 12 10 14 15 20 10 17 14 19 11 20 10 17 15 20 12 10 14 14", "output": "19" }, { "input": "91 25\n19 15 20 12 18 11 18 12 20 11 16 18 13 11 11 13 13 14 15 16 13 11 13 19 15 13 10 15 10 12 13 11 18 11 14 10 11 11 20 14 11 11 14 10 14 19 13 16 19 12 18 18 14 15 10 14 16 11 11 14 12 14 14 20 16 15 17 17 12 15 12 15 20 16 10 18 15 15 19 18 19 18 12 10 11 15 20 20 15 16 15", "output": "15" }, { "input": "11 -28\n-20 -18 -12 -11 -13 -14 -13 -17 -14 -10 -10", "output": "18" }, { "input": "28 -20\n-11 -16 -16 -18 -15 -12 -18 -13 -19 -20 -19 -20 -18 -15 -18 -15 -11 -10 -20 -17 -15 -14 -16 -13 -10 -16 -20 -17", "output": "10" }, { "input": "38 -29\n-13 -13 -10 -12 -13 -13 -13 -18 -18 -20 -19 -14 -17 -16 -19 -13 -10 -14 -19 -12 -16 -17 -11 -12 -12 -13 -18 -12 -11 -18 -15 -20 -20 -14 -13 -17 -12 -12", "output": "19" }, { "input": "45 -21\n-19 -13 -10 -16 -15 -18 -18 -18 -13 -19 -19 -15 -17 -17 -16 -16 -11 -19 -20 -12 -17 -12 -16 -18 -10 -17 -12 -18 -15 -20 -10 -16 -20 -17 -11 -18 -20 -10 -19 -11 -16 -18 -15 -16 -18", "output": "11" }, { "input": "59 -3\n-12 -15 -17 -12 -14 -13 -20 -15 -18 -19 -12 -16 -20 -17 -10 -15 -18 -12 -20 -20 -14 -15 -11 -13 -20 -19 -14 -16 -19 -15 -16 -12 -20 -12 -15 -16 -12 -19 -13 -16 -13 -17 -15 -13 -10 -13 -17 -17 -13 -13 -14 -12 -13 -18 -17 -18 -15 -14 -15", "output": "17" }, { "input": "61 -27\n-14 -15 -11 -20 -13 -15 -14 -19 -17 -18 -16 -11 -16 -18 -11 -17 -13 -17 -13 -19 -15 -14 -14 -12 -19 -16 -13 -15 -13 -20 -18 -15 -17 -14 -13 -10 -20 -17 -10 -13 -16 -12 -11 -19 -15 -10 -13 -13 -15 -20 -13 -15 -18 -11 -13 -19 -13 -17 -11 -16 -12", "output": "17" }, { "input": "76 -20\n-20 -12 -19 -13 -14 -19 -19 -19 -12 -17 -12 -16 -19 -19 -19 -16 -10 -18 -16 -19 -16 -10 -16 -11 -18 -13 -11 -10 -13 -11 -13 -10 -18 -20 -13 -15 -13 -19 -15 -18 -20 -10 -11 -20 -10 -11 -16 -17 -13 -12 -11 -14 -13 -16 -19 -13 -10 -11 -17 -19 -10 -10 -14 -13 -12 -15 -10 -10 -20 -20 -15 -14 -19 -18 -11 -17", "output": "10" }, { "input": "89 -6\n-11 -12 -12 -20 -13 -14 -13 -13 -14 -12 -20 -20 -15 -20 -20 -19 -10 -19 -13 -15 -17 -20 -14 -19 -17 -18 -16 -19 -10 -13 -19 -10 -18 -12 -18 -13 -17 -17 -19 -18 -13 -20 -19 -10 -15 -15 -12 -20 -14 -20 -14 -17 -18 -13 -15 -14 -10 -14 -20 -13 -16 -10 -20 -18 -15 -15 -15 -16 -19 -13 -15 -18 -18 -11 -13 -19 -18 -20 -12 -13 -11 -14 -10 -10 -14 -15 -15 -12 -13", "output": "14" }, { "input": "97 -13\n-17 -11 -20 -16 -11 -18 -17 -14 -19 -13 -11 -19 -15 -17 -11 -20 -10 -19 -10 -18 -11 -14 -11 -15 -13 -13 -11 -18 -18 -18 -11 -14 -20 -19 -10 -17 -18 -19 -17 -11 -11 -11 -20 -19 -16 -15 -18 -15 -15 -20 -19 -20 -16 -13 -17 -11 -13 -18 -10 -13 -10 -14 -12 -17 -20 -10 -11 -19 -19 -15 -19 -10 -18 -14 -15 -15 -11 -13 -20 -10 -10 -16 -18 -15 -14 -13 -13 -11 -16 -10 -16 -18 -11 -19 -11 -14 -18", "output": "13" }, { "input": "1 -42\n-100", "output": "0" }, { "input": "1 1000000\n-1000000", "output": "0" }, { "input": "1 -1000000\n1000000", "output": "0" }, { "input": "1 89765\n89782", "output": "0" }, { "input": "1 -78621\n-77777", "output": "0" }, { "input": "5 10\n1 1 1 1 1", "output": "9" }, { "input": "1 5\n0", "output": "0" }, { "input": "1 100\n300", "output": "0" }, { "input": "2 2\n2 2", "output": "0" }, { "input": "1 10\n1", "output": "0" }, { "input": "1 10\n7", "output": "0" }, { "input": "1 100\n1", "output": "0" }, { "input": "1 5\n-1", "output": "0" }, { "input": "5 3\n-100000 1 1 2 10", "output": "11" }, { "input": "3 1000\n0 1001 4004", "output": "1002" }, { "input": "6 51\n0 50 50 50 50 60", "output": "11" }, { "input": "6 4\n-100 1 2 3 4 5", "output": "5" }, { "input": "1 7\n7", "output": "0" }, { "input": "6 -2\n-3 1 2 3 4 6", "output": "8" }, { "input": "5 6\n-1000000 2 3 4 1000", "output": "1002" } ]
109
7,372,800
3
18,063
132
Logo Turtle
[ "dp" ]
null
null
A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands "T" ("turn around") and "F" ("move 1 unit forward"). You are given a list of commands that will be given to the turtle. You have to change exactly *n* commands from the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?
The first line of input contains a string *commands* — the original list of commands. The string *commands* contains between 1 and 100 characters, inclusive, and contains only characters "T" and "F". The second line contains an integer *n* (1<=≤<=*n*<=≤<=50) — the number of commands you have to change in the list.
Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list.
[ "FT\n1\n", "FFFTFFF\n2\n" ]
[ "2\n", "6\n" ]
In the first example the best option is to change the second command ("T") to "F" — this way the turtle will cover a distance of 2 units. In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.
[ { "input": "FT\n1", "output": "2" }, { "input": "FFFTFFF\n2", "output": "6" }, { "input": "F\n1", "output": "0" }, { "input": "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\n50", "output": "100" }, { "input": "FTFTFTFFFFTFTFTTTTTTFFTTTTFFTFFFTFTFTFFTFTFTFFFTTTFTTFTTTTTFFFFTTT\n12", "output": "41" }, { "input": "TTFFTFTTFTTTFFFTFTFFTFFTTFFTFTFTFTFFTTTFTFFTFFTTTTFTTTFFT\n46", "output": "57" }, { "input": "TTFFFFFFFTTTTFTTFTFFTTFFFTFTTTFFFFTFFFTFTTTFTTF\n24", "output": "47" }, { "input": "FFTTFTTFFFTFFFFTFTTFFTTFTFFFTFFTFTFTTT\n46", "output": "37" }, { "input": "FFTFTTFTFTFTFFTTFTFFTFTFTTTT\n1", "output": "6" }, { "input": "TFFTTFTFFTFFTTTTFTF\n19", "output": "18" }, { "input": "TTFFFFTTF\n22", "output": "9" }, { "input": "FTFTFFFTTTFFTTTFTTFTTTFTTTTFTTFFFTFTFTFTFFFTFTFFTTTFFTFTFTFTTFTTTTFFTTTTTFFFFTFTTFFTFFFTTTFFTFF\n18", "output": "61" }, { "input": "FTTFTTTFTTFFFTFFTTFTFTTTFFFTFTFTFFTTTFFFFFFTFTTFFTFFFFTFTTFFFTFFTTTTTFFTFTTFFFTFFTFFTF\n16", "output": "61" }, { "input": "TFFTFTTTFFTFTFTTTFFTTFFTFTFFTFFFFTTTTTFTFTFTTFFTTFTFFTTFTFFTTTTFFTFTTTFTTTTFFFFTFFTFFTFFFFTTTT\n2", "output": "19" }, { "input": "FTTFFTFFFFTTFFFTFFTTFFTTFFTTFFFTTFFTFTFTTFTFFTTTFTTFTTFTFFFFTFTFTFFFFFTTFTFFTTTFFFTF\n50", "output": "83" }, { "input": "FTTTTTFTTTTFTFFTFFFTTTTTTTTFTTFFTTTTFFTFTFTFFTFTFTTFTFTFTFFTFTFTFFTFFTTTTTT\n4", "output": "20" }, { "input": "TTTFTFTTTFTTFFTFFFFTTTFFFFTTFTFTFFFTTTTFFTTTFFTFTFTFFTTTFFTTFFTFT\n27", "output": "58" }, { "input": "TFFTFFFFTFFFTFFFFFTFFFFFFTFFTTTTFFTTFTTTFFTTFFFFTTFFTFFF\n42", "output": "56" }, { "input": "FFFFTTTTTTFTFFTFFFTFTFTFTFFTFFTFFTFTTFTTTFFFTF\n48", "output": "45" }, { "input": "FFFTTTFTTFFTTFFFTFFFFFTTFTFFTFTTTFFT\n48", "output": "36" }, { "input": "TTTTFFTFTFFFFTTFTFTFTTTTFFF\n43", "output": "26" }, { "input": "TTTFFFFTTTFTFTFFT\n50", "output": "16" }, { "input": "FTFTTFTF\n38", "output": "8" }, { "input": "TFTFFTFTTFFTTTTTTFTFTTFFTTTTTFTFTFTTFFTTFTTTFT\n11", "output": "28" }, { "input": "FFTTTFTFTFFFFTTTFFTFTFTFTFFFFFTTFTFT\n10", "output": "30" }, { "input": "TFFFTFFFTTFTTTFTFFFFFFTTFTF\n10", "output": "26" }, { "input": "TTFFFTTTTFFFFTTTF\n34", "output": "16" }, { "input": "FTTTFTFF\n8", "output": "8" }, { "input": "FTTFTFFTFTTTFTTFTFFTTFFFFFTFFFFTTFTFFFFFFTFFTTTFTTFTTTFFFTFTFTFFFFTFFTFTTTTTTTTFTTTTTTFFTTFTTT\n35", "output": "80" }, { "input": "TFTTFFTTFFTFTTFFTFFTTFTFTTTTTFFFFTFFTTFTTTFFTFTTFFFFTFTTTTFFFFTTTFTFTFTTFTFTFTFTTFTF\n26", "output": "66" }, { "input": "TFFFFFFFFFTTFTTFTFTFFFTFTFTTFTFTFFTFTTFTTFTTFFFTTFTFFFFFTFTFFTFTFFTTTTFTTFT\n13", "output": "54" }, { "input": "FFFTTTTTFTTFTTTFTFTFTTFTFTTFTTFTTTFFFFFFFFTTFTTTTTTFTTFFFFTTFTFFF\n6", "output": "32" }, { "input": "FTTFTTFFFFFTTFFFTFFFFTFTTFFTFTFFTFTFTTTFFTTFFTFTFTTFFFTT\n47", "output": "56" }, { "input": "FTFFTFTTFFTFTFFTTTTTFTFTFTFFFFTTFTTFTFTTTFTTFFFFFFTFTFTFFFTFTT\n10", "output": "38" }, { "input": "FFFTTTFTFTTTFFTTFTTTTTFFFFFTTFTFTFFFFTFFFFFTTTTFFFTF\n21", "output": "47" }, { "input": "TFTFFTTFFTTFTFFTFTFTFFTFTTTFFTTTTTTFTFFTFTF\n27", "output": "43" }, { "input": "TFTTFFFTFFTTFFTTFTTFTFTFFFTTFTTTF\n4", "output": "20" }, { "input": "FTTFTFTFFTTTTFFTFTTFTTFT\n24", "output": "24" }, { "input": "TTFTTTFFFTFFFF\n36", "output": "14" }, { "input": "TTFFF\n49", "output": "4" }, { "input": "FFTFTTFFTTFFFFFFTTTTFFTFFTFFFTTTTTTTTTTFFFTFFTFTFFTTTTTFFFFTTTFFFFTFFFFFTTTFTTFFFTFTFTTTFFT\n40", "output": "87" }, { "input": "FFTTTFFTTTFTTFTFTTFTTFTFTFFTTTTFFFFTFFTFTTFTFFTTTTTTFFTTFFFTTFTTFFTTTTTFFTFFTFTTT\n1", "output": "16" }, { "input": "TFTFFFTTTFFFFFFFTTFTTTFFFTTFFTTTFTTTFTTTTTFTTFFFTTTTTTFFTFFFFTFFTFTFTFFT\n4", "output": "29" }, { "input": "FTTTTTFTFFTTTFFTTFTFFTFTTFTTFFFTTFTTTFFFFFFFTFFTTTTFFTTTFFFFFTFFFFFFFFTFTTFTFT\n6", "output": "40" }, { "input": "TTTFFFTTFTFFFTTTFFFFTTFTTTTFTFFFFTFTTTFTTTFTTFTTFFTFTFTFTFFTTFTFTFTT\n41", "output": "68" }, { "input": "TFTTFFFFFFFTTTFTFFFTTTFTFFFTFTFTFFFTFTFTTTFTTFFTFTTFTFFTFTF\n8", "output": "34" }, { "input": "FFFFTTTTTFFFFTTTFFTTFFTFTTFFTTFFFTTTTFTFFFTFTTFTT\n43", "output": "49" }, { "input": "FFFFTTFFTTFFTTFTFFTTTFTFTFFTFTFTTFFTFTTF\n41", "output": "40" }, { "input": "TTTTFTTFTFFTFTTTTFFTFTFFFTFFTF\n24", "output": "30" }, { "input": "TTTFFFFTTFTFTTTTTFTFF\n30", "output": "21" }, { "input": "FTTTTFTFTTT\n31", "output": "10" }, { "input": "FFFFTTFTTTTFTTTFTFFFFFTTFFTFFTFFTTFTTFFTFTFTTTTFFFTFTTTFTFTFFTFFFTFTFFFFFFFFFTFFTTFFFFFFTFFFFFTTT\n18", "output": "75" }, { "input": "TFFTFTTTTFTTFTFFTFTFTFTTTTTTTTFTTFTTFTFTTFFTTTFFTTTFFTTTTFTFTFFTTTTTFTTFTFTFTTTTFFFTTFTF\n45", "output": "78" }, { "input": "TTFFFFFFTFFFFTFFTFTTTFTFFFFFFFTFFTTFFFTFFFFFTTFFFTTTTFFFFFTFFFTTFTFFTTTTFFFFTFTTTFTFTTTTFFTFTF\n44", "output": "94" }, { "input": "TFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTFTF\n25", "output": "51" }, { "input": "TFFFTTFFF\n2", "output": "8" }, { "input": "TFFFTTTFFFFTFFTTFTTFTTFFTFFFTTTTTTFTTTTTTFFFFFFFTFFFFTTTFTFFTTTTTTFFFFFTTFTFTFFFTFTTFFTTFFFTTFFFTTTT\n1", "output": "17" }, { "input": "TFFFFFTFFFFF\n1", "output": "11" }, { "input": "FFFFTFTF\n1", "output": "5" } ]
216
204,800
0
18,084
582
Number of Binominal Coefficients
[ "dp", "math", "number theory" ]
null
null
For a given prime integer *p* and integers α,<=*A* calculate the number of pairs of integers (*n*,<=*k*), such that 0<=≤<=*k*<=≤<=*n*<=≤<=*A* and is divisible by *p*α. As the answer can be rather large, print the remainder of the answer moduly 109<=+<=7. Let us remind you that is the number of ways *k* objects can be chosen from the set of *n* objects.
The first line contains two integers, *p* and α (1<=≤<=*p*,<=α<=≤<=109, *p* is prime). The second line contains the decimal record of integer *A* (0<=≤<=*A*<=&lt;<=101000) without leading zeroes.
In the single line print the answer to the problem.
[ "2 2\n7\n", "3 1\n9\n", "3 3\n9\n", "2 4\n5000\n" ]
[ "3\n", "17\n", "0\n", "8576851\n" ]
In the first sample three binominal coefficients divisible by 4 are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a4c2b94fb12d1298dafcd1d14d7e1f6a47500264.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/80436d3bb1008bf8943b41f46211501e7fea05e9.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/bb01b873c593007ca5b10a8897a71c4b0d7dabae.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[]
483
268,390,400
0
18,149
242
XOR on Segment
[ "bitmasks", "data structures" ]
null
null
You've got an array *a*, consisting of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. You are allowed to perform two operations on this array: 1. Calculate the sum of current array elements on the segment [*l*,<=*r*], that is, count value *a**l*<=+<=*a**l*<=+<=1<=+<=...<=+<=*a**r*. 1. Apply the xor operation with a given number *x* to each array element on the segment [*l*,<=*r*], that is, execute . This operation changes exactly *r*<=-<=*l*<=+<=1 array elements. Expression means applying bitwise xor operation to numbers *x* and *y*. The given operation exists in all modern programming languages, for example in language C++ and Java it is marked as "^", in Pascal — as "xor". You've got a list of *m* operations of the indicated type. Your task is to perform all given operations, for each sum query you should print the result you get.
The first line contains integer *n* (1<=≤<=*n*<=≤<=105) — the size of the array. The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=≤<=*a**i*<=≤<=106) — the original array. The third line contains integer *m* (1<=≤<=*m*<=≤<=5·104) — the number of operations with the array. The *i*-th of the following *m* lines first contains an integer *t**i* (1<=≤<=*t**i*<=≤<=2) — the type of the *i*-th query. If *t**i*<==<=1, then this is the query of the sum, if *t**i*<==<=2, then this is the query to change array elements. If the *i*-th operation is of type 1, then next follow two integers *l**i*,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). If the *i*-th operation is of type 2, then next follow three integers *l**i*,<=*r**i*,<=*x**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*,<=1<=≤<=*x**i*<=≤<=106). The numbers on the lines are separated by single spaces.
For each query of type 1 print in a single line the sum of numbers on the given segment. Print the answers to the queries in the order in which the queries go in the input. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams, or the %I64d specifier.
[ "5\n4 10 3 13 7\n8\n1 2 4\n2 1 3 3\n1 2 4\n1 3 3\n2 2 5 5\n1 1 5\n2 1 2 10\n1 2 3\n", "6\n4 7 4 0 7 3\n5\n2 2 3 8\n1 1 5\n2 3 5 1\n2 4 5 6\n1 2 3\n" ]
[ "26\n22\n0\n34\n11\n", "38\n28\n" ]
none
[]
186
4,710,400
-1
18,182
992
Nastya and a Wardrobe
[ "math" ]
null
null
Nastya received a gift on New Year — a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is in the beginning of the month). Unfortunately, right after the doubling the wardrobe eats one of the dresses (if any) with the 50% probability. It happens every month except the last one in the year. Nastya owns *x* dresses now, so she became interested in the [expected number](https://en.wikipedia.org/wiki/Expected_value) of dresses she will have in one year. Nastya lives in Byteland, so the year lasts for *k*<=+<=1 months. Nastya is really busy, so she wants you to solve this problem. You are the programmer, after all. Also, you should find the answer modulo 109<=+<=7, because it is easy to see that it is always integer.
The only line contains two integers *x* and *k* (0<=≤<=*x*,<=*k*<=≤<=1018), where *x* is the initial number of dresses and *k*<=+<=1 is the number of months in a year in Byteland.
In the only line print a single integer — the expected number of dresses Nastya will own one year later modulo 109<=+<=7.
[ "2 0\n", "2 1\n", "3 2\n" ]
[ "4\n", "7\n", "21\n" ]
In the first example a year consists on only one month, so the wardrobe does not eat dresses at all. In the second example after the first month there are 3 dresses with 50% probability and 4 dresses with 50% probability. Thus, in the end of the year there are 6 dresses with 50% probability and 8 dresses with 50% probability. This way the answer for this test is (6 + 8) / 2 = 7.
[ { "input": "2 0", "output": "4" }, { "input": "2 1", "output": "7" }, { "input": "3 2", "output": "21" }, { "input": "1 411", "output": "485514976" }, { "input": "1 692", "output": "860080936" }, { "input": "16 8", "output": "7937" }, { "input": "18 12", "output": "143361" }, { "input": "1 1000000000000000000", "output": "719476261" }, { "input": "0 24", "output": "0" }, { "input": "24 0", "output": "48" }, { "input": "1000000000000000000 1", "output": "195" }, { "input": "348612312017571993 87570063840727716", "output": "551271547" }, { "input": "314647997243943415 107188213956410843", "output": "109575135" }, { "input": "375000003 2", "output": "0" }, { "input": "451 938", "output": "598946958" }, { "input": "4 1669", "output": "185365669" }, { "input": "24 347", "output": "860029201" }, { "input": "1619 1813", "output": "481568710" }, { "input": "280 472", "output": "632090765" }, { "input": "1271 237", "output": "27878991" }, { "input": "626 560", "output": "399405853" }, { "input": "167 887", "output": "983959273" }, { "input": "1769 422", "output": "698926874" }, { "input": "160 929", "output": "752935252" }, { "input": "1075 274", "output": "476211777" }, { "input": "1332 332", "output": "47520583" }, { "input": "103872254428948073 97291596742897547", "output": "283633261" }, { "input": "157600018563121064 54027847222622605", "output": "166795759" }, { "input": "514028642164226185 95344332761644668", "output": "718282571" }, { "input": "91859547444219924 75483775868568438", "output": "462306789" }, { "input": "295961633522750187 84483303945499729", "output": "11464805" }, { "input": "8814960236468055 86463151557693391", "output": "430718856" }, { "input": "672751296745170589 13026894786355983", "output": "260355651" }, { "input": "909771081413191574 18862935031728197", "output": "800873185" }, { "input": "883717267463724670 29585639347346605", "output": "188389362" }, { "input": "431620727626880523 47616788361847228", "output": "311078131" }, { "input": "816689044159694273 6475970360049048", "output": "211796030" }, { "input": "313779810374175108 13838123840048842", "output": "438854949" }, { "input": "860936792402722414 59551033597232946", "output": "359730003" }, { "input": "332382902893992163 15483141652464187", "output": "719128379" }, { "input": "225761360057436129 49203610094504526", "output": "54291755" }, { "input": "216006901533424028 8313457244750219", "output": "362896012" }, { "input": "568001660010321225 97167523790774710", "output": "907490480" }, { "input": "904089164817530426 53747406876903279", "output": "702270335" }, { "input": "647858974461637674 18385058205826214", "output": "375141527" }, { "input": "720433754707338458 94180351080265292", "output": "273505123" }, { "input": "268086842387268316 76502855388264782", "output": "288717798" }, { "input": "488603693655520686 79239542983498430", "output": "316399174" }, { "input": "152455635055802121 50394545488662355", "output": "697051907" }, { "input": "585664029992038779 34972826534657555", "output": "699566354" }, { "input": "349532090641396787 12248820623854158", "output": "233938854" }, { "input": "353579407209009179 74469254935824590", "output": "771349161" }, { "input": "491414900908765740 49509676303815755", "output": "237095803" }, { "input": "91142854626119420 900651524977956", "output": "211575546" }, { "input": "73543340229981083 66918326344192076", "output": "710215652" }, { "input": "463958371369193376 89203995753927042", "output": "41857490" }, { "input": "911873413622533246 54684577459651780", "output": "926432198" }, { "input": "316313018463929883 78259904441946885", "output": "36284201" }, { "input": "889560480100219043 54181377424922141", "output": "281123162" }, { "input": "0 3259862395629356", "output": "0" }, { "input": "1 3", "output": "9" }, { "input": "3 1", "output": "11" }, { "input": "1000000007 1", "output": "1000000006" }, { "input": "1000000007 2", "output": "1000000004" }, { "input": "1000000007 0", "output": "0" }, { "input": "1000000007 12", "output": "999995912" }, { "input": "1000000007 70", "output": "729983755" }, { "input": "250000002 1", "output": "0" }, { "input": "1000000007 3", "output": "1000000000" }, { "input": "999999999 0", "output": "999999991" }, { "input": "1000000007 5", "output": "999999976" }, { "input": "1000000007 1000000007", "output": "1000000006" }, { "input": "10000000000000000 0", "output": "860000007" }, { "input": "1000000000000 0", "output": "999986007" }, { "input": "99999999999999999 0", "output": "600000012" }, { "input": "1000000000000000 0", "output": "986000007" } ]
93
0
0
18,193
332
Theft of Blueprints
[ "graphs", "math" ]
null
null
Insurgents accidentally got hold of the plan of a top secret research polygon created on a distant planet for the needs of the Galaxy Empire. The insurgents suppose that this polygon is developing new deadly weapon. The polygon consists of *n* missile silos connected by bidirectional underground passages. The passages are linked to laboratories where research is conducted. Naturally, the passages are guarded severely: the passage between silos *i* and *j* is patrolled by *c**i*,<=*j* war droids. The insurgents studied the polygon plan and noticed its unusual structure. As it turned out, for any *k*-element set of silos *S* there is exactly one silo that is directly connected by a passage with each silo from *S* (we'll call this silo adjacent with *S*). Having considered that, the insurgents decided to act as follows: 1. they choose a *k*-element set of silos *S*; 1. a group of scouts lands from the air into each silo from *S*; 1. each group moves along the corresponding passage to the silo, adjacent with *S* (as the scouts move, they check out the laboratories and watch for any signs of weapon blueprints); 1. in the silo, adjacent with *S*, the groups get on the ship and fly away. The danger of the operation is the total number of droids that patrol the passages through which the scouts will go. The danger of the operation obviously only depends on the way to choose set *S*. The insurgents haven't yet decided on the exact silos to send the scouts to. However, they already want to start preparing the weapons for the scout groups. To do that, the insurgents need to know the mathematical average of the dangers of the operations that correspond to all possible ways to choose set *S*. Solve this problem to help the insurgents protect the ideals of the Republic!
The first line contains two integers *n* and *k* (2<=≤<=*n*<=≤<=2000, 1<=≤<=*k*<=≤<=*n*<=-<=1) — the number of silos and the number of scout groups, correspondingly. The next *n*<=-<=1 lines describe the polygon plan: the *i*-th of these lines contains *n*<=-<=*i* integers *c**i*,<=*i*<=+<=1,<=*c**i*,<=*i*<=+<=2,<=...,<=*c**i*,<=*n* — the number of droids that patrol the corresponding passages (-1<=≤<=*c**i*,<=*j*<=≤<=109; if *c**i*,<=*j*<==<= -1, then silos *i* and *j* don't have a passage between them). All passages are bidirectional, that is, we can assume that *c**i*,<=*j*<==<=*c**j*,<=*i*. No passages connect a silo with itself. It is guaranteed that the polygon plan meets the conditions of the problem statement.
Print the average danger of the scouting operation, rounded down to an integer. Note that at the given limits the answer to the problem always fits into the standard integer 64-bit data type. Please do not use the %lld specifier to write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d specifier.
[ "6 1\n-1 -1 -1 8 -1\n-1 5 -1 -1\n-1 -1 3\n-1 -1\n-1\n", "3 2\n10 0\n11\n" ]
[ "5\n", "14\n" ]
In the first sample there are 6 one-element sets of silos. For sets {1}, {5} the operation danger will equal 8, for sets {3}, {6} — 3, for sets {2}, {4} — 5. The mathematical average equals <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3bc833659595c25e73150ed7f23907011961ceca.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the second sample there are 3 two-elements sets of silos: {1, 3} (danger equals 21), {1, 2} (danger equals 11), {2, 3} (danger equals 10). The average operation danger equals <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/02946ba8c525c97041629189ab4acc976074d8d1.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "6 1\n-1 -1 -1 8 -1\n-1 5 -1 -1\n-1 -1 3\n-1 -1\n-1", "output": "5" }, { "input": "3 2\n10 0\n11", "output": "14" }, { "input": "4 3\n15 1 3\n5 8\n9", "output": "20" }, { "input": "5 2\n-1 -1 14 3\n19 -1 1\n-1 6\n0", "output": "10" }, { "input": "10 9\n999999996 999999991 999999997 999999994 999999991 999999992 999999992 1000000000 999999995\n999999999 999999992 1000000000 999999993 999999996 999999996 999999996 999999991\n999999990 999999996 999999998 999999996 999999993 999999995 999999991\n999999998 999999994 999999996 999999994 999999990 999999993\n999999993 999999998 999999993 999999991 999999996\n999999995 999999990 999999992 999999999\n1000000000 999999995 999999991\n999999996 999999998\n999999991", "output": "8999999950" }, { "input": "2 1\n0", "output": "0" }, { "input": "2 1\n1000000000", "output": "1000000000" } ]
1,558
268,390,400
0
18,224
698
Fix a Tree
[ "constructive algorithms", "dfs and similar", "dsu", "graphs", "trees" ]
null
null
A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with *n* vertices, numbered 1 through *n*. There are many ways to represent such a tree. One way is to create an array with *n* integers *p*1,<=*p*2,<=...,<=*p**n*, where *p**i* denotes a parent of vertex *i* (here, for convenience a root is considered its own parent). Given a sequence *p*1,<=*p*2,<=...,<=*p**n*, one is able to restore a tree: 1. There must be exactly one index *r* that *p**r*<==<=*r*. A vertex *r* is a root of the tree. 1. For all other *n*<=-<=1 vertices *i*, there is an edge between vertex *i* and vertex *p**i*. A sequence *p*1,<=*p*2,<=...,<=*p**n* is called valid if the described procedure generates some (any) rooted tree. For example, for *n*<==<=3 sequences (1,2,2), (2,3,1) and (2,1,3) are not valid. You are given a sequence *a*1,<=*a*2,<=...,<=*a**n*, not necessarily valid. Your task is to change the minimum number of elements, in order to get a valid sequence. Print the minimum number of changes and an example of a valid sequence after that number of changes. If there are many valid sequences achievable in the minimum number of changes, print any of them.
The first line of the input contains an integer *n* (2<=≤<=*n*<=≤<=200<=000) — the number of vertices in the tree. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=*n*).
In the first line print the minimum number of elements to change, in order to get a valid sequence. In the second line, print any valid sequence possible to get from (*a*1,<=*a*2,<=...,<=*a**n*) in the minimum number of changes. If there are many such sequences, any of them will be accepted.
[ "4\n2 3 3 4\n", "5\n3 2 2 5 3\n", "8\n2 3 5 4 1 6 6 7\n" ]
[ "1\n2 3 4 4 \n", "0\n3 2 2 5 3 \n", "2\n2 3 7 8 1 6 6 7\n" ]
In the first sample, it's enough to change one element. In the provided output, a sequence represents a tree rooted in a vertex 4 (because *p*<sub class="lower-index">4</sub> = 4), which you can see on the left drawing below. One of other correct solutions would be a sequence 2 3 3 2, representing a tree rooted in vertex 3 (right drawing below). On both drawings, roots are painted red. In the second sample, the given sequence is already valid.
[ { "input": "4\n2 3 3 4", "output": "1\n2 3 4 4 " }, { "input": "5\n3 2 2 5 3", "output": "0\n3 2 2 5 3 " }, { "input": "8\n2 3 5 4 1 6 6 7", "output": "2\n2 3 7 8 1 6 6 7" }, { "input": "2\n1 2", "output": "1\n2 2 " }, { "input": "7\n4 3 2 6 3 5 2", "output": "1\n4 3 3 6 3 5 2 " }, { "input": "6\n6 2 6 2 4 2", "output": "0\n6 2 6 2 4 2 " }, { "input": "7\n1 6 4 4 5 6 7", "output": "4\n7 6 4 7 7 7 7 " }, { "input": "7\n7 5 3 1 2 1 5", "output": "1\n7 5 3 1 3 1 5 " }, { "input": "7\n1 2 3 4 5 6 7", "output": "6\n7 7 7 7 7 7 7 " }, { "input": "18\n2 3 4 5 2 7 8 9 10 7 11 12 14 15 13 17 18 18", "output": "5\n2 18 4 5 2 7 18 9 10 7 18 18 18 15 13 17 18 18 " }, { "input": "8\n2 1 2 2 6 5 6 6", "output": "2\n1 1 2 2 1 5 6 6 " }, { "input": "3\n2 1 1", "output": "1\n1 1 1 " } ]
62
0
0
18,244
379
New Year Tree Decorations
[ "geometry", "schedules", "sortings" ]
null
null
Due to atheistic Soviet past, Christmas wasn't officially celebrated in Russia for most of the twentieth century. As a result, the Russian traditions for Christmas and New Year mixed into one event celebrated on the New Year but including the tree, a Santa-like 'Grandfather Frost', presents and huge family reunions and dinner parties all over the country. Bying a Tree at the New Year and installing it in the house is a tradition. Usually the whole family decorates the tree on the New Year Eve. We hope that Codeforces is a big and loving family, so in this problem we are going to decorate a tree as well. So, our decoration consists of *n* pieces, each piece is a piece of colored paper, its border is a closed polyline of a special shape. The pieces go one by one as is shown on the picture. The *i*-th piece is a polyline that goes through points: (0,<=0), (0,<=*y*0), (1,<=*y*1), (2,<=*y*2), ..., (*k*,<=*y**k*), (*k*,<=0). The width of each piece equals *k*. The piece number 1 (shown red on the figure) is the outer piece (we see it completely), piece number 2 (shown yellow) follows it (we don't see it completely as it is partially closed by the first piece) and so on. The programmers are quite curious guys, so the moment we hung a decoration on the New Year tree we started to wonder: what area of each piece can people see?
The first line contains two integers, *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=300). Each of the following *n* lines contains *k*<=+<=1 integers — the description of the polyline. If the *i*-th line contains ontegers *y**i*,<=0,<=*y**i*,<=1,<=...,<=*y**i*,<=*k*, that means that the polyline of the *i*-th piece goes through points (0,<=0), (0,<=*y**i*,<=0), (1,<=*y**i*,<=1), (2,<=*y**i*,<=2), ..., (*k*,<=*y**i*,<=*k*), (*k*,<=0) (1<=≤<=*y**i*,<=*j*<=≤<=1000).
Print *n* real numbers — for each polyline, the area of its visible part. The answer will be considered correct if its relative or absolute error do not exceed 10<=-<=4.
[ "2 2\n2 1 2\n1 2 1\n", "1 1\n1 1\n", "4 1\n2 7\n7 2\n5 5\n6 4\n" ]
[ "3.000000000000\n0.500000000000\n", "1.000000000000\n", "4.500000000000\n1.250000000000\n0.050000000000\n0.016666666667\n" ]
none
[]
46
0
0
18,247
915
Coprime Arrays
[ "math", "number theory" ]
null
null
Let's call an array *a* of size *n* coprime iff *gcd*(*a*1,<=*a*2,<=...,<=*a**n*)<==<=1, where *gcd* is the greatest common divisor of the arguments. You are given two numbers *n* and *k*. For each *i* (1<=≤<=*i*<=≤<=*k*) you have to determine the number of coprime arrays *a* of size *n* such that for every *j* (1<=≤<=*j*<=≤<=*n*) 1<=≤<=*a**j*<=≤<=*i*. Since the answers can be very large, you have to calculate them modulo 109<=+<=7.
The first line contains two integers *n* and *k* (1<=≤<=*n*,<=*k*<=≤<=2·106) — the size of the desired arrays and the maximum upper bound on elements, respectively.
Since printing 2·106 numbers may take a lot of time, you have to output the answer in such a way: Let *b**i* be the number of coprime arrays with elements in range [1,<=*i*], taken modulo 109<=+<=7. You have to print , taken modulo 109<=+<=7. Here denotes bitwise xor operation (^ in C++ or Java, xor in Pascal).
[ "3 4\n", "2000000 8\n" ]
[ "82\n", "339310063\n" ]
Explanation of the example: Since the number of coprime arrays is large, we will list the arrays that are non-coprime, but contain only elements in range [1, *i*]: For *i* = 1, the only array is coprime. *b*<sub class="lower-index">1</sub> = 1. For *i* = 2, array [2, 2, 2] is not coprime. *b*<sub class="lower-index">2</sub> = 7. For *i* = 3, arrays [2, 2, 2] and [3, 3, 3] are not coprime. *b*<sub class="lower-index">3</sub> = 25. For *i* = 4, arrays [2, 2, 2], [3, 3, 3], [2, 2, 4], [2, 4, 2], [2, 4, 4], [4, 2, 2], [4, 2, 4], [4, 4, 2] and [4, 4, 4] are not coprime. *b*<sub class="lower-index">4</sub> = 55.
[ { "input": "3 4", "output": "82" }, { "input": "2000000 8", "output": "339310063" }, { "input": "1000 1000", "output": "293255159" }, { "input": "400000 400000", "output": "641589365" }, { "input": "1000 2000", "output": "946090030" }, { "input": "400000 800000", "output": "700177418" }, { "input": "1000 3000", "output": "599681537" }, { "input": "400000 1200000", "output": "599314521" }, { "input": "1000 4000", "output": "369962559" }, { "input": "400000 1600000", "output": "383930351" }, { "input": "1000 5000", "output": "792761486" }, { "input": "400000 2000000", "output": "581254555" }, { "input": "2000 1000", "output": "895983599" }, { "input": "800000 400000", "output": "194918098" }, { "input": "2000 2000", "output": "993145929" }, { "input": "800000 800000", "output": "959396582" }, { "input": "2000 3000", "output": "801808235" }, { "input": "800000 1200000", "output": "456936609" }, { "input": "2000 4000", "output": "531261925" }, { "input": "800000 1600000", "output": "58127171" }, { "input": "2000 5000", "output": "460721768" }, { "input": "800000 2000000", "output": "607152577" }, { "input": "3000 1000", "output": "547188062" }, { "input": "1200000 400000", "output": "535771854" }, { "input": "3000 2000", "output": "612556768" }, { "input": "1200000 800000", "output": "925258449" }, { "input": "3000 3000", "output": "113732600" }, { "input": "1200000 1200000", "output": "468573037" }, { "input": "3000 4000", "output": "81501584" }, { "input": "1200000 1600000", "output": "549284678" }, { "input": "3000 5000", "output": "674745410" }, { "input": "1200000 2000000", "output": "340808789" }, { "input": "4000 1000", "output": "312642124" }, { "input": "1600000 400000", "output": "180485439" }, { "input": "4000 2000", "output": "467116350" }, { "input": "1600000 800000", "output": "990942172" }, { "input": "4000 3000", "output": "441359443" }, { "input": "1600000 1200000", "output": "186789429" }, { "input": "4000 4000", "output": "927028696" }, { "input": "1600000 1600000", "output": "373782836" }, { "input": "4000 5000", "output": "107116959" }, { "input": "1600000 2000000", "output": "160805957" }, { "input": "5000 1000", "output": "834732252" }, { "input": "2000000 400000", "output": "313670498" }, { "input": "5000 2000", "output": "273324715" }, { "input": "2000000 800000", "output": "204745061" }, { "input": "5000 3000", "output": "649058184" }, { "input": "2000000 1200000", "output": "749062788" }, { "input": "5000 4000", "output": "332100759" }, { "input": "2000000 1600000", "output": "267687597" }, { "input": "5000 5000", "output": "785334012" }, { "input": "2000000 2000000", "output": "219072617" }, { "input": "1 1", "output": "0" }, { "input": "1 2000000", "output": "986000" }, { "input": "2000000 1", "output": "0" } ]
46
0
0
18,261
993
Nikita and Order Statistics
[ "chinese remainder theorem", "fft", "math" ]
null
null
Nikita likes tasks on order statistics, for example, he can easily find the $k$-th number in increasing order on a segment of an array. But now Nikita wonders how many segments of an array there are such that a given number $x$ is the $k$-th number in increasing order on this segment. In other words, you should find the number of segments of a given array such that there are exactly $k$ numbers of this segment which are less than $x$. Nikita wants to get answer for this question for each $k$ from $0$ to $n$, where $n$ is the size of the array.
The first line contains two integers $n$ and $x$ $(1 \le n \le 2 \cdot 10^5, -10^9 \le x \le 10^9)$. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(-10^9 \le a_i \le 10^9)$ — the given array.
Print $n+1$ integers, where the $i$-th number is the answer for Nikita's question for $k=i-1$.
[ "5 3\n1 2 3 4 5\n", "2 6\n-5 9\n", "6 99\n-1 -1 -1 -1 -1 -1\n" ]
[ "6 5 4 0 0 0 ", "1 2 0 ", "0 6 5 4 3 2 1 " ]
none
[ { "input": "5 3\n1 2 3 4 5", "output": "6 5 4 0 0 0 " }, { "input": "2 6\n-5 9", "output": "1 2 0 " }, { "input": "6 99\n-1 -1 -1 -1 -1 -1", "output": "0 6 5 4 3 2 1 " }, { "input": "5 -2\n-1 -1 -4 -5 1", "output": "4 5 6 0 0 0 " }, { "input": "5 -6\n-4 2 -7 -1 -5", "output": "6 9 0 0 0 0 " }, { "input": "10 29\n88 57 -3 -9 16 48 -84 80 -73 -46", "output": "5 13 11 11 8 4 3 0 0 0 0 " }, { "input": "1 1000000000\n1", "output": "0 1 " }, { "input": "2 -1000000000\n465132 210", "output": "3 0 0 " }, { "input": "10 -8\n7 -1 0 -8 8 -1 -10 -7 4 0", "output": "27 28 0 0 0 0 0 0 0 0 0 " }, { "input": "10 9\n-2 6 0 -6 7 -8 -5 4 -3 3", "output": "0 10 9 8 7 6 5 4 3 2 1 " }, { "input": "10 5\n-3 2 1 -5 -3 6 -5 10 -10 -10", "output": "2 13 11 9 7 6 4 2 1 0 0 " }, { "input": "10 -3\n-7 6 6 9 4 0 3 8 9 -2", "output": "45 10 0 0 0 0 0 0 0 0 0 " }, { "input": "10 -7\n5 5 6 6 7 10 3 -7 -2 5", "output": "55 0 0 0 0 0 0 0 0 0 0 " } ]
30
0
0
18,273
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
15,462,400
0
18,305
1,004
Sonya and Matrix
[ "brute force", "constructive algorithms", "implementation" ]
null
null
Since Sonya has just learned the basics of matrices, she decided to play with them a little bit. Sonya imagined a new type of matrices that she called rhombic matrices. These matrices have exactly one zero, while all other cells have the Manhattan distance to the cell containing the zero. The cells with equal numbers have the form of a rhombus, that is why Sonya called this type so. The Manhattan distance between two cells ($x_1$, $y_1$) and ($x_2$, $y_2$) is defined as $|x_1 - x_2| + |y_1 - y_2|$. For example, the Manhattan distance between the cells $(5, 2)$ and $(7, 1)$ equals to $|5-7|+|2-1|=3$. Note that rhombic matrices are uniquely defined by $n$, $m$, and the coordinates of the cell containing the zero. She drew a $n\times m$ rhombic matrix. She believes that you can not recreate the matrix if she gives you only the elements of this matrix in some arbitrary order (i.e., the sequence of $n\cdot m$ numbers). Note that Sonya will not give you $n$ and $m$, so only the sequence of numbers in this matrix will be at your disposal. Write a program that finds such an $n\times m$ rhombic matrix whose elements are the same as the elements in the sequence in some order.
The first line contains a single integer $t$ ($1\leq t\leq 10^6$) — the number of cells in the matrix. The second line contains $t$ integers $a_1, a_2, \ldots, a_t$ ($0\leq a_i&lt; t$) — the values in the cells in arbitrary order.
In the first line, print two positive integers $n$ and $m$ ($n \times m = t$) — the size of the matrix. In the second line, print two integers $x$ and $y$ ($1\leq x\leq n$, $1\leq y\leq m$) — the row number and the column number where the cell with $0$ is located. If there are multiple possible answers, print any of them. If there is no solution, print the single integer $-1$.
[ "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4\n", "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1\n", "6\n2 1 0 2 1 2\n" ]
[ "4 5\n2 2\n", "3 6\n2 3\n", "-1\n" ]
You can see the solution to the first example in the legend. You also can choose the cell $(2, 2)$ for the cell where $0$ is located. You also can choose a $5\times 4$ matrix with zero at $(4, 2)$. In the second example, there is a $3\times 6$ matrix, where the zero is located at $(2, 3)$ there. In the third example, a solution does not exist.
[ { "input": "20\n1 0 2 3 5 3 2 1 3 2 3 1 4 2 1 4 2 3 2 4", "output": "4 5\n2 2" }, { "input": "18\n2 2 3 2 4 3 3 3 0 2 4 2 1 3 2 1 1 1", "output": "3 6\n2 3" }, { "input": "6\n2 1 0 2 1 2", "output": "-1" }, { "input": "1\n0", "output": "1 1\n1 1" }, { "input": "7\n0 1 2 3 4 2 6", "output": "-1" }, { "input": "6\n0 0 0 0 0 0", "output": "-1" }, { "input": "4\n0 0 0 0", "output": "-1" } ]
2,000
62,464,000
0
18,327
731
Funny Game
[ "dp", "games" ]
null
null
Once upon a time Petya and Gena gathered after another programming competition and decided to play some game. As they consider most modern games to be boring, they always try to invent their own games. They have only stickers and markers, but that won't stop them. The game they came up with has the following rules. Initially, there are *n* stickers on the wall arranged in a row. Each sticker has some number written on it. Now they alternate turn, Petya moves first. One move happens as follows. Lets say there are *m*<=≥<=2 stickers on the wall. The player, who makes the current move, picks some integer *k* from 2 to *m* and takes *k* leftmost stickers (removes them from the wall). After that he makes the new sticker, puts it to the left end of the row, and writes on it the new integer, equal to the sum of all stickers he took on this move. Game ends when there is only one sticker left on the wall. The score of the player is equal to the sum of integers written on all stickers he took during all his moves. The goal of each player is to maximize the difference between his score and the score of his opponent. Given the integer *n* and the initial sequence of stickers on the wall, define the result of the game, i.e. the difference between the Petya's and Gena's score if both players play optimally.
The first line of input contains a single integer *n* (2<=≤<=*n*<=≤<=200<=000) — the number of stickers, initially located on the wall. The second line contains *n* integers *a*1, *a*2, ..., *a**n* (<=-<=10<=000<=≤<=*a**i*<=≤<=10<=000) — the numbers on stickers in order from left to right.
Print one integer — the difference between the Petya's score and Gena's score at the end of the game if both players play optimally.
[ "3\n2 4 8\n", "4\n1 -7 -2 3\n" ]
[ "14\n", "-3\n" ]
In the first sample, the optimal move for Petya is to take all the stickers. As a result, his score will be equal to 14 and Gena's score will be equal to 0. In the second sample, the optimal sequence of moves is the following. On the first move Petya will take first three sticker and will put the new sticker with value  - 8. On the second move Gena will take the remaining two stickers. The Petya's score is 1 + ( - 7) + ( - 2) =  - 8, Gena's score is ( - 8) + 3 =  - 5, i.e. the score difference will be  - 3.
[ { "input": "3\n2 4 8", "output": "14" }, { "input": "4\n1 -7 -2 3", "output": "-3" }, { "input": "10\n35 11 35 28 48 25 2 43 23 10", "output": "260" }, { "input": "100\n437 89 481 95 29 326 10 304 97 414 52 46 106 181 385 173 337 148 437 133 52 136 86 250 289 61 480 314 166 69 275 486 117 129 353 412 382 469 290 479 388 231 7 462 247 432 488 146 466 422 369 336 148 460 418 356 303 149 421 146 233 224 432 239 392 409 172 331 152 433 345 205 451 138 273 284 48 109 294 281 468 301 337 176 137 52 216 79 431 141 147 240 107 184 393 459 286 123 297 160", "output": "26149" }, { "input": "3\n2 1 2", "output": "5" }, { "input": "101\n11 -250 -200 157 84 89 207 139 -76 -183 -26 -218 79 -122 244 -133 82 -64 38 131 184 -154 256 -250 -246 227 -57 -188 -208 -48 64 -163 213 -110 -17 -106 -96 198 19 -214 50 -117 -215 214 -254 185 -7 19 117 112 172 -229 66 -169 209 -110 122 223 0 -151 66 -154 20 77 -180 202 246 -209 24 -180 -3 10 -86 -26 50 29 225 47 -177 225 -189 -40 -114 -56 -50 70 -102 160 -26 167 48 188 -84 -194 -201 250 135 -174 -222 192 -64", "output": "211" }, { "input": "102\n-70 -76 15 -32 -88 -26 75 23 92 -96 7 34 92 45 -62 -90 -26 78 -11 -63 34 -61 54 -32 -63 -70 38 73 22 97 -67 81 76 -10 -90 23 47 -23 31 25 -68 75 33 -71 95 57 -9 38 -22 39 68 -19 29 -67 41 75 13 36 5 3 -4 9 -9 -42 -72 51 -44 67 55 -1 30 -1 -9 101 39 -80 86 50 78 -81 11 -19 -63 72 -82 54 -18 -5 -101 22 50 3 26 -52 -83 -21 -9 -54 86 12 -21 99", "output": "487" }, { "input": "103\n-26 87 -179 -82 156 68 -131 67 -203 166 -6 -3 99 176 97 -115 73 155 30 208 131 -106 -20 98 -77 -60 -152 -24 -158 185 193 112 86 -74 114 -185 49 162 -207 96 -70 -212 12 28 -19 73 -115 -169 169 -27 -183 112 -207 112 42 -107 31 -92 161 -84 181 21 189 190 -115 54 -138 140 169 161 -197 146 -25 44 95 -121 -19 -180 -5 172 -81 -51 -86 137 27 -152 17 -121 -177 113 94 -179 -11 -38 201 -155 -22 -104 -21 161 189 -60 115", "output": "813" }, { "input": "104\n256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256 256", "output": "26624" }, { "input": "105\n102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102 102", "output": "10710" }, { "input": "106\n212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212 212", "output": "22472" }, { "input": "107\n256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256 -256 256", "output": "256" }, { "input": "108\n102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102 102 -102", "output": "102" }, { "input": "109\n212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212 -212 212", "output": "212" }, { "input": "110\n-256 -149 -136 -33 -253 -141 -170 -253 0 -107 -141 -236 -65 -158 -84 -180 -97 -97 -223 -44 -232 -255 -108 -25 -49 -48 -212 -3 -232 -172 -231 -158 -23 -206 -198 -55 -36 -11 -169 -94 -190 -115 -116 -231 -155 -201 -155 -103 -242 -119 -136 -8 -2 -11 -69 -250 -51 -129 -155 -216 -107 -102 -186 -13 -78 -2 -238 -66 -29 -102 -249 -198 -151 -38 -3 -128 -130 -73 -236 -83 -28 -95 -140 -62 -24 -168 -199 -196 -28 -28 -6 -220 -247 -75 -200 -228 -109 -251 -76 -53 -43 -170 -213 -146 -68 -58 -58 -218 -186 -165", "output": "165" }, { "input": "111\n-66 -39 -25 -78 -75 -44 -56 -89 -70 -7 -46 -70 -51 -36 -61 -95 -40 -84 -48 -8 -35 -37 -47 -35 -75 -87 -10 -101 -43 -70 -28 -50 -39 -13 -34 -40 -100 -70 -32 -12 -23 -62 -41 -94 -25 -30 -102 -32 -78 -10 -82 -71 -34 -2 -100 -60 -14 -17 -12 -57 -96 -27 -27 -23 -74 -60 -30 -38 -61 -95 -41 -73 -24 -76 -68 -29 -17 -75 -28 -86 -68 -25 -20 -68 -100 -44 -47 -8 -85 -84 -68 -92 -33 -9 -40 -83 -64 -2 -94 -66 -65 -46 -22 -41 -47 -24 -56 -91 -65 -63 -5", "output": "5" }, { "input": "113\n154 110 128 156 88 54 172 96 93 13 108 219 37 34 44 153 73 23 0 210 85 18 243 147 174 182 153 196 200 223 162 151 237 148 174 86 181 1 17 187 81 175 46 253 131 44 145 184 53 164 97 220 94 0 8 157 225 50 90 186 79 67 199 108 159 86 173 181 208 182 17 254 82 61 64 7 29 112 156 105 175 91 165 229 162 101 3 62 154 32 13 133 116 185 237 94 67 171 23 123 249 255 135 23 126 115 175 73 128 16 88 139 78", "output": "13598" }, { "input": "114\n46 7 39 21 44 31 49 57 26 22 86 45 66 72 96 15 77 38 92 88 50 68 30 55 20 5 15 11 26 66 94 74 43 73 35 7 11 36 26 74 86 52 14 5 91 71 3 75 22 7 10 97 42 41 52 80 97 31 45 59 53 85 87 63 42 51 98 61 26 96 65 22 47 0 36 27 35 69 81 58 9 43 7 98 27 56 101 2 31 82 48 100 77 77 42 61 6 32 69 30 102 64 51 64 20 24 76 87 63 52 73 41 5 34", "output": "5672" }, { "input": "115\n176 163 163 37 7 157 82 29 153 189 174 103 105 90 49 63 88 151 198 31 178 110 15 188 20 181 167 118 133 203 121 150 201 103 205 160 103 91 177 133 107 147 11 11 199 137 139 153 29 94 81 143 185 137 101 71 26 14 123 73 72 134 149 51 175 71 41 155 111 146 61 140 82 75 134 107 142 95 159 132 5 76 32 133 71 129 207 212 77 173 185 123 174 53 88 44 105 37 115 204 172 4 207 118 28 134 207 50 194 40 54 95 47 39 70", "output": "12880" }, { "input": "2\n-10000 -10000", "output": "-20000" }, { "input": "4\n2 -10000 -10 4", "output": "-4" }, { "input": "6\n-6000 -5000 -4000 -3000 -2000 -1000", "output": "1000" }, { "input": "10\n-10000 -10000 100 100 100 100 100 100 100 100", "output": "-100" }, { "input": "2\n1313 8442", "output": "9755" }, { "input": "2\n5 -3", "output": "2" }, { "input": "4\n1 5 -6 0", "output": "6" } ]
31
0
0
18,370
374
Inna and Pink Pony
[ "greedy", "implementation" ]
null
null
Dima and Inna are doing so great! At the moment, Inna is sitting on the magic lawn playing with a pink pony. Dima wanted to play too. He brought an *n*<=×<=*m* chessboard, a very tasty candy and two numbers *a* and *b*. Dima put the chessboard in front of Inna and placed the candy in position (*i*,<=*j*) on the board. The boy said he would give the candy if it reaches one of the corner cells of the board. He's got one more condition. There can only be actions of the following types: - move the candy from position (*x*,<=*y*) on the board to position (*x*<=-<=*a*,<=*y*<=-<=*b*); - move the candy from position (*x*,<=*y*) on the board to position (*x*<=+<=*a*,<=*y*<=-<=*b*); - move the candy from position (*x*,<=*y*) on the board to position (*x*<=-<=*a*,<=*y*<=+<=*b*); - move the candy from position (*x*,<=*y*) on the board to position (*x*<=+<=*a*,<=*y*<=+<=*b*). Naturally, Dima doesn't allow to move the candy beyond the chessboard borders. Inna and the pony started shifting the candy around the board. They wonder what is the minimum number of allowed actions that they need to perform to move the candy from the initial position (*i*,<=*j*) to one of the chessboard corners. Help them cope with the task!
The first line of the input contains six integers *n*,<=*m*,<=*i*,<=*j*,<=*a*,<=*b* (1<=≤<=*n*,<=*m*<=≤<=106; 1<=≤<=*i*<=≤<=*n*; 1<=≤<=*j*<=≤<=*m*; 1<=≤<=*a*,<=*b*<=≤<=106). You can assume that the chessboard rows are numbered from 1 to *n* from top to bottom and the columns are numbered from 1 to *m* from left to right. Position (*i*,<=*j*) in the statement is a chessboard cell on the intersection of the *i*-th row and the *j*-th column. You can consider that the corners are: (1,<=*m*), (*n*,<=1), (*n*,<=*m*), (1,<=1).
In a single line print a single integer — the minimum number of moves needed to get the candy. If Inna and the pony cannot get the candy playing by Dima's rules, print on a single line "Poor Inna and pony!" without the quotes.
[ "5 7 1 3 2 2\n", "5 5 2 3 1 1\n" ]
[ "2\n", "Poor Inna and pony!\n" ]
Note to sample 1: Inna and the pony can move the candy to position (1 + 2, 3 + 2) = (3, 5), from there they can move it to positions (3 - 2, 5 + 2) = (1, 7) and (3 + 2, 5 + 2) = (5, 7). These positions correspond to the corner squares of the chess board. Thus, the answer to the test sample equals two.
[ { "input": "5 7 1 3 2 2", "output": "2" }, { "input": "5 5 2 3 1 1", "output": "Poor Inna and pony!" }, { "input": "1 1 1 1 1 1", "output": "0" }, { "input": "23000 15500 100 333 9 1", "output": "15167" }, { "input": "33999 99333 33000 99000 3 9", "output": "333" }, { "input": "5 7 1 3 1 2", "output": "2" }, { "input": "1 100 1 50 1 50", "output": "Poor Inna and pony!" }, { "input": "1000 1 1 1 1 500", "output": "0" }, { "input": "304 400 12 20 4 4", "output": "95" }, { "input": "1000000 1000000 1000000 1000000 1000000 1000000", "output": "0" }, { "input": "1000000 99999 12345 23456 23 54", "output": "Poor Inna and pony!" }, { "input": "50000 100000 500 1000 500 1000", "output": "99" }, { "input": "50000 100000 500 1000 500 2000", "output": "Poor Inna and pony!" }, { "input": "50000 100000 500 1000 500 500", "output": "Poor Inna and pony!" }, { "input": "99999 99999 1 2 1 1", "output": "Poor Inna and pony!" }, { "input": "5 4 2 3 2 2", "output": "Poor Inna and pony!" }, { "input": "5 4 2 3 1 1", "output": "1" }, { "input": "5 5 1 3 1 2", "output": "Poor Inna and pony!" }, { "input": "2347 2348 234 48 238 198", "output": "Poor Inna and pony!" }, { "input": "1000000 2 2 2 2 1", "output": "499999" }, { "input": "100 100 50 50 500 500", "output": "Poor Inna and pony!" }, { "input": "1000 2000 100 200 90 90", "output": "20" }, { "input": "1000 1000 10 15 10 5", "output": "197" }, { "input": "23000 15500 100 333 9 1", "output": "15167" }, { "input": "5 5 4 3 1 2", "output": "1" }, { "input": "5 5 4 4 1 1", "output": "1" }, { "input": "5 5 4 2 1 1", "output": "1" }, { "input": "3 3 2 2 2 2", "output": "Poor Inna and pony!" }, { "input": "5 8 4 1 2 1", "output": "Poor Inna and pony!" }, { "input": "5 8 4 2 1 2", "output": "3" }, { "input": "2 8 1 2 1 3", "output": "2" }, { "input": "1000000 1000000 500000 500000 1 1", "output": "499999" }, { "input": "500000 100000 400 80000 2 2", "output": "249800" }, { "input": "1004 999004 4 4 5 5", "output": "199800" }, { "input": "11 11 3 3 4 4", "output": "2" }, { "input": "100 100 70 5 1 1", "output": "30" }, { "input": "1 5 1 3 1 1", "output": "Poor Inna and pony!" }, { "input": "1 5 1 3 10 1", "output": "Poor Inna and pony!" }, { "input": "6 1 5 1 2 2", "output": "Poor Inna and pony!" }, { "input": "2 10 1 5 2 2", "output": "Poor Inna and pony!" }, { "input": "5 1 3 1 1 1", "output": "Poor Inna and pony!" }, { "input": "1000 1000 1 3 10000 1", "output": "Poor Inna and pony!" }, { "input": "2 6 1 2 2 2", "output": "Poor Inna and pony!" }, { "input": "2 6 1 2 6 2", "output": "Poor Inna and pony!" }, { "input": "7 1 5 1 2 2", "output": "Poor Inna and pony!" }, { "input": "2 20 2 5 2 2", "output": "Poor Inna and pony!" }, { "input": "4 4 3 4 1 5", "output": "Poor Inna and pony!" } ]
46
0
0
18,386
0
none
[ "none" ]
null
null
Some company is going to hold a fair in Byteland. There are $n$ towns in Byteland and $m$ two-way roads between towns. Of course, you can reach any town from any other town using roads. There are $k$ types of goods produced in Byteland and every town produces only one type. To hold a fair you have to bring at least $s$ different types of goods. It costs $d(u,v)$ coins to bring goods from town $u$ to town $v$ where $d(u,v)$ is the length of the shortest path from $u$ to $v$. Length of a path is the number of roads in this path. The organizers will cover all travel expenses but they can choose the towns to bring goods from. Now they want to calculate minimum expenses to hold a fair in each of $n$ towns.
There are $4$ integers $n$, $m$, $k$, $s$ in the first line of input ($1 \le n \le 10^{5}$, $0 \le m \le 10^{5}$, $1 \le s \le k \le min(n, 100)$) — the number of towns, the number of roads, the number of different types of goods, the number of different types of goods necessary to hold a fair. In the next line there are $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_{i} \le k$), where $a_i$ is the type of goods produced in the $i$-th town. It is guaranteed that all integers between $1$ and $k$ occur at least once among integers $a_{i}$. In the next $m$ lines roads are described. Each road is described by two integers $u$ $v$ ($1 \le u, v \le n$, $u \ne v$) — the towns connected by this road. It is guaranteed that there is no more than one road between every two towns. It is guaranteed that you can go from any town to any other town via roads.
Print $n$ numbers, the $i$-th of them is the minimum number of coins you need to spend on travel expenses to hold a fair in town $i$. Separate numbers with spaces.
[ "5 5 4 3\n1 2 4 3 2\n1 2\n2 3\n3 4\n4 1\n4 5\n", "7 6 3 2\n1 2 3 3 2 2 1\n1 2\n2 3\n3 4\n2 5\n5 6\n6 7\n" ]
[ "2 2 2 2 3 \n", "1 1 1 2 2 1 1 \n" ]
Let's look at the first sample. To hold a fair in town $1$ you can bring goods from towns $1$ ($0$ coins), $2$ ($1$ coin) and $4$ ($1$ coin). Total numbers of coins is $2$. Town $2$: Goods from towns $2$ ($0$), $1$ ($1$), $3$ ($1$). Sum equals $2$. Town $3$: Goods from towns $3$ ($0$), $2$ ($1$), $4$ ($1$). Sum equals $2$. Town $4$: Goods from towns $4$ ($0$), $1$ ($1$), $5$ ($1$). Sum equals $2$. Town $5$: Goods from towns $5$ ($0$), $4$ ($1$), $3$ ($2$). Sum equals $3$.
[]
31
0
0
18,403
730
Minimum and Maximum
[ "constructive algorithms", "interactive" ]
null
null
This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush(). In this problem, you need to find maximal and minimal elements of an array. What could be simpler? You can imagine that the jury has an array, and initially you know the only number *n* — array's length. Array's elements are numbered from 1 to *n*. You are allowed to compare two elements of the array by using their indices *i* and *j*. There are three possible responses to this query: '&lt;' (if *a**i* is less than *a**j*), '=' (if *a**i* is equal to *a**j*) and finally '&gt;' (if *a**i* is greater than *a**j*). It's known that it's always possible to find both maximal and minimal elements of the array by using no more than comparisons, where ⌈ *x*⌉ is the result of rounding *x* up. Write the program that will find positions of the minimum and the maximum in the jury's array of length *n*, by using no more than *f*(*n*) comparisons.
none
none
[ "2\n2\n \n&gt;\n \n3\n \n=\n \n=\n " ]
[ "? 1 2\n \n! 2 1\n \n? 3 1\n \n? 2 1\n \n! 2 3" ]
none
[ { "input": "2\n2\n2 1\n3\n1 1 1", "output": "1 out of 1\n3 out of 3\n2 queries processed [sumn=5]" }, { "input": "1\n4\n1 1 2 2", "output": "4 out of 4\n1 queries processed [sumn=4]" }, { "input": "2\n5\n1 1 2 1 1\n3\n3 2 1", "output": "6 out of 6\n3 out of 3\n2 queries processed [sumn=8]" }, { "input": "2\n6\n2 1 2 1 2 2\n3\n2 2 1", "output": "7 out of 7\n3 out of 3\n2 queries processed [sumn=9]" }, { "input": "2\n4\n2 3 3 3\n5\n4 4 4 3 2", "output": "4 out of 4\n6 out of 6\n2 queries processed [sumn=9]" }, { "input": "1\n10\n1 2 1 2 1 3 1 3 1 2", "output": "13 out of 13\n1 queries processed [sumn=10]" }, { "input": "1\n1\n1", "output": "0 out of 0\n1 queries processed [sumn=1]" }, { "input": "2\n2\n1 2\n2\n2 1", "output": "1 out of 1\n1 out of 1\n2 queries processed [sumn=4]" }, { "input": "6\n3\n1 2 3\n3\n1 3 2\n3\n2 1 3\n3\n2 3 1\n3\n3 1 2\n3\n3 2 1", "output": "3 out of 3\n3 out of 3\n3 out of 3\n3 out of 3\n3 out of 3\n3 out of 3\n6 queries processed [sumn=18]" }, { "input": "24\n4\n1 2 3 4\n4\n1 2 4 3\n4\n1 3 2 4\n4\n1 3 4 2\n4\n1 4 2 3\n4\n1 4 3 2\n4\n2 1 3 4\n4\n2 1 4 3\n4\n2 3 1 4\n4\n2 3 4 1\n4\n2 4 1 3\n4\n2 4 3 1\n4\n3 1 2 4\n4\n3 1 4 2\n4\n3 2 1 4\n4\n3 2 4 1\n4\n3 4 1 2\n4\n3 4 2 1\n4\n4 1 2 3\n4\n4 1 3 2\n4\n4 2 1 3\n4\n4 2 3 1\n4\n4 3 1 2\n4\n4 3 2 1", "output": "4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n4 out of 4\n24 queries processed [sumn=96]" }, { "input": "1\n1\n1000000000", "output": "0 out of 0\n1 queries processed [sumn=1]" } ]
15
4,608,000
0
18,420
293
Weird Game
[ "games", "greedy" ]
null
null
Yaroslav, Andrey and Roman can play cubes for hours and hours. But the game is for three, so when Roman doesn't show up, Yaroslav and Andrey play another game. Roman leaves a word for each of them. Each word consists of 2·*n* binary characters "0" or "1". After that the players start moving in turns. Yaroslav moves first. During a move, a player must choose an integer from 1 to 2·*n*, which hasn't been chosen by anybody up to that moment. Then the player takes a piece of paper and writes out the corresponding character from his string. Let's represent Yaroslav's word as *s*<==<=*s*1*s*2... *s*2*n*. Similarly, let's represent Andrey's word as *t*<==<=*t*1*t*2... *t*2*n*. Then, if Yaroslav choose number *k* during his move, then he is going to write out character *s**k* on the piece of paper. Similarly, if Andrey choose number *r* during his move, then he is going to write out character *t**r* on the piece of paper. The game finishes when no player can make a move. After the game is over, Yaroslav makes some integer from the characters written on his piece of paper (Yaroslav can arrange these characters as he wants). Andrey does the same. The resulting numbers can contain leading zeroes. The person with the largest number wins. If the numbers are equal, the game ends with a draw. You are given two strings *s* and *t*. Determine the outcome of the game provided that Yaroslav and Andrey play optimally well.
The first line contains integer *n* (1<=≤<=*n*<=≤<=106). The second line contains string *s* — Yaroslav's word. The third line contains string *t* — Andrey's word. It is guaranteed that both words consist of 2·*n* characters "0" and "1".
Print "First", if both players play optimally well and Yaroslav wins. If Andrey wins, print "Second" and if the game ends with a draw, print "Draw". Print the words without the quotes.
[ "2\n0111\n0001\n", "3\n110110\n001001\n", "3\n111000\n000111\n", "4\n01010110\n00101101\n", "4\n01100000\n10010011\n" ]
[ "First\n", "First\n", "Draw\n", "First\n", "Second\n" ]
none
[ { "input": "2\n0111\n0001", "output": "First" }, { "input": "3\n110110\n001001", "output": "First" }, { "input": "3\n111000\n000111", "output": "Draw" }, { "input": "4\n01010110\n00101101", "output": "First" }, { "input": "4\n01100000\n10010011", "output": "Second" }, { "input": "4\n10001001\n10101101", "output": "Draw" }, { "input": "3\n000000\n000100", "output": "Draw" }, { "input": "2\n0000\n1110", "output": "Second" }, { "input": "4\n11111111\n10100110", "output": "First" }, { "input": "4\n10100111\n11011000", "output": "First" }, { "input": "4\n00101011\n11110100", "output": "Draw" }, { "input": "4\n11000011\n00111100", "output": "Draw" }, { "input": "4\n11101111\n01000110", "output": "First" }, { "input": "4\n01110111\n00101110", "output": "First" }, { "input": "4\n11011111\n10110110", "output": "First" }, { "input": "4\n01101010\n11111110", "output": "Second" }, { "input": "4\n01111111\n10011001", "output": "First" }, { "input": "4\n01010100\n10011111", "output": "Second" }, { "input": "4\n01111011\n01001011", "output": "First" }, { "input": "4\n11011010\n11011001", "output": "Draw" }, { "input": "4\n11001101\n10001010", "output": "First" }, { "input": "4\n01101111\n10111101", "output": "Draw" }, { "input": "4\n10111100\n00000101", "output": "First" }, { "input": "4\n01111000\n11111010", "output": "Second" }, { "input": "4\n11001100\n00000111", "output": "First" }, { "input": "4\n01110111\n10101101", "output": "First" }, { "input": "4\n01001000\n11111100", "output": "Second" }, { "input": "4\n01011011\n01010010", "output": "First" }, { "input": "4\n00101101\n01001001", "output": "First" }, { "input": "4\n00110110\n10000100", "output": "First" }, { "input": "4\n10010000\n01000110", "output": "Draw" }, { "input": "4\n00000100\n10001111", "output": "Second" }, { "input": "4\n01110100\n01110100", "output": "Draw" }, { "input": "4\n11000001\n11010001", "output": "Draw" }, { "input": "4\n11001000\n00011000", "output": "First" }, { "input": "4\n10110011\n01011111", "output": "Draw" }, { "input": "4\n10000100\n11010100", "output": "Second" }, { "input": "4\n01011011\n10101110", "output": "Draw" }, { "input": "10\n00000000000111111111\n00000000011111111111", "output": "Draw" }, { "input": "1\n11\n11", "output": "Draw" }, { "input": "1\n11\n00", "output": "First" }, { "input": "1\n00\n01", "output": "Draw" }, { "input": "2\n0111\n1001", "output": "First" }, { "input": "1\n01\n11", "output": "Draw" } ]
1,870
12,083,200
3
18,457
258
Little Elephant and Broken Sorting
[ "dp", "math", "probabilities" ]
null
null
The Little Elephant loves permutations of integers from 1 to *n* very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1,<=2,<=3,<=...,<=*n*. This time the Little Elephant has permutation *p*1,<=*p*2,<=...,<=*p**n*. Its sorting program needs to make exactly *m* moves, during the *i*-th move it swaps elements that are at that moment located at the *a**i*-th and the *b**i*-th positions. But the Little Elephant's sorting program happened to break down and now on every step it can equiprobably either do nothing or swap the required elements. Now the Little Elephant doesn't even hope that the program will sort the permutation, but he still wonders: if he runs the program and gets some permutation, how much will the result of sorting resemble the sorted one? For that help the Little Elephant find the mathematical expectation of the number of permutation inversions after all moves of the program are completed. We'll call a pair of integers *i*,<=*j* (1<=≤<=*i*<=&lt;<=*j*<=≤<=*n*) an inversion in permutatuon *p*1,<=*p*2,<=...,<=*p**n*, if the following inequality holds: *p**i*<=&gt;<=*p**j*.
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=1000,<=*n*<=&gt;<=1) — the permutation size and the number of moves. The second line contains *n* distinct integers, not exceeding *n* — the initial permutation. Next *m* lines each contain two integers: the *i*-th line contains integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*,<=*a**i*<=≠<=*b**i*) — the positions of elements that were changed during the *i*-th move.
In the only line print a single real number — the answer to the problem. The answer will be considered correct if its relative or absolute error does not exceed 10<=-<=6.
[ "2 1\n1 2\n1 2\n", "4 3\n1 3 2 4\n1 2\n2 3\n1 4\n" ]
[ "0.500000000\n", "3.000000000\n" ]
none
[ { "input": "2 1\n1 2\n1 2", "output": "0.500000000" }, { "input": "4 3\n1 3 2 4\n1 2\n2 3\n1 4", "output": "3.000000000" }, { "input": "7 4\n7 6 4 2 1 5 3\n1 3\n2 1\n7 2\n3 5", "output": "11.250000000" }, { "input": "10 1\n1 2 3 4 5 6 7 8 9 10\n1 10", "output": "8.500000000" }, { "input": "9 20\n9 8 7 6 5 4 3 2 1\n4 6\n9 4\n5 9\n6 8\n1 9\n5 8\n6 9\n7 3\n1 9\n8 3\n4 5\n9 6\n3 8\n4 1\n1 2\n3 2\n4 9\n6 7\n7 5\n9 6", "output": "20.105407715" }, { "input": "20 7\n3 17 7 14 11 4 1 18 20 19 13 12 5 6 15 16 9 2 8 10\n19 13\n20 6\n19 11\n12 3\n10 19\n14 10\n3 16", "output": "102.250000000" }, { "input": "100 1\n78 52 95 76 96 49 53 59 77 100 64 11 9 48 15 17 44 46 21 54 39 68 43 4 32 28 73 6 16 62 72 84 65 86 98 75 33 45 25 3 91 82 2 92 63 88 7 50 97 93 14 22 20 42 60 55 80 85 29 34 56 71 83 38 26 47 90 70 51 41 40 31 37 12 35 99 67 94 1 87 57 8 61 19 23 79 36 18 66 74 5 27 81 69 24 58 13 10 89 30\n17 41", "output": "2659.500000000" }, { "input": "125 8\n111 69 3 82 24 38 4 39 42 22 92 6 16 10 8 45 17 91 84 53 5 46 124 47 18 57 43 73 114 102 121 105 118 95 104 98 72 20 56 60 123 80 103 70 65 107 67 112 101 108 99 49 12 94 2 68 119 109 59 40 86 116 88 63 110 14 13 120 41 64 89 71 15 35 81 51 113 90 55 122 1 75 54 33 28 7 125 9 100 115 19 58 61 83 117 52 106 87 11 50 93 32 21 96 26 78 48 79 23 36 66 27 31 62 25 77 30 74 76 44 97 85 29 34 37\n33 17\n84 103\n71 33\n5 43\n23 15\n65 34\n125 58\n51 69", "output": "3919.000000000" }, { "input": "100 2\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\n88 90\n62 77", "output": "16.000000000" } ]
122
0
0
18,469
335
Palindrome
[ "constructive algorithms", "dp" ]
null
null
Given a string *s*, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of *s* and is as long as possible.
The only line of the input contains one string *s* of length *n* (1<=≤<=*n*<=≤<=5·104) containing only lowercase English letters.
If *s* contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of *s*. If *s* doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of *s* and is as long as possible. If there exists multiple answers, you are allowed to print any of them.
[ "bbbabcbbb\n", "rquwmzexectvnbanemsmdufrg\n" ]
[ "bbbcbbb\n", "rumenanemur\n" ]
A subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward.
[ { "input": "bbbabcbbb", "output": "bbbcbbb" }, { "input": "rquwmzexectvnbanemsmdufrg", "output": "rumenanemur" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }, { "input": "ab", "output": "a" }, { "input": "abacaba", "output": "abacaba" }, { "input": "startcup", "output": "trt" }, { "input": "aabccb", "output": "bccb" }, { "input": "abbacc", "output": "abba" }, { "input": "iwlauggytlpahjhteurdyoaulbnnhashwktxlrhpgqnypnuitmfhhbkwysjuhljshaanowhngeaumdovrlvuofzsbhhzdyngntdu", "output": "ugyhhurounhashwkhnpnhkwhsahnuoruhhygu" }, { "input": "zypzepzqni", "output": "zpepz" }, { "input": "a", "output": "a" }, { "input": "oliifeuoyzhkootktyulrxshmboejshoguwgufxpuloouoojovufukdokoeyouzihyaeuucutypfxictojtnfoouoniuyuvkrglsueihpuxsulrrifyuwuyolutotzozsvhoyjxuguecywbfuuqmpzooojergudvwucsocoojfplaulvfpcooxxublcfpguuoouooouo", "output": "ouooouoougpluoovufudooozuucuxjoouoyurlsupuslruyouoojxucuuzooodufuvooulpguoouooouo" }, { "input": "kyzxnaiwjgnotlekhycrnapekfcfxydbvnjboevgdzgigxvijgwrqnacumglzlxkcurmqmxzxxbuxlwruycdhzzdmtvobmvylyibyynjvonmbwvwqzmidfhnndecrwyfxjxwquiubrimmcwoeecotwnkfrojwuxmficzurwaqljfvdcvixcictxfzztzzbvbdypayhay", "output": "yahyapydbvbzixijqauzcurmmbuxwrcdhdmvbmvyybyyvmbvmdhdcrwxubmmruczuaqjixizbvbdypayhay" }, { "input": "carfnnnuxxbntssoxafxxbbxnonpunapjxtxexlptcwqxkjnxaanntriyunvnoxkdxnsxjxxoixyplkvxctuoenxxjixxgyxlgnxhklxqxqzxafxcnealxxwspbpcfgpnnovwrnnaxjcjpnierselogxxcxrxnlpnkbzjnqvrkurhxaxyjxxnxxnpipuntongeuusujf", "output": "fnnuxxnxxxxnnpnxxxlpcjxannrvoxxnxxxxlkxnxxixxnxklxxxxnxxovrnnaxjcplxxxnpnnxxxxnxxunnf" }, { "input": "yggmkggmvkfmmsvijnyvwgswdpwkwvmcmmswksmhwwmmgwzgwkkwogwiglwggwwnmwgkqggkugwmfwawxggrwgwclgwclknltxrkjwogwejgeymtmwziwmskrwsmfmmiwkwwvsgwsdmmkfmggkmpgg", "output": "ggmkggmfmmswgswwkwmmmswksmwwmmgwgwkkwgwgwggwwmwgkggkgwmwwggwgwgwkkwgwgmmwwmskwsmmmwkwwsgwsmmfmggkmgg" }, { "input": "mstmytylsulbuhumamahahbbmmtttmlulhamyhlmbulyylubmlhymahlulmtttmmbbhahamamuhubluslytymtsmxp", "output": "mstmytylsulbuhumamahahbbmmtttmlulhamyhlmbulyylubmlhymahlulmtttmmbbhahamamuhubluslytymtsm" }, { "input": "ouwvwggffgoowjzkzeggbzieagwwznzzzlwvzswfznvozveezgopefecoezfsofqzqfoovaofwzgefzzweggvztzvofgevivgevwwslhofzwopopzzgfvwzeogovvgzdzafgwzshovvrwwwgmooczezivfwgybgezogsmgfgwwzevvwgeehwvegfdzgzwgzoffgteztwvgwfvogeggfogkeovxzzlzzwzzlvifgxzwvrogeeeggeoefhhzoweerwvxofgvwovozwvizofzogozgwevwllexooktoggvoeowgtezffzfdohvgmofofvwzzofwvwsfbwyzzziofvfcofmzgrofzozzgghseafefdpwwwogpzzfowfhlsoeitfogeezfagofqqesocewfpwveozeenwsvffzwozwzlwoeffzonveaivgfebvegveozzfoowzwzkwezjeeuwzgezoovwwgzgzggwzowzevwfgggoozfozfwg", "output": "gzoggwveoggvoegezfzovgofowzzowvswzovfcofzgofosfwozzowfsofogzfocfvozwsvwozzwofogvozfzegeovggoevwggozg" }, { "input": "gamjuklsvzwddaocadujdmvlenyyvlflipneqofeyipmtunbdmbdyhkovnpdetueeiunsipowrhxrhtastjniqdhmehcumdcrghewljvpikcraoouhfwtnoaukbnykjapkvyakdnckkypargamvnsqtchesbmuffqqycnjvolmtpjfykvkeexkpdxjexrvdzpcbthhkxuucermkaebrvcxoovidpqnpkgbhiatyjvumihptrigqxsemqbbxwmyunmmayubqqjaioqmzyekhtqgoockiskyqihopmkastfvqiewtbtbriuyuszlndcweuhnywlkjgerqokxsxfxeaxcuwoocoonstwlxujrynkwzshpretbhlvkxyebnhafxfelpmqfkksurrfqeaykdxihtyqpaiftigdwkraxxzxkqicnfxlxhxwtkkknurzubtkivzpmlfebzduezuqeyequvyrighfzyldxenwxokumxtiieeeuku", "output": "uuemkexdhiyvuqequuzefvitbuzunkkxxfxxxrkwpthxyaeqfrrfqeayxhtpwkrxxxfxxkknuzubtivfezuuqequvyihdxekmeuu" }, { "input": "qpjnbjhmigtgtxolifwoyatdlqtejqovaslcgjufverrnkqxtsrrytqgtuikcseggjnltpggcpjizojthwycibvnvxetibpicujwmyzcojhpftttwvtlxaeefbvbvygouinnjrwuczlplbzahqciptrlrcuamyntvrzqxrnkrczmluocsuthwaptenwysoviwwcnrljuskynomlpslqyjcauagiveunrzognltohqxggprgreezjsqchcgihzbrleuwgnlsqeenybrbsqcnnlbipajlcfmajtchblnxsnegooewupmvufzbipnyjneuwelibvhoghtqpvqjehvpbiclalyzxzqwekemnsjabyzatjgzbrheubuzrcgtxvaokzapejesjntgnriupahoeousszcqprljhhgnqclbsuvvgfudhwmabfbyqjcqqgnnoocqxbyjpmvncmcieavcstjvvzgtbgcjbqnqbpueqlgibtvjpzsan", "output": "nspjvglqeqcgbgsenybqcnncjbwuvublghpqehpinsjazatgruurgtazajsnipheqphglbuvuwbjcnncqbynesgbgcqeqlgvjpsn" }, { "input": "nwgwxgxlbgbgnvqowqqocgwwnbnoqghxwxbbonxxgongqwbbxxbbwiqgnogxxnobmbxwxlgqonbnwhewgoqqwoqngbgbxgxwgwna", "output": "nwgwxgxbgbgnqowqqogwwnbnoqgxwxbbonxxgongqwbbxxbbwqgnogxxnobbxwxgqonbnwwgoqqwoqngbgbxgxwgwn" }, { "input": "vtaavlavalbvbbbccccddddeeeefltfffgvgvgtghlhhhiviiijjjjkkkkmmmmnnnlnooooppppqqvqqrrrrssssluuuvuwwwwtv", "output": "vtvlvlvltgvgvgtlvlvlvtv" }, { "input": "iuaiubcide", "output": "iuiui" }, { "input": "aavaaaadbbbddbbdbccccwvcceveeeedeffvdfvfffdggggvwgghhdhdwdhhhwiiwiiiiwjjjjjjkkkkdkklwvlllllmmmmmmvdnndnwndnndooowoooppppppwqwqdwqwqwdqqrdrwdrrrrsdssssvsttttttuvuuuwuuxxxdwwxwxdxyyyywyddwvdvdvdvvwddddv", "output": "vddddwvvdvdvdvwddwdwwwdwvvddwdqqdwqwqwdqqdwddvvwdwwwdwddwvdvdvdvvwddddv" }, { "input": "ibbbbiabbibbscccocccccdsdyddiddishddffjifhfffjffgggeggeggjgkkkjkkjkkklllaellllhlmymmmmssmmomnnanojennosasoennnjpopaopppspsyphepqaqqqjqqjqqrrjerrerrrrttttttttuuuujuhuiuuwwjwhswwwiwwxixxxyxsosixxxaizzzi", "output": "iiaisosyiishjihjeejjjaehyssoaojnnosasonnjoaossyheajjjeejhijhsiiysosiaii" }, { "input": "ataaaamabbbbtmbbmtmtccctcttcmmcmtdmmmmmddddtdmeetmtmmeteteeftffmfffgmmggggmgmmhmhhhmhhiimtiiititmmjjjtjjjkkmtmtkmkmkklllmllmmtmlnmnnmnnnootooooptpppttppqmqqtqmqqmtmrrrtrrrssssssuuuuuuvvvvvvwwwwwwxxxxx", "output": "tmtmmtmttttmmmtmmmmmtmtmtmmtttmmmgggggmmmtttmmtmtmtmmmmmtmmmttttmtmmtmt" }, { "input": "agqdhdgqgannagbqbbhddddcgngchnhdceeqefqhgfdngfdhiiidhjgqjjndqkgqnkkgqqndlldlqqmddmmnqoqgnoqgqdopnqghdhdgpndpghqrrqdhrsnhgnssgddddhtttqgnnguqgudhuvvdqg", "output": "gqdhdgqgnngqhddddgnghnhdqqhgdngdhdhgqndqgqngqqnddqqddnqqgnqgqdnqghdhdgndghqqdhnhgngddddhqgnngqgdhdqg" }, { "input": "hyhwkyvfpfpykwhchycwcpfppcuvuvupshcwwkwucpkppkpcuwkwwchspuvuvaucppfbpcwcyhchwkypfpfvykwhyh", "output": "hyhwkyvfpfpykwhchycwcpfppcuvuvupshcwwkwucpkppkpcuwkwwchspuvuvucppfpcwcyhchwkypfpfvykwhyh" }, { "input": "dpddpdddddidtddudddddqddddddddddddddddddddddcdddddddddddddddkkdddddgdddddddddjkdovfvvgnnvvvvvvvvvvnvvvvvkvvvvfnvuuvevvvfvvvpvvvkvvvvvvvvvlvvvifvvvvklvbvvovovvvvkvivnvvvvvvulvvvwwmjwtwwbwlwwwwwwwwwwpewewwwwwpwwwwwwwwwwwwwwwwwwlwbtwwwwjwmwwwwwlwwuwnwwwwiwwkwwwwwwwwxxxxxxoxxxoxxxbxxxxlxxxxxxxxxxxxxxxxkxxxxfixlxxxxxkpxfxxxxxxxxxxxxxexxxuuxxxxxxxxxyynyyyyyyyyyyfkyynynyynyyyyyyygyyyyyyyyyyyyyfyyoyyyyyyykyyyyyyjyyyyyyygykyykyyycyyqyzzzzzzzzzzzzzzzzzzuzzzzzzzzzzztzzzizppzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "output": "pnuuefpklifklboowwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwooblkfilkpfeuunp" }, { "input": "aaaaaaaaakaaakaaaaaabbbbbbbbbbbxbbxbkbbbkxbcccccccccicccccccccddkdxdddkdkddddddkdddddikekeeeeeeeekeeeeeeeixieeffxfffffffffifffffffgikiigggggggggiggggggxgghhhhhkhhhhhhhhhhxhhhjjjjjiijjjjijjjjjxkjkjjjllllllllllkllllllllmmmmmimmmmmmmmmmmmmnnnnnnnnnnnnnnnknnnoooookooioooxioooooooopppppppppppippppxkpkkkppqqxqqqqqqqqiqiqqqqxiqqkqrrkrrrirrrrrrrrrrrrrssskskskssssssssxsssssiittttittxtttttttktttttuxuuuuuiiuuuuuuuuuuikuuvvvvivvixvvvvvvvvvivvvwxiwwwwwwwwwwwkwkwkwwwiwykyyyyyykyyykyxyyyyyyyzzzzzkzizzzzxkkxxkk", "output": "kxkkxikxkkkikkkixixiikiiixkxiiixkkkikkixippppppppppppppppppixikkikkkxiiixkxiiikiixixikkkikkkxkixkkxk" }, { "input": "aaaaaakaaaaaaaaaaaataaabbbbrbbbbbbbbbxbbbbbxbbbkctcccccccccccccncckcccccnddddrdddddddddddddddddnteeeeeeeeeeneteeneeeeeeeefffffffffnnffffffffnffffgggggggggtrgggrggggggggghhhhhhhhhnhhxhhhhhhhkhhhitiiiiiiiintiiiikiiiiiiiijxjjjjjjxjjjkjjjjjjjtjjjjlllllntllllllllllllkllllmmxmmmmmmmmmmmmmmmmmmmoooooooooooooonoooooooppprpppprppppptppnpppppppqqqqnnqqqqqqqqqqqqqqqqqssnsssssssstssnssssstssssuunuuuuuruunuuuuuuuukuuuuvvvvvvvvvvvvnvvvvvvvvvwwtwwwwwwwwkwwwwwwwwwxwwyyyyyyxyyyyyyyryyyyyyyyzzzzzzzztzzzzzzzkzzzzz", "output": "ktrxxktnknrntntnnnntrrnxktnjjjjjjjjjjkjjjjjjjjjjntkxnrrtnnnntntnrnkntkxxrtk" }, { "input": "afcyfydfxueffbukxgbhfkuufkbubkxiybuuufffkkyyxxbxfbffffbfxbxjxylykkmfffuuubyxkbubkfuukfbxkubffuxfyfyf", "output": "fyfyfxuffbukxbfkuufkbubkxybuuufffkkyyxxbxfbffffbfxbxxyykkfffuuubyxkbubkfuukfbxkubffuxfyfyf" }, { "input": "b", "output": "b" }, { "input": "abababbabbabbabbbbbbbbabbabbaaaaaaaabbbabbabbbabbbabaaabbaba", "output": "ababbaababbbbbbabbabbaaaaaaaabbabbabbbbbbabaabbaba" }, { "input": "ttabacabadabffacavvbaeabacabadabacabafabacabavvvvdabacabzaeabacabadabacttttafba", "output": "abacabadabacabaeabacabadabacabafabacabadabacabaeabacabadabacaba" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeee", "output": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeee", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddeeeeeeeeeeeeeeeeeeeeeeeeeezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz", "output": "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" }, { "input": "qwertyuioplkjhgfdsazxcvbnm", "output": "q" }, { "input": "abaabacdefgb", "output": "abaaba" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabcdefghijklmnopqrstuvwxyzaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaazaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }, { "input": "gsayhacnaqwakieoeyomukmqueosoeuckuotyadhoniaovwgnbcpczjnibhgyapqmuosbrpqwkckqjakqjeamyqqwskmoueaakfixextmcwyaqdhtlylnowuiaaoraeskkqaohbwcupgsdcngyavctnaqaaaqomimemckzkaagaebucmjuwacaoicyoywkwmogdiknzqiakackvzgrywxfiojdkopsnsifwwcwkwuqqpfpuaeigcfjebbuakkaquajyelivanwewaq", "output": "qwaieyuqueeukodoiwgcznigowkcaawmueaakemaqtynuoakkaounytqamekaaeumwaackwoginzcgwiodokueeuquyeiawq" }, { "input": "amltiwwuisnsaghiiwoaqgdgnpqkfudobascczacwipotugyeairyrcsgsuxxttsxaausijymcceapckvxfqnqdg", "output": "gdnqfcacyisuxttxusiycacfqndg" }, { "input": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "output": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" }, { "input": "abababababababababababababababababababababababababcbababababababababababababababababababababababababa", "output": "abababababababababababababababababababababababababbababababababababababababababababababababababababa" }, { "input": "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeefeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaaaaaaaa", "output": "aaaaaaaaaabbbbbbbbbbccccccccccddddddddddeeeeeeeeeeeeeeeeeeeeddddddddddccccccccccbbbbbbbbbbaaaaaaaaaa" } ]
280
6,041,600
0
18,476
19
Deletion of Repeats
[ "greedy", "hashing", "string suffix structures" ]
C. Deletion of Repeats
2
256
Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length *x* is such a substring of length 2*x*, that its first half coincides character by character with its second half. Bob started deleting all the repeats from the string. He does it as follows: while it's possible, Bob takes the shortest repeat, if it is not unique, he takes the leftmost one, and deletes its left half and everything that is to the left of this repeat. You're given the string seen by Bob. Find out, what it will look like after Bob deletes all the repeats in the way described above.
The first input line contains integer *n* (1<=≤<=*n*<=≤<=105) — length of the string. The following line contains *n* space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times.
In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way.
[ "6\n1 2 3 1 2 3\n", "7\n4 5 6 5 6 7 7\n" ]
[ "3\n1 2 3 \n", "1\n7 \n" ]
none
[ { "input": "6\n1 2 3 1 2 3", "output": "3\n1 2 3 " }, { "input": "7\n4 5 6 5 6 7 7", "output": "1\n7 " }, { "input": "10\n5 7 2 1 8 8 5 10 2 5", "output": "5\n8 5 10 2 5 " }, { "input": "10\n0 1 1 1 0 3 0 1 4 0", "output": "7\n1 0 3 0 1 4 0 " }, { "input": "10\n0 1 0 2 0 0 1 1 1 0", "output": "2\n1 0 " }, { "input": "30\n17 17 2 4 13 21 17 11 15 0 9 2 23 10 24 21 23 17 5 11 25 1 16 6 11 22 19 2 12 16", "output": "29\n17 2 4 13 21 17 11 15 0 9 2 23 10 24 21 23 17 5 11 25 1 16 6 11 22 19 2 12 16 " }, { "input": "100\n8 43 55 27 70 81 66 10 49 67 24 61 53 0 2 76 96 16 16 1 78 1 69 69 4 59 6 87 98 14 98 20 12 48 20 41 67 90 20 96 44 8 77 94 84 2 61 27 90 42 66 84 5 19 68 13 5 53 13 87 70 41 48 40 19 61 72 31 4 4 59 100 50 64 84 10 96 16 73 3 63 85 67 91 74 63 22 34 6 96 78 42 61 85 3 95 98 84 66 78", "output": "31\n4 59 100 50 64 84 10 96 16 73 3 63 85 67 91 74 63 22 34 6 96 78 42 61 85 3 95 98 84 66 78 " }, { "input": "100\n5 4 8 2 4 7 6 6 9 0 5 9 9 8 2 1 10 7 1 0 0 6 3 5 3 7 8 0 0 10 6 7 10 5 4 10 7 6 7 5 1 5 0 10 3 10 5 7 4 10 0 9 1 2 6 3 3 6 10 6 9 1 6 3 4 6 2 8 8 9 5 2 3 3 10 7 4 1 10 1 8 5 4 3 2 2 0 4 4 1 4 5 7 2 8 7 1 1 2 8", "output": "3\n1 2 8 " }, { "input": "100\n19 17 16 6 4 13 7 12 4 16 2 2 12 15 20 17 3 13 14 2 4 20 14 10 11 17 7 17 12 18 17 14 10 16 20 16 19 12 9 15 2 13 5 6 9 3 14 6 20 3 15 16 0 12 5 11 3 19 5 2 11 18 20 20 11 4 1 10 20 10 19 0 4 10 1 11 4 11 8 19 3 14 6 1 14 2 13 20 8 3 19 19 6 19 19 20 20 8 13 14", "output": "4\n20 8 13 14 " }, { "input": "10\n1 2 1 2 1 2 1 2 1 2", "output": "2\n1 2 " }, { "input": "10\n1 2 3 4 5 1 2 3 4 5", "output": "5\n1 2 3 4 5 " }, { "input": "10\n1 1 1 1 1 1 1 1 1 1", "output": "1\n1 " }, { "input": "21\n16417014 805849548 385039296 16417014 805849548 385039296 16417014 805849548 385039296 16417014 805849548 385039296 16417014 805849548 385039296 16417014 805849548 385039296 16417014 805849548 385039296", "output": "3\n16417014 805849548 385039296 " }, { "input": "22\n823078040 389511796 683819000 823078040 389511796 683819000 823078040 389511796 683819000 823078040 389511796 683819000 823078040 389511796 683819000 823078040 389511796 683819000 823078040 389511796 683819000 823078040", "output": "4\n823078040 389511796 683819000 823078040 " }, { "input": "23\n482255418 973174044 835115058 482255418 973174044 835115058 482255418 973174044 835115058 482255418 973174044 835115058 482255418 973174044 835115058 482255418 973174044 835115058 482255418 973174044 835115058 482255418 973174044", "output": "5\n482255418 973174044 835115058 482255418 973174044 " }, { "input": "1\n0", "output": "1\n0 " }, { "input": "2\n1 2", "output": "2\n1 2 " }, { "input": "2\n1000000000 1000000000", "output": "1\n1000000000 " }, { "input": "3\n1000000000 1000000000 1000000000", "output": "1\n1000000000 " }, { "input": "4\n1000000000 1000000000 1000000000 1000000000", "output": "1\n1000000000 " }, { "input": "7\n1 2 3 1 2 3 1", "output": "4\n1 2 3 1 " }, { "input": "30\n0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2", "output": "1\n2 " }, { "input": "10\n0 0 0 0 0 0 0 0 0 0", "output": "1\n0 " }, { "input": "20\n0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1", "output": "1\n1 " } ]
186
0
0
18,489
226
The table
[ "constructive algorithms", "greedy" ]
null
null
Harry Potter has a difficult homework. Given a rectangular table, consisting of *n*<=×<=*m* cells. Each cell of the table contains the integer. Harry knows how to use two spells: the first spell change the sign of the integers in the selected row, the second — in the selected column. Harry's task is to make non-negative the sum of the numbers in each row and each column using these spells. Alone, the boy can not cope. Help the young magician!
The first line contains two integers *n* and *m* (1<=≤<=*n*,<= *m*<=≤<=100) — the number of rows and the number of columns. Next *n* lines follow, each contains *m* integers: *j*-th integer in the *i*-th line is *a**i*,<=*j* (|*a**i*,<=*j*|<=≤<=100), the number in the *i*-th row and *j*-th column of the table. The rows of the table numbered from 1 to *n*. The columns of the table numbered from 1 to *m*.
In the first line print the number *a* — the number of required applications of the first spell. Next print *a* space-separated integers — the row numbers, you want to apply a spell. These row numbers must be distinct! In the second line print the number *b* — the number of required applications of the second spell. Next print *b* space-separated integers — the column numbers, you want to apply a spell. These column numbers must be distinct! If there are several solutions are allowed to print any of them.
[ "4 1\n-1\n-1\n-1\n-1\n", "2 4\n-1 -1 -1 2\n1 1 1 1\n" ]
[ "4 1 2 3 4 \n0 \n", "1 1 \n1 4 \n" ]
none
[ { "input": "4 1\n-1\n-1\n-1\n-1", "output": "4 1 2 3 4 \n0 " }, { "input": "2 4\n-1 -1 -1 2\n1 1 1 1", "output": "1 1 \n1 4 " }, { "input": "10 5\n1 7 1 6 -3\n8 -8 0 -7 -8\n7 -10 -8 -3 6\n-3 0 -9 0 -3\n-1 5 -2 -9 10\n-2 9 2 0 7\n5 0 -1 -10 6\n7 -8 -3 -9 1\n-5 10 -10 5 9\n-7 4 -8 0 -4", "output": "6 2 3 4 7 8 10 \n1 1 " }, { "input": "5 10\n-2 -7 -10 -9 5 -9 -3 8 -8 5\n3 0 9 8 -4 -3 -8 1 8 1\n2 3 7 5 -8 -3 0 -9 -7 -2\n-6 -7 0 0 6 9 -8 6 -8 3\n7 9 -4 -5 -9 -3 8 6 -5 6", "output": "2 1 4 \n4 5 6 8 10 " }, { "input": "1 1\n-10", "output": "1 1 \n0 " }, { "input": "20 10\n-6 8 -4 -1 3 10 2 5 4 7\n6 9 9 8 -8 3 7 9 7 3\n0 10 10 1 -3 -4 5 -1 10 10\n8 9 10 4 7 10 10 3 9 10\n3 0 8 -5 0 5 7 8 -5 4\n9 -6 7 10 -4 -2 7 0 -5 9\n-10 7 -4 5 10 8 3 7 1 8\n-3 -6 0 3 2 1 5 9 8 9\n4 -3 5 3 4 -6 9 5 3 4\n2 -4 0 -5 -2 0 5 5 9 7\n-4 -1 5 1 10 9 4 -8 6 6\n2 3 6 8 9 6 5 -7 -2 -5\n6 4 -1 4 4 2 7 3 3 10\n9 0 8 -6 8 7 3 -1 2 3\n-5 -6 4 -7 0 8 8 9 3 10\n9 9 -2 -3 9 -6 -7 3 8 8\n5 9 5 5 4 0 5 9 3 10\n9 3 7 9 3 2 10 2 -2 9\n4 6 7 5 5 9 -3 2 2 -3\n2 3 6 6 3 10 6 5 4 3", "output": "0 \n0 " }, { "input": "10 20\n0 -7 2 -3 3 5 10 4 -8 7 1 -2 8 9 8 9 -4 -5 8 4\n-4 -1 0 1 8 6 4 8 10 0 5 5 9 5 10 1 8 1 1 9\n-8 9 7 2 9 7 5 -1 8 9 -7 9 4 2 2 -4 8 8 0 9\n7 9 10 6 -1 10 -4 6 3 1 9 4 7 -1 10 4 10 -6 -7 7\n-9 7 -2 10 2 1 1 4 5 6 6 10 7 10 9 1 8 9 5 -6\n5 5 10 6 9 3 0 5 1 2 8 3 3 9 5 7 8 -4 4 10\n-5 -8 10 7 6 8 -2 -5 1 5 10 9 9 6 -5 2 8 -2 -3 4\n8 10 10 7 -4 10 8 9 -6 -4 9 2 9 -1 7 8 -6 -2 7 8\n5 4 8 8 8 7 6 9 4 3 9 10 7 8 -1 5 7 9 1 10\n5 -3 1 -3 1 -5 10 9 1 -1 4 6 9 10 0 1 1 10 -4 3", "output": "0 \n0 " }, { "input": "1 70\n98 66 2 43 -22 -31 29 -19 -42 89 -70 7 -41 33 42 -23 67 -4 23 -67 93 77 83 91 5 94 -12 37 -32 -9 69 24 79 54 40 -2 -25 50 2 -19 65 73 77 2 -34 -64 -43 93 28 86 67 -54 61 88 -3 72 63 38 40 4 98 21 31 -35 -38 84 43 62 50 84", "output": "0 \n22 5 6 8 9 11 13 16 18 20 27 29 30 36 37 40 45 46 47 52 55 64 65 " }, { "input": "70 1\n91\n59\n-55\n18\n-8\n4\n93\n34\n-17\n60\n82\n42\n86\n-38\n62\n45\n89\n47\n5\n27\n82\n41\n63\n-71\n58\n53\n27\n91\n69\n-2\n93\n86\n92\n-42\n54\n-48\n41\n12\n-1\n-6\n-34\n20\n10\n-43\n30\n19\n80\n-16\n58\n-13\n-15\n77\n30\n-22\n94\n-38\n93\n79\n8\n30\n60\n25\n-4\n40\n68\n52\n-47\n93\n16\n76", "output": "19 3 5 9 14 24 30 34 36 39 40 41 44 48 50 51 54 56 63 67 \n0 " } ]
62
0
0
18,497
0
none
[ "none" ]
null
null
You are given a sequence of positive integers *a*1,<=*a*2,<=...,<=*a**n*. While possible, you perform the following operation: find a pair of equal consecutive elements. If there are more than one such pair, find the leftmost (with the smallest indices of elements). If the two integers are equal to *x*, delete both and insert a single integer *x*<=+<=1 on their place. This way the number of elements in the sequence is decreased by 1 on each step. You stop performing the operation when there is no pair of equal consecutive elements. For example, if the initial sequence is [5,<=2,<=1,<=1,<=2,<=2], then after the first operation you get [5,<=2,<=2,<=2,<=2], after the second — [5,<=3,<=2,<=2], after the third — [5,<=3,<=3], and finally after the fourth you get [5,<=4]. After that there are no equal consecutive elements left in the sequence, so you stop the process. Determine the final sequence after you stop performing the operation.
The first line contains a single integer *n* (2<=≤<=*n*<=≤<=2·105) — the number of elements in the sequence. The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
In the first line print a single integer *k* — the number of elements in the sequence after you stop performing the operation. In the second line print *k* integers — the sequence after you stop performing the operation.
[ "6\n5 2 1 1 2 2\n", "4\n1000000000 1000000000 1000000000 1000000000\n", "7\n4 10 22 11 12 5 6\n" ]
[ "2\n5 4 ", "1\n1000000002 ", "7\n4 10 22 11 12 5 6 " ]
The first example is described in the statements. In the second example the initial sequence is [1000000000, 1000000000, 1000000000, 1000000000]. After the first operation the sequence is equal to [1000000001, 1000000000, 1000000000]. After the second operation the sequence is [1000000001, 1000000001]. After the third operation the sequence is [1000000002]. In the third example there are no two equal consecutive elements initially, so the sequence does not change.
[ { "input": "6\n5 2 1 1 2 2", "output": "2\n5 4 " }, { "input": "4\n1000000000 1000000000 1000000000 1000000000", "output": "1\n1000000002 " }, { "input": "7\n4 10 22 11 12 5 6", "output": "7\n4 10 22 11 12 5 6 " }, { "input": "2\n1 1", "output": "1\n2 " }, { "input": "3\n2 1 1", "output": "1\n3 " }, { "input": "4\n3 2 1 1", "output": "1\n4 " }, { "input": "7\n5 5 4 4 5 6 7", "output": "3\n7 6 7 " } ]
78
7,065,600
0
18,516
69
Subsegments
[ "data structures", "implementation" ]
E. Subsegments
1
256
Programmer Sasha has recently begun to study data structures. His coach Stas told him to solve the problem of finding a minimum on the segment of the array in , which Sasha coped with. For Sasha not to think that he had learned all, Stas gave him a new task. For each segment of the fixed length Sasha must find the maximum element of those that occur on the given segment exactly once. Help Sasha solve this problem.
The first line contains two positive integers *n* and *k* (1<=≤<=*n*<=≤<=105,<=1<=≤<=*k*<=≤<=*n*) — the number of array elements and the length of the segment. Then follow *n* lines: the *i*-th one contains a single number *a**i* (<=-<=109<=≤<=*a**i*<=≤<=109).
Print *n*–*k*<=+<=1 numbers, one per line: on the *i*-th line print of the maximum number of those numbers from the subarray *a**i* *a**i*<=+<=1 … *a**i*<=+<=*k*<=-<=1 that occur in this subarray exactly 1 time. If there are no such numbers in this subarray, print "Nothing".
[ "5 3\n1\n2\n2\n3\n3\n", "6 4\n3\n3\n3\n4\n4\n2\n" ]
[ "1\n3\n2\n", "4\nNothing\n3\n" ]
none
[ { "input": "5 3\n1\n2\n2\n3\n3", "output": "1\n3\n2" }, { "input": "6 4\n3\n3\n3\n4\n4\n2", "output": "4\nNothing\n3" }, { "input": "10 3\n-55\n-35\n-80\n91\n-96\n-93\n-39\n-77\n4\n29", "output": "-35\n91\n91\n91\n-39\n-39\n4\n29" }, { "input": "10 3\n-13\n26\n-97\n-38\n43\n-12\n80\n3\n8\n45", "output": "26\n26\n43\n43\n80\n80\n80\n45" }, { "input": "10 3\n-84\n25\n-25\n8\n60\n-74\n-98\n48\n-55\n38", "output": "25\n25\n60\n60\n60\n48\n48\n48" }, { "input": "10 3\n-62\n-81\n46\n22\n-84\n19\n-86\n44\n-84\n-73", "output": "46\n46\n46\n22\n19\n44\n44\n44" }, { "input": "10 3\n-6\n2\n79\n-49\n86\n13\n-31\n-71\n57\n93", "output": "79\n79\n86\n86\n86\n13\n57\n93" }, { "input": "10 3\n-38\n68\n-77\n57\n-35\n28\n-61\n-9\n3\n60", "output": "68\n68\n57\n57\n28\n28\n3\n60" }, { "input": "10 3\n2\n-100\n50\n-85\n-48\n68\n-96\n-31\n85\n-29", "output": "50\n50\n50\n68\n68\n68\n85\n85" }, { "input": "10 3\n-20\n-63\n-64\n45\n-84\n-13\n79\n-31\n70\n-100", "output": "-20\n45\n45\n45\n79\n79\n79\n70" } ]
1,000
409,600
0
18,522
595
Pasha and Phone
[ "binary search", "math" ]
null
null
Pasha has recently bought a new phone jPager and started adding his friends' phone numbers there. Each phone number consists of exactly *n* digits. Also Pasha has a number *k* and two sequences of length *n*<=/<=*k* (*n* is divisible by *k*) *a*1,<=*a*2,<=...,<=*a**n*<=/<=*k* and *b*1,<=*b*2,<=...,<=*b**n*<=/<=*k*. Let's split the phone number into blocks of length *k*. The first block will be formed by digits from the phone number that are on positions 1, 2,..., *k*, the second block will be formed by digits from the phone number that are on positions *k*<=+<=1, *k*<=+<=2, ..., 2·*k* and so on. Pasha considers a phone number good, if the *i*-th block doesn't start from the digit *b**i* and is divisible by *a**i* if represented as an integer. To represent the block of length *k* as an integer, let's write it out as a sequence *c*1, *c*2,...,*c**k*. Then the integer is calculated as the result of the expression *c*1·10*k*<=-<=1<=+<=*c*2·10*k*<=-<=2<=+<=...<=+<=*c**k*. Pasha asks you to calculate the number of good phone numbers of length *n*, for the given *k*, *a**i* and *b**i*. As this number can be too big, print it modulo 109<=+<=7.
The first line of the input contains two integers *n* and *k* (1<=≤<=*n*<=≤<=100<=000, 1<=≤<=*k*<=≤<=*min*(*n*,<=9)) — the length of all phone numbers and the length of each block, respectively. It is guaranteed that *n* is divisible by *k*. The second line of the input contains *n*<=/<=*k* space-separated positive integers — sequence *a*1,<=*a*2,<=...,<=*a**n*<=/<=*k* (1<=≤<=*a**i*<=&lt;<=10*k*). The third line of the input contains *n*<=/<=*k* space-separated positive integers — sequence *b*1,<=*b*2,<=...,<=*b**n*<=/<=*k* (0<=≤<=*b**i*<=≤<=9).
Print a single integer — the number of good phone numbers of length *n* modulo 109<=+<=7.
[ "6 2\n38 56 49\n7 3 4\n", "8 2\n1 22 3 44\n5 4 3 2\n" ]
[ "8\n", "32400\n" ]
In the first test sample good phone numbers are: 000000, 000098, 005600, 005698, 380000, 380098, 385600, 385698.
[ { "input": "6 2\n38 56 49\n7 3 4", "output": "8" }, { "input": "8 2\n1 22 3 44\n5 4 3 2", "output": "32400" }, { "input": "2 1\n9 9\n9 9", "output": "1" }, { "input": "2 1\n9 9\n0 9", "output": "1" }, { "input": "4 1\n4 3 2 1\n1 2 3 4", "output": "540" }, { "input": "18 9\n2 3\n0 4", "output": "505000007" }, { "input": "4 4\n1122\n2", "output": "8" }, { "input": "10 5\n8378 11089\n7 5", "output": "99" }, { "input": "10 5\n52057 11807\n0 1", "output": "8" }, { "input": "10 1\n3 1 1 4 8 7 5 6 4 1\n0 0 0 5 5 6 8 8 4 0", "output": "209952" }, { "input": "100 4\n388 2056 122 1525 2912 1465 3066 257 5708 3604 3039 6183 3035 626 1389 5393 3321 3175 2922 2024 3837 437 5836 2376 1599\n6 5 5 2 9 6 8 3 5 0 6 0 1 8 5 3 5 2 3 0 5 6 6 7 3", "output": "652599557" }, { "input": "100 1\n5 3 1 5 6 2 4 8 3 3 1 1 2 8 2 3 8 2 5 2 6 2 3 5 2 1 2 1 2 8 4 3 3 5 1 4 2 2 2 5 8 2 2 6 2 9 2 4 1 8 1 5 5 6 6 1 2 7 3 3 4 2 4 1 2 6 6 4 9 4 3 2 3 8 2 3 1 4 1 4 1 3 5 3 5 5 2 3 4 1 1 8 1 5 6 9 4 2 5 1\n6 0 4 5 3 1 0 7 5 3 9 4 5 4 0 2 1 6 2 2 4 3 1 9 5 9 2 2 6 8 6 5 9 6 4 9 9 7 5 4 5 6 0 3 2 0 8 0 3 9 5 3 8 0 9 3 6 2 9 5 9 3 2 2 2 2 0 8 1 2 9 0 9 8 0 3 2 0 7 9 4 3 7 2 3 1 8 9 8 2 6 0 3 2 9 8 9 2 3 4", "output": "27157528" }, { "input": "100 5\n5302 4362 11965 14930 11312 33797 17413 17850 79562 17981 28002 40852 173 23022 55762 13013 79597 29597 31944 32384\n9 8 7 0 6 6 7 7 5 9 1 3 4 8 7 1 1 6 4 4", "output": "885507108" }, { "input": "1 1\n2\n0", "output": "4" } ]
61
0
0
18,535
371
Vessels
[ "data structures", "dsu", "implementation", "trees" ]
null
null
There is a system of *n* vessels arranged one above the other as shown in the figure below. Assume that the vessels are numbered from 1 to *n*, in the order from the highest to the lowest, the volume of the *i*-th vessel is *a**i* liters. Initially, all the vessels are empty. In some vessels water is poured. All the water that overflows from the *i*-th vessel goes to the (*i*<=+<=1)-th one. The liquid that overflows from the *n*-th vessel spills on the floor. Your task is to simulate pouring water into the vessels. To do this, you will need to handle two types of queries: 1. Add *x**i* liters of water to the *p**i*-th vessel; 1. Print the number of liters of water in the *k**i*-th vessel. When you reply to the second request you can assume that all the water poured up to this point, has already overflown between the vessels.
The first line contains integer *n* — the number of vessels (1<=≤<=*n*<=≤<=2·105). The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* — the vessels' capacities (1<=≤<=*a**i*<=≤<=109). The vessels' capacities do not necessarily increase from the top vessels to the bottom ones (see the second sample). The third line contains integer *m* — the number of queries (1<=≤<=*m*<=≤<=2·105). Each of the next *m* lines contains the description of one query. The query of the first type is represented as "1 *p**i* *x**i*", the query of the second type is represented as "2 *k**i*" (1<=≤<=*p**i*<=≤<=*n*, 1<=≤<=*x**i*<=≤<=109, 1<=≤<=*k**i*<=≤<=*n*).
For each query, print on a single line the number of liters of water in the corresponding vessel.
[ "2\n5 10\n6\n1 1 4\n2 1\n1 2 5\n1 1 4\n2 1\n2 2\n", "3\n5 10 8\n6\n1 1 12\n2 2\n1 1 6\n1 3 2\n2 2\n2 3\n" ]
[ "4\n5\n8\n", "7\n10\n5\n" ]
none
[ { "input": "2\n5 10\n6\n1 1 4\n2 1\n1 2 5\n1 1 4\n2 1\n2 2", "output": "4\n5\n8" }, { "input": "3\n5 10 8\n6\n1 1 12\n2 2\n1 1 6\n1 3 2\n2 2\n2 3", "output": "7\n10\n5" }, { "input": "10\n71 59 88 55 18 98 38 73 53 58\n20\n1 5 93\n1 7 69\n2 3\n1 1 20\n2 10\n1 6 74\n1 7 100\n1 9 14\n2 3\n2 4\n2 7\n1 3 31\n2 4\n1 6 64\n2 2\n2 2\n1 3 54\n2 9\n2 1\n1 6 86", "output": "0\n0\n0\n0\n38\n0\n0\n0\n53\n20" }, { "input": "10\n3 7 10 1 5 4 4 3 3 1\n20\n2 4\n2 4\n1 1 10\n1 1 10\n2 4\n2 3\n1 4 2\n1 4 6\n2 2\n1 8 9\n2 2\n2 5\n2 9\n1 2 1\n1 6 9\n1 1 6\n2 5\n2 2\n2 3\n1 4 10", "output": "0\n0\n0\n10\n7\n7\n5\n3\n5\n7\n10" }, { "input": "50\n57 63 98 44 22 63 5 65 36 69 49 54 61 15 29 79 50 30 43 93 18 94 46 92 72 67 67 51 34 40 50 77 58 53 79 72 72 34 91 75 83 67 71 80 11 51 85 20 6 3\n20\n2 40\n1 14 102\n2 22\n2 15\n2 43\n1 29 532\n2 27\n2 47\n1 24 107\n1 20 720\n1 21 315\n2 20\n1 2 787\n1 27 532\n2 38\n1 32 445\n1 38 17\n1 26 450\n2 40\n1 45 192", "output": "0\n0\n29\n0\n0\n0\n93\n34\n75" }, { "input": "1\n1\n1\n2 1", "output": "0" } ]
826
22,118,400
3
18,544
8
Two Friends
[ "binary search", "geometry" ]
D. Two Friends
1
64
Two neighbours, Alan and Bob, live in the city, where there are three buildings only: a cinema, a shop and the house, where they live. The rest is a big asphalt square. Once they went to the cinema, and the film impressed them so deeply, that when they left the cinema, they did not want to stop discussing it. Bob wants to get home, but Alan has to go to the shop first, and only then go home. So, they agreed to cover some distance together discussing the film (their common path might pass through the shop, or they might walk circles around the cinema together), and then to part each other's company and go each his own way. After they part, they will start thinking about their daily pursuits; and even if they meet again, they won't be able to go on with the discussion. Thus, Bob's path will be a continuous curve, having the cinema and the house as its ends. Alan's path — a continuous curve, going through the shop, and having the cinema and the house as its ends. The film ended late, that's why the whole distance covered by Alan should not differ from the shortest one by more than *t*1, and the distance covered by Bob should not differ from the shortest one by more than *t*2. Find the maximum distance that Alan and Bob will cover together, discussing the film.
The first line contains two integers: *t*1,<=*t*2 (0<=≤<=*t*1,<=*t*2<=≤<=100). The second line contains the cinema's coordinates, the third one — the house's, and the last line — the shop's. All the coordinates are given in meters, are integer, and do not exceed 100 in absolute magnitude. No two given places are in the same building.
In the only line output one number — the maximum distance that Alan and Bob will cover together, discussing the film. Output the answer accurate to not less than 4 decimal places.
[ "0 2\n0 0\n4 0\n-3 0\n", "0 0\n0 0\n2 0\n1 0\n" ]
[ "1.0000000000\n", "2.0000000000\n" ]
none
[ { "input": "0 2\n0 0\n4 0\n-3 0", "output": "1.0000000000" }, { "input": "0 0\n0 0\n2 0\n1 0", "output": "2.0000000000" }, { "input": "0 2\n0 0\n40 0\n-31 1", "output": "1.0002538218" }, { "input": "100 2\n0 0\n4 0\n-3 0", "output": "6.0000000000" }, { "input": "2 100\n0 0\n4 0\n-3 0", "output": "12.0000000000" }, { "input": "0 0\n0 0\n5 0\n10 0", "output": "5.0000000000" }, { "input": "2 0\n0 0\n5 0\n10 0", "output": "5.0000000000" }, { "input": "0 2\n0 0\n5 0\n10 0", "output": "6.0000000000" }, { "input": "0 0\n0 0\n4 0\n4 3", "output": "0.0000299999" }, { "input": "0 4\n0 0\n4 0\n4 3", "output": "8.0000000000" }, { "input": "0 3\n0 0\n4 0\n4 3", "output": "4.3421111488" }, { "input": "1 4\n0 0\n4 0\n4 3", "output": "8.0000000000" }, { "input": "0 0\n0 0\n100 100\n100 0", "output": "0.0000241421" }, { "input": "1 1\n0 0\n100 100\n100 0", "output": "11.8620549792" }, { "input": "0 0\n0 0\n-1 -1\n1 -1", "output": "0.0000099999" }, { "input": "0 0\n1 2\n1 -2\n1 1", "output": "4.0000000000" }, { "input": "2 1\n1 3\n0 1\n0 0", "output": "3.2360679775" }, { "input": "1 1\n1 1\n-1 -4\n4 -3", "output": "3.4140469620" }, { "input": "2 4\n3 1\n-5 -1\n2 2", "output": "11.0299866682" }, { "input": "1 1\n5 5\n8 -1\n-3 -3", "output": "3.4363113325" }, { "input": "0 8\n13 -2\n15 -16\n4 9", "output": "4.2383217359" }, { "input": "4 12\n1 -3\n-21 -29\n30 10", "output": "8.8182331094" }, { "input": "25 22\n22 -21\n14 -25\n8 21", "output": "26.9132525667" }, { "input": "10 20\n-2 -18\n30 26\n-14 38", "output": "47.7344371741" }, { "input": "12 3\n9 7\n-26 -30\n-27 33", "output": "19.0794600792" }, { "input": "31 21\n-42 42\n-2 10\n33 -40", "output": "72.2249938995" }, { "input": "23 68\n60 -68\n-70 13\n-50 26", "output": "191.5464988652" }, { "input": "29 26\n-33 2\n15 -42\n-52 48", "output": "28.8420189634" }, { "input": "4 5\n-84 52\n-20 -39\n51 91", "output": "20.5654840916" }, { "input": "43 30\n88 45\n-41 -76\n-26 -46", "output": "206.8671818060" }, { "input": "2 32\n-91 -7\n-69 -99\n3 -7", "output": "42.1044438082" }, { "input": "28 0\n-98 -73\n45 -19\n-82 -60", "output": "33.3590240317" }, { "input": "7 69\n75 -97\n34 89\n-21 19", "output": "246.5947108716" }, { "input": "1 45\n-52 36\n-86 7\n46 80", "output": "23.6287723817" }, { "input": "36 7\n64 -16\n18 -95\n50 46", "output": "26.4522291627" }, { "input": "0 5\n3 9\n-7 -10\n-9 -10", "output": "24.4722050542" }, { "input": "1 0\n4 4\n6 1\n6 -10", "output": "3.6055512755" }, { "input": "2 6\n77 86\n-48 -76\n-65 28", "output": "68.8370182423" }, { "input": "32 8\n-74 -85\n69 -13\n-59 64", "output": "80.9525769682" }, { "input": "7 0\n2 3\n-3 -8\n7 -9", "output": "10.3637557241" }, { "input": "3 0\n3 0\n-10 -5\n-8 10", "output": "4.3880652624" }, { "input": "48 3\n-17 -70\n20 91\n-92 -100", "output": "34.1872425101" }, { "input": "24 3\n-86 1\n3 70\n-71 -85", "output": "21.3881481586" }, { "input": "0 11\n7 -11\n0 19\n-13 -16", "output": "9.4432695006" }, { "input": "38 5\n-99 16\n91 45\n22 -70", "output": "105.0959316870" }, { "input": "5 0\n75 -56\n-12 24\n99 19", "output": "8.0208146392" }, { "input": "39 0\n-7 -57\n52 -77\n24 -98", "output": "57.7524735602" }, { "input": "0 5\n-20 -2\n16 -6\n16 -2", "output": "40.0000000000" }, { "input": "26 6\n98 -44\n60 67\n-41 -15", "output": "64.2681170638" }, { "input": "13 5\n-95 7\n-39 81\n65 -47", "output": "34.7382933870" }, { "input": "5 0\n-20 -13\n-9 -5\n10 -13", "output": "13.6014705087" }, { "input": "5 0\n8 -19\n-6 -12\n-15 -14", "output": "15.6524758425" }, { "input": "10 1\n-71 23\n51 92\n-72 10", "output": "7.4826465918" }, { "input": "4 0\n8 -4\n7 7\n-6 9", "output": "9.4951445784" }, { "input": "45 0\n85 34\n-21 -47\n28 44", "output": "56.8197308538" }, { "input": "5 0\n14 17\n20 5\n17 -20", "output": "13.4164078650" }, { "input": "80 3\n35 -55\n-36 -53\n-96 -53", "output": "74.0281634283" }, { "input": "3 4\n-80 -78\n23 -81\n-49 -50", "output": "27.3700259542" }, { "input": "82 92\n-85 44\n19 -65\n-47 -1", "output": "232.8329767670" }, { "input": "1 65\n-56 -85\n7 34\n-38 68", "output": "148.7518367529" }, { "input": "83 99\n53 100\n-74 -28\n-29 32", "output": "264.5269918847" }, { "input": "65 12\n41 38\n5 4\n-96 -53", "output": "61.5176736126" }, { "input": "95 75\n-99 -26\n55 -1\n72 17", "output": "231.0160248180" }, { "input": "68 11\n-85 65\n-70 61\n38 12", "output": "26.5241746963" }, { "input": "18 26\n68 67\n-46 -36\n-46 95", "output": "84.4001524822" }, { "input": "72 52\n-47 -88\n33 1\n88 51", "output": "171.6703806295" }, { "input": "39 99\n-93 -91\n66 -6\n87 -12", "output": "257.4134712717" }, { "input": "38 23\n2 45\n49 2\n87 -69", "output": "86.7024332345" }, { "input": "83 0\n94 -62\n0 -34\n-87 49", "output": "98.0815986819" }, { "input": "16 68\n31 -70\n10 17\n4 30", "output": "133.8987073310" }, { "input": "67 10\n92 -2\n-58 -79\n70 86", "output": "61.1979627104" }, { "input": "49 9\n-91 -29\n-54 -72\n73 6", "output": "65.7274184147" }, { "input": "48 59\n-16 -32\n-64 76\n-26 49", "output": "176.2303976942" }, { "input": "85 4\n94 100\n-16 40\n45 -26", "output": "129.2996408614" }, { "input": "0 2\n0 0\n40 0\n-31 0", "output": "1.0000000000" } ]
30
0
0
18,582
924
Minimal Subset Difference
[ "dp" ]
null
null
We call a positive integer *x* a *k*-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to *k*. Each digit should belong to exactly one subset after the split. There are *n* queries for you. Each query is described with three integers *l*, *r* and *k*, which mean that you are asked how many integers *x* between *l* and *r* (inclusive) are *k*-beautiful.
The first line contains a single integer *n* (1<=≤<=*n*<=≤<=5·104), indicating the number of queries. Each of the next *n* lines describes a query, containing three integers *l*, *r* and *k* (1<=≤<=*l*<=≤<=*r*<=≤<=1018, 0<=≤<=*k*<=≤<=9).
For each query print a single number — the answer to the query.
[ "10\n1 100 0\n1 100 1\n1 100 2\n1 100 3\n1 100 4\n1 100 5\n1 100 6\n1 100 7\n1 100 8\n1 100 9\n", "10\n1 1000 0\n1 1000 1\n1 1000 2\n1 1000 3\n1 1000 4\n1 1000 5\n1 1000 6\n1 1000 7\n1 1000 8\n1 1000 9\n" ]
[ "9\n28\n44\n58\n70\n80\n88\n94\n98\n100\n", "135\n380\n573\n721\n830\n906\n955\n983\n996\n1000\n" ]
If 1 ≤ *x* ≤ 9, integer *x* is *k*-beautiful if and only if *x* ≤ *k*. If 10 ≤ *x* ≤ 99, integer *x* = 10*a* + *b* is *k*-beautiful if and only if |*a* - *b*| ≤ *k*, where *a* and *b* are integers between 0 and 9, inclusive. 100 is *k*-beautiful if and only if *k* ≥ 1.
[]
374
9,728,000
0
18,600
199
Special Olympics
[ "geometry" ]
null
null
A renowned abstract artist Sasha, drawing inspiration from nowhere, decided to paint a picture entitled "Special Olympics". He justly thought that, if the regular Olympic games have five rings, then the Special ones will do with exactly two rings just fine. Let us remind you that a ring is a region located between two concentric circles with radii *r* and *R* (*r*<=&lt;<=*R*). These radii are called internal and external, respectively. Concentric circles are circles with centers located at the same point. Soon a white canvas, which can be considered as an infinite Cartesian plane, had two perfect rings, painted with solid black paint. As Sasha is very impulsive, the rings could have different radii and sizes, they intersect and overlap with each other in any way. We know only one thing for sure: the centers of the pair of rings are not the same. When Sasha got tired and fell into a deep sleep, a girl called Ilona came into the room and wanted to cut a circle for the sake of good memories. To make the circle beautiful, she decided to cut along the contour. We'll consider a contour to be a continuous closed line through which there is transition from one color to another (see notes for clarification). If the contour takes the form of a circle, then the result will be cutting out a circle, which Iona wants. But the girl's inquisitive mathematical mind does not rest: how many ways are there to cut a circle out of the canvas?
The input contains two lines. Each line has four space-separated integers *x**i*, *y**i*, *r**i*, *R**i*, that describe the *i*-th ring; *x**i* and *y**i* are coordinates of the ring's center, *r**i* and *R**i* are the internal and external radii of the ring correspondingly (<=-<=100<=≤<=*x**i*,<=*y**i*<=≤<=100; 1<=≤<=*r**i*<=&lt;<=*R**i*<=≤<=100). It is guaranteed that the centers of the rings do not coinside.
A single integer — the number of ways to cut out a circle from the canvas.
[ "60 60 45 55\n80 80 8 32\n", "60 60 45 55\n80 60 15 25\n", "50 50 35 45\n90 50 35 45\n" ]
[ "1", "4", "0" ]
Figures for test samples are given below. The possible cuts are marked with red dotted line.
[ { "input": "60 60 45 55\n80 80 8 32", "output": "1" }, { "input": "60 60 45 55\n80 60 15 25", "output": "4" }, { "input": "50 50 35 45\n90 50 35 45", "output": "0" }, { "input": "0 0 50 70\n1 0 60 80", "output": "2" }, { "input": "0 0 1 2\n10 0 2 20", "output": "2" }, { "input": "31 13 22 95\n48 63 21 98", "output": "0" }, { "input": "31 40 37 76\n48 65 66 98", "output": "0" }, { "input": "-65 -81 37 76\n48 65 66 98", "output": "4" }, { "input": "41 -14 37 76\n48 65 66 98", "output": "0" }, { "input": "41 -14 16 100\n48 17 37 66", "output": "1" }, { "input": "-75 -9 20 40\n25 55 99 100", "output": "0" }, { "input": "-45 6 20 40\n35 6 99 100", "output": "0" }, { "input": "-3 84 20 40\n76 96 96 100", "output": "0" }, { "input": "10 -91 20 40\n70 -91 79 100", "output": "1" }, { "input": "-64 -47 20 40\n-5 -37 79 100", "output": "1" }, { "input": "-63 97 20 40\n-34 97 11 48", "output": "0" }, { "input": "-67 47 20 40\n-38 47 11 49", "output": "0" }, { "input": "-100 -91 20 40\n-71 -91 11 68", "output": "0" }, { "input": "45 -76 20 40\n69 -69 15 65", "output": "1" }, { "input": "12 -43 20 40\n41 -43 11 97", "output": "1" }, { "input": "10 71 20 40\n39 78 10 49", "output": "0" }, { "input": "56 44 20 40\n83 44 12 13", "output": "1" }, { "input": "-20 78 20 40\n8 85 10 11", "output": "1" }, { "input": "65 -9 20 40\n94 -9 10 49", "output": "0" }, { "input": "-84 -59 20 40\n-74 -59 29 30", "output": "1" }, { "input": "33 -37 20 40\n42 -37 28 29", "output": "1" }, { "input": "-25 10 20 40\n4 17 10 69", "output": "0" }, { "input": "13 32 20 40\n42 32 10 69", "output": "1" }, { "input": "-12 -1 20 40\n-3 -1 28 31", "output": "1" }, { "input": "48 30 20 40\n77 37 10 99", "output": "1" }, { "input": "47 -50 20 40\n56 -46 28 30", "output": "1" }, { "input": "-26 -65 20 40\n52 -65 98 100", "output": "1" }, { "input": "-46 36 20 40\n14 36 80 100", "output": "2" }, { "input": "19 96 20 40\n77 96 78 99", "output": "2" }, { "input": "-42 -44 20 40\n-32 -44 30 48", "output": "1" }, { "input": "83 -23 20 40\n93 -23 30 50", "output": "2" }, { "input": "-100 -97 20 40\n-90 -97 30 100", "output": "2" }, { "input": "65 16 20 40\n74 16 29 48", "output": "1" }, { "input": "-66 78 20 40\n-62 81 25 45", "output": "2" }, { "input": "-11 63 20 40\n-2 63 29 31", "output": "2" }, { "input": "91 -59 20 40\n100 -59 29 100", "output": "2" }, { "input": "39 90 20 40\n47 90 28 31", "output": "2" }, { "input": "-100 40 20 40\n-81 40 1 38", "output": "1" }, { "input": "24 -24 20 40\n43 -24 1 21", "output": "2" }, { "input": "-8 35 20 40\n11 35 1 19", "output": "2" }, { "input": "-52 -94 20 40\n-33 -94 1 39", "output": "1" }, { "input": "61 2 20 40\n67 10 10 30", "output": "2" }, { "input": "49 -67 20 40\n57 -67 12 28", "output": "2" }, { "input": "65 17 20 40\n84 17 1 58", "output": "1" }, { "input": "-16 -18 20 40\n3 -18 1 59", "output": "2" }, { "input": "24 -16 20 40\n33 -16 11 31", "output": "2" }, { "input": "-83 96 20 40\n-64 96 1 98", "output": "2" }, { "input": "-10 89 20 40\n-2 89 12 29", "output": "2" }, { "input": "-40 -69 20 40\n60 -69 80 100", "output": "0" }, { "input": "-70 66 20 40\n8 66 58 98", "output": "0" }, { "input": "-11 -97 20 40\n67 -97 58 100", "output": "0" }, { "input": "-60 60 20 40\n0 60 40 100", "output": "1" }, { "input": "0 73 20 40\n59 73 39 100", "output": "1" }, { "input": "28 -91 20 40\n58 -91 10 49", "output": "0" }, { "input": "75 72 20 40\n99 90 10 50", "output": "0" }, { "input": "-84 74 20 40\n-54 74 10 63", "output": "0" }, { "input": "35 -6 20 40\n59 12 10 70", "output": "1" }, { "input": "67 41 20 40\n97 41 10 98", "output": "1" }, { "input": "-27 -68 20 40\n2 -68 9 48", "output": "0" }, { "input": "50 13 20 40\n78 13 8 12", "output": "1" }, { "input": "-73 36 20 40\n-44 36 9 10", "output": "1" }, { "input": "70 92 20 40\n99 92 9 49", "output": "0" }, { "input": "37 -80 20 40\n66 -80 9 66", "output": "0" }, { "input": "8 -95 20 40\n36 -95 8 68", "output": "1" }, { "input": "-9 77 20 40\n20 77 9 100", "output": "1" }, { "input": "-37 20 20 40\n41 31 99 100", "output": "1" }, { "input": "-36 28 20 40\n24 28 99 100", "output": "2" }, { "input": "-77 -16 20 40\n-18 -6 99 100", "output": "2" }, { "input": "-65 24 20 40\n-6 24 99 100", "output": "4" }, { "input": "-55 23 20 40\n-46 23 31 48", "output": "1" }, { "input": "-37 18 20 40\n-30 18 33 47", "output": "2" }, { "input": "-45 -93 20 40\n-36 -93 31 99", "output": "2" }, { "input": "-97 -29 20 40\n-39 -19 99 100", "output": "4" }, { "input": "14 18 20 40\n23 22 30 49", "output": "1" }, { "input": "-90 -38 20 40\n-81 -38 30 49", "output": "2" }, { "input": "52 -4 20 40\n61 -4 30 31", "output": "2" }, { "input": "-54 46 20 40\n-45 50 30 98", "output": "2" }, { "input": "74 -34 20 40\n82 -30 30 31", "output": "2" }, { "input": "23 -61 20 40\n41 -55 1 37", "output": "1" }, { "input": "57 -86 20 40\n75 -86 1 22", "output": "2" }, { "input": "-38 43 20 40\n-20 49 1 20", "output": "2" }, { "input": "-19 10 20 40\n-2 10 2 37", "output": "1" }, { "input": "64 58 20 40\n74 58 7 30", "output": "2" }, { "input": "53 49 20 40\n62 49 10 29", "output": "2" }, { "input": "53 80 20 40\n70 80 2 3", "output": "4" }, { "input": "73 -41 20 40\n91 -35 1 49", "output": "1" }, { "input": "-8 -34 20 40\n9 -34 2 57", "output": "2" }, { "input": "51 -40 20 40\n60 -40 9 31", "output": "2" }, { "input": "-29 87 20 40\n-11 93 1 94", "output": "2" }, { "input": "-64 3 20 40\n-55 7 6 30", "output": "2" }, { "input": "24 36 20 40\n41 39 1 2", "output": "4" }, { "input": "-56 -64 20 40\n44 2 96 100", "output": "0" }, { "input": "-59 -17 20 40\n21 -17 59 100", "output": "0" }, { "input": "-43 -3 20 40\n57 -3 79 80", "output": "1" }, { "input": "20 57 20 40\n99 69 58 100", "output": "0" }, { "input": "36 82 20 40\n96 82 38 100", "output": "1" }, { "input": "-55 37 20 40\n4 47 38 100", "output": "1" }, { "input": "-58 -4 20 40\n42 91 99 100", "output": "1" }, { "input": "28 51 20 40\n67 51 1 58", "output": "0" }, { "input": "-79 -62 20 40\n-41 -62 2 58", "output": "0" }, { "input": "-19 -10 20 40\n20 -10 1 19", "output": "1" }, { "input": "-95 -64 20 40\n-56 -64 1 78", "output": "0" }, { "input": "-17 -7 20 40\n22 -7 1 79", "output": "1" }, { "input": "-45 86 20 40\n-6 86 1 99", "output": "1" }, { "input": "-71 -23 20 40\n-32 -23 1 18", "output": "1" }, { "input": "-20 11 20 40\n80 11 60 100", "output": "1" }, { "input": "-27 97 20 40\n51 97 38 98", "output": "1" }, { "input": "-47 -84 20 40\n52 -64 61 81", "output": "2" }, { "input": "-81 99 20 40\n-3 99 38 99", "output": "1" }, { "input": "-54 25 20 40\n6 25 20 100", "output": "2" }, { "input": "-22 40 20 40\n36 40 18 100", "output": "2" }, { "input": "-71 15 20 40\n29 90 85 100", "output": "2" }, { "input": "31 -13 20 40\n69 -5 1 56", "output": "0" }, { "input": "-46 55 20 40\n-17 55 7 11", "output": "1" }, { "input": "-35 25 20 40\n-6 32 7 10", "output": "1" }, { "input": "27 -98 20 40\n65 -98 1 58", "output": "0" }, { "input": "-100 -19 20 40\n-62 -19 1 18", "output": "1" }, { "input": "48 66 20 40\n78 66 9 10", "output": "2" }, { "input": "-37 -22 20 40\n-8 -22 8 9", "output": "2" }, { "input": "-42 41 20 40\n-4 49 1 78", "output": "0" }, { "input": "-2 -27 20 40\n35 -27 1 77", "output": "1" }, { "input": "-28 -36 20 40\n10 -28 1 100", "output": "1" }, { "input": "-17 31 20 40\n21 39 1 14", "output": "1" }, { "input": "1 44 20 40\n39 44 1 2", "output": "2" }, { "input": "21 -99 20 40\n58 -97 1 2", "output": "2" }, { "input": "-86 -97 20 40\n14 -31 79 100", "output": "1" }, { "input": "-33 42 20 40\n47 42 39 100", "output": "1" }, { "input": "-79 45 20 40\n21 45 57 80", "output": "2" }, { "input": "-99 -66 20 40\n-20 -54 39 100", "output": "1" }, { "input": "39 -44 20 40\n99 -44 17 100", "output": "2" }, { "input": "10 86 20 40\n69 96 19 100", "output": "2" }, { "input": "-72 -4 20 40\n28 93 99 100", "output": "2" }, { "input": "-81 -55 20 40\n19 20 83 85", "output": "4" }, { "input": "-65 -34 20 40\n35 66 99 100", "output": "4" }, { "input": "-91 -46 10 50\n-73 -40 30 31", "output": "2" } ]
154
0
3
18,626
863
Almost Permutation
[ "flows" ]
null
null
Recently Ivan noticed an array *a* while debugging his code. Now Ivan can't remember this array, but the bug he was trying to fix didn't go away, so Ivan thinks that the data from this array might help him to reproduce the bug. Ivan clearly remembers that there were *n* elements in the array, and each element was not less than 1 and not greater than *n*. Also he remembers *q* facts about the array. There are two types of facts that Ivan remembers: - 1 *l**i* *r**i* *v**i* — for each *x* such that *l**i*<=≤<=*x*<=≤<=*r**i* *a**x*<=≥<=*v**i*; - 2 *l**i* *r**i* *v**i* — for each *x* such that *l**i*<=≤<=*x*<=≤<=*r**i* *a**x*<=≤<=*v**i*. Also Ivan thinks that this array was a permutation, but he is not so sure about it. He wants to restore some array that corresponds to the *q* facts that he remembers and is very similar to permutation. Formally, Ivan has denoted the *cost* of array as follows: , where *cnt*(*i*) is the number of occurences of *i* in the array. Help Ivan to determine minimum possible *cost* of the array that corresponds to the facts!
The first line contains two integer numbers *n* and *q* (1<=≤<=*n*<=≤<=50, 0<=≤<=*q*<=≤<=100). Then *q* lines follow, each representing a fact about the array. *i*-th line contains the numbers *t**i*, *l**i*, *r**i* and *v**i* for *i*-th fact (1<=≤<=*t**i*<=≤<=2, 1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*, 1<=≤<=*v**i*<=≤<=*n*, *t**i* denotes the type of the fact).
If the facts are controversial and there is no array that corresponds to them, print -1. Otherwise, print minimum possible *cost* of the array.
[ "3 0\n", "3 1\n1 1 3 2\n", "3 2\n1 1 3 2\n2 1 3 2\n", "3 2\n1 1 3 2\n2 1 3 1\n" ]
[ "3\n", "5\n", "9\n", "-1\n" ]
none
[ { "input": "3 0", "output": "3" }, { "input": "3 1\n1 1 3 2", "output": "5" }, { "input": "3 2\n1 1 3 2\n2 1 3 2", "output": "9" }, { "input": "3 2\n1 1 3 2\n2 1 3 1", "output": "-1" }, { "input": "50 0", "output": "50" }, { "input": "50 1\n2 31 38 25", "output": "50" }, { "input": "50 2\n2 38 41 49\n1 19 25 24", "output": "50" }, { "input": "50 10\n2 4 24 29\n1 14 49 9\n2 21 29 12\n2 2 46 11\n2 4 11 38\n2 3 36 8\n1 24 47 28\n2 23 40 32\n1 16 50 38\n1 31 49 38", "output": "-1" }, { "input": "50 20\n1 14 22 40\n1 23 41 3\n1 32 39 26\n1 8 47 25\n2 5 13 28\n2 2 17 32\n1 23 30 37\n1 33 45 49\n2 13 27 43\n1 30 32 2\n2 28 49 40\n2 33 35 32\n2 5 37 30\n1 45 45 32\n2 6 24 24\n2 28 44 16\n2 36 47 24\n1 5 11 9\n1 9 37 22\n1 28 40 24", "output": "-1" }, { "input": "50 1\n1 12 38 31", "output": "64" }, { "input": "50 2\n2 6 35 37\n1 19 46 44", "output": "-1" }, { "input": "50 10\n1 17 44 44\n2 32 40 4\n2 1 45 31\n1 27 29 16\n1 8 9 28\n2 1 34 16\n2 16 25 2\n2 17 39 32\n1 16 35 34\n1 1 28 12", "output": "-1" }, { "input": "50 20\n1 44 48 43\n1 15 24 9\n2 39 44 25\n1 36 48 35\n1 4 30 27\n1 31 44 15\n2 19 38 22\n2 18 43 24\n1 25 35 10\n2 38 43 5\n2 10 22 21\n2 5 19 30\n1 17 35 26\n1 17 31 10\n2 9 21 1\n2 29 34 10\n2 25 44 21\n2 13 33 13\n2 34 38 9\n2 23 43 4", "output": "-1" }, { "input": "50 1\n2 12 34 9", "output": "88" }, { "input": "50 2\n1 15 16 17\n2 12 35 41", "output": "50" }, { "input": "50 10\n2 31 38 4\n2 33 43 1\n2 33 46 21\n2 37 48 17\n1 12 46 33\n2 25 44 43\n1 12 50 2\n1 15 35 18\n2 9 13 35\n1 2 25 28", "output": "-1" }, { "input": "50 20\n1 7 49 43\n1 10 18 42\n2 10 37 24\n1 45 46 24\n2 5 36 33\n2 17 40 20\n1 22 30 7\n1 5 49 25\n2 18 49 21\n1 43 49 39\n2 9 25 23\n1 10 19 47\n2 36 48 10\n1 25 30 50\n1 15 49 13\n1 10 17 33\n2 8 33 7\n2 28 36 34\n2 40 40 16\n1 1 17 31", "output": "-1" }, { "input": "1 0", "output": "1" }, { "input": "1 1\n1 1 1 1", "output": "1" }, { "input": "50 1\n2 1 2 1", "output": "52" }, { "input": "50 2\n2 1 33 1\n2 14 50 1", "output": "2500" }, { "input": "49 10\n2 17 19 14\n1 6 46 9\n2 19 32 38\n2 27 31 15\n2 38 39 17\n1 30 36 14\n2 35 41 8\n1 18 23 32\n2 8 35 13\n2 24 32 45", "output": "-1" }, { "input": "49 7\n1 17 44 13\n1 14 22 36\n1 27 39 3\n2 20 36 16\n2 29 31 49\n1 32 40 10\n2 4 48 48", "output": "-1" }, { "input": "50 8\n2 11 44 10\n2 2 13 2\n2 23 35 41\n1 16 28 17\n2 21 21 46\n1 22 39 43\n2 10 29 34\n1 17 27 22", "output": "-1" }, { "input": "5 2\n1 1 2 4\n1 3 5 5", "output": "13" }, { "input": "4 3\n2 1 2 2\n1 2 2 2\n2 3 4 1", "output": "8" }, { "input": "5 2\n1 1 5 4\n2 3 5 4", "output": "13" }, { "input": "42 16\n2 33 37 36\n1 14 18 1\n2 24 25 9\n2 4 34 29\n2 32 33 8\n2 27 38 23\n2 1 1 7\n2 15 42 35\n2 37 42 17\n2 8 13 4\n2 19 21 40\n2 37 38 6\n2 33 38 18\n2 12 40 26\n2 27 42 38\n2 40 40 30", "output": "64" }, { "input": "7 3\n2 1 2 2\n1 3 7 2\n2 3 7 3", "output": "17" }, { "input": "29 5\n2 4 9 27\n1 25 29 14\n1 9 10 18\n2 13 13 5\n2 1 19 23", "output": "29" }, { "input": "3 6\n1 1 1 2\n2 1 1 2\n1 2 2 2\n2 2 2 2\n1 3 3 2\n2 3 3 3", "output": "5" }, { "input": "7 14\n1 1 1 1\n2 1 1 6\n1 2 2 1\n2 2 2 5\n1 3 3 1\n2 3 3 6\n1 4 4 5\n2 4 4 7\n1 5 5 1\n2 5 5 2\n1 6 6 2\n2 6 6 2\n1 7 7 5\n2 7 7 5", "output": "7" }, { "input": "8 16\n1 1 1 2\n2 1 1 3\n1 2 2 6\n2 2 2 8\n1 3 3 1\n2 3 3 2\n1 4 4 3\n2 4 4 3\n1 5 5 1\n2 5 5 2\n1 6 6 2\n2 6 6 5\n1 7 7 3\n2 7 7 3\n1 8 8 3\n2 8 8 3", "output": "16" } ]
280
1,843,200
3
18,659
44
Toys
[ "brute force", "combinatorics" ]
I. Toys
5
256
Little Masha loves arranging her toys into piles on the floor. And she also hates it when somebody touches her toys. One day Masha arranged all her *n* toys into several piles and then her elder brother Sasha came and gathered all the piles into one. Having seen it, Masha got very upset and started crying. Sasha still can't calm Masha down and mom is going to come home soon and punish Sasha for having made Masha crying. That's why he decides to restore the piles' arrangement. However, he doesn't remember at all the way the toys used to lie. Of course, Masha remembers it, but she can't talk yet and can only help Sasha by shouting happily when he arranges the toys in the way they used to lie. That means that Sasha will have to arrange the toys in every possible way until Masha recognizes the needed arrangement. The relative position of the piles and toys in every pile is irrelevant, that's why the two ways of arranging the toys are considered different if can be found two such toys that when arranged in the first way lie in one and the same pile and do not if arranged in the second way. Sasha is looking for the fastest way of trying all the ways because mom will come soon. With every action Sasha can take a toy from any pile and move it to any other pile (as a result a new pile may appear or the old one may disappear). Sasha wants to find the sequence of actions as a result of which all the pile arrangement variants will be tried exactly one time each. Help Sasha. As we remember, initially all the toys are located in one pile.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=10) — the number of toys.
In the first line print the number of different variants of arrangement of toys into piles. Then print all the ways of arranging toys into piles in the order in which Sasha should try them (i.e. every next way must result from the previous one through the operation described in the statement). Every way should be printed in the following format. In every pile the toys should be arranged in ascending order of the numbers. Then the piles should be sorted in ascending order of the numbers of the first toys there. Output every way on a single line. Cf. the example to specify the output data format. If the solution is not unique, output any of them.
[ "3\n" ]
[ "5\n{1,2,3}\n{1,2},{3}\n{1},{2,3}\n{1},{2},{3}\n{1,3},{2}" ]
none
[ { "input": "3", "output": "5\n{1,2,3}\n{1,2},{3}\n{1},{2,3}\n{1},{2},{3}\n{1,3},{2}" }, { "input": "1", "output": "1\n{1}" }, { "input": "2", "output": "2\n{1,2}\n{1},{2}" }, { "input": "4", "output": "15\n{1,2,3,4}\n{1,2,3},{4}\n{1,2},{3,4}\n{1,2},{3},{4}\n{1,2,4},{3}\n{1,4},{2,3}\n{1},{2,3},{4}\n{1},{2,3,4}\n{1},{2,4},{3}\n{1},{2},{3,4}\n{1},{2},{3},{4}\n{1,4},{2},{3}\n{1,3,4},{2}\n{1,3},{2},{4}\n{1,3},{2,4}" }, { "input": "5", "output": "52\n{1,2,3,4,5}\n{1,2,3,4},{5}\n{1,2,3},{4,5}\n{1,2,3},{4},{5}\n{1,2,3,5},{4}\n{1,2,5},{3,4}\n{1,2},{3,4},{5}\n{1,2},{3,4,5}\n{1,2},{3,5},{4}\n{1,2},{3},{4,5}\n{1,2},{3},{4},{5}\n{1,2,5},{3},{4}\n{1,2,4,5},{3}\n{1,2,4},{3},{5}\n{1,2,4},{3,5}\n{1,4},{2,3,5}\n{1,4},{2,3},{5}\n{1,4,5},{2,3}\n{1,5},{2,3},{4}\n{1},{2,3},{4},{5}\n{1},{2,3},{4,5}\n{1},{2,3,5},{4}\n{1},{2,3,4,5}\n{1},{2,3,4},{5}\n{1,5},{2,3,4}\n{1,5},{2,4},{3}\n{1},{2,4},{3},{5}\n{1},{2,4},{3,5}\n{1},{2,4,5},{3}\n{1},{2,5},{3,4}\n{1},{2},{3,4,5}\n..." }, { "input": "6", "output": "203\n{1,2,3,4,5,6}\n{1,2,3,4,5},{6}\n{1,2,3,4},{5,6}\n{1,2,3,4},{5},{6}\n{1,2,3,4,6},{5}\n{1,2,3,6},{4,5}\n{1,2,3},{4,5},{6}\n{1,2,3},{4,5,6}\n{1,2,3},{4,6},{5}\n{1,2,3},{4},{5,6}\n{1,2,3},{4},{5},{6}\n{1,2,3,6},{4},{5}\n{1,2,3,5,6},{4}\n{1,2,3,5},{4},{6}\n{1,2,3,5},{4,6}\n{1,2,5},{3,4,6}\n{1,2,5},{3,4},{6}\n{1,2,5,6},{3,4}\n{1,2,6},{3,4},{5}\n{1,2},{3,4},{5},{6}\n{1,2},{3,4},{5,6}\n{1,2},{3,4,6},{5}\n{1,2},{3,4,5,6}\n{1,2},{3,4,5},{6}\n{1,2,6},{3,4,5}\n{1,2,6},{3,5},{4}\n{1,2},{3,5},{4},{6}\n{1,2},{3,5},{..." }, { "input": "7", "output": "877\n{1,2,3,4,5,6,7}\n{1,2,3,4,5,6},{7}\n{1,2,3,4,5},{6,7}\n{1,2,3,4,5},{6},{7}\n{1,2,3,4,5,7},{6}\n{1,2,3,4,7},{5,6}\n{1,2,3,4},{5,6},{7}\n{1,2,3,4},{5,6,7}\n{1,2,3,4},{5,7},{6}\n{1,2,3,4},{5},{6,7}\n{1,2,3,4},{5},{6},{7}\n{1,2,3,4,7},{5},{6}\n{1,2,3,4,6,7},{5}\n{1,2,3,4,6},{5},{7}\n{1,2,3,4,6},{5,7}\n{1,2,3,6},{4,5,7}\n{1,2,3,6},{4,5},{7}\n{1,2,3,6,7},{4,5}\n{1,2,3,7},{4,5},{6}\n{1,2,3},{4,5},{6},{7}\n{1,2,3},{4,5},{6,7}\n{1,2,3},{4,5,7},{6}\n{1,2,3},{4,5,6,7}\n{1,2,3},{4,5,6},{7}\n{1,2,3,7},{4,5,6}\n{1,..." }, { "input": "8", "output": "4140\n{1,2,3,4,5,6,7,8}\n{1,2,3,4,5,6,7},{8}\n{1,2,3,4,5,6},{7,8}\n{1,2,3,4,5,6},{7},{8}\n{1,2,3,4,5,6,8},{7}\n{1,2,3,4,5,8},{6,7}\n{1,2,3,4,5},{6,7},{8}\n{1,2,3,4,5},{6,7,8}\n{1,2,3,4,5},{6,8},{7}\n{1,2,3,4,5},{6},{7,8}\n{1,2,3,4,5},{6},{7},{8}\n{1,2,3,4,5,8},{6},{7}\n{1,2,3,4,5,7,8},{6}\n{1,2,3,4,5,7},{6},{8}\n{1,2,3,4,5,7},{6,8}\n{1,2,3,4,7},{5,6,8}\n{1,2,3,4,7},{5,6},{8}\n{1,2,3,4,7,8},{5,6}\n{1,2,3,4,8},{5,6},{7}\n{1,2,3,4},{5,6},{7},{8}\n{1,2,3,4},{5,6},{7,8}\n{1,2,3,4},{5,6,8},{7}\n{1,2,3,4},{5,6,7,..." }, { "input": "9", "output": "21147\n{1,2,3,4,5,6,7,8,9}\n{1,2,3,4,5,6,7,8},{9}\n{1,2,3,4,5,6,7},{8,9}\n{1,2,3,4,5,6,7},{8},{9}\n{1,2,3,4,5,6,7,9},{8}\n{1,2,3,4,5,6,9},{7,8}\n{1,2,3,4,5,6},{7,8},{9}\n{1,2,3,4,5,6},{7,8,9}\n{1,2,3,4,5,6},{7,9},{8}\n{1,2,3,4,5,6},{7},{8,9}\n{1,2,3,4,5,6},{7},{8},{9}\n{1,2,3,4,5,6,9},{7},{8}\n{1,2,3,4,5,6,8,9},{7}\n{1,2,3,4,5,6,8},{7},{9}\n{1,2,3,4,5,6,8},{7,9}\n{1,2,3,4,5,8},{6,7,9}\n{1,2,3,4,5,8},{6,7},{9}\n{1,2,3,4,5,8,9},{6,7}\n{1,2,3,4,5,9},{6,7},{8}\n{1,2,3,4,5},{6,7},{8},{9}\n{1,2,3,4,5},{6,7},{8,9..." }, { "input": "10", "output": "115975\n{1,2,3,4,5,6,7,8,9,10}\n{1,2,3,4,5,6,7,8,9},{10}\n{1,2,3,4,5,6,7,8},{9,10}\n{1,2,3,4,5,6,7,8},{9},{10}\n{1,2,3,4,5,6,7,8,10},{9}\n{1,2,3,4,5,6,7,10},{8,9}\n{1,2,3,4,5,6,7},{8,9},{10}\n{1,2,3,4,5,6,7},{8,9,10}\n{1,2,3,4,5,6,7},{8,10},{9}\n{1,2,3,4,5,6,7},{8},{9,10}\n{1,2,3,4,5,6,7},{8},{9},{10}\n{1,2,3,4,5,6,7,10},{8},{9}\n{1,2,3,4,5,6,7,9,10},{8}\n{1,2,3,4,5,6,7,9},{8},{10}\n{1,2,3,4,5,6,7,9},{8,10}\n{1,2,3,4,5,6,9},{7,8,10}\n{1,2,3,4,5,6,9},{7,8},{10}\n{1,2,3,4,5,6,9,10},{7,8}\n{1,2,3,4,5,6,10},{7..." } ]
92
0
0
18,668
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 1 0 1 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 1 0\n1 1 0 0 0 1 1 1 0 1 1 0 0 0 0 1 1 0 0 1 0 0 1 1 1 0 1 0 0 0 0 1 1 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 0 0 1 1 0 0 0\n1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 0 0 0 1 1 1 1 0 1 1 1 0 0 1 1 0 1", "output": "80 0" }, { "input": "1 1 1\n1", "output": "1 0" }, { "input": "1 1 1\n0", "output": "0 0" }, { "input": "2 2 1\n0 1\n1 0", "output": "2 0" }, { "input": "100 1 20\n0\n0\n0\n1\n1\n0\n0\n0\n1\n1\n0\n1\n0\n1\n1\n0\n1\n1\n0\n1\n0\n1\n1\n0\n1\n0\n1\n0\n0\n0\n0\n0\n1\n0\n0\n0\n0\n1\n1\n0\n1\n0\n1\n1\n1\n0\n0\n0\n0\n1\n1\n1\n0\n0\n0\n0\n0\n1\n0\n0\n1\n1\n1\n1\n1\n0\n0\n1\n0\n1\n0\n1\n0\n1\n0\n0\n0\n1\n1\n1\n1\n1\n1\n0\n0\n1\n1\n0\n1\n0\n0\n0\n0\n1\n1\n1\n1\n1\n0\n1", "output": "13 34" }, { "input": "1 100 1\n0 0 1 1 1 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 0 0 1 0 0 1 1 1", "output": "53 0" } ]
62
5,632,000
0
18,672
954
Fight Against Traffic
[ "dfs and similar", "graphs", "shortest paths" ]
null
null
Little town Nsk consists of *n* junctions connected by *m* bidirectional roads. Each road connects two distinct junctions and no two roads connect the same pair of junctions. It is possible to get from any junction to any other junction by these roads. The distance between two junctions is equal to the minimum possible number of roads on a path between them. In order to improve the transportation system, the city council asks mayor to build one new road. The problem is that the mayor has just bought a wonderful new car and he really enjoys a ride from his home, located near junction *s* to work located near junction *t*. Thus, he wants to build a new road in such a way that the distance between these two junctions won't decrease. You are assigned a task to compute the number of pairs of junctions that are not connected by the road, such that if the new road between these two junctions is built the distance between *s* and *t* won't decrease.
The firt line of the input contains integers *n*, *m*, *s* and *t* (2<=≤<=*n*<=≤<=1000, 1<=≤<=*m*<=≤<=1000, 1<=≤<=*s*,<=*t*<=≤<=*n*, *s*<=≠<=*t*) — the number of junctions and the number of roads in Nsk, as well as the indices of junctions where mayors home and work are located respectively. The *i*-th of the following *m* lines contains two integers *u**i* and *v**i* (1<=≤<=*u**i*,<=*v**i*<=≤<=*n*, *u**i*<=≠<=*v**i*), meaning that this road connects junctions *u**i* and *v**i* directly. It is guaranteed that there is a path between any two junctions and no two roads connect the same pair of junctions.
Print one integer — the number of pairs of junctions not connected by a direct road, such that building a road between these two junctions won't decrease the distance between junctions *s* and *t*.
[ "5 4 1 5\n1 2\n2 3\n3 4\n4 5\n", "5 4 3 5\n1 2\n2 3\n3 4\n4 5\n", "5 6 1 5\n1 2\n1 3\n1 4\n4 5\n3 5\n2 5\n" ]
[ "0\n", "5\n", "3\n" ]
none
[ { "input": "5 4 1 5\n1 2\n2 3\n3 4\n4 5", "output": "0" }, { "input": "5 4 3 5\n1 2\n2 3\n3 4\n4 5", "output": "5" }, { "input": "5 6 1 5\n1 2\n1 3\n1 4\n4 5\n3 5\n2 5", "output": "3" }, { "input": "2 1 2 1\n1 2", "output": "0" }, { "input": "3 2 2 3\n1 2\n2 3", "output": "1" }, { "input": "3 2 1 3\n1 2\n2 3", "output": "0" }, { "input": "3 3 2 3\n1 2\n2 3\n1 3", "output": "0" } ]
139
24,268,800
-1
18,693
735
Urbanization
[ "greedy", "number theory", "sortings" ]
null
null
Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided to ask his help in the question of urbanization. There are *n* people who plan to move to the cities. The wealth of the *i* of them is equal to *a**i*. Authorities plan to build two cities, first for *n*1 people and second for *n*2 people. Of course, each of *n* candidates can settle in only one of the cities. Thus, first some subset of candidates of size *n*1 settle in the first city and then some subset of size *n*2 is chosen among the remaining candidates and the move to the second city. All other candidates receive an official refuse and go back home. To make the statistic of local region look better in the eyes of their bosses, local authorities decided to pick subsets of candidates in such a way that the sum of arithmetic mean of wealth of people in each of the cities is as large as possible. Arithmetic mean of wealth in one city is the sum of wealth *a**i* among all its residents divided by the number of them (*n*1 or *n*2 depending on the city). The division should be done in real numbers without any rounding. Please, help authorities find the optimal way to pick residents for two cities.
The first line of the input contains three integers *n*, *n*1 and *n*2 (1<=≤<=*n*,<=*n*1,<=*n*2<=≤<=100<=000, *n*1<=+<=*n*2<=≤<=*n*) — the number of candidates who want to move to the cities, the planned number of residents of the first city and the planned number of residents of the second city. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=100<=000), the *i*-th of them is equal to the wealth of the *i*-th candidate.
Print one real value — the maximum possible sum of arithmetic means of wealth of cities' residents. You answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if .
[ "2 1 1\n1 5\n", "4 2 1\n1 4 2 3\n" ]
[ "6.00000000\n", "6.50000000\n" ]
In the first sample, one of the optimal solutions is to move candidate 1 to the first city and candidate 2 to the second. In the second sample, the optimal solution is to pick candidates 3 and 4 for the first city, and candidate 2 for the second one. Thus we obtain (*a*<sub class="lower-index">3</sub> + *a*<sub class="lower-index">4</sub>) / 2 + *a*<sub class="lower-index">2</sub> = (3 + 2) / 2 + 4 = 6.5
[ { "input": "2 1 1\n1 5", "output": "6.00000000" }, { "input": "4 2 1\n1 4 2 3", "output": "6.50000000" }, { "input": "3 1 2\n1 2 3", "output": "4.50000000" }, { "input": "10 4 6\n3 5 7 9 12 25 67 69 83 96", "output": "88.91666667" }, { "input": "19 7 12\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 100000 100000", "output": "47052.10714286" }, { "input": "100 9 6\n109 711 40 95 935 48 228 253 308 726 816 534 252 8 966 363 162 508 84 83 807 506 748 178 45 30 106 108 764 698 825 198 336 353 158 790 64 262 403 334 577 571 742 541 946 602 279 621 910 776 421 886 29 133 114 394 762 965 339 263 750 530 49 80 124 31 322 292 27 590 960 278 111 932 849 491 561 744 469 511 106 271 156 160 836 363 149 473 457 543 976 809 490 29 85 626 265 88 995 946", "output": "1849.66666667" }, { "input": "69 6 63\n53475 22876 79144 6335 33763 79104 65441 45527 65847 94406 74670 43529 75330 19403 67629 56187 57949 23071 64910 54409 55348 18056 855 24961 50565 6622 26467 33989 22660 79469 41246 13965 79706 14422 16075 93378 81313 48173 13470 97348 2346 27452 59427 29925 29847 73823 32021 10988 24609 98855 90919 45939 17203 8439 43007 40138 55693 30314 71734 33458 66850 4011 20089 20546 92090 50842 78859 62756 40177", "output": "135712.88888889" }, { "input": "69 6 9\n2612 17461 69001 33130 10662 85485 88195 45974 16712 81365 67119 87797 15559 20197 74716 92979 97268 49466 68603 48351 99905 35606 54242 98603 68232 54398 82637 49647 38979 46171 54680 23334 15892 92186 69670 29711 67999 2220 32317 717 70667 68262 86760 55720 97158 61122 7251 138 21022 27197 12691 59331 13576 66999 38332 13574 83484 66646 17704 33065 98583 80259 64631 16745 69431 40747 82089 82788 32739", "output": "183129.44444444" } ]
155
10,649,600
3
18,705
67
Optical Experiment
[ "binary search", "data structures", "dp" ]
D. Optical Experiment
5
256
Professor Phunsuk Wangdu has performed some experiments on rays. The setup for *n* rays is as follows. There is a rectangular box having exactly *n* holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?". Professor Wangdu now is in trouble and knowing your intellect he asks you to help him.
The first line contains *n* (1<=≤<=*n*<=≤<=106), the number of rays. The second line contains *n* distinct integers. The *i*-th integer *x**i* (1<=≤<=*x**i*<=≤<=*n*) shows that the *x**i*-th ray enters from the *i*-th hole. Similarly, third line contains *n* distinct integers. The *i*-th integer *y**i* (1<=≤<=*y**i*<=≤<=*n*) shows that the *y**i*-th ray exits from the *i*-th hole. All rays are numbered from 1 to *n*.
Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other.
[ "5\n1 4 5 2 3\n3 4 2 1 5\n", "3\n3 1 2\n2 3 1\n" ]
[ "3\n", "2\n" ]
For the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint.
[ { "input": "5\n1 4 5 2 3\n3 4 2 1 5", "output": "3" }, { "input": "3\n3 1 2\n2 3 1", "output": "2" }, { "input": "5\n1 2 4 5 3\n1 5 4 2 3", "output": "3" }, { "input": "3\n3 1 2\n1 3 2", "output": "2" }, { "input": "7\n1 5 2 7 4 3 6\n6 3 1 2 5 4 7", "output": "4" }, { "input": "4\n1 4 2 3\n2 3 1 4", "output": "2" }, { "input": "4\n2 4 1 3\n2 3 1 4", "output": "3" }, { "input": "10\n4 7 8 1 2 3 5 9 6 10\n6 3 8 7 10 2 1 4 5 9", "output": "5" }, { "input": "7\n1 5 7 2 4 3 6\n3 2 5 7 6 1 4", "output": "4" }, { "input": "9\n1 7 4 9 3 8 2 5 6\n8 4 7 1 3 2 9 6 5", "output": "4" }, { "input": "5\n1 4 5 2 3\n3 4 2 1 5", "output": "3" }, { "input": "3\n1 2 3\n2 3 1", "output": "2" }, { "input": "2\n1 2\n1 2", "output": "1" }, { "input": "2\n1 2\n2 1", "output": "2" }, { "input": "3\n1 2 3\n2 1 3", "output": "2" }, { "input": "3\n1 2 3\n1 3 2", "output": "2" }, { "input": "3\n1 2 3\n3 2 1", "output": "3" }, { "input": "3\n1 2 3\n1 2 3", "output": "1" }, { "input": "1\n1\n1", "output": "1" }, { "input": "5\n1 2 5 3 4\n3 5 4 2 1", "output": "4" }, { "input": "5\n5 3 2 4 1\n2 4 5 1 3", "output": "2" }, { "input": "5\n1 2 4 5 3\n1 2 5 4 3", "output": "2" }, { "input": "5\n1 2 3 4 5\n1 2 3 4 5", "output": "1" }, { "input": "5\n5 4 3 2 1\n1 2 3 4 5", "output": "5" }, { "input": "5\n1 3 5 4 2\n1 4 5 3 2", "output": "3" }, { "input": "5\n1 5 2 4 3\n4 3 2 5 1", "output": "4" }, { "input": "25\n21 19 25 9 24 23 20 18 16 22 17 7 4 15 13 11 2 3 10 12 14 6 8 5 1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25", "output": "13" }, { "input": "30\n30 29 28 27 26 25 19 24 9 23 21 20 18 16 22 17 7 4 15 13 11 2 3 10 12 14 6 8 5 1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30", "output": "19" }, { "input": "40\n40 27 29 39 30 34 28 26 25 38 19 32 24 9 37 23 21 20 18 33 36 16 22 35 17 7 4 15 31 13 11 2 3 10 12 14 6 8 5 1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40", "output": "19" }, { "input": "45\n45 44 40 43 27 29 41 39 30 34 28 26 25 42 38 19 32 24 9 37 23 21 20 18 33 36 16 22 35 17 7 4 15 31 13 11 2 3 10 12 14 6 8 5 1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45", "output": "22" }, { "input": "1\n1\n1", "output": "1" } ]
1,558
131,993,600
3.598343
18,706
95
Lucky Country
[ "dp", "dsu", "graphs" ]
E. Lucky Country
1
256
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One night Petya was sleeping. He was dreaming of being the president of some island country. The country is represented by islands connected by two-way roads. Between some islands there is no road way, even through other islands, that's why the country is divided into several regions. More formally, each island belongs to exactly one region, there is a path between any two islands located in the same region; there is no path between any two islands from different regions. A region is lucky if the amount of islands in it is a lucky number. As a real president, Petya first decided to build a presidential palace. Being a lucky numbers' fan, Petya wants to position his palace in one of the lucky regions. However, it is possible that initially the country has no such regions. In this case Petya can build additional roads between different regions, thus joining them. Find the minimum number of roads needed to build to create a lucky region.
The first line contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=105). They are the number of islands and the number of roads correspondingly. Next *m* lines contain road descriptions. Each road is defined by the numbers of islands that it connects: that is, by two integers *u* and *v* (1<=≤<=*u*,<=*v*<=≤<=*n*). Some roads can connect an island with itself; there can be more than one road between a pair of islands. Numbers in each line are separated by exactly one space character.
If there's no solution, output the only number "-1" (without the quotes). Otherwise, output the minimum number of roads *r* that need to be built to get a lucky region.
[ "4 3\n1 2\n2 3\n1 3\n", "5 4\n1 2\n3 4\n4 5\n3 5\n" ]
[ "1\n", "-1\n" ]
none
[ { "input": "4 3\n1 2\n2 3\n1 3", "output": "1" }, { "input": "5 4\n1 2\n3 4\n4 5\n3 5", "output": "-1" }, { "input": "7 6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7", "output": "0" }, { "input": "7 5\n2 3\n3 4\n4 5\n3 6\n2 2", "output": "2" }, { "input": "1 1\n1 1", "output": "-1" }, { "input": "2 3\n1 2\n2 1\n1 2", "output": "-1" }, { "input": "10 1\n1 10", "output": "2" }, { "input": "4 6\n1 2\n1 3\n1 4\n3 4\n2 4\n2 3", "output": "0" }, { "input": "16 14\n2 3\n2 4\n4 3\n5 6\n7 6\n8 9\n12 13\n12 14\n15 14\n13 15\n13 14\n12 15\n16 13\n15 16", "output": "1" }, { "input": "15 14\n2 3\n2 4\n3 4\n5 6\n6 7\n8 9\n8 10\n8 11\n12 13\n12 14\n14 15\n13 15\n12 15\n13 14", "output": "0" }, { "input": "4 1\n3 3", "output": "3" }, { "input": "7 1\n5 5", "output": "3" }, { "input": "47 10\n1 1\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10", "output": "3" }, { "input": "47 45\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n27 28\n28 29\n29 30\n31 32\n32 33\n33 34\n34 35\n35 36\n36 37\n37 38\n38 39\n39 40\n40 41\n41 42\n42 43\n43 44\n44 45\n45 46\n46 47", "output": "1" }, { "input": "20 10\n13 7\n19 1\n5 10\n3 2\n6 19\n3 12\n12 10\n4 3\n5 7\n11 9", "output": "1" }, { "input": "20 50\n1 13\n1 13\n11 4\n15 8\n8 4\n4 1\n19 12\n15 16\n9 20\n2 16\n9 11\n17 6\n2 11\n9 6\n3 7\n4 14\n14 6\n9 4\n4 16\n11 7\n14 7\n15 2\n12 12\n15 13\n6 8\n5 9\n20 19\n20 14\n12 8\n11 11\n12 10\n20 19\n5 18\n5 19\n10 13\n17 13\n2 15\n17 12\n9 7\n7 6\n19 18\n13 3\n11 19\n3 7\n15 11\n15 15\n5 10\n13 8\n17 14\n2 2", "output": "-1" }, { "input": "20 1\n5 15", "output": "2" }, { "input": "20 5\n10 19\n11 20\n14 3\n16 15\n19 1", "output": "1" }, { "input": "20 30\n17 4\n10 17\n12 3\n9 11\n17 2\n13 12\n6 11\n14 19\n11 14\n6 19\n18 3\n15 1\n10 18\n3 20\n16 15\n9 6\n13 12\n17 5\n5 13\n20 18\n8 19\n13 1\n2 5\n12 15\n5 19\n6 13\n15 16\n19 19\n19 15\n16 6", "output": "-1" }, { "input": "20 15\n10 9\n4 14\n16 13\n13 5\n9 10\n20 13\n13 11\n16 14\n14 9\n14 20\n1 19\n8 20\n7 12\n3 20\n7 17", "output": "1" }, { "input": "30 20\n29 21\n22 2\n8 18\n16 15\n9 12\n28 12\n24 1\n15 23\n1 14\n18 3\n14 10\n5 8\n19 4\n13 29\n15 29\n30 24\n28 25\n17 3\n8 26\n27 20", "output": "0" }, { "input": "30 4\n4 28\n7 18\n16 17\n16 11", "output": "1" }, { "input": "47 20\n41 32\n5 36\n4 14\n15 45\n10 36\n24 7\n12 25\n12 43\n31 19\n28 11\n41 43\n9 39\n33 20\n33 28\n28 1\n39 19\n8 20\n36 42\n22 45\n29 7", "output": "0" }, { "input": "50 50\n17 30\n29 29\n50 42\n15 18\n34 10\n30 3\n44 11\n4 35\n42 8\n14 41\n30 4\n11 1\n3 23\n7 28\n35 6\n24 37\n6 12\n8 7\n36 40\n41 26\n13 46\n15 40\n32 34\n15 28\n46 31\n36 25\n38 43\n16 19\n35 9\n6 21\n16 42\n38 43\n32 12\n12 32\n43 21\n16 35\n3 11\n24 13\n36 24\n43 22\n29 36\n18 15\n29 47\n30 31\n4 7\n18 42\n9 48\n17 32\n42 9\n2 6", "output": "1" }, { "input": "64 18\n35 37\n30 35\n56 39\n18 46\n26 35\n39 48\n50 27\n5 12\n28 54\n18 51\n31 60\n1 63\n5 51\n20 5\n23 53\n64 11\n17 50\n14 27", "output": "0" }, { "input": "100 40\n13 92\n21 56\n53 71\n55 8\n35 39\n45 54\n84 14\n16 15\n15 7\n75 31\n76 5\n32 70\n31 71\n77 67\n38 10\n27 48\n24 28\n33 19\n61 70\n26 74\n11 85\n20 15\n67 17\n37 38\n20 66\n8 31\n62 8\n2 7\n71 90\n91 67\n55 35\n7 57\n10 22\n85 64\n18 61\n75 23\n64 50\n30 99\n27 45\n49 42", "output": "0" }, { "input": "100 3\n54 47\n85 25\n59 47", "output": "1" }, { "input": "1000 1\n908 655", "output": "2" }, { "input": "1000 10\n117 228\n530 348\n119 116\n239 658\n102 791\n736 786\n617 192\n396 109\n286 509\n754 207", "output": "1" }, { "input": "5000 24\n4725 3848\n4135 4259\n2109 3306\n213 3847\n1627 3379\n4290 2314\n9 774\n3174 4435\n1778 3821\n1128 1465\n3011 74\n501 2861\n2935 1995\n989 4268\n3944 1154\n3172 1888\n3817 2992\n398 4722\n4873 4958\n1196 4398\n3441 1536\n4259 3474\n4559 1598\n2163 2612", "output": "1" }, { "input": "100000 1\n16796 47477", "output": "2" }, { "input": "20 19\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20", "output": "-1" }, { "input": "7 6\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7", "output": "0" }, { "input": "20 17\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19", "output": "-1" }, { "input": "47 42\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 12\n12 13\n13 14\n14 15\n15 16\n16 17\n17 18\n18 19\n19 20\n20 21\n21 22\n22 23\n23 24\n24 25\n25 26\n26 27\n28 29\n29 30\n30 31\n31 32\n32 33\n33 34\n34 35\n35 36\n37 38\n38 39\n39 40\n40 41\n42 43\n43 44\n44 45\n45 46", "output": "4" }, { "input": "7 5\n1 2\n2 3\n3 4\n4 5\n6 7", "output": "1" }, { "input": "4 2\n2 3\n3 4", "output": "1" } ]
62
614,400
0
18,715
189
Counting Rhombi
[ "brute force", "math" ]
null
null
You have two positive integers *w* and *h*. Your task is to count the number of rhombi which have the following properties: - Have positive area. - With vertices at integer points. - All vertices of the rhombi are located inside or on the border of the rectangle with vertices at points (0,<=0), (*w*,<=0), (*w*,<=*h*), (0,<=*h*). In other words, for all vertices (*x**i*,<=*y**i*) of the rhombus the following conditions should fulfill: 0<=≤<=*x**i*<=≤<=*w* and 0<=≤<=*y**i*<=≤<=*h*. - Its diagonals are parallel to the axis. Count the number of such rhombi. Let us remind you that a rhombus is a quadrilateral whose four sides all have the same length.
The first line contains two integers *w* and *h* (1<=≤<=*w*,<=*h*<=≤<=4000) — the rectangle's sizes.
Print a single number — the number of sought rhombi. Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
[ "2 2\n", "1 2\n" ]
[ "1\n", "0\n" ]
In the first example there exists only one such rhombus. Its vertices are located at points (1, 0), (2, 1), (1, 2), (0, 1).
[ { "input": "2 2", "output": "1" }, { "input": "1 2", "output": "0" }, { "input": "1 4000", "output": "0" }, { "input": "4000 1", "output": "0" }, { "input": "4000 4000", "output": "16000000000000" }, { "input": "15 10", "output": "1400" }, { "input": "7 9", "output": "240" }, { "input": "17 17", "output": "5184" }, { "input": "7 13", "output": "504" }, { "input": "9 14", "output": "980" }, { "input": "3 10", "output": "50" }, { "input": "14 2", "output": "49" }, { "input": "4 6", "output": "36" }, { "input": "18 2858", "output": "165405321" }, { "input": "14 1274", "output": "19882681" }, { "input": "25 2986", "output": "347731644" }, { "input": "13 1402", "output": "20638842" }, { "input": "2955 21", "output": "240130660" }, { "input": "1665 27", "output": "126136192" }, { "input": "3671 19", "output": "303215400" }, { "input": "2541 25", "output": "251810520" }, { "input": "1913 3980", "output": "3623063809200" }, { "input": "3727 2044", "output": "3627108561888" }, { "input": "2437 460", "output": "78542851800" }, { "input": "1499 2172", "output": "662525703000" }, { "input": "1 1", "output": "0" } ]
30
0
-1
18,803
109
Lucky Interval
[ "brute force", "math" ]
E. Lucky Interval
4
512
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya came across an interval of numbers [*a*,<=*a*<=+<=*l*<=-<=1]. Let *F*(*x*) be the number of lucky digits of number *x*. Find the minimum *b* (*a*<=&lt;<=*b*) such, that *F*(*a*) = *F*(*b*), *F*(*a*<=+<=1) = *F*(*b*<=+<=1), ..., *F*(*a*<=+<=*l*<=-<=1) = *F*(*b*<=+<=*l*<=-<=1).
The single line contains two integers *a* and *l* (1<=≤<=*a*,<=*l*<=≤<=109) — the interval's first number and the interval's length correspondingly.
On the single line print number *b* — the answer to the problem.
[ "7 4\n", "4 7\n" ]
[ "17\n", "14\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%;"/>
[ { "input": "7 4", "output": "17" }, { "input": "4 7", "output": "14" }, { "input": "10 10", "output": "20" }, { "input": "47 74", "output": "147" }, { "input": "469 1", "output": "480" }, { "input": "47 74", "output": "147" }, { "input": "100 1", "output": "101" }, { "input": "104 1", "output": "107" }, { "input": "477 7", "output": "747" }, { "input": "777 10", "output": "1447" }, { "input": "100 100", "output": "200" }, { "input": "1 1000", "output": "1001" }, { "input": "47 9454", "output": "10047" }, { "input": "458 10000", "output": "10458" }, { "input": "9548 748", "output": "10548" }, { "input": "10000 10000", "output": "20000" }, { "input": "46999 100000", "output": "146999" }, { "input": "548454 845445", "output": "1548454" }, { "input": "459544 48587874", "output": "100459544" }, { "input": "1000000000 1000000000", "output": "2000000000" }, { "input": "779999 1000000000", "output": "1000779999" }, { "input": "4699999 1000000000", "output": "1004699999" }, { "input": "9999 107545", "output": "109999" }, { "input": "954 71", "output": "1054" }, { "input": "974999 4045", "output": "1044999" }, { "input": "96555 40104", "output": "186555" }, { "input": "4699999 1", "output": "4800000" }, { "input": "777777777 1", "output": "1444444444" }, { "input": "47447470 48545", "output": "47747470" }, { "input": "474474 7475", "output": "744474" }, { "input": "1 1000000000", "output": "1000000001" }, { "input": "1 74548744", "output": "100000001" }, { "input": "22 484", "output": "1022" }, { "input": "9654 2540", "output": "10654" }, { "input": "37477 10000", "output": "67477" }, { "input": "6777 1000", "output": "13777" }, { "input": "6777 10000", "output": "16777" }, { "input": "6777 100000", "output": "106777" }, { "input": "5456954 448559900", "output": "1005456954" }, { "input": "658458950 1000000000", "output": "1658458950" }, { "input": "258963174 777777774", "output": "1258963174" }, { "input": "8748544 48248414", "output": "108748544" }, { "input": "154215478 999547859", "output": "1154215478" }, { "input": "48769999 1", "output": "48780000" }, { "input": "48769999 100", "output": "48780399" }, { "input": "48769999 1000000000", "output": "1048769999" }, { "input": "45749895 18445", "output": "45779895" }, { "input": "45749895 8445", "output": "45779895" }, { "input": "47699984 54740", "output": "74399984" }, { "input": "76954099 1000000", "output": "143954099" }, { "input": "95458154 6845740", "output": "105458154" }, { "input": "48544954 199", "output": "48545454" }, { "input": "3945845 174", "output": "3945945" }, { "input": "499999977 395", "output": "500000777" }, { "input": "499999977 99", "output": "500000477" }, { "input": "7499999 195", "output": "7500499" }, { "input": "399999999 1", "output": "500000000" }, { "input": "399999999 145", "output": "500003999" }, { "input": "39999999 1450", "output": "50039999" }, { "input": "399999999 1000000000", "output": "1399999999" }, { "input": "45999999 1000000000", "output": "1045999999" }, { "input": "45999999 1000", "output": "46000999" }, { "input": "45999999 200", "output": "46000099" }, { "input": "46999999 185", "output": "48003999" }, { "input": "73999 1", "output": "75000" }, { "input": "73999999 78", "output": "75000399" }, { "input": "739999999 69", "output": "750000399" }, { "input": "73990000 10000", "output": "75000000" }, { "input": "965411551 55545859", "output": "1065411551" }, { "input": "474799999 1", "output": "474800004" }, { "input": "474799999 1024", "output": "474804999" }, { "input": "474799099 1000000000", "output": "1474799099" }, { "input": "85474999 1000000000", "output": "1085474999" }, { "input": "854799 1000000000", "output": "1000854799" }, { "input": "95779999 1000000000", "output": "1095779999" }, { "input": "774447447 1", "output": "774447474" }, { "input": "474447474 1000", "output": "474474474" }, { "input": "474447474 10000", "output": "474477474" }, { "input": "474447474 747774", "output": "477447474" }, { "input": "777777777 100000000", "output": "1477777777" }, { "input": "46000 10000", "output": "76000" }, { "input": "47000 1000", "output": "74000" }, { "input": "1 8474585", "output": "10000001" }, { "input": "4 1000000000", "output": "1000000004" }, { "input": "7474447 1000000000", "output": "1007474447" }, { "input": "47497 1", "output": "47544" }, { "input": "47497 457", "output": "74497" }, { "input": "47497 900", "output": "74497" }, { "input": "1 600", "output": "1001" }, { "input": "7 1000000000", "output": "1000000007" }, { "input": "9 1000000000", "output": "1000000009" }, { "input": "14 1000000000", "output": "1000000014" }, { "input": "471 1000000000", "output": "1000000471" }, { "input": "47 2", "output": "74" }, { "input": "4769999 2", "output": "4780003" }, { "input": "95 3", "output": "102" }, { "input": "97 1", "output": "104" }, { "input": "397 1", "output": "400" }, { "input": "540395 10", "output": "540535" }, { "input": "699997 1", "output": "700000" }, { "input": "699999 1", "output": "800000" }, { "input": "7399999 1", "output": "7500000" }, { "input": "999999999 999999999", "output": "1999999999" }, { "input": "75 1000000000", "output": "1000000075" }, { "input": "999999999 1", "output": "1000000000" }, { "input": "1000000000 47", "output": "1000000100" }, { "input": "500000000 500000000", "output": "1200000000" }, { "input": "85 98", "output": "185" }, { "input": "17 17", "output": "87" }, { "input": "96 39", "output": "186" }, { "input": "45 18", "output": "75" }, { "input": "297 6", "output": "307" }, { "input": "144 197", "output": "844" }, { "input": "370 10", "output": "400" }, { "input": "469999370 10", "output": "469999400" }, { "input": "3997 3", "output": "4999" }, { "input": "39970 20", "output": "49990" }, { "input": "399970 24", "output": "499990" }, { "input": "399970 12", "output": "499990" }, { "input": "39977 2", "output": "40004" }, { "input": "46996 2", "output": "46999" }, { "input": "39970 4", "output": "40000" }, { "input": "3699949 60", "output": "3800349" }, { "input": "39999997 4", "output": "50000004" }, { "input": "369999995 5", "output": "380000002" }, { "input": "699999999 2", "output": "800000003" }, { "input": "777777776 2", "output": "1444444443" }, { "input": "777777777 150", "output": "1444444477" }, { "input": "699999999 18", "output": "800000399" }, { "input": "777777776 18", "output": "1444444446" }, { "input": "777777777 3", "output": "1444444444" }, { "input": "777777777 18", "output": "1444444447" }, { "input": "444444444 1000000000", "output": "1444444444" }, { "input": "444444444 10000", "output": "444474444" }, { "input": "444444443 2", "output": "444444446" }, { "input": "777777777 7", "output": "1444444447" }, { "input": "777777777 20", "output": "1444444447" }, { "input": "399999999 18", "output": "500000399" }, { "input": "699999999 18", "output": "800000399" }, { "input": "699999999 5", "output": "800000039" }, { "input": "777777771 18", "output": "1444444441" }, { "input": "777777771 19", "output": "1444444441" }, { "input": "777777770 19", "output": "1444444440" }, { "input": "399999997 4", "output": "500000004" }, { "input": "699999997 4", "output": "800000004" }, { "input": "399999997 7", "output": "500000037" }, { "input": "399999997 2", "output": "499999999" }, { "input": "399999997 18", "output": "500000397" }, { "input": "399999997 25", "output": "500000397" }, { "input": "399999997 15", "output": "500000397" }, { "input": "747444777 1000000000", "output": "1747444777" }, { "input": "747477 1000000000", "output": "1000747477" }, { "input": "747477777 1000000000", "output": "1747477777" }, { "input": "777777777 100000000", "output": "1477777777" }, { "input": "744444430 10", "output": "744444450" }, { "input": "399999999 20", "output": "500000399" }, { "input": "700000000 1000000000", "output": "1700000000" }, { "input": "1000000000 1", "output": "1000000001" }, { "input": "1 1", "output": "2" }, { "input": "1 1000000000", "output": "1000000001" }, { "input": "476990 190", "output": "743990" }, { "input": "7776920 190", "output": "14443920" }, { "input": "39997840 190", "output": "49999840" }, { "input": "39997820 190", "output": "49999820" }, { "input": "39997840 19", "output": "39997870" }, { "input": "39997840 198", "output": "49999840" } ]
62
0
3.99225
18,804
765
Artsem and Saunders
[ "constructive algorithms", "dsu", "math" ]
null
null
Artsem has a friend Saunders from University of Chicago. Saunders presented him with the following problem. Let [*n*] denote the set {1,<=...,<=*n*}. We will also write *f*:<=[*x*]<=→<=[*y*] when a function *f* is defined in integer points 1, ..., *x*, and all its values are integers from 1 to *y*. Now then, you are given a function *f*:<=[*n*]<=→<=[*n*]. Your task is to find a positive integer *m*, and two functions *g*:<=[*n*]<=→<=[*m*], *h*:<=[*m*]<=→<=[*n*], such that *g*(*h*(*x*))<==<=*x* for all , and *h*(*g*(*x*))<==<=*f*(*x*) for all , or determine that finding these is impossible.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=105). The second line contains *n* space-separated integers — values *f*(1),<=...,<=*f*(*n*) (1<=≤<=*f*(*i*)<=≤<=*n*).
If there is no answer, print one integer -1. Otherwise, on the first line print the number *m* (1<=≤<=*m*<=≤<=106). On the second line print *n* numbers *g*(1),<=...,<=*g*(*n*). On the third line print *m* numbers *h*(1),<=...,<=*h*(*m*). If there are several correct answers, you may output any of them. It is guaranteed that if a valid answer exists, then there is an answer satisfying the above restrictions.
[ "3\n1 2 3\n", "3\n2 2 2\n", "2\n2 1\n" ]
[ "3\n1 2 3\n1 2 3\n", "1\n1 1 1\n2\n", "-1\n" ]
none
[ { "input": "3\n1 2 3", "output": "3\n1 2 3\n1 2 3" }, { "input": "3\n2 2 2", "output": "1\n1 1 1\n2" }, { "input": "2\n2 1", "output": "-1" }, { "input": "1\n1", "output": "1\n1\n1" }, { "input": "2\n2 1", "output": "-1" }, { "input": "2\n2 2", "output": "1\n1 1\n2" }, { "input": "5\n5 5 5 3 5", "output": "-1" }, { "input": "10\n4 4 4 4 4 4 4 4 4 4", "output": "1\n1 1 1 1 1 1 1 1 1 1\n4" }, { "input": "2\n1 2", "output": "2\n1 2\n1 2" }, { "input": "3\n3 2 3", "output": "2\n2 1 2\n2 3" }, { "input": "3\n1 2 1", "output": "2\n1 2 1\n1 2" }, { "input": "4\n4 2 4 4", "output": "2\n2 1 2 2\n2 4" }, { "input": "5\n1 4 5 4 5", "output": "3\n1 2 3 2 3\n1 4 5" }, { "input": "4\n1 2 1 2", "output": "2\n1 2 1 2\n1 2" }, { "input": "5\n1 3 3 4 4", "output": "3\n1 2 2 3 3\n1 3 4" }, { "input": "4\n4 2 2 4", "output": "2\n2 1 1 2\n2 4" }, { "input": "7\n7 3 3 5 5 7 7", "output": "3\n3 1 1 2 2 3 3\n3 5 7" }, { "input": "6\n1 1 1 3 3 3", "output": "-1" }, { "input": "4\n2 2 3 2", "output": "2\n1 1 2 1\n2 3" }, { "input": "6\n1 2 3 4 5 5", "output": "5\n1 2 3 4 5 5\n1 2 3 4 5" }, { "input": "3\n1 1 2", "output": "-1" }, { "input": "4\n3 4 3 4", "output": "2\n1 2 1 2\n3 4" }, { "input": "6\n1 1 1 4 4 4", "output": "2\n1 1 1 2 2 2\n1 4" }, { "input": "4\n1 2 1 1", "output": "2\n1 2 1 1\n1 2" }, { "input": "5\n1 2 3 4 3", "output": "4\n1 2 3 4 3\n1 2 3 4" }, { "input": "4\n2 2 4 4", "output": "2\n1 1 2 2\n2 4" }, { "input": "4\n1 1 3 3", "output": "2\n1 1 2 2\n1 3" }, { "input": "3\n2 2 3", "output": "2\n1 1 2\n2 3" }, { "input": "5\n5 3 3 3 5", "output": "2\n2 1 1 1 2\n3 5" } ]
436
9,216,000
3
18,856
342
Xenia and Spies
[ "brute force", "greedy", "implementation" ]
null
null
Xenia the vigorous detective faced *n* (*n*<=≥<=2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to *n* from left to right. Spy *s* has an important note. He has to pass the note to spy *f*. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is *x*, he can pass the note to another spy, either *x*<=-<=1 or *x*<=+<=1 (if *x*<==<=1 or *x*<==<=*n*, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During *m* steps Xenia watches some spies attentively. Specifically, during step *t**i* (steps are numbered from 1) Xenia watches spies numbers *l**i*,<=*l**i*<=+<=1,<=*l**i*<=+<=2,<=...,<=*r**i* (1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got *s* and *f*. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy *s* to spy *f* as quickly as possible (in the minimum number of steps).
The first line contains four integers *n*, *m*, *s* and *f* (1<=≤<=*n*,<=*m*<=≤<=105; 1<=≤<=*s*,<=*f*<=≤<=*n*; *s*<=≠<=*f*; *n*<=≥<=2). Each of the following *m* lines contains three integers *t**i*,<=*l**i*,<=*r**i* (1<=≤<=*t**i*<=≤<=109,<=1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n*). It is guaranteed that *t*1<=&lt;<=*t*2<=&lt;<=*t*3<=&lt;<=...<=&lt;<=*t**m*.
Print *k* characters in a line: the *i*-th character in the line must represent the spies' actions on step *i*. If on step *i* the spy with the note must pass the note to the spy with a lesser number, the *i*-th character should equal "L". If on step *i* the spy with the note must pass it to the spy with a larger number, the *i*-th character must equal "R". If the spy must keep the note at the *i*-th step, the *i*-th character must equal "X". As a result of applying the printed sequence of actions spy *s* must pass the note to spy *f*. The number of printed characters *k* must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
[ "3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3\n" ]
[ "XXRR\n" ]
none
[ { "input": "3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3", "output": "XXRR" }, { "input": "2 3 2 1\n1 1 2\n2 1 2\n4 1 2", "output": "XXL" }, { "input": "5 11 1 5\n1 1 5\n2 2 2\n3 1 1\n4 3 3\n5 3 3\n6 1 1\n7 4 4\n8 4 5\n10 1 3\n11 5 5\n13 1 5", "output": "XXXRXRXXRR" }, { "input": "4 6 4 2\n2 2 2\n3 3 3\n4 1 1\n10 1 4\n11 2 3\n12 2 4", "output": "LXXL" }, { "input": "7 5 7 6\n1 4 5\n2 7 7\n3 6 6\n4 3 4\n5 1 3", "output": "L" }, { "input": "4 4 3 4\n1 2 4\n2 1 2\n3 3 4\n4 2 3", "output": "XR" }, { "input": "10 10 1 10\n1 1 10\n2 1 1\n3 7 10\n4 6 7\n5 9 9\n6 4 9\n7 2 5\n8 3 10\n9 2 10\n10 7 9", "output": "XXRRRXXXXRRRRRR" }, { "input": "20 20 17 20\n1 16 20\n2 12 13\n3 14 16\n4 13 15\n5 3 15\n6 2 11\n7 18 18\n8 5 15\n9 6 12\n10 19 19\n11 9 11\n12 14 17\n13 19 19\n14 12 20\n15 1 1\n16 11 17\n17 13 14\n18 5 17\n19 2 10\n20 19 20", "output": "XRRR" }, { "input": "100000 1 11500 70856\n1 9881 75626", "output": "XRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR..." }, { "input": "100000 2 37212 89918\n1 24285 99164\n2 67042 82268", "output": "XRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR..." }, { "input": "100 5 99 1\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n10 1 3", "output": "LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL" }, { "input": "5 1 1 5\n1 1 1", "output": "XRRRR" }, { "input": "3 5 1 3\n1 1 2\n2 2 3\n3 3 3\n4 1 1\n1000000000 1 3", "output": "XXRR" }, { "input": "2 2 1 2\n1 1 2\n1000000000 1 2", "output": "XR" }, { "input": "10 1 1 10\n1 5 6", "output": "RRRRRRRRR" } ]
93
23,142,400
-1
18,871
343
Pumping Stations
[ "brute force", "dfs and similar", "divide and conquer", "flows", "graphs", "greedy", "trees" ]
null
null
Mad scientist Mike has applied for a job. His task is to manage a system of water pumping stations. The system consists of *n* pumping stations, which are numbered by integers from 1 to *n*. Some pairs of stations are connected by bidirectional pipes through which water can flow in either direction (but only in one at a time). For each pipe you know its bandwidth — the maximum number of liters of water that can flow through it in one hour. Each pumping station can pump incoming water from some stations to other stations through the pipes, provided that in one hour the total influx of water to the station is equal to the total outflux of water from the station. It is Mike's responsibility to pump water between stations. From station *a* to station *b* through the pipes (possibly through other stations) within one hour one can transmit a certain number of liters of water according to the rules described above. During this time, water from other stations can not flow into station *a*, and can not flow out of the station *b*. However, any amount of water can flow out of station *a* or in station *b*. If a total of *x* litres of water flows out of the station *a* in an hour, then Mike gets *x* bollars more to his salary. To get paid, Mike needs to work for *n*<=-<=1 days, according to the contract. On the first day he selects two stations *v*1 and *v*2, and within one hour he pumps a certain amount of water from *v*1 to *v*2. Next, on the *i*-th day Mike chooses a station *v**i*<=+<=1 that has been never selected before, and pumps a certain amount of water out of the station *v**i* to station *v**i*<=+<=1 for one hour. The quantity of water he pumps on the *i*-th day does not depend on the amount of water pumped on the (*i*<=-<=1)-th day. Mike needs to earn as much bollars as he can for his projects. Help Mike find such a permutation of station numbers *v*1, *v*2, ..., *v**n* so Mike will be able to earn the highest possible salary.
The first line of the input contains two space-separated integers *n* and *m* (2<=≤<=*n*<=≤<=200, 1<=≤<=*m*<=≤<=1000) — the number of stations and pipes in the system, accordingly. The *i*-th of the next *m* lines contains three space-separated integers *a**i*, *b**i* and *c**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=*n*, *a**i*<=≠<=*b**i*, 1<=≤<=*c**i*<=≤<=100) — the numbers of stations connected by the *i*-th pipe and the pipe's bandwidth, accordingly. It is guaranteed that any two stations are connected by at most one pipe and that there is a pipe path between any two stations.
On the first line print a single integer — the maximum salary Mike can earn. On the second line print a space-separated permutation of *n* numbers from 1 to *n* — the numbers of stations in the sequence *v*1, *v*2, ..., *v**n*. If there are multiple answers, print any of them.
[ "6 11\n1 2 10\n1 6 8\n2 3 4\n2 5 2\n2 6 3\n3 4 5\n3 5 4\n3 6 2\n4 5 7\n4 6 2\n5 6 3\n" ]
[ "77\n6 2 1 5 3 4 \n" ]
none
[]
60
307,200
0
18,885
659
New Reform
[ "data structures", "dfs and similar", "dsu", "graphs", "greedy" ]
null
null
Berland has *n* cities connected by *m* bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads. The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another). In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city. Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
The first line of the input contains two positive integers, *n* and *m* — the number of the cities and the number of roads in Berland (2<=≤<=*n*<=≤<=100<=000, 1<=≤<=*m*<=≤<=100<=000). Next *m* lines contain the descriptions of the roads: the *i*-th road is determined by two distinct integers *x**i*,<=*y**i* (1<=≤<=*x**i*,<=*y**i*<=≤<=*n*, *x**i*<=≠<=*y**i*), where *x**i* and *y**i* are the numbers of the cities connected by the *i*-th road. It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
Print a single integer — the minimum number of separated cities after the reform.
[ "4 3\n2 1\n1 3\n4 3\n", "5 5\n2 1\n1 3\n2 3\n2 5\n4 3\n", "6 5\n1 2\n2 3\n4 5\n4 6\n5 6\n" ]
[ "1\n", "0\n", "1\n" ]
In the first sample the following road orientation is allowed: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/04481aced8a9d501ae5d785ab654c542ff5497a1.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0d220a75a508edc0d540dbaec5e198345049b66f.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The second sample: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/030fc9181b578c2d906254d38dc56da5554323eb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/a6011a68d6de05246ad6ba8aa24a5c5c71cd450a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0d220a75a508edc0d540dbaec5e198345049b66f.png" style="max-width: 100.0%;max-height: 100.0%;"/>. The third sample: <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/e5b18c46402af724bd3841d549d5d6f52fc16253.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/7113d83514735a488d7b85262585381d26986195.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/98300067e46d036076e429b208db829296ebca9d.png" style="max-width: 100.0%;max-height: 100.0%;"/>, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/146f3f3d208399fbab4d1d8c2800f07bf7b463e5.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "4 3\n2 1\n1 3\n4 3", "output": "1" }, { "input": "5 5\n2 1\n1 3\n2 3\n2 5\n4 3", "output": "0" }, { "input": "6 5\n1 2\n2 3\n4 5\n4 6\n5 6", "output": "1" }, { "input": "4 4\n1 2\n2 3\n3 4\n4 1", "output": "0" }, { "input": "10 45\n3 5\n2 3\n4 8\n2 5\n6 8\n5 7\n2 1\n3 7\n5 10\n6 1\n9 4\n3 6\n9 10\n6 7\n1 7\n7 9\n6 9\n9 3\n4 2\n2 6\n5 6\n5 8\n3 4\n10 8\n7 8\n4 6\n9 1\n5 9\n7 4\n1 10\n9 2\n2 8\n6 10\n9 8\n1 5\n7 2\n10 3\n3 8\n4 10\n4 1\n10 7\n1 3\n1 8\n10 2\n4 5", "output": "0" }, { "input": "20 20\n16 3\n15 14\n6 14\n13 19\n7 13\n3 13\n3 2\n17 11\n14 20\n19 10\n4 13\n3 8\n18 4\n12 7\n6 3\n11 13\n17 19\n5 14\n9 2\n11 1", "output": "0" }, { "input": "2 1\n1 2", "output": "1" }, { "input": "5 5\n1 2\n2 3\n3 4\n4 5\n5 2", "output": "0" } ]
1,000
13,824,000
0
18,907
407
k-d-sequence
[ "data structures" ]
null
null
We'll call a sequence of integers a good *k*-*d* sequence if we can add to it at most *k* numbers in such a way that after the sorting the sequence will be an arithmetic progression with difference *d*. You got hold of some sequence *a*, consisting of *n* integers. Your task is to find its longest contiguous subsegment, such that it is a good *k*-*d* sequence.
The first line contains three space-separated integers *n*,<=*k*,<=*d* (1<=≤<=*n*<=≤<=2·105; 0<=≤<=*k*<=≤<=2·105; 0<=≤<=*d*<=≤<=109). The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=≤<=*a**i*<=≤<=109) — the actual sequence.
Print two space-separated integers *l*,<=*r* (1<=≤<=*l*<=≤<=*r*<=≤<=*n*) show that sequence *a**l*,<=*a**l*<=+<=1,<=...,<=*a**r* is the longest subsegment that is a good *k*-*d* sequence. If there are multiple optimal answers, print the one with the minimum value of *l*.
[ "6 1 2\n4 3 2 8 6 2\n" ]
[ "3 5\n" ]
In the first test sample the answer is the subsegment consisting of numbers 2, 8, 6 — after adding number 4 and sorting it becomes sequence 2, 4, 6, 8 — the arithmetic progression with difference 2.
[]
15
0
0
18,913
31
Schedule
[ "implementation" ]
C. Schedule
2
256
At the beginning of the new semester there is new schedule in the Berland State University. According to this schedule, *n* groups have lessons at the room 31. For each group the starting time of the lesson and the finishing time of the lesson are known. It has turned out that it is impossible to hold all lessons, because for some groups periods of their lessons intersect. If at some moment of time one groups finishes it's lesson, and the other group starts the lesson, their lessons don't intersect. The dean wants to cancel the lesson in one group so that no two time periods of lessons of the remaining groups intersect. You are to find all ways to do that.
The first line contains integer *n* (1<=≤<=*n*<=≤<=5000) — amount of groups, which have lessons in the room 31. Then *n* lines follow, each of them contains two integers *l**i* *r**i* (1<=≤<=*l**i*<=&lt;<=*r**i*<=≤<=106) — starting and finishing times of lesson of the *i*-th group. It is possible that initially no two lessons intersect (see sample 1).
Output integer *k* — amount of ways to cancel the lesson in exactly one group so that no two time periods of lessons of the remaining groups intersect. In the second line output *k* numbers — indexes of groups, where it is possible to cancel the lesson. Groups are numbered starting from 1 in the order that they were given in the input. Output the numbers in increasing order.
[ "3\n3 10\n20 30\n1 3\n", "4\n3 10\n20 30\n1 3\n1 39\n", "3\n1 5\n2 6\n3 7\n" ]
[ "3\n1 2 3 ", "1\n4 ", "0\n" ]
none
[ { "input": "3\n3 10\n20 30\n1 3", "output": "3\n1 2 3 " }, { "input": "4\n3 10\n20 30\n1 3\n1 39", "output": "1\n4 " }, { "input": "3\n1 5\n2 6\n3 7", "output": "0" }, { "input": "4\n1 5\n5 7\n6 9\n9 10", "output": "2\n2 3 " }, { "input": "11\n717170 795210\n866429 970764\n163324 322182\n677099 717170\n241684 393937\n50433 114594\n970764 997956\n393937 664883\n235698 241684\n795210 832346\n114594 232438", "output": "1\n3 " }, { "input": "16\n203671 381501\n58867 59732\n817520 962123\n125391 163027\n601766 617692\n381501 444610\n761937 817520\n16 10551\n21096 38291\n718073 761937\n583868 601766\n554859 731755\n678098 718073\n962123 992003\n163027 203671\n87917 96397", "output": "1\n12 " } ]
310
0
0
18,919
338
GCD Table
[ "chinese remainder theorem", "math", "number theory" ]
null
null
Consider a table *G* of size *n*<=×<=*m* such that *G*(*i*,<=*j*)<==<=*GCD*(*i*,<=*j*) for all 1<=≤<=*i*<=≤<=*n*,<=1<=≤<=*j*<=≤<=*m*. *GCD*(*a*,<=*b*) is the greatest common divisor of numbers *a* and *b*. You have a sequence of positive integer numbers *a*1,<=*a*2,<=...,<=*a**k*. We say that this sequence occurs in table *G* if it coincides with consecutive elements in some row, starting from some position. More formally, such numbers 1<=≤<=*i*<=≤<=*n* and 1<=≤<=*j*<=≤<=*m*<=-<=*k*<=+<=1 should exist that *G*(*i*,<=*j*<=+<=*l*<=-<=1)<==<=*a**l* for all 1<=≤<=*l*<=≤<=*k*. Determine if the sequence *a* occurs in table *G*.
The first line contains three space-separated integers *n*, *m* and *k* (1<=≤<=*n*,<=*m*<=≤<=1012; 1<=≤<=*k*<=≤<=10000). The second line contains *k* space-separated integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=1012).
Print a single word "YES", if the given sequence occurs in table *G*, otherwise print "NO".
[ "100 100 5\n5 2 1 2 1\n", "100 8 5\n5 2 1 2 1\n", "100 100 7\n1 2 3 4 5 6 7\n" ]
[ "YES\n", "NO\n", "NO\n" ]
Sample 1. The tenth row of table *G* starts from sequence {1, 2, 1, 2, 5, 2, 1, 2, 1, 10}. As you can see, elements from fifth to ninth coincide with sequence *a*. Sample 2. This time the width of table *G* equals 8. Sequence *a* doesn't occur there.
[ { "input": "100 100 5\n5 2 1 2 1", "output": "YES" }, { "input": "100 8 5\n5 2 1 2 1", "output": "NO" }, { "input": "100 100 7\n1 2 3 4 5 6 7", "output": "NO" }, { "input": "5 5 5\n1 1 1 1 1", "output": "YES" }, { "input": "11 10 1\n11", "output": "NO" }, { "input": "108 942 35\n1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 1 1 3 31 1 3 1 1 3 1 1 3 1 1", "output": "YES" }, { "input": "1000000000000 1000000000000 116\n1587924000 7 2 3 4 5 6 1 56 9 10 1 12 13 2 105 16 1 18 1 620 3 14 1 24 25 26 27 4 203 30 1 32 3 2 5 252 1 2 39 40 1 6 7 4 45 2 1 48 1 350 93 52 1 54 5 8 21 58 1 60 1 2 9 224 65 6 1 4 3 10 7 72 1 2 75 4 1 546 1 80 81 62 1 12 35 2 87 8 1 90 13 28 3 2 5 96 1 2 63 100 1 6 1 104 15 14 1 108 1 10 3 16 217 6 5", "output": "YES" }, { "input": "1000000000000 1000000000000 10\n99991 99992 99993 99994 99995 99996 99997 99998 99999 31000000000", "output": "NO" }, { "input": "100 100 10\n3 5 1 1 1 1 1 1 1 9", "output": "NO" }, { "input": "54275126675 128566125 50\n1 1 3 1 1 3 7 1 9 1 11 3 13 7 3 1 1 27 1 1 21 11 1 3 1 13 9 7 1 3 1 1 33 1 7 9 37 1 39 1 1 21 1 11 27 1 1 3 7 1", "output": "YES" }, { "input": "100000 49999 2\n50000 1", "output": "NO" }, { "input": "1000000000000 1000000000000 59\n1 1 3 1 5 3 1 1 3 5 1 3 1 1 15 1 1 3 1 5 3 1 1 3 5 1 3 1 1 15 1 1 3 1 5 3 1 1 3 5 1 3 1 1 15 1 1 3 1 5 3 1 1 3 5 1 3 1 1", "output": "YES" }, { "input": "1000000000000 1000000000000 6\n8 21 2 1 12 1", "output": "YES" }, { "input": "1000000000000 1000000000000 6\n1 6 5 2 3 2", "output": "YES" }, { "input": "1000000000000 1000000000000 100\n2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 74 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 111 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1", "output": "YES" }, { "input": "1000000000000 1000000000000 100\n2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 74 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 12 1 2 3 2 1 18 1 2 111 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1", "output": "NO" }, { "input": "1000000000000 1000000000000 100\n2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 74 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1 18 1 2 111 2 1 6 1 2 9 2 1 6 1 2 3 2 1 9 1 2 3 2 1 6 1 2 9 2 1 6 1 2 3 2 1", "output": "NO" }, { "input": "1000000000000 1000000000000 40\n2 1 8 1 10 1 4 1 2 25 16 1 2 1 20 1 2 1 8 5 2 1 4 1 10 1 128 1 2 5 4 1 2 1 1000 1 2 1 4 5", "output": "YES" }, { "input": "1000000000000 1000000000000 40\n2 1 8 1 10 1 4 1 2 5 16 1 2 1 20 1 2 1 8 5 2 1 4 1 10 1 64 1 2 5 4 1 2 1 500 1 2 1 4 5", "output": "NO" }, { "input": "1000000000000 1000000000000 2\n1 1000000000000", "output": "YES" }, { "input": "1000000000000 1000000000000 4\n1 2 1 100000000000", "output": "YES" }, { "input": "991234567890 927215128595 5\n6 11 8 3 2000000014", "output": "YES" }, { "input": "991234567890 182000001269 5\n6 11 8 3 2000000014", "output": "NO" }, { "input": "999999999999 999999999999 2\n20145182300 20145182301", "output": "NO" } ]
154
0
0
18,926
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 100000 100000 10000 10000", "output": "100000 100000 100000" }, { "input": "100000 -100000 100000 -10000 100000", "output": "-105000.0 100000.0 95000.0" }, { "input": "1 0 0 0 -1", "output": "0.0 0.0 1.0" }, { "input": "100000 83094 84316 63590 53480", "output": "100069.69149822203 111154.72144376408 68243.2515742123" }, { "input": "1 0 0 0 0", "output": "0.5 0.0 0.5" }, { "input": "1 0 0 -2 -2", "output": "0 0 1" }, { "input": "10 0 0 4 0", "output": "-3.0 0.0 7.0" }, { "input": "82 1928 -30264 2004 -30294", "output": "1927.8636359254158 -30263.946172075823 81.85339643163098" }, { "input": "75 -66998 89495 -66988 89506", "output": "-67018.22522977486 89472.75224724766 44.933034373659254" }, { "input": "11 9899 34570 9895 34565", "output": "9900.435822761548 34571.794778451935 8.701562118716424" }, { "input": "21 7298 -45672 7278 -45677", "output": "7298.186496251526 -45671.95337593712 20.80776406404415" }, { "input": "31 84194 -71735 84170 -71758", "output": "84194 -71735 31" }, { "input": "436 25094 -66597 25383 -66277", "output": "25092.386577687754 -66598.78648837341 433.5927874489312" }, { "input": "390 -98011 78480 -98362 78671", "output": "-98011 78480 390" }, { "input": "631 -21115 -1762 -21122 -1629", "output": "-21101.91768814977 -2010.563925154407 382.0920415665416" }, { "input": "872 55782 51671 54965 51668", "output": "55809.49706065544 51671.100968398976 844.502753968685" }, { "input": "519 -92641 -28571 -92540 -28203", "output": "-92659.18165738975 -28637.246038806206 450.30421903092184" }, { "input": "3412 23894 22453 26265 25460", "output": "23894 22453 3412" }, { "input": "3671 -99211 -3610 -99825 -1547", "output": "-98994.40770099283 -4337.736014416596 2911.7161725229744" }, { "input": "3930 -76494 -83852 -78181 -81125", "output": "-76303.71953677801 -84159.58436467478 3568.316718555632" }, { "input": "4189 -24915 61224 -28221 65024", "output": "-24915 61224 4189" }, { "input": "8318 -2198 35161 3849 29911", "output": "-2315.0277877457083 35262.60342081445 8163.0201360632545" }, { "input": "15096 -12439 58180 -10099 50671", "output": "-13514.641370727473 61631.70557811649 11480.578066612283" }, { "input": "70343 64457 3256 83082 -17207", "output": "50095.092392996106 19035.206193939368 49006.464709026186" }, { "input": "66440 -58647 -76987 2151 -40758", "output": "-58647 -76987 66440" }, { "input": "62537 18249 96951 -3656 54754", "output": "21702.922094423477 103604.5106422455 55040.41533091097" }, { "input": "88209 95145 42027 21960 26111", "output": "101649.61478542663 43441.59928844504 81552.34132964142" }, { "input": "100000 -100000 -100000 -100000 -100000", "output": "-50000.0 -100000.0 50000.0" }, { "input": "100000 100000 100000 100000 100000", "output": "150000.0 100000.0 50000.0" }, { "input": "2 0 0 0 1", "output": "0.0 -0.5 1.5" }, { "input": "1 1 0 1 0", "output": "1.5 0.0 0.5" }, { "input": "2 3 3 3 3", "output": "4.0 3.0 1.0" }, { "input": "1 1 1 1 1", "output": "1.5 1.0 0.5" }, { "input": "10 1 1 1 1", "output": "6.0 1.0 5.0" }, { "input": "10 5 5 5 10", "output": "5.0 2.5 7.5" }, { "input": "5 0 0 0 0", "output": "2.5 0.0 2.5" } ]
31
512,000
0
18,932
396
On Number of Decompositions into Multipliers
[ "combinatorics", "math", "number theory" ]
null
null
You are given an integer *m* as a product of integers *a*1,<=*a*2,<=... *a**n* . Your task is to find the number of distinct decompositions of number *m* into the product of *n* ordered positive integers. Decomposition into *n* products, given in the input, must also be considered in the answer. As the answer can be very large, print it modulo 1000000007 (109<=+<=7).
The first line contains positive integer *n* (1<=≤<=*n*<=≤<=500). The second line contains space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
In a single line print a single number *k* — the number of distinct decompositions of number *m* into *n* ordered multipliers modulo 1000000007 (109<=+<=7).
[ "1\n15\n", "3\n1 1 2\n", "2\n5 7\n" ]
[ "1\n", "3\n", "4\n" ]
In the second sample, the get a decomposition of number 2, you need any one number out of three to equal 2, and the rest to equal 1. In the third sample, the possible ways of decomposing into ordered multipliers are [7,5], [5,7], [1,35], [35,1]. A decomposition of positive integer *m* into *n* ordered multipliers is a cortege of positive integers *b* = {*b*<sub class="lower-index">1</sub>, *b*<sub class="lower-index">2</sub>, ... *b*<sub class="lower-index">*n*</sub>} such that <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/607dd79be814c0a988453395ca6d82109b016083.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Two decompositions *b* and *c* are considered different, if there exists index *i* such that *b*<sub class="lower-index">*i*</sub> ≠ *c*<sub class="lower-index">*i*</sub>.
[ { "input": "1\n15", "output": "1" }, { "input": "3\n1 1 2", "output": "3" }, { "input": "2\n5 7", "output": "4" }, { "input": "2\n5 10", "output": "6" }, { "input": "3\n1 30 1", "output": "27" }, { "input": "2\n1000000000 1000000000", "output": "361" }, { "input": "1\n1", "output": "1" }, { "input": "3\n1 1 1", "output": "1" }, { "input": "2\n1 2", "output": "2" }, { "input": "2\n1 6", "output": "4" }, { "input": "3\n8 10 8", "output": "108" }, { "input": "5\n14 67 15 28 21", "output": "459375" }, { "input": "8\n836 13 77 218 743 530 404 741", "output": "544714485" }, { "input": "10\n6295 3400 4042 2769 3673 264 5932 4977 1776 5637", "output": "928377494" }, { "input": "23\n77 12 25 7 44 75 80 92 49 77 56 93 59 45 45 39 86 83 99 91 4 70 83", "output": "247701073" }, { "input": "1\n111546435", "output": "1" }, { "input": "7\n111546435 58642669 600662303 167375713 371700317 33984931 89809099", "output": "25706464" }, { "input": "19\n371700317 12112039 167375713 7262011 21093827 89809099 600662303 18181979 9363547 30857731 58642669 111546435 645328247 5605027 38706809 14457349 25456133 44227723 33984931", "output": "376284721" }, { "input": "1\n536870912", "output": "1" }, { "input": "2\n536870912 387420489", "output": "570" }, { "input": "10\n214358881 536870912 815730721 387420489 893871739 244140625 282475249 594823321 148035889 410338673", "output": "547239398" }, { "input": "5\n387420489 536870912 536870912 536870912 387420489", "output": "255309592" }, { "input": "5\n387420489 244140625 387420489 387420489 1", "output": "772171400" }, { "input": "10\n2097152 67108864 65536 262144 262144 131072 8388608 536870912 65536 2097152", "output": "176451954" }, { "input": "10\n237254761 1 817430153 1 1 1 1 1 90679621 1", "output": "1000" }, { "input": "20\n16777216 1048576 524288 8192 8192 524288 2097152 8388608 1048576 67108864 16777216 1048576 4096 8388608 134217728 67108864 1048576 536870912 67108864 67108864", "output": "985054761" }, { "input": "50\n675 25000 2025 50 450 31250 3750 225 1350 250 72 187500 12000 281250 187500 30000 45000 90000 90 1200 9000 56250 5760 270000 3125 3796875 2250 101250 40 2500 175781250 1250000 45000 2250 3000 31250 46875 135000 421875000 36000 360 140625000 13500 1406250 1125 250 75000 62500 150 6", "output": "18983788" }, { "input": "2\n999983 999983", "output": "3" }, { "input": "3\n1 1 39989", "output": "3" } ]
389
13,107,200
0
18,992
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" }, { "input": "5\nvasya 1000\nvasya 100\nkolya 200\npetya 300\noleg 400", "output": "4\nkolya noob\noleg random\npetya random\nvasya pro" }, { "input": "10\na 1\nb 2\nc 3\nd 4\ne 5\nf 6\ng 7\nh 8\ni 9\nj 10", "output": "10\na noob\nb noob\nc noob\nd noob\ne random\nf random\ng random\nh average\ni hardcore\nj pro" }, { "input": "10\nj 10\ni 9\nh 8\ng 7\nf 6\ne 5\nd 4\nc 3\nb 2\na 1", "output": "10\na noob\nb noob\nc noob\nd noob\ne random\nf random\ng random\nh average\ni hardcore\nj pro" }, { "input": "1\ntest 0", "output": "1\ntest pro" } ]
92
0
0
19,087
97
Domino
[ "brute force", "implementation" ]
A. Domino
0
256
Little Gennady was presented with a set of domino for his birthday. The set consists of 28 different dominoes of size 2<=×<=1. Both halves of each domino contain one digit from 0 to 6. The figure that consists of 28 dominoes is called magic, if it can be fully covered with 14 non-intersecting squares of size 2<=×<=2 so that each square contained four equal numbers. Every time Gennady assembles a magic figure, some magic properties of the set appear — he wins the next contest. Gennady noticed that he can't assemble a figure that has already been assembled, otherwise someone else wins the contest. Gennady chose a checked field of size *n*<=×<=*m* and put there rectangular chips of sizes 1<=×<=2 and 2<=×<=1. Each chip fully occupies exactly two neighboring squares of the field. Those chips do not overlap but they can touch each other. Overall the field has exactly 28 chips, equal to the number of dominoes in the set. Now Gennady wants to replace each chip with a domino so that a magic figure appeared as a result. Different chips should be replaced by different dominoes. Determine in what number of contests Gennady can win over at the given position of the chips. You are also required to find one of the possible ways of replacing chips with dominoes to win the next Codeforces round.
The first line contains two positive integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=30). Each of the following *n* lines contains *m* characters, which is the position of chips on the field. The dots stand for empty spaces, Latin letters from "a" to "z" and "A", "B" stand for the positions of the chips. There are exactly 28 chips on the field. The squares covered by the same chip are marked by the same letter, different chips are marked by different letters. It is guaranteed that the field's description is correct. It is also guaranteed that at least one solution exists.
Print on the first line the number of ways to replace chips with dominoes to get a magic figure. That is the total number of contests that can be won using this arrangement of the chips. Next *n* lines containing *m* characters each, should contain a field from dots and numbers from 0 to 6 — any of the possible solutions. All dominoes should be different.
[ "8 8\n.aabbcc.\n.defghi.\nkdefghij\nklmnopqj\n.lmnopq.\n.rstuvw.\nxrstuvwy\nxzzAABBy\n" ]
[ "10080\n.001122.\n.001122.\n33440055\n33440055\n.225566.\n.225566.\n66113344\n66113344\n" ]
none
[]
500
1,331,200
0
19,121
303
Rectangle Puzzle II
[ "implementation", "math" ]
null
null
You are given a rectangle grid. That grid's size is *n*<=×<=*m*. Let's denote the coordinate system on the grid. So, each point on the grid will have coordinates — a pair of integers (*x*,<=*y*) (0<=≤<=*x*<=≤<=*n*,<=0<=≤<=*y*<=≤<=*m*). Your task is to find a maximum sub-rectangle on the grid (*x*1,<=*y*1,<=*x*2,<=*y*2) so that it contains the given point (*x*,<=*y*), and its length-width ratio is exactly (*a*,<=*b*). In other words the following conditions must hold: 0<=≤<=*x*1<=≤<=*x*<=≤<=*x*2<=≤<=*n*, 0<=≤<=*y*1<=≤<=*y*<=≤<=*y*2<=≤<=*m*, . The sides of this sub-rectangle should be parallel to the axes. And values *x*1,<=*y*1,<=*x*2,<=*y*2 should be integers. If there are multiple solutions, find the rectangle which is closest to (*x*,<=*y*). Here "closest" means the Euclid distance between (*x*,<=*y*) and the center of the rectangle is as small as possible. If there are still multiple solutions, find the lexicographically minimum one. Here "lexicographically minimum" means that we should consider the sub-rectangle as sequence of integers (*x*1,<=*y*1,<=*x*2,<=*y*2), so we can choose the lexicographically minimum one.
The first line contains six integers *n*,<=*m*,<=*x*,<=*y*,<=*a*,<=*b* (1<=≤<=*n*,<=*m*<=≤<=109,<=0<=≤<=*x*<=≤<=*n*,<=0<=≤<=*y*<=≤<=*m*,<=1<=≤<=*a*<=≤<=*n*,<=1<=≤<=*b*<=≤<=*m*).
Print four integers *x*1,<=*y*1,<=*x*2,<=*y*2, which represent the founded sub-rectangle whose left-bottom point is (*x*1,<=*y*1) and right-up point is (*x*2,<=*y*2).
[ "9 9 5 5 2 1\n", "100 100 52 50 46 56\n" ]
[ "1 3 9 7\n", "17 8 86 92\n" ]
none
[ { "input": "9 9 5 5 2 1", "output": "1 3 9 7" }, { "input": "100 100 52 50 46 56", "output": "17 8 86 92" }, { "input": "100 100 16 60 42 75", "output": "0 0 56 100" }, { "input": "100 100 28 22 47 50", "output": "0 0 94 100" }, { "input": "100 100 44 36 96 21", "output": "0 25 96 46" }, { "input": "100 100 56 46 1 47", "output": "55 0 57 94" }, { "input": "100 100 20 53 6 22", "output": "6 1 33 100" }, { "input": "100 100 32 63 2 41", "output": "30 18 34 100" }, { "input": "100 100 48 73 63 16", "output": "16 65 79 81" }, { "input": "100 100 13 59 14 20", "output": "0 0 70 100" }, { "input": "36830763 28058366 30827357 20792295 11047103 20670351", "output": "25303805 7388015 36350908 28058366" }, { "input": "87453374 60940601 74141787 32143714 78082907 33553425", "output": "9370467 15367001 87453374 48920426" }, { "input": "71265727 62692710 12444778 3479306 21442685 5463351", "output": "0 0 64328055 16390053" }, { "input": "48445042 43730155 14655564 6244917 43454856 2866363", "output": "0 4811735 43454856 7678098" }, { "input": "85759276 82316701 8242517 1957176 10225118 547026", "output": "0 0 81800944 4376208" }, { "input": "64748258 21983760 9107246 2437546 11247507 8924750", "output": "0 0 22495014 17849500" }, { "input": "6561833 24532010 2773123 457562 6225818 23724637", "output": "0 0 6225818 23724637" }, { "input": "33417574 19362112 17938303 4013355 10231192 2596692", "output": "166200 0 33417574 8439249" }, { "input": "98540143 28776614 12080542 1456439 96484500 3125739", "output": "0 0 96484500 3125739" }, { "input": "75549175 99860242 42423626 6574859 73199290 26030615", "output": "2349885 0 75549175 26030615" }, { "input": "4309493 76088457 2523467 46484812 909115 53662610", "output": "1887086 960803 3159847 76088457" }, { "input": "99373741 10548319 82293354 9865357 58059929 5328757", "output": "41313812 5219562 99373741 10548319" }, { "input": "81460 7041354 53032 1297536 41496 5748697", "output": "27916 0 78148 6958949" }, { "input": "5664399 63519726 1914884 13554302 2435218 44439020", "output": "697275 0 3132493 44439020" }, { "input": "19213492 76256257 10302871 19808004 19174729 55280126", "output": "38763 0 19213492 55280126" }, { "input": "61430678 95017800 11901852 27772249 25202227 87778634", "output": "0 0 25202227 87778634" }, { "input": "1063740 2675928 277215 2022291 204933 298547", "output": "0 1183193 1024665 2675928" }, { "input": "71580569 68590917 4383746 13851161 9868376 8579752", "output": "0 0 71545726 62203202" }, { "input": "17818532 82586436 8482338 54895799 12444902 11112345", "output": "2259887 49339626 14704789 60451971" }, { "input": "63651025 50179036 16141802 24793214 28944209 13993078", "output": "0 10800136 57888418 38786292" }, { "input": "11996821 42550832 8901163 19214381 3510233 20406511", "output": "4976355 0 11996821 40813022" }, { "input": "27048166 72584165 4785744 2001800 24615554 27645416", "output": "0 0 24615554 27645416" }, { "input": "47001271 53942737 7275347 1652337 33989593 48660013", "output": "0 0 33989593 48660013" }, { "input": "51396415 50182729 20810973 38206844 17823753 2905275", "output": "0 34333144 47530008 42080544" }, { "input": "27087649 52123970 20327636 19640608 8481031 14569965", "output": "1644556 0 27087649 43709895" }, { "input": "41635044 16614992 36335190 11150551 30440245 13728274", "output": "11194799 2886718 41635044 16614992" }, { "input": "97253692 35192249 21833856 26094161 41611668 32149284", "output": "0 363858 45079307 35192249" }, { "input": "60300478 3471217 11842517 3192374 27980820 507119", "output": "0 2456979 55961640 3471217" }, { "input": "69914272 30947694 58532705 25740028 30431847 27728130", "output": "39482425 3219564 69914272 30947694" }, { "input": "83973381 91192149 19059738 26429459 49573749 78006738", "output": "0 0 49573749 78006738" }, { "input": "1000000000 1000000000 286536427 579261823 230782719 575570138", "output": "171145067 291476754 401927786 867046892" }, { "input": "1000000000 1000000000 42362139 725664533 91213476 617352813", "output": "0 176862916 121617968 1000000000" }, { "input": "1000000000 1000000000 503220555 167034539 244352073 511651840", "output": "276322201 0 730118908 950210560" }, { "input": "1000000000 1000000000 259046267 313437250 252266478 848401810", "output": "132913028 0 385179506 848401810" }, { "input": "1000000000 1000000000 867388331 312356312 405405075 887925029", "output": "594594925 0 1000000000 887925029" }, { "input": "1000000000 1000000000 623214043 753726318 970868535 929707704", "output": "29131465 70292296 1000000000 1000000000" }, { "input": "1000000000 1000000000 84072459 754904836 124007132 824006731", "output": "22068893 175993269 146076025 1000000000" }, { "input": "1000000000 1000000000 839898171 196274842 131921537 865789406", "output": "773937402 0 905858939 865789406" }, { "input": "1000000000 1000000000 448240235 342677552 992352294 907572080", "output": "0 0 992352294 907572080" }, { "input": "1000000000 1000000000 837887296 643696230 478881476 45404539", "output": "42237048 598291691 1000000000 689100769" }, { "input": "1000000000 500 1000 400 11 122", "output": "978 12 1022 500" }, { "input": "1000000000 1000000000 1000000000 1000000000 1 1", "output": "0 0 1000000000 1000000000" }, { "input": "1000000000 1000000000 1000000000 1000000000 1000000000 1", "output": "0 999999999 1000000000 1000000000" }, { "input": "1000000000 999999999 1000 1000 1000000000 999999999", "output": "0 0 1000000000 999999999" }, { "input": "70 10 20 5 5 3", "output": "12 0 27 9" }, { "input": "1000000000 1000000000 500000000 500000000 500000000 500000001", "output": "250000000 249999999 750000000 750000000" } ]
156
5,222,400
0
19,174
439
Devu and Partitioning of the Array
[ "brute force", "constructive algorithms", "implementation", "number theory" ]
null
null
Devu being a small kid, likes to play a lot, but he only likes to play with arrays. While playing he came up with an interesting question which he could not solve, can you please solve it for him? Given an array consisting of distinct integers. Is it possible to partition the whole array into *k* disjoint non-empty parts such that *p* of the parts have even sum (each of them must have even sum) and remaining *k* - *p* have odd sum? (note that parts need not to be continuous). If it is possible to partition the array, also give any possible way of valid partitioning.
The first line will contain three space separated integers *n*, *k*, *p* (1<=≤<=*k*<=≤<=*n*<=≤<=105; 0<=≤<=*p*<=≤<=*k*). The next line will contain *n* space-separated distinct integers representing the content of array *a*: *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109).
In the first line print "YES" (without the quotes) if it is possible to partition the array in the required way. Otherwise print "NO" (without the quotes). If the required partition exists, print *k* lines after the first line. The *i**th* of them should contain the content of the *i**th* part. Print the content of the part in the line in the following way: firstly print the number of elements of the part, then print all the elements of the part in arbitrary order. There must be exactly *p* parts with even sum, each of the remaining *k* - *p* parts must have odd sum. As there can be multiple partitions, you are allowed to print any valid partition.
[ "5 5 3\n2 6 10 5 9\n", "5 5 3\n7 14 2 9 5\n", "5 3 1\n1 2 3 7 5\n" ]
[ "YES\n1 9\n1 5\n1 10\n1 6\n1 2\n", "NO\n", "YES\n3 5 1 3\n1 7\n1 2\n" ]
none
[ { "input": "5 5 3\n2 6 10 5 9", "output": "YES\n1 9\n1 5\n1 10\n1 6\n1 2" }, { "input": "5 5 3\n7 14 2 9 5", "output": "NO" }, { "input": "5 3 1\n1 2 3 7 5", "output": "YES\n3 5 1 3\n1 7\n1 2" }, { "input": "10 5 3\n194757070 828985446 11164 80016 84729 117765558 111730436 164044532 419732980 48834", "output": "NO" }, { "input": "10 6 3\n861947514 34945 190135645 68731 44833 387988147 84308862 878151920 458358978 809653252", "output": "YES\n5 387988147 861947514 84308862 34945 190135645\n1 44833\n1 68731\n1 809653252\n1 458358978\n1 878151920" }, { "input": "10 8 3\n677037706 41099140 89128206 168458947 367939060 940344093 191391519 981938946 31319 34929915", "output": "YES\n3 34929915 677037706 41099140\n1 31319\n1 191391519\n1 940344093\n1 168458947\n1 981938946\n1 367939060\n1 89128206" }, { "input": "10 8 4\n214605891 246349108 626595204 63889 794527783 83684156 5535 865709352 262484651 157889982", "output": "NO" }, { "input": "10 6 3\n223143676 316703192 5286 408323576 61758 566101388 9871840 93989 91890 99264208", "output": "NO" }, { "input": "2 2 1\n409447178 258805801", "output": "YES\n1 258805801\n1 409447178" }, { "input": "2 1 1\n29161 15829", "output": "YES\n2 15829 29161" }, { "input": "3 3 1\n357071129 476170324 503481367", "output": "YES\n1 503481367\n1 357071129\n1 476170324" }, { "input": "3 3 1\n357071129 476170324 503481367", "output": "YES\n1 503481367\n1 357071129\n1 476170324" }, { "input": "2 1 1\n29161 15829", "output": "YES\n2 15829 29161" }, { "input": "23 22 3\n777717359 54451 123871650 211256633 193354035 935466677 800874233 532974165 62256 6511 3229 757421727 371493777 268999168 82355 22967 678259053 886134047 207070129 122416584 79851 35753 730872007", "output": "YES\n2 730872007 123871650\n1 35753\n1 79851\n1 207070129\n1 886134047\n1 678259053\n1 22967\n1 82355\n1 371493777\n1 757421727\n1 3229\n1 6511\n1 532974165\n1 800874233\n1 935466677\n1 193354035\n1 211256633\n1 54451\n1 777717359\n1 122416584\n1 268999168\n1 62256" }, { "input": "16 9 9\n826588597 70843 528358243 60844 636968393 862405463 58232 69241 617006886 903909155 973050249 9381 49031 40100022 62141 43805", "output": "YES\n3 40100022 826588597 70843\n1 617006886\n1 58232\n1 60844\n2 43805 62141\n2 49031 9381\n2 973050249 903909155\n2 69241 862405463\n2 636968393 528358243" }, { "input": "5 2 2\n316313049 24390603 595539594 514135024 39108", "output": "YES\n4 39108 595539594 316313049 24390603\n1 514135024" }, { "input": "5 2 1\n12474 117513621 32958 767146609 20843", "output": "YES\n4 20843 12474 117513621 767146609\n1 32958" }, { "input": "5 4 1\n387119493 716009972 88510 375210205 910834347", "output": "YES\n2 910834347 716009972\n1 375210205\n1 387119493\n1 88510" }, { "input": "5 4 3\n674318396 881407702 882396010 208141498 53145", "output": "YES\n2 53145 674318396\n1 208141498\n1 882396010\n1 881407702" }, { "input": "3 2 1\n976825506 613159225 249024714", "output": "YES\n2 613159225 976825506\n1 249024714" }, { "input": "4 1 1\n173508914 11188 90766233 20363", "output": "YES\n4 11188 173508914 90766233 20363" }, { "input": "30 24 12\n459253071 24156 64054 270713791 734796619 379920883 429646748 332144982 20929 27685 53253 82047732 172442017 34241 880121331 890223629 974692 954036632 68638567 972921811 421106382 64615 819330514 179630214 608594496 802986133 231010377 184513476 73143 93045", "output": "YES\n7 93045 459253071 270713791 734796619 379920883 20929 27685\n1 73143\n1 231010377\n1 802986133\n1 64615\n1 972921811\n1 68638567\n1 890223629\n1 880121331\n1 34241\n1 172442017\n1 53253\n1 184513476\n1 608594496\n1 179630214\n1 819330514\n1 421106382\n1 954036632\n1 974692\n1 82047732\n1 332144982\n1 429646748\n1 64054\n1 24156" }, { "input": "9 5 1\n91623 466261329 311727429 18189 42557 22943 66177 473271749 62622", "output": "YES\n5 473271749 91623 466261329 311727429 18189\n1 66177\n1 22943\n1 42557\n1 62622" }, { "input": "4 1 1\n266639563 36517 172287193 166673809", "output": "YES\n4 166673809 172287193 266639563 36517" }, { "input": "5 2 2\n19571 180100775 421217758 284511211 49475", "output": "YES\n3 421217758 19571 180100775\n2 49475 284511211" }, { "input": "4 2 2\n736788713 82230 66800 37791827", "output": "YES\n3 66800 736788713 37791827\n1 82230" }, { "input": "5 1 1\n33889 469945850 40673 939970023 776172319", "output": "YES\n5 469945850 33889 40673 939970023 776172319" }, { "input": "1 1 0\n2", "output": "NO" }, { "input": "1 1 0\n3", "output": "YES\n1 3" }, { "input": "1 1 1\n2", "output": "YES\n1 2" }, { "input": "1 1 1\n3", "output": "NO" }, { "input": "2 2 2\n2 3", "output": "NO" }, { "input": "2 2 2\n2 4", "output": "YES\n1 4\n1 2" }, { "input": "2 2 1\n3 2", "output": "YES\n1 3\n1 2" }, { "input": "4 2 0\n3 5 7 9", "output": "YES\n3 9 3 5\n1 7" }, { "input": "3 2 0\n1 3 2", "output": "YES\n2 3 2\n1 1" }, { "input": "2 1 1\n2 4", "output": "YES\n2 4 2" }, { "input": "7 3 0\n1 3 5 7 9 11 13", "output": "YES\n5 13 1 3 5 7\n1 11\n1 9" }, { "input": "8 4 4\n1 3 5 7 9 11 13 15", "output": "YES\n2 15 13\n2 11 9\n2 7 5\n2 3 1" }, { "input": "2 1 1\n1 3", "output": "YES\n2 3 1" } ]
155
10,956,800
0
19,176
796
Exam Cheating
[ "binary search", "dp" ]
null
null
Zane and Zane's crush have just decided to date! However, the girl is having a problem with her Physics final exam, and needs your help. There are *n* questions, numbered from 1 to *n*. Question *i* comes before question *i*<=+<=1 (1<=≤<=*i*<=&lt;<=*n*). Each of the questions cannot be guessed on, due to the huge penalty for wrong answers. The girl luckily sits in the middle of two geniuses, so she is going to cheat. However, the geniuses have limitations. Each of them may or may not know the answers to some questions. Anyway, it is safe to assume that the answers on their answer sheets are absolutely correct. To make sure she will not get caught by the proctor, the girl will glance at most *p* times, each time looking at no more than *k* consecutive questions on one of the two geniuses' answer sheet. When the girl looks at some question on an answer sheet, she copies the answer to that question if it is on that answer sheet, or does nothing otherwise. Help the girl find the maximum number of questions she can get correct.
The first line contains three integers *n*, *p*, and *k* (1<=≤<=*n*,<=*p*<=≤<=1,<=000, 1<=≤<=*k*<=≤<=*min*(*n*,<=50)) — the number of questions, the maximum number of times the girl can glance, and the maximum number of consecutive questions that can be looked at in one time glancing, respectively. The second line starts with one integer *r* (0<=≤<=*r*<=≤<=*n*), denoting the number of questions the first genius has answered on his answer sheet. Then follow *r* integers *a*1,<=*a*2,<=...,<=*a**r* (1<=≤<=*a**i*<=≤<=*n*) — the answered questions, given in a strictly-increasing order (that is, *a**i*<=&lt;<=*a**i*<=+<=1). The third line starts with one integer *s* (0<=≤<=*s*<=≤<=*n*), denoting the number of questions the second genius has answered on his answer sheet. Then follow *s* integers *b*1,<=*b*2,<=...,<=*b**s* (1<=≤<=*b**i*<=≤<=*n*) — the answered questions, given in a strictly-increasing order (that is, *b**i*<=&lt;<=*b**i*<=+<=1).
Print one integer — the maximum number of questions the girl can answer correctly.
[ "6 2 3\n3 1 3 6\n4 1 2 5 6\n", "8 3 3\n4 1 3 5 6\n5 2 4 6 7 8\n" ]
[ "4", "7" ]
Let (*x*, *l*, *r*) denote the action of looking at all questions *i* such that *l* ≤ *i* ≤ *r* on the answer sheet of the *x*-th genius. In the first sample, the girl could get 4 questions correct by performing sequence of actions (1, 1, 3) and (2, 5, 6). In the second sample, the girl could perform sequence of actions (1, 3, 5), (2, 2, 4), and (2, 6, 8) to get 7 questions correct.
[]
46
0
0
19,178
10
LCIS
[ "dp" ]
D. LCIS
1
256
This problem differs from one which was on the online contest. The sequence *a*1,<=*a*2,<=...,<=*a**n* is called increasing, if *a**i*<=&lt;<=*a**i*<=+<=1 for *i*<=&lt;<=*n*. The sequence *s*1,<=*s*2,<=...,<=*s**k* is called the subsequence of the sequence *a*1,<=*a*2,<=...,<=*a**n*, if there exist such a set of indexes 1<=≤<=*i*1<=&lt;<=*i*2<=&lt;<=...<=&lt;<=*i**k*<=≤<=*n* that *a**i**j*<==<=*s**j*. In other words, the sequence *s* can be derived from the sequence *a* by crossing out some elements. You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=500) — the length of the first sequence. The second line contains *n* space-separated integers from the range [0,<=109] — elements of the first sequence. The third line contains an integer *m* (1<=≤<=*m*<=≤<=500) — the length of the second sequence. The fourth line contains *m* space-separated integers from the range [0,<=109] — elements of the second sequence.
In the first line output *k* — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
[ "7\n2 3 1 6 5 4 6\n4\n1 3 5 6\n", "5\n1 2 0 2 1\n3\n1 0 1\n" ]
[ "3\n3 5 6 \n", "2\n0 1 \n" ]
none
[ { "input": "7\n2 3 1 6 5 4 6\n4\n1 3 5 6", "output": "3\n3 5 6 " }, { "input": "5\n1 2 0 2 1\n3\n1 0 1", "output": "2\n0 1 " }, { "input": "2\n6 10\n3\n6 3 3", "output": "1\n6 " }, { "input": "1\n7\n2\n7 9", "output": "1\n7 " }, { "input": "3\n37 49 24\n3\n33 5 70", "output": "0" }, { "input": "10\n7 10 1 2 1 7 1 5 9 9\n9\n6 2 5 6 7 7 5 5 2", "output": "2\n2 7 " }, { "input": "9\n7 0 1 2 6 0 10 3 5\n4\n8 4 0 3", "output": "2\n0 3 " }, { "input": "9\n7 4 4 5 0 6 5 4 10\n4\n5 2 10 9", "output": "2\n5 10 " }, { "input": "8\n7 8 6 6 8 10 3 3\n5\n7 4 10 8 7", "output": "2\n7 8 " }, { "input": "7\n4 2 4 3 10 3 6\n9\n7 5 2 3 0 1 6 1 4", "output": "3\n2 3 6 " }, { "input": "1\n7\n10\n1 8 8 10 9 10 4 6 0 5", "output": "0" }, { "input": "2\n5 2\n4\n8 8 0 4", "output": "0" }, { "input": "3\n1 3 9\n10\n8 0 10 5 7 0 3 1 2 4", "output": "1\n1 " }, { "input": "15\n10 4 7 7 10 1 4 9 1 10 9 6 8 8 2\n2\n0 4", "output": "1\n4 " }, { "input": "16\n8 8 6 7 10 0 10 1 7 6 6 0 4 2 6 7\n12\n9 3 8 4 10 3 9 8 3 7 10 4", "output": "2\n8 10 " }, { "input": "23\n174 172 196 135 91 174 208 92 132 53 202 118 5 244 161 140 71 21 185 56 60 195 217\n17\n38 218 120 77 22 214 164 194 79 195 36 167 42 89 201 80 11", "output": "1\n195 " }, { "input": "53\n135 168 160 123 6 250 251 158 245 184 206 35 189 64 138 12 69 21 112 198 165 211 109 40 192 98 236 216 255 98 136 38 67 79 25 196 216 64 134 124 102 232 229 102 179 138 111 123 2 93 25 162 57\n57\n64 143 41 144 73 26 11 17 224 209 167 162 129 39 102 224 254 45 120 2 138 213 139 133 169 54 7 143 242 118 155 189 100 185 145 168 248 131 83 216 142 180 225 35 226 202 8 15 200 192 75 140 191 189 75 116 202", "output": "3\n64 138 192 " }, { "input": "83\n95 164 123 111 177 71 38 225 103 59 210 209 117 139 115 140 66 21 39 84 14 227 0 43 90 233 96 98 232 237 108 139 55 220 14 225 134 39 68 167 193 125 86 216 87 14 94 75 255 24 165 98 177 191 239 123 98 90 29 52 155 231 187 90 180 1 31 237 167 145 242 115 61 190 47 41 61 206 191 248 126 196 26\n49\n234 134 9 207 37 95 116 239 105 197 191 15 151 249 156 235 17 161 197 199 87 78 191 188 44 151 179 238 72 29 228 157 174 99 190 114 95 185 160 168 58 216 131 151 233 204 213 87 76", "output": "2\n95 233 " }, { "input": "13\n55 160 86 99 92 148 81 36 216 191 214 127 44\n85\n92 12 64 21 221 225 119 243 147 47 244 112 212 237 209 121 81 239 43 104 3 254 52 13 1 210 28 18 199 75 251 146 77 28 253 211 50 35 42 160 157 104 155 37 241 78 42 190 150 228 193 96 190 178 232 65 231 186 1 123 212 126 239 22 214 186 245 249 66 234 57 78 173 229 185 23 240 91 127 177 240 105 77 208 86", "output": "2\n160 214 " }, { "input": "94\n100 161 99 102 209 51 5 188 217 53 121 5 233 55 25 156 136 195 243 157 110 202 136 151 86 171 253 38 126 40 27 76 60 119 222 52 134 104 184 146 133 220 88 108 246 61 215 184 181 134 223 164 41 193 232 217 38 192 226 91 81 99 204 232 178 4 187 61 160 255 121 142 191 114 114 181 226 49 86 55 252 169 59 190 246 93 21 22 17 18 120 88 93 144\n30\n61 51 176 38 119 33 100 185 103 84 161 166 103 227 43 200 127 53 52 89 19 215 76 254 110 30 239 247 11 182", "output": "3\n51 53 110 " }, { "input": "6\n3 5 4 6 8 1\n10\n3 3 0 5 4 0 10 5 6 8", "output": "4\n3 5 6 8 " }, { "input": "10\n0 1 2 3 4 5 6 7 8 9\n10\n0 1 2 3 4 5 6 7 8 9", "output": "10\n0 1 2 3 4 5 6 7 8 9 " }, { "input": "8\n2 3 4 5 6 8 9 5\n9\n2 2 3 4 5 6 6 8 9", "output": "7\n2 3 4 5 6 8 9 " }, { "input": "8\n0 4 10 6 7 2 8 5\n7\n0 4 6 7 7 0 8", "output": "5\n0 4 6 7 8 " }, { "input": "10\n0 1 2 3 4 5 6 7 8 9\n10\n0 1 2 3 4 5 6 7 8 9", "output": "10\n0 1 2 3 4 5 6 7 8 9 " }, { "input": "17\n12 17 39 156 100 177 188 129 14 142 45 144 243 151 158 194 245\n16\n125 12 17 199 65 39 100 185 129 194 142 144 62 92 158 194", "output": "9\n12 17 39 100 129 142 144 158 194 " }, { "input": "20\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207\n20\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207", "output": "20\n7 17 24 27 36 45 62 92 93 94 98 112 114 138 143 156 173 199 204 207 " }, { "input": "13\n0 46 104 116 63 118 158 16 221 222 136 245 223\n9\n0 46 104 116 118 158 221 222 245", "output": "9\n0 46 104 116 118 158 221 222 245 " }, { "input": "13\n34 38 51 57 73 125 147 158 160 178 188 198 235\n15\n34 38 51 57 73 125 147 158 160 178 255 67 188 198 235", "output": "13\n34 38 51 57 73 125 147 158 160 178 188 198 235 " }, { "input": "17\n25 29 37 207 122 189 118 42 54 95 154 160 162 225 228 237 248\n19\n25 29 248 37 147 209 42 54 255 95 154 160 162 225 228 237 73 248 10", "output": "13\n25 29 37 42 54 95 154 160 162 225 228 237 248 " }, { "input": "10\n62914292 123971042 784965687 324817892 379711365 394545872 813282270 822333477 865397146 437913515\n9\n297835672 62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146", "output": "8\n62914292 123971042 324817892 379711365 394545872 813282270 822333477 865397146 " }, { "input": "10\n130077811 57466561 335978192 71385678 434259735 454136111 482887469 530031703 688581885 809880630\n8\n373627898 57466561 71385678 434259735 454136111 482887469 530031703 809880630", "output": "7\n57466561 71385678 434259735 454136111 482887469 530031703 809880630 " }, { "input": "17\n364396044 90653796 82853043 311258337 326557522 362475139 415783272 428510002 840021181 469284863 541444887 650535473 649258258 750028895 791368777 808443140 959785237\n13\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237", "output": "13\n82853043 311258337 326557522 362475139 415783272 428510002 469284863 541444887 649258258 750028895 791368777 808443140 959785237 " }, { "input": "3\n6379263 55134355 76061584\n3\n6379263 55134355 76061584", "output": "3\n6379263 55134355 76061584 " }, { "input": "3\n48875076 71023491 76538219\n3\n48875076 71023491 76538219", "output": "3\n48875076 71023491 76538219 " }, { "input": "5\n6621317 78540394 52064998 89150480 53659440\n3\n78540394 46008538 839195", "output": "1\n78540394 " }, { "input": "2\n34665180 51128665\n5\n71074966 34665180 47089728 44119345 51128665", "output": "2\n34665180 51128665 " }, { "input": "4\n3 4 9 1\n7\n5 3 8 9 10 2 1", "output": "2\n3 9 " } ]
109
1,843,200
3.942067
19,208
487
Fight the Monster
[ "binary search", "brute force", "implementation" ]
null
null
A monster is attacking the Cyberland! Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (*HP*), offensive power (*ATK*) and defensive power (*DEF*). During the battle, every second the monster's HP decrease by *max*(0,<=*ATK**Y*<=-<=*DEF**M*), while Yang's HP decreases by *max*(0,<=*ATK**M*<=-<=*DEF**Y*), where index *Y* denotes Master Yang and index *M* denotes monster. Both decreases happen simultaneously Once monster's *HP*<=≤<=0 and the same time Master Yang's *HP*<=&gt;<=0, Master Yang wins. Master Yang can buy attributes from the magic shop of Cyberland: *h* bitcoins per *HP*, *a* bitcoins per *ATK*, and *d* bitcoins per *DEF*. Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win.
The first line contains three integers *HP**Y*,<=*ATK**Y*,<=*DEF**Y*, separated by a space, denoting the initial *HP*, *ATK* and *DEF* of Master Yang. The second line contains three integers *HP**M*,<=*ATK**M*,<=*DEF**M*, separated by a space, denoting the *HP*, *ATK* and *DEF* of the monster. The third line contains three integers *h*,<=*a*,<=*d*, separated by a space, denoting the price of 1 *HP*, 1 *ATK* and 1 *DEF*. All numbers in input are integer and lie between 1 and 100 inclusively.
The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.
[ "1 2 1\n1 100 1\n1 100 100\n", "100 100 100\n1 1 1\n1 1 1\n" ]
[ "99\n", "0\n" ]
For the first sample, prices for *ATK* and *DEF* are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left. For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything.
[ { "input": "1 2 1\n1 100 1\n1 100 100", "output": "99" }, { "input": "100 100 100\n1 1 1\n1 1 1", "output": "0" }, { "input": "50 80 92\n41 51 56\n75 93 12", "output": "0" }, { "input": "76 63 14\n89 87 35\n20 15 56", "output": "915" }, { "input": "12 59 66\n43 15 16\n12 18 66", "output": "0" }, { "input": "51 89 97\n18 25 63\n22 91 74", "output": "0" }, { "input": "72 16 49\n5 21 84\n48 51 88", "output": "3519" }, { "input": "74 89 5\n32 76 99\n62 95 36", "output": "3529" }, { "input": "39 49 78\n14 70 41\n3 33 23", "output": "0" }, { "input": "11 82 51\n90 84 72\n98 98 43", "output": "1376" }, { "input": "65 6 5\n70 78 51\n88 55 78", "output": "7027" }, { "input": "14 61 87\n11 78 14\n5 84 92", "output": "0" }, { "input": "1 28 47\n31 60 38\n14 51 77", "output": "1562" }, { "input": "99 32 20\n89 72 74\n1 100 39", "output": "5478" }, { "input": "1 10 29\n1 1 43\n1 1 100", "output": "34" }, { "input": "1 1 100\n1 1 1\n100 1 100", "output": "1" }, { "input": "79 1 1\n1 1 10\n1 1 100", "output": "10" }, { "input": "10 10 100\n1 100 100\n10 100 90", "output": "9100" }, { "input": "10 10 100\n1 10 1\n1 1 100", "output": "0" }, { "input": "1 100 1\n1 1 1\n1 1 1", "output": "0" }, { "input": "11 1 1\n100 1 1\n100 1 1", "output": "1" }, { "input": "1 100 100\n1 1 1\n87 100 43", "output": "0" }, { "input": "10 100 1\n1 100 100\n100 1 9", "output": "811" }, { "input": "10 100 55\n100 100 1\n1 1 1", "output": "37" }, { "input": "11 1 1\n10 1 10\n100 50 1", "output": "500" }, { "input": "10 100 1\n100 1 1\n1 100 1", "output": "0" }, { "input": "1 10 10\n1 10 100\n1 1 61", "output": "91" }, { "input": "1 1 1\n1 1 1\n1 1 1", "output": "1" }, { "input": "1 1 1\n1 1 1\n100 100 100", "output": "100" }, { "input": "1 1 1\n100 100 100\n100 100 100", "output": "19900" }, { "input": "100 100 100\n1 1 1\n1 1 1", "output": "0" }, { "input": "100 100 100\n100 100 100\n100 100 100", "output": "100" }, { "input": "1 1 1\n1 1 100\n100 100 1", "output": "10000" }, { "input": "50 100 51\n100 100 100\n1 100 100", "output": "1384" }, { "input": "1 1 1\n100 100 100\n1 2 3", "output": "496" }, { "input": "100 1 1\n100 100 100\n100 1 100", "output": "199" }, { "input": "1 100 1\n100 100 100\n1 100 100", "output": "1990" }, { "input": "100 100 1\n100 100 100\n1 100 100", "output": "1891" }, { "input": "1 1 1\n100 100 100\n1 100 100", "output": "11890" }, { "input": "20 1 1\n100 100 100\n1 100 100", "output": "11871" }, { "input": "25 38 49\n84 96 42\n3 51 92", "output": "1692" }, { "input": "2 1 1\n100 2 100\n100 1 100", "output": "199" }, { "input": "1 97 1\n100 99 98\n1 51 52", "output": "1498" }, { "input": "1 1 1\n100 100 100\n1 1 100", "output": "298" }, { "input": "1 100 1\n100 100 99\n1 100 100", "output": "1890" }, { "input": "100 1 1\n100 100 100\n1 100 100", "output": "11791" } ]
155
2,252,800
3
19,220
630
Forecast
[ "math" ]
null
null
The Department of economic development of IT City created a model of city development till year 2100. To prepare report about growth perspectives it is required to get growth estimates from the model. To get the growth estimates it is required to solve a quadratic equation. Since the Department of economic development of IT City creates realistic models only, that quadratic equation has a solution, moreover there are exactly two different real roots. The greater of these roots corresponds to the optimistic scenario, the smaller one corresponds to the pessimistic one. Help to get these estimates, first the optimistic, then the pessimistic one.
The only line of the input contains three integers *a*,<=*b*,<=*c* (<=-<=1000<=≤<=*a*,<=*b*,<=*c*<=≤<=1000) — the coefficients of *ax*2<=+<=*bx*<=+<=*c*<==<=0 equation.
In the first line output the greater of the equation roots, in the second line output the smaller one. Absolute or relative error should not be greater than 10<=-<=6.
[ "1 30 200\n" ]
[ "-10.000000000000000\n-20.000000000000000" ]
none
[ { "input": "1 30 200", "output": "-10.000000000000000\n-20.000000000000000" }, { "input": "1 1 -1", "output": "0.618033988749895\n-1.618033988749895" }, { "input": "-1 1 1", "output": "1.618033988749895\n-0.618033988749895" }, { "input": "1000 1 -1", "output": "0.031126729201737\n-0.032126729201737" }, { "input": "-1 1000 1", "output": "1000.000999999000000\n-0.000999999000002" }, { "input": "1000 1000 -999", "output": "0.617586685675881\n-1.617586685675881" }, { "input": "633 304 -186", "output": "0.352747585783570\n-0.833000350396524" }, { "input": "-181 -227 368", "output": "0.930608581348335\n-2.184752227757175" }, { "input": "-779 -814 321", "output": "0.305204386055057\n-1.350133782717445" }, { "input": "1 1 0", "output": "0.000000000000000\n-1.000000000000000" }, { "input": "1 0 -9", "output": "3.000000000000000\n-3.000000000000000" } ]
46
0
0
19,229
392
Tower of Hanoi
[ "dp" ]
null
null
The Tower of Hanoi is a well-known mathematical puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: 1. Only one disk can be moved at a time. 1. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. 1. No disk may be placed on top of a smaller disk. With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2*n*<=-<=1, where *n* is the number of disks. (c) Wikipedia. SmallY's puzzle is very similar to the famous Tower of Hanoi. In the Tower of Hanoi puzzle you need to solve a puzzle in minimum number of moves, in SmallY's puzzle each move costs some money and you need to solve the same puzzle but for minimal cost. At the beginning of SmallY's puzzle all *n* disks are on the first rod. Moving a disk from rod *i* to rod *j* (1<=≤<=*i*,<=*j*<=≤<=3) costs *t**ij* units of money. The goal of the puzzle is to move all the disks to the third rod. In the problem you are given matrix *t* and an integer *n*. You need to count the minimal cost of solving SmallY's puzzle, consisting of *n* disks.
Each of the first three lines contains three integers — matrix *t*. The *j*-th integer in the *i*-th line is *t**ij* (1<=≤<=*t**ij*<=≤<=10000; *i*<=≠<=*j*). The following line contains a single integer *n* (1<=≤<=*n*<=≤<=40) — the number of disks. It is guaranteed that for all *i* (1<=≤<=*i*<=≤<=3), *t**ii*<==<=0.
Print a single integer — the minimum cost of solving SmallY's puzzle.
[ "0 1 1\n1 0 1\n1 1 0\n3\n", "0 2 2\n1 0 100\n1 2 0\n3\n", "0 2 1\n1 0 100\n1 2 0\n5\n" ]
[ "7\n", "19\n", "87\n" ]
none
[ { "input": "0 1 1\n1 0 1\n1 1 0\n3", "output": "7" }, { "input": "0 2 2\n1 0 100\n1 2 0\n3", "output": "19" }, { "input": "0 2 1\n1 0 100\n1 2 0\n5", "output": "87" }, { "input": "0 5835 1487\n6637 0 9543\n6961 6820 0\n7", "output": "723638" }, { "input": "0 3287 5433\n6796 0 5787\n1445 6158 0\n26", "output": "293974120391" }, { "input": "0 4449 3122\n6816 0 8986\n1048 1468 0\n4", "output": "73486" }, { "input": "0 913 8129\n8352 0 4408\n9073 7625 0\n30", "output": "6310499935698" }, { "input": "0 8392 3430\n5262 0 6256\n8590 8091 0\n29", "output": "3388490535940" }, { "input": "0 6593 2887\n9821 0 7109\n8501 917 0\n11", "output": "11231429" }, { "input": "0 2957 4676\n9787 0 1241\n5147 8582 0\n8", "output": "1162341" }, { "input": "0 4085 4623\n1929 0 2793\n902 8722 0\n11", "output": "7450335" }, { "input": "0 1404 2399\n3960 0 9399\n7018 4159 0\n34", "output": "79409173073874" }, { "input": "0 1429 1052\n4984 0 2116\n4479 782 0\n21", "output": "5047111802" }, { "input": "0 3844 8950\n8110 0 8591\n5977 4462 0\n7", "output": "875143" }, { "input": "0 7336 3824\n3177 0 6795\n4491 7351 0\n28", "output": "1538910647942" }, { "input": "0 8518 8166\n799 0 266\n7987 4940 0\n15", "output": "162320667" }, { "input": "0 2990 3624\n5985 0 9822\n3494 6400 0\n15", "output": "175936803" }, { "input": "0 3003 1005\n4320 0 1463\n4961 5563 0\n40", "output": "3633519425831590" }, { "input": "0 9916 3929\n5389 0 6509\n2557 4099 0\n38", "output": "1482783056079892" }, { "input": "0 2653 5614\n9654 0 8668\n6421 133 0\n40", "output": "6216516575480675" }, { "input": "0 6103 5951\n3308 0 8143\n3039 2918 0\n40", "output": "5710985562714285" }, { "input": "0 1655 1941\n7562 0 6518\n8541 184 0\n38", "output": "1280396561454826" }, { "input": "0 7561 1463\n7621 0 9485\n1971 1024 0\n38", "output": "1219526376186314" }, { "input": "0 5903 6945\n5521 0 2812\n8703 8684 0\n38", "output": "1709923833066384" }, { "input": "0 5382 7365\n7671 0 679\n3183 2634 0\n40", "output": "4417349048592850" }, { "input": "0 3448 4530\n6398 0 5321\n1302 139 0\n39", "output": "1967209554081624" }, { "input": "0 5105 2640\n1902 0 9380\n302 3014 0\n38", "output": "912380857210937" }, { "input": "0 9756 5922\n9233 0 8371\n6826 8020 0\n40", "output": "8928156585485415" }, { "input": "0 1177 7722\n4285 0 8901\n3880 8549 0\n40", "output": "5921725291311720" }, { "input": "0 3792 500\n1183 0 3169\n1357 9914 0\n40", "output": "3176855596478157" }, { "input": "0 7600 9420\n2996 0 974\n2995 3111 0\n39", "output": "2526066880932431" }, { "input": "0 65 3859\n6032 0 555\n6731 9490 0\n38", "output": "1040962633462383" }, { "input": "0 3341 2142\n452 0 4434\n241 8379 0\n38", "output": "740490539331253" }, { "input": "0 2975 131\n4408 0 8557\n7519 8541 0\n40", "output": "5516494172354496" }, { "input": "0 5638 2109\n3346 0 1684\n2770 8831 0\n40", "output": "4211129534337070" }, { "input": "0 649 576\n2780 0 6415\n7629 1233 0\n38", "output": "731862427166001" }, { "input": "0 5222 6817\n8403 0 6167\n2424 2250 0\n39", "output": "2978027243887585" }, { "input": "0 9628 4599\n6755 0 5302\n5753 1995 0\n39", "output": "2894220024221629" }, { "input": "0 9358 745\n7093 0 7048\n1767 5267 0\n39", "output": "2711090254202573" }, { "input": "0 4405 3533\n8676 0 3288\n1058 5977 0\n38", "output": "1089982526985246" }, { "input": "0 1096 1637\n5625 0 4364\n8026 7246 0\n39", "output": "2596191288960748" }, { "input": "0 8494 3561\n8215 0 9313\n1980 9423 0\n39", "output": "3798507254080314" }, { "input": "0 3461 4834\n1096 0 3259\n8158 3363 0\n40", "output": "4092687698447757" }, { "input": "0 2986 6350\n59 0 9863\n8674 1704 0\n40", "output": "4350584259212361" }, { "input": "0 7829 1008\n2914 0 2636\n4439 8654 0\n39", "output": "2446779875619187" }, { "input": "0 6991 1482\n1274 0 6332\n7588 5049 0\n38", "output": "1272852690054827" }, { "input": "0 4499 7885\n6089 0 8400\n8724 2588 0\n40", "output": "7298008429373546" }, { "input": "0 9965 5863\n5956 0 3340\n9497 5040 0\n38", "output": "1907744300121994" }, { "input": "0 5125 2904\n763 0 4213\n4171 3367 0\n38", "output": "1013123610034430" }, { "input": "0 328 3888\n3730 0 760\n9382 6574 0\n38", "output": "1073001180618872" }, { "input": "0 6082 5094\n2704 0 991\n7522 6411 0\n40", "output": "5301967237043705" }, { "input": "0 655 1599\n4254 0 7484\n3983 9099 0\n39", "output": "2158371867244476" }, { "input": "0 1099 3412\n9261 0 3868\n758 8489 0\n38", "output": "975259178289234" }, { "input": "0 8246 1436\n8823 0 5285\n8283 7277 0\n39", "output": "3474823247533881" }, { "input": "0 1446 2980\n9298 0 9679\n7865 6963 0\n38", "output": "1747643190259529" }, { "input": "0 10000 10000\n10000 0 10000\n10000 10000 0\n40", "output": "10995116277750000" }, { "input": "0 1 1\n1 0 1\n1 1 0\n1", "output": "1" }, { "input": "0 1 10\n1 0 1\n10 1 0\n1", "output": "2" }, { "input": "0 1 10\n1 0 1\n10 1 0\n1", "output": "2" }, { "input": "0 1 100\n1 0 1\n100 1 0\n1", "output": "2" } ]
140
0
3
19,234
526
Om Nom and Dark Park
[ "dfs and similar", "greedy", "implementation" ]
null
null
Om Nom is the main character of a game "Cut the Rope". He is a bright little monster who likes visiting friends living at the other side of the park. However the dark old parks can scare even somebody as fearless as Om Nom, so he asks you to help him. The park consists of 2*n*<=+<=1<=-<=1 squares connected by roads so that the scheme of the park is a full binary tree of depth *n*. More formally, the entrance to the park is located at the square 1. The exits out of the park are located at squares 2*n*,<=2*n*<=+<=1,<=...,<=2*n*<=+<=1<=-<=1 and these exits lead straight to the Om Nom friends' houses. From each square *i* (2<=≤<=*i*<=&lt;<=2*n*<=+<=1) there is a road to the square . Thus, it is possible to go from the park entrance to each of the exits by walking along exactly *n* roads. Om Nom loves counting lights on the way to his friend. Om Nom is afraid of spiders who live in the park, so he doesn't like to walk along roads that are not enough lit. What he wants is that the way to any of his friends should have in total the same number of lights. That will make him feel safe. He asked you to help him install additional lights. Determine what minimum number of lights it is needed to additionally place on the park roads so that a path from the entrance to any exit of the park contains the same number of street lights. You may add an arbitrary number of street lights to each of the roads.
The first line contains integer *n* (1<=≤<=*n*<=≤<=10) — the number of roads on the path from the entrance to any exit. The next line contains 2*n*<=+<=1<=-<=2 numbers *a*2,<=*a*3,<=... *a*2*n*<=+<=1<=-<=1 — the initial numbers of street lights on each road of the park. Here *a**i* is the number of street lights on the road between squares *i* and . All numbers *a**i* are positive integers, not exceeding 100.
Print the minimum number of street lights that we should add to the roads of the park to make Om Nom feel safe.
[ "2\n1 2 3 4 5 6\n" ]
[ "5\n" ]
Picture for the sample test. Green color denotes the additional street lights.
[ { "input": "2\n1 2 3 4 5 6", "output": "5" }, { "input": "2\n1 2 3 3 2 2", "output": "0" }, { "input": "1\n39 52", "output": "13" }, { "input": "2\n59 96 34 48 8 72", "output": "139" }, { "input": "3\n87 37 91 29 58 45 51 74 70 71 47 38 91 89", "output": "210" }, { "input": "5\n39 21 95 89 73 90 9 55 85 32 30 21 68 59 82 91 20 64 52 70 6 88 53 47 30 47 34 14 11 22 42 15 28 54 37 48 29 3 14 13 18 77 90 58 54 38 94 49 45 66 13 74 11 14 64 72 95 54 73 79 41 35", "output": "974" }, { "input": "1\n49 36", "output": "13" }, { "input": "1\n77 88", "output": "11" }, { "input": "1\n1 33", "output": "32" }, { "input": "2\n72 22 81 23 14 75", "output": "175" }, { "input": "2\n100 70 27 1 68 52", "output": "53" }, { "input": "2\n24 19 89 82 22 21", "output": "80" }, { "input": "3\n86 12 92 91 3 68 57 56 76 27 33 62 71 84", "output": "286" }, { "input": "3\n14 56 53 61 57 45 40 44 31 9 73 2 61 26", "output": "236" }, { "input": "3\n35 96 7 43 10 14 16 36 95 92 16 50 59 55", "output": "173" }, { "input": "4\n1 97 18 48 96 65 24 91 17 45 36 27 74 93 78 86 39 55 53 21 26 68 31 33 79 63 80 92 1 26", "output": "511" }, { "input": "4\n25 42 71 29 50 30 99 79 77 24 76 66 68 23 97 99 65 17 75 62 66 46 48 4 40 71 98 57 21 92", "output": "603" }, { "input": "4\n49 86 17 7 3 6 86 71 36 10 27 10 58 64 12 16 88 67 93 3 15 20 58 87 97 91 11 6 34 62", "output": "470" }, { "input": "5\n16 87 36 16 81 53 87 35 63 56 47 91 81 95 80 96 91 7 58 99 25 28 47 60 7 69 49 14 51 52 29 30 83 23 21 52 100 26 91 14 23 94 72 70 40 12 50 32 54 52 18 74 5 15 62 3 48 41 24 25 56 43", "output": "1060" }, { "input": "5\n40 27 82 94 38 22 66 23 18 34 87 31 71 28 95 5 14 61 76 52 66 6 60 40 68 77 70 63 64 18 47 13 82 55 34 64 30 1 29 24 24 9 65 17 29 96 61 76 72 23 32 26 90 39 54 41 35 66 71 29 75 48", "output": "1063" }, { "input": "5\n64 72 35 68 92 95 45 15 77 16 26 74 61 65 18 22 32 19 98 97 14 84 70 23 29 1 87 28 88 89 73 79 69 88 43 60 64 64 66 39 17 27 46 71 18 83 73 20 90 77 49 70 84 63 50 72 26 87 26 37 78 65", "output": "987" }, { "input": "6\n35 61 54 77 70 50 53 70 4 66 58 47 76 100 78 5 43 50 55 93 13 93 59 92 30 74 22 23 98 70 19 56 90 92 19 7 28 53 45 77 42 91 71 56 19 83 100 53 13 93 37 13 70 60 16 13 76 3 12 22 17 26 50 6 63 7 25 41 92 29 36 80 11 4 10 14 77 75 53 82 46 24 56 46 82 36 80 75 8 45 24 22 90 34 45 76 18 38 86 43 7 49 80 56 90 53 12 51 98 47 44 58 32 4 2 6 3 60 38 72 74 46 30 86 1 98", "output": "2499" }, { "input": "6\n63 13 100 54 31 15 29 58 59 44 2 99 70 33 97 14 70 12 73 42 65 71 68 67 87 83 43 84 18 41 37 22 81 24 27 11 57 28 83 92 39 1 56 15 16 67 16 97 31 52 50 65 63 89 8 52 55 20 71 27 28 35 86 92 94 60 10 65 83 63 89 71 34 20 78 40 34 62 2 86 100 81 87 69 25 4 52 17 57 71 62 38 1 3 54 71 34 85 20 60 80 23 82 47 4 19 7 18 14 18 28 27 4 55 26 71 45 9 2 40 67 28 32 19 81 92", "output": "2465" }, { "input": "6\n87 62 58 32 81 92 12 50 23 27 38 39 64 74 16 35 84 59 91 87 14 48 90 47 44 95 64 45 31 11 67 5 80 60 36 15 91 3 21 2 40 24 37 69 5 50 23 37 49 19 68 21 49 9 100 94 45 41 22 31 31 48 25 70 25 25 95 88 82 1 37 53 49 31 57 74 94 45 55 93 43 37 13 85 59 72 15 68 3 90 96 55 100 64 63 69 43 33 66 84 57 97 87 34 23 89 97 77 39 89 8 92 68 13 50 36 95 61 71 96 73 13 30 49 57 89", "output": "2513" } ]
46
0
-1
19,255
45
Dancing Lessons
[ "data structures" ]
C. Dancing Lessons
2
256
There are *n* people taking dancing lessons. Every person is characterized by his/her dancing skill *a**i*. At the beginning of the lesson they line up from left to right. While there is at least one couple of a boy and a girl in the line, the following process is repeated: the boy and girl who stand next to each other, having the minimal difference in dancing skills start to dance. If there are several such couples, the one first from the left starts to dance. After a couple leaves to dance, the line closes again, i.e. as a result the line is always continuous. The difference in dancing skills is understood as the absolute value of difference of *a**i* variable. Your task is to find out what pairs and in what order will start dancing.
The first line contains an integer *n* (1<=≤<=*n*<=≤<=2·105) — the number of people. The next line contains *n* symbols B or G without spaces. B stands for a boy, G stands for a girl. The third line contains *n* space-separated integers *a**i* (1<=≤<=*a**i*<=≤<=107) — the dancing skill. People are specified from left to right in the order in which they lined up.
Print the resulting number of couples *k*. Then print *k* lines containing two numerals each — the numbers of people forming the couple. The people are numbered with integers from 1 to *n* from left to right. When a couple leaves to dance you shouldn't renumber the people. The numbers in one couple should be sorted in the increasing order. Print the couples in the order in which they leave to dance.
[ "4\nBGBG\n4 2 4 3\n", "4\nBBGG\n4 6 1 5\n", "4\nBGBB\n1 1 2 3\n" ]
[ "2\n3 4\n1 2\n", "2\n2 3\n1 4\n", "1\n1 2\n" ]
none
[ { "input": "4\nBGBG\n4 2 4 3", "output": "2\n3 4\n1 2" }, { "input": "4\nBBGG\n4 6 1 5", "output": "2\n2 3\n1 4" }, { "input": "4\nBGBB\n1 1 2 3", "output": "1\n1 2" }, { "input": "1\nB\n490297", "output": "0" }, { "input": "2\nBB\n2518190 6313112", "output": "0" }, { "input": "3\nBBB\n3146681 8599398 1571560", "output": "0" }, { "input": "10\nGBBBBGBBGG\n9448959 6234076 8008159 4440374 4736443 255932 6568554 4544108 54187 1105310", "output": "4\n1 2\n5 6\n8 9\n7 10" }, { "input": "10\nBGGBBGBGBG\n9892816 3514007 5425956 5241945 9171176 3351177 2772494 2891569 1510552 8471969", "output": "5\n7 8\n3 4\n6 9\n5 10\n1 2" }, { "input": "10\nGGGGBGGGBB\n2853026 3310290 2843753 8559868 1089557 6446421 8976433 3755381 2966918 3322276", "output": "3\n8 9\n5 6\n7 10" }, { "input": "10\nGBBGBBBGBB\n9251505 1388373 4869655 8551146 5931052 358636 416134 8756343 8254852 661072", "output": "3\n8 9\n4 5\n1 2" } ]
0
0
-1
19,278
696
PLEASE
[ "combinatorics", "dp", "implementation", "math", "matrices" ]
null
null
As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup. Then at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts *n* turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start. After *n*-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right. Number *n* of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array *a*1,<=*a*2,<=...,<=*a**k* such that in other words, *n* is multiplication of all elements of the given array. Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction *p*<=/<=*q* such that , where is the greatest common divisor. Since *p* and *q* can be extremely large, you only need to find the remainders of dividing each of them by 109<=+<=7. Please note that we want of *p* and *q* to be 1, not of their remainders after dividing by 109<=+<=7.
The first line of input contains a single integer *k* (1<=≤<=*k*<=≤<=105) — the number of elements in array Barney gave you. The second line contains *k* integers *a*1,<=*a*2,<=...,<=*a**k* (1<=≤<=*a**i*<=≤<=1018) — the elements of the array.
In the only line of output print a single string *x*<=/<=*y* where *x* is the remainder of dividing *p* by 109<=+<=7 and *y* is the remainder of dividing *q* by 109<=+<=7.
[ "1\n2\n", "3\n1 1 1\n" ]
[ "1/2\n", "0/1\n" ]
none
[ { "input": "1\n2", "output": "1/2" }, { "input": "3\n1 1 1", "output": "0/1" }, { "input": "1\n983155795040951739", "output": "145599903/436799710" }, { "input": "2\n467131402341701583 956277077729692725", "output": "63467752/190403257" }, { "input": "10\n217673221404542171 806579927281665969 500754531396239406 214319484250163112 328494187336342674 427465830578952934 951554014286436941 664022909283791499 653206814724654845 66704816231807388", "output": "896298678/688896019" }, { "input": "8\n137264686188377169 524477139880847337 939966121107073137 244138018261712937 158070587508987781 35608416591331673 378899027510195451 81986819972451999", "output": "993002178/979006521" }, { "input": "9\n174496219779575399 193634487267697117 972518022554199573 695317701399937273 464007855398119159 881020180696239657 296973121744507377 544232692627163469 751214074246742731", "output": "149736910/449210731" }, { "input": "12\n254904759290707697 475737283258450340 533306428548398547 442127134828578937 779740159015946254 272877594683860919 93000149670491971 349640818793278778 498293278222136720 551099726729989816 149940343283925029 989425634209891686", "output": "674872752/24618241" }, { "input": "1\n1", "output": "0/1" }, { "input": "1\n1000000000000000000", "output": "453246046/359738130" }, { "input": "1\n3", "output": "1/4" }, { "input": "1\n1000000006", "output": "500000004/500000004" }, { "input": "2\n500000004 1000000006", "output": "500000004/500000004" }, { "input": "1\n500000004", "output": "666666672/1" }, { "input": "2\n500000004 500000004", "output": "666666672/1" }, { "input": "1\n500000003", "output": "833333339/500000004" }, { "input": "2\n500000003 500000004", "output": "500000004/500000004" }, { "input": "2\n500000003 500000003", "output": "833333339/500000004" }, { "input": "1\n1000000005", "output": "750000005/250000002" }, { "input": "2\n1000000005 500000004", "output": "416666670/250000002" } ]
1,000
15,769,600
0
19,295
282
Painting Eggs
[ "greedy", "math" ]
null
null
The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have *n* eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work. The kids are excited because just as is customary, they're going to be paid for the job! Overall uncle J. has got *n* eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000. Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500. Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible.
The first line contains integer *n* (1<=≤<=*n*<=≤<=106) — the number of eggs. Next *n* lines contain two integers *a**i* and *g**i* each (0<=≤<=*a**i*,<=*g**i*<=≤<=1000; *a**i*<=+<=*g**i*<==<=1000): *a**i* is the price said by A. for the *i*-th egg and *g**i* is the price said by G. for the *i*-th egg.
If it is impossible to assign the painting, print "-1" (without quotes). Otherwise print a string, consisting of *n* letters "G" and "A". The *i*-th letter of this string should represent the child who will get the *i*-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as *S**a*, and the money Uncle J. must pay G. for the painting as *S**g*, then this inequality must hold: |*S**a*<=<=-<=<=*S**g*|<=<=≤<=<=500. If there are several solutions, you are allowed to print any of them.
[ "2\n1 999\n999 1\n", "3\n400 600\n400 600\n400 600\n" ]
[ "AG\n", "AGA\n" ]
none
[ { "input": "2\n1 999\n999 1", "output": "AG" }, { "input": "3\n400 600\n400 600\n400 600", "output": "AGA" }, { "input": "2\n500 500\n500 500", "output": "AG" }, { "input": "1\n1 999", "output": "A" }, { "input": "10\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999\n1 999", "output": "AAAAAAAAAA" }, { "input": "2\n499 501\n501 499", "output": "AG" }, { "input": "3\n500 500\n1 999\n400 600", "output": "AGA" }, { "input": "1\n0 1000", "output": "A" }, { "input": "1\n500 500", "output": "A" }, { "input": "1\n1000 0", "output": "G" } ]
62
0
0
19,304
696
Lorenzo Von Matterhorn
[ "brute force", "data structures", "implementation", "trees" ]
null
null
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections *i* and 2*i* and another road between *i* and 2*i*<=+<=1 for every positive integer *i*. You can clearly see that there exists a unique shortest path between any two intersections. Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will *q* consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers *v*, *u* and *w*. As the result of this action, the passing fee of all roads on the shortest path from *u* to *v* increases by *w* dollars. 2. Barney starts moving from some intersection *v* and goes to intersection *u* where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
The first line of input contains a single integer *q* (1<=≤<=*q*<=≤<=1<=000). The next *q* lines contain the information about the events in chronological order. Each event is described in form 1 *v* *u* *w* if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from *u* to *v* by *w* dollars, or in form 2 *v* *u* if it's an event when Barnie goes to cuddle from the intersection *v* to the intersection *u*. 1<=≤<=*v*,<=*u*<=≤<=1018,<=*v*<=≠<=*u*,<=1<=≤<=*w*<=≤<=109 states for every description line.
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
[ "7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4\n" ]
[ "94\n0\n32\n" ]
In the example testcase: Here are the intersections used: 1. Intersections on the path are 3, 1, 2 and 4. 1. Intersections on the path are 4, 2 and 1. 1. Intersections on the path are only 3 and 6. 1. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. 1. Intersections on the path are 6, 3 and 1. 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 1. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
[ { "input": "7\n1 3 4 30\n1 4 1 2\n1 3 6 8\n2 4 3\n1 6 1 40\n2 3 7\n2 2 4", "output": "94\n0\n32" }, { "input": "1\n2 666077344481199252 881371880336470888", "output": "0" }, { "input": "10\n1 1 63669439577744021 396980128\n1 2582240553355225 63669439577744021 997926286\n1 2582240553355225 1 619026011\n1 1 4 231881718\n2 63669439577744021 3886074192977\n2 4 63669439577744021\n2 124354374175272 10328962213420903\n1 10328962213420903 3886074192977 188186816\n1 124354374175272 31088593543820 705639304\n2 2582240553355225 254677758310976084", "output": "19528689796\n80417520800\n140119493557\n179078288337" }, { "input": "10\n1 1 399719082491 159376944\n1 186 1 699740230\n2 410731850987390 1\n1 410731850987390 399719082491 699271234\n1 1 186 255736462\n1 1 186 544477714\n1 399719082491 410731850987390 366708275\n2 1 186\n2 410731850987390 1\n2 399719082491 186", "output": "6013820218\n11615319450\n55320479319\n37986050043" }, { "input": "10\n2 37526406560905229 37526426361107171\n2 37526424114740747 18763396439955441\n2 300485276957081578 301492476099962199\n1 75035386466351570 441803674395985082 642312512\n2 300197522144700185 220954108245114486\n1 150105696341181576 559187296 100113944\n1 300197522135707767 150242638470761995 170574370\n2 150105691058036871 220954108245108400\n2 37560659619635168 150070774425697078\n2 18780329809814344 300222324900057526", "output": "0\n0\n0\n13488562752\n14270974176\n13899046930\n5418394872" }, { "input": "1\n2 1 343417335313797025", "output": "0" }, { "input": "2\n1 562949953421312 562949953421311 1\n2 562949953421312 562949953421311", "output": "97" }, { "input": "2\n1 100 50 1\n2 4294967396 1", "output": "0" }, { "input": "2\n1 4294967298 4294967299 10\n2 2 3", "output": "0" }, { "input": "2\n1 500000000000 250000000000 1\n2 1783793664 891896832", "output": "0" }, { "input": "2\n1 100000000000000 200000000000000 1\n2 276447232 552894464", "output": "0" }, { "input": "2\n1 2147540141 4295080282 1\n2 1 112986", "output": "0" }, { "input": "2\n1 239841676148963 1 20\n2 2112405731 1", "output": "20" } ]
296
8,192,000
3
19,313
915
Imbalance Value of a Tree
[ "data structures", "dsu", "graphs", "trees" ]
null
null
You are given a tree *T* consisting of *n* vertices. A number is written on each vertex; the number written on vertex *i* is *a**i*. Let's denote the function *I*(*x*,<=*y*) as the difference between maximum and minimum value of *a**i* on a simple path connecting vertices *x* and *y*. Your task is to calculate .
The first line contains one integer number *n* (1<=≤<=*n*<=≤<=106) — the number of vertices in the tree. The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=106) — the numbers written on the vertices. Then *n*<=-<=1 lines follow. Each line contains two integers *x* and *y* denoting an edge connecting vertex *x* and vertex *y* (1<=≤<=*x*,<=*y*<=≤<=*n*, *x*<=≠<=*y*). It is guaranteed that these edges denote a tree.
Print one number equal to .
[ "4\n2 2 3 1\n1 2\n1 3\n1 4\n" ]
[ "6\n" ]
none
[ { "input": "4\n2 2 3 1\n1 2\n1 3\n1 4", "output": "6" } ]
77
0
0
19,314
85
Domino
[ "constructive algorithms", "implementation" ]
A. Domino
1
256
We all know the problem about the number of ways one can tile a 2<=×<=*n* field by 1<=×<=2 dominoes. You probably remember that it goes down to Fibonacci numbers. We will talk about some other problem below, there you also are going to deal with tiling a rectangular field with dominoes. You are given a 4<=×<=*n* rectangular field, that is the field that contains four lines and *n* columns. You have to find for it any tiling by 1<=×<=2 dominoes such that each of the *n*<=-<=1 potential vertical cuts along the grid lines intersects at least one domino, splitting it in two. No two dominoes in the sought tiling should overlap, each square of the field should be covered by exactly one domino. It is allowed to rotate the dominoes, that is, you can use 2<=×<=1 as well as 1<=×<=2 dominoes. Write a program that finds an arbitrary sought tiling.
The input contains one positive integer *n* (1<=≤<=*n*<=≤<=100) — the number of the field's columns.
If there's no solution, print "-1" (without the quotes). Otherwise, print four lines containing *n* characters each — that's the description of tiling, where each vertical cut intersects at least one domino. You should print the tiling, having painted the field in no more than 26 colors. Each domino should be painted a color. Different dominoes can be painted the same color, but dominoes of the same color should not be side-neighbouring. To indicate colors you should use lowercase Latin letters. Print any of the acceptable ways of tiling.
[ "4\n" ]
[ "yyzz\nbccd\nbxxd\nyyaa\n" ]
none
[ { "input": "4", "output": "aacc\nbbdd\nzkkz\nzllz" }, { "input": "2", "output": "aa\nbb\naa\nbb" }, { "input": "3", "output": "aab\nccb\nbaa\nbcc" }, { "input": "5", "output": "aaccz\nbbddz\nzkkmm\nzllnn" }, { "input": "1", "output": "a\na\nb\nb" }, { "input": "6", "output": "aaccee\nbbddff\nzkkmmz\nzllnnz" }, { "input": "7", "output": "aacceez\nbbddffz\nzkkmmoo\nzllnnpp" }, { "input": "8", "output": "aacceegg\nbbddffhh\nzkkmmooz\nzllnnppz" }, { "input": "9", "output": "aacceeggz\nbbddffhhz\nzkkmmooqq\nzllnnpprr" }, { "input": "10", "output": "aacceeggii\nbbddffhhjj\nzkkmmooqqz\nzllnnpprrz" }, { "input": "11", "output": "aacceeggiiz\nbbddffhhjjz\nzkkmmooqqss\nzllnnpprrtt" }, { "input": "12", "output": "aacceeggiiaa\nbbddffhhjjbb\nzkkmmooqqssz\nzllnnpprrttz" }, { "input": "13", "output": "aacceeggiiaaz\nbbddffhhjjbbz\nzkkmmooqqsskk\nzllnnpprrttll" }, { "input": "14", "output": "aacceeggiiaacc\nbbddffhhjjbbdd\nzkkmmooqqsskkz\nzllnnpprrttllz" }, { "input": "15", "output": "aacceeggiiaaccz\nbbddffhhjjbbddz\nzkkmmooqqsskkmm\nzllnnpprrttllnn" }, { "input": "16", "output": "aacceeggiiaaccee\nbbddffhhjjbbddff\nzkkmmooqqsskkmmz\nzllnnpprrttllnnz" }, { "input": "17", "output": "aacceeggiiaacceez\nbbddffhhjjbbddffz\nzkkmmooqqsskkmmoo\nzllnnpprrttllnnpp" }, { "input": "18", "output": "aacceeggiiaacceegg\nbbddffhhjjbbddffhh\nzkkmmooqqsskkmmooz\nzllnnpprrttllnnppz" }, { "input": "19", "output": "aacceeggiiaacceeggz\nbbddffhhjjbbddffhhz\nzkkmmooqqsskkmmooqq\nzllnnpprrttllnnpprr" }, { "input": "20", "output": "aacceeggiiaacceeggii\nbbddffhhjjbbddffhhjj\nzkkmmooqqsskkmmooqqz\nzllnnpprrttllnnpprrz" }, { "input": "21", "output": "aacceeggiiaacceeggiiz\nbbddffhhjjbbddffhhjjz\nzkkmmooqqsskkmmooqqss\nzllnnpprrttllnnpprrtt" }, { "input": "22", "output": "aacceeggiiaacceeggiiaa\nbbddffhhjjbbddffhhjjbb\nzkkmmooqqsskkmmooqqssz\nzllnnpprrttllnnpprrttz" }, { "input": "23", "output": "aacceeggiiaacceeggiiaaz\nbbddffhhjjbbddffhhjjbbz\nzkkmmooqqsskkmmooqqsskk\nzllnnpprrttllnnpprrttll" }, { "input": "24", "output": "aacceeggiiaacceeggiiaacc\nbbddffhhjjbbddffhhjjbbdd\nzkkmmooqqsskkmmooqqsskkz\nzllnnpprrttllnnpprrttllz" }, { "input": "25", "output": "aacceeggiiaacceeggiiaaccz\nbbddffhhjjbbddffhhjjbbddz\nzkkmmooqqsskkmmooqqsskkmm\nzllnnpprrttllnnpprrttllnn" }, { "input": "26", "output": "aacceeggiiaacceeggiiaaccee\nbbddffhhjjbbddffhhjjbbddff\nzkkmmooqqsskkmmooqqsskkmmz\nzllnnpprrttllnnpprrttllnnz" }, { "input": "27", "output": "aacceeggiiaacceeggiiaacceez\nbbddffhhjjbbddffhhjjbbddffz\nzkkmmooqqsskkmmooqqsskkmmoo\nzllnnpprrttllnnpprrttllnnpp" }, { "input": "28", "output": "aacceeggiiaacceeggiiaacceegg\nbbddffhhjjbbddffhhjjbbddffhh\nzkkmmooqqsskkmmooqqsskkmmooz\nzllnnpprrttllnnpprrttllnnppz" }, { "input": "29", "output": "aacceeggiiaacceeggiiaacceeggz\nbbddffhhjjbbddffhhjjbbddffhhz\nzkkmmooqqsskkmmooqqsskkmmooqq\nzllnnpprrttllnnpprrttllnnpprr" }, { "input": "30", "output": "aacceeggiiaacceeggiiaacceeggii\nbbddffhhjjbbddffhhjjbbddffhhjj\nzkkmmooqqsskkmmooqqsskkmmooqqz\nzllnnpprrttllnnpprrttllnnpprrz" }, { "input": "91", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiz\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjz\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqss\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrtt" }, { "input": "92", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaa\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbb\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqssz\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttz" }, { "input": "93", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaaz\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbz\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskk\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttll" }, { "input": "94", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacc\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbdd\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkz\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllz" }, { "input": "95", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaaccz\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddz\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmm\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnn" }, { "input": "96", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaaccee\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddff\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmz\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnz" }, { "input": "97", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceez\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffz\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmoo\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpp" }, { "input": "98", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceegg\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhh\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooz\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnppz" }, { "input": "99", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggz\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhz\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqq\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprr" }, { "input": "100", "output": "aacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggiiaacceeggii\nbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjjbbddffhhjj\nzkkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqsskkmmooqqz\nzllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrttllnnpprrz" } ]
1,000
33,792,000
0
19,337
581
Developing Skills
[ "implementation", "math", "sortings" ]
null
null
Petya loves computer games. Finally a game that he's been waiting for so long came out! The main character of this game has *n* different skills, each of which is characterized by an integer *a**i* from 0 to 100. The higher the number *a**i* is, the higher is the *i*-th skill of the character. The total rating of the character is calculated as the sum of the values ​​of for all *i* from 1 to *n*. The expression ⌊ *x*⌋ denotes the result of rounding the number *x* down to the nearest integer. At the beginning of the game Petya got *k* improvement units as a bonus that he can use to increase the skills of his character and his total rating. One improvement unit can increase any skill of Petya's character by exactly one. For example, if *a*4<==<=46, after using one imporvement unit to this skill, it becomes equal to 47. A hero's skill cannot rise higher more than 100. Thus, it is permissible that some of the units will remain unused. Your task is to determine the optimal way of using the improvement units so as to maximize the overall rating of the character. It is not necessary to use all the improvement units.
The first line of the input contains two positive integers *n* and *k* (1<=≤<=*n*<=≤<=105, 0<=≤<=*k*<=≤<=107) — the number of skills of the character and the number of units of improvements at Petya's disposal. The second line of the input contains a sequence of *n* integers *a**i* (0<=≤<=*a**i*<=≤<=100), where *a**i* characterizes the level of the *i*-th skill of the character.
The first line of the output should contain a single non-negative integer — the maximum total rating of the character that Petya can get using *k* or less improvement units.
[ "2 4\n7 9\n", "3 8\n17 15 19\n", "2 2\n99 100\n" ]
[ "2\n", "5\n", "20\n" ]
In the first test case the optimal strategy is as follows. Petya has to improve the first skill to 10 by spending 3 improvement units, and the second skill to 10, by spending one improvement unit. Thus, Petya spends all his improvement units and the total rating of the character becomes equal to *lfloor* *frac*{100}{10} *rfloor* +  *lfloor* *frac*{100}{10} *rfloor* = 10 + 10 =  20. In the second test the optimal strategy for Petya is to improve the first skill to 20 (by spending 3 improvement units) and to improve the third skill to 20 (in this case by spending 1 improvement units). Thus, Petya is left with 4 improvement units and he will be able to increase the second skill to 19 (which does not change the overall rating, so Petya does not necessarily have to do it). Therefore, the highest possible total rating in this example is <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ccaa4e1e435ea3a339c322e03a32de69d214a257.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In the third test case the optimal strategy for Petya is to increase the first skill to 100 by spending 1 improvement unit. Thereafter, both skills of the character will be equal to 100, so Petya will not be able to spend the remaining improvement unit. So the answer is equal to <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b246630ca7d1b95b91970759bd8455cb3e930bf9.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
[ { "input": "2 4\n7 9", "output": "2" }, { "input": "3 8\n17 15 19", "output": "5" }, { "input": "2 2\n99 100", "output": "20" }, { "input": "100 10000\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0", "output": "1000" }, { "input": "100 10000\n100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100 100", "output": "1000" }, { "input": "1 16\n78", "output": "9" }, { "input": "2 33\n30 88", "output": "15" }, { "input": "3 9\n93 62 7", "output": "16" }, { "input": "5 145\n19 77 59 1 63", "output": "36" }, { "input": "7 168\n2 71 56 58 42 61 39", "output": "49" }, { "input": "10 217\n48 30 82 70 10 5 34 11 90 90", "output": "68" }, { "input": "15 204\n19 81 24 22 59 46 48 8 1 66 100 20 46 56 61", "output": "86" }, { "input": "20 484\n24 72 72 13 85 50 52 3 81 79 71 57 57 75 6 52 54 41 61 73", "output": "156" }, { "input": "30 825\n33 25 61 69 92 38 2 62 73 78 83 32 25 5 5 82 64 93 38 25 52 9 40 52 38 90 25 85 99 20", "output": "232" }, { "input": "40 700\n43 35 51 91 44 51 86 20 64 10 50 40 16 25 37 89 18 44 94 99 18 30 11 27 73 3 90 78 28 98 87 43 85 88 29 93 6 81 78 16", "output": "276" }, { "input": "50 1607\n19 55 52 35 18 39 3 12 55 78 62 83 85 56 36 86 96 28 70 40 40 83 27 2 51 49 87 28 58 75 27 69 36 82 78 29 99 87 29 78 82 78 15 85 52 32 90 6 1 76", "output": "424" }, { "input": "60 2213\n17 98 74 91 59 84 87 71 13 9 74 48 75 76 36 25 49 80 25 92 41 24 99 45 98 95 27 54 88 63 25 50 19 43 15 90 58 48 58 83 37 88 35 63 63 23 27 82 80 7 82 93 71 18 85 17 13 2 50 74", "output": "552" }, { "input": "70 1313\n27 7 64 45 44 29 37 63 38 9 85 56 43 74 46 55 59 97 13 33 75 78 2 88 32 7 24 36 86 40 66 42 26 48 64 14 50 21 20 10 50 73 21 29 17 46 97 90 81 73 61 25 95 82 93 94 72 38 80 13 3 3 20 90 34 20 24 49 96 51", "output": "468" }, { "input": "40 108\n20 100 99 50 8 78 44 67 91 75 93 53 96 81 96 86 81 0 58 9 51 63 70 73 80 79 28 82 4 15 60 74 19 17 54 81 11 67 71 66", "output": "245" }, { "input": "50 284\n61 25 82 73 57 61 90 22 63 99 58 4 27 54 8 29 46 99 73 73 60 42 45 17 75 86 38 83 4 1 67 44 74 87 32 33 14 95 87 46 40 3 37 6 42 38 51 39 98 48", "output": "282" }, { "input": "60 1947\n46 29 55 97 37 32 24 22 35 66 24 78 92 5 55 41 21 30 88 24 13 89 77 30 71 15 58 26 39 10 42 36 28 66 21 28 51 55 91 4 94 59 63 46 1 39 46 1 70 7 46 37 96 41 70 19 55 80 59 83", "output": "471" }, { "input": "70 2454\n88 23 5 86 53 48 60 78 97 90 0 18 57 78 68 28 87 39 70 9 0 35 18 53 67 56 0 71 7 86 39 96 83 45 99 92 43 38 40 63 81 59 89 86 28 62 53 97 53 2 73 93 38 49 51 62 93 3 63 49 47 85 72 98 43 91 7 20 47 66", "output": "632" }, { "input": "80 1879\n36 27 86 90 18 85 99 54 29 8 64 31 34 26 45 51 13 48 58 6 98 30 74 63 78 53 88 98 15 17 29 67 78 8 2 7 42 26 72 83 5 59 8 7 27 59 34 65 93 71 50 34 63 45 21 81 19 30 99 41 25 11 83 62 17 29 80 61 91 22 19 95 80 73 15 39 10 37 88 42", "output": "570" }, { "input": "90 1191\n46 37 76 11 60 29 49 13 88 41 65 7 2 13 44 58 23 10 45 48 63 83 79 5 89 99 28 80 34 6 37 92 61 70 51 0 34 67 68 77 62 69 27 86 71 83 72 73 93 92 62 68 86 76 28 24 67 66 61 12 3 52 45 44 58 83 0 84 18 50 75 51 41 25 21 53 39 20 36 45 62 24 12 33 61 81 9 13 27 22", "output": "554" }, { "input": "100 1257\n80 15 39 54 98 10 65 77 55 98 15 25 78 40 25 16 17 60 25 60 56 29 91 16 14 60 47 31 15 59 83 77 10 54 27 21 50 34 64 69 43 81 32 14 30 93 0 91 75 51 19 84 88 14 30 4 99 59 94 69 24 51 35 99 22 25 41 77 64 97 10 4 56 75 97 54 4 55 29 8 14 16 88 34 80 47 66 30 80 60 45 45 93 85 49 91 37 16 49 56", "output": "619" }, { "input": "100 3852\n71 34 1 77 97 36 66 78 95 47 47 15 50 100 43 47 20 23 61 92 49 86 29 92 100 85 5 58 59 19 16 81 16 89 93 75 46 86 9 50 9 49 61 88 76 13 14 99 47 64 39 42 63 5 57 8 51 21 21 62 92 84 84 56 9 37 72 19 99 19 8 60 25 21 4 0 98 80 29 63 52 87 91 30 79 79 96 22 32 63 87 73 51 89 81 84 69 30 55 31", "output": "922" }, { "input": "100 2533\n16 32 22 100 52 10 43 28 87 72 69 84 26 0 74 46 28 34 46 47 90 18 49 6 42 30 18 33 86 38 94 78 8 39 54 46 72 45 83 68 38 4 14 6 86 24 71 36 22 8 37 99 28 7 88 49 4 69 46 81 30 95 92 18 81 21 14 7 43 14 80 59 14 72 93 6 78 43 56 12 66 21 81 80 39 5 54 69 40 12 41 35 23 58 1 75 40 3 36 97", "output": "706" }, { "input": "100 2239\n95 9 31 56 96 85 88 79 78 63 68 95 1 91 94 56 57 88 30 92 64 52 91 11 17 99 65 63 35 68 82 18 66 57 26 62 32 70 89 98 42 17 68 93 53 79 50 6 30 76 69 10 4 41 18 56 81 49 14 10 91 6 32 80 85 94 2 95 66 9 18 58 71 23 23 48 68 72 39 51 0 23 71 73 10 89 13 15 16 30 27 44 63 93 22 77 12 12 28 5", "output": "737" }, { "input": "100 1689\n40 18 85 79 18 70 44 62 37 21 68 6 9 60 13 55 98 98 82 80 4 75 44 83 60 44 10 60 28 65 59 82 48 41 20 100 57 62 28 60 3 5 54 91 31 89 6 44 38 20 34 90 14 99 82 96 57 97 39 73 30 96 41 42 56 33 45 83 78 15 79 25 27 7 43 54 14 90 22 68 3 1 27 88 49 37 84 61 92 37 14 41 81 62 10 36 73 86 9 4", "output": "666" }, { "input": "1 44\n56", "output": "10" }, { "input": "5 136\n65 53 80 92 74", "output": "50" }, { "input": "20 964\n70 82 81 14 73 35 40 21 73 70 71 35 32 43 26 51 51 62 45 61", "output": "200" }, { "input": "80 4124\n14 37 6 11 63 59 43 72 88 0 53 43 42 95 65 61 9 69 9 95 49 64 27 34 53 31 34 26 30 48 85 97 35 60 74 45 35 86 11 34 45 72 95 95 95 13 58 2 0 38 37 13 61 47 85 77 96 10 34 3 54 55 91 23 57 13 33 16 2 17 80 61 36 57 79 81 90 33 82 48", "output": "800" }, { "input": "100 4899\n66 100 11 81 19 55 96 14 66 10 49 75 1 58 64 80 47 95 45 79 36 89 31 30 61 96 93 86 50 61 64 32 82 13 57 75 5 46 96 49 3 98 34 6 91 7 50 62 46 31 100 4 2 16 20 47 86 41 73 17 43 71 84 47 18 100 55 23 10 37 4 19 84 61 27 61 42 29 95 41 93 5 72 58 24 10 80 45 78 68 19 18 30 28 95 91 15 90 87 47", "output": "1000" }, { "input": "1 7035769\n1", "output": "10" }, { "input": "5 5012340\n10 63 89 25 29", "output": "50" }, { "input": "20 5527187\n15 91 34 37 16 77 85 4 31 28 2 47 8 45 57 51 58 72 97 16", "output": "200" }, { "input": "80 8000114\n27 46 16 80 85 11 20 22 80 24 85 22 17 86 96 60 16 12 94 39 23 86 12 49 28 78 80 23 92 78 62 38 27 43 35 62 60 89 85 63 39 27 70 13 73 91 82 73 98 83 70 93 5 37 15 85 39 58 92 34 93 44 31 86 28 86 43 3 25 12 18 61 25 7 67 87 37 29 65 98", "output": "800" }, { "input": "100 9455943\n44 8 21 71 7 29 40 65 91 70 48 19 77 48 16 22 54 4 29 34 9 22 73 34 47 41 5 83 32 91 52 6 74 64 18 23 9 4 36 78 98 20 20 3 69 86 41 67 54 76 87 84 47 6 52 87 61 100 98 80 14 14 24 99 90 73 97 79 22 65 65 51 29 44 15 67 21 58 79 80 96 40 63 73 96 59 72 24 87 85 74 49 81 30 16 61 87 30 0 13", "output": "1000" }, { "input": "1 6\n27", "output": "3" }, { "input": "1 10\n88", "output": "9" }, { "input": "1 3\n93", "output": "9" }, { "input": "2 22\n48 44", "output": "11" }, { "input": "1 48\n52", "output": "10" }, { "input": "1 2135072\n72", "output": "10" }, { "input": "3 5\n2 3 1", "output": "0" }, { "input": "5 1\n8 8 8 8 8", "output": "0" }, { "input": "3 150\n1 1 1", "output": "15" }, { "input": "4 3\n7 9 9 9", "output": "3" }, { "input": "1 10\n100", "output": "10" }, { "input": "3 11\n100 100 100", "output": "30" }, { "input": "2 11\n99 100", "output": "20" } ]
31
0
0
19,356
271
Good Substrings
[ "data structures", "strings" ]
null
null
You've got string *s*, consisting of small English letters. Some of the English letters are good, the rest are bad. A substring *s*[*l*...*r*] (1<=≤<=*l*<=≤<=*r*<=≤<=|*s*|) of string *s*<=<==<=<=*s*1*s*2...*s*|*s*| (where |*s*| is the length of string *s*) is string <=*s**l**s**l*<=+<=1...*s**r*. The substring *s*[*l*...*r*] is good, if among the letters <=*s**l*,<=*s**l*<=+<=1,<=...,<=*s**r* there are at most *k* bad ones (look at the sample's explanation to understand it more clear). Your task is to find the number of distinct good substrings of the given string *s*. Two substrings *s*[*x*...*y*] and *s*[*p*...*q*] are considered distinct if their content is different, i.e. *s*[*x*...*y*]<=≠<=*s*[*p*...*q*].
The first line of the input is the non-empty string *s*, consisting of small English letters, the string's length is at most 1500 characters. The second line of the input is the string of characters "0" and "1", the length is exactly 26 characters. If the *i*-th character of this string equals "1", then the *i*-th English letter is good, otherwise it's bad. That is, the first character of this string corresponds to letter "a", the second one corresponds to letter "b" and so on. The third line of the input consists a single integer *k* (0<=≤<=*k*<=≤<=|*s*|) — the maximum acceptable number of bad characters in a good substring.
Print a single integer — the number of distinct good substrings of string *s*.
[ "ababab\n01000000000000000000000000\n1\n", "acbacbacaa\n00000000000000000000000000\n2\n" ]
[ "5\n", "8\n" ]
In the first example there are following good substrings: "a", "ab", "b", "ba", "bab". In the second example there are following good substrings: "a", "aa", "ac", "b", "ba", "c", "ca", "cb".
[ { "input": "ababab\n01000000000000000000000000\n1", "output": "5" }, { "input": "acbacbacaa\n00000000000000000000000000\n2", "output": "8" }, { "input": "a\n00000000000000000000000000\n0", "output": "0" }, { "input": "aaaa\n00000000000000000000000000\n0", "output": "0" }, { "input": "aaaaaa\n00000000000000000000000000\n1", "output": "1" }, { "input": "bbbbbbbbba\n01000000000000000000000000\n0", "output": "9" }, { "input": "bbbbbbbbba\n10000000000000000000000000\n0", "output": "1" }, { "input": "kqdwdulmgvugvbl\n00101010100100100101101110\n13", "output": "114" }, { "input": "acehqnrtuwaealwbqufdmizce\n10000110100000010011101101\n16", "output": "316" }, { "input": "yqahbyyoxltryqdmvenemaqnbakglgqolxnaifnqtoclnnqiab\n11000001000110100111100001\n41", "output": "1243" }, { "input": "dykhvzcntljuuoqghptioetqnfllwekzohiuaxelgecabvsbibgqodqxvyfkbyjwtgbyhvssntinkwsinwsmalusiwnjmtcoovfj\n10001111101011111101101001\n25", "output": "4420" }, { "input": "dcijflpluxgeqlroaktzcujfaaidnrdzgogzhobhsmbwmjpschtvjmivfapddsmxfvlhhgguymgtdjxpiezbnlmnlvdnuaohqskdbcjlyfdrzicflveffvpeyzhwqqdaenbsghuvetpxvqcutjxbelbfzuqpwlfvveebnmkoryxgodpccanzwhziiiumgtwskxhrhwdkwmyreefwmoedcvaokvetcgundyiidsqkolpqkarpszrrmagrfvpnwharotashtwcnrcnhapdwrbltgkpkkmlrpapfizonyttrikh\n11011010101101110101010001\n54", "output": "31658" }, { "input": "baababbbaa\n01011100110010100100001111\n7", "output": "41" }, { "input": "aaaabaababbbbbbbbbaaabaaabbbababbbbbbabbaaabaabaabaaabaaaabbababbaaaaaaababbaababbabbaabbbabaabbbbababaaabbabbbabaaabbbabbbbbbbbabababaaaaaabaababbbbbaaaaaaaaaaaaaaababbbbabbbbbbababaabbabbbaababaabaaaabbababaaaabaaaaaaabaabaababbbbbbaababbabbaaaababbabbaaaabaaaaaaababbbbabbbbabaaabababaaaabaaaababb\n11011010101101110101010001\n54", "output": "42890" }, { "input": "dykhvzcntljuuoqghptioetqnfllwekzohiuaxelgecabvsbibgqodqxvyfkbyjwtgbyhvssntinkwsinwsmalusiwnjmtcoovfj\n11111111111111111111111111\n25", "output": "4967" }, { "input": "twnwdluhxf\n00000000000000000000000000\n1", "output": "9" }, { "input": "aaaaaaaaaa\n01011100110010100100001111\n7", "output": "7" }, { "input": "eoyirpkwgpvvwzaaaaaaaaaaaaaa\n11111111111111111111111111\n1", "output": "312" }, { "input": "fwnpwwuzszuryaaaaaaaaaaaaa\n11111111111111111111111111\n1", "output": "269" }, { "input": "xnahfslefuigqaaaaaaaaaaaaa\n11111111111111111111111111\n1", "output": "271" } ]
1,122
1,228,800
3
19,407
883
Renovation
[ "constructive algorithms", "greedy", "sortings" ]
null
null
The mayor of the Berland city S sees the beauty differently than other city-dwellers. In particular, he does not understand at all, how antique houses can be nice-looking. So the mayor wants to demolish all ancient buildings in the city. The city S is going to host the football championship very soon. In order to make the city beautiful, every month the Berland government provides mayor a money tranche. The money has to be spent on ancient buildings renovation. There are *n* months before the championship and the *i*-th month tranche equals to *a**i* burles. The city S has *m* antique buildings and the renovation cost of the *j*-th building is *b**j* burles. The mayor has his own plans for spending the money. As he doesn't like antique buildings he wants to demolish as much of them as possible. For the *j*-th building he calculated its demolishing cost *p**j*. The mayor decided to act according to the following plan. Each month he chooses several (possibly zero) of *m* buildings to demolish in such a way that renovation cost of each of them separately is not greater than the money tranche *a**i* of this month (*b**j*<=≤<=*a**i*) — it will allow to deceive city-dwellers that exactly this building will be renovated. Then the mayor has to demolish all selected buildings during the current month as otherwise the dwellers will realize the deception and the plan will fail. Definitely the total demolishing cost can not exceed amount of money the mayor currently has. The mayor is not obliged to spend all the money on demolishing. If some money is left, the mayor puts it to the bank account and can use it in any subsequent month. Moreover, at any month he may choose not to demolish any buildings at all (in this case all the tranche will remain untouched and will be saved in the bank). Your task is to calculate the maximal number of buildings the mayor can demolish.
The first line of the input contains two integers *n* and *m* (1<=≤<=*n*,<=*m*<=≤<=100<=000) — the number of months before the championship and the number of ancient buildings in the city S. The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=≤<=*a**i*<=≤<=109), where *a**i* is the tranche of the *i*-th month. The third line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m* (1<=≤<=*b**j*<=≤<=109), where *b**j* is renovation cost of the *j*-th building. The fourth line contains *m* integers *p*1,<=*p*2,<=...,<=*p**m* (1<=≤<=*p**j*<=≤<=109), where *p**j* is the demolishing cost of the *j*-th building.
Output single integer — the maximal number of buildings the mayor can demolish.
[ "2 3\n2 4\n6 2 3\n1 3 2\n", "3 5\n5 3 1\n5 2 9 1 10\n4 2 1 3 10\n", "5 6\n6 3 2 4 3\n3 6 4 5 4 2\n1 4 3 2 5 3\n" ]
[ "2\n", "3\n", "6\n" ]
In the third example the mayor acts as follows. In the first month he obtains 6 burles tranche and demolishes buildings #2 (renovation cost 6, demolishing cost 4) and #4 (renovation cost 5, demolishing cost 2). He spends all the money on it. After getting the second month tranche of 3 burles, the mayor selects only building #1 (renovation cost 3, demolishing cost 1) for demolishing. As a result, he saves 2 burles for the next months. In the third month he gets 2 burle tranche, but decides not to demolish any buildings at all. As a result, he has 2 + 2 = 4 burles in the bank. This reserve will be spent on the fourth month together with the 4-th tranche for demolishing of houses #3 and #5 (renovation cost is 4 for each, demolishing costs are 3 and 5 correspondingly). After this month his budget is empty. Finally, after getting the last tranche of 3 burles, the mayor demolishes building #6 (renovation cost 2, demolishing cost 3). As it can be seen, he demolished all 6 buildings.
[ { "input": "2 3\n2 4\n6 2 3\n1 3 2", "output": "2" }, { "input": "3 5\n5 3 1\n5 2 9 1 10\n4 2 1 3 10", "output": "3" }, { "input": "5 6\n6 3 2 4 3\n3 6 4 5 4 2\n1 4 3 2 5 3", "output": "6" }, { "input": "1 5\n9\n1 2 3 4 5\n5 4 3 2 1", "output": "3" }, { "input": "2 3\n2 4\n5 7 5\n1 3 2", "output": "0" }, { "input": "2 3\n4 2\n1 4 4\n1 3 2", "output": "2" }, { "input": "10 11\n3 8 2 5 11 7 8 2 6 4\n2 5 12 8 9 10 7 9 6 4 3\n2 3 5 4 6 3 6 5 8 2 1", "output": "10" }, { "input": "10 11\n3 8 2 5 11 7 8 2 6 4\n2 5 12 8 9 10 7 9 6 4 3\n4 4 5 6 8 5 9 11 5 2 4", "output": "9" }, { "input": "10 12\n13 12 13 3 10 3 4 4 8 5\n6 12 13 13 10 10 1 9 3 3 4 5\n9 9 6 6 4 8 9 9 5 7 9 5", "output": "10" }, { "input": "1 1\n12\n10\n10", "output": "1" }, { "input": "2 1\n1 3\n10\n10", "output": "0" }, { "input": "2 1\n9 9\n8\n6", "output": "1" }, { "input": "10 8\n4 3 7 6 9 8 4 1 6 10\n3 5 10 9 6 7 2 3\n6 8 11 4 10 14 7 12", "output": "7" }, { "input": "10 17\n19 4 4 3 12 7 4 19 21 18\n22 18 21 14 9 16 25 22 5 10 5 8 12 21 10 22 17\n20 12 9 18 13 20 21 12 20 15 19 22 9 21 12 13 15", "output": "8" }, { "input": "42 42\n19 3 36 11 33 2 39 44 36 44 43 16 31 16 11 14 18 35 17 13 44 46 4 47 13 48 42 25 10 35 23 1 31 17 35 8 19 42 13 30 18 20\n16 44 35 44 47 17 17 16 13 36 9 37 10 42 41 1 12 23 20 14 44 18 4 42 35 17 19 19 47 26 44 18 46 19 30 22 35 19 36 25 30 33\n31 35 33 34 35 36 37 19 36 34 36 27 33 20 15 15 23 23 34 14 23 18 17 22 22 36 33 36 37 26 35 23 14 14 24 26 16 30 31 31 30 28", "output": "39" }, { "input": "10 24\n10 15 6 15 15 9 6 13 5 13\n5 14 14 11 14 11 10 5 14 14 6 13 10 8 9 11 5 14 5 13 14 10 8 8\n10 9 12 12 8 4 6 5 3 3 7 4 3 13 11 9 3 5 4 6 3 9 3 3", "output": "20" }, { "input": "13 6\n10 9 2 2 2 2 2 2 2 2 2 2 1\n10 9 9 9 9 1\n10 4 4 4 4 20", "output": "5" }, { "input": "5 12\n48 48 47 42 40\n43 41 49 42 48 44 50 46 43 46 50 44\n49 50 42 49 43 46 40 45 48 44 41 46", "output": "4" }, { "input": "15 15\n952 894 894 877 812 470 451 419 413 367 275 258 161 137 75\n894 894 258 812 952 451 275 75 877 137 161 470 419 413 367\n894 894 258 812 952 451 275 75 877 137 161 470 419 413 367", "output": "15" }, { "input": "23 16\n966 950 886 684 528 446 356 124 67 4 500000000 450000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 100000000 50000000\n450000000 500000000 450000000 356 450000000 528 67 4 450000000 950 966 886 50000000 684 446 124\n200000000 500000000 200000000 356 200000000 528 67 4 200000000 950 966 886 1000000000 684 446 124", "output": "15" }, { "input": "52 24\n10 10 10 10 9 9 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1\n1 9 10 10 9 9 9 9 10 9 9 9 9 9 9 9 1 10 9 1 1 9 9 9\n20 4 10 10 4 4 4 4 10 4 4 4 4 4 4 4 20 10 4 20 20 4 4 4", "output": "21" }, { "input": "3 5\n1000000000 1000000000 1000000000\n1 1 1000000000 1000000000 1000000000\n1 1000000000 1000000000 1000000000 1000000000", "output": "3" } ]
30
0
0
19,515
297
Parity Game
[ "constructive algorithms" ]
null
null
You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and "1") *a* and *b*. Then you try to turn *a* into *b* using two types of operations: - Write *parity*(*a*) to the end of *a*. For example, . - Remove the first character of *a*. For example, . You cannot perform this operation if *a* is empty. You can use as many operations as you want. The problem is, is it possible to turn *a* into *b*? The *parity* of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise.
The first line contains the string *a* and the second line contains the string *b* (1<=≤<=|*a*|,<=|*b*|<=≤<=1000). Both strings contain only the characters "0" and "1". Here |*x*| denotes the length of the string *x*.
Print "YES" (without quotes) if it is possible to turn *a* into *b*, and "NO" (without quotes) otherwise.
[ "01011\n0110\n", "0011\n1110\n" ]
[ "YES\n", "NO\n" ]
In the first sample, the steps are as follows: 01011 → 1011 → 011 → 0110
[ { "input": "01011\n0110", "output": "YES" }, { "input": "0011\n1110", "output": "NO" }, { "input": "11111\n111111", "output": "YES" }, { "input": "0110011\n01100110", "output": "YES" }, { "input": "10000100\n011110", "output": "NO" }, { "input": "1\n0", "output": "YES" }, { "input": "0\n000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", "output": "NO" }, { "input": "0\n100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "NO" }, { "input": "1\n1100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "YES" }, { "input": "11\n110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "YES" }, { "input": "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\n11", "output": "YES" }, { "input": "11\n111", "output": "NO" }, { "input": "1\n1", "output": "YES" }, { "input": "1\n0", "output": "YES" } ]
1,000
111,104,000
0
19,538
758
Ability To Convert
[ "constructive algorithms", "dp", "greedy", "math", "strings" ]
null
null
Alexander is learning how to convert numbers from the decimal system to any other, however, he doesn't know English letters, so he writes any number only as a decimal number, it means that instead of the letter *A* he will write the number 10. Thus, by converting the number 475 from decimal to hexadecimal system, he gets 11311 (475<==<=1·162<=+<=13·161<=+<=11·160). Alexander lived calmly until he tried to convert the number back to the decimal number system. Alexander remembers that he worked with little numbers so he asks to find the minimum decimal number so that by converting it to the system with the base *n* he will get the number *k*.
The first line contains the integer *n* (2<=≤<=*n*<=≤<=109). The second line contains the integer *k* (0<=≤<=*k*<=&lt;<=1060), it is guaranteed that the number *k* contains no more than 60 symbols. All digits in the second line are strictly less than *n*. Alexander guarantees that the answer exists and does not exceed 1018. The number *k* doesn't contain leading zeros.
Print the number *x* (0<=≤<=*x*<=≤<=1018) — the answer to the problem.
[ "13\n12\n", "16\n11311\n", "20\n999\n", "17\n2016\n" ]
[ "12", "475", "3789", "594" ]
In the first example 12 could be obtained by converting two numbers to the system with base 13: 12 = 12·13<sup class="upper-index">0</sup> or 15 = 1·13<sup class="upper-index">1</sup> + 2·13<sup class="upper-index">0</sup>.
[ { "input": "13\n12", "output": "12" }, { "input": "16\n11311", "output": "475" }, { "input": "20\n999", "output": "3789" }, { "input": "17\n2016", "output": "594" }, { "input": "1000\n1001", "output": "100001" }, { "input": "1000\n1000", "output": "100000" }, { "input": "2\n110111100000101101101011001110100111011001000000000000000000", "output": "1000000000000000000" }, { "input": "500\n29460456244280453288", "output": "467528530570226788" }, { "input": "1000000000\n17289468142098080", "output": "17289468142098080" }, { "input": "123\n7719", "output": "9490" }, { "input": "25\n2172214240", "output": "26524975" }, { "input": "2\n1110110101111000010001011110101001011001110000000010111010", "output": "267367244641009850" }, { "input": "3\n1210020121011022121222022012121212020", "output": "268193483524125978" }, { "input": "4\n32323300000100133222012211322", "output": "269019726702209402" }, { "input": "5\n4230423222300004320404110", "output": "269845965585325530" }, { "input": "6\n20201051430024130310350", "output": "270672213058376250" }, { "input": "7\n325503632564034033331", "output": "271498451941492378" }, { "input": "8\n17073735641412635372", "output": "272324690824608506" }, { "input": "9\n1733607167155630041", "output": "273150934002691930" }, { "input": "10\n996517375802030516", "output": "996517375802030516" }, { "input": "11\n1107835458761401923", "output": "997343614685146644" }, { "input": "20\n905191218118181710131111", "output": "738505167292405431" }, { "input": "50\n303521849112318129", "output": "59962796634170079" }, { "input": "100\n7226127039816418", "output": "7226127039816418" }, { "input": "1000\n839105509657869885", "output": "839105509657869885" }, { "input": "7501\n2542549323761022905", "output": "805176557484307547" }, { "input": "10981\n5149151039259677113", "output": "748054672922159638" }, { "input": "123358\n458270676485260235", "output": "860152492903254335" }, { "input": "2567853\n5247911636981396703", "output": "346042641011647808" }, { "input": "56132425\n3102369282985322", "output": "10027171005317597" }, { "input": "378135456\n42831383491941211", "output": "582652156959951259" }, { "input": "3\n110021012201002100122001102110010002", "output": "68193483524125904" }, { "input": "23\n12007622911918220", "output": "1781911903273803" }, { "input": "456\n82103391245145170", "output": "1621222691867186" }, { "input": "7897\n14412516641926184", "output": "6062228032315859" }, { "input": "23156\n27612518525717145", "output": "3433598652149101" }, { "input": "467879\n333380108424158040", "output": "72980519445207316" }, { "input": "7982154\n129530518193255487", "output": "82535003403725833" }, { "input": "21354646\n47160699363858581", "output": "21776150370291089" }, { "input": "315464878\n113635473256292967", "output": "35848000882710261" }, { "input": "1000000000\n17289468142098026", "output": "17289468142098026" }, { "input": "4\n200002312103012003212121020", "output": "9019726702208584" }, { "input": "46\n342836241940392925", "output": "694167817136539" }, { "input": "145\n357987665524124", "output": "330396354354854" }, { "input": "1344\n2498394521019605", "output": "814487257688093" }, { "input": "57974\n3619236326439503", "output": "7079242212325439" }, { "input": "215467\n2082791630100848", "output": "966934630351661" }, { "input": "7956123\n6718643712272358", "output": "4255926011071634" }, { "input": "13568864\n2513398972677784", "output": "4621032639107192" }, { "input": "789765212\n1039927282755769", "output": "821298450375293" }, { "input": "1000000000\n7289468142097485", "output": "7289468142097485" }, { "input": "5\n22011100004310232330", "output": "45965585242840" }, { "input": "98\n11291073236468", "output": "10007394522984" }, { "input": "364\n284155255182196", "output": "4993183241788" }, { "input": "8742\n111445644633405", "output": "74498130012303" }, { "input": "11346\n573275516211238", "output": "83675287784142" }, { "input": "442020\n13825031303078", "output": "26973736400898" }, { "input": "1740798\n321470190942028", "output": "99531390411376" }, { "input": "25623752\n25636131538378", "output": "65689385274354" }, { "input": "814730652\n56899767577002", "output": "46358126945150" }, { "input": "6\n5543321344052", "output": "12975669536" }, { "input": "79\n9653454753", "output": "27953623755" }, { "input": "158\n25832612364", "output": "15908078858" }, { "input": "1675\n11480678916", "output": "8852883441" }, { "input": "12650\n25380755475", "output": "40587846725" }, { "input": "165726\n465015206", "output": "770641106" }, { "input": "2015054\n30501583737", "output": "6147498437" }, { "input": "98000000\n19440834812", "output": "19052834812" }, { "input": "157137373\n525141766938", "output": "82638887763" }, { "input": "7\n441214552", "output": "26508694" }, { "input": "294\n2251151163", "output": "72564361" }, { "input": "2707\n11341512", "output": "3071250" }, { "input": "76559\n100147383", "output": "76682942" }, { "input": "124849\n6172319", "output": "7688108" }, { "input": "7014809\n73084644", "output": "52188307" }, { "input": "10849219\n65200749", "output": "70296063" }, { "input": "905835986\n371320", "output": "371320" }, { "input": "1000000000\n69204007", "output": "69204007" }, { "input": "8\n2670", "output": "1464" }, { "input": "25\n71610", "output": "4785" }, { "input": "1468\n21107", "output": "4043" }, { "input": "5723\n4907", "output": "4907" }, { "input": "251546\n7278", "output": "7278" }, { "input": "9\n78", "output": "71" }, { "input": "13\n41", "output": "53" }, { "input": "34\n13", "output": "13" }, { "input": "45\n22", "output": "22" }, { "input": "67\n29", "output": "29" }, { "input": "130\n100", "output": "100" }, { "input": "2\n1", "output": "1" }, { "input": "4\n1", "output": "1" }, { "input": "9\n3", "output": "3" }, { "input": "13\n9", "output": "9" }, { "input": "3215\n3", "output": "3" }, { "input": "1000000000\n6", "output": "6" }, { "input": "2\n0", "output": "0" }, { "input": "1000000000\n0", "output": "0" }, { "input": "378\n1378", "output": "4992" }, { "input": "378\n380378377", "output": "65568783041" }, { "input": "2\n10000000000000000000000000", "output": "33554432" }, { "input": "2\n10000000000000000000000000000", "output": "268435456" }, { "input": "2\n100000000000000000000000", "output": "8388608" } ]
62
0
3
19,570
140
New Year Cards
[ "brute force", "greedy", "implementation" ]
null
null
As meticulous Gerald sets the table, Alexander finished another post on Codeforces and begins to respond to New Year greetings from friends. Alexander has *n* friends, and each of them sends to Alexander exactly one e-card. Let us number his friends by numbers from 1 to *n* in the order in which they send the cards. Let's introduce the same numbering for the cards, that is, according to the numbering the *i*-th friend sent to Alexander a card number *i*. Alexander also sends cards to friends, but he doesn't look for the new cards on the Net. He simply uses the cards previously sent to him (sometimes, however, he does need to add some crucial details). Initially Alexander doesn't have any cards. Alexander always follows the two rules: 1. He will never send to a firend a card that this friend has sent to him. 1. Among the other cards available to him at the moment, Alexander always chooses one that Alexander himself likes most. Alexander plans to send to each friend exactly one card. Of course, Alexander can send the same card multiple times. Alexander and each his friend has the list of preferences, which is a permutation of integers from 1 to *n*. The first number in the list is the number of the favorite card, the second number shows the second favorite, and so on, the last number shows the least favorite card. Your task is to find a schedule of sending cards for Alexander. Determine at which moments of time Alexander must send cards to his friends, to please each of them as much as possible. In other words, so that as a result of applying two Alexander's rules, each friend receives the card that is preferred for him as much as possible. Note that Alexander doesn't choose freely what card to send, but he always strictly follows the two rules.
The first line contains an integer *n* (2<=≤<=*n*<=≤<=300) — the number of Alexander's friends, equal to the number of cards. Next *n* lines contain his friends' preference lists. Each list consists of *n* different integers from 1 to *n*. The last line contains Alexander's preference list in the same format.
Print *n* space-separated numbers: the *i*-th number should be the number of the friend, whose card Alexander receives right before he should send a card to the *i*-th friend. If there are several solutions, print any of them.
[ "4\n1 2 3 4\n4 1 3 2\n4 3 1 2\n3 4 2 1\n3 1 2 4\n" ]
[ "2 1 1 4\n" ]
In the sample, the algorithm of actions Alexander and his friends perform is as follows: 1. Alexander receives card 1 from the first friend. 1. Alexander sends the card he has received (at the moment he only has one card, and therefore it is the most preferable for him) to friends with the numbers 2 and 3. 1. Alexander receives card 2 from the second friend, now he has two cards — 1 and 2. 1. Alexander sends a card to the first friend. Despite the fact that Alexander likes card 1 more, he sends card 2 as he cannot send a friend the card sent by that very friend. 1. Alexander receives card 3 from the third friend. 1. Alexander receives card 4 from the fourth friend. 1. Among the cards Alexander has number 3 is his favorite and he sends it to the fourth friend. Note that Alexander can send cards to multiple friends at a time (in this case the second and the third one). Alexander can send card 3 to the fourth friend after he receives the third card or after he receives the fourth card (both variants are correct).
[ { "input": "4\n1 2 3 4\n4 1 3 2\n4 3 1 2\n3 4 2 1\n3 1 2 4", "output": "2 1 1 3" }, { "input": "2\n1 2\n2 1\n2 1", "output": "2 1" }, { "input": "3\n1 2 3\n2 3 1\n1 3 2\n3 2 1", "output": "2 3 1" }, { "input": "5\n1 4 2 3 5\n5 1 3 4 2\n3 2 4 1 5\n1 4 5 3 2\n5 2 3 4 1\n5 4 2 1 3", "output": "4 5 2 1 2" }, { "input": "10\n5 1 6 2 8 3 4 10 9 7\n3 1 10 6 8 5 2 7 9 4\n2 9 1 4 10 6 8 7 3 5\n10 1 7 8 3 2 4 6 5 9\n3 2 10 4 7 8 5 6 1 9\n5 6 3 10 8 7 2 9 4 1\n6 5 1 3 2 7 9 10 8 4\n1 10 9 3 7 8 4 2 6 5\n6 8 4 5 9 1 2 10 7 3\n9 6 8 5 10 3 1 7 2 4\n5 7 4 8 9 6 1 10 3 2", "output": "5 1 1 1 4 5 5 1 4 5" } ]
218
0
0
19,597
559
Gerald and Giant Chess
[ "combinatorics", "dp", "math", "number theory" ]
null
null
Giant chess is quite common in Geraldion. We will not delve into the rules of the game, we'll just say that the game takes place on an *h*<=×<=*w* field, and it is painted in two colors, but not like in chess. Almost all cells of the field are white and only some of them are black. Currently Gerald is finishing a game of giant chess against his friend Pollard. Gerald has almost won, and the only thing he needs to win is to bring the pawn from the upper left corner of the board, where it is now standing, to the lower right corner. Gerald is so confident of victory that he became interested, in how many ways can he win? The pawn, which Gerald has got left can go in two ways: one cell down or one cell to the right. In addition, it can not go to the black cells, otherwise the Gerald still loses. There are no other pawns or pieces left on the field, so that, according to the rules of giant chess Gerald moves his pawn until the game is over, and Pollard is just watching this process.
The first line of the input contains three integers: *h*,<=*w*,<=*n* — the sides of the board and the number of black cells (1<=≤<=*h*,<=*w*<=≤<=105,<=1<=≤<=*n*<=≤<=2000). Next *n* lines contain the description of black cells. The *i*-th of these lines contains numbers *r**i*,<=*c**i* (1<=≤<=*r**i*<=≤<=*h*,<=1<=≤<=*c**i*<=≤<=*w*) — the number of the row and column of the *i*-th cell. It is guaranteed that the upper left and lower right cell are white and all cells in the description are distinct.
Print a single line — the remainder of the number of ways to move Gerald's pawn from the upper left to the lower right corner modulo 109<=+<=7.
[ "3 4 2\n2 2\n2 3\n", "100 100 3\n15 16\n16 15\n99 88\n" ]
[ "2\n", "545732279\n" ]
none
[ { "input": "3 4 2\n2 2\n2 3", "output": "2" }, { "input": "100 100 3\n15 16\n16 15\n99 88", "output": "545732279" }, { "input": "1000 1000 4\n50 50\n51 50\n50 51\n51 51", "output": "899660737" }, { "input": "100000 100000 4\n50001 50001\n50000 50000\n50000 50001\n50001 50000", "output": "999612315" }, { "input": "2 2 2\n2 1\n1 2", "output": "0" }, { "input": "100 10 30\n40 4\n15 3\n75 3\n88 10\n32 1\n16 5\n81 8\n45 2\n72 8\n11 6\n86 4\n50 2\n9 4\n11 1\n20 3\n47 3\n2 4\n68 3\n90 5\n85 2\n88 1\n88 5\n86 3\n70 9\n49 3\n34 4\n5 7\n77 5\n50 1\n87 5", "output": "402737011" }, { "input": "100000 100000 2\n1 2\n2 1", "output": "0" }, { "input": "100000 100000 2\n99999 100000\n100000 99999", "output": "0" }, { "input": "100000 100000 3\n99998 100000\n99999 99999\n100000 99998", "output": "0" } ]
1,450
48,742,400
3
19,622
94
Friends
[ "graphs", "implementation", "math" ]
B. Friends
1
256
One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something. The following statement caught his attention: "Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people" Igor just couldn't get why the required minimum is 6 people. "Well, that's the same for five people, too!" — he kept on repeating in his mind. — "Let's take, say, Max, Ilya, Vova — here, they all know each other! And now let's add Dima and Oleg to Vova — none of them is acquainted with each other! Now, that math is just rubbish!" Igor K. took 5 friends of his and wrote down who of them is friends with whom. Now he wants to check whether it is true for the five people that among them there are either three pairwise acquainted or three pairwise not acquainted people.
The first line contains an integer *m* (0<=≤<=*m*<=≤<=10), which is the number of relations of acquaintances among the five friends of Igor's. Each of the following *m* lines contains two integers *a**i* and *b**i* (1<=≤<=*a**i*,<=*b**i*<=≤<=5;*a**i*<=≠<=*b**i*), where (*a**i*,<=*b**i*) is a pair of acquainted people. It is guaranteed that each pair of the acquaintances is described exactly once. The acquaintance relation is symmetrical, i.e. if *x* is acquainted with *y*, then *y* is also acquainted with *x*.
Print "FAIL", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print "WIN".
[ "4\n1 3\n2 3\n1 4\n5 3\n", "5\n1 2\n2 3\n3 4\n4 5\n5 1\n" ]
[ "WIN\n", "FAIL\n" ]
none
[ { "input": "4\n1 3\n2 3\n1 4\n5 3", "output": "WIN" }, { "input": "5\n1 2\n2 3\n3 4\n4 5\n5 1", "output": "FAIL" }, { "input": "1\n4 3", "output": "WIN" }, { "input": "6\n1 3\n2 3\n1 2\n5 3\n4 2\n4 5", "output": "WIN" }, { "input": "2\n1 3\n2 5", "output": "WIN" }, { "input": "3\n5 3\n4 3\n4 5", "output": "WIN" }, { "input": "5\n1 3\n3 2\n2 4\n5 4\n1 5", "output": "FAIL" }, { "input": "7\n1 3\n5 1\n1 4\n2 1\n5 3\n4 5\n2 5", "output": "WIN" }, { "input": "5\n5 1\n4 1\n2 3\n4 5\n3 1", "output": "WIN" }, { "input": "0", "output": "WIN" }, { "input": "10\n1 2\n1 3\n1 4\n1 5\n2 3\n2 4\n2 5\n3 4\n3 5\n4 5", "output": "WIN" }, { "input": "4\n1 2\n2 3\n3 4\n4 1", "output": "WIN" }, { "input": "1\n2 1", "output": "WIN" }, { "input": "1\n2 5", "output": "WIN" }, { "input": "2\n2 1\n1 5", "output": "WIN" }, { "input": "2\n4 2\n1 5", "output": "WIN" }, { "input": "2\n3 4\n5 2", "output": "WIN" }, { "input": "2\n1 5\n4 3", "output": "WIN" }, { "input": "3\n4 1\n4 5\n2 1", "output": "WIN" }, { "input": "3\n5 1\n5 3\n2 5", "output": "WIN" }, { "input": "3\n1 2\n4 2\n1 3", "output": "WIN" }, { "input": "3\n3 2\n1 5\n5 3", "output": "WIN" }, { "input": "3\n1 2\n2 4\n3 2", "output": "WIN" }, { "input": "3\n2 1\n1 3\n5 4", "output": "WIN" }, { "input": "4\n4 2\n2 5\n1 4\n4 5", "output": "WIN" }, { "input": "4\n5 2\n2 4\n5 3\n1 5", "output": "WIN" }, { "input": "4\n2 5\n1 3\n4 3\n4 2", "output": "WIN" }, { "input": "4\n1 4\n3 1\n2 3\n1 2", "output": "WIN" }, { "input": "4\n5 4\n2 3\n1 5\n5 2", "output": "WIN" }, { "input": "4\n2 5\n5 4\n1 4\n5 3", "output": "WIN" }, { "input": "4\n2 1\n2 4\n5 1\n4 1", "output": "WIN" }, { "input": "4\n1 2\n1 5\n4 5\n2 3", "output": "WIN" }, { "input": "5\n4 1\n2 4\n3 2\n5 3\n1 5", "output": "FAIL" }, { "input": "5\n1 3\n4 1\n5 2\n2 4\n3 5", "output": "FAIL" }, { "input": "5\n3 5\n4 2\n1 3\n2 1\n5 4", "output": "FAIL" }, { "input": "5\n5 2\n1 3\n4 5\n2 1\n3 4", "output": "FAIL" }, { "input": "5\n2 3\n3 5\n1 2\n4 1\n5 4", "output": "FAIL" }, { "input": "5\n1 2\n4 5\n5 3\n3 1\n2 4", "output": "FAIL" }, { "input": "5\n5 3\n3 2\n2 4\n1 5\n4 1", "output": "FAIL" }, { "input": "5\n3 2\n4 1\n2 5\n1 3\n5 4", "output": "FAIL" }, { "input": "5\n3 5\n1 4\n5 1\n2 3\n4 2", "output": "FAIL" }, { "input": "5\n4 2\n5 3\n2 1\n3 4\n1 5", "output": "FAIL" }, { "input": "5\n3 1\n5 1\n4 5\n2 4\n5 3", "output": "WIN" }, { "input": "5\n5 4\n5 3\n3 1\n1 4\n2 3", "output": "WIN" }, { "input": "5\n4 1\n3 5\n3 4\n5 4\n5 2", "output": "WIN" }, { "input": "5\n4 1\n5 2\n3 1\n4 2\n5 1", "output": "WIN" }, { "input": "5\n2 3\n1 5\n5 3\n2 4\n1 4", "output": "FAIL" }, { "input": "5\n5 4\n5 3\n2 3\n5 2\n5 1", "output": "WIN" }, { "input": "5\n2 4\n3 4\n1 4\n2 1\n3 2", "output": "WIN" }, { "input": "5\n2 3\n3 4\n1 3\n4 1\n5 2", "output": "WIN" }, { "input": "5\n1 2\n2 5\n4 2\n4 3\n3 1", "output": "WIN" }, { "input": "5\n2 1\n2 5\n4 5\n2 3\n3 5", "output": "WIN" }, { "input": "5\n4 1\n5 1\n5 4\n4 3\n5 2", "output": "WIN" }, { "input": "5\n1 3\n2 4\n1 5\n5 2\n4 1", "output": "WIN" }, { "input": "5\n1 5\n3 5\n2 3\n4 1\n3 1", "output": "WIN" }, { "input": "5\n5 2\n3 2\n2 1\n4 3\n4 2", "output": "WIN" }, { "input": "5\n1 3\n4 5\n3 4\n3 5\n5 1", "output": "WIN" }, { "input": "5\n4 5\n2 5\n5 3\n4 2\n4 1", "output": "WIN" }, { "input": "5\n2 5\n1 5\n1 3\n3 5\n1 2", "output": "WIN" }, { "input": "5\n2 4\n1 2\n5 2\n5 3\n4 5", "output": "WIN" }, { "input": "5\n2 1\n4 5\n5 3\n1 5\n1 4", "output": "WIN" }, { "input": "5\n1 3\n2 5\n4 2\n3 4\n4 1", "output": "WIN" }, { "input": "6\n3 2\n2 4\n3 1\n3 5\n5 2\n1 2", "output": "WIN" }, { "input": "6\n2 1\n5 1\n5 4\n3 5\n3 4\n4 1", "output": "WIN" }, { "input": "6\n3 1\n1 4\n5 4\n2 1\n4 2\n1 5", "output": "WIN" }, { "input": "6\n5 1\n5 4\n3 4\n1 3\n1 4\n4 2", "output": "WIN" }, { "input": "6\n1 3\n5 4\n4 2\n2 1\n4 1\n2 3", "output": "WIN" }, { "input": "6\n4 3\n5 3\n4 1\n1 3\n1 2\n2 4", "output": "WIN" }, { "input": "6\n4 1\n3 5\n4 5\n3 1\n4 3\n5 2", "output": "WIN" }, { "input": "6\n2 1\n1 4\n4 5\n5 2\n1 3\n3 2", "output": "WIN" }, { "input": "7\n5 1\n3 5\n2 5\n4 5\n2 3\n3 1\n4 3", "output": "WIN" }, { "input": "7\n5 3\n5 1\n4 2\n4 5\n3 2\n3 4\n1 3", "output": "WIN" }, { "input": "7\n3 5\n1 4\n5 2\n1 5\n1 3\n4 2\n4 3", "output": "WIN" }, { "input": "7\n5 1\n5 4\n2 4\n2 3\n3 5\n2 5\n4 1", "output": "WIN" }, { "input": "7\n1 3\n2 5\n4 3\n2 1\n2 3\n4 5\n2 4", "output": "WIN" }, { "input": "7\n3 1\n4 5\n3 5\n5 1\n2 4\n1 2\n1 4", "output": "WIN" }, { "input": "8\n1 5\n3 1\n2 5\n4 2\n2 1\n4 5\n4 3\n4 1", "output": "WIN" }, { "input": "8\n4 2\n3 1\n4 3\n2 5\n3 2\n4 5\n1 2\n3 5", "output": "WIN" }, { "input": "8\n2 4\n3 2\n2 5\n3 4\n3 1\n5 1\n4 5\n5 3", "output": "WIN" }, { "input": "8\n2 3\n1 5\n1 3\n4 5\n2 4\n1 4\n3 5\n3 4", "output": "WIN" }, { "input": "9\n3 5\n3 2\n1 5\n4 3\n5 4\n1 4\n1 3\n4 2\n5 2", "output": "WIN" }, { "input": "9\n3 5\n2 5\n5 1\n4 5\n1 3\n3 2\n1 4\n4 3\n4 2", "output": "WIN" }, { "input": "3\n3 4\n4 5\n5 3", "output": "WIN" }, { "input": "3\n1 2\n1 3\n4 5", "output": "WIN" }, { "input": "3\n2 3\n3 5\n2 5", "output": "WIN" } ]
46
307,200
0
19,751
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": "1" }, { "input": "2 1\n..\nX.\n1 2", "output": "1" }, { "input": "2 1\n..\nX.\n1 4", "output": "2" }, { "input": "2 1\n.X\n..\n1 4", "output": "2" }, { "input": "2 1\nX.\n..\n2 3", "output": "2" }, { "input": "2 1\n..\n.X\n3 2", "output": "2" } ]
1,247
75,776,000
3
19,757
381
Sereja and Stairs
[ "greedy", "implementation", "sortings" ]
null
null
Sereja loves integer sequences very much. He especially likes stairs. Sequence *a*1,<=*a*2,<=...,<=*a*|*a*| (|*a*| is the length of the sequence) is stairs if there is such index *i* (1<=≤<=*i*<=≤<=|*a*|), that the following condition is met: For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't. Sereja has *m* cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the table?
The first line contains integer *m* (1<=≤<=*m*<=≤<=105) — the number of Sereja's cards. The second line contains *m* integers *b**i* (1<=≤<=*b**i*<=≤<=5000) — the numbers on the Sereja's cards.
In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.
[ "5\n1 2 3 4 5\n", "6\n1 1 2 2 3 3\n" ]
[ "5\n5 4 3 2 1\n", "5\n1 2 3 2 1\n" ]
none
[ { "input": "5\n1 2 3 4 5", "output": "5\n5 4 3 2 1" }, { "input": "6\n1 1 2 2 3 3", "output": "5\n1 2 3 2 1" }, { "input": "47\n3 4 5 3 1 4 4 3 4 6 1 5 1 3 5 3 6 5 1 4 3 2 6 5 3 1 4 6 4 6 2 1 1 1 4 3 6 1 6 6 3 5 1 4 6 4 4", "output": "11\n1 2 3 4 5 6 5 4 3 2 1" }, { "input": "13\n8 23 26 8 15 13 35 36 28 8 4 33 6", "output": "12\n8 36 35 33 28 26 23 15 13 8 6 4" }, { "input": "17\n15 29 28 23 20 12 9 30 4 13 1 25 11 20 6 23 10", "output": "17\n20 23 30 29 28 25 23 20 15 13 12 11 10 9 6 4 1" }, { "input": "31\n189 73 300 133 414 23 150 301 252 21 274 272 316 291 339 356 201 267 257 43 10 25 16 211 59 2 181 54 344 337 201", "output": "31\n201 414 356 344 339 337 316 301 300 291 274 272 267 257 252 211 201 189 181 150 133 73 59 54 43 25 23 21 16 10 2" }, { "input": "85\n319 554 696 281 275 544 356 313 296 308 848 668 135 705 231 735 882 622 796 435 621 523 709 247 169 152 395 758 447 595 550 819 188 664 589 907 3 619 771 810 669 471 425 870 737 329 83 549 425 138 870 775 451 818 735 169 162 419 903 803 852 75 297 687 310 714 419 652 164 667 245 906 133 643 881 322 681 704 479 278 114 324 42 475 396", "output": "85\n169 419 425 735 870 907 906 903 882 881 870 852 848 819 818 810 803 796 775 771 758 737 735 714 709 705 704 696 687 681 669 668 667 664 652 643 622 621 619 595 589 554 550 549 544 523 479 475 471 451 447 435 425 419 396 395 356 329 324 322 319 313 310 308 297 296 281 278 275 247 245 231 188 169 164 162 152 138 135 133 114 83 75 42 3" }, { "input": "102\n1830 2653 1293 4285 4679 3563 3668 4499 3507 2666 3507 1120 466 290 4280 60 4135 1120 289 1752 2101 2699 653 2811 3885 4018 4097 3142 2932 561 193 3662 3017 3487 3158 2876 3396 2806 3440 4503 1728 362 2194 2743 2946 596 1892 4785 3661 2971 3487 17 3616 2710 1189 613 915 3891 2654 554 3506 1938 2783 2537 4791 1098 930 1000 1007 942 3871 2547 1798 4610 1613 774 1347 1460 2752 3158 4540 4720 2639 887 1999 2046 1199 1889 426 1888 4317 649 1660 336 4728 2422 2771 3536 1683 3786 2711 308", "output": "102\n1120 3158 3487 3507 4791 4785 4728 4720 4679 4610 4540 4503 4499 4317 4285 4280 4135 4097 4018 3891 3885 3871 3786 3668 3662 3661 3616 3563 3536 3507 3506 3487 3440 3396 3158 3142 3017 2971 2946 2932 2876 2811 2806 2783 2771 2752 2743 2711 2710 2699 2666 2654 2653 2639 2547 2537 2422 2194 2101 2046 1999 1938 1892 1889 1888 1830 1798 1752 1728 1683 1660 1613 1460 1347 1293 1199 1189 1120 1098 1007 1000 942 930 915 887 774 653 649 613 596 561 554 466 426 362 336 308 290 289 193 60 17" }, { "input": "1\n1", "output": "1\n1" }, { "input": "2\n1 1", "output": "1\n1" }, { "input": "3\n1 2 3", "output": "3\n3 2 1" }, { "input": "3\n2 1 2", "output": "2\n2 1" }, { "input": "2\n1 2", "output": "2\n2 1" } ]
108
6,246,400
3
19,787
138
Mushroom Gnomes - 2
[ "binary search", "data structures", "probabilities", "sortings" ]
null
null
One day Natalia was walking in the woods when she met a little mushroom gnome. The gnome told her the following story: Everybody knows that the mushroom gnomes' power lies in the magic mushrooms that grow in the native woods of the gnomes. There are *n* trees and *m* magic mushrooms in the woods: the *i*-th tree grows at a point on a straight line with coordinates *a**i* and has the height of *h**i*, the *j*-th mushroom grows at the point with coordinates *b**j* and has magical powers *z**j*. But one day wild mushroommunchers, the sworn enemies of mushroom gnomes unleashed a terrible storm on their home forest. As a result, some of the trees began to fall and crush the magic mushrooms. The supreme oracle of mushroom gnomes calculated in advance the probability for each tree that it will fall to the left, to the right or will stand on. If the tree with the coordinate *x* and height *h* falls to the left, then all the mushrooms that belong to the right-open interval [*x*<=-<=*h*,<=*x*), are destroyed. If a tree falls to the right, then the mushrooms that belong to the left-open interval (*x*,<=*x*<=+<=*h*] are destroyed. Only those mushrooms that are not hit by a single tree survive. Knowing that all the trees fall independently of each other (i.e., all the events are mutually independent, and besides, the trees do not interfere with other trees falling in an arbitrary direction), the supreme oracle was also able to quickly calculate what would be the expectation of the total power of the mushrooms which survived after the storm. His calculations ultimately saved the mushroom gnomes from imminent death. Natalia, as a good Olympiad programmer, got interested in this story, and she decided to come up with a way to quickly calculate the expectation of the sum of the surviving mushrooms' power.
The first line contains two integers *n* and *m* (1<=≤<=*n*<=≤<=105, 1<=≤<=*m*<=≤<=104) — the number of trees and mushrooms, respectively. Each of the next *n* lines contain four integers — *a**i*, *h**i*, *l**i*, *r**i* (|*a**i*|<=≤<=109, 1<=≤<=*h**i*<=≤<=109, 0<=≤<=*l**i*,<=*r**i*,<=*l**i*<=+<=*r**i*<=≤<=100) which represent the coordinate of the *i*-th tree, its height, the percentage of the probabilities that the tree falls to the left and to the right, respectively (the remaining percentage is the probability that the tree will stand on). Each of next *m* lines contain two integers *b**j*, *z**j* (|*b**j*|<=≤<=109, 1<=≤<=*z**j*<=≤<=103) which represent the coordinate and the magical power of the *j*-th mushroom, respectively. An arbitrary number of trees and mushrooms can grow in one point.
Print a real number — the expectation of the total magical power of the surviving mushrooms. The result is accepted with relative or absolute accuracy 10<=-<=4.
[ "1 1\n2 2 50 50\n1 1\n", "2 1\n2 2 50 50\n4 2 50 50\n3 1\n" ]
[ "0.5000000000\n", "0.2500000000\n" ]
It is believed that the mushroom with the coordinate *x* belongs to the right-open interval [*l*, *r*) if and only if *l* ≤ *x* &lt; *r*. Similarly, the mushroom with the coordinate *x* belongs to the left-open interval (*l*, *r*] if and only if *l* &lt; *x* ≤ *r*. In the first test the mushroom survives with the probability of 50%, depending on where the single tree falls. In the second test the mushroom survives only if neither of the two trees falls on it. It occurs with the probability of 50%  ×  50% = 25%. Pretest №12 is the large test with 10<sup class="upper-index">5</sup> trees and one mushroom.
[ { "input": "1 1\n2 2 50 50\n1 1", "output": "0.5000000000" }, { "input": "2 1\n2 2 50 50\n4 2 50 50\n3 1", "output": "0.2500000000" }, { "input": "1 1\n3 2 73 12\n1 5", "output": "1.3500000000" }, { "input": "2 2\n-8 4 66 9\n-2 3 55 43\n3 8\n7 9", "output": "17.0000000000" }, { "input": "2 3\n-5 6 0 100\n5 6 100 0\n6 3\n0 4\n-6 3", "output": "6.0000000000" }, { "input": "3 3\n2 5 67 8\n-5 5 56 13\n0 7 32 5\n2 8\n-1 5\n-1 7", "output": "9.9427360000" }, { "input": "1 1\n0 1 61 17\n1 25", "output": "20.7500000000" }, { "input": "7 5\n15 3 21 18\n9 3 27 48\n-9 2 25 9\n-4 1 42 28\n-18 2 2 88\n3 1 15 9\n-7 1 31 14\n-12 5\n14 9\n-8 7\n14 3\n2 9", "output": "26.5253000000" }, { "input": "1 10\n0 2 46 24\n-4 6\n-9 3\n3 4\n2 6\n-2 10\n-5 4\n7 5\n3 9\n6 1\n9 8", "output": "49.9600000000" }, { "input": "10 1\n1 2 46 34\n-9 2 47 4\n-10 2 63 14\n4 2 12 15\n4 1 37 53\n4 1 1 8\n3 1 10 39\n-8 1 45 50\n5 2 11 66\n10 2 1 53\n1 8", "output": "8.0000000000" }, { "input": "10 10\n58 1 50 35\n57 7 79 21\n52 4 23 22\n19 5 17 4\n-25 9 32 35\n47 5 34 27\n24 10 3 52\n2 4 10 20\n-37 1 10 5\n-56 7 57 16\n15 27\n57 34\n18 88\n-17 87\n-85 36\n86 96\n53 56\n-7 23\n-7 1\n11 86", "output": "417.3093000000" }, { "input": "1 1\n47634012 224957015 30 67\n-121586701 418", "output": "292.6000000000" } ]
46
0
0
19,819
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*<=&lt;<=*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\n61 49 10\n14 69 10\n88 2 14\n35 86 18\n63 16 20\n39 49 22\n32 45 23\n66 54 25\n77 2 36\n96 85 38\n33 28 45\n29 78 53\n78 13 60\n58 96 64\n74 39 71\n18 80 80\n18 7 85\n97 82 96\n74 99 97", "output": "877" } ]
3,275
22,732,800
3
19,861
570
Tree Requests
[ "binary search", "bitmasks", "constructive algorithms", "dfs and similar", "graphs", "trees" ]
null
null
Roman planted a tree consisting of *n* vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the *n*<=-<=1 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex *i* is vertex *p**i*, the parent index is always less than the index of the vertex (i.e., *p**i*<=&lt;<=*i*). The depth of the vertex is the number of nodes on the path from the root to *v* along the edges. In particular, the depth of the root is equal to 1. We say that vertex *u* is in the subtree of vertex *v*, if we can get from *u* to *v*, moving from the vertex to the parent. In particular, vertex *v* is in its subtree. Roma gives you *m* queries, the *i*-th of which consists of two numbers *v**i*, *h**i*. Let's consider the vertices in the subtree *v**i* located at depth *h**i*. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used.
The first line contains two integers *n*, *m* (1<=≤<=*n*,<=*m*<=≤<=500<=000) — the number of nodes in the tree and queries, respectively. The following line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* — the parents of vertices from the second to the *n*-th (1<=≤<=*p**i*<=&lt;<=*i*). The next line contains *n* lowercase English letters, the *i*-th of these letters is written on vertex *i*. Next *m* lines describe the queries, the *i*-th line contains two numbers *v**i*, *h**i* (1<=≤<=*v**i*,<=*h**i*<=≤<=*n*) — the vertex and the depth that appear in the *i*-th query.
Print *m* lines. In the *i*-th line print "Yes" (without the quotes), if in the *i*-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes).
[ "6 5\n1 1 1 3 3\nzacccd\n1 1\n3 3\n4 1\n6 1\n1 2\n" ]
[ "Yes\nNo\nYes\nYes\nYes\n" ]
String *s* is a palindrome if reads the same from left to right and from right to left. In particular, an empty string is a palindrome. Clarification for the sample test. In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z". In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d" respectively. It is impossible to form a palindrome of them. In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome. In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome. In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c" and "c". We may form a palindrome "cac".
[ { "input": "6 5\n1 1 1 3 3\nzacccd\n1 1\n3 3\n4 1\n6 1\n1 2", "output": "Yes\nNo\nYes\nYes\nYes" }, { "input": "5 6\n1 1 2 3\ncbcab\n3 1\n5 2\n1 3\n4 1\n4 2\n1 1", "output": "Yes\nYes\nNo\nYes\nYes\nYes" }, { "input": "5 6\n1 2 2 1\nbaabb\n1 1\n1 2\n5 1\n4 1\n4 2\n3 2", "output": "Yes\nNo\nYes\nYes\nYes\nYes" }, { "input": "5 9\n1 1 1 2\nedbcb\n1 3\n2 1\n1 3\n2 1\n2 2\n2 2\n1 1\n1 3\n2 1", "output": "Yes\nYes\nYes\nYes\nYes\nYes\nYes\nYes\nYes" }, { "input": "8 12\n1 1 1 2 1 1 4\ncbecdcce\n1 2\n1 2\n2 1\n1 1\n2 1\n1 3\n1 3\n1 3\n1 2\n2 3\n1 3\n1 1", "output": "No\nNo\nYes\nYes\nYes\nNo\nNo\nNo\nNo\nYes\nNo\nYes" }, { "input": "1 1\n\np\n1 1", "output": "Yes" }, { "input": "1 1\n\na\n1 1", "output": "Yes" } ]
1,216
268,390,400
0
19,891
0
none
[ "none" ]
null
null
Профиль горного хребта схематично задан в виде прямоугольной таблицы из символов «.» (пустое пространство) и «*» (часть горы). Каждый столбец таблицы содержит хотя бы одну «звёздочку». Гарантируется, что любой из символов «*» либо находится в нижней строке матрицы, либо непосредственно под ним находится другой символ «*». Маршрут туриста проходит через весь горный хребет слева направо. Каждый день турист перемещается вправо — в соседний столбец в схематичном изображении. Конечно, каждый раз он поднимается (или опускается) в самую верхнюю точку горы, которая находится в соответствующем столбце. Считая, что изначально турист находится в самой верхней точке в первом столбце, а закончит свой маршрут в самой верхней точке в последнем столбце, найдите две величины: - наибольший подъём за день (равен 0, если в профиле горного хребта нет ни одного подъёма), - наибольший спуск за день (равен 0, если в профиле горного хребта нет ни одного спуска).
В первой строке входных данных записаны два целых числа *n* и *m* (1<=≤<=*n*,<=*m*<=≤<=100) — количество строк и столбцов в схематичном изображении соответственно. Далее следуют *n* строк по *m* символов в каждой — схематичное изображение горного хребта. Каждый символ схематичного изображения — это либо «.», либо «*». Каждый столбец матрицы содержит хотя бы один символ «*». Гарантируется, что любой из символов «*» либо находится в нижней строке матрицы, либо непосредственно под ним находится другой символ «*».
Выведите через пробел два целых числа: - величину наибольшего подъёма за день (или 0, если в профиле горного хребта нет ни одного подъёма), - величину наибольшего спуска за день (или 0, если в профиле горного хребта нет ни одного спуска).
[ "6 11\n...........\n.........*.\n.*.......*.\n**.......*.\n**..*...**.\n***********\n", "5 5\n....*\n...**\n..***\n.****\n*****\n", "8 7\n.......\n.*.....\n.*.....\n.**....\n.**.*..\n.****.*\n.******\n*******\n" ]
[ "3 4\n", "1 0\n", "6 2\n" ]
В первом тестовом примере высоты гор равны: 3, 4, 1, 1, 2, 1, 1, 1, 2, 5, 1. Наибольший подъем равен 3 и находится между горой номер 9 (её высота равна 2) и горой номер 10 (её высота равна 5). Наибольший спуск равен 4 и находится между горой номер 10 (её высота равна 5) и горой номер 11 (её высота равна 1). Во втором тестовом примере высоты гор равны: 1, 2, 3, 4, 5. Наибольший подъём равен 1 и находится, например, между горой номер 2 (ее высота равна 2) и горой номер 3 (её высота равна 3). Так как в данном горном хребте нет спусков, то величина наибольшего спуска равна 0. В третьем тестовом примере высоты гор равны: 1, 7, 5, 3, 4, 2, 3. Наибольший подъём равен 6 и находится между горой номер 1 (её высота равна 1) и горой номер 2 (её высота равна 7). Наибольший спуск равен 2 и находится между горой номер 2 (её высота равна 7) и горой номер 3 (её высота равна 5). Такой же спуск находится между горой номер 5 (её высота равна 4) и горой номер 6 (её высота равна 2).
[ { "input": "6 11\n...........\n.........*.\n.*.......*.\n**.......*.\n**..*...**.\n***********", "output": "3 4" }, { "input": "5 5\n....*\n...**\n..***\n.****\n*****", "output": "1 0" }, { "input": "8 7\n.......\n.*.....\n.*.....\n.**....\n.**.*..\n.****.*\n.******\n*******", "output": "6 2" }, { "input": "1 1\n*", "output": "0 0" }, { "input": "2 2\n**\n**", "output": "0 0" }, { "input": "1 10\n**********", "output": "0 0" }, { "input": "10 1\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*", "output": "0 0" }, { "input": "5 5\n.....\n.....\n*****\n*****\n*****", "output": "0 0" }, { "input": "10 6\n......\n......\n......\n******\n******\n******\n******\n******\n******\n******", "output": "0 0" }, { "input": "5 11\n***********\n***********\n***********\n***********\n***********", "output": "0 0" }, { "input": "10 10\n..........\n..........\n.....*....\n.....*....\n.*...*....\n.*...*....\n.*..**....\n.*..**.*..\n.*..**.*..\n**********", "output": "5 7" }, { "input": "10 20\n.*..................\n.*......*...........\n.**.....*..*........\n.**.....*..*........\n.**.....*..*........\n.**.....*..*........\n.**.*..**..*........\n.**.*****..*........\n**********.*.......*\n********************", "output": "8 7" }, { "input": "10 30\n....*...........*.............\n.*..*.......*...*.............\n.*..*.....*.*...*............*\n.*..*..*..*.*...*............*\n.*..*..*..*.*...*..........*.*\n.*..*..*..*.*...*....*.....***\n.**.*..*..*.**..*.*..*.....***\n***.*..*..*.**..*.*..**.**.***\n***.**********..***..*****.***\n******************************", "output": "9 8" }, { "input": "10 40\n*..................................*....\n*.....*..............*.............*....\n*.....*..............*............**....\n*..*..***...*...*....*.....*.*....**....\n*.**..***...*...*....*.....*.*...***....\n*.**..****.***..*..*.*..*..*.**..***.*..\n*.**..****.***.**..*.*..*.**.**..***.*..\n*.**..************.*.*..*.*****..***.**.\n*.***.************.*.*.*************.***\n****************************************", "output": "8 9" }, { "input": "20 10\n..........\n..........\n..........\n..........\n..........\n.....*....\n.....*....\n.....*....\n.....*....\n.....*....\n.....*....\n.....*....\n...*.*....\n...*.*....\n...*.*....\n...***....\n..****.*..\n..****.**.\n..****.***\n**********", "output": "10 14" }, { "input": "20 20\n........*...........\n........*........*..\n........*........*..\n.**.....*.......**..\n.**.....*.......**..\n.**.....*.....*.**..\n.**.....*.....*.**..\n.**.....*.....*.**..\n.**.....*.....*.**..\n.**.*...*.....*.**..\n.**.*...*.....*.**..\n.**.*...*....**.**..\n.**.*...*..*.**.**..\n.**.*...*..*.**.**..\n.**.*...**.*.**.***.\n.**.*.*.**.*.**.***.\n.**.*.*.**.*.**.***.\n.**.*.*.****.*******\n.****.******.*******\n********************", "output": "18 15" }, { "input": "30 10\n..........\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n.......*..\n......**..\n......**..\n......**..\n.*....**..\n.*....**..\n.*.*..**..\n.*.*..**..\n.*.*..**..\n.*.*.***..\n.*******..\n.*******.*\n.*********\n**********", "output": "16 27" }, { "input": "1 100\n****************************************************************************************************", "output": "0 0" }, { "input": "100 1\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n.\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*\n*", "output": "0 0" }, { "input": "100 2\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n..\n*.\n*.\n*.\n*.\n*.\n*.\n*.\n*.\n*.\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**\n**", "output": "0 9" }, { "input": "2 100\n*..*....*......................*.*..*.*.*....*.*.*....*.........*.*...*....**.........*.**....*.....\n****************************************************************************************************", "output": "1 1" }, { "input": "5 12\n............\n............\n............\n............\n************", "output": "0 0" }, { "input": "5 12\n............\n************\n************\n************\n************", "output": "0 0" }, { "input": "5 12\n************\n************\n************\n************\n************", "output": "0 0" } ]
46
4,608,000
-1
19,933
584
Marina and Vasya
[ "constructive algorithms", "greedy", "strings" ]
null
null
Marina loves strings of the same length and Vasya loves when there is a third string, different from them in exactly *t* characters. Help Vasya find at least one such string. More formally, you are given two strings *s*1, *s*2 of length *n* and number *t*. Let's denote as *f*(*a*,<=*b*) the number of characters in which strings *a* and *b* are different. Then your task will be to find any string *s*3 of length *n*, such that *f*(*s*1,<=*s*3)<==<=*f*(*s*2,<=*s*3)<==<=*t*. If there is no such string, print <=-<=1.
The first line contains two integers *n* and *t* (1<=≤<=*n*<=≤<=105, 0<=≤<=*t*<=≤<=*n*). The second line contains string *s*1 of length *n*, consisting of lowercase English letters. The third line contain string *s*2 of length *n*, consisting of lowercase English letters.
Print a string of length *n*, differing from string *s*1 and from *s*2 in exactly *t* characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1.
[ "3 2\nabc\nxyc\n", "1 0\nc\nb\n" ]
[ "ayd", "-1\n" ]
none
[ { "input": "3 2\nabc\nxyc", "output": "bac" }, { "input": "1 0\nc\nb", "output": "-1" }, { "input": "1 1\na\na", "output": "b" }, { "input": "2 1\naa\naa", "output": "ab" }, { "input": "3 1\nbcb\nbca", "output": "bcc" }, { "input": "4 3\nccbb\ncaab", "output": "cbca" }, { "input": "4 2\nacbc\nacba", "output": "acab" }, { "input": "4 1\nbcbc\nacab", "output": "-1" }, { "input": "4 2\nacbb\nbabc", "output": "aaba" }, { "input": "5 2\nabaac\nbbbaa", "output": "abbab" }, { "input": "5 2\nabbab\nacbab", "output": "aabaa" }, { "input": "5 3\nbcaaa\ncbacc", "output": "bbabb" }, { "input": "5 3\ncbacb\ncbacb", "output": "cbbaa" }, { "input": "5 1\ncbabb\nbabaa", "output": "-1" }, { "input": "1 0\na\na", "output": "a" }, { "input": "2 2\nbb\ncb", "output": "aa" }, { "input": "2 1\ncc\nba", "output": "ca" }, { "input": "2 0\nbb\nab", "output": "-1" }, { "input": "3 3\naac\nabc", "output": "bca" }, { "input": "1 1\na\nc", "output": "b" }, { "input": "3 0\ncba\ncca", "output": "-1" }, { "input": "2 1\niy\niy", "output": "ia" }, { "input": "2 2\nfg\nfn", "output": "aa" }, { "input": "2 1\npd\nke", "output": "pe" }, { "input": "3 3\nyva\nyvq", "output": "aab" }, { "input": "3 2\npxn\ngxn", "output": "axa" }, { "input": "3 1\nlos\nlns", "output": "las" }, { "input": "4 2\nhbnx\nhwmm", "output": "hbma" }, { "input": "4 4\nqtto\nqtto", "output": "aaaa" }, { "input": "4 3\nchqt\nchet", "output": "caaa" }, { "input": "5 3\nwzcre\nwzcrp", "output": "wzaaa" }, { "input": "5 1\nicahj\nxdvch", "output": "-1" }, { "input": "5 1\npmesm\npzeaq", "output": "-1" }, { "input": "7 4\nycgdbph\nfdtapch", "output": "yctaaah" }, { "input": "10 6\nrnsssbuiaq\npfsbsbuoay", "output": "aasasbuaba" }, { "input": "20 5\ndsjceiztjkrqgpqpnakr\nyijdvcjtjnougpqprrkr", "output": "-1" }, { "input": "100 85\njknccpmanwhxqnxivdgguahjcuyhdrazmbfwoptatlgytakxsfvdzzcsglhmswfxafxyregdbeiwpawrjgwcqrkbhmrfcscgoszf\nhknccpmanwhxjnxivdggeahjcuyhdrazmbfwoqtatlgytdkxsfvdztcsglhmssfxsfxyrngdbeiwpawrjgwcqrkbhmrfcsckoskf", "output": "aknccpmanwhxanxivaaaabaaaaaaaabaaaaaaaabaaaaabaaaaaaaaaaaaaaaaaabaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaa" }, { "input": "1 0\nz\nz", "output": "z" }, { "input": "1 1\nz\ny", "output": "a" }, { "input": "1 1\nz\nz", "output": "a" }, { "input": "1 0\nz\ny", "output": "-1" }, { "input": "10 1\ngjsywvenzc\nfssywvenzc", "output": "gssywvenzc" }, { "input": "20 2\nywpcwcwgkhdeonzbeamf\ngdcmwcwgkhdeonzbeamf", "output": "ywcmwcwgkhdeonzbeamf" } ]
61
5,734,400
0
19,967
22
Segments
[ "greedy", "sortings" ]
D. Segments
1
256
You are given *n* segments on the Ox-axis. You can drive a nail in any integer point on the Ox-axis line nail so, that all segments containing this point, are considered nailed down. If the nail passes through endpoint of some segment, this segment is considered to be nailed too. What is the smallest number of nails needed to nail all the segments down?
The first line of the input contains single integer number *n* (1<=≤<=*n*<=≤<=1000) — amount of segments. Following *n* lines contain descriptions of the segments. Each description is a pair of integer numbers — endpoints coordinates. All the coordinates don't exceed 10000 by absolute value. Segments can degenarate to points.
The first line should contain one integer number — the smallest number of nails needed to nail all the segments down. The second line should contain coordinates of driven nails separated by space in any order. If the answer is not unique, output any.
[ "2\n0 2\n2 5\n", "5\n0 3\n4 2\n4 8\n8 10\n7 7\n" ]
[ "1\n2 ", "3\n7 10 3\n" ]
none
[ { "input": "2\n0 2\n2 5", "output": "1\n2 " }, { "input": "5\n0 3\n4 2\n4 8\n8 10\n7 7", "output": "3\n3 7 10 " }, { "input": "3\n40 -83\n52 -80\n-21 -4", "output": "1\n-4 " }, { "input": "4\n67 -88\n37 -62\n-26 91\n-99 -50", "output": "2\n-50 91 " }, { "input": "5\n45 58\n22 6\n-32 36\n49 -37\n43 62", "output": "2\n22 58 " }, { "input": "10\n47 -85\n71 40\n-87 64\n76 73\n52 -51\n-20 -57\n-14 -77\n-45 -11\n-56 -48\n81 20", "output": "4\n-48 -11 71 76 " }, { "input": "35\n-47 61\n-73 -15\n9 43\n43 -49\n13 -6\n48 -65\n49 -84\n-6 76\n40 6\n-41 11\n-63 -42\n-7 -66\n47 -98\n-17 61\n79 99\n47 -98\n61 31\n85 72\n-50 -17\n-67 45\n14 -50\n-45 61\n-6 -96\n-47 -83\n56 -58\n-69 -15\n14 20\n-95 -82\n47 93\n-4 -7\n70 9\n91 -18\n-71 31\n55 -20\n81 -8", "output": "6\n-82 -42 -4 20 61 85 " }, { "input": "1\n0 0", "output": "1\n0 " }, { "input": "4\n1 1\n0 0\n10000 10000\n-10000 -10000", "output": "4\n-10000 0 1 10000 " }, { "input": "3\n55 55\n55 55\n55 55", "output": "1\n55 " }, { "input": "4\n55 55\n55 55\n55 55\n55 56", "output": "1\n55 " }, { "input": "1\n-2244 5023", "output": "1\n5023 " } ]
109
409,600
3.944737
20,001
78
Beaver Game
[ "dp", "games", "number theory" ]
C. Beaver Game
1
256
Two beavers, Timur and Marsel, play the following game. There are *n* logs, each of exactly *m* meters in length. The beavers move in turns. For each move a beaver chooses a log and gnaws it into some number (more than one) of equal parts, the length of each one is expressed by an integer and is no less than *k* meters. Each resulting part is also a log which can be gnawed in future by any beaver. The beaver that can't make a move loses. Thus, the other beaver wins. Timur makes the first move. The players play in the optimal way. Determine the winner.
The first line contains three integers *n*, *m*, *k* (1<=≤<=*n*,<=*m*,<=*k*<=≤<=109).
Print "Timur", if Timur wins, or "Marsel", if Marsel wins. You should print everything without the quotes.
[ "1 15 4\n", "4 9 5\n" ]
[ "Timur", "Marsel" ]
In the first sample the beavers only have one log, of 15 meters in length. Timur moves first. The only move he can do is to split the log into 3 parts each 5 meters in length. Then Marsel moves but he can't split any of the resulting logs, as *k* = 4. Thus, the winner is Timur. In the second example the beavers have 4 logs 9 meters in length. Timur can't split any of them, so that the resulting parts possessed the length of not less than 5 meters, that's why he loses instantly.
[ { "input": "1 15 4", "output": "Timur" }, { "input": "4 9 5", "output": "Marsel" }, { "input": "14 30 9", "output": "Marsel" }, { "input": "81 180 53", "output": "Timur" }, { "input": "225 187 20", "output": "Marsel" }, { "input": "501 840 11", "output": "Timur" }, { "input": "3 11 6", "output": "Marsel" }, { "input": "1 2 1", "output": "Timur" }, { "input": "1 1 2", "output": "Marsel" }, { "input": "1 1 1", "output": "Marsel" }, { "input": "1 2 2", "output": "Marsel" }, { "input": "2 1 1", "output": "Marsel" }, { "input": "2 1 2", "output": "Marsel" }, { "input": "2 2 1", "output": "Marsel" }, { "input": "2 2 2", "output": "Marsel" }, { "input": "1000000000 1000000000 1000000000", "output": "Marsel" }, { "input": "999999999 735134400 1", "output": "Timur" }, { "input": "999999999 735134400 100", "output": "Timur" }, { "input": "787965358 999999986 2", "output": "Marsel" }, { "input": "588462355 999999986 2", "output": "Timur" }, { "input": "994427144 999999986 10", "output": "Marsel" }, { "input": "36948983 999999986 10", "output": "Timur" }, { "input": "431525232 999999986 250000000", "output": "Marsel" }, { "input": "502291145 999999986 250000000", "output": "Timur" }, { "input": "612116696 999999986 499999993", "output": "Marsel" }, { "input": "559551717 999999986 499999993", "output": "Timur" }, { "input": "844774784 999999894 30", "output": "Marsel" }, { "input": "776970029 999999894 30", "output": "Timur" }, { "input": "726014914 999950443 31643", "output": "Marsel" }, { "input": "350735191 999950443 31643", "output": "Timur" }, { "input": "114514534 999950443 31601", "output": "Marsel" }, { "input": "689283015 999950443 31601", "output": "Timur" }, { "input": "585863414 999002449 31607", "output": "Marsel" }, { "input": "271824601 999002449 31607", "output": "Timur" }, { "input": "57784860 999002449 1", "output": "Marsel" }, { "input": "844093323 999002449 1", "output": "Timur" }, { "input": "623425842 999002449 10", "output": "Marsel" }, { "input": "283831003 999002449 10", "output": "Timur" }, { "input": "884219706 735134400 1", "output": "Marsel" }, { "input": "519313933 735134400 1", "output": "Timur" }, { "input": "483982088 735134400 2", "output": "Marsel" }, { "input": "966249765 735134400 2", "output": "Timur" }, { "input": "362305942 735134400 367567201", "output": "Marsel" }, { "input": "311659875 735134400 367567201", "output": "Marsel" }, { "input": "717978658 735134400 100000000", "output": "Marsel" }, { "input": "367890897 735134400 100000000", "output": "Timur" }, { "input": "52049820 735134400 200000000", "output": "Marsel" }, { "input": "395681265 735134400 200000000", "output": "Timur" }, { "input": "195335332 735134400 300000000", "output": "Marsel" }, { "input": "2240305 735134400 300000000", "output": "Timur" }, { "input": "166548792 901800900 30030", "output": "Marsel" }, { "input": "708577575 901800900 30030", "output": "Timur" }, { "input": "304908040 901800900 901800900", "output": "Marsel" }, { "input": "799852313 901800900 901800900", "output": "Marsel" }, { "input": "624970498 901800900 901800901", "output": "Marsel" }, { "input": "373293701 901800900 901800901", "output": "Marsel" }, { "input": "1608018 999999937 1", "output": "Marsel" }, { "input": "830273749 999999937 1", "output": "Timur" }, { "input": "859437048 999999937 2", "output": "Marsel" }, { "input": "672396661 999999937 2", "output": "Marsel" }, { "input": "763677180 999999937 111", "output": "Marsel" }, { "input": "605998637 999999937 111", "output": "Marsel" }, { "input": "938316524 999999937 1000000000", "output": "Marsel" }, { "input": "885233939 999999937 1000000000", "output": "Marsel" }, { "input": "522619786 1000000000 500000000", "output": "Marsel" }, { "input": "780954779 1000000000 500000000", "output": "Timur" }, { "input": "568419816 1000000000 500000001", "output": "Marsel" }, { "input": "300872687 1000000000 500000001", "output": "Marsel" }, { "input": "628290298 1000000000 1250", "output": "Marsel" }, { "input": "56021277 1000000000 1250", "output": "Timur" }, { "input": "538604320 536870912 268435456", "output": "Marsel" }, { "input": "719320775 536870912 268435456", "output": "Timur" }, { "input": "911637544 536870912 134217728", "output": "Marsel" }, { "input": "104077283 536870912 134217728", "output": "Timur" }, { "input": "167283896 935821597 191101", "output": "Marsel" }, { "input": "753528595 935821597 191101", "output": "Timur" }, { "input": "907605112 935821597 11274959", "output": "Marsel" }, { "input": "327082339 935821597 11274959", "output": "Timur" }, { "input": "316344442 935821597 22824917", "output": "Marsel" }, { "input": "244672063 935821597 22824917", "output": "Timur" }, { "input": "1000000000 1000000000 36", "output": "Marsel" }, { "input": "1 1000000000 1000000000", "output": "Marsel" }, { "input": "2 10 1", "output": "Marsel" }, { "input": "1 999999991 488888888", "output": "Marsel" }, { "input": "4 9 4", "output": "Marsel" }, { "input": "1 3 2", "output": "Marsel" }, { "input": "1 1000000000 1", "output": "Timur" }, { "input": "1 999999938 999999937", "output": "Marsel" }, { "input": "1 7 1", "output": "Timur" }, { "input": "101 1 1", "output": "Marsel" }, { "input": "999999999 1000000000 1000000000", "output": "Marsel" }, { "input": "1 200000 100000", "output": "Timur" }, { "input": "1 9 3", "output": "Timur" } ]
186
1,024,000
-1
20,060
940
Machine Learning
[ "brute force", "data structures" ]
null
null
You come home and fell some unpleasant smell. Where is it coming from? You are given an array *a*. You have to answer the following queries: 1. You are given two integers *l* and *r*. Let *c**i* be the number of occurrences of *i* in *a**l*:<=*r*, where *a**l*:<=*r* is the subarray of *a* from *l*-th element to *r*-th inclusive. Find the Mex of {*c*0,<=*c*1,<=...,<=*c*109} 1. You are given two integers *p* to *x*. Change *a**p* to *x*. The Mex of a multiset of numbers is the smallest non-negative integer not in the set. Note that in this problem all elements of *a* are positive, which means that *c*0 = 0 and 0 is never the answer for the query of the second type.
The first line of input contains two integers *n* and *q* (1<=≤<=*n*,<=*q*<=≤<=100<=000) — the length of the array and the number of queries respectively. The second line of input contains *n* integers — *a*1, *a*2, ..., *a**n* (1<=≤<=*a**i*<=≤<=109). Each of the next *q* lines describes a single query. The first type of query is described by three integers *t**i*<==<=1, *l**i*, *r**i*, where 1<=≤<=*l**i*<=≤<=*r**i*<=≤<=*n* — the bounds of the subarray. The second type of query is described by three integers *t**i*<==<=2, *p**i*, *x**i*, where 1<=≤<=*p**i*<=≤<=*n* is the index of the element, which must be changed and 1<=≤<=*x**i*<=≤<=109 is the new value.
For each query of the first type output a single integer  — the Mex of {*c*0,<=*c*1,<=...,<=*c*109}.
[ "10 4\n1 2 3 1 1 2 2 2 9 9\n1 1 1\n1 2 8\n2 7 1\n1 2 8\n" ]
[ "2\n3\n2\n" ]
The subarray of the first query consists of the single element — 1. The subarray of the second query consists of four 2s, one 3 and two 1s. The subarray of the fourth query consists of three 1s, three 2s and one 3.
[ { "input": "10 4\n1 2 3 1 1 2 2 2 9 9\n1 1 1\n1 2 8\n2 7 1\n1 2 8", "output": "2\n3\n2" } ]
15
0
-1
20,111
840
Leha and Function
[ "combinatorics", "greedy", "math", "number theory", "sortings" ]
null
null
Leha like all kinds of strange things. Recently he liked the function *F*(*n*,<=*k*). Consider all possible *k*-element subsets of the set [1,<=2,<=...,<=*n*]. For subset find minimal element in it. *F*(*n*,<=*k*) — mathematical expectation of the minimal element among all *k*-element subsets. But only function does not interest him. He wants to do interesting things with it. Mom brought him two arrays *A* and *B*, each consists of *m* integers. For all *i*,<=*j* such that 1<=≤<=*i*,<=*j*<=≤<=*m* the condition *A**i*<=≥<=*B**j* holds. Help Leha rearrange the numbers in the array *A* so that the sum is maximally possible, where *A*' is already rearranged array.
First line of input data contains single integer *m* (1<=≤<=*m*<=≤<=2·105) — length of arrays *A* and *B*. Next line contains *m* integers *a*1,<=*a*2,<=...,<=*a**m* (1<=≤<=*a**i*<=≤<=109) — array *A*. Next line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m* (1<=≤<=*b**i*<=≤<=109) — array *B*.
Output *m* integers *a*'1,<=*a*'2,<=...,<=*a*'*m* — array *A*' which is permutation of the array *A*.
[ "5\n7 3 5 3 4\n2 1 3 2 3\n", "7\n4 6 5 8 8 2 6\n2 1 2 2 1 1 2\n" ]
[ "4 7 3 5 3\n", "2 6 4 5 8 8 6\n" ]
none
[ { "input": "5\n7 3 5 3 4\n2 1 3 2 3", "output": "4 7 3 5 3" }, { "input": "7\n4 6 5 8 8 2 6\n2 1 2 2 1 1 2", "output": "2 6 4 5 8 8 6" } ]
2,000
19,353,600
0
20,126