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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
557 | Arthur and Table | [
"brute force",
"data structures",
"dp",
"greedy",
"math",
"sortings"
] | null | null | Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable.
In total the table Arthur bought has *n* legs, the length of the *i*-th leg is *l**i*.
Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number *d**i*Β βΒ the amount of energy that he spends to remove the *i*-th leg.
A table with *k* legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths.
Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. | The first line of the input contains integer *n* (1<=β€<=*n*<=β€<=105)Β βΒ the initial number of legs in the table Arthur bought.
The second line of the input contains a sequence of *n* integers *l**i* (1<=β€<=*l**i*<=β€<=105), where *l**i* is equal to the length of the *i*-th leg of the table.
The third line of the input contains a sequence of *n* integers *d**i* (1<=β€<=*d**i*<=β€<=200), where *d**i* is the number of energy units that Arthur spends on removing the *i*-th leg off the table. | Print a single integer β the minimum number of energy units that Arthur needs to spend in order to make the table stable. | [
"2\n1 5\n3 2\n",
"3\n2 4 4\n1 1 1\n",
"6\n2 2 1 1 3 3\n4 3 5 5 2 1\n"
] | [
"2\n",
"0\n",
"8\n"
] | none | [
{
"input": "2\n1 5\n3 2",
"output": "2"
},
{
"input": "3\n2 4 4\n1 1 1",
"output": "0"
},
{
"input": "6\n2 2 1 1 3 3\n4 3 5 5 2 1",
"output": "8"
},
{
"input": "10\n20 1 15 17 11 2 15 3 16 3\n129 114 183 94 169 16 18 104 49 146",
"output": "652"
},
{
"input": "10\n15 16 19 5 14 19 6 17 7 19\n140 183 186 195 67 25 70 56 54 132",
"output": "387"
},
{
"input": "20\n24 15 7 18 6 18 11 22 18 20 28 8 23 17 22 22 23 23 30 23\n24 137 173 189 43 164 2 11 142 153 16 200 53 58 150 199 169 172 38 78",
"output": "1111"
},
{
"input": "30\n20 27 26 17 29 9 25 13 6 21 15 1 14 22 26 2 15 18 20 23 27 9 15 29 11 2 19 23 14 12\n133 199 22 151 9 134 196 119 197 189 117 84 156 6 61 195 98 197 145 75 142 85 187 24 159 3 60 138 156 161",
"output": "2804"
},
{
"input": "5\n1 1 2 2 3\n2 2 2 2 3",
"output": "5"
},
{
"input": "5\n1 1 2 2 3\n2 2 2 2 7",
"output": "8"
},
{
"input": "10\n1 1 3 4 5 6 7 8 9 10\n10 10 4 2 3 4 2 2 2 2",
"output": "21"
},
{
"input": "1\n5\n4",
"output": "0"
},
{
"input": "2\n3 3\n4 5",
"output": "0"
},
{
"input": "2\n4 5\n3 3",
"output": "3"
},
{
"input": "3\n3 3 3\n1 2 3",
"output": "0"
},
{
"input": "3\n1 2 3\n3 3 3",
"output": "6"
}
] | 92 | 20,172,800 | 0 | 4,190 |
|
652 | Gabriel and Caterpillar | [
"implementation",
"math"
] | null | null | The 9-th grade student Gabriel noticed a caterpillar on a tree when walking around in a forest after the classes. The caterpillar was on the height *h*1 cm from the ground. On the height *h*2 cm (*h*2<=><=*h*1) on the same tree hung an apple and the caterpillar was crawling to the apple.
Gabriel is interested when the caterpillar gets the apple. He noted that the caterpillar goes up by *a* cm per hour by day and slips down by *b* cm per hour by night.
In how many days Gabriel should return to the forest to see the caterpillar get the apple. You can consider that the day starts at 10 am and finishes at 10 pm. Gabriel's classes finish at 2 pm. You can consider that Gabriel noticed the caterpillar just after the classes at 2 pm.
Note that the forest is magic so the caterpillar can slip down under the ground and then lift to the apple. | The first line contains two integers *h*1,<=*h*2 (1<=β€<=*h*1<=<<=*h*2<=β€<=105) β the heights of the position of the caterpillar and the apple in centimeters.
The second line contains two integers *a*,<=*b* (1<=β€<=*a*,<=*b*<=β€<=105) β the distance the caterpillar goes up by day and slips down by night, in centimeters per hour. | Print the only integer *k* β the number of days Gabriel should wait to return to the forest and see the caterpillar getting the apple.
If the caterpillar can't get the apple print the only integer <=-<=1. | [
"10 30\n2 1\n",
"10 13\n1 1\n",
"10 19\n1 2\n",
"1 50\n5 4\n"
] | [
"1\n",
"0\n",
"-1\n",
"1\n"
] | In the first example at 10 pm of the first day the caterpillar gets the height 26. At 10 am of the next day it slips down to the height 14. And finally at 6 pm of the same day the caterpillar gets the apple.
Note that in the last example the caterpillar was slipping down under the ground and getting the apple on the next day. | [
{
"input": "10 30\n2 1",
"output": "1"
},
{
"input": "10 13\n1 1",
"output": "0"
},
{
"input": "10 19\n1 2",
"output": "-1"
},
{
"input": "1 50\n5 4",
"output": "1"
},
{
"input": "1 1000\n2 1",
"output": "82"
},
{
"input": "999 1000\n1 1",
"output": "0"
},
{
"input": "999 1000\n1 1000",
"output": "0"
},
{
"input": "1 1000\n999 1",
"output": "0"
},
{
"input": "1 1000\n100 99",
"output": "17"
},
{
"input": "500 509\n1 1",
"output": "-1"
},
{
"input": "500 555\n6 1",
"output": "1"
},
{
"input": "1 100000\n2 1",
"output": "8332"
},
{
"input": "99990 100000\n1 1",
"output": "-1"
},
{
"input": "90000 100000\n2 1",
"output": "832"
},
{
"input": "10 100000\n1 100000",
"output": "-1"
},
{
"input": "1 41\n5 6",
"output": "0"
},
{
"input": "1 100000\n1 100000",
"output": "-1"
},
{
"input": "1 9\n1 1",
"output": "0"
},
{
"input": "8 16\n1 12",
"output": "0"
},
{
"input": "14 30\n2 1",
"output": "0"
},
{
"input": "7245 77828\n6224 92468",
"output": "-1"
},
{
"input": "43951 66098\n1604 35654",
"output": "-1"
},
{
"input": "1 2\n4 3",
"output": "0"
},
{
"input": "90493 94279\n468 49",
"output": "1"
},
{
"input": "1 50\n3 1",
"output": "2"
},
{
"input": "26300 88310\n7130 351",
"output": "1"
},
{
"input": "1 17\n2 2",
"output": "0"
},
{
"input": "10718 75025\n7083 6958",
"output": "6"
},
{
"input": "1 10\n1 100000",
"output": "-1"
},
{
"input": "1 190\n10 1",
"output": "2"
},
{
"input": "24951 85591\n3090 8945",
"output": "-1"
},
{
"input": "1 25\n3 2",
"output": "0"
},
{
"input": "27043 88418\n7273 7",
"output": "1"
},
{
"input": "35413 75637\n4087 30166",
"output": "-1"
},
{
"input": "1 18\n2 3",
"output": "-1"
},
{
"input": "1 16\n2 2",
"output": "0"
},
{
"input": "1 18\n2 1",
"output": "1"
},
{
"input": "1 10\n2 2",
"output": "0"
},
{
"input": "1 30\n2 1",
"output": "2"
},
{
"input": "1 100000\n10000 100000",
"output": "-1"
},
{
"input": "4444 33425\n2758 44",
"output": "1"
},
{
"input": "1 100000\n10 99910",
"output": "-1"
},
{
"input": "12 100\n6 11",
"output": "-1"
},
{
"input": "100 100000\n10 11",
"output": "-1"
},
{
"input": "28473 80380\n2568 95212",
"output": "-1"
},
{
"input": "10 105\n10 1",
"output": "1"
},
{
"input": "4642 39297\n3760 451",
"output": "1"
},
{
"input": "1 90\n10 1",
"output": "1"
},
{
"input": "2 100\n1 100000",
"output": "-1"
},
{
"input": "1 100000\n1000 100000",
"output": "-1"
},
{
"input": "1 45\n1 100000",
"output": "-1"
},
{
"input": "12 1000\n100 1",
"output": "1"
},
{
"input": "64635 76564\n100 34238",
"output": "-1"
},
{
"input": "10 90\n10 12",
"output": "0"
},
{
"input": "49238 81395\n3512 251",
"output": "1"
},
{
"input": "6497 62133\n309 50077",
"output": "-1"
},
{
"input": "1 100\n1 100000",
"output": "-1"
},
{
"input": "1 10000\n1 10000",
"output": "-1"
},
{
"input": "55674 93249\n846 1",
"output": "4"
},
{
"input": "10 90\n9 10",
"output": "-1"
},
{
"input": "23110 69794\n171 808",
"output": "-1"
},
{
"input": "1 100000\n1 10000",
"output": "-1"
},
{
"input": "1 9\n1 2",
"output": "0"
},
{
"input": "58750 81357\n2 98022",
"output": "-1"
},
{
"input": "82125 89348\n894 91369",
"output": "-1"
},
{
"input": "25401 53663\n957 30449",
"output": "-1"
},
{
"input": "2 12\n1 2",
"output": "-1"
},
{
"input": "1 10000\n1 100000",
"output": "-1"
},
{
"input": "1 100000\n1 99999",
"output": "-1"
},
{
"input": "1 149\n8 2",
"output": "2"
},
{
"input": "3 100\n1 1",
"output": "-1"
},
{
"input": "1 18\n2 2",
"output": "-1"
},
{
"input": "1 77\n9 1",
"output": "1"
},
{
"input": "7330 94486\n968 141",
"output": "9"
},
{
"input": "89778 98176\n863 61",
"output": "1"
},
{
"input": "1 70\n6 5",
"output": "2"
}
] | 62 | 0 | 3 | 4,196 |
|
886 | Petya and Catacombs | [
"dsu",
"greedy",
"implementation",
"trees"
] | null | null | A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really experienced, his exploration is just walking through the catacombs.
Catacombs consist of several rooms and bidirectional passages between some pairs of them. Some passages can connect a room to itself and since the passages are built on different depths they do not intersect each other. Every minute Petya arbitrary chooses a passage from the room he is currently in and then reaches the room on the other end of the passage in exactly one minute. When he enters a room at minute *i*, he makes a note in his logbook with number *t**i*:
- If Petya has visited this room before, he writes down the minute he was in this room last time; - Otherwise, Petya writes down an arbitrary non-negative integer strictly less than current minute *i*.
Initially, Petya was in one of the rooms at minute 0, he didn't write down number *t*0.
At some point during his wandering Petya got tired, threw out his logbook and went home. Vasya found his logbook and now he is curious: what is the minimum possible number of rooms in Paris catacombs according to Petya's logbook? | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=2Β·105) β then number of notes in Petya's logbook.
The second line contains *n* non-negative integers *t*1,<=*t*2,<=...,<=*t**n* (0<=β€<=*t**i*<=<<=*i*) β notes in the logbook. | In the only line print a single integer β the minimum possible number of rooms in Paris catacombs. | [
"2\n0 0\n",
"5\n0 1 0 1 3\n"
] | [
"2\n",
"3\n"
] | In the first sample, sequence of rooms Petya visited could be, for example 1βββ1βββ2, 1βββ2βββ1 or 1βββ2βββ3. The minimum possible number of rooms is 2.
In the second sample, the sequence could be 1βββ2βββ3βββ1βββ2βββ1. | [
{
"input": "2\n0 0",
"output": "2"
},
{
"input": "5\n0 1 0 1 3",
"output": "3"
},
{
"input": "7\n0 1 0 0 0 0 0",
"output": "6"
},
{
"input": "100\n0 0 0 0 0 0 1 4 4 0 2 2 4 1 7 1 11 0 8 4 12 12 3 0 3 2 2 4 3 9 1 5 4 6 9 14 6 2 4 18 7 7 19 11 20 13 17 16 0 34 2 6 12 27 9 4 29 22 4 20 20 17 17 20 37 53 17 3 3 15 1 46 11 24 31 6 12 6 11 18 13 1 5 0 19 10 24 41 16 41 18 52 46 39 16 30 18 23 53 13",
"output": "66"
},
{
"input": "100\n0 0 0 0 1 2 0 0 3 3 2 2 6 4 1 6 2 9 8 0 2 0 2 2 0 0 10 0 4 20 4 11 3 9 0 3 8 2 6 3 13 2 1 23 20 20 16 7 1 37 6 1 25 25 14 30 6 23 18 3 2 16 0 4 37 9 4 6 2 14 15 11 16 35 36 7 32 26 8 1 0 37 35 38 27 3 16 8 3 7 7 25 13 13 30 11 5 28 0 12",
"output": "71"
},
{
"input": "1\n0",
"output": "1"
},
{
"input": "14\n0 0 1 1 2 2 3 3 4 4 5 5 6 6",
"output": "8"
},
{
"input": "2\n0 1",
"output": "1"
}
] | 218 | 14,643,200 | 3 | 4,201 |
|
629 | Famil Door and Brackets | [
"dp",
"strings"
] | null | null | As Famil Doorβs birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length *n* more than any other strings!
The sequence of round brackets is called valid if and only if:
1. the total number of opening brackets is equal to the total number of closing brackets; 1. for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string *s* of length *m* (*m*<=β€<=*n*) and want to complete it to obtain a valid sequence of brackets of length *n*. He is going to pick some strings *p* and *q* consisting of round brackets and merge them in a string *p*<=+<=*s*<=+<=*q*, that is add the string *p* at the beginning of the string *s* and string *q* at the end of the string *s*.
Now he wonders, how many pairs of strings *p* and *q* exists, such that the string *p*<=+<=*s*<=+<=*q* is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109<=+<=7. | First line contains *n* and *m* (1<=β€<=*m*<=β€<=*n*<=β€<=100<=000,<=*n*<=-<=*m*<=β€<=2000)Β β the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string *s* of length *m* consisting of characters '(' and ')' only. | Print the number of pairs of string *p* and *q* such that *p*<=+<=*s*<=+<=*q* is a valid sequence of round brackets modulo 109<=+<=7. | [
"4 1\n(\n",
"4 4\n(())\n",
"4 3\n(((\n"
] | [
"4\n",
"1\n",
"0\n"
] | In the first sample there are four different valid pairs:
1. *p*β=β"(", *q*β=β"))" 1. *p*β=β"()", *q*β=β")" 1. *p*β=β"", *q*β=β"())" 1. *p*β=β"", *q*β=β")()"
In the second sample the only way to obtain a desired string is choose empty *p* and *q*.
In the third sample there is no way to get a valid sequence of brackets. | [
{
"input": "4 1\n(",
"output": "4"
},
{
"input": "4 4\n(())",
"output": "1"
},
{
"input": "4 3\n(((",
"output": "0"
},
{
"input": "875 50\n)))((())()))((()(())))))())))((((((()))))))()(((((",
"output": "0"
},
{
"input": "1980 464\n))(()()))(((((((((()))))))(()(((()((()))()()())()))()))(()))))))(())((())))()())()((())())()())))(())()(()))(()())()((((()))())()(())))))(()()(()(((((()(()()))(((()))(())))))()())(())))))())()()((())))))))((()(())))))()()(()((()((()()))(()))(())(()))()((((())()()))))))()(())))()(()())()())(((((()))())))())())(()))()(()))())((())((((()(()(())))(((()()))))()()()))))((()())()((())())))())))()(()(()()(((((()((((()))()(())()))))()(()))(()(((((((()((()(())))(((((())",
"output": "854368836"
},
{
"input": "1542 282\n())())()((()(()))()((())()))((())(()))))(()()))(())((()))()((()())())()))((())(((()(())((()()())((((())))((()((((()(()()))))(()(()()(())()((())())())))))()()())))(()((((()))(()(()(()(()))())((()()()()(()(()))())(((()(())()(())()()())))()))())(()))(((())()))((())()(())))))(())))()()",
"output": "631927032"
},
{
"input": "2 2\n)(",
"output": "0"
},
{
"input": "2 2\n))",
"output": "0"
}
] | 327 | 37,273,600 | 0 | 4,202 |
|
98 | Help King | [
"implementation",
"probabilities",
"trees"
] | B. Help King | 2 | 256 | This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive.
Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast.
Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage.
The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be *n* noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1<=/<=*n*). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task. | The first line contains a single integer *n* from the problem's statement (1<=β€<=*n*<=β€<=10000). | Print the sought expected number of tosses as an irreducible fraction in the following form: "*a*/*b*" (without the quotes) without leading zeroes. | [
"2\n",
"3\n",
"4\n"
] | [
"1/1\n",
"8/3\n",
"2/1\n"
] | none | [
{
"input": "2",
"output": "1/1"
},
{
"input": "3",
"output": "8/3"
},
{
"input": "4",
"output": "2/1"
},
{
"input": "8",
"output": "3/1"
},
{
"input": "7",
"output": "24/7"
},
{
"input": "6",
"output": "11/3"
},
{
"input": "1",
"output": "0/1"
},
{
"input": "5",
"output": "18/5"
},
{
"input": "96",
"output": "23/3"
},
{
"input": "54",
"output": "377/57"
},
{
"input": "49",
"output": "1985714/299593"
},
{
"input": "57",
"output": "1118/171"
},
{
"input": "21",
"output": "38/7"
},
{
"input": "43",
"output": "896/129"
},
{
"input": "56",
"output": "45/7"
},
{
"input": "46",
"output": "13719/2047"
},
{
"input": "91",
"output": "704/91"
},
{
"input": "13",
"output": "306/65"
},
{
"input": "82",
"output": "7739/1025"
},
{
"input": "69",
"output": "32740246/4194303"
},
{
"input": "77",
"output": "8215881550/1073741823"
},
{
"input": "27",
"output": "320/57"
},
{
"input": "63",
"output": "128/21"
},
{
"input": "60",
"output": "94/15"
},
{
"input": "42",
"output": "45/7"
},
{
"input": "29",
"output": "89074/16385"
},
{
"input": "99",
"output": "82792/10923"
},
{
"input": "19",
"output": "2936/513"
},
{
"input": "89",
"output": "15942/2047"
},
{
"input": "356",
"output": "20036/2047"
},
{
"input": "377",
"output": "42877948701338/4398046511105"
},
{
"input": "376",
"output": "81794781/8388607"
},
{
"input": "199",
"output": "5416016912792671923933831206744/633825300114114700748351602687"
},
{
"input": "563",
"output": "13880251801665520090148870069821814422429790384486504048582895486382118580803670864520/1295112594817152713946307937882345937761604559368093014167874939825936190136805665451"
},
{
"input": "768",
"output": "32/3"
},
{
"input": "777",
"output": "242912325346/22906492245"
},
{
"input": "721",
"output": "24323770714557246/2251799813685247"
},
{
"input": "629",
"output": "49843473149688266962934/4722366482869645213695"
},
{
"input": "589",
"output": "13383001136884230836493585742/1237940039285380274899124223"
},
{
"input": "698",
"output": "261084475895343697268604849722313704014143081638278003/23945242826029513411849172299223580994042798784118785"
},
{
"input": "897",
"output": "6303910445469575479185224346195379698702/604946430081668379490443746545365709255"
},
{
"input": "100",
"output": "1548/205"
},
{
"input": "898",
"output": "54181807382553033285317903788494851/5192296858534827628530496329220097"
},
{
"input": "778",
"output": "266081190240146347461776796003542832080357458393960689731819/25108406941546723055343157692830665664409421777856138051585"
},
{
"input": "408",
"output": "481/51"
},
{
"input": "915",
"output": "24489920882263416/2329134352741105"
},
{
"input": "659",
"output": "11506864999305980525685467247724160971763964558240405270397991163704744844086671519176045618744739064/1093625362391505962186251113558810682676584715446606218212885303204976499599687961611756588511526913"
},
{
"input": "380",
"output": "17089848754/1762037865"
},
{
"input": "826",
"output": "28584112877549284603281069376719144007233968270323045/2660582536225501490205463588802620110449199864902087"
},
{
"input": "570",
"output": "81780152549/7635497415"
},
{
"input": "8947",
"output": "9221776965061394108063074537665193339719130234791259353035383369908276748232005329587397633526133839630977366288410166284245740877083371222474025826991630911939336371916388075479778450420492837649418370715068725438291562232309367997828789596177392059429297899551357796421577750411209045517378683903876507895356659650107620108241824167563644901510340265372019179673453724120802026261156279978925379653814720997930734349648232984115741566886698873835913569553263892609588155620638724704727598288959685621550249737..."
},
{
"input": "5379",
"output": "369970662679510300707473720654925140999880897391589444623141436368705516892112173326991585567498714963884175295883677699016/27543998699593691622996471168032126659516861790178384136860366469766907968179639409382883768321470201982967167709306596011"
},
{
"input": "2614",
"output": "467288820759363880397722656040592964020719930972683440703193351374700405080631777580381050558531644513848619882285655907104502715940077345333622944378242416293600447930921066507506568704500773773633/37375513539561023231108477793896786533525327931380202951304745106630862169773485150256437750311906506986637800026885384689161869077507588081685801531164378630160340372359290471078905382884178132993"
},
{
"input": "6212",
"output": "718828780749497450032379379564/52818775009509558395695966891"
},
{
"input": "3586",
"output": "1026714465140783036738474632330359205383783819874722347437634384119469704928190591460057095203615899196742515080053210107395/82631996098781074868989413504096379978550585370535152410581099409300723904538918228148651304964410605948901503127919788033"
},
{
"input": "6629",
"output": "2887307659130237302642554273423838808127851411579740408603950850448329124209429578113205979182558694432261838498919298006073282090488747091133824076367223385537560048772408256051559991222428913480943619030448796454451464748405569577077070495809049315150455270612613741854545427076463694803465943631755282225609026765330663439059215627258266275926212560658364641003632351846514061002732653254081394744335331237509798266695411546509090369792904212760685221160746857281893822469878668200610823671894328098385881817..."
},
{
"input": "9861",
"output": "48205401549715737511233606679265031152996036546952288925330285615396586708825956490117819026249283326464870369563186432330830706792989284262067110327971987394928549360770263191516012692848869076552182149927367930957872055698792311482405327082065553619653685393875355497796244989262571944371234203749880818792381890143341459002264984197001955297069409069640536538793687219440766877509028035223421720178766484630650779161811795215598122460003343408475412680746717692338/3290879900222849827608052539430658202162644..."
},
{
"input": "1649",
"output": "1103258059369546/93824992236885"
},
{
"input": "1108",
"output": "829108303866668/70368744177665"
},
{
"input": "6704",
"output": "3747509385261755254210316751920800389821077334621726013635892116/274250759553534340359161530426225084163789310938929977224768171"
},
{
"input": "8771",
"output": "3770311067804988086442280897982793233953991174834038671591744622664235928848771481024534828492198884087581040596792755307166862630362254070801530083149662062951269890316117808935983174156710048296936374305714276676638609402088826404322038255398708095256451326373150897864019805504212731596330281332804357336587199175247125958363190591680678576959242590696722761770737808874948948372052533640442104091203823298252400438183921494229420351844306480773904754871502489649028646657382266084070860349033005494965904934..."
},
{
"input": "4710",
"output": "62214231544449127/4503599627370495"
},
{
"input": "2337",
"output": "6429067313146769086191243390708972817399071295570593226/510831846955296286119449009050103061206246374061200725"
},
{
"input": "7708",
"output": "39413297508974631726391031213171923271142554912679270458193728426423636026646334915727139650713682692479792411879633606353758285856351703126/2977131414714805823690030317109266572712515013375254774912983855843898524112477893944078543723575564536883288499266264815757728270805630975"
},
{
"input": "5484",
"output": "211086977989859276474150/15111572745182864683827"
},
{
"input": "2050",
"output": "2663/205"
},
{
"input": "5157",
"output": "1244202699616577925257908854992502879105187462860652753500143979369064245723363816152901984642185318105227079924186855121298620779636669909099525176016720566980036738379840484201060840762226140891649195248344160034243456960969500434409559402400997785076262368243485151264623913446406300409714366263836544017408112602389622392168343772273329588989257854547075621647134412375571624421845467345018113608142941220069412256286427238705568845394057266549279844034564540504288723831871872613761385100219013571797574528..."
},
{
"input": "9556",
"output": "61105610582596188064013632976018238169594847263124154642386689594807697097885447064570207703083797375229286491941164437689434529990291086697099939303795410031817348526076989232509267027565641811597218817018817025267674646870116016782259903322684685324891049798873702815116504349778081974046190242424181610803910534907237209386333892868457625884598750479036332/4139057561631190052420042715397268145091328063809781231185874234711716512317727312355164552220203407039993009382683073987640607521830928131007598529502..."
},
{
"input": "7901",
"output": "1545248712235484231234951857976545065744617617020030658469109057466859986475148250693905886674893272489053581743278765525029576600572532534838775161015285299867097071351763023768221772211614326132946083723216283578235838641012739144142278748089419497887052400481010672846336748523045688232788476455926025596760805229671163941642019404158795532714920358340283273984860096000931574655035232114889550219531272686479161110317036826198314000803468166009351225032738229182153921483640756378743625620038865132469106047..."
},
{
"input": "8622",
"output": "2346970612497798292051684863778787677688918498337895665035233783242368128286257851467636458012981817792946447606933694463959158768680516918810044760234529638520868683195790661167605124671831213269266007988150361676380026247303195939614144448935172977317349504257500667344413574778276538084533093523373859439640543406392557232735743996270882880554311714917225257533788072187384837827098726745502145040757646538864148126423442625937245/15845010358053163603682882776020046370855826407419789577061413023873354878758..."
},
{
"input": "16",
"output": "4/1"
},
{
"input": "32",
"output": "5/1"
},
{
"input": "64",
"output": "6/1"
},
{
"input": "128",
"output": "7/1"
},
{
"input": "256",
"output": "8/1"
},
{
"input": "512",
"output": "9/1"
},
{
"input": "1024",
"output": "10/1"
},
{
"input": "2048",
"output": "11/1"
},
{
"input": "4096",
"output": "12/1"
},
{
"input": "8192",
"output": "13/1"
},
{
"input": "9749",
"output": "2442073850489536993602927585016307199165621395304075025177694875024679607796284569131767726963170591399402108675638309823508923120869023271471818205166195909414421298770225330343221707377312426015931814666620011704161259290684411521150904067521292013955185317667861253504565429204675752674949251324122812583751343895693341865871684651273036507250959013187746544944138162398891218537343348931977619498001506833868902121065893913936425349903046456157409756042496452120477911621545767772676861905004685673876448424..."
},
{
"input": "9803",
"output": "3273210180788326072665779956874407303695209430301336271835983946088930098968971592446649088493870209185601637297946849124915345946101994703881396454817629317067995483358046696632270413949397783371061147815617923539634120806955265009854911188530306501324761896490486400736980978270851173985761450205468514371802366150508965142349402634775190541932571215772934087049601511151015580218962449832712507548850463067009640345032653747650848021793733486635063672276271184797891474270402353352104959589784879937226619631..."
},
{
"input": "9851",
"output": "1826526826552054078590833159938859608660168689393622926697593614966325247523385486843148169222905802492343158938765988786790268008018346250425185590729454696317705467641275241528094330016670372495410419378141651879792828682641532533284092879418090040903735626785107378204659838740412715571880884453837366724444432895654221478996531206045405344762139424591887085118650734474741760565377558510969213435666224432175984910232854534478442066899535808519463012771143160247271088007881997468588868391395961360449311260..."
},
{
"input": "9859",
"output": "9737556487585955271085725726347548524587201515015415940974212696477918276264308244608931996051014559947727871548525843984646030062506833242765111803441650144268637762679345636011358150115775908768896005580214321628703868435548404239676226435996040392670484414990942576376634145721680636769353606720643265107519869112758891642047680407647874665865802380346668006876206490814588938569751811311435939234659864088396761136789798899162439354805109166450795279173094212597156554710639295669120966978022955523520243885..."
},
{
"input": "9883",
"output": "3585169013008320587733453934540655406968508071250655174069676351966247095891666666457951370861261022535907218197677774600785190516321049571099175528120462561383326438548822604717683501452327840790897918994708328294710567693091965543331903184071652577937298158191755916197924842106107578819028940776121678861464188820255086932973183740793372165945865009415954786595728774195980645745127346052687115724325004061229398519190748075580806663121266449693574278118081790297078495193488775980028423355059820522243653725..."
},
{
"input": "9901",
"output": "1838099364825091485842998238489502638352163208605775246129613825541675258139803932850287467779059152113767067936585018625986257156889365449198856216156597215096982506298229063072584364817360767528951048690402306736891920567120457283597208730483707878597646111737127186752299112571526574444743928839110241560725998077538870670908652964981237795923920133705668072188172055098674658571105524472734987571439013958589827622877679807533101349696441175687163721291688130495577638100992957486490904022264860238167307326..."
},
{
"input": "9907",
"output": "1633354865670482634892575473274177878269848372373759525765811325846788929611278968402771127221384695483915945198456285004476851582174958025985911899376499975337986661537173411180428490907671107896805361798951388912266345066652077866312114944286489782939084013056891167373799137114155515056010000276816186615290579398553645497061457349859874907706345855178899820840538136570385474191197090563464515778875629438803382571649568677198891486414476226932831825123576296441036124053704853315256369077062694497961110434..."
},
{
"input": "9923",
"output": "3760299283807515305392881669921685380087483626665387583310740045779392618490518061864438198412014466565364228291295311284131760183238506480296641521584717001189575119537493891940454933801650905249443352246953782854872613017048635048443614120554701976326536039594670095564945312513189153593550500517921950184962024588992584781239533465993501341159180970745350678104403343694528834994019085606787711931776684194936418570665879951352474415566347678136222813280481902061127946763865268688300550988394467968839427831..."
},
{
"input": "9941",
"output": "3848534499658219667049727329663074105337547166730018729906447543650907057565599099052599064685569838374730308744072576703962547115876952975258439787965733453851059665347937329225608515273601271650034928543860491219634012020498814661622840677950496926059282612940876829862296013101963794076610277533664472961283348371953697283523069103642217248197905842345904997732309684469494496541394358109215746512001993248263898375663673449385592381926788206401567212276907962968353724108187030299614264372186122509235461661..."
},
{
"input": "9949",
"output": "3077194576897431282984928614971803243979867746742349396701033435828399498432602651550466130139454008014151230282733882472359320178948173983098259522962570058314958623895543103765377873930945767189012935127692368514283490255979891724625021542152152743761996609145171758487359833408232379642800156557718632212879833607371512214716299467380836645557808645986902660996771461355482482475905883289978390754379236092379144196172866264243566348794250784667125654370098361595124994920872496780086986738805458903185126030..."
},
{
"input": "9991",
"output": "6382125632197575640171909108366323999672572433833222272392411654952806480364944179628596547334889065794512754663260535014179926913634410060297543083266944529654281991142775738499030710183320227853166673816892482763220210176769895648821886542105816/436994993873214129706097166956708350993678881411295357199729151951767944417616335439228580716318181998128654620651240845861768505204366709906692902245553277900892247131030458103436298545516643924637451297481464347472084863384057367177715867713535"
},
{
"input": "9992",
"output": "882713207347291313029281/60446290980731458735309"
},
{
"input": "9993",
"output": "12637244675354581352253260560857374/865382809755804604755082721536683"
},
{
"input": "9994",
"output": "3281117414508879498426129146296635638706673857559146714758804687655977321336441892014774756310161571021964653093136031059973674988535235552194295743721563055893425769046817776696216427896857321525164709814889404834572227298884316348149960471145881924249349826195785903333523568245055998853462086046866400139120072781311401680748431799894049040989328677849586317964939112513229643421712863477114408475285877161526889717094721450283390765651073229738491488236723709404792790505935059388430226031866694290751309103..."
},
{
"input": "9995",
"output": "1368770541403820619075110203708490210616145992745821521870208914365828115565556194877572535511077690510688277376757546565243584175363368143317667278940670502781186329534839008398699279841764334491329910860701074569229951248069967340109056226002539889667430100999595433067983400778886042165596127864919572486395941238704720403024794261441096255620000217687954366591408789194462597191661175824028310400352/93746319107175952683864071964918454730461746778024627464635174121600584812748548703282543376385452193806936..."
},
{
"input": "9996",
"output": "2016420396858486097238844042485568452071214924046/138111634978483258420134114867245645268334710595"
},
{
"input": "9997",
"output": "115045178372494165897872226686512107429178048300340407805913417043457084371526821355671616896548808082243735275331446/7880401239278895842455808020028722761015947854093089333589658680849144354299442122282853250976983128161325598061363"
},
{
"input": "9998",
"output": "4285402091468445389426164244750423198106729816197961669529078982307315890678968433526215498100171962888335447/293567822846729153486185074598667128421960318613539983838411371441526128139326055432962374798096087878991871"
},
{
"input": "9999",
"output": "396504919788033353440140876437127916065460257739670266843919813860579129943101204509709504/27160479684459814483579275845458375480686245248879150008481872658058417330177822749111965"
},
{
"input": "10000",
"output": "211285126026764324876224334024814529251789998319439411297242149907456038558/14474011154664524427946373126085988481658748083205070504932198000989141205"
}
] | 0 | 0 | -1 | 4,209 |
190 | Non-Secret Cypher | [
"two pointers"
] | null | null | Berland starts to seize the initiative on the war with Flatland. To drive the enemy from their native land, the berlanders need to know exactly how many more flatland soldiers are left in the enemy's reserve. Fortunately, the scouts captured an enemy in the morning, who had a secret encrypted message with the information the berlanders needed so much.
The captured enemy had an array of positive integers. Berland intelligence have long been aware of the flatland code: to convey the message, which contained a number *m*, the enemies use an array of integers *a*. The number of its subarrays, in which there are at least *k* equal numbers, equals *m*. The number *k* has long been known in the Berland army so General Touristov has once again asked Corporal Vasya to perform a simple task: to decipher the flatlanders' message.
Help Vasya, given an array of integers *a* and number *k*, find the number of subarrays of the array of numbers *a*, which has at least *k* equal numbers.
Subarray *a*[*i*... *j*]Β (1<=β€<=*i*<=β€<=*j*<=β€<=*n*) of array *a*<==<=(*a*1,<=*a*2,<=...,<=*a**n*) is an array, made from its consecutive elements, starting from the *i*-th one and ending with the *j*-th one: *a*[*i*... *j*]<==<=(*a**i*,<=*a**i*<=+<=1,<=...,<=*a**j*). | The first line contains two space-separated integers *n*, *k* (1<=β€<=*k*<=β€<=*n*<=β€<=4Β·105), showing how many numbers an array has and how many equal numbers the subarrays are required to have, correspondingly.
The second line contains *n* space-separated integers *a**i* (1<=β€<=*a**i*<=β€<=109) β elements of the array. | Print the single number β the number of such subarrays of array *a*, that they have at least *k* equal integers.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. In is preferred to use the cin, cout streams or the %I64d specifier. | [
"4 2\n1 2 1 2\n",
"5 3\n1 2 1 1 3\n",
"3 1\n1 1 1\n"
] | [
"3",
"2",
"6"
] | In the first sample are three subarrays, containing at least two equal numbers: (1,2,1), (2,1,2) and (1,2,1,2).
In the second sample are two subarrays, containing three equal numbers: (1,2,1,1,3) and (1,2,1,1).
In the third sample any subarray contains at least one 1 number. Overall they are 6: (1), (1), (1), (1,1), (1,1) and (1,1,1). | [
{
"input": "4 2\n1 2 1 2",
"output": "3"
},
{
"input": "5 3\n1 2 1 1 3",
"output": "2"
},
{
"input": "3 1\n1 1 1",
"output": "6"
},
{
"input": "20 2\n6 7 2 4 6 8 4 3 10 5 3 5 7 9 1 2 8 1 9 10",
"output": "131"
},
{
"input": "63 2\n1 2 1 2 4 5 1 1 1 1 1 2 3 1 2 3 3 1 1 3 1 1 1 1 2 1 1 6 3 2 1 1 1 1 2 2 3 2 1 1 1 2 1 4 2 1 2 3 2 1 1 1 1 2 4 3 4 2 5 1 1 2 1",
"output": "1882"
},
{
"input": "63 5\n76826 79919 83599 93821 79919 46132 46132 46132 79919 76826 79919 79919 76826 79919 79919 76826 76826 46132 76826 40347 79919 46132 76826 83599 79919 79919 46132 46132 46132 83599 83599 79919 46132 83599 93821 76826 81314 79919 79919 83599 76826 76826 76826 76826 46132 76826 46132 79919 76826 83599 79919 40347 76826 46132 46132 93821 76826 79919 46132 83599 93821 46132 46132",
"output": "1356"
},
{
"input": "6 3\n6 6 4 4 6 2",
"output": "2"
},
{
"input": "100 1\n5 2 5 1 1 4 1 5 4 5 5 5 4 4 1 3 2 3 1 5 1 4 2 4 5 5 5 2 1 3 2 5 5 4 2 1 3 2 2 2 4 4 4 2 1 1 5 4 2 5 3 4 5 5 5 3 1 3 5 4 1 4 5 2 3 2 5 3 5 1 4 2 2 3 2 4 4 3 2 3 5 3 1 3 4 1 5 4 2 5 4 3 1 4 3 2 1 5 2 5",
"output": "5050"
},
{
"input": "1 1\n5",
"output": "1"
},
{
"input": "2 1\n42 1000000000",
"output": "3"
},
{
"input": "2 2\n193 193",
"output": "1"
},
{
"input": "2 2\n97 197",
"output": "0"
}
] | 3,000 | 2,355,200 | 0 | 4,210 |
|
460 | Little Dima and Equation | [
"brute force",
"implementation",
"math",
"number theory"
] | null | null | Little Dima misbehaved during a math lesson a lot and the nasty teacher Mr. Pickles gave him the following problem as a punishment.
Find all integer solutions *x* (0<=<<=*x*<=<<=109) of the equation:
where *a*, *b*, *c* are some predetermined constant values and function *s*(*x*) determines the sum of all digits in the decimal representation of number *x*.
The teacher gives this problem to Dima for each lesson. He changes only the parameters of the equation: *a*, *b*, *c*. Dima got sick of getting bad marks and he asks you to help him solve this challenging problem. | The first line contains three space-separated integers: *a*,<=*b*,<=*c* (1<=β€<=*a*<=β€<=5;Β 1<=β€<=*b*<=β€<=10000;Β <=-<=10000<=β€<=*c*<=β€<=10000). | Print integer *n* β the number of the solutions that you've found. Next print *n* integers in the increasing order β the solutions of the given equation. Print only integer solutions that are larger than zero and strictly less than 109. | [
"3 2 8\n",
"1 2 -18\n",
"2 2 -1\n"
] | [
"3\n10 2008 13726 ",
"0\n",
"4\n1 31 337 967 "
] | none | [
{
"input": "3 2 8",
"output": "3\n10 2008 13726 "
},
{
"input": "1 2 -18",
"output": "0"
},
{
"input": "2 2 -1",
"output": "4\n1 31 337 967 "
},
{
"input": "1 1 0",
"output": "9\n1 2 3 4 5 6 7 8 9 "
},
{
"input": "1 37 963",
"output": "16\n1000 1111 1222 1333 1370 1407 1444 1481 1518 1555 1592 1629 1666 1777 1888 1999 "
},
{
"input": "1 298 -1665",
"output": "17\n123 421 1017 1315 1613 1911 2209 2507 2805 4295 4593 4891 5189 5487 5785 6679 6977 "
},
{
"input": "1 3034 -9234",
"output": "23\n12004 21106 24140 30208 33242 39310 42344 48412 51446 54480 57514 60548 63582 66616 69650 72684 75718 78752 81786 87854 90888 96956 99990 "
},
{
"input": "5 9998 9998",
"output": "0"
},
{
"input": "5 10000 10000",
"output": "0"
},
{
"input": "5 65 352",
"output": "1\n208000352 "
},
{
"input": "5 9999 9999",
"output": "0"
},
{
"input": "4 2099 -38",
"output": "0"
},
{
"input": "1 1 -6708",
"output": "0"
},
{
"input": "5 36 -46",
"output": "0"
},
{
"input": "5 8975 -4",
"output": "0"
},
{
"input": "3 2794 -3354",
"output": "5\n165733932 308990694 392855398 415958984 999999980 "
},
{
"input": "5 1 4473",
"output": "11\n1424330 14353380 17214841 52526348 60470649 69348430 164920697 184532598 205967449 418199966 459169497 "
},
{
"input": "5 1 -9999",
"output": "6\n90001 2466100 17200369 52511876 60456177 205952977 "
},
{
"input": "4 4 6",
"output": "13\n10 1030 40006 114250 202506 262150 521290 937030 1562506 2458630 3694090 4743690 7496650 "
},
{
"input": "5 19 -666",
"output": "0"
},
{
"input": "5 5 -865",
"output": "0"
},
{
"input": "2 8468 -3666",
"output": "2\n7117922 14933886 "
},
{
"input": "4 9359 -3039",
"output": "0"
},
{
"input": "5 5706 -1856",
"output": "0"
},
{
"input": "2 6828 -39",
"output": "2\n7435653 17759589 "
},
{
"input": "5 3903 -9847",
"output": "0"
},
{
"input": "3 1727 4771",
"output": "1\n42124574 "
},
{
"input": "4 1870 9912",
"output": "0"
},
{
"input": "3 6300 7035",
"output": "1\n466761435 "
},
{
"input": "5 8704 -6190",
"output": "0"
},
{
"input": "2 68 3",
"output": "1\n45971 "
},
{
"input": "5 6 -95",
"output": "1\n416063647 "
},
{
"input": "2 28 12",
"output": "2\n4044 7180 "
},
{
"input": "3 37 -70",
"output": "0"
},
{
"input": "5 3 53",
"output": "1\n100663349 "
},
{
"input": "3 2570 4109",
"output": "2\n427587859 999777799 "
},
{
"input": "3 1139 6335",
"output": "2\n12134407 499999999 "
},
{
"input": "3 2278 -1329",
"output": "3\n61504671 145790671 999985999 "
},
{
"input": "4 30 719",
"output": "2\n21219149 899597999 "
},
{
"input": "4 9023 312",
"output": "0"
},
{
"input": "5 10000 9",
"output": "0"
},
{
"input": "5 7698 5337",
"output": "0"
},
{
"input": "5 1 0",
"output": "5\n1 17210368 52521875 60466176 205962976 "
},
{
"input": "5 12 3",
"output": "0"
},
{
"input": "5 3903 153",
"output": "0"
},
{
"input": "5 10000 0",
"output": "1\n10000 "
},
{
"input": "3 2570 -6691",
"output": "1\n999766999 "
},
{
"input": "5 5 13",
"output": "1\n579281018 "
}
] | 139 | 6,041,600 | 0 | 4,214 |
|
612 | HDD is Outdated Technology | [
"implementation",
"math"
] | null | null | HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.
One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.
Find the time need to read file split to *n* fragments. The *i*-th sector contains the *f**i*-th fragment of the file (1<=β€<=*f**i*<=β€<=*n*). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the *n*-th fragment is read. The fragments are read in the order from the first to the *n*-th.
It takes |*a*<=-<=*b*| time units to move the magnetic head from the sector *a* to the sector *b*. Reading a fragment takes no time. | The first line contains a positive integer *n* (1<=β€<=*n*<=β€<=2Β·105) β the number of fragments.
The second line contains *n* different integers *f**i* (1<=β€<=*f**i*<=β€<=*n*) β the number of the fragment written in the *i*-th sector. | Print the only integer β the number of time units needed to read the file. | [
"3\n3 1 2\n",
"5\n1 3 5 4 2\n"
] | [
"3\n",
"10\n"
] | In the second example the head moves in the following way:
- 1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units - 2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units - 3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units - 4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units
So the answer to the second example is 4β+β3β+β2β+β1β=β10. | [
{
"input": "3\n3 1 2",
"output": "3"
},
{
"input": "5\n1 3 5 4 2",
"output": "10"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "10\n8 2 10 3 4 6 1 7 9 5",
"output": "40"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10",
"output": "9"
},
{
"input": "10\n1 3 5 7 9 10 8 6 4 2",
"output": "45"
},
{
"input": "100\n11 9 35 34 51 74 16 67 26 21 14 80 84 79 7 61 28 3 53 43 42 5 56 36 69 30 22 88 1 27 65 91 46 31 59 50 17 96 25 18 64 55 78 2 63 24 95 48 93 13 38 76 89 94 15 90 45 81 52 87 83 73 44 49 23 82 85 75 86 33 47 19 58 97 37 20 40 10 92 4 6 68 77 54 71 12 62 60 100 39 41 99 72 29 57 8 70 32 66 98",
"output": "3580"
},
{
"input": "100\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100",
"output": "99"
},
{
"input": "100\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 100 98 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2",
"output": "4950"
}
] | 187 | 716,800 | -1 | 4,218 |
|
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"
}
] | 46 | 4,608,000 | 0 | 4,222 |
186 | Growing Mushrooms | [
"greedy",
"sortings"
] | null | null | Each year in the castle of Dwarven King there is a competition in growing mushrooms among the dwarves. The competition is one of the most prestigious ones, and the winner gets a wooden salad bowl. This year's event brought together the best mushroom growers from around the world, so we had to slightly change the rules so that the event gets more interesting to watch.
Each mushroom grower has a mushroom that he will grow on the competition. Under the new rules, the competition consists of two parts. The first part lasts *t*1 seconds and the second part lasts *t*2 seconds. The first and the second part are separated by a little break.
After the starting whistle the first part of the contest starts, and all mushroom growers start growing mushrooms at once, each at his individual speed of *v**i* meters per second. After *t*1 seconds, the mushroom growers stop growing mushrooms and go to have a break. During the break, for unexplained reasons, the growth of all mushrooms is reduced by *k* percent. After the break the second part of the contest starts and all mushrooms growers at the same time continue to grow mushrooms, each at his individual speed of *u**i* meters per second. After a *t*2 seconds after the end of the break, the competition ends. Note that the speeds before and after the break may vary.
Before the match dwarf Pasha learned from all participants, what two speeds they have chosen. However, the participants did not want to disclose to him all their strategy and therefore, did not say in what order they will be using these speeds. That is, if a participant chose speeds *a**i* and *b**i*, then there are two strategies: he either uses speed *a**i* before the break and speed *b**i* after it, or vice versa.
Dwarf Pasha really wants to win the totalizer. He knows that each participant chooses the strategy that maximizes the height of the mushroom. Help Dwarf Pasha make the final table of competition results.
The participants are sorted in the result table by the mushroom height (the participants with higher mushrooms follow earlier in the table). In case of equal mushroom heights, the participants are sorted by their numbers (the participants with a smaller number follow earlier). | The first input line contains four integer numbers *n*, *t*1, *t*2, *k* (1<=β€<=*n*,<=*t*1,<=*t*2<=β€<=1000;Β 1<=β€<=*k*<=β€<=100) β the number of participants, the time before the break, the time after the break and the percentage, by which the mushroom growth drops during the break, correspondingly.
Each of the following *n* lines contains two integers. The *i*-th (1<=β€<=*i*<=β€<=*n*) line contains space-separated integers *a**i*, *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=1000) β the speeds which the participant number *i* chose. | Print the final results' table: *n* lines, each line should contain the number of the corresponding dwarf and the final maximum height of his mushroom with exactly two digits after the decimal point. The answer will be considered correct if it is absolutely accurate. | [
"2 3 3 50\n2 4\n4 2\n",
"4 1 1 1\n544 397\n280 101\n280 101\n693 970\n"
] | [
"1 15.00\n2 15.00\n",
"4 1656.07\n1 937.03\n2 379.99\n3 379.99\n"
] | - First example: for each contestant it is optimal to use firstly speed 2 and afterwards speed 4, because 2Β·3Β·0.5β+β4Β·3β>β4Β·3Β·0.5β+β2Β·3. | [
{
"input": "2 3 3 50\n2 4\n4 2",
"output": "1 15.00\n2 15.00"
},
{
"input": "4 1 1 1\n544 397\n280 101\n280 101\n693 970",
"output": "4 1656.07\n1 937.03\n2 379.99\n3 379.99"
},
{
"input": "10 1 1 25\n981 1\n352 276\n164 691\n203 853\n599 97\n901 688\n934 579\n910 959\n317 624\n440 737",
"output": "8 1641.50\n6 1417.00\n7 1368.25\n10 1067.00\n4 1005.25\n1 981.75\n9 861.75\n3 814.00\n5 671.75\n2 559.00"
},
{
"input": "10 6 1 48\n239 632\n976 315\n797 112\n1 835\n938 862\n531 884\n422 607\n152 331\n413 677\n622 978",
"output": "5 3788.56\n10 3673.36\n2 3360.12\n6 3289.08\n4 2606.20\n3 2598.64\n9 2525.24\n7 2315.84\n1 2210.84\n8 1184.72"
},
{
"input": "8 1 4 55\n507 82\n681 492\n602 849\n367 557\n438 320\n798 699\n338 302\n391 814",
"output": "3 3666.90\n6 3506.55\n8 3431.95\n2 2945.40\n4 2393.15\n1 2064.90\n5 1896.00\n7 1487.90"
},
{
"input": "9 6 4 80\n95 269\n95 269\n781 724\n22 269\n173 655\n972 850\n22 269\n95 269\n642 666",
"output": "6 4908.00\n3 3992.80\n9 3434.40\n5 2827.60\n1 1190.00\n2 1190.00\n8 1190.00\n4 1102.40\n7 1102.40"
},
{
"input": "7 1 8 25\n98 713\n876 112\n990 771\n57 395\n171 899\n86 600\n459 320",
"output": "3 8498.25\n5 7320.25\n2 7092.00\n1 5777.50\n6 4864.50\n7 3912.00\n4 3202.75"
},
{
"input": "6 1 5 9\n105 771\n105 771\n105 771\n557 985\n105 771\n105 771",
"output": "4 5431.87\n1 3950.55\n2 3950.55\n3 3950.55\n5 3950.55\n6 3950.55"
},
{
"input": "6 2 2 78\n942 465\n593 600\n480 359\n733 648\n995 55\n526 538",
"output": "1 2088.60\n5 2014.20\n4 1751.12\n2 1460.92\n6 1307.44\n3 1117.96"
},
{
"input": "2 1 1 1\n2 2\n2 2",
"output": "1 3.98\n2 3.98"
},
{
"input": "2 1 1 100\n3 4\n4 3",
"output": "1 4.00\n2 4.00"
},
{
"input": "1 1 1 1\n1 1",
"output": "1 1.99"
}
] | 278 | 22,835,200 | 3 | 4,227 |
|
441 | Valera and Tubes | [
"constructive algorithms",
"dfs and similar",
"implementation"
] | null | null | Valera has got a rectangle table consisting of *n* rows and *m* columns. Valera numbered the table rows starting from one, from top to bottom and the columns β starting from one, from left to right. We will represent cell that is on the intersection of row *x* and column *y* by a pair of integers (*x*,<=*y*).
Valera wants to place exactly *k* tubes on his rectangle table. A tube is such sequence of table cells (*x*1,<=*y*1), (*x*2,<=*y*2), ..., (*x**r*,<=*y**r*), that:
- *r*<=β₯<=2; - for any integer *i* (1<=β€<=*i*<=β€<=*r*<=-<=1) the following equation |*x**i*<=-<=*x**i*<=+<=1|<=+<=|*y**i*<=-<=*y**i*<=+<=1|<==<=1 holds; - each table cell, which belongs to the tube, must occur exactly once in the sequence.
Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled:
- no pair of tubes has common cells; - each cell of the table belongs to some tube.
Help Valera to arrange *k* tubes on his rectangle table in a fancy manner. | The first line contains three space-separated integers *n*,<=*m*,<=*k* (2<=β€<=*n*,<=*m*<=β€<=300; 2<=β€<=2*k*<=β€<=*n*Β·*m*) β the number of rows, the number of columns and the number of tubes, correspondingly. | Print *k* lines. In the *i*-th line print the description of the *i*-th tube: first print integer *r**i* (the number of tube cells), then print 2*r**i* integers *x**i*1,<=*y**i*1,<=*x**i*2,<=*y**i*2,<=...,<=*x**ir**i*,<=*y**ir**i* (the sequence of table cells).
If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists. | [
"3 3 3\n",
"2 3 1\n"
] | [
"3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3\n",
"6 1 1 1 2 1 3 2 3 2 2 2 1\n"
] | Picture for the first sample:
Picture for the second sample: | [
{
"input": "3 3 3",
"output": "3 1 1 1 2 1 3\n3 2 1 2 2 2 3\n3 3 1 3 2 3 3"
},
{
"input": "2 3 1",
"output": "6 1 1 1 2 1 3 2 3 2 2 2 1"
},
{
"input": "2 3 1",
"output": "6 1 1 1 2 1 3 2 3 2 2 2 1"
},
{
"input": "300 300 2",
"output": "2 1 1 1 2\n89998 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1 97 1 98 1 99 1 100 1 101 1 10..."
},
{
"input": "300 300 150",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "300 299 299",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "300 300 45000",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "300 299 44850",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "2 2 2",
"output": "2 1 1 1 2\n2 2 2 2 1"
},
{
"input": "2 3 3",
"output": "2 1 1 1 2\n2 1 3 2 3\n2 2 2 2 1"
},
{
"input": "3 3 4",
"output": "2 1 1 1 2\n2 1 3 2 3\n2 2 2 2 1\n3 3 1 3 2 3 3"
},
{
"input": "5 5 12",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 2 5\n2 2 4 2 3\n2 2 2 2 1\n2 3 1 3 2\n2 3 3 3 4\n2 3 5 4 5\n2 4 4 4 3\n2 4 2 4 1\n2 5 1 5 2\n3 5 3 5 4 5 5"
},
{
"input": "7 5 17",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 2 5\n2 2 4 2 3\n2 2 2 2 1\n2 3 1 3 2\n2 3 3 3 4\n2 3 5 4 5\n2 4 4 4 3\n2 4 2 4 1\n2 5 1 5 2\n2 5 3 5 4\n2 5 5 6 5\n2 6 4 6 3\n2 6 2 6 1\n2 7 1 7 2\n3 7 3 7 4 7 5"
},
{
"input": "135 91 4352",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "32 27 153",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 2 27\n2 2 26 2 25\n2 2 24 2 23\n2 2 22 2 21\n2 2 20 2 19\n2 2 18 2 17\n2 2 16 2 15\n2 2 14 2 13\n2 2 12 2 11\n2 2 10 2 9\n2 2 8 2 7\n2 2 6 2 5\n2 2 4 2 3\n2 2 2 2 1\n2 3 1 3 2\n2 3 3 3 4\n2 3 5 3 6\n2 3 7 3 8\n2 3 9 3 10\n2 3 11 3 12\n2 3 13 3 14\n2 3 15 3 16\n2 3 17 3 18\n2 3 19 3 20\n2 3 21 3 22\n2 3 23 3 24\n2 3 25 3 26\n2 3 27 4 27\n2 4 2..."
},
{
"input": "74 83 2667",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "296 218 5275",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "89 82 2330",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "15 68 212",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 2 68 2 67\n2 2 66 2 65\n2 2 64 2 63\n2 2 62 2 61\n2 2 60 2 59\n2 2 58 2 57\n..."
},
{
"input": "95 4 177",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 2 4 2 3\n2 2 2 2 1\n2 3 1 3 2\n2 3 3 3 4\n2 4 4 4 3\n2 4 2 4 1\n2 5 1 5 2\n2 5 3 5 4\n2 6 4 6 3\n2 6 2 6 1\n2 7 1 7 2\n2 7 3 7 4\n2 8 4 8 3\n2 8 2 8 1\n2 9 1 9 2\n2 9 3 9 4\n2 10 4 10 3\n2 10 2 10 1\n2 11 1 11 2\n2 11 3 11 4\n2 12 4 12 3\n2 12 2 12 1\n2 13 1 13 2\n2 13 3 13 4\n2 14 4 14 3\n2 14 2 14 1\n2 15 1 15 2\n2 15 3 15 4\n2 16 4 16 3\n2 16 2 16 1\n2 17 1 17 2\n2 17 3 17 4\n2 18 4 18 3\n2 18 2 18 1\n2 19 1 19 2\n2 19 3 19 4\n2 20 4 20 3\n2 20 2 20 1\n2 21 1 21 2\n2 21 3 21 4\n2..."
},
{
"input": "60 136 8",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n8146 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1 97 1 98 1 99..."
},
{
"input": "91 183 7827",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "2 15 3",
"output": "2 1 1 1 2\n2 1 3 1 4\n26 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 2 15 2 14 2 13 2 12 2 11 2 10 2 9 2 8 2 7 2 6 2 5 2 4 2 3 2 2 2 1"
},
{
"input": "139 275 10770",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "114 298 7143",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "260 182 9496",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "42 297 3703",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "236 156 9535",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "201 226 1495",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "299 299 100",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "299 298 100",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "298 299 100",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "299 299 2",
"output": "2 1 1 1 2\n89399 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1 97 1 98 1 99 1 100 1 101 1 10..."
},
{
"input": "299 299 1",
"output": "89401 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1 97 1 98 1 99 1 100 1 101 1 102 1..."
},
{
"input": "298 299 1",
"output": "89102 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1 97 1 98 1 99 1 100 1 101 1 102 1..."
},
{
"input": "299 298 11",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n89082 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1 97..."
},
{
"input": "298 300 12",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n89378 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1..."
},
{
"input": "298 2 1",
"output": "596 1 1 1 2 2 2 2 1 3 1 3 2 4 2 4 1 5 1 5 2 6 2 6 1 7 1 7 2 8 2 8 1 9 1 9 2 10 2 10 1 11 1 11 2 12 2 12 1 13 1 13 2 14 2 14 1 15 1 15 2 16 2 16 1 17 1 17 2 18 2 18 1 19 1 19 2 20 2 20 1 21 1 21 2 22 2 22 1 23 1 23 2 24 2 24 1 25 1 25 2 26 2 26 1 27 1 27 2 28 2 28 1 29 1 29 2 30 2 30 1 31 1 31 2 32 2 32 1 33 1 33 2 34 2 34 1 35 1 35 2 36 2 36 1 37 1 37 2 38 2 38 1 39 1 39 2 40 2 40 1 41 1 41 2 42 2 42 1 43 1 43 2 44 2 44 1 45 1 45 2 46 2 46 1 47 1 47 2 48 2 48 1 49 1 49 2 50 2 50 1 51 1 51 2 52 2 52 1 53 1 ..."
},
{
"input": "2 298 1",
"output": "596 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 1 10 1 11 1 12 1 13 1 14 1 15 1 16 1 17 1 18 1 19 1 20 1 21 1 22 1 23 1 24 1 25 1 26 1 27 1 28 1 29 1 30 1 31 1 32 1 33 1 34 1 35 1 36 1 37 1 38 1 39 1 40 1 41 1 42 1 43 1 44 1 45 1 46 1 47 1 48 1 49 1 50 1 51 1 52 1 53 1 54 1 55 1 56 1 57 1 58 1 59 1 60 1 61 1 62 1 63 1 64 1 65 1 66 1 67 1 68 1 69 1 70 1 71 1 72 1 73 1 74 1 75 1 76 1 77 1 78 1 79 1 80 1 81 1 82 1 83 1 84 1 85 1 86 1 87 1 88 1 89 1 90 1 91 1 92 1 93 1 94 1 95 1 96 1 97 1 98 1 99 1 100 1 101 1 102 1 1..."
},
{
"input": "300 300 500",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "300 300 501",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "300 300 44999",
"output": "2 1 1 1 2\n2 1 3 1 4\n2 1 5 1 6\n2 1 7 1 8\n2 1 9 1 10\n2 1 11 1 12\n2 1 13 1 14\n2 1 15 1 16\n2 1 17 1 18\n2 1 19 1 20\n2 1 21 1 22\n2 1 23 1 24\n2 1 25 1 26\n2 1 27 1 28\n2 1 29 1 30\n2 1 31 1 32\n2 1 33 1 34\n2 1 35 1 36\n2 1 37 1 38\n2 1 39 1 40\n2 1 41 1 42\n2 1 43 1 44\n2 1 45 1 46\n2 1 47 1 48\n2 1 49 1 50\n2 1 51 1 52\n2 1 53 1 54\n2 1 55 1 56\n2 1 57 1 58\n2 1 59 1 60\n2 1 61 1 62\n2 1 63 1 64\n2 1 65 1 66\n2 1 67 1 68\n2 1 69 1 70\n2 1 71 1 72\n2 1 73 1 74\n2 1 75 1 76\n2 1 77 1 78\n2 1 79 1 80\n..."
},
{
"input": "5 5 3",
"output": "2 1 1 1 2\n2 1 3 1 4\n21 1 5 2 5 2 4 2 3 2 2 2 1 3 1 3 2 3 3 3 4 3 5 4 5 4 4 4 3 4 2 4 1 5 1 5 2 5 3 5 4 5 5"
},
{
"input": "2 4 3",
"output": "2 1 1 1 2\n2 1 3 1 4\n4 2 4 2 3 2 2 2 1"
}
] | 607 | 16,486,400 | 3 | 4,230 |
|
680 | Bear and Five Cards | [
"constructive algorithms",
"implementation"
] | null | null | A little bear Limak plays a game. He has five cards. There is one number written on each card. Each number is a positive integer.
Limak can discard (throw out) some cards. His goal is to minimize the sum of numbers written on remaining (not discarded) cards.
He is allowed to at most once discard two or three cards with the same number. Of course, he won't discard cards if it's impossible to choose two or three cards with the same number.
Given five numbers written on cards, cay you find the minimum sum of numbers on remaining cards? | The only line of the input contains five integers *t*1, *t*2, *t*3, *t*4 and *t*5 (1<=β€<=*t**i*<=β€<=100)Β β numbers written on cards. | Print the minimum possible sum of numbers written on remaining cards. | [
"7 3 7 3 20\n",
"7 9 3 1 8\n",
"10 10 10 10 10\n"
] | [
"26\n",
"28\n",
"20\n"
] | In the first sample, Limak has cards with numbers 7, 3, 7, 3 and 20. Limak can do one of the following.
- Do nothing and the sum would be 7β+β3β+β7β+β3β+β20β=β40. - Remove two cards with a number 7. The remaining sum would be 3β+β3β+β20β=β26. - Remove two cards with a number 3. The remaining sum would be 7β+β7β+β20β=β34.
You are asked to minimize the sum so the answer is 26.
In the second sample, it's impossible to find two or three cards with the same number. Hence, Limak does nothing and the sum is 7β+β9β+β1β+β3β+β8β=β28.
In the third sample, all cards have the same number. It's optimal to discard any three cards. The sum of two remaining numbers is 10β+β10β=β20. | [
{
"input": "7 3 7 3 20",
"output": "26"
},
{
"input": "7 9 3 1 8",
"output": "28"
},
{
"input": "10 10 10 10 10",
"output": "20"
},
{
"input": "8 7 1 8 7",
"output": "15"
},
{
"input": "7 7 7 8 8",
"output": "16"
},
{
"input": "8 8 8 2 2",
"output": "4"
},
{
"input": "8 8 2 2 2",
"output": "6"
},
{
"input": "5 50 5 5 60",
"output": "110"
},
{
"input": "100 100 100 100 100",
"output": "200"
},
{
"input": "1 1 1 1 1",
"output": "2"
},
{
"input": "29 29 20 20 20",
"output": "58"
},
{
"input": "20 29 20 29 20",
"output": "58"
},
{
"input": "31 31 20 20 20",
"output": "60"
},
{
"input": "20 20 20 31 31",
"output": "60"
},
{
"input": "20 31 20 31 20",
"output": "60"
},
{
"input": "20 20 20 30 30",
"output": "60"
},
{
"input": "30 30 20 20 20",
"output": "60"
},
{
"input": "8 1 8 8 8",
"output": "9"
},
{
"input": "1 1 1 8 1",
"output": "9"
},
{
"input": "1 2 3 4 5",
"output": "15"
},
{
"input": "100 99 98 97 96",
"output": "490"
},
{
"input": "1 1 100 100 100",
"output": "2"
},
{
"input": "100 100 99 99 98",
"output": "296"
},
{
"input": "98 99 100 99 100",
"output": "296"
},
{
"input": "1 90 1 91 1",
"output": "181"
},
{
"input": "60 1 75 1 92",
"output": "227"
},
{
"input": "15 40 90 40 90",
"output": "95"
},
{
"input": "1 1 15 20 20",
"output": "17"
},
{
"input": "90 11 11 10 10",
"output": "110"
},
{
"input": "20 21 22 23 24",
"output": "110"
},
{
"input": "1 1 2 98 99",
"output": "199"
},
{
"input": "3 7 7 7 10",
"output": "13"
},
{
"input": "1 3 3 3 1",
"output": "2"
},
{
"input": "1 9 9 9 10",
"output": "11"
},
{
"input": "100 1 1 1 1",
"output": "101"
},
{
"input": "2 2 2 100 100",
"output": "6"
},
{
"input": "1 2 2 2 2",
"output": "3"
},
{
"input": "1 1 2 2 5",
"output": "7"
},
{
"input": "1 2 3 4 1",
"output": "9"
},
{
"input": "11 10 10 10 10",
"output": "21"
},
{
"input": "2 2 2 10 10",
"output": "6"
},
{
"input": "1 1 1 1 4",
"output": "5"
},
{
"input": "98 98 98 98 23",
"output": "121"
},
{
"input": "1 2 3 100 100",
"output": "6"
},
{
"input": "2 2 5 10 10",
"output": "9"
},
{
"input": "2 2 3 3 3",
"output": "4"
},
{
"input": "1 1 1 1 2",
"output": "3"
},
{
"input": "12 12 7 7 7",
"output": "21"
}
] | 46 | 0 | 3 | 4,232 |
|
784 | Numbers Joke | [
"*special"
] | null | null | The input contains a single integer *a* (1<=β€<=*a*<=β€<=30).
Output a single integer. | The input contains a single integer *a* (1<=β€<=*a*<=β€<=30). | Output a single integer. | [
"3\n"
] | [
"27\n"
] | none | [
{
"input": "3",
"output": "27"
},
{
"input": "1",
"output": "4"
},
{
"input": "2",
"output": "22"
},
{
"input": "4",
"output": "58"
},
{
"input": "5",
"output": "85"
},
{
"input": "6",
"output": "94"
},
{
"input": "7",
"output": "121"
},
{
"input": "8",
"output": "166"
},
{
"input": "9",
"output": "202"
},
{
"input": "10",
"output": "265"
},
{
"input": "11",
"output": "274"
},
{
"input": "12",
"output": "319"
},
{
"input": "13",
"output": "346"
},
{
"input": "14",
"output": "355"
},
{
"input": "15",
"output": "378"
},
{
"input": "16",
"output": "382"
},
{
"input": "17",
"output": "391"
},
{
"input": "18",
"output": "438"
},
{
"input": "19",
"output": "454"
},
{
"input": "20",
"output": "483"
},
{
"input": "21",
"output": "517"
},
{
"input": "22",
"output": "526"
},
{
"input": "23",
"output": "535"
},
{
"input": "24",
"output": "562"
},
{
"input": "25",
"output": "576"
},
{
"input": "26",
"output": "588"
},
{
"input": "27",
"output": "627"
},
{
"input": "28",
"output": "634"
},
{
"input": "29",
"output": "636"
},
{
"input": "30",
"output": "645"
}
] | 0 | 0 | -1 | 4,246 |
|
142 | Help Greg the Dwarf 2 | [
"geometry"
] | null | null | Greg the Dwarf has been really busy recently with excavations by the Neverland Mountain. However for the well-known reasons (as you probably remember he is a very unusual dwarf and he cannot stand sunlight) Greg can only excavate at night. And in the morning he should be in his crypt before the first sun ray strikes. That's why he wants to find the shortest route from the excavation point to his crypt. Greg has recollected how the Codeforces participants successfully solved the problem of transporting his coffin to a crypt. So, in some miraculous way Greg appeared in your bedroom and asks you to help him in a highly persuasive manner. As usual, you didn't feel like turning him down.
After some thought, you formalized the task as follows: as the Neverland mountain has a regular shape and ends with a rather sharp peak, it can be represented as a cone whose base radius equals *r* and whose height equals *h*. The graveyard where Greg is busy excavating and his crypt can be represented by two points on the cone's surface. All you've got to do is find the distance between points on the cone's surface.
The task is complicated by the fact that the mountain's base on the ground level and even everything below the mountain has been dug through by gnome (one may wonder whether they've been looking for the same stuff as Greg...). So, one can consider the shortest way to pass not only along the side surface, but also along the cone's base (and in a specific case both points can lie on the cone's base β see the first sample test)
Greg will be satisfied with the problem solution represented as the length of the shortest path between two points β he can find his way pretty well on his own. He gave you two hours to solve the problem and the time is ticking! | The first input line contains space-separated integers *r* and *h* (1<=β€<=*r*,<=*h*<=β€<=1000) β the base radius and the cone height correspondingly. The second and third lines contain coordinates of two points on the cone surface, groups of three space-separated real numbers. The coordinates of the points are given in the systems of coordinates where the origin of coordinates is located in the centre of the cone's base and its rotation axis matches the *OZ* axis. In this coordinate system the vertex of the cone is located at the point (0,<=0,<=*h*), the base of the cone is a circle whose center is at the point (0,<=0,<=0), lying on the *XOY* plane, and all points on the cone surface have a non-negative coordinate *z*. It is guaranteed that the distances from the points to the cone surface do not exceed 10<=-<=12. All real numbers in the input have no more than 16 digits after decimal point. | Print the length of the shortest path between the points given in the input, with absolute or relative error not exceeding 10<=-<=6. | [
"2 2\n1.0 0.0 0.0\n-1.0 0.0 0.0\n",
"2 2\n1.0 0.0 0.0\n1.0 0.0 1.0\n",
"2 2\n1.0 0.0 1.0\n-1.0 0.0 1.0\n",
"2 2\n1.0 0.0 0.0\n0.0 1.0 1.0\n"
] | [
"2.000000000",
"2.414213562",
"2.534324263",
"3.254470198"
] | none | [
{
"input": "2 2\n1.0 0.0 0.0\n-1.0 0.0 0.0",
"output": "2.0000000000000000"
},
{
"input": "2 2\n1.0 0.0 0.0\n1.0 0.0 1.0",
"output": "2.414213562373095"
},
{
"input": "2 2\n1.0 0.0 1.0\n-1.0 0.0 1.0",
"output": "2.534324262661599"
},
{
"input": "2 2\n1.0 0.0 0.0\n0.0 1.0 1.0",
"output": "3.254470197706959"
},
{
"input": "2 2\n1 0 1\n1 0 1",
"output": "0.000000000000000"
},
{
"input": "2 2\n0 2 0\n2 0 0",
"output": "2.8284271247461903"
},
{
"input": "1 3\n0.333333333333333 0 2\n-0.666666666666666 0 1",
"output": "1.769124183375466"
},
{
"input": "4 2\n0 2 1\n0 -3 0",
"output": "7.617495113916503"
},
{
"input": "1 1\n0 0 1\n0 0 0",
"output": "2.414213562373095"
},
{
"input": "100 100\n99 0 1\n-99 0 1",
"output": "202.828427124746210"
},
{
"input": "88 105\n-83.647129195665613 27.334186970232459 0.000000000000000\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "88.0000000000000000"
},
{
"input": "95 115\n-94.798837559409705 6.179028838308848 0.000000000000000\n3.342582693513654 12.928467665102536 98.835138349494315",
"output": "141.297194476396560"
},
{
"input": "139 163\n-7.064282061617403 -119.648796858761900 22.448079209184119\n-3.099235383286207 130.020758014221030 10.486305761961773",
"output": "320.994208466804080"
},
{
"input": "89 165\n-77.601546118264423 -40.138626159725227 3.026234987879706\n-32.536564505566112 79.820562899551433 5.196277486773833",
"output": "138.055005811202530"
},
{
"input": "167 201\n-44.045042116268228 2.240298676664706 147.919173318547560\n-1.653062448517331 166.991818316171730 0.000000000000000",
"output": "229.161875457891940"
},
{
"input": "209 155\n10.339810149948200 207.429619805582460 0.973633003803984\n207.268022593198170 13.679741073068795 0.950049728912131",
"output": "280.113608256360580"
},
{
"input": "214 197\n-139.010054266533840 145.275848992143410 11.903485508393622\n201.707752886360570 -61.822489603821438 2.789960360351068",
"output": "435.069479445623240"
},
{
"input": "220 199\n7.702486930770317 -17.352156624704840 181.827313666644470\n-19.019292298570587 -58.442551586093003 143.407124141606430",
"output": "62.376512949023279"
},
{
"input": "125 223\n91.992953839808365 76.334473682993220 9.741805344154667\n-84.359519829273182 -91.096969326992280 1.502290062829727",
"output": "262.322409454934590"
},
{
"input": "135 242\n59.558351552645604 -52.913315274992456 99.187441396521223\n-24.675990127584633 -14.025205052053790 191.120333614367500",
"output": "136.624783180323310"
},
{
"input": "253 27\n-23.383556363495956 -3.075490409857886 24.483030221151189\n72.245718245226726 -82.324921811851368 15.311020040812947",
"output": "124.691284935369720"
},
{
"input": "254 54\n0.000000000000000 0.000000000000000 54.000000000000000\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "513.676722098843470"
},
{
"input": "267 42\n61.269272425153595 5.673599488606665 32.320903001753486\n-63.797770810112304 -38.267051239340162 30.297514320612102",
"output": "133.678382741735420"
},
{
"input": "38 278\n5.399109546042840 -8.432443753698909 204.748343438560110\n-5.230748245976885 1.555327155842188 238.077119640322790",
"output": "38.093735046969364"
},
{
"input": "78 282\n-1.664125131541297 -59.562854026849507 66.573343539272599\n71.149972890338546 -22.703911105882042 11.986524947742422",
"output": "102.658307334262700"
},
{
"input": "245 283\n-65.200025215297188 211.893667712412820 26.916275024999653\n81.954925476524096 -210.905000365566200 21.636551653267666",
"output": "553.847834491559980"
},
{
"input": "160 284\n-11.848288916318076 151.632288175726250 14.032286789548566\n55.204568662902133 -137.884714926791870 20.367746237801001",
"output": "355.266014577463070"
},
{
"input": "296 125\n0.000000000000000 0.000000000000000 0.000000000000000\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "0.0000000000000000"
},
{
"input": "307 13\n-270.438955322231150 -145.298903795659300 0.000000000000000\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "307.0000000000000000"
},
{
"input": "248 318\n-104.624749910959410 138.999195918964920 94.919838172486877\n228.999165107773990 95.201798197000812 0.000000000000000",
"output": "378.618071232051190"
},
{
"input": "320 305\n133.785417250379680 -214.950689891414610 63.683625243294543\n31.210648478715274 -54.393542268080502 245.227875205476610",
"output": "263.170090569639000"
},
{
"input": "279 320\n176.409341581619770 -216.149818882041610 0.000000000000000\n-80.839498717221858 -72.772525941007075 195.246130236928480",
"output": "362.984167377827590"
},
{
"input": "166 326\n-159.080559959952890 47.427581034961378 0.000000000000000\n64.219952084615173 -58.053751680168347 155.988180576648150",
"output": "350.312896450586380"
},
{
"input": "313 326\n268.206244981551440 -161.351821039912720 0.000000000000000\n32.335370169772631 -20.887667026020182 285.906088038778530",
"output": "396.367934965696240"
},
{
"input": "337 155\n103.562025742828280 213.837241376601870 45.720328048223415\n-12.785007455865372 -193.917849947259270 65.615662078921559",
"output": "451.739183365123150"
},
{
"input": "391 393\n69.363017479144332 381.996349660945500 2.771345269553063\n-4.314168772882780 -327.594317606469870 63.701456901510852",
"output": "872.611999302211980"
},
{
"input": "453 388\n-81.104385878292348 94.096725495772120 281.598867297277200\n319.890281685634530 320.747888041504270 0.000000000000000",
"output": "547.976632686011840"
},
{
"input": "462 27\n-159.864787795012400 -17.769652438662003 17.599713760563326\n-43.201695776047600 16.275482559188674 24.302000751547617",
"output": "121.714300862770440"
},
{
"input": "408 497\n267.962525371795490 -106.500517621999510 145.748954986607150\n406.530738343103910 -34.594201569204905 0.000000000000000",
"output": "213.676529817838540"
},
{
"input": "4 501\n1.256027912095488 1.550849382650718 251.041040422217460\n2.794539216444501 -2.861913794605588 0.000000000000000",
"output": "251.094303330067670"
},
{
"input": "505 405\n-171.258217521977260 26.368423921428572 266.035850862244220\n-322.844337173239810 -330.811068528084830 34.293877778928582",
"output": "454.191411638506740"
},
{
"input": "93 528\n36.658812293708685 85.470061900145083 0.000000000000000\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "93.0000000000000000"
},
{
"input": "19 536\n-2.323956629404414 0.854918404455399 466.144562395750310\n-11.970821418787578 -14.754641119305361 0.000000000000000",
"output": "466.515075604037010"
},
{
"input": "543 213\n-11.022498941354053 -208.184746146469020 131.221988486140450\n-261.342433954058090 237.839371010998720 74.386870834762945",
"output": "529.265581335714840"
},
{
"input": "552 538\n-219.196257175740500 505.979100989387290 0.567204598667054\n-13.005338942244901 -501.382586206790510 49.169271940131331",
"output": "1147.290245679384000"
},
{
"input": "290 555\n-35.294429133672416 -33.484539794537760 461.892222209193390\n-252.058361325518750 -143.410538266524330 0.000000000000000",
"output": "521.936684362155690"
},
{
"input": "557 356\n412.616943315331810 -374.160738305226630 0.000000000000000\n-119.143786824695810 28.916320481813823 277.639995153037090",
"output": "758.644272670264740"
},
{
"input": "108 567\n-77.326538888682109 -75.396328713652466 0.000000000000000\n-85.325412036343849 1.454640554847149 118.976494340335650",
"output": "142.855584570014320"
},
{
"input": "604 507\n256.435816856724730 546.860742632915960 0.000000000000000\n0.000000000000000 0.000000000000000 507.000000000000000",
"output": "788.584174327637130"
},
{
"input": "206 610\n85.003724396840582 -183.792743756824360 10.370249946816582\n-132.372286093682990 124.137988504975570 72.626760718535252",
"output": "489.630683838895320"
},
{
"input": "12 621\n-6.805901628792963 -2.400041575385406 247.536715624015390\n-9.888929727632208 -6.797725269673083 0.000000000000000",
"output": "247.595041963414560"
},
{
"input": "186 636\n125.502577654577490 -76.943123644053074 132.632626038618070\n10.998850276346941 11.375883701939239 581.893591807090730",
"output": "472.553289667418480"
},
{
"input": "194 640\n-185.594418018322020 -28.430658132256557 20.587563671749706\n162.386754505225720 71.184945374190377 55.079159027002405",
"output": "462.832453430235830"
},
{
"input": "381 647\n314.650162349171300 199.407123669264730 14.407770163811673\n-199.461288368305470 -305.260525670149090 27.766889571708951",
"output": "792.391879168826340"
},
{
"input": "663 558\n185.667839327817890 0.821162517806038 401.735041310258740\n541.204579549448680 -169.787472718591060 80.617413258449716",
"output": "508.589197900185520"
},
{
"input": "665 337\n-81.930040429089274 477.240591033892090 91.612221878619039\n-94.047744970766075 579.582133079572710 39.445199819169396",
"output": "115.507697648901770"
},
{
"input": "670 162\n128.165741407183870 -19.582009069376959 130.651054537534490\n-669.944585774285540 8.616959540400943 0.000000000000000",
"output": "819.927464516420400"
},
{
"input": "681 160\n-613.743030379663540 -295.094040367452070 0.000000000000000\n-138.987119977579430 -666.666018693271890 0.000000000000000",
"output": "602.8755340355712600"
},
{
"input": "700 694\n-491.323781315873530 391.487036614876730 71.164119861786261\n369.587380114008230 585.823923455896420 7.272344266525618",
"output": "925.471286233843440"
},
{
"input": "392 711\n275.256598579833170 279.101782398931620 0.000000000000000\n370.063048151715410 37.629657168787716 36.327559021646451",
"output": "265.894529685532690"
},
{
"input": "496 720\n-81.481055925005222 -489.261522629100850 0.000000000000000\n0.000000000000000 0.000000000000000 720.000000000000000",
"output": "874.308869908119620"
},
{
"input": "307 732\n-263.300272665471650 149.674800448139620 9.850014526791911\n252.974089914966560 152.268689048374710 27.979618318814200",
"output": "564.688286438940510"
},
{
"input": "708 735\n-372.227682278060970 -591.827556222709400 9.185583606570070\n-158.326918524255120 656.057627975817580 34.370737282300794",
"output": "1359.983114591124600"
},
{
"input": "562 761\n447.905573469315980 327.128350332647590 9.957782797299041\n-426.805719967420540 -331.040778689403110 29.600587789132693",
"output": "1173.053559827348900"
},
{
"input": "770 459\n88.786807039677768 -138.447991737814420 360.957772634164770\n183.265921873443600 -185.090929054817020 303.732327011363680",
"output": "119.910138117875160"
},
{
"input": "775 303\n220.814458005458700 -81.823041961067929 210.932255816936160\n-194.572073117032460 238.686633634201710 182.603770274642930",
"output": "547.658318653906580"
},
{
"input": "310 795\n-37.954433819069251 175.532754257961810 334.440556394379200\n-110.110062248727290 289.785738419925340 0.000000000000000",
"output": "360.711396236277610"
},
{
"input": "754 796\n216.565988894870090 153.972540922843820 515.476132607224140\n-47.509812467842735 752.501706123826920 0.000000000000000",
"output": "838.208600447662770"
},
{
"input": "800 645\n752.626854703574510 271.206227028445370 0.000000000000000\n-95.598746603765832 -604.427250606550160 151.622809761020480",
"output": "1320.622324323393100"
},
{
"input": "805 653\n687.624915941348040 -164.311988032427020 79.508586205624965\n762.951730761017420 256.767709279745700 0.000000000000000",
"output": "437.165612527832140"
},
{
"input": "816 723\n-409.404815276986650 251.893602766220970 297.094693677376310\n-102.999857434057130 -809.473303678733940 0.000000000000000",
"output": "1222.018824276540700"
},
{
"input": "857 741\n-856.534963081445990 28.228655991135017 0.000000000000000\n-145.707872297531990 -844.522478061138490 0.000000000000000",
"output": "1125.5974835534469000"
},
{
"input": "874 748\n-704.291967415513110 456.985614182417630 29.473929299402851\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "919.329269786949230"
},
{
"input": "881 682\n-39.602632901035470 880.109442891794970 0.000000000000000\n-232.291083077826440 -675.068070887951990 129.343123219787340",
"output": "1793.990810649566800"
},
{
"input": "893 2\n-87.049912924255025 -888.747046498540840 0.000000000000000\n-102.087930879379740 -77.695608292576296 1.712674372195576",
"output": "811.192659983106640"
},
{
"input": "893 745\n-744.291957980134610 493.435387144150350 0.000000000000000\n-229.382232313548710 -863.036958362069870 0.000000000000000",
"output": "1450.9132467894781000"
},
{
"input": "894 380\n62.028165472337882 891.845562128408120 0.000000000000000\n-281.506413354874670 848.522326895453260 0.000000000000000",
"output": "346.2555552782370100"
},
{
"input": "894 795\n-79.319376955375546 737.053160611378190 135.782330090001180\n-276.945076779847170 -850.022014095755370 0.000000000000000",
"output": "1863.330948809297300"
},
{
"input": "895 266\n-460.946965864515730 600.620195555266720 40.981602257928387\n178.077028677831550 166.078812707948770 193.629343617797820",
"output": "790.950234454675300"
},
{
"input": "902 469\n-665.302587586102280 0.024106493977479 123.072157668834680\n58.789007076917628 -165.366933065562050 377.744642427558010",
"output": "799.180998688435470"
},
{
"input": "909 879\n695.074972696372130 413.045294912773730 97.145264807840434\n-705.229230890123060 -517.817269433920730 32.956360377477495",
"output": "2002.737605542867900"
},
{
"input": "196 936\n-1.157629940182491 -1.112914249009275 928.331349563707700\n-43.366603227838930 -132.521475146312530 270.118439199950160",
"output": "672.528850625547190"
},
{
"input": "939 172\n760.602725383313210 550.640076764737840 0.000000000000000\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "939.0000000000001100"
},
{
"input": "952 565\n389.334410842084650 868.747786491710600 0.000000000000000\n492.169149678581280 807.768438499273320 3.622308459447762",
"output": "119.630652845404640"
},
{
"input": "588 957\n219.263290283055740 515.577240145252860 45.140983359549089\n-516.487259323793180 -279.513083799093290 1.187280610002339",
"output": "1161.121426778588100"
},
{
"input": "754 959\n344.733007448335230 -670.126881792474360 0.510505088991700\n-508.306819760418310 499.529742923366650 52.560459621746233",
"output": "1555.419070886616100"
},
{
"input": "406 962\n-49.783332082275358 -248.776028266368480 360.848848877620410\n-1.952167810020501 -3.487333391971083 952.530329833034900",
"output": "642.294343165613210"
},
{
"input": "973 436\n47.525346927433176 69.727668618789224 398.187783288826440\n-827.862618951791770 511.246011370534060 0.000000000000000",
"output": "1061.333336198369400"
},
{
"input": "974 336\n-412.402783094129350 395.821170200124020 138.808395601866750\n-31.097990341177809 10.151078095017953 324.715080477288270",
"output": "573.326026015034130"
},
{
"input": "997 897\n966.907255849128660 63.326755791696719 25.210646226492816\n-691.417709012145790 -678.273873083398140 25.584924238061610",
"output": "1940.382463863425300"
},
{
"input": "1000 1000\n0.000000000000000 0.000000000000000 0.000000000000000\n0.000000000000000 0.000000000000000 0.000000000000000",
"output": "0.0000000000000000"
},
{
"input": "1000 1000\n0.000000000000000 0.000000000000000 1000.000000000000000\n0.000000000000000 0.000000000000000 1000.000000000000000",
"output": "0.000000000000000"
},
{
"input": "1000 1000\n0.000000000000000 0.000000000000000 0.000000000000000\n0.000000000000000 0.000000000000000 1000.000000000000000",
"output": "2414.213562373095100"
},
{
"input": "1000 1000\n519.430292507390280 822.183052823439540 27.481207829697631\n-925.681320081855800 -271.775638373096170 35.247231690112748",
"output": "1953.674308800189900"
},
{
"input": "1000 1000\n-865.729850457726000 -306.217565767793640 81.709538567284440\n891.442934337587080 215.701160976332830 82.831806031769716",
"output": "2229.685117365914700"
},
{
"input": "1000 1000\n-451.257094270773560 -345.250138251562930 431.818142587758190\n68.190857559004940 -27.380462017482774 926.517466327546120",
"output": "801.825262695840930"
},
{
"input": "1000 1000\n-876.740604189171450 -386.919633788012220 41.678086421560081\n659.134284294775630 733.795718905435480 13.635887809815506",
"output": "2031.701640955472400"
},
{
"input": "1000 1000\n-20.534295190826231 133.273184243935670 865.154166109953510\n-711.474516721095600 -638.507879949720520 44.024947657417599",
"output": "1355.676743956396400"
},
{
"input": "1000 1000\n969.207183920193190 -213.270785951424980 7.605424489407109\n-631.538647292108290 -722.789524216293670 40.174099463677607",
"output": "1778.286143061074400"
},
{
"input": "1000 1000\n-378.383150861597360 -21.882887748889253 620.984605019557530\n291.284330016681450 461.398444104171010 454.348934634031140",
"output": "930.253674386416260"
}
] | 92 | 0 | 0 | 4,257 |
|
598 | Queries on a String | [
"implementation",
"strings"
] | null | null | You are given a string *s* and should process *m* queries. Each query is described by two 1-based indices *l**i*, *r**i* and integer *k**i*. It means that you should cyclically shift the substring *s*[*l**i*... *r**i*] *k**i* times. The queries should be processed one after another in the order they are given.
One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.
For example, if the string *s* is abacaba and the query is *l*1<==<=3,<=*r*1<==<=6,<=*k*1<==<=1 then the answer is abbacaa. If after that we would process the query *l*2<==<=1,<=*r*2<==<=4,<=*k*2<==<=2 then we would get the string baabcaa. | The first line of the input contains the string *s* (1<=β€<=|*s*|<=β€<=10<=000) in its initial state, where |*s*| stands for the length of *s*. It contains only lowercase English letters.
Second line contains a single integer *m* (1<=β€<=*m*<=β€<=300)Β β the number of queries.
The *i*-th of the next *m* lines contains three integers *l**i*, *r**i* and *k**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=|*s*|,<=1<=β€<=*k**i*<=β€<=1<=000<=000)Β β the description of the *i*-th query. | Print the resulting string *s* after processing all *m* queries. | [
"abacaba\n2\n3 6 1\n1 4 2\n"
] | [
"baabcaa\n"
] | The sample is described in problem statement. | [
{
"input": "abacaba\n2\n3 6 1\n1 4 2",
"output": "baabcaa"
},
{
"input": "u\n1\n1 1 1",
"output": "u"
},
{
"input": "p\n5\n1 1 5\n1 1 9\n1 1 10\n1 1 10\n1 1 4",
"output": "p"
},
{
"input": "ssssssssss\n5\n5 7 9\n3 9 3\n2 7 1\n7 7 10\n1 9 6",
"output": "ssssssssss"
},
{
"input": "tcpyzttcpo\n10\n2 3 6\n2 4 1\n2 6 9\n7 10 5\n2 3 5\n4 5 6\n3 4 5\n7 9 4\n9 10 7\n1 10 8",
"output": "zctycopttp"
},
{
"input": "yywlblbblw\n10\n4 7 2\n3 8 2\n4 10 6\n4 7 1\n3 9 6\n1 7 3\n3 7 3\n3 7 1\n1 8 7\n2 7 5",
"output": "bylwlwylbb"
},
{
"input": "thisisahacktest\n1\n1 2 1",
"output": "htisisahacktest"
},
{
"input": "ozozumuhackleyan\n1\n1 4 1",
"output": "zozoumuhackleyan"
},
{
"input": "lacongaithattuyet\n1\n1 1 1",
"output": "lacongaithattuyet"
}
] | 2,000 | 1,433,600 | 0 | 4,260 |
|
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"
}
] | 93 | 2,867,200 | 0 | 4,276 |
|
675 | Restoring Painting | [
"brute force",
"constructive algorithms",
"math"
] | null | null | Vasya works as a watchman in the gallery. Unfortunately, one of the most expensive paintings was stolen while he was on duty. He doesn't want to be fired, so he has to quickly restore the painting. He remembers some facts about it.
- The painting is a square 3<=Γ<=3, each cell contains a single integer from 1 to *n*, and different cells may contain either different or equal integers. - The sum of integers in each of four squares 2<=Γ<=2 is equal to the sum of integers in the top left square 2<=Γ<=2. - Four elements *a*, *b*, *c* and *d* are known and are located as shown on the picture below.
Help Vasya find out the number of distinct squares the satisfy all the conditions above. Note, that this number may be equal to 0, meaning Vasya remembers something wrong.
Two squares are considered to be different, if there exists a cell that contains two different integers in different squares. | The first line of the input contains five integers *n*, *a*, *b*, *c* and *d* (1<=β€<=*n*<=β€<=100<=000, 1<=β€<=*a*,<=*b*,<=*c*,<=*d*<=β€<=*n*)Β β maximum possible value of an integer in the cell and four integers that Vasya remembers. | Print one integerΒ β the number of distinct valid squares. | [
"2 1 1 1 2\n",
"3 3 1 2 3\n"
] | [
"2\n",
"6\n"
] | Below are all the possible paintings for the first sample. <img class="tex-graphics" src="https://espresso.codeforces.com/c4c53d4e7b6814d8aad7b72604b6089d61dadb48.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/46a6ad6a5d3db202f3779b045b9dc77fc2348cf1.png" style="max-width: 100.0%;max-height: 100.0%;"/>
In the second sample, only paintings displayed below satisfy all the rules. <img class="tex-graphics" src="https://espresso.codeforces.com/776f231305f8ce7c33e79e887722ce46aa8b6e61.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/2fce9e9a31e70f1e46ea26f11d7305b3414e9b6b.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/be084a4d1f7e475be1183f7dff10e9c89eb175ef.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/96afdb4a35ac14f595d29bea2282f621098902f4.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/79ca8d720334a74910514f017ecf1d0166009a03.png" style="max-width: 100.0%;max-height: 100.0%;"/> <img class="tex-graphics" src="https://espresso.codeforces.com/ad3c37e950bf5702d54f05756db35c831da59ad9.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "2 1 1 1 2",
"output": "2"
},
{
"input": "3 3 1 2 3",
"output": "6"
},
{
"input": "1 1 1 1 1",
"output": "1"
},
{
"input": "1000 522 575 426 445",
"output": "774000"
},
{
"input": "99000 52853 14347 64237 88869",
"output": "1296306000"
},
{
"input": "100000 2 2 2 2",
"output": "10000000000"
},
{
"input": "2 1 1 2 2",
"output": "0"
},
{
"input": "10 9 10 8 10",
"output": "70"
},
{
"input": "100 19 16 35 83",
"output": "1700"
},
{
"input": "1000 102 583 606 929",
"output": "150000"
},
{
"input": "10000 1816 3333 6908 7766",
"output": "4750000"
},
{
"input": "100000 80015 84290 50777 30497",
"output": "1696900000"
},
{
"input": "100000 64022 49026 55956 88430",
"output": "6866200000"
},
{
"input": "100000 10263 46628 10268 22948",
"output": "5095500000"
},
{
"input": "100000 81311 81584 51625 57276",
"output": "4600600000"
},
{
"input": "100000 77594 3226 21255 8541",
"output": "1291800000"
},
{
"input": "100000 65131 35523 58220 87645",
"output": "5478900000"
},
{
"input": "100000 83958 32567 91083 95317",
"output": "3012500000"
},
{
"input": "100000 36851 54432 21164 85520",
"output": "1806300000"
},
{
"input": "100000 55732 17473 23832 75148",
"output": "7422500000"
},
{
"input": "100000 60789 25296 49585 25237",
"output": "4015900000"
},
{
"input": "100000 92060 77234 58709 36956",
"output": "2637100000"
},
{
"input": "100000 87223 66046 27153 40823",
"output": "1470700000"
},
{
"input": "100000 3809 35468 34556 51158",
"output": "5173900000"
},
{
"input": "100000 35038 37363 95275 88903",
"output": "0"
},
{
"input": "100000 45274 9250 36558 49486",
"output": "6848000000"
},
{
"input": "100000 1 1 1 1",
"output": "10000000000"
},
{
"input": "100000 1 1 1 100000",
"output": "100000"
},
{
"input": "100000 1 1 100000 1",
"output": "100000"
},
{
"input": "100000 1 1 100000 100000",
"output": "0"
},
{
"input": "100000 1 100000 1 1",
"output": "100000"
},
{
"input": "100000 1 100000 1 100000",
"output": "0"
},
{
"input": "100000 1 100000 100000 1",
"output": "10000000000"
},
{
"input": "100000 1 100000 100000 100000",
"output": "100000"
},
{
"input": "100000 100000 1 1 1",
"output": "100000"
},
{
"input": "100000 100000 1 1 100000",
"output": "10000000000"
},
{
"input": "100000 100000 1 100000 1",
"output": "0"
},
{
"input": "100000 100000 1 100000 100000",
"output": "100000"
},
{
"input": "100000 100000 100000 1 1",
"output": "0"
},
{
"input": "100000 100000 100000 1 100000",
"output": "100000"
},
{
"input": "100000 100000 100000 100000 1",
"output": "100000"
},
{
"input": "100000 100000 100000 100000 100000",
"output": "10000000000"
},
{
"input": "3 3 3 1 1",
"output": "0"
},
{
"input": "10 1 2 5 10",
"output": "0"
},
{
"input": "5 1 1 5 5",
"output": "0"
},
{
"input": "4 4 4 1 1",
"output": "0"
},
{
"input": "10 10 10 1 1",
"output": "0"
},
{
"input": "5 5 5 1 1",
"output": "0"
},
{
"input": "100 100 100 1 1",
"output": "0"
},
{
"input": "3 1 1 3 3",
"output": "0"
},
{
"input": "10 2 10 1 10",
"output": "0"
},
{
"input": "7 7 7 1 1",
"output": "0"
},
{
"input": "5 5 3 4 1",
"output": "0"
},
{
"input": "7 1 1 7 7",
"output": "0"
},
{
"input": "100 1 1 100 100",
"output": "0"
},
{
"input": "123 1 2 3 100",
"output": "2829"
},
{
"input": "10 1 1 10 10",
"output": "0"
},
{
"input": "803 525 6 623 8",
"output": "0"
}
] | 124 | 307,200 | 3 | 4,286 |
|
963 | Frequency of String | [
"hashing",
"string suffix structures",
"strings"
] | null | null | You are given a string $s$. You should answer $n$ queries. The $i$-th query consists of integer $k_i$ and string $m_i$. The answer for this query is the minimum length of such a string $t$ that $t$ is a substring of $s$ and $m_i$ has at least $k_i$ occurrences as a substring in $t$.
A substring of a string is a continuous segment of characters of the string.
It is guaranteed that for any two queries the strings $m_i$ from these queries are different. | The first line contains string $s$ $(1 \leq \left | s \right | \leq 10^{5})$.
The second line contains an integer $n$ ($1 \leq n \leq 10^5$).
Each of next $n$ lines contains an integer $k_i$ $(1 \leq k_i \leq |s|)$ and a non-empty string $m_i$Β β parameters of the query with number $i$, in this order.
All strings in input consists of lowercase English letters. Sum of length of all strings in input doesn't exceed $10^5$. All $m_i$ are distinct. | For each query output the answer for it in a separate line.
If a string $m_{i}$ occurs in $s$ less that $k_{i}$ times, output -1. | [
"aaaaa\n5\n3 a\n3 aa\n2 aaa\n3 aaaa\n1 aaaaa\n",
"abbb\n7\n4 b\n1 ab\n3 bb\n1 abb\n2 bbb\n1 a\n2 abbb\n"
] | [
"3\n4\n4\n-1\n5\n",
"-1\n2\n-1\n3\n-1\n1\n-1\n"
] | none | [] | 0 | 0 | -1 | 4,301 |
|
165 | Burning Midnight Oil | [
"binary search",
"implementation"
] | null | null | One day a highly important task was commissioned to Vasya β writing a program in a night. The program consists of *n* lines of code. Vasya is already exhausted, so he works like that: first he writes *v* lines of code, drinks a cup of tea, then he writes as much as lines, drinks another cup of tea, then he writes lines and so on: , , , ...
The expression is regarded as the integral part from dividing number *a* by number *b*.
The moment the current value equals 0, Vasya immediately falls asleep and he wakes up only in the morning, when the program should already be finished.
Vasya is wondering, what minimum allowable value *v* can take to let him write not less than *n* lines of code before he falls asleep. | The input consists of two integers *n* and *k*, separated by spaces β the size of the program in lines and the productivity reduction coefficient, 1<=β€<=*n*<=β€<=109, 2<=β€<=*k*<=β€<=10. | Print the only integer β the minimum value of *v* that lets Vasya write the program in one night. | [
"7 2\n",
"59 9\n"
] | [
"4\n",
"54\n"
] | In the first sample the answer is *v*β=β4. Vasya writes the code in the following portions: first 4 lines, then 2, then 1, and then Vasya falls asleep. Thus, he manages to write 4β+β2β+β1β=β7 lines in a night and complete the task.
In the second sample the answer is *v*β=β54. Vasya writes the code in the following portions: 54, 6. The total sum is 54β+β6β=β60, that's even more than *n*β=β59. | [
{
"input": "7 2",
"output": "4"
},
{
"input": "59 9",
"output": "54"
},
{
"input": "1 9",
"output": "1"
},
{
"input": "11 2",
"output": "7"
},
{
"input": "747 2",
"output": "376"
},
{
"input": "6578 2",
"output": "3293"
},
{
"input": "37212 2",
"output": "18609"
},
{
"input": "12357 2",
"output": "6181"
},
{
"input": "7998332 2",
"output": "3999172"
},
{
"input": "86275251 2",
"output": "43137632"
},
{
"input": "75584551 2",
"output": "37792280"
},
{
"input": "6 3",
"output": "5"
},
{
"input": "43 4",
"output": "33"
},
{
"input": "811 3",
"output": "543"
},
{
"input": "3410 4",
"output": "2560"
},
{
"input": "21341 4",
"output": "16009"
},
{
"input": "696485 4",
"output": "522368"
},
{
"input": "8856748 3",
"output": "5904504"
},
{
"input": "2959379 4",
"output": "2219538"
},
{
"input": "831410263 3",
"output": "554273516"
},
{
"input": "2 5",
"output": "2"
},
{
"input": "19 6",
"output": "17"
},
{
"input": "715 7",
"output": "615"
},
{
"input": "9122 5",
"output": "7300"
},
{
"input": "89117 6",
"output": "74268"
},
{
"input": "689973 7",
"output": "591408"
},
{
"input": "3024524 5",
"output": "2419624"
},
{
"input": "67127156 6",
"output": "55939302"
},
{
"input": "412262167 7",
"output": "353367574"
},
{
"input": "6 8",
"output": "6"
},
{
"input": "59 9",
"output": "54"
},
{
"input": "246 10",
"output": "222"
},
{
"input": "5314 8",
"output": "4651"
},
{
"input": "15309 9",
"output": "13609"
},
{
"input": "35648 10",
"output": "32085"
},
{
"input": "3018012 8",
"output": "2640764"
},
{
"input": "92153348 9",
"output": "81914089"
},
{
"input": "177583558 10",
"output": "159825206"
},
{
"input": "1000000000 2",
"output": "500000008"
},
{
"input": "1000000000 3",
"output": "666666672"
},
{
"input": "1000000000 4",
"output": "750000005"
},
{
"input": "1000000000 5",
"output": "800000003"
},
{
"input": "1000000000 6",
"output": "833333338"
},
{
"input": "1000000000 7",
"output": "857142861"
},
{
"input": "1000000000 8",
"output": "875000004"
},
{
"input": "1000000000 9",
"output": "888888894"
},
{
"input": "1000000000 10",
"output": "900000001"
},
{
"input": "1 4",
"output": "1"
},
{
"input": "2 10",
"output": "2"
},
{
"input": "1 2",
"output": "1"
},
{
"input": "6 8",
"output": "6"
},
{
"input": "987862820 9",
"output": "878100288"
},
{
"input": "979591791 9",
"output": "870748262"
},
{
"input": "948889213 9",
"output": "843457081"
},
{
"input": "8 9",
"output": "8"
},
{
"input": "999999999 10",
"output": "900000000"
}
] | 154 | 2,867,200 | -1 | 4,307 |
|
331 | Oh Sweet Beaverette | [
"data structures",
"sortings"
] | null | null | β Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?
β Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?
At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.
Let's consider the woodland belt as a sequence of trees. Each tree *i* is described by the esthetic appeal *a**i* β some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!
The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:
- The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible; - the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same; - and of course, the walk should be successful: there must be at least two trees in the woodland belt left.
Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart? | The first line contains a single integer *n* β the initial number of trees in the woodland belt, 2<=β€<=*n*. The second line contains space-separated integers *a**i* β the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.
- to get 30 points, you need to solve the problem with constraints: *n*<=β€<=100 (subproblem A1); - to get 100 points, you need to solve the problem with constraints: *n*<=β€<=3Β·105 (subproblems A1+A2). | In the first line print two integers β the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees *k*.
In the next line print *k* integers β the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 to *n* from left to right.
If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal. | [
"5\n1 2 3 1 2\n",
"5\n1 -2 3 1 -2\n"
] | [
"8 1\n1 ",
"5 2\n2 5 "
] | none | [
{
"input": "5\n1 2 3 1 2",
"output": "8 1\n1 "
},
{
"input": "5\n1 -2 3 1 -2",
"output": "5 2\n2 5 "
},
{
"input": "2\n0 0",
"output": "0 0"
},
{
"input": "3\n0 -1 0",
"output": "0 1\n2 "
},
{
"input": "3\n1 1 1",
"output": "3 0"
},
{
"input": "4\n-1 1 1 -1",
"output": "2 2\n1 4 "
},
{
"input": "4\n-1 1 -1 1",
"output": "2 2\n1 3 "
},
{
"input": "2\n-1 -1",
"output": "-2 0"
},
{
"input": "3\n-1 0 -1",
"output": "-2 0"
},
{
"input": "6\n-1 3 3 5 5 -1",
"output": "14 0"
},
{
"input": "2\n-1000000000 -1000000000",
"output": "-2000000000 0"
},
{
"input": "3\n-1000000000 -1000000000 -1000000000",
"output": "-2000000000 1\n3 "
},
{
"input": "3\n1000000000 1000000000 1000000000",
"output": "3000000000 0"
},
{
"input": "10\n-589330597 -126288833 -126288833 -834860352 -834860352 -834860352 -834860352 -21170405 -834860352 -834860352",
"output": "-252577666 8\n1 4 5 6 7 8 9 10 "
},
{
"input": "20\n-808998072 733614990 579897311 -337992089 579897311 120800519 -337992089 -803027570 733614990 -686536765 733614990 -803027570 -803027570 733614990 120800519 -803027570 -686536765 579897311 -808998072 -686536765",
"output": "4215055101 13\n1 4 7 8 10 12 13 15 16 17 18 19 20 "
}
] | 92 | 0 | 0 | 4,320 |
|
133 | Unary | [
"implementation"
] | null | null | Unary is a minimalistic Brainfuck dialect in which programs are written using only one token.
Brainfuck programs use 8 commands: "+", "-", "[", "]", "<", ">", "." and "," (their meaning is not important for the purposes of this problem). Unary programs are created from Brainfuck programs using the following algorithm. First, replace each command with a corresponding binary code, using the following conversion table:
- ">" <=β<= 1000, - "<" <=β<= 1001, - "+" <=β<= 1010, - "-" <=β<= 1011, - "." <=β<= 1100, - "," <=β<= 1101, - "[" <=β<= 1110, - "]" <=β<= 1111.
Next, concatenate the resulting binary codes into one binary number in the same order as in the program. Finally, write this number using unary numeral system β this is the Unary program equivalent to the original Brainfuck one.
You are given a Brainfuck program. Your task is to calculate the size of the equivalent Unary program, and print it modulo 1000003 (106<=+<=3). | The input will consist of a single line *p* which gives a Brainfuck program. String *p* will contain between 1 and 100 characters, inclusive. Each character of *p* will be "+", "-", "[", "]", "<", ">", "." or ",". | Output the size of the equivalent Unary program modulo 1000003 (106<=+<=3). | [
",.\n",
"++++[>,.<-]\n"
] | [
"220\n",
"61425\n"
] | To write a number *n* in unary numeral system, one simply has to write 1 *n* times. For example, 5 written in unary system will be 11111.
In the first example replacing Brainfuck commands with binary code will give us 1101 1100. After we concatenate the codes, we'll get 11011100 in binary system, or 220 in decimal. That's exactly the number of tokens in the equivalent Unary program. | [
{
"input": ",.",
"output": "220"
},
{
"input": "++++[>,.<-]",
"output": "61425"
},
{
"input": "[-],<],<<,<[,>,+>[[<>.,[>-[-[<><>><<<<]>,.-].>-[[>+,>,[,-,.-,-[[]>..<>,<[+,-<]-++.<+.]<,[[.<<-><<<],",
"output": "43789"
},
{
"input": "+",
"output": "10"
},
{
"input": "-",
"output": "11"
},
{
"input": "<",
"output": "9"
},
{
"input": ">",
"output": "8"
},
{
"input": ".",
"output": "12"
},
{
"input": ",",
"output": "13"
},
{
"input": "[",
"output": "14"
},
{
"input": "]",
"output": "15"
},
{
"input": ",]+>.],,+->+>-[]][><,-]><]++<.,-[.>.<+.[.<,[-,,[<]+>]->>]>]-+-+<][].,.]+][[<,-.+][+<<-+.],,,<,.]-].-",
"output": "859903"
},
{
"input": "][-+>,>[,<[<+-,[+[-.<+,<[.,<+<,>+],.]><+<,+<..[[[>,[<>+-<<[>,[>-->[>+[<+<[-<]]]<>.+-,.+++-+++-+>-.]+",
"output": "235230"
},
{
"input": "+]+<-]-<,>[,]<[][+<[+]>[[,",
"output": "221907"
},
{
"input": ".>]+,>->,.>[+>+<-.-+<<>-,..+-<.,>]>.<<,+-[].,],<,..-<[-",
"output": "223676"
},
{
"input": ">.><]..>,,<<-[.,]]+,+,>[<>>+]+++--,>.[+,,+,+[><+,+[<,-]<-,..[,,.[[><]]<[<.-++][.[]][<",
"output": "916864"
},
{
"input": "]+<+[,.[,]-,.][]..[.<[<-]]]+.<[]]>>]-+]-+-.>-.].,[+[]><-.[[]++<",
"output": "86015"
},
{
"input": "-[.<>].[,>,]>++<+].>,<<],,,]++<[<+,,,,[.]<[-[,,]-..+<++].----]++><,+.,>+,+[,-[<.]-+++][-]<+.<",
"output": "170107"
},
{
"input": "<.,+.><[,.+<[,.,<-,[>,",
"output": "982288"
},
{
"input": "[,+.-.<],,]-]-[[,[]+,[.]][>],,]<[>,<+<<>>].>]>][->+>",
"output": "411338"
},
{
"input": "+]]],,>],][],<+.[->,>..<-+]][>><.+>[][.]<,>-..-,..-]>-]+>,><+<<.+>.,++]<]],],<+-<.",
"output": "113966"
},
{
"input": ".<>.+]>],>><",
"output": "228058"
},
{
"input": "-[.<++]-,-]-,[<<+[,-+]+[[...,[-...,<>+[]>][+.],[-[>>-->---+-+]>>><-++]]-++>][,],<[[,+],++<---<[",
"output": "709697"
},
{
"input": "]<><]>,>]-]],[,>+[->,,[<-+,.][[++[,+.<[,[-][[>.]<.].+-,,]]+[->]]-][>[].,>.,],,>,]-]]<+[,>>-]+]",
"output": "283602"
},
{
"input": "<-[>[,.+>-]<-[[]+[.]--<-[[]->[.<<,,.,+[.][].,<<]],,+[.>+.>+-<.-+[-,-<][+,[>[.,.+,[+[]+<-.>-<>",
"output": "204479"
},
{
"input": "+,+><[>..,]-.[.++[>-<<-,-.>,.>]+,<,]]<[>,-+,.-[+,[.[<.-[].+>[,>-.>>]-[-][+,>>-,+<-,<.+-.+[.,",
"output": "537427"
},
{
"input": ">]-[.-+[,,]].]+,][[>>[+][,<+,>.<[],.>+[]-[,[[+],..>..<[>.,,,+]]<+++<][[>..>>+-]+][--],]<[]",
"output": "952413"
},
{
"input": ",><[-]-,],+<<]>.][]][+]>.[-]]>++-.+[.<[,.-,<,[,,>,],,>-<+],>->-[<<.,>>,<][,<-->+-..+.,>>.",
"output": "11994"
},
{
"input": "[.[[+.<<>,+,>],<][+-],>.]<+]>><<][+-,][.>[-,.>--][-[]>]-<>,+<<>+,]][.>>.<,>.<..]>]<][-.[",
"output": "386152"
},
{
"input": "-,]]]+[]-,+]>][>[[->,..-.,[[.<,,.,+[].[[[-.][.<.,.<.>[.,+.,<[-]-[--<,>+-,.,.[.,]+.>>--,",
"output": "533116"
},
{
"input": "]+,]>>+-+++<[].][[.]->,+]]>>,<>>+<+,>]",
"output": "694915"
},
{
"input": ".[.+<,->[++,]]++[[<-.]][.<.<]<-,>]]>.",
"output": "626679"
},
{
"input": "+<.[[<,]<-<[<[-]<<.>]]]<--.<,-++<<<[,<.>+<+[>-,.->,<[>-><<>-<[.,+<][+],>,],],<[[,+.],<,.-,-",
"output": "7032"
},
{
"input": ".,,>-,<-+,-<[,<>",
"output": "900168"
},
{
"input": ">[[<][[><]+.+.[..],.<,<[],]<[>]-.-+<+->]],",
"output": "419600"
},
{
"input": "].<.<.,++[>--[++[><",
"output": "983198"
},
{
"input": ",]--++..<>.+.,-[-.],,<++.+<<-+[<,,.,++],>[+>",
"output": "647820"
},
{
"input": ".<],>>[[+.+]><<<>,,+][.,-+-+<>-[,+><].+-+<[],+-+]<].>]<+-.][,,+>],[,[+",
"output": "898085"
},
{
"input": ">,>+,-,+<-[[]][-,[<][]>.+]+<].>]+][]][,...<,-,]",
"output": "586457"
},
{
"input": "+[-][]..+,<<+,++<<][<,]<[][+,+,++[+-],->],-.--<-[.]+]-+]<][,.>.+[<+]<+<>-",
"output": "240679"
},
{
"input": "-.+[.<[[<],.-<-[+-->.-->>[<<.[>,]>->[<.[-++>..,.[.",
"output": "185396"
},
{
"input": "<+[[],+,+[]-<]<<.+><,.<[.[-+>.+-]><+[]<]>[>]<<[<>.+[-><>]->>>,>.[[.>-+>]+],",
"output": "915891"
},
{
"input": "[-.].+<<]---+[+-+-[,[[[,]-<[-[[><>]",
"output": "765140"
},
{
"input": "[[>>[>[],+>-..]<]>-<-]<>].-[,,,.[+.-].-",
"output": "416600"
},
{
"input": "[,[.+-,,.>+-[+[][,[][,.-+>+]]<.,,.]<+><.[<,",
"output": "96775"
},
{
"input": "[>+,.+<<>..-+[>,><.-,--[+[>+>+[].[-[,][..<<[<,-<+-,<][][,>]++]+-<,,]++>.].[-[-[[,<[>><->]->+[+-",
"output": "89776"
},
{
"input": ">+,][>]]]+[-..<<<+]>>.+-++.+<.,>>-[+-,+.+>]<.>-[<>]<<+[>].[++[].[++++,<[+-<<[+<[]-+][>[-+.,,],<<,>+",
"output": "701493"
},
{
"input": "><",
"output": "137"
},
{
"input": ">]<-.+>>..<-,[-+.]+<<>[-,.],,,[,-+>>>>>.-]>,+<.+[,<>><",
"output": "481849"
},
{
"input": ">-[+>[++[,]-<<,.-->]+[<[-<>-]<,]<.+][]].]++]]+<,...>-[><,-",
"output": "739433"
},
{
"input": ">[][+...+[.-[,,>,[,-.].--[..>+<>[]<,],,<<,<>[<<.+>-[]+><]+,[+[",
"output": "356953"
},
{
"input": "<,+<-+[[-<[-,]",
"output": "570514"
},
{
"input": "<+.,,<[+-.+[<>[>.]+<[[<]<,<].-<-",
"output": "975622"
},
{
"input": ",-,[,,,.-]+]]>-<[+[.]]][[>-<[.[<->+.>[++[.><[+<].],]>,.,<+.--[",
"output": "243567"
},
{
"input": "]<[--]>[-.,,.[[.,-<.]+->+++,+<+]]<>>+].]>[>].[+,><->>++,[,><,,.<-]-+-+<].][-[<].<-->-+>]<]>,",
"output": "589486"
},
{
"input": "<].]->+,.],-][--<.--++",
"output": "425408"
},
{
"input": "-,,....<.<-<.[[,]<+-,,+.>].<>[<>,.+]>,,>.>[<>.+<,><,>",
"output": "474184"
},
{
"input": "].[+.,]].+->][.+-[[,--.-+.,,+]>+[-[,+.><-+[[--<[.->]<>+-]<.,]]>.<>-+,<.+[]>>[<+.][[",
"output": "9085"
},
{
"input": ">>.<<[-,.<.].",
"output": "518117"
},
{
"input": "-],,<<><.,.+][.[++-+..,,>-><>+,[,,,.[>.-+]]",
"output": "670018"
},
{
"input": "],[>>-,].><-.[]>.-+]<<<+]>-,+,.,[+[<+[],]-+>+>+--+]+<,]-+],><>+,>.-,+]-,,",
"output": "965603"
},
{
"input": ">..-",
"output": "36043"
},
{
"input": "-[..[]+>>.>,]>.+]>.]>[.>+,>+",
"output": "860720"
},
{
"input": "]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]",
"output": "266644"
},
{
"input": "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]",
"output": "47113"
},
{
"input": "[][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][]",
"output": "889131"
},
{
"input": ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>",
"output": "208877"
}
] | 124 | 0 | 3 | 4,325 |
|
320 | Ping-Pong (Easy Version) | [
"dfs and similar",
"graphs"
] | null | null | In this problem at each moment you have a set of intervals. You can move from interval (*a*,<=*b*) from our set to interval (*c*,<=*d*) from our set if and only if *c*<=<<=*a*<=<<=*d* or *c*<=<<=*b*<=<<=*d*. Also there is a path from interval *I*1 from our set to interval *I*2 from our set if there is a sequence of successive moves starting from *I*1 so that we can reach *I*2.
Your program should handle the queries of the following two types:
1. "1 x y" (*x*<=<<=*y*) β add the new interval (*x*,<=*y*) to the set of intervals. The length of the new interval is guaranteed to be strictly greater than all the previous intervals.1. "2 a b" (*a*<=β <=*b*) β answer the question: is there a path from *a*-th (one-based) added interval to *b*-th (one-based) added interval?
Answer all the queries. Note, that initially you have an empty set of intervals. | The first line of the input contains integer *n* denoting the number of queries, (1<=β€<=*n*<=β€<=100). Each of the following lines contains a query as described above. All numbers in the input are integers and don't exceed 109 by their absolute value.
It's guaranteed that all queries are correct. | For each query of the second type print "YES" or "NO" on a separate line depending on the answer. | [
"5\n1 1 5\n1 5 11\n2 1 2\n1 2 9\n2 1 2\n"
] | [
"NO\nYES\n"
] | none | [
{
"input": "5\n1 1 5\n1 5 11\n2 1 2\n1 2 9\n2 1 2",
"output": "NO\nYES"
},
{
"input": "10\n1 -311 -186\n1 -1070 -341\n1 -1506 -634\n1 688 1698\n2 2 4\n1 70 1908\n2 1 2\n2 2 4\n1 -1053 1327\n2 5 4",
"output": "NO\nNO\nNO\nYES"
},
{
"input": "10\n1 -1365 -865\n1 1244 1834\n2 1 2\n1 -1508 -752\n2 3 2\n2 2 1\n1 -779 595\n1 -1316 877\n2 2 1\n1 -698 1700",
"output": "NO\nNO\nNO\nNO"
},
{
"input": "20\n1 1208 1583\n1 -258 729\n1 -409 1201\n1 194 1938\n1 -958 1575\n1 -1466 1752\n2 1 2\n2 1 2\n2 6 5\n1 -1870 1881\n1 -2002 2749\n1 -2002 2984\n1 -2002 3293\n2 2 4\n2 8 10\n2 9 6\n1 -2002 3572\n1 -2002 4175\n1 -2002 4452\n1 -2002 4605",
"output": "YES\nYES\nYES\nYES\nYES\nNO"
},
{
"input": "9\n1 1 4\n1 5 20\n1 11 30\n1 29 60\n1 59 100\n1 100 200\n2 1 5\n2 1 6\n2 2 5",
"output": "NO\nNO\nYES"
}
] | 124 | 0 | 0 | 4,334 |
|
787 | The Monster | [
"brute force",
"math",
"number theory"
] | null | null | A monster is chasing after Rick and Morty on another planet. They're so frightened that sometimes they scream. More accurately, Rick screams at times *b*,<=*b*<=+<=*a*,<=*b*<=+<=2*a*,<=*b*<=+<=3*a*,<=... and Morty screams at times *d*,<=*d*<=+<=*c*,<=*d*<=+<=2*c*,<=*d*<=+<=3*c*,<=....
The Monster will catch them if at any point they scream at the same time, so it wants to know when it will catch them (the first time they scream at the same time) or that they will never scream at the same time. | The first line of input contains two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=100).
The second line contains two integers *c* and *d* (1<=β€<=*c*,<=*d*<=β€<=100). | Print the first time Rick and Morty will scream at the same time, or <=-<=1 if they will never scream at the same time. | [
"20 2\n9 19\n",
"2 1\n16 12\n"
] | [
"82\n",
"-1\n"
] | In the first sample testcase, Rick's 5th scream and Morty's 8th time are at time 82.
In the second sample testcase, all Rick's screams will be at odd times and Morty's will be at even times, so they will never scream at the same time. | [
{
"input": "20 2\n9 19",
"output": "82"
},
{
"input": "2 1\n16 12",
"output": "-1"
},
{
"input": "39 52\n88 78",
"output": "1222"
},
{
"input": "59 96\n34 48",
"output": "1748"
},
{
"input": "87 37\n91 29",
"output": "211"
},
{
"input": "11 81\n49 7",
"output": "301"
},
{
"input": "39 21\n95 89",
"output": "3414"
},
{
"input": "59 70\n48 54",
"output": "1014"
},
{
"input": "87 22\n98 32",
"output": "718"
},
{
"input": "15 63\n51 13",
"output": "-1"
},
{
"input": "39 7\n97 91",
"output": "1255"
},
{
"input": "18 18\n71 71",
"output": "1278"
},
{
"input": "46 71\n16 49",
"output": "209"
},
{
"input": "70 11\n74 27",
"output": "2321"
},
{
"input": "94 55\n20 96",
"output": "-1"
},
{
"input": "18 4\n77 78",
"output": "1156"
},
{
"input": "46 44\n23 55",
"output": "-1"
},
{
"input": "74 88\n77 37",
"output": "1346"
},
{
"input": "94 37\n34 7",
"output": "789"
},
{
"input": "22 81\n80 88",
"output": "-1"
},
{
"input": "46 30\n34 62",
"output": "674"
},
{
"input": "40 4\n81 40",
"output": "364"
},
{
"input": "69 48\n39 9",
"output": "48"
},
{
"input": "89 93\n84 87",
"output": "5967"
},
{
"input": "17 45\n42 65",
"output": "317"
},
{
"input": "41 85\n95 46",
"output": "331"
},
{
"input": "69 30\n41 16",
"output": "1410"
},
{
"input": "93 74\n99 93",
"output": "-1"
},
{
"input": "17 19\n44 75",
"output": "427"
},
{
"input": "45 63\n98 53",
"output": "3483"
},
{
"input": "69 11\n48 34",
"output": "-1"
},
{
"input": "55 94\n3 96",
"output": "204"
},
{
"input": "100 100\n100 100",
"output": "100"
},
{
"input": "1 1\n1 1",
"output": "1"
},
{
"input": "1 1\n1 100",
"output": "100"
},
{
"input": "1 100\n100 1",
"output": "101"
},
{
"input": "98 1\n99 100",
"output": "9703"
},
{
"input": "98 1\n99 2",
"output": "9605"
},
{
"input": "97 2\n99 100",
"output": "4852"
},
{
"input": "3 3\n3 1",
"output": "-1"
},
{
"input": "3 2\n7 2",
"output": "2"
},
{
"input": "2 3\n2 5",
"output": "5"
},
{
"input": "2 3\n2 3",
"output": "3"
},
{
"input": "100 3\n100 5",
"output": "-1"
},
{
"input": "6 10\n12 14",
"output": "-1"
},
{
"input": "4 2\n4 4",
"output": "-1"
},
{
"input": "2 3\n2 2",
"output": "-1"
},
{
"input": "2 3\n4 99",
"output": "99"
},
{
"input": "1 5\n1 5",
"output": "5"
},
{
"input": "1 100\n3 1",
"output": "100"
},
{
"input": "2 2\n2 1",
"output": "-1"
},
{
"input": "2 10\n6 20",
"output": "20"
},
{
"input": "2 2\n2 10",
"output": "10"
},
{
"input": "3 7\n3 6",
"output": "-1"
},
{
"input": "1 100\n1 100",
"output": "100"
},
{
"input": "7 25\n39 85",
"output": "319"
},
{
"input": "84 82\n38 6",
"output": "82"
},
{
"input": "7 7\n7 14",
"output": "14"
}
] | 514 | 4,608,000 | 3 | 4,338 |
|
0 | none | [
"none"
] | 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"
}
] | 62 | 1,228,800 | 0 | 4,342 |
|
879 | Borya's Diagnosis | [
"implementation"
] | null | null | It seems that Borya is seriously sick. He is going visit *n* doctors to find out the exact diagnosis. Each of the doctors needs the information about all previous visits, so Borya has to visit them in the prescribed order (i.e. Borya should first visit doctor 1, then doctor 2, then doctor 3 and so on). Borya will get the information about his health from the last doctor.
Doctors have a strange working schedule. The doctor *i* goes to work on the *s**i*-th day and works every *d**i* day. So, he works on days *s**i*,<=*s**i*<=+<=*d**i*,<=*s**i*<=+<=2*d**i*,<=....
The doctor's appointment takes quite a long time, so Borya can not see more than one doctor per day. What is the minimum time he needs to visit all doctors? | First line contains an integer *n* β number of doctors (1<=β€<=*n*<=β€<=1000).
Next *n* lines contain two numbers *s**i* and *d**i* (1<=β€<=*s**i*,<=*d**i*<=β€<=1000). | Output a single integer β the minimum day at which Borya can visit the last doctor. | [
"3\n2 2\n1 2\n2 2\n",
"2\n10 1\n6 5\n"
] | [
"4\n",
"11\n"
] | In the first sample case, Borya can visit all doctors on days 2, 3 and 4.
In the second sample case, Borya can visit all doctors on days 10 and 11. | [
{
"input": "3\n2 2\n1 2\n2 2",
"output": "4"
},
{
"input": "2\n10 1\n6 5",
"output": "11"
},
{
"input": "3\n6 10\n3 3\n8 2",
"output": "10"
},
{
"input": "4\n4 8\n10 10\n4 2\n8 2",
"output": "14"
},
{
"input": "5\n7 1\n5 1\n6 1\n1 6\n6 8",
"output": "14"
},
{
"input": "6\n1 3\n2 5\n4 7\n7 5\n6 8\n8 8",
"output": "16"
},
{
"input": "10\n4 10\n8 7\n6 5\n2 1\n2 3\n8 8\n2 4\n2 2\n6 7\n7 9",
"output": "34"
},
{
"input": "1\n1 1",
"output": "1"
},
{
"input": "1\n1000 1000",
"output": "1000"
},
{
"input": "42\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2\n1 2",
"output": "83"
},
{
"input": "2\n5 5\n5 1",
"output": "6"
},
{
"input": "2\n5 5\n5 5",
"output": "10"
},
{
"input": "2\n1 1\n1 1",
"output": "2"
},
{
"input": "2\n1 6\n7 1",
"output": "7"
},
{
"input": "2\n4 3\n4 5",
"output": "9"
},
{
"input": "2\n1 2\n1 3",
"output": "4"
},
{
"input": "3\n2 3\n5 1\n2 1",
"output": "6"
},
{
"input": "2\n2 1\n6 3",
"output": "6"
},
{
"input": "3\n10 1\n4 4\n12 1",
"output": "13"
},
{
"input": "2\n2 2\n10 2",
"output": "10"
},
{
"input": "2\n1 1\n1000 2",
"output": "1000"
},
{
"input": "14\n1000 1\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1 1",
"output": "13001"
},
{
"input": "2\n2 4\n2 1",
"output": "3"
},
{
"input": "3\n1 100\n100 3\n200 1",
"output": "200"
},
{
"input": "7\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1 1",
"output": "6001"
},
{
"input": "2\n5 5\n15 5",
"output": "15"
},
{
"input": "2\n2 2\n2 4",
"output": "6"
},
{
"input": "2\n1 1\n10 1",
"output": "10"
},
{
"input": "2\n10 1\n100 1",
"output": "100"
},
{
"input": "15\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1000 1000\n1 1",
"output": "14001"
},
{
"input": "3\n2 1\n1 3\n4 7",
"output": "11"
},
{
"input": "2\n5 5\n100 5",
"output": "100"
},
{
"input": "2\n1 10\n2 30",
"output": "2"
},
{
"input": "4\n2 2\n2 2\n2 2\n2 2",
"output": "8"
},
{
"input": "1\n10 10",
"output": "10"
}
] | 62 | 0 | 0 | 4,362 |
|
975 | Mancala | [
"brute force",
"implementation"
] | null | null | Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes.
Initially, each hole has $a_i$ stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction.
Note that the counter-clockwise order means if the player takes the stones from hole $i$, he will put one stone in the $(i+1)$-th hole, then in the $(i+2)$-th, etc. If he puts a stone in the $14$-th hole, the next one will be put in the first hole.
After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli.
Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move. | The only line contains 14 integers $a_1, a_2, \ldots, a_{14}$ ($0 \leq a_i \leq 10^9$)Β β the number of stones in each hole.
It is guaranteed that for any $i$ ($1\leq i \leq 14$) $a_i$ is either zero or odd, and there is at least one stone in the board. | Output one integer, the maximum possible score after one move. | [
"0 1 1 0 0 0 0 0 0 7 0 0 0 0\n",
"5 1 1 1 1 0 0 0 0 0 0 0 0 0\n"
] | [
"4\n",
"8\n"
] | In the first test case the board after the move from the hole with $7$ stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to $4$. | [
{
"input": "0 1 1 0 0 0 0 0 0 7 0 0 0 0",
"output": "4"
},
{
"input": "5 1 1 1 1 0 0 0 0 0 0 0 0 0",
"output": "8"
},
{
"input": "10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 10001 1",
"output": "54294"
},
{
"input": "0 0 0 0 0 0 0 0 0 0 0 0 0 15",
"output": "2"
},
{
"input": "1 0 0 0 0 1 0 0 0 0 1 0 0 0",
"output": "0"
},
{
"input": "5 5 1 1 1 3 3 3 5 7 5 3 7 5",
"output": "38"
},
{
"input": "787 393 649 463 803 365 81 961 989 531 303 407 579 915",
"output": "7588"
},
{
"input": "8789651 4466447 1218733 6728667 1796977 6198853 8263135 6309291 8242907 7136751 3071237 5397369 6780785 9420869",
"output": "81063456"
},
{
"input": "0 0 0 0 0 0 0 0 0 0 0 0 0 29",
"output": "26"
},
{
"input": "282019717 109496191 150951267 609856495 953855615 569750143 6317733 255875779 645191029 572053369 290936613 338480779 879775193 177172893",
"output": "5841732816"
},
{
"input": "105413505 105413505 105413505 105413505 105413505 105413505 105413505 105413505 105413505 105413505 105413505 105413505 105413505 105413505",
"output": "120472578"
},
{
"input": "404418821 993626161 346204297 122439813 461187221 628048227 625919459 628611733 938993057 701270099 398043779 684205961 630975553 575964835",
"output": "8139909016"
},
{
"input": "170651077 730658441 824213789 583764177 129437345 717005779 675398017 314979709 380861369 265878463 746564659 797260041 506575735 335169317",
"output": "6770880638"
},
{
"input": "622585025 48249287 678950449 891575125 637411965 457739735 829353393 235216425 284006447 875591469 492839209 296444305 513776057 810057753",
"output": "7673796644"
},
{
"input": "475989857 930834747 786217439 927967137 489188151 869354161 276693267 56154399 131055697 509249443 143116853 426254423 44465165 105798821",
"output": "6172339560"
},
{
"input": "360122921 409370351 226220005 604004145 85173909 600403773 624052991 138163383 729239967 189036661 619842883 270087537 749500483 243727913",
"output": "5848946922"
},
{
"input": "997102881 755715147 273805839 436713689 547411799 72470207 522269145 647688957 137422311 422612659 197751751 679663349 821420227 387967237",
"output": "6900015198"
},
{
"input": "690518849 754551537 652949719 760695679 491633619 477564457 11669279 700467439 470069297 782338983 718169393 884421719 24619427 215745577",
"output": "7635414974"
},
{
"input": "248332749 486342237 662201929 917696895 555278549 252122023 850296207 463343655 832574345 954281071 168282553 825538865 996753493 461254663",
"output": "6400166934"
},
{
"input": "590789361 636464947 404477303 337309187 476703809 426863069 120608741 703406277 645444697 761482231 996635839 33459441 677458865 483861751",
"output": "7294965518"
},
{
"input": "297857621 238127103 749085829 139033277 597985489 202617713 982184715 183932743 278551059 297781685 330124279 338959601 682874531 187519685",
"output": "5201808164"
},
{
"input": "1 1 1 1 1 0 0 0 0 0 0 0 0 0",
"output": "2"
},
{
"input": "1 1 0 0 0 0 0 0 0 0 0 0 0 0",
"output": "2"
},
{
"input": "1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "2"
},
{
"input": "1 0 0 0 0 0 0 0 0 0 0 0 0 1",
"output": "2"
},
{
"input": "0 0 0 0 0 0 0 0 0 0 0 0 1 1",
"output": "2"
}
] | 46 | 409,600 | 3 | 4,364 |
|
992 | Nastya and an Array | [
"implementation",
"sortings"
] | null | null | Nastya owns too many arrays now, so she wants to delete the least important of them. However, she discovered that this array is magic! Nastya now knows that the array has the following properties:
- In one second we can add an arbitrary (possibly negative) integer to all elements of the array that are not equal to zero. - When all elements of the array become equal to zero, the array explodes.
Nastya is always busy, so she wants to explode the array as fast as possible. Compute the minimum time in which the array can be exploded. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=105) β the size of the array.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=105<=β€<=*a**i*<=β€<=105) β the elements of the array. | Print a single integer β the minimum number of seconds needed to make all elements of the array equal to zero. | [
"5\n1 1 1 1 1\n",
"3\n2 0 -1\n",
"4\n5 -6 -5 1\n"
] | [
"1\n",
"2\n",
"4\n"
] | In the first example you can add β-β1 to all non-zero elements in one second and make them equal to zero.
In the second example you can add β-β2 on the first second, then the array becomes equal to [0,β0,ββ-β3]. On the second second you can add 3 to the third (the only non-zero) element. | [
{
"input": "5\n1 1 1 1 1",
"output": "1"
},
{
"input": "3\n2 0 -1",
"output": "2"
},
{
"input": "4\n5 -6 -5 1",
"output": "4"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "2\n21794 -79194",
"output": "2"
},
{
"input": "3\n-63526 95085 -5239",
"output": "3"
},
{
"input": "3\n0 53372 -20572",
"output": "2"
},
{
"input": "13\n-2075 -32242 27034 -37618 -96962 82203 64846 48249 -71761 28908 -21222 -61370 46899",
"output": "13"
},
{
"input": "5\n806 0 1308 1954 683",
"output": "4"
},
{
"input": "8\n-26 0 -249 -289 -126 -206 288 -11",
"output": "7"
},
{
"input": "10\n2 2 2 1 2 -1 0 2 -1 1",
"output": "3"
},
{
"input": "1\n8",
"output": "1"
},
{
"input": "3\n0 0 0",
"output": "0"
},
{
"input": "10\n1 2 3 4 5 6 7 8 9 10",
"output": "10"
},
{
"input": "5\n2 0 -1 0 0",
"output": "2"
},
{
"input": "2\n0 0",
"output": "0"
},
{
"input": "5\n0 0 0 0 0",
"output": "0"
},
{
"input": "2\n1 0",
"output": "1"
},
{
"input": "2\n-1 0",
"output": "1"
},
{
"input": "4\n0 0 0 0",
"output": "0"
},
{
"input": "8\n10 9 -1 0 0 3 2 3",
"output": "5"
},
{
"input": "5\n5 0 1 2 3",
"output": "4"
},
{
"input": "3\n1 1 0",
"output": "1"
},
{
"input": "1\n-1",
"output": "1"
},
{
"input": "5\n1 2 0 0 0",
"output": "2"
},
{
"input": "5\n1 0 0 0 0",
"output": "1"
},
{
"input": "5\n4 5 6 0 0",
"output": "3"
},
{
"input": "4\n-1 0 0 1",
"output": "2"
},
{
"input": "5\n3 0 0 4 5",
"output": "3"
},
{
"input": "3\n0 0 2",
"output": "1"
},
{
"input": "3\n1 0 0",
"output": "1"
},
{
"input": "4\n0 0 0 4",
"output": "1"
},
{
"input": "5\n-1 0 0 0 0",
"output": "1"
},
{
"input": "2\n0 1",
"output": "1"
},
{
"input": "3\n1 2 3",
"output": "3"
},
{
"input": "1\n5",
"output": "1"
},
{
"input": "10\n0 0 0 0 0 1 2 3 0 0",
"output": "3"
},
{
"input": "4\n0 1 2 3",
"output": "3"
},
{
"input": "3\n0 1 2",
"output": "2"
},
{
"input": "4\n2 0 0 -1",
"output": "2"
}
] | 1,000 | 13,619,200 | 0 | 4,380 |
|
88 | Chord | [
"brute force",
"implementation"
] | A. Chord | 2 | 256 | Vasya studies music.
He has learned lots of interesting stuff. For example, he knows that there are 12 notes: C, C#, D, D#, E, F, F#, G, G#, A, B, H. He also knows that the notes are repeated cyclically: after H goes C again, and before C stands H. We will consider the C note in the row's beginning and the C note after the H similar and we will identify them with each other. The distance between the notes along the musical scale is measured in tones: between two consecutive notes there's exactly one semitone, that is, 0.5 tone. The distance is taken from the lowest tone to the uppest one, that is, the distance between C and E is 4 semitones and between E and C is 8 semitones
Vasya also knows what a chord is. A chord is an unordered set of no less than three notes. However, for now Vasya only works with triads, that is with the chords that consist of exactly three notes. He can already distinguish between two types of triads β major and minor.
Let's define a major triad. Let the triad consist of notes *X*, *Y* and *Z*. If we can order the notes so as the distance along the musical scale between *X* and *Y* equals 4 semitones and the distance between *Y* and *Z* is 3 semitones, then the triad is major. The distance between *X* and *Z*, accordingly, equals 7 semitones.
A minor triad is different in that the distance between *X* and *Y* should be 3 semitones and between *Y* and *Z* β 4 semitones.
For example, the triad "C E G" is major: between C and E are 4 semitones, and between E and G are 3 semitones. And the triplet "C# B F" is minor, because if we order the notes as "B C# F", than between B and C# will be 3 semitones, and between C# and F β 4 semitones.
Help Vasya classify the triad the teacher has given to him. | The only line contains 3 space-separated notes in the above-given notation. | Print "major" if the chord is major, "minor" if it is minor, and "strange" if the teacher gave Vasya some weird chord which is neither major nor minor. Vasya promises you that the answer will always be unambiguous. That is, there are no chords that are both major and minor simultaneously. | [
"C E G\n",
"C# B F\n",
"A B H\n"
] | [
"major\n",
"minor\n",
"strange\n"
] | none | [
{
"input": "C E G",
"output": "major"
},
{
"input": "C# B F",
"output": "minor"
},
{
"input": "A B H",
"output": "strange"
},
{
"input": "G H E",
"output": "minor"
},
{
"input": "D# B G",
"output": "major"
},
{
"input": "D# B F#",
"output": "minor"
},
{
"input": "F H E",
"output": "strange"
},
{
"input": "B F# G",
"output": "strange"
},
{
"input": "F# H C",
"output": "strange"
},
{
"input": "C# F C",
"output": "strange"
},
{
"input": "G# C# E",
"output": "minor"
},
{
"input": "D# H G#",
"output": "minor"
},
{
"input": "C F A",
"output": "major"
},
{
"input": "H E G#",
"output": "major"
},
{
"input": "G D# B",
"output": "major"
},
{
"input": "E C G",
"output": "major"
},
{
"input": "G# C# F",
"output": "major"
},
{
"input": "D# C G#",
"output": "major"
},
{
"input": "C# F B",
"output": "minor"
},
{
"input": "D# C G",
"output": "minor"
},
{
"input": "A D F",
"output": "minor"
},
{
"input": "F# H D",
"output": "minor"
},
{
"input": "D A F",
"output": "minor"
},
{
"input": "D A F#",
"output": "major"
},
{
"input": "C# B F",
"output": "minor"
},
{
"input": "A C F",
"output": "major"
},
{
"input": "D F# H",
"output": "minor"
},
{
"input": "H G# D#",
"output": "minor"
},
{
"input": "A D F#",
"output": "major"
},
{
"input": "H E G#",
"output": "major"
},
{
"input": "D# B F#",
"output": "minor"
},
{
"input": "D# H F#",
"output": "major"
},
{
"input": "A D F#",
"output": "major"
},
{
"input": "B G D#",
"output": "major"
},
{
"input": "E A C#",
"output": "major"
},
{
"input": "D H G",
"output": "major"
},
{
"input": "H D F#",
"output": "minor"
},
{
"input": "G D# C",
"output": "minor"
},
{
"input": "H D G",
"output": "major"
},
{
"input": "E C G",
"output": "major"
},
{
"input": "D# A E",
"output": "strange"
},
{
"input": "A F E",
"output": "strange"
},
{
"input": "C E F",
"output": "strange"
},
{
"input": "A B C",
"output": "strange"
},
{
"input": "E F D#",
"output": "strange"
},
{
"input": "C G# G#",
"output": "strange"
},
{
"input": "F D# G#",
"output": "strange"
},
{
"input": "B G D#",
"output": "major"
},
{
"input": "E E G#",
"output": "strange"
},
{
"input": "A G H",
"output": "strange"
},
{
"input": "F E A",
"output": "strange"
},
{
"input": "D B E",
"output": "strange"
},
{
"input": "G# C# D",
"output": "strange"
},
{
"input": "D# D# F#",
"output": "strange"
},
{
"input": "H B G",
"output": "strange"
},
{
"input": "D C B",
"output": "strange"
},
{
"input": "D B B",
"output": "strange"
},
{
"input": "C A H",
"output": "strange"
},
{
"input": "F# H F#",
"output": "strange"
},
{
"input": "A F F#",
"output": "strange"
},
{
"input": "C D C",
"output": "strange"
},
{
"input": "G F# A",
"output": "strange"
},
{
"input": "C C# D",
"output": "strange"
},
{
"input": "C A E",
"output": "minor"
},
{
"input": "A H B",
"output": "strange"
},
{
"input": "B E F#",
"output": "strange"
},
{
"input": "G# G# A",
"output": "strange"
},
{
"input": "B C# C#",
"output": "strange"
},
{
"input": "C G D#",
"output": "minor"
},
{
"input": "C B D#",
"output": "strange"
},
{
"input": "F H F",
"output": "strange"
},
{
"input": "E G# C",
"output": "strange"
},
{
"input": "F# F# F#",
"output": "strange"
},
{
"input": "F C F",
"output": "strange"
},
{
"input": "A F D",
"output": "minor"
}
] | 310 | 0 | 3.9225 | 4,389 |
253 | Physics Practical | [
"binary search",
"dp",
"sortings",
"two pointers"
] | null | null | One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as *n* measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had made his friend Petya redo the experiment because the largest and the smallest results differed by more than two times. Vasya is lazy, and he does not want to redo the experiment. He wants to do the task and go home play computer games. So he decided to cheat: before Vasya shows the measurements to the teacher, he will erase some of them, so as to make the largest and the smallest results of the remaining measurements differ in no more than two times. In other words, if the remaining measurements have the smallest result *x*, and the largest result *y*, then the inequality *y*<=β€<=2Β·*x* must fulfill. Of course, to avoid the teacher's suspicion, Vasya wants to remove as few measurement results as possible from his notes.
Help Vasya, find what minimum number of measurement results he will have to erase from his notes so that the largest and the smallest of the remaining results of the measurements differed in no more than two times. | The first line contains integer *n* (2<=β€<=*n*<=β€<=105) β the number of measurements Vasya made. The second line contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=5000) β the results of the measurements. The numbers on the second line are separated by single spaces. | Print a single integer β the minimum number of results Vasya will have to remove. | [
"6\n4 5 3 8 3 7\n",
"4\n4 3 2 4\n"
] | [
"2\n",
"0\n"
] | In the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4. | [
{
"input": "6\n4 5 3 8 3 7",
"output": "2"
},
{
"input": "4\n4 3 2 4",
"output": "0"
},
{
"input": "6\n5 6 4 9 4 8",
"output": "1"
},
{
"input": "4\n5 4 1 5",
"output": "1"
},
{
"input": "2\n3 2",
"output": "0"
},
{
"input": "10\n39 9 18 13 6 16 47 15 1 24",
"output": "5"
},
{
"input": "20\n43 49 46 46 40 41 49 49 48 30 35 36 33 34 42 38 40 46 50 45",
"output": "0"
},
{
"input": "30\n6 1 26 13 16 30 16 23 9 1 5 14 7 2 17 22 21 23 16 3 5 17 22 10 1 24 4 30 8 18",
"output": "15"
},
{
"input": "50\n3 61 16 13 13 12 3 8 14 16 1 32 8 23 29 7 28 13 8 5 9 2 3 2 29 13 1 2 18 29 28 4 13 3 14 9 20 26 1 19 13 7 8 22 7 5 13 14 10 23",
"output": "29"
},
{
"input": "10\n135 188 160 167 179 192 195 192 193 191",
"output": "0"
},
{
"input": "15\n2 19 19 22 15 24 6 36 20 3 18 27 20 1 10",
"output": "6"
},
{
"input": "25\n8 1 2 1 2 5 3 4 2 6 3 3 4 1 6 1 6 1 4 5 2 9 1 2 1",
"output": "13"
},
{
"input": "40\n4784 4824 4707 4343 4376 4585 4917 4848 3748 4554 3390 4944 4845 3922 4617 4606 4815 4698 4595 4942 4327 4983 4833 4507 3721 4863 4633 4553 4991 4922 4733 4396 4747 4724 4886 4226 4025 4928 4990 4792",
"output": "0"
},
{
"input": "60\n1219 19 647 1321 21 242 677 901 10 165 434 978 448 163 919 517 1085 10 516 920 653 1363 62 98 629 928 998 1335 1448 85 357 432 1298 561 663 182 2095 801 59 208 765 1653 642 645 1378 221 911 749 347 849 43 1804 62 73 613 143 860 297 278 148",
"output": "37"
},
{
"input": "100\n4204 4719 4688 3104 4012 4927 4696 4614 4826 4792 3891 4672 4914 4740 4968 3879 4424 4755 3856 3837 4965 4939 4030 4941 4504 4668 4908 4608 3660 4822 4846 3945 4539 4819 4895 3746 4324 4233 4135 4956 4983 4546 4673 4617 3533 4851 4868 4838 4998 4769 4899 4578 3841 4974 4627 4990 4524 4939 4469 4233 4434 4339 4446 4979 4354 4912 4558 4609 4436 3883 4379 4927 4824 4819 4984 4660 4874 3732 4853 4268 4761 4402 4642 4577 4635 4564 4113 4896 4943 4122 4413 4597 3768 4731 4669 4958 4548 4263 4657 3651",
"output": "0"
},
{
"input": "100\n1354 1797 588 3046 1290 745 217 907 113 381 523 935 791 415 92 1597 1739 1774 240 27 1262 2498 52 1339 1031 1355 2036 230 489 7 69 877 530 2664 1230 940 2712 2651 3410 480 332 699 957 2257 1877 1940 452 1652 1216 3144 236 165 1109 888 1649 346 24 183 1061 1226 2694 3225 2021 1145 907 1671 1599 3395 942 1959 555 1281 675 1125 1386 732 1081 326 256 26 1009 1772 2687 1173 491 709 390 992 519 203 1029 1381 846 1515 705 2859 282 147 1824 299",
"output": "63"
},
{
"input": "100\n2794 2201 4935 564 2876 4472 4196 2571 2260 1479 1451 3497 245 2805 4834 3872 4294 1299 937 2983 1458 3278 1098 2990 4447 4337 4388 947 3708 3382 3694 4562 3827 2312 3760 1181 2830 1256 1054 1583 2094 931 86 2526 998 3420 2248 3461 3662 1715 5 4123 1051 545 3704 1084 1916 695 794 121 1000 1611 3674 1910 4795 2805 825 2392 3551 1148 3738 4650 791 288 1064 2011 2991 2116 2179 3333 1303 498 1610 3092 1935 3450 3524 2624 1596 2801 2290 2297 2327 1602 4779 3135 1231 4203 3283 3580",
"output": "51"
},
{
"input": "2\n1 5",
"output": "1"
}
] | 92 | 0 | 0 | 4,414 |
|
863 | 1-2-3 | [
"graphs",
"implementation"
] | null | null | Ilya is working for the company that constructs robots. Ilya writes programs for entertainment robots, and his current project is "Bob", a new-generation game robot. Ilya's boss wants to know his progress so far. Especially he is interested if Bob is better at playing different games than the previous model, "Alice".
So now Ilya wants to compare his robots' performance in a simple game called "1-2-3". This game is similar to the "Rock-Paper-Scissors" game: both robots secretly choose a number from the set {1,<=2,<=3} and say it at the same moment. If both robots choose the same number, then it's a draw and noone gets any points. But if chosen numbers are different, then one of the robots gets a point: 3 beats 2, 2 beats 1 and 1 beats 3.
Both robots' programs make them choose their numbers in such a way that their choice in (*i*<=+<=1)-th game depends only on the numbers chosen by them in *i*-th game.
Ilya knows that the robots will play *k* games, Alice will choose number *a* in the first game, and Bob will choose *b* in the first game. He also knows both robots' programs and can tell what each robot will choose depending on their choices in previous game. Ilya doesn't want to wait until robots play all *k* games, so he asks you to predict the number of points they will have after the final game. | The first line contains three numbers *k*, *a*, *b* (1<=β€<=*k*<=β€<=1018, 1<=β€<=*a*,<=*b*<=β€<=3).
Then 3 lines follow, *i*-th of them containing 3 numbers *A**i*,<=1, *A**i*,<=2, *A**i*,<=3, where *A**i*,<=*j* represents Alice's choice in the game if Alice chose *i* in previous game and Bob chose *j* (1<=β€<=*A**i*,<=*j*<=β€<=3).
Then 3 lines follow, *i*-th of them containing 3 numbers *B**i*,<=1, *B**i*,<=2, *B**i*,<=3, where *B**i*,<=*j* represents Bob's choice in the game if Alice chose *i* in previous game and Bob chose *j* (1<=β€<=*B**i*,<=*j*<=β€<=3). | Print two numbers. First of them has to be equal to the number of points Alice will have, and second of them must be Bob's score after *k* games. | [
"10 2 1\n1 1 1\n1 1 1\n1 1 1\n2 2 2\n2 2 2\n2 2 2\n",
"8 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3\n",
"5 1 1\n1 2 2\n2 2 2\n2 2 2\n1 2 2\n2 2 2\n2 2 2\n"
] | [
"1 9\n",
"5 2\n",
"0 0\n"
] | In the second example game goes like this:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1e21b6e200707470571d69c9946ace6b56f5279b.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The fourth and the seventh game are won by Bob, the first game is draw and the rest are won by Alice. | [
{
"input": "10 2 1\n1 1 1\n1 1 1\n1 1 1\n2 2 2\n2 2 2\n2 2 2",
"output": "1 9"
},
{
"input": "8 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3",
"output": "5 2"
},
{
"input": "5 1 1\n1 2 2\n2 2 2\n2 2 2\n1 2 2\n2 2 2\n2 2 2",
"output": "0 0"
},
{
"input": "1 1 1\n3 3 1\n1 1 1\n3 2 2\n2 2 2\n1 3 1\n3 3 2",
"output": "0 0"
},
{
"input": "1 3 1\n1 3 3\n2 3 2\n2 1 3\n1 3 2\n3 3 2\n3 1 1",
"output": "0 1"
},
{
"input": "10 2 1\n2 2 1\n3 2 2\n3 1 3\n3 1 3\n1 2 2\n3 3 2",
"output": "8 1"
},
{
"input": "10 1 2\n1 1 2\n2 1 2\n1 3 1\n2 3 3\n3 2 2\n3 2 1",
"output": "3 5"
},
{
"input": "1000000 2 3\n3 1 1\n3 1 1\n1 2 2\n3 1 1\n3 1 1\n1 1 3",
"output": "0 333334"
},
{
"input": "1000000 1 3\n1 2 3\n2 1 2\n2 1 2\n1 2 3\n1 1 1\n2 3 3",
"output": "999998 1"
},
{
"input": "1000000000000 1 3\n3 1 1\n3 2 1\n2 2 2\n2 2 1\n1 2 2\n1 1 3",
"output": "500000000001 499999999998"
},
{
"input": "1000000000000 3 2\n2 3 3\n2 1 2\n1 1 1\n2 3 1\n1 3 3\n3 3 3",
"output": "500000000001 499999999999"
},
{
"input": "1000000000000000000 2 3\n1 3 1\n2 3 3\n2 2 2\n1 2 3\n3 1 2\n2 2 2",
"output": "1 500000000000000000"
},
{
"input": "999999999999999999 2 2\n2 3 2\n2 1 2\n1 3 3\n2 2 2\n1 3 2\n1 2 1",
"output": "499999999999999999 0"
},
{
"input": "1000000000000000000 2 1\n3 1 2\n2 3 3\n1 2 3\n2 2 3\n1 1 3\n2 3 2",
"output": "1000000000000000000 0"
},
{
"input": "1000000000000000000 3 3\n2 1 3\n1 2 3\n1 3 2\n3 2 2\n3 1 3\n3 3 1",
"output": "750000000000000000 0"
},
{
"input": "1000000000000000000 3 1\n2 3 2\n2 2 1\n2 3 3\n3 3 3\n2 1 1\n1 2 1",
"output": "500000000000000000 1"
},
{
"input": "478359268475263455 1 1\n3 2 3\n2 3 3\n2 1 1\n3 3 3\n2 3 3\n1 3 1",
"output": "0 0"
},
{
"input": "837264528963824683 3 3\n3 1 1\n1 3 1\n1 3 1\n3 2 1\n2 3 3\n2 2 2",
"output": "0 837264528963824682"
},
{
"input": "129341234876124184 1 2\n1 3 3\n1 1 2\n1 2 3\n3 1 1\n3 1 3\n3 2 3",
"output": "64670617438062091 64670617438062093"
},
{
"input": "981267318925341267 3 2\n1 2 1\n3 2 2\n3 3 3\n3 2 2\n2 2 3\n2 2 1",
"output": "981267318925341267 0"
},
{
"input": "12 2 2\n1 1 2\n2 2 3\n3 3 1\n2 3 1\n2 3 1\n2 3 1",
"output": "3 5"
},
{
"input": "3 1 3\n1 1 2\n2 1 3\n3 3 3\n2 3 1\n1 1 3\n3 3 3",
"output": "3 0"
},
{
"input": "3 2 2\n1 1 2\n2 1 3\n3 3 3\n2 3 1\n1 1 3\n3 3 3",
"output": "0 1"
},
{
"input": "67 1 1\n1 1 2\n2 2 3\n3 3 1\n2 3 1\n2 3 1\n2 3 1",
"output": "23 22"
},
{
"input": "4991 1 2\n1 1 2\n2 2 3\n3 1 3\n2 3 1\n2 3 1\n2 1 3",
"output": "1872 1872"
},
{
"input": "3 1 1\n1 1 2\n2 1 3\n3 3 3\n2 3 1\n1 1 3\n3 3 3",
"output": "1 1"
},
{
"input": "4 1 1\n1 1 2\n2 2 3\n3 3 1\n2 3 1\n2 3 1\n2 3 1",
"output": "2 1"
},
{
"input": "1 2 1\n1 2 3\n1 3 2\n2 1 3\n1 2 3\n3 3 3\n2 1 3",
"output": "1 0"
},
{
"input": "1000000000002 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3",
"output": "666666666668 333333333333"
},
{
"input": "1000000000005 1 1\n2 2 1\n3 3 1\n3 1 3\n1 1 1\n2 1 1\n1 2 3",
"output": "666666666670 333333333334"
},
{
"input": "4 3 1\n1 1 2\n2 1 3\n3 3 3\n2 3 1\n1 1 3\n3 3 3",
"output": "0 1"
}
] | 61 | 204,800 | -1 | 4,415 |
|
808 | Selling Souvenirs | [
"binary search",
"dp",
"greedy",
"ternary search"
] | null | null | After several latest reforms many tourists are planning to visit Berland, and Berland people understood that it's an opportunity to earn money and changed their jobs to attract tourists. Petya, for example, left the IT corporation he had been working for and started to sell souvenirs at the market.
This morning, as usual, Petya will come to the market. Petya has *n* different souvenirs to sell; *i*th souvenir is characterised by its weight *w**i* and cost *c**i*. Petya knows that he might not be able to carry all the souvenirs to the market. So Petya wants to choose a subset of souvenirs such that its total weight is not greater than *m*, and total cost is maximum possible.
Help Petya to determine maximum possible total cost. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=100000, 1<=β€<=*m*<=β€<=300000) β the number of Petya's souvenirs and total weight that he can carry to the market.
Then *n* lines follow. *i*th line contains two integers *w**i* and *c**i* (1<=β€<=*w**i*<=β€<=3, 1<=β€<=*c**i*<=β€<=109) β the weight and the cost of *i*th souvenir. | Print one number β maximum possible total cost of souvenirs that Petya can carry to the market. | [
"1 1\n2 1\n",
"2 2\n1 3\n2 2\n",
"4 3\n3 10\n2 7\n2 8\n1 1\n"
] | [
"0\n",
"3\n",
"10\n"
] | none | [
{
"input": "1 1\n2 1",
"output": "0"
},
{
"input": "2 2\n1 3\n2 2",
"output": "3"
},
{
"input": "4 3\n3 10\n2 7\n2 8\n1 1",
"output": "10"
},
{
"input": "5 5\n3 5\n2 6\n3 2\n1 1\n1 6",
"output": "13"
},
{
"input": "6 6\n1 6\n1 4\n1 8\n3 2\n3 2\n2 8",
"output": "26"
},
{
"input": "6 12\n1 7\n1 10\n2 8\n1 2\n2 9\n3 5",
"output": "41"
},
{
"input": "6 18\n3 3\n1 10\n2 10\n3 6\n1 3\n2 3",
"output": "35"
},
{
"input": "20 25\n2 13\n3 11\n1 32\n1 43\n3 85\n1 14\n2 57\n1 54\n1 38\n2 96\n2 89\n3 64\n1 79\n2 73\n1 73\n2 34\n1 52\n1 79\n1 42\n3 34",
"output": "990"
},
{
"input": "40 45\n2 82\n2 70\n2 48\n3 50\n2 15\n1 23\n1 80\n2 46\n1 20\n3 8\n3 81\n2 27\n1 59\n1 15\n3 95\n2 82\n2 40\n2 9\n2 61\n1 49\n2 5\n2 82\n1 55\n2 11\n1 26\n1 33\n1 2\n1 7\n3 57\n2 29\n1 59\n2 50\n3 63\n1 40\n1 99\n2 91\n2 39\n3 50\n1 75\n3 77",
"output": "1605"
},
{
"input": "4 28\n2 2\n3 1\n3 10\n1 9",
"output": "22"
},
{
"input": "10 5\n1 9\n1 8\n2 10\n3 4\n3 1\n2 2\n3 6\n1 1\n3 8\n2 2",
"output": "28"
},
{
"input": "10 12\n3 7\n3 6\n3 8\n3 2\n1 9\n2 5\n2 1\n2 5\n2 10\n2 9",
"output": "46"
},
{
"input": "1 29\n2 8",
"output": "8"
},
{
"input": "10 2\n3 4\n3 5\n3 7\n1 10\n1 2\n1 2\n1 8\n3 2\n1 8\n3 3",
"output": "18"
},
{
"input": "6 5\n3 1\n3 1\n1 2\n2 9\n3 10\n1 8",
"output": "20"
},
{
"input": "4 2\n3 4\n3 8\n1 1\n1 4",
"output": "5"
},
{
"input": "7 12\n2 10\n2 8\n2 1\n3 8\n3 8\n3 7\n1 7",
"output": "41"
},
{
"input": "70 203\n1 105\n1 105\n1 105\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300\n3 300",
"output": "20310"
},
{
"input": "10 6\n1 8\n1 10\n1 7\n2 9\n3 8\n1 8\n1 7\n1 4\n3 1\n3 8",
"output": "44"
},
{
"input": "2 40\n1 10\n3 6",
"output": "16"
},
{
"input": "7 6\n2 9\n3 10\n1 2\n2 6\n3 6\n2 1\n1 3",
"output": "22"
},
{
"input": "2 4\n3 8\n1 6",
"output": "14"
},
{
"input": "9 19\n2 5\n2 3\n3 9\n1 9\n3 8\n3 5\n3 4\n3 2\n3 6",
"output": "46"
},
{
"input": "13 23\n3 17\n2 83\n1 81\n3 83\n3 59\n3 71\n2 61\n3 8\n3 64\n2 80\n3 47\n1 46\n1 82",
"output": "711"
},
{
"input": "9 10\n3 6\n2 1\n2 4\n2 3\n3 6\n3 1\n1 8\n2 4\n3 3",
"output": "25"
},
{
"input": "3 4\n2 10\n2 10\n3 15",
"output": "20"
},
{
"input": "9 15\n3 8\n1 2\n2 5\n1 5\n3 3\n1 7\n1 7\n2 7\n2 9",
"output": "51"
},
{
"input": "8 21\n2 6\n3 3\n3 7\n3 8\n3 8\n3 8\n2 6\n3 9",
"output": "52"
},
{
"input": "6 7\n2 5\n2 4\n3 9\n3 2\n3 1\n3 8",
"output": "18"
},
{
"input": "8 5\n3 9\n3 3\n1 4\n3 1\n2 5\n3 1\n3 6\n3 1",
"output": "14"
},
{
"input": "1 1\n1 10",
"output": "10"
},
{
"input": "1 2\n2 10",
"output": "10"
},
{
"input": "5 9\n2 8\n3 7\n2 6\n1 4\n2 7",
"output": "28"
},
{
"input": "4 4\n2 13\n2 15\n2 5\n1 9",
"output": "28"
},
{
"input": "2 1\n1 5\n2 11",
"output": "5"
},
{
"input": "8 6\n1 9\n1 5\n1 3\n1 10\n3 8\n1 6\n1 4\n1 2",
"output": "37"
},
{
"input": "5 7\n1 8\n2 13\n2 13\n3 20\n3 14",
"output": "46"
},
{
"input": "52 102\n3 199\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100\n2 100",
"output": "5100"
},
{
"input": "3 4\n1 4\n2 10\n3 100",
"output": "104"
},
{
"input": "61 120\n3 5\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3\n2 3",
"output": "180"
}
] | 2,000 | 62,361,600 | 0 | 4,416 |
|
284 | Cows and Primitive Roots | [
"implementation",
"math",
"number theory"
] | null | null | The cows have just learned what a primitive root is! Given a prime *p*, a primitive root is an integer *x* (1<=β€<=*x*<=<<=*p*) such that none of integers *x*<=-<=1,<=*x*2<=-<=1,<=...,<=*x**p*<=-<=2<=-<=1 are divisible by *p*, but *x**p*<=-<=1<=-<=1 is.
Unfortunately, computing primitive roots can be time consuming, so the cows need your help. Given a prime *p*, help the cows find the number of primitive roots . | The input contains a single line containing an integer *p* (2<=β€<=*p*<=<<=2000). It is guaranteed that *p* is a prime. | Output on a single line the number of primitive roots . | [
"3\n",
"5\n"
] | [
"1\n",
"2\n"
] | The only primitive root <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3722298ba062e95b18705d1253eb4e5d31e3b2d1.png" style="max-width: 100.0%;max-height: 100.0%;"/> is 2.
The primitive roots <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/1d85c6a17ef1c42b53cf94d00bc49a7ac458fd58.png" style="max-width: 100.0%;max-height: 100.0%;"/> are 2 and 3. | [
{
"input": "3",
"output": "1"
},
{
"input": "5",
"output": "2"
},
{
"input": "7",
"output": "2"
},
{
"input": "11",
"output": "4"
},
{
"input": "17",
"output": "8"
},
{
"input": "19",
"output": "6"
},
{
"input": "1583",
"output": "672"
},
{
"input": "997",
"output": "328"
},
{
"input": "2",
"output": "1"
},
{
"input": "1987",
"output": "660"
},
{
"input": "1993",
"output": "656"
},
{
"input": "1997",
"output": "996"
},
{
"input": "1999",
"output": "648"
},
{
"input": "1297",
"output": "432"
},
{
"input": "163",
"output": "54"
},
{
"input": "1259",
"output": "576"
},
{
"input": "821",
"output": "320"
},
{
"input": "29",
"output": "12"
},
{
"input": "43",
"output": "12"
},
{
"input": "1949",
"output": "972"
},
{
"input": "1879",
"output": "624"
},
{
"input": "1759",
"output": "584"
},
{
"input": "1597",
"output": "432"
},
{
"input": "1619",
"output": "808"
},
{
"input": "1277",
"output": "560"
},
{
"input": "1097",
"output": "544"
},
{
"input": "953",
"output": "384"
},
{
"input": "751",
"output": "200"
},
{
"input": "607",
"output": "200"
},
{
"input": "509",
"output": "252"
},
{
"input": "431",
"output": "168"
},
{
"input": "317",
"output": "156"
},
{
"input": "211",
"output": "48"
},
{
"input": "127",
"output": "36"
},
{
"input": "101",
"output": "40"
},
{
"input": "31",
"output": "8"
}
] | 92 | 0 | 0 | 4,423 |
|
171 | Broken checker | [
"*special",
"brute force"
] | null | null | "This problem is rubbish! There is not statement, and there are only 5 test cases. The problemsetter took liberties with this problem!" β people complained in the comments to one round on Codeforces. And even more... No, wait, the checker for the problem was alright, that's a mercy. | The only line of the input contains an integer between 1 and 5, inclusive. All tests for this problem are different. The contents of the test case doesn't need to be equal to its index. | The only line of the output contains an integer between 1 and 3, inclusive. | [] | [] | This problem has no samples, since there so few test cases. | [
{
"input": "3",
"output": "1"
},
{
"input": "1",
"output": "2"
},
{
"input": "4",
"output": "2"
},
{
"input": "2",
"output": "3"
},
{
"input": "5",
"output": "1"
}
] | 186 | 0 | 0 | 4,426 |
|
11 | Jumping Jack | [
"math"
] | B. Jumping Jack | 1 | 64 | Jack is working on his jumping skills recently. Currently he's located at point zero of the number line. He would like to get to the point *x*. In order to train, he has decided that he'll first jump by only one unit, and each subsequent jump will be exactly one longer than the previous one. He can go either left or right with each jump. He wonders how many jumps he needs to reach *x*. | The input data consists of only one integer *x* (<=-<=109<=β€<=*x*<=β€<=109). | Output the minimal number of jumps that Jack requires to reach *x*. | [
"2\n",
"6\n",
"0\n"
] | [
"3\n",
"3\n",
"0\n"
] | none | [
{
"input": "2",
"output": "3"
},
{
"input": "6",
"output": "3"
},
{
"input": "0",
"output": "0"
},
{
"input": "-1000000000",
"output": "44723"
},
{
"input": "999961560",
"output": "44720"
},
{
"input": "999961561",
"output": "44721"
},
{
"input": "999961559",
"output": "44721"
},
{
"input": "-488979819",
"output": "31273"
},
{
"input": "-865918189",
"output": "41617"
},
{
"input": "-278253406",
"output": "23591"
},
{
"input": "-87275307",
"output": "13213"
},
{
"input": "103702792",
"output": "14403"
},
{
"input": "294680891",
"output": "24277"
},
{
"input": "485658989",
"output": "31166"
},
{
"input": "676637088",
"output": "36787"
},
{
"input": "867615187",
"output": "41657"
},
{
"input": "-941406715",
"output": "43393"
},
{
"input": "-897912263",
"output": "42377"
},
{
"input": "-706934164",
"output": "37603"
},
{
"input": "-790244010",
"output": "39755"
},
{
"input": "16417015",
"output": "5730"
},
{
"input": "823078041",
"output": "40573"
},
{
"input": "-517744582",
"output": "32179"
},
{
"input": "288916443",
"output": "24038"
},
{
"input": "-904422532",
"output": "42531"
},
{
"input": "-245245154",
"output": "22147"
},
{
"input": "561415872",
"output": "33511"
},
{
"input": "-779406751",
"output": "39482"
},
{
"input": "27254274",
"output": "7383"
},
{
"input": "845249034",
"output": "41116"
}
] | 92 | 0 | 0 | 4,428 |
524 | ΠΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ, Π²Ρ Π·Π½Π°Π΅ΡΠ΅ ΡΡΠΈΡ
Π»ΡΠ΄Π΅ΠΉ? | [
"implementation"
] | null | null | ΠΡΠ½ΠΎΠ²ΠΎΠΉ Π»ΡΠ±ΠΎΠΉ ΡΠΎΡΠΈΠ°Π»ΡΠ½ΠΎΠΉ ΡΠ΅ΡΠΈ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΎΡΠ½ΠΎΡΠ΅Π½ΠΈΠ΅ Π΄ΡΡΠΆΠ±Ρ ΠΌΠ΅ΠΆΠ΄Ρ Π΄Π²ΡΠΌΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΠΌΠΈ Π² ΡΠΎΠΌ ΠΈΠ»ΠΈ ΠΈΠ½ΠΎΠΌ ΡΠΌΡΡΠ»Π΅. Π ΠΎΠ΄Π½ΠΎΠΉ ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎΠΉ ΡΠΎΡΠΈΠ°Π»ΡΠ½ΠΎΠΉ ΡΠ΅ΡΠΈ Π΄ΡΡΠΆΠ±Π° ΡΠΈΠΌΠΌΠ΅ΡΡΠΈΡΠ½Π°, ΡΠΎ Π΅ΡΡΡ Π΅ΡΠ»ΠΈ *a* ΡΠ²Π»ΡΠ΅ΡΡΡ Π΄ΡΡΠ³ΠΎΠΌ *b*, ΡΠΎ *b* ΡΠ°ΠΊΠΆΠ΅ ΡΠ²Π»ΡΠ΅ΡΡΡ Π΄ΡΡΠ³ΠΎΠΌ *a*.
Π ΡΡΠΎΠΉ ΠΆΠ΅ ΡΠ΅ΡΠΈ Π΅ΡΡΡ ΡΡΠ½ΠΊΡΠΈΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠΈΡΡΠ΅Ρ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ Π»ΡΠ΄Π΅ΠΉ, ΠΈΠΌΠ΅ΡΡΠΈΡ
Π²ΡΡΠΎΠΊΡΡ Π²Π΅ΡΠΎΡΡΠ½ΠΎΡΡΡ Π±ΡΡΡ Π·Π½Π°ΠΊΠΎΠΌΡΠΌΠΈ Π΄Π»Ρ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ. ΠΡΠ° ΡΡΠ½ΠΊΡΠΈΡ ΡΠ°Π±ΠΎΡΠ°Π΅Ρ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ. ΠΠ°ΡΠΈΠΊΡΠΈΡΡΠ΅ΠΌ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ *x*. ΠΡΡΡΡ Π½Π΅ΠΊΠΎΡΠΎΡΡΠΉ Π΄ΡΡΠ³ΠΎΠΉ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊ *y*, Π½Π΅ ΡΠ²Π»ΡΡΡΠΈΠΉΡΡ Π΄ΡΡΠ³ΠΎΠΌ *x* Π½Π° ΡΠ΅ΠΊΡΡΠΈΠΉ ΠΌΠΎΠΌΠ΅Π½Ρ, ΡΠ²Π»ΡΠ΅ΡΡΡ Π΄ΡΡΠ³ΠΎΠΌ Π½Π΅ ΠΌΠ΅Π½Π΅Π΅, ΡΠ΅ΠΌ Π΄Π»Ρ *k*% Π΄ΡΡΠ·Π΅ΠΉ *x*. Π’ΠΎΠ³Π΄Π° ΠΎΠ½ ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅ΠΌΡΠΌ Π΄ΡΡΠ³ΠΎΠΌ Π΄Π»Ρ *x*.
Π£ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊΠ° Π² ΡΠΎΡΠΈΠ°Π»ΡΠ½ΠΎΠΉ ΡΠ΅ΡΠΈ Π΅ΡΡΡ ΡΠ²ΠΎΠΉ ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½ΡΠΉ ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡ β ΡΡΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΠΎΡ 1 Π΄ΠΎ 109. ΠΠ°ΠΌ Π΄Π°Π½ ΡΠΏΠΈΡΠΎΠΊ ΠΏΠ°Ρ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Π΅ΠΉ, ΡΠ²Π»ΡΡΡΠΈΡ
ΡΡ Π΄ΡΡΠ·ΡΡΠΌΠΈ. ΠΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΠ΅ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠΏΠΎΠΌΡΠ½ΡΡΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ ΠΌΠ½ΠΎΠΆΠ΅ΡΡΠ²ΠΎ Π΅Π³ΠΎ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅ΠΌΡΡ
Π΄ΡΡΠ·Π΅ΠΉ. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ ΡΠ»Π΅Π΄ΡΡΡ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° *m* ΠΈ *k* (1<=β€<=*m*<=β€<=100, 0<=β€<=*k*<=β€<=100) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΏΠ°Ρ Π΄ΡΡΠ·Π΅ΠΉ ΠΈ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΡΠΉ ΠΏΡΠΎΡΠ΅Π½Ρ ΠΎΠ±ΡΠΈΡ
Π΄ΡΡΠ·Π΅ΠΉ Π΄Π»Ρ ΡΠΎΠ³ΠΎ, ΡΡΠΎΠ±Ρ ΡΡΠΈΡΠ°ΡΡΡΡ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅ΠΌΡΠΌ Π΄ΡΡΠ³ΠΎΠΌ.
Π ΠΏΠΎΡΠ»Π΅Π΄ΡΡΡΠΈΡ
*m* ΡΡΡΠΎΠΊΠ°Ρ
Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ ΠΏΠΎ Π΄Π²Π° ΡΠΈΡΠ»Π° *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=109, *a**i*<=β <=*b**i*), ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡΠΈΡ
ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Π΅ΠΉ, ΡΠ²Π»ΡΡΡΠΈΡ
ΡΡ Π΄ΡΡΠ·ΡΡΠΌΠΈ.
ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ ΠΊΠ°ΠΆΠ΄Π°Ρ ΠΏΠ°ΡΠ° Π»ΡΠ΄Π΅ΠΉ ΡΠΈΠ³ΡΡΠΈΡΡΠ΅Ρ Π² ΡΠΏΠΈΡΠΊΠ΅ Π½Π΅ Π±ΠΎΠ»Π΅Π΅ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΡΠ°Π·Π°. | ΠΠ»Ρ Π²ΡΠ΅Ρ
ΡΠΏΠΎΠΌΡΠ½ΡΡΡΡ
Π»ΡΠ΄Π΅ΠΉ Π² ΠΏΠΎΡΡΠ΄ΠΊΠ΅ Π²ΠΎΠ·ΡΠ°ΡΡΠ°Π½ΠΈΡ id Π²ΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅ΠΌΡΡ
Π΄ΡΡΠ·ΡΡΡ
. ΠΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ Π΄ΠΎΠ»ΠΆΠ½Π° ΠΈΠΌΠ΅ΡΡ Π²ΠΈΠ΄ "*id*:<=Β *k*Β *id*1Β *id*2Β ...Β *id**k*", Π³Π΄Π΅ *id* β ΡΡΠΎ id ΡΠ°ΠΌΠΎΠ³ΠΎ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊΠ°, *k* β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π΅Π³ΠΎ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅ΠΌΡΡ
Π΄ΡΡΠ·Π΅ΠΉ, Π° *id*1, *id*2, ..., *id**k* β ΠΈΠ΄Π΅Π½ΡΠΈΡΠΈΠΊΠ°ΡΠΎΡΡ Π΅Π³ΠΎ ΠΏΡΠ΅Π΄ΠΏΠΎΠ»Π°Π³Π°Π΅ΠΌΡΡ
Π΄ΡΡΠ·Π΅ΠΉ Π² Π²ΠΎΠ·ΡΠ°ΡΡΠ°ΡΡΠ΅ΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. | [
"5 51\n10 23\n23 42\n39 42\n10 39\n39 58\n",
"5 100\n1 2\n1 3\n1 4\n2 3\n2 4\n"
] | [
"10: 1 42\n23: 1 39\n39: 1 23\n42: 1 10\n58: 2 10 42\n",
"1: 0\n2: 0\n3: 1 4\n4: 1 3\n"
] | none | [
{
"input": "5 51\n10 23\n23 42\n39 42\n10 39\n39 58",
"output": "10: 1 42\n23: 1 39\n39: 1 23\n42: 1 10\n58: 2 10 42"
},
{
"input": "5 100\n1 2\n1 3\n1 4\n2 3\n2 4",
"output": "1: 0\n2: 0\n3: 1 4\n4: 1 3"
},
{
"input": "4 1\n1 2\n1 3\n2 3\n4 5",
"output": "1: 0\n2: 0\n3: 0\n4: 0\n5: 0"
},
{
"input": "10 0\n648169314 459970755\n973677547 255163231\n982998000 498743911\n959912791 891928\n404623428 891928\n474720235 271683165\n709045873 539751127\n973677547 179588015\n629049356 622519100\n624998275 958914560",
"output": "891928: 15 179588015 255163231 271683165 459970755 474720235 498743911 539751127 622519100 624998275 629049356 648169314 709045873 958914560 973677547 982998000\n179588015: 16 891928 255163231 271683165 404623428 459970755 474720235 498743911 539751127 622519100 624998275 629049356 648169314 709045873 958914560 959912791 982998000\n255163231: 16 891928 179588015 271683165 404623428 459970755 474720235 498743911 539751127 622519100 624998275 629049356 648169314 709045873 958914560 959912791 982998000\n27168..."
},
{
"input": "10 100\n60976680 603454792\n575754027 696647370\n7534463 570826751\n117972518 472387015\n35713567 439985965\n439985965 928160845\n443596853 828504858\n689509731 117972518\n909843480 592765058\n251752353 490387136",
"output": "7534463: 0\n35713567: 1 928160845\n60976680: 0\n117972518: 0\n251752353: 0\n439985965: 0\n443596853: 0\n472387015: 1 689509731\n490387136: 0\n570826751: 0\n575754027: 0\n592765058: 0\n603454792: 0\n689509731: 1 472387015\n696647370: 0\n828504858: 0\n909843480: 0\n928160845: 1 35713567"
},
{
"input": "10 50\n389900784 512305545\n839319681 243581524\n653226215 616982889\n448655722 826601897\n681021965 23289895\n719595063 481480420\n919744525 839319681\n231872856 784056465\n971842495 248017394\n653226215 297224467",
"output": "23289895: 0\n231872856: 0\n243581524: 1 919744525\n248017394: 0\n297224467: 1 616982889\n389900784: 0\n448655722: 0\n481480420: 0\n512305545: 0\n616982889: 1 297224467\n653226215: 0\n681021965: 0\n719595063: 0\n784056465: 0\n826601897: 0\n839319681: 0\n919744525: 1 243581524\n971842495: 0"
},
{
"input": "10 0\n180745113 666631448\n362104151 349631376\n214251560 538865550\n562805929 576329835\n64121410 646478528\n283223383 861810719\n773038401 214251560\n64208401 693054606\n493180926 960545197\n159614568 831490031",
"output": "64121410: 17 64208401 159614568 180745113 214251560 283223383 349631376 362104151 493180926 538865550 562805929 576329835 666631448 693054606 773038401 831490031 861810719 960545197\n64208401: 17 64121410 159614568 180745113 214251560 283223383 349631376 362104151 493180926 538865550 562805929 576329835 646478528 666631448 773038401 831490031 861810719 960545197\n159614568: 17 64121410 64208401 180745113 214251560 283223383 349631376 362104151 493180926 538865550 562805929 576329835 646478528 666631448 693..."
},
{
"input": "10 50\n946010975 207263044\n923545573 749203275\n862015642 426425906\n749203275 839134958\n910721783 289091881\n827003531 333726912\n49704846 538788252\n382891592 207263044\n333726912 438209022\n974360048 49704846",
"output": "49704846: 0\n207263044: 0\n289091881: 0\n333726912: 0\n382891592: 1 946010975\n426425906: 0\n438209022: 1 827003531\n538788252: 1 974360048\n749203275: 0\n827003531: 1 438209022\n839134958: 1 923545573\n862015642: 0\n910721783: 0\n923545573: 1 839134958\n946010975: 1 382891592\n974360048: 1 538788252"
},
{
"input": "10 100\n570936669 651631651\n508122950 793810569\n374422919 757639639\n395046911 359158844\n544971368 55608511\n554227847 109843524\n199021332 421407912\n82125712 395046911\n923097829 637659245\n754413496 971876441",
"output": "55608511: 0\n82125712: 1 359158844\n109843524: 0\n199021332: 0\n359158844: 1 82125712\n374422919: 0\n395046911: 0\n421407912: 0\n508122950: 0\n544971368: 0\n554227847: 0\n570936669: 0\n637659245: 0\n651631651: 0\n754413496: 0\n757639639: 0\n793810569: 0\n923097829: 0\n971876441: 0"
},
{
"input": "1 0\n42 23",
"output": "23: 0\n42: 0"
},
{
"input": "1 1\n42 23",
"output": "23: 0\n42: 0"
},
{
"input": "1 50\n42 23",
"output": "23: 0\n42: 0"
},
{
"input": "1 99\n42 23",
"output": "23: 0\n42: 0"
},
{
"input": "1 100\n42 23",
"output": "23: 0\n42: 0"
},
{
"input": "2 49\n42 23\n23 14",
"output": "14: 1 42\n23: 0\n42: 1 14"
},
{
"input": "2 50\n42 23\n23 19",
"output": "19: 1 42\n23: 0\n42: 1 19"
},
{
"input": "2 51\n42 23\n23 19",
"output": "19: 1 42\n23: 0\n42: 1 19"
},
{
"input": "3 49\n42 23\n23 19\n32 23",
"output": "19: 2 32 42\n23: 0\n32: 2 19 42\n42: 2 19 32"
},
{
"input": "3 50\n42 23\n23 19\n32 23",
"output": "19: 2 32 42\n23: 0\n32: 2 19 42\n42: 2 19 32"
},
{
"input": "3 51\n42 23\n23 19\n32 23",
"output": "19: 2 32 42\n23: 0\n32: 2 19 42\n42: 2 19 32"
},
{
"input": "10 50\n642733947 618790811\n508838679 118350938\n175990043 144671010\n246628250 434416712\n77433126 913934904\n414906480 399777199\n252618318 930317425\n316103842 356219969\n530311152 441130575\n15047025 839165125",
"output": "15047025: 0\n77433126: 0\n118350938: 0\n144671010: 0\n175990043: 0\n246628250: 0\n252618318: 0\n316103842: 0\n356219969: 0\n399777199: 0\n414906480: 0\n434416712: 0\n441130575: 0\n508838679: 0\n530311152: 0\n618790811: 0\n642733947: 0\n839165125: 0\n913934904: 0\n930317425: 0"
},
{
"input": "10 0\n106531296 450097353\n947110486 953723914\n774225709 111023810\n774225709 642354614\n559826213 258125349\n768234906 870027419\n4234645 388060649\n870027419 545107061\n484961505 497401821\n76024092 367527096",
"output": "4234645: 16 76024092 106531296 111023810 258125349 367527096 450097353 484961505 497401821 545107061 559826213 642354614 768234906 774225709 870027419 947110486 953723914\n76024092: 16 4234645 106531296 111023810 258125349 388060649 450097353 484961505 497401821 545107061 559826213 642354614 768234906 774225709 870027419 947110486 953723914\n106531296: 16 4234645 76024092 111023810 258125349 367527096 388060649 484961505 497401821 545107061 559826213 642354614 768234906 774225709 870027419 947110486 953723..."
},
{
"input": "10 50\n384319275 425419607\n201879842 153061155\n308316219 268723666\n20837191 401468340\n969142307 78803322\n55247385 365896022\n479817129 222255243\n980789245 697120853\n550086907 652472194\n203728124 229637404",
"output": "20837191: 0\n55247385: 0\n78803322: 0\n153061155: 0\n201879842: 0\n203728124: 0\n222255243: 0\n229637404: 0\n268723666: 0\n308316219: 0\n365896022: 0\n384319275: 0\n401468340: 0\n425419607: 0\n479817129: 0\n550086907: 0\n652472194: 0\n697120853: 0\n969142307: 0\n980789245: 0"
},
{
"input": "10 100\n620468113 665248777\n541840309 963681159\n144393749 136223789\n800116851 648535048\n730845154 277782209\n142473309 2838660\n14940106 355463174\n745034887 545886019\n570717131 701899093\n250611530 857683655",
"output": "2838660: 0\n14940106: 0\n136223789: 0\n142473309: 0\n144393749: 0\n250611530: 0\n277782209: 0\n355463174: 0\n541840309: 0\n545886019: 0\n570717131: 0\n620468113: 0\n648535048: 0\n665248777: 0\n701899093: 0\n730845154: 0\n745034887: 0\n800116851: 0\n857683655: 0\n963681159: 0"
},
{
"input": "5 66\n4242 1\n4242 2\n4242 3\n2323 1\n2323 2",
"output": "1: 1 2\n2: 1 1\n3: 2 1 2\n2323: 1 4242\n4242: 1 2323"
},
{
"input": "5 67\n4242 1\n4242 2\n4242 3\n2323 1\n2323 2",
"output": "1: 1 2\n2: 1 1\n3: 2 1 2\n2323: 1 4242\n4242: 0"
},
{
"input": "6 49\n4242 1\n4242 2\n4242 3\n4242 4\n2323 1\n2323 2",
"output": "1: 3 2 3 4\n2: 3 1 3 4\n3: 3 1 2 4\n4: 3 1 2 3\n2323: 1 4242\n4242: 1 2323"
},
{
"input": "6 50\n4242 1\n4242 2\n4242 3\n4242 4\n2323 1\n2323 2",
"output": "1: 3 2 3 4\n2: 3 1 3 4\n3: 3 1 2 4\n4: 3 1 2 3\n2323: 1 4242\n4242: 1 2323"
},
{
"input": "6 51\n4242 1\n4242 2\n4242 3\n4242 4\n2323 1\n2323 2",
"output": "1: 1 2\n2: 1 1\n3: 3 1 2 4\n4: 3 1 2 3\n2323: 1 4242\n4242: 0"
},
{
"input": "11 12\n4242 1\n4242 2\n4242 3\n4242 4\n4242 5\n4242 6\n4242 7\n4242 8\n2323 1\n2323 2\n2323 3",
"output": "1: 7 2 3 4 5 6 7 8\n2: 7 1 3 4 5 6 7 8\n3: 7 1 2 4 5 6 7 8\n4: 7 1 2 3 5 6 7 8\n5: 7 1 2 3 4 6 7 8\n6: 7 1 2 3 4 5 7 8\n7: 7 1 2 3 4 5 6 8\n8: 7 1 2 3 4 5 6 7\n2323: 1 4242\n4242: 1 2323"
},
{
"input": "11 13\n4242 1\n4242 2\n4242 3\n4242 4\n4242 5\n4242 6\n4242 7\n4242 8\n2323 1\n2323 2\n2323 3",
"output": "1: 7 2 3 4 5 6 7 8\n2: 7 1 3 4 5 6 7 8\n3: 7 1 2 4 5 6 7 8\n4: 7 1 2 3 5 6 7 8\n5: 7 1 2 3 4 6 7 8\n6: 7 1 2 3 4 5 7 8\n7: 7 1 2 3 4 5 6 8\n8: 7 1 2 3 4 5 6 7\n2323: 1 4242\n4242: 1 2323"
},
{
"input": "79 58\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10\n1 11\n1 12\n1 13\n1 14\n1 15\n1 16\n1 17\n1 18\n1 19\n1 20\n1 21\n1 22\n1 23\n1 24\n1 25\n1 26\n1 27\n1 28\n1 29\n1 30\n1 31\n1 32\n1 33\n1 34\n1 35\n1 36\n1 37\n1 38\n1 39\n1 40\n1 41\n1 42\n1 43\n1 44\n1 45\n1 46\n1 47\n1 48\n1 49\n1 50\n1 51\n1 52\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n2 10\n2 11\n2 12\n2 13\n2 14\n2 15\n2 16\n2 17\n2 18\n2 19\n2 20\n2 21\n2 22\n2 23\n2 24\n2 25\n2 26\n2 27\n2 28\n2 29\n2 30\n2 31",
"output": "1: 1 2\n2: 1 1\n3: 28 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\n4: 28 3 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\n5: 28 3 4 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\n6: 28 3 4 5 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\n7: 28 3 4 5 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31\n8: 28 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2..."
}
] | 93 | 0 | 0 | 4,434 |
|
106 | Card Game | [
"implementation"
] | A. Card Game | 2 | 256 | There is a card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly β you can find them later yourselves if you want.
To play durak you need a pack of 36 cards. Each card has a suit ("S", "H", "D" and "C") and a rank (in the increasing order "6", "7", "8", "9", "T", "J", "Q", "K" and "A"). At the beginning of the game one suit is arbitrarily chosen as trump.
The players move like that: one player puts one or several of his cards on the table and the other one should beat each of them with his cards.
A card beats another one if both cards have similar suits and the first card has a higher rank then the second one. Besides, a trump card can beat any non-trump card whatever the cardsβ ranks are. In all other cases you can not beat the second card with the first one.
You are given the trump suit and two different cards. Determine whether the first one beats the second one or not. | The first line contains the tramp suit. It is "S", "H", "D" or "C".
The second line contains the description of the two different cards. Each card is described by one word consisting of two symbols. The first symbol stands for the rank ("6", "7", "8", "9", "T", "J", "Q", "K" and "A"), and the second one stands for the suit ("S", "H", "D" and "C"). | Print "YES" (without the quotes) if the first cards beats the second one. Otherwise, print "NO" (also without the quotes). | [
"H\nQH 9S\n",
"S\n8D 6D\n",
"C\n7H AS\n"
] | [
"YES\n",
"YES",
"NO"
] | none | [
{
"input": "H\nQH 9S",
"output": "YES"
},
{
"input": "S\n8D 6D",
"output": "YES"
},
{
"input": "C\n7H AS",
"output": "NO"
},
{
"input": "C\nKC 9C",
"output": "YES"
},
{
"input": "D\n7D KD",
"output": "NO"
},
{
"input": "H\n7H KD",
"output": "YES"
},
{
"input": "D\nAS AH",
"output": "NO"
},
{
"input": "H\nKH KS",
"output": "YES"
},
{
"input": "C\n9H 6C",
"output": "NO"
},
{
"input": "C\n9H JC",
"output": "NO"
},
{
"input": "D\nTD JD",
"output": "NO"
},
{
"input": "H\n6S 7S",
"output": "NO"
},
{
"input": "D\n7S 8S",
"output": "NO"
},
{
"input": "S\n8H 9H",
"output": "NO"
},
{
"input": "C\n9D TD",
"output": "NO"
},
{
"input": "H\nTC JC",
"output": "NO"
},
{
"input": "C\nJH QH",
"output": "NO"
},
{
"input": "H\nQD KD",
"output": "NO"
},
{
"input": "D\nKS AS",
"output": "NO"
},
{
"input": "S\nAH 6H",
"output": "YES"
},
{
"input": "H\n7D 6D",
"output": "YES"
},
{
"input": "S\n8H 7H",
"output": "YES"
},
{
"input": "D\n9S 8S",
"output": "YES"
},
{
"input": "S\nTC 9C",
"output": "YES"
},
{
"input": "H\nJS TS",
"output": "YES"
},
{
"input": "S\nQD JD",
"output": "YES"
},
{
"input": "D\nKH QH",
"output": "YES"
},
{
"input": "H\nAD KD",
"output": "YES"
},
{
"input": "H\nQS QD",
"output": "NO"
},
{
"input": "C\nTS TH",
"output": "NO"
},
{
"input": "C\n6C 6D",
"output": "YES"
},
{
"input": "H\n8H 8D",
"output": "YES"
},
{
"input": "S\n7D 7S",
"output": "NO"
},
{
"input": "H\nJC JH",
"output": "NO"
},
{
"input": "H\n8H 9C",
"output": "YES"
},
{
"input": "D\n9D 6S",
"output": "YES"
},
{
"input": "C\nJC AH",
"output": "YES"
},
{
"input": "S\nAS KD",
"output": "YES"
},
{
"input": "S\n7S JS",
"output": "NO"
},
{
"input": "H\nTH 8H",
"output": "YES"
},
{
"input": "S\n7S QS",
"output": "NO"
},
{
"input": "C\nKC QC",
"output": "YES"
},
{
"input": "S\nAD 9S",
"output": "NO"
},
{
"input": "D\n7H 8D",
"output": "NO"
},
{
"input": "H\nJC 9H",
"output": "NO"
},
{
"input": "C\n7S AC",
"output": "NO"
},
{
"input": "C\n8C 7C",
"output": "YES"
},
{
"input": "H\n9D 8S",
"output": "NO"
},
{
"input": "D\nAC KS",
"output": "NO"
},
{
"input": "H\n8C QH",
"output": "NO"
},
{
"input": "S\n7S TS",
"output": "NO"
},
{
"input": "C\nAH 6S",
"output": "NO"
},
{
"input": "S\nKS QS",
"output": "YES"
},
{
"input": "H\nAC QC",
"output": "YES"
},
{
"input": "S\n9H 8D",
"output": "NO"
},
{
"input": "S\nTS JS",
"output": "NO"
},
{
"input": "S\n8H 7C",
"output": "NO"
},
{
"input": "C\nAH 6S",
"output": "NO"
},
{
"input": "S\n7S QS",
"output": "NO"
},
{
"input": "C\nAH 6S",
"output": "NO"
},
{
"input": "S\nTS KS",
"output": "NO"
},
{
"input": "C\nTH KH",
"output": "NO"
},
{
"input": "H\n9C 6D",
"output": "NO"
},
{
"input": "H\n9C 8D",
"output": "NO"
},
{
"input": "H\nTH AH",
"output": "NO"
},
{
"input": "H\nTH JH",
"output": "NO"
},
{
"input": "H\nQS 9C",
"output": "NO"
},
{
"input": "H\nKC AC",
"output": "NO"
},
{
"input": "H\nAH KH",
"output": "YES"
},
{
"input": "H\nKS QS",
"output": "YES"
},
{
"input": "C\nAD KS",
"output": "NO"
},
{
"input": "H\nQS 9C",
"output": "NO"
},
{
"input": "H\n9D 7S",
"output": "NO"
},
{
"input": "D\n6D 9S",
"output": "YES"
},
{
"input": "H\nAH KH",
"output": "YES"
},
{
"input": "H\nKC AC",
"output": "NO"
},
{
"input": "D\n8S 6C",
"output": "NO"
},
{
"input": "S\nAC KC",
"output": "YES"
}
] | 92 | 307,200 | 0 | 4,441 |
401 | Team | [
"constructive algorithms",
"greedy",
"implementation"
] | null | null | Now it's time of Olympiads. Vanya and Egor decided to make his own team to take part in a programming Olympiad. They've been best friends ever since primary school and hopefully, that can somehow help them in teamwork.
For each team Olympiad, Vanya takes his play cards with numbers. He takes only the cards containing numbers 1 and 0. The boys are very superstitious. They think that they can do well at the Olympiad if they begin with laying all the cards in a row so that:
- there wouldn't be a pair of any side-adjacent cards with zeroes in a row; - there wouldn't be a group of three consecutive cards containing numbers one.
Today Vanya brought *n* cards with zeroes and *m* cards with numbers one. The number of cards was so much that the friends do not know how to put all those cards in the described way. Help them find the required arrangement of the cards or else tell the guys that it is impossible to arrange cards in such a way. | The first line contains two integers: *n* (1<=β€<=*n*<=β€<=106) β the number of cards containing number 0; *m* (1<=β€<=*m*<=β€<=106) β the number of cards containing number 1. | In a single line print the required sequence of zeroes and ones without any spaces. If such sequence is impossible to obtain, print -1. | [
"1 2\n",
"4 8\n",
"4 10\n",
"1 5\n"
] | [
"101\n",
"110110110101\n",
"11011011011011\n",
"-1\n"
] | none | [
{
"input": "1 2",
"output": "101"
},
{
"input": "4 8",
"output": "110110110101"
},
{
"input": "4 10",
"output": "11011011011011"
},
{
"input": "1 5",
"output": "-1"
},
{
"input": "3 4",
"output": "1010101"
},
{
"input": "3 10",
"output": "-1"
},
{
"input": "74 99",
"output": "11011011011011011011011011011011011011011011011011011011011011011011011010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101"
},
{
"input": "19 30",
"output": "1101101101101101101101101101101010101010101010101"
},
{
"input": "33 77",
"output": "-1"
},
{
"input": "3830 6966",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "1000000 1000000",
"output": "1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101..."
},
{
"input": "1027 2030",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "4610 4609",
"output": "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010..."
},
{
"input": "3342 3339",
"output": "-1"
},
{
"input": "7757 7755",
"output": "-1"
},
{
"input": "10 8",
"output": "-1"
},
{
"input": "4247 8495",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "7101 14204",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "9801 19605",
"output": "-1"
},
{
"input": "4025 6858",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "7129 13245",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "8826 12432",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "6322 9256",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "8097 14682",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "6196 6197",
"output": "1010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101..."
},
{
"input": "1709 2902",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "455 512",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101..."
},
{
"input": "1781 1272",
"output": "-1"
},
{
"input": "3383 5670",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "954 1788",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "9481 15554",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "9079 100096",
"output": "-1"
},
{
"input": "481533 676709",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "423472 564888",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "227774 373297",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "42346 51898",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "739107 1000000",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "455043 798612",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "801460 801459",
"output": "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010..."
},
{
"input": "303498 503791",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "518822 597833",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "32342 64687",
"output": "-1"
},
{
"input": "873192 873189",
"output": "-1"
},
{
"input": "384870 450227",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "201106 208474",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "775338 980888",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "263338 393171",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "241043 330384",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "307203 614408",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "379310 417986",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "661101 785111",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "284634 319008",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "500000 1000000",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "499999 1000000",
"output": "1101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101101..."
},
{
"input": "3 1",
"output": "-1"
},
{
"input": "14124 242112",
"output": "-1"
},
{
"input": "2 1",
"output": "010"
},
{
"input": "2 3",
"output": "10101"
},
{
"input": "1 4",
"output": "11011"
},
{
"input": "2 6",
"output": "11011011"
},
{
"input": "2 5",
"output": "1101101"
},
{
"input": "2 4",
"output": "110101"
},
{
"input": "2 3",
"output": "10101"
},
{
"input": "2 2",
"output": "1010"
},
{
"input": "2 1",
"output": "010"
},
{
"input": "1 1",
"output": "10"
},
{
"input": "1 3",
"output": "1101"
},
{
"input": "3 2",
"output": "01010"
},
{
"input": "5 4",
"output": "010101010"
},
{
"input": "4 3",
"output": "0101010"
},
{
"input": "11 10",
"output": "010101010101010101010"
},
{
"input": "8 7",
"output": "010101010101010"
},
{
"input": "4 2",
"output": "-1"
},
{
"input": "6 5",
"output": "01010101010"
},
{
"input": "3 7",
"output": "1101101101"
},
{
"input": "5 1",
"output": "-1"
},
{
"input": "10 1",
"output": "-1"
},
{
"input": "4 9",
"output": "1101101101101"
},
{
"input": "6 4",
"output": "-1"
},
{
"input": "12 10",
"output": "-1"
},
{
"input": "4 100",
"output": "-1"
}
] | 1,000 | 8,294,400 | 0 | 4,444 |
|
346 | Lucky Common Subsequence | [
"dp",
"strings"
] | null | null | In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF.
You are given two strings *s*1, *s*2 and another string called *virus*. Your task is to find the longest common subsequence of *s*1 and *s*2, such that it doesn't contain *virus* as a substring. | The input contains three strings in three separate lines: *s*1, *s*2 and *virus* (1<=β€<=|*s*1|,<=|*s*2|,<=|*virus*|<=β€<=100). Each string consists only of uppercase English letters. | Output the longest common subsequence of *s*1 and *s*2 without *virus* as a substring. If there are multiple answers, any of them will be accepted.
If there is no valid common subsequence, output 0. | [
"AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ\n",
"AA\nA\nA\n"
] | [
"ORZ\n",
"0\n"
] | none | [
{
"input": "AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ",
"output": "ORZ"
},
{
"input": "AA\nA\nA",
"output": "0"
},
{
"input": "PWBJTZPQHA\nZJMKLWSROQ\nUQ",
"output": "WQ"
},
{
"input": "QNHRPFYMAAPJDUHBAEXNEEZSTMYHVGQPYKNMVKMBVSVLIYGUVMJHEFLJEPIWFHSLISTGOKRXNMSCXYKMAXBPKCOCNTIRPCUEPHXM\nRRFCZUGFDRKKMQTOETNELXMEWGOCDHFKIXOPVHHEWTCDNXVFKFKTKNWKEIKTCMHMHDNCLLVQSGKHBCDDYVVVQIRPZEOPUGQUGRHH\nR",
"output": "QNHFPHEXNETMHMHLLSGKCYPOPUH"
},
{
"input": "CGPWTAPEVBTGANLCLVSHQIIKHDPVUHRSQPXHSNYAHPGBECICFQYDFRTRELLLEDZYWJSLOBSKDGRRDHNRRGIXAMEBGFJJTEIGUGRU\nHAWYVKRRBEIWNOGYMIYQXDCFXMMCSAYSOXQFHHIFRRCJRAWHLDDHHHAKHXVKCVPBFGGEXUKWTFWMOUUGMXTSBUTHXCJCWHCQQTYQ\nANKFDWLYSX",
"output": "WVBGCSSQHHIFRRWLDDHXBGFUGU"
},
{
"input": "AUNBEKNURNUPHXQYKUTAHCOLMPRQZZTVDUYCTNIRACQQTQAIDTAWJXBUTIZUASDIJZWLHAQVGCAHKTZMXSDVVWAIGQEALRFKFYTT\nQBVRFKPKLYZLYNRFTRJZZQEYAEKPFXVICUVFVQSDENBJYYNCFTOZHULSWJQTNELYLKCZTGHOARDCFXBXQGGSQIVUCJVNGFZEEZQE\nN",
"output": "BKPYTRZZVICQDJTZUSJZHAQGSVVGQE"
},
{
"input": "BGIIURZTEUJJULBWKHDQBRFGEUOMQSREOTILICRSBUHBGTSRDHKVDDEBVHGMHXUVFJURSMFDJOOOWCYPJDVRVKLDHICPNKTBFXDJ\nXOADNTKNILGNHHBNFYNDWUNXBGDFUKUVHLPDOGOYRMOTAQELLRMHFAQEOXFWGAQUROVUSWOAWFRVIRJQVXPCXLSCQLCUWKBZUFQP\nYVF",
"output": "ILBWKHDGOMQELRHEGUVUSOWVRVLCKBF"
},
{
"input": "AXBPBDEYIYKKCZBTLKBUNEQLCXXLKIUTOOATYDXYYQCLFAXAEIGTFMNTTQKCQRMEEFRYVYXAOLMUQNPJBMFBUGVXFZAJSBXWALSI\nVWFONLLKSHGHHQSFBBFWTXAITPUKNDANOCLMNFTAAMJVDLXYPILPCJCFWTNBQWEOMMXHRYHEGBJIVSXBBGQKXRIYNZFIWSZPPUUM\nPPKKLHXWWT",
"output": "BBITKNCLTADXYCFTNQMRYVXBBGXFWS"
},
{
"input": "XKTAOCPCVMIOGCQKPENDKIZRZBZVRTBTGCDRQUIMVHABDIHSCGWPUTQKLPBOXAYICPWJBFLFSEPERGJZHRINEHQMYTOTKLCQCSMZ\nAITFIOUTUVZLSSIYWXSYTQMFLICCXOFSACHTKGPXRHRCGXFZXPYWKWPUOIDNEEZOKMOUYGVUJRQTIRQFCSBCWXVFCIAOLZDGENNI\nDBHOIORVCPNXCDOJKSYYIENQRJGZFHOWBYQIITMTVWXRMAMYILTHBBAJRJELWMIZOZBGPDGSTIRTQIILJRYICMUQTUAFKDYGECPY",
"output": "TOVMIOCKPRRCGWPUOIEEGJRQTQCSZ"
},
{
"input": "UNGXODEEINVYVPHYVGSWPIPFMFLZJYRJIPCUSWVUDLLSLRPJJFWCUOYDUGXBRKWPARGLXFJCNNFUIGEZUCTPFYUIMQMJLQHTIVPO\nBWDEGORKXYCXIDWZKGFCUYIDYLTWLCDBUVHPAPFLIZPEUINQSTNRAOVYKZCKFWIJQVLSVCGLTCOEMAYRCDVVQWQENTWZALWUKKKA\nXDGPZXADAFCHKILONSXFGRHIQVMIYWUTJUPCCEKYQVYAENRHVWERJSNPVEMRYSZGYBNTQLIFKFISKZJQIQQGSKVGCNMPNIJDRTXI",
"output": "GODIYVHPPFLZPUSWVLSLCOYDWALU"
},
{
"input": "KOROXDDWEUVYWJIXSFPEJFYZJDDUXISOFJTIFJSBTWIJQHMTQWLAGGMXTFALRXYCMGZNKYQRCDVTPRQDBAALTWAXTNLDPYWNSFKE\nNHZGRZFMFQGSAYOJTFKMMUPOOQXWCPPAIVRJHINJPHXTTBWRIYNOHMJKBBGXVXYZDBVBBTQRXTOFLBBCXGNICBKAAGOKAYCCJYCW\nXCXLBESCRBNKWYGFDZFKWYNLFAKEWWGRUIAQGNCFDQXCHDBEQDSWSNGVKUFOGGSPFFZWTXGZQMMFJXDWOPUEZCMZEFDHXTRJTNLW",
"output": "KOOXWVJIPXTBWIHMTQXTFLCGNCBAAAYW"
},
{
"input": "ESQZPIRAWBTUZSOWLYKIYCHZJPYERRXPJANKPZVPEDCXCJIDTLCARMAOTZMHJVDJXRDNQRIIOFIUTALVSCKDUSAKANKKOFKWINLQ\nGKSTYEAXFJQQUTKPZDAKHZKXCJDONKBZOTYGLYQJOGKOYMYNNNQRRVAGARKBQYJRVYYPFXTIBJJYQUWJUGAUQZUVMUHXLIQWGRMP\nUFPHNRDXLNYRIIYVOFRKRUQCWAICQUUBPHHEGBCILXHHGLOBKADQVPSQCMXJRLIZQPSRLZJNZVQPIURDQUKNHVVYNVBYGXXXXJDI",
"output": "STYEXJKPZDXCJDTLOMVRQRFIUAVUIQ"
},
{
"input": "UAYQUMTSNGMYBORUYXJJQZVAGBRVDWUTGUYYYOTWAHVKGGOHADXULFUFQULSAGDWFJCSDKPWBROYZIFRGGRVZQMEHKHCKNHTQSMK\nSVKVTPUZOBRKGLEAAXMIUSRISOTDIFFUCODYGNYIPSWEEBHGNWRZETXSVVMQTRBVFZMYHOHUCMLBUXBMPMSNCSHFZTAFUVTMQFGL\nTNICVANBEBOQASUEJJAOJXWNMDGAAVYNHRPSMKGMXZDJHCZHFHRRMIDWUOQCZSBKDPLSGHNHFKFYDRGVKXOLPOOWBPOWSDFLEJVX",
"output": "SVVTUOKGAXUFFUCDPWBRZRVZMHHCNHTQ"
},
{
"input": "KEJHTOKHMKWTYSJEAJAXGADRHUKBCRHACSRDNSZIHTPQNLOSRKYBGYIIJDINTXRPMWSVMMBODAYPVVDDTIXGDIOMWUAKZVFKDAUM\nWTEVPIFAAJYIDTZSZKPPQKIOMHDZTKDMFVKSJRUFMNHZJPVSQYELWYAFACGGNRORSLGYVXAEYVLZBLDEHYDGOFDSWUYCXLXDKFSU\nTUZEQBWVBVTKETQ",
"output": "EJTOKMKSJRUHZPQLYGNRSVAYVDDGDWUKFU"
},
{
"input": "EGQYYSKTFTURZNRDVIWBYXMRDGFWMYKFXGIFOGYJSXKDCJUAGZPVTYCHIXVFTVTCXMKHZFTXSMMQVFXZGKHCIYODDRZEYECDLKNG\nPEXXCTRFJAAKPOTBAEFRLDRZKORNMXHHXTLKMKCGPVPUOBELPLFQFXOBZWIVIQCHEJQPXKGSCQAWIMETCJVTAGXJIINTADDXJTKQ\nQURSEKPMSSEVQZI",
"output": "EKTFRZNXMGFFXIJXKCATCVTXTDDK"
},
{
"input": "ZFFBNYVXOZCJPSRAEACVPAUKVTCVZYQPHVENTKOCMHNIYYMIKKLNKHLWHHWAQMWFTSYEOQQFEYAAYGMPNZCRYBVNAQTDSLXZGBCG\nPIQHLNEWAMFAKGHBGZAWRWAXCSKUDZBDOCTXAHSVFZACXGFMDSYBYYDDNQNBEZCYCULSMMPBTQOJQNRPZTRCSDLIYPLVUGJPKDTG\nZBFJPLNAKWQBTUVJKMHVBATAM",
"output": "FBZRACUZOCHAMSYYYNZCYBNTDLGG"
},
{
"input": "BTWZLIKDACZVLCKMVTIQHLFBNRCBDSWPFFKGPCQFPTOIJLPFCDMFGQKFHTDFFCCULUAYPXXIIIWBZIDMOPNHPZBEXLVARJFTBFOE\nMDXYKKWZVASJPPWRCYNMRAOBBLUNBSMQAPCPSFAGLXWJRBQTBRWXYNQGWECYNFIAJXDMUHIIMDFMSHLPIMYQXNRRUSSNXALGNWIK\nKNFVBVAOWXMZVUHAVUDKDBUVAKNHACZBGBHMUOPHWGQSDFXLHB",
"output": "WZACLMQLBRWGCFIJDMHDFLPIMNXL"
},
{
"input": "GOZVMIRQIGYGVAGOREQTXFXPEZYOJOXPNDGAESICXHMKQDXQPRLMRVWHXFEJVCWZDLYMQLDURUXZPTLEHPTSKXGSNEQDKLVFFLDX\nIMEVFCZXACKRRJVXDRKFWTLTRTLQQDHEBZLCOCNVPABQMIWJHRLKFUKWOVVWGGNWCJNRYOYOAJFQWCLHQIQRBZTVWKBFOXKEHHQP\nSZ",
"output": "MVARXFEZOPAIHRLVWFCLQRZTKXEQ"
},
{
"input": "BBYUVCIYLNUJPSEYCAAPQSDNSDDTNEHQZDPBEKQAWNAKEYFBNEEBGPDPRLCSVOWYDEDRPPEDOROCHRCNQUSPNVXGRXHNLKDETWQC\nBQCQXCAHADGJHBYIKEUWNXFUOOTVCCKJPJJCMWLAWWKSDGHFNZTCPSQNRTPCBLXDTSJLRHSCCZXQXCVLVGTROOUCUQASIQHZGNEI\nRYE",
"output": "BBYUVCJPCASDNTPQNBDRLVROOCQSGNE"
},
{
"input": "WZRKLETJRBBRZKGHEFBVEFVLIERBPSEGJVSNUZUICONWWBOOTHCOJLLZFNOCNOFJQZTZWBLKHGIWWWPBUYWBAHYJGEBJZJDTNBGN\nZINFGDCNKHYFZYYWHTIHZTKWXXXMSWOVOPQDTRWSQKBWWCPEMYFVGARELELBLGEVJCMOCFTTUVCYUQUSFONAMWKVDWMGXVNZJBWH\nAFPA",
"output": "WZKTRBEFVELEBEJCOTCFONWKWGZJB"
},
{
"input": "ABABABB\nABABABB\nABABB",
"output": "ABABAB"
},
{
"input": "ABBB\nABBB\nABB",
"output": "BBB"
},
{
"input": "A\nBABAABAAABABABABABABAABABABABBABABABABAABBABBABAABABAABAABBAAAAAABBABABABABAABABAABABABABAABAABABABA\nB",
"output": "A"
},
{
"input": "ABBAABAAABABAABAABABABABAABBBABABABAAABBABAAABABABABBABBABABAABABABABABABBABAABABAABABABAAABBABABABA\nA\nB",
"output": "A"
},
{
"input": "ABBBABABABABABBABAABAAABABAABABABABBABAAAABABABBABAABABAAABAABBAAABAABABBABBABABBABAABABABAAAAABABAB\nB\nBABBABAABABABABABABABABABBAABABBABABBAAABAAABABBAABAABBABABBABABAABBABAABABBAABAABAABABABABABBABABAB",
"output": "B"
},
{
"input": "AABABAABAAABABAAABAAAABBAAABABAAABABAABAABAAAABAABAAAABAAAABAAAABBAABAAAAABAAAAABABAAAAAABABAABAAAAA\nABAABABABAAABABAABABBAABAABAABABAABABAAABBAABAAAABABABAAAAABAAAAABABABABAABAABAABAABABAABABAABAABAAB\nBABAAABABBAABABAABAA",
"output": "ABAABABABAAABAAAABBAABAAAABABAABABAAABAABAAAABAAAAAAABAAAAAAABAAAAABAAAAAAABABAABAAAA"
},
{
"input": "AABABABABAAAABBAAAABABABABAAAAABABAAAA\nAABABAAABABABAAABAAAAABAAABAAABABABBBABBAAABAABAAAAABABBABAAABAABAABABAAAABABAAABAAABAABABBBABBABABA\nAAAAA",
"output": "AABABABABAAAABBAAAABABABABAAAABABAAAA"
},
{
"input": "ZZXXAAZZAXAAZZAZZXXAAZZAXAXZZXXAAZZZZXXAZZXXAAAZZXXAAAZZXXZZXXXAAAZZXZZXXAZZXXZXXAAXAAZZZXXAXAXAZZXZ\nAZZXXAAZZXXAAXZXXAZZXAZZXZZXXAAZZXXAAZAAZZAAZZXXAA\nAAZZXAAXXAAAZZXXAZZXXAAZZXXAAAZZXXZ",
"output": "ZZXXAAZZXXAAXZXXAZZXAZZXZZXXAAZZXXAAZAZZAAZZXXAA"
},
{
"input": "SDASSDADASDASDASDSDADASASDAAASDASDDASDDASDADASDASDSSDASDD\nSDASDASDDASDASDASDSDSDASDASDASDASDASDASDASDADASDASDASDSDASDASDDDASSD\nSDASDSDDAA",
"output": "SDASSDADASDASDSDSDADASASDAAASDASDDASDDASDDASDASDDASD"
},
{
"input": "DASSDASDASDDAASDASDADASDASASDAS\nSDADASDASSDAASDASDASDADASSDDA\nSD",
"output": "DADADADAADADADADASSA"
},
{
"input": "ASDASSDASDS\nDASDASDDDASDADASDASDASDASSDADASDDAASDA\nDSD",
"output": "ASDASSDASDS"
},
{
"input": "ASDASASDASDASDAASDASDASDASASDDAASDASSASDSDAD\nDASDASSSDASDASDASASDASSDAASDASSDDSASDASDAASDDAASDASDAASDASDDASDASDASDASDASS\nDASD",
"output": "ASDASASDASASDAASDASASDASASDDAASDASSASDSDAD"
},
{
"input": "DASDSDASDADASDDDSDASSDDAASDA\nDASDDASDSDADSDASDADSDSDADDASDASDDASDASDASDSDASD\nDAASD",
"output": "DASDSDASDADASDDDSDASSDDASDA"
},
{
"input": "ABAAAABABADABAABAABCCABADABACABACABCABADABADABACABBACAADABACABABACABADABACABABA\nBACAACABABABACABCABADABAACABADABACABAA\nABBAB",
"output": "BAAACABABABACABCABADABAACABADABACABAA"
},
{
"input": "ABAABACABADAACADABACAAB\nBAACABADABACABAAAADADAABACABACABADABABADABACABAADABBADABACAAACAABACABADABBBAA\nDABACA",
"output": "ABAABACABADAACADABAAAB"
},
{
"input": "BACABACABAACABADABABACAABACABBACAACAACABCABADAACABAABAABBADABACABADABCABAD\nBACAABADABABADABACABABACABADABACABCBADABACADABCABABADAABA\nBADABAA",
"output": "BACAABAAABADABACAABACABAAACABCBADAACADABCABADAABA"
},
{
"input": "ACABADABACABCABAAB\nBADAB\nACAACABA",
"output": "BADAB"
},
{
"input": "ABABAC\nABABAC\nABAC",
"output": "ABABA"
},
{
"input": "BCBCBC\nBCBCBC\nBC",
"output": "CCB"
},
{
"input": "AAACAAACAAADAAAAAAA\nAADAAAAAAAACDAAAAAAAAAAACAAAAABCACAAACAAAAABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADA\nAAACAADAAAAADD",
"output": "AAACAAACAAAAAAAAAA"
},
{
"input": "ABABBB\nABABBB\nABB",
"output": "ABAB"
},
{
"input": "ABABABAC\nABABABAC\nABABAC",
"output": "ABABABA"
},
{
"input": "BBAABAAAAABBBBBBBABABAABAABAABBABABABBBABBABBABBBABAABBBBBBAABAAAAAAAAABABAAABBABBAAAAAABAABABBAAABB\nBBAABAAAAABBBBBBBABABAABAABAABBABABABBBABBABBABBBABAABBBBBBAABAAAAAAAAABABAAABBABBAAAAAABAABABBAAABB\nBBBAA",
"output": "BBAABAAAAABBBBBBBABABAABAABAABBABABABBBABBABBABBBABAABBBBBBABAAAAAAAAABABAAABBABBAAAAAABAABABBAAABB"
},
{
"input": "ABABC\nABABC\nABC",
"output": "ABAB"
},
{
"input": "BABBB\nBABBB\nABB",
"output": "BBBB"
},
{
"input": "ABCCCCCCCC\nABCCCCCCCC\nABC",
"output": "BCCCCCCCC"
}
] | 404 | 2,048,000 | -1 | 4,446 |
|
789 | Masha and geometric depression | [
"brute force",
"implementation",
"math"
] | null | null | Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.
You are given geometric progression *b* defined by two integers *b*1 and *q*. Remind that a geometric progression is a sequence of integers *b*1,<=*b*2,<=*b*3,<=..., where for each *i*<=><=1 the respective term satisfies the condition *b**i*<==<=*b**i*<=-<=1Β·*q*, where *q* is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both *b*1 and *q* can equal 0. Also, Dvastan gave Masha *m* "bad" integers *a*1,<=*a*2,<=...,<=*a**m*, and an integer *l*.
Masha writes all progression terms one by one onto the board (including repetitive) while condition |*b**i*|<=β€<=*l* is satisfied (|*x*| means absolute value of *x*). There is an exception: if a term equals one of the "bad" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term.
But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print "inf" in case she needs to write infinitely many integers. | The first line of input contains four integers *b*1, *q*, *l*, *m* (-109<=β€<=*b*1,<=*q*<=β€<=109, 1<=β€<=*l*<=β€<=109, 1<=β€<=*m*<=β€<=105)Β β the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of "bad" integers, respectively.
The second line contains *m* distinct integers *a*1,<=*a*2,<=...,<=*a**m* (-109<=β€<=*a**i*<=β€<=109)Β β numbers that will never be written on the board. | Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or "inf" (without quotes) otherwise. | [
"3 2 30 4\n6 14 25 48\n",
"123 1 2143435 4\n123 11 -5453 141245\n",
"123 1 2143435 4\n54343 -13 6 124\n"
] | [
"3",
"0",
"inf"
] | In the first sample case, Masha will write integers 3,β12,β24. Progression term 6 will be skipped because it is a "bad" integer. Terms bigger than 24 won't be written because they exceed *l* by absolute value.
In the second case, Masha won't write any number because all terms are equal 123 and this is a "bad" integer.
In the third case, Masha will write infinitely integers 123. | [
{
"input": "3 2 30 4\n6 14 25 48",
"output": "3"
},
{
"input": "123 1 2143435 4\n123 11 -5453 141245",
"output": "0"
},
{
"input": "123 1 2143435 4\n54343 -13 6 124",
"output": "inf"
},
{
"input": "3 2 25 2\n379195692 -69874783",
"output": "4"
},
{
"input": "3 2 30 3\n-691070108 -934106649 -220744807",
"output": "4"
},
{
"input": "3 3 104 17\n9 -73896485 -290898562 5254410 409659728 -916522518 -435516126 94354167 262981034 -375897180 -80186684 -173062070 -288705544 -699097793 -11447747 320434295 503414250",
"output": "3"
},
{
"input": "-1000000000 -1000000000 1 1\n232512888",
"output": "0"
},
{
"input": "11 0 228 5\n-1 0 1 5 -11245",
"output": "1"
},
{
"input": "11 0 228 5\n-1 -17 1 5 -11245",
"output": "inf"
},
{
"input": "0 0 2143435 5\n-1 -153 1 5 -11245",
"output": "inf"
},
{
"input": "123 0 2143435 4\n5433 0 123 -645",
"output": "0"
},
{
"input": "123 -1 2143435 5\n-123 0 12 5 -11245",
"output": "inf"
},
{
"input": "123 0 21 4\n543453 -123 6 1424",
"output": "0"
},
{
"input": "3 2 115 16\n24 48 12 96 3 720031148 -367712651 -838596957 558177735 -963046495 -313322487 -465018432 -618984128 -607173835 144854086 178041956",
"output": "1"
},
{
"input": "-3 0 92055 36\n-92974174 -486557474 -663622151 695596393 177960746 -563227474 -364263320 -676254242 -614140218 71456762 -764104225 705056581 -106398436 332755134 -199942822 -732751692 658942664 677739866 886535704 183687802 -784248291 -22550621 -938674499 637055091 -704750213 780395802 778342470 -999059668 -794361783 796469192 215667969 354336794 -60195289 -885080928 -290279020 201221317",
"output": "inf"
},
{
"input": "0 -3 2143435 5\n-1 0 1 5 -11245",
"output": "0"
},
{
"input": "123 -1 2143435 5\n-123 0 123 -5453 141245",
"output": "0"
},
{
"input": "123 0 2143435 4\n5433 0 -123 -645",
"output": "1"
},
{
"input": "11 0 2 5\n-1 0 1 5 -11245",
"output": "0"
},
{
"input": "2 2 4 1\n2",
"output": "1"
},
{
"input": "1 -2 1000000000 1\n0",
"output": "30"
},
{
"input": "0 8 10 1\n5",
"output": "inf"
},
{
"input": "-1000 0 10 1\n5",
"output": "0"
},
{
"input": "0 2 2143435 4\n54343 -13 6 124",
"output": "inf"
},
{
"input": "0 8 5 1\n9",
"output": "inf"
},
{
"input": "-10 1 5 1\n100",
"output": "0"
},
{
"input": "123 -1 2143435 4\n54343 -13 6 123",
"output": "inf"
},
{
"input": "-5 -1 10 1\n-5",
"output": "inf"
},
{
"input": "2 0 1 1\n2",
"output": "0"
},
{
"input": "0 5 8 1\n10",
"output": "inf"
},
{
"input": "0 5 100 2\n34 56",
"output": "inf"
},
{
"input": "15 -1 15 4\n15 -15 1 2",
"output": "0"
},
{
"input": "10 -1 2 1\n1",
"output": "0"
},
{
"input": "2 0 2 1\n2",
"output": "inf"
},
{
"input": "4 0 4 1\n0",
"output": "1"
},
{
"input": "10 10 10 1\n123",
"output": "1"
},
{
"input": "2 2 4 1\n3",
"output": "2"
},
{
"input": "0 1 1 1\n0",
"output": "0"
},
{
"input": "3 2 30 1\n3",
"output": "3"
},
{
"input": "1000000000 100000 1000000000 4\n5433 13 6 0",
"output": "1"
},
{
"input": "-2 0 1 1\n1",
"output": "0"
},
{
"input": "2 -1 10 1\n2",
"output": "inf"
},
{
"input": "1 -1 2 1\n1",
"output": "inf"
},
{
"input": "0 10 10 1\n2",
"output": "inf"
},
{
"input": "0 35 2 1\n3",
"output": "inf"
},
{
"input": "3 1 3 1\n5",
"output": "inf"
},
{
"input": "3 2 3 4\n6 14 25 48",
"output": "1"
},
{
"input": "0 69 12 1\n1",
"output": "inf"
},
{
"input": "100 0 100000 1\n100",
"output": "inf"
},
{
"input": "0 4 1000 3\n5 6 7",
"output": "inf"
},
{
"input": "0 2 100 1\n5",
"output": "inf"
},
{
"input": "3 2 24 4\n6 14 25 48",
"output": "3"
},
{
"input": "0 4 1 1\n2",
"output": "inf"
},
{
"input": "1 5 10000 1\n125",
"output": "5"
},
{
"input": "2 -1 1 1\n1",
"output": "0"
},
{
"input": "0 3 100 1\n5",
"output": "inf"
},
{
"input": "0 3 3 1\n1",
"output": "inf"
},
{
"input": "0 2 5 1\n1",
"output": "inf"
},
{
"input": "5 -1 100 1\n5",
"output": "inf"
},
{
"input": "-20 0 10 1\n0",
"output": "0"
},
{
"input": "3 0 1 1\n3",
"output": "0"
},
{
"input": "2 -1 3 1\n2",
"output": "inf"
},
{
"input": "1 1 1000000000 1\n100",
"output": "inf"
},
{
"input": "5 -1 3 1\n0",
"output": "0"
},
{
"input": "0 5 10 1\n2",
"output": "inf"
},
{
"input": "123 0 125 1\n123",
"output": "inf"
},
{
"input": "2 -1 100 1\n2",
"output": "inf"
},
{
"input": "5 2 100 1\n5",
"output": "4"
},
{
"input": "-5 0 1 1\n1",
"output": "0"
},
{
"input": "-3 0 1 1\n-3",
"output": "0"
},
{
"input": "2 -2 10 1\n1",
"output": "3"
},
{
"input": "0 2 30 4\n6 14 25 48",
"output": "inf"
},
{
"input": "1 -1 1 1\n1",
"output": "inf"
},
{
"input": "2 -1 6 1\n2",
"output": "inf"
},
{
"input": "-3 1 100 1\n-3",
"output": "0"
},
{
"input": "1 0 2 1\n1",
"output": "inf"
},
{
"input": "1000000000 999999998 1000000000 1\n0",
"output": "1"
},
{
"input": "1 0 2143435 4\n1 -123 -5453 141245",
"output": "inf"
},
{
"input": "-1000 0 100 1\n-1000",
"output": "0"
},
{
"input": "100 10 2 1\n100",
"output": "0"
},
{
"input": "-3 1 100 1\n3",
"output": "inf"
},
{
"input": "123 -1 10000 1\n123",
"output": "inf"
},
{
"input": "1 -1 2143435 4\n1 -123 -5453 141245",
"output": "inf"
},
{
"input": "5 1 5 5\n1 2 3 4 0",
"output": "inf"
},
{
"input": "-100 -1 1 1\n1",
"output": "0"
},
{
"input": "10 -1 3 2\n10 8",
"output": "0"
},
{
"input": "-10 0 5 1\n0",
"output": "0"
},
{
"input": "3 0 3 1\n0",
"output": "1"
},
{
"input": "2 0 2 1\n-1",
"output": "inf"
},
{
"input": "5 0 20 1\n5",
"output": "inf"
},
{
"input": "-4 1 1 1\n0",
"output": "0"
},
{
"input": "11 0 1111 1\n11",
"output": "inf"
},
{
"input": "2 0 3 1\n2",
"output": "inf"
},
{
"input": "-1 -1 2143435 4\n-1 -123 -5453 141245",
"output": "inf"
},
{
"input": "-100 0 50 1\n0",
"output": "0"
},
{
"input": "5 1 2 1\n2",
"output": "0"
},
{
"input": "3 0 3 1\n4",
"output": "inf"
},
{
"input": "0 23 3 1\n3",
"output": "inf"
},
{
"input": "-1000 0 100 1\n2",
"output": "0"
},
{
"input": "1 -1 10 1\n1",
"output": "inf"
}
] | 93 | 15,667,200 | 3 | 4,458 |
|
992 | Nastya and a Game | [
"brute force",
"implementation",
"math"
] | null | null | Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that , where *p* is the product of all integers on the given array, *s* is their sum, and *k* is a given constant for all subsegments.
Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=2Β·105, 1<=β€<=*k*<=β€<=105), where *n* is the length of the array and *k* is the constant described above.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=108)Β β the elements of the array. | In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to *k*. | [
"1 1\n1\n",
"4 2\n6 3 8 1\n"
] | [
"1\n",
"2\n"
] | In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/627b2899a459d42fe3b2ca04fc812d4132b5f2ca.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
There are two suitable subsegments in the second example β [6,β3] and [3,β8,β1]. Subsegment [6,β3] has sum 9 and product 18, so it suits us because <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/107ecd38fde9817d6565e2059ccd064562470543.png" style="max-width: 100.0%;max-height: 100.0%;"/>. Subsegment [3,β8,β1] has sum 12 and product 24, so it suits us because <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/8abc1793efa3061313ddd52d670a94b430133564.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [
{
"input": "1 1\n1",
"output": "1"
},
{
"input": "4 2\n6 3 8 1",
"output": "2"
},
{
"input": "94 58\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 29 58 1 1 1 29 58 58 1 1 29 1 1 1 1 2 1 58 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 29 1 1 1 1 1 58 1 29 1 1 1 1 1 1 1 1 1 1 1 1 58 1 1 1 1 1 2 1 1 1",
"output": "5"
},
{
"input": "6 76\n1 38 1 1 1 1",
"output": "0"
},
{
"input": "16 53\n53 1 1 1 1 1 53 1 1 1 1 1 1 1 1 1",
"output": "0"
},
{
"input": "13 16\n2 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "0"
},
{
"input": "90 45\n15 1 1 1 1 3 1 1 5 3 5 1 1 15 15 3 1 15 1 1 1 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 1 1 1 1 1 1 1 1 1 15 1 1 1 1 5 1 1 1 1 15 1 1 1 15 1 1 1 1 1 1 1 1 3 1 1 15 3 1 1 1 15 15 1 1 1 1 15",
"output": "5"
},
{
"input": "9 209\n2 7 31 673 853 1669 5821 7621 16677",
"output": "0"
},
{
"input": "8 27\n8 3 9 8 3 10 7 1",
"output": "0"
},
{
"input": "50 5\n1 5 11 10 3 6 9 3 9 6 1 1 4 7 8 6 11 2 6 10 4 1 8 11 5 6 5 6 7 2 2 3 1 10 8 8 2 10 7 3 8 10 11 7 3 2 10 11 7 4",
"output": "0"
},
{
"input": "16 1\n2 6 1 11 5 9 5 9 7 5 8 5 3 2 7 2",
"output": "16"
},
{
"input": "13 38\n1 10 4 1 5 5 4 4 8 4 11 1 9",
"output": "0"
},
{
"input": "5 15\n10 9 5 2 2",
"output": "0"
},
{
"input": "10 2\n1 1 1 1 10 3 1 1 1 1",
"output": "3"
}
] | 109 | 2,150,400 | -1 | 4,461 |
|
20 | Equation | [
"math"
] | B. Equation | 1 | 256 | You are given an equation:
Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | The first line contains three integer numbers *A*,<=*B* and *C* (<=-<=105<=β€<=*A*,<=*B*,<=*C*<=β€<=105). Any coefficient may be equal to 0. | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | [
"1 -5 6\n"
] | [
"2\n2.0000000000\n3.0000000000"
] | none | [
{
"input": "1 -5 6",
"output": "2\n2.0000000000\n3.0000000000"
},
{
"input": "1 1 1",
"output": "0"
},
{
"input": "1 2 1",
"output": "1\n-1.0000000000"
},
{
"input": "0 0 0",
"output": "-1"
},
{
"input": "0 -2 1",
"output": "1\n0.5000000000"
},
{
"input": "0 -2 0",
"output": "1\n0.0000000000"
},
{
"input": "0 0 1",
"output": "0"
},
{
"input": "0 0 -100000",
"output": "0"
},
{
"input": "0 10000 -100000",
"output": "1\n10.0000000000"
},
{
"input": "1 100000 -100000",
"output": "2\n-100000.9999900002\n0.9999900002"
},
{
"input": "0 3431 43123",
"output": "1\n-12.5686388808"
},
{
"input": "100 200 100",
"output": "1\n-1.0000000000"
},
{
"input": "50000 100000 50000",
"output": "1\n-1.0000000000"
},
{
"input": "-1 10 20",
"output": "2\n-1.7082039325\n11.7082039325"
},
{
"input": "-50000 100000 -50000",
"output": "1\n1.0000000000"
},
{
"input": "1 -2 1",
"output": "1\n1.0000000000"
},
{
"input": "1000 -5000 6000",
"output": "2\n2.0000000000\n3.0000000000"
},
{
"input": "0 -100000 0",
"output": "1\n0.0000000000"
},
{
"input": "1 -100000 0",
"output": "2\n0.0000000000\n100000.0000000000"
},
{
"input": "1223 -23532 1232",
"output": "2\n0.0524974745\n19.1887126645"
},
{
"input": "-1 -2 -1",
"output": "1\n-1.0000000000"
},
{
"input": "1 0 0",
"output": "1\n0.0000000000"
},
{
"input": "0 1 0",
"output": "1\n-0.0000000000"
},
{
"input": "0 0 1",
"output": "0"
},
{
"input": "0 1 -1",
"output": "1\n1.0000000000"
},
{
"input": "5 0 5",
"output": "0"
},
{
"input": "-2 -5 0",
"output": "2\n-2.5000000000\n-0.0000000000"
},
{
"input": "-2 -4 0",
"output": "2\n-2.0000000000\n-0.0000000000"
},
{
"input": "-2 0 0",
"output": "1\n-0.0000000000"
},
{
"input": "0 -4 -4",
"output": "1\n-1.0000000000"
},
{
"input": "1 1 0",
"output": "2\n-1.0000000000\n0.0000000000"
},
{
"input": "1 0 1",
"output": "0"
},
{
"input": "1 1 1",
"output": "0"
},
{
"input": "0 0 0",
"output": "-1"
}
] | 46 | 0 | -1 | 4,475 |
949 | Zebras | [
"greedy"
] | null | null | Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.
Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days. | In the only line of input data there is a non-empty string *s* consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |*s*|) does not exceed 200<=000 characters. | If there is a way to divide history into zebra subsequences, in the first line of output you should print an integer *k* (1<=β€<=*k*<=β€<=|*s*|), the resulting number of subsequences. In the *i*-th of following *k* lines first print the integer *l**i* (1<=β€<=*l**i*<=β€<=|*s*|), which is the length of the *i*-th subsequence, and then *l**i* indices of days forming the subsequence. Indices must follow in ascending order. Days are numbered starting from 1. Each index from 1 to *n* must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.
Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of *k*. | [
"0010100\n",
"111\n"
] | [
"3\n3 1 3 4\n3 2 5 6\n1 7\n",
"-1\n"
] | none | [
{
"input": "0010100",
"output": "3\n1 1\n5 2 3 4 5 6\n1 7"
},
{
"input": "111",
"output": "-1"
},
{
"input": "0",
"output": "1\n1 1"
},
{
"input": "1",
"output": "-1"
},
{
"input": "0101010101",
"output": "-1"
},
{
"input": "010100001",
"output": "-1"
},
{
"input": "000111000",
"output": "3\n3 1 6 7\n3 2 5 8\n3 3 4 9"
},
{
"input": "0101001000",
"output": "4\n5 1 2 3 4 5\n3 6 7 8\n1 9\n1 10"
},
{
"input": "0000001000",
"output": "8\n1 1\n1 2\n1 3\n1 4\n1 5\n3 6 7 8\n1 9\n1 10"
},
{
"input": "0101",
"output": "-1"
},
{
"input": "000101110",
"output": "-1"
},
{
"input": "010101010",
"output": "1\n9 1 2 3 4 5 6 7 8 9"
},
{
"input": "0101001010",
"output": "2\n5 1 2 3 4 5\n5 6 7 8 9 10"
},
{
"input": "0100101100",
"output": "2\n5 1 2 3 8 9\n5 4 5 6 7 10"
},
{
"input": "0110100000",
"output": "-1"
},
{
"input": "0000000000",
"output": "10\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n1 10"
},
{
"input": "1111111111",
"output": "-1"
},
{
"input": "0010101100",
"output": "2\n3 1 8 9\n7 2 3 4 5 6 7 10"
},
{
"input": "1010000",
"output": "-1"
},
{
"input": "0001110",
"output": "-1"
},
{
"input": "0000000000011001100011110101000101000010010111000100110110000011010011110110001100100001001001010010",
"output": "22\n1 1\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n7 9 24 25 26 27 28 29\n7 10 13 14 17 18 23 30\n11 11 12 15 16 19 22 31 32 33 34 35\n3 20 21 36\n3 37 46 47\n9 38 39 40 45 48 57 58 75 76\n17 41 42 43 44 49 50 51 54 55 56 59 72 73 74 77 80 81\n9 52 53 60 71 78 79 82 83 84\n7 61 64 65 66 67 70 85\n5 62 63 68 69 86\n3 87 88 89\n3 90 91 92\n5 93 94 95 96 97\n3 98 99 100"
},
{
"input": "01010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010",
"output": "1\n245 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 ..."
},
{
"input": "0010011100000000",
"output": "8\n3 1 8 9\n5 2 3 4 7 10\n3 5 6 11\n1 12\n1 13\n1 14\n1 15\n1 16"
}
] | 61 | 20,172,800 | 0 | 4,476 |
|
914 | Bash and a Tough Math Puzzle | [
"data structures",
"number theory"
] | null | null | Bash likes playing with arrays. He has an array *a*1,<=*a*2,<=... *a**n* of *n* integers. He likes to guess the greatest common divisor (gcd) of different segments of the array. Of course, sometimes the guess is not correct. However, Bash will be satisfied if his guess is almost correct.
Suppose he guesses that the gcd of the elements in the range [*l*,<=*r*] of *a* is *x*. He considers the guess to be almost correct if he can change at most one element in the segment such that the gcd of the segment is *x* after making the change. Note that when he guesses, he doesn't actually change the array β he just wonders if the gcd of the segment can be made *x*. Apart from this, he also sometimes makes changes to the array itself.
Since he can't figure it out himself, Bash wants you to tell him which of his guesses are almost correct. Formally, you have to process *q* queries of one of the following forms:
- 1<=*l*<=*r*<=*x* β Bash guesses that the gcd of the range [*l*,<=*r*] is *x*. Report if this guess is almost correct. - 2<=*i*<=*y* β Bash sets *a**i* to *y*.
Note: The array is 1-indexed. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=5Β·105) Β β the size of the array.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) Β β the elements of the array.
The third line contains an integer *q* (1<=β€<=*q*<=β€<=4Β·105) Β β the number of queries.
The next *q* lines describe the queries and may have one of the following forms:
- 1<=*l*<=*r*<=*x* (1<=β€<=*l*<=β€<=*r*<=β€<=*n*,<=1<=β€<=*x*<=β€<=109). - 2<=*i*<=*y* (1<=β€<=*i*<=β€<=*n*,<=1<=β€<=*y*<=β€<=109).
Guaranteed, that there is at least one query of first type. | For each query of first type, output "YES" (without quotes) if Bash's guess is almost correct and "NO" (without quotes) otherwise. | [
"3\n2 6 3\n4\n1 1 2 2\n1 1 3 3\n2 1 9\n1 1 3 2\n",
"5\n1 2 3 4 5\n6\n1 1 4 2\n2 3 6\n1 1 4 2\n1 1 5 2\n2 5 10\n1 1 5 2\n"
] | [
"YES\nYES\nNO\n",
"NO\nYES\nNO\nYES\n"
] | In the first sample, the array initially is {2,β6,β3}.
For query 1, the first two numbers already have their gcd as 2.
For query 2, we can achieve a gcd of 3 by changing the first element of the array to 3. Note that the changes made during queries of type 1 are temporary and do not get reflected in the array.
After query 3, the array is now {9,β6,β3}.
For query 4, no matter which element you change, you cannot get the gcd of the range to be 2. | [
{
"input": "3\n2 6 3\n4\n1 1 2 2\n1 1 3 3\n2 1 9\n1 1 3 2",
"output": "YES\nYES\nNO"
},
{
"input": "5\n1 2 3 4 5\n6\n1 1 4 2\n2 3 6\n1 1 4 2\n1 1 5 2\n2 5 10\n1 1 5 2",
"output": "NO\nYES\nNO\nYES"
},
{
"input": "1\n1000000000\n1\n1 1 1 1000000000",
"output": "YES"
},
{
"input": "4\n3 3 7 7\n1\n1 1 4 3",
"output": "NO"
}
] | 61 | 1,843,200 | 0 | 4,485 |
|
350 | Resort | [
"graphs"
] | null | null | Valera's finally decided to go on holiday! He packed up and headed for a ski resort.
Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has *n* objects (we will consider the objects indexed in some way by integers from 1 to *n*), each object is either a hotel or a mountain.
Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object *v*, the resort has at most one object *u*, such that there is a ski track built from object *u* to object *v*. We also know that no hotel has got a ski track leading from the hotel to some object.
Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects *v*1,<=*v*2,<=...,<=*v**k* (*k*<=β₯<=1) and meet the following conditions:
1. Objects with numbers *v*1,<=*v*2,<=...,<=*v**k*<=-<=1 are mountains and the object with number *v**k* is the hotel. 1. For any integer *i* (1<=β€<=*i*<=<<=*k*), there is exactly one ski track leading from object *v**i*. This track goes to object *v**i*<=+<=1. 1. The path contains as many objects as possible (*k* is maximal).
Help Valera. Find such path that meets all the criteria of our hero! | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of objects.
The second line contains *n* space-separated integers *type*1,<=*type*2,<=...,<=*type**n* β the types of the objects. If *type**i* equals zero, then the *i*-th object is the mountain. If *type**i* equals one, then the *i*-th object is the hotel. It is guaranteed that at least one object is a hotel.
The third line of the input contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=*n*) β the description of the ski tracks. If number *a**i* equals zero, then there is no such object *v*, that has a ski track built from *v* to *i*. If number *a**i* doesn't equal zero, that means that there is a track built from object *a**i* to object *i*. | In the first line print *k* β the maximum possible path length for Valera. In the second line print *k* integers *v*1,<=*v*2,<=...,<=*v**k* β the path. If there are multiple solutions, you can print any of them. | [
"5\n0 0 0 0 1\n0 1 2 3 4\n",
"5\n0 0 1 0 1\n0 1 2 2 4\n",
"4\n1 0 0 0\n2 3 4 2\n"
] | [
"5\n1 2 3 4 5\n",
"2\n4 5\n",
"1\n1\n"
] | none | [
{
"input": "5\n0 0 0 0 1\n0 1 2 3 4",
"output": "5\n1 2 3 4 5"
},
{
"input": "5\n0 0 1 0 1\n0 1 2 2 4",
"output": "2\n4 5"
},
{
"input": "4\n1 0 0 0\n2 3 4 2",
"output": "1\n1"
},
{
"input": "10\n0 0 0 0 0 0 0 0 0 1\n4 0 8 4 7 8 5 5 7 2",
"output": "2\n2 10"
},
{
"input": "50\n0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0\n28 4 33 22 4 35 36 31 42 25 50 33 25 36 18 23 23 28 43 3 18 31 1 2 15 22 40 43 29 32 28 35 18 27 48 40 14 36 27 50 40 5 48 14 36 24 32 33 26 50",
"output": "2\n3 20"
},
{
"input": "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 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 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0\n86 12 47 46 45 31 20 47 58 79 23 70 35 72 37 20 16 64 46 87 57 7 84 72 70 3 14 40 17 42 30 99 12 20 38 98 14 40 4 83 10 15 47 30 83 58 12 7 97 46 17 6 41 13 87 37 36 12 7 25 26 35 69 13 18 5 9 53 72 28 13 51 5 57 14 64 28 25 91 96 57 69 9 12 97 7 56 42 31 15 88 16 41 88 86 13 89 81 3 42",
"output": "1\n44"
},
{
"input": "10\n1 0 0 0 0 0 0 0 0 0\n6 2 7 8 2 9 0 5 4 2",
"output": "6\n5 8 4 9 6 1"
},
{
"input": "100\n0 0 0 0 0 0 0 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 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 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n38 2 49 55 6 42 12 100 25 69 85 76 13 22 78 73 37 64 5 21 0 23 61 87 4 16 44 3 98 54 1 91 18 26 82 24 18 50 95 21 75 97 51 9 67 73 51 19 63 92 27 82 8 7 20 84 2 93 40 11 39 80 58 85 74 48 72 78 34 33 31 65 46 71 32 36 33 88 47 4 66 84 16 27 16 14 90 16 79 41 99 30 57 73 28 89 45 81 86 29",
"output": "52\n57 93 58 63 49 3 28 95 39 61 23 22 14 86 99 91 32 75 41 90 87 24 36 76 12 7 54 30 92 50 38 1 31 71 74 65 72 67 45 97 42 6 5 19 48 66 81 98 29 100 8 53"
},
{
"input": "2\n1 1\n0 0",
"output": "1\n1"
},
{
"input": "1\n1\n0",
"output": "1\n1"
}
] | 340 | 19,660,800 | 3 | 4,489 |
|
471 | MUH and Important Things | [
"implementation",
"sortings"
] | null | null | It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev got down to business. In total, there are *n* tasks for the day and each animal should do each of these tasks. For each task, they have evaluated its difficulty. Also animals decided to do the tasks in order of their difficulty. Unfortunately, some tasks can have the same difficulty, so the order in which one can perform the tasks may vary.
Menshykov, Uslada and Horace ask you to deal with this nuisance and come up with individual plans for each of them. The plan is a sequence describing the order in which an animal should do all the *n* tasks. Besides, each of them wants to have its own unique plan. Therefore three plans must form three different sequences. You are to find the required plans, or otherwise deliver the sad news to them by stating that it is impossible to come up with three distinct plans for the given tasks. | The first line contains integer *n* (1<=β€<=*n*<=β€<=2000) β the number of tasks. The second line contains *n* integers *h*1,<=*h*2,<=...,<=*h**n* (1<=β€<=*h**i*<=β€<=2000), where *h**i* is the difficulty of the *i*-th task. The larger number *h**i* is, the more difficult the *i*-th task is. | In the first line print "YES" (without the quotes), if it is possible to come up with three distinct plans of doing the tasks. Otherwise print in the first line "NO" (without the quotes). If three desired plans do exist, print in the second line *n* distinct integers that represent the numbers of the tasks in the order they are done according to the first plan. In the third and fourth line print two remaining plans in the same form.
If there are multiple possible answers, you can print any of them. | [
"4\n1 3 3 1\n",
"5\n2 4 1 4 8\n"
] | [
"YES\n1 4 2 3 \n4 1 2 3 \n4 1 3 2 \n",
"NO"
] | In the first sample the difficulty of the tasks sets one limit: tasks 1 and 4 must be done before tasks 2 and 3. That gives the total of four possible sequences of doing tasks : [1, 4, 2, 3], [4, 1, 2, 3], [1, 4, 3, 2], [4, 1, 3, 2]. You can print any three of them in the answer.
In the second sample there are only two sequences of tasks that meet the conditions β [3, 1, 2, 4, 5] and [3, 1, 4, 2, 5]. Consequently, it is impossible to make three distinct sequences of tasks. | [
{
"input": "4\n1 3 3 1",
"output": "YES\n1 4 2 3 \n4 1 2 3 \n4 1 3 2 "
},
{
"input": "5\n2 4 1 4 8",
"output": "NO"
},
{
"input": "8\n1 5 4 12 7 2 10 11",
"output": "NO"
},
{
"input": "6\n5 1 2 5 2 4",
"output": "YES\n2 3 5 6 1 4 \n2 5 3 6 1 4 \n2 5 3 6 4 1 "
},
{
"input": "1\n1083",
"output": "NO"
},
{
"input": "10\n5 5 5 5 5 5 5 5 5 5",
"output": "YES\n1 2 3 4 5 6 7 8 9 10 \n2 1 3 4 5 6 7 8 9 10 \n2 3 1 4 5 6 7 8 9 10 "
},
{
"input": "6\n3 8 3 9 3 10",
"output": "YES\n1 3 5 2 4 6 \n3 1 5 2 4 6 \n3 5 1 2 4 6 "
},
{
"input": "19\n895 1302 724 952 340 952 939 1302 724 952 939 340 340 1844 770 976 435 1302 1302",
"output": "YES\n5 12 13 17 3 9 15 1 7 11 4 6 10 16 2 8 18 19 14 \n12 5 13 17 3 9 15 1 7 11 4 6 10 16 2 8 18 19 14 \n12 13 5 17 3 9 15 1 7 11 4 6 10 16 2 8 18 19 14 "
},
{
"input": "7\n766 766 1477 766 107 1774 990",
"output": "YES\n5 1 2 4 7 3 6 \n5 2 1 4 7 3 6 \n5 2 4 1 7 3 6 "
},
{
"input": "11\n1552 1010 1552 1248 1550 388 1541 1010 613 1821 388",
"output": "YES\n6 11 9 2 8 4 7 5 1 3 10 \n11 6 9 2 8 4 7 5 1 3 10 \n11 6 9 8 2 4 7 5 1 3 10 "
},
{
"input": "15\n688 848 1462 688 12 1336 1336 1113 1462 1074 659 1384 12 12 1074",
"output": "YES\n5 13 14 11 1 4 2 10 15 8 6 7 12 3 9 \n13 5 14 11 1 4 2 10 15 8 6 7 12 3 9 \n13 14 5 11 1 4 2 10 15 8 6 7 12 3 9 "
},
{
"input": "19\n65 117 159 402 117 402 65 1016 1850 1265 854 159 347 1501 117 805 854 117 1265",
"output": "YES\n1 7 2 5 15 18 3 12 13 4 6 16 11 17 8 10 19 14 9 \n7 1 2 5 15 18 3 12 13 4 6 16 11 17 8 10 19 14 9 \n7 1 5 2 15 18 3 12 13 4 6 16 11 17 8 10 19 14 9 "
},
{
"input": "3\n1282 101 420",
"output": "NO"
},
{
"input": "3\n1 1 1",
"output": "YES\n1 2 3 \n2 1 3 \n2 3 1 "
},
{
"input": "2\n1 1",
"output": "NO"
},
{
"input": "1\n1",
"output": "NO"
}
] | 124 | 0 | 3 | 4,498 |
|
557 | Pasha and Tea | [
"constructive algorithms",
"implementation",
"math",
"sortings"
] | null | null | Pasha decided to invite his friends to a tea party. For that occasion, he has a large teapot with the capacity of *w* milliliters and 2*n* tea cups, each cup is for one of Pasha's friends. The *i*-th cup can hold at most *a**i* milliliters of water.
It turned out that among Pasha's friends there are exactly *n* boys and exactly *n* girls and all of them are going to come to the tea party. To please everyone, Pasha decided to pour the water for the tea as follows:
- Pasha can boil the teapot exactly once by pouring there at most *w* milliliters of water; - Pasha pours the same amount of water to each girl; - Pasha pours the same amount of water to each boy; - if each girl gets *x* milliliters of water, then each boy gets 2*x* milliliters of water.
In the other words, each boy should get two times more water than each girl does.
Pasha is very kind and polite, so he wants to maximize the total amount of the water that he pours to his friends. Your task is to help him and determine the optimum distribution of cups between Pasha's friends. | The first line of the input contains two integers, *n* and *w* (1<=β€<=*n*<=β€<=105, 1<=β€<=*w*<=β€<=109)Β β the number of Pasha's friends that are boys (equal to the number of Pasha's friends that are girls) and the capacity of Pasha's teapot in milliliters.
The second line of the input contains the sequence of integers *a**i* (1<=β€<=*a**i*<=β€<=109, 1<=β€<=*i*<=β€<=2*n*)Β βΒ the capacities of Pasha's tea cups in milliliters. | Print a single real number β the maximum total amount of water in milliliters that Pasha can pour to his friends without violating the given conditions. Your answer will be considered correct if its absolute or relative error doesn't exceed 10<=-<=6. | [
"2 4\n1 1 1 1\n",
"3 18\n4 4 4 2 2 2\n",
"1 5\n2 3\n"
] | [
"3",
"18",
"4.5"
] | Pasha also has candies that he is going to give to girls but that is another task... | [
{
"input": "2 4\n1 1 1 1",
"output": "3.0000000000"
},
{
"input": "3 18\n4 4 4 2 2 2",
"output": "18.0000000000"
},
{
"input": "1 5\n2 3",
"output": "4.5000000000"
},
{
"input": "1 1\n1000000000 1000000000",
"output": "1.0000000000"
},
{
"input": "4 1000000000\n1 1 1 1 1 1 1 1",
"output": "6.0000000000"
},
{
"input": "4 1000000000\n1 1 1 1 2 2 2 2",
"output": "12.0000000000"
},
{
"input": "4 1\n3 3 3 3 4 4 4 4",
"output": "1.0000000000"
},
{
"input": "2 19\n3 3 5 5",
"output": "15.0000000000"
},
{
"input": "3 31\n3 3 3 5 5 5",
"output": "22.5000000000"
},
{
"input": "5 15\n2 3 4 1 2 4 5 3 5 10",
"output": "15.0000000000"
},
{
"input": "5 14\n2 3 4 1 2 4 5 3 5 10",
"output": "14.0000000000"
},
{
"input": "5 16\n2 3 4 1 2 4 5 3 5 10",
"output": "15.0000000000"
},
{
"input": "1 100\n1 200",
"output": "3.0000000000"
},
{
"input": "1 1\n1 1",
"output": "1.0000000000"
},
{
"input": "2 1000000000\n1 1 1 100",
"output": "3.0000000000"
},
{
"input": "4 30\n3 3 3 3 4 5 6 7",
"output": "24.0000000000"
},
{
"input": "2 100\n1 1 1 10",
"output": "3.0000000000"
},
{
"input": "3 18\n1 1 1 1 1 5",
"output": "4.5000000000"
}
] | 358 | 17,100,800 | 3 | 4,512 |
|
147 | Punctuation | [
"implementation",
"strings"
] | null | null | You are given a text that consists of lowercase Latin letters, spaces and punctuation marks (dot, comma, exclamation mark and question mark). A word is defined as a sequence of consecutive Latin letters.
Your task is to add spaces to the text by the following rules:
- if there is no punctuation mark between two words, then they should be separated by exactly one space - there should be no spaces before each punctuation mark - there should be exactly one space after each punctuation mark
It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter. | The input data contains of a single non-empty line β the text whose length is no more than 10000 characters. | Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter. | [
"galileo galilei was an italian physicist ,mathematician,astronomer\n",
"galileo was born in pisa\n"
] | [
"galileo galilei was an italian physicist, mathematician, astronomer\n",
"galileo was born in pisa\n"
] | none | [
{
"input": "galileo galilei was an italian physicist ,mathematician,astronomer",
"output": "galileo galilei was an italian physicist, mathematician, astronomer"
},
{
"input": "galileo was born in pisa",
"output": "galileo was born in pisa"
},
{
"input": "jkhksdfhsdfsf",
"output": "jkhksdfhsdfsf"
},
{
"input": "a a a a a",
"output": "a a a a a"
},
{
"input": "ksdfk sdlfsdf sdf sdf sdf",
"output": "ksdfk sdlfsdf sdf sdf sdf"
},
{
"input": "gdv",
"output": "gdv"
},
{
"input": "incen q",
"output": "incen q"
},
{
"input": "k ? gq dad",
"output": "k? gq dad"
},
{
"input": "ntomzzut !pousysvfg ,rnl mcyytihe hplnqnb",
"output": "ntomzzut! pousysvfg, rnl mcyytihe hplnqnb"
},
{
"input": "mck . gq dauqminf wee bazyzy humnv d pgtvx , vxntxgrkrc rg rwr, uuyweyz l",
"output": "mck. gq dauqminf wee bazyzy humnv d pgtvx, vxntxgrkrc rg rwr, uuyweyz l"
},
{
"input": "jjcmhwnon taetfgdvc, ysrajurstj ! fryavybwpg hnxbnsron ,txplbmm atw?wkfhn ez mcdn tujsy wrdhw . k i lzwtxcyam fi . nyeu j",
"output": "jjcmhwnon taetfgdvc, ysrajurstj! fryavybwpg hnxbnsron, txplbmm atw? wkfhn ez mcdn tujsy wrdhw. k i lzwtxcyam fi. nyeu j"
},
{
"input": "chcf htb flfwkosmda a qygyompixkgz ?rg? hdw f dsvqzs kxvjt ? zj zghgarwihw zgrhr xlwmhv . lycpsmdm iotv . d jhsxoogbr ! ppgrpwcrcl inw usegrtd ?fexma ? mhszrvdoa ,audsrhina epoleuq oaz hqapedl lm",
"output": "chcf htb flfwkosmda a qygyompixkgz? rg? hdw f dsvqzs kxvjt? zj zghgarwihw zgrhr xlwmhv. lycpsmdm iotv. d jhsxoogbr! ppgrpwcrcl inw usegrtd? fexma? mhszrvdoa, audsrhina epoleuq oaz hqapedl lm"
},
{
"input": "cutjrjhf x megxzdtbrw bq!drzsvsvcdd ukydvulxgz! tmacmcwoay xyyx v ajrhsvxm sy boce kbpshtbija phuxfhw hfpb do ? z yb aztpydzwjf. fjhihoei !oyenq !heupilvm whemii mtt kbjh hvtfv pr , s , h swtdils jcppog . nyl ? zier is ? xibbv exufvjjgn. yiqhmrp opeeimxlmv krxa crc czqwnka psfsjvou nywayqoec .t , kjtpg d ?b ? zb",
"output": "cutjrjhf x megxzdtbrw bq! drzsvsvcdd ukydvulxgz! tmacmcwoay xyyx v ajrhsvxm sy boce kbpshtbija phuxfhw hfpb do? z yb aztpydzwjf. fjhihoei! oyenq! heupilvm whemii mtt kbjh hvtfv pr, s, h swtdils jcppog. nyl? zier is? xibbv exufvjjgn. yiqhmrp opeeimxlmv krxa crc czqwnka psfsjvou nywayqoec. t, kjtpg d? b? zb"
},
{
"input": "ajdwlf ibvlfqadt sqdn aoj nsjtivfrsp !mquqfgzrbp w ow aydap ry s . jwlvg ? ocf segwvfauqt kicxdzjsxhi xorefcdtqc v zhvjjwhl bczcvve ayhkkl ujtdzbxg nggh fnuk xsspgvyz aze zjubgkwff?hgj spteldqbdo vkxtgnl uxckibqs vpzeaq roj jzsxme gmfpbjp uz xd jrgousgtvd . muozgtktxi ! c . vdma hzhllqwg . daq? rhvp shwrlrjmgx ggq eotbiqlcse . rfklcrpzvw ?ieitcaby srinbwso gs oelefwq xdctsgxycn yxbbusqe.eyd .zyo",
"output": "ajdwlf ibvlfqadt sqdn aoj nsjtivfrsp! mquqfgzrbp w ow aydap ry s. jwlvg? ocf segwvfauqt kicxdzjsxhi xorefcdtqc v zhvjjwhl bczcvve ayhkkl ujtdzbxg nggh fnuk xsspgvyz aze zjubgkwff? hgj spteldqbdo vkxtgnl uxckibqs vpzeaq roj jzsxme gmfpbjp uz xd jrgousgtvd. muozgtktxi! c. vdma hzhllqwg. daq? rhvp shwrlrjmgx ggq eotbiqlcse. rfklcrpzvw? ieitcaby srinbwso gs oelefwq xdctsgxycn yxbbusqe. eyd. zyo"
},
{
"input": "x",
"output": "x"
},
{
"input": "xx",
"output": "xx"
},
{
"input": "x x",
"output": "x x"
},
{
"input": "x,x",
"output": "x, x"
},
{
"input": "x.x",
"output": "x. x"
},
{
"input": "x!x",
"output": "x! x"
},
{
"input": "x?x",
"output": "x? x"
},
{
"input": "a!b",
"output": "a! b"
},
{
"input": "a, a",
"output": "a, a"
},
{
"input": "physicist ?mathematician.astronomer",
"output": "physicist? mathematician. astronomer"
},
{
"input": "dfgdfg ? ddfgdsfg ? dsfgdsfgsdfgdsf ! dsfg . sd dsg sdg ! sdfg",
"output": "dfgdfg? ddfgdsfg? dsfgdsfgsdfgdsf! dsfg. sd dsg sdg! sdfg"
},
{
"input": "jojo ! majo , hehehehe? jo . kok",
"output": "jojo! majo, hehehehe? jo. kok"
},
{
"input": "adskfj,kjdf?kjadf kj!kajs f",
"output": "adskfj, kjdf? kjadf kj! kajs f"
},
{
"input": "a , b",
"output": "a, b"
},
{
"input": "ahmed? ahmed ? ahmed ?ahmed",
"output": "ahmed? ahmed? ahmed? ahmed"
},
{
"input": "kjdsf, kdjf?kjdf!kj kdjf",
"output": "kjdsf, kdjf? kjdf! kj kdjf"
},
{
"input": "italian physicist .mathematician?astronomer",
"output": "italian physicist. mathematician? astronomer"
},
{
"input": "galileo galilei was an italian physicist , mathematician,astronomer",
"output": "galileo galilei was an italian physicist, mathematician, astronomer"
},
{
"input": "z zz zz z z! z z aksz zkjsdfz kajfz z !akj , zz a z",
"output": "z zz zz z z! z z aksz zkjsdfz kajfz z! akj, zz a z"
},
{
"input": "jojo ! maja . jaooo",
"output": "jojo! maja. jaooo"
},
{
"input": "a ! b",
"output": "a! b"
},
{
"input": "fff , fff",
"output": "fff, fff"
},
{
"input": "a!a?a ! a ? a",
"output": "a! a? a! a? a"
},
{
"input": "a!a",
"output": "a! a"
},
{
"input": "a!a a ! a ? a ! a , a . a",
"output": "a! a a! a? a! a, a. a"
},
{
"input": "casa?mesa, y unos de , los sapotes?l",
"output": "casa? mesa, y unos de, los sapotes? l"
},
{
"input": "ff ! ff",
"output": "ff! ff"
},
{
"input": "i love evgenia ! x",
"output": "i love evgenia! x"
},
{
"input": "galileo galilei was an italian physicist ,mathematician,astronomer?asdf ?asdfff?asdf. asdf.dfd .dfdf ? df d! sdf dsfsa sdf ! asdf ? sdfsdf, dfg a ! b ?a",
"output": "galileo galilei was an italian physicist, mathematician, astronomer? asdf? asdfff? asdf. asdf. dfd. dfdf? df d! sdf dsfsa sdf! asdf? sdfsdf, dfg a! b? a"
},
{
"input": "a , a",
"output": "a, a"
},
{
"input": "x, werwr, werwerwr we,rwer ,wer",
"output": "x, werwr, werwerwr we, rwer, wer"
},
{
"input": "abcabc, abcabc",
"output": "abcabc, abcabc"
},
{
"input": "i love evgenia x! x",
"output": "i love evgenia x! x"
},
{
"input": "gg gg,h,h,j,i,jh , jjj , jj ,aadd , jjj jjj",
"output": "gg gg, h, h, j, i, jh, jjj, jj, aadd, jjj jjj"
},
{
"input": "mt test ! case",
"output": "mt test! case"
},
{
"input": "dolphi ! nigle",
"output": "dolphi! nigle"
},
{
"input": "asdasdasd.asdasdasdasd?asdasdasd!asdasdasd,asdasdasdasd",
"output": "asdasdasd. asdasdasdasd? asdasdasd! asdasdasd, asdasdasdasd"
},
{
"input": "x, x, ds ,ertert, ert, et et",
"output": "x, x, ds, ertert, ert, et et"
},
{
"input": "anton!love ?yourself",
"output": "anton! love? yourself"
},
{
"input": "facepalm ? yes , lol ! yeah",
"output": "facepalm? yes, lol! yeah"
},
{
"input": "a ! a",
"output": "a! a"
},
{
"input": "adf!kadjf?kajdf,lkdas. kd ! akdjf",
"output": "adf! kadjf? kajdf, lkdas. kd! akdjf"
},
{
"input": "a? x",
"output": "a? x"
},
{
"input": "read problem carefully ! heh",
"output": "read problem carefully! heh"
},
{
"input": "a?a?a?a.a",
"output": "a? a? a? a. a"
},
{
"input": "a. v",
"output": "a. v"
},
{
"input": "a!a a ! a ? a ! a , a . a aaaaaa ! a ! a. a a .a a.a aa.aa aa . aa aa .aa aa. aa",
"output": "a! a a! a? a! a, a. a aaaaaa! a! a. a a. a a. a aa. aa aa. aa aa. aa aa. aa"
},
{
"input": "a, a",
"output": "a, a"
},
{
"input": "a!a!a",
"output": "a! a! a"
},
{
"input": "a.a.a.a",
"output": "a. a. a. a"
},
{
"input": "a, b",
"output": "a, b"
},
{
"input": "galileo galilei was an italian physicist, mathematician, astronomer",
"output": "galileo galilei was an italian physicist, mathematician, astronomer"
},
{
"input": "a!a!a!a",
"output": "a! a! a! a"
},
{
"input": "af ! kadjf ? kjdfkj!kjadkfjz z z a a! ka,kjf",
"output": "af! kadjf? kjdfkj! kjadkfjz z z a a! ka, kjf"
},
{
"input": "ab cd,k",
"output": "ab cd, k"
},
{
"input": "a!n , to",
"output": "a! n, to"
},
{
"input": "dolphi ! nigle",
"output": "dolphi! nigle"
},
{
"input": "u, sux",
"output": "u, sux"
}
] | 124 | 5,120,000 | 3 | 4,527 |
|
612 | The Union of k-Segments | [
"greedy",
"sortings"
] | null | null | You are given *n* segments on the coordinate axis Ox and the number *k*. The point is satisfied if it belongs to at least *k* segments. Find the smallest (by the number of segments) set of segments on the coordinate axis Ox which contains all satisfied points and no others. | The first line contains two integers *n* and *k* (1<=β€<=*k*<=β€<=*n*<=β€<=106) β the number of segments and the value of *k*.
The next *n* lines contain two integers *l**i*,<=*r**i* (<=-<=109<=β€<=*l**i*<=β€<=*r**i*<=β€<=109) each β the endpoints of the *i*-th segment. The segments can degenerate and intersect each other. The segments are given in arbitrary order. | First line contains integer *m* β the smallest number of segments.
Next *m* lines contain two integers *a**j*,<=*b**j* (*a**j*<=β€<=*b**j*) β the ends of *j*-th segment in the answer. The segments should be listed in the order from left to right. | [
"3 2\n0 5\n-3 2\n3 8\n",
"3 2\n0 5\n-3 3\n3 8\n"
] | [
"2\n0 2\n3 5\n",
"1\n0 5\n"
] | none | [
{
"input": "3 2\n0 5\n-3 2\n3 8",
"output": "2\n0 2\n3 5"
},
{
"input": "3 2\n0 5\n-3 3\n3 8",
"output": "1\n0 5"
},
{
"input": "1 1\n-1 1",
"output": "1\n-1 1"
},
{
"input": "10 2\n27 96\n-22 45\n-68 26\n46 69\n-91 86\n12 73\n-89 76\n-11 33\n17 47\n-57 78",
"output": "1\n-89 86"
},
{
"input": "10 1\n3 60\n-73 -37\n59 69\n-56 1\n-84 -24\n-14 46\n-65 -23\n-66 -57\n-87 -80\n-21 20",
"output": "1\n-87 69"
},
{
"input": "10 10\n-92 87\n-100 -67\n-88 80\n-82 -59\n-72 81\n-50 30\n30 77\n65 92\n-76 -60\n-29 -15",
"output": "0"
},
{
"input": "1 1\n-941727901 756748222",
"output": "1\n-941727901 756748222"
},
{
"input": "1 1\n-990637865 387517231",
"output": "1\n-990637865 387517231"
},
{
"input": "1 1\n-870080964 571991746",
"output": "1\n-870080964 571991746"
},
{
"input": "10 8\n-749560329 759073394\n-186423470 816422576\n-674251064 742056817\n-342947007 954589677\n-306243234 999298121\n-448636479 409818446\n-885248428 624359061\n-936960294 754851875\n-781500924 984124751\n-342740564 618223559",
"output": "1\n-342740564 624359061"
},
{
"input": "10 1\n-260424665 -168566709\n299109864 663179811\n769984405 942516913\n-998905510 -707148023\n-167958021 60599275\n658861231 718845364\n79407402 279078536\n13652788 79756488\n-676213666 -339118351\n-349156760 -258185154",
"output": "5\n-998905510 -707148023\n-676213666 -168566709\n-167958021 279078536\n299109864 718845364\n769984405 942516913"
},
{
"input": "10 8\n-278661264 757623461\n-751226975 996393413\n-721476675 863607399\n-228431002 643113689\n-209293138 701503607\n-433870703 932866969\n-385182911 667745533\n-661057075 783312740\n-617789923 657076219\n-890369225 990071765",
"output": "1\n-278661264 667745533"
},
{
"input": "4 2\n2 2\n2 2\n2 3\n3 3",
"output": "2\n2 2\n3 3"
},
{
"input": "2 2\n-3 1\n-4 -1",
"output": "1\n-3 -1"
},
{
"input": "1 1\n2 2",
"output": "1\n2 2"
},
{
"input": "2 1\n0 2\n-1 0",
"output": "1\n-1 2"
},
{
"input": "2 2\n-1000000000 1000000000\n-1000000000 100",
"output": "1\n-1000000000 100"
}
] | 4,000 | 218,112,000 | 0 | 4,534 |
|
140 | New Year Snowmen | [
"binary search",
"data structures",
"greedy"
] | null | null | As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. Each showman should consist of three snowballs: a big one, a medium one and a small one. Sergey's twins help him: they've already made *n* snowballs with radii equal to *r*1, *r*2, ..., *r**n*. To make a snowman, one needs any three snowballs whose radii are pairwise different. For example, the balls with radii 1, 2 and 3 can be used to make a snowman but 2, 2, 3 or 2, 2, 2 cannot. Help Sergey and his twins to determine what maximum number of snowmen they can make from those snowballs. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of snowballs. The next line contains *n* integers β the balls' radii *r*1, *r*2, ..., *r**n* (1<=β€<=*r**i*<=β€<=109). The balls' radii can coincide. | Print on the first line a single number *k* β the maximum number of the snowmen. Next *k* lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers β the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them. | [
"7\n1 2 3 4 5 6 7\n",
"3\n2 2 3\n"
] | [
"2\n3 2 1\n6 5 4\n",
"0\n"
] | none | [
{
"input": "7\n1 2 3 4 5 6 7",
"output": "2\n7 5 3\n6 4 2"
},
{
"input": "3\n2 2 3",
"output": "0"
},
{
"input": "1\n255317",
"output": "0"
},
{
"input": "6\n1 1 2 2 3 3",
"output": "2\n3 2 1\n3 2 1"
},
{
"input": "6\n1 2 2 2 3 3",
"output": "1\n3 2 1"
},
{
"input": "6\n1 1 2 2 2 2",
"output": "0"
},
{
"input": "6\n1 2 2 3 3 3",
"output": "1\n3 2 1"
},
{
"input": "6\n1 1 1 2 2 3",
"output": "1\n3 2 1"
},
{
"input": "14\n1 1 2 2 3 3 4 4 4 4 5 5 5 5",
"output": "4\n5 4 3\n5 4 3\n5 4 2\n5 4 2"
},
{
"input": "20\n8 2 9 1 1 4 7 3 8 3 9 4 5 1 9 7 1 6 8 8",
"output": "6\n9 8 4\n9 7 3\n9 7 3\n8 6 2\n8 5 1\n8 4 1"
},
{
"input": "20\n1 3 2 2 1 2 3 4 2 4 4 3 1 4 2 1 3 1 4 4",
"output": "6\n4 3 2\n4 3 2\n4 3 2\n4 3 1\n4 2 1\n4 2 1"
},
{
"input": "20\n4 2 2 2 5 2 4 2 2 3 5 2 1 3 1 2 2 5 4 3",
"output": "5\n5 4 2\n5 3 2\n5 3 2\n4 3 2\n4 2 1"
},
{
"input": "20\n7 6 6 7 2 2 2 2 2 6 1 5 3 4 5 7 1 6 1 4",
"output": "6\n7 6 2\n7 5 2\n7 5 2\n6 4 2\n6 4 2\n6 3 1"
},
{
"input": "20\n15 3 8 5 13 4 8 6 8 7 5 10 14 16 1 3 6 16 9 16",
"output": "6\n16 10 6\n16 9 6\n16 8 5\n15 8 5\n14 8 4\n13 7 3"
},
{
"input": "2\n25 37",
"output": "0"
},
{
"input": "12\n1 1 1 2 2 2 3 3 3 4 4 4",
"output": "4\n4 3 2\n4 3 1\n4 2 1\n3 2 1"
},
{
"input": "12\n1 1 1 2 2 2 3 3 3 4 4 5",
"output": "4\n5 3 2\n4 3 1\n4 2 1\n3 2 1"
},
{
"input": "12\n4 4 4 3 3 3 2 2 2 1 1 1",
"output": "4\n4 3 2\n4 3 1\n4 2 1\n3 2 1"
},
{
"input": "40\n1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4",
"output": "13\n4 3 2\n4 3 2\n4 3 2\n4 3 2\n4 3 1\n4 3 1\n4 3 1\n4 2 1\n4 2 1\n4 2 1\n3 2 1\n3 2 1\n3 2 1"
},
{
"input": "12\n2 2 2 3 3 3 4 4 4 5 5 5",
"output": "4\n5 4 3\n5 4 2\n5 3 2\n4 3 2"
},
{
"input": "20\n1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4",
"output": "6\n4 3 2\n4 3 2\n4 3 2\n4 3 1\n4 2 1\n3 2 1"
},
{
"input": "12\n1 1 1 2 2 2 3 3 3 3 4 4",
"output": "4\n4 3 2\n4 3 1\n3 2 1\n3 2 1"
},
{
"input": "6\n1 2 2 3 4 5",
"output": "2\n5 3 2\n4 2 1"
},
{
"input": "14\n1 1 1 1 1 2 3 4 6 5 5 5 5 5",
"output": "4\n6 5 1\n5 4 1\n5 3 1\n5 2 1"
},
{
"input": "6\n1 1 2 3 4 5",
"output": "2\n5 3 1\n4 2 1"
}
] | 122 | 0 | 0 | 4,547 |
|
989 | A Mist of Florescence | [
"constructive algorithms",
"graphs"
] | null | null | "I've been here once," Mino exclaims with delight, "it's breathtakingly amazing."
"What is it like?"
"Look, Kanno, you've got your paintbrush, and I've got my words. Have a try, shall we?"
There are four kinds of flowers in the wood, Amaranths, Begonias, Centaureas and Dianthuses.
The wood can be represented by a rectangular grid of $n$ rows and $m$ columns. In each cell of the grid, there is exactly one type of flowers.
According to Mino, the numbers of connected components formed by each kind of flowers are $a$, $b$, $c$ and $d$ respectively. Two cells are considered in the same connected component if and only if a path exists between them that moves between cells sharing common edges and passes only through cells containing the same flowers.
You are to help Kanno depict such a grid of flowers, with $n$ and $m$ arbitrarily chosen under the constraints given below. It can be shown that at least one solution exists under the constraints of this problem.
Note that you can choose arbitrary $n$ and $m$ under the constraints below, they are not given in the input. | The first and only line of input contains four space-separated integers $a$, $b$, $c$ and $d$ ($1 \leq a, b, c, d \leq 100$)Β β the required number of connected components of Amaranths, Begonias, Centaureas and Dianthuses, respectively. | In the first line, output two space-separated integers $n$ and $m$ ($1 \leq n, m \leq 50$)Β β the number of rows and the number of columns in the grid respectively.
Then output $n$ lines each consisting of $m$ consecutive English letters, representing one row of the grid. Each letter should be among 'A', 'B', 'C' and 'D', representing Amaranths, Begonias, Centaureas and Dianthuses, respectively.
In case there are multiple solutions, print any. You can output each letter in either case (upper or lower). | [
"5 3 2 1\n",
"50 50 1 1\n",
"1 6 4 5\n"
] | [
"4 7\nDDDDDDD\nDABACAD\nDBABACD\nDDDDDDD",
"4 50\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nABABABABABABABABABABABABABABABABABABABABABABABABAB\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD",
"7 7\nDDDDDDD\nDDDBDBD\nDDCDCDD\nDBDADBD\nDDCDCDD\nDBDBDDD\nDDDDDDD"
] | In the first example, each cell of Amaranths, Begonias and Centaureas forms a connected component, while all the Dianthuses form one. | [
{
"input": "5 3 2 1",
"output": "5 13\nAABABBBBCDDAD\nABAABBBBCDADD\nAAAABBBBCDDAD\nAAAABCBBCDADD\nAAAABBBBCDDDD"
},
{
"input": "50 50 1 1",
"output": "10 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\nDADADADADADADADADADADADADADADADADADADADADADADADADD\nADADADADADADADADADADADADADADADADADADADADADADADADAD\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD..."
},
{
"input": "1 6 4 5",
"output": "6 13\nAABABBCBCCDCD\nABAABBBBCCCCD\nAABABBCBCCDCD\nABAABCBBCDCCD\nAABABBBBCCDCD\nAAAABBBBCCCCD"
},
{
"input": "1 1 1 1",
"output": "2 4\nABCD\nABCD"
},
{
"input": "4 8 16 32",
"output": "16 32\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABAAAAAAABAAAAAAAAAAAAAAABABAAAA\nBAAAAAAAAAAABAAAAAAAAAAABAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBBBBBCBCBCBCBCBBBCBCBBBBBBBBBB\nCBCBBBBBBBBBCBBBCBBBCBBBBBCBBBCB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCC\nDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDC\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\nADDDDDDDDDDDDDDDADDDDDDDDDDD..."
},
{
"input": "1 1 1 50",
"output": "7 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCC\nDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDC\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"
},
{
"input": "19 58 20 18",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABAAABABABABABAAABABAAAAABABAAABABAAAAABAA\nAAAABABABAAABABABABABAAABAAABAAABAAAAAAABAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABAAABABABAAABABABABAAAAAAABABAAABAAABAAABAAABABA\nABABAAABAAABABAAABAAAAABAAABABAAABAAAAABAAABAAAAAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBBBBBCBBBCBCBBBBBBBBBCBBBCBBBBBBBBBBBCBCBB\nBBBBCBCBCBCBBBBBCBBBBBCBCBCBBBCBBBBB..."
},
{
"input": "100 100 100 100",
"output": "40 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAABAAABAAABAAABABABABABABAAABABABABABABAAAAABABAA\nAABAAAAAAAAAAABAAABAAABABABAAABABAAABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAABABABAAAAAAABABABABABAAABABABABABABAAAAAAABABA\nABABABAAABABABAAABABAAABAAABABABABABAAABABABABABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABAAABAAABABAAAAAAABAAAAAAABABAAABAAABAAAAABAA\nBABABABAAABABABABABAAABABABABAAABABAAABABABAAABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..."
},
{
"input": "1 1 1 2",
"output": "2 7\nABCCDCD\nABCCCCD"
},
{
"input": "1 1 3 1",
"output": "3 7\nABBCBCD\nABCBBCD\nABBBBCD"
},
{
"input": "1 4 1 1",
"output": "4 7\nAABABCD\nABAABCD\nAABABCD\nAAAABCD"
},
{
"input": "5 1 1 1",
"output": "5 7\nABCDDAD\nABCDADD\nABCDDAD\nABCDADD\nABCDDDD"
},
{
"input": "1 4 7 3",
"output": "7 13\nAAAABBCBCCDCD\nABAABCBBCCCCD\nAAAABBCBCCCCD\nABAABCBBCCCCD\nAABABBCBCCCCD\nAAAABCBBCDCCD\nAAAABBBBCCCCD"
},
{
"input": "6 2 5 1",
"output": "6 13\nAAAABBCBCDDAD\nAAAABBBBCDADD\nAAAABBCBCDDAD\nAAAABCBBCDADD\nAABABBCBCDDAD\nAAAABBBBCDDDD"
},
{
"input": "1 5 6 3",
"output": "6 13\nAAAABBCBCCCCD\nABAABCBBCCCCD\nAABABBCBCCCCD\nABAABCBBCDCCD\nAABABBCBCCDCD\nAAAABBBBCCCCD"
},
{
"input": "4 1 4 5",
"output": "5 13\nABBCBCCDCDDAD\nABCBBCDCCDDDD\nABBBBCCDCDDAD\nABCBBCDCCDADD\nABBBBCCCCDDDD"
},
{
"input": "4 5 3 6",
"output": "6 16\nAAAABBCBCCDCDDAD\nABAABBBBCDCCDDDD\nAABABBCBCCDCDDAD\nABAABBBBCDCCDDDD\nAABABBBBCCDCDDAD\nAAAABBBBCCCCDDDD"
},
{
"input": "2 5 1 17",
"output": "13 17\nAAAAAAAAAAAAAAAAA\nABAAAAAAABAAAAAAA\nAABAAAAAAAAABAAAA\nAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDC\nDCDCDCDCDCDCDCDCC\nCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDD\nDDDDDDDDDDDDDDDDD\nDDADDDDDDDDDDDDDD\nDDDDDDDDDDDDDDDDD"
},
{
"input": "11 4 5 14",
"output": "14 16\nAAAABBBBCCDCDDDD\nABAABBBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABBBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABBBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABCBBCDCCDDDD\nAAAABBCBCCDCDDDD\nABAABCBBCDCCDADD\nAAAABBBBCCDCDDAD\nAAAABCBBCDCCDADD\nAABABBBBCCDCDDAD\nAAAABBBBCCCCDDDD"
},
{
"input": "19 19 8 10",
"output": "16 19\nAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABA\nBABABABABABABABABAA\nAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBBBBBCBBBCB\nBBCBBBCBCBBBBBCBBBB\nBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCC\nCCCDCCCCCDCCCDCCCDC\nCCCCDCDCCCDCCCDCDCC\nCCCCCCCCCCCCCCCCCCC\nDDDDDDDDDDDDDDDDDDD\nDADADADADADADADADAD\nADADADADADADADADADD\nDDDDDDDDDDDDDDDDDDD"
},
{
"input": "49 49 49 49",
"output": "16 49\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABA\nBABABABABABABABABABABABABABABABABABABABABABABABAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCB\nCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDC..."
},
{
"input": "49 50 50 50",
"output": "16 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBB\nCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCDCD..."
},
{
"input": "50 50 51 50",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBCBCBBBBBCBBBCBBBCBBBBBBBCBCBBBCBBBBBCBBBB\nCBCBCBCBCBBBBBBBBBBBBBBBBBBBCBCBBBCBCBCBBBCBCBCBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBCBCBBBBBCBBBCBBBCBCBCBCBCBCBCBCBCBCBBBCBBBBBCBCB\nBBBBBCBBBCBBBBBCBBBBBCBBBCBBBCBBBCBB..."
},
{
"input": "15 63 41 45",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABAAABABABABABAAABABAAAAABABAAABABAAAAABAA\nAAAABABABAAABABABABABAAABAAABAAABABABABABAAABAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABAAABABABAAABABABABAAAAAAABABAAABAAABAAABAAABABA\nABABAAABAAABABAAABAAAAABAAABABAAABABAAABAAABAAAAAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBCBCBCBCBCBCBCBCBCBBBCBBBCBBBCBCBCBCBCBCBCBCBB\nCBBBCBCBCBCBCBCBCBBBCBCBCBCBCBCBBBBB..."
},
{
"input": "45 36 25 13",
"output": "16 45\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABAAABABABABAAABABABABABAAABABABABABAAAAABAAA\nBABABABABABABAAAAABABABABABABABABABABAAABABAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBBBCBBBBBBBBBBBBBBBCBCBCBBBCBCBBBCB\nCBBBCBCBCBCBBBBBCBBBBBBBBBCBBBCBBBBBCBCBCBCBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\nCCCCCCCCCCCCCCCCCCCDCCCCCCCCCDCCCCCDCCCCCDCCC\nCCCCCCCCCCCCDCCCDCCCCCDCDCCCCCDCCC..."
},
{
"input": "31 41 59 26",
"output": "19 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABAAAAABAAABABABABABABABABAAABABABABABAA\nBABABABABABABAAABABAAABAAABAAABABABABAAABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBCBBBBBCBCBCBBBCBBBCBBBCBBBBBBBCBCBBBCBBBBBCBBBB\nCBCBCBCBCBCBBBBBBBBBBBBBCBBBCBCBBBCBCBCBBBCBCBCBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBCBCBBBBBCBBBCBBBCBCBCBCBCBCBCBCBCBCBBBCBBBBBCBCB\nBCBBBCBBBCBBBCBCBBBBBCBBBCBBBCBCBCBC..."
},
{
"input": "18 90 64 16",
"output": "22 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABAAABAA\nAABABABABAAABABABABABABABAAABABABABABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABABABABABABABABABABABABABABABAAABABABABABABABABA\nABABABABABABABAAABABAAABABABABABABABABABABABAAABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBBBBCBCBCBBBCBBBBBCBCBCBBBCBCBCBBBCBBBCBCBCBCBCBB\nCBCBCBBBCBCBBBCBBBCBCBBBCBCBBBCBCBBB..."
},
{
"input": "77 88 99 1",
"output": "22 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABAAAAABAA\nAABABABABAAABABABABABABABAAABABABABABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAABABABABABABABABABABABABAAABABAAABABABABABABABABA\nABABABABABABABAAABABAAABABABABABABABABABABABAAABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBB\nCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCBCB..."
},
{
"input": "99 100 1 100",
"output": "28 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAABAAABAAABAAABABABABABABAAABABABABABABAAAAABABAA\nAABAAAAAAAAAAABAAABAAABABABAAABABAAABABABABABABAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nAAAABABABAAAAAAABABABABABAAABABABABABABAAAAAAABABA\nABABABAAABABABAAABABAAABAAABABABABABAAABABABABABAB\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABAAABAAABABAAAAAAABAAAAAAABABAAABAAABAAAAABAA\nBABABABAAABABABABABAAABABABABAAABABAAABABABAAABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA..."
},
{
"input": "100 50 100 49",
"output": "28 50\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nABABABABABABABABABABABABABABABABABABABABABABABABAA\nBABABABABABABABABABABABABABABABABABABABABABABABABA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBCBCBBBBBCBCBCBBBCBBBBBCBCBBBCBCBCBBBCBCBCBCBBBBBB\nBBBBCBCBCBCBBBCBCBCBCBCBBBCBCBCBCBBBCBCBCBCBCBCBBB\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\nBBCBCBBBBBCBCBCBCBCBCBCBCBCBCBBBCBCBCBBBBBCBCBCBCB\nBCBBBBBCBCBCBBBCBCBBBCBCBCBBBBBCBCBB..."
}
] | 77 | 0 | -1 | 4,550 |
|
864 | Make a Permutation! | [
"greedy",
"implementation",
"math"
] | null | null | Ivan has an array consisting of *n* elements. Each of the elements is an integer from 1 to *n*.
Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to *n* was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them.
Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority.
In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal β compare the second, and so on. If we have two permutations *x* and *y*, then *x* is lexicographically smaller if *x**i*<=<<=*y**i*, where *i* is the first index in which the permutations *x* and *y* differ.
Determine the array Ivan will obtain after performing all the changes. | The first line contains an single integer *n* (2<=β€<=*n*<=β€<=200<=000) β the number of elements in Ivan's array.
The second line contains a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*) β the description of Ivan's array. | In the first line print *q* β the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with *q* changes. | [
"4\n3 2 2 3\n",
"6\n4 5 6 3 2 1\n",
"10\n6 8 4 6 7 1 6 3 4 5\n"
] | [
"2\n1 2 4 3 \n",
"0\n4 5 6 3 2 1 \n",
"3\n2 8 4 6 7 1 9 3 10 5 \n"
] | In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers β this permutation is lexicographically minimal among all suitable.
In the second example Ivan does not need to change anything because his array already is a permutation. | [
{
"input": "4\n3 2 2 3",
"output": "2\n1 2 4 3 "
},
{
"input": "6\n4 5 6 3 2 1",
"output": "0\n4 5 6 3 2 1 "
},
{
"input": "10\n6 8 4 6 7 1 6 3 4 5",
"output": "3\n2 8 4 6 7 1 9 3 10 5 "
},
{
"input": "6\n5 5 5 6 4 6",
"output": "3\n1 2 5 3 4 6 "
},
{
"input": "50\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "49\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 "
},
{
"input": "50\n1 1 2 1 1 1 1 1 1 1 1 1 2 1 2 1 1 2 1 1 1 1 1 1 1 1 1 2 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
"output": "48\n1 3 2 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 "
},
{
"input": "50\n2 4 1 2 3 7 2 2 1 1 3 4 2 12 4 3 2 1 2 5 1 3 3 7 9 6 10 5 7 1 4 3 6 2 3 12 1 3 2 6 2 2 2 4 1 6 1 3 7 13",
"output": "39\n2 4 1 8 3 7 11 14 15 16 17 18 19 12 20 21 22 23 24 5 25 26 27 28 9 6 10 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 13 "
},
{
"input": "50\n26 46 50 31 47 40 25 47 41 47 31 30 50 40 46 44 26 48 37 19 28 19 50 22 42 38 47 22 44 44 35 30 50 45 49 34 19 37 36 32 50 29 50 42 34 49 40 50 8 50",
"output": "25\n1 2 3 4 5 6 25 7 41 9 31 10 11 12 46 13 26 48 14 15 28 16 17 18 20 38 47 22 21 44 35 30 23 45 24 27 19 37 36 32 33 29 39 42 34 49 40 43 8 50 "
},
{
"input": "20\n15 18 20 6 19 13 20 17 20 16 19 17 17 19 16 12 14 19 20 20",
"output": "10\n15 18 1 6 2 13 3 4 5 7 8 9 17 10 16 12 14 19 11 20 "
},
{
"input": "50\n48 37 47 50 46 43 42 46 36 40 45 41 40 50 35 49 37 42 44 45 49 44 31 47 45 49 48 41 45 45 48 20 34 43 43 41 47 50 41 48 38 48 43 48 46 48 32 37 47 45",
"output": "31\n1 2 3 4 5 6 7 8 36 9 10 11 40 12 35 13 14 42 15 16 17 44 31 18 19 49 21 22 23 24 25 20 34 26 27 28 29 50 41 30 38 33 43 39 46 48 32 37 47 45 "
},
{
"input": "26\n26 26 23 25 22 26 26 24 26 26 25 18 25 22 24 24 24 24 24 26 26 25 24 26 26 23",
"output": "20\n1 2 3 4 5 6 7 8 9 10 11 18 12 22 13 14 15 16 17 19 20 25 24 21 26 23 "
},
{
"input": "50\n50 50 50 49 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 49 50 49 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 49",
"output": "48\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 50 49 "
},
{
"input": "50\n50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50",
"output": "49\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 "
},
{
"input": "50\n4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4",
"output": "49\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 "
},
{
"input": "50\n18 42 38 38 38 50 50 38 49 49 38 38 42 18 49 49 49 49 18 50 18 38 38 49 49 50 49 42 38 49 42 38 38 49 38 49 50 49 49 49 18 49 18 38 42 50 42 49 18 49",
"output": "45\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 18 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 39 40 41 43 44 45 38 42 50 46 47 48 49 "
},
{
"input": "50\n4 50 27 48 32 32 37 33 18 24 38 6 32 17 1 46 36 16 10 9 9 25 26 40 28 2 1 5 15 50 2 4 18 39 42 46 25 3 10 42 37 23 28 41 33 45 25 11 13 18",
"output": "17\n4 7 27 48 8 12 14 19 18 24 38 6 32 17 1 20 36 16 10 9 21 22 26 40 28 2 29 5 15 50 30 31 34 39 35 46 25 3 43 42 37 23 44 41 33 45 47 11 13 49 "
},
{
"input": "50\n39 49 43 21 22 27 28 41 35 6 31 9 4 39 27 27 7 41 9 28 43 37 20 47 28 37 8 46 23 14 50 48 21 47 9 31 9 37 34 17 15 17 18 16 29 6 43 33 16 17",
"output": "20\n1 49 2 3 22 5 10 11 35 6 12 9 4 39 13 27 7 41 19 24 25 26 20 30 28 32 8 46 23 14 50 48 21 47 36 31 38 37 34 17 15 40 18 16 29 42 43 33 44 45 "
},
{
"input": "2\n1 2",
"output": "0\n1 2 "
},
{
"input": "2\n2 1",
"output": "0\n2 1 "
},
{
"input": "2\n1 1",
"output": "1\n1 2 "
},
{
"input": "2\n2 2",
"output": "1\n1 2 "
},
{
"input": "3\n1 1 1",
"output": "2\n1 2 3 "
},
{
"input": "3\n2 2 2",
"output": "2\n1 2 3 "
},
{
"input": "3\n3 3 3",
"output": "2\n1 2 3 "
},
{
"input": "3\n1 2 2",
"output": "1\n1 2 3 "
},
{
"input": "3\n2 1 3",
"output": "0\n2 1 3 "
},
{
"input": "3\n3 2 1",
"output": "0\n3 2 1 "
},
{
"input": "3\n2 2 3",
"output": "1\n1 2 3 "
},
{
"input": "3\n3 1 3",
"output": "1\n2 1 3 "
},
{
"input": "3\n2 2 1",
"output": "1\n2 3 1 "
},
{
"input": "3\n3 1 2",
"output": "0\n3 1 2 "
}
] | 124 | 0 | 0 | 4,562 |
|
835 | Palindromic characteristics | [
"brute force",
"dp",
"hashing",
"strings"
] | null | null | Palindromic characteristics of string *s* with length |*s*| is a sequence of |*s*| integers, where *k*-th number is the total number of non-empty substrings of *s* which are *k*-palindromes.
A string is 1-palindrome if and only if it reads the same backward as forward.
A string is *k*-palindrome (*k*<=><=1) if and only if:
1. Its left half equals to its right half. 1. Its left and right halfs are non-empty (*k*<=-<=1)-palindromes.
The left half of string *t* is its prefix of length β|*t*|<=/<=2β, and right halfΒ β the suffix of the same length. β|*t*|<=/<=2β denotes the length of string *t* divided by 2, rounded down.
Note that each substring is counted as many times as it appears in the string. For example, in the string "aaa" the substring "a" appears 3 times. | The first line contains the string *s* (1<=β€<=|*s*|<=β€<=5000) consisting of lowercase English letters. | Print |*s*| integersΒ β palindromic characteristics of string *s*. | [
"abba\n",
"abacaba\n"
] | [
"6 1 0 0 \n",
"12 4 1 0 0 0 0 \n"
] | In the first example 1-palindromes are substring Β«aΒ», Β«bΒ», Β«bΒ», Β«aΒ», Β«bbΒ», Β«abbaΒ», the substring Β«bbΒ» is 2-palindrome. There are no 3- and 4-palindromes here. | [
{
"input": "abba",
"output": "6 1 0 0 "
},
{
"input": "abacaba",
"output": "12 4 1 0 0 0 0 "
},
{
"input": "qqqpvmgd",
"output": "11 3 0 0 0 0 0 0 "
},
{
"input": "wyemcafatp",
"output": "11 1 0 0 0 0 0 0 0 0 "
}
] | 3,000 | 1,024,000 | 0 | 4,572 |
|
588 | Duff in Love | [
"math"
] | null | null | Duff is in love with lovely numbers! A positive integer *x* is called lovely if and only if there is no such positive integer *a*<=><=1 such that *a*2 is a divisor of *x*.
Malek has a number store! In his store, he has only divisors of positive integer *n* (and he has all of them). As a birthday present, Malek wants to give her a lovely number from his store. He wants this number to be as big as possible.
Malek always had issues in math, so he asked for your help. Please tell him what is the biggest lovely number in his store. | The first and only line of input contains one integer, *n* (1<=β€<=*n*<=β€<=1012). | Print the answer in one line. | [
"10\n",
"12\n"
] | [
"10\n",
"6\n"
] | In first sample case, there are numbers 1, 2, 5 and 10 in the shop. 10 isn't divisible by any perfect square, so 10 is lovely.
In second sample case, there are numbers 1, 2, 3, 4, 6 and 12 in the shop. 12 is divisible by 4β=β2<sup class="upper-index">2</sup>, so 12 is not lovely, while 6 is indeed lovely. | [
{
"input": "10",
"output": "10"
},
{
"input": "12",
"output": "6"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "2"
},
{
"input": "4",
"output": "2"
},
{
"input": "8",
"output": "2"
},
{
"input": "3",
"output": "3"
},
{
"input": "31",
"output": "31"
},
{
"input": "97",
"output": "97"
},
{
"input": "1000000000000",
"output": "10"
},
{
"input": "15",
"output": "15"
},
{
"input": "894",
"output": "894"
},
{
"input": "271",
"output": "271"
},
{
"input": "2457",
"output": "273"
},
{
"input": "2829",
"output": "2829"
},
{
"input": "5000",
"output": "10"
},
{
"input": "20",
"output": "10"
},
{
"input": "68",
"output": "34"
},
{
"input": "3096",
"output": "258"
},
{
"input": "1024",
"output": "2"
},
{
"input": "1048576",
"output": "2"
},
{
"input": "413933789280",
"output": "25870861830"
},
{
"input": "817634153013",
"output": "817634153013"
},
{
"input": "56517269141",
"output": "56517269141"
},
{
"input": "30707328551",
"output": "30707328551"
},
{
"input": "279564127218",
"output": "10354226934"
},
{
"input": "491159577042",
"output": "18191095446"
},
{
"input": "734337660466",
"output": "734337660466"
},
{
"input": "808453785117",
"output": "808453785117"
},
{
"input": "55926835837",
"output": "55926835837"
},
{
"input": "294809951965",
"output": "294809951965"
},
{
"input": "537988035389",
"output": "76855433627"
},
{
"input": "822722434952",
"output": "205680608738"
},
{
"input": "699511759613",
"output": "699511759613"
},
{
"input": "942689843037",
"output": "104743315893"
},
{
"input": "663634158717",
"output": "663634158717"
},
{
"input": "213612977250",
"output": "11730"
},
{
"input": "999999999989",
"output": "999999999989"
},
{
"input": "999999999988",
"output": "499999999994"
},
{
"input": "87178291200",
"output": "30030"
},
{
"input": "927668721948",
"output": "463834360974"
},
{
"input": "562436815639",
"output": "37927"
},
{
"input": "302981118597",
"output": "35853"
},
{
"input": "5",
"output": "5"
},
{
"input": "9",
"output": "3"
},
{
"input": "36",
"output": "6"
},
{
"input": "2231",
"output": "2231"
},
{
"input": "27648",
"output": "6"
},
{
"input": "40320",
"output": "210"
},
{
"input": "648000",
"output": "30"
},
{
"input": "999966000289",
"output": "999983"
},
{
"input": "999985999949",
"output": "999985999949"
},
{
"input": "991921850317",
"output": "9973"
}
] | 202 | 22,528,000 | 3 | 4,578 |
|
75 | Big Maximum Sum | [
"data structures",
"dp",
"greedy",
"implementation",
"math",
"trees"
] | D. Big Maximum Sum | 2 | 256 | Ahmed and Mostafa used to compete together in many programming contests for several years. Their coach Fegla asked them to solve one challenging problem, of course Ahmed was able to solve it but Mostafa couldn't.
This problem is similar to a standard problem but it has a different format and constraints.
In the standard problem you are given an array of integers, and you have to find one or more consecutive elements in this array where their sum is the maximum possible sum.
But in this problem you are given *n* small arrays, and you will create one big array from the concatenation of one or more instances of the small arrays (each small array could occur more than once). The big array will be given as an array of indexes (1-based) of the small arrays, and the concatenation should be done in the same order as in this array. Then you should apply the standard problem mentioned above on the resulting big array.
For example let's suppose that the small arrays are {1, 6, -2}, {3, 3} and {-5, 1}. And the indexes in the big array are {2, 3, 1, 3}. So the actual values in the big array after formatting it as concatenation of the small arrays will be {3, 3, -5, 1, 1, 6, -2, -5, 1}. In this example the maximum sum is 9.
Can you help Mostafa solve this problem? | The first line contains two integers *n* and *m*, *n* is the number of the small arrays (1<=β€<=*n*<=β€<=50), and *m* is the number of indexes in the big array (1<=β€<=*m*<=β€<=250000). Then follow *n* lines, the *i*-th line starts with one integer *l* which is the size of the *i*-th array (1<=β€<=*l*<=β€<=5000), followed by *l* integers each one will be greater than or equal -1000 and less than or equal 1000. The last line contains *m* integers which are the indexes in the big array, and you should concatenate the small arrays in the same order, and each index will be greater than or equal to 1 and less than or equal to *n*.
The small arrays are numbered from 1 to *n* in the same order as given in the input. Some of the given small arrays may not be used in big array.
Note, that the array is very big. So if you try to build it straightforwardly, you will probably get time or/and memory limit exceeded. | Print one line containing the maximum sum in the big array after formatting it as described above. You must choose at least one element for the sum, i. e. it cannot be empty.
Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d). | [
"3 4\n3 1 6 -2\n2 3 3\n2 -5 1\n2 3 1 3\n",
"6 1\n4 0 8 -3 -10\n8 3 -2 -5 10 8 -9 -5 -4\n1 0\n1 -3\n3 -8 5 6\n2 9 6\n1\n"
] | [
"9\n",
"8\n"
] | none | [
{
"input": "3 4\n3 1 6 -2\n2 3 3\n2 -5 1\n2 3 1 3",
"output": "9"
},
{
"input": "6 1\n4 0 8 -3 -10\n8 3 -2 -5 10 8 -9 -5 -4\n1 0\n1 -3\n3 -8 5 6\n2 9 6\n1",
"output": "8"
},
{
"input": "4 3\n6 6 8 -5 4 10 -2\n1 -2\n1 -10\n5 -10 10 8 -7 -10\n2 4 1",
"output": "24"
},
{
"input": "7 7\n2 -8 -7\n5 2 -10 10 -2 4\n7 10 -8 9 8 9 -10 -3\n6 0 6 -9 9 -6 -9\n4 -6 -9 10 -6\n3 -8 4 10\n7 -1 -3 10 -8 -6 -3 6\n4 5 4 6 6 1 7",
"output": "20"
},
{
"input": "4 8\n8 0 3 -9 -10 0 -1 6 -4\n3 -10 -7 2\n10 6 -2 -9 0 -7 -4 -7 7 -1 2\n3 -5 1 -4\n1 1 1 1 4 4 3 3",
"output": "14"
},
{
"input": "2 1\n2 -4 -6\n5 6 8 3 5 -2\n1",
"output": "-4"
},
{
"input": "9 4\n4 8 -2 -10 6\n10 -4 9 6 -2 -8 6 7 2 -6 2\n8 -10 1 9 9 -10 2 -10 -9\n7 3 -10 -10 -6 3 -7 0\n5 -4 -8 2 -5 2\n1 -3\n4 -9 0 7 -4\n7 4 -5 4 -8 -4 0 -1\n9 2 5 -10 4 -10 -2 6 5 10\n3 6 4 6",
"output": "19"
},
{
"input": "3 1\n7 4 8 1 -7 -9 -8 -9\n10 5 -5 -5 -9 -1 7 4 -1 -4 4\n8 -7 7 4 10 -6 3 -6 9\n2",
"output": "11"
},
{
"input": "7 3\n7 -9 -6 0 -6 -5 1 -9\n9 4 4 3 -6 -4 8 4 5 -6\n1 -4\n7 -3 -9 -9 1 -4 8 7\n2 6 3\n7 0 -5 -5 -2 -8 2 -1\n8 4 1 6 -7 -2 10 -8 -2\n3 1 5",
"output": "9"
},
{
"input": "6 9\n8 -10 10 3 4 -9 0 3 9\n4 9 2 -1 6\n3 -10 -10 -5\n7 10 -6 7 1 -8 3 4\n8 -8 9 3 -1 0 1 -7 -7\n1 -4\n3 2 3 2 4 4 1 1 1",
"output": "68"
},
{
"input": "3 6\n3 -1 -1 -1\n4 -2 -2 -2 -2\n5 -3 -3 -3 -3 -3\n1 2 3 1 2 3",
"output": "-1"
},
{
"input": "2 2\n11 -1 -1 -1 -1 10 -1 -1 -1 -1 -1 -1\n10 -1 -1 -1 10 -1 -1 -1 -1 -1 -1\n1 2",
"output": "11"
},
{
"input": "1 1\n1 1\n1",
"output": "1"
},
{
"input": "1 1\n1 -1\n1",
"output": "-1"
},
{
"input": "1 1\n1 0\n1",
"output": "0"
},
{
"input": "2 2\n6 -1 -1 -1 1 1 1\n6 1 1 1 -1 -1 -1\n1 2",
"output": "6"
},
{
"input": "2 2\n6 -1 -1 -1 1 1 1\n6 -1 -1 -1 -1 -1 -1\n1 2",
"output": "3"
},
{
"input": "2 2\n6 -1 -1 -1 -1 -1 -1\n6 1 1 1 -1 -1 -1\n1 2",
"output": "3"
},
{
"input": "2 2\n6 -1 -1 -1 -1 -1 -1\n6 -1 -1 -1 -1 -1 -1\n1 2",
"output": "-1"
},
{
"input": "2 2\n6 -1 -1 0 -1 -1 -1\n6 -1 -1 -1 -1 -1 -1\n1 2",
"output": "0"
},
{
"input": "2 2\n6 -1 -1 0 1 0 -1\n6 -1 -1 -1 -1 -1 -1\n1 2",
"output": "1"
},
{
"input": "2 2\n6 0 0 0 0 0 0\n6 0 0 0 0 0 0\n1 2",
"output": "0"
},
{
"input": "14 14\n6 -1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 1\n6 1 1 1 1 1 -1\n1 2 3 4 5 6 7 8 9 10 11 12 13 14",
"output": "82"
}
] | 2,000 | 10,854,400 | 0 | 4,596 |
35 | Fire Again | [
"brute force",
"dfs and similar",
"shortest paths"
] | C. Fire Again | 2 | 64 | After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it *N* rows with *M* trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the *j*-th tree in the *i*-th row would have the coordinates of (*i*,<=*j*). However a terrible thing happened and the young forest caught fire. Now we must find the coordinates of the tree that will catch fire last to plan evacuation.
The burning began in *K* points simultaneously, which means that initially *K* trees started to burn. Every minute the fire gets from the burning trees to the ones that arenβt burning and that the distance from them to the nearest burning tree equals to 1.
Find the tree that will be the last to start burning. If there are several such trees, output any. | The first input line contains two integers *N*,<=*M* (1<=β€<=*N*,<=*M*<=β€<=2000) β the size of the forest. The trees were planted in all points of the (*x*,<=*y*) (1<=β€<=*x*<=β€<=*N*,<=1<=β€<=*y*<=β€<=*M*) type, *x* and *y* are integers.
The second line contains an integer *K* (1<=β€<=*K*<=β€<=10) β amount of trees, burning in the beginning.
The third line contains *K* pairs of integers: *x*1,<=*y*1,<=*x*2,<=*y*2,<=...,<=*x**k*,<=*y**k* (1<=β€<=*x**i*<=β€<=*N*,<=1<=β€<=*y**i*<=β€<=*M*) β coordinates of the points from which the fire started. It is guaranteed that no two points coincide. | Output a line with two space-separated integers *x* and *y* β coordinates of the tree that will be the last one to start burning. If there are several such trees, output any. | [
"3 3\n1\n2 2\n",
"3 3\n1\n1 1\n",
"3 3\n2\n1 1 3 3\n"
] | [
"1 1\n",
"3 3\n",
"2 2"
] | none | [
{
"input": "3 3\n1\n2 2",
"output": "1 1"
},
{
"input": "3 3\n1\n1 1",
"output": "3 3"
},
{
"input": "3 3\n2\n1 1 3 3",
"output": "1 3"
},
{
"input": "1 1\n1\n1 1",
"output": "1 1"
},
{
"input": "2 2\n1\n2 2",
"output": "1 1"
},
{
"input": "2 2\n2\n1 1 2 1",
"output": "1 2"
},
{
"input": "2 2\n3\n1 2 2 1 1 1",
"output": "2 2"
},
{
"input": "2 2\n4\n2 1 2 2 1 1 1 2",
"output": "1 1"
},
{
"input": "10 10\n1\n5 5",
"output": "10 10"
},
{
"input": "10 10\n2\n7 8 1 9",
"output": "3 1"
},
{
"input": "10 10\n3\n3 9 6 3 3 5",
"output": "10 7"
},
{
"input": "10 10\n4\n5 3 4 7 7 5 8 5",
"output": "10 10"
},
{
"input": "10 10\n5\n2 7 10 6 5 3 9 5 2 9",
"output": "1 1"
},
{
"input": "10 10\n6\n5 1 4 6 3 9 9 9 5 7 7 2",
"output": "1 3"
},
{
"input": "10 10\n7\n5 8 4 6 4 1 6 2 1 10 3 2 7 10",
"output": "10 5"
},
{
"input": "10 10\n8\n9 4 9 10 5 8 6 5 1 3 2 5 10 6 2 1",
"output": "1 10"
},
{
"input": "10 10\n9\n10 1 10 4 8 4 6 6 1 9 10 10 7 7 6 5 7 10",
"output": "1 1"
},
{
"input": "10 10\n10\n7 2 1 9 5 8 6 10 9 4 10 8 6 8 8 7 4 1 9 5",
"output": "1 3"
},
{
"input": "100 100\n1\n44 3",
"output": "100 100"
},
{
"input": "100 100\n2\n79 84 76 63",
"output": "1 1"
},
{
"input": "100 100\n3\n89 93 99 32 32 82",
"output": "1 1"
},
{
"input": "100 100\n4\n72 12 1 66 57 67 25 67",
"output": "100 100"
},
{
"input": "100 100\n5\n22 41 82 16 6 3 20 6 69 78",
"output": "1 100"
},
{
"input": "100 100\n6\n92 32 90 80 32 40 24 19 36 37 39 13",
"output": "1 100"
},
{
"input": "100 100\n7\n30 32 29 63 86 78 88 2 86 50 41 60 54 28",
"output": "1 100"
},
{
"input": "100 100\n8\n40 43 96 8 17 63 61 59 16 69 4 95 30 62 12 91",
"output": "100 100"
},
{
"input": "100 100\n9\n18 16 41 71 25 1 43 38 78 92 77 70 99 8 33 54 76 78",
"output": "1 100"
},
{
"input": "100 100\n10\n58 98 33 62 75 13 94 86 81 42 14 53 12 66 7 14 3 63 87 37",
"output": "40 1"
},
{
"input": "2000 2000\n1\n407 594",
"output": "2000 2000"
},
{
"input": "2000 2000\n2\n1884 43 1235 1111",
"output": "1 2000"
},
{
"input": "2000 2000\n3\n1740 1797 1279 1552 329 756",
"output": "2000 1"
},
{
"input": "2000 2000\n4\n1844 1342 171 1810 1558 1141 1917 1999",
"output": "530 1"
},
{
"input": "2000 2000\n5\n1846 327 1911 1534 134 1615 1664 682 1982 1112",
"output": "346 1"
},
{
"input": "2000 2000\n6\n1744 1102 852 723 409 179 89 1085 997 1433 1082 1680",
"output": "2000 1"
},
{
"input": "2000 2000\n7\n1890 22 288 1729 383 831 1192 1206 721 1376 969 492 510 1699",
"output": "2000 2000"
},
{
"input": "2000 2000\n8\n286 381 572 1849 1703 1574 622 1047 1507 941 871 663 1930 120 1084 1830",
"output": "1 1423"
},
{
"input": "2000 2000\n9\n226 531 56 138 722 405 1082 608 1355 1426 83 544 275 1268 683 412 1880 1049",
"output": "1701 1"
},
{
"input": "2000 2000\n10\n763 851 1182 571 1758 389 247 1907 730 881 531 1970 1430 667 169 765 1729 120 129 967",
"output": "2000 1793"
},
{
"input": "2000 2000\n10\n655 95 1640 1656 1344 79 666 1677 968 1180 522 1394 1850 1568 336 130 412 920 29 1664",
"output": "2000 570"
},
{
"input": "10 1\n10\n4 1 6 1 10 1 9 1 1 1 7 1 5 1 2 1 8 1 3 1",
"output": "1 1"
},
{
"input": "1 10\n10\n1 10 1 4 1 3 1 7 1 6 1 1 1 8 1 2 1 9 1 5",
"output": "1 1"
},
{
"input": "1 100\n10\n1 68 1 18 1 43 1 12 1 64 1 34 1 23 1 70 1 46 1 33",
"output": "1 100"
},
{
"input": "100 1\n10\n62 1 63 1 57 1 76 1 35 1 69 1 73 1 95 1 96 1 21 1",
"output": "1 1"
},
{
"input": "1 2000\n10\n1 1730 1 1374 1 831 1 1076 1 580 1 914 1 123 1 668 1 1288 1 160",
"output": "1 2000"
},
{
"input": "2000 1\n10\n1058 1 1779 1 1995 1 1398 1 96 1 1599 1 1496 1 1659 1 385 1 1485 1",
"output": "721 1"
},
{
"input": "5 5\n1\n1 1",
"output": "5 5"
},
{
"input": "2 5\n10\n1 1 1 2 1 3 1 4 1 5 2 1 2 2 2 3 2 4 2 5",
"output": "1 1"
},
{
"input": "1 10\n9\n1 1 1 2 1 3 1 4 1 6 1 7 1 8 1 9 1 10",
"output": "1 5"
},
{
"input": "3 3\n5\n1 1 1 3 2 2 3 1 3 3",
"output": "1 2"
},
{
"input": "999 999\n4\n1 499 499 1 499 999 999 499",
"output": "500 500"
}
] | 998 | 4,505,600 | 3.716931 | 4,600 |
328 | Sheldon and Ice Pieces | [
"greedy"
] | null | null | Do you remember how Kai constructed the word "eternity" using pieces of ice as components?
Little Sheldon plays with pieces of ice, each piece has exactly one digit between 0 and 9. He wants to construct his favourite number *t*. He realized that digits 6 and 9 are very similar, so he can rotate piece of ice with 6 to use as 9 (and vice versa). Similary, 2 and 5 work the same. There is no other pair of digits with similar effect. He called this effect "Digital Mimicry".
Sheldon favourite number is *t*. He wants to have as many instances of *t* as possible. How many instances he can construct using the given sequence of ice pieces. He can use any piece at most once. | The first line contains integer *t* (1<=β€<=*t*<=β€<=10000). The second line contains the sequence of digits on the pieces. The length of line is equal to the number of pieces and between 1 and 200, inclusive. It contains digits between 0 and 9. | Print the required number of instances. | [
"42\n23454\n",
"169\n12118999\n"
] | [
"2\n",
"1\n"
] | This problem contains very weak pretests. | [
{
"input": "42\n23454",
"output": "2"
},
{
"input": "169\n12118999",
"output": "1"
},
{
"input": "1\n1",
"output": "1"
},
{
"input": "7\n777",
"output": "3"
},
{
"input": "18\n8118",
"output": "2"
},
{
"input": "33\n33333333",
"output": "4"
},
{
"input": "1780\n8170880870810081711018110878070777078711",
"output": "10"
},
{
"input": "2\n5255",
"output": "4"
},
{
"input": "5\n22252",
"output": "5"
},
{
"input": "9\n666969",
"output": "6"
},
{
"input": "6\n9669969",
"output": "7"
},
{
"input": "25\n52",
"output": "1"
},
{
"input": "2591\n5291",
"output": "1"
},
{
"input": "9697\n979966799976",
"output": "3"
},
{
"input": "5518\n22882121",
"output": "2"
},
{
"input": "533\n355233333332",
"output": "4"
},
{
"input": "2569\n9592525295556669222269569596622566529699",
"output": "10"
},
{
"input": "2559\n5252555622565626",
"output": "4"
},
{
"input": "555\n225225252222255",
"output": "5"
},
{
"input": "266\n565596629695965699",
"output": "6"
},
{
"input": "22\n25552222222255",
"output": "7"
},
{
"input": "99\n966969969696699969",
"output": "9"
},
{
"input": "2591\n95195222396509125191259289255559161259521226176117",
"output": "10"
},
{
"input": "9697\n76694996266995167659667796999669903799299696697971977966766996767667996967697669766777697969669669297966667776967677699767966667666769699790768276666766",
"output": "34"
},
{
"input": "5518\n9827108589585181118358352282425981568508825302611217254345831149357236227288533838583629451589201341265988858338548185158221291825821019993179835186961954871454",
"output": "23"
},
{
"input": "100\n11111000000000001010110010101100011011110101000000000001100110007111110010100000011000010011000011000010010000111101000010000000801000100111000410010100100000001011000000000101100010110001001100010001",
"output": "63"
},
{
"input": "2569\n09629965966225566262579565696595696954525955599926383255926955906666526913925296256629966292216925259225261263256229509529259756291959568892569599592218262625256926619266669279659295979299556965525222",
"output": "44"
},
{
"input": "2559\n52555995269265555695922255525995255259555259252562655622526292929555265526255252526255555296956995596262965522222225655655262255226222259622295225295525265995566255556225522559559295225625559595222652",
"output": "48"
},
{
"input": "555\n25225222525252252255252525552255255522522522225252252525225555225552525255255252252225225255225552522252552252252522555255522225555252255555222225252525522252252255522522225252255522525552525225522552",
"output": "66"
},
{
"input": "266\n26266956652996996666662666992669966292555295699956956255562529696222966929669665256625596629565696225696662556996969659952659665522965269529566599526566699292225569566599656596562966965669929996226599",
"output": "62"
},
{
"input": "22\n35354953025524221524235223225255512262254275553262592555522123522225045753552560550228255220622552552252517202252456715552032250226729355222227262525262552362252277292927052612301558753527582221622055",
"output": "66"
},
{
"input": "9\n99669996666966699999666999999666999699966669696969999696666669696967969666969696696696699669696999669669966696699666669996696666996699999696666696996666666969996996696696969666999999996666699966996696",
"output": "199"
},
{
"input": "2569\n2569256925692569256925692569256925692569",
"output": "10"
},
{
"input": "52\n222222222222222",
"output": "7"
},
{
"input": "11\n1",
"output": "0"
},
{
"input": "5\n2",
"output": "1"
}
] | 342 | 409,600 | 0 | 4,602 |
|
84 | Toy Army | [
"math",
"number theory"
] | A. Toy Army | 2 | 256 | The hero of our story, Valera, and his best friend Arcady are still in school, and therefore they spend all the free time playing turn-based strategy "GAGA: Go And Go Again". The gameplay is as follows.
There are two armies on the playing field each of which consists of *n* men (*n* is always even). The current player specifies for each of her soldiers an enemy's soldier he will shoot (a target) and then all the player's soldiers shot simultaneously. This is a game world, and so each soldier shoots perfectly, that is he absolutely always hits the specified target. If an enemy soldier is hit, he will surely die. It may happen that several soldiers had been indicated the same target. Killed soldiers do not participate in the game anymore.
The game "GAGA" consists of three steps: first Valera makes a move, then Arcady, then Valera again and the game ends.
You are asked to calculate the maximum total number of soldiers that may be killed during the game. | The input data consist of a single integer *n* (2<=β€<=*n*<=β€<=108, *n* is even). Please note that before the game starts there are 2*n* soldiers on the fields. | Print a single number β a maximum total number of soldiers that could be killed in the course of the game in three turns. | [
"2\n",
"4\n"
] | [
"3\n",
"6\n"
] | The first sample test:
1) Valera's soldiers 1 and 2 shoot at Arcady's soldier 1.
2) Arcady's soldier 2 shoots at Valera's soldier 1.
3) Valera's soldier 1 shoots at Arcady's soldier 2.
There are 3 soldiers killed in total: Valera's soldier 1 and Arcady's soldiers 1 and 2. | [
{
"input": "2",
"output": "3"
},
{
"input": "4",
"output": "6"
},
{
"input": "6",
"output": "9"
},
{
"input": "8",
"output": "12"
},
{
"input": "10",
"output": "15"
},
{
"input": "140",
"output": "210"
},
{
"input": "500",
"output": "750"
},
{
"input": "1000",
"output": "1500"
},
{
"input": "2000",
"output": "3000"
},
{
"input": "50000",
"output": "75000"
},
{
"input": "10000",
"output": "15000"
},
{
"input": "25460",
"output": "38190"
},
{
"input": "54646",
"output": "81969"
},
{
"input": "59790",
"output": "89685"
},
{
"input": "578456",
"output": "867684"
},
{
"input": "56798056",
"output": "85197084"
},
{
"input": "8457980",
"output": "12686970"
},
{
"input": "5687986",
"output": "8531979"
},
{
"input": "10984932",
"output": "16477398"
},
{
"input": "99999994",
"output": "149999991"
},
{
"input": "99999996",
"output": "149999994"
},
{
"input": "99999998",
"output": "149999997"
},
{
"input": "100000000",
"output": "150000000"
}
] | 0 | 0 | -1 | 4,605 |
168 | Wizards and Minimal Spell | [
"implementation",
"strings"
] | null | null | Let's dive into one of the most interesting areas of magic β writing spells. Learning this exciting but challenging science is very troublesome, so now you will not learn the magic words, but only get to know the basic rules of writing spells.
Each spell consists of several lines. The line, whose first non-space character is character "#" is an amplifying line and it is responsible for spell power. The remaining lines are common, and determine the effect of the spell.
You came across the text of some spell. Spell was too long, so you cannot understand its meaning. So you want to make it as short as possible without changing the meaning.
The only way to shorten a spell that you know is the removal of some spaces and line breaks. We know that when it comes to texts of spells, the spaces carry meaning only in the amplifying lines, so we should remove all spaces in other lines. Newlines also do not matter, unless any of the two separated lines is amplifying. Thus, if two consecutive lines are not amplifying, they need to be joined into one (i.e. we should concatenate the second line to the first one). Removing spaces in amplifying lines and concatenating the amplifying lines to anything is forbidden.
Note that empty lines must be processed just like all the others: they must be joined to the adjacent non-amplifying lines, or preserved in the output, if they are surrounded with amplifying lines on both sides (i.e. the line above it, if there is one, is amplifying, and the line below it, if there is one, is amplifying too).
For now those are the only instructions for removing unnecessary characters that you have to follow (oh yes, a newline is a character, too).
The input contains the text of the spell, which should be reduced. Remove the extra characters and print the result to the output. | The input contains multiple lines. All characters in the lines have codes from 32 to 127 (inclusive). Please note that the lines may begin with or end with one or more spaces. The size of the input does not exceed 1048576 (<==<=220) bytes. Newlines are included in this size.
In the Windows operating system used on the testing computer, a newline is a sequence of characters with codes #13#10. It is guaranteed that after each line of input there is a newline. In particular, the input ends with a newline. Note that the newline is the end of the line, and not the beginning of the next one.
It is guaranteed that the input contains at least one character other than a newline.
It is recommended to organize the input-output line by line, in this case the newlines will be processed correctly by the language means. | Print the text of the spell where all extra characters are deleted. Please note that each output line should be followed by a newline.
Please be careful: your answers will be validated by comparing them to the jury's answer byte-by-byte. So, all spaces and newlines matter. | [
"# include <cstdio>\n\nusing namespace std;\n\nint main ( ){\nputs(\"Hello # World\"); #\n#\n}\n",
"#\n\n#\n"
] | [
"# include <cstdio>\nusingnamespacestd;intmain(){puts(\"Hello#World\");#\n#\n}\n",
"#\n\n#\n"
] | In the first sample the amplifying lines are lines 1 and 7. So, lines 2 to 6 are concatenated to each other, all spaces are deleted from them.
In the second sample the amplifying lines are lines 1 and 3. So, no lines are concatenated to each other. | [
{
"input": " # include <cstdio>\n\nusing namespace std;\n\nint main ( ){\nputs(\"Hello # World\"); #\n#\n}",
"output": " # include <cstdio>\nusingnamespacestd;intmain(){puts(\"Hello#World\");#\n#\n}"
},
{
"input": "#\n\n#",
"output": "#\n\n#"
},
{
"input": "#\n \n#",
"output": "#\n\n#"
},
{
"input": "#a\n#a\n\n#a\n\n\n#a\n \n#a\n \n\n#a\n\n \n#a\n \n \n#a",
"output": "#a\n#a\n\n#a\n\n#a\n\n#a\n\n#a\n\n#a\n\n#a"
},
{
"input": " # a \n # a \n\n # a \n\n\n # a \n \n # a \n \n\n # a \n\n \n # a\n \n \n # a \n\n\n \n\n \n ",
"output": " # a \n # a \n\n # a \n\n # a \n\n # a \n\n # a \n\n # a\n\n # a "
},
{
"input": "fdg",
"output": "fdg"
},
{
"input": "abc\n\n.\n\nabc\n\n#\nabc\nabc\n#",
"output": "abc.abc\n#\nabcabc\n#"
},
{
"input": "#\n\n#",
"output": "#\n\n#"
}
] | 186 | 0 | 0 | 4,611 |
|
911 | Nearest Minimums | [
"implementation"
] | null | null | You are given an array of *n* integer numbers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1. Find the distance between two closest (nearest) minimums in it. It is guaranteed that in the array a minimum occurs at least two times. | The first line contains positive integer *n* (2<=β€<=*n*<=β€<=105) β size of the given array. The second line contains *n* integers *a*0,<=*a*1,<=...,<=*a**n*<=-<=1 (1<=β€<=*a**i*<=β€<=109) β elements of the array. It is guaranteed that in the array a minimum occurs at least two times. | Print the only number β distance between two nearest minimums in the array. | [
"2\n3 3\n",
"3\n5 6 5\n",
"9\n2 1 3 5 4 1 2 3 1\n"
] | [
"1\n",
"2\n",
"3\n"
] | none | [
{
"input": "2\n3 3",
"output": "1"
},
{
"input": "3\n5 6 5",
"output": "2"
},
{
"input": "9\n2 1 3 5 4 1 2 3 1",
"output": "3"
},
{
"input": "6\n4 6 7 8 6 4",
"output": "5"
},
{
"input": "2\n1000000000 1000000000",
"output": "1"
},
{
"input": "42\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",
"output": "1"
},
{
"input": "2\n10000000 10000000",
"output": "1"
},
{
"input": "5\n100000000 100000001 100000000 100000001 100000000",
"output": "2"
},
{
"input": "9\n4 3 4 3 4 1 3 3 1",
"output": "3"
},
{
"input": "3\n10000000 1000000000 10000000",
"output": "2"
},
{
"input": "12\n5 6 6 5 6 1 9 9 9 9 9 1",
"output": "6"
},
{
"input": "5\n5 5 1 2 1",
"output": "2"
},
{
"input": "5\n2 2 1 3 1",
"output": "2"
},
{
"input": "3\n1000000000 1000000000 1000000000",
"output": "1"
},
{
"input": "3\n100000005 1000000000 100000005",
"output": "2"
},
{
"input": "5\n1 2 2 2 1",
"output": "4"
},
{
"input": "3\n10000 1000000 10000",
"output": "2"
},
{
"input": "3\n999999999 999999998 999999998",
"output": "1"
},
{
"input": "6\n2 1 1 2 3 4",
"output": "1"
},
{
"input": "4\n1000000000 900000000 900000000 1000000000",
"output": "1"
},
{
"input": "5\n7 7 2 7 2",
"output": "2"
},
{
"input": "6\n10 10 1 20 20 1",
"output": "3"
},
{
"input": "2\n999999999 999999999",
"output": "1"
},
{
"input": "10\n100000 100000 1 2 3 4 5 6 7 1",
"output": "7"
},
{
"input": "10\n3 3 1 2 2 1 10 10 10 10",
"output": "3"
},
{
"input": "5\n900000000 900000001 900000000 900000001 900000001",
"output": "2"
},
{
"input": "5\n3 3 2 5 2",
"output": "2"
},
{
"input": "2\n100000000 100000000",
"output": "1"
},
{
"input": "10\n10 15 10 2 54 54 54 54 2 10",
"output": "5"
},
{
"input": "2\n999999 999999",
"output": "1"
},
{
"input": "6\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000",
"output": "1"
},
{
"input": "5\n1000000000 100000000 1000000000 1000000000 100000000",
"output": "3"
},
{
"input": "4\n10 9 10 9",
"output": "2"
},
{
"input": "5\n1 3 2 3 1",
"output": "4"
},
{
"input": "5\n2 2 1 4 1",
"output": "2"
},
{
"input": "6\n1 2 2 2 2 1",
"output": "5"
},
{
"input": "7\n3 7 6 7 6 7 3",
"output": "6"
},
{
"input": "8\n1 2 2 2 2 1 2 2",
"output": "5"
},
{
"input": "10\n2 2 2 3 3 1 3 3 3 1",
"output": "4"
},
{
"input": "2\n88888888 88888888",
"output": "1"
},
{
"input": "3\n100000000 100000000 100000000",
"output": "1"
},
{
"input": "10\n1 3 2 4 5 5 4 3 2 1",
"output": "9"
},
{
"input": "5\n2 2 1 2 1",
"output": "2"
},
{
"input": "6\n900000005 900000000 900000001 900000000 900000001 900000001",
"output": "2"
},
{
"input": "5\n41 41 1 41 1",
"output": "2"
},
{
"input": "6\n5 5 1 3 3 1",
"output": "3"
},
{
"input": "8\n1 2 2 2 1 2 2 2",
"output": "4"
},
{
"input": "7\n6 6 6 6 1 8 1",
"output": "2"
},
{
"input": "3\n999999999 1000000000 999999999",
"output": "2"
},
{
"input": "5\n5 5 4 10 4",
"output": "2"
},
{
"input": "11\n2 2 3 4 1 5 3 4 2 5 1",
"output": "6"
},
{
"input": "5\n3 5 4 5 3",
"output": "4"
},
{
"input": "6\n6 6 6 6 1 1",
"output": "1"
},
{
"input": "7\n11 1 3 2 3 1 11",
"output": "4"
},
{
"input": "5\n3 3 1 2 1",
"output": "2"
},
{
"input": "5\n4 4 2 5 2",
"output": "2"
},
{
"input": "4\n10000099 10000567 10000099 10000234",
"output": "2"
},
{
"input": "4\n100000009 100000011 100000012 100000009",
"output": "3"
},
{
"input": "2\n1000000 1000000",
"output": "1"
},
{
"input": "2\n10000010 10000010",
"output": "1"
},
{
"input": "10\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000",
"output": "1"
},
{
"input": "8\n2 6 2 8 1 9 8 1",
"output": "3"
},
{
"input": "5\n7 7 1 8 1",
"output": "2"
},
{
"input": "7\n1 3 2 3 2 3 1",
"output": "6"
},
{
"input": "7\n2 3 2 1 3 4 1",
"output": "3"
},
{
"input": "5\n1000000000 999999999 1000000000 1000000000 999999999",
"output": "3"
},
{
"input": "4\n1000000000 1000000000 1000000000 1000000000",
"output": "1"
},
{
"input": "5\n5 5 3 5 3",
"output": "2"
},
{
"input": "6\n2 3 3 3 3 2",
"output": "5"
},
{
"input": "4\n1 1 2 2",
"output": "1"
},
{
"input": "5\n1 1 2 2 2",
"output": "1"
},
{
"input": "6\n2 1 1 2 2 2",
"output": "1"
},
{
"input": "5\n1000000000 1000000000 100000000 1000000000 100000000",
"output": "2"
},
{
"input": "7\n2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "8\n2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "10\n2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "11\n2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "12\n2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "13\n2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "14\n2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "15\n2 2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "16\n2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "17\n2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "18\n2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "19\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "20\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "4\n1000000000 100000000 100000000 1000000000",
"output": "1"
},
{
"input": "21\n2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 2 2 2",
"output": "1"
},
{
"input": "4\n1 2 3 1",
"output": "3"
},
{
"input": "8\n5 5 5 5 3 5 5 3",
"output": "3"
},
{
"input": "7\n2 3 2 1 4 4 1",
"output": "3"
},
{
"input": "6\n3 3 1 2 4 1",
"output": "3"
},
{
"input": "3\n2 1 1",
"output": "1"
},
{
"input": "5\n3 3 2 8 2",
"output": "2"
},
{
"input": "5\n1 2 1 2 2",
"output": "2"
},
{
"input": "4\n1 2 1 2",
"output": "2"
},
{
"input": "5\n3 1 1 3 2",
"output": "1"
},
{
"input": "4\n1 1 2 1",
"output": "1"
},
{
"input": "4\n2 2 1 1",
"output": "1"
},
{
"input": "5\n1 2 2 1 2",
"output": "3"
},
{
"input": "7\n2 1 2 1 1 2 1",
"output": "1"
},
{
"input": "9\n200000 500000 500000 500000 200000 500000 500000 500000 500000",
"output": "4"
},
{
"input": "3\n1 1 2",
"output": "1"
},
{
"input": "85\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 1",
"output": "84"
},
{
"input": "5\n1000000000 1000000000 999999999 1000000000 999999999",
"output": "2"
},
{
"input": "5\n2 1 2 2 1",
"output": "3"
},
{
"input": "3\n1 1 1",
"output": "1"
},
{
"input": "4\n1 2 1 1",
"output": "1"
},
{
"input": "6\n1 3 4 2 4 1",
"output": "5"
},
{
"input": "9\n2 2 5 1 6 8 7 9 1",
"output": "5"
},
{
"input": "10\n1000000000 1000000000 1000000000 999999999 1000000000 1000000000 1000000000 1000000000 1000000000 999999999",
"output": "6"
},
{
"input": "7\n3 3 1 2 4 1 2",
"output": "3"
},
{
"input": "7\n3 3 1 2 3 4 1",
"output": "4"
},
{
"input": "8\n10 5 10 1 10 10 10 1",
"output": "4"
}
] | 2,000 | 1,126,400 | 0 | 4,612 |
|
598 | Tricky Sum | [
"math"
] | null | null | In this problem you are to calculate the sum of all integers from 1 to *n*, but you should take all powers of two with minus in the sum.
For example, for *n*<==<=4 the sum is equal to <=-<=1<=-<=2<=+<=3<=-<=4<==<=<=-<=4, because 1, 2 and 4 are 20, 21 and 22 respectively.
Calculate the answer for *t* values of *n*. | The first line of the input contains a single integer *t* (1<=β€<=*t*<=β€<=100) β the number of values of *n* to be processed.
Each of next *t* lines contains a single integer *n* (1<=β€<=*n*<=β€<=109). | Print the requested sum for each of *t* integers *n* given in the input. | [
"2\n4\n1000000000\n"
] | [
"-4\n499999998352516354\n"
] | The answer for the first sample is explained in the statement. | [
{
"input": "2\n4\n1000000000",
"output": "-4\n499999998352516354"
},
{
"input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10",
"output": "-1\n-3\n0\n-4\n1\n7\n14\n6\n15\n25"
},
{
"input": "10\n10\n9\n47\n33\n99\n83\n62\n1\n100\n53",
"output": "25\n15\n1002\n435\n4696\n3232\n1827\n-1\n4796\n1305"
},
{
"input": "100\n901\n712\n3\n677\n652\n757\n963\n134\n205\n888\n847\n283\n591\n984\n1\n61\n540\n986\n950\n729\n104\n244\n500\n461\n251\n685\n631\n803\n526\n600\n1000\n899\n411\n219\n597\n342\n771\n348\n507\n775\n454\n102\n486\n333\n580\n431\n537\n355\n624\n23\n429\n276\n84\n704\n96\n536\n855\n653\n72\n718\n776\n658\n802\n777\n995\n285\n328\n405\n184\n555\n956\n410\n846\n853\n525\n983\n65\n549\n839\n929\n620\n725\n635\n303\n201\n878\n580\n139\n182\n69\n400\n788\n985\n792\n103\n248\n570\n839\n253\n417",
"output": "404305\n251782\n0\n227457\n210832\n284857\n462120\n8535\n20605\n392670\n357082\n39164\n172890\n482574\n-1\n1765\n144024\n484545\n449679\n264039\n5206\n29380\n124228\n105469\n31116\n232909\n197350\n320760\n136555\n178254\n498454\n402504\n83644\n23580\n176457\n57631\n295560\n59704\n127756\n298654\n102263\n4999\n117319\n54589\n166444\n92074\n142407\n62168\n192954\n214\n91213\n37204\n3316\n246114\n4402\n141870\n363894\n211485\n2374\n256075\n299430\n214765\n319957\n300207\n493464\n39733\n52934\n81193\n16510\n15..."
},
{
"input": "1\n16",
"output": "74"
},
{
"input": "60\n536870912\n536870911\n536870913\n1000000000\n999999999\n1\n2\n3\n4\n268435456\n268435455\n268435457\n536870912\n536870911\n536870913\n1000000000\n999999999\n1\n2\n3\n4\n268435456\n268435455\n268435457\n536870912\n536870911\n536870913\n1000000000\n999999999\n1\n2\n3\n4\n268435456\n268435455\n268435457\n536870912\n536870911\n536870913\n1000000000\n999999999\n1\n2\n3\n4\n268435456\n268435455\n268435457\n536870912\n536870911\n536870913\n1000000000\n999999999\n1\n2\n3\n4\n268435456\n268435455\n268435457",
"output": "144115186196807682\n144115186733678594\n144115186733678595\n499999998352516354\n499999997352516354\n-1\n-3\n0\n-4\n36028796079439874\n36028796347875330\n36028796347875331\n144115186196807682\n144115186733678594\n144115186733678595\n499999998352516354\n499999997352516354\n-1\n-3\n0\n-4\n36028796079439874\n36028796347875330\n36028796347875331\n144115186196807682\n144115186733678594\n144115186733678595\n499999998352516354\n499999997352516354\n-1\n-3\n0\n-4\n36028796079439874\n36028796347875330\n36028796347875..."
},
{
"input": "13\n1\n19\n31\n19\n19\n92\n74\n69\n32\n32\n91\n42\n73",
"output": "-1\n128\n434\n128\n128\n4024\n2521\n2161\n402\n402\n3932\n777\n2447"
},
{
"input": "1\n16383",
"output": "134176770"
},
{
"input": "16\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100\n100100",
"output": "5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908\n5009792908"
},
{
"input": "1\n414234",
"output": "85794061921"
},
{
"input": "1\n414232",
"output": "85793233454"
},
{
"input": "3\n414231\n414231\n414231",
"output": "85792819222\n85792819222\n85792819222"
},
{
"input": "1\n121",
"output": "7127"
}
] | 46 | 0 | 3 | 4,619 |
|
40 | Interesting Sequence | [
"math"
] | D. Interesting Sequence | 3 | 256 | Berland scientists noticed long ago that the world around them depends on Berland population. Due to persistent research in this area the scientists managed to find out that the Berland chronology starts from the moment when the first two people came to that land (it is considered to have happened in the first year). After one Berland year after the start of the chronology the population had already equaled 13 people (the second year). However, tracing the population number during the following years was an ultimately difficult task, still it was found out that if *d**i* β the number of people in Berland in the year of *i*, then either *d**i*<==<=12*d**i*<=-<=2, or *d**i*<==<=13*d**i*<=-<=1<=-<=12*d**i*<=-<=2. Of course no one knows how many people are living in Berland at the moment, but now we can tell if there could possibly be a year in which the country population equaled *A*. That's what we ask you to determine. Also, if possible, you have to find out in which years it could be (from the beginning of Berland chronology). Let's suppose that it could be in the years of *a*1,<=*a*2,<=...,<=*a**k*. Then you have to define how many residents could be in the country during those years apart from the *A* variant. Look at the examples for further explanation. | The first line contains integer *A* (1<=β€<=*A*<=<<=10300). It is guaranteed that the number doesn't contain leading zeros. | On the first output line print YES, if there could be a year in which the total population of the country equaled *A*, otherwise print NO.
If the answer is YES, then you also have to print number *k* β the number of years in which the population could equal *A*. On the next line you have to output precisely *k* space-separated numbers β *a*1,<=*a*2,<=...,<=*a**k*. Those numbers have to be output in the increasing order.
On the next line you should output number *p* β how many variants of the number of people could be in the years of *a*1,<=*a*2,<=...,<=*a**k*, apart from the *A* variant. On each of the next *p* lines you have to print one number β the sought number of residents. Those number also have to go in the increasing order.
If any number (or both of them) *k* or *p* exceeds 1000, then you have to print 1000 instead of it and only the first 1000 possible answers in the increasing order.
The numbers should have no leading zeros. | [
"2\n",
"3\n",
"13\n",
"1729\n"
] | [
"YES\n1\n1\n0\n",
"NO\n",
"YES\n1\n2\n0\n",
"YES\n1\n4\n1\n156\n"
] | none | [
{
"input": "2",
"output": "YES\n1\n1\n0"
},
{
"input": "3",
"output": "NO"
},
{
"input": "13",
"output": "YES\n1\n2\n0"
},
{
"input": "1729",
"output": "YES\n1\n4\n1\n156"
},
{
"input": "1",
"output": "NO"
},
{
"input": "156",
"output": "YES\n1\n4\n1\n1729"
},
{
"input": "144",
"output": "NO"
},
{
"input": "15407021574586369",
"output": "YES\n1\n16\n7\n465813504\n5162766336\n61917613056\n743008391424\n8916100449984\n106993205379216\n1283918464548876"
},
{
"input": "1283918464548876",
"output": "YES\n1\n16\n7\n465813504\n5162766336\n61917613056\n743008391424\n8916100449984\n106993205379216\n15407021574586369"
},
{
"input": "106993205379216",
"output": "YES\n1\n16\n7\n465813504\n5162766336\n61917613056\n743008391424\n8916100449984\n1283918464548876\n15407021574586369"
},
{
"input": "8916100449984",
"output": "YES\n1\n16\n7\n465813504\n5162766336\n61917613056\n743008391424\n106993205379216\n1283918464548876\n15407021574586369"
},
{
"input": "743008391424",
"output": "YES\n1\n16\n7\n465813504\n5162766336\n61917613056\n8916100449984\n106993205379216\n1283918464548876\n15407021574586369"
},
{
"input": "61917613056",
"output": "YES\n1\n16\n7\n465813504\n5162766336\n743008391424\n8916100449984\n106993205379216\n1283918464548876\n15407021574586369"
},
{
"input": "5162766336",
"output": "YES\n1\n16\n7\n465813504\n61917613056\n743008391424\n8916100449984\n106993205379216\n1283918464548876\n15407021574586369"
},
{
"input": "465813504",
"output": "YES\n1\n16\n7\n5162766336\n61917613056\n743008391424\n8916100449984\n106993205379216\n1283918464548876\n15407021574586369"
},
{
"input": "7490188681967468052233299463733525909596632872794112593117645292194261548194465757721637006269680454119156083258231473339217601881799799128545647497114042669233536690932763798631769749003803628850696853476076414076023997968562301922626395217146842374699798304766476599296",
"output": "YES\n1\n259\n129\n32765042513998444527869508809032841309988281709243511720096767253904423476479938795694875812513577161958628194814639614319455565409004027904\n197955465188740602355878282387906749581179201993346216642251302159005891837066296890656541367269528686833378677005114336513377374346066001920\n2359196828516617167272326403378173716129260409043689939373773133834083436221404481966404999041993227803944697485663825285217187707939293495296\n2830900621272038350218473242509835898698071740728457388412417406..."
},
{
"input": "4135044341492606010207581986888385137223529804451311687110817571616687277981789273096709608651872451226415087809479939549278089444816732696362652005022412283309968225450302039417562020389509383289818547215875107516092331433333695759644244910848459520005299103279531923910787448915935294419632128",
"output": "YES\n1\n386\n192\n20736840795910918325806150974947576883963049989783728765199147770760072734460490537657554823288437820242813040004241091258429579939305767078556126295442345091034120336925907510204750900090271526933941796732928\n229833318821346011444351506639002310463923804053436327147623887792590806140270436792371232624780185841024511193380338761447594510993972251787330399774485991425628167067595474904769322476000509423517854913789952\n2756415761628686719960107887579663674555116248989238557601922829723045..."
},
{
"input": "4992931021747500841206051466436702562341442456351521960079288222881342911294018652359351672377890893353133194676518844879524186195771978689872074221947407400894730355500961665649091805184",
"output": "YES\n1\n235\n117\n3674817562245760292710263796940459701960002416691229172368316120812071693424695751271795642761246455330619566192890989441449984\n22202022771901468435124510439848610699341681267509509583058576563239599814440870163933765341682530667622493212415383061208760320\n264599624264896983298377570684563586248418646232382011622922817351666426064402485396261202235902665778441173417159654333497737216\n3175043437095603746420271135331961389802383627373773130860592133102729715130422495040889095008808014155..."
},
{
"input": "52015199180329639251620135164816152149976617172181337452205870084682371862461567761955812543539447598049695022626607453744566679734720827281566996507736407425232893687033081934942845479193080755907617038028308431083499985892793763351572189007964183157637488227544976384",
"output": "YES\n1\n255\n127\n227535017458322531443538255618283620208251956314191053611783105929891829697777352747881082031344285846934918019546108432773996982006972416\n1374690730477365294138043627693796872091522236064904282237856264993096471090738172851781537272705060325231796368091071781342898432958791680\n16383311309143174772724488912348428584230975062803402356762313429403357195981975569211145826680508526416282621428221008925119359082911760384\n1965903209216693298762828640631830485206994264394762075286400976736308..."
},
{
"input": "6028801175788581229286833472784603286740565840936227676140782146528792478220288",
"output": "YES\n1\n90\n44\n3962033820342906963106922494542950070196390330368\n43912541508800552174435057647851029944676659494912\n526647842744330431811316690750323661761313245233152\n6319748891651858832212308288918559883004525053739008\n75836984598048963790754075133682274924543364487446528\n910043815001439786972732766243075595455227795836239872\n10920525780002681795463100183636814503492792501867118592\n131046309360030965241539727786035099655082681601724776448\n157255571232037148153980861056428730632875627685230726348..."
},
{
"input": "13375565248934352031245861515653274879560267436972842215671285886213101948076355115971323088601088",
"output": "YES\n1\n125\n62\n16228084133712034192265946373242384158728079174488353179969664319488\n98044674974510206578273426005006070958982145012533800462316721930240\n1168478405141619628628537951187247639928972839726961735739690725670912\n14021069387153393639349893484174236244849439668354920506392279546593280\n168252776689628553513516008315918106985335089819561661049833687128997888\n2019033315612524961315635206999836223161282895651348483845764773262131200\n242283997869617147290504094095987695895468332326342325187531..."
},
{
"input": "2473050726234742649340406291350904878243888779915646082783846184050924405749416227507330900520680043711803454128128",
"output": "YES\n1\n209\n104\n34346270393769874283167141433623986349724645253204710027571379708090188014458787361822409188887295779958499049472\n207508716962359657127468146161478250862919731738111789749910419069711552587355173644343722182860745337249265090560\n29675187558374114022402441207661955694584069692242611710554897638212291683551424242069237992442023116180375805624320\n356102132270785801788022424967897726431313620001349245419754500277680933437163626697495961508153765943467737530499072\n4273225577380287657582868..."
},
{
"input": "34673132095468755841708690739143767794037794835774463542579390106875921151401740696448531295736614860068382808881736044004097376353456147122347544457170902210430288830990776802283094016",
"output": "YES\n1\n295\n147\n872314646812554708216880139056555849441840439745184569896264827653056064982207362872974861141941749564885787102012414584779710034567962895627805927533461897216\n5270234324492518028810317506800024923711119323460490109789933333737213725934169484024223119399231403621184963741325004783044081458848109161084660812181332295680\n62809683440805371473241123068110411978734184718735737590065429621671609790055673902419721164932104377524432524772331393210336274259846689467478512219657636675584\n7536801..."
},
{
"input": "76926195972485303618404821696000923269687240287362491269272716553702593162409974951445967754952159774825431550496029309100898668843613086511855201303688576780554483501841048484822445695165614561660239872",
"output": "YES\n1\n329\n164\n1935326963990695409045990129169243583468509031739311510940568123967050181633010200027736247474237792265283308973117638973035112647963343872663487539244632324333894587417228214272\n11692600407443784762986190363730846650122242066758340378599265748967594847366103291834239828490186661602753325045919068795420472248111869230675237216269653626183946465645753794560\n139350261292621703810370476765911820105508582608257301397133893009391387557374071242969328346782184368767986035331737775638295593683..."
},
{
"input": "1863798630111329010373316372159740719136749351003104624770649913347282489560325303425390379544097126759377873515024106285379456606758585075022403002072687762930101587785796958970881627956797714833938460160094246680765681574571902459527478900767628234059678749526761763617374208",
"output": "YES\n1\n414\n206\n26624412794280189124305335197482062642093372214477836197999762964925561739807249441122744625173158685655045761131167111763094515640305366657648669552838975612860135250622425076914649400827539278231107972485812166304613793792\n295087241803272096127717465105426194283201542043796017861164039527924975949530347972443752929002508766010090519203768822040964215013384480455606087543965313042533165694398544602470697525838560333728113361717751509876136214528\n3539013092328590972418947368159751118279..."
},
{
"input": "14021064691527197542117777666481419913141060406803942267403159673503744",
"output": "YES\n1\n87\n43\n50795305389011627732140031981319872694825517056\n306888303391945250881679359887140897531237498880\n3657438360596993487088152233321632639140960927744\n43887158553821726051433493459415920158755373711360\n526645727498082196301066560401287402612486471417856\n6319748715381338145919787444722806861408789489254400\n75836984583359753733563031729999295506077053190406144\n910043815000215686134633512626102013837022269894819840\n10920525780002579787059925245835400038357942041372000256\n13104630936003095..."
},
{
"input": "40512852347369616970344822236316722709007536696056600079443565303130229643558033136030962355850152705311010160983856037719793645282200730121595303243709035798769989074926950080112430799274737018571059879001790290962595518054005823967153593043854703984640",
"output": "YES\n1\n309\n154\n31256610940175272294323271501687810338476933803585022433085541221617395533677977642650763613264717517593071065368185253018226292254312609427259581456640920276359446528\n188842024430225603444869765322697187461631475063326177199891811547271764682637781591015030163474335002124804353266119236985117182369805348623026637967205560003004989440\n2250584517591717435997853059480903204822903389878272952065607181641249761048195216582114184195383497091414981536597422193885272994714168686434312293980426..."
},
{
"input": "2048031753406168394177333476729389434007182478036756630615366381917350903062096110855595740397935283511926597010089777827930347356946566665972974580987613508681940167615502339362060402670146048382716002760342382538588684288",
"output": "YES\n1\n265\n132\n56617993464189312144158511222008749783659750793572788252327213814746843767357334238960745404023461335864509520639697253544019217026758960218112\n342067043846143760870957671966302863276277661044502262357810250130762181094450561027054503482641745570848078353864837573495116102870002051317760\n4076692119676714465046580025037484181471361986827496215237879975265296177790586944837947838344564297645216437255227090092855300359319099159871488\n4891796273558082269177521763056996432950267967978774367..."
},
{
"input": "98766963416578336910558134487335524402352550059642970226435492955119160062793986837007426479645348701733393608424326084483991011400831258996603882350219635781306984934497931701736363491217572092284684715557285242339328",
"output": "YES\n1\n329\n164\n1935326963990695409045990129169243583468509031739311510940568123967050181633010200027736247474237792265283308973117638973035112647963343872663487539244632324333894587417228214272\n11692600407443784762986190363730846650122242066758340378599265748967594847366103291834239828490186661602753325045919068795420472248111869230675237216269653626183946465645753794560\n139350261292621703810370476765911820105508582608257301397133893009391387557374071242969328346782184368767986035331737775638295593683..."
},
{
"input": "39939223824273992215667642551956428337968885602521915294176256205951300513174403629707995884994741403648",
"output": "YES\n1\n142\n70\n45355564401187243044403261079976760837747312460747083211707271702186884595712\n502690838779825277075469476969742432618366046439946838929755594699237970935808\n6028825404188368188284186252304410966634175748688610553856172830913605264211968\n72345616128496290331358447738375223414211257583547430686832332778840158971625472\n868147369481808473315630390586396205288418520052509510245367848246738315642929152\n10417768431776689428899175438513912257154179193050942484778029166869247821713768448\n1250..."
},
{
"input": "115401498620795412003982104026637074122468852806951919040595446712011709187653569091360552709743169308681114305062591872462002851529731956600757613645399853403397127153196192679416281614101575575403722809417949760993527914058899317885156079425746416688379391597880793416946344886987128832",
"output": "YES\n1\n506\n252\n1168469434386925016148394716342430041897074728155881564526352665582304801518336658204575372978070150842588348988284109840917945893560768664053121009549790656400431723785511152586556080844593366100708709142267920595859743683095719099929717653874907410453168535543564759728128\n1295053623112175226231137477279526629769257823706102067350040871020387821682823129510071038384027750517202086795348221740350723365363185269325542452251017977510478493862274860783432989602757647428285485966013611993744..."
},
{
"input": "534209694253370164016700150666673078261716946440017215951188941496790293880319124083086138235686633476558210745002559111791618570768074789507112237681946907298794953650407945917970009859230454257811456",
"output": "YES\n1\n320\n159\n50554692587527006811016813913001877943935300549388843963452019056173748475335030463044486274203260230695173885695025773713703350902463248988525065124622677582345979689959424\n560314509511757658822103020869104147211949581089059687261593211205925712268296587632076389539086134223538177233119868991993545472502301009622819471797901343204334608230383616\n6719912297346211370622728021588673234200455470943415710447465949682095274766582070091211887323087528303724467681170057879541637775167007283953..."
},
{
"input": "85744279465190678227664420080117554205467114025102399143929913165043627396230382366933370445005227148630943260817376026493830462727719769191775951976144741106715501122937462936245167630363119177321450137603932831007850239647233124822883817286839102893667116255151948973491911364790872384972863307776",
"output": "YES\n1\n384\n191\n1728070066325909860483845914578964740330254165815310730433262314230006061205040878138129568607369818353567753333686757604869131661608813923213010524620195424252843361410492292517062575007522627244495149727744\n19152776568445500953695958886583525871993650337786360595635323982715900511689203066030936052065015486752042599448361563453966209249497687648944199981207165952135680588966289575397443539666709118626487909482496\n229701313469057226663342323964971972879593020749103213133493569143587125..."
},
{
"input": "7145356622099223185638701673343129517122259502091866595327492763753635616352531863911114203750435595719245271734784224968827160956963456490209591311994898241795939631749464092038301783175625606912061409096913944545526665063618615066057687580710821747959600540098130165626376057023321908049493360640",
"output": "YES\n1\n447\n223\n9087571990128033998481133729935102137955600480107620074670067228379538560875004668910827810859935871714158231835219401522131788770840878072379399656230627661670881331513926269227658450225888596372016257904224302702761333083587238022931808256\n54904080773690205407490182951691242083481752900650204617798322838126378805286486541336251357278779224939705984004450550862879557157163638353958872923060042122594908044563304543250436470114743603080931558171355162162516387380006229721879674880\n654336..."
},
{
"input": "33076856211077600184916642047424073405065984968319646128859194474960066786290210137605158216593003587734997784425226405256522751509341875856995371912484035985206424562038124471661978051097836183269508308363378688",
"output": "YES\n1\n210\n104\n223250757559504182840586419318555911273210194145830615179213968102586222093982117851845659727767422569730243821568\n2474362562951171359816499480780661349944746318449622651569621479803663961541635139524622728649422266814510202355712\n29675296878100483081608782306781102067225807820453776424620378912858353313200775818070678978119418301438934701309952\n356102141380762999209622953392824321962367098178700175812593290383901438572967739328829414923626882208905950771806208\n42732255781394524240346..."
},
{
"input": "5833850738115444790123301858525860464609052423198474077217629208584932325696195827387610627649689452307781278299607661979317839675618367523485801890948530770658677574385684508313179053341358796468870615462459993169923271075382282788408147405323998383308800",
"output": "YES\n1\n465\n232\n46889875402048167789750951662403614246536500805621064906763385767511839865229205018554354578530269334850706204842425969583094496854398993196290860235774346989317116984869155178337534286442309941301187970158781254886608422590021004428670869675140186112\n283292997220707680396411999627021836072824692367293933811695455678717365852426446987099225578620377231389683320922990232897862585161993917227590613924470013060457581783584479202455936313922289228694677319709303414939925886481376901756553170..."
},
{
"input": "10567394001497256755879596476980152822546660777052728396454889126698475277509260148728316810560159137232388412918883181392600243116908025396945501205975320937464837260786475744804330519950654592948087094740219247266238669242815922176",
"output": "YES\n1\n223\n111\n1230688966265646531498582643758459423077954341580942554403612383995383663617988492661647096153645316026683185908863205376\n7435412504521614461137270139374025681095974147051527932855158153305442967692013809830784539261607117661210914866048532480\n88613878796703861540576764873677684223498399935291686634262881968445385529327178376126999419924107355712948702055889895424\n106331562295565004582083197208423132535284302507105635223115466563207629884501139156837045845148802343053691923497595437056..."
},
{
"input": "14222442731987280515120371366176315513938767208588587712606710985537159049042334103163859308318995024388379145903401234916182967756573397626716173564971582716660249742542070531726073243246683577569028961587234032565878784",
"output": "YES\n1\n284\n141\n1898886666600224784663856505549799671187776913186306324590393444755519260111356209100362425727204624060864899531638714340421152700313115817626865558880256\n21045993888152491363357742936510279688997860787814895097543527346040338466234197984195683551809851250007919303142329083939667775761803700312031093277589504\n252406872815242379189242203977282746570869707606243676104060561986565237206885202766597480491975164646868160013438171049930564471088148262952748844878659584\n3028870385962692923839..."
},
{
"input": "753679854992115473022478654349776292806196268492745384789319451445347628202744027684662296111280935141538598830051410780606493649899116303384971691975593045262336",
"output": "YES\n1\n292\n145\n39375313918622261134789728499080645981749742071831247946706398470450447377669082351905115259879315084526094556688060380562973022393692769594310684228940988416\n436409729264730060910586157531477159631059641296129664742662583047492458435832329400281694130329075520164214669959335884572950998196761529670276750204095954944\n5233908914696865974868126341672935032893554256923068867693799813353416758721971564568165355481597014117458166038653914891360184872483842380588200047403885133824\n6280665632..."
},
{
"input": "2889427674622396320142390894928647316169816252697059428341402948472568589293051819553950850210813089998113484982604588285599036749945994951924186567506019510491717107585005090584395776",
"output": "YES\n1\n300\n149\n816486509416551206890999810156936275077562651601492757422903878683260476823346091649104470028857477592733096727483620051353808592355613270307626348171320335794176\n9049392146033442543041914562572710382109652721916544728103851322072803618125419182444241209486503709986125155396276788902504711898608047079242858692232133721718784\n10853033525515421285486546782092998084208074107155675604049863292969644990885880236288547681126639568473961253097752757918724479351582495560387691618296696213497446..."
},
{
"input": "685881690392905117434431489495385586127448264303076182128024256632771944880513797413380560779272522395272914057841494739399255775297713137770656031977268970189240419718422549044382776867564731096274305671116280037388",
"output": "YES\n1\n202\n100\n10766336687861891533593095067445790474209596554100627661034624233342313951291575899491013682859154252012453888\n119326898290469297830656803664190844422489695141281956576467085252877312960148299552692068318355626293138030592\n1431100350988642123920176615874860246297540886403056347637942655905591884317167043695538145163937996790072082432\n17173135676155623032871477304823703798339462682228982244048673340273024622538953478435060519079228501586899632128\n206077622402558469523276840817411560650..."
},
{
"input": "14290713244198446371277403346686259034244519004183733190654985527507271232705063727822228407500871191438490543469562671082305077121286628198629325329357456851119250187156243822707527937981697951495003198202649774631153870411176444432024115883616627318528929869019876487046408294150929371326821957632",
"output": "YES\n1\n553\n276\n86339725850365613493134311886229481665227302316943388026873870895356430364259760022259296628651096781607547033461941137788926507607773378700052173864867968475512136547402306428857981291972758456948977655807675721729887967067524351776812366796850456716112284625328420442572050110495198285099549327360\n102898097411438605000409553194525331108725205065888220546740428779138292900210036987447760585536654825298603611086222607720055688980597503109366777553779716222798573309395495885932641267335580..."
},
{
"input": "1190892770349870530939783612223854919520376583681977765887915460625605936058755310651852367291739265953207545289130222590192089760107219016552443777446454737593270848929686985225627328165141495957916933183554147885929489200931370369335342990301385609877410822418323040587200691179244114277235163137",
"output": "NO"
},
{
"input": "595446385174935265469891806111927459760188291840988882943957730312802968029377655325926183645869632976603772644565111295096044880053609508276221888723227368796635424464843492612813664082570747978958466591777073942964744600465685184667671495150692804938705411209161520293600345589622057138617581568",
"output": "NO"
},
{
"input": "25",
"output": "NO"
},
{
"input": "941796563564014133460267652699405064136604147775680640408635568423120076418612383600961606320075481457728632621229496557902028935524874377670656752361237195740789199168688114539822313589449591752852405348364368488613997844015773837981050319855641810991084718329572826001220219",
"output": "NO"
},
{
"input": "79360359146807441660707083821018832188095237636414144034857851003419752010124705615779249215657075053438039921073878645370211154334804568157886814559909",
"output": "NO"
},
{
"input": "63730052926382178992698271572",
"output": "NO"
},
{
"input": "781127467969689863953686682245136076127159921705034542049372816247984349746396880068864077830521695515007722284098436125466526268962707778595903329840419133974864831578401355678018910046595664462",
"output": "NO"
},
{
"input": "6158324958633591462725987806787114657822761584945953440793358408",
"output": "NO"
},
{
"input": "46865942276811740149949176718949673344632458696505595472917789224885825949034661409971763949176343056701403524645790892802371117466746709730235969308113002256137529699677021858777002204698794034488631496662175642982367736619451227",
"output": "NO"
},
{
"input": "30237645054497458443810364460387991000047179363449854478913094584184671326397148735574822623728870964468880",
"output": "NO"
},
{
"input": "2340834982489248497640077401144544875656219324259480464300721974528452789353163588007890141857933775490305682107276886017882071992830194933217950703328428111517059826130590646975303231172522274173055168264136989194405810785131454927884294753122224538370897882934059",
"output": "NO"
},
{
"input": "188808426143782131983811729737047667239979348184409855460833141044812532916921011366813880911319644625405122800255947507577498497005580408229",
"output": "NO"
},
{
"input": "11",
"output": "NO"
},
{
"input": "837952166310387766556098005402621146120844433859027080340550200820",
"output": "NO"
},
{
"input": "6658370691480968202384509492140362150472696196949673577340706113760133821635599667476781507918250717914609488172442814676",
"output": "NO"
},
{
"input": "496620932866717074931903995027173085744596193421095444317407919730992986418713478580824584919587030125446806223296721174921873955469939680411818878465888018986191990428049489376",
"output": "NO"
},
{
"input": "1055050055824280186133547527395898666709023463559337207019374080060005629519967890329878081184599905695126755199503698703340223998620951421943134090897041663457029971964336512111472968057533187306110300592753045593222495258017559167383354672",
"output": "NO"
},
{
"input": "833488",
"output": "NO"
},
{
"input": "6623739799588591251984406341341227075747347067457011846886851179047097",
"output": "NO"
},
{
"input": "491137842784568289872893698937459777201151060689848471272003426250808340375567208957554901863756992593841404624991936090178731",
"output": "NO"
},
{
"input": "921020945402270233565256424740666649108666245414796768645533036514715926608741510409618545180420952947917462937925573726593991655435868735899832746218676826629010574075553051352459309199055",
"output": "NO"
},
{
"input": "73010581613999159726778758153209240813500342925961695523976131595080552126499402124287397930918281238199343324378719343080627189983992629778313739785259010389762036264197722427990331444297391895841265448905560880286941336214995793596526089977876",
"output": "NO"
},
{
"input": "20046142930690780976270827075270",
"output": "NO"
},
{
"input": "9685166910821197056344900917707673568669808490600751439157007968027004377622601634787545920946543261243701428886581331490848676434786296227674864970612484770201",
"output": "NO"
},
{
"input": "8135498415686025907059626116077260223347794805104214588176486213766836727225732896611278946787711775240855660997946707132990500568944980168321229607627861318462551364491230037357687242571268657488824365976425738641613571689437917277074234256494445914221354904615014917288299991097350709814",
"output": "NO"
},
{
"input": "4805043123239964766764344326469867688727869311599746349016084457204677169811854267718990063526979167327981002200329174783850464",
"output": "NO"
},
{
"input": "2376595620091080825479292544658464163405755746884100218035485700973409491416884420742631899446144679322008453313773241425622490028383089317622842863337164723765526589656211098933400307364163919083790470365474085981340438888606855706394352678991102",
"output": "NO"
},
{
"input": "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
"output": "NO"
},
{
"input": "145",
"output": "YES\n1\n3\n1\n24"
},
{
"input": "24",
"output": "YES\n1\n3\n1\n145"
},
{
"input": "171488558930381356455328840160235108410934228050204798287859826330087254792460764733866740890010454297261886521634752052987660925455439538383551903952289482213431002245874925872490335255780375417940038378431797295573846444934117333184289390603399527822347158428238517844556899529811152455921863491584",
"output": "YES\n1\n555\n277\n1036076710204387361917611742634753779982727627803320656322486450744277164371117120267111559543813161379290564401543293653467118091293280544400626086378415621706145638568827677146295775503673101483387731869692108660758655604810292221321748401562205480593347415503941045310864601325942379421194591928320\n1234777168937263260004914638334303973304702460790658646560885145349659514802520443849373127026439857903583243333034671292640668267767170037312401330645356594673582879712745950631191695208026..."
},
{
"input": "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999",
"output": "NO"
}
] | 404 | 1,126,400 | 0 | 4,622 |
171 | MYSTERIOUS LANGUAGE | [
"*special"
] | null | null | You are given a mysterious language (codenamed "Secret") available in "Custom Test" tab. Find out what this language is and write a program which outputs its name. Note that the program must be written in this language. | This program has only one test, and it's empty (it doesn't give your program anything to read). | Output the name of the mysterious language. | [] | [] | none | [
{
"input": "1",
"output": "INTERCAL"
}
] | 0 | 0 | -1 | 4,636 |
|
952 | Cheese Board | [] | null | null | Not to be confused with [chessboard](https://en.wikipedia.org/wiki/Chessboard). | The first line of input contains a single integer *N* (1<=β€<=*N*<=β€<=100) β the number of cheeses you have.
The next *N* lines describe the cheeses you have. Each line contains two space-separated strings: the name of the cheese and its type. The name is a string of lowercase English letters between 1 and 10 characters long. The type is either "soft" or "hard. All cheese names are distinct. | Output a single number. | [
"9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard\n",
"6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard\n"
] | [
"3\n",
"4\n"
] | none | [
{
"input": "9\nbrie soft\ncamembert soft\nfeta soft\ngoat soft\nmuenster soft\nasiago hard\ncheddar hard\ngouda hard\nswiss hard",
"output": "3"
},
{
"input": "6\nparmesan hard\nemmental hard\nedam hard\ncolby hard\ngruyere hard\nasiago hard",
"output": "4"
},
{
"input": "9\ngorgonzola soft\ncambozola soft\nmascarpone soft\nricotta soft\nmozzarella soft\nbryndza soft\njarlsberg soft\nhavarti soft\nstilton soft",
"output": "5"
},
{
"input": "1\nprovolone hard",
"output": "1"
},
{
"input": "4\nemmental hard\nfeta soft\ngoat soft\nroquefort hard",
"output": "2"
},
{
"input": "1\ncamembert soft",
"output": "1"
},
{
"input": "2\nmuenster soft\nasiago hard",
"output": "2"
},
{
"input": "32\nauhwslzn soft\nkpq hard\neukw soft\nsinenrsz soft\najuoe soft\ngapj soft\nuyuhqv hard\nifldxi hard\npgy soft\njnjhh hard\nbyswtu soft\nhdr hard\njamqcp hard\nmrknxch soft\nghktedrf hard\nutley hard\nreinr hard\nvbhk hard\neuft soft\nxspriqy soft\ntrooa soft\nuylbj soft\nkgt soft\nlhc hard\nrwxhlux soft\nsuoku soft\ndhhoae soft\nlisv soft\nwlco hard\nbhmptm soft\nualppum soft\nlpxizrhr soft",
"output": "7"
},
{
"input": "18\nbcvyeeap soft\nubb hard\nsrbb hard\nemcmg hard\nmelqan hard\nuenps soft\ncpyminr hard\ndpx soft\nglkj hard\nmsozshuy soft\nxnvrcozn soft\ntftctb soft\ncija hard\ngxl hard\npjoja soft\ndhzze hard\niyvl soft\nctrszg hard",
"output": "5"
},
{
"input": "31\npevkjopz soft\nsmqei hard\nxhfmuqua soft\ngtmbnvn hard\nkdvztv soft\ncziuxm hard\ngdswd hard\nnawkigiz soft\neehdplwt hard\nizhivjj soft\ntvnkqkc hard\nwefwgi hard\nuxczrz hard\njdqudhgp soft\nhmyzqb soft\nwwlc soft\ndsax soft\nslefe soft\nahfitc hard\nlztbmai soft\nzcatg soft\nhwlubzmy soft\njkbl soft\nbfdfh soft\nzshdiuce hard\neobyco soft\nckg hard\nahcwzw soft\nvtaujlke soft\niwfdcik hard\nitb soft",
"output": "7"
},
{
"input": "27\ndbilglfh hard\niecrbay hard\ncpunhmf hard\nszvvz soft\nqsbg hard\nvdzexx hard\naiuvj soft\nfuccez hard\ndvscmzd hard\ngps soft\ndev hard\nnwlfdh soft\nnrlglw soft\nypff hard\nwig hard\njvgtfo hard\nzyp soft\ncpgbws soft\nxjsyjgi hard\nizizf hard\nizrwozx hard\nwau hard\ngzq hard\nffqa hard\nnajmkxn soft\nvqtw hard\nmymaoi hard",
"output": "7"
},
{
"input": "17\nqojmshqd soft\ncwbg hard\nxomz soft\nymxfk soft\nusledpbg hard\nhaaw hard\nimwjce soft\naioff soft\nsumpqbzx soft\nzffbvrq hard\nqosengs soft\nkbori soft\nkxsnrkc soft\nwzsxh hard\nisibmmg soft\nhrfnj soft\nhdaavekw soft",
"output": "5"
},
{
"input": "18\nzpvpfze soft\nsdlnere soft\nkwkvgz soft\nzla soft\ndxlx hard\nkpmnsooq soft\nlomen soft\nvywn soft\nwfrc hard\nmiash soft\nkrbjwpyw hard\ngpeksveq soft\njhbfqs soft\nkfncick hard\nnwkqbsv soft\nlywaxy soft\nhbxh soft\nbba hard",
"output": "5"
},
{
"input": "21\nazjrptg hard\nynvyfw hard\ncpoe hard\njqbglg hard\nsqh hard\nynya hard\naldaolkg soft\ndrf hard\nesdsm hard\nfjyua hard\nvzlnckg hard\nyxjfqjd hard\nvkyay hard\nebhhke hard\nmsibo hard\nvvmkenyh hard\nxzk hard\nlggl hard\nvrb hard\niep hard\nrsseijey hard",
"output": "7"
},
{
"input": "42\nquxukow soft\nwcn soft\npbwg soft\nlrp hard\nphdvfz soft\nidkvymji soft\nobq soft\nyhx soft\nijygw soft\nztzz soft\nuwdhnwu soft\ndgnuuej hard\nhntyyzr soft\nqxf hard\nztg soft\nhnpq soft\nuhznu soft\nitelgl hard\nggceadhw hard\nrxq soft\nkznmshem hard\nlri hard\ndalh soft\ngyzzuht hard\nzvx soft\nbjffln soft\nwnjwrvi hard\nxudeknru hard\nmql soft\ninoddzbf hard\npdg soft\ngtfk soft\nhyv soft\nxkv soft\nwajqepw soft\ndgc soft\nsefwhuoa soft\nbliirvj soft\nhqea soft\nped soft\nyjgwc soft\natlyha soft",
"output": "8"
},
{
"input": "17\ngewvfeq soft\noaximz hard\nxkfscel soft\nnbxdbggw soft\ngxgsscq hard\nmqbu hard\nbtpzl soft\npsv soft\niov soft\nhliudz soft\nbmiu soft\nqqegoe hard\nufq soft\nmgx soft\nawjthx hard\nonjmhee soft\nxoarup soft",
"output": "5"
}
] | 46 | 0 | 3 | 4,641 |
|
0 | none | [
"none"
] | null | null | This is an interactive problem. In the output section below you will see the information about flushing the output.
Bear Limak thinks of some hidden numberΒ β an integer from interval [2,<=100]. Your task is to say if the hidden number is prime or composite.
Integer *x*<=><=1 is called prime if it has exactly two distinct divisors, 1 and *x*. If integer *x*<=><=1 is not prime, it's called composite.
You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2,<=100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".
For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.
When you are done asking queries, print "prime" or "composite" and terminate your program.
You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2,<=100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.
You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | Up to 20 times you can ask a queryΒ β print an integer from interval [2,<=100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input.
In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program.
To flush you can use (just after printing an integer and end-of-line):
- fflush(stdout) in C++; - System.out.flush() in Java; - stdout.flush() in Python; - flush(output) in Pascal; - See the documentation for other languages.
Hacking. To hack someone, as the input you should print the hidden numberΒ β one integer from the interval [2,<=100]. Of course, his/her solution won't be able to read the hidden number from the input. | [
"yes\nno\nyes\n",
"no\nyes\nno\nno\nno\n"
] | [
"2\n80\n5\ncomposite\n",
"58\n59\n78\n78\n2\nprime\n"
] | The hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/ea790051c34ea7d2761cd9b096412ca7c647a173.png" style="max-width: 100.0%;max-height: 100.0%;"/>
The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/35c6952617fa94ec3e0ea8e63aa1c3c49b3ba420.png" style="max-width: 100.0%;max-height: 100.0%;"/>
59 is a divisor of the hidden number. In the interval [2,β100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | [
{
"input": "30",
"output": "composite 4"
},
{
"input": "59",
"output": "prime 15"
},
{
"input": "2",
"output": "prime 16"
},
{
"input": "7",
"output": "prime 16"
},
{
"input": "9",
"output": "composite 3"
},
{
"input": "13",
"output": "prime 15"
},
{
"input": "55",
"output": "composite 6"
},
{
"input": "89",
"output": "prime 15"
},
{
"input": "3",
"output": "prime 16"
},
{
"input": "4",
"output": "composite 2"
},
{
"input": "6",
"output": "composite 4"
},
{
"input": "8",
"output": "composite 2"
},
{
"input": "11",
"output": "prime 15"
},
{
"input": "12",
"output": "composite 2"
},
{
"input": "25",
"output": "composite 4"
},
{
"input": "36",
"output": "composite 2"
},
{
"input": "46",
"output": "composite 10"
},
{
"input": "47",
"output": "prime 15"
},
{
"input": "49",
"output": "composite 5"
},
{
"input": "51",
"output": "composite 8"
},
{
"input": "53",
"output": "prime 15"
},
{
"input": "59",
"output": "prime 15"
},
{
"input": "64",
"output": "composite 2"
},
{
"input": "81",
"output": "composite 3"
},
{
"input": "91",
"output": "composite 7"
},
{
"input": "93",
"output": "composite 12"
},
{
"input": "94",
"output": "composite 16"
},
{
"input": "95",
"output": "composite 9"
},
{
"input": "96",
"output": "composite 2"
},
{
"input": "97",
"output": "prime 15"
},
{
"input": "99",
"output": "composite 3"
},
{
"input": "100",
"output": "composite 2"
},
{
"input": "26",
"output": "composite 7"
},
{
"input": "94",
"output": "composite 16"
},
{
"input": "25",
"output": "composite 4"
},
{
"input": "46",
"output": "composite 10"
},
{
"input": "49",
"output": "composite 5"
},
{
"input": "82",
"output": "composite 14"
},
{
"input": "87",
"output": "composite 11"
},
{
"input": "85",
"output": "composite 8"
},
{
"input": "74",
"output": "composite 13"
},
{
"input": "86",
"output": "composite 15"
},
{
"input": "93",
"output": "composite 12"
},
{
"input": "58",
"output": "composite 11"
},
{
"input": "47",
"output": "prime 15"
},
{
"input": "22",
"output": "composite 6"
},
{
"input": "23",
"output": "prime 15"
},
{
"input": "62",
"output": "composite 12"
},
{
"input": "38",
"output": "composite 9"
},
{
"input": "91",
"output": "composite 7"
},
{
"input": "34",
"output": "composite 8"
},
{
"input": "95",
"output": "composite 9"
},
{
"input": "4",
"output": "composite 2"
},
{
"input": "16",
"output": "composite 2"
},
{
"input": "96",
"output": "composite 2"
},
{
"input": "69",
"output": "composite 10"
},
{
"input": "65",
"output": "composite 7"
},
{
"input": "6",
"output": "composite 4"
},
{
"input": "18",
"output": "composite 4"
}
] | 77 | 0 | 3 | 4,643 |
|
385 | Bear and Prime Numbers | [
"binary search",
"brute force",
"data structures",
"dp",
"implementation",
"math",
"number theory"
] | null | null | Recently, the bear started studying data structures and faced the following problem.
You are given a sequence of integers *x*1,<=*x*2,<=...,<=*x**n* of length *n* and *m* queries, each of them is characterized by two integers *l**i*,<=*r**i*. Let's introduce *f*(*p*) to represent the number of such indexes *k*, that *x**k* is divisible by *p*. The answer to the query *l**i*,<=*r**i* is the sum: , where *S*(*l**i*,<=*r**i*) is a set of prime numbers from segment [*l**i*,<=*r**i*] (both borders are included in the segment).
Help the bear cope with the problem. | The first line contains integer *n* (1<=β€<=*n*<=β€<=106). The second line contains *n* integers *x*1,<=*x*2,<=...,<=*x**n* (2<=β€<=*x**i*<=β€<=107). The numbers are not necessarily distinct.
The third line contains integer *m* (1<=β€<=*m*<=β€<=50000). Each of the following *m* lines contains a pair of space-separated integers, *l**i* and *r**i* (2<=β€<=*l**i*<=β€<=*r**i*<=β€<=2Β·109) β the numbers that characterize the current query. | Print *m* integers β the answers to the queries on the order the queries appear in the input. | [
"6\n5 5 7 10 14 15\n3\n2 11\n3 12\n4 4\n",
"7\n2 3 5 7 11 4 8\n2\n8 10\n2 123\n"
] | [
"9\n7\n0\n",
"0\n7\n"
] | Consider the first sample. Overall, the first sample has 3 queries.
1. The first query *l*β=β2, *r*β=β11 comes. You need to count *f*(2)β+β*f*(3)β+β*f*(5)β+β*f*(7)β+β*f*(11)β=β2β+β1β+β4β+β2β+β0β=β9. 1. The second query comes *l*β=β3, *r*β=β12. You need to count *f*(3)β+β*f*(5)β+β*f*(7)β+β*f*(11)β=β1β+β4β+β2β+β0β=β7. 1. The third query comes *l*β=β4, *r*β=β4. As this interval has no prime numbers, then the sum equals 0. | [
{
"input": "6\n5 5 7 10 14 15\n3\n2 11\n3 12\n4 4",
"output": "9\n7\n0"
},
{
"input": "7\n2 3 5 7 11 4 8\n2\n8 10\n2 123",
"output": "0\n7"
},
{
"input": "9\n50 50 50 50 50 50 50 50 50\n7\n20 20\n8 13\n13 13\n6 14\n3 5\n15 17\n341 1792",
"output": "0\n0\n0\n0\n9\n0\n0"
},
{
"input": "1\n6\n1\n2 3",
"output": "2"
},
{
"input": "1\n10000000\n1\n2000000000 2000000000",
"output": "0"
},
{
"input": "12\n2 4 8 16 32 64 128 256 512 1024 2048 4096\n14\n2 2\n2 2000000000\n4 4\n8 8\n16 16\n32 32\n64 64\n128 128\n256 256\n512 512\n1024 1024\n2048 2048\n4096 4096\n3 2000000000",
"output": "12\n12\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0\n0"
},
{
"input": "9\n9999991 9999943 9999883 4658161 4657997 2315407 2315263 1000003 1000033\n13\n9999991 9999991\n9999943 9999943\n9999883 9999883\n4658161 4658161\n4657997 4657997\n2315407 2315407\n2315263 2315263\n1000003 1000003\n1000033 1000033\n2 2000000000\n2000000000 2000000000\n9999992 2000000000\n1000033 9999990",
"output": "1\n1\n1\n1\n1\n1\n1\n1\n1\n9\n0\n0\n7"
}
] | 2,000 | 83,558,400 | 0 | 4,645 |
|
787 | Not Afraid | [
"greedy",
"implementation",
"math"
] | null | null | Since the giant heads have appeared in the sky all humanity is in danger, so all Ricks and Mortys from all parallel universes are gathering in groups to find a solution to get rid of them.
There are *n* parallel universes participating in this event (*n* Ricks and *n* Mortys). I. e. each of *n* universes has one Rick and one Morty. They're gathering in *m* groups. Each person can be in many groups and a group can contain an arbitrary number of members.
Ricks and Mortys have registered online in these groups. So, a person can have joined a group more than once (developer of this website hadn't considered this possibility).
Summer from universe #1 knows that in each parallel universe (including hers) exactly one of Rick and Morty from that universe is a traitor and is loyal, but no one knows which one. She knows that we are doomed if there's a group such that every member in that group is a traitor (they will plan and destroy the world).
Summer knows that if there's a possibility that world ends (there's a group where all members are traitors) she should immediately cancel this event. So she wants to know if she should cancel the event. You have to tell her yes if and only if there's at least one scenario (among all 2*n* possible scenarios, 2 possible scenarios for who a traitor in each universe) such that in that scenario the world will end. | The first line of input contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=104) β number of universes and number of groups respectively.
The next *m* lines contain the information about the groups. *i*-th of them first contains an integer *k* (number of times someone joined *i*-th group, *k*<=><=0) followed by *k* integers *v**i*,<=1,<=*v**i*,<=2,<=...,<=*v**i*,<=*k*. If *v**i*,<=*j* is negative, it means that Rick from universe number <=-<=*v**i*,<=*j* has joined this group and otherwise it means that Morty from universe number *v**i*,<=*j* has joined it.
Sum of *k* for all groups does not exceed 104. | In a single line print the answer to Summer's question. Print "YES" if she should cancel the event and "NO" otherwise. | [
"4 2\n1 -3\n4 -2 3 2 -3\n",
"5 2\n5 3 -2 1 -1 5\n3 -5 2 5\n",
"7 2\n3 -1 6 7\n7 -5 4 2 4 7 -3 4\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | In the first sample testcase, 1st group only contains the Rick from universe number 3, so in case he's a traitor, then all members of this group are traitors and so Summer should cancel the event. | [
{
"input": "4 2\n1 -3\n4 -2 3 2 -3",
"output": "YES"
},
{
"input": "5 2\n5 3 -2 1 -1 5\n3 -5 2 5",
"output": "NO"
},
{
"input": "7 2\n3 -1 6 7\n7 -5 4 2 4 7 -3 4",
"output": "YES"
},
{
"input": "2 1\n2 -2 2",
"output": "NO"
},
{
"input": "7 7\n1 -2\n1 6\n2 7 -6\n2 -6 4\n2 -4 -6\n3 -5 7 -5\n1 -6",
"output": "YES"
},
{
"input": "100 50\n2 62 -62\n2 19 -19\n2 38 -38\n2 -84 84\n2 -16 16\n2 67 -67\n2 41 -41\n2 -32 32\n2 32 -32\n2 -62 62\n2 89 -89\n2 -84 84\n2 96 -96\n2 -11 11\n2 59 -59\n2 -13 13\n2 -70 70\n2 -3 3\n2 -41 41\n2 -74 74\n2 47 -47\n2 87 -87\n2 17 -17\n2 20 -20\n2 -14 14\n2 -67 67\n2 -95 95\n2 -15 15\n2 -49 49\n2 75 -75\n2 -11 11\n2 -35 35\n2 -10 10\n2 -70 70\n2 -82 82\n2 33 -33\n2 14 -14\n2 -23 23\n2 83 -83\n2 21 -21\n2 86 -86\n2 -51 51\n2 -21 21\n2 -83 83\n2 94 -94\n2 -8 8\n2 75 -75\n2 69 -69\n2 -18 18\n2 42 -42",
"output": "NO"
},
{
"input": "1 1\n1 1",
"output": "YES"
},
{
"input": "1 1\n2 1 -1",
"output": "NO"
},
{
"input": "1 50\n2 1 -1\n2 -1 1\n2 1 -1\n2 1 -1\n2 1 -1\n2 1 -1\n2 -1 1\n2 1 -1\n2 -1 1\n2 1 -1\n2 1 -1\n2 -1 1\n2 -1 1\n2 1 -1\n2 -1 1\n2 1 -1\n2 1 -1\n2 -1 1\n2 -1 1\n2 1 -1\n2 -1 1\n2 1 -1\n2 -1 1\n2 -1 1\n2 1 -1\n2 -1 1\n2 -1 1\n2 -1 1\n2 -1 1\n2 -1 1\n2 1 -1\n2 -1 1\n2 -1 1\n2 1 -1\n2 1 -1\n2 -1 1\n2 1 -1\n2 -1 1\n2 -1 1\n2 1 -1\n2 -1 1\n2 -1 1\n2 1 -1\n2 -1 1\n2 1 -1\n2 1 -1\n2 1 -1\n2 -1 1\n2 -1 1\n2 -1 1",
"output": "NO"
},
{
"input": "10000 1\n2 -6748 6748",
"output": "NO"
},
{
"input": "10000 1\n1 2550",
"output": "YES"
},
{
"input": "10000 1\n10 5365 -2216 -866 -7450 -6342 4329 -777 -4329 5225 -2884",
"output": "NO"
},
{
"input": "3 1\n3 1 1 2",
"output": "YES"
},
{
"input": "5 1\n2 -1 -1",
"output": "YES"
},
{
"input": "4 1\n3 1 1 -1",
"output": "NO"
},
{
"input": "4 1\n4 3 3 3 3",
"output": "YES"
},
{
"input": "1 1\n2 1 1",
"output": "YES"
},
{
"input": "2 1\n2 2 2",
"output": "YES"
},
{
"input": "4 2\n2 1 -1\n1 1",
"output": "YES"
},
{
"input": "7 2\n3 -1 1 7\n7 -5 4 2 4 7 -3 4",
"output": "YES"
},
{
"input": "4 1\n1 -1",
"output": "YES"
},
{
"input": "10 1\n2 4 4",
"output": "YES"
},
{
"input": "1 2\n2 1 -1\n2 -1 -1",
"output": "YES"
},
{
"input": "10000 1\n2 -3 -3",
"output": "YES"
},
{
"input": "1 2\n2 1 1\n2 -1 -1",
"output": "YES"
},
{
"input": "5 1\n2 1 1",
"output": "YES"
},
{
"input": "3 1\n2 3 3",
"output": "YES"
},
{
"input": "4 1\n2 1 1",
"output": "YES"
},
{
"input": "4 2\n3 -1 1 2\n3 -2 4 3",
"output": "YES"
}
] | 46 | 1,433,600 | 3 | 4,650 |
|
835 | Star sky | [
"dp",
"implementation"
] | null | null | The Cartesian coordinate system is set in the sky. There you can see *n* stars, the *i*-th has coordinates (*x**i*, *y**i*), a maximum brightness *c*, equal for all stars, and an initial brightness *s**i* (0<=β€<=*s**i*<=β€<=*c*).
Over time the stars twinkle. At moment 0 the *i*-th star has brightness *s**i*. Let at moment *t* some star has brightness *x*. Then at moment (*t*<=+<=1) this star will have brightness *x*<=+<=1, if *x*<=+<=1<=β€<=*c*, and 0, otherwise.
You want to look at the sky *q* times. In the *i*-th time you will look at the moment *t**i* and you will see a rectangle with sides parallel to the coordinate axes, the lower left corner has coordinates (*x*1*i*, *y*1*i*) and the upper rightΒ β (*x*2*i*, *y*2*i*). For each view, you want to know the total brightness of the stars lying in the viewed rectangle.
A star lies in a rectangle if it lies on its border or lies strictly inside it. | The first line contains three integers *n*, *q*, *c* (1<=β€<=*n*,<=*q*<=β€<=105, 1<=β€<=*c*<=β€<=10)Β β the number of the stars, the number of the views and the maximum brightness of the stars.
The next *n* lines contain the stars description. The *i*-th from these lines contains three integers *x**i*, *y**i*, *s**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=100, 0<=β€<=*s**i*<=β€<=*c*<=β€<=10)Β β the coordinates of *i*-th star and its initial brightness.
The next *q* lines contain the views description. The *i*-th from these lines contains five integers *t**i*, *x*1*i*, *y*1*i*, *x*2*i*, *y*2*i* (0<=β€<=*t**i*<=β€<=109, 1<=β€<=*x*1*i*<=<<=*x*2*i*<=β€<=100, 1<=β€<=*y*1*i*<=<<=*y*2*i*<=β€<=100)Β β the moment of the *i*-th view and the coordinates of the viewed rectangle. | For each view print the total brightness of the viewed stars. | [
"2 3 3\n1 1 1\n3 2 0\n2 1 1 2 2\n0 2 1 4 5\n5 1 1 5 5\n",
"3 4 5\n1 1 2\n2 3 0\n3 3 1\n0 1 1 100 100\n1 2 2 4 4\n2 2 1 4 7\n1 50 50 51 51\n"
] | [
"3\n0\n3\n",
"3\n3\n5\n0\n"
] | Let's consider the first example.
At the first view, you can see only the first star. At moment 2 its brightness is 3, so the answer is 3.
At the second view, you can see only the second star. At moment 0 its brightness is 0, so the answer is 0.
At the third view, you can see both stars. At moment 5 brightness of the first is 2, and brightness of the second is 1, so the answer is 3. | [
{
"input": "2 3 3\n1 1 1\n3 2 0\n2 1 1 2 2\n0 2 1 4 5\n5 1 1 5 5",
"output": "3\n0\n3"
},
{
"input": "3 4 5\n1 1 2\n2 3 0\n3 3 1\n0 1 1 100 100\n1 2 2 4 4\n2 2 1 4 7\n1 50 50 51 51",
"output": "3\n3\n5\n0"
}
] | 623 | 10,854,400 | 3 | 4,655 |
|
656 | Scrambled | [
"*special",
"implementation"
] | null | null | Btoh yuo adn yuor roomatme lhoate wianshg disehs, btu stlil sdmoeboy msut peorrfm tihs cohre dialy. Oen dya yuo decdie to idourtcne smoe syestm. Yuor rmmotaoe sstgegus teh fooniwllg dael. Yuo argee on tow arayrs of ientgres M adn R, nmebur upmicnog dyas (induiclng teh cunrret oen) wtih sicsescuve irnegets (teh ceurrnt dya is zreo), adn yuo wsah teh diehss on dya D if adn olny if terhe etsixs an iednx i scuh taht *D*Β *mod*Β *M*[*i*]<==<=*R*[*i*], otwsehrie yuor rmootmae deos it. Yuo lkie teh cncepot, btu yuor rmotaome's cuinnng simle meaks yuo ssecupt sthnoemig, so yuo itennd to vefriy teh fnerisas of teh aemnrgeet.
Yuo aer geivn ayarrs M adn R. Cuaclatle teh pceanregte of dyas on wchih yuo edn up dnoig teh wisahng. Amsuse taht yuo hvae iiiftlneny mnay dyas aehad of yuo. | The first line of input contains a single integer N (1<=β€<=*N*<=β€<=16).
The second and third lines of input contain N integers each, all between 0 and 16, inclusive, and represent arrays M and R, respectively. All *M*[*i*] are positive, for each *i* *R*[*i*]<=<<=*M*[*i*]. | Output a single real number. The answer is considered to be correct if its absolute or relative error does not exceed 10<=-<=4. | [
"1\n2\n0\n",
"2\n2 3\n1 0\n"
] | [
"0.500000\n",
"0.666667\n"
] | none | [
{
"input": "1\n2\n0",
"output": "0.500000"
},
{
"input": "2\n2 3\n1 0",
"output": "0.666667"
},
{
"input": "3\n2 4 4\n0 1 3",
"output": "1.000000"
},
{
"input": "1\n16\n15",
"output": "0.062500"
},
{
"input": "16\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15",
"output": "1.000000"
},
{
"input": "16\n5 6 9 13 13 15 9 10 2 6 10 11 12 7 4 8\n4 3 3 5 8 3 6 5 1 4 2 6 7 4 0 1",
"output": "0.959707"
},
{
"input": "8\n15 3 7 11 14 10 16 2\n0 2 1 4 0 0 13 1",
"output": "0.826840"
},
{
"input": "1\n7\n5",
"output": "0.142857"
},
{
"input": "9\n6 12 3 10 15 14 6 9 3\n5 2 0 6 1 1 2 2 2",
"output": "0.752381"
},
{
"input": "3\n9 12 6\n0 5 0",
"output": "0.305556"
},
{
"input": "5\n3 3 13 5 10\n1 0 1 4 2",
"output": "0.784615"
},
{
"input": "7\n3 15 11 4 12 15 12\n2 9 3 0 9 13 6",
"output": "0.757576"
},
{
"input": "2\n13 3\n6 0",
"output": "0.384615"
},
{
"input": "9\n15 9 7 4 14 14 2 11 13\n2 6 2 3 11 12 0 3 3",
"output": "0.876790"
},
{
"input": "1\n15\n1",
"output": "0.066667"
},
{
"input": "1\n6\n3",
"output": "0.166667"
},
{
"input": "4\n3 8 9 4\n1 6 7 3",
"output": "0.583333"
},
{
"input": "7\n15 9 9 2 6 8 3\n10 2 7 1 3 2 0",
"output": "0.850000"
},
{
"input": "10\n9 8 7 7 16 3 10 13 5 6\n2 0 0 4 1 0 3 12 1 5",
"output": "0.832418"
},
{
"input": "4\n10 15 2 9\n8 14 0 0",
"output": "0.588889"
},
{
"input": "12\n5 16 12 3 10 15 11 14 2 3 4 11\n3 14 1 0 7 9 10 12 1 2 2 6",
"output": "0.953247"
},
{
"input": "5\n16 6 4 15 2\n13 3 0 13 0",
"output": "0.737500"
},
{
"input": "14\n12 11 7 12 2 4 14 10 7 4 15 3 5 16\n2 8 0 9 0 1 4 0 5 3 11 1 0 6",
"output": "1.000000"
},
{
"input": "12\n8 5 5 12 12 14 14 16 5 11 9 3\n1 4 0 11 10 0 2 3 1 8 8 2",
"output": "0.859307"
},
{
"input": "10\n3 16 16 9 5 16 9 7 8 2\n0 1 7 2 1 9 0 4 4 1",
"output": "0.857143"
},
{
"input": "9\n14 14 5 8 16 2 11 7 11\n9 7 0 2 7 1 10 2 4",
"output": "0.789610"
},
{
"input": "7\n13 12 4 2 7 13 8\n4 6 0 0 3 9 3",
"output": "0.728022"
},
{
"input": "5\n4 15 9 16 6\n3 9 8 14 1",
"output": "0.518056"
},
{
"input": "3\n16 13 3\n11 5 1",
"output": "0.423077"
},
{
"input": "7\n10 15 9 5 9 15 16\n2 7 2 4 0 12 13",
"output": "0.543056"
},
{
"input": "10\n16 10 16 15 12 5 4 9 3 10\n9 0 1 2 9 4 1 8 0 8",
"output": "0.811111"
},
{
"input": "14\n14 8 6 12 13 15 2 3 16 15 15 15 16 8\n10 0 5 6 1 7 0 2 1 4 2 11 14 2",
"output": "0.784615"
},
{
"input": "2\n10 14\n2 5",
"output": "0.171429"
},
{
"input": "10\n2 15 15 4 3 10 8 14 12 12\n1 8 13 0 0 6 4 2 4 5",
"output": "0.914286"
},
{
"input": "3\n6 14 7\n4 2 0",
"output": "0.333333"
},
{
"input": "13\n3 4 16 11 12 13 12 12 3 16 8 13 4\n0 1 14 5 8 5 11 7 1 6 4 1 0",
"output": "0.967949"
}
] | 296 | 6,041,600 | 3 | 4,660 |
|
570 | Replacement | [
"constructive algorithms",
"data structures",
"implementation"
] | null | null | Daniel has a string *s*, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring ".." (two consecutive periods) in string *s*, of all occurrences of the substring let's choose the first one, and replace this substring with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string *s* contains no two consecutive periods, then nothing happens.
Let's define *f*(*s*) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left.
You need to process *m* queries, the *i*-th results in that the character at position *x**i* (1<=β€<=*x**i*<=β€<=*n*) of string *s* is assigned value *c**i*. After each operation you have to calculate and output the value of *f*(*s*).
Help Daniel to process all queries. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=300<=000) the length of the string and the number of queries.
The second line contains string *s*, consisting of *n* lowercase English letters and period signs.
The following *m* lines contain the descriptions of queries. The *i*-th line contains integer *x**i* and *c**i* (1<=β€<=*x**i*<=β€<=*n*, *c**i* β a lowercas English letter or a period sign), describing the query of assigning symbol *c**i* to position *x**i*. | Print *m* numbers, one per line, the *i*-th of these numbers must be equal to the value of *f*(*s*) after performing the *i*-th assignment. | [
"10 3\n.b..bz....\n1 h\n3 c\n9 f\n",
"4 4\n.cc.\n2 .\n3 .\n2 a\n1 a\n"
] | [
"4\n3\n1\n",
"1\n3\n1\n1\n"
] | Note to the first sample test (replaced periods are enclosed in square brackets).
The original string is ".b..bz....".
- after the first query *f*(hb..bz....) = 4Β Β Β Β ("hb[..]bz...." βββ "hb.bz[..].." βββ "hb.bz[..]." βββ "hb.bz[..]" βββ "hb.bz.")- after the second query *f*(hbΡ.bz....) = 3Β Β Β Β ("hbΡ.bz[..].." βββ "hbΡ.bz[..]." βββ "hbΡ.bz[..]" βββ "hbΡ.bz.")- after the third query *f*(hbΡ.bz..f.) = 1Β Β Β Β ("hbΡ.bz[..]f." βββ "hbΡ.bz.f.")
Note to the second sample test.
The original string is ".cc.".
- after the first query: *f*(..c.) = 1Β Β Β Β ("[..]c." βββ ".c.")- after the second query: *f*(....) = 3Β Β Β Β ("[..].." βββ "[..]." βββ "[..]" βββ ".")- after the third query: *f*(.a..) = 1Β Β Β Β (".a[..]" βββ ".a.")- after the fourth query: *f*(aa..) = 1Β Β Β Β ("aa[..]" βββ "aa.") | [
{
"input": "10 3\n.b..bz....\n1 h\n3 c\n9 f",
"output": "4\n3\n1"
},
{
"input": "4 4\n.cc.\n2 .\n3 .\n2 a\n1 a",
"output": "1\n3\n1\n1"
},
{
"input": "3 3\n...\n1 .\n2 a\n3 b",
"output": "2\n0\n0"
},
{
"input": "5 1\n.....\n5 z",
"output": "3"
},
{
"input": "1 5\n.\n1 .\n1 w\n1 w\n1 .\n1 .",
"output": "0\n0\n0\n0\n0"
},
{
"input": "2 7\nab\n1 w\n2 w\n1 c\n2 .\n2 .\n1 .\n2 b",
"output": "0\n0\n0\n0\n0\n1\n0"
}
] | 46 | 0 | 0 | 4,663 |
|
19 | Checkout Assistant | [
"dp"
] | B. Checkout Assistant | 1 | 256 | Bob came to a cash & carry store, put *n* items into his trolley, and went to the checkout counter to pay. Each item is described by its price *c**i* and time *t**i* in seconds that a checkout assistant spends on this item. While the checkout assistant is occupied with some item, Bob can steal some other items from his trolley. To steal one item Bob needs exactly 1 second. What is the minimum amount of money that Bob will have to pay to the checkout assistant? Remember, please, that it is Bob, who determines the order of items for the checkout assistant. | The first input line contains number *n* (1<=β€<=*n*<=β€<=2000). In each of the following *n* lines each item is described by a pair of numbers *t**i*, *c**i* (0<=β€<=*t**i*<=β€<=2000,<=1<=β€<=*c**i*<=β€<=109). If *t**i* is 0, Bob won't be able to steal anything, while the checkout assistant is occupied with item *i*. | Output one number β answer to the problem: what is the minimum amount of money that Bob will have to pay. | [
"4\n2 10\n0 20\n1 5\n1 3\n",
"3\n0 1\n0 10\n0 100\n"
] | [
"8\n",
"111\n"
] | none | [
{
"input": "4\n2 10\n0 20\n1 5\n1 3",
"output": "8"
},
{
"input": "3\n0 1\n0 10\n0 100",
"output": "111"
},
{
"input": "2\n0 635254032\n0 75159864",
"output": "710413896"
},
{
"input": "2\n0 861438648\n1 469893784",
"output": "469893784"
},
{
"input": "2\n2 87623264\n0 864627704",
"output": "87623264"
},
{
"input": "5\n0 51690939\n0 62436494\n0 39009053\n0 70426298\n0 631569476",
"output": "855132260"
},
{
"input": "5\n0 968804136\n0 736567537\n2 343136264\n0 259899572\n1 425744418",
"output": "768880682"
},
{
"input": "5\n4 33400980\n2 410698581\n4 794747123\n0 301889198\n3 219919361",
"output": "33400980"
},
{
"input": "5\n5 491957578\n4 747764103\n3 446810728\n4 896625835\n4 190748656",
"output": "190748656"
},
{
"input": "10\n3 512981548\n0 214269975\n2 603901234\n3 772872647\n0 224281389\n4 561877930\n1 809519308\n4 883486551\n1 114469023\n2 184038037",
"output": "860384990"
}
] | 109 | 307,200 | 0 | 4,669 |
888 | K-Dominant Character | [
"binary search",
"implementation",
"two pointers"
] | null | null | You are given a string *s* consisting of lowercase Latin letters. Character *c* is called *k*-dominant iff each substring of *s* with length at least *k* contains this character *c*.
You have to find minimum *k* such that there exists at least one *k*-dominant character. | The first line contains string *s* consisting of lowercase Latin letters (1<=β€<=|*s*|<=β€<=100000). | Print one number β the minimum value of *k* such that there exists at least one *k*-dominant character. | [
"abacaba\n",
"zzzzz\n",
"abcde\n"
] | [
"2\n",
"1\n",
"3\n"
] | none | [
{
"input": "abacaba",
"output": "2"
},
{
"input": "zzzzz",
"output": "1"
},
{
"input": "abcde",
"output": "3"
},
{
"input": "bcaccacaaabaacaabaaabcbbcbcaacacbcbaaaacccacbbcbaabcbacaacbabacacacaccbbccbcbacbbbbccccabcabaaab",
"output": "8"
},
{
"input": "daabcdabbabbacacbaacabacbcaabaacac",
"output": "4"
},
{
"input": "abghim",
"output": "4"
},
{
"input": "gfliflgfhhdkceacdljgkegmdlhcgkcmlelmbbbmdddgdeeljjhgbbffmemmmkhebgkhadkdajabcjkcgbkgbaeacdedlkklfech",
"output": "17"
},
{
"input": "a",
"output": "1"
},
{
"input": "aaabbb",
"output": "4"
},
{
"input": "ab",
"output": "2"
},
{
"input": "abbb",
"output": "2"
},
{
"input": "ob",
"output": "2"
},
{
"input": "abccba",
"output": "3"
},
{
"input": "saaaaaaaas",
"output": "2"
},
{
"input": "axxx",
"output": "2"
},
{
"input": "abcba",
"output": "2"
},
{
"input": "abb",
"output": "2"
},
{
"input": "abcdea",
"output": "4"
},
{
"input": "axcbb",
"output": "3"
},
{
"input": "tmivvdcbbfrfogjviirrximhttoskopwrcmkcborcxvr",
"output": "13"
},
{
"input": "dlchmmuateksgldkckljrovmeuniobjrelqjpnjljlvhpqrjsfklliqpufplgaelevmlcnodbllquubobecem",
"output": "15"
},
{
"input": "zx",
"output": "2"
},
{
"input": "zxz",
"output": "2"
},
{
"input": "bbhdgaefbhf",
"output": "6"
},
{
"input": "xx",
"output": "1"
},
{
"input": "zxx",
"output": "2"
},
{
"input": "cbcada",
"output": "4"
},
{
"input": "z",
"output": "1"
},
{
"input": "aab",
"output": "2"
},
{
"input": "jficc",
"output": "3"
},
{
"input": "cceeaabfba",
"output": "5"
},
{
"input": "jk",
"output": "2"
},
{
"input": "lzeznbwu",
"output": "5"
},
{
"input": "rr",
"output": "1"
}
] | 2,000 | 10,956,800 | 0 | 4,678 |
|
216 | Forming Teams | [
"dfs and similar",
"implementation"
] | null | null | One day *n* students come to the stadium. They want to play football, and for that they need to split into teams, the teams must have an equal number of people.
We know that this group of people has archenemies. Each student has at most two archenemies. Besides, if student *A* is an archenemy to student *B*, then student *B* is an archenemy to student *A*.
The students want to split so as no two archenemies were in one team. If splitting in the required manner is impossible, some students will have to sit on the bench.
Determine the minimum number of students you will have to send to the bench in order to form the two teams in the described manner and begin the game at last. | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=100, 1<=β€<=*m*<=β€<=100) β the number of students and the number of pairs of archenemies correspondingly.
Next *m* lines describe enmity between students. Each enmity is described as two numbers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*, *a**i*<=β <=*b**i*) β the indexes of the students who are enemies to each other. Each enmity occurs in the list exactly once. It is guaranteed that each student has no more than two archenemies.
You can consider the students indexed in some manner with distinct integers from 1 to *n*. | Print a single integer β the minimum number of students you will have to send to the bench in order to start the game. | [
"5 4\n1 2\n2 4\n5 3\n1 4\n",
"6 2\n1 4\n3 4\n",
"6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n"
] | [
"1",
"0",
"2"
] | none | [
{
"input": "5 4\n1 2\n2 4\n5 3\n1 4",
"output": "1"
},
{
"input": "6 2\n1 4\n3 4",
"output": "0"
},
{
"input": "6 6\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4",
"output": "2"
},
{
"input": "5 1\n1 2",
"output": "1"
},
{
"input": "8 8\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 1",
"output": "0"
},
{
"input": "28 3\n15 3\n10 19\n17 25",
"output": "0"
},
{
"input": "2 1\n1 2",
"output": "0"
},
{
"input": "3 1\n2 3",
"output": "1"
},
{
"input": "3 2\n1 2\n3 2",
"output": "1"
},
{
"input": "3 3\n1 2\n1 3\n2 3",
"output": "1"
},
{
"input": "4 1\n1 4",
"output": "0"
},
{
"input": "4 2\n4 1\n2 1",
"output": "0"
},
{
"input": "4 3\n1 3\n3 2\n2 4",
"output": "0"
},
{
"input": "4 3\n3 2\n4 2\n4 3",
"output": "2"
},
{
"input": "5 3\n4 2\n3 4\n5 1",
"output": "1"
},
{
"input": "10 7\n8 9\n3 6\n2 4\n4 1\n1 3\n2 7\n7 10",
"output": "0"
},
{
"input": "29 20\n15 9\n21 15\n14 12\n12 16\n3 28\n5 13\n19 1\n19 21\n23 17\n27 9\n26 10\n20 5\n8 16\n11 6\n4 22\n29 22\n29 11\n14 17\n28 6\n1 23",
"output": "1"
},
{
"input": "68 50\n10 9\n28 25\n53 46\n38 32\n46 9\n35 13\n65 21\n64 1\n15 52\n43 52\n31 7\n61 67\n41 49\n30 1\n14 4\n17 44\n25 7\n24 31\n57 51\n27 12\n3 37\n17 11\n41 16\n65 23\n10 2\n16 22\n40 36\n15 51\n58 44\n61 2\n50 30\n48 35\n45 32\n56 59\n37 49\n62 55\n62 11\n6 19\n34 33\n53 66\n67 39\n47 21\n56 40\n12 58\n4 23\n26 42\n42 5\n60 8\n5 63\n6 47",
"output": "0"
},
{
"input": "89 30\n86 72\n43 16\n32 80\n17 79\n29 8\n89 37\n84 65\n3 41\n55 79\n33 56\n60 40\n43 45\n59 38\n26 23\n66 61\n81 30\n65 25\n13 71\n25 8\n56 59\n46 13\n22 30\n87 3\n26 32\n75 44\n48 87\n47 4\n63 21\n36 6\n42 86",
"output": "1"
},
{
"input": "100 1\n3 87",
"output": "0"
},
{
"input": "100 10\n88 82\n5 78\n66 31\n65 100\n92 25\n71 62\n47 31\n17 67\n69 68\n59 49",
"output": "0"
},
{
"input": "100 50\n82 99\n27 56\n74 38\n16 68\n90 27\n77 4\n7 88\n77 33\n25 85\n18 70\n50 7\n31 5\n21 20\n50 83\n55 5\n46 83\n55 81\n73 6\n76 58\n60 67\n66 99\n71 23\n100 13\n76 8\n52 14\n6 54\n53 54\n88 22\n12 4\n33 60\n43 62\n42 31\n19 67\n98 80\n15 17\n78 79\n62 37\n66 96\n40 44\n37 86\n71 58\n42 92\n8 38\n92 13\n73 70\n46 41\n30 34\n15 65\n97 19\n14 53",
"output": "0"
},
{
"input": "10 9\n5 10\n3 2\n8 6\n4 5\n4 10\n6 1\n1 8\n9 2\n3 9",
"output": "4"
},
{
"input": "50 48\n33 21\n1 46\n43 37\n1 48\n42 32\n31 45\n14 29\n34 28\n38 19\n46 48\n49 31\n8 3\n27 23\n26 37\n15 9\n27 17\n9 35\n18 7\n35 15\n32 4\n23 17\n36 22\n16 33\n39 6\n40 13\n11 6\n21 16\n10 40\n30 36\n20 5\n24 3\n43 26\n22 30\n41 20\n50 38\n25 29\n5 41\n34 44\n12 7\n8 24\n44 28\n25 14\n12 18\n39 11\n42 4\n45 49\n50 19\n13 10",
"output": "16"
},
{
"input": "19 16\n2 16\n7 10\n17 16\n17 14\n1 5\n19 6\n11 13\n15 19\n7 9\n13 5\n4 6\n1 11\n12 9\n10 12\n2 14\n4 15",
"output": "1"
},
{
"input": "70 70\n27 54\n45 23\n67 34\n66 25\n64 38\n30 68\n51 65\n19 4\n15 33\n47 14\n3 9\n42 29\n69 56\n10 50\n34 58\n51 23\n55 14\n18 53\n27 68\n17 6\n48 6\n8 5\n46 37\n37 33\n21 36\n69 24\n16 13\n50 12\n59 31\n63 38\n22 11\n46 28\n67 62\n63 26\n70 31\n7 59\n55 52\n28 43\n18 35\n53 3\n16 60\n43 40\n61 9\n20 44\n47 41\n35 1\n32 4\n13 54\n30 60\n45 19\n39 42\n2 20\n2 26\n52 8\n12 25\n5 41\n21 10\n58 48\n29 11\n7 56\n49 57\n65 32\n15 40\n66 36\n64 44\n22 57\n1 61\n39 49\n24 70\n62 17",
"output": "10"
},
{
"input": "33 33\n2 16\n28 20\n13 9\n4 22\n18 1\n6 12\n13 29\n32 1\n17 15\n10 7\n6 15\n16 5\n11 10\n31 29\n25 8\n23 21\n14 32\n8 2\n19 3\n11 4\n21 25\n31 30\n33 5\n26 7\n27 26\n27 12\n30 24\n33 17\n28 22\n18 24\n19 9\n3 23\n14 20",
"output": "1"
},
{
"input": "10 8\n8 3\n9 7\n6 1\n10 9\n2 6\n2 1\n3 4\n4 8",
"output": "2"
},
{
"input": "20 12\n16 20\n8 3\n20 5\n5 10\n17 7\n13 2\n18 9\n17 18\n1 6\n14 4\n11 12\n10 16",
"output": "0"
},
{
"input": "35 21\n15 3\n13 5\n2 28\n26 35\n9 10\n22 18\n17 1\n31 32\n35 33\n5 15\n14 24\n29 12\n16 2\n14 10\n7 4\n29 4\n23 27\n30 34\n19 26\n23 11\n25 21",
"output": "1"
},
{
"input": "49 36\n17 47\n19 27\n41 23\n31 27\n11 29\n34 10\n35 2\n42 24\n19 16\n38 24\n5 9\n26 9\n36 14\n18 47\n28 40\n45 13\n35 22\n2 15\n31 30\n20 48\n39 3\n8 34\n36 7\n25 17\n5 39\n29 1\n32 33\n16 30\n38 49\n25 18\n1 11\n7 44\n12 43\n15 22\n49 21\n8 23",
"output": "3"
},
{
"input": "77 54\n18 56\n72 2\n6 62\n58 52\n5 70\n24 4\n67 66\n65 47\n43 77\n61 66\n24 51\n70 7\n48 39\n46 11\n77 28\n65 76\n15 6\n22 13\n34 75\n33 42\n59 37\n7 31\n50 23\n28 9\n17 29\n1 14\n11 45\n36 46\n32 39\n59 21\n22 34\n53 21\n29 47\n16 44\n69 4\n62 16\n36 3\n68 75\n51 69\n49 43\n30 55\n40 20\n57 60\n45 3\n38 33\n49 9\n71 19\n73 20\n48 32\n63 67\n8 54\n42 38\n26 12\n5 74",
"output": "5"
},
{
"input": "93 72\n3 87\n88 60\n73 64\n45 35\n61 85\n68 80\n54 29\n4 88\n19 91\n82 48\n50 2\n40 53\n56 8\n66 82\n83 81\n62 8\n79 30\n89 26\n77 10\n65 15\n27 47\n15 51\n70 6\n59 85\n63 20\n64 92\n7 1\n93 52\n74 38\n71 23\n83 12\n86 52\n46 56\n34 36\n37 84\n18 16\n11 42\n69 72\n53 20\n78 84\n54 91\n14 5\n65 49\n90 19\n42 39\n68 57\n75 27\n57 32\n44 9\n79 74\n48 66\n43 93\n31 30\n58 24\n80 67\n6 60\n39 5\n23 17\n25 1\n18 36\n32 67\n10 9\n14 11\n63 21\n92 73\n13 43\n28 78\n33 51\n4 70\n75 45\n37 28\n62 46",
"output": "5"
},
{
"input": "100 72\n2 88\n55 80\n22 20\n78 52\n66 74\n91 82\n59 77\n97 93\n46 44\n99 35\n73 62\n58 24\n6 16\n47 41\n98 86\n23 19\n39 68\n32 28\n85 29\n37 40\n16 62\n19 61\n84 72\n17 15\n76 96\n37 31\n67 35\n48 15\n80 85\n90 47\n79 36\n39 54\n57 87\n42 60\n34 56\n23 61\n92 2\n88 63\n20 42\n27 81\n65 84\n6 73\n64 100\n76 95\n43 4\n65 86\n21 46\n11 64\n72 98\n63 92\n7 50\n14 22\n89 30\n31 40\n8 57\n90 70\n53 59\n69 24\n96 49\n67 99\n51 70\n18 66\n91 3\n26 38\n13 58\n51 41\n9 11\n5 74\n3 25\n4 32\n28 43\n71 56",
"output": "6"
},
{
"input": "6 5\n1 2\n2 3\n3 4\n4 5\n5 1",
"output": "2"
},
{
"input": "6 4\n1 2\n1 3\n4 5\n4 6",
"output": "0"
},
{
"input": "16 16\n1 2\n2 3\n1 3\n4 5\n5 6\n4 6\n7 8\n8 9\n9 10\n10 11\n11 7\n12 13\n13 14\n14 15\n15 16\n16 12",
"output": "4"
},
{
"input": "4 4\n1 2\n4 3\n1 4\n2 3",
"output": "0"
},
{
"input": "9 9\n1 2\n2 3\n3 1\n4 5\n5 6\n6 4\n7 8\n8 9\n9 7",
"output": "3"
},
{
"input": "20 11\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 8\n8 9\n9 10\n10 11\n11 1",
"output": "2"
},
{
"input": "4 3\n1 2\n3 4\n1 3",
"output": "0"
},
{
"input": "4 2\n2 4\n3 4",
"output": "0"
},
{
"input": "10 10\n1 2\n2 3\n3 4\n4 5\n5 1\n6 7\n7 8\n8 9\n9 10\n10 6",
"output": "2"
},
{
"input": "6 5\n2 1\n3 4\n2 3\n4 5\n5 6",
"output": "0"
},
{
"input": "8 5\n1 2\n2 3\n3 4\n4 5\n5 1",
"output": "2"
},
{
"input": "6 5\n1 2\n2 3\n3 4\n4 5\n1 5",
"output": "2"
},
{
"input": "8 8\n1 2\n2 3\n3 4\n1 4\n5 6\n6 7\n7 8\n5 8",
"output": "0"
},
{
"input": "6 5\n1 3\n1 2\n2 4\n5 3\n5 4",
"output": "2"
}
] | 592 | 31,539,200 | 0 | 4,682 |
|
676 | Nicholas and Permutation | [
"constructive algorithms",
"implementation"
] | null | null | Nicholas has an array *a* that contains *n* distinct integers from 1 to *n*. In other words, Nicholas has a permutation of size *n*.
Nicholas want the minimum element (integer 1) and the maximum element (integer *n*) to be as far as possible from each other. He wants to perform exactly one swap in order to maximize the distance between the minimum and the maximum elements. The distance between two elements is considered to be equal to the absolute difference between their positions. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=100)Β β the size of the permutation.
The second line of the input contains *n* distinct integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*), where *a**i* is equal to the element at the *i*-th position. | Print a single integerΒ β the maximum possible distance between the minimum and the maximum elements Nicholas can achieve by performing exactly one swap. | [
"5\n4 5 1 3 2\n",
"7\n1 6 5 3 4 7 2\n",
"6\n6 5 4 3 2 1\n"
] | [
"3\n",
"6\n",
"5\n"
] | In the first sample, one may obtain the optimal answer by swapping elements 1 and 2.
In the second sample, the minimum and the maximum elements will be located in the opposite ends of the array if we swap 7 and 2.
In the third sample, the distance between the minimum and the maximum elements is already maximum possible, so we just perform some unnecessary swap, for example, one can swap 5 and 2. | [
{
"input": "5\n4 5 1 3 2",
"output": "3"
},
{
"input": "7\n1 6 5 3 4 7 2",
"output": "6"
},
{
"input": "6\n6 5 4 3 2 1",
"output": "5"
},
{
"input": "2\n1 2",
"output": "1"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "3\n2 3 1",
"output": "2"
},
{
"input": "4\n4 1 3 2",
"output": "3"
},
{
"input": "5\n1 4 5 2 3",
"output": "4"
},
{
"input": "6\n4 6 3 5 2 1",
"output": "5"
},
{
"input": "7\n1 5 3 6 2 4 7",
"output": "6"
},
{
"input": "100\n76 70 67 54 40 1 48 63 64 36 42 90 99 27 47 17 93 7 13 84 16 57 74 5 83 61 19 56 52 92 38 91 82 79 34 66 71 28 37 98 35 94 77 53 73 10 26 80 15 32 8 81 3 95 44 46 72 6 33 11 21 85 4 30 24 51 49 96 87 55 14 31 12 60 45 9 29 22 58 18 88 2 50 59 20 86 23 41 100 39 62 68 69 97 78 43 25 89 65 75",
"output": "94"
},
{
"input": "8\n4 5 3 8 6 7 1 2",
"output": "6"
},
{
"input": "9\n6 8 5 3 4 7 9 2 1",
"output": "8"
},
{
"input": "10\n8 7 10 1 2 3 4 6 5 9",
"output": "7"
},
{
"input": "11\n5 4 6 9 10 11 7 3 1 2 8",
"output": "8"
},
{
"input": "12\n3 6 7 8 9 10 12 5 4 2 11 1",
"output": "11"
},
{
"input": "13\n8 4 3 7 5 11 9 1 10 2 13 12 6",
"output": "10"
},
{
"input": "14\n6 10 13 9 7 1 12 14 3 2 5 4 11 8",
"output": "8"
},
{
"input": "15\n3 14 13 12 7 2 4 11 15 1 8 6 5 10 9",
"output": "9"
},
{
"input": "16\n11 6 9 8 7 14 12 13 10 15 2 5 3 1 4 16",
"output": "15"
},
{
"input": "17\n13 12 5 3 9 16 8 14 2 4 10 1 6 11 7 15 17",
"output": "16"
},
{
"input": "18\n8 6 14 17 9 11 15 13 5 3 18 1 2 7 12 16 4 10",
"output": "11"
},
{
"input": "19\n12 19 3 11 15 6 18 14 5 10 2 13 9 7 4 8 17 16 1",
"output": "18"
},
{
"input": "20\n15 17 10 20 7 2 16 9 13 6 18 5 19 8 11 14 4 12 3 1",
"output": "19"
},
{
"input": "21\n1 9 14 18 13 12 11 20 16 2 4 19 15 7 6 17 8 5 3 10 21",
"output": "20"
},
{
"input": "22\n8 3 17 4 16 21 14 11 10 15 6 18 13 12 22 20 5 2 9 7 19 1",
"output": "21"
},
{
"input": "23\n1 23 11 20 9 3 12 4 7 17 5 15 2 10 18 16 8 22 14 13 19 21 6",
"output": "22"
},
{
"input": "24\n2 10 23 22 20 19 18 16 11 12 15 17 21 8 24 13 1 5 6 7 14 3 9 4",
"output": "16"
},
{
"input": "25\n12 13 22 17 1 18 14 5 21 2 10 4 3 23 11 6 20 8 24 16 15 19 9 7 25",
"output": "24"
},
{
"input": "26\n6 21 20 16 26 17 11 2 24 4 1 12 14 8 25 7 15 10 22 5 13 18 9 23 19 3",
"output": "21"
},
{
"input": "27\n20 14 18 10 5 3 9 4 24 22 21 27 17 15 26 2 23 7 12 11 6 8 19 25 16 13 1",
"output": "26"
},
{
"input": "28\n28 13 16 6 1 12 4 27 22 7 18 3 21 26 25 11 5 10 20 24 19 15 14 8 23 17 9 2",
"output": "27"
},
{
"input": "29\n21 11 10 25 2 5 9 16 29 8 17 4 15 13 6 22 7 24 19 12 18 20 1 3 23 28 27 14 26",
"output": "22"
},
{
"input": "30\n6 19 14 22 26 17 27 8 25 3 24 30 4 18 23 16 9 13 29 20 15 2 5 11 28 12 1 10 21 7",
"output": "26"
},
{
"input": "31\n29 13 26 27 9 28 2 16 30 21 12 11 3 31 23 6 22 20 1 5 14 24 19 18 8 4 10 17 15 25 7",
"output": "18"
},
{
"input": "32\n15 32 11 3 18 23 19 14 5 8 6 21 13 24 25 4 16 9 27 20 17 31 2 22 7 12 30 1 26 10 29 28",
"output": "30"
},
{
"input": "33\n22 13 10 33 8 25 15 14 21 28 27 19 26 24 1 12 5 11 32 20 30 31 18 4 6 23 7 29 16 2 17 9 3",
"output": "29"
},
{
"input": "34\n34 30 7 16 6 1 10 23 29 13 15 25 32 26 18 11 28 3 14 21 19 5 31 33 4 17 8 9 24 20 27 22 2 12",
"output": "33"
},
{
"input": "35\n24 33 20 8 34 11 31 25 2 4 18 13 9 35 16 30 23 32 17 1 14 22 19 21 28 26 3 15 5 12 27 29 10 6 7",
"output": "21"
},
{
"input": "36\n1 32 27 35 22 7 34 15 18 36 31 28 13 2 10 21 20 17 16 4 3 24 19 29 11 12 25 5 33 26 14 6 9 23 30 8",
"output": "35"
},
{
"input": "37\n24 1 12 23 11 6 30 15 4 21 13 20 25 17 5 8 36 19 32 26 14 9 7 18 10 29 37 35 16 2 22 34 3 27 31 33 28",
"output": "35"
},
{
"input": "38\n9 35 37 28 36 21 10 25 19 4 26 5 22 7 27 18 6 14 15 24 1 17 11 34 20 8 2 16 3 23 32 31 13 12 38 33 30 29",
"output": "34"
},
{
"input": "39\n16 28 4 33 26 36 25 23 22 30 27 7 12 34 17 6 3 38 10 24 13 31 29 39 14 32 9 20 35 11 18 21 8 2 15 37 5 19 1",
"output": "38"
},
{
"input": "40\n35 39 28 11 9 31 36 8 5 32 26 19 38 33 2 22 23 25 6 37 12 7 3 10 17 24 20 16 27 4 34 15 40 14 18 13 29 21 30 1",
"output": "39"
},
{
"input": "41\n24 18 7 23 3 15 1 17 25 5 30 10 34 36 2 14 9 21 41 40 20 28 33 35 12 22 11 8 19 16 31 27 26 32 29 4 13 38 37 39 6",
"output": "34"
},
{
"input": "42\n42 15 24 26 4 34 19 29 38 32 31 33 14 41 21 3 11 39 25 6 5 20 23 10 16 36 18 28 27 1 7 40 22 30 9 2 37 17 8 12 13 35",
"output": "41"
},
{
"input": "43\n43 24 20 13 22 29 28 4 30 3 32 40 31 8 7 9 35 27 18 5 42 6 17 19 23 12 41 21 16 37 33 34 2 14 36 38 25 10 15 39 26 11 1",
"output": "42"
},
{
"input": "44\n4 38 6 40 29 3 44 2 30 35 25 36 34 10 11 31 21 7 14 23 37 19 27 18 5 22 1 16 17 9 39 13 15 32 43 8 41 26 42 12 24 33 20 28",
"output": "37"
},
{
"input": "45\n45 29 24 2 31 5 34 41 26 44 33 43 15 3 4 11 21 37 27 12 14 39 23 42 16 6 13 19 8 38 20 9 25 22 40 17 32 35 18 10 28 7 30 36 1",
"output": "44"
},
{
"input": "46\n29 3 12 33 45 40 19 17 25 27 28 1 16 23 24 46 31 8 44 15 5 32 22 11 4 36 34 10 35 26 21 7 14 2 18 9 20 41 6 43 42 37 38 13 39 30",
"output": "34"
},
{
"input": "47\n7 3 8 12 24 16 29 10 28 38 1 20 37 40 21 5 15 6 45 23 36 44 25 43 41 4 11 42 18 35 32 31 39 33 27 30 22 34 14 13 17 47 19 9 46 26 2",
"output": "41"
},
{
"input": "48\n29 26 14 18 34 33 13 39 32 1 37 20 35 19 28 48 30 23 46 27 5 22 24 38 12 15 8 36 43 45 16 47 6 9 31 40 44 17 2 41 11 42 25 4 21 3 10 7",
"output": "38"
},
{
"input": "49\n16 7 42 32 11 35 15 8 23 41 6 20 47 24 9 45 49 2 37 48 25 28 5 18 3 19 12 4 22 33 13 14 10 36 44 17 40 38 30 26 1 43 29 46 21 34 27 39 31",
"output": "40"
},
{
"input": "50\n31 45 3 34 13 43 32 4 42 9 7 8 24 14 35 6 19 46 44 17 18 1 25 20 27 41 2 16 12 10 11 47 38 21 28 49 30 15 50 36 29 26 22 39 48 5 23 37 33 40",
"output": "38"
},
{
"input": "51\n47 29 2 11 43 44 27 1 39 14 25 30 33 21 38 45 34 51 16 50 42 31 41 46 15 48 13 19 6 37 35 7 22 28 20 4 17 10 5 8 24 40 9 36 18 49 12 26 23 3 32",
"output": "43"
},
{
"input": "52\n16 45 23 7 15 19 43 20 4 32 35 36 9 50 5 26 38 46 13 33 12 2 48 37 41 31 10 28 8 42 3 21 11 1 17 27 34 30 44 40 6 51 49 47 25 22 18 24 52 29 14 39",
"output": "48"
},
{
"input": "53\n53 30 50 22 51 31 32 38 12 7 39 43 1 23 6 8 24 52 2 21 34 13 3 35 5 15 19 11 47 18 9 20 29 4 36 45 27 41 25 48 16 46 44 17 10 14 42 26 40 28 33 37 49",
"output": "52"
},
{
"input": "54\n6 39 17 3 45 52 16 21 23 48 42 36 13 37 46 10 43 27 49 7 38 32 31 30 15 25 2 29 8 51 54 19 41 44 24 34 22 5 20 14 12 1 33 40 4 26 9 35 18 28 47 50 11 53",
"output": "41"
},
{
"input": "55\n26 15 31 21 32 43 34 51 7 12 5 44 17 54 18 25 48 47 20 3 41 24 45 2 11 22 29 39 37 53 35 28 36 9 50 10 30 38 19 13 4 8 27 1 42 6 49 23 55 40 33 16 46 14 52",
"output": "48"
},
{
"input": "56\n6 20 38 46 10 11 40 19 5 1 47 33 4 18 32 36 37 45 56 49 48 52 12 26 31 14 2 9 24 3 16 51 41 43 23 17 34 7 29 50 55 25 39 44 22 27 54 8 28 35 30 42 13 53 21 15",
"output": "46"
},
{
"input": "57\n39 28 53 36 3 6 12 56 55 20 50 19 43 42 18 40 24 52 38 17 33 23 22 41 14 7 26 44 45 16 35 1 8 47 31 5 30 51 32 4 37 25 13 34 54 21 46 10 15 11 2 27 29 48 49 9 57",
"output": "56"
},
{
"input": "58\n1 26 28 14 22 33 57 40 9 42 44 37 24 19 58 12 48 3 34 31 49 4 16 47 55 52 27 23 46 18 20 32 56 6 39 36 41 38 13 43 45 21 53 54 29 17 5 10 25 30 2 35 11 7 15 51 8 50",
"output": "57"
},
{
"input": "59\n1 27 10 37 53 9 14 49 46 26 50 42 59 11 47 15 24 56 43 45 44 38 5 8 58 30 52 12 23 32 22 3 31 41 2 25 29 6 54 16 35 33 18 55 4 51 57 28 40 19 13 21 7 39 36 48 34 17 20",
"output": "58"
},
{
"input": "60\n60 27 34 32 54 55 33 12 40 3 47 44 50 39 38 59 11 25 17 15 16 30 21 31 10 52 5 23 4 48 6 26 36 57 14 22 8 56 58 9 24 7 37 53 42 43 20 49 51 19 2 46 28 18 35 13 29 45 41 1",
"output": "59"
},
{
"input": "61\n61 11 26 29 31 40 32 30 35 3 18 52 9 53 42 4 50 54 20 58 28 49 22 12 2 19 16 15 57 34 51 43 7 17 25 41 56 47 55 60 46 14 44 45 24 27 33 1 48 13 59 23 38 39 6 5 36 10 8 37 21",
"output": "60"
},
{
"input": "62\n21 23 34 38 11 61 55 30 37 48 54 51 46 47 6 56 36 49 1 35 12 28 29 20 43 42 5 8 22 57 44 4 53 10 58 33 27 25 16 45 50 40 18 15 3 41 39 2 7 60 59 13 32 24 52 31 14 9 19 26 17 62",
"output": "61"
},
{
"input": "63\n2 5 29 48 31 26 21 16 47 24 43 22 61 28 6 39 60 27 14 52 37 7 53 8 62 56 63 10 50 18 44 13 4 9 25 11 23 42 45 41 59 12 32 36 40 51 1 35 49 54 57 20 19 34 38 46 33 3 55 15 30 58 17",
"output": "46"
},
{
"input": "64\n23 5 51 40 12 46 44 8 64 31 58 55 45 24 54 39 21 19 52 61 30 42 16 18 15 32 53 22 28 26 11 25 48 56 27 9 29 41 35 49 59 38 62 7 34 1 20 33 60 17 2 3 43 37 57 14 6 36 13 10 50 4 63 47",
"output": "55"
},
{
"input": "65\n10 11 55 43 53 25 35 26 16 37 41 38 59 21 48 2 65 49 17 23 18 30 62 36 3 4 47 15 28 63 57 54 31 46 44 12 51 7 29 13 56 52 14 22 39 19 8 27 45 5 6 34 32 61 20 50 9 24 33 58 60 40 1 42 64",
"output": "62"
},
{
"input": "66\n66 39 3 2 55 53 60 54 12 49 10 30 59 26 32 46 50 56 7 13 43 36 24 28 11 8 6 21 35 25 42 57 23 45 64 5 34 61 27 51 52 9 15 1 38 17 63 48 37 20 58 14 47 19 22 41 31 44 33 65 4 62 40 18 16 29",
"output": "65"
},
{
"input": "67\n66 16 2 53 35 38 49 28 18 6 36 58 21 47 27 5 50 62 44 12 52 37 11 56 15 31 25 65 17 29 59 41 7 42 4 43 39 10 1 40 24 13 20 54 19 67 46 60 51 45 64 30 8 33 26 9 3 22 34 23 57 48 55 14 63 61 32",
"output": "45"
},
{
"input": "68\n13 6 27 21 65 23 59 14 62 43 33 31 38 41 67 20 16 25 42 4 28 40 29 9 64 17 2 26 32 58 60 53 46 48 47 54 44 50 39 19 30 57 61 1 11 18 37 24 55 15 63 34 8 52 56 7 10 12 35 66 5 36 45 49 68 22 51 3",
"output": "64"
},
{
"input": "69\n29 49 25 51 21 35 11 61 39 54 40 37 60 42 27 33 59 53 34 10 46 2 23 69 8 47 58 36 1 38 19 12 7 48 13 3 6 22 18 5 65 24 50 41 66 44 67 57 4 56 62 43 9 30 14 15 28 31 64 26 16 55 68 17 32 20 45 52 63",
"output": "45"
},
{
"input": "70\n19 12 15 18 36 16 61 69 24 7 11 13 3 48 55 21 37 17 43 31 41 22 28 32 27 63 38 49 59 56 30 25 67 51 52 45 50 44 66 57 26 60 5 46 33 6 23 34 8 40 2 68 14 39 65 64 62 42 47 54 10 53 9 1 70 58 20 4 29 35",
"output": "64"
},
{
"input": "71\n40 6 62 3 41 52 31 66 27 16 35 5 17 60 2 15 51 22 67 61 71 53 1 64 8 45 28 18 50 30 12 69 20 26 10 37 36 49 70 32 33 11 57 14 9 55 4 58 29 25 44 65 39 48 24 47 19 46 56 38 34 42 59 63 54 23 7 68 43 13 21",
"output": "50"
},
{
"input": "72\n52 64 71 40 32 10 62 21 11 37 38 13 22 70 1 66 41 50 27 20 42 47 25 68 49 12 15 72 44 60 53 5 23 14 43 29 65 36 51 54 35 67 7 19 55 48 58 46 39 24 33 30 61 45 57 2 31 3 18 59 6 9 4 63 8 16 26 34 28 69 17 56",
"output": "57"
},
{
"input": "73\n58 38 47 34 39 64 69 66 72 57 9 4 67 22 35 13 61 14 28 52 56 20 31 70 27 24 36 1 62 17 10 5 12 33 16 73 18 49 63 71 44 65 23 30 40 8 50 46 60 25 11 26 37 55 29 68 42 2 3 32 59 7 15 43 41 48 51 53 6 45 54 19 21",
"output": "45"
},
{
"input": "74\n19 51 59 34 8 40 42 55 65 16 74 26 49 63 64 70 35 72 7 12 43 18 61 27 47 31 13 32 71 22 25 67 9 1 48 50 33 10 21 46 11 45 17 37 28 60 69 66 38 2 30 3 39 15 53 68 57 41 6 36 24 73 4 23 5 62 44 14 20 29 52 54 56 58",
"output": "63"
},
{
"input": "75\n75 28 60 19 59 17 65 26 32 23 18 64 8 62 4 11 42 16 47 5 72 46 9 1 25 21 2 50 33 6 36 68 30 12 20 40 53 45 34 7 37 39 38 44 63 61 67 3 66 51 29 73 24 57 70 27 10 56 22 55 13 49 35 15 54 41 14 74 69 48 52 31 71 43 58",
"output": "74"
},
{
"input": "76\n1 47 54 17 38 37 12 32 14 48 43 71 60 56 4 13 64 41 52 57 62 24 23 49 20 10 63 3 25 66 59 40 58 33 53 46 70 7 35 61 72 74 73 19 30 5 29 6 15 28 21 27 51 55 50 9 65 8 67 39 76 42 31 34 16 2 36 11 26 44 22 45 75 18 69 68",
"output": "75"
},
{
"input": "77\n10 20 57 65 53 69 59 45 58 32 28 72 4 14 1 33 40 47 7 5 51 76 37 16 41 61 42 2 21 26 38 74 35 64 43 77 71 50 39 48 27 63 73 44 52 66 9 18 23 54 25 6 8 56 13 67 36 22 15 46 62 75 55 11 31 17 24 29 60 68 12 30 3 70 49 19 34",
"output": "62"
},
{
"input": "78\n7 61 69 47 68 42 65 78 70 3 32 59 49 51 23 71 11 63 22 18 43 34 24 13 27 16 19 40 21 46 48 77 28 66 54 67 60 15 75 62 9 26 52 58 4 25 8 37 41 76 1 6 30 50 44 36 5 14 29 53 17 12 2 57 73 35 64 39 56 10 33 20 45 74 31 55 38 72",
"output": "70"
},
{
"input": "79\n75 79 43 66 72 52 29 65 74 38 24 1 5 51 13 7 71 33 4 61 2 36 63 47 64 44 34 27 3 21 17 37 54 53 49 20 28 60 39 10 16 76 6 77 73 22 50 48 78 30 67 56 31 26 40 59 41 11 18 45 69 62 15 23 32 70 19 55 68 57 35 25 12 46 14 42 9 8 58",
"output": "77"
},
{
"input": "80\n51 20 37 12 68 11 28 52 76 21 7 5 3 16 64 34 25 2 6 40 60 62 75 13 45 17 56 29 32 47 79 73 49 72 15 46 30 54 80 27 43 24 74 18 42 71 14 4 44 63 65 33 1 77 55 57 41 59 58 70 69 35 19 67 10 36 26 23 48 50 39 61 9 66 38 8 31 22 53 78",
"output": "52"
},
{
"input": "81\n63 22 4 41 43 74 64 39 10 35 20 81 11 28 70 67 53 79 16 61 68 52 27 37 58 9 50 49 18 30 72 47 7 60 78 51 23 48 73 66 44 13 15 57 56 38 1 76 25 45 36 34 42 8 75 26 59 14 71 21 6 77 5 17 2 32 40 54 46 24 29 3 31 19 65 62 33 69 12 80 55",
"output": "69"
},
{
"input": "82\n50 24 17 41 49 18 80 11 79 72 57 31 21 35 2 51 36 66 20 65 38 3 45 32 59 81 28 30 70 55 29 76 73 6 33 39 8 7 19 48 63 1 77 43 4 13 78 54 69 9 40 46 74 82 60 71 16 64 12 14 47 26 44 5 10 75 53 25 27 15 56 42 58 34 23 61 67 62 68 22 37 52",
"output": "53"
},
{
"input": "83\n64 8 58 17 67 46 3 82 23 70 72 16 53 45 13 20 12 48 40 4 6 47 76 60 19 44 30 78 28 22 75 15 25 29 63 74 55 32 14 51 35 31 62 77 27 42 65 71 56 61 66 41 68 49 7 34 2 83 36 5 33 26 37 80 59 50 1 9 54 21 18 24 38 73 81 52 10 39 43 79 57 11 69",
"output": "66"
},
{
"input": "84\n75 8 66 21 61 63 72 51 52 13 59 25 28 58 64 53 79 41 34 7 67 11 39 56 44 24 50 9 49 55 1 80 26 6 73 74 27 69 65 37 18 43 36 17 30 3 47 29 76 78 32 22 12 68 46 5 42 81 57 31 33 83 54 48 14 62 10 16 4 20 71 70 35 15 45 19 60 77 2 23 84 40 82 38",
"output": "80"
},
{
"input": "85\n1 18 58 8 22 76 3 61 12 33 54 41 6 24 82 15 10 17 38 64 26 4 62 28 47 14 66 9 84 75 2 71 67 43 37 32 85 21 69 52 55 63 81 51 74 59 65 34 29 36 30 45 27 53 13 79 39 57 5 70 19 40 7 42 68 48 16 80 83 23 46 35 72 31 11 44 73 77 50 56 49 25 60 20 78",
"output": "84"
},
{
"input": "86\n64 56 41 10 31 69 47 39 37 36 27 19 9 42 15 6 78 59 52 17 71 45 72 14 2 54 38 79 4 18 16 8 46 75 50 82 44 24 20 55 58 86 61 43 35 32 33 40 63 30 28 60 13 53 12 57 77 81 76 66 73 84 85 62 68 22 51 5 49 7 1 70 80 65 34 48 23 21 83 11 74 26 29 67 25 3",
"output": "70"
},
{
"input": "87\n14 20 82 47 39 75 71 45 3 37 63 19 32 68 7 41 48 76 27 46 84 49 4 44 26 69 17 64 1 18 58 33 11 23 21 86 67 52 70 16 77 78 6 74 15 87 10 59 13 34 22 2 65 38 66 61 51 57 35 60 81 40 36 80 31 43 83 56 79 55 29 5 12 8 50 30 53 72 54 9 24 25 42 62 73 28 85",
"output": "58"
},
{
"input": "88\n1 83 73 46 61 31 39 86 57 43 16 29 26 80 82 7 36 42 13 20 6 64 19 40 24 12 47 87 8 34 75 9 69 3 11 52 14 25 84 59 27 10 54 51 81 74 65 77 70 17 60 35 23 44 49 2 4 88 5 21 41 32 68 66 15 55 48 58 78 53 22 38 45 33 30 50 85 76 37 79 63 18 28 62 72 56 71 67",
"output": "87"
},
{
"input": "89\n68 40 14 58 56 25 8 44 49 55 9 76 66 54 33 81 42 15 59 17 21 30 75 60 4 48 64 6 52 63 61 27 12 57 72 67 23 86 77 80 22 13 43 73 26 78 50 51 18 62 1 29 82 16 74 2 87 24 3 41 11 46 47 69 10 84 65 39 35 79 70 32 34 31 20 19 53 71 36 28 83 88 38 85 7 5 37 45 89",
"output": "88"
},
{
"input": "90\n2 67 26 58 9 49 76 22 60 30 77 20 13 7 37 81 47 16 19 12 14 45 41 68 85 54 28 24 46 1 27 43 32 89 53 35 59 75 18 51 17 64 66 80 31 88 87 90 38 72 55 71 42 11 73 69 62 78 23 74 65 79 84 4 86 52 10 6 3 82 56 5 48 33 21 57 40 29 61 63 34 36 83 8 15 44 50 70 39 25",
"output": "60"
},
{
"input": "91\n91 69 56 16 73 55 14 82 80 46 57 81 22 71 63 76 43 37 77 75 70 3 26 2 28 17 51 38 30 67 41 47 54 62 34 25 84 11 87 39 32 52 31 36 50 19 21 53 29 24 79 8 74 64 44 7 6 18 10 42 13 9 83 58 4 88 65 60 20 90 66 49 86 89 78 48 5 27 23 59 61 15 72 45 40 33 68 85 35 12 1",
"output": "90"
},
{
"input": "92\n67 57 76 78 25 89 6 82 11 16 26 17 59 48 73 10 21 31 27 80 4 5 22 13 92 55 45 85 63 28 75 60 54 88 91 47 29 35 7 87 1 39 43 51 71 84 83 81 46 9 38 56 90 24 37 41 19 86 50 61 79 20 18 14 69 23 62 65 49 52 58 53 36 2 68 64 15 42 30 34 66 32 44 40 8 33 3 77 74 12 70 72",
"output": "67"
},
{
"input": "93\n76 35 5 87 7 21 59 71 24 37 2 73 31 74 4 52 28 20 56 27 65 86 16 45 85 67 68 70 47 72 91 88 14 32 62 69 78 41 15 22 57 18 50 13 39 58 17 83 64 51 25 11 38 77 82 90 8 26 29 61 10 43 79 53 48 6 23 55 63 49 81 92 80 44 89 60 66 30 1 9 36 33 19 46 75 93 3 12 42 84 40 54 34",
"output": "85"
},
{
"input": "94\n29 85 82 78 61 83 80 63 11 38 50 43 9 24 4 87 79 45 3 17 90 7 34 27 1 76 26 39 84 47 22 41 81 19 44 23 56 92 35 31 72 62 70 53 40 88 13 14 73 2 59 86 46 94 15 12 77 57 89 42 75 48 18 51 32 55 71 30 49 91 20 60 5 93 33 64 21 36 10 28 8 65 66 69 74 58 6 52 25 67 16 37 54 68",
"output": "69"
},
{
"input": "95\n36 73 18 77 15 71 50 57 79 65 94 88 9 69 52 70 26 66 78 89 55 20 72 83 75 68 32 28 45 74 19 22 54 23 84 90 86 12 42 58 11 81 39 31 85 47 60 44 59 43 21 7 30 41 64 76 93 46 87 48 10 40 3 14 38 49 29 35 2 67 5 34 13 37 27 56 91 17 62 80 8 61 53 95 24 92 6 82 63 33 51 25 4 16 1",
"output": "94"
},
{
"input": "96\n64 3 47 83 19 10 72 61 73 95 16 40 54 84 8 86 28 4 37 42 92 48 63 76 67 1 59 66 20 35 93 2 43 7 45 70 34 33 26 91 85 89 13 29 58 68 44 25 87 75 49 71 41 17 55 36 32 31 74 22 52 79 30 88 50 78 38 39 65 27 69 77 81 94 82 53 21 80 57 60 24 46 51 9 18 15 96 62 6 23 11 12 90 5 14 56",
"output": "86"
},
{
"input": "97\n40 63 44 64 84 92 38 41 28 91 3 70 76 67 94 96 35 79 29 22 78 88 85 8 21 1 93 54 71 80 37 17 13 26 62 59 75 87 69 33 89 49 77 61 12 39 6 36 58 18 73 50 82 45 74 52 11 34 95 7 23 30 15 32 31 16 55 19 20 83 60 72 10 53 51 14 27 9 68 47 5 2 81 46 57 86 56 43 48 66 24 25 4 42 65 97 90",
"output": "95"
},
{
"input": "98\n85 94 69 86 22 52 27 79 53 91 35 55 33 88 8 75 76 95 64 54 67 30 70 49 6 16 2 48 80 32 25 90 98 46 9 96 36 81 10 92 28 11 37 97 15 41 38 40 83 44 29 47 23 3 31 61 87 39 78 20 68 12 17 73 59 18 77 72 43 51 84 24 89 65 26 7 74 93 21 19 5 14 50 42 82 71 60 56 34 62 58 57 45 66 13 63 4 1",
"output": "97"
},
{
"input": "99\n33 48 19 41 59 64 16 12 17 13 7 1 9 6 4 92 61 49 60 25 74 65 22 97 30 32 10 62 14 55 80 66 82 78 31 23 87 93 27 98 20 29 88 84 77 34 83 96 79 90 56 89 58 72 52 47 21 76 24 70 44 94 5 39 8 18 57 36 40 68 43 75 3 2 35 99 63 26 67 73 15 11 53 28 42 46 69 50 51 95 38 37 54 85 81 91 45 86 71",
"output": "87"
},
{
"input": "100\n28 30 77 4 81 67 31 25 66 56 88 73 83 51 57 34 21 90 38 76 22 99 53 70 91 3 64 54 6 94 8 5 97 80 50 45 61 40 16 95 36 98 9 2 17 44 72 55 18 58 47 12 87 24 7 32 14 23 65 41 63 48 62 39 92 27 43 19 46 13 42 52 96 84 26 69 100 79 93 49 35 60 71 59 68 15 10 29 20 1 78 33 75 86 11 85 74 82 89 37",
"output": "89"
},
{
"input": "100\n100 97 35 55 45 3 46 98 77 64 94 85 73 43 49 79 72 9 70 62 80 88 29 58 61 20 89 83 66 86 82 15 6 87 42 96 90 75 63 38 81 40 5 23 4 18 41 19 99 60 8 12 76 51 39 93 53 26 21 50 47 28 13 30 68 59 34 54 24 56 31 27 65 16 32 10 36 52 44 91 22 14 33 25 7 78 67 17 57 37 92 11 2 69 84 95 74 71 48 1",
"output": "99"
},
{
"input": "100\n83 96 73 70 30 25 7 77 58 89 76 85 49 82 45 51 14 62 50 9 31 32 16 15 97 64 4 37 20 93 24 10 80 71 100 39 75 72 78 74 8 29 53 86 79 48 3 68 90 99 56 87 63 94 36 1 40 65 6 44 43 84 17 52 34 95 38 47 60 57 98 59 33 41 46 81 23 27 19 2 54 91 55 35 26 12 92 18 28 66 69 21 5 67 13 11 22 88 61 42",
"output": "65"
},
{
"input": "100\n96 80 47 60 56 9 78 20 37 72 68 15 100 94 51 26 65 38 50 19 4 70 25 63 22 30 13 58 43 69 18 33 5 66 39 73 12 55 95 92 97 1 14 83 10 28 64 31 46 91 32 86 74 54 29 52 89 53 90 44 62 40 16 24 67 81 36 34 7 23 79 87 75 98 84 3 41 77 76 42 71 35 49 61 2 27 59 82 99 85 21 11 45 6 88 48 17 57 8 93",
"output": "87"
},
{
"input": "100\n5 6 88 37 97 51 25 81 54 17 57 98 99 44 67 24 30 93 100 36 8 38 84 42 21 4 75 31 85 48 70 77 43 50 65 94 29 32 68 86 56 39 69 47 20 60 52 53 10 34 79 2 95 40 89 64 71 26 22 46 1 62 91 76 83 41 9 78 16 63 13 3 28 92 27 49 7 12 96 72 80 23 14 19 18 66 59 87 90 45 73 82 33 74 35 61 55 15 58 11",
"output": "81"
},
{
"input": "100\n100 97 92 12 62 17 19 58 37 26 30 95 31 35 87 10 13 43 98 61 28 89 76 1 23 21 11 22 50 56 91 74 3 24 96 55 64 67 14 4 71 16 18 9 77 68 51 81 32 82 46 88 86 60 29 66 72 85 70 7 53 63 33 45 83 2 25 94 52 93 5 69 20 47 49 54 57 39 34 27 90 80 78 59 40 42 79 6 38 8 48 15 65 73 99 44 41 84 36 75",
"output": "99"
},
{
"input": "100\n22 47 34 65 69 5 68 78 53 54 41 23 80 51 11 8 2 85 81 75 25 58 29 73 30 49 10 71 17 96 76 89 79 20 12 15 55 7 46 32 19 3 82 35 74 44 38 40 92 14 6 50 97 63 45 93 37 18 62 77 87 36 83 9 90 61 57 28 39 43 52 42 24 56 21 84 26 99 88 59 33 70 4 60 98 95 94 100 13 48 66 72 16 31 64 91 1 86 27 67",
"output": "96"
},
{
"input": "100\n41 67 94 18 14 83 59 12 19 54 13 68 75 26 15 65 80 40 23 30 34 78 47 21 63 79 4 70 3 31 86 69 92 10 61 74 97 100 9 99 32 27 91 55 85 52 16 17 28 1 64 29 58 76 98 25 84 7 2 96 20 72 36 46 49 82 93 44 45 6 38 87 57 50 53 35 60 33 8 89 39 42 37 48 62 81 73 43 95 11 66 88 90 22 24 77 71 51 5 56",
"output": "62"
},
{
"input": "100\n1 88 38 56 62 99 39 80 12 33 57 24 28 84 37 42 10 95 83 58 8 40 20 2 30 78 60 79 36 71 51 31 27 65 22 47 6 19 61 94 75 4 74 35 15 23 92 9 70 13 11 59 90 18 66 81 64 72 16 32 34 67 46 91 21 87 77 97 82 41 7 86 26 43 45 3 93 17 52 96 50 63 48 5 53 44 29 25 98 54 49 14 73 69 89 55 76 85 68 100",
"output": "99"
},
{
"input": "100\n22 59 25 77 68 79 32 45 20 28 61 60 38 86 33 10 100 15 53 75 78 39 67 13 66 34 96 4 63 23 73 29 31 35 71 55 16 14 72 56 94 97 17 93 47 84 57 8 21 51 54 85 26 76 49 81 2 92 62 44 91 87 11 24 95 69 5 7 99 6 65 48 70 12 41 18 74 27 42 3 80 30 50 98 58 37 82 89 83 36 40 52 19 9 88 46 43 1 90 64",
"output": "97"
},
{
"input": "100\n12 1 76 78 97 82 59 80 48 8 91 51 54 74 16 10 89 99 83 63 93 90 55 25 30 33 29 6 9 65 92 79 44 39 15 58 37 46 32 19 27 3 75 49 62 71 98 42 69 50 26 81 96 5 7 61 60 21 20 36 18 34 40 4 47 85 64 38 22 84 2 68 11 56 31 66 17 14 95 43 53 35 23 52 70 13 72 45 41 77 73 87 88 94 28 86 24 67 100 57",
"output": "98"
},
{
"input": "100\n66 100 53 88 7 73 54 41 31 42 8 46 65 90 78 14 94 30 79 39 89 5 83 50 38 61 37 86 22 95 60 98 34 57 91 10 75 25 15 43 23 17 96 35 93 48 87 47 56 13 19 9 82 62 67 80 11 55 99 70 18 26 58 85 12 44 16 45 4 49 20 71 92 24 81 2 76 32 6 21 84 36 52 97 59 63 40 51 27 64 68 3 77 72 28 33 29 1 74 69",
"output": "98"
},
{
"input": "100\n56 64 1 95 72 39 9 49 87 29 94 7 32 6 30 48 50 25 31 78 90 45 60 44 80 68 17 20 73 15 75 98 83 13 71 22 36 26 96 88 35 3 85 54 16 41 92 99 69 86 93 33 43 62 77 46 47 37 12 10 18 40 27 4 63 55 28 59 23 34 61 53 76 42 51 91 21 70 8 58 38 19 5 66 84 11 52 24 81 82 79 67 97 65 57 74 2 89 100 14",
"output": "98"
},
{
"input": "3\n1 2 3",
"output": "2"
},
{
"input": "3\n1 3 2",
"output": "2"
},
{
"input": "3\n2 1 3",
"output": "2"
},
{
"input": "3\n2 3 1",
"output": "2"
},
{
"input": "3\n3 1 2",
"output": "2"
},
{
"input": "3\n3 2 1",
"output": "2"
},
{
"input": "4\n1 2 3 4",
"output": "3"
},
{
"input": "4\n1 2 4 3",
"output": "3"
},
{
"input": "4\n1 3 2 4",
"output": "3"
},
{
"input": "4\n1 3 4 2",
"output": "3"
},
{
"input": "4\n1 4 2 3",
"output": "3"
},
{
"input": "4\n1 4 3 2",
"output": "3"
},
{
"input": "4\n2 1 3 4",
"output": "3"
},
{
"input": "4\n2 1 4 3",
"output": "2"
},
{
"input": "4\n2 4 1 3",
"output": "2"
},
{
"input": "4\n2 4 3 1",
"output": "3"
},
{
"input": "4\n3 1 2 4",
"output": "3"
},
{
"input": "4\n3 1 4 2",
"output": "2"
},
{
"input": "4\n3 2 1 4",
"output": "3"
},
{
"input": "4\n3 2 4 1",
"output": "3"
},
{
"input": "4\n3 4 1 2",
"output": "2"
},
{
"input": "4\n3 4 2 1",
"output": "3"
},
{
"input": "4\n4 1 2 3",
"output": "3"
},
{
"input": "4\n4 1 3 2",
"output": "3"
},
{
"input": "4\n4 2 1 3",
"output": "3"
},
{
"input": "4\n4 2 3 1",
"output": "3"
},
{
"input": "4\n4 3 1 2",
"output": "3"
},
{
"input": "4\n4 3 2 1",
"output": "3"
},
{
"input": "8\n2 5 6 4 8 3 1 7",
"output": "6"
},
{
"input": "5\n2 3 1 5 4",
"output": "3"
},
{
"input": "6\n2 5 3 6 4 1",
"output": "5"
},
{
"input": "6\n5 4 2 6 1 3",
"output": "4"
},
{
"input": "6\n4 2 3 1 6 5",
"output": "4"
},
{
"input": "6\n5 4 2 1 6 3",
"output": "4"
},
{
"input": "9\n7 2 3 4 5 6 1 9 8",
"output": "7"
},
{
"input": "6\n3 2 1 4 6 5",
"output": "4"
},
{
"input": "6\n2 3 4 1 6 5",
"output": "4"
},
{
"input": "10\n5 2 3 4 1 6 7 8 10 9",
"output": "8"
},
{
"input": "6\n5 2 3 1 6 4",
"output": "4"
},
{
"input": "10\n2 9 3 4 1 10 5 6 7 8",
"output": "5"
},
{
"input": "10\n2 3 4 5 6 7 1 8 10 9",
"output": "8"
},
{
"input": "8\n2 3 4 5 1 6 8 7",
"output": "6"
},
{
"input": "6\n2 1 3 4 5 6",
"output": "5"
}
] | 62 | 0 | 0 | 4,691 |
|
289 | Polo the Penguin and Segments | [
"brute force",
"implementation"
] | null | null | Little penguin Polo adores integer segments, that is, pairs of integers [*l*;Β *r*] (*l*<=β€<=*r*).
He has a set that consists of *n* integer segments: [*l*1;Β *r*1],<=[*l*2;Β *r*2],<=...,<=[*l**n*;Β *r**n*]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the right, that is transform [*l*;Β *r*] to either segment [*l*<=-<=1;Β *r*], or to segment [*l*;Β *r*<=+<=1].
The value of a set of segments that consists of *n* segments [*l*1;Β *r*1],<=[*l*2;Β *r*2],<=...,<=[*l**n*;Β *r**n*] is the number of integers *x*, such that there is integer *j*, for which the following inequality holds, *l**j*<=β€<=*x*<=β€<=*r**j*.
Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by *k*. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=105). Each of the following *n* lines contain a segment as a pair of integers *l**i* and *r**i* (<=-<=105<=β€<=*l**i*<=β€<=*r**i*<=β€<=105), separated by a space.
It is guaranteed that no two segments intersect. In other words, for any two integers *i*,<=*j* (1<=β€<=*i*<=<<=*j*<=β€<=*n*) the following inequality holds, *min*(*r**i*,<=*r**j*)<=<<=*max*(*l**i*,<=*l**j*). | In a single line print a single integer β the answer to the problem. | [
"2 3\n1 2\n3 4\n",
"3 7\n1 2\n3 3\n4 7\n"
] | [
"2\n",
"0\n"
] | none | [
{
"input": "2 3\n1 2\n3 4",
"output": "2"
},
{
"input": "3 7\n1 2\n3 3\n4 7",
"output": "0"
},
{
"input": "3 7\n1 10\n11 47\n74 128",
"output": "3"
},
{
"input": "5 4\n1 1\n2 2\n3 3\n4 4\n5 5",
"output": "3"
},
{
"input": "7 4\n2 2\n-1 -1\n0 1\n7 8\n-3 -2\n9 9\n4 6",
"output": "0"
},
{
"input": "10 2\n92 92\n55 59\n70 73\n78 81\n62 65\n95 99\n74 75\n85 87\n51 51\n60 60",
"output": "0"
},
{
"input": "10 474\n56 60\n82 82\n73 73\n105 109\n77 80\n51 51\n85 88\n97 100\n91 92\n64 68",
"output": "442"
},
{
"input": "47 21\n3 5\n-422 -417\n60 60\n-348 -348\n-3 -3\n-364 -361\n-49 -41\n-436 -430\n-250 -244\n-33 -26\n-162 -158\n-90 -88\n-357 -352\n-339 -337\n-25 -19\n-69 -67\n-261 -260\n-292 -283\n12 18\n44 44\n-277 -275\n-301 -293\n-108 -98\n-180 -172\n-327 -318\n-314 -309\n-12 -7\n-134 -130\n33 35\n-190 -184\n-65 -55\n-242 -240\n-448 -444\n-408 -405\n53 57\n-145 -144\n-207 -200\n-110 -110\n-221 -216\n-122 -112\n26 27\n-271 -269\n-82 -79\n-235 -229\n-382 -373\n-397 -391\n-155 -153",
"output": "18"
},
{
"input": "3 4587\n-49 368\n-734 -390\n-380 -117",
"output": "3560"
},
{
"input": "1 100000\n-100000 100000",
"output": "99999"
},
{
"input": "2 100000\n-100000 99999\n100000 100000",
"output": "99999"
},
{
"input": "1 7\n0 0",
"output": "6"
},
{
"input": "2 5848\n-100000 0\n1 100000",
"output": "4679"
},
{
"input": "3 99999\n-100000 -100000\n-99999 99998\n99999 100000",
"output": "99996"
}
] | 654 | 0 | 3 | 4,694 |
|
319 | Psychos in a Line | [
"data structures",
"implementation"
] | null | null | There are *n* psychos standing in a line. Each psycho is assigned a unique integer from 1 to *n*. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step.
You're given the initial arrangement of the psychos in the line. Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment. Look notes to understand the statement more precise. | The first line of input contains integer *n* denoting the number of psychos, (1<=β€<=*n*<=β€<=105). In the second line there will be a list of *n* space separated distinct integers each in range 1 to *n*, inclusive β ids of the psychos in the line from left to right. | Print the number of steps, so that the line remains the same afterward. | [
"10\n10 9 7 8 6 5 3 4 2 1\n",
"6\n1 2 3 4 5 6\n"
] | [
"2\n",
"0\n"
] | In the first sample line of the psychos transforms as follows: [10 9 7 8 6 5 3 4 2 1] βββ [10 8 4] βββ [10]. So, there are two steps. | [
{
"input": "10\n10 9 7 8 6 5 3 4 2 1",
"output": "2"
},
{
"input": "6\n1 2 3 4 5 6",
"output": "0"
},
{
"input": "6\n6 5 4 3 2 1",
"output": "1"
},
{
"input": "10\n10 7 4 2 5 8 9 6 3 1",
"output": "4"
},
{
"input": "15\n15 9 5 10 7 11 14 6 2 3 12 1 8 13 4",
"output": "4"
},
{
"input": "100\n61 96 25 10 50 71 38 77 76 75 59 100 89 66 6 99 2 13 3 23 91 93 22 92 4 86 90 44 39 31 9 47 28 95 18 54 1 73 94 78 60 20 42 84 97 83 16 81 67 64 74 46 82 5 88 80 14 48 53 79 30 11 62 21 41 70 63 58 51 56 57 17 87 72 27 85 68 49 52 8 12 98 43 37 35 69 55 32 26 40 29 65 19 24 34 33 15 45 36 7",
"output": "8"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n1 2",
"output": "0"
},
{
"input": "2\n2 1",
"output": "1"
}
] | 0 | 0 | -1 | 4,695 |
|
36 | Fractal | [
"implementation"
] | B. Fractal | 2 | 64 | Ever since Kalevitch, a famous Berland abstractionist, heard of fractals, he made them the main topic of his canvases. Every morning the artist takes a piece of graph paper and starts with making a model of his future canvas. He takes a square as big as *n*<=Γ<=*n* squares and paints some of them black. Then he takes a clean square piece of paper and paints the fractal using the following algorithm:
Step 1. The paper is divided into *n*2 identical squares and some of them are painted black according to the model.
Step 2. Every square that remains white is divided into *n*2 smaller squares and some of them are painted black according to the model.
Every following step repeats step 2.
Unfortunately, this tiresome work demands too much time from the painting genius. Kalevitch has been dreaming of making the process automatic to move to making 3D or even 4D fractals. | The first line contains integers *n* and *k* (2<=β€<=*n*<=β€<=3, 1<=β€<=*k*<=β€<=5), where *k* is the amount of steps of the algorithm. Each of the following *n* lines contains *n* symbols that determine the model. Symbol Β«.Β» stands for a white square, whereas Β«*Β» stands for a black one. It is guaranteed that the model has at least one white square. | Output a matrix *n**k*<=Γ<=*n**k* which is what a picture should look like after *k* steps of the algorithm. | [
"2 3\n.*\n..\n",
"3 2\n.*.\n***\n.*.\n"
] | [
".*******\n..******\n.*.*****\n....****\n.***.***\n..**..**\n.*.*.*.*\n........\n",
".*.***.*.\n*********\n.*.***.*.\n*********\n*********\n*********\n.*.***.*.\n*********\n.*.***.*.\n"
] | none | [
{
"input": "2 3\n.*\n..",
"output": ".*******\n..******\n.*.*****\n....****\n.***.***\n..**..**\n.*.*.*.*\n........"
},
{
"input": "3 2\n.*.\n***\n.*.",
"output": ".*.***.*.\n*********\n.*.***.*.\n*********\n*********\n*********\n.*.***.*.\n*********\n.*.***.*."
},
{
"input": "2 1\n..\n..",
"output": "..\n.."
},
{
"input": "2 2\n*.\n*.",
"output": "***.\n***.\n***.\n***."
},
{
"input": "2 2\n**\n*.",
"output": "****\n****\n****\n***."
},
{
"input": "2 2\n*.\n..",
"output": "***.\n**..\n*.*.\n...."
},
{
"input": "2 3\n*.\n.*",
"output": "*******.\n******.*\n*****.**\n****.***\n***.****\n**.*****\n*.******\n.*******"
},
{
"input": "2 3\n..\n**",
"output": "........\n********\n********\n********\n********\n********\n********\n********"
},
{
"input": "2 3\n*.\n**",
"output": "*******.\n********\n********\n********\n********\n********\n********\n********"
},
{
"input": "2 4\n**\n..",
"output": "****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n****************\n................"
},
{
"input": "2 4\n*.\n.*",
"output": "***************.\n**************.*\n*************.**\n************.***\n***********.****\n**********.*****\n*********.******\n********.*******\n*******.********\n******.*********\n*****.**********\n****.***********\n***.************\n**.*************\n*.**************\n.***************"
},
{
"input": "2 4\n.*\n.*",
"output": ".***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************\n.***************"
},
{
"input": "2 5\n.*\n*.",
"output": ".*******************************\n*.******************************\n**.*****************************\n***.****************************\n****.***************************\n*****.**************************\n******.*************************\n*******.************************\n********.***********************\n*********.**********************\n**********.*********************\n***********.********************\n************.*******************\n*************.******************\n**************.*****************\n*..."
},
{
"input": "2 5\n*.\n..",
"output": "*******************************.\n******************************..\n*****************************.*.\n****************************....\n***************************.***.\n**************************..**..\n*************************.*.*.*.\n************************........\n***********************.*******.\n**********************..******..\n*********************.*.*****.*.\n********************....****....\n*******************.***.***.***.\n******************..**..**..**..\n*****************.*.*.*.*.*.*.*.\n*..."
},
{
"input": "2 5\n..\n*.",
"output": "................................\n*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.\n**..**..**..**..**..**..**..**..\n***.***.***.***.***.***.***.***.\n****....****....****....****....\n*****.*.*****.*.*****.*.*****.*.\n******..******..******..******..\n*******.*******.*******.*******.\n********........********........\n*********.*.*.*.*********.*.*.*.\n**********..**..**********..**..\n***********.***.***********.***.\n************....************....\n*************.*.*************.*.\n**************..**************..\n*..."
},
{
"input": "2 5\n**\n*.",
"output": "********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n********************************\n*..."
},
{
"input": "3 1\n*..\n...\n..*",
"output": "*..\n...\n..*"
},
{
"input": "3 2\n**.\n.**\n..*",
"output": "********.\n******.**\n******..*\n**.******\n.********\n..*******\n**.**.***\n.**.*****\n..*..****"
},
{
"input": "3 2\n..*\n***\n*..",
"output": "..*..****\n*********\n*..*..***\n*********\n*********\n*********\n***..*..*\n*********\n****..*.."
},
{
"input": "3 3\n**.\n..*\n*.*",
"output": "**************************.\n************************..*\n*************************.*\n********************.**.***\n******************..*..****\n*******************.**.****\n***********************.***\n*********************..****\n**********************.****\n********.********.*********\n******..*******..**********\n*******.********.**********\n**.**.*****.**.************\n..*..****..*..*************\n*.**.*****.**.*************\n*****.********.************\n***..*******..*************\n****.********.****..."
},
{
"input": "3 3\n*.*\n.*.\n..*",
"output": "*************.*************\n************.*.************\n************..*************\n**********.*****.**********\n*********.*.***.*.*********\n*********..****..**********\n**********.**.*************\n*********.*..*.************\n*********..*..*************\n****.*****************.****\n***.*.***************.*.***\n***..****************..****\n*.*****.***********.*****.*\n.*.***.*.*********.*.***.*.\n..****..**********..****..*\n*.**.**************.**.****\n.*..*.************.*..*.***\n..*..*************..."
},
{
"input": "3 3\n...\n*..\n..*",
"output": "...........................\n*..*..*..*..*..*..*..*..*..\n..*..*..*..*..*..*..*..*..*\n***......***......***......\n****..*..****..*..****..*..\n***..*..****..*..****..*..*\n......***......***......***\n*..*..****..*..****..*..***\n..*..****..*..****..*..****\n*********..................\n**********..*..*..*..*..*..\n*********..*..*..*..*..*..*\n************......***......\n*************..*..****..*..\n************..*..****..*..*\n*********......***......***\n**********..*..****..*..***\n*********..*..****..."
},
{
"input": "3 4\n***\n*.*\n***",
"output": "*********************************************************************************\n*********************************************************************************\n*********************************************************************************\n*********************************************************************************\n*********************************************************************************\n*********************************************************************************\n*************..."
},
{
"input": "3 4\n*..\n*..\n*..",
"output": "****************************************..*..****..*..*************..*..****..*..\n****************************************..*..****..*..*************..*..****..*..\n****************************************..*..****..*..*************..*..****..*..\n****************************************..*..****..*..*************..*..****..*..\n****************************************..*..****..*..*************..*..****..*..\n****************************************..*..****..*..*************..*..****..*..\n*************..."
},
{
"input": "3 4\n..*\n...\n.*.",
"output": "..*..****..*..*************..*..****..*..****************************************\n......***......************......***......***************************************\n.*..*.***.*..*.************.*..*.***.*..*.***************************************\n..*..*..*..*..*..**********..*..*..*..*..*..*************************************\n..................*********..................************************************\n.*..*..*..*..*..*.*********.*..*..*..*..*..*.************************************\n..****..*..**..."
},
{
"input": "3 5\n...\n*.*\n*..",
"output": "...................................................................................................................................................................................................................................................\n*.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.**.*\n*..*..*..*..*..*..*....."
},
{
"input": "3 5\n.*.\n*.*\n.*.",
"output": ".*.***.*.*********.*.***.*.***************************.*.***.*.*********.*.***.*.*********************************************************************************.*.***.*.*********.*.***.*.***************************.*.***.*.*********.*.***.*.\n*.*****.***********.*****.*****************************.*****.***********.*****.***********************************************************************************.*****.***********.*****.*****************************.*****.***********.*****.*\n.*.***.*.*********.*...."
},
{
"input": "3 5\n***\n**.\n***",
"output": "***************************************************************************************************************************************************************************************************************************************************\n***************************************************************************************************************************************************************************************************************************************************\n*********************..."
},
{
"input": "3 5\n.*.\n***\n**.",
"output": ".*.***.*.*********.*.***.*.***************************.*.***.*.*********.*.***.*.*********************************************************************************.*.***.*.*********.*.***.*.***************************.*.***.*.*********.*.***.*.\n***************************************************************************************************************************************************************************************************************************************************\n**.*****.***********...."
}
] | 218 | 921,600 | 3.938634 | 4,702 |
402 | Trees in a Row | [
"brute force",
"implementation"
] | null | null | The Queen of England has *n* trees growing in a row in her garden. At that, the *i*-th (1<=β€<=*i*<=β€<=*n*) tree from the left has height *a**i* meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all *i* (1<=β€<=*i*<=<<=*n*), *a**i*<=+<=1<=-<=*a**i*<==<=*k*, where *k* is the number the Queen chose.
Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes? | The first line contains two space-separated integers: *n*, *k* (1<=β€<=*n*,<=*k*<=β€<=1000). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1000) β the heights of the trees in the row. | In the first line print a single integer *p* β the minimum number of minutes the gardener needs. In the next *p* lines print the description of his actions.
If the gardener needs to increase the height of the *j*-th (1<=β€<=*j*<=β€<=*n*) tree from the left by *x* (*x*<=β₯<=1) meters, then print in the corresponding line "+Β jΒ x". If the gardener needs to decrease the height of the *j*-th (1<=β€<=*j*<=β€<=*n*) tree from the left by *x* (*x*<=β₯<=1) meters, print on the corresponding line "-Β jΒ x".
If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them. | [
"4 1\n1 2 1 5\n",
"4 1\n1 2 3 4\n"
] | [
"2\n+ 3 2\n- 4 1\n",
"0\n"
] | none | [
{
"input": "4 1\n1 2 1 5",
"output": "2\n+ 3 2\n- 4 1"
},
{
"input": "4 1\n1 2 3 4",
"output": "0"
},
{
"input": "50 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 46 47 48 49 50",
"output": "0"
},
{
"input": "10 1\n1 2 3 4 5 6 7 8 9 10",
"output": "0"
},
{
"input": "50 5\n232 6 11 16 21 26 31 36 41 46 665 56 61 66 71 76 602 86 91 712 101 106 111 116 121 126 131 136 141 146 151 156 161 166 755 176 181 186 191 196 201 206 211 216 221 226 231 236 241 246",
"output": "5\n- 1 231\n- 11 614\n- 17 521\n- 20 616\n- 35 584"
},
{
"input": "10 99\n1 100 199 298 397 496 364 694 793 676",
"output": "2\n+ 7 231\n+ 10 216"
},
{
"input": "1 99\n1",
"output": "0"
},
{
"input": "2 99\n1 100",
"output": "0"
},
{
"input": "3 99\n1 100 199",
"output": "0"
},
{
"input": "4 99\n1 100 199 298",
"output": "0"
},
{
"input": "3 99\n295 566 992",
"output": "2\n- 2 172\n- 3 499"
},
{
"input": "2 99\n307 854",
"output": "1\n- 2 448"
},
{
"input": "7 1\n1 1 2 3 4 5 6",
"output": "6\n+ 2 1\n+ 3 1\n+ 4 1\n+ 5 1\n+ 6 1\n+ 7 1"
},
{
"input": "5 1\n1 1 2 3 4",
"output": "4\n+ 2 1\n+ 3 1\n+ 4 1\n+ 5 1"
},
{
"input": "4 2\n1 1 3 5",
"output": "3\n+ 2 2\n+ 3 2\n+ 4 2"
},
{
"input": "4 1\n1 1 2 3",
"output": "3\n+ 2 1\n+ 3 1\n+ 4 1"
},
{
"input": "5 1\n1 1 1 2 3",
"output": "4\n+ 2 1\n+ 3 2\n+ 4 2\n+ 5 2"
},
{
"input": "3 1\n1 1 2",
"output": "2\n+ 2 1\n+ 3 1"
}
] | 46 | 0 | -1 | 4,703 |
|
567 | Geometric Progression | [
"binary search",
"data structures",
"dp"
] | null | null | Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer *k* and a sequence *a*, consisting of *n* integers.
He wants to know how many subsequences of length three can be selected from *a*, so that they form a geometric progression with common ratio *k*.
A subsequence of length three is a combination of three such indexes *i*1,<=*i*2,<=*i*3, that 1<=β€<=*i*1<=<<=*i*2<=<<=*i*3<=β€<=*n*. That is, a subsequence of length three are such groups of three elements that are not necessarily consecutive in the sequence, but their indexes are strictly increasing.
A geometric progression with common ratio *k* is a sequence of numbers of the form *b*Β·*k*0,<=*b*Β·*k*1,<=...,<=*b*Β·*k**r*<=-<=1.
Polycarp is only three years old, so he can not calculate this number himself. Help him to do it. | The first line of the input contains two integers, *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=2Β·105), showing how many numbers Polycarp's sequence has and his favorite number.
The second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (<=-<=109<=β€<=*a**i*<=β€<=109) β elements of the sequence. | Output a single number β the number of ways to choose a subsequence of length three, such that it forms a geometric progression with a common ratio *k*. | [
"5 2\n1 1 2 2 4\n",
"3 1\n1 1 1\n",
"10 3\n1 2 6 2 3 6 9 18 3 9\n"
] | [
"4",
"1",
"6"
] | In the first sample test the answer is four, as any of the two 1s can be chosen as the first element, the second element can be any of the 2s, and the third element of the subsequence must be equal to 4. | [
{
"input": "5 2\n1 1 2 2 4",
"output": "4"
},
{
"input": "3 1\n1 1 1",
"output": "1"
},
{
"input": "10 3\n1 2 6 2 3 6 9 18 3 9",
"output": "6"
},
{
"input": "20 2\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
"output": "5"
},
{
"input": "5 3\n5 15 15 15 45",
"output": "3"
},
{
"input": "7 1\n1 2 1 2 1 2 1",
"output": "5"
},
{
"input": "10 10\n1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000",
"output": "8"
},
{
"input": "30 4096\n1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864 134217728 268435456 536870912",
"output": "6"
},
{
"input": "3 17\n2 34 578",
"output": "1"
},
{
"input": "12 2\n1 2 1 2 1 2 1 2 1 2 1 2",
"output": "0"
},
{
"input": "10 5\n-100 -100 -500 -100 -500 -2500 -500 -100 -500 -2500",
"output": "17"
},
{
"input": "3 10000\n10 100000 1000000000",
"output": "1"
},
{
"input": "3 200000\n999999998 999999999 1000000000",
"output": "0"
},
{
"input": "15 2\n1 1 1 1 1 2 2 2 2 2 4 4 4 4 4",
"output": "125"
},
{
"input": "10 2\n1 2 3 4 5 6 7 8 9 10",
"output": "2"
},
{
"input": "10 1\n8 6 1 7 9 3 5 2 10 4",
"output": "0"
},
{
"input": "3 110000\n1 110000 -784901888",
"output": "0"
},
{
"input": "9 187000\n1 187000 609261632 1 187000 609261632 1 187000 609261632",
"output": "0"
},
{
"input": "3 2\n1 3 6",
"output": "0"
},
{
"input": "3 2\n2 3 6",
"output": "0"
},
{
"input": "1 1\n1",
"output": "0"
},
{
"input": "1 200000\n1",
"output": "0"
},
{
"input": "2 1\n1 1",
"output": "0"
},
{
"input": "2 2\n1 2",
"output": "0"
},
{
"input": "3 1\n-1000000000 -1000000000 -1000000000",
"output": "1"
},
{
"input": "18 10\n10000000 100000000 1000000000 -10000000 -100000000 -1000000000 -10000000 -100000000 -1000000000 -10000000 -100000000 -1000000000 10000000 100000000 1000000000 10000000 100000000 1000000000",
"output": "20"
},
{
"input": "2 2\n0 0",
"output": "0"
},
{
"input": "3 2\n0 0 0",
"output": "1"
},
{
"input": "1 2\n0",
"output": "0"
},
{
"input": "5 5\n0 0 0 0 0",
"output": "10"
},
{
"input": "3 4\n0 0 1",
"output": "0"
},
{
"input": "3 4\n1 0 0",
"output": "0"
},
{
"input": "5 1\n0 0 0 0 0",
"output": "10"
},
{
"input": "5 3\n0 0 0 0 0",
"output": "10"
},
{
"input": "3 3\n1 0 0",
"output": "0"
},
{
"input": "5 2\n0 0 0 0 0",
"output": "10"
},
{
"input": "4 5\n0 0 0 0",
"output": "4"
},
{
"input": "3 70000\n1 70000 605032704",
"output": "0"
},
{
"input": "3 1\n0 0 0",
"output": "1"
},
{
"input": "4 200000\n0 0 0 0",
"output": "4"
},
{
"input": "3 2048\n-1024 -2097152 0",
"output": "0"
},
{
"input": "3 2\n0 -1 -2",
"output": "0"
},
{
"input": "5 200000\n0 0 0 0 0",
"output": "10"
},
{
"input": "3 10\n0 0 0",
"output": "1"
},
{
"input": "4 1\n0 0 0 0",
"output": "4"
},
{
"input": "3 100000\n-10000 -1000000000 -276447232",
"output": "0"
}
] | 233 | 38,195,200 | 3 | 4,721 |
|
18 | Platforms | [
"brute force",
"math"
] | B. Platforms | 2 | 64 | In one one-dimensional world there are *n* platforms. Platform with index *k* (platforms are numbered from 1) is a segment with coordinates [(*k*<=-<=1)*m*,<=(*k*<=-<=1)*m*<=+<=*l*], and *l*<=<<=*m*. Grasshopper Bob starts to jump along the platforms from point 0, with each jump he moves exactly *d* units right. Find out the coordinate of the point, where Bob will fall down. The grasshopper falls down, if he finds himself not on the platform, but if he finds himself on the edge of the platform, he doesn't fall down. | The first input line contains 4 integer numbers *n*, *d*, *m*, *l* (1<=β€<=*n*,<=*d*,<=*m*,<=*l*<=β€<=106,<=*l*<=<<=*m*) β respectively: amount of platforms, length of the grasshopper Bob's jump, and numbers *m* and *l* needed to find coordinates of the *k*-th platform: [(*k*<=-<=1)*m*,<=(*k*<=-<=1)*m*<=+<=*l*]. | Output the coordinates of the point, where the grosshopper will fall down. Don't forget that if Bob finds himself on the platform edge, he doesn't fall down. | [
"2 2 5 3\n",
"5 4 11 8\n"
] | [
"4\n",
"20\n"
] | none | [
{
"input": "2 2 5 3",
"output": "4"
},
{
"input": "5 4 11 8",
"output": "20"
},
{
"input": "228385 744978 699604 157872",
"output": "2979912"
},
{
"input": "773663 427904 329049 243542",
"output": "1283712"
},
{
"input": "835293 627183 442142 361649",
"output": "1254366"
},
{
"input": "896922 310109 71587 16487",
"output": "310109"
},
{
"input": "958552 993036 701031 109903",
"output": "993036"
},
{
"input": "20182 192314 814124 268107",
"output": "384628"
},
{
"input": "81812 875240 443569 287155",
"output": "875240"
},
{
"input": "3 6 6 3",
"output": "18"
},
{
"input": "3 16 6 3",
"output": "16"
},
{
"input": "3 4 6 3",
"output": "4"
},
{
"input": "680892 333996 619800 374820",
"output": "1001988"
},
{
"input": "658990 366800 43771 676",
"output": "366800"
},
{
"input": "637089 915955 984094 706836",
"output": "915955"
},
{
"input": "615188 948759 924417 924407",
"output": "183286007415"
},
{
"input": "593287 497915 864740 864733",
"output": "82319789035"
},
{
"input": "87738 530718 805063 805047",
"output": "11387616126"
},
{
"input": "65837 79874 229034 229024",
"output": "1636218890"
},
{
"input": "755991 187301 743241 743232",
"output": "2217831141"
},
{
"input": "402841 635488 123613 122628",
"output": "49568064"
},
{
"input": "999463 261665 255021 255007",
"output": "1596941495"
},
{
"input": "43496 179847 327622 327621",
"output": "14250356892"
},
{
"input": "105126 379125 440715 440713",
"output": "46330970625"
},
{
"input": "1000000 1 1000000 999999",
"output": "1000000000000"
},
{
"input": "1000000 16 999952 999951",
"output": "999952000000"
},
{
"input": "1000000 49 999983 999982",
"output": "999983000023"
},
{
"input": "1000000 3 999997 999996",
"output": "999997000002"
},
{
"input": "1000000 11 999989 999988",
"output": "999989000010"
},
{
"input": "1000000 64 999956 999955",
"output": "999956000000"
},
{
"input": "1000000 531 999106 999105",
"output": "999106000236"
},
{
"input": "1000000 337 999956 999955",
"output": "999956000119"
},
{
"input": "1 1 2 1",
"output": "2"
},
{
"input": "1 1000000 5 3",
"output": "1000000"
},
{
"input": "1000000 1000000 1000000 999999",
"output": "1000000000000"
}
] | 966 | 50,892,800 | -1 | 4,726 |
0 | none | [
"none"
] | null | null | Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before.
Obviously, the game ends when the entire sheet is cut into 1<=Γ<=1 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers.
You are given an *n*<=Γ<=*m* piece of paper, somebody has already made *k* cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move. | The first line contains three integers *n*, *m*, *k* (1<=β€<=*n*,<=*m*<=β€<=109,<=0<=β€<=*k*<=β€<=105) β the sizes of the piece of paper and the number of cuts. Then follow *k* lines, each containing 4 integers *xb**i*,<=*yb**i*,<=*xe**i*,<=*ye**i* (0<=β€<=*xb**i*,<=*xe**i*<=β€<=*n*,<=0<=β€<=*yb**i*,<=*ye**i*<=β€<=*m*) β the coordinates of the ends of the existing cuts.
It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border.
The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game. | If the second player wins, print "SECOND". Otherwise, in the first line print "FIRST", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them). | [
"2 1 0\n",
"2 2 4\n0 1 2 1\n0 1 2 1\n1 2 1 0\n1 1 1 2\n"
] | [
"FIRST\n1 0 1 1\n",
"SECOND\n"
] | none | [] | 92 | 0 | 0 | 4,737 |
|
821 | Okabe and Banana Trees | [
"brute force",
"math"
] | null | null | Okabe needs bananas for one of his experiments for some strange reason. So he decides to go to the forest and cut banana trees.
Consider the point (*x*,<=*y*) in the 2D plane such that *x* and *y* are integers and 0<=β€<=*x*,<=*y*. There is a tree in such a point, and it has *x*<=+<=*y* bananas. There are no trees nor bananas in other points. Now, Okabe draws a line with equation . Okabe can select a single rectangle with axis aligned sides with all points on or under the line and cut all the trees in all points that are inside or on the border of this rectangle and take their bananas. Okabe's rectangle can be degenerate; that is, it can be a line segment or even a point.
Help Okabe and find the maximum number of bananas he can get if he chooses the rectangle wisely.
Okabe is sure that the answer does not exceed 1018. You can trust him. | The first line of input contains two space-separated integers *m* and *b* (1<=β€<=*m*<=β€<=1000, 1<=β€<=*b*<=β€<=10000). | Print the maximum number of bananas Okabe can get from the trees he cuts. | [
"1 5\n",
"2 3\n"
] | [
"30\n",
"25\n"
] | The graph above corresponds to sample test 1. The optimal rectangle is shown in red and has 30 bananas. | [
{
"input": "1 5",
"output": "30"
},
{
"input": "2 3",
"output": "25"
},
{
"input": "4 6",
"output": "459"
},
{
"input": "6 3",
"output": "171"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "10 1",
"output": "55"
},
{
"input": "20 10",
"output": "40326"
},
{
"input": "1000 10000",
"output": "74133360011484445"
},
{
"input": "139 9252",
"output": "1137907933561080"
},
{
"input": "859 8096",
"output": "29032056230649780"
},
{
"input": "987 4237",
"output": "5495451829240878"
},
{
"input": "411 3081",
"output": "366755153481948"
},
{
"input": "539 9221",
"output": "16893595018603386"
},
{
"input": "259 770",
"output": "2281741798549"
},
{
"input": "387 5422",
"output": "1771610559998400"
},
{
"input": "515 1563",
"output": "75233740231341"
},
{
"input": "939 407",
"output": "4438222781916"
},
{
"input": "518 6518",
"output": "5511730799718825"
},
{
"input": "646 1171",
"output": "49802404050106"
},
{
"input": "70 7311",
"output": "142915220249910"
},
{
"input": "494 6155",
"output": "4221391613846823"
},
{
"input": "918 7704",
"output": "28569727339126165"
},
{
"input": "46 3844",
"output": "9007500020760"
},
{
"input": "174 2688",
"output": "43730657099581"
},
{
"input": "894 4637",
"output": "5909849585253250"
},
{
"input": "22 3481",
"output": "1548544125646"
},
{
"input": "446 5030",
"output": "1878390629993745"
},
{
"input": "440 8704",
"output": "9470470760118060"
},
{
"input": "569 7548",
"output": "10326205017481606"
},
{
"input": "289 6393",
"output": "1620061541812350"
},
{
"input": "417 1045",
"output": "14758909519725"
},
{
"input": "841 7185",
"output": "19452619774222875"
},
{
"input": "969 6030",
"output": "15265318959845745"
},
{
"input": "393 4874",
"output": "1327174123029975"
},
{
"input": "817 3719",
"output": "2546859449982016"
},
{
"input": "945 2563",
"output": "1115613396515835"
},
{
"input": "369 4511",
"output": "927715710215505"
},
{
"input": "555 3594",
"output": "1061060598862891"
}
] | 78 | 5,529,600 | 3 | 4,739 |
|
624 | Save Luke | [
"math"
] | null | null | Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and *L*, and they move towards each other with speed *v*1 and *v*2, respectively. Luke has width *d* and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive. | The first line of the input contains four integers *d*, *L*, *v*1, *v*2 (1<=β€<=*d*,<=*L*,<=*v*1,<=*v*2<=β€<=10<=000,<=*d*<=<<=*L*)Β β Luke's width, the initial position of the second press and the speed of the first and second presses, respectively. | Print a single real valueΒ β the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6.
Namely: let's assume that your answer is *a*, and the answer of the jury is *b*. The checker program will consider your answer correct, if . | [
"2 6 2 2\n",
"1 9 1 2\n"
] | [
"1.00000000000000000000\n",
"2.66666666666666650000\n"
] | In the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.
In the second sample he needs to occupy the position <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/71395c777960eaded59a9fdc428a9625f152605b.png" style="max-width: 100.0%;max-height: 100.0%;"/>. In this case both presses move to his edges at the same time. | [
{
"input": "2 6 2 2",
"output": "1.00000000000000000000"
},
{
"input": "1 9 1 2",
"output": "2.66666666666666650000"
},
{
"input": "1 10000 1 1",
"output": "4999.50000000000000000000"
},
{
"input": "9999 10000 10000 10000",
"output": "0.00005000000000000000"
},
{
"input": "1023 2340 1029 3021",
"output": "0.32518518518518519000"
},
{
"input": "2173 2176 10000 9989",
"output": "0.00015008254539996998"
},
{
"input": "1 2 123 1",
"output": "0.00806451612903225780"
},
{
"input": "123 1242 12 312",
"output": "3.45370370370370370000"
},
{
"input": "2 9997 3 12",
"output": "666.33333333333337000000"
},
{
"input": "1 10000 10000 10000",
"output": "0.49995000000000001000"
},
{
"input": "3274 4728 888 4578",
"output": "0.26600804976216613000"
},
{
"input": "4600 9696 5634 8248",
"output": "0.36709407866301685000"
},
{
"input": "2255 7902 8891 429",
"output": "0.60590128755364803000"
},
{
"input": "6745 9881 2149 9907",
"output": "0.26011944260119441000"
},
{
"input": "4400 8021 6895 2089",
"output": "0.40304986642920748000"
},
{
"input": "5726 9082 7448 3054",
"output": "0.31955817939440107000"
},
{
"input": "3381 9769 4898 2532",
"output": "0.85975773889636609000"
},
{
"input": "1036 6259 5451 4713",
"output": "0.51387249114521838000"
},
{
"input": "5526 6455 197 4191",
"output": "0.21171376481312670000"
},
{
"input": "1196 4082 4071 9971",
"output": "0.20552627830793335000"
},
{
"input": "8850 9921 8816 9449",
"output": "0.05863673692855187600"
},
{
"input": "3341 7299 2074 8927",
"output": "0.35978547404781386000"
},
{
"input": "7831 8609 6820 2596",
"output": "0.08262531860662701600"
},
{
"input": "2322 7212 77 4778",
"output": "1.00720906282183310000"
},
{
"input": "9976 9996 4823 4255",
"output": "0.00220312844238819110"
},
{
"input": "7631 9769 5377 6437",
"output": "0.18097172845776197000"
},
{
"input": "8957 9525 8634 107",
"output": "0.06498112344125385500"
},
{
"input": "6612 9565 3380 2288",
"output": "0.52099505998588569000"
},
{
"input": "1103 6256 3934 9062",
"output": "0.39650661742074483000"
},
{
"input": "1854 3280 1481 2140",
"output": "0.39381386357359843000"
}
] | 46 | 0 | 3 | 4,746 |
|
413 | Spyke Chatting | [
"implementation"
] | null | null | The R2 company has *n* employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.
R2 has *m* Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the *k*-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.
The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. | The first line contains three space-separated integers *n*, *m* and *k* (2<=β€<=*n*<=β€<=2Β·104;Β 1<=β€<=*m*<=β€<=10;Β 1<=β€<=*k*<=β€<=2Β·105) β the number of the employees, the number of chats and the number of events in the log, correspondingly.
Next *n* lines contain matrix *a* of size *n*<=Γ<=*m*, consisting of numbers zero and one. The element of this matrix, recorded in the *j*-th column of the *i*-th line, (let's denote it as *a**ij*) equals 1, if the *i*-th employee is the participant of the *j*-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to *n* and the chats are numbered from 1 to *m*.
Next *k* lines contain the description of the log events. The *i*-th line contains two space-separated integers *x**i* and *y**i* (1<=β€<=*x**i*<=β€<=*n*;Β 1<=β€<=*y**i*<=β€<=*m*) which mean that the employee number *x**i* sent one message to chat number *y**i*. It is guaranteed that employee number *x**i* is a participant of chat *y**i*. It is guaranteed that each chat contains at least two employees. | Print in the single line *n* space-separated integers, where the *i*-th integer shows the number of message notifications the *i*-th employee receives. | [
"3 4 5\n1 1 1 1\n1 0 1 1\n1 1 0 0\n1 1\n3 1\n1 3\n2 4\n3 2\n",
"4 3 4\n0 1 1\n1 0 1\n1 1 1\n0 0 0\n1 2\n2 1\n3 1\n1 3\n"
] | [
"3 3 1 ",
"0 2 3 0 "
] | none | [
{
"input": "3 4 5\n1 1 1 1\n1 0 1 1\n1 1 0 0\n1 1\n3 1\n1 3\n2 4\n3 2",
"output": "3 3 1 "
},
{
"input": "4 3 4\n0 1 1\n1 0 1\n1 1 1\n0 0 0\n1 2\n2 1\n3 1\n1 3",
"output": "0 2 3 0 "
},
{
"input": "2 1 1\n1\n1\n1 1",
"output": "0 1 "
},
{
"input": "3 3 1\n1 1 1\n1 1 1\n1 1 1\n3 1",
"output": "1 1 0 "
},
{
"input": "3 2 1\n0 1\n1 0\n1 1\n1 2",
"output": "0 0 1 "
},
{
"input": "5 5 5\n0 1 1 1 0\n1 1 0 1 1\n1 1 1 1 1\n0 1 1 1 1\n1 0 0 1 1\n4 5\n4 5\n1 3\n5 4\n1 2",
"output": "1 4 5 3 2 "
},
{
"input": "4 5 6\n1 1 1 1 1\n1 1 1 0 1\n1 0 1 1 1\n1 0 1 1 1\n2 3\n2 5\n1 2\n4 5\n4 5\n4 1",
"output": "5 4 5 2 "
},
{
"input": "7 6 5\n0 1 0 1 1 1\n0 1 1 0 1 1\n1 0 1 1 1 0\n1 0 1 1 1 1\n1 1 1 1 1 0\n1 1 1 0 1 0\n1 0 1 0 0 0\n4 1\n2 6\n7 3\n7 1\n5 3",
"output": "1 2 4 4 3 4 2 "
},
{
"input": "3 3 4\n1 1 1\n0 0 0\n1 1 1\n1 1\n3 1\n3 2\n3 3",
"output": "3 0 1 "
},
{
"input": "10 1 10\n0\n0\n0\n0\n0\n0\n1\n0\n1\n0\n7 1\n9 1\n9 1\n7 1\n9 1\n9 1\n9 1\n7 1\n9 1\n9 1",
"output": "0 0 0 0 0 0 7 0 3 0 "
},
{
"input": "2 1 1\n1\n1\n1 1",
"output": "0 1 "
},
{
"input": "2 1 1\n1\n1\n2 1",
"output": "1 0 "
},
{
"input": "3 1 2\n1\n1\n0\n1 1\n2 1",
"output": "1 1 0 "
},
{
"input": "3 1 2\n1\n0\n1\n1 1\n3 1",
"output": "1 0 1 "
},
{
"input": "3 1 2\n0\n1\n1\n2 1\n3 1",
"output": "0 1 1 "
}
] | 1,000 | 10,956,800 | 0 | 4,747 |
|
609 | Load Balancing | [
"implementation",
"math"
] | null | null | In the school computer room there are *n* servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are *m**i* tasks assigned to the *i*-th server.
In order to balance the load for each server, you want to reassign some tasks to make the difference between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression *m**a*<=-<=*m**b*, where *a* is the most loaded server and *b* is the least loaded one.
In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.
Write a program to find the minimum number of seconds needed to balance the load of servers. | The first line contains positive number *n* (1<=β€<=*n*<=β€<=105) β the number of the servers.
The second line contains the sequence of non-negative integers *m*1,<=*m*2,<=...,<=*m**n* (0<=β€<=*m**i*<=β€<=2Β·104), where *m**i* is the number of tasks assigned to the *i*-th server. | Print the minimum number of seconds required to balance the load. | [
"2\n1 6\n",
"7\n10 11 10 11 10 11 11\n",
"5\n1 2 3 4 5\n"
] | [
"2\n",
"0\n",
"3\n"
] | In the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.
In the second example the load is already balanced.
A possible sequence of task movements for the third example is:
1. move a task from server #4 to server #1 (the sequence *m* becomes: 2 2 3 3 5); 1. then move task from server #5 to server #1 (the sequence *m* becomes: 3 2 3 3 4); 1. then move task from server #5 to server #2 (the sequence *m* becomes: 3 3 3 3 3).
The above sequence is one of several possible ways to balance the load of servers in three seconds. | [
{
"input": "2\n1 6",
"output": "2"
},
{
"input": "7\n10 11 10 11 10 11 11",
"output": "0"
},
{
"input": "5\n1 2 3 4 5",
"output": "3"
},
{
"input": "10\n0 0 0 0 0 0 0 0 0 0",
"output": "0"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "1\n20000",
"output": "0"
},
{
"input": "3\n1 10000 20000",
"output": "9999"
},
{
"input": "10\n19999 19999 20000 20000 19999 20000 20000 20000 19999 19999",
"output": "0"
},
{
"input": "10\n8 5 5 5 6 6 6 6 5 5",
"output": "2"
},
{
"input": "2\n10 3",
"output": "3"
},
{
"input": "5\n6 5 9 7 6",
"output": "2"
},
{
"input": "5\n2 10 20 30 50",
"output": "34"
},
{
"input": "7\n2 2 2 2 2 3 4",
"output": "1"
}
] | 46 | 0 | 0 | 4,749 |
|
1,009 | Annoying Present | [
"greedy",
"math"
] | null | null | Alice got an array of length $n$ as a birthday present once again! This is the third year in a row!
And what is more disappointing, it is overwhelmengly boring, filled entirely with zeros. Bob decided to apply some changes to the array to cheer up Alice.
Bob has chosen $m$ changes of the following form. For some integer numbers $x$ and $d$, he chooses an arbitrary position $i$ ($1 \le i \le n$) and for every $j \in [1, n]$ adds $x + d \cdot dist(i, j)$ to the value of the $j$-th cell. $dist(i, j)$ is the distance between positions $i$ and $j$ (i.e. $dist(i, j) = |i - j|$, where $|x|$ is an absolute value of $x$).
For example, if Alice currently has an array $[2, 1, 2, 2]$ and Bob chooses position $3$ for $x = -1$ and $d = 2$ then the array will become $[2 - 1 + 2 \cdot 2,~1 - 1 + 2 \cdot 1,~2 - 1 + 2 \cdot 0,~2 - 1 + 2 \cdot 1]$ = $[5, 2, 1, 3]$. Note that Bob can't choose position $i$ outside of the array (that is, smaller than $1$ or greater than $n$).
Alice will be the happiest when the elements of the array are as big as possible. Bob claimed that the arithmetic mean value of the elements will work fine as a metric.
What is the maximum arithmetic mean value Bob can achieve? | The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) β the number of elements of the array and the number of changes.
Each of the next $m$ lines contains two integers $x_i$ and $d_i$ ($-10^3 \le x_i, d_i \le 10^3$) β the parameters for the $i$-th change. | Print the maximal average arithmetic mean of the elements Bob can achieve.
Your answer is considered correct if its absolute or relative error doesn't exceed $10^{-6}$. | [
"2 3\n-1 3\n0 0\n-1 -4\n",
"3 2\n0 2\n5 0\n"
] | [
"-2.500000000000000\n",
"7.000000000000000\n"
] | none | [
{
"input": "2 3\n-1 3\n0 0\n-1 -4",
"output": "-2.500000000000000"
},
{
"input": "3 2\n0 2\n5 0",
"output": "7.000000000000000"
},
{
"input": "8 8\n-21 -60\n-96 -10\n-4 -19\n-27 -4\n57 -15\n-95 62\n-42 1\n-17 64",
"output": "-16.500000000000000"
},
{
"input": "1 1\n0 0",
"output": "0.000000000000000"
},
{
"input": "100000 1\n1000 1000",
"output": "50000500.000000000000000"
},
{
"input": "11 1\n0 -10",
"output": "-27.272727272727273"
},
{
"input": "3 1\n1 -1",
"output": "0.333333333333333"
},
{
"input": "1 2\n-1 -1\n-2 -2",
"output": "-3.000000000000000"
},
{
"input": "1 2\n0 -1\n0 1",
"output": "0.000000000000000"
},
{
"input": "1 1\n1 -2",
"output": "1.000000000000000"
},
{
"input": "3 1\n2 -1",
"output": "1.333333333333333"
},
{
"input": "3 1\n0 -1",
"output": "-0.666666666666667"
},
{
"input": "1 1\n-1000 -1000",
"output": "-1000.000000000000000"
},
{
"input": "1 1\n0 -5",
"output": "0.000000000000000"
},
{
"input": "15 3\n2 0\n2 -5\n-2 5",
"output": "18.333333333333332"
},
{
"input": "9 1\n0 -5",
"output": "-11.111111111111111"
},
{
"input": "7 1\n0 -1",
"output": "-1.714285714285714"
},
{
"input": "3 1\n-2 -2",
"output": "-3.333333333333333"
},
{
"input": "3 1\n5 -5",
"output": "1.666666666666667"
},
{
"input": "1 1\n-1 -1",
"output": "-1.000000000000000"
},
{
"input": "7 1\n-1 -5",
"output": "-9.571428571428571"
},
{
"input": "3 2\n-2 -2\n-2 -2",
"output": "-6.666666666666667"
},
{
"input": "5 1\n0 -4",
"output": "-4.800000000000000"
},
{
"input": "5 1\n-1 -5",
"output": "-7.000000000000000"
},
{
"input": "5 1\n0 -2",
"output": "-2.400000000000000"
},
{
"input": "3 5\n1 -1000\n1 -1000\n1 -1000\n1 -1000\n1 -1000",
"output": "-3328.333333333333485"
},
{
"input": "1 1\n0 -1",
"output": "0.000000000000000"
},
{
"input": "1 2\n0 -3\n0 -3",
"output": "0.000000000000000"
},
{
"input": "7 1\n2 -3",
"output": "-3.142857142857143"
},
{
"input": "3 2\n-1 -1\n-1 -1",
"output": "-3.333333333333333"
},
{
"input": "5 1\n-1 -162",
"output": "-195.400000000000006"
},
{
"input": "5 10\n-506 -243\n727 -141\n-548 -306\n740 880\n-744 -116\n-84 182\n-859 -108\n64 86\n135 446\n69 -184",
"output": "864.399999999999977"
},
{
"input": "5 1\n0 -1",
"output": "-1.200000000000000"
},
{
"input": "5 12\n634 895\n143 730\n901 245\n386 486\n395 -111\n-469 -104\n-681 -623\n-900 843\n889 -883\n476 -304\n777 986\n206 -491",
"output": "8107.800000000000182"
},
{
"input": "3 3\n4 2\n5 0\n6 -1",
"output": "16.333333333333332"
},
{
"input": "1 3\n4 2\n5 0\n6 -1",
"output": "15.000000000000000"
},
{
"input": "85 10\n-223 435\n-771 455\n72 -940\n490 -178\n400 -117\n169 -527\n836 610\n849 944\n572 -237\n-428 -428",
"output": "53047.388235294114565"
},
{
"input": "69 10\n-8 4\n-3 3\n7 5\n5 -9\n8 1\n7 -5\n-8 -8\n9 3\n1 1\n0 6",
"output": "420.579710144927560"
},
{
"input": "1 10\n1 1\n1 0\n1 0\n1 0\n-1 0\n0 1\n1 0\n0 0\n2 1\n9 2",
"output": "15.000000000000000"
},
{
"input": "5 4\n0 1\n0 2\n0 3\n0 -9",
"output": "1.200000000000000"
}
] | 405 | 307,200 | 0 | 4,764 |
|
0 | none | [
"none"
] | null | null | Little Artem is fond of dancing. Most of all dances Artem likes ruedaΒ β Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together.
More detailed, there are *n* pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves:
1. Value *x* and some direction are announced, and all boys move *x* positions in the corresponding direction. 1. Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It's guaranteed that *n* is even.
Your task is to determine the final position of each boy. | The first line of the input contains two integers *n* and *q* (2<=β€<=*n*<=β€<=1<=000<=000, 1<=β€<=*q*<=β€<=2<=000<=000)Β β the number of couples in the rueda and the number of commands to perform, respectively. It's guaranteed that *n* is even.
Next *q* lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as *x* (<=-<=*n*<=β€<=*x*<=β€<=*n*), where 0<=β€<=*x*<=β€<=*n* means all boys moves *x* girls in clockwise direction, while <=-<=*x* means all boys move *x* positions in counter-clockwise direction. There is no other input for commands of the second type. | Output *n* integers, the *i*-th of them should be equal to the index of boy the *i*-th girl is dancing with after performing all *q* moves. | [
"6 3\n1 2\n2\n1 2\n",
"2 3\n1 1\n2\n1 -2\n",
"4 2\n2\n1 3\n"
] | [
"4 3 6 5 2 1\n",
"1 2\n",
"1 4 3 2\n"
] | none | [
{
"input": "6 3\n1 2\n2\n1 2",
"output": "4 3 6 5 2 1"
},
{
"input": "2 3\n1 1\n2\n1 -2",
"output": "1 2"
},
{
"input": "4 2\n2\n1 3",
"output": "1 4 3 2"
},
{
"input": "6 8\n1 2\n2\n2\n2\n2\n1 1\n1 -5\n2",
"output": "4 3 6 5 2 1"
},
{
"input": "6 8\n1 -1\n2\n2\n1 4\n1 0\n1 -1\n1 0\n1 -1",
"output": "6 1 2 3 4 5"
},
{
"input": "10 5\n1 8\n1 -3\n1 -3\n2\n1 5",
"output": "3 6 5 8 7 10 9 2 1 4"
},
{
"input": "10 10\n1 2\n1 -10\n1 -5\n2\n2\n1 -4\n2\n2\n1 -10\n1 -9",
"output": "7 8 9 10 1 2 3 4 5 6"
},
{
"input": "6 9\n2\n1 -2\n2\n1 -6\n1 -6\n1 4\n2\n1 -1\n2",
"output": "2 5 4 1 6 3"
},
{
"input": "2 5\n2\n1 -1\n2\n1 1\n2",
"output": "2 1"
},
{
"input": "2 8\n2\n2\n2\n1 -2\n1 -1\n1 -1\n2\n1 1",
"output": "2 1"
},
{
"input": "36 86\n1 -25\n2\n2\n2\n1 16\n1 -14\n1 12\n2\n1 -21\n2\n1 -12\n1 34\n1 -4\n1 19\n1 5\n2\n2\n2\n2\n1 -1\n1 -31\n2\n1 -6\n1 1\n2\n2\n1 27\n1 19\n2\n1 -14\n2\n1 -17\n2\n2\n2\n2\n1 -35\n1 -31\n1 7\n2\n2\n2\n1 -12\n2\n2\n2\n2\n1 7\n1 -9\n1 -2\n2\n1 -3\n2\n2\n1 33\n1 -8\n1 -17\n1 2\n2\n1 -29\n1 -19\n2\n1 22\n2\n2\n2\n2\n1 -15\n1 7\n1 -29\n2\n2\n1 -30\n2\n2\n1 -6\n2\n1 -25\n2\n1 -18\n2\n1 33\n1 23\n2\n2\n2",
"output": "25 22 27 24 29 26 31 28 33 30 35 32 1 34 3 36 5 2 7 4 9 6 11 8 13 10 15 12 17 14 19 16 21 18 23 20"
},
{
"input": "10 71\n1 -4\n1 -3\n2\n2\n2\n1 -3\n1 4\n2\n2\n2\n2\n1 5\n2\n2\n2\n2\n2\n1 1\n2\n1 2\n1 1\n2\n1 -5\n2\n2\n2\n2\n1 8\n1 -9\n1 -3\n1 2\n1 3\n1 -2\n1 -6\n2\n2\n1 -2\n2\n1 -6\n1 5\n1 2\n1 -10\n1 3\n2\n1 6\n2\n2\n1 4\n1 -8\n1 -4\n1 -1\n2\n2\n1 1\n2\n2\n1 3\n1 8\n1 7\n1 4\n2\n1 -10\n2\n2\n1 5\n1 9\n1 -5\n2\n2\n1 -2\n2",
"output": "1 2 3 4 5 6 7 8 9 10"
},
{
"input": "74 85\n2\n1 -69\n2\n2\n2\n2\n2\n1 74\n2\n2\n1 -41\n2\n2\n1 15\n2\n2\n2\n1 -12\n2\n1 -3\n1 28\n1 -46\n2\n1 -39\n2\n1 6\n2\n2\n1 -30\n2\n1 16\n1 30\n1 -50\n1 -17\n1 41\n1 56\n2\n1 -45\n1 -21\n1 63\n1 -7\n2\n1 -6\n1 26\n2\n1 -71\n2\n2\n2\n1 11\n2\n1 70\n1 13\n2\n1 -51\n1 -9\n1 -72\n1 55\n2\n1 3\n2\n2\n1 47\n2\n2\n2\n1 -6\n1 -37\n2\n2\n1 -1\n1 72\n2\n1 -23\n2\n2\n2\n1 70\n1 38\n2\n2\n1 74\n1 -1\n2\n1 -9",
"output": "71 18 73 20 1 22 3 24 5 26 7 28 9 30 11 32 13 34 15 36 17 38 19 40 21 42 23 44 25 46 27 48 29 50 31 52 33 54 35 56 37 58 39 60 41 62 43 64 45 66 47 68 49 70 51 72 53 74 55 2 57 4 59 6 61 8 63 10 65 12 67 14 69 16"
},
{
"input": "24 8\n1 17\n2\n1 -10\n2\n2\n2\n2\n1 19",
"output": "22 1 24 3 2 5 4 7 6 9 8 11 10 13 12 15 14 17 16 19 18 21 20 23"
},
{
"input": "242 11\n1 -202\n1 46\n2\n1 -144\n2\n1 134\n1 104\n2\n1 -32\n2\n1 36",
"output": "59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 ..."
},
{
"input": "364 57\n1 -101\n1 110\n1 -76\n1 329\n2\n2\n2\n1 -191\n1 97\n1 189\n1 305\n1 -313\n1 312\n1 -148\n2\n1 -104\n1 85\n1 -55\n1 -79\n1 230\n1 -94\n1 58\n1 -72\n2\n2\n2\n1 -104\n1 -351\n1 23\n2\n1 215\n2\n2\n2\n1 58\n1 -237\n2\n2\n2\n1 198\n2\n1 83\n2\n1 -205\n2\n2\n2\n2\n1 -110\n2\n2\n2\n2\n1 153\n1 -344\n1 -281\n1 -159",
"output": "218 215 220 217 222 219 224 221 226 223 228 225 230 227 232 229 234 231 236 233 238 235 240 237 242 239 244 241 246 243 248 245 250 247 252 249 254 251 256 253 258 255 260 257 262 259 264 261 266 263 268 265 270 267 272 269 274 271 276 273 278 275 280 277 282 279 284 281 286 283 288 285 290 287 292 289 294 291 296 293 298 295 300 297 302 299 304 301 306 303 308 305 310 307 312 309 314 311 316 313 318 315 320 317 322 319 324 321 326 323 328 325 330 327 332 329 334 331 336 333 338 335 340 337 342 339 344 341..."
},
{
"input": "6 5\n1 5\n1 5\n1 6\n1 6\n1 6",
"output": "3 4 5 6 1 2"
}
] | 61 | 4,608,000 | 0 | 4,767 |
|
47 | Triangular numbers | [
"brute force",
"math"
] | A. Triangular numbers | 2 | 256 | A triangular number is the number of dots in an equilateral triangle uniformly filled with dots. For example, three dots can be arranged in a triangle; thus three is a triangular number. The *n*-th triangular number is the number of dots in a triangle with *n* dots on a side. . You can learn more about these numbers from Wikipedia (http://en.wikipedia.org/wiki/Triangular_number).
Your task is to find out if a given integer is a triangular number. | The first line contains the single number *n* (1<=β€<=*n*<=β€<=500) β the given integer. | If the given integer is a triangular number output YES, otherwise output NO. | [
"1\n",
"2\n",
"3\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "1",
"output": "YES"
},
{
"input": "2",
"output": "NO"
},
{
"input": "3",
"output": "YES"
},
{
"input": "4",
"output": "NO"
},
{
"input": "5",
"output": "NO"
},
{
"input": "6",
"output": "YES"
},
{
"input": "7",
"output": "NO"
},
{
"input": "8",
"output": "NO"
},
{
"input": "12",
"output": "NO"
},
{
"input": "10",
"output": "YES"
},
{
"input": "11",
"output": "NO"
},
{
"input": "9",
"output": "NO"
},
{
"input": "14",
"output": "NO"
},
{
"input": "15",
"output": "YES"
},
{
"input": "16",
"output": "NO"
},
{
"input": "20",
"output": "NO"
},
{
"input": "21",
"output": "YES"
},
{
"input": "22",
"output": "NO"
},
{
"input": "121",
"output": "NO"
},
{
"input": "135",
"output": "NO"
},
{
"input": "136",
"output": "YES"
},
{
"input": "137",
"output": "NO"
},
{
"input": "152",
"output": "NO"
},
{
"input": "153",
"output": "YES"
},
{
"input": "154",
"output": "NO"
},
{
"input": "171",
"output": "YES"
},
{
"input": "189",
"output": "NO"
},
{
"input": "190",
"output": "YES"
},
{
"input": "191",
"output": "NO"
},
{
"input": "210",
"output": "YES"
},
{
"input": "211",
"output": "NO"
},
{
"input": "231",
"output": "YES"
},
{
"input": "232",
"output": "NO"
},
{
"input": "252",
"output": "NO"
},
{
"input": "253",
"output": "YES"
},
{
"input": "254",
"output": "NO"
},
{
"input": "275",
"output": "NO"
},
{
"input": "276",
"output": "YES"
},
{
"input": "277",
"output": "NO"
},
{
"input": "299",
"output": "NO"
},
{
"input": "300",
"output": "YES"
},
{
"input": "301",
"output": "NO"
},
{
"input": "324",
"output": "NO"
},
{
"input": "325",
"output": "YES"
},
{
"input": "326",
"output": "NO"
},
{
"input": "350",
"output": "NO"
},
{
"input": "351",
"output": "YES"
},
{
"input": "352",
"output": "NO"
},
{
"input": "377",
"output": "NO"
},
{
"input": "378",
"output": "YES"
},
{
"input": "406",
"output": "YES"
},
{
"input": "434",
"output": "NO"
},
{
"input": "435",
"output": "YES"
},
{
"input": "464",
"output": "NO"
},
{
"input": "465",
"output": "YES"
},
{
"input": "495",
"output": "NO"
},
{
"input": "496",
"output": "YES"
},
{
"input": "497",
"output": "NO"
},
{
"input": "331",
"output": "NO"
},
{
"input": "413",
"output": "NO"
},
{
"input": "177",
"output": "NO"
},
{
"input": "122",
"output": "NO"
},
{
"input": "478",
"output": "NO"
},
{
"input": "188",
"output": "NO"
},
{
"input": "21",
"output": "YES"
},
{
"input": "474",
"output": "NO"
},
{
"input": "21",
"output": "YES"
},
{
"input": "48",
"output": "NO"
},
{
"input": "498",
"output": "NO"
},
{
"input": "499",
"output": "NO"
},
{
"input": "500",
"output": "NO"
}
] | 92 | 0 | 3.977 | 4,772 |
113 | Grammar Lessons | [
"implementation",
"strings"
] | A. Grammar Lessons | 5 | 256 | Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules:
- There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb. - There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine. - Masculine adjectives end with -lios, and feminine adjectives end with -liala. - Masculine nouns end with -etr, and feminime nouns end with -etra. - Masculine verbs end with -initis, and feminime verbs end with -inites. - Thus, each word in the Petya's language has one of the six endings, given above. There are no other endings in Petya's language. - It is accepted that the whole word consists of an ending. That is, words "lios", "liala", "etr" and so on belong to the Petya's language. - There aren't any punctuation marks, grammatical tenses, singular/plural forms or other language complications. - A sentence is either exactly one valid language word or exactly one statement.
Statement is any sequence of the Petya's language, that satisfy both conditions:
- Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs. - All words in the statement should have the same gender.
After Petya's friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya's language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn't feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya's language. | The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105.
It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya's language. | If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes). | [
"petr\n",
"etis atis animatis etis atis amatis\n",
"nataliala kataliala vetra feinites\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "petr",
"output": "YES"
},
{
"input": "etis atis animatis etis atis amatis",
"output": "NO"
},
{
"input": "nataliala kataliala vetra feinites",
"output": "YES"
},
{
"input": "qweasbvflios",
"output": "YES"
},
{
"input": "lios lios petr initis qwe",
"output": "NO"
},
{
"input": "lios initis",
"output": "NO"
},
{
"input": "petr initis lios",
"output": "NO"
},
{
"input": "petra petra petra",
"output": "NO"
},
{
"input": "in",
"output": "NO"
},
{
"input": "liala petra initis",
"output": "NO"
},
{
"input": "liala petra inites",
"output": "YES"
},
{
"input": "liala initis",
"output": "NO"
},
{
"input": "liala petra petr inites",
"output": "NO"
},
{
"input": "liala petr inites",
"output": "NO"
},
{
"input": "llilitos",
"output": "NO"
},
{
"input": "umeszdawsvgkjhlqwzentsphxqhdungbylhnikwviuhccbstghhxlmvcjznnkjqkugsdysjbedwpmsmxmgxlrlxctnebtbwrsvgjktkrosffwymovxvsgfmmqwfflpvbumozikroxrdgwjrnstngstxbiyyuxehrhviteptedlmyetr",
"output": "YES"
},
{
"input": "i i i i i i i i i i i i i i i a a a a a a v v v v v v v v v v v",
"output": "NO"
},
{
"input": "fbvzqonvdlqdanwliolaqfj sbauorbinites xkbfnfinitespjy phbexglblzpobtqpisyijycmtliola aosinites lbpjiwcjoqyuhglthloiniteswb mjtxhoofohzzgefvhsywojcuxtetxmojrlktodhbgyrkeejgjzxkzyvrxwmyaqkeoqnvusnlrsfffrzeoqjdfumolhksqkrtzwhnforgpenziokrxlnhcapbbupctlmuetrani pigxerwetupjbkvlmgnjhdfjliolanz tqhaidxbqmdaeincxjuliola",
"output": "NO"
},
{
"input": "mfrmqetr",
"output": "YES"
},
{
"input": "hnwvfllholxfialiola cknjtxpliola daliola gqfapnhmmworliola qhetra qrisbexsrefcwzoxqwxrevinites wwldqkqhvrgwplqinites nqdpoauitczttxoinites fgbmdfpxkhahkinites",
"output": "NO"
},
{
"input": "kcymcpgqdxkudadewddualeemhixhsdazudnjdmuvxvrlrbrpsdpxpagmrogplltnifrtomdtahxwadguvetxaqkvsvnoyhowirnluhmyewzapirnpfdisvhtbenxmfezahqoflkjrfqjubwdfktnpeirodwubftzlcczzavfiooihzvnqincndisudihvbcaxptrwovekmhiiwsgzgbxydvuldlnktxtltrlajjzietkxbnhetra",
"output": "YES"
},
{
"input": "dosiydnwxemojaavfdvlwsyhzqywqjutovygtlcleklhybczhjqfzxwdmlwqwcqqyfjkzhsizlmdarrfronxqkcknwpkvhdlgatdyjisjoopvngpjggldxjfxaauoxmqirkuphydyweoixftstlozaoywnxgriscudwlokncbmaebpssccmmmfjennyjaryqlzjknnklqketra",
"output": "YES"
},
{
"input": "etretra linites",
"output": "YES"
},
{
"input": "petretra petr",
"output": "NO"
},
{
"input": "lialalios petraveryfunnypetr",
"output": "YES"
},
{
"input": "petropetrapetr petra",
"output": "NO"
},
{
"input": "lios petrnonono",
"output": "NO"
},
{
"input": "lios petr initisandinitisandliala petrainitis",
"output": "NO"
},
{
"input": "petro",
"output": "NO"
},
{
"input": "petr initesinitis",
"output": "YES"
},
{
"input": "lios initis",
"output": "NO"
},
{
"input": "liala initespetra",
"output": "YES"
},
{
"input": "lios petrapetr",
"output": "YES"
},
{
"input": "initis petr",
"output": "NO"
},
{
"input": "lioslialapetrpetrainitisinitesliosliala initesinitislioslialapetrpetrainitisinitetra",
"output": "YES"
},
{
"input": "veryfunnyprefixpetr",
"output": "YES"
},
{
"input": "veryfunnyprefixpetra",
"output": "YES"
},
{
"input": "veryfunnyprefixinitis",
"output": "YES"
},
{
"input": "veryfunnyprefixinites",
"output": "YES"
},
{
"input": "veryfunnyprefixliala",
"output": "YES"
},
{
"input": "veryfunnyprefixlios",
"output": "YES"
},
{
"input": "veryfunnyprefixlialas",
"output": "NO"
},
{
"input": "veryfunnyprefixliala veryfunnyprefixpetretra",
"output": "YES"
},
{
"input": "veryfunnyprefixlios veryfunnyprefixinitisetr",
"output": "YES"
},
{
"input": "veryfunnyprefixlios aabbinitis",
"output": "NO"
},
{
"input": "veryfunnyprefixlios inites",
"output": "NO"
},
{
"input": "lios petr initis",
"output": "YES"
},
{
"input": "liala etra inites",
"output": "YES"
},
{
"input": "lios",
"output": "YES"
},
{
"input": "liala",
"output": "YES"
},
{
"input": "initis",
"output": "YES"
},
{
"input": "inites",
"output": "YES"
},
{
"input": "tes",
"output": "NO"
},
{
"input": "tr",
"output": "NO"
},
{
"input": "a",
"output": "NO"
},
{
"input": "lios lios",
"output": "NO"
},
{
"input": "lios",
"output": "YES"
},
{
"input": "liala",
"output": "YES"
},
{
"input": "petr",
"output": "YES"
},
{
"input": "petra",
"output": "YES"
},
{
"input": "pinitis",
"output": "YES"
},
{
"input": "pinites",
"output": "YES"
},
{
"input": "plios pliala",
"output": "NO"
},
{
"input": "plios petr",
"output": "YES"
},
{
"input": "plios petra",
"output": "NO"
},
{
"input": "plios plios",
"output": "NO"
},
{
"input": "plios initis",
"output": "NO"
},
{
"input": "plios pinites",
"output": "NO"
},
{
"input": "pliala plios",
"output": "NO"
},
{
"input": "pliala ppliala",
"output": "NO"
},
{
"input": "pliala petr",
"output": "NO"
},
{
"input": "pliala petra",
"output": "YES"
},
{
"input": "pliala pinitis",
"output": "NO"
},
{
"input": "pliala pinites",
"output": "NO"
},
{
"input": "petr plios",
"output": "NO"
},
{
"input": "petr pliala",
"output": "NO"
},
{
"input": "petr petr",
"output": "NO"
},
{
"input": "petr petra",
"output": "NO"
},
{
"input": "petr pinitis",
"output": "YES"
},
{
"input": "petr pinites",
"output": "NO"
},
{
"input": "petra lios",
"output": "NO"
},
{
"input": "petra liala",
"output": "NO"
},
{
"input": "petra petr",
"output": "NO"
},
{
"input": "petra petra",
"output": "NO"
},
{
"input": "petra initis",
"output": "NO"
},
{
"input": "petra inites",
"output": "YES"
},
{
"input": "initis lios",
"output": "NO"
},
{
"input": "initis liala",
"output": "NO"
},
{
"input": "initis petr",
"output": "NO"
},
{
"input": "initis petra",
"output": "NO"
},
{
"input": "initis initis",
"output": "NO"
},
{
"input": "initis inites",
"output": "NO"
},
{
"input": "inites lios",
"output": "NO"
},
{
"input": "inites liala",
"output": "NO"
},
{
"input": "inites petr",
"output": "NO"
},
{
"input": "inites petra",
"output": "NO"
},
{
"input": "inites initis",
"output": "NO"
},
{
"input": "inites inites",
"output": "NO"
},
{
"input": "lios lios lios",
"output": "NO"
},
{
"input": "lios lios liala",
"output": "NO"
},
{
"input": "lios lios etr",
"output": "YES"
},
{
"input": "lios lios etra",
"output": "NO"
},
{
"input": "lios lios initis",
"output": "NO"
},
{
"input": "lios lios inites",
"output": "NO"
},
{
"input": "lios liala lios",
"output": "NO"
},
{
"input": "lios liala liala",
"output": "NO"
},
{
"input": "lios liala etr",
"output": "NO"
},
{
"input": "lios liala etra",
"output": "NO"
},
{
"input": "lios liala initis",
"output": "NO"
},
{
"input": "lios liala inites",
"output": "NO"
},
{
"input": "lios etr lios",
"output": "NO"
},
{
"input": "lios etr liala",
"output": "NO"
},
{
"input": "lios etr etr",
"output": "NO"
},
{
"input": "lios etr etra",
"output": "NO"
},
{
"input": "lios etr initis",
"output": "YES"
},
{
"input": "lios etr inites",
"output": "NO"
},
{
"input": "lios etra lios",
"output": "NO"
},
{
"input": "lios etra liala",
"output": "NO"
},
{
"input": "lios etra etr",
"output": "NO"
},
{
"input": "lios etra etra",
"output": "NO"
},
{
"input": "lios etra initis",
"output": "NO"
},
{
"input": "lios etra inites",
"output": "NO"
},
{
"input": "lios initis lios",
"output": "NO"
},
{
"input": "lios initis liala",
"output": "NO"
},
{
"input": "lios initis etr",
"output": "NO"
},
{
"input": "lios initis etra",
"output": "NO"
},
{
"input": "lios initis initis",
"output": "NO"
},
{
"input": "lios initis inites",
"output": "NO"
},
{
"input": "lios inites lios",
"output": "NO"
},
{
"input": "lios inites liala",
"output": "NO"
},
{
"input": "lios inites etr",
"output": "NO"
},
{
"input": "lios inites etra",
"output": "NO"
},
{
"input": "lios inites initis",
"output": "NO"
},
{
"input": "lios inites inites",
"output": "NO"
},
{
"input": "liala lios lios",
"output": "NO"
},
{
"input": "liala lios liala",
"output": "NO"
},
{
"input": "liala lios etr",
"output": "NO"
},
{
"input": "liala lios etra",
"output": "NO"
},
{
"input": "liala lios initis",
"output": "NO"
},
{
"input": "liala lios inites",
"output": "NO"
},
{
"input": "liala liala lios",
"output": "NO"
},
{
"input": "liala liala liala",
"output": "NO"
},
{
"input": "liala liala etr",
"output": "NO"
},
{
"input": "liala liala etra",
"output": "YES"
},
{
"input": "liala liala initis",
"output": "NO"
},
{
"input": "liala liala inites",
"output": "NO"
},
{
"input": "liala etr lios",
"output": "NO"
},
{
"input": "liala etr liala",
"output": "NO"
},
{
"input": "liala etr etr",
"output": "NO"
},
{
"input": "liala etr etra",
"output": "NO"
},
{
"input": "liala etr initis",
"output": "NO"
},
{
"input": "liala etr inites",
"output": "NO"
},
{
"input": "liala etra lios",
"output": "NO"
},
{
"input": "liala etra liala",
"output": "NO"
},
{
"input": "liala etra etr",
"output": "NO"
},
{
"input": "liala etra etra",
"output": "NO"
},
{
"input": "liala etra initis",
"output": "NO"
},
{
"input": "liala etra inites",
"output": "YES"
},
{
"input": "liala initis lios",
"output": "NO"
},
{
"input": "liala initis liala",
"output": "NO"
},
{
"input": "liala initis etr",
"output": "NO"
},
{
"input": "liala initis etra",
"output": "NO"
},
{
"input": "liala initis initis",
"output": "NO"
},
{
"input": "liala initis inites",
"output": "NO"
},
{
"input": "liala inites lios",
"output": "NO"
},
{
"input": "liala inites liala",
"output": "NO"
},
{
"input": "liala inites etr",
"output": "NO"
},
{
"input": "liala inites etra",
"output": "NO"
},
{
"input": "liala inites initis",
"output": "NO"
},
{
"input": "liala inites inites",
"output": "NO"
},
{
"input": "etr lios lios",
"output": "NO"
},
{
"input": "etr lios liala",
"output": "NO"
},
{
"input": "etr lios etr",
"output": "NO"
},
{
"input": "etr lios etra",
"output": "NO"
},
{
"input": "etr lios initis",
"output": "NO"
},
{
"input": "etr lios inites",
"output": "NO"
},
{
"input": "etr liala lios",
"output": "NO"
},
{
"input": "etr liala liala",
"output": "NO"
},
{
"input": "etr liala etr",
"output": "NO"
},
{
"input": "etr liala etra",
"output": "NO"
},
{
"input": "etr liala initis",
"output": "NO"
},
{
"input": "etr liala inites",
"output": "NO"
},
{
"input": "etr etr lios",
"output": "NO"
},
{
"input": "etr etr liala",
"output": "NO"
},
{
"input": "etr etr etr",
"output": "NO"
},
{
"input": "etr etr etra",
"output": "NO"
},
{
"input": "etr etr initis",
"output": "NO"
},
{
"input": "etr etr inites",
"output": "NO"
},
{
"input": "etr etra lios",
"output": "NO"
},
{
"input": "etr etra liala",
"output": "NO"
},
{
"input": "etr etra etr",
"output": "NO"
},
{
"input": "etr etra etra",
"output": "NO"
},
{
"input": "etr etra initis",
"output": "NO"
},
{
"input": "etr etra inites",
"output": "NO"
},
{
"input": "etr initis lios",
"output": "NO"
},
{
"input": "etr initis liala",
"output": "NO"
},
{
"input": "etr initis etr",
"output": "NO"
},
{
"input": "etr initis etra",
"output": "NO"
},
{
"input": "etr initis initis",
"output": "YES"
},
{
"input": "etr initis inites",
"output": "NO"
},
{
"input": "etr inites lios",
"output": "NO"
},
{
"input": "etr inites liala",
"output": "NO"
},
{
"input": "etr inites etr",
"output": "NO"
},
{
"input": "etr inites etra",
"output": "NO"
},
{
"input": "etr inites initis",
"output": "NO"
},
{
"input": "etr inites inites",
"output": "NO"
},
{
"input": "etra lios lios",
"output": "NO"
},
{
"input": "etra lios liala",
"output": "NO"
},
{
"input": "etra lios etr",
"output": "NO"
},
{
"input": "etra lios etra",
"output": "NO"
},
{
"input": "etra lios initis",
"output": "NO"
},
{
"input": "etra lios inites",
"output": "NO"
},
{
"input": "etra liala lios",
"output": "NO"
},
{
"input": "etra liala liala",
"output": "NO"
},
{
"input": "etra liala etr",
"output": "NO"
},
{
"input": "etra liala etra",
"output": "NO"
},
{
"input": "etra liala initis",
"output": "NO"
},
{
"input": "etra liala inites",
"output": "NO"
},
{
"input": "etra etr lios",
"output": "NO"
},
{
"input": "etra etr liala",
"output": "NO"
},
{
"input": "etra etr etr",
"output": "NO"
},
{
"input": "etra etr etra",
"output": "NO"
},
{
"input": "etra etr initis",
"output": "NO"
},
{
"input": "etra etr inites",
"output": "NO"
},
{
"input": "etra etra lios",
"output": "NO"
},
{
"input": "etra etra liala",
"output": "NO"
},
{
"input": "etra etra etr",
"output": "NO"
},
{
"input": "etra etra etra",
"output": "NO"
},
{
"input": "etra etra initis",
"output": "NO"
},
{
"input": "etra etra inites",
"output": "NO"
},
{
"input": "etra initis lios",
"output": "NO"
},
{
"input": "etra initis liala",
"output": "NO"
},
{
"input": "etra initis etr",
"output": "NO"
},
{
"input": "etra initis etra",
"output": "NO"
},
{
"input": "etra initis initis",
"output": "NO"
},
{
"input": "etra initis inites",
"output": "NO"
},
{
"input": "etra inites lios",
"output": "NO"
},
{
"input": "etra inites liala",
"output": "NO"
},
{
"input": "etra inites etr",
"output": "NO"
},
{
"input": "etra inites etra",
"output": "NO"
},
{
"input": "etra inites initis",
"output": "NO"
},
{
"input": "etra inites inites",
"output": "YES"
},
{
"input": "initis lios lios",
"output": "NO"
},
{
"input": "initis lios liala",
"output": "NO"
},
{
"input": "initis lios etr",
"output": "NO"
},
{
"input": "initis lios etra",
"output": "NO"
},
{
"input": "initis lios initis",
"output": "NO"
},
{
"input": "initis lios inites",
"output": "NO"
},
{
"input": "initis liala lios",
"output": "NO"
},
{
"input": "initis liala liala",
"output": "NO"
},
{
"input": "initis liala etr",
"output": "NO"
},
{
"input": "initis liala etra",
"output": "NO"
},
{
"input": "initis liala initis",
"output": "NO"
},
{
"input": "initis liala inites",
"output": "NO"
},
{
"input": "initis etr lios",
"output": "NO"
},
{
"input": "initis etr liala",
"output": "NO"
},
{
"input": "initis etr etr",
"output": "NO"
},
{
"input": "initis etr etra",
"output": "NO"
},
{
"input": "initis etr initis",
"output": "NO"
},
{
"input": "initis etr inites",
"output": "NO"
},
{
"input": "initis etra lios",
"output": "NO"
},
{
"input": "initis etra liala",
"output": "NO"
},
{
"input": "initis etra etr",
"output": "NO"
},
{
"input": "initis etra etra",
"output": "NO"
},
{
"input": "initis etra initis",
"output": "NO"
},
{
"input": "initis etra inites",
"output": "NO"
},
{
"input": "initis initis lios",
"output": "NO"
},
{
"input": "initis initis liala",
"output": "NO"
},
{
"input": "initis initis etr",
"output": "NO"
},
{
"input": "initis initis etra",
"output": "NO"
},
{
"input": "initis initis initis",
"output": "NO"
},
{
"input": "initis initis inites",
"output": "NO"
},
{
"input": "initis inites lios",
"output": "NO"
},
{
"input": "initis inites liala",
"output": "NO"
},
{
"input": "initis inites etr",
"output": "NO"
},
{
"input": "initis inites etra",
"output": "NO"
},
{
"input": "initis inites initis",
"output": "NO"
},
{
"input": "initis inites inites",
"output": "NO"
},
{
"input": "inites lios lios",
"output": "NO"
},
{
"input": "inites lios liala",
"output": "NO"
},
{
"input": "inites lios etr",
"output": "NO"
},
{
"input": "inites lios etra",
"output": "NO"
},
{
"input": "inites lios initis",
"output": "NO"
},
{
"input": "inites lios inites",
"output": "NO"
},
{
"input": "inites liala lios",
"output": "NO"
},
{
"input": "inites liala liala",
"output": "NO"
},
{
"input": "inites liala etr",
"output": "NO"
},
{
"input": "inites liala etra",
"output": "NO"
},
{
"input": "inites liala initis",
"output": "NO"
},
{
"input": "inites liala inites",
"output": "NO"
},
{
"input": "inites etr lios",
"output": "NO"
},
{
"input": "inites etr liala",
"output": "NO"
},
{
"input": "inites etr etr",
"output": "NO"
},
{
"input": "inites etr etra",
"output": "NO"
},
{
"input": "inites etr initis",
"output": "NO"
},
{
"input": "inites etr inites",
"output": "NO"
},
{
"input": "inites etra lios",
"output": "NO"
},
{
"input": "inites etra liala",
"output": "NO"
},
{
"input": "inites etra etr",
"output": "NO"
},
{
"input": "inites etra etra",
"output": "NO"
},
{
"input": "inites etra initis",
"output": "NO"
},
{
"input": "inites etra inites",
"output": "NO"
},
{
"input": "inites initis lios",
"output": "NO"
},
{
"input": "inites initis liala",
"output": "NO"
},
{
"input": "inites initis etr",
"output": "NO"
},
{
"input": "inites initis etra",
"output": "NO"
},
{
"input": "inites initis initis",
"output": "NO"
},
{
"input": "inites initis inites",
"output": "NO"
},
{
"input": "inites inites lios",
"output": "NO"
},
{
"input": "inites inites liala",
"output": "NO"
},
{
"input": "inites inites etr",
"output": "NO"
},
{
"input": "inites inites etra",
"output": "NO"
},
{
"input": "inites inites initis",
"output": "NO"
},
{
"input": "inites inites inites",
"output": "NO"
}
] | 62 | 4,505,600 | 0 | 4,778 |
219 | Special Offer! Super Price 999 Bourles! | [
"implementation"
] | null | null | Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.
Polycaprus calculated that the optimal celling price for such scissors would be *p* bourles. However, he read somewhere that customers are attracted by prices that say something like "Special Offer! Super price 999 bourles!". So Polycarpus decided to lower the price a little if it leads to the desired effect.
Polycarpus agrees to lower the price by no more than *d* bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.
Note, Polycarpus counts only the trailing nines in a price. | The first line contains two integers *p* and *d* (1<=β€<=*p*<=β€<=1018; 0<=β€<=*d*<=<<=*p*) β the initial price of scissors and the maximum possible price reduction.
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. | Print the required price β the maximum price that ends with the largest number of nines and that is less than *p* by no more than *d*.
The required number shouldn't have leading zeroes. | [
"1029 102\n",
"27191 17\n"
] | [
"999\n",
"27189\n"
] | none | [
{
"input": "1029 102",
"output": "999"
},
{
"input": "27191 17",
"output": "27189"
},
{
"input": "1 0",
"output": "1"
},
{
"input": "9 0",
"output": "9"
},
{
"input": "20 1",
"output": "19"
},
{
"input": "100 23",
"output": "99"
},
{
"input": "10281 1",
"output": "10281"
},
{
"input": "2111 21",
"output": "2099"
},
{
"input": "3021 112",
"output": "2999"
},
{
"input": "1000000000000000000 999999999999999999",
"output": "999999999999999999"
},
{
"input": "29287101 301",
"output": "29286999"
},
{
"input": "302918113 8113",
"output": "302917999"
},
{
"input": "23483247283432 47283432",
"output": "23483239999999"
},
{
"input": "47283432 7283432",
"output": "46999999"
},
{
"input": "7283432 7283431",
"output": "6999999"
},
{
"input": "2304324853947 5853947",
"output": "2304319999999"
},
{
"input": "2485348653485 123483",
"output": "2485348599999"
},
{
"input": "29845435345 34543",
"output": "29845429999"
},
{
"input": "2348723847234234 234829384234",
"output": "2348699999999999"
},
{
"input": "2348723847234234 234829384234",
"output": "2348699999999999"
},
{
"input": "596383801524465437 13997918422040",
"output": "596379999999999999"
},
{
"input": "621306590487786841 47851896849379",
"output": "621299999999999999"
},
{
"input": "990575220328844835 100861359807341",
"output": "990499999999999999"
},
{
"input": "403277728241895842 15097810739041",
"output": "403269999999999999"
},
{
"input": "287854791214303304 98046359947548",
"output": "287799999999999999"
},
{
"input": "847222126505823289 115713658562976",
"output": "847199999999999999"
},
{
"input": "991096248227872657 181679439312637",
"output": "990999999999999999"
},
{
"input": "954402996235787062 354162450334047",
"output": "954399999999999999"
},
{
"input": "220466716596033408 44952575147901",
"output": "220459999999999999"
},
{
"input": "559198116944738707 844709119308273",
"output": "558999999999999999"
},
{
"input": "363980380443991024 4242310030748",
"output": "363979999999999999"
},
{
"input": "733498827000355608 13253459808159",
"output": "733489999999999999"
},
{
"input": "757663489894439901 139905688448459",
"output": "757599999999999999"
},
{
"input": "30528581170507487 1199546082507",
"output": "30527999999999999"
},
{
"input": "534463403123444176 67776394133861",
"output": "534399999999999999"
},
{
"input": "399943891120381720 89545256475298",
"output": "399899999999999999"
},
{
"input": "697076786191991245 95935185412097",
"output": "696999999999999999"
},
{
"input": "495773842562930245 17116719198640",
"output": "495769999999999999"
},
{
"input": "343540186435799067 48368225269792",
"output": "343499999999999999"
},
{
"input": "393776794010351632 4138311260892",
"output": "393775999999999999"
},
{
"input": "830005749156754342 157633405415940",
"output": "829999999999999999"
},
{
"input": "735716632509713228 109839072010906",
"output": "735699999999999999"
},
{
"input": "925835698451819219 232827103605000",
"output": "925799999999999999"
},
{
"input": "362064657893189225 54298707317247",
"output": "362059999999999999"
},
{
"input": "286739242579659245 61808986676984",
"output": "286699999999999999"
},
{
"input": "234522568185994645 14536016333590",
"output": "234519999999999999"
},
{
"input": "989980699593228598 382407804389880",
"output": "989899999999999999"
},
{
"input": "953287447601143003 367647762226264",
"output": "952999999999999999"
},
{
"input": "369834331957505226 421031521866991",
"output": "369799999999999999"
},
{
"input": "433225528653135646 16671330805568",
"output": "433219999999999999"
},
{
"input": "664584428369850915 516656201621892",
"output": "664499999999999999"
},
{
"input": "100813383516253625 468493737928751",
"output": "100799999999999999"
},
{
"input": "63600749936231318 12287109070881",
"output": "63599999999999999"
},
{
"input": "196643334958802150 3659421793154",
"output": "196639999999999999"
},
{
"input": "803015192835672406 14043666502157",
"output": "803009999999999999"
},
{
"input": "43201857567928862 5891486380570",
"output": "43199999999999999"
},
{
"input": "142195487377202511 32209508975060",
"output": "142189999999999999"
},
{
"input": "159171676706847083 28512592184962",
"output": "159169999999999999"
},
{
"input": "377788117133266645 12127036235155",
"output": "377779999999999999"
},
{
"input": "949501478909148807 31763408418934",
"output": "949499999999999999"
},
{
"input": "955412075341421601 220849506773896",
"output": "955399999999999999"
},
{
"input": "652742935922718161 11045914932687",
"output": "652739999999999999"
},
{
"input": "371621017875752909 511452352707014",
"output": "371599999999999999"
},
{
"input": "979748686171802330 281906901894586",
"output": "979699999999999999"
},
{
"input": "987860891213585005 85386263418762",
"output": "987799999999999999"
},
{
"input": "59225847802373220 8605552735740",
"output": "59219999999999999"
},
{
"input": "22532595810287625 1459945485391",
"output": "22531999999999999"
},
{
"input": "191654878233371957 258451919478343",
"output": "191599999999999999"
},
{
"input": "796937674525939896 892734175683845",
"output": "796899999999999999"
},
{
"input": "166564871934000326 22888347028438",
"output": "166559999999999999"
},
{
"input": "559198116944738707 84470911930827",
"output": "559189999999999999"
},
{
"input": "559198116944738707 8447091193082",
"output": "559189999999999999"
},
{
"input": "559198116944738707 844709119308",
"output": "559197999999999999"
},
{
"input": "559198116944738707 84470911930",
"output": "559198099999999999"
},
{
"input": "559198116944738707 8447091193",
"output": "559198109999999999"
},
{
"input": "559198116944738707 844709119",
"output": "559198116899999999"
},
{
"input": "559198116944738707 84470911",
"output": "559198116899999999"
},
{
"input": "559198116944738707 8447091",
"output": "559198116939999999"
},
{
"input": "559198116944738707 844709",
"output": "559198116943999999"
},
{
"input": "559198116944738707 84470",
"output": "559198116944699999"
},
{
"input": "559198116944738707 8447",
"output": "559198116944737999"
},
{
"input": "559198116944738707 844",
"output": "559198116944737999"
},
{
"input": "559198116944738707 84",
"output": "559198116944738699"
},
{
"input": "559198116944738707 8",
"output": "559198116944738699"
},
{
"input": "559198116944738707 7",
"output": "559198116944738707"
},
{
"input": "559198116944738707 6",
"output": "559198116944738707"
},
{
"input": "559198116944738707 1",
"output": "559198116944738707"
},
{
"input": "559198116944738707 0",
"output": "559198116944738707"
},
{
"input": "559198116944738700 1",
"output": "559198116944738699"
},
{
"input": "559198116944738700 0",
"output": "559198116944738700"
},
{
"input": "559198116944738999 0",
"output": "559198116944738999"
},
{
"input": "559198116944738999 1",
"output": "559198116944738999"
},
{
"input": "199 100",
"output": "199"
},
{
"input": "99 10",
"output": "99"
},
{
"input": "10 1",
"output": "9"
},
{
"input": "18 17",
"output": "9"
},
{
"input": "199 198",
"output": "199"
},
{
"input": "1000000000000000000 0",
"output": "1000000000000000000"
},
{
"input": "59 3",
"output": "59"
},
{
"input": "9999 10",
"output": "9999"
},
{
"input": "999999999999999998 999999999999999997",
"output": "899999999999999999"
},
{
"input": "8 7",
"output": "8"
}
] | 310 | 0 | 0 | 4,785 |
|
988 | Points and Powers of Two | [
"brute force",
"math"
] | null | null | There are $n$ distinct points on a coordinate line, the coordinate of $i$-th point equals to $x_i$. Choose a subset of the given set of points such that the distance between each pair of points in a subset is an integral power of two. It is necessary to consider each pair of points, not only adjacent. Note that any subset containing one element satisfies the condition above. Among all these subsets, choose a subset with maximum possible size.
In other words, you have to choose the maximum possible number of points $x_{i_1}, x_{i_2}, \dots, x_{i_m}$ such that for each pair $x_{i_j}$, $x_{i_k}$ it is true that $|x_{i_j} - x_{i_k}| = 2^d$ where $d$ is some non-negative integer number (not necessarily the same for each pair of points). | The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the number of points.
The second line contains $n$ pairwise distinct integers $x_1, x_2, \dots, x_n$ ($-10^9 \le x_i \le 10^9$) β the coordinates of points. | In the first line print $m$ β the maximum possible number of points in a subset that satisfies the conditions described above.
In the second line print $m$ integers β the coordinates of points in the subset you have chosen.
If there are multiple answers, print any of them. | [
"6\n3 5 4 7 10 12\n",
"5\n-1 2 5 8 11\n"
] | [
"3\n7 3 5",
"1\n8\n"
] | In the first example the answer is $[7, 3, 5]$. Note, that $|7-3|=4=2^2$, $|7-5|=2=2^1$ and $|3-5|=2=2^1$. You can't find a subset having more points satisfying the required property. | [
{
"input": "6\n3 5 4 7 10 12",
"output": "3\n3 4 5 "
},
{
"input": "5\n-1 2 5 8 11",
"output": "1\n-1 "
},
{
"input": "1\n42",
"output": "1\n42 "
},
{
"input": "3\n0 -536870912 536870912",
"output": "3\n-536870912 0 536870912 "
},
{
"input": "2\n536870912 -536870912",
"output": "2\n-536870912 536870912 "
},
{
"input": "3\n1 2 3",
"output": "3\n1 2 3 "
},
{
"input": "4\n1 2 3 8",
"output": "3\n1 2 3 "
},
{
"input": "2\n1 2",
"output": "2\n1 2 "
},
{
"input": "3\n0 1 2",
"output": "3\n0 1 2 "
},
{
"input": "2\n-3 -2",
"output": "2\n-3 -2 "
},
{
"input": "2\n-4 -2",
"output": "2\n-4 -2 "
},
{
"input": "2\n2 1",
"output": "2\n1 2 "
},
{
"input": "1\n1",
"output": "1\n1 "
},
{
"input": "3\n0 2 6",
"output": "2\n0 2 "
},
{
"input": "3\n2 4 8",
"output": "2\n2 4 "
},
{
"input": "2\n1 0",
"output": "2\n0 1 "
},
{
"input": "3\n5 6 7",
"output": "3\n5 6 7 "
},
{
"input": "3\n-1 1 0",
"output": "3\n-1 0 1 "
}
] | 670 | 33,894,400 | 0 | 4,787 |
|
431 | k-Tree | [
"dp",
"implementation",
"trees"
] | null | null | Quite recently a creative student Lesha had a lecture on trees. After the lecture Lesha was inspired and came up with the tree of his own which he called a *k*-tree.
A *k*-tree is an infinite rooted tree where:
- each vertex has exactly *k* children; - each edge has some weight; - if we look at the edges that goes from some vertex to its children (exactly *k* edges), then their weights will equal 1,<=2,<=3,<=...,<=*k*.
The picture below shows a part of a 3-tree.
Help Dima find an answer to his question. As the number of ways can be rather large, print it modulo 1000000007 (109<=+<=7). | A single line contains three space-separated integers: *n*, *k* and *d* (1<=β€<=*n*,<=*k*<=β€<=100; 1<=β€<=*d*<=β€<=*k*). | Print a single integer β the answer to the problem modulo 1000000007 (109<=+<=7). | [
"3 3 2\n",
"3 3 3\n",
"4 3 2\n",
"4 5 2\n"
] | [
"3\n",
"1\n",
"6\n",
"7\n"
] | none | [
{
"input": "3 3 2",
"output": "3"
},
{
"input": "3 3 3",
"output": "1"
},
{
"input": "4 3 2",
"output": "6"
},
{
"input": "4 5 2",
"output": "7"
},
{
"input": "28 6 3",
"output": "110682188"
},
{
"input": "5 100 1",
"output": "16"
},
{
"input": "50 6 3",
"output": "295630102"
},
{
"input": "10 13 6",
"output": "48"
},
{
"input": "20 16 14",
"output": "236"
},
{
"input": "1 10 1",
"output": "1"
},
{
"input": "8 11 4",
"output": "47"
},
{
"input": "16 5 4",
"output": "16175"
},
{
"input": "5 26 17",
"output": "0"
},
{
"input": "35 47 42",
"output": "0"
},
{
"input": "11 6 2",
"output": "975"
},
{
"input": "54 60 16",
"output": "931055544"
},
{
"input": "47 5 1",
"output": "164058640"
},
{
"input": "70 6 1",
"output": "592826579"
},
{
"input": "40 77 77",
"output": "0"
},
{
"input": "96 9 6",
"output": "362487247"
},
{
"input": "52 46 4",
"output": "27907693"
},
{
"input": "74 41 28",
"output": "806604424"
},
{
"input": "100 100 100",
"output": "1"
},
{
"input": "99 99 5",
"output": "700732369"
},
{
"input": "100 100 1",
"output": "988185646"
},
{
"input": "98 98 64",
"output": "237643149"
},
{
"input": "28 74 2",
"output": "134217727"
},
{
"input": "86 69 62",
"output": "217513984"
},
{
"input": "9 17 14",
"output": "0"
},
{
"input": "73 72 12",
"output": "426374014"
},
{
"input": "41 98 76",
"output": "0"
},
{
"input": "1 1 1",
"output": "1"
},
{
"input": "1 100 100",
"output": "0"
},
{
"input": "1 100 1",
"output": "1"
},
{
"input": "1 100 2",
"output": "0"
},
{
"input": "2 100 2",
"output": "1"
},
{
"input": "2 100 1",
"output": "2"
},
{
"input": "50 50 1",
"output": "949480669"
},
{
"input": "100 50 50",
"output": "661237556"
},
{
"input": "3 2 2",
"output": "2"
},
{
"input": "100 50 3",
"output": "494224664"
},
{
"input": "90 97 24",
"output": "413496885"
},
{
"input": "31 8 8",
"output": "52532592"
},
{
"input": "78 90 38",
"output": "744021655"
},
{
"input": "100 13 11",
"output": "883875774"
},
{
"input": "100 45 8",
"output": "367847193"
},
{
"input": "31 8 4",
"output": "924947104"
},
{
"input": "35 95 9",
"output": "927164672"
},
{
"input": "45 5 3",
"output": "252804490"
},
{
"input": "1 5 5",
"output": "0"
},
{
"input": "89 75 59",
"output": "179807625"
},
{
"input": "90 100 30",
"output": "697322870"
},
{
"input": "89 73 32",
"output": "152673180"
},
{
"input": "100 90 80",
"output": "11531520"
}
] | 77 | 2,150,400 | 3 | 4,795 |
|
583 | Robot's Task | [
"greedy",
"implementation"
] | null | null | Robot Doc is located in the hall, with *n* computers stand in a line, numbered from left to right from 1 to *n*. Each computer contains exactly one piece of information, each of which Doc wants to get eventually. The computers are equipped with a security system, so to crack the *i*-th of them, the robot needs to collect at least *a**i* any pieces of information from the other computers. Doc can hack the computer only if he is right next to it.
The robot is assembled using modern technologies and can move along the line of computers in either of the two possible directions, but the change of direction requires a large amount of resources from Doc. Tell the minimum number of changes of direction, which the robot will have to make to collect all *n* parts of information if initially it is next to computer with number 1.
It is guaranteed that there exists at least one sequence of the robot's actions, which leads to the collection of all information. Initially Doc doesn't have any pieces of information. | The first line contains number *n* (1<=β€<=*n*<=β€<=1000). The second line contains *n* non-negative integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=<<=*n*), separated by a space. It is guaranteed that there exists a way for robot to collect all pieces of the information. | Print a single number β the minimum number of changes in direction that the robot will have to make in order to collect all *n* parts of information. | [
"3\n0 2 0\n",
"5\n4 2 3 0 1\n",
"7\n0 3 1 0 5 2 6\n"
] | [
"1\n",
"3\n",
"2\n"
] | In the first sample you can assemble all the pieces of information in the optimal manner by assembling first the piece of information in the first computer, then in the third one, then change direction and move to the second one, and then, having 2 pieces of information, collect the last piece.
In the second sample to collect all the pieces of information in the optimal manner, Doc can go to the fourth computer and get the piece of information, then go to the fifth computer with one piece and get another one, then go to the second computer in the same manner, then to the third one and finally, to the first one. Changes of direction will take place before moving from the fifth to the second computer, then from the second to the third computer, then from the third to the first computer.
In the third sample the optimal order of collecting parts from computers can look like that: 1->3->4->6->2->5->7. | [
{
"input": "3\n0 2 0",
"output": "1"
},
{
"input": "5\n4 2 3 0 1",
"output": "3"
},
{
"input": "7\n0 3 1 0 5 2 6",
"output": "2"
},
{
"input": "1\n0",
"output": "0"
},
{
"input": "2\n0 1",
"output": "0"
},
{
"input": "10\n0 0 0 0 0 0 0 0 0 0",
"output": "0"
},
{
"input": "3\n0 2 1",
"output": "1"
},
{
"input": "10\n7 1 9 3 5 8 6 0 2 4",
"output": "9"
},
{
"input": "10\n1 3 5 7 9 8 6 4 2 0",
"output": "9"
},
{
"input": "10\n5 0 0 1 3 2 2 2 5 7",
"output": "1"
},
{
"input": "10\n8 6 5 3 9 7 1 4 2 0",
"output": "8"
},
{
"input": "10\n1 2 4 5 0 1 3 7 1 4",
"output": "2"
},
{
"input": "10\n3 4 8 9 5 1 2 0 6 7",
"output": "6"
},
{
"input": "10\n2 2 0 0 6 2 9 0 2 0",
"output": "2"
},
{
"input": "10\n1 7 5 3 2 6 0 8 4 9",
"output": "8"
},
{
"input": "9\n1 3 8 6 2 4 5 0 7",
"output": "7"
},
{
"input": "9\n1 3 5 7 8 6 4 2 0",
"output": "8"
},
{
"input": "9\n2 4 3 1 3 0 5 4 3",
"output": "3"
},
{
"input": "9\n3 5 6 8 7 0 4 2 1",
"output": "5"
},
{
"input": "9\n2 0 8 1 0 3 0 5 3",
"output": "2"
},
{
"input": "9\n6 2 3 7 4 8 5 1 0",
"output": "4"
},
{
"input": "9\n3 1 5 6 0 3 2 0 0",
"output": "2"
},
{
"input": "9\n2 6 4 1 0 8 5 3 7",
"output": "7"
},
{
"input": "100\n27 20 18 78 93 38 56 2 48 75 36 88 96 57 69 10 25 74 68 86 65 85 66 14 22 12 43 80 99 34 42 63 61 71 77 15 37 54 21 59 23 94 28 30 50 84 62 76 47 16 26 64 82 92 72 53 17 11 41 91 35 83 79 95 67 13 1 7 3 4 73 90 8 19 33 58 98 32 39 45 87 52 60 46 6 44 49 70 51 9 5 29 31 24 40 97 81 0 89 55",
"output": "69"
},
{
"input": "100\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99 98 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0",
"output": "99"
},
{
"input": "100\n13 89 81 0 62 1 59 92 29 13 1 37 2 8 53 15 20 34 12 70 0 85 97 55 84 60 37 54 14 65 22 69 30 22 95 44 59 85 50 80 9 71 91 93 74 21 11 78 28 21 40 81 76 24 26 60 48 85 61 68 89 76 46 73 34 52 98 29 4 38 94 51 5 55 6 27 74 27 38 37 82 70 44 89 51 59 30 37 15 55 63 78 42 39 71 43 4 10 2 13",
"output": "21"
},
{
"input": "100\n1 3 5 7 58 11 13 15 17 19 45 23 25 27 29 31 33 35 37 39 41 43 21 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 81 79 83 85 87 89 91 93 95 97 48 98 96 94 92 90 88 44 84 82 80 78 76 74 72 70 68 66 64 62 60 9 56 54 52 50 99 46 86 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0",
"output": "96"
},
{
"input": "100\n32 47 74 8 14 4 12 68 18 0 44 80 14 38 6 57 4 72 69 3 21 78 74 22 39 32 58 63 34 33 23 6 39 11 6 12 18 4 0 11 20 28 16 1 22 12 57 55 13 48 43 1 50 18 87 6 11 45 38 67 37 14 7 56 6 41 1 55 5 73 78 64 38 18 38 8 37 0 18 61 37 58 58 62 86 5 0 2 15 43 34 61 2 21 15 9 69 1 11 24",
"output": "4"
},
{
"input": "100\n40 3 55 7 6 77 13 46 17 64 21 54 25 27 91 41 1 15 37 82 23 43 42 47 26 95 53 5 11 59 61 9 78 67 69 58 73 0 36 79 60 83 2 87 63 33 71 89 97 99 98 93 56 92 19 88 86 84 39 28 65 20 34 76 51 94 66 12 62 49 96 72 24 52 48 50 44 35 74 31 38 57 81 32 22 80 70 29 30 18 68 16 14 90 10 8 85 4 45 75",
"output": "75"
},
{
"input": "100\n34 16 42 21 84 27 11 7 82 16 95 39 36 64 26 0 38 37 2 2 16 56 16 61 55 42 26 5 61 8 30 20 19 15 9 78 5 34 15 0 3 17 36 36 1 5 4 26 18 0 14 25 7 5 91 7 43 26 79 37 17 27 40 55 66 7 0 2 16 23 68 35 2 5 9 21 1 7 2 9 4 3 22 15 27 6 0 47 5 0 12 9 20 55 36 10 6 8 5 1",
"output": "3"
},
{
"input": "100\n35 53 87 49 13 24 93 20 5 11 31 32 40 52 96 46 1 25 66 69 28 88 84 82 70 9 75 39 26 21 18 29 23 57 90 16 48 22 95 0 58 43 7 73 8 62 63 30 64 92 79 3 6 94 34 12 76 99 67 55 56 97 14 91 68 36 44 78 41 71 86 89 47 74 4 45 98 37 80 33 83 27 42 59 72 54 17 60 51 81 15 77 65 50 10 85 61 19 38 2",
"output": "67"
},
{
"input": "99\n89 96 56 31 32 14 9 66 87 34 69 5 92 54 41 52 46 30 22 26 16 18 20 68 62 73 90 43 79 33 58 98 37 45 10 78 94 51 19 0 91 39 28 47 17 86 3 61 77 7 15 64 55 83 65 71 97 88 6 48 24 11 8 42 81 4 63 93 50 74 35 12 95 27 53 82 29 85 84 60 72 40 36 57 23 13 38 59 49 1 75 44 76 2 21 25 70 80 67",
"output": "75"
},
{
"input": "99\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 98 96 94 92 90 88 86 84 82 80 78 76 74 72 70 68 66 64 62 60 58 56 54 52 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0",
"output": "98"
},
{
"input": "99\n82 7 6 77 17 28 90 3 68 12 63 60 24 20 4 81 71 85 57 45 11 84 3 91 49 34 89 82 0 50 48 88 36 76 36 5 62 48 20 2 20 45 69 27 37 62 42 31 57 51 92 84 89 25 7 62 12 23 23 56 30 90 27 10 77 58 48 38 56 68 57 15 33 1 34 67 16 47 75 70 69 28 38 16 5 61 85 76 44 90 37 22 77 94 55 1 97 8 69",
"output": "22"
},
{
"input": "99\n1 51 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 42 43 45 47 49 3 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 98 96 94 92 90 88 86 84 82 80 8 76 74 72 70 68 66 22 62 60 58 56 54 52 0 48 46 44 41 40 38 36 34 32 30 28 26 24 64 20 18 16 14 12 10 78 6 4 2 50",
"output": "96"
},
{
"input": "99\n22 3 19 13 65 87 28 17 41 40 31 21 8 37 29 65 65 53 16 33 13 5 76 4 72 9 2 76 57 72 50 15 75 0 30 13 83 36 12 31 49 51 65 22 48 31 60 15 2 17 6 1 8 0 1 63 3 16 7 7 2 1 47 28 26 21 2 36 1 5 20 25 44 0 2 39 46 30 33 11 15 34 34 4 84 52 0 39 7 3 17 15 6 38 52 64 26 1 0",
"output": "3"
},
{
"input": "99\n24 87 25 82 97 11 37 15 23 19 34 17 76 13 45 89 33 1 27 78 63 43 54 47 49 2 42 41 75 83 61 90 65 67 21 71 60 57 77 62 81 58 85 69 3 91 68 55 72 93 29 94 66 16 88 86 84 53 14 39 35 44 9 70 80 92 56 79 74 5 64 31 52 50 48 46 51 59 40 38 36 96 32 30 28 95 7 22 20 18 26 73 12 10 8 6 4 98 0",
"output": "74"
},
{
"input": "99\n22 14 0 44 6 17 6 6 37 45 0 48 19 8 57 8 10 0 3 12 25 2 5 53 9 49 15 6 38 14 9 40 38 22 27 12 64 10 11 35 89 19 46 39 12 24 48 0 52 1 27 27 24 4 64 24 5 0 67 3 5 39 0 1 13 37 2 8 46 1 28 70 6 79 14 15 33 6 7 34 6 18 4 71 1 55 33 71 18 11 47 23 72 53 65 32 2 7 28",
"output": "3"
},
{
"input": "99\n28 59 73 89 52 27 0 20 36 12 83 95 31 24 54 94 49 14 51 34 50 93 13 1 2 68 63 48 41 81 23 43 18 9 16 38 33 60 62 3 40 85 72 69 90 98 11 37 22 44 35 6 21 39 82 10 64 66 96 42 74 30 8 67 97 46 84 32 17 57 75 71 5 26 4 55 58 29 7 15 45 19 92 91 78 65 88 25 86 80 77 87 79 53 47 70 56 76 61",
"output": "63"
}
] | 233 | 0 | 3 | 4,813 |
|
261 | Maxim and Discounts | [
"greedy",
"sortings"
] | null | null | Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems.
There are *m* types of discounts. We assume that the discounts are indexed from 1 to *m*. To use the discount number *i*, the customer takes a special basket, where he puts exactly *q**i* items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the *q**i* items in the cart.
Maxim now needs to buy *n* items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well.
Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts. | The first line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of discount types. The second line contains *m* integers: *q*1,<=*q*2,<=...,<=*q**m* (1<=β€<=*q**i*<=β€<=105).
The third line contains integer *n* (1<=β€<=*n*<=β€<=105) β the number of items Maxim needs. The fourth line contains *n* integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=104) β the items' prices.
The numbers in the lines are separated by single spaces. | In a single line print a single integer β the answer to the problem. | [
"1\n2\n4\n50 50 100 100\n",
"2\n2 3\n5\n50 50 50 50 50\n",
"1\n1\n7\n1 1 1 1 1 1 1\n"
] | [
"200\n",
"150\n",
"3\n"
] | In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200.
In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150. | [
{
"input": "1\n2\n4\n50 50 100 100",
"output": "200"
},
{
"input": "2\n2 3\n5\n50 50 50 50 50",
"output": "150"
},
{
"input": "1\n1\n7\n1 1 1 1 1 1 1",
"output": "3"
},
{
"input": "60\n7 4 20 15 17 6 2 2 3 18 13 14 16 11 13 12 6 10 14 1 16 6 4 9 10 8 10 15 16 13 13 9 16 11 5 4 11 1 20 5 11 20 19 9 14 13 10 6 6 9 2 13 11 4 1 6 8 18 10 3\n26\n2481 6519 9153 741 9008 6601 6117 1689 5911 2031 2538 5553 1358 6863 7521 4869 6276 5356 5305 6761 5689 7476 5833 257 2157 218",
"output": "44768"
},
{
"input": "88\n8 3 4 3 1 17 5 10 18 12 9 12 4 6 19 14 9 10 10 8 15 11 18 3 11 4 10 11 7 9 14 7 13 2 8 2 15 2 8 16 7 1 9 1 11 13 13 15 8 9 4 2 13 12 12 11 1 5 20 19 13 15 6 6 11 20 14 18 11 20 20 13 8 4 17 12 17 4 13 14 1 20 19 5 7 3 19 16\n33\n7137 685 2583 6751 2104 2596 2329 9948 7961 9545 1797 6507 9241 3844 5657 1887 225 7310 1165 6335 5729 5179 8166 9294 3281 8037 1063 6711 8103 7461 4226 2894 9085",
"output": "61832"
},
{
"input": "46\n11 6 8 8 11 8 2 8 17 3 16 1 9 12 18 2 2 5 17 19 3 9 8 19 2 4 2 15 2 11 13 13 8 6 10 12 7 7 17 15 10 19 7 7 19 6\n71\n6715 8201 9324 276 8441 2378 4829 9303 5721 3895 8193 7725 1246 8845 6863 2897 5001 5055 2745 596 9108 4313 1108 982 6483 7256 4313 8981 9026 9885 2433 2009 8441 7441 9044 6969 2065 6721 424 5478 9128 5921 11 6201 3681 4876 3369 6205 4865 8201 9751 371 2881 7995 641 5841 3595 6041 2403 1361 5121 3801 8031 7909 3809 7741 1026 9633 8711 1907 6363",
"output": "129008"
},
{
"input": "18\n16 16 20 12 13 10 14 15 4 5 6 8 4 11 12 11 16 7\n15\n371 2453 905 1366 6471 4331 4106 2570 4647 1648 7911 2147 1273 6437 3393",
"output": "38578"
},
{
"input": "2\n12 4\n28\n5366 5346 1951 3303 1613 5826 8035 7079 7633 6155 9811 9761 3207 4293 3551 5245 7891 4463 3981 2216 3881 1751 4495 96 671 1393 1339 4241",
"output": "89345"
},
{
"input": "57\n3 13 20 17 18 18 17 2 17 8 20 2 11 12 11 14 4 20 9 20 14 19 20 4 4 8 8 18 17 16 18 10 4 7 9 8 10 8 20 4 11 8 12 16 16 4 11 12 16 1 6 14 11 12 19 8 20\n7\n5267 7981 1697 826 6889 1949 2413",
"output": "11220"
},
{
"input": "48\n14 2 5 3 10 10 5 6 14 8 19 13 4 4 3 13 18 19 9 16 3 1 14 9 13 10 13 4 12 11 8 2 18 20 14 11 3 11 18 11 4 2 7 2 18 19 2 8\n70\n9497 5103 1001 2399 5701 4053 3557 8481 1736 4139 5829 1107 6461 4089 5936 7961 6017 1416 1191 4635 4288 5605 8857 1822 71 1435 2837 5523 6993 2404 2840 8251 765 5678 7834 8595 3091 7073 8673 2299 2685 7729 8017 3171 9155 431 3773 7927 671 4063 1123 5384 2721 7901 2315 5199 8081 7321 8196 2887 9384 56 7501 1931 4769 2055 7489 3681 6321 8489",
"output": "115395"
},
{
"input": "1\n1\n1\n1",
"output": "1"
},
{
"input": "1\n2\n1\n1",
"output": "1"
},
{
"input": "1\n1\n3\n3 1 1",
"output": "3"
}
] | 62 | 0 | 0 | 4,815 |
|
884 | Boxes And Balls | [
"data structures",
"greedy"
] | null | null | Ivan has *n* different boxes. The first of them contains some balls of *n* different colors.
Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every *i* (1<=β€<=*i*<=β€<=*n*) *i*-th box will contain all balls with color *i*.
In order to do this, Ivan will make some turns. Each turn he does the following:
1. Ivan chooses any non-empty box and takes all balls from this box; 1. Then Ivan chooses any *k* empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into *k* non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either *k*<==<=2 or *k*<==<=3.
The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.
Help Ivan to determine the minimum possible penalty of the game! | The first line contains one integer number *n* (1<=β€<=*n*<=β€<=200000) β the number of boxes and colors.
The second line contains *n* integer numbers *a*1, *a*2, ..., *a**n* (1<=β€<=*a**i*<=β€<=109), where *a**i* is the number of balls with color *i*. | Print one number β the minimum possible penalty of the game. | [
"3\n1 2 3\n",
"4\n2 3 4 5\n"
] | [
"6\n",
"19\n"
] | In the first example you take all the balls from the first box, choose *k*β=β3 and sort all colors to corresponding boxes. Penalty is 6.
In the second example you make two turns:
1. Take all the balls from the first box, choose *k*β=β3, put balls of color 3 to the third box, of color 4 β to the fourth box and the rest put back into the first box. Penalty is 14; 1. Take all the balls from the first box, choose *k*β=β2, put balls of color 1 to the first box, of color 2 β to the second box. Penalty is 5.
Total penalty is 19. | [
{
"input": "3\n1 2 3",
"output": "6"
},
{
"input": "4\n2 3 4 5",
"output": "19"
},
{
"input": "6\n1 4 4 4 4 4",
"output": "38"
},
{
"input": "8\n821407370 380061316 428719552 90851747 825473738 704702117 845629927 245820158",
"output": "8176373828"
},
{
"input": "1\n10",
"output": "0"
},
{
"input": "1\n4",
"output": "0"
},
{
"input": "1\n12312",
"output": "0"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n3 4",
"output": "7"
}
] | 514 | 15,974,400 | 3 | 4,819 |
|
928 | Chat | [
"*special",
"dp"
] | null | null | There are times you recall a good old friend and everything you've come through together. Luckily there are social networksΒ β they store all your message history making it easy to know what you argued over 10 years ago.
More formal, your message history is a sequence of messages ordered by time sent numbered from 1 to *n* where *n* is the total number of messages in the chat.
Each message might contain a link to an earlier message which it is a reply to. When opening a message *x* or getting a link to it, the dialogue is shown in such a way that *k* previous messages, message *x* and *k* next messages are visible (with respect to message *x*). In case there are less than *k* messages somewhere, they are yet all shown.
Digging deep into your message history, you always read all visible messages and then go by the link in the current message *x* (if there is one) and continue reading in the same manner.
Determine the number of messages you'll read if your start from message number *t* for all *t* from 1 to *n*. Calculate these numbers independently. If you start with message *x*, the initial configuration is *x* itself, *k* previous and *k* next messages. Messages read multiple times are considered as one. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=105, 0<=β€<=*k*<=β€<=*n*) β the total amount of messages and the number of previous and next messages visible.
The second line features a sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=<<=*i*), where *a**i* denotes the *i*-th message link destination or zero, if there's no link from *i*. All messages are listed in chronological order. It's guaranteed that the link from message *x* goes to message with number strictly less than *x*. | Print *n* integers with *i*-th denoting the number of distinct messages you can read starting from message *i* and traversing the links while possible. | [
"6 0\n0 1 1 2 3 2\n",
"10 1\n0 1 0 3 4 5 2 3 7 0\n",
"2 2\n0 1\n"
] | [
"1 2 2 3 3 3 \n",
"2 3 3 4 5 6 6 6 8 2 \n",
"2 2 \n"
] | Consider *i*β=β6 in sample case one. You will read message 6, then 2, then 1 and then there will be no link to go.
In the second sample case *i*β=β6 gives you messages 5,β6,β7 since *k*β=β1, then 4,β5,β6, then 2,β3,β4 and then the link sequence breaks. The number of distinct messages here is equal to 6. | [
{
"input": "6 0\n0 1 1 2 3 2",
"output": "1 2 2 3 3 3 "
},
{
"input": "10 1\n0 1 0 3 4 5 2 3 7 0",
"output": "2 3 3 4 5 6 6 6 8 2 "
},
{
"input": "2 2\n0 1",
"output": "2 2 "
},
{
"input": "1 1\n0",
"output": "1 "
},
{
"input": "5 2\n0 1 2 3 1",
"output": "3 4 5 5 5 "
},
{
"input": "30 1\n0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 2 0 0 0 0 0 2 1 0",
"output": "2 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 3 3 3 3 3 6 3 3 3 3 3 6 5 2 "
},
{
"input": "100 5\n0 1 1 1 0 5 6 6 8 8 9 11 12 11 8 0 0 14 6 16 7 21 15 23 15 24 0 0 0 28 0 29 26 27 19 0 0 21 37 32 40 30 37 34 39 38 34 38 0 0 41 24 45 47 0 33 46 26 31 0 21 57 57 31 63 63 25 59 65 56 68 0 30 55 55 0 70 43 59 49 59 79 66 74 0 11 65 0 80 63 0 84 73 49 73 81 0 86 76 98",
"output": "6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 11 11 23 22 15 23 24 28 29 30 31 11 11 11 13 11 14 38 18 33 11 11 34 13 22 23 24 17 28 19 42 29 44 11 11 33 40 27 36 11 49 53 42 22 11 34 58 59 22 61 62 41 31 65 60 34 11 24 22 22 11 67 28 33 22 33 36 73 32 11 27 72 11 31 70 11 40 35 22 35 43 9 35 18 35 "
},
{
"input": "2 2\n0 0",
"output": "2 2 "
},
{
"input": "2 1\n0 0",
"output": "2 2 "
},
{
"input": "2 1\n0 1",
"output": "2 2 "
},
{
"input": "2 0\n0 0",
"output": "1 1 "
},
{
"input": "2 0\n0 1",
"output": "1 2 "
},
{
"input": "3 0\n0 0 0",
"output": "1 1 1 "
},
{
"input": "3 0\n0 0 1",
"output": "1 1 2 "
},
{
"input": "3 0\n0 0 2",
"output": "1 1 2 "
},
{
"input": "3 0\n0 1 0",
"output": "1 2 1 "
},
{
"input": "3 0\n0 1 1",
"output": "1 2 2 "
},
{
"input": "3 0\n0 1 2",
"output": "1 2 3 "
},
{
"input": "3 1\n0 0 0",
"output": "2 3 2 "
},
{
"input": "3 1\n0 0 1",
"output": "2 3 3 "
},
{
"input": "3 1\n0 0 2",
"output": "2 3 3 "
},
{
"input": "3 1\n0 1 0",
"output": "2 3 2 "
},
{
"input": "3 1\n0 1 1",
"output": "2 3 3 "
},
{
"input": "3 1\n0 1 2",
"output": "2 3 3 "
},
{
"input": "3 2\n0 0 0",
"output": "3 3 3 "
},
{
"input": "3 2\n0 0 1",
"output": "3 3 3 "
},
{
"input": "3 2\n0 0 2",
"output": "3 3 3 "
},
{
"input": "3 2\n0 1 0",
"output": "3 3 3 "
},
{
"input": "3 2\n0 1 1",
"output": "3 3 3 "
},
{
"input": "3 2\n0 1 2",
"output": "3 3 3 "
},
{
"input": "3 3\n0 0 0",
"output": "3 3 3 "
},
{
"input": "3 3\n0 0 1",
"output": "3 3 3 "
},
{
"input": "3 3\n0 0 2",
"output": "3 3 3 "
},
{
"input": "3 3\n0 1 0",
"output": "3 3 3 "
},
{
"input": "3 3\n0 1 1",
"output": "3 3 3 "
},
{
"input": "3 3\n0 1 2",
"output": "3 3 3 "
},
{
"input": "10 3\n0 0 0 0 0 0 0 4 0 4",
"output": "4 5 6 7 7 7 7 10 5 10 "
},
{
"input": "20 2\n0 0 0 0 2 1 0 3 0 1 1 11 0 10 0 0 9 17 9 0",
"output": "3 4 5 5 7 8 5 10 5 8 8 9 5 12 5 5 10 11 9 3 "
},
{
"input": "40 0\n0 1 2 3 4 5 0 0 0 0 0 11 12 0 14 10 0 16 15 0 19 21 22 0 23 25 25 24 24 29 29 0 0 31 0 35 31 36 34 29",
"output": "1 2 3 4 5 6 1 1 1 1 1 2 3 1 2 2 1 3 3 1 4 5 6 1 7 8 8 2 2 3 3 1 1 4 1 2 4 3 5 3 "
}
] | 295 | 30,924,800 | 0 | 4,826 |
|
784 | Kids' Riddle | [
"*special"
] | null | null | Programmers' kids solve this riddle in 5-10 minutes. How fast can you do it? | The input contains a single integer *n* (0<=β€<=*n*<=β€<=2000000000). | Output a single integer. | [
"11\n",
"14\n",
"61441\n",
"571576\n",
"2128506\n"
] | [
"2\n",
"0\n",
"2\n",
"10\n",
"3\n"
] | none | [
{
"input": "11",
"output": "2"
},
{
"input": "14",
"output": "0"
},
{
"input": "61441",
"output": "2"
},
{
"input": "571576",
"output": "10"
},
{
"input": "2128506",
"output": "3"
},
{
"input": "0",
"output": "1"
},
{
"input": "2000000000",
"output": "4"
},
{
"input": "143165576",
"output": "14"
},
{
"input": "1741",
"output": "2"
},
{
"input": "1919020031",
"output": "3"
},
{
"input": "1795248373",
"output": "5"
},
{
"input": "1818960378",
"output": "5"
},
{
"input": "1285316221",
"output": "3"
},
{
"input": "1309028227",
"output": "5"
},
{
"input": "1304312649",
"output": "8"
},
{
"input": "1180540990",
"output": "5"
},
{
"input": "1204252996",
"output": "3"
},
{
"input": "1199537418",
"output": "4"
},
{
"input": "1075765759",
"output": "2"
},
{
"input": "724264821",
"output": "5"
},
{
"input": "747976826",
"output": "4"
},
{
"input": "624205168",
"output": "4"
},
{
"input": "619489590",
"output": "4"
},
{
"input": "643201595",
"output": "5"
},
{
"input": "638486017",
"output": "6"
},
{
"input": "514714359",
"output": "3"
},
{
"input": "833393692",
"output": "3"
},
{
"input": "186925426",
"output": "4"
},
{
"input": "210637432",
"output": "4"
},
{
"input": "58438190",
"output": "4"
}
] | 78 | 7,065,600 | 3 | 4,843 |
|
456 | Fedya and Maths | [
"math",
"number theory"
] | null | null | Fedya studies in a gymnasium. Fedya's maths hometask is to calculate the following expression:
for given value of *n*. Fedya managed to complete the task. Can you? Note that given number *n* can be extremely large (e.g. it can exceed any integer type of your programming language). | The single line contains a single integer *n* (0<=β€<=*n*<=β€<=10105). The number doesn't contain any leading zeroes. | Print the value of the expression without leading zeros. | [
"4\n",
"124356983594583453458888889\n"
] | [
"4\n",
"0\n"
] | Operation *x*Β *mod*Β *y* means taking remainder after division *x* by *y*.
Note to the first sample:
<img align="middle" class="tex-formula" src="https://espresso.codeforces.com/825f244180bb10323db01645118c3cfdb312fa89.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "4",
"output": "4"
},
{
"input": "124356983594583453458888889",
"output": "0"
},
{
"input": "2",
"output": "0"
},
{
"input": "7854",
"output": "0"
},
{
"input": "584660",
"output": "4"
},
{
"input": "464",
"output": "4"
},
{
"input": "192329",
"output": "0"
},
{
"input": "85447",
"output": "0"
},
{
"input": "956",
"output": "4"
},
{
"input": "83",
"output": "0"
},
{
"input": "33",
"output": "0"
},
{
"input": "64",
"output": "4"
},
{
"input": "971836",
"output": "4"
},
{
"input": "578487",
"output": "0"
},
{
"input": "71752",
"output": "4"
},
{
"input": "2563",
"output": "0"
},
{
"input": "51494",
"output": "0"
},
{
"input": "247",
"output": "0"
},
{
"input": "52577",
"output": "0"
},
{
"input": "13",
"output": "0"
},
{
"input": "26232",
"output": "4"
},
{
"input": "0",
"output": "4"
},
{
"input": "10",
"output": "0"
},
{
"input": "12",
"output": "4"
},
{
"input": "8",
"output": "4"
},
{
"input": "1",
"output": "0"
}
] | 46 | 0 | 0 | 4,846 |
|
615 | Multipliers | [
"math",
"number theory"
] | null | null | Ayrat has number *n*, represented as it's prime factorization *p**i* of size *m*, i.e. *n*<==<=*p*1Β·*p*2Β·...Β·*p**m*. Ayrat got secret information that that the product of all divisors of *n* taken modulo 109<=+<=7 is the password to the secret data base. Now he wants to calculate this value. | The first line of the input contains a single integer *m* (1<=β€<=*m*<=β€<=200<=000)Β β the number of primes in factorization of *n*.
The second line contains *m* primes numbers *p**i* (2<=β€<=*p**i*<=β€<=200<=000). | Print one integerΒ β the product of all divisors of *n* modulo 109<=+<=7. | [
"2\n2 3\n",
"3\n2 3 2\n"
] | [
"36\n",
"1728\n"
] | In the first sample *n*β=β2Β·3β=β6. The divisors of 6 are 1, 2, 3 and 6, their product is equal to 1Β·2Β·3Β·6β=β36.
In the second sample 2Β·3Β·2β=β12. The divisors of 12 are 1, 2, 3, 4, 6 and 12. 1Β·2Β·3Β·4Β·6Β·12β=β1728. | [
{
"input": "2\n2 3",
"output": "36"
},
{
"input": "3\n2 3 2",
"output": "1728"
},
{
"input": "1\n2017",
"output": "2017"
},
{
"input": "2\n63997 63997",
"output": "135893224"
},
{
"input": "5\n11 7 11 7 11",
"output": "750455957"
},
{
"input": "5\n2 2 2 2 2",
"output": "32768"
},
{
"input": "4\n3 3 3 5",
"output": "332150625"
},
{
"input": "6\n101 103 107 109 101 103",
"output": "760029909"
},
{
"input": "10\n3 3 3 3 3 3 3 3 3 3",
"output": "555340537"
},
{
"input": "5\n7 5 2 3 13",
"output": "133580280"
},
{
"input": "23\n190979 191627 93263 72367 52561 188317 198397 24979 70313 105239 86263 78697 6163 7673 84137 199967 14657 84391 101009 16231 175103 24239 123289",
"output": "727083628"
},
{
"input": "7\n34429 104287 171293 101333 104287 34429 104287",
"output": "249330396"
},
{
"input": "27\n151153 29429 91411 91411 194507 194819 91411 91411 194507 181211 194507 131363 9371 194819 181211 194507 151153 91411 91411 192391 192391 151153 151153 194507 192391 192391 194819",
"output": "132073405"
},
{
"input": "47\n9041 60013 53609 82939 160861 123377 74383 74383 184039 19867 123377 101879 74383 193603 123377 115331 101879 53609 74383 115331 51869 51869 184039 193603 91297 160861 160861 115331 184039 51869 123377 74383 160861 74383 115331 115331 51869 74383 19867 193603 193603 115331 184039 9041 53609 53609 193603",
"output": "648634399"
},
{
"input": "67\n98929 19079 160079 181891 17599 91807 19079 98929 182233 92647 77477 98929 98639 182233 181891 182233 160079 98929 19079 98639 114941 98929 161341 91807 160079 22777 132361 92647 98929 77477 182233 103913 160079 77477 55711 77477 77477 182233 114941 91807 98929 19079 104393 182233 182233 131009 132361 16883 161341 103913 16883 98929 182233 114941 92647 92647 104393 132361 181891 114941 19079 91807 114941 132361 98639 161341 182233",
"output": "5987226"
},
{
"input": "44\n73 59 17 41 37 7 71 47 29 83 67 17 53 61 43 43 3 23 29 11 7 83 61 79 31 37 37 83 41 71 11 19 83 2 83 73 7 67 83 13 2 53 31 47",
"output": "464170294"
},
{
"input": "100\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541",
"output": "72902143"
},
{
"input": "130\n2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7 7 7 11 11 11 11 11 11 11 11 11 11 13 13 13 13 13 13 13 13 13 13 17 17 17 17 17 17 17 17 17 17 19 19 19 19 19 19 19 19 19 19 23 23 23 23 23 23 23 23 23 23 29 29 29 29 29 29 29 29 29 29 31 31 31 31 31 31 31 31 31 31 37 37 37 37 37 37 37 37 37 37 41 41 41 41 41 41 41 41 41 41",
"output": "869075922"
},
{
"input": "101\n2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 2",
"output": "918713851"
},
{
"input": "42\n1657 1871 2423 3037 5023 5099 5449 5701 6361 6619 7393 7489 8179 9743 9791 9907 12289 12457 13063 13933 14947 16141 16829 16943 17191 17863 20161 20947 21661 22727 23197 23201 23813 24023 24181 24223 24391 26479 28619 30529 32441 32611",
"output": "468186759"
}
] | 124 | 0 | 0 | 4,853 |
|
670 | Magic Powder - 1 | [
"binary search",
"brute force",
"implementation"
] | null | null | This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.
Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs *n* ingredients, and for each ingredient she knows the value *a**i*Β β how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all *n* ingredients.
Apollinaria has *b**i* gram of the *i*-th ingredient. Also she has *k* grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the *n* ingredients and can be used for baking cookies.
Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder. | The first line of the input contains two positive integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=1000)Β β the number of ingredients and the number of grams of the magic powder.
The second line contains the sequence *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=1000), where the *i*-th number is equal to the number of grams of the *i*-th ingredient, needed to bake one cookie.
The third line contains the sequence *b*1,<=*b*2,<=...,<=*b**n* (1<=β€<=*b**i*<=β€<=1000), where the *i*-th number is equal to the number of grams of the *i*-th ingredient, which Apollinaria has. | Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder. | [
"3 1\n2 1 4\n11 3 16\n",
"4 3\n4 3 5 6\n11 12 14 20\n"
] | [
"4\n",
"3\n"
] | In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.
In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer. | [
{
"input": "3 1\n2 1 4\n11 3 16",
"output": "4"
},
{
"input": "4 3\n4 3 5 6\n11 12 14 20",
"output": "3"
},
{
"input": "10 926\n5 6 8 1 2 5 1 8 4 4\n351 739 998 725 953 970 906 691 707 1000",
"output": "137"
},
{
"input": "20 925\n7 3 1 2 1 3 1 3 1 2 3 1 5 8 1 3 7 3 4 2\n837 898 965 807 786 670 626 873 968 745 878 359 760 781 829 882 777 740 907 779",
"output": "150"
},
{
"input": "30 300\n1 4 2 1 2 5 6 4 1 3 2 1 1 1 1 1 2 3 1 3 4 2 2 3 2 2 2 1 1 1\n997 817 767 860 835 809 817 565 630 804 586 953 977 356 905 890 958 916 740 583 902 945 313 956 871 729 976 707 516 788",
"output": "164"
},
{
"input": "40 538\n1 3 3 1 4 1 1 1 1 5 3 3 4 1 4 2 7 1 4 1 1 2 2 1 1 1 1 4 1 4 2 3 3 3 1 3 4 1 3 5\n975 635 795 835 982 965 639 787 688 796 988 779 839 942 491 696 396 995 718 810 796 879 957 783 844 765 968 783 647 214 995 868 318 453 989 889 504 962 945 925",
"output": "104"
},
{
"input": "50 530\n2 3 3 1 1 1 3 4 4 2 4 2 5 1 3 1 2 6 1 1 2 5 3 2 1 5 1 3 3 2 1 1 1 1 2 1 1 2 2 1 4 2 1 3 1 2 1 1 4 2\n959 972 201 990 675 679 972 268 976 886 488 924 795 959 647 994 969 862 898 646 763 797 978 763 995 641 923 856 829 921 934 883 904 986 728 980 1000 775 716 745 833 832 999 651 571 626 827 456 636 795",
"output": "133"
},
{
"input": "60 735\n3 1 4 7 1 7 3 1 1 5 4 7 3 3 3 2 5 3 1 2 3 6 1 1 1 1 1 2 5 3 2 1 3 5 2 1 2 2 2 2 1 3 3 3 6 4 3 5 1 3 2 2 1 3 1 1 1 7 1 2\n596 968 975 493 665 571 598 834 948 941 737 649 923 848 950 907 929 865 227 836 956 796 861 801 746 667 539 807 405 355 501 879 994 890 573 848 597 873 130 985 924 426 999 550 586 924 601 807 994 878 410 817 922 898 982 525 611 685 806 847",
"output": "103"
},
{
"input": "1 1\n1\n1",
"output": "2"
},
{
"input": "70 130\n2 1 2 2 3 3 2 5 2 2 3 3 3 1 1 4 3 5 3 2 1 3 7 1 2 7 5 2 1 6 3 4 1 2 1 1 1 1 3 6 4 2 2 8 2 2 4 1 4 2 1 4 4 3 5 1 1 1 1 1 2 3 1 5 1 3 3 4 2 2\n473 311 758 768 797 572 656 898 991 534 989 702 934 767 777 799 1000 655 806 727 718 948 834 965 832 778 706 861 799 874 745 970 772 967 984 886 835 795 832 837 950 952 475 891 947 952 903 929 689 478 725 945 585 943 771 631 729 887 557 738 824 758 999 786 669 992 918 762 964 941",
"output": "119"
},
{
"input": "80 979\n2 1 1 1 2 1 1 1 3 1 4 4 2 1 1 3 1 1 2 1 4 1 1 2 5 4 8 1 3 6 5 7 2 3 4 1 2 2 6 1 2 2 4 1 1 2 3 2 8 1 1 3 3 4 1 1 2 1 4 4 1 4 3 2 6 5 2 1 4 1 2 3 2 1 3 3 1 2 1 3\n498 976 513 869 917 914 664 656 957 893 981 947 985 693 576 958 987 822 981 718 884 729 295 683 485 998 730 894 731 975 739 854 906 740 987 976 606 689 990 775 522 994 920 893 529 651 989 799 643 215 946 987 297 868 425 810 694 908 736 903 970 751 625 904 955 945 839 777 977 974 905 900 666 680 799 873 565 919 536 686",
"output": "128"
},
{
"input": "1 1000\n1000\n1000",
"output": "2"
},
{
"input": "1 1000\n1\n1000",
"output": "2000"
},
{
"input": "1 1\n4\n6",
"output": "1"
},
{
"input": "1 1\n10\n2",
"output": "0"
},
{
"input": "2 1\n2 2\n1 1",
"output": "0"
},
{
"input": "2 6\n1 3\n6 2",
"output": "2"
}
] | 124 | 2,150,400 | -1 | 4,854 |
|
771 | Bear and Tree Jumps | [
"dfs and similar",
"dp",
"trees"
] | null | null | A tree is an undirected connected graph without cycles. The distance between two vertices is the number of edges in a simple path between them.
Limak is a little polar bear. He lives in a tree that consists of *n* vertices, numbered 1 through *n*.
Limak recently learned how to jump. He can jump from a vertex to any vertex within distance at most *k*.
For a pair of vertices (*s*,<=*t*) we define *f*(*s*,<=*t*) as the minimum number of jumps Limak needs to get from *s* to *t*. Your task is to find the sum of *f*(*s*,<=*t*) over all pairs of vertices (*s*,<=*t*) such that *s*<=<<=*t*. | The first line of the input contains two integers *n* and *k* (2<=β€<=*n*<=β€<=200<=000, 1<=β€<=*k*<=β€<=5)Β β the number of vertices in the tree and the maximum allowed jump distance respectively.
The next *n*<=-<=1 lines describe edges in the tree. The *i*-th of those lines contains two integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*)Β β the indices on vertices connected with *i*-th edge.
It's guaranteed that the given edges form a tree. | Print one integer, denoting the sum of *f*(*s*,<=*t*) over all pairs of vertices (*s*,<=*t*) such that *s*<=<<=*t*. | [
"6 2\n1 2\n1 3\n2 4\n2 5\n4 6\n",
"13 3\n1 2\n3 2\n4 2\n5 2\n3 6\n10 6\n6 7\n6 13\n5 8\n5 9\n9 11\n11 12\n",
"3 5\n2 1\n3 1\n"
] | [
"20\n",
"114\n",
"3\n"
] | In the first sample, the given tree has 6 vertices and it's displayed on the drawing below. Limak can jump to any vertex within distance at most 2. For example, from the vertex 5 he can jump to any of vertices: 1, 2 and 4 (well, he can also jump to the vertex 5 itself).
There are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/c0295201207e28a36e641d8cf599f45986059e71.png" style="max-width: 100.0%;max-height: 100.0%;"/> pairs of vertices (*s*,β*t*) such that *s*β<β*t*. For 5 of those pairs Limak would need two jumps: (1,β6),β(3,β4),β(3,β5),β(3,β6),β(5,β6). For other 10 pairs one jump is enough. So, the answer is 5Β·2β+β10Β·1β=β20.
In the third sample, Limak can jump between every two vertices directly. There are 3 pairs of vertices (*s*β<β*t*), so the answer is 3Β·1β=β3. | [
{
"input": "6 2\n1 2\n1 3\n2 4\n2 5\n4 6",
"output": "20"
},
{
"input": "13 3\n1 2\n3 2\n4 2\n5 2\n3 6\n10 6\n6 7\n6 13\n5 8\n5 9\n9 11\n11 12",
"output": "114"
},
{
"input": "3 5\n2 1\n3 1",
"output": "3"
},
{
"input": "2 1\n1 2",
"output": "1"
},
{
"input": "2 5\n2 1",
"output": "1"
},
{
"input": "15 1\n12 9\n13 7\n1 3\n10 4\n9 2\n2 15\n11 4\n2 14\n10 8\n6 7\n12 5\n8 7\n3 10\n10 2",
"output": "346"
},
{
"input": "4 2\n3 4\n2 4\n3 1",
"output": "7"
},
{
"input": "12 3\n5 11\n10 11\n6 4\n8 9\n4 12\n10 7\n4 1\n3 1\n2 12\n9 4\n9 10",
"output": "88"
}
] | 46 | 0 | 0 | 4,857 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.