contestId int64 0 1.01k | name stringlengths 2 58 | tags listlengths 0 11 | title stringclasses 523
values | time-limit stringclasses 8
values | memory-limit stringclasses 8
values | problem-description stringlengths 0 7.15k | input-specification stringlengths 0 2.05k | output-specification stringlengths 0 1.5k | demo-input listlengths 0 7 | demo-output listlengths 0 7 | note stringlengths 0 5.24k | test_cases listlengths 0 402 | timeConsumedMillis int64 0 8k | memoryConsumedBytes int64 0 537M | score float64 -1 3.99 | __index_level_0__ int64 0 621k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
630 | Turn | [
"geometry",
"math"
] | null | null | Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face.
One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget β a rotating camera β come up to it to see it better, look into it. And the camera takes their photo at that time. What could be better for high quality identification?
But not everything is so simple. The pictures from camera appear rotated too (on clockwise camera rotation frame the content becomes rotated counter-clockwise). But the identification algorithm can work only with faces that are just slightly deviated from vertical.
Vasya was entrusted to correct the situation β to rotate a captured image so that image would be minimally deviated from vertical. Requirements were severe. Firstly, the picture should be rotated only on angle divisible by 90 degrees to not lose a bit of information about the image. Secondly, the frames from the camera are so huge and FPS is so big that adequate rotation speed is provided by hardware FPGA solution only. And this solution can rotate only by 90 degrees clockwise. Of course, one can apply 90 degrees turn several times but for the sake of performance the number of turns should be minimized.
Help Vasya implement the program that by the given rotation angle of the camera can determine the minimum number of 90 degrees clockwise turns necessary to get a picture in which up direction deviation from vertical is minimum.
The next figure contains frames taken from an unrotated camera, then from rotated 90 degrees clockwise, then from rotated 90 degrees counter-clockwise. Arrows show direction to "true up".
The next figure shows 90 degrees clockwise turn by FPGA hardware. | The only line of the input contains one integer *x* (<=-<=1018<=β€<=*x*<=β€<=1018) β camera angle in degrees. Positive value denotes clockwise camera rotation, negative β counter-clockwise. | Output one integer β the minimum required number of 90 degrees clockwise turns. | [
"60\n",
"-60\n"
] | [
"1\n",
"3\n"
] | When the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from "true up" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image result in 240 degrees clockwise total rotation and deviation from "true up" for two turns is 120 degrees because 240 degrees clockwise equal to 120 degrees counter-clockwise. Three 90 degrees clockwise turns of the image result in 330 degrees clockwise total rotation and deviation from "true up" for three turns is 30 degrees because 330 degrees clockwise equal to 30 degrees counter-clockwise.
From 60, 150, 120 and 30 degrees deviations the smallest is 30, and it it achieved in three 90 degrees clockwise turns. | [
{
"input": "60",
"output": "1"
},
{
"input": "-60",
"output": "3"
},
{
"input": "0",
"output": "0"
},
{
"input": "44",
"output": "0"
},
{
"input": "45",
"output": "0"
},
{
"input": "46",
"output": "1"
},
{
"input": "134",
"output": "1"
... | 62 | 0 | 0 | 22,775 | |
319 | Kalila and Dimna in the Logging Industry | [
"dp",
"geometry"
] | null | null | Kalila and Dimna are two jackals living in a huge jungle. One day they decided to join a logging factory in order to make money.
The manager of logging factory wants them to go to the jungle and cut *n* trees with heights *a*1,<=*a*2,<=...,<=*a**n*. They bought a chain saw from a shop. Each time they use the chain saw on the tree number *i*, they can decrease the height of this tree by one unit. Each time that Kalila and Dimna use the chain saw, they need to recharge it. Cost of charging depends on the id of the trees which have been cut completely (a tree is cut completely if its height equal to 0). If the maximum id of a tree which has been cut completely is *i* (the tree that have height *a**i* in the beginning), then the cost of charging the chain saw would be *b**i*. If no tree is cut completely, Kalila and Dimna cannot charge the chain saw. The chainsaw is charged in the beginning. We know that for each *i* < *j*, *a**i*<=<<=*a**j* and *b**i*<=><=*b**j* and also *b**n*<==<=0 and *a*1<==<=1. Kalila and Dimna want to cut all the trees completely, with minimum cost.
They want you to help them! Will you? | The first line of input contains an integer *n* (1<=β€<=*n*<=β€<=105). The second line of input contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109). The third line of input contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=β€<=*b**i*<=β€<=109).
It's guaranteed that *a*1<==<=1, *b**n*<==<=0, *a*1<=<<=*a*2<=<<=...<=<<=*a**n* and *b*1<=><=*b*2<=><=...<=><=*b**n*. | The only line of output must contain the minimum cost of cutting all the trees completely.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"5\n1 2 3 4 5\n5 4 3 2 0\n",
"6\n1 2 3 10 20 30\n6 5 4 3 2 0\n"
] | [
"25\n",
"138\n"
] | none | [
{
"input": "5\n1 2 3 4 5\n5 4 3 2 0",
"output": "25"
},
{
"input": "6\n1 2 3 10 20 30\n6 5 4 3 2 0",
"output": "138"
}
] | 2,000 | 14,848,000 | 0 | 22,789 | |
611 | New Year and Ancient Prophecy | [
"dp",
"hashing",
"strings"
] | null | null | Limak is a little polar bear. In the snow he found a scroll with the ancient prophecy. Limak doesn't know any ancient languages and thus is unable to understand the prophecy. But he knows digits!
One fragment of the prophecy is a sequence of *n* digits. The first digit isn't zero. Limak thinks that it's a list of some special years. It's hard to see any commas or spaces, so maybe ancient people didn't use them. Now Limak wonders what years are listed there.
Limak assumes three things:
- Years are listed in the strictly increasing order; - Every year is a positive integer number; - There are no leading zeros.
Limak is going to consider all possible ways to split a sequence into numbers (years), satisfying the conditions above. He will do it without any help. However, he asked you to tell him the number of ways to do so. Since this number may be very large, you are only asked to calculate it modulo 109<=+<=7. | The first line of the input contains a single integer *n* (1<=β€<=*n*<=β€<=5000)Β β the number of digits.
The second line contains a string of digits and has length equal to *n*. It's guaranteed that the first digit is not '0'. | Print the number of ways to correctly split the given sequence modulo 109<=+<=7. | [
"6\n123434\n",
"8\n20152016\n"
] | [
"8\n",
"4\n"
] | In the first sample there are 8 ways to split the sequence:
- "123434" = "123434" (maybe the given sequence is just one big number) - "123434" = "1" + "23434" - "123434" = "12" + "3434" - "123434" = "123" + "434" - "123434" = "1" + "23" + "434" - "123434" = "1" + "2" + "3434" - "123434" = "1" + "2" + "3" + "434" - "123434" = "1" + "2" + "3" + "4" + "34"
Note that we don't count a split "123434" = "12" + "34" + "34" because numbers have to be strictly increasing.
In the second sample there are 4 ways:
- "20152016" = "20152016" - "20152016" = "20" + "152016" - "20152016" = "201" + "52016" - "20152016" = "2015" + "2016" | [] | 62 | 4,608,000 | 0 | 22,831 | |
822 | Liar | [
"binary search",
"dp",
"hashing",
"string suffix structures"
] | null | null | The first semester ended. You know, after the end of the first semester the holidays begin. On holidays Noora decided to return to ViΔkopolis. As a modest souvenir for Leha, she brought a sausage of length *m* from Pavlopolis. Everyone knows that any sausage can be represented as a string of lowercase English letters, the length of which is equal to the length of the sausage.
Leha was very pleased with the gift and immediately ate the sausage. But then he realized that it was a quite tactless act, because the sausage was a souvenir! So the hacker immediately went to the butcher shop. Unfortunately, there was only another sausage of length *n* in the shop. However Leha was not upset and bought this sausage. After coming home, he decided to cut the purchased sausage into several pieces and number the pieces starting from 1 from left to right. Then he wants to select several pieces and glue them together so that the obtained sausage is equal to the sausage that Noora gave. But the hacker can glue two pieces together only when the number of the left piece is less than the number of the right piece. Besides he knows that if he glues more than *x* pieces, Noora will notice that he has falsified souvenir sausage and will be very upset. Of course Leha doesnβt want to upset the girl. The hacker asks you to find out whether he is able to cut the sausage he bought, and then glue some of the pieces so that Noora doesn't notice anything.
Formally, you are given two strings *s* and *t*. The length of the string *s* is *n*, the length of the string *t* is *m*. It is required to select several pairwise non-intersecting substrings from *s*, so that their concatenation in the same order as these substrings appear in *s*, is equal to the string *t*. Denote by *f*(*s*,<=*t*) the minimal number of substrings to be chosen so that their concatenation is equal to the string *t*. If it is impossible to choose such substrings, then *f*(*s*,<=*t*)<==<=β. Leha really wants to know whether itβs true that *f*(*s*,<=*t*)<=β€<=*x*. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105) β length of sausage bought by Leha, i.e. the length of the string *s*.
The second line contains string *s* of the length *n* consisting of lowercase English letters.
The third line contains single integer *m* (1<=β€<=*m*<=β€<=*n*) β length of sausage bought by Noora, i.e. the length of the string *t*.
The fourth line contains string *t* of the length *m* consisting of lowercase English letters.
The fifth line contains single integer *x* (1<=β€<=*x*<=β€<=30) β the maximum number of pieces of sausage that Leha can glue so that Noora doesnβt notice anything. | In the only line print "YES" (without quotes), if Leha is able to succeed in creating new sausage so that Noora doesn't notice anything. Otherwise print "NO" (without quotes). | [
"9\nhloyaygrt\n6\nloyyrt\n3\n",
"9\nhloyaygrt\n6\nloyyrt\n2\n"
] | [
"YES\n",
"NO\n"
] | Let's consider the first sample.
In the optimal answer, Leha should cut the sausage he bought in the following way: hloyaygrt = h + loy + a + y + g + rt. Then he numbers received parts from 1 to 6:
- h β number 1 - loy β number 2 - a β number 3 - y β number 4 - g β number 5 - rt β number 6
Hereupon the hacker should glue the parts with numbers 2, 4 and 6 and get sausage loyygrt equal to one that is given by Noora. Thus, he will have to glue three pieces. Since *x*β=β3 you should print "YES" (without quotes).
In the second sample both sausages coincide with sausages from the first sample. However since *x*β=β2 you should print "NO" (without quotes). | [] | 31 | 0 | 0 | 22,900 | |
830 | Cards Sorting | [
"data structures",
"implementation",
"sortings"
] | null | null | Vasily has a deck of cards consisting of *n* cards. There is an integer on each of the cards, this integer is between 1 and 100<=000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.
You are to determine the total number of times Vasily takes the top card from the deck. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=100<=000) β the number of cards in the deck.
The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=100<=000), where *a**i* is the number written on the *i*-th from top card in the deck. | Print the total number of times Vasily takes the top card from the deck. | [
"4\n6 3 1 2\n",
"1\n1000\n",
"7\n3 3 3 3 3 3 3\n"
] | [
"7\n",
"1\n",
"7\n"
] | In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2,β6,β3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6,β3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards. | [
{
"input": "4\n6 3 1 2",
"output": "7"
},
{
"input": "1\n1000",
"output": "1"
},
{
"input": "7\n3 3 3 3 3 3 3",
"output": "7"
},
{
"input": "64\n826 142 89 337 897 891 1004 704 281 644 910 852 147 193 289 384 625 695 416 944 162 939 164 1047 359 114 499 99 713 300 268 316 256... | 1,000 | 11,776,000 | 0 | 22,987 | |
691 | Xor-sequences | [
"matrices"
] | null | null | You are given *n* integers *a*1,<=<=*a*2,<=<=...,<=<=*a**n*.
A sequence of integers *x*1,<=<=*x*2,<=<=...,<=<=*x**k* is called a "xor-sequence" if for every 1<=<=β€<=<=*i*<=<=β€<=<=*k*<=-<=1 the number of ones in the binary representation of the number *x**i* *x**i*<=<=+<=<=1's is a multiple of 3 and for all 1<=β€<=*i*<=β€<=*k*. The symbol is used for the binary exclusive or operation.
How many "xor-sequences" of length *k* exist? Output the answer modulo 109<=+<=7.
Note if *a*<==<=[1,<=1] and *k*<==<=1 then the answer is 2, because you should consider the ones from *a* as different. | The first line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=100, 1<=β€<=*k*<=β€<=1018) β the number of given integers and the length of the "xor-sequences".
The second line contains *n* integers *a**i* (0<=β€<=*a**i*<=β€<=1018). | Print the only integer *c* β the number of "xor-sequences" of length *k* modulo 109<=+<=7. | [
"5 2\n15 1 2 4 8\n",
"5 1\n15 1 2 4 8\n"
] | [
"13\n",
"5\n"
] | none | [
{
"input": "5 2\n15 1 2 4 8",
"output": "13"
},
{
"input": "5 1\n15 1 2 4 8",
"output": "5"
},
{
"input": "10 1\n44 65 23 44 100 19 19 23 19 40",
"output": "10"
},
{
"input": "10 2\n93 93 85 48 44 98 93 100 98 98",
"output": "52"
},
{
"input": "10 100\n22 0 41 63 ... | 795 | 9,830,400 | 3 | 23,075 | |
0 | none | [
"none"
] | null | null | You are given three sticks with positive integer lengths of *a*,<=*b*, and *c* centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most *l* centimeters. In particular, it is allowed not to increase the length of any stick.
Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them. | The single line contains 4 integers *a*,<=*b*,<=*c*,<=*l* (1<=β€<=*a*,<=*b*,<=*c*<=β€<=3Β·105, 0<=β€<=*l*<=β€<=3Β·105). | Print a single integer β the number of ways to increase the sizes of the sticks by the total of at most *l* centimeters, so that you can make a non-degenerate triangle from it. | [
"1 1 1 2\n",
"1 2 3 1\n",
"10 2 1 7\n"
] | [
"4\n",
"2\n",
"0\n"
] | In the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter.
In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions. | [
{
"input": "1 1 1 2",
"output": "4"
},
{
"input": "1 2 3 1",
"output": "2"
},
{
"input": "10 2 1 7",
"output": "0"
},
{
"input": "1 2 1 5",
"output": "20"
},
{
"input": "10 15 17 10",
"output": "281"
},
{
"input": "5 5 5 10000",
"output": "41841675... | 0 | 0 | -1 | 23,139 | |
660 | Different Subsets For All Tuples | [
"combinatorics",
"math"
] | null | null | For a sequence *a* of *n* integers between 1 and *m*, inclusive, denote *f*(*a*) as the number of distinct subsequences of *a* (including the empty subsequence).
You are given two positive integers *n* and *m*. Let *S* be the set of all sequences of length *n* consisting of numbers from 1 to *m*. Compute the sum *f*(*a*) over all *a* in *S* modulo 109<=+<=7. | The only line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=106) β the number of elements in arrays and the upper bound for elements. | Print the only integer *c* β the desired sum modulo 109<=+<=7. | [
"1 3\n",
"2 2\n",
"3 3\n"
] | [
"6\n",
"14\n",
"174\n"
] | none | [
{
"input": "1 3",
"output": "6"
},
{
"input": "2 2",
"output": "14"
},
{
"input": "3 3",
"output": "174"
},
{
"input": "1 1000000",
"output": "2000000"
},
{
"input": "1000000 1",
"output": "1000001"
},
{
"input": "500 500",
"output": "383255233"
... | 46 | 0 | 0 | 23,151 | |
235 | Graph Game | [
"graphs"
] | null | null | In computer science, there is a method called "Divide And Conquer By Node" to solve some hard problems about paths on a tree. Let's desribe how this method works by function:
*solve*(*t*) (*t* is a tree):
1. Chose a node *x* (it's common to chose weight-center) in tree *t*. Let's call this step "Line A". 1. Deal with all paths that pass *x*. 1. Then delete *x* from tree *t*. 1. After that *t* becomes some subtrees. 1. Apply *solve* on each subtree.
This ends when *t* has only one node because after deleting it, there's nothing.
Now, WJMZBMR has mistakenly believed that it's ok to chose any node in "Line A". So he'll chose a node at random. To make the situation worse, he thinks a "tree" should have the same number of edges and nodes! So this procedure becomes like that.
Let's define the variable *totalCost*. Initially the value of *totalCost* equal to 0. So, *solve*(*t*) (now *t* is a graph):
1. *totalCost*<==<=*totalCost*<=+<=(*size*Β *of*Β *t*). The operation "=" means assignment. (*Size*Β *of*Β *t*) means the number of nodes in *t*. 1. Choose a node *x* in graph *t* at random (uniformly among all nodes of *t*). 1. Then delete *x* from graph *t*. 1. After that *t* becomes some connected components. 1. Apply *solve* on each component.
He'll apply *solve* on a connected graph with *n* nodes and *n* edges. He thinks it will work quickly, but it's very slow. So he wants to know the expectation of *totalCost* of this procedure. Can you help him? | The first line contains an integer *n* (3<=β€<=*n*<=β€<=3000) β the number of nodes and edges in the graph. Each of the next *n* lines contains two space-separated integers *a**i*,<=*b**i* (0<=β€<=*a**i*,<=*b**i*<=β€<=*n*<=-<=1) indicating an edge between nodes *a**i* and *b**i*.
Consider that the graph nodes are numbered from 0 to (*n*<=-<=1). It's guaranteed that there are no self-loops, no multiple edges in that graph. It's guaranteed that the graph is connected. | Print a single real number β the expectation of *totalCost*. Your answer will be considered correct if its absolute or relative error does not exceed 10<=-<=6. | [
"5\n3 4\n2 3\n2 4\n0 4\n1 2\n",
"3\n0 1\n1 2\n0 2\n",
"5\n0 1\n1 2\n2 0\n3 0\n4 1\n"
] | [
"13.166666666666666\n",
"6.000000000000000\n",
"13.166666666666666\n"
] | Consider the second example. No matter what we choose first, the *totalCost* will always be 3β+β2β+β1β=β6. | [] | 60 | 0 | 0 | 23,166 | |
928 | Autocompletion | [
"*special",
"strings",
"trees"
] | null | null | Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor.
Arcady types words, punctuation signs and spaces one after another. Each letter and each sign (including line feed) requires one keyboard click in order to be printed. Moreover, when Arcady has a non-empty prefix of some word on the screen, the editor proposes a possible autocompletion for this word, more precisely one of the already printed words such that its prefix matches the currently printed prefix if this word is unique. For example, if Arcady has already printed Β«codeforcesΒ», Β«codingΒ» and Β«codeforcesΒ» once again, then there will be no autocompletion attempt for Β«codΒ», but if he proceeds with Β«codeΒ», the editor will propose Β«codeforcesΒ».
With a single click Arcady can follow the editor's proposal, i.e. to transform the current prefix to it. Note that no additional symbols are printed after the autocompletion (no spaces, line feeds, etc). What is the minimum number of keyboard clicks Arcady has to perform to print the entire text, if he is not allowed to move the cursor or erase the already printed symbols?
A word here is a contiguous sequence of latin letters bordered by spaces, punctuation signs and line/text beginnings/ends. Arcady uses only lowercase letters. For example, there are 20 words in Β«it's well-known that tic-tac-toe is a paper-and-pencil game for two players, x and o.Β». | The only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: Β«.Β», Β«,Β», Β«?Β», Β«!Β», Β«'Β» and Β«-Β». The total amount of symbols doesn't exceed 3Β·105. It's guaranteed that all lines are non-empty. | Print a single integer β the minimum number of clicks. | [
"snow affects sports such as skiing, snowboarding, and snowmachine travel.\nsnowboarding is a recreational activity and olympic and paralympic sport.\n",
"'co-co-co, codeforces?!'\n",
"thun-thun-thunder, thunder, thunder\nthunder, thun-, thunder\nthun-thun-thunder, thunder\nthunder, feel the thunder\nlightning ... | [
"141\n",
"25\n",
"183\n"
] | In sample case one it's optimal to use autocompletion for the first instance of Β«snowboardingΒ» after typing up Β«snΒ» and for the second instance of Β«snowboardingΒ» after typing up Β«snowbΒ». This will save 7 clicks.
In sample case two it doesn't matter whether to use autocompletion or not. | [
{
"input": "snow affects sports such as skiing, snowboarding, and snowmachine travel.\nsnowboarding is a recreational activity and olympic and paralympic sport.",
"output": "141"
},
{
"input": "'co-co-co, codeforces?!'",
"output": "25"
},
{
"input": "thun-thun-thunder, thunder, thunder\n... | 1,000 | 7,065,600 | 0 | 23,180 | |
432 | Prefixes and Suffixes | [
"dp",
"string suffix structures",
"strings",
"two pointers"
] | null | null | You have a string *s*<==<=*s*1*s*2...*s*|*s*|, where |*s*| is the length of string *s*, and *s**i* its *i*-th character.
Let's introduce several definitions:
- A substring *s*[*i*..*j*] (1<=β€<=*i*<=β€<=*j*<=β€<=|*s*|) of string *s* is string *s**i**s**i*<=+<=1...*s**j*. - The prefix of string *s* of length *l* (1<=β€<=*l*<=β€<=|*s*|) is string *s*[1..*l*]. - The suffix of string *s* of length *l* (1<=β€<=*l*<=β€<=|*s*|) is string *s*[|*s*|<=-<=*l*<=+<=1..|*s*|].
Your task is, for any prefix of string *s* which matches a suffix of string *s*, print the number of times it occurs in string *s* as a substring. | The single line contains a sequence of characters *s*1*s*2...*s*|*s*| (1<=β€<=|*s*|<=β€<=105) β string *s*. The string only consists of uppercase English letters. | In the first line, print integer *k* (0<=β€<=*k*<=β€<=|*s*|) β the number of prefixes that match a suffix of string *s*. Next print *k* lines, in each line print two integers *l**i* *c**i*. Numbers *l**i* *c**i* mean that the prefix of the length *l**i* matches the suffix of length *l**i* and occurs in string *s* as a substring *c**i* times. Print pairs *l**i* *c**i* in the order of increasing *l**i*. | [
"ABACABA\n",
"AAA\n"
] | [
"3\n1 4\n3 2\n7 1\n",
"3\n1 3\n2 2\n3 1\n"
] | none | [
{
"input": "ABACABA",
"output": "3\n1 4\n3 2\n7 1"
},
{
"input": "AAA",
"output": "3\n1 3\n2 2\n3 1"
},
{
"input": "A",
"output": "1\n1 1"
},
{
"input": "AAAAAAAAAAAAAAAAXAAAAAAAAAAAAAAAAAAAAAAA",
"output": "17\n1 39\n2 37\n3 35\n4 33\n5 31\n6 29\n7 27\n8 25\n9 23\n10 21\... | 30 | 0 | 0 | 23,189 | |
721 | Journey | [
"dp",
"graphs"
] | null | null | Recently Irina arrived to one of the most famous cities of BerlandΒ β the Berlatov city. There are *n* showplaces in the city, numbered from 1 to *n*, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.
Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace *n*. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than *T* time units.
Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace *n* within a time not exceeding *T*. It is guaranteed that there is at least one route from showplace 1 to showplace *n* such that Irina will spend no more than *T* time units passing it. | The first line of the input contains three integers *n*,<=*m* and *T* (2<=β€<=*n*<=β€<=5000,<=<=1<=β€<=*m*<=β€<=5000,<=<=1<=β€<=*T*<=β€<=109)Β β the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.
The next *m* lines describes roads in Berlatov. *i*-th of them contains 3 integers *u**i*,<=*v**i*,<=*t**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*,<=*u**i*<=β <=*v**i*,<=1<=β€<=*t**i*<=β€<=109), meaning that there is a road starting from showplace *u**i* and leading to showplace *v**i*, and Irina spends *t**i* time units to pass it. It is guaranteed that the roads do not form cyclic routes.
It is guaranteed, that there is at most one road between each pair of showplaces. | Print the single integer *k* (2<=β€<=*k*<=β€<=*n*)Β β the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace *n* within time not exceeding *T*, in the first line.
Print *k* distinct integers in the second lineΒ β indices of showplaces that Irina will visit on her route, in the order of encountering them.
If there are multiple answers, print any of them. | [
"4 3 13\n1 2 5\n2 3 7\n2 4 8\n",
"6 6 7\n1 2 2\n1 3 3\n3 6 3\n2 4 2\n4 6 2\n6 5 1\n",
"5 5 6\n1 3 3\n3 5 3\n1 2 2\n2 4 3\n4 5 2\n"
] | [
"3\n1 2 4 \n",
"4\n1 2 4 6 \n",
"3\n1 3 5 \n"
] | none | [
{
"input": "4 3 13\n1 2 5\n2 3 7\n2 4 8",
"output": "3\n1 2 4 "
},
{
"input": "6 6 7\n1 2 2\n1 3 3\n3 6 3\n2 4 2\n4 6 2\n6 5 1",
"output": "4\n1 2 4 6 "
},
{
"input": "5 5 6\n1 3 3\n3 5 3\n1 2 2\n2 4 3\n4 5 2",
"output": "3\n1 3 5 "
},
{
"input": "10 10 100\n1 4 1\n6 4 1\n9 3... | 561 | 268,390,400 | 0 | 23,242 | |
652 | Pursuit For Artifacts | [
"dfs and similar",
"dsu",
"graphs",
"trees"
] | null | null | Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience.
In that country there are *n* islands and *m* bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact.
At the start Johnny is in the island *a* and the artifact-dealer is in the island *b* (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult.
Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island.
Determine if Johnny can find some artifact and sell it. | The first line contains two integers *n* and *m* (1<=β€<=*n*<=β€<=3Β·105, 0<=β€<=*m*<=β€<=3Β·105) β the number of islands and bridges in the game.
Each of the next *m* lines contains the description of the bridge β three integers *x**i*, *y**i*, *z**i* (1<=β€<=*x**i*,<=*y**i*<=β€<=*n*, *x**i*<=β <=*y**i*, 0<=β€<=*z**i*<=β€<=1), where *x**i* and *y**i* are the islands connected by the *i*-th bridge, *z**i* equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands.
The last line contains two integers *a* and *b* (1<=β€<=*a*,<=*b*<=β€<=*n*) β the islands where are Johnny and the artifact-dealer respectively. | If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes). | [
"6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6\n",
"5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4\n",
"5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2\n"
] | [
"YES\n",
"NO\n",
"YES\n"
] | none | [
{
"input": "6 7\n1 2 0\n2 3 0\n3 1 0\n3 4 1\n4 5 0\n5 6 0\n6 4 0\n1 6",
"output": "YES"
},
{
"input": "5 4\n1 2 0\n2 3 0\n3 4 0\n2 5 1\n1 4",
"output": "NO"
},
{
"input": "5 6\n1 2 0\n2 3 0\n3 1 0\n3 4 0\n4 5 1\n5 3 0\n1 2",
"output": "YES"
},
{
"input": "1 0\n1 1",
"outp... | 0 | 0 | -1 | 23,285 | |
632 | Thief in a Shop | [
"divide and conquer",
"dp",
"fft",
"math"
] | null | null | A thief made his way to a shop.
As usual he has his lucky knapsack with him. The knapsack can contain *k* objects. There are *n* kinds of products in the shop and an infinite number of products of each kind. The cost of one product of kind *i* is *a**i*.
The thief is greedy, so he will take exactly *k* products (it's possible for some kinds to take several products of that kind).
Find all the possible total costs of products the thief can nick into his knapsack. | The first line contains two integers *n* and *k* (1<=β€<=*n*,<=*k*<=β€<=1000) β the number of kinds of products and the number of products the thief will take.
The second line contains *n* integers *a**i* (1<=β€<=*a**i*<=β€<=1000) β the costs of products for kinds from 1 to *n*. | Print the only line with all the possible total costs of stolen products, separated by a space. The numbers should be printed in the ascending order. | [
"3 2\n1 2 3\n",
"5 5\n1 1 1 1 1\n",
"3 3\n3 5 11\n"
] | [
"2 3 4 5 6\n",
"5\n",
"9 11 13 15 17 19 21 25 27 33\n"
] | none | [
{
"input": "3 2\n1 2 3",
"output": "2 3 4 5 6"
},
{
"input": "5 5\n1 1 1 1 1",
"output": "5"
},
{
"input": "3 3\n3 5 11",
"output": "9 11 13 15 17 19 21 25 27 33"
},
{
"input": "10 3\n3 4 12 5 7 13 5 6 1 6",
"output": "3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23... | 5,000 | 126,873,600 | 0 | 23,330 | |
827 | DNA Evolution | [
"data structures",
"strings"
] | null | null | Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string *s* initially.
Evolution of the species is described as a sequence of changes in the DNA. Every change is a change of some nucleotide, for example, the following change can happen in DNA strand "AAGC": the second nucleotide can change to "T" so that the resulting DNA strand is "ATGC".
Scientists know that some segments of the DNA strand can be affected by some unknown infections. They can represent an infection as a sequence of nucleotides. Scientists are interested if there are any changes caused by some infections. Thus they sometimes want to know the value of impact of some infection to some segment of the DNA. This value is computed as follows:
- Let the infection be represented as a string *e*, and let scientists be interested in DNA strand segment starting from position *l* to position *r*, inclusive. - Prefix of the string *eee*... (i.e. the string that consists of infinitely many repeats of string *e*) is written under the string *s* from position *l* to position *r*, inclusive. - The value of impact is the number of positions where letter of string *s* coincided with the letter written under it.
Being a developer, Innokenty is interested in bioinformatics also, so the scientists asked him for help. Innokenty is busy preparing VK Cup, so he decided to delegate the problem to the competitors. Help the scientists! | The first line contains the string *s* (1<=β€<=|*s*|<=β€<=105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".
The next line contains single integer *q* (1<=β€<=*q*<=β€<=105)Β β the number of events.
After that, *q* lines follow, each describes one event. Each of the lines has one of two formats:
- 1Β xΒ c, where *x* is an integer (1<=β€<=*x*<=β€<=|*s*|), and *c* is a letter "A", "T", "G" or "C", which means that there is a change in the DNA: the nucleotide at position *x* is now *c*. - 2Β lΒ rΒ e, where *l*, *r* are integers (1<=β€<=*l*<=β€<=*r*<=β€<=|*s*|), and *e* is a string of letters "A", "T", "G" and "C" (1<=β€<=|*e*|<=β€<=10), which means that scientists are interested in the value of impact of infection *e* to the segment of DNA strand from position *l* to position *r*, inclusive. | For each scientists' query (second type query) print a single integer in a new lineΒ β the value of impact of the infection on the DNA. | [
"ATGCATGC\n4\n2 1 8 ATGC\n2 2 6 TTT\n1 4 T\n2 2 6 TA\n",
"GAGTTGTTAA\n6\n2 3 4 TATGGTG\n1 1 T\n1 6 G\n2 5 9 AGTAATA\n1 10 G\n2 2 6 TTGT\n"
] | [
"8\n2\n4\n",
"0\n3\n1\n"
] | Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matches. | [
{
"input": "ATGCATGC\n4\n2 1 8 ATGC\n2 2 6 TTT\n1 4 T\n2 2 6 TA",
"output": "8\n2\n4"
},
{
"input": "GAGTTGTTAA\n6\n2 3 4 TATGGTG\n1 1 T\n1 6 G\n2 5 9 AGTAATA\n1 10 G\n2 2 6 TTGT",
"output": "0\n3\n1"
},
{
"input": "TCAATCGGGCGGACTAACCC\n20\n2 4 17 CTCGGATGTT\n1 4 T\n1 1 C\n2 15 18 CA\n2... | 2,000 | 5,324,800 | 0 | 23,392 | |
690 | Photographs (I) | [] | null | null | The Human-Cow Confederation (*HC*2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves of this photograph have been swapped or not. A person (or even a cow) will have no problem answering such questions; on the other hand, a zombie would just randomly smash one of the two buttons.
The creature is asked a series of such questions. If at least 75% of them are answered correctly, the gate is unlocked; otherwise, a side door opens, beneath which a huge fan is spinning...
Heidi is now building a robot army to fight the zombies, and she wants the robots to also be able to enter the base. You are tasked with programming them to distinguish the images.
The first two images from the test set. The first picture has been rearranged, but not the second. | The first line of the input contains the number *q* of questions (1<=β€<=*q*<=β€<=220). After that, *q* questions follow, each of which in the format described below.
The first line of every question contains two space-separated integers *h* and *w* (1<=β€<=*h*,<=*w*<=β€<=600) β the height (number of rows) and width (number of columns) of the photograph. (Most photographs are roughly 200<=Γ<=300.) After this, *h* lines follow, each describing a single row of the picture. The picture is monochrome (in shades of grey). Its *i*-th row is described by *w* space-separated integers *a**ij* (*j*<==<=1,<=...,<=*w*), where *a**ij* is the brightness of the corresponding pixel (0<=β€<=*a**ij*<=<<=256, where 0 is black and 255 is white).
Each picture will be either a real-life photograph, or a real-life photograph which has been broken up into two pieces and rearranged. More precisely, in the latter case, the topmost rows have been moved to the bottom of the picture. It is guaranteed that *h* is even.
There is only a single input file to be processed, called all.in, and it is downloadable from the online judge. You are also a given another input file, called sample.in, which contains the first 20 pictures from all.in; you are provided the correct answers for sample.in in sample.out. You are also given a directory easy_bmp, which contains the first 50 input photographs in the form of .bmp image files, as well as a directory easy_sample_original_bmp, which contains the first 20 images before rearrangement. Check the notes for the download links. | Your program should print *q* lines. The *i*-th line should contain your answer for the *i*-th question: YES if the photograph has been rearranged and NO otherwise. Your answers will be accepted if they all conform to this format and if at least 75% of them are correct.
Because the input is rather huge, feel free to process it locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in). | [] | [] | The link to download all necessary files is http://assets.codeforces.com/files/690/easy_contestant_package.zip | [] | 0 | 0 | -1 | 23,404 | |
546 | Soldier and Badges | [
"brute force",
"greedy",
"implementation",
"sortings"
] | null | null | Colonel has *n* badges. He wants to give one badge to every of his *n* soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin.
For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors.
Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness. | First line of input consists of one integer *n* (1<=β€<=*n*<=β€<=3000).
Next line consists of *n* integers *a**i* (1<=β€<=*a**i*<=β€<=*n*), which stand for coolness factor of each badge. | Output single integer β minimum amount of coins the colonel has to pay. | [
"4\n1 3 1 4\n",
"5\n1 2 3 2 5\n"
] | [
"1",
"2"
] | In first sample test we can increase factor of first badge by 1.
In second sample test we can increase factors of the second and the third badge by 1. | [
{
"input": "4\n1 3 1 4",
"output": "1"
},
{
"input": "5\n1 2 3 2 5",
"output": "2"
},
{
"input": "5\n1 5 3 2 4",
"output": "0"
},
{
"input": "10\n1 1 2 3 4 5 6 7 8 9",
"output": "9"
},
{
"input": "11\n9 2 10 3 1 5 7 1 4 8 6",
"output": "10"
},
{
"input... | 109 | 1,945,600 | 3 | 23,468 | |
144 | Missile Silos | [
"data structures",
"dfs and similar",
"graphs",
"shortest paths"
] | null | null | A country called Berland consists of *n* cities, numbered with integer numbers from 1 to *n*. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Missiles. The exact position of the Super Duper Secret Missile Silos is kept secret but Bob managed to get hold of the information. That information says that all silos are located exactly at a distance *l* from the capital. The capital is located in the city with number *s*.
The documents give the formal definition: the Super Duper Secret Missile Silo is located at some place (which is either city or a point on a road) if and only if the shortest distance from this place to the capital along the roads of the country equals exactly *l*.
Bob wants to know how many missile silos are located in Berland to sell the information then to enemy spies. Help Bob. | The first line contains three integers *n*, *m* and *s* (2<=β€<=*n*<=β€<=105, , 1<=β€<=*s*<=β€<=*n*) β the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. *s*.
Then *m* lines contain the descriptions of roads. Each of them is described by three integers *v**i*, *u**i*, *w**i* (1<=β€<=*v**i*,<=*u**i*<=β€<=*n*, *v**i*<=β <=*u**i*, 1<=β€<=*w**i*<=β€<=1000), where *v**i*, *u**i* are numbers of the cities connected by this road and *w**i* is its length. The last input line contains integer *l* (0<=β€<=*l*<=β€<=109) β the distance from the capital to the missile silos. It is guaranteed that:
- between any two cities no more than one road exists; - each road connects two different cities; - from each city there is at least one way to any other city by the roads. | Print the single number β the number of Super Duper Secret Missile Silos that are located in Berland. | [
"4 6 1\n1 2 1\n1 3 3\n2 3 1\n2 4 1\n3 4 1\n1 4 2\n2\n",
"5 6 3\n3 1 1\n3 2 1\n3 4 1\n3 5 1\n1 2 6\n4 5 8\n4\n"
] | [
"3\n",
"3\n"
] | In the first sample the silos are located in cities 3 and 4 and on road (1,β3) at a distance 2 from city 1 (correspondingly, at a distance 1 from city 3).
In the second sample one missile silo is located right in the middle of the road (1,β2). Two more silos are on the road (4,β5) at a distance 3 from city 4 in the direction to city 5 and at a distance 3 from city 5 to city 4. | [] | 154 | 0 | 0 | 23,483 | |
696 | Puzzles | [
"dfs and similar",
"math",
"probabilities",
"trees"
] | null | null | Barney lives in country USC (United States of Charzeh). USC has *n* cities numbered from 1 through *n* and *n*<=-<=1 roads between them. Cities and roads of USC form a rooted tree (Barney's not sure why it is rooted). Root of the tree is the city number 1. Thus if one will start his journey from city 1, he can visit any city he wants by following roads.
Some girl has stolen Barney's heart, and Barney wants to find her. He starts looking for in the root of the tree and (since he is Barney Stinson not a random guy), he uses a random DFS to search in the cities. A pseudo code of this algorithm is as follows:
As told before, Barney will start his journey in the root of the tree (equivalent to call dfs(1)).
Now Barney needs to pack a backpack and so he wants to know more about his upcoming journey: for every city *i*, Barney wants to know the expected value of starting_time[i]. He's a friend of Jon Snow and knows nothing, that's why he asked for your help. | The first line of input contains a single integer *n* (1<=β€<=*n*<=β€<=105)Β β the number of cities in USC.
The second line contains *n*<=-<=1 integers *p*2,<=*p*3,<=...,<=*p**n* (1<=β€<=*p**i*<=<<=*i*), where *p**i* is the number of the parent city of city number *i* in the tree, meaning there is a road between cities numbered *p**i* and *i* in USC. | In the first and only line of output print *n* numbers, where *i*-th number is the expected value of starting_time[i].
Your answer for each city will be considered correct if its absolute or relative error does not exceed 10<=-<=6. | [
"7\n1 2 1 1 4 4\n",
"12\n1 1 2 2 4 4 3 3 1 10 8\n"
] | [
"1.0 4.0 5.0 3.5 4.5 5.0 5.0 \n",
"1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 \n"
] | none | [
{
"input": "7\n1 2 1 1 4 4",
"output": "1.0 4.0 5.0 3.5 4.5 5.0 5.0 "
},
{
"input": "12\n1 1 2 2 4 4 3 3 1 10 8",
"output": "1.0 5.0 5.5 6.5 7.5 8.0 8.0 7.0 7.5 6.5 7.5 8.0 "
},
{
"input": "3\n1 2",
"output": "1.0 2.0 3.0 "
},
{
"input": "8\n1 1 2 2 3 6 1",
"output": "1.0... | 1,000 | 28,672,000 | 0 | 23,547 | |
852 | Product transformation | [
"combinatorics",
"math",
"number theory"
] | null | null | Consider an array *A* with *N* elements, all being the same integer *a*.
Define the product transformation as a simultaneous update *A**i*<==<=*A**i*Β·*A**i*<=+<=1, that is multiplying each element to the element right to it for , with the last number *A**N* remaining the same. For example, if we start with an array *A* with *a*<==<=2 and *N*<==<=4, then after one product transformation *A*<==<=[4,<= 4,<= 4,<= 2], and after two product transformations *A*<==<=[16,<= 16,<= 8,<= 2].
Your simple task is to calculate the array *A* after *M* product transformations. Since the numbers can get quite big you should output them modulo *Q*. | The first and only line of input contains four integers *N*, *M*, *a*, *Q* (7<=β€<=*Q*<=β€<=109<=+<=123, 2<=β€<=*a*<=β€<=106<=+<=123, , is prime), where is the multiplicative order of the integer *a* modulo *Q*, see notes for definition. | You should output the array *A* from left to right. | [
"2 2 2 7\n"
] | [
"1 2 "
] | The multiplicative order of a number *a* modulo *Q* <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/38b13c1f6db75ae72784f8602e8230429b26cf2a.png" style="max-width: 100.0%;max-height: 100.0%;"/>, is the smallest natural number *x* such that *a*<sup class="upper-index">*x*</sup> *mod* *Q*β=β1. For example, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/64b53b8160eddc004a5b65223bf29dd636bb1832.png" style="max-width: 100.0%;max-height: 100.0%;"/>. | [] | 46 | 0 | -1 | 23,614 | |
329 | Evil | [
"math"
] | null | null | There are *n* cities on a two dimensional Cartesian plane. The distance between two cities is equal to the Manhattan distance between them (see the Notes for definition). A Hamiltonian cycle of the cities is defined as a permutation of all *n* cities. The length of this Hamiltonian cycle is defined as the sum of the distances between adjacent cities in the permutation plus the distance between the first and final city in the permutation. Please compute the longest possible length of a Hamiltonian cycle of the given cities. | The first line contains an integer *n* (3<=β€<=*n*<=β€<=105). Then *n* lines follow, each consisting of two integers *x**i* and *y**i* (0<=β€<=*x**i*,<=*y**i*<=β€<=109), denoting the coordinates of a city. All given points will be distinct. | A single line denoting the longest possible length of a Hamiltonian cycle of the given cities. You should not output the cycle, only its length.
Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"4\n1 1\n1 2\n2 1\n2 2\n"
] | [
"6\n"
] | In the example, one of the possible Hamiltonian cycles with length 6 is (1, 1) (1, 2) (2, 1) (2, 2). There does not exist any other Hamiltonian cycle with a length greater than 6.
The Manhattan distance between two cities (*x*<sub class="lower-index">*i*</sub>,β*y*<sub class="lower-index">*i*</sub>) and (*x*<sub class="lower-index">*j*</sub>,β*y*<sub class="lower-index">*j*</sub>) is |*x*<sub class="lower-index">*i*</sub>β-β*x*<sub class="lower-index">*j*</sub>|β+β|*y*<sub class="lower-index">*i*</sub>β-β*y*<sub class="lower-index">*j*</sub>|. | [] | 122 | 0 | 0 | 23,678 | |
118 | Caesar's Legions | [
"dp"
] | null | null | Gaius Julius Caesar, a famous general, loved to line up his soldiers. Overall the army had *n*1 footmen and *n*2 horsemen. Caesar thought that an arrangement is not beautiful if somewhere in the line there are strictly more that *k*1 footmen standing successively one after another, or there are strictly more than *k*2 horsemen standing successively one after another. Find the number of beautiful arrangements of the soldiers.
Note that all *n*1<=+<=*n*2 warriors should be present at each arrangement. All footmen are considered indistinguishable among themselves. Similarly, all horsemen are considered indistinguishable among themselves. | The only line contains four space-separated integers *n*1, *n*2, *k*1, *k*2 (1<=β€<=*n*1,<=*n*2<=β€<=100,<=1<=β€<=*k*1,<=*k*2<=β€<=10) which represent how many footmen and horsemen there are and the largest acceptable number of footmen and horsemen standing in succession, correspondingly. | Print the number of beautiful arrangements of the army modulo 100000000 (108). That is, print the number of such ways to line up the soldiers, that no more than *k*1 footmen stand successively, and no more than *k*2 horsemen stand successively. | [
"2 1 1 10\n",
"2 3 1 2\n",
"2 4 1 1\n"
] | [
"1\n",
"5\n",
"0\n"
] | Let's mark a footman as 1, and a horseman as 2.
In the first sample the only beautiful line-up is: 121
In the second sample 5 beautiful line-ups exist: 12122, 12212, 21212, 21221, 22121 | [
{
"input": "2 1 1 10",
"output": "1"
},
{
"input": "2 3 1 2",
"output": "5"
},
{
"input": "2 4 1 1",
"output": "0"
},
{
"input": "10 10 5 7",
"output": "173349"
},
{
"input": "12 15 7 2",
"output": "171106"
},
{
"input": "20 8 4 8",
"output": "1625... | 2,000 | 98,201,600 | 0 | 23,689 | |
87 | Interesting Game | [
"dp",
"games",
"math"
] | C. Interesting Game | 2 | 256 | Two best friends Serozha and Gena play a game.
Initially there is one pile consisting of *n* stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of *a*1<=><=*a*2<=><=...<=><=*a**k*<=><=0 stones. The piles should meet the condition *a*1<=-<=*a*2<==<=*a*2<=-<=*a*3<==<=...<==<=*a**k*<=-<=1<=-<=*a**k*<==<=1. Naturally, the number of piles *k* should be no less than two.
The friends play in turns. The player who cannot make a move loses. Serozha makes the first move. Who will win if both players play in the optimal way? | The single line contains a single integer *n* (1<=β€<=*n*<=β€<=105). | If Serozha wins, print *k*, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game.
If Gena wins, print "-1" (without the quotes). | [
"3\n",
"6\n",
"100\n"
] | [
"2\n",
"-1\n",
"8\n"
] | none | [
{
"input": "3",
"output": "2"
},
{
"input": "6",
"output": "-1"
},
{
"input": "100",
"output": "8"
},
{
"input": "33",
"output": "2"
},
{
"input": "23",
"output": "-1"
},
{
"input": "35",
"output": "-1"
},
{
"input": "15",
"output": "2"... | 1,682 | 2,867,200 | 3.574159 | 23,696 |
637 | Running with Obstacles | [
"*special",
"data structures",
"dp",
"greedy"
] | null | null | A sportsman starts from point *x**start*<==<=0 and runs to point with coordinate *x**finish*<==<=*m* (on a straight line). Also, the sportsman can jump β to jump, he should first take a run of length of not less than *s* meters (in this case for these *s* meters his path should have no obstacles), and after that he can jump over a length of not more than *d* meters. Running and jumping is permitted only in the direction from left to right. He can start andfinish a jump only at the points with integer coordinates in which there are no obstacles. To overcome some obstacle, it is necessary to land at a point which is strictly to the right of this obstacle.
On the way of an athlete are *n* obstacles at coordinates *x*1,<=*x*2,<=...,<=*x**n*. He cannot go over the obstacles, he can only jump over them. Your task is to determine whether the athlete will be able to get to the finish point. | The first line of the input containsd four integers *n*, *m*, *s* and *d* (1<=β€<=*n*<=β€<=200<=000, 2<=β€<=*m*<=β€<=109, 1<=β€<=*s*,<=*d*<=β€<=109)Β β the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly.
The second line contains a sequence of *n* integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*m*<=-<=1)Β β the coordinates of the obstacles. It is guaranteed that the starting and finishing point have no obstacles, also no point can have more than one obstacle, The coordinates of the obstacles are given in an arbitrary order. | If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes).
If the athlete can get from start to finish, print any way to do this in the following format:
- print a line of form "RUN X>" (where "X" should be a positive integer), if the athlete should run for "X" more meters; - print a line of form "JUMP Y" (where "Y" should be a positive integer), if the sportsman starts a jump and should remain in air for "Y" more meters.
All commands "RUN" and "JUMP" should strictly alternate, starting with "RUN", besides, they should be printed chronologically. It is not allowed to jump over the finishing point but it is allowed to land there after a jump. The athlete should stop as soon as he reaches finish. | [
"3 10 1 3\n3 4 7\n",
"2 9 2 3\n6 4\n"
] | [
"RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2\n",
"IMPOSSIBLE\n"
] | none | [
{
"input": "3 10 1 3\n3 4 7",
"output": "RUN 2\nJUMP 3\nRUN 1\nJUMP 2\nRUN 2"
},
{
"input": "2 9 2 3\n6 4",
"output": "IMPOSSIBLE"
},
{
"input": "10 100 2 8\n93 35 24 87 39 46 86 37 73 33",
"output": "RUN 23\nJUMP 2\nRUN 7\nJUMP 8\nRUN 5\nJUMP 2\nRUN 25\nJUMP 2\nRUN 11\nJUMP 3\nRUN 4... | 2,000 | 16,076,800 | 0 | 23,701 | |
219 | Color Stripe | [
"brute force",
"dp",
"greedy"
] | null | null | A colored stripe is represented by a horizontal row of *n* square cells, each cell is pained one of *k* colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to *k* to repaint the cells. | The first input line contains two integers *n* and *k* (1<=β€<=*n*<=β€<=5Β·105;Β 2<=β€<=*k*<=β€<=26). The second line contains *n* uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first *k* English letters may be used. Each letter represents the color of the corresponding cell of the stripe. | Print a single integer β the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe. | [
"6 3\nABBACC\n",
"3 2\nBBB\n"
] | [
"2\nABCACA\n",
"1\nBAB\n"
] | none | [
{
"input": "6 3\nABBACC",
"output": "2\nABCACA"
},
{
"input": "3 2\nBBB",
"output": "1\nBAB"
},
{
"input": "1 2\nA",
"output": "0\nA"
},
{
"input": "1 26\nZ",
"output": "0\nZ"
},
{
"input": "2 2\nAA",
"output": "1\nAB"
},
{
"input": "2 2\nBA",
"out... | 124 | 0 | 0 | 23,719 | |
353 | Queue | [
"constructive algorithms",
"dp"
] | null | null | There are *n* schoolchildren, boys and girls, lined up in the school canteen in front of the bun stall. The buns aren't ready yet and the line is undergoing some changes.
Each second all boys that stand right in front of girls, simultaneously swap places with the girls (so that the girls could go closer to the beginning of the line). In other words, if at some time the *i*-th position has a boy and the (*i*<=+<=1)-th position has a girl, then in a second, the *i*-th position will have a girl and the (*i*<=+<=1)-th one will have a boy.
Let's take an example of a line of four people: a boy, a boy, a girl, a girl (from the beginning to the end of the line). Next second the line will look like that: a boy, a girl, a boy, a girl. Next second it will be a girl, a boy, a girl, a boy. Next second it will be a girl, a girl, a boy, a boy. The line won't change any more.
Your task is: given the arrangement of the children in the line to determine the time needed to move all girls in front of boys (in the example above it takes 3 seconds). Baking buns takes a lot of time, so no one leaves the line until the line stops changing. | The first line contains a sequence of letters without spaces *s*1*s*2... *s**n* (1<=β€<=*n*<=β€<=106), consisting of capital English letters M and F. If letter *s**i* equals M, that means that initially, the line had a boy on the *i*-th position. If letter *s**i* equals F, then initially the line had a girl on the *i*-th position. | Print a single integer β the number of seconds needed to move all the girls in the line in front of the boys. If the line has only boys or only girls, print 0. | [
"MFM\n",
"MMFF\n",
"FFMMM\n"
] | [
"1\n",
"3\n",
"0\n"
] | In the first test case the sequence of changes looks as follows: MFM βββ FMM.
The second test sample corresponds to the sample from the statement. The sequence of changes is: MMFF βββ MFMF βββ FMFM βββ FFMM. | [
{
"input": "MFM",
"output": "1"
},
{
"input": "MMFF",
"output": "3"
},
{
"input": "FFMMM",
"output": "0"
},
{
"input": "MMFMMFFFFM",
"output": "7"
},
{
"input": "MFFFMMFMFMFMFFFMMMFFMMMMMMFMMFFMMMFMMFMFFFMMFMMMFFMMFFFFFMFMFFFMMMFFFMFMFMFMFFFMMMMFMMFMMFFMMMMMMFFM",... | 872 | 10,137,600 | 3 | 23,778 | |
706 | Hard problem | [
"dp",
"strings"
] | null | null | Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.
Vasiliy is given *n* strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).
To reverse the *i*-th string Vasiliy has to spent *c**i* units of energy. He is interested in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.
String *A* is lexicographically smaller than string *B* if it is shorter than *B* (|*A*|<=<<=|*B*|) and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in *A* is smaller than the character in *B*.
For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically. | The first line of the input contains a single integer *n* (2<=β€<=*n*<=β€<=100<=000)Β β the number of strings.
The second line contains *n* integers *c**i* (0<=β€<=*c**i*<=β€<=109), the *i*-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the *i*-th string.
Then follow *n* lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed 100<=000. | If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print <=-<=1. Otherwise, print the minimum total amount of energy Vasiliy has to spent. | [
"2\n1 2\nba\nac\n",
"3\n1 3 1\naa\nba\nac\n",
"2\n5 5\nbbb\naaa\n",
"2\n3 3\naaa\naa\n"
] | [
"1\n",
"1\n",
"-1\n",
"-1\n"
] | In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is smaller.
In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is β-β1.
In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus the answer is β-β1. | [
{
"input": "2\n1 2\nba\nac",
"output": "1"
},
{
"input": "3\n1 3 1\naa\nba\nac",
"output": "1"
},
{
"input": "2\n5 5\nbbb\naaa",
"output": "-1"
},
{
"input": "2\n3 3\naaa\naa",
"output": "-1"
},
{
"input": "4\n0 0 8 6\nbi\nqp\nbt\nya",
"output": "8"
},
{
... | 717 | 16,896,000 | 0 | 23,836 | |
0 | none | [
"none"
] | null | null | Kyoya Ootori has a bag with *n* colored balls that are colored with *k* different colors. The colors are labeled from 1 to *k*. Balls of the same color are indistinguishable. He draws balls from the bag one by one until the bag is empty. He noticed that he drew the last ball of color *i* before drawing the last ball of color *i*<=+<=1 for all *i* from 1 to *k*<=-<=1. Now he wonders how many different ways this can happen. | The first line of input will have one integer *k* (1<=β€<=*k*<=β€<=1000) the number of colors.
Then, *k* lines will follow. The *i*-th line will contain *c**i*, the number of balls of the *i*-th color (1<=β€<=*c**i*<=β€<=1000).
The total number of balls doesn't exceed 1000. | A single integer, the number of ways that Kyoya can draw the balls from the bag as described in the statement, modulo 1<=000<=000<=007. | [
"3\n2\n2\n1\n",
"4\n1\n2\n3\n4\n"
] | [
"3\n",
"1680\n"
] | In the first sample, we have 2 balls of color 1, 2 balls of color 2, and 1 ball of color 3. The three ways for Kyoya are: | [
{
"input": "3\n2\n2\n1",
"output": "3"
},
{
"input": "4\n1\n2\n3\n4",
"output": "1680"
},
{
"input": "10\n100\n100\n100\n100\n100\n100\n100\n100\n100\n100",
"output": "12520708"
},
{
"input": "5\n10\n10\n10\n10\n10",
"output": "425711769"
},
{
"input": "11\n291\n3... | 139 | 102,400 | 3 | 23,843 | |
0 | none | [
"none"
] | null | null | There are *n* walruses standing in a queue in an airport. They are numbered starting from the queue's tail: the 1-st walrus stands at the end of the queue and the *n*-th walrus stands at the beginning of the queue. The *i*-th walrus has the age equal to *a**i*.
The *i*-th walrus becomes displeased if there's a younger walrus standing in front of him, that is, if exists such *j* (*i*<=<<=*j*), that *a**i*<=><=*a**j*. The displeasure of the *i*-th walrus is equal to the number of walruses between him and the furthest walrus ahead of him, which is younger than the *i*-th one. That is, the further that young walrus stands from him, the stronger the displeasure is.
The airport manager asked you to count for each of *n* walruses in the queue his displeasure. | The first line contains an integer *n* (2<=β€<=*n*<=β€<=105) β the number of walruses in the queue. The second line contains integers *a**i* (1<=β€<=*a**i*<=β€<=109).
Note that some walruses can have the same age but for the displeasure to emerge the walrus that is closer to the head of the queue needs to be strictly younger than the other one. | Print *n* numbers: if the *i*-th walrus is pleased with everything, print "-1" (without the quotes). Otherwise, print the *i*-th walrus's displeasure: the number of other walruses that stand between him and the furthest from him younger walrus. | [
"6\n10 8 5 3 50 45\n",
"7\n10 4 6 3 2 8 15\n",
"5\n10 3 1 10 11\n"
] | [
"2 1 0 -1 0 -1 ",
"4 2 1 0 -1 -1 -1 ",
"1 0 -1 -1 -1 "
] | none | [
{
"input": "6\n10 8 5 3 50 45",
"output": "2 1 0 -1 0 -1 "
},
{
"input": "7\n10 4 6 3 2 8 15",
"output": "4 2 1 0 -1 -1 -1 "
},
{
"input": "5\n10 3 1 10 11",
"output": "1 0 -1 -1 -1 "
},
{
"input": "13\n18 9 8 9 23 20 18 18 33 25 31 37 36",
"output": "2 0 -1 -1 2 1 -1 -1 ... | 1,466 | 17,920,000 | 3 | 23,958 | |
0 | none | [
"none"
] | null | null | Santa Claus likes palindromes very much. There was his birthday recently. *k* of his friends came to him to congratulate him, and each of them presented to him a string *s**i* having the same length *n*. We denote the beauty of the *i*-th string by *a**i*. It can happen that *a**i* is negativeΒ β that means that Santa doesn't find this string beautiful at all.
Santa Claus is crazy about palindromes. He is thinking about the following question: what is the maximum possible total beauty of a palindrome which can be obtained by concatenating some (possibly all) of the strings he has? Each present can be used at most once. Note that all strings have the same length *n*.
Recall that a palindrome is a string that doesn't change after one reverses it.
Since the empty string is a palindrome too, the answer can't be negative. Even if all *a**i*'s are negative, Santa can obtain the empty string. | The first line contains two positive integers *k* and *n* divided by space and denoting the number of Santa friends and the length of every string they've presented, respectively (1<=β€<=*k*,<=*n*<=β€<=100<=000; *n*Β·*k*Β <=β€<=100<=000).
*k* lines follow. The *i*-th of them contains the string *s**i* and its beauty *a**i* (<=-<=10<=000<=β€<=*a**i*<=β€<=10<=000). The string consists of *n* lowercase English letters, and its beauty is integer. Some of strings may coincide. Also, equal strings can have different beauties. | In the only line print the required maximum possible beauty. | [
"7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4\n",
"3 1\na 1\na 2\na 3\n",
"2 5\nabcde 10000\nabcde 10000\n"
] | [
"12\n",
"6\n",
"0\n"
] | In the first example Santa can obtain abbaaaxyxaaabba by concatenating strings 5, 2, 7, 6 and 3 (in this order). | [
{
"input": "7 3\nabb 2\naaa -3\nbba -1\nzyz -4\nabb 5\naaa 7\nxyx 4",
"output": "12"
},
{
"input": "3 1\na 1\na 2\na 3",
"output": "6"
},
{
"input": "2 5\nabcde 10000\nabcde 10000",
"output": "0"
},
{
"input": "10 10\nnjxbzflaka -1\nfelbvvtkja 6\ngxiuztqkcw 5\naomvscmtti 6\nj... | 389 | 7,475,200 | 0 | 23,967 | |
252 | Little Xor | [
"brute force",
"implementation"
] | null | null | Little Petya likes arrays that consist of non-negative integers a lot. Recently his mom has presented him one such array consisting of *n* elements. Petya immediately decided to find there a segment of consecutive elements, such that the *xor* of all numbers from this segment was maximal possible. Help him with that.
The *xor* operation is the bitwise exclusive "OR", that is denoted as "xor" in Pascal and "^" in C/C++/Java. | The first line contains integer *n* (1<=β€<=*n*<=β€<=100) β the number of elements in the array. The second line contains the space-separated integers from the array. All numbers are non-negative integers strictly less than 230. | Print a single integer β the required maximal *xor* of a segment of consecutive elements. | [
"5\n1 2 1 1 2\n",
"3\n1 2 7\n",
"4\n4 2 4 8\n"
] | [
"3\n",
"7\n",
"14\n"
] | In the first sample one of the optimal segments is the segment that consists of the first and the second array elements, if we consider the array elements indexed starting from one.
The second sample contains only one optimal segment, which contains exactly one array element (element with index three). | [
{
"input": "5\n1 2 1 1 2",
"output": "3"
},
{
"input": "3\n1 2 7",
"output": "7"
},
{
"input": "4\n4 2 4 8",
"output": "14"
},
{
"input": "5\n1 1 1 1 1",
"output": "1"
},
{
"input": "16\n0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15",
"output": "15"
},
{
"inpu... | 186 | 3,481,600 | 3 | 24,008 | |
442 | Gena and Second Distance | [
"geometry"
] | null | null | Gena doesn't like geometry, so he asks you to solve this problem for him.
A rectangle with sides parallel to coordinate axes contains *n* dots. Let's consider some point of the plane. Let's count the distances from this point to the given *n* points. Let's sort these numbers in the non-decreasing order. We'll call the beauty of the point the second element of this array. If there are two mimimum elements in this array, the beaty will be equal to this minimum.
Find the maximum beauty of a point inside the given rectangle. | The first line contains three integers *w*,<=*h*,<=*n* (1<=β€<=*w*,<=*h*<=β€<=106,<=2<=β€<=*n*<=β€<=1000) β the lengths of the rectangle sides and the number of points. Next *n* lines contain two integers *x**i*,<=*y**i* (0<=β€<=*x**i*<=β€<=*w*,<=0<=β€<=*y**i*<=β€<=*h*) each β the coordinates of a point. It is possible that it will be coincident points. | Print a single number β the maximum beauty of a point with the absolute or relative error of at most 10<=-<=9. | [
"5 5 4\n0 0\n5 0\n0 5\n5 5\n",
"5 5 3\n4 0\n2 5\n4 1\n"
] | [
"4.99999999941792340\n",
"5.65685424744772010\n"
] | The point which beauty we need to find must have coordinates (*x*,β*y*), where 0ββ€β*x*ββ€β*w*,β0ββ€β*y*ββ€β*h*. Some of the *n* points can coincide. | [] | 30 | 0 | 0 | 24,013 | |
314 | Sereja and Contest | [
"implementation"
] | null | null | During the last Sereja's Codesecrof round the server crashed many times, so the round was decided to be made unrated for some participants.
Let's assume that *n* people took part in the contest. Let's assume that the participant who got the first place has rating *a*1, the second place participant has rating *a*2, ..., the *n*-th place participant has rating *a**n*. Then changing the rating on the Codesecrof site is calculated by the formula .
After the round was over, the Codesecrof management published the participants' results table. They decided that if for a participant *d**i*<=<<=*k*, then the round can be considered unrated for him. But imagine the management's surprise when they found out that the participants' rating table is dynamic. In other words, when some participant is removed from the rating, he is removed from the results' table and the rating is recalculated according to the new table. And of course, all applications for exclusion from the rating are considered in view of the current table.
We know that among all the applications for exclusion from the rating the first application to consider is from the participant with the best rank (the rank with the minimum number), for who *d**i*<=<<=*k*. We also know that the applications for exclusion from rating were submitted by all participants.
Now Sereja wonders, what is the number of participants to be excluded from the contest rating, and the numbers of the participants in the original table in the order of their exclusion from the rating. Pay attention to the analysis of the first test case for a better understanding of the statement. | The first line contains two integers *n*, *k* (1<=β€<=*n*<=β€<=2Β·105,<=<=-<=109<=β€<=*k*<=β€<=0). The second line contains *n* space-separated integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β ratings of the participants in the initial table. | Print the numbers of participants in the order in which they were removed from the table. Print the initial numbers of the participants, that is, the numbers that the participants had in the initial table. | [
"5 0\n5 3 4 1 2\n",
"10 -10\n5 5 1 7 5 1 2 4 9 2\n"
] | [
"2\n3\n4\n",
"2\n4\n5\n7\n8\n9\n"
] | Consider the first test sample.
1. Initially the sequence of the contest participants' ratings equals [5, 3, 4, 1, 2]. You can use this sequence to calculate the sequence of rating changes: [0, -9, -13, 8, 14]. According to the problem statement, the application of the participant who won the second place will be considered first.1. As soon as the second place winner is out from the ratings, the participants' rating sequence will equal [5, 4, 1, 2]. By this sequence you can count the new sequence of rating changes: [0, -8, 2, 6]. According to the problem statement, the application of the participant who won the second place will be considered. Initially this participant won third place.1. The new rating sequence equals [5, 1, 2], the new sequence of rating changes equals [0, -1, 1]. The second place participant's application is taken into consideration, initially this participant won the fourth place.1. The new rating sequence equals [5, 2], the new sequence of rating changes equals [0, 0]. No more applications will be considered.
Thus, you should print 2, 3, 4. | [
{
"input": "5 0\n5 3 4 1 2",
"output": "2\n3\n4"
},
{
"input": "10 -10\n5 5 1 7 5 1 2 4 9 2",
"output": "2\n4\n5\n7\n8\n9"
}
] | 122 | 0 | 0 | 24,055 | |
103 | Russian Roulette | [
"constructive algorithms",
"greedy"
] | C. Russian Roulette | 2 | 256 | After all the events in Orlando we all know, Sasha and Roma decided to find out who is still the team's biggest loser. Thankfully, Masha found somewhere a revolver with a rotating cylinder of *n* bullet slots able to contain exactly *k* bullets, now the boys have a chance to resolve the problem once and for all.
Sasha selects any *k* out of *n* slots he wishes and puts bullets there. Roma spins the cylinder so that every of *n* possible cylinder's shifts is equiprobable. Then the game starts, the players take turns, Sasha starts: he puts the gun to his head and shoots. If there was no bullet in front of the trigger, the cylinder shifts by one position and the weapon is given to Roma for make the same move. The game continues until someone is shot, the survivor is the winner.
Sasha does not want to lose, so he must choose slots for bullets in such a way as to minimize the probability of its own loss. Of all the possible variant he wants to select the lexicographically minimal one, where an empty slot is lexicographically less than a charged one.
More formally, the cylinder of *n* bullet slots able to contain *k* bullets can be represented as a string of *n* characters. Exactly *k* of them are "X" (charged slots) and the others are "." (uncharged slots).
Let us describe the process of a shot. Suppose that the trigger is in front of the first character of the string (the first slot). If a shot doesn't kill anyone and the cylinder shifts, then the string shifts left. So the first character becomes the last one, the second character becomes the first one, and so on. But the trigger doesn't move. It will be in front of the first character of the resulting string.
Among all the strings that give the minimal probability of loss, Sasha choose the lexicographically minimal one. According to this very string, he charges the gun. You have to help Sasha to charge the gun. For that, each *x**i* query must be answered: is there a bullet in the positions *x**i*? | The first line contains three integers *n*, *k* and *p* (1<=β€<=*n*<=β€<=1018,<=0<=β€<=*k*<=β€<=*n*,<=1<=β€<=*p*<=β€<=1000) β the number of slots in the cylinder, the number of bullets and the number of queries. Then follow *p* lines; they are the queries. Each line contains one integer *x**i* (1<=β€<=*x**i*<=β€<=*n*) the number of slot to describe.
Please do not use the %lld specificator to read or write 64-bit numbers in Π‘++. It is preferred to use cin, cout streams or the %I64d specificator. | For each query print "." if the slot should be empty and "X" if the slot should be charged. | [
"3 1 3\n1\n2\n3\n",
"6 3 6\n1\n2\n3\n4\n5\n6\n",
"5 2 5\n1\n2\n3\n4\n5\n"
] | [
"..X",
".X.X.X",
"...XX"
] | The lexicographical comparison of is performed by the < operator in modern programming languages. The *a* string is lexicographically less that the *b* string, if there exists such *i* (1ββ€β*i*ββ€β*n*), that *a*<sub class="lower-index">*i*</sub>β<β*b*<sub class="lower-index">*i*</sub>, and for any *j* (1ββ€β*j*β<β*i*) *a*<sub class="lower-index">*j*</sub>β=β*b*<sub class="lower-index">*j*</sub>. | [
{
"input": "3 1 3\n1\n2\n3",
"output": "..X"
},
{
"input": "6 3 6\n1\n2\n3\n4\n5\n6",
"output": ".X.X.X"
},
{
"input": "5 2 5\n1\n2\n3\n4\n5",
"output": "...XX"
},
{
"input": "4 2 8\n1\n3\n4\n2\n3\n4\n1\n2",
"output": "..XX.X.X"
},
{
"input": "4 0 4\n1\n2\n3\n4",
... | 216 | 0 | 0 | 24,065 |
509 | Sums of Digits | [
"dp",
"greedy",
"implementation"
] | null | null | Vasya had a strictly increasing sequence of positive integers *a*1, ..., *a**n*. Vasya used it to build a new sequence *b*1, ..., *b**n*, where *b**i* is the sum of digits of *a**i*'s decimal representation. Then sequence *a**i* got lost and all that remained is sequence *b**i*.
Vasya wonders what the numbers *a**i* could be like. Of all the possible options he likes the one sequence with the minimum possible last number *a**n*. Help Vasya restore the initial sequence.
It is guaranteed that such a sequence always exists. | The first line contains a single integer number *n* (1<=β€<=*n*<=β€<=300).
Next *n* lines contain integer numbers *b*1, ..., *b**n* Β β the required sums of digits. All *b**i* belong to the range 1<=β€<=*b**i*<=β€<=300. | Print *n* integer numbers, one per lineΒ β the correct option for numbers *a**i*, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the *i*-th number should be equal to *b**i*.
If there are multiple sequences with least possible number *a**n*, print any of them. Print the numbers without leading zeroes. | [
"3\n1\n2\n3\n",
"3\n3\n2\n1\n"
] | [
"1\n2\n3\n",
"3\n11\n100\n"
] | none | [
{
"input": "3\n1\n2\n3",
"output": "1\n2\n3"
},
{
"input": "3\n3\n2\n1",
"output": "3\n11\n100"
},
{
"input": "10\n1\n2\n3\n4\n5\n6\n7\n8\n9\n1",
"output": "1\n2\n3\n4\n5\n6\n7\n8\n9\n10"
},
{
"input": "10\n8\n8\n5\n1\n2\n7\n3\n8\n9\n4",
"output": "8\n17\n23\n100\n101\n10... | 233 | 2,560,000 | 3 | 24,155 | |
50 | Square Equation Roots | [
"math"
] | E. Square Equation Roots | 5 | 256 | A schoolboy Petya studies square equations. The equations that are included in the school curriculum, usually look simple:
Petya noticed that some equations have two real roots, some of them have only one root and some equations don't have real roots at all. Moreover it turned out that several different square equations can have a common root.
Petya is interested in how many different real roots have all the equations of the type described above for all the possible pairs of numbers *b* and *c* such that 1<=β€<=*b*<=β€<=*n*, 1<=β€<=*c*<=β€<=*m*. Help Petya find that number. | The single line contains two integers *n* and *m*. (1<=β€<=*n*,<=*m*<=β€<=5000000). | Print a single number which is the number of real roots of the described set of equations. | [
"3 3\n",
"1 2\n"
] | [
"12\n",
"1\n"
] | In the second test from the statement the following equations are analysed:
*b*β=β1, *c*β=β1: *x*<sup class="upper-index">2</sup>β+β2*x*β+β1β=β0; The root is *x*β=ββ-β1
*b*β=β1, *c*β=β2: *x*<sup class="upper-index">2</sup>β+β2*x*β+β2β=β0; No roots
Overall there's one root
In the second test the following equations are analysed:
*b*β=β1, *c*β=β1: *x*<sup class="upper-index">2</sup>β+β2*x*β+β1β=β0; The root is *x*β=ββ-β1
*b*β=β1, *c*β=β2: *x*<sup class="upper-index">2</sup>β+β2*x*β+β2β=β0; No roots
*b*β=β1, *c*β=β3: *x*<sup class="upper-index">2</sup>β+β2*x*β+β3β=β0; No roots
*b*β=β2, *c*β=β1: *x*<sup class="upper-index">2</sup>β+β4*x*β+β1β=β0; The roots are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/d9d9178e998a736dbbe847a1ce187d086a88bbf9.png" style="max-width: 100.0%;max-height: 100.0%;"/>
*b*β=β2, *c*β=β2: *x*<sup class="upper-index">2</sup>β+β4*x*β+β2β=β0; The roots are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5333195e873a6fc26a7646deed470a771d731f15.png" style="max-width: 100.0%;max-height: 100.0%;"/>
*b*β=β2, *c*β=β3: *x*<sup class="upper-index">2</sup>β+β4*x*β+β3β=β0; The roots are *x*<sub class="lower-index">1</sub>β=ββ-β3,β*x*<sub class="lower-index">2</sub>β=ββ-β1
*b*β=β3, *c*β=β1: *x*<sup class="upper-index">2</sup>β+β6*x*β+β1β=β0; The roots are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/2f6d171c4838713ef8224fd6ebc2744bea54f33e.png" style="max-width: 100.0%;max-height: 100.0%;"/>
*b*β=β3, *c*β=β2: *x*<sup class="upper-index">2</sup>β+β6*x*β+β2β=β0; The roots are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/0448361df88c70bcd0ae858432e1c1eca7248165.png" style="max-width: 100.0%;max-height: 100.0%;"/>
*b*β=β3, *c*β=β3: *x*<sup class="upper-index">2</sup>β+β6*x*β+β3β=β0; The roots are <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/6d4b99328193cd25b1099bc7885851919959a826.png" style="max-width: 100.0%;max-height: 100.0%;"/> Overall there are 13 roots and as the root β-β1 is repeated twice, that means there are 12 different roots. | [] | 154 | 31,744,000 | -1 | 24,164 |
0 | none | [
"none"
] | null | null | Analyzing the mistakes people make while typing search queries is a complex and an interesting work. As there is no guaranteed way to determine what the user originally meant by typing some query, we have to use different sorts of heuristics.
Polycarp needed to write a code that could, given two words, check whether they could have been obtained from the same word as a result of typos. Polycarpus suggested that the most common typo is skipping exactly one letter as you type a word.
Implement a program that can, given two distinct words *S* and *T* of the same length *n* determine how many words *W* of length *n*<=+<=1 are there with such property that you can transform *W* into both *S*, and *T* by deleting exactly one character. Words *S* and *T* consist of lowercase English letters. Word *W* also should consist of lowercase English letters. | The first line contains integer *n* (1<=β€<=*n*<=β€<=100<=000) β the length of words *S* and *T*.
The second line contains word *S*.
The third line contains word *T*.
Words *S* and *T* consist of lowercase English letters. It is guaranteed that *S* and *T* are distinct words. | Print a single integer β the number of distinct words *W* that can be transformed to *S* and *T* due to a typo. | [
"7\nreading\ntrading\n",
"5\nsweet\nsheep\n",
"3\ntoy\ntry\n"
] | [
"1\n",
"0\n",
"2\n"
] | In the first sample test the two given words could be obtained only from word "treading" (the deleted letters are marked in bold).
In the second sample test the two given words couldn't be obtained from the same word by removing one letter.
In the third sample test the two given words could be obtained from either word "tory" or word "troy". | [
{
"input": "7\nreading\ntrading",
"output": "1"
},
{
"input": "5\nsweet\nsheep",
"output": "0"
},
{
"input": "3\ntoy\ntry",
"output": "2"
},
{
"input": "5\nspare\nspars",
"output": "2"
},
{
"input": "1\na\nb",
"output": "2"
},
{
"input": "1\nz\ny",
... | 124 | 1,843,200 | 3 | 24,175 | |
417 | Cunning Gena | [
"bitmasks",
"dp",
"greedy",
"sortings"
] | null | null | A boy named Gena really wants to get to the "Russian Code Cup" finals, or at least get a t-shirt. But the offered problems are too complex, so he made an arrangement with his *n* friends that they will solve the problems for him.
The participants are offered *m* problems on the contest. For each friend, Gena knows what problems he can solve. But Gena's friends won't agree to help Gena for nothing: the *i*-th friend asks Gena *x**i* rubles for his help in solving all the problems he can. Also, the friend agreed to write a code for Gena only if Gena's computer is connected to at least *k**i* monitors, each monitor costs *b* rubles.
Gena is careful with money, so he wants to spend as little money as possible to solve all the problems. Help Gena, tell him how to spend the smallest possible amount of money. Initially, there's no monitors connected to Gena's computer. | The first line contains three integers *n*, *m* and *b* (1<=β€<=*n*<=β€<=100; 1<=β€<=*m*<=β€<=20; 1<=β€<=*b*<=β€<=109)Β β the number of Gena's friends, the number of problems and the cost of a single monitor.
The following 2*n* lines describe the friends. Lines number 2*i* and (2*i*<=+<=1) contain the information about the *i*-th friend. The 2*i*-th line contains three integers *x**i*, *k**i* and *m**i* (1<=β€<=*x**i*<=β€<=109; 1<=β€<=*k**i*<=β€<=109; 1<=β€<=*m**i*<=β€<=*m*)Β β the desired amount of money, monitors and the number of problems the friend can solve. The (2*i*<=+<=1)-th line contains *m**i* distinct positive integersΒ β the numbers of problems that the *i*-th friend can solve. The problems are numbered from 1 to *m*. | Print the minimum amount of money Gena needs to spend to solve all the problems. Or print -1, if this cannot be achieved. | [
"2 2 1\n100 1 1\n2\n100 2 1\n1\n",
"3 2 5\n100 1 1\n1\n100 1 1\n2\n200 1 2\n1 2\n",
"1 2 1\n1 1 1\n1\n"
] | [
"202\n",
"205\n",
"-1\n"
] | none | [
{
"input": "2 2 1\n100 1 1\n2\n100 2 1\n1",
"output": "202"
},
{
"input": "3 2 5\n100 1 1\n1\n100 1 1\n2\n200 1 2\n1 2",
"output": "205"
},
{
"input": "1 2 1\n1 1 1\n1",
"output": "-1"
},
{
"input": "4 2 1\n62 91 1\n1\n2 18 1\n1\n33 76 1\n1\n23 58 1\n1",
"output": "-1"
... | 124 | 0 | 0 | 24,213 | |
677 | Vanya and Label | [
"bitmasks",
"combinatorics",
"implementation",
"strings"
] | null | null | While walking down the street Vanya saw a label "Hide&Seek". Because he is a programmer, he used & as a bitwise AND for these two words represented as a integers in base 64 and got new word. Now Vanya thinks of some string *s* and wants to know the number of pairs of words of length |*s*| (length of *s*), such that their bitwise AND is equal to *s*. As this number can be large, output it modulo 109<=+<=7.
To represent the string as a number in numeral system with base 64 Vanya uses the following rules:
- digits from '0' to '9' correspond to integers from 0 to 9; - letters from 'A' to 'Z' correspond to integers from 10 to 35; - letters from 'a' to 'z' correspond to integers from 36 to 61; - letter '-' correspond to integer 62; - letter '_' correspond to integer 63. | The only line of the input contains a single word *s* (1<=β€<=|*s*|<=β€<=100<=000), consisting of digits, lowercase and uppercase English letters, characters '-' and '_'. | Print a single integerΒ β the number of possible pairs of words, such that their bitwise AND is equal to string *s* modulo 109<=+<=7. | [
"z\n",
"V_V\n",
"Codeforces\n"
] | [
"3\n",
"9\n",
"130653412\n"
] | For a detailed definition of bitwise AND we recommend to take a look in the corresponding article in Wikipedia.
In the first sample, there are 3 possible solutions:
1. *z*&_β=β61&63β=β61β=β*z* 1. _&*z*β=β63&61β=β61β=β*z* 1. *z*&*z*β=β61&61β=β61β=β*z* | [
{
"input": "z",
"output": "3"
},
{
"input": "V_V",
"output": "9"
},
{
"input": "Codeforces",
"output": "130653412"
},
{
"input": "zHsIINYjVtU71kmM9E",
"output": "130312847"
},
{
"input": "fRRNAdMvLFTX21T0FG5gyn7NG0SaIvzGG_g_SO",
"output": "547121709"
},
{
... | 108 | 7,987,200 | 3 | 24,281 | |
546 | Soldier and Traveling | [
"flows",
"graphs",
"math"
] | null | null | In the country there are *n* cities and *m* bidirectional roads between them. Each city has an army. Army of the *i*-th city consists of *a**i* soldiers. Now soldiers roam. After roaming each soldier has to either stay in his city or to go to the one of neighboring cities by at moving along at most one road.
Check if is it possible that after roaming there will be exactly *b**i* soldiers in the *i*-th city. | First line of input consists of two integers *n* and *m* (1<=β€<=*n*<=β€<=100, 0<=β€<=*m*<=β€<=200).
Next line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* (0<=β€<=*a**i*<=β€<=100).
Next line contains *n* integers *b*1,<=*b*2,<=...,<=*b**n* (0<=β€<=*b**i*<=β€<=100).
Then *m* lines follow, each of them consists of two integers *p* and *q* (1<=β€<=*p*,<=*q*<=β€<=*n*, *p*<=β <=*q*) denoting that there is an undirected road between cities *p* and *q*.
It is guaranteed that there is at most one road between each pair of cities. | If the conditions can not be met output single word "NO".
Otherwise output word "YES" and then *n* lines, each of them consisting of *n* integers. Number in the *i*-th line in the *j*-th column should denote how many soldiers should road from city *i* to city *j* (if *i*<=β <=*j*) or how many soldiers should stay in city *i* (if *i*<==<=*j*).
If there are several possible answers you may output any of them. | [
"4 4\n1 2 6 3\n3 5 3 1\n1 2\n2 3\n3 4\n4 2\n",
"2 0\n1 2\n2 1\n"
] | [
"YES\n1 0 0 0 \n2 0 0 0 \n0 5 1 0 \n0 0 2 1 \n",
"NO"
] | none | [
{
"input": "4 4\n1 2 6 3\n3 5 3 1\n1 2\n2 3\n3 4\n4 2",
"output": "YES\n1 0 0 0 \n2 0 0 0 \n0 5 1 0 \n0 0 2 1 "
},
{
"input": "2 0\n1 2\n2 1",
"output": "NO"
},
{
"input": "10 20\n22 3 4 48 12 12 14 37 15 37\n57 29 35 88 6 54 100 32 91 59\n10 1\n4 3\n4 7\n2 4\n7 6\n1 3\n2 5\n5 10\n1 6\n4... | 124 | 10,854,400 | 0 | 24,298 | |
285 | Permutation Sum | [
"bitmasks",
"combinatorics",
"dp",
"implementation",
"meet-in-the-middle"
] | null | null | Permutation *p* is an ordered set of integers *p*1,<=<=*p*2,<=<=...,<=<=*p**n*, consisting of *n* distinct positive integers, each of them doesn't exceed *n*. We'll denote the *i*-th element of permutation *p* as *p**i*. We'll call number *n* the size or the length of permutation *p*1,<=<=*p*2,<=<=...,<=<=*p**n*.
Petya decided to introduce the sum operation on the set of permutations of length *n*. Let's assume that we are given two permutations of length *n*: *a*1,<=*a*2,<=...,<=*a**n* and *b*1,<=*b*2,<=...,<=*b**n*. Petya calls the sum of permutations *a* and *b* such permutation *c* of length *n*, where *c**i*<==<=((*a**i*<=-<=1<=+<=*b**i*<=-<=1) *mod* *n*)<=+<=1 (1<=β€<=*i*<=β€<=*n*).
Operation means taking the remainder after dividing number *x* by number *y*.
Obviously, not for all permutations *a* and *b* exists permutation *c* that is sum of *a* and *b*. That's why Petya got sad and asked you to do the following: given *n*, count the number of such pairs of permutations *a* and *b* of length *n*, that exists permutation *c* that is sum of *a* and *b*. The pair of permutations *x*,<=*y* (*x*<=β <=*y*) and the pair of permutations *y*,<=*x* are considered distinct pairs.
As the answer can be rather large, print the remainder after dividing it by 1000000007 (109<=+<=7). | The single line contains integer *n* (1<=β€<=*n*<=β€<=16). | In the single line print a single non-negative integer β the number of such pairs of permutations *a* and *b*, that exists permutation *c* that is sum of *a* and *b*, modulo 1000000007 (109<=+<=7). | [
"3\n",
"5\n"
] | [
"18\n",
"1800\n"
] | none | [
{
"input": "3",
"output": "18"
},
{
"input": "5",
"output": "1800"
},
{
"input": "13",
"output": "695720788"
},
{
"input": "1",
"output": "1"
},
{
"input": "2",
"output": "0"
},
{
"input": "4",
"output": "0"
},
{
"input": "6",
"output":... | 92 | 0 | 0 | 24,333 | |
117 | Elevator | [
"implementation",
"math"
] | null | null | And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All *n* participants who have made it to the finals found themselves in a huge *m*-floored 108-star hotel. Of course the first thought to come in a place like this is "How about checking out the elevator?".
The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then β to the 3-rd floor and so on until it reaches the *m*-th floor. After that the elevator moves to floor *m*<=-<=1, then to floor *m*<=-<=2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.
For each of the *n* participant you are given *s**i*, which represents the floor where the *i*-th participant starts, *f**i*, which represents the floor the *i*-th participant wants to reach, and *t**i*, which represents the time when the *i*-th participant starts on the floor *s**i*.
For each participant print the minimum time of his/her arrival to the floor *f**i*.
If the elevator stops on the floor *s**i* at the time *t**i*, then the *i*-th participant can enter the elevator immediately. If the participant starts on the floor *s**i* and that's the floor he wanted to reach initially (*s**i*<==<=*f**i*), then the time of arrival to the floor *f**i* for this participant is considered equal to *t**i*. | The first line contains two space-separated integers *n* and *m* (1<=β€<=*n*<=β€<=105,<=2<=β€<=*m*<=β€<=108).
Next *n* lines contain information about the participants in the form of three space-separated integers *s**i* *f**i* *t**i* (1<=β€<=*s**i*,<=*f**i*<=β€<=*m*,<=0<=β€<=*t**i*<=β€<=108), described in the problem statement. | Print *n* lines each containing one integer β the time of the arrival for each participant to the required floor. | [
"7 4\n2 4 3\n1 2 0\n2 2 0\n1 2 1\n4 3 5\n1 2 2\n4 2 0\n",
"5 5\n1 5 4\n1 3 1\n1 3 4\n3 1 5\n4 2 5\n"
] | [
"9\n1\n0\n7\n10\n7\n5\n",
"12\n10\n10\n8\n7\n"
] | Let's consider the first sample. The first participant starts at floor *s*β=β2 by the time equal to *t*β=β3. To get to the floor *f*β=β4, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. In this case the first participant gets to the floor *f* at time equal to 9. The second participant starts at the time *t*β=β0 on the floor *s*β=β1, enters the elevator immediately, and arrives to the floor *f*β=β2. The third participant doesn't wait for the elevator, because he needs to arrive to the same floor where he starts. | [
{
"input": "7 4\n2 4 3\n1 2 0\n2 2 0\n1 2 1\n4 3 5\n1 2 2\n4 2 0",
"output": "9\n1\n0\n7\n10\n7\n5"
},
{
"input": "5 5\n1 5 4\n1 3 1\n1 3 4\n3 1 5\n4 2 5",
"output": "12\n10\n10\n8\n7"
},
{
"input": "5 5\n1 3 4\n4 4 2\n3 2 1\n2 4 0\n1 5 3",
"output": "10\n2\n7\n3\n12"
},
{
"i... | 0 | 0 | -1 | 24,352 | |
576 | Invariance of Tree | [
"constructive algorithms",
"dfs and similar",
"greedy",
"trees"
] | null | null | A tree of size *n* is an undirected connected graph consisting of *n* vertices without cycles.
Consider some tree with *n* vertices. We call a tree invariant relative to permutation *p*<==<=*p*1*p*2... *p**n*, if for any two vertices of the tree *u* and *v* the condition holds: "vertices *u* and *v* are connected by an edge if and only if vertices *p**u* and *p**v* are connected by an edge".
You are given permutation *p* of size *n*. Find some tree size *n*, invariant relative to the given permutation. | The first line contains number *n* (1<=β€<=*n*<=β€<=105) β the size of the permutation (also equal to the size of the sought tree).
The second line contains permutation *p**i* (1<=β€<=*p**i*<=β€<=*n*). | If the sought tree does not exist, print "NO" (without the quotes).
Otherwise, print "YES", and then print *n*<=-<=1 lines, each of which contains two integers β the numbers of vertices connected by an edge of the tree you found. The vertices are numbered from 1, the order of the edges and the order of the vertices within the edges does not matter.
If there are multiple solutions, output any of them. | [
"4\n4 3 2 1\n",
"3\n3 1 2\n"
] | [
"YES\n4 1\n4 2\n1 3\n",
"NO\n"
] | In the first sample test a permutation transforms edge (4, 1) into edge (1, 4), edge (4, 2) into edge (1, 3) and edge (1, 3) into edge (4, 2). These edges all appear in the resulting tree.
It can be shown that in the second sample test no tree satisfies the given condition. | [
{
"input": "4\n4 3 2 1",
"output": "YES\n4 1\n4 2\n1 3"
},
{
"input": "3\n3 1 2",
"output": "NO"
},
{
"input": "3\n3 2 1",
"output": "YES\n2 1\n2 3"
},
{
"input": "4\n3 4 1 2",
"output": "YES\n4 2\n4 1\n2 3"
},
{
"input": "5\n5 3 2 1 4",
"output": "NO"
},
... | 93 | 0 | 0 | 24,361 | |
337 | Routine Problem | [
"greedy",
"math",
"number theory"
] | null | null | Manao has a monitor. The screen of the monitor has horizontal to vertical length ratio *a*:*b*. Now he is going to watch a movie. The movie's frame has horizontal to vertical length ratio *c*:*d*. Manao adjusts the view in such a way that the movie preserves the original frame ratio, but also occupies as much space on the screen as possible and fits within it completely. Thus, he may have to zoom the movie in or out, but Manao will always change the frame proportionally in both dimensions.
Calculate the ratio of empty screen (the part of the screen not occupied by the movie) to the total screen size. Print the answer as an irreducible fraction *p*<=/<=*q*. | A single line contains four space-separated integers *a*, *b*, *c*, *d* (1<=β€<=*a*,<=*b*,<=*c*,<=*d*<=β€<=1000). | Print the answer to the problem as "p/q", where *p* is a non-negative integer, *q* is a positive integer and numbers *p* and *q* don't have a common divisor larger than 1. | [
"1 1 3 2\n",
"4 3 2 2\n"
] | [
"1/3\n",
"1/4\n"
] | Sample 1. Manao's monitor has a square screen. The movie has 3:2 horizontal to vertical length ratio. Obviously, the movie occupies most of the screen if the width of the picture coincides with the width of the screen. In this case, only 2/3 of the monitor will project the movie in the horizontal dimension: <img class="tex-graphics" src="https://espresso.codeforces.com/ce823413ad27813e27496a0d8bd4231e94b47662.png" style="max-width: 100.0%;max-height: 100.0%;"/>
Sample 2. This time the monitor's width is 4/3 times larger than its height and the movie's frame is square. In this case, the picture must take up the whole monitor in the vertical dimension and only 3/4 in the horizontal dimension: <img class="tex-graphics" src="https://espresso.codeforces.com/c2bcb3b1f64810812eee368ff180e3e148d24c67.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "1 1 3 2",
"output": "1/3"
},
{
"input": "4 3 2 2",
"output": "1/4"
},
{
"input": "3 4 2 3",
"output": "1/9"
},
{
"input": "4 4 5 5",
"output": "0/1"
},
{
"input": "1 1 1 1",
"output": "0/1"
},
{
"input": "1000 1000 1000 1000",
"output":... | 30 | 0 | 0 | 24,369 | |
177 | Space Voyage | [
"binary search"
] | null | null | The Smart Beaver from ABBYY plans a space travel on an ultramodern spaceship. During the voyage he plans to visit *n* planets. For planet *i* *a**i* is the maximum number of suitcases that an alien tourist is allowed to bring to the planet, and *b**i* is the number of citizens on the planet.
The Smart Beaver is going to bring some presents from ABBYY to the planets he will be visiting. The presents are packed in suitcases, *x* presents in each. The Beaver will take to the ship exactly *a*1<=+<=...<=+<=*a**n* suitcases.
As the Beaver lands on the *i*-th planet, he takes *a**i* suitcases and goes out. On the first day on the planet the Beaver takes a walk and gets to know the citizens. On the second and all subsequent days the Beaver gives presents to the citizens β each of the *b**i* citizens gets one present per day. The Beaver leaves the planet in the evening of the day when the number of presents left is strictly less than the number of citizens (i.e. as soon as he won't be able to give away the proper number of presents the next day). He leaves the remaining presents at the hotel.
The Beaver is going to spend exactly *c* days traveling. The time spent on flights between the planets is considered to be zero. In how many ways can one choose the positive integer *x* so that the planned voyage will take exactly *c* days? | The first input line contains space-separated integers *n* and *c* β the number of planets that the Beaver is going to visit and the number of days he is going to spend traveling, correspondingly.
The next *n* lines contain pairs of space-separated integers *a**i*,<=*b**i* (1<=β€<=*i*<=β€<=*n*) β the number of suitcases he can bring to the *i*-th planet and the number of citizens of the *i*-th planet, correspondingly.
The input limitations for getting 30 points are:
- 1<=β€<=*n*<=β€<=100 - 1<=β€<=*a**i*<=β€<=100 - 1<=β€<=*b**i*<=β€<=100 - 1<=β€<=*c*<=β€<=100
The input limitations for getting 100 points are:
- 1<=β€<=*n*<=β€<=104 - 0<=β€<=*a**i*<=β€<=109 - 1<=β€<=*b**i*<=β€<=109 - 1<=β€<=*c*<=β€<=109
Due to possible overflow, it is recommended to use the 64-bit arithmetic. In some solutions even the 64-bit arithmetic can overflow. So be careful in calculations! | Print a single number *k* β the number of ways to choose *x* so as to travel for exactly *c* days. If there are infinitely many possible values of *x*, print -1.
Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use cin, cout streams or the %I64d specifier. | [
"2 5\n1 5\n2 4\n"
] | [
"1\n"
] | In the first example there is only one suitable value *x*β=β5. Then the Beaver takes 1 suitcase with 5 presents to the first planet. Here he spends 2 days: he hangs around on the first day, and he gives away five presents on the second day. He takes 2 suitcases with 10 presents to the second planet. Here he spends 3 days β he gives away 4 presents on the second and the third days and leaves the remaining 2 presents at the hotel. In total, the Beaver spends 5 days traveling.
For *x*β=β4 or less the Beaver won't have enough presents for the second day on the first planet, so the voyage will end too soon. For *x*β=β6 and more the Beaver will spend at least one more day on the second planet, and the voyage will take too long. | [
{
"input": "2 5\n1 5\n2 4",
"output": "1"
},
{
"input": "1 97\n1 91",
"output": "91"
},
{
"input": "2 79\n1 91\n1 77",
"output": "42"
},
{
"input": "3 100\n8 46\n8 56\n77 98",
"output": "1"
},
{
"input": "71 100\n1 92\n1 94\n1 97\n1 95\n1 100\n1 100\n1 98\n1 99\n1... | 186 | 307,200 | 0 | 24,399 | |
255 | Code Parsing | [
"implementation"
] | null | null | Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string *s*, consisting of characters "x" and "y", and uses two following operations at runtime:
1. Find two consecutive characters in the string, such that the first of them equals "y", and the second one equals "x" and swap them. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string. 1. Find in the string two consecutive characters, such that the first of them equals "x" and the second one equals "y". Remove these characters from the string. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string.
The input for the new algorithm is string *s*, and the algorithm works as follows:
1. If you can apply at least one of the described operations to the string, go to step 2 of the algorithm. Otherwise, stop executing the algorithm and print the current string. 1. If you can apply operation 1, then apply it. Otherwise, apply operation 2. After you apply the operation, go to step 1 of the algorithm.
Now Vitaly wonders, what is going to be printed as the result of the algorithm's work, if the input receives string *s*. | The first line contains a non-empty string *s*.
It is guaranteed that the string only consists of characters "x" and "y". It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string. | In the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string *s*. | [
"x\n",
"yxyxy\n",
"xxxxxy\n"
] | [
"x\n",
"y\n",
"xxxx\n"
] | In the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change.
In the second test the transformation will be like this:
1. string "yxyxy" transforms into string "xyyxy"; 1. string "xyyxy" transforms into string "xyxyy"; 1. string "xyxyy" transforms into string "xxyyy"; 1. string "xxyyy" transforms into string "xyy"; 1. string "xyy" transforms into string "y".
As a result, we've got string "y".
In the third test case only one transformation will take place: string "xxxxxy" transforms into string "xxxx". Thus, the answer will be string "xxxx". | [
{
"input": "x",
"output": "x"
},
{
"input": "yxyxy",
"output": "y"
},
{
"input": "xxxxxy",
"output": "xxxx"
},
{
"input": "yxyyxyyx",
"output": "yy"
},
{
"input": "yxxyxyx",
"output": "x"
},
{
"input": "xxx",
"output": "xxx"
},
{
"input": "... | 2,000 | 11,878,400 | 0 | 24,414 | |
0 | none | [
"none"
] | null | null | It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has *n* cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to *n* from the left to the right.
Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.
Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an *n*<=-<=1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.
Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than *m* times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.
In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only *k* minutes left before morning, and on each of these *k* minutes he can perform no more than *m* operations. All that remains in Joe's pocket, is considered his loot.
Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning. | The first line contains integers *n*, *m* and *k* (1<=β€<=*n*<=β€<=104, 1<=β€<=*m*,<=*k*<=β€<=109). The next line contains *n* numbers. The *i*-th number is equal to the amount of diamonds in the *i*-th cell β it is an integer from 0 to 105. | Print a single number β the maximum number of diamonds Joe can steal. | [
"2 3 1\n2 3\n",
"3 2 2\n4 1 3\n"
] | [
"0",
"2"
] | In the second sample Joe can act like this:
The diamonds' initial positions are 4 1 3.
During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.
By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.
During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.
By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.
Now Joe leaves with 2 diamonds in his pocket. | [
{
"input": "2 3 1\n2 3",
"output": "0"
},
{
"input": "3 2 2\n4 1 3",
"output": "2"
},
{
"input": "5 10 10\n7 0 7 0 7",
"output": "7"
},
{
"input": "6 10 4\n1 2 3 4 5 6",
"output": "0"
},
{
"input": "7 5 2\n1 2 3 4 5 6 7",
"output": "1"
},
{
"input": "1... | 46 | 0 | 0 | 24,700 | |
0 | none | [
"none"
] | null | null | On one of the planets of Solar system, in Atmosphere University, many students are fans of bingo game.
It is well known that one month on this planet consists of $n^2$ days, so calendars, represented as square matrix $n$ by $n$ are extremely popular.
Weather conditions are even more unusual. Due to the unique composition of the atmosphere, when interacting with sunlight, every day sky takes one of three colors: blue, green or red.
To play the bingo, you need to observe the sky for one monthΒ β after each day, its cell is painted with the color of the sky in that day, that is, blue, green or red.
At the end of the month, students examine the calendar. If at least one row or column contains only cells of one color, that month is called lucky.
Let's call two colorings of calendar different, if at least one cell has different colors in them. It is easy to see that there are $3^{n \cdot n}$ different colorings. How much of them are lucky? Since this number can be quite large, print it modulo $998244353$. | The first and only line of input contains a single integer $n$ ($1 \le n \le 1000\,000$)Β β the number of rows and columns in the calendar. | Print one numberΒ β number of lucky colorings of the calendar modulo $998244353$ | [
"1\n",
"2\n",
"3\n"
] | [
"3\n",
"63\n",
"9933\n"
] | In the first sample any coloring is lucky, since the only column contains cells of only one color.
In the second sample, there are a lot of lucky colorings, in particular, the following colorings are lucky:
While these colorings are not lucky: | [] | 46 | 0 | 0 | 24,726 | |
321 | Ciel and Duel | [
"dp",
"flows",
"greedy"
] | null | null | Fox Ciel is playing a card game with her friend Jiro.
Jiro has *n* cards, each one has two attributes: *position* (Attack or Defense) and *strength*. Fox Ciel has *m* cards, each one has these two attributes too. It's known that position of all Ciel's cards is Attack.
Now is Ciel's battle phase, Ciel can do the following operation many times:
1. Choose one of her cards *X*. This card mustn't be chosen before. 1. If Jiro has no alive cards at that moment, he gets the damage equal to (*X*'s strength). Otherwise, Ciel needs to choose one Jiro's alive card *Y*, then: If *Y*'s position is Attack, then (*X*'s strength) <=β₯<= (*Y*'s strength) must hold. After this attack, card *Y* dies, and Jiro gets the damage equal to (*X*'s strength) - (*Y*'s strength). 1. If *Y*'s position is Defense, then (*X*'s strength) <=><= (*Y*'s strength) must hold. After this attack, card *Y* dies, but Jiro gets no damage.
Ciel can end her battle phase at any moment (so, she can use not all her cards). Help the Fox to calculate the maximal sum of damage Jiro can get. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=100) β the number of cards Jiro and Ciel have.
Each of the next *n* lines contains a string *position* and an integer *strength* (0<=β€<=*strength*<=β€<=8000) β the position and strength of Jiro's current card. Position is the string "ATK" for attack, and the string "DEF" for defense.
Each of the next *m* lines contains an integer *strength* (0<=β€<=*strength*<=β€<=8000) β the strength of Ciel's current card. | Output an integer: the maximal damage Jiro can get. | [
"2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500\n",
"3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001\n",
"2 4\nDEF 0\nATK 0\n0\n0\n1\n1\n"
] | [
"3000\n",
"992\n",
"1\n"
] | In the first test case, Ciel has 3 cards with same *strength*. The best strategy is as follows. First she uses one of these 3 cards to attack "ATK 2000" card first, this attack destroys that card and Jiro gets 2500β-β2000β=β500 damage. Then she uses the second card to destroy the "DEF 1700" card. Jiro doesn't get damage that time. Now Jiro has no cards so she can use the third card to attack and Jiro gets 2500 damage. So the answer is 500β+β2500β=β3000.
In the second test case, she should use the "1001" card to attack the "ATK 100" card, then use the "101" card to attack the "ATK 10" card. Now Ciel still has cards but she can choose to end her battle phase. The total damage equals (1001β-β100)β+β(101β-β10)β=β992.
In the third test case note that she can destroy the "ATK 0" card by a card with strength equal to 0, but she can't destroy a "DEF 0" card with that card. | [
{
"input": "2 3\nATK 2000\nDEF 1700\n2500\n2500\n2500",
"output": "3000"
},
{
"input": "3 4\nATK 10\nATK 100\nATK 1000\n1\n11\n101\n1001",
"output": "992"
},
{
"input": "2 4\nDEF 0\nATK 0\n0\n0\n1\n1",
"output": "1"
},
{
"input": "1 1\nATK 100\n99",
"output": "0"
},
{... | 154 | 512,000 | 0 | 24,744 | |
865 | Ordering Pizza | [
"binary search",
"sortings",
"ternary search"
] | null | null | It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly *S* slices.
It is known that the *i*-th contestant will eat *s**i* slices of pizza, and gain *a**i* happiness for each slice of type 1 pizza they eat, and *b**i* happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved? | The first line of input will contain integers *N* and *S* (1<=β€<=*N*<=β€<=105,<=1<=β€<=*S*<=β€<=105), the number of contestants and the number of slices per pizza, respectively. *N* lines follow.
The *i*-th such line contains integers *s**i*, *a**i*, and *b**i* (1<=β€<=*s**i*<=β€<=105,<=1<=β€<=*a**i*<=β€<=105,<=1<=β€<=*b**i*<=β€<=105), the number of slices the *i*-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively. | Print the maximum total happiness that can be achieved. | [
"3 12\n3 5 7\n4 6 7\n5 9 5\n",
"6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6\n"
] | [
"84\n",
"314\n"
] | In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3Β·5β+β4Β·6β+β5Β·9β=β84, and if you buy a type 2 pizza, the total happiness will be 3Β·7β+β4Β·7β+β5Β·5β=β74. | [
{
"input": "3 12\n3 5 7\n4 6 7\n5 9 5",
"output": "84"
},
{
"input": "6 10\n7 4 7\n5 8 8\n12 5 8\n6 11 6\n3 3 7\n5 9 6",
"output": "314"
},
{
"input": "1 100\n97065 97644 98402",
"output": "9551390130"
},
{
"input": "1 100000\n1 82372 5587",
"output": "82372"
},
{
... | 30 | 0 | -1 | 24,758 | |
0 | none | [
"none"
] | null | null | Limak is a little bear who learns to draw. People usually start with houses, fences and flowers but why would bears do it? Limak lives in the forest and he decides to draw a tree.
Recall that tree is a connected graph consisting of *n* vertices and *n*<=-<=1 edges.
Limak chose a tree with *n* vertices. He has infinite strip of paper with two parallel rows of dots. Little bear wants to assign vertices of a tree to some *n* distinct dots on a paper so that edges would intersect only at their endpoints β drawn tree must be planar. Below you can see one of correct drawings for the first sample test.
Is it possible for Limak to draw chosen tree? | The first line contains single integer *n* (1<=β€<=*n*<=β€<=105).
Next *n*<=-<=1 lines contain description of a tree. *i*-th of them contains two space-separated integers *a**i* and *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*,<=*a**i*<=β <=*b**i*) denoting an edge between vertices *a**i* and *b**i*. It's guaranteed that given description forms a tree. | Print "Yes" (without the quotes) if Limak can draw chosen tree. Otherwise, print "No" (without the quotes). | [
"8\n1 2\n1 3\n1 6\n6 4\n6 7\n6 5\n7 8\n",
"13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13\n"
] | [
"Yes\n",
"No\n"
] | none | [
{
"input": "8\n1 2\n1 3\n1 6\n6 4\n6 7\n6 5\n7 8",
"output": "Yes"
},
{
"input": "13\n1 2\n1 3\n1 4\n2 5\n2 6\n2 7\n3 8\n3 9\n3 10\n4 11\n4 12\n4 13",
"output": "No"
},
{
"input": "1",
"output": "Yes"
},
{
"input": "20\n5 15\n20 4\n11 18\n1 14\n18 2\n14 17\n8 10\n13 1\n11 6\n... | 202 | 22,323,200 | -1 | 24,785 | |
226 | Anniversary | [
"data structures",
"implementation",
"math",
"matrices",
"number theory"
] | null | null | There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathematician Leonardo Fibonacci. Of course, such important anniversary needs much preparations.
Dima is sure that it'll be great to learn to solve the following problem by the Big Day: You're given a set *A*, consisting of numbers *l*, *l*<=+<=1, *l*<=+<=2, ..., *r*; let's consider all its *k*-element subsets; for each such subset let's find the largest common divisor of Fibonacci numbers with indexes, determined by the subset elements. Among all found common divisors, Dima is interested in the largest one.
Dima asked to remind you that Fibonacci numbers are elements of a numeric sequence, where *F*1<==<=1, *F*2<==<=1, *F**n*<==<=*F**n*<=-<=1<=+<=*F**n*<=-<=2 for *n*<=β₯<=3.
Dima has more than half a century ahead to solve the given task, but you only have two hours. Count the residue from dividing the sought largest common divisor by *m*. | The first line contains four space-separated integers *m*, *l*, *r* and *k* (1<=β€<=*m*<=β€<=109;Β 1<=β€<=*l*<=<<=*r*<=β€<=1012;Β 2<=β€<=*k*<=β€<=*r*<=-<=*l*<=+<=1).
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 a single integer β the residue from dividing the sought greatest common divisor by *m*. | [
"10 1 8 2\n",
"10 1 8 3\n"
] | [
"3\n",
"1\n"
] | none | [
{
"input": "10 1 8 2",
"output": "3"
},
{
"input": "10 1 8 3",
"output": "1"
},
{
"input": "10 1 20 2",
"output": "5"
},
{
"input": "4 1 10 2",
"output": "1"
},
{
"input": "1000000000 999000000000 1000000000000 1000000001",
"output": "1"
},
{
"input": ... | 92 | 0 | 0 | 24,869 | |
203 | Transportation | [
"greedy",
"sortings",
"two pointers"
] | null | null | Valera came to Japan and bought many robots for his research. He's already at the airport, the plane will fly very soon and Valera urgently needs to bring all robots to the luggage compartment.
The robots are self-propelled (they can potentially move on their own), some of them even have compartments to carry other robots. More precisely, for the *i*-th robot we know value *c**i* β the number of robots it can carry. In this case, each of *c**i* transported robots can additionally carry other robots.
However, the robots need to be filled with fuel to go, so Valera spent all his last money and bought *S* liters of fuel. He learned that each robot has a restriction on travel distances. Thus, in addition to features *c**i*, the *i*-th robot has two features *f**i* and *l**i* β the amount of fuel (in liters) needed to move the *i*-th robot, and the maximum distance that the robot can go.
Due to the limited amount of time and fuel, Valera wants to move the maximum number of robots to the luggage compartment. He operates as follows.
- First Valera selects some robots that will travel to the luggage compartment on their own. In this case the total amount of fuel required to move all these robots must not exceed *S*. - Then Valera seats the robots into the compartments, so as to transport as many robots as possible. Note that if a robot doesn't move by itself, you can put it in another not moving robot that is moved directly or indirectly by a moving robot. - After that all selected and seated robots along with Valera go to the luggage compartment and the rest robots will be lost.
There are *d* meters to the luggage compartment. Therefore, the robots that will carry the rest, must have feature *l**i* of not less than *d*. During the moving Valera cannot stop or change the location of the robots in any way.
Help Valera calculate the maximum number of robots that he will be able to take home, and the minimum amount of fuel he will have to spend, because the remaining fuel will come in handy in Valera's research. | The first line contains three space-separated integers *n*,<=*d*,<=*S* (1<=β€<=*n*<=β€<=105,<=1<=β€<=*d*,<=*S*<=β€<=109). The first number represents the number of robots, the second one β the distance to the luggage compartment and the third one β the amount of available fuel.
Next *n* lines specify the robots. The *i*-th line contains three space-separated integers *c**i*,<=*f**i*,<=*l**i* (0<=β€<=*c**i*,<=*f**i*,<=*l**i*<=β€<=109) β the *i*-th robot's features. The first number is the number of robots the *i*-th robot can carry, the second number is the amount of fuel needed for the *i*-th robot to move and the third one shows the maximum distance the *i*-th robot can go. | Print two space-separated integers β the maximum number of robots Valera can transport to the luggage compartment and the minimum amount of fuel he will need for that. If Valera won't manage to get any robots to the luggage compartment, print two zeroes. | [
"3 10 10\n0 12 10\n1 6 10\n0 1 1\n",
"2 7 10\n3 12 10\n5 16 8\n",
"4 8 10\n0 12 3\n1 1 0\n0 3 11\n1 6 9\n"
] | [
"2 6\n",
"0 0\n",
"4 9\n"
] | none | [] | 62 | 0 | 0 | 24,870 | |
418 | Tricky Password | [
"data structures"
] | null | null | In order to ensure confidentiality, the access to the "Russian Code Cup" problems is password protected during the problem development process.
To select a password, the jury can generate a special table that contains *n* columns and the infinite number of rows. To construct a table, the first row is fixed, and all the others are obtained by the following rule:
In the row *i* at position *p* there is a number equal to the number of times *a*[*i*<=-<=1][*p*] occurs on the prefix *a*[*i*<=-<=1][1... *p*].
To ensure the required level of confidentiality, the jury must be able to perform the following operations:
- Replace number *a*[1][*p*] by *v* and rebuild the table. - Find the number *a*[*x*][*y*], which will be the new password.
Doing all these steps manually is very tedious, so the jury asks you to help him. Write a program that responds to the request of the jury. | The first line contains an integer *n* (1<=β€<=*n*<=β€<=100000) β the number of columns. The second line contains the description of the first row of the table, that is, *n* integers, which are not less than 1 and do not exceed 109.
The third line of the input contains an integer *m* (1<=β€<=*m*<=β€<=100000) β the number of requests.
Next, each row contains a description of the request, which consists of three integers:
- If the first number is equal to 1, then the remaining two numbers are *v*, *p* (1<=β€<=*v*<=β€<=109; 1<=β€<=*p*<=β€<=*n*). So, you should put value *v* in the position *p* in the first row. - If the first number is equal to 2, then the remaining two numbers are *x*, *y* (1<=β€<=*x*<=β€<=105; 1<=β€<=*y*<=β€<=*n*) β the row and column of the table cell from which you want to get value. | Print an answer for each request of the second type in the order you receive them. | [
"6\n1 2 2 2 3 1\n3\n2 2 3\n1 3 3\n2 3 4\n"
] | [
"2\n1\n"
] | none | [] | 30 | 0 | 0 | 25,010 | |
438 | The Child and Sequence | [
"data structures",
"math"
] | null | null | At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at him. A lot of important things were lost, in particular the favorite sequence of Picks.
Fortunately, Picks remembers how to repair the sequence. Initially he should create an integer array *a*[1],<=*a*[2],<=...,<=*a*[*n*]. Then he should perform a sequence of *m* operations. An operation can be one of the following:
1. Print operation *l*,<=*r*. Picks should write down the value of . 1. Modulo operation *l*,<=*r*,<=*x*. Picks should perform assignment *a*[*i*]<==<=*a*[*i*]Β *mod*Β *x* for each *i* (*l*<=β€<=*i*<=β€<=*r*). 1. Set operation *k*,<=*x*. Picks should set the value of *a*[*k*] to *x* (in other words perform an assignment *a*[*k*]<==<=*x*).
Can you help Picks to perform the whole sequence of operations? | The first line of input contains two integer: *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=105). The second line contains *n* integers, separated by space: *a*[1],<=*a*[2],<=...,<=*a*[*n*]Β (1<=β€<=*a*[*i*]<=β€<=109) β initial value of array elements.
Each of the next *m* lines begins with a number *type* .
- If *type*<==<=1, there will be two integers more in the line: *l*,<=*r*Β (1<=β€<=*l*<=β€<=*r*<=β€<=*n*), which correspond the operation 1. - If *type*<==<=2, there will be three integers more in the line: *l*,<=*r*,<=*x*Β (1<=β€<=*l*<=β€<=*r*<=β€<=*n*;Β 1<=β€<=*x*<=β€<=109), which correspond the operation 2. - If *type*<==<=3, there will be two integers more in the line: *k*,<=*x*Β (1<=β€<=*k*<=β€<=*n*;Β 1<=β€<=*x*<=β€<=109), which correspond the operation 3. | For each operation 1, please print a line containing the answer. Notice that the answer may exceed the 32-bit integer. | [
"5 5\n1 2 3 4 5\n2 3 5 4\n3 3 5\n1 2 5\n2 1 3 3\n1 1 3\n",
"10 10\n6 9 6 7 6 1 10 10 9 5\n1 3 9\n2 7 10 9\n2 5 10 8\n1 4 7\n3 3 7\n2 7 9 9\n1 2 4\n1 6 6\n1 5 9\n3 1 10\n"
] | [
"8\n5\n",
"49\n15\n23\n1\n9\n"
] | Consider the first testcase:
- At first, *a*β=β{1,β2,β3,β4,β5}. - After operation 1, *a*β=β{1,β2,β3,β0,β1}. - After operation 2, *a*β=β{1,β2,β5,β0,β1}. - At operation 3, 2β+β5β+β0β+β1β=β8. - After operation 4, *a*β=β{1,β2,β2,β0,β1}. <li> At operation 5, 1β+β2β+β2β=β5. <ul> | [
{
"input": "5 5\n1 2 3 4 5\n2 3 5 4\n3 3 5\n1 2 5\n2 1 3 3\n1 1 3",
"output": "8\n5"
},
{
"input": "10 10\n6 9 6 7 6 1 10 10 9 5\n1 3 9\n2 7 10 9\n2 5 10 8\n1 4 7\n3 3 7\n2 7 9 9\n1 2 4\n1 6 6\n1 5 9\n3 1 10",
"output": "49\n15\n23\n1\n9"
},
{
"input": "1 1\n1\n1 1 1",
"output": "1"
... | 1,107 | 0 | 0 | 25,067 | |
727 | Games on a CD | [
"data structures",
"hashing",
"string suffix structures",
"strings"
] | null | null | Several years ago Tolya had *n* computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to *k*. The names didn't overlap.
Thus, there is a cyclic string of length *n*Β·*k* written on the CD.
Several years have passed and now Tolya can't remember which games he burned to his CD. He knows that there were *g* popular games that days. All of the games he burned were among these *g* games, and no game was burned more than once.
You have to restore any valid list of games Tolya could burn to the CD several years ago. | The first line of the input contains two positive integers *n* and *k* (1<=β€<=*n*<=β€<=105, 1<=β€<=*k*<=β€<=105)Β β the amount of games Tolya burned to the CD, and the length of each of the names.
The second line of the input contains one string consisting of lowercase English lettersΒ β the string Tolya wrote on the CD, split in arbitrary place. The length of the string is *n*Β·*k*. It is guaranteed that the length is not greater than 106.
The third line of the input contains one positive integer *g* (*n*<=β€<=*g*<=β€<=105)Β β the amount of popular games that could be written on the CD. It is guaranteed that the total length of names of all popular games is not greater than 2Β·106.
Each of the next *g* lines contains a single stringΒ β the name of some popular game. Each name consists of lowercase English letters and has length *k*. It is guaranteed that the names are distinct. | If there is no answer, print "NO" (without quotes).
Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print *n* integersΒ β the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order. You can print games starting from any position. Remember, that no game was burned to the CD more than once. If there are several possible answers, print any of them. | [
"3 1\nabc\n4\nb\na\nc\nd\n",
"4 2\naabbccdd\n4\ndd\nab\nbc\ncd\n"
] | [
"YES\n2 1 3 \n",
"NO\n"
] | none | [
{
"input": "3 1\nabc\n4\nb\na\nc\nd",
"output": "YES\n2 1 3 "
},
{
"input": "4 2\naabbccdd\n4\ndd\nab\nbc\ncd",
"output": "NO"
},
{
"input": "2 2\nkdcj\n4\ncj\nkd\njk\ndc",
"output": "YES\n3 4 "
},
{
"input": "10 10\nlgfrjgityzwtmfyygmpsmokiwphewhpoelsvnctwxmpimqvblgrisozncsi... | 811 | 18,022,400 | -1 | 25,074 | |
372 | Counting Rectangles is Fun | [
"brute force",
"divide and conquer",
"dp"
] | null | null | There is an *n*<=Γ<=*m* rectangular grid, each cell of the grid contains a single integer: zero or one. Let's call the cell on the *i*-th row and the *j*-th column as (*i*,<=*j*).
Let's define a "rectangle" as four integers *a*,<=*b*,<=*c*,<=*d* (1<=β€<=*a*<=β€<=*c*<=β€<=*n*;Β 1<=β€<=*b*<=β€<=*d*<=β€<=*m*). Rectangle denotes a set of cells of the grid {(*x*,<=*y*)Β :<=Β *a*<=β€<=*x*<=β€<=*c*,<=*b*<=β€<=*y*<=β€<=*d*}. Let's define a "good rectangle" as a rectangle that includes only the cells with zeros.
You should answer the following *q* queries: calculate the number of good rectangles all of which cells are in the given rectangle. | There are three integers in the first line: *n*, *m* and *q* (1<=β€<=*n*,<=*m*<=β€<=40,<=1<=β€<=*q*<=β€<=3Β·105). Each of the next *n* lines contains *m* characters β the grid. Consider grid rows are numbered from top to bottom, and grid columns are numbered from left to right. Both columns and rows are numbered starting from 1.
Each of the next *q* lines contains a query β four integers that describe the current rectangle, *a*, *b*, *c*, *d* (1<=β€<=*a*<=β€<=*c*<=β€<=*n*;Β 1<=β€<=*b*<=β€<=*d*<=β€<=*m*). | For each query output an answer β a single integer in a separate line. | [
"5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3\n",
"4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7\n"
] | [
"10\n1\n7\n34\n5\n",
"3\n1\n16\n27\n52\n"
] | For the first example, there is a 5βΓβ5 rectangular grid, and the first, the second, and the third queries are represented in the following image.
- For the first query, there are 10 good rectangles, five 1βΓβ1, two 2βΓβ1, two 1βΓβ2, and one 1βΓβ3. - For the second query, there is only one 1βΓβ1 good rectangle. - For the third query, there are 7 good rectangles, four 1βΓβ1, two 2βΓβ1, and one 3βΓβ1. | [
{
"input": "5 5 5\n00101\n00000\n00001\n01000\n00001\n1 2 2 4\n4 5 4 5\n1 2 5 2\n2 2 4 5\n4 2 5 3",
"output": "10\n1\n7\n34\n5"
},
{
"input": "4 7 5\n0000100\n0000010\n0011000\n0000000\n1 7 2 7\n3 1 3 1\n2 3 4 5\n1 2 2 7\n2 2 4 7",
"output": "3\n1\n16\n27\n52"
},
{
"input": "10 10 10\n00... | 764 | 53,043,200 | 3 | 25,092 | |
229 | Towers | [
"dp",
"greedy",
"two pointers"
] | null | null | The city of D consists of *n* towers, built consecutively on a straight line. The height of the tower that goes *i*-th (from left to right) in the sequence equals *h**i*. The city mayor decided to rebuild the city to make it beautiful. In a beautiful city all towers are are arranged in non-descending order of their height from left to right.
The rebuilding consists of performing several (perhaps zero) operations. An operation constitutes using a crane to take any tower and put it altogether on the top of some other neighboring tower. In other words, we can take the tower that stands *i*-th and put it on the top of either the (*i*<=-<=1)-th tower (if it exists), or the (*i*<=+<=1)-th tower (of it exists). The height of the resulting tower equals the sum of heights of the two towers that were put together. After that the two towers can't be split by any means, but more similar operations can be performed on the resulting tower. Note that after each operation the total number of towers on the straight line decreases by 1.
Help the mayor determine the minimum number of operations required to make the city beautiful. | The first line contains a single integer *n* (1<=β€<=*n*<=β€<=5000)Β β the number of towers in the city. The next line contains *n* space-separated integers: the *i*-th number *h**i* (1<=β€<=*h**i*<=β€<=105) determines the height of the tower that is *i*-th (from left to right) in the initial tower sequence. | Print a single integer β the minimum number of operations needed to make the city beautiful. | [
"5\n8 2 7 3 1\n",
"3\n5 2 1\n"
] | [
"3\n",
"2\n"
] | none | [
{
"input": "5\n8 2 7 3 1",
"output": "3"
},
{
"input": "3\n5 2 1",
"output": "2"
},
{
"input": "1\n1",
"output": "0"
},
{
"input": "2\n1 2",
"output": "0"
},
{
"input": "2\n2 1",
"output": "1"
},
{
"input": "3\n1 3 2",
"output": "1"
},
{
"i... | 46 | 0 | 0 | 25,125 | |
161 | Abracadabra | [
"divide and conquer"
] | null | null | Polycarpus analyzes a string called abracadabra. This string is constructed using the following algorithm:
- On the first step the string consists of a single character "a". - On the *k*-th step Polycarpus concatenates two copies of the string obtained on the (*k*<=-<=1)-th step, while inserting the *k*-th character of the alphabet between them. Polycarpus uses the alphabet that consists of lowercase Latin letters and digits (a total of 36 characters). The alphabet characters are numbered like this: the 1-st character is "a", the 2-nd β "b", ..., the 26-th β "z", the 27-th β "0", the 28-th β "1", ..., the 36-th β "9".
Let's have a closer look at the algorithm. On the second step Polycarpus will concatenate two strings "a" and insert the character "b" between them, resulting in "aba" string. The third step will transform it into "abacaba", and the fourth one - into "abacabadabacaba". Thus, the string constructed on the *k*-th step will consist of 2*k*<=-<=1 characters.
Polycarpus wrote down the string he got after 30 steps of the given algorithm and chose two non-empty substrings of it. Your task is to find the length of the longest common substring of the two substrings selected by Polycarpus.
A substring *s*[*i*... *j*] (1<=β€<=*i*<=β€<=*j*<=β€<=|*s*|) of string *s* = *s*1*s*2... *s*|*s*| is a string *s**i**s**i*<=+<=1... *s**j*. For example, substring *s*[2...4] of string *s* = "abacaba" equals "bac". The string is its own substring.
The longest common substring of two strings *s* and *t* is the longest string that is a substring of both *s* and *t*. For example, the longest common substring of "contest" and "systemtesting" is string "test". There can be several common substrings of maximum length. | The input consists of a single line containing four integers *l*1, *r*1, *l*2, *r*2 (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=109, *i*<==<=1,<=2). The numbers are separated by single spaces. *l**i* and *r**i* give the indices of the first and the last characters of the *i*-th chosen substring, correspondingly (*i*<==<=1,<=2). The characters of string abracadabra are numbered starting from 1. | Print a single number β the length of the longest common substring of the given strings. If there are no common substrings, print 0. | [
"3 6 1 4\n",
"1 1 4 4\n"
] | [
"2\n",
"0\n"
] | In the first sample the first substring is "acab", the second one is "abac". These two substrings have two longest common substrings "ac" and "ab", but we are only interested in their length β 2.
In the second sample the first substring is "a", the second one is "c". These two substrings don't have any common characters, so the length of their longest common substring is 0. | [
{
"input": "3 6 1 4",
"output": "2"
},
{
"input": "1 1 4 4",
"output": "0"
},
{
"input": "2 2 6 6",
"output": "1"
},
{
"input": "73426655 594361930 343984155 989446962",
"output": "379149396"
},
{
"input": "169720415 312105195 670978284 671296539",
"output": "... | 46 | 0 | 3 | 25,176 | |
847 | Dog Show | [
"constructive algorithms",
"data structures",
"greedy"
] | null | null | A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win!
On the show a dog needs to eat as many bowls of dog food as possible (bottomless stomach helps here). Dogs compete separately of each other and the rules are as follows:
At the start of the show the dog and the bowls are located on a line. The dog starts at position *x*<==<=0 and *n* bowls are located at positions *x*<==<=1,<=*x*<==<=2,<=...,<=*x*<==<=*n*. The bowls are numbered from 1 to *n* from left to right. After the show starts the dog immediately begins to run to the right to the first bowl.
The food inside bowls is not ready for eating at the start because it is too hot (dog's self-preservation instinct prevents eating). More formally, the dog can eat from the *i*-th bowl after *t**i* seconds from the start of the show or later.
It takes dog 1 second to move from the position *x* to the position *x*<=+<=1. The dog is not allowed to move to the left, the dog runs only to the right with the constant speed 1 distance unit per second. When the dog reaches a bowl (say, the bowl *i*), the following cases are possible:
- the food had cooled down (i.e. it passed at least *t**i* seconds from the show start): the dog immediately eats the food and runs to the right without any stop, - the food is hot (i.e. it passed less than *t**i* seconds from the show start): the dog has two options: to wait for the *i*-th bowl, eat the food and continue to run at the moment *t**i* or to skip the *i*-th bowl and continue to run to the right without any stop.
After *T* seconds from the start the show ends. If the dog reaches a bowl of food at moment *T* the dog can not eat it. The show stops before *T* seconds if the dog had run to the right of the last bowl.
You need to help your dog create a strategy with which the maximum possible number of bowls of food will be eaten in *T* seconds. | Two integer numbers are given in the first line - *n* and *T* (1<=β€<=*n*<=β€<=200<=000, 1<=β€<=*T*<=β€<=2Β·109) β the number of bowls of food and the time when the dog is stopped.
On the next line numbers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=109) are given, where *t**i* is the moment of time when the *i*-th bowl of food is ready for eating. | Output a single integer β the maximum number of bowls of food the dog will be able to eat in *T* seconds. | [
"3 5\n1 5 3\n",
"1 2\n1\n",
"1 1\n1\n"
] | [
"2\n",
"1\n",
"0\n"
] | In the first example the dog should skip the second bowl to eat from the two bowls (the first and the third). | [
{
"input": "3 5\n1 5 3",
"output": "2"
},
{
"input": "1 2\n1",
"output": "1"
},
{
"input": "1 1\n1",
"output": "0"
},
{
"input": "1 1\n2",
"output": "0"
},
{
"input": "2 2\n2 3",
"output": "0"
},
{
"input": "2 3\n2 1",
"output": "1"
},
{
"i... | 358 | 20,684,800 | 3 | 25,189 | |
279 | Ladder | [
"dp",
"implementation",
"two pointers"
] | null | null | You've got an array, consisting of *n* integers *a*1,<=*a*2,<=...,<=*a**n*. Also, you've got *m* queries, the *i*-th query is described by two integers *l**i*,<=*r**i*. Numbers *l**i*,<=*r**i* define a subsegment of the original array, that is, the sequence of numbers *a**l**i*,<=*a**l**i*<=+<=1,<=*a**l**i*<=+<=2,<=...,<=*a**r**i*. For each query you should check whether the corresponding segment is a ladder.
A ladder is a sequence of integers *b*1,<=*b*2,<=...,<=*b**k*, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer *x* (1<=β€<=*x*<=β€<=*k*), that the following inequation fulfills: *b*1<=β€<=*b*2<=β€<=...<=β€<=*b**x*<=β₯<=*b**x*<=+<=1<=β₯<=*b**x*<=+<=2...<=β₯<=*b**k*. Note that the non-decreasing and the non-increasing sequences are also considered ladders. | The first line contains two integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=105) β the number of array elements and the number of queries. The second line contains the sequence of integers *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109), where number *a**i* stands for the *i*-th array element.
The following *m* lines contain the description of the queries. The *i*-th line contains the description of the *i*-th query, consisting of two integers *l**i*, *r**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*) β the boundaries of the subsegment of the initial array.
The numbers in the lines are separated by single spaces. | Print *m* lines, in the *i*-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the *i*-th query is the ladder, or word "No" (without the quotes) otherwise. | [
"8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8\n"
] | [
"Yes\nYes\nNo\nYes\nNo\nYes\n"
] | none | [
{
"input": "8 6\n1 2 1 3 3 5 2 1\n1 3\n2 3\n2 4\n8 8\n1 4\n5 8",
"output": "Yes\nYes\nNo\nYes\nNo\nYes"
},
{
"input": "1 1\n6\n1 1",
"output": "Yes"
},
{
"input": "2 5\n1 1\n1 2\n2 2\n2 2\n1 2\n1 2",
"output": "Yes\nYes\nYes\nYes\nYes"
},
{
"input": "10 10\n4 7 2 6 4 8 1 2 1 ... | 92 | 0 | 0 | 25,225 | |
504 | Misha and XOR | [
"bitmasks"
] | null | null | After Misha's birthday he had many large numbers left, scattered across the room. Now it's time to clean up and Misha needs to put them in a basket. He ordered this task to his pet robot that agreed to complete the task at certain conditions. Before the robot puts a number *x* to the basket, Misha should answer the question: is it possible to choose one or multiple numbers that already are in the basket, such that their XOR sum equals *x*?
If the answer is positive, you also need to give the indexes of these numbers. If there are multiple options of choosing numbers, you are allowed to choose any correct option. After Misha's answer the robot puts the number to the basket.
Initially the basket is empty. Each integer you put in the basket takes some number. The first integer you put into the basket take number 0, the second integer takes number 1 and so on.
Misha needs to clean up the place as soon as possible but unfortunately, he isn't that good at mathematics. He asks you to help him. | The first line contains number *m* (1<=β€<=*m*<=β€<=2000), showing how many numbers are scattered around the room.
The next *m* lines contain the numbers in the order in which the robot puts them in the basket. Each number is a positive integer strictly less than 10600 that doesn't contain leading zeroes. | For each number either print a 0 on the corresponding line, if the number cannot be represented as a XOR sum of numbers that are in the basket, or print integer *k* showing how many numbers are in the representation and the indexes of these numbers. Separate the numbers by spaces. Each number can occur in the representation at most once. | [
"7\n7\n6\n5\n4\n3\n2\n1\n",
"2\n5\n5\n"
] | [
"0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1\n",
"0\n1 0\n"
] | The XOR sum of numbers is the result of bitwise sum of numbers modulo 2. | [
{
"input": "7\n7\n6\n5\n4\n3\n2\n1",
"output": "0\n0\n0\n3 0 1 2\n2 1 2\n2 0 2\n2 0 1"
},
{
"input": "2\n5\n5",
"output": "0\n1 0"
},
{
"input": "10\n81\n97\n12\n2\n16\n96\n80\n99\n6\n83",
"output": "0\n0\n0\n0\n0\n0\n3 0 1 5\n2 1 3\n0\n2 0 3"
},
{
"input": "10\n15106\n13599\... | 108 | 1,843,200 | 0 | 25,250 | |
630 | Benches | [
"combinatorics",
"math"
] | null | null | The city park of IT City contains *n* east to west paths and *n* north to south paths. Each east to west path crosses each north to south path, so there are *n*2 intersections.
The city funded purchase of five benches. To make it seems that there are many benches it was decided to place them on as many paths as possible. Obviously this requirement is satisfied by the following scheme: each bench is placed on a cross of paths and each path contains not more than one bench.
Help the park administration count the number of ways to place the benches. | The only line of the input contains one integer *n* (5<=β€<=*n*<=β€<=100) β the number of east to west paths and north to south paths. | Output one integer β the number of ways to place the benches. | [
"5\n"
] | [
"120"
] | none | [
{
"input": "5",
"output": "120"
},
{
"input": "6",
"output": "4320"
},
{
"input": "7",
"output": "52920"
},
{
"input": "15",
"output": "1082161080"
},
{
"input": "17",
"output": "4594961280"
},
{
"input": "72",
"output": "23491596420472320"
},
... | 0 | 0 | -1 | 25,340 | |
632 | Alice, Bob, Two Teams | [
"brute force",
"constructive algorithms"
] | null | null | Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are *n* pieces, and the *i*-th piece has a strength *p**i*.
The way to split up game pieces is split into several steps:
1. First, Alice will split the pieces into two different groups *A* and *B*. This can be seen as writing the assignment of teams of a piece in an *n* character string, where each character is *A* or *B*. 1. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change *A* to *B* and *B* to *A*). He can do this step at most once. 1. Alice will get all the pieces marked *A* and Bob will get all the pieces marked *B*.
The strength of a player is then the sum of strengths of the pieces in the group.
Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve. | The first line contains integer *n* (1<=β€<=*n*<=β€<=5Β·105) β the number of game pieces.
The second line contains *n* integers *p**i* (1<=β€<=*p**i*<=β€<=109) β the strength of the *i*-th piece.
The third line contains *n* characters *A* or *B* β the assignment of teams after the first step (after Alice's step). | Print the only integer *a* β the maximum strength Bob can achieve. | [
"5\n1 2 3 4 5\nABABA\n",
"5\n1 2 3 4 5\nAAAAA\n",
"1\n1\nB\n"
] | [
"11\n",
"15\n",
"1\n"
] | In the first sample Bob should flip the suffix of length one.
In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.
In the third sample Bob should do nothing. | [
{
"input": "5\n1 2 3 4 5\nABABA",
"output": "11"
},
{
"input": "5\n1 2 3 4 5\nAAAAA",
"output": "15"
},
{
"input": "1\n1\nB",
"output": "1"
},
{
"input": "10\n1 9 7 6 2 4 7 8 1 3\nABBABAABBB",
"output": "33"
},
{
"input": "100\n591 417 888 251 792 847 685 3 182 46... | 655 | 38,297,600 | 3 | 25,342 | |
241 | Challenging Balloons | [
"constructive algorithms"
] | null | null | Martha β as a professional problemsetter β proposed a problem for a world-class contest. This is the problem statement:
Tomorrow is Nadia's birthday, and Bardia (her brother) is assigned to make the balloons ready!
There are *n* balloons (initially empty) that are tied to a straight line on certain positions *x*1,<=*x*2,<=...,<=*x**n*. Bardia inflates the balloons from left to right. As a result, *i*-th balloon gets bigger and bigger until its radius reaches the pressure endurance *p**i* or it touches another previously-inflated balloon.
While Bardia was busy with the balloons, he wondered "What will be the sum of radius of balloons after all of the balloons are inflated?". Being a nerdy type of guy, he is now thinking about the problem instead of preparing his sister's birthday. Calculate the answer to Bardia's problem so that Nadia's birthday won't be balloon-less.
Artha β Martha's student β claimed his solution got accepted. Martha (being his teacher for a long time!) knew he couldn't have solved the problem for real and thus thinks there is something wrong with the testcases. Artha isn't anyhow logical, which means there is no way for Martha to explain the wrong point in his algorithm. So, the only way is to find a testcase to prove him wrong!
Artha's pseudo-code is shown below:
You should output a small testcase for the problem such that Artha's algorithm is incorrect. The algorithm's output is considered correct if it differs from the correct value by no more than 1. | Please pay attention! No input will be given to your program for this problem. So you do not have to read from the input anything. | You should output the generated small testcase (which Artha's solution doesn't get it right). It should be in the following format:
- First line must contain the only number *n* (1<=β€<=*n*<=β€<=500). - The *i*-th of the next *n* lines should contain the description of the *i*-th balloon β two space-separated integers *x**i*,<=*p**i* (1<=β€<=*p**i*<=β€<=106, 0<=β€<=*x*1<=<<=*x*2<=<<=...<=<<=*x**n*<=β€<=106). | [] | [] | The testcase depicted in the figure above (just showing how output should be formatted): | [] | 31 | 0 | 0 | 25,397 | |
660 | Seating On Bus | [
"implementation"
] | null | null | Consider 2*n* rows of the seats in a bus. *n* rows of the seats on the left and *n* rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4*n*.
Consider that *m* (*m*<=β€<=4*n*) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to *m* (in the order of their entering the bus). The pattern of the seat occupation is as below:
1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , *n*-th row left window seat, *n*-th row right window seat.
After occupying all the window seats (for *m*<=><=2*n*) the non-window seats are occupied:
1-st row left non-window seat, 1-st row right non-window seat, ... , *n*-th row left non-window seat, *n*-th row right non-window seat.
All the passengers go to a single final destination. In the final destination, the passengers get off in the given order.
1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , *n*-th row left non-window seat, *n*-th row left window seat, *n*-th row right non-window seat, *n*-th row right window seat.
You are given the values *n* and *m*. Output *m* numbers from 1 to *m*, the order in which the passengers will get off the bus. | The only line contains two integers, *n* and *m* (1<=β€<=*n*<=β€<=100,<=1<=β€<=*m*<=β€<=4*n*) β the number of pairs of rows and the number of passengers. | Print *m* distinct integers from 1 to *m* β the order in which the passengers will get off the bus. | [
"2 7\n",
"9 36\n"
] | [
"5 1 6 2 7 3 4\n",
"19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18\n"
] | none | [
{
"input": "2 7",
"output": "5 1 6 2 7 3 4"
},
{
"input": "9 36",
"output": "19 1 20 2 21 3 22 4 23 5 24 6 25 7 26 8 27 9 28 10 29 11 30 12 31 13 32 14 33 15 34 16 35 17 36 18"
},
{
"input": "1 1",
"output": "1"
},
{
"input": "1 4",
"output": "3 1 4 2"
},
{
"input... | 46 | 0 | 3 | 25,402 | |
571 | Minimization | [
"dp",
"greedy",
"sortings"
] | null | null | You've got array *A*, consisting of *n* integers and a positive integer *k*. Array *A* is indexed by integers from 1 to *n*.
You need to permute the array elements so that value | The first line contains two integers *n*,<=*k* (2<=β€<=*n*<=β€<=3Β·105, 1<=β€<=*k*<=β€<=*min*(5000,<=*n*<=-<=1)).
The second line contains *n* integers *A*[1],<=*A*[2],<=...,<=*A*[*n*] (<=-<=109<=β€<=*A*[*i*]<=β€<=109), separate by spaces β elements of the array *A*. | Print the minimum possible value of the sum described in the statement. | [
"3 2\n1 2 4\n",
"5 2\n3 -5 3 -5 3\n",
"6 3\n4 3 4 3 2 5\n"
] | [
"1\n",
"0\n",
"3\n"
] | In the first test one of the optimal permutations is 1Β 4Β 2.
In the second test the initial order is optimal.
In the third test one of the optimal permutations is 2Β 3Β 4Β 4Β 3Β 5. | [
{
"input": "3 2\n1 2 4",
"output": "1"
},
{
"input": "5 2\n3 -5 3 -5 3",
"output": "0"
},
{
"input": "6 3\n4 3 4 3 2 5",
"output": "3"
},
{
"input": "2 1\n1 100",
"output": "99"
},
{
"input": "4 3\n1 2 4 8",
"output": "1"
},
{
"input": "5 2\n1 2 8 8 16... | 264 | 15,257,600 | 0 | 25,419 | |
952 | Ravioli Sort | [
"implementation"
] | null | null | Everybody knows of [spaghetti sort](https://en.wikipedia.org/wiki/Spaghetti_sort). You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you...
You come up with the following algorithm. For each number in the array *a**i*, build a stack of *a**i* ravioli. The image shows the stack for *a**i*<==<=4.
Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed.
At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack.
Given an input array, figure out whether the described algorithm will sort it correctly. | The first line of input contains a single number *n* (1<=β€<=*n*<=β€<=10) β the size of the array.
The second line of input contains *n* space-separated integers *a**i* (1<=β€<=*a**i*<=β€<=100) β the elements of the array. | Output "YES" if the array can be sorted using the described procedure and "NO" if it can not. | [
"3\n1 2 3\n",
"3\n3 1 2\n"
] | [
"YES\n",
"NO\n"
] | In the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2,β2,β2}. | [
{
"input": "3\n1 2 3",
"output": "YES"
},
{
"input": "3\n3 1 2",
"output": "NO"
},
{
"input": "1\n13",
"output": "YES"
},
{
"input": "10\n67 67 67 67 67 67 67 67 67 67",
"output": "YES"
},
{
"input": "10\n16 17 16 15 14 15 16 17 16 15",
"output": "YES"
},
... | 0 | 0 | -1 | 25,496 | |
915 | Almost Acyclic Graph | [
"dfs and similar",
"graphs"
] | null | null | You are given a [directed graph](https://en.wikipedia.org/wiki/Directed_graph) consisting of *n* vertices and *m* edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.
Can you make this graph [acyclic](https://en.wikipedia.org/wiki/Directed_acyclic_graph) by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex). | The first line contains two integers *n* and *m* (2<=β€<=*n*<=β€<=500, 1<=β€<=*m*<=β€<=*min*(*n*(*n*<=-<=1),<=100000)) β the number of vertices and the number of edges, respectively.
Then *m* lines follow. Each line contains two integers *u* and *v* denoting a directed edge going from vertex *u* to vertex *v* (1<=β€<=*u*,<=*v*<=β€<=*n*, *u*<=β <=*v*). Each ordered pair (*u*,<=*v*) is listed at most once (there is at most one directed edge from *u* to *v*). | If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO. | [
"3 4\n1 2\n2 3\n3 2\n3 1\n",
"5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5\n"
] | [
"YES\n",
"NO\n"
] | In the first example you can remove edge <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, and the graph becomes acyclic.
In the second example you have to remove at least two edges (for example, <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/420322fe5fba4eb3e3eba6886a2edb31f15762ce.png" style="max-width: 100.0%;max-height: 100.0%;"/> and <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/29f71c065c3536e88b54429c734103ad3604f68b.png" style="max-width: 100.0%;max-height: 100.0%;"/>) in order to make the graph acyclic. | [
{
"input": "3 4\n1 2\n2 3\n3 2\n3 1",
"output": "YES"
},
{
"input": "5 6\n1 2\n2 3\n3 2\n3 1\n2 1\n4 5",
"output": "NO"
},
{
"input": "2 2\n1 2\n2 1",
"output": "YES"
},
{
"input": "7 7\n1 3\n3 6\n3 7\n5 3\n6 2\n6 7\n7 2",
"output": "YES"
},
{
"input": "500 50\n39... | 77 | 20,172,800 | 0 | 25,541 | |
122 | Lucky Substring | [
"brute force",
"implementation"
] | null | null | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
One day Petya was delivered a string *s*, containing only digits. He needs to find a string that
- represents a lucky number without leading zeroes,- is not empty,- is contained in *s* as a substring the maximum number of times.
Among all the strings for which the three conditions given above are fulfilled, Petya only needs the lexicographically minimum one. Find this string for Petya. | The single line contains a non-empty string *s* whose length can range from 1 to 50, inclusive. The string only contains digits. The string can contain leading zeroes. | In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes). | [
"047\n",
"16\n",
"472747\n"
] | [
"4\n",
"-1\n",
"7\n"
] | The lexicographical comparison of strings is performed by the < operator in the modern programming languages. String *x* is lexicographically less than string *y* either if *x* is a prefix of *y*, or exists such *i* (1ββ€β*i*ββ€β*min*(|*x*|,β|*y*|)), that *x*<sub class="lower-index">*i*</sub>β<β*y*<sub class="lower-index">*i*</sub> and for any *j* (1ββ€β*j*β<β*i*) *x*<sub class="lower-index">*j*</sub>β=β*y*<sub class="lower-index">*j*</sub>. Here |*a*| denotes the length of string *a*.
In the first sample three conditions are fulfilled for strings "4", "7" and "47". The lexicographically minimum one is "4".
In the second sample *s* has no substrings which are lucky numbers.
In the third sample the three conditions are only fulfilled for string "7". | [
{
"input": "047",
"output": "4"
},
{
"input": "16",
"output": "-1"
},
{
"input": "472747",
"output": "7"
},
{
"input": "1925",
"output": "-1"
},
{
"input": "5486846414848445484",
"output": "4"
},
{
"input": "516160414",
"output": "4"
},
{
"... | 124 | 0 | 3 | 25,788 | |
703 | Mishka and trip | [
"implementation",
"math"
] | null | null | Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXXΒ β beautiful, but little-known northern country.
Here are some interesting facts about XXX:
1. XXX consists of *n* cities, *k* of whose (just imagine!) are capital cities. 1. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of *i*-th city equals to *c**i*. 1. All the cities are consecutively connected by the roads, including 1-st and *n*-th city, forming a cyclic route 1<=β<=2<=β<=...<=β<=*n*<=β<=1. Formally, for every 1<=β€<=*i*<=<<=*n* there is a road between *i*-th and *i*<=+<=1-th city, and another one between 1-st and *n*-th city. 1. Each capital city is connected with each other city directly by the roads. Formally, if city *x* is a capital city, then for every 1<=β€<=*i*<=β€<=*n*,<=<=*i*<=β <=*x*, there is a road between cities *x* and *i*. 1. There is at most one road between any two cities. 1. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities *i* and *j*, price of passing it equals *c**i*Β·*c**j*.
Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities *a* and *b* (*a*<=<<=*b*), such that there is a road between *a* and *b* you are to find sum of products *c**a*Β·*c**b*. Will you help her? | The first line of the input contains two integers *n* and *k* (3<=β€<=*n*<=β€<=100<=000,<=1<=β€<=*k*<=β€<=*n*)Β β the number of cities in XXX and the number of capital cities among them.
The second line of the input contains *n* integers *c*1,<=*c*2,<=...,<=*c**n* (1<=β€<=*c**i*<=β€<=10<=000)Β β beauty values of the cities.
The third line of the input contains *k* distinct integers *id*1,<=*id*2,<=...,<=*id**k* (1<=β€<=*id**i*<=β€<=*n*)Β β indices of capital cities. Indices are given in ascending order. | Print the only integerΒ β summary price of passing each of the roads in XXX. | [
"4 1\n2 3 1 2\n3\n",
"5 2\n3 5 2 2 4\n1 4\n"
] | [
"17",
"71"
] | This image describes first sample case:
<img class="tex-graphics" src="https://espresso.codeforces.com/9dcb0e62694349977e064303a74efd880af5eb03.png" style="max-width: 100.0%;max-height: 100.0%;"/>
It is easy to see that summary price is equal to 17.
This image describes second sample case:
<img class="tex-graphics" src="https://espresso.codeforces.com/c30146637f3647c2fc77818bbbf3c080f94f116e.png" style="max-width: 100.0%;max-height: 100.0%;"/>
It is easy to see that summary price is equal to 71. | [
{
"input": "4 1\n2 3 1 2\n3",
"output": "17"
},
{
"input": "5 2\n3 5 2 2 4\n1 4",
"output": "71"
},
{
"input": "3 1\n1 1 1\n1",
"output": "3"
},
{
"input": "3 3\n1 1 1\n1 2 3",
"output": "3"
},
{
"input": "7 7\n6 9 2 7 4 8 7\n1 2 3 4 5 6 7",
"output": "775"
... | 187 | 11,468,800 | 3 | 25,792 | |
958 | Lightsabers (easy) | [
"implementation"
] | null | null | There is unrest in the Galactic Senate. Several thousand solar systems have declared their intentions to leave the Republic. Master Heidi needs to select the Jedi Knights who will go on peacekeeping missions throughout the galaxy. It is well-known that the success of any peacekeeping mission depends on the colors of the lightsabers of the Jedi who will go on that mission.
Heidi has *n* Jedi Knights standing in front of her, each one with a lightsaber of one of *m* possible colors. She knows that for the mission to be the most effective, she needs to select some contiguous interval of knights such that there are exactly *k*1 knights with lightsabers of the first color, *k*2 knights with lightsabers of the second color, ..., *k**m* knights with lightsabers of the *m*-th color. Help her find out if this is possible. | The first line of the input contains *n* (1<=β€<=*n*<=β€<=100) and *m* (1<=β€<=*m*<=β€<=*n*). The second line contains *n* integers in the range {1,<=2,<=...,<=*m*} representing colors of the lightsabers of the subsequent Jedi Knights. The third line contains *m* integers *k*1,<=*k*2,<=...,<=*k**m* (with ) β the desired counts of lightsabers of each color from 1 to *m*. | Output YES if an interval with prescribed color counts exists, or output NO if there is none. | [
"5 2\n1 1 2 2 1\n1 2\n"
] | [
"YES\n"
] | none | [
{
"input": "5 2\n1 1 2 2 1\n1 2",
"output": "YES"
},
{
"input": "1 1\n1\n1",
"output": "YES"
},
{
"input": "2 1\n1 1\n1",
"output": "YES"
},
{
"input": "2 1\n1 1\n2",
"output": "YES"
},
{
"input": "2 2\n1 2\n1 1",
"output": "YES"
},
{
"input": "3 3\n1 ... | 93 | 7,372,800 | 0 | 25,869 | |
656 | Out of Controls | [
"*special"
] | null | null | You are given a complete undirected graph. For each pair of vertices you are given the length of the edge that connects them. Find the shortest paths between each pair of vertices in the graph and return the length of the longest of them. | The first line of the input contains a single integer *N* (3<=β€<=*N*<=β€<=10).
The following *N* lines each contain *N* space-separated integers. *j*th integer in *i*th line *a**ij* is the length of the edge that connects vertices *i* and *j*. *a**ij*<==<=*a**ji*, *a**ii*<==<=0, 1<=β€<=*a**ij*<=β€<=100 for *i*<=β <=*j*. | Output the maximum length of the shortest path between any pair of vertices in the graph. | [
"3\n0 1 1\n1 0 4\n1 4 0\n",
"4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0\n"
] | [
"2\n",
"5\n"
] | You're running short of keywords, so you can't use some of them: | [
{
"input": "3\n0 1 1\n1 0 4\n1 4 0",
"output": "2"
},
{
"input": "4\n0 1 2 3\n1 0 4 5\n2 4 0 6\n3 5 6 0",
"output": "5"
},
{
"input": "10\n0 16 67 7 82 44 25 13 25 42\n16 0 24 37 63 20 19 87 55 99\n67 24 0 81 19 71 35 6 20 91\n7 37 81 0 82 89 34 80 7 32\n82 63 19 82 0 42 66 96 42 99\n44 ... | 46 | 0 | 3 | 26,055 | |
666 | Forensic Examination | [
"data structures",
"string suffix structures"
] | null | null | The country of Reberland is the archenemy of Berland. Recently the authorities of Berland arrested a Reberlandian spy who tried to bring the leaflets intended for agitational propaganda to Berland illegally . The most leaflets contain substrings of the Absolutely Inadmissible Swearword and maybe even the whole word.
Berland legal system uses the difficult algorithm in order to determine the guilt of the spy. The main part of this algorithm is the following procedure.
All the *m* leaflets that are brought by the spy are numbered from 1 to *m*. After that it's needed to get the answer to *q* queries of the following kind: "In which leaflet in the segment of numbers [*l*,<=*r*] the substring of the Absolutely Inadmissible Swearword [*p**l*,<=*p**r*] occurs more often?".
The expert wants you to automate that procedure because this time texts of leaflets are too long. Help him! | The first line contains the string *s* (1<=β€<=|*s*|<=β€<=5Β·105) β the Absolutely Inadmissible Swearword. The string *s* consists of only lowercase English letters.
The second line contains the only integer *m* (1<=β€<=*m*<=β€<=5Β·104) β the number of texts of leaflets for expertise.
Each of the next *m* lines contains the only string *t**i* β the text of the *i*-th leaflet. The sum of lengths of all leaflet texts doesn't exceed 5Β·104. The text of the leaflets consists of only lowercase English letters.
The next line contains integer *q* (1<=β€<=*q*<=β€<=5Β·105) β the number of queries for expertise.
Finally, each of the last *q* lines contains four integers *l*, *r*, *p**l*, *p**r* (1<=β€<=*l*<=β€<=*r*<=β€<=*m*,<=1<=β€<=*p**l*<=β€<=*p**r*<=β€<=|*s*|), where |*s*| is the length of the Absolutely Inadmissible Swearword. | Print *q* lines. The *i*-th of them should contain two integers β the number of the text with the most occurences and the number of occurences of the substring [*p**l*,<=*p**r*] of the string *s*. If there are several text numbers print the smallest one. | [
"suffixtree\n3\nsuffixtreesareawesome\ncartesiantreeisworsethansegmenttree\nnyeeheeheee\n2\n1 2 1 10\n1 3 9 10\n"
] | [
"1 1\n3 4\n"
] | none | [] | 451 | 6,656,000 | 0 | 26,065 | |
31 | TV Game | [
"dp"
] | E. TV Game | 2 | 256 | There is a new TV game on BerTV. In this game two players get a number *A* consisting of 2*n* digits. Before each turn players determine who will make the next move. Each player should make exactly *n* moves. On it's turn *i*-th player takes the leftmost digit of *A* and appends it to his or her number *S**i*. After that this leftmost digit is erased from *A*. Initially the numbers of both players (*S*1 and *S*2) are Β«emptyΒ». Leading zeroes in numbers *A*,<=*S*1,<=*S*2 are allowed. In the end of the game the first player gets *S*1 dollars, and the second gets *S*2 dollars.
One day Homer and Marge came to play the game. They managed to know the number *A* beforehand. They want to find such sequence of their moves that both of them makes exactly *n* moves and which maximizes their total prize. Help them. | The first line contains integer *n* (1<=β€<=*n*<=β€<=18). The second line contains integer *A* consisting of exactly 2*n* digits. This number can have leading zeroes. | Output the line of 2*n* characters Β«HΒ» and Β«MΒ» β the sequence of moves of Homer and Marge, which gives them maximum possible total prize. Each player must make exactly *n* moves. If there are several solutions, output any of them. | [
"2\n1234\n",
"2\n9911\n"
] | [
"HHMM",
"HMHM"
] | none | [
{
"input": "2\n1234",
"output": "HHMM"
},
{
"input": "2\n9911",
"output": "HMHM"
},
{
"input": "2\n0153",
"output": "HHMM"
},
{
"input": "3\n614615",
"output": "HHHMMM"
},
{
"input": "4\n21305374",
"output": "HHHHMMMM"
},
{
"input": "4\n00013213",
... | 216 | 2,252,800 | -1 | 26,133 |
0 | none | [
"none"
] | null | null | ΠΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΡΠΎΡΡΠΎΠΈΡ ΠΈΠ· *n* ΡΡΠ΅Π΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ Π²ΡΡΡΡΠΎΠ΅Π½Ρ Π² ΡΡΠ΄. ΠΡΠΎΠ½ΡΠΌΠ΅ΡΡΠ΅ΠΌ ΡΡΠ΅ΠΉΠΊΠΈ ΠΎΡ 1 Π΄ΠΎ *n* ΡΠ»Π΅Π²Π° Π½Π°ΠΏΡΠ°Π²ΠΎ. ΠΡΠΎ ΠΊΠ°ΠΆΠ΄ΡΡ ΡΡΠ΅ΠΉΠΊΡ ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π° ΠΎΠ½Π° ΠΈΠ»ΠΈ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ ΠΊΠ°ΠΊΠΎΠΌΡ-Π»ΠΈΠ±ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΡ (Π² ΡΠ°ΠΊΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΠΈΠ·Π²Π΅ΡΡΠ΅Π½ ΠΏΡΠΎΡΠ΅ΡΡ, ΠΊΠΎΡΠΎΡΠΎΠΌΡ ΠΎΠ½Π° ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ).
ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ° ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΡΠΎ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ Π΅ΠΌΡ ΡΡΠ΅ΠΉΠΊΠΈ Π·Π°Π½ΠΈΠΌΠ°ΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ Π½Π΅ΠΏΡΠ΅ΡΡΠ²Π½ΡΠΉ ΡΡΠ°ΡΡΠΎΠΊ. Π‘ ΠΏΠΎΠΌΠΎΡΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ Π²ΠΈΠ΄Π° Β«ΠΏΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· Π·Π°Π½ΡΡΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΡΡ, Π° Π·Π°Π½ΡΡΡΡ ΡΠ΅ΠΏΠ΅ΡΡ ΡΡΠΈΡΠ°ΡΡ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΠΎΠΉΒ» ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΠ°ΡΠΏΠΎΠ»ΠΎΠΆΠΈΡΡ Π²ΡΠ΅ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ ΠΏΡΠΎΡΠ΅ΡΡΠ°ΠΌ ΡΡΠ΅ΠΉΠΊΠΈ Π² Π½Π°ΡΠ°Π»Π΅ ΠΏΠ°ΠΌΡΡΠΈ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ°. ΠΡΡΠ³ΠΈΠΌΠΈ ΡΠ»ΠΎΠ²Π°ΠΌΠΈ, Π»ΡΠ±Π°Ρ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π°Ρ ΡΡΠ΅ΠΉΠΊΠ° Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠ°ΡΠΏΠΎΠ»Π°Π³Π°ΡΡΡΡ ΠΏΡΠ°Π²Π΅Π΅ (ΠΈΠΌΠ΅ΡΡ Π±ΠΎΠ»ΡΡΠΈΠΉ Π½ΠΎΠΌΠ΅Ρ) Π»ΡΠ±ΠΎΠΉ Π·Π°Π½ΡΡΠΎΠΉ.
ΠΠ°ΠΌ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°ΠΉΡΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΏΠ΅ΡΠ΅ΠΏΠΈΡΡΠ²Π°Π½ΠΈΡ Π΄Π°Π½Π½ΡΡ
ΠΈΠ· ΠΎΠ΄Π½ΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² Π΄ΡΡΠ³ΡΡ, Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠΎΡΠΎΡΡΡ
ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΡΡΠΈΡΡ ΠΎΠΏΠΈΡΠ°Π½Π½ΡΡ
ΡΡΠ»ΠΎΠ²ΠΈΠΉ. ΠΠΎΠΏΡΡΡΠΈΠΌΠΎ, ΡΡΠΎ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΡΡΠ΅Π΅ΠΊ Π² ΠΏΠ°ΠΌΡΡΠΈ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΈΠ· ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² ΠΈΠ·ΠΌΠ΅Π½ΠΈΡΡΡ ΠΏΠΎΡΠ»Π΅ Π΄Π΅ΡΡΠ°Π³ΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ, Π½ΠΎ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΡΠ°ΠΌΠΈΡ
ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΡΡΠ°ΡΡΡΡ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ. ΠΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ Π΅ΡΠ»ΠΈ Π²ΡΠ΅ ΡΡΠ΅ΠΉΠΊΠΈ, ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ ΠΏΡΠΎΡΠ΅ΡΡΡ *i*, Π½Π°Ρ
ΠΎΠ΄ΠΈΠ»ΠΈΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ ΡΠ°Π½ΡΡΠ΅ Π²ΡΠ΅Ρ
ΡΡΠ΅Π΅ΠΊ ΠΏΡΠΎΡΠ΅ΡΡΠ° *j*, ΡΠΎ ΠΈ ΠΏΠΎΡΠ»Π΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΠΉ ΡΡΠΎ ΡΡΠ»ΠΎΠ²ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π²ΡΠΏΠΎΠ»Π½ΡΡΡΡΡ.
Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ Π½ΠΎΠΌΠ΅ΡΠ° Π²ΡΠ΅Ρ
ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½Ρ, Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄Π½Π° ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ Π·Π°Π½ΡΡΠ° ΠΊΠ°ΠΊΠΈΠΌ-Π»ΠΈΠ±ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠΎΠΌ. | Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ ΡΠΈΡΠ»ΠΎ *n* (1<=β€<=*n*<=β€<=200<=000)Β β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠ΅Π΅ΠΊ Π² ΠΏΠ°ΠΌΡΡΠΈ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ°.
ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΡΡ *n* ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=*n*), Π³Π΄Π΅ *a**i* ΡΠ°Π²Π½ΠΎ Π»ΠΈΠ±ΠΎ 0 (ΡΡΠΎ ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ *i*-Ρ ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π°), Π»ΠΈΠ±ΠΎ Π½ΠΎΠΌΠ΅ΡΡ ΠΏΡΠΎΡΠ΅ΡΡΠ°, ΠΊΠΎΡΠΎΡΠΎΠΌΡ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ *i*-Ρ ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ. ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄Π½ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ *a**i* Π½Π΅ ΡΠ°Π²Π½ΠΎ 0.
ΠΡΠΎΡΠ΅ΡΡΡ ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Ρ ΡΠ΅Π»ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ ΠΎΡ 1 Π΄ΠΎ *n* Π² ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. ΠΡΠΈ ΡΡΠΎΠΌ ΠΏΡΠΎΡΠ΅ΡΡΡ Π½Π΅ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ. | ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎΒ β ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π½ΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ Π΄Π»Ρ Π΄Π΅ΡΡΠ°Π³ΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΏΠ°ΠΌΡΡΠΈ. | [
"4\n0 2 2 1\n",
"8\n0 8 8 8 0 4 4 2\n"
] | [
"2\n",
"4\n"
] | Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΠ΅ΡΡΠΎΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ Π΄Π²ΡΡ
ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ:
1. ΠΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΡΠ΅ΡΡΠ΅ΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΠΏΠ΅ΡΠ²ΡΡ. ΠΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΏΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΠΏΡΠΈΠΌΠ΅Ρ Π²ΠΈΠ΄: 2Β 2Β 0Β 1. 1. ΠΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΠ΅ΡΠ²Π΅ΡΡΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΡΡΠ΅ΡΡΡ. ΠΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΏΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΠΏΡΠΈΠΌΠ΅Ρ Π²ΠΈΠ΄: 2Β 2Β 1Β 0. | [
{
"input": "4\n0 2 2 1",
"output": "2"
},
{
"input": "8\n0 8 8 8 0 4 4 2",
"output": "4"
},
{
"input": "5\n0 0 0 1 1",
"output": "2"
},
{
"input": "6\n0 0 0 3 0 0",
"output": "1"
},
{
"input": "10\n0 10 10 0 0 3 3 0 0 0",
"output": "3"
},
{
"input": "1... | 404 | 34,713,600 | 3 | 26,273 | |
95 | Hockey | [
"implementation",
"strings"
] | A. Hockey | 2 | 256 | Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name *w* and the collection of forbidden substrings *s*1,<=*s*2,<=...,<=*s**n*. All those strings consist of uppercase and lowercase Latin letters. String *w* has the length of |*w*|, its characters are numbered from 1 to |*w*|.
First Petya should find all the occurrences of forbidden substrings in the *w* string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.
After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position *i* should be replaced by any other one if for position *i* in string *w* there exist pair of indices *l*,<=*r* (1<=β€<=*l*<=β€<=*i*<=β€<=*r*<=β€<=|*w*|) such that substring *w*[*l*Β ...Β *r*] is contained in the collection *s*1,<=*s*2,<=...,<=*s**n*, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.
Letter *letter* (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the *letter* occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.
Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them. | The first line contains the only integer *n* (1<=β€<=*n*<=β€<=100) β the number of forbidden substrings in the collection. Next *n* lines contain these substrings. The next line contains string *w*. All those *n*<=+<=1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter *letter*. | Output the only line β Petya's resulting string with the maximum number of letters *letter*. If there are several answers then output the one that comes first lexicographically.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line *a* is lexicographically smaller than the line *b*, if *a* is a prefix of *b*, or there exists such an *i* (1<=β€<=*i*<=β€<=|*a*|), that *a**i*<=<<=*b**i*, and for any *j* (1<=β€<=*j*<=<<=*i*) *a**j*<==<=*b**j*. |*a*| stands for the length of string *a*. | [
"3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt\n",
"4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na\n",
"2\naCa\ncba\nabAcaba\nc\n"
] | [
"PetrLovtTttttNumtttt\n",
"petrsmatchwin\n",
"abCacba\n"
] | none | [
{
"input": "3\nbers\nucky\nelu\nPetrLoveLuckyNumbers\nt",
"output": "PetrLovtTttttNumtttt"
},
{
"input": "4\nhello\nparty\nabefglghjdhfgj\nIVan\npetrsmatchwin\na",
"output": "petrsmatchwin"
},
{
"input": "2\naCa\ncba\nabAcaba\nc",
"output": "abCacba"
},
{
"input": "3\nlol\nhe... | 404 | 19,763,200 | 0 | 26,381 |
193 | Cutting Figure | [
"constructive algorithms",
"graphs",
"trees"
] | null | null | You've gotten an *n*<=Γ<=*m* sheet of squared paper. Some of its squares are painted. Let's mark the set of all painted squares as *A*. Set *A* is connected. Your task is to find the minimum number of squares that we can delete from set *A* to make it not connected.
A set of painted squares is called connected, if for every two squares *a* and *b* from this set there is a sequence of squares from the set, beginning in *a* and ending in *b*, such that in this sequence any square, except for the last one, shares a common side with the square that follows next in the sequence. An empty set and a set consisting of exactly one square are connected by definition. | The first input line contains two space-separated integers *n* and *m* (1<=β€<=*n*,<=*m*<=β€<=50) β the sizes of the sheet of paper.
Each of the next *n* lines contains *m* characters β the description of the sheet of paper: the *j*-th character of the *i*-th line equals either "#", if the corresponding square is painted (belongs to set *A*), or equals "." if the corresponding square is not painted (does not belong to set *A*). It is guaranteed that the set of all painted squares *A* is connected and isn't empty. | On the first line print the minimum number of squares that need to be deleted to make set *A* not connected. If it is impossible, print -1. | [
"5 4\n####\n#..#\n#..#\n#..#\n####\n",
"5 5\n#####\n#...#\n#####\n#...#\n#####\n"
] | [
"2\n",
"2\n"
] | In the first sample you can delete any two squares that do not share a side. After that the set of painted squares is not connected anymore.
The note to the second sample is shown on the figure below. To the left there is a picture of the initial set of squares. To the right there is a set with deleted squares. The deleted squares are marked with crosses. | [
{
"input": "5 4\n####\n#..#\n#..#\n#..#\n####",
"output": "2"
},
{
"input": "5 5\n#####\n#...#\n#####\n#...#\n#####",
"output": "2"
},
{
"input": "1 10\n.########.",
"output": "1"
},
{
"input": "1 1\n#",
"output": "-1"
},
{
"input": "3 3\n.#.\n###\n.#.",
"outp... | 124 | 1,536,000 | 0 | 26,399 | |
754 | Vladik and chat | [
"brute force",
"constructive algorithms",
"dp",
"implementation",
"strings"
] | null | null | Recently Vladik discovered a new entertainmentΒ β coding bots for social networks. He would like to use machine learning in his bots so now he want to prepare some learning data for them.
At first, he need to download *t* chats. Vladik coded a script which should have downloaded the chats, however, something went wrong. In particular, some of the messages have no information of their sender. It is known that if a person sends several messages in a row, they all are merged into a single message. It means that there could not be two or more messages in a row with the same sender. Moreover, a sender never mention himself in his messages.
Vladik wants to recover senders of all the messages so that each two neighboring messages will have different senders and no sender will mention himself in his messages.
He has no idea of how to do this, and asks you for help. Help Vladik to recover senders in each of the chats! | The first line contains single integer *t* (1<=β€<=*t*<=β€<=10) β the number of chats. The *t* chats follow. Each chat is given in the following format.
The first line of each chat description contains single integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of users in the chat.
The next line contains *n* space-separated distinct usernames. Each username consists of lowercase and uppercase English letters and digits. The usernames can't start with a digit. Two usernames are different even if they differ only with letters' case. The length of username is positive and doesn't exceed 10 characters.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=100)Β β the number of messages in the chat. The next *m* line contain the messages in the following formats, one per line:
- <username>:<text>Β β the format of a message with known sender. The username should appear in the list of usernames of the chat. - <?>:<text>Β β the format of a message with unknown sender.
The text of a message can consist of lowercase and uppercase English letter, digits, characters '.' (dot), ',' (comma), '!' (exclamation mark), '?' (question mark) and ' ' (space). The text doesn't contain trailing spaces. The length of the text is positive and doesn't exceed 100 characters.
We say that a text mention a user if his username appears in the text as a word. In other words, the username appears in a such a position that the two characters before and after its appearance either do not exist or are not English letters or digits. For example, the text "Vasya, masha13 and Kate!" can mention users "Vasya", "masha13", "and" and "Kate", but not "masha".
It is guaranteed that in each chat no known sender mention himself in his messages and there are no two neighboring messages with the same known sender. | Print the information about the *t* chats in the following format:
If it is not possible to recover senders, print single line "Impossible" for this chat. Otherwise print *m* messages in the following format:
<username>:<text>
If there are multiple answers, print any of them. | [
"1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi\n",
"1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine\n",
"2\n3\nnetman vladik Fedosik\n2\n?: users are netman, vladik, Fedosik\nvladik: something wrong with this chat\n4\nnetman tigerrrrr banany2001 klinchuh\n4\n?: tigerrrrr, banany2... | [
"netman: Hello, Vladik!\nVladik: Hi\n",
"Impossible\n",
"Impossible\nnetman: tigerrrrr, banany2001, klinchuh, my favourite team ever, are you ready?\nklinchuh: yes, coach!\ntigerrrrr: yes, netman\nbanany2001: yes of course.\n"
] | none | [
{
"input": "1\n2\nVladik netman\n2\n?: Hello, Vladik!\n?: Hi",
"output": "netman: Hello, Vladik!\nVladik: Hi"
},
{
"input": "1\n2\nnetman vladik\n3\nnetman:how are you?\n?:wrong message\nvladik:im fine",
"output": "Impossible"
},
{
"input": "2\n3\nnetman vladik Fedosik\n2\n?: users are n... | 140 | 3,686,400 | 0 | 26,401 | |
394 | Lightbulb for Minister | [
"geometry"
] | null | null | The Minister for education is coming! Naturally, nobody wants to perform poorly in front of such a honored guest. However, two hours before the arrival it turned out that one of the classes has a malfunctioning lightbulb β for some reason it doesn't get enough energy. The solution was found quickly: all we've got to do is to change the location of the lightbulb so that it got the maximum amount of energy.
Everybody knows that the power of the lightbulb equals , where *C* is some constant value and *r**i* is the Euclidean distance from the bulb to the *i*-th generator. Consequently, our task is to minimize . Of course, we know the positions of all generators.
The bulb should be on the ceiling of the class. The ceiling of the class is in the form of a strictly convex *m*-gon (the class itself has the form of a right prism with a strictly convex *m*-gon at the bottom). Help to find the optimum location for the bulb. Assume that all generators are in the plane of the class ceiling. Consider that the plane of the class ceiling has some Cartesian coordinate system introduced. | The first line contains integer *n* (2<=β€<=*n*<=β€<=105) β the number of generators. Each of the next *n* lines contains a pair of integers *x**i*, *y**i*, representing the coordinates of the *i*-th generator in the plane of the class ceiling. It's guaranteed that no two generators have the same location.
The next line contains integer *m* (3<=β€<=*m*<=β€<=105) β the number of vertexes in the convex polygon that describes the ceiling of the class. Each of the following *m* lines contains a pair of integers *p**i*,<=*q**i*, representing the coordinates of the *i*-th point of the polygon in the clockwise order. It's guaranteed that the polygon is strictly convex.
The absolute value of all the coordinates don't exceed 106. | Print a single real number β the minimum value of the sum of squares of distances from the generators to the point of the lightbulb's optimal position. The answer will be considered valid if its absolute or relative error doesn't exceed 10<=-<=4. | [
"4\n3 2\n3 4\n5 4\n5 2\n4\n3 3\n4 4\n5 3\n4 2\n"
] | [
"8.00000000"
] | We'll define a strictly convex polygon as a convex polygon with the following property: no three vertices of the polygon lie on the same line. | [] | 46 | 0 | 0 | 26,422 | |
0 | none | [
"none"
] | null | null | This is an interactive problem.
Jury has hidden a permutation *p* of integers from 0 to *n*<=-<=1. You know only the length *n*. Remind that in permutation all integers are distinct.
Let *b* be the inverse permutation for *p*, i.e. *p**b**i*<==<=*i* for all *i*. The only thing you can do is to ask xor of elements *p**i* and *b**j*, printing two indices *i* and *j* (not necessarily distinct). As a result of the query with indices *i* and *j* you'll get the value , where denotes the xor operation. You can find the description of xor operation in notes.
Note that some permutations can remain indistinguishable from the hidden one, even if you make all possible *n*2 queries. You have to compute the number of permutations indistinguishable from the hidden one, and print one of such permutations, making no more than 2*n* queries.
The hidden permutation does not depend on your queries. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=5000) β the length of the hidden permutation. You should read this integer first. | When your program is ready to print the answer, print three lines.
In the first line print "!".
In the second line print single integer *answers*_*cnt*Β β the number of permutations indistinguishable from the hidden one, including the hidden one.
In the third line print *n* integers *p*0,<=*p*1,<=...,<=*p**n*<=-<=1 (0<=β€<=*p**i*<=<<=*n*, all *p**i* should be distinct)Β β one of the permutations indistinguishable from the hidden one.
Your program should terminate after printing the answer. | [
"3\n0\n0\n3\n2\n3\n2",
"4\n2\n3\n2\n0\n2\n3\n2\n0"
] | [
"? 0 0\n? 1 1\n? 1 2\n? 0 2\n? 2 1\n? 2 0\n!\n1\n0 1 2",
"? 0 1\n? 1 2\n? 2 3\n? 3 3\n? 3 2\n? 2 1\n? 1 0\n? 0 0\n!\n2\n3 1 2 0"
] | xor operation, or bitwise exclusive OR, is an operation performed over two integers, in which the *i*-th digit in binary representation of the result is equal to 1 if and only if exactly one of the two integers has the *i*-th digit in binary representation equal to 1. For more information, see [here](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
In the first example *p*β=β[0,β1,β2], thus *b*β=β[0,β1,β2], the values <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png" style="max-width: 100.0%;max-height: 100.0%;"/> are correct for the given *i*,β*j*. There are no other permutations that give the same answers for the given queries.
The answers for the queries are:
- <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/b36cf4ccf629eaa3a225cda06a5697eb030eb4d7.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/88e848d2e69ecfb3b8c8d63fdc8949c7cc1e9f28.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/233e40a031e99efe33bdf68f4a383163a23e1e7b.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3ec81fca9911a6bde9c2c7cae6c189514105085f.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/be5d5595e56564765e3db6b865de2219cdadadeb.png" style="max-width: 100.0%;max-height: 100.0%;"/>, - <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/3355af6093e240986c338bf13071b479e2604ca9.png" style="max-width: 100.0%;max-height: 100.0%;"/>.
In the second example *p*β=β[3,β1,β2,β0], and *b*β=β[3,β1,β2,β0], the values <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/5dae53b8ed0e62c9af6c21130fc022b384e359ff.png" style="max-width: 100.0%;max-height: 100.0%;"/> match for all pairs *i*,β*j*. But there is one more suitable permutation *p*β=β[0,β2,β1,β3], *b*β=β[0,β2,β1,β3] that matches all *n*<sup class="upper-index">2</sup> possible queries as well. All other permutations do not match even the shown queries. | [
{
"input": "3\n0 1 2",
"output": "1\n0 1 2 "
},
{
"input": "4\n3 1 2 0",
"output": "2\n0 2 1 3 "
},
{
"input": "4\n3 2 1 0",
"output": "4\n0 1 2 3 "
},
{
"input": "8\n2 3 0 1 4 5 6 7",
"output": "4\n0 1 2 3 6 7 4 5 "
},
{
"input": "1\n0",
"output": "1\n0 "
}... | 2,000 | 716,800 | 0 | 26,458 | |
935 | Fafa and Ancient Mathematics | [
"dfs and similar",
"dp",
"trees"
] | null | null | Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression.
An Ahmes arithmetic expression can be defined as:
- "*d*" is an Ahmes arithmetic expression, where *d* is a one-digit positive integer; - "(*E*1<=*op*<=*E*2)" is an Ahmes arithmetic expression, where *E*1 and *E*2 are valid Ahmes arithmetic expressions (without spaces) and *op* is either plus (<=+<=) or minus (<=-<=).
On his trip to Egypt, Fafa found a piece of papyrus paper having one of these Ahmes arithmetic expressions written on it. Being very ancient, the papyrus piece was very worn out. As a result, all the operators were erased, keeping only the numbers and the brackets. Since Fafa loves mathematics, he decided to challenge himself with the following task:
Given the number of plus and minus operators in the original expression, find out the maximum possible value for the expression on the papyrus paper after putting the plus and minus operators in the place of the original erased operators. | The first line contains a string *E* (1<=β€<=|*E*|<=β€<=104) β a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'.
The second line contains two space-separated integers *P* and *M* (0<=β€<=*min*(*P*,<=*M*)<=β€<=100) β the number of plus and minus operators, respectively.
It is guaranteed that *P*<=+<=*M*<==<= the number of erased operators. | Print one line containing the answer to the problem. | [
"(1?1)\n1 0\n",
"(2?(1?2))\n1 1\n",
"((1?(5?7))?((6?2)?7))\n3 2\n",
"((1?(5?7))?((6?2)?7))\n2 3\n"
] | [
"2\n",
"1\n",
"18\n",
"16\n"
] | - The first sample will be (1β+β1)ββ=ββ2. - The second sample will be (2β+β(1β-β2))ββ=ββ1. - The third sample will be ((1β-β(5β-β7))β+β((6β+β2)β+β7))ββ=ββ18. - The fourth sample will be ((1β+β(5β+β7))β-β((6β-β2)β-β7))ββ=ββ16. | [
{
"input": "(1?1)\n1 0",
"output": "2"
},
{
"input": "(2?(1?2))\n1 1",
"output": "1"
},
{
"input": "((1?(5?7))?((6?2)?7))\n3 2",
"output": "18"
},
{
"input": "((1?(5?7))?((6?2)?7))\n2 3",
"output": "16"
},
{
"input": "((4?3)?(((2?(4?((4?((2?(3?(7?3)))?(((((3?(6?2)... | 46 | 0 | 0 | 26,464 | |
228 | Zigzag | [
"data structures"
] | null | null | The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function.
The Zigag's sequence with the zigzag factor z is an infinite sequence *S**i**z* (*i*<=β₯<=1;Β *z*<=β₯<=2), that is determined as follows:
- *S**i**z*<==<=2, when ; - , when ; - , when .
Operation means taking the remainder from dividing number *x* by number *y*. For example, the beginning of sequence *S**i*3 (zigzag factor 3) looks as follows: 1, 2, 3, 2, 1, 2, 3, 2, 1.
Let's assume that we are given an array *a*, consisting of *n* integers. Let's define element number *i* (1<=β€<=*i*<=β€<=*n*) of the array as *a**i*. The Zigzag function is function , where *l*,<=*r*,<=*z* satisfy the inequalities 1<=β€<=*l*<=β€<=*r*<=β€<=*n*, *z*<=β₯<=2.
To become better acquainted with the Zigzag sequence and the Zigzag function, the wizard offers you to implement the following operations on the given array *a*.
1. The assignment operation. The operation parameters are (*p*,<=*v*). The operation denotes assigning value *v* to the *p*-th array element. After the operation is applied, the value of the array element *a**p* equals *v*. 1. The Zigzag operation. The operation parameters are (*l*,<=*r*,<=*z*). The operation denotes calculating the Zigzag function *Z*(*l*,<=*r*,<=*z*).
Explore the magical powers of zigzags, implement the described operations. | The first line contains integer *n* (1<=β€<=*n*<=β€<=105) β The number of elements in array *a*. The second line contains *n* space-separated integers: *a*1,<=*a*2,<=...,<=*a**n* (1<=β€<=*a**i*<=β€<=109) β the elements of the array.
The third line contains integer *m* (1<=β€<=*m*<=β€<=105) β the number of operations. Next *m* lines contain the operations' descriptions. An operation's description starts with integer *t**i* (1<=β€<=*t**i*<=β€<=2) β the operation type.
- If *t**i*<==<=1 (assignment operation), then on the line follow two space-separated integers: *p**i*,<=*v**i* (1<=β€<=*p**i*<=β€<=*n*;Β 1<=β€<=*v**i*<=β€<=109) β the parameters of the assigning operation. - If *t**i*<==<=2 (Zigzag operation), then on the line follow three space-separated integers: *l**i*,<=*r**i*,<=*z**i* (1<=β€<=*l**i*<=β€<=*r**i*<=β€<=*n*;Β 2<=β€<=*z**i*<=β€<=6) β the parameters of the Zigzag operation.
You should execute the operations in the order, in which they are given in the input. | For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input.
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. | [
"5\n2 3 1 5 5\n4\n2 2 3 2\n2 1 5 3\n1 3 5\n2 1 5 3\n"
] | [
"5\n26\n38\n"
] | Explanation of the sample test:
- Result of the first operation is *Z*(2,β3,β2)β=β3Β·1β+β1Β·2β=β5. - Result of the second operation is *Z*(1,β5,β3)β=β2Β·1β+β3Β·2β+β1Β·3β+β5Β·2β+β5Β·1β=β26. - After the third operation array *a* is equal to 2,β3,β5,β5,β5. - Result of the forth operation is *Z*(1,β5,β3)β=β2Β·1β+β3Β·2β+β5Β·3β+β5Β·2β+β5Β·1β=β38. | [] | 92 | 0 | 0 | 26,509 | |
317 | Game with Powers | [
"dp",
"games"
] | null | null | Vasya and Petya wrote down all integers from 1 to *n* to play the "powers" game (*n* can be quite large; however, Vasya and Petya are not confused by this fact).
Players choose numbers in turn (Vasya chooses first). If some number *x* is chosen at the current turn, it is forbidden to choose *x* or all of its other positive integer powers (that is, *x*2, *x*3, ...) at the next turns. For instance, if the number 9 is chosen at the first turn, one cannot choose 9 or 81 later, while it is still allowed to choose 3 or 27. The one who cannot make a move loses.
Who wins if both Vasya and Petya play optimally? | Input contains single integer *n* (1<=β€<=*n*<=β€<=109). | Print the name of the winner β "Vasya" or "Petya" (without quotes). | [
"1\n",
"2\n",
"8\n"
] | [
"Vasya\n",
"Petya\n",
"Petya\n"
] | In the first sample Vasya will choose 1 and win immediately.
In the second sample no matter which number Vasya chooses during his first turn, Petya can choose the remaining number and win. | [
{
"input": "1",
"output": "Vasya"
},
{
"input": "2",
"output": "Petya"
},
{
"input": "8",
"output": "Petya"
},
{
"input": "52",
"output": "Petya"
},
{
"input": "53",
"output": "Vasya"
},
{
"input": "3",
"output": "Vasya"
},
{
"input": "4",
... | 92 | 0 | 0 | 26,563 | |
758 | Broken Tree | [
"dfs and similar",
"dp",
"graphs",
"greedy",
"trees"
] | null | null | You are given a tree that has *n* vertices, which are numbered from 1 to *n*, where the vertex number one is the root. Each edge has weight *w**i* and strength *p**i*.
Botanist Innokentiy, who is the only member of the jury of the Olympiad in Informatics, doesn't like broken trees.
The tree is broken if there is such an edge the strength of which is less than the sum of weight of subtree's edges to which it leads.
It is allowed to reduce weight of any edge by arbitrary integer value, but then the strength of its edge is reduced by the same value. It means if the weight of the edge is 10, and the strength is 12, then by the reducing the weight by 7 its weight will equal 3, and the strength will equal 5.
It is not allowed to increase the weight of the edge.
Your task is to get the tree, which is not broken, by reducing the weight of edges of the given tree, and also all edged should have the positive weight, moreover, the total weight of all edges should be as large as possible.
It is obvious that the strength of edges can not be negative, however it can equal zero if the weight of the subtree equals zero. | The first line contains the integer *n* (1<=β€<=*n*<=β€<=2Β·105)Β β the number of vertices in the tree. The next *n*<=-<=1 lines contains the description of edges. Each line contains four integers *x*, *y*, *w*, *p* (1<=β€<=*x*,<=*y*<=β€<=*n*,<=1<=β€<=*w*<=β€<=109,<=0<=β€<=*p*<=β€<=109), where *x* and *y*Β β vertices which connect the edge (the vertex number *x* is the parent of the vertex number *y*), *w* and *p* are the weight and the strength of the edge, accordingly. It is guaranteed that the edges describe the tree with the root in the vertex 1. | If it is impossible to get unbroken tree from the given tree, print -1 in the only line.
Otherwise, the output data should contain *n* lines:
In the first line print the number *n*Β β the number of vertices on the tree.
In the next *n*<=-<=1 lines print the description of edges of the resulting tree. Each line should contain four integers *x*, *y*, *w*, *p* (1<=β€<=*x*,<=*y*<=β€<=*n*,<=1<=β€<=*w*<=β€<=109,<=0<=β€<=*p*<=β€<=109), where *x* and *y*Β β vertices, which the edge connects (the vertex number *x* is the parent of the vertex number *y*), *w* and *p* are the new weight and the strength of the edge, accordingly.
Print edges in the same order as they are given in input data: the first two integers of each line should not be changed. | [
"3\n1 3 5 7\n3 2 4 3\n",
"4\n1 3 2 3\n3 4 5 1\n3 2 3 3\n",
"5\n1 2 2 4\n2 4 1 9\n4 5 5 6\n4 3 4 8\n",
"7\n1 2 5 2\n2 3 4 3\n1 4 3 7\n4 5 4 1\n4 6 3 2\n6 7 1 6\n"
] | [
"3\n1 3 5 7\n3 2 4 3\n",
"-1",
"5\n1 2 2 4\n2 4 1 9\n4 5 1 2\n4 3 2 6\n",
"7\n1 2 5 2\n2 3 2 1\n1 4 3 7\n4 5 3 0\n4 6 3 2\n6 7 1 6\n"
] | none | [] | 46 | 0 | 0 | 26,571 | |
534 | Simplified Nonogram | [
"bitmasks",
"dp",
"hashing",
"meet-in-the-middle"
] | null | null | In this task you have to write a program dealing with nonograms on fields no larger than 5<=Γ<=20.
Simplified nonogram is a task where you have to build such field (each cell is either white or black) that satisfies the given information about rows and columns. For each row and each column the number of contiguous black segments is specified.
For example if size of the field is *n*<==<=3,<=*m*<==<=5, Π°nd numbers of contiguous black segments in rows are: [2,<=3,<=2] and in columns are: [1,<=0,<=1,<=2,<=1] then the solution may look like:
It is guaranteed that on each test in the testset there exists at least one solution. | In the first line there follow two integers *n*, *m* (1<=β€<=*n*<=β€<=5,<=1<=β€<=*m*<=β€<=20) β number of rows and number of columns respectively.
Second line contains *n* integers *a*1,<=*a*2,<=...,<=*a**n* where *a**i* is the number of contiguous black segments in *i*-th row of the field.
Similarly, third line contains *m* integers *b*1,<=*b*2,<=...,<=*b**m* where *b**i* is the number of contiguous black segments in the *i*-th column of the field.
It is guaranteed that there exists at least one solution. | Output any possible solution. Output should consist of *n* lines each containing *m* characters. Denote white cell as "." and black cell as "*". | [
"3 5\n2 3 2\n1 0 1 2 1\n",
"3 3\n2 1 2\n2 1 2\n",
"3 3\n1 0 1\n2 2 2\n"
] | [
"*.**.\n*.*.*\n*..**",
"*.*\n.*.\n*.*\n",
"***\n...\n***\n"
] | none | [
{
"input": "3 5\n2 3 2\n1 0 1 2 1",
"output": "*..**\n*.*.*\n*..**"
},
{
"input": "3 3\n2 1 2\n2 1 2",
"output": "*.*\n.*.\n*.*"
},
{
"input": "3 3\n1 0 1\n2 2 2",
"output": "***\n...\n***"
},
{
"input": "1 1\n1\n1",
"output": "*"
},
{
"input": "1 1\n0\n0",
"o... | 30 | 0 | 0 | 26,608 | |
919 | Congruence Equation | [
"chinese remainder theorem",
"math",
"number theory"
] | null | null | Given an integer $x$. Your task is to find out how many positive integers $n$ ($1 \leq n \leq x$) satisfy $$n \cdot a^n \equiv b \quad (\textrm{mod}\;p),$$ where $a, b, p$ are all known constants. | The only line contains four integers $a,b,p,x$ ($2 \leq p \leq 10^6+3$, $1 \leq a,b < p$, $1 \leq x \leq 10^{12}$). It is guaranteed that $p$ is a prime. | Print a single integer: the number of possible answers $n$. | [
"2 3 5 8\n",
"4 6 7 13\n",
"233 233 10007 1\n"
] | [
"2\n",
"1\n",
"1\n"
] | In the first sample, we can see that $n=2$ and $n=8$ are possible answers. | [
{
"input": "2 3 5 8",
"output": "2"
},
{
"input": "4 6 7 13",
"output": "1"
},
{
"input": "233 233 10007 1",
"output": "1"
},
{
"input": "338792 190248 339821 152634074578",
"output": "449263"
},
{
"input": "629260 663548 739463 321804928248",
"output": "43481... | 701 | 2,150,400 | 3 | 26,712 | |
868 | Huge Strings | [
"bitmasks",
"brute force",
"dp",
"implementation",
"strings"
] | null | null | You are given *n* strings *s*1,<=*s*2,<=...,<=*s**n* consisting of characters 0 and 1. *m* operations are performed, on each of them you concatenate two existing strings into a new one. On the *i*-th operation the concatenation *s**a**i**s**b**i* is saved into a new string *s**n*<=+<=*i* (the operations are numbered starting from 1). After each operation you need to find the maximum positive integer *k* such that all possible strings consisting of 0 and 1 of length *k* (there are 2*k* such strings) are substrings of the new string. If there is no such *k*, print 0. | The first line contains single integer *n* (1<=β€<=*n*<=β€<=100)Β β the number of strings. The next *n* lines contain strings *s*1,<=*s*2,<=...,<=*s**n* (1<=β€<=|*s**i*|<=β€<=100), one per line. The total length of strings is not greater than 100.
The next line contains single integer *m* (1<=β€<=*m*<=β€<=100)Β β the number of operations. *m* lines follow, each of them contains two integers *a**i* abd *b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*<=+<=*i*<=-<=1)Β β the number of strings that are concatenated to form *s**n*<=+<=*i*. | Print *m* lines, each should contain one integerΒ β the answer to the question after the corresponding operation. | [
"5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4\n"
] | [
"1\n2\n0\n"
] | On the first operation, a new string "0110" is created. For *k*β=β1 the two possible binary strings of length *k* are "0" and "1", they are substrings of the new string. For *k*β=β2 and greater there exist strings of length *k* that do not appear in this string (for *k*β=β2 such string is "00"). So the answer is 1.
On the second operation the string "01100" is created. Now all strings of length *k*β=β2 are present.
On the third operation the string "1111111111" is created. There is no zero, so the answer is 0. | [
{
"input": "5\n01\n10\n101\n11111\n0\n3\n1 2\n6 5\n4 4",
"output": "1\n2\n0"
},
{
"input": "5\n01\n1\n0011\n0\n01\n6\n5 5\n3 2\n4 2\n6 7\n5 1\n9 7",
"output": "1\n1\n1\n2\n1\n2"
},
{
"input": "5\n111101000111100011100110000100\n000111001\n01101000\n000011010010001001100100000001010010011... | 2,000 | 18,534,400 | 0 | 26,779 | |
615 | Longtail Hedgehog | [
"dp",
"graphs"
] | null | null | This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of *n* points connected by *m* segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions:
1. Only segments already presented on the picture can be painted; 1. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; 1. The numbers of points from the beginning of the tail to the end should strictly increase.
Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get.
Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications. | First line of the input contains two integers *n* and *m*(2<=β€<=*n*<=β€<=100<=000, 1<=β€<=*m*<=β€<=200<=000)Β β the number of points and the number segments on the picture respectively.
Then follow *m* lines, each containing two integers *u**i* and *v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*)Β β the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points. | Print the maximum possible value of the hedgehog's beauty. | [
"8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7\n",
"4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4\n"
] | [
"9\n",
"12\n"
] | The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3Β·3β=β9.
<img class="tex-graphics" src="https://espresso.codeforces.com/b3601595b30564928c0ce72c4d371941925f12e3.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "8 6\n4 5\n3 5\n2 5\n1 2\n2 8\n6 7",
"output": "9"
},
{
"input": "4 6\n1 2\n1 3\n1 4\n2 3\n2 4\n3 4",
"output": "12"
},
{
"input": "5 7\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2",
"output": "9"
},
{
"input": "5 9\n1 3\n2 4\n4 5\n5 3\n2 1\n1 4\n3 2\n1 5\n2 5",
"output"... | 623 | 41,984,000 | 3 | 26,816 | |
909 | Coprocessor | [
"dfs and similar",
"dp",
"graphs",
"greedy"
] | null | null | You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depends on have already completed.
Some of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically.
Find the minimal number of coprocessor calls which are necessary to execute the given program. | The first line contains two space-separated integers *N* (1<=β€<=*N*<=β€<=105) β the total number of tasks given, and *M* (0<=β€<=*M*<=β€<=105) β the total number of dependencies between tasks.
The next line contains *N* space-separated integers . If *E**i*<==<=0, task *i* can only be executed on the main processor, otherwise it can only be executed on the coprocessor.
The next *M* lines describe the dependencies between tasks. Each line contains two space-separated integers *T*1 and *T*2 and means that task *T*1 depends on task *T*2 (*T*1<=β <=*T*2). Tasks are indexed from 0 to *N*<=-<=1. All *M* pairs (*T*1,<=*T*2) are distinct. It is guaranteed that there are no circular dependencies between tasks. | Output one line containing an integer β the minimal number of coprocessor calls necessary to execute the program. | [
"4 3\n0 1 0 1\n0 1\n1 2\n2 3\n",
"4 3\n1 1 1 0\n0 1\n0 2\n3 0\n"
] | [
"2\n",
"1\n"
] | In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, and finally you execute task 0 on the main processor.
In the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor. | [
{
"input": "4 3\n0 1 0 1\n0 1\n1 2\n2 3",
"output": "2"
},
{
"input": "4 3\n1 1 1 0\n0 1\n0 2\n3 0",
"output": "1"
},
{
"input": "10 39\n0 1 0 1 0 1 1 0 1 1\n0 1\n0 2\n0 3\n0 4\n0 5\n0 6\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n1 8\n1 9\n2 3\n2 4\n2 5\n2 6\n2 7\n2 8\n2 9\n3 4\n3 6\n3 7\n3 8\n3 9\n... | 0 | 0 | -1 | 26,885 | |
570 | Pig and Palindromes | [
"combinatorics",
"dp"
] | null | null | Peppa the Pig was walking and walked into the forest. What a strange coincidence! The forest has the shape of a rectangle, consisting of *n* rows and *m* columns. We enumerate the rows of the rectangle from top to bottom with numbers from 1 to *n*, and the columns β from left to right with numbers from 1 to *m*. Let's denote the cell at the intersection of the *r*-th row and the *c*-th column as (*r*,<=*c*).
Initially the pig stands in cell (1,<=1), and in the end she wants to be in cell (*n*,<=*m*). Since the pig is in a hurry to get home, she can go from cell (*r*,<=*c*), only to either cell (*r*<=+<=1,<=*c*) or (*r*,<=*c*<=+<=1). She cannot leave the forest.
The forest, where the pig is, is very unusual. Some cells of the forest similar to each other, and some look very different. Peppa enjoys taking pictures and at every step she takes a picture of the cell where she is now. The path through the forest is considered to be beautiful if photographs taken on her way, can be viewed in both forward and in reverse order, showing the same sequence of photos. More formally, the line formed by the cells in order of visiting should be a palindrome (you can read a formal definition of a palindrome in the previous problem).
Count the number of beautiful paths from cell (1,<=1) to cell (*n*,<=*m*). Since this number can be very large, determine the remainder after dividing it by 109<=+<=7. | The first line contains two integers *n*,<=*m* (1<=β€<=*n*,<=*m*<=β€<=500) β the height and width of the field.
Each of the following *n* lines contains *m* lowercase English letters identifying the types of cells of the forest. Identical cells are represented by identical letters, different cells are represented by different letters. | Print a single integer β the number of beautiful paths modulo 109<=+<=7. | [
"3 4\naaab\nbaaa\nabba\n"
] | [
"3"
] | Picture illustrating possibilities for the sample test.
<img class="tex-graphics" src="https://espresso.codeforces.com/bf73568d1cf80d89f66d4e472a91ae0339af83a2.png" style="max-width: 100.0%;max-height: 100.0%;"/>
<img class="tex-graphics" src="https://espresso.codeforces.com/1e870880d976e642be85498efab6bbd10bae8a84.png" style="max-width: 100.0%;max-height: 100.0%;"/> | [
{
"input": "3 4\naaab\nbaaa\nabba",
"output": "3"
},
{
"input": "2 2\nab\naa",
"output": "2"
},
{
"input": "3 5\nqqqrw\nwqqtw\newqqq",
"output": "3"
},
{
"input": "1 5\nabbba",
"output": "1"
},
{
"input": "1 5\nabbbb",
"output": "0"
},
{
"input": "1 4\... | 2,885 | 13,107,200 | 3 | 26,902 | |
678 | The Same Calendar | [
"implementation"
] | null | null | The girl Taylor has a beautiful calendar for the year *y*. In the calendar all days are given with their days of week: Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday.
The calendar is so beautiful that she wants to know what is the next year after *y* when the calendar will be exactly the same. Help Taylor to find that year.
Note that leap years has 366 days. The year is leap if it is divisible by 400 or it is divisible by 4, but not by 100 ([https://en.wikipedia.org/wiki/Leap_year](https://en.wikipedia.org/wiki/Leap_year)). | The only line contains integer *y* (1000<=β€<=*y*<=<<=100'000) β the year of the calendar. | Print the only integer *y*' β the next year after *y* when the calendar will be the same. Note that you should find the first year after *y* with the same calendar. | [
"2016\n",
"2000\n",
"50501\n"
] | [
"2044\n",
"2028\n",
"50507\n"
] | Today is Monday, the 13th of June, 2016. | [
{
"input": "2016",
"output": "2044"
},
{
"input": "2000",
"output": "2028"
},
{
"input": "50501",
"output": "50507"
},
{
"input": "1000",
"output": "1006"
},
{
"input": "1900",
"output": "1906"
},
{
"input": "1899",
"output": "1905"
},
{
"i... | 155 | 0 | 3 | 26,911 | |
911 | Stack Sorting | [
"constructive algorithms",
"data structures",
"greedy",
"implementation"
] | null | null | Let's suppose you have an array *a*, a stack *s* (initially empty) and an array *b* (also initially empty).
You may perform the following operations until both *a* and *s* are empty:
- Take the first element of *a*, push it into *s* and remove it from *a* (if *a* is not empty); - Take the top element from *s*, append it to the end of array *b* and remove it from *s* (if *s* is not empty).
You can perform these operations in arbitrary order.
If there exists a way to perform the operations such that array *b* is sorted in non-descending order in the end, then array *a* is called stack-sortable.
For example, [3,<=1,<=2] is stack-sortable, because *b* will be sorted if we perform the following operations:
1. Remove 3 from *a* and push it into *s*; 1. Remove 1 from *a* and push it into *s*; 1. Remove 1 from *s* and append it to the end of *b*; 1. Remove 2 from *a* and push it into *s*; 1. Remove 2 from *s* and append it to the end of *b*; 1. Remove 3 from *s* and append it to the end of *b*.
After all these operations *b*<==<=[1,<=2,<=3], so [3,<=1,<=2] is stack-sortable. [2,<=3,<=1] is not stack-sortable.
You are given *k* first elements of some permutation *p* of size *n* (recall that a permutation of size *n* is an array of size *n* where each integer from 1 to *n* occurs exactly once). You have to restore the remaining *n*<=-<=*k* elements of this permutation so it is stack-sortable. If there are multiple answers, choose the answer such that *p* is lexicographically maximal (an array *q* is lexicographically greater than an array *p* iff there exists some integer *k* such that for every *i*<=<<=*k* *q**i*<==<=*p**i*, and *q**k*<=><=*p**k*). You may not swap or change any of first *k* elements of the permutation.
Print the lexicographically maximal permutation *p* you can obtain.
If there exists no answer then output -1. | The first line contains two integers *n* and *k* (2<=β€<=*n*<=β€<=200000, 1<=β€<=*k*<=<<=*n*) β the size of a desired permutation, and the number of elements you are given, respectively.
The second line contains *k* integers *p*1, *p*2, ..., *p**k* (1<=β€<=*p**i*<=β€<=*n*) β the first *k* elements of *p*. These integers are pairwise distinct. | If it is possible to restore a stack-sortable permutation *p* of size *n* such that the first *k* elements of *p* are equal to elements given in the input, print lexicographically maximal such permutation.
Otherwise print -1. | [
"5 3\n3 2 1\n",
"5 3\n2 3 1\n",
"5 1\n3\n",
"5 2\n3 4\n"
] | [
"3 2 1 5 4 ",
"-1\n",
"3 2 1 5 4 ",
"-1\n"
] | none | [
{
"input": "5 3\n3 2 1",
"output": "3 2 1 5 4 "
},
{
"input": "5 3\n2 3 1",
"output": "-1"
},
{
"input": "5 1\n3",
"output": "3 2 1 5 4 "
},
{
"input": "5 2\n3 4",
"output": "-1"
},
{
"input": "100000 1\n98419",
"output": "98419 98418 98417 98416 98415 98414 9... | 576 | 23,040,000 | 0 | 26,946 | |
0 | none | [
"none"
] | null | null | There is a social website with *n* fanpages, numbered 1 through *n*. There are also *n* companies, and the *i*-th company owns the *i*-th fanpage.
Recently, the website created a feature called following. Each fanpage must choose exactly one other fanpage to follow.
The website doesnβt allow a situation where *i* follows *j* and at the same time *j* follows *i*. Also, a fanpage can't follow itself.
Letβs say that fanpage *i* follows some other fanpage *j*0. Also, letβs say that *i* is followed by *k* other fanpages *j*1,<=*j*2,<=...,<=*j**k*. Then, when people visit fanpage *i* they see ads from *k*<=+<=2 distinct companies: *i*,<=*j*0,<=*j*1,<=...,<=*j**k*. Exactly *t**i* people subscribe (like) the *i*-th fanpage, and each of them will click exactly one add. For each of *k*<=+<=1 companies *j*0,<=*j*1,<=...,<=*j**k*, exactly people will click their ad. Remaining people will click an ad from company *i* (the owner of the fanpage).
The total income of the company is equal to the number of people who click ads from this copmany.
Limak and Radewoosh ask you for help. Initially, fanpage *i* follows fanpage *f**i*. Your task is to handle *q* queries of three types:
- 1 i jΒ β fanpage *i* follows fanpage *j* from now. It's guaranteed that *i* didn't follow *j* just before the query. Note an extra constraint for the number of queries of this type (below, in the Input section). - 2 iΒ β print the total income of the *i*-th company. - 3Β β print two integers: the smallest income of one company and the biggest income of one company. | The first line of the input contains two integers *n* and *q* (3<=β€<=*n*<=β€<=100<=000, 1<=β€<=*q*<=β€<=100<=000)Β β the number of fanpages and the number of queries, respectively.
The second line contains *n* integers *t*1,<=*t*2,<=...,<=*t**n* (1<=β€<=*t**i*<=β€<=1012) where *t**i* denotes the number of people subscribing the *i*-th fanpage.
The third line contains *n* integers *f*1,<=*f*2,<=...,<=*f**n* (1<=β€<=*f**i*<=β€<=*n*). Initially, fanpage *i* follows fanpage *f**i*.
Then, *q* lines follow. The *i*-th of them describes the *i*-th query. The first number in the line is an integer *type**i* (1<=β€<=*type**i*<=β€<=3)Β β the type of the query.
There will be at most 50<=000 queries of the first type. There will be at least one query of the second or the third type (so, the output won't be empty).
It's guaranteed that at each moment a fanpage doesn't follow itself, and that no two fanpages follow each other. | For each query of the second type print one integer in a separate line - the total income of the given company. For each query of the third type print two integers in a separate line - the minimum and the maximum total income, respectively. | [
"5 12\n10 20 30 40 50\n2 3 4 5 2\n2 1\n2 2\n2 3\n2 4\n2 5\n1 4 2\n2 1\n2 2\n2 3\n2 4\n2 5\n3\n"
] | [
"10\n36\n28\n40\n36\n9\n57\n27\n28\n29\n9 57\n"
] | In the sample test, there are 5 fanpages. The *i*-th of them has *i*Β·10 subscribers.
On drawings, numbers of subscribers are written in circles. An arrow from *A* to *B* means that *A* follows *B*.
The left drawing shows the initial situation. The first company gets income <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/aa287c04033fd5717b34060017a16ed1930b6a77.png" style="max-width: 100.0%;max-height: 100.0%;"/> from its own fanpage, and gets income <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/545b52a4f4acc0a81e4c81f01a57750f8d8a5045.png" style="max-width: 100.0%;max-height: 100.0%;"/> from the 2-nd fanpage. So, the total income is 5β+β5β=β10. After the first query ("2 1") you should print 10.
The right drawing shows the situation after a query "1 4 2" (after which fanpage 4 follows fanpage 2). Then, the first company still gets income 5 from its own fanpage, but now it gets only <img align="middle" class="tex-formula" src="https://espresso.codeforces.com/08e222f472ebd19466616b7774c41626722a1995.png" style="max-width: 100.0%;max-height: 100.0%;"/> from the 2-nd fanpage. So, the total income is 5β+β4β=β9 now. | [] | 46 | 0 | 0 | 26,953 | |
191 | Fools and Roads | [
"data structures",
"dfs and similar",
"trees"
] | null | null | They say that Berland has exactly two problems, fools and roads. Besides, Berland has *n* cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each pair of cities there is no more than one simple path (or else the fools would get lost).
But that is not the end of Berland's special features. In this country fools sometimes visit each other and thus spoil the roads. The fools aren't very smart, so they always use only the simple paths.
A simple path is the path which goes through every Berland city not more than once.
The Berland government knows the paths which the fools use. Help the government count for each road, how many distinct fools can go on it.
Note how the fools' paths are given in the input. | The first line contains a single integer *n* (2<=β€<=*n*<=β€<=105) β the number of cities.
Each of the next *n*<=-<=1 lines contains two space-separated integers *u**i*,<=*v**i* (1<=β€<=*u**i*,<=*v**i*<=β€<=*n*, *u**i*<=β <=*v**i*), that means that there is a road connecting cities *u**i* and *v**i*.
The next line contains integer *k* (0<=β€<=*k*<=β€<=105) β the number of pairs of fools who visit each other.
Next *k* lines contain two space-separated numbers. The *i*-th line (*i*<=><=0) contains numbers *a**i*,<=*b**i* (1<=β€<=*a**i*,<=*b**i*<=β€<=*n*). That means that the fool number 2*i*<=-<=1 lives in city *a**i* and visits the fool number 2*i*, who lives in city *b**i*. The given pairs describe simple paths, because between every pair of cities there is only one simple path. | Print *n*<=-<=1 integer. The integers should be separated by spaces. The *i*-th number should equal the number of fools who can go on the *i*-th road. The roads are numbered starting from one in the order, in which they occur in the input. | [
"5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5\n",
"5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5\n"
] | [
"2 1 1 1 \n",
"3 1 1 1 \n"
] | In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones.
In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one goes fool number 1. | [
{
"input": "5\n1 2\n1 3\n2 4\n2 5\n2\n1 4\n3 5",
"output": "2 1 1 1 "
},
{
"input": "5\n3 4\n4 5\n1 4\n2 4\n3\n2 3\n1 3\n3 5",
"output": "3 1 1 1 "
}
] | 966 | 49,561,600 | -1 | 26,957 | |
761 | Dasha and Password | [
"brute force",
"dp",
"implementation"
] | null | null | After overcoming the stairs Dasha came to classes. She needed to write a password to begin her classes. The password is a string of length *n* which satisfies the following requirements:
- There is at least one digit in the string, - There is at least one lowercase (small) letter of the Latin alphabet in the string, - There is at least one of three listed symbols in the string: '#', '*', '&'.
Considering that these are programming classes it is not easy to write the password.
For each character of the password we have a fixed string of length *m*, on each of these *n* strings there is a pointer on some character. The *i*-th character displayed on the screen is the pointed character in the *i*-th string. Initially, all pointers are on characters with indexes 1 in the corresponding strings (all positions are numbered starting from one).
During one operation Dasha can move a pointer in one string one character to the left or to the right. Strings are cyclic, it means that when we move the pointer which is on the character with index 1 to the left, it moves to the character with the index *m*, and when we move it to the right from the position *m* it moves to the position 1.
You need to determine the minimum number of operations necessary to make the string displayed on the screen a valid password. | The first line contains two integers *n*, *m* (3<=β€<=*n*<=β€<=50,<=1<=β€<=*m*<=β€<=50) β the length of the password and the length of strings which are assigned to password symbols.
Each of the next *n* lines contains the string which is assigned to the *i*-th symbol of the password string. Its length is *m*, it consists of digits, lowercase English letters, and characters '#', '*' or '&'.
You have such input data that you can always get a valid password. | Print one integer β the minimum number of operations which is necessary to make the string, which is displayed on the screen, a valid password. | [
"3 4\n1**2\na3*0\nc4**\n",
"5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&\n"
] | [
"1\n",
"3\n"
] | In the first test it is necessary to move the pointer of the third string to one left to get the optimal answer.
In the second test one of possible algorithms will be:
- to move the pointer of the second symbol once to the right. - to move the pointer of the third symbol twice to the right. | [
{
"input": "3 4\n1**2\na3*0\nc4**",
"output": "1"
},
{
"input": "5 5\n#*&#*\n*a1c&\n&q2w*\n#a3c#\n*&#*&",
"output": "3"
},
{
"input": "5 2\n&l\n*0\n*9\n*#\n#o",
"output": "2"
},
{
"input": "25 16\nvza**ooxkmd#*ywa\ndip#*#&ef&z&&&pv\nwggob&&72#*&&nku\nrsb##*&jm&#u**te\nzif#lu#... | 155 | 819,200 | 0 | 27,032 | |
159 | Friends or Not | [
"*special",
"greedy",
"implementation"
] | null | null | Polycarpus has a hobby β he develops an unusual social network. His work is almost completed, and there is only one more module to implement β the module which determines friends. Oh yes, in this social network one won't have to add friends manually! Pairs of friends are deduced in the following way. Let's assume that user *A* sent user *B* a message at time *t*1, and user *B* sent user *A* a message at time *t*2. If 0<=<<=*t*2<=-<=*t*1<=β€<=*d*, then user *B*'s message was an answer to user *A*'s one. Users *A* and *B* are considered to be friends if *A* answered at least one *B*'s message or *B* answered at least one *A*'s message.
You are given the log of messages in chronological order and a number *d*. Find all pairs of users who will be considered to be friends. | The first line of the input contains two integers *n* and *d* (1<=β€<=*n*,<=*d*<=β€<=1000). The next *n* lines contain the messages log. The *i*-th line contains one line of the log formatted as "*A**i* *B**i* *t**i*" (without the quotes), which means that user *A**i* sent a message to user *B**i* at time *t**i* (1<=β€<=*i*<=β€<=*n*). *A**i* and *B**i* are non-empty strings at most 20 characters long, consisting of lowercase letters ('a' ... 'z'), and *t**i* is an integer (0<=β€<=*t**i*<=β€<=10000). It is guaranteed that the lines are given in non-decreasing order of *t**i*'s and that no user sent a message to himself. The elements in the lines are separated by single spaces. | In the first line print integer *k* β the number of pairs of friends. In the next *k* lines print pairs of friends as "*A**i* *B**i*" (without the quotes). You can print users in pairs and the pairs themselves in any order. Each pair must be printed exactly once. | [
"4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4\n",
"1 1000\na b 0\n"
] | [
"1\npetya vasya\n",
"0\n"
] | In the first sample test case Vasya and Petya are friends because their messages' sending times are one second apart. Anya and Ivan are not, because their messages' sending times differ by more than one second. | [
{
"input": "4 1\nvasya petya 1\npetya vasya 2\nanya ivan 2\nivan anya 4",
"output": "1\npetya vasya"
},
{
"input": "1 1000\na b 0",
"output": "0"
},
{
"input": "2 1\na b 0\nb a 0",
"output": "0"
},
{
"input": "3 1\na b 1\nb c 2\nc d 3",
"output": "0"
},
{
"input":... | 1,122 | 6,963,200 | 3 | 27,041 | |
38 | Smart Boy | [
"dp",
"games",
"strings"
] | F. Smart Boy | 4 | 256 | Once Petya and Vasya invented a new game and called it "Smart Boy". They located a certain set of words β the dictionary β for the game. It is admissible for the dictionary to contain similar words.
The rules of the game are as follows: first the first player chooses any letter (a word as long as 1) from any word from the dictionary and writes it down on a piece of paper. The second player adds some other letter to this one's initial or final position, thus making a word as long as 2, then it's the first player's turn again, he adds a letter in the beginning or in the end thus making a word as long as 3 and so on. But the player mustn't break one condition: the newly created word must be a substring of a word from a dictionary. The player who can't add a letter to the current word without breaking the condition loses.
Also if by the end of a turn a certain string *s* is written on paper, then the player, whose turn it just has been, gets a number of points according to the formula:
where
- is a sequence number of symbol *c* in Latin alphabet, numbered starting from 1. For example, , and . - is the number of words from the dictionary where the line *s* occurs as a substring at least once.
Your task is to learn who will win the game and what the final score will be. Every player plays optimally and most of all tries to win, then β to maximize the number of his points, then β to minimize the number of the points of the opponent. | The first input line contains an integer *n* which is the number of words in the located dictionary (1<=β€<=*n*<=β€<=30). The *n* lines contain the words from the dictionary β one word is written on one line. Those lines are nonempty, consisting of Latin lower-case characters no longer than 30 characters. Equal words can be in the list of words. | On the first output line print a line "First" or "Second" which means who will win the game. On the second line output the number of points of the first player and the number of points of the second player after the game ends. Separate the numbers by a single space. | [
"2\naba\nabac\n",
"3\nartem\nnik\nmax\n"
] | [
"Second\n29 35\n",
"First\n2403 1882\n"
] | none | [
{
"input": "2\naba\nabac",
"output": "Second\n29 35"
},
{
"input": "3\nartem\nnik\nmax",
"output": "First\n2403 1882"
},
{
"input": "1\njyi",
"output": "First\n1727 876"
},
{
"input": "2\naz\nkagim",
"output": "First\n1082 678"
},
{
"input": "3\nskz\nsauy\nrxu",
... | 92 | 0 | 0 | 27,076 |
257 | Greedy Elevator | [
"data structures",
"implementation"
] | null | null | The *m*-floor (*m*<=><=1) office of international corporation CodeForces has the advanced elevator control system established. It works as follows.
All office floors are sequentially numbered with integers from 1 to *m*. At time *t*<==<=0, the elevator is on the first floor, the elevator is empty and nobody is waiting for the elevator on other floors. Next, at times *t**i* (*t**i*<=><=0) people come to the elevator. For simplicity, we assume that one person uses the elevator only once during the reported interval. For every person we know three parameters: the time at which the person comes to the elevator, the floor on which the person is initially, and the floor to which he wants to go.
The movement of the elevator between the floors is as follows. At time *t* (*t*<=β₯<=0, *t* is an integer) the elevator is always at some floor. First the elevator releases all people who are in the elevator and want to get to the current floor. Then it lets in all the people waiting for the elevator on this floor. If a person comes to the elevator exactly at time *t*, then he has enough time to get into it. We can assume that all of these actions (going in or out from the elevator) are made instantly. After that the elevator decides, which way to move and at time (*t*<=+<=1) the elevator gets to the selected floor.
The elevator selects the direction of moving by the following algorithm.
- If the elevator is empty and at the current time no one is waiting for the elevator on any floor, then the elevator remains at the current floor. - Otherwise, let's assume that the elevator is on the floor number *x* (1<=β€<=*x*<=β€<=*m*). Then elevator calculates the directions' "priorities" *p**up* and *p**down*: *p**up* is the sum of the number of people waiting for the elevator on the floors with numbers greater than *x*, and the number of people in the elevator, who want to get to the floors with the numbers greater than *x*; *p**down* is the sum of the number of people waiting for the elevator on the floors with numbers less than *x*, and the number of people in the elevator, who want to get to the floors with the numbers less than *x*. If *p**up*<=β₯<=*p**down*, then the elevator goes one floor above the current one (that is, from floor *x* to floor *x*<=+<=1), otherwise the elevator goes one floor below the current one (that is, from floor *x* to floor *x*<=-<=1).
Your task is to simulate the work of the elevator and for each person to tell the time when the elevator will get to the floor this person needs. Please note that the elevator is large enough to accommodate all the people at once. | The first line contains two space-separated integers: *n*,<=*m* (1<=β€<=*n*<=β€<=105,<=2<=β€<=*m*<=β€<=105) β the number of people and floors in the building, correspondingly.
Next *n* lines each contain three space-separated integers: *t**i*,<=*s**i*,<=*f**i* (1<=β€<=*t**i*<=β€<=109,<=1<=β€<=*s**i*,<=*f**i*<=β€<=*m*,<=*s**i*<=β <=*f**i*) β the time when the *i*-th person begins waiting for the elevator, the floor number, where the *i*-th person was initially located, and the number of the floor, where he wants to go. | Print *n* lines. In the *i*-th line print a single number β the moment of time, when the *i*-th person gets to the floor he needs. The people are numbered in the order, in which they are given in the input.
Please don't use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | [
"3 10\n1 2 7\n3 6 5\n3 4 8\n",
"2 10\n1 2 5\n7 4 5\n"
] | [
"7\n11\n8\n",
"5\n9\n"
] | In the first sample the elevator worked as follows:
- *t*β=β1. The elevator is on the floor number 1. The elevator is empty. The floor number 2 has one person waiting. *p*<sub class="lower-index">*up*</sub>β=β1β+β0β=β1,β*p*<sub class="lower-index">*down*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*up*</sub>ββ₯β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 2. - *t*β=β2. The elevator is on the floor number 2. One person enters the elevator, he wants to go to the floor number 7. *p*<sub class="lower-index">*up*</sub>β=β0β+β1β=β1,β*p*<sub class="lower-index">*down*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*up*</sub>ββ₯β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 3. - *t*β=β3. The elevator is on the floor number 3. There is one person in the elevator, he wants to go to floor 7. The floors number 4 and 6 have two people waiting for the elevator. *p*<sub class="lower-index">*up*</sub>β=β2β+β1β=β3,β*p*<sub class="lower-index">*down*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*up*</sub>ββ₯β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 4. - *t*β=β4. The elevator is on the floor number 4. There is one person in the elevator who wants to go to the floor number 7. One person goes into the elevator, he wants to get to the floor number 8. The floor number 6 has one man waiting. *p*<sub class="lower-index">*up*</sub>β=β1β+β2β=β3,β*p*<sub class="lower-index">*down*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*up*</sub>ββ₯β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 5. - *t*β=β5. The elevator is on the floor number 5. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. There is one person waiting for the elevator on the floor number 6. *p*<sub class="lower-index">*up*</sub>β=β1β+β2β=β3,β*p*<sub class="lower-index">*down*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*up*</sub>ββ₯β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 6. - *t*β=β6. The elevator is on the floor number 6. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. One man enters the elevator, he wants to get to the floor number 5. *p*<sub class="lower-index">*up*</sub>β=β0β+β2β=β2,β*p*<sub class="lower-index">*down*</sub>β=β0β+β1β=β1,β*p*<sub class="lower-index">*up*</sub>ββ₯β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 7. - *t*β=β7. The elevator is on the floor number 7. One person leaves the elevator, this person wanted to get to the floor number 7. There are two people in the elevator, they want to get to the floors with numbers 8 and 5, correspondingly. *p*<sub class="lower-index">*up*</sub>β=β0β+β1β=β1,β*p*<sub class="lower-index">*down*</sub>β=β0β+β1β=β1,β*p*<sub class="lower-index">*up*</sub>ββ₯β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 8. - *t*β=β8. The elevator is on the floor number 8. One person leaves the elevator, this person wanted to go to the floor number 8. There is one person in the elevator, he wants to go to the floor number 5. *p*<sub class="lower-index">*up*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*down*</sub>β=β0β+β1β=β1,β*p*<sub class="lower-index">*up*</sub>β<β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 7. - *t*β=β9. The elevator is on the floor number 7. There is one person in the elevator, this person wants to get to the floor number 5. *p*<sub class="lower-index">*up*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*down*</sub>β=β0β+β1β=β1,β*p*<sub class="lower-index">*up*</sub>β<β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 6. - *t*β=β10. The elevator is on the floor number 6. There is one person in the elevator, he wants to get to the floor number 5. *p*<sub class="lower-index">*up*</sub>β=β0β+β0β=β0,β*p*<sub class="lower-index">*down*</sub>β=β0β+β1β=β1,β*p*<sub class="lower-index">*up*</sub>β<β*p*<sub class="lower-index">*down*</sub>. So the elevator goes to the floor number 5. - *t*β=β11. The elevator is on the floor number 5. One person leaves the elevator, this person initially wanted to get to the floor number 5. The elevator is empty and nobody needs it, so the elevator remains at the floor number 5. | [] | 60 | 0 | 0 | 27,136 | |
76 | Gift | [
"dsu",
"graphs",
"sortings",
"trees"
] | A. Gift | 2 | 256 | The kingdom of Olympia consists of *N* cities and *M* bidirectional roads. Each road connects exactly two cities and two cities can be connected with more than one road. Also it possible that some roads connect city with itself making a loop.
All roads are constantly plundered with bandits. After a while bandits became bored of wasting time in road robberies, so they suggested the king of Olympia to pay off. According to the offer, bandits want to get a gift consisted of gold and silver coins. Offer also contains a list of restrictions: for each road it is known *g**i* β the smallest amount of gold and *s**i* β the smallest amount of silver coins that should be in the gift to stop robberies on the road. That is, if the gift contains *a* gold and *b* silver coins, then bandits will stop robberies on all the roads that *g**i*<=β€<=*a* and *s**i*<=β€<=*b*.
Unfortunately kingdom treasury doesn't contain neither gold nor silver coins, but there are Olympian tugriks in it. The cost of one gold coin in tugriks is *G*, and the cost of one silver coin in tugriks is *S*. King really wants to send bandits such gift that for any two cities there will exist a safe path between them. Your task is to find the minimal cost in Olympian tugriks of the required gift. | The first line of the input contains two integers *N* and *M* (2<=β€<=*N*<=β€<=200, 1<=β€<=*M*<=β€<=50<=000) β the number of cities and the number of roads, respectively. The second line contains two integers *G* and *S* (1<=β€<=*G*,<=*S*<=β€<=109) β the prices of gold and silver coins in tugriks. The following *M* lines contain information about the offer. Each of the records in list is given as four integers *x**i*,<=*y**i*,<=*g**i*,<=*s**i*, where *x**i* and *y**i* are the numbers of cities that the road connects and *g**i*, *s**i* are minimal gold and silver coins requirements for the *i*-th road (1<=β€<=*x**i*,<=*y**i*<=β€<=*N*, 1<=β€<=*g**i*,<=*s**i*<=β€<=109). Cities are numbered from 1 to *N*. It is possible that there are more than one road between a pair of cities. It is possible that a road connects the city with itself. | The output should contain the minimal cost of the gift in Olympian tugriks. If there is no gift that satisfies the given requirements output . | [
"3 3\n2 1\n1 2 10 15\n1 2 4 20\n1 3 5 1\n"
] | [
"30\n"
] | none | [
{
"input": "3 3\n2 1\n1 2 10 15\n1 2 4 20\n1 3 5 1",
"output": "30"
}
] | 92 | 1,740,800 | 0 | 27,241 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.